File tree Expand file tree Collapse file tree 3 files changed +15
-19
lines changed Expand file tree Collapse file tree 3 files changed +15
-19
lines changed Original file line number Diff line number Diff line change 1
1
//! Functions to read and write control registers.
2
2
//! See Intel Vol. 3a Section 2.5, especially Figure 2-7.
3
3
4
+ use arch:: { _xgetbv, _xsetbv} ;
5
+
4
6
bitflags ! {
5
7
pub struct Cr0 : usize {
6
8
const CR0_ENABLE_PAGING = 1 << 31 ;
@@ -102,16 +104,11 @@ pub unsafe fn cr4_write(val: Cr4) {
102
104
/// Read Extended Control Register XCR0.
103
105
/// Only supported if CR4_ENABLE_OS_XSAVE is set.
104
106
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 ) )
109
108
}
110
109
111
110
/// Write to Extended Control Register XCR0.
112
111
/// Only supported if CR4_ENABLE_OS_XSAVE is set.
113
112
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 ) ;
117
114
}
Original file line number Diff line number Diff line change @@ -15,6 +15,11 @@ extern crate phf;
15
15
#[ macro_use]
16
16
extern crate std;
17
17
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
+
18
23
macro_rules! check_flag {
19
24
( $doc: meta, $fun: ident, $flag: expr) => (
20
25
#[ $doc]
Original file line number Diff line number Diff line change 1
1
//! Functions to read time stamp counters on x86.
2
2
3
+ use core:: mem;
4
+ use arch:: { _rdtsc, __rdtscp} ;
5
+
3
6
/// Read the time stamp counter.
4
7
///
5
8
/// The RDTSC instruction is not a serializing instruction.
13
16
/// # Safety
14
17
/// * Causes a GP fault if the TSD flag in register CR4 is set and the CPL
15
18
/// is greater than 0.
16
- #[ allow( unused_mut) ]
17
19
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 ( ) )
23
21
}
24
22
25
23
/// Read the time stamp counter.
@@ -35,11 +33,7 @@ pub unsafe fn rdtsc() -> u64 {
35
33
/// # Safety
36
34
/// * Causes a GP fault if the TSD flag in register CR4 is set and the
37
35
/// CPL is greater than 0.
38
- #[ allow( unused_mut) ]
39
36
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)
45
39
}
You can’t perform that action at this time.
0 commit comments