Skip to content

Commit

Permalink
kernel: initial real object registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Jan 12, 2025
1 parent 38a7a16 commit a379e12
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 172 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ limine = "0.2.0"
uart_16550 = "0.3.0"
volatile-register = "0.2.2"
buddy_system_allocator = "0.11.0"
stash = { version = "0.1.6", default-features = false }

bindgen = { git = "https://github.com/oro-os/dep.rust-bindgen.git" }
syn = { version = "2.0.60", features = ["full", "printing"] }
Expand Down
4 changes: 2 additions & 2 deletions 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::{scheduler::Switch, thread::SystemCallRequest};
use oro_kernel::scheduler::{Switch, SystemCallRequest};
use oro_mem::mapper::AddressSegment;
use oro_sync::Lock;
use oro_sysabi::syscall::Opcode;
Expand Down Expand Up @@ -132,7 +132,7 @@ unsafe extern "C" fn syscall_enter_noncompat_rust() -> ! {

current_thread.lock().handle_mut().irq_stack_ptr = stack_ptr;

let switch = scheduler.event_system_call(syscall_request);
let switch = scheduler.event_system_call(&syscall_request);

drop(scheduler);

Expand Down
2 changes: 2 additions & 0 deletions oro-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ oro-debug.workspace = true
oro-sync.workspace = true
oro-sysabi.workspace = true

stash = { workspace = true, default-features = false }

[lints]
workspace = true
36 changes: 23 additions & 13 deletions oro-kernel/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use oro_mem::{
},
mapper::{AddressSegment, AddressSpace as _, MapError},
};
use oro_sync::{Lock, Mutex};
use oro_sync::{Lock, ReentrantMutex};

use crate::{
AddressSpace, Kernel, UserHandle,
arch::{Arch, InstanceHandle},
module::Module,
port::Port,
registry::Registry,
ring::Ring,
thread::Thread,
};
Expand Down Expand Up @@ -56,25 +57,27 @@ pub struct Instance<A: Arch> {
/// Strong reference to prevent the module from being
/// deallocated while the instance is still alive, which would
/// otherwise reclaim the executable memory pages and wreak havoc.
module: Arc<Mutex<Module<A>>>,
module: Arc<ReentrantMutex<Module<A>>>,
/// The ring on which this instance resides.
ring: Weak<Mutex<Ring<A>>>,
ring: Weak<ReentrantMutex<Ring<A>>>,
/// The thread list for the instance.
pub(super) threads: Vec<Arc<Mutex<Thread<A>>>>,
pub(super) threads: Vec<Arc<ReentrantMutex<Thread<A>>>>,
/// The port list for the instance.
ports: Vec<Arc<Mutex<Port>>>,
ports: Vec<Arc<ReentrantMutex<Port>>>,
/// The instance's architecture handle.
handle: A::InstanceHandle,
/// The root object registry for the instance.
registry: Arc<ReentrantMutex<Registry>>,
}

