2021-10-20 13:54:33 +03:00
|
|
|
//!
|
|
|
|
use crate::mem::{
|
|
|
|
self,
|
|
|
|
phys::{self, PageUsage},
|
|
|
|
virt::{MapAttributes, Space},
|
|
|
|
};
|
2021-10-20 16:32:07 +03:00
|
|
|
use crate::proc::{PROCESSES, SCHED};
|
2021-10-21 19:10:41 +03:00
|
|
|
use crate::sync::IrqSafeSpinLock;
|
2021-10-20 13:54:33 +03:00
|
|
|
use alloc::rc::Rc;
|
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::fmt;
|
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
use error::Errno;
|
|
|
|
|
|
|
|
pub use crate::arch::platform::context::{self, Context};
|
|
|
|
|
|
|
|
/// Wrapper type for a process struct reference
|
2021-10-20 16:32:07 +03:00
|
|
|
pub type ProcessRef = Rc<Process>;
|
|
|
|
|
|
|
|
/// Wrapper type for process exit code
|
|
|
|
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct ExitCode(i32);
|
2021-10-20 13:54:33 +03:00
|
|
|
|
|
|
|
/// Wrapper type for process ID
|
|
|
|
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct Pid(u32);
|
|
|
|
|
|
|
|
/// List of possible process states
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub enum State {
|
|
|
|
/// Process is ready to be executed and/or is scheduled for it
|
|
|
|
Ready,
|
|
|
|
/// Process is currently running or is in system call/interrupt handler
|
|
|
|
Running,
|
|
|
|
/// Process has finished execution and is waiting to be reaped
|
|
|
|
Finished,
|
|
|
|
/// Process is waiting for some external event
|
|
|
|
Waiting,
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
struct ProcessInner {
|
|
|
|
space: Option<&'static mut Space>,
|
|
|
|
state: State,
|
|
|
|
id: Pid,
|
|
|
|
exit: Option<ExitCode>
|
|
|
|
}
|
|
|
|
|
2021-10-20 13:54:33 +03:00
|
|
|
/// Structure describing an operating system process
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Process {
|
2021-10-20 16:32:07 +03:00
|
|
|
ctx: UnsafeCell<Context>,
|
2021-10-21 19:10:41 +03:00
|
|
|
inner: IrqSafeSpinLock<ProcessInner>,
|
2021-10-20 16:32:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i32> for ExitCode {
|
|
|
|
fn from(f: i32) -> Self {
|
|
|
|
Self(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<()> for ExitCode {
|
|
|
|
fn from(_: ()) -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Pid {
|
|
|
|
/// Kernel idle process always has PID of zero
|
2021-10-20 16:32:07 +03:00
|
|
|
pub const IDLE: Self = Self(Self::KERNEL_BIT);
|
2021-10-20 13:54:33 +03:00
|
|
|
|
|
|
|
const KERNEL_BIT: u32 = 1 << 31;
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Constructs an instance of user-space PID
|
|
|
|
pub const fn user(id: u32) -> Self {
|
|
|
|
assert!(id < 256, "PID is too high");
|
|
|
|
Self(id)
|
|
|
|
}
|
|
|
|
|
2021-10-20 13:54:33 +03:00
|
|
|
/// Allocates a new kernel-space PID
|
|
|
|
pub fn new_kernel() -> Self {
|
|
|
|
static LAST: AtomicU32 = AtomicU32::new(0);
|
|
|
|
let id = LAST.fetch_add(1, Ordering::Relaxed);
|
|
|
|
assert!(id & Self::KERNEL_BIT == 0, "Out of kernel PIDs");
|
|
|
|
Self(id | Self::KERNEL_BIT)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a new user-space PID.
|
|
|
|
///
|
|
|
|
/// First user PID is #1.
|
|
|
|
pub fn new_user() -> Self {
|
|
|
|
static LAST: AtomicU32 = AtomicU32::new(1);
|
|
|
|
let id = LAST.fetch_add(1, Ordering::Relaxed);
|
|
|
|
assert!(id < 256, "Out of user PIDs");
|
|
|
|
Self(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this PID belongs to a kernel process
|
|
|
|
pub fn is_kernel(self) -> bool {
|
|
|
|
self.0 & Self::KERNEL_BIT != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns address space ID of a user-space process.
|
|
|
|
///
|
|
|
|
/// Panics if called on kernel process PID.
|
|
|
|
pub fn asid(self) -> u8 {
|
|
|
|
assert!(!self.is_kernel());
|
|
|
|
self.0 as u8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Pid {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Pid(#{}{})",
|
|
|
|
if self.is_kernel() { "K" } else { "U" },
|
|
|
|
self.0 & !Self::KERNEL_BIT
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
2021-10-20 16:32:07 +03:00
|
|
|
const USTACK_VIRT_TOP: usize = 0x100000000;
|
|
|
|
const USTACK_PAGES: usize = 4;
|
|
|
|
|
2021-10-21 12:16:24 +03:00
|
|
|
/// Returns currently executing process
|
|
|
|
pub fn current() -> ProcessRef {
|
|
|
|
SCHED.current_process()
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Schedules an initial thread for execution
|
2021-10-20 13:54:33 +03:00
|
|
|
///
|
2021-10-20 16:32:07 +03:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Unsafe: only allowed to be called once, repeated calls
|
|
|
|
/// will generate undefined behavior
|
|
|
|
pub unsafe fn enter(proc: ProcessRef) -> ! {
|
|
|
|
// FIXME use some global lock to guarantee atomicity of thread entry?
|
|
|
|
proc.inner.lock().state = State::Running;
|
|
|
|
let ctx = proc.ctx.get();
|
|
|
|
|
|
|
|
(&mut *ctx).enter()
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Schedules a next thread for execution
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Unsafe:
|
2021-10-20 13:54:33 +03:00
|
|
|
///
|
2021-10-20 16:32:07 +03:00
|
|
|
/// * Does not ensure src and dst threads are not the same thread
|
|
|
|
/// * Does not ensure src is actually current context
|
|
|
|
pub unsafe fn switch(src: ProcessRef, dst: ProcessRef, discard: bool) {
|
|
|
|
{
|
|
|
|
let mut src_lock = src.inner.lock();
|
|
|
|
let mut dst_lock = dst.inner.lock();
|
|
|
|
|
|
|
|
if !discard {
|
|
|
|
assert_eq!(src_lock.state, State::Running);
|
|
|
|
src_lock.state = State::Ready;
|
|
|
|
}
|
|
|
|
assert_eq!(dst_lock.state, State::Ready);
|
|
|
|
dst_lock.state = State::Running;
|
|
|
|
}
|
|
|
|
|
|
|
|
let src_ctx = src.ctx.get();
|
|
|
|
let dst_ctx = dst.ctx.get();
|
|
|
|
|
|
|
|
(&mut *src_ctx).switch(&mut *dst_ctx);
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Returns the process ID
|
|
|
|
pub fn id(&self) -> Pid {
|
|
|
|
self.inner.lock().id
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Creates a new kernel process
|
|
|
|
pub fn new_kernel(entry: extern "C" fn(usize) -> !, arg: usize) -> Result<ProcessRef, Errno> {
|
2021-10-20 13:54:33 +03:00
|
|
|
let id = Pid::new_kernel();
|
2021-10-20 16:32:07 +03:00
|
|
|
let res = Rc::new(Self {
|
|
|
|
ctx: UnsafeCell::new(Context::kernel(entry as usize, arg)),
|
2021-10-21 19:10:41 +03:00
|
|
|
inner: IrqSafeSpinLock::new(ProcessInner {
|
2021-10-20 16:32:07 +03:00
|
|
|
id,
|
|
|
|
exit: None,
|
|
|
|
space: None,
|
|
|
|
state: State::Ready,
|
|
|
|
}),
|
|
|
|
});
|
2021-10-21 12:16:24 +03:00
|
|
|
debugln!("New kernel process: {}", id);
|
2021-10-20 13:54:33 +03:00
|
|
|
assert!(PROCESSES.lock().insert(id, res.clone()).is_none());
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Terminates a process.
|
2021-10-20 16:32:07 +03:00
|
|
|
pub fn exit<I: Into<ExitCode>>(&self, status: I) {
|
|
|
|
let status = status.into();
|
|
|
|
let drop = {
|
|
|
|
let mut lock = self.inner.lock();
|
|
|
|
let drop = lock.state == State::Running;
|
|
|
|
infoln!("Process {} is exiting: {:?}", lock.id, status);
|
|
|
|
assert!(lock.exit.is_none());
|
|
|
|
lock.exit = Some(status);
|
|
|
|
lock.state = State::Finished;
|
|
|
|
SCHED.dequeue(lock.id);
|
|
|
|
drop
|
|
|
|
};
|
|
|
|
if drop {
|
|
|
|
SCHED.switch(true);
|
|
|
|
panic!("This code should never run");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect(&self) -> Option<ExitCode> {
|
|
|
|
let lock = self.inner.lock();
|
|
|
|
if lock.state == State::Finished {
|
|
|
|
lock.exit
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for a process to finish and reaps it
|
|
|
|
pub fn waitpid(pid: Pid) -> Result<ExitCode, Errno> {
|
|
|
|
loop {
|
|
|
|
let proc = PROCESSES.lock().get(&pid).cloned().ok_or(Errno::DoesNotExist)?;
|
|
|
|
|
|
|
|
if let Some(r) = proc.collect() {
|
|
|
|
PROCESSES.lock().remove(&proc.id());
|
|
|
|
debugln!("r = {}", Rc::strong_count(&proc));
|
|
|
|
return Ok(r);
|
|
|
|
} else {
|
|
|
|
cortex_a::asm::wfi();
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:32:07 +03:00
|
|
|
/// Loads a new program into current process address space
|
2021-10-20 13:54:33 +03:00
|
|
|
pub fn execve<F: FnOnce(&mut Space) -> Result<usize, Errno>>(
|
|
|
|
loader: F,
|
|
|
|
arg: usize,
|
|
|
|
) -> Result<(), Errno> {
|
2021-10-20 16:32:07 +03:00
|
|
|
unsafe {
|
|
|
|
// Run with interrupts disabled
|
|
|
|
asm!("msr daifset, #2");
|
|
|
|
}
|
|
|
|
|
|
|
|
let proc = SCHED.current_process();
|
|
|
|
let mut lock = proc.inner.lock();
|
|
|
|
if lock.id.is_kernel() {
|
|
|
|
let mut proc_lock = PROCESSES.lock();
|
|
|
|
let old_pid = lock.id;
|
|
|
|
assert!(
|
|
|
|
proc_lock.remove(&old_pid).is_some(),
|
|
|
|
"Failed to downgrade kernel process (remove kernel pid)"
|
|
|
|
);
|
|
|
|
lock.id = Pid::new_user();
|
|
|
|
debugln!(
|
|
|
|
"Process downgrades from kernel to user: {} -> {}",
|
|
|
|
old_pid,
|
|
|
|
lock.id
|
|
|
|
);
|
2021-10-21 12:16:24 +03:00
|
|
|
assert!(proc_lock.insert(lock.id, proc.clone()).is_none());
|
2021-10-20 16:32:07 +03:00
|
|
|
unsafe {
|
|
|
|
SCHED.hack_current_pid(lock.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_space = Space::alloc_empty()?;
|
|
|
|
let new_space_phys = (new_space as *mut _ as usize) - mem::KERNEL_OFFSET;
|
|
|
|
|
|
|
|
let ustack_virt_bottom = Self::USTACK_VIRT_TOP - Self::USTACK_PAGES * mem::PAGE_SIZE;
|
|
|
|
for i in 0..Self::USTACK_PAGES {
|
|
|
|
let page = phys::alloc_page(PageUsage::UserPrivate).unwrap();
|
|
|
|
let flags = MapAttributes::SH_OUTER
|
|
|
|
| MapAttributes::NOT_GLOBAL
|
|
|
|
| MapAttributes::UXN
|
|
|
|
| MapAttributes::PXN
|
|
|
|
| MapAttributes::AP_BOTH_READWRITE;
|
|
|
|
new_space
|
|
|
|
.map(ustack_virt_bottom + i * mem::PAGE_SIZE, page, flags)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = loader(new_space)?;
|
|
|
|
|
|
|
|
debugln!("Will now enter at {:#x}", entry);
|
|
|
|
// TODO drop old address space
|
|
|
|
lock.space = Some(new_space);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// TODO drop old context
|
|
|
|
let ctx = proc.ctx.get();
|
|
|
|
|
|
|
|
ctx.write(Context::user(
|
|
|
|
entry,
|
|
|
|
arg,
|
|
|
|
new_space_phys | ((lock.id.asid() as usize) << 48),
|
|
|
|
Self::USTACK_VIRT_TOP,
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(lock.state, State::Running);
|
|
|
|
|
|
|
|
drop(lock);
|
|
|
|
|
|
|
|
(*ctx).enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Process {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
debugln!("Dropping process {}", self.id());
|
2021-10-20 13:54:33 +03:00
|
|
|
}
|
|
|
|
}
|