Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions uefi-test-runner/src/boot/memory.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use alloc::vec::Vec;
use uefi::boot;
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
use uefi_raw::table::boot::MemoryType;

pub fn test() {
info!("Testing memory functions");

bootservices::allocate_pages();
bootservices::allocate_pool();
bootservices::test_memory_map();

global::alloc_vec();
global::alloc_alignment();
Expand All @@ -19,8 +15,10 @@ pub fn test() {

/// Tests that directly use UEFI boot services to allocate memory.
mod bootservices {
use alloc::vec::Vec;
use uefi::boot;
use uefi::boot::AllocateType;
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
use uefi_raw::table::boot::MemoryType;

/// Tests the `allocate_pages` boot service.
Expand Down Expand Up @@ -53,6 +51,44 @@ mod bootservices {
}
unsafe { boot::free_pool(ptr) }.unwrap();
}

/// Tests getting the memory map.
pub fn test_memory_map() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this function seems unrelated to the rest of the PR -- could you drop this change or move it to a separate PR?

info!("Testing memory map functions");

let mut memory_map =
boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to retrieve UEFI memory map");

memory_map.sort();

// Collect the descriptors into a vector
let descriptors = memory_map.entries().copied().collect::<Vec<_>>();

// Ensured we have at least one entry.
// Real memory maps usually have dozens of entries.
assert!(!descriptors.is_empty(), "Memory map is empty");

let mut curr_value = descriptors[0];

for value in descriptors.iter().skip(1) {
if value.phys_start <= curr_value.phys_start {
panic!("memory map sorting failed");
}
curr_value = *value;
}

// This is pretty much a basic sanity test to ensure returned memory
// isn't filled with random values.
let first_desc = descriptors[0];

#[cfg(target_arch = "x86_64")]
{
let phys_start = first_desc.phys_start;
assert_eq!(phys_start, 0, "Memory does not start at address 0");
}
let page_count = first_desc.page_count;
assert!(page_count != 0, "Memory map entry has size zero");
}
}

/// Tests that use [`uefi::allocator::Allocator`], which is configured as the
Expand Down Expand Up @@ -97,40 +133,3 @@ mod global {
}
}
}

fn test_memory_map() {
info!("Testing memory map functions");

let mut memory_map =
boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to retrieve UEFI memory map");

memory_map.sort();

// Collect the descriptors into a vector
let descriptors = memory_map.entries().copied().collect::<Vec<_>>();

// Ensured we have at least one entry.
// Real memory maps usually have dozens of entries.
assert!(!descriptors.is_empty(), "Memory map is empty");

let mut curr_value = descriptors[0];

for value in descriptors.iter().skip(1) {
if value.phys_start <= curr_value.phys_start {
panic!("memory map sorting failed");
}
curr_value = *value;
}

// This is pretty much a basic sanity test to ensure returned memory
// isn't filled with random values.
let first_desc = descriptors[0];

#[cfg(target_arch = "x86_64")]
{
let phys_start = first_desc.phys_start;
assert_eq!(phys_start, 0, "Memory does not start at address 0");
}
let page_count = first_desc.page_count;
assert!(page_count != 0, "Memory map entry has size zero");
}