common/
obj_allocator.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use sel4::{
    Cap, CapTypeForObjectOfFixedSize,
    cap::{CNode, Endpoint, Granule, Notification, PT, Tcb, Untyped, VSpace},
    cap_type::{self},
    init_thread::slot,
};
use sel4_kit::slot_manager::LeafSlot;
use spin::Mutex;

pub struct ObjectAllocator {
    ut: Mutex<Untyped>,
}

impl ObjectAllocator {
    pub const fn empty() -> Self {
        Self {
            ut: Mutex::new(sel4::cap::Untyped::from_bits(0)),
        }
    }

    pub fn init(&self, untyped: Untyped) {
        *self.ut.lock() = untyped;
    }

    fn untyped(&self) -> Untyped {
        *self.ut.lock()
    }

    /// Allocate cap with Generic definition and size_bits before rebuilding the cspace
    pub fn allocate_variable_sized_origin<T: sel4::CapTypeForObjectOfVariableSize>(
        &self,
        size_bits: usize,
    ) -> sel4::Cap<T> {
        let leaf_slot = super::slot::alloc_slot();
        self.untyped()
            .untyped_retype(
                &T::object_blueprint(size_bits),
                &slot::CNODE.cap().absolute_cptr_for_self(),
                leaf_slot.offset_of_cnode(),
                1,
            )
            .unwrap();
        leaf_slot.cap()
    }
}

impl ObjectAllocator {
    #[inline]
    pub fn alloc_untyped(&self, size_bits: usize) -> Cap<cap_type::Untyped> {
        self.allocate_and_retyped_variable_sized(size_bits)
    }

    /// TODO: 申请多个位置,且判断位置是否超出
    pub fn allocate_slot(&self) -> LeafSlot {
        super::slot::alloc_slot()
    }

    pub fn extend_slot(&self, slot: LeafSlot) {
        loop {
            let cap_alloc = self.untyped().untyped_retype(
                &sel4::ObjectBlueprint::CNode { size_bits: 12 },
                &sel4::init_thread::slot::CNODE
                    .cap()
                    .absolute_cptr_for_self(),
                slot.cnode_idx(),
                1,
            );
            if cap_alloc == Ok(()) {
                break;
            } else if cap_alloc == Err(sel4::Error::NotEnoughMemory) {
                LeafSlot::from_cap(self.untyped()).delete().unwrap();
                crate::root::alloc_untyped(self.untyped().into()).unwrap();
            } else {
                cap_alloc.unwrap();
            }
        }
    }

    /// Allocate the slot at the new cspace.
    pub fn allocate_and_retype(&self, blueprint: sel4::ObjectBlueprint) -> sel4::cap::Unspecified {
        let leaf_slot = self.allocate_slot();
        loop {
            let cap_alloc = self.untyped().untyped_retype(
                &blueprint,
                &leaf_slot.cnode_abs_cptr(),
                leaf_slot.offset_of_cnode(),
                1,
            );
            if cap_alloc == Ok(()) {
                break;
            } else if cap_alloc == Err(sel4::Error::NotEnoughMemory) {
                LeafSlot::from_cap(self.untyped()).delete().unwrap();
                crate::root::alloc_untyped(self.untyped().into()).unwrap();
            } else {
                cap_alloc.unwrap();
            }
        }
        leaf_slot.cap()
    }

    /// Allocate the slot at the new cspace.
    pub fn retype_to_first(&self, blueprint: sel4::ObjectBlueprint) -> sel4::cap::Unspecified {
        self.untyped()
            .untyped_retype(
                &blueprint,
                &slot::CNODE.cap().absolute_cptr_from_bits_with_depth(0, 52),
                0,
                1,
            )
            .unwrap();
        sel4::init_thread::Slot::from_index(0).cap()
    }

    /// Allocate and retype the slot at the new cspace
    pub fn allocate_and_retyped_fixed_sized<T: sel4::CapTypeForObjectOfFixedSize>(
        &self,
    ) -> sel4::Cap<T> {
        self.allocate_and_retype(T::object_blueprint()).cast()
    }

    /// ALlocate and retype the slot at the new cspace
    pub fn allocate_and_retyped_variable_sized<T: sel4::CapTypeForObjectOfVariableSize>(
        &self,
        size_bits: usize,
    ) -> sel4::Cap<T> {
        self.allocate_and_retype(T::object_blueprint(size_bits))
            .cast()
    }

    /// 申请一个物理页 [Granule]
    #[inline]
    pub fn alloc_page(&self) -> Granule {
        self.allocate_and_retype(cap_type::Granule::object_blueprint())
            .cast()
    }

    /// 申请一个 [Endpoint]
    #[inline]
    pub fn alloc_endpoint(&self) -> Endpoint {
        self.allocate_and_retype(cap_type::Endpoint::object_blueprint())
            .cast()
    }

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

    /// 申请一个 [VSpace]
    #[inline]
    pub fn alloc_vspace(&self) -> VSpace {
        self.allocate_and_retyped_fixed_sized::<cap_type::VSpace>()
    }

    /// 申请一个页表 [PT]
    #[inline]
    pub fn alloc_pt(&self) -> PT {
        self.allocate_and_retyped_fixed_sized::<cap_type::PT>()
    }

    /// 申请一个进程控制块 [Tcb]
    #[inline]
    pub fn alloc_tcb(&self) -> Tcb {
        self.allocate_and_retyped_fixed_sized::<cap_type::Tcb>()
    }

    /// 申请一个 Notification [Notification]
    pub fn alloc_notification(&self) -> Notification {
        self.allocate_and_retyped_fixed_sized::<cap_type::Notification>()
    }

    /// 申请多个页
    #[cfg(feature = "alloc")]
    #[inline]
    pub fn alloc_pages(&self, pages: usize) -> alloc::vec::Vec<Granule> {
        let leaf_slot = super::slot::alloc_slots(pages);

        self.untyped()
            .untyped_retype(
                &cap_type::Granule::object_blueprint(),
                &leaf_slot.cnode_abs_cptr(),
                leaf_slot.offset_of_cnode(),
                pages,
            )
            .unwrap();

        (0..pages)
            .map(|x| leaf_slot.next_nth_slot(x).cap())
            .collect()
    }
}