mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel: test: implement mutable borrow
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
d7006e675e
commit
b93abcc926
5 changed files with 185 additions and 34 deletions
|
|
@ -19,6 +19,9 @@ struct ProcessImpl {
|
||||||
|
|
||||||
/// The network connection to the client process.
|
/// The network connection to the client process.
|
||||||
conn: TcpStream,
|
conn: TcpStream,
|
||||||
|
|
||||||
|
/// Memory that may need to be returned to the caller
|
||||||
|
memory_to_return: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Process {
|
impl PartialEq for Process {
|
||||||
|
|
@ -68,7 +71,7 @@ impl ProcessInit {
|
||||||
impl Process {
|
impl Process {
|
||||||
pub fn current() -> Process {
|
pub fn current() -> Process {
|
||||||
let current_pid = PROCESS_TABLE.with(|pt| pt.borrow().current);
|
let current_pid = PROCESS_TABLE.with(|pt| pt.borrow().current);
|
||||||
Process{pid: current_pid}
|
Process { pid: current_pid }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mark this process as running (on the current core?!)
|
/// Mark this process as running (on the current core?!)
|
||||||
|
|
@ -86,7 +89,9 @@ impl Process {
|
||||||
{
|
{
|
||||||
PROCESS_TABLE.with(|pt| {
|
PROCESS_TABLE.with(|pt| {
|
||||||
let process_table = pt.borrow();
|
let process_table = pt.borrow();
|
||||||
let current = &process_table.table[process_table.current.get() as usize - 1].as_ref().unwrap();
|
let current = &process_table.table[process_table.current.get() as usize - 1]
|
||||||
|
.as_ref()
|
||||||
|
.unwrap();
|
||||||
f(¤t.inner)
|
f(¤t.inner)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -132,15 +137,31 @@ impl Process {
|
||||||
|
|
||||||
pub fn set_context_result(&mut self, context: CtxID, result: xous::Result) {
|
pub fn set_context_result(&mut self, context: CtxID, result: xous::Result) {
|
||||||
assert!(context == INITIAL_CONTEXT);
|
assert!(context == INITIAL_CONTEXT);
|
||||||
|
let mut response = vec![];
|
||||||
|
for word in result.to_args().iter_mut() {
|
||||||
|
response.extend_from_slice(&word.to_le_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
PROCESS_TABLE.with(|pt| {
|
PROCESS_TABLE.with(|pt| {
|
||||||
let mut process_table = pt.borrow_mut();
|
let mut process_table = pt.borrow_mut();
|
||||||
let current_pid_idx = process_table.current.get() as usize - 1;
|
let current_pid_idx = process_table.current.get() as usize - 1;
|
||||||
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
|
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
|
||||||
for word in result.to_args().iter_mut() {
|
|
||||||
process.conn
|
if let Some(buf) = process.memory_to_return.take() {
|
||||||
.write_all(&word.to_le_bytes())
|
response.extend_from_slice(&buf);
|
||||||
.expect("Disconnection");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.conn.write_all(&response).expect("Disconnection");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn return_memory(&mut self, buf: &[u8]) {
|
||||||
|
PROCESS_TABLE.with(|pt| {
|
||||||
|
let mut process_table = pt.borrow_mut();
|
||||||
|
let current_pid_idx = process_table.current.get() as usize - 1;
|
||||||
|
let process = &mut process_table.table[current_pid_idx].as_mut().unwrap();
|
||||||
|
assert!(process.memory_to_return.is_none());
|
||||||
|
process.memory_to_return = Some(buf.to_vec());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,6 +175,7 @@ impl Process {
|
||||||
let process = ProcessImpl {
|
let process = ProcessImpl {
|
||||||
inner: Default::default(),
|
inner: Default::default(),
|
||||||
conn: init_data.conn,
|
conn: init_data.conn,
|
||||||
|
memory_to_return: None,
|
||||||
};
|
};
|
||||||
if pid_idx >= process_table.table.len() {
|
if pid_idx >= process_table.table.len() {
|
||||||
process_table.table.push(Some(process));
|
process_table.table.push(Some(process));
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,10 @@ use core::cell::RefCell;
|
||||||
use std::thread_local;
|
use std::thread_local;
|
||||||
|
|
||||||
use crate::filled_array;
|
use crate::filled_array;
|
||||||
use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
// use crate::mem::{MemoryManagerHandle, PAGE_SIZE};
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
use core::mem;
|
// use core::mem;
|
||||||
use xous::{pid_from_usize, CtxID, Error, MemoryAddress, MemoryType, Message, CID, PID, SID};
|
use xous::{pid_from_usize, CtxID, Error, MemoryAddress, Message, CID, PID, SID};
|
||||||
|
|
||||||
const MAX_PROCESS_COUNT: usize = 32;
|
const MAX_PROCESS_COUNT: usize = 32;
|
||||||
const MAX_SERVER_COUNT: usize = 32;
|
const MAX_SERVER_COUNT: usize = 32;
|
||||||
|
|
@ -397,11 +397,11 @@ impl SystemServices {
|
||||||
};
|
};
|
||||||
process.state = ProcessState::Running(available_contexts);
|
process.state = ProcessState::Running(available_contexts);
|
||||||
process.current_context = context as u8;
|
process.current_context = context as u8;
|
||||||
process.mapping.activate();
|
process.mapping.activate()?;
|
||||||
|
|
||||||
// Activate the current context
|
// Activate the current context
|
||||||
let mut arch_process = crate::arch::process::Process::current();
|
let mut arch_process = crate::arch::process::Process::current();
|
||||||
arch_process.set_context(context);
|
arch_process.set_context(context)?;
|
||||||
}
|
}
|
||||||
self.pid = pid;
|
self.pid = pid;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -744,7 +744,7 @@ impl SystemServices {
|
||||||
// Set up the new process, if necessary. Remove the new context from
|
// Set up the new process, if necessary. Remove the new context from
|
||||||
// the list of ready contexts.
|
// the list of ready contexts.
|
||||||
new.state = match new.state {
|
new.state = match new.state {
|
||||||
ProcessState::Setup(init_context) => {
|
ProcessState::Setup(_init_context) => {
|
||||||
// entrypoint, stack, stack_size) => {
|
// entrypoint, stack, stack_size) => {
|
||||||
// let mut process = ProcessHandle::get();
|
// let mut process = ProcessHandle::get();
|
||||||
// println!(
|
// println!(
|
||||||
|
|
@ -1109,10 +1109,21 @@ impl SystemServices {
|
||||||
pub fn return_memory(
|
pub fn return_memory(
|
||||||
&mut self,
|
&mut self,
|
||||||
src_virt: *mut u8,
|
src_virt: *mut u8,
|
||||||
_dest_pid: PID,
|
dest_pid: PID,
|
||||||
_dest_virt: *mut u8,
|
_dest_virt: *mut u8,
|
||||||
_len: usize,
|
len: usize,
|
||||||
) -> Result<*mut u8, xous::Error> {
|
) -> Result<*mut u8, xous::Error> {
|
||||||
|
let buf = unsafe { core::slice::from_raw_parts(src_virt, len) };
|
||||||
|
let current_pid = self.current_pid();
|
||||||
|
{
|
||||||
|
let target_process = self.get_process(dest_pid)?;
|
||||||
|
target_process.activate()?;
|
||||||
|
let mut arch_process = crate::arch::process::Process::current();
|
||||||
|
arch_process.return_memory(buf);
|
||||||
|
}
|
||||||
|
let target_process = self.get_process(current_pid)?;
|
||||||
|
target_process.activate()?;
|
||||||
|
|
||||||
Ok(src_virt)
|
Ok(src_virt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ fn send_move_message() {
|
||||||
let bt = unsafe {
|
let bt = unsafe {
|
||||||
Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()))
|
Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()))
|
||||||
};
|
};
|
||||||
let s = String::from_utf8_lossy(&bt);
|
// let s = String::from_utf8_lossy(&bt);
|
||||||
// println!("Got message: {:?} -> \"{}\"", bt, s);
|
// println!("Got message: {:?} -> \"{}\"", bt, s);
|
||||||
} else {
|
} else {
|
||||||
panic!("unexpected message type");
|
panic!("unexpected message type");
|
||||||
|
|
@ -175,6 +175,8 @@ fn send_borrow_message() {
|
||||||
let server_spec = "localhost:0";
|
let server_spec = "localhost:0";
|
||||||
let (main_thread, server_spec) = start_kernel(server_spec);
|
let (main_thread, server_spec) = start_kernel(server_spec);
|
||||||
let (server_addr_send, server_addr_recv) = channel();
|
let (server_addr_send, server_addr_recv) = channel();
|
||||||
|
let test_str = "Hello, world!";
|
||||||
|
let test_bytes = test_str.as_bytes();
|
||||||
|
|
||||||
let xous_server = spawn(move || {
|
let xous_server = spawn(move || {
|
||||||
xous::hosted::set_xous_address(server_spec);
|
xous::hosted::set_xous_address(server_spec);
|
||||||
|
|
@ -188,7 +190,8 @@ fn send_borrow_message() {
|
||||||
let bt = unsafe {
|
let bt = unsafe {
|
||||||
Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()))
|
Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()))
|
||||||
};
|
};
|
||||||
let s = String::from_utf8_lossy(&bt);
|
assert_eq!(*test_bytes, *bt);
|
||||||
|
// let s = String::from_utf8_lossy(&bt);
|
||||||
// println!("Got message: {:?} -> \"{}\"", bt, s);
|
// println!("Got message: {:?} -> \"{}\"", bt, s);
|
||||||
xous::return_memory(envelope.sender, m.buf).unwrap();
|
xous::return_memory(envelope.sender, m.buf).unwrap();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -206,11 +209,71 @@ fn send_borrow_message() {
|
||||||
let conn = xous::connect(sid).expect("couldn't connect to server");
|
let conn = xous::connect(sid).expect("couldn't connect to server");
|
||||||
|
|
||||||
// Convert the message into a "Carton" that can be shipped as a message
|
// Convert the message into a "Carton" that can be shipped as a message
|
||||||
let msg = xous::carton::Carton::from_bytes(format!("Hello, world!").as_bytes());
|
let carton = xous::carton::Carton::from_bytes(test_bytes);
|
||||||
|
|
||||||
// Send the message to the server
|
// Send the message to the server
|
||||||
xous::send_message(conn, xous::Message::Borrow(msg.into_message(0)))
|
carton.lend(conn, 0).expect("couldn't lend message to server");
|
||||||
.expect("couldn't send a message");
|
});
|
||||||
|
|
||||||
|
xous_server.join().expect("couldn't join server process");
|
||||||
|
xous_client.join().expect("couldn't join client process");
|
||||||
|
|
||||||
|
// Any process ought to be able to shut down the system currently.
|
||||||
|
rsyscall(SysCall::Shutdown).expect("unable to shutdown server");
|
||||||
|
|
||||||
|
main_thread.join().expect("couldn't join kernel process");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn send_mutableborrow_message() {
|
||||||
|
let server_spec = "localhost:0";
|
||||||
|
let (main_thread, server_spec) = start_kernel(server_spec);
|
||||||
|
let (server_addr_send, server_addr_recv) = channel();
|
||||||
|
let test_str = "Hello, world!";
|
||||||
|
let test_bytes = test_str.as_bytes();
|
||||||
|
|
||||||
|
let xous_server = spawn(move || {
|
||||||
|
xous::hosted::set_xous_address(server_spec);
|
||||||
|
let sid = xous::create_server(0x7884_3123).expect("couldn't create test server");
|
||||||
|
server_addr_send.send(sid).unwrap();
|
||||||
|
let envelope = xous::receive_message(sid).expect("couldn't receive messages");
|
||||||
|
// println!("Received message from {}", envelope.sender);
|
||||||
|
let message = envelope.message;
|
||||||
|
if let xous::Message::MutableBorrow(m) = message {
|
||||||
|
let buf = m.buf;
|
||||||
|
let mut bt = unsafe {
|
||||||
|
Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len()))
|
||||||
|
};
|
||||||
|
for letter in bt.iter_mut() {
|
||||||
|
*letter += 1;
|
||||||
|
}
|
||||||
|
xous::return_memory(envelope.sender, m.buf).unwrap();
|
||||||
|
} else {
|
||||||
|
panic!("unexpected message type");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let xous_client = spawn(move || {
|
||||||
|
xous::hosted::set_xous_address(server_spec);
|
||||||
|
|
||||||
|
// Get the server address (out of band) so we know what to connect to
|
||||||
|
let sid = server_addr_recv.recv().unwrap();
|
||||||
|
|
||||||
|
// Perform a connection to the server
|
||||||
|
let conn = xous::connect(sid).expect("couldn't connect to server");
|
||||||
|
|
||||||
|
// Convert the message into a "Carton" that can be shipped as a message
|
||||||
|
let mut carton = xous::carton::Carton::from_bytes(&test_bytes);
|
||||||
|
let mut check_bytes = test_bytes.to_vec();
|
||||||
|
for letter in check_bytes.iter_mut() {
|
||||||
|
*letter += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the message to the server
|
||||||
|
carton.lend_mut(conn, 3).expect("couldn't mutably lend data");
|
||||||
|
|
||||||
|
let modified_bytes: &[u8] = carton.as_ref();
|
||||||
|
assert_eq!(&check_bytes, &modified_bytes);
|
||||||
});
|
});
|
||||||
|
|
||||||
xous_server.join().expect("couldn't join server process");
|
xous_server.join().expect("couldn't join server process");
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,81 @@
|
||||||
//! 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;
|
extern crate alloc;
|
||||||
extern crate core;
|
|
||||||
use alloc::alloc::{alloc, dealloc, Layout};
|
use alloc::alloc::{alloc, dealloc, Layout};
|
||||||
|
|
||||||
use crate::{MemoryMessage, MemoryRange};
|
use crate::{MemoryMessage, MemoryRange, CID, Message, Error};
|
||||||
|
|
||||||
pub struct Carton {
|
#[derive(Debug)]
|
||||||
contents: *mut u8,
|
pub struct Carton<'a> {
|
||||||
size: usize,
|
range: MemoryRange,
|
||||||
|
slice: &'a [u8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Carton {
|
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 len = bytes.len();
|
let size = bytes.len();
|
||||||
let layout = Layout::from_size_align(len, 4096).unwrap();
|
let layout = Layout::from_size_align(size, 4096).unwrap();
|
||||||
let new_mem = unsafe {
|
let new_mem = unsafe {
|
||||||
let new_mem = alloc(layout);
|
let new_mem = alloc(layout);
|
||||||
core::ptr::copy(src_mem, new_mem, len);
|
core::ptr::copy(src_mem, new_mem, size);
|
||||||
new_mem
|
new_mem
|
||||||
};
|
};
|
||||||
Carton {
|
Carton {
|
||||||
contents: new_mem,
|
range: MemoryRange::new(new_mem as usize, size),
|
||||||
size: len,
|
slice: unsafe { core::slice::from_raw_parts_mut(new_mem, size) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_message(self, id: usize) -> MemoryMessage {
|
pub fn into_message(self, id: usize) -> MemoryMessage {
|
||||||
MemoryMessage {
|
MemoryMessage {
|
||||||
id,
|
id,
|
||||||
buf: MemoryRange::new(self.contents as usize, self.size),
|
buf: self.range,
|
||||||
offset: None,
|
offset: None,
|
||||||
valid: None,
|
valid: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform an immutable lend of this Carton to the specified server.
|
||||||
|
/// This function will block until the server returns.
|
||||||
|
pub fn lend(&self, connection: CID, id: usize) -> Result<(), Error> {
|
||||||
|
let msg = MemoryMessage {
|
||||||
|
id,
|
||||||
|
buf: self.range,
|
||||||
|
offset: None,
|
||||||
|
valid: None,
|
||||||
|
};
|
||||||
|
crate::send_message(connection, Message::Borrow(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Perform a mutable lend of this Carton to the server.
|
||||||
|
pub fn lend_mut(&mut self, connection: CID, id: usize) -> Result<(), Error> {
|
||||||
|
let msg = MemoryMessage {
|
||||||
|
id,
|
||||||
|
buf: self.range,
|
||||||
|
offset: None,
|
||||||
|
valid: None,
|
||||||
|
};
|
||||||
|
crate::send_message(connection, Message::MutableBorrow(msg))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Carton {
|
impl<'a> AsRef<MemoryRange> for Carton<'a> {
|
||||||
|
fn as_ref(&self) -> &MemoryRange {
|
||||||
|
&self.range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AsRef<[u8]> for Carton<'a> {
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
&self.slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Drop for Carton<'a> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let layout = Layout::from_size_align(self.size, 4096).unwrap();
|
let layout = Layout::from_size_align(self.range.len(), 4096).unwrap();
|
||||||
let ptr = self.contents;
|
let ptr = self.range.as_mut_ptr();
|
||||||
unsafe { dealloc(ptr, layout) };
|
unsafe { dealloc(ptr, layout) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,26 @@ fn _xous_syscall_to(
|
||||||
xsc.read_exact(&mut data).expect("Server shut down");
|
xsc.read_exact(&mut data).expect("Server shut down");
|
||||||
// pkt.extend_from_slice(data);
|
// pkt.extend_from_slice(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
crate::Message::Borrow(crate::MemoryMessage {
|
||||||
|
id: _id,
|
||||||
|
buf,
|
||||||
|
offset: _offset,
|
||||||
|
valid: _valid,
|
||||||
|
}) => {
|
||||||
|
// Read the buffer back from the remote host and ensure it's the same
|
||||||
|
use core::slice;
|
||||||
|
let mut check_data = Vec::new();
|
||||||
|
check_data.resize(buf.len(), 0);
|
||||||
|
let data = unsafe {
|
||||||
|
slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len())
|
||||||
|
};
|
||||||
|
xsc.read_exact(&mut check_data).expect("Server shut down");
|
||||||
|
|
||||||
|
assert_eq!(data, check_data.as_slice());
|
||||||
|
// pkt.extend_from_slice(data);
|
||||||
|
}
|
||||||
|
|
||||||
crate::Message::Move(crate::MemoryMessage {
|
crate::Message::Move(crate::MemoryMessage {
|
||||||
id: _id,
|
id: _id,
|
||||||
buf: _buf,
|
buf: _buf,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue