embedded_shadow/shadow/
kernel.rs

1#![allow(unsafe_code)]
2
3use crate::{
4    AccessPolicy, PersistTrigger, policy::PersistPolicy, storage::ShadowStorageBase,
5    view::KernelView,
6};
7
8pub struct KernelShadow<'a, const TS: usize, const BS: usize, const BC: usize, AP, PP, PT, PK, SS>
9where
10    AP: AccessPolicy,
11    PP: PersistPolicy<PK>,
12    PT: PersistTrigger<PK>,
13    bitmaps::BitsImpl<BC>: bitmaps::Bits,
14{
15    storage: &'a ShadowStorageBase<TS, BS, BC, AP, PP, PT, PK, SS>,
16}
17
18impl<'a, const TS: usize, const BS: usize, const BC: usize, AP, PP, PT, PK, SS>
19    KernelShadow<'a, TS, BS, BC, AP, PP, PT, PK, SS>
20where
21    AP: AccessPolicy,
22    PP: PersistPolicy<PK>,
23    PT: PersistTrigger<PK>,
24    bitmaps::BitsImpl<BC>: bitmaps::Bits,
25{
26    pub(crate) fn new(storage: &'a ShadowStorageBase<TS, BS, BC, AP, PP, PT, PK, SS>) -> Self {
27        Self { storage }
28    }
29
30    pub fn with_view<R>(&self, f: impl FnOnce(&mut KernelView<TS, BS, BC>) -> R) -> R {
31        critical_section::with(|_| unsafe { self.with_view_unchecked(f) })
32    }
33
34    /// # Safety
35    /// This function is unsafe because it requires exclusive access to the ShadowStorage.
36    /// You must ensure that no other code is accessing the ShadowStorage at the same time.
37    /// Generally, if your kernel is running inside an ISR and cannot be interrupted by other ISRs,
38    /// then it is safe to call this function.
39    pub unsafe fn with_view_unchecked<R>(
40        &self,
41        f: impl FnOnce(&mut KernelView<TS, BS, BC>) -> R,
42    ) -> R {
43        let table = unsafe { &mut *self.storage.table.get() };
44        let mut view = KernelView::new(table);
45        f(&mut view)
46    }
47}