From 24a70b0eb5acdbe0ebe6afb3281d99378a3c522d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 18 May 2025 04:29:06 +0800 Subject: [PATCH] implement writepage mechanism, deprecate sync/dirty writepage uses a *direct* memory message (no rkyv) for efficiency this saves us an extra page spent on the sideband data shuffling by loading those args directly into the cpu registers that get shuttled along with a memory message (the now increasingly misnamed "advisory" fields of length/valid). --- services/xous-swapper/src/lib.rs | 77 ++++++++++++++++---------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/services/xous-swapper/src/lib.rs b/services/xous-swapper/src/lib.rs index a04a254ab..cae786708 100644 --- a/services/xous-swapper/src/lib.rs +++ b/services/xous-swapper/src/lib.rs @@ -15,8 +15,7 @@ pub enum SwapAbi { // HardOom = 4, // meant to be initiated within the kernel to itself StealPage = 5, ReleaseMemory = 6, - MarkDirty = 7, - Sync = 8, + WritePage = 7, } /// SYNC WITH `kernel/src/swap.rs` impl SwapAbi { @@ -29,8 +28,7 @@ impl SwapAbi { // 4 => HardOom, 5 => StealPage, 6 => ReleaseMemory, - 7 => MarkDirty, - 8 => Sync, + 7 => WritePage, _ => Invalid, } } @@ -42,14 +40,28 @@ impl SwapAbi { pub enum Opcode { /// Userspace request to GC some physical pages GarbageCollect, - /// Userspace request to sync FLASH memory to disk - FlashSync, + /// This call will only process full-page writes to an offset in SPI FLASH. + /// The full-page criteria is acceptable because (a) we need the full page anyways + /// due to the sector erase constraint and (b) the caller has to have the full page + /// anyways because the page was read in and mapped into the caller's space at some + /// point in time. + WritePage, /// Test messages #[cfg(feature = "swap-userspace-testing")] Test0, None, } +/// An aligned structure for sending FLASH data between structures. This only works +/// if SPINOR_ERASE_SIZE == 4096. +#[repr(C, align(4096))] +pub struct FlashPage { + pub data: [u8; N], +} +impl FlashPage { + pub fn new() -> Self { Self { data: [0u8; SPINOR_ERASE_SIZE as usize] } } +} + pub const SWAPPER_PUBLIC_NAME: &'static str = "_swapper server_"; pub struct Swapper { @@ -79,36 +91,34 @@ impl Swapper { // no result is given, but the call blocks until the GC call has completed in the swapper. } - /// Sync needs to be called from within the context of the Swapper to allow for proper locking - /// of the system while the sync happens. - pub fn sync(&self, region: Option<&[T]>) { - if let Some(region) = region { - let base = region.as_ptr() as usize; - let len_bytes = region.len() * core::mem::size_of::(); - xous::send_message( - self.conn, - xous::Message::new_blocking_scalar( - Opcode::FlashSync.to_usize().unwrap(), - 1, - base, - len_bytes, - 0, - ), - ) - .expect("Couldn't call sync on swapper"); - } else { - xous::send_message( - self.conn, - xous::Message::new_blocking_scalar(Opcode::FlashSync.to_usize().unwrap(), 0, 0, 0, 0), - ) - .expect("Couldn't call sync on swapper"); + /// `offset` is transmitted with the address bank mask for MMAP_VIRT attached to avoid + /// the first sector falling foul of the NonZero requirement. + pub fn write_page(&self, offset: usize, page: FlashPage) -> Result { + if (offset & (cramium_hal::board::SPINOR_ERASE_SIZE as usize - 1)) != 0 { + return Err(xous::Error::BadAddress); } + let msg = MemoryMessage { + id: Opcode::WritePage.to_usize().unwrap(), + // safety: page.data is guaranteed to be aligned due to the repr(C) alignment directive + // also, all values are representable as a u8. + buf: unsafe { + MemoryRange::new(page.data.as_ptr() as usize, SPINOR_ERASE_SIZE as usize).unwrap() + }, + offset: NonZero::new(offset), + valid: None, // unused, the whole page is valid by definition + }; + // we do a Borrow instead of a Move not because we care about the return value, + // but because we care that the call finished before proceeding. + xous::send_message(self.conn, xous::Message::Borrow(msg)) } } use core::sync::atomic::{AtomicU32, Ordering}; +use std::num::NonZero; +use cramium_hal::board::SPINOR_ERASE_SIZE; use num_traits::ToPrimitive; +use xous::{MemoryMessage, MemoryRange}; static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Swapper { fn drop(&mut self) { @@ -121,12 +131,3 @@ impl Drop for Swapper { } } } - -/// This is safe to call from any context because the kernel call completes without activating -/// the swapper process - the pages are immediately marked dirty within the context of the calling process. -pub fn mark_dirty(region: &[T]) { - let base = region.as_ptr() as usize; - let len_bytes = region.len() * core::mem::size_of::(); - xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::MarkDirty as usize, base, len_bytes, 0, 0, 0, 0)) - .expect("Couldn't mark region as dirty"); -}