-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
163 lines (141 loc) · 4.49 KB
/
cpu.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ccan/compiler/compiler.h>
#include <ukernel/util.h>
#include <ukernel/x86.h>
#include <ukernel/cpu.h>
struct feature_bit {
int bit;
const char *name;
};
bool use_sysenter = false;
static struct x86_features features;
static COLD void print_features(uint32_t mask, const struct feature_bit *fs)
{
bool first = true;
for(int i=0; fs[i].name != NULL; i++) {
if(CHECK_FLAG(mask, 1 << fs[i].bit)) {
printf("%s%s", first ? "" : " ", fs[i].name);
first = false;
}
}
}
/* x86 CPUID parsing (only for features on the first page, and those things
* that're used by the kernel)
*/
COLD void scan_cpuid(void)
{
memset(&features, 0, sizeof(features));
struct cpuid_out id;
char cpuname[13];
x86_cpuid(&id, 0, 0, 0, 0);
memcpy(&cpuname[0], &id.ebx, sizeof(uint32_t));
memcpy(&cpuname[4], &id.edx, sizeof(uint32_t));
memcpy(&cpuname[8], &id.ecx, sizeof(uint32_t));
cpuname[12] = '\0';
int max_page = id.eax;
printf("CPU is a `%s' (with %d info pages)\n", cpuname, max_page);
if(1 <= max_page) {
/* intel page 01h */
printf("page 01h:\n");
x86_cpuid(&id, 1, 0, 0, 0);
features.ecx = id.ecx;
features.edx = id.edx;
int stepping = id.eax & 0xf, model = (id.eax >> 4) & 0xf,
family_id = (id.eax >> 8) & 0xf, proc_type = (id.eax >> 12) & 0x3,
ext_model = (id.eax >> 16) & 0xf, ext_family = (id.eax >> 20) & 0xff;
if(family_id == 0x6 || family_id == 0xf) model |= ext_model << 4;
if(family_id == 0xf) family_id += ext_family;
printf(" type %d, model %d, family %d, stepping %d\n",
proc_type, model, family_id, stepping);
features.type = proc_type;
features.family = family_id;
features.model = ext_model;
features.stepping = stepping;
int brand_ix = id.ebx & 0xff, clflush_sz = ((id.ebx >> 8) & 0xff) << 3,
max_local_cpus = (id.ebx >> 16) & 0xff,
init_apic_id = (id.ebx >> 24) & 0xff;
printf(" brand_ix %d, clflush_sz %d, max_local_cpus %d, apic_id %d\n",
brand_ix, clflush_sz, max_local_cpus, init_apic_id);
static const struct feature_bit p01_ecx[] = {
{ 0, "sse3" }, { 1, "pclmulqdq" }, { 2, "dtes64" }, { 3, "monitor" },
{ 4, "dscpl" }, { 5, "vmx" }, { 6, "smx" }, { 7, "eist" },
{ 8, "tm2" }, { 9, "ssse3" }, { 10, "cnxtid" }, { 12, "fma" },
{ 13, "cmpxchg16b" }, { 14, "xtpruc" }, { 15, "pdcm" }, { 17, "pcid" },
{ 18, "dca" }, { 19, "sse41" }, { 20, "sse42" }, { 21, "x2apic" },
{ 22, "movbe" }, { 23, "popcnt" }, { 24, "tscdl" }, { 25, "aesni" },
{ 26, "xsave" }, { 27, "osxsave" }, { 28, "avx" }, { 29, "f16c" },
{ 30, "rdrand" },
{ .name = NULL },
}, p01_edx[] = {
{ 0, "fpu" }, { 1, "vme" }, { 2, "de" }, { 3, "pse" },
{ 4, "tsc" }, { 5, "msr" }, { 6, "pae" }, { 7, "mce" },
{ 8, "cx8" }, { 9, "apic" }, { 11, "sysenter" }, { 12, "mtrr" },
{ 13, "pge" }, { 14, "mca" }, { 15, "cmov" }, { 16, "pat" },
{ 17, "pse36" }, { 18, "psn" }, { 19, "clflush" }, { 21, "ds" },
{ 22, "acpi" }, { 23, "mmx" }, { 24, "fxsr" }, { 25, "sse" },
{ 26, "sse2" }, { 27, "ss" }, { 28, "htt" }, { 29, "tm" },
{ 31, "pbe" },
{ .name = NULL }
};
printf(" features: ");
print_features(id.edx, p01_edx); /* trad first. */
if(p01_ecx != 0) {
printf(" ");
print_features(id.ecx, p01_ecx);
}
printf("\n");
}
if(2 <= max_page) {
/* intel page 02h */
printf("page 02h:\n");
x86_cpuid(&id, 2, 0, 0, 0);
/* (the first word is undefined when b31 is set.) */
int id_iters = CHECK_FLAG(id.eax, 0x80000000u) ? 1 : (id.eax & 0xff),
iter_id = 0;
do {
uint32_t descs[3] = { id.ebx, id.ecx, id.edx };
for(int i=0; i < 3; i++) {
int d = descs[i] & 0xff;
if(CHECK_FLAG(descs[i], 0x80000000u) || d == 0) continue;
switch(d) {
case 0x7d:
printf(" L2d: 2M, 64B line, 8-way set assoc\n");
break;
default:
printf(" descriptor %#02x\n", descs[i] & 0xff);
}
}
x86_cpuid(&id, 2, 0, 0, 0);
} while(++iter_id < id_iters);
}
#ifdef CONFIG_X86_SYSENTER
use_sysenter = cpu_has_sysenter();
#endif
}
/* TODO: for smp, get this from the per-CPU spot & inline the call. */
const cpu_features *get_features(void) {
return &features;
}
bool cpu_has_sysenter(void)
{
if(!CHECK_FLAG(features.edx, X86_FEATURE_D_SEP)
|| (features.family == 6 && features.model < 3
&& features.stepping < 3))
{
return false;
} else {
return true;
}
}
SYSCALL L4_Word_t sys_processorcontrol(
L4_Word_t proc_no,
L4_Word_t internal_freq,
L4_Word_t external_freq,
L4_Word_t voltage)
{
printf("%s: called\n", __func__);
return 1;
}