Skip to content

Use the core::arch intrinsics where possible #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/controlregs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Functions to read and write control registers.
//! See Intel Vol. 3a Section 2.5, especially Figure 2-7.

use arch::{_xgetbv, _xsetbv};

bitflags! {
pub struct Cr0: usize {
const CR0_ENABLE_PAGING = 1 << 31;
Expand Down Expand Up @@ -102,16 +104,11 @@ pub unsafe fn cr4_write(val: Cr4) {
/// 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)
Xcr0::from_bits_truncate(_xgetbv(0))
}

/// 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));
_xsetbv(0, val.bits);
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ extern crate phf;
#[macro_use]
extern crate std;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;

macro_rules! check_flag {
($doc:meta, $fun:ident, $flag:expr) => (
#[$doc]
Expand Down
18 changes: 6 additions & 12 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Functions to read time stamp counters on x86.

use core::mem;
use arch::{_rdtsc, __rdtscp};

/// Read the time stamp counter.
///
/// The RDTSC instruction is not a serializing instruction.
Expand All @@ -13,13 +16,8 @@
/// # Safety
/// * Causes a GP fault if the TSD flag in register CR4 is set and the CPL
/// is greater than 0.
#[allow(unused_mut)]
pub unsafe fn rdtsc() -> u64 {
let mut low: u32;
let mut high: u32;

asm!("rdtsc" : "={eax}" (low), "={edx}" (high));
((high as u64) << 32) | (low as u64)
mem::transmute(_rdtsc())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it much better to use as u64 for this sort of thing?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is... It's fixed on master now thanks for raising this concern.

}

/// Read the time stamp counter.
Expand All @@ -35,11 +33,7 @@ pub unsafe fn rdtsc() -> u64 {
/// # Safety
/// * Causes a GP fault if the TSD flag in register CR4 is set and the
/// CPL is greater than 0.
#[allow(unused_mut)]
pub unsafe fn rdtscp() -> u64 {
let mut low: u32;
let mut high: u32;

asm!("rdtscp" : "={eax}" (low), "={edx}" (high) ::: "volatile");
((high as u64) << 32) | (low as u64)
let mut _aux = 0;
__rdtscp(&mut _aux)
}