VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PATMR3Dbg.cpp@ 72287

Last change on this file since 72287 was 70948, checked in by vboxsync, 7 years ago

VMM: Added a bMainExecutionEngine member to the VM structure for use instead of fHMEnabled and fNEMEnabled. Changed a lot of HMIsEnabled invocations to use the new macros VM_IS_RAW_MODE_ENABLED and VM_IS_HM_OR_NEM_ENABLED. Eliminated fHMEnabledFixed. Fixed inverted test for raw-mode debug register sanity checking. Some other minor cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1/* $Id: PATMR3Dbg.cpp 70948 2018-02-10 15:38:12Z vboxsync $ */
2/** @file
3 * PATM - Dynamic Guest OS Patching Manager, Debugger Related Parts.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PATM
23#include <VBox/vmm/patm.h>
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/hm.h>
26#include "PATMInternal.h"
27#include "PATMA.h"
28#include <VBox/vmm/vm.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31
32#include <iprt/assert.h>
33#include <iprt/dbg.h>
34#include <iprt/string.h>
35
36
37/*********************************************************************************************************************************
38* Defined Constants And Macros *
39*********************************************************************************************************************************/
40/** Adds a structure member to a debug (pseudo) module as a symbol. */
41#define ADD_MEMBER(a_hDbgMod, a_Struct, a_Member, a_pszName) \
42 do { \
43 rc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, RT_OFFSETOF(a_Struct, a_Member), \
44 RT_SIZEOFMEMB(a_Struct, a_Member), 0 /*fFlags*/, NULL /*piOrdinal*/); \
45 AssertRC(rc); \
46 } while (0)
47
48/** Adds a structure member to a debug (pseudo) module as a symbol. */
49#define ADD_FUNC(a_hDbgMod, a_BaseRCPtr, a_FuncRCPtr, a_cbFunc, a_pszName) \
50 do { \
51 int rcAddFunc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, \
52 (RTRCUINTPTR)a_FuncRCPtr - (RTRCUINTPTR)(a_BaseRCPtr), \
53 a_cbFunc, 0 /*fFlags*/, NULL /*piOrdinal*/); \
54 AssertRC(rcAddFunc); \
55 } while (0)
56
57
58
59/**
60 * Called by PATMR3Init.
61 *
62 * @param pVM The cross context VM structure.
63 */
64void patmR3DbgInit(PVM pVM)
65{
66 pVM->patm.s.hDbgModPatchMem = NIL_RTDBGMOD;
67}
68
69
70/**
71 * Called by PATMR3Term.
72 *
73 * @param pVM The cross context VM structure.
74 */
75void patmR3DbgTerm(PVM pVM)
76{
77 if (pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD)
78 {
79 RTDbgModRelease(pVM->patm.s.hDbgModPatchMem);
80 pVM->patm.s.hDbgModPatchMem = NIL_RTDBGMOD;
81 }
82}
83
84
85/**
86 * Called by when the patch memory is reinitialized.
87 *
88 * @param pVM The cross context VM structure.
89 */
90void patmR3DbgReset(PVM pVM)
91{
92 if (pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD)
93 {
94 RTDbgModRemoveAll(pVM->patm.s.hDbgModPatchMem, true);
95 }
96}
97
98
99static size_t patmR3DbgDescribePatchAsSymbol(PPATMPATCHREC pPatchRec, char *pszName, size_t cbLeft)
100{
101 char * const pszNameStart = pszName;
102#define ADD_SZ(a_sz) \
103 do { \
104 if (cbLeft >= sizeof(a_sz)) \
105 { \
106 memcpy(pszName, a_sz, sizeof(a_sz)); \
107 pszName += sizeof(a_sz) - 1; \
108 cbLeft -= sizeof(a_sz) - 1;\
109 }\
110 } while (0)
111
112 /* Start the name off with the address of the guest code. */
113 size_t cch = RTStrPrintf(pszName, cbLeft, "Patch_%#08x", pPatchRec->patch.pPrivInstrGC);
114 cbLeft -= cch;
115 pszName += cch;
116
117 /* Append flags. */
118 uint64_t fFlags = pPatchRec->patch.flags;
119 if (fFlags & PATMFL_INTHANDLER)
120 ADD_SZ("_IntHandler");
121 if (fFlags & PATMFL_SYSENTER)
122 ADD_SZ("_SysEnter");
123 if (fFlags & PATMFL_GUEST_SPECIFIC)
124 ADD_SZ("_GuestSpecific");
125 if (fFlags & PATMFL_USER_MODE)
126 ADD_SZ("_UserMode");
127 if (fFlags & PATMFL_IDTHANDLER)
128 ADD_SZ("_IdtHnd");
129 if (fFlags & PATMFL_TRAPHANDLER)
130 ADD_SZ("_TrapHnd");
131 if (fFlags & PATMFL_DUPLICATE_FUNCTION)
132 ADD_SZ("_DupFunc");
133 if (fFlags & PATMFL_REPLACE_FUNCTION_CALL)
134 ADD_SZ("_ReplFunc");
135 if (fFlags & PATMFL_TRAPHANDLER_WITH_ERRORCODE)
136 ADD_SZ("_TrapHndErrCd");
137 if (fFlags & PATMFL_MMIO_ACCESS)
138 ADD_SZ("_MmioAccess");
139 if (fFlags & PATMFL_SYSENTER_XP)
140 ADD_SZ("_SysEnterXP");
141 if (fFlags & PATMFL_INT3_REPLACEMENT)
142 ADD_SZ("_Int3Repl");
143 if (fFlags & PATMFL_SUPPORT_CALLS)
144 ADD_SZ("_SupCalls");
145 if (fFlags & PATMFL_SUPPORT_INDIRECT_CALLS)
146 ADD_SZ("_SupIndirCalls");
147 if (fFlags & PATMFL_IDTHANDLER_WITHOUT_ENTRYPOINT)
148 ADD_SZ("_IdtHandlerWE");
149 if (fFlags & PATMFL_INHIBIT_IRQS)
150 ADD_SZ("_InhibitIrqs");
151 if (fFlags & PATMFL_RECOMPILE_NEXT)
152 ADD_SZ("_RecompileNext");
153 if (fFlags & PATMFL_CALLABLE_AS_FUNCTION)
154 ADD_SZ("_Callable");
155 if (fFlags & PATMFL_TRAMPOLINE)
156 ADD_SZ("_Trampoline");
157 if (fFlags & PATMFL_PATCHED_GUEST_CODE)
158 ADD_SZ("_PatchedGuestCode");
159 if (fFlags & PATMFL_MUST_INSTALL_PATCHJMP)
160 ADD_SZ("_MustInstallPatchJmp");
161 if (fFlags & PATMFL_INT3_REPLACEMENT_BLOCK)
162 ADD_SZ("_Int3ReplBlock");
163 if (fFlags & PATMFL_EXTERNAL_JUMP_INSIDE)
164 ADD_SZ("_ExtJmp");
165 if (fFlags & PATMFL_CODE_REFERENCED)
166 ADD_SZ("_CodeRefed");
167
168 return pszName - pszNameStart;
169}
170
171
172/**
173 * Called when a new patch is added or when first populating the address space.
174 *
175 * @param pVM The cross context VM structure.
176 * @param pPatchRec The patch record.
177 */
178void patmR3DbgAddPatch(PVM pVM, PPATMPATCHREC pPatchRec)
179{
180 if ( pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD
181 && pPatchRec->patch.pPatchBlockOffset > 0
182 && !(pPatchRec->patch.flags & PATMFL_GLOBAL_FUNCTIONS))
183 {
184 /** @todo find a cheap way of checking whether we've already added the patch.
185 * Using a flag would be nice, except I don't want to consider saved
186 * state considerations right now (I don't recall if we're still
187 * depending on structure layout there or not). */
188 char szName[256];
189 size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, szName, sizeof(szName));
190
191 /* If we have a symbol near the guest address, append that. */
192 if (off + 8 <= sizeof(szName))
193 {
194 RTDBGSYMBOL Symbol;
195 RTGCINTPTR offDisp;
196 DBGFADDRESS Addr;
197
198 int rc = DBGFR3AsSymbolByAddr(pVM->pUVM, DBGF_AS_GLOBAL,
199 DBGFR3AddrFromFlat(pVM->pUVM, &Addr, pPatchRec->patch.pPrivInstrGC),
200 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL,
201 &offDisp, &Symbol, NULL /*phMod*/);
202 if (RT_SUCCESS(rc))
203 {
204 szName[off++] = '_';
205 szName[off++] = '_';
206 RTStrCopy(&szName[off], sizeof(szName) - off, Symbol.szName);
207 }
208 }
209
210 /* Add it (may fail due to enable/disable patches). */
211 RTDbgModSymbolAdd(pVM->patm.s.hDbgModPatchMem, szName, 0 /*iSeg*/,
212 pPatchRec->patch.pPatchBlockOffset,
213 pPatchRec->patch.cbPatchBlockSize,
214 0 /*fFlags*/, NULL /*piOrdinal*/);
215 }
216}
217
218
219/**
220 * Enumeration callback used by patmR3DbgAddPatches
221 *
222 * @returns 0 (continue enum)
223 * @param pNode The patch record node.
224 * @param pvUser The cross context VM structure.
225 */
226static DECLCALLBACK(int) patmR3DbgAddPatchCallback(PAVLOU32NODECORE pNode, void *pvUser)
227{
228 patmR3DbgAddPatch((PVM)pvUser, (PPATMPATCHREC)pNode);
229 return 0;
230}
231
232
233/**
234 * Populates an empty "patches" (hDbgModPatchMem) module with patch symbols.
235 *
236 * @param pVM The cross context VM structure.
237 * @param hDbgMod The debug module handle.
238 */
239static void patmR3DbgAddPatches(PVM pVM, RTDBGMOD hDbgMod)
240{
241 /*
242 * Global functions and a start marker.
243 */
244 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, g_patmLookupAndCallRecord.cbFunction, "PATMLookupAndCall");
245 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, g_patmRetFunctionRecord.cbFunction, "PATMRetFunction");
246 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, g_patmLookupAndJumpRecord.cbFunction, "PATMLookupAndJump");
247 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, g_patmIretFunctionRecord.cbFunction, "PATMIretFunction");
248
249 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pPatchMemGC, 0, "PatchMemStart");
250 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pGCStackGC, PATM_STACK_TOTAL_SIZE, "PATMStack");
251
252 /*
253 * The patches.
254 */
255 RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
256}
257
258
259/**
260 * Populate DBGF_AS_RC with PATM symbols.
261 *
262 * Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
263 * accessed for the first time.
264 *
265 * @param pVM The cross context VM structure.
266 * @param hDbgAs The DBGF_AS_RC address space handle.
267 */
268VMMR3_INT_DECL(void) PATMR3DbgPopulateAddrSpace(PVM pVM, RTDBGAS hDbgAs)
269{
270 AssertReturnVoid(VM_IS_RAW_MODE_ENABLED(pVM));
271
272 /*
273 * Add a fake debug module for the PATMGCSTATE structure.
274 */
275 RTDBGMOD hDbgMod;
276 int rc = RTDbgModCreate(&hDbgMod, "patmgcstate", sizeof(PATMGCSTATE), 0 /*fFlags*/);
277 if (RT_SUCCESS(rc))
278 {
279 ADD_MEMBER(hDbgMod, PATMGCSTATE, uVMFlags, "uVMFlags");
280 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPendingAction, "uPendingAction");
281 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPatchCalls, "uPatchCalls");
282 ADD_MEMBER(hDbgMod, PATMGCSTATE, uScratch, "uScratch");
283 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEFlags, "uIretEFlags");
284 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretCS, "uIretCS");
285 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEIP, "uIretEIP");
286 ADD_MEMBER(hDbgMod, PATMGCSTATE, Psp, "Psp");
287 ADD_MEMBER(hDbgMod, PATMGCSTATE, fPIF, "fPIF");
288 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCPtrInhibitInterrupts, "GCPtrInhibitInterrupts");
289 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallPatchTargetAddr, "GCCallPatchTargetAddr");
290 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallReturnAddr, "GCCallReturnAddr");
291 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEAX, "Restore.uEAX");
292 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uECX, "Restore.uECX");
293 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEDI, "Restore.uEDI");
294 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.eFlags, "Restore.eFlags");
295 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uFlags, "Restore.uFlags");
296
297 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pGCStateGC, 0 /*fFlags*/);
298 AssertLogRelRC(rc);
299 RTDbgModRelease(hDbgMod);
300 }
301
302 /*
303 * Add something for the stats so we get some kind of symbols for
304 * references to them while disassembling patches.
305 */
306 rc = RTDbgModCreate(&hDbgMod, "patmstats", PATM_STAT_MEMSIZE, 0 /*fFlags*/);
307 if (RT_SUCCESS(rc))
308 {
309 ADD_FUNC(hDbgMod, pVM->patm.s.pStatsGC, pVM->patm.s.pStatsGC, PATM_STAT_MEMSIZE, "PATMMemStatsStart");
310
311 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pStatsGC, 0 /*fFlags*/);
312 AssertLogRelRC(rc);
313 RTDbgModRelease(hDbgMod);
314 }
315
316 /*
317 * Add a fake debug module for the patches and stack.
318 */
319 rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem + PATM_STACK_TOTAL_SIZE + PAGE_SIZE, 0 /*fFlags*/);
320 if (RT_SUCCESS(rc))
321 {
322 pVM->patm.s.hDbgModPatchMem = hDbgMod;
323 patmR3DbgAddPatches(pVM, hDbgMod);
324
325 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pPatchMemGC, 0 /*fFlags*/);
326 AssertLogRelRC(rc);
327 }
328}
329
330
331/**
332 * Annotates an instruction if patched.
333 *
334 * @param pVM The cross context VM structure.
335 * @param RCPtr The instruction address.
336 * @param cbInstr The instruction length.
337 * @param pszBuf The output buffer. This will be an empty string if the
338 * instruction wasn't patched. If it's patched, it will
339 * hold a symbol-like string describing the patch.
340 * @param cbBuf The size of the output buffer.
341 */
342VMMR3_INT_DECL(void) PATMR3DbgAnnotatePatchedInstruction(PVM pVM, RTRCPTR RCPtr, uint8_t cbInstr, char *pszBuf, size_t cbBuf)
343{
344 /*
345 * Always zero the buffer.
346 */
347 AssertReturnVoid(cbBuf > 0);
348 *pszBuf = '\0';
349
350 /*
351 * Drop out immediately if it cannot be a patched instruction.
352 */
353 if (!PATMIsEnabled(pVM))
354 return;
355 if ( RCPtr < pVM->patm.s.pPatchedInstrGCLowest
356 || RCPtr > pVM->patm.s.pPatchedInstrGCHighest)
357 return;
358
359 /*
360 * Look for a patch record covering any part of the instruction.
361 *
362 * The first query results in a patched less or equal to RCPtr. While the
363 * second results in one that's greater than RCPtr.
364 */
365 PPATMPATCHREC pPatchRec;
366 pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, false /*fFromAbove*/);
367 if ( !pPatchRec
368 || RCPtr - pPatchRec->patch.pPrivInstrGC > pPatchRec->patch.cbPrivInstr)
369 {
370 pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, true /*fFromAbove*/);
371 if ( !pPatchRec
372 || (RTRCPTR)(RCPtr + cbInstr) < pPatchRec->patch.pPrivInstrGC )
373 return;
374 }
375
376 /*
377 * Lazy bird uses the symbol name generation code for describing the patch.
378 */
379 size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, pszBuf, cbBuf);
380 if (off + 1 < cbBuf)
381 {
382 const char *pszState;
383 switch (pPatchRec->patch.uState)
384 {
385 case PATCH_REFUSED: pszState = "Refused"; break;
386 case PATCH_DISABLED: pszState = "Disabled"; break;
387 case PATCH_ENABLED: pszState = "Enabled"; break;
388 case PATCH_UNUSABLE: pszState = "Unusable"; break;
389 case PATCH_DIRTY: pszState = "Dirty"; break;
390 case PATCH_DISABLE_PENDING: pszState = "DisablePending"; break;
391 default: pszState = "State???"; AssertFailed(); break;
392 }
393
394 if (pPatchRec->patch.cbPatchBlockSize > 0)
395 off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b) - %#x LB %#x",
396 pszState, pPatchRec->patch.cbPatchJump,
397 pPatchRec->patch.pPatchBlockOffset + pVM->patm.s.pPatchMemGC,
398 pPatchRec->patch.cbPatchBlockSize);
399 else
400 off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b)", pszState, pPatchRec->patch.cbPatchJump);
401 }
402
403}
404
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