2020-06-03 07:34:55 +00:00
|
|
|
pub const MAX_CONTEXT: CtxID = 31;
|
|
|
|
|
use crate::services::ProcessInner;
|
2020-06-15 10:13:10 +00:00
|
|
|
use std::net::TcpStream;
|
|
|
|
|
use std::sync::Mutex;
|
2020-06-03 07:34:55 +00:00
|
|
|
use xous;
|
2020-06-15 10:13:10 +00:00
|
|
|
use xous::{CtxID, PID};
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
pub type ContextInit = ();
|
2020-06-03 07:34:55 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
pub struct Process {
|
2020-06-03 07:34:55 +00:00
|
|
|
/// Global parameters used by the operating system
|
|
|
|
|
pub inner: ProcessInner,
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
/// The network connection to the client process.
|
|
|
|
|
conn: TcpStream,
|
|
|
|
|
|
|
|
|
|
/// Current process ID
|
|
|
|
|
pid: PID,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq for Process {
|
|
|
|
|
fn eq(&self, other: &Process) -> bool {
|
|
|
|
|
self.pid == other.pid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ProcessTable {
|
|
|
|
|
current: usize,
|
|
|
|
|
table: Vec<Process>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
|
static ref PROCESS_TABLE: Mutex<ProcessTable> = Mutex::new(ProcessTable {
|
|
|
|
|
current: 0,
|
|
|
|
|
table: Vec::new(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn current_pid() -> PID {
|
|
|
|
|
PROCESS_TABLE.lock().unwrap().current as PID + 1
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
|
/// Everything required to keep track of a single thread of execution.
|
2020-06-15 10:13:10 +00:00
|
|
|
/// In a `std` environment, we can't manage threads so this is a no-op.
|
|
|
|
|
pub struct ProcessContext {}
|
|
|
|
|
|
|
|
|
|
/// Everything required to initialize a process on this platform
|
|
|
|
|
pub struct ProcessInit {
|
|
|
|
|
/// A network connection to the client
|
|
|
|
|
conn: TcpStream,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProcessInit {
|
|
|
|
|
pub fn new(conn: TcpStream) -> ProcessInit {
|
|
|
|
|
ProcessInit { conn }
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Process {
|
2020-06-15 10:13:10 +00:00
|
|
|
/// Mark this process as running (on the current core?!)
|
|
|
|
|
pub fn activate(&self) {
|
|
|
|
|
let mut pt = PROCESS_TABLE.lock().unwrap();
|
|
|
|
|
assert!(pt.table[self.pid as usize - 1] == *self);
|
|
|
|
|
pt.current = self.pid as _;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
pub fn current_context(&mut self) -> &mut ProcessContext {
|
2020-06-15 10:13:10 +00:00
|
|
|
unimplemented!()
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the current context number.
|
|
|
|
|
pub fn set_context_nr(&mut self, context: CtxID) {
|
2020-06-15 10:13:10 +00:00
|
|
|
if context != 0 {
|
|
|
|
|
panic!("context was {}, not 0", context);
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
pub fn context(&mut self, _context_nr: CtxID) -> &mut ProcessContext {
|
|
|
|
|
unimplemented!()
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find_free_context_nr(&self) -> Option<CtxID> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
pub fn set_context_result(&mut self, _context_nr: CtxID, _result: xous::Result) {
|
|
|
|
|
unimplemented!()
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Initialize this process context with the given entrypoint and stack
|
|
|
|
|
/// addresses.
|
2020-06-15 10:13:10 +00:00
|
|
|
pub fn create(pid: PID, init_data: ProcessInit) -> PID {
|
|
|
|
|
let mut process_table = PROCESS_TABLE.lock().unwrap();
|
|
|
|
|
assert!(pid != 0, "PID is zero!");
|
|
|
|
|
|
|
|
|
|
let pid_idx = (pid - 1) as usize;
|
|
|
|
|
|
|
|
|
|
assert!(pid_idx >= process_table.table.len(), "PID {} already allocated", pid);
|
|
|
|
|
let process = Process {
|
|
|
|
|
inner: Default::default(),
|
|
|
|
|
conn: init_data.conn,
|
|
|
|
|
pid,
|
|
|
|
|
};
|
|
|
|
|
if pid_idx >= process_table.table.len() {
|
|
|
|
|
process_table.table.push(process);
|
|
|
|
|
} else {
|
|
|
|
|
panic!("pid already allocated!");
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
pid
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProcessContext {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ProcessHandle<'a> {
|
2020-06-15 10:13:10 +00:00
|
|
|
inner: std::sync::MutexGuard<'a, ProcessTable>,
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wraps the MemoryManager in a safe mutex. Because of this, accesses
|
|
|
|
|
/// to the Memory Manager should only be made during interrupt contexts.
|
|
|
|
|
impl<'a> ProcessHandle<'a> {
|
|
|
|
|
/// Get the singleton Process.
|
|
|
|
|
pub fn get() -> ProcessHandle<'a> {
|
|
|
|
|
ProcessHandle {
|
2020-06-15 10:13:10 +00:00
|
|
|
inner: PROCESS_TABLE.lock().unwrap(),
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
|
|
|
|
|
pub fn set(pid: PID) {
|
|
|
|
|
PROCESS_TABLE.lock().unwrap().current = pid as usize - 1;
|
|
|
|
|
}
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
|
impl Deref for ProcessHandle<'_> {
|
|
|
|
|
type Target = Process;
|
|
|
|
|
fn deref(&self) -> &Process {
|
2020-06-15 10:13:10 +00:00
|
|
|
&self.inner.table[self.inner.current]
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
impl DerefMut for ProcessHandle<'_> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Process {
|
2020-06-15 10:13:10 +00:00
|
|
|
let current = self.inner.current;
|
|
|
|
|
&mut self.inner.table[current]
|
2020-06-03 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
}
|