From 0aaa5ac087896901d4eb75f50e2f1d3d7d9e1dc5 Mon Sep 17 00:00:00 2001 From: Johnnie Birch <45402135+jlb6740@users.noreply.github.com> Date: Sat, 25 Jul 2020 19:44:21 -0700 Subject: [PATCH 1/2] Update inline assembly for x86 to support new syntax https://github.com/rust-lang/rfcs/blob/master/text/2843-llvm-asm.md https://github.com/rust-lang/rfcs/pull/2873 --- src/cpucounter.c | 5 ++++- src/cpucounter.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cpucounter.c b/src/cpucounter.c index a77ee25..bc052a3 100644 --- a/src/cpucounter.c +++ b/src/cpucounter.c @@ -3,6 +3,9 @@ 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; } diff --git a/src/cpucounter.rs b/src/cpucounter.rs index 5c9e41a..8517769 100644 --- a/src/cpucounter.rs +++ b/src/cpucounter.rs @@ -8,7 +8,7 @@ pub(crate) struct CPUCounter; #[inline] 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 } From 7030f5af0b616725b2c837be289b6a879735e02b Mon Sep 17 00:00:00 2001 From: Johnnie Birch <45402135+jlb6740@users.noreply.github.com> Date: Sat, 25 Jul 2020 19:59:26 -0700 Subject: [PATCH 2/2] Add support for Arm --- src/cpucounter.c | 10 ++++++++++ src/cpucounter.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/cpucounter.c b/src/cpucounter.c index bc052a3..c62aa1b 100644 --- a/src/cpucounter.c +++ b/src/cpucounter.c @@ -1,5 +1,6 @@ #include +#if defined(__x86_64__) || defined(__amd64__) uint64_t cpucounter(void) { uint64_t low, high; @@ -9,3 +10,12 @@ uint64_t cpucounter(void) : "%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 8517769..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); 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;