Skip to content

Commit

Permalink
kernel: refactor to use tabs kernel-wide
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Jan 20, 2025
1 parent 32ce794 commit aca1b90
Show file tree
Hide file tree
Showing 19 changed files with 575 additions and 930 deletions.
20 changes: 11 additions & 9 deletions oro-arch-x86_64/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub unsafe fn initialize_primary(lapic: Lapic) {

let root_ring = kernel.state().root_ring();

'module: while next != 0 {
while next != 0 {
let Some(module) =
Phys::from_address_unchecked(next).as_ref::<oro_boot_protocol::Module>()
else {
Expand Down Expand Up @@ -107,9 +107,7 @@ pub unsafe fn initialize_primary(lapic: Lapic) {

let module_handle = Module::new(id.clone()).expect("failed to create root ring module");

let entry_point = {
let module_lock = module_handle.lock();

let entry_point = module_handle.with(|module_lock| {
let mapper = module_lock.mapper();

let elf_base = Phys::from_address_unchecked(module.base).as_ptr_unchecked::<u8>();
Expand All @@ -124,20 +122,20 @@ pub unsafe fn initialize_primary(lapic: Lapic) {

for segment in elf.segments() {
let mapper_segment = match segment.ty() {
ElfSegmentType::Ignored => continue 'module,
ElfSegmentType::Ignored => return None,
ElfSegmentType::Invalid { flags, ptype } => {
dbg_err!(
"root ring module {id} has invalid segment; skipping: \
ptype={ptype:?} flags={flags:?}",
);
continue 'module;
return None;
}
ElfSegmentType::ModuleCode => AddressSpaceLayout::user_code(),
ElfSegmentType::ModuleData => AddressSpaceLayout::user_data(),
ElfSegmentType::ModuleRoData => AddressSpaceLayout::user_rodata(),
ty => {
dbg_err!("root ring module {id} has invalid segment {ty:?}; skipping",);
continue 'module;
return None;
}
};

Expand Down Expand Up @@ -193,10 +191,14 @@ pub unsafe fn initialize_primary(lapic: Lapic) {
}
}

elf.entry_point()
Some(elf.entry_point())
});

let Some(entry_point) = entry_point else {
continue;
};

let instance = Instance::new(&module_handle, &root_ring)
let instance = Instance::new(&module_handle, root_ring)
.expect("failed to create root ring instance");

// Create a thread for the entry point.
Expand Down
2 changes: 1 addition & 1 deletion oro-arch-x86_64/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use core::arch::{asm, naked_asm};

use oro_kernel::{interface::SystemCallRequest, scheduler::Switch};
use oro_kernel::{scheduler::Switch, syscall::SystemCallRequest};
use oro_mem::mapper::AddressSegment;
use oro_sync::Lock;

Expand Down
22 changes: 0 additions & 22 deletions oro-kernel/src/id.rs

This file was deleted.

2 changes: 1 addition & 1 deletion oro-kernel/src/iface/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Always available, regardless of the caller's ring.
use crate::{arch::Arch, interface::InterfaceResponse, tab::Tab, thread::Thread};
use crate::{arch::Arch, syscall::InterfaceResponse, tab::Tab, thread::Thread};

mod thread_v0;

Expand Down
150 changes: 79 additions & 71 deletions oro-kernel/src/iface/kernel/thread_v0.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Implements version 0 of the thread control interface.
use oro_sync::Lock;
use oro_sysabi::{key, syscall::Error as SysError};

use super::KernelInterface;
use crate::{
arch::Arch,
interface::{InterfaceResponse, SystemCallResponse},
syscall::{InterfaceResponse, SystemCallResponse},
tab::Tab,
thread::{ChangeStateError, RunState, Thread},
};
Expand All @@ -27,64 +26,63 @@ pub enum Error {
#[repr(transparent)]
pub struct ThreadV0;

/// Given a thread, index and key, checks to see if the index (thread ID)
/// is `0` (indicating 'self') or another thread, attempts to look up the
/// thread ID in the instance's thread table, and then executes the given
/// match block with the target thread (either self or the found thread).
macro_rules! with_thread_id {
($thread:expr, $index:expr, ($source_id:ident, $thr_target:ident) for match $key:ident { $($tt:tt)* }) => {
if $index == 0 {
{
$thread.with_mut(|$thr_target| {
let $source_id = $thr_target.id();
match $key {
$( $tt )*
}
})
}
/// Resolves the target thread from the given index,
/// checking that the caller has permission to access it.
macro_rules! resolve_target {
($thread:expr, $index:expr) => {{
let thread = $thread;
let index = $index;
if index == 0 || index == thread.id() {
thread.clone()
} else {
$thread.with(|thread_lock| {
let instance = thread_lock.instance();
let instance_lock = instance.lock();
let threads = instance_lock.threads();
if let Some(other_thread) = threads.get($index) {
{
let $source_id = thread_lock.id();
other_thread.with_mut(|$thr_target| {
match $key {
$( $tt )*
}
})
match crate::tab::get().lookup::<Thread<A>>(index) {
Some(t) => {
if t.with(|t| t.ring().id()) != thread.with(|t| t.ring().id()) {
return InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadIndex,
ret: 0,
});
}
} else {
InterfaceResponse::Immediate(SystemCallResponse {

t
}
None => {
return InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadIndex,
ret: 0,
})
});
}
})
}
}
};
}};
}

impl KernelInterface for ThreadV0 {
const TYPE_ID: u64 = oro_sysabi::id::iface::KERNEL_THREAD_V0;

fn get<A: Arch>(thread: &Tab<Thread<A>>, index: u64, key: u64) -> InterfaceResponse {
with_thread_id!(thread, index, (_caller_id, target) for match key {
key!("id") => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: target.id(),
}),
key!("status") => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: target.run_state() as u64,
}),
_ => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadKey,
ret: 0,
}),
})
let target = resolve_target!(thread, index);

match key {
key!("id") => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: target.id(),
})
}
key!("status") => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: target.with(|t| t.run_state()) as u64,
})
}
_ => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadKey,
ret: 0,
})
}
}
}

