From 95c27c765e2db44f794bd6b79c90d9612d261197 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 9 May 2024 02:08:51 +0800 Subject: [PATCH] refactor loader to use a single function to swap MAC offset in swap also pre-map some pages to handle offset increment in swapper --- loader/src/lib.rs | 2 ++ loader/src/main.rs | 3 +-- loader/src/phase2.rs | 16 ++++++++++++++++ loader/src/platform/cramium/swap.rs | 5 ++--- loader/src/platform/precursor/swap.rs | 5 ++--- loader/src/swap.rs | 22 ++++++++++++++++++++++ 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/loader/src/lib.rs b/loader/src/lib.rs index 532fc2bd0..98172fda7 100644 --- a/loader/src/lib.rs +++ b/loader/src/lib.rs @@ -3,6 +3,8 @@ #[cfg(feature = "swap")] pub mod swap; +pub const PAGE_SIZE: usize = 4096; + // Locate the hard-wired IFRAM allocations for UDMA #[allow(dead_code)] #[cfg(feature = "cramium-soc")] diff --git a/loader/src/main.rs b/loader/src/main.rs index aee4d672a..948b0a346 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -30,14 +30,13 @@ use core::{mem, ptr, slice}; use asm::*; use bootconfig::BootConfig; use consts::*; +pub use loader::PAGE_SIZE; use minielf::*; use phase1::{phase_1, InitialProcess}; use phase2::{phase_2, ProgramDescription}; #[cfg(feature = "swap")] use platform::SwapHal; - pub type XousPid = u8; -pub const PAGE_SIZE: usize = 4096; const WORD_SIZE: usize = mem::size_of::(); pub const SIGBLOCK_SIZE: usize = 0x1000; const STACK_PAGE_COUNT: usize = 8; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 7ad5bbc09..5c0403d84 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -291,6 +291,22 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { } } + // allocate swap offset tracker + { + // note that this computation is mirrored in xous-swapper + let swap_size_usable = crate::swap::derive_usable_swap(swap_spec.swap_len as usize); + for offset in (0..(swap_size_usable / PAGE_SIZE) * core::mem::size_of::()).step_by(4096) { + let offset_page = cfg.alloc() as usize; // this also zeroes the page + cfg.map_page( + root, + offset_page, + SWAP_OFFSET_VADDR + offset, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + } + } + // map any hardware-specific pages into the userspace swapper crate::platform::userspace_maps(cfg); } diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index b72516eb2..7cd0129e7 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -270,9 +270,8 @@ impl SwapHal { flash_spim.mem_read(SWAP_IMG_START as u32, &mut buf.data, false); // compute offsets for swap - let mac_size = (swap.ram_size as usize / 4096) * size_of::(); - let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1); - let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + let ram_size_actual = crate::swap::derive_usable_swap(swap.ram_size as usize); + let mac_size = crate::swap::derive_mac_size(swap.ram_size as usize); if SDBG { println!( "mac area size: {:x}, ram_size_actual: {:x}, swap.ram_size: {:x}, mac offset: {:x}", diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index e52759970..b150d3fcc 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -34,9 +34,8 @@ 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 = (swap.ram_size as usize / 4096) * size_of::(); - let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1); - let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + let ram_size_actual = crate::swap::derive_usable_swap(swap.ram_size as usize); + let mac_size = crate::swap::derive_mac_size(swap.ram_size as usize); if SDBG { println!( "mac area size: {:x}, ram_size_actual: {:x}, swap.ram_size: {:x}, mac offset: {:x}", diff --git a/loader/src/swap.rs b/loader/src/swap.rs index 01ec4dee9..824450f64 100644 --- a/loader/src/swap.rs +++ b/loader/src/swap.rs @@ -1,3 +1,9 @@ +use core::mem::size_of; + +use aes_gcm_siv::Tag; + +use crate::PAGE_SIZE; + /// Virtual address fields: /// 31 22 21 12 11 0 /// | L1 index | L2 index | LSB of addr | @@ -42,6 +48,7 @@ pub const SWAP_PT_VADDR: usize = 0xE000_0000; // E000_0000 - E100_0000 => 16 MiB of vaddr space for page tables; should be more than enough pub const SWAP_CFG_VADDR: usize = 0xE100_0000; pub const SWAP_RPT_VADDR: usize = 0xE100_1000; +pub const SWAP_OFFSET_VADDR: usize = 0xE110_0000; pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000; #[cfg(feature = "cramium-soc")] pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000; @@ -82,3 +89,18 @@ pub const SPIM_RAM_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRA #[allow(dead_code)] #[cfg(feature = "cramium-soc")] pub const SPIM_FLASH_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 5 * 4096; + +/// Function that derives the usable amount of swap space from the total length of swap memory available. +/// This is used repeatedly in the initialization process to define the boundary between the swap page +/// storage and the message authentication code (MAC) tables. +/// +/// This is a slight over-estimate because once we remove the MAC area, we need even less storage, +/// but it's a small error. +pub fn derive_usable_swap(swap_len: usize) -> usize { + let mac_size = (swap_len as usize / 4096) * size_of::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1); + let swap_size_usable = (swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + swap_size_usable +} + +pub fn derive_mac_size(swap_len: usize) -> usize { (swap_len / 4096) * size_of::() }