Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
ObsidianOS
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ObsidianOS
ObsidianOS
Commits
931b705a
Commit
931b705a
authored
Jun 20, 2020
by
Nifou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Execute a binary with the rust's libstd!
parent
c6bd772f
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
190 additions
and
21 deletions
+190
-21
kernel/Cargo.lock
kernel/Cargo.lock
+1
-0
kernel/Cargo.toml
kernel/Cargo.toml
+1
-0
kernel/src/arch/x86_64/boot.asm
kernel/src/arch/x86_64/boot.asm
+1
-1
kernel/src/elf/loader.rs
kernel/src/elf/loader.rs
+1
-1
kernel/src/initfs.rs
kernel/src/initfs.rs
+4
-0
kernel/src/lib.rs
kernel/src/lib.rs
+1
-0
kernel/src/memory/mod.rs
kernel/src/memory/mod.rs
+1
-1
kernel/src/syscall/memory.rs
kernel/src/syscall/memory.rs
+111
-0
kernel/src/syscall/misc.rs
kernel/src/syscall/misc.rs
+39
-0
kernel/src/syscall/mod.rs
kernel/src/syscall/mod.rs
+14
-10
kernel/src/tasking/memory.rs
kernel/src/tasking/memory.rs
+0
-8
userspace/src/bins/test-std/Cargo.lock
userspace/src/bins/test-std/Cargo.lock
+5
-0
userspace/src/bins/test-std/Cargo.toml
userspace/src/bins/test-std/Cargo.toml
+5
-0
userspace/src/bins/test-std/Makefile
userspace/src/bins/test-std/Makefile
+3
-0
userspace/src/bins/test-std/src/main.rs
userspace/src/bins/test-std/src/main.rs
+3
-0
No files found.
kernel/Cargo.lock
View file @
931b705a
...
...
@@ -85,6 +85,7 @@ name = "obsidianos-kernel"
version = "0.0.0"
dependencies = [
"bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"goblin 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"linked_list_allocator 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
...
...
kernel/Cargo.toml
View file @
931b705a
...
...
@@ -13,6 +13,7 @@ linked_list_allocator = "0.8.4"
multiboot2
=
"0.8.2"
bit_field
=
"0.10.0"
x86
=
"0.33.0"
bitflags
=
"1.2.1"
[dependencies.goblin]
default-features
=
false
...
...
kernel/src/arch/x86_64/boot.asm
View file @
931b705a
...
...
@@ -119,7 +119,7 @@ set_up_page_tables:
mov
[(
p2_table
-
0xc0000000
)
+
ecx
*
8
],
eax
; map ecx-th entry
inc
ecx
; increase counter
cmp
ecx
,
3
; the kernel size is 0x400000 (see memory/mod.rs:KERNEL_SIZE), so 0x400000 / 0x200000 + 1 = 3
cmp
ecx
,
6
; the kernel size is 0x800000 (see memory/mod.rs:KERNEL_SIZE), so 0x800000 / 0x200000 + 1 = 6
jne
.map_p2_table
; else map the next entry
ret
...
...
kernel/src/elf/loader.rs
View file @
931b705a
...
...
@@ -127,7 +127,7 @@ impl<'a> ElfLoader<'a> {
// Map the binary in the kernel's address space
let
mappings
=
memory
::
map
(
start
,
start
+
ph
.p_
file
sz
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
);
memory
::
map
(
start
,
start
+
ph
.p_
mem
sz
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
);
// Copy the binary in memory
unsafe
{
...
...
kernel/src/initfs.rs
View file @
931b705a
...
...
@@ -75,6 +75,10 @@ impl InitFS {
String
::
from
(
"/bin/hello2"
),
include_bytes!
(
"../../root/bin/hello2"
)
as
&
[
u8
],
);
files
.insert
(
String
::
from
(
"/bin/test-std"
),
include_bytes!
(
"../../root/bin/test-std"
)
as
&
[
u8
],
);
files
.insert
(
String
::
from
(
"/README.md"
),
include_bytes!
(
"../../README.md"
)
as
&
[
u8
],
...
...
kernel/src/lib.rs
View file @
931b705a
...
...
@@ -135,6 +135,7 @@ pub extern "C" fn kmain(info_addr: usize) {
elf
::
load
(
String
::
from
(
"/bin/hello"
));
elf
::
load
(
String
::
from
(
"/bin/hello2"
));
elf
::
load
(
String
::
from
(
"/bin/test-std"
));
println!
(
"ObsidianOS is ready!"
);
x86_64
::
instructions
::
interrupts
::
enable
();
...
...
kernel/src/memory/mod.rs
View file @
931b705a
...
...
@@ -55,7 +55,7 @@ pub const KERNEL_START_VIRT: u64 = 0xc0000000;
pub
const
KERNEL_START_PHYS
:
u64
=
0
;
/// The size of the kernel
pub
const
KERNEL_SIZE
:
u64
=
0x
4
00000
;
pub
const
KERNEL_SIZE
:
u64
=
0x
8
00000
;
lazy_static!
{
/// A global kernel memory mapper
...
...
kernel/src/syscall/memory.rs
0 → 100644
View file @
931b705a
/*
* Copyright (C) 2020 Nicolas Fouquet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Syscalls used to manage the memory
use
bitflags
::
bitflags
;
use
x86_64
::
structures
::
paging
::
page_table
::
PageTableFlags
as
Flags
;
use
crate
::
tasking
::
scheduler
::
SCHEDULER
;
use
super
::
Syscall
;
bitflags!
{
struct
MmapFlags
:
u32
{
const
SHARED
=
1
<<
0
;
const
PRIVATE
=
1
<<
1
;
const
FIXED
=
1
<<
4
;
const
ANONYMOUS
=
1
<<
5
;
}
}
bitflags!
{
struct
MmapProt
:
u32
{
const
NONE
=
0
;
const
READ
=
1
<<
0
;
const
WRITE
=
1
<<
1
;
const
EXEC
=
1
<<
2
;
}
}
impl
From
<
MmapProt
>
for
Flags
{
fn
from
(
prot
:
MmapProt
)
->
Flags
{
let
mut
flags
=
Flags
::
NO_EXECUTE
;
if
prot
.contains
(
MmapProt
::
READ
)
{
flags
.insert
(
Flags
::
PRESENT
);
}
if
prot
.contains
(
MmapProt
::
WRITE
)
{
flags
.insert
(
Flags
::
WRITABLE
);
}
if
prot
.contains
(
MmapProt
::
EXEC
)
{
flags
.remove
(
Flags
::
NO_EXECUTE
);
}
flags
.insert
(
Flags
::
USER_ACCESSIBLE
);
flags
}
}
impl
<
'a
>
Syscall
<
'a
>
{
/// Map files or devices into memory
pub
fn
mmap
(
&
self
,
addr
:
usize
,
len
:
usize
,
prot
:
usize
,
flags
:
usize
,
fd
:
usize
,
offset
:
usize
)
->
usize
{
println!
(
"mmap({:#x}, {}, {}, {}, {:#x}, {})"
,
addr
,
len
,
prot
,
flags
,
fd
,
offset
);
let
prot
=
MmapProt
::
from_bits_truncate
(
prot
as
u32
);
let
flags
=
MmapFlags
::
from_bits_truncate
(
flags
as
u32
);
if
flags
.contains
(
MmapFlags
::
ANONYMOUS
)
{
if
addr
==
0
{
crate
::
memory
::
switch_to_kernel_page_table
();
unsafe
{
SCHEDULER
.force_unlock
();
}
SCHEDULER
.lock
()
.get_current_mut
()
.memory
.map
(
0x300000
,
0x300000
+
len
as
u64
,
Flags
::
from
(
prot
),
Flags
::
PRESENT
);
SCHEDULER
.lock
()
.get_current_mut
()
.memory
.switch_page_table
();
0x300000
}
else
{
addr
}
}
else
{
0
}
}
/// Change data segment size
pub
fn
brk
(
&
self
,
addr
:
usize
)
->
usize
{
print!
(
"brk({:#x}) = "
,
addr
);
if
addr
==
0
{
println!
(
"0x10000000"
);
crate
::
memory
::
switch_to_kernel_page_table
();
unsafe
{
SCHEDULER
.force_unlock
();
}
SCHEDULER
.lock
()
.get_current_mut
()
.memory
.map
(
0x10000000
,
0x10004000
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
,
Flags
::
PRESENT
);
SCHEDULER
.lock
()
.get_current_mut
()
.memory
.switch_page_table
();
0x10000000
}
else
{
println!
(
"{:#x}"
,
addr
);
addr
}
}
}
kernel/src/syscall/misc.rs
0 → 100644
View file @
931b705a
/*
* Copyright (C) 2020 Nicolas Fouquet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Other syscalls
use
x86_64
::{
VirtAddr
,
registers
::
model_specific
::
FsBase
,
};
use
super
::
Syscall
;
impl
<
'a
>
Syscall
<
'a
>
{
/// Set og get some architecture specific registers
pub
fn
arch_prctl
(
&
mut
self
,
code
:
usize
,
addr
:
usize
)
->
usize
{
println!
(
"arch_prctl({:#x}, {:#x}) = 0"
,
code
,
addr
);
const
ARCH_SET_FS
:
usize
=
0x1002
;
match
code
{
ARCH_SET_FS
=>
{
unsafe
{
x86
::
msr
::
wrmsr
(
x86
::
msr
::
IA32_FS_BASE
,
addr
as
u64
);
}
},
_
=>
(),
}
0
}
}
kernel/src/syscall/mod.rs
View file @
931b705a
...
...
@@ -26,15 +26,14 @@ use crate::tasking::{
pub
mod
process
;
pub
mod
fs
;
pub
mod
memory
;
pub
mod
misc
;
#[allow(dead_code)]
#[repr(packed)]
#[derive(Debug,
Clone,
Copy)]
/// The stack which contains the registers saved and restored during a syscall
pub
struct
SyscallStack
{
/// The FS register
pub
fs
:
usize
,
/// The R11 register
pub
r11
:
usize
,
...
...
@@ -125,10 +124,7 @@ pub unsafe extern fn syscall() {
push r8
push r9
push r10
push r11
push fs
mov r11, 0x10
mov fs, r11"
push r11"
:
:
:
:
"intel"
,
"volatile"
);
// Get reference to stack variables
...
...
@@ -142,8 +138,7 @@ pub unsafe extern fn syscall() {
// of a function is stored in RAX, so nothing to do!
// Interrupt return
llvm_asm!
(
"pop fs
pop r11
llvm_asm!
(
"pop r11
pop r10
pop r9
pop r8
...
...
@@ -159,7 +154,10 @@ pub unsafe extern fn syscall() {
// --- Syscall numbers ---
const
WRITE
:
usize
=
1
;
const
MMAP
:
usize
=
9
;
const
BRK
:
usize
=
12
;
const
EXIT
:
usize
=
60
;
const
ARCH_PRCTL
:
usize
=
158
;
/// A structure used to call the right handler for each syscall
pub
struct
Syscall
<
'a
>
{
...
...
@@ -183,8 +181,14 @@ impl<'a> Syscall<'a> {
match
num
{
WRITE
=>
self
.write
(
args
[
0
],
args
[
1
],
args
[
2
]),
MMAP
=>
self
.mmap
(
args
[
0
],
args
[
1
],
args
[
2
],
args
[
3
],
args
[
4
],
args
[
5
]),
BRK
=>
self
.brk
(
args
[
0
]),
EXIT
=>
self
.exit
(
args
[
0
]),
_
=>
0
,
ARCH_PRCTL
=>
self
.arch_prctl
(
args
[
0
],
args
[
1
]),
_
=>
{
println!
(
"Unknown syscall: {}"
,
num
);
0
},
}
}
}
kernel/src/tasking/memory.rs
View file @
931b705a
...
...
@@ -55,14 +55,6 @@ impl ProcessMemory {
let
frame
=
PhysFrame
::
containing_address
(
PhysAddr
::
new
(
memory
::
KERNEL_START_PHYS
+
i
as
u64
*
4096
));
unsafe
{
mapper
.map_to
(
page
,
frame
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
,
&
mut
*
memory
::
FRAME_ALLOCATOR
.r
#
try
()
.unwrap
()
.lock
())
.unwrap
()
.ignore
();
}
}
// Map the kernel's page table frame
if
let
Some
(
frame
)
=
memory
::
KERNEL_PAGE_TABLE
.r
#
try
()
{
unsafe
{
mapper
.identity_map
(
*
frame
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
,
&
mut
*
memory
::
FRAME_ALLOCATOR
.r
#
try
()
.unwrap
()
.lock
())
.unwrap
()
.ignore
();
}
}
// Map the table itself
unsafe
{
mapper
.identity_map
(
page_table_frame
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
,
&
mut
*
memory
::
FRAME_ALLOCATOR
.r
#
try
()
.unwrap
()
.lock
())
.unwrap
()
.ignore
();
}
});
Self
{
...
...
userspace/src/bins/test-std/Cargo.lock
0 → 100644
View file @
931b705a
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "test-std"
version = "0.0.0"
userspace/src/bins/test-std/Cargo.toml
0 → 100644
View file @
931b705a
[package]
name
=
"test-std"
version
=
"0.0.0"
authors
=
[
"Nifou <nfouquet@mailfence.com>"
]
edition
=
"2018"
userspace/src/bins/test-std/Makefile
0 → 100644
View file @
931b705a
all
:
@
cargo rustc
--target
x86_64-unknown-linux-musl
@
cp
target/x86_64-unknown-linux-musl/debug/test-std ../../../build/bins/test-std
userspace/src/bins/test-std/src/main.rs
0 → 100644
View file @
931b705a
fn
main
()
{
println!
(
"Hello, world!"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment