kernel: implement move test

We can now `Move` memory from one "process" to another.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-13 18:31:51 +08:00
parent 40ed6365e2
commit 05ccba9450
7 changed files with 39 additions and 36 deletions

View file

@ -77,10 +77,10 @@ pub fn move_page_inner(
pub fn lend_page_inner(
_mm: &mut MemoryManager,
_src_space: &MemoryMapping,
_src_addr: *mut usize,
_src_addr: *mut u8,
_dest_pid: PID,
_dest_space: &MemoryMapping,
_dest_addr: *mut usize,
_dest_addr: *mut u8,
_mutable: bool,
) -> Result<usize, Error> {
unimplemented!()

View file

@ -91,14 +91,15 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<ThreadMessage>)
})
.unwrap();
// Update the address pointer. This will get turned back into a
// usable pointer by casting it back into a Box<[T]> on the other
// side. This is a pointer to &[T], which itself contains len()
// usable pointer by casting it back into a &[T] on the other
// side. This is just a pointer to the start of data
// as well as the index into the data it points at. The lengths
// should still be equal once we reconstitute the data in the
// other process.
// ::debug_here::debug_here!();
let sliced_data = tmp_data.into_boxed_slice();
assert_eq!(
(*sliced_data).len(),
sliced_data.len(),
msg.buf.len(),
"deconstructed data {} != message buf length {}",
sliced_data.len(),

View file

@ -241,10 +241,10 @@ impl MemoryManager {
/// to fit `size` bytes.
pub fn find_virtual_address(
&mut self,
virt_ptr: *mut usize,
virt_ptr: *mut u8,
size: usize,
kind: xous::MemoryType,
) -> Result<*mut usize, xous::Error> {
) -> Result<*mut u8, xous::Error> {
// If we were supplied a perfectly good address, return that.
if virt_ptr as usize != 0 {
return Ok(virt_ptr);
@ -260,7 +260,7 @@ impl MemoryManager {
if new_virt + size > process_inner.mem_heap_base + process_inner.mem_heap_max {
return Err(xous::Error::OutOfMemory);
}
return Ok(new_virt as *mut usize);
return Ok(new_virt as *mut u8);
}
xous::MemoryType::Default => (
process_inner.mem_default_base,
@ -290,7 +290,7 @@ impl MemoryManager {
xous::MemoryType::Messages => process_inner.mem_message_last = potential_start,
other => panic!("invalid kind: {:?}", other),
}
return Ok(potential_start as *mut usize);
return Ok(potential_start as *mut u8);
}
}
@ -309,7 +309,7 @@ impl MemoryManager {
xous::MemoryType::Messages => process_inner.mem_message_last = potential_start,
other => panic!("invalid kind: {:?}", other),
}
return Ok(potential_start as *mut usize);
return Ok(potential_start as *mut u8);
}
}
Err(xous::Error::BadAddress)
@ -321,7 +321,7 @@ impl MemoryManager {
/// needing to actually have pages to back it.
pub fn reserve_range(
&mut self,
virt_ptr: *mut usize,
virt_ptr: *mut u8,
size: usize,
flags: MemoryFlags,
) -> Result<xous::MemoryRange, xous::Error> {
@ -350,7 +350,7 @@ impl MemoryManager {
#[allow(dead_code)]
pub fn map_zeroed_page(&mut self, pid: PID, is_user: bool) -> Result<*mut usize, xous::Error> {
let virt =
self.find_virtual_address(0 as *mut usize, PAGE_SIZE, xous::MemoryType::Default)?
self.find_virtual_address(core::ptr::null_mut(), PAGE_SIZE, xous::MemoryType::Default)?
as usize;
// Grab the next available page. This claims it for this process.
@ -383,7 +383,7 @@ impl MemoryManager {
Ok(virt)
}
pub fn is_main_memory(&self, phys: *mut usize) -> bool {
pub fn is_main_memory(&self, phys: *mut u8) -> bool {
(phys as usize) >= self.ram_start && (phys as usize) < self.ram_start + self.ram_size
}
@ -395,8 +395,8 @@ impl MemoryManager {
/// * MemoryInUse - The specified page is already mapped
pub fn map_range(
&mut self,
phys_ptr: *mut usize,
virt_ptr: *mut usize,
phys_ptr: *mut u8,
virt_ptr: *mut u8,
size: usize,
pid: PID,
flags: MemoryFlags,
@ -482,10 +482,10 @@ impl MemoryManager {
pub fn lend_page(
&mut self,
src_mapping: &MemoryMapping,
src_addr: *mut usize,
src_addr: *mut u8,
dest_pid: PID,
dest_mapping: &MemoryMapping,
dest_addr: *mut usize,
dest_addr: *mut u8,
mutable: bool,
) -> Result<usize, xous::Error> {
// If this page is to be writable, detach it from this process.

View file

@ -888,9 +888,9 @@ impl SystemServices {
#[cfg(baremetal)]
pub fn send_memory(
&mut self,
src_virt: *mut usize,
src_virt: *mut u8,
dest_pid: PID,
dest_virt: *mut usize,
dest_virt: *mut u8,
len: usize,
) -> Result<*mut usize, xous::Error> {
if len == 0 {
@ -944,11 +944,11 @@ impl SystemServices {
#[cfg(not(baremetal))]
pub fn send_memory(
&mut self,
src_virt: *mut usize,
src_virt: *mut u8,
_dest_pid: PID,
_dest_virt: *mut usize,
_dest_virt: *mut u8,
_len: usize,
) -> Result<*mut usize, xous::Error> {
) -> Result<*mut u8, xous::Error> {
Ok(src_virt)
}
@ -979,12 +979,12 @@ impl SystemServices {
/// * **BadAlignment**: The provided address or length was not page-aligned
pub fn lend_memory(
&mut self,
src_virt: *mut usize,
src_virt: *mut u8,
dest_pid: PID,
dest_virt: *mut usize,
dest_virt: *mut u8,
len: usize,
mutable: bool,
) -> Result<*mut usize, xous::Error> {
) -> Result<*mut u8, xous::Error> {
if len == 0 {
return Err(xous::Error::BadAddress);
}

View file

@ -18,11 +18,11 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
SysCall::MapMemory(phys, virt, size, req_flags) => {
let mut mm = MemoryManagerHandle::get();
let phys_ptr = phys
.map(|x| x.get() as *mut usize)
.unwrap_or(core::ptr::null_mut::<usize>());
.map(|x| x.get() as *mut u8)
.unwrap_or(core::ptr::null_mut());
let virt_ptr = virt
.map(|x| x.get() as *mut usize)
.unwrap_or(core::ptr::null_mut::<usize>());
.map(|x| x.get() as *mut u8)
.unwrap_or(core::ptr::null_mut());
// Don't let the address exceed the user area (unless it's PID 1)
if pid.get() != 1
@ -107,7 +107,7 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
let start = process_inner.mem_heap_base + process_inner.mem_heap_size;
process_inner.mem_heap_size += delta;
Ok(start as *mut usize)
Ok(start as *mut u8)
})?
};
let mut mm = MemoryManagerHandle::get();
@ -350,7 +350,7 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
let new_virt = ss.send_memory(
msg.buf.as_mut_ptr(),
server_pid,
core::ptr::null_mut::<usize>(),
core::ptr::null_mut(),
msg.buf.len(),
)?;
(
@ -367,7 +367,7 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
let new_virt = ss.lend_memory(
msg.buf.as_mut_ptr(),
server_pid,
core::ptr::null_mut::<usize>(),
core::ptr::null_mut(),
msg.buf.len(),
true,
)?;
@ -385,7 +385,7 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
let new_virt = ss.lend_memory(
msg.buf.as_mut_ptr(),
server_pid,
core::ptr::null_mut::<usize>(),
core::ptr::null_mut(),
msg.buf.len(),
false,
)?;

View file

@ -154,7 +154,9 @@ fn send_move_message() {
let message = envelope.message;
if let xous::Message::Move(m) = message {
let buf = m.buf;
let bt = unsafe { core::slice::from_raw_parts(buf.as_ptr(), buf.len())};
// let bt = unsafe { Box::from_raw(buf.as_mut_ptr() as *mut [u8]) };
// let bt = unsafe { core::slice::from_raw_parts(buf.as_ptr(), buf.len())};
let bt = unsafe { Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len())) };
let s = String::from_utf8_lossy(&bt);
println!("Got message: {:?} -> \"{}\"", bt, s);
} else {

View file

@ -316,8 +316,8 @@ impl MemoryRange {
self.addr.get() as *const u8
}
pub fn as_mut_ptr(&self) -> *mut usize {
self.addr.get() as *mut usize
pub fn as_mut_ptr(&self) -> *mut u8 {
self.addr.get() as *mut u8
}
}