sel4_microkit_base/
message.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7/// Type alias for [`MessageInfo`] labels.
8pub type MessageLabel = sel4::Word;
9
10/// Type alias for message register values.
11pub type MessageRegisterValue = sel4::Word;
12
13/// Corresponds to `microkit_msginfo`.
14#[derive(Debug, Clone)]
15pub struct MessageInfo {
16    inner: sel4::MessageInfo,
17}
18
19impl MessageInfo {
20    #[doc(hidden)]
21    pub fn from_inner(inner: sel4::MessageInfo) -> Self {
22        Self { inner }
23    }
24
25    #[doc(hidden)]
26    pub fn inner(&self) -> &sel4::MessageInfo {
27        &self.inner
28    }
29
30    #[doc(hidden)]
31    pub fn into_inner(self) -> sel4::MessageInfo {
32        self.inner
33    }
34
35    pub fn new(label: MessageLabel, count: usize) -> Self {
36        Self::from_inner(sel4::MessageInfo::new(label, 0, 0, count))
37    }
38
39    /// The label associated with this message.
40    pub fn label(&self) -> MessageLabel {
41        self.inner.label()
42    }
43
44    /// The number of meaningful bits in `MessageLabel`.
45    pub const fn label_width() -> usize {
46        sel4::MessageInfo::label_width()
47    }
48
49    /// The number of filled message registers associated with this message.
50    pub fn count(&self) -> usize {
51        self.inner.length()
52    }
53
54    /// Interpret this message as a [`sel4::Fault`].
55    pub fn fault(&self) -> sel4::Fault {
56        sel4::with_ipc_buffer(|ipc_buffer| sel4::Fault::new(ipc_buffer, self.inner()))
57    }
58}
59
60impl Default for MessageInfo {
61    fn default() -> Self {
62        Self::new(0, 0)
63    }
64}
65
66/// Provides access to the protection domain's message registers.
67pub fn with_msg_regs<T>(f: impl FnOnce(&[MessageRegisterValue]) -> T) -> T {
68    sel4::with_ipc_buffer(|ipc_buffer| f(ipc_buffer.msg_regs()))
69}
70
71/// Provides mutable access to the protection domain's message registers.
72pub fn with_msg_regs_mut<T>(f: impl FnOnce(&mut [MessageRegisterValue]) -> T) -> T {
73    sel4::with_ipc_buffer_mut(|ipc_buffer| f(ipc_buffer.msg_regs_mut()))
74}
75
76/// Provides access to the protection domain's message registers, viewed as an array of bytes.
77pub fn with_msg_bytes<T>(f: impl FnOnce(&[u8]) -> T) -> T {
78    sel4::with_ipc_buffer(|ipc_buffer| f(ipc_buffer.msg_bytes()))
79}
80
81/// Provides mutable access to the protection domain's message registers, viewed as an array of
82/// bytes.
83pub fn with_msg_bytes_mut<T>(f: impl FnOnce(&mut [u8]) -> T) -> T {
84    sel4::with_ipc_buffer_mut(|ipc_buffer| f(ipc_buffer.msg_bytes_mut()))
85}
86
87/// Corresponds to `microkit_mr_set`.
88pub fn set_mr(i: usize, value: MessageRegisterValue) {
89    with_msg_regs_mut(|regs| regs[i] = value)
90}
91
92/// Corresponds to `microkit_mr_get`.
93pub fn get_mr(i: usize) -> MessageRegisterValue {
94    with_msg_regs(|regs| regs[i])
95}