aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / mem / memory_map / api.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Module for the traits [`MemoryMap`] and [`MemoryMapMut`]. |
| 4 | |
| 5 | use super::*; |
| 6 | use core::fmt::Debug; |
| 7 | use core::ops::{Index, IndexMut}; |
| 8 | |
| 9 | /// An accessory to the UEFI memory map and associated metadata that can be |
| 10 | /// either iterated or indexed like an array. |
| 11 | /// |
| 12 | /// A [`MemoryMap`] is always associated with the unique [`MemoryMapKey`] |
| 13 | /// bundled with the map. |
| 14 | /// |
| 15 | /// To iterate over the entries, call [`MemoryMap::entries`]. |
| 16 | /// |
| 17 | /// ## UEFI pitfalls |
| 18 | /// Note that a MemoryMap can quickly become outdated, as soon as any explicit |
| 19 | /// or hidden allocation happens. |
| 20 | /// |
| 21 | /// As soon as boot services are excited, all previous obtained memory maps must |
| 22 | /// be considered as outdated, except if the [`MemoryMapKey`] equals the one |
| 23 | /// returned by `exit_boot_services()`. |
| 24 | /// |
| 25 | /// **Please note** that when working with memory maps, the `entry_size` is |
| 26 | /// usually larger than `size_of::<MemoryDescriptor` [[0]]. So to be safe, |
| 27 | /// always use `entry_size` as step-size when interfacing with the memory map on |
| 28 | /// a low level. |
| 29 | /// |
| 30 | /// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059 |
| 31 | pub trait MemoryMap: Debug + Index<usize, Output = MemoryDescriptor> { |
| 32 | /// Returns the associated [`MemoryMapMeta`]. |
| 33 | #[must_use] |
| 34 | fn meta(&self) -> MemoryMapMeta; |
| 35 | |
| 36 | /// Returns the associated [`MemoryMapKey`]. Note that this isn't |
| 37 | /// necessarily the key of the latest valid UEFI memory map. |
| 38 | #[must_use] |
| 39 | fn key(&self) -> MemoryMapKey; |
| 40 | |
| 41 | /// Returns the number of keys in the map. |
| 42 | #[must_use] |
| 43 | fn len(&self) -> usize; |
| 44 | |
| 45 | /// Returns if the memory map is empty. |
| 46 | #[must_use] |
| 47 | fn is_empty(&self) -> bool { |
| 48 | self.len() == 0 |
| 49 | } |
| 50 | |
| 51 | /// Returns a reference to the [`MemoryDescriptor`] at the given index, if |
| 52 | /// present. |
| 53 | #[must_use] |
| 54 | fn get(&self, index: usize) -> Option<&MemoryDescriptor> { |
| 55 | if index >= self.len() { |
| 56 | None |
| 57 | } else { |
| 58 | let offset = index * self.meta().desc_size; |
| 59 | // SAFETY: The memory is valid. |
| 60 | unsafe { |
| 61 | self.buffer() |
| 62 | .as_ptr() |
| 63 | .add(offset) |
| 64 | .cast::<MemoryDescriptor>() |
| 65 | .as_ref() |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Returns a reference to the underlying memory. |
| 71 | #[must_use] |
| 72 | fn buffer(&self) -> &[u8]; |
| 73 | |
| 74 | /// Returns an Iterator of type [`MemoryMapIter`]. |
| 75 | #[must_use] |
| 76 | fn entries(&self) -> MemoryMapIter<'_>; |
| 77 | |
| 78 | /// Returns if the underlying memory map is sorted regarding the physical |
| 79 | /// address start. |
| 80 | #[must_use] |
| 81 | fn is_sorted(&self) -> bool { |
| 82 | let iter = self.entries(); |
| 83 | let iter = iter.clone().zip(iter.skip(1)); |
| 84 | |
| 85 | for (curr, next) in iter { |
| 86 | if next.phys_start < curr.phys_start { |
| 87 | log::debug!("next.phys_start < curr.phys_start: curr={curr:?}, next={next:?}"); |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | true |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Extension to [`MemoryMap`] that adds mutable operations. This also includes |
| 96 | /// the ability to sort the memory map. |
| 97 | pub trait MemoryMapMut: MemoryMap + IndexMut<usize> { |
| 98 | /// Returns a mutable reference to the [`MemoryDescriptor`] at the given |
| 99 | /// index, if present. |
| 100 | #[must_use] |
| 101 | fn get_mut(&mut self, index: usize) -> Option<&mut MemoryDescriptor> { |
| 102 | if index >= self.len() { |
| 103 | None |
| 104 | } else { |
| 105 | let offset = index * self.meta().desc_size; |
| 106 | // SAFETY: The memory is valid. |
| 107 | unsafe { |
| 108 | self.buffer_mut() |
| 109 | .as_mut_ptr() |
| 110 | .add(offset) |
| 111 | .cast::<MemoryDescriptor>() |
| 112 | .as_mut() |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// Sorts the memory map by physical address in place. This operation is |
| 118 | /// optional and should be invoked only once. |
| 119 | fn sort(&mut self); |
| 120 | |
| 121 | /// Returns a reference to the underlying memory. |
| 122 | /// |
| 123 | /// # Safety |
| 124 | /// |
| 125 | /// This is unsafe as there is a potential to create invalid entries. |
| 126 | unsafe fn buffer_mut(&mut self) -> &mut [u8]; |
| 127 | } |