Skip to content

Commit 42fd623

Browse files
authored
Merge pull request #42 from GabrielMajeri/core-arch
Use the core::arch intrinsics where possible
2 parents 81fb86a + 4495f13 commit 42fd623

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/controlregs.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Functions to read and write control registers.
22
//! See Intel Vol. 3a Section 2.5, especially Figure 2-7.
33
4+
use arch::{_xgetbv, _xsetbv};
5+
46
bitflags! {
57
pub struct Cr0: usize {
68
const CR0_ENABLE_PAGING = 1 << 31;
@@ -102,16 +104,11 @@ pub unsafe fn cr4_write(val: Cr4) {
102104
/// Read Extended Control Register XCR0.
103105
/// Only supported if CR4_ENABLE_OS_XSAVE is set.
104106
pub unsafe fn xcr0() -> Xcr0 {
105-
let high: u32;
106-
let low: u32;
107-
asm!("xgetbv" : "={eax}"(low), "={edx}"(high) : "{ecx}"(0));
108-
Xcr0::from_bits_truncate((high as u64) << 32 | low as u64)
107+
Xcr0::from_bits_truncate(_xgetbv(0))
109108
}
110109

111110
/// Write to Extended Control Register XCR0.
112111
/// Only supported if CR4_ENABLE_OS_XSAVE is set.
113112
pub unsafe fn xcr0_write(val: Xcr0) {
114-
let high: u32 = (val.bits >> 32) as u32;
115-
let low: u32 = val.bits as u32;
116-
asm!("xsetbv" :: "{eax}"(low), "{ecx}"(0), "{edx}"(high));
113+
_xsetbv(0, val.bits);
117114
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ extern crate phf;
1515
#[macro_use]
1616
extern crate std;
1717

18+
#[cfg(target_arch = "x86")]
19+
use core::arch::x86 as arch;
20+
#[cfg(target_arch = "x86_64")]
21+
use core::arch::x86_64 as arch;
22+
1823
macro_rules! check_flag {
1924
($doc:meta, $fun:ident, $flag:expr) => (
2025
#[$doc]

src/time.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Functions to read time stamp counters on x86.
22
3+
use core::mem;
4+
use arch::{_rdtsc, __rdtscp};
5+
36
/// Read the time stamp counter.
47
///
58
/// The RDTSC instruction is not a serializing instruction.
@@ -13,13 +16,8 @@
1316
/// # Safety
1417
/// * Causes a GP fault if the TSD flag in register CR4 is set and the CPL
1518
/// is greater than 0.
16-
#[allow(unused_mut)]
1719
pub unsafe fn rdtsc() -> u64 {
18-
let mut low: u32;
19-
let mut high: u32;
20-
21-
asm!("rdtsc" : "={eax}" (low), "={edx}" (high));
22-
((high as u64) << 32) | (low as u64)
20+
mem::transmute(_rdtsc())
2321
}
2422

2523
/// Read the time stamp counter.
@@ -35,11 +33,7 @@ pub unsafe fn rdtsc() -> u64 {
3533
/// # Safety
3634
/// * Causes a GP fault if the TSD flag in register CR4 is set and the
3735
/// CPL is greater than 0.
38-
#[allow(unused_mut)]
3936
pub unsafe fn rdtscp() -> u64 {
40-
let mut low: u32;
41-
let mut high: u32;
42-
43-
asm!("rdtscp" : "={eax}" (low), "={edx}" (high) ::: "volatile");
44-
((high as u64) << 32) | (low as u64)
37+
let mut _aux = 0;
38+
__rdtscp(&mut _aux)
4539
}

0 commit comments

Comments
 (0)