123 lines
3.7 KiB
Rust
123 lines
3.7 KiB
Rust
#![allow(unused_imports, dead_code)]
|
|
|
|
use enumflags2::{BitFlags, bitflags, make_bitflags};
|
|
|
|
use crate::arch::current;
|
|
use crate::memory::MemoryRegion;
|
|
use crate::memory::MemoryRegionFlags;
|
|
use crate::memory::alloc;
|
|
use alloc::string::String;
|
|
use alloc::vec::Vec;
|
|
use current::asm;
|
|
|
|
#[allow(dead_code)]
|
|
pub struct ProcessTable {}
|
|
|
|
#[allow(dead_code)]
|
|
pub struct Process {
|
|
/// Unique process ID.
|
|
proc_id: u32,
|
|
/// ID of the parent process.
|
|
parent_proc_id: u32,
|
|
/// List of all child processes.
|
|
child_proc_ids: Vec<u32>,
|
|
/// Human readable name of the process, should be the command.
|
|
name: String,
|
|
/// The base address of the process stack within its memory region.
|
|
stack_base_addr: usize,
|
|
/// The length of the stack. This, plus the start address, is the stack size.
|
|
stack_length: usize,
|
|
/// The stack pointer, which is relative to the memory region start.
|
|
stack_ptr: usize,
|
|
/// The region of memory allocated to the process.
|
|
mem_region: MemoryRegion,
|
|
/// Process priority. Lower number is higher priority.
|
|
prio: u16,
|
|
/// Which semaphor the process is waiting on.
|
|
semaphor_wait: Option<u32>,
|
|
/// What capabilities the process has
|
|
capabilities: BitFlags<ProcessCapabilities>,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub unsafe fn context_switch() -> ! {
|
|
#[allow(unused_unsafe)]
|
|
unsafe {
|
|
loop {
|
|
asm::halt();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub enum ProcessState {
|
|
Running,
|
|
Waiting,
|
|
Sleeping,
|
|
Suspended,
|
|
}
|
|
|
|
#[bitflags]
|
|
#[repr(u32)]
|
|
#[derive(Copy, Clone)]
|
|
pub enum ProcessCapabilities {
|
|
// Process capabilities
|
|
ProcessEnum, // Enumerate running process IDs and names
|
|
ProcessRead, // Read information from a process struct
|
|
ProcessKill, // Kill any process
|
|
ProcessSpawn, // Create a new process
|
|
ProcessExec, // Replace self with new process image
|
|
ProcessSession, // Create and accept IPC requests.
|
|
// Capability meta
|
|
CapabilityRead, // Inspect a process's capabilities
|
|
CapabilityAdd, // Add a capability to a process
|
|
// Hardware access capabilities
|
|
HardwareWrite, // Write to memory-mapped IO
|
|
HardwareRead, // Read from memory-mapped IO
|
|
HardwareLock, // Obtain exclusive access to a device
|
|
HardwareQuery, // Check on the lock status of a device
|
|
// Kernel config capabilities
|
|
KernelCfgRead, // Read kernel configurations
|
|
KernelCfgWrite, // Modify kernel configurations
|
|
KernelProtCall, // Call protected kernel functions
|
|
// Server resolution capabilities
|
|
ServerEnum, // Enumerate server processes
|
|
ServerGet, // Get the PID of a specific server for IPC.
|
|
ServerRegister, // Register a process as a server.
|
|
}
|
|
|
|
// Interprocess communication system:
|
|
// Processes communicate through "sessions" which are mediated by the kernel.
|
|
// Sessions can only be opened if two processes both request a session with
|
|
// each other (mutual operation). Submitting a session request is a blocking
|
|
// system call that will return either once the other process accepts, or the
|
|
// kernel denies the request due to missing capability, or the other process
|
|
// not existing yet.
|
|
pub struct ProcessSession {
|
|
proc_id_a: u32,
|
|
proc_id_b: u32,
|
|
shared_mem: MemoryRegion,
|
|
}
|
|
|
|
impl ProcessSession {
|
|
pub fn new(a: ProcessSessionRequest, b: ProcessSessionRequest) -> Self {
|
|
ProcessSession {
|
|
proc_id_a: a.proc_id,
|
|
proc_id_b: b.proc_id,
|
|
shared_mem: MemoryRegion::new(
|
|
MemoryRegionFlags::Readable | MemoryRegionFlags::Writable,
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ProcessSessionRequest {
|
|
proc_id: u32,
|
|
}
|
|
|
|
impl ProcessSessionRequest {
|
|
pub fn accept(self, b: ProcessSessionRequest) -> ProcessSession {
|
|
ProcessSession::new(self, b)
|
|
}
|
|
}
|