From 69dc2479dbdfe3953c7f88539115d473434a8549 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Sat, 15 Nov 2025 10:10:59 +0800 Subject: [PATCH] Rename initialization methods --- ostd/src/arch/loongarch/mod.rs | 8 ++++++++ ostd/src/arch/riscv/irq/chip/mod.rs | 9 +++++---- ostd/src/arch/riscv/irq/ipi.rs | 15 ++++++++------- ostd/src/arch/riscv/mod.rs | 27 ++++++++++++++------------- ostd/src/arch/riscv/timer/mod.rs | 25 +++++++++++++++++++------ ostd/src/arch/x86/mod.rs | 10 +++++----- 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/ostd/src/arch/loongarch/mod.rs b/ostd/src/arch/loongarch/mod.rs index c22153ff5..46a37762b 100644 --- a/ostd/src/arch/loongarch/mod.rs +++ b/ostd/src/arch/loongarch/mod.rs @@ -51,6 +51,14 @@ pub(crate) unsafe fn late_init_on_bsp() { unsafe { crate::io::init(io_mem_builder) }; } +/// Initializes application-processor-specific state. +/// +/// # Safety +/// +/// 1. This function must be called only once on each application processor. +/// 2. This function must be called after the BSP's call to [`late_init_on_bsp`] +/// and before any other architecture-specific code in this module is called +/// on this AP. pub(crate) unsafe fn init_on_ap() { unimplemented!() } diff --git a/ostd/src/arch/riscv/irq/chip/mod.rs b/ostd/src/arch/riscv/irq/chip/mod.rs index c06a044d8..6c66a468c 100644 --- a/ostd/src/arch/riscv/irq/chip/mod.rs +++ b/ostd/src/arch/riscv/irq/chip/mod.rs @@ -26,14 +26,15 @@ use crate::{ /// The [`IrqChip`] singleton. pub static IRQ_CHIP: Once = Once::new(); -/// Initializes the Platform-Level Interrupt Controller (PLIC). +/// Initializes the Platform-Level Interrupt Controller (PLIC) on the BSP. /// /// # Safety /// /// This function is safe to call on the following conditions: -/// 1. It is called once and at most once at a proper timing in the boot context. +/// 1. It is called once and at most once at a proper timing in the boot context +/// of the BSP. /// 2. It is called before any other public functions of this module is called. -pub(in crate::arch) unsafe fn init(io_mem_builder: &IoMemAllocatorBuilder) { +pub(in crate::arch) unsafe fn init_on_bsp(io_mem_builder: &IoMemAllocatorBuilder) { let device_tree = DEVICE_TREE.get().unwrap(); let mut plics = Plic::from_fdt(device_tree, io_mem_builder); plics.iter_mut().for_each(|plic| plic.init()); @@ -55,7 +56,7 @@ pub(in crate::arch) unsafe fn init(io_mem_builder: &IoMemAllocatorBuilder) { /// 1. It is called once and at most once on this AP. /// 2. It is called before any other public functions of this module is called /// on this AP. -pub(in crate::arch) unsafe fn init_current_hart() { +pub(in crate::arch) unsafe fn init_on_ap() { // SAFETY: Accessing the `sie` CSR to enable the external interrupt is safe // here due to the same reasons mentioned in `init`. unsafe { riscv::register::sie::set_sext() }; diff --git a/ostd/src/arch/riscv/irq/ipi.rs b/ostd/src/arch/riscv/irq/ipi.rs index ed6333442..476c40816 100644 --- a/ostd/src/arch/riscv/irq/ipi.rs +++ b/ostd/src/arch/riscv/irq/ipi.rs @@ -19,13 +19,13 @@ impl HwCpuId { pub(in crate::arch) static IPI_IRQ: Once = Once::new(); -/// Initializes the global IPI-related state and local state on BSP. +/// Initializes the global IPI-related state and local state on the BSP. /// /// # Safety /// -/// This function can only be called before any other IPI-related function is -/// called. -pub(in crate::arch) unsafe fn init() { +/// This function can only be called on the BSP and before any other +/// IPI-related function is called. +pub(in crate::arch) unsafe fn init_on_bsp() { let mut irq = IrqLine::alloc().unwrap(); // SAFETY: This will be called upon an inter-processor interrupt. irq.on_active(|f| unsafe { crate::smp::do_inter_processor_call(f) }); @@ -37,12 +37,13 @@ pub(in crate::arch) unsafe fn init() { unsafe { riscv::register::sie::set_ssoft() }; } -/// Initializes the IPI-related state on this CPU. +/// Initializes the IPI-related state on this AP. /// /// # Safety /// -/// This function can only be called before any other harts can IPI this hart. -pub(in crate::arch) unsafe fn init_current_hart() { +/// This function can only be called before any other harts can send IPIs to +/// this application hart. +pub(in crate::arch) unsafe fn init_on_ap() { // SAFETY: Enabling the software interrupts is safe here due to the same // reasons mentioned in `init`. unsafe { riscv::register::sie::set_ssoft() }; diff --git a/ostd/src/arch/riscv/mod.rs b/ostd/src/arch/riscv/mod.rs index b8d521831..31a9c474e 100644 --- a/ostd/src/arch/riscv/mod.rs +++ b/ostd/src/arch/riscv/mod.rs @@ -43,15 +43,16 @@ pub(crate) unsafe fn late_init_on_bsp() { // SAFETY: This function is called once and at most once at a proper timing // in the boot context of the BSP, with no external interrupt-related // operations having been performed. - unsafe { irq::chip::init(&io_mem_builder) }; + unsafe { irq::chip::init_on_bsp(&io_mem_builder) }; - // SAFETY: This is called before any IPI-related operation is performed. - unsafe { irq::ipi::init() }; + // SAFETY: This is called on the BSP and before any IPI-related operation is + // performed. + unsafe { irq::ipi::init_on_bsp() }; // SAFETY: This function is called once and at most once at a proper timing // in the boot context of the BSP, with no timer-related operations having // been performed. - unsafe { timer::init() }; + unsafe { timer::init_on_bsp() }; // SAFETY: We're on the BSP and we're ready to boot all APs. unsafe { crate::boot::smp::boot_all_aps() }; @@ -69,22 +70,22 @@ pub(crate) unsafe fn late_init_on_bsp() { /// /// # Safety /// -/// This function must be called only once on each application processor. -/// And it should be called after the BSP's call to [`late_init_on_bsp`] -/// and before any other architecture-specific code in this module is called on -/// this AP. +/// 1. This function must be called only once on each application processor. +/// 2. This function must be called after the BSP's call to [`late_init_on_bsp`] +/// and before any other architecture-specific code in this module is called +/// on this AP. pub(crate) unsafe fn init_on_ap() { // SAFETY: The safety is upheld by the caller. unsafe { trap::init_on_cpu() }; // SAFETY: The safety is upheld by the caller. - unsafe { irq::chip::init_current_hart() }; + unsafe { irq::chip::init_on_ap() }; - // SAFETY: This is called before any IPI-related operation is performed. - unsafe { irq::ipi::init_current_hart() }; + // SAFETY: This is called before any harts can send IPIs to this AP. + unsafe { irq::ipi::init_on_ap() }; - // SAFETY: The caller ensures that this function is only called once here. - unsafe { timer::init_current_hart() }; + // SAFETY: The caller ensures that this is only called once on this AP. + unsafe { timer::init_on_ap() }; } /// Returns the frequency of TSC. The unit is Hz. diff --git a/ostd/src/arch/riscv/timer/mod.rs b/ostd/src/arch/riscv/timer/mod.rs index 48b6ea588..73916876b 100644 --- a/ostd/src/arch/riscv/timer/mod.rs +++ b/ostd/src/arch/riscv/timer/mod.rs @@ -20,14 +20,15 @@ pub(super) static TIMER_IRQ: Once = Once::new(); static TIMEBASE_FREQ: AtomicU64 = AtomicU64::new(0); static TIMER_INTERVAL: AtomicU64 = AtomicU64::new(0); -/// Initializes the timer module. +/// Initializes the timer module on the BSP. /// /// # Safety /// /// This function is safe to call on the following conditions: -/// 1. It is called once and at most once at a proper timing in the boot context. +/// 1. It is called once and at most once at a proper timing in the boot context +/// of the BSP. /// 2. It is called before any other public functions of this module is called. -pub(super) unsafe fn init() { +pub(super) unsafe fn init_on_bsp() { TIMEBASE_FREQ.store( DEVICE_TREE .get() @@ -58,7 +59,19 @@ pub(super) unsafe fn init() { timer_irq }); - // SAFETY: The caller ensures that this is only called once on the boot hart. + // SAFETY: The caller ensures that this is only called once on the + // bootstrapping hart. + unsafe { init_current_hart() }; +} + +/// Initializes the timer on this AP. +/// +/// # Safety +/// +/// This function must be called on an AP that hasn't called this function. +pub(super) unsafe fn init_on_ap() { + // SAFETY: The caller ensures that this is only called once on the + // current application hart. unsafe { init_current_hart() }; } @@ -66,8 +79,8 @@ pub(super) unsafe fn init() { /// /// # Safety /// -/// This function must be called in a hart that hasn't called this function. -pub(super) unsafe fn init_current_hart() { +/// This function must be called on a hart that hasn't called this function. +unsafe fn init_current_hart() { set_next_timer(); // SAFETY: Accessing the `sie` CSR to enable the timer interrupt is safe // here because this function is only called during timer initialization, diff --git a/ostd/src/arch/x86/mod.rs b/ostd/src/arch/x86/mod.rs index a343e6700..19dcdb387 100644 --- a/ostd/src/arch/x86/mod.rs +++ b/ostd/src/arch/x86/mod.rs @@ -83,14 +83,14 @@ pub(crate) unsafe fn late_init_on_bsp() { unsafe { crate::io::init(io_mem_builder) }; } -/// Architecture-specific initialization on the application processor. +/// Initializes application-processor-specific state. /// /// # Safety /// -/// This function must be called only once on each application processor. -/// And it should be called after the BSP's call to [`init_on_bsp`]. -/// -/// [`init_on_bsp`]: crate::cpu::init_on_bsp +/// 1. This function must be called only once on each application processor. +/// 2. This function must be called after the BSP's call to [`late_init_on_bsp`] +/// and before any other architecture-specific code in this module is called +/// on this AP. pub(crate) unsafe fn init_on_ap() { timer::init_on_ap(); }