Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/uefi/src/proto/debug/mod.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / uefi / src / proto / debug / mod.rs` 260 lines ยท 9357 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! Provides support for the UEFI debugging protocol. 4 //! 5 //! This protocol is designed to allow debuggers to query the state of the firmware, 6 //! as well as set up callbacks for various events. 7 //! 8 //! It also defines a Debugport protocol for debugging over serial devices. 9 //! 10 //! An example UEFI debugger is Intel's [UDK Debugger Tool][udk]. 11 //! 12 //! [udk]: https://firmware.intel.com/develop/intel-uefi-tools-and-utilities/intel-uefi-development-kit-debugger-tool 13 14 use core::ffi::c_void; 15 16 use crate::proto::unsafe_protocol; 17 use crate::{Result, Status, StatusExt}; 18 19 // re-export for ease of use 20 pub use context::SystemContext; 21 pub use exception::ExceptionType; 22 23 mod context; 24 mod exception; 25 26 /// Debug support [`Protocol`]. 27 /// 28 /// The debugging support protocol allows debuggers to connect to a UEFI machine. 29 /// It is expected that there will typically be two instances of the EFI Debug Support protocol in the system. 30 /// One associated with the native processor instruction set (IA-32, x64, ARM, RISC-V, or Itanium processor 31 /// family), and one for the EFI virtual machine that implements EFI byte code (EBC). 32 /// While multiple instances of the EFI Debug Support protocol are expected, there must never be more than 33 /// one for any given instruction set. 34 /// 35 /// NOTE: OVMF only implements this protocol interface for the virtual EBC processor 36 /// 37 /// [`Protocol`]: uefi::proto::Protocol 38 #[derive(Debug)] 39 #[repr(C)] 40 #[unsafe_protocol("`2755590c`-6f3c-42fa-9ea4-`a3ba543cda25`")] 41 pub struct DebugSupport { 42 isa: ProcessorArch, 43 get_maximum_processor_index: 44 extern "efiapi" fn(this: &mut Self, max_processor_index: &mut usize) -> Status, 45 register_periodic_callback: unsafe extern "efiapi" fn( 46 this: &mut Self, 47 processor_index: usize, 48 periodic_callback: Option, 49 ) -> Status, 50 register_exception_callback: unsafe extern "efiapi" fn( 51 this: &mut Self, 52 processor_index: usize, 53 exception_callback: Option, 54 exception_type: ExceptionType, 55 ) -> Status, 56 invalidate_instruction_cache: unsafe extern "efiapi" fn( 57 this: &mut Self, 58 processor_index: usize, 59 start: *mut c_void, 60 length: u64, 61 ) -> Status, 62 } 63 64 impl DebugSupport { 65 /// Returns the processor architecture of the running CPU. 66 #[must_use] 67 pub const fn arch(&self) -> ProcessorArch { 68 self.isa 69 } 70 71 /// Returns the maximum value that may be used for the processor_index parameter in 72 /// `register_periodic_callback()` and `register_exception_callback()`. 73 /// 74 /// Note: Applications built with EDK2 (such as OVMF) always return `0` as of 2021-09-15 75 pub fn get_maximum_processor_index(&mut self) -> usize { 76 // initially set to a canary value for testing purposes 77 let mut max_processor_index: usize = usize::MAX; 78 79 // per the UEFI spec, this call should only return EFI_SUCCESS 80 let _ = (self.get_maximum_processor_index)(self, &mut max_processor_index); 81 82 max_processor_index 83 } 84 85 /// Registers a function to be called back periodically in interrupt context. 86 /// Pass `None` for `callback` to deregister the currently registered function for 87 /// a specified `processor_index`. Will return `Status::INVALID_PARAMETER` if 88 /// `processor_index` exceeds the current maximum from `Self::get_maximum_processor_index`. 89 /// 90 /// Note: Applications built with EDK2 (such as OVMF) ignore the `processor_index` parameter 91 /// 92 /// # Safety 93 /// No portion of the debug agent that runs in interrupt context may make any 94 /// calls to EFI services or other protocol interfaces. 95 pub unsafe fn register_periodic_callback( 96 &mut self, 97 processor_index: usize, 98 callback: Option, 99 ) -> Result { 100 if processor_index > self.get_maximum_processor_index() { 101 return Err(Status::INVALID_PARAMETER.into()); 102 } 103 104 // Safety: As we've validated the `processor_index`, this should always be safe 105 // SAFETY: The memory is valid. 106 unsafe { (self.register_periodic_callback)(self, processor_index, callback) }.to_result() 107 } 108 109 /// Registers a function to be called when a given processor exception occurs. 110 /// Pass `None` for `callback` to deregister the currently registered function for a 111 /// given `exception_type` and `processor_index`. Will return `Status::INVALID_PARAMETER` 112 /// if `processor_index` exceeds the current maximum from `Self::get_maximum_processor_index`. 113 /// 114 /// Note: Applications built with EDK2 (such as OVMF) ignore the `processor_index` parameter 115 /// 116 /// # Safety 117 /// No portion of the debug agent that runs in interrupt context may make any 118 /// calls to EFI services or other protocol interfaces. 119 pub unsafe fn register_exception_callback( 120 &mut self, 121 processor_index: usize, 122 callback: Option, 123 exception_type: ExceptionType, 124 ) -> Result { 125 if processor_index > self.get_maximum_processor_index() { 126 return Err(Status::INVALID_PARAMETER.into()); 127 } 128 129 // Safety: As we've validated the `processor_index`, this should always be safe 130 // SAFETY: The memory is valid. 131 unsafe { 132 (self.register_exception_callback)(self, processor_index, callback, exception_type) 133 } 134 .to_result() 135 } 136 137 /// Invalidates processor instruction cache for a memory range for a given `processor_index`. 138 /// 139 /// Note: Applications built with EDK2 (such as OVMF) ignore the `processor_index` parameter 140 /// 141 /// # Safety 142 /// `start` must be a c_void ptr to a valid memory address 143 pub unsafe fn invalidate_instruction_cache( 144 &mut self, 145 processor_index: usize, 146 start: *mut c_void, 147 length: u64, 148 ) -> Result { 149 if processor_index > self.get_maximum_processor_index() { 150 return Err(Status::INVALID_PARAMETER.into()); 151 } 152 153 // per the UEFI spec, this call should only return EFI_SUCCESS 154 // Safety: As we've validated the `processor_index`, this should always be safe 155 // SAFETY: The memory is valid. 156 unsafe { (self.invalidate_instruction_cache)(self, processor_index, start, length) } 157 .to_result() 158 } 159 } 160 161 newtype_enum! { 162 /// The instruction set architecture of the running processor. 163 /// 164 /// UEFI can be and has been ported to new CPU architectures in the past, 165 /// therefore modeling this C enum as a Rust enum (where the compiler must know 166 /// about every variant in existence) would _not_ be safe. 167 pub enum ProcessorArch: u32 => { 168 /// 32-bit x86 PC 169 X86_32 = 0x014C, 170 /// 64-bit x86 PC 171 X86_64 = 0x8664, 172 /// Intel Itanium 173 ITANIUM = 0x200, 174 /// UEFI Interpreter bytecode 175 EBC = 0x0EBC, 176 /// ARM Thumb / Mixed 177 ARM = 0x01C2, 178 /// ARM 64-bit 179 AARCH_64 = 0xAA64, 180 /// RISC-V 32-bit 181 RISCV_32 = 0x5032, 182 /// RISC-V 64-bit 183 RISCV_64 = 0x5064, 184 /// RISC-V 128-bit 185 RISCV_128 = 0x5128, 186 }} 187 188 /// Debug Port [`Protocol`]. 189 /// 190 /// The debug port protocol abstracts the underlying debug port 191 /// hardware, whether it is a regular Serial port or something else. 192 /// 193 /// [`Protocol`]: uefi::proto::Protocol 194 #[derive(Debug)] 195 #[repr(C)] 196 #[unsafe_protocol("`eba4e8d2`-3858-41ec-a281-`2647ba9660d0`")] 197 pub struct DebugPort { 198 reset: extern "efiapi" fn(this: &Self) -> Status, 199 write: extern "efiapi" fn( 200 this: &Self, 201 timeout: u32, 202 buffer_size: &mut usize, 203 buffer: *const c_void, 204 ) -> Status, 205 read: extern "efiapi" fn( 206 this: &Self, 207 timeout: u32, 208 buffer_size: &mut usize, 209 buffer: *mut c_void, 210 ) -> Status, 211 poll: extern "efiapi" fn(this: &Self) -> Status, 212 } 213 214 impl DebugPort { 215 /// Resets the debugport device. 216 pub fn reset(&self) -> Result { 217 (self.reset)(self).to_result() 218 } 219 220 /// Write data to the debugport device. 221 /// 222 /// Note: `timeout` is given in microseconds 223 pub fn write(&self, timeout: u32, data: &[u8]) -> Result<(), usize> { 224 let mut buffer_size = data.len(); 225 226 (self.write)( 227 self, 228 timeout, 229 &mut buffer_size, 230 data.as_ptr().cast::(), 231 ) 232 .to_result_with( 233 || debug_assert_eq!(buffer_size, data.len()), 234 |_| buffer_size, 235 ) 236 } 237 238 /// Read data from the debugport device. 239 /// 240 /// Note: `timeout` is given in microseconds 241 pub fn read(&self, timeout: u32, data: &mut [u8]) -> Result<(), usize> { 242 let mut buffer_size = data.len(); 243 244 (self.read)( 245 self, 246 timeout, 247 &mut buffer_size, 248 data.as_mut_ptr().cast::(), 249 ) 250 .to_result_with( 251 || debug_assert_eq!(buffer_size, data.len()), 252 |_| buffer_size, 253 ) 254 } 255 256 /// Check to see if any data is available to be read from the debugport device. 257 pub fn poll(&self) -> Result { 258 (self.poll)(self).to_result() 259 } 260 }