Skip to content

Commit 0ba90da

Browse files
committed
Apply suggestions
1 parent 447c56d commit 0ba90da

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

src/registers/model_specific.rs

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ impl Pat {
120120
/// The underlying model specific register.
121121
pub const MSR: Msr = Msr(0x277);
122122
/// The default PAT configuration following a power up or reset of the processor.
123-
pub const DEFAULT: [PatFlags; 8] = [
124-
PatFlags::WRITE_BACK,
125-
PatFlags::WRITE_THROUGH,
126-
PatFlags::UNCACHED,
127-
PatFlags::UNCACHEABLE,
128-
PatFlags::WRITE_BACK,
129-
PatFlags::WRITE_THROUGH,
130-
PatFlags::UNCACHED,
131-
PatFlags::UNCACHEABLE,
123+
pub const DEFAULT: [PatMemoryType; 8] = [
124+
PatMemoryType::WriteBack,
125+
PatMemoryType::WriteThrough,
126+
PatMemoryType::Uncached,
127+
PatMemoryType::Uncacheable,
128+
PatMemoryType::WriteBack,
129+
PatMemoryType::WriteThrough,
130+
PatMemoryType::Uncached,
131+
PatMemoryType::Uncacheable,
132132
];
133133
}
134134

@@ -182,48 +182,39 @@ bitflags! {
182182
}
183183

184184
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
185-
/// Flags for the [PAT](Pat).
186-
pub struct PatFlags(u8);
187-
impl PatFlags {
185+
/// Memory types used in the [PAT](Pat).
186+
#[repr(u8)]
187+
pub enum PatMemoryType {
188188
/// Disables caching.
189-
pub const UNCACHEABLE: Self = Self(0x00);
189+
Uncacheable = 0x00,
190190
/// Uses a write combining cache policy.
191-
pub const WRITE_COMBINING: Self = Self(0x01);
191+
WriteCombining = 0x01,
192192
/// Uses a write through cache policy.
193-
pub const WRITE_THROUGH: Self = Self(0x04);
193+
WriteThrough = 0x04,
194194
/// Uses a write protected cache policy.
195-
pub const WRITE_PROTECTED: Self = Self(0x05);
195+
WriteProtected = 0x05,
196196
/// Uses a write back cache policy.
197-
pub const WRITE_BACK: Self = Self(0x06);
197+
WriteBack = 0x06,
198198
/// Same as uncacheable, but can be overridden by MTRRs.
199-
pub const UNCACHED: Self = Self(0x07);
200-
199+
Uncached = 0x07,
200+
}
201+
impl PatMemoryType {
201202
/// Converts from bits, returning `None` if the value is invalid.
202203
pub const fn from_bits(bits: u8) -> Option<Self> {
203-
match Self(bits) {
204-
Self::UNCACHEABLE
205-
| Self::WRITE_COMBINING
206-
| Self::WRITE_THROUGH
207-
| Self::WRITE_PROTECTED
208-
| Self::WRITE_BACK
209-
| Self::UNCACHED => Some(Self(bits)),
204+
match bits {
205+
0x00 => Some(Self::Uncacheable),
206+
0x01 => Some(Self::WriteCombining),
207+
0x04 => Some(Self::WriteThrough),
208+
0x05 => Some(Self::WriteProtected),
209+
0x06 => Some(Self::WriteBack),
210+
0x07 => Some(Self::Uncached),
210211
_ => None,
211212
}
212213
}
213214

214-
/// Converts from bits without checking if the value is valid.
215-
///
216-
/// # Safety
217-
///
218-
/// `bits` must correspond to a valid memory type, otherwise a general protection exception will
219-
/// occur if it is written to the PAT.
220-
pub const unsafe fn from_bits_unchecked(bits: u8) -> Self {
221-
Self(bits)
222-
}
223-
224215
/// Gets the underlying bits.
225216
pub const fn bits(self) -> u8 {
226-
self.0
217+
self as u8
227218
}
228219
}
229220

@@ -706,34 +697,32 @@ mod x86_64 {
706697
impl Pat {
707698
/// Reads IA32_PAT.
708699
///
709-
/// # Safety
710-
///
711700
/// The PAT must be supported on the CPU, otherwise a general protection exception will
712701
/// occur. Support can be detected using the `cpuid` instruction.
713702
#[inline]
714-
pub unsafe fn read() -> [PatFlags; 8] {
703+
pub fn read() -> [PatMemoryType; 8] {
715704
let bits = unsafe { Self::MSR.read() };
716-
let mut flags = [PatFlags::UNCACHEABLE; 8];
705+
let mut flags = [PatMemoryType::Uncacheable; 8];
717706
for (i, flag) in flags.iter_mut().enumerate() {
718-
*flag = PatFlags((bits >> (8 * i)) as u8);
707+
*flag = PatMemoryType::from_bits((bits >> (8 * i)) as u8).unwrap();
719708
}
720709
flags
721710
}
722711

723712
/// Writes IA32_PAT.
724713
///
714+
/// The PAT must be supported on the CPU, otherwise a general protection exception will
715+
/// occur. Support can be detected using the `cpuid` instruction.
716+
///
725717
/// # Safety
726718
///
727719
/// All affected pages must be flushed from the TLB. Processor caches may also need to be
728720
/// flushed. Additionally, all pages that map to a given frame must have the same memory
729721
/// type.
730-
///
731-
/// The PAT must be supported on the CPU, otherwise a general protection exception will
732-
/// occur. Support can be detected using the `cpuid` instruction.
733722
#[inline]
734-
pub unsafe fn write(flags: [PatFlags; 8]) {
723+
pub unsafe fn write(table: [PatMemoryType; 8]) {
735724
let mut bits = 0u64;
736-
for (i, flag) in flags.iter().enumerate() {
725+
for (i, flag) in table.iter().enumerate() {
737726
bits |= (flag.bits() as u64) << (8 * i);
738727
}
739728
let mut msr = Self::MSR;

src/structures/paging/mapper/mapped_page_table.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ impl<P: PageTableFrameMapping> Mapper<Size4KiB> for MappedPageTable<'_, P> {
421421

422422
let frame = p1_entry.frame().map_err(|err| match err {
423423
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
424+
#[allow(deprecated)]
425+
FrameError::HugeFrame => unreachable!(),
424426
})?;
425427

426428
p1_entry.set_unused();
@@ -837,6 +839,8 @@ impl From<FrameError> for PageTableWalkError {
837839
#[inline]
838840
fn from(err: FrameError) -> Self {
839841
match err {
842+
#[allow(deprecated)]
843+
FrameError::HugeFrame => unreachable!(),
840844
FrameError::FrameNotPresent => PageTableWalkError::NotMapped,
841845
}
842846
}

src/structures/paging/mapper/recursive_page_table.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ impl Mapper<Size1GiB> for RecursivePageTable<'_> {
327327
}
328328
p4_entry.frame::<Size4KiB>().map_err(|err| match err {
329329
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
330+
#[allow(deprecated)]
331+
FrameError::HugeFrame => unreachable!(),
330332
})?;
331333

332334
let p3 = unsafe { &mut *(p3_ptr(page, self.recursive_index)) };
@@ -448,6 +450,8 @@ impl Mapper<Size2MiB> for RecursivePageTable<'_> {
448450
}
449451
p4_entry.frame::<Size4KiB>().map_err(|err| match err {
450452
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
453+
#[allow(deprecated)]
454+
FrameError::HugeFrame => unreachable!(),
451455
})?;
452456

453457
let p3 = unsafe { &mut *(p3_ptr(page, self.recursive_index)) };
@@ -457,6 +461,8 @@ impl Mapper<Size2MiB> for RecursivePageTable<'_> {
457461
}
458462
p3_entry.frame::<Size4KiB>().map_err(|err| match err {
459463
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
464+
#[allow(deprecated)]
465+
FrameError::HugeFrame => unreachable!(),
460466
})?;
461467

462468
let p2 = unsafe { &mut *(p2_ptr(page, self.recursive_index)) };
@@ -607,6 +613,8 @@ impl Mapper<Size4KiB> for RecursivePageTable<'_> {
607613
}
608614
p4_entry.frame::<Size4KiB>().map_err(|err| match err {
609615
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
616+
#[allow(deprecated)]
617+
FrameError::HugeFrame => unreachable!(),
610618
})?;
611619

612620
let p3 = unsafe { &mut *(p3_ptr(page, self.recursive_index)) };
@@ -616,6 +624,8 @@ impl Mapper<Size4KiB> for RecursivePageTable<'_> {
616624
}
617625
p3_entry.frame::<Size4KiB>().map_err(|err| match err {
618626
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
627+
#[allow(deprecated)]
628+
FrameError::HugeFrame => unreachable!(),
619629
})?;
620630

621631
let p2 = unsafe { &mut *(p2_ptr(page, self.recursive_index)) };
@@ -625,13 +635,17 @@ impl Mapper<Size4KiB> for RecursivePageTable<'_> {
625635
}
626636
p2_entry.frame::<Size4KiB>().map_err(|err| match err {
627637
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
638+
#[allow(deprecated)]
639+
FrameError::HugeFrame => unreachable!(),
628640
})?;
629641

630642
let p1 = unsafe { &mut *(p1_ptr(page, self.recursive_index)) };
631643
let p1_entry = &mut p1[page.p1_index()];
632644

633645
let frame = p1_entry.frame().map_err(|err| match err {
634646
FrameError::FrameNotPresent => UnmapError::PageNotMapped,
647+
#[allow(deprecated)]
648+
FrameError::HugeFrame => unreachable!(),
635649
})?;
636650

637651
p1_entry.set_unused();

src/structures/paging/page_table.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use bitflags::bitflags;
1515
pub enum FrameError {
1616
/// The entry does not have the `PRESENT` flag set, so it isn't currently mapped to a frame.
1717
FrameNotPresent,
18+
#[deprecated = "`HugeFrame` is no longer returned by the `frame` method, as it can now return huge pages"]
19+
/// The entry does have the `HUGE_PAGE` flag set.
20+
HugeFrame,
1821
}
1922

2023
/// A 64-bit page table entry.

0 commit comments

Comments
 (0)