From 28b415cb274d38e15dbf640de92da60499f17962 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 Mar 2024 18:30:18 +0800 Subject: [PATCH] handle swapped pages and allocation advisories --- kernel/src/arch/riscv/mem.rs | 57 ++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/kernel/src/arch/riscv/mem.rs b/kernel/src/arch/riscv/mem.rs index 24ecabba9..7a7378850 100644 --- a/kernel/src/arch/riscv/mem.rs +++ b/kernel/src/arch/riscv/mem.rs @@ -10,6 +10,7 @@ use xous_kernel::{MemoryFlags, PID}; use crate::arch::process::InitialProcess; use crate::mem::MemoryManager; +use crate::swap::Swap; // pub const DEFAULT_STACK_TOP: usize = 0x8000_0000; pub const DEFAULT_HEAP_BASE: usize = 0x2000_0000; @@ -32,7 +33,7 @@ pub const FLG_A: usize = 0x40; pub const FLG_D: usize = 0x80; extern "C" { - fn flush_mmu(); + pub fn flush_mmu(); } unsafe fn zeropage(s: *mut u32) { @@ -888,9 +889,14 @@ pub fn ensure_page_exists_inner(address: usize) -> Result Result> 22) & ((1 << 12) - 1); let ppn0 = (new_page >> 12) & ((1 << 10) - 1); unsafe { - // Map the page to our process - *entry = (ppn1 << 20) | (ppn0 << 10) | (flags | FLG_VALID /* valid */ | FLG_D /* D */ | FLG_A/* A */); - flush_mmu(); + #[cfg(feature = "swap")] + if flags & MMUFlags::P.bits() == 1 { + // page is swapped; fill page, map and return + Swap::with_mut(|s| s.retrieve_page(crate::arch::process::current_pid(), virt, new_page)) + + // the execution flow diverges from here: it returns via the interrupt context handler. -> ! + } else { + // page is reserved: simple zero it out + // Map the page to our process + *entry = + (ppn1 << 20) | (ppn0 << 10) | (flags | FLG_VALID /* valid */ | FLG_D /* D */ | FLG_A/* A */); + flush_mmu(); + zeropage(virt as *mut u32); + } // Zero-out the page + #[cfg(not(feature = "swap"))] zeropage(virt as *mut u32); // Move the page into userspace @@ -917,6 +936,11 @@ pub fn ensure_page_exists_inner(address: usize) -> Result Result<(), xous_ker /// Takes in the target PID and virtual address to evict. Performs the unmapping, release from /// the target, and re-mapping into the swapper's memory space. Returns a pointer to the /// data in the swapper's virtual memory space -pub fn evict_page(target_pid: PID, vaddr: usize) -> Result { +pub fn evict_page_inner(target_pid: PID, vaddr: usize) -> Result { use crate::services::SystemServices; SystemServices::with(|system_services| { // swap to the target memory space @@ -1095,3 +1119,26 @@ pub fn evict_page(target_pid: PID, vaddr: usize) -> Result Result { + use crate::services::SystemServices; + SystemServices::with(|system_services| { + let swapper_pid = PID::new(xous_kernel::SWAPPER_PID).unwrap(); + // swap to the swapper space + let swapper_map = system_services.get_process(swapper_pid).unwrap().mapping; + swapper_map.activate()?; + + let payload_virt = MemoryManager::with_mut(|mm| { + let payload_virt = mm + .find_virtual_address(core::ptr::null_mut(), PAGE_SIZE, xous_kernel::MemoryType::Messages) + .expect("couldn't find virtuall address in swapper space for target page") + as usize; + let _result = + map_page_inner(mm, swapper_pid, paddr, payload_virt, MemoryFlags::R | MemoryFlags::W, true); + unsafe { flush_mmu() }; + payload_virt + }); + Ok(payload_virt) + }) +}