VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/CPUMR3Db.cpp@ 89934

Last change on this file since 89934 was 89934, checked in by vboxsync, 3 years ago

VMM/CPUCpuId: Some intel updates. bugref:10052

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.1 KB
Line 
1/* $Id: CPUMR3Db.cpp 89934 2021-06-29 00:05:41Z vboxsync $ */
2/** @file
3 * CPUM - CPU database part.
4 */
5
6/*
7 * Copyright (C) 2013-2020 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 "CPUMInternal.h"
25#include <VBox/vmm/vm.h>
26#include <VBox/vmm/mm.h>
27
28#include <VBox/err.h>
29#if !defined(RT_ARCH_ARM64)
30# include <iprt/asm-amd64-x86.h>
31#endif
32#include <iprt/mem.h>
33#include <iprt/string.h>
34
35
36/*********************************************************************************************************************************
37* Defined Constants And Macros *
38*********************************************************************************************************************************/
39/** @def NULL_ALONE
40 * For eliminating an unnecessary data dependency in standalone builds (for
41 * VBoxSVC). */
42/** @def ZERO_ALONE
43 * For eliminating an unnecessary data size dependency in standalone builds (for
44 * VBoxSVC). */
45#ifndef CPUM_DB_STANDALONE
46# define NULL_ALONE(a_aTable) a_aTable
47# define ZERO_ALONE(a_cTable) a_cTable
48#else
49# define NULL_ALONE(a_aTable) NULL
50# define ZERO_ALONE(a_cTable) 0
51#endif
52
53
54/** @name Short macros for the MSR range entries.
55 *
56 * These are rather cryptic, but this is to reduce the attack on the right
57 * margin.
58 *
59 * @{ */
60/** Alias one MSR onto another (a_uTarget). */
61#define MAL(a_uMsr, a_szName, a_uTarget) \
62 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_MsrAlias, kCpumMsrWrFn_MsrAlias, 0, a_uTarget, 0, 0, a_szName)
63/** Functions handles everything. */
64#define MFN(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff) \
65 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, 0, a_szName)
66/** Functions handles everything, with GP mask. */
67#define MFG(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_fWrGpMask) \
68 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, a_fWrGpMask, a_szName)
69/** Function handlers, read-only. */
70#define MFO(a_uMsr, a_szName, a_enmRdFnSuff) \
71 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_ReadOnly, 0, 0, 0, UINT64_MAX, a_szName)
72/** Function handlers, ignore all writes. */
73#define MFI(a_uMsr, a_szName, a_enmRdFnSuff) \
74 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_IgnoreWrite, 0, 0, UINT64_MAX, 0, a_szName)
75/** Function handlers, with value. */
76#define MFV(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uValue) \
77 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, a_uValue, 0, 0, a_szName)
78/** Function handlers, with write ignore mask. */
79#define MFW(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_fWrIgnMask) \
80 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, a_fWrIgnMask, 0, a_szName)
81/** Function handlers, extended version. */
82#define MFX(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uValue, a_fWrIgnMask, a_fWrGpMask) \
83 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, a_uValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
84/** Function handlers, with CPUMCPU storage variable. */
85#define MFS(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_CpumCpuMember) \
86 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, \
87 RT_OFFSETOF(CPUMCPU, a_CpumCpuMember), 0, 0, 0, a_szName)
88/** Function handlers, with CPUMCPU storage variable, ignore mask and GP mask. */
89#define MFZ(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_CpumCpuMember, a_fWrIgnMask, a_fWrGpMask) \
90 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, \
91 RT_OFFSETOF(CPUMCPU, a_CpumCpuMember), 0, a_fWrIgnMask, a_fWrGpMask, a_szName)
92/** Read-only fixed value. */
93#define MVO(a_uMsr, a_szName, a_uValue) \
94 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_ReadOnly, 0, a_uValue, 0, UINT64_MAX, a_szName)
95/** Read-only fixed value, ignores all writes. */
96#define MVI(a_uMsr, a_szName, a_uValue) \
97 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, UINT64_MAX, 0, a_szName)
98/** Read fixed value, ignore writes outside GP mask. */
99#define MVG(a_uMsr, a_szName, a_uValue, a_fWrGpMask) \
100 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, 0, a_fWrGpMask, a_szName)
101/** Read fixed value, extended version with both GP and ignore masks. */
102#define MVX(a_uMsr, a_szName, a_uValue, a_fWrIgnMask, a_fWrGpMask) \
103 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
104/** The short form, no CPUM backing. */
105#define MSN(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask) \
106 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, \
107 a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
108
109/** Range: Functions handles everything. */
110#define RFN(a_uFirst, a_uLast, a_szName, a_enmRdFnSuff, a_enmWrFnSuff) \
111 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, 0, a_szName)
112/** Range: Read fixed value, read-only. */
113#define RVO(a_uFirst, a_uLast, a_szName, a_uValue) \
114 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_ReadOnly, 0, a_uValue, 0, UINT64_MAX, a_szName)
115/** Range: Read fixed value, ignore writes. */
116#define RVI(a_uFirst, a_uLast, a_szName, a_uValue) \
117 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, UINT64_MAX, 0, a_szName)
118/** Range: The short form, no CPUM backing. */
119#define RSN(a_uFirst, a_uLast, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask) \
120 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, \
121 a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
122
123/** Internal form used by the macros. */
124#ifdef VBOX_WITH_STATISTICS
125# define RINT(a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName) \
126 { a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, 0, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName, \
127 { 0 }, { 0 }, { 0 }, { 0 } }
128#else
129# define RINT(a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName) \
130 { a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, 0, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName }
131#endif
132/** @} */
133
134#ifndef CPUM_DB_STANDALONE
135
136#include "cpus/Intel_Core_i7_6700K.h"
137#include "cpus/Intel_Core_i7_5600U.h"
138#include "cpus/Intel_Core_i7_3960X.h"
139#include "cpus/Intel_Core_i5_3570.h"
140#include "cpus/Intel_Core_i7_2635QM.h"
141#include "cpus/Intel_Xeon_X5482_3_20GHz.h"
142#include "cpus/Intel_Core2_X6800_2_93GHz.h"
143#include "cpus/Intel_Core2_T7600_2_33GHz.h"
144#include "cpus/Intel_Core_Duo_T2600_2_16GHz.h"
145#include "cpus/Intel_Pentium_M_processor_2_00GHz.h"
146#include "cpus/Intel_Pentium_4_3_00GHz.h"
147#include "cpus/Intel_Pentium_N3530_2_16GHz.h"
148#include "cpus/Intel_Atom_330_1_60GHz.h"
149#include "cpus/Intel_80486.h"
150#include "cpus/Intel_80386.h"
151#include "cpus/Intel_80286.h"
152#include "cpus/Intel_80186.h"
153#include "cpus/Intel_8086.h"
154
155#include "cpus/AMD_Ryzen_7_1800X_Eight_Core.h"
156#include "cpus/AMD_FX_8150_Eight_Core.h"
157#include "cpus/AMD_Phenom_II_X6_1100T.h"
158#include "cpus/Quad_Core_AMD_Opteron_2384.h"
159#include "cpus/AMD_Athlon_64_X2_Dual_Core_4200.h"
160#include "cpus/AMD_Athlon_64_3200.h"
161
162#include "cpus/VIA_QuadCore_L4700_1_2_GHz.h"
163
164#include "cpus/ZHAOXIN_KaiXian_KX_U5581_1_8GHz.h"
165
166#include "cpus/Hygon_C86_7185_32_core.h"
167
168
169/**
170 * The database entries.
171 *
172 * 1. The first entry is special. It is the fallback for unknown
173 * processors. Thus, it better be pretty representative.
174 *
175 * 2. The first entry for a CPU vendor is likewise important as it is
176 * the default entry for that vendor.
177 *
178 * Generally we put the most recent CPUs first, since these tend to have the
179 * most complicated and backwards compatible list of MSRs.
180 */
181static CPUMDBENTRY const * const g_apCpumDbEntries[] =
182{
183#ifdef VBOX_CPUDB_Intel_Core_i7_6700K_h
184 &g_Entry_Intel_Core_i7_6700K,
185#endif
186#ifdef VBOX_CPUDB_Intel_Core_i7_5600U_h
187 &g_Entry_Intel_Core_i7_5600U,
188#endif
189#ifdef VBOX_CPUDB_Intel_Core_i5_3570_h
190 &g_Entry_Intel_Core_i5_3570,
191#endif
192#ifdef VBOX_CPUDB_Intel_Core_i7_3960X_h
193 &g_Entry_Intel_Core_i7_3960X,
194#endif
195#ifdef VBOX_CPUDB_Intel_Core_i7_2635QM_h
196 &g_Entry_Intel_Core_i7_2635QM,
197#endif
198#ifdef VBOX_CPUDB_Intel_Pentium_N3530_2_16GHz_h
199 &g_Entry_Intel_Pentium_N3530_2_16GHz,
200#endif
201#ifdef VBOX_CPUDB_Intel_Atom_330_1_60GHz_h
202 &g_Entry_Intel_Atom_330_1_60GHz,
203#endif
204#ifdef VBOX_CPUDB_Intel_Pentium_M_processor_2_00GHz_h
205 &g_Entry_Intel_Pentium_M_processor_2_00GHz,
206#endif
207#ifdef VBOX_CPUDB_Intel_Xeon_X5482_3_20GHz_h
208 &g_Entry_Intel_Xeon_X5482_3_20GHz,
209#endif
210#ifdef VBOX_CPUDB_Intel_Core2_X6800_2_93GHz_h
211 &g_Entry_Intel_Core2_X6800_2_93GHz,
212#endif
213#ifdef VBOX_CPUDB_Intel_Core2_T7600_2_33GHz_h
214 &g_Entry_Intel_Core2_T7600_2_33GHz,
215#endif
216#ifdef VBOX_CPUDB_Intel_Core_Duo_T2600_2_16GHz_h
217 &g_Entry_Intel_Core_Duo_T2600_2_16GHz,
218#endif
219#ifdef VBOX_CPUDB_Intel_Pentium_4_3_00GHz_h
220 &g_Entry_Intel_Pentium_4_3_00GHz,
221#endif
222#ifdef VBOX_CPUDB_Intel_Pentium_4_3_00GHz_h
223 &g_Entry_Intel_Pentium_4_3_00GHz,
224#endif
225/** @todo pentium, pentium mmx, pentium pro, pentium II, pentium III */
226#ifdef VBOX_CPUDB_Intel_80486_h
227 &g_Entry_Intel_80486,
228#endif
229#ifdef VBOX_CPUDB_Intel_80386_h
230 &g_Entry_Intel_80386,
231#endif
232#ifdef VBOX_CPUDB_Intel_80286_h
233 &g_Entry_Intel_80286,
234#endif
235#ifdef VBOX_CPUDB_Intel_80186_h
236 &g_Entry_Intel_80186,
237#endif
238#ifdef VBOX_CPUDB_Intel_8086_h
239 &g_Entry_Intel_8086,
240#endif
241
242#ifdef VBOX_CPUDB_AMD_Ryzen_7_1800X_Eight_Core_h
243 &g_Entry_AMD_Ryzen_7_1800X_Eight_Core,
244#endif
245#ifdef VBOX_CPUDB_AMD_FX_8150_Eight_Core_h
246 &g_Entry_AMD_FX_8150_Eight_Core,
247#endif
248#ifdef VBOX_CPUDB_AMD_Phenom_II_X6_1100T_h
249 &g_Entry_AMD_Phenom_II_X6_1100T,
250#endif
251#ifdef VBOX_CPUDB_Quad_Core_AMD_Opteron_2384_h
252 &g_Entry_Quad_Core_AMD_Opteron_2384,
253#endif
254#ifdef VBOX_CPUDB_AMD_Athlon_64_X2_Dual_Core_4200_h
255 &g_Entry_AMD_Athlon_64_X2_Dual_Core_4200,
256#endif
257#ifdef VBOX_CPUDB_AMD_Athlon_64_3200_h
258 &g_Entry_AMD_Athlon_64_3200,
259#endif
260
261#ifdef VBOX_CPUDB_ZHAOXIN_KaiXian_KX_U5581_1_8GHz_h
262 &g_Entry_ZHAOXIN_KaiXian_KX_U5581_1_8GHz,
263#endif
264
265#ifdef VBOX_CPUDB_VIA_QuadCore_L4700_1_2_GHz_h
266 &g_Entry_VIA_QuadCore_L4700_1_2_GHz,
267#endif
268
269#ifdef VBOX_CPUDB_NEC_V20_h
270 &g_Entry_NEC_V20,
271#endif
272
273#ifdef VBOX_CPUDB_Hygon_C86_7185_32_core_h
274 &g_Entry_Hygon_C86_7185_32_core,
275#endif
276};
277
278
279/**
280 * Returns the number of entries in the CPU database.
281 *
282 * @returns Number of entries.
283 * @sa PFNCPUMDBGETENTRIES
284 */
285VMMR3DECL(uint32_t) CPUMR3DbGetEntries(void)
286{
287 return RT_ELEMENTS(g_apCpumDbEntries);
288}
289
290
291/**
292 * Returns CPU database entry for the given index.
293 *
294 * @returns Pointer the CPU database entry, NULL if index is out of bounds.
295 * @param idxCpuDb The index (0..CPUMR3DbGetEntries).
296 * @sa PFNCPUMDBGETENTRYBYINDEX
297 */
298VMMR3DECL(PCCPUMDBENTRY) CPUMR3DbGetEntryByIndex(uint32_t idxCpuDb)
299{
300 AssertReturn(idxCpuDb <= RT_ELEMENTS(g_apCpumDbEntries), NULL);
301 return g_apCpumDbEntries[idxCpuDb];
302}
303
304
305/**
306 * Returns CPU database entry with the given name.
307 *
308 * @returns Pointer the CPU database entry, NULL if not found.
309 * @param pszName The name of the profile to return.
310 * @sa PFNCPUMDBGETENTRYBYNAME
311 */
312VMMR3DECL(PCCPUMDBENTRY) CPUMR3DbGetEntryByName(const char *pszName)
313{
314 AssertPtrReturn(pszName, NULL);
315 AssertReturn(*pszName, NULL);
316 for (size_t i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
317 if (strcmp(g_apCpumDbEntries[i]->pszName, pszName) == 0)
318 return g_apCpumDbEntries[i];
319 return NULL;
320}
321
322
323
324/**
325 * Binary search used by cpumR3MsrRangesInsert and has some special properties
326 * wrt to mismatches.
327 *
328 * @returns Insert location.
329 * @param paMsrRanges The MSR ranges to search.
330 * @param cMsrRanges The number of MSR ranges.
331 * @param uMsr What to search for.
332 */
333static uint32_t cpumR3MsrRangesBinSearch(PCCPUMMSRRANGE paMsrRanges, uint32_t cMsrRanges, uint32_t uMsr)
334{
335 if (!cMsrRanges)
336 return 0;
337
338 uint32_t iStart = 0;
339 uint32_t iLast = cMsrRanges - 1;
340 for (;;)
341 {
342 uint32_t i = iStart + (iLast - iStart + 1) / 2;
343 if ( uMsr >= paMsrRanges[i].uFirst
344 && uMsr <= paMsrRanges[i].uLast)
345 return i;
346 if (uMsr < paMsrRanges[i].uFirst)
347 {
348 if (i <= iStart)
349 return i;
350 iLast = i - 1;
351 }
352 else
353 {
354 if (i >= iLast)
355 {
356 if (i < cMsrRanges)
357 i++;
358 return i;
359 }
360 iStart = i + 1;
361 }
362 }
363}
364
365
366/**
367 * Ensures that there is space for at least @a cNewRanges in the table,
368 * reallocating the table if necessary.
369 *
370 * @returns Pointer to the MSR ranges on success, NULL on failure. On failure
371 * @a *ppaMsrRanges is freed and set to NULL.
372 * @param pVM The cross context VM structure. If NULL,
373 * use the process heap, otherwise the VM's hyper heap.
374 * @param ppaMsrRanges The variable pointing to the ranges (input/output).
375 * @param cMsrRanges The current number of ranges.
376 * @param cNewRanges The number of ranges to be added.
377 */
378static PCPUMMSRRANGE cpumR3MsrRangesEnsureSpace(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t cMsrRanges, uint32_t cNewRanges)
379{
380 uint32_t cMsrRangesAllocated;
381 if (!pVM)
382 cMsrRangesAllocated = RT_ALIGN_32(cMsrRanges, 16);
383 else
384 {
385 /*
386 * We're using the hyper heap now, but when the range array was copied over to it from
387 * the host-context heap, we only copy the exact size and not the ensured size.
388 * See @bugref{7270}.
389 */
390 cMsrRangesAllocated = cMsrRanges;
391 }
392 if (cMsrRangesAllocated < cMsrRanges + cNewRanges)
393 {
394 void *pvNew;
395 uint32_t cNew = RT_ALIGN_32(cMsrRanges + cNewRanges, 16);
396 if (pVM)
397 {
398 Assert(ppaMsrRanges == &pVM->cpum.s.GuestInfo.paMsrRangesR3);
399 Assert(cMsrRanges == pVM->cpum.s.GuestInfo.cMsrRanges);
400
401 size_t cb = cMsrRangesAllocated * sizeof(**ppaMsrRanges);
402 size_t cbNew = cNew * sizeof(**ppaMsrRanges);
403 int rc = MMR3HyperRealloc(pVM, *ppaMsrRanges, cb, 32, MM_TAG_CPUM_MSRS, cbNew, &pvNew);
404 if (RT_FAILURE(rc))
405 {
406 *ppaMsrRanges = NULL;
407 pVM->cpum.s.GuestInfo.paMsrRangesR0 = NIL_RTR0PTR;
408 LogRel(("CPUM: cpumR3MsrRangesEnsureSpace: MMR3HyperRealloc failed. rc=%Rrc\n", rc));
409 return NULL;
410 }
411 *ppaMsrRanges = (PCPUMMSRRANGE)pvNew;
412 }
413 else
414 {
415 pvNew = RTMemRealloc(*ppaMsrRanges, cNew * sizeof(**ppaMsrRanges));
416 if (!pvNew)
417 {
418 RTMemFree(*ppaMsrRanges);
419 *ppaMsrRanges = NULL;
420 return NULL;
421 }
422 }
423 *ppaMsrRanges = (PCPUMMSRRANGE)pvNew;
424 }
425
426 if (pVM)
427 {
428 /* Update the R0 pointer. */
429 Assert(ppaMsrRanges == &pVM->cpum.s.GuestInfo.paMsrRangesR3);
430 pVM->cpum.s.GuestInfo.paMsrRangesR0 = MMHyperR3ToR0(pVM, *ppaMsrRanges);
431 }
432
433 return *ppaMsrRanges;
434}
435
436
437/**
438 * Inserts a new MSR range in into an sorted MSR range array.
439 *
440 * If the new MSR range overlaps existing ranges, the existing ones will be
441 * adjusted/removed to fit in the new one.
442 *
443 * @returns VBox status code.
444 * @retval VINF_SUCCESS
445 * @retval VERR_NO_MEMORY
446 *
447 * @param pVM The cross context VM structure. If NULL,
448 * use the process heap, otherwise the VM's hyper heap.
449 * @param ppaMsrRanges The variable pointing to the ranges (input/output).
450 * Must be NULL if using the hyper heap.
451 * @param pcMsrRanges The variable holding number of ranges. Must be NULL
452 * if using the hyper heap.
453 * @param pNewRange The new range.
454 */
455int cpumR3MsrRangesInsert(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t *pcMsrRanges, PCCPUMMSRRANGE pNewRange)
456{
457 Assert(pNewRange->uLast >= pNewRange->uFirst);
458 Assert(pNewRange->enmRdFn > kCpumMsrRdFn_Invalid && pNewRange->enmRdFn < kCpumMsrRdFn_End);
459 Assert(pNewRange->enmWrFn > kCpumMsrWrFn_Invalid && pNewRange->enmWrFn < kCpumMsrWrFn_End);
460
461 /*
462 * Validate and use the VM's MSR ranges array if we are using the hyper heap.
463 */
464 if (pVM)
465 {
466 AssertReturn(!ppaMsrRanges, VERR_INVALID_PARAMETER);
467 AssertReturn(!pcMsrRanges, VERR_INVALID_PARAMETER);
468
469 ppaMsrRanges = &pVM->cpum.s.GuestInfo.paMsrRangesR3;
470 pcMsrRanges = &pVM->cpum.s.GuestInfo.cMsrRanges;
471 }
472 else
473 {
474 AssertReturn(ppaMsrRanges, VERR_INVALID_POINTER);
475 AssertReturn(pcMsrRanges, VERR_INVALID_POINTER);
476 }
477
478 uint32_t cMsrRanges = *pcMsrRanges;
479 PCPUMMSRRANGE paMsrRanges = *ppaMsrRanges;
480
481 /*
482 * Optimize the linear insertion case where we add new entries at the end.
483 */
484 if ( cMsrRanges > 0
485 && paMsrRanges[cMsrRanges - 1].uLast < pNewRange->uFirst)
486 {
487 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
488 if (!paMsrRanges)
489 return VERR_NO_MEMORY;
490 paMsrRanges[cMsrRanges] = *pNewRange;
491 *pcMsrRanges += 1;
492 }
493 else
494 {
495 uint32_t i = cpumR3MsrRangesBinSearch(paMsrRanges, cMsrRanges, pNewRange->uFirst);
496 Assert(i == cMsrRanges || pNewRange->uFirst <= paMsrRanges[i].uLast);
497 Assert(i == 0 || pNewRange->uFirst > paMsrRanges[i - 1].uLast);
498
499 /*
500 * Adding an entirely new entry?
501 */
502 if ( i >= cMsrRanges
503 || pNewRange->uLast < paMsrRanges[i].uFirst)
504 {
505 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
506 if (!paMsrRanges)
507 return VERR_NO_MEMORY;
508 if (i < cMsrRanges)
509 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
510 paMsrRanges[i] = *pNewRange;
511 *pcMsrRanges += 1;
512 }
513 /*
514 * Replace existing entry?
515 */
516 else if ( pNewRange->uFirst == paMsrRanges[i].uFirst
517 && pNewRange->uLast == paMsrRanges[i].uLast)
518 paMsrRanges[i] = *pNewRange;
519 /*
520 * Splitting an existing entry?
521 */
522 else if ( pNewRange->uFirst > paMsrRanges[i].uFirst
523 && pNewRange->uLast < paMsrRanges[i].uLast)
524 {
525 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 2);
526 if (!paMsrRanges)
527 return VERR_NO_MEMORY;
528 if (i < cMsrRanges)
529 memmove(&paMsrRanges[i + 2], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
530 paMsrRanges[i + 1] = *pNewRange;
531 paMsrRanges[i + 2] = paMsrRanges[i];
532 paMsrRanges[i ].uLast = pNewRange->uFirst - 1;
533 paMsrRanges[i + 2].uFirst = pNewRange->uLast + 1;
534 *pcMsrRanges += 2;
535 }
536 /*
537 * Complicated scenarios that can affect more than one range.
538 *
539 * The current code does not optimize memmove calls when replacing
540 * one or more existing ranges, because it's tedious to deal with and
541 * not expected to be a frequent usage scenario.
542 */
543 else
544 {
545 /* Adjust start of first match? */
546 if ( pNewRange->uFirst <= paMsrRanges[i].uFirst
547 && pNewRange->uLast < paMsrRanges[i].uLast)
548 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
549 else
550 {
551 /* Adjust end of first match? */
552 if (pNewRange->uFirst > paMsrRanges[i].uFirst)
553 {
554 Assert(paMsrRanges[i].uLast >= pNewRange->uFirst);
555 paMsrRanges[i].uLast = pNewRange->uFirst - 1;
556 i++;
557 }
558 /* Replace the whole first match (lazy bird). */
559 else
560 {
561 if (i + 1 < cMsrRanges)
562 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
563 cMsrRanges = *pcMsrRanges -= 1;
564 }
565
566 /* Do the new range affect more ranges? */
567 while ( i < cMsrRanges
568 && pNewRange->uLast >= paMsrRanges[i].uFirst)
569 {
570 if (pNewRange->uLast < paMsrRanges[i].uLast)
571 {
572 /* Adjust the start of it, then we're done. */
573 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
574 break;
575 }
576
577 /* Remove it entirely. */
578 if (i + 1 < cMsrRanges)
579 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
580 cMsrRanges = *pcMsrRanges -= 1;
581 }
582 }
583
584 /* Now, perform a normal insertion. */
585 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
586 if (!paMsrRanges)
587 return VERR_NO_MEMORY;
588 if (i < cMsrRanges)
589 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
590 paMsrRanges[i] = *pNewRange;
591 *pcMsrRanges += 1;
592 }
593 }
594
595 return VINF_SUCCESS;
596}
597
598
599/**
600 * Reconciles CPUID info with MSRs (selected ones).
601 *
602 * @returns VBox status code.
603 * @param pVM The cross context VM structure.
604 */
605int cpumR3MsrReconcileWithCpuId(PVM pVM)
606{
607 PCCPUMMSRRANGE papToAdd[10];
608 uint32_t cToAdd = 0;
609
610 /*
611 * The IA32_FLUSH_CMD MSR was introduced in MCUs for CVS-2018-3646 and associates.
612 */
613 if (pVM->cpum.s.GuestFeatures.fFlushCmd && !cpumLookupMsrRange(pVM, MSR_IA32_FLUSH_CMD))
614 {
615 static CPUMMSRRANGE const s_FlushCmd =
616 {
617 /*.uFirst =*/ MSR_IA32_FLUSH_CMD,
618 /*.uLast =*/ MSR_IA32_FLUSH_CMD,
619 /*.enmRdFn =*/ kCpumMsrRdFn_WriteOnly,
620 /*.enmWrFn =*/ kCpumMsrWrFn_Ia32FlushCmd,
621 /*.offCpumCpu =*/ UINT16_MAX,
622 /*.fReserved =*/ 0,
623 /*.uValue =*/ 0,
624 /*.fWrIgnMask =*/ 0,
625 /*.fWrGpMask =*/ ~MSR_IA32_FLUSH_CMD_F_L1D,
626 /*.szName = */ "IA32_FLUSH_CMD"
627 };
628 papToAdd[cToAdd++] = &s_FlushCmd;
629 }
630
631 /*
632 * The MSR_IA32_ARCH_CAPABILITIES was introduced in various spectre MCUs, or at least
633 * documented in relation to such.
634 */
635 if (pVM->cpum.s.GuestFeatures.fArchCap && !cpumLookupMsrRange(pVM, MSR_IA32_ARCH_CAPABILITIES))
636 {
637 static CPUMMSRRANGE const s_ArchCaps =
638 {
639 /*.uFirst =*/ MSR_IA32_ARCH_CAPABILITIES,
640 /*.uLast =*/ MSR_IA32_ARCH_CAPABILITIES,
641 /*.enmRdFn =*/ kCpumMsrRdFn_Ia32ArchCapabilities,
642 /*.enmWrFn =*/ kCpumMsrWrFn_ReadOnly,
643 /*.offCpumCpu =*/ UINT16_MAX,
644 /*.fReserved =*/ 0,
645 /*.uValue =*/ 0,
646 /*.fWrIgnMask =*/ 0,
647 /*.fWrGpMask =*/ UINT64_MAX,
648 /*.szName = */ "IA32_ARCH_CAPABILITIES"
649 };
650 papToAdd[cToAdd++] = &s_ArchCaps;
651 }
652
653 /*
654 * Do the adding.
655 */
656 for (uint32_t i = 0; i < cToAdd; i++)
657 {
658 PCCPUMMSRRANGE pRange = papToAdd[i];
659 LogRel(("CPUM: MSR/CPUID reconciliation insert: %#010x %s\n", pRange->uFirst, pRange->szName));
660 int rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
661 pRange);
662 if (RT_FAILURE(rc))
663 return rc;
664 }
665 return VINF_SUCCESS;
666}
667
668
669/**
670 * Worker for cpumR3MsrApplyFudge that applies one table.
671 *
672 * @returns VBox status code.
673 * @param pVM The cross context VM structure.
674 * @param paRanges Array of MSRs to fudge.
675 * @param cRanges Number of MSRs in the array.
676 */
677static int cpumR3MsrApplyFudgeTable(PVM pVM, PCCPUMMSRRANGE paRanges, size_t cRanges)
678{
679 for (uint32_t i = 0; i < cRanges; i++)
680 if (!cpumLookupMsrRange(pVM, paRanges[i].uFirst))
681 {
682 LogRel(("CPUM: MSR fudge: %#010x %s\n", paRanges[i].uFirst, paRanges[i].szName));
683 int rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
684 &paRanges[i]);
685 if (RT_FAILURE(rc))
686 return rc;
687 }
688 return VINF_SUCCESS;
689}
690
691
692/**
693 * Fudges the MSRs that guest are known to access in some odd cases.
694 *
695 * A typical example is a VM that has been moved between different hosts where
696 * for instance the cpu vendor differs.
697 *
698 * Another example is older CPU profiles (e.g. Atom Bonnet) for newer CPUs (e.g.
699 * Atom Silvermont), where features reported thru CPUID aren't present in the
700 * MSRs (e.g. AMD64_TSC_AUX).
701 *
702 *
703 * @returns VBox status code.
704 * @param pVM The cross context VM structure.
705 */
706int cpumR3MsrApplyFudge(PVM pVM)
707{
708 /*
709 * Basic.
710 */
711 static CPUMMSRRANGE const s_aFudgeMsrs[] =
712 {
713 MFO(0x00000000, "IA32_P5_MC_ADDR", Ia32P5McAddr),
714 MFX(0x00000001, "IA32_P5_MC_TYPE", Ia32P5McType, Ia32P5McType, 0, 0, UINT64_MAX),
715 MVO(0x00000017, "IA32_PLATFORM_ID", 0),
716 MFN(0x0000001b, "IA32_APIC_BASE", Ia32ApicBase, Ia32ApicBase),
717 MVI(0x0000008b, "BIOS_SIGN", 0),
718 MFX(0x000000fe, "IA32_MTRRCAP", Ia32MtrrCap, ReadOnly, 0x508, 0, 0),
719 MFX(0x00000179, "IA32_MCG_CAP", Ia32McgCap, ReadOnly, 0x005, 0, 0),
720 MFX(0x0000017a, "IA32_MCG_STATUS", Ia32McgStatus, Ia32McgStatus, 0, ~(uint64_t)UINT32_MAX, 0),
721 MFN(0x000001a0, "IA32_MISC_ENABLE", Ia32MiscEnable, Ia32MiscEnable),
722 MFN(0x000001d9, "IA32_DEBUGCTL", Ia32DebugCtl, Ia32DebugCtl),
723 MFO(0x000001db, "P6_LAST_BRANCH_FROM_IP", P6LastBranchFromIp),
724 MFO(0x000001dc, "P6_LAST_BRANCH_TO_IP", P6LastBranchToIp),
725 MFO(0x000001dd, "P6_LAST_INT_FROM_IP", P6LastIntFromIp),
726 MFO(0x000001de, "P6_LAST_INT_TO_IP", P6LastIntToIp),
727 MFS(0x00000277, "IA32_PAT", Ia32Pat, Ia32Pat, Guest.msrPAT),
728 MFZ(0x000002ff, "IA32_MTRR_DEF_TYPE", Ia32MtrrDefType, Ia32MtrrDefType, GuestMsrs.msr.MtrrDefType, 0, ~(uint64_t)0xc07),
729 MFN(0x00000400, "IA32_MCi_CTL_STATUS_ADDR_MISC", Ia32McCtlStatusAddrMiscN, Ia32McCtlStatusAddrMiscN),
730 };
731 int rc = cpumR3MsrApplyFudgeTable(pVM, &s_aFudgeMsrs[0], RT_ELEMENTS(s_aFudgeMsrs));
732 AssertLogRelRCReturn(rc, rc);
733
734 /*
735 * XP might mistake opterons and other newer CPUs for P4s.
736 */
737 if (pVM->cpum.s.GuestFeatures.uFamily >= 0xf)
738 {
739 static CPUMMSRRANGE const s_aP4FudgeMsrs[] =
740 {
741 MFX(0x0000002c, "P4_EBC_FREQUENCY_ID", IntelP4EbcFrequencyId, IntelP4EbcFrequencyId, 0xf12010f, UINT64_MAX, 0),
742 };
743 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aP4FudgeMsrs[0], RT_ELEMENTS(s_aP4FudgeMsrs));
744 AssertLogRelRCReturn(rc, rc);
745 }
746
747 if (pVM->cpum.s.GuestFeatures.fRdTscP)
748 {
749 static CPUMMSRRANGE const s_aRdTscPFudgeMsrs[] =
750 {
751 MFX(0xc0000103, "AMD64_TSC_AUX", Amd64TscAux, Amd64TscAux, 0, 0, ~(uint64_t)UINT32_MAX),
752 };
753 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aRdTscPFudgeMsrs[0], RT_ELEMENTS(s_aRdTscPFudgeMsrs));
754 AssertLogRelRCReturn(rc, rc);
755 }
756
757 /*
758 * Windows 10 incorrectly writes to MSR_IA32_TSX_CTRL without checking
759 * CPUID.ARCH_CAP(EAX=7h,ECX=0):EDX[bit 29] or the MSR feature bits in
760 * MSR_IA32_ARCH_CAPABILITIES[bit 7], see @bugref{9630}.
761 * Ignore writes to this MSR and return 0 on reads.
762 */
763 if (pVM->cpum.s.GuestFeatures.fArchCap)
764 {
765 static CPUMMSRRANGE const s_aTsxCtrl[] =
766 {
767 MVI(MSR_IA32_TSX_CTRL, "IA32_TSX_CTRL", 0),
768 };
769 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aTsxCtrl[0], RT_ELEMENTS(s_aTsxCtrl));
770 AssertLogRelRCReturn(rc, rc);
771 }
772
773 return rc;
774}
775
776
777/**
778 * Do we consider @a enmConsider a better match for @a enmTarget than
779 * @a enmFound?
780 *
781 * Only called when @a enmConsider isn't exactly what we're looking for.
782 *
783 * @returns true/false.
784 * @param enmConsider The new microarch to consider.
785 * @param enmTarget The target microarch.
786 * @param enmFound The best microarch match we've found thus far.
787 */
788DECLINLINE(bool) cpumR3DbIsBetterMarchMatch(CPUMMICROARCH enmConsider, CPUMMICROARCH enmTarget, CPUMMICROARCH enmFound)
789{
790 Assert(enmConsider != enmTarget);
791
792 /*
793 * If we've got an march match, don't bother with enmConsider.
794 */
795 if (enmFound == enmTarget)
796 return false;
797
798 /*
799 * Found is below: Pick 'consider' if it's closer to the target or above it.
800 */
801 if (enmFound < enmTarget)
802 return enmConsider > enmFound;
803
804 /*
805 * Found is above: Pick 'consider' if it's also above (paranoia: or equal)
806 * and but closer to the target.
807 */
808 return enmConsider >= enmTarget && enmConsider < enmFound;
809}
810
811
812/**
813 * Do we consider @a enmConsider a better match for @a enmTarget than
814 * @a enmFound?
815 *
816 * Only called for intel family 06h CPUs.
817 *
818 * @returns true/false.
819 * @param enmConsider The new microarch to consider.
820 * @param enmTarget The target microarch.
821 * @param enmFound The best microarch match we've found thus far.
822 */
823static bool cpumR3DbIsBetterIntelFam06Match(CPUMMICROARCH enmConsider, CPUMMICROARCH enmTarget, CPUMMICROARCH enmFound)
824{
825 /* Check intel family 06h claims. */
826 AssertReturn(enmConsider >= kCpumMicroarch_Intel_P6_Core_Atom_First && enmConsider <= kCpumMicroarch_Intel_P6_Core_Atom_End,
827 false);
828 AssertReturn( (enmTarget >= kCpumMicroarch_Intel_P6_Core_Atom_First && enmTarget <= kCpumMicroarch_Intel_P6_Core_Atom_End)
829 || enmTarget == kCpumMicroarch_Intel_Unknown,
830 false);
831
832 /* Put matches out of the way. */
833 if (enmConsider == enmTarget)
834 return true;
835 if (enmFound == enmTarget)
836 return false;
837
838 /* If found isn't a family 06h march, whatever we're considering must be a better choice. */
839 if ( enmFound < kCpumMicroarch_Intel_P6_Core_Atom_First
840 || enmFound > kCpumMicroarch_Intel_P6_Core_Atom_End)
841 return true;
842
843 /*
844 * The family 06h stuff is split into three categories:
845 * - Common P6 heritage
846 * - Core
847 * - Atom
848 *
849 * Determin which of the three arguments are Atom marchs, because that's
850 * all we need to make the right choice.
851 */
852 bool const fConsiderAtom = enmConsider >= kCpumMicroarch_Intel_Atom_First;
853 bool const fTargetAtom = enmTarget >= kCpumMicroarch_Intel_Atom_First;
854 bool const fFoundAtom = enmFound >= kCpumMicroarch_Intel_Atom_First;
855
856 /*
857 * Want atom:
858 */
859 if (fTargetAtom)
860 {
861 /* Pick the atom if we've got one of each.*/
862 if (fConsiderAtom != fFoundAtom)
863 return fConsiderAtom;
864 /* If we haven't got any atoms under consideration, pick a P6 or the earlier core.
865 Note! Not entirely sure Dothan is the best choice, but it'll do for now. */
866 if (!fConsiderAtom)
867 {
868 if (enmConsider > enmFound)
869 return enmConsider <= kCpumMicroarch_Intel_P6_M_Dothan;
870 return enmFound > kCpumMicroarch_Intel_P6_M_Dothan;
871 }
872 /* else: same category, default comparison rules. */
873 Assert(fConsiderAtom && fFoundAtom);
874 }
875 /*
876 * Want non-atom:
877 */
878 /* Pick the non-atom if we've got one of each. */
879 else if (fConsiderAtom != fFoundAtom)
880 return fFoundAtom;
881 /* If we've only got atoms under consideration, pick the older one just to pick something. */
882 else if (fConsiderAtom)
883 return enmConsider < enmFound;
884 else
885 Assert(!fConsiderAtom && !fFoundAtom);
886
887 /*
888 * Same basic category. Do same compare as caller.
889 */
890 return cpumR3DbIsBetterMarchMatch(enmConsider, enmTarget, enmFound);
891}
892
893
894int cpumR3DbGetCpuInfo(const char *pszName, PCPUMINFO pInfo)
895{
896 CPUMDBENTRY const *pEntry = NULL;
897 int rc;
898
899 if (!strcmp(pszName, "host"))
900#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
901 {
902 /*
903 * Create a CPU database entry for the host CPU. This means getting
904 * the CPUID bits from the real CPU and grabbing the closest matching
905 * database entry for MSRs.
906 */
907 rc = CPUMR3CpuIdDetectUnknownLeafMethod(&pInfo->enmUnknownCpuIdMethod, &pInfo->DefCpuId);
908 if (RT_FAILURE(rc))
909 return rc;
910 rc = CPUMR3CpuIdCollectLeaves(&pInfo->paCpuIdLeavesR3, &pInfo->cCpuIdLeaves);
911 if (RT_FAILURE(rc))
912 return rc;
913 pInfo->fMxCsrMask = CPUMR3DeterminHostMxCsrMask();
914
915 /* Lookup database entry for MSRs. */
916 CPUMCPUVENDOR const enmVendor = CPUMR3CpuIdDetectVendorEx(pInfo->paCpuIdLeavesR3[0].uEax,
917 pInfo->paCpuIdLeavesR3[0].uEbx,
918 pInfo->paCpuIdLeavesR3[0].uEcx,
919 pInfo->paCpuIdLeavesR3[0].uEdx);
920 uint32_t const uStd1Eax = pInfo->paCpuIdLeavesR3[1].uEax;
921 uint8_t const uFamily = ASMGetCpuFamily(uStd1Eax);
922 uint8_t const uModel = ASMGetCpuModel(uStd1Eax, enmVendor == CPUMCPUVENDOR_INTEL);
923 uint8_t const uStepping = ASMGetCpuStepping(uStd1Eax);
924 CPUMMICROARCH const enmMicroarch = CPUMR3CpuIdDetermineMicroarchEx(enmVendor, uFamily, uModel, uStepping);
925
926 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
927 {
928 CPUMDBENTRY const *pCur = g_apCpumDbEntries[i];
929 if ((CPUMCPUVENDOR)pCur->enmVendor == enmVendor)
930 {
931 /* Match against Family, Microarch, model and stepping. Except
932 for family, always match the closer with preference given to
933 the later/older ones. */
934 if (pCur->uFamily == uFamily)
935 {
936 if (pCur->enmMicroarch == enmMicroarch)
937 {
938 if (pCur->uModel == uModel)
939 {
940 if (pCur->uStepping == uStepping)
941 {
942 /* Perfect match. */
943 pEntry = pCur;
944 break;
945 }
946
947 if ( !pEntry
948 || pEntry->uModel != uModel
949 || pEntry->enmMicroarch != enmMicroarch
950 || pEntry->uFamily != uFamily)
951 pEntry = pCur;
952 else if ( pCur->uStepping >= uStepping
953 ? pCur->uStepping < pEntry->uStepping || pEntry->uStepping < uStepping
954 : pCur->uStepping > pEntry->uStepping)
955 pEntry = pCur;
956 }
957 else if ( !pEntry
958 || pEntry->enmMicroarch != enmMicroarch
959 || pEntry->uFamily != uFamily)
960 pEntry = pCur;
961 else if ( pCur->uModel >= uModel
962 ? pCur->uModel < pEntry->uModel || pEntry->uModel < uModel
963 : pCur->uModel > pEntry->uModel)
964 pEntry = pCur;
965 }
966 else if ( !pEntry
967 || pEntry->uFamily != uFamily)
968 pEntry = pCur;
969 /* Special march matching rules applies to intel family 06h. */
970 else if ( enmVendor == CPUMCPUVENDOR_INTEL
971 && uFamily == 6
972 ? cpumR3DbIsBetterIntelFam06Match(pCur->enmMicroarch, enmMicroarch, pEntry->enmMicroarch)
973 : cpumR3DbIsBetterMarchMatch(pCur->enmMicroarch, enmMicroarch, pEntry->enmMicroarch))
974 pEntry = pCur;
975 }
976 /* We don't do closeness matching on family, we use the first
977 entry for the CPU vendor instead. (P4 workaround.) */
978 else if (!pEntry)
979 pEntry = pCur;
980 }
981 }
982
983 if (pEntry)
984 LogRel(("CPUM: Matched host CPU %s %#x/%#x/%#x %s with CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
985 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
986 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor), pEntry->uFamily, pEntry->uModel,
987 pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
988 else
989 {
990 pEntry = g_apCpumDbEntries[0];
991 LogRel(("CPUM: No matching processor database entry %s %#x/%#x/%#x %s, falling back on '%s'\n",
992 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
993 pEntry->pszName));
994 }
995 }
996 else
997#else
998 pszName = g_apCpumDbEntries[0]->pszName; /* Just pick the first entry for non-x86 hosts. */
999#endif
1000 {
1001 /*
1002 * We're supposed to be emulating a specific CPU that is included in
1003 * our CPU database. The CPUID tables needs to be copied onto the
1004 * heap so the caller can modify them and so they can be freed like
1005 * in the host case above.
1006 */
1007 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
1008 if (!strcmp(pszName, g_apCpumDbEntries[i]->pszName))
1009 {
1010 pEntry = g_apCpumDbEntries[i];
1011 break;
1012 }
1013 if (!pEntry)
1014 {
1015 LogRel(("CPUM: Cannot locate any CPU by the name '%s'\n", pszName));
1016 return VERR_CPUM_DB_CPU_NOT_FOUND;
1017 }
1018
1019 pInfo->cCpuIdLeaves = pEntry->cCpuIdLeaves;
1020 if (pEntry->cCpuIdLeaves)
1021 {
1022 /* Must allocate a multiple of 16 here, matching cpumR3CpuIdEnsureSpace. */
1023 size_t cbExtra = sizeof(pEntry->paCpuIdLeaves[0]) * (RT_ALIGN(pEntry->cCpuIdLeaves, 16) - pEntry->cCpuIdLeaves);
1024 pInfo->paCpuIdLeavesR3 = (PCPUMCPUIDLEAF)RTMemDupEx(pEntry->paCpuIdLeaves,
1025 sizeof(pEntry->paCpuIdLeaves[0]) * pEntry->cCpuIdLeaves,
1026 cbExtra);
1027 if (!pInfo->paCpuIdLeavesR3)
1028 return VERR_NO_MEMORY;
1029 }
1030 else
1031 pInfo->paCpuIdLeavesR3 = NULL;
1032
1033 pInfo->enmUnknownCpuIdMethod = pEntry->enmUnknownCpuId;
1034 pInfo->DefCpuId = pEntry->DefUnknownCpuId;
1035 pInfo->fMxCsrMask = pEntry->fMxCsrMask;
1036
1037 LogRel(("CPUM: Using CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
1038 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor),
1039 pEntry->uFamily, pEntry->uModel, pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
1040 }
1041
1042 pInfo->fMsrMask = pEntry->fMsrMask;
1043 pInfo->iFirstExtCpuIdLeaf = 0; /* Set by caller. */
1044 pInfo->uScalableBusFreq = pEntry->uScalableBusFreq;
1045 pInfo->paCpuIdLeavesR0 = NIL_RTR0PTR;
1046 pInfo->paMsrRangesR0 = NIL_RTR0PTR;
1047
1048 /*
1049 * Copy the MSR range.
1050 */
1051 uint32_t cMsrs = 0;
1052 PCPUMMSRRANGE paMsrs = NULL;
1053
1054 PCCPUMMSRRANGE pCurMsr = pEntry->paMsrRanges;
1055 uint32_t cLeft = pEntry->cMsrRanges;
1056 while (cLeft-- > 0)
1057 {
1058 rc = cpumR3MsrRangesInsert(NULL /* pVM */, &paMsrs, &cMsrs, pCurMsr);
1059 if (RT_FAILURE(rc))
1060 {
1061 Assert(!paMsrs); /* The above function frees this. */
1062 RTMemFree(pInfo->paCpuIdLeavesR3);
1063 pInfo->paCpuIdLeavesR3 = NULL;
1064 return rc;
1065 }
1066 pCurMsr++;
1067 }
1068
1069 pInfo->paMsrRangesR3 = paMsrs;
1070 pInfo->cMsrRanges = cMsrs;
1071 return VINF_SUCCESS;
1072}
1073
1074
1075/**
1076 * Insert an MSR range into the VM.
1077 *
1078 * If the new MSR range overlaps existing ranges, the existing ones will be
1079 * adjusted/removed to fit in the new one.
1080 *
1081 * @returns VBox status code.
1082 * @param pVM The cross context VM structure.
1083 * @param pNewRange Pointer to the MSR range being inserted.
1084 */
1085VMMR3DECL(int) CPUMR3MsrRangesInsert(PVM pVM, PCCPUMMSRRANGE pNewRange)
1086{
1087 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1088 AssertReturn(pNewRange, VERR_INVALID_PARAMETER);
1089
1090 return cpumR3MsrRangesInsert(pVM, NULL /* ppaMsrRanges */, NULL /* pcMsrRanges */, pNewRange);
1091}
1092
1093
1094/**
1095 * Register statistics for the MSRs.
1096 *
1097 * This must not be called before the MSRs have been finalized and moved to the
1098 * hyper heap.
1099 *
1100 * @returns VBox status code.
1101 * @param pVM The cross context VM structure.
1102 */
1103int cpumR3MsrRegStats(PVM pVM)
1104{
1105 /*
1106 * Global statistics.
1107 */
1108 PCPUM pCpum = &pVM->cpum.s;
1109 STAM_REL_REG(pVM, &pCpum->cMsrReads, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Reads",
1110 STAMUNIT_OCCURENCES, "All RDMSRs making it to CPUM.");
1111 STAM_REL_REG(pVM, &pCpum->cMsrReadsRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsRaisingGP",
1112 STAMUNIT_OCCURENCES, "RDMSR raising #GPs, except unknown MSRs.");
1113 STAM_REL_REG(pVM, &pCpum->cMsrReadsUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsUnknown",
1114 STAMUNIT_OCCURENCES, "RDMSR on unknown MSRs (raises #GP).");
1115 STAM_REL_REG(pVM, &pCpum->cMsrWrites, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Writes",
1116 STAMUNIT_OCCURENCES, "All WRMSRs making it to CPUM.");
1117 STAM_REL_REG(pVM, &pCpum->cMsrWritesRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesRaisingGP",
1118 STAMUNIT_OCCURENCES, "WRMSR raising #GPs, except unknown MSRs.");
1119 STAM_REL_REG(pVM, &pCpum->cMsrWritesToIgnoredBits, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesToIgnoredBits",
1120 STAMUNIT_OCCURENCES, "Writing of ignored bits.");
1121 STAM_REL_REG(pVM, &pCpum->cMsrWritesUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesUnknown",
1122 STAMUNIT_OCCURENCES, "WRMSR on unknown MSRs (raises #GP).");
1123
1124
1125# ifdef VBOX_WITH_STATISTICS
1126 /*
1127 * Per range.
1128 */
1129 PCPUMMSRRANGE paRanges = pVM->cpum.s.GuestInfo.paMsrRangesR3;
1130 uint32_t cRanges = pVM->cpum.s.GuestInfo.cMsrRanges;
1131 for (uint32_t i = 0; i < cRanges; i++)
1132 {
1133 char szName[160];
1134 ssize_t cchName;
1135
1136 if (paRanges[i].uFirst == paRanges[i].uLast)
1137 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%s",
1138 paRanges[i].uFirst, paRanges[i].szName);
1139 else
1140 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%#010x-%s",
1141 paRanges[i].uFirst, paRanges[i].uLast, paRanges[i].szName);
1142
1143 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-reads");
1144 STAMR3Register(pVM, &paRanges[i].cReads, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RDMSR");
1145
1146 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-writes");
1147 STAMR3Register(pVM, &paRanges[i].cWrites, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR");
1148
1149 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-GPs");
1150 STAMR3Register(pVM, &paRanges[i].cGps, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "#GPs");
1151
1152 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-ign-bits-writes");
1153 STAMR3Register(pVM, &paRanges[i].cIgnoredBits, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR w/ ignored bits");
1154 }
1155# endif /* VBOX_WITH_STATISTICS */
1156
1157 return VINF_SUCCESS;
1158}
1159
1160#endif /* !CPUM_DB_STANDALONE */
1161
Note: See TracBrowser for help on using the repository browser.

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