Nothing to see here, move along meow
1use crate::header::{KernelObjectHeader, NONE_SENTINEL};
2use crate::object_tag::ObjectTag;
3use crate::untyped::{self, UntypedState};
4
5pub trait KernelObject: Sized {
6 const TAG: ObjectTag;
7 const METADATA_SIZE: usize;
8 const METADATA_ALIGN: usize;
9
10 fn init_default(header: KernelObjectHeader) -> Self;
11}
12
13#[derive(Debug, Clone, Copy)]
14#[repr(C)]
15pub struct EndpointObject {
16 pub header: KernelObjectHeader,
17 pub sender_head: u32,
18 pub sender_tail: u32,
19 pub sender_len: u16,
20 pub receiver_head: u32,
21 pub receiver_tail: u32,
22 pub receiver_len: u16,
23 pub holder: u32,
24 pub _pad: [u8; 12],
25}
26
27const _: () = assert!(core::mem::size_of::<EndpointObject>() == 64);
28
29impl KernelObject for EndpointObject {
30 const TAG: ObjectTag = ObjectTag::Endpoint;
31 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
32 const METADATA_ALIGN: usize = 64;
33
34 fn init_default(header: KernelObjectHeader) -> Self {
35 Self {
36 header,
37 sender_head: NONE_SENTINEL,
38 sender_tail: NONE_SENTINEL,
39 sender_len: 0,
40 receiver_head: NONE_SENTINEL,
41 receiver_tail: NONE_SENTINEL,
42 receiver_len: 0,
43 holder: NONE_SENTINEL,
44 _pad: [0; 12],
45 }
46 }
47}
48
49#[derive(Debug, Clone, Copy)]
50#[repr(C)]
51pub struct NotificationObject {
52 pub header: KernelObjectHeader,
53 pub word: u64,
54 pub waiters: [u32; 4],
55 pub waiter_count: u8,
56 pub _pad: [u8; 15],
57}
58
59const _: () = assert!(core::mem::size_of::<NotificationObject>() == 64);
60
61impl KernelObject for NotificationObject {
62 const TAG: ObjectTag = ObjectTag::Notification;
63 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
64 const METADATA_ALIGN: usize = 64;
65
66 fn init_default(header: KernelObjectHeader) -> Self {
67 Self {
68 header,
69 word: 0,
70 waiters: [NONE_SENTINEL; 4],
71 waiter_count: 0,
72 _pad: [0; 15],
73 }
74 }
75}
76
77#[derive(Debug, Clone, Copy)]
78#[repr(C)]
79pub struct SchedContextObject {
80 pub header: KernelObjectHeader,
81 pub replenish_at: u64,
82 pub budget_us: u64,
83 pub period_us: u64,
84 pub remaining_us: u64,
85 pub attached_pid: u32,
86 pub priority: u8,
87 pub flags: u8,
88 pub _pad: [u8; 2],
89}
90
91const _: () = assert!(core::mem::size_of::<SchedContextObject>() == 64);
92
93impl KernelObject for SchedContextObject {
94 const TAG: ObjectTag = ObjectTag::SchedContext;
95 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
96 const METADATA_ALIGN: usize = 64;
97
98 fn init_default(header: KernelObjectHeader) -> Self {
99 Self {
100 header,
101 replenish_at: 0,
102 budget_us: 0,
103 period_us: 0,
104 remaining_us: 0,
105 attached_pid: NONE_SENTINEL,
106 priority: 0,
107 flags: 0,
108 _pad: [0; 2],
109 }
110 }
111}
112
113#[derive(Debug, Clone, Copy)]
114#[repr(C)]
115pub struct UntypedObject {
116 pub header: KernelObjectHeader,
117 pub phys_base: u64,
118 pub watermark: u32,
119 pub child_count: u32,
120 pub first_child: u32,
121 pub size_bits: u8,
122 pub is_device: u8,
123 pub _pad: [u8; 18],
124}
125
126const _: () = assert!(core::mem::size_of::<UntypedObject>() == 64);
127
128impl KernelObject for UntypedObject {
129 const TAG: ObjectTag = ObjectTag::Untyped;
130 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
131 const METADATA_ALIGN: usize = 64;
132
133 fn init_default(header: KernelObjectHeader) -> Self {
134 Self {
135 header,
136 phys_base: 0,
137 watermark: 0,
138 child_count: 0,
139 first_child: NONE_SENTINEL,
140 size_bits: 0,
141 is_device: 0,
142 _pad: [0; 18],
143 }
144 }
145}
146
147impl UntypedObject {
148 pub const fn to_state(&self) -> UntypedState {
149 UntypedState::from_parts(
150 self.watermark,
151 self.size_bits,
152 self.is_device != 0,
153 self.child_count,
154 )
155 }
156
157 pub fn apply_state(&mut self, state: &UntypedState) {
158 self.watermark = state.watermark();
159 self.size_bits = state.size_bits();
160 self.is_device = match state.is_device() {
161 true => 1,
162 false => 0,
163 };
164 self.child_count = state.child_count();
165 }
166}
167
168#[derive(Debug, Clone, Copy)]
169#[repr(C)]
170pub struct IrqHandlerObject {
171 pub header: KernelObjectHeader,
172 pub vector: u8,
173 pub source_kind: u8,
174 pub port_base: u16,
175 pub port_count: u16,
176 pub _pad1: [u8; 2],
177 pub source_data: u32,
178 pub _pad2: [u8; 28],
179}
180
181const _: () = assert!(core::mem::size_of::<IrqHandlerObject>() == 64);
182
183impl KernelObject for IrqHandlerObject {
184 const TAG: ObjectTag = ObjectTag::IrqHandler;
185 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
186 const METADATA_ALIGN: usize = 64;
187
188 fn init_default(header: KernelObjectHeader) -> Self {
189 Self {
190 header,
191 vector: 0,
192 source_kind: 0,
193 port_base: 0,
194 port_count: 0,
195 _pad1: [0; 2],
196 source_data: 0,
197 _pad2: [0; 28],
198 }
199 }
200}
201
202#[derive(Debug, Clone, Copy)]
203#[repr(C)]
204pub struct FramebufferObject {
205 pub header: KernelObjectHeader,
206 pub phys_addr: u64,
207 pub byte_size: u64,
208 pub width: u32,
209 pub height: u32,
210 pub pitch: u32,
211 pub bpp: u16,
212 pub _pad: [u8; 10],
213}
214
215const _: () = assert!(core::mem::size_of::<FramebufferObject>() == 64);
216
217impl KernelObject for FramebufferObject {
218 const TAG: ObjectTag = ObjectTag::Framebuffer;
219 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
220 const METADATA_ALIGN: usize = 64;
221
222 fn init_default(header: KernelObjectHeader) -> Self {
223 Self {
224 header,
225 phys_addr: 0,
226 byte_size: 0,
227 width: 0,
228 height: 0,
229 pitch: 0,
230 bpp: 0,
231 _pad: [0; 10],
232 }
233 }
234}
235
236#[derive(Debug, Clone, Copy)]
237#[repr(C)]
238pub struct PciDeviceObject {
239 pub header: KernelObjectHeader,
240 pub device_table_idx: u8,
241 pub _pad: [u8; 39],
242}
243
244const _: () = assert!(core::mem::size_of::<PciDeviceObject>() == 64);
245
246impl KernelObject for PciDeviceObject {
247 const TAG: ObjectTag = ObjectTag::PciDevice;
248 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
249 const METADATA_ALIGN: usize = 64;
250
251 fn init_default(header: KernelObjectHeader) -> Self {
252 Self {
253 header,
254 device_table_idx: 0,
255 _pad: [0; 39],
256 }
257 }
258}
259
260#[derive(Debug, Clone, Copy)]
261#[repr(C)]
262pub struct ProcessObject {
263 pub header: KernelObjectHeader,
264 pub pid: u32,
265 pub _pad: [u8; 36],
266}
267
268const _: () = assert!(core::mem::size_of::<ProcessObject>() == 64);
269
270impl KernelObject for ProcessObject {
271 const TAG: ObjectTag = ObjectTag::Process;
272 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
273 const METADATA_ALIGN: usize = 64;
274
275 fn init_default(header: KernelObjectHeader) -> Self {
276 Self {
277 header,
278 pid: NONE_SENTINEL,
279 _pad: [0; 36],
280 }
281 }
282}
283
284#[derive(Debug, Clone, Copy)]
285#[repr(C)]
286pub struct FrameObject {
287 pub header: KernelObjectHeader,
288 pub phys_addr: u64,
289 pub size_bits: u8,
290 pub _pad0: u8,
291 pub frame_table_idx: u16,
292 pub _pad: [u8; 28],
293}
294
295const _: () = assert!(core::mem::size_of::<FrameObject>() == 64);
296
297impl KernelObject for FrameObject {
298 const TAG: ObjectTag = ObjectTag::Frame;
299 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
300 const METADATA_ALIGN: usize = 64;
301
302 fn init_default(header: KernelObjectHeader) -> Self {
303 Self {
304 header,
305 phys_addr: 0,
306 size_bits: 12,
307 _pad0: 0,
308 frame_table_idx: NONE_SENTINEL as u16,
309 _pad: [0; 28],
310 }
311 }
312}
313
314#[derive(Debug, Clone, Copy)]
315#[repr(C)]
316pub struct CNodeObject {
317 pub header: KernelObjectHeader,
318 pub slots_phys: u64,
319 pub size_bits: u8,
320 pub frame_count: u8,
321 pub _pad: [u8; 30],
322}
323
324const _: () = assert!(core::mem::size_of::<CNodeObject>() == 64);
325
326impl KernelObject for CNodeObject {
327 const TAG: ObjectTag = ObjectTag::CNode;
328 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
329 const METADATA_ALIGN: usize = 64;
330
331 fn init_default(header: KernelObjectHeader) -> Self {
332 Self {
333 header,
334 slots_phys: 0,
335 size_bits: 0,
336 frame_count: 0,
337 _pad: [0; 30],
338 }
339 }
340}
341
342macro_rules! assert_layout_matches {
343 ($obj:ty, $size:expr, $align:expr) => {
344 const _: () = assert!(<$obj>::METADATA_SIZE as u32 == $size);
345 const _: () = assert!(<$obj>::METADATA_ALIGN as u32 == $align);
346 };
347}
348
349assert_layout_matches!(
350 EndpointObject,
351 untyped::FIXED_OBJECT_SIZE,
352 untyped::FIXED_OBJECT_ALIGN
353);
354assert_layout_matches!(
355 NotificationObject,
356 untyped::FIXED_OBJECT_SIZE,
357 untyped::FIXED_OBJECT_ALIGN
358);
359assert_layout_matches!(
360 SchedContextObject,
361 untyped::FIXED_OBJECT_SIZE,
362 untyped::FIXED_OBJECT_ALIGN
363);
364assert_layout_matches!(
365 UntypedObject,
366 untyped::FIXED_OBJECT_SIZE,
367 untyped::FIXED_OBJECT_ALIGN
368);
369assert_layout_matches!(
370 IrqHandlerObject,
371 untyped::FIXED_OBJECT_SIZE,
372 untyped::FIXED_OBJECT_ALIGN
373);
374assert_layout_matches!(
375 FramebufferObject,
376 untyped::FIXED_OBJECT_SIZE,
377 untyped::FIXED_OBJECT_ALIGN
378);
379assert_layout_matches!(
380 PciDeviceObject,
381 untyped::FIXED_OBJECT_SIZE,
382 untyped::FIXED_OBJECT_ALIGN
383);
384assert_layout_matches!(
385 ProcessObject,
386 untyped::FIXED_OBJECT_SIZE,
387 untyped::FIXED_OBJECT_ALIGN
388);
389assert_layout_matches!(
390 FrameObject,
391 untyped::FIXED_OBJECT_SIZE,
392 untyped::FIXED_OBJECT_ALIGN
393);
394assert_layout_matches!(
395 CNodeObject,
396 untyped::FIXED_OBJECT_SIZE,
397 untyped::FIXED_OBJECT_ALIGN
398);
399
400#[derive(Debug, Clone, Copy)]
401#[repr(C)]
402pub struct VRegionObject {
403 pub header: KernelObjectHeader,
404 pub phys_base: u64,
405 pub owner_vaddr: u64,
406 pub child_vaddr: u64,
407 pub owner_pid: u32,
408 pub child_pid: u32,
409 pub page_count: u16,
410 pub flags: crate::types::VRegionFlags,
411 pub is_derived: u8,
412 pub _pad: [u8; 3],
413}
414
415const _: () = assert!(core::mem::size_of::<VRegionObject>() == 64);
416
417impl KernelObject for VRegionObject {
418 const TAG: ObjectTag = ObjectTag::VRegion;
419 const METADATA_SIZE: usize = core::mem::size_of::<Self>();
420 const METADATA_ALIGN: usize = 64;
421
422 fn init_default(header: KernelObjectHeader) -> Self {
423 Self {
424 header,
425 phys_base: 0,
426 owner_vaddr: 0,
427 owner_pid: NONE_SENTINEL,
428 child_vaddr: 0,
429 child_pid: NONE_SENTINEL,
430 page_count: 0,
431 flags: crate::types::VRegionFlags::from_raw_unchecked(0),
432 is_derived: 0,
433 _pad: [0; 3],
434 }
435 }
436}
437
438assert_layout_matches!(
439 VRegionObject,
440 untyped::FIXED_OBJECT_SIZE,
441 untyped::FIXED_OBJECT_ALIGN
442);