Skip to content

Commit

Permalink
Add support for Arm
Browse files Browse the repository at this point in the history
  • Loading branch information
jlb6740 committed Jul 26, 2020
1 parent 0aaa5ac commit 7030f5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cpucounter.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdint.h>

#if defined(__x86_64__) || defined(__amd64__)
uint64_t cpucounter(void)
{
uint64_t low, high;
Expand All @@ -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
10 changes: 10 additions & 0 deletions 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);
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

0 comments on commit 7030f5a

Please sign in to comment.