2019-10-09 01:35:47 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
2020-05-07 03:57:01 +02:00
|
|
|
#include "umka.h"
|
2019-10-09 01:35:47 +02:00
|
|
|
|
2020-02-21 03:21:09 +01:00
|
|
|
#define MSR_IA32_DEBUGCTLMSR 0x1d9
|
|
|
|
#define MSR_IA32_LASTBRANCHFROMIP 0x1db
|
|
|
|
#define MSR_IA32_LASTBRANCHTOIP 0x1dc
|
|
|
|
|
2019-10-09 01:35:47 +02:00
|
|
|
int covfd, msrfd;
|
|
|
|
|
|
|
|
uint64_t rdmsr(uint32_t reg)
|
|
|
|
{
|
|
|
|
uint64_t data;
|
|
|
|
|
|
|
|
if (pread(msrfd, &data, sizeof data, reg) != sizeof data) {
|
|
|
|
perror("rdmsr: pread");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wrmsr(uint32_t reg, uint64_t data)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/cpu/0/msr", O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("wrmsr: open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwrite(fd, &data, sizeof data, reg) != sizeof data) {
|
|
|
|
perror("wrmsr: pwrite");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_sigtrap() {
|
2020-02-21 03:21:09 +01:00
|
|
|
uint64_t from = rdmsr(MSR_IA32_LASTBRANCHFROMIP);
|
|
|
|
uint64_t to = rdmsr(MSR_IA32_LASTBRANCHTOIP);
|
2019-10-09 01:35:47 +02:00
|
|
|
|
|
|
|
if ((from >= (uintptr_t)coverage_begin && from < (uintptr_t)coverage_end) ||
|
|
|
|
(to >= (uintptr_t)coverage_begin && to < (uintptr_t)coverage_end)) {
|
2020-01-29 11:53:13 +01:00
|
|
|
write(covfd, &from, 4);
|
|
|
|
write(covfd, &to, 4);
|
2019-10-09 01:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 03:21:09 +01:00
|
|
|
wrmsr(MSR_IA32_DEBUGCTLMSR, 3);
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:58:16 +01:00
|
|
|
uint32_t set_eflags_tf(uint32_t tf);
|
2019-10-09 01:35:47 +02:00
|
|
|
|
|
|
|
void trace_lbr_begin() {
|
|
|
|
struct sigaction action;
|
|
|
|
action.sa_sigaction = &handle_sigtrap;
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction(SIGTRAP, &action, NULL);
|
|
|
|
|
2020-02-21 03:21:09 +01:00
|
|
|
wrmsr(MSR_IA32_DEBUGCTLMSR, 3);
|
2019-10-09 01:35:47 +02:00
|
|
|
msrfd = open("/dev/cpu/0/msr", O_RDONLY);
|
|
|
|
if (msrfd < 0) {
|
|
|
|
perror("rdmsr: open");
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-01-29 11:53:13 +01:00
|
|
|
char coverage_filename[32];
|
|
|
|
sprintf(coverage_filename, "coverage.%i", getpid());
|
2020-09-17 18:44:57 +02:00
|
|
|
covfd = open(coverage_filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_IWOTH);
|
|
|
|
void *coverage_begin_addr = coverage_begin;
|
|
|
|
void *coverage_end_addr = coverage_end;
|
2020-02-21 03:21:09 +01:00
|
|
|
write(covfd, &coverage_begin_addr, 4);
|
|
|
|
write(covfd, &coverage_end_addr, 4);
|
2019-10-09 01:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void trace_lbr_end() {
|
2020-02-21 03:21:09 +01:00
|
|
|
wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
|
2019-10-09 01:35:47 +02:00
|
|
|
close(msrfd);
|
|
|
|
close(covfd);
|
|
|
|
}
|
2020-03-09 22:03:57 +01:00
|
|
|
|
|
|
|
uint32_t trace_lbr_pause(void) {
|
|
|
|
return set_eflags_tf(0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace_lbr_resume(uint32_t value) {
|
|
|
|
set_eflags_tf(value);
|
|
|
|
}
|