@@ -18,6 +18,7 @@ use core::{ptr, slice};
18
18
19
19
#[ cfg( feature = "alloc" ) ]
20
20
use alloc:: vec:: Vec ;
21
+ use core:: fmt:: Debug ;
21
22
22
23
pub use uefi_raw:: table:: boot:: {
23
24
EventType , InterfaceType , MemoryAttribute , MemoryDescriptor , MemoryType , Tpl ,
@@ -1723,18 +1724,6 @@ impl MemoryMapBackingMemory {
1723
1724
mmm. map_size + extra_size
1724
1725
}
1725
1726
1726
- /// Returns a raw pointer to the beginning of the allocation.
1727
- #[ must_use]
1728
- pub fn as_ptr ( & self ) -> * const u8 {
1729
- self . 0 . as_ptr ( ) . cast ( )
1730
- }
1731
-
1732
- /// Returns a mutable raw pointer to the beginning of the allocation.
1733
- #[ must_use]
1734
- pub fn as_mut_ptr ( & mut self ) -> * mut u8 {
1735
- self . 0 . as_ptr ( ) . cast ( )
1736
- }
1737
-
1738
1727
/// Returns a slice to the underlying memory.
1739
1728
#[ must_use]
1740
1729
pub fn as_slice ( & self ) -> & [ u8 ] {
@@ -1819,7 +1808,7 @@ impl MemoryMapMeta {
1819
1808
/// a low level.
1820
1809
///
1821
1810
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
1822
- pub trait MemoryMap : Index < usize , Output = MemoryDescriptor > {
1811
+ pub trait MemoryMap : Debug {
1823
1812
// TODO also require IntoIterator?! :)
1824
1813
1825
1814
/// Returns the associated [`MemoryMapMeta`].
@@ -1861,7 +1850,7 @@ pub trait MemoryMap: Index<usize, Output = MemoryDescriptor> {
1861
1850
1862
1851
/// Extension to [`MemoryMap`] that adds mutable operations. This also includes
1863
1852
/// the ability to sort the memory map.
1864
- pub trait MemoryMapMut : MemoryMap + IndexMut < usize > {
1853
+ pub trait MemoryMapMut : MemoryMap {
1865
1854
/// Returns a mutable reference to the [`MemoryDescriptor`] at the given
1866
1855
/// index, if present.
1867
1856
#[ must_use]
@@ -1882,84 +1871,97 @@ pub trait MemoryMapMut: MemoryMap + IndexMut<usize> {
1882
1871
1883
1872
/// Sorts the memory map by physical address in place. This operation is
1884
1873
/// optional and should be invoked only once.
1885
- #[ must_use]
1886
1874
fn sort ( & mut self ) ;
1887
1875
1888
1876
/// Returns a reference to the underlying memory.
1889
1877
///
1890
1878
/// # Safety
1891
1879
///
1892
1880
/// This is unsafe as there is a potential to create invalid entries.
1893
- unsafe fn buffer_mut ( & self ) -> & mut [ u8 ] ;
1881
+ unsafe fn buffer_mut ( & mut self ) -> & mut [ u8 ] ;
1894
1882
}
1895
1883
1896
- /// An accessory to the memory map that can be either iterated or
1897
- /// indexed like an array.
1898
- ///
1899
- /// A [`MemoryMapOwned`] is always associated with the unique [`MemoryMapKey`]
1900
- /// contained in the struct.
1901
- ///
1902
- /// To iterate over the entries, call [`MemoryMapOwned::entries`]. To get a sorted
1903
- /// map, you manually have to call [`MemoryMapOwned::sort`] first.
1904
- ///
1905
- /// ## UEFI pitfalls
1906
- /// **Please note** that when working with memory maps, the `entry_size` is
1907
- /// usually larger than `size_of::<MemoryDescriptor` [[0]]. So to be safe,
1908
- /// always use `entry_size` as step-size when interfacing with the memory map on
1909
- /// a low level.
1910
- ///
1911
- /// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
1884
+ /// Implementation of [`MemoryMap`] for the given buffer.
1912
1885
#[ derive( Debug ) ]
1913
- pub struct MemoryMapOwned {
1914
- /// Backing memory, properly initialized at this point.
1915
- buf : MemoryMapBackingMemory ,
1886
+ pub struct MemoryMapRef < ' a > {
1887
+ buf : & ' a [ u8 ] ,
1916
1888
key : MemoryMapKey ,
1917
1889
meta : MemoryMapMeta ,
1918
1890
len : usize ,
1919
1891
}
1920
1892
1921
- impl MemoryMapOwned {
1922
- /// Creates a [`MemoryMapOwned`] from the give initialized memory map behind
1923
- /// the buffer and the reported `desc_size` from UEFI.
1924
- pub ( crate ) fn from_initialized_mem ( buf : MemoryMapBackingMemory , meta : MemoryMapMeta ) -> Self {
1925
- assert ! ( meta. desc_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1926
- let len = meta. entry_count ( ) ;
1927
- MemoryMapOwned {
1928
- key : MemoryMapKey ( 0 ) ,
1929
- buf,
1930
- meta,
1931
- len,
1893
+ impl < ' a > MemoryMap for MemoryMapRef < ' a > {
1894
+ fn meta ( & self ) -> MemoryMapMeta {
1895
+ self . meta
1896
+ }
1897
+
1898
+ fn key ( & self ) -> MemoryMapKey {
1899
+ self . key
1900
+ }
1901
+
1902
+ fn len ( & self ) -> usize {
1903
+ self . len
1904
+ }
1905
+
1906
+ fn buffer ( & self ) -> & [ u8 ] {
1907
+ self . buf
1908
+ }
1909
+
1910
+ fn entries ( & self ) -> MemoryMapIter < ' _ > {
1911
+ MemoryMapIter {
1912
+ memory_map : self ,
1913
+ index : 0 ,
1932
1914
}
1933
1915
}
1916
+ }
1934
1917
1935
- # [ cfg ( test ) ]
1936
- fn from_raw ( buf : & mut [ u8 ] , desc_size : usize ) -> Self {
1937
- let mem = MemoryMapBackingMemory :: from_slice ( buf ) ;
1938
- Self :: from_initialized_mem (
1939
- mem ,
1940
- MemoryMapMeta {
1941
- map_size : buf . len ( ) ,
1942
- desc_size ,
1943
- map_key : MemoryMapKey ( 0 ) ,
1944
- desc_version : MemoryDescriptor :: VERSION ,
1945
- } ,
1946
- )
1918
+ /// Implementation of [`MemoryMapMut`] for the given buffer.
1919
+ # [ derive ( Debug ) ]
1920
+ pub struct MemoryMapRefMut < ' a > {
1921
+ buf : & ' a mut [ u8 ] ,
1922
+ key : MemoryMapKey ,
1923
+ meta : MemoryMapMeta ,
1924
+ len : usize ,
1925
+ }
1926
+
1927
+ impl < ' a > MemoryMap for MemoryMapRefMut < ' a > {
1928
+ fn meta ( & self ) -> MemoryMapMeta {
1929
+ self . meta
1947
1930
}
1948
1931
1949
- #[ must_use]
1950
- /// Returns the unique [`MemoryMapKey`] associated with the memory map.
1951
- pub fn key ( & self ) -> MemoryMapKey {
1932
+ fn key ( & self ) -> MemoryMapKey {
1952
1933
self . key
1953
1934
}
1954
1935
1955
- /// Sorts the memory map by physical address in place.
1956
- /// This operation is optional and should be invoked only once.
1957
- pub fn sort ( & mut self ) {
1936
+ fn len ( & self ) -> usize {
1937
+ self . len
1938
+ }
1939
+
1940
+ fn buffer ( & self ) -> & [ u8 ] {
1941
+ self . buf
1942
+ }
1943
+
1944
+ fn entries ( & self ) -> MemoryMapIter < ' _ > {
1945
+ MemoryMapIter {
1946
+ memory_map : self ,
1947
+ index : 0 ,
1948
+ }
1949
+ }
1950
+ }
1951
+
1952
+ impl < ' a > MemoryMapMut for MemoryMapRefMut < ' a > {
1953
+ fn sort ( & mut self ) {
1958
1954
unsafe {
1959
1955
self . qsort ( 0 , self . len - 1 ) ;
1960
1956
}
1961
1957
}
1962
1958
1959
+ unsafe fn buffer_mut ( & mut self ) -> & mut [ u8 ] {
1960
+ self . buf
1961
+ }
1962
+ }
1963
+
1964
+ impl < ' a > MemoryMapRefMut < ' a > {
1963
1965
/// Hoare partition scheme for quicksort.
1964
1966
/// Must be called with `low` and `high` being indices within bounds.
1965
1967
unsafe fn qsort ( & mut self , low : usize , high : usize ) {
@@ -2021,96 +2023,101 @@ impl MemoryMapOwned {
2021
2023
let elem = unsafe { & * self . buf . as_ptr ( ) . add ( offset) . cast :: < MemoryDescriptor > ( ) } ;
2022
2024
elem. phys_start
2023
2025
}
2026
+ }
2024
2027
2025
- /// Returns an [`MemoryMapIter`] emitting [`MemoryDescriptor`]s.
2026
- ///
2027
- /// To get a sorted map, call [`MemoryMapOwned::sort`] first.
2028
- ///
2029
- /// # UEFI pitfalls
2030
- /// Currently, only the descriptor version specified in
2031
- /// [`MemoryDescriptor`] is supported. This is going to change if the UEFI
2032
- /// spec ever introduces a new memory descriptor version.
2033
- #[ must_use]
2034
- pub fn entries ( & self ) -> MemoryMapIter {
2035
- MemoryMapIter {
2036
- memory_map : self ,
2037
- index : 0 ,
2038
- }
2039
- }
2028
+ /// Implementation of [`MemoryMapMut`] that owns the buffer on the UEFI heap.
2029
+ #[ derive( Debug ) ]
2030
+ pub struct MemoryMapOwned {
2031
+ /// Backing memory, properly initialized at this point.
2032
+ buf : MemoryMapBackingMemory ,
2033
+ key : MemoryMapKey ,
2034
+ meta : MemoryMapMeta ,
2035
+ len : usize ,
2036
+ }
2040
2037
2041
- /// Returns a reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
2042
- #[ must_use]
2043
- pub fn get ( & self , index : usize ) -> Option < & MemoryDescriptor > {
2044
- if index >= self . len {
2045
- return None ;
2038
+ impl MemoryMapOwned {
2039
+ /// Creates a [`MemoryMapOwned`] from the give initialized memory map behind
2040
+ /// the buffer and the reported `desc_size` from UEFI.
2041
+ pub ( crate ) fn from_initialized_mem ( buf : MemoryMapBackingMemory , meta : MemoryMapMeta ) -> Self {
2042
+ assert ! ( meta. desc_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
2043
+ let len = meta. entry_count ( ) ;
2044
+ MemoryMapOwned {
2045
+ key : MemoryMapKey ( 0 ) ,
2046
+ buf,
2047
+ meta,
2048
+ len,
2046
2049
}
2050
+ }
2047
2051
2048
- let desc = unsafe {
2049
- & * self
2050
- . buf
2051
- . as_ptr ( )
2052
- . add ( self . meta . desc_size * index)
2053
- . cast :: < MemoryDescriptor > ( )
2054
- } ;
2052
+ #[ cfg( test) ]
2053
+ fn from_raw ( buf : & mut [ u8 ] , desc_size : usize ) -> Self {
2054
+ let mem = MemoryMapBackingMemory :: from_slice ( buf) ;
2055
+ Self :: from_initialized_mem (
2056
+ mem,
2057
+ MemoryMapMeta {
2058
+ map_size : buf. len ( ) ,
2059
+ desc_size,
2060
+ map_key : MemoryMapKey ( 0 ) ,
2061
+ desc_version : MemoryDescriptor :: VERSION ,
2062
+ } ,
2063
+ )
2064
+ }
2065
+ }
2055
2066
2056
- Some ( desc)
2067
+ impl MemoryMap for MemoryMapOwned {
2068
+ fn meta ( & self ) -> MemoryMapMeta {
2069
+ self . meta
2057
2070
}
2058
2071
2059
- /// Returns a mut reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
2060
- #[ must_use]
2061
- pub fn get_mut ( & mut self , index : usize ) -> Option < & mut MemoryDescriptor > {
2062
- if index >= self . len {
2063
- return None ;
2064
- }
2072
+ fn key ( & self ) -> MemoryMapKey {
2073
+ self . key
2074
+ }
2065
2075
2066
- let desc = unsafe {
2067
- & mut * self
2068
- . buf
2069
- . as_mut_ptr ( )
2070
- . add ( self . meta . desc_size * index)
2071
- . cast :: < MemoryDescriptor > ( )
2072
- } ;
2076
+ fn len ( & self ) -> usize {
2077
+ self . len
2078
+ }
2073
2079
2074
- Some ( desc)
2080
+ fn buffer ( & self ) -> & [ u8 ] {
2081
+ self . buf . as_slice ( )
2075
2082
}
2076
2083
2077
- /// Provides access to the raw memory map.
2078
- ///
2079
- /// This is for example useful if you want to embed the memory map into
2080
- /// another data structure, such as a Multiboot2 boot information.
2081
- #[ must_use]
2082
- pub fn as_raw ( & self ) -> ( & [ u8 ] , MemoryMapMeta ) {
2083
- ( self . buf . as_slice ( ) , self . meta )
2084
+ fn entries ( & self ) -> MemoryMapIter < ' _ > {
2085
+ MemoryMapIter {
2086
+ memory_map : self ,
2087
+ index : 0 ,
2088
+ }
2084
2089
}
2085
2090
}
2086
2091
2087
- impl core:: ops:: Index < usize > for MemoryMapOwned {
2088
- type Output = MemoryDescriptor ;
2089
-
2090
- fn index ( & self , index : usize ) -> & Self :: Output {
2091
- self . get ( index) . unwrap ( )
2092
+ impl MemoryMapMut for MemoryMapOwned {
2093
+ fn sort ( & mut self ) {
2094
+ let mut reference = MemoryMapRefMut {
2095
+ buf : self . buf . as_mut_slice ( ) ,
2096
+ key : self . key ,
2097
+ meta : self . meta ,
2098
+ len : self . len ,
2099
+ } ;
2100
+ reference. sort ( ) ;
2092
2101
}
2093
- }
2094
2102
2095
- impl core:: ops:: IndexMut < usize > for MemoryMapOwned {
2096
- fn index_mut ( & mut self , index : usize ) -> & mut Self :: Output {
2097
- self . get_mut ( index) . unwrap ( )
2103
+ unsafe fn buffer_mut ( & mut self ) -> & mut [ u8 ] {
2104
+ self . buf . as_mut_slice ( )
2098
2105
}
2099
2106
}
2100
2107
2101
2108
/// An iterator of [`MemoryDescriptor`]. The underlying memory map is always
2102
2109
/// associated with a unique [`MemoryMapKey`].
2103
2110
#[ derive( Debug , Clone ) ]
2104
2111
pub struct MemoryMapIter < ' a > {
2105
- memory_map : & ' a MemoryMapOwned ,
2112
+ memory_map : & ' a dyn MemoryMap ,
2106
2113
index : usize ,
2107
2114
}
2108
2115
2109
2116
impl < ' a > Iterator for MemoryMapIter < ' a > {
2110
2117
type Item = & ' a MemoryDescriptor ;
2111
2118
2112
2119
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2113
- let sz = self . memory_map . len - self . index ;
2120
+ let sz = self . memory_map . len ( ) - self . index ;
2114
2121
2115
2122
( sz, Some ( sz) )
2116
2123
}
@@ -2126,7 +2133,7 @@ impl<'a> Iterator for MemoryMapIter<'a> {
2126
2133
2127
2134
impl ExactSizeIterator for MemoryMapIter < ' _ > {
2128
2135
fn len ( & self ) -> usize {
2129
- self . memory_map . len
2136
+ self . memory_map . len ( )
2130
2137
}
2131
2138
}
2132
2139
@@ -2257,12 +2264,9 @@ pub struct ProtocolSearchKey(NonNull<c_void>);
2257
2264
2258
2265
#[ cfg( test) ]
2259
2266
mod tests_mmap_artificial {
2267
+ use super :: * ;
2260
2268
use core:: mem:: { size_of, size_of_val} ;
2261
2269
2262
- use crate :: table:: boot:: { MemoryAttribute , MemoryMapOwned , MemoryType } ;
2263
-
2264
- use super :: { MemoryDescriptor , MemoryMapIter } ;
2265
-
2266
2270
fn buffer_to_map ( buffer : & mut [ MemoryDescriptor ] ) -> MemoryMapOwned {
2267
2271
let byte_buffer = {
2268
2272
unsafe {
0 commit comments