Rename initialization methods

This commit is contained in:
Ruihan Li 2025-11-15 10:10:59 +08:00 committed by Junyang Zhang
parent 19b1fe36c5
commit 69dc2479db
6 changed files with 59 additions and 35 deletions

View File

@ -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!()
}

View File

@ -26,14 +26,15 @@ use crate::{
/// The [`IrqChip`] singleton.
pub static IRQ_CHIP: Once<IrqChip> = 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() };

View File

@ -19,13 +19,13 @@ impl HwCpuId {
pub(in crate::arch) static IPI_IRQ: Once<IrqLine> = 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() };

View File

@ -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.

View File

@ -20,14 +20,15 @@ pub(super) static TIMER_IRQ: Once<IrqLine> = 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,

View File

@ -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();
}