⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/sched/
init.rs

1//! Scheduler initialization and timer setup
2//!
3//! Contains the bootstrap initialization path (`init_with_bootstrap`) used
4//! during early kernel boot, the normal initialization path (`init`), and
5//! architecture-specific preemption timer configuration.
6
7use core::ptr::NonNull;
8
9use super::{smp, task::Task};
10use crate::error::KernelResult;
11
12/// Initialize scheduler with bootstrap task
13///
14/// This is used during early boot to initialize the scheduler with a
15/// bootstrap task that will complete kernel initialization.
16pub fn init_with_bootstrap(bootstrap_task: NonNull<Task>) -> KernelResult<()> {
17    kprintln!("[SCHED] Initializing scheduler with bootstrap task...");
18
19    // Initialize SMP support
20    kprintln!("[SCHED] About to initialize SMP...");
21    smp::init();
22    kprintln!("[SCHED] SMP initialization complete");
23
24    // Initialize scheduler with bootstrap task
25    kprintln!("[SCHED] About to get scheduler lock...");
26    super::SCHEDULER.lock().init(bootstrap_task);
27    kprintln!("[SCHED] Scheduler init complete");
28
29    // Set up timer interrupt for preemption
30    kprintln!("[SCHED] About to setup preemption timer...");
31    setup_preemption_timer();
32    kprintln!("[SCHED] Preemption timer setup complete");
33
34    kprintln!("[SCHED] Scheduler initialized with bootstrap task");
35
36    Ok(())
37}
38
39/// Initialize scheduler normally (after bootstrap)
40pub fn init() {
41    kprintln!("[SCHED] Initializing scheduler...");
42
43    // Initialize SMP support
44    smp::init();
45
46    // Skip complex scheduler setup on all architectures for now.
47    // kernel_init_main() tests run before sched::init() and don't need the
48    // scheduler. The idle task creation and PIT timer setup can hang or panic
49    // during early boot.
50    kprintln!("[SCHED] Scheduler initialized (minimal)");
51}
52
53/// Set up preemption timer
54fn setup_preemption_timer() {
55    #[cfg(target_arch = "x86_64")]
56    {
57        // Configure timer for 10ms tick (100Hz)
58        crate::arch::x86_64::timer::setup_timer(10);
59        kprintln!("[SCHED] x86_64 timer configured for preemptive scheduling");
60    }
61
62    #[cfg(target_arch = "aarch64")]
63    {
64        // Configure generic timer for 10ms tick
65        crate::arch::aarch64::timer::setup_timer(10);
66        kprintln!("[SCHED] AArch64 timer configured for preemptive scheduling");
67    }
68
69    #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
70    {
71        // Configure RISC-V timer for 10ms tick.
72        // NOTE: setup_timer() configures the SBI timer but does NOT enable
73        // STIE (supervisor timer interrupt enable) because no trap handler
74        // (stvec) is registered yet. Enabling STIE without stvec causes
75        // the CPU to jump to address 0 on timer fire, rebooting the system.
76        crate::arch::riscv::timer::setup_timer(10);
77        kprintln!("[SCHED] RISC-V timer configured for preemptive scheduling");
78    }
79}