-
Notifications
You must be signed in to change notification settings - Fork 20
/
cpuid.c
84 lines (69 loc) · 1.76 KB
/
cpuid.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
80
81
82
83
84
/*
* Licensed under the terms of the GNU GPL License version 2.
*
* Generic routines for retrieving cpuid registers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <x86info.h>
/* returns zero on success */
int native_cpuid(unsigned int cpunr, unsigned long long idx,
unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
unsigned int a = 0, b = 0, c = 0, d = 0;
c = idx >> 32;
bind_cpu(cpunr);
asm("cpuid"
: "=a" (a),
"=b" (b),
"+c" (c),
"=d" (d)
: "0" ((unsigned int)idx));
if (eax!=NULL)
*eax = a;
if (ebx!=NULL)
*ebx = b;
if (ecx!=NULL)
*ecx = c;
if (edx!=NULL)
*edx = d;
return 0;
}
void cpuid4(unsigned int CPU_number, unsigned long long idx,
unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
{
cpuid(CPU_number, 4 | (idx << 32), eax, ebx, ecx, edx);
}
/* Some CPUID calls want 'count' to be placed in ecx */
void cpuid_count(unsigned int CPU_number, unsigned int op, int count,
unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
*ecx = count;
cpuid(CPU_number, op, eax, ebx, ecx, edx);
}
unsigned int cpuid_ebx(unsigned int CPU_number, unsigned int op)
{
unsigned int eax, ebx, ecx, edx;
cpuid(CPU_number, op, &eax, &ebx, &ecx, &edx);
return ebx;
}
void dump_raw_cpuid(int cpunum, unsigned int begin, unsigned int end)
{
unsigned int i;
unsigned int eax, ebx, ecx, edx;
/* Dump all the CPUID results in raw hex */
for (i = begin; i <= end; i++) {
ecx = 0;
cpuid(cpunum, i, &eax, &ebx, &ecx, &edx);
printf("eax in: 0x%08x, eax = %08x ebx = %08x ecx = %08x edx = %08x\n", i, eax, ebx, ecx, edx);
}
printf("\n");
}