Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cpucounter.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#include <stdint.h>

#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
12 changes: 11 additions & 1 deletion src/cpucounter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down