Skip to content

Commit 7dfb388

Browse files
committed
registers: Update documentation for control flags
Signed-off-by: Joe Richey <[email protected]>
1 parent 9974200 commit 7dfb388

File tree

2 files changed

+89
-51
lines changed

2 files changed

+89
-51
lines changed

src/registers/control.rs

+62-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Functions to read and write control registers.
22

33
pub use super::model_specific::{Efer, EferFlags};
4+
#[cfg(docsrs)]
5+
use crate::{registers::rflags::RFlags, structures::paging::PageTableFlags};
46

57
use bitflags::bitflags;
68

@@ -9,126 +11,148 @@ use bitflags::bitflags;
911
pub struct Cr0;
1012

1113
bitflags! {
12-
/// Configuration flags of the Cr0 register.
14+
/// Configuration flags of the [`Cr0`] register.
1315
pub struct Cr0Flags: u64 {
1416
/// Enables protected mode.
1517
const PROTECTED_MODE_ENABLE = 1;
1618
/// Enables monitoring of the coprocessor, typical for x87 instructions.
1719
///
18-
/// Controls together with the `TASK_SWITCHED` flag whether a `wait` or `fwait`
19-
/// instruction should cause a device-not-available exception.
20+
/// Controls (together with the [`TASK_SWITCHED`](Cr0Flags::TASK_SWITCHED)
21+
/// flag) whether a `wait` or `fwait` instruction should cause an `#NE` exception.
2022
const MONITOR_COPROCESSOR = 1 << 1;
21-
/// Force all x87 and MMX instructions to cause an exception.
23+
/// Force all x87 and MMX instructions to cause an `#NE` exception.
2224
const EMULATE_COPROCESSOR = 1 << 2;
2325
/// Automatically set to 1 on _hardware_ task switch.
2426
///
2527
/// This flags allows lazily saving x87/MMX/SSE instructions on hardware context switches.
2628
const TASK_SWITCHED = 1 << 3;
27-
/// Indicates support of 387DX math coprocessor instructions when set
29+
/// Indicates support of 387DX math coprocessor instructions.
30+
///
31+
/// Always set on all recent x86 processors, cannot be cleared.
2832
const EXTENSION_TYPE = 1 << 4;
29-
/// Enables the native error reporting mechanism for x87 FPU errors.
33+
/// Enables the native (internal) error reporting mechanism for x87 FPU errors.
3034
const NUMERIC_ERROR = 1 << 5;
3135
/// Controls whether supervisor-level writes to read-only pages are inhibited.
3236
///
3337
/// When set, it is not possible to write to read-only pages from ring 0.
3438
const WRITE_PROTECT = 1 << 16;
35-
/// Enables automatic alignment checking.
39+
/// Enables automatic usermode alignment checking if [`RFlags::ALIGNMENT_CHECK`] is also set.
3640
const ALIGNMENT_MASK = 1 << 18;
37-
/// Ignored. Used to control write-back/write-through cache strategy on older CPUs.
41+
/// Ignored, should always be unset.
42+
///
43+
/// Must be unset if [`CACHE_DISABLE`](Cr0Flags::CACHE_DISABLE) is unset.
44+
/// Older CPUs used this to control write-back/write-through cache strategy.
3845
const NOT_WRITE_THROUGH = 1 << 29;
39-
/// Disables internal caches (only for some cases).
46+
/// Disables some processor caches, specifics are model-dependent.
4047
const CACHE_DISABLE = 1 << 30;
41-
/// Enables page translation.
48+
/// Enables paging.
49+
///
50+
/// If this bit is set, [`PROTECTED_MODE_ENABLE`](Cr0Flags::PROTECTED_MODE_ENABLE) must be set.
4251
const PAGING = 1 << 31;
4352
}
4453
}
4554

4655
/// Contains the Page Fault Linear Address (PFLA).
4756
///
48-
/// When page fault occurs, the CPU sets this register to the accessed address.
57+
/// When a page fault occurs, the CPU sets this register to the faulting virtual address.
4958
#[derive(Debug)]
5059
pub struct Cr2;
5160

