Skip to main content

kernel/sched/
process.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! The definition of a process (its data and state)
3//!
4//! Authors: MarioS271
5
6use crate::lib::addr::VirtAddr;
7use crate::mm::vmm::address_space::AddressSpace;
8use core::sync::atomic::AtomicI32;
9
10/// Type which represents a PID
11pub type Pid = i32;
12
13/// Type which represents an atomic PID
14pub type AtomicPid = AtomicI32;
15
16/// Represents all necessary data to manage processes
17pub struct Process {
18    pub pid: Pid,
19    pub parent_pid: Pid,
20    pub addr_space: AddressSpace,
21    pub status: ProcessStatus,
22    pub kernel_stack_top: VirtAddr,
23    pub regs: SavedRegs
24}
25impl Process {
26    /// Create a new process which owns an address space
27    pub fn new(pid: Pid, parent_pid: Pid, addr_space: AddressSpace, kernel_stack_top: VirtAddr) -> Self {
28        Self {
29            pid,
30            parent_pid,
31            addr_space,
32            status: ProcessStatus::Ready,
33            kernel_stack_top,
34            regs: SavedRegs::new()
35        }
36    }
37}
38
39/// The status of a process
40pub enum ProcessStatus {
41    /// Ready to be run by the scheduler
42    Ready,
43    /// Currently running on a CPU
44    Running,
45    /// Waiting on a resource (I/O, a lock, ...)
46    Waiting,
47    /// Exited; also contains an exit code
48    Zombie(i32)
49}
50
51/// The saved CPU registers for when execution was paused by the scheduler
52#[cfg(target_arch = "x86_64")]
53pub struct SavedRegs {
54    pub rax: u64, pub rbx: u64, pub rcx: u64, pub rdx: u64,
55    pub rsi: u64, pub rdi: u64, pub rbp: u64,
56    pub r8:  u64, pub r9:  u64, pub r10: u64, pub r11: u64,
57    pub r12: u64, pub r13: u64, pub r14: u64, pub r15: u64,
58    pub rip: u64, pub rsp: u64, pub rflags: u64,
59}
60impl SavedRegs {
61    /// Construct a zeroed (except `rflags`) [`SavedRegs`] instance
62    ///
63    /// `rflags` gets the reserved bit 1, which the CPU requires to be set,
64    /// plus IF so the process can be preempted
65    pub const fn new() -> Self {
66        Self {
67            rax: 0, rbx: 0, rcx: 0, rdx: 0,
68            rsi: 0, rdi: 0, rbp: 0,
69            r8: 0, r9: 0, r10: 0, r11: 0,
70            r12: 0, r13: 0, r14: 0, r15: 0,
71            rip: 0, rsp: 0, rflags: 0x202
72        }
73    }
74}