forked from dankamongmen/x86info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuid-freebsd.c
79 lines (69 loc) · 1.79 KB
/
cpuid-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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Licensed under the terms of the GNU GPL License version 2.
*
* FreeBSD Routines for retrieving cpuid registers.
* Originally submitted by Stanislav Sedov <stas@FreeBSD.org>
*/
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/cpuctl.h>
#include <sys/cpuset.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <x86info.h>
void bind_cpu(unsigned int cpunr)
{
cpuset_t mask;
CPU_ZERO(&mask);
CPU_SET(cpunr, &mask);
(void) cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
sizeof(mask), &mask);
}
static const char *NATIVE_CPUID_FAILED_MSG = "WARNING: Native cpuid failed\n";
void cpuid(unsigned int CPU_number, unsigned long long idx,
unsigned int *eax,
unsigned int *ebx,
unsigned int *ecx,
unsigned int *edx)
{
static int nodriver=0;
char cpuname[20];
int fh;
cpuctl_cpuid_args_t args;
if (nodriver == 1) {
if (native_cpuid(CPU_number, idx, eax,ebx,ecx,edx))
printf("%s", NATIVE_CPUID_FAILED_MSG);
return;
}
args.level = idx;
/* Ok, use the /dev/CPU interface in preference to the _up code. */
(void)snprintf(cpuname, sizeof(cpuname), "/dev/cpuctl%u", CPU_number);
fh = open(cpuname, O_RDONLY);
if (fh != -1) {
if (ioctl(fh, CPUCTL_CPUID, &args) != 0) {
perror(cpuname);
exit(EXIT_FAILURE);
}
if (eax!=0) *eax = args.data[0];
if (ebx!=0) *ebx = args.data[1];
if (ecx!=0) *ecx = args.data[2];
if (edx!=0) *edx = args.data[3];
if (close(fh) == -1) {
perror("close");
exit(EXIT_FAILURE);
}
} else {
/* Something went wrong, just do UP and hope for the best. */
nodriver = 1;
if (nrCPUs != 1)
perror(cpuname);
if (native_cpuid(CPU_number, idx, eax,ebx,ecx,edx))
printf("%s", NATIVE_CPUID_FAILED_MSG);
return;
}
}
#endif /* __FreeBSD__ */