diff --git a/src/cpucounter.c b/src/cpucounter.c index a77ee25..c62aa1b 100644 --- a/src/cpucounter.c +++ b/src/cpucounter.c @@ -1,8 +1,21 @@ #include +#if defined(__x86_64__) || defined(__amd64__) uint64_t cpucounter(void) { uint64_t low, high; - __asm__ __volatile__ ("rdtscp" : "=a" (low), "=d" (high) : : "%ecx"); + __asm__ __volatile__("rdtscp" + : "=a"(low), "=d"(high) + : + : "%ecx"); return (high << 32) | low; } +#elif defined(__aarch64__) +uint64_t cpucounter(void) +{ + uint64_t virtual_timer_value; + __asm__ __volatile__("mrs %0, cntvct_el0" + : "=r"(virtual_timer_value)); + return virtual_timer_value; +} +#endif diff --git a/src/cpucounter.rs b/src/cpucounter.rs index 5c9e41a..a8b5d06 100644 --- a/src/cpucounter.rs +++ b/src/cpucounter.rs @@ -6,12 +6,22 @@ pub(crate) struct CPUCounter; #[cfg(asm)] #[inline] +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] unsafe fn cpucounter() -> u64 { let (low, high): (u64, u64); - llvm_asm!("rdtscp" : "={eax}" (low), "={edx}" (high) : : "ecx"); + asm!("rdtscp", out("eax") low, out("edx") high, out("ecx") _); (high << 32) | low } +#[cfg(asm)] +#[inline] +#[cfg(any(target_arch = "aarch64"))] +unsafe fn cpucounter() -> u64 { + let (vtm): (u64); + asm!("mrs {}, cntvct_el0", out(reg) vtm); + vtm +} + #[cfg(not(asm))] extern "C" { fn cpucounter() -> u64;