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.
This commit is contained in:
bunnie 2024-03-07 22:21:36 +08:00
parent 496d13b157
commit 5c3edef519
2 changed files with 78 additions and 1 deletions

View file

@ -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"]

View file

@ -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<Opcode> = FromPrimitive::from_usize(msg.body.id());
write!(duart, "Swapper got {:?}", msg);
match op {