52-
/// Contains the physical address of the level 4 page table.
61+
/// Contains the physical address of the highest-level page table.
5362
#[derive(Debug)]
5463
pub struct Cr3;
5564

5665
bitflags! {
57-
/// Controls cache settings for the level 4 page table.
66+
/// Controls cache settings for the highest-level page table.
67+
///
68+
/// Unused if paging is disabled or if [`PCID`](Cr4Flags::PCID) is enabled.
5869
pub struct Cr3Flags: u64 {
59-
/// Use a writethrough cache policy for the P4 table (else a writeback policy is used).
70+
/// Use a writethrough cache policy for the table (otherwise a writeback policy is used).
6071
const PAGE_LEVEL_WRITETHROUGH = 1 << 3;
61-
/// Disable caching for the P4 table.
72+
/// Disable caching for the table.
6273
const PAGE_LEVEL_CACHE_DISABLE = 1 << 4;
6374
}
6475
}
6576

66-
/// Various control flags modifying the basic operation of the CPU while in protected mode.
67-
///
68-
/// Note: The documention for the individual fields is taken from the AMD64 and Intel x86_64
69-
/// manuals.
77+
/// Contains various control flags that enable architectural extensions, and
78+
/// indicate support for specific processor capabilities.
7079
#[derive(Debug)]
7180
pub struct Cr4;
7281

