aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Gerd Zellweger <mail@gerdzellweger.com> 2017-11-14 11:33:47 +0100
committerGravatar GitHub <noreply@github.com> 2017-11-14 11:33:47 +0100
commit3be39d1c4346ec39af572b12784b21bd60bebf2c (patch)
tree24122135848ff723d2d39255dfbec2861ab1ed59 /src
parent2b6d77154f01e835d32d44c2e7f05c54b01f901a (diff)
parent4f1d9fce9ffae8d613e651322532faa989bb3f8e (diff)
downloadrust-x86-3be39d1c4346ec39af572b12784b21bd60bebf2c.tar.gz
rust-x86-3be39d1c4346ec39af572b12784b21bd60bebf2c.tar.zst
rust-x86-3be39d1c4346ec39af572b12784b21bd60bebf2c.zip
Merge pull request #36 from ColinFinck/cr4-xcr0
Add missing flags for CR4 and add functions to read and write the Extended Control Register XCR0
Diffstat (limited to 'src')
-rw-r--r--src/shared/control_regs.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/shared/control_regs.rs b/src/shared/control_regs.rs
index 306e52f..1a1f6a3 100644
--- a/src/shared/control_regs.rs
+++ b/src/shared/control_regs.rs
@@ -1,5 +1,5 @@
//! Functions to read and write control registers.
-//! See Intel Vol. 3a Section 2.5, especially Figure 2.6.
+//! See Intel Vol. 3a Section 2.5, especially Figure 2-7.
bitflags! {
pub flags Cr0: usize {
@@ -19,12 +19,15 @@ bitflags! {
bitflags! {
pub flags Cr4: usize {
+ const CR4_ENABLE_PROTECTION_KEY = 1 << 22,
const CR4_ENABLE_SMAP = 1 << 21,
const CR4_ENABLE_SMEP = 1 << 20,
const CR4_ENABLE_OS_XSAVE = 1 << 18,
const CR4_ENABLE_PCID = 1 << 17,
+ const CR4_ENABLE_FSGSBASE = 1 << 16,
const CR4_ENABLE_SMX = 1 << 14,
const CR4_ENABLE_VMX = 1 << 13,
+ const CR4_ENABLE_UMIP = 1 << 11,
const CR4_UNMASKED_SSE = 1 << 10,
const CR4_ENABLE_SSE = 1 << 9,
const CR4_ENABLE_PPMC = 1 << 8,
@@ -39,6 +42,20 @@ bitflags! {
}
}
+bitflags! {
+ pub flags Xcr0: u64 {
+ const XCR0_PKRU_STATE = 1 << 9,
+ const XCR0_HI16_ZMM_STATE = 1 << 7,
+ const XCR0_ZMM_HI256_STATE = 1 << 6,
+ const XCR0_OPMASK_STATE = 1 << 5,
+ const XCR0_BNDCSR_STATE = 1 << 4,
+ const XCR0_BNDREG_STATE = 1 << 3,
+ const XCR0_AVX_STATE = 1 << 2,
+ const XCR0_SSE_STATE = 1 << 1,
+ const XCR0_FPU_MMX_STATE = 1 << 0,
+ }
+}
+
/// Read cr0
pub unsafe fn cr0() -> Cr0 {
@@ -82,3 +99,20 @@ pub unsafe fn cr4() -> Cr4 {
pub unsafe fn cr4_write(val: Cr4) {
asm!("mov $0, %cr4" :: "r" (val.bits) : "memory");
}
+
+/// Read Extended Control Register XCR0.
+/// Only supported if CR4_ENABLE_OS_XSAVE is set.
+pub unsafe fn xcr0() -> Xcr0 {
+ let high: u32;
+ let low: u32;
+ asm!("xgetbv" : "={eax}"(low), "={edx}"(high) : "{ecx}"(0));
+ Xcr0::from_bits_truncate((high as u64) << 32 | low as u64)
+}
+
+/// Write to Extended Control Register XCR0.
+/// Only supported if CR4_ENABLE_OS_XSAVE is set.
+pub unsafe fn xcr0_write(val: Xcr0) {
+ let high: u32 = (val.bits >> 32) as u32;
+ let low: u32 = val.bits as u32;
+ asm!("xsetbv" :: "{eax}"(low), "{ecx}"(0), "{edx}"(high));
+}