Skip to content

mem: clarify confusion around MemoryDescriptor #1174

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

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion uefi-raw/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,30 @@ bitflags! {
}
}

/// A structure describing a region of memory.
/// A structure describing a region of memory. This type corresponds to [version]
/// of this struct in the UEFI spec and is always bound to a corresponding
/// UEFI memory map.
///
/// # UEFI pitfalls
/// As of May 2024:
/// The memory descriptor struct might be extended in the future by a new UEFI
/// spec revision, which will be reflected in another `version` of that
/// descriptor. The version is reported when using `get_memory_map` of
/// [`BootServices`].
///
/// Also note that you **must never** work with `size_of::<MemoryDescriptor>`
/// but always with `desc_size`, which is reported when using `get_memory_map`
/// as well [[0]]. For example, although the actual size is of version 1
/// descriptors is `40`, the reported `desc_size` is `48`.
///
/// [version]: MemoryDescriptor::VERSION
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct MemoryDescriptor {
/// Type of memory occupying this range.
pub ty: MemoryType,
// Implicit 32-bit padding.
/// Starting physical address.
pub phys_start: PhysicalAddress,
/// Starting virtual address.
Expand Down
28 changes: 24 additions & 4 deletions uefi/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,21 +1617,35 @@ pub struct MemoryMapSize {
/// An accessory to the memory map that can be either iterated or
/// indexed like an array.
///
/// A [`MemoryMap`] is always associated with the
/// unique [`MemoryMapKey`] contained in the struct.
/// A [`MemoryMap`] is always associated with the unique [`MemoryMapKey`]
/// contained in the struct.
///
/// To iterate over the entries, call [`MemoryMap::entries`]. To get a sorted
/// map, you manually have to call [`MemoryMap::sort`] first.
///
/// ## UEFI pitfalls
/// **Please note that when working with memory maps, the `entry_size` is
/// usually larger than `size_of::<MemoryDescriptor` [[0]]. So to be safe,
/// always use `entry_size` as step-size when interfacing with the memory map on
/// a low level.
///
///
///
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
#[derive(Debug)]
pub struct MemoryMap<'buf> {
key: MemoryMapKey,
buf: &'buf mut [u8],
/// Usually bound to the size of a [`MemoryDescriptor`] but can indicate if
/// this field is ever extended by a new UEFI standard.
entry_size: usize,
len: usize,
}

impl<'buf> MemoryMap<'buf> {
/// Creates a [`MemoryMap`] from the given buffer and entry size.
/// The entry size is usually bound to the size of a [`MemoryDescriptor`]
/// but can indicate if this field is ever extended by a new UEFI standard.
///
/// This allows parsing a memory map provided by a kernel after boot
/// services have already exited.
Expand Down Expand Up @@ -1722,8 +1736,14 @@ impl<'buf> MemoryMap<'buf> {
elem.phys_start
}

/// Returns an iterator over the contained memory map. To get a sorted map,
/// call [`MemoryMap::sort`] first.
/// Returns an [`MemoryMapIter`] emitting [`MemoryDescriptor`]s.
///
/// To get a sorted map, call [`MemoryMap::sort`] first.
///
/// # UEFI pitfalls
/// Currently, only the descriptor version specified in
/// [`MemoryDescriptor`] is supported. This is going to change if the UEFI
/// spec ever introduces a new memory descriptor version.
#[must_use]
pub fn entries(&self) -> MemoryMapIter {
MemoryMapIter {
Expand Down