diff --git a/services/xous-swapper/Cargo.toml b/services/xous-swapper/Cargo.toml index b1d2cba5c..a81352db2 100644 --- a/services/xous-swapper/Cargo.toml +++ b/services/xous-swapper/Cargo.toml @@ -18,13 +18,18 @@ num-derive = { version = "0.3.3", default-features = false } num-traits = { version = "0.2.14", default-features = false } log = "0.4.14" loader = { path = "../../loader", features = ["swap"] } +aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ + "aes", +] } +cramium-hal = { path = "../../libs/cramium-hal", optional = true, default_features = false } utralib = { version = "0.1.24", optional = true, default-features = false } [target.'cfg(any(windows,unix))'.dependencies] [features] -cramium-soc = ["utralib/cramium-soc"] +cramium-soc = ["utralib/cramium-soc", "cramium-hal"] +spi-alt-channel = [] # needs to match loader setting! cramium-fpga = ["utralib/cramium-fpga"] precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 52e7f3589..fe8e72e1e 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -5,7 +5,7 @@ use core::sync::atomic::{AtomicUsize, Ordering}; use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; use num_traits::FromPrimitive; -use platform::SwapMac; +use platform::SwapHal; use utralib::*; static DUART_CSR_PA: AtomicUsize = AtomicUsize::new(0); @@ -76,11 +76,31 @@ impl Write for DebugUart { /// This structure contains shared state accessible between the userspace code and the blocking swap call /// handler. struct SwapperSharedState { - pub key: [u8; 32], pub pts: SwapPageTables, - pub macs: SwapMac, + pub hal: SwapHal, pub rpt: RuntimePageTracker, } +impl SwapperSharedState { + pub fn pt_walk(&self, pid: u8, va: usize) -> Option { + let l1_pt = &self.pts.roots[pid as usize - 1]; + let l1_entry = (l1_pt.entries[(va & 0xFFC0_0000) >> 22] >> 10) << 12; + + if l1_entry != 0 { + // this is safe because all possible values can be represented as `u32`, the pointer + // is valid, aligned, and the bounds are known. + let l0_pt = unsafe { core::slice::from_raw_parts(l1_entry as *const u32, 1024) }; + let l0_entry = l0_pt[(va & 0x003F_F000) >> 12]; + if l0_entry & 1 != 0 { + // bit 1 is the "valid" bit + Some(((l0_entry as usize >> 10) << 12) | va & 0xFFF) + } else { + None + } + } else { + None + } + } +} struct SharedStateStorage { pub inner: Option, } @@ -113,16 +133,12 @@ fn swap_handler( // have to do mapping requests because it's already done for us! let swap_spec = unsafe { &*(SWAP_CFG_VADDR as *mut SwapSpec) }; - // extract our key - let mut key = [0u8; 32]; - key.copy_from_slice(&swap_spec.key); // swapper is not allowed to use `log` for debugging under most circumstances, because // the swapper can't send messages when handling a swap call. Instead, we use a local // debug UART to handle this. This needs to be enabled with the "debug-print" feature // and is mutually exclusive with the "gdb-stub" feature in the kernel since it uses // the same physical hardware. sss.inner = Some(SwapperSharedState { - key, // safety: this is only safe because: // - the loader puts the swap root page table pages starting at SWAP_PT_VADDR // - all the page table entries are fully initialized and contains only representable data @@ -135,7 +151,7 @@ fn swap_handler( ) }, }, - macs: SwapMac::new(swap_spec.mac_base as usize, swap_spec.mac_len as usize), + hal: SwapHal::new(swap_spec), // safety: this is only safe because the loader guarantees this raw pointer is initialized and // aligned correctly rpt: RuntimePageTracker { @@ -164,14 +180,22 @@ fn swap_handler( let vaddr_in_swap = a4; writeln!( DebugUart {}, - "rfs PID{}, vaddr_pid {:x}, vaddr_swap {:x}", + "rfs ptw PID{}, vaddr_pid {:x}, vaddr_swap {:x}", pid, vaddr_in_pid, vaddr_in_swap ) .ok(); // walk the PT to find the swap data - // ss.pts[pid as usize] + let paddr_in_swap = ss.pt_walk(pid as u8, vaddr_in_pid); + writeln!( + DebugUart {}, + "resolved address in swap for 0x{:x}:{} -> 0x{:x?}", + vaddr_in_pid, + pid, + paddr_in_swap + ) + .ok(); } Some(Opcode::AllocateAdvisory) => { let advisories = [ @@ -213,8 +237,7 @@ fn main() { // due to the advisory allocs that start piling up very quickly if let Some(ss) = sss.inner { log::info!( - "Swap params: key: {:x?}, root: {:x}, rpt: {:?}", - ss.key, + "Swap params: key: root: {:x}, rpt: {:?}", ss.pts.roots[0].entries[0], &ss.rpt.allocs[..8] ); diff --git a/services/xous-swapper/src/platform/cramium/hw.rs b/services/xous-swapper/src/platform/cramium/hw.rs index b903030c9..45767d947 100644 --- a/services/xous-swapper/src/platform/cramium/hw.rs +++ b/services/xous-swapper/src/platform/cramium/hw.rs @@ -1,14 +1,110 @@ +use core::mem::size_of; + +use aes_gcm_siv::{aead::Aead, AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag}; +use cramium_hal::udma::*; +use loader::swap::{SwapSpec, SPIM_RAM_IFRAM_ADDR}; +use num_traits::ToBytes; + +pub const PAGE_SIZE: usize = 4096; + /// 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 SwapMac { - pub base: usize, - pub bounds: usize, +pub struct SwapHal { + swap_mac_start: usize, + cipher: Aes256GcmSiv, + ram_spim: Spim, } -impl SwapMac { - pub fn new(base: usize, bounds: usize) -> Self { SwapMac { base, bounds } } +impl SwapHal { + pub fn new(spec: &SwapSpec) -> Self { + // compute the MAC area needed for the total RAM size. This is a slight over-estimate + // because once we remove the MAC area, we need even less storage, but it's a small error. + let mac_size = (spec.swap_len as usize / 4096) * size_of::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1); + let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; - pub fn lookup_mac(&self, swap_page_offset: usize) -> [u8; 16] { todo!() } + #[cfg(feature = "spi-alt-channel")] + let channel = SpimChannel::Channel0; + #[cfg(not(feature = "spi-alt-channel"))] + let channel = SpimChannel::Channel1; + SwapMac { + swap_mac_start: ram_size_actual, + cipher: Aes256GcmSiv::new((spec.key).into()), + // safety: this is safe because the global clocks were gated on by the bootloader + // note that also the IFRAM0 range is pre-allocated by the bootloader + ram_spim: unsafe { + Spim::new_with_ifram( + channel, + 50_000_000, + 50_000_000, + SpimClkPol::LeadingEdgeRise, + SpimClkPha::CaptureOnLeading, + SpimCs::Cs1, + 0, + 0, + None, + 1024, // this is limited by the page length + 1024, + Some(6), + IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR, 4096), + ) + }, + } + } - pub fn store_mac(&mut self, swap_page_offset: usize, mac: &[u8; 16]) { todo!() } + /// `buf` contents are replaced with encrypted data + pub fn encrypt_swap_to( + &mut self, + buf: &mut [u8], + swap_count: u32, + dest_offset: usize, + src_vaddr: usize, + src_pid: u8, + ) { + assert!(buf.len() == PAGE_SIZE); + assert!(dest_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&swap_count.to_be_bytes()); // this is the `swap_count` field + nonce[5] = src_pid; + let ppage_masked = dest_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = src_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + match self.cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + Ok(tag) => { + self.ram_spim.mem_ram_write(dest_offset as u32, buf); + self.ram_spim.mem_ram_write( + (self.swap_mac_start + (dest_offset / PAGE_SIZE) * size_of::()) as u32, + tag.as_slice(), + ); + } + Err(e) => panic!("Encryption error to swap ram: {:?}", e), + } + } + + pub fn decrypt_swap_from( + &mut self, + buf: &mut [u8], + swap_count: u32, + src_offset: usize, + dst_vaddr: usize, + dst_pid: u8, + ) -> Result<(), Error> { + assert!(src_offset & (PAGE_SIZE - 1) == 0); + assert!(buf.len() == PAGE_SIZE); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&swap_count.to_be_bytes()); // this is the `swap_count` field + nonce[5] = dst_pid; + let ppage_masked = src_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + let mut tag = [0u8; size_of::()]; + self.ram_spim + .mem_read((self.swap_mac_start + (src_offset / PAGE_SIZE) * size_of::()) as u32, &mut tag); + self.ram_spim.mem_read(src_offset as u32, buf); + self.cipher.decrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf, (&tag).into()) + } } diff --git a/services/xous-swapper/src/platform/precursor/hw.rs b/services/xous-swapper/src/platform/precursor/hw.rs index e614c1247..d7d1cc593 100644 --- a/services/xous-swapper/src/platform/precursor/hw.rs +++ b/services/xous-swapper/src/platform/precursor/hw.rs @@ -1,18 +1,111 @@ +use core::mem::size_of; + +use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag}; +use loader::swap::SwapSpec; +use xous::MemoryRange; + +pub const PAGE_SIZE: usize = 4096; + /// This defines a set of functions to get and receive MACs (message /// authentication codes, also referred to as the tag in AES-GCM-SIV. -pub struct SwapMac { - pub macs: &'static mut [[u8; 16]], +pub struct SwapHal { + swap_mem: MemoryRange, + dst_data_area: &'static mut [u8], + dst_mac_area: &'static mut [u8], + cipher: Aes256GcmSiv, } -impl SwapMac { - pub fn new(base: usize, bounds: usize) -> Self { - SwapMac { - // 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(base as *mut [u8; 16], bounds as usize / 16) }, +impl SwapHal { + pub fn new(spec: &SwapSpec) -> Self { + let swap_mem = xous::syscall::map_memory( + xous::MemoryAddress::new(spec.swap_base as usize), + None, + spec.swap_len as usize, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("Couldn't map swap area into swapper"); + // compute the MAC area needed for the total RAM size. This is a slight over-estimate + // because once we remove the MAC area, we need even less storage, but it's a small error. + let mac_size = (spec.swap_len as usize / 4096) * size_of::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1); + let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + + // safety: this is safe because all values within the memory region can be represented in `u8` + let swap_slice = swap_mem.as_mut_ptr(); + Self { + swap_mem, + // safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our + // calculations on lengths ensure area alignment + dst_data_area: unsafe { core::slice::from_raw_parts_mut(swap_slice as *mut u8, ram_size_actual) }, + // safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our + // calculations on lengths ensure area alignment + dst_mac_area: unsafe { + core::slice::from_raw_parts_mut((swap_slice as *mut u8).add(ram_size_actual), mac_size) + }, + cipher: Aes256GcmSiv::new((&spec.key).into()), } } - pub fn lookup_mac(&self, swap_page_offset: usize) -> [u8; 16] { todo!() } + /// The data to be encrypted is provided in `buf`, and is replaced with part of the encrypted data upon + /// completion of the routine. + pub fn encrypt_swap_to( + &mut self, + buf: &mut [u8], + swap_count: u32, + dest_offset: usize, + src_vaddr: usize, + src_pid: u8, + ) { + // println!("enc_to: pid: {}, src_vaddr: {:x} dest_offset: {:x}", src_pid, src_vaddr, dest_offset); + assert!(buf.len() == PAGE_SIZE); + assert!(dest_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&swap_count.to_be_bytes()); // this is the `swap_count` field + nonce[5] = src_pid; + let ppage_masked = dest_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = src_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + match self.cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + Ok(tag) => { + self.dst_data_area[dest_offset..dest_offset + PAGE_SIZE].copy_from_slice(buf); + let mac_offset = (dest_offset / PAGE_SIZE) * size_of::(); + self.dst_mac_area[mac_offset..mac_offset + size_of::()].copy_from_slice(tag.as_slice()); + // println!("Nonce: {:x?}, tag: {:x?}", &nonce, tag.as_slice()); + // println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]); + } + Err(e) => panic!("Encryption error to swap ram: {:?}", e), + } + } - pub fn store_mac(&mut self, swap_page_offset: usize, mac: &[u8; 16]) { todo!() } + /// Used to examine contents of swap RAM. Decrypted data is returned as a slice. + pub fn decrypt_swap_from( + &mut self, + buf: &mut [u8], + swap_count: u32, + src_offset: usize, + dst_vaddr: usize, + dst_pid: u8, + ) -> Result<(), Error> { + // println!("Decrypt swap:"); + // println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid); + assert!(src_offset & (PAGE_SIZE - 1) == 0); + assert!(buf.len() == PAGE_SIZE); + + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&swap_count.to_be_bytes()); // this is the `swap_count` field + nonce[5] = dst_pid; + let ppage_masked = src_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + let mut tag = [0u8; size_of::()]; + let mac_offset = (src_offset / PAGE_SIZE) * size_of::(); + tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::()]); + // println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]); + buf.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]); + // println!("Nonce: {:x?}, tag: {:x?}", &nonce, &tag); + self.cipher.decrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf, (&tag).into()) + } }