refactor(kernel): move SyscallOutcome/Result to syscall module, move test helpers to tests module
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
pub mod interrupt;
|
||||
pub mod syscall;
|
||||
|
||||
pub use syscall::{SyscallOutcome, SyscallResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests/mod.rs"]
|
||||
mod tests;
|
||||
@@ -33,22 +35,6 @@ use types::{
|
||||
time::TimeSlice,
|
||||
};
|
||||
|
||||
pub enum SyscallOutcome {
|
||||
Complete(SyscallResult),
|
||||
BlockCaller(domain::thread::BlockReason),
|
||||
SwitchTo(ThreadId),
|
||||
TerminateCaller,
|
||||
}
|
||||
|
||||
pub enum SyscallResult {
|
||||
Ok,
|
||||
Value(usize),
|
||||
Slot(CapSlot),
|
||||
Message(Message),
|
||||
WakeReason(domain::syscall::WakeReason),
|
||||
Error(KernelError),
|
||||
}
|
||||
|
||||
pub struct Kernel<P: Platform> {
|
||||
platform: P,
|
||||
processes: ProcessTable,
|
||||
@@ -117,6 +103,14 @@ impl<P: Platform> Kernel<P> {
|
||||
self.pending_results[thread.raw() as usize].as_ref()
|
||||
}
|
||||
|
||||
pub fn free_page_count(&self) -> usize {
|
||||
self.page_pool.free_count()
|
||||
}
|
||||
|
||||
pub fn process_exists(&self, pid: ProcessId) -> bool {
|
||||
self.processes.get(pid).is_ok()
|
||||
}
|
||||
|
||||
fn caller(&self) -> Result<(ThreadId, ProcessId), KernelError> {
|
||||
let core = self.platform.core_id();
|
||||
let tid = self.scheduler.current(core)
|
||||
@@ -128,14 +122,6 @@ impl<P: Platform> Kernel<P> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn free_page_count(&self) -> usize {
|
||||
self.page_pool.free_count()
|
||||
}
|
||||
|
||||
pub fn process_exists(&self, pid: ProcessId) -> bool {
|
||||
self.processes.get(pid).is_ok()
|
||||
}
|
||||
|
||||
pub(crate) fn cleanup_threads(&mut self, thread_ids: &[Option<ThreadId>; 4]) {
|
||||
use domain::scheduler::Scheduler;
|
||||
for slot in thread_ids {
|
||||
@@ -184,82 +170,4 @@ impl<P: Platform> Kernel<P> {
|
||||
let cap = Capability::new(resource, permissions, Generation::initial());
|
||||
self.processes.get_mut(to)?.capabilities_mut().insert(cap)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn spawn_thread_for_test(
|
||||
&mut self,
|
||||
pid: ProcessId,
|
||||
priority: Priority,
|
||||
time_slice: TimeSlice,
|
||||
) -> ThreadId {
|
||||
use domain::scheduler::Scheduler;
|
||||
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
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn register_irq_for_test(&mut self, irq: types::id::IrqId, thread: ThreadId) {
|
||||
let _ = self.irqs.register(irq, thread);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn block_thread_for_test(
|
||||
&mut self,
|
||||
tid: ThreadId,
|
||||
reason: domain::thread::BlockReason,
|
||||
) {
|
||||
use domain::scheduler::Scheduler;
|
||||
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);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn switch_to_thread_for_test(&mut self, tid: ThreadId) {
|
||||
use domain::scheduler::Scheduler;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn create_endpoint_for_test(&mut self) -> types::id::EndpointId {
|
||||
self.endpoints.create().expect("create endpoint")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn grant_cap_for_test(
|
||||
&mut self,
|
||||
pid: ProcessId,
|
||||
resource: Resource,
|
||||
permissions: PermissionSet,
|
||||
) -> CapSlot {
|
||||
self.grant_cap(pid, resource, permissions).expect("grant cap")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user