sel4/
init_thread.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
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: MIT
//

//! Items that are applicable within the context of the root task's initial thread's CSpace.

use core::marker::PhantomData;
use core::ops::Range;

use sel4_config::sel4_cfg;

use crate::{
    cap_type,
    const_helpers::{u32_into_usize, usize_into_word, word_into_usize},
    sys, CPtr, CPtrBits, Cap, CapType,
};

/// The index of a slot in the initial thread's root CNode.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Slot<T: CapType = cap_type::Unspecified> {
    index: usize,
    _phantom: PhantomData<T>,
}

impl<T: CapType> Slot<T> {
    const fn from_sys(slot: u32) -> Self {
        Self::from_index(u32_into_usize(slot))
    }

    pub const fn from_index(index: usize) -> Self {
        Self {
            index,
            _phantom: PhantomData,
        }
    }

    pub const fn index(&self) -> usize {
        self.index
    }

    pub const fn cptr_bits(&self) -> CPtrBits {
        usize_into_word(self.index)
    }

    pub const fn cptr(&self) -> CPtr {
        CPtr::from_bits(self.cptr_bits())
    }

    pub const fn cap(&self) -> Cap<T> {
        self.cptr().cast()
    }

    pub const fn cast<T1: CapType>(&self) -> Slot<T1> {
        Slot::from_index(self.index)
    }

    pub const fn upcast(&self) -> Slot {
        self.cast()
    }
}

impl Slot {
    pub const fn downcast<T: CapType>(&self) -> Slot<T> {
        self.cast()
    }
}

/// Corresponds to `seL4_SlotRegion`.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SlotRegion<T: CapType> {
    range: Range<usize>,
    _phantom: PhantomData<T>,
}

#[allow(clippy::len_without_is_empty)]
impl<T: CapType> SlotRegion<T> {
    pub(crate) const fn from_range(range: Range<usize>) -> Self {
        Self {
            range,
            _phantom: PhantomData,
        }
    }

    pub(crate) const fn from_sys(sys: sys::seL4_SlotRegion) -> Self {
        Self::from_range(word_into_usize(sys.start)..word_into_usize(sys.end))
    }

    pub const fn start(&self) -> usize {
        self.range.start
    }

    pub const fn end(&self) -> usize {
        self.range.end
    }

    pub const fn range(&self) -> Range<usize> {
        self.start()..self.end()
    }

    pub fn len(&self) -> usize {
        self.range.len()
    }

    pub fn index(&self, i: usize) -> Slot<T> {
        assert!(i < self.len());
        Slot::from_index(self.range.start + i)
    }
}

/// Initial CSpace slot constants corresponding to `seL4_Cap*`.
pub mod slot {
    use super::{cap_type, sel4_cfg, sys, Slot};

    macro_rules! mk {
        [
            $(
                $(#[$outer:meta])*
                ($name:ident, $cap_type:ident, $sys_name:ident),
            )*
        ] => {
            $(
                $(#[$outer])*
                #[doc = "Corresponds to `"]
                #[doc = stringify!($sys_name)]
                #[doc = "`."]
                pub const $name: Slot<cap_type::$cap_type> = Slot::from_sys(sys::seL4_RootCNodeCapSlots::$sys_name);
            )*
        };
    }

    mk![
        (NULL, Null, seL4_CapNull),
        (TCB, Tcb, seL4_CapInitThreadTCB),
        (CNODE, CNode, seL4_CapInitThreadCNode),
        (VSPACE, VSpace, seL4_CapInitThreadVSpace),
        (IRQ_CONTROL, IrqControl, seL4_CapIRQControl),
        (ASID_CONTROL, AsidControl, seL4_CapASIDControl),
        (ASID_POOL, AsidPool, seL4_CapInitThreadASIDPool),
        #[sel4_cfg(not(ARCH_X86_64))]
        (IO_PORT_CONTROL, Null, seL4_CapIOPortControl),
        #[sel4_cfg(ARCH_X86_64)]
        (IO_PORT_CONTROL, IOPortControl, seL4_CapIOPortControl),
        #[cfg(any())] // TODO
        (IO_SPACE, Null, seL4_CapIOSpace),
        (BOOT_INFO_FRAME, Granule, seL4_CapBootInfoFrame),
        (IPC_BUFFER, Granule, seL4_CapInitThreadIPCBuffer),
        #[cfg(any())] // TODO
        (DOMAIN, Null, seL4_CapDomain),
        #[cfg(any())] // TODO
        (SMMU_SID_CONTROL, Null, seL4_CapSMMUSIDControl),
        #[cfg(any())] // TODO
        (SMMU_CB_CONTROL, Null, seL4_CapSMMUCBControl),
        #[sel4_cfg(KERNEL_MCS)]
        (SC, SchedControl, seL4_CapInitThreadSC),
        #[cfg(any())] // TODO
        (SMC, Null, seL4_CapSMC),
    ];
}

/// Suspends the initial thread using [`slot::TCB`].
#[cfg(feature = "state")]
pub fn suspend_self<T>() -> T {
    slot::TCB.cap().tcb_suspend().unwrap();

    unreachable!()
}