VirtualBox

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

Last change on this file since 107307 was 107227, checked in by vboxsync, 2 months ago

VMM: Cleaning up ARMv8 / x86 split. jiraref:VBP-1470

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 116.5 KB
Line 
1/* $Id: DBGFReg.cpp 107227 2024-12-04 15:20:14Z 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.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_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 AssertRCSuccess(rc2);
2219 if (RT_FAILURE(rc2))
2220 dbgfR3RegValClear(&paRegs[iReg].Val);
2221 }
2222}
2223
2224
2225/**
2226 * @callback_method_impl{FNRTSTRSPACECALLBACK, Worker used by
2227 * dbgfR3RegNmQueryAllWorker}
2228 */
2229static DECLCALLBACK(int) dbgfR3RegNmQueryAllEnum(PRTSTRSPACECORE pStr, void *pvUser)
2230{
2231 PCDBGFREGSET pSet = (PCDBGFREGSET)pStr;
2232 if (pSet->enmType != DBGFREGSETTYPE_CPU)
2233 {
2234 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
2235 if (pArgs->iReg < pArgs->cRegs)
2236 dbgfR3RegNmQueryAllInSet(pSet, pSet->cDescs, &pArgs->paRegs[pArgs->iReg], pArgs->cRegs - pArgs->iReg);
2237 pArgs->iReg += pSet->cDescs;
2238 }
2239
2240 return 0;
2241}
2242
2243
2244/**
2245 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker used by DBGFR3RegNmQueryAll}
2246 */
2247static DECLCALLBACK(VBOXSTRICTRC) dbgfR3RegNmQueryAllWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
2248{
2249 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
2250 PDBGFREGENTRYNM paRegs = pArgs->paRegs;
2251 size_t const cRegs = pArgs->cRegs;
2252 PUVM pUVM = pVM->pUVM;
2253 PUVMCPU pUVCpu = pVCpu->pUVCpu;
2254
2255 DBGF_REG_DB_LOCK_READ(pUVM);
2256
2257 /*
2258 * My guest CPU registers.
2259 */
2260 size_t iCpuReg = pVCpu->idCpu * pUVM->dbgf.s.cPerCpuRegs;
2261 if (pUVCpu->dbgf.s.pGuestRegSet)
2262 {
2263 if (iCpuReg < cRegs)
2264 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pGuestRegSet, pUVM->dbgf.s.cPerCpuRegs, &paRegs[iCpuReg], cRegs - iCpuReg);
2265 }
2266 else
2267 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, pUVM->dbgf.s.cPerCpuRegs);
2268
2269 /*
2270 * My hypervisor CPU registers.
2271 */
2272 iCpuReg = pUVM->cCpus * pUVM->dbgf.s.cPerCpuRegs + pUVCpu->idCpu * pUVM->dbgf.s.cPerCpuHyperRegs;
2273 if (pUVCpu->dbgf.s.pHyperRegSet)
2274 {
2275 if (iCpuReg < cRegs)
2276 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pHyperRegSet, pUVM->dbgf.s.cPerCpuHyperRegs, &paRegs[iCpuReg], cRegs - iCpuReg);
2277 }
2278 else
2279 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, pUVM->dbgf.s.cPerCpuHyperRegs);
2280
2281 /*
2282 * The primary CPU does all the other registers.
2283 */
2284 if (pUVCpu->idCpu == 0)
2285 {
2286 pArgs->iReg = pUVM->cCpus * (pUVM->dbgf.s.cPerCpuRegs + pUVM->dbgf.s.cPerCpuHyperRegs);
2287 RTStrSpaceEnumerate(&pUVM->dbgf.s.RegSetSpace, dbgfR3RegNmQueryAllEnum, pArgs);
2288 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, pArgs->iReg, cRegs);
2289 }
2290
2291 DBGF_REG_DB_UNLOCK_READ(pUVM);
2292 return VINF_SUCCESS; /* Ignore errors. */
2293}
2294
2295
2296/**
2297 * Queries all register.
2298 *
2299 * @returns VBox status code.
2300 * @param pUVM The user mode VM handle.
2301 * @param paRegs The output register value array. The register
2302 * name string is read only and shall not be freed
2303 * or modified.
2304 * @param cRegs The number of entries in @a paRegs. The
2305 * correct size can be obtained by calling
2306 * DBGFR3RegNmQueryAllCount.
2307 */
2308VMMR3DECL(int) DBGFR3RegNmQueryAll(PUVM pUVM, PDBGFREGENTRYNM paRegs, size_t cRegs)
2309{
2310 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2311 PVM pVM = pUVM->pVM;
2312 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2313 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
2314 AssertReturn(cRegs > 0, VERR_OUT_OF_RANGE);
2315
2316 DBGFR3REGNMQUERYALLARGS Args;
2317 Args.paRegs = paRegs;
2318 Args.cRegs = cRegs;
2319
2320 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3RegNmQueryAllWorker, &Args);
2321}
2322
2323
2324/**
2325 * On CPU worker for the register modifications, used by DBGFR3RegNmSet.
2326 *
2327 * @returns VBox status code.
2328 *
2329 * @param pUVM The user mode VM handle.
2330 * @param pLookupRec The register lookup record. Maybe be modified,
2331 * so please pass a copy of the user's one.
2332 * @param pValue The new register value.
2333 * @param pMask Indicate which bits to modify.
2334 */
2335static DECLCALLBACK(int) dbgfR3RegNmSetWorkerOnCpu(PUVM pUVM, PDBGFREGLOOKUP pLookupRec,
2336 PCDBGFREGVAL pValue, PCDBGFREGVAL pMask)
2337{
2338 RT_NOREF_PV(pUVM);
2339 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
2340 if (pSubField && pSubField->pfnSet)
2341 return pSubField->pfnSet(pLookupRec->pSet->uUserArg.pv, pSubField, pValue->u128, pMask->u128);
2342 return pLookupRec->pDesc->pfnSet(pLookupRec->pSet->uUserArg.pv, pLookupRec->pDesc, pValue, pMask);
2343}
2344
2345
2346/**
2347 * Worker for the register setting.
2348 *
2349 * @returns VBox status code.
2350 * @retval VINF_SUCCESS
2351 * @retval VERR_INVALID_VM_HANDLE
2352 * @retval VERR_INVALID_CPU_ID
2353 * @retval VERR_DBGF_REGISTER_NOT_FOUND
2354 * @retval VERR_DBGF_UNSUPPORTED_CAST
2355 * @retval VINF_DBGF_TRUNCATED_REGISTER
2356 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
2357 *
2358 * @param pUVM The user mode VM handle.
2359 * @param idDefCpu The virtual CPU ID for the default CPU register
2360 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
2361 * @param pszReg The register to query.
2362 * @param pValue The value to set
2363 * @param enmType How to interpret the value in @a pValue.
2364 */
2365VMMR3DECL(int) DBGFR3RegNmSet(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType)
2366{
2367 /*
2368 * Validate input.
2369 */
2370 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2371 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2372 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2373 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
2374 AssertReturn(enmType > DBGFREGVALTYPE_INVALID && enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
2375 AssertPtrReturn(pValue, VERR_INVALID_PARAMETER);
2376
2377 /*
2378 * Resolve the register and check that it is writable.
2379 */
2380 bool fGuestRegs = true;
2381 if ((idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY)
2382 {
2383 fGuestRegs = false;
2384 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2385 }
2386 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
2387 if (pLookupRec)
2388 {
2389 PCDBGFREGDESC pDesc = pLookupRec->pDesc;
2390 PCDBGFREGSET pSet = pLookupRec->pSet;
2391 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
2392
2393 if ( !(pDesc->fFlags & DBGFREG_FLAGS_READ_ONLY)
2394 && (pSubField
2395 ? !(pSubField->fFlags & DBGFREGSUBFIELD_FLAGS_READ_ONLY)
2396 && (pSubField->pfnSet != NULL || pDesc->pfnSet != NULL)
2397 : pDesc->pfnSet != NULL) )
2398 {
2399 /*
2400 * Calculate the modification mask and cast the input value to the
2401 * type of the target register.
2402 */
2403 DBGFREGVAL Mask = DBGFREGVAL_INITIALIZE_ZERO;
2404 DBGFREGVAL Value = DBGFREGVAL_INITIALIZE_ZERO;
2405 switch (enmType)
2406 {
2407 case DBGFREGVALTYPE_U8:
2408 Value.u8 = pValue->u8;
2409 Mask.u8 = UINT8_MAX;
2410 break;
2411 case DBGFREGVALTYPE_U16:
2412 Value.u16 = pValue->u16;
2413 Mask.u16 = UINT16_MAX;
2414 break;
2415 case DBGFREGVALTYPE_U32:
2416 Value.u32 = pValue->u32;
2417 Mask.u32 = UINT32_MAX;
2418 break;
2419 case DBGFREGVALTYPE_U64:
2420 Value.u64 = pValue->u64;
2421 Mask.u64 = UINT64_MAX;
2422 break;
2423 case DBGFREGVALTYPE_U128:
2424 Value.u128 = pValue->u128;
2425 Mask.u128.s.Lo = UINT64_MAX;
2426 Mask.u128.s.Hi = UINT64_MAX;
2427 break;
2428 case DBGFREGVALTYPE_U256:
2429 Value.u256 = pValue->u256;
2430 Mask.u256.QWords.qw0 = UINT64_MAX;
2431 Mask.u256.QWords.qw1 = UINT64_MAX;
2432 Mask.u256.QWords.qw2 = UINT64_MAX;
2433 Mask.u256.QWords.qw3 = UINT64_MAX;
2434 break;
2435 case DBGFREGVALTYPE_U512:
2436 Value.u512 = pValue->u512;
2437 Mask.u512.QWords.qw0 = UINT64_MAX;
2438 Mask.u512.QWords.qw1 = UINT64_MAX;
2439 Mask.u512.QWords.qw2 = UINT64_MAX;
2440 Mask.u512.QWords.qw3 = UINT64_MAX;
2441 Mask.u512.QWords.qw4 = UINT64_MAX;
2442 Mask.u512.QWords.qw5 = UINT64_MAX;
2443 Mask.u512.QWords.qw6 = UINT64_MAX;
2444 Mask.u512.QWords.qw7 = UINT64_MAX;
2445 break;
2446 case DBGFREGVALTYPE_R80:
2447#ifdef RT_COMPILER_WITH_80BIT_LONG_DOUBLE
2448 Value.r80Ex.lrd = pValue->r80Ex.lrd;
2449#else
2450 Value.r80Ex.au64[0] = pValue->r80Ex.au64[0];
2451 Value.r80Ex.au16[4] = pValue->r80Ex.au16[4];
2452#endif
2453 Value.r80Ex.au64[0] = UINT64_MAX;
2454 Value.r80Ex.au16[4] = UINT16_MAX;
2455 break;
2456 case DBGFREGVALTYPE_DTR:
2457 Value.dtr.u32Limit = pValue->dtr.u32Limit;
2458 Value.dtr.u64Base = pValue->dtr.u64Base;
2459 Mask.dtr.u32Limit = UINT32_MAX;
2460 Mask.dtr.u64Base = UINT64_MAX;
2461 break;
2462 case DBGFREGVALTYPE_32BIT_HACK:
2463 case DBGFREGVALTYPE_END:
2464 case DBGFREGVALTYPE_INVALID:
2465 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
2466 }
2467
2468 int rc = VINF_SUCCESS;
2469 DBGFREGVALTYPE enmRegType = pDesc->enmType;
2470 if (pSubField)
2471 {
2472 unsigned const cBits = pSubField->cBits + pSubField->cShift;
2473 if (cBits <= 8)
2474 enmRegType = DBGFREGVALTYPE_U8;
2475 else if (cBits <= 16)
2476 enmRegType = DBGFREGVALTYPE_U16;
2477 else if (cBits <= 32)
2478 enmRegType = DBGFREGVALTYPE_U32;
2479 else if (cBits <= 64)
2480 enmRegType = DBGFREGVALTYPE_U64;
2481 else if (cBits <= 128)
2482 enmRegType = DBGFREGVALTYPE_U128;
2483 else if (cBits <= 256)
2484 enmRegType = DBGFREGVALTYPE_U256;
2485 else
2486 enmRegType = DBGFREGVALTYPE_U512;
2487 }
2488 else if (pLookupRec->pAlias)
2489 {
2490 /* Restrict the input to the size of the alias register. */
2491 DBGFREGVALTYPE enmAliasType = pLookupRec->pAlias->enmType;
2492 if (enmAliasType != enmType)
2493 {
2494 rc = dbgfR3RegValCast(&Value, enmType, enmAliasType);
2495 if (RT_FAILURE(rc))
2496 return rc;
2497 dbgfR3RegValCast(&Mask, enmType, enmAliasType);
2498 enmType = enmAliasType;
2499 }
2500 }
2501
2502 if (enmType != enmRegType)
2503 {
2504 int rc2 = dbgfR3RegValCast(&Value, enmType, enmRegType);
2505 if (RT_FAILURE(rc2))
2506 return rc2;
2507 if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
2508 rc2 = VINF_SUCCESS;
2509 dbgfR3RegValCast(&Mask, enmType, enmRegType);
2510 }
2511
2512 /*
2513 * Subfields needs some extra processing if there is no subfield
2514 * setter, since we'll be feeding it to the normal register setter
2515 * instead. The mask and value must be shifted and truncated to the
2516 * subfield position.
2517 */
2518 if (pSubField && !pSubField->pfnSet)
2519 {
2520 /* The shift factor is for displaying a subfield value
2521 2**cShift times larger than the stored value. We have
2522 to undo this before adjusting value and mask. */
2523 if (pSubField->cShift)
2524 {
2525 /* Warn about trunction of the lower bits that get
2526 shifted out below. */
2527 if (rc == VINF_SUCCESS)
2528 {
2529 DBGFREGVAL Value2 = Value;
2530 RTUInt128AssignAndNFirstBits(&Value2.u128, -pSubField->cShift);
2531 if (!RTUInt128BitAreAllClear(&Value2.u128))
2532 rc = VINF_DBGF_TRUNCATED_REGISTER;
2533 }
2534 RTUInt128AssignShiftRight(&Value.u128, pSubField->cShift);
2535 }
2536
2537 RTUInt128AssignAndNFirstBits(&Value.u128, pSubField->cBits);
2538 if (rc == VINF_SUCCESS && RTUInt128IsNotEqual(&Value.u128, &Value.u128))
2539 rc = VINF_DBGF_TRUNCATED_REGISTER;
2540 RTUInt128AssignAndNFirstBits(&Mask.u128, pSubField->cBits);
2541
2542 RTUInt128AssignShiftLeft(&Value.u128, pSubField->iFirstBit);
2543 RTUInt128AssignShiftLeft(&Mask.u128, pSubField->iFirstBit);
2544 }
2545
2546 /*
2547 * Do the actual work on an EMT.
2548 */
2549 if (pSet->enmType == DBGFREGSETTYPE_CPU)
2550 idDefCpu = pSet->uUserArg.pVCpu->idCpu;
2551 else if (idDefCpu != VMCPUID_ANY)
2552 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
2553
2554 int rc2 = VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmSetWorkerOnCpu, 4,
2555 pUVM, pLookupRec, &Value, &Mask);
2556
2557 if (rc == VINF_SUCCESS || RT_FAILURE(rc2))
2558 rc = rc2;
2559 return rc;
2560 }
2561 return VERR_DBGF_READ_ONLY_REGISTER;
2562 }
2563 return VERR_DBGF_REGISTER_NOT_FOUND;
2564}
2565
2566
2567/**
2568 * Set a given set of registers.
2569 *
2570 * @returns VBox status code.
2571 * @retval VINF_SUCCESS
2572 * @retval VERR_INVALID_VM_HANDLE
2573 * @retval VERR_INVALID_CPU_ID
2574 * @retval VERR_DBGF_REGISTER_NOT_FOUND
2575 * @retval VERR_DBGF_UNSUPPORTED_CAST
2576 * @retval VINF_DBGF_TRUNCATED_REGISTER
2577 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
2578 *
2579 * @param pUVM The user mode VM handle.
2580 * @param idDefCpu The virtual CPU ID for the default CPU register
2581 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
2582 * @param paRegs The array of registers to set.
2583 * @param cRegs Number of registers in the array.
2584 *
2585 * @todo This is a _very_ lazy implementation by a lazy developer, some semantics
2586 * need to be figured out before the real implementation especially how and
2587 * when errors and informational status codes like VINF_DBGF_TRUNCATED_REGISTER
2588 * should be returned (think of an error right in the middle of the batch, should we
2589 * save the state and roll back?).
2590 */
2591VMMR3DECL(int) DBGFR3RegNmSetBatch(PUVM pUVM, VMCPUID idDefCpu, PCDBGFREGENTRYNM paRegs, size_t cRegs)
2592{
2593 /*
2594 * Validate input.
2595 */
2596 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2597 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2598 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2599 AssertPtrReturn(paRegs, VERR_INVALID_PARAMETER);
2600 AssertReturn(cRegs > 0, VERR_INVALID_PARAMETER);
2601
2602 for (uint32_t i = 0; i < cRegs; i++)
2603 {
2604 int rc = DBGFR3RegNmSet(pUVM, idDefCpu, paRegs[i].pszName, &paRegs[i].Val, paRegs[i].enmType);
2605 if (RT_FAILURE(rc))
2606 return rc;
2607 }
2608
2609 return VINF_SUCCESS;
2610}
2611
2612
2613/**
2614 * Internal worker for DBGFR3RegFormatValue, cbBuf is sufficent.
2615 *
2616 * @copydoc DBGFR3RegFormatValueEx
2617 */
2618DECLINLINE(ssize_t) dbgfR3RegFormatValueInt(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
2619 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
2620{
2621 switch (enmType)
2622 {
2623 case DBGFREGVALTYPE_U8:
2624 return RTStrFormatU8(pszBuf, cbBuf, pValue->u8, uBase, cchWidth, cchPrecision, fFlags);
2625 case DBGFREGVALTYPE_U16:
2626 return RTStrFormatU16(pszBuf, cbBuf, pValue->u16, uBase, cchWidth, cchPrecision, fFlags);
2627 case DBGFREGVALTYPE_U32:
2628 return RTStrFormatU32(pszBuf, cbBuf, pValue->u32, uBase, cchWidth, cchPrecision, fFlags);
2629 case DBGFREGVALTYPE_U64:
2630 return RTStrFormatU64(pszBuf, cbBuf, pValue->u64, uBase, cchWidth, cchPrecision, fFlags);
2631 case DBGFREGVALTYPE_U128:
2632 return RTStrFormatU128(pszBuf, cbBuf, &pValue->u128, uBase, cchWidth, cchPrecision, fFlags);
2633 case DBGFREGVALTYPE_U256:
2634 return RTStrFormatU256(pszBuf, cbBuf, &pValue->u256, uBase, cchWidth, cchPrecision, fFlags);
2635 case DBGFREGVALTYPE_U512:
2636 return RTStrFormatU512(pszBuf, cbBuf, &pValue->u512, uBase, cchWidth, cchPrecision, fFlags);
2637 case DBGFREGVALTYPE_R80:
2638 return RTStrFormatR80u2(pszBuf, cbBuf, &pValue->r80Ex, cchWidth, cchPrecision, fFlags);
2639 case DBGFREGVALTYPE_DTR:
2640 {
2641 ssize_t cch = RTStrFormatU64(pszBuf, cbBuf, pValue->dtr.u64Base,
2642 16, 2+16, 0, RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
2643 AssertReturn(cch > 0, VERR_DBGF_REG_IPE_1);
2644 pszBuf[cch++] = ':';
2645 cch += RTStrFormatU64(&pszBuf[cch], cbBuf - cch, pValue->dtr.u32Limit,
2646 16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
2647 return cch;
2648 }
2649
2650 case DBGFREGVALTYPE_32BIT_HACK:
2651 case DBGFREGVALTYPE_END:
2652 case DBGFREGVALTYPE_INVALID:
2653 break;
2654 /* no default, want gcc warnings */
2655 }
2656
2657 RTStrPrintf(pszBuf, cbBuf, "!enmType=%d!", enmType);
2658 return VERR_DBGF_REG_IPE_2;
2659}
2660
2661
2662/**
2663 * Format a register value, extended version.
2664 *
2665 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
2666 * @param pszBuf The output buffer.
2667 * @param cbBuf The size of the output buffer.
2668 * @param pValue The value to format.
2669 * @param enmType The value type.
2670 * @param uBase The base (ignored if not applicable).
2671 * @param cchWidth The width if RTSTR_F_WIDTH is set, otherwise
2672 * ignored.
2673 * @param cchPrecision The width if RTSTR_F_PRECISION is set, otherwise
2674 * ignored.
2675 * @param fFlags String formatting flags, RTSTR_F_XXX.
2676 */
2677VMMR3DECL(ssize_t) DBGFR3RegFormatValueEx(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
2678 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
2679{
2680 /*
2681 * Format to temporary buffer using worker shared with dbgfR3RegPrintfCbFormatNormal.
2682 */
2683 char szTmp[160];
2684 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), pValue, enmType, uBase, cchWidth, cchPrecision, fFlags);
2685 if (cchOutput > 0)
2686 {
2687 if ((size_t)cchOutput < cbBuf)
2688 memcpy(pszBuf, szTmp, cchOutput + 1);
2689 else
2690 {
2691 if (cbBuf)
2692 {
2693 memcpy(pszBuf, szTmp, cbBuf - 1); /* (parfait is wrong about out of bound read here) */
2694 pszBuf[cbBuf - 1] = '\0';
2695 }
2696 cchOutput = VERR_BUFFER_OVERFLOW;
2697 }
2698 }
2699 return cchOutput;
2700}
2701
2702
2703/**
2704 * Format a register value as hexadecimal and with default width according to
2705 * the type.
2706 *
2707 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
2708 * @param pszBuf The output buffer.
2709 * @param cbBuf The size of the output buffer.
2710 * @param pValue The value to format.
2711 * @param enmType The value type.
2712 * @param fSpecial Same as RTSTR_F_SPECIAL.
2713 */
2714VMMR3DECL(ssize_t) DBGFR3RegFormatValue(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType, bool fSpecial)
2715{
2716 int cchWidth = 0;
2717 switch (enmType)
2718 {
2719 case DBGFREGVALTYPE_U8: cchWidth = 2 + fSpecial*2; break;
2720 case DBGFREGVALTYPE_U16: cchWidth = 4 + fSpecial*2; break;
2721 case DBGFREGVALTYPE_U32: cchWidth = 8 + fSpecial*2; break;
2722 case DBGFREGVALTYPE_U64: cchWidth = 16 + fSpecial*2; break;
2723 case DBGFREGVALTYPE_U128: cchWidth = 32 + fSpecial*2; break;
2724 case DBGFREGVALTYPE_U256: cchWidth = 64 + fSpecial*2; break;
2725 case DBGFREGVALTYPE_U512: cchWidth = 128 + fSpecial*2; break;
2726 case DBGFREGVALTYPE_R80: cchWidth = 0; break;
2727 case DBGFREGVALTYPE_DTR: cchWidth = 16+1+4 + fSpecial*2; break;
2728
2729 case DBGFREGVALTYPE_32BIT_HACK:
2730 case DBGFREGVALTYPE_END:
2731 case DBGFREGVALTYPE_INVALID:
2732 break;
2733 /* no default, want gcc warnings */
2734 }
2735 uint32_t fFlags = RTSTR_F_ZEROPAD;
2736 if (fSpecial)
2737 fFlags |= RTSTR_F_SPECIAL;
2738 if (cchWidth != 0)
2739 fFlags |= RTSTR_F_WIDTH;
2740 return DBGFR3RegFormatValueEx(pszBuf, cbBuf, pValue, enmType, 16, cchWidth, 0, fFlags);
2741}
2742
2743
2744/**
2745 * Format a register using special hacks as well as sub-field specifications
2746 * (the latter isn't implemented yet).
2747 */
2748static size_t
2749dbgfR3RegPrintfCbFormatField(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2750 PCDBGFREGLOOKUP pLookupRec, int cchWidth, int cchPrecision, unsigned fFlags)
2751{
2752 char szTmp[160];
2753
2754 NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
2755
2756 /*
2757 * Retrieve the register value.
2758 */
2759 DBGFREGVAL Value;
2760 DBGFREGVALTYPE enmType;
2761 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2762 if (RT_FAILURE(rc))
2763 {
2764 ssize_t cchDefine = RTErrQueryDefine(rc, szTmp, sizeof(szTmp), true /*fFailIfUnknown*/);
2765 if (cchDefine <= 0)
2766 cchDefine = RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc);
2767 return pfnOutput(pvArgOutput, szTmp, cchDefine);
2768 }
2769
2770 char *psz = szTmp;
2771
2772 /*
2773 * Special case: Format eflags.
2774 */
2775 if ( pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU
2776 && pLookupRec->pDesc->enmReg == DBGFREG_RFLAGS
2777 && pLookupRec->pSubField == NULL)
2778 {
2779 rc = dbgfR3RegValCast(&Value, enmType, DBGFREGVALTYPE_U32);
2780 AssertRC(rc);
2781 uint32_t const efl = Value.u32;
2782
2783 /* the iopl */
2784 psz += RTStrPrintf(psz, sizeof(szTmp) / 2, "iopl=%u ", X86_EFL_GET_IOPL(efl));
2785
2786 /* add flags */
2787 static const struct
2788 {
2789 const char *pszSet;
2790 const char *pszClear;
2791 uint32_t fFlag;
2792 } aFlags[] =
2793 {
2794 { "vip",NULL, X86_EFL_VIP },
2795 { "vif",NULL, X86_EFL_VIF },
2796 { "ac", NULL, X86_EFL_AC },
2797 { "vm", NULL, X86_EFL_VM },
2798 { "rf", NULL, X86_EFL_RF },
2799 { "nt", NULL, X86_EFL_NT },
2800 { "ov", "nv", X86_EFL_OF },
2801 { "dn", "up", X86_EFL_DF },
2802 { "ei", "di", X86_EFL_IF },
2803 { "tf", NULL, X86_EFL_TF },
2804 { "ng", "pl", X86_EFL_SF },
2805 { "zr", "nz", X86_EFL_ZF },
2806 { "ac", "na", X86_EFL_AF },
2807 { "po", "pe", X86_EFL_PF },
2808 { "cy", "nc", X86_EFL_CF },
2809 };
2810 for (unsigned i = 0; i < RT_ELEMENTS(aFlags); i++)
2811 {
2812 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
2813 if (pszAdd)
2814 {
2815 *psz++ = *pszAdd++;
2816 *psz++ = *pszAdd++;
2817 if (*pszAdd)
2818 *psz++ = *pszAdd++;
2819 *psz++ = ' ';
2820 }
2821 }
2822
2823 /* drop trailing space */
2824 psz--;
2825 }
2826 else
2827 {
2828 /*
2829 * General case.
2830 */
2831 AssertMsgFailed(("Not implemented: %s\n", pLookupRec->Core.pszString));
2832 return pfnOutput(pvArgOutput, pLookupRec->Core.pszString, pLookupRec->Core.cchString);
2833 }
2834
2835 /* Output the string. */
2836 return pfnOutput(pvArgOutput, szTmp, psz - &szTmp[0]);
2837}
2838
2839
2840/**
2841 * Formats a register having parsed up to the register name.
2842 */
2843static size_t
2844dbgfR3RegPrintfCbFormatNormal(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2845 PCDBGFREGLOOKUP pLookupRec, unsigned uBase, int cchWidth, int cchPrecision, unsigned fFlags)
2846{
2847 char szTmp[160];
2848
2849 /*
2850 * Get the register value.
2851 */
2852 DBGFREGVAL Value;
2853 DBGFREGVALTYPE enmType;
2854 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2855 if (RT_FAILURE(rc))
2856 {
2857 ssize_t cchDefine = RTErrQueryDefine(rc, szTmp, sizeof(szTmp), true /*fFailIfUnknown*/);
2858 if (cchDefine <= 0)
2859 cchDefine = RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc);
2860 return pfnOutput(pvArgOutput, szTmp, cchDefine);
2861 }
2862
2863 /*
2864 * Format the value.
2865 */
2866 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), &Value, enmType, uBase, cchWidth, cchPrecision, fFlags);
2867 if (RT_UNLIKELY(cchOutput <= 0))
2868 {
2869 AssertFailed();
2870 return pfnOutput(pvArgOutput, "internal-error", sizeof("internal-error") - 1);
2871 }
2872 return pfnOutput(pvArgOutput, szTmp, cchOutput);
2873}
2874
2875
2876/**
2877 * @callback_method_impl{FNSTRFORMAT}
2878 */
2879static DECLCALLBACK(size_t)
2880dbgfR3RegPrintfCbFormat(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2881 const char **ppszFormat, va_list *pArgs, int cchWidth,
2882 int cchPrecision, unsigned fFlags, char chArgSize)
2883{
2884 NOREF(pArgs); NOREF(chArgSize);
2885
2886 /*
2887 * Parse the format type and hand the job to the appropriate worker.
2888 */
2889 PDBGFR3REGPRINTFARGS pThis = (PDBGFR3REGPRINTFARGS)pvArg;
2890 const char *pszFormat = *ppszFormat;
2891 if ( pszFormat[0] != 'V'
2892 || pszFormat[1] != 'R')
2893 {
2894 AssertMsgFailed(("'%s'\n", pszFormat));
2895 return 0;
2896 }
2897 unsigned offCurly = 2;
2898 if (pszFormat[offCurly] != '{')
2899 {
2900 AssertMsgReturn(pszFormat[offCurly], ("'%s'\n", pszFormat), 0);
2901 offCurly++;
2902 AssertMsgReturn(pszFormat[offCurly] == '{', ("'%s'\n", pszFormat), 0);
2903 }
2904 const char *pachReg = &pszFormat[offCurly + 1];
2905
2906 /*
2907 * The end and length of the register.
2908 */
2909 const char *pszEnd = strchr(pachReg, '}');
2910 AssertMsgReturn(pszEnd, ("Missing closing curly bracket: '%s'\n", pszFormat), 0);
2911 size_t const cchReg = pszEnd - pachReg;
2912
2913 /*
2914 * Look up the register - same as dbgfR3RegResolve, except for locking and
2915 * input string termination.
2916 */
2917 PRTSTRSPACE pRegSpace = &pThis->pUVM->dbgf.s.RegSpace;
2918 /* Try looking up the name without any case folding or cpu prefixing. */
2919 PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGetN(pRegSpace, pachReg, cchReg);
2920 if (!pLookupRec)
2921 {
2922 /* Lower case it and try again. */
2923 char szName[DBGF_REG_MAX_NAME * 4 + 16];
2924 ssize_t cchFolded = dbgfR3RegCopyToLower(pachReg, cchReg, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
2925 if (cchFolded > 0)
2926 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2927 if ( !pLookupRec
2928 && cchFolded >= 0
2929 && pThis->idCpu != VMCPUID_ANY)
2930 {
2931 /* Prefix it with the specified CPU set. */
2932 size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), pThis->fGuestRegs ? "cpu%u." : "hypercpu%u.", pThis->idCpu);
2933 dbgfR3RegCopyToLower(pachReg, cchReg, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
2934 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2935 }
2936 }
2937 AssertMsgReturn(pLookupRec, ("'%s'\n", pszFormat), 0);
2938 AssertMsgReturn( pLookupRec->pSet->enmType != DBGFREGSETTYPE_CPU
2939 || pLookupRec->pSet->uUserArg.pVCpu->idCpu == pThis->idCpu,
2940 ("'%s' idCpu=%u, pSet/cpu=%u\n", pszFormat, pThis->idCpu, pLookupRec->pSet->uUserArg.pVCpu->idCpu),
2941 0);
2942
2943 /*
2944 * Commit the parsed format string. Up to this point it is nice to know
2945 * what register lookup failed and such, so we've delayed comitting.
2946 */
2947 *ppszFormat = pszEnd + 1;
2948
2949 /*
2950 * Call the responsible worker.
2951 */
2952 switch (pszFormat[offCurly - 1])
2953 {
2954 case 'R': /* %VR{} */
2955 case 'X': /* %VRX{} */
2956 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2957 16, cchWidth, cchPrecision, fFlags);
2958 case 'U':
2959 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2960 10, cchWidth, cchPrecision, fFlags);
2961 case 'O':
2962 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2963 8, cchWidth, cchPrecision, fFlags);
2964 case 'B':
2965 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2966 2, cchWidth, cchPrecision, fFlags);
2967 case 'F':
2968 return dbgfR3RegPrintfCbFormatField(pThis, pfnOutput, pvArgOutput, pLookupRec, cchWidth, cchPrecision, fFlags);
2969 default:
2970 AssertFailed();
2971 return 0;
2972 }
2973}
2974
2975
2976
2977/**
2978 * @callback_method_impl{FNRTSTROUTPUT}
2979 */
2980static DECLCALLBACK(size_t)
2981dbgfR3RegPrintfCbOutput(void *pvArg, const char *pachChars, size_t cbChars)
2982{
2983 PDBGFR3REGPRINTFARGS pArgs = (PDBGFR3REGPRINTFARGS)pvArg;
2984 size_t cbToCopy = cbChars;
2985 if (cbToCopy >= pArgs->cchLeftBuf)
2986 {
2987 if (RT_SUCCESS(pArgs->rc))
2988 pArgs->rc = VERR_BUFFER_OVERFLOW;
2989 cbToCopy = pArgs->cchLeftBuf;
2990 }
2991 if (cbToCopy > 0)
2992 {
2993 memcpy(&pArgs->pszBuf[pArgs->offBuf], pachChars, cbToCopy);
2994 pArgs->offBuf += cbToCopy;
2995 pArgs->cchLeftBuf -= cbToCopy;
2996 pArgs->pszBuf[pArgs->offBuf] = '\0';
2997 }
2998 return cbToCopy;
2999}
3000
3001
3002/**
3003 * On CPU worker for the register formatting, used by DBGFR3RegPrintfV.
3004 *
3005 * @returns VBox status code.
3006 *
3007 * @param pArgs The argument package and state.
3008 */
3009static DECLCALLBACK(int) dbgfR3RegPrintfWorkerOnCpu(PDBGFR3REGPRINTFARGS pArgs)
3010{
3011 DBGF_REG_DB_LOCK_READ(pArgs->pUVM);
3012 RTStrFormatV(dbgfR3RegPrintfCbOutput, pArgs, dbgfR3RegPrintfCbFormat, pArgs, pArgs->pszFormat, pArgs->va);
3013 DBGF_REG_DB_UNLOCK_READ(pArgs->pUVM);
3014 return pArgs->rc;
3015}
3016
3017
3018/**
3019 * Format a registers.
3020 *
3021 * This is restricted to registers from one CPU, that specified by @a idCpu.
3022 *
3023 * @returns VBox status code.
3024 * @param pUVM The user mode VM handle.
3025 * @param idCpu The CPU ID of any CPU registers that may be
3026 * printed, pass VMCPUID_ANY if not applicable.
3027 * @param pszBuf The output buffer.
3028 * @param cbBuf The size of the output buffer.
3029 * @param pszFormat The format string. Register names are given by
3030 * %VR{name}, they take no arguments.
3031 * @param va Other format arguments.
3032 */
3033VMMR3DECL(int) DBGFR3RegPrintfV(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, va_list va)
3034{
3035 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
3036 AssertReturn(cbBuf > 0, VERR_BUFFER_OVERFLOW);
3037 *pszBuf = '\0';
3038
3039 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
3040 AssertReturn((idCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
3041 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
3042
3043 /*
3044 * Set up an argument package and execute the formatting on the
3045 * specified CPU.
3046 */
3047 DBGFR3REGPRINTFARGS Args;
3048 Args.pUVM = pUVM;
3049 Args.idCpu = idCpu != VMCPUID_ANY ? idCpu & ~DBGFREG_HYPER_VMCPUID : idCpu;
3050 Args.fGuestRegs = idCpu != VMCPUID_ANY && !(idCpu & DBGFREG_HYPER_VMCPUID);
3051 Args.pszBuf = pszBuf;
3052 Args.pszFormat = pszFormat;
3053 va_copy(Args.va, va);
3054 Args.offBuf = 0;
3055 Args.cchLeftBuf = cbBuf - 1;
3056 Args.rc = VINF_SUCCESS;
3057 int rc = VMR3ReqPriorityCallWaitU(pUVM, Args.idCpu, (PFNRT)dbgfR3RegPrintfWorkerOnCpu, 1, &Args);
3058 va_end(Args.va);
3059 return rc;
3060}
3061
3062
3063/**
3064 * Format a registers.
3065 *
3066 * This is restricted to registers from one CPU, that specified by @a idCpu.
3067 *
3068 * @returns VBox status code.
3069 * @param pUVM The user mode VM handle.
3070 * @param idCpu The CPU ID of any CPU registers that may be
3071 * printed, pass VMCPUID_ANY if not applicable.
3072 * @param pszBuf The output buffer.
3073 * @param cbBuf The size of the output buffer.
3074 * @param pszFormat The format string. Register names are given by
3075 * %VR{name}, %VRU{name}, %VRO{name} and
3076 * %VRB{name}, which are hexadecimal, (unsigned)
3077 * decimal, octal and binary representation. None
3078 * of these types takes any arguments.
3079 * @param ... Other format arguments.
3080 */
3081VMMR3DECL(int) DBGFR3RegPrintf(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, ...)
3082{
3083 va_list va;
3084 va_start(va, pszFormat);
3085 int rc = DBGFR3RegPrintfV(pUVM, idCpu, pszBuf, cbBuf, pszFormat, va);
3086 va_end(va);
3087 return rc;
3088}
3089
Note: See TracBrowser for help on using the repository browser.

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