VirtualBox

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

Last change on this file since 69674 was 69221, checked in by vboxsync, 7 years ago

VMM: scm cleanups

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

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