VirtualBox

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

Last change on this file since 66819 was 66581, checked in by vboxsync, 8 years ago

VMM: Nested Hw.virt: Implemented various SVM intercepts in IEM, addressed some todos.

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

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