kernel: get initial hosted non-test processes working

This now has the ability to fork off processes.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-29 17:52:01 +08:00
parent d9024076c6
commit e7e8a2b748
9 changed files with 141 additions and 45 deletions

8
Cargo.lock generated
View file

@ -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",

View file

@ -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();

View file

@ -30,3 +30,4 @@ report-memory = ["stats_alloc"]
[target.'cfg(any(windows, unix))'.dependencies]
rand = "0.7"
hex = "0.4"

View file

@ -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<SocketAddr>) {
});
}
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");
}
}

View file

@ -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<TcpStream>,
@ -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<Option<ProcessImpl>>,
}
thread_local!(
static PROCESS_TABLE: RefCell<ProcessTable> = 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<PID, xous::Error> {
pub fn register_connection_for_key(conn: TcpStream, key: ProcessKey) -> 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() {
@ -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(())
})
}

View file

@ -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;

View file

@ -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"

View file

@ -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<F: FnOnce()> {
@ -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<Option<SocketAddr>> = RefCell::new(None));
thread_local!(static XOUS_SERVER_CONNECTION: RefCell<Option<ServerConnection>> = RefCell::new(None));
thread_local!(static THREAD_ID: RefCell<TID> = RefCell::new(1));
thread_local!(static PROCESS_ID: RefCell<PID> = RefCell::new(PID::new(1).unwrap()));
thread_local!(static PROCESS_KEY: RefCell<[u8; 16]> = RefCell::new([0u8; 16]));
thread_local!(static PROCESS_KEY: RefCell<Option<ProcessKey>> = 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<T>(joiner: WaitHandle<T>) -> 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<F>(
_args: &ProcessArgs<F>,
) -> core::result::Result<ProcessInit, crate::Error>
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<F>(
_args: &ProcessArgs<F>,
) -> core::result::Result<ProcessInit, crate::Error>
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<ServerConnection, ()> {
// 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)),

View file

@ -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::*;