correspending kernel changes for run time page tracker split

memory alloc table split into RAM region and everything else ("extra")
This commit is contained in:
bunnie 2024-05-12 20:23:29 +08:00
parent 903c903b2f
commit 84d42460eb
3 changed files with 65 additions and 35 deletions

View file

@ -30,6 +30,7 @@ _start:
/////// resume prep code /////// resume prep code
// if resume flag is false (0), skip this section // if resume flag is false (0), skip this section
andi a7, a7, 0x2 // select just the resume flag
beq a7, zero, coldboot beq a7, zero, coldboot
/* /*

View file

@ -41,12 +41,17 @@ use xous_kernel::*;
/// # Safety /// # Safety
/// ///
/// This is safe to call only to initialize the kernel. /// This is safe to call only to initialize the kernel.
pub unsafe extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offset: *mut u32) { pub unsafe extern "C" fn init(
arg_offset: *const u32,
init_offset: *const u32,
rpt_offset: usize,
xpt_offset: usize,
) {
args::KernelArguments::init(arg_offset); args::KernelArguments::init(arg_offset);
let args = args::KernelArguments::get(); let args = args::KernelArguments::get();
// Everything needs memory, so the first thing we should do is initialize the memory manager. // Everything needs memory, so the first thing we should do is initialize the memory manager.
crate::mem::MemoryManager::with_mut(|mm| { crate::mem::MemoryManager::with_mut(|mm| {
mm.init_from_memory(rpt_offset, &args).expect("couldn't initialize memory manager") mm.init_from_memory(rpt_offset, xpt_offset, &args).expect("couldn't initialize memory manager")
}); });
SystemServices::with_mut(|system_services| system_services.init_from_memory(init_offset, &args)); SystemServices::with_mut(|system_services| system_services.init_from_memory(init_offset, &args));

View file

@ -106,6 +106,7 @@ static mut MEMORY_ALLOCATIONS: &mut [SwapAlloc] = &mut [];
#[cfg(baremetal)] #[cfg(baremetal)]
#[cfg(not(feature = "swap"))] #[cfg(not(feature = "swap"))]
static mut MEMORY_ALLOCATIONS: &mut [Option<PID>] = &mut []; static mut MEMORY_ALLOCATIONS: &mut [Option<PID>] = &mut [];
static mut EXTRA_ALLOCATIONS: &mut [Option<PID>] = &mut [];
#[cfg(feature = "swap")] #[cfg(feature = "swap")]
static mut SWAP_TIMESTAMP: u64 = 0; static mut SWAP_TIMESTAMP: u64 = 0;
#[cfg(baremetal)] #[cfg(baremetal)]
@ -165,7 +166,8 @@ impl MemoryManager {
#[cfg(baremetal)] #[cfg(baremetal)]
pub fn init_from_memory( pub fn init_from_memory(
&mut self, &mut self,
base: *mut u32, rpt_base: usize,
xpt_base: usize,
args: &crate::args::KernelArguments, args: &crate::args::KernelArguments,
) -> Result<(), xous_kernel::Error> { ) -> Result<(), xous_kernel::Error> {
use core::slice; use core::slice;
@ -180,7 +182,8 @@ impl MemoryManager {
self.ram_size = xarg_def.data[3] as usize; self.ram_size = xarg_def.data[3] as usize;
self.ram_name = xarg_def.data[4]; self.ram_name = xarg_def.data[4];
let mut mem_size = self.ram_size / PAGE_SIZE; let mem_size = self.ram_size / PAGE_SIZE;
let mut extra_size = 0;
for tag in args_iter { for tag in args_iter {
if tag.name == u32::from_le_bytes(*b"MREx") { if tag.name == u32::from_le_bytes(*b"MREx") {
unsafe { unsafe {
@ -200,17 +203,18 @@ impl MemoryManager {
unsafe { unsafe {
for range in EXTRA_REGIONS.iter() { for range in EXTRA_REGIONS.iter() {
mem_size += range.mem_size as usize / PAGE_SIZE; extra_size += range.mem_size as usize / PAGE_SIZE;
} }
} }
#[cfg(feature = "swap")] #[cfg(feature = "swap")]
unsafe { unsafe {
MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(base as *mut SwapAlloc, mem_size) MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(rpt_base as *mut SwapAlloc, mem_size)
}; };
#[cfg(not(feature = "swap"))] #[cfg(not(feature = "swap"))]
unsafe { unsafe {
MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(base as *mut Option<PID>, mem_size) MEMORY_ALLOCATIONS = slice::from_raw_parts_mut(rpt_base as *mut Option<PID>, mem_size)
}; };
unsafe { EXTRA_ALLOCATIONS = slice::from_raw_parts_mut(xpt_base as *mut Option<PID>, extra_size) }
Ok(()) Ok(())
} }
@ -242,7 +246,9 @@ impl MemoryManager {
#[cfg(all(baremetal, feature = "print-debug"))] #[cfg(all(baremetal, feature = "print-debug"))]
pub fn print_ownership(&self) { pub fn print_ownership(&self) {
println!("Ownership ({} bytes in all):", unsafe { MEMORY_ALLOCATIONS.len() }); println!("Ownership ({} bytes in all):", unsafe {
MEMORY_ALLOCATIONS.len() + EXTRA_ALLOCATIONS.len()
});
let mut offset = 0; let mut offset = 0;
unsafe { unsafe {
@ -267,7 +273,7 @@ impl MemoryManager {
} }
} }
offset += self.ram_size / PAGE_SIZE; offset = 0;
// Go through additional regions looking for this address, and claim it // Go through additional regions looking for this address, and claim it
// if it's not in use. // if it's not in use.
@ -275,7 +281,7 @@ impl MemoryManager {
for region in EXTRA_REGIONS { for region in EXTRA_REGIONS {
println!(" Region {}:", region); println!(" Region {}:", region);
for o in 0..(region.mem_size as usize) / PAGE_SIZE { for o in 0..(region.mem_size as usize) / PAGE_SIZE {
if MEMORY_ALLOCATIONS[offset + o].is_some() { if EXTRA_ALLOCATIONS[offset + o].is_some() {
println!( println!(
" {:08x} => {}", " {:08x} => {}",
(region.mem_start as usize) + o * PAGE_SIZE, (region.mem_start as usize) + o * PAGE_SIZE,
@ -663,7 +669,6 @@ impl MemoryManager {
) -> Result<(), xous_kernel::Error> { ) -> Result<(), xous_kernel::Error> {
/// Modify the memory tracking table to note which process owns /// Modify the memory tracking table to note which process owns
/// the specified address. /// the specified address.
#[cfg(not(feature = "swap"))]
fn action_inner( fn action_inner(
owner_addr: &mut Option<PID>, owner_addr: &mut Option<PID>,
pid: PID, pid: PID,
@ -699,7 +704,7 @@ impl MemoryManager {
Ok(()) Ok(())
} }
#[cfg(feature = "swap")] #[cfg(feature = "swap")]
fn action_inner( fn action_inner_tracking(
owner_addr: &mut SwapAlloc, owner_addr: &mut SwapAlloc,
pid: PID, pid: PID,
action: ClaimReleaseMove, action: ClaimReleaseMove,
@ -734,6 +739,7 @@ impl MemoryManager {
} }
Ok(()) Ok(())
} }
let addr = addr as usize; let addr = addr as usize;
// Ensure the address lies on a page boundary // Ensure the address lies on a page boundary
@ -748,10 +754,10 @@ impl MemoryManager {
#[cfg(not(feature = "swap"))] #[cfg(not(feature = "swap"))]
return unsafe { action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action) }; return unsafe { action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action) };
#[cfg(feature = "swap")] #[cfg(feature = "swap")]
return unsafe { action_inner(&mut MEMORY_ALLOCATIONS[offset], pid, action, addr) }; return unsafe { action_inner_tracking(&mut MEMORY_ALLOCATIONS[offset], pid, action, addr) };
} }
offset += self.ram_size / PAGE_SIZE; offset = 0;
// Go through additional regions looking for this address, and claim it // Go through additional regions looking for this address, and claim it
// if it's not in use. // if it's not in use.
unsafe { unsafe {
@ -777,10 +783,7 @@ impl MemoryManager {
// ------------------------------- // -------------------------------
offset += (addr - (region.mem_start as usize)) / PAGE_SIZE; offset += (addr - (region.mem_start as usize)) / PAGE_SIZE;
#[cfg(not(feature = "swap"))] return action_inner(&mut EXTRA_ALLOCATIONS[offset], pid, action);
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; offset += region.mem_size as usize / PAGE_SIZE;
} }
@ -816,33 +819,39 @@ impl MemoryManager {
if offset < self.ram_size as usize / PAGE_SIZE { if offset < self.ram_size as usize / PAGE_SIZE {
Some(self.ram_start as usize + offset * PAGE_SIZE) Some(self.ram_start as usize + offset * PAGE_SIZE)
} else { } else {
// The offset is not within RAM, so it must be in the extra
// regions list. Loop through all regions looking for the
// address. NOTE: This needs to be linear because each memory
// region has a different length.
let mut offset_in_region = offset - (self.ram_size as usize / PAGE_SIZE);
unsafe {
for region in EXTRA_REGIONS {
// If the offset exceeds the current region, skip to the
// next region.
if offset_in_region >= (region.mem_size as usize / PAGE_SIZE) {
offset_in_region -= region.mem_size as usize / PAGE_SIZE;
continue;
}
return Some(region.mem_start as usize + (offset_in_region * PAGE_SIZE));
}
}
// No region was found. // No region was found.
None None
} }
} }
/// Convert an offset in the `EXTRA_ALLOCATIONS` array into a physical address.
#[cfg(baremetal)]
fn allocation_offset_to_address_extra(&self, offset: usize) -> Option<usize> {
// Loop through all regions looking for the address.
// NOTE: This needs to be linear because each memory region has a different length.
let mut offset_in_region = offset;
unsafe {
for region in EXTRA_REGIONS {
// If the offset exceeds the current region, skip to the
// next region.
if offset_in_region >= (region.mem_size as usize / PAGE_SIZE) {
offset_in_region -= region.mem_size as usize / PAGE_SIZE;
continue;
}
return Some(region.mem_start as usize + (offset_in_region * PAGE_SIZE));
}
}
// No region was found.
None
}
/// Free all memory that belongs to a process. This does not unmap the /// Free all memory that belongs to a process. This does not unmap the
/// memory from the process, it only marks it as free. /// memory from the process, it only marks it as free.
/// This is very unsafe because the memory can immediately be re-allocated /// This is very unsafe because the memory can immediately be re-allocated
/// to another process, so only call this as part of destroying a process. /// to another process, so only call this as part of destroying a process.
pub unsafe fn release_all_memory_for_process(&mut self, _pid: PID) { pub unsafe fn release_all_memory_for_process(&mut self, _pid: PID) {
// release the main memory allocs
#[cfg(baremetal)] #[cfg(baremetal)]
for (idx, owner) in MEMORY_ALLOCATIONS.iter_mut().enumerate() { for (idx, owner) in MEMORY_ALLOCATIONS.iter_mut().enumerate() {
// If this address has been allocated to this process, consider // If this address has been allocated to this process, consider
@ -872,6 +881,21 @@ impl MemoryManager {
} }
} }
} }
// release the extra allocs
#[cfg(baremetal)]
for (idx, owner) in EXTRA_ALLOCATIONS.iter_mut().enumerate() {
if owner == &mut Some(_pid) {
let phys_addr = self.allocation_offset_to_address_extra(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 = PID::new(1);
} else {
// Mark this page as free, which allows it to be re-allocated.
*owner = None;
}
}
}
} }
/// Adjust the flags on the given memory range. This allows for stripping flags from a memory /// Adjust the flags on the given memory range. This allows for stripping flags from a memory