common/mem/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! seL4 Cap 内存管理模块
//!
//!

use alloc::vec::Vec;
use sel4::{
    Cap, CapTypeForObjectOfFixedSize, CapTypeForObjectOfVariableSize, cap::Untyped, cap_type,
};
use sel4_kit::slot_manager::LeafSlot;

use crate::{
    page::PhysPage,
    slot::{alloc_slot, recycle_slot},
};

pub struct CapMemSet {
    /// (Untyped, size in bytes)
    untypes: Vec<(Untyped, usize)>,
    alloc_func: Option<fn() -> (Untyped, usize)>,
    recycle_frames: Vec<Cap<cap_type::Granule>>,
}

impl CapMemSet {
    pub fn new(alloc_func: Option<fn() -> (Untyped, usize)>) -> Self {
        CapMemSet {
            untypes: Vec::new(),
            alloc_func,
            recycle_frames: Vec::new(),
        }
    }

    pub fn check_available(&mut self, size: usize) {
        if let Some((_, available)) = self.untypes.last() {
            if *available > size {
                return;
            }
        }
        if let Some(func) = self.alloc_func {
            let (untyped, available) = func();
            if available >= size {
                self.untypes.push((untyped, available));
                return;
            }
        }
        panic!(
            "No available untyped memory for allocation of size {}",
            size
        );
    }

    pub fn untyped_list(&self) -> &[(Untyped, usize)] {
        &self.untypes
    }

    pub fn add(&mut self, untyped: Untyped, size: usize) {
        self.untypes.push((untyped, size));
    }

    pub fn alloc_fixed<T: CapTypeForObjectOfFixedSize>(&mut self) -> LeafSlot {
        let dst = alloc_slot();
        let phys_size = 1 << T::object_blueprint().physical_size_bits();
        self.check_available(phys_size);
        let last = self
            .untypes
            .last_mut()
            .expect("No untyped memory available");
        last.1 -= phys_size;
        last.0
            .untyped_retype(
                &T::object_blueprint(),
                &dst.cnode_abs_cptr(),
                dst.offset_of_cnode(),
                1,
            )
            .unwrap();
        dst
    }

    pub fn alloc_variable<T: CapTypeForObjectOfVariableSize>(
        &mut self,
        size_bits: usize,
    ) -> LeafSlot {
        let dst = alloc_slot();
        let phys_size = 1 << T::object_blueprint(size_bits).physical_size_bits();
        self.check_available(phys_size);
        let last = self
            .untypes
            .last_mut()
            .expect("No untyped memory available");
        last.1 -= phys_size;
        last.0
            .untyped_retype(
                &T::object_blueprint(size_bits),
                &dst.cnode_abs_cptr(),
                dst.offset_of_cnode(),
                1,
            )
            .unwrap();
        dst
    }

    #[inline]
    pub fn alloc_page(&mut self) -> Cap<cap_type::Granule> {
        match self.recycle_frames.pop() {
            Some(recycled_frame) => {
                PhysPage::new(recycled_frame).lock().fill(0);
                recycled_frame
            }
            None => self.alloc_fixed::<cap_type::Granule>().into(),
        }
    }

    #[inline]
    pub fn recycle_page(&mut self, frame_cap: Cap<cap_type::Granule>) {
        self.recycle_frames.push(frame_cap);
    }

    #[inline]
    pub fn alloc_pt(&mut self) -> Cap<cap_type::PT> {
        self.alloc_fixed::<cap_type::PT>().into()
    }

    #[inline]
    pub fn alloc_vspace(&mut self) -> Cap<cap_type::VSpace> {
        self.alloc_fixed::<cap_type::VSpace>().into()
    }

    #[inline]
    pub fn alloc_tcb(&mut self) -> Cap<cap_type::Tcb> {
        self.alloc_fixed::<cap_type::Tcb>().into()
    }

    /// 申请一个 [sel4::cap::CNode]
    #[inline]
    pub fn alloc_cnode(&mut self, size_bits: usize) -> Cap<cap_type::CNode> {
        self.alloc_variable::<cap_type::CNode>(size_bits).into()
    }

    pub fn release(&mut self) {
        self.recycle_frames.iter().for_each(|page_cap| {
            let slot = LeafSlot::from_cap(*page_cap);
            slot.revoke().unwrap();
            slot.delete().unwrap();
            recycle_slot(slot);
        });
    }
}