From e7e8a2b748fab9dd1ec1e0eb9e241ec61cda1c6c Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 29 Jul 2020 17:52:01 +0800 Subject: [PATCH] kernel: get initial hosted non-test processes working This now has the ability to fork off processes. Signed-off-by: Sean Cross --- Cargo.lock | 8 ++++ examples/log/src/main.rs | 2 +- kernel/Cargo.toml | 1 + kernel/src/arch/hosted.rs | 51 ++++++++++++++++---- kernel/src/arch/hosted/process.rs | 36 ++++++++++++-- kernel/src/services.rs | 1 - xous-rs/Cargo.toml | 6 +-- xous-rs/src/arch/hosted.rs | 79 +++++++++++++++++++++---------- xous-rs/src/lib.rs | 2 +- 9 files changed, 141 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aaf16a6dc..63c768a81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,6 +273,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" + [[package]] name = "humantime" version = "1.3.0" @@ -300,6 +306,7 @@ version = "0.1.0" dependencies = [ "bitflags", "debug-here", + "hex", "lazy_static", "rand", "riscv", @@ -751,6 +758,7 @@ name = "xous" version = "0.1.0" dependencies = [ "bitflags", + "hex", "lazy_static", "num-derive", "num-traits", diff --git a/examples/log/src/main.rs b/examples/log/src/main.rs index cb9be7f31..904387b92 100644 --- a/examples/log/src/main.rs +++ b/examples/log/src/main.rs @@ -272,7 +272,7 @@ fn reader_thread(output: &mut implementation::OutputWriter) { fn main() -> Result<(), xous::Error> { let mut output = implementation::init(); let mut writer = output.get_writer(); - + xous::arch::ensure_connection().unwrap(); xous::create_thread(move || reader_thread(&mut writer)).unwrap(); output.run(); diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index ef49e1b9b..10870ef13 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -30,3 +30,4 @@ report-memory = ["stats_alloc"] [target.'cfg(any(windows, unix))'.dependencies] rand = "0.7" +hex = "0.4" diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index 0c2c009f8..46d8bbd69 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -14,11 +14,11 @@ use std::thread_local; use crate::arch::process::Process; use crate::services::SystemServices; -use xous::{MemoryAddress, ProcessInit, Result, SysCall, PID, TID}; +use xous::{MemoryAddress, ProcessInit, ProcessKey, Result, SysCall, PID, TID}; enum ThreadMessage { SysCall(PID, TID, SysCall), - NewConnection(TcpStream, [u8; 16] /* access key */), + NewConnection(TcpStream, ProcessKey), } #[derive(Debug)] @@ -56,6 +56,16 @@ pub fn set_send_addr(send_addr: Sender) { }); } +fn generate_pid_key() -> [u8; 16] { + use rand::{thread_rng, Rng}; + let mut process_key = [0u8; 16]; + let mut rng = thread_rng(); + for b in process_key.iter_mut() { + *b = rng.gen(); + } + process_key +} + /// Each client gets its own connection and its own thread, which is handled here. fn handle_connection( conn: TcpStream, @@ -255,7 +265,7 @@ fn listen_thread( chn.send(ThreadMessage::NewConnection( conn.try_clone() .expect("couldn't make a copy of the network connection for the kernel"), - access_key, + ProcessKey::new(access_key), )) .expect("couldn't request a new PID"); let NewPidMessage::NewPid(new_pid) = new_pid_channel @@ -308,11 +318,13 @@ fn listen_thread( tcp_sender.send(ClientMessage::NewConnection(conn)).unwrap(); } Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { - match shutdown_listener_receiver.recv_timeout(std::time::Duration::from_millis(500)) { + match shutdown_listener_receiver + .recv_timeout(std::time::Duration::from_millis(500)) + { Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue, Ok(()) | Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { return; - }, + } } } Err(e) => { @@ -372,9 +384,9 @@ pub fn idle() -> bool { // Allocate PID1 with the key we were passed. let pid1_key = PID1_KEY.with(|p1k| *p1k.borrow()); - 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 pid1_init = ProcessInit { key: ProcessKey::new(pid1_key) }; + let pid1 = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap(); + assert_eq!(pid1.get(), 1); let listen_addr = env::var("XOUS_LISTEN_ADDR") .map(|s| { @@ -407,8 +419,29 @@ pub fn idle() -> bool { println!("KERNEL: Starting initial processes:"); let mut args = std::env::args(); args.next(); + + // Set the current PID to 1, which was created above. This ensures all init processes + // are owned by PID1. + crate::arch::process::set_current_pid(pid1); + + println!(" PID | Command"); + println!("-------+------------------"); for arg in args { - println!(" {}", arg); + + let process_key = generate_pid_key(); + let init = xous::ProcessInit { key: ProcessKey::new(process_key) }; + let new_pid = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap(); + println!(" {:^5} | {}", new_pid, arg); + + use std::process::Command; + let server_env = format!("{}", address); + let process_key_env = hex::encode(process_key); + let cmd = Command::new("cmd") + .args(&["/C", &arg]) + .env("XOUS_SERVER", server_env) + .env("XOUS_PROCESS_KEY", process_key_env) + .spawn() + .expect("couldn't start command"); } } diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index 8132cd51c..b7661923b 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -4,7 +4,7 @@ use core::cell::RefCell; use std::io::Write; use std::net::TcpStream; use std::thread_local; -use xous::{ProcessInit, ThreadInit, PID, TID}; +use xous::{ProcessInit, ProcessKey, ThreadInit, PID, TID}; pub const INITIAL_TID: usize = 1; @@ -12,12 +12,13 @@ pub struct Process { pid: PID, } +#[derive(Debug)] struct ProcessImpl { /// Global parameters used by the operating system pub inner: ProcessInner, /// A 16-byte key used to register a process when it first starts - key: [u8; 16], + key: ProcessKey, /// The network connection to the client process. conn: Option, @@ -41,13 +42,20 @@ impl PartialEq for Process { } struct ProcessTable { + /// The process upon which the current syscall is operating current: PID, + + /// The number of processes that exist + total: usize, + + /// The actual table contents table: Vec>, } thread_local!( static PROCESS_TABLE: RefCell = RefCell::new(ProcessTable { current: unsafe { PID::new_unchecked(1) }, + total: 0, table: Vec::new(), }) ); @@ -57,10 +65,28 @@ pub fn current_pid() -> PID { } pub fn set_current_pid(pid: PID) { - PROCESS_TABLE.with(|pt| (*pt.borrow_mut()).current = pid); + PROCESS_TABLE.with(|pt| { + let pid_idx = (pid.get() - 1) as usize; + let mut pt = pt.borrow_mut(); + + // // If the PID doesn't exist, only allow it if the table is + // // currently empty. + // for (idx, i) in pt.table.iter().enumerate() { + // println!("pt.table[{}]: {:?}", idx, i); + // } + match pt.table.get_mut(pid_idx) { + None | Some(None) => { + // if pid.get() != 1 || pt.total > 0 { + panic!("PID {} does not exist", pid); + // } + } + Some(_) => {} + } + pt.current = pid + }); } -pub fn register_connection_for_key(conn: TcpStream, key: [u8; 16]) -> Result { +pub fn register_connection_for_key(conn: TcpStream, key: ProcessKey) -> Result { PROCESS_TABLE.with(|pt| { let mut process_table = pt.borrow_mut(); for (pid_minus_1, process) in process_table.table.iter_mut().enumerate() { @@ -272,6 +298,7 @@ impl Process { threads: [Thread { allocated: false }; MAX_THREAD + 1], }; + process_table.total += 1; if pid_idx >= process_table.table.len() { process_table.table.push(Some(process)); } else if process_table.table[pid_idx].is_none() { @@ -298,6 +325,7 @@ impl Process { .shutdown(std::net::Shutdown::Both) .unwrap(); process_table.table[pid_idx] = None; + process_table.total -= 1; Ok(()) }) } diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 5de3e6258..5a66d1c4b 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -299,7 +299,6 @@ impl SystemServices { } let new_pid = pid_from_usize(idx + 1)?; arch::process::Process::create(new_pid, init_process); - // #[allow(clippy::unit_arg)] let ppid = crate::arch::process::current_pid(); // println!("Creating new process for PID {} with PPID {}", new_pid, ppid); entry.state = ProcessState::Allocated; diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index a203feece..c0086533c 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -18,8 +18,6 @@ default-features = false # will not be implemented. This should only be set by the kernel. forget-memory-messages = [] -[target.'cfg(windows)'.dependencies] -lazy_static = "1.4" - -[target.'cfg(unix)'.dependencies] +[target.'cfg(any(windows,unix))'.dependencies] lazy_static = "1.4" +hex = "0.4" diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index 1403e1c3a..5fb412ddf 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -9,12 +9,20 @@ use std::thread_local; use crate::{Result, PID, TID}; +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct ProcessKey([u8; 16]); +impl ProcessKey { + pub fn new(key: [u8; 16]) -> ProcessKey { + ProcessKey(key) + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub struct ThreadInit {} #[derive(Copy, Clone, Debug, PartialEq)] pub struct ProcessInit { - pub key: [u8; 16], + pub key: ProcessKey, } pub struct ProcessArgs { @@ -51,10 +59,10 @@ pub fn thread_to_args(call: usize, _init: &ThreadInit) -> [usize; 8] { pub fn process_to_args(call: usize, init: &ProcessInit) -> [usize; 8] { [ 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 _, + u32::from_le_bytes(init.key.0[0..4].try_into().unwrap()) as _, + u32::from_le_bytes(init.key.0[4..8].try_into().unwrap()) as _, + u32::from_le_bytes(init.key.0[8..12].try_into().unwrap()) as _, + u32::from_le_bytes(init.key.0[12..16].try_into().unwrap()) as _, 0, 0, 0, @@ -89,14 +97,16 @@ pub fn args_to_process( v.extend_from_slice(&(a4 as u32).to_le_bytes()); let mut key = [0u8; 16]; key.copy_from_slice(&v); - Ok(ProcessInit { key }) + Ok(ProcessInit { + key: ProcessKey(key), + }) } thread_local!(static NETWORK_CONNECT_ADDRESS: RefCell> = RefCell::new(None)); thread_local!(static XOUS_SERVER_CONNECTION: RefCell> = RefCell::new(None)); thread_local!(static THREAD_ID: RefCell = RefCell::new(1)); thread_local!(static PROCESS_ID: RefCell = RefCell::new(PID::new(1).unwrap())); -thread_local!(static PROCESS_KEY: RefCell<[u8; 16]> = RefCell::new([0u8; 16])); +thread_local!(static PROCESS_KEY: RefCell> = RefCell::new(None)); fn default_xous_address() -> SocketAddr { std::env::var("XOUS_SERVER") @@ -109,8 +119,18 @@ fn default_xous_address() -> SocketAddr { .unwrap_or_else(|_| SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0)) } +fn default_process_key() -> ProcessKey { + std::env::var("XOUS_PROCESS_KEY") + .map(|s| { + let mut base = ProcessKey([0u8; 16]); + hex::decode_to_slice(s, &mut base.0).unwrap(); + base + }) + .unwrap_or(ProcessKey([0u8; 16])) +} + pub fn set_process_key(new_key: &[u8; 16]) { - PROCESS_KEY.with(|pk| *pk.borrow_mut() = *new_key); + PROCESS_KEY.with(|pk| *pk.borrow_mut() = Some(ProcessKey(*new_key))); } /// Set the network address for this particular thread. @@ -171,23 +191,15 @@ pub fn wait_thread(joiner: WaitHandle) -> crate::SysCallResult { .map_err(|_| crate::Error::InternalError) } -/// If no connection exists, create a new connection to the server. This means -/// our parent PID will be PID1. Otherwise, reuse the same connection. -pub fn create_process_pre( - _args: &ProcessArgs, -) -> core::result::Result -where - F: FnOnce(), -{ - // Ensure there is a connection, because after this function returns - // we'll make a syscall with CreateProcess(). This should only need - // to happen for PID1. +pub fn ensure_connection() -> core::result::Result<(), crate::Error> { XOUS_SERVER_CONNECTION.with(|xsc| { let mut xsc = xsc.borrow_mut(); if xsc.is_none() { NETWORK_CONNECT_ADDRESS.with(|nca| { let addr = nca.borrow().unwrap_or_else(default_xous_address); - let pid1_key = PROCESS_KEY.with(|pk| *pk.borrow()); + let pid1_key = PROCESS_KEY + .with(|pk| *pk.borrow()) + .unwrap_or_else(default_process_key); match xous_connect_impl(addr, &pid1_key) { Ok(a) => { *xsc = Some(a); @@ -199,9 +211,26 @@ where } else { Ok(()) } - })?; + }) +} + +/// If no connection exists, create a new connection to the server. This means +/// our parent PID will be PID1. Otherwise, reuse the same connection. +pub fn create_process_pre( + _args: &ProcessArgs, +) -> core::result::Result +where + F: FnOnce(), +{ + ensure_connection()?; + + // Ensure there is a connection, because after this function returns + // we'll make a syscall with CreateProcess(). This should only need + // to happen for PID1. Ok(ProcessInit { - key: PROCESS_KEY.with(|pk| *pk.borrow()), + key: PROCESS_KEY + .with(|pk| *pk.borrow()) + .unwrap_or_else(default_process_key), }) } @@ -252,13 +281,13 @@ pub fn wait_process(joiner: ProcessHandle) -> crate::SysCallResult { fn xous_connect_impl( addr: SocketAddr, - key: &[u8; 16], + key: &ProcessKey, ) -> core::result::Result { // eprintln!("Opening connection to Xous server @ {} with key {:?}...", addr, key); - assert_ne!(key, &[0u8; 16]); + assert_ne!(&key.0, &[0u8; 16]); match TcpStream::connect(addr) { Ok(mut conn) => { - conn.write_all(key).unwrap(); // Send key to authenticate us as PID 1 + conn.write_all(&key.0).unwrap(); // Send key to authenticate us as PID 1 Ok(ServerConnection { send: Arc::new(Mutex::new(conn.try_clone().unwrap())), recv: Arc::new(Mutex::new(conn)), diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index c0c72aa9f..59a90b0bc 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -12,7 +12,7 @@ pub mod definitions; mod messages; pub mod syscall; -pub use arch::{ProcessInit, ThreadInit, ProcessArgs}; +pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ThreadInit}; pub use definitions::*; pub use messages::*; pub use syscall::*;