From 44658adb47169539beb573f5da7186f40466a447 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 May 2024 17:02:15 +0800 Subject: [PATCH 1/4] deal with deprecation notice in rust 1.38 annoying. is this actually necessary? --- .cargo/config | 21 +-------------------- .cargo/config.toml | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 20 deletions(-) mode change 100644 => 120000 .cargo/config create mode 100644 .cargo/config.toml diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 58625288e..000000000 --- a/.cargo/config +++ /dev/null @@ -1,20 +0,0 @@ -[alias] -xtask = "run --package xtask --" - -# [net] -# offline = true # do not access the network - -[build] -rustflags = ["--cfg", "crossbeam_no_atomic_64"] - -# [target.riscv32imac-unknown-xous-elf] -# linker = "/usr/bin/mold" -# rustflags = [ -# "-C", "linker-flavor=ld", -# "-C", "relocation-model=pic", -# "-C", "link-arg=-shared", -# "--cfg", "crossbeam_no_atomic_64" -# ] - -[target.riscv32imac-unknown-xous-elf] -rustflags = ["--cfg", 'curve25519_dalek_backend="u32e_backend"'] \ No newline at end of file diff --git a/.cargo/config b/.cargo/config new file mode 120000 index 000000000..ab8b69cbc --- /dev/null +++ b/.cargo/config @@ -0,0 +1 @@ +config.toml \ No newline at end of file diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..58625288e --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,20 @@ +[alias] +xtask = "run --package xtask --" + +# [net] +# offline = true # do not access the network + +[build] +rustflags = ["--cfg", "crossbeam_no_atomic_64"] + +# [target.riscv32imac-unknown-xous-elf] +# linker = "/usr/bin/mold" +# rustflags = [ +# "-C", "linker-flavor=ld", +# "-C", "relocation-model=pic", +# "-C", "link-arg=-shared", +# "--cfg", "crossbeam_no_atomic_64" +# ] + +[target.riscv32imac-unknown-xous-elf] +rustflags = ["--cfg", 'curve25519_dalek_backend="u32e_backend"'] \ No newline at end of file From 1523fb6b83946d4c00d6cbaf7d283dd4f7353267 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 May 2024 17:07:36 +0800 Subject: [PATCH 2/4] cleanup warning from rust 1.38 --- loader/curve25519-dalek-loader/src/traits.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/loader/curve25519-dalek-loader/src/traits.rs b/loader/curve25519-dalek-loader/src/traits.rs index d127b3ed2..625c4b97b 100644 --- a/loader/curve25519-dalek-loader/src/traits.rs +++ b/loader/curve25519-dalek-loader/src/traits.rs @@ -388,6 +388,7 @@ pub trait VartimePrecomputedMultiscalarMul: Sized { /// This trait is only for debugging/testing, since it should be /// impossible for a `curve25519-dalek` user to construct an invalid /// point. +#[allow(dead_code)] pub(crate) trait ValidityCheck { /// Checks whether the point is on the curve. Not CT. fn is_valid(&self) -> bool; From fd4a8cf7e2277460cbddf4b9ced8e86bf83d9e0f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 May 2024 04:12:46 +0800 Subject: [PATCH 3/4] add a call to get the free memory status from the kernel this will be helpful for debugging our page tracker --- kernel/src/mem.rs | 2 ++ kernel/src/swap.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ kernel/src/syscall.rs | 1 + 3 files changed, 60 insertions(+) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index a640204c6..7f2e3d141 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -178,6 +178,8 @@ impl MemoryManager { owned_bytes } + pub fn ram_size(&self) -> usize { self.ram_size } + #[cfg(all(baremetal, feature = "print-debug"))] pub fn print_ownership(&self) { println!("Ownership ({} bytes in all):", unsafe { MEMORY_ALLOCATIONS.len() }); diff --git a/kernel/src/swap.rs b/kernel/src/swap.rs index 9a053380e..6e8f2a95e 100644 --- a/kernel/src/swap.rs +++ b/kernel/src/swap.rs @@ -10,16 +10,23 @@ use crate::arch::mem::PAGE_SIZE; use crate::mem::MemoryManager; use crate::services::SystemServices; +/// This ABI is copy-paste synchronized with what's in the userspace handler. It's left out of +/// xous-rs so that we can change it without having to push crates to crates.io. +/// Since there is only one place the ABI could be used, we're going to stick with +/// this primitive method of synchronization because it reduces the activation barrier +/// to fix bugs. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum SwapAbi { Invalid = 0, Evict = 1, + GetFreeMem = 2, } impl SwapAbi { pub fn from(val: usize) -> SwapAbi { use SwapAbi::*; match val { 1 => Evict, + 2 => GetFreeMem, _ => Invalid, } } @@ -88,6 +95,20 @@ impl Swap { SWAP.with(|ss| f(&mut ss.borrow_mut())) } + #[cfg(baremetal)] + pub fn with(f: F) -> R + where + F: FnOnce(&Swap) -> R, + { + #[cfg(baremetal)] + unsafe { + f(&*core::ptr::addr_of!(SWAP)) + } + + #[cfg(not(baremetal))] + SWAP.with(|ss| f(&ss.borrow_mut())) + } + pub fn register_handler( &mut self, s0: u32, @@ -342,6 +363,42 @@ impl Swap { } } + pub fn get_free_mem(&self) -> SysCallResult { + println!("RAM usage:"); + let mut total_bytes = 0; + let mut ram_size = 0; + crate::services::SystemServices::with(|system_services| { + crate::mem::MemoryManager::with(|mm| { + for process in &system_services.processes { + if !process.free() { + let bytes_used = mm.ram_used_by(process.pid); + total_bytes += bytes_used; + println!( + " PID {:>3}: {:>4} k {}", + process.pid, + bytes_used / 1024, + system_services.process_name(process.pid).unwrap_or("") + ); + } + } + ram_size = mm.ram_size(); + }); + }); + println!("{} k total", total_bytes / 1024); + + /* + crate::services::SystemServices::with(|system_services| { + let current_pid = system_services.current_pid(); + let process = system_services.get_process(PID::new(9).unwrap()).unwrap(); + println!("PID {} {}:", process.pid, system_services.process_name(process.pid).unwrap_or("")); + process.activate().unwrap(); + crate::arch::mem::MemoryMapping::current().print_map(); + system_services.get_process(current_pid).unwrap().activate().unwrap(); + }); */ + + Ok(xous_kernel::Result::Scalar5(total_bytes, ram_size, 0, 0, 0)) + } + /// The address space on entry to `retrieve_page` is `target_pid`; it must ensure /// that the address space is still `target_pid` on return. /// diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 320480f9a..974591ecf 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1029,6 +1029,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR } match SwapAbi::from(op) { SwapAbi::Evict => Swap::with_mut(|swap| swap.evict_page(a1, a2)), + SwapAbi::GetFreeMem => Swap::with(|swap| swap.get_free_mem()), SwapAbi::Invalid => { println!( "Invalid SwapOp: {:x} {:x} {:x} {:x} {:x} {:x} {:x}", From eb9124b9c75cc496c5bea7dfd7a224fc5830974b Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 May 2024 04:13:11 +0800 Subject: [PATCH 4/4] test the get free memory call --- services/xous-swapper/src/main.rs | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 85ab62c55..713d2983d 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -2,11 +2,37 @@ mod debug; mod platform; use core::fmt::Write; use std::fmt::Debug; +use std::thread; +use std::thread::sleep; +use std::time::Duration; use debug::*; use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; use num_traits::FromPrimitive; use platform::{SwapHal, PAGE_SIZE}; +use xous::Result; + +/// This ABI is copy-paste synchronized with what's in the kernel. It's left out of +/// xous-rs so that we can change it without having to push crates to crates.io. +/// Since there is only one place the ABI could be used, we're going to stick with +/// this primitive method of synchronization because it reduces the activation barrier +/// to fix bugs. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum SwapAbi { + Invalid = 0, + Evict = 1, + GetFreeMem = 2, +} +impl SwapAbi { + pub fn from(val: usize) -> SwapAbi { + use SwapAbi::*; + match val { + 1 => Evict, + 2 => GetFreeMem, + _ => Invalid, + } + } +} pub struct PtPage { pub entries: [u32; 1024], @@ -149,6 +175,21 @@ fn swap_handler( let pid = a2 as u8; let vaddr_in_pid = a3; let vaddr_in_swap = a4; + // next steps on the swap journey: + // - create a "dummy" Evictor routine that just tell the kernel to evict one of our resident + // processes + // - this should trigger WriteToSwap here + // - implement the routine here + // - also implement some routines to track free memory to check that things are doing what we + // expect them to do + // + // - Do something with allocate advisories (e.g. LRU cache with pre-defined capacity) + // - Replace the Evictor routine with a thing that checks free memory level, and then more + // intelligently swaps stuff out to create free space for the kernel + // + // - Somehow come up with some test cases for stress-testing the swapper...probably some function + // that allocates a bunch of heap, and occasionally touches the contents, while other system + // processes trundle on. } Some(Opcode::ReadFromSwap) => { let pid = a2 as u8; @@ -222,6 +263,21 @@ fn main() { log::set_max_level(log::LevelFilter::Info); log::info!("my PID is {}", xous::process::id()); + thread::spawn({ + move || { + loop { + match xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::GetFreeMem as usize, 0, 0, 0, 0, 0, 0)) { + Ok(Result::Scalar5(mem, total, _, _, _)) => { + log::info!("Free mem: {}kiB / {}kiB", mem / 1024, total / 1024) + } + Ok(e) => log::warn!("Unexpected response: {:?}", e), + Err(e) => log::warn!("Error: {:?}", e), + } + sleep(Duration::from_millis(1000)); + } + } + }); + let mut msg_opt = None; loop { xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();