Repository navigation
4.2 Scheduler and Task Lifecycle
Relevant source files
- src/kernel/arch_utils.s
- src/kernel/drivers/mouse.zig
- src/kernel/fs/ata_block_device.zig
- src/kernel/fs/coda_file.zig
- src/kernel/fs/coda_sm.zig
- src/kernel/interrupts/irq_stubs.asm
- src/kernel/scheduler.zig
This page details the implementation of CadenzaOS's cooperative and preemptive task scheduling subsystem src/kernel/scheduler.zig1-11 It covers task state definitions, register context structures (TaskContext, InterruptContext) src/kernel/scheduler.zig36-88 dynamic task registration, interrupt-driven preemption via irq0_stubsrc/kernel/interrupts/irq_stubs.asm105-160 and low-level cooperative context switching via switch_taskssrc/kernel/arch_utils.s23-85
Sources: src/kernel/scheduler.zig1-11, src/kernel/scheduler.zig36-88, src/kernel/interrupts/irq_stubs.asm105-160, src/kernel/arch_utils.s23-85
CadenzaOS defines explicit representations for task execution states and processor register frames within src/kernel/scheduler.zig.
-
TaskState: An enumeration of possible task execution conditions (Ready,Running,Suspended, andDead) src/kernel/scheduler.zig36-50 -
TaskContext: A packed struct representing the register layout preserved during cooperative context switches, encompassing general-purpose registers (r15down torax) and the instruction pointer (rip) src/kernel/scheduler.zig52-62 -
InterruptContext: A packed struct capturing complete processor state during interrupt-driven switches, combining GPRs saved by assembly stubs with the hardware trap frame (rip,cs,rflags,rsp,ss) pushed automatically by the CPU src/kernel/scheduler.zig64-88 -
Task: The core scheduler-owned task record containing its unique identifier (id), saved stack pointer (stack_ptr), currentTaskState,wake_tick, and backing allocations for stack and code memory src/kernel/scheduler.zig90-113 -
resolveUserRange: A utility function within the scheduler to translate a task-supplied address range into a kernel-virtual slice. It handles various address forms including physical, higher-half virtual, and bare offsets within the task's code or stack memory src/kernel/scheduler.zig132-175

Figure 4.2.1: Mapping between natural language lifecycle states and TaskState enum variants in src/kernel/scheduler.zig.
Sources: src/kernel/scheduler.zig36-113, src/kernel/scheduler.zig132-175
The Scheduler struct maintains the master task table (tasks), the active task index (current_task_idx), preemption flags (yield_enabled), and a global tick counter (ticks) src/kernel/scheduler.zig178-194
-
Scheduler.init: Allocates and returns an empty scheduler instance with all task slots initialized tonullsrc/kernel/scheduler.zig201-208 -
registerCurrentThreadAsTask: Converts the initial kernel execution context into a scheduler-managed task (slot 0) by allocating contiguous stack pages via frame allocators without forcing an immediate context switch src/kernel/scheduler.zig210-230 -
registerDynamicTask: Validates argument size limits (MAX_ARGS,MAX_ARG_BYTES), finds an unused task slot, assigns a unique identifier, allocates dedicated stack pages, and builds an initial task record src/kernel/scheduler.zig29-35src/kernel/scheduler.zig231-300
Sources: src/kernel/scheduler.zig29-35, src/kernel/scheduler.zig178-300
Timer-driven preemption relies on hardware interrupt 0 (IRQ0). The low-level assembly stub irq0_stub preserves general-purpose registers to match TaskContext, invokes the timer handler and scheduler preemption handler preempt_handler, and restores the next task's execution environment src/kernel/interrupts/irq_stubs.asm105-160

Figure 4.2.2: Execution flow mapping hardware timer preemption from irq0_stub through preempt_handler.
Sources: src/kernel/interrupts/irq_stubs.asm105-160, src/kernel/scheduler.zig1-11
Low-level context switching between threads is performed by the assembly routine switch_tasks defined in arch_utils.ssrc/kernel/arch_utils.s23-85
-
Parameters (System V AMD64 ABI):
-
%rdi: Pointer to storage for the outgoing task's stack pointer (old_rsp) src/kernel/arch_utils.s25-28 -
%rsi: The pre-saved stack pointer of the incoming task (new_rsp) src/kernel/arch_utils.s25-29 -
Execution Steps:
- Saves all general-purpose registers (
raxthroughr15) onto the current task stack src/kernel/arch_utils.s33-53 - Stores the active stack pointer into
*old_rsp((%rdi)) and loadsnew_rsp(%rsi) into%rspsrc/kernel/arch_utils.s55-58 - Pops general-purpose registers in reverse order from the new task stack src/kernel/arch_utils.s60-78
- Executes a standard
retinstruction to transfer control to the restored task's instruction pointer src/kernel/arch_utils.s80-85
Sources: src/kernel/arch_utils.s23-85
- Overview
- Getting Started and Build System
- Kernel Configuration and Global State
- Project Governance and Contributing
- Boot and Early Kernel Initialization
- Bootloader Stages and Linking
- kmain Boot Sequence
- Splash Screen and Branding
- Interrupts, Scheduling, and Syscalls
- IDT, PIC, APIC and IRQ Stubs
- Scheduler and Task Lifecycle
- System Call Interface
- Demonstration Tasks
- CodaFS Filesystem and Storage
- On-Disk Format and Operations
- Space Manager and Block Devices
- Binary Format and Program Loading
- Shell, Terminal, and Display
- Shell Command Pipeline
- Terminal, Input Devices and Prompts
- Display: VGA, Framebuffer, Console and Serial