7382
bitflags! {
74-
/// Controls cache settings for the level 4 page table.
83+
/// Configuration flags of the [`Cr4`] register.
7584
pub struct Cr4Flags: u64 {
7685
/// Enables hardware-supported performance enhancements for software running in
7786
/// virtual-8086 mode.
7887
const VIRTUAL_8086_MODE_EXTENSIONS = 1;
7988
/// Enables support for protected-mode virtual interrupts.
8089
const PROTECTED_MODE_VIRTUAL_INTERRUPTS = 1 << 1;
81-
/// When set, only privilege-level 0 can execute the RDTSC or RDTSCP instructions.
90+
/// When set, only privilege-level 0 can execute the `RDTSC` or `RDTSCP` instructions.
8291
const TIMESTAMP_DISABLE = 1 << 2;
83-
/// Enables I/O breakpoint capability and enforces treatment of DR4 and DR5 registers
92+
/// Enables I/O breakpoint capability and enforces treatment of `DR4` and `DR5` registers
8493
/// as reserved.
8594
const DEBUGGING_EXTENSIONS = 1 << 3;
86-
/// Enables the use of 4MB physical frames; ignored in long mode.
95+
/// Enables the use of 4MB physical frames; ignored if
96+
/// [`PHYSICAL_ADDRESS_EXTENSION`](Cr4Flags::PHYSICAL_ADDRESS_EXTENSION)
97+
/// is set (so always ignored in long mode).
8798
const PAGE_SIZE_EXTENSION = 1 << 4;
88-
/// Enables physical address extension and 2MB physical frames; required in long mode.
99+
/// Enables physical address extensions and 2MB physical frames. Required in long mode.
89100
const PHYSICAL_ADDRESS_EXTENSION = 1 << 5;
90101
/// Enables the machine-check exception mechanism.
91102
const MACHINE_CHECK_EXCEPTION = 1 << 6;
92-
/// Enables the global-page mechanism, which allows to make page translations global
93-
/// to all processes.
103+
/// Enables the global page feature, allowing some page translations to
104+
/// be marked as global (see [`PageTableFlags::GLOBAL`]).
94105
const PAGE_GLOBAL = 1 << 7;
95-
/// Allows software running at any privilege level to use the RDPMC instruction.
106+
/// Allows software running at any privilege level to use the `RDPMC` instruction.
96107
const PERFORMANCE_MONITOR_COUNTER = 1 << 8;
97-
/// Enable the use of legacy SSE instructions; allows using FXSAVE/FXRSTOR for saving
108+
/// Enables the use of legacy SSE instructions; allows using `FXSAVE`/`FXRSTOR` for saving
98109
/// processor state of 128-bit media instructions.
99110
const OSFXSR = 1 << 9;
100-
/// Enables the SIMD floating-point exception (#XF) for handling unmasked 256-bit and
111+
/// Enables the SIMD floating-point exception (`#XF`) for handling unmasked 256-bit and
101112
/// 128-bit media floating-point errors.
102113
const OSXMMEXCPT_ENABLE = 1 << 10;
103-
/// Prevents the execution of the SGDT, SIDT, SLDT, SMSW, and STR instructions by
114+
/// Prevents the execution of the `SGDT`, `SIDT`, `SLDT`, `SMSW`, and `STR` instructions by
104115
/// user-mode software.
105116
const USER_MODE_INSTRUCTION_PREVENTION = 1 << 11;
106-
/// Enables 5-level paging on supported CPUs.
117+
/// Enables 5-level paging on supported CPUs (Intel Only).
107118
const L5_PAGING = 1 << 12;
108-
/// Enables VMX insturctions.
119+
/// Enables VMX instructions (Intel Only).
109120
const VIRTUAL_MACHINE_EXTENSIONS = 1 << 13;
110-
/// Enables SMX instructions.
121+
/// Enables SMX instructions (Intel Only).
111122
const SAFER_MODE_EXTENSIONS = 1 << 14;
112123
/// Enables software running in 64-bit mode at any privilege level to read and write
113124
/// the FS.base and GS.base hidden segment register state.
114125
const FSGSBASE = 1 << 16;
115126
/// Enables process-context identifiers (PCIDs).
116127
const PCID = 1 << 17;
117-
/// Enables extendet processor state management instructions, including XGETBV and XSAVE.
128+
/// Enables extended processor state management instructions, including `XGETBV` and `XSAVE`.
118129
const OSXSAVE = 1 << 18;
119-
/// When set, the `LOADIWKEY` instruction is available; additionally, if system firmware has activated the AES key locker instructions, register EBX of CPUID leaf 0x19, bit 0 (AESKLE) is set and the AES key locker instructions are enabled. See the [Intel Key Locker Specification](https://software.intel.com/content/www/us/en/develop/download/intel-key-locker-specification.html) for information on this feature.
130+
/// Enables the Key Locker feature (Intel Only).
131+
///
132+
/// This enables creation and use of opaque AES key handles; see the
133+
/// [Intel Key Locker Specification](https://software.intel.com/content/www/us/en/develop/download/intel-key-locker-specification.html)
134+
/// for more information.
120135
const KEY_LOCKER = 1 << 19;
121136
/// Prevents the execution of instructions that reside in pages accessible by user-mode
122137
/// software when the processor is in supervisor-mode.
123138
const SUPERVISOR_MODE_EXECUTION_PROTECTION = 1 << 20;
124139
/// Enables restrictions for supervisor-mode software when reading data from user-mode
125140
/// pages.
126141
const SUPERVISOR_MODE_ACCESS_PREVENTION = 1 << 21;
127-
/// Enables 4-level and 5-level paging to associate each linear address with a protection key in user mode.
142+
/// Enables protection keys for user-mode pages.
143+
///
144+
/// Also enables access to the PKRU register (via the `RDPKRU`/`WRPKRU`
145+
/// instructions) to set user-mode protection key access controls.
128146
const PROTECTION_KEY = 1 << 22;
129-
/// When set, enables intel control-flow enforcement technology. See chapter 18 of the Intel software developer manuals, volume 1, for more information.
147+
/// Enables Control-flow Enforcement Technology (CET)
148+
///
149+
/// This enables the shadow stack feature, ensuring return addresses read
150+
/// via `RET` and `IRET` have not been corrupted.
130151
const CONTROL_FLOW_ENFORCEMENT = 1 << 23;
131-
/// When set, allows 4-level and 5-level paging implementations to use the `IA32_PKRS` MSR to specify, for each protection key, whether supervisor-mode linear addresses with a particular protection key can be read or written.
152+
/// Enables protection keys for supervisor-mode pages (Intel Only).
153+
///
154+
/// Also enables the `IA32_PKRS` MSR to set supervisor-mode protection
155+
/// key access controls.
132156
const PROTECTION_KEY_SUPERVISOR = 1 << 24;
133157
}
134158
}

