| 1 |
|
| 2 |
|
| 3 |
#include <sys/types.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <unistd.h> |
| 7 |
#include <string.h> |
| 8 |
#include <errno.h> |
| 9 |
|
| 10 |
#include "msrtool.h" |
| 11 |
|
| 12 |
static int msr_fd[MAX_CORES] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
| 13 |
|
| 14 |
int freebsd_probe(const struct sysdef *system) |
| 15 |
{ |
| 16 |
#ifdef __FreeBSD__ |
| 17 |
struct stat st; |
| 18 |
|
| 19 |
return stat("/dev/cpuctl0", &st) == 0; |
| 20 |
#else |
| 21 |
return 0; |
| 22 |
#endif |
| 23 |
} |
| 24 |
|
| 25 |
int freebsd_open(uint8_t cpu, enum SysModes mode) |
| 26 |
{ |
| 27 |
#ifdef __FreeBSD__ |
| 28 |
int flags; |
| 29 |
char devname[32]; |
| 30 |
|
| 31 |
if (cpu >= MAX_CORES) |
| 32 |
return 0; |
| 33 |
|
| 34 |
if (SYS_RDWR == mode) |
| 35 |
flags = O_RDWR; |
| 36 |
else if (SYS_WRONLY == mode) |
| 37 |
flags = O_WRONLY; |
| 38 |
else |
| 39 |
flags = O_RDONLY; |
| 40 |
|
| 41 |
snprintf(devname, sizeof(devname), "/dev/cpuctl%u", cpu); |
| 42 |
msr_fd[cpu] = open(devname, flags); |
| 43 |
if (msr_fd[cpu] < 0) { |
| 44 |
perror(devname); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
return 1; |
| 48 |
#else |
| 49 |
return 0; |
| 50 |
#endif |
| 51 |
} |
| 52 |
|
| 53 |
int freebsd_close(uint8_t cpu) |
| 54 |
{ |
| 55 |
if (cpu >= MAX_CORES) |
| 56 |
return 0; |
| 57 |
|
| 58 |
if (msr_fd[cpu] != -1) |
| 59 |
close(msr_fd[cpu]); |
| 60 |
msr_fd[cpu] = -1; |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val) |
| 65 |
{ |
| 66 |
#ifdef __FreeBSD__ |
| 67 |
cpuctl_msr_args_t args; |
| 68 |
|
| 69 |
if (cpu >= MAX_CORES) |
| 70 |
return 0; |
| 71 |
|
| 72 |
if (msr_fd[cpu] < 0) |
| 73 |
return 0; |
| 74 |
|
| 75 |
args.msr = addr; |
| 76 |
if (ioctl(msr_fd[cpu], CPUCTL_RDMSR, &args) < 0) { |
| 77 |
perror("ioctl(CPUCTL_RDMSR)"); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
val->hi = args.data >> 32; |
| 82 |
val->lo = args.data & 0xffffffff; |
| 83 |
return 1; |
| 84 |
#else |
| 85 |
return 0; |
| 86 |
#endif |
| 87 |
} |