fn set<A: Arch>(
Expand All @@ -93,11 +91,15 @@ impl KernelInterface for ThreadV0 {
key: u64,
value: u64,
) -> InterfaceResponse {
with_thread_id!(thread, index, (caller_id, target) for match key {
key!("id") => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::ReadOnly,
ret: 0,
}),
let target = resolve_target!(thread, index);

match key {
key!("id") => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::ReadOnly,
ret: 0,
})
}
key!("status") => {
let Ok(new_state) = RunState::try_from(value) else {
return InterfaceResponse::Immediate(SystemCallResponse {
Expand All @@ -106,25 +108,31 @@ impl KernelInterface for ThreadV0 {
});
};

match target.transition_to(caller_id, new_state) {
Ok(None) => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: 0,
}),
match target.with_mut(|t| t.transition_to(thread.id(), new_state)) {
Ok(None) => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::Ok,
ret: 0,
})
}
Ok(Some(transition)) => InterfaceResponse::Pending(transition),
Err(e) => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::InterfaceError,
ret: match e {
ChangeStateError::Race => Error::Race,
ChangeStateError::Terminated => Error::Terminated,
} as u64,
}),
Err(e) => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::InterfaceError,
ret: match e {
ChangeStateError::Race => Error::Race,
ChangeStateError::Terminated => Error::Terminated,
} as u64,
})
}
}
},
_ => InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadKey,
ret: 0,
}),
})
}
_ => {
InterfaceResponse::Immediate(SystemCallResponse {
error: SysError::BadKey,
ret: 0,
})
}
}
}
}
3 changes: 2 additions & 1 deletion oro-kernel/src/iface/root_ring/boot_vbuf_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ use oro_sysabi::{key, syscall::Error as SysError};

use crate::{
arch::Arch,
interface::{Interface, InterfaceResponse, SystemCallResponse},
interface::Interface,
syscall::{InterfaceResponse, SystemCallResponse},
tab::Tab,
thread::Thread,
};
Expand Down
3 changes: 2 additions & 1 deletion oro-kernel/src/iface/root_ring/debug_out_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use oro_sysabi::{key, syscall::Error as SysError};

use crate::{
arch::Arch,
interface::{Interface, InterfaceResponse, SystemCallResponse},
interface::Interface,
syscall::{InterfaceResponse, SystemCallResponse},
tab::Tab,
thread::Thread,
};
Expand Down
Loading

0 comments on commit aca1b90

Please sign in to comment.