Milo models execution units as Kernel::Process::Process instances scheduled across a global intrusive runqueue (Kernel::Scheduler::Runqueue / Kernel::Scheduler) and intrusive sleep queues (Kernel::Process::WaitQueue), with AArch64 context-switch and exception-exit assembly in arch/aarch64-linux-gnu/boot.s and arch/aarch64-linux-gnu/cpu.cpp.
Process Control Block (kernel/process/Process.hpp, kernel/process/Process.cpp)Each Process is allocated from the kernel heap (MM::kzalloc) via Process::create(uint32_t ppid) and linked into a global doubly-linked process list (g_process_list_head..g_process_list_tail), subject to a dynamic process quota (default_rlimit_nproc() in kernel/process/resource.{hpp,cpp}, computed as ram_size / (1 MiB) with a minimum of 64).
Key resources and metadata owned by a Process:
Identity & Session/Group: m_pid (monotonic uint32_t starting at 1), m_ppid, m_pgid, m_sid (setsid()), m_umask (default 0022), m_cwd (MAX_PATH_LEN = 256, default "/"), and m_exe_path.
Stacks & Contexts: An 8-page (KERNEL_STACK_PAGES = 8U, 32 KiB) kernel stack (m_kstack_paddr, m_kstack_top), HAL::KernelContext m_kctx (112 bytes), HAL::CpuContext m_context (288 bytes), optional UserStack m_user_stack (default 64 KiB / 16 pages at 0x3FFF0000 built by UserStackBuilder in kernel/process/stack_builder.{hpp,cpp}), and MM::AddressSpace m_address_space.
User Memory Cursors & Thread ID: m_entry_point, m_brk_start, m_brk_current, m_mmap_next_vaddr, and m_clear_child_tid (sys_set_tid_address, cleared in user space on exit).
Subsystems & Accounting: FdTable m_fds (INITIAL_FD_CAPACITY = 16U, FDs 0, 1, 2 opened on /dev/console during Process::create), SignalTable m_signals, WaitQueue m_child_wait_queue, RLimit64 limits for RLIMIT_NOFILE (1024 soft / 4096 hard) and RLIMIT_NPROC, m_timeslice (DEFAULT_TIMESLICE_TICKS = 4U), and tick counters m_utime, m_stime, m_cutime, and m_cstime.
ProcessState)Unused (0): Default zero-initialized enum value.
Ready (1): Eligible to run or currently queued in the runqueue.
Running (2): Currently executing on the CPU (Process::current()).
Blocked (3): Sleeping on a WaitQueue (WaitQueue::sleep()) waiting for I/O, child exit (sys_wait4), or a signal wakeup (send_signal).
Stopped (4): Stopped by a stop signal (SIG_STOP, SIG_TSTP, SIG_TTIN, SIG_TTOU) in deliver_pending_signals; can be resumed by SIG_CONT (send_signal re-enqueues the process and marks continued).
Zombie (5): Set by exit_current_process(int64_t exit_code) (Process.cpp), which records exit_status, closes proc->fds().destroy(), clears clear_child_tid, reparents orphaned children to PID 1 (or 0 if PID 1 is absent), sends SIG_CHLD and wakes child_wait_queue() on the parent (or PID 1 if an adopted child is already Zombie), removes the process from the runqueue, and calls Scheduler::schedule(). Process::destroy(*proc) reaps a Zombie (or unused/stopped/unscheduled) process, frees its kernel stack, user stack (if not already freed with an invalid AddressSpace), AddressSpace, and FdTable, and frees the Process object.
arch/aarch64-linux-gnu/boot.s, arch/aarch64-linux-gnu/cpu.cpp, kernel/hal/cpu.hpp)Milo separates EL1 kernel-thread/task switching (HAL::KernelContext, 112 bytes) from EL0 user trap/execution frames (HAL::CpuContext, 288 bytes):
Kernel Context Switch (cpu_switch_context in boot.s, called by AArch64CpuControl::switch_kernel_context): If x0 (prev) is non-null, saves callee-saved registers x19–x28, frame pointer x29, link register x30, and sp into prev; then loads x19–x30 and sp from x1 (next) and executes ret to resume next in EL1.
New Kernel Thread Bootstrap (kernel_thread_trampoline in boot.s): Configured by HAL::Test::init_kernel_thread_context (arch_test.cpp) with x19 = entry, x20 = arg, x30 = &kernel_thread_trampoline, and 16-byte aligned sp. On first switch-in, the trampoline unmasks IRQs (msr daifclr, #2), passes x0 = x20, calls blr x19, and on return calls kthread_exit_trampoline -> Kernel::Process::exit_current_process(0).
New User Process Kernel Stack (AArch64CpuControl::init_user_kstack in cpu.cpp): Copies user_ctx (HAL::CpuContext) onto the top of the kernel stack (kstack_top - sizeof(HAL::CpuContext)), sets arch_kctx->x30 = &user_trap_exit and arch_kctx->sp = frame_addr. When switched in via cpu_switch_context, ret jumps to user_trap_exit (boot.s), which restores sp_el0, elr_el1, spsr_el1, tpidr_el0, and x0–x30 and executes eret into EL0.
Direct / Legacy EL0 Context Helpers (enter_user_mode, enter_user_context, return_to_kernel in boot.s): Save EL1 callee-saved registers into global kernel_ctx and eret into EL0 (spsr_el1 = 0x0 for EL0t); return_to_kernel restores kernel_ctx and returns to the caller.
kernel/process/scheduler.hpp, kernel/process/scheduler.cpp)Policy: FIFO doubly-linked Runqueue (enqueue, dequeue, remove, empty, size) with round-robin timeslices (DEFAULT_TIMESLICE_TICKS = 4U).
Atomic Guard (Scheduler::schedule()): Requires interrupts to be disabled (!cpu.interrupts_enabled()) and guards against re-entry (!g_in_schedule), panicking via scheduler_atomic_panic if violated. Re-enqueues a still-Running prev process when other runnable tasks exist, dequeues the next Ready process, initializes its kernel stack if needed, marks it Running, activates its AddressSpace if valid, and calls cpu.switch_kernel_context.
Timer Tick (Scheduler::on_timer_tick(bool from_user)): Increments g_timer_ticks, charges m_utime (when from_user is true) or m_stime (when from EL1) on the current Running process, and when tick_timeslice() reaches zero, resets the timeslice and calls schedule().
Cooperative Yield & Idle Wait (Scheduler::yield(), Scheduler::run_until_idle(), Process::execute()): yield() runs schedule() under IrqGuard. run_until_idle() loops while has_any_active_process() (Ready, Running, or Blocked) is true, calling schedule() when the runqueue is non-empty or briefly enabling IRQs and halting (cpu.halt() / wfi) until an interrupt wakes a task. Process::execute() enqueues the process and runs Scheduler::run_until_idle() until the target process reaches Zombie or no non-zombie processes remain.
SMP / Multi-Core ([Unimplemented]): Scheduling and process/runqueue state target a single active CPU execution flow (GICv2 SPI routing defaults to CPU 0 in gicv2.cpp, and uname reports #1 SMP PREEMPT in TimeSyscalls.cpp), without multi-core runqueues or cross-CPU IPI scheduling.