VirtualBox

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

Last change on this file since 46167 was 46167, checked in by vboxsync, 11 years ago

Buried DBGFSym.cpp and with it loadsyms - rip.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use