From 87676c0fc5d052aa2ae1242ec3e2555b0a563e69 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 12 Mar 2020 17:01:40 +0800 Subject: [PATCH] mem: start implementing find_virtual_address() Right now it just returns the address, which is exactly what we had before. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 0663a4706..a05edbc3d 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -232,6 +232,18 @@ impl MemoryManager { Err(xous::Error::OutOfMemory) } + // Find a virtual address in the current process that is big enough + // to fit `size` bytes. + fn find_virtual_address(&mut self, virt_ptr: *mut usize, size: usize) -> Result { + // If we were supplied a perfectly good address, return that. + if virt_ptr as usize != 0 { + return Ok(virt_ptr as usize); + } + + // let mm = SystemServicesHandle::get().current_process(); + Err(xous::Error::BadAddress) + } + /// Reserve the given range without actually allocating memory. /// That way we can overpromise on stack size and heap size without /// needing to actually have pages to back it. @@ -241,7 +253,10 @@ impl MemoryManager { size: usize, flags: MemoryFlags, ) -> Result { - let virt = virt_ptr as usize; + + // If no address was specified, pick the next address that fits + // in the "default" range + let virt = self.find_virtual_address(virt_ptr, size)?; if virt & 0xfff != 0 { return Err(xous::Error::BadAlignment); @@ -275,11 +290,11 @@ impl MemoryManager { let ss = SystemServicesHandle::get(); let pid = ss.current_pid(); let phys = phys_ptr as usize; - let virt = virt_ptr as usize; + let virt = self.find_virtual_address(virt_ptr, size)?; // If no physical address is specified, give the user the next available pages if phys == 0 { - return self.reserve_range(virt_ptr, size, flags); + return self.reserve_range(virt as *mut usize, size, flags); } // 1. Attempt to claim all physical pages in the range