From 8606d0ae8307539fcd387406c188f94ff700617c Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 12 May 2024 22:29:50 +0800 Subject: [PATCH] add some more tracking to the loader this will give us some more pages to swap out beyond the initial set of free pages. --- loader/src/bootconfig.rs | 25 +++++++++++++++++++++++++ loader/src/swap.rs | 14 ++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index 4b2d110e1..04de5b36e 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -184,6 +184,28 @@ impl BootConfig { panic!("Tried to change region {:08x} that isn't in defined memory!", addr); } + #[cfg(feature = "swap")] + pub fn change_owner_tracking(&mut self, pid: XousPid, addr: usize, vaddr: usize) { + // First, check to see if the region is in RAM, + if addr >= self.sram_start as usize && addr < self.sram_start as usize + self.sram_size { + self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] + .update(pid, vaddr as u32); + return; + } + // The region isn't in RAM, so check the other memory regions. + let mut xpt_offset = 0; + for region in self.regions.iter() { + let rstart = region.start as usize; + let rlen = region.length as usize; + if addr >= rstart && addr < rstart + rlen { + self.extra_page_tracker[xpt_offset + (addr - rstart) / PAGE_SIZE] = XousPid::from(pid); + return; + } + xpt_offset += rlen / PAGE_SIZE; + } + panic!("Tried to change region {:08x} that isn't in defined memory!", addr); + } + /// Map the given page to the specified process table. If necessary, /// allocate a new page. /// @@ -196,7 +218,10 @@ impl BootConfig { } assert!(!(phys == 0 && flags & FLG_VALID != 0), "cannot map zero page"); if flags & FLG_VALID != 0 { + #[cfg(not(feature = "swap"))] self.change_owner(owner, phys); + #[cfg(feature = "swap")] + self.change_owner_tracking(owner, phys, virt); } match WORD_SIZE { 4 => self.map_page_32(root, phys, virt, flags, owner), diff --git a/loader/src/swap.rs b/loader/src/swap.rs index faab51f35..cc32b6bb7 100644 --- a/loader/src/swap.rs +++ b/loader/src/swap.rs @@ -5,6 +5,7 @@ use aes_gcm_siv::Tag; use crate::XousPid; use crate::PAGE_SIZE; +use crate::SWAPPER_PID; /// Virtual address fields: /// 31 22 21 12 11 0 @@ -113,13 +114,22 @@ pub const SWAP_FLG_WIRED: u32 = 0x1_00; #[derive(Copy, Clone)] pub struct SwapAlloc { - timestamp: u32, + _timestamp: u32, /// virtual_page_number[19:0] | flags[3:0] | pid[7:0] vpn: u32, } impl SwapAlloc { - pub fn from(pid: u8) -> SwapAlloc { SwapAlloc { timestamp: 0, vpn: pid as u32 | SWAP_FLG_WIRED } } + /// Allocations in the loader all start out as "wired", with no virtual address for tracking. + pub fn from(pid: u8) -> SwapAlloc { SwapAlloc { _timestamp: 0, vpn: pid as u32 | SWAP_FLG_WIRED } } + + /// As the page tables are laid in, we can update the tracker with the virtual address. If + /// the page it belongs to the kernel or swapper, mark it as unswappable (WIRED) + pub fn update(&mut self, pid: u8, vaddr: u32) { + self.vpn = pid as u32 + | vaddr & !0xFFFu32 + | if (pid == 1) || (pid == SWAPPER_PID) { SWAP_FLG_WIRED } else { 0 }; + } /// This is a slight abuse of the naming system to provide us cross-compatibility with the case where the /// structure is defined as an overload of the `u8` type