mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: support omitting virtual address when mapping
This forms the basis for message passing. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
967464df93
commit
08e416c776
5 changed files with 94 additions and 10 deletions
|
|
@ -411,7 +411,7 @@ pub fn virt_to_phys(virt: usize) -> Result<usize, xous::Error> {
|
|||
let l0pt_virt = PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE;
|
||||
let ref mut l0_pt = unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) };
|
||||
|
||||
// Allocate a new level 1 pagetable entry if one doesn't exist.
|
||||
// If the level 1 pagetable doesn't exist, then this address is invalid
|
||||
if l1_pt[vpn1] & MMUFlags::VALID.bits() == 0 {
|
||||
return Err(xous::Error::BadAddress);
|
||||
}
|
||||
|
|
@ -422,3 +422,8 @@ pub fn virt_to_phys(virt: usize) -> Result<usize, xous::Error> {
|
|||
}
|
||||
Ok(l0_pt.entries[vpn0])
|
||||
}
|
||||
|
||||
/// Determine whether a virtual address has been mapped
|
||||
pub fn address_available(virt: usize) -> bool {
|
||||
virt_to_phys(virt).is_err()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,3 +180,7 @@ fn _xous_syscall(
|
|||
pub fn virt_to_phys(virt: usize) -> Result<usize, xous::Result> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
pub fn address_available(virt: usize) -> bool {
|
||||
virt_to_phys(virt).is_err()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -234,12 +234,80 @@ impl MemoryManager {
|
|||
|
||||
/// 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<usize, xous::Error> {
|
||||
fn find_virtual_address(
|
||||
&mut self,
|
||||
virt_ptr: *mut usize,
|
||||
size: usize,
|
||||
kind: xous::MemoryType,
|
||||
) -> Result<usize, xous::Error> {
|
||||
// If we were supplied a perfectly good address, return that.
|
||||
if virt_ptr as usize != 0 {
|
||||
return Ok(virt_ptr as usize);
|
||||
}
|
||||
|
||||
let mut ss = SystemServicesHandle::get();
|
||||
let process = ss.current_process_mut()?;
|
||||
|
||||
let (start, end, initial) = match kind {
|
||||
xous::MemoryType::Stack => return Err(xous::Error::BadAddress),
|
||||
xous::MemoryType::Heap => {
|
||||
let new_virt = process.mem_heap_base + process.mem_heap_size + PAGE_SIZE;
|
||||
if new_virt + size > process.mem_heap_base + process.mem_heap_max {
|
||||
return Err(xous::Error::OutOfMemory);
|
||||
}
|
||||
return Ok(new_virt);
|
||||
}
|
||||
xous::MemoryType::Default => (
|
||||
process.mem_default_base,
|
||||
process.mem_default_base + 0x10000000,
|
||||
process.mem_default_last,
|
||||
),
|
||||
xous::MemoryType::Messages => (
|
||||
process.mem_message_base,
|
||||
process.mem_message_base + 0x10000000,
|
||||
process.mem_message_last,
|
||||
),
|
||||
};
|
||||
|
||||
// Look for a sequence of `size` pages that are free.
|
||||
for potential_start in (initial..end - size).step_by(PAGE_SIZE) {
|
||||
println!(" Checking {:08x}...", potential_start);
|
||||
let mut all_free = true;
|
||||
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
||||
if !crate::arch::mem::address_available(check_page) {
|
||||
all_free = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_free {
|
||||
match kind {
|
||||
xous::MemoryType::Default => process.mem_default_last = potential_start,
|
||||
xous::MemoryType::Messages => process.mem_message_last = potential_start,
|
||||
other => panic!("invalid kind: {:?}", other),
|
||||
}
|
||||
return Ok(potential_start);
|
||||
}
|
||||
}
|
||||
|
||||
for potential_start in (start..initial).step_by(PAGE_SIZE) {
|
||||
println!(" Checking {:08x}...", potential_start);
|
||||
let mut all_free = true;
|
||||
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
||||
if !crate::arch::mem::address_available(check_page) {
|
||||
all_free = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_free {
|
||||
match kind {
|
||||
xous::MemoryType::Default => process.mem_default_last = potential_start,
|
||||
xous::MemoryType::Messages => process.mem_message_last = potential_start,
|
||||
other => panic!("invalid kind: {:?}", other),
|
||||
}
|
||||
return Ok(potential_start);
|
||||
}
|
||||
}
|
||||
|
||||
Err(xous::Error::BadAddress)
|
||||
}
|
||||
|
||||
|
|
@ -252,10 +320,9 @@ impl MemoryManager {
|
|||
size: usize,
|
||||
flags: MemoryFlags,
|
||||
) -> Result<xous::Result, xous::Error> {
|
||||
|
||||
// If no address was specified, pick the next address that fits
|
||||
// in the "default" range
|
||||
let virt = self.find_virtual_address(virt_ptr, size)?;
|
||||
let virt = self.find_virtual_address(virt_ptr, size, xous::MemoryType::Default)?;
|
||||
|
||||
if virt & 0xfff != 0 {
|
||||
return Err(xous::Error::BadAlignment);
|
||||
|
|
@ -286,10 +353,12 @@ impl MemoryManager {
|
|||
size: usize,
|
||||
flags: MemoryFlags,
|
||||
) -> Result<xous::Result, xous::Error> {
|
||||
let ss = SystemServicesHandle::get();
|
||||
let pid = ss.current_pid();
|
||||
let phys = phys_ptr as usize;
|
||||
let virt = self.find_virtual_address(virt_ptr, size)?;
|
||||
let virt = self.find_virtual_address(virt_ptr, size, xous::MemoryType::Default)?;
|
||||
let pid = {
|
||||
let ss = SystemServicesHandle::get();
|
||||
ss.current_pid()
|
||||
};
|
||||
|
||||
// If no physical address is specified, give the user the next available pages
|
||||
if phys == 0 {
|
||||
|
|
|
|||
|
|
@ -65,6 +65,9 @@ pub struct Process {
|
|||
/// Address where messages are passed into
|
||||
pub mem_message_base: usize,
|
||||
|
||||
/// The last address that was allocated from
|
||||
pub mem_message_last: usize,
|
||||
|
||||
/// Base address of the heap
|
||||
pub mem_heap_base: usize,
|
||||
|
||||
|
|
@ -115,6 +118,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices {
|
|||
mem_default_base: arch::mem::DEFAULT_BASE,
|
||||
mem_default_last: arch::mem::DEFAULT_BASE,
|
||||
mem_message_base: arch::mem::DEFAULT_MESSAGE_BASE,
|
||||
mem_message_last: arch::mem::DEFAULT_MESSAGE_BASE,
|
||||
mem_heap_base: arch::mem::DEFAULT_HEAP_BASE,
|
||||
mem_heap_size: 0,
|
||||
mem_heap_max: 524288,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::arch;
|
||||
use crate::irq::interrupt_claim;
|
||||
use crate::mem::MemoryManagerHandle;
|
||||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
||||
use crate::processtable::{ProcessState, SystemServicesHandle};
|
||||
use xous::*;
|
||||
|
||||
|
|
@ -138,9 +138,12 @@ pub fn handle(call: SysCall) -> xous::Result {
|
|||
match call {
|
||||
SysCall::MapPhysical(phys, virt, size, req_flags) => {
|
||||
let mut mm = MemoryManagerHandle::get();
|
||||
// Don't let the address exceed the user area (unless it's PID 1)
|
||||
if pid != 1 && (virt as usize) != 0 && (virt as usize) >= arch::mem::USER_AREA_END {
|
||||
return xous::Result::Error(xous::Error::BadAddress);
|
||||
} else if size & 4095 != 0 {
|
||||
|
||||
// Don't allow mapping non-page values
|
||||
} else if size & (PAGE_SIZE - 1) != 0 {
|
||||
// println!("map: bad alignment of size {:08x}", size);
|
||||
return xous::Result::Error(xous::Error::BadAlignment);
|
||||
}
|
||||
|
|
@ -149,7 +152,6 @@ pub fn handle(call: SysCall) -> xous::Result {
|
|||
// phys as u32, virt as u32, size, req_flags
|
||||
// );
|
||||
mm.map_range(phys, virt, size, req_flags)
|
||||
.map(|_x| xous::Result::ReturnResult)
|
||||
.unwrap_or_else(|e| xous::Result::Error(e))
|
||||
}
|
||||
SysCall::IncreaseHeap(delta, flags) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue