From 68a2145055dfbdf8a7038af7d202602f54d07a44 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 1 Dec 2022 09:55:44 -0800 Subject: [PATCH] refac: move timer static from `arch` to `rt` (#375) --- src/arch/x86_64.rs | 4 ---- src/arch/x86_64/interrupt.rs | 7 ++----- src/arch/x86_64/shell.rs | 5 ----- src/lib.rs | 3 +++ src/rt.rs | 16 ++++++++++++++-- src/shell.rs | 8 ++++---- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index 2be21014..0da26af7 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -25,10 +25,6 @@ mod tests; pub type MinPageSize = mm::size::Size4Kb; -pub fn tick_timer() { - interrupt::TIMER.advance_ticks(0); -} - #[cfg(target_os = "none")] bootloader::entry_point!(arch_entry); diff --git a/src/arch/x86_64/interrupt.rs b/src/arch/x86_64/interrupt.rs index 71d69e46..9fdf0eb6 100644 --- a/src/arch/x86_64/interrupt.rs +++ b/src/arch/x86_64/interrupt.rs @@ -25,8 +25,6 @@ pub fn enable_hardware_interrupts(acpi: Option<&acpi::InterruptModel>) { controller .start_periodic_timer(TIMER_INTERVAL) .expect("10ms should be a reasonable interval for the PIT or local APIC timer..."); - time::set_global_timer(&TIMER) - .expect("`enable_hardware_interrupts` should only be called once!"); tracing::info!(granularity = ?TIMER_INTERVAL, "global timer initialized") } @@ -56,8 +54,7 @@ static TSS: sync::Lazy = sync::Lazy::new(|| { pub(in crate::arch) static GDT: sync::InitOnce = sync::InitOnce::uninitialized(); -const TIMER_INTERVAL: time::Duration = time::Duration::from_millis(10); -pub(super) static TIMER: time::Timer = time::Timer::new(TIMER_INTERVAL); +pub const TIMER_INTERVAL: time::Duration = time::Duration::from_millis(10); static TEST_INTERRUPT_WAS_FIRED: AtomicUsize = AtomicUsize::new(0); @@ -96,7 +93,7 @@ impl hal_core::interrupt::Handlers for InterruptHandlers { } fn timer_tick() { - TIMER.pend_ticks(1); + crate::rt::TIMER.pend_ticks(1); } fn ps2_keyboard(scancode: u8) { diff --git a/src/arch/x86_64/shell.rs b/src/arch/x86_64/shell.rs index 1d488d6e..6bc31d76 100644 --- a/src/arch/x86_64/shell.rs +++ b/src/arch/x86_64/shell.rs @@ -18,8 +18,3 @@ pub const DUMP_ARCH: Command = Command::new("arch") Ok(()) }), ]); - -pub fn dump_timer(_: &str) -> Result<(), shell::Error> { - tracing::info!(timer = ?super::interrupt::TIMER); - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index afd14063..a093000e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,6 +147,9 @@ pub fn kernel_start(bootinfo: impl BootInfo, archinfo: crate::arch::ArchInfo) -> // tracing. arch::init(&bootinfo, &archinfo); + // initialize the kernel runtime. + rt::init(); + #[cfg(test)] arch::run_tests(); diff --git a/src/rt.rs b/src/rt.rs index 793ee76a..7ad402fd 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -5,7 +5,10 @@ use core::{ future::Future, sync::atomic::{AtomicBool, AtomicUsize, Ordering::*}, }; -use maitake::scheduler::{self, StaticScheduler, Stealer}; +use maitake::{ + scheduler::{self, StaticScheduler, Stealer}, + time, +}; use mycelium_util::sync::InitOnce; use rand::Rng; @@ -49,6 +52,8 @@ struct Runtime { /// 512 CPU cores ought to be enough for anybody... pub const MAX_CORES: usize = 512; +pub static TIMER: time::Timer = time::Timer::new(arch::interrupt::TIMER_INTERVAL); + static RUNTIME: Runtime = { // This constant is used as an array initializer; the clippy warning that it // contains interior mutability is not actually a problem here, since we @@ -83,6 +88,13 @@ where }) } +/// Initialize the kernel runtime. +pub fn init() { + time::set_global_timer(&TIMER).expect("`rt::init` should only be called once!"); + + tracing::info!("kernel runtime initialized"); +} + static SCHEDULER: arch::LocalKey>> = arch::LocalKey::new(|| Cell::new(None)); @@ -108,7 +120,7 @@ impl Core { // turn the timer wheel if it wasn't turned recently and no one else is // holding a lock, ensuring any pending timer ticks are consumed. - arch::tick_timer(); + TIMER.advance_ticks(0); // if there are remaining tasks to poll, continue without stealing. if tick.has_remaining { diff --git a/src/shell.rs b/src/shell.rs index 4d223f4a..968695d0 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -42,10 +42,10 @@ pub fn eval(line: &str) { .with_fn(|line| Err(Error::other(line, "not yet implemented"))), Command::new("timer") .with_help("print the timer wheel") - .with_fn(crate::arch::shell::dump_timer), - Command::new("timer") - .with_help("print the timer wheel") - .with_fn(crate::arch::shell::dump_timer), + .with_fn(|_| { + tracing::info!(timer = ?crate::rt::TIMER); + Ok(()) + }), crate::arch::shell::DUMP_ARCH, Command::new("heap") .with_help("print kernel heap statistics")