diff options
| author | Louis Burda <quent.burda@gmail.com> | 2022-08-13 16:44:04 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2022-08-13 16:45:08 +0200 |
| commit | 98babf91dcf166bc7644a3d70a90dac272f12a75 (patch) | |
| tree | 529acab95367ae6ed35eddf0731ee82f73c94842 /kmod/asm.h | |
| parent | 88d598bc2894c28d8dd9c2259c5c8cbe2ec7ce55 (diff) | |
| download | cachepc-98babf91dcf166bc7644a3d70a90dac272f12a75.tar.gz cachepc-98babf91dcf166bc7644a3d70a90dac272f12a75.zip | |
Reorder repo into module and tests
Diffstat (limited to 'kmod/asm.h')
| -rwxr-xr-x | kmod/asm.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/kmod/asm.h b/kmod/asm.h new file mode 100755 index 0000000..35f803b --- /dev/null +++ b/kmod/asm.h @@ -0,0 +1,85 @@ +#pragma once + +#include <linux/kernel.h> + +#define CPUID_AFFECTED_REGS "rax", "rbx", "rcx", "rdx" + +__attribute__((always_inline)) +static inline uint64_t cachepc_readpmc(uint64_t event); + +__attribute__((always_inline)) +static inline void cachepc_cpuid(void); + +__attribute__((always_inline)) +static inline void cachepc_lfence(void); + +__attribute__((always_inline)) +static inline void cachepc_sfence(void); + +__attribute__((always_inline)) +static inline void cachepc_mfence(void); + +__attribute__((always_inline)) +static inline void cachepc_readq(void *p); + +uint64_t +cachepc_readpmc(uint64_t event) +{ + uint32_t lo, hi; + + event = 0xC0010201 + 2 * event; + + asm volatile ( + "rdmsr" + : "=a" (lo), "=d" (hi) + : "c"(event) + ); + + return ((uint64_t) hi << 32) | (uint64_t) lo; +} + +void +cachepc_cpuid(void) +{ + asm volatile( + "mov $0x80000005, %%eax\n\t" + "cpuid\n\t" + ::: CPUID_AFFECTED_REGS + ); +} + +void +cachepc_lfence(void) +{ + asm volatile( + "lfence\n\t" + ::: "memory" + ); +} + +void +cachepc_sfence(void) +{ + asm volatile( + "sfence\n\t" + ::: "memory" + ); +} + +void +cachepc_mfence(void) +{ + asm volatile( + "mfence\n\t" + ::: "memory" + ); +} + +void +cachepc_readq(void *p) +{ + asm volatile ( + "movq (%0), %%r10\n\t" + : : "r" (p) : "r10" + ); +} |
