monitor/mon/compartment/
compconfig.rs

1use monitor_api::SharedCompConfig;
2use talc::Span;
3use twizzler_abi::object::{MAX_SIZE, NULLPAGE_SIZE};
4
5use crate::mon::space::MapHandle;
6
7/// Manages a compartment configuration object.
8pub struct CompConfigObject {
9    handle: MapHandle,
10}
11
12impl CompConfigObject {
13    /// Create a new CompConfigObject from a handle. Initializes the comp config.
14    pub fn new(handle: MapHandle, init_val: SharedCompConfig) -> Self {
15        let mut this = Self { handle };
16        this.write_config(init_val);
17        this
18    }
19
20    /// Write a comp config to this object.
21    pub fn write_config(&mut self, val: SharedCompConfig) {
22        // Safety: only the monitor can write to a comp config object, and we have a mutable
23        // reference to it.
24        unsafe {
25            let base = self.handle.monitor_data_base();
26            (base as *mut SharedCompConfig).write(val);
27        }
28    }
29
30    /// Read the comp config data.
31    pub(crate) fn read_comp_config(&self) -> SharedCompConfig {
32        // Safety: no other compartment can write this.
33        unsafe { self.get_comp_config().read() }
34    }
35
36    /// Get a pointer to this comp config data.
37    pub fn get_comp_config(&self) -> *const SharedCompConfig {
38        self.handle.monitor_data_base() as *const SharedCompConfig
39    }
40
41    /// Return a span that can be used for allocations in the comp config object.
42    pub fn alloc_span(&self) -> Span {
43        let offset_from_base =
44            core::mem::size_of::<SharedCompConfig>().next_multiple_of(NULLPAGE_SIZE);
45        assert!(offset_from_base < MAX_SIZE / 2);
46        // Safety: the pointers stay in-bounds (in an object).
47        unsafe {
48            Span::new(
49                self.handle.monitor_data_base().add(offset_from_base),
50                self.handle.monitor_data_start().add(MAX_SIZE / 2),
51            )
52        }
53    }
54}