sel4/
object.rs

1//
2// Copyright 2023, Colias Group, LLC
3// Copyright (c) 2020 Arm Limited
4//
5// SPDX-License-Identifier: MIT
6//
7
8use core::ffi::c_uint;
9
10use crate::{
11    const_helpers::u32_into_usize, sel4_cfg, sel4_cfg_enum, sel4_cfg_wrap_match, sys, CapType,
12    ObjectBlueprintArch, ObjectTypeArch,
13};
14
15#[sel4_cfg(KERNEL_MCS)]
16use crate::const_helpers::usize_max;
17
18#[sel4_cfg(KERNEL_MCS)]
19pub const MIN_SCHED_CONTEXT_BITS: usize = u32_into_usize(sys::seL4_MinSchedContextBits);
20
21/// Corresponds to `seL4_ObjectType`.
22#[sel4_cfg_enum]
23#[derive(Debug, Clone, Eq, PartialEq)]
24pub enum ObjectType {
25    Untyped,
26    Endpoint,
27    Notification,
28    CNode,
29    Tcb,
30    #[sel4_cfg(KERNEL_MCS)]
31    SchedContext,
32    #[sel4_cfg(KERNEL_MCS)]
33    Reply,
34    Arch(ObjectTypeArch),
35}
36
37impl ObjectType {
38    pub const fn into_sys(self) -> c_uint {
39        sel4_cfg_wrap_match! {
40            match self {
41                Self::Untyped => sys::api_object::seL4_UntypedObject,
42                Self::Endpoint => sys::api_object::seL4_EndpointObject,
43                Self::Notification => sys::api_object::seL4_NotificationObject,
44                Self::CNode => sys::api_object::seL4_CapTableObject,
45                Self::Tcb => sys::api_object::seL4_TCBObject,
46                #[sel4_cfg(KERNEL_MCS)]
47                Self::SchedContext => sys::api_object::seL4_SchedContextObject,
48                #[sel4_cfg(KERNEL_MCS)]
49                Self::Reply => sys::api_object::seL4_ReplyObject,
50                Self::Arch(arch) => arch.into_sys(),
51            }
52        }
53    }
54}
55
56impl From<ObjectTypeArch> for ObjectType {
57    fn from(ty: ObjectTypeArch) -> Self {
58        Self::Arch(ty)
59    }
60}
61
62/// An object description for [`Untyped::untyped_retype`](crate::cap::Untyped::untyped_retype).
63#[sel4_cfg_enum]
64#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
65pub enum ObjectBlueprint {
66    Untyped {
67        size_bits: usize,
68    },
69    Endpoint,
70    Notification,
71    CNode {
72        size_bits: usize,
73    },
74    Tcb,
75    #[sel4_cfg(KERNEL_MCS)]
76    SchedContext {
77        size_bits: usize,
78    },
79    #[sel4_cfg(KERNEL_MCS)]
80    Reply,
81    Arch(ObjectBlueprintArch),
82}
83
84impl ObjectBlueprint {
85    pub const fn ty(self) -> ObjectType {
86        sel4_cfg_wrap_match! {
87            match self {
88                Self::Untyped { .. } => ObjectType::Untyped,
89                Self::Endpoint => ObjectType::Endpoint,
90                Self::Notification => ObjectType::Notification,
91                Self::CNode { .. } => ObjectType::CNode,
92                Self::Tcb => ObjectType::Tcb,
93                #[sel4_cfg(KERNEL_MCS)]
94                Self::SchedContext { .. } => ObjectType::SchedContext,
95                #[sel4_cfg(KERNEL_MCS)]
96                Self::Reply => ObjectType::Reply,
97                Self::Arch(arch) => ObjectType::Arch(arch.ty()),
98            }
99        }
100    }
101
102    pub const fn api_size_bits(self) -> Option<usize> {
103        sel4_cfg_wrap_match! {
104            match self {
105                Self::Untyped { size_bits } => Some(size_bits),
106                Self::CNode { size_bits } => Some(size_bits),
107                #[sel4_cfg(KERNEL_MCS)]
108                Self::SchedContext { size_bits } => Some(size_bits),
109                _ => None,
110            }
111        }
112    }
113
114    pub const fn physical_size_bits(self) -> usize {
115        sel4_cfg_wrap_match! {
116            match self {
117                Self::Untyped { size_bits } => size_bits,
118                Self::Endpoint => u32_into_usize(sys::seL4_EndpointBits),
119                Self::Notification => u32_into_usize(sys::seL4_NotificationBits),
120                Self::CNode { size_bits } => u32_into_usize(sys::seL4_SlotBits) + size_bits,
121                Self::Tcb => u32_into_usize(sys::seL4_TCBBits),
122                #[sel4_cfg(KERNEL_MCS)]
123                Self::SchedContext { size_bits } => usize_max(MIN_SCHED_CONTEXT_BITS, size_bits),
124                #[sel4_cfg(KERNEL_MCS)]
125                Self::Reply => u32_into_usize(sys::seL4_ReplyBits),
126                Self::Arch(arch) => arch.physical_size_bits(),
127            }
128        }
129    }
130
131    pub const fn asid_pool() -> Self {
132        Self::Untyped {
133            size_bits: u32_into_usize(sys::seL4_ASIDPoolBits),
134        }
135    }
136}
137
138impl From<ObjectBlueprintArch> for ObjectBlueprint {
139    fn from(blueprint: ObjectBlueprintArch) -> Self {
140        Self::Arch(blueprint)
141    }
142}
143
144/// Trait for [`CapType`]s which correspond to kernel objects.
145pub trait CapTypeForObject: CapType {
146    fn object_type() -> ObjectType;
147}
148
149/// Trait for [`CapType`]s which correspond to kernel objects of fixed size.
150pub trait CapTypeForObjectOfFixedSize: CapTypeForObject {
151    fn object_blueprint() -> ObjectBlueprint;
152}
153
154/// Trait for [`CapType`]s which correspond to kernel objects of variable size.
155pub trait CapTypeForObjectOfVariableSize: CapTypeForObject {
156    fn object_blueprint(size_bits: usize) -> ObjectBlueprint;
157}