From 491e4325fa127c6ad68c99abd394d58de7c8e348 Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Mon, 12 Aug 2024 13:01:01 +0000 Subject: [PATCH] Enable IRQs before leaving the OSTD boot routine --- kernel/aster-nix/src/lib.rs | 3 - .../src/process/process/timer_manager.rs | 69 ++++++++++--------- kernel/aster-nix/src/taskless.rs | 3 +- ostd/src/boot/smp.rs | 1 + ostd/src/lib.rs | 2 + ostd/src/trap/irq.rs | 15 ---- ostd/src/trap/mod.rs | 4 +- 7 files changed, 42 insertions(+), 55 deletions(-) diff --git a/kernel/aster-nix/src/lib.rs b/kernel/aster-nix/src/lib.rs index ac2872b28..754fe9a85 100644 --- a/kernel/aster-nix/src/lib.rs +++ b/kernel/aster-nix/src/lib.rs @@ -89,9 +89,6 @@ fn init_thread() { // Work queue should be initialized before interrupt is enabled, // in case any irq handler uses work queue as bottom half thread::work_queue::init(); - // FIXME: Remove this if we move the step of mounting - // the filesystems to be done within the init process. - ostd::trap::enable_local(); net::lazy_init(); fs::lazy_init(); // driver::pci::virtio::block::block_device_test(); diff --git a/kernel/aster-nix/src/process/process/timer_manager.rs b/kernel/aster-nix/src/process/process/timer_manager.rs index 4edacba33..fc0d0e519 100644 --- a/kernel/aster-nix/src/process/process/timer_manager.rs +++ b/kernel/aster-nix/src/process/process/timer_manager.rs @@ -18,12 +18,14 @@ use ostd::{ use super::Process; use crate::{ - prelude::*, process::{ posix_thread::PosixThreadExt, signal::{constants::SIGALRM, signals::kernel::KernelSignal}, }, - thread::work_queue::{submit_work_item, work_item::WorkItem}, + thread::{ + work_queue::{submit_work_item, work_item::WorkItem}, + Thread, + }, time::{ clocks::{ProfClock, RealTimeClock}, Timer, TimerManager, @@ -36,40 +38,43 @@ use crate::{ /// invoke the callbacks of expired timers which are based on the updated /// CPU clock. fn update_cpu_time() { - let current_thread = current_thread!(); - if let Some(posix_thread) = current_thread.as_posix_thread() { - let process = posix_thread.process(); - let timer_manager = process.timer_manager(); - let jiffies_interval = Duration::from_millis(1000 / TIMER_FREQ); - // Based on whether the timer interrupt occurs in kernel mode or user mode, - // the function will add the duration of one timer interrupt interval to the - // corresponding CPU clocks. - if is_kernel_interrupted() { - posix_thread - .prof_clock() - .kernel_clock() - .add_time(jiffies_interval); - process - .prof_clock() - .kernel_clock() - .add_time(jiffies_interval); - } else { - posix_thread - .prof_clock() - .user_clock() - .add_time(jiffies_interval); - process.prof_clock().user_clock().add_time(jiffies_interval); - timer_manager - .virtual_timer() - .timer_manager() - .process_expired_timers(); - } + let Some(current_thread) = Thread::current() else { + return; + }; + let Some(posix_thread) = current_thread.as_posix_thread() else { + return; + }; + let process = posix_thread.process(); + let timer_manager = process.timer_manager(); + let jiffies_interval = Duration::from_millis(1000 / TIMER_FREQ); + // Based on whether the timer interrupt occurs in kernel mode or user mode, + // the function will add the duration of one timer interrupt interval to the + // corresponding CPU clocks. + if is_kernel_interrupted() { + posix_thread + .prof_clock() + .kernel_clock() + .add_time(jiffies_interval); + process + .prof_clock() + .kernel_clock() + .add_time(jiffies_interval); + } else { + posix_thread + .prof_clock() + .user_clock() + .add_time(jiffies_interval); + process.prof_clock().user_clock().add_time(jiffies_interval); timer_manager - .prof_timer() + .virtual_timer() .timer_manager() .process_expired_timers(); - posix_thread.process_expired_timers(); } + timer_manager + .prof_timer() + .timer_manager() + .process_expired_timers(); + posix_thread.process_expired_timers(); } /// Registers a function to update the CPU clock in processes and diff --git a/kernel/aster-nix/src/taskless.rs b/kernel/aster-nix/src/taskless.rs index 64ac8f82a..fc3f66894 100644 --- a/kernel/aster-nix/src/taskless.rs +++ b/kernel/aster-nix/src/taskless.rs @@ -190,7 +190,7 @@ fn taskless_softirq_handler( mod test { use core::sync::atomic::AtomicUsize; - use ostd::{prelude::*, trap::enable_local}; + use ostd::prelude::*; use super::*; @@ -198,7 +198,6 @@ mod test { static DONE: AtomicBool = AtomicBool::new(false); if !DONE.load(Ordering::SeqCst) { super::init(); - enable_local(); DONE.store(true, Ordering::SeqCst); } } diff --git a/ostd/src/boot/smp.rs b/ostd/src/boot/smp.rs index 05b938451..7748e3f63 100644 --- a/ostd/src/boot/smp.rs +++ b/ostd/src/boot/smp.rs @@ -119,6 +119,7 @@ fn ap_early_entry(local_apic_id: u32) -> ! { } trap::init(); + crate::arch::irq::enable_local(); // Mark the AP as started. let ap_boot_info = AP_BOOT_INFO.get().unwrap(); diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index 4c40790d6..215d57e4e 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -86,6 +86,8 @@ pub fn init() { mm::kspace::activate_kernel_page_table(); + arch::irq::enable_local(); + invoke_ffi_init_funcs(); } diff --git a/ostd/src/trap/irq.rs b/ostd/src/trap/irq.rs index 298f82c5f..e6dbe7d1f 100644 --- a/ostd/src/trap/irq.rs +++ b/ostd/src/trap/irq.rs @@ -172,18 +172,3 @@ impl Drop for DisabledLocalIrqGuard { } } } - -/// Enables all IRQs on the current CPU. -/// -/// FIXME: The reason we need to add this API is that currently IRQs -/// are enabled when the CPU enters the user space for the first time, -/// which is too late. During the OS initialization phase, -/// we need to get the block device working and mount the filesystems, -/// thus requiring the IRQs should be enabled as early as possible. -/// -/// FIXME: this method may be unsound. -pub fn enable_local() { - if !crate::arch::irq::is_local_enabled() { - crate::arch::irq::enable_local(); - } -} diff --git a/ostd/src/trap/mod.rs b/ostd/src/trap/mod.rs index 694b82592..358cf045c 100644 --- a/ostd/src/trap/mod.rs +++ b/ostd/src/trap/mod.rs @@ -11,9 +11,7 @@ pub use softirq::SoftIrqLine; pub use trapframe::TrapFrame; pub(crate) use self::handler::call_irq_callback_functions; -pub use self::irq::{ - disable_local, enable_local, DisabledLocalIrqGuard, IrqCallbackFunction, IrqLine, -}; +pub use self::irq::{disable_local, DisabledLocalIrqGuard, IrqCallbackFunction, IrqLine}; pub(crate) fn init() { unsafe {