-
Notifications
You must be signed in to change notification settings - Fork 19
/
x86.c
466 lines (411 loc) · 11.6 KB
/
x86.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*-
* Copyright (c) 2011 NetApp, Inc.
* Copyright (c) 2015 xhyve developers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdint.h>
#include <strings.h>
#include <xhyve/support/misc.h>
#include <xhyve/support/atomic.h>
#include <xhyve/support/specialreg.h>
#include <xhyve/vmm/vmm.h>
#include <xhyve/vmm/vmm_host.h>
#include <xhyve/vmm/vmm_ktr.h>
#include <xhyve/vmm/x86.h>
#define CPUID_VM_HIGH 0x40000000
static const char bhyve_id[12] = "bhyve bhyve ";
static volatile u_long bhyve_xcpuids;
/*
* The default CPU topology is a single thread per package.
*/
static u_int threads_per_core = 1;
static u_int cores_per_package = 1;
static int cpuid_leaf_b = 1;
/*
* Round up to the next power of two, if necessary, and then take log2.
* Returns -1 if argument is zero.
*/
static __inline int
log2(u_int x)
{
return (fls((int) (x << (1 - powerof2(x)))) - 1);
}
int
x86_emulate_cpuid(struct vm *vm, int vcpu_id,
uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
{
const struct xsave_limits *limits;
uint64_t cr4;
int error, level, width, x2apic_id;
unsigned int func, regs[4], logical_cpus;
u_int cpu_feature, amd_feature, amd_feature2, cpu_high, cpu_exthigh;
u_int tsc_is_invariant, smp_tsc;
enum x2apic_state x2apic_state;
VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", *eax, *ecx);
tsc_is_invariant = 1;
smp_tsc = 1;
do_cpuid(0, regs);
cpu_high = regs[0];
do_cpuid(1, regs);
cpu_feature = regs[3];
do_cpuid(0x80000000, regs);
cpu_exthigh = regs[0];
do_cpuid(0x80000001, regs);
amd_feature = regs[3] & ~(cpu_feature & 0x0183f3ff);
amd_feature2 = regs[2];
/*
* Requests for invalid CPUID levels should map to the highest
* available level instead.
*/
if (cpu_exthigh != 0 && *eax >= 0x80000000) {
if (*eax > cpu_exthigh)
*eax = cpu_exthigh;
} else if (*eax >= 0x40000000) {
if (*eax > CPUID_VM_HIGH)
*eax = CPUID_VM_HIGH;
} else if (*eax > cpu_high) {
*eax = cpu_high;
}
func = *eax;
/*
* In general the approach used for CPU topology is to
* advertise a flat topology where all CPUs are packages with
* no multi-core or SMT.
*/
switch (func) {
/*
* Pass these through to the guest
*/
case CPUID_0000_0000:
case CPUID_0000_0002:
case CPUID_0000_0003:
case CPUID_8000_0000:
case CPUID_8000_0002:
case CPUID_8000_0003:
case CPUID_8000_0004:
case CPUID_8000_0006:
cpuid_count(*eax, *ecx, regs);
break;
case CPUID_8000_0008:
cpuid_count(*eax, *ecx, regs);
break;
case CPUID_8000_0001:
cpuid_count(*eax, *ecx, regs);
/*
* Hide SVM and Topology Extension features from guest.
*/
regs[2] &= ~((unsigned) (AMDID2_SVM | AMDID2_TOPOLOGY));
/*
* Don't advertise extended performance counter MSRs
* to the guest.
*/
regs[2] &= ~((unsigned) AMDID2_PCXC);
regs[2] &= ~((unsigned) AMDID2_PNXC);
regs[2] &= ~((unsigned) AMDID2_PTSCEL2I);
/*
* Don't advertise Instruction Based Sampling feature.
*/
regs[2] &= ~((unsigned) AMDID2_IBS);
/* NodeID MSR not available */
regs[2] &= ~((unsigned) AMDID2_NODE_ID);
/* Don't advertise the OS visible workaround feature */
regs[2] &= ~((unsigned) AMDID2_OSVW);
/*
* Hide rdtscp/ia32_tsc_aux until we know how
* to deal with them.
*/
regs[3] &= ~((unsigned) AMDID_RDTSCP);
break;
case CPUID_8000_0007:
/*
* AMD uses this leaf to advertise the processor's
* power monitoring and RAS capabilities. These
* features are hardware-specific and exposing
* them to a guest doesn't make a lot of sense.
*
* Intel uses this leaf only to advertise the
* "Invariant TSC" feature with all other bits
* being reserved (set to zero).
*/
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
/*
* "Invariant TSC" can be advertised to the guest if:
* - host TSC frequency is invariant
* - host TSCs are synchronized across physical cpus
*
* XXX This still falls short because the vcpu
* can observe the TSC moving backwards as it
* migrates across physical cpus. But at least
* it should discourage the guest from using the
* TSC to keep track of time.
*/
if (tsc_is_invariant && smp_tsc)
regs[3] |= AMDPM_TSC_INVARIANT;
break;
case CPUID_0000_0001:
do_cpuid(1, regs);
error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state);
if (error) {
xhyve_abort("x86_emulate_cpuid: error %d "
"fetching x2apic state\n", error);
}
/*
* Override the APIC ID only in ebx
*/
regs[1] &= ~((unsigned) CPUID_LOCAL_APIC_ID);
regs[1] |= (((unsigned) vcpu_id) << CPUID_0000_0001_APICID_SHIFT);
/*
* Don't expose VMX, SpeedStep, TME or SMX capability.
* Advertise x2APIC capability and Hypervisor guest.
*/
regs[2] &= ~((unsigned) (CPUID2_VMX | CPUID2_EST | CPUID2_TM2));
regs[2] &= ~((unsigned) CPUID2_SMX);
regs[2] |= (unsigned) CPUID2_HV;
if (x2apic_state != ((unsigned) X2APIC_DISABLED))
regs[2] |= ((unsigned) CPUID2_X2APIC);
else
regs[2] &= ~((unsigned) CPUID2_X2APIC);
/*
* Only advertise CPUID2_XSAVE in the guest if
* the host is using XSAVE.
*/
if (!(regs[2] & ((unsigned) CPUID2_OSXSAVE)))
regs[2] &= ~((unsigned) CPUID2_XSAVE);
/*
* If CPUID2_XSAVE is being advertised and the
* guest has set CR4_XSAVE, set
* CPUID2_OSXSAVE.
*/
regs[2] &= ~((unsigned) CPUID2_OSXSAVE);
if (regs[2] & ((unsigned) CPUID2_XSAVE)) {
error = vm_get_register(vm, vcpu_id, VM_REG_GUEST_CR4, &cr4);
if (error)
xhyve_abort("x86_emulate_cpuid: error %d "
"fetching %%cr4\n", error);
if (cr4 & CR4_XSAVE)
regs[2] |= ((unsigned) CPUID2_OSXSAVE);
}
/*
* Hide monitor/mwait until we know how to deal with
* these instructions.
*/
regs[2] &= ~((unsigned) CPUID2_MON);
/*
* Hide the performance and debug features.
*/
regs[2] &= ~((unsigned) CPUID2_PDCM);
/*
* No TSC deadline support in the APIC yet
*/
regs[2] &= ~((unsigned) CPUID2_TSCDLT);
/*
* Hide thermal monitoring
*/
regs[3] &= ~((unsigned) (CPUID_ACPI | CPUID_TM));
/*
* Hide the debug store capability.
*/
regs[3] &= ~((unsigned) CPUID_DS);
/*
* Advertise the Machine Check and MTRR capability.
*
* Some guest OSes (e.g. Windows) will not boot if
* these features are absent.
*/
regs[3] |= (unsigned) (CPUID_MCA | CPUID_MCE | CPUID_MTRR);
logical_cpus = threads_per_core * cores_per_package;
regs[1] &= ~((unsigned) CPUID_HTT_CORES);
regs[1] |= (logical_cpus & 0xff) << 16;
regs[3] |= (unsigned) CPUID_HTT;
break;
case CPUID_0000_0004:
cpuid_count(*eax, *ecx, regs);
if (regs[0] || regs[1] || regs[2] || regs[3]) {
regs[0] &= 0x3ff;
regs[0] |= (cores_per_package - 1) << 26;
/*
* Cache topology:
* - L1 and L2 are shared only by the logical
* processors in a single core.
* - L3 and above are shared by all logical
* processors in the package.
*/
logical_cpus = threads_per_core;
level = (regs[0] >> 5) & 0x7;
if (level >= 3)
logical_cpus *= cores_per_package;
regs[0] |= (logical_cpus - 1) << 14;
}
break;
case CPUID_0000_0007:
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
/* leaf 0 */
if (*ecx == 0) {
cpuid_count(*eax, *ecx, regs);
/* Only leaf 0 is supported */
regs[0] = 0;
/*
* Expose known-safe features.
*/
regs[1] &= (CPUID_STDEXT_FSGSBASE |
CPUID_STDEXT_BMI1 | CPUID_STDEXT_HLE |
CPUID_STDEXT_AVX2 | CPUID_STDEXT_BMI2 |
CPUID_STDEXT_ERMS | CPUID_STDEXT_RTM |
CPUID_STDEXT_AVX512F |
CPUID_STDEXT_AVX512PF |
CPUID_STDEXT_AVX512ER |
CPUID_STDEXT_AVX512CD);
regs[2] = 0;
regs[3] = 0;
/* FIXME */
// regs[1] |= CPUID_STDEXT_INVPCID;
}
break;
case CPUID_0000_0006:
regs[0] = CPUTPM1_ARAT;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
break;
case CPUID_0000_000A:
/*
* Handle the access, but report 0 for
* all options
*/
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
break;
case CPUID_0000_000B:
/*
* Processor topology enumeration
*/
logical_cpus = 0;
width = 0;
level = 0;
x2apic_id = 0;
if (*ecx == 0) {
logical_cpus = threads_per_core;
width = log2(logical_cpus);
level = CPUID_TYPE_SMT;
x2apic_id = vcpu_id;
}
if (*ecx == 1) {
logical_cpus = threads_per_core *
cores_per_package;
width = log2(logical_cpus);
level = CPUID_TYPE_CORE;
x2apic_id = vcpu_id;
}
if (!cpuid_leaf_b || *ecx >= 2) {
width = 0;
logical_cpus = 0;
level = 0;
x2apic_id = 0;
}
regs[0] = width & 0x1f;
regs[1] = logical_cpus & 0xffff;
regs[2] = (((unsigned) level) << 8) | (*ecx & 0xff);
regs[3] = (unsigned) x2apic_id;
break;
case CPUID_0000_000D:
limits = vmm_get_xsave_limits();
if (!limits->xsave_enabled) {
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
break;
}
cpuid_count(*eax, *ecx, regs);
switch (*ecx) {
case 0:
/*
* Only permit the guest to use bits
* that are active in the host in
* %xcr0. Also, claim that the
* maximum save area size is
* equivalent to the host's current
* save area size. Since this runs
* "inside" of vmrun(), it runs with
* the guest's xcr0, so the current
* save area size is correct as-is.
*/
regs[0] &= limits->xcr0_allowed;
regs[2] = limits->xsave_max_size;
regs[3] &= (limits->xcr0_allowed >> 32);
break;
case 1:
/* Only permit XSAVEOPT. */
regs[0] &= CPUID_EXTSTATE_XSAVEOPT;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
break;
default:
/*
* If the leaf is for a permitted feature,
* pass through as-is, otherwise return
* all zeroes.
*/
if (!(limits->xcr0_allowed & (1ul << *ecx))) {
regs[0] = 0;
regs[1] = 0;
regs[2] = 0;
regs[3] = 0;
}
break;
}
break;
case 0x40000000:
regs[0] = CPUID_VM_HIGH;
bcopy(bhyve_id, ®s[1], 4);
bcopy(bhyve_id + 4, ®s[2], 4);
bcopy(bhyve_id + 8, ®s[3], 4);
break;
default:
/*
* The leaf value has already been clamped so
* simply pass this through, keeping count of
* how many unhandled leaf values have been seen.
*/
atomic_add_long(&bhyve_xcpuids, 1);
cpuid_count(*eax, *ecx, regs);
break;
}
*eax = regs[0];
*ebx = regs[1];
*ecx = regs[2];
*edx = regs[3];
return (1);
}