diff options
author | 2015-11-29 12:17:36 +0100 | |
---|---|---|
committer | 2015-11-29 12:17:36 +0100 | |
commit | 8ea2eb2dc442e9d0e89e08d40283b1aaba1cc87a (patch) | |
tree | eaa63e77f9386864c26fb87a9f50f4628611abc8 /src/time.rs | |
parent | bec7731473a0fae1d837a8a0fdfc902504ea9fcd (diff) | |
download | rust-x86-8ea2eb2dc442e9d0e89e08d40283b1aaba1cc87a.tar.gz rust-x86-8ea2eb2dc442e9d0e89e08d40283b1aaba1cc87a.tar.zst rust-x86-8ea2eb2dc442e9d0e89e08d40283b1aaba1cc87a.zip |
Fixing volatile and memory attributes for assembly.
For volatile:
You can prevent an asm instruction from being deleted by writing the
keyword volatile after the asm. [...] The volatile keyword indicates
that the instruction has important side-effects. GCC will not
delete a volatile asm if it is reachable.
and
An asm instruction without any output operands will be treated
identically to a volatile asm instruction.
And for memory:
This will cause GCC to not keep memory values cached in
registers across the assembler instruction and not optimize
stores or loads to that memory.
See also:
https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Extended-Asm.html
http://stackoverflow.com/questions/14449141/the-difference-between-asm-asm-volatile-and-clobbering-memory
Diffstat (limited to 'src/time.rs')
-rw-r--r-- | src/time.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/time.rs b/src/time.rs index ad8f466..7d402f4 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,5 +1,18 @@ +//! Functions to read time stamp counters on x86. /// Read the time stamp counter. +/// +/// The RDTSC instruction is not a serializing instruction. +/// It does not necessarily wait until all previous instructions +/// have been executed before reading the counter. Similarly, +/// subsequent instructions may begin execution before the +/// read operation is performed. If software requires RDTSC to be +/// executed only after all previous instructions have completed locally, +/// it can either use RDTSCP or execute the sequence LFENCE;RDTSC. +/// +/// # 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; @@ -8,3 +21,25 @@ pub unsafe fn rdtsc() -> u64 { asm!("rdtsc" : "={eax}" (low), "={edx}" (high)); ((high as u64) << 32) | (low as u64) } + +/// Read the time stamp counter. +/// +/// The RDTSCP instruction waits until all previous instructions +/// have been executed before reading the counter. +/// However, subsequent instructions may begin execution +/// before the read operation is performed. +/// +/// Volatile is used here because the function may be used to act as +/// an instruction barrier. +/// +/// # 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) +}
\ No newline at end of file |