aboutsummaryrefslogtreecommitdiff
path: root/src/shared/time.rs
diff options
context:
space:
mode:
authorGravatar Gerd Zellweger <mail@gerdzellweger.com> 2017-11-15 12:38:53 +0100
committerGravatar GitHub <noreply@github.com> 2017-11-15 12:38:53 +0100
commit1e2efb6b7a0d76d9b672e09862515f5ac2541d9f (patch)
tree1de68720694c91dd3aaf71286f53d0c995fcb746 /src/shared/time.rs
parent7afc050be9266413f9104e51a59e061b9a918bbc (diff)
parent7ee37f04538c7ded6f1ec4c8576a99fefa0d9fc3 (diff)
downloadrust-x86-1e2efb6b7a0d76d9b672e09862515f5ac2541d9f.tar.gz
rust-x86-1e2efb6b7a0d76d9b672e09862515f5ac2541d9f.tar.zst
rust-x86-1e2efb6b7a0d76d9b672e09862515f5ac2541d9f.zip
Merge pull request #37 from RWTH-OS/devel
move time.rs into the directory shared
Diffstat (limited to 'src/shared/time.rs')
-rw-r--r--src/shared/time.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/shared/time.rs b/src/shared/time.rs
new file mode 100644
index 0000000..eff567d
--- /dev/null
+++ b/src/shared/time.rs
@@ -0,0 +1,45 @@
+//! 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;
+ let mut high: u32;
+
+ 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)
+}