Skip to main content

current_cpu_index

Function current_cpu_index 

Source
pub fn current_cpu_index() -> usize
Expand description

Resolve current CPU index via a GS-relative load from offset 0.

Hot path (after init_gs_base has run on the BSP): a single non-serialising mov rax, gs:[0] — no MSR, no pipeline stall.

Slow/early-boot path (before init_gs_base is called the first time): reads IA32_GS_BASE via rdmsr as a null-guard; if the GS base is 0 (hardware reset value, before our wrmsr) the function returns 0 (BSP slot) without touching GS. Once the BSP calls init_gs_base, the slow path is never taken again.

Corrupt-GS defence: the returned index is clamped to [0, MAX_CPUS-1] so a bogus GS value can never produce an out-of-bounds array access.

Invariant for APs: smp_main always calls init_gs_base before the first current_cpu_index() on each AP, so the hot path is safe for APs.