Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kernel virtmem cont #189

Merged
merged 42 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
208e3b0
fix(wavltree): return tree lifetimes from `Cursor`
JonasKruckenberg Dec 18, 2024
c1e2e78
feat(loader): report stacks & TLS region and online hart mask
JonasKruckenberg Dec 18, 2024
f3baa84
wip
JonasKruckenberg Dec 18, 2024
00d08e2
Merge branch 'main' into jonas/feat/kernel-virtmem
JonasKruckenberg Dec 18, 2024
0f7372e
fmt & clippy
JonasKruckenberg Dec 18, 2024
2b5a77b
Update aspace.rs
JonasKruckenberg Dec 18, 2024
e1685b6
refactor: properly setup address space range constants
JonasKruckenberg Dec 19, 2024
ac64c0f
fix tests
JonasKruckenberg Dec 19, 2024
a254b3f
fmt
JonasKruckenberg Dec 19, 2024
e83c1ca
fix: fix tests by identity mapping RTC driver
JonasKruckenberg Dec 19, 2024
5d4c116
fmt
JonasKruckenberg Dec 19, 2024
5fb8160
wip
JonasKruckenberg Dec 19, 2024
1768a22
refactor: rename `pmm` to `mmu`
JonasKruckenberg Dec 19, 2024
04c2a8f
refactor(loader): report physical address offset and physical memory …
JonasKruckenberg Dec 22, 2024
d7b3564
fix(mmu): report error if Flush is dropped with unflushed changes
JonasKruckenberg Dec 22, 2024
c299420
wip
JonasKruckenberg Dec 22, 2024
e6417ea
refactor(mmu): allow any `FramesIterator` implementation
JonasKruckenberg Dec 22, 2024
f7d4abc
fix(dtb-parser): implement `Iterator` for `Strings`
JonasKruckenberg Dec 22, 2024
7fcd97b
refactor: don't explicitly map RTC device
JonasKruckenberg Dec 22, 2024
f7d2deb
chore(mmu): better printing of addresses
JonasKruckenberg Dec 22, 2024
55cbfe1
fix(wavltree): return `Pin<&mut T>` from `insert`
JonasKruckenberg Dec 22, 2024
d91e4da
fix(mmu): correct pretty printing of addresses
JonasKruckenberg Dec 25, 2024
ab95d6e
wip
JonasKruckenberg Dec 25, 2024
dfe403b
fmt & fixes
JonasKruckenberg Dec 26, 2024
5c2feec
fix: expand initial heap for elf processing
JonasKruckenberg Dec 26, 2024
d3ca827
fix startup
JonasKruckenberg Dec 26, 2024
a21a111
fix: print stack trace on panic
JonasKruckenberg Dec 26, 2024
e4c6580
feat: support short backtrace
JonasKruckenberg Dec 26, 2024
119d3bf
fix tests
JonasKruckenberg Dec 26, 2024
240e2af
don't panic in drop
JonasKruckenberg Dec 26, 2024
86a216b
fix(kernel/vm): don't panic when virt allocation range is empty
JonasKruckenberg Dec 26, 2024
e8e2ece
fmt
JonasKruckenberg Dec 26, 2024
188ea02
fix(loader): prevent frame allocator from handing out loader memory
JonasKruckenberg Dec 28, 2024
b66b360
test: add tests for `mmu` crate
JonasKruckenberg Dec 28, 2024
22d9609
fix: better address arithmetic
JonasKruckenberg Dec 28, 2024
ca9f574
fix(mmu): correct `align_down` implementation
JonasKruckenberg Dec 28, 2024
2f999ab
tests(mmu): fix tests
JonasKruckenberg Dec 28, 2024
13c85c3
fix(kernel/vm): return gap size zero instead of panic
JonasKruckenberg Dec 28, 2024
485e14b
Merge branch 'main' into jonas/feat/kernel-virtmem
JonasKruckenberg Dec 29, 2024
f53a165
enable debug assertions in release mode
JonasKruckenberg Dec 29, 2024
3d13f23
fix: increase QEMU physical memory for tests
JonasKruckenberg Dec 29, 2024
3a0c09d
clippy & fmt
JonasKruckenberg Dec 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 72 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kernel = { path = "kernel" }
loader-api = { path = "loader/api" }
backtrace = { path = "libs/backtrace" }
dtb-parser = { path = "libs/dtb-parser" }
pmm = { path = "libs/pmm" }
mmu = { path = "libs/mmu" }
ktest = { path = "libs/ktest" }
linked-list = { path = "libs/linked-list" }
panic-common = { path = "libs/panic-common" }
Expand All @@ -50,10 +50,13 @@ arrayvec = { version = "0.7.4", default-features = false }
object = { version = "0.36.4", default-features = false }
gimli = { version = "0.31.0", default-features = false, features = ["read"] }
xmas-elf = "0.9.1"
talc = "4.4.2"
talc = { version = "4.4.2", default-features = false }
pin-project-lite = "0.2.15"
rand_chacha = { version = "0.3.1", default-features = false }
rand = { version = "0.8.5", default-features = false }
static_assertions = "1.1.0"
addr2line = { version = "0.24.2", default-features = false }
rustc-demangle = { version = "0.1.24", default-features = false }

