ostd/cpu/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// SPDX-License-Identifier: MPL-2.0
//! CPU-related definitions.
mod id;
pub mod local;
pub use id::{all_cpus, num_cpus, AtomicCpuSet, CpuId, CpuIdFromIntError, CpuSet, PinCurrentCpu};
/// The CPU privilege level: user mode or kernel mode.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum PrivilegeLevel {
    /// User mode.
    User = 0,
    /// Kernel mode.
    Kernel = 1,
}
/// Initializes the CPU module (the BSP part).
///
/// # Safety
///
/// The caller must ensure that
/// 1. We're in the boot context of the BSP and APs have not yet booted.
/// 2. The number of CPUs is available.
/// 3. CPU-local storage has NOT been used.
pub(crate) unsafe fn init_on_bsp() {
    let num_cpus = crate::arch::boot::smp::count_processors().unwrap_or(1);
    // SAFETY:
    // 1. We're in the boot context of the BSP and APs have not yet booted.
    // 2. The number of CPUs is correct.
    // 3. CPU-local storage has NOT been used.
    unsafe { local::copy_bsp_for_ap(num_cpus as usize) };
    // For this point on, CPU-local storage on all CPUs are safe to use.
    // SAFETY:
    // 1. We're in the boot context of the BSP.
    // 2. The number of CPUs is correct.
    unsafe { id::init_on_bsp(num_cpus) };
}
/// Initializes the CPU module (the AP part).
///
/// # Safety
///
/// The caller must ensure that:
/// 1. We're in the boot context of an AP.
/// 2. The CPU ID of the AP is correct.
pub(crate) unsafe fn init_on_ap(cpu_id: u32) {
    // SAFETY:
    // 1. We're in the boot context of an AP.
    // 2. The CPU ID of the AP is correct.
    unsafe { id::init_on_ap(cpu_id) };
}