1 | /* $Id: PATMSSM.cpp 35346 2010-12-27 16:13:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PATMSSM - Dynamic Guest OS Patching Manager; Save and load state
|
---|
4 | *
|
---|
5 | * NOTE: CSAM assumes patch memory is never reused!!
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #define LOG_GROUP LOG_GROUP_PATM
|
---|
24 | #include <VBox/vmm/patm.h>
|
---|
25 | #include <VBox/vmm/cpum.h>
|
---|
26 | #include <VBox/vmm/mm.h>
|
---|
27 | #include <VBox/vmm/ssm.h>
|
---|
28 | #include <VBox/param.h>
|
---|
29 | #include <iprt/avl.h>
|
---|
30 | #include "PATMInternal.h"
|
---|
31 | #include "PATMPatch.h"
|
---|
32 | #include "PATMA.h"
|
---|
33 | #include <VBox/vmm/vm.h>
|
---|
34 | #include <VBox/vmm/csam.h>
|
---|
35 | #include "internal/pgm.h"
|
---|
36 | #include <VBox/dbg.h>
|
---|
37 | #include <VBox/err.h>
|
---|
38 | #include <VBox/log.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <VBox/dis.h>
|
---|
43 | #include <VBox/disopcode.h>
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | #define PATM_SUBTRACT_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) - (uintptr_t)(b)
|
---|
49 | #define PATM_ADD_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) + (uintptr_t)(b)
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Internal Functions *
|
---|
53 | *******************************************************************************/
|
---|
54 | static void patmCorrectFixup(PVM pVM, unsigned ulSSMVersion, PATM &patmInfo, PPATCHINFO pPatch, PRELOCREC pRec, int32_t offset, RTRCPTR *pFixup);
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Global Variables *
|
---|
58 | *******************************************************************************/
|
---|
59 | /**
|
---|
60 | * SSM descriptor table for the PATM structure.
|
---|
61 | */
|
---|
62 | static SSMFIELD const g_aPatmFields[] =
|
---|
63 | {
|
---|
64 | /** @todo there are a bunch more fields here which can be marked as ignored. */
|
---|
65 | SSMFIELD_ENTRY_IGNORE( PATM, offVM),
|
---|
66 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchMemGC),
|
---|
67 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pPatchMemHC),
|
---|
68 | SSMFIELD_ENTRY( PATM, cbPatchMem),
|
---|
69 | SSMFIELD_ENTRY( PATM, offPatchMem),
|
---|
70 | SSMFIELD_ENTRY( PATM, fOutOfMemory),
|
---|
71 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
72 | SSMFIELD_ENTRY( PATM, deltaReloc),
|
---|
73 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStateHC),
|
---|
74 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStateGC),
|
---|
75 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStackGC),
|
---|
76 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStackHC),
|
---|
77 | SSMFIELD_ENTRY_RCPTR( PATM, pCPUMCtxGC),
|
---|
78 | SSMFIELD_ENTRY_RCPTR( PATM, pStatsGC),
|
---|
79 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pStatsHC),
|
---|
80 | SSMFIELD_ENTRY( PATM, uCurrentPatchIdx),
|
---|
81 | SSMFIELD_ENTRY( PATM, ulCallDepth),
|
---|
82 | SSMFIELD_ENTRY( PATM, cPageRecords),
|
---|
83 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCLowest),
|
---|
84 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCHighest),
|
---|
85 | SSMFIELD_ENTRY_RCPTR( PATM, PatchLookupTreeGC),
|
---|
86 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, PatchLookupTreeHC),
|
---|
87 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperCallGC),
|
---|
88 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperRetGC),
|
---|
89 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperJumpGC),
|
---|
90 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperIretGC),
|
---|
91 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGlobalPatchRec),
|
---|
92 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterGC),
|
---|
93 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterPatchGC),
|
---|
94 | SSMFIELD_ENTRY( PATM, uSysEnterPatchIdx),
|
---|
95 | SSMFIELD_ENTRY_RCPTR( PATM, pvFaultMonitor),
|
---|
96 | SSMFIELD_ENTRY_GCPHYS( PATM, mmio.GCPhys),
|
---|
97 | SSMFIELD_ENTRY_RCPTR( PATM, mmio.pCachedData),
|
---|
98 | SSMFIELD_ENTRY_IGN_RCPTR( PATM, mmio.Alignment0),
|
---|
99 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, savedstate.pSSM),
|
---|
100 | SSMFIELD_ENTRY( PATM, savedstate.cPatches),
|
---|
101 | SSMFIELD_ENTRY_PAD_HC64( PATM, savedstate.Alignment0, sizeof(uint32_t)),
|
---|
102 | SSMFIELD_ENTRY_IGNORE( PATM, StatNrOpcodeRead),
|
---|
103 | SSMFIELD_ENTRY_IGNORE( PATM, StatDisabled),
|
---|
104 | SSMFIELD_ENTRY_IGNORE( PATM, StatUnusable),
|
---|
105 | SSMFIELD_ENTRY_IGNORE( PATM, StatEnabled),
|
---|
106 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalled),
|
---|
107 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledFunctionPatches),
|
---|
108 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledTrampoline),
|
---|
109 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledJump),
|
---|
110 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3Callable),
|
---|
111 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3BlockRun),
|
---|
112 | SSMFIELD_ENTRY_IGNORE( PATM, StatOverwritten),
|
---|
113 | SSMFIELD_ENTRY_IGNORE( PATM, StatFixedConflicts),
|
---|
114 | SSMFIELD_ENTRY_IGNORE( PATM, StatFlushed),
|
---|
115 | SSMFIELD_ENTRY_IGNORE( PATM, StatPageBoundaryCrossed),
|
---|
116 | SSMFIELD_ENTRY_IGNORE( PATM, StatMonitored),
|
---|
117 | SSMFIELD_ENTRY_IGNORE( PATM, StatHandleTrap),
|
---|
118 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBack),
|
---|
119 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBackFail),
|
---|
120 | SSMFIELD_ENTRY_IGNORE( PATM, StatPATMMemoryUsed),
|
---|
121 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQSuccess),
|
---|
122 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQFailed),
|
---|
123 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateUseExisting),
|
---|
124 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionFound),
|
---|
125 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionNotFound),
|
---|
126 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWrite),
|
---|
127 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteDetect),
|
---|
128 | SSMFIELD_ENTRY_IGNORE( PATM, StatDirty),
|
---|
129 | SSMFIELD_ENTRY_IGNORE( PATM, StatPushTrap),
|
---|
130 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpreted),
|
---|
131 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpretedFailed),
|
---|
132 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysEnter),
|
---|
133 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysExit),
|
---|
134 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIret),
|
---|
135 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIretFailed),
|
---|
136 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirty),
|
---|
137 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyGood),
|
---|
138 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyBad),
|
---|
139 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageInserted),
|
---|
140 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageRemoved),
|
---|
141 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshSuccess),
|
---|
142 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshFailed),
|
---|
143 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRet),
|
---|
144 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRetReused),
|
---|
145 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenJump),
|
---|
146 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenCall),
|
---|
147 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenPopf),
|
---|
148 | SSMFIELD_ENTRY_IGNORE( PATM, StatCheckPendingIRQ),
|
---|
149 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupReplace),
|
---|
150 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupInsert),
|
---|
151 | SSMFIELD_ENTRY_IGNORE( PATM, StatU32FunctionMaxSlotsUsed),
|
---|
152 | SSMFIELD_ENTRY_IGNORE( PATM, Alignment0),
|
---|
153 | SSMFIELD_ENTRY_TERM()
|
---|
154 | };
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * SSM descriptor table for the PATMGCSTATE structure.
|
---|
158 | */
|
---|
159 | static SSMFIELD const g_aPatmGCStateFields[] =
|
---|
160 | {
|
---|
161 | SSMFIELD_ENTRY( PATMGCSTATE, uVMFlags),
|
---|
162 | SSMFIELD_ENTRY( PATMGCSTATE, uPendingAction),
|
---|
163 | SSMFIELD_ENTRY( PATMGCSTATE, uPatchCalls),
|
---|
164 | SSMFIELD_ENTRY( PATMGCSTATE, uScratch),
|
---|
165 | SSMFIELD_ENTRY( PATMGCSTATE, uIretEFlags),
|
---|
166 | SSMFIELD_ENTRY( PATMGCSTATE, uIretCS),
|
---|
167 | SSMFIELD_ENTRY( PATMGCSTATE, uIretEIP),
|
---|
168 | SSMFIELD_ENTRY( PATMGCSTATE, Psp),
|
---|
169 | SSMFIELD_ENTRY( PATMGCSTATE, fPIF),
|
---|
170 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCPtrInhibitInterrupts),
|
---|
171 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCCallPatchTargetAddr),
|
---|
172 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCCallReturnAddr),
|
---|
173 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uEAX),
|
---|
174 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uECX),
|
---|
175 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uEDI),
|
---|
176 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.eFlags),
|
---|
177 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uFlags),
|
---|
178 | SSMFIELD_ENTRY_TERM()
|
---|
179 | };
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * SSM descriptor table for the PATMPATCHREC structure.
|
---|
183 | */
|
---|
184 | static SSMFIELD const g_aPatmPatchRecFields[] =
|
---|
185 | {
|
---|
186 | SSMFIELD_ENTRY( PATMPATCHREC, Core.Key),
|
---|
187 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, Core.pLeft),
|
---|
188 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, Core.pRight),
|
---|
189 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, Core.uchHeight),
|
---|
190 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
191 | SSMFIELD_ENTRY( PATMPATCHREC, CoreOffset.Key),
|
---|
192 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, CoreOffset.pLeft),
|
---|
193 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, CoreOffset.pRight),
|
---|
194 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, CoreOffset.uchHeight),
|
---|
195 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
196 | SSMFIELD_ENTRY( PATMPATCHREC, patch.uState),
|
---|
197 | SSMFIELD_ENTRY( PATMPATCHREC, patch.uOldState),
|
---|
198 | SSMFIELD_ENTRY( PATMPATCHREC, patch.uOpMode),
|
---|
199 | SSMFIELD_ENTRY_RCPTR( PATMPATCHREC, patch.pPrivInstrGC),
|
---|
200 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.unusedHC),
|
---|
201 | SSMFIELD_ENTRY( PATMPATCHREC, patch.aPrivInstr),
|
---|
202 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cbPrivInstr),
|
---|
203 | SSMFIELD_ENTRY( PATMPATCHREC, patch.opcode),
|
---|
204 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cbPatchJump),
|
---|
205 | SSMFIELD_ENTRY_RCPTR( PATMPATCHREC, patch.pPatchJumpDestGC),
|
---|
206 | SSMFIELD_ENTRY( PATMPATCHREC, patch.pPatchBlockOffset),
|
---|
207 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cbPatchBlockSize),
|
---|
208 | SSMFIELD_ENTRY( PATMPATCHREC, patch.uCurPatchOffset),
|
---|
209 | SSMFIELD_ENTRY_PAD_HC64( PATMPATCHREC, patch.Alignment0, sizeof(uint32_t)),
|
---|
210 | SSMFIELD_ENTRY( PATMPATCHREC, patch.flags),
|
---|
211 | SSMFIELD_ENTRY_RCPTR( PATMPATCHREC, patch.pInstrGCLowest),
|
---|
212 | SSMFIELD_ENTRY_RCPTR( PATMPATCHREC, patch.pInstrGCHighest),
|
---|
213 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.FixupTree),
|
---|
214 | SSMFIELD_ENTRY( PATMPATCHREC, patch.nrFixups),
|
---|
215 | SSMFIELD_ENTRY( PATMPATCHREC, patch.nrJumpRecs), // should be zero?
|
---|
216 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.JumpTree),
|
---|
217 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.Patch2GuestAddrTree),
|
---|
218 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.Guest2PatchAddrTree),
|
---|
219 | SSMFIELD_ENTRY( PATMPATCHREC, patch.nrPatch2GuestRecs),
|
---|
220 | SSMFIELD_ENTRY_PAD_HC64( PATMPATCHREC, patch.Alignment1, sizeof(uint32_t)),
|
---|
221 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.unused.pPatchLocStartHC), // saved as zero
|
---|
222 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.unused.pPatchLocEndHC), // ditto
|
---|
223 | SSMFIELD_ENTRY_IGN_RCPTR( PATMPATCHREC, patch.unused.pGuestLoc), // ditto
|
---|
224 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, patch.unused.opsize), // ditto
|
---|
225 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHREC, patch.pTempInfo),
|
---|
226 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cCodeWrites),
|
---|
227 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cTraps),
|
---|
228 | SSMFIELD_ENTRY( PATMPATCHREC, patch.cInvalidWrites),
|
---|
229 | SSMFIELD_ENTRY( PATMPATCHREC, patch.uPatchIdx),
|
---|
230 | SSMFIELD_ENTRY( PATMPATCHREC, patch.bDirtyOpcode),
|
---|
231 | SSMFIELD_ENTRY_IGNORE( PATMPATCHREC, patch.Alignment2),
|
---|
232 | SSMFIELD_ENTRY_TERM()
|
---|
233 | };
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * SSM descriptor table for the RELOCREC structure.
|
---|
237 | */
|
---|
238 | static SSMFIELD const g_aPatmRelocRec[] =
|
---|
239 | {
|
---|
240 | SSMFIELD_ENTRY_HCPTR_HACK_U32( RELOCREC, Core.Key), // Used to store the relocation type
|
---|
241 | SSMFIELD_ENTRY_IGN_HCPTR( RELOCREC, Core.pLeft),
|
---|
242 | SSMFIELD_ENTRY_IGN_HCPTR( RELOCREC, Core.pRight),
|
---|
243 | SSMFIELD_ENTRY_IGNORE( RELOCREC, Core.uchHeight),
|
---|
244 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 7),
|
---|
245 | SSMFIELD_ENTRY( RELOCREC, uType),
|
---|
246 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
247 | SSMFIELD_ENTRY_HCPTR_HACK_U32( RELOCREC, pRelocPos), // converted to a patch member offset.
|
---|
248 | SSMFIELD_ENTRY_RCPTR( RELOCREC, pSource),
|
---|
249 | SSMFIELD_ENTRY_RCPTR( RELOCREC, pDest),
|
---|
250 | SSMFIELD_ENTRY_TERM()
|
---|
251 | };
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * SSM descriptor table for the RECPATCHTOGUEST structure.
|
---|
255 | */
|
---|
256 | static SSMFIELD const g_aPatmRecPatchToGuest[] =
|
---|
257 | {
|
---|
258 | SSMFIELD_ENTRY( RECPATCHTOGUEST, Core.Key),
|
---|
259 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
260 | SSMFIELD_ENTRY_IGN_HCPTR( RECPATCHTOGUEST, Core.pLeft),
|
---|
261 | SSMFIELD_ENTRY_IGN_HCPTR( RECPATCHTOGUEST, Core.pRight),
|
---|
262 | SSMFIELD_ENTRY_IGNORE( RECPATCHTOGUEST, Core.uchHeight),
|
---|
263 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 7),
|
---|
264 | SSMFIELD_ENTRY_RCPTR( RECPATCHTOGUEST, pOrgInstrGC),
|
---|
265 | SSMFIELD_ENTRY( RECPATCHTOGUEST, enmType),
|
---|
266 | SSMFIELD_ENTRY( RECPATCHTOGUEST, fDirty),
|
---|
267 | SSMFIELD_ENTRY( RECPATCHTOGUEST, fJumpTarget),
|
---|
268 | SSMFIELD_ENTRY( RECPATCHTOGUEST, u8DirtyOpcode),
|
---|
269 | SSMFIELD_ENTRY_PAD_HC_AUTO( 1, 5),
|
---|
270 | SSMFIELD_ENTRY_TERM()
|
---|
271 | };
|
---|
272 |
|
---|
273 |
|
---|
274 | #ifdef VBOX_STRICT
|
---|
275 | /**
|
---|
276 | * Callback function for RTAvlPVDoWithAll
|
---|
277 | *
|
---|
278 | * Counts the number of patches in the tree
|
---|
279 | *
|
---|
280 | * @returns VBox status code.
|
---|
281 | * @param pNode Current node
|
---|
282 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
283 | */
|
---|
284 | static DECLCALLBACK(int) patmCountLeafPV(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
285 | {
|
---|
286 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
287 | return VINF_SUCCESS;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Callback function for RTAvlU32DoWithAll
|
---|
292 | *
|
---|
293 | * Counts the number of patches in the tree
|
---|
294 | *
|
---|
295 | * @returns VBox status code.
|
---|
296 | * @param pNode Current node
|
---|
297 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
298 | */
|
---|
299 | static DECLCALLBACK(int) patmCountLeaf(PAVLU32NODECORE pNode, void *pcPatches)
|
---|
300 | {
|
---|
301 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
302 | return VINF_SUCCESS;
|
---|
303 | }
|
---|
304 | #endif /* VBOX_STRICT */
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Callback function for RTAvloU32DoWithAll
|
---|
308 | *
|
---|
309 | * Counts the number of patches in the tree
|
---|
310 | *
|
---|
311 | * @returns VBox status code.
|
---|
312 | * @param pNode Current node
|
---|
313 | * @param pcPatches Pointer to patch counter
|
---|
314 | */
|
---|
315 | static DECLCALLBACK(int) patmCountPatch(PAVLOU32NODECORE pNode, void *pcPatches)
|
---|
316 | {
|
---|
317 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Callback function for RTAvlU32DoWithAll
|
---|
323 | *
|
---|
324 | * Saves all patch to guest lookup records.
|
---|
325 | *
|
---|
326 | * @returns VBox status code.
|
---|
327 | * @param pNode Current node
|
---|
328 | * @param pVM1 VM Handle
|
---|
329 | */
|
---|
330 | static DECLCALLBACK(int) patmSaveP2GLookupRecords(PAVLU32NODECORE pNode, void *pVM1)
|
---|
331 | {
|
---|
332 | PVM pVM = (PVM)pVM1;
|
---|
333 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
334 | PRECPATCHTOGUEST pPatchToGuestRec = (PRECPATCHTOGUEST)pNode;
|
---|
335 |
|
---|
336 | /* Save the lookup record. */
|
---|
337 | int rc = SSMR3PutMem(pSSM, pPatchToGuestRec, sizeof(RECPATCHTOGUEST));
|
---|
338 | AssertRCReturn(rc, rc);
|
---|
339 |
|
---|
340 | return VINF_SUCCESS;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Callback function for RTAvlPVDoWithAll
|
---|
345 | *
|
---|
346 | * Saves all patch to guest lookup records.
|
---|
347 | *
|
---|
348 | * @returns VBox status code.
|
---|
349 | * @param pNode Current node
|
---|
350 | * @param pVM1 VM Handle
|
---|
351 | */
|
---|
352 | static DECLCALLBACK(int) patmSaveFixupRecords(PAVLPVNODECORE pNode, void *pVM1)
|
---|
353 | {
|
---|
354 | PVM pVM = (PVM)pVM1;
|
---|
355 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
356 | RELOCREC rec = *(PRELOCREC)pNode;
|
---|
357 | RTRCPTR *pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
358 |
|
---|
359 | Assert(rec.pRelocPos);
|
---|
360 | /* Convert pointer to an offset into patch memory. */
|
---|
361 | PATM_SUBTRACT_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
362 |
|
---|
363 | if (rec.uType == FIXUP_ABSOLUTE)
|
---|
364 | {
|
---|
365 | /* Core.Key abused to store the fixup type. */
|
---|
366 | if (*pFixup == pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions))
|
---|
367 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPU_FF_ACTION;
|
---|
368 | else
|
---|
369 | if (*pFixup == CPUMR3GetGuestCpuIdDefRCPtr(pVM))
|
---|
370 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_DEFAULT;
|
---|
371 | else
|
---|
372 | if (*pFixup == CPUMR3GetGuestCpuIdStdRCPtr(pVM))
|
---|
373 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_STANDARD;
|
---|
374 | else
|
---|
375 | if (*pFixup == CPUMR3GetGuestCpuIdExtRCPtr(pVM))
|
---|
376 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_EXTENDED;
|
---|
377 | else
|
---|
378 | if (*pFixup == CPUMR3GetGuestCpuIdCentaurRCPtr(pVM))
|
---|
379 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_CENTAUR;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* Save the lookup record. */
|
---|
383 | int rc = SSMR3PutMem(pSSM, &rec, sizeof(rec));
|
---|
384 | AssertRCReturn(rc, rc);
|
---|
385 |
|
---|
386 | return VINF_SUCCESS;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Callback function for RTAvloU32DoWithAll
|
---|
392 | *
|
---|
393 | * Saves the state of the patch that's being enumerated
|
---|
394 | *
|
---|
395 | * @returns VBox status code.
|
---|
396 | * @param pNode Current node
|
---|
397 | * @param pVM1 VM Handle
|
---|
398 | */
|
---|
399 | static DECLCALLBACK(int) patmSavePatchState(PAVLOU32NODECORE pNode, void *pVM1)
|
---|
400 | {
|
---|
401 | PVM pVM = (PVM)pVM1;
|
---|
402 | PPATMPATCHREC pPatch = (PPATMPATCHREC)pNode;
|
---|
403 | PATMPATCHREC patch = *pPatch;
|
---|
404 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
405 | int rc;
|
---|
406 |
|
---|
407 | Assert(!(pPatch->patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
411 | */
|
---|
412 | AssertMsg(patch.patch.uState == PATCH_REFUSED || (patch.patch.pPatchBlockOffset || (patch.patch.flags & (PATMFL_SYSENTER_XP|PATMFL_INT3_REPLACEMENT))),
|
---|
413 | ("State = %x pPatchBlockHC=%08x flags=%x\n", patch.patch.uState, PATCHCODE_PTR_HC(&patch.patch), patch.patch.flags));
|
---|
414 | Assert(pPatch->patch.JumpTree == 0);
|
---|
415 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->DisasmJumpTree == 0);
|
---|
416 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->IllegalInstrTree == 0);
|
---|
417 |
|
---|
418 | /* Save the patch record itself */
|
---|
419 | rc = SSMR3PutMem(pSSM, &patch, sizeof(patch));
|
---|
420 | AssertRCReturn(rc, rc);
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * Reset HC pointers in fixup records and save them.
|
---|
424 | */
|
---|
425 | #ifdef VBOX_STRICT
|
---|
426 | uint32_t nrFixupRecs = 0;
|
---|
427 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmCountLeafPV, &nrFixupRecs);
|
---|
428 | AssertMsg(nrFixupRecs == pPatch->patch.nrFixups, ("Fixup inconsistency! counted %d vs %d\n", nrFixupRecs, pPatch->patch.nrFixups));
|
---|
429 | #endif
|
---|
430 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmSaveFixupRecords, pVM);
|
---|
431 |
|
---|
432 | #ifdef VBOX_STRICT
|
---|
433 | uint32_t nrLookupRecords = 0;
|
---|
434 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmCountLeaf, &nrLookupRecords);
|
---|
435 | Assert(nrLookupRecords == pPatch->patch.nrPatch2GuestRecs);
|
---|
436 | #endif
|
---|
437 |
|
---|
438 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmSaveP2GLookupRecords, pVM);
|
---|
439 | return VINF_SUCCESS;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Execute state save operation.
|
---|
444 | *
|
---|
445 | * @returns VBox status code.
|
---|
446 | * @param pVM VM Handle.
|
---|
447 | * @param pSSM SSM operation handle.
|
---|
448 | */
|
---|
449 | DECLCALLBACK(int) patmR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
450 | {
|
---|
451 | PATM patmInfo = pVM->patm.s;
|
---|
452 | int rc;
|
---|
453 |
|
---|
454 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
458 | */
|
---|
459 | patmInfo.pPatchMemHC = NULL;
|
---|
460 | patmInfo.pGCStateHC = 0;
|
---|
461 | patmInfo.pvFaultMonitor = 0;
|
---|
462 |
|
---|
463 | Assert(patmInfo.ulCallDepth == 0);
|
---|
464 |
|
---|
465 | /*
|
---|
466 | * Count the number of patches in the tree (feeling lazy)
|
---|
467 | */
|
---|
468 | patmInfo.savedstate.cPatches = 0;
|
---|
469 | RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmCountPatch, &patmInfo.savedstate.cPatches);
|
---|
470 |
|
---|
471 | /*
|
---|
472 | * Save PATM structure
|
---|
473 | */
|
---|
474 | rc = SSMR3PutMem(pSSM, &patmInfo, sizeof(patmInfo));
|
---|
475 | AssertRCReturn(rc, rc);
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * Save patch memory contents
|
---|
479 | */
|
---|
480 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
481 | AssertRCReturn(rc, rc);
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Save GC state memory
|
---|
485 | */
|
---|
486 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
487 | AssertRCReturn(rc, rc);
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * Save PATM stack page
|
---|
491 | */
|
---|
492 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
493 | AssertRCReturn(rc, rc);
|
---|
494 |
|
---|
495 | /*
|
---|
496 | * Save all patches
|
---|
497 | */
|
---|
498 | rc = RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmSavePatchState, pVM);
|
---|
499 | AssertRCReturn(rc, rc);
|
---|
500 |
|
---|
501 | /** @note patch statistics are not saved. */
|
---|
502 |
|
---|
503 | return VINF_SUCCESS;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Execute state load operation.
|
---|
508 | *
|
---|
509 | * @returns VBox status code.
|
---|
510 | * @param pVM VM Handle.
|
---|
511 | * @param pSSM SSM operation handle.
|
---|
512 | * @param uVersion Data layout version.
|
---|
513 | * @param uPass The data pass.
|
---|
514 | */
|
---|
515 | DECLCALLBACK(int) patmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
516 | {
|
---|
517 | PATM patmInfo;
|
---|
518 | int rc;
|
---|
519 |
|
---|
520 | if ( uVersion != PATM_SSM_VERSION
|
---|
521 | && uVersion != PATM_SSM_VERSION_FIXUP_HACK
|
---|
522 | && uVersion != PATM_SSM_VERSION_VER16
|
---|
523 | )
|
---|
524 | {
|
---|
525 | AssertMsgFailed(("patmR3Load: Invalid version uVersion=%d!\n", uVersion));
|
---|
526 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
527 | }
|
---|
528 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
529 |
|
---|
530 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Restore PATM structure
|
---|
534 | */
|
---|
535 | RT_ZERO(patmInfo);
|
---|
536 | rc = SSMR3GetStructEx(pSSM, &patmInfo, sizeof(patmInfo), SSMSTRUCT_FLAGS_MEM_BAND_AID, &g_aPatmFields[0], NULL);
|
---|
537 | AssertRCReturn(rc, rc);
|
---|
538 |
|
---|
539 | /* Relative calls are made to the helper functions. Therefor their relative location must not change! */
|
---|
540 | /* Note: we reuse the saved global helpers and assume they are identical, which is kind of dangerous. */
|
---|
541 | if ( (pVM->patm.s.pfnHelperCallGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperCallGC - patmInfo.pPatchMemGC)
|
---|
542 | || (pVM->patm.s.pfnHelperRetGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperRetGC - patmInfo.pPatchMemGC)
|
---|
543 | || (pVM->patm.s.pfnHelperJumpGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperJumpGC - patmInfo.pPatchMemGC)
|
---|
544 | || (pVM->patm.s.pfnHelperIretGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperIretGC - patmInfo.pPatchMemGC))
|
---|
545 | {
|
---|
546 | AssertMsgFailed(("Helper function ptrs don't match!!!\n"));
|
---|
547 | return VERR_SSM_INVALID_STATE;
|
---|
548 | }
|
---|
549 |
|
---|
550 | if (pVM->patm.s.cbPatchMem != patmInfo.cbPatchMem)
|
---|
551 | {
|
---|
552 | AssertMsgFailed(("Patch memory ptrs and/or sizes don't match!!!\n"));
|
---|
553 | return VERR_SSM_INVALID_STATE;
|
---|
554 | }
|
---|
555 | pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
|
---|
556 | pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
|
---|
557 | pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
|
---|
558 | pVM->patm.s.fOutOfMemory = patmInfo.fOutOfMemory;
|
---|
559 |
|
---|
560 | /* Lowest and highest patched instruction */
|
---|
561 | pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
|
---|
562 | pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
|
---|
563 |
|
---|
564 | /* Sysenter handlers */
|
---|
565 | pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
|
---|
566 | pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
|
---|
567 | pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
|
---|
568 |
|
---|
569 | Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
|
---|
570 |
|
---|
571 | Log(("pPatchMemGC %RRv vs old %RRv\n", pVM->patm.s.pPatchMemGC, patmInfo.pPatchMemGC));
|
---|
572 | Log(("pGCStateGC %RRv vs old %RRv\n", pVM->patm.s.pGCStateGC, patmInfo.pGCStateGC));
|
---|
573 | Log(("pGCStackGC %RRv vs old %RRv\n", pVM->patm.s.pGCStackGC, patmInfo.pGCStackGC));
|
---|
574 | Log(("pCPUMCtxGC %RRv vs old %RRv\n", pVM->patm.s.pCPUMCtxGC, patmInfo.pCPUMCtxGC));
|
---|
575 |
|
---|
576 |
|
---|
577 | /** @note patch statistics are not restored. */
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * Restore patch memory contents
|
---|
581 | */
|
---|
582 | Log(("Restore patch memory: new %RRv old %RRv\n", pVM->patm.s.pPatchMemGC, patmInfo.pPatchMemGC));
|
---|
583 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
584 | AssertRCReturn(rc, rc);
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * Restore GC state memory
|
---|
588 | */
|
---|
589 | RT_BZERO(pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
590 | rc = SSMR3GetStructEx(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE), SSMSTRUCT_FLAGS_MEM_BAND_AID, &g_aPatmGCStateFields[0], NULL);
|
---|
591 | AssertRCReturn(rc, rc);
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Restore PATM stack page
|
---|
595 | */
|
---|
596 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
597 | AssertRCReturn(rc, rc);
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * Load all patches
|
---|
601 | */
|
---|
602 | for (unsigned i = 0; i < patmInfo.savedstate.cPatches; i++)
|
---|
603 | {
|
---|
604 | PATMPATCHREC patch, *pPatchRec;
|
---|
605 |
|
---|
606 | RT_ZERO(patch);
|
---|
607 | rc = SSMR3GetStructEx(pSSM, &patch, sizeof(patch), SSMSTRUCT_FLAGS_MEM_BAND_AID, &g_aPatmPatchRecFields[0], NULL);
|
---|
608 | AssertRCReturn(rc, rc);
|
---|
609 |
|
---|
610 | Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
611 |
|
---|
612 | rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
|
---|
613 | if (RT_FAILURE(rc))
|
---|
614 | {
|
---|
615 | AssertMsgFailed(("Out of memory!!!!\n"));
|
---|
616 | return VERR_NO_MEMORY;
|
---|
617 | }
|
---|
618 | /*
|
---|
619 | * Only restore the patch part of the tree record; not the internal data (except the key of course)
|
---|
620 | */
|
---|
621 | pPatchRec->patch = patch.patch;
|
---|
622 | pPatchRec->Core.Key = patch.Core.Key;
|
---|
623 | pPatchRec->CoreOffset.Key = patch.CoreOffset.Key;
|
---|
624 |
|
---|
625 | Log(("Restoring patch %RRv -> %RRv\n", pPatchRec->patch.pPrivInstrGC, patmInfo.pPatchMemGC + pPatchRec->patch.pPatchBlockOffset));
|
---|
626 | bool ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
|
---|
627 | Assert(ret);
|
---|
628 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
629 | {
|
---|
630 | if (pPatchRec->patch.pPatchBlockOffset)
|
---|
631 | {
|
---|
632 | /* We actually generated code for this patch. */
|
---|
633 | ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
|
---|
634 | AssertMsg(ret, ("Inserting patch %RRv offset %08RX32 failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
|
---|
635 | }
|
---|
636 | }
|
---|
637 | /* Set to zero as we don't need it anymore. */
|
---|
638 | pPatchRec->patch.pTempInfo = 0;
|
---|
639 |
|
---|
640 | PATMP2GLOOKUPREC cacheRec;
|
---|
641 | RT_ZERO(cacheRec);
|
---|
642 | cacheRec.pPatch = &pPatchRec->patch;
|
---|
643 |
|
---|
644 | uint8_t *pPrivInstrHC = PATMGCVirtToHCVirt(pVM, &cacheRec, pPatchRec->patch.pPrivInstrGC);
|
---|
645 | /* Can fail due to page or page table not present. */
|
---|
646 |
|
---|
647 | /*
|
---|
648 | * Restore fixup records and correct HC pointers in fixup records
|
---|
649 | */
|
---|
650 | pPatchRec->patch.FixupTree = 0;
|
---|
651 | pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
|
---|
652 | for (unsigned j = 0; j < patch.patch.nrFixups; j++)
|
---|
653 | {
|
---|
654 | RELOCREC rec;
|
---|
655 | int32_t offset;
|
---|
656 | RTRCPTR *pFixup;
|
---|
657 |
|
---|
658 | RT_ZERO(rec);
|
---|
659 | rc = SSMR3GetStructEx(pSSM, &rec, sizeof(rec), SSMSTRUCT_FLAGS_MEM_BAND_AID, &g_aPatmRelocRec[0], NULL);
|
---|
660 | AssertRCReturn(rc, rc);
|
---|
661 |
|
---|
662 | if (pPrivInstrHC)
|
---|
663 | {
|
---|
664 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
665 | offset = (int32_t)(intptr_t)rec.pRelocPos;
|
---|
666 | /* Convert to HC pointer again. */
|
---|
667 | PATM_ADD_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
668 | pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
669 |
|
---|
670 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
671 | {
|
---|
672 | if ( rec.uType == FIXUP_REL_JMPTOPATCH
|
---|
673 | && (pPatchRec->patch.flags & PATMFL_PATCHED_GUEST_CODE))
|
---|
674 | {
|
---|
675 | Assert(pPatchRec->patch.cbPatchJump == SIZEOF_NEARJUMP32 || pPatchRec->patch.cbPatchJump == SIZEOF_NEAR_COND_JUMP32);
|
---|
676 | unsigned offset2 = (pPatchRec->patch.cbPatchJump == SIZEOF_NEARJUMP32) ? 1 : 2;
|
---|
677 |
|
---|
678 | rec.pRelocPos = pPrivInstrHC + offset2;
|
---|
679 | pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
680 | }
|
---|
681 |
|
---|
682 | patmCorrectFixup(pVM, uVersion, patmInfo, &pPatchRec->patch, &rec, offset, pFixup);
|
---|
683 | }
|
---|
684 |
|
---|
685 | rc = patmPatchAddReloc32(pVM, &pPatchRec->patch, rec.pRelocPos, rec.uType, rec.pSource, rec.pDest);
|
---|
686 | AssertRCReturn(rc, rc);
|
---|
687 | }
|
---|
688 | }
|
---|
689 | /* Release previous lock if any. */
|
---|
690 | if (cacheRec.Lock.pvMap)
|
---|
691 | PGMPhysReleasePageMappingLock(pVM, &cacheRec.Lock);
|
---|
692 |
|
---|
693 | /* And all patch to guest lookup records */
|
---|
694 | Assert(pPatchRec->patch.nrPatch2GuestRecs || pPatchRec->patch.uState == PATCH_REFUSED || (pPatchRec->patch.flags & (PATMFL_SYSENTER_XP | PATMFL_IDTHANDLER | PATMFL_TRAPHANDLER | PATMFL_INT3_REPLACEMENT)));
|
---|
695 |
|
---|
696 | pPatchRec->patch.Patch2GuestAddrTree = 0;
|
---|
697 | pPatchRec->patch.Guest2PatchAddrTree = 0;
|
---|
698 | if (pPatchRec->patch.nrPatch2GuestRecs)
|
---|
699 | {
|
---|
700 | RECPATCHTOGUEST rec;
|
---|
701 | uint32_t nrPatch2GuestRecs = pPatchRec->patch.nrPatch2GuestRecs;
|
---|
702 |
|
---|
703 | pPatchRec->patch.nrPatch2GuestRecs = 0; /* incremented by patmr3AddP2GLookupRecord */
|
---|
704 | for (uint32_t j=0;j<nrPatch2GuestRecs;j++)
|
---|
705 | {
|
---|
706 | RT_ZERO(rec);
|
---|
707 | rc = SSMR3GetStructEx(pSSM, &rec, sizeof(rec), SSMSTRUCT_FLAGS_MEM_BAND_AID, &g_aPatmRecPatchToGuest[0], NULL);
|
---|
708 | AssertRCReturn(rc, rc);
|
---|
709 |
|
---|
710 | patmr3AddP2GLookupRecord(pVM, &pPatchRec->patch, (uintptr_t)rec.Core.Key + pVM->patm.s.pPatchMemHC, rec.pOrgInstrGC, rec.enmType, rec.fDirty);
|
---|
711 | }
|
---|
712 | Assert(pPatchRec->patch.Patch2GuestAddrTree);
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (pPatchRec->patch.flags & PATMFL_CODE_MONITORED)
|
---|
716 | {
|
---|
717 | /* Insert the guest page lookup records (for detection self-modifying code) */
|
---|
718 | rc = patmInsertPatchPages(pVM, &pPatchRec->patch);
|
---|
719 | AssertRCReturn(rc, rc);
|
---|
720 | }
|
---|
721 |
|
---|
722 | #if 0 /* can fail def LOG_ENABLED */
|
---|
723 | if ( pPatchRec->patch.uState != PATCH_REFUSED
|
---|
724 | && !(pPatchRec->patch.flags & PATMFL_INT3_REPLACEMENT))
|
---|
725 | {
|
---|
726 | pPatchRec->patch.pTempInfo = (PPATCHINFOTEMP)MMR3HeapAllocZ(pVM, MM_TAG_PATM_PATCH, sizeof(PATCHINFOTEMP));
|
---|
727 | Log(("Patch code ----------------------------------------------------------\n"));
|
---|
728 | patmr3DisasmCodeStream(pVM, PATCHCODE_PTR_GC(&pPatchRec->patch), PATCHCODE_PTR_GC(&pPatchRec->patch), patmr3DisasmCallback, &pPatchRec->patch);
|
---|
729 | Log(("Patch code ends -----------------------------------------------------\n"));
|
---|
730 | MMR3HeapFree(pPatchRec->patch.pTempInfo);
|
---|
731 | pPatchRec->patch.pTempInfo = NULL;
|
---|
732 | }
|
---|
733 | #endif
|
---|
734 | /* Remove the patch in case the gc mapping is not present. */
|
---|
735 | if ( !pPrivInstrHC
|
---|
736 | && pPatchRec->patch.uState == PATCH_ENABLED)
|
---|
737 | {
|
---|
738 | Log(("Remove patch %RGv due to failed HC address translation\n", pPatchRec->patch.pPrivInstrGC));
|
---|
739 | PATMR3RemovePatch(pVM, pPatchRec->patch.pPrivInstrGC);
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | /*
|
---|
744 | * Correct absolute fixups in the global patch. (helper functions)
|
---|
745 | * Bit of a mess. Uses the new patch record, but restored patch functions.
|
---|
746 | */
|
---|
747 | PRELOCREC pRec = 0;
|
---|
748 | AVLPVKEY key = 0;
|
---|
749 |
|
---|
750 | Log(("Correct fixups in global helper functions\n"));
|
---|
751 | while (true)
|
---|
752 | {
|
---|
753 | int32_t offset;
|
---|
754 | RTRCPTR *pFixup;
|
---|
755 |
|
---|
756 | /* Get the record that's closest from above */
|
---|
757 | pRec = (PRELOCREC)RTAvlPVGetBestFit(&pVM->patm.s.pGlobalPatchRec->patch.FixupTree, key, true);
|
---|
758 | if (pRec == 0)
|
---|
759 | break;
|
---|
760 |
|
---|
761 | key = (AVLPVKEY)(pRec->pRelocPos + 1); /* search for the next record during the next round. */
|
---|
762 |
|
---|
763 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
764 | offset = (int32_t)(pRec->pRelocPos - pVM->patm.s.pPatchMemHC);
|
---|
765 | pFixup = (RTRCPTR *)pRec->pRelocPos;
|
---|
766 |
|
---|
767 | /* Correct fixups that refer to PATM structures in the hypervisor region (their addresses might have changed). */
|
---|
768 | patmCorrectFixup(pVM, uVersion, patmInfo, &pVM->patm.s.pGlobalPatchRec->patch, pRec, offset, pFixup);
|
---|
769 | }
|
---|
770 |
|
---|
771 | #ifdef VBOX_WITH_STATISTICS
|
---|
772 | /*
|
---|
773 | * Restore relevant old statistics
|
---|
774 | */
|
---|
775 | pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
|
---|
776 | pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
|
---|
777 | pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
|
---|
778 | pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
|
---|
779 | #endif
|
---|
780 |
|
---|
781 | return VINF_SUCCESS;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * Correct fixups to predefined hypervisor PATM regions. (their addresses might have changed)
|
---|
786 | *
|
---|
787 | * @returns VBox status code.
|
---|
788 | * @param pVM VM Handle.
|
---|
789 | * @param ulSSMVersion SSM version
|
---|
790 | * @param patmInfo Saved PATM structure
|
---|
791 | * @param pPatch Patch record
|
---|
792 | * @param pRec Relocation record
|
---|
793 | * @param offset Offset of referenced data/code
|
---|
794 | * @param pFixup Fixup address
|
---|
795 | */
|
---|
796 | static void patmCorrectFixup(PVM pVM, unsigned ulSSMVersion, PATM &patmInfo, PPATCHINFO pPatch, PRELOCREC pRec, int32_t offset, RTRCPTR *pFixup)
|
---|
797 | {
|
---|
798 | int32_t delta = pVM->patm.s.pPatchMemGC - patmInfo.pPatchMemGC;
|
---|
799 |
|
---|
800 | switch (pRec->uType)
|
---|
801 | {
|
---|
802 | case FIXUP_ABSOLUTE:
|
---|
803 | {
|
---|
804 | if (pRec->pSource && !PATMIsPatchGCAddr(pVM, (RTRCUINTPTR)pRec->pSource))
|
---|
805 | break;
|
---|
806 |
|
---|
807 | if ( *pFixup >= patmInfo.pGCStateGC
|
---|
808 | && *pFixup < patmInfo.pGCStateGC + sizeof(PATMGCSTATE))
|
---|
809 | {
|
---|
810 | LogFlow(("Changing absolute GCState at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC));
|
---|
811 | *pFixup = (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC;
|
---|
812 | }
|
---|
813 | else
|
---|
814 | if ( *pFixup >= patmInfo.pCPUMCtxGC
|
---|
815 | && *pFixup < patmInfo.pCPUMCtxGC + sizeof(CPUMCTX))
|
---|
816 | {
|
---|
817 | LogFlow(("Changing absolute CPUMCTX at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC));
|
---|
818 |
|
---|
819 | /* The CPUMCTX structure has completely changed, so correct the offsets too. */
|
---|
820 | if (ulSSMVersion == PATM_SSM_VERSION_VER16)
|
---|
821 | {
|
---|
822 | unsigned uCPUMOffset = *pFixup - patmInfo.pCPUMCtxGC;
|
---|
823 |
|
---|
824 | /* ''case RT_OFFSETOF()'' does not work as gcc refuses to use & as a constant expression.
|
---|
825 | * Defining RT_OFFSETOF as __builtin_offsetof for gcc would make this possible. But this
|
---|
826 | * function is not available in older gcc versions, at least not in gcc-3.3 */
|
---|
827 | if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr0))
|
---|
828 | {
|
---|
829 | LogFlow(("Changing dr[0] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[0])));
|
---|
830 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[0]);
|
---|
831 | }
|
---|
832 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr1))
|
---|
833 | {
|
---|
834 | LogFlow(("Changing dr[1] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[1])));
|
---|
835 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[1]);
|
---|
836 | }
|
---|
837 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr2))
|
---|
838 | {
|
---|
839 | LogFlow(("Changing dr[2] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[2])));
|
---|
840 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[2]);
|
---|
841 | }
|
---|
842 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr3))
|
---|
843 | {
|
---|
844 | LogFlow(("Changing dr[3] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[3])));
|
---|
845 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[3]);
|
---|
846 | }
|
---|
847 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr4))
|
---|
848 | {
|
---|
849 | LogFlow(("Changing dr[4] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[4])));
|
---|
850 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[4]);
|
---|
851 | }
|
---|
852 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr5))
|
---|
853 | {
|
---|
854 | LogFlow(("Changing dr[5] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[5])));
|
---|
855 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[5]);
|
---|
856 | }
|
---|
857 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr6))
|
---|
858 | {
|
---|
859 | LogFlow(("Changing dr[6] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[6])));
|
---|
860 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[6]);
|
---|
861 | }
|
---|
862 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr7))
|
---|
863 | {
|
---|
864 | LogFlow(("Changing dr[7] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[7])));
|
---|
865 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[7]);
|
---|
866 | }
|
---|
867 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr0))
|
---|
868 | {
|
---|
869 | LogFlow(("Changing cr0 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr0)));
|
---|
870 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr0);
|
---|
871 | }
|
---|
872 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr2))
|
---|
873 | {
|
---|
874 | LogFlow(("Changing cr2 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr2)));
|
---|
875 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr2);
|
---|
876 | }
|
---|
877 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr3))
|
---|
878 | {
|
---|
879 | LogFlow(("Changing cr3 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr3)));
|
---|
880 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr3);
|
---|
881 | }
|
---|
882 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr4))
|
---|
883 | {
|
---|
884 | LogFlow(("Changing cr4 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr4)));
|
---|
885 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr4);
|
---|
886 | }
|
---|
887 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, tr))
|
---|
888 | {
|
---|
889 | LogFlow(("Changing tr offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, tr)));
|
---|
890 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, tr);
|
---|
891 | }
|
---|
892 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, ldtr))
|
---|
893 | {
|
---|
894 | LogFlow(("Changing ldtr offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, ldtr)));
|
---|
895 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, ldtr);
|
---|
896 | }
|
---|
897 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, gdtr.pGdt))
|
---|
898 | {
|
---|
899 | LogFlow(("Changing pGdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, gdtr.pGdt)));
|
---|
900 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, gdtr.pGdt);
|
---|
901 | }
|
---|
902 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, gdtr.cbGdt))
|
---|
903 | {
|
---|
904 | LogFlow(("Changing cbGdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, gdtr.cbGdt)));
|
---|
905 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, gdtr.cbGdt);
|
---|
906 | }
|
---|
907 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, idtr.pIdt))
|
---|
908 | {
|
---|
909 | LogFlow(("Changing pIdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, idtr.pIdt)));
|
---|
910 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, idtr.pIdt);
|
---|
911 | }
|
---|
912 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, idtr.cbIdt))
|
---|
913 | {
|
---|
914 | LogFlow(("Changing cbIdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, idtr.cbIdt)));
|
---|
915 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, idtr.cbIdt);
|
---|
916 | }
|
---|
917 | else
|
---|
918 | AssertMsgFailed(("Unexpected CPUMCTX offset %x\n", uCPUMOffset));
|
---|
919 | }
|
---|
920 | else
|
---|
921 | *pFixup = (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC;
|
---|
922 | }
|
---|
923 | else
|
---|
924 | if ( *pFixup >= patmInfo.pStatsGC
|
---|
925 | && *pFixup < patmInfo.pStatsGC + PATM_STAT_MEMSIZE)
|
---|
926 | {
|
---|
927 | LogFlow(("Changing absolute Stats at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC));
|
---|
928 | *pFixup = (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC;
|
---|
929 | }
|
---|
930 | else
|
---|
931 | if ( *pFixup >= patmInfo.pGCStackGC
|
---|
932 | && *pFixup < patmInfo.pGCStackGC + PATM_STACK_TOTAL_SIZE)
|
---|
933 | {
|
---|
934 | LogFlow(("Changing absolute Stack at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC));
|
---|
935 | *pFixup = (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC;
|
---|
936 | }
|
---|
937 | else
|
---|
938 | if ( *pFixup >= patmInfo.pPatchMemGC
|
---|
939 | && *pFixup < patmInfo.pPatchMemGC + patmInfo.cbPatchMem)
|
---|
940 | {
|
---|
941 | LogFlow(("Changing absolute PatchMem at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC));
|
---|
942 | *pFixup = (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC;
|
---|
943 | }
|
---|
944 | else
|
---|
945 | /* Boldly ASSUMES:
|
---|
946 | * 1. That pCPUMCtxGC is in the VM structure and that its location is
|
---|
947 | * at the first page of the same 4 MB chunk.
|
---|
948 | * 2. That the forced actions were in the first 32 bytes of the VM
|
---|
949 | * structure.
|
---|
950 | * 3. That the CPUM leafs are less than 8KB into the structure. */
|
---|
951 | if ( ulSSMVersion <= PATM_SSM_VERSION_FIXUP_HACK
|
---|
952 | && *pFixup - (patmInfo.pCPUMCtxGC & UINT32_C(0xffc00000)) < UINT32_C(32))
|
---|
953 | {
|
---|
954 | LogFlow(("Changing fLocalForcedActions fixup from %RRv to %RRv\n", *pFixup, pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions)));
|
---|
955 | *pFixup = pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions);
|
---|
956 | }
|
---|
957 | else
|
---|
958 | if ( ulSSMVersion <= PATM_SSM_VERSION_FIXUP_HACK
|
---|
959 | && *pFixup - (patmInfo.pCPUMCtxGC & UINT32_C(0xffc00000)) < UINT32_C(8192))
|
---|
960 | {
|
---|
961 | static int cCpuidFixup = 0;
|
---|
962 | #ifdef LOG_ENABLED
|
---|
963 | RTRCPTR oldFixup = *pFixup;
|
---|
964 | #endif
|
---|
965 | /* very dirty assumptions about the cpuid patch and cpuid ordering. */
|
---|
966 | switch(cCpuidFixup & 3)
|
---|
967 | {
|
---|
968 | case 0:
|
---|
969 | *pFixup = CPUMR3GetGuestCpuIdDefRCPtr(pVM);
|
---|
970 | break;
|
---|
971 | case 1:
|
---|
972 | *pFixup = CPUMR3GetGuestCpuIdStdRCPtr(pVM);
|
---|
973 | break;
|
---|
974 | case 2:
|
---|
975 | *pFixup = CPUMR3GetGuestCpuIdExtRCPtr(pVM);
|
---|
976 | break;
|
---|
977 | case 3:
|
---|
978 | *pFixup = CPUMR3GetGuestCpuIdCentaurRCPtr(pVM);
|
---|
979 | break;
|
---|
980 | }
|
---|
981 | LogFlow(("Changing cpuid fixup %d from %RRv to %RRv\n", cCpuidFixup, oldFixup, *pFixup));
|
---|
982 | cCpuidFixup++;
|
---|
983 | }
|
---|
984 | else
|
---|
985 | if (ulSSMVersion >= PATM_SSM_VERSION)
|
---|
986 | {
|
---|
987 | #ifdef LOG_ENABLED
|
---|
988 | RTRCPTR oldFixup = *pFixup;
|
---|
989 | #endif
|
---|
990 | /* Core.Key abused to store the type of fixup */
|
---|
991 | switch ((uintptr_t)pRec->Core.Key)
|
---|
992 | {
|
---|
993 | case PATM_FIXUP_CPU_FF_ACTION:
|
---|
994 | *pFixup = pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions);
|
---|
995 | LogFlow(("Changing cpu ff action fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
996 | break;
|
---|
997 | case PATM_FIXUP_CPUID_DEFAULT:
|
---|
998 | *pFixup = CPUMR3GetGuestCpuIdDefRCPtr(pVM);
|
---|
999 | LogFlow(("Changing cpuid def fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1000 | break;
|
---|
1001 | case PATM_FIXUP_CPUID_STANDARD:
|
---|
1002 | *pFixup = CPUMR3GetGuestCpuIdStdRCPtr(pVM);
|
---|
1003 | LogFlow(("Changing cpuid std fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1004 | break;
|
---|
1005 | case PATM_FIXUP_CPUID_EXTENDED:
|
---|
1006 | *pFixup = CPUMR3GetGuestCpuIdExtRCPtr(pVM);
|
---|
1007 | LogFlow(("Changing cpuid ext fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1008 | break;
|
---|
1009 | case PATM_FIXUP_CPUID_CENTAUR:
|
---|
1010 | *pFixup = CPUMR3GetGuestCpuIdCentaurRCPtr(pVM);
|
---|
1011 | LogFlow(("Changing cpuid centaur fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1012 | break;
|
---|
1013 | default:
|
---|
1014 | AssertMsgFailed(("Unexpected fixup value %x\n", *pFixup));
|
---|
1015 | break;
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | #ifdef RT_OS_WINDOWS
|
---|
1020 | AssertCompile(RT_OFFSETOF(VM, fGlobalForcedActions) < 32);
|
---|
1021 | #endif
|
---|
1022 | break;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | case FIXUP_REL_JMPTOPATCH:
|
---|
1026 | {
|
---|
1027 | RTRCPTR pTarget = (RTRCPTR)((RTRCINTPTR)pRec->pDest + delta);
|
---|
1028 |
|
---|
1029 | if ( pPatch->uState == PATCH_ENABLED
|
---|
1030 | && (pPatch->flags & PATMFL_PATCHED_GUEST_CODE))
|
---|
1031 | {
|
---|
1032 | uint8_t oldJump[SIZEOF_NEAR_COND_JUMP32];
|
---|
1033 | uint8_t temp[SIZEOF_NEAR_COND_JUMP32];
|
---|
1034 | RTRCPTR pJumpOffGC;
|
---|
1035 | RTRCINTPTR displ = (RTRCINTPTR)pTarget - (RTRCINTPTR)pRec->pSource;
|
---|
1036 | RTRCINTPTR displOld= (RTRCINTPTR)pRec->pDest - (RTRCINTPTR)pRec->pSource;
|
---|
1037 |
|
---|
1038 | Log(("Relative fixup (g2p) %08X -> %08X at %08X (source=%08x, target=%08x)\n", *(int32_t*)pRec->pRelocPos, displ, pRec->pRelocPos, pRec->pSource, pRec->pDest));
|
---|
1039 |
|
---|
1040 | Assert(pRec->pSource - pPatch->cbPatchJump == pPatch->pPrivInstrGC);
|
---|
1041 | #ifdef PATM_RESOLVE_CONFLICTS_WITH_JUMP_PATCHES
|
---|
1042 | if (pPatch->cbPatchJump == SIZEOF_NEAR_COND_JUMP32)
|
---|
1043 | {
|
---|
1044 | Assert(pPatch->flags & PATMFL_JUMP_CONFLICT);
|
---|
1045 |
|
---|
1046 | pJumpOffGC = pPatch->pPrivInstrGC + 2; //two byte opcode
|
---|
1047 | oldJump[0] = pPatch->aPrivInstr[0];
|
---|
1048 | oldJump[1] = pPatch->aPrivInstr[1];
|
---|
1049 | *(RTRCUINTPTR *)&oldJump[2] = displOld;
|
---|
1050 | }
|
---|
1051 | else
|
---|
1052 | #endif
|
---|
1053 | if (pPatch->cbPatchJump == SIZEOF_NEARJUMP32)
|
---|
1054 | {
|
---|
1055 | pJumpOffGC = pPatch->pPrivInstrGC + 1; //one byte opcode
|
---|
1056 | oldJump[0] = 0xE9;
|
---|
1057 | *(RTRCUINTPTR *)&oldJump[1] = displOld;
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | {
|
---|
1061 | AssertMsgFailed(("Invalid patch jump size %d\n", pPatch->cbPatchJump));
|
---|
1062 | break;
|
---|
1063 | }
|
---|
1064 | Assert(pPatch->cbPatchJump <= sizeof(temp));
|
---|
1065 |
|
---|
1066 | /*
|
---|
1067 | * Read old patch jump and compare it to the one we previously installed
|
---|
1068 | */
|
---|
1069 | int rc = PGMPhysSimpleReadGCPtr(VMMGetCpu0(pVM), temp, pPatch->pPrivInstrGC, pPatch->cbPatchJump);
|
---|
1070 | Assert(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1071 |
|
---|
1072 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1073 | {
|
---|
1074 | RTRCPTR pPage = pPatch->pPrivInstrGC & PAGE_BASE_GC_MASK;
|
---|
1075 |
|
---|
1076 | rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_ALL, pPage, pPage + (PAGE_SIZE - 1) /* inclusive! */, 0, patmVirtPageHandler, "PATMGCMonitorPage", 0, "PATMMonitorPatchJump");
|
---|
1077 | Assert(RT_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT);
|
---|
1078 | }
|
---|
1079 | else
|
---|
1080 | if (memcmp(temp, oldJump, pPatch->cbPatchJump))
|
---|
1081 | {
|
---|
1082 | Log(("PATM: Patch jump was overwritten -> disabling patch!!\n"));
|
---|
1083 | /*
|
---|
1084 | * Disable patch; this is not a good solution
|
---|
1085 | */
|
---|
1086 | /* @todo hopefully it was completely overwritten (if the read was successful)!!!! */
|
---|
1087 | pPatch->uState = PATCH_DISABLED;
|
---|
1088 | }
|
---|
1089 | else
|
---|
1090 | if (RT_SUCCESS(rc))
|
---|
1091 | {
|
---|
1092 | rc = PGMPhysSimpleDirtyWriteGCPtr(VMMGetCpu0(pVM), pJumpOffGC, &displ, sizeof(displ));
|
---|
1093 | AssertRC(rc);
|
---|
1094 | }
|
---|
1095 | else
|
---|
1096 | AssertMsgFailed(("Unexpected error %d from MMR3PhysReadGCVirt\n", rc));
|
---|
1097 | }
|
---|
1098 | else
|
---|
1099 | Log(("Skip the guest jump to patch code for this disabled patch %08X\n", pRec->pRelocPos));
|
---|
1100 |
|
---|
1101 | pRec->pDest = pTarget;
|
---|
1102 | break;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | case FIXUP_REL_JMPTOGUEST:
|
---|
1106 | {
|
---|
1107 | RTRCPTR pSource = (RTRCPTR)((RTRCINTPTR)pRec->pSource + delta);
|
---|
1108 | RTRCINTPTR displ = (RTRCINTPTR)pRec->pDest - (RTRCINTPTR)pSource;
|
---|
1109 |
|
---|
1110 | Assert(!(pPatch->flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
1111 | Log(("Relative fixup (p2g) %08X -> %08X at %08X (source=%08x, target=%08x)\n", *(int32_t*)pRec->pRelocPos, displ, pRec->pRelocPos, pRec->pSource, pRec->pDest));
|
---|
1112 | *(RTRCUINTPTR *)pRec->pRelocPos = displ;
|
---|
1113 | pRec->pSource = pSource;
|
---|
1114 | break;
|
---|
1115 |
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 | }
|
---|
1119 |
|
---|