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