Skip to main content

ax_plat/
percpu.rs

1//! CPU-local data structures and accessors.
2
3#[ax_percpu::def_percpu]
4static CPU_ID: usize = 0;
5
6#[ax_percpu::def_percpu]
7static IS_BSP: bool = false;
8
9/// Returns the ID of the current CPU.
10#[inline]
11pub fn this_cpu_id() -> usize {
12    CPU_ID.read_current()
13}
14
15/// Returns whether the current CPU is the primary CPU (aka the bootstrap
16/// processor or BSP)
17#[inline]
18pub fn this_cpu_is_bsp() -> bool {
19    IS_BSP.read_current()
20}
21
22/// Initializes CPU-local data structures for the primary core.
23///
24/// This function should be called as early as possible, as other
25/// initializations may access the CPU-local data.
26pub fn init_primary(cpu_id: usize) {
27    ax_percpu::init();
28    ax_percpu::init_percpu_reg(cpu_id);
29    unsafe {
30        CPU_ID.write_current_raw(cpu_id);
31        IS_BSP.write_current_raw(true);
32    }
33}
34
35/// Initializes CPU-local data structures for secondary cores.
36///
37/// This function should be called as early as possible, as other
38/// initializations may access the CPU-local data.
39#[cfg(feature = "smp")]
40pub fn init_secondary(cpu_id: usize) {
41    ax_percpu::init_percpu_reg(cpu_id);
42    unsafe {
43        CPU_ID.write_current_raw(cpu_id);
44        IS_BSP.write_current_raw(false);
45    }
46}