@@ -120,15 +120,15 @@ impl Pat {
120
120
/// The underlying model specific register.
121
121
pub const MSR : Msr = Msr ( 0x277 ) ;
122
122
/// 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 ,
132
132
] ;
133
133
}
134
134
@@ -182,48 +182,39 @@ bitflags! {
182
182
}
183
183
184
184
#[ 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 {
188
188
/// Disables caching.
189
- pub const UNCACHEABLE : Self = Self ( 0x00 ) ;
189
+ Uncacheable = 0x00 ,
190
190
/// Uses a write combining cache policy.
191
- pub const WRITE_COMBINING : Self = Self ( 0x01 ) ;
191
+ WriteCombining = 0x01 ,
192
192
/// Uses a write through cache policy.
193
- pub const WRITE_THROUGH : Self = Self ( 0x04 ) ;
193
+ WriteThrough = 0x04 ,
194
194
/// Uses a write protected cache policy.
195
- pub const WRITE_PROTECTED : Self = Self ( 0x05 ) ;
195
+ WriteProtected = 0x05 ,
196
196
/// Uses a write back cache policy.
197
- pub const WRITE_BACK : Self = Self ( 0x06 ) ;
197
+ WriteBack = 0x06 ,
198
198
/// Same as uncacheable, but can be overridden by MTRRs.
199
- pub const UNCACHED : Self = Self ( 0x07 ) ;
200
-
199
+ Uncached = 0x07 ,
200
+ }
201
+ impl PatMemoryType {
201
202
/// Converts from bits, returning `None` if the value is invalid.
202
203
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 ) ,
210
211
_ => None ,
211
212
}
212
213
}
213
214
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
-
224
215
/// Gets the underlying bits.
225
216
pub const fn bits ( self ) -> u8 {
226
- self . 0
217
+ self as u8
227
218
}
228
219
}
229
220
@@ -706,34 +697,32 @@ mod x86_64 {
706
697
impl Pat {
707
698
/// Reads IA32_PAT.
708
699
///
709
- /// # Safety
710
- ///
711
700
/// The PAT must be supported on the CPU, otherwise a general protection exception will
712
701
/// occur. Support can be detected using the `cpuid` instruction.
713
702
#[ inline]
714
- pub unsafe fn read ( ) -> [ PatFlags ; 8 ] {
703
+ pub fn read ( ) -> [ PatMemoryType ; 8 ] {
715
704
let bits = unsafe { Self :: MSR . read ( ) } ;
716
- let mut flags = [ PatFlags :: UNCACHEABLE ; 8 ] ;
705
+ let mut flags = [ PatMemoryType :: Uncacheable ; 8 ] ;
717
706
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 ( ) ;
719
708
}
720
709
flags
721
710
}
722
711
723
712
/// Writes IA32_PAT.
724
713
///
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
+ ///
725
717
/// # Safety
726
718
///
727
719
/// All affected pages must be flushed from the TLB. Processor caches may also need to be
728
720
/// flushed. Additionally, all pages that map to a given frame must have the same memory
729
721
/// 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.
733
722
#[ inline]
734
- pub unsafe fn write ( flags : [ PatFlags ; 8 ] ) {
723
+ pub unsafe fn write ( table : [ PatMemoryType ; 8 ] ) {
735
724
let mut bits = 0u64 ;
736
- for ( i, flag) in flags . iter ( ) . enumerate ( ) {
725
+ for ( i, flag) in table . iter ( ) . enumerate ( ) {
737
726
bits |= ( flag. bits ( ) as u64 ) << ( 8 * i) ;
738
727
}
739
728
let mut msr = Self :: MSR ;
0 commit comments