mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
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:
parent
ea3a473c9f
commit
6ab4133165
3 changed files with 11 additions and 12 deletions
|
|
@ -9,6 +9,9 @@ use std::thread_local;
|
||||||
|
|
||||||
use crate::{Result, PID, TID};
|
use crate::{Result, PID, TID};
|
||||||
|
|
||||||
|
mod mem;
|
||||||
|
pub use mem::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub struct ProcessKey([u8; 16]);
|
pub struct ProcessKey([u8; 16]);
|
||||||
impl ProcessKey {
|
impl ProcessKey {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
//! A Carton is an object that wraps another object for shipping across the kernel
|
//! 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.
|
//! 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};
|
use crate::{Error, MemoryMessage, MemoryRange, Message, CID};
|
||||||
|
|
||||||
|
|
@ -15,7 +13,6 @@ impl<'a> Carton<'a> {
|
||||||
pub fn from_bytes(bytes: &[u8]) -> Self {
|
pub fn from_bytes(bytes: &[u8]) -> Self {
|
||||||
let src_mem = bytes.as_ptr();
|
let src_mem = bytes.as_ptr();
|
||||||
let size = bytes.len();
|
let size = bytes.len();
|
||||||
// let layout = Layout::from_size_align(size, 4096).unwrap();
|
|
||||||
let new_mem = crate::map_memory(
|
let new_mem = crate::map_memory(
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
|
|
@ -23,11 +20,9 @@ impl<'a> Carton<'a> {
|
||||||
crate::MemoryFlags::R | crate::MemoryFlags::W,
|
crate::MemoryFlags::R | crate::MemoryFlags::W,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// unsafe {
|
unsafe {
|
||||||
// let new_mem = alloc(layout);
|
core::ptr::copy(src_mem, new_mem.as_mut_ptr(), new_mem.len());
|
||||||
// core::ptr::copy(src_mem, new_mem, size);
|
};
|
||||||
// new_mem
|
|
||||||
// };
|
|
||||||
Carton {
|
Carton {
|
||||||
range: new_mem,
|
range: new_mem,
|
||||||
slice: unsafe { core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), new_mem.len()) },
|
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> {
|
impl<'a> Drop for Carton<'a> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// let layout = Layout::from_size_align(self.range.len(), 4096).unwrap();
|
crate::unmap_memory(self.range).unwrap();
|
||||||
// let ptr = self.range.as_mut_ptr();
|
|
||||||
// unsafe { dealloc(ptr, layout) };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -701,6 +701,7 @@ pub fn map_memory(
|
||||||
size: usize,
|
size: usize,
|
||||||
flags: MemoryFlags,
|
flags: MemoryFlags,
|
||||||
) -> core::result::Result<MemoryRange, Error> {
|
) -> core::result::Result<MemoryRange, Error> {
|
||||||
|
crate::arch::map_memory_pre(&phys, &virt, size, flags)?;
|
||||||
let result = rsyscall(SysCall::MapMemory(
|
let result = rsyscall(SysCall::MapMemory(
|
||||||
phys,
|
phys,
|
||||||
virt,
|
virt,
|
||||||
|
|
@ -708,7 +709,7 @@ pub fn map_memory(
|
||||||
flags,
|
flags,
|
||||||
))?;
|
))?;
|
||||||
if let Result::MemoryRange(range) = result {
|
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 {
|
} else if let Result::Error(e) = result {
|
||||||
Err(e)
|
Err(e)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -719,8 +720,10 @@ pub fn map_memory(
|
||||||
/// Map the given physical address to the given virtual address.
|
/// Map the given physical address to the given virtual address.
|
||||||
/// The `size` field must be page-aligned.
|
/// The `size` field must be page-aligned.
|
||||||
pub fn unmap_memory(range: MemoryRange) -> core::result::Result<(), Error> {
|
pub fn unmap_memory(range: MemoryRange) -> core::result::Result<(), Error> {
|
||||||
|
crate::arch::unmap_memory_pre(&range)?;
|
||||||
let result = rsyscall(SysCall::UnmapMemory(range))?;
|
let result = rsyscall(SysCall::UnmapMemory(range))?;
|
||||||
if let Result::Ok = result {
|
if let Result::Ok = result {
|
||||||
|
crate::arch::unmap_memory_post(range)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if let Result::Error(e) = result {
|
} else if let Result::Error(e) = result {
|
||||||
Err(e)
|
Err(e)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue