diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index fe8e72e1e..50eced2f2 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -1,14 +1,12 @@ +mod debug; mod platform; +use core::fmt::Write; +use std::fmt::Debug; -use core::fmt::{Error, Write}; -use core::sync::atomic::{AtomicUsize, Ordering}; - +use debug::*; use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; use num_traits::FromPrimitive; -use platform::SwapHal; -use utralib::*; - -static DUART_CSR_PA: AtomicUsize = AtomicUsize::new(0); +use platform::{SwapHal, PAGE_SIZE}; pub struct PtPage { pub entries: [u32; 1024], @@ -36,43 +34,6 @@ pub enum Opcode { EvalTrim = 256, } -pub struct DebugUart {} -impl DebugUart { - #[cfg(feature = "debug-print")] - pub fn putc(&mut self, c: u8) { - let mut csr_ptr = DUART_CSR_PA.load(Ordering::SeqCst); - // map it if it doesn't exist - if csr_ptr == 0 { - let debug_uart_mem = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::app_uart::HW_APP_UART_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't claim the debug UART"); - DUART_CSR_PA.store(csr_ptr, Ordering::SeqCst); - csr_ptr = debug_uart_mem.as_ptr() as usize; - } - let mut csr = CSR::new(csr_ptr as *mut u32); - - // Wait until TXFULL is `0` - while csr.r(utra::app_uart::TXFULL) != 0 {} - csr.wfo(utra::app_uart::RXTX_RXTX, c as u32); - } - - #[cfg(not(feature = "debug-print"))] - pub fn putc(&self, _c: u8) {} -} - -impl Write for DebugUart { - fn write_str(&mut self, s: &str) -> Result<(), Error> { - for c in s.bytes() { - self.putc(c); - } - Ok(()) - } -} - /// This structure contains shared state accessible between the userspace code and the blocking swap call /// handler. struct SwapperSharedState { @@ -187,7 +148,9 @@ fn swap_handler( ) .ok(); // walk the PT to find the swap data - let paddr_in_swap = ss.pt_walk(pid as u8, vaddr_in_pid); + let paddr_in_swap = ss + .pt_walk(pid as u8, vaddr_in_pid) + .expect("Couldn't resolve swapped data. Was the page actually swapped?"); writeln!( DebugUart {}, "resolved address in swap for 0x{:x}:{} -> 0x{:x?}", @@ -196,6 +159,14 @@ fn swap_handler( paddr_in_swap ) .ok(); + // safety: this is only safe because the pointer we're passed from the kernel is guaranteed to be + // a valid u8-page in memory + let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) }; + // TODO: manage swap_count + ss.hal + .decrypt_swap_from(buf, 0, paddr_in_swap, vaddr_in_pid, pid) + .expect("Decryption error: swap image corrupted, the tag does not match the data!"); + // at this point, the `buf` has our desired data, we're done, modulo updating the count. } Some(Opcode::AllocateAdvisory) => { let advisories = [ diff --git a/services/xous-swapper/src/platform/cramium/hw.rs b/services/xous-swapper/src/platform/cramium/hw.rs index c726ebd6f..a909bc795 100644 --- a/services/xous-swapper/src/platform/cramium/hw.rs +++ b/services/xous-swapper/src/platform/cramium/hw.rs @@ -1,32 +1,26 @@ +use core::fmt::Write; 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 loader::swap::{SwapSpec, SPIM_RAM_IFRAM_ADDR, SWAP_HAL_VADDR}; use num_traits::ToBytes; use xous::MemoryRange; +use crate::debug::*; + 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 SwapHal { - ifram_range: MemoryRange, swap_mac_start: usize, cipher: Aes256GcmSiv, ram_spim: Spim, } impl SwapHal { pub fn new(spec: &SwapSpec) -> Self { - let ifram_range = xous::syscall::map_memory( - xous::MemoryAddress::new(SPIM_RAM_IFRAM_ADDR), - None, - PAGE_SIZE, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("Couldn't map IFRAM 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::(); @@ -38,11 +32,11 @@ impl SwapHal { #[cfg(not(feature = "spi-alt-channel"))] let channel = SpimChannel::Channel1; SwapMac { - ifram_range, 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 + // note that also the IFRAM0 range is pre-allocated by the bootloader, and pre-mapped + // into the correct virtual address as well. ram_spim: unsafe { Spim::new_with_ifram( channel, @@ -57,7 +51,7 @@ impl SwapHal { 1024, // this is limited by the page length 1024, Some(6), - IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, ifram_range.as_ptr() as usize, PAGE_SIZE), + IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, SWAP_HAL_VADDR, PAGE_SIZE), ) }, } diff --git a/services/xous-swapper/src/platform/precursor/hw.rs b/services/xous-swapper/src/platform/precursor/hw.rs index d7d1cc593..2448a5eda 100644 --- a/services/xous-swapper/src/platform/precursor/hw.rs +++ b/services/xous-swapper/src/platform/precursor/hw.rs @@ -1,45 +1,35 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag}; -use loader::swap::SwapSpec; -use xous::MemoryRange; +use loader::swap::{SwapSpec, SWAP_HAL_VADDR}; 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 SwapHal { - swap_mem: MemoryRange, dst_data_area: &'static mut [u8], dst_mac_area: &'static mut [u8], cipher: Aes256GcmSiv, } 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 + // safety: the ram swap area is pre-mapped into our virtual address by the loader, 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 + dst_data_area: unsafe { + core::slice::from_raw_parts_mut(SWAP_HAL_VADDR as *mut u8, ram_size_actual) + }, + // safety: the ram swap area is pre-mapped into our virtual address by the loader, 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) + core::slice::from_raw_parts_mut((SWAP_HAL_VADDR as *mut u8).add(ram_size_actual), mac_size) }, cipher: Aes256GcmSiv::new((&spec.key).into()), } @@ -87,8 +77,11 @@ impl SwapHal { dst_vaddr: usize, dst_pid: u8, ) -> Result<(), Error> { - // println!("Decrypt swap:"); - // println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid); + // use core::fmt::Write; + // use crate::debug::*; + // writeln!(DebugUart {}, "Decrypt swap:").ok(); + // writeln!(DebugUart {}, " offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, + // dst_pid).ok(); assert!(src_offset & (PAGE_SIZE - 1) == 0); assert!(buf.len() == PAGE_SIZE); @@ -103,9 +96,12 @@ impl SwapHal { 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]); + // writeln!(DebugUart {}, "dst_mac_area: {:x?}", &self.dst_mac_area[..32]).ok(); 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()) + // writeln!(DebugUart {}, "Nonce: {:x?}, tag: {:x?}", &nonce, &tag).ok(); + let result = + self.cipher.decrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf, (&tag).into()); + // writeln!(DebugUart {}, "result: {:?}, buf: {:x?}", result, &buf[..16]).ok(); + result } }