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 / proto / security / memory_protection.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | use crate::data_types::PhysicalAddress; |
| 4 | use crate::mem::memory_map::MemoryAttribute; |
| 5 | use crate::proto::unsafe_protocol; |
| 6 | use crate::{Result, StatusExt}; |
| 7 | use core::ops::Range; |
| 8 | use uefi_raw::protocol::memory_protection::MemoryAttributeProtocol; |
| 9 | |
| 10 | #[cfg(doc)] |
| 11 | use crate::Status; |
| 12 | |
| 13 | /// Memory Attribute [`Protocol`] for Memory Protection. |
| 14 | /// |
| 15 | /// Protocol for getting and setting memory protection attributes. |
| 16 | /// |
| 17 | /// Corresponds to the C type `EFI_MEMORY_ATTRIBUTE_PROTOCOL`. |
| 18 | /// |
| 19 | /// [`Protocol`]: uefi::proto::Protocol |
| 20 | #[repr(transparent)] |
| 21 | #[derive(Debug)] |
| 22 | #[unsafe_protocol(MemoryAttributeProtocol::GUID)] |
| 23 | pub struct MemoryProtection(MemoryAttributeProtocol); |
| 24 | |
| 25 | impl MemoryProtection { |
| 26 | /// Get the attributes of a memory region. |
| 27 | /// |
| 28 | /// The attribute mask this returns will only contain bits in the |
| 29 | /// set of [`READ_PROTECT`], [`EXECUTE_PROTECT`], and [`READ_ONLY`]. |
| 30 | /// |
| 31 | /// If the attributes are not consistent within the region, |
| 32 | /// [`Status::NO_MAPPING`] is returned. |
| 33 | /// |
| 34 | /// Implementations typically require that the start and end of the memory |
| 35 | /// region are aligned to the [UEFI page size]. |
| 36 | /// |
| 37 | /// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT |
| 38 | /// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT |
| 39 | /// [`READ_ONLY`]: MemoryAttribute::READ_ONLY |
| 40 | /// [UEFI page size]: uefi::boot::PAGE_SIZE |
| 41 | pub fn get_memory_attributes( |
| 42 | &self, |
| 43 | byte_region: Range<PhysicalAddress>, |
| 44 | ) -> Result<MemoryAttribute> { |
| 45 | let mut attributes = MemoryAttribute::empty(); |
| 46 | let (base_address, length) = range_to_base_and_len(byte_region); |
| 47 | // SAFETY: The memory is valid. |
| 48 | unsafe { |
| 49 | (self.0.get_memory_attributes)(&self.0, base_address, length, &mut attributes) |
| 50 | .to_result_with_val(|| attributes) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Set the attributes of a memory region. |
| 55 | /// |
| 56 | /// The valid attributes to set are [`READ_PROTECT`], |
| 57 | /// [`EXECUTE_PROTECT`], and [`READ_ONLY`]. |
| 58 | /// |
| 59 | /// Implementations typically require that the start and end of the memory |
| 60 | /// region are aligned to the [UEFI page size]. |
| 61 | /// |
| 62 | /// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT |
| 63 | /// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT |
| 64 | /// [`READ_ONLY`]: MemoryAttribute::READ_ONLY |
| 65 | /// [UEFI page size]: uefi::boot::PAGE_SIZE |
| 66 | pub fn set_memory_attributes( |
| 67 | &self, |
| 68 | byte_region: Range<PhysicalAddress>, |
| 69 | attributes: MemoryAttribute, |
| 70 | ) -> Result { |
| 71 | let (base_address, length) = range_to_base_and_len(byte_region); |
| 72 | // SAFETY: The memory is valid. |
| 73 | unsafe { |
| 74 | (self.0.set_memory_attributes)(&self.0, base_address, length, attributes).to_result() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Clear the attributes of a memory region. |
| 79 | /// |
| 80 | /// The valid attributes to clear are [`READ_PROTECT`], |
| 81 | /// [`EXECUTE_PROTECT`], and [`READ_ONLY`]. |
| 82 | /// |
| 83 | /// Implementations typically require that the start and end of the memory |
| 84 | /// region are aligned to the [UEFI page size]. |
| 85 | /// |
| 86 | /// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT |
| 87 | /// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT |
| 88 | /// [`READ_ONLY`]: MemoryAttribute::READ_ONLY |
| 89 | /// [UEFI page size]: uefi::boot::PAGE_SIZE |
| 90 | pub fn clear_memory_attributes( |
| 91 | &self, |
| 92 | byte_region: Range<PhysicalAddress>, |
| 93 | attributes: MemoryAttribute, |
| 94 | ) -> Result { |
| 95 | let (base_address, length) = range_to_base_and_len(byte_region); |
| 96 | // SAFETY: The memory is valid. |
| 97 | unsafe { |
| 98 | (self.0.clear_memory_attributes)(&self.0, base_address, length, attributes).to_result() |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Convert a byte `Range` to `(base_address, length)`. |
| 104 | const fn range_to_base_and_len(r: Range<PhysicalAddress>) -> (PhysicalAddress, PhysicalAddress) { |
| 105 | (r.start, r.end.checked_sub(r.start).unwrap()) |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | |
| 112 | #[test] |
| 113 | fn test_range_conversion() { |
| 114 | assert_eq!(range_to_base_and_len(2..5), (2, 3)); |
| 115 | } |
| 116 | } |