use core::arch::asm;
use crate::arch::_rdtsc;
pub unsafe fn rdtsc() -> u64 {
_rdtsc() as u64
}
pub unsafe fn rdtscp() -> (u64, u32) {
let eax: u32;
let ecx: u32;
let edx: u32;
asm!(
"rdtscp",
lateout("eax") eax,
lateout("ecx") ecx,
lateout("edx") edx,
options(nomem, nostack)
);
let counter: u64 = (edx as u64) << 32 | eax as u64;
(counter, ecx)
}
#[cfg(all(test, feature = "utest"))]
mod test {
use super::*;
#[test]
fn check_rdtsc() {
let cpuid = crate::cpuid::CpuId::new();
let has_tsc = cpuid
.get_feature_info()
.map_or(false, |finfo| finfo.has_tsc());
if has_tsc {
unsafe {
assert!(rdtsc() > 0, "rdtsc returned 0, unlikely!");
}
}
}
#[test]
fn check_rdtscp() {
let cpuid = crate::cpuid::CpuId::new();
let has_rdtscp = cpuid
.get_extended_processor_and_feature_identifiers()
.map_or(false, |einfo| einfo.has_rdtscp());
if has_rdtscp {
unsafe {
assert!(rdtscp().0 > 0, "rdtscp returned 0, unlikely!");
if cfg!(target_os = "linux") {
let mut cpu: u32 = 0;
let mut node: u32 = 0;
libc::syscall(libc::SYS_getcpu, &mut cpu, &mut node, 0);
assert_eq!(
rdtscp().1,
node << 12 | cpu,
"rdtscp AUX didn't match getcpu call!"
);
}
}
}
}
}