# wast dependencies
bumpalo = "3.14.0"
Expand Down Expand Up @@ -91,6 +94,7 @@ lto = true
strip = true
overflow-checks = true
split-debuginfo = "packed"
debug-assertions = true

[profile.bench]
debug = true
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ test-docs crate="" *cargo_args="":
# run all tests
test $K23_PROFILE=(profile) cargo_args="" *args="": && (test-docs cargo_args)
{{ _cargo }} test \
-p mmu \
-p kernel \
--target kernel/riscv64gc-k23-none-kernel.json \
--profile {{ profile }} \
Expand Down Expand Up @@ -154,7 +155,7 @@ _run_riscv64 binary *args: (_build_bootimg binary)
-machine virt \
-cpu rv64 \
-smp 1 \
-m 64M \
-m 128M \
-d guest_errors,int \
-display none \
-serial stdio \
Expand Down
10 changes: 7 additions & 3 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@ harness = false

[dependencies]
loader-api.workspace = true
pmm.workspace = true
mmu.workspace = true
panic-unwind.workspace = true
semihosting-logger = { workspace = true, features = ["hartid"] }
sync.workspace = true
thread-local.workspace = true
wavltree= { workspace = true, features = ["dot"] }
dtb-parser.workspace = true
backtrace.workspace = true

talc.workspace = true
talc = { workspace = true, features = ["lock_api", "counters"] }
log.workspace = true
cfg-if.workspace = true
pin-project-lite.workspace = true
rand_chacha.workspace = true
rand.workspace = true
onlyerror.workspace = true
bitflags.workspace = true
xmas-elf.workspace = true
static_assertions.workspace = true

[target.'cfg(any(target_arch = "riscv64", target_arch = "riscv32"))'.dependencies]
riscv.workspace = true

[dev-dependencies]
ktest.workspace = true
wast.workspace = true
hashbrown.workspace = true
4 changes: 2 additions & 2 deletions kernel/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use loader_api::BootInfo;
use pmm::AddressRangeExt;
use mmu::AddressRangeExt;
use talc::{ErrOnOom, Span, Talc, Talck};

#[global_allocator]
Expand All @@ -14,7 +14,7 @@ pub fn init(boot_info: &BootInfo) {
log::debug!("Kernel heap: {heap:?}");

let mut alloc = KERNEL_ALLOCATOR.lock();
let span = Span::from_base_size(heap.start.as_raw() as *mut u8, heap.size());
let span = Span::from_base_size(heap.start.as_mut_ptr(), heap.size());
unsafe {
let old_heap = alloc.claim(span).unwrap();
alloc.extend(old_heap, span);
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use pmm::arch::*;
pub use mmu::arch::*;

cfg_if::cfg_if! {
if #[cfg(target_arch = "riscv64")] {
Expand Down
42 changes: 37 additions & 5 deletions kernel/src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,51 @@ pub mod trap_handler;
pub mod vm;

use core::arch::asm;
use mmu::VirtualAddress;
use riscv::sstatus::FS;
use riscv::{interrupt, sie, sstatus};
use static_assertions::const_assert_eq;

pub use setjmp_longjmp::{call_with_setjmp, longjmp, setjmp, JmpBuf, JmpBufStruct};

pub fn finish_hart_init() {
/// Virtual address where the kernel address space starts.
///
///
pub const KERNEL_ASPACE_BASE: VirtualAddress = VirtualAddress::new(0xffffffc000000000).unwrap();
pub const KERNEL_ASPACE_SIZE: usize = 1 << mmu::arch::VIRT_ADDR_BITS;
const_assert_eq!(KERNEL_ASPACE_BASE.get(), mmu::arch::CANONICAL_ADDRESS_MASK);
const_assert_eq!(KERNEL_ASPACE_SIZE - 1, !mmu::arch::CANONICAL_ADDRESS_MASK);

/// Virtual address where the user address space starts.
///
/// The first 2MiB are reserved for catching null pointer dereferences, but this might
/// change in the future if we decide that the null-checking performed by the WASM runtime
/// is sufficiently robust.
pub const USER_ASPACE_BASE: VirtualAddress = VirtualAddress::new(0x0000000000200000).unwrap();
pub const USER_ASPACE_SIZE: usize = (1 << mmu::arch::VIRT_ADDR_BITS) - USER_ASPACE_BASE.get();

/// Return whether the given virtual address is in the kernel address space.
pub const fn is_kernel_address(virt: VirtualAddress) -> bool {
virt.get() >= KERNEL_ASPACE_BASE.get()
&& virt.checked_sub_addr(KERNEL_ASPACE_BASE).unwrap() < KERNEL_ASPACE_SIZE
}

/// Suspend the calling hart until an interrupt is received.
pub fn wait_for_interrupt() {
unsafe { asm!("wfi") }
}

/// Early architecture-specific, per-hart initialization.
pub fn hart_init_early() {}

/// Late architecture-specific, per-hart initialization.
pub fn hart_init_late() {
unsafe {
// Enable interrupts
interrupt::enable();
// Enable supervisor timer interrupts
sie::set_stie();
// Enable FPU
sstatus::set_fs(FS::Initial);
}
}

pub fn wait_for_interrupt() {
unsafe { asm!("wfi") }
}
Loading
Loading