lib.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //! A library for parsing ACPI tables. This crate can be used by bootloaders and kernels for
  2. //! architectures that support ACPI. The crate is far from feature-complete, but can still be used
  3. //! for finding and parsing the static tables, which is enough to set up hardware such as the APIC
  4. //! and HPET on x86_64.
  5. //!
  6. //! The crate is designed for use in conjunction with the `aml_parser` crate, which is the (much
  7. //! less complete) AML parser used to parse the DSDT and SSDTs. These crates are separate because
  8. //! some kernels may want to detect the static tables, but delay AML parsing to a later stage.
  9. //!
  10. //! ### Usage
  11. //! To use the library, you will need to provide an implementation of the `AcpiHandler` trait,
  12. //! which allows the library to make requests such as mapping a particular region of physical
  13. //! memory into the virtual address space.
  14. //!
  15. //! You should then call one of the entry points, based on how much information you have:
  16. //! * Call `parse_rsdp` if you have the physical address of the RSDP
  17. //! * Call `parse_rsdt` if you have the physical address of the RSDT / XSDT
  18. //! * Call `search_for_rsdp_bios` if you don't have the address of either structure, but **you
  19. //! know you're running on BIOS, not UEFI**
  20. //!
  21. //! All of these methods return an instance of `Acpi`. This struct contains all the information
  22. //! gathered from the static tables, and can be queried to set up hardware etc.
  23. #![no_std]
  24. #![feature(nll)]
  25. #![feature(alloc)]
  26. #![feature(exclusive_range_pattern)]
  27. extern crate alloc;
  28. #[cfg_attr(test, macro_use)]
  29. #[cfg(test)]
  30. extern crate std;
  31. mod fadt;
  32. pub mod handler;
  33. mod hpet;
  34. pub mod interrupt;
  35. mod madt;
  36. mod rsdp;
  37. mod rsdp_search;
  38. mod sdt;
  39. pub use crate::{
  40. handler::{AcpiHandler, PhysicalMapping},
  41. madt::MadtError,
  42. rsdp_search::search_for_rsdp_bios,
  43. };
  44. use crate::{hpet::HpetInfo, interrupt::InterruptModel, rsdp::Rsdp, sdt::SdtHeader};
  45. use alloc::vec::Vec;
  46. use core::mem;
  47. #[derive(Debug)]
  48. // TODO: manually implement Debug to print signatures correctly etc.
  49. pub enum AcpiError {
  50. RsdpIncorrectSignature,
  51. RsdpInvalidOemId,
  52. RsdpInvalidChecksum,
  53. NoValidRsdp,
  54. SdtInvalidSignature([u8; 4]),
  55. SdtInvalidOemId([u8; 4]),
  56. SdtInvalidTableId([u8; 4]),
  57. SdtInvalidChecksum([u8; 4]),
  58. InvalidMadt(MadtError),
  59. }
  60. #[derive(Clone, Copy, Debug)]
  61. #[repr(C, packed)]
  62. pub(crate) struct GenericAddress {
  63. address_space: u8,
  64. bit_width: u8,
  65. bit_offset: u8,
  66. access_size: u8,
  67. address: u64,
  68. }
  69. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  70. pub enum ProcessorState {
  71. /// A processor in this state is unusable, and you must not attempt to bring it up.
  72. Disabled,
  73. /// A processor waiting for a SIPI (Startup Inter-processor Interrupt) is currently not active,
  74. /// but may be brought up.
  75. WaitingForSipi,
  76. /// A Running processor is currently brought up and running code.
  77. Running,
  78. }
  79. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  80. pub struct Processor {
  81. pub processor_uid: u8,
  82. pub local_apic_id: u8,
  83. /// The state of this processor. Always check that the processor is not `Disabled` before
  84. /// attempting to bring it up!
  85. pub state: ProcessorState,
  86. /// Whether this processor is the Bootstrap Processor (BSP), or an Application Processor (AP).
  87. /// When the bootloader is entered, the BSP is the only processor running code. To run code on
  88. /// more than one processor, you need to "bring up" the APs.
  89. pub is_ap: bool,
  90. }
  91. }
  92. }
  93. #[derive(Debug)]
  94. pub struct Acpi {
  95. acpi_revision: u8,
  96. boot_processor: Option<Processor>,
  97. application_processors: Vec<Processor>,
  98. /// ACPI theoretically allows for more than one interrupt model to be supported by the same
  99. /// hardware. For simplicity and because hardware practically will only support one model, we
  100. /// just error in cases that the tables detail more than one.
  101. interrupt_model: Option<InterruptModel>,
  102. hpet: Option<HpetInfo>,
  103. /// The physical address of the DSDT, if we manage to find it.
  104. dsdt_address: Option<usize>,
  105. /// The physical addresses of the SSDTs, if there are any,
  106. ssdt_addresses: Vec<usize>,
  107. }
  108. impl Acpi {
  109. /// A description of the boot processor. Until you bring any more up, this is the only processor
  110. /// running code, even on SMP systems.
  111. pub fn boot_processor<'a>(&'a self) -> &'a Option<Processor> {
  112. &self.boot_processor
  113. }
  114. /// Descriptions of each of the application processors. These are not brought up until you do
  115. /// so. The application processors must be brought up in the order that they appear in this
  116. /// list.
  117. pub fn application_processors<'a>(&'a self) -> &'a Vec<Processor> {
  118. &self.application_processors
  119. }
  120. /// The interrupt model supported by this system.
  121. pub fn interrupt_model<'a>(&'a self) -> &'a Option<InterruptModel> {
  122. &self.interrupt_model
  123. }
  124. }
  125. /// This is the entry point of `acpi` if you have the **physical** address of the RSDP. It maps
  126. /// the RSDP, works out what version of ACPI the hardware supports, and passes the physical
  127. /// address of the RSDT/XSDT to `parse_rsdt`.
  128. pub fn parse_rsdp<H>(handler: &mut H, rsdp_address: usize) -> Result<Acpi, AcpiError>
  129. where
  130. H: AcpiHandler,
  131. {
  132. let rsdp_mapping = handler.map_physical_region::<Rsdp>(rsdp_address, mem::size_of::<Rsdp>());
  133. (*rsdp_mapping).validate()?;
  134. parse_validated_rsdp(handler, rsdp_mapping)
  135. }
  136. fn parse_validated_rsdp<H>(
  137. handler: &mut H,
  138. rsdp_mapping: PhysicalMapping<Rsdp>,
  139. ) -> Result<Acpi, AcpiError>
  140. where
  141. H: AcpiHandler,
  142. {
  143. let revision = (*rsdp_mapping).revision();
  144. if revision == 0 {
  145. /*
  146. * We're running on ACPI Version 1.0. We should use the 32-bit RSDT address.
  147. */
  148. let rsdt_address = (*rsdp_mapping).rsdt_address();
  149. handler.unmap_physical_region(rsdp_mapping);
  150. parse_rsdt(handler, revision, rsdt_address as usize)
  151. } else {
  152. /*
  153. * We're running on ACPI Version 2.0+. We should use the 64-bit XSDT address, truncated
  154. * to 32 bits on x86.
  155. */
  156. let xsdt_address = (*rsdp_mapping).xsdt_address();
  157. handler.unmap_physical_region(rsdp_mapping);
  158. parse_rsdt(handler, revision, xsdt_address as usize)
  159. }
  160. }
  161. /// This is the entry point of `acpi` if you already have the **physical** address of the
  162. /// RSDT/XSDT; it parses all the SDTs in the RSDT/XSDT, calling the relevant handlers in the
  163. /// implementation's `AcpiHandler`.
  164. ///
  165. /// If the given revision is 0, an address to the RSDT is expected. Otherwise, an address to
  166. /// the XSDT is expected.
  167. pub fn parse_rsdt<H>(
  168. handler: &mut H,
  169. revision: u8,
  170. physical_address: usize,
  171. ) -> Result<Acpi, AcpiError>
  172. where
  173. H: AcpiHandler,
  174. {
  175. let mut acpi = Acpi {
  176. acpi_revision: revision,
  177. boot_processor: None,
  178. application_processors: Vec::new(),
  179. interrupt_model: None,
  180. hpet: None,
  181. dsdt_address: None,
  182. ssdt_addresses: Vec::with_capacity(0),
  183. };
  184. let header = sdt::peek_at_sdt_header(handler, physical_address);
  185. let mapping =
  186. handler.map_physical_region::<SdtHeader>(physical_address, header.length() as usize);
  187. if revision == 0 {
  188. /*
  189. * ACPI Version 1.0. It's a RSDT!
  190. */
  191. (*mapping).validate(b"RSDT")?;
  192. let num_tables =
  193. ((*mapping).length() as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u32>();
  194. let tables_base =
  195. ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u32;
  196. for i in 0..num_tables {
  197. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
  198. as usize)?;
  199. }
  200. } else {
  201. /*
  202. * ACPI Version 2.0+. It's a XSDT!
  203. */
  204. (*mapping).validate(b"XSDT")?;
  205. let num_tables =
  206. ((*mapping).length() as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u64>();
  207. let tables_base =
  208. ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u64;
  209. for i in 0..num_tables {
  210. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
  211. as usize)?;
  212. }
  213. }
  214. handler.unmap_physical_region(mapping);
  215. Ok(acpi)
  216. }