Skip to content

Instantly share code, notes, and snippets.

@pankkor
Last active July 4, 2023 08:44
Show Gist options
  • Save pankkor/850dfd31706e3480bcc46fda9c08334a to your computer and use it in GitHub Desktop.
Save pankkor/850dfd31706e3480bcc46fda9c08334a to your computer and use it in GitHub Desktop.
#ifndef __aarch64__
#error Arches other than arm64 are not supported
#endif // #ifndef __aarch64__
#define FORCE_INLINE inline __attribute__((always_inline))
typedef unsigned long u64;
static FORCE_INLINE u64 rdtsc(void) {
u64 val;
// use isb to avoid speculative read of cntvct_el0
__asm__ volatile("isb;\n\tmrs %0, cntvct_el0" : "=r" (val) :: "memory");
return val;
}
static FORCE_INLINE u64 tsc_freq(void) {
u64 val;
__asm__ volatile("mrs %0, cntfrq_el0" : "=r" (val));
return val;
}
static FORCE_INLINE u64 tsc_to_ns(u64 ticks) {
return (ticks * 1000000000) / tsc_freq();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment