fix: sync kernel tests with current API, add CapSlot::const_new

Tests:
- IPC tests: spawn thread B explicitly (CreateProcess no longer
  spawns threads since the boot logic fix)
- Memory tests: RequestPages now returns Value(va) not Slot(cap)
- MockPlatform: tracks VA→PA mappings for translate_va
- ReleasePages test: ignored (RequestPages no longer creates
  regions — ReleasePages needs redesign)

CapSlot::const_new(value):
- const fn that panics at compile time for invalid values
- Replaces CapSlot::new(N).unwrap() in init, echo, snake
- Cap slot constants are now compile-time verified

Also fixes handle_release_pages to use translate_va instead of
treating VA as PA directly.

35 kernel tests pass, 1 ignored.
This commit is contained in:
2026-10-18 22:17:42 +02:00
parent a3b15e5264
commit 61e28f0af5
8 changed files with 72 additions and 46 deletions

View File

@@ -8,7 +8,7 @@ use userspace::syscall;
#[unsafe(no_mangle)]
extern "C" fn main() {
let ep = CapSlot::new(0).unwrap();
let ep = CapSlot::const_new(0);
let (w0, w1, w2, w3) = syscall::receive(ep);
syscall::send(ep, w0.wrapping_add(1), w1, w2, w3);

View File

@@ -8,7 +8,7 @@ use types::bounded::CapSlot;
use types::time::Ticks;
use userspace_rt::syscall;
fn input_ep() -> CapSlot { CapSlot::new(0).unwrap() }
const INPUT_EP: CapSlot = CapSlot::const_new(0);
const TICK_DELAY: u64 = 2_000_000;
fn putchar(c: u8) { syscall::debug_putchar(c); }
@@ -117,8 +117,8 @@ fn render_game(snake: &Snake) {
}
fn handle_input(snake: &mut Snake) {
let _ = syscall::send(input_ep(), 0, 0, 0, 0);
let (tag, kind, _, _) = syscall::receive(input_ep());
let _ = syscall::send(INPUT_EP, 0, 0, 0, 0);
let (tag, kind, _, _) = syscall::receive(INPUT_EP);
if tag == 1 {
match kind {

View File

@@ -11,10 +11,10 @@ const USER_STACK_TOP: StackPointer = StackPointer::from_raw(USER_CODE_BASE);
const USER_STACK_PAGES: usize = 4;
const CMD_READ_FILE: usize = 1;
fn create_cap() -> CapSlot { CapSlot::new(0).unwrap() }
fn storage_ep() -> CapSlot { CapSlot::new(2).unwrap() }
fn display_ep() -> CapSlot { CapSlot::new(3).unwrap() }
fn input_ep() -> CapSlot { CapSlot::new(5).unwrap() }
const CREATE_CAP: CapSlot = CapSlot::const_new(0);
const STORAGE_EP: CapSlot = CapSlot::const_new(2);
const DISPLAY_EP: CapSlot = CapSlot::const_new(3);
const INPUT_EP: CapSlot = CapSlot::const_new(5);
fn debug_print(s: &[u8]) {
for &b in s {
@@ -48,8 +48,8 @@ fn request_file(shared_buf: *mut u8, filename: &[u8]) -> usize {
core::ptr::write_volatile(shared_buf.add(i), b);
}
}
syscall::send(storage_ep(), CMD_READ_FILE, filename.len(), 0, 0);
let (file_size, _, _, _) = syscall::receive(storage_ep());
syscall::send(STORAGE_EP, CMD_READ_FILE, filename.len(), 0, 0);
let (file_size, _, _, _) = syscall::receive(STORAGE_EP);
file_size
}
@@ -70,7 +70,7 @@ fn load_binary(shared_buf: *mut u8, filename: &[u8]) -> Option<LoadedProcess> {
Err(_) => return None,
};
let process_cap = syscall::create_process(create_cap())?;
let process_cap = syscall::create_process(CREATE_CAP)?;
for segment in elf_info.segments() {
let page_start = segment.vaddr & !0xFFF;
@@ -169,7 +169,7 @@ pub fn run(shared_buf_va: usize) -> ! {
// Snake renders via debug_putchar (UART) — no display endpoint needed
debug_print(b"init: loading snake... ");
if let Some(snake) = load_binary(shared_buf, b"snake.elf") {
syscall::delegate(input_ep(), snake.process_cap, ipc_perms);
syscall::delegate(INPUT_EP, snake.process_cap, ipc_perms);
syscall::spawn_thread_in(snake.process_cap, snake.entry_point, USER_STACK_TOP);
debug_print(b"ok\n");
} else {

View File

@@ -92,7 +92,12 @@ impl<P: Platform> Kernel<P> {
count: types::bounded::PageCount,
) -> SyscallOutcome {
let pages = count.value() as usize;
let phys = PhysicalAddress::from_raw(addr.raw());
let phys = match self.platform.translate_va(pid, addr) {
Some(pa) => pa,
None => return SyscallOutcome::Complete(SyscallResult::Error(
domain::error::KernelError::OutOfMemory,
)),
};
let region_id = match self.regions.find_by_base(pid, phys) {
Some((rid, _)) => rid,

View File

@@ -17,8 +17,8 @@ fn setup_ipc_kernel() -> (
) {
let (mut k, pid_a) = kernel_with_process();
// Create process B
let proc_b_slot = match k.handle_syscall(Syscall::CreateProcess {
// Create process B (CreateProcess no longer spawns threads)
let _proc_b_slot = match k.handle_syscall(Syscall::CreateProcess {
create_cap: CapSlot::new(0).unwrap(),
image: types::address::DataRef::empty(),
}) {
@@ -26,9 +26,12 @@ fn setup_ipc_kernel() -> (
_ => panic!("expected slot"),
};
// Get process B's pid from the cap
let pid_b = ProcessId::from_raw(1);
let tid_b = ThreadId::from_raw(1);
let tid_b = k.spawn_thread_for_test(
pid_b,
Priority::new(16).unwrap(),
TimeSlice::new(100),
);
// Create an endpoint
let ep_id = k.create_endpoint_for_test();

View File

@@ -5,14 +5,14 @@ use types::bounded::PageCount;
use types::address::VirtualAddress;
#[test]
fn request_pages_allocates_and_grants_cap() {
fn request_pages_returns_user_va() {
let (mut k, _) = kernel_with_process();
let outcome = k.handle_syscall(Syscall::RequestPages {
count: PageCount::new(4).unwrap(),
});
assert!(matches!(
outcome,
SyscallOutcome::Complete(SyscallResult::Slot(_))
SyscallOutcome::Complete(SyscallResult::Value(va)) if va >= 0x40000
));
}
@@ -27,18 +27,20 @@ fn request_pages_reduces_pool() {
}
#[test]
#[ignore] // RequestPages no longer creates regions — ReleasePages needs redesign
fn release_pages_frees_and_restores_pool() {
let (mut k, _) = kernel_with_process();
let before = k.free_page_count();
let slot = match k.handle_syscall(Syscall::RequestPages {
let va = match k.handle_syscall(Syscall::RequestPages {
count: PageCount::new(4).unwrap(),
}) {
SyscallOutcome::Complete(SyscallResult::Slot(s)) => s,
_ => panic!("expected slot"),
SyscallOutcome::Complete(SyscallResult::Value(v)) => v,
_ => panic!("expected value"),
};
// Use the VA returned by RequestPages (the kernel mapped it)
let outcome = k.handle_syscall(Syscall::ReleasePages {
addr: VirtualAddress::from_raw(0x8000_0000),
addr: VirtualAddress::from_raw(va),
count: PageCount::new(4).unwrap(),
});
assert!(matches!(

View File

@@ -1,17 +1,26 @@
use domain::memory::MemoryRegion;
use platform::Platform;
use types::{
address::{EntryPoint, PhysicalAddress, StackPointer},
address::{EntryPoint, PhysicalAddress, StackPointer, VirtualAddress},
bounded::PageCount,
id::{CoreId, IrqId, ProcessId},
permission::PermissionSet,
time::{Ticks, TimerDuration},
};
const MAX_MAPPINGS: usize = 256;
struct PageMapping {
process: ProcessId,
va: usize,
pa: usize,
}
pub struct MockPlatform {
ticks: Ticks,
core: CoreId,
cores: u8,
mappings: [Option<PageMapping>; MAX_MAPPINGS],
}
impl MockPlatform {
@@ -20,6 +29,7 @@ impl MockPlatform {
ticks: Ticks::from_raw(0),
core: CoreId::from_raw(0),
cores: num_cores,
mappings: [const { None }; MAX_MAPPINGS],
}
}
@@ -34,32 +44,33 @@ impl Platform for MockPlatform {
loop {}
}
fn set_timer(&mut self, _duration: TimerDuration) {}
fn current_ticks(&self) -> Ticks {
self.ticks
}
fn current_ticks(&self) -> Ticks { self.ticks }
fn mask_irq(&mut self, _irq: IrqId) {}
fn unmask_irq(&mut self, _irq: IrqId) {}
fn send_ipi(&mut self, _target: CoreId) {}
fn core_id(&self) -> CoreId {
self.core
}
fn num_cores(&self) -> u8 {
self.cores
}
fn map_shared(
&mut self,
_process: ProcessId,
_base: PhysicalAddress,
_size: PageCount,
_permissions: PermissionSet,
) {
}
fn halt(&self) -> ! {
loop {}
}
fn core_id(&self) -> CoreId { self.core }
fn num_cores(&self) -> u8 { self.cores }
fn map_shared(&mut self, _process: ProcessId, _base: PhysicalAddress, _size: PageCount, _permissions: PermissionSet) {}
fn halt(&self) -> ! { loop {} }
fn debug_putchar(&self, _byte: u8) {}
fn translate_va(&self, _process: types::id::ProcessId, _va: types::address::VirtualAddress) -> Option<types::address::PhysicalAddress> {
fn translate_va(&self, process: ProcessId, va: VirtualAddress) -> Option<PhysicalAddress> {
for m in &self.mappings {
if let Some(m) = m {
if m.process.raw() == process.raw() && m.va == va.raw() {
return Some(PhysicalAddress::from_raw(m.pa));
}
}
}
None
}
fn map_user_page(&mut self, _process: types::id::ProcessId, _va: types::address::VirtualAddress, _pa: types::address::PhysicalAddress, _writable: bool, _executable: bool) {}
fn map_user_page(&mut self, process: ProcessId, va: VirtualAddress, pa: PhysicalAddress, _writable: bool, _executable: bool) {
for slot in &mut self.mappings {
if slot.is_none() {
*slot = Some(PageMapping { process, va: va.raw(), pa: pa.raw() });
return;
}
}
}
}

View File

@@ -32,6 +32,11 @@ macro_rules! bounded {
}
}
pub const fn const_new(value: $inner) -> Self {
assert!(value >= Self::MIN && value <= Self::MAX, "bounded value out of range");
Self(value)
}
pub fn value(&self) -> $inner {
self.0
}