VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFReg.cpp@ 107763

最後變更 在這個檔案從107763是 107763,由 vboxsync 提交於 2 月 前

VMM/CPUM,DBGF: Register IA32_ARCH_CAPABILITIES and IA32_SPEC_CTRL as CPU registers with DBGF. jiraref:VBP-947

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 116.6 KB
 
1/* $Id: DBGFReg.cpp 107763 2025-01-14 16:12:57Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Register Methods.
4 */
5
6/*
7 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.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_DBGF
33#include <VBox/vmm/dbgf.h>
34#include "DBGFInternal.h"
35#include <VBox/vmm/mm.h>
36#include <VBox/vmm/vm.h>
37#include <VBox/vmm/uvm.h>
38#include <VBox/param.h>
39#include <VBox/err.h>
40#include <VBox/log.h>
41#include <iprt/ctype.h>
42#include <iprt/string.h>
43#include <iprt/uint128.h>
44
45
46/*********************************************************************************************************************************
47* Defined Constants And Macros *
48*********************************************************************************************************************************/
49/** Locks the register database for writing. */
50#define DBGF_REG_DB_LOCK_WRITE(pUVM) \
51 do { \
52 int rcSem = RTSemRWRequestWrite((pUVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
53 AssertRC(rcSem); \
54 } while (0)
55
56/** Unlocks the register database after writing. */
57#define DBGF_REG_DB_UNLOCK_WRITE(pUVM) \
58 do { \
59 int rcSem = RTSemRWReleaseWrite((pUVM)->dbgf.s.hRegDbLock); \
60 AssertRC(rcSem); \
61 } while (0)
62
63/** Locks the register database for reading. */
64#define DBGF_REG_DB_LOCK_READ(pUVM) \
65 do { \
66 int rcSem = RTSemRWRequestRead((pUVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
67 AssertRC(rcSem); \
68 } while (0)
69
70/** Unlocks the register database after reading. */
71#define DBGF_REG_DB_UNLOCK_READ(pUVM) \
72 do { \
73 int rcSem = RTSemRWReleaseRead((pUVM)->dbgf.s.hRegDbLock); \
74 AssertRC(rcSem); \
75 } while (0)
76
77
78/** The max length of a set, register or sub-field name. */
79#define DBGF_REG_MAX_NAME 40
80
81
82/*********************************************************************************************************************************
83* Structures and Typedefs *
84*********************************************************************************************************************************/
85/**
86 * Register set registration record type.
87 */
88typedef enum DBGFREGSETTYPE
89{
90 /** Invalid zero value. */
91 DBGFREGSETTYPE_INVALID = 0,
92 /** CPU record. */
93 DBGFREGSETTYPE_CPU,
94 /** Device record. */
95 DBGFREGSETTYPE_DEVICE,
96 /** End of valid record types. */
97 DBGFREGSETTYPE_END
98} DBGFREGSETTYPE;
99
100
101/**
102 * Register set registration record.
103 */
104typedef struct DBGFREGSET
105{
106 /** String space core. */
107 RTSTRSPACECORE Core;
108 /** The registration record type. */
109 DBGFREGSETTYPE enmType;
110 /** The user argument for the callbacks. */
111 union
112 {
113 /** The CPU view. */
114 PVMCPU pVCpu;
115 /** The device view. */
116 PPDMDEVINS pDevIns;
117 /** The general view. */
118 void *pv;
119 } uUserArg;
120
121 /** The register descriptors. */
122 PCDBGFREGDESC paDescs;
123 /** The number of register descriptors. */
124 uint32_t cDescs;
125
126 /** Array of lookup records.
127 * The first part of the array runs parallel to paDescs, the rest are
128 * covering for aliases and bitfield variations. It's done this way to
129 * simplify the query all operations. */
130 struct DBGFREGLOOKUP *paLookupRecs;
131 /** The number of lookup records. */
132 uint32_t cLookupRecs;
133
134 /** The register name prefix. */
135 char szPrefix[1];
136} DBGFREGSET;
137/** Pointer to a register registration record. */
138typedef DBGFREGSET *PDBGFREGSET;
139/** Pointer to a const register registration record. */
140typedef DBGFREGSET const *PCDBGFREGSET;
141
142
143/**
144 * Register lookup record.
145 */
146typedef struct DBGFREGLOOKUP
147{
148 /** The string space core. */
149 RTSTRSPACECORE Core;
150 /** Pointer to the set. */
151 PCDBGFREGSET pSet;
152 /** Pointer to the register descriptor. */
153 PCDBGFREGDESC pDesc;
154 /** If an alias this points to the alias descriptor, NULL if not. */
155 PCDBGFREGALIAS pAlias;
156 /** If a sub-field this points to the sub-field descriptor, NULL if not. */
157 PCDBGFREGSUBFIELD pSubField;
158} DBGFREGLOOKUP;
159/** Pointer to a register lookup record. */
160typedef DBGFREGLOOKUP *PDBGFREGLOOKUP;
161/** Pointer to a const register lookup record. */
162typedef DBGFREGLOOKUP const *PCDBGFREGLOOKUP;
163
164
165/**
166 * Argument packet from DBGFR3RegNmQueryAll to dbgfR3RegNmQueryAllWorker.
167 */
168typedef struct DBGFR3REGNMQUERYALLARGS
169{
170 /** The output register array. */
171 PDBGFREGENTRYNM paRegs;
172 /** The number of entries in the output array. */
173 size_t cRegs;
174 /** The current register number when enumerating the string space.
175 * @remarks Only used by EMT(0). */
176 size_t iReg;
177} DBGFR3REGNMQUERYALLARGS;
178/** Pointer to a dbgfR3RegNmQueryAllWorker argument packet. */
179typedef DBGFR3REGNMQUERYALLARGS *PDBGFR3REGNMQUERYALLARGS;
180
181
182/**
183 * Argument packet passed by DBGFR3RegPrintfV to dbgfR3RegPrintfCbOutput and
184 * dbgfR3RegPrintfCbFormat.
185 */
186typedef struct DBGFR3REGPRINTFARGS
187{
188 /** The user mode VM handle. */
189 PUVM pUVM;
190 /** The target CPU. */
191 VMCPUID idCpu;
192 /** Set if we're looking at guest registers. */
193 bool fGuestRegs;
194 /** The output buffer. */
195 char *pszBuf;
196 /** The format string. */
197 const char *pszFormat;
198 /** The va list with format arguments. */
199 va_list va;
200
201 /** The current buffer offset. */
202 size_t offBuf;
203 /** The amount of buffer space left, not counting the terminator char. */
204 size_t cchLeftBuf;
205 /** The status code of the whole operation. First error is return,
206 * subsequent ones are suppressed. */
207 int rc;
208} DBGFR3REGPRINTFARGS;
209/** Pointer to a DBGFR3RegPrintfV argument packet. */
210typedef DBGFR3REGPRINTFARGS *PDBGFR3REGPRINTFARGS;
211
212
213
214/**
215 * Initializes the register database.
216 *
217 * @returns VBox status code.
218 * @param pUVM The user mode VM handle.
219 */
220int dbgfR3RegInit(PUVM pUVM)
221{
222 int rc = VINF_SUCCESS;
223 if (!pUVM->dbgf.s.fRegDbInitialized)
224 {
225 rc = RTSemRWCreate(&pUVM->dbgf.s.hRegDbLock);
226 pUVM->dbgf.s.fRegDbInitialized = RT_SUCCESS(rc);
227 }
228 return rc;
229}
230
231
232/**
233 * Terminates the register database.
234 *
235 * @param pUVM The user mode VM handle.
236 */
237void dbgfR3RegTerm(PUVM pUVM)
238{
239 RTSemRWDestroy(pUVM->dbgf.s.hRegDbLock);
240 pUVM->dbgf.s.hRegDbLock = NIL_RTSEMRW;
241 pUVM->dbgf.s.fRegDbInitialized = false;
242}
243
244
245/**
246 * Validates a register name.
247 *
248 * This is used for prefixes, aliases and field names.
249 *
250 * @returns true if valid, false if not.
251 * @param pszName The register name to validate.
252 * @param chDot Set to '.' if accepted, otherwise 0.
253 */
254static bool dbgfR3RegIsNameValid(const char *pszName, char chDot)
255{
256 const char *psz = pszName;
257 if (!RT_C_IS_ALPHA(*psz))
258 return false;
259 char ch;
260 while ((ch = *++psz))
261 if ( !RT_C_IS_LOWER(ch)
262 && !RT_C_IS_DIGIT(ch)
263 && ch != '_'
264 && ch != chDot)
265 return false;
266 if (psz - pszName > DBGF_REG_MAX_NAME)
267 return false;
268 return true;
269}
270
271
272/**
273 * Common worker for registering a register set.
274 *
275 * @returns VBox status code.
276 * @param pUVM The user mode VM handle.
277 * @param paRegisters The register descriptors.
278 * @param enmType The set type.
279 * @param pvUserArg The user argument for the callbacks.
280 * @param pszPrefix The name prefix.
281 * @param iInstance The instance number to be appended to @a
282 * pszPrefix when creating the set name.
283 */
284static int dbgfR3RegRegisterCommon(PUVM pUVM, PCDBGFREGDESC paRegisters, DBGFREGSETTYPE enmType, void *pvUserArg,
285 const char *pszPrefix, uint32_t iInstance)
286{
287 /*
288 * Validate input.
289 */
290 /* The name components. */
291 AssertMsgReturn(dbgfR3RegIsNameValid(pszPrefix, 0), ("%s\n", pszPrefix), VERR_INVALID_NAME);
292 const char *psz = RTStrEnd(pszPrefix, RTSTR_MAX);
293 bool const fNeedUnderscore = RT_C_IS_DIGIT(psz[-1]);
294 size_t const cchPrefix = psz - pszPrefix + fNeedUnderscore;
295 AssertMsgReturn(cchPrefix < RT_SIZEOFMEMB(DBGFREGSET, szPrefix) - 4 - 1, ("%s\n", pszPrefix), VERR_INVALID_NAME);
296
297 AssertMsgReturn(iInstance <= 9999, ("%d\n", iInstance), VERR_INVALID_NAME);
298
299 /* The descriptors. */
300#ifdef VBOX_VMM_TARGET_X86
301 DBGFREG const enmCpuFirst = DBGFREG_X86_FIRST;
302 DBGFREG const enmCpuLast = DBGFREG_X86_LAST;
303#elif defined(VBOX_VMM_TARGET_ARMV8)
304 DBGFREG const enmCpuFirst = DBGFREG_ARMV8_FIRST;
305 DBGFREG const enmCpuLast = DBGFREG_ARMV8_LAST;
306#else
307# error "port me"
308#endif
309 unsigned const cCpuDescs = (unsigned)enmCpuLast - (unsigned)enmCpuFirst + 1;
310 uint32_t cLookupRecs = 0;
311 uint32_t iDesc;
312 for (iDesc = 0; paRegisters[iDesc].pszName != NULL; iDesc++)
313 {
314 AssertMsgReturn(dbgfR3RegIsNameValid(paRegisters[iDesc].pszName, 0), ("%s (#%u)\n", paRegisters[iDesc].pszName, iDesc), VERR_INVALID_NAME);
315
316 if (enmType == DBGFREGSETTYPE_CPU) /* The CPU descriptors must be in enum order. */
317 AssertMsgReturn(iDesc < cCpuDescs && (unsigned)paRegisters[iDesc].enmReg == iDesc + (unsigned)enmCpuFirst,
318 ("%d iDesc=%u+%d=%u\n", paRegisters[iDesc].enmReg, iDesc, enmCpuFirst, iDesc + (unsigned)enmCpuFirst),
319 VERR_INVALID_PARAMETER);
320 else
321 AssertReturn(paRegisters[iDesc].enmReg == DBGFREG_END, VERR_INVALID_PARAMETER);
322 AssertReturn( paRegisters[iDesc].enmType > DBGFREGVALTYPE_INVALID
323 && paRegisters[iDesc].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
324 AssertMsgReturn(!(paRegisters[iDesc].fFlags & ~DBGFREG_FLAGS_READ_ONLY),
325 ("%#x (#%u)\n", paRegisters[iDesc].fFlags, iDesc),
326 VERR_INVALID_PARAMETER);
327 AssertPtrReturn(paRegisters[iDesc].pfnGet, VERR_INVALID_PARAMETER);
328 AssertReturn(RT_VALID_PTR(paRegisters[iDesc].pfnSet) || (paRegisters[iDesc].fFlags & DBGFREG_FLAGS_READ_ONLY),
329 VERR_INVALID_PARAMETER);
330
331 uint32_t iAlias = 0;
332 PCDBGFREGALIAS paAliases = paRegisters[iDesc].paAliases;
333 if (paAliases)
334 {
335 AssertPtrReturn(paAliases, VERR_INVALID_PARAMETER);
336 for (; paAliases[iAlias].pszName; iAlias++)
337 {
338 AssertMsgReturn(dbgfR3RegIsNameValid(paAliases[iAlias].pszName, 0), ("%s (%s)\n", paAliases[iAlias].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
339 AssertReturn( paAliases[iAlias].enmType > DBGFREGVALTYPE_INVALID
340 && paAliases[iAlias].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
341 }
342 }
343
344 uint32_t iSubField = 0;
345 PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
346 if (paSubFields)
347 {
348 AssertPtrReturn(paSubFields, VERR_INVALID_PARAMETER);
349 for (; paSubFields[iSubField].pszName; iSubField++)
350 {
351 AssertMsgReturn(dbgfR3RegIsNameValid(paSubFields[iSubField].pszName, '.'), ("%s (%s)\n", paSubFields[iSubField].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
352 AssertReturn(paSubFields[iSubField].iFirstBit + paSubFields[iSubField].cBits <= 128, VERR_INVALID_PARAMETER);
353 AssertReturn(paSubFields[iSubField].cBits + paSubFields[iSubField].cShift <= 128, VERR_INVALID_PARAMETER);
354 AssertPtrNullReturn(paSubFields[iSubField].pfnGet, VERR_INVALID_POINTER);
355 AssertPtrNullReturn(paSubFields[iSubField].pfnSet, VERR_INVALID_POINTER);
356 }
357 }
358
359 cLookupRecs += (1 + iAlias) * (1 + iSubField);
360 }
361
362 /* Check the instance number of the CPUs. */
363 AssertReturn(enmType != DBGFREGSETTYPE_CPU || iInstance < pUVM->cCpus, VERR_INVALID_CPU_ID);
364
365 /*
366 * Allocate a new record and all associated lookup records.
367 */
368 size_t cbRegSet = RT_UOFFSETOF_DYN(DBGFREGSET, szPrefix[cchPrefix + 4 + 1]);
369 cbRegSet = RT_ALIGN_Z(cbRegSet, 32);
370 size_t const offLookupRecArray = cbRegSet;
371 cbRegSet += cLookupRecs * sizeof(DBGFREGLOOKUP);
372
373 PDBGFREGSET pRegSet = (PDBGFREGSET)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_REG, cbRegSet);
374 if (!pRegSet)
375 return VERR_NO_MEMORY;
376
377 /*
378 * Initialize the new record.
379 */
380 pRegSet->Core.pszString = pRegSet->szPrefix;
381 pRegSet->enmType = enmType;
382 pRegSet->uUserArg.pv = pvUserArg;
383 pRegSet->paDescs = paRegisters;
384 pRegSet->cDescs = iDesc;
385 pRegSet->cLookupRecs = cLookupRecs;
386 pRegSet->paLookupRecs = (PDBGFREGLOOKUP)((uintptr_t)pRegSet + offLookupRecArray);
387 if (fNeedUnderscore)
388 RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s_%u", pszPrefix, iInstance);
389 else
390 RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s%u", pszPrefix, iInstance);
391
392 /*
393 * Initialize the lookup records. See DBGFREGSET::paLookupRecs.
394 */
395 char szName[DBGF_REG_MAX_NAME * 3 + 16];
396 strcpy(szName, pRegSet->szPrefix);
397 char *pszReg = strchr(szName, '\0');
398 *pszReg++ = '.';
399
400 /* Array parallel to the descriptors. */
401 int rc = VINF_SUCCESS;
402 PDBGFREGLOOKUP pLookupRec = &pRegSet->paLookupRecs[0];
403 for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
404 {
405 strcpy(pszReg, paRegisters[iDesc].pszName);
406 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
407 if (!pLookupRec->Core.pszString)
408 rc = VERR_NO_STR_MEMORY;
409 pLookupRec->pSet = pRegSet;
410 pLookupRec->pDesc = &paRegisters[iDesc];
411 pLookupRec->pAlias = NULL;
412 pLookupRec->pSubField = NULL;
413 pLookupRec++;
414 }
415
416 /* Aliases and sub-fields. */
417 for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
418 {
419 PCDBGFREGALIAS pCurAlias = NULL; /* first time we add sub-fields for the real name. */
420 PCDBGFREGALIAS pNextAlias = paRegisters[iDesc].paAliases;
421 const char *pszRegName = paRegisters[iDesc].pszName;
422 while (RT_SUCCESS(rc))
423 {
424 /* Add sub-field records. */
425 PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
426 if (paSubFields)
427 {
428 size_t cchReg = strlen(pszRegName);
429 memcpy(pszReg, pszRegName, cchReg);
430 char *pszSub = &pszReg[cchReg];
431 *pszSub++ = '.';
432 for (uint32_t iSubField = 0; paSubFields[iSubField].pszName && RT_SUCCESS(rc); iSubField++)
433 {
434 strcpy(pszSub, paSubFields[iSubField].pszName);
435 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
436 if (!pLookupRec->Core.pszString)
437 rc = VERR_NO_STR_MEMORY;
438 pLookupRec->pSet = pRegSet;
439 pLookupRec->pDesc = &paRegisters[iDesc];
440 pLookupRec->pAlias = pCurAlias;
441 pLookupRec->pSubField = &paSubFields[iSubField];
442 pLookupRec++;
443 }
444 }
445
446 /* Advance to the next alias. */
447 if (pNextAlias)
448 pCurAlias = pNextAlias++;
449 if (!pCurAlias)
450 break;
451 pszRegName = pCurAlias->pszName;
452 if (!pszRegName)
453 break;
454
455 /* The alias record. */
456 strcpy(pszReg, pszRegName);
457 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
458 if (!pLookupRec->Core.pszString)
459 rc = VERR_NO_STR_MEMORY;
460 pLookupRec->pSet = pRegSet;
461 pLookupRec->pDesc = &paRegisters[iDesc];
462 pLookupRec->pAlias = pCurAlias;
463 pLookupRec->pSubField = NULL;
464 pLookupRec++;
465 }
466 }
467 Assert(pLookupRec == &pRegSet->paLookupRecs[pRegSet->cLookupRecs]);
468
469 if (RT_SUCCESS(rc))
470 {
471 /*
472 * Insert the record into the register set string space and optionally into
473 * the CPU register set cache.
474 */
475 DBGF_REG_DB_LOCK_WRITE(pUVM);
476
477 bool fInserted = RTStrSpaceInsert(&pUVM->dbgf.s.RegSetSpace, &pRegSet->Core);
478 if (fInserted)
479 {
480 pUVM->dbgf.s.cRegs += pRegSet->cDescs;
481 if (enmType == DBGFREGSETTYPE_CPU)
482 {
483 if (!strcmp(pszPrefix, "cpu"))
484 {
485 if (!pUVM->dbgf.s.cPerCpuRegs)
486 pUVM->dbgf.s.cPerCpuRegs = pRegSet->cDescs;
487 else
488 AssertLogRelMsgStmt(pUVM->dbgf.s.cPerCpuRegs == pRegSet->cDescs,
489 ("%d vs %d\n", pUVM->dbgf.s.cPerCpuRegs, pRegSet->cDescs),
490 pUVM->dbgf.s.cPerCpuRegs = RT_MAX(pRegSet->cDescs, pUVM->dbgf.s.cPerCpuRegs));
491 pUVM->aCpus[iInstance].dbgf.s.pGuestRegSet = pRegSet;
492 }
493 else
494 {
495 Assert(!strcmp(pszPrefix, "hypercpu"));
496 if (!pUVM->dbgf.s.cPerCpuHyperRegs)
497 pUVM->dbgf.s.cPerCpuHyperRegs = pRegSet->cDescs;
498 else
499 AssertLogRelMsgStmt(pUVM->dbgf.s.cPerCpuHyperRegs == pRegSet->cDescs,
500 ("%d vs %d\n", pUVM->dbgf.s.cPerCpuHyperRegs, pRegSet->cDescs),
501 pUVM->dbgf.s.cPerCpuHyperRegs = RT_MAX(pRegSet->cDescs, pUVM->dbgf.s.cPerCpuHyperRegs));
502 pUVM->aCpus[iInstance].dbgf.s.pHyperRegSet = pRegSet;
503 }
504 }
505
506 PDBGFREGLOOKUP paLookupRecs = pRegSet->paLookupRecs;
507 uint32_t iLookupRec = pRegSet->cLookupRecs;
508 while (iLookupRec-- > 0)
509 {
510 bool fInserted2 = RTStrSpaceInsert(&pUVM->dbgf.s.RegSpace, &paLookupRecs[iLookupRec].Core);
511 AssertMsg(fInserted2, ("'%s'", paLookupRecs[iLookupRec].Core.pszString)); NOREF(fInserted2);
512 }
513
514 DBGF_REG_DB_UNLOCK_WRITE(pUVM);
515 return VINF_SUCCESS;
516 }
517
518 DBGF_REG_DB_UNLOCK_WRITE(pUVM);
519 rc = VERR_DUPLICATE;
520 }
521
522 /*
523 * Bail out.
524 */
525 for (uint32_t i = 0; i < pRegSet->cLookupRecs; i++)
526 MMR3HeapFree((char *)pRegSet->paLookupRecs[i].Core.pszString);
527 MMR3HeapFree(pRegSet);
528
529 return rc;
530}
531
532
533/**
534 * Registers a set of registers for a CPU.
535 *
536 * @returns VBox status code.
537 * @param pVM The cross context VM structure.
538 * @param pVCpu The cross context virtual CPU structure.
539 * @param paRegisters The register descriptors.
540 * @param fGuestRegs Set if it's the guest registers, clear if
541 * hypervisor registers.
542 */
543VMMR3_INT_DECL(int) DBGFR3RegRegisterCpu(PVM pVM, PVMCPU pVCpu, PCDBGFREGDESC paRegisters, bool fGuestRegs)
544{
545 PUVM pUVM = pVM->pUVM;
546 if (!pUVM->dbgf.s.fRegDbInitialized)
547 {
548 int rc = dbgfR3RegInit(pUVM);
549 if (RT_FAILURE(rc))
550 return rc;
551 }
552
553 AssertReturn(fGuestRegs, VERR_RAW_MODE_NOT_SUPPORTED);
554 return dbgfR3RegRegisterCommon(pUVM, paRegisters, DBGFREGSETTYPE_CPU, pVCpu, fGuestRegs ? "cpu" : "hypercpu", pVCpu->idCpu);
555}
556
557
558/**
559 * Registers a set of registers for a device.
560 *
561 * @returns VBox status code.
562 * @param pVM The cross context VM structure.
563 * @param paRegisters The register descriptors.
564 * @param pDevIns The device instance. This will be the callback user
565 * argument.
566 * @param pszPrefix The device name.
567 * @param iInstance The device instance.
568 */
569VMMR3_INT_DECL(int) DBGFR3RegRegisterDevice(PVM pVM, PCDBGFREGDESC paRegisters, PPDMDEVINS pDevIns,
570 const char *pszPrefix, uint32_t iInstance)
571{
572 AssertPtrReturn(paRegisters, VERR_INVALID_POINTER);
573 AssertPtrReturn(pDevIns, VERR_INVALID_POINTER);
574 AssertPtrReturn(pszPrefix, VERR_INVALID_POINTER);
575
576 return dbgfR3RegRegisterCommon(pVM->pUVM, paRegisters, DBGFREGSETTYPE_DEVICE, pDevIns, pszPrefix, iInstance);
577}
578
579
580/**
581 * Clears the register value variable.
582 *
583 * @param pValue The variable to clear.
584 */
585DECLINLINE(void) dbgfR3RegValClear(PDBGFREGVAL pValue)
586{
587 pValue->au64[0] = 0;
588 pValue->au64[1] = 0;
589 pValue->au64[2] = 0;
590 pValue->au64[3] = 0;
591 pValue->au64[4] = 0;
592 pValue->au64[5] = 0;
593 pValue->au64[6] = 0;
594 pValue->au64[7] = 0;
595}
596
597
598/**
599 * Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
600 *
601 * @param pValue The value.
602 * @param u64 The integer value.
603 */
604DECLINLINE(void) dbgfR3RegValR80SetU64(PDBGFREGVAL pValue, uint64_t u64)
605{
606 /** @todo fixme */
607 pValue->r80.s.fSign = 0;
608 pValue->r80.s.uExponent = 16383;
609 pValue->r80.s.uMantissa = u64;
610}
611
612
613/**
614 * Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
615 *
616 * @param pValue The value.
617 * @param u128 The integer value.
618 */
619DECLINLINE(void) dbgfR3RegValR80SetU128(PDBGFREGVAL pValue, RTUINT128U u128)
620{
621 /** @todo fixme */
622 pValue->r80.s.fSign = 0;
623 pValue->r80.s.uExponent = 16383;
624 pValue->r80.s.uMantissa = u128.s.Lo;
625}
626
627
628/**
629 * Get a 80-bit floating point variable as a 64-bit unsigned integer.
630 *
631 * @returns 64-bit unsigned integer.
632 * @param pValue The value.
633 */
634DECLINLINE(uint64_t) dbgfR3RegValR80GetU64(PCDBGFREGVAL pValue)
635{
636 /** @todo stupid, stupid MSC. */
637 return pValue->r80.s.uMantissa;
638}
639
640
641/**
642 * Get a 80-bit floating point variable as a 128-bit unsigned integer.
643 *
644 * @returns 128-bit unsigned integer.
645 * @param pValue The value.
646 */
647DECLINLINE(RTUINT128U) dbgfR3RegValR80GetU128(PCDBGFREGVAL pValue)
648{
649 /** @todo stupid, stupid MSC. */
650 RTUINT128U uRet;
651#if 0
652 uRet.s.Lo = (uint64_t)InVal.lrd;
653 uRet.s.Hi = (uint64_t)InVal.lrd / _4G / _4G;
654#else
655 uRet.s.Lo = pValue->r80.s.uMantissa;
656 uRet.s.Hi = 0;
657#endif
658 return uRet;
659}
660
661
662/**
663 * Performs a cast between register value types.
664 *
665 * @retval VINF_SUCCESS
666 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
667 * @retval VINF_DBGF_TRUNCATED_REGISTER
668 * @retval VERR_DBGF_UNSUPPORTED_CAST
669 *
670 * @param pValue The value to cast (input + output).
671 * @param enmFromType The input value.
672 * @param enmToType The desired output value.
673 */
674static int dbgfR3RegValCast(PDBGFREGVAL pValue, DBGFREGVALTYPE enmFromType, DBGFREGVALTYPE enmToType)
675{
676 DBGFREGVAL const InVal = *pValue;
677 dbgfR3RegValClear(pValue);
678
679 /* Note! No default cases here as gcc warnings about missing enum values
680 are desired. */
681 switch (enmFromType)
682 {
683 case DBGFREGVALTYPE_U8:
684 switch (enmToType)
685 {
686 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u8; return VINF_SUCCESS;
687 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
688 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
689 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
690 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
691 case DBGFREGVALTYPE_U256: pValue->u256.Words.w0 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
692 case DBGFREGVALTYPE_U512: pValue->u512.Words.w0 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
693 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u8); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
694 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
695
696 case DBGFREGVALTYPE_32BIT_HACK:
697 case DBGFREGVALTYPE_END:
698 case DBGFREGVALTYPE_INVALID:
699 break;
700 }
701 break;
702
703 case DBGFREGVALTYPE_U16:
704 switch (enmToType)
705 {
706 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u16; return VINF_DBGF_TRUNCATED_REGISTER;
707 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u16; return VINF_SUCCESS;
708 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
709 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
710 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
711 case DBGFREGVALTYPE_U256: pValue->u256.Words.w0 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
712 case DBGFREGVALTYPE_U512: pValue->u512.Words.w0 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
713 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u16); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
714 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
715
716 case DBGFREGVALTYPE_32BIT_HACK:
717 case DBGFREGVALTYPE_END:
718 case DBGFREGVALTYPE_INVALID:
719 break;
720 }
721 break;
722
723 case DBGFREGVALTYPE_U32:
724 switch (enmToType)
725 {
726 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
727 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
728 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u32; return VINF_SUCCESS;
729 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
730 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
731 case DBGFREGVALTYPE_U256: pValue->u256.DWords.dw0 = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
732 case DBGFREGVALTYPE_U512: pValue->u512.DWords.dw0 = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
733 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u32); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
734 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
735
736 case DBGFREGVALTYPE_32BIT_HACK:
737 case DBGFREGVALTYPE_END:
738 case DBGFREGVALTYPE_INVALID:
739 break;
740 }
741 break;
742
743 case DBGFREGVALTYPE_U64:
744 switch (enmToType)
745 {
746 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
747 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
748 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
749 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u64; return VINF_SUCCESS;
750 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
751 case DBGFREGVALTYPE_U256: pValue->u256.QWords.qw0 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
752 case DBGFREGVALTYPE_U512: pValue->u512.QWords.qw0 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
753 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u64); return VINF_DBGF_TRUNCATED_REGISTER;
754 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
755
756 case DBGFREGVALTYPE_32BIT_HACK:
757 case DBGFREGVALTYPE_END:
758 case DBGFREGVALTYPE_INVALID:
759 break;
760 }
761 break;
762
763 case DBGFREGVALTYPE_U128:
764 switch (enmToType)
765 {
766 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
767 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
768 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
769 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
770 case DBGFREGVALTYPE_U128: pValue->u128 = InVal.u128; return VINF_SUCCESS;
771 case DBGFREGVALTYPE_U256: pValue->u256.DQWords.dqw0 = InVal.u128; return VINF_SUCCESS;
772 case DBGFREGVALTYPE_U512: pValue->u512.DQWords.dqw0 = InVal.u128; return VINF_SUCCESS;
773 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU128(pValue, InVal.u128); return VINF_DBGF_TRUNCATED_REGISTER;
774 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
775
776 case DBGFREGVALTYPE_32BIT_HACK:
777 case DBGFREGVALTYPE_END:
778 case DBGFREGVALTYPE_INVALID:
779 break;
780 }
781 break;
782
783 case DBGFREGVALTYPE_U256:
784 switch (enmToType)
785 {
786 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u256.Words.w0; return VINF_DBGF_TRUNCATED_REGISTER;
787 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u256.Words.w0; return VINF_DBGF_TRUNCATED_REGISTER;
788 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u256.DWords.dw0; return VINF_DBGF_TRUNCATED_REGISTER;
789 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u256.QWords.qw0; return VINF_DBGF_TRUNCATED_REGISTER;
790 case DBGFREGVALTYPE_U128: pValue->u128 = InVal.u256.DQWords.dqw0; return VINF_DBGF_TRUNCATED_REGISTER;
791 case DBGFREGVALTYPE_U256: pValue->u256 = InVal.u256; return VINF_SUCCESS;
792 case DBGFREGVALTYPE_U512: pValue->u512.OWords.ow0 = InVal.u256; return VINF_SUCCESS;
793 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU128(pValue, InVal.u256.DQWords.dqw0); return VINF_DBGF_TRUNCATED_REGISTER;
794 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
795
796 case DBGFREGVALTYPE_32BIT_HACK:
797 case DBGFREGVALTYPE_END:
798 case DBGFREGVALTYPE_INVALID:
799 break;
800 }
801 break;
802
803 case DBGFREGVALTYPE_U512:
804 switch (enmToType)
805 {
806 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u512.Words.w0; return VINF_DBGF_TRUNCATED_REGISTER;
807 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u512.Words.w0; return VINF_DBGF_TRUNCATED_REGISTER;
808 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u512.DWords.dw0; return VINF_DBGF_TRUNCATED_REGISTER;
809 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u512.QWords.qw0; return VINF_DBGF_TRUNCATED_REGISTER;
810 case DBGFREGVALTYPE_U128: pValue->u128 = InVal.u512.DQWords.dqw0; return VINF_DBGF_TRUNCATED_REGISTER;
811 case DBGFREGVALTYPE_U256: pValue->u256 = InVal.u512.OWords.ow0; return VINF_DBGF_TRUNCATED_REGISTER;
812 case DBGFREGVALTYPE_U512: pValue->u512 = InVal.u512; return VINF_SUCCESS;
813 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU128(pValue, InVal.u512.DQWords.dqw0); return VINF_DBGF_TRUNCATED_REGISTER;
814 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
815
816 case DBGFREGVALTYPE_32BIT_HACK:
817 case DBGFREGVALTYPE_END:
818 case DBGFREGVALTYPE_INVALID:
819 break;
820 }
821 break;
822
823 case DBGFREGVALTYPE_R80:
824 switch (enmToType)
825 {
826 case DBGFREGVALTYPE_U8: pValue->u8 = (uint8_t )dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
827 case DBGFREGVALTYPE_U16: pValue->u16 = (uint16_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
828 case DBGFREGVALTYPE_U32: pValue->u32 = (uint32_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
829 case DBGFREGVALTYPE_U64: pValue->u64 = (uint64_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
830 case DBGFREGVALTYPE_U128: pValue->u128 = dbgfR3RegValR80GetU128(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
831 case DBGFREGVALTYPE_U256: pValue->u256.DQWords.dqw0 = dbgfR3RegValR80GetU128(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
832 case DBGFREGVALTYPE_U512: pValue->u512.DQWords.dqw0 = dbgfR3RegValR80GetU128(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
833 case DBGFREGVALTYPE_R80: pValue->r80 = InVal.r80; return VINF_SUCCESS;
834 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
835
836 case DBGFREGVALTYPE_32BIT_HACK:
837 case DBGFREGVALTYPE_END:
838 case DBGFREGVALTYPE_INVALID:
839 break;
840 }
841 break;
842
843 case DBGFREGVALTYPE_DTR:
844 switch (enmToType)
845 {
846 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
847 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
848 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
849 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
850 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
851 case DBGFREGVALTYPE_U256: pValue->u256.QWords.qw0 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
852 case DBGFREGVALTYPE_U512: pValue->u512.QWords.qw0 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
853 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.dtr.u64Base); return VINF_DBGF_TRUNCATED_REGISTER;
854 case DBGFREGVALTYPE_DTR: pValue->dtr = InVal.dtr; return VINF_SUCCESS;
855
856 case DBGFREGVALTYPE_32BIT_HACK:
857 case DBGFREGVALTYPE_END:
858 case DBGFREGVALTYPE_INVALID:
859 break;
860 }
861 break;
862
863 case DBGFREGVALTYPE_INVALID:
864 case DBGFREGVALTYPE_END:
865 case DBGFREGVALTYPE_32BIT_HACK:
866 break;
867 }
868
869 AssertMsgFailed(("%d / %d\n", enmFromType, enmToType));
870 return VERR_DBGF_UNSUPPORTED_CAST;
871}
872
873
874/**
875 * Worker for the CPU register queries.
876 *
877 * @returns VBox status code.
878 * @retval VINF_SUCCESS
879 * @retval VERR_INVALID_VM_HANDLE
880 * @retval VERR_INVALID_CPU_ID
881 * @retval VERR_DBGF_REGISTER_NOT_FOUND
882 * @retval VERR_DBGF_UNSUPPORTED_CAST
883 * @retval VINF_DBGF_TRUNCATED_REGISTER
884 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
885 *
886 * @param pUVM The user mode VM handle.
887 * @param idCpu The virtual CPU ID.
888 * @param enmReg The register to query.
889 * @param enmType The desired return type.
890 * @param fGuestRegs Query guest CPU registers if set (true),
891 * hypervisor CPU registers if clear (false).
892 * @param pValue Where to return the register value.
893 */
894static DECLCALLBACK(int) dbgfR3RegCpuQueryWorkerOnCpu(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType,
895 bool fGuestRegs, PDBGFREGVAL pValue)
896{
897 int rc = VINF_SUCCESS;
898 DBGF_REG_DB_LOCK_READ(pUVM);
899
900 /*
901 * Look up the register set of the specified CPU.
902 */
903 PDBGFREGSET pSet = fGuestRegs
904 ? pUVM->aCpus[idCpu].dbgf.s.pGuestRegSet
905 : pUVM->aCpus[idCpu].dbgf.s.pHyperRegSet;
906 if (RT_LIKELY(pSet))
907 {
908 /*
909 * Look up the register and get the register value.
910 */
911#ifdef VBOX_VMM_TARGET_X86
912 DBGFREG const enmCpuFirst = DBGFREG_X86_FIRST;
913#elif defined(VBOX_VMM_TARGET_ARMV8)
914 DBGFREG const enmCpuFirst = DBGFREG_ARMV8_FIRST;
915#else
916# error "port me"
917#endif
918 uint32_t const idxDesc = (uint32_t)enmReg - (uint32_t)enmCpuFirst;
919 if (RT_LIKELY(idxDesc < pSet->cDescs))
920 {
921 PCDBGFREGDESC const pDesc = &pSet->paDescs[idxDesc];
922
923 pValue->au64[0] = pValue->au64[1] = 0;
924 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
925 if (RT_SUCCESS(rc))
926 {
927 /*
928 * Do the cast if the desired return type doesn't match what
929 * the getter returned.
930 */
931 if (pDesc->enmType == enmType)
932 rc = VINF_SUCCESS;
933 else
934 rc = dbgfR3RegValCast(pValue, pDesc->enmType, enmType);
935 }
936 }
937 else
938 rc = VERR_DBGF_REGISTER_NOT_FOUND;
939 }
940 else
941 rc = VERR_INVALID_CPU_ID;
942
943 DBGF_REG_DB_UNLOCK_READ(pUVM);
944 return rc;
945}
946
947
948/**
949 * Internal worker for the CPU register query functions.
950 *
951 * @returns VBox status code.
952 * @retval VINF_SUCCESS
953 * @retval VERR_INVALID_VM_HANDLE
954 * @retval VERR_INVALID_CPU_ID
955 * @retval VERR_DBGF_REGISTER_NOT_FOUND
956 * @retval VERR_DBGF_UNSUPPORTED_CAST
957 * @retval VINF_DBGF_TRUNCATED_REGISTER
958 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
959 *
960 * @param pUVM The user mode VM handle.
961 * @param idCpu The virtual CPU ID. Can be OR'ed with
962 * DBGFREG_HYPER_VMCPUID.
963 * @param enmReg The register to query.
964 * @param enmType The desired return type.
965 * @param pValue Where to return the register value.
966 */
967static int dbgfR3RegCpuQueryWorker(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType, PDBGFREGVAL pValue)
968{
969 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
970 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
971 AssertMsgReturn(enmReg >= DBGFREG_AL && enmReg <= DBGFREG_END, ("%d\n", enmReg), VERR_INVALID_PARAMETER);
972
973 bool const fGuestRegs = !(idCpu & DBGFREG_HYPER_VMCPUID);
974 idCpu &= ~DBGFREG_HYPER_VMCPUID;
975 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
976
977 return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorkerOnCpu, 6,
978 pUVM, idCpu, enmReg, enmType, fGuestRegs, pValue);
979}
980
981
982/**
983 * Queries a 8-bit CPU register value.
984 *
985 * @retval VINF_SUCCESS
986 * @retval VERR_INVALID_VM_HANDLE
987 * @retval VERR_INVALID_CPU_ID
988 * @retval VERR_DBGF_REGISTER_NOT_FOUND
989 * @retval VERR_DBGF_UNSUPPORTED_CAST
990 * @retval VINF_DBGF_TRUNCATED_REGISTER
991 *
992 * @param pUVM The user mode VM handle.
993 * @param idCpu The target CPU ID. Can be OR'ed with
994 * DBGFREG_HYPER_VMCPUID.
995 * @param enmReg The register that's being queried.
996 * @param pu8 Where to store the register value.
997 */
998VMMR3DECL(int) DBGFR3RegCpuQueryU8(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint8_t *pu8)
999{
1000 DBGFREGVAL Value;
1001 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U8, &Value);
1002 if (RT_SUCCESS(rc))
1003 *pu8 = Value.u8;
1004 else
1005 *pu8 = 0;
1006 return rc;
1007}
1008
1009
1010/**
1011 * Queries a 16-bit CPU register value.
1012 *
1013 * @retval VINF_SUCCESS
1014 * @retval VERR_INVALID_VM_HANDLE
1015 * @retval VERR_INVALID_CPU_ID
1016 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1017 * @retval VERR_DBGF_UNSUPPORTED_CAST
1018 * @retval VINF_DBGF_TRUNCATED_REGISTER
1019 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1020 *
1021 * @param pUVM The user mode VM handle.
1022 * @param idCpu The target CPU ID. Can be OR'ed with
1023 * DBGFREG_HYPER_VMCPUID.
1024 * @param enmReg The register that's being queried.
1025 * @param pu16 Where to store the register value.
1026 */
1027VMMR3DECL(int) DBGFR3RegCpuQueryU16(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint16_t *pu16)
1028{
1029 DBGFREGVAL Value;
1030 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U16, &Value);
1031 if (RT_SUCCESS(rc))
1032 *pu16 = Value.u16;
1033 else
1034 *pu16 = 0;
1035 return rc;
1036}
1037
1038
1039/**
1040 * Queries a 32-bit CPU register value.
1041 *
1042 * @retval VINF_SUCCESS
1043 * @retval VERR_INVALID_VM_HANDLE
1044 * @retval VERR_INVALID_CPU_ID
1045 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1046 * @retval VERR_DBGF_UNSUPPORTED_CAST
1047 * @retval VINF_DBGF_TRUNCATED_REGISTER
1048 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1049 *
1050 * @param pUVM The user mode VM handle.
1051 * @param idCpu The target CPU ID. Can be OR'ed with
1052 * DBGFREG_HYPER_VMCPUID.
1053 * @param enmReg The register that's being queried.
1054 * @param pu32 Where to store the register value.
1055 */
1056VMMR3DECL(int) DBGFR3RegCpuQueryU32(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint32_t *pu32)
1057{
1058 DBGFREGVAL Value;
1059 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U32, &Value);
1060 if (RT_SUCCESS(rc))
1061 *pu32 = Value.u32;
1062 else
1063 *pu32 = 0;
1064 return rc;
1065}
1066
1067
1068/**
1069 * Queries a 64-bit CPU register value.
1070 *
1071 * @retval VINF_SUCCESS
1072 * @retval VERR_INVALID_VM_HANDLE
1073 * @retval VERR_INVALID_CPU_ID
1074 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1075 * @retval VERR_DBGF_UNSUPPORTED_CAST
1076 * @retval VINF_DBGF_TRUNCATED_REGISTER
1077 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1078 *
1079 * @param pUVM The user mode VM handle.
1080 * @param idCpu The target CPU ID. Can be OR'ed with
1081 * DBGFREG_HYPER_VMCPUID.
1082 * @param enmReg The register that's being queried.
1083 * @param pu64 Where to store the register value.
1084 */
1085VMMR3DECL(int) DBGFR3RegCpuQueryU64(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint64_t *pu64)
1086{
1087 DBGFREGVAL Value;
1088 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U64, &Value);
1089 if (RT_SUCCESS(rc))
1090 *pu64 = Value.u64;
1091 else
1092 *pu64 = 0;
1093 return rc;
1094}
1095
1096
1097/**
1098 * Queries a descriptor table register value.
1099 *
1100 * @retval VINF_SUCCESS
1101 * @retval VERR_INVALID_VM_HANDLE
1102 * @retval VERR_INVALID_CPU_ID
1103 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1104 * @retval VERR_DBGF_UNSUPPORTED_CAST
1105 * @retval VINF_DBGF_TRUNCATED_REGISTER
1106 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1107 *
1108 * @param pUVM The user mode VM handle.
1109 * @param idCpu The target CPU ID. Can be OR'ed with
1110 * DBGFREG_HYPER_VMCPUID.
1111 * @param enmReg The register that's being queried.
1112 * @param pu64Base Where to store the register base value.
1113 * @param pu16Limit Where to store the register limit value.
1114 */
1115VMMR3DECL(int) DBGFR3RegCpuQueryXdtr(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint64_t *pu64Base, uint16_t *pu16Limit)
1116{
1117 DBGFREGVAL Value;
1118 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_DTR, &Value);
1119 if (RT_SUCCESS(rc))
1120 {
1121 *pu64Base = Value.dtr.u64Base;
1122 *pu16Limit = Value.dtr.u32Limit;
1123 }
1124 else
1125 {
1126 *pu64Base = 0;
1127 *pu16Limit = 0;
1128 }
1129 return rc;
1130}
1131
1132
1133#if 0 /* rewrite / remove */
1134
1135/**
1136 * Wrapper around CPUMQueryGuestMsr for dbgfR3RegCpuQueryBatchWorker.
1137 *
1138 * @retval VINF_SUCCESS
1139 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1140 *
1141 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1142 * @param pReg The where to store the register value and
1143 * size.
1144 * @param idMsr The MSR to get.
1145 */
1146static void dbgfR3RegGetMsrBatch(PVMCPU pVCpu, PDBGFREGENTRY pReg, uint32_t idMsr)
1147{
1148 pReg->enmType = DBGFREGVALTYPE_U64;
1149 int rc = CPUMQueryGuestMsr(pVCpu, idMsr, &pReg->Val.u64);
1150 if (RT_FAILURE(rc))
1151 {
1152 AssertMsg(rc == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", rc));
1153 pReg->Val.u64 = 0;
1154 }
1155}
1156
1157
1158static DECLCALLBACK(int) dbgfR3RegCpuQueryBatchWorker(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1159{
1160#if 0
1161 PVMCPU pVCpu = &pUVM->pVM->aCpus[idCpu];
1162 PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1163
1164 PDBGFREGENTRY pReg = paRegs - 1;
1165 while (cRegs-- > 0)
1166 {
1167 pReg++;
1168 pReg->Val.au64[0] = 0;
1169 pReg->Val.au64[1] = 0;
1170
1171 DBGFREG const enmReg = pReg->enmReg;
1172 AssertMsgReturn(enmReg >= 0 && enmReg <= DBGFREG_END, ("%d (%#x)\n", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
1173 if (enmReg != DBGFREG_END)
1174 {
1175 PCDBGFREGDESC pDesc = &g_aDbgfRegDescs[enmReg];
1176 if (!pDesc->pfnGet)
1177 {
1178 PCRTUINT128U pu = (PCRTUINT128U)((uintptr_t)pCtx + pDesc->offCtx);
1179 pReg->enmType = pDesc->enmType;
1180 switch (pDesc->enmType)
1181 {
1182 case DBGFREGVALTYPE_U8: pReg->Val.u8 = pu->au8[0]; break;
1183 case DBGFREGVALTYPE_U16: pReg->Val.u16 = pu->au16[0]; break;
1184 case DBGFREGVALTYPE_U32: pReg->Val.u32 = pu->au32[0]; break;
1185 case DBGFREGVALTYPE_U64: pReg->Val.u64 = pu->au64[0]; break;
1186 case DBGFREGVALTYPE_U128:
1187 pReg->Val.au64[0] = pu->au64[0];
1188 pReg->Val.au64[1] = pu->au64[1];
1189 break;
1190 case DBGFREGVALTYPE_R80:
1191 pReg->Val.au64[0] = pu->au64[0];
1192 pReg->Val.au16[5] = pu->au16[5];
1193 break;
1194 default:
1195 AssertMsgFailedReturn(("%s %d\n", pDesc->pszName, pDesc->enmType), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
1196 }
1197 }
1198 else
1199 {
1200 int rc = pDesc->pfnGet(pVCpu, pDesc, pCtx, &pReg->Val.u);
1201 if (RT_FAILURE(rc))
1202 return rc;
1203 }
1204 }
1205 }
1206 return VINF_SUCCESS;
1207#else
1208 return VERR_NOT_IMPLEMENTED;
1209#endif
1210}
1211
1212
1213/**
1214 * Query a batch of registers.
1215 *
1216 * @retval VINF_SUCCESS
1217 * @retval VERR_INVALID_VM_HANDLE
1218 * @retval VERR_INVALID_CPU_ID
1219 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1220 *
1221 * @param pUVM The user mode VM handle.
1222 * @param idCpu The target CPU ID. Can be OR'ed with
1223 * DBGFREG_HYPER_VMCPUID.
1224 * @param paRegs Pointer to an array of @a cRegs elements. On
1225 * input the enmReg members indicates which
1226 * registers to query. On successful return the
1227 * other members are set. DBGFREG_END can be used
1228 * as a filler.
1229 * @param cRegs The number of entries in @a paRegs.
1230 */
1231VMMR3DECL(int) DBGFR3RegCpuQueryBatch(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1232{
1233 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1234 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1235 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
1236 if (!cRegs)
1237 return VINF_SUCCESS;
1238 AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
1239 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
1240 size_t iReg = cRegs;
1241 while (iReg-- > 0)
1242 {
1243 DBGFREG enmReg = paRegs[iReg].enmReg;
1244 AssertMsgReturn(enmReg < DBGFREG_END && enmReg >= DBGFREG_AL, ("%d (%#x)", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
1245 }
1246
1247 return VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pUVM, idCpu, paRegs, cRegs);
1248}
1249
1250
1251/**
1252 * Query all registers for a Virtual CPU.
1253 *
1254 * @retval VINF_SUCCESS
1255 * @retval VERR_INVALID_VM_HANDLE
1256 * @retval VERR_INVALID_CPU_ID
1257 *
1258 * @param pUVM The user mode VM handle.
1259 * @param idCpu The target CPU ID. Can be OR'ed with
1260 * DBGFREG_HYPER_VMCPUID.
1261 * @param paRegs Pointer to an array of @a cRegs elements.
1262 * These will be filled with the CPU register
1263 * values. Overflowing entries will be set to
1264 * DBGFREG_END. The returned registers can be
1265 * accessed by using the DBGFREG values as index.
1266 * @param cRegs The number of entries in @a paRegs. The
1267 * recommended value is DBGFREG_ALL_COUNT.
1268 */
1269VMMR3DECL(int) DBGFR3RegCpuQueryAll(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1270{
1271 /*
1272 * Validate input.
1273 */
1274 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1275 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1276 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
1277 if (!cRegs)
1278 return VINF_SUCCESS;
1279 AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
1280 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
1281
1282 /*
1283 * Convert it into a batch query (lazy bird).
1284 */
1285 unsigned iReg = 0;
1286 while (iReg < cRegs && iReg < DBGFREG_ALL_COUNT)
1287 {
1288 paRegs[iReg].enmReg = (DBGFREG)iReg;
1289 iReg++;
1290 }
1291 while (iReg < cRegs)
1292 paRegs[iReg++].enmReg = DBGFREG_END;
1293
1294 return VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pUVM, idCpu, paRegs, cRegs);
1295}
1296
1297#endif /* rewrite or remove? */
1298
1299/**
1300 * Gets the name of a register.
1301 *
1302 * @returns Pointer to read-only register name (lower case). NULL if the
1303 * parameters are invalid.
1304 *
1305 * @param pUVM The user mode VM handle.
1306 * @param enmReg The register identifier.
1307 * @param enmType The register type. This is for sort out
1308 * aliases. Pass DBGFREGVALTYPE_INVALID to get
1309 * the standard name.
1310 */
1311VMMR3DECL(const char *) DBGFR3RegCpuName(PUVM pUVM, DBGFREG enmReg, DBGFREGVALTYPE enmType)
1312{
1313 AssertReturn(enmReg >= DBGFREG_AL && enmReg < DBGFREG_END, NULL);
1314 AssertReturn(enmType >= DBGFREGVALTYPE_INVALID && enmType < DBGFREGVALTYPE_END, NULL);
1315 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1316 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1317
1318 PCDBGFREGSET pSet = pUVM->aCpus[0].dbgf.s.pGuestRegSet;
1319 if (RT_UNLIKELY(!pSet))
1320 return NULL;
1321
1322 PCDBGFREGDESC pDesc = &pSet->paDescs[enmReg];
1323 PCDBGFREGALIAS pAlias = pDesc->paAliases;
1324 if ( pAlias
1325 && pDesc->enmType != enmType
1326 && enmType != DBGFREGVALTYPE_INVALID)
1327 {
1328 while (pAlias->pszName)
1329 {
1330 if (pAlias->enmType == enmType)
1331 return pAlias->pszName;
1332 pAlias++;
1333 }
1334 }
1335
1336 return pDesc->pszName;
1337}
1338
1339
1340/**
1341 * Fold the string to lower case and copy it into the destination buffer.
1342 *
1343 * @returns Number of folder characters, -1 on overflow.
1344 * @param pszSrc The source string.
1345 * @param cchSrc How much to fold and copy.
1346 * @param pszDst The output buffer.
1347 * @param cbDst The size of the output buffer.
1348 */
1349static ssize_t dbgfR3RegCopyToLower(const char *pszSrc, size_t cchSrc, char *pszDst, size_t cbDst)
1350{
1351 ssize_t cchFolded = 0;
1352 char ch;
1353 while (cchSrc-- > 0 && (ch = *pszSrc++))
1354 {
1355 if (RT_UNLIKELY(cbDst <= 1))
1356 return -1;
1357 cbDst--;
1358
1359 char chLower = RT_C_TO_LOWER(ch);
1360 cchFolded += chLower != ch;
1361 *pszDst++ = chLower;
1362 }
1363 if (RT_UNLIKELY(!cbDst))
1364 return -1;
1365 *pszDst = '\0';
1366 return cchFolded;
1367}
1368
1369
1370/**
1371 * Resolves the register name.
1372 *
1373 * @returns Lookup record.
1374 * @param pUVM The user mode VM handle.
1375 * @param idDefCpu The default CPU ID set.
1376 * @param pszReg The register name.
1377 * @param fGuestRegs Default to guest CPU registers if set, the
1378 * hypervisor CPU registers if clear.
1379 */
1380static PCDBGFREGLOOKUP dbgfR3RegResolve(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, bool fGuestRegs)
1381{
1382 DBGF_REG_DB_LOCK_READ(pUVM);
1383
1384 /* Try looking up the name without any case folding or cpu prefixing. */
1385 PRTSTRSPACE pRegSpace = &pUVM->dbgf.s.RegSpace;
1386 PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, pszReg);
1387 if (!pLookupRec)
1388 {
1389 char szName[DBGF_REG_MAX_NAME * 4 + 16];
1390
1391 /* Lower case it and try again. */
1392 ssize_t cchFolded = dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
1393 if (cchFolded > 0)
1394 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
1395 if ( !pLookupRec
1396 && cchFolded >= 0
1397 && idDefCpu != VMCPUID_ANY)
1398 {
1399 /* Prefix it with the specified CPU set. */
1400 size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), fGuestRegs ? "cpu%u." : "hypercpu%u.", idDefCpu);
1401 dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
1402 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
1403 }
1404 }
1405
1406 DBGF_REG_DB_UNLOCK_READ(pUVM);
1407 return pLookupRec;
1408}
1409
1410
1411/**
1412 * Validates the register name.
1413 *
1414 * @returns VBox status code.
1415 * @retval VINF_SUCCESS if the register was found.
1416 * @retval VERR_DBGF_REGISTER_NOT_FOUND if not found.
1417 *
1418 * @param pUVM The user mode VM handle.
1419 * @param idDefCpu The default CPU.
1420 * @param pszReg The registe name.
1421 */
1422VMMR3DECL(int) DBGFR3RegNmValidate(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg)
1423{
1424 /*
1425 * Validate input.
1426 */
1427 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1428 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1429 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
1430 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
1431
1432 /*
1433 * Resolve the register.
1434 */
1435 bool fGuestRegs = true;
1436 if ((idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY)
1437 {
1438 fGuestRegs = false;
1439 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
1440 }
1441
1442 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
1443 if (!pLookupRec)
1444 return VERR_DBGF_REGISTER_NOT_FOUND;
1445 return VINF_SUCCESS;
1446}
1447
1448
1449/**
1450 * On CPU worker for the register queries, used by dbgfR3RegNmQueryWorker and
1451 * dbgfR3RegPrintfCbFormatNormal.
1452 *
1453 * @returns VBox status code.
1454 *
1455 * @param pUVM The user mode VM handle.
1456 * @param pLookupRec The register lookup record.
1457 * @param enmType The desired return type.
1458 * @param pValue Where to return the register value.
1459 * @param penmType Where to store the register value type.
1460 * Optional.
1461 */
1462static DECLCALLBACK(int) dbgfR3RegNmQueryWorkerOnCpu(PUVM pUVM, PCDBGFREGLOOKUP pLookupRec, DBGFREGVALTYPE enmType,
1463 PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1464{
1465 PCDBGFREGDESC pDesc = pLookupRec->pDesc;
1466 PCDBGFREGSET pSet = pLookupRec->pSet;
1467 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
1468 DBGFREGVALTYPE enmValueType = pDesc->enmType;
1469 int rc;
1470
1471 NOREF(pUVM);
1472
1473 /*
1474 * Get the register or sub-field value.
1475 */
1476 dbgfR3RegValClear(pValue);
1477 if (!pSubField)
1478 {
1479 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
1480 if ( pLookupRec->pAlias
1481 && pLookupRec->pAlias->enmType != enmValueType
1482 && RT_SUCCESS(rc))
1483 {
1484 rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
1485 enmValueType = pLookupRec->pAlias->enmType;
1486 }
1487 }
1488 else
1489 {
1490 if (pSubField->pfnGet)
1491 {
1492 rc = pSubField->pfnGet(pSet->uUserArg.pv, pSubField, &pValue->u128);
1493 enmValueType = DBGFREGVALTYPE_U128;
1494 }
1495 else
1496 {
1497 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
1498 if ( pLookupRec->pAlias
1499 && pLookupRec->pAlias->enmType != enmValueType
1500 && RT_SUCCESS(rc))
1501 {
1502 rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
1503 enmValueType = pLookupRec->pAlias->enmType;
1504 }
1505 if (RT_SUCCESS(rc))
1506 {
1507 rc = dbgfR3RegValCast(pValue, enmValueType, DBGFREGVALTYPE_U128);
1508 if (RT_SUCCESS(rc))
1509 {
1510 RTUInt128AssignShiftLeft(&pValue->u128, -pSubField->iFirstBit);
1511 RTUInt128AssignAndNFirstBits(&pValue->u128, pSubField->cBits);
1512 if (pSubField->cShift)
1513 RTUInt128AssignShiftLeft(&pValue->u128, pSubField->cShift);
1514 }
1515 }
1516 }
1517 if (RT_SUCCESS(rc))
1518 {
1519 unsigned const cBits = pSubField->cBits + pSubField->cShift;
1520 if (cBits <= 8)
1521 enmValueType = DBGFREGVALTYPE_U8;
1522 else if (cBits <= 16)
1523 enmValueType = DBGFREGVALTYPE_U16;
1524 else if (cBits <= 32)
1525 enmValueType = DBGFREGVALTYPE_U32;
1526 else if (cBits <= 64)
1527 enmValueType = DBGFREGVALTYPE_U64;
1528 else
1529 enmValueType = DBGFREGVALTYPE_U128;
1530 rc = dbgfR3RegValCast(pValue, DBGFREGVALTYPE_U128, enmValueType);
1531 }
1532 }
1533 if (RT_SUCCESS(rc))
1534 {
1535 /*
1536 * Do the cast if the desired return type doesn't match what
1537 * the getter returned.
1538 */
1539 if ( enmValueType == enmType
1540 || enmType == DBGFREGVALTYPE_END)
1541 {
1542 rc = VINF_SUCCESS;
1543 if (penmType)
1544 *penmType = enmValueType;
1545 }
1546 else
1547 {
1548 rc = dbgfR3RegValCast(pValue, enmValueType, enmType);
1549 if (penmType)
1550 *penmType = RT_SUCCESS(rc) ? enmType : enmValueType;
1551 }
1552 }
1553
1554 return rc;
1555}
1556
1557
1558/**
1559 * Worker for the register queries.
1560 *
1561 * @returns VBox status code.
1562 * @retval VINF_SUCCESS
1563 * @retval VERR_INVALID_VM_HANDLE
1564 * @retval VERR_INVALID_CPU_ID
1565 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1566 * @retval VERR_DBGF_UNSUPPORTED_CAST
1567 * @retval VINF_DBGF_TRUNCATED_REGISTER
1568 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1569 *
1570 * @param pUVM The user mode VM handle.
1571 * @param idDefCpu The virtual CPU ID for the default CPU register
1572 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
1573 * @param pszReg The register to query.
1574 * @param enmType The desired return type.
1575 * @param pValue Where to return the register value.
1576 * @param penmType Where to store the register value type.
1577 * Optional.
1578 */
1579static int dbgfR3RegNmQueryWorker(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, DBGFREGVALTYPE enmType,
1580 PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1581{
1582 /*
1583 * Validate input.
1584 */
1585 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1586 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1587 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
1588 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
1589
1590 Assert(enmType > DBGFREGVALTYPE_INVALID && enmType <= DBGFREGVALTYPE_END);
1591 AssertPtr(pValue);
1592
1593 /*
1594 * Resolve the register and call the getter on the relevant CPU.
1595 */
1596 bool fGuestRegs = true;
1597 if ((idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY)
1598 {
1599 fGuestRegs = false;
1600 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
1601 }
1602 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
1603 if (pLookupRec)
1604 {
1605 if (pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU)
1606 idDefCpu = pLookupRec->pSet->uUserArg.pVCpu->idCpu;
1607 else if (idDefCpu != VMCPUID_ANY)
1608 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
1609 return VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmQueryWorkerOnCpu, 5,
1610 pUVM, pLookupRec, enmType, pValue, penmType);
1611 }
1612 return VERR_DBGF_REGISTER_NOT_FOUND;
1613}
1614
1615
1616/**
1617 * Queries a descriptor table register value.
1618 *
1619 * @retval VINF_SUCCESS
1620 * @retval VERR_INVALID_VM_HANDLE
1621 * @retval VERR_INVALID_CPU_ID
1622 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1623 *
1624 * @param pUVM The user mode VM handle.
1625 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1626 * applicable. Can be OR'ed with
1627 * DBGFREG_HYPER_VMCPUID.
1628 * @param pszReg The register that's being queried. Except for
1629 * CPU registers, this must be on the form
1630 * "set.reg[.sub]".
1631 * @param pValue Where to store the register value.
1632 * @param penmType Where to store the register value type.
1633 */
1634VMMR3DECL(int) DBGFR3RegNmQuery(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1635{
1636 return dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_END, pValue, penmType);
1637}
1638
1639
1640/**
1641 * Queries a 8-bit register value.
1642 *
1643 * @retval VINF_SUCCESS
1644 * @retval VERR_INVALID_VM_HANDLE
1645 * @retval VERR_INVALID_CPU_ID
1646 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1647 * @retval VERR_DBGF_UNSUPPORTED_CAST
1648 * @retval VINF_DBGF_TRUNCATED_REGISTER
1649 *
1650 * @param pUVM The user mode VM handle.
1651 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1652 * applicable. Can be OR'ed with
1653 * DBGFREG_HYPER_VMCPUID.
1654 * @param pszReg The register that's being queried. Except for
1655 * CPU registers, this must be on the form
1656 * "set.reg[.sub]".
1657 * @param pu8 Where to store the register value.
1658 */
1659VMMR3DECL(int) DBGFR3RegNmQueryU8(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint8_t *pu8)
1660{
1661 DBGFREGVAL Value;
1662 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U8, &Value, NULL);
1663 if (RT_SUCCESS(rc))
1664 *pu8 = Value.u8;
1665 else
1666 *pu8 = 0;
1667 return rc;
1668}
1669
1670
1671/**
1672 * Queries a 16-bit register value.
1673 *
1674 * @retval VINF_SUCCESS
1675 * @retval VERR_INVALID_VM_HANDLE
1676 * @retval VERR_INVALID_CPU_ID
1677 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1678 * @retval VERR_DBGF_UNSUPPORTED_CAST
1679 * @retval VINF_DBGF_TRUNCATED_REGISTER
1680 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1681 *
1682 * @param pUVM The user mode VM handle.
1683 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1684 * applicable. Can be OR'ed with
1685 * DBGFREG_HYPER_VMCPUID.
1686 * @param pszReg The register that's being queried. Except for
1687 * CPU registers, this must be on the form
1688 * "set.reg[.sub]".
1689 * @param pu16 Where to store the register value.
1690 */
1691VMMR3DECL(int) DBGFR3RegNmQueryU16(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint16_t *pu16)
1692{
1693 DBGFREGVAL Value;
1694 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U16, &Value, NULL);
1695 if (RT_SUCCESS(rc))
1696 *pu16 = Value.u16;
1697 else
1698 *pu16 = 0;
1699 return rc;
1700}
1701
1702
1703/**
1704 * Queries a 32-bit register value.
1705 *
1706 * @retval VINF_SUCCESS
1707 * @retval VERR_INVALID_VM_HANDLE
1708 * @retval VERR_INVALID_CPU_ID
1709 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1710 * @retval VERR_DBGF_UNSUPPORTED_CAST
1711 * @retval VINF_DBGF_TRUNCATED_REGISTER
1712 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1713 *
1714 * @param pUVM The user mode VM handle.
1715 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1716 * applicable. Can be OR'ed with
1717 * DBGFREG_HYPER_VMCPUID.
1718 * @param pszReg The register that's being queried. Except for
1719 * CPU registers, this must be on the form
1720 * "set.reg[.sub]".
1721 * @param pu32 Where to store the register value.
1722 */
1723VMMR3DECL(int) DBGFR3RegNmQueryU32(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint32_t *pu32)
1724{
1725 DBGFREGVAL Value;
1726 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U32, &Value, NULL);
1727 if (RT_SUCCESS(rc))
1728 *pu32 = Value.u32;
1729 else
1730 *pu32 = 0;
1731 return rc;
1732}
1733
1734
1735/**
1736 * Queries a 64-bit register value.
1737 *
1738 * @retval VINF_SUCCESS
1739 * @retval VERR_INVALID_VM_HANDLE
1740 * @retval VERR_INVALID_CPU_ID
1741 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1742 * @retval VERR_DBGF_UNSUPPORTED_CAST
1743 * @retval VINF_DBGF_TRUNCATED_REGISTER
1744 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1745 *
1746 * @param pUVM The user mode VM handle.
1747 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1748 * applicable. Can be OR'ed with
1749 * DBGFREG_HYPER_VMCPUID.
1750 * @param pszReg The register that's being queried. Except for
1751 * CPU registers, this must be on the form
1752 * "set.reg[.sub]".
1753 * @param pu64 Where to store the register value.
1754 */
1755VMMR3DECL(int) DBGFR3RegNmQueryU64(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64)
1756{
1757 DBGFREGVAL Value;
1758 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U64, &Value, NULL);
1759 if (RT_SUCCESS(rc))
1760 *pu64 = Value.u64;
1761 else
1762 *pu64 = 0;
1763 return rc;
1764}
1765
1766
1767/**
1768 * Queries a 128-bit register value.
1769 *
1770 * @retval VINF_SUCCESS
1771 * @retval VERR_INVALID_VM_HANDLE
1772 * @retval VERR_INVALID_CPU_ID
1773 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1774 * @retval VERR_DBGF_UNSUPPORTED_CAST
1775 * @retval VINF_DBGF_TRUNCATED_REGISTER
1776 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1777 *
1778 * @param pUVM The user mode VM handle.
1779 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1780 * applicable. Can be OR'ed with
1781 * DBGFREG_HYPER_VMCPUID.
1782 * @param pszReg The register that's being queried. Except for
1783 * CPU registers, this must be on the form
1784 * "set.reg[.sub]".
1785 * @param pu128 Where to store the register value.
1786 */
1787VMMR3DECL(int) DBGFR3RegNmQueryU128(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PRTUINT128U pu128)
1788{
1789 DBGFREGVAL Value;
1790 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U128, &Value, NULL);
1791 if (RT_SUCCESS(rc))
1792 *pu128 = Value.u128;
1793 else
1794 pu128->s.Hi = pu128->s.Lo = 0;
1795 return rc;
1796}
1797
1798
1799#if 0
1800/**
1801 * Queries a long double register value.
1802 *
1803 * @retval VINF_SUCCESS
1804 * @retval VERR_INVALID_VM_HANDLE
1805 * @retval VERR_INVALID_CPU_ID
1806 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1807 * @retval VERR_DBGF_UNSUPPORTED_CAST
1808 * @retval VINF_DBGF_TRUNCATED_REGISTER
1809 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1810 *
1811 * @param pUVM The user mode VM handle.
1812 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1813 * applicable. Can be OR'ed with
1814 * DBGFREG_HYPER_VMCPUID.
1815 * @param pszReg The register that's being queried. Except for
1816 * CPU registers, this must be on the form
1817 * "set.reg[.sub]".
1818 * @param plrd Where to store the register value.
1819 */
1820VMMR3DECL(int) DBGFR3RegNmQueryLrd(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, long double *plrd)
1821{
1822 DBGFREGVAL Value;
1823 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_R80, &Value, NULL);
1824 if (RT_SUCCESS(rc))
1825 *plrd = Value.lrd;
1826 else
1827 *plrd = 0;
1828 return rc;
1829}
1830#endif
1831
1832
1833/**
1834 * Queries a descriptor table register value.
1835 *
1836 * @retval VINF_SUCCESS
1837 * @retval VERR_INVALID_VM_HANDLE
1838 * @retval VERR_INVALID_CPU_ID
1839 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1840 * @retval VERR_DBGF_UNSUPPORTED_CAST
1841 * @retval VINF_DBGF_TRUNCATED_REGISTER
1842 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1843 *
1844 * @param pUVM The user mode VM handle.
1845 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1846 * applicable. Can be OR'ed with
1847 * DBGFREG_HYPER_VMCPUID.
1848 * @param pszReg The register that's being queried. Except for
1849 * CPU registers, this must be on the form
1850 * "set.reg[.sub]".
1851 * @param pu64Base Where to store the register base value.
1852 * @param pu16Limit Where to store the register limit value.
1853 */
1854VMMR3DECL(int) DBGFR3RegNmQueryXdtr(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64Base, uint16_t *pu16Limit)
1855{
1856 DBGFREGVAL Value;
1857 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_DTR, &Value, NULL);
1858 if (RT_SUCCESS(rc))
1859 {
1860 *pu64Base = Value.dtr.u64Base;
1861 *pu16Limit = Value.dtr.u32Limit;
1862 }
1863 else
1864 {
1865 *pu64Base = 0;
1866 *pu16Limit = 0;
1867 }
1868 return rc;
1869}
1870
1871
1872/**
1873 * Gets the number of bits in value of type @a enmValType.
1874 */
1875static unsigned dbgfR3RegGetBitsForValType(DBGFREGVALTYPE enmValType)
1876{
1877 switch (enmValType)
1878 {
1879 case DBGFREGVALTYPE_U8: return 8;
1880 case DBGFREGVALTYPE_U16: return 16;
1881 case DBGFREGVALTYPE_U32: return 32;
1882 case DBGFREGVALTYPE_U64: return 64;
1883 case DBGFREGVALTYPE_U128: return 128;
1884 case DBGFREGVALTYPE_U256: return 256;
1885 case DBGFREGVALTYPE_U512: return 512;
1886 case DBGFREGVALTYPE_R80: return 80;
1887 case DBGFREGVALTYPE_DTR: return 80;
1888 /* no default, want gcc warnings */
1889 case DBGFREGVALTYPE_32BIT_HACK:
1890 case DBGFREGVALTYPE_END:
1891 case DBGFREGVALTYPE_INVALID:
1892 break;
1893 }
1894 return 512;
1895}
1896
1897
1898/**
1899 * On CPU worker for the extended register queries, used by DBGFR3RegNmQueryEx.
1900 *
1901 * @returns VBox status code.
1902 *
1903 * @param pUVM The user mode VM handle.
1904 * @param pLookupRec The register lookup record.
1905 * @param fFlags DBGFR3REG_QUERY_EX_F_XXX
1906 * @param paRegs Where to return the register values.
1907 * @param cRegs The number of register values to return.
1908 * The caller has checked that this is sufficient
1909 * to store the entire result.
1910 */
1911static DECLCALLBACK(int) dbgfR3RegNmQueryExWorkerOnCpu(PUVM pUVM, PCDBGFREGLOOKUP pLookupRec, uint32_t fFlags,
1912 PDBGFREGENTRYNM paRegs, size_t cRegs)
1913{
1914 PCDBGFREGDESC pDesc = pLookupRec->pDesc;
1915 PCDBGFREGSET pSet = pLookupRec->pSet;
1916 Assert(!pLookupRec->pSubField);
1917 NOREF(pUVM);
1918
1919 /*
1920 * The register first.
1921 */
1922 AssertReturn(cRegs > 0, VERR_BUFFER_OVERFLOW);
1923 dbgfR3RegValClear(&paRegs[0].Val);
1924 paRegs[0].pszName = pLookupRec->Core.pszString;
1925 paRegs[0].enmType = pDesc->enmType;
1926 paRegs[0].u.uInfo = 0;
1927 paRegs[0].u.s.fMain = true;
1928 int rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, &paRegs[0].Val);
1929 AssertRCReturn(rc, rc);
1930 DBGFREGVAL const MainValue = paRegs[0].Val;
1931 uint32_t iReg = 1;
1932
1933 /* If it's a alias we looked up we may have to do some casting and
1934 restricting the number of bits included in the sub-fields. */
1935 unsigned cMaxBits = sizeof(paRegs[0].Val) * 8;
1936 if (pLookupRec->pAlias)
1937 {
1938 paRegs[0].enmType = pLookupRec->pAlias->enmType;
1939 paRegs[0].u.uInfo = 0;
1940 paRegs[0].u.s.fAlias = true;
1941 if (paRegs[0].enmType != pDesc->enmType)
1942 {
1943 dbgfR3RegValCast(&paRegs[0].Val, pDesc->enmType, paRegs[0].enmType);
1944 cMaxBits = dbgfR3RegGetBitsForValType(paRegs[0].enmType);
1945 }
1946
1947 /* Add the main value as the 2nd entry. */
1948 paRegs[iReg].pszName = pDesc->pszName;
1949 paRegs[iReg].enmType = pDesc->enmType;
1950 paRegs[iReg].Val = MainValue;
1951 paRegs[iReg].u.uInfo = 0;
1952 paRegs[iReg].u.s.fMain = true;
1953 iReg++;
1954 }
1955
1956 /*
1957 * (Other) Aliases.
1958 */
1959 if ( (fFlags & DBGFR3REG_QUERY_EX_F_ALIASES)
1960 && pDesc->paAliases)
1961 {
1962 PCDBGFREGALIAS const paAliases = pDesc->paAliases;
1963 for (uint32_t i = 0; paAliases[i].pszName != NULL; i++)
1964 if (&paAliases[i] != pLookupRec->pAlias )
1965 {
1966 AssertReturn(iReg < cRegs, VERR_BUFFER_OVERFLOW);
1967 paRegs[iReg].pszName = paAliases[i].pszName;
1968 paRegs[iReg].enmType = paAliases[i].enmType;
1969 paRegs[iReg].u.uInfo = 0;
1970 paRegs[iReg].u.s.fAlias = true;
1971 paRegs[iReg].Val = MainValue;
1972 dbgfR3RegValCast(&paRegs[iReg].Val, pDesc->enmType, paAliases[i].enmType);
1973 iReg++;
1974 }
1975 }
1976
1977 /*
1978 * Subfields.
1979 */
1980 if ( (fFlags & DBGFR3REG_QUERY_EX_F_SUBFIELDS)
1981 && pDesc->paSubFields)
1982 {
1983 PCDBGFREGSUBFIELD const paSubFields = pDesc->paSubFields;
1984 for (uint32_t i = 0; paSubFields[i].pszName != NULL; i++)
1985 if (paSubFields[i].iFirstBit < cMaxBits || paSubFields[i].pfnGet)
1986 {
1987 AssertReturn(iReg < cRegs, VERR_BUFFER_OVERFLOW);
1988 int rc2;
1989 paRegs[iReg].pszName = paSubFields[i].pszName;
1990 paRegs[iReg].u.uInfo = 0;
1991 paRegs[iReg].u.s.fSubField = true;
1992 paRegs[iReg].u.s.cBits = paSubFields[i].cBits + paSubFields[i].cShift;
1993 if (paSubFields[i].pfnGet)
1994 {
1995 dbgfR3RegValClear(&paRegs[iReg].Val);
1996 rc2 = paSubFields[i].pfnGet(pSet->uUserArg.pv, &paSubFields[i], &paRegs[iReg].Val.u128);
1997 }
1998 else
1999 {
2000 paRegs[iReg].Val = MainValue;
2001 rc2 = dbgfR3RegValCast(&paRegs[iReg].Val, pDesc->enmType, DBGFREGVALTYPE_U128);
2002 if (RT_SUCCESS(rc2))
2003 {
2004 RTUInt128AssignShiftLeft(&paRegs[iReg].Val.u128, -paSubFields[i].iFirstBit);
2005 RTUInt128AssignAndNFirstBits(&paRegs[iReg].Val.u128, paSubFields[i].cBits);
2006 if (paSubFields[i].cShift)
2007 RTUInt128AssignShiftLeft(&paRegs[iReg].Val.u128, paSubFields[i].cShift);
2008 }
2009 }
2010 if (RT_SUCCESS(rc2))
2011 {
2012 unsigned const cBits = paSubFields[i].cBits + paSubFields[i].cShift;
2013 if (cBits <= 8)
2014 paRegs[iReg].enmType = DBGFREGVALTYPE_U8;
2015 else if (cBits <= 16)
2016 paRegs[iReg].enmType = DBGFREGVALTYPE_U16;
2017 else if (cBits <= 32)
2018 paRegs[iReg].enmType = DBGFREGVALTYPE_U32;
2019 else if (cBits <= 64)
2020 paRegs[iReg].enmType = DBGFREGVALTYPE_U64;
2021 else
2022 paRegs[iReg].enmType = DBGFREGVALTYPE_U128;
2023 rc2 = dbgfR3RegValCast(&paRegs[iReg].Val, DBGFREGVALTYPE_U128, paRegs[iReg].enmType);
2024 }
2025 if (RT_SUCCESS(rc2))
2026 iReg++;
2027 else
2028 rc = rc2;
2029 }
2030 }
2031 return rc;
2032}
2033
2034
2035/**
2036 * Queries a register with aliases and/or sub-fields.
2037 *
2038 * @retval VINF_SUCCESS
2039 * @retval VERR_INVALID_VM_HANDLE
2040 * @retval VERR_INVALID_CPU_ID
2041 * @retval VERR_BUFFER_OVERFLOW w/ *pcRegs set to the required size.
2042 * No other data returned.
2043 * @retval VERR_DBGF_REGISTER_NOT_FOUND
2044 * @retval VERR_DBGF_UNSUPPORTED_CAST
2045 * @retval VINF_DBGF_TRUNCATED_REGISTER
2046 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
2047 *
2048 * @param pUVM The user mode VM handle.
2049 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
2050 * applicable. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
2051 * @param pszReg The register that's being queried. Except for CPU
2052 * registers, this must be on the form "set.reg[.sub]".
2053 * @param fFlags DBGFR3REG_QUERY_EX_F_XXX
2054 * @param paRegs
2055 * @param pcRegs On input this is the size of the paRegs buffer.
2056 * On successful return this is set to the number of
2057 * registers returned. This is set to the required number
2058 * of register entries when VERR_BUFFER_OVERFLOW is
2059 * returned.
2060 */
2061VMMR3DECL(int) DBGFR3RegNmQueryEx(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint32_t fFlags,
2062 PDBGFREGENTRYNM paRegs, size_t *pcRegs)
2063{
2064 /*
2065 * Validate input.
2066 */
2067 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2068 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2069 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2070 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
2071 AssertReturn(!(fFlags & ~DBGFR3REG_QUERY_EX_F_VALID_MASK), VERR_INVALID_FLAGS);
2072 AssertPtrReturn(pcRegs, VERR_INVALID_POINTER);
2073 AssertPtrNullReturn(paRegs, VERR_INVALID_POINTER);
2074
2075 /*
2076 * Resolve the register and call the getter on the relevant CPU.
2077 */
2078 bool fGuestRegs = true;
2079 if ((idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY)
2080 {
2081 fGuestRegs = false;
2082 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2083 }
2084 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
2085 if (pLookupRec)
2086 {
2087 /*
2088 * Determine how many register values we'd be returning.
2089 */
2090 size_t cRegs = 1; /* we always return the direct hit. */
2091
2092 if ( (fFlags & DBGFR3REG_QUERY_EX_F_ALIASES)
2093 && !pLookupRec->pSubField
2094 && pLookupRec->pDesc->paAliases)
2095 {
2096 PCDBGFREGALIAS const paAliases = pLookupRec->pDesc->paAliases;
2097 for (uint32_t i = 0; paAliases[i].pszName != NULL; i++)
2098 cRegs++;
2099 }
2100 else if (pLookupRec->pAlias)
2101 cRegs++;
2102
2103 if ( (fFlags & DBGFR3REG_QUERY_EX_F_SUBFIELDS)
2104 && !pLookupRec->pSubField
2105 && pLookupRec->pDesc->paSubFields)
2106 {
2107 unsigned const cMaxBits = !pLookupRec->pAlias ? sizeof(paRegs[0].Val) * 8
2108 : dbgfR3RegGetBitsForValType(pLookupRec->pAlias->enmType);
2109 PCDBGFREGSUBFIELD const paSubFields = pLookupRec->pDesc->paSubFields;
2110 for (uint32_t i = 0; paSubFields[i].pszName != NULL; i++)
2111 if (paSubFields[i].iFirstBit < cMaxBits || paSubFields[i].pfnGet)
2112 cRegs++;
2113 }
2114
2115 /*
2116 * Did the caller provide sufficient room for the register values, then
2117 * retrieve the register on the specified CPU.
2118 */
2119 if (paRegs && *pcRegs >= cRegs)
2120 {
2121 *pcRegs = cRegs;
2122
2123 if (pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU)
2124 idDefCpu = pLookupRec->pSet->uUserArg.pVCpu->idCpu;
2125 else if (idDefCpu != VMCPUID_ANY)
2126 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2127
2128 /* If we hit a sub-field we'll just use the regular worker to get it. */
2129 if (!pLookupRec->pSubField)
2130 return VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmQueryExWorkerOnCpu, 5,
2131 pUVM, pLookupRec, fFlags, paRegs, cRegs);
2132 Assert(cRegs == 1);
2133 paRegs[0].pszName = pLookupRec->Core.pszString;
2134 paRegs[0].enmType = DBGFREGVALTYPE_END;
2135 paRegs[0].u.uInfo = 0;
2136 paRegs[0].u.s.cBits = pLookupRec->pSubField->cBits + pLookupRec->pSubField->cShift;
2137 paRegs[0].u.s.fSubField = true;
2138 dbgfR3RegValClear(&paRegs[0].Val);
2139 return VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmQueryWorkerOnCpu, 5,
2140 pUVM, pLookupRec, DBGFREGVALTYPE_END, &paRegs[0].Val, &paRegs[0].enmType);
2141 }
2142 *pcRegs = cRegs;
2143 return VERR_BUFFER_OVERFLOW;
2144 }
2145 return VERR_DBGF_REGISTER_NOT_FOUND;
2146
2147}
2148
2149
2150/// @todo VMMR3DECL(int) DBGFR3RegNmQueryBatch(PUVM pUVM,VMCPUID idDefCpu, DBGFREGENTRYNM paRegs, size_t cRegs);
2151
2152
2153/**
2154 * Gets the number of registers returned by DBGFR3RegNmQueryAll.
2155 *
2156 * @returns VBox status code.
2157 * @param pUVM The user mode VM handle.
2158 * @param pcRegs Where to return the register count.
2159 */
2160VMMR3DECL(int) DBGFR3RegNmQueryAllCount(PUVM pUVM, size_t *pcRegs)
2161{
2162 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2163 *pcRegs = pUVM->dbgf.s.cRegs;
2164 return VINF_SUCCESS;
2165}
2166
2167
2168/**
2169 * Pad register entries.
2170 *
2171 * @param paRegs The output array.
2172 * @param cRegs The size of the output array.
2173 * @param iReg The first register to pad.
2174 * @param cRegsToPad The number of registers to pad.
2175 */
2176static void dbgfR3RegNmQueryAllPadEntries(PDBGFREGENTRYNM paRegs, size_t cRegs, size_t iReg, size_t cRegsToPad)
2177{
2178 if (iReg < cRegs)
2179 {
2180 size_t iEndReg = iReg + cRegsToPad;
2181 if (iEndReg > cRegs)
2182 iEndReg = cRegs;
2183 while (iReg < iEndReg)
2184 {
2185 paRegs[iReg].pszName = NULL;
2186 paRegs[iReg].enmType = DBGFREGVALTYPE_END;
2187 paRegs[iReg].u.uInfo = 0;
2188 dbgfR3RegValClear(&paRegs[iReg].Val);
2189 iReg++;
2190 }
2191 }
2192}
2193
2194
2195/**
2196 * Query all registers in a set.
2197 *
2198 * @param pSet The set.
2199 * @param cRegsToQuery The number of registers to query.
2200 * @param paRegs The output array.
2201 * @param cRegs The size of the output array.
2202 */
2203static void dbgfR3RegNmQueryAllInSet(PCDBGFREGSET pSet, size_t cRegsToQuery, PDBGFREGENTRYNM paRegs, size_t cRegs)
2204{
2205 if (cRegsToQuery > pSet->cDescs)
2206 cRegsToQuery = pSet->cDescs;
2207 if (cRegsToQuery > cRegs)
2208 cRegsToQuery = cRegs;
2209
2210 for (size_t iReg = 0; iReg < cRegsToQuery; iReg++)
2211 {
2212 paRegs[iReg].enmType = pSet->paDescs[iReg].enmType;
2213 paRegs[iReg].pszName = pSet->paLookupRecs[iReg].Core.pszString;
2214 paRegs[iReg].u.uInfo = 0;
2215 paRegs[iReg].u.s.fMain = true;
2216 dbgfR3RegValClear(&paRegs[iReg].Val);
2217 int rc2 = pSet->paDescs[iReg].pfnGet(pSet->uUserArg.pv, &pSet->paDescs[iReg], &paRegs[iReg].Val);
2218 AssertMsg(rc2 == VINF_SUCCESS || rc2 == VERR_CPUM_RAISE_GP_0, ("rc2=%Rrc iReg=%u %s\n", rc2, iReg, paRegs[iReg].pszName));
2219 AssertRCSuccess(rc2);
2220 if (RT_FAILURE(rc2))
2221 dbgfR3RegValClear(&paRegs[iReg].Val);
2222 }
2223}
2224
2225
2226/**
2227 * @callback_method_impl{FNRTSTRSPACECALLBACK, Worker used by
2228 * dbgfR3RegNmQueryAllWorker}
2229 */
2230static DECLCALLBACK(int) dbgfR3RegNmQueryAllEnum(PRTSTRSPACECORE pStr, void *pvUser)
2231{
2232 PCDBGFREGSET pSet = (PCDBGFREGSET)pStr;
2233 if (pSet->enmType != DBGFREGSETTYPE_CPU)
2234 {
2235 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
2236 if (pArgs->iReg < pArgs->cRegs)
2237 dbgfR3RegNmQueryAllInSet(pSet, pSet->cDescs, &pArgs->paRegs[pArgs->iReg], pArgs->cRegs - pArgs->iReg);
2238 pArgs->iReg += pSet->cDescs;
2239 }
2240
2241 return 0;
2242}
2243
2244
2245/**
2246 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker used by DBGFR3RegNmQueryAll}
2247 */
2248static DECLCALLBACK(VBOXSTRICTRC) dbgfR3RegNmQueryAllWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
2249{
2250 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
2251 PDBGFREGENTRYNM paRegs = pArgs->paRegs;
2252 size_t const cRegs = pArgs->cRegs;
2253 PUVM pUVM = pVM->pUVM;
2254 PUVMCPU pUVCpu = pVCpu->pUVCpu;
2255
2256 DBGF_REG_DB_LOCK_READ(pUVM);
2257
2258 /*
2259 * My guest CPU registers.
2260 */
2261 size_t iCpuReg = pVCpu->idCpu * pUVM->dbgf.s.cPerCpuRegs;
2262 if (pUVCpu->dbgf.s.pGuestRegSet)
2263 {
2264 if (iCpuReg < cRegs)
2265 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pGuestRegSet, pUVM->dbgf.s.cPerCpuRegs, &paRegs[iCpuReg], cRegs - iCpuReg);
2266 }
2267 else
2268 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, pUVM->dbgf.s.cPerCpuRegs);
2269
2270 /*
2271 * My hypervisor CPU registers.
2272 */
2273 iCpuReg = pUVM->cCpus * pUVM->dbgf.s.cPerCpuRegs + pUVCpu->idCpu * pUVM->dbgf.s.cPerCpuHyperRegs;
2274 if (pUVCpu->dbgf.s.pHyperRegSet)
2275 {
2276 if (iCpuReg < cRegs)
2277 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pHyperRegSet, pUVM->dbgf.s.cPerCpuHyperRegs, &paRegs[iCpuReg], cRegs - iCpuReg);
2278 }
2279 else
2280 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, pUVM->dbgf.s.cPerCpuHyperRegs);
2281
2282 /*
2283 * The primary CPU does all the other registers.
2284 */
2285 if (pUVCpu->idCpu == 0)
2286 {
2287 pArgs->iReg = pUVM->cCpus * (pUVM->dbgf.s.cPerCpuRegs + pUVM->dbgf.s.cPerCpuHyperRegs);
2288 RTStrSpaceEnumerate(&pUVM->dbgf.s.RegSetSpace, dbgfR3RegNmQueryAllEnum, pArgs);
2289 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, pArgs->iReg, cRegs);
2290 }
2291
2292 DBGF_REG_DB_UNLOCK_READ(pUVM);
2293 return VINF_SUCCESS; /* Ignore errors. */
2294}
2295
2296
2297/**
2298 * Queries all register.
2299 *
2300 * @returns VBox status code.
2301 * @param pUVM The user mode VM handle.
2302 * @param paRegs The output register value array. The register
2303 * name string is read only and shall not be freed
2304 * or modified.
2305 * @param cRegs The number of entries in @a paRegs. The
2306 * correct size can be obtained by calling
2307 * DBGFR3RegNmQueryAllCount.
2308 */
2309VMMR3DECL(int) DBGFR3RegNmQueryAll(PUVM pUVM, PDBGFREGENTRYNM paRegs, size_t cRegs)
2310{
2311 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2312 PVM pVM = pUVM->pVM;
2313 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2314 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
2315 AssertReturn(cRegs > 0, VERR_OUT_OF_RANGE);
2316
2317 DBGFR3REGNMQUERYALLARGS Args;
2318 Args.paRegs = paRegs;
2319 Args.cRegs = cRegs;
2320
2321 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3RegNmQueryAllWorker, &Args);
2322}
2323
2324
2325/**
2326 * On CPU worker for the register modifications, used by DBGFR3RegNmSet.
2327 *
2328 * @returns VBox status code.
2329 *
2330 * @param pUVM The user mode VM handle.
2331 * @param pLookupRec The register lookup record. Maybe be modified,
2332 * so please pass a copy of the user's one.
2333 * @param pValue The new register value.
2334 * @param pMask Indicate which bits to modify.
2335 */
2336static DECLCALLBACK(int) dbgfR3RegNmSetWorkerOnCpu(PUVM pUVM, PDBGFREGLOOKUP pLookupRec,
2337 PCDBGFREGVAL pValue, PCDBGFREGVAL pMask)
2338{
2339 RT_NOREF_PV(pUVM);
2340 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
2341 if (pSubField && pSubField->pfnSet)
2342 return pSubField->pfnSet(pLookupRec->pSet->uUserArg.pv, pSubField, pValue->u128, pMask->u128);
2343 return pLookupRec->pDesc->pfnSet(pLookupRec->pSet->uUserArg.pv, pLookupRec->pDesc, pValue, pMask);
2344}
2345
2346
2347/**
2348 * Worker for the register setting.
2349 *
2350 * @returns VBox status code.
2351 * @retval VINF_SUCCESS
2352 * @retval VERR_INVALID_VM_HANDLE
2353 * @retval VERR_INVALID_CPU_ID
2354 * @retval VERR_DBGF_REGISTER_NOT_FOUND
2355 * @retval VERR_DBGF_UNSUPPORTED_CAST
2356 * @retval VINF_DBGF_TRUNCATED_REGISTER
2357 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
2358 *
2359 * @param pUVM The user mode VM handle.
2360 * @param idDefCpu The virtual CPU ID for the default CPU register
2361 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
2362 * @param pszReg The register to query.
2363 * @param pValue The value to set
2364 * @param enmType How to interpret the value in @a pValue.
2365 */
2366VMMR3DECL(int) DBGFR3RegNmSet(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType)
2367{
2368 /*
2369 * Validate input.
2370 */
2371 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2372 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2373 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2374 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
2375 AssertReturn(enmType > DBGFREGVALTYPE_INVALID && enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
2376 AssertPtrReturn(pValue, VERR_INVALID_PARAMETER);
2377
2378 /*
2379 * Resolve the register and check that it is writable.
2380 */
2381 bool fGuestRegs = true;
2382 if ((idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY)
2383 {
2384 fGuestRegs = false;
2385 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2386 }
2387 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
2388 if (pLookupRec)
2389 {
2390 PCDBGFREGDESC pDesc = pLookupRec->pDesc;
2391 PCDBGFREGSET pSet = pLookupRec->pSet;
2392 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
2393
2394 if ( !(pDesc->fFlags & DBGFREG_FLAGS_READ_ONLY)
2395 && (pSubField
2396 ? !(pSubField->fFlags & DBGFREGSUBFIELD_FLAGS_READ_ONLY)
2397 && (pSubField->pfnSet != NULL || pDesc->pfnSet != NULL)
2398 : pDesc->pfnSet != NULL) )
2399 {
2400 /*
2401 * Calculate the modification mask and cast the input value to the
2402 * type of the target register.
2403 */
2404 DBGFREGVAL Mask = DBGFREGVAL_INITIALIZE_ZERO;
2405 DBGFREGVAL Value = DBGFREGVAL_INITIALIZE_ZERO;
2406 switch (enmType)
2407 {
2408 case DBGFREGVALTYPE_U8:
2409 Value.u8 = pValue->u8;
2410 Mask.u8 = UINT8_MAX;
2411 break;
2412 case DBGFREGVALTYPE_U16:
2413 Value.u16 = pValue->u16;
2414 Mask.u16 = UINT16_MAX;
2415 break;
2416 case DBGFREGVALTYPE_U32:
2417 Value.u32 = pValue->u32;
2418 Mask.u32 = UINT32_MAX;
2419 break;
2420 case DBGFREGVALTYPE_U64:
2421 Value.u64 = pValue->u64;
2422 Mask.u64 = UINT64_MAX;
2423 break;
2424 case DBGFREGVALTYPE_U128:
2425 Value.u128 = pValue->u128;
2426 Mask.u128.s.Lo = UINT64_MAX;
2427 Mask.u128.s.Hi = UINT64_MAX;
2428 break;
2429 case DBGFREGVALTYPE_U256:
2430 Value.u256 = pValue->u256;
2431 Mask.u256.QWords.qw0 = UINT64_MAX;
2432 Mask.u256.QWords.qw1 = UINT64_MAX;
2433 Mask.u256.QWords.qw2 = UINT64_MAX;
2434 Mask.u256.QWords.qw3 = UINT64_MAX;
2435 break;
2436 case DBGFREGVALTYPE_U512:
2437 Value.u512 = pValue->u512;
2438 Mask.u512.QWords.qw0 = UINT64_MAX;
2439 Mask.u512.QWords.qw1 = UINT64_MAX;
2440 Mask.u512.QWords.qw2 = UINT64_MAX;
2441 Mask.u512.QWords.qw3 = UINT64_MAX;
2442 Mask.u512.QWords.qw4 = UINT64_MAX;
2443 Mask.u512.QWords.qw5 = UINT64_MAX;
2444 Mask.u512.QWords.qw6 = UINT64_MAX;
2445 Mask.u512.QWords.qw7 = UINT64_MAX;
2446 break;
2447 case DBGFREGVALTYPE_R80:
2448#ifdef RT_COMPILER_WITH_80BIT_LONG_DOUBLE
2449 Value.r80Ex.lrd = pValue->r80Ex.lrd;
2450#else
2451 Value.r80Ex.au64[0] = pValue->r80Ex.au64[0];
2452 Value.r80Ex.au16[4] = pValue->r80Ex.au16[4];
2453#endif
2454 Value.r80Ex.au64[0] = UINT64_MAX;
2455 Value.r80Ex.au16[4] = UINT16_MAX;
2456 break;
2457 case DBGFREGVALTYPE_DTR:
2458 Value.dtr.u32Limit = pValue->dtr.u32Limit;
2459 Value.dtr.u64Base = pValue->dtr.u64Base;
2460 Mask.dtr.u32Limit = UINT32_MAX;
2461 Mask.dtr.u64Base = UINT64_MAX;
2462 break;
2463 case DBGFREGVALTYPE_32BIT_HACK:
2464 case DBGFREGVALTYPE_END:
2465 case DBGFREGVALTYPE_INVALID:
2466 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
2467 }
2468
2469 int rc = VINF_SUCCESS;
2470 DBGFREGVALTYPE enmRegType = pDesc->enmType;
2471 if (pSubField)
2472 {
2473 unsigned const cBits = pSubField->cBits + pSubField->cShift;
2474 if (cBits <= 8)
2475 enmRegType = DBGFREGVALTYPE_U8;
2476 else if (cBits <= 16)
2477 enmRegType = DBGFREGVALTYPE_U16;
2478 else if (cBits <= 32)
2479 enmRegType = DBGFREGVALTYPE_U32;
2480 else if (cBits <= 64)
2481 enmRegType = DBGFREGVALTYPE_U64;
2482 else if (cBits <= 128)
2483 enmRegType = DBGFREGVALTYPE_U128;
2484 else if (cBits <= 256)
2485 enmRegType = DBGFREGVALTYPE_U256;
2486 else
2487 enmRegType = DBGFREGVALTYPE_U512;
2488 }
2489 else if (pLookupRec->pAlias)
2490 {
2491 /* Restrict the input to the size of the alias register. */
2492 DBGFREGVALTYPE enmAliasType = pLookupRec->pAlias->enmType;
2493 if (enmAliasType != enmType)
2494 {
2495 rc = dbgfR3RegValCast(&Value, enmType, enmAliasType);
2496 if (RT_FAILURE(rc))
2497 return rc;
2498 dbgfR3RegValCast(&Mask, enmType, enmAliasType);
2499 enmType = enmAliasType;
2500 }
2501 }
2502
2503 if (enmType != enmRegType)
2504 {
2505 int rc2 = dbgfR3RegValCast(&Value, enmType, enmRegType);
2506 if (RT_FAILURE(rc2))
2507 return rc2;
2508 if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
2509 rc2 = VINF_SUCCESS;
2510 dbgfR3RegValCast(&Mask, enmType, enmRegType);
2511 }
2512
2513 /*
2514 * Subfields needs some extra processing if there is no subfield
2515 * setter, since we'll be feeding it to the normal register setter
2516 * instead. The mask and value must be shifted and truncated to the
2517 * subfield position.
2518 */
2519 if (pSubField && !pSubField->pfnSet)
2520 {
2521 /* The shift factor is for displaying a subfield value
2522 2**cShift times larger than the stored value. We have
2523 to undo this before adjusting value and mask. */
2524 if (pSubField->cShift)
2525 {
2526 /* Warn about trunction of the lower bits that get
2527 shifted out below. */
2528 if (rc == VINF_SUCCESS)
2529 {
2530 DBGFREGVAL Value2 = Value;
2531 RTUInt128AssignAndNFirstBits(&Value2.u128, -pSubField->cShift);
2532 if (!RTUInt128BitAreAllClear(&Value2.u128))
2533 rc = VINF_DBGF_TRUNCATED_REGISTER;
2534 }
2535 RTUInt128AssignShiftRight(&Value.u128, pSubField->cShift);
2536 }
2537
2538 RTUInt128AssignAndNFirstBits(&Value.u128, pSubField->cBits);
2539 if (rc == VINF_SUCCESS && RTUInt128IsNotEqual(&Value.u128, &Value.u128))
2540 rc = VINF_DBGF_TRUNCATED_REGISTER;
2541 RTUInt128AssignAndNFirstBits(&Mask.u128, pSubField->cBits);
2542
2543 RTUInt128AssignShiftLeft(&Value.u128, pSubField->iFirstBit);
2544 RTUInt128AssignShiftLeft(&Mask.u128, pSubField->iFirstBit);
2545 }
2546
2547 /*
2548 * Do the actual work on an EMT.
2549 */
2550 if (pSet->enmType == DBGFREGSETTYPE_CPU)
2551 idDefCpu = pSet->uUserArg.pVCpu->idCpu;
2552 else if (idDefCpu != VMCPUID_ANY)
2553 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2554
2555 int rc2 = VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmSetWorkerOnCpu, 4,
2556 pUVM, pLookupRec, &Value, &Mask);
2557
2558 if (rc == VINF_SUCCESS || RT_FAILURE(rc2))
2559 rc = rc2;
2560 return rc;
2561 }
2562 return VERR_DBGF_READ_ONLY_REGISTER;
2563 }
2564 return VERR_DBGF_REGISTER_NOT_FOUND;
2565}
2566
2567
2568/**
2569 * Set a given set of registers.
2570 *
2571 * @returns VBox status code.
2572 * @retval VINF_SUCCESS
2573 * @retval VERR_INVALID_VM_HANDLE
2574 * @retval VERR_INVALID_CPU_ID
2575 * @retval VERR_DBGF_REGISTER_NOT_FOUND
2576 * @retval VERR_DBGF_UNSUPPORTED_CAST
2577 * @retval VINF_DBGF_TRUNCATED_REGISTER
2578 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
2579 *
2580 * @param pUVM The user mode VM handle.
2581 * @param idDefCpu The virtual CPU ID for the default CPU register
2582 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
2583 * @param paRegs The array of registers to set.
2584 * @param cRegs Number of registers in the array.
2585 *
2586 * @todo This is a _very_ lazy implementation by a lazy developer, some semantics
2587 * need to be figured out before the real implementation especially how and
2588 * when errors and informational status codes like VINF_DBGF_TRUNCATED_REGISTER
2589 * should be returned (think of an error right in the middle of the batch, should we
2590 * save the state and roll back?).
2591 */
2592VMMR3DECL(int) DBGFR3RegNmSetBatch(PUVM pUVM, VMCPUID idDefCpu, PCDBGFREGENTRYNM paRegs, size_t cRegs)
2593{
2594 /*
2595 * Validate input.
2596 */
2597 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2598 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2599 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2600 AssertPtrReturn(paRegs, VERR_INVALID_PARAMETER);
2601 AssertReturn(cRegs > 0, VERR_INVALID_PARAMETER);
2602
2603 for (uint32_t i = 0; i < cRegs; i++)
2604 {
2605 int rc = DBGFR3RegNmSet(pUVM, idDefCpu, paRegs[i].pszName, &paRegs[i].Val, paRegs[i].enmType);
2606 if (RT_FAILURE(rc))
2607 return rc;
2608 }
2609
2610 return VINF_SUCCESS;
2611}
2612
2613
2614/**
2615 * Internal worker for DBGFR3RegFormatValue, cbBuf is sufficent.
2616 *
2617 * @copydoc DBGFR3RegFormatValueEx
2618 */
2619DECLINLINE(ssize_t) dbgfR3RegFormatValueInt(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
2620 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
2621{
2622 switch (enmType)
2623 {
2624 case DBGFREGVALTYPE_U8:
2625 return RTStrFormatU8(pszBuf, cbBuf, pValue->u8, uBase, cchWidth, cchPrecision, fFlags);
2626 case DBGFREGVALTYPE_U16:
2627 return RTStrFormatU16(pszBuf, cbBuf, pValue->u16, uBase, cchWidth, cchPrecision, fFlags);
2628 case DBGFREGVALTYPE_U32:
2629 return RTStrFormatU32(pszBuf, cbBuf, pValue->u32, uBase, cchWidth, cchPrecision, fFlags);
2630 case DBGFREGVALTYPE_U64:
2631 return RTStrFormatU64(pszBuf, cbBuf, pValue->u64, uBase, cchWidth, cchPrecision, fFlags);
2632 case DBGFREGVALTYPE_U128:
2633 return RTStrFormatU128(pszBuf, cbBuf, &pValue->u128, uBase, cchWidth, cchPrecision, fFlags);
2634 case DBGFREGVALTYPE_U256:
2635 return RTStrFormatU256(pszBuf, cbBuf, &pValue->u256, uBase, cchWidth, cchPrecision, fFlags);
2636 case DBGFREGVALTYPE_U512:
2637 return RTStrFormatU512(pszBuf, cbBuf, &pValue->u512, uBase, cchWidth, cchPrecision, fFlags);
2638 case DBGFREGVALTYPE_R80:
2639 return RTStrFormatR80u2(pszBuf, cbBuf, &pValue->r80Ex, cchWidth, cchPrecision, fFlags);
2640 case DBGFREGVALTYPE_DTR:
2641 {
2642 ssize_t cch = RTStrFormatU64(pszBuf, cbBuf, pValue->dtr.u64Base,
2643 16, 2+16, 0, RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
2644 AssertReturn(cch > 0, VERR_DBGF_REG_IPE_1);
2645 pszBuf[cch++] = ':';
2646 cch += RTStrFormatU64(&pszBuf[cch], cbBuf - cch, pValue->dtr.u32Limit,
2647 16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
2648 return cch;
2649 }
2650
2651 case DBGFREGVALTYPE_32BIT_HACK:
2652 case DBGFREGVALTYPE_END:
2653 case DBGFREGVALTYPE_INVALID:
2654 break;
2655 /* no default, want gcc warnings */
2656 }
2657
2658 RTStrPrintf(pszBuf, cbBuf, "!enmType=%d!", enmType);
2659 return VERR_DBGF_REG_IPE_2;
2660}
2661
2662
2663/**
2664 * Format a register value, extended version.
2665 *
2666 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
2667 * @param pszBuf The output buffer.
2668 * @param cbBuf The size of the output buffer.
2669 * @param pValue The value to format.
2670 * @param enmType The value type.
2671 * @param uBase The base (ignored if not applicable).
2672 * @param cchWidth The width if RTSTR_F_WIDTH is set, otherwise
2673 * ignored.
2674 * @param cchPrecision The width if RTSTR_F_PRECISION is set, otherwise
2675 * ignored.
2676 * @param fFlags String formatting flags, RTSTR_F_XXX.
2677 */
2678VMMR3DECL(ssize_t) DBGFR3RegFormatValueEx(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
2679 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
2680{
2681 /*
2682 * Format to temporary buffer using worker shared with dbgfR3RegPrintfCbFormatNormal.
2683 */
2684 char szTmp[160];
2685 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), pValue, enmType, uBase, cchWidth, cchPrecision, fFlags);
2686 if (cchOutput > 0)
2687 {
2688 if ((size_t)cchOutput < cbBuf)
2689 memcpy(pszBuf, szTmp, cchOutput + 1);
2690 else
2691 {
2692 if (cbBuf)
2693 {
2694 memcpy(pszBuf, szTmp, cbBuf - 1); /* (parfait is wrong about out of bound read here) */
2695 pszBuf[cbBuf - 1] = '\0';
2696 }
2697 cchOutput = VERR_BUFFER_OVERFLOW;
2698 }
2699 }
2700 return cchOutput;
2701}
2702
2703
2704/**
2705 * Format a register value as hexadecimal and with default width according to
2706 * the type.
2707 *
2708 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
2709 * @param pszBuf The output buffer.
2710 * @param cbBuf The size of the output buffer.
2711 * @param pValue The value to format.
2712 * @param enmType The value type.
2713 * @param fSpecial Same as RTSTR_F_SPECIAL.
2714 */
2715VMMR3DECL(ssize_t) DBGFR3RegFormatValue(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType, bool fSpecial)
2716{
2717 int cchWidth = 0;
2718 switch (enmType)
2719 {
2720 case DBGFREGVALTYPE_U8: cchWidth = 2 + fSpecial*2; break;
2721 case DBGFREGVALTYPE_U16: cchWidth = 4 + fSpecial*2; break;
2722 case DBGFREGVALTYPE_U32: cchWidth = 8 + fSpecial*2; break;
2723 case DBGFREGVALTYPE_U64: cchWidth = 16 + fSpecial*2; break;
2724 case DBGFREGVALTYPE_U128: cchWidth = 32 + fSpecial*2; break;
2725 case DBGFREGVALTYPE_U256: cchWidth = 64 + fSpecial*2; break;
2726 case DBGFREGVALTYPE_U512: cchWidth = 128 + fSpecial*2; break;
2727 case DBGFREGVALTYPE_R80: cchWidth = 0; break;
2728 case DBGFREGVALTYPE_DTR: cchWidth = 16+1+4 + fSpecial*2; break;
2729
2730 case DBGFREGVALTYPE_32BIT_HACK:
2731 case DBGFREGVALTYPE_END:
2732 case DBGFREGVALTYPE_INVALID:
2733 break;
2734 /* no default, want gcc warnings */
2735 }
2736 uint32_t fFlags = RTSTR_F_ZEROPAD;
2737 if (fSpecial)
2738 fFlags |= RTSTR_F_SPECIAL;
2739 if (cchWidth != 0)
2740 fFlags |= RTSTR_F_WIDTH;
2741 return DBGFR3RegFormatValueEx(pszBuf, cbBuf, pValue, enmType, 16, cchWidth, 0, fFlags);
2742}
2743
2744
2745/**
2746 * Format a register using special hacks as well as sub-field specifications
2747 * (the latter isn't implemented yet).
2748 */
2749static size_t
2750dbgfR3RegPrintfCbFormatField(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2751 PCDBGFREGLOOKUP pLookupRec, int cchWidth, int cchPrecision, unsigned fFlags)
2752{
2753 char szTmp[160];
2754
2755 NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
2756
2757 /*
2758 * Retrieve the register value.
2759 */
2760 DBGFREGVAL Value;
2761 DBGFREGVALTYPE enmType;
2762 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2763 if (RT_FAILURE(rc))
2764 {
2765 ssize_t cchDefine = RTErrQueryDefine(rc, szTmp, sizeof(szTmp), true /*fFailIfUnknown*/);
2766 if (cchDefine <= 0)
2767 cchDefine = RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc);
2768 return pfnOutput(pvArgOutput, szTmp, cchDefine);
2769 }
2770
2771 char *psz = szTmp;
2772
2773 /*
2774 * Special case: Format eflags.
2775 */
2776 if ( pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU
2777 && pLookupRec->pDesc->enmReg == DBGFREG_RFLAGS
2778 && pLookupRec->pSubField == NULL)
2779 {
2780 rc = dbgfR3RegValCast(&Value, enmType, DBGFREGVALTYPE_U32);
2781 AssertRC(rc);
2782 uint32_t const efl = Value.u32;
2783
2784 /* the iopl */
2785 psz += RTStrPrintf(psz, sizeof(szTmp) / 2, "iopl=%u ", X86_EFL_GET_IOPL(efl));
2786
2787 /* add flags */
2788 static const struct
2789 {
2790 const char *pszSet;
2791 const char *pszClear;
2792 uint32_t fFlag;
2793 } aFlags[] =
2794 {
2795 { "vip",NULL, X86_EFL_VIP },
2796 { "vif",NULL, X86_EFL_VIF },
2797 { "ac", NULL, X86_EFL_AC },
2798 { "vm", NULL, X86_EFL_VM },
2799 { "rf", NULL, X86_EFL_RF },
2800 { "nt", NULL, X86_EFL_NT },
2801 { "ov", "nv", X86_EFL_OF },
2802 { "dn", "up", X86_EFL_DF },
2803 { "ei", "di", X86_EFL_IF },
2804 { "tf", NULL, X86_EFL_TF },
2805 { "ng", "pl", X86_EFL_SF },
2806 { "zr", "nz", X86_EFL_ZF },
2807 { "ac", "na", X86_EFL_AF },
2808 { "po", "pe", X86_EFL_PF },
2809 { "cy", "nc", X86_EFL_CF },
2810 };
2811 for (unsigned i = 0; i < RT_ELEMENTS(aFlags); i++)
2812 {
2813 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
2814 if (pszAdd)
2815 {
2816 *psz++ = *pszAdd++;
2817 *psz++ = *pszAdd++;
2818 if (*pszAdd)
2819 *psz++ = *pszAdd++;
2820 *psz++ = ' ';
2821 }
2822 }
2823
2824 /* drop trailing space */
2825 psz--;
2826 }
2827 else
2828 {
2829 /*
2830 * General case.
2831 */
2832 AssertMsgFailed(("Not implemented: %s\n", pLookupRec->Core.pszString));
2833 return pfnOutput(pvArgOutput, pLookupRec->Core.pszString, pLookupRec->Core.cchString);
2834 }
2835
2836 /* Output the string. */
2837 return pfnOutput(pvArgOutput, szTmp, psz - &szTmp[0]);
2838}
2839
2840
2841/**
2842 * Formats a register having parsed up to the register name.
2843 */
2844static size_t
2845dbgfR3RegPrintfCbFormatNormal(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2846 PCDBGFREGLOOKUP pLookupRec, unsigned uBase, int cchWidth, int cchPrecision, unsigned fFlags)
2847{
2848 char szTmp[160];
2849
2850 /*
2851 * Get the register value.
2852 */
2853 DBGFREGVAL Value;
2854 DBGFREGVALTYPE enmType;
2855 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2856 if (RT_FAILURE(rc))
2857 {
2858 ssize_t cchDefine = RTErrQueryDefine(rc, szTmp, sizeof(szTmp), true /*fFailIfUnknown*/);
2859 if (cchDefine <= 0)
2860 cchDefine = RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc);
2861 return pfnOutput(pvArgOutput, szTmp, cchDefine);
2862 }
2863
2864 /*
2865 * Format the value.
2866 */
2867 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), &Value, enmType, uBase, cchWidth, cchPrecision, fFlags);
2868 if (RT_UNLIKELY(cchOutput <= 0))
2869 {
2870 AssertFailed();
2871 return pfnOutput(pvArgOutput, "internal-error", sizeof("internal-error") - 1);
2872 }
2873 return pfnOutput(pvArgOutput, szTmp, cchOutput);
2874}
2875
2876
2877/**
2878 * @callback_method_impl{FNSTRFORMAT}
2879 */
2880static DECLCALLBACK(size_t)
2881dbgfR3RegPrintfCbFormat(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2882 const char **ppszFormat, va_list *pArgs, int cchWidth,
2883 int cchPrecision, unsigned fFlags, char chArgSize)
2884{
2885 NOREF(pArgs); NOREF(chArgSize);
2886
2887 /*
2888 * Parse the format type and hand the job to the appropriate worker.
2889 */
2890 PDBGFR3REGPRINTFARGS pThis = (PDBGFR3REGPRINTFARGS)pvArg;
2891 const char *pszFormat = *ppszFormat;
2892 if ( pszFormat[0] != 'V'
2893 || pszFormat[1] != 'R')
2894 {
2895 AssertMsgFailed(("'%s'\n", pszFormat));
2896 return 0;
2897 }
2898 unsigned offCurly = 2;
2899 if (pszFormat[offCurly] != '{')
2900 {
2901 AssertMsgReturn(pszFormat[offCurly], ("'%s'\n", pszFormat), 0);
2902 offCurly++;
2903 AssertMsgReturn(pszFormat[offCurly] == '{', ("'%s'\n", pszFormat), 0);
2904 }
2905 const char *pachReg = &pszFormat[offCurly + 1];
2906
2907 /*
2908 * The end and length of the register.
2909 */
2910 const char *pszEnd = strchr(pachReg, '}');
2911 AssertMsgReturn(pszEnd, ("Missing closing curly bracket: '%s'\n", pszFormat), 0);
2912 size_t const cchReg = pszEnd - pachReg;
2913
2914 /*
2915 * Look up the register - same as dbgfR3RegResolve, except for locking and
2916 * input string termination.
2917 */
2918 PRTSTRSPACE pRegSpace = &pThis->pUVM->dbgf.s.RegSpace;
2919 /* Try looking up the name without any case folding or cpu prefixing. */
2920 PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGetN(pRegSpace, pachReg, cchReg);
2921 if (!pLookupRec)
2922 {
2923 /* Lower case it and try again. */
2924 char szName[DBGF_REG_MAX_NAME * 4 + 16];
2925 ssize_t cchFolded = dbgfR3RegCopyToLower(pachReg, cchReg, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
2926 if (cchFolded > 0)
2927 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2928 if ( !pLookupRec
2929 && cchFolded >= 0
2930 && pThis->idCpu != VMCPUID_ANY)
2931 {
2932 /* Prefix it with the specified CPU set. */
2933 size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), pThis->fGuestRegs ? "cpu%u." : "hypercpu%u.", pThis->idCpu);
2934 dbgfR3RegCopyToLower(pachReg, cchReg, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
2935 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2936 }
2937 }
2938 AssertMsgReturn(pLookupRec, ("'%s'\n", pszFormat), 0);
2939 AssertMsgReturn( pLookupRec->pSet->enmType != DBGFREGSETTYPE_CPU
2940 || pLookupRec->pSet->uUserArg.pVCpu->idCpu == pThis->idCpu,
2941 ("'%s' idCpu=%u, pSet/cpu=%u\n", pszFormat, pThis->idCpu, pLookupRec->pSet->uUserArg.pVCpu->idCpu),
2942 0);
2943
2944 /*
2945 * Commit the parsed format string. Up to this point it is nice to know
2946 * what register lookup failed and such, so we've delayed comitting.
2947 */
2948 *ppszFormat = pszEnd + 1;
2949
2950 /*
2951 * Call the responsible worker.
2952 */
2953 switch (pszFormat[offCurly - 1])
2954 {
2955 case 'R': /* %VR{} */
2956 case 'X': /* %VRX{} */
2957 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2958 16, cchWidth, cchPrecision, fFlags);
2959 case 'U':
2960 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2961 10, cchWidth, cchPrecision, fFlags);
2962 case 'O':
2963 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2964 8, cchWidth, cchPrecision, fFlags);
2965 case 'B':
2966 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2967 2, cchWidth, cchPrecision, fFlags);
2968 case 'F':
2969 return dbgfR3RegPrintfCbFormatField(pThis, pfnOutput, pvArgOutput, pLookupRec, cchWidth, cchPrecision, fFlags);
2970 default:
2971 AssertFailed();
2972 return 0;
2973 }
2974}
2975
2976
2977
2978/**
2979 * @callback_method_impl{FNRTSTROUTPUT}
2980 */
2981static DECLCALLBACK(size_t)
2982dbgfR3RegPrintfCbOutput(void *pvArg, const char *pachChars, size_t cbChars)
2983{
2984 PDBGFR3REGPRINTFARGS pArgs = (PDBGFR3REGPRINTFARGS)pvArg;
2985 size_t cbToCopy = cbChars;
2986 if (cbToCopy >= pArgs->cchLeftBuf)
2987 {
2988 if (RT_SUCCESS(pArgs->rc))
2989 pArgs->rc = VERR_BUFFER_OVERFLOW;
2990 cbToCopy = pArgs->cchLeftBuf;
2991 }
2992 if (cbToCopy > 0)
2993 {
2994 memcpy(&pArgs->pszBuf[pArgs->offBuf], pachChars, cbToCopy);
2995 pArgs->offBuf += cbToCopy;
2996 pArgs->cchLeftBuf -= cbToCopy;
2997 pArgs->pszBuf[pArgs->offBuf] = '\0';
2998 }
2999 return cbToCopy;
3000}
3001
3002
3003/**
3004 * On CPU worker for the register formatting, used by DBGFR3RegPrintfV.
3005 *
3006 * @returns VBox status code.
3007 *
3008 * @param pArgs The argument package and state.
3009 */
3010static DECLCALLBACK(int) dbgfR3RegPrintfWorkerOnCpu(PDBGFR3REGPRINTFARGS pArgs)
3011{
3012 DBGF_REG_DB_LOCK_READ(pArgs->pUVM);
3013 RTStrFormatV(dbgfR3RegPrintfCbOutput, pArgs, dbgfR3RegPrintfCbFormat, pArgs, pArgs->pszFormat, pArgs->va);
3014 DBGF_REG_DB_UNLOCK_READ(pArgs->pUVM);
3015 return pArgs->rc;
3016}
3017
3018
3019/**
3020 * Format a registers.
3021 *
3022 * This is restricted to registers from one CPU, that specified by @a idCpu.
3023 *
3024 * @returns VBox status code.
3025 * @param pUVM The user mode VM handle.
3026 * @param idCpu The CPU ID of any CPU registers that may be
3027 * printed, pass VMCPUID_ANY if not applicable.
3028 * @param pszBuf The output buffer.
3029 * @param cbBuf The size of the output buffer.
3030 * @param pszFormat The format string. Register names are given by
3031 * %VR{name}, they take no arguments.
3032 * @param va Other format arguments.
3033 */
3034VMMR3DECL(int) DBGFR3RegPrintfV(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, va_list va)
3035{
3036 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
3037 AssertReturn(cbBuf > 0, VERR_BUFFER_OVERFLOW);
3038 *pszBuf = '\0';
3039
3040 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3041 AssertReturn((idCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
3042 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
3043
3044 /*
3045 * Set up an argument package and execute the formatting on the
3046 * specified CPU.
3047 */
3048 DBGFR3REGPRINTFARGS Args;
3049 Args.pUVM = pUVM;
3050 Args.idCpu = idCpu != VMCPUID_ANY ? idCpu & ~DBGFREG_HYPER_VMCPUID : idCpu;
3051 Args.fGuestRegs = idCpu != VMCPUID_ANY && !(idCpu & DBGFREG_HYPER_VMCPUID);
3052 Args.pszBuf = pszBuf;
3053 Args.pszFormat = pszFormat;
3054 va_copy(Args.va, va);
3055 Args.offBuf = 0;
3056 Args.cchLeftBuf = cbBuf - 1;
3057 Args.rc = VINF_SUCCESS;
3058 int rc = VMR3ReqPriorityCallWaitU(pUVM, Args.idCpu, (PFNRT)dbgfR3RegPrintfWorkerOnCpu, 1, &Args);
3059 va_end(Args.va);
3060 return rc;
3061}
3062
3063
3064/**
3065 * Format a registers.
3066 *
3067 * This is restricted to registers from one CPU, that specified by @a idCpu.
3068 *
3069 * @returns VBox status code.
3070 * @param pUVM The user mode VM handle.
3071 * @param idCpu The CPU ID of any CPU registers that may be
3072 * printed, pass VMCPUID_ANY if not applicable.
3073 * @param pszBuf The output buffer.
3074 * @param cbBuf The size of the output buffer.
3075 * @param pszFormat The format string. Register names are given by
3076 * %VR{name}, %VRU{name}, %VRO{name} and
3077 * %VRB{name}, which are hexadecimal, (unsigned)
3078 * decimal, octal and binary representation. None
3079 * of these types takes any arguments.
3080 * @param ... Other format arguments.
3081 */
3082VMMR3DECL(int) DBGFR3RegPrintf(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, ...)
3083{
3084 va_list va;
3085 va_start(va, pszFormat);
3086 int rc = DBGFR3RegPrintfV(pUVM, idCpu, pszBuf, cbBuf, pszFormat, va);
3087 va_end(va);
3088 return rc;
3089}
3090
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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