Skip to main content

kernel/cpu/
state.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Per-CPU state and SMP topology placeholder for [`KState`].
3//!
4//! Authors: MarioS271
5
6use crate::lib::sync::niche_cell::NicheCell;
7use alloc::boxed::Box;
8
9#[cfg(target_arch = "x86_64")]
10use crate::arch::x86_64::cpu::state::*;
11
12// TODO: add getter for cpu_state(cpu_index)
13
14/// Per-CPU descriptor tables: one TSS and GDT per CPU, plus the shared IDT.
15#[repr(align(64))]
16pub struct Cpu {
17    global_cpu_state: GlobalCpuState,
18    bsp_cpu_state: CpuState,
19    ap_cpu_states: NicheCell<Box<[CpuState]>>
20}
21
22impl Cpu {
23    /// Construct with a default TSS and GDT for every CPU and a fresh IDT.
24    pub const fn new() -> Self {
25        Self {
26            global_cpu_state: GlobalCpuState::new(),
27            bsp_cpu_state: CpuState::new(),
28            ap_cpu_states: NicheCell::new(),
29        }
30    }
31
32    pub unsafe fn init_ap_cpu_states(&self, value: Box<[CpuState]>) {
33        unsafe { self.ap_cpu_states.init(value) };
34    }
35
36    /// Getter for `Cpu::global_cpu_state`
37    pub fn global_cpu_state(&self) -> &GlobalCpuState {
38        &self.global_cpu_state
39    }
40
41    /// Getter for `Cpu::bsp_cpu_state`
42    pub fn bsp_cpu_state(&self) -> &CpuState {
43        &self.bsp_cpu_state
44    }
45
46    /// Getter for `Cpu::ap_cpu_states`
47    ///
48    /// # Safety
49    /// The caller must guarantee that at this method's entire execution time,
50    /// no mutable references or pointers to this data exist or will exist
51    pub unsafe fn ap_cpu_states(&self) -> &Option<Box<[CpuState]>> {
52        unsafe { self.ap_cpu_states.get() }
53    }
54}