diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index 54f30daea..50dde2311 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -485,9 +485,9 @@ pub fn idle() -> bool { 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 _tid1 = SystemServices::with_mut(|ss| ss.create_thread(pid1, ThreadInit {})).unwrap(); + let process_1 = SystemServices::with_mut(|ss| ss.create_process(pid1_init)).unwrap(); + assert_eq!(process_1.pid().get(), 1); + let _tid1 = SystemServices::with_mut(|ss| ss.create_thread(process_1.pid(), ThreadInit {})).unwrap(); let listen_addr = env::var("XOUS_LISTEN_ADDR") .map(|s| { @@ -524,7 +524,7 @@ pub fn idle() -> bool { // 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); + crate::arch::process::set_current_pid(process_1.pid()); // Go through each arg and spawn it as a new process. Failures here will // halt the entire system. @@ -535,10 +535,10 @@ pub fn idle() -> bool { let init = xous_kernel::ProcessInit { key: ProcessKey::new(process_key), }; - let new_pid = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap(); - println!(" {:^5} | {}", new_pid, arg); + let new_process = SystemServices::with_mut(|ss| ss.create_process(init)).unwrap(); + println!(" {:^5} | {}", new_process, arg); let process_args = xous_kernel::ProcessArgs::new("program", arg); - xous_kernel::arch::create_process_post(process_args, init, new_pid) + xous_kernel::arch::create_process_post(process_args, init, new_process) .expect("couldn't spawn"); } } diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index f41c66d7c..8c2052475 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -7,7 +7,7 @@ use core::cell::RefCell; use std::io::Write; use std::net::TcpStream; use std::thread_local; -use xous_kernel::{ProcessInit, ProcessKey, ThreadInit, PID, TID}; +use xous_kernel::{ProcessInit, ProcessKey, ProcessStartup, ThreadInit, PID, TID}; pub const INITIAL_TID: usize = 2; pub const EXCEPTION_TID: usize = 1; @@ -337,7 +337,7 @@ impl Process { /// Initialize this process with the given memory space. THIS DOES NOT /// INITIALIZE A MAIN THREAD. You must call `setup_thread()` in order to /// select a main thread. - pub fn create(pid: PID, init_data: ProcessInit) -> PID { + pub fn create(pid: PID, init_data: ProcessInit) -> ProcessStartup { PROCESS_TABLE.with(|process_table| { let mut process_table = process_table.borrow_mut(); let pid_idx = (pid.get() - 1) as usize; @@ -359,7 +359,7 @@ impl Process { } else { panic!("pid already allocated!"); } - pid + ProcessStartup::new(pid) }) } @@ -376,7 +376,7 @@ impl Process { .as_mut() .unwrap() .shutdown(std::net::Shutdown::Both) - .unwrap(); + .ok(); process_table.table[pid_idx] = None; process_table.total -= 1; Ok(()) diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index c958cdd58..ce5a705e5 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -9,7 +9,7 @@ pub const INITIAL_TID: TID = 2; pub const IRQ_TID: TID = 0; use crate::arch::mem::PAGE_SIZE; use crate::services::ProcessInner; -use xous_kernel::{ProcessInit, ThreadInit, PID, TID}; +use xous_kernel::{ProcessInit, ThreadInit, PID, TID, ProcessStartup}; // use crate::args::KernelArguments; pub const DEFAULT_STACK_SIZE: usize = 131072; @@ -497,7 +497,7 @@ impl Process { ); } - pub fn create(_pid: PID, _init_data: ProcessInit) -> PID { + pub fn create(_pid: PID, _init_data: ProcessInit) -> ProcessStartup { todo!(); } diff --git a/kernel/src/services.rs b/kernel/src/services.rs index fcd22533c..aaf299ab3 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -6,6 +6,7 @@ use crate::arch::mem::MemoryMapping; pub use crate::arch::process::Process as ArchProcess; pub use crate::arch::process::Thread; use xous_kernel::MemoryRange; +use xous_kernel::arch::ProcessStartup; use core::num::NonZeroU8; @@ -389,19 +390,19 @@ impl SystemServices { /// 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()`. - pub fn create_process(&mut self, init_process: ProcessInit) -> Result { + pub fn create_process(&mut self, init_process: ProcessInit) -> Result { for (idx, mut entry) in self.processes.iter_mut().enumerate() { if entry.state != ProcessState::Free { continue; } let new_pid = pid_from_usize(idx + 1)?; - arch::process::Process::create(new_pid, init_process); + let startup = arch::process::Process::create(new_pid, init_process); let ppid = crate::arch::process::current_pid(); // println!("Creating new process for PID {} with PPID {}", new_pid, ppid); entry.state = ProcessState::Allocated; entry.ppid = ppid; entry.pid = new_pid; - return Ok(new_pid); + return Ok(startup); } Err(xous_kernel::Error::ProcessNotFound) } diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index d0ebcf399..b02dac6a1 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -803,7 +803,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR }), SysCall::CreateProcess(process_init) => SystemServices::with_mut(|ss| { ss.create_process(process_init) - .map(xous_kernel::Result::ProcessID) + .map(xous_kernel::Result::NewProcess) }), SysCall::CreateServerWithAddress(name) => SystemServices::with_mut(|ss| { ss.create_server_with_address(pid, name) diff --git a/xous-rs/src/arch/hosted/process.rs b/xous-rs/src/arch/hosted/process.rs index 4f09b4eb1..f105c96b3 100644 --- a/xous-rs/src/arch/hosted/process.rs +++ b/xous-rs/src/arch/hosted/process.rs @@ -1,4 +1,4 @@ -use super::{CHILD_PROCESS_ADDRESS}; +use super::CHILD_PROCESS_ADDRESS; pub use crate::PID; use core::convert::{TryFrom, TryInto}; @@ -69,6 +69,22 @@ pub struct ProcessStartup { pid: crate::PID, } +impl ProcessStartup { + pub fn new(pid: crate::PID) -> Self { + ProcessStartup { pid } + } + + pub fn pid(&self) -> crate::PID { + self.pid + } +} + +impl core::fmt::Display for ProcessStartup { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}", self.pid) + } +} + impl From<&[usize; 7]> for ProcessStartup { fn from(src: &[usize; 7]) -> ProcessStartup { ProcessStartup { @@ -103,11 +119,11 @@ pub fn create_process_pre(_args: &ProcessArgs) -> core::result::Result core::result::Result { use std::process::Command; let server_env = format!("{}", CHILD_PROCESS_ADDRESS.lock().unwrap()); - let pid_env = format!("{}", pid); + let pid_env = format!("{}", startup.pid); let process_name_env = args.name.to_string(); let process_key_env = hex::encode(&init.key.0); let (shell, args) = if cfg!(windows) { diff --git a/xous-rs/src/arch/riscv/process.rs b/xous-rs/src/arch/riscv/process.rs index dcbfd6c9d..cf46ffea3 100644 --- a/xous-rs/src/arch/riscv/process.rs +++ b/xous-rs/src/arch/riscv/process.rs @@ -116,9 +116,9 @@ pub fn create_process_pre(_args: &ProcessArgs) -> core::result::Result core::result::Result { - Ok(ProcessHandle(pid)) + Ok(ProcessHandle(startup.pid)) } /// Wait for a process to terminate. diff --git a/xous-rs/src/arch/test/mod.rs b/xous-rs/src/arch/test/mod.rs index 865f8481e..ff043e0fa 100644 --- a/xous-rs/src/arch/test/mod.rs +++ b/xous-rs/src/arch/test/mod.rs @@ -69,12 +69,13 @@ where pub fn create_process_post_as_thread( args: ProcessArgsAsThread, init: ProcessInit, - pid: PID, + startup: ProcessStartup, ) -> core::result::Result where F: FnOnce() + Send + 'static, { let server_address = xous_address(); + let pid = startup.pid; let f = args.main; let thread_main = std::thread::Builder::new() @@ -132,6 +133,22 @@ pub struct ProcessStartup { pid: crate::PID, } +impl ProcessStartup { + pub fn new(pid: crate::PID) -> Self { + ProcessStartup { pid } + } + + pub fn pid(&self) -> crate::PID { + self.pid + } +} + +impl core::fmt::Display for ProcessStartup { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}", self.pid) + } +} + impl From<&[usize; 7]> for ProcessStartup { fn from(src: &[usize; 7]) -> ProcessStartup { ProcessStartup { @@ -153,7 +170,6 @@ impl Into<[usize; 7]> for &ProcessStartup { } } - impl Into<[usize; 7]> for &ProcessInit { fn into(self) -> [usize; 7] { [ @@ -204,11 +220,11 @@ pub fn create_process_pre(_args: &ProcessArgs) -> core::result::Result core::result::Result { use std::process::Command; let server_env = format!("{}", xous_address()); - let pid_env = format!("{}", pid); + let pid_env = format!("{}", startup.pid); let process_name_env = args.name.to_string(); let process_key_env = hex::encode(&init.key.0); let (shell, args) = if cfg!(windows) { diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index 2dc343de1..607678755 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -19,7 +19,7 @@ pub mod string; pub mod stringbuffer; pub mod syscall; -pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ThreadInit}; +pub use arch::{ProcessArgs, ProcessInit, ProcessKey, ProcessStartup, ThreadInit}; pub use definitions::*; pub use string::*; pub use stringbuffer::*; diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 3a351b3b8..e20ba72f4 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -1659,8 +1659,8 @@ where { let process_init = crate::arch::create_process_pre_as_thread(&args)?; rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { - if let Result::ProcessID(pid) = result { - crate::arch::create_process_post_as_thread(args, process_init, pid) + if let Result::NewProcess(startup) = result { + crate::arch::create_process_post_as_thread(args, process_init, startup) } else { Err(Error::InternalError) } @@ -1678,8 +1678,8 @@ pub fn create_process( ) -> core::result::Result { let process_init = crate::arch::create_process_pre(&args)?; rsyscall(SysCall::CreateProcess(process_init)).and_then(|result| { - if let Result::ProcessID(pid) = result { - crate::arch::create_process_post(args, process_init, pid) + if let Result::NewProcess(startup) = result { + crate::arch::create_process_post(args, process_init, startup) } else { Err(Error::InternalError) }