checking in the corresponding kernel changes to the RPT type alias

This commit is contained in:
bunnie 2024-05-12 16:28:14 +08:00
parent 6080bc45d8
commit a3e55834e9
5 changed files with 317 additions and 174 deletions

View file

@ -17,6 +17,7 @@ xous-kernel = { package = "xous", version = "0.9.60", features = [
utralib = { version = "0.1.24", optional = true, default_features = false }
rand_chacha = { version = "0.3.1", optional = true, default_features = false }
cramium-hal = { path = "../libs/cramium-hal", optional = true, default_features = false }
loader = { path = "../loader", optional = true, features = ["swap"] }
[target.'cfg(any(windows,unix))'.dev-dependencies]
xous-kernel = { package = "xous", version = "0.9.60", features = [
@ -51,16 +52,16 @@ gdb-stub = ["gdbstub", "gdbstub_arch"]
print-panics = []
report-memory = ["stats_alloc"]
wrap-print = []
big-heap = [] # Cause heaps to start at 12 MM instead of 512 kB
big-heap = [] # Cause heaps to start at 12 MM instead of 512 kB
v2p = ["xous-kernel/v2p"]
swap = ["xous-kernel/swap"]
swap = ["xous-kernel/swap", "loader/swap"]
debug-swap = []
# patches for simulation targets ONLY. Applying these flags will result in totally broken security.
hwsim = []
# default = ["print-panics", "debug-print", "wrap-print"]
default = ["print-panics"]
default = ["print-panics", "debug-swap"]
[target.'cfg(any(windows, unix))'.dependencies]
crossbeam-channel = "0.5"

View file

@ -359,7 +359,10 @@ impl MemoryMapping {
if l1_pt.entries[vpn1] & MMUFlags::VALID.bits() == 0 {
let pid = crate::arch::current_pid();
// Allocate a fresh page
#[cfg(not(feature = "swap"))]
let l0pt_phys = mm.alloc_page(pid)?;
#[cfg(feature = "swap")]
let l0pt_phys = mm.alloc_page(pid, Some(addr))?;
// Mark this entry as a leaf node (WRX as 0), and indicate
// it is a valid page by setting "V".
@ -618,7 +621,10 @@ pub fn map_page_inner(
// Allocate a new level 1 pagetable entry if one doesn't exist.
if unsafe { l1_pt.add(vpn1).read_volatile() } & MMUFlags::VALID.bits() == 0 {
// Allocate a fresh page for the level 1 page table.
#[cfg(not(feature = "swap"))]
let l0_pt_phys = mm.alloc_page(pid)?;
#[cfg(feature = "swap")]
let l0_pt_phys = mm.alloc_page(pid, Some(virt))?;
// Mark this entry as a leaf node (WRX as 0), and indicate
// it is a valid page by setting "V".
@ -916,9 +922,14 @@ pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Er
return Err(xous_kernel::Error::BadAddress);
}
#[cfg(not(feature = "swap"))]
let new_page = MemoryManager::with_mut(|mm| {
mm.alloc_page(crate::arch::process::current_pid()).expect("Couldn't allocate new page")
});
#[cfg(feature = "swap")]
let new_page = MemoryManager::with_mut(|mm| {
mm.alloc_page(crate::arch::process::current_pid(), Some(address)).expect("Couldn't allocate new page")
});
let ppn1 = (new_page >> 22) & ((1 << 12) - 1);
let ppn0 = (new_page >> 12) & ((1 << 10) - 1);
@ -959,23 +970,6 @@ 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
// note that the prior path on `retrieve_page` *won't* hit this call, because `retrieve_page`
// diverges.
#[cfg(feature = "swap")]
{
println!("ensure_page_exists_inner - advise alloc");
Swap::with_mut(|s| {
s.advise_alloc(
crate::arch::process::current_pid(),
crate::arch::process::current_tid(),
virt,
new_page,
None,
)
});
}
};
Ok(new_page)
@ -1145,7 +1139,7 @@ pub fn evict_page_inner(target_pid: PID, vaddr: usize) -> Result<usize, xous_ker
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")
.expect("couldn't find virtual address in swapper space for target page")
as usize;
let _result = map_page_inner(mm, swapper_pid, target_paddr, payload_virt, MemoryFlags::R, true);
unsafe { flush_mmu() };

View file

@ -55,13 +55,59 @@ impl Default for MemoryManager {
fn default() -> Self { Self::default_hack() }
}
#[cfg(feature = "swap")]
use loader::swap::SWAP_FLG_WIRED;
#[cfg(feature = "swap")]
pub struct SwapAlloc {
timestamp: u32,
/// virtual_page_number[19:0] | flags[3:0] | pid[7:0]
vpn: u32,
}
#[cfg(feature = "swap")]
impl SwapAlloc {
pub fn is_pid(&self, pid: PID) -> bool { self.vpn as u8 == pid.get() }
pub fn is_some(&self) -> bool { self.vpn as u8 != 0 }
pub fn is_none(&self) -> bool { self.vpn as u8 == 0 }
pub fn get_pid(&self) -> Option<PID> {
if self.vpn as u8 == 0 { None } else { Some(PID::new(self.vpn as u8).unwrap()) }
}
pub unsafe fn update(&mut self, pid: Option<PID>, vaddr: Option<usize>) {
self.timestamp = crate::swap::Swap::with_mut(|s| s.next_epoch());
if let Some(pid) = pid {
if let Some(va) = vaddr {
self.vpn = (va as u32) & !0xFFF | pid.get() as u32;
} else {
self.vpn = SWAP_FLG_WIRED | pid.get() as u32;
}
} else {
self.vpn = 0;
}
}
pub unsafe fn reparent(&mut self, pid: PID) {
self.timestamp = crate::swap::Swap::with_mut(|s| s.next_epoch());
self.vpn = self.vpn & !&0xFFu32 | pid.get() as u32;
}
}
#[cfg(not(baremetal))]
std::thread_local!(static MEMORY_MANAGER: core::cell::RefCell<MemoryManager> = core::cell::RefCell::new(MemoryManager::default()));
#[cfg(baremetal)]
static mut MEMORY_MANAGER: MemoryManager = MemoryManager::default_hack();
#[cfg(baremetal)]
#[cfg(feature = "swap")]
static mut MEMORY_ALLOCATIONS: &mut [SwapAlloc] = &mut [];
#[cfg(baremetal)]
#[cfg(not(feature = "swap"))]
static mut MEMORY_ALLOCATIONS: &mut [Option<PID>] = &mut [];
#[cfg(feature = "swap")]
static mut SWAP_TIMESTAMP: u64 = 0;
#[cfg(baremetal)]
static mut EXTRA_REGIONS: &[MemoryRangeExtra] = &[];
@ -157,8 +203,14 @@ impl MemoryManager {
mem_size += range.mem_size as usize / PAGE_SIZE;
}
}
unsafe { MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(base as *mut Option<PID>, mem_size) };
#[cfg(feature = "swap")]
unsafe {
MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(base as *mut SwapAlloc, mem_size)
};
#[cfg(not(feature = "swap"))]
unsafe {
MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(base as *mut Option<PID>, mem_size)
};
Ok(())
}
@ -168,6 +220,7 @@ impl MemoryManager {
pub fn ram_used_by(&self, pid: PID) -> usize {
let mut owned_bytes = 0;
#[cfg(baremetal)]
#[cfg(not(feature = "swap"))]
unsafe {
for owner in &MEMORY_ALLOCATIONS[0..self.ram_size / PAGE_SIZE] {
if owner == &Some(pid) {
@ -175,6 +228,15 @@ impl MemoryManager {
}
}
}
#[cfg(baremetal)]
#[cfg(feature = "swap")]
unsafe {
for owner in &MEMORY_ALLOCATIONS[0..self.ram_size / PAGE_SIZE] {
if owner.is_pid(pid) {
owned_bytes += PAGE_SIZE;
}
}
}
owned_bytes
}
@ -199,7 +261,7 @@ impl MemoryManager {
};
for o in 0..self.ram_size / PAGE_SIZE {
unsafe {
if let Some(_allocation) = MEMORY_ALLOCATIONS[offset + o] {
if MEMORY_ALLOCATIONS[offset + o].is_some() {
println!(" {:08x} => {}", self.ram_size + o * PAGE_SIZE, _allocation.get());
}
}
@ -213,7 +275,7 @@ impl MemoryManager {
for region in EXTRA_REGIONS {
println!(" Region {}:", region);
for o in 0..(region.mem_size as usize) / PAGE_SIZE {
if let Some(_allocation) = MEMORY_ALLOCATIONS[offset + o] {
if MEMORY_ALLOCATIONS[offset + o].is_some() {
println!(
" {:08x} => {}",
(region.mem_start as usize) + o * PAGE_SIZE,
@ -229,6 +291,7 @@ impl MemoryManager {
/// Allocate a single page to the given process. DOES NOT ZERO THE PAGE!!!
/// This function CANNOT zero the page, as it hasn't been mapped yet.
#[cfg(baremetal)]
#[cfg(not(feature = "swap"))]
pub fn alloc_page(&mut self, pid: PID) -> Result<usize, xous_kernel::Error> {
// Go through all RAM pages looking for a free page.
// println!("Allocating page for PID {}", pid);
@ -251,6 +314,30 @@ impl MemoryManager {
Err(xous_kernel::Error::OutOfMemory)
}
#[cfg(baremetal)]
#[cfg(feature = "swap")]
pub fn alloc_page(&mut self, pid: PID, vaddr: Option<usize>) -> Result<usize, xous_kernel::Error> {
// Go through all RAM pages looking for a free page.
// println!("Allocating page for PID {}", pid);
unsafe {
let end_point = self.ram_size / PAGE_SIZE;
let starting_point = self.last_ram_page.max(end_point);
for (allocation, index) in MEMORY_ALLOCATIONS[starting_point..end_point]
.iter_mut()
.zip(starting_point..)
.chain(MEMORY_ALLOCATIONS[..starting_point].iter_mut().zip(0..))
{
if allocation.is_none() {
allocation.update(Some(pid), vaddr);
self.last_ram_page = index + 1;
let page = index * PAGE_SIZE + self.ram_start;
return Ok(page);
}
}
}
Err(xous_kernel::Error::OutOfMemory)
}
/// Find a virtual address in the current process that is big enough
/// to fit `size` bytes.
pub fn find_virtual_address(
@ -365,7 +452,10 @@ impl MemoryManager {
as usize;
// Grab the next available page. This claims it for this process.
#[cfg(not(feature = "swap"))]
let phys = self.alloc_page(pid)?;
#[cfg(feature = "swap")]
let phys = self.alloc_page(pid, Some(virt))?;
// Actually perform the map. At this stage, every physical page should be owned by us.
if let Err(e) = crate::arch::mem::map_page_inner(
@ -573,6 +663,7 @@ impl MemoryManager {
) -> Result<(), xous_kernel::Error> {
/// Modify the memory tracking table to note which process owns
/// the specified address.
#[cfg(not(feature = "swap"))]
fn action_inner(
owner_addr: &mut Option<PID>,
pid: PID,
@ -607,6 +698,42 @@ impl MemoryManager {
}
Ok(())
}
#[cfg(feature = "swap")]
fn action_inner(
owner_addr: &mut SwapAlloc,
pid: PID,
action: ClaimReleaseMove,
addr: usize,
) -> Result<(), xous_kernel::Error> {
if let Some(current_pid) = owner_addr.get_pid() {
if current_pid != pid {
// klog!(
// "In claim_or_release({}, {}, {:?}) -- addr is owned by {} not {}",
// owner_addr.map(|v| v.get()).unwrap_or_default(),
// pid,
// action,
// current_pid,
// pid
// );
if let ClaimReleaseMove::Move(existing_pid) = action {
if existing_pid != current_pid {
return Err(xous_kernel::Error::MemoryInUse);
}
} else {
return Err(xous_kernel::Error::MemoryInUse);
}
}
}
match action {
ClaimReleaseMove::Claim | ClaimReleaseMove::Move(_) => {
unsafe { owner_addr.update(Some(pid), Some(addr)) };
}
ClaimReleaseMove::Release => {
unsafe { owner_addr.update(None, None) };
}
}
Ok(())
}
let addr = addr as usize;
// Ensure the address lies on a page boundary
@ -618,7 +745,10 @@ impl MemoryManager {
// Happy path: The address is in main RAM
if addr >= self.ram_start && addr < self.ram_start + self.ram_size {
offset += (addr - self.ram_start) / PAGE_SIZE;
#[cfg(not(feature = "swap"))]
return unsafe { action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action) };
#[cfg(feature = "swap")]
return unsafe { action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action, addr) };
}
offset += self.ram_size / PAGE_SIZE;
@ -647,7 +777,10 @@ impl MemoryManager {
// -------------------------------
offset += (addr - (region.mem_start as usize)) / PAGE_SIZE;
#[cfg(not(feature = "swap"))]
return action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action);
#[cfg(feature = "swap")]
return action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action, addr);
}
offset += region.mem_size as usize / PAGE_SIZE;
}
@ -714,6 +847,7 @@ impl MemoryManager {
for (idx, owner) in MEMORY_ALLOCATIONS.iter_mut().enumerate() {
// If this address has been allocated to this process, consider
// freeing it or reparenting it.
#[cfg(not(feature = "swap"))]
if owner == &mut Some(_pid) {
let phys_addr = self.allocation_offset_to_address(idx).unwrap();
if crate::arch::mem::page_is_lent(phys_addr as *mut u8) {
@ -725,6 +859,18 @@ impl MemoryManager {
*owner = None;
}
}
#[cfg(feature = "swap")]
if owner.is_pid(_pid) {
let phys_addr = self.allocation_offset_to_address(idx).unwrap();
if crate::arch::mem::page_is_lent(phys_addr as *mut u8) {
// If the page is lent, reparent it to PID 1 so it will
// get freed when it is returned.
owner.reparent(PID::new(1).unwrap());
} else {
// Mark this page as free, which allows it to be re-allocated.
owner.update(None, None);
}
}
}
}
@ -776,7 +922,7 @@ impl MemoryManager {
SystemServices::with(|system_services| {
let current_pid = system_services.current_pid();
// Actiavte the debugging process and iterate through it,
// Activate the debugging process and iterate through it,
// noting down each active thread.
for phys in (self.ram_start..self.ram_start + self.ram_size).step_by(PAGE_SIZE) {
let mut owner = None;
@ -796,12 +942,16 @@ impl MemoryManager {
Ok(None) => continue,
Ok(Some(virt)) => {
let allocation_offset = (phys - self.ram_start) / PAGE_SIZE;
let existing_owner = unsafe { MEMORY_ALLOCATIONS[allocation_offset] };
if existing_owner != Some(pid) {
let existing_owner = unsafe { &MEMORY_ALLOCATIONS[allocation_offset] };
#[cfg(feature = "swap")]
let eo = &existing_owner.get_pid();
#[cfg(not(feature = "swap"))]
let eo = existing_owner;
if eo != &Some(pid) {
let is_lent = {
if let Some(existing_owner) = existing_owner {
if let Some(existing_owner) = eo {
system_services
.get_process(existing_owner)
.get_process(*existing_owner)
.unwrap()
.activate()
.unwrap();
@ -821,9 +971,8 @@ impl MemoryManager {
println!(
"!!! 0x{:08x} is owned by {} ({}) but is mapped to {} ({}) -- {}",
phys,
existing_owner.map(|v| v.get() as isize).unwrap_or(-1),
existing_owner
.map(|v| system_services.process_name(v).unwrap_or("<unknown>"))
eo.map(|v| v.get() as isize).unwrap_or(-1),
eo.map(|v| system_services.process_name(v).unwrap_or("<unknown>"))
.unwrap_or("<none>"),
pid.get(),
system_services.process_name(pid).unwrap_or("<unknown>"),

View file

@ -39,10 +39,12 @@ pub enum BlockingSwapOp {
/// PID of the target block, current paddr of block, original vaddr in the space of target block PID,
/// physical address of block
ReadFromSwap(PID, TID, usize, usize, usize),
/*
/// PID of the process to return to after the allocate advisory - if incurred during a page fault
AllocateAdvisory(PID, TID),
/// advisory issued as part of a syscall - e.g., unmap
AllocateAdvisorySyscall(PID, TID, usize, AdviseUnmap),
*/
}
/// Tracks the entire range of the structure being unmapped. May require multiple
@ -62,8 +64,9 @@ static mut SWAP: Swap = Swap {
nested_op: None,
swapper_state: 0,
swapper_args: [0usize; 8],
alloc_advisories: [AllocAdvice::Uninit, AllocAdvice::Uninit, AllocAdvice::Uninit],
alloc_advisories: [AllocAdvice::Uninit; 8],
missing_pages: 0,
epoch: 0,
};
pub struct Swap {
@ -77,14 +80,17 @@ pub struct Swap {
/// is due to things like an alloc advisory pulling in a new superpage while handling the advisory...?
/// Anyways, there is an assert that checks for the overflow condition of nesting.
nested_op: Option<BlockingSwapOp>,
/// state for the swapper. this is a PID-2 local virtual address, passed from the swapper on registration
/// Userspace state pointer for the swapper. This is a PID-2 local virtual address, passed from the
/// swapper on registration
swapper_state: usize,
/// storage for args
swapper_args: [usize; 8],
/// track advisories to the allocator
alloc_advisories: [AllocAdvice; 3],
/// count missing advisories because the swapper has yet to register
alloc_advisories: [AllocAdvice; 8],
/// count missing advisories because the advisory list overflowed
missing_pages: usize,
/// swap epoch tracker
epoch: u32,
}
impl Swap {
pub fn with_mut<F, R>(f: F) -> R
@ -114,6 +120,21 @@ impl Swap {
SWAP.with(|ss| f(&ss.borrow_mut()))
}
pub fn next_epoch(&mut self) -> u32 {
if self.epoch < u32::MAX {
self.epoch += 1;
self.epoch
} else {
// Epoch rollover handling -- two proposals:
// - Fast epoch rollover, but long-lasting performance impact: just reset all counters to 0, and
// let the system re-discover LRU order based on usage patterns again
// - Slow epoch rollover, with no performance impact: go through all pages and "compact" the
// count down to the lowest level, resetting the epoch counter to the next available epoch.
// LRU patterns are maintained, but the search could take a long time.
todo!("Handle swap epoch rollover");
}
}
pub fn register_handler(
&mut self,
s0: u32,
@ -319,6 +340,7 @@ impl Swap {
Ok(xous_kernel::Result::ResumeProcess)
})
}
/*
Some(BlockingSwapOp::AllocateAdvisory(pid, tid)) => {
// Switch to the previous process' address space.
SystemServices::with_mut(|ss| {
@ -353,7 +375,7 @@ impl Swap {
} else {
result
}
}
} */
None => panic!("No previous swap op was set"),
};
if let Some(op) = self.nested_op.take() {
@ -442,141 +464,127 @@ impl Swap {
));
}
}
/// Accumulate allocations and advise the swapper en-bulk of allocations. This will diverge
/// only when it's determined that we need to advise the swapper.
pub fn advise_alloc(
/*
/// Update the alloc scoreboard to transmit to the swapper
pub fn tally_alloc_free(
&mut self,
// also the PID to return from after reporting to the swapper
target_pid: PID,
target_tid: TID,
target_vaddr_in_pid: usize,
paddr: usize,
maybe_unmap: Option<AdviseUnmap>,
is_free: bool,
) {
let mut last_index: usize = 0;
let mut overflow = false;
let new_advice = if is_unmap.is_some() {
AllocAdvice::Free(target_pid, target_vaddr_in_pid, paddr)
} else {
AllocAdvice::Allocate(target_pid, target_vaddr_in_pid, paddr)
};
// First, search the advisories for existing, cached advice that matches a previous op
// advisories are tracked on tables that are keyed off of the physical address,
// so what we need to look for are items that match on the physical address.
let mut stored = false;
for (index, advisory) in self.alloc_advisories.iter_mut().enumerate() {
match advisory {
AllocAdvice::Allocate(_, _, pa) => {
// If the cached advisory is an allocate, see if the incoming advisory is
// a free that matches the address of the existing op. If so, remove it from
// the advisory cache to save on reporting.
if *pa == paddr && maybe_unmap.is_some() {
*advisory = AllocAdvice::Uninit;
return;
if *pa == paddr {
*advisory = new_advice;
stored = true;
break;
}
last_index = index;
overflow = true;
}
AllocAdvice::Free(_, _, _) => {
last_index = index;
overflow = true;
AllocAdvice::Free(_, _, pa) => {
if *pa == paddr {
*advisory = new_advice;
stored = true;
break;
}
}
AllocAdvice::Uninit => {
if maybe_unmap.is_none() {
*advisory = AllocAdvice::Allocate(target_pid, target_vaddr_in_pid, paddr);
} else {
*advisory = AllocAdvice::Free(target_pid, target_vaddr_in_pid, paddr);
}
last_index = index;
overflow = false;
self.alloc_advisories[index] = new_advice;
stored = true;
break;
}
}
}
if !stored {
self.missing_pages += 1;
println!("advise_alloc total missed advisories: {}", self.missing_pages);
}
if self.pc != 0 {
if last_index >= self.alloc_advisories.len() - 1 {
#[cfg(feature = "debug-swap")]
{
// debugging
SystemServices::with(|ss| {
let current = ss.get_process(current_pid()).unwrap();
let state = current.state();
println!(
"advise_alloc: switching to userspace from PID{}-{:?}",
current.pid.get(),
state
);
});
}
self.flush_advisories();
}
}
let swapper_pid = PID::new(xous_kernel::SWAPPER_PID).unwrap();
SystemServices::with(|ss| ss.get_process(swapper_pid).unwrap().mapping.activate().unwrap());
// This should be called between quantum if advisories exist to be flushed.
pub fn flush_advisories(&mut self) {
assert!(self.pc != 0);
let mut alloc_handoff = [AllocAdvice::Uninit; 2];
// this is safe because we've changed into the swapper's memory space
if let Some(unmap_advice) = maybe_unmap {
// not an allocate -> this came through the unmap syscall
unsafe {
self.blocking_activate_swapper(BlockingSwapOp::AllocateAdvisorySyscall(
target_pid,
target_tid,
target_vaddr_in_pid,
unmap_advice,
));
}
} else {
unsafe {
self.blocking_activate_swapper(BlockingSwapOp::AllocateAdvisory(
target_pid, target_tid,
));
}
}
// ^^ also note this has the side effect of clearing the advisory storage table
// call proceeds to swapper space -> we've diverged and will return via the
// swapper return path
// take the first two items and send them
for (index, advisory) in self.alloc_advisories.iter().enumerate() {}
// LEFT OFF at:
// I think advisories have diverged from their original design intent
// We don't want to advise the swapper of every map/unmap -- doing so causes
// a lot of overhead because message passing between processes leads to lots
// of useless advisories. We only want to advise on allocations of heap, stack,
// and text (code) pages. Everything else (memory messages, etc.) would be data
// in active use and probably should not be unmapped?
//
// Also, for efficiency, we want to advise allocations in pairs, to reduce
// expensive user-space transitions; but I think unmaps may have to be advised
// individually because they could be syscalls that have to be returned to.
//
// There is also the problem of having to send multiple advisories to clear
// out the advisory cache, which means we need to have a way to re-enter
// the loop when the call returns.
//
// Questions:
// - can we differentiate between allocates of these types?
// - can we differentiate between the unmap calls?
// - how do we do a loop around advisories being sent to clear the advisory cache?
// - are unmaps special in that they need some specific return handling?
//
if last_index >= self.alloc_advisories.len() - 1 {
#[cfg(feature = "debug-swap")]
{
// debugging
SystemServices::with(|ss| {
let current = ss.get_process(current_pid()).unwrap();
let state = current.state();
println!(
"advise_alloc: switching to userspace from PID{}-{:?}",
current.pid.get(),
state
);
});
}
} else {
// If the swapper hasn't registered yet, don't advise it of anything - the call
// would crash. That being said, this means we effectively get some extra pages of
// memory that are "wired" (not swappable) since they were never marked as allocated.
// If this is a small number, I think it's not worth the complexity to track and
// register? For now, just print to the kernel log so we know how bad this is;
// if the swapper registers *immediately* (without doing any debug prints or sycalls),
// we actually miss no pages.
if overflow {
self.missing_pages += 1;
println!("advise_alloc: missed advisory: {}", self.missing_pages);
let swapper_pid = PID::new(xous_kernel::SWAPPER_PID).unwrap();
SystemServices::with(|ss| ss.get_process(swapper_pid).unwrap().mapping.activate().unwrap());
// this is safe because we've changed into the swapper's memory space
if let Some(unmap_advice) = is_unmap {
// not an allocate -> this came through the unmap syscall
unsafe {
self.blocking_activate_swapper(BlockingSwapOp::AllocateAdvisorySyscall(
target_pid,
target_tid,
target_vaddr_in_pid,
unmap_advice,
));
}
} else {
unsafe {
self.blocking_activate_swapper(BlockingSwapOp::AllocateAdvisory(target_pid, target_tid));
}
}
// ^^ also note this has the side effect of clearing the advisory storage table
// call proceeds to swapper space -> we've diverged and will return via the
// swapper return path
}
}
pub fn unmap(&mut self, range: MemoryRange) -> SysCallResult {
let virt = range.as_ptr() as usize;
let size = range.len();
if cfg!(baremetal) && virt & 0xfff != 0 {
return Err(xous_kernel::Error::BadAlignment);
}
let pid = crate::arch::process::current_pid();
let tid = crate::arch::process::current_tid();
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
self.unmap_inner(pid, tid, addr, AdviseUnmap { base: virt, size })?;
}
Ok(xous_kernel::Result::Ok)
}
/// Note: this call may diverge into the swapper if the advice buffer fills up. We have to be
/// prepared to resume the loop where it left off!
///
/// ASSUME: we are in the process space of the unmap caller, *not* the swapper, on entry.
fn unmap_inner(
&mut self,
pid: PID,
tid: TID,
virt: usize,
mapping: AdviseUnmap,
) -> Result<usize, xous_kernel::Error> {
// If the virtual address has an assigned physical address, release that
// address from this process.
if let Ok(phys) = crate::arch::mem::virt_to_phys(virt as usize) {
MemoryManager::with_mut(|mm| mm.release_page_swap(phys as *mut usize, pid).ok());
self.advise_alloc(pid, tid, virt as usize, phys, Some(mapping));
} else {
// return a null physical pointer if only virtual memory is being freed
self.advise_alloc(pid, tid, virt as usize, 0, Some(mapping));
};
// Free the virtual address.
MemoryManager::with_mut(|mm| crate::arch::mem::unmap_page_inner(mm, virt as usize))
}
*/
}

View file

@ -731,31 +731,22 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
Ok(xous_kernel::Result::MemoryRange(range))
})
}
SysCall::UnmapMemory(range) => {
#[cfg(feature = "swap")]
// this call may diverge if it generates an advisory to the swapper
let result = crate::swap::Swap::with_mut(|s| s.unmap(range));
#[cfg(not(feature = "swap"))]
let result = {
MemoryManager::with_mut(|mm| {
let mut result = Ok(xous_kernel::Result::Ok);
let virt = range.as_ptr() as usize;
let size = range.len();
if cfg!(baremetal) && virt & 0xfff != 0 {
return Err(xous_kernel::Error::BadAlignment);
SysCall::UnmapMemory(range) => MemoryManager::with_mut(|mm| {
let mut result = Ok(xous_kernel::Result::Ok);
let virt = range.as_ptr() as usize;
let size = range.len();
if cfg!(baremetal) && virt & 0xfff != 0 {
return Err(xous_kernel::Error::BadAlignment);
}
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
if let Err(e) = mm.unmap_page(addr as *mut usize) {
if result.is_ok() {
result = Err(e);
}
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
if let Err(e) = mm.unmap_page(addr as *mut usize) {
if result.is_ok() {
result = Err(e);
}
}
}
result
})
};
}
}
result
}
}),
SysCall::IncreaseHeap(delta, flags) => {
if delta & 0xfff != 0 {
return Err(xous_kernel::Error::BadAlignment);