Skip to content

Commit 99dad6d

Browse files
committed
memory_encryption: reduce function duplication in page_table.rs
1 parent 684aaa7 commit 99dad6d

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

src/structures/paging/page_table.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) static PHYSICAL_ADDRESS_MASK: AtomicU64 = AtomicU64::new(0x000f_ffff_
2828
#[derive(Clone)]
2929
#[repr(transparent)]
3030
pub struct PageTableEntry {
31-
pub(crate) entry: u64,
31+
entry: u64,
3232
}
3333

3434
impl PageTableEntry {
@@ -50,6 +50,18 @@ impl PageTableEntry {
5050
self.entry = 0;
5151
}
5252

53+
/// Returns the flags of this entry.
54+
#[inline]
55+
pub fn flags(&self) -> PageTableFlags {
56+
PageTableFlags::from_bits_retain(self.entry & Self::physical_address_mask())
57+
}
58+
59+
/// Returns the physical address mapped by this entry, might be zero.
60+
#[inline]
61+
pub fn addr(&self) -> PhysAddr {
62+
PhysAddr::new(self.entry & Self::physical_address_mask())
63+
}
64+
5365
/// Returns the physical frame mapped by this entry.
5466
///
5567
/// Returns the following errors:
@@ -68,12 +80,6 @@ impl PageTableEntry {
6880
}
6981
}
7082

71-
/// Sets the flags of this entry.
72-
#[inline]
73-
pub fn set_flags(&mut self, flags: PageTableFlags) {
74-
self.entry = self.addr().as_u64() | flags.bits();
75-
}
76-
7783
/// Map the entry to the specified physical address with the specified flags.
7884
#[inline]
7985
pub fn set_addr(&mut self, addr: PhysAddr, flags: PageTableFlags) {
@@ -87,37 +93,28 @@ impl PageTableEntry {
8793
assert!(!flags.contains(PageTableFlags::HUGE_PAGE));
8894
self.set_addr(frame.start_address(), flags)
8995
}
90-
}
9196

92-
#[cfg(feature = "dynamic_flags")]
93-
impl PageTableEntry {
94-
/// Returns the physical address mapped by this entry, might be zero.
97+
/// Sets the flags of this entry.
9598
#[inline]
96-
pub fn addr(&self) -> PhysAddr {
97-
PhysAddr::new(self.entry & PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed))
99+
pub fn set_flags(&mut self, flags: PageTableFlags) {
100+
self.entry = self.addr().as_u64() | flags.bits();
98101
}
102+
}
99103

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-
)
104+
#[cfg(feature = "dynamic_flags")]
105+
impl PageTableEntry {
106+
#[inline(always)]
107+
fn physical_address_mask() -> u64 {
108+
PHYSICAL_ADDRESS_MASK.load(Ordering::Relaxed)
106109
}
107110
}
108111

109112
#[cfg(not(feature = "dynamic_flags"))]
110113
impl PageTableEntry {
111-
/// Returns the physical address mapped by this entry, might be zero.
112-
#[inline]
113-
pub fn addr(&self) -> PhysAddr {
114-
PhysAddr::new(self.entry & 0x000f_ffff_ffff_f000u64)
115-
}
116-
117-
/// Returns the flags of this entry.
118-
#[inline]
119-
pub fn flags(&self) -> PageTableFlags {
120-
PageTableFlags::from_bits_truncate(self.entry)
114+
#[inline(always)]
115+
#[rustversion::attr(since(1.61), const)]
116+
fn physical_address_mask() -> u64 {
117+
0x000f_ffff_ffff_f000u64
121118
}
122119
}
123120

0 commit comments

Comments
 (0)