Skip to content

Commit e1ddf73

Browse files
authored
Isolate arch-specific and memory allocators into a separate library (kerla_runtime) (#77)
1 parent 8f2fce6 commit e1ddf73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1489
-1092
lines changed

Cargo.lock

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"runtime",
34
"kernel",
45
"libs/*",
56
]

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export RUSTFLAGS = -Z emit-stack-sizes
4747
CARGOFLAGS += -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem
4848
CARGOFLAGS += --target $(target_json)
4949
CARGOFLAGS += $(if $(RELEASE),--release,)
50-
TESTCARGOFLAGS += --package kerla -Z unstable-options
50+
TESTCARGOFLAGS += --package kerla_kernel -Z unstable-options
5151
TESTCARGOFLAGS += --config "target.$(ARCH).runner = './tools/run-unittests.sh'"
5252
WATCHFLAGS += --clear
53+
5354
export CARGO_FROM_MAKE=1
5455
export INITRAMFS_PATH
5556
export ARCH
@@ -62,7 +63,7 @@ export NM
6263
.PHONY: build
6364
build:
6465
$(MAKE) build-crate
65-
cp target/$(ARCH)/$(build_mode)/kerla $(kernel_elf)
66+
cp target/$(ARCH)/$(build_mode)/kerla_kernel $(kernel_elf)
6667

6768
$(PROGRESS) "NM" $(kernel_symbols)
6869
$(NM) $(kernel_elf) | rustfilt | awk '{ $$2=""; print $$0 }' > $(kernel_symbols)

kernel/Cargo.toml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
[package]
2-
name = "kerla"
2+
name = "kerla_kernel"
33
version = "0.0.4"
44
authors = ["Seiya Nuta <[email protected]>"]
55
edition = "2018"
66

77
[[bin]]
8-
name = "kerla"
8+
name = "kerla_kernel"
99
path = "main.rs"
1010

1111
[dependencies]
12-
kerla_utils = { path = "../libs/kerla_utils", features = ["no_std"] }
13-
spin = "0.9.2"
14-
x86 = "0.43.0"
1512
memoffset = "0.6.4"
16-
buddy_system_allocator = "0.8.0"
17-
arrayvec = { version = "0.7.2", default-features = false }
13+
log = "0.4"
14+
spin = "0.9.2"
1815
goblin = { version = "0.4", default-features = false, features = ["elf64"] }
16+
smoltcp = { version = "0.7.5", default-features = false, features = ["alloc", "proto-ipv4", "socket", "socket-raw", "socket-udp", "socket-tcp", "proto-dhcpv4", "ethernet"] }
17+
18+
# Data structues.
1919
bitflags = "1.3.2"
20+
arrayvec = { version = "0.7.2", default-features = false }
2021
hashbrown = { version = "0.11.2", features = ["nightly"] }
21-
log = "0.4"
2222
crossbeam = { version = "0.8.1", default-features = false, features = ["alloc"] }
23-
smoltcp = { version = "0.7.5", default-features = false, features = ["alloc", "proto-ipv4", "socket", "socket-raw", "socket-udp", "socket-tcp", "proto-dhcpv4", "ethernet"] }
24-
cfg-if = "1"
2523
atomic_refcell = "0.1.6"
26-
vte = "0.10"
24+
25+
# Arch-specific dependencies.
26+
x86 = "0.43.0"
27+
28+
# Local dependencies.
29+
kerla_runtime = { path = "../runtime" }
30+
kerla_utils = { path = "../libs/kerla_utils", features = ["no_std"] }

kernel/arch/x64/arch_prctl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const ARCH_SET_FS: i32 = 0x1002;
99

