aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / pi / mp.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Multi-processor management protocols. |
| 4 | //! |
| 5 | //! On any system with more than one logical processor we can categorize them as: |
| 6 | //! |
| 7 | //! * BSP — bootstrap processor, executes modules that are necessary for booting the system |
| 8 | //! * AP — application processor, any processor other than the bootstrap processor |
| 9 | //! |
| 10 | //! This module contains protocols that provide a generalized way of performing the following tasks on these logical processors: |
| 11 | //! |
| 12 | //! * retrieving information of multi-processor environment and MP-related status of specific processors |
| 13 | //! * dispatching user-provided function to APs |
| 14 | //! * maintaining MP-related processor status |
| 15 | |
| 16 | use crate::data_types::Event; |
| 17 | use crate::proto::unsafe_protocol; |
| 18 | use crate::{Result, Status, StatusExt}; |
| 19 | use bitflags::bitflags; |
| 20 | use core::ffi::c_void; |
| 21 | use core::ptr; |
| 22 | use core::time::Duration; |
| 23 | |
| 24 | /// Callback to be called on the AP. |
| 25 | pub type Procedure = extern "efiapi" fn(*mut c_void); |
| 26 | |
| 27 | bitflags! { |
| 28 | /// Flags indicating if the processor is BSP or AP, |
| 29 | /// if the processor is enabled or disabled, and if |
| 30 | /// the processor is healthy. |
| 31 | #[repr(transparent)] |
| 32 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 33 | struct StatusFlag: u32 { |
| 34 | /// Processor is playing the role of BSP. |
| 35 | const PROCESSOR_AS_BSP_BIT = 1; |
| 36 | /// Processor is enabled. |
| 37 | const PROCESSOR_ENABLED_BIT = 1 << 1; |
| 38 | /// Processor is healthy. |
| 39 | const PROCESSOR_HEALTH_STATUS_BIT = 1 << 2; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Information about number of logical processors on the platform. |
| 44 | #[derive(Default, Debug)] |
| 45 | pub struct ProcessorCount { |
| 46 | /// Total number of processors (including BSP). |
| 47 | pub total: usize, |
| 48 | /// Number of processors (including BSP) that are currently enabled. |
| 49 | pub enabled: usize, |
| 50 | } |
| 51 | |
| 52 | /// Flag for [`MpServices::get_processor_info`] requesting the extended |
| 53 | /// topology in [`ProcessorInformation::extended_information`]. |
| 54 | /// |
| 55 | /// Corresponds to `CPU_V2_EXTENDED_TOPOLOGY` in the PI specification. |
| 56 | pub const CPU_V2_EXTENDED_TOPOLOGY: usize = 1 << 24; |
| 57 | |
| 58 | /// Information about processor on the platform. |
| 59 | #[repr(C)] |
| 60 | #[derive(Default, Debug)] |
| 61 | pub struct ProcessorInformation { |
| 62 | /// Unique processor ID determined by system hardware. |
| 63 | pub processor_id: u64, |
| 64 | /// Flags indicating BSP, enabled and healthy status. |
| 65 | status_flag: StatusFlag, |
| 66 | /// Physical location of the processor. |
| 67 | pub location: CpuPhysicalLocation, |
| 68 | /// Extended physical location of the processor. |
| 69 | /// |
| 70 | /// Only filled by the firmware if [`CPU_V2_EXTENDED_TOPOLOGY`] is set in |
| 71 | /// the processor number passed to [`MpServices::get_processor_info`]. |
| 72 | // The PI spec wraps this in EXTENDED_PROCESSOR_INFORMATION, a union with |
| 73 | // Location2 as its only member; the wrapper is skipped here. |
| 74 | pub extended_information: CpuPhysicalLocation2, |
| 75 | } |
| 76 | |
| 77 | impl ProcessorInformation { |
| 78 | /// Returns `true` if the processor is playing the role of BSP. |
| 79 | #[must_use] |
| 80 | pub const fn is_bsp(&self) -> bool { |
| 81 | self.status_flag.contains(StatusFlag::PROCESSOR_AS_BSP_BIT) |
| 82 | } |
| 83 | |
| 84 | /// Returns `true` if the processor is enabled. |
| 85 | #[must_use] |
| 86 | pub const fn is_enabled(&self) -> bool { |
| 87 | self.status_flag.contains(StatusFlag::PROCESSOR_ENABLED_BIT) |
| 88 | } |
| 89 | |
| 90 | /// Returns `true` if the processor is healthy. |
| 91 | #[must_use] |
| 92 | pub const fn is_healthy(&self) -> bool { |
| 93 | self.status_flag |
| 94 | .contains(StatusFlag::PROCESSOR_HEALTH_STATUS_BIT) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Information about physical location of the processor. |
| 99 | #[repr(C)] |
| 100 | #[derive(Default, Debug)] |
| 101 | pub struct CpuPhysicalLocation { |
| 102 | /// Zero-based physical package number that identifies |
| 103 | /// the cartridge of the processor. |
| 104 | pub package: u32, |
| 105 | /// Zero-based physical core number within package of the processor. |
| 106 | pub core: u32, |
| 107 | /// Zero-based logical thread number within core of the processor. |
| 108 | pub thread: u32, |
| 109 | } |
| 110 | |
| 111 | /// Information about the 6-level physical location of the processor. |
| 112 | /// |
| 113 | /// Corresponds to `EFI_CPU_PHYSICAL_LOCATION2` in the PI specification. |
| 114 | #[repr(C)] |
| 115 | #[derive(Default, Debug)] |
| 116 | pub struct CpuPhysicalLocation2 { |
| 117 | /// Zero-based physical package number that identifies |
| 118 | /// the cartridge of the processor. |
| 119 | pub package: u32, |
| 120 | /// Zero-based physical module number within package of the processor. |
| 121 | pub module: u32, |
| 122 | /// Zero-based physical tile number within module of the processor. |
| 123 | pub tile: u32, |
| 124 | /// Zero-based physical die number within tile of the processor. |
| 125 | pub die: u32, |
| 126 | /// Zero-based physical core number within die of the processor. |
| 127 | pub core: u32, |
| 128 | /// Zero-based logical thread number within core of the processor. |
| 129 | pub thread: u32, |
| 130 | } |
| 131 | |
| 132 | /// MP Services [`Protocol`]. |
| 133 | /// |
| 134 | /// Protocol that provides services needed for multi-processor management. |
| 135 | /// |
| 136 | /// [`Protocol`]: uefi::proto::Protocol |
| 137 | #[derive(Debug)] |
| 138 | #[repr(C)] |
| 139 | #[unsafe_protocol("3fdda605-a76e-4f46-ad29-12f4531b3d08")] |
| 140 | pub struct MpServices { |
| 141 | get_number_of_processors: extern "efiapi" fn( |
| 142 | this: *const Self, |
| 143 | number_of_processors: *mut usize, |
| 144 | number_of_enabled_processors: *mut usize, |
| 145 | ) -> Status, |
| 146 | get_processor_info: extern "efiapi" fn( |
| 147 | this: *const Self, |
| 148 | processor_number: usize, |
| 149 | processor_info_buffer: *mut ProcessorInformation, |
| 150 | ) -> Status, |
| 151 | startup_all_aps: extern "efiapi" fn( |
| 152 | this: *const Self, |
| 153 | procedure: Procedure, |
| 154 | single_thread: bool, |
| 155 | wait_event: *mut c_void, |
| 156 | timeout_in_micro_seconds: usize, |
| 157 | procedure_argument: *mut c_void, |
| 158 | failed_cpu_list: *mut *mut usize, |
| 159 | ) -> Status, |
| 160 | startup_this_ap: extern "efiapi" fn( |
| 161 | this: *const Self, |
| 162 | procedure: Procedure, |
| 163 | processor_number: usize, |
| 164 | wait_event: *mut c_void, |
| 165 | timeout_in_micro_seconds: usize, |
| 166 | procedure_argument: *mut c_void, |
| 167 | finished: *mut bool, |
| 168 | ) -> Status, |
| 169 | switch_bsp: extern "efiapi" fn( |
| 170 | this: *const Self, |
| 171 | processor_number: usize, |
| 172 | enable_old_bsp: bool, |
| 173 | ) -> Status, |
| 174 | enable_disable_ap: extern "efiapi" fn( |
| 175 | this: *const Self, |
| 176 | processor_number: usize, |
| 177 | enable_ap: bool, |
| 178 | health_flag: *const u32, |
| 179 | ) -> Status, |
| 180 | who_am_i: extern "efiapi" fn(this: *const Self, processor_number: *mut usize) -> Status, |
| 181 | } |
| 182 | |
| 183 | impl MpServices { |
| 184 | /// Retrieves the number of logical processors and the number of enabled logical processors in the system. |
| 185 | pub fn get_number_of_processors(&self) -> Result<ProcessorCount> { |
| 186 | let mut total: usize = 0; |
| 187 | let mut enabled: usize = 0; |
| 188 | (self.get_number_of_processors)(self, &mut total, &mut enabled) |
| 189 | .to_result_with_val(|| ProcessorCount { total, enabled }) |
| 190 | } |
| 191 | |
| 192 | /// Gets detailed information on the requested processor at the instant this call is made. |
| 193 | /// |
| 194 | /// Set [`CPU_V2_EXTENDED_TOPOLOGY`] in `processor_number` to also |
| 195 | /// retrieve [`ProcessorInformation::extended_information`]. |
| 196 | pub fn get_processor_info(&self, processor_number: usize) -> Result<ProcessorInformation> { |
| 197 | let mut pi: ProcessorInformation = Default::default(); |
| 198 | (self.get_processor_info)(self, processor_number, &mut pi).to_result_with_val(|| pi) |
| 199 | } |
| 200 | |
| 201 | /// Executes provided function on all APs. |
| 202 | pub fn startup_all_aps( |
| 203 | &self, |
| 204 | single_thread: bool, |
| 205 | procedure: Procedure, |
| 206 | procedure_argument: *mut c_void, |
| 207 | event: Option<Event>, |
| 208 | timeout: Option<Duration>, |
| 209 | ) -> Result { |
| 210 | let timeout_arg = match timeout { |
| 211 | Some(timeout) => timeout.as_micros().try_into().unwrap(), |
| 212 | None => 0, |
| 213 | }; |
| 214 | |
| 215 | let event_arg = match event { |
| 216 | Some(event) => event.as_ptr(), |
| 217 | None => ptr::null_mut(), |
| 218 | }; |
| 219 | |
| 220 | (self.startup_all_aps)( |
| 221 | self, |
| 222 | procedure, |
| 223 | single_thread, |
| 224 | event_arg, |
| 225 | timeout_arg, |
| 226 | procedure_argument, |
| 227 | ptr::null_mut(), |
| 228 | ) |
| 229 | .to_result() |
| 230 | } |
| 231 | |
| 232 | /// Executes provided function on a specific AP in blocking mode. |
| 233 | pub fn startup_this_ap( |
| 234 | &self, |
| 235 | processor_number: usize, |
| 236 | procedure: Procedure, |
| 237 | procedure_argument: *mut c_void, |
| 238 | event: Option<Event>, |
| 239 | timeout: Option<Duration>, |
| 240 | ) -> Result { |
| 241 | let timeout_arg = match timeout { |
| 242 | Some(timeout) => timeout.as_micros().try_into().unwrap(), |
| 243 | None => 0, |
| 244 | }; |
| 245 | |
| 246 | let event_arg = match event { |
| 247 | Some(event) => event.as_ptr(), |
| 248 | None => ptr::null_mut(), |
| 249 | }; |
| 250 | |
| 251 | (self.startup_this_ap)( |
| 252 | self, |
| 253 | procedure, |
| 254 | processor_number, |
| 255 | event_arg, |
| 256 | timeout_arg, |
| 257 | procedure_argument, |
| 258 | ptr::null_mut(), |
| 259 | ) |
| 260 | .to_result() |
| 261 | } |
| 262 | |
| 263 | /// Switches the requested AP to be the BSP from that point onward. |
| 264 | pub fn switch_bsp(&self, processor_number: usize, enable_old_bsp: bool) -> Result { |
| 265 | (self.switch_bsp)(self, processor_number, enable_old_bsp).to_result() |
| 266 | } |
| 267 | |
| 268 | /// Enables or disables an AP from this point onward. |
| 269 | /// |
| 270 | /// The `healthy` argument can be used to specify the new health status of the AP. |
| 271 | pub fn enable_disable_ap( |
| 272 | &self, |
| 273 | processor_number: usize, |
| 274 | enable_ap: bool, |
| 275 | healthy: Option<bool>, |
| 276 | ) -> Result { |
| 277 | let health_flag_raw: u32; |
| 278 | let health_flag_ptr = match healthy { |
| 279 | Some(healthy) => { |
| 280 | let mut sf = StatusFlag::empty(); |
| 281 | sf.set(StatusFlag::PROCESSOR_HEALTH_STATUS_BIT, healthy); |
| 282 | health_flag_raw = sf.bits(); |
| 283 | &health_flag_raw |
| 284 | } |
| 285 | None => ptr::null(), |
| 286 | }; |
| 287 | (self.enable_disable_ap)(self, processor_number, enable_ap, health_flag_ptr).to_result() |
| 288 | } |
| 289 | |
| 290 | /// Gets the handle number of the caller processor. |
| 291 | pub fn who_am_i(&self) -> Result<usize> { |
| 292 | let mut processor_number: usize = 0; |
| 293 | (self.who_am_i)(self, &mut processor_number).to_result_with_val(|| processor_number) |
| 294 | } |
| 295 | } |