lib.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #![no_std]
  2. #[cfg(test)]
  3. extern crate std;
  4. mod rsdp;
  5. mod sdt;
  6. use core::ops::Deref;
  7. use core::ptr::NonNull;
  8. use rsdp::Rsdp;
  9. #[derive(Debug)]
  10. pub enum AcpiError
  11. {
  12. RsdpIncorrectSignature,
  13. RsdpInvalidOemId,
  14. RsdpInvalidChecksum,
  15. }
  16. /// Describes a physical mapping created by `AcpiHandler::map_physical_region` and unmapped by
  17. /// `AcpiHandler::unmap_physical_region`. The region mapped must be at least `mem::size_of::<T>()`
  18. /// bytes, but may be bigger.
  19. pub struct PhysicalMapping<T>
  20. {
  21. pub physical_start : usize,
  22. pub virtual_start : NonNull<T>,
  23. pub mapped_length : usize, // Differs from `region_length` if padding is added to align to page boundaries
  24. }
  25. impl<T> Deref for PhysicalMapping<T>
  26. {
  27. type Target = T;
  28. fn deref(&self) -> &T
  29. {
  30. unsafe
  31. {
  32. self.virtual_start.as_ref()
  33. }
  34. }
  35. }
  36. /// The kernel must provide an implementation of this trait for `acpi` to interface with. It has
  37. /// utility methods `acpi` uses to for e.g. mapping physical memory, but also an interface for
  38. /// `acpi` to tell the kernel about the tables it's parsing, such as how the kernel should
  39. /// configure the APIC or PCI routing.
  40. pub trait AcpiHandler
  41. {
  42. /// Given a starting physical address, map a region of physical memory that contains a `T`
  43. /// somewhere in the virtual address space. The address doesn't have to be page-aligned, so
  44. /// the implementation may have to add padding to either end.
  45. fn map_physical_region<T>(&mut self, physical_address : usize) -> PhysicalMapping<T>;
  46. /// Unmap the given physical mapping. Safe because we consume the mapping, and so it can't be
  47. /// used after being passed to this function.
  48. fn unmap_physical_region<T>(&mut self, region : PhysicalMapping<T>);
  49. }
  50. /// This is the entry point of `acpi`. Given the **physical** address of the RSDP, it parses all
  51. /// the SDTs in the RSDT, calling the relevant handlers in the implementation's `AcpiHandler`.
  52. pub fn parse_acpi<T>(handler : &mut T, rsdp_address : usize) -> Result<(), AcpiError>
  53. where T : AcpiHandler
  54. {
  55. let rsdp_mapping = handler.map_physical_region::<Rsdp>(rsdp_address);
  56. (*rsdp_mapping).validate()?;
  57. // TODO: map the RSDT
  58. // TODO: parse the RSDT
  59. handler.unmap_physical_region(rsdp_mapping);
  60. Ok(())
  61. }