diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 9320781ad..d137ba6e0 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -9,7 +9,7 @@ use std::{fmt::Debug, num::NonZeroU8}; use debug::*; use heapless::LinearMap; -use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; +use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_OFFSET_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; use lru::LruCache; use num_traits::*; use platform::{SwapHal, PAGE_SIZE}; @@ -58,6 +58,10 @@ pub struct RuntimePageTracker { pub allocs: &'static mut [Option], } +pub struct OffsetTracker { + pub offsets: &'static mut [u32], +} + #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] #[repr(usize)] pub enum Opcode { @@ -79,6 +83,7 @@ pub struct SwapperSharedState { pub pts: SwapPageTables, pub hal: SwapHal, pub rpt: RuntimePageTracker, + pub ot: OffsetTracker, pub sram_start: usize, pub sram_size: usize, pub swap_size: usize, @@ -196,6 +201,18 @@ fn swap_handler( ) }, }, + // safety: this is safe because the loader has allocated this region and zeroed the contents, + // and the length is correctly set up by the loader. Note that the length is slightly + // longer than it needs to be -- the region that has to be tracked does not include the + // area of swap dedicated to the MAC table, which swap_len includes. + ot: OffsetTracker { + offsets: unsafe { + core::slice::from_raw_parts_mut( + SWAP_OFFSET_VADDR as *mut u32, + loader::swap::derive_usable_swap(swap_spec.swap_len as usize) / PAGE_SIZE, + ) + }, + }, sram_start: swap_spec.sram_start as usize, sram_size: swap_spec.sram_size as usize, swap_size: swap_spec.swap_len as usize, @@ -359,14 +376,10 @@ fn main() { // The cache is keyed by physical page number; the values are an Option<(PID, vaddr)> tuple that // corresponds to the current owner of that page. let total_ram = sss.inner.as_ref().unwrap().sram_size; - let total_swap = sss.inner.as_ref().unwrap().swap_size; let mut lru = LruCache::new(NonZeroUsize::new(total_ram / PAGE_SIZE).unwrap()); // Offset tracker // Track an offset count for every page that gets written to swap. - // TODO: find a way to bind this into the swapper's exclusive state in the interrupt context...maybe - // even just have the loader allocate this block directly? - let mut offset = vec![0u32; total_swap / PAGE_SIZE]; /* for (index, pa) in (start..(start + total)).step_by(PAGE_SIZE).enumerate() { if let Some(pid) = sss.inner.as_ref().unwrap().rpt.allocs[index] { diff --git a/services/xous-swapper/src/platform/cramium/hw.rs b/services/xous-swapper/src/platform/cramium/hw.rs index b94e622ec..e823334f4 100644 --- a/services/xous-swapper/src/platform/cramium/hw.rs +++ b/services/xous-swapper/src/platform/cramium/hw.rs @@ -24,9 +24,7 @@ impl SwapHal { // 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; + let ram_size_actual = loader::swap::derive_usable_swap(spec.swap_len as usize); #[cfg(feature = "spi-alt-channel")] let channel = SpimChannel::Channel0; diff --git a/services/xous-swapper/src/platform/precursor/hw.rs b/services/xous-swapper/src/platform/precursor/hw.rs index 2448a5eda..50ee6b815 100644 --- a/services/xous-swapper/src/platform/precursor/hw.rs +++ b/services/xous-swapper/src/platform/precursor/hw.rs @@ -14,12 +14,7 @@ pub struct SwapHal { } 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; - + let ram_size_actual = loader::swap::derive_usable_swap(spec.swap_len as usize); Self { // safety: the ram swap area is pre-mapped into our virtual address by the loader, and our // calculations on lengths ensure area alignment @@ -29,7 +24,10 @@ impl SwapHal { // 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_HAL_VADDR 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), + loader::swap::derive_mac_size(spec.swap_len as usize), + ) }, cipher: Aes256GcmSiv::new((&spec.key).into()), }