src/registers/xcontrol.rs

+27-13
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,43 @@ pub struct XCr0;
77

88
bitflags! {
99
/// Configuration flags of the XCr0 register.
10+
///
11+
/// For MPX, [`BNDREG`](XCr0Flags::BNDREG) and [`BNDCSR`](XCr0Flags::BNDCSR) must be set/unset simultaneously.
12+
/// For AVX-512, [`OPMASK`](XCr0Flags::OPMASK), [`ZMM_HI256`](XCr0Flags::ZMM_HI256), and [`HI16_ZMM`](XCr0Flags::HI16_ZMM) must be set/unset simultaneously.
1013
pub struct XCr0Flags: u64 {
11-
/// Enables x87 FPU
14+
/// Enables using the x87 FPU state
15+
/// with `XSAVE`/`XRSTOR`.
16+
///
17+
/// Must be set.
1218
const X87 = 1;
13-
/// Enables 128-bit (legacy) SSE
14-
/// Must be set to enable AVX and YMM
19+
/// Enables using MXCSR and the XMM registers
20+
/// with `XSAVE`/`XRSTOR`.
21+
///
22+
/// Must be set if [`YMM`](XCr0Flags::YMM) is set.
1523
const SSE = 1<<1;
16-
/// Enables 256-bit SSE
17-
/// Must be set to enable AVX
24+
/// Enables AVX instructions and using the upper halves of the YMM registers
25+
/// with `XSAVE`/`XRSTOR`.
1826
const YMM = 1<<2;
19-
/// When set, MPX instructions are enabled and the bound registers BND0-BND3 can be managed by XSAVE.
27+
/// Enables MPX instructions and using the BND0-BND3 bound registers
28+
/// with `XSAVE`/`XRSTOR` (Intel Only).
2029
const BNDREG = 1 << 3;
21-
/// When set, MPX instructions can be executed and XSAVE can manage the BNDCFGU and BNDSTATUS registers.
30+
/// Enables MPX instructions and using the BNDCFGU and BNDSTATUS registers
31+
/// with `XSAVE`/`XRSTOR` (Intel Only).
2232
const BNDCSR = 1 << 4;
23-
/// If set, AVX-512 instructions can be executed and XSAVE can manage the K0-K7 mask registers.
33+
/// Enables AVX-512 instructions and using the K0-K7 mask registers
34+
/// with `XSAVE`/`XRSTOR` (Intel Only).
2435
const OPMASK = 1 << 5;
25-
/// If set, AVX-512 instructions can be executed and XSAVE can be used to manage the upper halves of the lower ZMM registers.
36+
/// Enables AVX-512 instructions and using the upper halves of the lower ZMM registers
37+
/// with `XSAVE`/`XRSTOR` (Intel Only).
2638
const ZMM_HI256 = 1 << 6;
27-
/// If set, AVX-512 instructions can be executed and XSAVE can manage the upper ZMM registers.
39+
/// Enables AVX-512 instructions and using the upper ZMM registers
40+
/// with `XSAVE`/`XRSTOR` (Intel Only).
2841
const HI16_ZMM = 1 << 7;
29-
/// When set, PKRU state management is supported by
30-
/// XSAVE/XRSTOR
42+
/// Enables using the PKRU register
43+
/// with `XSAVE`/`XRSTOR`.
3144
const MPK = 1<<9;
32-
/// When set the Lightweight Profiling extensions are enabled
45+
/// Enables Lightweight Profiling extensions and managing LWP state
46+
/// with `XSAVE`/`XRSTOR` (AMD Only).
3347
const LWP = 1<<62;
3448
}
3549
}

0 commit comments

Comments
 (0)