Skip to content

Add PhysicalAddress and VirtualAddress type aliases #518

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
Sep 25, 2022
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## uefi - [Unreleased]

### Added

- Added `PhysicalAddress` and `VirtualAddress` type aliases.

### Changed

- Fixed the definition of `AllocateType` so that `MaxAddress` and
`Address` always take a 64-bit value, regardless of target platform.

## uefi-macros - [Unreleased]

## uefi-services - [Unreleased]
Expand Down
8 changes: 8 additions & 0 deletions src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ pub trait Align {
}
}

/// Physical memory address. This is always a 64-bit value, regardless
/// of target platform.
pub type PhysicalAddress = u64;

/// Virtual memory address. This is always a 64-bit value, regardless
/// of target platform.
pub type VirtualAddress = u64;

mod guid;
pub use self::guid::Guid;
pub use self::guid::{unsafe_guid, Identify};
Expand Down
18 changes: 11 additions & 7 deletions src/proto/security/memory_protection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::data_types::PhysicalAddress;
use crate::proto::Protocol;
use crate::table::boot::MemoryAttribute;
use crate::{unsafe_guid, Result, Status};
Expand All @@ -14,21 +15,21 @@ use core::ops::Range;
pub struct MemoryProtection {
get_memory_attributes: unsafe extern "efiapi" fn(
this: *const Self,
base_address: u64,
base_address: PhysicalAddress,
length: u64,
attributes: *mut MemoryAttribute,
) -> Status,

set_memory_attributes: unsafe extern "efiapi" fn(
this: *const Self,
base_address: u64,
base_address: PhysicalAddress,
length: u64,
attributes: MemoryAttribute,
) -> Status,

clear_memory_attributes: unsafe extern "efiapi" fn(
this: *const Self,
base_address: u64,
base_address: PhysicalAddress,
length: u64,
attributes: MemoryAttribute,
) -> Status,
Expand All @@ -46,7 +47,10 @@ impl MemoryProtection {
/// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT
/// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
pub fn get_memory_attributes(&self, byte_region: Range<u64>) -> Result<MemoryAttribute> {
pub fn get_memory_attributes(
&self,
byte_region: Range<PhysicalAddress>,
) -> Result<MemoryAttribute> {
let mut attributes = MemoryAttribute::empty();
let (base_address, length) = range_to_base_and_len(byte_region);
unsafe {
Expand All @@ -65,7 +69,7 @@ impl MemoryProtection {
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
pub fn set_memory_attributes(
&self,
byte_region: Range<u64>,
byte_region: Range<PhysicalAddress>,
attributes: MemoryAttribute,
) -> Result {
let (base_address, length) = range_to_base_and_len(byte_region);
Expand All @@ -82,7 +86,7 @@ impl MemoryProtection {
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
pub fn clear_memory_attributes(
&self,
byte_region: Range<u64>,
byte_region: Range<PhysicalAddress>,
attributes: MemoryAttribute,
) -> Result {
let (base_address, length) = range_to_base_and_len(byte_region);
Expand All @@ -91,7 +95,7 @@ impl MemoryProtection {
}

/// Convert a byte `Range` to `(base_address, length)`.
fn range_to_base_and_len(r: Range<u64>) -> (u64, u64) {
fn range_to_base_and_len(r: Range<PhysicalAddress>) -> (PhysicalAddress, PhysicalAddress) {
(r.start, r.end.checked_sub(r.start).unwrap())
}

Expand Down
22 changes: 11 additions & 11 deletions src/table/boot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! UEFI services available during boot.

use super::{Header, Revision};
use crate::data_types::Align;
use crate::data_types::{Align, PhysicalAddress, VirtualAddress};
use crate::proto::device_path::{DevicePath, FfiDevicePath};
#[cfg(feature = "exts")]
use crate::proto::{loaded_image::LoadedImage, media::fs::SimpleFileSystem};
Expand Down Expand Up @@ -95,9 +95,9 @@ pub struct BootServices {
alloc_ty: u32,
mem_ty: MemoryType,
count: usize,
addr: &mut u64,
addr: &mut PhysicalAddress,
) -> Status,
free_pages: extern "efiapi" fn(addr: u64, pages: usize) -> Status,
free_pages: extern "efiapi" fn(addr: PhysicalAddress, pages: usize) -> Status,
get_memory_map: unsafe extern "efiapi" fn(
size: &mut usize,
map: *mut MemoryDescriptor,
Expand Down Expand Up @@ -324,17 +324,17 @@ impl BootServices {
ty: AllocateType,
mem_ty: MemoryType,
count: usize,
) -> Result<u64> {
) -> Result<PhysicalAddress> {
let (ty, mut addr) = match ty {
AllocateType::AnyPages => (0, 0),
AllocateType::MaxAddress(addr) => (1, addr as u64),
AllocateType::Address(addr) => (2, addr as u64),
AllocateType::MaxAddress(addr) => (1, addr),
AllocateType::Address(addr) => (2, addr),
};
(self.allocate_pages)(ty, mem_ty, count, &mut addr).into_with_val(|| addr)
}

/// Frees memory pages allocated by UEFI.
pub fn free_pages(&self, addr: u64, count: usize) -> Result {
pub fn free_pages(&self, addr: PhysicalAddress, count: usize) -> Result {
(self.free_pages)(addr, count).into()
}

Expand Down Expand Up @@ -1522,9 +1522,9 @@ pub enum AllocateType {
/// Allocate any possible pages.
AnyPages,
/// Allocate pages at any address below the given address.
MaxAddress(usize),
MaxAddress(PhysicalAddress),
/// Allocate pages at the specified address.
Address(usize),
Address(PhysicalAddress),
}

newtype_enum! {
Expand Down Expand Up @@ -1594,9 +1594,9 @@ pub struct MemoryDescriptor {
/// Skip 4 bytes as UEFI declares items in structs should be naturally aligned
padding: u32,
/// Starting physical address.
pub phys_start: u64,
pub phys_start: PhysicalAddress,
/// Starting virtual address.
pub virt_start: u64,
pub virt_start: VirtualAddress,
/// Number of 4 KiB pages contained in this range.
pub page_count: u64,
/// The capability attributes of this memory range.
Expand Down