Repository navigation
Make PhysicalMapping safely Send / Sync #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ use alloc::{ | |
| use bit_field::BitField; | ||
| use core::{ | ||
| mem, | ||
| ptr, | ||
| slice, | ||
| str::FromStr, | ||
| sync::atomic::{AtomicU64, Ordering}, | ||
|
|
@@ -204,7 +205,7 @@ where | |
| }; | ||
| let stream = unsafe { | ||
| slice::from_raw_parts( | ||
| mapping.raw.virtual_start.as_ptr().byte_add(mem::size_of::<SdtHeader>()) as *const u8, | ||
| ptr::from_ref(&*mapping).byte_add(size_of::<SdtHeader>()) as *const u8, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applies throughout: I get why the pattern here works (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps (Agree otherwise though, seems sensible) |
||
| table.length as usize - mem::size_of::<SdtHeader>(), | ||
| ) | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ extern crate alloc; | |
| pub mod address; | ||
| #[cfg(feature = "aml")] | ||
| pub mod aml; | ||
| pub mod physical_mapping; | ||
| #[cfg(feature = "alloc")] | ||
| pub mod platform; | ||
| pub mod registers; | ||
|
|
@@ -65,14 +66,9 @@ pub use pci_types::PciAddress; | |
| pub use sdt::{fadt::PowerProfile, hpet::HpetInfo, madt::MadtError}; | ||
|
|
||
| use crate::sdt::{SdtHeader, Signature}; | ||
| use core::{ | ||
| fmt, | ||
| mem, | ||
| ops::{Deref, DerefMut}, | ||
| pin::Pin, | ||
| ptr::NonNull, | ||
| }; | ||
| use core::{mem, ptr}; | ||
| use log::warn; | ||
| pub use physical_mapping::{PhysicalMapping, RawPhysicalMapping}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could we group |
||
| use rsdp::Rsdp; | ||
|
|
||
| /// `AcpiTables` represents a platform's of ACPI static tables, enumerated from the RSDT/XSDT. It | ||
|
|
@@ -169,9 +165,8 @@ where | |
| rsdt_entry_size: usize, | ||
| quirks: AcpiQuirks, | ||
| ) -> Result<AcpiTables<H>, AcpiError> { | ||
| let rsdt_mapping = unsafe { | ||
| PhysicalMapping::<_, SdtHeader>::new(rsdt_address, mem::size_of::<SdtHeader>(), handler.clone()) | ||
| }; | ||
| let rsdt_mapping = | ||
| unsafe { PhysicalMapping::<_, SdtHeader>::new(rsdt_address, size_of::<SdtHeader>(), handler.clone()) }; | ||
|
|
||
| let rsdt_length = rsdt_mapping.length; | ||
| let rsdt_mapping = | ||
|
|
@@ -182,10 +177,10 @@ where | |
| /// Iterate over the **physical** addresses of the SDTs. | ||
| pub fn table_entries(&self) -> impl Iterator<Item = usize> { | ||
| let mut table_entries_ptr = | ||
| unsafe { self.rsdt_mapping.raw.virtual_start.as_ptr().byte_add(mem::size_of::<SdtHeader>()) } | ||
| .cast::<u8>(); | ||
| let mut num_entries = (self.rsdt_mapping.raw.region_length.saturating_sub(mem::size_of::<SdtHeader>())) | ||
| / self.rsdt_entry_size; | ||
| unsafe { ptr::from_ref(&*self.rsdt_mapping).byte_add(size_of::<SdtHeader>()) }.cast::<u8>(); | ||
| let mut num_entries = | ||
| (self.rsdt_mapping.get_raw().get_region_length().saturating_sub(mem::size_of::<SdtHeader>())) | ||
| / self.rsdt_entry_size; | ||
|
|
||
| core::iter::from_fn(move || { | ||
| if num_entries > 0 { | ||
|
|
@@ -347,119 +342,6 @@ pub struct AcpiQuirks { | |
| pub ignore_xsdt: bool, | ||
| } | ||
|
|
||
| /// Describes a physical mapping. | ||
| /// | ||
| /// The region mapped must be at least `size_of::<T>()` bytes, but may be bigger. | ||
| #[derive(Debug)] | ||
| pub struct RawPhysicalMapping<T: ?Sized> { | ||
| /// The physical address of the mapped structure. The actual mapping may start at a lower address | ||
| /// if the requested physical address is not well-aligned. | ||
| pub physical_start: usize, | ||
| /// The virtual address of the mapped structure. It must be a valid, non-null pointer to the | ||
| /// start of the requested structure. The actual virtual mapping may start at a lower address | ||
| /// if the requested address is not well-aligned. | ||
| pub virtual_start: NonNull<T>, | ||
| /// The size of the requested region, in bytes. Can be equal or larger to `size_of::<T>()`. If a | ||
| /// larger region has been mapped, this should still be the requested size. | ||
| pub region_length: usize, | ||
| /// The total size of the produced mapping. This may be the same as `region_length`, or larger to | ||
| /// meet requirements of the mapping implementation. | ||
| pub mapped_length: usize, | ||
| } | ||
|
|
||
| impl<T: ?Sized> Clone for RawPhysicalMapping<T> { | ||
| fn clone(&self) -> Self { | ||
| *self | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> Copy for RawPhysicalMapping<T> {} | ||
|
|
||
| /// Describes a physical mapping created by [`Handler::map_physical_region`] and unmapped by | ||
| /// [`Handler::unmap_physical_region`]. The region mapped must be at least `size_of::<T>()` | ||
| /// bytes, but may be bigger. | ||
| pub struct PhysicalMapping<H, T> | ||
| where | ||
| H: Handler, | ||
| { | ||
| pub raw: RawPhysicalMapping<T>, | ||
| /// The [`Handler`] that was used to produce the mapping. When this mapping is dropped, this | ||
| /// handler will be used to unmap the region. | ||
| pub handler: H, | ||
| } | ||
|
|
||
| impl<H, T> PhysicalMapping<H, T> | ||
| where | ||
| H: Handler, | ||
| { | ||
| /// Creates a new physical mapping from the given handler. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// - `physical_address` must point to a valid `T` in physical memory. | ||
| /// - `size` must be at least `size_of::<T>()`. | ||
| pub unsafe fn new(physical_address: usize, size: usize, handler: H) -> PhysicalMapping<H, T> { | ||
| let raw = unsafe { handler.map_physical_region(physical_address, size) }; | ||
| PhysicalMapping { raw, handler } | ||
| } | ||
|
|
||
| /// Get a pinned reference to the inner `T`. This is generally only useful if `T` is `!Unpin`, | ||
| /// otherwise the mapping can simply be dereferenced to access the inner type. | ||
| pub fn get(&self) -> Pin<&T> { | ||
| unsafe { Pin::new_unchecked(self.raw.virtual_start.as_ref()) } | ||
| } | ||
| } | ||
|
|
||
| impl<H, T> fmt::Debug for PhysicalMapping<H, T> | ||
| where | ||
| H: Handler, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("PhysicalMapping") | ||
| .field("physical_start", &self.raw.physical_start) | ||
| .field("virtual_start", &self.raw.virtual_start) | ||
| .field("region_length", &self.raw.region_length) | ||
| .field("mapped_length", &self.raw.mapped_length) | ||
| .field("handler", &()) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| unsafe impl<H: Handler + Send, T: Send> Send for PhysicalMapping<H, T> {} | ||
|
|
||
| impl<H, T> Deref for PhysicalMapping<H, T> | ||
| where | ||
| T: Unpin, | ||
| H: Handler, | ||
| { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &T { | ||
| unsafe { self.raw.virtual_start.as_ref() } | ||
| } | ||
| } | ||
|
|
||
| impl<H, T> DerefMut for PhysicalMapping<H, T> | ||
| where | ||
| T: Unpin, | ||
| H: Handler, | ||
| { | ||
| fn deref_mut(&mut self) -> &mut T { | ||
| unsafe { self.raw.virtual_start.as_mut() } | ||
| } | ||
| } | ||
|
|
||
| impl<H, T> Drop for PhysicalMapping<H, T> | ||
| where | ||
| H: Handler, | ||
| { | ||
| fn drop(&mut self) { | ||
| unsafe { | ||
| self.handler.unmap_physical_region(self.raw); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A `Handle` is an opaque reference to an object that is managed by the host on behalf of this | ||
| /// library. | ||
| /// | ||
|
|
@@ -496,8 +378,6 @@ pub trait Handler: Clone { | |
|
|
||
| /// Unmap the given physical mapping. This is called when a [`PhysicalMapping`] is dropped, you should **not** manually call this. | ||
| /// | ||
| /// Note: A reference to the [`Handler`] used to construct `region` can be acquired from [`PhysicalMapping::handler`]. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// `region` must denote a physical mapping _currently allocated_ via this handler. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we create helpers as the above, this could move to there as a safety requirement on the caller?