1010
pub fn arch_prctl(current: &Arc<Process>, code: i32, uaddr: UserVAddr) -> Result<()> {
1111
match code {
12-
// TODO: Move to arch directory.
1312
ARCH_SET_FS => {
1413
let value = uaddr.value() as u64;
1514
current.arch().fsbase.store(value);

kernel/arch/x64/mod.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,14 @@
1-
global_asm!(include_str!("boot.S"));
2-
global_asm!(include_str!("trap.S"));
3-
global_asm!(include_str!("usercopy.S"));
1+
use kerla_runtime::{address::UserVAddr, arch::PAGE_SIZE};
2+
3+
global_asm!(include_str!("usermode.S"));
44

5-
#[macro_use]
6-
mod cpu_local;
7-
mod address;
8-
mod apic;
95
mod arch_prctl;
10-
mod backtrace;
11-
mod boot;
12-
mod bootinfo;
13-
mod gdt;
14-
mod idle;
15-
mod idt;
16-
mod interrupt;
17-
mod ioapic;
18-
mod lock;
19-
mod page_table;
20-
mod pit;
216
mod process;
22-
mod profile;
23-
mod semihosting;
24-
mod serial;
25-
mod syscall;
26-
mod tss;
27-
mod vga;
287

298
pub const KERNEL_STACK_SIZE: usize = PAGE_SIZE * 256;
309
pub const USER_VALLOC_END: UserVAddr = unsafe { UserVAddr::new_unchecked(0x0000_0fff_0000_0000) };
3110
pub const USER_VALLOC_BASE: UserVAddr = unsafe { UserVAddr::new_unchecked(0x0000_000a_0000_0000) };
3211
pub const USER_STACK_TOP: UserVAddr = USER_VALLOC_BASE;
33-
pub const PAGE_SIZE: usize = 4096;
34-
pub const TICK_HZ: usize = 1000;
3512

36-
pub use address::{PAddr, UserVAddr, VAddr};
3713
pub use arch_prctl::arch_prctl;
38-
pub use backtrace::Backtrace;
39-
pub use boot::init;
40-
pub use idle::{halt, idle};
41-
pub use ioapic::enable_irq;
42-
pub use lock::{SpinLock, SpinLockGuard};
43-
pub use page_table::{PageFaultReason, PageTable};
4414
pub use process::{switch_thread, Process};
45-
pub use profile::read_clock_counter;
46-
#[cfg(test)]
47-
pub use semihosting::{semihosting_halt, ExitStatus};
48-
pub use serial::{print_str, printchar};
49-
pub use syscall::SyscallFrame;

kernel/arch/x64/process.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
use core::cell::UnsafeCell;
22

3-
use super::{
4-
address::VAddr,
5-
gdt::{USER_CS64, USER_DS},
6-
syscall::SyscallFrame,
7-
tss::TSS,
8-
UserVAddr, KERNEL_STACK_SIZE, PAGE_SIZE,
9-
};
10-
use super::{cpu_local::cpu_local_head, gdt::USER_RPL};
113
use crate::result::Result;
12-
use crate::{
13-
mm::page_allocator::{alloc_pages, AllocPageFlags},
14-
process::signal::Signal,
15-
};
4+
use crate::{arch::KERNEL_STACK_SIZE, process::signal::Signal};
165
use crossbeam::atomic::AtomicCell;
6+
use kerla_runtime::address::{UserVAddr, VAddr};
7+
use kerla_runtime::{
8+
arch::x64_specific::{cpu_local_head, TSS, USER_CS64, USER_DS, USER_RPL},
9+
arch::SyscallFrame,
10+
arch::PAGE_SIZE,
11+
page_allocator::{alloc_pages, AllocPageFlags},
12+
};
1713
use x86::current::segmentation::wrfsbase;
1814

1915
#[repr(C, packed)]

kernel/arch/x64/trap.S renamed to kernel/arch/x64/usermode.S

Lines changed: 3 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.set INTERRUPT_HANDLER_SIZE, 16
2-
.set GS_RSP0, 0
3-
.set GS_RSP3, 8
1+
// Offsets in CpuLocalHead.
2+
.set GS_RSP0, 0
3+
.set GS_RSP3, 8
44

55
.global syscall_entry
66
syscall_entry:
@@ -78,115 +78,6 @@ syscall_entry:
7878
swapgs
7979
sysretq
8080

81-
//
82-
// Interrupt/exception handlers
83-
//
84-
.align INTERRUPT_HANDLER_SIZE
85-
.global interrupt_handlers
86-
interrupt_handlers:
87-
.set i, 0
88-
.rept 256
89-
.set handler_start, .
90-
// Exceptions with error code.
91-
.if i == 8 || 10 <= i && i <= 14 || i == 17
92-
.align INTERRUPT_HANDLER_SIZE
93-
cli
94-
push i
95-
jmp interrupt_common
96-
.align INTERRUPT_HANDLER_SIZE
97-
// Interrupts and exceptions without error code.
98-
.else
99-
.align INTERRUPT_HANDLER_SIZE
100-
cli
101-
push 0 // Dummy value as error code.
102-
push i
103-
jmp interrupt_common
104-
.align INTERRUPT_HANDLER_SIZE
105-
.endif
106-
107-
// Increment the counter.
108-
.set i, i + 1
109-
.endr
110-
111-
.extern x64_handle_interrupt
112-
interrupt_common:
113-
//
114-
// The current stack frame:
115-
//
116-
// +--------------------+
117-
// 48 | SS |
118-
// +--------------------+
119-
// 40 | RSP |
120-
// +--------------------+
121-
// 32 | RFLAGS |
122-
// +--------------------+
123-
// 24 | CS |
124-
// +--------------------+
125-
// 16 | RIP |
126-
// +--------------------+
127-
// 8 | Error code |
128-
// +--------------------+
129-
// 0 | IRQ Number | <- RSP
130-
// +--------------------+
131-
//
132-
133-
// Check CS register in the IRET frame to determine if the interrupt has
134-
// occurred in user mode.
135-
test qword ptr [rsp + 24], 3
136-
jz 1f
137-
swapgs
138-
1:
139-
// Save RDI and set the IRQ number to RDI at once.
140-
xchg rdi, [rsp]
141-
142-
// Save registers except RDI (we have already saved it above).
143-
push r15
144-
push r14
145-
push r13
146-
push r12
147-
push r11
148-
push r10
149-
push r9
150-
push r8
151-
push rbp
152-
push rsi
153-
push rdx
154-
push rcx
155-
push rbx
156-
push rax
157-
158-
mov rsi, rsp
159-
call x64_handle_interrupt
160-
161-
pop rax
162-
pop rbx
163-
pop rcx
164-
pop rdx
165-
pop rsi
166-
pop rbp
167-
pop r8
168-
pop r9
169-
pop r10
170-
pop r11
171-
pop r12
172-
pop r13
173-
pop r14
174-
pop r15
175-
pop rdi
176-
177-
// Skip error code.
178-
add rsp, 8
179-
180-
// Check CS register in the IRET frame to determine whether the exception
181-
// occur in the userspace. If so, do SWAPGS.
182-
test qword ptr [rsp + 8], 3
183-
jz 1f
184-
185-
cli
186-
swapgs
187-
1:
188-
iretq
189-
19081
.global kthread_entry
19182
kthread_entry:
19283
sti

0 commit comments

Comments
 (0)