kernel: rework process creation

This now uses a system of access keys to associate processes with the
kernel.

This currently does not pass tests.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-27 21:44:50 +08:00
parent ab7a56937c
commit 7258fc83b7
7 changed files with 267 additions and 176 deletions

View file

@ -16,11 +16,11 @@ use std::time::Duration;
use crate::arch::process::Process; use crate::arch::process::Process;
use crate::services::SystemServices; use crate::services::SystemServices;
use xous::{MemoryAddress, Result, SysCall, PID, TID}; use xous::{MemoryAddress, ProcessInit, Result, SysCall, PID, TID};
enum ThreadMessage { enum ThreadMessage {
SysCall(PID, TID, SysCall), SysCall(PID, TID, SysCall),
NewConnection(TcpStream), NewConnection(TcpStream, [u8; 16] /* access key */),
} }
#[derive(Debug)] #[derive(Debug)]
@ -31,6 +31,12 @@ enum BackchannelMessage {
thread_local!(static NETWORK_LISTEN_ADDRESS: RefCell<SocketAddr> = RefCell::new(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080))); thread_local!(static NETWORK_LISTEN_ADDRESS: RefCell<SocketAddr> = RefCell::new(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)));
thread_local!(static SEND_ADDR: RefCell<Option<Sender<SocketAddr>>> = RefCell::new(None)); thread_local!(static SEND_ADDR: RefCell<Option<Sender<SocketAddr>>> = RefCell::new(None));
thread_local!(static PID1_KEY: RefCell<[u8; 16]> = RefCell::new([0u8; 16]));
#[cfg(test)]
pub fn set_pid1_key(new_key: [u8; 16]) {
PID1_KEY.with(|p1k| *p1k.borrow_mut() = new_key);
}
/// Set the network address for this particular thread. /// Set the network address for this particular thread.
#[cfg(test)] #[cfg(test)]
@ -173,7 +179,6 @@ fn listen_thread(
let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| { let listener = TcpListener::bind(listen_addr).unwrap_or_else(|e| {
panic!("Unable to create server: {}", e); panic!("Unable to create server: {}", e);
}); });
let client_addr = listener.local_addr().unwrap();
// Notify the host what our kernel address is, if a listener exists. // Notify the host what our kernel address is, if a listener exists.
if let Some(las) = local_addr_sender.take() { if let Some(las) = local_addr_sender.take() {
las.send(listener.local_addr().unwrap()).unwrap(); las.send(listener.local_addr().unwrap()).unwrap();
@ -181,29 +186,29 @@ fn listen_thread(
let mut clients = vec![]; let mut clients = vec![];
let pid1_should_exit = should_exit.clone(); // let pid1_should_exit = should_exit.clone();
let pid1_thread = spawn(move || { // let pid1_thread = spawn(move || {
let mut client = TcpStream::connect(client_addr).expect("couldn't connect to xous server"); // let mut client = TcpStream::connect(client_addr).expect("couldn't connect to xous server");
client.set_read_timeout(Some(Duration::from_millis(100))).expect("couldn't set read timeout duration"); // client.set_read_timeout(Some(Duration::from_millis(100))).expect("couldn't set read timeout duration");
let mut buffer = [0; size_of::<usize>() * 9]; // let mut buffer = [0; size_of::<usize>() * 9];
// println!("KERNEL(1): Started PID1 idle thread"); // // println!("KERNEL(1): Started PID1 idle thread");
loop { // loop {
if pid1_should_exit.load(core::sync::atomic::Ordering::Relaxed) { // if pid1_should_exit.load(core::sync::atomic::Ordering::Relaxed) {
return; // return;
} // }
match client.read_exact(&mut buffer) { // match client.read_exact(&mut buffer) {
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => { // Err(e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
continue; // continue;
} // }
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return, // Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return,
Err(e) => panic!("KERNEL(1): Unable to read buffer: {}", e), // Err(e) => panic!("KERNEL(1): Unable to read buffer: {}", e),
Ok(()) => (), //println!("KERNEL(1): Read {} bytes", x), // Ok(()) => (), //println!("KERNEL(1): Read {} bytes", x),
} // }
} // }
}); // });
fn accept_new_connection( fn accept_new_connection(
conn: TcpStream, mut conn: TcpStream,
chn: &Sender<ThreadMessage>, chn: &Sender<ThreadMessage>,
backchannel: &Receiver<BackchannelMessage>, backchannel: &Receiver<BackchannelMessage>,
clients: &mut Vec<(std::thread::JoinHandle<()>, TcpStream)>, clients: &mut Vec<(std::thread::JoinHandle<()>, TcpStream)>,
@ -211,10 +216,15 @@ fn listen_thread(
) -> bool { ) -> bool {
let thr_chn = chn.clone(); let thr_chn = chn.clone();
let mut access_key = [0u8; 16];
conn.read_exact(&mut access_key).unwrap();
// Spawn a new process. This process will start out in the "Setup()" state. // Spawn a new process. This process will start out in the "Setup()" state.
chn.send(ThreadMessage::NewConnection(conn.try_clone().expect( chn.send(ThreadMessage::NewConnection(
"couldn't make a copy of the network connection for the kernel", conn.try_clone()
))) .expect("couldn't make a copy of the network connection for the kernel"),
access_key,
))
.expect("couldn't request a new PID"); .expect("couldn't request a new PID");
let new_pid = match backchannel let new_pid = match backchannel
.recv() .recv()
@ -234,7 +244,6 @@ fn listen_thread(
fn exit_server( fn exit_server(
should_exit: std::sync::Arc<core::sync::atomic::AtomicBool>, should_exit: std::sync::Arc<core::sync::atomic::AtomicBool>,
clients: Vec<(std::thread::JoinHandle<()>, TcpStream)>, clients: Vec<(std::thread::JoinHandle<()>, TcpStream)>,
pid1_thread: std::thread::JoinHandle<()>,
) { ) {
should_exit.store(true, core::sync::atomic::Ordering::Relaxed); should_exit.store(true, core::sync::atomic::Ordering::Relaxed);
for (jh, conn) in clients { for (jh, conn) in clients {
@ -243,50 +252,58 @@ fn listen_thread(
.expect("couldn't shutdown client"); .expect("couldn't shutdown client");
jh.join().expect("couldn't join client thread"); jh.join().expect("couldn't join client thread");
} }
pid1_thread.join().unwrap();
} }
let pid1 = listener.accept().unwrap(); // let pid1 = listener.accept().unwrap();
if accept_new_connection(pid1.0, &chn, &backchannel, &mut clients, &should_exit) { // if accept_new_connection(pid1.0, &chn, &backchannel, &mut clients, &should_exit) {
exit_server(should_exit, clients, pid1_thread); // exit_server(should_exit, clients);
return; // return;
} // }
// Use `listener` in a nonblocking setup so that we can exit when doing tests // Use `listener` in a nonblocking setup so that we can exit when doing tests
listener enum ClientMessage {
.set_nonblocking(true) NewConnection(TcpStream),
.expect("couldn't set TcpListener to nonblocking"); // BackChannel(BackchannelMessage),
loop { };
let (sender, receiver) = channel();
let tcp_sender = sender.clone();
spawn(move || loop {
match listener.accept() { match listener.accept() {
Ok((conn, _addr)) => { Ok((conn, _addr)) => {
if accept_new_connection(conn, &chn, &backchannel, &mut clients, &should_exit) { tcp_sender.send(ClientMessage::NewConnection(conn)).unwrap();
exit_server(should_exit, clients, pid1_thread);
return;
}
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
match backchannel.recv_timeout(Duration::from_millis(1)) {
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
continue;
}
Err(e) => panic!(
"KERNEL: got error when trying to receive quit timeout: {:?} ({})",
e, e
),
Ok(BackchannelMessage::NewPid(x)) => {
panic!("got unexpected message from main thread: new pid {}", x)
}
Ok(BackchannelMessage::Exit) => {
exit_server(should_exit, clients, pid1_thread);
return;
}
}
} }
Err(e) => { Err(e) => {
println!("error accepting connections: {}", e); println!("error accepting connections: {}", e);
return; return;
} }
} }
});
// spawn(move || loop {
// match backchannel.recv() {
// Err(e) => panic!(
// "KERNEL: got error when trying to receive quit timeout: {:?} ({})",
// e, e
// ),
// Ok(msg) => sender.send(ClientMessage::BackChannel(msg)).unwrap(),
// }
// });
for msg in receiver {
match msg {
ClientMessage::NewConnection(conn) => {
if accept_new_connection(conn, &chn, &backchannel, &mut clients, &should_exit) {
exit_server(should_exit, clients);
return;
}
}
// ClientMessage::BackChannel(BackchannelMessage::NewPid(x)) => {
// panic!("got unexpected message from main thread: new pid {}", x)
// }
// ClientMessage::BackChannel(BackchannelMessage::Exit) => {
// exit_server(should_exit, clients);
// return;
// }
}
} }
} }
@ -298,6 +315,12 @@ pub fn idle() -> bool {
let (sender, receiver) = channel(); let (sender, receiver) = channel();
let (backchannel_sender, backchannel_receiver) = channel(); let (backchannel_sender, backchannel_receiver) = channel();
// Allocate PID1 with the key we were passed.
let pid1_key = PID1_KEY.with(|p1k| p1k.borrow().clone());
let pid1_init = ProcessInit { key: pid1_key };
let new_pid = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap();
assert_eq!(new_pid.get(), 1);
let listen_addr = env::var("XOUS_LISTEN_ADDR") let listen_addr = env::var("XOUS_LISTEN_ADDR")
.map(|s| { .map(|s| {
s.to_socket_addrs() s.to_socket_addrs()
@ -314,28 +337,39 @@ pub fn idle() -> bool {
while let Ok(msg) = receiver.recv() { while let Ok(msg) = receiver.recv() {
match msg { match msg {
ThreadMessage::NewConnection(mut conn) => { ThreadMessage::NewConnection(conn, access_key) => {
// TODO: Figure out how to set the parent PID // The new process should already have a PID registered. Convert its access key
crate::arch::process::set_current_pid(PID::new(1).unwrap()); // into a PID, and register the connection with the server.
println!("KERNEL: Attempting to look up access key {:?}", access_key);
let new_pid =
crate::arch::process::register_connection_for_key(conn, access_key).unwrap();
println!(
"KERNEL: Access key {:?} mapped to PID {}",
access_key, new_pid
);
// Spawn a new process inside the kernel. This will assign us a PID. // // On the initial connection, set the parent PID to 1. In the future
let new_pid = SystemServices::with_mut(|ss| { // // there may be some sort of security token check here.
ss.create_process(process::ProcessInit::new(conn.try_clone().unwrap()), ()) // crate::arch::process::set_current_pid(PID::new(1).unwrap());
})
.unwrap(); // // Spawn a new process inside the kernel. This will assign us a PID.
// let new_pid = SystemServices::with_mut(|ss| {
// ss.create_process(process::ProcessInit::new(conn.try_clone().unwrap()), ())
// })
// .unwrap();
// Inform the backchannel of the new process ID. // Inform the backchannel of the new process ID.
backchannel_sender backchannel_sender
.send(BackchannelMessage::NewPid(new_pid)) .send(BackchannelMessage::NewPid(new_pid))
.expect("couldn't send new pid to new connection"); .expect("couldn't send new pid to new connection");
conn.write_all(&new_pid.get().to_le_bytes()).expect("couldn't send pid to new process"); // conn.write_all(&new_pid.get().to_le_bytes())
// .expect("couldn't send pid to new process");
// Switch to this process immediately, which moves it from `Setup(_)` to `Running(0)`. // Switch to this process immediately, which moves it from `Setup(_)` to `Running(0)`.
// Note that in this system, multiple processes can be active at once. This is // Note that in this system, multiple processes can be active at once. This is
// similar to having one core for each process. However, each process may only have // similar to having one core for each process
// one context running at a time. // SystemServices::with_mut(|ss| ss.switch_to_thread(new_pid, Some(1))).unwrap();
SystemServices::with_mut(|ss| ss.switch_to_thread(new_pid, Some(1))).unwrap();
} }
ThreadMessage::SysCall(pid, thread_id, call) => { ThreadMessage::SysCall(pid, thread_id, call) => {
// println!("KERNEL({}): Received syscall {:?}", pid, call); // println!("KERNEL({}): Received syscall {:?}", pid, call);

View file

@ -4,7 +4,7 @@ use core::cell::RefCell;
use std::io::Write; use std::io::Write;
use std::net::TcpStream; use std::net::TcpStream;
use std::thread_local; use std::thread_local;
use xous::{ThreadInit, PID, TID}; use xous::{ProcessInit, ThreadInit, PID, TID};
pub const INITIAL_TID: usize = 1; pub const INITIAL_TID: usize = 1;
@ -16,8 +16,11 @@ struct ProcessImpl {
/// Global parameters used by the operating system /// Global parameters used by the operating system
pub inner: ProcessInner, pub inner: ProcessInner,
/// A 16-byte key used to register a process when it first starts
key: [u8; 16],
/// The network connection to the client process. /// The network connection to the client process.
conn: TcpStream, conn: Option<TcpStream>,
/// Memory that may need to be returned to the caller /// Memory that may need to be returned to the caller
memory_to_return: Option<Vec<u8>>, memory_to_return: Option<Vec<u8>>,
@ -57,6 +60,21 @@ pub fn set_current_pid(pid: PID) {
PROCESS_TABLE.with(|pt| (*pt.borrow_mut()).current = pid); PROCESS_TABLE.with(|pt| (*pt.borrow_mut()).current = pid);
} }
pub fn register_connection_for_key(conn: TcpStream, key: [u8; 16]) -> Result<PID, xous::Error> {
PROCESS_TABLE.with(|pt| {
let mut process_table = pt.borrow_mut();
for (pid_minus_1, process) in process_table.table.iter_mut().enumerate() {
if let Some(process) = process.as_mut() {
if process.key == key && process.conn.is_none() {
process.conn = Some(conn);
return Ok(PID::new(pid_minus_1 as u8 + 1).unwrap());
}
}
}
Err(xous::Error::ProcessNotFound)
})
}
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
/// Everything required to keep track of a single thread of execution. /// Everything required to keep track of a single thread of execution.
@ -71,17 +89,17 @@ impl Default for Thread {
} }
} }
/// Everything required to initialize a process on this platform // /// Everything required to initialize a process on this platform
pub struct ProcessInit { // pub struct ProcessInit {
/// A network connection to the client // /// A network connection to the client
conn: TcpStream, // conn: TcpStream,
} // }
impl ProcessInit { // impl ProcessInit {
pub fn new(conn: TcpStream) -> ProcessInit { // pub fn new(conn: TcpStream) -> ProcessInit {
ProcessInit { conn } // ProcessInit { conn }
} // }
} // }
impl Process { impl Process {
pub fn current() -> Process { pub fn current() -> Process {
@ -171,7 +189,11 @@ impl Process {
if !process.threads[thread - 1].allocated { if !process.threads[thread - 1].allocated {
::debug_here::debug_here!(); ::debug_here::debug_here!();
} }
assert!(process.threads[thread - 1].allocated, "tried to switch to thread {} which wasn't allocated", thread); assert!(
process.threads[thread - 1].allocated,
"tried to switch to thread {} which wasn't allocated",
thread
);
process.current_thread = thread; process.current_thread = thread;
}); });
Ok(()) Ok(())
@ -214,7 +236,12 @@ impl Process {
response.extend_from_slice(&buf); response.extend_from_slice(&buf);
} }
process.conn.write_all(&response).expect("Disconnection"); process
.conn
.as_mut()
.unwrap()
.write_all(&response)
.expect("Disconnection");
}); });
} }
@ -238,7 +265,8 @@ impl Process {
let process = ProcessImpl { let process = ProcessImpl {
inner: Default::default(), inner: Default::default(),
conn: init_data.conn, conn: None,
key: init_data.key,
memory_to_return: None, memory_to_return: None,
current_thread: INITIAL_TID, current_thread: INITIAL_TID,
threads: [Thread { allocated: false }; MAX_THREAD], threads: [Thread { allocated: false }; MAX_THREAD],
@ -263,7 +291,12 @@ impl Process {
panic!("attempted to destroy PID that exceeds table index: {}", pid); panic!("attempted to destroy PID that exceeds table index: {}", pid);
} }
let process = process_table.table[pid_idx].as_mut().unwrap(); let process = process_table.table[pid_idx].as_mut().unwrap();
process.conn.shutdown(std::net::Shutdown::Both).unwrap(); process
.conn
.as_mut()
.unwrap()
.shutdown(std::net::Shutdown::Both)
.unwrap();
process_table.table[pid_idx] = None; process_table.table[pid_idx] = None;
Ok(()) Ok(())
}) })
@ -274,7 +307,7 @@ impl Process {
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();
process.conn.write_all(bytes).unwrap(); process.conn.as_mut().unwrap().write_all(bytes).unwrap();
}); });
Ok(()) Ok(())
} }

View file

@ -1,7 +1,7 @@
use crate::arch; use crate::arch;
use crate::arch::mem::MemoryMapping; use crate::arch::mem::MemoryMapping;
pub use crate::arch::process::Process as ArchProcess; pub use crate::arch::process::Process as ArchProcess;
pub use crate::arch::process::{Thread, ProcessInit}; pub use crate::arch::process::Thread;
use core::cell::RefCell; use core::cell::RefCell;
use std::thread_local; use std::thread_local;
@ -10,7 +10,9 @@ 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, ThreadInit, Error, MemoryAddress, Message, CID, PID, SID, TID}; use xous::{
pid_from_usize, Error, MemoryAddress, Message, ProcessInit, ThreadInit, CID, PID, SID, TID,
};
const MAX_PROCESS_COUNT: usize = 32; const MAX_PROCESS_COUNT: usize = 32;
const MAX_SERVER_COUNT: usize = 32; const MAX_SERVER_COUNT: usize = 32;
@ -41,6 +43,9 @@ pub enum ProcessState {
/// This is an unallocated, free process /// This is an unallocated, free process
Free, Free,
/// This process has been allocated, but has no threads yet
Allocated,
/// This is a brand-new process that hasn't been run yet, and needs its /// This is a brand-new process that hasn't been run yet, and needs its
/// initial context set up. /// initial context set up.
Setup(ThreadInit), Setup(ThreadInit),
@ -287,11 +292,7 @@ impl SystemServices {
/// Add a new entry to the process table. This results in a new address space /// Add a new entry to the process table. This results in a new address space
/// and a new PID, though the process is in the state `Setup()`. /// and a new PID, though the process is in the state `Setup()`.
pub fn create_process( pub fn create_process(&mut self, init_process: ProcessInit) -> Result<PID, xous::Error> {
&mut self,
init_process: ProcessInit,
init_context: ThreadInit,
) -> Result<PID, xous::Error> {
for (idx, mut entry) in self.processes.iter_mut().enumerate() { for (idx, mut entry) in self.processes.iter_mut().enumerate() {
if entry.state != ProcessState::Free { if entry.state != ProcessState::Free {
continue; continue;
@ -301,7 +302,7 @@ impl SystemServices {
// #[allow(clippy::unit_arg)] // #[allow(clippy::unit_arg)]
let ppid = crate::arch::process::current_pid(); let ppid = crate::arch::process::current_pid();
// println!("Creating new process for PID {} with PPID {}", new_pid, ppid); // println!("Creating new process for PID {} with PPID {}", new_pid, ppid);
entry.state = ProcessState::Setup(init_context); entry.state = ProcessState::Allocated;
entry.ppid = ppid; entry.ppid = ppid;
entry.pid = new_pid; entry.pid = new_pid;
return Ok(new_pid); return Ok(new_pid);
@ -364,11 +365,7 @@ impl SystemServices {
/// 2. Save the process state, if it hasn't already been saved /// 2. Save the process state, if it hasn't already been saved
/// 3. Run the new process, returning to an illegal instruction /// 3. Run the new process, returning to an illegal instruction
#[allow(dead_code)] #[allow(dead_code)]
pub fn finish_callback_and_resume( pub fn finish_callback_and_resume(&mut self, pid: PID, tid: TID) -> Result<(), xous::Error> {
&mut self,
pid: PID,
tid: TID,
) -> Result<(), xous::Error> {
// Get the current process (which was the interrupt handler) and mark it // Get the current process (which was the interrupt handler) and mark it
// as Ready. Note that the new PID may very well be the same PID. // as Ready. Note that the new PID may very well be the same PID.
{ {
@ -521,6 +518,7 @@ impl SystemServices {
process.state = match process.state { process.state = match process.state {
ProcessState::Free => return Err(xous::Error::ProcessNotFound), ProcessState::Free => return Err(xous::Error::ProcessNotFound),
ProcessState::Sleeping => return Err(xous::Error::ProcessNotFound), ProcessState::Sleeping => return Err(xous::Error::ProcessNotFound),
ProcessState::Allocated => return Err(xous::Error::ProcessNotFound),
ProcessState::Setup(setup) => { ProcessState::Setup(setup) => {
// Activate the process, which enables its memory mapping // Activate the process, which enables its memory mapping
process.activate()?; process.activate()?;
@ -601,7 +599,9 @@ impl SystemServices {
Some(ctx) => { Some(ctx) => {
// Ensure the specified context is ready to run, or is // Ensure the specified context is ready to run, or is
// currently running. // currently running.
if ready_threads & (1 << ctx) == 0 /*&& ctx != current_thread*/ { if ready_threads & (1 << ctx) == 0
/*&& ctx != current_thread*/
{
return Err(xous::Error::InvalidContext); return Err(xous::Error::InvalidContext);
} }
ctx ctx
@ -671,7 +671,7 @@ impl SystemServices {
pub fn thread_is_running(&self, pid: PID, tid: TID) -> bool { pub fn thread_is_running(&self, pid: PID, tid: TID) -> bool {
let process = self.get_process(pid).unwrap(); let process = self.get_process(pid).unwrap();
if let ProcessState::Running(thread_ids) = process.state { if let ProcessState::Running(thread_ids) = process.state {
if thread_ids & (1 << tid) == 0 { if thread_ids & (1 << tid) == 0 {
return true; return true;
} }
@ -735,7 +735,7 @@ impl SystemServices {
// Ensure the new process can be run. // Ensure the new process can be run.
match new.state { match new.state {
ProcessState::Free => return Err(xous::Error::ProcessNotFound), ProcessState::Free => return Err(xous::Error::ProcessNotFound),
ProcessState::Setup(_) => new_tid = INITIAL_TID, ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID,
ProcessState::Running(x) | ProcessState::Ready(x) => { ProcessState::Running(x) | ProcessState::Ready(x) => {
// If no new context is specified, take the previous // If no new context is specified, take the previous
// context. If that is not runnable, do a round-robin // context. If that is not runnable, do a round-robin
@ -750,14 +750,14 @@ impl SystemServices {
// "PID {}: Looking for a valid context in the mask {:08b}, curent context {} ({:08b})", // "PID {}: Looking for a valid context in the mask {:08b}, curent context {} ({:08b})",
// new_pid, x, new.current_context, new.current_context // new_pid, x, new.current_context, new.current_context
// ); // );
new_tid = 0;//new.current_thread as usize; new_tid = 0; //new.current_thread as usize;
while x & (1 << new_tid) == 0 { while x & (1 << new_tid) == 0 {
new_tid += 1; new_tid += 1;
if new_tid > arch::process::MAX_THREAD { if new_tid > arch::process::MAX_THREAD {
// new_tid = 0; // new_tid = 0;
// } // }
// // If we've looped around, return an error. // // If we've looped around, return an error.
// if new_tid == new.current_thread as usize { // if new_tid == new.current_thread as usize {
println!("Looked through all contexts and couldn't find one that was ready"); println!("Looked through all contexts and couldn't find one that was ready");
return Err(xous::Error::ProcessNotFound); return Err(xous::Error::ProcessNotFound);
} }
@ -802,6 +802,7 @@ impl SystemServices {
// .expect("couldn't reserve stack"); // .expect("couldn't reserve stack");
ProcessState::Running(0) ProcessState::Running(0)
} }
ProcessState::Allocated => ProcessState::Running(0),
ProcessState::Free => panic!("process was suddenly Free"), ProcessState::Free => panic!("process was suddenly Free"),
ProcessState::Ready(x) | ProcessState::Running(x) => { ProcessState::Ready(x) | ProcessState::Running(x) => {
ProcessState::Running(x & !(1 << new_tid)) ProcessState::Running(x & !(1 << new_tid))
@ -823,7 +824,7 @@ impl SystemServices {
// if can_resume { // if can_resume {
// ProcessState::Ready(1 << previous_tid) // ProcessState::Ready(1 << previous_tid)
// } else { // } else {
ProcessState::Sleeping ProcessState::Sleeping
// } // }
} }
// Otherwise, there are additional threads that can be run. // Otherwise, there are additional threads that can be run.
@ -833,7 +834,7 @@ impl SystemServices {
// if can_resume { // if can_resume {
// ProcessState::Ready(x | (1 << previous_tid)) // ProcessState::Ready(x | (1 << previous_tid))
// } else { // } else {
ProcessState::Ready(x) ProcessState::Ready(x)
// } // }
} }
other => panic!( other => panic!(
@ -858,23 +859,25 @@ impl SystemServices {
// } // }
// return Ok(new_tid); // return Ok(new_tid);
// } // }
let new = self.get_process_mut(new_pid)?;/* let new = self.get_process_mut(new_pid)?; /*
new.state = match new.state { new.state = match new.state {
ProcessState::Running(x) if (x & 1 << new_tid) == 0 => { ProcessState::Running(x) if (x & 1 << new_tid) == 0 => {
return Err(xous::Error::ProcessNotFound) return Err(xous::Error::ProcessNotFound)
} }
// ProcessState::Running(x) => { // ProcessState::Running(x) => {
// if can_resume { // if can_resume {
// ProcessState::Running((x | (1 << previous_tid)) & !(1 << new_tid)) // ProcessState::Running((x | (1 << previous_tid)) & !(1 << new_tid))
// } else { // } else {
// ProcessState::Running(x | (1 << previous_tid)) // ProcessState::Running(x | (1 << previous_tid))
// } // }
// } // }
other => */panic!( other => */
"PID {} invalid process state (not Running): {:?}", panic!(
previous_pid, new.state "PID {} invalid process state (not Running): {:?}",
)/*, previous_pid, new.state
}*/; ) /*,
}*/
;
// if advance_thread { // if advance_thread {
// new.current_thread += 1; // new.current_thread += 1;
// if new.current_thread as TID > arch::process::MAX_CONTEXT { // if new.current_thread as TID > arch::process::MAX_CONTEXT {
@ -1176,11 +1179,7 @@ impl SystemServices {
/// ///
/// * **ContextNotAvailable**: The process has used all of its context /// * **ContextNotAvailable**: The process has used all of its context
/// slots. /// slots.
pub fn create_thread( pub fn create_thread(&mut self, pid: PID, thread_init: ThreadInit) -> Result<TID, xous::Error> {
&mut self,
pid: PID,
thread_init: ThreadInit,
) -> Result<TID, xous::Error> {
let mut process = self let mut process = self
.get_process_mut(pid) .get_process_mut(pid)
.expect("couldn't find current process"); .expect("couldn't find current process");

View file

@ -508,14 +508,8 @@ pub fn handle_inner(pid: PID, tid: TID, call: SysCall) -> SysCallResult {
}) })
}), }),
SysCall::CreateProcess(process_init) => SystemServices::with_mut(|ss| { SysCall::CreateProcess(process_init) => SystemServices::with_mut(|ss| {
#[allow(clippy::unit_arg)] ss.create_process(process_init)
ss.create_process(process_init).map(|new_tid| { .map(|new_pid| xous::Result::ProcessID(new_pid))
if !cfg!(baremetal) {
ss.switch_to_thread(pid, Some(new_tid))
.expect("couldn't activate new thread");
}
xous::Result::ProcessID(new_tid)
})
}), }),
SysCall::CreateServer(name) => { SysCall::CreateServer(name) => {
SystemServices::with_mut(|ss| ss.create_server(pid, name).map(xous::Result::ServerID)) SystemServices::with_mut(|ss| ss.create_server(pid, name).map(xous::Result::ServerID))

View file

@ -415,7 +415,7 @@ fn server_client_same_process() {
// Start the kernel in its own thread // Start the kernel in its own thread
let main_thread = start_kernel(SERVER_SPEC); let main_thread = start_kernel(SERVER_SPEC);
let internal_server = xous::create_process(xous::arch::ProcessArgs::new(Box::new(|| { let internal_server = xous::create_process(xous::arch::ProcessArgs::new(|| {
let server = xous::create_server(b"s_c_same_process").expect("couldn't create server"); let server = xous::create_server(b"s_c_same_process").expect("couldn't create server");
let connection = xous::connect(server).expect("couldn't connect to our own server"); let connection = xous::connect(server).expect("couldn't connect to our own server");
let msg_contents = xous::ScalarMessage { let msg_contents = xous::ScalarMessage {
@ -432,7 +432,7 @@ fn server_client_same_process() {
let msg = xous::receive_message(server).expect("couldn't receive message"); let msg = xous::receive_message(server).expect("couldn't receive message");
assert_eq!(msg.message, xous::Message::Scalar(msg_contents)); assert_eq!(msg.message, xous::Message::Scalar(msg_contents));
}))).expect("couldn't start server"); })).expect("couldn't start server");
xous::wait_process(internal_server) xous::wait_process(internal_server)

View file

@ -1,5 +1,6 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::mem::size_of; use std::mem::size_of;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream, ToSocketAddrs}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream, ToSocketAddrs};
@ -9,7 +10,11 @@ use std::thread_local;
use crate::{Result, PID, TID}; use crate::{Result, PID, TID};
pub type ThreadInit = (); pub type ThreadInit = ();
pub type ProcessInit = ();
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ProcessInit {
pub key: [u8; 16],
}
pub struct ProcessArgs<F: FnOnce()> { pub struct ProcessArgs<F: FnOnce()> {
main: Option<F>, main: Option<F>,
@ -38,8 +43,17 @@ pub fn thread_to_args(call: usize, _init: &ThreadInit) -> [usize; 8] {
[call, 0, 0, 0, 0, 0, 0, 0] [call, 0, 0, 0, 0, 0, 0, 0]
} }
pub fn process_to_args(call: usize, _init: &ProcessInit) -> [usize; 8] { pub fn process_to_args(call: usize, init: &ProcessInit) -> [usize; 8] {
[call, 0, 0, 0, 0, 0, 0, 0] [
call,
u32::from_le_bytes(init.key[0..4].try_into().unwrap()) as _,
u32::from_le_bytes(init.key[4..8].try_into().unwrap()) as _,
u32::from_le_bytes(init.key[8..12].try_into().unwrap()) as _,
u32::from_le_bytes(init.key[12..16].try_into().unwrap()) as _,
0,
0,
0,
]
} }
pub fn args_to_thread( pub fn args_to_thread(
@ -55,15 +69,24 @@ pub fn args_to_thread(
} }
pub fn args_to_process( pub fn args_to_process(
_a1: usize, a1: usize,
_a2: usize, a2: usize,
_a3: usize, a3: usize,
_a4: usize, a4: usize,
_a5: usize, _a5: usize,
_a6: usize, _a6: usize,
_a7: usize, _a7: usize,
) -> core::result::Result<ProcessInit, crate::Error> { ) -> core::result::Result<ProcessInit, crate::Error> {
Ok(()) let mut v = vec![];
v.extend_from_slice(&(a1 as u32).to_le_bytes());
v.extend_from_slice(&(a2 as u32).to_le_bytes());
v.extend_from_slice(&(a3 as u32).to_le_bytes());
v.extend_from_slice(&(a4 as u32).to_le_bytes());
let mut key = [0u8; 16];
key.copy_from_slice(&v);
Ok(ProcessInit {
key
})
} }
thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell<Option<SocketAddr>> = RefCell::new(None)); thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell<Option<SocketAddr>> = RefCell::new(None));
@ -163,28 +186,34 @@ where
F: FnOnce(), F: FnOnce(),
{ {
// Ensure there is a connection, because after this function returns // Ensure there is a connection, because after this function returns
// we'll make a syscall with CreateProcess(). // we'll make a syscall with CreateProcess(). This should only happen
// for PID1.
XOUS_SERVER_CONNECTION.with(|xsc| { XOUS_SERVER_CONNECTION.with(|xsc| {
let mut xsc = xsc.borrow_mut(); let mut xsc = xsc.borrow_mut();
if xsc.is_none() { if xsc.is_none() {
NETWORK_CONNECT_ADDRESS.with(|nca| { NETWORK_CONNECT_ADDRESS.with(|nca| {
let addr = nca.borrow().unwrap_or_else(default_xous_address); let addr = nca.borrow().unwrap_or_else(default_xous_address);
match xous_connect_impl(addr) { let pid1_key = [0u8; 16];
Ok(a) => *xsc = Some(a), match xous_connect_impl(addr, &pid1_key) {
Err(_) => { Ok(a) => {
return Err(crate::Error::InternalError); *xsc = Some(a);
Ok(())
} }
Err(_) => Err(crate::Error::InternalError),
} }
Ok(())
}) })
} else { } else {
Ok(()) Ok(())
} }
})?;
Ok(ProcessInit {
key: [1; 16],
}) })
} }
pub fn create_process_post<F>( pub fn create_process_post<F>(
mut args: ProcessArgs<F>, mut args: ProcessArgs<F>,
init: ProcessInit,
pid: PID, pid: PID,
) -> core::result::Result<ProcessHandle, crate::Error> ) -> core::result::Result<ProcessHandle, crate::Error>
where where
@ -195,8 +224,8 @@ where
Ok(std::thread::Builder::new() Ok(std::thread::Builder::new()
.spawn(move || { .spawn(move || {
set_xous_address(server_spec); set_xous_address(server_spec);
xous_connect_impl(server_spec).unwrap(); xous_connect_impl(server_spec, &init.key).unwrap();
PROCESS_ID.with(|p| *p.borrow_mut() = pid.unwrap()); PROCESS_ID.with(|p| *p.borrow_mut() = pid);
(args.main.take().unwrap())(); (args.main.take().unwrap())();
}) })
.map(ProcessHandle) .map(ProcessHandle)
@ -211,12 +240,13 @@ pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult {
.map_err(|_| crate::Error::InternalError) .map_err(|_| crate::Error::InternalError)
} }
fn xous_connect_impl(addr: SocketAddr) -> core::result::Result<ServerConnection, ()> { fn xous_connect_impl(addr: SocketAddr, key: &[u8; 16]) -> core::result::Result<ServerConnection, ()> {
// println!("Opening connection to Xous server @ {}...", addr); // println!("Opening connection to Xous server @ {}...", addr);
match TcpStream::connect(addr) { match TcpStream::connect(addr) {
Ok(mut conn) => { Ok(mut conn) => {
let mut pid = [0u8; 1]; // let mut pid = [0u8; 1];
// conn.read_exact(&mut pid).unwrap(); // conn.read_exact(&mut pid).unwrap();
conn.write_all(key).unwrap(); // Send key to authenticate us as PID 1
Ok(ServerConnection { Ok(ServerConnection {
send: Arc::new(Mutex::new(conn.try_clone().unwrap())), send: Arc::new(Mutex::new(conn.try_clone().unwrap())),
recv: Arc::new(Mutex::new(conn)), recv: Arc::new(Mutex::new(conn)),
@ -250,9 +280,8 @@ pub fn _xous_syscall(
THREAD_ID.with(|tid| { THREAD_ID.with(|tid| {
let call = 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();
if xsc.borrow().is_none() { let mut xsc_borrowed = xsc.borrow_mut();
panic!("Not connected to server!"); let xsc_asmut = xsc_borrowed.as_mut().expect("not connected to server!");
}
_xous_syscall_to( _xous_syscall_to(
nr, nr,
a1, a1,
@ -263,13 +292,13 @@ pub fn _xous_syscall(
a6, a6,
a7, a7,
&call, &call,
&mut xsc.borrow_mut().as_mut().unwrap().send.lock().unwrap(), &mut xsc_asmut.send.lock().unwrap(),
); );
_xous_syscall_result( _xous_syscall_result(
&call, &call,
ret, ret,
*tid.borrow(), *tid.borrow(),
xsc.borrow_mut().as_mut().unwrap(), xsc_asmut,
); );
}) })
}); });
@ -321,7 +350,6 @@ fn _xous_syscall_result(
let mut pkt = [0usize; 8]; let mut pkt = [0usize; 8];
let mut raw_bytes = [0u8; size_of::<usize>() * 9]; let mut raw_bytes = [0u8; size_of::<usize>() * 9];
stream.read_exact(&mut raw_bytes).expect("Server shut down"); stream.read_exact(&mut raw_bytes).expect("Server shut down");
use std::convert::TryInto;
let mut raw_bytes_chunks = raw_bytes.chunks(size_of::<usize>()); let mut raw_bytes_chunks = raw_bytes.chunks(size_of::<usize>());

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
pid_from_usize, CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange, pid_from_usize, CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange,
MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessInit, Result, MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, ProcessInit,
ScalarMessage, SysCallResult, ThreadInit, ProcessArgs, CID, PID, SID, Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID,
}; };
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -617,7 +617,7 @@ impl SysCall {
} }
Some(SysCallNumber::CreateProcess) => { Some(SysCallNumber::CreateProcess) => {
SysCall::CreateProcess(crate::arch::args_to_process(a1, a2, a3, a4, a5, a6, a7)?) SysCall::CreateProcess(crate::arch::args_to_process(a1, a2, a3, a4, a5, a6, a7)?)
}, }
Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess, Some(SysCallNumber::TerminateProcess) => SysCall::TerminateProcess,
Some(SysCallNumber::Shutdown) => SysCall::Shutdown, Some(SysCallNumber::Shutdown) => SysCall::Shutdown,
Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7),
@ -834,14 +834,17 @@ pub fn wait_thread<T>(joiner: crate::arch::WaitHandle<T>) -> SysCallResult {
crate::arch::wait_thread(joiner) crate::arch::wait_thread(joiner)
} }
/// Create a new process by ru /// Create a new process by running it in its own thread
pub fn create_process<F>(args: ProcessArgs<F>) -> core::result::Result<crate::arch::ProcessHandle, Error> pub fn create_process<F>(
where F: FnOnce() + Send + 'static args: ProcessArgs<F>,
{ ) -> core::result::Result<crate::arch::ProcessHandle, Error>
where
F: FnOnce() + Send + 'static,
{
let process_init = crate::arch::create_process_pre(&args)?; let process_init = crate::arch::create_process_pre(&args)?;
rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| {
if let Result::ProcessID(pid) = result { if let Result::ProcessID(pid) = result {
crate::arch::create_process_post(args, pid) crate::arch::create_process_post(args, process_init, pid)
} else { } else {
Err(Error::InternalError) Err(Error::InternalError)
} }