refactor(kernel): move SyscallOutcome/Result to syscall module, move test helpers to tests module

This commit is contained in:
2026-10-18 00:39:28 +02:00
parent 38ebb6002b
commit 2c4a9dbadc
8 changed files with 118 additions and 115 deletions

View File

@@ -7,18 +7,20 @@ mod simple;
mod tick;
use domain::memory::page_pool::PagePool;
use domain::memory::MemoryRegion;
use domain::resource::Resource;
use domain::scheduler::Scheduler;
use domain::thread::{SavedContext, ThreadState};
use platform::Platform;
use types::{
address::{EntryPoint, PhysicalAddress, StackPointer},
bounded::Priority,
id::{CoreId, IrqId, ProcessId},
bounded::{CapSlot, Priority},
id::{CoreId, EndpointId, IrqId, ProcessId, ThreadId},
permission::PermissionSet,
time::{Ticks, TimerDuration, TimeSlice},
};
use domain::memory::MemoryRegion;
use domain::thread::SavedContext;
use crate::Kernel;
use crate::{Kernel, SyscallResult};
pub struct MockPlatform {
ticks: Ticks,
@@ -74,3 +76,74 @@ pub fn kernel_with_process() -> (Kernel<MockPlatform>, ProcessId) {
.unwrap();
(kernel, pid)
}
impl Kernel<MockPlatform> {
pub fn spawn_thread_for_test(
&mut self,
pid: ProcessId,
priority: Priority,
time_slice: TimeSlice,
) -> ThreadId {
let tid = self.threads.spawn(pid, priority, time_slice, None).expect("spawn");
let _ = self.processes.get_mut(pid).map(|p| p.add_thread(tid));
let core = self.platform.core_id();
self.scheduler.add(tid, priority, core);
tid
}
pub fn register_irq_for_test(&mut self, irq: IrqId, thread: ThreadId) {
let _ = self.irqs.register(irq, thread);
}
pub fn block_thread_for_test(
&mut self,
tid: ThreadId,
reason: domain::thread::BlockReason,
) {
let core = self.platform.core_id();
if let Ok(t) = self.threads.get_mut(tid) {
if matches!(t.state(), ThreadState::Ready) {
let _ = t.set_running(core);
}
let _ = t.block(reason);
}
self.scheduler.remove(tid);
}
pub fn switch_to_thread_for_test(&mut self, tid: ThreadId) {
let core = self.platform.core_id();
if let Some(current) = self.scheduler.current(core) {
if current == tid {
return;
}
if let Ok(t) = self.threads.get_mut(current) {
if matches!(t.state(), ThreadState::Running(_)) {
let priority = t.priority();
let _ = t.preempt();
self.scheduler.add(current, priority, core);
}
}
}
if let Ok(t) = self.threads.get(tid) {
if matches!(t.state(), ThreadState::Ready) {
self.scheduler.next(core);
let _ = self.threads.get_mut(tid).map(|t| t.set_running(core));
}
}
}
pub fn create_endpoint_for_test(&mut self) -> EndpointId {
self.endpoints.create().expect("create endpoint")
}
pub fn grant_cap_for_test(
&mut self,
pid: ProcessId,
resource: Resource,
permissions: PermissionSet,
) -> CapSlot {
self.grant_cap(pid, resource, permissions).expect("grant cap")
}
}