VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/CPUMR3Db-armv8.cpp@ 102828

Last change on this file since 102828 was 99756, checked in by vboxsync, 20 months ago

VMM/CPUMR3Db: Fix harmless range check, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/* $Id: CPUMR3Db-armv8.cpp 99756 2023-05-11 19:26:52Z vboxsync $ */
2/** @file
3 * CPUM - CPU database part - ARMv8 specifics.
4 */
5
6/*
7 * Copyright (C) 2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_CPUM
33#include <VBox/vmm/cpum.h>
34#include "CPUMInternal-armv8.h"
35#include <VBox/vmm/vm.h>
36#include <VBox/vmm/mm.h>
37
38#include <iprt/errcore.h>
39#include <iprt/armv8.h>
40#include <iprt/mem.h>
41#include <iprt/string.h>
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47/** @def NULL_ALONE
48 * For eliminating an unnecessary data dependency in standalone builds (for
49 * VBoxSVC). */
50/** @def ZERO_ALONE
51 * For eliminating an unnecessary data size dependency in standalone builds (for
52 * VBoxSVC). */
53#ifndef CPUM_DB_STANDALONE
54# define NULL_ALONE(a_aTable) a_aTable
55# define ZERO_ALONE(a_cTable) a_cTable
56#else
57# define NULL_ALONE(a_aTable) NULL
58# define ZERO_ALONE(a_cTable) 0
59#endif
60
61
62#ifndef CPUM_DB_STANDALONE
63
64/**
65 * The database entries.
66 *
67 * 1. The first entry is special. It is the fallback for unknown
68 * processors. Thus, it better be pretty representative.
69 *
70 * 2. The first entry for a CPU vendor is likewise important as it is
71 * the default entry for that vendor.
72 *
73 * Generally we put the most recent CPUs first, since these tend to have the
74 * most complicated and backwards compatible list of MSRs.
75 */
76static CPUMDBENTRY const * const g_apCpumDbEntries[] =
77{
78 NULL
79};
80
81
82/**
83 * Returns the number of entries in the CPU database.
84 *
85 * @returns Number of entries.
86 * @sa PFNCPUMDBGETENTRIES
87 */
88VMMR3DECL(uint32_t) CPUMR3DbGetEntries(void)
89{
90 return RT_ELEMENTS(g_apCpumDbEntries);
91}
92
93
94/**
95 * Returns CPU database entry for the given index.
96 *
97 * @returns Pointer the CPU database entry, NULL if index is out of bounds.
98 * @param idxCpuDb The index (0..CPUMR3DbGetEntries).
99 * @sa PFNCPUMDBGETENTRYBYINDEX
100 */
101VMMR3DECL(PCCPUMDBENTRY) CPUMR3DbGetEntryByIndex(uint32_t idxCpuDb)
102{
103 AssertReturn(idxCpuDb < RT_ELEMENTS(g_apCpumDbEntries), NULL);
104 return g_apCpumDbEntries[idxCpuDb];
105}
106
107
108/**
109 * Returns CPU database entry with the given name.
110 *
111 * @returns Pointer the CPU database entry, NULL if not found.
112 * @param pszName The name of the profile to return.
113 * @sa PFNCPUMDBGETENTRYBYNAME
114 */
115VMMR3DECL(PCCPUMDBENTRY) CPUMR3DbGetEntryByName(const char *pszName)
116{
117 AssertPtrReturn(pszName, NULL);
118 AssertReturn(*pszName, NULL);
119 for (size_t i = 0; i < RT_ELEMENTS(g_apCpumDbEntries); i++)
120 if (strcmp(g_apCpumDbEntries[i]->pszName, pszName) == 0)
121 return g_apCpumDbEntries[i];
122 return NULL;
123}
124
125
126
127/**
128 * Binary search used by cpumR3SysRegRangesInsert and has some special properties
129 * wrt to mismatches.
130 *
131 * @returns Insert location.
132 * @param paSysRegRanges The system register ranges to search.
133 * @param cSysRegRanges The number of system register ranges.
134 * @param uSysReg What to search for.
135 */
136static uint32_t cpumR3SysRegRangesBinSearch(PCCPUMSYSREGRANGE paSysRegRanges, uint32_t cSysRegRanges, uint16_t uSysReg)
137{
138 if (!cSysRegRanges)
139 return 0;
140
141 uint32_t iStart = 0;
142 uint32_t iLast = cSysRegRanges - 1;
143 for (;;)
144 {
145 uint32_t i = iStart + (iLast - iStart + 1) / 2;
146 if ( uSysReg >= paSysRegRanges[i].uFirst
147 && uSysReg <= paSysRegRanges[i].uLast)
148 return i;
149 if (uSysReg < paSysRegRanges[i].uFirst)
150 {
151 if (i <= iStart)
152 return i;
153 iLast = i - 1;
154 }
155 else
156 {
157 if (i >= iLast)
158 {
159 if (i < cSysRegRanges)
160 i++;
161 return i;
162 }
163 iStart = i + 1;
164 }
165 }
166}
167
168
169/**
170 * Ensures that there is space for at least @a cNewRanges in the table,
171 * reallocating the table if necessary.
172 *
173 * @returns Pointer to the system register ranges on success, NULL on failure. On failure
174 * @a *ppaSysRegRanges is freed and set to NULL.
175 * @param pVM The cross context VM structure. If NULL,
176 * use the process heap, otherwise the VM's hyper heap.
177 * @param ppaSysRegRanges The variable pointing to the ranges (input/output).
178 * @param cSysRegRanges The current number of ranges.
179 * @param cNewRanges The number of ranges to be added.
180 */
181static PCPUMSYSREGRANGE cpumR3SysRegRangesEnsureSpace(PVM pVM, PCPUMSYSREGRANGE *ppaSysRegRanges, uint32_t cSysRegRanges, uint32_t cNewRanges)
182{
183 if ( cSysRegRanges + cNewRanges
184 > RT_ELEMENTS(pVM->cpum.s.GuestInfo.aSysRegRanges) + (pVM ? 0 : 128 /* Catch too many system registers in CPU reporter! */))
185 {
186 LogRel(("CPUM: Too many system register ranges! %#x, max %#x\n",
187 cSysRegRanges + cNewRanges, RT_ELEMENTS(pVM->cpum.s.GuestInfo.aSysRegRanges)));
188 return NULL;
189 }
190 if (pVM)
191 {
192 Assert(cSysRegRanges == pVM->cpum.s.GuestInfo.cSysRegRanges);
193 Assert(*ppaSysRegRanges == pVM->cpum.s.GuestInfo.aSysRegRanges);
194 }
195 else
196 {
197 if (cSysRegRanges + cNewRanges > RT_ALIGN_32(cSysRegRanges, 16))
198 {
199
200 uint32_t const cNew = RT_ALIGN_32(cSysRegRanges + cNewRanges, 16);
201 void *pvNew = RTMemRealloc(*ppaSysRegRanges, cNew * sizeof(**ppaSysRegRanges));
202 if (pvNew)
203 *ppaSysRegRanges = (PCPUMSYSREGRANGE)pvNew;
204 else
205 {
206 RTMemFree(*ppaSysRegRanges);
207 *ppaSysRegRanges = NULL;
208 return NULL;
209 }
210 }
211 }
212
213 return *ppaSysRegRanges;
214}
215
216
217/**
218 * Inserts a new system register range in into an sorted system register range array.
219 *
220 * If the new system register range overlaps existing ranges, the existing ones will be
221 * adjusted/removed to fit in the new one.
222 *
223 * @returns VBox status code.
224 * @retval VINF_SUCCESS
225 * @retval VERR_NO_MEMORY
226 *
227 * @param pVM The cross context VM structure. If NULL,
228 * use the process heap, otherwise the VM's hyper heap.
229 * @param ppaSysRegRanges The variable pointing to the ranges (input/output).
230 * Must be NULL if using the hyper heap.
231 * @param pcSysRegRanges The variable holding number of ranges. Must be NULL
232 * if using the hyper heap.
233 * @param pNewRange The new range.
234 */
235int cpumR3SysRegRangesInsert(PVM pVM, PCPUMSYSREGRANGE *ppaSysRegRanges, uint32_t *pcSysRegRanges, PCCPUMSYSREGRANGE pNewRange)
236{
237 Assert(pNewRange->uLast >= pNewRange->uFirst);
238 Assert(pNewRange->enmRdFn > kCpumSysRegRdFn_Invalid && pNewRange->enmRdFn < kCpumSysRegRdFn_End);
239 Assert(pNewRange->enmWrFn > kCpumSysRegWrFn_Invalid && pNewRange->enmWrFn < kCpumSysRegWrFn_End);
240
241 /*
242 * Validate and use the VM's system register ranges array if we are using the hyper heap.
243 */
244 if (pVM)
245 {
246 AssertReturn(!ppaSysRegRanges, VERR_INVALID_PARAMETER);
247 AssertReturn(!pcSysRegRanges, VERR_INVALID_PARAMETER);
248
249 ppaSysRegRanges = &pVM->cpum.s.GuestInfo.paSysRegRangesR3;
250 pcSysRegRanges = &pVM->cpum.s.GuestInfo.cSysRegRanges;
251 }
252 else
253 {
254 AssertReturn(ppaSysRegRanges, VERR_INVALID_POINTER);
255 AssertReturn(pcSysRegRanges, VERR_INVALID_POINTER);
256 }
257
258 uint32_t cSysRegRanges = *pcSysRegRanges;
259 PCPUMSYSREGRANGE paSysRegRanges = *ppaSysRegRanges;
260
261 /*
262 * Optimize the linear insertion case where we add new entries at the end.
263 */
264 if ( cSysRegRanges > 0
265 && paSysRegRanges[cSysRegRanges - 1].uLast < pNewRange->uFirst)
266 {
267 paSysRegRanges = cpumR3SysRegRangesEnsureSpace(pVM, ppaSysRegRanges, cSysRegRanges, 1);
268 if (!paSysRegRanges)
269 return VERR_NO_MEMORY;
270 paSysRegRanges[cSysRegRanges] = *pNewRange;
271 *pcSysRegRanges += 1;
272 }
273 else
274 {
275 uint32_t i = cpumR3SysRegRangesBinSearch(paSysRegRanges, cSysRegRanges, pNewRange->uFirst);
276 Assert(i == cSysRegRanges || pNewRange->uFirst <= paSysRegRanges[i].uLast);
277 Assert(i == 0 || pNewRange->uFirst > paSysRegRanges[i - 1].uLast);
278
279 /*
280 * Adding an entirely new entry?
281 */
282 if ( i >= cSysRegRanges
283 || pNewRange->uLast < paSysRegRanges[i].uFirst)
284 {
285 paSysRegRanges = cpumR3SysRegRangesEnsureSpace(pVM, ppaSysRegRanges, cSysRegRanges, 1);
286 if (!paSysRegRanges)
287 return VERR_NO_MEMORY;
288 if (i < cSysRegRanges)
289 memmove(&paSysRegRanges[i + 1], &paSysRegRanges[i], (cSysRegRanges - i) * sizeof(paSysRegRanges[0]));
290 paSysRegRanges[i] = *pNewRange;
291 *pcSysRegRanges += 1;
292 }
293 /*
294 * Replace existing entry?
295 */
296 else if ( pNewRange->uFirst == paSysRegRanges[i].uFirst
297 && pNewRange->uLast == paSysRegRanges[i].uLast)
298 paSysRegRanges[i] = *pNewRange;
299 /*
300 * Splitting an existing entry?
301 */
302 else if ( pNewRange->uFirst > paSysRegRanges[i].uFirst
303 && pNewRange->uLast < paSysRegRanges[i].uLast)
304 {
305 paSysRegRanges = cpumR3SysRegRangesEnsureSpace(pVM, ppaSysRegRanges, cSysRegRanges, 2);
306 if (!paSysRegRanges)
307 return VERR_NO_MEMORY;
308 if (i < cSysRegRanges)
309 memmove(&paSysRegRanges[i + 2], &paSysRegRanges[i], (cSysRegRanges - i) * sizeof(paSysRegRanges[0]));
310 paSysRegRanges[i + 1] = *pNewRange;
311 paSysRegRanges[i + 2] = paSysRegRanges[i];
312 paSysRegRanges[i ].uLast = pNewRange->uFirst - 1;
313 paSysRegRanges[i + 2].uFirst = pNewRange->uLast + 1;
314 *pcSysRegRanges += 2;
315 }
316 /*
317 * Complicated scenarios that can affect more than one range.
318 *
319 * The current code does not optimize memmove calls when replacing
320 * one or more existing ranges, because it's tedious to deal with and
321 * not expected to be a frequent usage scenario.
322 */
323 else
324 {
325 /* Adjust start of first match? */
326 if ( pNewRange->uFirst <= paSysRegRanges[i].uFirst
327 && pNewRange->uLast < paSysRegRanges[i].uLast)
328 paSysRegRanges[i].uFirst = pNewRange->uLast + 1;
329 else
330 {
331 /* Adjust end of first match? */
332 if (pNewRange->uFirst > paSysRegRanges[i].uFirst)
333 {
334 Assert(paSysRegRanges[i].uLast >= pNewRange->uFirst);
335 paSysRegRanges[i].uLast = pNewRange->uFirst - 1;
336 i++;
337 }
338 /* Replace the whole first match (lazy bird). */
339 else
340 {
341 if (i + 1 < cSysRegRanges)
342 memmove(&paSysRegRanges[i], &paSysRegRanges[i + 1], (cSysRegRanges - i - 1) * sizeof(paSysRegRanges[0]));
343 cSysRegRanges = *pcSysRegRanges -= 1;
344 }
345
346 /* Do the new range affect more ranges? */
347 while ( i < cSysRegRanges
348 && pNewRange->uLast >= paSysRegRanges[i].uFirst)
349 {
350 if (pNewRange->uLast < paSysRegRanges[i].uLast)
351 {
352 /* Adjust the start of it, then we're done. */
353 paSysRegRanges[i].uFirst = pNewRange->uLast + 1;
354 break;
355 }
356
357 /* Remove it entirely. */
358 if (i + 1 < cSysRegRanges)
359 memmove(&paSysRegRanges[i], &paSysRegRanges[i + 1], (cSysRegRanges - i - 1) * sizeof(paSysRegRanges[0]));
360 cSysRegRanges = *pcSysRegRanges -= 1;
361 }
362 }
363
364 /* Now, perform a normal insertion. */
365 paSysRegRanges = cpumR3SysRegRangesEnsureSpace(pVM, ppaSysRegRanges, cSysRegRanges, 1);
366 if (!paSysRegRanges)
367 return VERR_NO_MEMORY;
368 if (i < cSysRegRanges)
369 memmove(&paSysRegRanges[i + 1], &paSysRegRanges[i], (cSysRegRanges - i) * sizeof(paSysRegRanges[0]));
370 paSysRegRanges[i] = *pNewRange;
371 *pcSysRegRanges += 1;
372 }
373 }
374
375 return VINF_SUCCESS;
376}
377
378
379/**
380 * Insert an system register range into the VM.
381 *
382 * If the new system register range overlaps existing ranges, the existing ones will be
383 * adjusted/removed to fit in the new one.
384 *
385 * @returns VBox status code.
386 * @param pVM The cross context VM structure.
387 * @param pNewRange Pointer to the MSR range being inserted.
388 */
389VMMR3DECL(int) CPUMR3SysRegRangesInsert(PVM pVM, PCCPUMSYSREGRANGE pNewRange)
390{
391 AssertReturn(pVM, VERR_INVALID_PARAMETER);
392 AssertReturn(pNewRange, VERR_INVALID_PARAMETER);
393
394 return cpumR3SysRegRangesInsert(pVM, NULL /* ppaSysRegRanges */, NULL /* pcSysRegRanges */, pNewRange);
395}
396
397#endif /* !CPUM_DB_STANDALONE */
398
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