mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
hosted: start working on memory-based message passing
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
e1a92e929c
commit
acf9b049f7
8 changed files with 249 additions and 39 deletions
|
|
@ -89,7 +89,7 @@ fn listen_thread(
|
|||
backchannel: Receiver<BackchannelMessage>,
|
||||
) {
|
||||
let client_addr = listen_addr.clone();
|
||||
println!("KERNEL(1): Starting Xous server on {}...", listen_addr);
|
||||
// println!("KERNEL(1): Starting Xous server on {}...", listen_addr);
|
||||
let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| {
|
||||
panic!("Unable to create server: {}", e);
|
||||
});
|
||||
|
|
@ -99,7 +99,7 @@ fn listen_thread(
|
|||
let pid1_thread = spawn(move || {
|
||||
let mut client = TcpStream::connect(client_addr).expect("couldn't connect to xous server");
|
||||
let mut buffer = [0; 32];
|
||||
println!("KERNEL(1): Started PID1 idle thread");
|
||||
// println!("KERNEL(1): Started PID1 idle thread");
|
||||
loop {
|
||||
match client.read(&mut buffer) {
|
||||
Ok(0) => return,
|
||||
|
|
@ -130,7 +130,7 @@ fn listen_thread(
|
|||
BackchannelMessage::NewPid(p) => p,
|
||||
x => panic!("unexpected backchannel message from main thread: {:?}", x),
|
||||
};
|
||||
println!("KERNEL({}): New client connected from {}", new_pid, addr);
|
||||
// println!("KERNEL({}): New client connected from {}", new_pid, addr);
|
||||
let conn_copy = conn.try_clone().expect("couldn't duplicate connection");
|
||||
let jh = spawn(move || handle_connection(conn, new_pid, thr_chn));
|
||||
clients.push((jh, conn_copy));
|
||||
|
|
@ -184,20 +184,20 @@ pub fn idle() -> bool {
|
|||
while let Ok(msg) = receiver.recv() {
|
||||
match msg {
|
||||
ThreadMessage::NewConnection(conn) => {
|
||||
println!("KERNEL(?): Going to call ss.spawn_process()");
|
||||
// println!("KERNEL(?): Going to call ss.spawn_process()");
|
||||
let new_pid = SystemServices::with_mut(|ss| {
|
||||
ss.spawn_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
|
||||
})
|
||||
.unwrap();
|
||||
println!("KERNEL({}): SystemServices assigned new PID of {}", new_pid, new_pid);
|
||||
// println!("KERNEL({}): SystemServices assigned new PID of {}", new_pid, new_pid);
|
||||
backchannel_sender
|
||||
.send(BackchannelMessage::NewPid(new_pid))
|
||||
.expect("couldn't send new pid to new connection");
|
||||
}
|
||||
ThreadMessage::SysCall(pid, call) => {
|
||||
println!("KERNEL({}): Received syscall {:?}", pid, call);
|
||||
// println!("KERNEL({}): Received syscall {:?}", pid, call);
|
||||
SystemServices::with_mut(|ss| ss.switch_to(pid, Some(1))).unwrap();
|
||||
println!("KERNEL({}): Now running as the new process", pid);
|
||||
// println!("KERNEL({}): Now running as the new process", pid);
|
||||
|
||||
// If the call being made is to terminate the current process, we need to know
|
||||
// because we won't be able to send a response.
|
||||
|
|
@ -208,7 +208,7 @@ pub fn idle() -> bool {
|
|||
// This is because the "process" will be "terminated" (the network socket will be closed),
|
||||
// and we won't be able to send the response after we're done.
|
||||
if is_shutdown {
|
||||
println!("KERNEL: Detected shutdown -- sending final \"Ok\" to the client");
|
||||
// println!("KERNEL: Detected shutdown -- sending final \"Ok\" to the client");
|
||||
let mut process = Process::current();
|
||||
let mut response_vec = Vec::new();
|
||||
for word in Result::Ok.to_args().iter_mut() {
|
||||
|
|
@ -216,16 +216,16 @@ pub fn idle() -> bool {
|
|||
}
|
||||
process.send(&response_vec).unwrap_or_else(|e| {
|
||||
// If we're unable to send data to the process, assume it's dead and terminate it.
|
||||
println!("Unable to send response to process: {:?} -- terminating", e);
|
||||
// println!("Unable to send response to process: {:?} -- terminating", e);
|
||||
crate::syscall::handle(pid, SysCall::TerminateProcess).ok();
|
||||
});
|
||||
println!("Done sending");
|
||||
// println!("KERNEL: Done sending");
|
||||
}
|
||||
|
||||
// Handle the syscall within the Xous kernel
|
||||
let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error);
|
||||
|
||||
println!("KERNEL({}): Syscall response {:?}", pid, response);
|
||||
// println!("KERNEL({}): Syscall response {:?}", pid, response);
|
||||
// There's a response if it wasn't a blocked process and we're not terminating.
|
||||
// Send the response back to the target.
|
||||
if response != Result::BlockedProcess && !is_terminate && !is_shutdown {
|
||||
|
|
@ -237,10 +237,10 @@ pub fn idle() -> bool {
|
|||
}
|
||||
process.send(&response_vec).unwrap_or_else(|e| {
|
||||
// If we're unable to send data to the process, assume it's dead and terminate it.
|
||||
println!(
|
||||
"KERNEL({}): Unable to send response to process: {:?} -- terminating",
|
||||
pid, e
|
||||
);
|
||||
// println!(
|
||||
// "KERNEL({}): Unable to send response to process: {:?} -- terminating",
|
||||
// pid, e
|
||||
// );
|
||||
crate::syscall::handle(pid, SysCall::TerminateProcess).ok();
|
||||
});
|
||||
}
|
||||
|
|
@ -257,11 +257,11 @@ pub fn idle() -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
println!("Exiting Xous because the listen thread channel has closed. Waiting for thread to finish...");
|
||||
// println!("Exiting Xous because the listen thread channel has closed. Waiting for thread to finish...");
|
||||
listen_thread_handle
|
||||
.join()
|
||||
.expect("error waiting for listen thread to return");
|
||||
|
||||
println!("Thank you for using Xous!");
|
||||
// println!("Thank you for using Xous!");
|
||||
false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -507,9 +507,9 @@ impl SystemServices {
|
|||
///
|
||||
/// If the current process is not running, or if it's "Running" but has no free contexts
|
||||
pub fn switch_to(&mut self, pid: PID, context: Option<CtxID>) -> Result<(), xous::Error> {
|
||||
println!("KERNEL(?): Getting current process...");
|
||||
// println!("KERNEL(?): Getting current process...");
|
||||
let process = self.get_process_mut(pid)?;
|
||||
println!("KERNEL(?): Current process is {:?}", process);
|
||||
// println!("KERNEL(?): Current process is {:?}", process);
|
||||
|
||||
// Determine which context number to switch to
|
||||
process.state = match process.state {
|
||||
|
|
@ -1139,17 +1139,17 @@ impl SystemServices {
|
|||
/// * **ServerNotFound**: The server queue was full and a free slot could not
|
||||
/// be found.
|
||||
pub fn create_server(&mut self, name: usize) -> Result<SID, xous::Error> {
|
||||
println!(
|
||||
"KERNEL({}): Looking through server list for free server",
|
||||
self.pid.get()
|
||||
);
|
||||
// println!(
|
||||
// "KERNEL({}): Looking through server list for free server",
|
||||
// self.pid.get()
|
||||
// );
|
||||
|
||||
for entry in self.servers.iter_mut() {
|
||||
if entry == &None {
|
||||
println!(
|
||||
"KERNEL({}): Found a free slot for a server -- allocating an entry",
|
||||
self.pid.get()
|
||||
);
|
||||
// println!(
|
||||
// "KERNEL({}): Found a free slot for a server -- allocating an entry",
|
||||
// self.pid.get()
|
||||
// );
|
||||
let pid = self.pid;
|
||||
|
||||
// TODO: Come up with a way to randomize this
|
||||
|
|
|
|||
|
|
@ -79,20 +79,20 @@ fn send_scalar_message() {
|
|||
|
||||
let xous_server = spawn(move || {
|
||||
xous::hosted::set_xous_address(&xous_client_spec);
|
||||
println!("SERVER: Creating server");
|
||||
// println!("SERVER: Creating server");
|
||||
let sid = xous::create_server(0x7884_3123).expect("couldn't create test server");
|
||||
println!("SERVER: Now listening on {:?}", sid);
|
||||
// println!("SERVER: Now listening on {:?}", sid);
|
||||
server_addr_send.send(sid).unwrap();
|
||||
println!("SERVER: Listening for message...");
|
||||
// println!("SERVER: Listening for message...");
|
||||
let msg = xous::receive_message(sid).expect("couldn't receive messages");
|
||||
println!("SERVER: Received message: {:?}", msg);
|
||||
// println!("SERVER: Received message: {:?}", msg);
|
||||
});
|
||||
|
||||
let xous_client = spawn(move || {
|
||||
xous::hosted::set_xous_address(&xous_server_spec);
|
||||
println!("CLIENT: Waiting for server address...");
|
||||
// println!("CLIENT: Waiting for server address...");
|
||||
let sid = server_addr_recv.recv().unwrap();
|
||||
println!("CLIENT: Connecting to server {:?}", sid);
|
||||
// println!("CLIENT: Connecting to server {:?}", sid);
|
||||
let conn = xous::connect(sid).expect("couldn't connect to server");
|
||||
xous::send_message(conn, xous::Message::Scalar(xous::ScalarMessage { id: 1, arg1: 2, arg2: 3, arg3: 4, arg4: 5} )).expect("couldn't send message");
|
||||
});
|
||||
|
|
@ -105,3 +105,69 @@ fn send_scalar_message() {
|
|||
|
||||
main_thread.join().expect("couldn't join kernel process");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_move_message() {
|
||||
let server_spec = "localhost:9997";
|
||||
xous::hosted::set_xous_address(server_spec);
|
||||
let server_addr = server_spec
|
||||
.to_socket_addrs()
|
||||
.expect("invalid server address")
|
||||
.next()
|
||||
.expect("unable to resolve server address");
|
||||
|
||||
// Launch the main thread
|
||||
let server_spec_server = server_spec.clone();
|
||||
let main_thread = spawn(move || {
|
||||
crate::arch::set_listen_address(&server_spec_server);
|
||||
kmain()
|
||||
});
|
||||
|
||||
// Connect to server. This first instance needs to make sure the kernel is listening.
|
||||
// let mut server_conn = None;
|
||||
let mut connected = false;
|
||||
for i in 1..11 {
|
||||
println!("Retrying connection {}/10", i);
|
||||
let res = TcpStream::connect_timeout(&server_addr, Duration::from_millis(200));
|
||||
if res.is_ok() {
|
||||
connected = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Convert the Option<conn> into conn
|
||||
assert!(connected, "unable to connect to server");
|
||||
|
||||
let xous_client_spec = server_spec.to_owned();
|
||||
let xous_server_spec = server_spec.to_owned();
|
||||
|
||||
let (server_addr_send, server_addr_recv) = channel();
|
||||
|
||||
let xous_server = spawn(move || {
|
||||
xous::hosted::set_xous_address(&xous_client_spec);
|
||||
// println!("SERVER: Creating server");
|
||||
let sid = xous::create_server(0x7884_3123).expect("couldn't create test server");
|
||||
// println!("SERVER: Now listening on {:?}", sid);
|
||||
server_addr_send.send(sid).unwrap();
|
||||
// println!("SERVER: Listening for message...");
|
||||
let msg = xous::receive_message(sid).expect("couldn't receive messages");
|
||||
// println!("SERVER: Received message: {:?}", msg);
|
||||
});
|
||||
|
||||
let xous_client = spawn(move || {
|
||||
xous::hosted::set_xous_address(&xous_server_spec);
|
||||
// println!("CLIENT: Waiting for server address...");
|
||||
let sid = server_addr_recv.recv().unwrap();
|
||||
// println!("CLIENT: Connecting to server {:?}", sid);
|
||||
let conn = xous::connect(sid).expect("couldn't connect to server");
|
||||
let msg = xous::carton::Carton::from_bytes(format!("Hello, world!").as_bytes());
|
||||
xous::send_message(conn, xous::Message::Move(msg.into_message(0))).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");
|
||||
}
|
||||
|
|
|
|||
46
xous-rs/src/carton.rs
Normal file
46
xous-rs/src/carton.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
//! 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;
|
||||
extern crate core;
|
||||
use alloc::alloc::{alloc, dealloc, Layout};
|
||||
|
||||
use crate::{MemoryMessage, MemoryRange};
|
||||
|
||||
pub struct Carton {
|
||||
contents: *mut u8,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl Carton {
|
||||
pub fn from_bytes(bytes: &[u8]) -> Self {
|
||||
let src_mem = bytes.as_ptr();
|
||||
let len = bytes.len();
|
||||
let layout = Layout::from_size_align(len, 4096).unwrap();
|
||||
let new_mem = unsafe {
|
||||
let new_mem = alloc(layout);
|
||||
core::ptr::copy(src_mem, new_mem, len);
|
||||
new_mem
|
||||
};
|
||||
Carton {
|
||||
contents: new_mem,
|
||||
size: len,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_message(self, id: usize) -> MemoryMessage {
|
||||
MemoryMessage {
|
||||
id,
|
||||
buf: MemoryRange::new(self.contents as usize, self.size),
|
||||
offset: None,
|
||||
valid: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Carton {
|
||||
fn drop(&mut self) {
|
||||
let layout = Layout::from_size_align(self.size, 4096).unwrap();
|
||||
let ptr = self.contents;
|
||||
unsafe { dealloc(ptr, layout) };
|
||||
}
|
||||
}
|
||||
|
|
@ -69,16 +69,47 @@ fn _xous_syscall_to(
|
|||
ret: &mut Result,
|
||||
xsc: &mut TcpStream,
|
||||
) {
|
||||
println!(
|
||||
"Making Syscall: {:?}",
|
||||
crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
|
||||
);
|
||||
// println!(
|
||||
// "Making Syscall: {:?}",
|
||||
// crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap()
|
||||
// );
|
||||
let call = crate::SysCall::from_args(nr, a1, a2, a3, a4, a5, a6, a7).unwrap();
|
||||
|
||||
// Send the packet to the server
|
||||
let mut pkt = vec![];
|
||||
for word in &[nr, a1, a2, a3, a4, a5, a6, a7] {
|
||||
xsc.write_all(&word.to_le_bytes())
|
||||
.expect("Server shut down");
|
||||
pkt.extend_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
if let crate::SysCall::SendMessage(_, ref msg) = call {
|
||||
match msg {
|
||||
crate::Message::MutableBorrow(crate::MemoryMessage {
|
||||
id: _id,
|
||||
buf,
|
||||
offset: _offset,
|
||||
valid: _valid,
|
||||
})
|
||||
| crate::Message::ImmutableBorrow(crate::MemoryMessage {
|
||||
id: _id,
|
||||
buf,
|
||||
offset: _offset,
|
||||
valid: _valid,
|
||||
})
|
||||
| crate::Message::Move(crate::MemoryMessage {
|
||||
id: _id,
|
||||
buf,
|
||||
offset: _offset,
|
||||
valid: _valid,
|
||||
}) => {
|
||||
use core::slice;
|
||||
let data: &[u8] =
|
||||
unsafe { slice::from_raw_parts(buf.addr.get() as _, buf.size.get()) };
|
||||
pkt.extend_from_slice(data);
|
||||
}
|
||||
crate::Message::Scalar(_) => (),
|
||||
}
|
||||
}
|
||||
|
||||
xsc.write_all(&pkt).expect("Server shut down");
|
||||
|
||||
// Receive the packet back
|
||||
loop {
|
||||
|
|
@ -90,10 +121,48 @@ fn _xous_syscall_to(
|
|||
}
|
||||
|
||||
*ret = Result::from_args(pkt);
|
||||
|
||||
println!(" Response: {:?}", *ret);
|
||||
if Result::BlockedProcess == *ret {
|
||||
// println!(" Waiting again");
|
||||
} else {
|
||||
if let crate::SysCall::SendMessage(_, ref msg) = call {
|
||||
match msg {
|
||||
crate::Message::MutableBorrow(crate::MemoryMessage {
|
||||
id: _id,
|
||||
buf,
|
||||
offset: _offset,
|
||||
valid: _valid,
|
||||
}) => {
|
||||
// Read the buffer back from the remote host.
|
||||
use core::slice;
|
||||
let mut data = unsafe {
|
||||
slice::from_raw_parts_mut(buf.addr.get() as _, buf.size.get())
|
||||
};
|
||||
xsc.read_exact(&mut data).expect("Server shut down");
|
||||
// pkt.extend_from_slice(data);
|
||||
}
|
||||
crate::Message::Move(crate::MemoryMessage {
|
||||
id: _id,
|
||||
buf,
|
||||
offset: _offset,
|
||||
valid: _valid,
|
||||
}) => {
|
||||
let offset = buf.addr.get() as *mut u8;
|
||||
let size = buf.size.get();
|
||||
extern crate alloc;
|
||||
use alloc::alloc::{dealloc, Layout};
|
||||
let layout = Layout::from_size_align(size, 4096).unwrap();
|
||||
// Free memory that was moved
|
||||
unsafe {
|
||||
dealloc(offset, layout);
|
||||
}
|
||||
}
|
||||
// Nothing to do for Immutable borrow, since the memory can't change
|
||||
crate::Message::ImmutableBorrow(_) => (),
|
||||
crate::Message::Scalar(_) => (),
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,12 @@ pub use hosted::*;
|
|||
|
||||
pub mod definitions;
|
||||
pub mod syscall;
|
||||
pub mod carton;
|
||||
mod messages;
|
||||
|
||||
pub use definitions::*;
|
||||
pub use syscall::*;
|
||||
pub use messages::*;
|
||||
|
||||
/// Convert a four-letter string into a 32-bit int.
|
||||
#[macro_export]
|
||||
|
|
|
|||
2
xous-rs/src/messages/mod.rs
Normal file
2
xous-rs/src/messages/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod stringmessage;
|
||||
pub use stringmessage::StringMessage;
|
||||
24
xous-rs/src/messages/stringmessage.rs
Normal file
24
xous-rs/src/messages/stringmessage.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use crate::{MemoryMessage, MemoryRange};
|
||||
|
||||
pub struct StringMessage {
|
||||
backing: *mut u8,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
impl StringMessage {
|
||||
pub fn new(src: &str) -> Self {
|
||||
StringMessage {
|
||||
backing: 0 as _,
|
||||
length: 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_message(self, id: usize) -> MemoryMessage {
|
||||
MemoryMessage {
|
||||
id,
|
||||
buf: MemoryRange::new(self.backing as _, self.length),
|
||||
offset: None,
|
||||
valid: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue