x64 0.16.2

x86_64 support with LA57 (5-level paging) for 57-bit virtual addresses
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! Functions to read and write model specific registers.

use bitflags::bitflags;
// imports for intra doc links
#[cfg(doc)]
use crate::registers::segmentation::{FS, GS};

/// A model specific register.
#[cfg_attr(
    not(all(feature = "instructions", target_arch = "x86_64")),
    allow(dead_code)
)] // FIXME
#[derive(Debug)]
pub struct Msr(u32);

impl Msr {
    /// Create an instance from a register.
    #[inline]
    pub const fn new(reg: u32) -> Msr {
        Msr(reg)
    }
}

/// The Extended Feature Enable Register.
#[derive(Debug)]
pub struct Efer;

/// [FS].Base Model Specific Register.
#[derive(Debug)]
pub struct FsBase;

/// [GS].Base Model Specific Register.
///
#[cfg_attr(
    all(feature = "instructions", target_arch = "x86_64"),
    doc = "[`GS::swap`] swaps this register with [`KernelGsBase`]."
)]
#[derive(Debug)]
pub struct GsBase;

/// KernelGsBase Model Specific Register.
///
#[cfg_attr(
    all(feature = "instructions", target_arch = "x86_64"),
    doc = "[`GS::swap`] swaps this register with [`GsBase`]."
)]
#[derive(Debug)]
pub struct KernelGsBase;

/// Syscall Register: STAR
#[derive(Debug)]
pub struct Star;

/// Syscall Register: LSTAR
#[derive(Debug)]
pub struct LStar;

/// Syscall Register: SFMASK
#[doc(alias = "FMask")]
#[derive(Debug)]
pub struct SFMask;

/// IA32_U_CET: user mode CET configuration
#[derive(Debug)]
pub struct UCet;

/// IA32_S_CET: supervisor mode CET configuration
#[derive(Debug)]
pub struct SCet;

/// IA32_APIC_BASE: status and location of the local APIC
///
/// IA32_APIC_BASE must be supported on the CPU, otherwise, a general protection exception will occur. Support can be detected using the `cpuid` instruction.
#[derive(Debug)]
pub struct ApicBase;

impl Efer {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0080);
}

impl FsBase {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0100);
}

impl GsBase {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0101);
}

impl KernelGsBase {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0102);
}

impl Star {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0081);
}

impl LStar {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0082);
}

impl SFMask {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0xC000_0084);
}

impl UCet {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0x6A0);
}

impl SCet {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0x6A2);
}

impl ApicBase {
    /// The underlying model specific register.
    pub const MSR: Msr = Msr(0x1B);
}

bitflags! {
    /// Flags of the Extended Feature Enable Register.
    #[repr(transparent)]
    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
    pub struct EferFlags: u64 {
        /// Enables the `syscall` and `sysret` instructions.
        const SYSTEM_CALL_EXTENSIONS = 1;
        /// Activates long mode, requires activating paging.
        const LONG_MODE_ENABLE = 1 << 8;
        /// Indicates that long mode is active.
        const LONG_MODE_ACTIVE = 1 << 10;
        /// Enables the no-execute page-protection feature.
        const NO_EXECUTE_ENABLE = 1 << 11;
        /// Enables SVM extensions.
        const SECURE_VIRTUAL_MACHINE_ENABLE = 1 << 12;
        /// Enable certain limit checks in 64-bit mode.
        const LONG_MODE_SEGMENT_LIMIT_ENABLE = 1 << 13;
        /// Enable the `fxsave` and `fxrstor` instructions to execute faster in 64-bit mode.
        const FAST_FXSAVE_FXRSTOR = 1 << 14;
        /// Changes how the `invlpg` instruction operates on TLB entries of upper-level entries.
        const TRANSLATION_CACHE_EXTENSION = 1 << 15;
    }
}

bitflags! {
    /// Flags stored in IA32_U_CET and IA32_S_CET (Table-2-2 in Intel SDM Volume
    /// 4). The Intel SDM-equivalent names are described in parentheses.
    #[repr(transparent)]
    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
    pub struct CetFlags: u64 {
        /// Enable shadow stack (SH_STK_EN)
        const SS_ENABLE = 1 << 0;
        /// Enable WRSS{D,Q}W instructions (WR_SHTK_EN)
        const SS_WRITE_ENABLE = 1 << 1;
        /// Enable indirect branch tracking (ENDBR_EN)
        const IBT_ENABLE = 1 << 2;
        /// Enable legacy treatment for indirect branch tracking (LEG_IW_EN)
        const IBT_LEGACY_ENABLE = 1 << 3;
        /// Enable no-track opcode prefix for indirect branch tracking (NO_TRACK_EN)
        const IBT_NO_TRACK_ENABLE = 1 << 4;
        /// Disable suppression of CET on legacy compatibility (SUPPRESS_DIS)
        const IBT_LEGACY_SUPPRESS_ENABLE = 1 << 5;
        /// Enable suppression of indirect branch tracking (SUPPRESS)
        const IBT_SUPPRESS_ENABLE = 1 << 10;
        /// Is IBT waiting for a branch to return? (read-only, TRACKER)
        const IBT_TRACKED = 1 << 11;
    }
}

bitflags! {
    /// Flags for the Advanced Programmable Interrupt Controller Base Register.
    #[repr(transparent)]
    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
    pub struct ApicBaseFlags: u64 {
        // bits 0 - 7 are reserved.
        /// Indicates whether the current processor is the bootstrap processor
        const BSP = 1 << 8;
        // bit 9 is reserved.
        /// Places the local APIC in the x2APIC mode. Processor support for x2APIC feature can be
        /// detected using the `cpuid` instruction. (CPUID.(EAX=1):ECX.21)
        const X2APIC_ENABLE = 1 << 10;
        /// Enables or disables the local Apic
        const LAPIC_ENABLE = 1 << 11;
    }
}

#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
mod x86_64 {
    use super::*;
    use crate::addr::VirtAddr;
    use crate::registers::rflags::RFlags;
    use crate::structures::gdt::SegmentSelector;
    use crate::structures::paging::Page;
    use crate::structures::paging::PhysFrame;
    use crate::structures::paging::Size4KiB;
    use crate::PhysAddr;
    use crate::PrivilegeLevel;
    use bit_field::BitField;
    use core::convert::TryInto;
    use core::fmt;
    // imports for intra doc links
    #[cfg(doc)]
    use crate::registers::{
        control::Cr4Flags,
        segmentation::{Segment, Segment64, CS, SS},
    };
    use core::arch::asm;

    impl Msr {
        /// Read 64 bits msr register.
        ///
        /// ## Safety
        ///
        /// The caller must ensure that this read operation has no unsafe side
        /// effects.
        #[inline]
        pub unsafe fn read(&self) -> u64 {
            let (high, low): (u32, u32);
            unsafe {
                asm!(
                    "rdmsr",
                    in("ecx") self.0,
                    out("eax") low, out("edx") high,
                    options(nomem, nostack, preserves_flags),
                );
            }
            ((high as u64) << 32) | (low as u64)
        }

        /// Write 64 bits to msr register.
        ///
        /// ## Safety
        ///
        /// The caller must ensure that this write operation has no unsafe side
        /// effects.
        #[inline]
        pub unsafe fn write(&mut self, value: u64) {
            let low = value as u32;
            let high = (value >> 32) as u32;

            unsafe {
                asm!(
                    "wrmsr",
                    in("ecx") self.0,
                    in("eax") low, in("edx") high,
                    options(nostack, preserves_flags),
                );
            }
        }
    }

    impl Efer {
        /// Read the current EFER flags.
        #[inline]
        pub fn read() -> EferFlags {
            EferFlags::from_bits_truncate(Self::read_raw())
        }

        /// Read the current raw EFER flags.
        #[inline]
        pub fn read_raw() -> u64 {
            unsafe { Self::MSR.read() }
        }

        /// Write the EFER flags, preserving reserved values.
        ///
        /// Preserves the value of reserved fields.
        ///
        /// ## Safety
        ///
        /// Unsafe because it's possible to break memory
        /// safety with wrong flags, e.g. by disabling long mode.
        #[inline]
        pub unsafe fn write(flags: EferFlags) {
            let old_value = Self::read_raw();
            let reserved = old_value & !(EferFlags::all().bits());
            let new_value = reserved | flags.bits();

            unsafe {
                Self::write_raw(new_value);
            }
        }

        /// Write the EFER flags.
        ///
        /// Does not preserve any bits, including reserved fields.
        ///
        /// ## Safety
        ///
        /// Unsafe because it's possible to
        /// break memory safety with wrong flags, e.g. by disabling long mode.
        #[inline]
        pub unsafe fn write_raw(flags: u64) {
            let mut msr = Self::MSR;
            unsafe {
                msr.write(flags);
            }
        }

        /// Update EFER flags.
        ///
        /// Preserves the value of reserved fields.
        ///
        /// ## Safety
        ///
        /// Unsafe because it's possible to break memory
        /// safety with wrong flags, e.g. by disabling long mode.
        #[inline]
        pub unsafe fn update<F>(f: F)
        where
            F: FnOnce(&mut EferFlags),
        {
            let mut flags = Self::read();
            f(&mut flags);
            unsafe {
                Self::write(flags);
            }
        }
    }

    impl FsBase {
        /// Read the current FsBase register.
        ///
        /// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is set, the more efficient
        /// [`FS::read_base`] can be used instead.
        #[inline]
        pub fn read() -> VirtAddr {
            VirtAddr::new(unsafe { Self::MSR.read() })
        }

        /// Write a given virtual address to the FS.Base register.
        ///
        /// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is set, the more efficient
        /// [`FS::write_base`] can be used instead.
        #[inline]
        pub fn write(address: VirtAddr) {
            let mut msr = Self::MSR;
            unsafe { msr.write(address.as_u64()) };
        }
    }

    impl GsBase {
        /// Read the current GsBase register.
        ///
        /// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is set, the more efficient
        /// [`GS::read_base`] can be used instead.
        #[inline]
        pub fn read() -> VirtAddr {
            VirtAddr::new(unsafe { Self::MSR.read() })
        }

        /// Write a given virtual address to the GS.Base register.
        ///
        /// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is set, the more efficient
        /// [`GS::write_base`] can be used instead.
        #[inline]
        pub fn write(address: VirtAddr) {
            let mut msr = Self::MSR;
            unsafe { msr.write(address.as_u64()) };
        }
    }

    impl KernelGsBase {
        /// Read the current KernelGsBase register.
        #[inline]
        pub fn read() -> VirtAddr {
            VirtAddr::new(unsafe { Self::MSR.read() })
        }

        /// Write a given virtual address to the KernelGsBase register.
        #[inline]
        pub fn write(address: VirtAddr) {
            let mut msr = Self::MSR;
            unsafe { msr.write(address.as_u64()) };
        }
    }

    impl Star {
        /// Read the Ring 0 and Ring 3 segment bases.
        /// The remaining fields are ignored because they are
        /// not valid for long mode.
        ///
        /// # Returns
        /// - Field 1 (SYSRET): The CS selector is set to this field + 16. SS.Sel is set to
        ///   this field + 8. Because SYSRET always returns to CPL 3, the
        ///   RPL bits 1:0 should be initialized to 11b.
        /// - Field 2 (SYSCALL): This field is copied directly into CS.Sel. SS.Sel is set to
        ///   this field + 8. Because SYSCALL always switches to CPL 0, the RPL bits
        ///   33:32 should be initialized to 00b.
        #[inline]
        pub fn read_raw() -> (u16, u16) {
            let msr_value = unsafe { Self::MSR.read() };
            let sysret = msr_value.get_bits(48..64);
            let syscall = msr_value.get_bits(32..48);
            (sysret.try_into().unwrap(), syscall.try_into().unwrap())
        }

        /// Read the Ring 0 and Ring 3 segment bases.
        /// Returns
        /// - CS Selector SYSRET
        /// - SS Selector SYSRET
        /// - CS Selector SYSCALL
        /// - SS Selector SYSCALL
        #[inline]
        pub fn read() -> (
            SegmentSelector,
            SegmentSelector,
            SegmentSelector,
            SegmentSelector,
        ) {
            let raw = Self::read_raw();
            (
                SegmentSelector(raw.0 + 16),
                SegmentSelector(raw.0 + 8),
                SegmentSelector(raw.1),
                SegmentSelector(raw.1 + 8),
            )
        }

        /// Write the Ring 0 and Ring 3 segment bases.
        /// The remaining fields are ignored because they are
        /// not valid for long mode.
        ///
        /// # Parameters
        /// - sysret: The CS selector is set to this field + 16. SS.Sel is set to
        ///   this field + 8. Because SYSRET always returns to CPL 3, the
        ///   RPL bits 1:0 should be initialized to 11b.
        /// - syscall: This field is copied directly into CS.Sel. SS.Sel is set to
        ///   this field + 8. Because SYSCALL always switches to CPL 0, the RPL bits
        ///   33:32 should be initialized to 00b.
        ///
        /// # Safety
        ///
        /// Unsafe because this can cause system instability if passed in the
        /// wrong values for the fields.
        #[inline]
        pub unsafe fn write_raw(sysret: u16, syscall: u16) {
            let mut msr_value = 0u64;
            msr_value.set_bits(48..64, sysret.into());
            msr_value.set_bits(32..48, syscall.into());
            let mut msr = Self::MSR;
            unsafe {
                msr.write(msr_value);
            }
        }

        /// Write the Ring 0 and Ring 3 segment bases.
        /// The remaining fields are ignored because they are
        /// not valid for long mode.
        /// This function will fail if the segment selectors are
        /// not in the correct offset of each other or if the
        /// segment selectors do not have correct privileges.
        #[inline]
        pub fn write(
            cs_sysret: SegmentSelector,
            ss_sysret: SegmentSelector,
            cs_syscall: SegmentSelector,
            ss_syscall: SegmentSelector,
        ) -> Result<(), InvalidStarSegmentSelectors> {
            // Convert to i32 to prevent underflows.
            let cs_sysret_cmp = i32::from(cs_sysret.0) - 16;
            let ss_sysret_cmp = i32::from(ss_sysret.0) - 8;
            let cs_syscall_cmp = i32::from(cs_syscall.0);
            let ss_syscall_cmp = i32::from(ss_syscall.0) - 8;

            if cs_sysret_cmp != ss_sysret_cmp {
                return Err(InvalidStarSegmentSelectors::SysretOffset);
            }

            if cs_syscall_cmp != ss_syscall_cmp {
                return Err(InvalidStarSegmentSelectors::SyscallOffset);
            }

            if ss_sysret.rpl() != PrivilegeLevel::Ring3 {
                return Err(InvalidStarSegmentSelectors::SysretPrivilegeLevel);
            }

            if ss_syscall.rpl() != PrivilegeLevel::Ring0 {
                return Err(InvalidStarSegmentSelectors::SyscallPrivilegeLevel);
            }

            unsafe { Self::write_raw(ss_sysret.0 - 8, cs_syscall.0) };

            Ok(())
        }
    }

    #[derive(Debug)]
    pub enum InvalidStarSegmentSelectors {
        SysretOffset,
        SyscallOffset,
        SysretPrivilegeLevel,
        SyscallPrivilegeLevel,
    }

    impl fmt::Display for InvalidStarSegmentSelectors {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::SysretOffset => write!(f, "Sysret CS and SS are not offset by 8."),
                Self::SyscallOffset => write!(f, "Syscall CS and SS are not offset by 8."),
                Self::SysretPrivilegeLevel => {
                    write!(f, "Sysret's segment must be a Ring3 segment.")
                }
                Self::SyscallPrivilegeLevel => {
                    write!(f, "Syscall's segment must be a Ring0 segment.")
                }
            }
        }
    }

    impl LStar {
        /// Read the current LStar register.
        /// This holds the target RIP of a syscall.
        #[inline]
        pub fn read() -> VirtAddr {
            VirtAddr::new(unsafe { Self::MSR.read() })
        }

        /// Write a given virtual address to the LStar register.
        /// This holds the target RIP of a syscall.
        #[inline]
        pub fn write(address: VirtAddr) {
            let mut msr = Self::MSR;
            unsafe { msr.write(address.as_u64()) };
        }
    }

    impl SFMask {
        /// Read to the SFMask register.
        /// The SFMASK register is used to specify which RFLAGS bits
        /// are cleared during a SYSCALL. In long mode, SFMASK is used
        /// to specify which RFLAGS bits are cleared when SYSCALL is
        /// executed. If a bit in SFMASK is set to 1, the corresponding
        /// bit in RFLAGS is cleared to 0. If a bit in SFMASK is cleared
        /// to 0, the corresponding rFLAGS bit is not modified.
        #[inline]
        pub fn read() -> RFlags {
            RFlags::from_bits(unsafe { Self::MSR.read() }).unwrap()
        }

        /// Write to the SFMask register.
        /// The SFMASK register is used to specify which RFLAGS bits
        /// are cleared during a SYSCALL. In long mode, SFMASK is used
        /// to specify which RFLAGS bits are cleared when SYSCALL is
        /// executed. If a bit in SFMASK is set to 1, the corresponding
        /// bit in RFLAGS is cleared to 0. If a bit in SFMASK is cleared
        /// to 0, the corresponding rFLAGS bit is not modified.
        #[inline]
        pub fn write(value: RFlags) {
            let mut msr = Self::MSR;
            unsafe { msr.write(value.bits()) };
        }

        /// Update the SFMask register.
        ///
        /// The SFMASK register is used to specify which RFLAGS bits
        /// are cleared during a SYSCALL. In long mode, SFMASK is used
        /// to specify which RFLAGS bits are cleared when SYSCALL is
        /// executed. If a bit in SFMASK is set to 1, the corresponding
        /// bit in RFLAGS is cleared to 0. If a bit in SFMASK is cleared
        /// to 0, the corresponding rFLAGS bit is not modified.
        #[inline]
        pub fn update<F>(f: F)
        where
            F: FnOnce(&mut RFlags),
        {
            let mut flags = Self::read();
            f(&mut flags);
            Self::write(flags);
        }
    }

    impl UCet {
        /// Read the raw IA32_U_CET.
        #[inline]
        fn read_raw() -> u64 {
            unsafe { Self::MSR.read() }
        }

        /// Write the raw IA32_U_CET.
        #[inline]
        fn write_raw(value: u64) {
            let mut msr = Self::MSR;
            unsafe {
                msr.write(value);
            }
        }

        /// Read IA32_U_CET. Returns a tuple of the flags and the address to the legacy code page bitmap.
        #[inline]
        pub fn read() -> (CetFlags, Page) {
            let value = Self::read_raw();
            let cet_flags = CetFlags::from_bits_truncate(value);
            let legacy_bitmap =
                Page::from_start_address(VirtAddr::new(value & !(Page::<Size4KiB>::SIZE - 1)))
                    .unwrap();

            (cet_flags, legacy_bitmap)
        }

        /// Write IA32_U_CET.
        #[inline]
        pub fn write(flags: CetFlags, legacy_bitmap: Page) {
            Self::write_raw(flags.bits() | legacy_bitmap.start_address().as_u64());
        }

        /// Updates IA32_U_CET.
        #[inline]
        pub fn update<F>(f: F)
        where
            F: FnOnce(&mut CetFlags, &mut Page),
        {
            let (mut flags, mut legacy_bitmap) = Self::read();
            f(&mut flags, &mut legacy_bitmap);
            Self::write(flags, legacy_bitmap);
        }
    }

    impl SCet {
        /// Read the raw IA32_S_CET.
        #[inline]
        fn read_raw() -> u64 {
            unsafe { Self::MSR.read() }
        }

        /// Write the raw IA32_S_CET.
        #[inline]
        fn write_raw(value: u64) {
            let mut msr = Self::MSR;
            unsafe {
                msr.write(value);
            }
        }

        /// Read IA32_S_CET. Returns a tuple of the flags and the address to the legacy code page bitmap.
        #[inline]
        pub fn read() -> (CetFlags, Page) {
            let value = Self::read_raw();
            let cet_flags = CetFlags::from_bits_truncate(value);
            let legacy_bitmap =
                Page::from_start_address(VirtAddr::new(value & !(Page::<Size4KiB>::SIZE - 1)))
                    .unwrap();

            (cet_flags, legacy_bitmap)
        }

        /// Write IA32_S_CET.
        #[inline]
        pub fn write(flags: CetFlags, legacy_bitmap: Page) {
            Self::write_raw(flags.bits() | legacy_bitmap.start_address().as_u64());
        }

        /// Updates IA32_S_CET.
        #[inline]
        pub fn update<F>(f: F)
        where
            F: FnOnce(&mut CetFlags, &mut Page),
        {
            let (mut flags, mut legacy_bitmap) = Self::read();
            f(&mut flags, &mut legacy_bitmap);
            Self::write(flags, legacy_bitmap);
        }
    }

    impl ApicBase {
        /// Reads the IA32_APIC_BASE MSR.
        #[inline]
        pub fn read() -> (PhysFrame, ApicBaseFlags) {
            let (frame, flags) = Self::read_raw();
            (frame, ApicBaseFlags::from_bits_truncate(flags))
        }

        /// Reads the raw IA32_APIC_BASE MSR.
        #[inline]
        pub fn read_raw() -> (PhysFrame, u64) {
            let raw = unsafe { Self::MSR.read() };
            // extract bits 12 - 51 (incl.)
            let addr = PhysAddr::new_truncate(raw);
            let frame = PhysFrame::containing_address(addr);
            (frame, raw)
        }

        /// Writes the IA32_APIC_BASE MSR preserving reserved values.
        ///
        /// Preserves the value of reserved fields.
        ///
        /// ## Safety
        ///
        /// Unsafe because changing the APIC base address allows hijacking a page of physical memory space in ways that would violate Rust's memory rules.
        #[inline]
        pub unsafe fn write(frame: PhysFrame, flags: ApicBaseFlags) {
            let (_, old_flags) = Self::read_raw();
            let reserved = old_flags & !(ApicBaseFlags::all().bits());
            let new_flags = reserved | flags.bits();

            unsafe {
                Self::write_raw(frame, new_flags);
            }
        }

        /// Writes the IA32_APIC_BASE MSR flags.
        ///
        /// Does not preserve any bits, including reserved fields.
        ///
        /// ## Safety
        ///
        /// Unsafe because it's possible to set reserved bits to `1` and changing the APIC base address allows hijacking a page of physical memory space in ways that would violate Rust's memory rules.
        #[inline]
        pub unsafe fn write_raw(frame: PhysFrame, flags: u64) {
            let addr = frame.start_address();
            let mut msr = Self::MSR;
            unsafe {
                msr.write(flags | addr.as_u64());
            }
        }
    }
}