forked from kernelslacker/x86info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdmsr-freebsd.c
58 lines (47 loc) · 971 Bytes
/
rdmsr-freebsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Licensed under the terms of the GNU GPL License version 2.
*
* FreeBSD Routines for reading MSRs.
*/
#if defined(__FreeBSD__)
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/cpuctl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <x86info.h>
int read_msr(int cpu, unsigned int idx, unsigned long long *val)
{
char cpuname[16];
int fh;
static int nodriver=0;
cpuctl_msr_args_t args;
if (nodriver==1)
return 0;
(void)snprintf(cpuname, sizeof(cpuname), "/dev/cpuctl%d", cpu);
fh = open(cpuname, O_RDONLY);
if (fh==-1) {
perror(cpuname);
nodriver=1;
return 0;
}
args.msr = idx;
if (ioctl(fh, CPUCTL_RDMSR, &args) != 0) {
if (close(fh) == -1) {
perror("close");
exit(EXIT_FAILURE);
}
return 0;
}
*val = args.data;
if (close(fh)==-1) {
perror("close");
exit(EXIT_FAILURE);
}
return 1;
}
#endif /* __FreeBSD__ */