Skip to content

Commit f8d48b9

Browse files
authored
Merge pull request #791 from nicholasbishop/bishop-raw-mem-types
Move various memory types to uefi-raw and re-export in uefi
2 parents ed34335 + 3fc9540 commit f8d48b9

File tree

2 files changed

+144
-141
lines changed

2 files changed

+144
-141
lines changed

uefi-raw/src/table/boot.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,147 @@
11
//! UEFI services available during boot.
22
3+
use crate::{PhysicalAddress, VirtualAddress};
4+
use bitflags::bitflags;
5+
6+
bitflags! {
7+
/// Flags describing the capabilities of a memory range.
8+
#[repr(transparent)]
9+
pub struct MemoryAttribute: u64 {
10+
/// Supports marking as uncacheable.
11+
const UNCACHEABLE = 0x1;
12+
/// Supports write-combining.
13+
const WRITE_COMBINE = 0x2;
14+
/// Supports write-through.
15+
const WRITE_THROUGH = 0x4;
16+
/// Support write-back.
17+
const WRITE_BACK = 0x8;
18+
/// Supports marking as uncacheable, exported and
19+
/// supports the "fetch and add" semaphore mechanism.
20+
const UNCACHABLE_EXPORTED = 0x10;
21+
/// Supports write-protection.
22+
const WRITE_PROTECT = 0x1000;
23+
/// Supports read-protection.
24+
const READ_PROTECT = 0x2000;
25+
/// Supports disabling code execution.
26+
const EXECUTE_PROTECT = 0x4000;
27+
/// Persistent memory.
28+
const NON_VOLATILE = 0x8000;
29+
/// This memory region is more reliable than other memory.
30+
const MORE_RELIABLE = 0x10000;
31+
/// This memory range can be set as read-only.
32+
const READ_ONLY = 0x20000;
33+
/// This memory is earmarked for specific purposes such as for specific
34+
/// device drivers or applications. This serves as a hint to the OS to
35+
/// avoid this memory for core OS data or code that cannot be relocated.
36+
const SPECIAL_PURPOSE = 0x4_0000;
37+
/// This memory region is capable of being protected with the CPU's memory
38+
/// cryptography capabilities.
39+
const CPU_CRYPTO = 0x8_0000;
40+
/// This memory must be mapped by the OS when a runtime service is called.
41+
const RUNTIME = 0x8000_0000_0000_0000;
42+
/// This memory region is described with additional ISA-specific memory
43+
/// attributes as specified in `MemoryAttribute::ISA_MASK`.
44+
const ISA_VALID = 0x4000_0000_0000_0000;
45+
/// These bits are reserved for describing optional ISA-specific cache-
46+
/// ability attributes that are not covered by the standard UEFI Memory
47+
/// Attribute cacheability bits such as `UNCACHEABLE`, `WRITE_COMBINE`,
48+
/// `WRITE_THROUGH`, `WRITE_BACK`, and `UNCACHEABLE_EXPORTED`.
49+
///
50+
/// See Section 2.3 "Calling Conventions" in the UEFI Specification
51+
/// for further information on each ISA that takes advantage of this.
52+
const ISA_MASK = 0x0FFF_F000_0000_0000;
53+
}
54+
}
55+
56+
/// A structure describing a region of memory.
57+
#[derive(Debug, Copy, Clone)]
58+
#[repr(C)]
59+
pub struct MemoryDescriptor {
60+
/// Type of memory occupying this range.
61+
pub ty: MemoryType,
62+
/// Starting physical address.
63+
pub phys_start: PhysicalAddress,
64+
/// Starting virtual address.
65+
pub virt_start: VirtualAddress,
66+
/// Number of 4 KiB pages contained in this range.
67+
pub page_count: u64,
68+
/// The capability attributes of this memory range.
69+
pub att: MemoryAttribute,
70+
}
71+
72+
impl MemoryDescriptor {
73+
/// Memory descriptor version number.
74+
pub const VERSION: u32 = 1;
75+
}
76+
77+
impl Default for MemoryDescriptor {
78+
fn default() -> MemoryDescriptor {
79+
MemoryDescriptor {
80+
ty: MemoryType::RESERVED,
81+
phys_start: 0,
82+
virt_start: 0,
83+
page_count: 0,
84+
att: MemoryAttribute::empty(),
85+
}
86+
}
87+
}
88+
89+
newtype_enum! {
90+
/// The type of a memory range.
91+
///
92+
/// UEFI allows firmwares and operating systems to introduce new memory types
93+
/// in the 0x70000000..0xFFFFFFFF range. Therefore, we don't know the full set
94+
/// of memory types at compile time, and it is _not_ safe to model this C enum
95+
/// as a Rust enum.
96+
pub enum MemoryType: u32 => {
97+
/// This enum variant is not used.
98+
RESERVED = 0,
99+
/// The code portions of a loaded UEFI application.
100+
LOADER_CODE = 1,
101+
/// The data portions of a loaded UEFI applications,
102+
/// as well as any memory allocated by it.
103+
LOADER_DATA = 2,
104+
/// Code of the boot drivers.
105+
///
106+
/// Can be reused after OS is loaded.
107+
BOOT_SERVICES_CODE = 3,
108+
/// Memory used to store boot drivers' data.
109+
///
110+
/// Can be reused after OS is loaded.
111+
BOOT_SERVICES_DATA = 4,
112+
/// Runtime drivers' code.
113+
RUNTIME_SERVICES_CODE = 5,
114+
/// Runtime services' code.
115+
RUNTIME_SERVICES_DATA = 6,
116+
/// Free usable memory.
117+
CONVENTIONAL = 7,
118+
/// Memory in which errors have been detected.
119+
UNUSABLE = 8,
120+
/// Memory that holds ACPI tables.
121+
/// Can be reclaimed after they are parsed.
122+
ACPI_RECLAIM = 9,
123+
/// Firmware-reserved addresses.
124+
ACPI_NON_VOLATILE = 10,
125+
/// A region used for memory-mapped I/O.
126+
MMIO = 11,
127+
/// Address space used for memory-mapped port I/O.
128+
MMIO_PORT_SPACE = 12,
129+
/// Address space which is part of the processor.
130+
PAL_CODE = 13,
131+
/// Memory region which is usable and is also non-volatile.
132+
PERSISTENT_MEMORY = 14,
133+
}}
134+
135+
impl MemoryType {
136+
/// Construct a custom `MemoryType`. Values in the range `0x80000000..=0xffffffff` are free for use if you are
137+
/// an OS loader.
138+
#[must_use]
139+
pub const fn custom(value: u32) -> MemoryType {
140+
assert!(value >= 0x80000000);
141+
MemoryType(value)
142+
}
143+
}
144+
3145
newtype_enum! {
4146
/// Task priority level.
5147
///

uefi/src/table/boot.rs

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! UEFI services available during boot.
22
33
use super::{Header, Revision};
4-
use crate::data_types::{Align, PhysicalAddress, VirtualAddress};
4+
use crate::data_types::{Align, PhysicalAddress};
55
use crate::proto::device_path::{DevicePath, FfiDevicePath};
66
use crate::proto::{Protocol, ProtocolPointer};
77
use crate::{Char16, Event, Guid, Handle, Result, Status, StatusExt};
@@ -20,7 +20,7 @@ use {
2020
::alloc::vec::Vec,
2121
};
2222

23-
pub use uefi_raw::table::boot::Tpl;
23+
pub use uefi_raw::table::boot::{MemoryAttribute, MemoryDescriptor, MemoryType, Tpl};
2424

2525
// TODO: this similar to `SyncUnsafeCell`. Once that is stabilized we
2626
// can use it instead.
@@ -1814,151 +1814,12 @@ pub enum AllocateType {
18141814
Address(PhysicalAddress),
18151815
}
18161816

1817-
newtype_enum! {
1818-
/// The type of a memory range.
1819-
///
1820-
/// UEFI allows firmwares and operating systems to introduce new memory types
1821-
/// in the 0x70000000..0xFFFFFFFF range. Therefore, we don't know the full set
1822-
/// of memory types at compile time, and it is _not_ safe to model this C enum
1823-
/// as a Rust enum.
1824-
pub enum MemoryType: u32 => {
1825-
/// This enum variant is not used.
1826-
RESERVED = 0,
1827-
/// The code portions of a loaded UEFI application.
1828-
LOADER_CODE = 1,
1829-
/// The data portions of a loaded UEFI applications,
1830-
/// as well as any memory allocated by it.
1831-
LOADER_DATA = 2,
1832-
/// Code of the boot drivers.
1833-
///
1834-
/// Can be reused after OS is loaded.
1835-
BOOT_SERVICES_CODE = 3,
1836-
/// Memory used to store boot drivers' data.
1837-
///
1838-
/// Can be reused after OS is loaded.
1839-
BOOT_SERVICES_DATA = 4,
1840-
/// Runtime drivers' code.
1841-
RUNTIME_SERVICES_CODE = 5,
1842-
/// Runtime services' code.
1843-
RUNTIME_SERVICES_DATA = 6,
1844-
/// Free usable memory.
1845-
CONVENTIONAL = 7,
1846-
/// Memory in which errors have been detected.
1847-
UNUSABLE = 8,
1848-
/// Memory that holds ACPI tables.
1849-
/// Can be reclaimed after they are parsed.
1850-
ACPI_RECLAIM = 9,
1851-
/// Firmware-reserved addresses.
1852-
ACPI_NON_VOLATILE = 10,
1853-
/// A region used for memory-mapped I/O.
1854-
MMIO = 11,
1855-
/// Address space used for memory-mapped port I/O.
1856-
MMIO_PORT_SPACE = 12,
1857-
/// Address space which is part of the processor.
1858-
PAL_CODE = 13,
1859-
/// Memory region which is usable and is also non-volatile.
1860-
PERSISTENT_MEMORY = 14,
1861-
}}
1862-
1863-
impl MemoryType {
1864-
/// Construct a custom `MemoryType`. Values in the range `0x80000000..=0xffffffff` are free for use if you are
1865-
/// an OS loader.
1866-
#[must_use]
1867-
pub const fn custom(value: u32) -> MemoryType {
1868-
assert!(value >= 0x80000000);
1869-
MemoryType(value)
1870-
}
1871-
}
1872-
1873-
/// A structure describing a region of memory.
1874-
#[derive(Debug, Copy, Clone)]
1875-
#[repr(C)]
1876-
pub struct MemoryDescriptor {
1877-
/// Type of memory occupying this range.
1878-
pub ty: MemoryType,
1879-
/// Starting physical address.
1880-
pub phys_start: PhysicalAddress,
1881-
/// Starting virtual address.
1882-
pub virt_start: VirtualAddress,
1883-
/// Number of 4 KiB pages contained in this range.
1884-
pub page_count: u64,
1885-
/// The capability attributes of this memory range.
1886-
pub att: MemoryAttribute,
1887-
}
1888-
1889-
impl MemoryDescriptor {
1890-
/// Memory descriptor version number.
1891-
pub const VERSION: u32 = 1;
1892-
}
1893-
1894-
impl Default for MemoryDescriptor {
1895-
fn default() -> MemoryDescriptor {
1896-
MemoryDescriptor {
1897-
ty: MemoryType::RESERVED,
1898-
phys_start: 0,
1899-
virt_start: 0,
1900-
page_count: 0,
1901-
att: MemoryAttribute::empty(),
1902-
}
1903-
}
1904-
}
1905-
19061817
impl Align for MemoryDescriptor {
19071818
fn alignment() -> usize {
19081819
mem::align_of::<Self>()
19091820
}
19101821
}
19111822

1912-
bitflags! {
1913-
/// Flags describing the capabilities of a memory range.
1914-
#[repr(transparent)]
1915-
pub struct MemoryAttribute: u64 {
1916-
/// Supports marking as uncacheable.
1917-
const UNCACHEABLE = 0x1;
1918-
/// Supports write-combining.
1919-
const WRITE_COMBINE = 0x2;
1920-
/// Supports write-through.
1921-
const WRITE_THROUGH = 0x4;
1922-
/// Support write-back.
1923-
const WRITE_BACK = 0x8;
1924-
/// Supports marking as uncacheable, exported and
1925-
/// supports the "fetch and add" semaphore mechanism.
1926-
const UNCACHABLE_EXPORTED = 0x10;
1927-
/// Supports write-protection.
1928-
const WRITE_PROTECT = 0x1000;
1929-
/// Supports read-protection.
1930-
const READ_PROTECT = 0x2000;
1931-
/// Supports disabling code execution.
1932-
const EXECUTE_PROTECT = 0x4000;
1933-
/// Persistent memory.
1934-
const NON_VOLATILE = 0x8000;
1935-
/// This memory region is more reliable than other memory.
1936-
const MORE_RELIABLE = 0x10000;
1937-
/// This memory range can be set as read-only.
1938-
const READ_ONLY = 0x20000;
1939-
/// This memory is earmarked for specific purposes such as for specific
1940-
/// device drivers or applications. This serves as a hint to the OS to
1941-
/// avoid this memory for core OS data or code that cannot be relocated.
1942-
const SPECIAL_PURPOSE = 0x4_0000;
1943-
/// This memory region is capable of being protected with the CPU's memory
1944-
/// cryptography capabilities.
1945-
const CPU_CRYPTO = 0x8_0000;
1946-
/// This memory must be mapped by the OS when a runtime service is called.
1947-
const RUNTIME = 0x8000_0000_0000_0000;
1948-
/// This memory region is described with additional ISA-specific memory
1949-
/// attributes as specified in `MemoryAttribute::ISA_MASK`.
1950-
const ISA_VALID = 0x4000_0000_0000_0000;
1951-
/// These bits are reserved for describing optional ISA-specific cache-
1952-
/// ability attributes that are not covered by the standard UEFI Memory
1953-
/// Attribute cacheability bits such as `UNCACHEABLE`, `WRITE_COMBINE`,
1954-
/// `WRITE_THROUGH`, `WRITE_BACK`, and `UNCACHEABLE_EXPORTED`.
1955-
///
1956-
/// See Section 2.3 "Calling Conventions" in the UEFI Specification
1957-
/// for further information on each ISA that takes advantage of this.
1958-
const ISA_MASK = 0x0FFF_F000_0000_0000;
1959-
}
1960-
}
1961-
19621823
/// A unique identifier of a memory map.
19631824
///
19641825
/// If the memory map changes, this value is no longer valid.

0 commit comments

Comments
 (0)