Capture FFA_MEM_RECLAIM flags in dedicated type
Add MemReclaimFlags for handling the flags of the FFA_MEM_RECLAIM
interface.
Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I0d4c5c8089d1f2a3411985a808f28eaf05de9ea3
diff --git a/src/lib.rs b/src/lib.rs
index 2e6a119..7e8e18e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -62,6 +62,8 @@
InvalidVersionForFunctionId(Version, FuncId),
#[error("Invalid character count {0}")]
InvalidCharacterCount(u8),
+ #[error("Invalid memory reclaim flags {0}")]
+ InvalidMemReclaimFlags(u32),
#[error("Memory management error")]
MemoryManagementError(#[from] memory_management::Error),
}
@@ -87,6 +89,7 @@
| Error::InvalidNotificationCount
| Error::InvalidPartitionInfoGetRegsResponse
| Error::InvalidCharacterCount(_)
+ | Error::InvalidMemReclaimFlags(_)
| Error::MemoryManagementError(_) => Self::InvalidParameters,
}
}
@@ -1315,7 +1318,7 @@
MemRelinquish,
MemReclaim {
handle: memory_management::Handle,
- flags: u32,
+ flags: memory_management::MemReclaimFlags,
},
MemPermGet {
addr: MemAddr,
@@ -1825,7 +1828,7 @@
FuncId::MemRelinquish => Self::MemRelinquish,
FuncId::MemReclaim => Self::MemReclaim {
handle: memory_management::Handle::from([regs[1] as u32, regs[2] as u32]),
- flags: regs[3] as u32,
+ flags: (regs[3] as u32).try_into()?,
},
FuncId::MemPermGet32 => Self::MemPermGet {
addr: MemAddr::Addr32(regs[1] as u32),
@@ -2294,7 +2297,7 @@
let handle_regs: [u32; 2] = handle.into();
a[1] = handle_regs[0].into();
a[2] = handle_regs[1].into();
- a[3] = flags.into();
+ a[3] = u32::from(flags).into();
}
Interface::MemPermGet { addr, page_cnt } => {
a[1] = match addr {
diff --git a/src/memory_management.rs b/src/memory_management.rs
index aa7aeef..3203c15 100644
--- a/src/memory_management.rs
+++ b/src/memory_management.rs
@@ -847,6 +847,47 @@
}
}
+/// Flags field of the FFA_MEM_RECLAIM interface.
+#[derive(Debug, Eq, PartialEq, Clone, Copy)]
+pub struct MemReclaimFlags {
+ pub zero_memory: bool,
+ pub time_slicing: bool,
+}
+
+impl MemReclaimFlags {
+ pub const ZERO_MEMORY: u32 = 0b1 << 0;
+ pub const TIME_SLICING: u32 = 0b1 << 1;
+ const MBZ_BITS: u32 = 0xffff_fffc;
+}
+
+impl TryFrom<u32> for MemReclaimFlags {
+ type Error = crate::Error;
+
+ fn try_from(val: u32) -> Result<Self, Self::Error> {
+ if (val & Self::MBZ_BITS) != 0 {
+ Err(crate::Error::InvalidMemReclaimFlags(val))
+ } else {
+ Ok(MemReclaimFlags {
+ zero_memory: val & Self::ZERO_MEMORY != 0,
+ time_slicing: val & Self::TIME_SLICING != 0,
+ })
+ }
+ }
+}
+
+impl From<MemReclaimFlags> for u32 {
+ fn from(flags: MemReclaimFlags) -> Self {
+ let mut bits: u32 = 0;
+ if flags.zero_memory {
+ bits |= MemReclaimFlags::ZERO_MEMORY;
+ }
+ if flags.time_slicing {
+ bits |= MemReclaimFlags::TIME_SLICING;
+ }
+ bits
+ }
+}
+
/// Success argument structure for `FFA_MEM_DONATE`, `FFA_MEM_LEND` and `FFA_MEM_SHARE`.
pub struct SuccessArgsMemOp {
pub handle: Handle,