1 | /* $Id: EMR3Dbg.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Execution Monitor / Manager, Debugger Related Bits.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_EM
|
---|
33 | #include <VBox/vmm/em.h>
|
---|
34 | #include <VBox/vmm/hm.h>
|
---|
35 | #include <VBox/vmm/nem.h>
|
---|
36 | #include <VBox/dbg.h>
|
---|
37 | #include "EMInternal.h"
|
---|
38 | #include <VBox/vmm/vm.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/ctype.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /** @callback_method_impl{FNDBGCCMD,
|
---|
44 | * Implements the '.alliem' command. }
|
---|
45 | */
|
---|
46 | static DECLCALLBACK(int) enmR3DbgCmdAllIem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
|
---|
47 | {
|
---|
48 | int rc;
|
---|
49 | bool f;
|
---|
50 |
|
---|
51 | if (cArgs == 0)
|
---|
52 | {
|
---|
53 | rc = EMR3QueryExecutionPolicy(pUVM, EMEXECPOLICY_IEM_ALL, &f);
|
---|
54 | if (RT_FAILURE(rc))
|
---|
55 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "EMR3QueryExecutionPolicy(,EMEXECPOLICY_IEM_ALL,");
|
---|
56 | DBGCCmdHlpPrintf(pCmdHlp, f ? "alliem: enabled\n" : "alliem: disabled\n");
|
---|
57 | }
|
---|
58 | else
|
---|
59 | {
|
---|
60 | rc = DBGCCmdHlpVarToBool(pCmdHlp, &paArgs[0], &f);
|
---|
61 | if (RT_FAILURE(rc))
|
---|
62 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToBool");
|
---|
63 | rc = EMR3SetExecutionPolicy(pUVM, EMEXECPOLICY_IEM_ALL, f);
|
---|
64 | if (RT_FAILURE(rc))
|
---|
65 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "EMR3SetExecutionPolicy(,EMEXECPOLICY_IEM_ALL,%RTbool)", f);
|
---|
66 | }
|
---|
67 | return VINF_SUCCESS;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /** Describes a optional boolean argument. */
|
---|
72 | static DBGCVARDESC const g_BoolArg = { 0, 1, DBGCVAR_CAT_ANY, 0, "boolean", "Boolean value." };
|
---|
73 |
|
---|
74 | /** Commands. */
|
---|
75 | static DBGCCMD const g_aCmds[] =
|
---|
76 | {
|
---|
77 | {
|
---|
78 | "alliem", 0, 1, &g_BoolArg, 1, 0, enmR3DbgCmdAllIem, "[boolean]",
|
---|
79 | "Enables or disabled executing ALL code in IEM, if no arguments are given it displays the current status."
|
---|
80 | },
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Translates EMEXITTYPE into a name.
|
---|
86 | *
|
---|
87 | * @returns Pointer to read-only name, NULL if unknown type.
|
---|
88 | * @param enmExitType The exit type to name.
|
---|
89 | */
|
---|
90 | VMM_INT_DECL(const char *) EMR3GetExitTypeName(EMEXITTYPE enmExitType)
|
---|
91 | {
|
---|
92 | switch (enmExitType)
|
---|
93 | {
|
---|
94 | case EMEXITTYPE_INVALID: return "invalid";
|
---|
95 | case EMEXITTYPE_IO_PORT_READ: return "I/O port read";
|
---|
96 | case EMEXITTYPE_IO_PORT_WRITE: return "I/O port write";
|
---|
97 | case EMEXITTYPE_IO_PORT_STR_READ: return "I/O port string read";
|
---|
98 | case EMEXITTYPE_IO_PORT_STR_WRITE: return "I/O port string write";
|
---|
99 | case EMEXITTYPE_MMIO: return "MMIO access";
|
---|
100 | case EMEXITTYPE_MMIO_READ: return "MMIO read";
|
---|
101 | case EMEXITTYPE_MMIO_WRITE: return "MMIO write";
|
---|
102 | case EMEXITTYPE_MSR_READ: return "MSR read";
|
---|
103 | case EMEXITTYPE_MSR_WRITE: return "MSR write";
|
---|
104 | case EMEXITTYPE_CPUID: return "CPUID";
|
---|
105 | case EMEXITTYPE_RDTSC: return "RDTSC";
|
---|
106 | case EMEXITTYPE_MOV_CRX: return "MOV CRx";
|
---|
107 | case EMEXITTYPE_MOV_DRX: return "MOV DRx";
|
---|
108 | case EMEXITTYPE_VMREAD: return "VMREAD";
|
---|
109 | case EMEXITTYPE_VMWRITE: return "VMWRITE";
|
---|
110 |
|
---|
111 | /* Raw-mode only: */
|
---|
112 | case EMEXITTYPE_INVLPG: return "INVLPG";
|
---|
113 | case EMEXITTYPE_LLDT: return "LLDT";
|
---|
114 | case EMEXITTYPE_RDPMC: return "RDPMC";
|
---|
115 | case EMEXITTYPE_CLTS: return "CLTS";
|
---|
116 | case EMEXITTYPE_STI: return "STI";
|
---|
117 | case EMEXITTYPE_INT: return "INT";
|
---|
118 | case EMEXITTYPE_SYSCALL: return "SYSCALL";
|
---|
119 | case EMEXITTYPE_SYSENTER: return "SYSENTER";
|
---|
120 | case EMEXITTYPE_HLT: return "HLT";
|
---|
121 | }
|
---|
122 | return NULL;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Translates flags+type into an exit name.
|
---|
128 | *
|
---|
129 | * @returns Exit name.
|
---|
130 | * @param uFlagsAndType The exit to name.
|
---|
131 | * @param pszFallback Buffer for formatting a numeric fallback.
|
---|
132 | * @param cbFallback Size of fallback buffer.
|
---|
133 | */
|
---|
134 | static const char *emR3HistoryGetExitName(uint32_t uFlagsAndType, char *pszFallback, size_t cbFallback)
|
---|
135 | {
|
---|
136 | const char *pszExitName;
|
---|
137 | switch (uFlagsAndType & EMEXIT_F_KIND_MASK)
|
---|
138 | {
|
---|
139 | case EMEXIT_F_KIND_EM:
|
---|
140 | pszExitName = EMR3GetExitTypeName((EMEXITTYPE)(uFlagsAndType & EMEXIT_F_TYPE_MASK));
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case EMEXIT_F_KIND_VMX:
|
---|
144 | pszExitName = HMGetVmxExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
145 | break;
|
---|
146 |
|
---|
147 | case EMEXIT_F_KIND_SVM:
|
---|
148 | pszExitName = HMGetSvmExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
149 | break;
|
---|
150 |
|
---|
151 | case EMEXIT_F_KIND_NEM:
|
---|
152 | pszExitName = NEMR3GetExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
153 | break;
|
---|
154 |
|
---|
155 | case EMEXIT_F_KIND_XCPT:
|
---|
156 | switch (uFlagsAndType & EMEXIT_F_TYPE_MASK)
|
---|
157 | {
|
---|
158 | case X86_XCPT_DE: return "Xcpt #DE";
|
---|
159 | case X86_XCPT_DB: return "Xcpt #DB";
|
---|
160 | case X86_XCPT_NMI: return "Xcpt #NMI";
|
---|
161 | case X86_XCPT_BP: return "Xcpt #BP";
|
---|
162 | case X86_XCPT_OF: return "Xcpt #OF";
|
---|
163 | case X86_XCPT_BR: return "Xcpt #BR";
|
---|
164 | case X86_XCPT_UD: return "Xcpt #UD";
|
---|
165 | case X86_XCPT_NM: return "Xcpt #NM";
|
---|
166 | case X86_XCPT_DF: return "Xcpt #DF";
|
---|
167 | case X86_XCPT_CO_SEG_OVERRUN: return "Xcpt #CO_SEG_OVERRUN";
|
---|
168 | case X86_XCPT_TS: return "Xcpt #TS";
|
---|
169 | case X86_XCPT_NP: return "Xcpt #NP";
|
---|
170 | case X86_XCPT_SS: return "Xcpt #SS";
|
---|
171 | case X86_XCPT_GP: return "Xcpt #GP";
|
---|
172 | case X86_XCPT_PF: return "Xcpt #PF";
|
---|
173 | case X86_XCPT_MF: return "Xcpt #MF";
|
---|
174 | case X86_XCPT_AC: return "Xcpt #AC";
|
---|
175 | case X86_XCPT_MC: return "Xcpt #MC";
|
---|
176 | case X86_XCPT_XF: return "Xcpt #XF";
|
---|
177 | case X86_XCPT_VE: return "Xcpt #VE";
|
---|
178 | case X86_XCPT_SX: return "Xcpt #SX";
|
---|
179 | default:
|
---|
180 | pszExitName = NULL;
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | break;
|
---|
184 |
|
---|
185 | default:
|
---|
186 | AssertFailed();
|
---|
187 | pszExitName = NULL;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | if (pszExitName)
|
---|
191 | return pszExitName;
|
---|
192 | RTStrPrintf(pszFallback, cbFallback, "%#06x", uFlagsAndType & (EMEXIT_F_KIND_MASK | EMEXIT_F_TYPE_MASK));
|
---|
193 | return pszFallback;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Displays the VM-exit history.
|
---|
199 | *
|
---|
200 | * @param pVM The cross context VM structure.
|
---|
201 | * @param pHlp The info helper functions.
|
---|
202 | * @param pszArgs Arguments, ignored.
|
---|
203 | */
|
---|
204 | static DECLCALLBACK(void) emR3InfoExitHistory(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
205 | {
|
---|
206 | NOREF(pszArgs);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Figure out target cpu and parse arguments.
|
---|
210 | */
|
---|
211 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
212 | if (!pVCpu)
|
---|
213 | pVCpu = pVM->apCpusR3[0];
|
---|
214 | bool fReverse = true;
|
---|
215 | uint32_t cLeft = RT_ELEMENTS(pVCpu->em.s.aExitHistory);
|
---|
216 |
|
---|
217 | while (pszArgs && *pszArgs)
|
---|
218 | {
|
---|
219 | pszArgs = RTStrStripL(pszArgs);
|
---|
220 | if (!*pszArgs)
|
---|
221 | break;
|
---|
222 | if (RT_C_IS_DIGIT(*pszArgs))
|
---|
223 | {
|
---|
224 | /* The number to dump. */
|
---|
225 | uint32_t uValue = cLeft;
|
---|
226 | RTStrToUInt32Ex(pszArgs, (char **)&pszArgs, 0, &uValue);
|
---|
227 | if (uValue > 0)
|
---|
228 | cLeft = RT_MIN(uValue, RT_ELEMENTS(pVCpu->em.s.aExitHistory));
|
---|
229 | }
|
---|
230 | else if (RTStrCmp(pszArgs, "reverse") == 0)
|
---|
231 | {
|
---|
232 | pszArgs += 7;
|
---|
233 | fReverse = true;
|
---|
234 | }
|
---|
235 | else if (RTStrCmp(pszArgs, "ascending") == 0)
|
---|
236 | {
|
---|
237 | pszArgs += 9;
|
---|
238 | fReverse = false;
|
---|
239 | }
|
---|
240 | else if (RTStrCmp(pszArgs, "asc") == 0)
|
---|
241 | {
|
---|
242 | pszArgs += 3;
|
---|
243 | fReverse = false;
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | const char *pszStart = pszArgs;
|
---|
248 | while (*pszArgs && !RT_C_IS_SPACE(*pszArgs))
|
---|
249 | pszArgs++;
|
---|
250 | pHlp->pfnPrintf(pHlp, "Unknown option: %.*s\n", pszArgs - pszStart, pszArgs);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * Do the job.
|
---|
256 | */
|
---|
257 | uint64_t idx = pVCpu->em.s.iNextExit;
|
---|
258 | if (idx == 0)
|
---|
259 | pHlp->pfnPrintf(pHlp, "CPU[%u]: VM-exit history: empty\n", pVCpu->idCpu);
|
---|
260 | else
|
---|
261 | {
|
---|
262 | /*
|
---|
263 | * Print header.
|
---|
264 | */
|
---|
265 | pHlp->pfnPrintf(pHlp,
|
---|
266 | "CPU[%u]: VM-exit history:\n"
|
---|
267 | " Exit No.: TSC timestamp / delta RIP (Flat/*) Exit Name\n"
|
---|
268 | , pVCpu->idCpu);
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Adjust bounds if ascending order.
|
---|
272 | */
|
---|
273 | if (!fReverse)
|
---|
274 | {
|
---|
275 | if (idx > cLeft)
|
---|
276 | idx -= cLeft;
|
---|
277 | else
|
---|
278 | {
|
---|
279 | cLeft = idx;
|
---|
280 | idx = 0;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Print the entries.
|
---|
286 | */
|
---|
287 | uint64_t uPrevTimestamp = 0;
|
---|
288 | do
|
---|
289 | {
|
---|
290 | if (fReverse)
|
---|
291 | idx -= 1;
|
---|
292 | PCEMEXITENTRY const pEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)idx & 0xff];
|
---|
293 |
|
---|
294 | /* Get the exit name. */
|
---|
295 | char szExitName[16];
|
---|
296 | const char *pszExitName = emR3HistoryGetExitName(pEntry->uFlagsAndType, szExitName, sizeof(szExitName));
|
---|
297 |
|
---|
298 | /* Calc delta (negative if reverse order, positive ascending). */
|
---|
299 | int64_t offDelta = uPrevTimestamp != 0 && pEntry->uTimestamp != 0 ? pEntry->uTimestamp - uPrevTimestamp : 0;
|
---|
300 | uPrevTimestamp = pEntry->uTimestamp;
|
---|
301 |
|
---|
302 | char szPC[32];
|
---|
303 | if (!(pEntry->uFlagsAndType & (EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC)))
|
---|
304 | RTStrPrintf(szPC, sizeof(szPC), "%016RX64 ", pEntry->uFlatPC);
|
---|
305 | else if (pEntry->uFlagsAndType & EMEXIT_F_UNFLATTENED_PC)
|
---|
306 | RTStrPrintf(szPC, sizeof(szPC), "%016RX64*", pEntry->uFlatPC);
|
---|
307 | else
|
---|
308 | RTStrPrintf(szPC, sizeof(szPC), "%04x:%08RX32* ", (uint32_t)(pEntry->uFlatPC >> 32), (uint32_t)pEntry->uFlatPC);
|
---|
309 |
|
---|
310 | /* Do the printing. */
|
---|
311 | if (pEntry->idxSlot == UINT32_MAX)
|
---|
312 | pHlp->pfnPrintf(pHlp, " %10RU64: %#018RX64/%+-9RI64 %s %#07x %s\n",
|
---|
313 | idx, pEntry->uTimestamp, offDelta, szPC, pEntry->uFlagsAndType, pszExitName);
|
---|
314 | else
|
---|
315 | {
|
---|
316 | /** @todo more on this later */
|
---|
317 | pHlp->pfnPrintf(pHlp, " %10RU64: %#018RX64/%+-9RI64 %s %#07x %s slot=%#x\n",
|
---|
318 | idx, pEntry->uTimestamp, offDelta, szPC, pEntry->uFlagsAndType, pszExitName, pEntry->idxSlot);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* Advance if ascending. */
|
---|
322 | if (!fReverse)
|
---|
323 | idx += 1;
|
---|
324 | } while (--cLeft > 0 && idx > 0);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | int emR3InitDbg(PVM pVM)
|
---|
330 | {
|
---|
331 | /*
|
---|
332 | * Register info dumpers.
|
---|
333 | */
|
---|
334 | const char *pszExitsDesc = "Dumps the VM-exit history. Arguments: Number of entries; 'asc', 'ascending' or 'reverse'.";
|
---|
335 | int rc = DBGFR3InfoRegisterInternalEx(pVM, "exits", pszExitsDesc, emR3InfoExitHistory, DBGFINFO_FLAGS_ALL_EMTS);
|
---|
336 | AssertLogRelRCReturn(rc, rc);
|
---|
337 | rc = DBGFR3InfoRegisterInternalEx(pVM, "exithistory", pszExitsDesc, emR3InfoExitHistory, DBGFINFO_FLAGS_ALL_EMTS);
|
---|
338 | AssertLogRelRCReturn(rc, rc);
|
---|
339 |
|
---|
340 | #ifdef VBOX_WITH_DEBUGGER
|
---|
341 | /*
|
---|
342 | * Register debugger commands.
|
---|
343 | */
|
---|
344 | rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
|
---|
345 | AssertLogRelRCReturn(rc, rc);
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | return VINF_SUCCESS;
|
---|
349 | }
|
---|
350 |
|
---|