Skip to content

Commit 073c196

Browse files
committed
x86_64/memory_encryption: fix review and clippy issues
1 parent 0a8a281 commit 073c196

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/structures/mem_encrypt.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! Provides glue to support memory encryption features of some CPUs.
2+
//!
3+
//! Memory encryption typically relies on using a physical address bit in the page table entry to
4+
//! mark a page as encrypted. This module provides a function that, given a c-bit, updates the
5+
//! dynamic page-table flags structure to ensure that the flag is treated properly and not
6+
//! returned as part of the physical address.
7+
18
use core::sync::atomic::{AtomicU64, Ordering};
29

310
use crate::structures::paging::page_table::PHYSICAL_ADDRESS_MASK;

src/structures/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod gdt;
66

77
pub mod idt;
88

9-
#[cfg(feature = "amd_sev")]
9+
#[cfg(feature = "memory_encryption")]
1010
pub mod mem_encrypt;
1111
pub mod paging;
1212
pub mod port;

src/structures/paging/page_table.rs

+19-32
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
33
use super::{PageSize, PhysFrame, Size4KiB};
44
use crate::addr::PhysAddr;
5+
use bitflags::bitflags;
56
use core::fmt;
67
#[cfg(feature = "step_trait")]
78
use core::iter::Step;
89
use core::ops::{Index, IndexMut};
10+
#[cfg(feature = "dynamic_flags")]
911
use core::sync::atomic::{AtomicU64, Ordering};
10-
use bitflags::bitflags;
1112

1213
/// The error returned by the `PageTableEntry::frame` method.
1314
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -49,19 +50,13 @@ impl PageTableEntry {
4950
self.entry = 0;
5051
}
5152

52-
/// Returns the flags of this entry.
53-
#[inline]
54-
pub fn flags(&self) -> PageTableFlags {
55-
PageTableFlags::from_address_truncated(self.entry)
56-
}
57-
5853
/// Returns the physical frame mapped by this entry.
5954
///
6055
/// Returns the following errors:
6156
///
6257
/// - `FrameError::FrameNotPresent` if the entry doesn't have the `PRESENT` flag set.
6358
/// - `FrameError::HugeFrame` if the entry has the `HUGE_PAGE` flag set (for huge pages the
64-
/// `addr` function must be used)
59+
/// `addr` function must be used)
6560
#[inline]
6661
pub fn frame(&self) -> Result<PhysFrame, FrameError> {
6762
if !self.flags().contains(PageTableFlags::PRESENT) {
@@ -76,14 +71,14 @@ impl PageTableEntry {
7671
/// Sets the flags of this entry.
7772
#[inline]
7873
pub fn set_flags(&mut self, flags: PageTableFlags) {
79-
self.entry = self.addr().as_u64() | flags.bits_dynamic();
74+
self.entry = self.addr().as_u64() | flags.bits();
8075
}
8176

8277
/// Map the entry to the specified physical address with the specified flags.
8378
#[inline]
8479
pub fn set_addr(&mut self, addr: PhysAddr, flags: PageTableFlags) {
8580
assert!(addr.is_aligned(Size4KiB::SIZE));
86-
self.entry = (addr.as_u64()) | flags.bits_dynamic();
81+
self.entry = (addr.as_u64()) | flags.bits();
8782
}
8883

8984
/// Map the entry to the specified physical frame with the specified flags.
@@ -101,6 +96,14 @@ impl PageTableEntry {
10196
pub fn addr(&self) -> PhysAddr {
10297
PhysAddr::new(self.entry & PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed))
10398
}
99+
100+
/// Returns the flags of this entry.
101+
#[inline]
102+
pub fn flags(&self) -> PageTableFlags {
103+
PageTableFlags::from_bits_retain(
104+
self.entry & !PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed),
105+
)
106+
}
104107
}
105108

106109
#[cfg(not(feature = "dynamic_flags"))]
@@ -110,6 +113,12 @@ impl PageTableEntry {
110113
pub fn addr(&self) -> PhysAddr {
111114
PhysAddr::new(self.entry & 0x000f_ffff_ffff_f000u64)
112115
}
116+
117+
/// Returns the flags of this entry.
118+
#[inline]
119+
pub fn flags(&self) -> PageTableFlags {
120+
PageTableFlags::from_bits_truncate(self.entry)
121+
}
113122
}
114123

115124
impl Default for PageTableEntry {
@@ -193,28 +202,6 @@ bitflags! {
193202
}
194203
}
195204

196-
#[cfg(feature = "dynamic_flags")]
197-
impl PageTableFlags {
198-
pub fn from_address_truncated(address: u64) -> PageTableFlags {
199-
PageTableFlags::from_bits_retain(address & !PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed))
200-
}
201-
202-
pub fn bits_dynamic(&self) -> u64 {
203-
self.bits() & !PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed)
204-
}
205-
}
206-
207-
#[cfg(not(feature = "dynamic_flags"))]
208-
impl PageTableFlags {
209-
pub const fn from_address_truncated(address: u64) -> PageTableFlags {
210-
PageTableFlags::from_bits_truncate(address)
211-
}
212-
213-
pub const fn bits_dynamic(&self) -> u64 {
214-
self.bits()
215-
}
216-
}
217-
218205
/// The number of entries in a page table.
219206
const ENTRY_COUNT: usize = 512;
220207

0 commit comments

Comments
 (0)