VirtualBox

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

Last change on this file since 58251 was 58122, checked in by vboxsync, 9 years ago

VMM: Made @param pVM more uniform and to the point.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.6 KB
Line 
1/* $Id: CPUMR3Db.cpp 58122 2015-10-08 17:11:58Z vboxsync $ */
2/** @file
3 * CPUM - CPU database part.
4 */
5
6/*
7 * Copyright (C) 2013-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.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#include <iprt/asm-amd64-x86.h>
30#include <iprt/mem.h>
31#include <iprt/string.h>
32
33
34/*********************************************************************************************************************************
35* Structures and Typedefs *
36*********************************************************************************************************************************/
37typedef struct CPUMDBENTRY
38{
39 /** The CPU name. */
40 const char *pszName;
41 /** The full CPU name. */
42 const char *pszFullName;
43 /** The CPU vendor (CPUMCPUVENDOR). */
44 uint8_t enmVendor;
45 /** The CPU family. */
46 uint8_t uFamily;
47 /** The CPU model. */
48 uint8_t uModel;
49 /** The CPU stepping. */
50 uint8_t uStepping;
51 /** The microarchitecture. */
52 CPUMMICROARCH enmMicroarch;
53 /** Scalable bus frequency used for reporting other frequencies. */
54 uint64_t uScalableBusFreq;
55 /** Flags (TBD). */
56 uint32_t fFlags;
57 /** The maximum physical address with of the CPU. This should correspond to
58 * the value in CPUID leaf 0x80000008 when present. */
59 uint8_t cMaxPhysAddrWidth;
60 /** Pointer to an array of CPUID leaves. */
61 PCCPUMCPUIDLEAF paCpuIdLeaves;
62 /** The number of CPUID leaves in the array paCpuIdLeaves points to. */
63 uint32_t cCpuIdLeaves;
64 /** The method used to deal with unknown CPUID leaves. */
65 CPUMUNKNOWNCPUID enmUnknownCpuId;
66 /** The default unknown CPUID value. */
67 CPUMCPUID DefUnknownCpuId;
68
69 /** MSR mask. Several microarchitectures ignore higher bits of the */
70 uint32_t fMsrMask;
71
72 /** The number of ranges in the table pointed to b paMsrRanges. */
73 uint32_t cMsrRanges;
74 /** MSR ranges for this CPU. */
75 PCCPUMMSRRANGE paMsrRanges;
76} CPUMDBENTRY;
77
78
79/*********************************************************************************************************************************
80* Defined Constants And Macros *
81*********************************************************************************************************************************/
82
83/** @def NULL_ALONE
84 * For eliminating an unnecessary data dependency in standalone builds (for
85 * VBoxSVC). */
86/** @def ZERO_ALONE
87 * For eliminating an unnecessary data size dependency in standalone builds (for
88 * VBoxSVC). */
89#ifndef CPUM_DB_STANDALONE
90# define NULL_ALONE(a_aTable) a_aTable
91# define ZERO_ALONE(a_cTable) a_cTable
92#else
93# define NULL_ALONE(a_aTable) NULL
94# define ZERO_ALONE(a_cTable) 0
95#endif
96
97
98/** @name Short macros for the MSR range entries.
99 *
100 * These are rather cryptic, but this is to reduce the attack on the right
101 * margin.
102 *
103 * @{ */
104/** Alias one MSR onto another (a_uTarget). */
105#define MAL(a_uMsr, a_szName, a_uTarget) \
106 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_MsrAlias, kCpumMsrWrFn_MsrAlias, 0, a_uTarget, 0, 0, a_szName)
107/** Functions handles everything. */
108#define MFN(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff) \
109 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, 0, a_szName)
110/** Functions handles everything, with GP mask. */
111#define MFG(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_fWrGpMask) \
112 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, a_fWrGpMask, a_szName)
113/** Function handlers, read-only. */
114#define MFO(a_uMsr, a_szName, a_enmRdFnSuff) \
115 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_ReadOnly, 0, 0, 0, UINT64_MAX, a_szName)
116/** Function handlers, ignore all writes. */
117#define MFI(a_uMsr, a_szName, a_enmRdFnSuff) \
118 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_IgnoreWrite, 0, 0, UINT64_MAX, 0, a_szName)
119/** Function handlers, with value. */
120#define MFV(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uValue) \
121 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, a_uValue, 0, 0, a_szName)
122/** Function handlers, with write ignore mask. */
123#define MFW(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_fWrIgnMask) \
124 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, a_fWrIgnMask, 0, a_szName)
125/** Function handlers, extended version. */
126#define MFX(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uValue, a_fWrIgnMask, a_fWrGpMask) \
127 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, a_uValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
128/** Function handlers, with CPUMCPU storage variable. */
129#define MFS(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_CpumCpuMember) \
130 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, \
131 RT_OFFSETOF(CPUMCPU, a_CpumCpuMember), 0, 0, 0, a_szName)
132/** Function handlers, with CPUMCPU storage variable, ignore mask and GP mask. */
133#define MFZ(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_CpumCpuMember, a_fWrIgnMask, a_fWrGpMask) \
134 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, \
135 RT_OFFSETOF(CPUMCPU, a_CpumCpuMember), 0, a_fWrIgnMask, a_fWrGpMask, a_szName)
136/** Read-only fixed value. */
137#define MVO(a_uMsr, a_szName, a_uValue) \
138 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_ReadOnly, 0, a_uValue, 0, UINT64_MAX, a_szName)
139/** Read-only fixed value, ignores all writes. */
140#define MVI(a_uMsr, a_szName, a_uValue) \
141 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, UINT64_MAX, 0, a_szName)
142/** Read fixed value, ignore writes outside GP mask. */
143#define MVG(a_uMsr, a_szName, a_uValue, a_fWrGpMask) \
144 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, 0, a_fWrGpMask, a_szName)
145/** Read fixed value, extended version with both GP and ignore masks. */
146#define MVX(a_uMsr, a_szName, a_uValue, a_fWrIgnMask, a_fWrGpMask) \
147 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
148/** The short form, no CPUM backing. */
149#define MSN(a_uMsr, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask) \
150 RINT(a_uMsr, a_uMsr, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, \
151 a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
152
153/** Range: Functions handles everything. */
154#define RFN(a_uFirst, a_uLast, a_szName, a_enmRdFnSuff, a_enmWrFnSuff) \
155 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, 0, 0, 0, a_szName)
156/** Range: Read fixed value, read-only. */
157#define RVO(a_uFirst, a_uLast, a_szName, a_uValue) \
158 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_ReadOnly, 0, a_uValue, 0, UINT64_MAX, a_szName)
159/** Range: Read fixed value, ignore writes. */
160#define RVI(a_uFirst, a_uLast, a_szName, a_uValue) \
161 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_FixedValue, kCpumMsrWrFn_IgnoreWrite, 0, a_uValue, UINT64_MAX, 0, a_szName)
162/** Range: The short form, no CPUM backing. */
163#define RSN(a_uFirst, a_uLast, a_szName, a_enmRdFnSuff, a_enmWrFnSuff, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask) \
164 RINT(a_uFirst, a_uLast, kCpumMsrRdFn_##a_enmRdFnSuff, kCpumMsrWrFn_##a_enmWrFnSuff, 0, \
165 a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName)
166
167/** Internal form used by the macros. */
168#ifdef VBOX_WITH_STATISTICS
169# define RINT(a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName) \
170 { a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, 0, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName, \
171 { 0 }, { 0 }, { 0 }, { 0 } }
172#else
173# define RINT(a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName) \
174 { a_uFirst, a_uLast, a_enmRdFn, a_enmWrFn, a_offCpumCpu, 0, a_uInitOrReadValue, a_fWrIgnMask, a_fWrGpMask, a_szName }
175#endif
176/** @} */
177
178
179#include "cpus/Intel_Core_i7_3960X.h"
180#include "cpus/Intel_Core_i5_3570.h"
181#include "cpus/Intel_Core_i7_2635QM.h"
182#include "cpus/Intel_Xeon_X5482_3_20GHz.h"
183#include "cpus/Intel_Pentium_M_processor_2_00GHz.h"
184#include "cpus/Intel_Pentium_4_3_00GHz.h"
185
186#include "cpus/AMD_FX_8150_Eight_Core.h"
187#include "cpus/AMD_Phenom_II_X6_1100T.h"
188#include "cpus/Quad_Core_AMD_Opteron_2384.h"
189#include "cpus/AMD_Athlon_64_X2_Dual_Core_4200.h"
190#include "cpus/AMD_Athlon_64_3200.h"
191
192#include "cpus/VIA_QuadCore_L4700_1_2_GHz.h"
193
194
195
196/**
197 * The database entries.
198 *
199 * 1. The first entry is special. It is the fallback for unknown
200 * processors. Thus, it better be pretty representative.
201 *
202 * 2. The first entry for a CPU vendor is likewise important as it is
203 * the default entry for that vendor.
204 *
205 * Generally we put the most recent CPUs first, since these tend to have the
206 * most complicated and backwards compatible list of MSRs.
207 */
208static CPUMDBENTRY const * const g_apCpumDbEntries[] =
209{
210#ifdef VBOX_CPUDB_Intel_Core_i5_3570
211 &g_Entry_Intel_Core_i5_3570,
212#endif
213#ifdef VBOX_CPUDB_Intel_Core_i7_3960X
214 &g_Entry_Intel_Core_i7_3960X,
215#endif
216#ifdef VBOX_CPUDB_Intel_Core_i7_2635QM
217 &g_Entry_Intel_Core_i7_2635QM,
218#endif
219#ifdef Intel_Pentium_M_processor_2_00GHz
220 &g_Entry_Intel_Pentium_M_processor_2_00GHz,
221#endif
222#ifdef VBOX_CPUDB_Intel_Xeon_X5482_3_20GHz
223 &g_Entry_Intel_Xeon_X5482_3_20GHz,
224#endif
225#ifdef VBOX_CPUDB_Intel_Pentium_4_3_00GHz
226 &g_Entry_Intel_Pentium_4_3_00GHz,
227#endif
228
229#ifdef VBOX_CPUDB_AMD_FX_8150_Eight_Core
230 &g_Entry_AMD_FX_8150_Eight_Core,
231#endif
232#ifdef VBOX_CPUDB_AMD_Phenom_II_X6_1100T
233 &g_Entry_AMD_Phenom_II_X6_1100T,
234#endif
235#ifdef VBOX_CPUDB_Quad_Core_AMD_Opteron_2384
236 &g_Entry_Quad_Core_AMD_Opteron_2384,
237#endif
238#ifdef VBOX_CPUDB_AMD_Athlon_64_X2_Dual_Core_4200
239 &g_Entry_AMD_Athlon_64_X2_Dual_Core_4200,
240#endif
241#ifdef VBOX_CPUDB_AMD_Athlon_64_3200
242 &g_Entry_AMD_Athlon_64_3200,
243#endif
244
245#ifdef VBOX_CPUDB_VIA_QuadCore_L4700_1_2_GHz
246 &g_Entry_VIA_QuadCore_L4700_1_2_GHz,
247#endif
248};
249
250
251#ifndef CPUM_DB_STANDALONE
252
253/**
254 * Binary search used by cpumR3MsrRangesInsert and has some special properties
255 * wrt to mismatches.
256 *
257 * @returns Insert location.
258 * @param paMsrRanges The MSR ranges to search.
259 * @param cMsrRanges The number of MSR ranges.
260 * @param uMsr What to search for.
261 */
262static uint32_t cpumR3MsrRangesBinSearch(PCCPUMMSRRANGE paMsrRanges, uint32_t cMsrRanges, uint32_t uMsr)
263{
264 if (!cMsrRanges)
265 return 0;
266
267 uint32_t iStart = 0;
268 uint32_t iLast = cMsrRanges - 1;
269 for (;;)
270 {
271 uint32_t i = iStart + (iLast - iStart + 1) / 2;
272 if ( uMsr >= paMsrRanges[i].uFirst
273 && uMsr <= paMsrRanges[i].uLast)
274 return i;
275 if (uMsr < paMsrRanges[i].uFirst)
276 {
277 if (i <= iStart)
278 return i;
279 iLast = i - 1;
280 }
281 else
282 {
283 if (i >= iLast)
284 {
285 if (i < cMsrRanges)
286 i++;
287 return i;
288 }
289 iStart = i + 1;
290 }
291 }
292}
293
294
295/**
296 * Ensures that there is space for at least @a cNewRanges in the table,
297 * reallocating the table if necessary.
298 *
299 * @returns Pointer to the MSR ranges on success, NULL on failure. On failure
300 * @a *ppaMsrRanges is freed and set to NULL.
301 * @param pVM The cross context VM structure. If NULL,
302 * use the process heap, otherwise the VM's hyper heap.
303 * @param ppaMsrRanges The variable pointing to the ranges (input/output).
304 * @param cMsrRanges The current number of ranges.
305 * @param cNewRanges The number of ranges to be added.
306 */
307static PCPUMMSRRANGE cpumR3MsrRangesEnsureSpace(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t cMsrRanges, uint32_t cNewRanges)
308{
309 uint32_t cMsrRangesAllocated;
310 if (!pVM)
311 cMsrRangesAllocated = RT_ALIGN_32(cMsrRanges, 16);
312 else
313 {
314 /*
315 * We're using the hyper heap now, but when the range array was copied over to it from
316 * the host-context heap, we only copy the exact size and not the ensured size.
317 * See @bugref{7270}.
318 */
319 cMsrRangesAllocated = cMsrRanges;
320 }
321 if (cMsrRangesAllocated < cMsrRanges + cNewRanges)
322 {
323 void *pvNew;
324 uint32_t cNew = RT_ALIGN_32(cMsrRanges + cNewRanges, 16);
325 if (pVM)
326 {
327 Assert(ppaMsrRanges == &pVM->cpum.s.GuestInfo.paMsrRangesR3);
328 Assert(cMsrRanges == pVM->cpum.s.GuestInfo.cMsrRanges);
329
330 size_t cb = cMsrRangesAllocated * sizeof(**ppaMsrRanges);
331 size_t cbNew = cNew * sizeof(**ppaMsrRanges);
332 int rc = MMR3HyperRealloc(pVM, *ppaMsrRanges, cb, 32, MM_TAG_CPUM_MSRS, cbNew, &pvNew);
333 if (RT_FAILURE(rc))
334 {
335 *ppaMsrRanges = NULL;
336 pVM->cpum.s.GuestInfo.paMsrRangesR0 = NIL_RTR0PTR;
337 pVM->cpum.s.GuestInfo.paMsrRangesRC = NIL_RTRCPTR;
338 LogRel(("CPUM: cpumR3MsrRangesEnsureSpace: MMR3HyperRealloc failed. rc=%Rrc\n", rc));
339 return NULL;
340 }
341 *ppaMsrRanges = (PCPUMMSRRANGE)pvNew;
342 }
343 else
344 {
345 pvNew = RTMemRealloc(*ppaMsrRanges, cNew * sizeof(**ppaMsrRanges));
346 if (!pvNew)
347 {
348 RTMemFree(*ppaMsrRanges);
349 *ppaMsrRanges = NULL;
350 return NULL;
351 }
352 }
353 *ppaMsrRanges = (PCPUMMSRRANGE)pvNew;
354 }
355
356 if (pVM)
357 {
358 /* Update R0 and RC pointers. */
359 Assert(ppaMsrRanges == &pVM->cpum.s.GuestInfo.paMsrRangesR3);
360 pVM->cpum.s.GuestInfo.paMsrRangesR0 = MMHyperR3ToR0(pVM, *ppaMsrRanges);
361 pVM->cpum.s.GuestInfo.paMsrRangesRC = MMHyperR3ToRC(pVM, *ppaMsrRanges);
362 }
363
364 return *ppaMsrRanges;
365}
366
367
368/**
369 * Inserts a new MSR range in into an sorted MSR range array.
370 *
371 * If the new MSR range overlaps existing ranges, the existing ones will be
372 * adjusted/removed to fit in the new one.
373 *
374 * @returns VBox status code.
375 * @retval VINF_SUCCESS
376 * @retval VERR_NO_MEMORY
377 *
378 * @param pVM The cross context VM structure. If NULL,
379 * use the process heap, otherwise the VM's hyper heap.
380 * @param ppaMsrRanges The variable pointing to the ranges (input/output).
381 * Must be NULL if using the hyper heap.
382 * @param pcMsrRanges The variable holding number of ranges. Must be NULL
383 * if using the hyper heap.
384 * @param pNewRange The new range.
385 */
386int cpumR3MsrRangesInsert(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t *pcMsrRanges, PCCPUMMSRRANGE pNewRange)
387{
388 Assert(pNewRange->uLast >= pNewRange->uFirst);
389 Assert(pNewRange->enmRdFn > kCpumMsrRdFn_Invalid && pNewRange->enmRdFn < kCpumMsrRdFn_End);
390 Assert(pNewRange->enmWrFn > kCpumMsrWrFn_Invalid && pNewRange->enmWrFn < kCpumMsrWrFn_End);
391
392 /*
393 * Validate and use the VM's MSR ranges array if we are using the hyper heap.
394 */
395 if (pVM)
396 {
397 AssertReturn(!ppaMsrRanges, VERR_INVALID_PARAMETER);
398 AssertReturn(!pcMsrRanges, VERR_INVALID_PARAMETER);
399
400 ppaMsrRanges = &pVM->cpum.s.GuestInfo.paMsrRangesR3;
401 pcMsrRanges = &pVM->cpum.s.GuestInfo.cMsrRanges;
402 }
403
404 uint32_t cMsrRanges = *pcMsrRanges;
405 PCPUMMSRRANGE paMsrRanges = *ppaMsrRanges;
406
407 /*
408 * Optimize the linear insertion case where we add new entries at the end.
409 */
410 if ( cMsrRanges > 0
411 && paMsrRanges[cMsrRanges - 1].uLast < pNewRange->uFirst)
412 {
413 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
414 if (!paMsrRanges)
415 return VERR_NO_MEMORY;
416 paMsrRanges[cMsrRanges] = *pNewRange;
417 *pcMsrRanges += 1;
418 }
419 else
420 {
421 uint32_t i = cpumR3MsrRangesBinSearch(paMsrRanges, cMsrRanges, pNewRange->uFirst);
422 Assert(i == cMsrRanges || pNewRange->uFirst <= paMsrRanges[i].uLast);
423 Assert(i == 0 || pNewRange->uFirst > paMsrRanges[i - 1].uLast);
424
425 /*
426 * Adding an entirely new entry?
427 */
428 if ( i >= cMsrRanges
429 || pNewRange->uLast < paMsrRanges[i].uFirst)
430 {
431 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
432 if (!paMsrRanges)
433 return VERR_NO_MEMORY;
434 if (i < cMsrRanges)
435 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
436 paMsrRanges[i] = *pNewRange;
437 *pcMsrRanges += 1;
438 }
439 /*
440 * Replace existing entry?
441 */
442 else if ( pNewRange->uFirst == paMsrRanges[i].uFirst
443 && pNewRange->uLast == paMsrRanges[i].uLast)
444 paMsrRanges[i] = *pNewRange;
445 /*
446 * Splitting an existing entry?
447 */
448 else if ( pNewRange->uFirst > paMsrRanges[i].uFirst
449 && pNewRange->uLast < paMsrRanges[i].uLast)
450 {
451 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 2);
452 if (!paMsrRanges)
453 return VERR_NO_MEMORY;
454 if (i < cMsrRanges)
455 memmove(&paMsrRanges[i + 2], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
456 paMsrRanges[i + 1] = *pNewRange;
457 paMsrRanges[i + 2] = paMsrRanges[i];
458 paMsrRanges[i ].uLast = pNewRange->uFirst - 1;
459 paMsrRanges[i + 2].uFirst = pNewRange->uLast + 1;
460 *pcMsrRanges += 2;
461 }
462 /*
463 * Complicated scenarios that can affect more than one range.
464 *
465 * The current code does not optimize memmove calls when replacing
466 * one or more existing ranges, because it's tedious to deal with and
467 * not expected to be a frequent usage scenario.
468 */
469 else
470 {
471 /* Adjust start of first match? */
472 if ( pNewRange->uFirst <= paMsrRanges[i].uFirst
473 && pNewRange->uLast < paMsrRanges[i].uLast)
474 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
475 else
476 {
477 /* Adjust end of first match? */
478 if (pNewRange->uFirst > paMsrRanges[i].uFirst)
479 {
480 Assert(paMsrRanges[i].uLast >= pNewRange->uFirst);
481 paMsrRanges[i].uLast = pNewRange->uFirst - 1;
482 i++;
483 }
484 /* Replace the whole first match (lazy bird). */
485 else
486 {
487 if (i + 1 < cMsrRanges)
488 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
489 cMsrRanges = *pcMsrRanges -= 1;
490 }
491
492 /* Do the new range affect more ranges? */
493 while ( i < cMsrRanges
494 && pNewRange->uLast >= paMsrRanges[i].uFirst)
495 {
496 if (pNewRange->uLast < paMsrRanges[i].uLast)
497 {
498 /* Adjust the start of it, then we're done. */
499 paMsrRanges[i].uFirst = pNewRange->uLast + 1;
500 break;
501 }
502
503 /* Remove it entirely. */
504 if (i + 1 < cMsrRanges)
505 memmove(&paMsrRanges[i], &paMsrRanges[i + 1], (cMsrRanges - i - 1) * sizeof(paMsrRanges[0]));
506 cMsrRanges = *pcMsrRanges -= 1;
507 }
508 }
509
510 /* Now, perform a normal insertion. */
511 paMsrRanges = cpumR3MsrRangesEnsureSpace(pVM, ppaMsrRanges, cMsrRanges, 1);
512 if (!paMsrRanges)
513 return VERR_NO_MEMORY;
514 if (i < cMsrRanges)
515 memmove(&paMsrRanges[i + 1], &paMsrRanges[i], (cMsrRanges - i) * sizeof(paMsrRanges[0]));
516 paMsrRanges[i] = *pNewRange;
517 *pcMsrRanges += 1;
518 }
519 }
520
521 return VINF_SUCCESS;
522}
523
524
525/**
526 * Worker for cpumR3MsrApplyFudge that applies one table.
527 *
528 * @returns VBox status code.
529 * @param pVM The cross context VM structure.
530 * @param paRanges Array of MSRs to fudge.
531 * @param cRanges Number of MSRs in the array.
532 */
533static int cpumR3MsrApplyFudgeTable(PVM pVM, PCCPUMMSRRANGE paRanges, size_t cRanges)
534{
535 for (uint32_t i = 0; i < cRanges; i++)
536 if (!cpumLookupMsrRange(pVM, paRanges[i].uFirst))
537 {
538 LogRel(("CPUM: MSR fudge: %#010x %s\n", paRanges[i].uFirst, paRanges[i].szName));
539 int rc = cpumR3MsrRangesInsert(NULL /* pVM */, &pVM->cpum.s.GuestInfo.paMsrRangesR3, &pVM->cpum.s.GuestInfo.cMsrRanges,
540 &paRanges[i]);
541 if (RT_FAILURE(rc))
542 return rc;
543 }
544 return VINF_SUCCESS;
545}
546
547
548/**
549 * Fudges the MSRs that guest are known to access in some odd cases.
550 *
551 * A typical example is a VM that has been moved between different hosts where
552 * for instance the cpu vendor differs.
553 *
554 * @returns VBox status code.
555 * @param pVM The cross context VM structure.
556 */
557int cpumR3MsrApplyFudge(PVM pVM)
558{
559 /*
560 * Basic.
561 */
562 static CPUMMSRRANGE const s_aFudgeMsrs[] =
563 {
564 MFO(0x00000000, "IA32_P5_MC_ADDR", Ia32P5McAddr),
565 MFX(0x00000001, "IA32_P5_MC_TYPE", Ia32P5McType, Ia32P5McType, 0, 0, UINT64_MAX),
566 MVO(0x00000017, "IA32_PLATFORM_ID", 0),
567 MFN(0x0000001b, "IA32_APIC_BASE", Ia32ApicBase, Ia32ApicBase),
568 MVI(0x0000008b, "BIOS_SIGN", 0),
569 MFX(0x000000fe, "IA32_MTRRCAP", Ia32MtrrCap, ReadOnly, 0x508, 0, 0),
570 MFX(0x00000179, "IA32_MCG_CAP", Ia32McgCap, ReadOnly, 0x005, 0, 0),
571 MFX(0x0000017a, "IA32_MCG_STATUS", Ia32McgStatus, Ia32McgStatus, 0, ~(uint64_t)UINT32_MAX, 0),
572 MFN(0x000001a0, "IA32_MISC_ENABLE", Ia32MiscEnable, Ia32MiscEnable),
573 MFN(0x000001d9, "IA32_DEBUGCTL", Ia32DebugCtl, Ia32DebugCtl),
574 MFO(0x000001db, "P6_LAST_BRANCH_FROM_IP", P6LastBranchFromIp),
575 MFO(0x000001dc, "P6_LAST_BRANCH_TO_IP", P6LastBranchToIp),
576 MFO(0x000001dd, "P6_LAST_INT_FROM_IP", P6LastIntFromIp),
577 MFO(0x000001de, "P6_LAST_INT_TO_IP", P6LastIntToIp),
578 MFS(0x00000277, "IA32_PAT", Ia32Pat, Ia32Pat, Guest.msrPAT),
579 MFZ(0x000002ff, "IA32_MTRR_DEF_TYPE", Ia32MtrrDefType, Ia32MtrrDefType, GuestMsrs.msr.MtrrDefType, 0, ~(uint64_t)0xc07),
580 MFN(0x00000400, "IA32_MCi_CTL_STATUS_ADDR_MISC", Ia32McCtlStatusAddrMiscN, Ia32McCtlStatusAddrMiscN),
581 };
582 int rc = cpumR3MsrApplyFudgeTable(pVM, &s_aFudgeMsrs[0], RT_ELEMENTS(s_aFudgeMsrs));
583 AssertLogRelRCReturn(rc, rc);
584
585 /*
586 * XP might mistake opterons and other newer CPUs for P4s.
587 */
588 if (pVM->cpum.s.GuestFeatures.uFamily >= 0xf)
589 {
590 static CPUMMSRRANGE const s_aP4FudgeMsrs[] =
591 {
592 MFX(0x0000002c, "P4_EBC_FREQUENCY_ID", IntelP4EbcFrequencyId, IntelP4EbcFrequencyId, 0xf12010f, UINT64_MAX, 0),
593 };
594 rc = cpumR3MsrApplyFudgeTable(pVM, &s_aP4FudgeMsrs[0], RT_ELEMENTS(s_aP4FudgeMsrs));
595 AssertLogRelRCReturn(rc, rc);
596 }
597
598 return rc;
599}
600
601
602int cpumR3DbGetCpuInfo(const char *pszName, PCPUMINFO pInfo)
603{
604 CPUMDBENTRY const *pEntry = NULL;
605 int rc;
606
607 if (!strcmp(pszName, "host"))
608 {
609 /*
610 * Create a CPU database entry for the host CPU. This means getting
611 * the CPUID bits from the real CPU and grabbing the closest matching
612 * database entry for MSRs.
613 */
614 rc = CPUMR3CpuIdDetectUnknownLeafMethod(&pInfo->enmUnknownCpuIdMethod, &pInfo->DefCpuId);
615 if (RT_FAILURE(rc))
616 return rc;
617 rc = CPUMR3CpuIdCollectLeaves(&pInfo->paCpuIdLeavesR3, &pInfo->cCpuIdLeaves);
618 if (RT_FAILURE(rc))
619 return rc;
620
621 /* Lookup database entry for MSRs. */
622 CPUMCPUVENDOR const enmVendor = CPUMR3CpuIdDetectVendorEx(pInfo->paCpuIdLeavesR3[0].uEax,
623 pInfo->paCpuIdLeavesR3[0].uEbx,
624 pInfo->paCpuIdLeavesR3[0].uEcx,
625 pInfo->paCpuIdLeavesR3[0].uEdx);
626 uint32_t const uStd1Eax = pInfo->paCpuIdLeavesR3[1].uEax;
627 uint8_t const uFamily = ASMGetCpuFamily(uStd1Eax);
628 uint8_t const uModel = ASMGetCpuModel(uStd1Eax, enmVendor == CPUMCPUVENDOR_INTEL);
629 uint8_t const uStepping = ASMGetCpuStepping(uStd1Eax);
630 CPUMMICROARCH const enmMicroarch = CPUMR3CpuIdDetermineMicroarchEx(enmVendor, uFamily, uModel, uStepping);
631
632 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
633 {
634 CPUMDBENTRY const *pCur = g_apCpumDbEntries[i];
635 if ((CPUMCPUVENDOR)pCur->enmVendor == enmVendor)
636 {
637 /* Match against Family, Microarch, model and stepping. Except
638 for family, always match the closer with preference given to
639 the later/older ones. */
640 if (pCur->uFamily == uFamily)
641 {
642 if (pCur->enmMicroarch == enmMicroarch)
643 {
644 if (pCur->uModel == uModel)
645 {
646 if (pCur->uStepping == uStepping)
647 {
648 /* Perfect match. */
649 pEntry = pCur;
650 break;
651 }
652
653 if ( !pEntry
654 || pEntry->uModel != uModel
655 || pEntry->enmMicroarch != enmMicroarch
656 || pEntry->uFamily != uFamily)
657 pEntry = pCur;
658 else if ( pCur->uStepping >= uStepping
659 ? pCur->uStepping < pEntry->uStepping || pEntry->uStepping < uStepping
660 : pCur->uStepping > pEntry->uStepping)
661 pEntry = pCur;
662 }
663 else if ( !pEntry
664 || pEntry->enmMicroarch != enmMicroarch
665 || pEntry->uFamily != uFamily)
666 pEntry = pCur;
667 else if ( pCur->uModel >= uModel
668 ? pCur->uModel < pEntry->uModel || pEntry->uModel < uModel
669 : pCur->uModel > pEntry->uModel)
670 pEntry = pCur;
671 }
672 else if ( !pEntry
673 || pEntry->uFamily != uFamily)
674 pEntry = pCur;
675 else if ( pCur->enmMicroarch >= enmMicroarch
676 ? pCur->enmMicroarch < pEntry->enmMicroarch || pEntry->enmMicroarch < enmMicroarch
677 : pCur->enmMicroarch > pEntry->enmMicroarch)
678 pEntry = pCur;
679 }
680 /* We don't do closeness matching on family, we use the first
681 entry for the CPU vendor instead. (P4 workaround.) */
682 else if (!pEntry)
683 pEntry = pCur;
684 }
685 }
686
687 if (pEntry)
688 LogRel(("CPUM: Matched host CPU %s %#x/%#x/%#x %s with CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
689 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
690 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor), pEntry->uFamily, pEntry->uModel,
691 pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
692 else
693 {
694 pEntry = g_apCpumDbEntries[0];
695 LogRel(("CPUM: No matching processor database entry %s %#x/%#x/%#x %s, falling back on '%s'\n",
696 CPUMR3CpuVendorName(enmVendor), uFamily, uModel, uStepping, CPUMR3MicroarchName(enmMicroarch),
697 pEntry->pszName));
698 }
699 }
700 else
701 {
702 /*
703 * We're supposed to be emulating a specific CPU that is included in
704 * our CPU database. The CPUID tables needs to be copied onto the
705 * heap so the caller can modify them and so they can be freed like
706 * in the host case above.
707 */
708 for (unsigned i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
709 if (!strcmp(pszName, g_apCpumDbEntries[i]->pszName))
710 {
711 pEntry = g_apCpumDbEntries[i];
712 break;
713 }
714 if (!pEntry)
715 {
716 LogRel(("CPUM: Cannot locate any CPU by the name '%s'\n", pszName));
717 return VERR_CPUM_DB_CPU_NOT_FOUND;
718 }
719
720 pInfo->cCpuIdLeaves = pEntry->cCpuIdLeaves;
721 if (pEntry->cCpuIdLeaves)
722 {
723 pInfo->paCpuIdLeavesR3 = (PCPUMCPUIDLEAF)RTMemDup(pEntry->paCpuIdLeaves,
724 sizeof(pEntry->paCpuIdLeaves[0]) * pEntry->cCpuIdLeaves);
725 if (!pInfo->paCpuIdLeavesR3)
726 return VERR_NO_MEMORY;
727 }
728 else
729 pInfo->paCpuIdLeavesR3 = NULL;
730
731 pInfo->enmUnknownCpuIdMethod = pEntry->enmUnknownCpuId;
732 pInfo->DefCpuId = pEntry->DefUnknownCpuId;
733
734 LogRel(("CPUM: Using CPU DB entry '%s' (%s %#x/%#x/%#x %s)\n",
735 pEntry->pszName, CPUMR3CpuVendorName((CPUMCPUVENDOR)pEntry->enmVendor),
736 pEntry->uFamily, pEntry->uModel, pEntry->uStepping, CPUMR3MicroarchName(pEntry->enmMicroarch) ));
737 }
738
739 pInfo->fMsrMask = pEntry->fMsrMask;
740 pInfo->iFirstExtCpuIdLeaf = 0; /* Set by caller. */
741 pInfo->uPadding = 0;
742 pInfo->uScalableBusFreq = pEntry->uScalableBusFreq;
743 pInfo->paCpuIdLeavesR0 = NIL_RTR0PTR;
744 pInfo->paMsrRangesR0 = NIL_RTR0PTR;
745 pInfo->paCpuIdLeavesRC = NIL_RTRCPTR;
746 pInfo->paMsrRangesRC = NIL_RTRCPTR;
747
748 /*
749 * Copy the MSR range.
750 */
751 uint32_t cMsrs = 0;
752 PCPUMMSRRANGE paMsrs = NULL;
753
754 PCCPUMMSRRANGE pCurMsr = pEntry->paMsrRanges;
755 uint32_t cLeft = pEntry->cMsrRanges;
756 while (cLeft-- > 0)
757 {
758 rc = cpumR3MsrRangesInsert(NULL /* pVM */, &paMsrs, &cMsrs, pCurMsr);
759 if (RT_FAILURE(rc))
760 {
761 Assert(!paMsrs); /* The above function frees this. */
762 RTMemFree(pInfo->paCpuIdLeavesR3);
763 pInfo->paCpuIdLeavesR3 = NULL;
764 return rc;
765 }
766 pCurMsr++;
767 }
768
769 pInfo->paMsrRangesR3 = paMsrs;
770 pInfo->cMsrRanges = cMsrs;
771 return VINF_SUCCESS;
772}
773
774
775/**
776 * Insert an MSR range into the VM.
777 *
778 * If the new MSR range overlaps existing ranges, the existing ones will be
779 * adjusted/removed to fit in the new one.
780 *
781 * @returns VBox status code.
782 * @param pVM The cross context VM structure.
783 * @param pNewRange Pointer to the MSR range being inserted.
784 */
785VMMR3DECL(int) CPUMR3MsrRangesInsert(PVM pVM, PCCPUMMSRRANGE pNewRange)
786{
787 AssertReturn(pVM, VERR_INVALID_PARAMETER);
788 AssertReturn(pNewRange, VERR_INVALID_PARAMETER);
789
790 return cpumR3MsrRangesInsert(pVM, NULL /* ppaMsrRanges */, NULL /* pcMsrRanges */, pNewRange);
791}
792
793
794/**
795 * Register statistics for the MSRs.
796 *
797 * This must not be called before the MSRs have been finalized and moved to the
798 * hyper heap.
799 *
800 * @returns VBox status code.
801 * @param pVM The cross context VM structure.
802 */
803int cpumR3MsrRegStats(PVM pVM)
804{
805 /*
806 * Global statistics.
807 */
808 PCPUM pCpum = &pVM->cpum.s;
809 STAM_REL_REG(pVM, &pCpum->cMsrReads, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Reads",
810 STAMUNIT_OCCURENCES, "All RDMSRs making it to CPUM.");
811 STAM_REL_REG(pVM, &pCpum->cMsrReadsRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsRaisingGP",
812 STAMUNIT_OCCURENCES, "RDMSR raising #GPs, except unknown MSRs.");
813 STAM_REL_REG(pVM, &pCpum->cMsrReadsUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/ReadsUnknown",
814 STAMUNIT_OCCURENCES, "RDMSR on unknown MSRs (raises #GP).");
815 STAM_REL_REG(pVM, &pCpum->cMsrWrites, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/Writes",
816 STAMUNIT_OCCURENCES, "All RDMSRs making it to CPUM.");
817 STAM_REL_REG(pVM, &pCpum->cMsrWritesRaiseGp, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesRaisingGP",
818 STAMUNIT_OCCURENCES, "WRMSR raising #GPs, except unknown MSRs.");
819 STAM_REL_REG(pVM, &pCpum->cMsrWritesToIgnoredBits, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesToIgnoredBits",
820 STAMUNIT_OCCURENCES, "Writing of ignored bits.");
821 STAM_REL_REG(pVM, &pCpum->cMsrWritesUnknown, STAMTYPE_COUNTER, "/CPUM/MSR-Totals/WritesUnknown",
822 STAMUNIT_OCCURENCES, "WRMSR on unknown MSRs (raises #GP).");
823
824
825# ifdef VBOX_WITH_STATISTICS
826 /*
827 * Per range.
828 */
829 PCPUMMSRRANGE paRanges = pVM->cpum.s.GuestInfo.paMsrRangesR3;
830 uint32_t cRanges = pVM->cpum.s.GuestInfo.cMsrRanges;
831 for (uint32_t i = 0; i < cRanges; i++)
832 {
833 char szName[160];
834 ssize_t cchName;
835
836 if (paRanges[i].uFirst == paRanges[i].uLast)
837 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%s",
838 paRanges[i].uFirst, paRanges[i].szName);
839 else
840 cchName = RTStrPrintf(szName, sizeof(szName), "/CPUM/MSRs/%#010x-%#010x-%s",
841 paRanges[i].uFirst, paRanges[i].uLast, paRanges[i].szName);
842
843 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-reads");
844 STAMR3Register(pVM, &paRanges[i].cReads, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RDMSR");
845
846 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-writes");
847 STAMR3Register(pVM, &paRanges[i].cWrites, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR");
848
849 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-GPs");
850 STAMR3Register(pVM, &paRanges[i].cGps, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "#GPs");
851
852 RTStrCopy(&szName[cchName], sizeof(szName) - cchName, "-ign-bits-writes");
853 STAMR3Register(pVM, &paRanges[i].cIgnoredBits, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "WRMSR w/ ignored bits");
854 }
855# endif /* VBOX_WITH_STATISTICS */
856
857 return VINF_SUCCESS;
858}
859
860#endif /* !CPUM_DB_STANDALONE */
861
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