diff --git a/oro-arch-aarch64/src/mem/address_space.rs b/oro-arch-aarch64/src/mem/address_space.rs index 8ba6a98..e78cae9 100644 --- a/oro-arch-aarch64/src/mem/address_space.rs +++ b/oro-arch-aarch64/src/mem/address_space.rs @@ -87,9 +87,17 @@ impl AddressSpaceLayout { /// The segment for the ring registry pub const KERNEL_RING_REGISTRY_IDX: usize = 400; /// The segment for the module instance registry - pub const KERNEL_MODULE_INSTANCE_REGISTRY_IDX: usize = 401; + pub const KERNEL_INSTANCE_REGISTRY_IDX: usize = 401; + /// The segment for the module instance item registry + pub const KERNEL_INSTANCE_ITEM_REGISTRY_IDX: usize = 402; + /// The segment for the thread registry + pub const KERNEL_THREAD_REGISTRY_IDX: usize = 403; + /// The segment for the thread item registry + pub const KERNEL_THREAD_ITEM_REGISTRY_IDX: usize = 404; + /// The segment for the module registry + pub const KERNEL_MODULE_REGISTRY_IDX: usize = 405; /// The segment for the port registry - pub const KERNEL_PORT_REGISTRY_IDX: usize = 402; + pub const KERNEL_PORT_REGISTRY_IDX: usize = 406; /// The kernel executable range, shared by the RX, RO, and RW segments. pub const KERNEL_EXE_IDX: usize = 511; } @@ -233,6 +241,48 @@ impl AddressSpaceLayout { } } +#[expect(clippy::missing_docs_in_private_items)] +macro_rules! registries { + ($($name:ident => $idx:ident),* $(,)?) => { + $(fn $name() -> Self::SupervisorSegment { + static DESCRIPTOR: Segment = unsafe { + Segment { + valid_range: ( + AddressSpaceLayout::$idx, + AddressSpaceLayout::$idx, + ), + l0_template: L0PageTableDescriptor::new() + .with_valid() + .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) + .with_user_no_exec() + .with_kernel_no_exec(), + l1_table_template: L1PageTableDescriptor::new() + .with_valid() + .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) + .with_user_no_exec() + .with_kernel_no_exec(), + l2_table_template: L2PageTableDescriptor::new() + .with_valid() + .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) + .with_user_no_exec() + .with_kernel_no_exec(), + l3_template: L3PageTableBlockDescriptor::new() + .with_valid() + .with_block_access_permissions( + PageTableEntryBlockAccessPerm::KernelRWUserNoAccess, + ) + .with_user_no_exec() + .with_kernel_no_exec() + .with_not_secure() + .with_mair_index(MairEntry::NormalMemory.index() as u64), + } + }; + + &DESCRIPTOR + })* + } +} + /// L0 intermediate PTE for the kernel executable segment. /// /// Defined here as a constant since it's used within overlapping @@ -270,6 +320,16 @@ unsafe impl AddressSpace for AddressSpaceLayout { type UserHandle = Ttbr0Handle; type UserSegment = &'static Segment; + registries! { + kernel_ring_registry => KERNEL_RING_REGISTRY_IDX, + kernel_instance_registry => KERNEL_INSTANCE_REGISTRY_IDX, + kernel_instance_item_registry => KERNEL_INSTANCE_ITEM_REGISTRY_IDX, + kernel_port_registry => KERNEL_PORT_REGISTRY_IDX, + kernel_thread_registry => KERNEL_THREAD_REGISTRY_IDX, + kernel_thread_item_registry => KERNEL_THREAD_ITEM_REGISTRY_IDX, + kernel_module_registry => KERNEL_MODULE_REGISTRY_IDX, + } + unsafe fn current_supervisor_space

(_translator: &P) -> Self::SupervisorHandle where P: Translator, @@ -461,118 +521,4 @@ unsafe impl AddressSpace for AddressSpaceLayout { &DESCRIPTOR } - - fn kernel_ring_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - static DESCRIPTOR: Segment = unsafe { - Segment { - valid_range: ( - AddressSpaceLayout::KERNEL_RING_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_RING_REGISTRY_IDX, - ), - l0_template: L0PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l1_table_template: L1PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l2_table_template: L2PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l3_template: L3PageTableBlockDescriptor::new() - .with_valid() - .with_block_access_permissions( - PageTableEntryBlockAccessPerm::KernelRWUserNoAccess, - ) - .with_user_no_exec() - .with_kernel_no_exec() - .with_not_secure() - .with_mair_index(MairEntry::NormalMemory.index() as u64), - } - }; - - &DESCRIPTOR - } - - fn kernel_module_instance_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - static DESCRIPTOR: Segment = unsafe { - Segment { - valid_range: ( - AddressSpaceLayout::KERNEL_MODULE_INSTANCE_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_MODULE_INSTANCE_REGISTRY_IDX, - ), - l0_template: L0PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l1_table_template: L1PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l2_table_template: L2PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l3_template: L3PageTableBlockDescriptor::new() - .with_valid() - .with_block_access_permissions( - PageTableEntryBlockAccessPerm::KernelRWUserNoAccess, - ) - .with_user_no_exec() - .with_kernel_no_exec() - .with_not_secure() - .with_mair_index(MairEntry::NormalMemory.index() as u64), - } - }; - - &DESCRIPTOR - } - - fn kernel_port_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - static DESCRIPTOR: Segment = unsafe { - Segment { - valid_range: ( - AddressSpaceLayout::KERNEL_PORT_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_PORT_REGISTRY_IDX, - ), - l0_template: L0PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l1_table_template: L1PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l2_table_template: L2PageTableDescriptor::new() - .with_valid() - .with_table_access_permissions(PageTableEntryTableAccessPerm::KernelOnly) - .with_user_no_exec() - .with_kernel_no_exec(), - l3_template: L3PageTableBlockDescriptor::new() - .with_valid() - .with_block_access_permissions( - PageTableEntryBlockAccessPerm::KernelRWUserNoAccess, - ) - .with_user_no_exec() - .with_kernel_no_exec() - .with_not_secure() - .with_mair_index(MairEntry::NormalMemory.index() as u64), - } - }; - - &DESCRIPTOR - } } diff --git a/oro-arch-x86_64/src/mem/address_space.rs b/oro-arch-x86_64/src/mem/address_space.rs index 66b2a33..290ebac 100644 --- a/oro-arch-x86_64/src/mem/address_space.rs +++ b/oro-arch-x86_64/src/mem/address_space.rs @@ -56,9 +56,17 @@ impl AddressSpaceLayout { /// The segment for the ring registry pub const KERNEL_RING_REGISTRY_IDX: usize = 400; /// The segment for the module instance registry - pub const KERNEL_MODULE_INSTANCE_REGISTRY_IDX: usize = 401; + pub const KERNEL_INSTANCE_REGISTRY_IDX: usize = 401; + /// The segment for the module instance item registry + pub const KERNEL_INSTANCE_ITEM_REGISTRY_IDX: usize = 402; + /// The segment for the thread registry + pub const KERNEL_THREAD_REGISTRY_IDX: usize = 403; + /// The segment for the thread item registry + pub const KERNEL_THREAD_ITEM_REGISTRY_IDX: usize = 404; + /// The segment for the module registry + pub const KERNEL_MODULE_REGISTRY_IDX: usize = 405; /// The segment for the port registry - pub const KERNEL_PORT_REGISTRY_IDX: usize = 402; + pub const KERNEL_PORT_REGISTRY_IDX: usize = 406; /// The kernel executable range, shared by the RX, RO, and RW segments. pub const KERNEL_EXE_IDX: usize = 511; } @@ -146,6 +154,31 @@ impl AddressSpaceLayout { } } +#[expect(clippy::missing_docs_in_private_items)] +macro_rules! registries { + ($($name:ident => $idx:ident),* $(,)?) => { + $(fn $name() -> Self::SupervisorSegment { + const DESCRIPTOR: AddressSegment = AddressSegment { + valid_range: ( + AddressSpaceLayout::$idx, + AddressSpaceLayout::$idx, + ), + entry_template: PageTableEntry::new() + .with_global() + .with_present() + .with_no_exec() + .with_writable(), + intermediate_entry_template: PageTableEntry::new() + .with_present() + .with_no_exec() + .with_writable(), + }; + + &DESCRIPTOR + })* + } +} + /// Intermediate page table entry template for the kernel code segment. /// /// Defined here so that the overlapping kernel segments can share the same @@ -164,6 +197,16 @@ unsafe impl AddressSpace for AddressSpaceLayout { type UserHandle = AddressSpaceHandle; type UserSegment = &'static AddressSegment; + registries! { + kernel_ring_registry => KERNEL_RING_REGISTRY_IDX, + kernel_instance_registry => KERNEL_INSTANCE_REGISTRY_IDX, + kernel_instance_item_registry => KERNEL_INSTANCE_ITEM_REGISTRY_IDX, + kernel_port_registry => KERNEL_PORT_REGISTRY_IDX, + kernel_thread_registry => KERNEL_THREAD_REGISTRY_IDX, + kernel_thread_item_registry => KERNEL_THREAD_ITEM_REGISTRY_IDX, + kernel_module_registry => KERNEL_MODULE_REGISTRY_IDX, + } + unsafe fn current_supervisor_space

(_translator: &P) -> Self::SupervisorHandle where P: Translator, @@ -301,67 +344,4 @@ unsafe impl AddressSpace for AddressSpaceLayout { &DESCRIPTOR } - - fn kernel_ring_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - const DESCRIPTOR: AddressSegment = AddressSegment { - valid_range: ( - AddressSpaceLayout::KERNEL_RING_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_RING_REGISTRY_IDX, - ), - entry_template: PageTableEntry::new() - .with_global() - .with_present() - .with_no_exec() - .with_writable(), - intermediate_entry_template: PageTableEntry::new() - .with_present() - .with_no_exec() - .with_writable(), - }; - - &DESCRIPTOR - } - - fn kernel_port_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - const DESCRIPTOR: AddressSegment = AddressSegment { - valid_range: ( - AddressSpaceLayout::KERNEL_PORT_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_PORT_REGISTRY_IDX, - ), - entry_template: PageTableEntry::new() - .with_global() - .with_present() - .with_no_exec() - .with_writable(), - intermediate_entry_template: PageTableEntry::new() - .with_present() - .with_no_exec() - .with_writable(), - }; - - &DESCRIPTOR - } - - fn kernel_module_instance_registry() -> Self::SupervisorSegment { - #[expect(clippy::missing_docs_in_private_items)] - const DESCRIPTOR: AddressSegment = AddressSegment { - valid_range: ( - AddressSpaceLayout::KERNEL_MODULE_INSTANCE_REGISTRY_IDX, - AddressSpaceLayout::KERNEL_MODULE_INSTANCE_REGISTRY_IDX, - ), - entry_template: PageTableEntry::new() - .with_global() - .with_present() - .with_no_exec() - .with_writable(), - intermediate_entry_template: PageTableEntry::new() - .with_present() - .with_no_exec() - .with_writable(), - }; - - &DESCRIPTOR - } } diff --git a/oro-kernel/src/lib.rs b/oro-kernel/src/lib.rs index 9fe07a4..cf2d8ed 100644 --- a/oro-kernel/src/lib.rs +++ b/oro-kernel/src/lib.rs @@ -17,7 +17,7 @@ pub mod registry; pub mod ring; pub mod thread; -use self::registry::Handle; +use self::registry::{Handle, Item}; use oro_macro::assert; use oro_mem::{ mapper::{AddressSegment, AddressSpace, MapError}, @@ -123,11 +123,29 @@ where { /// The shared, spinlocked page frame allocator (PFA) for the /// entire system. - pfa: UnfairCriticalSpinlock, + pfa: UnfairCriticalSpinlock, /// The physical address translator. - pat: Pat, + pat: Pat, + /// Ring registry. - ring_registry: registry::Registry, IntCtrl, AddrSpace, Pat>, + ring_registry: registry::Registry, IntCtrl, AddrSpace, Pat>, + /// Thread registry. + #[expect(dead_code)] + thread_registry: registry::Registry, IntCtrl, AddrSpace, Pat>, + /// Module registry. + #[expect(dead_code)] + module_registry: registry::Registry, + /// Instance registry. + #[expect(dead_code)] + instance_registry: registry::Registry, IntCtrl, AddrSpace, Pat>, + /// Instance item registry. + #[expect(dead_code)] + instance_item_registry: + registry::Registry>, IntCtrl, AddrSpace, Pat>, + /// Thread item registry. + #[expect(dead_code)] + thread_item_registry: + registry::Registry>, IntCtrl, AddrSpace, Pat>, } impl KernelState @@ -151,19 +169,35 @@ where /// or else registry accesses will page fault. #[allow(clippy::missing_panics_doc)] pub unsafe fn new(pat: Pat, pfa: UnfairCriticalSpinlock) -> Result { - let ring_registry = { - let mut pfa_lock = pfa.lock::(); + #[expect(clippy::missing_docs_in_private_items)] + macro_rules! init_registries { + ($($id:ident => $regfn:ident),* $(,)?) => { + $( + let $id = { + let mut pfa_lock = pfa.lock::(); + + let reg = registry::Registry::new( + pat.clone(), + &mut *pfa_lock, + AddrSpace::$regfn(), + )?; - let reg = registry::Registry::new( - pat.clone(), - &mut *pfa_lock, - AddrSpace::kernel_ring_registry(), - )?; + let _ = pfa_lock; - let _ = pfa_lock; + reg + }; + )* + }; + } - reg - }; + init_registries! { + ring_registry => kernel_ring_registry, + thread_registry => kernel_thread_registry, + module_registry => kernel_module_registry, + instance_registry => kernel_instance_registry, + instance_item_registry => kernel_instance_item_registry, + thread_item_registry => kernel_thread_item_registry, + } let root_ring_id = ring_registry.insert_permanent( &pfa, @@ -179,6 +213,11 @@ where pfa, pat, ring_registry, + thread_registry, + module_registry, + instance_registry, + instance_item_registry, + thread_item_registry, }) } diff --git a/oro-mem/src/mapper.rs b/oro-mem/src/mapper.rs index e9ae014..f8ce89b 100644 --- a/oro-mem/src/mapper.rs +++ b/oro-mem/src/mapper.rs @@ -8,7 +8,27 @@ //! for those regions to behave as the kernel expects. use crate::{pfa::alloc::Alloc, translate::Translator}; -/// A const trait that provides descriptors for the layout of an address space +#[allow(clippy::missing_docs_in_private_items)] +macro_rules! define_registries { + ($($(#[doc = $doc:literal])* $name:ident);* ;) => { + $( + $(#[doc = $doc])* + /// + /// This must be read-write, non-user accessible, and is + /// **not** executable. + /// + /// Must **not** overlap with any other segment. + /// + /// Must NOT span more than 2^63 bytes (on 64-bit architectures) + /// or 2^31 bytes (on 32-bit architectures). + /// + /// Must NOT border the beginning or end of an address space. + fn $name() -> Self::SupervisorSegment; + )* + }; +} + +/// A trait that provides descriptors for the layout of an address space /// for the underlying architecture. /// /// # Safety @@ -112,44 +132,22 @@ pub unsafe trait AddressSpace: 'static { /// It **must not** overlap with any other segment. fn kernel_core_local() -> Self::SupervisorSegment; - /// Returns the layout descriptor for the kernel's Ring registry. - /// - /// This must be read-write, non-user accessible, and is - /// **not** executable. - /// - /// Must **not** overlap with any other segment. - /// - /// Must NOT span more than 2^63 bytes (on 64-bit architectures) - /// or 2^31 bytes (on 32-bit architectures). - /// - /// Must NOT border the beginning or end of an address space. - fn kernel_ring_registry() -> Self::SupervisorSegment; - - /// Returns the layout descriptor for the kernel's Port registry. - /// - /// This must be read-write, non-user accessible, and is - /// **not** executable. - /// - /// Must **not** overlap with any other segment. - /// - /// Must NOT span more than 2^63 bytes (on 64-bit architectures) - /// or 2^31 bytes (on 32-bit architectures). - /// - /// Must NOT border the beginning or end of an address space. - fn kernel_port_registry() -> Self::SupervisorSegment; - - /// Returns the layout descriptor for the kernel's Module Instance registry. - /// - /// This must be read-write, non-user accessible, and is - /// **not** executable. - /// - /// Must **not** overlap with any other segment. - /// - /// Must NOT span more than 2^63 bytes (on 64-bit architectures) - /// or 2^31 bytes (on 32-bit architectures). - /// - /// Must NOT border the beginning or end of an address space. - fn kernel_module_instance_registry() -> Self::SupervisorSegment; + define_registries! { + /// Returns the layout descriptor for the kernel's Ring registry. + kernel_ring_registry; + /// Returns the layout descriptor for the kernel's Port registry. + kernel_port_registry; + /// Returns the layout descriptor for the kernel's Thread registry. + kernel_thread_registry; + /// Returns the layout descriptor for the kernel's Thread item registry. + kernel_thread_item_registry; + /// Returns the layout descriptor for the kernel's Module Instance registry. + kernel_instance_registry; + /// Returns the layout descriptor for the kernel's Module Instance item registry. + kernel_instance_item_registry; + /// Returns the layout descriptor for the kernel's Module registry. + kernel_module_registry; + } } /// An address space segment descriptor.