From b62a639031a934c1f9d910faa06385affd43cf71 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 Mar 2024 19:03:45 +0800 Subject: [PATCH] move AllocAdvice record to externally visible xous-rs definitions this structure moves between swapper and userland. --- xous-rs/src/definitions.rs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 60be34778..4e6705ae7 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -587,3 +587,45 @@ macro_rules! msg_blocking_scalar_unpack { } }}; } + +#[cfg(feature = "swap")] +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum AllocAdvice { + /// the PID of the allocation, virtual address in PID space, physical address + Allocate(PID, usize, usize), + /// the PID of the page freed, virtuall address in PID space, physical address + Free(PID, usize, usize), + /// not yet initialized record + Uninit, +} +#[cfg(feature = "swap")] +impl AllocAdvice { + pub fn serialize(&self) -> (usize, usize) { + match self { + AllocAdvice::Allocate(pid, vaddr, paddr) => { + ( + (pid.get() as usize) << 24 | (vaddr >> 12), + (1 << 24) | (paddr >> 12), // 1 indicates an alloc + ) + } + AllocAdvice::Free(pid, vaddr, paddr) => { + ( + (pid.get() as usize) << 24 | (vaddr >> 12), + (0 << 24) | (paddr >> 12), // 0 indicates a free + ) + } + AllocAdvice::Uninit => (0, 0), + } + } + + pub fn deserialize(a0: usize, a1: usize) -> Self { + if a0 == 0 && a1 == 0 { + AllocAdvice::Uninit + } else if (a1 & (1 << 24)) == 0 { + // don't have to mask a0 or a1 high bits because << 12 shifts the high flag byte out + AllocAdvice::Free(NonZeroU8::new((a0 >> 24) as u8).unwrap(), a0 << 12, a1 << 12) + } else { + AllocAdvice::Allocate(NonZeroU8::new((a0 >> 24) as u8).unwrap(), a0 << 12, a1 << 12) + } + } +}