From 0bf3366911d2a56141d7f0f6110838b822a6dd55 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 17 May 2025 16:28:57 +0800 Subject: [PATCH] attempted cleanup on the sync api to improve the race condition unfortunately this doesn't get us all the way there. There is always a chance that the call that marks a page as dirty is not coupled closely enough to the actual update of the page data. Without actual hardware support for this we can't use this approach. --- services/xous-swapper/src/lib.rs | 56 ++++++----- services/xous-swapper/src/main.rs | 39 +++++++- .../src/platform/cramium/spinor.rs | 94 ------------------- 3 files changed, 70 insertions(+), 119 deletions(-) delete mode 100644 services/xous-swapper/src/platform/cramium/spinor.rs diff --git a/services/xous-swapper/src/lib.rs b/services/xous-swapper/src/lib.rs index f9cf441f9..a04a254ab 100644 --- a/services/xous-swapper/src/lib.rs +++ b/services/xous-swapper/src/lib.rs @@ -42,6 +42,8 @@ impl SwapAbi { pub enum Opcode { /// Userspace request to GC some physical pages GarbageCollect, + /// Userspace request to sync FLASH memory to disk + FlashSync, /// Test messages #[cfg(feature = "swap-userspace-testing")] Test0, @@ -76,9 +78,37 @@ 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"); + } + } } use core::sync::atomic::{AtomicU32, Ordering}; + +use num_traits::ToPrimitive; static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Swapper { fn drop(&mut self) { @@ -92,33 +122,11 @@ 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"); } - -pub fn sync(region: Option<&[T]>) { - #[cfg(any(feature = "precursor", feature = "renode"))] - let region_len = precursor_hal::board::PDDB_LEN; - #[cfg(feature = "cramium-soc")] - let region_len = cramium_hal::board::SPINOR_LEN; - if let Some(region) = region { - let base = region.as_ptr() as usize; - let len_bytes = region.len() * core::mem::size_of::(); - xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::Sync as usize, base, len_bytes, 0, 0, 0, 0)) - .expect("Couldn't mark region as dirty"); - } else { - xous::rsyscall(xous::SysCall::SwapOp( - SwapAbi::Sync as usize, - xous::arch::MMAP_VIRT_BASE, - region_len as usize, - 0, - 0, - 0, - 0, - )) - .expect("Couldn't mark region as dirty"); - } -} diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index b1866179a..9b5bf27cd 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -813,7 +813,7 @@ fn main() { xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); let msg = msg_opt.as_mut().unwrap(); let op: Option = FromPrimitive::from_usize(msg.body.id()); - log::debug!("Swapper got {:x?}", op); + log::info!("Swapper got {:x?}", op); match op { Some(Opcode::GarbageCollect) => { if let Some(scalar) = msg.body.scalar_message_mut() { @@ -834,6 +834,43 @@ fn main() { scalar.arg1 = free_pages; } } + Some(Opcode::FlashSync) => { + if let Some(scalar) = msg.body.scalar_message_mut() { + let specified = scalar.arg1 != 0; + let base = scalar.arg2; + let len_bytes = scalar.arg3; + let sender_pid = msg.sender.pid().unwrap().get(); + if specified { + xous::rsyscall(xous::SysCall::SwapOp( + SwapAbi::Sync as usize, + base, + len_bytes, + sender_pid as usize, + 0, + 0, + 0, + )) + .expect("Couldn't mark region as dirty"); + } else { + #[cfg(any(feature = "precursor", feature = "renode"))] + let region_len = precursor_hal::board::PDDB_LEN; + #[cfg(feature = "cramium-soc")] + let region_len = cramium_hal::board::SPINOR_LEN; + + xous::rsyscall(xous::SysCall::SwapOp( + SwapAbi::Sync as usize, + xous::arch::MMAP_VIRT_BASE, + region_len as usize, + sender_pid as usize, + 0, + 0, + 0, + )) + .expect("Couldn't mark region as dirty"); + } + scalar.arg1 = 0; + } + } #[cfg(feature = "swap-userspace-testing")] Some(Opcode::Test0) => { log::info!("Free mem: {}kiB", get_free_pages() * PAGE_SIZE / 1024); diff --git a/services/xous-swapper/src/platform/cramium/spinor.rs b/services/xous-swapper/src/platform/cramium/spinor.rs deleted file mode 100644 index 816d19bdc..000000000 --- a/services/xous-swapper/src/platform/cramium/spinor.rs +++ /dev/null @@ -1,94 +0,0 @@ -use core::sync::atomic::{AtomicBool, Ordering}; - -use cramium_api::SpimChannel; -use cramium_hal::udma::*; -use xous_ipc::Buffer; - -use crate::api::{BulkErase, Opcode, WriteRegion}; - -static OP_IN_PROGRESS: AtomicBool = AtomicBool::new(false); - -pub fn spinor_handler() -> ! { - let channel = SpimChannel::Channel1; - - // safety: this is safe because the global clocks were gated on by the bootloader - let flash_spim = unsafe { - Spim::new( - channel, - 25_000_000, - 50_000_000, - SpimClkPol::LeadingEdgeRise, - SpimClkPha::CaptureOnLeading, - SpimCs::Cs0, - 0, - 0, - None, - 1024, // this is limited by the page length - 1024, - Some(6), - Some(SpimMode::Quad), - ) - }; - - let xns = xous_api_names::XousNames::new().unwrap(); - let sid = xns.register_name(crate::api::SERVER_NAME_SPINOR, None).unwrap(); - let mut client_id: Option<[u32; 4]> = None; - - let mut msg_opt = None; - loop { - xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); - let msg = msg_opt.as_mut().unwrap(); - let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::Invalid); - log::debug!("{:?}", opcode); - match opcode { - Opcode::AcquireExclusive => { - if let Some(scalar) = msg.body.scalar_message_mut() { - let id0 = scalar.arg1; - let id1 = scalar.arg2; - let id2 = scalar.arg3; - let id3 = scalar.arg4; - if client_id.is_none() { - OP_IN_PROGRESS.store(true, Ordering::Relaxed); // lock out suspends when the exclusive lock is acquired - client_id = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]); - log::trace!("giving {:x?} an exclusive lock", client_id); - scalar.arg1 = 1; - } else { - scalar.arg1 = 0; - } - } - } - Opcode::ReleaseExclusive => { - if let Some(scalar) = msg.body.scalar_message_mut() { - client_id = None; - OP_IN_PROGRESS.store(false, Ordering::Relaxed); - scalar.arg1 = 1; - } - } - Opcode::WriteRegion => { - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut wr = buffer.to_original::().unwrap(); - // TODO: implement this. `spinor` would be created in this implementation somewhere. - // note: this must reject out-of-bound length requests for security reasons - // wr.result = Some(spinor.write_region(&mut wr)); - todo!("implement `spinor`"); - - buffer.replace(wr).expect("couldn't return response code to WriteRegion"); - } - Opcode::BulkErase => { - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut wr = buffer.to_original::().unwrap(); - // TODO: implement this. `spinor` would be created in this implementation somewhere. - // note: this must reject out-of-bound length requests for security reasons - // wr.result = Some(spinor.bulk_erase(&mut wr)); - todo!("implement `spinor`"); - - buffer.replace(wr).expect("couldn't return response code to WriteRegion"); - } - Opcode::Invalid => { - log::error!("Invalid Opcode!"); - } - } - } -}