handle swapped pages and allocation advisories

This commit is contained in:
bunnie 2024-03-08 18:30:18 +08:00
parent 14364e09c6
commit 28b415cb27

View file

@ -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<usize, xous_kernel::Er
let flags = current_entry & 0x1ff;
#[cfg(not(feature = "swap"))]
if flags & MMUFlags::VALID.bits() != 0 {
return Ok(address);
}
#[cfg(feature = "swap")]
if (flags & MMUFlags::VALID.bits() != 0) && (flags & MMUFlags::P.bits() == 0) {
return Ok(address);
}
// If the flags are nonzero, but the "Valid" bit is not 1 and
// the page isn't shared, then this is a reserved page. Allocate
@ -902,14 +908,27 @@ pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Er
let new_page = MemoryManager::with_mut(|mm| {
mm.alloc_page(crate::arch::process::current_pid()).expect("Couldn't allocate new page")
});
let ppn1 = (new_page >> 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<usize, xous_kernel::Er
| (ppn0 << 10)
| (flags | FLG_VALID /* valid */ | FLG_U /* USER */ | FLG_D /* D */ | FLG_A/* A */);
flush_mmu();
// if swap is activated, advise the swapper that data was allocated
// not that the prior path on `retrieve_page` *won't* hit this call, because `retrieve_page` diverges.
#[cfg(feature = "swap")]
Swap::with_mut(|s| s.advise_alloc(crate::arch::process::current_pid(), virt, new_page, true));
};
Ok(new_page)
@ -1055,7 +1079,7 @@ pub fn update_page_flags(virt: usize, flags: MemoryFlags) -> 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<usize, xous_kernel::Error> {
pub fn evict_page_inner(target_pid: PID, vaddr: usize) -> Result<usize, xous_kernel::Error> {
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<usize, xous_kernel::E
Ok(payload_virt)
})
}
#[cfg(feature = "swap")]
pub fn map_page_to_swapper(paddr: usize) -> Result<usize, xous_kernel::Error> {
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)
})
}