message::move: implement movable messages

This is the first implementation of message passing with moving memory.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-03-31 18:03:19 +08:00
parent 8c3f931445
commit abce9eb0c1
11 changed files with 266 additions and 134 deletions

View file

@ -3,9 +3,11 @@
#[macro_use]
mod debug;
mod log_string;
mod start;
use core::panic::PanicInfo;
use log_string::LogString;
#[panic_handler]
fn handle_panic(arg: &PanicInfo) -> ! {
@ -71,12 +73,23 @@ fn main() {
// }
println!("Starting server...");
let server_addr = xous::syscall::create_server(xous::make_name!("📜")).expect("couldn't create server");
let server_addr =
xous::syscall::create_server(xous::make_name!("📜")).expect("couldn't create server");
println!("Server listening on address {:?}", server_addr);
loop {
println!("Waiting for an event...");
let msg = xous::syscall::receive_message(server_addr).expect("couldn't get address");
println!("Got message: {:?}", msg);
// println!("Waiting for an event...");
let envelope = xous::syscall::receive_message(server_addr).expect("couldn't get address");
// println!("Got message envelope: {:?}", envelope);
match envelope.message {
xous::Message::Scalar(msg) => {
println!("Scalar message from {}: {:?}", envelope.sender, msg)
}
xous::Message::Move(msg) => {
let log_entry = LogString::from_message(msg);
println!("Log message from {}: {}", envelope.sender, log_entry);
}
_ => unimplemented!(),
}
}
}

View file

@ -7,6 +7,9 @@ mod start;
mod timer;
mod logstr;
use core::fmt::Write;
use core::panic::PanicInfo;
#[panic_handler]
@ -25,13 +28,13 @@ fn handle_irq(irq_no: usize, arg: *mut usize) {
println!("");
}
fn print_and_yield(index: *mut usize) -> ! {
let num = index as usize;
loop {
println!("THREAD {}", num);
xous::syscall::yield_slice();
}
}
// fn print_and_yield(index: *mut usize) -> ! {
// let num = index as usize;
// loop {
// println!("THREAD {}", num);
// xous::syscall::yield_slice();
// }
// }
#[no_mangle]
fn main() {
@ -122,7 +125,7 @@ fn main() {
let mut counter = 0;
loop {
println!("Sending a scalar message with id {}...", counter + 4096);
let result = xous::syscall::send_message(
xous::syscall::send_message(
connection,
xous::Message::Scalar(xous::ScalarMessage {
id: counter + 4096,
@ -132,10 +135,22 @@ fn main() {
arg4: counter + 1,
}),
)
.expect("couldn't send message");
.expect("couldn't send scalar message");
counter += 1;
if counter & 2 == 0 {
xous::syscall::yield_slice();
}
let mut ls = logstr::LogStr::new();
write!(ls, "Hello, Server! Loop number: {}", counter).expect("couldn't send hello message");
xous::syscall::send_message(
connection,
xous::Message::Move(
ls.into_memory_message(0)
.expect("couldn't form memory message"),
),
)
.expect("couldn't send memory message");
}
}

View file

@ -97,21 +97,6 @@ impl MemoryMapping {
satp::write(self.satp);
}
// /// Get the flags for a given address, or `0` if none is set.
// pub fn flags_for_address(&self, addr: usize) -> usize {
// let vpn1 = (addr >> 22) & ((1 << 10) - 1);
// let vpn0 = (addr >> 12) & ((1 << 10) - 1);
// let l1_pt = unsafe { &mut (*(PAGE_TABLE_ROOT_OFFSET as *mut RootPageTable)) };
// let l0_pt = l1_pt.entries[vpn1];
// if l0_pt & 1 == 0 {
// return 0;
// }
// let l0pt_virt = PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE;
// let ref mut l0_pt = unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) };
// l0_pt.entries[vpn0]
// }
pub fn print_map(&self) {
println!("Memory Maps:");
let l1_pt = unsafe { &mut (*(PAGE_TABLE_ROOT_OFFSET as *mut RootPageTable)) };
@ -385,13 +370,17 @@ pub fn map_page_inner(
Ok(())
}
/// Ummap the given page from the specified process table. Never
/// allocate a new page.
/// Ummap the given page from the specified process table. Never allocate a new
/// page.
///
/// # Returns
///
/// The physical address for the page that was just unmapped
///
/// # Errors
///
/// * BadAddress - Address was not already mapped.
pub fn unmap_page_inner(_mm: &mut MemoryManager, virt: usize) -> Result<(), xous::Error> {
pub fn unmap_page_inner(_mm: &mut MemoryManager, virt: usize) -> Result<usize, xous::Error> {
let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1);
let vpo = (virt >> 0) & ((1 << 12) - 1);
@ -419,10 +408,11 @@ pub fn unmap_page_inner(_mm: &mut MemoryManager, virt: usize) -> Result<(), xous
if l0_pt.entries[vpn0] & 1 == 0 {
return Err(xous::Error::BadAddress);
}
let phys = (l0_pt.entries[vpn0] >> 10) << 12;
l0_pt.entries[vpn0] = 0;
unsafe { flush_mmu() };
Ok(())
Ok(phys)
}
pub fn virt_to_phys(virt: usize) -> Result<usize, xous::Error> {
@ -448,7 +438,7 @@ pub fn virt_to_phys(virt: usize) -> Result<usize, xous::Error> {
if l0_pt.entries[vpn0] & 1 == 0 {
return Err(xous::Error::BadAddress);
}
Ok(l0_pt.entries[vpn0])
Ok((l0_pt.entries[vpn0] >> 10) << 12)
}
/// Determine whether a virtual address has been mapped

View file

@ -101,7 +101,7 @@ pub mod mem {
pub fn unmap_page_inner(
mm: &mut MemoryManager,
virt: usize,
) -> Result<(), XousError> {
) -> Result<usize, XousError> {
unimplemented!();
}

View file

@ -104,14 +104,15 @@ pub extern "C" fn main() {
0xF0002000 as *mut usize,
((debug::SUPERVISOR_UART.base as u32) & !4095) as *mut usize,
4096,
1,
MemoryFlags::R | MemoryFlags::W,
MemoryType::Default,
)
.expect("unable to map serial port");
println!("KMAIN: Supervisor mode started...");
debug::SUPERVISOR_UART.enable_rx();
println!("Claiming IRQ 3 via syscall...");
xous::claim_interrupt(3, debug::irq, 0 as *mut usize)
.expect("Couldn't claim interrupt 3");
xous::claim_interrupt(3, debug::irq, 0 as *mut usize).expect("Couldn't claim interrupt 3");
print!("}} ");
}

View file

@ -208,8 +208,7 @@ impl MemoryManager {
}
}
/// Allocate a single page to the given process.
/// DOES NOT ZERO THE PAGE!!!
/// Allocate a single page to the given process. DOES NOT ZERO THE PAGE!!!
/// This function CANNOT zero the page, as it hasn't been mapped yet.
pub fn alloc_page(&mut self, pid: PID) -> Result<usize, xous::Error> {
// Go through all RAM pages looking for a free page.
@ -275,7 +274,7 @@ impl MemoryManager {
// 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);
// 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) {
@ -294,7 +293,7 @@ impl MemoryManager {
}
for potential_start in (start..initial).step_by(PAGE_SIZE) {
println!(" Checking {:08x}...", potential_start);
// 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) {
@ -323,7 +322,7 @@ impl MemoryManager {
virt_ptr: *mut usize,
size: usize,
flags: MemoryFlags,
) -> Result<xous::Result, xous::Error> {
) -> Result<xous::MemoryRange, 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, xous::MemoryType::Default)?;
@ -341,10 +340,10 @@ impl MemoryManager {
// FIXME: Un-reserve addresses if we encounter an error here
mm.reserve_address(self, virt, flags)?;
}
Ok(xous::Result::MemoryRange(xous::MemoryRange {
Ok(xous::MemoryRange {
base: virt_ptr as *mut u8,
size,
}))
})
}
/// Attempt to allocate a single page from the default section.
@ -394,14 +393,12 @@ impl MemoryManager {
phys_ptr: *mut usize,
virt_ptr: *mut usize,
size: usize,
pid: u8,
flags: MemoryFlags,
) -> Result<xous::Result, xous::Error> {
kind: xous::MemoryType,
) -> Result<xous::MemoryRange, xous::Error> {
let phys = phys_ptr as usize;
let virt = self.find_virtual_address(virt_ptr, size, xous::MemoryType::Default)?;
let pid = {
let ss = SystemServicesHandle::get();
ss.current_pid()
};
let virt = self.find_virtual_address(virt_ptr, size, kind)?;
// If no physical address is specified, give the user the next available pages
if phys == 0 {
@ -437,56 +434,19 @@ impl MemoryManager {
}
}
Ok(xous::Result::MemoryRange(xous::MemoryRange {
Ok(xous::MemoryRange {
base: virt as *mut u8,
size,
}))
})
}
// /// Map a range of physical addresses into the current memory space.
// pub fn map_range(&mut self, phys: *mut usize, virt: *mut usize, size: usize, flags: MemoryFlags) -> xous::Result {
// // If a physical address range was requested, verify that it is valid
// if phys as usize != 0 {
// }
// let virt = if virt as usize == 0 {
// // No virt address was
// }
// for offset in (0..size).step_by(4096) {
// if let xous::Result::Error(e) = crate::arch::mem::
// map_page_inner(
// ((phys as usize) + offset) as usize,
// ((virt as usize) + offset) as usize,
// req_flags,
// )
// .map(|x| xous::Result::MemoryAddress(x.get() as *mut usize))
// .unwrap_or_else(|e| xous::Result::Error(e))
// {
// result = xous::Result::Error(e);
// break;
// }
// last_mapped = offset;
// }
// if result != xous::Result::Ok {
// for offset in (0..last_mapped).step_by(4096) {
// mm.unmap_page(
// ((phys as usize) + offset) as *mut usize,
// ((virt as usize) + offset) as *mut usize,
// req_flags,
// )
// .expect("couldn't unmap page");
// }
// }
// result
// }
/// Attempt to map the given physical address into the virtual address space
/// of this process.
///
/// # Errors
///
/// * MemoryInUse - The specified page is already mapped
pub fn unmap_page(&mut self, virt: *mut usize) -> Result<(), xous::Error> {
pub fn unmap_page(&mut self, virt: *mut usize) -> Result<usize, xous::Error> {
let pid = crate::arch::current_pid();
let phys = crate::arch::mem::virt_to_phys(virt as usize)?;
self.release_page(phys as *mut usize, pid)?;

View file

@ -118,8 +118,8 @@ impl Server {
id,
buf: MemoryAddress::new(buf),
buf_size: MemorySize::new(buf_size),
_offset: MemorySize::new(offset),
_valid: MemorySize::new(valid),
offset: MemorySize::new(offset),
valid: MemorySize::new(valid),
}),
},
context,
@ -139,8 +139,8 @@ impl Server {
id,
buf: MemoryAddress::new(buf),
buf_size: MemorySize::new(buf_size),
_offset: MemorySize::new(offset),
_valid: MemorySize::new(valid),
offset: MemorySize::new(offset),
valid: MemorySize::new(valid),
}),
},
context,
@ -153,8 +153,8 @@ impl Server {
id,
buf: MemoryAddress::new(buf),
buf_size: MemorySize::new(buf_size),
_offset: MemorySize::new(offset),
_valid: MemorySize::new(valid),
offset: MemorySize::new(offset),
valid: MemorySize::new(valid),
}),
},
context,
@ -194,9 +194,23 @@ impl Server {
self.queue[self.queue_head] = match envelope.message {
xous::Message::Scalar(msg) => QueuedMessage::ScalarMessage(
envelope.sender, context, msg.id, msg.arg1, msg.arg2, msg.arg3, msg.arg4,
envelope.sender,
context,
msg.id,
msg.arg1,
msg.arg2,
msg.arg3,
msg.arg4,
),
xous::Message::Move(msg) => QueuedMessage::MemoryMessageSend(
envelope.sender,
context,
msg.id,
msg.buf.map(|x| x.get()).unwrap_or(0) as usize,
msg.buf_size.map(|x| x.get()).unwrap_or(0) as usize,
msg.offset.map(|x| x.get()).unwrap_or(0) as usize,
msg.valid.map(|x| x.get()).unwrap_or(0) as usize,
),
xous::Message::Move(msg) => unimplemented!(),
xous::Message::MutableBorrow(msg) => unimplemented!(),
xous::Message::ImmutableBorrow(msg) => unimplemented!(),
};

View file

@ -7,7 +7,7 @@ use crate::filled_array;
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
use crate::server::Server;
use core::{mem, slice};
use xous::{CtxID, MemoryFlags, MessageEnvelope, CID, PID, SID};
use xous::{CtxID, MemoryFlags, MemoryType, MessageEnvelope, CID, PID, SID};
const MAX_PROCESS_COUNT: usize = 32;
const MAX_SERVER_COUNT: usize = 32;
@ -647,6 +647,97 @@ impl SystemServices {
Ok(new_context)
}
/// Move memory from one process to another.
///
/// During this process, memory is deallocated from the first process, then
/// we switch contexts and look for a free slot in the second process. After
/// that, we switch back to the first process and return.
///
/// If no free slot can be found, memory is re-attached to the first
/// process. By following this break-then-make approach, we avoid getting
/// into a situation where memory may appear in two different processes at
/// once.
///
/// The given memory range is guaranteed to be unavailable in this process
/// after this function returns.
///
/// # Returns
///
/// Returns the virtual address of the memory region in the target process.
pub fn send_memory(
&mut self,
src_virt: *mut usize,
dest_pid: PID,
len: usize,
writable: bool,
borrow: bool,
) -> Result<usize, xous::Error> {
let current_pid = self.current_pid();
let phys = {
let mut error = None;
let mut mm = MemoryManagerHandle::get();
// Unmap each address from the current memory space. If we
// encounter an error, continue unmapping.
let phys = mm.unmap_page(src_virt).unwrap_or_else(|e| {
error = Some(e);
0
});
for addr in
((src_virt as usize + PAGE_SIZE)..((src_virt as usize) + len)).step_by(PAGE_SIZE)
{
if let Err(e) = mm.unmap_page(addr as *mut usize) {
error = Some(e)
}
}
if let Some(e) = error {
return Err(e);
}
phys
};
// Switch to the target process, so we can manipulate its page tables.
// From this point forward we can't use the `?` operator, since it would
// leave us in the incorrect memory space.
self.get_process(dest_pid)?.mapping.activate();
let mut mm = MemoryManagerHandle::get();
let mut flags = MemoryFlags::R;
if writable {
flags |= MemoryFlags::W;
}
let result = mm.map_range(
phys as *mut usize,
0 as *mut usize,
len,
dest_pid,
flags,
MemoryType::Messages,
);
if let Ok(ref range) = result {
for offset in
((range.base as usize)..(range.base as usize + range.size)).step_by(PAGE_SIZE)
{
println!("Handing page to user");
crate::arch::mem::hand_page_to_user(offset as *mut usize)
.expect("couldn't hand page to user");
}
}
// Finally, switch back to the original process.
self.get_process(current_pid)
.expect("couldn't find previous process")
.mapping
.activate();
println!(
"send_memory: Sent phys {:08x} from {:08x} to {:08x}",
phys,
src_virt as usize,
result.as_ref().unwrap().base as usize
);
result.map(|virt| virt.base as usize)
}
pub fn spawn_thread(
&mut self,
entrypoint: *mut usize,

View file

@ -153,18 +153,34 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
// "Mapping {:08x} -> {:08x} ({} bytes, flags: {:?})",
// phys as u32, virt as u32, size, req_flags
// );
let result = mm.map_range(phys, virt, size, req_flags)?;
if let xous::Result::MemoryRange(ref r) = result {
// If we're handing back an address in main RAM, zero it out
if phys as usize == 0 || mm.is_main_memory(phys) {
unsafe { r.base.write_bytes(0, r.size / mem::size_of::<usize>()) };
let range = mm.map_range(phys, virt, size, pid, req_flags, MemoryType::Default)?;
// If we're handing back an address in main RAM, zero it out. If
// phys is 0, then the page will be lazily allocated, so we
// don't need to do this.
if phys as usize != 0 {
if mm.is_main_memory(phys) {
println!(
"Going to zero out {} bytes @ {:08x}",
range.size, range.base as usize
);
unsafe {
range
.base
.write_bytes(0, range.size / mem::size_of::<usize>())
};
println!("Done zeroing out");
}
for offset in ((r.base as usize)..(r.base as usize + r.size)).step_by(PAGE_SIZE) {
for offset in
((range.base as usize)..(range.base as usize + range.size)).step_by(PAGE_SIZE)
{
println!("Handing page to user");
crate::arch::mem::hand_page_to_user(offset as *mut usize)
.expect("couldn't hand page to user");
}
}
Ok(result)
Ok(xous::Result::MemoryRange(range))
}
SysCall::IncreaseHeap(delta, flags) => {
if delta & 0xfff != 0 {
@ -182,7 +198,9 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
start as *mut usize
};
let mut mm = MemoryManagerHandle::get();
mm.reserve_range(start, delta, flags)
Ok(xous::Result::MemoryRange(
mm.reserve_range(start, delta, flags)?,
))
}
SysCall::DecreaseHeap(delta) => {
if delta & 0xfff != 0 {
@ -277,18 +295,46 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
SysCall::SendMessage(cid, message) => {
let mut ss = SystemServicesHandle::get();
let sidx = ss.sidx_from_cid(cid).ok_or(xous::Error::ServerNotFound)?;
let (server_pid, available_context) = {
let server = ss
.server_from_sidx(sidx)
.ok_or(xous::Error::ServerNotFound)?;
(server.pid, server.take_available_context())
};
// Determine whether the call is blocking. If so, switch to the
// server context right away.
let blocking = match message {
Message::MutableBorrow(_) | Message::ImmutableBorrow(_) => true,
Message::Scalar(_) | Message::Move(_) => false,
let server_pid = ss
.server_from_sidx(sidx)
.expect("server couldn't be located")
.pid;
// Translate memory messages from the client process to the server
// process. Additionally, determine whether the call is blocking.
// If so, switch to the server context right away.
let (message, blocking) = match message {
Message::Scalar(_) => (message, false),
Message::Move(msg) => {
let new_virt = if let Some(virt) = msg.buf {
if msg.buf_size.is_none() {
return Err(xous::Error::BadAddress);
}
let len = msg.buf_size.unwrap().get();
MemoryAddress::new(ss.send_memory(
virt.get() as *mut usize,
server_pid,
len,
true,
false,
)?)
} else {
None
};
(
Message::Move(MemoryMessage {
id: msg.id,
buf: new_virt,
buf_size: msg.buf_size,
offset: msg.offset,
valid: msg.valid,
}),
false,
)
}
Message::MutableBorrow(_) => unimplemented!(),
Message::ImmutableBorrow(_) => unimplemented!(),
};
let envelope = MessageEnvelope {
@ -298,7 +344,11 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
// If the server has an available context to receive the message,
// transfer it right away.
if let Some(ctx_number) = available_context {
if let Some(ctx_number) = ss
.server_from_sidx(sidx)
.expect("server couldn't be located")
.take_available_context()
{
println!(
"There are contexts available to handle this message. Marking PID {} as Ready",
server_pid
@ -325,9 +375,7 @@ pub fn handle(call: SysCall) -> core::result::Result<xous::Result, xous::Error>
let server = ss
.server_from_sidx(sidx)
.ok_or(xous::Error::ServerNotFound)?;
println!("Adding to queue");
ss.queue_server_message(sidx, context_nr, envelope)?;
println!("Done adding to queue");
// Park this context if it's blocking. This is roughly
// equivalent to a "Yield".

View file

@ -70,10 +70,10 @@ pub struct MemoryMessage {
/// The offset within the buffer where the interesting
/// stuff starts.
pub _offset: Option<MemorySize>,
pub offset: Option<MemorySize>,
/// How many bytes in the buffer are valid
pub _valid: Option<MemorySize>,
pub valid: Option<MemorySize>,
}
#[repr(C)]

View file

@ -447,8 +447,8 @@ impl SysCall {
mm.id as usize,
mm.buf.map(|x| x.get()).unwrap_or(0) as usize,
mm.buf_size.map(|x| x.get()).unwrap_or(0) as usize,
mm._offset.map(|x| x.get()).unwrap_or(0) as usize,
mm._valid.map(|x| x.get()).unwrap_or(0) as usize,
mm.offset.map(|x| x.get()).unwrap_or(0) as usize,
mm.valid.map(|x| x.get()).unwrap_or(0) as usize,
],
Message::ImmutableBorrow(mm) => [
SysCallNumber::SendMessage as usize,
@ -457,8 +457,8 @@ impl SysCall {
mm.id as usize,
mm.buf.map(|x| x.get()).unwrap_or(0) as usize,
mm.buf_size.map(|x| x.get()).unwrap_or(0) as usize,
mm._offset.map(|x| x.get()).unwrap_or(0) as usize,
mm._valid.map(|x| x.get()).unwrap_or(0) as usize,
mm.offset.map(|x| x.get()).unwrap_or(0) as usize,
mm.valid.map(|x| x.get()).unwrap_or(0) as usize,
],
Message::Move(mm) => [
SysCallNumber::SendMessage as usize,
@ -467,8 +467,8 @@ impl SysCall {
mm.id as usize,
mm.buf.map(|x| x.get()).unwrap_or(0) as usize,
mm.buf_size.map(|x| x.get()).unwrap_or(0) as usize,
mm._offset.map(|x| x.get()).unwrap_or(0) as usize,
mm._valid.map(|x| x.get()).unwrap_or(0) as usize,
mm.offset.map(|x| x.get()).unwrap_or(0) as usize,
mm.valid.map(|x| x.get()).unwrap_or(0) as usize,
],
Message::Scalar(sc) => [
SysCallNumber::SendMessage as usize,
@ -545,8 +545,8 @@ impl SysCall {
id: a3,
buf: MemoryAddress::new(a4),
buf_size: MemorySize::new(a5),
_offset: MemoryAddress::new(a6),
_valid: MemorySize::new(a7),
offset: MemoryAddress::new(a6),
valid: MemorySize::new(a7),
}),
),
2 => SysCall::SendMessage(
@ -555,8 +555,8 @@ impl SysCall {
id: a3,
buf: MemoryAddress::new(a4),
buf_size: MemorySize::new(a5),
_offset: MemoryAddress::new(a6),
_valid: MemorySize::new(a7),
offset: MemoryAddress::new(a6),
valid: MemorySize::new(a7),
}),
),
3 => SysCall::SendMessage(
@ -565,8 +565,8 @@ impl SysCall {
id: a3,
buf: MemoryAddress::new(a4),
buf_size: MemorySize::new(a5),
_offset: MemoryAddress::new(a6),
_valid: MemorySize::new(a7),
offset: MemoryAddress::new(a6),
valid: MemorySize::new(a7),
}),
),
4 => SysCall::SendMessage(