Skip to main content

strat9_kernel/ipc/
message.rs

1pub use strat9_abi::data::IpcMessage;
2use strat9_abi::data::{IPC_MESSAGE_ALIGN, IPC_MESSAGE_SIZE};
3
4use zerocopy::FromBytes;
5
6#[repr(C)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, FromBytes)]
8pub struct IpcLabel {
9    pub tier: u8,
10    pub family: u8,
11    pub compartment: u16,
12}
13
14/// Performs the ipc message from raw operation.
15pub fn ipc_message_from_raw(buf: &[u8; IPC_MESSAGE_SIZE]) -> IpcMessage {
16    // SAFETY: `buf` has exactly `IPC_MESSAGE_SIZE` bytes, matching `IpcMessage` size.
17    unsafe { core::ptr::read_unaligned(buf.as_ptr() as *const IpcMessage) }
18}
19
20/// Performs the ipc message to raw operation.
21pub fn ipc_message_to_raw(msg: &IpcMessage, out: &mut [u8; IPC_MESSAGE_SIZE]) {
22    // SAFETY: `out` has exactly `IPC_MESSAGE_SIZE` bytes, matching `IpcMessage` size.
23    unsafe {
24        core::ptr::copy_nonoverlapping(
25            msg as *const _ as *const u8,
26            out.as_mut_ptr(),
27            IPC_MESSAGE_SIZE,
28        );
29    }
30}
31
32static_assertions::assert_eq_size!(IpcMessage, [u8; IPC_MESSAGE_SIZE]);
33static_assertions::const_assert_eq!(core::mem::align_of::<IpcMessage>(), IPC_MESSAGE_ALIGN);