diff options
author | Andy Lutomirski <luto@amacapital.net> | 2014-09-28 23:05:46 -0700 |
---|---|---|
committer | Andy Lutomirski <luto@amacapital.net> | 2014-09-28 23:05:56 -0700 |
commit | 77e505073b4c5362eef23bb871db58f5d8e3b1dc (patch) | |
tree | 2ca67e496837cf3a89dde65f35085758c42c5e05 | |
parent | 1e13707b65ee0d5e763e5663757fa913cf535e1d (diff) | |
download | misc-tests-77e505073b4c5362eef23bb871db58f5d8e3b1dc.tar.gz |
Add dump_all_pmcs
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | dump_all_pmcs.c | 72 |
2 files changed, 73 insertions, 1 deletions
@@ -4,7 +4,7 @@ SIMPLE_C_TARGETS := dump-vsyscall context_switch_latency kernel_pf user_visible_ SIMPLE_CC_TARGETS := evil-clock-test -SPLIT_C_TARGETS := dump-vdso dump-vvar syscall_exit_regs sigreturn +SPLIT_C_TARGETS := dump-vdso dump-vvar syscall_exit_regs sigreturn test_mremap_vdso dump_all_pmcs SPLIT_CC_TARGETS := timing_test test_vsyscall test_vdso_parser ALL_TARGETS := $(SIMPLE_C_TARGETS) $(SIMPLE_CC_TARGETS) $(SPLIT_C_TARGETS:%=%_64) $(SPLIT_CC_TARGETS:%=%_64) $(SPLIT_C_TARGETS:%=%_32) $(SPLIT_CC_TARGETS:%=%_32) syscall32_from_64 diff --git a/dump_all_pmcs.c b/dump_all_pmcs.c new file mode 100644 index 0000000..be7a043 --- /dev/null +++ b/dump_all_pmcs.c @@ -0,0 +1,72 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <sys/prctl.h> +#include <asm/prctl.h> +#include <sys/ucontext.h> +#include <unistd.h> +#include <errno.h> +#include <signal.h> +#include <string.h> +#include <err.h> + +static volatile sig_atomic_t got_sigbus = 0; + +static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), + int flags) +{ + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = handler; + sa.sa_flags = SA_SIGINFO | flags; + sigemptyset(&sa.sa_mask); + if (sigaction(sig, &sa, 0)) + err(1, "sigaction"); +} + +#ifdef __x86_64__ +# define REG_IP REG_RIP +#else +# define REG_IP REG_EIP +#endif + +extern const char rdpmc_insn[]; + +static void sigsegv(int sig, siginfo_t *si, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t*)ctx_void; + + if (ctx->uc_mcontext.gregs[REG_IP] == (unsigned long)rdpmc_insn) { + got_sigbus = 1; + ctx->uc_mcontext.gregs[REG_IP] += 2; + } else { + raise(SIGABRT); + } +} + +__attribute__((noinline, noclone)) static void do_rdpmc(unsigned int ecx) +{ + unsigned int eax, edx; + + got_sigbus = 0; + + asm volatile ("rdpmc_insn: rdpmc" : "=a" (eax), "=d" (edx) : "c" (ecx)); + + if (got_sigbus) + return; + + printf("PMC 0x%08X: 0x%08X%08X\n", ecx, edx, eax); +} + +int main() +{ + sethandler(SIGSEGV, sigsegv, 0); + + for (unsigned int i = 0; i < 4096; i++) + do_rdpmc(i); + + for (unsigned int i = 0; i < 4096; i++) + do_rdpmc(i | (1<<30)); + + return 0; +} |