Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsunyan committed Jan 2, 2019
1 parent 26e6189 commit 66cfb65
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
14 changes: 7 additions & 7 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,23 @@ config_t * load_config(config_t * old_config) {
if (!error) {
if (config->uv || config->tdp_apply || config->tjoffset_apply) {
if (config->fd_msr < 0) {
#ifdef __linux__
char * dev = "/dev/cpu/0/msr";
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#if IS_FREEBSD
char * dev = "/dev/cpuctl0";
#else
char * dev = "/dev/cpu/0/msr";
#endif
int fd = open(dev, O_RDWR | O_SYNC);
if (fd < 0) {
int pid = fork();
if (pid < 0) {
perror("Fork failed");
} else if (pid == 0) {
#ifdef __linux__
char * executable = "/sbin/modprobe";
execlp(executable, executable, "msr", NULL);
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#if IS_FREEBSD
char * executable = "/sbin/kldload";
execlp(executable, executable, "cpuctl", NULL);
#else
char * executable = "/sbin/modprobe";
execlp(executable, executable, "msr", NULL);
#endif
perror("Exec failed");
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdbool.h>

#define IS_FREEBSD defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)

#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

Expand Down
25 changes: 15 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "config.h"

#include <errno.h>
#include <math.h>
#include <setjmp.h>
Expand All @@ -7,19 +9,16 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"
#if IS_FREEBSD
#include <sys/cpuctl.h>
#include <sys/ioccom.h>
#endif

#define absf(x) ((x) < 0 ? -(x) : (x))

#ifdef __linux__
#define rd(c, a, t) (pread(c->fd_msr, &(t), 8, (a)) == 8)
#define wr(c, a, t) (pwrite(c->fd_msr, &(t), 8, (a)) == 8)
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#include <sys/cpuctl.h>
#include <sys/ioccom.h>
#if IS_FREEBSD

static inline bool cpuctl_rd(int fd, int a, uint64_t *t) {
static inline bool cpuctl_rd(int fd, int a, uint64_t * t) {
cpuctl_msr_args_t args;
args.msr = a;
if (ioctl(fd, CPUCTL_RDMSR, &args) == -1) {
Expand All @@ -29,7 +28,7 @@ static inline bool cpuctl_rd(int fd, int a, uint64_t *t) {
return true;
}

static inline bool cpuctl_wr(int fd, int a, uint64_t *t) {
static inline bool cpuctl_wr(int fd, int a, uint64_t * t) {
cpuctl_msr_args_t args;
args.msr = a;
args.data = *t;
Expand All @@ -38,6 +37,12 @@ static inline bool cpuctl_wr(int fd, int a, uint64_t *t) {

#define rd(c, a, t) (cpuctl_rd(c->fd_msr, (a), &(t)))
#define wr(c, a, t) (cpuctl_wr(c->fd_msr, (a), &(t)))

#else

#define rd(c, a, t) (pread(c->fd_msr, &(t), 8, (a)) == 8)
#define wr(c, a, t) (pwrite(c->fd_msr, &(t), 8, (a)) == 8)

#endif

static jmp_buf sigsegv_handler_jmp_buf;
Expand Down

0 comments on commit 66cfb65

Please sign in to comment.