VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/CPUMR3CpuId.cpp@ 57211

最後變更 在這個檔案從57211是 57124,由 vboxsync 提交於 10 年 前

CPUMR3CpuId.cpp: Disable AVX and XSAVE for 64-bit guest on 32-bit host, at least for the present.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 292.8 KB
 
1/* $Id: CPUMR3CpuId.cpp 57124 2015-07-30 07:44:21Z vboxsync $ */
2/** @file
3 * CPUM - CPU ID part.
4 */
5
6/*
7 * Copyright (C) 2013-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_CPUM
22#include <VBox/vmm/cpum.h>
23#include <VBox/vmm/dbgf.h>
24#include <VBox/vmm/hm.h>
25#include <VBox/vmm/ssm.h>
26#include "CPUMInternal.h"
27#include <VBox/vmm/vm.h>
28#include <VBox/vmm/mm.h>
29
30#include <VBox/err.h>
31#include <iprt/asm-amd64-x86.h>
32#include <iprt/ctype.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35
36
37/*******************************************************************************
38* Defined Constants And Macros *
39*******************************************************************************/
40/** For sanity and avoid wasting hyper heap on buggy config / saved state. */
41#define CPUM_CPUID_MAX_LEAVES 2048
42/* Max size we accept for the XSAVE area. */
43#define CPUM_MAX_XSAVE_AREA_SIZE 10240
44/* Min size we accept for the XSAVE area. */
45#define CPUM_MIN_XSAVE_AREA_SIZE 0x240
46
47
48/*******************************************************************************
49* Global Variables *
50*******************************************************************************/
51/**
52 * The intel pentium family.
53 */
54static const CPUMMICROARCH g_aenmIntelFamily06[] =
55{
56 /* [ 0(0x00)] = */ kCpumMicroarch_Intel_P6, /* Pentium Pro A-step (says sandpile.org). */
57 /* [ 1(0x01)] = */ kCpumMicroarch_Intel_P6, /* Pentium Pro */
58 /* [ 2(0x02)] = */ kCpumMicroarch_Intel_Unknown,
59 /* [ 3(0x03)] = */ kCpumMicroarch_Intel_P6_II, /* PII Klamath */
60 /* [ 4(0x04)] = */ kCpumMicroarch_Intel_Unknown,
61 /* [ 5(0x05)] = */ kCpumMicroarch_Intel_P6_II, /* PII Deschutes */
62 /* [ 6(0x06)] = */ kCpumMicroarch_Intel_P6_II, /* Celeron Mendocino. */
63 /* [ 7(0x07)] = */ kCpumMicroarch_Intel_P6_III, /* PIII Katmai. */
64 /* [ 8(0x08)] = */ kCpumMicroarch_Intel_P6_III, /* PIII Coppermine (includes Celeron). */
65 /* [ 9(0x09)] = */ kCpumMicroarch_Intel_P6_M_Banias, /* Pentium/Celeron M Banias. */
66 /* [10(0x0a)] = */ kCpumMicroarch_Intel_P6_III, /* PIII Xeon */
67 /* [11(0x0b)] = */ kCpumMicroarch_Intel_P6_III, /* PIII Tualatin (includes Celeron). */
68 /* [12(0x0c)] = */ kCpumMicroarch_Intel_Unknown,
69 /* [13(0x0d)] = */ kCpumMicroarch_Intel_P6_M_Dothan, /* Pentium/Celeron M Dothan. */
70 /* [14(0x0e)] = */ kCpumMicroarch_Intel_Core_Yonah, /* Core Yonah (Enhanced Pentium M). */
71 /* [15(0x0f)] = */ kCpumMicroarch_Intel_Core2_Merom, /* Merom */
72 /* [16(0x10)] = */ kCpumMicroarch_Intel_Unknown,
73 /* [17(0x11)] = */ kCpumMicroarch_Intel_Unknown,
74 /* [18(0x12)] = */ kCpumMicroarch_Intel_Unknown,
75 /* [19(0x13)] = */ kCpumMicroarch_Intel_Unknown,
76 /* [20(0x14)] = */ kCpumMicroarch_Intel_Unknown,
77 /* [21(0x15)] = */ kCpumMicroarch_Intel_P6_M_Dothan, /* Tolapai - System-on-a-chip. */
78 /* [22(0x16)] = */ kCpumMicroarch_Intel_Core2_Merom,
79 /* [23(0x17)] = */ kCpumMicroarch_Intel_Core2_Penryn,
80 /* [24(0x18)] = */ kCpumMicroarch_Intel_Unknown,
81 /* [25(0x19)] = */ kCpumMicroarch_Intel_Unknown,
82 /* [26(0x1a)] = */ kCpumMicroarch_Intel_Core7_Nehalem,
83 /* [27(0x1b)] = */ kCpumMicroarch_Intel_Unknown,
84 /* [28(0x1c)] = */ kCpumMicroarch_Intel_Atom_Bonnell, /* Diamonville, Pineview, */
85 /* [29(0x1d)] = */ kCpumMicroarch_Intel_Core2_Penryn,
86 /* [30(0x1e)] = */ kCpumMicroarch_Intel_Core7_Nehalem, /* Clarksfield, Lynnfield, Jasper Forest. */
87 /* [31(0x1f)] = */ kCpumMicroarch_Intel_Core7_Nehalem, /* Only listed by sandpile.org. 2 cores ABD/HVD, whatever that means. */
88 /* [32(0x20)] = */ kCpumMicroarch_Intel_Unknown,
89 /* [33(0x21)] = */ kCpumMicroarch_Intel_Unknown,
90 /* [34(0x22)] = */ kCpumMicroarch_Intel_Unknown,
91 /* [35(0x23)] = */ kCpumMicroarch_Intel_Unknown,
92 /* [36(0x24)] = */ kCpumMicroarch_Intel_Unknown,
93 /* [37(0x25)] = */ kCpumMicroarch_Intel_Core7_Westmere, /* Arrandale, Clarksdale. */
94 /* [38(0x26)] = */ kCpumMicroarch_Intel_Atom_Lincroft,
95 /* [39(0x27)] = */ kCpumMicroarch_Intel_Atom_Saltwell,
96 /* [40(0x28)] = */ kCpumMicroarch_Intel_Unknown,
97 /* [41(0x29)] = */ kCpumMicroarch_Intel_Unknown,
98 /* [42(0x2a)] = */ kCpumMicroarch_Intel_Core7_SandyBridge,
99 /* [43(0x2b)] = */ kCpumMicroarch_Intel_Unknown,
100 /* [44(0x2c)] = */ kCpumMicroarch_Intel_Core7_Westmere, /* Gulftown, Westmere-EP. */
101 /* [45(0x2d)] = */ kCpumMicroarch_Intel_Core7_SandyBridge, /* SandyBridge-E, SandyBridge-EN, SandyBridge-EP. */
102 /* [46(0x2e)] = */ kCpumMicroarch_Intel_Core7_Nehalem, /* Beckton (Xeon). */
103 /* [47(0x2f)] = */ kCpumMicroarch_Intel_Core7_Westmere, /* Westmere-EX. */
104 /* [48(0x30)] = */ kCpumMicroarch_Intel_Unknown,
105 /* [49(0x31)] = */ kCpumMicroarch_Intel_Unknown,
106 /* [50(0x32)] = */ kCpumMicroarch_Intel_Unknown,
107 /* [51(0x33)] = */ kCpumMicroarch_Intel_Unknown,
108 /* [52(0x34)] = */ kCpumMicroarch_Intel_Unknown,
109 /* [53(0x35)] = */ kCpumMicroarch_Intel_Atom_Saltwell, /* ?? */
110 /* [54(0x36)] = */ kCpumMicroarch_Intel_Atom_Saltwell, /* Cedarview, ++ */
111 /* [55(0x37)] = */ kCpumMicroarch_Intel_Atom_Silvermont,
112 /* [56(0x38)] = */ kCpumMicroarch_Intel_Unknown,
113 /* [57(0x39)] = */ kCpumMicroarch_Intel_Unknown,
114 /* [58(0x3a)] = */ kCpumMicroarch_Intel_Core7_IvyBridge,
115 /* [59(0x3b)] = */ kCpumMicroarch_Intel_Unknown,
116 /* [60(0x3c)] = */ kCpumMicroarch_Intel_Core7_Haswell,
117 /* [61(0x3d)] = */ kCpumMicroarch_Intel_Core7_Broadwell,
118 /* [62(0x3e)] = */ kCpumMicroarch_Intel_Core7_IvyBridge,
119 /* [63(0x3f)] = */ kCpumMicroarch_Intel_Core7_Haswell,
120 /* [64(0x40)] = */ kCpumMicroarch_Intel_Unknown,
121 /* [65(0x41)] = */ kCpumMicroarch_Intel_Unknown,
122 /* [66(0x42)] = */ kCpumMicroarch_Intel_Unknown,
123 /* [67(0x43)] = */ kCpumMicroarch_Intel_Unknown,
124 /* [68(0x44)] = */ kCpumMicroarch_Intel_Unknown,
125 /* [69(0x45)] = */ kCpumMicroarch_Intel_Core7_Haswell,
126 /* [70(0x46)] = */ kCpumMicroarch_Intel_Core7_Haswell,
127 /* [71(0x47)] = */ kCpumMicroarch_Intel_Unknown,
128 /* [72(0x48)] = */ kCpumMicroarch_Intel_Unknown,
129 /* [73(0x49)] = */ kCpumMicroarch_Intel_Unknown,
130 /* [74(0x4a)] = */ kCpumMicroarch_Intel_Atom_Silvermont,
131 /* [75(0x4b)] = */ kCpumMicroarch_Intel_Unknown,
132 /* [76(0x4c)] = */ kCpumMicroarch_Intel_Unknown,
133 /* [77(0x4d)] = */ kCpumMicroarch_Intel_Atom_Silvermont,
134 /* [78(0x4e)] = */ kCpumMicroarch_Intel_Unknown,
135 /* [79(0x4f)] = */ kCpumMicroarch_Intel_Unknown,
136};
137
138
139
140/**
141 * Figures out the (sub-)micro architecture given a bit of CPUID info.
142 *
143 * @returns Micro architecture.
144 * @param enmVendor The CPU vendor .
145 * @param bFamily The CPU family.
146 * @param bModel The CPU model.
147 * @param bStepping The CPU stepping.
148 */
149VMMR3DECL(CPUMMICROARCH) CPUMR3CpuIdDetermineMicroarchEx(CPUMCPUVENDOR enmVendor, uint8_t bFamily,
150 uint8_t bModel, uint8_t bStepping)
151{
152 if (enmVendor == CPUMCPUVENDOR_AMD)
153 {
154 switch (bFamily)
155 {
156 case 0x02: return kCpumMicroarch_AMD_Am286; /* Not really kosher... */
157 case 0x03: return kCpumMicroarch_AMD_Am386;
158 case 0x23: return kCpumMicroarch_AMD_Am386; /* SX*/
159 case 0x04: return bModel < 14 ? kCpumMicroarch_AMD_Am486 : kCpumMicroarch_AMD_Am486Enh;
160 case 0x05: return bModel < 6 ? kCpumMicroarch_AMD_K5 : kCpumMicroarch_AMD_K6; /* Genode LX is 0x0a, lump it with K6. */
161 case 0x06:
162 switch (bModel)
163 {
164 case 0: return kCpumMicroarch_AMD_K7_Palomino;
165 case 1: return kCpumMicroarch_AMD_K7_Palomino;
166 case 2: return kCpumMicroarch_AMD_K7_Palomino;
167 case 3: return kCpumMicroarch_AMD_K7_Spitfire;
168 case 4: return kCpumMicroarch_AMD_K7_Thunderbird;
169 case 6: return kCpumMicroarch_AMD_K7_Palomino;
170 case 7: return kCpumMicroarch_AMD_K7_Morgan;
171 case 8: return kCpumMicroarch_AMD_K7_Thoroughbred;
172 case 10: return kCpumMicroarch_AMD_K7_Barton; /* Thorton too. */
173 }
174 return kCpumMicroarch_AMD_K7_Unknown;
175 case 0x0f:
176 /*
177 * This family is a friggin mess. Trying my best to make some
178 * sense out of it. Too much happened in the 0x0f family to
179 * lump it all together as K8 (130nm->90nm->65nm, AMD-V, ++).
180 *
181 * Emperical CPUID.01h.EAX evidence from revision guides, wikipedia,
182 * cpu-world.com, and other places:
183 * - 130nm:
184 * - ClawHammer: F7A/SH-CG, F5A/-CG, F4A/-CG, F50/-B0, F48/-C0, F58/-C0,
185 * - SledgeHammer: F50/SH-B0, F48/-C0, F58/-C0, F4A/-CG, F5A/-CG, F7A/-CG, F51/-B3
186 * - Newcastle: FC0/DH-CG (errum #180: FE0/DH-CG), FF0/DH-CG
187 * - Dublin: FC0/-CG, FF0/-CG, F82/CH-CG, F4A/-CG, F48/SH-C0,
188 * - Odessa: FC0/DH-CG (errum #180: FE0/DH-CG)
189 * - Paris: FF0/DH-CG, FC0/DH-CG (errum #180: FE0/DH-CG),
190 * - 90nm:
191 * - Winchester: 10FF0/DH-D0, 20FF0/DH-E3.
192 * - Oakville: 10FC0/DH-D0.
193 * - Georgetown: 10FC0/DH-D0.
194 * - Sonora: 10FC0/DH-D0.
195 * - Venus: 20F71/SH-E4
196 * - Troy: 20F51/SH-E4
197 * - Athens: 20F51/SH-E4
198 * - San Diego: 20F71/SH-E4.
199 * - Lancaster: 20F42/SH-E5
200 * - Newark: 20F42/SH-E5.
201 * - Albany: 20FC2/DH-E6.
202 * - Roma: 20FC2/DH-E6.
203 * - Venice: 20FF0/DH-E3, 20FC2/DH-E6, 20FF2/DH-E6.
204 * - Palermo: 10FC0/DH-D0, 20FF0/DH-E3, 20FC0/DH-E3, 20FC2/DH-E6, 20FF2/DH-E6
205 * - 90nm introducing Dual core:
206 * - Denmark: 20F30/JH-E1, 20F32/JH-E6
207 * - Italy: 20F10/JH-E1, 20F12/JH-E6
208 * - Egypt: 20F10/JH-E1, 20F12/JH-E6
209 * - Toledo: 20F32/JH-E6, 30F72/DH-E6 (single code variant).
210 * - Manchester: 20FB1/BH-E4, 30FF2/BH-E4.
211 * - 90nm 2nd gen opteron ++, AMD-V introduced (might be missing in some cheaper models):
212 * - Santa Ana: 40F32/JH-F2, /-F3
213 * - Santa Rosa: 40F12/JH-F2, 40F13/JH-F3
214 * - Windsor: 40F32/JH-F2, 40F33/JH-F3, C0F13/JH-F3, 40FB2/BH-F2, ??20FB1/BH-E4??.
215 * - Manila: 50FF2/DH-F2, 40FF2/DH-F2
216 * - Orleans: 40FF2/DH-F2, 50FF2/DH-F2, 50FF3/DH-F3.
217 * - Keene: 40FC2/DH-F2.
218 * - Richmond: 40FC2/DH-F2
219 * - Taylor: 40F82/BH-F2
220 * - Trinidad: 40F82/BH-F2
221 *
222 * - 65nm:
223 * - Brisbane: 60FB1/BH-G1, 60FB2/BH-G2.
224 * - Tyler: 60F81/BH-G1, 60F82/BH-G2.
225 * - Sparta: 70FF1/DH-G1, 70FF2/DH-G2.
226 * - Lima: 70FF1/DH-G1, 70FF2/DH-G2.
227 * - Sherman: /-G1, 70FC2/DH-G2.
228 * - Huron: 70FF2/DH-G2.
229 */
230 if (bModel < 0x10)
231 return kCpumMicroarch_AMD_K8_130nm;
232 if (bModel >= 0x60 && bModel < 0x80)
233 return kCpumMicroarch_AMD_K8_65nm;
234 if (bModel >= 0x40)
235 return kCpumMicroarch_AMD_K8_90nm_AMDV;
236 switch (bModel)
237 {
238 case 0x21:
239 case 0x23:
240 case 0x2b:
241 case 0x2f:
242 case 0x37:
243 case 0x3f:
244 return kCpumMicroarch_AMD_K8_90nm_DualCore;
245 }
246 return kCpumMicroarch_AMD_K8_90nm;
247 case 0x10:
248 return kCpumMicroarch_AMD_K10;
249 case 0x11:
250 return kCpumMicroarch_AMD_K10_Lion;
251 case 0x12:
252 return kCpumMicroarch_AMD_K10_Llano;
253 case 0x14:
254 return kCpumMicroarch_AMD_Bobcat;
255 case 0x15:
256 switch (bModel)
257 {
258 case 0x00: return kCpumMicroarch_AMD_15h_Bulldozer; /* Any? prerelease? */
259 case 0x01: return kCpumMicroarch_AMD_15h_Bulldozer; /* Opteron 4200, FX-81xx. */
260 case 0x02: return kCpumMicroarch_AMD_15h_Piledriver; /* Opteron 4300, FX-83xx. */
261 case 0x10: return kCpumMicroarch_AMD_15h_Piledriver; /* A10-5800K for e.g. */
262 case 0x11: /* ?? */
263 case 0x12: /* ?? */
264 case 0x13: return kCpumMicroarch_AMD_15h_Piledriver; /* A10-6800K for e.g. */
265 }
266 return kCpumMicroarch_AMD_15h_Unknown;
267 case 0x16:
268 return kCpumMicroarch_AMD_Jaguar;
269
270 }
271 return kCpumMicroarch_AMD_Unknown;
272 }
273
274 if (enmVendor == CPUMCPUVENDOR_INTEL)
275 {
276 switch (bFamily)
277 {
278 case 3:
279 return kCpumMicroarch_Intel_80386;
280 case 4:
281 return kCpumMicroarch_Intel_80486;
282 case 5:
283 return kCpumMicroarch_Intel_P5;
284 case 6:
285 if (bModel < RT_ELEMENTS(g_aenmIntelFamily06))
286 return g_aenmIntelFamily06[bModel];
287 return kCpumMicroarch_Intel_Atom_Unknown;
288 case 15:
289 switch (bModel)
290 {
291 case 0: return kCpumMicroarch_Intel_NB_Willamette;
292 case 1: return kCpumMicroarch_Intel_NB_Willamette;
293 case 2: return kCpumMicroarch_Intel_NB_Northwood;
294 case 3: return kCpumMicroarch_Intel_NB_Prescott;
295 case 4: return kCpumMicroarch_Intel_NB_Prescott2M; /* ?? */
296 case 5: return kCpumMicroarch_Intel_NB_Unknown; /*??*/
297 case 6: return kCpumMicroarch_Intel_NB_CedarMill;
298 case 7: return kCpumMicroarch_Intel_NB_Gallatin;
299 default: return kCpumMicroarch_Intel_NB_Unknown;
300 }
301 break;
302 /* The following are not kosher but kind of follow intuitively from 6, 5 & 4. */
303 case 1:
304 return kCpumMicroarch_Intel_8086;
305 case 2:
306 return kCpumMicroarch_Intel_80286;
307 }
308 return kCpumMicroarch_Intel_Unknown;
309 }
310
311 if (enmVendor == CPUMCPUVENDOR_VIA)
312 {
313 switch (bFamily)
314 {
315 case 5:
316 switch (bModel)
317 {
318 case 1: return kCpumMicroarch_Centaur_C6;
319 case 4: return kCpumMicroarch_Centaur_C6;
320 case 8: return kCpumMicroarch_Centaur_C2;
321 case 9: return kCpumMicroarch_Centaur_C3;
322 }
323 break;
324
325 case 6:
326 switch (bModel)
327 {
328 case 5: return kCpumMicroarch_VIA_C3_M2;
329 case 6: return kCpumMicroarch_VIA_C3_C5A;
330 case 7: return bStepping < 8 ? kCpumMicroarch_VIA_C3_C5B : kCpumMicroarch_VIA_C3_C5C;
331 case 8: return kCpumMicroarch_VIA_C3_C5N;
332 case 9: return bStepping < 8 ? kCpumMicroarch_VIA_C3_C5XL : kCpumMicroarch_VIA_C3_C5P;
333 case 10: return kCpumMicroarch_VIA_C7_C5J;
334 case 15: return kCpumMicroarch_VIA_Isaiah;
335 }
336 break;
337 }
338 return kCpumMicroarch_VIA_Unknown;
339 }
340
341 if (enmVendor == CPUMCPUVENDOR_CYRIX)
342 {
343 switch (bFamily)
344 {
345 case 4:
346 switch (bModel)
347 {
348 case 9: return kCpumMicroarch_Cyrix_5x86;
349 }
350 break;
351
352 case 5:
353 switch (bModel)
354 {
355 case 2: return kCpumMicroarch_Cyrix_M1;
356 case 4: return kCpumMicroarch_Cyrix_MediaGX;
357 case 5: return kCpumMicroarch_Cyrix_MediaGXm;
358 }
359 break;
360
361 case 6:
362 switch (bModel)
363 {
364 case 0: return kCpumMicroarch_Cyrix_M2;
365 }
366 break;
367
368 }
369 return kCpumMicroarch_Cyrix_Unknown;
370 }
371
372 return kCpumMicroarch_Unknown;
373}
374
375
376/**
377 * Translates a microarchitecture enum value to the corresponding string
378 * constant.
379 *
380 * @returns Read-only string constant (omits "kCpumMicroarch_" prefix). Returns
381 * NULL if the value is invalid.
382 *
383 * @param enmMicroarch The enum value to convert.
384 */
385VMMR3DECL(const char *) CPUMR3MicroarchName(CPUMMICROARCH enmMicroarch)
386{
387 switch (enmMicroarch)
388 {
389#define CASE_RET_STR(enmValue) case enmValue: return #enmValue + (sizeof("kCpumMicroarch_") - 1)
390 CASE_RET_STR(kCpumMicroarch_Intel_8086);
391 CASE_RET_STR(kCpumMicroarch_Intel_80186);
392 CASE_RET_STR(kCpumMicroarch_Intel_80286);
393 CASE_RET_STR(kCpumMicroarch_Intel_80386);
394 CASE_RET_STR(kCpumMicroarch_Intel_80486);
395 CASE_RET_STR(kCpumMicroarch_Intel_P5);
396
397 CASE_RET_STR(kCpumMicroarch_Intel_P6);
398 CASE_RET_STR(kCpumMicroarch_Intel_P6_II);
399 CASE_RET_STR(kCpumMicroarch_Intel_P6_III);
400
401 CASE_RET_STR(kCpumMicroarch_Intel_P6_M_Banias);
402 CASE_RET_STR(kCpumMicroarch_Intel_P6_M_Dothan);
403 CASE_RET_STR(kCpumMicroarch_Intel_Core_Yonah);
404
405 CASE_RET_STR(kCpumMicroarch_Intel_Core2_Merom);
406 CASE_RET_STR(kCpumMicroarch_Intel_Core2_Penryn);
407
408 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Nehalem);
409 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Westmere);
410 CASE_RET_STR(kCpumMicroarch_Intel_Core7_SandyBridge);
411 CASE_RET_STR(kCpumMicroarch_Intel_Core7_IvyBridge);
412 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Haswell);
413 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Broadwell);
414 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Skylake);
415 CASE_RET_STR(kCpumMicroarch_Intel_Core7_Cannonlake);
416
417 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Bonnell);
418 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Lincroft);
419 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Saltwell);
420 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Silvermont);
421 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Airmount);
422 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Goldmont);
423 CASE_RET_STR(kCpumMicroarch_Intel_Atom_Unknown);
424
425 CASE_RET_STR(kCpumMicroarch_Intel_NB_Willamette);
426 CASE_RET_STR(kCpumMicroarch_Intel_NB_Northwood);
427 CASE_RET_STR(kCpumMicroarch_Intel_NB_Prescott);
428 CASE_RET_STR(kCpumMicroarch_Intel_NB_Prescott2M);
429 CASE_RET_STR(kCpumMicroarch_Intel_NB_CedarMill);
430 CASE_RET_STR(kCpumMicroarch_Intel_NB_Gallatin);
431 CASE_RET_STR(kCpumMicroarch_Intel_NB_Unknown);
432
433 CASE_RET_STR(kCpumMicroarch_Intel_Unknown);
434
435 CASE_RET_STR(kCpumMicroarch_AMD_Am286);
436 CASE_RET_STR(kCpumMicroarch_AMD_Am386);
437 CASE_RET_STR(kCpumMicroarch_AMD_Am486);
438 CASE_RET_STR(kCpumMicroarch_AMD_Am486Enh);
439 CASE_RET_STR(kCpumMicroarch_AMD_K5);
440 CASE_RET_STR(kCpumMicroarch_AMD_K6);
441
442 CASE_RET_STR(kCpumMicroarch_AMD_K7_Palomino);
443 CASE_RET_STR(kCpumMicroarch_AMD_K7_Spitfire);
444 CASE_RET_STR(kCpumMicroarch_AMD_K7_Thunderbird);
445 CASE_RET_STR(kCpumMicroarch_AMD_K7_Morgan);
446 CASE_RET_STR(kCpumMicroarch_AMD_K7_Thoroughbred);
447 CASE_RET_STR(kCpumMicroarch_AMD_K7_Barton);
448 CASE_RET_STR(kCpumMicroarch_AMD_K7_Unknown);
449
450 CASE_RET_STR(kCpumMicroarch_AMD_K8_130nm);
451 CASE_RET_STR(kCpumMicroarch_AMD_K8_90nm);
452 CASE_RET_STR(kCpumMicroarch_AMD_K8_90nm_DualCore);
453 CASE_RET_STR(kCpumMicroarch_AMD_K8_90nm_AMDV);
454 CASE_RET_STR(kCpumMicroarch_AMD_K8_65nm);
455
456 CASE_RET_STR(kCpumMicroarch_AMD_K10);
457 CASE_RET_STR(kCpumMicroarch_AMD_K10_Lion);
458 CASE_RET_STR(kCpumMicroarch_AMD_K10_Llano);
459 CASE_RET_STR(kCpumMicroarch_AMD_Bobcat);
460 CASE_RET_STR(kCpumMicroarch_AMD_Jaguar);
461
462 CASE_RET_STR(kCpumMicroarch_AMD_15h_Bulldozer);
463 CASE_RET_STR(kCpumMicroarch_AMD_15h_Piledriver);
464 CASE_RET_STR(kCpumMicroarch_AMD_15h_Steamroller);
465 CASE_RET_STR(kCpumMicroarch_AMD_15h_Excavator);
466 CASE_RET_STR(kCpumMicroarch_AMD_15h_Unknown);
467
468 CASE_RET_STR(kCpumMicroarch_AMD_16h_First);
469
470 CASE_RET_STR(kCpumMicroarch_AMD_Unknown);
471
472 CASE_RET_STR(kCpumMicroarch_Centaur_C6);
473 CASE_RET_STR(kCpumMicroarch_Centaur_C2);
474 CASE_RET_STR(kCpumMicroarch_Centaur_C3);
475 CASE_RET_STR(kCpumMicroarch_VIA_C3_M2);
476 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5A);
477 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5B);
478 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5C);
479 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5N);
480 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5XL);
481 CASE_RET_STR(kCpumMicroarch_VIA_C3_C5P);
482 CASE_RET_STR(kCpumMicroarch_VIA_C7_C5J);
483 CASE_RET_STR(kCpumMicroarch_VIA_Isaiah);
484 CASE_RET_STR(kCpumMicroarch_VIA_Unknown);
485
486 CASE_RET_STR(kCpumMicroarch_Cyrix_5x86);
487 CASE_RET_STR(kCpumMicroarch_Cyrix_M1);
488 CASE_RET_STR(kCpumMicroarch_Cyrix_MediaGX);
489 CASE_RET_STR(kCpumMicroarch_Cyrix_MediaGXm);
490 CASE_RET_STR(kCpumMicroarch_Cyrix_M2);
491 CASE_RET_STR(kCpumMicroarch_Cyrix_Unknown);
492
493 CASE_RET_STR(kCpumMicroarch_Unknown);
494
495#undef CASE_RET_STR
496 case kCpumMicroarch_Invalid:
497 case kCpumMicroarch_Intel_End:
498 case kCpumMicroarch_Intel_Core7_End:
499 case kCpumMicroarch_Intel_Atom_End:
500 case kCpumMicroarch_Intel_P6_Core_Atom_End:
501 case kCpumMicroarch_Intel_NB_End:
502 case kCpumMicroarch_AMD_K7_End:
503 case kCpumMicroarch_AMD_K8_End:
504 case kCpumMicroarch_AMD_15h_End:
505 case kCpumMicroarch_AMD_16h_End:
506 case kCpumMicroarch_AMD_End:
507 case kCpumMicroarch_VIA_End:
508 case kCpumMicroarch_Cyrix_End:
509 case kCpumMicroarch_32BitHack:
510 break;
511 /* no default! */
512 }
513
514 return NULL;
515}
516
517
518
519/**
520 * Gets a matching leaf in the CPUID leaf array.
521 *
522 * @returns Pointer to the matching leaf, or NULL if not found.
523 * @param paLeaves The CPUID leaves to search. This is sorted.
524 * @param cLeaves The number of leaves in the array.
525 * @param uLeaf The leaf to locate.
526 * @param uSubLeaf The subleaf to locate. Pass 0 if no sub-leaves.
527 */
528static PCPUMCPUIDLEAF cpumR3CpuIdGetLeaf(PCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, uint32_t uLeaf, uint32_t uSubLeaf)
529{
530 /* Lazy bird does linear lookup here since this is only used for the
531 occational CPUID overrides. */
532 for (uint32_t i = 0; i < cLeaves; i++)
533 if ( paLeaves[i].uLeaf == uLeaf
534 && paLeaves[i].uSubLeaf == (uSubLeaf & paLeaves[i].fSubLeafMask))
535 return &paLeaves[i];
536 return NULL;
537}
538
539
540/**
541 * Gets a matching leaf in the CPUID leaf array, converted to a CPUMCPUID.
542 *
543 * @returns true if found, false it not.
544 * @param paLeaves The CPUID leaves to search. This is sorted.
545 * @param cLeaves The number of leaves in the array.
546 * @param uLeaf The leaf to locate.
547 * @param uSubLeaf The subleaf to locate. Pass 0 if no sub-leaves.
548 * @param pLegacy The legacy output leaf.
549 */
550static bool cpumR3CpuIdGetLeafLegacy(PCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, uint32_t uLeaf, uint32_t uSubLeaf,
551 PCPUMCPUID pLegacy)
552{
553 PCPUMCPUIDLEAF pLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, uLeaf, uSubLeaf);
554 if (pLeaf)
555 {
556 pLegacy->uEax = pLeaf->uEax;
557 pLegacy->uEbx = pLeaf->uEbx;
558 pLegacy->uEcx = pLeaf->uEcx;
559 pLegacy->uEdx = pLeaf->uEdx;
560 return true;
561 }
562 return false;
563}
564
565
566/**
567 * Ensures that the CPUID leaf array can hold one more leaf.
568 *
569 * @returns Pointer to the CPUID leaf array (*ppaLeaves) on success. NULL on
570 * failure.
571 * @param pVM Pointer to the VM, used as the heap selector. Passing
572 * NULL uses the host-context heap, otherwise the VM's
573 * hyper heap is used.
574 * @param ppaLeaves Pointer to the variable holding the array pointer
575 * (input/output).
576 * @param cLeaves The current array size.
577 *
578 * @remarks This function will automatically update the R0 and RC pointers when
579 * using the hyper heap, which means @a ppaLeaves and @a cLeaves must
580 * be the corresponding VM's CPUID arrays (which is asserted).
581 */
582static PCPUMCPUIDLEAF cpumR3CpuIdEnsureSpace(PVM pVM, PCPUMCPUIDLEAF *ppaLeaves, uint32_t cLeaves)
583{
584 /*
585 * If pVM is not specified, we're on the regular heap and can waste a
586 * little space to speed things up.
587 */
588 uint32_t cAllocated;
589 if (!pVM)
590 {
591 cAllocated = RT_ALIGN(cLeaves, 16);
592 if (cLeaves + 1 > cAllocated)
593 {
594 void *pvNew = RTMemRealloc(*ppaLeaves, (cAllocated + 16) * sizeof(**ppaLeaves));
595 if (pvNew)
596 *ppaLeaves = (PCPUMCPUIDLEAF)pvNew;
597 else
598 {
599 RTMemFree(*ppaLeaves);
600 *ppaLeaves = NULL;
601 }
602 }
603 }
604 /*
605 * Otherwise, we're on the hyper heap and are probably just inserting
606 * one or two leaves and should conserve space.
607 */
608 else
609 {
610#ifdef IN_VBOX_CPU_REPORT
611 AssertReleaseFailed();
612#else
613 Assert(ppaLeaves == &pVM->cpum.s.GuestInfo.paCpuIdLeavesR3);
614 Assert(cLeaves == pVM->cpum.s.GuestInfo.cCpuIdLeaves);
615
616 size_t cb = cLeaves * sizeof(**ppaLeaves);
617 size_t cbNew = (cLeaves + 1) * sizeof(**ppaLeaves);
618 int rc = MMR3HyperRealloc(pVM, *ppaLeaves, cb, 32, MM_TAG_CPUM_CPUID, cbNew, (void **)ppaLeaves);
619 if (RT_SUCCESS(rc))
620 {
621 /* Update the R0 and RC pointers. */
622 pVM->cpum.s.GuestInfo.paCpuIdLeavesR0 = MMHyperR3ToR0(pVM, *ppaLeaves);
623 pVM->cpum.s.GuestInfo.paCpuIdLeavesRC = MMHyperR3ToRC(pVM, *ppaLeaves);
624 }
625 else
626 {
627 *ppaLeaves = NULL;
628 pVM->cpum.s.GuestInfo.paCpuIdLeavesR0 = NIL_RTR0PTR;
629 pVM->cpum.s.GuestInfo.paCpuIdLeavesRC = NIL_RTRCPTR;
630 LogRel(("CPUM: cpumR3CpuIdEnsureSpace: MMR3HyperRealloc failed. rc=%Rrc\n", rc));
631 }
632#endif
633 }
634 return *ppaLeaves;
635}
636
637
638/**
639 * Append a CPUID leaf or sub-leaf.
640 *
641 * ASSUMES linear insertion order, so we'll won't need to do any searching or
642 * replace anything. Use cpumR3CpuIdInsert() for those cases.
643 *
644 * @returns VINF_SUCCESS or VERR_NO_MEMORY. On error, *ppaLeaves is freed, so
645 * the caller need do no more work.
646 * @param ppaLeaves Pointer to the the pointer to the array of sorted
647 * CPUID leaves and sub-leaves.
648 * @param pcLeaves Where we keep the leaf count for *ppaLeaves.
649 * @param uLeaf The leaf we're adding.
650 * @param uSubLeaf The sub-leaf number.
651 * @param fSubLeafMask The sub-leaf mask.
652 * @param uEax The EAX value.
653 * @param uEbx The EBX value.
654 * @param uEcx The ECX value.
655 * @param uEdx The EDX value.
656 * @param fFlags The flags.
657 */
658static int cpumR3CollectCpuIdInfoAddOne(PCPUMCPUIDLEAF *ppaLeaves, uint32_t *pcLeaves,
659 uint32_t uLeaf, uint32_t uSubLeaf, uint32_t fSubLeafMask,
660 uint32_t uEax, uint32_t uEbx, uint32_t uEcx, uint32_t uEdx, uint32_t fFlags)
661{
662 if (!cpumR3CpuIdEnsureSpace(NULL /* pVM */, ppaLeaves, *pcLeaves))
663 return VERR_NO_MEMORY;
664
665 PCPUMCPUIDLEAF pNew = &(*ppaLeaves)[*pcLeaves];
666 Assert( *pcLeaves == 0
667 || pNew[-1].uLeaf < uLeaf
668 || (pNew[-1].uLeaf == uLeaf && pNew[-1].uSubLeaf < uSubLeaf) );
669
670 pNew->uLeaf = uLeaf;
671 pNew->uSubLeaf = uSubLeaf;
672 pNew->fSubLeafMask = fSubLeafMask;
673 pNew->uEax = uEax;
674 pNew->uEbx = uEbx;
675 pNew->uEcx = uEcx;
676 pNew->uEdx = uEdx;
677 pNew->fFlags = fFlags;
678
679 *pcLeaves += 1;
680 return VINF_SUCCESS;
681}
682
683
684/**
685 * Checks that we've updated the CPUID leaves array correctly.
686 *
687 * This is a no-op in non-strict builds.
688 *
689 * @param paLeaves The leaves array.
690 * @param cLeaves The number of leaves.
691 */
692static void cpumR3CpuIdAssertOrder(PCPUMCPUIDLEAF paLeaves, uint32_t cLeaves)
693{
694#ifdef VBOX_STRICT
695 for (uint32_t i = 1; i < cLeaves; i++)
696 if (paLeaves[i].uLeaf != paLeaves[i - 1].uLeaf)
697 AssertMsg(paLeaves[i].uLeaf > paLeaves[i - 1].uLeaf, ("%#x vs %#x\n", paLeaves[i].uLeaf, paLeaves[i - 1].uLeaf));
698 else
699 {
700 AssertMsg(paLeaves[i].uSubLeaf > paLeaves[i - 1].uSubLeaf,
701 ("%#x: %#x vs %#x\n", paLeaves[i].uLeaf, paLeaves[i].uSubLeaf, paLeaves[i - 1].uSubLeaf));
702 AssertMsg(paLeaves[i].fSubLeafMask == paLeaves[i - 1].fSubLeafMask,
703 ("%#x/%#x: %#x vs %#x\n", paLeaves[i].uLeaf, paLeaves[i].uSubLeaf, paLeaves[i].fSubLeafMask, paLeaves[i - 1].fSubLeafMask));
704 AssertMsg(paLeaves[i].fFlags == paLeaves[i - 1].fFlags,
705 ("%#x/%#x: %#x vs %#x\n", paLeaves[i].uLeaf, paLeaves[i].uSubLeaf, paLeaves[i].fFlags, paLeaves[i - 1].fFlags));
706 }
707#else
708 NOREF(paLeaves);
709 NOREF(cLeaves);
710#endif
711}
712
713
714/**
715 * Inserts a CPU ID leaf, replacing any existing ones.
716 *
717 * When inserting a simple leaf where we already got a series of sub-leaves with
718 * the same leaf number (eax), the simple leaf will replace the whole series.
719 *
720 * When pVM is NULL, this ASSUMES that the leaves array is still on the normal
721 * host-context heap and has only been allocated/reallocated by the
722 * cpumR3CpuIdEnsureSpace function.
723 *
724 * @returns VBox status code.
725 * @param pVM Pointer to the VM, used as the heap selector.
726 * Passing NULL uses the host-context heap, otherwise
727 * the VM's hyper heap is used.
728 * @param ppaLeaves Pointer to the the pointer to the array of sorted
729 * CPUID leaves and sub-leaves. Must be NULL if using
730 * the hyper heap.
731 * @param pcLeaves Where we keep the leaf count for *ppaLeaves. Must be
732 * NULL if using the hyper heap.
733 * @param pNewLeaf Pointer to the data of the new leaf we're about to
734 * insert.
735 */
736static int cpumR3CpuIdInsert(PVM pVM, PCPUMCPUIDLEAF *ppaLeaves, uint32_t *pcLeaves, PCPUMCPUIDLEAF pNewLeaf)
737{
738 /*
739 * Validate input parameters if we are using the hyper heap and use the VM's CPUID arrays.
740 */
741 if (pVM)
742 {
743 AssertReturn(!ppaLeaves, VERR_INVALID_PARAMETER);
744 AssertReturn(!pcLeaves, VERR_INVALID_PARAMETER);
745
746 ppaLeaves = &pVM->cpum.s.GuestInfo.paCpuIdLeavesR3;
747 pcLeaves = &pVM->cpum.s.GuestInfo.cCpuIdLeaves;
748 }
749
750 PCPUMCPUIDLEAF paLeaves = *ppaLeaves;
751 uint32_t cLeaves = *pcLeaves;
752
753 /*
754 * Validate the new leaf a little.
755 */
756 AssertLogRelMsgReturn(!(pNewLeaf->fFlags & ~CPUMCPUIDLEAF_F_VALID_MASK),
757 ("%#x/%#x: %#x", pNewLeaf->uLeaf, pNewLeaf->uSubLeaf, pNewLeaf->fFlags),
758 VERR_INVALID_FLAGS);
759 AssertLogRelMsgReturn(pNewLeaf->fSubLeafMask != 0 || pNewLeaf->uSubLeaf == 0,
760 ("%#x/%#x: %#x", pNewLeaf->uLeaf, pNewLeaf->uSubLeaf, pNewLeaf->fSubLeafMask),
761 VERR_INVALID_PARAMETER);
762 AssertLogRelMsgReturn(RT_IS_POWER_OF_TWO(pNewLeaf->fSubLeafMask + 1),
763 ("%#x/%#x: %#x", pNewLeaf->uLeaf, pNewLeaf->uSubLeaf, pNewLeaf->fSubLeafMask),
764 VERR_INVALID_PARAMETER);
765 AssertLogRelMsgReturn((pNewLeaf->fSubLeafMask & pNewLeaf->uSubLeaf) == pNewLeaf->uSubLeaf,
766 ("%#x/%#x: %#x", pNewLeaf->uLeaf, pNewLeaf->uSubLeaf, pNewLeaf->fSubLeafMask),
767 VERR_INVALID_PARAMETER);
768
769 /*
770 * Find insertion point. The lazy bird uses the same excuse as in
771 * cpumR3CpuIdGetLeaf(), but optimizes for linear insertion (saved state).
772 */
773 uint32_t i;
774 if ( cLeaves > 0
775 && paLeaves[cLeaves - 1].uLeaf < pNewLeaf->uLeaf)
776 {
777 /* Add at end. */
778 i = cLeaves;
779 }
780 else if ( cLeaves > 0
781 && paLeaves[cLeaves - 1].uLeaf == pNewLeaf->uLeaf)
782 {
783 /* Either replacing the last leaf or dealing with sub-leaves. Spool
784 back to the first sub-leaf to pretend we did the linear search. */
785 i = cLeaves - 1;
786 while ( i > 0
787 && paLeaves[i - 1].uLeaf == pNewLeaf->uLeaf)
788 i--;
789 }
790 else
791 {
792 /* Linear search from the start. */
793 i = 0;
794 while ( i < cLeaves
795 && paLeaves[i].uLeaf < pNewLeaf->uLeaf)
796 i++;
797 }
798 if ( i < cLeaves
799 && paLeaves[i].uLeaf == pNewLeaf->uLeaf)
800 {
801 if (paLeaves[i].fSubLeafMask != pNewLeaf->fSubLeafMask)
802 {
803 /*
804 * The sub-leaf mask differs, replace all existing leaves with the
805 * same leaf number.
806 */
807 uint32_t c = 1;
808 while ( i + c < cLeaves
809 && paLeaves[i + c].uLeaf == pNewLeaf->uLeaf)
810 c++;
811 if (c > 1 && i + c < cLeaves)
812 {
813 memmove(&paLeaves[i + c], &paLeaves[i + 1], (cLeaves - i - c) * sizeof(paLeaves[0]));
814 *pcLeaves = cLeaves -= c - 1;
815 }
816
817 paLeaves[i] = *pNewLeaf;
818 cpumR3CpuIdAssertOrder(*ppaLeaves, *pcLeaves);
819 return VINF_SUCCESS;
820 }
821
822 /* Find sub-leaf insertion point. */
823 while ( i < cLeaves
824 && paLeaves[i].uSubLeaf < pNewLeaf->uSubLeaf
825 && paLeaves[i].uLeaf == pNewLeaf->uLeaf)
826 i++;
827
828 /*
829 * If we've got an exactly matching leaf, replace it.
830 */
831 if ( i < cLeaves
832 && paLeaves[i].uLeaf == pNewLeaf->uLeaf
833 && paLeaves[i].uSubLeaf == pNewLeaf->uSubLeaf)
834 {
835 paLeaves[i] = *pNewLeaf;
836 cpumR3CpuIdAssertOrder(*ppaLeaves, *pcLeaves);
837 return VINF_SUCCESS;
838 }
839 }
840
841 /*
842 * Adding a new leaf at 'i'.
843 */
844 AssertLogRelReturn(cLeaves < CPUM_CPUID_MAX_LEAVES, VERR_TOO_MANY_CPUID_LEAVES);
845 paLeaves = cpumR3CpuIdEnsureSpace(pVM, ppaLeaves, cLeaves);
846 if (!paLeaves)
847 return VERR_NO_MEMORY;
848
849 if (i < cLeaves)
850 memmove(&paLeaves[i + 1], &paLeaves[i], (cLeaves - i) * sizeof(paLeaves[0]));
851 *pcLeaves += 1;
852 paLeaves[i] = *pNewLeaf;
853
854 cpumR3CpuIdAssertOrder(*ppaLeaves, *pcLeaves);
855 return VINF_SUCCESS;
856}
857
858
859/**
860 * Removes a range of CPUID leaves.
861 *
862 * This will not reallocate the array.
863 *
864 * @param paLeaves The array of sorted CPUID leaves and sub-leaves.
865 * @param pcLeaves Where we keep the leaf count for @a paLeaves.
866 * @param uFirst The first leaf.
867 * @param uLast The last leaf.
868 */
869static void cpumR3CpuIdRemoveRange(PCPUMCPUIDLEAF paLeaves, uint32_t *pcLeaves, uint32_t uFirst, uint32_t uLast)
870{
871 uint32_t cLeaves = *pcLeaves;
872
873 Assert(uFirst <= uLast);
874
875 /*
876 * Find the first one.
877 */
878 uint32_t iFirst = 0;
879 while ( iFirst < cLeaves
880 && paLeaves[iFirst].uLeaf < uFirst)
881 iFirst++;
882
883 /*
884 * Find the end (last + 1).
885 */
886 uint32_t iEnd = iFirst;
887 while ( iEnd < cLeaves
888 && paLeaves[iEnd].uLeaf <= uLast)
889 iEnd++;
890
891 /*
892 * Adjust the array if anything needs removing.
893 */
894 if (iFirst < iEnd)
895 {
896 if (iEnd < cLeaves)
897 memmove(&paLeaves[iFirst], &paLeaves[iEnd], (cLeaves - iEnd) * sizeof(paLeaves[0]));
898 *pcLeaves = cLeaves -= (iEnd - iFirst);
899 }
900
901 cpumR3CpuIdAssertOrder(paLeaves, *pcLeaves);
902}
903
904
905
906/**
907 * Checks if ECX make a difference when reading a given CPUID leaf.
908 *
909 * @returns @c true if it does, @c false if it doesn't.
910 * @param uLeaf The leaf we're reading.
911 * @param pcSubLeaves Number of sub-leaves accessible via ECX.
912 * @param pfFinalEcxUnchanged Whether ECX is passed thru when going beyond the
913 * final sub-leaf (for leaf 0xb only).
914 */
915static bool cpumR3IsEcxRelevantForCpuIdLeaf(uint32_t uLeaf, uint32_t *pcSubLeaves, bool *pfFinalEcxUnchanged)
916{
917 *pfFinalEcxUnchanged = false;
918
919 uint32_t auCur[4];
920 uint32_t auPrev[4];
921 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &auPrev[0], &auPrev[1], &auPrev[2], &auPrev[3]);
922
923 /* Look for sub-leaves. */
924 uint32_t uSubLeaf = 1;
925 for (;;)
926 {
927 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &auCur[0], &auCur[1], &auCur[2], &auCur[3]);
928 if (memcmp(auCur, auPrev, sizeof(auCur)))
929 break;
930
931 /* Advance / give up. */
932 uSubLeaf++;
933 if (uSubLeaf >= 64)
934 {
935 *pcSubLeaves = 1;
936 return false;
937 }
938 }
939
940 /* Count sub-leaves. */
941 uint32_t cMinLeaves = uLeaf == 0xd ? 64 : 0;
942 uint32_t cRepeats = 0;
943 uSubLeaf = 0;
944 for (;;)
945 {
946 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &auCur[0], &auCur[1], &auCur[2], &auCur[3]);
947
948 /* Figuring out when to stop isn't entirely straight forward as we need
949 to cover undocumented behavior up to a point and implementation shortcuts. */
950
951 /* 1. Look for more than 4 repeating value sets. */
952 if ( auCur[0] == auPrev[0]
953 && auCur[1] == auPrev[1]
954 && ( auCur[2] == auPrev[2]
955 || ( auCur[2] == uSubLeaf
956 && auPrev[2] == uSubLeaf - 1) )
957 && auCur[3] == auPrev[3])
958 {
959 if ( uLeaf != 0xd
960 || uSubLeaf >= 64
961 || ( auCur[0] == 0
962 && auCur[1] == 0
963 && auCur[2] == 0
964 && auCur[3] == 0
965 && auPrev[2] == 0) )
966 cRepeats++;
967 if (cRepeats > 4 && uSubLeaf >= cMinLeaves)
968 break;
969 }
970 else
971 cRepeats = 0;
972
973 /* 2. Look for zero values. */
974 if ( auCur[0] == 0
975 && auCur[1] == 0
976 && (auCur[2] == 0 || auCur[2] == uSubLeaf)
977 && (auCur[3] == 0 || uLeaf == 0xb /* edx is fixed */)
978 && uSubLeaf >= cMinLeaves)
979 {
980 cRepeats = 0;
981 break;
982 }
983
984 /* 3. Leaf 0xb level type 0 check. */
985 if ( uLeaf == 0xb
986 && (auCur[2] & 0xff00) == 0
987 && (auPrev[2] & 0xff00) == 0)
988 {
989 cRepeats = 0;
990 break;
991 }
992
993 /* 99. Give up. */
994 if (uSubLeaf >= 128)
995 {
996#ifndef IN_VBOX_CPU_REPORT
997 /* Ok, limit it according to the documentation if possible just to
998 avoid annoying users with these detection issues. */
999 uint32_t cDocLimit = UINT32_MAX;
1000 if (uLeaf == 0x4)
1001 cDocLimit = 4;
1002 else if (uLeaf == 0x7)
1003 cDocLimit = 1;
1004 else if (uLeaf == 0xd)
1005 cDocLimit = 63;
1006 else if (uLeaf == 0xf)
1007 cDocLimit = 2;
1008 if (cDocLimit != UINT32_MAX)
1009 {
1010 *pfFinalEcxUnchanged = auCur[2] == uSubLeaf && uLeaf == 0xb;
1011 *pcSubLeaves = cDocLimit + 3;
1012 return true;
1013 }
1014#endif
1015 *pcSubLeaves = UINT32_MAX;
1016 return true;
1017 }
1018
1019 /* Advance. */
1020 uSubLeaf++;
1021 memcpy(auPrev, auCur, sizeof(auCur));
1022 }
1023
1024 /* Standard exit. */
1025 *pfFinalEcxUnchanged = auCur[2] == uSubLeaf && uLeaf == 0xb;
1026 *pcSubLeaves = uSubLeaf + 1 - cRepeats;
1027 if (*pcSubLeaves == 0)
1028 *pcSubLeaves = 1;
1029 return true;
1030}
1031
1032
1033/**
1034 * Gets a CPU ID leaf.
1035 *
1036 * @returns VBox status code.
1037 * @param pVM Pointer to the VM.
1038 * @param pLeaf Where to store the found leaf.
1039 * @param uLeaf The leaf to locate.
1040 * @param uSubLeaf The subleaf to locate. Pass 0 if no sub-leaves.
1041 */
1042VMMR3DECL(int) CPUMR3CpuIdGetLeaf(PVM pVM, PCPUMCPUIDLEAF pLeaf, uint32_t uLeaf, uint32_t uSubLeaf)
1043{
1044 PCPUMCPUIDLEAF pcLeaf = cpumR3CpuIdGetLeaf(pVM->cpum.s.GuestInfo.paCpuIdLeavesR3, pVM->cpum.s.GuestInfo.cCpuIdLeaves,
1045 uLeaf, uSubLeaf);
1046 if (pcLeaf)
1047 {
1048 memcpy(pLeaf, pcLeaf, sizeof(*pLeaf));
1049 return VINF_SUCCESS;
1050 }
1051
1052 return VERR_NOT_FOUND;
1053}
1054
1055
1056/**
1057 * Inserts a CPU ID leaf, replacing any existing ones.
1058 *
1059 * @returns VBox status code.
1060 * @param pVM Pointer to the VM.
1061 * @param pNewLeaf Pointer to the leaf being inserted.
1062 */
1063VMMR3DECL(int) CPUMR3CpuIdInsert(PVM pVM, PCPUMCPUIDLEAF pNewLeaf)
1064{
1065 /*
1066 * Validate parameters.
1067 */
1068 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1069 AssertReturn(pNewLeaf, VERR_INVALID_PARAMETER);
1070
1071 /*
1072 * Disallow replacing CPU ID leaves that this API currently cannot manage.
1073 * These leaves have dependencies on saved-states, see PATMCpuidReplacement().
1074 * If you want to modify these leaves, use CPUMSetGuestCpuIdFeature().
1075 */
1076 if ( pNewLeaf->uLeaf == UINT32_C(0x00000000) /* Standard */
1077 || pNewLeaf->uLeaf == UINT32_C(0x00000001)
1078 || pNewLeaf->uLeaf == UINT32_C(0x80000000) /* Extended */
1079 || pNewLeaf->uLeaf == UINT32_C(0x80000001)
1080 || pNewLeaf->uLeaf == UINT32_C(0xc0000000) /* Centaur */
1081 || pNewLeaf->uLeaf == UINT32_C(0xc0000001) )
1082 {
1083 return VERR_NOT_SUPPORTED;
1084 }
1085
1086 return cpumR3CpuIdInsert(pVM, NULL /* ppaLeaves */, NULL /* pcLeaves */, pNewLeaf);
1087}
1088
1089/**
1090 * Collects CPUID leaves and sub-leaves, returning a sorted array of them.
1091 *
1092 * @returns VBox status code.
1093 * @param ppaLeaves Where to return the array pointer on success.
1094 * Use RTMemFree to release.
1095 * @param pcLeaves Where to return the size of the array on
1096 * success.
1097 */
1098VMMR3DECL(int) CPUMR3CpuIdCollectLeaves(PCPUMCPUIDLEAF *ppaLeaves, uint32_t *pcLeaves)
1099{
1100 *ppaLeaves = NULL;
1101 *pcLeaves = 0;
1102
1103 /*
1104 * Try out various candidates. This must be sorted!
1105 */
1106 static struct { uint32_t uMsr; bool fSpecial; } const s_aCandidates[] =
1107 {
1108 { UINT32_C(0x00000000), false },
1109 { UINT32_C(0x10000000), false },
1110 { UINT32_C(0x20000000), false },
1111 { UINT32_C(0x30000000), false },
1112 { UINT32_C(0x40000000), false },
1113 { UINT32_C(0x50000000), false },
1114 { UINT32_C(0x60000000), false },
1115 { UINT32_C(0x70000000), false },
1116 { UINT32_C(0x80000000), false },
1117 { UINT32_C(0x80860000), false },
1118 { UINT32_C(0x8ffffffe), true },
1119 { UINT32_C(0x8fffffff), true },
1120 { UINT32_C(0x90000000), false },
1121 { UINT32_C(0xa0000000), false },
1122 { UINT32_C(0xb0000000), false },
1123 { UINT32_C(0xc0000000), false },
1124 { UINT32_C(0xd0000000), false },
1125 { UINT32_C(0xe0000000), false },
1126 { UINT32_C(0xf0000000), false },
1127 };
1128
1129 for (uint32_t iOuter = 0; iOuter < RT_ELEMENTS(s_aCandidates); iOuter++)
1130 {
1131 uint32_t uLeaf = s_aCandidates[iOuter].uMsr;
1132 uint32_t uEax, uEbx, uEcx, uEdx;
1133 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &uEax, &uEbx, &uEcx, &uEdx);
1134
1135 /*
1136 * Does EAX look like a typical leaf count value?
1137 */
1138 if ( uEax > uLeaf
1139 && uEax - uLeaf < UINT32_C(0xff)) /* Adjust 0xff limit when exceeded by real HW. */
1140 {
1141 /* Yes, dump them. */
1142 uint32_t cLeaves = uEax - uLeaf + 1;
1143 while (cLeaves-- > 0)
1144 {
1145 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &uEax, &uEbx, &uEcx, &uEdx);
1146
1147 uint32_t fFlags = 0;
1148
1149 /* There are currently three known leaves containing an APIC ID
1150 that needs EMT specific attention */
1151 if (uLeaf == 1)
1152 fFlags |= CPUMCPUIDLEAF_F_CONTAINS_APIC_ID;
1153 else if (uLeaf == 0xb && uEcx != 0)
1154 fFlags |= CPUMCPUIDLEAF_F_CONTAINS_APIC_ID;
1155 else if ( uLeaf == UINT32_C(0x8000001e)
1156 && ( uEax
1157 || uEbx
1158 || uEdx
1159 || ASMIsAmdCpuEx((*ppaLeaves)[0].uEbx, (*ppaLeaves)[0].uEcx, (*ppaLeaves)[0].uEdx)) )
1160 fFlags |= CPUMCPUIDLEAF_F_CONTAINS_APIC_ID;
1161
1162
1163 /* Check three times here to reduce the chance of CPU migration
1164 resulting in false positives with things like the APIC ID. */
1165 uint32_t cSubLeaves;
1166 bool fFinalEcxUnchanged;
1167 if ( cpumR3IsEcxRelevantForCpuIdLeaf(uLeaf, &cSubLeaves, &fFinalEcxUnchanged)
1168 && cpumR3IsEcxRelevantForCpuIdLeaf(uLeaf, &cSubLeaves, &fFinalEcxUnchanged)
1169 && cpumR3IsEcxRelevantForCpuIdLeaf(uLeaf, &cSubLeaves, &fFinalEcxUnchanged))
1170 {
1171 if (cSubLeaves > (uLeaf == 0xd ? 68U : 16U))
1172 {
1173 /* This shouldn't happen. But in case it does, file all
1174 relevant details in the release log. */
1175 LogRel(("CPUM: VERR_CPUM_TOO_MANY_CPUID_SUBLEAVES! uLeaf=%#x cSubLeaves=%#x\n", uLeaf, cSubLeaves));
1176 LogRel(("------------------ dump of problematic sub-leaves -----------------\n"));
1177 for (uint32_t uSubLeaf = 0; uSubLeaf < 128; uSubLeaf++)
1178 {
1179 uint32_t auTmp[4];
1180 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &auTmp[0], &auTmp[1], &auTmp[2], &auTmp[3]);
1181 LogRel(("CPUM: %#010x, %#010x => %#010x %#010x %#010x %#010x\n",
1182 uLeaf, uSubLeaf, auTmp[0], auTmp[1], auTmp[2], auTmp[3]));
1183 }
1184 LogRel(("----------------- dump of what we've found so far -----------------\n"));
1185 for (uint32_t i = 0 ; i < *pcLeaves; i++)
1186 LogRel(("CPUM: %#010x, %#010x/%#010x => %#010x %#010x %#010x %#010x\n",
1187 (*ppaLeaves)[i].uLeaf, (*ppaLeaves)[i].uSubLeaf, (*ppaLeaves)[i].fSubLeafMask,
1188 (*ppaLeaves)[i].uEax, (*ppaLeaves)[i].uEbx, (*ppaLeaves)[i].uEcx, (*ppaLeaves)[i].uEdx));
1189 LogRel(("\nPlease create a defect on virtualbox.org and attach this log file!\n\n"));
1190 return VERR_CPUM_TOO_MANY_CPUID_SUBLEAVES;
1191 }
1192
1193 if (fFinalEcxUnchanged)
1194 fFlags |= CPUMCPUIDLEAF_F_INTEL_TOPOLOGY_SUBLEAVES;
1195
1196 for (uint32_t uSubLeaf = 0; uSubLeaf < cSubLeaves; uSubLeaf++)
1197 {
1198 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &uEax, &uEbx, &uEcx, &uEdx);
1199 int rc = cpumR3CollectCpuIdInfoAddOne(ppaLeaves, pcLeaves,
1200 uLeaf, uSubLeaf, UINT32_MAX, uEax, uEbx, uEcx, uEdx, fFlags);
1201 if (RT_FAILURE(rc))
1202 return rc;
1203 }
1204 }
1205 else
1206 {
1207 int rc = cpumR3CollectCpuIdInfoAddOne(ppaLeaves, pcLeaves,
1208 uLeaf, 0, 0, uEax, uEbx, uEcx, uEdx, fFlags);
1209 if (RT_FAILURE(rc))
1210 return rc;
1211 }
1212
1213 /* next */
1214 uLeaf++;
1215 }
1216 }
1217 /*
1218 * Special CPUIDs needs special handling as they don't follow the
1219 * leaf count principle used above.
1220 */
1221 else if (s_aCandidates[iOuter].fSpecial)
1222 {
1223 bool fKeep = false;
1224 if (uLeaf == 0x8ffffffe && uEax == UINT32_C(0x00494544))
1225 fKeep = true;
1226 else if ( uLeaf == 0x8fffffff
1227 && RT_C_IS_PRINT(RT_BYTE1(uEax))
1228 && RT_C_IS_PRINT(RT_BYTE2(uEax))
1229 && RT_C_IS_PRINT(RT_BYTE3(uEax))
1230 && RT_C_IS_PRINT(RT_BYTE4(uEax))
1231 && RT_C_IS_PRINT(RT_BYTE1(uEbx))
1232 && RT_C_IS_PRINT(RT_BYTE2(uEbx))
1233 && RT_C_IS_PRINT(RT_BYTE3(uEbx))
1234 && RT_C_IS_PRINT(RT_BYTE4(uEbx))
1235 && RT_C_IS_PRINT(RT_BYTE1(uEcx))
1236 && RT_C_IS_PRINT(RT_BYTE2(uEcx))
1237 && RT_C_IS_PRINT(RT_BYTE3(uEcx))
1238 && RT_C_IS_PRINT(RT_BYTE4(uEcx))
1239 && RT_C_IS_PRINT(RT_BYTE1(uEdx))
1240 && RT_C_IS_PRINT(RT_BYTE2(uEdx))
1241 && RT_C_IS_PRINT(RT_BYTE3(uEdx))
1242 && RT_C_IS_PRINT(RT_BYTE4(uEdx)) )
1243 fKeep = true;
1244 if (fKeep)
1245 {
1246 int rc = cpumR3CollectCpuIdInfoAddOne(ppaLeaves, pcLeaves,
1247 uLeaf, 0, 0, uEax, uEbx, uEcx, uEdx, 0);
1248 if (RT_FAILURE(rc))
1249 return rc;
1250 }
1251 }
1252 }
1253
1254 cpumR3CpuIdAssertOrder(*ppaLeaves, *pcLeaves);
1255 return VINF_SUCCESS;
1256}
1257
1258
1259/**
1260 * Determines the method the CPU uses to handle unknown CPUID leaves.
1261 *
1262 * @returns VBox status code.
1263 * @param penmUnknownMethod Where to return the method.
1264 * @param pDefUnknown Where to return default unknown values. This
1265 * will be set, even if the resulting method
1266 * doesn't actually needs it.
1267 */
1268VMMR3DECL(int) CPUMR3CpuIdDetectUnknownLeafMethod(PCPUMUNKNOWNCPUID penmUnknownMethod, PCPUMCPUID pDefUnknown)
1269{
1270 uint32_t uLastStd = ASMCpuId_EAX(0);
1271 uint32_t uLastExt = ASMCpuId_EAX(0x80000000);
1272 if (!ASMIsValidExtRange(uLastExt))
1273 uLastExt = 0x80000000;
1274
1275 uint32_t auChecks[] =
1276 {
1277 uLastStd + 1,
1278 uLastStd + 5,
1279 uLastStd + 8,
1280 uLastStd + 32,
1281 uLastStd + 251,
1282 uLastExt + 1,
1283 uLastExt + 8,
1284 uLastExt + 15,
1285 uLastExt + 63,
1286 uLastExt + 255,
1287 0x7fbbffcc,
1288 0x833f7872,
1289 0xefff2353,
1290 0x35779456,
1291 0x1ef6d33e,
1292 };
1293
1294 static const uint32_t s_auValues[] =
1295 {
1296 0xa95d2156,
1297 0x00000001,
1298 0x00000002,
1299 0x00000008,
1300 0x00000000,
1301 0x55773399,
1302 0x93401769,
1303 0x12039587,
1304 };
1305
1306 /*
1307 * Simple method, all zeros.
1308 */
1309 *penmUnknownMethod = CPUMUNKNOWNCPUID_DEFAULTS;
1310 pDefUnknown->uEax = 0;
1311 pDefUnknown->uEbx = 0;
1312 pDefUnknown->uEcx = 0;
1313 pDefUnknown->uEdx = 0;
1314
1315 /*
1316 * Intel has been observed returning the last standard leaf.
1317 */
1318 uint32_t auLast[4];
1319 ASMCpuIdExSlow(uLastStd, 0, 0, 0, &auLast[0], &auLast[1], &auLast[2], &auLast[3]);
1320
1321 uint32_t cChecks = RT_ELEMENTS(auChecks);
1322 while (cChecks > 0)
1323 {
1324 uint32_t auCur[4];
1325 ASMCpuIdExSlow(auChecks[cChecks - 1], 0, 0, 0, &auCur[0], &auCur[1], &auCur[2], &auCur[3]);
1326 if (memcmp(auCur, auLast, sizeof(auCur)))
1327 break;
1328 cChecks--;
1329 }
1330 if (cChecks == 0)
1331 {
1332 /* Now, what happens when the input changes? Esp. ECX. */
1333 uint32_t cTotal = 0;
1334 uint32_t cSame = 0;
1335 uint32_t cLastWithEcx = 0;
1336 uint32_t cNeither = 0;
1337 uint32_t cValues = RT_ELEMENTS(s_auValues);
1338 while (cValues > 0)
1339 {
1340 uint32_t uValue = s_auValues[cValues - 1];
1341 uint32_t auLastWithEcx[4];
1342 ASMCpuIdExSlow(uLastStd, uValue, uValue, uValue,
1343 &auLastWithEcx[0], &auLastWithEcx[1], &auLastWithEcx[2], &auLastWithEcx[3]);
1344
1345 cChecks = RT_ELEMENTS(auChecks);
1346 while (cChecks > 0)
1347 {
1348 uint32_t auCur[4];
1349 ASMCpuIdExSlow(auChecks[cChecks - 1], uValue, uValue, uValue, &auCur[0], &auCur[1], &auCur[2], &auCur[3]);
1350 if (!memcmp(auCur, auLast, sizeof(auCur)))
1351 {
1352 cSame++;
1353 if (!memcmp(auCur, auLastWithEcx, sizeof(auCur)))
1354 cLastWithEcx++;
1355 }
1356 else if (!memcmp(auCur, auLastWithEcx, sizeof(auCur)))
1357 cLastWithEcx++;
1358 else
1359 cNeither++;
1360 cTotal++;
1361 cChecks--;
1362 }
1363 cValues--;
1364 }
1365
1366 Log(("CPUM: cNeither=%d cSame=%d cLastWithEcx=%d cTotal=%d\n", cNeither, cSame, cLastWithEcx, cTotal));
1367 if (cSame == cTotal)
1368 *penmUnknownMethod = CPUMUNKNOWNCPUID_LAST_STD_LEAF;
1369 else if (cLastWithEcx == cTotal)
1370 *penmUnknownMethod = CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX;
1371 else
1372 *penmUnknownMethod = CPUMUNKNOWNCPUID_LAST_STD_LEAF;
1373 pDefUnknown->uEax = auLast[0];
1374 pDefUnknown->uEbx = auLast[1];
1375 pDefUnknown->uEcx = auLast[2];
1376 pDefUnknown->uEdx = auLast[3];
1377 return VINF_SUCCESS;
1378 }
1379
1380 /*
1381 * Unchanged register values?
1382 */
1383 cChecks = RT_ELEMENTS(auChecks);
1384 while (cChecks > 0)
1385 {
1386 uint32_t const uLeaf = auChecks[cChecks - 1];
1387 uint32_t cValues = RT_ELEMENTS(s_auValues);
1388 while (cValues > 0)
1389 {
1390 uint32_t uValue = s_auValues[cValues - 1];
1391 uint32_t auCur[4];
1392 ASMCpuIdExSlow(uLeaf, uValue, uValue, uValue, &auCur[0], &auCur[1], &auCur[2], &auCur[3]);
1393 if ( auCur[0] != uLeaf
1394 || auCur[1] != uValue
1395 || auCur[2] != uValue
1396 || auCur[3] != uValue)
1397 break;
1398 cValues--;
1399 }
1400 if (cValues != 0)
1401 break;
1402 cChecks--;
1403 }
1404 if (cChecks == 0)
1405 {
1406 *penmUnknownMethod = CPUMUNKNOWNCPUID_PASSTHRU;
1407 return VINF_SUCCESS;
1408 }
1409
1410 /*
1411 * Just go with the simple method.
1412 */
1413 return VINF_SUCCESS;
1414}
1415
1416
1417/**
1418 * Translates a unknow CPUID leaf method into the constant name (sans prefix).
1419 *
1420 * @returns Read only name string.
1421 * @param enmUnknownMethod The method to translate.
1422 */
1423VMMR3DECL(const char *) CPUMR3CpuIdUnknownLeafMethodName(CPUMUNKNOWNCPUID enmUnknownMethod)
1424{
1425 switch (enmUnknownMethod)
1426 {
1427 case CPUMUNKNOWNCPUID_DEFAULTS: return "DEFAULTS";
1428 case CPUMUNKNOWNCPUID_LAST_STD_LEAF: return "LAST_STD_LEAF";
1429 case CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX: return "LAST_STD_LEAF_WITH_ECX";
1430 case CPUMUNKNOWNCPUID_PASSTHRU: return "PASSTHRU";
1431
1432 case CPUMUNKNOWNCPUID_INVALID:
1433 case CPUMUNKNOWNCPUID_END:
1434 case CPUMUNKNOWNCPUID_32BIT_HACK:
1435 break;
1436 }
1437 return "Invalid-unknown-CPUID-method";
1438}
1439
1440
1441/**
1442 * Detect the CPU vendor give n the
1443 *
1444 * @returns The vendor.
1445 * @param uEAX EAX from CPUID(0).
1446 * @param uEBX EBX from CPUID(0).
1447 * @param uECX ECX from CPUID(0).
1448 * @param uEDX EDX from CPUID(0).
1449 */
1450VMMR3DECL(CPUMCPUVENDOR) CPUMR3CpuIdDetectVendorEx(uint32_t uEAX, uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
1451{
1452 if (ASMIsValidStdRange(uEAX))
1453 {
1454 if (ASMIsAmdCpuEx(uEBX, uECX, uEDX))
1455 return CPUMCPUVENDOR_AMD;
1456
1457 if (ASMIsIntelCpuEx(uEBX, uECX, uEDX))
1458 return CPUMCPUVENDOR_INTEL;
1459
1460 if (ASMIsViaCentaurCpuEx(uEBX, uECX, uEDX))
1461 return CPUMCPUVENDOR_VIA;
1462
1463 if ( uEBX == UINT32_C(0x69727943) /* CyrixInstead */
1464 && uECX == UINT32_C(0x64616574)
1465 && uEDX == UINT32_C(0x736E4978))
1466 return CPUMCPUVENDOR_CYRIX;
1467
1468 /* "Geode by NSC", example: family 5, model 9. */
1469
1470 /** @todo detect the other buggers... */
1471 }
1472
1473 return CPUMCPUVENDOR_UNKNOWN;
1474}
1475
1476
1477/**
1478 * Translates a CPU vendor enum value into the corresponding string constant.
1479 *
1480 * The named can be prefixed with 'CPUMCPUVENDOR_' to construct a valid enum
1481 * value name. This can be useful when generating code.
1482 *
1483 * @returns Read only name string.
1484 * @param enmVendor The CPU vendor value.
1485 */
1486VMMR3DECL(const char *) CPUMR3CpuVendorName(CPUMCPUVENDOR enmVendor)
1487{
1488 switch (enmVendor)
1489 {
1490 case CPUMCPUVENDOR_INTEL: return "INTEL";
1491 case CPUMCPUVENDOR_AMD: return "AMD";
1492 case CPUMCPUVENDOR_VIA: return "VIA";
1493 case CPUMCPUVENDOR_CYRIX: return "CYRIX";
1494 case CPUMCPUVENDOR_UNKNOWN: return "UNKNOWN";
1495
1496 case CPUMCPUVENDOR_INVALID:
1497 case CPUMCPUVENDOR_32BIT_HACK:
1498 break;
1499 }
1500 return "Invalid-cpu-vendor";
1501}
1502
1503
1504static PCCPUMCPUIDLEAF cpumR3CpuIdFindLeaf(PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, uint32_t uLeaf)
1505{
1506 /* Could do binary search, doing linear now because I'm lazy. */
1507 PCCPUMCPUIDLEAF pLeaf = paLeaves;
1508 while (cLeaves-- > 0)
1509 {
1510 if (pLeaf->uLeaf == uLeaf)
1511 return pLeaf;
1512 pLeaf++;
1513 }
1514 return NULL;
1515}
1516
1517
1518static PCCPUMCPUIDLEAF cpumR3CpuIdFindLeafEx(PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, uint32_t uLeaf, uint32_t uSubLeaf)
1519{
1520 PCCPUMCPUIDLEAF pLeaf = cpumR3CpuIdFindLeaf(paLeaves, cLeaves, uLeaf);
1521 if ( !pLeaf
1522 || pLeaf->uSubLeaf != (uSubLeaf & pLeaf->fSubLeafMask))
1523 return pLeaf;
1524
1525 /* Linear sub-leaf search. Lazy as usual. */
1526 cLeaves -= pLeaf - paLeaves;
1527 while ( cLeaves-- > 0
1528 && pLeaf->uLeaf == uLeaf)
1529 {
1530 if (pLeaf->uSubLeaf == (uSubLeaf & pLeaf->fSubLeafMask))
1531 return pLeaf;
1532 pLeaf++;
1533 }
1534
1535 return NULL;
1536}
1537
1538
1539int cpumR3CpuIdExplodeFeatures(PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, PCPUMFEATURES pFeatures)
1540{
1541 RT_ZERO(*pFeatures);
1542 if (cLeaves >= 2)
1543 {
1544 AssertLogRelReturn(paLeaves[0].uLeaf == 0, VERR_CPUM_IPE_1);
1545 AssertLogRelReturn(paLeaves[1].uLeaf == 1, VERR_CPUM_IPE_1);
1546 PCCPUMCPUIDLEAF const pStd0Leaf = cpumR3CpuIdFindLeafEx(paLeaves, cLeaves, 0, 0);
1547 AssertLogRelReturn(pStd0Leaf, VERR_CPUM_IPE_1);
1548 PCCPUMCPUIDLEAF const pStd1Leaf = cpumR3CpuIdFindLeafEx(paLeaves, cLeaves, 1, 0);
1549 AssertLogRelReturn(pStd1Leaf, VERR_CPUM_IPE_1);
1550
1551 pFeatures->enmCpuVendor = CPUMR3CpuIdDetectVendorEx(pStd0Leaf->uEax,
1552 pStd0Leaf->uEbx,
1553 pStd0Leaf->uEcx,
1554 pStd0Leaf->uEdx);
1555 pFeatures->uFamily = ASMGetCpuFamily(pStd1Leaf->uEax);
1556 pFeatures->uModel = ASMGetCpuModel(pStd1Leaf->uEax, pFeatures->enmCpuVendor == CPUMCPUVENDOR_INTEL);
1557 pFeatures->uStepping = ASMGetCpuStepping(pStd1Leaf->uEax);
1558 pFeatures->enmMicroarch = CPUMR3CpuIdDetermineMicroarchEx((CPUMCPUVENDOR)pFeatures->enmCpuVendor,
1559 pFeatures->uFamily,
1560 pFeatures->uModel,
1561 pFeatures->uStepping);
1562
1563 PCCPUMCPUIDLEAF pLeaf = cpumR3CpuIdFindLeaf(paLeaves, cLeaves, 0x80000008);
1564 if (pLeaf)
1565 pFeatures->cMaxPhysAddrWidth = pLeaf->uEax & 0xff;
1566 else if (pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_PSE36)
1567 pFeatures->cMaxPhysAddrWidth = 36;
1568 else
1569 pFeatures->cMaxPhysAddrWidth = 32;
1570
1571 /* Standard features. */
1572 pFeatures->fMsr = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_MSR);
1573 pFeatures->fApic = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_APIC);
1574 pFeatures->fX2Apic = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_X2APIC);
1575 pFeatures->fPse = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_PSE);
1576 pFeatures->fPse36 = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_PSE36);
1577 pFeatures->fPae = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_PAE);
1578 pFeatures->fPat = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_PAT);
1579 pFeatures->fFxSaveRstor = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_FXSR);
1580 pFeatures->fXSaveRstor = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_XSAVE);
1581 pFeatures->fOpSysXSaveRstor = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_OSXSAVE);
1582 pFeatures->fMmx = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_MMX);
1583 pFeatures->fSse = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_SSE);
1584 pFeatures->fSse2 = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_SSE2);
1585 pFeatures->fSse3 = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_SSE3);
1586 pFeatures->fSsse3 = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_SSSE3);
1587 pFeatures->fSse41 = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_SSE4_1);
1588 pFeatures->fSse42 = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_SSE4_2);
1589 pFeatures->fAvx = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_AVX);
1590 pFeatures->fTsc = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_TSC);
1591 pFeatures->fSysEnter = RT_BOOL(pStd1Leaf->uEdx & X86_CPUID_FEATURE_EDX_SEP);
1592 pFeatures->fHypervisorPresent = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_HVP);
1593 pFeatures->fMonitorMWait = RT_BOOL(pStd1Leaf->uEcx & X86_CPUID_FEATURE_ECX_MONITOR);
1594
1595 /* Structured extended features. */
1596 PCCPUMCPUIDLEAF const pSxfLeaf0 = cpumR3CpuIdFindLeafEx(paLeaves, cLeaves, 7, 0);
1597 if (pSxfLeaf0)
1598 {
1599 pFeatures->fAvx2 = RT_BOOL(pSxfLeaf0->uEcx & X86_CPUID_STEXT_FEATURE_EBX_AVX2);
1600 pFeatures->fAvx512Foundation = RT_BOOL(pSxfLeaf0->uEcx & X86_CPUID_STEXT_FEATURE_EBX_AVX512F);
1601 }
1602
1603 /* MWAIT/MONITOR leaf. */
1604 PCCPUMCPUIDLEAF const pMWaitLeaf = cpumR3CpuIdFindLeaf(paLeaves, cLeaves, 5);
1605 if (pMWaitLeaf)
1606 {
1607 pFeatures->fMWaitExtensions = (pMWaitLeaf->uEcx & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
1608 == (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0);
1609 }
1610
1611 /* Extended features. */
1612 PCCPUMCPUIDLEAF const pExtLeaf = cpumR3CpuIdFindLeaf(paLeaves, cLeaves, 0x80000001);
1613 if (pExtLeaf)
1614 {
1615 pFeatures->fLongMode = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE);
1616 pFeatures->fSysCall = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_EXT_FEATURE_EDX_SYSCALL);
1617 pFeatures->fNoExecute = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_EXT_FEATURE_EDX_NX);
1618 pFeatures->fLahfSahf = RT_BOOL(pExtLeaf->uEcx & X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF);
1619 pFeatures->fRdTscP = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_EXT_FEATURE_EDX_RDTSCP);
1620 pFeatures->fMovCr8In32Bit = RT_BOOL(pExtLeaf->uEcx & X86_CPUID_AMD_FEATURE_ECX_CMPL);
1621 pFeatures->f3DNow = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_3DNOW);
1622 pFeatures->f3DNowPrefetch = (pExtLeaf->uEcx & X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF)
1623 || (pExtLeaf->uEdx & ( X86_CPUID_EXT_FEATURE_EDX_LONG_MODE
1624 | X86_CPUID_AMD_FEATURE_EDX_3DNOW));
1625 }
1626
1627 if ( pExtLeaf
1628 && pFeatures->enmCpuVendor == CPUMCPUVENDOR_AMD)
1629 {
1630 /* AMD features. */
1631 pFeatures->fMsr |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_MSR);
1632 pFeatures->fApic |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_APIC);
1633 pFeatures->fPse |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_PSE);
1634 pFeatures->fPse36 |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_PSE36);
1635 pFeatures->fPae |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_PAE);
1636 pFeatures->fPat |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_PAT);
1637 pFeatures->fFxSaveRstor |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_FXSR);
1638 pFeatures->fMmx |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_MMX);
1639 pFeatures->fTsc |= RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_TSC);
1640 pFeatures->fAmdMmxExts = RT_BOOL(pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_AXMMX);
1641 }
1642
1643 /*
1644 * Quirks.
1645 */
1646 pFeatures->fLeakyFxSR = pExtLeaf
1647 && (pExtLeaf->uEdx & X86_CPUID_AMD_FEATURE_EDX_FFXSR)
1648 && pFeatures->enmCpuVendor == CPUMCPUVENDOR_AMD
1649 && pFeatures->uFamily >= 6 /* K7 and up */;
1650
1651 /*
1652 * Max extended (/FPU) state.
1653 */
1654 pFeatures->cbMaxExtendedState = pFeatures->fFxSaveRstor ? sizeof(X86FXSTATE) : sizeof(X86FPUSTATE);
1655 if (pFeatures->fXSaveRstor)
1656 {
1657 PCCPUMCPUIDLEAF const pXStateLeaf0 = cpumR3CpuIdFindLeafEx(paLeaves, cLeaves, 13, 0);
1658 if (pXStateLeaf0)
1659 {
1660 if ( pXStateLeaf0->uEcx >= sizeof(X86FXSTATE)
1661 && pXStateLeaf0->uEcx <= CPUM_MAX_XSAVE_AREA_SIZE
1662 && RT_ALIGN_32(pXStateLeaf0->uEcx, 8) == pXStateLeaf0->uEcx
1663 && pXStateLeaf0->uEbx >= sizeof(X86FXSTATE)
1664 && pXStateLeaf0->uEbx <= pXStateLeaf0->uEcx
1665 && RT_ALIGN_32(pXStateLeaf0->uEbx, 8) == pXStateLeaf0->uEbx)
1666 {
1667 pFeatures->cbMaxExtendedState = pXStateLeaf0->uEcx;
1668
1669 PCCPUMCPUIDLEAF const pXStateLeaf1 = cpumR3CpuIdFindLeafEx(paLeaves, cLeaves, 13, 1);
1670 if ( pXStateLeaf1
1671 && pXStateLeaf1->uEbx > pFeatures->cbMaxExtendedState
1672 && pXStateLeaf1->uEbx <= CPUM_MAX_XSAVE_AREA_SIZE
1673 && (pXStateLeaf1->uEcx || pXStateLeaf1->uEdx) )
1674 pFeatures->cbMaxExtendedState = pXStateLeaf0->uEbx;
1675 }
1676 else
1677 AssertLogRelMsgFailedStmt(("Unexpected max/cur XSAVE area sizes: %#x/%#x\n", pXStateLeaf0->uEcx, pXStateLeaf0->uEbx),
1678 pFeatures->fXSaveRstor = 0);
1679 }
1680 else
1681 AssertLogRelMsgFailedStmt(("Expected leaf eax=0xd/ecx=0 with the XSAVE/XRSTOR feature!\n"),
1682 pFeatures->fXSaveRstor = 0);
1683 }
1684 }
1685 else
1686 AssertLogRelReturn(cLeaves == 0, VERR_CPUM_IPE_1);
1687 return VINF_SUCCESS;
1688}
1689
1690
1691/*
1692 *
1693 * Init related code.
1694 * Init related code.
1695 * Init related code.
1696 *
1697 *
1698 */
1699#ifdef VBOX_IN_VMM
1700
1701
1702/**
1703 * Gets an exactly matching leaf + sub-leaf in the CPUID leaf array.
1704 *
1705 * This ignores the fSubLeafMask.
1706 *
1707 * @returns Pointer to the matching leaf, or NULL if not found.
1708 * @param paLeaves The CPUID leaves to search. This is sorted.
1709 * @param cLeaves The number of leaves in the array.
1710 * @param uLeaf The leaf to locate.
1711 * @param uSubLeaf The subleaf to locate.
1712 */
1713static PCPUMCPUIDLEAF cpumR3CpuIdGetExactLeaf(PCPUM pCpum, uint32_t uLeaf, uint32_t uSubLeaf)
1714{
1715 uint64_t uNeedle = RT_MAKE_U64(uSubLeaf, uLeaf);
1716 PCPUMCPUIDLEAF paLeaves = pCpum->GuestInfo.paCpuIdLeavesR3;
1717 uint32_t iEnd = pCpum->GuestInfo.cCpuIdLeaves;
1718 if (iEnd)
1719 {
1720 uint32_t iBegin = 0;
1721 for (;;)
1722 {
1723 uint32_t const i = (iEnd - iBegin) / 2 + iBegin;
1724 uint64_t const uCur = RT_MAKE_U64(paLeaves[i].uSubLeaf, paLeaves[i].uLeaf);
1725 if (uNeedle < uCur)
1726 {
1727 if (i > iBegin)
1728 iEnd = i;
1729 else
1730 break;
1731 }
1732 else if (uNeedle > uCur)
1733 {
1734 if (i + 1 < iEnd)
1735 iBegin = i + 1;
1736 else
1737 break;
1738 }
1739 else
1740 return &paLeaves[i];
1741 }
1742 }
1743 return NULL;
1744}
1745
1746
1747/**
1748 * Loads MSR range overrides.
1749 *
1750 * This must be called before the MSR ranges are moved from the normal heap to
1751 * the hyper heap!
1752 *
1753 * @returns VBox status code (VMSetError called).
1754 * @param pVM Pointer to the cross context VM structure
1755 * @param pMsrNode The CFGM node with the MSR overrides.
1756 */
1757static int cpumR3LoadMsrOverrides(PVM pVM, PCFGMNODE pMsrNode)
1758{
1759 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pMsrNode); pNode; pNode = CFGMR3GetNextChild(pNode))
1760 {
1761 /*
1762 * Assemble a valid MSR range.
1763 */
1764 CPUMMSRRANGE MsrRange;
1765 MsrRange.offCpumCpu = 0;
1766 MsrRange.fReserved = 0;
1767
1768 int rc = CFGMR3GetName(pNode, MsrRange.szName, sizeof(MsrRange.szName));
1769 if (RT_FAILURE(rc))
1770 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry (name is probably too long): %Rrc\n", rc);
1771
1772 rc = CFGMR3QueryU32(pNode, "First", &MsrRange.uFirst);
1773 if (RT_FAILURE(rc))
1774 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying mandatory 'First' value: %Rrc\n",
1775 MsrRange.szName, rc);
1776
1777 rc = CFGMR3QueryU32Def(pNode, "Last", &MsrRange.uLast, MsrRange.uFirst);
1778 if (RT_FAILURE(rc))
1779 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying 'Last' value: %Rrc\n",
1780 MsrRange.szName, rc);
1781
1782 char szType[32];
1783 rc = CFGMR3QueryStringDef(pNode, "Type", szType, sizeof(szType), "FixedValue");
1784 if (RT_FAILURE(rc))
1785 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying 'Type' value: %Rrc\n",
1786 MsrRange.szName, rc);
1787 if (!RTStrICmp(szType, "FixedValue"))
1788 {
1789 MsrRange.enmRdFn = kCpumMsrRdFn_FixedValue;
1790 MsrRange.enmWrFn = kCpumMsrWrFn_IgnoreWrite;
1791
1792 rc = CFGMR3QueryU64Def(pNode, "Value", &MsrRange.uValue, 0);
1793 if (RT_FAILURE(rc))
1794 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying 'Value' value: %Rrc\n",
1795 MsrRange.szName, rc);
1796
1797 rc = CFGMR3QueryU64Def(pNode, "WrGpMask", &MsrRange.fWrGpMask, 0);
1798 if (RT_FAILURE(rc))
1799 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying 'WrGpMask' value: %Rrc\n",
1800 MsrRange.szName, rc);
1801
1802 rc = CFGMR3QueryU64Def(pNode, "WrIgnMask", &MsrRange.fWrIgnMask, 0);
1803 if (RT_FAILURE(rc))
1804 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid MSR entry '%s': Error querying 'WrIgnMask' value: %Rrc\n",
1805 MsrRange.szName, rc);
1806 }
1807 else
1808 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1809 "Invalid MSR entry '%s': Unknown type '%s'\n", MsrRange.szName, szType);
1810
1811 /*
1812 * Insert the range into the table (replaces/splits/shrinks existing
1813 * MSR ranges).
1814 */
1815 rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
1816 &MsrRange);
1817 if (RT_FAILURE(rc))
1818 return VMSetError(pVM, rc, RT_SRC_POS, "Error adding MSR entry '%s': %Rrc\n", MsrRange.szName, rc);
1819 }
1820
1821 return VINF_SUCCESS;
1822}
1823
1824
1825/**
1826 * Loads CPUID leaf overrides.
1827 *
1828 * This must be called before the CPUID leaves are moved from the normal
1829 * heap to the hyper heap!
1830 *
1831 * @returns VBox status code (VMSetError called).
1832 * @param pVM Pointer to the cross context VM structure
1833 * @param pParentNode The CFGM node with the CPUID leaves.
1834 * @param pszLabel How to label the overrides we're loading.
1835 */
1836static int cpumR3LoadCpuIdOverrides(PVM pVM, PCFGMNODE pParentNode, const char *pszLabel)
1837{
1838 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pParentNode); pNode; pNode = CFGMR3GetNextChild(pNode))
1839 {
1840 /*
1841 * Get the leaf and subleaf numbers.
1842 */
1843 char szName[128];
1844 int rc = CFGMR3GetName(pNode, szName, sizeof(szName));
1845 if (RT_FAILURE(rc))
1846 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry (name is probably too long): %Rrc\n", pszLabel, rc);
1847
1848 /* The leaf number is either specified directly or thru the node name. */
1849 uint32_t uLeaf;
1850 rc = CFGMR3QueryU32(pNode, "Leaf", &uLeaf);
1851 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1852 {
1853 rc = RTStrToUInt32Full(szName, 16, &uLeaf);
1854 if (rc != VINF_SUCCESS)
1855 return VMSetError(pVM, VERR_INVALID_NAME, RT_SRC_POS,
1856 "Invalid %s entry: Invalid leaf number: '%s' \n", pszLabel, szName);
1857 }
1858 else if (RT_FAILURE(rc))
1859 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'Leaf' value: %Rrc\n",
1860 pszLabel, szName, rc);
1861
1862 uint32_t uSubLeaf;
1863 rc = CFGMR3QueryU32Def(pNode, "SubLeaf", &uSubLeaf, 0);
1864 if (RT_FAILURE(rc))
1865 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'SubLeaf' value: %Rrc\n",
1866 pszLabel, szName, rc);
1867
1868 uint32_t fSubLeafMask;
1869 rc = CFGMR3QueryU32Def(pNode, "SubLeafMask", &fSubLeafMask, 0);
1870 if (RT_FAILURE(rc))
1871 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'SubLeafMask' value: %Rrc\n",
1872 pszLabel, szName, rc);
1873
1874 /*
1875 * Look up the specified leaf, since the output register values
1876 * defaults to any existing values. This allows overriding a single
1877 * register, without needing to know the other values.
1878 */
1879 PCCPUMCPUIDLEAF pLeaf = cpumR3CpuIdGetExactLeaf(&pVM->cpum.s, uLeaf, uSubLeaf);
1880 CPUMCPUIDLEAF Leaf;
1881 if (pLeaf)
1882 Leaf = *pLeaf;
1883 else
1884 RT_ZERO(Leaf);
1885 Leaf.uLeaf = uLeaf;
1886 Leaf.uSubLeaf = uSubLeaf;
1887 Leaf.fSubLeafMask = fSubLeafMask;
1888
1889 rc = CFGMR3QueryU32Def(pNode, "eax", &Leaf.uEax, Leaf.uEax);
1890 if (RT_FAILURE(rc))
1891 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'eax' value: %Rrc\n",
1892 pszLabel, szName, rc);
1893 rc = CFGMR3QueryU32Def(pNode, "ebx", &Leaf.uEbx, Leaf.uEbx);
1894 if (RT_FAILURE(rc))
1895 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'ebx' value: %Rrc\n",
1896 pszLabel, szName, rc);
1897 rc = CFGMR3QueryU32Def(pNode, "ecx", &Leaf.uEcx, Leaf.uEcx);
1898 if (RT_FAILURE(rc))
1899 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'ecx' value: %Rrc\n",
1900 pszLabel, szName, rc);
1901 rc = CFGMR3QueryU32Def(pNode, "edx", &Leaf.uEdx, Leaf.uEdx);
1902 if (RT_FAILURE(rc))
1903 return VMSetError(pVM, rc, RT_SRC_POS, "Invalid %s entry '%s': Error querying 'edx' value: %Rrc\n",
1904 pszLabel, szName, rc);
1905
1906 /*
1907 * Insert the leaf into the table (replaces existing ones).
1908 */
1909 rc = cpumR3CpuIdInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paCpuIdLeavesR3, &pVM->cpum.s.GuestInfo.cCpuIdLeaves,
1910 &Leaf);
1911 if (RT_FAILURE(rc))
1912 return VMSetError(pVM, rc, RT_SRC_POS, "Error adding CPUID leaf entry '%s': %Rrc\n", szName, rc);
1913 }
1914
1915 return VINF_SUCCESS;
1916}
1917
1918
1919
1920/**
1921 * Fetches overrides for a CPUID leaf.
1922 *
1923 * @returns VBox status code.
1924 * @param pLeaf The leaf to load the overrides into.
1925 * @param pCfgNode The CFGM node containing the overrides
1926 * (/CPUM/HostCPUID/ or /CPUM/CPUID/).
1927 * @param iLeaf The CPUID leaf number.
1928 */
1929static int cpumR3CpuIdFetchLeafOverride(PCPUMCPUID pLeaf, PCFGMNODE pCfgNode, uint32_t iLeaf)
1930{
1931 PCFGMNODE pLeafNode = CFGMR3GetChildF(pCfgNode, "%RX32", iLeaf);
1932 if (pLeafNode)
1933 {
1934 uint32_t u32;
1935 int rc = CFGMR3QueryU32(pLeafNode, "eax", &u32);
1936 if (RT_SUCCESS(rc))
1937 pLeaf->uEax = u32;
1938 else
1939 AssertReturn(rc == VERR_CFGM_VALUE_NOT_FOUND, rc);
1940
1941 rc = CFGMR3QueryU32(pLeafNode, "ebx", &u32);
1942 if (RT_SUCCESS(rc))
1943 pLeaf->uEbx = u32;
1944 else
1945 AssertReturn(rc == VERR_CFGM_VALUE_NOT_FOUND, rc);
1946
1947 rc = CFGMR3QueryU32(pLeafNode, "ecx", &u32);
1948 if (RT_SUCCESS(rc))
1949 pLeaf->uEcx = u32;
1950 else
1951 AssertReturn(rc == VERR_CFGM_VALUE_NOT_FOUND, rc);
1952
1953 rc = CFGMR3QueryU32(pLeafNode, "edx", &u32);
1954 if (RT_SUCCESS(rc))
1955 pLeaf->uEdx = u32;
1956 else
1957 AssertReturn(rc == VERR_CFGM_VALUE_NOT_FOUND, rc);
1958
1959 }
1960 return VINF_SUCCESS;
1961}
1962
1963
1964/**
1965 * Load the overrides for a set of CPUID leaves.
1966 *
1967 * @returns VBox status code.
1968 * @param paLeaves The leaf array.
1969 * @param cLeaves The number of leaves.
1970 * @param uStart The start leaf number.
1971 * @param pCfgNode The CFGM node containing the overrides
1972 * (/CPUM/HostCPUID/ or /CPUM/CPUID/).
1973 */
1974static int cpumR3CpuIdInitLoadOverrideSet(uint32_t uStart, PCPUMCPUID paLeaves, uint32_t cLeaves, PCFGMNODE pCfgNode)
1975{
1976 for (uint32_t i = 0; i < cLeaves; i++)
1977 {
1978 int rc = cpumR3CpuIdFetchLeafOverride(&paLeaves[i], pCfgNode, uStart + i);
1979 if (RT_FAILURE(rc))
1980 return rc;
1981 }
1982
1983 return VINF_SUCCESS;
1984}
1985
1986/**
1987 * Init a set of host CPUID leaves.
1988 *
1989 * @returns VBox status code.
1990 * @param paLeaves The leaf array.
1991 * @param cLeaves The number of leaves.
1992 * @param uStart The start leaf number.
1993 * @param pCfgNode The /CPUM/HostCPUID/ node.
1994 */
1995static int cpumR3CpuIdInitHostSet(uint32_t uStart, PCPUMCPUID paLeaves, uint32_t cLeaves, PCFGMNODE pCfgNode)
1996{
1997 /* Using the ECX variant for all of them can't hurt... */
1998 for (uint32_t i = 0; i < cLeaves; i++)
1999 ASMCpuIdExSlow(uStart + i, 0, 0, 0, &paLeaves[i].uEax, &paLeaves[i].uEbx, &paLeaves[i].uEcx, &paLeaves[i].uEdx);
2000
2001 /* Load CPUID leaf override; we currently don't care if the user
2002 specifies features the host CPU doesn't support. */
2003 return cpumR3CpuIdInitLoadOverrideSet(uStart, paLeaves, cLeaves, pCfgNode);
2004}
2005
2006
2007/**
2008 * Installs the CPUID leaves and explods the data into structures like
2009 * GuestFeatures and CPUMCTX::aoffXState.
2010 *
2011 * @returns VBox status code.
2012 * @param pVM The cross context VM handle.
2013 * @param pCpum The CPUM part of @a VM.
2014 * @param paLeaves The leaves. These will be copied (but not freed).
2015 * @param cLeaves The number of leaves.
2016 */
2017static int cpumR3CpuIdInstallAndExplodeLeaves(PVM pVM, PCPUM pCpum, PCPUMCPUIDLEAF paLeaves, uint32_t cLeaves)
2018{
2019 cpumR3CpuIdAssertOrder(paLeaves, cLeaves);
2020
2021 /*
2022 * Install the CPUID information.
2023 */
2024 int rc = MMHyperDupMem(pVM, paLeaves, sizeof(paLeaves[0]) * cLeaves, 32,
2025 MM_TAG_CPUM_CPUID, (void **)&pCpum->GuestInfo.paCpuIdLeavesR3);
2026
2027 AssertLogRelRCReturn(rc, rc);
2028 pCpum->GuestInfo.cCpuIdLeaves = cLeaves;
2029 pCpum->GuestInfo.paCpuIdLeavesR0 = MMHyperR3ToR0(pVM, pCpum->GuestInfo.paCpuIdLeavesR3);
2030 pCpum->GuestInfo.paCpuIdLeavesRC = MMHyperR3ToRC(pVM, pCpum->GuestInfo.paCpuIdLeavesR3);
2031 Assert(MMHyperR0ToR3(pVM, pCpum->GuestInfo.paCpuIdLeavesR0) == (void *)pCpum->GuestInfo.paCpuIdLeavesR3);
2032 Assert(MMHyperRCToR3(pVM, pCpum->GuestInfo.paCpuIdLeavesRC) == (void *)pCpum->GuestInfo.paCpuIdLeavesR3);
2033
2034 /*
2035 * Update the default CPUID leaf if necessary.
2036 */
2037 switch (pCpum->GuestInfo.enmUnknownCpuIdMethod)
2038 {
2039 case CPUMUNKNOWNCPUID_LAST_STD_LEAF:
2040 case CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX:
2041 {
2042 /* We don't use CPUID(0).eax here because of the NT hack that only
2043 changes that value without actually removing any leaves. */
2044 uint32_t i = 0;
2045 if ( pCpum->GuestInfo.cCpuIdLeaves > 0
2046 && pCpum->GuestInfo.paCpuIdLeavesR3[0].uLeaf <= UINT32_C(0xff))
2047 {
2048 while ( i + 1 < pCpum->GuestInfo.cCpuIdLeaves
2049 && pCpum->GuestInfo.paCpuIdLeavesR3[i + 1].uLeaf <= UINT32_C(0xff))
2050 i++;
2051 pCpum->GuestInfo.DefCpuId.uEax = pCpum->GuestInfo.paCpuIdLeavesR3[i].uEax;
2052 pCpum->GuestInfo.DefCpuId.uEbx = pCpum->GuestInfo.paCpuIdLeavesR3[i].uEbx;
2053 pCpum->GuestInfo.DefCpuId.uEcx = pCpum->GuestInfo.paCpuIdLeavesR3[i].uEcx;
2054 pCpum->GuestInfo.DefCpuId.uEdx = pCpum->GuestInfo.paCpuIdLeavesR3[i].uEdx;
2055 }
2056 break;
2057 }
2058 default:
2059 break;
2060 }
2061
2062 /*
2063 * Explode the guest CPU features.
2064 */
2065 rc = cpumR3CpuIdExplodeFeatures(pCpum->GuestInfo.paCpuIdLeavesR3, pCpum->GuestInfo.cCpuIdLeaves, &pCpum->GuestFeatures);
2066 AssertLogRelRCReturn(rc, rc);
2067
2068 /*
2069 * Adjust the scalable bus frequency according to the CPUID information
2070 * we're now using.
2071 */
2072 if (CPUMMICROARCH_IS_INTEL_CORE7(pVM->cpum.s.GuestFeatures.enmMicroarch))
2073 pCpum->GuestInfo.uScalableBusFreq = pCpum->GuestFeatures.enmMicroarch >= kCpumMicroarch_Intel_Core7_SandyBridge
2074 ? UINT64_C(100000000) /* 100MHz */
2075 : UINT64_C(133333333); /* 133MHz */
2076
2077 /*
2078 * Populate the legacy arrays. Currently used for everything, later only
2079 * for patch manager.
2080 */
2081 struct { PCPUMCPUID paCpuIds; uint32_t cCpuIds, uBase; } aOldRanges[] =
2082 {
2083 { pCpum->aGuestCpuIdPatmStd, RT_ELEMENTS(pCpum->aGuestCpuIdPatmStd), 0x00000000 },
2084 { pCpum->aGuestCpuIdPatmExt, RT_ELEMENTS(pCpum->aGuestCpuIdPatmExt), 0x80000000 },
2085 { pCpum->aGuestCpuIdPatmCentaur, RT_ELEMENTS(pCpum->aGuestCpuIdPatmCentaur), 0xc0000000 },
2086 };
2087 for (uint32_t i = 0; i < RT_ELEMENTS(aOldRanges); i++)
2088 {
2089 uint32_t cLeft = aOldRanges[i].cCpuIds;
2090 uint32_t uLeaf = aOldRanges[i].uBase + cLeft;
2091 PCPUMCPUID pLegacyLeaf = &aOldRanges[i].paCpuIds[cLeft];
2092 while (cLeft-- > 0)
2093 {
2094 uLeaf--;
2095 pLegacyLeaf--;
2096
2097 PCCPUMCPUIDLEAF pLeaf = cpumR3CpuIdGetExactLeaf(pCpum, uLeaf, 0 /* uSubLeaf */);
2098 if (pLeaf)
2099 {
2100 pLegacyLeaf->uEax = pLeaf->uEax;
2101 pLegacyLeaf->uEbx = pLeaf->uEbx;
2102 pLegacyLeaf->uEcx = pLeaf->uEcx;
2103 pLegacyLeaf->uEdx = pLeaf->uEdx;
2104 }
2105 else
2106 *pLegacyLeaf = pCpum->GuestInfo.DefCpuId;
2107 }
2108 }
2109
2110 /*
2111 * Configure XSAVE offsets according to the CPUID info.
2112 */
2113 memset(&pVM->aCpus[0].cpum.s.Guest.aoffXState[0], 0xff, sizeof(pVM->aCpus[0].cpum.s.Guest.aoffXState));
2114 pVM->aCpus[0].cpum.s.Guest.aoffXState[XSAVE_C_X87_BIT] = 0;
2115 pVM->aCpus[0].cpum.s.Guest.aoffXState[XSAVE_C_SSE_BIT] = 0;
2116 for (uint32_t iComponent = XSAVE_C_SSE_BIT + 1; iComponent < 63; iComponent++)
2117 if (pCpum->fXStateGuestMask & RT_BIT_64(iComponent))
2118 {
2119 PCPUMCPUIDLEAF pSubLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 0xd, iComponent);
2120 AssertLogRelMsgReturn(pSubLeaf, ("iComponent=%#x\n", iComponent), VERR_CPUM_IPE_1);
2121 AssertLogRelMsgReturn(pSubLeaf->fSubLeafMask >= iComponent, ("iComponent=%#x\n", iComponent), VERR_CPUM_IPE_1);
2122 AssertLogRelMsgReturn( pSubLeaf->uEax > 0
2123 && pSubLeaf->uEbx >= CPUM_MIN_XSAVE_AREA_SIZE
2124 && pSubLeaf->uEax <= pCpum->GuestFeatures.cbMaxExtendedState
2125 && pSubLeaf->uEbx <= pCpum->GuestFeatures.cbMaxExtendedState
2126 && pSubLeaf->uEbx + pSubLeaf->uEax <= pCpum->GuestFeatures.cbMaxExtendedState,
2127 ("iComponent=%#x eax=%#x ebx=%#x cbMax=%#x\n", iComponent, pSubLeaf->uEax, pSubLeaf->uEbx,
2128 pCpum->GuestFeatures.cbMaxExtendedState),
2129 VERR_CPUM_IPE_1);
2130 pVM->aCpus[0].cpum.s.Guest.aoffXState[iComponent] = pSubLeaf->uEbx;
2131 }
2132 memset(&pVM->aCpus[0].cpum.s.Hyper.aoffXState[0], 0xff, sizeof(pVM->aCpus[0].cpum.s.Hyper.aoffXState));
2133
2134 /* Copy the CPU #0 data to the other CPUs. */
2135 for (VMCPUID iCpu = 1; iCpu < pVM->cCpus; iCpu++)
2136 {
2137 memcpy(&pVM->aCpus[iCpu].cpum.s.Guest.aoffXState[0], &pVM->aCpus[0].cpum.s.Guest.aoffXState[0],
2138 sizeof(pVM->aCpus[iCpu].cpum.s.Guest.aoffXState));
2139 memcpy(&pVM->aCpus[iCpu].cpum.s.Hyper.aoffXState[0], &pVM->aCpus[0].cpum.s.Hyper.aoffXState[0],
2140 sizeof(pVM->aCpus[iCpu].cpum.s.Hyper.aoffXState));
2141 }
2142
2143 return VINF_SUCCESS;
2144}
2145
2146
2147/** @name Instruction Set Extension Options
2148 * @{ */
2149/** Configuration option type (extended boolean, really). */
2150typedef uint8_t CPUMISAEXTCFG;
2151/** Always disable the extension. */
2152#define CPUMISAEXTCFG_DISABLED false
2153/** Enable the extension if it's supported by the host CPU. */
2154#define CPUMISAEXTCFG_ENABLED_SUPPORTED true
2155/** Enable the extension if it's supported by the host CPU, but don't let
2156 * the portable CPUID feature disable it. */
2157#define CPUMISAEXTCFG_ENABLED_PORTABLE UINT8_C(127)
2158/** Always enable the extension. */
2159#define CPUMISAEXTCFG_ENABLED_ALWAYS UINT8_C(255)
2160/** @} */
2161
2162/**
2163 * CPUID Configuration (from CFGM).
2164 *
2165 * @remarks The members aren't document since we would only be duplicating the
2166 * \@cfgm entries in cpumR3CpuIdReadConfig.
2167 */
2168typedef struct CPUMCPUIDCONFIG
2169{
2170 bool fNt4LeafLimit;
2171 bool fInvariantTsc;
2172
2173 CPUMISAEXTCFG enmCmpXchg16b;
2174 CPUMISAEXTCFG enmMonitor;
2175 CPUMISAEXTCFG enmMWaitExtensions;
2176 CPUMISAEXTCFG enmSse41;
2177 CPUMISAEXTCFG enmSse42;
2178 CPUMISAEXTCFG enmAvx;
2179 CPUMISAEXTCFG enmAvx2;
2180 CPUMISAEXTCFG enmXSave;
2181 CPUMISAEXTCFG enmAesNi;
2182 CPUMISAEXTCFG enmPClMul;
2183 CPUMISAEXTCFG enmPopCnt;
2184 CPUMISAEXTCFG enmMovBe;
2185 CPUMISAEXTCFG enmRdRand;
2186 CPUMISAEXTCFG enmRdSeed;
2187 CPUMISAEXTCFG enmCLFlushOpt;
2188
2189 CPUMISAEXTCFG enmAbm;
2190 CPUMISAEXTCFG enmSse4A;
2191 CPUMISAEXTCFG enmMisAlnSse;
2192 CPUMISAEXTCFG enm3dNowPrf;
2193 CPUMISAEXTCFG enmAmdExtMmx;
2194
2195 uint32_t uMaxStdLeaf;
2196 uint32_t uMaxExtLeaf;
2197 uint32_t uMaxCentaurLeaf;
2198 uint32_t uMaxIntelFamilyModelStep;
2199 char szCpuName[128];
2200} CPUMCPUIDCONFIG;
2201/** Pointer to CPUID config (from CFGM). */
2202typedef CPUMCPUIDCONFIG *PCPUMCPUIDCONFIG;
2203
2204
2205/**
2206 * Insert hypervisor identification leaves.
2207 *
2208 * We only return minimal information, primarily ensuring that the
2209 * 0x40000000 function returns 0x40000001 and identifying ourselves.
2210 * Hypervisor-specific interface is supported through GIM which will
2211 * modify these leaves if required depending on the GIM provider.
2212 *
2213 * @returns VBox status code.
2214 * @param pCpum The CPUM instance data.
2215 * @param pConfig The CPUID configuration we've read from CFGM.
2216 */
2217static int cpumR3CpuIdPlantHypervisorLeaves(PCPUM pCpum, PCPUMCPUIDCONFIG pConfig)
2218{
2219 CPUMCPUIDLEAF NewLeaf;
2220 NewLeaf.uLeaf = UINT32_C(0x40000000);
2221 NewLeaf.uSubLeaf = 0;
2222 NewLeaf.fSubLeafMask = 0;
2223 NewLeaf.uEax = UINT32_C(0x40000001);
2224 NewLeaf.uEbx = 0x786f4256 /* 'VBox' */;
2225 NewLeaf.uEcx = 0x786f4256 /* 'VBox' */;
2226 NewLeaf.uEdx = 0x786f4256 /* 'VBox' */;
2227 NewLeaf.fFlags = 0;
2228 int rc = cpumR3CpuIdInsert(NULL /* pVM */, &pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves, &NewLeaf);
2229 AssertLogRelRCReturn(rc, rc);
2230
2231 NewLeaf.uLeaf = UINT32_C(0x40000001);
2232 NewLeaf.uEax = 0x656e6f6e; /* 'none' */
2233 NewLeaf.uEbx = 0;
2234 NewLeaf.uEcx = 0;
2235 NewLeaf.uEdx = 0;
2236 NewLeaf.fFlags = 0;
2237 rc = cpumR3CpuIdInsert(NULL /* pVM */, &pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves, &NewLeaf);
2238 AssertLogRelRCReturn(rc, rc);
2239
2240 return VINF_SUCCESS;
2241}
2242
2243
2244/**
2245 * Mini CPU selection support for making Mac OS X happy.
2246 *
2247 * Executes the /CPUM/MaxIntelFamilyModelStep config.
2248 *
2249 * @param pCpum The CPUM instance data.
2250 * @param pConfig The CPUID configuration we've read from CFGM.
2251 */
2252static void cpumR3CpuIdLimitIntelFamModStep(PCPUM pCpum, PCPUMCPUIDCONFIG pConfig)
2253{
2254 if (pCpum->GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_INTEL)
2255 {
2256 PCPUMCPUIDLEAF pStdFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 1, 0);
2257 uint32_t uCurIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(ASMGetCpuStepping(pStdFeatureLeaf->uEax),
2258 ASMGetCpuModelIntel(pStdFeatureLeaf->uEax),
2259 ASMGetCpuFamily(pStdFeatureLeaf->uEax),
2260 0);
2261 uint32_t uMaxIntelFamilyModelStep = pConfig->uMaxIntelFamilyModelStep;
2262 if (pConfig->uMaxIntelFamilyModelStep < uCurIntelFamilyModelStep)
2263 {
2264 uint32_t uNew = pStdFeatureLeaf->uEax & UINT32_C(0xf0003000);
2265 uNew |= RT_BYTE1(uMaxIntelFamilyModelStep) & 0xf; /* stepping */
2266 uNew |= (RT_BYTE2(uMaxIntelFamilyModelStep) & 0xf) << 4; /* 4 low model bits */
2267 uNew |= (RT_BYTE2(uMaxIntelFamilyModelStep) >> 4) << 16; /* 4 high model bits */
2268 uNew |= (RT_BYTE3(uMaxIntelFamilyModelStep) & 0xf) << 8; /* 4 low family bits */
2269 if (RT_BYTE3(uMaxIntelFamilyModelStep) > 0xf) /* 8 high family bits, using intel's suggested calculation. */
2270 uNew |= ( (RT_BYTE3(uMaxIntelFamilyModelStep) - (RT_BYTE3(uMaxIntelFamilyModelStep) & 0xf)) & 0xff ) << 20;
2271 LogRel(("CPU: CPUID(0).EAX %#x -> %#x (uMaxIntelFamilyModelStep=%#x, uCurIntelFamilyModelStep=%#x\n",
2272 pStdFeatureLeaf->uEax, uNew, uMaxIntelFamilyModelStep, uCurIntelFamilyModelStep));
2273 pStdFeatureLeaf->uEax = uNew;
2274 }
2275 }
2276}
2277
2278
2279
2280/**
2281 * Limit it the number of entries, zapping the remainder.
2282 *
2283 * The limits are masking off stuff about power saving and similar, this
2284 * is perhaps a bit crudely done as there is probably some relatively harmless
2285 * info too in these leaves (like words about having a constant TSC).
2286 *
2287 * @param pCpum The CPUM instance data.
2288 * @param pConfig The CPUID configuration we've read from CFGM.
2289 */
2290static void cpumR3CpuIdLimitLeaves(PCPUM pCpum, PCPUMCPUIDCONFIG pConfig)
2291{
2292 /*
2293 * Standard leaves.
2294 */
2295 uint32_t uSubLeaf = 0;
2296 PCPUMCPUIDLEAF pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 0, uSubLeaf);
2297 if (pCurLeaf)
2298 {
2299 uint32_t uLimit = pCurLeaf->uEax;
2300 if (uLimit <= UINT32_C(0x000fffff))
2301 {
2302 if (uLimit > pConfig->uMaxStdLeaf)
2303 {
2304 pCurLeaf->uEax = uLimit = pConfig->uMaxStdLeaf;
2305 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2306 uLimit + 1, UINT32_C(0x000fffff));
2307 }
2308
2309 /* NT4 hack, no zapping of extra leaves here. */
2310 if (pConfig->fNt4LeafLimit && uLimit > 3)
2311 pCurLeaf->uEax = uLimit = 3;
2312
2313 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x00000000), ++uSubLeaf)) != NULL)
2314 pCurLeaf->uEax = uLimit;
2315 }
2316 else
2317 {
2318 LogRel(("CPUID: Invalid standard range: %#x\n", uLimit));
2319 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2320 UINT32_C(0x00000000), UINT32_C(0x0fffffff));
2321 }
2322 }
2323
2324 /*
2325 * Extended leaves.
2326 */
2327 uSubLeaf = 0;
2328 pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x80000000), uSubLeaf);
2329 if (pCurLeaf)
2330 {
2331 uint32_t uLimit = pCurLeaf->uEax;
2332 if ( uLimit >= UINT32_C(0x80000000)
2333 && uLimit <= UINT32_C(0x800fffff))
2334 {
2335 if (uLimit > pConfig->uMaxExtLeaf)
2336 {
2337 pCurLeaf->uEax = uLimit = pConfig->uMaxExtLeaf;
2338 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2339 uLimit + 1, UINT32_C(0x800fffff));
2340 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x80000000), ++uSubLeaf)) != NULL)
2341 pCurLeaf->uEax = uLimit;
2342 }
2343 }
2344 else
2345 {
2346 LogRel(("CPUID: Invalid extended range: %#x\n", uLimit));
2347 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2348 UINT32_C(0x80000000), UINT32_C(0x8ffffffd));
2349 }
2350 }
2351
2352 /*
2353 * Centaur leaves (VIA).
2354 */
2355 uSubLeaf = 0;
2356 pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0xc0000000), uSubLeaf);
2357 if (pCurLeaf)
2358 {
2359 uint32_t uLimit = pCurLeaf->uEax;
2360 if ( uLimit >= UINT32_C(0xc0000000)
2361 && uLimit <= UINT32_C(0xc00fffff))
2362 {
2363 if (uLimit > pConfig->uMaxCentaurLeaf)
2364 {
2365 pCurLeaf->uEax = uLimit = pConfig->uMaxCentaurLeaf;
2366 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2367 uLimit + 1, UINT32_C(0xcfffffff));
2368 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0xc0000000), ++uSubLeaf)) != NULL)
2369 pCurLeaf->uEax = uLimit;
2370 }
2371 }
2372 else
2373 {
2374 LogRel(("CPUID: Invalid centaur range: %#x\n", uLimit));
2375 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
2376 UINT32_C(0xc0000000), UINT32_C(0xcfffffff));
2377 }
2378 }
2379}
2380
2381
2382/**
2383 * Clears a CPUID leaf and all sub-leaves (to zero).
2384 *
2385 * @param pCpum The CPUM instance data.
2386 * @param uLeaf The leaf to clear.
2387 */
2388static void cpumR3CpuIdZeroLeaf(PCPUM pCpum, uint32_t uLeaf)
2389{
2390 uint32_t uSubLeaf = 0;
2391 PCPUMCPUIDLEAF pCurLeaf;
2392 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, uLeaf, uSubLeaf)) != NULL)
2393 {
2394 pCurLeaf->uEax = 0;
2395 pCurLeaf->uEbx = 0;
2396 pCurLeaf->uEcx = 0;
2397 pCurLeaf->uEdx = 0;
2398 uSubLeaf++;
2399 }
2400}
2401
2402
2403/**
2404 * Used by cpumR3CpuIdSanitize to ensure that we don't have any sub-leaves for
2405 * the given leaf.
2406 *
2407 * @returns pLeaf.
2408 * @param pCpum The CPUM instance data.
2409 * @param pLeaf The leaf to ensure is alone with it's EAX input value.
2410 */
2411static PCPUMCPUIDLEAF cpumR3CpuIdMakeSingleLeaf(PCPUM pCpum, PCPUMCPUIDLEAF pLeaf)
2412{
2413 Assert((uintptr_t)(pLeaf - pCpum->GuestInfo.paCpuIdLeavesR3) < pCpum->GuestInfo.cCpuIdLeaves);
2414 if (pLeaf->fSubLeafMask != 0)
2415 {
2416 /*
2417 * Figure out how many sub-leaves in need of removal (we'll keep the first).
2418 * Log everything while we're at it.
2419 */
2420 LogRel(("CPUM:\n"
2421 "CPUM: Unexpected CPUID sub-leaves for leaf %#x; fSubLeafMask=%#x\n", pLeaf->uLeaf, pLeaf->fSubLeafMask));
2422 PCPUMCPUIDLEAF pLast = &pCpum->GuestInfo.paCpuIdLeavesR3[pCpum->GuestInfo.cCpuIdLeaves - 1];
2423 PCPUMCPUIDLEAF pSubLeaf = pLeaf;
2424 for (;;)
2425 {
2426 LogRel(("CPUM: %08x/%08x: %08x %08x %08x %08x; flags=%#x mask=%#x\n",
2427 pSubLeaf->uLeaf, pSubLeaf->uSubLeaf,
2428 pSubLeaf->uEax, pSubLeaf->uEbx, pSubLeaf->uEcx, pSubLeaf->uEdx,
2429 pSubLeaf->fFlags, pSubLeaf->fSubLeafMask));
2430 if (pSubLeaf == pLast || pSubLeaf[1].uLeaf != pLeaf->uLeaf)
2431 break;
2432 pSubLeaf++;
2433 }
2434 LogRel(("CPUM:\n"));
2435
2436 /*
2437 * Remove the offending sub-leaves.
2438 */
2439 if (pSubLeaf != pLeaf)
2440 {
2441 if (pSubLeaf != pLast)
2442 memmove(pLeaf + 1, pSubLeaf + 1, (uintptr_t)pLast - (uintptr_t)pSubLeaf);
2443 pCpum->GuestInfo.cCpuIdLeaves -= (uint32_t)(pSubLeaf - pLeaf);
2444 }
2445
2446 /*
2447 * Convert the first sub-leaf into a single leaf.
2448 */
2449 pLeaf->uSubLeaf = 0;
2450 pLeaf->fSubLeafMask = 0;
2451 }
2452 return pLeaf;
2453}
2454
2455
2456/**
2457 * Sanitizes and adjust the CPUID leaves.
2458 *
2459 * Drop features that aren't virtualized (or virtualizable). Adjust information
2460 * and capabilities to fit the virtualized hardware. Remove information the
2461 * guest shouldn't have (because it's wrong in the virtual world or because it
2462 * gives away host details) or that we don't have documentation for and no idea
2463 * what means.
2464 *
2465 * @returns VBox status code.
2466 * @param pVM Pointer to the cross context VM structure (for cCpus).
2467 * @param pCpum The CPUM instance data.
2468 * @param pConfig The CPUID configuration we've read from CFGM.
2469 */
2470static int cpumR3CpuIdSanitize(PVM pVM, PCPUM pCpum, PCPUMCPUIDCONFIG pConfig)
2471{
2472#define PORTABLE_CLEAR_BITS_WHEN(Lvl, a_pLeafReg, FeatNm, fMask, uValue) \
2473 if ( pCpum->u8PortableCpuIdLevel >= (Lvl) && ((a_pLeafReg) & (fMask)) == (uValue) ) \
2474 { \
2475 LogRel(("PortableCpuId: " #a_pLeafReg "[" #FeatNm "]: %#x -> 0\n", (a_pLeafReg) & (fMask))); \
2476 (a_pLeafReg) &= ~(uint32_t)(fMask); \
2477 }
2478#define PORTABLE_DISABLE_FEATURE_BIT(Lvl, a_pLeafReg, FeatNm, fBitMask) \
2479 if ( pCpum->u8PortableCpuIdLevel >= (Lvl) && ((a_pLeafReg) & (fBitMask)) ) \
2480 { \
2481 LogRel(("PortableCpuId: " #a_pLeafReg "[" #FeatNm "]: 1 -> 0\n")); \
2482 (a_pLeafReg) &= ~(uint32_t)(fBitMask); \
2483 }
2484#define PORTABLE_DISABLE_FEATURE_BIT_CFG(Lvl, a_pLeafReg, FeatNm, fBitMask, enmConfig) \
2485 if ( pCpum->u8PortableCpuIdLevel >= (Lvl) \
2486 && ((a_pLeafReg) & (fBitMask)) \
2487 && (enmConfig) != CPUMISAEXTCFG_ENABLED_PORTABLE ) \
2488 { \
2489 LogRel(("PortableCpuId: " #a_pLeafReg "[" #FeatNm "]: 1 -> 0\n")); \
2490 (a_pLeafReg) &= ~(uint32_t)(fBitMask); \
2491 }
2492 Assert(pCpum->GuestFeatures.enmCpuVendor != CPUMCPUVENDOR_INVALID);
2493
2494 /* Cpuid 1:
2495 * EAX: CPU model, family and stepping.
2496 *
2497 * ECX + EDX: Supported features. Only report features we can support.
2498 * Note! When enabling new features the Synthetic CPU and Portable CPUID
2499 * options may require adjusting (i.e. stripping what was enabled).
2500 *
2501 * EBX: Branding, CLFLUSH line size, logical processors per package and
2502 * initial APIC ID.
2503 */
2504 PCPUMCPUIDLEAF pStdFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 1, 0); /* Note! Must refetch when used later. */
2505 AssertLogRelReturn(pStdFeatureLeaf, VERR_CPUM_IPE_2);
2506 pStdFeatureLeaf = cpumR3CpuIdMakeSingleLeaf(pCpum, pStdFeatureLeaf);
2507
2508 pStdFeatureLeaf->uEdx &= X86_CPUID_FEATURE_EDX_FPU
2509 | X86_CPUID_FEATURE_EDX_VME
2510 | X86_CPUID_FEATURE_EDX_DE
2511 | X86_CPUID_FEATURE_EDX_PSE
2512 | X86_CPUID_FEATURE_EDX_TSC
2513 | X86_CPUID_FEATURE_EDX_MSR
2514 //| X86_CPUID_FEATURE_EDX_PAE - set later if configured.
2515 | X86_CPUID_FEATURE_EDX_MCE
2516 | X86_CPUID_FEATURE_EDX_CX8
2517 //| X86_CPUID_FEATURE_EDX_APIC - set by the APIC device if present.
2518 //| RT_BIT_32(10) - not defined
2519 /* Note! we don't report sysenter/sysexit support due to our inability to keep the IOPL part of eflags in sync while in ring 1 (see @bugref{1757}) */
2520 //| X86_CPUID_FEATURE_EDX_SEP
2521 | X86_CPUID_FEATURE_EDX_MTRR
2522 | X86_CPUID_FEATURE_EDX_PGE
2523 | X86_CPUID_FEATURE_EDX_MCA
2524 | X86_CPUID_FEATURE_EDX_CMOV
2525 | X86_CPUID_FEATURE_EDX_PAT /* 16 */
2526 | X86_CPUID_FEATURE_EDX_PSE36
2527 //| X86_CPUID_FEATURE_EDX_PSN - no serial number.
2528 | X86_CPUID_FEATURE_EDX_CLFSH
2529 //| RT_BIT_32(20) - not defined
2530 //| X86_CPUID_FEATURE_EDX_DS - no debug store.
2531 //| X86_CPUID_FEATURE_EDX_ACPI - not supported (not DevAcpi, right?).
2532 | X86_CPUID_FEATURE_EDX_MMX
2533 | X86_CPUID_FEATURE_EDX_FXSR
2534 | X86_CPUID_FEATURE_EDX_SSE
2535 | X86_CPUID_FEATURE_EDX_SSE2
2536 //| X86_CPUID_FEATURE_EDX_SS - no self snoop.
2537 //| X86_CPUID_FEATURE_EDX_HTT - no hyperthreading/cores - see below.
2538 //| X86_CPUID_FEATURE_EDX_TM - no thermal monitor.
2539 //| RT_BIT_32(30) - not defined
2540 //| X86_CPUID_FEATURE_EDX_PBE - no pending break enabled.
2541 ;
2542 pStdFeatureLeaf->uEcx &= 0
2543 | X86_CPUID_FEATURE_ECX_SSE3
2544 | (pConfig->enmPClMul ? X86_CPUID_FEATURE_ECX_PCLMUL : 0)
2545 //| X86_CPUID_FEATURE_ECX_DTES64 - not implemented yet.
2546 /* Can't properly emulate monitor & mwait with guest SMP; force the guest to use hlt for idling VCPUs. */
2547 | ((pConfig->enmMonitor && pVM->cCpus == 1) ? X86_CPUID_FEATURE_ECX_MONITOR : 0)
2548 //| X86_CPUID_FEATURE_ECX_CPLDS - no CPL qualified debug store.
2549 //| X86_CPUID_FEATURE_ECX_VMX - not virtualized yet.
2550 //| X86_CPUID_FEATURE_ECX_SMX - not virtualized yet.
2551 //| X86_CPUID_FEATURE_ECX_EST - no extended speed step.
2552 //| X86_CPUID_FEATURE_ECX_TM2 - no thermal monitor 2.
2553 | X86_CPUID_FEATURE_ECX_SSSE3
2554 //| X86_CPUID_FEATURE_ECX_CNTXID - no L1 context id (MSR++).
2555 //| X86_CPUID_FEATURE_ECX_FMA - not implemented yet.
2556 | (pConfig->enmCmpXchg16b ? X86_CPUID_FEATURE_ECX_CX16 : 0)
2557 /* ECX Bit 14 - xTPR Update Control. Processor supports changing IA32_MISC_ENABLES[bit 23]. */
2558 //| X86_CPUID_FEATURE_ECX_TPRUPDATE
2559 //| X86_CPUID_FEATURE_ECX_PDCM - not implemented yet.
2560 //| X86_CPUID_FEATURE_ECX_PCID - not implemented yet.
2561 //| X86_CPUID_FEATURE_ECX_DCA - not implemented yet.
2562 | (pConfig->enmSse41 ? X86_CPUID_FEATURE_ECX_SSE4_1 : 0)
2563 | (pConfig->enmSse42 ? X86_CPUID_FEATURE_ECX_SSE4_2 : 0)
2564 //| X86_CPUID_FEATURE_ECX_X2APIC - turned on later by the device if enabled.
2565 | (pConfig->enmMovBe ? X86_CPUID_FEATURE_ECX_MOVBE : 0)
2566 | (pConfig->enmPopCnt ? X86_CPUID_FEATURE_ECX_POPCNT : 0)
2567 //| X86_CPUID_FEATURE_ECX_TSCDEADL - not implemented yet.
2568 | (pConfig->enmAesNi ? X86_CPUID_FEATURE_ECX_AES : 0)
2569 | (pConfig->enmXSave ? X86_CPUID_FEATURE_ECX_XSAVE : 0 )
2570 //| X86_CPUID_FEATURE_ECX_OSXSAVE - mirrors CR4.OSXSAVE state, set dynamically.
2571 | (pConfig->enmAvx ? X86_CPUID_FEATURE_ECX_AVX : 0)
2572 //| X86_CPUID_FEATURE_ECX_F16C - not implemented yet.
2573 | (pConfig->enmRdRand ? X86_CPUID_FEATURE_ECX_RDRAND : 0)
2574 //| X86_CPUID_FEATURE_ECX_HVP - Set explicitly later.
2575 ;
2576
2577 if (pCpum->u8PortableCpuIdLevel > 0)
2578 {
2579 PORTABLE_CLEAR_BITS_WHEN(1, pStdFeatureLeaf->uEax, ProcessorType, (UINT32_C(3) << 12), (UINT32_C(2) << 12));
2580 PORTABLE_DISABLE_FEATURE_BIT( 1, pStdFeatureLeaf->uEcx, SSSE3, X86_CPUID_FEATURE_ECX_SSSE3);
2581 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, SSE4_1, X86_CPUID_FEATURE_ECX_SSE4_1, pConfig->enmSse41);
2582 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, SSE4_2, X86_CPUID_FEATURE_ECX_SSE4_2, pConfig->enmSse42);
2583 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, MOVBE, X86_CPUID_FEATURE_ECX_MOVBE, pConfig->enmMovBe);
2584 PORTABLE_DISABLE_FEATURE_BIT( 1, pStdFeatureLeaf->uEcx, AES, X86_CPUID_FEATURE_ECX_AES);
2585 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, PCLMUL, X86_CPUID_FEATURE_ECX_PCLMUL, pConfig->enmPClMul);
2586 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, POPCNT, X86_CPUID_FEATURE_ECX_POPCNT, pConfig->enmPopCnt);
2587 PORTABLE_DISABLE_FEATURE_BIT( 1, pStdFeatureLeaf->uEcx, F16C, X86_CPUID_FEATURE_ECX_F16C);
2588 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, XSAVE, X86_CPUID_FEATURE_ECX_XSAVE, pConfig->enmXSave);
2589 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, AVX, X86_CPUID_FEATURE_ECX_AVX, pConfig->enmAvx);
2590 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, RDRAND, X86_CPUID_FEATURE_ECX_RDRAND, pConfig->enmRdRand);
2591 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pStdFeatureLeaf->uEcx, CX16, X86_CPUID_FEATURE_ECX_CX16, pConfig->enmCmpXchg16b);
2592 PORTABLE_DISABLE_FEATURE_BIT( 2, pStdFeatureLeaf->uEcx, SSE3, X86_CPUID_FEATURE_ECX_SSE3);
2593 PORTABLE_DISABLE_FEATURE_BIT( 3, pStdFeatureLeaf->uEdx, SSE2, X86_CPUID_FEATURE_EDX_SSE2);
2594 PORTABLE_DISABLE_FEATURE_BIT( 3, pStdFeatureLeaf->uEdx, SSE, X86_CPUID_FEATURE_EDX_SSE);
2595 PORTABLE_DISABLE_FEATURE_BIT( 3, pStdFeatureLeaf->uEdx, CLFSH, X86_CPUID_FEATURE_EDX_CLFSH);
2596 PORTABLE_DISABLE_FEATURE_BIT( 3, pStdFeatureLeaf->uEdx, CMOV, X86_CPUID_FEATURE_EDX_CMOV);
2597
2598 Assert(!(pStdFeatureLeaf->uEdx & ( X86_CPUID_FEATURE_EDX_SEP
2599 | X86_CPUID_FEATURE_EDX_PSN
2600 | X86_CPUID_FEATURE_EDX_DS
2601 | X86_CPUID_FEATURE_EDX_ACPI
2602 | X86_CPUID_FEATURE_EDX_SS
2603 | X86_CPUID_FEATURE_EDX_TM
2604 | X86_CPUID_FEATURE_EDX_PBE
2605 )));
2606 Assert(!(pStdFeatureLeaf->uEcx & ( X86_CPUID_FEATURE_ECX_DTES64
2607 | X86_CPUID_FEATURE_ECX_CPLDS
2608 | X86_CPUID_FEATURE_ECX_VMX
2609 | X86_CPUID_FEATURE_ECX_SMX
2610 | X86_CPUID_FEATURE_ECX_EST
2611 | X86_CPUID_FEATURE_ECX_TM2
2612 | X86_CPUID_FEATURE_ECX_CNTXID
2613 | X86_CPUID_FEATURE_ECX_FMA
2614 | X86_CPUID_FEATURE_ECX_TPRUPDATE
2615 | X86_CPUID_FEATURE_ECX_PDCM
2616 | X86_CPUID_FEATURE_ECX_DCA
2617 | X86_CPUID_FEATURE_ECX_OSXSAVE
2618 )));
2619 }
2620
2621 /* Set up APIC ID for CPU 0, configure multi core/threaded smp. */
2622 pStdFeatureLeaf->uEbx &= UINT32_C(0x0000ffff); /* (APIC-ID := 0 and #LogCpus := 0) */
2623#ifdef VBOX_WITH_MULTI_CORE
2624 if (pVM->cCpus > 1)
2625 {
2626 /* If CPUID Fn0000_0001_EDX[HTT] = 1 then LogicalProcessorCount is the number of threads per CPU
2627 core times the number of CPU cores per processor */
2628 pStdFeatureLeaf->uEbx |= pVM->cCpus <= 0xff ? (pVM->cCpus << 16) : UINT32_C(0x00ff0000);
2629 pStdFeatureLeaf->uEdx |= X86_CPUID_FEATURE_EDX_HTT; /* necessary for hyper-threading *or* multi-core CPUs */
2630 }
2631#endif
2632
2633 /* Force standard feature bits. */
2634 if (pConfig->enmPClMul == CPUMISAEXTCFG_ENABLED_ALWAYS)
2635 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_PCLMUL;
2636 if (pConfig->enmMonitor == CPUMISAEXTCFG_ENABLED_ALWAYS)
2637 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_MONITOR;
2638 if (pConfig->enmCmpXchg16b == CPUMISAEXTCFG_ENABLED_ALWAYS)
2639 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_CX16;
2640 if (pConfig->enmSse41 == CPUMISAEXTCFG_ENABLED_ALWAYS)
2641 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_SSE4_1;
2642 if (pConfig->enmSse42 == CPUMISAEXTCFG_ENABLED_ALWAYS)
2643 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_SSE4_2;
2644 if (pConfig->enmMovBe == CPUMISAEXTCFG_ENABLED_ALWAYS)
2645 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_MOVBE;
2646 if (pConfig->enmPopCnt == CPUMISAEXTCFG_ENABLED_ALWAYS)
2647 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_POPCNT;
2648 if (pConfig->enmAesNi == CPUMISAEXTCFG_ENABLED_ALWAYS)
2649 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_AES;
2650 if (pConfig->enmXSave == CPUMISAEXTCFG_ENABLED_ALWAYS)
2651 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_XSAVE;
2652 if (pConfig->enmAvx == CPUMISAEXTCFG_ENABLED_ALWAYS)
2653 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_AVX;
2654 if (pConfig->enmRdRand == CPUMISAEXTCFG_ENABLED_ALWAYS)
2655 pStdFeatureLeaf->uEcx |= X86_CPUID_FEATURE_ECX_RDRAND;
2656
2657 pStdFeatureLeaf = NULL; /* Must refetch! */
2658
2659 /* Cpuid 0x80000001: (Similar, but in no way identical to 0x00000001.)
2660 * AMD:
2661 * EAX: CPU model, family and stepping.
2662 *
2663 * ECX + EDX: Supported features. Only report features we can support.
2664 * Note! When enabling new features the Synthetic CPU and Portable CPUID
2665 * options may require adjusting (i.e. stripping what was enabled).
2666 * ASSUMES that this is ALWAYS the AMD defined feature set if present.
2667 *
2668 * EBX: Branding ID and package type (or reserved).
2669 *
2670 * Intel and probably most others:
2671 * EAX: 0
2672 * EBX: 0
2673 * ECX + EDX: Subset of AMD features, mainly for AMD64 support.
2674 */
2675 PCPUMCPUIDLEAF pExtFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x80000001), 0);
2676 if (pExtFeatureLeaf)
2677 {
2678 pExtFeatureLeaf = cpumR3CpuIdMakeSingleLeaf(pCpum, pExtFeatureLeaf);
2679
2680 pExtFeatureLeaf->uEdx &= X86_CPUID_AMD_FEATURE_EDX_FPU
2681 | X86_CPUID_AMD_FEATURE_EDX_VME
2682 | X86_CPUID_AMD_FEATURE_EDX_DE
2683 | X86_CPUID_AMD_FEATURE_EDX_PSE
2684 | X86_CPUID_AMD_FEATURE_EDX_TSC
2685 | X86_CPUID_AMD_FEATURE_EDX_MSR //?? this means AMD MSRs..
2686 //| X86_CPUID_AMD_FEATURE_EDX_PAE - turned on when necessary
2687 //| X86_CPUID_AMD_FEATURE_EDX_MCE - not virtualized yet.
2688 | X86_CPUID_AMD_FEATURE_EDX_CX8
2689 //| X86_CPUID_AMD_FEATURE_EDX_APIC - set by the APIC device if present.
2690 //| RT_BIT_32(10) - reserved
2691 /* Note! We don't report sysenter/sysexit support due to our inability to keep the IOPL part of
2692 eflags in sync while in ring 1 (see @bugref{1757}). HM enables them later. */
2693 //| X86_CPUID_EXT_FEATURE_EDX_SYSCALL
2694 | X86_CPUID_AMD_FEATURE_EDX_MTRR
2695 | X86_CPUID_AMD_FEATURE_EDX_PGE
2696 | X86_CPUID_AMD_FEATURE_EDX_MCA
2697 | X86_CPUID_AMD_FEATURE_EDX_CMOV
2698 | X86_CPUID_AMD_FEATURE_EDX_PAT
2699 | X86_CPUID_AMD_FEATURE_EDX_PSE36
2700 //| RT_BIT_32(18) - reserved
2701 //| RT_BIT_32(19) - reserved
2702 //| X86_CPUID_EXT_FEATURE_EDX_NX - enabled later by PGM
2703 //| RT_BIT_32(21) - reserved
2704 | (pConfig->enmAmdExtMmx ? X86_CPUID_AMD_FEATURE_EDX_AXMMX : 0)
2705 | X86_CPUID_AMD_FEATURE_EDX_MMX
2706 | X86_CPUID_AMD_FEATURE_EDX_FXSR
2707 | X86_CPUID_AMD_FEATURE_EDX_FFXSR
2708 //| X86_CPUID_EXT_FEATURE_EDX_PAGE1GB
2709 | X86_CPUID_EXT_FEATURE_EDX_RDTSCP
2710 //| RT_BIT_32(28) - reserved
2711 //| X86_CPUID_EXT_FEATURE_EDX_LONG_MODE - turned on when necessary
2712 | X86_CPUID_AMD_FEATURE_EDX_3DNOW_EX
2713 | X86_CPUID_AMD_FEATURE_EDX_3DNOW
2714 ;
2715 pExtFeatureLeaf->uEcx &= X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF
2716 //| X86_CPUID_AMD_FEATURE_ECX_CMPL - set below if applicable.
2717 //| X86_CPUID_AMD_FEATURE_ECX_SVM - not virtualized.
2718 //| X86_CPUID_AMD_FEATURE_ECX_EXT_APIC
2719 /* Note: This could prevent teleporting from AMD to Intel CPUs! */
2720 | X86_CPUID_AMD_FEATURE_ECX_CR8L /* expose lock mov cr0 = mov cr8 hack for guests that can use this feature to access the TPR. */
2721 | (pConfig->enmAbm ? X86_CPUID_AMD_FEATURE_ECX_ABM : 0)
2722 | (pConfig->enmSse4A ? X86_CPUID_AMD_FEATURE_ECX_SSE4A : 0)
2723 | (pConfig->enmMisAlnSse ? X86_CPUID_AMD_FEATURE_ECX_MISALNSSE : 0)
2724 | (pConfig->enm3dNowPrf ? X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF : 0)
2725 //| X86_CPUID_AMD_FEATURE_ECX_OSVW
2726 //| X86_CPUID_AMD_FEATURE_ECX_IBS
2727 //| X86_CPUID_AMD_FEATURE_ECX_XOP
2728 //| X86_CPUID_AMD_FEATURE_ECX_SKINIT
2729 //| X86_CPUID_AMD_FEATURE_ECX_WDT
2730 //| RT_BIT_32(14) - reserved
2731 //| X86_CPUID_AMD_FEATURE_ECX_LWP - not supported
2732 //| X86_CPUID_AMD_FEATURE_ECX_FMA4 - not yet virtualized.
2733 //| RT_BIT_32(17) - reserved
2734 //| RT_BIT_32(18) - reserved
2735 //| X86_CPUID_AMD_FEATURE_ECX_NODEID - not yet virtualized.
2736 //| RT_BIT_32(20) - reserved
2737 //| X86_CPUID_AMD_FEATURE_ECX_TBM - not yet virtualized.
2738 //| X86_CPUID_AMD_FEATURE_ECX_TOPOEXT - not yet virtualized.
2739 //| RT_BIT_32(23) - reserved
2740 //| RT_BIT_32(24) - reserved
2741 //| RT_BIT_32(25) - reserved
2742 //| RT_BIT_32(26) - reserved
2743 //| RT_BIT_32(27) - reserved
2744 //| RT_BIT_32(28) - reserved
2745 //| RT_BIT_32(29) - reserved
2746 //| RT_BIT_32(30) - reserved
2747 //| RT_BIT_32(31) - reserved
2748 ;
2749#ifdef VBOX_WITH_MULTI_CORE
2750 if ( pVM->cCpus > 1
2751 && pCpum->GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_AMD)
2752 pExtFeatureLeaf->uEcx |= X86_CPUID_AMD_FEATURE_ECX_CMPL; /* CmpLegacy */
2753#endif
2754
2755 if (pCpum->u8PortableCpuIdLevel > 0)
2756 {
2757 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEcx, CR8L, X86_CPUID_AMD_FEATURE_ECX_CR8L);
2758 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pExtFeatureLeaf->uEcx, ABM, X86_CPUID_AMD_FEATURE_ECX_ABM, pConfig->enmAbm);
2759 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pExtFeatureLeaf->uEcx, SSE4A, X86_CPUID_AMD_FEATURE_ECX_SSE4A, pConfig->enmSse4A);
2760 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pExtFeatureLeaf->uEcx, MISALNSSE, X86_CPUID_AMD_FEATURE_ECX_MISALNSSE, pConfig->enmMisAlnSse);
2761 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pExtFeatureLeaf->uEcx, 3DNOWPRF, X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF, pConfig->enm3dNowPrf);
2762 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEcx, XOP, X86_CPUID_AMD_FEATURE_ECX_XOP);
2763 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEcx, TBM, X86_CPUID_AMD_FEATURE_ECX_TBM);
2764 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEcx, FMA4, X86_CPUID_AMD_FEATURE_ECX_FMA4);
2765 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pExtFeatureLeaf->uEdx, AXMMX, X86_CPUID_AMD_FEATURE_EDX_AXMMX, pConfig->enmAmdExtMmx);
2766 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEdx, 3DNOW, X86_CPUID_AMD_FEATURE_EDX_3DNOW);
2767 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEdx, 3DNOW_EX, X86_CPUID_AMD_FEATURE_EDX_3DNOW_EX);
2768 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEdx, FFXSR, X86_CPUID_AMD_FEATURE_EDX_FFXSR);
2769 PORTABLE_DISABLE_FEATURE_BIT( 1, pExtFeatureLeaf->uEdx, RDTSCP, X86_CPUID_EXT_FEATURE_EDX_RDTSCP);
2770 PORTABLE_DISABLE_FEATURE_BIT( 2, pExtFeatureLeaf->uEcx, LAHF_SAHF, X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF);
2771 PORTABLE_DISABLE_FEATURE_BIT( 3, pExtFeatureLeaf->uEcx, CMOV, X86_CPUID_AMD_FEATURE_EDX_CMOV);
2772
2773 Assert(!(pExtFeatureLeaf->uEcx & ( X86_CPUID_AMD_FEATURE_ECX_SVM
2774 | X86_CPUID_AMD_FEATURE_ECX_EXT_APIC
2775 | X86_CPUID_AMD_FEATURE_ECX_OSVW
2776 | X86_CPUID_AMD_FEATURE_ECX_IBS
2777 | X86_CPUID_AMD_FEATURE_ECX_SKINIT
2778 | X86_CPUID_AMD_FEATURE_ECX_WDT
2779 | X86_CPUID_AMD_FEATURE_ECX_LWP
2780 | X86_CPUID_AMD_FEATURE_ECX_NODEID
2781 | X86_CPUID_AMD_FEATURE_ECX_TOPOEXT
2782 | UINT32_C(0xff964000)
2783 )));
2784 Assert(!(pExtFeatureLeaf->uEdx & ( RT_BIT(10)
2785 | X86_CPUID_EXT_FEATURE_EDX_SYSCALL
2786 | RT_BIT(18)
2787 | RT_BIT(19)
2788 | RT_BIT(21)
2789 | X86_CPUID_AMD_FEATURE_EDX_AXMMX
2790 | X86_CPUID_EXT_FEATURE_EDX_PAGE1GB
2791 | RT_BIT(28)
2792 )));
2793 }
2794
2795 /* Force extended feature bits. */
2796 if (pConfig->enmAbm == CPUMISAEXTCFG_ENABLED_ALWAYS)
2797 pExtFeatureLeaf->uEcx |= X86_CPUID_AMD_FEATURE_ECX_ABM;
2798 if (pConfig->enmSse4A == CPUMISAEXTCFG_ENABLED_ALWAYS)
2799 pExtFeatureLeaf->uEcx |= X86_CPUID_AMD_FEATURE_ECX_SSE4A;
2800 if (pConfig->enmMisAlnSse == CPUMISAEXTCFG_ENABLED_ALWAYS)
2801 pExtFeatureLeaf->uEcx |= X86_CPUID_AMD_FEATURE_ECX_MISALNSSE;
2802 if (pConfig->enm3dNowPrf == CPUMISAEXTCFG_ENABLED_ALWAYS)
2803 pExtFeatureLeaf->uEcx |= X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF;
2804 if (pConfig->enmAmdExtMmx == CPUMISAEXTCFG_ENABLED_ALWAYS)
2805 pExtFeatureLeaf->uEdx |= X86_CPUID_AMD_FEATURE_EDX_AXMMX;
2806 }
2807 pExtFeatureLeaf = NULL; /* Must refetch! */
2808
2809
2810 /* Cpuid 2:
2811 * Intel: (Nondeterministic) Cache and TLB information
2812 * AMD: Reserved
2813 * VIA: Reserved
2814 * Safe to expose.
2815 */
2816 uint32_t uSubLeaf = 0;
2817 PCPUMCPUIDLEAF pCurLeaf;
2818 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 2, uSubLeaf)) != NULL)
2819 {
2820 if ((pCurLeaf->uEax & 0xff) > 1)
2821 {
2822 LogRel(("CpuId: Std[2].al: %d -> 1\n", pCurLeaf->uEax & 0xff));
2823 pCurLeaf->uEax &= UINT32_C(0xffffff01);
2824 }
2825 uSubLeaf++;
2826 }
2827
2828 /* Cpuid 3:
2829 * Intel: EAX, EBX - reserved (transmeta uses these)
2830 * ECX, EDX - Processor Serial Number if available, otherwise reserved
2831 * AMD: Reserved
2832 * VIA: Reserved
2833 * Safe to expose
2834 */
2835 pStdFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 1, 0);
2836 if (!(pStdFeatureLeaf->uEdx & X86_CPUID_FEATURE_EDX_PSN))
2837 {
2838 uSubLeaf = 0;
2839 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 3, uSubLeaf)) != NULL)
2840 {
2841 pCurLeaf->uEcx = pCurLeaf->uEdx = 0;
2842 if (pCpum->u8PortableCpuIdLevel > 0)
2843 pCurLeaf->uEax = pCurLeaf->uEbx = 0;
2844 uSubLeaf++;
2845 }
2846 }
2847
2848 /* Cpuid 4 + ECX:
2849 * Intel: Deterministic Cache Parameters Leaf.
2850 * AMD: Reserved
2851 * VIA: Reserved
2852 * Safe to expose, except for EAX:
2853 * Bits 25-14: Maximum number of addressable IDs for logical processors sharing this cache (see note)**
2854 * Bits 31-26: Maximum number of processor cores in this physical package**
2855 * Note: These SMP values are constant regardless of ECX
2856 */
2857 uSubLeaf = 0;
2858 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 4, uSubLeaf)) != NULL)
2859 {
2860 pCurLeaf->uEax &= UINT32_C(0x00003fff); /* Clear the #maxcores, #threads-sharing-cache (both are #-1).*/
2861#ifdef VBOX_WITH_MULTI_CORE
2862 if ( pVM->cCpus > 1
2863 && pCpum->GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_INTEL)
2864 {
2865 AssertReturn(pVM->cCpus <= 64, VERR_TOO_MANY_CPUS);
2866 /* One logical processor with possibly multiple cores. */
2867 /* See http://www.intel.com/Assets/PDF/appnote/241618.pdf p. 29 */
2868 pCurLeaf->uEax |= pVM->cCpus <= 0x40 ? ((pVM->cCpus - 1) << 26) : UINT32_C(0xfc000000); /* 6 bits only -> 64 cores! */
2869 }
2870#endif
2871 uSubLeaf++;
2872 }
2873
2874 /* Cpuid 5: Monitor/mwait Leaf
2875 * Intel: ECX, EDX - reserved
2876 * EAX, EBX - Smallest and largest monitor line size
2877 * AMD: EDX - reserved
2878 * EAX, EBX - Smallest and largest monitor line size
2879 * ECX - extensions (ignored for now)
2880 * VIA: Reserved
2881 * Safe to expose
2882 */
2883 uSubLeaf = 0;
2884 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 5, uSubLeaf)) != NULL)
2885 {
2886 pStdFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 1, 0);
2887 if (!(pStdFeatureLeaf->uEcx & X86_CPUID_FEATURE_ECX_MONITOR))
2888 pCurLeaf->uEax = pCurLeaf->uEbx = 0;
2889
2890 pCurLeaf->uEcx = pCurLeaf->uEdx = 0;
2891 if (pConfig->enmMWaitExtensions)
2892 {
2893 pCurLeaf->uEcx = X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0;
2894 /** @todo: for now we just expose host's MWAIT C-states, although conceptually
2895 it shall be part of our power management virtualization model */
2896#if 0
2897 /* MWAIT sub C-states */
2898 pCurLeaf->uEdx =
2899 (0 << 0) /* 0 in C0 */ |
2900 (2 << 4) /* 2 in C1 */ |
2901 (2 << 8) /* 2 in C2 */ |
2902 (2 << 12) /* 2 in C3 */ |
2903 (0 << 16) /* 0 in C4 */
2904 ;
2905#endif
2906 }
2907 else
2908 pCurLeaf->uEcx = pCurLeaf->uEdx = 0;
2909 uSubLeaf++;
2910 }
2911
2912 /* Cpuid 6: Digital Thermal Sensor and Power Management Paramenters.
2913 * Intel: Various stuff.
2914 * AMD: EAX, EBX, EDX - reserved.
2915 * ECX - Bit zero is EffFreq, indicating MSR_0000_00e7 and MSR_0000_00e8
2916 * present. Same as intel.
2917 * VIA: ??
2918 *
2919 * We clear everything here for now.
2920 */
2921 cpumR3CpuIdZeroLeaf(pCpum, 6);
2922
2923 /* Cpuid 7 + ECX: Structured Extended Feature Flags Enumeration
2924 * EAX: Number of sub leaves.
2925 * EBX+ECX+EDX: Feature flags
2926 *
2927 * We only have documentation for one sub-leaf, so clear all other (no need
2928 * to remove them as such, just set them to zero).
2929 *
2930 * Note! When enabling new features the Synthetic CPU and Portable CPUID
2931 * options may require adjusting (i.e. stripping what was enabled).
2932 */
2933 uSubLeaf = 0;
2934 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 7, uSubLeaf)) != NULL)
2935 {
2936 switch (uSubLeaf)
2937 {
2938 case 0:
2939 {
2940 pCurLeaf->uEax = 0; /* Max ECX input is 0. */
2941 pCurLeaf->uEbx &= 0
2942 //| X86_CPUID_STEXT_FEATURE_EBX_FSGSBASE RT_BIT(0)
2943 //| X86_CPUID_STEXT_FEATURE_EBX_TSC_ADJUST RT_BIT(1)
2944 //| RT_BIT(2) - reserved
2945 //| X86_CPUID_STEXT_FEATURE_EBX_BMI1 RT_BIT(3)
2946 //| X86_CPUID_STEXT_FEATURE_EBX_HLE RT_BIT(4)
2947 | (pConfig->enmAvx2 ? X86_CPUID_STEXT_FEATURE_EBX_AVX2 : 0)
2948 //| RT_BIT(6) - reserved
2949 //| X86_CPUID_STEXT_FEATURE_EBX_SMEP RT_BIT(7)
2950 //| X86_CPUID_STEXT_FEATURE_EBX_BMI2 RT_BIT(8)
2951 //| X86_CPUID_STEXT_FEATURE_EBX_ERMS RT_BIT(9)
2952 //| X86_CPUID_STEXT_FEATURE_EBX_INVPCID RT_BIT(10)
2953 //| X86_CPUID_STEXT_FEATURE_EBX_RTM RT_BIT(11)
2954 //| X86_CPUID_STEXT_FEATURE_EBX_PQM RT_BIT(12)
2955 | X86_CPUID_STEXT_FEATURE_EBX_DEPR_FPU_CS_DS
2956 //| X86_CPUID_STEXT_FEATURE_EBX_MPE RT_BIT(14)
2957 //| X86_CPUID_STEXT_FEATURE_EBX_PQE RT_BIT(15)
2958 //| X86_CPUID_STEXT_FEATURE_EBX_AVX512F RT_BIT(16)
2959 //| RT_BIT(17) - reserved
2960 | (pConfig->enmRdSeed ? X86_CPUID_STEXT_FEATURE_EBX_RDSEED : 0)
2961 //| X86_CPUID_STEXT_FEATURE_EBX_ADX RT_BIT(19)
2962 //| X86_CPUID_STEXT_FEATURE_EBX_SMAP RT_BIT(20)
2963 //| RT_BIT(21) - reserved
2964 //| RT_BIT(22) - reserved
2965 | (pConfig->enmCLFlushOpt ? X86_CPUID_STEXT_FEATURE_EBX_CLFLUSHOPT : 0)
2966 //| RT_BIT(24) - reserved
2967 //| X86_CPUID_STEXT_FEATURE_EBX_INTEL_PT RT_BIT(25)
2968 //| X86_CPUID_STEXT_FEATURE_EBX_AVX512PF RT_BIT(26)
2969 //| X86_CPUID_STEXT_FEATURE_EBX_AVX512ER RT_BIT(27)
2970 //| X86_CPUID_STEXT_FEATURE_EBX_AVX512CD RT_BIT(28)
2971 //| X86_CPUID_STEXT_FEATURE_EBX_SHA RT_BIT(29)
2972 //| RT_BIT(30) - reserved
2973 //| RT_BIT(31) - reserved
2974 ;
2975 pCurLeaf->uEcx &= 0
2976 //| X86_CPUID_STEXT_FEATURE_ECX_PREFETCHWT1 - we do not do vector functions yet.
2977 ;
2978 pCurLeaf->uEdx &= 0;
2979
2980 if (pCpum->u8PortableCpuIdLevel > 0)
2981 {
2982 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, FSGSBASE, X86_CPUID_STEXT_FEATURE_EBX_FSGSBASE);
2983 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pCurLeaf->uEbx, AVX2, X86_CPUID_STEXT_FEATURE_EBX_AVX2, pConfig->enmAvx2);
2984 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, SMEP, X86_CPUID_STEXT_FEATURE_EBX_SMEP);
2985 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, BMI2, X86_CPUID_STEXT_FEATURE_EBX_BMI2);
2986 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, INVPCID, X86_CPUID_STEXT_FEATURE_EBX_INVPCID);
2987 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, AVX512F, X86_CPUID_STEXT_FEATURE_EBX_AVX512F);
2988 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pCurLeaf->uEbx, RDSEED, X86_CPUID_STEXT_FEATURE_EBX_RDSEED, pConfig->enmRdSeed);
2989 PORTABLE_DISABLE_FEATURE_BIT_CFG(1, pCurLeaf->uEbx, CLFLUSHOPT, X86_CPUID_STEXT_FEATURE_EBX_RDSEED, pConfig->enmCLFlushOpt);
2990 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, AVX512PF, X86_CPUID_STEXT_FEATURE_EBX_AVX512PF);
2991 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, AVX512ER, X86_CPUID_STEXT_FEATURE_EBX_AVX512ER);
2992 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, AVX512CD, X86_CPUID_STEXT_FEATURE_EBX_AVX512CD);
2993 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, SMAP, X86_CPUID_STEXT_FEATURE_EBX_SMAP);
2994 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEbx, SHA, X86_CPUID_STEXT_FEATURE_EBX_SHA);
2995 PORTABLE_DISABLE_FEATURE_BIT( 1, pCurLeaf->uEcx, PREFETCHWT1, X86_CPUID_STEXT_FEATURE_ECX_PREFETCHWT1);
2996 }
2997
2998 /* Force standard feature bits. */
2999 if (pConfig->enmAvx2 == CPUMISAEXTCFG_ENABLED_ALWAYS)
3000 pCurLeaf->uEbx |= X86_CPUID_STEXT_FEATURE_EBX_AVX2;
3001 if (pConfig->enmRdSeed == CPUMISAEXTCFG_ENABLED_ALWAYS)
3002 pCurLeaf->uEbx |= X86_CPUID_STEXT_FEATURE_EBX_RDSEED;
3003 if (pConfig->enmCLFlushOpt == CPUMISAEXTCFG_ENABLED_ALWAYS)
3004 pCurLeaf->uEbx |= X86_CPUID_STEXT_FEATURE_EBX_CLFLUSHOPT;
3005 break;
3006 }
3007
3008 default:
3009 /* Invalid index, all values are zero. */
3010 pCurLeaf->uEax = 0;
3011 pCurLeaf->uEbx = 0;
3012 pCurLeaf->uEcx = 0;
3013 pCurLeaf->uEdx = 0;
3014 break;
3015 }
3016 uSubLeaf++;
3017 }
3018
3019 /* Cpuid 8: Marked as reserved by Intel and AMD.
3020 * We zero this since we don't know what it may have been used for.
3021 */
3022 cpumR3CpuIdZeroLeaf(pCpum, 8);
3023
3024 /* Cpuid 9: Direct Cache Access (DCA) Parameters
3025 * Intel: EAX - Value of PLATFORM_DCA_CAP bits.
3026 * EBX, ECX, EDX - reserved.
3027 * AMD: Reserved
3028 * VIA: ??
3029 *
3030 * We zero this.
3031 */
3032 cpumR3CpuIdZeroLeaf(pCpum, 9);
3033
3034 /* Cpuid 0xa: Architectural Performance Monitor Features
3035 * Intel: EAX - Value of PLATFORM_DCA_CAP bits.
3036 * EBX, ECX, EDX - reserved.
3037 * AMD: Reserved
3038 * VIA: ??
3039 *
3040 * We zero this, for now at least.
3041 */
3042 cpumR3CpuIdZeroLeaf(pCpum, 10);
3043
3044 /* Cpuid 0xb+ECX: x2APIC Features / Processor Topology.
3045 * Intel: EAX - APCI ID shift right for next level.
3046 * EBX - Factory configured cores/threads at this level.
3047 * ECX - Level number (same as input) and level type (1,2,0).
3048 * EDX - Extended initial APIC ID.
3049 * AMD: Reserved
3050 * VIA: ??
3051 */
3052 uSubLeaf = 0;
3053 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 11, uSubLeaf)) != NULL)
3054 {
3055 if (pCurLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC_ID)
3056 {
3057 uint8_t bLevelType = RT_BYTE2(pCurLeaf->uEcx);
3058 if (bLevelType == 1)
3059 {
3060 /* Thread level - we don't do threads at the moment. */
3061 pCurLeaf->uEax = 0; /** @todo is this correct? Real CPUs never do 0 here, I think... */
3062 pCurLeaf->uEbx = 1;
3063 }
3064 else if (bLevelType == 2)
3065 {
3066 /* Core level. */
3067 pCurLeaf->uEax = 1; /** @todo real CPUs are supposed to be in the 4-6 range, not 1. Our APIC ID assignments are a little special... */
3068#ifdef VBOX_WITH_MULTI_CORE
3069 while (RT_BIT_32(pCurLeaf->uEax) < pVM->cCpus)
3070 pCurLeaf->uEax++;
3071#endif
3072 pCurLeaf->uEbx = pVM->cCpus;
3073 }
3074 else
3075 {
3076 AssertLogRelMsg(bLevelType == 0, ("bLevelType=%#x uSubLeaf=%#x\n", bLevelType, uSubLeaf));
3077 pCurLeaf->uEax = 0;
3078 pCurLeaf->uEbx = 0;
3079 pCurLeaf->uEcx = 0;
3080 }
3081 pCurLeaf->uEcx = (pCurLeaf->uEcx & UINT32_C(0xffffff00)) | (uSubLeaf & 0xff);
3082 pCurLeaf->uEdx = 0; /* APIC ID is filled in by CPUMGetGuestCpuId() at runtime. Init for EMT(0) as usual. */
3083 }
3084 else
3085 {
3086 pCurLeaf->uEax = 0;
3087 pCurLeaf->uEbx = 0;
3088 pCurLeaf->uEcx = 0;
3089 pCurLeaf->uEdx = 0;
3090 }
3091 uSubLeaf++;
3092 }
3093
3094 /* Cpuid 0xc: Marked as reserved by Intel and AMD.
3095 * We zero this since we don't know what it may have been used for.
3096 */
3097 cpumR3CpuIdZeroLeaf(pCpum, 12);
3098
3099 /* Cpuid 0xd + ECX: Processor Extended State Enumeration
3100 * ECX=0: EAX - Valid bits in XCR0[31:0].
3101 * EBX - Maximum state size as per current XCR0 value.
3102 * ECX - Maximum state size for all supported features.
3103 * EDX - Valid bits in XCR0[63:32].
3104 * ECX=1: EAX - Various X-features.
3105 * EBX - Maximum state size as per current XCR0|IA32_XSS value.
3106 * ECX - Valid bits in IA32_XSS[31:0].
3107 * EDX - Valid bits in IA32_XSS[63:32].
3108 * ECX=N, where N in 2..63 and indicates a bit in XCR0 and/or IA32_XSS,
3109 * if the bit invalid all four registers are set to zero.
3110 * EAX - The state size for this feature.
3111 * EBX - The state byte offset of this feature.
3112 * ECX - Bit 0 indicates whether this sub-leaf maps to a valid IA32_XSS bit (=1) or a valid XCR0 bit (=0).
3113 * EDX - Reserved, but is set to zero if invalid sub-leaf index.
3114 *
3115 * Clear them all as we don't currently implement extended CPU state.
3116 */
3117 /* Figure out the supported XCR0/XSS mask component. */
3118 uint64_t fGuestXcr0Mask = 0;
3119 pStdFeatureLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 1, 0);
3120 if (pStdFeatureLeaf && (pStdFeatureLeaf->uEcx & X86_CPUID_FEATURE_ECX_XSAVE))
3121 {
3122 fGuestXcr0Mask = XSAVE_C_X87 | XSAVE_C_SSE;
3123 if (pStdFeatureLeaf && (pStdFeatureLeaf->uEcx & X86_CPUID_FEATURE_ECX_AVX))
3124 fGuestXcr0Mask |= XSAVE_C_YMM;
3125 pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 7, 0);
3126 if (pCurLeaf && (pCurLeaf->uEbx & X86_CPUID_STEXT_FEATURE_EBX_AVX512F))
3127 fGuestXcr0Mask |= XSAVE_C_ZMM_16HI | XSAVE_C_ZMM_HI256 | XSAVE_C_OPMASK;
3128 fGuestXcr0Mask &= pCpum->fXStateHostMask;
3129 }
3130 pStdFeatureLeaf = NULL;
3131 pCpum->fXStateGuestMask = fGuestXcr0Mask;
3132
3133 /* Work the sub-leaves. */
3134 uint32_t cbXSaveMax = sizeof(X86FXSTATE);
3135 for (uSubLeaf = 0; uSubLeaf < 63; uSubLeaf++)
3136 {
3137 pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, 13, uSubLeaf);
3138 if (pCurLeaf)
3139 {
3140 if (fGuestXcr0Mask)
3141 {
3142 switch (uSubLeaf)
3143 {
3144 case 0:
3145 pCurLeaf->uEax &= RT_LO_U32(fGuestXcr0Mask);
3146 pCurLeaf->uEdx &= RT_HI_U32(fGuestXcr0Mask);
3147 AssertLogRelMsgReturn((pCurLeaf->uEax & (XSAVE_C_X87 | XSAVE_C_SSE)) == (XSAVE_C_X87 | XSAVE_C_SSE),
3148 ("CPUID(0xd/0).EAX missing mandatory X87 or SSE bits: %#RX32", pCurLeaf->uEax),
3149 VERR_CPUM_IPE_1);
3150 cbXSaveMax = pCurLeaf->uEcx;
3151 AssertLogRelMsgReturn(cbXSaveMax <= CPUM_MAX_XSAVE_AREA_SIZE && cbXSaveMax >= CPUM_MIN_XSAVE_AREA_SIZE,
3152 ("%#x max=%#x\n", cbXSaveMax, CPUM_MAX_XSAVE_AREA_SIZE), VERR_CPUM_IPE_2);
3153 AssertLogRelMsgReturn(pCurLeaf->uEbx >= CPUM_MIN_XSAVE_AREA_SIZE && pCurLeaf->uEbx <= cbXSaveMax,
3154 ("ebx=%#x cbXSaveMax=%#x\n", pCurLeaf->uEbx, cbXSaveMax),
3155 VERR_CPUM_IPE_2);
3156 continue;
3157 case 1:
3158 pCurLeaf->uEax &= 0;
3159 pCurLeaf->uEcx &= 0;
3160 pCurLeaf->uEdx &= 0;
3161 /** @todo what about checking ebx? */
3162 continue;
3163 default:
3164 if (fGuestXcr0Mask & RT_BIT_64(uSubLeaf))
3165 {
3166 AssertLogRelMsgReturn( pCurLeaf->uEax <= cbXSaveMax
3167 && pCurLeaf->uEax > 0
3168 && pCurLeaf->uEbx < cbXSaveMax
3169 && pCurLeaf->uEbx >= CPUM_MIN_XSAVE_AREA_SIZE
3170 && pCurLeaf->uEbx + pCurLeaf->uEax <= cbXSaveMax,
3171 ("%#x: eax=%#x ebx=%#x cbMax=%#x\n",
3172 uSubLeaf, pCurLeaf->uEax, pCurLeaf->uEbx, cbXSaveMax),
3173 VERR_CPUM_IPE_2);
3174 AssertLogRel(!(pCurLeaf->uEcx & 1));
3175 pCurLeaf->uEcx = 0; /* Bit 0 should be zero (XCR0), the reset are reserved... */
3176 pCurLeaf->uEdx = 0; /* it's reserved... */
3177 continue;
3178 }
3179 break;
3180 }
3181 }
3182
3183 /* Clear the leaf. */
3184 pCurLeaf->uEax = 0;
3185 pCurLeaf->uEbx = 0;
3186 pCurLeaf->uEcx = 0;
3187 pCurLeaf->uEdx = 0;
3188 }
3189 }
3190
3191 /* Cpuid 0xe: Marked as reserved by Intel and AMD.
3192 * We zero this since we don't know what it may have been used for.
3193 */
3194 cpumR3CpuIdZeroLeaf(pCpum, 14);
3195
3196 /* Cpuid 0xf + ECX: Platform qualifity of service monitoring (PQM).
3197 * We zero this as we don't currently virtualize PQM.
3198 */
3199 cpumR3CpuIdZeroLeaf(pCpum, 15);
3200
3201 /* Cpuid 0x10 + ECX: Platform qualifity of service enforcement (PQE).
3202 * We zero this as we don't currently virtualize PQE.
3203 */
3204 cpumR3CpuIdZeroLeaf(pCpum, 16);
3205
3206 /* Cpuid 0x11: Marked as reserved by Intel and AMD.
3207 * We zero this since we don't know what it may have been used for.
3208 */
3209 cpumR3CpuIdZeroLeaf(pCpum, 17);
3210
3211 /* Cpuid 0x12 + ECX: SGX resource enumeration.
3212 * We zero this as we don't currently virtualize this.
3213 */
3214 cpumR3CpuIdZeroLeaf(pCpum, 18);
3215
3216 /* Cpuid 0x13: Marked as reserved by Intel and AMD.
3217 * We zero this since we don't know what it may have been used for.
3218 */
3219 cpumR3CpuIdZeroLeaf(pCpum, 19);
3220
3221 /* Cpuid 0x14 + ECX: Processor Trace (PT) capability enumeration.
3222 * We zero this as we don't currently virtualize this.
3223 */
3224 cpumR3CpuIdZeroLeaf(pCpum, 20);
3225
3226 /* Cpuid 0x15: Timestamp Counter / Core Crystal Clock info.
3227 * Intel: uTscFrequency = uCoreCrystalClockFrequency * EBX / EAX.
3228 * EAX - denominator (unsigned).
3229 * EBX - numerator (unsigned).
3230 * ECX, EDX - reserved.
3231 * AMD: Reserved / undefined / not implemented.
3232 * VIA: Reserved / undefined / not implemented.
3233 * We zero this as we don't currently virtualize this.
3234 */
3235 cpumR3CpuIdZeroLeaf(pCpum, 21);
3236
3237 /* Cpuid 0x16: Processor frequency info
3238 * Intel: EAX - Core base frequency in MHz.
3239 * EBX - Core maximum frequency in MHz.
3240 * ECX - Bus (reference) frequency in MHz.
3241 * EDX - Reserved.
3242 * AMD: Reserved / undefined / not implemented.
3243 * VIA: Reserved / undefined / not implemented.
3244 * We zero this as we don't currently virtualize this.
3245 */
3246 cpumR3CpuIdZeroLeaf(pCpum, 22);
3247
3248 /* Cpuid 0x17..0x10000000: Unknown.
3249 * We don't know these and what they mean, so remove them. */
3250 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
3251 UINT32_C(0x00000017), UINT32_C(0x0fffffff));
3252
3253
3254 /* CpuId 0x40000000..0x4fffffff: Reserved for hypervisor/emulator.
3255 * We remove all these as we're a hypervisor and must provide our own.
3256 */
3257 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
3258 UINT32_C(0x40000000), UINT32_C(0x4fffffff));
3259
3260
3261 /* Cpuid 0x80000000 is harmless. */
3262
3263 /* Cpuid 0x80000001 is handled with cpuid 1 way up above. */
3264
3265 /* Cpuid 0x80000002...0x80000004 contains the processor name and is considered harmless. */
3266
3267 /* Cpuid 0x800000005 & 0x800000006 contain information about L1, L2 & L3 cache and TLB identifiers.
3268 * Safe to pass on to the guest.
3269 *
3270 * AMD: 0x800000005 L1 cache information
3271 * 0x800000006 L2/L3 cache information
3272 * Intel: 0x800000005 reserved
3273 * 0x800000006 L2 cache information
3274 * VIA: 0x800000005 TLB and L1 cache information
3275 * 0x800000006 L2 cache information
3276 */
3277
3278 /* Cpuid 0x800000007: Advanced Power Management Information.
3279 * AMD: EAX: Processor feedback capabilities.
3280 * EBX: RAS capabilites.
3281 * ECX: Advanced power monitoring interface.
3282 * EDX: Enhanced power management capabilities.
3283 * Intel: EAX, EBX, ECX - reserved.
3284 * EDX - Invariant TSC indicator supported (bit 8), the rest is reserved.
3285 * VIA: Reserved
3286 * We let the guest see EDX_TSCINVAR (and later maybe EDX_EFRO). Actually, we should set EDX_TSCINVAR.
3287 */
3288 uSubLeaf = 0;
3289 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x80000007), uSubLeaf)) != NULL)
3290 {
3291 pCurLeaf->uEax = pCurLeaf->uEbx = pCurLeaf->uEcx = 0;
3292 if (pCpum->GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_AMD)
3293 {
3294 pCurLeaf->uEdx &= 0
3295 //| X86_CPUID_AMD_ADVPOWER_EDX_TS
3296 //| X86_CPUID_AMD_ADVPOWER_EDX_FID
3297 //| X86_CPUID_AMD_ADVPOWER_EDX_VID
3298 //| X86_CPUID_AMD_ADVPOWER_EDX_TTP
3299 //| X86_CPUID_AMD_ADVPOWER_EDX_TM
3300 //| X86_CPUID_AMD_ADVPOWER_EDX_STC
3301 //| X86_CPUID_AMD_ADVPOWER_EDX_MC
3302 //| X86_CPUID_AMD_ADVPOWER_EDX_HWPSTATE
3303#if 0 /*
3304 * We don't expose X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR, because newer
3305 * Linux kernels blindly assume that the AMD performance counters work
3306 * if this is set for 64 bits guests. (Can't really find a CPUID feature
3307 * bit for them though.)
3308 */
3309 /** @todo need to recheck this with new MSR emulation. */
3310 | X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR
3311#endif
3312 //| X86_CPUID_AMD_ADVPOWER_EDX_CPB RT_BIT(9)
3313 //| X86_CPUID_AMD_ADVPOWER_EDX_EFRO RT_BIT(10)
3314 //| X86_CPUID_AMD_ADVPOWER_EDX_PFI RT_BIT(11)
3315 //| X86_CPUID_AMD_ADVPOWER_EDX_PA RT_BIT(12)
3316 | 0;
3317 }
3318 else
3319 pCurLeaf->uEdx &= X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR;
3320 if (pConfig->fInvariantTsc)
3321 pCurLeaf->uEdx |= X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR;
3322 uSubLeaf++;
3323 }
3324
3325 /* Cpuid 0x80000008:
3326 * AMD: EBX, EDX - reserved
3327 * EAX: Virtual/Physical/Guest address Size
3328 * ECX: Number of cores + APICIdCoreIdSize
3329 * Intel: EAX: Virtual/Physical address Size
3330 * EBX, ECX, EDX - reserved
3331 * VIA: EAX: Virtual/Physical address Size
3332 * EBX, ECX, EDX - reserved
3333 *
3334 * We only expose the virtual+pysical address size to the guest atm.
3335 * On AMD we set the core count, but not the apic id stuff as we're
3336 * currently not doing the apic id assignments in a complatible manner.
3337 */
3338 uSubLeaf = 0;
3339 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x80000008), uSubLeaf)) != NULL)
3340 {
3341 pCurLeaf->uEax &= UINT32_C(0x0000ffff); /* Virtual & physical address sizes only. */
3342 pCurLeaf->uEbx = 0; /* reserved */
3343 pCurLeaf->uEdx = 0; /* reserved */
3344
3345 /* Set APICIdCoreIdSize to zero (use legacy method to determine the number of cores per cpu).
3346 * Set core count to 0, indicating 1 core. Adjust if we're in multi core mode on AMD. */
3347 pCurLeaf->uEcx = 0;
3348#ifdef VBOX_WITH_MULTI_CORE
3349 if ( pVM->cCpus > 1
3350 && pCpum->GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_AMD)
3351 pCurLeaf->uEcx |= (pVM->cCpus - 1) & UINT32_C(0xff);
3352#endif
3353 uSubLeaf++;
3354 }
3355
3356 /* Cpuid 0x80000009: Reserved
3357 * We zero this since we don't know what it may have been used for.
3358 */
3359 cpumR3CpuIdZeroLeaf(pCpum, UINT32_C(0x80000009));
3360
3361 /* Cpuid 0x8000000a: SVM Information
3362 * AMD: EAX - SVM revision.
3363 * EBX - Number of ASIDs.
3364 * ECX - Reserved.
3365 * EDX - SVM Feature identification.
3366 * We clear all as we currently does not virtualize SVM.
3367 */
3368 cpumR3CpuIdZeroLeaf(pCpum, UINT32_C(0x8000000a));
3369
3370 /* Cpuid 0x8000000b thru 0x80000018: Reserved
3371 * We clear these as we don't know what purpose they might have. */
3372 for (uint32_t uLeaf = UINT32_C(0x8000000b); uLeaf <= UINT32_C(0x80000018); uLeaf++)
3373 cpumR3CpuIdZeroLeaf(pCpum, uLeaf);
3374
3375 /* Cpuid 0x80000019: TLB configuration
3376 * Seems to be harmless, pass them thru as is. */
3377
3378 /* Cpuid 0x8000001a: Peformance optimization identifiers.
3379 * Strip anything we don't know what is or addresses feature we don't implement. */
3380 uSubLeaf = 0;
3381 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x8000001a), uSubLeaf)) != NULL)
3382 {
3383 pCurLeaf->uEax &= RT_BIT_32(0) /* FP128 - use 1x128-bit instead of 2x64-bit. */
3384 | RT_BIT_32(1) /* MOVU - Prefere unaligned MOV over MOVL + MOVH. */
3385 //| RT_BIT_32(2) /* FP256 - use 1x256-bit instead of 2x128-bit. */
3386 ;
3387 pCurLeaf->uEbx = 0; /* reserved */
3388 pCurLeaf->uEcx = 0; /* reserved */
3389 pCurLeaf->uEdx = 0; /* reserved */
3390 uSubLeaf++;
3391 }
3392
3393 /* Cpuid 0x8000001b: Instruct based sampling (IBS) information.
3394 * Clear this as we don't currently virtualize this feature. */
3395 cpumR3CpuIdZeroLeaf(pCpum, UINT32_C(0x8000001b));
3396
3397 /* Cpuid 0x8000001c: Lightweight profiling (LWP) information.
3398 * Clear this as we don't currently virtualize this feature. */
3399 cpumR3CpuIdZeroLeaf(pCpum, UINT32_C(0x8000001c));
3400
3401 /* Cpuid 0x8000001d+ECX: Get cache configuration descriptors.
3402 * We need to sanitize the cores per cache (EAX[25:14]).
3403 *
3404 * This is very much the same as Intel's CPUID(4) leaf, except EAX[31:26]
3405 * and EDX[2] are reserved here, and EAX[14:25] is documented having a
3406 * slightly different meaning.
3407 */
3408 uSubLeaf = 0;
3409 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x8000001d), uSubLeaf)) != NULL)
3410 {
3411#ifdef VBOX_WITH_MULTI_CORE
3412 uint32_t cCores = ((pCurLeaf->uEax >> 14) & 0xfff) + 1;
3413 if (cCores > pVM->cCpus)
3414 cCores = pVM->cCpus;
3415 pCurLeaf->uEax &= UINT32_C(0x00003fff);
3416 pCurLeaf->uEax |= ((cCores - 1) & 0xfff) << 14;
3417#else
3418 pCurLeaf->uEax &= UINT32_C(0x00003fff);
3419#endif
3420 uSubLeaf++;
3421 }
3422
3423 /* Cpuid 0x8000001e: Get APIC / unit / node information.
3424 * If AMD, we configure it for our layout (on EMT(0)). In the multi-core
3425 * setup, we have one compute unit with all the cores in it. Single node.
3426 */
3427 uSubLeaf = 0;
3428 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0x8000001e), uSubLeaf)) != NULL)
3429 {
3430 pCurLeaf->uEax = 0; /* Extended APIC ID = EMT(0).idApic (== 0). */
3431 if (pCurLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC_ID)
3432 {
3433#ifdef VBOX_WITH_MULTI_CORE
3434 pCurLeaf->uEbx = pVM->cCpus < 0x100
3435 ? (pVM->cCpus - 1) << 8 : UINT32_C(0x0000ff00); /* Compute unit ID 0, core per unit. */
3436#else
3437 pCurLeaf->uEbx = 0; /* Compute unit ID 0, 1 core per unit. */
3438#endif
3439 pCurLeaf->uEcx = 0; /* Node ID 0, 1 node per CPU. */
3440 }
3441 else
3442 {
3443 Assert(pCpum->GuestFeatures.enmCpuVendor != CPUMCPUVENDOR_AMD);
3444 pCurLeaf->uEbx = 0; /* Reserved. */
3445 pCurLeaf->uEcx = 0; /* Reserved. */
3446 }
3447 pCurLeaf->uEdx = 0; /* Reserved. */
3448 uSubLeaf++;
3449 }
3450
3451 /* Cpuid 0x8000001f...0x8ffffffd: Unknown.
3452 * We don't know these and what they mean, so remove them. */
3453 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
3454 UINT32_C(0x8000001f), UINT32_C(0x8ffffffd));
3455
3456 /* Cpuid 0x8ffffffe: Mystery AMD K6 leaf.
3457 * Just pass it thru for now. */
3458
3459 /* Cpuid 0x8fffffff: Mystery hammer time leaf!
3460 * Just pass it thru for now. */
3461
3462 /* Cpuid 0xc0000000: Centaur stuff.
3463 * Harmless, pass it thru. */
3464
3465 /* Cpuid 0xc0000001: Centaur features.
3466 * VIA: EAX - Family, model, stepping.
3467 * EDX - Centaur extended feature flags. Nothing interesting, except may
3468 * FEMMS (bit 5), but VIA marks it as 'reserved', so never mind.
3469 * EBX, ECX - reserved.
3470 * We keep EAX but strips the rest.
3471 */
3472 uSubLeaf = 0;
3473 while ((pCurLeaf = cpumR3CpuIdGetExactLeaf(pCpum, UINT32_C(0xc0000001), uSubLeaf)) != NULL)
3474 {
3475 pCurLeaf->uEbx = 0;
3476 pCurLeaf->uEcx = 0;
3477 pCurLeaf->uEdx = 0; /* Bits 0 thru 9 are documented on sandpil.org, but we don't want them, except maybe 5 (FEMMS). */
3478 uSubLeaf++;
3479 }
3480
3481 /* Cpuid 0xc0000002: Old Centaur Current Performance Data.
3482 * We only have fixed stale values, but should be harmless. */
3483
3484 /* Cpuid 0xc0000003: Reserved.
3485 * We zero this since we don't know what it may have been used for.
3486 */
3487 cpumR3CpuIdZeroLeaf(pCpum, UINT32_C(0xc0000003));
3488
3489 /* Cpuid 0xc0000004: Centaur Performance Info.
3490 * We only have fixed stale values, but should be harmless. */
3491
3492
3493 /* Cpuid 0xc0000005...0xcfffffff: Unknown.
3494 * We don't know these and what they mean, so remove them. */
3495 cpumR3CpuIdRemoveRange(pCpum->GuestInfo.paCpuIdLeavesR3, &pCpum->GuestInfo.cCpuIdLeaves,
3496 UINT32_C(0xc0000005), UINT32_C(0xcfffffff));
3497
3498 return VINF_SUCCESS;
3499#undef PORTABLE_DISABLE_FEATURE_BIT
3500#undef PORTABLE_CLEAR_BITS_WHEN
3501}
3502
3503
3504/**
3505 * Reads a value in /CPUM/IsaExts/ node.
3506 *
3507 * @returns VBox status code (error message raised).
3508 * @param pVM The VM handle (for errors).
3509 * @param pIsaExts The /CPUM/IsaExts node (can be NULL).
3510 * @param pszValueName The value / extension name.
3511 * @param penmValue Where to return the choice.
3512 * @param enmDefault The default choice.
3513 */
3514static int cpumR3CpuIdReadIsaExtCfg(PVM pVM, PCFGMNODE pIsaExts, const char *pszValueName,
3515 CPUMISAEXTCFG *penmValue, CPUMISAEXTCFG enmDefault)
3516{
3517 /*
3518 * Try integer encoding first.
3519 */
3520 uint64_t uValue;
3521 int rc = CFGMR3QueryInteger(pIsaExts, pszValueName, &uValue);
3522 if (RT_SUCCESS(rc))
3523 switch (uValue)
3524 {
3525 case 0: *penmValue = CPUMISAEXTCFG_DISABLED; break;
3526 case 1: *penmValue = CPUMISAEXTCFG_ENABLED_SUPPORTED; break;
3527 case 2: *penmValue = CPUMISAEXTCFG_ENABLED_ALWAYS; break;
3528 case 9: *penmValue = CPUMISAEXTCFG_ENABLED_PORTABLE; break;
3529 default:
3530 return VMSetError(pVM, VERR_CPUM_INVALID_CONFIG_VALUE, RT_SRC_POS,
3531 "Invalid config value for '/CPUM/IsaExts/%s': %llu (expected 0/'disabled', 1/'enabled', 2/'portable', or 9/'forced')",
3532 pszValueName, uValue);
3533 }
3534 /*
3535 * If missing, use default.
3536 */
3537 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
3538 *penmValue = enmDefault;
3539 else
3540 {
3541 if (rc == VERR_CFGM_NOT_INTEGER)
3542 {
3543 /*
3544 * Not an integer, try read it as a string.
3545 */
3546 char szValue[32];
3547 rc = CFGMR3QueryString(pIsaExts, pszValueName, szValue, sizeof(szValue));
3548 if (RT_SUCCESS(rc))
3549 {
3550 RTStrToLower(szValue);
3551 size_t cchValue = strlen(szValue);
3552#define EQ(a_str) (cchValue == sizeof(a_str) - 1U && memcmp(szValue, a_str, sizeof(a_str) - 1))
3553 if ( EQ("disabled") || EQ("disable") || EQ("off") || EQ("no"))
3554 *penmValue = CPUMISAEXTCFG_DISABLED;
3555 else if (EQ("enabled") || EQ("enable") || EQ("on") || EQ("yes"))
3556 *penmValue = CPUMISAEXTCFG_ENABLED_SUPPORTED;
3557 else if (EQ("forced") || EQ("force") || EQ("always"))
3558 *penmValue = CPUMISAEXTCFG_ENABLED_ALWAYS;
3559 else if (EQ("portable"))
3560 *penmValue = CPUMISAEXTCFG_ENABLED_PORTABLE;
3561 else if (EQ("default") || EQ("def"))
3562 *penmValue = enmDefault;
3563 else
3564 return VMSetError(pVM, VERR_CPUM_INVALID_CONFIG_VALUE, RT_SRC_POS,
3565 "Invalid config value for '/CPUM/IsaExts/%s': '%s' (expected 0/'disabled', 1/'enabled', 2/'portable', or 9/'forced')",
3566 pszValueName, uValue);
3567#undef EQ
3568 }
3569 }
3570 if (RT_FAILURE(rc))
3571 return VMSetError(pVM, rc, RT_SRC_POS, "Error reading config value '/CPUM/IsaExts/%s': %Rrc", pszValueName, rc);
3572 }
3573 return VINF_SUCCESS;
3574}
3575
3576
3577/**
3578 * Reads a value in /CPUM/IsaExts/ node, forcing it to DISABLED if wanted.
3579 *
3580 * @returns VBox status code (error message raised).
3581 * @param pVM The VM handle (for errors).
3582 * @param pIsaExts The /CPUM/IsaExts node (can be NULL).
3583 * @param pszValueName The value / extension name.
3584 * @param penmValue Where to return the choice.
3585 * @param enmDefault The default choice.
3586 * @param fAllowed Allowed choice. Applied both to the result and to
3587 * the default value.
3588 */
3589static int cpumR3CpuIdReadIsaExtCfgEx(PVM pVM, PCFGMNODE pIsaExts, const char *pszValueName,
3590 CPUMISAEXTCFG *penmValue, CPUMISAEXTCFG enmDefault, bool fAllowed)
3591{
3592 int rc;
3593 if (fAllowed)
3594 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, pszValueName, penmValue, enmDefault);
3595 else
3596 {
3597 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, pszValueName, penmValue, false /*enmDefault*/);
3598 if (RT_SUCCESS(rc) && *penmValue == CPUMISAEXTCFG_ENABLED_ALWAYS)
3599 LogRel(("CPUM: Ignoring forced '%s'\n", pszValueName));
3600 *penmValue = CPUMISAEXTCFG_DISABLED;
3601 }
3602 return rc;
3603}
3604
3605
3606/**
3607 * Reads a value in /CPUM/IsaExts/ node that used to be located in /CPUM/.
3608 *
3609 * @returns VBox status code (error message raised).
3610 * @param pVM The VM handle (for errors).
3611 * @param pIsaExts The /CPUM/IsaExts node (can be NULL).
3612 * @param pCpumCfg The /CPUM node (can be NULL).
3613 * @param pszValueName The value / extension name.
3614 * @param penmValue Where to return the choice.
3615 * @param enmDefault The default choice.
3616 */
3617static int cpumR3CpuIdReadIsaExtCfgLegacy(PVM pVM, PCFGMNODE pIsaExts, PCFGMNODE pCpumCfg, const char *pszValueName,
3618 CPUMISAEXTCFG *penmValue, CPUMISAEXTCFG enmDefault)
3619{
3620 if (CFGMR3Exists(pCpumCfg, pszValueName))
3621 {
3622 if (!CFGMR3Exists(pIsaExts, pszValueName))
3623 LogRel(("Warning: /CPUM/%s is deprecated, use /CPUM/IsaExts/%s instead.\n", pszValueName, pszValueName));
3624 else
3625 return VMSetError(pVM, VERR_DUPLICATE, RT_SRC_POS,
3626 "Duplicate config values '/CPUM/%s' and '/CPUM/IsaExts/%s' - please remove the former!",
3627 pszValueName, pszValueName);
3628
3629 bool fLegacy;
3630 int rc = CFGMR3QueryBoolDef(pCpumCfg, pszValueName, &fLegacy, enmDefault != CPUMISAEXTCFG_DISABLED);
3631 if (RT_SUCCESS(rc))
3632 {
3633 *penmValue = fLegacy;
3634 return VINF_SUCCESS;
3635 }
3636 return VMSetError(pVM, VERR_DUPLICATE, RT_SRC_POS, "Error querying '/CPUM/%s': %Rrc", pszValueName, rc);
3637 }
3638
3639 return cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, pszValueName, penmValue, enmDefault);
3640}
3641
3642
3643static int cpumR3CpuIdReadConfig(PVM pVM, PCPUMCPUIDCONFIG pConfig, PCFGMNODE pCpumCfg, bool fNestedPagingAndFullGuestExec)
3644{
3645 int rc;
3646
3647 /** @cfgm{/CPUM/PortableCpuIdLevel, 8-bit, 0, 3, 0}
3648 * When non-zero CPUID features that could cause portability issues will be
3649 * stripped. The higher the value the more features gets stripped. Higher
3650 * values should only be used when older CPUs are involved since it may
3651 * harm performance and maybe also cause problems with specific guests. */
3652 rc = CFGMR3QueryU8Def(pCpumCfg, "PortableCpuIdLevel", &pVM->cpum.s.u8PortableCpuIdLevel, 0);
3653 AssertLogRelRCReturn(rc, rc);
3654
3655 /** @cfgm{/CPUM/GuestCpuName, string}
3656 * The name of the CPU we're to emulate. The default is the host CPU.
3657 * Note! CPUs other than "host" one is currently unsupported. */
3658 rc = CFGMR3QueryStringDef(pCpumCfg, "GuestCpuName", pConfig->szCpuName, sizeof(pConfig->szCpuName), "host");
3659 AssertLogRelRCReturn(rc, rc);
3660
3661 /** @cfgm{/CPUM/NT4LeafLimit, boolean, false}
3662 * Limit the number of standard CPUID leaves to 0..3 to prevent NT4 from
3663 * bugchecking with MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED (0x3e).
3664 * This option corresponds somewhat to IA32_MISC_ENABLES.BOOT_NT4[bit 22].
3665 */
3666 rc = CFGMR3QueryBoolDef(pCpumCfg, "NT4LeafLimit", &pConfig->fNt4LeafLimit, false);
3667 AssertLogRelRCReturn(rc, rc);
3668
3669 /** @cfgm{/CPUM/InvariantTsc, boolean, complicated}
3670 * Set the invariant TSC flag in 0x80000007 if true, otherwas take default
3671 * action. By default the flag is passed thru as is from the host CPU, except
3672 * on AMD CPUs where it's suppressed to avoid trouble from linux assuming we
3673 * virtualize performance counters.
3674 */
3675 rc = CFGMR3QueryBoolDef(pCpumCfg, "InvariantTsc", &pConfig->fInvariantTsc, false);
3676 AssertLogRelRCReturn(rc, rc);
3677
3678 /** @cfgm{/CPUM/MaxIntelFamilyModelStep, uint32_t, UINT32_MAX}
3679 * Restrict the reported CPU family+model+stepping of intel CPUs. This is
3680 * probably going to be a temporary hack, so don't depend on this.
3681 * The 1st byte of the value is the stepping, the 2nd byte value is the model
3682 * number and the 3rd byte value is the family, and the 4th value must be zero.
3683 */
3684 rc = CFGMR3QueryU32Def(pCpumCfg, "MaxIntelFamilyModelStep", &pConfig->uMaxIntelFamilyModelStep, UINT32_MAX);
3685 AssertLogRelRCReturn(rc, rc);
3686
3687 /** @cfgm{/CPUM/MaxStdLeaf, uint32_t, 0x00000016}
3688 * The last standard leaf to keep. The actual last value that is stored in EAX
3689 * is RT_MAX(CPUID[0].EAX,/CPUM/MaxStdLeaf). Leaves beyond the max leaf are
3690 * removed. (This works independently of and differently from NT4LeafLimit.)
3691 * The default is usually set to what we're able to reasonably sanitize.
3692 */
3693 rc = CFGMR3QueryU32Def(pCpumCfg, "MaxStdLeaf", &pConfig->uMaxStdLeaf, UINT32_C(0x00000016));
3694 AssertLogRelRCReturn(rc, rc);
3695
3696 /** @cfgm{/CPUM/MaxExtLeaf, uint32_t, 0x8000001e}
3697 * The last extended leaf to keep. The actual last value that is stored in EAX
3698 * is RT_MAX(CPUID[0x80000000].EAX,/CPUM/MaxStdLeaf). Leaves beyond the max
3699 * leaf are removed. The default is set to what we're able to sanitize.
3700 */
3701 rc = CFGMR3QueryU32Def(pCpumCfg, "MaxExtLeaf", &pConfig->uMaxExtLeaf, UINT32_C(0x8000001e));
3702 AssertLogRelRCReturn(rc, rc);
3703
3704 /** @cfgm{/CPUM/MaxCentaurLeaf, uint32_t, 0xc0000004}
3705 * The last extended leaf to keep. The actual last value that is stored in EAX
3706 * is RT_MAX(CPUID[0xc0000000].EAX,/CPUM/MaxCentaurLeaf). Leaves beyond the max
3707 * leaf are removed. The default is set to what we're able to sanitize.
3708 */
3709 rc = CFGMR3QueryU32Def(pCpumCfg, "MaxCentaurLeaf", &pConfig->uMaxCentaurLeaf, UINT32_C(0xc0000004));
3710 AssertLogRelRCReturn(rc, rc);
3711
3712
3713 /*
3714 * Instruction Set Architecture (ISA) Extensions.
3715 */
3716 PCFGMNODE pIsaExts = CFGMR3GetChild(pCpumCfg, "IsaExts");
3717 if (pIsaExts)
3718 {
3719 rc = CFGMR3ValidateConfig(pIsaExts, "/CPUM/IsaExts/",
3720 "CMPXCHG16B"
3721 "|MONITOR"
3722 "|MWaitExtensions"
3723 "|SSE4.1"
3724 "|SSE4.2"
3725 "|XSAVE"
3726 "|AVX"
3727 "|AVX2"
3728 "|AESNI"
3729 "|PCLMUL"
3730 "|POPCNT"
3731 "|MOVBE"
3732 "|RDRAND"
3733 "|RDSEED"
3734 "|CLFLUSHOPT"
3735 "|ABM"
3736 "|SSE4A"
3737 "|MISALNSSE"
3738 "|3DNOWPRF"
3739 "|AXMMX"
3740 , "" /*pszValidNodes*/, "CPUM" /*pszWho*/, 0 /*uInstance*/);
3741 if (RT_FAILURE(rc))
3742 return rc;
3743 }
3744
3745 /** @cfgm{/CPUM/IsaExts/CMPXCHG16B, boolean, depends}
3746 * Expose CMPXCHG16B to the guest if supported by the host. For the time
3747 * being the default is to only do this for VMs with nested paging and AMD-V or
3748 * unrestricted guest mode.
3749 */
3750 rc = cpumR3CpuIdReadIsaExtCfgLegacy(pVM, pIsaExts, pCpumCfg, "CMPXCHG16B", &pConfig->enmCmpXchg16b, fNestedPagingAndFullGuestExec);
3751 AssertLogRelRCReturn(rc, rc);
3752
3753 /** @cfgm{/CPUM/IsaExts/MONITOR, boolean, true}
3754 * Expose MONITOR/MWAIT instructions to the guest.
3755 */
3756 rc = cpumR3CpuIdReadIsaExtCfgLegacy(pVM, pIsaExts, pCpumCfg, "MONITOR", &pConfig->enmMonitor, true);
3757 AssertLogRelRCReturn(rc, rc);
3758
3759 /** @cfgm{/CPUM/IsaExts/MWaitExtensions, boolean, false}
3760 * Expose MWAIT extended features to the guest. For now we expose just MWAIT
3761 * break on interrupt feature (bit 1).
3762 */
3763 rc = cpumR3CpuIdReadIsaExtCfgLegacy(pVM, pIsaExts, pCpumCfg, "MWaitExtensions", &pConfig->enmMWaitExtensions, false);
3764 AssertLogRelRCReturn(rc, rc);
3765
3766 /** @cfgm{/CPUM/IsaExts/SSE4.1, boolean, true}
3767 * Expose SSE4.1 to the guest if available.
3768 */
3769 rc = cpumR3CpuIdReadIsaExtCfgLegacy(pVM, pIsaExts, pCpumCfg, "SSE4.1", &pConfig->enmSse41, true);
3770 AssertLogRelRCReturn(rc, rc);
3771
3772 /** @cfgm{/CPUM/IsaExts/SSE4.2, boolean, true}
3773 * Expose SSE4.2 to the guest if available.
3774 */
3775 rc = cpumR3CpuIdReadIsaExtCfgLegacy(pVM, pIsaExts, pCpumCfg, "SSE4.2", &pConfig->enmSse42, true);
3776 AssertLogRelRCReturn(rc, rc);
3777
3778 bool const fMayHaveXSave = fNestedPagingAndFullGuestExec
3779 && pVM->cpum.s.HostFeatures.fXSaveRstor
3780 && pVM->cpum.s.HostFeatures.fOpSysXSaveRstor
3781#if HC_ARCH_BITS == 32 /* Seems this may be broken when doing 64-bit on 32-bit, just disable it for now. */
3782 && !HMIsLongModeAllowed(pVM)
3783#endif
3784 ;
3785 uint64_t const fXStateHostMask = pVM->cpum.s.fXStateHostMask;
3786
3787 /** @cfgm{/CPUM/IsaExts/XSAVE, boolean, depends}
3788 * Expose XSAVE/XRSTOR to the guest if available. For the time being the
3789 * default is to only expose this to VMs with nested paging and AMD-V or
3790 * unrestricted guest execution mode. Not possible to force this one without
3791 * host support at the moment.
3792 */
3793 rc = cpumR3CpuIdReadIsaExtCfgEx(pVM, pIsaExts, "XSAVE", &pConfig->enmXSave, fNestedPagingAndFullGuestExec,
3794 fMayHaveXSave /*fAllowed*/);
3795 AssertLogRelRCReturn(rc, rc);
3796
3797 /** @cfgm{/CPUM/IsaExts/AVX, boolean, depends}
3798 * Expose the AVX instruction set extensions to the guest if available and
3799 * XSAVE is exposed too. For the time being the default is to only expose this
3800 * to VMs with nested paging and AMD-V or unrestricted guest execution mode.
3801 */
3802 rc = cpumR3CpuIdReadIsaExtCfgEx(pVM, pIsaExts, "AVX", &pConfig->enmAvx, fNestedPagingAndFullGuestExec,
3803 fMayHaveXSave && pConfig->enmXSave && (fXStateHostMask & XSAVE_C_YMM) /*fAllowed*/);
3804 AssertLogRelRCReturn(rc, rc);
3805
3806 /** @cfgm{/CPUM/IsaExts/AVX2, boolean, depends}
3807 * Expose the AVX2 instruction set extensions to the guest if available and
3808 * XSAVE is exposed too. For the time being the default is to only expose this
3809 * to VMs with nested paging and AMD-V or unrestricted guest execution mode.
3810 */
3811 rc = cpumR3CpuIdReadIsaExtCfgEx(pVM, pIsaExts, "AVX2", &pConfig->enmAvx2, fNestedPagingAndFullGuestExec,
3812 fMayHaveXSave && pConfig->enmXSave && (fXStateHostMask & XSAVE_C_YMM) /*fAllowed*/);
3813 AssertLogRelRCReturn(rc, rc);
3814
3815 /** @cfgm{/CPUM/IsaExts/AESNI, isaextcfg, depends}
3816 * Whether to expose the AES instructions to the guest. For the time being the
3817 * default is to only do this for VMs with nested paging and AMD-V or
3818 * unrestricted guest mode.
3819 */
3820 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "AESNI", &pConfig->enmAesNi, fNestedPagingAndFullGuestExec);
3821 AssertLogRelRCReturn(rc, rc);
3822
3823 /** @cfgm{/CPUM/IsaExts/PCLMUL, isaextcfg, depends}
3824 * Whether to expose the PCLMULQDQ instructions to the guest. For the time
3825 * being the default is to only do this for VMs with nested paging and AMD-V or
3826 * unrestricted guest mode.
3827 */
3828 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "PCLMUL", &pConfig->enmPClMul, fNestedPagingAndFullGuestExec);
3829 AssertLogRelRCReturn(rc, rc);
3830
3831 /** @cfgm{/CPUM/IsaExts/POPCNT, isaextcfg, depends}
3832 * Whether to expose the POPCNT instructions to the guest. For the time
3833 * being the default is to only do this for VMs with nested paging and AMD-V or
3834 * unrestricted guest mode.
3835 */
3836 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "POPCNT", &pConfig->enmPopCnt, fNestedPagingAndFullGuestExec);
3837 AssertLogRelRCReturn(rc, rc);
3838
3839 /** @cfgm{/CPUM/IsaExts/MOVBE, isaextcfg, depends}
3840 * Whether to expose the MOVBE instructions to the guest. For the time
3841 * being the default is to only do this for VMs with nested paging and AMD-V or
3842 * unrestricted guest mode.
3843 */
3844 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "MOVBE", &pConfig->enmMovBe, fNestedPagingAndFullGuestExec);
3845 AssertLogRelRCReturn(rc, rc);
3846
3847 /** @cfgm{/CPUM/IsaExts/RDRAND, isaextcfg, depends}
3848 * Whether to expose the RDRAND instructions to the guest. For the time being
3849 * the default is to only do this for VMs with nested paging and AMD-V or
3850 * unrestricted guest mode.
3851 */
3852 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "RDRAND", &pConfig->enmRdRand, fNestedPagingAndFullGuestExec);
3853 AssertLogRelRCReturn(rc, rc);
3854
3855 /** @cfgm{/CPUM/IsaExts/RDSEED, isaextcfg, depends}
3856 * Whether to expose the RDSEED instructions to the guest. For the time being
3857 * the default is to only do this for VMs with nested paging and AMD-V or
3858 * unrestricted guest mode.
3859 */
3860 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "RDSEED", &pConfig->enmRdSeed, fNestedPagingAndFullGuestExec);
3861 AssertLogRelRCReturn(rc, rc);
3862
3863 /** @cfgm{/CPUM/IsaExts/CLFLUSHOPT, isaextcfg, depends}
3864 * Whether to expose the CLFLUSHOPT instructions to the guest. For the time
3865 * being the default is to only do this for VMs with nested paging and AMD-V or
3866 * unrestricted guest mode.
3867 */
3868 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "CLFLUSHOPT", &pConfig->enmCLFlushOpt, fNestedPagingAndFullGuestExec);
3869 AssertLogRelRCReturn(rc, rc);
3870
3871
3872 /* AMD: */
3873
3874 /** @cfgm{/CPUM/IsaExts/ABM, isaextcfg, depends}
3875 * Whether to expose the AMD ABM instructions to the guest. For the time
3876 * being the default is to only do this for VMs with nested paging and AMD-V or
3877 * unrestricted guest mode.
3878 */
3879 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "ABM", &pConfig->enmAbm, fNestedPagingAndFullGuestExec);
3880 AssertLogRelRCReturn(rc, rc);
3881
3882 /** @cfgm{/CPUM/IsaExts/SSE4A, isaextcfg, depends}
3883 * Whether to expose the AMD SSE4A instructions to the guest. For the time
3884 * being the default is to only do this for VMs with nested paging and AMD-V or
3885 * unrestricted guest mode.
3886 */
3887 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "SSE4A", &pConfig->enmSse4A, fNestedPagingAndFullGuestExec);
3888 AssertLogRelRCReturn(rc, rc);
3889
3890 /** @cfgm{/CPUM/IsaExts/MISALNSSE, isaextcfg, depends}
3891 * Whether to expose the AMD MisAlSse feature (MXCSR flag 17) to the guest. For
3892 * the time being the default is to only do this for VMs with nested paging and
3893 * AMD-V or unrestricted guest mode.
3894 */
3895 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "MISALNSSE", &pConfig->enmMisAlnSse, fNestedPagingAndFullGuestExec);
3896 AssertLogRelRCReturn(rc, rc);
3897
3898 /** @cfgm{/CPUM/IsaExts/3DNOWPRF, isaextcfg, depends}
3899 * Whether to expose the AMD 3D Now! prefetch instructions to the guest.
3900 * For the time being the default is to only do this for VMs with nested paging
3901 * and AMD-V or unrestricted guest mode.
3902 */
3903 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "3DNOWPRF", &pConfig->enm3dNowPrf, fNestedPagingAndFullGuestExec);
3904 AssertLogRelRCReturn(rc, rc);
3905
3906 /** @cfgm{/CPUM/IsaExts/AXMMX, isaextcfg, depends}
3907 * Whether to expose the AMD's MMX Extensions to the guest. For the time being
3908 * the default is to only do this for VMs with nested paging and AMD-V or
3909 * unrestricted guest mode.
3910 */
3911 rc = cpumR3CpuIdReadIsaExtCfg(pVM, pIsaExts, "AXMMX", &pConfig->enmAmdExtMmx, fNestedPagingAndFullGuestExec);
3912 AssertLogRelRCReturn(rc, rc);
3913
3914 return VINF_SUCCESS;
3915}
3916
3917
3918/**
3919 * Initializes the emulated CPU's CPUID & MSR information.
3920 *
3921 * @returns VBox status code.
3922 * @param pVM Pointer to the VM.
3923 */
3924int cpumR3InitCpuIdAndMsrs(PVM pVM)
3925{
3926 PCPUM pCpum = &pVM->cpum.s;
3927 PCFGMNODE pCpumCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "CPUM");
3928
3929 /*
3930 * Read the configuration.
3931 */
3932 CPUMCPUIDCONFIG Config;
3933 RT_ZERO(Config);
3934
3935 int rc = cpumR3CpuIdReadConfig(pVM, &Config, pCpumCfg, HMAreNestedPagingAndFullGuestExecEnabled(pVM));
3936 AssertRCReturn(rc, rc);
3937
3938 /*
3939 * Get the guest CPU data from the database and/or the host.
3940 *
3941 * The CPUID and MSRs are currently living on the regular heap to avoid
3942 * fragmenting the hyper heap (and because there isn't/wasn't any realloc
3943 * API for the hyper heap). This means special cleanup considerations.
3944 */
3945 rc = cpumR3DbGetCpuInfo(Config.szCpuName, &pCpum->GuestInfo);
3946 if (RT_FAILURE(rc))
3947 return rc == VERR_CPUM_DB_CPU_NOT_FOUND
3948 ? VMSetError(pVM, rc, RT_SRC_POS,
3949 "Info on guest CPU '%s' could not be found. Please, select a different CPU.", Config.szCpuName)
3950 : rc;
3951
3952 /** @cfgm{/CPUM/MSRs/[Name]/[First|Last|Type|Value|...],}
3953 * Overrides the guest MSRs.
3954 */
3955 rc = cpumR3LoadMsrOverrides(pVM, CFGMR3GetChild(pCpumCfg, "MSRs"));
3956
3957 /** @cfgm{/CPUM/HostCPUID/[000000xx|800000xx|c000000x]/[eax|ebx|ecx|edx],32-bit}
3958 * Overrides the CPUID leaf values (from the host CPU usually) used for
3959 * calculating the guest CPUID leaves. This can be used to preserve the CPUID
3960 * values when moving a VM to a different machine. Another use is restricting
3961 * (or extending) the feature set exposed to the guest. */
3962 if (RT_SUCCESS(rc))
3963 rc = cpumR3LoadCpuIdOverrides(pVM, CFGMR3GetChild(pCpumCfg, "HostCPUID"), "HostCPUID");
3964
3965 if (RT_SUCCESS(rc) && CFGMR3GetChild(pCpumCfg, "CPUID")) /* 2nd override, now discontinued. */
3966 rc = VMSetError(pVM, VERR_CFGM_CONFIG_UNKNOWN_NODE, RT_SRC_POS,
3967 "Found unsupported configuration node '/CPUM/CPUID/'. "
3968 "Please use IMachine::setCPUIDLeaf() instead.");
3969
3970 /*
3971 * Pre-explode the CPUID info.
3972 */
3973 if (RT_SUCCESS(rc))
3974 rc = cpumR3CpuIdExplodeFeatures(pCpum->GuestInfo.paCpuIdLeavesR3, pCpum->GuestInfo.cCpuIdLeaves, &pCpum->GuestFeatures);
3975
3976 /*
3977 * Sanitize the cpuid information passed on to the guest.
3978 */
3979 if (RT_SUCCESS(rc))
3980 {
3981 rc = cpumR3CpuIdSanitize(pVM, pCpum, &Config);
3982 if (RT_SUCCESS(rc))
3983 {
3984 cpumR3CpuIdLimitLeaves(pCpum, &Config);
3985 cpumR3CpuIdLimitIntelFamModStep(pCpum, &Config);
3986 }
3987 }
3988
3989 /*
3990 * Plant our own hypervisor CPUID leaves.
3991 */
3992 if (RT_SUCCESS(rc))
3993 rc = cpumR3CpuIdPlantHypervisorLeaves(pCpum, &Config);
3994
3995 /*
3996 * MSR fudging.
3997 */
3998 if (RT_SUCCESS(rc))
3999 {
4000 /** @cfgm{/CPUM/FudgeMSRs, boolean, true}
4001 * Fudges some common MSRs if not present in the selected CPU database entry.
4002 * This is for trying to keep VMs running when moved between different hosts
4003 * and different CPU vendors. */
4004 bool fEnable;
4005 rc = CFGMR3QueryBoolDef(pCpumCfg, "FudgeMSRs", &fEnable, true); AssertRC(rc);
4006 if (RT_SUCCESS(rc) && fEnable)
4007 {
4008 rc = cpumR3MsrApplyFudge(pVM);
4009 AssertLogRelRC(rc);
4010 }
4011 }
4012 if (RT_SUCCESS(rc))
4013 {
4014 /*
4015 * Move the MSR and CPUID arrays over on the hypervisor heap, and explode
4016 * guest CPU features again.
4017 */
4018 void *pvFree = pCpum->GuestInfo.paCpuIdLeavesR3;
4019 int rc1 = cpumR3CpuIdInstallAndExplodeLeaves(pVM, pCpum, pCpum->GuestInfo.paCpuIdLeavesR3,
4020 pCpum->GuestInfo.cCpuIdLeaves);
4021 RTMemFree(pvFree);
4022
4023 pvFree = pCpum->GuestInfo.paMsrRangesR3;
4024 int rc2 = MMHyperDupMem(pVM, pvFree,
4025 sizeof(pCpum->GuestInfo.paMsrRangesR3[0]) * pCpum->GuestInfo.cMsrRanges, 32,
4026 MM_TAG_CPUM_MSRS, (void **)&pCpum->GuestInfo.paMsrRangesR3);
4027 RTMemFree(pvFree);
4028 AssertLogRelRCReturn(rc1, rc1);
4029 AssertLogRelRCReturn(rc2, rc2);
4030
4031 pCpum->GuestInfo.paMsrRangesR0 = MMHyperR3ToR0(pVM, pCpum->GuestInfo.paMsrRangesR3);
4032 pCpum->GuestInfo.paMsrRangesRC = MMHyperR3ToRC(pVM, pCpum->GuestInfo.paMsrRangesR3);
4033
4034
4035 /*
4036 * Some more configuration that we're applying at the end of everything
4037 * via the CPUMSetGuestCpuIdFeature API.
4038 */
4039
4040 /* Check if PAE was explicitely enabled by the user. */
4041 bool fEnable;
4042 rc = CFGMR3QueryBoolDef(CFGMR3GetRoot(pVM), "EnablePAE", &fEnable, false);
4043 AssertRCReturn(rc, rc);
4044 if (fEnable)
4045 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
4046
4047 /* We don't normally enable NX for raw-mode, so give the user a chance to force it on. */
4048 rc = CFGMR3QueryBoolDef(pCpumCfg, "EnableNX", &fEnable, false);
4049 AssertRCReturn(rc, rc);
4050 if (fEnable)
4051 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
4052
4053 /* We don't enable the Hypervisor Present bit by default, but it may be needed by some guests. */
4054 rc = CFGMR3QueryBoolDef(pCpumCfg, "EnableHVP", &fEnable, false);
4055 AssertRCReturn(rc, rc);
4056 if (fEnable)
4057 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
4058
4059 return VINF_SUCCESS;
4060 }
4061
4062 /*
4063 * Failed before switching to hyper heap.
4064 */
4065 RTMemFree(pCpum->GuestInfo.paCpuIdLeavesR3);
4066 pCpum->GuestInfo.paCpuIdLeavesR3 = NULL;
4067 RTMemFree(pCpum->GuestInfo.paMsrRangesR3);
4068 pCpum->GuestInfo.paMsrRangesR3 = NULL;
4069 return rc;
4070}
4071
4072
4073
4074/*
4075 *
4076 *
4077 * Saved state related code.
4078 * Saved state related code.
4079 * Saved state related code.
4080 *
4081 *
4082 */
4083
4084/**
4085 * Called both in pass 0 and the final pass.
4086 *
4087 * @param pVM Pointer to the VM.
4088 * @param pSSM The saved state handle.
4089 */
4090void cpumR3SaveCpuId(PVM pVM, PSSMHANDLE pSSM)
4091{
4092 /*
4093 * Save all the CPU ID leaves.
4094 */
4095 SSMR3PutU32(pSSM, sizeof(pVM->cpum.s.GuestInfo.paCpuIdLeavesR3[0]));
4096 SSMR3PutU32(pSSM, pVM->cpum.s.GuestInfo.cCpuIdLeaves);
4097 SSMR3PutMem(pSSM, pVM->cpum.s.GuestInfo.paCpuIdLeavesR3,
4098 sizeof(pVM->cpum.s.GuestInfo.paCpuIdLeavesR3[0]) * pVM->cpum.s.GuestInfo.cCpuIdLeaves);
4099
4100 SSMR3PutMem(pSSM, &pVM->cpum.s.GuestInfo.DefCpuId, sizeof(pVM->cpum.s.GuestInfo.DefCpuId));
4101
4102 /*
4103 * Save a good portion of the raw CPU IDs as well as they may come in
4104 * handy when validating features for raw mode.
4105 */
4106 CPUMCPUID aRawStd[16];
4107 for (unsigned i = 0; i < RT_ELEMENTS(aRawStd); i++)
4108 ASMCpuIdExSlow(i, 0, 0, 0, &aRawStd[i].uEax, &aRawStd[i].uEbx, &aRawStd[i].uEcx, &aRawStd[i].uEdx);
4109 SSMR3PutU32(pSSM, RT_ELEMENTS(aRawStd));
4110 SSMR3PutMem(pSSM, &aRawStd[0], sizeof(aRawStd));
4111
4112 CPUMCPUID aRawExt[32];
4113 for (unsigned i = 0; i < RT_ELEMENTS(aRawExt); i++)
4114 ASMCpuIdExSlow(i | UINT32_C(0x80000000), 0, 0, 0, &aRawExt[i].uEax, &aRawExt[i].uEbx, &aRawExt[i].uEcx, &aRawExt[i].uEdx);
4115 SSMR3PutU32(pSSM, RT_ELEMENTS(aRawExt));
4116 SSMR3PutMem(pSSM, &aRawExt[0], sizeof(aRawExt));
4117}
4118
4119
4120static int cpumR3LoadOneOldGuestCpuIdArray(PSSMHANDLE pSSM, uint32_t uBase, PCPUMCPUIDLEAF *ppaLeaves, uint32_t *pcLeaves)
4121{
4122 uint32_t cCpuIds;
4123 int rc = SSMR3GetU32(pSSM, &cCpuIds);
4124 if (RT_SUCCESS(rc))
4125 {
4126 if (cCpuIds < 64)
4127 {
4128 for (uint32_t i = 0; i < cCpuIds; i++)
4129 {
4130 CPUMCPUID CpuId;
4131 rc = SSMR3GetMem(pSSM, &CpuId, sizeof(CpuId));
4132 if (RT_FAILURE(rc))
4133 break;
4134
4135 CPUMCPUIDLEAF NewLeaf;
4136 NewLeaf.uLeaf = uBase + i;
4137 NewLeaf.uSubLeaf = 0;
4138 NewLeaf.fSubLeafMask = 0;
4139 NewLeaf.uEax = CpuId.uEax;
4140 NewLeaf.uEbx = CpuId.uEbx;
4141 NewLeaf.uEcx = CpuId.uEcx;
4142 NewLeaf.uEdx = CpuId.uEdx;
4143 NewLeaf.fFlags = 0;
4144 rc = cpumR3CpuIdInsert(NULL /* pVM */, ppaLeaves, pcLeaves, &NewLeaf);
4145 }
4146 }
4147 else
4148 rc = VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
4149 }
4150 if (RT_FAILURE(rc))
4151 {
4152 RTMemFree(*ppaLeaves);
4153 *ppaLeaves = NULL;
4154 *pcLeaves = 0;
4155 }
4156 return rc;
4157}
4158
4159
4160static int cpumR3LoadGuestCpuIdArray(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, PCPUMCPUIDLEAF *ppaLeaves, uint32_t *pcLeaves)
4161{
4162 *ppaLeaves = NULL;
4163 *pcLeaves = 0;
4164
4165 int rc;
4166 if (uVersion > CPUM_SAVED_STATE_VERSION_PUT_STRUCT)
4167 {
4168 /*
4169 * The new format. Starts by declaring the leave size and count.
4170 */
4171 uint32_t cbLeaf;
4172 SSMR3GetU32(pSSM, &cbLeaf);
4173 uint32_t cLeaves;
4174 rc = SSMR3GetU32(pSSM, &cLeaves);
4175 if (RT_SUCCESS(rc))
4176 {
4177 if (cbLeaf == sizeof(**ppaLeaves))
4178 {
4179 if (cLeaves <= CPUM_CPUID_MAX_LEAVES)
4180 {
4181 /*
4182 * Load the leaves one by one.
4183 *
4184 * The uPrev stuff is a kludge for working around a week worth of bad saved
4185 * states during the CPUID revamp in March 2015. We saved too many leaves
4186 * due to a bug in cpumR3CpuIdInstallAndExplodeLeaves, thus ending up with
4187 * garbage entires at the end of the array when restoring. We also had
4188 * a subleaf insertion bug that triggered with the leaf 4 stuff below,
4189 * this kludge doesn't deal correctly with that, but who cares...
4190 */
4191 uint32_t uPrev = 0;
4192 for (uint32_t i = 0; i < cLeaves && RT_SUCCESS(rc); i++)
4193 {
4194 CPUMCPUIDLEAF Leaf;
4195 rc = SSMR3GetMem(pSSM, &Leaf, sizeof(Leaf));
4196 if (RT_SUCCESS(rc))
4197 {
4198 if ( uVersion != CPUM_SAVED_STATE_VERSION_BAD_CPUID_COUNT
4199 || Leaf.uLeaf >= uPrev)
4200 {
4201 rc = cpumR3CpuIdInsert(NULL /* pVM */, ppaLeaves, pcLeaves, &Leaf);
4202 uPrev = Leaf.uLeaf;
4203 }
4204 else
4205 uPrev = UINT32_MAX;
4206 }
4207 }
4208 }
4209 else
4210 rc = SSMR3SetLoadError(pSSM, VERR_TOO_MANY_CPUID_LEAVES, RT_SRC_POS,
4211 "Too many CPUID leaves: %#x, max %#x", cLeaves, CPUM_CPUID_MAX_LEAVES);
4212 }
4213 else
4214 rc = SSMR3SetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
4215 "CPUMCPUIDLEAF size differs: saved=%#x, our=%#x", cbLeaf, sizeof(**ppaLeaves));
4216 }
4217 }
4218 else
4219 {
4220 /*
4221 * The old format with its three inflexible arrays.
4222 */
4223 rc = cpumR3LoadOneOldGuestCpuIdArray(pSSM, UINT32_C(0x00000000), ppaLeaves, pcLeaves);
4224 if (RT_SUCCESS(rc))
4225 rc = cpumR3LoadOneOldGuestCpuIdArray(pSSM, UINT32_C(0x80000000), ppaLeaves, pcLeaves);
4226 if (RT_SUCCESS(rc))
4227 rc = cpumR3LoadOneOldGuestCpuIdArray(pSSM, UINT32_C(0xc0000000), ppaLeaves, pcLeaves);
4228 if (RT_SUCCESS(rc))
4229 {
4230 /*
4231 * Fake up leaf 4 on intel like we used to do in CPUMGetGuestCpuId earlier.
4232 */
4233 PCPUMCPUIDLEAF pLeaf = cpumR3CpuIdGetLeaf(*ppaLeaves, *pcLeaves, 0, 0);
4234 if ( pLeaf
4235 && ASMIsIntelCpuEx(pLeaf->uEbx, pLeaf->uEcx, pLeaf->uEdx))
4236 {
4237 CPUMCPUIDLEAF Leaf;
4238 Leaf.uLeaf = 4;
4239 Leaf.fSubLeafMask = UINT32_MAX;
4240 Leaf.uSubLeaf = 0;
4241 Leaf.uEdx = UINT32_C(0); /* 3 flags, 0 is fine. */
4242 Leaf.uEcx = UINT32_C(63); /* sets - 1 */
4243 Leaf.uEbx = (UINT32_C(7) << 22) /* associativity -1 */
4244 | (UINT32_C(0) << 12) /* phys line partitions - 1 */
4245 | UINT32_C(63); /* system coherency line size - 1 */
4246 Leaf.uEax = (RT_MIN(pVM->cCpus - 1, UINT32_C(0x3f)) << 26) /* cores per package - 1 */
4247 | (UINT32_C(0) << 14) /* threads per cache - 1 */
4248 | (UINT32_C(1) << 5) /* cache level */
4249 | UINT32_C(1); /* cache type (data) */
4250 Leaf.fFlags = 0;
4251 rc = cpumR3CpuIdInsert(NULL /* pVM */, ppaLeaves, pcLeaves, &Leaf);
4252 if (RT_SUCCESS(rc))
4253 {
4254 Leaf.uSubLeaf = 1; /* Should've been cache type 2 (code), but buggy code made it data. */
4255 rc = cpumR3CpuIdInsert(NULL /* pVM */, ppaLeaves, pcLeaves, &Leaf);
4256 }
4257 if (RT_SUCCESS(rc))
4258 {
4259 Leaf.uSubLeaf = 2; /* Should've been cache type 3 (unified), but buggy code made it data. */
4260 Leaf.uEcx = 4095; /* sets - 1 */
4261 Leaf.uEbx &= UINT32_C(0x003fffff); /* associativity - 1 */
4262 Leaf.uEbx |= UINT32_C(23) << 22;
4263 Leaf.uEax &= UINT32_C(0xfc003fff); /* threads per cache - 1 */
4264 Leaf.uEax |= RT_MIN(pVM->cCpus - 1, UINT32_C(0xfff)) << 14;
4265 Leaf.uEax &= UINT32_C(0xffffff1f); /* level */
4266 Leaf.uEax |= UINT32_C(2) << 5;
4267 rc = cpumR3CpuIdInsert(NULL /* pVM */, ppaLeaves, pcLeaves, &Leaf);
4268 }
4269 }
4270 }
4271 }
4272 return rc;
4273}
4274
4275
4276/**
4277 * Loads the CPU ID leaves saved by pass 0, inner worker.
4278 *
4279 * @returns VBox status code.
4280 * @param pVM Pointer to the VM.
4281 * @param pSSM The saved state handle.
4282 * @param uVersion The format version.
4283 * @param paLeaves Guest CPUID leaves loaded from the state.
4284 * @param cLeaves The number of leaves in @a paLeaves.
4285 */
4286int cpumR3LoadCpuIdInner(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, PCPUMCPUIDLEAF paLeaves, uint32_t cLeaves)
4287{
4288 AssertMsgReturn(uVersion >= CPUM_SAVED_STATE_VERSION_VER3_2, ("%u\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
4289
4290 /*
4291 * Continue loading the state into stack buffers.
4292 */
4293 CPUMCPUID GuestDefCpuId;
4294 int rc = SSMR3GetMem(pSSM, &GuestDefCpuId, sizeof(GuestDefCpuId));
4295 AssertRCReturn(rc, rc);
4296
4297 CPUMCPUID aRawStd[16];
4298 uint32_t cRawStd;
4299 rc = SSMR3GetU32(pSSM, &cRawStd); AssertRCReturn(rc, rc);
4300 if (cRawStd > RT_ELEMENTS(aRawStd))
4301 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
4302 rc = SSMR3GetMem(pSSM, &aRawStd[0], cRawStd * sizeof(aRawStd[0]));
4303 AssertRCReturn(rc, rc);
4304 for (uint32_t i = cRawStd; i < RT_ELEMENTS(aRawStd); i++)
4305 ASMCpuIdExSlow(i, 0, 0, 0, &aRawStd[i].uEax, &aRawStd[i].uEbx, &aRawStd[i].uEcx, &aRawStd[i].uEdx);
4306
4307 CPUMCPUID aRawExt[32];
4308 uint32_t cRawExt;
4309 rc = SSMR3GetU32(pSSM, &cRawExt); AssertRCReturn(rc, rc);
4310 if (cRawExt > RT_ELEMENTS(aRawExt))
4311 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
4312 rc = SSMR3GetMem(pSSM, &aRawExt[0], cRawExt * sizeof(aRawExt[0]));
4313 AssertRCReturn(rc, rc);
4314 for (uint32_t i = cRawExt; i < RT_ELEMENTS(aRawExt); i++)
4315 ASMCpuIdExSlow(i | UINT32_C(0x80000000), 0, 0, 0, &aRawExt[i].uEax, &aRawExt[i].uEbx, &aRawExt[i].uEcx, &aRawExt[i].uEdx);
4316
4317 /*
4318 * Get the raw CPU IDs for the current host.
4319 */
4320 CPUMCPUID aHostRawStd[16];
4321 for (unsigned i = 0; i < RT_ELEMENTS(aHostRawStd); i++)
4322 ASMCpuIdExSlow(i, 0, 0, 0, &aHostRawStd[i].uEax, &aHostRawStd[i].uEbx, &aHostRawStd[i].uEcx, &aHostRawStd[i].uEdx);
4323
4324 CPUMCPUID aHostRawExt[32];
4325 for (unsigned i = 0; i < RT_ELEMENTS(aHostRawExt); i++)
4326 ASMCpuIdExSlow(i | UINT32_C(0x80000000), 0, 0, 0,
4327 &aHostRawExt[i].uEax, &aHostRawExt[i].uEbx, &aHostRawExt[i].uEcx, &aHostRawExt[i].uEdx);
4328
4329 /*
4330 * Get the host and guest overrides so we don't reject the state because
4331 * some feature was enabled thru these interfaces.
4332 * Note! We currently only need the feature leaves, so skip rest.
4333 */
4334 PCFGMNODE pOverrideCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "CPUM/HostCPUID");
4335 CPUMCPUID aHostOverrideStd[2];
4336 memcpy(&aHostOverrideStd[0], &aHostRawStd[0], sizeof(aHostOverrideStd));
4337 cpumR3CpuIdInitLoadOverrideSet(UINT32_C(0x00000000), &aHostOverrideStd[0], RT_ELEMENTS(aHostOverrideStd), pOverrideCfg);
4338
4339 CPUMCPUID aHostOverrideExt[2];
4340 memcpy(&aHostOverrideExt[0], &aHostRawExt[0], sizeof(aHostOverrideExt));
4341 cpumR3CpuIdInitLoadOverrideSet(UINT32_C(0x80000000), &aHostOverrideExt[0], RT_ELEMENTS(aHostOverrideExt), pOverrideCfg);
4342
4343 /*
4344 * This can be skipped.
4345 */
4346 bool fStrictCpuIdChecks;
4347 CFGMR3QueryBoolDef(CFGMR3GetChild(CFGMR3GetRoot(pVM), "CPUM"), "StrictCpuIdChecks", &fStrictCpuIdChecks, true);
4348
4349 /*
4350 * Define a bunch of macros for simplifying the santizing/checking code below.
4351 */
4352 /* Generic expression + failure message. */
4353#define CPUID_CHECK_RET(expr, fmt) \
4354 do { \
4355 if (!(expr)) \
4356 { \
4357 char *pszMsg = RTStrAPrintf2 fmt; /* lack of variadic macros sucks */ \
4358 if (fStrictCpuIdChecks) \
4359 { \
4360 int rcCpuid = SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, "%s", pszMsg); \
4361 RTStrFree(pszMsg); \
4362 return rcCpuid; \
4363 } \
4364 LogRel(("CPUM: %s\n", pszMsg)); \
4365 RTStrFree(pszMsg); \
4366 } \
4367 } while (0)
4368#define CPUID_CHECK_WRN(expr, fmt) \
4369 do { \
4370 if (!(expr)) \
4371 LogRel(fmt); \
4372 } while (0)
4373
4374 /* For comparing two values and bitch if they differs. */
4375#define CPUID_CHECK2_RET(what, host, saved) \
4376 do { \
4377 if ((host) != (saved)) \
4378 { \
4379 if (fStrictCpuIdChecks) \
4380 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, \
4381 N_(#what " mismatch: host=%#x saved=%#x"), (host), (saved)); \
4382 LogRel(("CPUM: " #what " differs: host=%#x saved=%#x\n", (host), (saved))); \
4383 } \
4384 } while (0)
4385#define CPUID_CHECK2_WRN(what, host, saved) \
4386 do { \
4387 if ((host) != (saved)) \
4388 LogRel(("CPUM: " #what " differs: host=%#x saved=%#x\n", (host), (saved))); \
4389 } while (0)
4390
4391 /* For checking raw cpu features (raw mode). */
4392#define CPUID_RAW_FEATURE_RET(set, reg, bit) \
4393 do { \
4394 if ((aHostRaw##set [1].reg & bit) != (aRaw##set [1].reg & bit)) \
4395 { \
4396 if (fStrictCpuIdChecks) \
4397 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, \
4398 N_(#bit " mismatch: host=%d saved=%d"), \
4399 !!(aHostRaw##set [1].reg & (bit)), !!(aRaw##set [1].reg & (bit)) ); \
4400 LogRel(("CPUM: " #bit" differs: host=%d saved=%d\n", \
4401 !!(aHostRaw##set [1].reg & (bit)), !!(aRaw##set [1].reg & (bit)) )); \
4402 } \
4403 } while (0)
4404#define CPUID_RAW_FEATURE_WRN(set, reg, bit) \
4405 do { \
4406 if ((aHostRaw##set [1].reg & bit) != (aRaw##set [1].reg & bit)) \
4407 LogRel(("CPUM: " #bit" differs: host=%d saved=%d\n", \
4408 !!(aHostRaw##set [1].reg & (bit)), !!(aRaw##set [1].reg & (bit)) )); \
4409 } while (0)
4410#define CPUID_RAW_FEATURE_IGN(set, reg, bit) do { } while (0)
4411
4412 /* For checking guest features. */
4413#define CPUID_GST_FEATURE_RET(set, reg, bit) \
4414 do { \
4415 if ( (aGuestCpuId##set [1].reg & bit) \
4416 && !(aHostRaw##set [1].reg & bit) \
4417 && !(aHostOverride##set [1].reg & bit) \
4418 ) \
4419 { \
4420 if (fStrictCpuIdChecks) \
4421 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, \
4422 N_(#bit " is not supported by the host but has already exposed to the guest")); \
4423 LogRel(("CPUM: " #bit " is not supported by the host but has already exposed to the guest\n")); \
4424 } \
4425 } while (0)
4426#define CPUID_GST_FEATURE_WRN(set, reg, bit) \
4427 do { \
4428 if ( (aGuestCpuId##set [1].reg & bit) \
4429 && !(aHostRaw##set [1].reg & bit) \
4430 && !(aHostOverride##set [1].reg & bit) \
4431 ) \
4432 LogRel(("CPUM: " #bit " is not supported by the host but has already exposed to the guest\n")); \
4433 } while (0)
4434#define CPUID_GST_FEATURE_EMU(set, reg, bit) \
4435 do { \
4436 if ( (aGuestCpuId##set [1].reg & bit) \
4437 && !(aHostRaw##set [1].reg & bit) \
4438 && !(aHostOverride##set [1].reg & bit) \
4439 ) \
4440 LogRel(("CPUM: Warning - " #bit " is not supported by the host but already exposed to the guest. This may impact performance.\n")); \
4441 } while (0)
4442#define CPUID_GST_FEATURE_IGN(set, reg, bit) do { } while (0)
4443
4444 /* For checking guest features if AMD guest CPU. */
4445#define CPUID_GST_AMD_FEATURE_RET(set, reg, bit) \
4446 do { \
4447 if ( (aGuestCpuId##set [1].reg & bit) \
4448 && fGuestAmd \
4449 && (!fGuestAmd || !(aHostRaw##set [1].reg & bit)) \
4450 && !(aHostOverride##set [1].reg & bit) \
4451 ) \
4452 { \
4453 if (fStrictCpuIdChecks) \
4454 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, \
4455 N_(#bit " is not supported by the host but has already exposed to the guest")); \
4456 LogRel(("CPUM: " #bit " is not supported by the host but has already exposed to the guest\n")); \
4457 } \
4458 } while (0)
4459#define CPUID_GST_AMD_FEATURE_WRN(set, reg, bit) \
4460 do { \
4461 if ( (aGuestCpuId##set [1].reg & bit) \
4462 && fGuestAmd \
4463 && (!fGuestAmd || !(aHostRaw##set [1].reg & bit)) \
4464 && !(aHostOverride##set [1].reg & bit) \
4465 ) \
4466 LogRel(("CPUM: " #bit " is not supported by the host but has already exposed to the guest\n")); \
4467 } while (0)
4468#define CPUID_GST_AMD_FEATURE_EMU(set, reg, bit) \
4469 do { \
4470 if ( (aGuestCpuId##set [1].reg & bit) \
4471 && fGuestAmd \
4472 && (!fGuestAmd || !(aHostRaw##set [1].reg & bit)) \
4473 && !(aHostOverride##set [1].reg & bit) \
4474 ) \
4475 LogRel(("CPUM: Warning - " #bit " is not supported by the host but already exposed to the guest. This may impact performance.\n")); \
4476 } while (0)
4477#define CPUID_GST_AMD_FEATURE_IGN(set, reg, bit) do { } while (0)
4478
4479 /* For checking AMD features which have a corresponding bit in the standard
4480 range. (Intel defines very few bits in the extended feature sets.) */
4481#define CPUID_GST_FEATURE2_RET(reg, ExtBit, StdBit) \
4482 do { \
4483 if ( (aGuestCpuIdExt [1].reg & (ExtBit)) \
4484 && !(fHostAmd \
4485 ? aHostRawExt[1].reg & (ExtBit) \
4486 : aHostRawStd[1].reg & (StdBit)) \
4487 && !(aHostOverrideExt[1].reg & (ExtBit)) \
4488 ) \
4489 { \
4490 if (fStrictCpuIdChecks) \
4491 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS, \
4492 N_(#ExtBit " is not supported by the host but has already exposed to the guest")); \
4493 LogRel(("CPUM: " #ExtBit " is not supported by the host but has already exposed to the guest\n")); \
4494 } \
4495 } while (0)
4496#define CPUID_GST_FEATURE2_WRN(reg, ExtBit, StdBit) \
4497 do { \
4498 if ( (aGuestCpuId[1].reg & (ExtBit)) \
4499 && !(fHostAmd \
4500 ? aHostRawExt[1].reg & (ExtBit) \
4501 : aHostRawStd[1].reg & (StdBit)) \
4502 && !(aHostOverrideExt[1].reg & (ExtBit)) \
4503 ) \
4504 LogRel(("CPUM: " #ExtBit " is not supported by the host but has already exposed to the guest\n")); \
4505 } while (0)
4506#define CPUID_GST_FEATURE2_EMU(reg, ExtBit, StdBit) \
4507 do { \
4508 if ( (aGuestCpuIdExt [1].reg & (ExtBit)) \
4509 && !(fHostAmd \
4510 ? aHostRawExt[1].reg & (ExtBit) \
4511 : aHostRawStd[1].reg & (StdBit)) \
4512 && !(aHostOverrideExt[1].reg & (ExtBit)) \
4513 ) \
4514 LogRel(("CPUM: Warning - " #ExtBit " is not supported by the host but already exposed to the guest. This may impact performance.\n")); \
4515 } while (0)
4516#define CPUID_GST_FEATURE2_IGN(reg, ExtBit, StdBit) do { } while (0)
4517
4518 /*
4519 * For raw-mode we'll require that the CPUs are very similar since we don't
4520 * intercept CPUID instructions for user mode applications.
4521 */
4522 if (!HMIsEnabled(pVM))
4523 {
4524 /* CPUID(0) */
4525 CPUID_CHECK_RET( aHostRawStd[0].uEbx == aRawStd[0].uEbx
4526 && aHostRawStd[0].uEcx == aRawStd[0].uEcx
4527 && aHostRawStd[0].uEdx == aRawStd[0].uEdx,
4528 (N_("CPU vendor mismatch: host='%.4s%.4s%.4s' saved='%.4s%.4s%.4s'"),
4529 &aHostRawStd[0].uEbx, &aHostRawStd[0].uEdx, &aHostRawStd[0].uEcx,
4530 &aRawStd[0].uEbx, &aRawStd[0].uEdx, &aRawStd[0].uEcx));
4531 CPUID_CHECK2_WRN("Std CPUID max leaf", aHostRawStd[0].uEax, aRawStd[0].uEax);
4532 CPUID_CHECK2_WRN("Reserved bits 15:14", (aHostRawExt[1].uEax >> 14) & 3, (aRawExt[1].uEax >> 14) & 3);
4533 CPUID_CHECK2_WRN("Reserved bits 31:28", aHostRawExt[1].uEax >> 28, aRawExt[1].uEax >> 28);
4534
4535 bool const fIntel = ASMIsIntelCpuEx(aRawStd[0].uEbx, aRawStd[0].uEcx, aRawStd[0].uEdx);
4536
4537 /* CPUID(1).eax */
4538 CPUID_CHECK2_RET("CPU family", ASMGetCpuFamily(aHostRawStd[1].uEax), ASMGetCpuFamily(aRawStd[1].uEax));
4539 CPUID_CHECK2_RET("CPU model", ASMGetCpuModel(aHostRawStd[1].uEax, fIntel), ASMGetCpuModel(aRawStd[1].uEax, fIntel));
4540 CPUID_CHECK2_WRN("CPU type", (aHostRawStd[1].uEax >> 12) & 3, (aRawStd[1].uEax >> 12) & 3 );
4541
4542 /* CPUID(1).ebx - completely ignore CPU count and APIC ID. */
4543 CPUID_CHECK2_RET("CPU brand ID", aHostRawStd[1].uEbx & 0xff, aRawStd[1].uEbx & 0xff);
4544 CPUID_CHECK2_WRN("CLFLUSH chunk count", (aHostRawStd[1].uEbx >> 8) & 0xff, (aRawStd[1].uEbx >> 8) & 0xff);
4545
4546 /* CPUID(1).ecx */
4547 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE3);
4548 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_PCLMUL);
4549 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_DTES64);
4550 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_MONITOR);
4551 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_CPLDS);
4552 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_VMX);
4553 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_SMX);
4554 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_EST);
4555 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_TM2);
4556 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSSE3);
4557 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_CNTXID);
4558 CPUID_RAW_FEATURE_RET(Std, uEcx, RT_BIT_32(11) /*reserved*/ );
4559 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_FMA);
4560 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_CX16);
4561 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_TPRUPDATE);
4562 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_PDCM);
4563 CPUID_RAW_FEATURE_RET(Std, uEcx, RT_BIT_32(16) /*reserved*/);
4564 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_PCID);
4565 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_DCA);
4566 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE4_1);
4567 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE4_2);
4568 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_X2APIC);
4569 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_MOVBE);
4570 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_POPCNT);
4571 CPUID_RAW_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_TSCDEADL);
4572 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_AES);
4573 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_XSAVE);
4574 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_OSXSAVE);
4575 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_AVX);
4576 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_F16C);
4577 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_RDRAND);
4578 CPUID_RAW_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_HVP);
4579
4580 /* CPUID(1).edx */
4581 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_FPU);
4582 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_VME);
4583 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_DE);
4584 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSE);
4585 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_TSC);
4586 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_MSR);
4587 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PAE);
4588 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MCE);
4589 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CX8);
4590 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_APIC);
4591 CPUID_RAW_FEATURE_RET(Std, uEdx, RT_BIT_32(10) /*reserved*/);
4592 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_SEP);
4593 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MTRR);
4594 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PGE);
4595 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MCA);
4596 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CMOV);
4597 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PAT);
4598 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSE36);
4599 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSN);
4600 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CLFSH);
4601 CPUID_RAW_FEATURE_RET(Std, uEdx, RT_BIT_32(20) /*reserved*/);
4602 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_DS);
4603 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_ACPI);
4604 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_MMX);
4605 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_FXSR);
4606 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_SSE);
4607 CPUID_RAW_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_SSE2);
4608 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_SS);
4609 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_HTT);
4610 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_TM);
4611 CPUID_RAW_FEATURE_RET(Std, uEdx, RT_BIT_32(30) /*JMPE/IA64*/);
4612 CPUID_RAW_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PBE);
4613
4614 /* CPUID(2) - config, mostly about caches. ignore. */
4615 /* CPUID(3) - processor serial number. ignore. */
4616 /* CPUID(4) - config, cache and topology - takes ECX as input. ignore. */
4617 /* CPUID(5) - mwait/monitor config. ignore. */
4618 /* CPUID(6) - power management. ignore. */
4619 /* CPUID(7) - ???. ignore. */
4620 /* CPUID(8) - ???. ignore. */
4621 /* CPUID(9) - DCA. ignore for now. */
4622 /* CPUID(a) - PeMo info. ignore for now. */
4623 /* CPUID(b) - topology info - takes ECX as input. ignore. */
4624
4625 /* CPUID(d) - XCR0 stuff - takes ECX as input. We only warn about the main level (ECX=0) for now. */
4626 CPUID_CHECK_WRN( aRawStd[0].uEax < UINT32_C(0x0000000d)
4627 || aHostRawStd[0].uEax >= UINT32_C(0x0000000d),
4628 ("CPUM: Standard leaf D was present on saved state host, not present on current.\n"));
4629 if ( aRawStd[0].uEax >= UINT32_C(0x0000000d)
4630 && aHostRawStd[0].uEax >= UINT32_C(0x0000000d))
4631 {
4632 CPUID_CHECK2_WRN("Valid low XCR0 bits", aHostRawStd[0xd].uEax, aRawStd[0xd].uEax);
4633 CPUID_CHECK2_WRN("Valid high XCR0 bits", aHostRawStd[0xd].uEdx, aRawStd[0xd].uEdx);
4634 CPUID_CHECK2_WRN("Current XSAVE/XRSTOR area size", aHostRawStd[0xd].uEbx, aRawStd[0xd].uEbx);
4635/** @todo XSAVE: Stricter XSAVE feature checks for raw-mode. */
4636 CPUID_CHECK2_WRN("Max XSAVE/XRSTOR area size", aHostRawStd[0xd].uEcx, aRawStd[0xd].uEcx);
4637 }
4638
4639 /* CPUID(0x80000000) - same as CPUID(0) except for eax.
4640 Note! Intel have/is marking many of the fields here as reserved. We
4641 will verify them as if it's an AMD CPU. */
4642 CPUID_CHECK_RET( (aHostRawExt[0].uEax >= UINT32_C(0x80000001) && aHostRawExt[0].uEax <= UINT32_C(0x8000007f))
4643 || !(aRawExt[0].uEax >= UINT32_C(0x80000001) && aRawExt[0].uEax <= UINT32_C(0x8000007f)),
4644 (N_("Extended leaves was present on saved state host, but is missing on the current\n")));
4645 if (aRawExt[0].uEax >= UINT32_C(0x80000001) && aRawExt[0].uEax <= UINT32_C(0x8000007f))
4646 {
4647 CPUID_CHECK_RET( aHostRawExt[0].uEbx == aRawExt[0].uEbx
4648 && aHostRawExt[0].uEcx == aRawExt[0].uEcx
4649 && aHostRawExt[0].uEdx == aRawExt[0].uEdx,
4650 (N_("CPU vendor mismatch: host='%.4s%.4s%.4s' saved='%.4s%.4s%.4s'"),
4651 &aHostRawExt[0].uEbx, &aHostRawExt[0].uEdx, &aHostRawExt[0].uEcx,
4652 &aRawExt[0].uEbx, &aRawExt[0].uEdx, &aRawExt[0].uEcx));
4653 CPUID_CHECK2_WRN("Ext CPUID max leaf", aHostRawExt[0].uEax, aRawExt[0].uEax);
4654
4655 /* CPUID(0x80000001).eax - same as CPUID(0).eax. */
4656 CPUID_CHECK2_RET("CPU family", ASMGetCpuFamily(aHostRawExt[1].uEax), ASMGetCpuFamily(aRawExt[1].uEax));
4657 CPUID_CHECK2_RET("CPU model", ASMGetCpuModel(aHostRawExt[1].uEax, fIntel), ASMGetCpuModel(aRawExt[1].uEax, fIntel));
4658 CPUID_CHECK2_WRN("CPU type", (aHostRawExt[1].uEax >> 12) & 3, (aRawExt[1].uEax >> 12) & 3 );
4659 CPUID_CHECK2_WRN("Reserved bits 15:14", (aHostRawExt[1].uEax >> 14) & 3, (aRawExt[1].uEax >> 14) & 3 );
4660 CPUID_CHECK2_WRN("Reserved bits 31:28", aHostRawExt[1].uEax >> 28, aRawExt[1].uEax >> 28);
4661
4662 /* CPUID(0x80000001).ebx - Brand ID (maybe), just warn if things differs. */
4663 CPUID_CHECK2_WRN("CPU BrandID", aHostRawExt[1].uEbx & 0xffff, aRawExt[1].uEbx & 0xffff);
4664 CPUID_CHECK2_WRN("Reserved bits 16:27", (aHostRawExt[1].uEbx >> 16) & 0xfff, (aRawExt[1].uEbx >> 16) & 0xfff);
4665 CPUID_CHECK2_WRN("PkgType", (aHostRawExt[1].uEbx >> 28) & 0xf, (aRawExt[1].uEbx >> 28) & 0xf);
4666
4667 /* CPUID(0x80000001).ecx */
4668 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF);
4669 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_CMPL);
4670 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SVM);
4671 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_EXT_APIC);
4672 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_CR8L);
4673 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_ABM);
4674 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SSE4A);
4675 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_MISALNSSE);
4676 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF);
4677 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_OSVW);
4678 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_IBS);
4679 CPUID_RAW_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_XOP);
4680 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SKINIT);
4681 CPUID_RAW_FEATURE_IGN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_WDT);
4682 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(14));
4683 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(15));
4684 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(16));
4685 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(17));
4686 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(18));
4687 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(19));
4688 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(20));
4689 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(21));
4690 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(22));
4691 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(23));
4692 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(24));
4693 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(25));
4694 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(26));
4695 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(27));
4696 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(28));
4697 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(29));
4698 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(30));
4699 CPUID_RAW_FEATURE_WRN(Ext, uEcx, RT_BIT_32(31));
4700
4701 /* CPUID(0x80000001).edx */
4702 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_FPU);
4703 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_VME);
4704 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_DE);
4705 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_PSE);
4706 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_TSC);
4707 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_MSR);
4708 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_PAE);
4709 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_MCE);
4710 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_CX8);
4711 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_APIC);
4712 CPUID_RAW_FEATURE_IGN(Ext, uEdx, RT_BIT_32(10) /*reserved*/);
4713 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_SEP);
4714 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_MTRR);
4715 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_PGE);
4716 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_MCA);
4717 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_CMOV);
4718 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_PAT);
4719 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_PSE36);
4720 CPUID_RAW_FEATURE_IGN(Ext, uEdx, RT_BIT_32(18) /*reserved*/);
4721 CPUID_RAW_FEATURE_IGN(Ext, uEdx, RT_BIT_32(19) /*reserved*/);
4722 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_NX);
4723 CPUID_RAW_FEATURE_IGN(Ext, uEdx, RT_BIT_32(21) /*reserved*/);
4724 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_AXMMX);
4725 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_MMX);
4726 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_FXSR);
4727 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_FFXSR);
4728 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_PAGE1GB);
4729 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_RDTSCP);
4730 CPUID_RAW_FEATURE_IGN(Ext, uEdx, RT_BIT_32(28) /*reserved*/);
4731 CPUID_RAW_FEATURE_IGN(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_LONG_MODE);
4732 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_3DNOW_EX);
4733 CPUID_RAW_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_3DNOW);
4734
4735 /** @todo verify the rest as well. */
4736 }
4737 }
4738
4739
4740
4741 /*
4742 * Verify that we can support the features already exposed to the guest on
4743 * this host.
4744 *
4745 * Most of the features we're emulating requires intercepting instruction
4746 * and doing it the slow way, so there is no need to warn when they aren't
4747 * present in the host CPU. Thus we use IGN instead of EMU on these.
4748 *
4749 * Trailing comments:
4750 * "EMU" - Possible to emulate, could be lots of work and very slow.
4751 * "EMU?" - Can this be emulated?
4752 */
4753 CPUMCPUID aGuestCpuIdStd[2];
4754 RT_ZERO(aGuestCpuIdStd);
4755 cpumR3CpuIdGetLeafLegacy(paLeaves, cLeaves, 1, 0, &aGuestCpuIdStd[1]);
4756
4757 /* CPUID(1).ecx */
4758 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE3); // -> EMU
4759 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_PCLMUL); // -> EMU?
4760 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_DTES64); // -> EMU?
4761 CPUID_GST_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_MONITOR);
4762 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_CPLDS); // -> EMU?
4763 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_VMX); // -> EMU
4764 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SMX); // -> EMU
4765 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_EST); // -> EMU
4766 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_TM2); // -> EMU?
4767 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSSE3); // -> EMU
4768 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_CNTXID); // -> EMU
4769 CPUID_GST_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_SDBG);
4770 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_FMA); // -> EMU? what's this?
4771 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_CX16); // -> EMU?
4772 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_TPRUPDATE);//-> EMU
4773 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_PDCM); // -> EMU
4774 CPUID_GST_FEATURE_RET(Std, uEcx, RT_BIT_32(16) /*reserved*/);
4775 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_PCID);
4776 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_DCA); // -> EMU?
4777 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE4_1); // -> EMU
4778 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_SSE4_2); // -> EMU
4779 CPUID_GST_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_X2APIC);
4780 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_MOVBE); // -> EMU
4781 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_POPCNT); // -> EMU
4782 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_TSCDEADL);
4783 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_AES); // -> EMU
4784 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_XSAVE); // -> EMU
4785 CPUID_GST_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_OSXSAVE);
4786 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_AVX); // -> EMU?
4787 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_F16C);
4788 CPUID_GST_FEATURE_RET(Std, uEcx, X86_CPUID_FEATURE_ECX_RDRAND);
4789 CPUID_GST_FEATURE_IGN(Std, uEcx, X86_CPUID_FEATURE_ECX_HVP); // Normally not set by host
4790
4791 /* CPUID(1).edx */
4792 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_FPU);
4793 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_VME);
4794 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_DE); // -> EMU?
4795 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSE);
4796 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_TSC); // -> EMU
4797 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_MSR); // -> EMU
4798 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_PAE);
4799 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MCE);
4800 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CX8); // -> EMU?
4801 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_APIC);
4802 CPUID_GST_FEATURE_RET(Std, uEdx, RT_BIT_32(10) /*reserved*/);
4803 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_SEP);
4804 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MTRR);
4805 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PGE);
4806 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_MCA);
4807 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CMOV); // -> EMU
4808 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PAT);
4809 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSE36);
4810 CPUID_GST_FEATURE_IGN(Std, uEdx, X86_CPUID_FEATURE_EDX_PSN);
4811 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_CLFSH); // -> EMU
4812 CPUID_GST_FEATURE_RET(Std, uEdx, RT_BIT_32(20) /*reserved*/);
4813 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_DS); // -> EMU?
4814 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_ACPI); // -> EMU?
4815 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_MMX); // -> EMU
4816 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_FXSR); // -> EMU
4817 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_SSE); // -> EMU
4818 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_SSE2); // -> EMU
4819 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_SS); // -> EMU?
4820 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_HTT); // -> EMU?
4821 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_TM); // -> EMU?
4822 CPUID_GST_FEATURE_RET(Std, uEdx, RT_BIT_32(30) /*JMPE/IA64*/); // -> EMU
4823 CPUID_GST_FEATURE_RET(Std, uEdx, X86_CPUID_FEATURE_EDX_PBE); // -> EMU?
4824
4825 /* CPUID(0x80000000). */
4826 CPUMCPUID aGuestCpuIdExt[2];
4827 RT_ZERO(aGuestCpuIdExt);
4828 if (cpumR3CpuIdGetLeafLegacy(paLeaves, cLeaves, UINT32_C(0x80000001), 0, &aGuestCpuIdExt[1]))
4829 {
4830 /** @todo deal with no 0x80000001 on the host. */
4831 bool const fHostAmd = ASMIsAmdCpuEx(aHostRawStd[0].uEbx, aHostRawStd[0].uEcx, aHostRawStd[0].uEdx);
4832 bool const fGuestAmd = ASMIsAmdCpuEx(aGuestCpuIdExt[0].uEbx, aGuestCpuIdExt[0].uEcx, aGuestCpuIdExt[0].uEdx);
4833
4834 /* CPUID(0x80000001).ecx */
4835 CPUID_GST_FEATURE_WRN(Ext, uEcx, X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF); // -> EMU
4836 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_CMPL); // -> EMU
4837 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SVM); // -> EMU
4838 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_EXT_APIC);// ???
4839 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_CR8L); // -> EMU
4840 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_ABM); // -> EMU
4841 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SSE4A); // -> EMU
4842 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_MISALNSSE);//-> EMU
4843 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_3DNOWPRF);// -> EMU
4844 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_OSVW); // -> EMU?
4845 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_IBS); // -> EMU
4846 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_XOP); // -> EMU
4847 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_SKINIT); // -> EMU
4848 CPUID_GST_AMD_FEATURE_RET(Ext, uEcx, X86_CPUID_AMD_FEATURE_ECX_WDT); // -> EMU
4849 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(14));
4850 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(15));
4851 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(16));
4852 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(17));
4853 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(18));
4854 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(19));
4855 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(20));
4856 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(21));
4857 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(22));
4858 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(23));
4859 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(24));
4860 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(25));
4861 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(26));
4862 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(27));
4863 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(28));
4864 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(29));
4865 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(30));
4866 CPUID_GST_AMD_FEATURE_WRN(Ext, uEcx, RT_BIT_32(31));
4867
4868 /* CPUID(0x80000001).edx */
4869 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_FPU, X86_CPUID_FEATURE_EDX_FPU); // -> EMU
4870 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_VME, X86_CPUID_FEATURE_EDX_VME); // -> EMU
4871 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_DE, X86_CPUID_FEATURE_EDX_DE); // -> EMU
4872 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_PSE, X86_CPUID_FEATURE_EDX_PSE);
4873 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_TSC, X86_CPUID_FEATURE_EDX_TSC); // -> EMU
4874 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_MSR, X86_CPUID_FEATURE_EDX_MSR); // -> EMU
4875 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_PAE, X86_CPUID_FEATURE_EDX_PAE);
4876 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_MCE, X86_CPUID_FEATURE_EDX_MCE);
4877 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_CX8, X86_CPUID_FEATURE_EDX_CX8); // -> EMU?
4878 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_APIC, X86_CPUID_FEATURE_EDX_APIC);
4879 CPUID_GST_AMD_FEATURE_WRN(Ext, uEdx, RT_BIT_32(10) /*reserved*/);
4880 CPUID_GST_FEATURE_IGN( Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_SYSCALL); // On Intel: long mode only.
4881 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_MTRR, X86_CPUID_FEATURE_EDX_MTRR);
4882 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_PGE, X86_CPUID_FEATURE_EDX_PGE);
4883 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_MCA, X86_CPUID_FEATURE_EDX_MCA);
4884 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_CMOV, X86_CPUID_FEATURE_EDX_CMOV); // -> EMU
4885 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_PAT, X86_CPUID_FEATURE_EDX_PAT);
4886 CPUID_GST_FEATURE2_IGN( uEdx, X86_CPUID_AMD_FEATURE_EDX_PSE36, X86_CPUID_FEATURE_EDX_PSE36);
4887 CPUID_GST_AMD_FEATURE_WRN(Ext, uEdx, RT_BIT_32(18) /*reserved*/);
4888 CPUID_GST_AMD_FEATURE_WRN(Ext, uEdx, RT_BIT_32(19) /*reserved*/);
4889 CPUID_GST_FEATURE_RET( Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_NX);
4890 CPUID_GST_FEATURE_WRN( Ext, uEdx, RT_BIT_32(21) /*reserved*/);
4891 CPUID_GST_FEATURE_RET( Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_AXMMX);
4892 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_MMX, X86_CPUID_FEATURE_EDX_MMX); // -> EMU
4893 CPUID_GST_FEATURE2_RET( uEdx, X86_CPUID_AMD_FEATURE_EDX_FXSR, X86_CPUID_FEATURE_EDX_FXSR); // -> EMU
4894 CPUID_GST_AMD_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_FFXSR);
4895 CPUID_GST_AMD_FEATURE_RET(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_PAGE1GB);
4896 CPUID_GST_AMD_FEATURE_RET(Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_RDTSCP);
4897 CPUID_GST_FEATURE_IGN( Ext, uEdx, RT_BIT_32(28) /*reserved*/);
4898 CPUID_GST_FEATURE_RET( Ext, uEdx, X86_CPUID_EXT_FEATURE_EDX_LONG_MODE);
4899 CPUID_GST_AMD_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_3DNOW_EX);
4900 CPUID_GST_AMD_FEATURE_RET(Ext, uEdx, X86_CPUID_AMD_FEATURE_EDX_3DNOW);
4901 }
4902
4903 /** @todo check leaf 7 */
4904
4905 /* CPUID(d) - XCR0 stuff - takes ECX as input.
4906 * ECX=0: EAX - Valid bits in XCR0[31:0].
4907 * EBX - Maximum state size as per current XCR0 value.
4908 * ECX - Maximum state size for all supported features.
4909 * EDX - Valid bits in XCR0[63:32].
4910 * ECX=1: EAX - Various X-features.
4911 * EBX - Maximum state size as per current XCR0|IA32_XSS value.
4912 * ECX - Valid bits in IA32_XSS[31:0].
4913 * EDX - Valid bits in IA32_XSS[63:32].
4914 * ECX=N, where N in 2..63 and indicates a bit in XCR0 and/or IA32_XSS,
4915 * if the bit invalid all four registers are set to zero.
4916 * EAX - The state size for this feature.
4917 * EBX - The state byte offset of this feature.
4918 * ECX - Bit 0 indicates whether this sub-leaf maps to a valid IA32_XSS bit (=1) or a valid XCR0 bit (=0).
4919 * EDX - Reserved, but is set to zero if invalid sub-leaf index.
4920 */
4921 uint64_t fGuestXcr0Mask = 0;
4922 PCPUMCPUIDLEAF pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x0000000d), 0);
4923 if ( pCurLeaf
4924 && (aGuestCpuIdStd[1].uEcx & X86_CPUID_FEATURE_ECX_XSAVE)
4925 && ( pCurLeaf->uEax
4926 || pCurLeaf->uEbx
4927 || pCurLeaf->uEcx
4928 || pCurLeaf->uEdx) )
4929 {
4930 fGuestXcr0Mask = RT_MAKE_U64(pCurLeaf->uEax, pCurLeaf->uEdx);
4931 if (fGuestXcr0Mask & ~pVM->cpum.s.fXStateHostMask)
4932 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4933 N_("CPUID(0xd/0).EDX:EAX mismatch: %#llx saved, %#llx supported by the current host (XCR0 bits)"),
4934 fGuestXcr0Mask, pVM->cpum.s.fXStateHostMask);
4935 if ((fGuestXcr0Mask & (XSAVE_C_X87 | XSAVE_C_SSE)) != (XSAVE_C_X87 | XSAVE_C_SSE))
4936 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4937 N_("CPUID(0xd/0).EDX:EAX missing mandatory X87 or SSE bits: %#RX64"), fGuestXcr0Mask);
4938
4939 /* We don't support any additional features yet. */
4940 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x0000000d), 1);
4941 if (pCurLeaf && pCurLeaf->uEax)
4942 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4943 N_("CPUID(0xd/1).EAX=%#x, expected zero"), pCurLeaf->uEax);
4944 if (pCurLeaf && (pCurLeaf->uEcx || pCurLeaf->uEdx))
4945 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4946 N_("CPUID(0xd/1).EDX:ECX=%#llx, expected zero"),
4947 RT_MAKE_U64(pCurLeaf->uEdx, pCurLeaf->uEcx));
4948
4949
4950 for (uint32_t uSubLeaf = 2; uSubLeaf < 64; uSubLeaf++)
4951 {
4952 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x0000000d), uSubLeaf);
4953 if (pCurLeaf)
4954 {
4955 /* If advertised, the state component offset and size must match the one used by host. */
4956 if (pCurLeaf->uEax || pCurLeaf->uEbx || pCurLeaf->uEcx || pCurLeaf->uEdx)
4957 {
4958 CPUMCPUID RawHost;
4959 ASMCpuIdExSlow(UINT32_C(0x0000000d), 0, uSubLeaf, 0,
4960 &RawHost.uEax, &RawHost.uEbx, &RawHost.uEcx, &RawHost.uEdx);
4961 if ( RawHost.uEbx != pCurLeaf->uEbx
4962 || RawHost.uEax != pCurLeaf->uEax)
4963 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4964 N_("CPUID(0xd/%#x).EBX/EAX=%#x/%#x, current host uses %#x/%#x (offset/size)"),
4965 uSubLeaf, pCurLeaf->uEbx, pCurLeaf->uEax, RawHost.uEbx, RawHost.uEax);
4966 }
4967 }
4968 }
4969 }
4970 /* Clear leaf 0xd just in case we're loading an old state... */
4971 else if (pCurLeaf)
4972 {
4973 for (uint32_t uSubLeaf = 0; uSubLeaf < 64; uSubLeaf++)
4974 {
4975 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x0000000d), uSubLeaf);
4976 if (pCurLeaf)
4977 {
4978 AssertLogRelMsg( uVersion <= CPUM_SAVED_STATE_VERSION_PUT_STRUCT
4979 || ( pCurLeaf->uEax == 0
4980 && pCurLeaf->uEbx == 0
4981 && pCurLeaf->uEcx == 0
4982 && pCurLeaf->uEdx == 0),
4983 ("uVersion=%#x; %#x %#x %#x %#x\n",
4984 uVersion, pCurLeaf->uEax, pCurLeaf->uEbx, pCurLeaf->uEcx, pCurLeaf->uEdx));
4985 pCurLeaf->uEax = pCurLeaf->uEbx = pCurLeaf->uEcx = pCurLeaf->uEdx = 0;
4986 }
4987 }
4988 }
4989
4990 /* Update the fXStateGuestMask value for the VM. */
4991 if (pVM->cpum.s.fXStateGuestMask != fGuestXcr0Mask)
4992 {
4993 LogRel(("CPUM: fXStateGuestMask=%#llx -> %#llx\n", pVM->cpum.s.fXStateGuestMask, fGuestXcr0Mask));
4994 pVM->cpum.s.fXStateGuestMask = fGuestXcr0Mask;
4995 if (!fGuestXcr0Mask && (aGuestCpuIdStd[1].uEcx & X86_CPUID_FEATURE_ECX_XSAVE))
4996 return SSMR3SetLoadError(pSSM, VERR_SSM_LOAD_CPUID_MISMATCH, RT_SRC_POS,
4997 N_("Internal Processing Error: XSAVE feature bit enabled, but leaf 0xd is empty."));
4998 }
4999
5000#undef CPUID_CHECK_RET
5001#undef CPUID_CHECK_WRN
5002#undef CPUID_CHECK2_RET
5003#undef CPUID_CHECK2_WRN
5004#undef CPUID_RAW_FEATURE_RET
5005#undef CPUID_RAW_FEATURE_WRN
5006#undef CPUID_RAW_FEATURE_IGN
5007#undef CPUID_GST_FEATURE_RET
5008#undef CPUID_GST_FEATURE_WRN
5009#undef CPUID_GST_FEATURE_EMU
5010#undef CPUID_GST_FEATURE_IGN
5011#undef CPUID_GST_FEATURE2_RET
5012#undef CPUID_GST_FEATURE2_WRN
5013#undef CPUID_GST_FEATURE2_EMU
5014#undef CPUID_GST_FEATURE2_IGN
5015#undef CPUID_GST_AMD_FEATURE_RET
5016#undef CPUID_GST_AMD_FEATURE_WRN
5017#undef CPUID_GST_AMD_FEATURE_EMU
5018#undef CPUID_GST_AMD_FEATURE_IGN
5019
5020 /*
5021 * We're good, commit the CPU ID leaves.
5022 */
5023 MMHyperFree(pVM, pVM->cpum.s.GuestInfo.paCpuIdLeavesR3);
5024 pVM->cpum.s.GuestInfo.paCpuIdLeavesR3 = NULL;
5025 pVM->cpum.s.GuestInfo.paCpuIdLeavesR0 = NIL_RTR0PTR;
5026 pVM->cpum.s.GuestInfo.paCpuIdLeavesRC = NIL_RTRCPTR;
5027 pVM->cpum.s.GuestInfo.DefCpuId = GuestDefCpuId;
5028 rc = cpumR3CpuIdInstallAndExplodeLeaves(pVM, &pVM->cpum.s, paLeaves, cLeaves);
5029 AssertLogRelRCReturn(rc, rc);
5030
5031 return VINF_SUCCESS;
5032}
5033
5034
5035/**
5036 * Loads the CPU ID leaves saved by pass 0.
5037 *
5038 * @returns VBox status code.
5039 * @param pVM Pointer to the VM.
5040 * @param pSSM The saved state handle.
5041 * @param uVersion The format version.
5042 */
5043int cpumR3LoadCpuId(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion)
5044{
5045 AssertMsgReturn(uVersion >= CPUM_SAVED_STATE_VERSION_VER3_2, ("%u\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
5046
5047 /*
5048 * Load the CPUID leaves array first and call worker to do the rest, just so
5049 * we can free the memory when we need to without ending up in column 1000.
5050 */
5051 PCPUMCPUIDLEAF paLeaves;
5052 uint32_t cLeaves;
5053 int rc = cpumR3LoadGuestCpuIdArray(pVM, pSSM, uVersion, &paLeaves, &cLeaves);
5054 AssertRC(rc);
5055 if (RT_SUCCESS(rc))
5056 {
5057 rc = cpumR3LoadCpuIdInner(pVM, pSSM, uVersion, paLeaves, cLeaves);
5058 RTMemFree(paLeaves);
5059 }
5060 return rc;
5061}
5062
5063
5064
5065/**
5066 * Loads the CPU ID leaves saved by pass 0 in an pre 3.2 saved state.
5067 *
5068 * @returns VBox status code.
5069 * @param pVM Pointer to the VM.
5070 * @param pSSM The saved state handle.
5071 * @param uVersion The format version.
5072 */
5073int cpumR3LoadCpuIdPre32(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion)
5074{
5075 AssertMsgReturn(uVersion < CPUM_SAVED_STATE_VERSION_VER3_2, ("%u\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
5076
5077 /*
5078 * Restore the CPUID leaves.
5079 *
5080 * Note that we support restoring less than the current amount of standard
5081 * leaves because we've been allowed more is newer version of VBox.
5082 */
5083 uint32_t cElements;
5084 int rc = SSMR3GetU32(pSSM, &cElements); AssertRCReturn(rc, rc);
5085 if (cElements > RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd))
5086 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
5087 SSMR3GetMem(pSSM, &pVM->cpum.s.aGuestCpuIdPatmStd[0], cElements*sizeof(pVM->cpum.s.aGuestCpuIdPatmStd[0]));
5088
5089 rc = SSMR3GetU32(pSSM, &cElements); AssertRCReturn(rc, rc);
5090 if (cElements != RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt))
5091 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
5092 SSMR3GetMem(pSSM, &pVM->cpum.s.aGuestCpuIdPatmExt[0], sizeof(pVM->cpum.s.aGuestCpuIdPatmExt));
5093
5094 rc = SSMR3GetU32(pSSM, &cElements); AssertRCReturn(rc, rc);
5095 if (cElements != RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur))
5096 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
5097 SSMR3GetMem(pSSM, &pVM->cpum.s.aGuestCpuIdPatmCentaur[0], sizeof(pVM->cpum.s.aGuestCpuIdPatmCentaur));
5098
5099 SSMR3GetMem(pSSM, &pVM->cpum.s.GuestInfo.DefCpuId, sizeof(pVM->cpum.s.GuestInfo.DefCpuId));
5100
5101 /*
5102 * Check that the basic cpuid id information is unchanged.
5103 */
5104 /** @todo we should check the 64 bits capabilities too! */
5105 uint32_t au32CpuId[8] = {0,0,0,0, 0,0,0,0};
5106 ASMCpuIdExSlow(0, 0, 0, 0, &au32CpuId[0], &au32CpuId[1], &au32CpuId[2], &au32CpuId[3]);
5107 ASMCpuIdExSlow(1, 0, 0, 0, &au32CpuId[4], &au32CpuId[5], &au32CpuId[6], &au32CpuId[7]);
5108 uint32_t au32CpuIdSaved[8];
5109 rc = SSMR3GetMem(pSSM, &au32CpuIdSaved[0], sizeof(au32CpuIdSaved));
5110 if (RT_SUCCESS(rc))
5111 {
5112 /* Ignore CPU stepping. */
5113 au32CpuId[4] &= 0xfffffff0;
5114 au32CpuIdSaved[4] &= 0xfffffff0;
5115
5116 /* Ignore APIC ID (AMD specs). */
5117 au32CpuId[5] &= ~0xff000000;
5118 au32CpuIdSaved[5] &= ~0xff000000;
5119
5120 /* Ignore the number of Logical CPUs (AMD specs). */
5121 au32CpuId[5] &= ~0x00ff0000;
5122 au32CpuIdSaved[5] &= ~0x00ff0000;
5123
5124 /* Ignore some advanced capability bits, that we don't expose to the guest. */
5125 au32CpuId[6] &= ~( X86_CPUID_FEATURE_ECX_DTES64
5126 | X86_CPUID_FEATURE_ECX_VMX
5127 | X86_CPUID_FEATURE_ECX_SMX
5128 | X86_CPUID_FEATURE_ECX_EST
5129 | X86_CPUID_FEATURE_ECX_TM2
5130 | X86_CPUID_FEATURE_ECX_CNTXID
5131 | X86_CPUID_FEATURE_ECX_TPRUPDATE
5132 | X86_CPUID_FEATURE_ECX_PDCM
5133 | X86_CPUID_FEATURE_ECX_DCA
5134 | X86_CPUID_FEATURE_ECX_X2APIC
5135 );
5136 au32CpuIdSaved[6] &= ~( X86_CPUID_FEATURE_ECX_DTES64
5137 | X86_CPUID_FEATURE_ECX_VMX
5138 | X86_CPUID_FEATURE_ECX_SMX
5139 | X86_CPUID_FEATURE_ECX_EST
5140 | X86_CPUID_FEATURE_ECX_TM2
5141 | X86_CPUID_FEATURE_ECX_CNTXID
5142 | X86_CPUID_FEATURE_ECX_TPRUPDATE
5143 | X86_CPUID_FEATURE_ECX_PDCM
5144 | X86_CPUID_FEATURE_ECX_DCA
5145 | X86_CPUID_FEATURE_ECX_X2APIC
5146 );
5147
5148 /* Make sure we don't forget to update the masks when enabling
5149 * features in the future.
5150 */
5151 AssertRelease(!(pVM->cpum.s.aGuestCpuIdPatmStd[1].uEcx &
5152 ( X86_CPUID_FEATURE_ECX_DTES64
5153 | X86_CPUID_FEATURE_ECX_VMX
5154 | X86_CPUID_FEATURE_ECX_SMX
5155 | X86_CPUID_FEATURE_ECX_EST
5156 | X86_CPUID_FEATURE_ECX_TM2
5157 | X86_CPUID_FEATURE_ECX_CNTXID
5158 | X86_CPUID_FEATURE_ECX_TPRUPDATE
5159 | X86_CPUID_FEATURE_ECX_PDCM
5160 | X86_CPUID_FEATURE_ECX_DCA
5161 | X86_CPUID_FEATURE_ECX_X2APIC
5162 )));
5163 /* do the compare */
5164 if (memcmp(au32CpuIdSaved, au32CpuId, sizeof(au32CpuIdSaved)))
5165 {
5166 if (SSMR3HandleGetAfter(pSSM) == SSMAFTER_DEBUG_IT)
5167 LogRel(("cpumR3LoadExec: CpuId mismatch! (ignored due to SSMAFTER_DEBUG_IT)\n"
5168 "Saved=%.*Rhxs\n"
5169 "Real =%.*Rhxs\n",
5170 sizeof(au32CpuIdSaved), au32CpuIdSaved,
5171 sizeof(au32CpuId), au32CpuId));
5172 else
5173 {
5174 LogRel(("cpumR3LoadExec: CpuId mismatch!\n"
5175 "Saved=%.*Rhxs\n"
5176 "Real =%.*Rhxs\n",
5177 sizeof(au32CpuIdSaved), au32CpuIdSaved,
5178 sizeof(au32CpuId), au32CpuId));
5179 rc = VERR_SSM_LOAD_CPUID_MISMATCH;
5180 }
5181 }
5182 }
5183
5184 return rc;
5185}
5186
5187
5188
5189/*
5190 *
5191 *
5192 * CPUID Info Handler.
5193 * CPUID Info Handler.
5194 * CPUID Info Handler.
5195 *
5196 *
5197 */
5198
5199
5200
5201/**
5202 * Get L1 cache / TLS associativity.
5203 */
5204static const char *getCacheAss(unsigned u, char *pszBuf)
5205{
5206 if (u == 0)
5207 return "res0 ";
5208 if (u == 1)
5209 return "direct";
5210 if (u == 255)
5211 return "fully";
5212 if (u >= 256)
5213 return "???";
5214
5215 RTStrPrintf(pszBuf, 16, "%d way", u);
5216 return pszBuf;
5217}
5218
5219
5220/**
5221 * Get L2 cache associativity.
5222 */
5223const char *getL2CacheAss(unsigned u)
5224{
5225 switch (u)
5226 {
5227 case 0: return "off ";
5228 case 1: return "direct";
5229 case 2: return "2 way ";
5230 case 3: return "res3 ";
5231 case 4: return "4 way ";
5232 case 5: return "res5 ";
5233 case 6: return "8 way ";
5234 case 7: return "res7 ";
5235 case 8: return "16 way";
5236 case 9: return "res9 ";
5237 case 10: return "res10 ";
5238 case 11: return "res11 ";
5239 case 12: return "res12 ";
5240 case 13: return "res13 ";
5241 case 14: return "res14 ";
5242 case 15: return "fully ";
5243 default: return "????";
5244 }
5245}
5246
5247
5248/** CPUID(1).EDX field descriptions. */
5249static DBGFREGSUBFIELD const g_aLeaf1EdxSubFields[] =
5250{
5251 DBGFREGSUBFIELD_RO("FPU\0" "x87 FPU on Chip", 0, 1, 0),
5252 DBGFREGSUBFIELD_RO("VME\0" "Virtual 8086 Mode Enhancements", 1, 1, 0),
5253 DBGFREGSUBFIELD_RO("DE\0" "Debugging extensions", 2, 1, 0),
5254 DBGFREGSUBFIELD_RO("PSE\0" "Page Size Extension", 3, 1, 0),
5255 DBGFREGSUBFIELD_RO("TSC\0" "Time Stamp Counter", 4, 1, 0),
5256 DBGFREGSUBFIELD_RO("MSR\0" "Model Specific Registers", 5, 1, 0),
5257 DBGFREGSUBFIELD_RO("PAE\0" "Physical Address Extension", 6, 1, 0),
5258 DBGFREGSUBFIELD_RO("MCE\0" "Machine Check Exception", 7, 1, 0),
5259 DBGFREGSUBFIELD_RO("CX8\0" "CMPXCHG8B instruction", 8, 1, 0),
5260 DBGFREGSUBFIELD_RO("APIC\0" "APIC On-Chip", 9, 1, 0),
5261 DBGFREGSUBFIELD_RO("SEP\0" "SYSENTER and SYSEXIT Present", 11, 1, 0),
5262 DBGFREGSUBFIELD_RO("MTRR\0" "Memory Type Range Registers", 12, 1, 0),
5263 DBGFREGSUBFIELD_RO("PGE\0" "PTE Global Bit", 13, 1, 0),
5264 DBGFREGSUBFIELD_RO("MCA\0" "Machine Check Architecture", 14, 1, 0),
5265 DBGFREGSUBFIELD_RO("CMOV\0" "Conditional Move instructions", 15, 1, 0),
5266 DBGFREGSUBFIELD_RO("PAT\0" "Page Attribute Table", 16, 1, 0),
5267 DBGFREGSUBFIELD_RO("PSE-36\0" "36-bit Page Size Extension", 17, 1, 0),
5268 DBGFREGSUBFIELD_RO("PSN\0" "Processor Serial Number", 18, 1, 0),
5269 DBGFREGSUBFIELD_RO("CLFSH\0" "CLFLUSH instruction", 19, 1, 0),
5270 DBGFREGSUBFIELD_RO("DS\0" "Debug Store", 21, 1, 0),
5271 DBGFREGSUBFIELD_RO("ACPI\0" "Thermal Mon. & Soft. Clock Ctrl.", 22, 1, 0),
5272 DBGFREGSUBFIELD_RO("MMX\0" "Intel MMX Technology", 23, 1, 0),
5273 DBGFREGSUBFIELD_RO("FXSR\0" "FXSAVE and FXRSTOR instructions", 24, 1, 0),
5274 DBGFREGSUBFIELD_RO("SSE\0" "SSE support", 25, 1, 0),
5275 DBGFREGSUBFIELD_RO("SSE2\0" "SSE2 support", 26, 1, 0),
5276 DBGFREGSUBFIELD_RO("SS\0" "Self Snoop", 27, 1, 0),
5277 DBGFREGSUBFIELD_RO("HTT\0" "Hyper-Threading Technology", 28, 1, 0),
5278 DBGFREGSUBFIELD_RO("TM\0" "Therm. Monitor", 29, 1, 0),
5279 DBGFREGSUBFIELD_RO("PBE\0" "Pending Break Enabled", 31, 1, 0),
5280 DBGFREGSUBFIELD_TERMINATOR()
5281};
5282
5283/** CPUID(1).ECX field descriptions. */
5284static DBGFREGSUBFIELD const g_aLeaf1EcxSubFields[] =
5285{
5286 DBGFREGSUBFIELD_RO("SSE3\0" "SSE3 support", 0, 1, 0),
5287 DBGFREGSUBFIELD_RO("PCLMUL\0" "PCLMULQDQ support (for AES-GCM)", 1, 1, 0),
5288 DBGFREGSUBFIELD_RO("DTES64\0" "DS Area 64-bit Layout", 2, 1, 0),
5289 DBGFREGSUBFIELD_RO("MONITOR\0" "MONITOR/MWAIT instructions", 3, 1, 0),
5290 DBGFREGSUBFIELD_RO("CPL-DS\0" "CPL Qualified Debug Store", 4, 1, 0),
5291 DBGFREGSUBFIELD_RO("VMX\0" "Virtual Machine Extensions", 5, 1, 0),
5292 DBGFREGSUBFIELD_RO("SMX\0" "Safer Mode Extensions", 6, 1, 0),
5293 DBGFREGSUBFIELD_RO("EST\0" "Enhanced SpeedStep Technology", 7, 1, 0),
5294 DBGFREGSUBFIELD_RO("TM2\0" "Terminal Monitor 2", 8, 1, 0),
5295 DBGFREGSUBFIELD_RO("SSSE3\0" "Supplemental Streaming SIMD Extensions 3", 9, 1, 0),
5296 DBGFREGSUBFIELD_RO("CNTX-ID\0" "L1 Context ID", 10, 1, 0),
5297 DBGFREGSUBFIELD_RO("SDBG\0" "Silicon Debug interface", 11, 1, 0),
5298 DBGFREGSUBFIELD_RO("FMA\0" "Fused Multiply Add extensions", 12, 1, 0),
5299 DBGFREGSUBFIELD_RO("CX16\0" "CMPXCHG16B instruction", 13, 1, 0),
5300 DBGFREGSUBFIELD_RO("TPRUPDATE\0" "xTPR Update Control", 14, 1, 0),
5301 DBGFREGSUBFIELD_RO("PDCM\0" "Perf/Debug Capability MSR", 15, 1, 0),
5302 DBGFREGSUBFIELD_RO("PCID\0" "Process Context Identifiers", 17, 1, 0),
5303 DBGFREGSUBFIELD_RO("DCA\0" "Direct Cache Access", 18, 1, 0),
5304 DBGFREGSUBFIELD_RO("SSE4_1\0" "SSE4_1 support", 19, 1, 0),
5305 DBGFREGSUBFIELD_RO("SSE4_2\0" "SSE4_2 support", 20, 1, 0),
5306 DBGFREGSUBFIELD_RO("X2APIC\0" "x2APIC support", 21, 1, 0),
5307 DBGFREGSUBFIELD_RO("MOVBE\0" "MOVBE instruction", 22, 1, 0),
5308 DBGFREGSUBFIELD_RO("POPCNT\0" "POPCNT instruction", 23, 1, 0),
5309 DBGFREGSUBFIELD_RO("TSCDEADL\0" "Time Stamp Counter Deadline", 24, 1, 0),
5310 DBGFREGSUBFIELD_RO("AES\0" "AES instructions", 25, 1, 0),
5311 DBGFREGSUBFIELD_RO("XSAVE\0" "XSAVE instruction", 26, 1, 0),
5312 DBGFREGSUBFIELD_RO("OSXSAVE\0" "OSXSAVE instruction", 27, 1, 0),
5313 DBGFREGSUBFIELD_RO("AVX\0" "AVX support", 28, 1, 0),
5314 DBGFREGSUBFIELD_RO("F16C\0" "16-bit floating point conversion instructions", 29, 1, 0),
5315 DBGFREGSUBFIELD_RO("RDRAND\0" "RDRAND instruction", 30, 1, 0),
5316 DBGFREGSUBFIELD_RO("HVP\0" "Hypervisor Present (we're a guest)", 31, 1, 0),
5317 DBGFREGSUBFIELD_TERMINATOR()
5318};
5319
5320/** CPUID(7,0).EBX field descriptions. */
5321static DBGFREGSUBFIELD const g_aLeaf7Sub0EbxSubFields[] =
5322{
5323 DBGFREGSUBFIELD_RO("FSGSBASE\0" "RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE instr.", 0, 1, 0),
5324 DBGFREGSUBFIELD_RO("TSCADJUST\0" "Supports MSR_IA32_TSC_ADJUST", 1, 1, 0),
5325 DBGFREGSUBFIELD_RO("BMI1\0" "Advanced Bit Manipulation extension 1", 3, 1, 0),
5326 DBGFREGSUBFIELD_RO("HLE\0" "Hardware Lock Elision", 4, 1, 0),
5327 DBGFREGSUBFIELD_RO("AVX2\0" "Advanced Vector Extensions 2", 5, 1, 0),
5328 DBGFREGSUBFIELD_RO("SMEP\0" "Supervisor Mode Execution Prevention", 7, 1, 0),
5329 DBGFREGSUBFIELD_RO("BMI2\0" "Advanced Bit Manipulation extension 2", 8, 1, 0),
5330 DBGFREGSUBFIELD_RO("ERMS\0" "Enhanced REP MOVSB/STOSB instructions", 9, 1, 0),
5331 DBGFREGSUBFIELD_RO("INVPCID\0" "INVPCID instruction", 10, 1, 0),
5332 DBGFREGSUBFIELD_RO("RTM\0" "Restricted Transactional Memory", 11, 1, 0),
5333 DBGFREGSUBFIELD_RO("PQM\0" "Platform Quality of Service Monitoring", 12, 1, 0),
5334 DBGFREGSUBFIELD_RO("DEPFPU_CS_DS\0" "Deprecates FPU CS, FPU DS values if set", 13, 1, 0),
5335 DBGFREGSUBFIELD_RO("MPE\0" "Intel Memory Protection Extensions", 14, 1, 0),
5336 DBGFREGSUBFIELD_RO("PQE\0" "Platform Quality of Service Enforcement", 15, 1, 0),
5337 DBGFREGSUBFIELD_RO("AVX512F\0" "AVX512 Foundation instructions", 16, 1, 0),
5338 DBGFREGSUBFIELD_RO("RDSEED\0" "RDSEED instruction", 18, 1, 0),
5339 DBGFREGSUBFIELD_RO("ADX\0" "ADCX/ADOX instructions", 19, 1, 0),
5340 DBGFREGSUBFIELD_RO("SMAP\0" "Supervisor Mode Access Prevention", 20, 1, 0),
5341 DBGFREGSUBFIELD_RO("CLFLUSHOPT\0" "CLFLUSHOPT (Cache Line Flush) instruction", 23, 1, 0),
5342 DBGFREGSUBFIELD_RO("INTEL_PT\0" "Intel Processor Trace", 25, 1, 0),
5343 DBGFREGSUBFIELD_RO("AVX512PF\0" "AVX512 Prefetch instructions", 26, 1, 0),
5344 DBGFREGSUBFIELD_RO("AVX512ER\0" "AVX512 Exponential & Reciprocal instructions", 27, 1, 0),
5345 DBGFREGSUBFIELD_RO("AVX512CD\0" "AVX512 Conflict Detection instructions", 28, 1, 0),
5346 DBGFREGSUBFIELD_RO("SHA\0" "Secure Hash Algorithm extensions", 29, 1, 0),
5347 DBGFREGSUBFIELD_TERMINATOR()
5348};
5349
5350/** CPUID(7,0).ECX field descriptions. */
5351static DBGFREGSUBFIELD const g_aLeaf7Sub0EcxSubFields[] =
5352{
5353 DBGFREGSUBFIELD_RO("PREFETCHWT1\0" "PREFETCHWT1 instruction", 0, 1, 0),
5354 DBGFREGSUBFIELD_RO("PKU\0" "Protection Key for Usermode pages", 3, 1, 0),
5355 DBGFREGSUBFIELD_RO("OSPKU\0" "CR4.PKU mirror", 4, 1, 0),
5356 DBGFREGSUBFIELD_TERMINATOR()
5357};
5358
5359
5360/** CPUID(13,0).EAX+EDX, XCR0, ++ bit descriptions. */
5361static DBGFREGSUBFIELD const g_aXSaveStateBits[] =
5362{
5363 DBGFREGSUBFIELD_RO("x87\0" "Legacy FPU state", 0, 1, 0),
5364 DBGFREGSUBFIELD_RO("SSE\0" "128-bit SSE state", 1, 1, 0),
5365 DBGFREGSUBFIELD_RO("YMM_Hi128\0" "Upper 128 bits of YMM0-15 (AVX)", 2, 1, 0),
5366 DBGFREGSUBFIELD_RO("BNDREGS\0" "MPX bound register state", 3, 1, 0),
5367 DBGFREGSUBFIELD_RO("BNDCSR\0" "MPX bound config and status state", 4, 1, 0),
5368 DBGFREGSUBFIELD_RO("Opmask\0" "opmask state", 5, 1, 0),
5369 DBGFREGSUBFIELD_RO("ZMM_Hi256\0" "Upper 256 bits of ZMM0-15 (AVX-512)", 6, 1, 0),
5370 DBGFREGSUBFIELD_RO("Hi16_ZMM\0" "512-bits ZMM16-31 state (AVX-512)", 7, 1, 0),
5371 DBGFREGSUBFIELD_RO("LWP\0" "Lightweight Profiling (AMD)", 62, 1, 0),
5372 DBGFREGSUBFIELD_TERMINATOR()
5373};
5374
5375/** CPUID(13,1).EAX field descriptions. */
5376static DBGFREGSUBFIELD const g_aLeaf13Sub1EaxSubFields[] =
5377{
5378 DBGFREGSUBFIELD_RO("XSAVEOPT\0" "XSAVEOPT is available", 0, 1, 0),
5379 DBGFREGSUBFIELD_RO("XSAVEC\0" "XSAVEC and compacted XRSTOR supported", 1, 1, 0),
5380 DBGFREGSUBFIELD_RO("XGETBC1\0" "XGETBV with ECX=1 supported", 2, 1, 0),
5381 DBGFREGSUBFIELD_RO("XSAVES\0" "XSAVES/XRSTORS and IA32_XSS supported", 3, 1, 0),
5382 DBGFREGSUBFIELD_TERMINATOR()
5383};
5384
5385
5386/** CPUID(0x80000001,0).EDX field descriptions. */
5387static DBGFREGSUBFIELD const g_aExtLeaf1EdxSubFields[] =
5388{
5389 DBGFREGSUBFIELD_RO("FPU\0" "x87 FPU on Chip", 0, 1, 0),
5390 DBGFREGSUBFIELD_RO("VME\0" "Virtual 8086 Mode Enhancements", 1, 1, 0),
5391 DBGFREGSUBFIELD_RO("DE\0" "Debugging extensions", 2, 1, 0),
5392 DBGFREGSUBFIELD_RO("PSE\0" "Page Size Extension", 3, 1, 0),
5393 DBGFREGSUBFIELD_RO("TSC\0" "Time Stamp Counter", 4, 1, 0),
5394 DBGFREGSUBFIELD_RO("MSR\0" "K86 Model Specific Registers", 5, 1, 0),
5395 DBGFREGSUBFIELD_RO("PAE\0" "Physical Address Extension", 6, 1, 0),
5396 DBGFREGSUBFIELD_RO("MCE\0" "Machine Check Exception", 7, 1, 0),
5397 DBGFREGSUBFIELD_RO("CX8\0" "CMPXCHG8B instruction", 8, 1, 0),
5398 DBGFREGSUBFIELD_RO("APIC\0" "APIC On-Chip", 9, 1, 0),
5399 DBGFREGSUBFIELD_RO("SEP\0" "SYSCALL/SYSRET", 11, 1, 0),
5400 DBGFREGSUBFIELD_RO("MTRR\0" "Memory Type Range Registers", 12, 1, 0),
5401 DBGFREGSUBFIELD_RO("PGE\0" "PTE Global Bit", 13, 1, 0),
5402 DBGFREGSUBFIELD_RO("MCA\0" "Machine Check Architecture", 14, 1, 0),
5403 DBGFREGSUBFIELD_RO("CMOV\0" "Conditional Move instructions", 15, 1, 0),
5404 DBGFREGSUBFIELD_RO("PAT\0" "Page Attribute Table", 16, 1, 0),
5405 DBGFREGSUBFIELD_RO("PSE-36\0" "36-bit Page Size Extension", 17, 1, 0),
5406 DBGFREGSUBFIELD_RO("NX\0" "No-Execute/Execute-Disable", 20, 1, 0),
5407 DBGFREGSUBFIELD_RO("AXMMX\0" "AMD Extensions to MMX instructions", 22, 1, 0),
5408 DBGFREGSUBFIELD_RO("MMX\0" "Intel MMX Technology", 23, 1, 0),
5409 DBGFREGSUBFIELD_RO("FXSR\0" "FXSAVE and FXRSTOR Instructions", 24, 1, 0),
5410 DBGFREGSUBFIELD_RO("FFXSR\0" "AMD fast FXSAVE and FXRSTOR instructions", 25, 1, 0),
5411 DBGFREGSUBFIELD_RO("Page1GB\0" "1 GB large page", 26, 1, 0),
5412 DBGFREGSUBFIELD_RO("RDTSCP\0" "RDTSCP instruction", 27, 1, 0),
5413 DBGFREGSUBFIELD_RO("LM\0" "AMD64 Long Mode", 29, 1, 0),
5414 DBGFREGSUBFIELD_RO("3DNOWEXT\0" "AMD Extensions to 3DNow", 30, 1, 0),
5415 DBGFREGSUBFIELD_RO("3DNOW\0" "AMD 3DNow", 31, 1, 0),
5416 DBGFREGSUBFIELD_TERMINATOR()
5417};
5418
5419/** CPUID(0x80000001,0).ECX field descriptions. */
5420static DBGFREGSUBFIELD const g_aExtLeaf1EcxSubFields[] =
5421{
5422 DBGFREGSUBFIELD_RO("LahfSahf\0" "LAHF/SAHF support in 64-bit mode", 0, 1, 0),
5423 DBGFREGSUBFIELD_RO("CmpLegacy\0" "Core multi-processing legacy mode", 1, 1, 0),
5424 DBGFREGSUBFIELD_RO("SVM\0" "AMD VM extensions", 2, 1, 0),
5425 DBGFREGSUBFIELD_RO("EXTAPIC\0" "AMD Extended APIC registers", 3, 1, 0),
5426 DBGFREGSUBFIELD_RO("CR8L\0" "AMD LOCK MOV CR0 means MOV CR8", 4, 1, 0),
5427 DBGFREGSUBFIELD_RO("ABM\0" "AMD Advanced Bit Manipulation", 5, 1, 0),
5428 DBGFREGSUBFIELD_RO("SSE4A\0" "SSE4A instructions", 6, 1, 0),
5429 DBGFREGSUBFIELD_RO("MISALIGNSSE\0" "AMD Misaligned SSE mode", 7, 1, 0),
5430 DBGFREGSUBFIELD_RO("3DNOWPRF\0" "AMD PREFETCH and PREFETCHW instructions", 8, 1, 0),
5431 DBGFREGSUBFIELD_RO("OSVW\0" "AMD OS Visible Workaround", 9, 1, 0),
5432 DBGFREGSUBFIELD_RO("IBS\0" "Instruct Based Sampling", 10, 1, 0),
5433 DBGFREGSUBFIELD_RO("XOP\0" "Extended Operation support", 11, 1, 0),
5434 DBGFREGSUBFIELD_RO("SKINIT\0" "SKINIT, STGI, and DEV support", 12, 1, 0),
5435 DBGFREGSUBFIELD_RO("WDT\0" "AMD Watchdog Timer support", 13, 1, 0),
5436 DBGFREGSUBFIELD_RO("LWP\0" "Lightweight Profiling support", 15, 1, 0),
5437 DBGFREGSUBFIELD_RO("FMA4\0" "Four operand FMA instruction support", 16, 1, 0),
5438 DBGFREGSUBFIELD_RO("NodeId\0" "NodeId in MSR C001_100C", 19, 1, 0),
5439 DBGFREGSUBFIELD_RO("TBM\0" "Trailing Bit Manipulation instructions", 21, 1, 0),
5440 DBGFREGSUBFIELD_RO("TOPOEXT\0" "Topology Extensions", 22, 1, 0),
5441 DBGFREGSUBFIELD_TERMINATOR()
5442};
5443
5444
5445static void cpumR3CpuIdInfoMnemonicListU32(PCDBGFINFOHLP pHlp, uint32_t uVal, PCDBGFREGSUBFIELD pDesc,
5446 const char *pszLeadIn, uint32_t cchWidth)
5447{
5448 if (pszLeadIn)
5449 pHlp->pfnPrintf(pHlp, "%*s", cchWidth, pszLeadIn);
5450
5451 for (uint32_t iBit = 0; iBit < 32; iBit++)
5452 if (RT_BIT_32(iBit) & uVal)
5453 {
5454 while ( pDesc->pszName != NULL
5455 && iBit >= (uint32_t)pDesc->iFirstBit + pDesc->cBits)
5456 pDesc++;
5457 if ( pDesc->pszName != NULL
5458 && iBit - (uint32_t)pDesc->iFirstBit < (uint32_t)pDesc->cBits)
5459 {
5460 if (pDesc->cBits == 1)
5461 pHlp->pfnPrintf(pHlp, " %s", pDesc->pszName);
5462 else
5463 {
5464 uint32_t uFieldValue = uVal >> pDesc->iFirstBit;
5465 if (pDesc->cBits < 32)
5466 uFieldValue &= RT_BIT_32(pDesc->cBits) - UINT32_C(1);
5467 pHlp->pfnPrintf(pHlp, pDesc->cBits < 4 ? " %s=%u" : " %s=%#x", pDesc->pszName, uFieldValue);
5468 iBit = pDesc->iFirstBit + pDesc->cBits - 1;
5469 }
5470 }
5471 else
5472 pHlp->pfnPrintf(pHlp, " %u", iBit);
5473 }
5474 if (pszLeadIn)
5475 pHlp->pfnPrintf(pHlp, "\n");
5476}
5477
5478
5479static void cpumR3CpuIdInfoMnemonicListU64(PCDBGFINFOHLP pHlp, uint64_t uVal, PCDBGFREGSUBFIELD pDesc,
5480 const char *pszLeadIn, uint32_t cchWidth)
5481{
5482 if (pszLeadIn)
5483 pHlp->pfnPrintf(pHlp, "%*s", cchWidth, pszLeadIn);
5484
5485 for (uint32_t iBit = 0; iBit < 64; iBit++)
5486 if (RT_BIT_64(iBit) & uVal)
5487 {
5488 while ( pDesc->pszName != NULL
5489 && iBit >= (uint32_t)pDesc->iFirstBit + pDesc->cBits)
5490 pDesc++;
5491 if ( pDesc->pszName != NULL
5492 && iBit - (uint32_t)pDesc->iFirstBit < (uint32_t)pDesc->cBits)
5493 {
5494 if (pDesc->cBits == 1)
5495 pHlp->pfnPrintf(pHlp, " %s", pDesc->pszName);
5496 else
5497 {
5498 uint64_t uFieldValue = uVal >> pDesc->iFirstBit;
5499 if (pDesc->cBits < 64)
5500 uFieldValue &= RT_BIT_64(pDesc->cBits) - UINT64_C(1);
5501 pHlp->pfnPrintf(pHlp, pDesc->cBits < 4 ? " %s=%llu" : " %s=%#llx", pDesc->pszName, uFieldValue);
5502 iBit = pDesc->iFirstBit + pDesc->cBits - 1;
5503 }
5504 }
5505 else
5506 pHlp->pfnPrintf(pHlp, " %u", iBit);
5507 }
5508 if (pszLeadIn)
5509 pHlp->pfnPrintf(pHlp, "\n");
5510}
5511
5512
5513static void cpumR3CpuIdInfoValueWithMnemonicListU64(PCDBGFINFOHLP pHlp, uint64_t uVal, PCDBGFREGSUBFIELD pDesc,
5514 const char *pszLeadIn, uint32_t cchWidth)
5515{
5516 if (!uVal)
5517 pHlp->pfnPrintf(pHlp, "%*s %#010x`%08x\n", cchWidth, pszLeadIn, RT_HI_U32(uVal), RT_LO_U32(uVal));
5518 else
5519 {
5520 pHlp->pfnPrintf(pHlp, "%*s %#010x`%08x (", cchWidth, pszLeadIn, RT_HI_U32(uVal), RT_LO_U32(uVal));
5521 cpumR3CpuIdInfoMnemonicListU64(pHlp, uVal, pDesc, NULL, 0);
5522 pHlp->pfnPrintf(pHlp, " )\n");
5523 }
5524}
5525
5526
5527static void cpumR3CpuIdInfoVerboseCompareListU32(PCDBGFINFOHLP pHlp, uint32_t uVal1, uint32_t uVal2, PCDBGFREGSUBFIELD pDesc,
5528 uint32_t cchWidth)
5529{
5530 uint32_t uCombined = uVal1 | uVal2;
5531 for (uint32_t iBit = 0; iBit < 32; iBit++)
5532 if ( (RT_BIT_32(iBit) & uCombined)
5533 || (iBit == pDesc->iFirstBit && pDesc->pszName) )
5534 {
5535 while ( pDesc->pszName != NULL
5536 && iBit >= (uint32_t)pDesc->iFirstBit + pDesc->cBits)
5537 pDesc++;
5538
5539 if ( pDesc->pszName != NULL
5540 && iBit - (uint32_t)pDesc->iFirstBit < (uint32_t)pDesc->cBits)
5541 {
5542 size_t cchMnemonic = strlen(pDesc->pszName);
5543 const char *pszDesc = pDesc->pszName + cchMnemonic + 1;
5544 size_t cchDesc = strlen(pszDesc);
5545 uint32_t uFieldValue1 = uVal1 >> pDesc->iFirstBit;
5546 uint32_t uFieldValue2 = uVal2 >> pDesc->iFirstBit;
5547 if (pDesc->cBits < 32)
5548 {
5549 uFieldValue1 &= RT_BIT_32(pDesc->cBits) - UINT32_C(1);
5550 uFieldValue2 &= RT_BIT_32(pDesc->cBits) - UINT32_C(1);
5551 }
5552
5553 pHlp->pfnPrintf(pHlp, pDesc->cBits < 4 ? " %s - %s%*s= %u (%u)\n" : " %s - %s%*s= %#x (%#x)\n",
5554 pDesc->pszName, pszDesc,
5555 cchMnemonic + 3 + cchDesc < cchWidth ? cchWidth - (cchMnemonic + 3 + cchDesc) : 1, "",
5556 uFieldValue1, uFieldValue2);
5557
5558 iBit = pDesc->iFirstBit + pDesc->cBits - 1U;
5559 pDesc++;
5560 }
5561 else
5562 pHlp->pfnPrintf(pHlp, " %2u - Reserved%*s= %u (%u)\n", iBit, 13 < cchWidth ? cchWidth - 13 : 1, "",
5563 RT_BOOL(uVal1 & RT_BIT_32(iBit)), RT_BOOL(uVal2 & RT_BIT_32(iBit)));
5564 }
5565}
5566
5567
5568/**
5569 * Produces a detailed summary of standard leaf 0x00000001.
5570 *
5571 * @param pHlp The info helper functions.
5572 * @param paLeaves The CPUID leaves array.
5573 * @param cLeaves The number of leaves in the array.
5574 * @param pCurLeaf The 0x00000001 leaf.
5575 * @param fVerbose Whether to be very verbose or not.
5576 * @param fIntel Set if intel CPU.
5577 */
5578static void cpumR3CpuIdInfoStdLeaf1Details(PCDBGFINFOHLP pHlp, PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves,
5579 PCCPUMCPUIDLEAF pCurLeaf, bool fVerbose, bool fIntel)
5580{
5581 Assert(pCurLeaf); Assert(pCurLeaf->uLeaf == 1);
5582 static const char * const s_apszTypes[4] = { "primary", "overdrive", "MP", "reserved" };
5583 uint32_t uEAX = pCurLeaf->uEax;
5584 uint32_t uEBX = pCurLeaf->uEbx;
5585
5586 pHlp->pfnPrintf(pHlp,
5587 "%36s %2d \tExtended: %d \tEffective: %d\n"
5588 "%36s %2d \tExtended: %d \tEffective: %d\n"
5589 "%36s %d\n"
5590 "%36s %d (%s)\n"
5591 "%36s %#04x\n"
5592 "%36s %d\n"
5593 "%36s %d\n"
5594 "%36s %#04x\n"
5595 ,
5596 "Family:", (uEAX >> 8) & 0xf, (uEAX >> 20) & 0x7f, ASMGetCpuFamily(uEAX),
5597 "Model:", (uEAX >> 4) & 0xf, (uEAX >> 16) & 0x0f, ASMGetCpuModel(uEAX, fIntel),
5598 "Stepping:", ASMGetCpuStepping(uEAX),
5599 "Type:", (uEAX >> 12) & 3, s_apszTypes[(uEAX >> 12) & 3],
5600 "APIC ID:", (uEBX >> 24) & 0xff,
5601 "Logical CPUs:",(uEBX >> 16) & 0xff,
5602 "CLFLUSH Size:",(uEBX >> 8) & 0xff,
5603 "Brand ID:", (uEBX >> 0) & 0xff);
5604 if (fVerbose)
5605 {
5606 CPUMCPUID Host;
5607 ASMCpuIdExSlow(1, 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5608 pHlp->pfnPrintf(pHlp, "Features\n");
5609 pHlp->pfnPrintf(pHlp, " Mnemonic - Description = guest (host)\n");
5610 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEdx, Host.uEdx, g_aLeaf1EdxSubFields, 56);
5611 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEcx, Host.uEcx, g_aLeaf1EcxSubFields, 56);
5612 }
5613 else
5614 {
5615 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEdx, g_aLeaf1EdxSubFields, "Features EDX:", 36);
5616 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEcx, g_aLeaf1EcxSubFields, "Features ECX:", 36);
5617 }
5618}
5619
5620
5621/**
5622 * Produces a detailed summary of standard leaf 0x00000007.
5623 *
5624 * @param pHlp The info helper functions.
5625 * @param paLeaves The CPUID leaves array.
5626 * @param cLeaves The number of leaves in the array.
5627 * @param pCurLeaf The first 0x00000007 leaf.
5628 * @param fVerbose Whether to be very verbose or not.
5629 */
5630static void cpumR3CpuIdInfoStdLeaf7Details(PCDBGFINFOHLP pHlp, PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves,
5631 PCCPUMCPUIDLEAF pCurLeaf, bool fVerbose)
5632{
5633 Assert(pCurLeaf); Assert(pCurLeaf->uLeaf == 7);
5634 pHlp->pfnPrintf(pHlp, "Structured Extended Feature Flags Enumeration (leaf 7):\n");
5635 for (;;)
5636 {
5637 CPUMCPUID Host;
5638 ASMCpuIdExSlow(pCurLeaf->uLeaf, 0, pCurLeaf->uSubLeaf, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5639
5640 switch (pCurLeaf->uSubLeaf)
5641 {
5642 case 0:
5643 if (fVerbose)
5644 {
5645 pHlp->pfnPrintf(pHlp, " Mnemonic - Description = guest (host)\n");
5646 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEbx, Host.uEbx, g_aLeaf7Sub0EbxSubFields, 56);
5647 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEcx, Host.uEcx, g_aLeaf7Sub0EcxSubFields, 56);
5648 if (pCurLeaf->uEdx || Host.uEdx)
5649 pHlp->pfnPrintf(pHlp, "%36 %#x (%#x)\n", "Ext Features EDX:", pCurLeaf->uEdx, Host.uEdx);
5650 }
5651 else
5652 {
5653 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEbx, g_aLeaf7Sub0EbxSubFields, "Ext Features EBX:", 36);
5654 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEcx, g_aLeaf7Sub0EcxSubFields, "Ext Features ECX:", 36);
5655 if (pCurLeaf->uEdx)
5656 pHlp->pfnPrintf(pHlp, "%36 %#x\n", "Ext Features EDX:", pCurLeaf->uEdx);
5657 }
5658 break;
5659
5660 default:
5661 if (pCurLeaf->uEdx || pCurLeaf->uEcx || pCurLeaf->uEbx)
5662 pHlp->pfnPrintf(pHlp, "Unknown extended feature sub-leaf #%u: EAX=%#x EBX=%#x ECX=%#x EDX=%#x\n",
5663 pCurLeaf->uSubLeaf, pCurLeaf->uEax, pCurLeaf->uEbx, pCurLeaf->uEcx, pCurLeaf->uEdx);
5664 break;
5665
5666 }
5667
5668 /* advance. */
5669 pCurLeaf++;
5670 if ( (uintptr_t)(pCurLeaf - paLeaves) >= cLeaves
5671 || pCurLeaf->uLeaf != 0x7)
5672 break;
5673 }
5674}
5675
5676
5677/**
5678 * Produces a detailed summary of standard leaf 0x0000000d.
5679 *
5680 * @param pHlp The info helper functions.
5681 * @param paLeaves The CPUID leaves array.
5682 * @param cLeaves The number of leaves in the array.
5683 * @param pCurLeaf The first 0x00000007 leaf.
5684 * @param fVerbose Whether to be very verbose or not.
5685 */
5686static void cpumR3CpuIdInfoStdLeaf13Details(PCDBGFINFOHLP pHlp, PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves,
5687 PCCPUMCPUIDLEAF pCurLeaf, bool fVerbose)
5688{
5689 Assert(pCurLeaf); Assert(pCurLeaf->uLeaf == 13);
5690 pHlp->pfnPrintf(pHlp, "Processor Extended State Enumeration (leaf 0xd):\n");
5691 for (uint32_t uSubLeaf = 0; uSubLeaf < 64; uSubLeaf++)
5692 {
5693 CPUMCPUID Host;
5694 ASMCpuIdExSlow(UINT32_C(0x0000000d), 0, uSubLeaf, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5695
5696 switch (uSubLeaf)
5697 {
5698 case 0:
5699 if (pCurLeaf && pCurLeaf->uSubLeaf == uSubLeaf)
5700 pHlp->pfnPrintf(pHlp, "%42s %#x/%#x\n", "XSAVE area cur/max size by XCR0, guest:",
5701 pCurLeaf->uEbx, pCurLeaf->uEcx);
5702 pHlp->pfnPrintf(pHlp, "%42s %#x/%#x\n", "XSAVE area cur/max size by XCR0, host:", Host.uEbx, Host.uEcx);
5703
5704 if (pCurLeaf && pCurLeaf->uSubLeaf == uSubLeaf)
5705 cpumR3CpuIdInfoValueWithMnemonicListU64(pHlp, RT_MAKE_U64(pCurLeaf->uEax, pCurLeaf->uEdx), g_aXSaveStateBits,
5706 "Valid XCR0 bits, guest:", 42);
5707 cpumR3CpuIdInfoValueWithMnemonicListU64(pHlp, RT_MAKE_U64(Host.uEax, Host.uEdx), g_aXSaveStateBits,
5708 "Valid XCR0 bits, host:", 42);
5709 break;
5710
5711 case 1:
5712 if (pCurLeaf && pCurLeaf->uSubLeaf == uSubLeaf)
5713 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEax, g_aLeaf13Sub1EaxSubFields, "XSAVE features, guest:", 42);
5714 cpumR3CpuIdInfoMnemonicListU32(pHlp, Host.uEax, g_aLeaf13Sub1EaxSubFields, "XSAVE features, host:", 42);
5715
5716 if (pCurLeaf && pCurLeaf->uSubLeaf == uSubLeaf)
5717 pHlp->pfnPrintf(pHlp, "%42s %#x\n", "XSAVE area cur size XCR0|XSS, guest:", pCurLeaf->uEbx);
5718 pHlp->pfnPrintf(pHlp, "%42s %#x\n", "XSAVE area cur size XCR0|XSS, host:", Host.uEbx);
5719
5720 if (pCurLeaf && pCurLeaf->uSubLeaf == uSubLeaf)
5721 cpumR3CpuIdInfoValueWithMnemonicListU64(pHlp, RT_MAKE_U64(pCurLeaf->uEcx, pCurLeaf->uEdx), g_aXSaveStateBits,
5722 " Valid IA32_XSS bits, guest:", 42);
5723 cpumR3CpuIdInfoValueWithMnemonicListU64(pHlp, RT_MAKE_U64(Host.uEdx, Host.uEcx), g_aXSaveStateBits,
5724 " Valid IA32_XSS bits, host:", 42);
5725 break;
5726
5727 default:
5728 if ( pCurLeaf
5729 && pCurLeaf->uSubLeaf == uSubLeaf
5730 && (pCurLeaf->uEax || pCurLeaf->uEbx || pCurLeaf->uEcx || pCurLeaf->uEdx) )
5731 {
5732 pHlp->pfnPrintf(pHlp, " State #%u, guest: off=%#06x, cb=%#06x %s", uSubLeaf, pCurLeaf->uEbx,
5733 pCurLeaf->uEax, pCurLeaf->uEcx & RT_BIT_32(0) ? "XCR0-bit" : "IA32_XSS-bit");
5734 if (pCurLeaf->uEcx & ~RT_BIT_32(0))
5735 pHlp->pfnPrintf(pHlp, " ECX[reserved]=%#x\n", pCurLeaf->uEcx & ~RT_BIT_32(0));
5736 if (pCurLeaf->uEdx)
5737 pHlp->pfnPrintf(pHlp, " EDX[reserved]=%#x\n", pCurLeaf->uEdx);
5738 pHlp->pfnPrintf(pHlp, " --");
5739 cpumR3CpuIdInfoMnemonicListU64(pHlp, RT_BIT_64(uSubLeaf), g_aXSaveStateBits, NULL, 0);
5740 pHlp->pfnPrintf(pHlp, "\n");
5741 }
5742 if (Host.uEax || Host.uEbx || Host.uEcx || Host.uEdx)
5743 {
5744 pHlp->pfnPrintf(pHlp, " State #%u, host: off=%#06x, cb=%#06x %s", uSubLeaf, Host.uEbx,
5745 Host.uEax, Host.uEcx & RT_BIT_32(0) ? "XCR0-bit" : "IA32_XSS-bit");
5746 if (Host.uEcx & ~RT_BIT_32(0))
5747 pHlp->pfnPrintf(pHlp, " ECX[reserved]=%#x\n", Host.uEcx & ~RT_BIT_32(0));
5748 if (Host.uEdx)
5749 pHlp->pfnPrintf(pHlp, " EDX[reserved]=%#x\n", Host.uEdx);
5750 pHlp->pfnPrintf(pHlp, " --");
5751 cpumR3CpuIdInfoMnemonicListU64(pHlp, RT_BIT_64(uSubLeaf), g_aXSaveStateBits, NULL, 0);
5752 pHlp->pfnPrintf(pHlp, "\n");
5753 }
5754 break;
5755
5756 }
5757
5758 /* advance. */
5759 if (pCurLeaf)
5760 {
5761 while ( (uintptr_t)(pCurLeaf - paLeaves) < cLeaves
5762 && pCurLeaf->uSubLeaf <= uSubLeaf
5763 && pCurLeaf->uLeaf == UINT32_C(0x0000000d))
5764 pCurLeaf++;
5765 if ( (uintptr_t)(pCurLeaf - paLeaves) >= cLeaves
5766 || pCurLeaf->uLeaf != UINT32_C(0x0000000d))
5767 pCurLeaf = NULL;
5768 }
5769 }
5770}
5771
5772
5773static PCCPUMCPUIDLEAF cpumR3CpuIdInfoRawRange(PCDBGFINFOHLP pHlp, PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves,
5774 PCCPUMCPUIDLEAF pCurLeaf, uint32_t uUpToLeaf, const char *pszTitle)
5775{
5776 if ( (uintptr_t)(pCurLeaf - paLeaves) < cLeaves
5777 && pCurLeaf->uLeaf <= uUpToLeaf)
5778 {
5779 pHlp->pfnPrintf(pHlp,
5780 " %s\n"
5781 " Leaf/sub-leaf eax ebx ecx edx\n", pszTitle);
5782 while ( (uintptr_t)(pCurLeaf - paLeaves) < cLeaves
5783 && pCurLeaf->uLeaf <= uUpToLeaf)
5784 {
5785 CPUMCPUID Host;
5786 ASMCpuIdExSlow(pCurLeaf->uLeaf, 0, pCurLeaf->uSubLeaf, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5787 pHlp->pfnPrintf(pHlp,
5788 "Gst: %08x/%04x %08x %08x %08x %08x\n"
5789 "Hst: %08x %08x %08x %08x\n",
5790 pCurLeaf->uLeaf, pCurLeaf->uSubLeaf, pCurLeaf->uEax, pCurLeaf->uEbx, pCurLeaf->uEcx, pCurLeaf->uEdx,
5791 Host.uEax, Host.uEbx, Host.uEcx, Host.uEdx);
5792 pCurLeaf++;
5793 }
5794 }
5795
5796 return pCurLeaf;
5797}
5798
5799
5800/**
5801 * Display the guest CpuId leaves.
5802 *
5803 * @param pVM Pointer to the VM.
5804 * @param pHlp The info helper functions.
5805 * @param pszArgs "terse", "default" or "verbose".
5806 */
5807DECLCALLBACK(void) cpumR3CpuIdInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
5808{
5809 /*
5810 * Parse the argument.
5811 */
5812 unsigned iVerbosity = 1;
5813 if (pszArgs)
5814 {
5815 pszArgs = RTStrStripL(pszArgs);
5816 if (!strcmp(pszArgs, "terse"))
5817 iVerbosity--;
5818 else if (!strcmp(pszArgs, "verbose"))
5819 iVerbosity++;
5820 }
5821
5822 uint32_t uLeaf;
5823 CPUMCPUID Host;
5824 uint32_t cLeaves = pVM->cpum.s.GuestInfo.cCpuIdLeaves;
5825 PCPUMCPUIDLEAF paLeaves = pVM->cpum.s.GuestInfo.paCpuIdLeavesR3;
5826 PCCPUMCPUIDLEAF pCurLeaf;
5827 PCCPUMCPUIDLEAF pNextLeaf;
5828 bool const fIntel = ASMIsIntelCpuEx(pVM->cpum.s.aGuestCpuIdPatmStd[0].uEbx,
5829 pVM->cpum.s.aGuestCpuIdPatmStd[0].uEcx,
5830 pVM->cpum.s.aGuestCpuIdPatmStd[0].uEdx);
5831
5832 /*
5833 * Standard leaves. Custom raw dump here due to ECX sub-leaves host handling.
5834 */
5835 uint32_t cHstMax = ASMCpuId_EAX(0);
5836 uint32_t cGstMax = paLeaves[0].uLeaf == 0 ? paLeaves[0].uEax : 0;
5837 uint32_t cMax = RT_MAX(cGstMax, cHstMax);
5838 pHlp->pfnPrintf(pHlp,
5839 " Raw Standard CPUID Leaves\n"
5840 " Leaf/sub-leaf eax ebx ecx edx\n");
5841 for (uLeaf = 0, pCurLeaf = paLeaves; uLeaf <= cMax; uLeaf++)
5842 {
5843 uint32_t cMaxSubLeaves = 1;
5844 if (uLeaf == 4 || uLeaf == 7 || uLeaf == 0xb)
5845 cMaxSubLeaves = 16;
5846 else if (uLeaf == 0xd)
5847 cMaxSubLeaves = 128;
5848
5849 for (uint32_t uSubLeaf = 0; uSubLeaf < cMaxSubLeaves; uSubLeaf++)
5850 {
5851 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5852 if ( (uintptr_t)(pCurLeaf - paLeaves) < cLeaves
5853 && pCurLeaf->uLeaf == uLeaf
5854 && pCurLeaf->uSubLeaf == uSubLeaf)
5855 {
5856 pHlp->pfnPrintf(pHlp,
5857 "Gst: %08x/%04x %08x %08x %08x %08x\n"
5858 "Hst: %08x %08x %08x %08x\n",
5859 uLeaf, uSubLeaf, pCurLeaf->uEax, pCurLeaf->uEbx, pCurLeaf->uEcx, pCurLeaf->uEdx,
5860 Host.uEax, Host.uEbx, Host.uEcx, Host.uEdx);
5861 pCurLeaf++;
5862 }
5863 else if ( uLeaf != 0xd
5864 || uSubLeaf <= 1
5865 || Host.uEbx != 0 )
5866 pHlp->pfnPrintf(pHlp,
5867 "Hst: %08x/%04x %08x %08x %08x %08x\n",
5868 uLeaf, uSubLeaf, Host.uEax, Host.uEbx, Host.uEcx, Host.uEdx);
5869
5870 /* Done? */
5871 if ( ( (uintptr_t)(pCurLeaf - paLeaves) >= cLeaves
5872 || pCurLeaf->uLeaf != uLeaf)
5873 && ( (uLeaf == 0x4 && ((Host.uEax & 0x000f) == 0 || (Host.uEax & 0x000f) >= 8))
5874 || (uLeaf == 0x7 && Host.uEax == 0)
5875 || (uLeaf == 0xb && ((Host.uEcx & 0xff00) == 0 || (Host.uEcx & 0xff00) >= 8))
5876 || (uLeaf == 0xb && (Host.uEcx & 0xff) != uSubLeaf)
5877 || (uLeaf == 0xd && uSubLeaf >= 128)
5878 )
5879 )
5880 break;
5881 }
5882 }
5883 pNextLeaf = pCurLeaf;
5884
5885 /*
5886 * If verbose, decode it.
5887 */
5888 if (iVerbosity && paLeaves[0].uLeaf == 0)
5889 pHlp->pfnPrintf(pHlp,
5890 "%36s %.04s%.04s%.04s\n"
5891 "%36s 0x00000000-%#010x\n"
5892 ,
5893 "Name:", &paLeaves[0].uEbx, &paLeaves[0].uEdx, &paLeaves[0].uEcx,
5894 "Supports:", paLeaves[0].uEax);
5895
5896 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x00000001), 0)) != NULL)
5897 cpumR3CpuIdInfoStdLeaf1Details(pHlp, paLeaves, cLeaves, pCurLeaf, iVerbosity > 1, fIntel);
5898
5899 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x00000007), 0)) != NULL)
5900 cpumR3CpuIdInfoStdLeaf7Details(pHlp, paLeaves, cLeaves, pCurLeaf, iVerbosity > 1);
5901
5902 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x0000000d), 0)) != NULL)
5903 cpumR3CpuIdInfoStdLeaf13Details(pHlp, paLeaves, cLeaves, pCurLeaf, iVerbosity > 1);
5904
5905 pCurLeaf = pNextLeaf;
5906
5907 /*
5908 * Hypervisor leaves.
5909 *
5910 * Unlike most of the other leaves reported, the guest hypervisor leaves
5911 * aren't a subset of the host CPUID bits.
5912 */
5913 pCurLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, UINT32_C(0x3fffffff), "Unknown CPUID Leaves");
5914
5915 ASMCpuIdExSlow(UINT32_C(0x40000000), 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5916 cHstMax = Host.uEax >= UINT32_C(0x40000001) && Host.uEax <= UINT32_C(0x40000fff) ? Host.uEax : 0;
5917 cGstMax = (uintptr_t)(pCurLeaf - paLeaves) < cLeaves && pCurLeaf->uLeaf == UINT32_C(0x40000000)
5918 ? RT_MIN(pCurLeaf->uEax, UINT32_C(0x40000fff)) : 0;
5919 cMax = RT_MAX(cHstMax, cGstMax);
5920 if (cMax >= UINT32_C(0x40000000))
5921 {
5922 pNextLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, cMax, "Raw Hypervisor CPUID Leaves");
5923
5924 /** @todo dump these in more detail. */
5925
5926 pCurLeaf = pNextLeaf;
5927 }
5928
5929
5930 /*
5931 * Extended. Custom raw dump here due to ECX sub-leaves host handling.
5932 * Implemented after AMD specs.
5933 */
5934 pCurLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, UINT32_C(0x7fffffff), "Unknown CPUID Leaves");
5935
5936 ASMCpuIdExSlow(UINT32_C(0x80000000), 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5937 cHstMax = ASMIsValidExtRange(Host.uEax) ? RT_MIN(Host.uEax, UINT32_C(0x80000fff)) : 0;
5938 cGstMax = (uintptr_t)(pCurLeaf - paLeaves) < cLeaves && pCurLeaf->uLeaf == UINT32_C(0x80000000)
5939 ? RT_MIN(pCurLeaf->uEax, UINT32_C(0x80000fff)) : 0;
5940 cMax = RT_MAX(cHstMax, cGstMax);
5941 if (cMax >= UINT32_C(0x80000000))
5942 {
5943
5944 pHlp->pfnPrintf(pHlp,
5945 " Raw Extended CPUID Leaves\n"
5946 " Leaf/sub-leaf eax ebx ecx edx\n");
5947 PCCPUMCPUIDLEAF pExtLeaf = pCurLeaf;
5948 for (uLeaf = UINT32_C(0x80000000); uLeaf <= cMax; uLeaf++)
5949 {
5950 uint32_t cMaxSubLeaves = 1;
5951 if (uLeaf == UINT32_C(0x8000001d))
5952 cMaxSubLeaves = 16;
5953
5954 for (uint32_t uSubLeaf = 0; uSubLeaf < cMaxSubLeaves; uSubLeaf++)
5955 {
5956 ASMCpuIdExSlow(uLeaf, 0, uSubLeaf, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
5957 if ( (uintptr_t)(pCurLeaf - paLeaves) < cLeaves
5958 && pCurLeaf->uLeaf == uLeaf
5959 && pCurLeaf->uSubLeaf == uSubLeaf)
5960 {
5961 pHlp->pfnPrintf(pHlp,
5962 "Gst: %08x/%04x %08x %08x %08x %08x\n"
5963 "Hst: %08x %08x %08x %08x\n",
5964 uLeaf, uSubLeaf, pCurLeaf->uEax, pCurLeaf->uEbx, pCurLeaf->uEcx, pCurLeaf->uEdx,
5965 Host.uEax, Host.uEbx, Host.uEcx, Host.uEdx);
5966 pCurLeaf++;
5967 }
5968 else if ( uLeaf != 0xd
5969 || uSubLeaf <= 1
5970 || Host.uEbx != 0 )
5971 pHlp->pfnPrintf(pHlp,
5972 "Hst: %08x/%04x %08x %08x %08x %08x\n",
5973 uLeaf, uSubLeaf, Host.uEax, Host.uEbx, Host.uEcx, Host.uEdx);
5974
5975 /* Done? */
5976 if ( ( (uintptr_t)(pCurLeaf - paLeaves) >= cLeaves
5977 || pCurLeaf->uLeaf != uLeaf)
5978 && (uLeaf == UINT32_C(0x8000001d) && ((Host.uEax & 0x000f) == 0 || (Host.uEax & 0x000f) >= 8)) )
5979 break;
5980 }
5981 }
5982 pNextLeaf = pCurLeaf;
5983
5984 /*
5985 * Understandable output
5986 */
5987 if (iVerbosity)
5988 pHlp->pfnPrintf(pHlp,
5989 "Ext Name: %.4s%.4s%.4s\n"
5990 "Ext Supports: 0x80000000-%#010x\n",
5991 &pExtLeaf->uEbx, &pExtLeaf->uEdx, &pExtLeaf->uEcx, pExtLeaf->uEax);
5992
5993 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000001), 0);
5994 if (iVerbosity && pCurLeaf)
5995 {
5996 uint32_t uEAX = pCurLeaf->uEax;
5997 pHlp->pfnPrintf(pHlp,
5998 "Family: %d \tExtended: %d \tEffective: %d\n"
5999 "Model: %d \tExtended: %d \tEffective: %d\n"
6000 "Stepping: %d\n"
6001 "Brand ID: %#05x\n",
6002 (uEAX >> 8) & 0xf, (uEAX >> 20) & 0x7f, ASMGetCpuFamily(uEAX),
6003 (uEAX >> 4) & 0xf, (uEAX >> 16) & 0x0f, ASMGetCpuModel(uEAX, fIntel),
6004 ASMGetCpuStepping(uEAX),
6005 pCurLeaf->uEbx & 0xfff);
6006
6007 if (iVerbosity == 1)
6008 {
6009 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEdx, g_aExtLeaf1EdxSubFields, "Ext Features EDX:", 34);
6010 cpumR3CpuIdInfoMnemonicListU32(pHlp, pCurLeaf->uEcx, g_aExtLeaf1EdxSubFields, "Ext Features ECX:", 34);
6011 }
6012 else
6013 {
6014 ASMCpuIdExSlow(0x80000001, 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
6015 pHlp->pfnPrintf(pHlp, "Ext Features\n");
6016 pHlp->pfnPrintf(pHlp, " Mnemonic - Description = guest (host)\n");
6017 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEdx, Host.uEdx, g_aExtLeaf1EdxSubFields, 56);
6018 cpumR3CpuIdInfoVerboseCompareListU32(pHlp, pCurLeaf->uEcx, Host.uEcx, g_aExtLeaf1EcxSubFields, 56);
6019 }
6020 }
6021
6022 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000002), 0)) != NULL)
6023 {
6024 char szString[4*4*3+1] = {0};
6025 uint32_t *pu32 = (uint32_t *)szString;
6026 *pu32++ = pCurLeaf->uEax;
6027 *pu32++ = pCurLeaf->uEbx;
6028 *pu32++ = pCurLeaf->uEcx;
6029 *pu32++ = pCurLeaf->uEdx;
6030 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000003), 0);
6031 if (pCurLeaf)
6032 {
6033 *pu32++ = pCurLeaf->uEax;
6034 *pu32++ = pCurLeaf->uEbx;
6035 *pu32++ = pCurLeaf->uEcx;
6036 *pu32++ = pCurLeaf->uEdx;
6037 }
6038 pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000004), 0);
6039 if (pCurLeaf)
6040 {
6041 *pu32++ = pCurLeaf->uEax;
6042 *pu32++ = pCurLeaf->uEbx;
6043 *pu32++ = pCurLeaf->uEcx;
6044 *pu32++ = pCurLeaf->uEdx;
6045 }
6046 pHlp->pfnPrintf(pHlp, "Full Name: \"%s\"\n", szString);
6047 }
6048
6049 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000005), 0)) != NULL)
6050 {
6051 uint32_t uEAX = pCurLeaf->uEax;
6052 uint32_t uEBX = pCurLeaf->uEbx;
6053 uint32_t uECX = pCurLeaf->uEcx;
6054 uint32_t uEDX = pCurLeaf->uEdx;
6055 char sz1[32];
6056 char sz2[32];
6057
6058 pHlp->pfnPrintf(pHlp,
6059 "TLB 2/4M Instr/Uni: %s %3d entries\n"
6060 "TLB 2/4M Data: %s %3d entries\n",
6061 getCacheAss((uEAX >> 8) & 0xff, sz1), (uEAX >> 0) & 0xff,
6062 getCacheAss((uEAX >> 24) & 0xff, sz2), (uEAX >> 16) & 0xff);
6063 pHlp->pfnPrintf(pHlp,
6064 "TLB 4K Instr/Uni: %s %3d entries\n"
6065 "TLB 4K Data: %s %3d entries\n",
6066 getCacheAss((uEBX >> 8) & 0xff, sz1), (uEBX >> 0) & 0xff,
6067 getCacheAss((uEBX >> 24) & 0xff, sz2), (uEBX >> 16) & 0xff);
6068 pHlp->pfnPrintf(pHlp, "L1 Instr Cache Line Size: %d bytes\n"
6069 "L1 Instr Cache Lines Per Tag: %d\n"
6070 "L1 Instr Cache Associativity: %s\n"
6071 "L1 Instr Cache Size: %d KB\n",
6072 (uEDX >> 0) & 0xff,
6073 (uEDX >> 8) & 0xff,
6074 getCacheAss((uEDX >> 16) & 0xff, sz1),
6075 (uEDX >> 24) & 0xff);
6076 pHlp->pfnPrintf(pHlp,
6077 "L1 Data Cache Line Size: %d bytes\n"
6078 "L1 Data Cache Lines Per Tag: %d\n"
6079 "L1 Data Cache Associativity: %s\n"
6080 "L1 Data Cache Size: %d KB\n",
6081 (uECX >> 0) & 0xff,
6082 (uECX >> 8) & 0xff,
6083 getCacheAss((uECX >> 16) & 0xff, sz1),
6084 (uECX >> 24) & 0xff);
6085 }
6086
6087 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000006), 0)) != NULL)
6088 {
6089 uint32_t uEAX = pCurLeaf->uEax;
6090 uint32_t uEBX = pCurLeaf->uEbx;
6091 uint32_t uEDX = pCurLeaf->uEdx;
6092
6093 pHlp->pfnPrintf(pHlp,
6094 "L2 TLB 2/4M Instr/Uni: %s %4d entries\n"
6095 "L2 TLB 2/4M Data: %s %4d entries\n",
6096 getL2CacheAss((uEAX >> 12) & 0xf), (uEAX >> 0) & 0xfff,
6097 getL2CacheAss((uEAX >> 28) & 0xf), (uEAX >> 16) & 0xfff);
6098 pHlp->pfnPrintf(pHlp,
6099 "L2 TLB 4K Instr/Uni: %s %4d entries\n"
6100 "L2 TLB 4K Data: %s %4d entries\n",
6101 getL2CacheAss((uEBX >> 12) & 0xf), (uEBX >> 0) & 0xfff,
6102 getL2CacheAss((uEBX >> 28) & 0xf), (uEBX >> 16) & 0xfff);
6103 pHlp->pfnPrintf(pHlp,
6104 "L2 Cache Line Size: %d bytes\n"
6105 "L2 Cache Lines Per Tag: %d\n"
6106 "L2 Cache Associativity: %s\n"
6107 "L2 Cache Size: %d KB\n",
6108 (uEDX >> 0) & 0xff,
6109 (uEDX >> 8) & 0xf,
6110 getL2CacheAss((uEDX >> 12) & 0xf),
6111 (uEDX >> 16) & 0xffff);
6112 }
6113
6114 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000007), 0)) != NULL)
6115 {
6116 uint32_t uEDX = pCurLeaf->uEdx;
6117
6118 pHlp->pfnPrintf(pHlp, "APM Features: ");
6119 if (uEDX & RT_BIT(0)) pHlp->pfnPrintf(pHlp, " TS");
6120 if (uEDX & RT_BIT(1)) pHlp->pfnPrintf(pHlp, " FID");
6121 if (uEDX & RT_BIT(2)) pHlp->pfnPrintf(pHlp, " VID");
6122 if (uEDX & RT_BIT(3)) pHlp->pfnPrintf(pHlp, " TTP");
6123 if (uEDX & RT_BIT(4)) pHlp->pfnPrintf(pHlp, " TM");
6124 if (uEDX & RT_BIT(5)) pHlp->pfnPrintf(pHlp, " STC");
6125 if (uEDX & RT_BIT(6)) pHlp->pfnPrintf(pHlp, " MC");
6126 if (uEDX & RT_BIT(7)) pHlp->pfnPrintf(pHlp, " HWPSTATE");
6127 if (uEDX & RT_BIT(8)) pHlp->pfnPrintf(pHlp, " TscInvariant");
6128 if (uEDX & RT_BIT(9)) pHlp->pfnPrintf(pHlp, " CPB");
6129 if (uEDX & RT_BIT(10)) pHlp->pfnPrintf(pHlp, " EffFreqRO");
6130 if (uEDX & RT_BIT(11)) pHlp->pfnPrintf(pHlp, " PFI");
6131 if (uEDX & RT_BIT(12)) pHlp->pfnPrintf(pHlp, " PA");
6132 for (unsigned iBit = 13; iBit < 32; iBit++)
6133 if (uEDX & RT_BIT(iBit))
6134 pHlp->pfnPrintf(pHlp, " %d", iBit);
6135 pHlp->pfnPrintf(pHlp, "\n");
6136
6137 ASMCpuIdExSlow(UINT32_C(0x80000007), 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
6138 pHlp->pfnPrintf(pHlp, "Host Invariant-TSC support: %RTbool\n",
6139 cHstMax >= UINT32_C(0x80000007) && (Host.uEdx & RT_BIT(8)));
6140
6141 }
6142
6143 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0x80000008), 0)) != NULL)
6144 {
6145 uint32_t uEAX = pCurLeaf->uEax;
6146 uint32_t uECX = pCurLeaf->uEcx;
6147
6148 pHlp->pfnPrintf(pHlp,
6149 "Physical Address Width: %d bits\n"
6150 "Virtual Address Width: %d bits\n"
6151 "Guest Physical Address Width: %d bits\n",
6152 (uEAX >> 0) & 0xff,
6153 (uEAX >> 8) & 0xff,
6154 (uEAX >> 16) & 0xff);
6155 pHlp->pfnPrintf(pHlp,
6156 "Physical Core Count: %d\n",
6157 (uECX >> 0) & 0xff);
6158 }
6159
6160 pCurLeaf = pNextLeaf;
6161 }
6162
6163
6164
6165 /*
6166 * Centaur.
6167 */
6168 pCurLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, UINT32_C(0xbfffffff), "Unknown CPUID Leaves");
6169
6170 ASMCpuIdExSlow(UINT32_C(0xc0000000), 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
6171 cHstMax = Host.uEax >= UINT32_C(0xc0000001) && Host.uEax <= UINT32_C(0xc0000fff)
6172 ? RT_MIN(Host.uEax, UINT32_C(0xc0000fff)) : 0;
6173 cGstMax = (uintptr_t)(pCurLeaf - paLeaves) < cLeaves && pCurLeaf->uLeaf == UINT32_C(0xc0000000)
6174 ? RT_MIN(pCurLeaf->uEax, UINT32_C(0xc0000fff)) : 0;
6175 cMax = RT_MAX(cHstMax, cGstMax);
6176 if (cMax >= UINT32_C(0xc0000000))
6177 {
6178 pNextLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, cMax, "Raw Centaur CPUID Leaves");
6179
6180 /*
6181 * Understandable output
6182 */
6183 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0xc0000000), 0)) != NULL)
6184 pHlp->pfnPrintf(pHlp,
6185 "Centaur Supports: 0xc0000000-%#010x\n",
6186 pCurLeaf->uEax);
6187
6188 if (iVerbosity && (pCurLeaf = cpumR3CpuIdGetLeaf(paLeaves, cLeaves, UINT32_C(0xc0000001), 0)) != NULL)
6189 {
6190 ASMCpuIdExSlow(0xc0000001, 0, 0, 0, &Host.uEax, &Host.uEbx, &Host.uEcx, &Host.uEdx);
6191 uint32_t uEdxGst = pCurLeaf->uEdx;
6192 uint32_t uEdxHst = Host.uEdx;
6193
6194 if (iVerbosity == 1)
6195 {
6196 pHlp->pfnPrintf(pHlp, "Centaur Features EDX: ");
6197 if (uEdxGst & RT_BIT(0)) pHlp->pfnPrintf(pHlp, " AIS");
6198 if (uEdxGst & RT_BIT(1)) pHlp->pfnPrintf(pHlp, " AIS-E");
6199 if (uEdxGst & RT_BIT(2)) pHlp->pfnPrintf(pHlp, " RNG");
6200 if (uEdxGst & RT_BIT(3)) pHlp->pfnPrintf(pHlp, " RNG-E");
6201 if (uEdxGst & RT_BIT(4)) pHlp->pfnPrintf(pHlp, " LH");
6202 if (uEdxGst & RT_BIT(5)) pHlp->pfnPrintf(pHlp, " FEMMS");
6203 if (uEdxGst & RT_BIT(6)) pHlp->pfnPrintf(pHlp, " ACE");
6204 if (uEdxGst & RT_BIT(7)) pHlp->pfnPrintf(pHlp, " ACE-E");
6205 /* possibly indicating MM/HE and MM/HE-E on older chips... */
6206 if (uEdxGst & RT_BIT(8)) pHlp->pfnPrintf(pHlp, " ACE2");
6207 if (uEdxGst & RT_BIT(9)) pHlp->pfnPrintf(pHlp, " ACE2-E");
6208 if (uEdxGst & RT_BIT(10)) pHlp->pfnPrintf(pHlp, " PHE");
6209 if (uEdxGst & RT_BIT(11)) pHlp->pfnPrintf(pHlp, " PHE-E");
6210 if (uEdxGst & RT_BIT(12)) pHlp->pfnPrintf(pHlp, " PMM");
6211 if (uEdxGst & RT_BIT(13)) pHlp->pfnPrintf(pHlp, " PMM-E");
6212 for (unsigned iBit = 14; iBit < 32; iBit++)
6213 if (uEdxGst & RT_BIT(iBit))
6214 pHlp->pfnPrintf(pHlp, " %d", iBit);
6215 pHlp->pfnPrintf(pHlp, "\n");
6216 }
6217 else
6218 {
6219 pHlp->pfnPrintf(pHlp, "Mnemonic - Description = guest (host)\n");
6220 pHlp->pfnPrintf(pHlp, "AIS - Alternate Instruction Set = %d (%d)\n", !!(uEdxGst & RT_BIT( 0)), !!(uEdxHst & RT_BIT( 0)));
6221 pHlp->pfnPrintf(pHlp, "AIS-E - AIS enabled = %d (%d)\n", !!(uEdxGst & RT_BIT( 1)), !!(uEdxHst & RT_BIT( 1)));
6222 pHlp->pfnPrintf(pHlp, "RNG - Random Number Generator = %d (%d)\n", !!(uEdxGst & RT_BIT( 2)), !!(uEdxHst & RT_BIT( 2)));
6223 pHlp->pfnPrintf(pHlp, "RNG-E - RNG enabled = %d (%d)\n", !!(uEdxGst & RT_BIT( 3)), !!(uEdxHst & RT_BIT( 3)));
6224 pHlp->pfnPrintf(pHlp, "LH - LongHaul MSR 0000_110Ah = %d (%d)\n", !!(uEdxGst & RT_BIT( 4)), !!(uEdxHst & RT_BIT( 4)));
6225 pHlp->pfnPrintf(pHlp, "FEMMS - FEMMS = %d (%d)\n", !!(uEdxGst & RT_BIT( 5)), !!(uEdxHst & RT_BIT( 5)));
6226 pHlp->pfnPrintf(pHlp, "ACE - Advanced Cryptography Engine = %d (%d)\n", !!(uEdxGst & RT_BIT( 6)), !!(uEdxHst & RT_BIT( 6)));
6227 pHlp->pfnPrintf(pHlp, "ACE-E - ACE enabled = %d (%d)\n", !!(uEdxGst & RT_BIT( 7)), !!(uEdxHst & RT_BIT( 7)));
6228 /* possibly indicating MM/HE and MM/HE-E on older chips... */
6229 pHlp->pfnPrintf(pHlp, "ACE2 - Advanced Cryptography Engine 2 = %d (%d)\n", !!(uEdxGst & RT_BIT( 8)), !!(uEdxHst & RT_BIT( 8)));
6230 pHlp->pfnPrintf(pHlp, "ACE2-E - ACE enabled = %d (%d)\n", !!(uEdxGst & RT_BIT( 9)), !!(uEdxHst & RT_BIT( 9)));
6231 pHlp->pfnPrintf(pHlp, "PHE - Padlock Hash Engine = %d (%d)\n", !!(uEdxGst & RT_BIT(10)), !!(uEdxHst & RT_BIT(10)));
6232 pHlp->pfnPrintf(pHlp, "PHE-E - PHE enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(11)), !!(uEdxHst & RT_BIT(11)));
6233 pHlp->pfnPrintf(pHlp, "PMM - Montgomery Multiplier = %d (%d)\n", !!(uEdxGst & RT_BIT(12)), !!(uEdxHst & RT_BIT(12)));
6234 pHlp->pfnPrintf(pHlp, "PMM-E - PMM enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(13)), !!(uEdxHst & RT_BIT(13)));
6235 pHlp->pfnPrintf(pHlp, "14 - Reserved = %d (%d)\n", !!(uEdxGst & RT_BIT(14)), !!(uEdxHst & RT_BIT(14)));
6236 pHlp->pfnPrintf(pHlp, "15 - Reserved = %d (%d)\n", !!(uEdxGst & RT_BIT(15)), !!(uEdxHst & RT_BIT(15)));
6237 pHlp->pfnPrintf(pHlp, "Parallax = %d (%d)\n", !!(uEdxGst & RT_BIT(16)), !!(uEdxHst & RT_BIT(16)));
6238 pHlp->pfnPrintf(pHlp, "Parallax enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(17)), !!(uEdxHst & RT_BIT(17)));
6239 pHlp->pfnPrintf(pHlp, "Overstress = %d (%d)\n", !!(uEdxGst & RT_BIT(18)), !!(uEdxHst & RT_BIT(18)));
6240 pHlp->pfnPrintf(pHlp, "Overstress enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(19)), !!(uEdxHst & RT_BIT(19)));
6241 pHlp->pfnPrintf(pHlp, "TM3 - Temperature Monitoring 3 = %d (%d)\n", !!(uEdxGst & RT_BIT(20)), !!(uEdxHst & RT_BIT(20)));
6242 pHlp->pfnPrintf(pHlp, "TM3-E - TM3 enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(21)), !!(uEdxHst & RT_BIT(21)));
6243 pHlp->pfnPrintf(pHlp, "RNG2 - Random Number Generator 2 = %d (%d)\n", !!(uEdxGst & RT_BIT(22)), !!(uEdxHst & RT_BIT(22)));
6244 pHlp->pfnPrintf(pHlp, "RNG2-E - RNG2 enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(23)), !!(uEdxHst & RT_BIT(23)));
6245 pHlp->pfnPrintf(pHlp, "24 - Reserved = %d (%d)\n", !!(uEdxGst & RT_BIT(24)), !!(uEdxHst & RT_BIT(24)));
6246 pHlp->pfnPrintf(pHlp, "PHE2 - Padlock Hash Engine 2 = %d (%d)\n", !!(uEdxGst & RT_BIT(25)), !!(uEdxHst & RT_BIT(25)));
6247 pHlp->pfnPrintf(pHlp, "PHE2-E - PHE2 enabled = %d (%d)\n", !!(uEdxGst & RT_BIT(26)), !!(uEdxHst & RT_BIT(26)));
6248 for (unsigned iBit = 27; iBit < 32; iBit++)
6249 if ((uEdxGst | uEdxHst) & RT_BIT(iBit))
6250 pHlp->pfnPrintf(pHlp, "Bit %d = %d (%d)\n", iBit, !!(uEdxGst & RT_BIT(iBit)), !!(uEdxHst & RT_BIT(iBit)));
6251 pHlp->pfnPrintf(pHlp, "\n");
6252 }
6253 }
6254
6255 pCurLeaf = pNextLeaf;
6256 }
6257
6258 /*
6259 * The remainder.
6260 */
6261 pCurLeaf = cpumR3CpuIdInfoRawRange(pHlp, paLeaves, cLeaves, pCurLeaf, UINT32_C(0xffffffff), "Unknown CPUID Leaves");
6262}
6263
6264
6265
6266
6267
6268/*
6269 *
6270 *
6271 * PATM interfaces.
6272 * PATM interfaces.
6273 * PATM interfaces.
6274 *
6275 *
6276 */
6277
6278
6279# if defined(VBOX_WITH_RAW_MODE) || defined(DOXYGEN_RUNNING)
6280/** @name Patchmanager CPUID legacy table APIs
6281 * @{
6282 */
6283
6284/**
6285 * Gets a pointer to the default CPUID leaf.
6286 *
6287 * @returns Raw-mode pointer to the default CPUID leaf (read-only).
6288 * @param pVM Pointer to the VM.
6289 * @remark Intended for PATM only.
6290 */
6291VMMR3_INT_DECL(RCPTRTYPE(PCCPUMCPUID)) CPUMR3GetGuestCpuIdPatmDefRCPtr(PVM pVM)
6292{
6293 return (RCPTRTYPE(PCCPUMCPUID))VM_RC_ADDR(pVM, &pVM->cpum.s.GuestInfo.DefCpuId);
6294}
6295
6296
6297/**
6298 * Gets a number of standard CPUID leaves (PATM only).
6299 *
6300 * @returns Number of leaves.
6301 * @param pVM Pointer to the VM.
6302 * @remark Intended for PATM - legacy, don't use in new code.
6303 */
6304VMMR3_INT_DECL(uint32_t) CPUMR3GetGuestCpuIdPatmStdMax(PVM pVM)
6305{
6306 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd);
6307}
6308
6309
6310/**
6311 * Gets a number of extended CPUID leaves (PATM only).
6312 *
6313 * @returns Number of leaves.
6314 * @param pVM Pointer to the VM.
6315 * @remark Intended for PATM - legacy, don't use in new code.
6316 */
6317VMMR3_INT_DECL(uint32_t) CPUMR3GetGuestCpuIdPatmExtMax(PVM pVM)
6318{
6319 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt);
6320}
6321
6322
6323/**
6324 * Gets a number of centaur CPUID leaves.
6325 *
6326 * @returns Number of leaves.
6327 * @param pVM Pointer to the VM.
6328 * @remark Intended for PATM - legacy, don't use in new code.
6329 */
6330VMMR3_INT_DECL(uint32_t) CPUMR3GetGuestCpuIdPatmCentaurMax(PVM pVM)
6331{
6332 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur);
6333}
6334
6335
6336/**
6337 * Gets a pointer to the array of standard CPUID leaves.
6338 *
6339 * CPUMR3GetGuestCpuIdStdMax() give the size of the array.
6340 *
6341 * @returns Raw-mode pointer to the standard CPUID leaves (read-only).
6342 * @param pVM Pointer to the VM.
6343 * @remark Intended for PATM - legacy, don't use in new code.
6344 */
6345VMMR3_INT_DECL(RCPTRTYPE(PCCPUMCPUID)) CPUMR3GetGuestCpuIdPatmStdRCPtr(PVM pVM)
6346{
6347 return RCPTRTYPE(PCCPUMCPUID)VM_RC_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdPatmStd[0]);
6348}
6349
6350
6351/**
6352 * Gets a pointer to the array of extended CPUID leaves.
6353 *
6354 * CPUMGetGuestCpuIdExtMax() give the size of the array.
6355 *
6356 * @returns Raw-mode pointer to the extended CPUID leaves (read-only).
6357 * @param pVM Pointer to the VM.
6358 * @remark Intended for PATM - legacy, don't use in new code.
6359 */
6360VMMR3_INT_DECL(RCPTRTYPE(PCCPUMCPUID)) CPUMR3GetGuestCpuIdPatmExtRCPtr(PVM pVM)
6361{
6362 return (RCPTRTYPE(PCCPUMCPUID))VM_RC_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdPatmExt[0]);
6363}
6364
6365
6366/**
6367 * Gets a pointer to the array of centaur CPUID leaves.
6368 *
6369 * CPUMGetGuestCpuIdCentaurMax() give the size of the array.
6370 *
6371 * @returns Raw-mode pointer to the centaur CPUID leaves (read-only).
6372 * @param pVM Pointer to the VM.
6373 * @remark Intended for PATM - legacy, don't use in new code.
6374 */
6375VMMR3_INT_DECL(RCPTRTYPE(PCCPUMCPUID)) CPUMR3GetGuestCpuIdPatmCentaurRCPtr(PVM pVM)
6376{
6377 return (RCPTRTYPE(PCCPUMCPUID))VM_RC_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdPatmCentaur[0]);
6378}
6379
6380/** @} */
6381# endif /* VBOX_WITH_RAW_MODE || DOXYGEN_RUNNING */
6382
6383#endif /* VBOX_IN_VMM */
6384
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette