VirtualBox

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

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

VMM/CPUM: Moved CPUIDs and MSRs from the hyper heap and into the VM structure (might not be such a great idea for MSRs actually). bugref:10093

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.3 KB
Line 
1/* $Id: CPUMR3Db.cpp 91266 2021-09-15 22:26:50Z 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 if ( cMsrRanges + cNewRanges
381 > RT_ELEMENTS(pVM->cpum.s.GuestInfo.aMsrRanges) + (pVM ? 0 : 128 /* Catch too many MSRs in CPU reporter! */))
382 {
383 LogRel(("CPUM: Too many MSR ranges! %#x, max %#x\n",
384 cMsrRanges + cNewRanges, RT_ELEMENTS(pVM->cpum.s.GuestInfo.aMsrRanges)));
385 return NULL;
386 }
387 if (pVM)
388 {
389 Assert(cMsrRanges == pVM->cpum.s.GuestInfo.cMsrRanges);
390 Assert(*ppaMsrRanges == pVM->cpum.s.GuestInfo.aMsrRanges);
391 }
392 else
393 {
394 if (cMsrRanges + cNewRanges > RT_ALIGN_32(cMsrRanges, 16))
395 {
396
397 uint32_t const cNew = RT_ALIGN_32(cMsrRanges + cNewRanges, 16);
398 void *pvNew = RTMemRealloc(*ppaMsrRanges, cNew * sizeof(**ppaMsrRanges));
399 if (pvNew)
400 *ppaMsrRanges = (PCPUMMSRRANGE)pvNew;
401 else
402 {
403 RTMemFree(*ppaMsrRanges);
404 *ppaMsrRanges = NULL;
405 return NULL;
406 }
407 }
408 }
409
410 return *ppaMsrRanges;
411}
412
413
414/**
415 * Inserts a new MSR range in into an sorted MSR range array.
416 *
417 * If the new MSR range overlaps existing ranges, the existing ones will be
418 * adjusted/removed to fit in the new one.
419 *
420 * @returns VBox status code.
421 * @retval VINF_SUCCESS
422 * @retval VERR_NO_MEMORY
423 *
424 * @param pVM The cross context VM structure. If NULL,
425 * use the process heap, otherwise the VM's hyper heap.
426 * @param ppaMsrRanges The variable pointing to the ranges (input/output).
427 * Must be NULL if using the hyper heap.
428 * @param pcMsrRanges The variable holding number of ranges. Must be NULL
429 * if using the hyper heap.
430 * @param pNewRange The new range.
431 */
432int cpumR3MsrRangesInsert(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t *pcMsrRanges, PCCPUMMSRRANGE pNewRange)
433{
434 Assert(pNewRange->uLast >= pNewRange->uFirst);
435 Assert(pNewRange->enmRdFn > kCpumMsrRdFn_Invalid && pNewRange->enmRdFn < kCpumMsrRdFn_End);
436 Assert(pNewRange->enmWrFn > kCpumMsrWrFn_Invalid && pNewRange->enmWrFn < kCpumMsrWrFn_End);
437
438 /*
439 * Validate and use the VM's MSR ranges array if we are using the hyper heap.
440 */
441 if (pVM)
442 {
443 AssertReturn(!ppaMsrRanges, VERR_INVALID_PARAMETER);
444 AssertReturn(!pcMsrRanges, VERR_INVALID_PARAMETER);
445 AssertReturn(pVM->cpum.s.GuestInfo.paMsrRangesR3 == pVM->cpum.s.GuestInfo.aMsrRanges, VERR_INTERNAL_ERROR_3);
446
447 ppaMsrRanges = &pVM->cpum.s.GuestInfo.paMsrRangesR3;
448 pcMsrRanges = &pVM->cpum.s.GuestInfo.cMsrRanges;
449 }
450 else
451 {
452 AssertReturn(ppaMsrRanges, VERR_INVALID_POINTER);
453 AssertReturn(pcMsrRanges, VERR_INVALID_POINTER);
454 }
455
456 uint32_t cMsrRanges = *pcMsrRanges;
457 PCPUMMSRRANGE paMsrRanges = *ppaMsrRanges;
458
459 /*
460 * Optimize the linear insertion case where we add new entries at the end.
461 */
462 if ( cMsrRanges > 0
463 && paMsrRanges[cMsrRanges - 1].uLast < pNewRange->uFirst)
464 {
465 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
466 if (!paMsrRanges)
467 return VERR_NO_MEMORY;
468 paMsrRanges[cMsrRanges] = *pNewRange;
469 *pcMsrRanges += 1;
470 }
471 else
472 {
473 uint32_t i = cpumR3MsrRangesBinSearch(paMsrRanges, cMsrRanges, pNewRange->uFirst);
474 Assert(i == cMsrRanges || pNewRange->uFirst <= paMsrRanges[i].uLast);
475 Assert(i == 0 || pNewRange->uFirst > paMsrRanges[i - 1].uLast);
476
477 /*
478 * Adding an entirely new entry?
479 */
480 if ( i >= cMsrRanges
481 || pNewRange->uLast < paMsrRanges[i].uFirst)
482 {
483 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
484 if (!paMsrRanges)
485 return VERR_NO_MEMORY;
486 if (i < cMsrRanges)
487 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
488 paMsrRanges[i] = *pNewRange;
489 *pcMsrRanges += 1;
490 }
491 /*
492 * Replace existing entry?
493 */
494 else if ( pNewRange->uFirst == paMsrRanges[i].uFirst
495 && pNewRange->uLast == paMsrRanges[i].uLast)
496 paMsrRanges[i] = *pNewRange;
497 /*
498 * Splitting an existing entry?
499 */
500 else if ( pNewRange->uFirst > paMsrRanges[i].uFirst
501 && pNewRange->uLast < paMsrRanges[i].uLast)
502 {
503 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 2);
504 if (!paMsrRanges)
505 return VERR_NO_MEMORY;
506 if (i < cMsrRanges)
507 memmove(&paMsrRanges[i + 2], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
508 paMsrRanges[i + 1] = *pNewRange;
509 paMsrRanges[i + 2] = paMsrRanges[i];
510 paMsrRanges[i ].uLast = pNewRange->uFirst - 1;
511 paMsrRanges[i + 2].uFirst = pNewRange->uLast + 1;
512 *pcMsrRanges += 2;
513 }
514 /*
515 * Complicated scenarios that can affect more than one range.
516 *
517 * The current code does not optimize memmove calls when replacing
518 * one or more existing ranges, because it's tedious to deal with and
519 * not expected to be a frequent usage scenario.
520 */
521 else
522 {
523 /* Adjust start of first match? */
524 if ( pNewRange->uFirst <= paMsrRanges[i].uFirst
525 && pNewRange->uLast < paMsrRanges[i].uLast)
526 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
527 else
528 {
529 /* Adjust end of first match? */
530 if (pNewRange->uFirst > paMsrRanges[i].uFirst)
531 {
532 Assert(paMsrRanges[i].uLast >= pNewRange->uFirst);
533 paMsrRanges[i].uLast = pNewRange->uFirst - 1;
534 i++;
535 }
536 /* Replace the whole first match (lazy bird). */
537 else
538 {
539 if (i + 1 < cMsrRanges)
540 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
541 cMsrRanges = *pcMsrRanges -= 1;
542 }
543
544 /* Do the new range affect more ranges? */
545 while ( i < cMsrRanges
546 && pNewRange->uLast >= paMsrRanges[i].uFirst)
547 {
548 if (pNewRange->uLast < paMsrRanges[i].uLast)
549 {
550 /* Adjust the start of it, then we're done. */
551 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
552 break;
553 }
554
555 /* Remove it entirely. */
556 if (i + 1 < cMsrRanges)
557 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
558 cMsrRanges = *pcMsrRanges -= 1;
559 }
560 }
561
562 /* Now, perform a normal insertion. */
563 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
564 if (!paMsrRanges)
565 return VERR_NO_MEMORY;
566 if (i < cMsrRanges)
567 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
568 paMsrRanges[i] = *pNewRange;
569 *pcMsrRanges += 1;
570 }
571 }
572
573 return VINF_SUCCESS;
574}
575
576
577/**
578 * Reconciles CPUID info with MSRs (selected ones).
579 *
580 * @returns VBox status code.
581 * @param pVM The cross context VM structure.
582 */
583int cpumR3MsrReconcileWithCpuId(PVM pVM)
584{
585 PCCPUMMSRRANGE papToAdd[10];
586 uint32_t cToAdd = 0;
587
588 /*
589 * The IA32_FLUSH_CMD MSR was introduced in MCUs for CVS-2018-3646 and associates.
590 */
591 if (pVM->cpum.s.GuestFeatures.fFlushCmd && !cpumLookupMsrRange(pVM, MSR_IA32_FLUSH_CMD))
592 {
593 static CPUMMSRRANGE const s_FlushCmd =
594 {
595 /*.uFirst =*/ MSR_IA32_FLUSH_CMD,
596 /*.uLast =*/ MSR_IA32_FLUSH_CMD,
597 /*.enmRdFn =*/ kCpumMsrRdFn_WriteOnly,
598 /*.enmWrFn =*/ kCpumMsrWrFn_Ia32FlushCmd,
599 /*.offCpumCpu =*/ UINT16_MAX,
600 /*.fReserved =*/ 0,
601 /*.uValue =*/ 0,
602 /*.fWrIgnMask =*/ 0,
603 /*.fWrGpMask =*/ ~MSR_IA32_FLUSH_CMD_F_L1D,
604 /*.szName = */ "IA32_FLUSH_CMD"
605 };
606 papToAdd[cToAdd++] = &s_FlushCmd;
607 }
608
609 /*
610 * The MSR_IA32_ARCH_CAPABILITIES was introduced in various spectre MCUs, or at least
611 * documented in relation to such.
612 */
613 if (pVM->cpum.s.GuestFeatures.fArchCap && !cpumLookupMsrRange(pVM, MSR_IA32_ARCH_CAPABILITIES))
614 {
615 static CPUMMSRRANGE const s_ArchCaps =
616 {
617 /*.uFirst =*/ MSR_IA32_ARCH_CAPABILITIES,
618 /*.uLast =*/ MSR_IA32_ARCH_CAPABILITIES,
619 /*.enmRdFn =*/ kCpumMsrRdFn_Ia32ArchCapabilities,
620 /*.enmWrFn =*/ kCpumMsrWrFn_ReadOnly,
621 /*.offCpumCpu =*/ UINT16_MAX,
622 /*.fReserved =*/ 0,
623 /*.uValue =*/ 0,
624 /*.fWrIgnMask =*/ 0,
625 /*.fWrGpMask =*/ UINT64_MAX,
626 /*.szName = */ "IA32_ARCH_CAPABILITIES"
627 };
628 papToAdd[cToAdd++] = &s_ArchCaps;
629 }
630
631 /*
632 * Do the adding.
633 */
634 for (uint32_t i = 0; i < cToAdd; i++)
635 {
636 PCCPUMMSRRANGE pRange = papToAdd[i];
637 LogRel(("CPUM: MSR/CPUID reconciliation insert: %#010x %s\n", pRange->uFirst, pRange->szName));
638 int rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
639 pRange);
640 if (RT_FAILURE(rc))
641 return rc;
642 }
643 return VINF_SUCCESS;
644}
645
646
647/**
648 * Worker for cpumR3MsrApplyFudge that applies one table.
649 *
650 * @returns VBox status code.
651 * @param pVM The cross context VM structure.
652 * @param paRanges Array of MSRs to fudge.
653 * @param cRanges Number of MSRs in the array.
654 */
655static int cpumR3MsrApplyFudgeTable(PVM pVM, PCCPUMMSRRANGE paRanges, size_t cRanges)
656{
657 for (uint32_t i = 0; i < cRanges; i++)
658 if (!cpumLookupMsrRange(pVM, paRanges[i].uFirst))
659 {
660 LogRel(("CPUM: MSR fudge: %#010x %s\n", paRanges[i].uFirst, paRanges[i].szName));
661 int rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
662 &paRanges[i]);
663 if (RT_FAILURE(rc))
664 return rc;
665 }
666 return VINF_SUCCESS;
667}
668
669
670/**
671 * Fudges the MSRs that guest are known to access in some odd cases.
672 *
673 * A typical example is a VM that has been moved between different hosts where
674 * for instance the cpu vendor differs.
675 *
676 * Another example is older CPU profiles (e.g. Atom Bonnet) for newer CPUs (e.g.
677 * Atom Silvermont), where features reported thru CPUID aren't present in the
678 * MSRs (e.g. AMD64_TSC_AUX).
679 *
680 *
681 * @returns VBox status code.
682 * @param pVM The cross context VM structure.
683 */
684int cpumR3MsrApplyFudge(PVM pVM)
685{
686 /*
687 * Basic.
688 */
689 static CPUMMSRRANGE const s_aFudgeMsrs[] =
690 {
691 MFO(0x00000000, "IA32_P5_MC_ADDR", Ia32P5McAddr),
692 MFX(0x00000001, "IA32_P5_MC_TYPE", Ia32P5McType, Ia32P5McType, 0, 0, UINT64_MAX),
693 MVO(0x00000017, "IA32_PLATFORM_ID", 0),
694 MFN(0x0000001b, "IA32_APIC_BASE", Ia32ApicBase, Ia32ApicBase),
695 MVI(0x0000008b, "BIOS_SIGN", 0),
696 MFX(0x000000fe, "IA32_MTRRCAP", Ia32MtrrCap, ReadOnly, 0x508, 0, 0),
697 MFX(0x00000179, "IA32_MCG_CAP", Ia32McgCap, ReadOnly, 0x005, 0, 0),
698 MFX(0x0000017a, "IA32_MCG_STATUS", Ia32McgStatus, Ia32McgStatus, 0, ~(uint64_t)UINT32_MAX, 0),
699 MFN(0x000001a0, "IA32_MISC_ENABLE", Ia32MiscEnable, Ia32MiscEnable),
700 MFN(0x000001d9, "IA32_DEBUGCTL", Ia32DebugCtl, Ia32DebugCtl),
701 MFO(0x000001db, "P6_LAST_BRANCH_FROM_IP", P6LastBranchFromIp),
702 MFO(0x000001dc, "P6_LAST_BRANCH_TO_IP", P6LastBranchToIp),
703 MFO(0x000001dd, "P6_LAST_INT_FROM_IP", P6LastIntFromIp),
704 MFO(0x000001de, "P6_LAST_INT_TO_IP", P6LastIntToIp),
705 MFS(0x00000277, "IA32_PAT", Ia32Pat, Ia32Pat, Guest.msrPAT),
706 MFZ(0x000002ff, "IA32_MTRR_DEF_TYPE", Ia32MtrrDefType, Ia32MtrrDefType, GuestMsrs.msr.MtrrDefType, 0, ~(uint64_t)0xc07),
707 MFN(0x00000400, "IA32_MCi_CTL_STATUS_ADDR_MISC", Ia32McCtlStatusAddrMiscN, Ia32McCtlStatusAddrMiscN),
708 };
709 int rc = cpumR3MsrApplyFudgeTable(pVM, &s_aFudgeMsrs[0], RT_ELEMENTS(s_aFudgeMsrs));
710 AssertLogRelRCReturn(rc, rc);
711
712 /*
713 * XP might mistake opterons and other newer CPUs for P4s.
714 */
715 if (pVM->cpum.s.GuestFeatures.uFamily >= 0xf)
716 {
717 static CPUMMSRRANGE const s_aP4FudgeMsrs[] =
718 {
719 MFX(0x0000002c, "P4_EBC_FREQUENCY_ID", IntelP4EbcFrequencyId, IntelP4EbcFrequencyId, 0xf12010f, UINT64_MAX, 0),
720 };
721 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aP4FudgeMsrs[0], RT_ELEMENTS(s_aP4FudgeMsrs));
722 AssertLogRelRCReturn(rc, rc);
723 }
724
725 if (pVM->cpum.s.GuestFeatures.fRdTscP)
726 {
727 static CPUMMSRRANGE const s_aRdTscPFudgeMsrs[] =
728 {
729 MFX(0xc0000103, "AMD64_TSC_AUX", Amd64TscAux, Amd64TscAux, 0, 0, ~(uint64_t)UINT32_MAX),
730 };
731 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aRdTscPFudgeMsrs[0], RT_ELEMENTS(s_aRdTscPFudgeMsrs));
732 AssertLogRelRCReturn(rc, rc);
733 }
734
735 /*
736 * Windows 10 incorrectly writes to MSR_IA32_TSX_CTRL without checking
737 * CPUID.ARCH_CAP(EAX=7h,ECX=0):EDX[bit 29] or the MSR feature bits in
738 * MSR_IA32_ARCH_CAPABILITIES[bit 7], see @bugref{9630}.
739 * Ignore writes to this MSR and return 0 on reads.
740 */
741 if (pVM->cpum.s.GuestFeatures.fArchCap)
742 {
743 static CPUMMSRRANGE const s_aTsxCtrl[] =
744 {
745 MVI(MSR_IA32_TSX_CTRL, "IA32_TSX_CTRL", 0),
746 };
747 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aTsxCtrl[0], RT_ELEMENTS(s_aTsxCtrl));
748 AssertLogRelRCReturn(rc, rc);
749 }
750
751 return rc;
752}
753
754
755/**
756 * Do we consider @a enmConsider a better match for @a enmTarget than
757 * @a enmFound?
758 *
759 * Only called when @a enmConsider isn't exactly what we're looking for.
760 *
761 * @returns true/false.
762 * @param enmConsider The new microarch to consider.
763 * @param enmTarget The target microarch.
764 * @param enmFound The best microarch match we've found thus far.
765 */
766DECLINLINE(bool) cpumR3DbIsBetterMarchMatch(CPUMMICROARCH enmConsider, CPUMMICROARCH enmTarget, CPUMMICROARCH enmFound)
767{
768 Assert(enmConsider != enmTarget);
769
770 /*
771 * If we've got an march match, don't bother with enmConsider.
772 */
773 if (enmFound == enmTarget)
774 return false;
775
776 /*
777 * Found is below: Pick 'consider' if it's closer to the target or above it.
778 */
779 if (enmFound < enmTarget)
780 return enmConsider > enmFound;
781
782 /*
783 * Found is above: Pick 'consider' if it's also above (paranoia: or equal)
784 * and but closer to the target.
785 */
786 return enmConsider >= enmTarget && enmConsider < enmFound;
787}
788
789
790/**
791 * Do we consider @a enmConsider a better match for @a enmTarget than
792 * @a enmFound?
793 *
794 * Only called for intel family 06h CPUs.
795 *
796 * @returns true/false.
797 * @param enmConsider The new microarch to consider.
798 * @param enmTarget The target microarch.
799 * @param enmFound The best microarch match we've found thus far.
800 */
801static bool cpumR3DbIsBetterIntelFam06Match(CPUMMICROARCH enmConsider, CPUMMICROARCH enmTarget, CPUMMICROARCH enmFound)
802{
803 /* Check intel family 06h claims. */
804 AssertReturn(enmConsider >= kCpumMicroarch_Intel_P6_Core_Atom_First && enmConsider <= kCpumMicroarch_Intel_P6_Core_Atom_End,
805 false);
806 AssertReturn( (enmTarget >= kCpumMicroarch_Intel_P6_Core_Atom_First && enmTarget <= kCpumMicroarch_Intel_P6_Core_Atom_End)
807 || enmTarget == kCpumMicroarch_Intel_Unknown,
808 false);
809
810 /* Put matches out of the way. */
811 if (enmConsider == enmTarget)
812 return true;
813 if (enmFound == enmTarget)
814 return false;
815
816 /* If found isn't a family 06h march, whatever we're considering must be a better choice. */
817 if ( enmFound < kCpumMicroarch_Intel_P6_Core_Atom_First
818 || enmFound > kCpumMicroarch_Intel_P6_Core_Atom_End)
819 return true;
820
821 /*
822 * The family 06h stuff is split into three categories:
823 * - Common P6 heritage
824 * - Core
825 * - Atom
826 *
827 * Determin which of the three arguments are Atom marchs, because that's
828 * all we need to make the right choice.
829 */
830 bool const fConsiderAtom = enmConsider >= kCpumMicroarch_Intel_Atom_First;
831 bool const fTargetAtom = enmTarget >= kCpumMicroarch_Intel_Atom_First;
832 bool const fFoundAtom = enmFound >= kCpumMicroarch_Intel_Atom_First;
833
834 /*
835 * Want atom:
836 */
837 if (fTargetAtom)
838 {
839 /* Pick the atom if we've got one of each.*/
840 if (fConsiderAtom != fFoundAtom)
841 return fConsiderAtom;
842 /* If we haven't got any atoms under consideration, pick a P6 or the earlier core.
843 Note! Not entirely sure Dothan is the best choice, but it'll do for now. */
844 if (!fConsiderAtom)
845 {
846 if (enmConsider > enmFound)
847 return enmConsider <= kCpumMicroarch_Intel_P6_M_Dothan;
848 return enmFound > kCpumMicroarch_Intel_P6_M_Dothan;
849 }
850 /* else: same category, default comparison rules. */
851 Assert(fConsiderAtom && fFoundAtom);
852 }
853 /*
854 * Want non-atom:
855 */
856 /* Pick the non-atom if we've got one of each. */
857 else if (fConsiderAtom != fFoundAtom)
858 return fFoundAtom;
859 /* If we've only got atoms under consideration, pick the older one just to pick something. */
860 else if (fConsiderAtom)
861 return enmConsider < enmFound;
862 else
863 Assert(!fConsiderAtom && !fFoundAtom);
864
865 /*
866 * Same basic category. Do same compare as caller.
867 */
868 return cpumR3DbIsBetterMarchMatch(enmConsider, enmTarget, enmFound);
869}
870
871
872int cpumR3DbGetCpuInfo(const char *pszName, PCPUMINFO pInfo)
873{
874 CPUMDBENTRY const *pEntry = NULL;
875 int rc;
876
877 if (!strcmp(pszName, "host"))
878#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
879 {
880 /*
881 * Create a CPU database entry for the host CPU. This means getting
882 * the CPUID bits from the real CPU and grabbing the closest matching
883 * database entry for MSRs.
884 */
885 rc = CPUMR3CpuIdDetectUnknownLeafMethod(&pInfo->enmUnknownCpuIdMethod, &pInfo->DefCpuId);
886 if (RT_FAILURE(rc))
887 return rc;
888 rc = CPUMR3CpuIdCollectLeaves(&pInfo->paCpuIdLeavesR3, &pInfo->cCpuIdLeaves);
889 if (RT_FAILURE(rc))
890 return rc;
891 pInfo->fMxCsrMask = CPUMR3DeterminHostMxCsrMask();
892
893 /* Lookup database entry for MSRs. */
894 CPUMCPUVENDOR const enmVendor = CPUMR3CpuIdDetectVendorEx(pInfo->paCpuIdLeavesR3[0].uEax,
895 pInfo->paCpuIdLeavesR3[0].uEbx,
896 pInfo->paCpuIdLeavesR3[0].uEcx,
897 pInfo->paCpuIdLeavesR3[0].uEdx);
898 uint32_t const uStd1Eax = pInfo->paCpuIdLeavesR3[1].uEax;
899 uint8_t const uFamily = ASMGetCpuFamily(uStd1Eax);
900 uint8_t const uModel = ASMGetCpuModel(uStd1Eax, enmVendor == CPUMCPUVENDOR_INTEL);
901 uint8_t const uStepping = ASMGetCpuStepping(uStd1Eax);
902 CPUMMICROARCH const enmMicroarch = CPUMR3CpuIdDetermineMicroarchEx(enmVendor, uFamily, uModel, uStepping);
903
904 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
905 {
906 CPUMDBENTRY const *pCur = g_apCpumDbEntries[i];
907 if ((CPUMCPUVENDOR)pCur->enmVendor == enmVendor)
908 {
909 /* Match against Family, Microarch, model and stepping. Except
910 for family, always match the closer with preference given to
911 the later/older ones. */
912 if (pCur->uFamily == uFamily)
913 {
914 if (pCur->enmMicroarch == enmMicroarch)
915 {
916 if (pCur->uModel == uModel)
917 {
918 if (pCur->uStepping == uStepping)
919 {
920 /* Perfect match. */
921 pEntry = pCur;
922 break;
923 }
924
925 if ( !pEntry
926 || pEntry->uModel != uModel
927 || pEntry->enmMicroarch != enmMicroarch
928 || pEntry->uFamily != uFamily)
929 pEntry = pCur;
930 else if ( pCur->uStepping >= uStepping
931 ? pCur->uStepping < pEntry->uStepping || pEntry->uStepping < uStepping
932 : pCur->uStepping > pEntry->uStepping)
933 pEntry = pCur;
934 }
935 else if ( !pEntry
936 || pEntry->enmMicroarch != enmMicroarch
937 || pEntry->uFamily != uFamily)
938 pEntry = pCur;
939 else if ( pCur->uModel >= uModel
940 ? pCur->uModel < pEntry->uModel || pEntry->uModel < uModel
941 : pCur->uModel > pEntry->uModel)
942 pEntry = pCur;
943 }
944 else if ( !pEntry
945 || pEntry->uFamily != uFamily)
946 pEntry = pCur;
947 /* Special march matching rules applies to intel family 06h. */
948 else if ( enmVendor == CPUMCPUVENDOR_INTEL
949 && uFamily == 6
950 ? cpumR3DbIsBetterIntelFam06Match(pCur->enmMicroarch, enmMicroarch, pEntry->enmMicroarch)
951 : cpumR3DbIsBetterMarchMatch(pCur->enmMicroarch, enmMicroarch, pEntry->enmMicroarch))
952 pEntry = pCur;
953 }
954 /* We don't do closeness matching on family, we use the first
955 entry for the CPU vendor instead. (P4 workaround.) */
956 else if (!pEntry)
957 pEntry = pCur;
958 }
959 }
960
961 if (pEntry)
962 LogRel(("CPUM: Matched host CPU %s %#x/%#x/%#x %s with CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
963 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
964 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor), pEntry->uFamily, pEntry->uModel,
965 pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
966 else
967 {
968 pEntry = g_apCpumDbEntries[0];
969 LogRel(("CPUM: No matching processor database entry %s %#x/%#x/%#x %s, falling back on '%s'\n",
970 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
971 pEntry->pszName));
972 }
973 }
974 else
975#else
976 pszName = g_apCpumDbEntries[0]->pszName; /* Just pick the first entry for non-x86 hosts. */
977#endif
978 {
979 /*
980 * We're supposed to be emulating a specific CPU that is included in
981 * our CPU database. The CPUID tables needs to be copied onto the
982 * heap so the caller can modify them and so they can be freed like
983 * in the host case above.
984 */
985 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
986 if (!strcmp(pszName, g_apCpumDbEntries[i]->pszName))
987 {
988 pEntry = g_apCpumDbEntries[i];
989 break;
990 }
991 if (!pEntry)
992 {
993 LogRel(("CPUM: Cannot locate any CPU by the name '%s'\n", pszName));
994 return VERR_CPUM_DB_CPU_NOT_FOUND;
995 }
996
997 pInfo->cCpuIdLeaves = pEntry->cCpuIdLeaves;
998 if (pEntry->cCpuIdLeaves)
999 {
1000 /* Must allocate a multiple of 16 here, matching cpumR3CpuIdEnsureSpace. */
1001 size_t cbExtra = sizeof(pEntry->paCpuIdLeaves[0]) * (RT_ALIGN(pEntry->cCpuIdLeaves, 16) - pEntry->cCpuIdLeaves);
1002 pInfo->paCpuIdLeavesR3 = (PCPUMCPUIDLEAF)RTMemDupEx(pEntry->paCpuIdLeaves,
1003 sizeof(pEntry->paCpuIdLeaves[0]) * pEntry->cCpuIdLeaves,
1004 cbExtra);
1005 if (!pInfo->paCpuIdLeavesR3)
1006 return VERR_NO_MEMORY;
1007 }
1008 else
1009 pInfo->paCpuIdLeavesR3 = NULL;
1010
1011 pInfo->enmUnknownCpuIdMethod = pEntry->enmUnknownCpuId;
1012 pInfo->DefCpuId = pEntry->DefUnknownCpuId;
1013 pInfo->fMxCsrMask = pEntry->fMxCsrMask;
1014
1015 LogRel(("CPUM: Using CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
1016 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor),
1017 pEntry->uFamily, pEntry->uModel, pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
1018 }
1019
1020 pInfo->fMsrMask = pEntry->fMsrMask;
1021 pInfo->iFirstExtCpuIdLeaf = 0; /* Set by caller. */
1022 pInfo->uScalableBusFreq = pEntry->uScalableBusFreq;
1023
1024 /*
1025 * Copy the MSR range.
1026 */
1027 uint32_t cMsrs = 0;
1028 PCPUMMSRRANGE paMsrs = NULL;
1029
1030 PCCPUMMSRRANGE pCurMsr = pEntry->paMsrRanges;
1031 uint32_t cLeft = pEntry->cMsrRanges;
1032 while (cLeft-- > 0)
1033 {
1034 rc = cpumR3MsrRangesInsert(NULL /* pVM */, &paMsrs, &cMsrs, pCurMsr);
1035 if (RT_FAILURE(rc))
1036 {
1037 Assert(!paMsrs); /* The above function frees this. */
1038 RTMemFree(pInfo->paCpuIdLeavesR3);
1039 pInfo->paCpuIdLeavesR3 = NULL;
1040 return rc;
1041 }
1042 pCurMsr++;
1043 }
1044
1045 pInfo->paMsrRangesR3 = paMsrs;
1046 pInfo->cMsrRanges = cMsrs;
1047 return VINF_SUCCESS;
1048}
1049
1050
1051/**
1052 * Insert an MSR range into the VM.
1053 *
1054 * If the new MSR range overlaps existing ranges, the existing ones will be
1055 * adjusted/removed to fit in the new one.
1056 *
1057 * @returns VBox status code.
1058 * @param pVM The cross context VM structure.
1059 * @param pNewRange Pointer to the MSR range being inserted.
1060 */
1061VMMR3DECL(int) CPUMR3MsrRangesInsert(PVM pVM, PCCPUMMSRRANGE pNewRange)
1062{
1063 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1064 AssertReturn(pNewRange, VERR_INVALID_PARAMETER);
1065
1066 return cpumR3MsrRangesInsert(pVM, NULL /* ppaMsrRanges */, NULL /* pcMsrRanges */, pNewRange);
1067}
1068
1069
1070/**
1071 * Register statistics for the MSRs.
1072 *
1073 * This must not be called before the MSRs have been finalized and moved to the
1074 * hyper heap.
1075 *
1076 * @returns VBox status code.
1077 * @param pVM The cross context VM structure.
1078 */
1079int cpumR3MsrRegStats(PVM pVM)
1080{
1081 /*
1082 * Global statistics.
1083 */
1084 PCPUM pCpum = &pVM->cpum.s;
1085 STAM_REL_REG(pVM, &pCpum->cMsrReads, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Reads",
1086 STAMUNIT_OCCURENCES, "All RDMSRs making it to CPUM.");
1087 STAM_REL_REG(pVM, &pCpum->cMsrReadsRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsRaisingGP",
1088 STAMUNIT_OCCURENCES, "RDMSR raising #GPs, except unknown MSRs.");
1089 STAM_REL_REG(pVM, &pCpum->cMsrReadsUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsUnknown",
1090 STAMUNIT_OCCURENCES, "RDMSR on unknown MSRs (raises #GP).");
1091 STAM_REL_REG(pVM, &pCpum->cMsrWrites, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Writes",
1092 STAMUNIT_OCCURENCES, "All WRMSRs making it to CPUM.");
1093 STAM_REL_REG(pVM, &pCpum->cMsrWritesRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesRaisingGP",
1094 STAMUNIT_OCCURENCES, "WRMSR raising #GPs, except unknown MSRs.");
1095 STAM_REL_REG(pVM, &pCpum->cMsrWritesToIgnoredBits, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesToIgnoredBits",
1096 STAMUNIT_OCCURENCES, "Writing of ignored bits.");
1097 STAM_REL_REG(pVM, &pCpum->cMsrWritesUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesUnknown",
1098 STAMUNIT_OCCURENCES, "WRMSR on unknown MSRs (raises #GP).");
1099
1100
1101# ifdef VBOX_WITH_STATISTICS
1102 /*
1103 * Per range.
1104 */
1105 PCPUMMSRRANGE paRanges = pVM->cpum.s.GuestInfo.paMsrRangesR3;
1106 uint32_t cRanges = pVM->cpum.s.GuestInfo.cMsrRanges;
1107 for (uint32_t i = 0; i < cRanges; i++)
1108 {
1109 char szName[160];
1110 ssize_t cchName;
1111
1112 if (paRanges[i].uFirst == paRanges[i].uLast)
1113 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%s",
1114 paRanges[i].uFirst, paRanges[i].szName);
1115 else
1116 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%#010x-%s",
1117 paRanges[i].uFirst, paRanges[i].uLast, paRanges[i].szName);
1118
1119 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-reads");
1120 STAMR3Register(pVM, &paRanges[i].cReads, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RDMSR");
1121
1122 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-writes");
1123 STAMR3Register(pVM, &paRanges[i].cWrites, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR");
1124
1125 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-GPs");
1126 STAMR3Register(pVM, &paRanges[i].cGps, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "#GPs");
1127
1128 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-ign-bits-writes");
1129 STAMR3Register(pVM, &paRanges[i].cIgnoredBits, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR w/ ignored bits");
1130 }
1131# endif /* VBOX_WITH_STATISTICS */
1132
1133 return VINF_SUCCESS;
1134}
1135
1136#endif /* !CPUM_DB_STANDALONE */
1137
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