lib.rs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 know
  19. //! 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, exclusive_range_pattern)]
  25. extern crate alloc;
  26. #[cfg_attr(test, macro_use)]
  27. #[cfg(test)]
  28. extern crate std;
  29. mod fadt;
  30. pub mod handler;
  31. mod hpet;
  32. pub mod interrupt;
  33. mod madt;
  34. mod mcfg;
  35. mod rsdp;
  36. mod rsdp_search;
  37. mod sdt;
  38. pub use crate::{
  39. handler::{AcpiHandler, PhysicalMapping},
  40. hpet::HpetInfo,
  41. interrupt::InterruptModel,
  42. madt::MadtError,
  43. mcfg::PciConfigRegions,
  44. rsdp_search::search_for_rsdp_bios,
  45. };
  46. use crate::{rsdp::Rsdp, sdt::SdtHeader};
  47. use alloc::vec::Vec;
  48. use core::mem;
  49. #[derive(Debug)]
  50. // TODO: manually implement Debug to print signatures correctly etc.
  51. pub enum AcpiError {
  52. RsdpIncorrectSignature,
  53. RsdpInvalidOemId,
  54. RsdpInvalidChecksum,
  55. NoValidRsdp,
  56. SdtInvalidSignature([u8; 4]),
  57. SdtInvalidOemId([u8; 4]),
  58. SdtInvalidTableId([u8; 4]),
  59. SdtInvalidChecksum([u8; 4]),
  60. InvalidMadt(MadtError),
  61. }
  62. #[derive(Clone, Copy, Debug)]
  63. #[repr(C, packed)]
  64. pub(crate) struct GenericAddress {
  65. address_space: u8,
  66. bit_width: u8,
  67. bit_offset: u8,
  68. access_size: u8,
  69. address: u64,
  70. }
  71. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  72. pub enum ProcessorState {
  73. /// A processor in this state is unusable, and you must not attempt to bring it up.
  74. Disabled,
  75. /// A processor waiting for a SIPI (Startup Inter-processor Interrupt) is currently not active,
  76. /// but may be brought up.
  77. WaitingForSipi,
  78. /// A Running processor is currently brought up and running code.
  79. Running,
  80. }
  81. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  82. pub struct Processor {
  83. pub processor_uid: u8,
  84. pub local_apic_id: u8,
  85. /// The state of this processor. Always check that the processor is not `Disabled` before
  86. /// attempting to bring it up!
  87. pub state: ProcessorState,
  88. /// Whether this processor is the Bootstrap Processor (BSP), or an Application Processor (AP).
  89. /// When the bootloader is entered, the BSP is the only processor running code. To run code on
  90. /// more than one processor, you need to "bring up" the APs.
  91. pub is_ap: bool,
  92. }
  93. #[derive(Debug)]
  94. pub struct AmlTable {
  95. /// Physical address of the start of the AML stream (excluding the table header).
  96. pub address: usize,
  97. /// Length (in bytes) of the AML stream.
  98. pub length: u32,
  99. }
  100. impl AmlTable {
  101. /// Create an `AmlTable` from the address and length of the table **including the SDT header**.
  102. pub(crate) fn new(address: usize, length: u32) -> AmlTable {
  103. AmlTable {
  104. address: address + mem::size_of::<SdtHeader>(),
  105. length: length - mem::size_of::<SdtHeader>() as u32,
  106. }
  107. }
  108. }
  109. #[derive(Debug)]
  110. pub struct Acpi {
  111. pub acpi_revision: u8,
  112. /// The boot processor. Until you bring up any APs, this is the only processor running code.
  113. pub boot_processor: Option<Processor>,
  114. /// Application processes. These are not brought up until you do so, and must be brought up in
  115. /// the order they appear in this list.
  116. pub application_processors: Vec<Processor>,
  117. /// ACPI theoretically allows for more than one interrupt model to be supported by the same
  118. /// hardware. For simplicity and because hardware practically will only support one model, we
  119. /// just error in cases that the tables detail more than one.
  120. pub interrupt_model: Option<InterruptModel>,
  121. pub hpet: Option<HpetInfo>,
  122. /// Info about the DSDT, if we find it.
  123. pub dsdt: Option<AmlTable>,
  124. /// Info about any SSDTs, if there are any.
  125. pub ssdts: Vec<AmlTable>,
  126. /// Info about the PCI-E configuration memory regions, collected from the MCFG.
  127. pub pci_config_regions: Option<PciConfigRegions>,
  128. }
  129. /// This is the entry point of `acpi` if you have the **physical** address of the RSDP. It maps
  130. /// the RSDP, works out what version of ACPI the hardware supports, and passes the physical
  131. /// address of the RSDT/XSDT to `parse_rsdt`.
  132. pub fn parse_rsdp<H>(handler: &mut H, rsdp_address: usize) -> Result<Acpi, AcpiError>
  133. where
  134. H: AcpiHandler,
  135. {
  136. let rsdp_mapping = handler.map_physical_region::<Rsdp>(rsdp_address, mem::size_of::<Rsdp>());
  137. (*rsdp_mapping).validate()?;
  138. parse_validated_rsdp(handler, rsdp_mapping)
  139. }
  140. fn parse_validated_rsdp<H>(
  141. handler: &mut H,
  142. rsdp_mapping: PhysicalMapping<Rsdp>,
  143. ) -> Result<Acpi, AcpiError>
  144. where
  145. H: AcpiHandler,
  146. {
  147. let revision = (*rsdp_mapping).revision();
  148. if revision == 0 {
  149. /*
  150. * We're running on ACPI Version 1.0. We should use the 32-bit RSDT address.
  151. */
  152. let rsdt_address = (*rsdp_mapping).rsdt_address();
  153. handler.unmap_physical_region(rsdp_mapping);
  154. parse_rsdt(handler, revision, rsdt_address as usize)
  155. } else {
  156. /*
  157. * We're running on ACPI Version 2.0+. We should use the 64-bit XSDT address, truncated
  158. * to 32 bits on x86.
  159. */
  160. let xsdt_address = (*rsdp_mapping).xsdt_address();
  161. handler.unmap_physical_region(rsdp_mapping);
  162. parse_rsdt(handler, revision, xsdt_address as usize)
  163. }
  164. }
  165. /// This is the entry point of `acpi` if you already have the **physical** address of the
  166. /// RSDT/XSDT; it parses all the SDTs in the RSDT/XSDT, calling the relevant handlers in the
  167. /// implementation's `AcpiHandler`.
  168. ///
  169. /// If the given revision is 0, an address to the RSDT is expected. Otherwise, an address to
  170. /// the XSDT is expected.
  171. pub fn parse_rsdt<H>(
  172. handler: &mut H,
  173. revision: u8,
  174. physical_address: usize,
  175. ) -> Result<Acpi, AcpiError>
  176. where
  177. H: AcpiHandler,
  178. {
  179. let mut acpi = Acpi {
  180. acpi_revision: revision,
  181. boot_processor: None,
  182. application_processors: Vec::new(),
  183. interrupt_model: None,
  184. hpet: None,
  185. dsdt: None,
  186. ssdts: Vec::with_capacity(0),
  187. pci_config_regions: None,
  188. };
  189. let header = sdt::peek_at_sdt_header(handler, physical_address);
  190. let mapping =
  191. handler.map_physical_region::<SdtHeader>(physical_address, header.length() as usize);
  192. if revision == 0 {
  193. /*
  194. * ACPI Version 1.0. It's a RSDT!
  195. */
  196. (*mapping).validate(b"RSDT")?;
  197. let num_tables =
  198. ((*mapping).length() as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u32>();
  199. let tables_base =
  200. ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u32;
  201. for i in 0..num_tables {
  202. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
  203. as usize)?;
  204. }
  205. } else {
  206. /*
  207. * ACPI Version 2.0+. It's a XSDT!
  208. */
  209. (*mapping).validate(b"XSDT")?;
  210. let num_tables =
  211. ((*mapping).length() as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u64>();
  212. let tables_base =
  213. ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u64;
  214. for i in 0..num_tables {
  215. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
  216. as usize)?;
  217. }
  218. }
  219. handler.unmap_physical_region(mapping);
  220. Ok(acpi)
  221. }