x86/
time.rs

1//! Functions to read time stamp counters on x86.
2use core::arch::asm;
3
4use crate::arch::_rdtsc;
5
6/// Read the time stamp counter.
7///
8/// The RDTSC instruction is not a serializing instruction.
9/// It does not necessarily wait until all previous instructions
10/// have been executed before reading the counter. Similarly,
11/// subsequent instructions may begin execution before the
12/// read operation is performed. If software requires RDTSC to be
13/// executed only after all previous instructions have completed locally,
14/// it can either use RDTSCP or execute the sequence LFENCE;RDTSC.
15///
16/// # Safety
17/// * Causes a GP fault if the TSD flag in register CR4 is set and the CPL
18///   is greater than 0.
19pub unsafe fn rdtsc() -> u64 {
20    _rdtsc() as u64
21}
22
23/// Read the time stamp counter.
24///
25/// The RDTSCP instruction waits until all previous instructions have been
26/// executed before reading the counter. However, subsequent instructions may
27/// begin execution before the read operation is performed.
28///
29/// Volatile is used here because the function may be used to act as an
30/// instruction barrier.
31///
32/// # Returns
33/// - The current time stamp counter value of the CPU as a `u64`.
34/// - The contents of `IA32_TSC_AUX` on that particular core. This is an OS
35///   defined value. For example, Linux writes `numa_id << 12 | core_id` into
36///   it. See also [`crate::rdpid`].
37///
38/// # Note
39/// One can use `core::arch::x86_64::__rdtscp` from the Rust core library as
40/// well. We don't rely on it because it only returns the time-stamp counter of
41/// rdtscp and not the contents of `IA32_TSC_AUX`.
42///
43/// # Safety
44/// * Causes a GP fault if the TSD flag in register CR4 is set and the CPL is
45///   greater than 0.
46pub unsafe fn rdtscp() -> (u64, u32) {
47    let eax: u32;
48    let ecx: u32;
49    let edx: u32;
50    asm!(
51      "rdtscp",
52      lateout("eax") eax,
53      lateout("ecx") ecx,
54      lateout("edx") edx,
55      options(nomem, nostack)
56    );
57
58    let counter: u64 = (edx as u64) << 32 | eax as u64;
59    (counter, ecx)
60}
61
62#[cfg(all(test, feature = "utest"))]
63mod test {
64    use super::*;
65
66    #[test]
67    fn check_rdtsc() {
68        let cpuid = crate::cpuid::CpuId::new();
69        let has_tsc = cpuid
70            .get_feature_info()
71            .map_or(false, |finfo| finfo.has_tsc());
72
73        if has_tsc {
74            unsafe {
75                assert!(rdtsc() > 0, "rdtsc returned 0, unlikely!");
76            }
77        }
78    }
79
80    #[test]
81    fn check_rdtscp() {
82        let cpuid = crate::cpuid::CpuId::new();
83        let has_rdtscp = cpuid
84            .get_extended_processor_and_feature_identifiers()
85            .map_or(false, |einfo| einfo.has_rdtscp());
86
87        if has_rdtscp {
88            unsafe {
89                // Check cycle counter:
90                assert!(rdtscp().0 > 0, "rdtscp returned 0, unlikely!");
91
92                // Check TSC AUX is correct (currently when using Linux only):
93                // See also: https://elixir.bootlin.com/linux/v5.18.8/source/arch/x86/include/asm/segment.h#L241
94                if cfg!(target_os = "linux") {
95                    let mut cpu: u32 = 0;
96                    let mut node: u32 = 0;
97                    libc::syscall(libc::SYS_getcpu, &mut cpu, &mut node, 0);
98                    assert_eq!(
99                        rdtscp().1,
100                        node << 12 | cpu,
101                        "rdtscp AUX didn't match getcpu call!"
102                    );
103                }
104            }
105        }
106    }
107}