impl<A: Arch> Instance<A> {
/// Creates a new instance, allocating a new mapper.
///
/// Notably, this does **not** spawn any threads.
pub fn new(
module: &Arc<Mutex<Module<A>>>,
ring: &Arc<Mutex<Ring<A>>>,
) -> Result<Arc<Mutex<Self>>, MapError> {
module: &Arc<ReentrantMutex<Module<A>>>,
ring: &Arc<ReentrantMutex<Ring<A>>>,
) -> Result<Arc<ReentrantMutex<Self>>, MapError> {
let id = Kernel::<A>::get().state().allocate_id();

let mapper = AddressSpace::<A>::new_user_space(Kernel::<A>::get().mapper())
Expand All @@ -90,13 +93,14 @@ impl<A: Arch> Instance<A> {
AddressSpace::<A>::user_rodata()
.apply_user_space_shallow(handle.mapper(), module.lock().mapper())?;

let r = Arc::new(Mutex::new(Self {
let r = Arc::new(ReentrantMutex::new(Self {
id,
module: module.clone(),
ring: Arc::downgrade(ring),
threads: Vec::new(),
ports: Vec::new(),
handle,
registry: Arc::default(),
}));

ring.lock().instances.push(r.clone());
Expand All @@ -117,22 +121,22 @@ impl<A: Arch> Instance<A> {
}

/// The handle to the module from which this instance was spawned.
pub fn module(&self) -> Arc<Mutex<Module<A>>> {
pub fn module(&self) -> Arc<ReentrantMutex<Module<A>>> {
self.module.clone()
}

/// The weak handle to the ring on which this instance resides.
pub fn ring(&self) -> Weak<Mutex<Ring<A>>> {
pub fn ring(&self) -> Weak<ReentrantMutex<Ring<A>>> {
self.ring.clone()
}

/// Gets a handle to the list of threads for this instance.
pub fn threads(&self) -> &[Arc<Mutex<Thread<A>>>] {
pub fn threads(&self) -> &[Arc<ReentrantMutex<Thread<A>>>] {
&self.threads
}

/// Gets a handle to the list of ports for this instance.
pub fn ports(&self) -> &[Arc<Mutex<Port>>] {
pub fn ports(&self) -> &[Arc<ReentrantMutex<Port>>] {
&self.ports
}

Expand All @@ -141,4 +145,10 @@ impl<A: Arch> Instance<A> {
pub fn mapper(&self) -> &UserHandle<A> {
self.handle.mapper()
}

/// Returns a handle to the instance's object registry.
#[must_use]
pub fn registry(&self) -> Arc<ReentrantMutex<Registry>> {
self.registry.clone()
}
}
17 changes: 9 additions & 8 deletions oro-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod arch;
pub mod instance;
pub mod module;
pub mod port;
pub mod registry;
pub mod ring;
pub mod scheduler;
pub mod sync;
Expand All @@ -43,7 +44,7 @@ use oro_mem::{
mapper::{AddressSegment, MapError, AddressSpace as _},
pfa::Alloc,
};
use oro_sync::{Lock, Mutex, TicketMutex};
use oro_sync::{Lock, ReentrantMutex, TicketMutex};

use self::{arch::Arch, scheduler::Scheduler};

Expand Down Expand Up @@ -200,16 +201,16 @@ impl<A: Arch> Kernel<A> {
/// core boot/powerdown/bringup cycles.
pub struct KernelState<A: Arch> {
/// List of all modules.
modules: TicketMutex<Vec<Weak<Mutex<module::Module<A>>>>>,
modules: TicketMutex<Vec<Weak<ReentrantMutex<module::Module<A>>>>>,
/// List of all rings.
rings: TicketMutex<Vec<Weak<Mutex<ring::Ring<A>>>>>,
rings: TicketMutex<Vec<Weak<ReentrantMutex<ring::Ring<A>>>>>,
/// List of all instances.
instances: TicketMutex<Vec<Weak<Mutex<instance::Instance<A>>>>>,
instances: TicketMutex<Vec<Weak<ReentrantMutex<instance::Instance<A>>>>>,
/// List of all threads.
threads: TicketMutex<Vec<Weak<Mutex<thread::Thread<A>>>>>,
threads: TicketMutex<Vec<Weak<ReentrantMutex<thread::Thread<A>>>>>,

/// The root ring.
root_ring: Arc<Mutex<ring::Ring<A>>>,
root_ring: Arc<ReentrantMutex<ring::Ring<A>>>,

/// The ID counter for resource allocation.
id_counter: AtomicU64,
Expand Down Expand Up @@ -252,14 +253,14 @@ impl<A: Arch> KernelState<A> {
}

/// Returns a handle to the root ring.
pub fn root_ring(&'static self) -> Arc<Mutex<ring::Ring<A>>> {
pub fn root_ring(&'static self) -> Arc<ReentrantMutex<ring::Ring<A>>> {
self.root_ring.clone()
}

/// Returns a reference to the mutex-guarded list of threads.
pub fn threads(
&'static self,
) -> &'static impl Lock<Target = Vec<Weak<Mutex<thread::Thread<A>>>>> {
) -> &'static impl Lock<Target = Vec<Weak<ReentrantMutex<thread::Thread<A>>>>> {
&self.threads
}

Expand Down
10 changes: 5 additions & 5 deletions oro-kernel/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oro_mem::{
},
mapper::{AddressSpace as _, MapError},
};
use oro_sync::{Lock, Mutex};
use oro_sync::{Lock, ReentrantMutex};

use crate::{AddressSpace, Kernel, UserHandle, arch::Arch, instance::Instance};

Expand All @@ -29,7 +29,7 @@ pub struct Module<A: Arch> {
/// to refer to the module during module loading.
module_id: Id<{ IdType::Module }>,
/// The list of instances spawned from this module.
pub(super) instances: Vec<Weak<Mutex<Instance<A>>>>,
pub(super) instances: Vec<Weak<ReentrantMutex<Instance<A>>>>,
/// The module's address space mapper handle.
///
/// Only uninitialized if the module is in the process of being freed.
Expand All @@ -43,12 +43,12 @@ pub struct Module<A: Arch> {

impl<A: Arch> Module<A> {
/// Creates a new module.
pub fn new(module_id: Id<{ IdType::Module }>) -> Result<Arc<Mutex<Self>>, MapError> {
pub fn new(module_id: Id<{ IdType::Module }>) -> Result<Arc<ReentrantMutex<Self>>, MapError> {
let id = Kernel::<A>::get().state().allocate_id();

let mapper = AddressSpace::<A>::new_user_space_empty().ok_or(MapError::OutOfMemory)?;

let r = Arc::new(Mutex::new(Self {
let r = Arc::new(ReentrantMutex::new(Self {
id,
module_id,
instances: Vec::new(),
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<A: Arch> Module<A> {
}

/// Returns a list of weak handles to instances spawned from this module.
pub fn instances(&self) -> &[Weak<Mutex<Instance<A>>>] {
pub fn instances(&self) -> &[Weak<ReentrantMutex<Instance<A>>>] {
&self.instances
}

Expand Down
Loading

0 comments on commit a379e12

Please sign in to comment.