VirtualBox

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

Last change on this file since 57009 was 56287, checked in by vboxsync, 10 years ago

VMM: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1/* $Id: PATMR3Dbg.cpp 56287 2015-06-09 11:15:22Z vboxsync $ */
2/** @file
3 * PATM - Dynamic Guest OS Patching Manager, Debugger Related Parts.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 and a start marker.
242 */
243 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, g_patmLookupAndCallRecord.cbFunction, "PATMLookupAndCall");
244 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, g_patmRetFunctionRecord.cbFunction, "PATMRetFunction");
245 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, g_patmLookupAndJumpRecord.cbFunction, "PATMLookupAndJump");
246 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, g_patmIretFunctionRecord.cbFunction, "PATMIretFunction");
247
248 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pPatchMemGC, 0, "PatchMemStart");
249 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pGCStackGC, PATM_STACK_TOTAL_SIZE, "PATMStack");
250
251 /*
252 * The patches.
253 */
254 RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
255}
256
257
258/**
259 * Populate DBGF_AS_RC with PATM symbols.
260 *
261 * Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
262 * accessed for the first time.
263 *
264 * @param pVM The cross context VM structure.
265 * @param hDbgAs The DBGF_AS_RC address space handle.
266 */
267VMMR3_INT_DECL(void) PATMR3DbgPopulateAddrSpace(PVM pVM, RTDBGAS hDbgAs)
268{
269 AssertReturnVoid(!HMIsEnabled(pVM));
270
271 /*
272 * Add a fake debug module for the PATMGCSTATE structure.
273 */
274 RTDBGMOD hDbgMod;
275 int rc = RTDbgModCreate(&hDbgMod, "patmgcstate", sizeof(PATMGCSTATE), 0 /*fFlags*/);
276 if (RT_SUCCESS(rc))
277 {
278 ADD_MEMBER(hDbgMod, PATMGCSTATE, uVMFlags, "uVMFlags");
279 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPendingAction, "uPendingAction");
280 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPatchCalls, "uPatchCalls");
281 ADD_MEMBER(hDbgMod, PATMGCSTATE, uScratch, "uScratch");
282 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEFlags, "uIretEFlags");
283 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretCS, "uIretCS");
284 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEIP, "uIretEIP");
285 ADD_MEMBER(hDbgMod, PATMGCSTATE, Psp, "Psp");
286 ADD_MEMBER(hDbgMod, PATMGCSTATE, fPIF, "fPIF");
287 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCPtrInhibitInterrupts, "GCPtrInhibitInterrupts");
288 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallPatchTargetAddr, "GCCallPatchTargetAddr");
289 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallReturnAddr, "GCCallReturnAddr");
290 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEAX, "Restore.uEAX");
291 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uECX, "Restore.uECX");
292 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEDI, "Restore.uEDI");
293 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.eFlags, "Restore.eFlags");
294 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uFlags, "Restore.uFlags");
295
296 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pGCStateGC, 0 /*fFlags/*/);
297 AssertLogRelRC(rc);
298 RTDbgModRelease(hDbgMod);
299 }
300
301 /*
302 * Add something for the stats so we get some kind of symbols for
303 * references to them while disassembling patches.
304 */
305 rc = RTDbgModCreate(&hDbgMod, "patmstats", PATM_STAT_MEMSIZE, 0 /*fFlags*/);
306 if (RT_SUCCESS(rc))
307 {
308 ADD_FUNC(hDbgMod, pVM->patm.s.pStatsGC, pVM->patm.s.pStatsGC, PATM_STAT_MEMSIZE, "PATMMemStatsStart");
309
310 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pStatsGC, 0 /*fFlags/*/);
311 AssertLogRelRC(rc);
312 RTDbgModRelease(hDbgMod);
313 }
314
315 /*
316 * Add a fake debug module for the patches and stack.
317 */
318 rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem + PATM_STACK_TOTAL_SIZE + PAGE_SIZE, 0 /*fFlags*/);
319 if (RT_SUCCESS(rc))
320 {
321 pVM->patm.s.hDbgModPatchMem = hDbgMod;
322 patmR3DbgAddPatches(pVM, hDbgMod);
323
324 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pPatchMemGC, 0 /*fFlags/*/);
325 AssertLogRelRC(rc);
326 }
327}
328
329
330/**
331 * Annotates an instruction if patched.
332 *
333 * @param pVM The VM handle.
334 * @param RCPtr The instruction address.
335 * @param cbInstr The instruction length.
336 * @param pszBuf The output buffer. This will be an empty string
337 * if the instruction wasn't patched. If it's
338 * patched, it will hold a symbol-like string
339 * 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