VirtualBox

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

Last change on this file since 46155 was 46150, checked in by vboxsync, 12 years ago

PATM: Patch symbols.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: PATMR3Dbg.cpp 46150 2013-05-17 17:21:45Z 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
98/**
99 * Called when a new patch is added or when first populating the address space.
100 *
101 * @param pVM The cross context VM structure.
102 * @param pPatchRec The patch record.
103 */
104void patmR3DbgAddPatch(PVM pVM, PPATMPATCHREC pPatchRec)
105{
106 if ( pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD
107 && pPatchRec->patch.pPatchBlockOffset > 0
108 && !(pPatchRec->patch.flags & PATMFL_GLOBAL_FUNCTIONS))
109 {
110 /** @todo find a cheap way of checking whether we've already added the patch.
111 * Using a flag would be nice, except I don't want to consider saved
112 * state considerations right now (I don't recall if we're still
113 * depending on structure layout there or not). */
114 int rc;
115 char szName[256];
116
117#define ADD_SZ(a_sz) \
118 do { \
119 if (cbLeft >= sizeof(a_sz)) \
120 { \
121 memcpy(pszName, a_sz, sizeof(a_sz)); \
122 pszName += sizeof(a_sz); \
123 cbLeft -= sizeof(a_sz);\
124 }\
125 } while (0)
126
127 /* Start the name off with the address of the guest code. */
128 size_t cch = RTStrPrintf(szName, sizeof(szName), "Patch_%#08x", pPatchRec->patch.pPrivInstrGC);
129 char *pszName = &szName[cch];
130 size_t cbLeft = sizeof(szName) - cch;
131
132 /* Append flags. */
133 uint64_t fFlags = pPatchRec->patch.flags;
134 if (fFlags & PATMFL_INTHANDLER)
135 ADD_SZ("_IntHandler");
136 if (fFlags & PATMFL_SYSENTER)
137 ADD_SZ("_SysEnter");
138 if (fFlags & PATMFL_GUEST_SPECIFIC)
139 ADD_SZ("_GuestSpecific");
140 if (fFlags & PATMFL_USER_MODE)
141 ADD_SZ("_UserMode");
142 if (fFlags & PATMFL_IDTHANDLER)
143 ADD_SZ("_IdtHandler");
144 if (fFlags & PATMFL_TRAPHANDLER)
145 ADD_SZ("_TrapHandler");
146 if (fFlags & PATMFL_DUPLICATE_FUNCTION)
147 ADD_SZ("_DupFunc");
148 if (fFlags & PATMFL_REPLACE_FUNCTION_CALL)
149 ADD_SZ("_ReplFunc");
150 if (fFlags & PATMFL_TRAPHANDLER_WITH_ERRORCODE)
151 ADD_SZ("_TrapHandlerErrCd");
152 if (fFlags & PATMFL_MMIO_ACCESS)
153 ADD_SZ("_MmioAccess");
154 if (fFlags & PATMFL_SYSENTER_XP)
155 ADD_SZ("_SysEnterXP");
156 if (fFlags & PATMFL_INT3_REPLACEMENT)
157 ADD_SZ("_Int3Replacement");
158 if (fFlags & PATMFL_SUPPORT_CALLS)
159 ADD_SZ("_SupportCalls");
160 if (fFlags & PATMFL_SUPPORT_INDIRECT_CALLS)
161 ADD_SZ("_SupportIndirectCalls");
162 if (fFlags & PATMFL_IDTHANDLER_WITHOUT_ENTRYPOINT)
163 ADD_SZ("_IdtHandlerWE");
164 if (fFlags & PATMFL_INHIBIT_IRQS)
165 ADD_SZ("_InhibitIrqs");
166 if (fFlags & PATMFL_RECOMPILE_NEXT)
167 ADD_SZ("_RecompileNext");
168 if (fFlags & PATMFL_CALLABLE_AS_FUNCTION)
169 ADD_SZ("_Callable");
170 if (fFlags & PATMFL_TRAMPOLINE)
171 ADD_SZ("_Trampoline");
172 if (fFlags & PATMFL_PATCHED_GUEST_CODE)
173 ADD_SZ("_PatchedGuestCode");
174 if (fFlags & PATMFL_MUST_INSTALL_PATCHJMP)
175 ADD_SZ("_MustInstallPatchJmp");
176 if (fFlags & PATMFL_INT3_REPLACEMENT_BLOCK)
177 ADD_SZ("_Int3ReplacementBlock");
178 if (fFlags & PATMFL_EXTERNAL_JUMP_INSIDE)
179 ADD_SZ("_ExtJmp");
180 if (fFlags & PATMFL_CODE_REFERENCED)
181 ADD_SZ("_CodeRefed");
182
183 /* If we have a symbol near the guest address, append that. */
184 if (cbLeft > 8)
185 {
186 DBGFSYMBOL Symbol;
187 RTGCINTPTR offDisp;
188
189 rc = DBGFR3SymbolByAddr(pVM, pPatchRec->patch.pPrivInstrGC, &offDisp, &Symbol);
190 if (RT_SUCCESS(rc))
191 {
192 ADD_SZ("__");
193 RTStrCopy(pszName, cbLeft, Symbol.szName);
194 }
195 }
196
197 /* Add it (may fail due to enable/disable patches). */
198 RTDbgModSymbolAdd(pVM->patm.s.hDbgModPatchMem, szName, 0 /*iSeg*/,
199 pPatchRec->patch.pPatchBlockOffset,
200 pPatchRec->patch.cbPatchBlockSize,
201 0 /*fFlags*/, NULL /*piOrdinal*/);
202
203 }
204}
205
206
207/**
208 * Enumeration callback used by patmR3DbgAddPatches
209 *
210 * @returns 0 (continue enum)
211 * @param pNode The patch record node.
212 * @param pvUser The cross context VM structure.
213 */
214static DECLCALLBACK(int) patmR3DbgAddPatchCallback(PAVLOU32NODECORE pNode, void *pvUser)
215{
216 patmR3DbgAddPatch((PVM)pvUser, (PPATMPATCHREC)pNode);
217 return 0;
218}
219
220
221/**
222 * Populates an empty "patches" (hDbgModPatchMem) module with patch symbols.
223 *
224 * @param pVM The cross context VM structure.
225 * @param hDbgMod The debug module handle.
226 */
227static void patmR3DbgAddPatches(PVM pVM, RTDBGMOD hDbgMod)
228{
229 /*
230 * Global functions.
231 */
232 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, PATMLookupAndCallRecord.size, "PATMLookupAndCall");
233 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, PATMRetFunctionRecord.size, "PATMRetFunction");
234 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, PATMLookupAndJumpRecord.size, "PATMLookupAndJump");
235 ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, PATMIretFunctionRecord.size, "PATMIretFunction");
236
237 /*
238 * The patches.
239 */
240 RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
241}
242
243
244/**
245 * Populate DBGF_AS_RC with PATM symbols.
246 *
247 * Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
248 * accessed for the first time.
249 *
250 * @param pVM The cross context VM structure.
251 * @param hDbgAs The DBGF_AS_RC address space handle.
252 */
253VMMR3_INT_DECL(void) PATMR3DbgPopulateAddrSpace(PVM pVM, RTDBGAS hDbgAs)
254{
255 AssertReturnVoid(!HMIsEnabled(pVM));
256
257 /*
258 * Add a fake debug module for the PATMGCSTATE structure.
259 */
260 RTDBGMOD hDbgMod;
261 int rc = RTDbgModCreate(&hDbgMod, "patmgcstate", sizeof(PATMGCSTATE), 0 /*fFlags*/);
262 if (RT_SUCCESS(rc))
263 {
264 ADD_MEMBER(hDbgMod, PATMGCSTATE, uVMFlags, "uVMFlags");
265 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPendingAction, "uPendingAction");
266 ADD_MEMBER(hDbgMod, PATMGCSTATE, uPatchCalls, "uPatchCalls");
267 ADD_MEMBER(hDbgMod, PATMGCSTATE, uScratch, "uScratch");
268 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEFlags, "uIretEFlags");
269 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretCS, "uIretCS");
270 ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEIP, "uIretEIP");
271 ADD_MEMBER(hDbgMod, PATMGCSTATE, Psp, "Psp");
272 ADD_MEMBER(hDbgMod, PATMGCSTATE, fPIF, "fPIF");
273 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCPtrInhibitInterrupts, "GCPtrInhibitInterrupts");
274 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallPatchTargetAddr, "GCCallPatchTargetAddr");
275 ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallReturnAddr, "GCCallReturnAddr");
276 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEAX, "Restore.uEAX");
277 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uECX, "Restore.uECX");
278 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEDI, "Restore.uEDI");
279 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.eFlags, "Restore.eFlags");
280 ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uFlags, "Restore.uFlags");
281
282 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pGCStateGC, 0 /*fFlags/*/);
283 AssertLogRelRC(rc);
284 RTDbgModRelease(hDbgMod);
285 }
286
287 /*
288 * Add a fake debug module for the patches.
289 */
290 rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem, 0 /*fFlags*/);
291 if (RT_SUCCESS(rc))
292 {
293 pVM->patm.s.hDbgModPatchMem = hDbgMod;
294 patmR3DbgAddPatches(pVM, hDbgMod);
295
296 rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pPatchMemGC, 0 /*fFlags/*/);
297 AssertLogRelRC(rc);
298 }
299}
300
301
Note: See TracBrowser for help on using the repository browser.

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