xous: begin using hooks for memory allocation

This moves memory allocation into hooks, and allows for a common syscall
regardless of platform.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-08-03 17:58:22 +08:00
parent ea3a473c9f
commit 6ab4133165
3 changed files with 11 additions and 12 deletions

View file

@ -9,6 +9,9 @@ use std::thread_local;
use crate::{Result, PID, TID};
mod mem;
pub use mem::*;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ProcessKey([u8; 16]);
impl ProcessKey {

View file

@ -1,7 +1,5 @@
//! A Carton is an object that wraps another object for shipping across the kernel
//! boundary. Structs that are stored in Cartons can be sent as messages.
// extern crate alloc;
// use alloc::alloc::{alloc, dealloc, Layout};
use crate::{Error, MemoryMessage, MemoryRange, Message, CID};
@ -15,7 +13,6 @@ impl<'a> Carton<'a> {
pub fn from_bytes(bytes: &[u8]) -> Self {
let src_mem = bytes.as_ptr();
let size = bytes.len();
// let layout = Layout::from_size_align(size, 4096).unwrap();
let new_mem = crate::map_memory(
None,
None,
@ -23,11 +20,9 @@ impl<'a> Carton<'a> {
crate::MemoryFlags::R | crate::MemoryFlags::W,
)
.unwrap();
// unsafe {
// let new_mem = alloc(layout);
// core::ptr::copy(src_mem, new_mem, size);
// new_mem
// };
unsafe {
core::ptr::copy(src_mem, new_mem.as_mut_ptr(), new_mem.len());
};
Carton {
range: new_mem,
slice: unsafe { core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), new_mem.len()) },
@ -81,8 +76,6 @@ impl<'a> AsRef<[u8]> for Carton<'a> {
impl<'a> Drop for Carton<'a> {
fn drop(&mut self) {
// let layout = Layout::from_size_align(self.range.len(), 4096).unwrap();
// let ptr = self.range.as_mut_ptr();
// unsafe { dealloc(ptr, layout) };
crate::unmap_memory(self.range).unwrap();
}
}

View file

@ -701,6 +701,7 @@ pub fn map_memory(
size: usize,
flags: MemoryFlags,
) -> core::result::Result<MemoryRange, Error> {
crate::arch::map_memory_pre(&phys, &virt, size, flags)?;
let result = rsyscall(SysCall::MapMemory(
phys,
virt,
@ -708,7 +709,7 @@ pub fn map_memory(
flags,
))?;
if let Result::MemoryRange(range) = result {
Ok(range)
Ok(crate::arch::map_memory_post(phys, virt, size, flags, range)?)
} else if let Result::Error(e) = result {
Err(e)
} else {
@ -719,8 +720,10 @@ pub fn map_memory(
/// Map the given physical address to the given virtual address.
/// The `size` field must be page-aligned.
pub fn unmap_memory(range: MemoryRange) -> core::result::Result<(), Error> {
crate::arch::unmap_memory_pre(&range)?;
let result = rsyscall(SysCall::UnmapMemory(range))?;
if let Result::Ok = result {
crate::arch::unmap_memory_post(range)?;
Ok(())
} else if let Result::Error(e) = result {
Err(e)