|
| 1 | +//! `oro-sync` backing functionality. |
| 2 | +//! |
| 3 | +//! These are mostly just implementations to allow certain advanced features of |
| 4 | +//! the `oro-sync` crate work (e.g. `ReentrantMutex`). |
| 5 | +
|
| 6 | +use core::mem::MaybeUninit; |
| 7 | + |
| 8 | +use crate::arch::Arch; |
| 9 | + |
| 10 | +/// Holds the "global" kernel ID function pointer, which retrieves the **core local** |
| 11 | +/// kernel ID for the calling context's current core. |
| 12 | +/// |
| 13 | +/// This is an ergonomics hack to avoid `A: Arch` in a lot of places. |
| 14 | +static mut KERNEL_ID_FN: MaybeUninit<fn() -> u32> = MaybeUninit::uninit(); |
| 15 | + |
| 16 | +/// Debug field for ensuring the kernel ID function is set. |
| 17 | +#[cfg(debug_assertions)] |
| 18 | +static HAS_SET_KERNEL_ID_FN: core::sync::atomic::AtomicBool = |
| 19 | + core::sync::atomic::AtomicBool::new(false); |
| 20 | + |
| 21 | +/// Retrieves the current core's kernel ID. This is linked to by `oro-sync` for the |
| 22 | +/// [`oro_sync::ReentrantLock`] implementation. |
| 23 | +#[doc(hidden)] |
| 24 | +#[no_mangle] |
| 25 | +unsafe extern "C" fn oro_sync_current_core_id() -> u32 { |
| 26 | + #[cfg(debug_assertions)] |
| 27 | + { |
| 28 | + assert!( |
| 29 | + HAS_SET_KERNEL_ID_FN.load(core::sync::atomic::Ordering::Relaxed), |
| 30 | + "kernel ID function not set" |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + KERNEL_ID_FN.assume_init()() |
| 35 | +} |
| 36 | + |
| 37 | +/// The generic kernel ID fetcher, based on the [`Arch`] type. |
| 38 | +#[doc(hidden)] |
| 39 | +fn get_arch_kernel_id<A: Arch>() -> u32 { |
| 40 | + crate::Kernel::<A>::get().id() |
| 41 | +} |
| 42 | + |
| 43 | +/// Initializes the kernel ID function pointer. |
| 44 | +/// |
| 45 | +/// # Safety |
| 46 | +/// This must be called **exactly once** prior to any [`oro_sync::ReentrantMutex`] locking. |
| 47 | +/// |
| 48 | +/// Further, `A` **must** be the same [`Arch`] type as the kernel being initialized, |
| 49 | +/// and **must** be the same across **all cores**. |
| 50 | +pub unsafe fn initialize_kernel_id_fn<A: Arch>() { |
| 51 | + #[cfg(debug_assertions)] |
| 52 | + { |
| 53 | + HAS_SET_KERNEL_ID_FN.store(true, core::sync::atomic::Ordering::Relaxed); |
| 54 | + } |
| 55 | + |
| 56 | + // SAFETY(qix-): We have offloaded safety considerations to the caller here. |
| 57 | + #[expect(static_mut_refs)] |
| 58 | + { |
| 59 | + KERNEL_ID_FN.write(get_arch_kernel_id::<A>); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Installs a dummy kernel ID for use during early boot. |
| 64 | +/// |
| 65 | +/// # Safety |
| 66 | +/// This function *may* be called prior to [`initialize_kernel_id_fn`], but |
| 67 | +/// **must** be called prior to any locking with [`oro_sync::ReentrantMutex`] |
| 68 | +/// if it is. |
| 69 | +/// |
| 70 | +/// **The handler installed by this function must not be used once multiple |
| 71 | +/// cores are active.** |
| 72 | +pub unsafe fn install_dummy_kernel_id_fn() { |
| 73 | + #[cfg(debug_assertions)] |
| 74 | + { |
| 75 | + HAS_SET_KERNEL_ID_FN.store(true, core::sync::atomic::Ordering::Relaxed); |
| 76 | + } |
| 77 | + |
| 78 | + // SAFETY(qix-): We have offloaded safety considerations to the caller here. |
| 79 | + #[expect(static_mut_refs)] |
| 80 | + { |
| 81 | + KERNEL_ID_FN.write(|| 0); |
| 82 | + } |
| 83 | +} |
0 commit comments