From 5c3edef5194787887308b6831462bbf62cdf4c59 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 7 Mar 2024 22:21:36 +0800 Subject: [PATCH] define initial data structures & registration note that the SMT is a trait that has an implementation dependent structure, because we can't always assume that swap is memory-mapped. --- services/xous-swapper/Cargo.toml | 1 + services/xous-swapper/src/main.rs | 78 ++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/services/xous-swapper/Cargo.toml b/services/xous-swapper/Cargo.toml index f394aee80..860b3737b 100644 --- a/services/xous-swapper/Cargo.toml +++ b/services/xous-swapper/Cargo.toml @@ -28,6 +28,7 @@ cramium-fpga = ["utralib/cramium-fpga"] precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] renode = ["utralib/renode"] + # for now, debug-print only works on Precursor targets debug-print = ["precursor", "utralib/precursor"] default = ["debug-print"] diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 3a1bd4570..68422871b 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -2,6 +2,51 @@ use core::fmt::{Error, Write}; use utralib::*; +/// This trait defines a set of functions to get and receive MACs (message +/// authentication codes, also referred to as the tag in AES-GCM-SIV. +/// +/// The implementation of the MAC table depends on the details of the hardware; +/// we cannot always guarantee that the MACs are memory mapped, as they could +/// be stored in an off-chip SPI RAM that is accessible only through a register +/// interface. +pub trait SmtAccessor { + /// Lookup the MAC corresponding to a given page in swap. Offsets are + /// relative to the base of the swap region, and are given in units of pages, not bytes. + fn lookup_mac(swap_page_offset: usize) -> [u8; 16]; + /// Store a MAC for a given page in swap. + fn store_mac(swap_page_offset: usize, mac: &[u8; 16]); +} + +/// An array of pointers to the SATPs (root page table) of all the processes. +pub struct SwapPageTables { + satps: &'static mut [usize], +} +/// This is an implementation for SMTs that are memory mapped. Directly mapped +/// tables are just as lice of MACs +pub struct SwapMacTableMemMap { + macs: &'static mut [[u8; 16]], +} +impl SmtAccessor for SwapMacTableMemMap { + fn lookup_mac(swap_page_offset: usize) -> [u8; 16] { todo!() } + + fn store_mac(swap_page_offset: usize, mac: &[u8; 16]) { todo!() } +} +/// This is an implementation for SMTs that are accessible only through a SPI +/// register interface. The base and bounds must be translated to SPI accesses +/// in a hardware-specific manner. +pub struct SwapMacTableSpi { + base: usize, + bounds: usize, +} +impl SmtAccessor for SwapMacTableSpi { + fn lookup_mac(swap_page_offset: usize) -> [u8; 16] { todo!() } + + fn store_mac(swap_page_offset: usize, mac: &[u8; 16]) { todo!() } +} +pub struct RuntimePageTracker { + allocs: &'static mut [u8], +} + #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] #[repr(usize)] pub enum Opcode { @@ -74,8 +119,39 @@ fn main() { write!(duart, "Swapper started.\n\r"); let sid = xous::create_server().unwrap(); + + // Register the swapper with the kernel. Written as a raw syscall, since this is + // the only instance of its use (no point in use-once code to wrap it). + let (s0, s1, s2, s3) = sid.to_u32(); + let (spt_init, smt_base_init, smt_bounds_init, rpt_init) = + xous::rsyscall(xous::SysCall::RegisterSwapper(s0, s1, s2, s3)) + .and_then(|result| { + if let xous::Result::Scalar5(spt, smt_base, smt_bounds, rpt, _) = result { + Ok((spt, smt, rpt)) + } else { + panic!("Failed to register swapper"); + } + }) + .unwrap(); + // safety: this is only safe because the loader guarantees this raw pointer is initialized and aligned + // correctly + let spt = unsafe { &mut *(spt_init as *mut SwapPageTables) }; + #[cfg(feature = "precursor")] + let smt = SwapMacTableMemMap { + // safety: this is only safe because the loader guarantees memory-mapped SMT is initialized and + // aligned and properly mapped into the swapper's memory space. + macs: unsafe { core::slice::from_raw_parts_mut(smt_base_init as *mut [u8; 16], smt_bounds_init) }, + }; + #[cfg(feature = "cramium-soc")] + let smt = SwapMacTableSpi { base: smt_base_init, bounds: smt_bounds_init }; + // safety: this is only safe because the loader guarantees this raw pointer is initialized and aligned + // correctly + let rpt = unsafe { &mut *(rpt_init as *mut RuntimePageTracker) }; + + let mut msg_opt = None; loop { - let msg = xous::receive_message(shch_sid).unwrap(); + xous::reply_and_receive_next(shch_sid, &mut msg_opt).unwrap(); + let msg = msg_opt.as_mut().unwrap(); let op: Option = FromPrimitive::from_usize(msg.body.id()); write!(duart, "Swapper got {:?}", msg); match op {