1 | /* $Id: PATMSSM.cpp 49808 2013-12-06 10:09:30Z 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-2013 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/cpumctx-v1_6.h>
|
---|
27 | #include <VBox/vmm/mm.h>
|
---|
28 | #include <VBox/vmm/ssm.h>
|
---|
29 | #include <VBox/param.h>
|
---|
30 | #include <iprt/avl.h>
|
---|
31 | #include "PATMInternal.h"
|
---|
32 | #include "PATMPatch.h"
|
---|
33 | #include "PATMA.h"
|
---|
34 | #include <VBox/vmm/vm.h>
|
---|
35 | #include <VBox/vmm/csam.h>
|
---|
36 | #include "internal/pgm.h"
|
---|
37 | #include <VBox/dbg.h>
|
---|
38 | #include <VBox/err.h>
|
---|
39 | #include <VBox/log.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <VBox/dis.h>
|
---|
44 | #include <VBox/disopcode.h>
|
---|
45 | #include <VBox/version.h>
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Patch information - SSM version.
|
---|
49 | *
|
---|
50 | * the difference is the missing pTrampolinePatchesHead member
|
---|
51 | * to avoid changing the saved state version for now (will come later).
|
---|
52 | */
|
---|
53 | typedef struct PATCHINFOSSM
|
---|
54 | {
|
---|
55 | uint32_t uState;
|
---|
56 | uint32_t uOldState;
|
---|
57 | DISCPUMODE uOpMode;
|
---|
58 |
|
---|
59 | /* GC pointer of privileged instruction */
|
---|
60 | RCPTRTYPE(uint8_t *) pPrivInstrGC;
|
---|
61 | R3PTRTYPE(uint8_t *) unusedHC; /* todo Can't remove due to structure size dependencies in saved states. */
|
---|
62 | uint8_t aPrivInstr[MAX_INSTR_SIZE];
|
---|
63 | uint32_t cbPrivInstr;
|
---|
64 | uint32_t opcode; //opcode for priv instr (OP_*)
|
---|
65 | uint32_t cbPatchJump; //patch jump size
|
---|
66 |
|
---|
67 | /* Only valid for PATMFL_JUMP_CONFLICT patches */
|
---|
68 | RTRCPTR pPatchJumpDestGC;
|
---|
69 |
|
---|
70 | RTGCUINTPTR32 pPatchBlockOffset;
|
---|
71 | uint32_t cbPatchBlockSize;
|
---|
72 | uint32_t uCurPatchOffset;
|
---|
73 | #if HC_ARCH_BITS == 64
|
---|
74 | uint32_t Alignment0; /**< Align flags correctly. */
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | uint64_t flags;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Lowest and highest patched GC instruction address. To optimize searches.
|
---|
81 | */
|
---|
82 | RTRCPTR pInstrGCLowest;
|
---|
83 | RTRCPTR pInstrGCHighest;
|
---|
84 |
|
---|
85 | /* Tree of fixup records for the patch. */
|
---|
86 | R3PTRTYPE(PAVLPVNODECORE) FixupTree;
|
---|
87 | uint32_t nrFixups;
|
---|
88 |
|
---|
89 | /* Tree of jumps inside the generated patch code. */
|
---|
90 | uint32_t nrJumpRecs;
|
---|
91 | R3PTRTYPE(PAVLPVNODECORE) JumpTree;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Lookup trees for determining the corresponding guest address of an
|
---|
95 | * instruction in the patch block.
|
---|
96 | */
|
---|
97 | R3PTRTYPE(PAVLU32NODECORE) Patch2GuestAddrTree;
|
---|
98 | R3PTRTYPE(PAVLU32NODECORE) Guest2PatchAddrTree;
|
---|
99 | uint32_t nrPatch2GuestRecs;
|
---|
100 | #if HC_ARCH_BITS == 64
|
---|
101 | uint32_t Alignment1;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | /* Unused, but can't remove due to structure size dependencies in the saved state. */
|
---|
105 | PATMP2GLOOKUPREC_OBSOLETE unused;
|
---|
106 |
|
---|
107 | /* Temporary information during patch creation. Don't waste hypervisor memory for this. */
|
---|
108 | R3PTRTYPE(PPATCHINFOTEMP) pTempInfo;
|
---|
109 |
|
---|
110 | /* Count the number of writes to the corresponding guest code. */
|
---|
111 | uint32_t cCodeWrites;
|
---|
112 |
|
---|
113 | /* Count the number of invalid writes to pages monitored for the patch. */
|
---|
114 | //some statistics to determine if we should keep this patch activated
|
---|
115 | uint32_t cTraps;
|
---|
116 |
|
---|
117 | uint32_t cInvalidWrites;
|
---|
118 |
|
---|
119 | // Index into the uPatchRun and uPatchTrap arrays (0..MAX_PATCHES-1)
|
---|
120 | uint32_t uPatchIdx;
|
---|
121 |
|
---|
122 | /* First opcode byte, that's overwritten when a patch is marked dirty. */
|
---|
123 | uint8_t bDirtyOpcode;
|
---|
124 | uint8_t Alignment2[7]; /**< Align the structure size on a 8-byte boundary. */
|
---|
125 | } PATCHINFOSSM, *PPATCHINFOSSM;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Lookup record for patches - SSM version.
|
---|
129 | */
|
---|
130 | typedef struct PATMPATCHRECSSM
|
---|
131 | {
|
---|
132 | /** The key is a GC virtual address. */
|
---|
133 | AVLOU32NODECORE Core;
|
---|
134 | /** The key is a patch offset. */
|
---|
135 | AVLOU32NODECORE CoreOffset;
|
---|
136 |
|
---|
137 | PATCHINFOSSM patch;
|
---|
138 | } PATMPATCHRECSSM, *PPATMPATCHRECSSM;
|
---|
139 |
|
---|
140 |
|
---|
141 | /*******************************************************************************
|
---|
142 | * Internal Functions *
|
---|
143 | *******************************************************************************/
|
---|
144 | static void patmCorrectFixup(PVM pVM, unsigned ulSSMVersion, PATM &patmInfo, PPATCHINFO pPatch, PRELOCREC pRec, int32_t offset, RTRCPTR *pFixup);
|
---|
145 |
|
---|
146 | /*******************************************************************************
|
---|
147 | * Global Variables *
|
---|
148 | *******************************************************************************/
|
---|
149 | /**
|
---|
150 | * SSM descriptor table for the PATM structure.
|
---|
151 | */
|
---|
152 | static SSMFIELD const g_aPatmFields[] =
|
---|
153 | {
|
---|
154 | /** @todo there are a bunch more fields here which can be marked as ignored. */
|
---|
155 | SSMFIELD_ENTRY_IGNORE( PATM, offVM),
|
---|
156 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchMemGC),
|
---|
157 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pPatchMemHC),
|
---|
158 | SSMFIELD_ENTRY( PATM, cbPatchMem),
|
---|
159 | SSMFIELD_ENTRY( PATM, offPatchMem),
|
---|
160 | SSMFIELD_ENTRY( PATM, fOutOfMemory),
|
---|
161 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
162 | SSMFIELD_ENTRY( PATM, deltaReloc),
|
---|
163 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStateHC),
|
---|
164 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStateGC),
|
---|
165 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStackGC),
|
---|
166 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStackHC),
|
---|
167 | SSMFIELD_ENTRY_RCPTR( PATM, pCPUMCtxGC),
|
---|
168 | SSMFIELD_ENTRY_RCPTR( PATM, pStatsGC),
|
---|
169 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pStatsHC),
|
---|
170 | SSMFIELD_ENTRY( PATM, uCurrentPatchIdx),
|
---|
171 | SSMFIELD_ENTRY( PATM, ulCallDepth),
|
---|
172 | SSMFIELD_ENTRY( PATM, cPageRecords),
|
---|
173 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCLowest),
|
---|
174 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCHighest),
|
---|
175 | SSMFIELD_ENTRY_RCPTR( PATM, PatchLookupTreeGC),
|
---|
176 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, PatchLookupTreeHC),
|
---|
177 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperCallGC),
|
---|
178 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperRetGC),
|
---|
179 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperJumpGC),
|
---|
180 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperIretGC),
|
---|
181 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGlobalPatchRec),
|
---|
182 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterGC),
|
---|
183 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterPatchGC),
|
---|
184 | SSMFIELD_ENTRY( PATM, uSysEnterPatchIdx),
|
---|
185 | SSMFIELD_ENTRY_RCPTR( PATM, pvFaultMonitor),
|
---|
186 | SSMFIELD_ENTRY_GCPHYS( PATM, mmio.GCPhys),
|
---|
187 | SSMFIELD_ENTRY_RCPTR( PATM, mmio.pCachedData),
|
---|
188 | SSMFIELD_ENTRY_IGN_RCPTR( PATM, mmio.Alignment0),
|
---|
189 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, savedstate.pSSM),
|
---|
190 | SSMFIELD_ENTRY( PATM, savedstate.cPatches),
|
---|
191 | SSMFIELD_ENTRY_PAD_HC64( PATM, savedstate.Alignment0, sizeof(uint32_t)),
|
---|
192 | SSMFIELD_ENTRY_IGNORE( PATM, StatNrOpcodeRead),
|
---|
193 | SSMFIELD_ENTRY_IGNORE( PATM, StatDisabled),
|
---|
194 | SSMFIELD_ENTRY_IGNORE( PATM, StatUnusable),
|
---|
195 | SSMFIELD_ENTRY_IGNORE( PATM, StatEnabled),
|
---|
196 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalled),
|
---|
197 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledFunctionPatches),
|
---|
198 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledTrampoline),
|
---|
199 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledJump),
|
---|
200 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3Callable),
|
---|
201 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3BlockRun),
|
---|
202 | SSMFIELD_ENTRY_IGNORE( PATM, StatOverwritten),
|
---|
203 | SSMFIELD_ENTRY_IGNORE( PATM, StatFixedConflicts),
|
---|
204 | SSMFIELD_ENTRY_IGNORE( PATM, StatFlushed),
|
---|
205 | SSMFIELD_ENTRY_IGNORE( PATM, StatPageBoundaryCrossed),
|
---|
206 | SSMFIELD_ENTRY_IGNORE( PATM, StatMonitored),
|
---|
207 | SSMFIELD_ENTRY_IGNORE( PATM, StatHandleTrap),
|
---|
208 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBack),
|
---|
209 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBackFail),
|
---|
210 | SSMFIELD_ENTRY_IGNORE( PATM, StatPATMMemoryUsed),
|
---|
211 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQSuccess),
|
---|
212 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQFailed),
|
---|
213 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateUseExisting),
|
---|
214 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionFound),
|
---|
215 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionNotFound),
|
---|
216 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWrite),
|
---|
217 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteDetect),
|
---|
218 | SSMFIELD_ENTRY_IGNORE( PATM, StatDirty),
|
---|
219 | SSMFIELD_ENTRY_IGNORE( PATM, StatPushTrap),
|
---|
220 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpreted),
|
---|
221 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpretedFailed),
|
---|
222 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysEnter),
|
---|
223 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysExit),
|
---|
224 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIret),
|
---|
225 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIretFailed),
|
---|
226 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirty),
|
---|
227 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyGood),
|
---|
228 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyBad),
|
---|
229 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageInserted),
|
---|
230 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageRemoved),
|
---|
231 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshSuccess),
|
---|
232 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshFailed),
|
---|
233 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRet),
|
---|
234 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRetReused),
|
---|
235 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenJump),
|
---|
236 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenCall),
|
---|
237 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenPopf),
|
---|
238 | SSMFIELD_ENTRY_IGNORE( PATM, StatCheckPendingIRQ),
|
---|
239 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupReplace),
|
---|
240 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupInsert),
|
---|
241 | SSMFIELD_ENTRY_IGNORE( PATM, StatU32FunctionMaxSlotsUsed),
|
---|
242 | SSMFIELD_ENTRY_IGNORE( PATM, Alignment0),
|
---|
243 | SSMFIELD_ENTRY_TERM()
|
---|
244 | };
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * SSM descriptor table for the PATM structure starting with r86139.
|
---|
248 | */
|
---|
249 | static SSMFIELD const g_aPatmFields86139[] =
|
---|
250 | {
|
---|
251 | /** @todo there are a bunch more fields here which can be marked as ignored. */
|
---|
252 | SSMFIELD_ENTRY_IGNORE( PATM, offVM),
|
---|
253 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchMemGC),
|
---|
254 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pPatchMemHC),
|
---|
255 | SSMFIELD_ENTRY( PATM, cbPatchMem),
|
---|
256 | SSMFIELD_ENTRY( PATM, offPatchMem),
|
---|
257 | SSMFIELD_ENTRY( PATM, fOutOfMemory),
|
---|
258 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
259 | SSMFIELD_ENTRY( PATM, deltaReloc),
|
---|
260 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStateHC),
|
---|
261 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStateGC),
|
---|
262 | SSMFIELD_ENTRY_RCPTR( PATM, pGCStackGC),
|
---|
263 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGCStackHC),
|
---|
264 | SSMFIELD_ENTRY_RCPTR( PATM, pCPUMCtxGC),
|
---|
265 | SSMFIELD_ENTRY_RCPTR( PATM, pStatsGC),
|
---|
266 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pStatsHC),
|
---|
267 | SSMFIELD_ENTRY( PATM, uCurrentPatchIdx),
|
---|
268 | SSMFIELD_ENTRY( PATM, ulCallDepth),
|
---|
269 | SSMFIELD_ENTRY( PATM, cPageRecords),
|
---|
270 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCLowest),
|
---|
271 | SSMFIELD_ENTRY_RCPTR( PATM, pPatchedInstrGCHighest),
|
---|
272 | SSMFIELD_ENTRY_RCPTR( PATM, PatchLookupTreeGC),
|
---|
273 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, PatchLookupTreeHC),
|
---|
274 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperCallGC),
|
---|
275 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperRetGC),
|
---|
276 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperJumpGC),
|
---|
277 | SSMFIELD_ENTRY_RCPTR( PATM, pfnHelperIretGC),
|
---|
278 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, pGlobalPatchRec),
|
---|
279 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterGC),
|
---|
280 | SSMFIELD_ENTRY_RCPTR( PATM, pfnSysEnterPatchGC),
|
---|
281 | SSMFIELD_ENTRY( PATM, uSysEnterPatchIdx),
|
---|
282 | SSMFIELD_ENTRY_RCPTR( PATM, pvFaultMonitor),
|
---|
283 | SSMFIELD_ENTRY_GCPHYS( PATM, mmio.GCPhys),
|
---|
284 | SSMFIELD_ENTRY_RCPTR( PATM, mmio.pCachedData),
|
---|
285 | SSMFIELD_ENTRY_IGN_RCPTR( PATM, mmio.Alignment0),
|
---|
286 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, savedstate.pSSM),
|
---|
287 | SSMFIELD_ENTRY( PATM, savedstate.cPatches),
|
---|
288 | SSMFIELD_ENTRY_PAD_HC64( PATM, savedstate.Alignment0, sizeof(uint32_t)),
|
---|
289 | SSMFIELD_ENTRY_IGN_HCPTR( PATM, hDbgModPatchMem),
|
---|
290 | SSMFIELD_ENTRY_PAD_HC32( PATM, Alignment0, sizeof(uint32_t)),
|
---|
291 | SSMFIELD_ENTRY_IGNORE( PATM, StatNrOpcodeRead),
|
---|
292 | SSMFIELD_ENTRY_IGNORE( PATM, StatDisabled),
|
---|
293 | SSMFIELD_ENTRY_IGNORE( PATM, StatUnusable),
|
---|
294 | SSMFIELD_ENTRY_IGNORE( PATM, StatEnabled),
|
---|
295 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalled),
|
---|
296 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledFunctionPatches),
|
---|
297 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledTrampoline),
|
---|
298 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstalledJump),
|
---|
299 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3Callable),
|
---|
300 | SSMFIELD_ENTRY_IGNORE( PATM, StatInt3BlockRun),
|
---|
301 | SSMFIELD_ENTRY_IGNORE( PATM, StatOverwritten),
|
---|
302 | SSMFIELD_ENTRY_IGNORE( PATM, StatFixedConflicts),
|
---|
303 | SSMFIELD_ENTRY_IGNORE( PATM, StatFlushed),
|
---|
304 | SSMFIELD_ENTRY_IGNORE( PATM, StatPageBoundaryCrossed),
|
---|
305 | SSMFIELD_ENTRY_IGNORE( PATM, StatMonitored),
|
---|
306 | SSMFIELD_ENTRY_IGNORE( PATM, StatHandleTrap),
|
---|
307 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBack),
|
---|
308 | SSMFIELD_ENTRY_IGNORE( PATM, StatSwitchBackFail),
|
---|
309 | SSMFIELD_ENTRY_IGNORE( PATM, StatPATMMemoryUsed),
|
---|
310 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQSuccess),
|
---|
311 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateREQFailed),
|
---|
312 | SSMFIELD_ENTRY_IGNORE( PATM, StatDuplicateUseExisting),
|
---|
313 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionFound),
|
---|
314 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionNotFound),
|
---|
315 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWrite),
|
---|
316 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteDetect),
|
---|
317 | SSMFIELD_ENTRY_IGNORE( PATM, StatDirty),
|
---|
318 | SSMFIELD_ENTRY_IGNORE( PATM, StatPushTrap),
|
---|
319 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpreted),
|
---|
320 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchWriteInterpretedFailed),
|
---|
321 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysEnter),
|
---|
322 | SSMFIELD_ENTRY_IGNORE( PATM, StatSysExit),
|
---|
323 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIret),
|
---|
324 | SSMFIELD_ENTRY_IGNORE( PATM, StatEmulIretFailed),
|
---|
325 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirty),
|
---|
326 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyGood),
|
---|
327 | SSMFIELD_ENTRY_IGNORE( PATM, StatInstrDirtyBad),
|
---|
328 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageInserted),
|
---|
329 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchPageRemoved),
|
---|
330 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshSuccess),
|
---|
331 | SSMFIELD_ENTRY_IGNORE( PATM, StatPatchRefreshFailed),
|
---|
332 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRet),
|
---|
333 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenRetReused),
|
---|
334 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenJump),
|
---|
335 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenCall),
|
---|
336 | SSMFIELD_ENTRY_IGNORE( PATM, StatGenPopf),
|
---|
337 | SSMFIELD_ENTRY_IGNORE( PATM, StatCheckPendingIRQ),
|
---|
338 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupReplace),
|
---|
339 | SSMFIELD_ENTRY_IGNORE( PATM, StatFunctionLookupInsert),
|
---|
340 | SSMFIELD_ENTRY_IGNORE( PATM, StatU32FunctionMaxSlotsUsed),
|
---|
341 | SSMFIELD_ENTRY_IGNORE( PATM, Alignment0),
|
---|
342 | SSMFIELD_ENTRY_TERM()
|
---|
343 | };
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * SSM descriptor table for the PATMGCSTATE structure.
|
---|
347 | */
|
---|
348 | static SSMFIELD const g_aPatmGCStateFields[] =
|
---|
349 | {
|
---|
350 | SSMFIELD_ENTRY( PATMGCSTATE, uVMFlags),
|
---|
351 | SSMFIELD_ENTRY( PATMGCSTATE, uPendingAction),
|
---|
352 | SSMFIELD_ENTRY( PATMGCSTATE, uPatchCalls),
|
---|
353 | SSMFIELD_ENTRY( PATMGCSTATE, uScratch),
|
---|
354 | SSMFIELD_ENTRY( PATMGCSTATE, uIretEFlags),
|
---|
355 | SSMFIELD_ENTRY( PATMGCSTATE, uIretCS),
|
---|
356 | SSMFIELD_ENTRY( PATMGCSTATE, uIretEIP),
|
---|
357 | SSMFIELD_ENTRY( PATMGCSTATE, Psp),
|
---|
358 | SSMFIELD_ENTRY( PATMGCSTATE, fPIF),
|
---|
359 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCPtrInhibitInterrupts),
|
---|
360 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCCallPatchTargetAddr),
|
---|
361 | SSMFIELD_ENTRY_RCPTR( PATMGCSTATE, GCCallReturnAddr),
|
---|
362 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uEAX),
|
---|
363 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uECX),
|
---|
364 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uEDI),
|
---|
365 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.eFlags),
|
---|
366 | SSMFIELD_ENTRY( PATMGCSTATE, Restore.uFlags),
|
---|
367 | SSMFIELD_ENTRY_TERM()
|
---|
368 | };
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * SSM descriptor table for the PATMPATCHREC structure.
|
---|
372 | */
|
---|
373 | static SSMFIELD const g_aPatmPatchRecFields[] =
|
---|
374 | {
|
---|
375 | SSMFIELD_ENTRY( PATMPATCHRECSSM, Core.Key),
|
---|
376 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, Core.pLeft),
|
---|
377 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, Core.pRight),
|
---|
378 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, Core.uchHeight),
|
---|
379 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
380 | SSMFIELD_ENTRY( PATMPATCHRECSSM, CoreOffset.Key),
|
---|
381 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, CoreOffset.pLeft),
|
---|
382 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, CoreOffset.pRight),
|
---|
383 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, CoreOffset.uchHeight),
|
---|
384 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 3),
|
---|
385 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.uState),
|
---|
386 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.uOldState),
|
---|
387 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.uOpMode),
|
---|
388 | SSMFIELD_ENTRY_RCPTR( PATMPATCHRECSSM, patch.pPrivInstrGC),
|
---|
389 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.unusedHC),
|
---|
390 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.aPrivInstr),
|
---|
391 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cbPrivInstr),
|
---|
392 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.opcode),
|
---|
393 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cbPatchJump),
|
---|
394 | SSMFIELD_ENTRY_RCPTR( PATMPATCHRECSSM, patch.pPatchJumpDestGC),
|
---|
395 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.pPatchBlockOffset),
|
---|
396 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cbPatchBlockSize),
|
---|
397 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.uCurPatchOffset),
|
---|
398 | SSMFIELD_ENTRY_PAD_HC64( PATMPATCHRECSSM, patch.Alignment0, sizeof(uint32_t)),
|
---|
399 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.flags),
|
---|
400 | SSMFIELD_ENTRY_RCPTR( PATMPATCHRECSSM, patch.pInstrGCLowest),
|
---|
401 | SSMFIELD_ENTRY_RCPTR( PATMPATCHRECSSM, patch.pInstrGCHighest),
|
---|
402 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.FixupTree),
|
---|
403 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.nrFixups),
|
---|
404 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.nrJumpRecs), // should be zero?
|
---|
405 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.JumpTree),
|
---|
406 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.Patch2GuestAddrTree),
|
---|
407 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.Guest2PatchAddrTree),
|
---|
408 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.nrPatch2GuestRecs),
|
---|
409 | SSMFIELD_ENTRY_PAD_HC64( PATMPATCHRECSSM, patch.Alignment1, sizeof(uint32_t)),
|
---|
410 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.unused.pPatchLocStartHC), // saved as zero
|
---|
411 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.unused.pPatchLocEndHC), // ditto
|
---|
412 | SSMFIELD_ENTRY_IGN_RCPTR( PATMPATCHRECSSM, patch.unused.pGuestLoc), // ditto
|
---|
413 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, patch.unused.opsize), // ditto
|
---|
414 | SSMFIELD_ENTRY_IGN_HCPTR( PATMPATCHRECSSM, patch.pTempInfo),
|
---|
415 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cCodeWrites),
|
---|
416 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cTraps),
|
---|
417 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.cInvalidWrites),
|
---|
418 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.uPatchIdx),
|
---|
419 | SSMFIELD_ENTRY( PATMPATCHRECSSM, patch.bDirtyOpcode),
|
---|
420 | SSMFIELD_ENTRY_IGNORE( PATMPATCHRECSSM, patch.Alignment2),
|
---|
421 | SSMFIELD_ENTRY_TERM()
|
---|
422 | };
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * SSM descriptor table for the RELOCREC structure.
|
---|
426 | */
|
---|
427 | static SSMFIELD const g_aPatmRelocRec[] =
|
---|
428 | {
|
---|
429 | SSMFIELD_ENTRY_HCPTR_HACK_U32( RELOCREC, Core.Key), // Used to store the relocation type
|
---|
430 | SSMFIELD_ENTRY_IGN_HCPTR( RELOCREC, Core.pLeft),
|
---|
431 | SSMFIELD_ENTRY_IGN_HCPTR( RELOCREC, Core.pRight),
|
---|
432 | SSMFIELD_ENTRY_IGNORE( RELOCREC, Core.uchHeight),
|
---|
433 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 7),
|
---|
434 | SSMFIELD_ENTRY( RELOCREC, uType),
|
---|
435 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
436 | SSMFIELD_ENTRY_HCPTR_HACK_U32( RELOCREC, pRelocPos), // converted to a patch member offset.
|
---|
437 | SSMFIELD_ENTRY_RCPTR( RELOCREC, pSource),
|
---|
438 | SSMFIELD_ENTRY_RCPTR( RELOCREC, pDest),
|
---|
439 | SSMFIELD_ENTRY_TERM()
|
---|
440 | };
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * SSM descriptor table for the RECPATCHTOGUEST structure.
|
---|
444 | */
|
---|
445 | static SSMFIELD const g_aPatmRecPatchToGuest[] =
|
---|
446 | {
|
---|
447 | SSMFIELD_ENTRY( RECPATCHTOGUEST, Core.Key),
|
---|
448 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
449 | SSMFIELD_ENTRY_IGN_HCPTR( RECPATCHTOGUEST, Core.pLeft),
|
---|
450 | SSMFIELD_ENTRY_IGN_HCPTR( RECPATCHTOGUEST, Core.pRight),
|
---|
451 | SSMFIELD_ENTRY_IGNORE( RECPATCHTOGUEST, Core.uchHeight),
|
---|
452 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 7),
|
---|
453 | SSMFIELD_ENTRY_RCPTR( RECPATCHTOGUEST, pOrgInstrGC),
|
---|
454 | SSMFIELD_ENTRY( RECPATCHTOGUEST, enmType),
|
---|
455 | SSMFIELD_ENTRY( RECPATCHTOGUEST, fDirty),
|
---|
456 | SSMFIELD_ENTRY( RECPATCHTOGUEST, fJumpTarget),
|
---|
457 | SSMFIELD_ENTRY( RECPATCHTOGUEST, u8DirtyOpcode),
|
---|
458 | SSMFIELD_ENTRY_PAD_HC_AUTO( 1, 5),
|
---|
459 | SSMFIELD_ENTRY_TERM()
|
---|
460 | };
|
---|
461 |
|
---|
462 | #ifdef VBOX_STRICT
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Callback function for RTAvlPVDoWithAll
|
---|
466 | *
|
---|
467 | * Counts the number of patches in the tree
|
---|
468 | *
|
---|
469 | * @returns VBox status code.
|
---|
470 | * @param pNode Current node
|
---|
471 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
472 | */
|
---|
473 | static DECLCALLBACK(int) patmCountLeafPV(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
474 | {
|
---|
475 | NOREF(pNode);
|
---|
476 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
477 | return VINF_SUCCESS;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Callback function for RTAvlU32DoWithAll
|
---|
482 | *
|
---|
483 | * Counts the number of patches in the tree
|
---|
484 | *
|
---|
485 | * @returns VBox status code.
|
---|
486 | * @param pNode Current node
|
---|
487 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
488 | */
|
---|
489 | static DECLCALLBACK(int) patmCountLeaf(PAVLU32NODECORE pNode, void *pcPatches)
|
---|
490 | {
|
---|
491 | NOREF(pNode);
|
---|
492 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
493 | return VINF_SUCCESS;
|
---|
494 | }
|
---|
495 |
|
---|
496 | #endif /* VBOX_STRICT */
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Callback function for RTAvloU32DoWithAll
|
---|
500 | *
|
---|
501 | * Counts the number of patches in the tree
|
---|
502 | *
|
---|
503 | * @returns VBox status code.
|
---|
504 | * @param pNode Current node
|
---|
505 | * @param pcPatches Pointer to patch counter
|
---|
506 | */
|
---|
507 | static DECLCALLBACK(int) patmCountPatch(PAVLOU32NODECORE pNode, void *pcPatches)
|
---|
508 | {
|
---|
509 | NOREF(pNode);
|
---|
510 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
511 | return VINF_SUCCESS;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Callback function for RTAvlU32DoWithAll
|
---|
516 | *
|
---|
517 | * Saves all patch to guest lookup records.
|
---|
518 | *
|
---|
519 | * @returns VBox status code.
|
---|
520 | * @param pNode Current node
|
---|
521 | * @param pVM1 Pointer to the VM
|
---|
522 | */
|
---|
523 | static DECLCALLBACK(int) patmSaveP2GLookupRecords(PAVLU32NODECORE pNode, void *pVM1)
|
---|
524 | {
|
---|
525 | PVM pVM = (PVM)pVM1;
|
---|
526 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
527 | PRECPATCHTOGUEST pPatchToGuestRec = (PRECPATCHTOGUEST)pNode;
|
---|
528 |
|
---|
529 | /* Save the lookup record. */
|
---|
530 | int rc = SSMR3PutStructEx(pSSM, pPatchToGuestRec, sizeof(RECPATCHTOGUEST), 0 /*fFlags*/, &g_aPatmRecPatchToGuest[0], NULL);
|
---|
531 | AssertRCReturn(rc, rc);
|
---|
532 |
|
---|
533 | return VINF_SUCCESS;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Callback function for RTAvlPVDoWithAll
|
---|
538 | *
|
---|
539 | * Saves all patch to guest lookup records.
|
---|
540 | *
|
---|
541 | * @returns VBox status code.
|
---|
542 | * @param pNode Current node
|
---|
543 | * @param pVM1 Pointer to the VM
|
---|
544 | */
|
---|
545 | static DECLCALLBACK(int) patmSaveFixupRecords(PAVLPVNODECORE pNode, void *pVM1)
|
---|
546 | {
|
---|
547 | PVM pVM = (PVM)pVM1;
|
---|
548 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
549 | RELOCREC rec = *(PRELOCREC)pNode;
|
---|
550 | RTRCPTR *pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
551 |
|
---|
552 | /* Convert pointer to an offset into patch memory. May not be applicable
|
---|
553 | to all fixup types, thus the UINT32_MAX. */
|
---|
554 | Assert(rec.pRelocPos);
|
---|
555 | uintptr_t offRelocPos = (uintptr_t)rec.pRelocPos - (uintptr_t)pVM->patm.s.pPatchMemHC;
|
---|
556 | if (offRelocPos > pVM->patm.s.cbPatchMem)
|
---|
557 | offRelocPos = UINT32_MAX;
|
---|
558 | rec.pRelocPos = (uint8_t *)offRelocPos;
|
---|
559 |
|
---|
560 | /* Zero rec.Core.Key since it's unused and may trigger SSM check due to the hack below. */
|
---|
561 | rec.Core.Key = 0;
|
---|
562 |
|
---|
563 | if (rec.uType == FIXUP_ABSOLUTE)
|
---|
564 | {
|
---|
565 | /* Core.Key abused to store the fixup type. */
|
---|
566 | if (*pFixup == pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions))
|
---|
567 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPU_FF_ACTION;
|
---|
568 | else
|
---|
569 | if (*pFixup == CPUMR3GetGuestCpuIdDefRCPtr(pVM))
|
---|
570 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_DEFAULT;
|
---|
571 | else
|
---|
572 | if (*pFixup == CPUMR3GetGuestCpuIdStdRCPtr(pVM))
|
---|
573 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_STANDARD;
|
---|
574 | else
|
---|
575 | if (*pFixup == CPUMR3GetGuestCpuIdExtRCPtr(pVM))
|
---|
576 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_EXTENDED;
|
---|
577 | else
|
---|
578 | if (*pFixup == CPUMR3GetGuestCpuIdCentaurRCPtr(pVM))
|
---|
579 | rec.Core.Key = (AVLPVKEY)PATM_FIXUP_CPUID_CENTAUR;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /* Save the lookup record. */
|
---|
583 | int rc = SSMR3PutStructEx(pSSM, &rec, sizeof(rec), 0 /*fFlags*/, &g_aPatmRelocRec[0], NULL);
|
---|
584 | AssertRCReturn(rc, rc);
|
---|
585 |
|
---|
586 | return VINF_SUCCESS;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Converts a saved state patch record to the memory record.
|
---|
591 | *
|
---|
592 | * @returns nothing.
|
---|
593 | * @param pPatch The memory record.
|
---|
594 | * @param pPatchSSM The SSM version of the patch record.
|
---|
595 | */
|
---|
596 | static void patmR3PatchConvertSSM2Mem(PPATMPATCHREC pPatch, PPATMPATCHRECSSM pPatchSSM)
|
---|
597 | {
|
---|
598 | /*
|
---|
599 | * Only restore the patch part of the tree record; not the internal data (except the key of course)
|
---|
600 | */
|
---|
601 | pPatch->Core.Key = pPatchSSM->Core.Key;
|
---|
602 | pPatch->CoreOffset.Key = pPatchSSM->CoreOffset.Key;
|
---|
603 | pPatch->patch.uState = pPatchSSM->patch.uState;
|
---|
604 | pPatch->patch.uOldState = pPatchSSM->patch.uOldState;
|
---|
605 | pPatch->patch.uOpMode = pPatchSSM->patch.uOpMode;
|
---|
606 | pPatch->patch.pPrivInstrGC = pPatchSSM->patch.pPrivInstrGC;
|
---|
607 | pPatch->patch.unusedHC = pPatchSSM->patch.unusedHC;
|
---|
608 | memcpy(&pPatch->patch.aPrivInstr[0], &pPatchSSM->patch.aPrivInstr[0], MAX_INSTR_SIZE);
|
---|
609 | pPatch->patch.cbPrivInstr = pPatchSSM->patch.cbPrivInstr;
|
---|
610 | pPatch->patch.opcode = pPatchSSM->patch.opcode;
|
---|
611 | pPatch->patch.cbPatchJump = pPatchSSM->patch.cbPatchJump;
|
---|
612 | pPatch->patch.pPatchJumpDestGC = pPatchSSM->patch.pPatchJumpDestGC;
|
---|
613 | pPatch->patch.pPatchBlockOffset = pPatchSSM->patch.pPatchBlockOffset;
|
---|
614 | pPatch->patch.cbPatchBlockSize = pPatchSSM->patch.cbPatchBlockSize;
|
---|
615 | pPatch->patch.uCurPatchOffset = pPatchSSM->patch.uCurPatchOffset;
|
---|
616 | pPatch->patch.flags = pPatchSSM->patch.flags;
|
---|
617 | pPatch->patch.pInstrGCLowest = pPatchSSM->patch.pInstrGCLowest;
|
---|
618 | pPatch->patch.pInstrGCHighest = pPatchSSM->patch.pInstrGCHighest;
|
---|
619 | pPatch->patch.FixupTree = pPatchSSM->patch.FixupTree;
|
---|
620 | pPatch->patch.nrFixups = pPatchSSM->patch.nrFixups;
|
---|
621 | pPatch->patch.nrJumpRecs = pPatchSSM->patch.nrJumpRecs;
|
---|
622 | pPatch->patch.JumpTree = pPatchSSM->patch.JumpTree;
|
---|
623 | pPatch->patch.Patch2GuestAddrTree = pPatchSSM->patch.Patch2GuestAddrTree;
|
---|
624 | pPatch->patch.Guest2PatchAddrTree = pPatchSSM->patch.Guest2PatchAddrTree;
|
---|
625 | pPatch->patch.nrPatch2GuestRecs = pPatchSSM->patch.nrPatch2GuestRecs;
|
---|
626 | pPatch->patch.unused = pPatchSSM->patch.unused;
|
---|
627 | pPatch->patch.pTempInfo = pPatchSSM->patch.pTempInfo;
|
---|
628 | pPatch->patch.cCodeWrites = pPatchSSM->patch.cCodeWrites;
|
---|
629 | pPatch->patch.cTraps = pPatchSSM->patch.cTraps;
|
---|
630 | pPatch->patch.cInvalidWrites = pPatchSSM->patch.cInvalidWrites;
|
---|
631 | pPatch->patch.uPatchIdx = pPatchSSM->patch.uPatchIdx;
|
---|
632 | pPatch->patch.bDirtyOpcode = pPatchSSM->patch.bDirtyOpcode;
|
---|
633 | pPatch->patch.pTrampolinePatchesHead = NULL;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Converts a memory patch record to the saved state version.
|
---|
638 | *
|
---|
639 | * @returns nothing.
|
---|
640 | * @param pPatchSSM The saved state record.
|
---|
641 | * @param pPatch The memory version to save.
|
---|
642 | */
|
---|
643 | static void patmR3PatchConvertMem2SSM(PPATMPATCHRECSSM pPatchSSM, PPATMPATCHREC pPatch)
|
---|
644 | {
|
---|
645 | pPatchSSM->Core = pPatch->Core;
|
---|
646 | pPatchSSM->CoreOffset = pPatch->CoreOffset;
|
---|
647 | pPatchSSM->patch.uState = pPatch->patch.uState;
|
---|
648 | pPatchSSM->patch.uOldState = pPatch->patch.uOldState;
|
---|
649 | pPatchSSM->patch.uOpMode = pPatch->patch.uOpMode;
|
---|
650 | pPatchSSM->patch.pPrivInstrGC = pPatch->patch.pPrivInstrGC;
|
---|
651 | pPatchSSM->patch.unusedHC = pPatch->patch.unusedHC;
|
---|
652 | memcpy(&pPatchSSM->patch.aPrivInstr[0], &pPatch->patch.aPrivInstr[0], MAX_INSTR_SIZE);
|
---|
653 | pPatchSSM->patch.cbPrivInstr = pPatch->patch.cbPrivInstr;
|
---|
654 | pPatchSSM->patch.opcode = pPatch->patch.opcode;
|
---|
655 | pPatchSSM->patch.cbPatchJump = pPatch->patch.cbPatchJump;
|
---|
656 | pPatchSSM->patch.pPatchJumpDestGC = pPatch->patch.pPatchJumpDestGC;
|
---|
657 | pPatchSSM->patch.pPatchBlockOffset = pPatch->patch.pPatchBlockOffset;
|
---|
658 | pPatchSSM->patch.cbPatchBlockSize = pPatch->patch.cbPatchBlockSize;
|
---|
659 | pPatchSSM->patch.uCurPatchOffset = pPatch->patch.uCurPatchOffset;
|
---|
660 | pPatchSSM->patch.flags = pPatch->patch.flags;
|
---|
661 | pPatchSSM->patch.pInstrGCLowest = pPatch->patch.pInstrGCLowest;
|
---|
662 | pPatchSSM->patch.pInstrGCHighest = pPatch->patch.pInstrGCHighest;
|
---|
663 | pPatchSSM->patch.FixupTree = pPatch->patch.FixupTree;
|
---|
664 | pPatchSSM->patch.nrFixups = pPatch->patch.nrFixups;
|
---|
665 | pPatchSSM->patch.nrJumpRecs = pPatch->patch.nrJumpRecs;
|
---|
666 | pPatchSSM->patch.JumpTree = pPatch->patch.JumpTree;
|
---|
667 | pPatchSSM->patch.Patch2GuestAddrTree = pPatch->patch.Patch2GuestAddrTree;
|
---|
668 | pPatchSSM->patch.Guest2PatchAddrTree = pPatch->patch.Guest2PatchAddrTree;
|
---|
669 | pPatchSSM->patch.nrPatch2GuestRecs = pPatch->patch.nrPatch2GuestRecs;
|
---|
670 | pPatchSSM->patch.unused = pPatch->patch.unused;
|
---|
671 | pPatchSSM->patch.pTempInfo = pPatch->patch.pTempInfo;
|
---|
672 | pPatchSSM->patch.cCodeWrites = pPatch->patch.cCodeWrites;
|
---|
673 | pPatchSSM->patch.cTraps = pPatch->patch.cTraps;
|
---|
674 | pPatchSSM->patch.cInvalidWrites = pPatch->patch.cInvalidWrites;
|
---|
675 | pPatchSSM->patch.uPatchIdx = pPatch->patch.uPatchIdx;
|
---|
676 | pPatchSSM->patch.bDirtyOpcode = pPatch->patch.bDirtyOpcode;
|
---|
677 | }
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Callback function for RTAvloU32DoWithAll
|
---|
681 | *
|
---|
682 | * Saves the state of the patch that's being enumerated
|
---|
683 | *
|
---|
684 | * @returns VBox status code.
|
---|
685 | * @param pNode Current node
|
---|
686 | * @param pVM1 Pointer to the VM
|
---|
687 | */
|
---|
688 | static DECLCALLBACK(int) patmSavePatchState(PAVLOU32NODECORE pNode, void *pVM1)
|
---|
689 | {
|
---|
690 | PVM pVM = (PVM)pVM1;
|
---|
691 | PPATMPATCHREC pPatch = (PPATMPATCHREC)pNode;
|
---|
692 | PATMPATCHRECSSM patch;
|
---|
693 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
694 | int rc;
|
---|
695 |
|
---|
696 | Assert(!(pPatch->patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
697 |
|
---|
698 | patmR3PatchConvertMem2SSM(&patch, pPatch);
|
---|
699 | Log4(("patmSavePatchState: cbPatchJump=%u uCurPathOffset=%#x pInstrGCLowest/Higest=%#x/%#x nrFixups=%#x nrJumpRecs=%#x\n",
|
---|
700 | patch.patch.cbPatchJump, patch.patch.uCurPatchOffset, patch.patch.pInstrGCLowest, patch.patch.pInstrGCHighest,
|
---|
701 | patch.patch.nrFixups, patch.patch.nrJumpRecs));
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
705 | */
|
---|
706 | AssertMsg(patch.patch.uState == PATCH_REFUSED || (patch.patch.pPatchBlockOffset || (patch.patch.flags & (PATMFL_SYSENTER_XP|PATMFL_INT3_REPLACEMENT))),
|
---|
707 | ("State = %x pPatchBlockHC=%08x flags=%x\n", patch.patch.uState, PATCHCODE_PTR_HC(&patch.patch), patch.patch.flags));
|
---|
708 | Assert(pPatch->patch.JumpTree == 0);
|
---|
709 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->DisasmJumpTree == 0);
|
---|
710 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->IllegalInstrTree == 0);
|
---|
711 |
|
---|
712 | /* Save the patch record itself */
|
---|
713 | rc = SSMR3PutStructEx(pSSM, &patch, sizeof(patch), 0 /*fFlags*/, &g_aPatmPatchRecFields[0], NULL);
|
---|
714 | AssertRCReturn(rc, rc);
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * Reset HC pointers in fixup records and save them.
|
---|
718 | */
|
---|
719 | #ifdef VBOX_STRICT
|
---|
720 | uint32_t nrFixupRecs = 0;
|
---|
721 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmCountLeafPV, &nrFixupRecs);
|
---|
722 | AssertMsg(nrFixupRecs == pPatch->patch.nrFixups, ("Fixup inconsistency! counted %d vs %d\n", nrFixupRecs, pPatch->patch.nrFixups));
|
---|
723 | #endif
|
---|
724 | rc = RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmSaveFixupRecords, pVM);
|
---|
725 | AssertRCReturn(rc, rc);
|
---|
726 |
|
---|
727 | #ifdef VBOX_STRICT
|
---|
728 | uint32_t nrLookupRecords = 0;
|
---|
729 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmCountLeaf, &nrLookupRecords);
|
---|
730 | Assert(nrLookupRecords == pPatch->patch.nrPatch2GuestRecs);
|
---|
731 | #endif
|
---|
732 |
|
---|
733 | rc = RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmSaveP2GLookupRecords, pVM);
|
---|
734 | AssertRCReturn(rc, rc);
|
---|
735 |
|
---|
736 | return VINF_SUCCESS;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Execute state save operation.
|
---|
741 | *
|
---|
742 | * @returns VBox status code.
|
---|
743 | * @param pVM Pointer to the VM.
|
---|
744 | * @param pSSM SSM operation handle.
|
---|
745 | */
|
---|
746 | DECLCALLBACK(int) patmR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
747 | {
|
---|
748 | PATM patmInfo = pVM->patm.s;
|
---|
749 | int rc;
|
---|
750 |
|
---|
751 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
752 |
|
---|
753 | /*
|
---|
754 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
755 | */
|
---|
756 | patmInfo.pPatchMemHC = NULL;
|
---|
757 | patmInfo.pGCStateHC = 0;
|
---|
758 | patmInfo.pvFaultMonitor = 0;
|
---|
759 |
|
---|
760 | Assert(patmInfo.ulCallDepth == 0);
|
---|
761 |
|
---|
762 | /*
|
---|
763 | * Count the number of patches in the tree (feeling lazy)
|
---|
764 | */
|
---|
765 | patmInfo.savedstate.cPatches = 0;
|
---|
766 | RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmCountPatch, &patmInfo.savedstate.cPatches);
|
---|
767 |
|
---|
768 | /*
|
---|
769 | * Save PATM structure
|
---|
770 | */
|
---|
771 | rc = SSMR3PutStructEx(pSSM, &patmInfo, sizeof(patmInfo), 0 /*fFlags*/, &g_aPatmFields[0], NULL);
|
---|
772 | AssertRCReturn(rc, rc);
|
---|
773 |
|
---|
774 | /*
|
---|
775 | * Save patch memory contents
|
---|
776 | */
|
---|
777 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
778 | AssertRCReturn(rc, rc);
|
---|
779 |
|
---|
780 | /*
|
---|
781 | * Save GC state memory
|
---|
782 | */
|
---|
783 | rc = SSMR3PutStructEx(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE), 0 /*fFlags*/, &g_aPatmGCStateFields[0], NULL);
|
---|
784 | AssertRCReturn(rc, rc);
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * Save PATM stack page
|
---|
788 | */
|
---|
789 | SSMR3PutU32(pSSM, PATM_STACK_TOTAL_SIZE);
|
---|
790 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
791 | AssertRCReturn(rc, rc);
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Save all patches
|
---|
795 | */
|
---|
796 | rc = RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmSavePatchState, pVM);
|
---|
797 | AssertRCReturn(rc, rc);
|
---|
798 |
|
---|
799 | /** @note patch statistics are not saved. */
|
---|
800 |
|
---|
801 | return VINF_SUCCESS;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * @callback_method_impl{FNSSMINTLOADEXEC, Dummy load function for HM mode.}
|
---|
807 | */
|
---|
808 | DECLCALLBACK(int) patmR3LoadDummy(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
809 | {
|
---|
810 | return SSMR3SkipToEndOfUnit(pSSM);
|
---|
811 | }
|
---|
812 |
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Execute state load operation.
|
---|
816 | *
|
---|
817 | * @returns VBox status code.
|
---|
818 | * @param pVM Pointer to the VM.
|
---|
819 | * @param pSSM SSM operation handle.
|
---|
820 | * @param uVersion Data layout version.
|
---|
821 | * @param uPass The data pass.
|
---|
822 | */
|
---|
823 | DECLCALLBACK(int) patmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
824 | {
|
---|
825 | PATM patmInfo;
|
---|
826 | int rc;
|
---|
827 |
|
---|
828 | if ( uVersion != PATM_SSM_VERSION
|
---|
829 | && uVersion != PATM_SSM_VERSION_MEM
|
---|
830 | && uVersion != PATM_SSM_VERSION_FIXUP_HACK
|
---|
831 | && uVersion != PATM_SSM_VERSION_VER16
|
---|
832 | )
|
---|
833 | {
|
---|
834 | AssertMsgFailed(("patmR3Load: Invalid version uVersion=%d!\n", uVersion));
|
---|
835 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
836 | }
|
---|
837 | uint32_t const fStructRestoreFlags = uVersion <= PATM_SSM_VERSION_MEM ? SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED : 0;
|
---|
838 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
839 |
|
---|
840 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
841 |
|
---|
842 | /*
|
---|
843 | * Restore PATM structure
|
---|
844 | */
|
---|
845 | RT_ZERO(patmInfo);
|
---|
846 | if ( uVersion == PATM_SSM_VERSION_MEM
|
---|
847 | && SSMR3HandleRevision(pSSM) >= 86139
|
---|
848 | && SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(4, 2, 51))
|
---|
849 | rc = SSMR3GetStructEx(pSSM, &patmInfo, sizeof(patmInfo), SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED,
|
---|
850 | &g_aPatmFields86139[0], NULL);
|
---|
851 | else
|
---|
852 | rc = SSMR3GetStructEx(pSSM, &patmInfo, sizeof(patmInfo), fStructRestoreFlags, &g_aPatmFields[0], NULL);
|
---|
853 | AssertRCReturn(rc, rc);
|
---|
854 |
|
---|
855 | /* Relative calls are made to the helper functions. Therefor their relative location must not change! */
|
---|
856 | /* Note: we reuse the saved global helpers and assume they are identical, which is kind of dangerous. */
|
---|
857 | AssertLogRelReturn((pVM->patm.s.pfnHelperCallGC - pVM->patm.s.pPatchMemGC) == (patmInfo.pfnHelperCallGC - patmInfo.pPatchMemGC),
|
---|
858 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
859 | AssertLogRelReturn((pVM->patm.s.pfnHelperRetGC - pVM->patm.s.pPatchMemGC) == (patmInfo.pfnHelperRetGC - patmInfo.pPatchMemGC),
|
---|
860 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
861 | AssertLogRelReturn((pVM->patm.s.pfnHelperJumpGC - pVM->patm.s.pPatchMemGC) == (patmInfo.pfnHelperJumpGC - patmInfo.pPatchMemGC),
|
---|
862 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
863 | AssertLogRelReturn((pVM->patm.s.pfnHelperIretGC - pVM->patm.s.pPatchMemGC) == (patmInfo.pfnHelperIretGC - patmInfo.pPatchMemGC),
|
---|
864 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
865 | AssertLogRelReturn(pVM->patm.s.cbPatchMem == patmInfo.cbPatchMem, VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
866 |
|
---|
867 | pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
|
---|
868 | pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
|
---|
869 | pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
|
---|
870 | pVM->patm.s.fOutOfMemory = patmInfo.fOutOfMemory;
|
---|
871 |
|
---|
872 | /* Lowest and highest patched instruction */
|
---|
873 | pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
|
---|
874 | pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
|
---|
875 |
|
---|
876 | /* Sysenter handlers */
|
---|
877 | pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
|
---|
878 | pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
|
---|
879 | pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
|
---|
880 |
|
---|
881 | Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
|
---|
882 |
|
---|
883 | Log(("pPatchMemGC %RRv vs old %RRv\n", pVM->patm.s.pPatchMemGC, patmInfo.pPatchMemGC));
|
---|
884 | Log(("pGCStateGC %RRv vs old %RRv\n", pVM->patm.s.pGCStateGC, patmInfo.pGCStateGC));
|
---|
885 | Log(("pGCStackGC %RRv vs old %RRv\n", pVM->patm.s.pGCStackGC, patmInfo.pGCStackGC));
|
---|
886 | Log(("pCPUMCtxGC %RRv vs old %RRv\n", pVM->patm.s.pCPUMCtxGC, patmInfo.pCPUMCtxGC));
|
---|
887 |
|
---|
888 |
|
---|
889 | /** @note patch statistics are not restored. */
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * Restore patch memory contents
|
---|
893 | */
|
---|
894 | Log(("Restore patch memory: new %RRv old %RRv\n", pVM->patm.s.pPatchMemGC, patmInfo.pPatchMemGC));
|
---|
895 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
896 | AssertRCReturn(rc, rc);
|
---|
897 |
|
---|
898 | /*
|
---|
899 | * Restore GC state memory
|
---|
900 | */
|
---|
901 | RT_BZERO(pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
902 | rc = SSMR3GetStructEx(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE), fStructRestoreFlags, &g_aPatmGCStateFields[0], NULL);
|
---|
903 | AssertRCReturn(rc, rc);
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * Restore PATM stack page
|
---|
907 | */
|
---|
908 | uint32_t cbStack = PATM_STACK_TOTAL_SIZE;
|
---|
909 | if (uVersion > PATM_SSM_VERSION_MEM)
|
---|
910 | {
|
---|
911 | rc = SSMR3GetU32(pSSM, &cbStack);
|
---|
912 | AssertRCReturn(rc, rc);
|
---|
913 | }
|
---|
914 | AssertCompile(!(PATM_STACK_TOTAL_SIZE & 31));
|
---|
915 | AssertLogRelMsgReturn(cbStack > 0 && cbStack <= PATM_STACK_TOTAL_SIZE && !(cbStack & 31),
|
---|
916 | ("cbStack=%#x vs %#x", cbStack, PATM_STACK_TOTAL_SIZE),
|
---|
917 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
918 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, cbStack);
|
---|
919 | AssertRCReturn(rc, rc);
|
---|
920 | if (cbStack < PATM_STACK_TOTAL_SIZE)
|
---|
921 | memset((uint8_t *)pVM->patm.s.pGCStackHC + cbStack, 0, PATM_STACK_TOTAL_SIZE - cbStack);
|
---|
922 |
|
---|
923 | /*
|
---|
924 | * Load all patches
|
---|
925 | */
|
---|
926 | for (unsigned i = 0; i < patmInfo.savedstate.cPatches; i++)
|
---|
927 | {
|
---|
928 | PATMPATCHRECSSM patch;
|
---|
929 | PATMPATCHREC *pPatchRec;
|
---|
930 |
|
---|
931 | RT_ZERO(patch);
|
---|
932 | rc = SSMR3GetStructEx(pSSM, &patch, sizeof(patch), fStructRestoreFlags, &g_aPatmPatchRecFields[0], NULL);
|
---|
933 | AssertRCReturn(rc, rc);
|
---|
934 | Log4(("patmR3Load: cbPatchJump=%u uCurPathOffset=%#x pInstrGCLowest/Higest=%#x/%#x nrFixups=%#x nrJumpRecs=%#x\n",
|
---|
935 | patch.patch.cbPatchJump, patch.patch.uCurPatchOffset, patch.patch.pInstrGCLowest, patch.patch.pInstrGCHighest,
|
---|
936 | patch.patch.nrFixups, patch.patch.nrJumpRecs));
|
---|
937 |
|
---|
938 | Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
939 |
|
---|
940 | rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
|
---|
941 | if (RT_FAILURE(rc))
|
---|
942 | {
|
---|
943 | AssertMsgFailed(("Out of memory!!!!\n"));
|
---|
944 | return VERR_NO_MEMORY;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* Convert SSM version to memory. */
|
---|
948 | patmR3PatchConvertSSM2Mem(pPatchRec, &patch);
|
---|
949 |
|
---|
950 | Log(("Restoring patch %RRv -> %RRv state %x\n", pPatchRec->patch.pPrivInstrGC, patmInfo.pPatchMemGC + pPatchRec->patch.pPatchBlockOffset, pPatchRec->patch.uState));
|
---|
951 | bool ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
|
---|
952 | Assert(ret);
|
---|
953 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
954 | {
|
---|
955 | if (pPatchRec->patch.pPatchBlockOffset)
|
---|
956 | {
|
---|
957 | /* We actually generated code for this patch. */
|
---|
958 | ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
|
---|
959 | AssertMsg(ret, ("Inserting patch %RRv offset %08RX32 failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
|
---|
960 | }
|
---|
961 | }
|
---|
962 | /* Set to zero as we don't need it anymore. */
|
---|
963 | pPatchRec->patch.pTempInfo = 0;
|
---|
964 |
|
---|
965 | PATMP2GLOOKUPREC cacheRec;
|
---|
966 | RT_ZERO(cacheRec);
|
---|
967 | cacheRec.pPatch = &pPatchRec->patch;
|
---|
968 |
|
---|
969 | uint8_t *pPrivInstrHC = patmR3GCVirtToHCVirt(pVM, &cacheRec, pPatchRec->patch.pPrivInstrGC);
|
---|
970 | /* Can fail due to page or page table not present. */
|
---|
971 |
|
---|
972 | /*
|
---|
973 | * Restore fixup records and correct HC pointers in fixup records
|
---|
974 | */
|
---|
975 | pPatchRec->patch.FixupTree = 0;
|
---|
976 | pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
|
---|
977 | for (unsigned j = 0; j < patch.patch.nrFixups; j++)
|
---|
978 | {
|
---|
979 | RELOCREC rec;
|
---|
980 | int32_t offset;
|
---|
981 | RTRCPTR *pFixup;
|
---|
982 |
|
---|
983 | RT_ZERO(rec);
|
---|
984 | rc = SSMR3GetStructEx(pSSM, &rec, sizeof(rec), fStructRestoreFlags, &g_aPatmRelocRec[0], NULL);
|
---|
985 | AssertRCReturn(rc, rc);
|
---|
986 |
|
---|
987 | if (pPrivInstrHC)
|
---|
988 | {
|
---|
989 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
990 | offset = (int32_t)(intptr_t)rec.pRelocPos;
|
---|
991 | /* Convert to HC pointer again. */
|
---|
992 | if ((uintptr_t)rec.pRelocPos < pVM->patm.s.cbPatchMem)
|
---|
993 | rec.pRelocPos = pVM->patm.s.pPatchMemHC + (uintptr_t)rec.pRelocPos;
|
---|
994 | else
|
---|
995 | rec.pRelocPos = NULL;
|
---|
996 | pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
997 |
|
---|
998 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
999 | {
|
---|
1000 | if ( rec.uType == FIXUP_REL_JMPTOPATCH
|
---|
1001 | && (pPatchRec->patch.flags & PATMFL_PATCHED_GUEST_CODE))
|
---|
1002 | {
|
---|
1003 | Assert(pPatchRec->patch.cbPatchJump == SIZEOF_NEARJUMP32 || pPatchRec->patch.cbPatchJump == SIZEOF_NEAR_COND_JUMP32);
|
---|
1004 | unsigned offset2 = (pPatchRec->patch.cbPatchJump == SIZEOF_NEARJUMP32) ? 1 : 2;
|
---|
1005 |
|
---|
1006 | rec.pRelocPos = pPrivInstrHC + offset2;
|
---|
1007 | pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | patmCorrectFixup(pVM, uVersion, patmInfo, &pPatchRec->patch, &rec, offset, pFixup);
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | rc = patmPatchAddReloc32(pVM, &pPatchRec->patch, rec.pRelocPos, rec.uType, rec.pSource, rec.pDest);
|
---|
1014 | AssertRCReturn(rc, rc);
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 | /* Release previous lock if any. */
|
---|
1018 | if (cacheRec.Lock.pvMap)
|
---|
1019 | PGMPhysReleasePageMappingLock(pVM, &cacheRec.Lock);
|
---|
1020 |
|
---|
1021 | /* And all patch to guest lookup records */
|
---|
1022 | Assert(pPatchRec->patch.nrPatch2GuestRecs || pPatchRec->patch.uState == PATCH_REFUSED || (pPatchRec->patch.flags & (PATMFL_SYSENTER_XP | PATMFL_IDTHANDLER | PATMFL_TRAPHANDLER | PATMFL_INT3_REPLACEMENT)));
|
---|
1023 |
|
---|
1024 | pPatchRec->patch.Patch2GuestAddrTree = 0;
|
---|
1025 | pPatchRec->patch.Guest2PatchAddrTree = 0;
|
---|
1026 | if (pPatchRec->patch.nrPatch2GuestRecs)
|
---|
1027 | {
|
---|
1028 | RECPATCHTOGUEST rec;
|
---|
1029 | uint32_t nrPatch2GuestRecs = pPatchRec->patch.nrPatch2GuestRecs;
|
---|
1030 |
|
---|
1031 | pPatchRec->patch.nrPatch2GuestRecs = 0; /* incremented by patmr3AddP2GLookupRecord */
|
---|
1032 | for (uint32_t j=0;j<nrPatch2GuestRecs;j++)
|
---|
1033 | {
|
---|
1034 | RT_ZERO(rec);
|
---|
1035 | rc = SSMR3GetStructEx(pSSM, &rec, sizeof(rec), fStructRestoreFlags, &g_aPatmRecPatchToGuest[0], NULL);
|
---|
1036 | AssertRCReturn(rc, rc);
|
---|
1037 |
|
---|
1038 | patmR3AddP2GLookupRecord(pVM, &pPatchRec->patch, (uintptr_t)rec.Core.Key + pVM->patm.s.pPatchMemHC, rec.pOrgInstrGC, rec.enmType, rec.fDirty);
|
---|
1039 | }
|
---|
1040 | Assert(pPatchRec->patch.Patch2GuestAddrTree);
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | if (pPatchRec->patch.flags & PATMFL_CODE_MONITORED)
|
---|
1044 | {
|
---|
1045 | /* Insert the guest page lookup records (for detection self-modifying code) */
|
---|
1046 | rc = patmInsertPatchPages(pVM, &pPatchRec->patch);
|
---|
1047 | AssertRCReturn(rc, rc);
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | #if 0 /* can fail def LOG_ENABLED */
|
---|
1051 | if ( pPatchRec->patch.uState != PATCH_REFUSED
|
---|
1052 | && !(pPatchRec->patch.flags & PATMFL_INT3_REPLACEMENT))
|
---|
1053 | {
|
---|
1054 | pPatchRec->patch.pTempInfo = (PPATCHINFOTEMP)MMR3HeapAllocZ(pVM, MM_TAG_PATM_PATCH, sizeof(PATCHINFOTEMP));
|
---|
1055 | Log(("Patch code ----------------------------------------------------------\n"));
|
---|
1056 | patmr3DisasmCodeStream(pVM, PATCHCODE_PTR_GC(&pPatchRec->patch), PATCHCODE_PTR_GC(&pPatchRec->patch), patmr3DisasmCallback, &pPatchRec->patch);
|
---|
1057 | Log(("Patch code ends -----------------------------------------------------\n"));
|
---|
1058 | MMR3HeapFree(pPatchRec->patch.pTempInfo);
|
---|
1059 | pPatchRec->patch.pTempInfo = NULL;
|
---|
1060 | }
|
---|
1061 | #endif
|
---|
1062 | /* Remove the patch in case the gc mapping is not present. */
|
---|
1063 | if ( !pPrivInstrHC
|
---|
1064 | && pPatchRec->patch.uState == PATCH_ENABLED)
|
---|
1065 | {
|
---|
1066 | Log(("Remove patch %RGv due to failed HC address translation\n", pPatchRec->patch.pPrivInstrGC));
|
---|
1067 | PATMR3RemovePatch(pVM, pPatchRec->patch.pPrivInstrGC);
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /*
|
---|
1072 | * Correct absolute fixups in the global patch. (helper functions)
|
---|
1073 | * Bit of a mess. Uses the new patch record, but restored patch functions.
|
---|
1074 | */
|
---|
1075 | PRELOCREC pRec = 0;
|
---|
1076 | AVLPVKEY key = 0;
|
---|
1077 |
|
---|
1078 | Log(("Correct fixups in global helper functions\n"));
|
---|
1079 | while (true)
|
---|
1080 | {
|
---|
1081 | int32_t offset;
|
---|
1082 | RTRCPTR *pFixup;
|
---|
1083 |
|
---|
1084 | /* Get the record that's closest from above */
|
---|
1085 | pRec = (PRELOCREC)RTAvlPVGetBestFit(&pVM->patm.s.pGlobalPatchRec->patch.FixupTree, key, true);
|
---|
1086 | if (pRec == 0)
|
---|
1087 | break;
|
---|
1088 |
|
---|
1089 | key = (AVLPVKEY)(pRec->pRelocPos + 1); /* search for the next record during the next round. */
|
---|
1090 |
|
---|
1091 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
1092 | offset = (int32_t)(pRec->pRelocPos - pVM->patm.s.pPatchMemHC);
|
---|
1093 | pFixup = (RTRCPTR *)pRec->pRelocPos;
|
---|
1094 |
|
---|
1095 | /* Correct fixups that refer to PATM structures in the hypervisor region (their addresses might have changed). */
|
---|
1096 | patmCorrectFixup(pVM, uVersion, patmInfo, &pVM->patm.s.pGlobalPatchRec->patch, pRec, offset, pFixup);
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | #ifdef VBOX_WITH_STATISTICS
|
---|
1100 | /*
|
---|
1101 | * Restore relevant old statistics
|
---|
1102 | */
|
---|
1103 | pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
|
---|
1104 | pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
|
---|
1105 | pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
|
---|
1106 | pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
|
---|
1107 | #endif
|
---|
1108 |
|
---|
1109 | return VINF_SUCCESS;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /**
|
---|
1113 | * Correct fixups to predefined hypervisor PATM regions. (their addresses might have changed)
|
---|
1114 | *
|
---|
1115 | * @returns VBox status code.
|
---|
1116 | * @param pVM Pointer to the VM.
|
---|
1117 | * @param ulSSMVersion SSM version
|
---|
1118 | * @param patmInfo Saved PATM structure
|
---|
1119 | * @param pPatch Patch record
|
---|
1120 | * @param pRec Relocation record
|
---|
1121 | * @param offset Offset of referenced data/code
|
---|
1122 | * @param pFixup Fixup address
|
---|
1123 | */
|
---|
1124 | static void patmCorrectFixup(PVM pVM, unsigned ulSSMVersion, PATM &patmInfo, PPATCHINFO pPatch, PRELOCREC pRec, int32_t offset, RTRCPTR *pFixup)
|
---|
1125 | {
|
---|
1126 | int32_t delta = pVM->patm.s.pPatchMemGC - patmInfo.pPatchMemGC;
|
---|
1127 |
|
---|
1128 | switch (pRec->uType)
|
---|
1129 | {
|
---|
1130 | case FIXUP_ABSOLUTE:
|
---|
1131 | {
|
---|
1132 | if (pRec->pSource && !PATMIsPatchGCAddr(pVM, (RTRCUINTPTR)pRec->pSource))
|
---|
1133 | break;
|
---|
1134 |
|
---|
1135 | if ( *pFixup >= patmInfo.pGCStateGC
|
---|
1136 | && *pFixup < patmInfo.pGCStateGC + sizeof(PATMGCSTATE))
|
---|
1137 | {
|
---|
1138 | LogFlow(("Changing absolute GCState at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC));
|
---|
1139 | *pFixup = (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC;
|
---|
1140 | }
|
---|
1141 | else
|
---|
1142 | if ( *pFixup >= patmInfo.pCPUMCtxGC
|
---|
1143 | && *pFixup < patmInfo.pCPUMCtxGC + sizeof(CPUMCTX))
|
---|
1144 | {
|
---|
1145 | LogFlow(("Changing absolute CPUMCTX at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC));
|
---|
1146 |
|
---|
1147 | /* The CPUMCTX structure has completely changed, so correct the offsets too. */
|
---|
1148 | if (ulSSMVersion == PATM_SSM_VERSION_VER16)
|
---|
1149 | {
|
---|
1150 | unsigned uCPUMOffset = *pFixup - patmInfo.pCPUMCtxGC;
|
---|
1151 |
|
---|
1152 | /* ''case RT_OFFSETOF()'' does not work as gcc refuses to use & as a constant expression.
|
---|
1153 | * Defining RT_OFFSETOF as __builtin_offsetof for gcc would make this possible. But this
|
---|
1154 | * function is not available in older gcc versions, at least not in gcc-3.3 */
|
---|
1155 | if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr0))
|
---|
1156 | {
|
---|
1157 | LogFlow(("Changing dr[0] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[0])));
|
---|
1158 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[0]);
|
---|
1159 | }
|
---|
1160 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr1))
|
---|
1161 | {
|
---|
1162 | LogFlow(("Changing dr[1] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[1])));
|
---|
1163 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[1]);
|
---|
1164 | }
|
---|
1165 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr2))
|
---|
1166 | {
|
---|
1167 | LogFlow(("Changing dr[2] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[2])));
|
---|
1168 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[2]);
|
---|
1169 | }
|
---|
1170 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr3))
|
---|
1171 | {
|
---|
1172 | LogFlow(("Changing dr[3] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[3])));
|
---|
1173 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[3]);
|
---|
1174 | }
|
---|
1175 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr4))
|
---|
1176 | {
|
---|
1177 | LogFlow(("Changing dr[4] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[4])));
|
---|
1178 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[4]);
|
---|
1179 | }
|
---|
1180 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr5))
|
---|
1181 | {
|
---|
1182 | LogFlow(("Changing dr[5] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[5])));
|
---|
1183 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[5]);
|
---|
1184 | }
|
---|
1185 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr6))
|
---|
1186 | {
|
---|
1187 | LogFlow(("Changing dr[6] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[6])));
|
---|
1188 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[6]);
|
---|
1189 | }
|
---|
1190 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, dr7))
|
---|
1191 | {
|
---|
1192 | LogFlow(("Changing dr[7] offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, dr[7])));
|
---|
1193 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, dr[7]);
|
---|
1194 | }
|
---|
1195 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr0))
|
---|
1196 | {
|
---|
1197 | LogFlow(("Changing cr0 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr0)));
|
---|
1198 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr0);
|
---|
1199 | }
|
---|
1200 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr2))
|
---|
1201 | {
|
---|
1202 | LogFlow(("Changing cr2 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr2)));
|
---|
1203 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr2);
|
---|
1204 | }
|
---|
1205 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr3))
|
---|
1206 | {
|
---|
1207 | LogFlow(("Changing cr3 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr3)));
|
---|
1208 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr3);
|
---|
1209 | }
|
---|
1210 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, cr4))
|
---|
1211 | {
|
---|
1212 | LogFlow(("Changing cr4 offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, cr4)));
|
---|
1213 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, cr4);
|
---|
1214 | }
|
---|
1215 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, tr))
|
---|
1216 | {
|
---|
1217 | LogFlow(("Changing tr offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, tr)));
|
---|
1218 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, tr);
|
---|
1219 | }
|
---|
1220 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, ldtr))
|
---|
1221 | {
|
---|
1222 | LogFlow(("Changing ldtr offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, ldtr)));
|
---|
1223 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, ldtr);
|
---|
1224 | }
|
---|
1225 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, gdtr.pGdt))
|
---|
1226 | {
|
---|
1227 | LogFlow(("Changing pGdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, gdtr.pGdt)));
|
---|
1228 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, gdtr.pGdt);
|
---|
1229 | }
|
---|
1230 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, gdtr.cbGdt))
|
---|
1231 | {
|
---|
1232 | LogFlow(("Changing cbGdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, gdtr.cbGdt)));
|
---|
1233 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, gdtr.cbGdt);
|
---|
1234 | }
|
---|
1235 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, idtr.pIdt))
|
---|
1236 | {
|
---|
1237 | LogFlow(("Changing pIdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, idtr.pIdt)));
|
---|
1238 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, idtr.pIdt);
|
---|
1239 | }
|
---|
1240 | else if (uCPUMOffset == (unsigned)RT_OFFSETOF(CPUMCTX_VER1_6, idtr.cbIdt))
|
---|
1241 | {
|
---|
1242 | LogFlow(("Changing cbIdt offset from %x to %x\n", uCPUMOffset, RT_OFFSETOF(CPUMCTX, idtr.cbIdt)));
|
---|
1243 | *pFixup = pVM->patm.s.pCPUMCtxGC + RT_OFFSETOF(CPUMCTX, idtr.cbIdt);
|
---|
1244 | }
|
---|
1245 | else
|
---|
1246 | AssertMsgFailed(("Unexpected CPUMCTX offset %x\n", uCPUMOffset));
|
---|
1247 | }
|
---|
1248 | else
|
---|
1249 | *pFixup = (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC;
|
---|
1250 | }
|
---|
1251 | else
|
---|
1252 | if ( *pFixup >= patmInfo.pStatsGC
|
---|
1253 | && *pFixup < patmInfo.pStatsGC + PATM_STAT_MEMSIZE)
|
---|
1254 | {
|
---|
1255 | LogFlow(("Changing absolute Stats at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC));
|
---|
1256 | *pFixup = (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC;
|
---|
1257 | }
|
---|
1258 | else
|
---|
1259 | if ( *pFixup >= patmInfo.pGCStackGC
|
---|
1260 | && *pFixup < patmInfo.pGCStackGC + PATM_STACK_TOTAL_SIZE)
|
---|
1261 | {
|
---|
1262 | LogFlow(("Changing absolute Stack at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC));
|
---|
1263 | *pFixup = (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC;
|
---|
1264 | }
|
---|
1265 | else
|
---|
1266 | if ( *pFixup >= patmInfo.pPatchMemGC
|
---|
1267 | && *pFixup < patmInfo.pPatchMemGC + patmInfo.cbPatchMem)
|
---|
1268 | {
|
---|
1269 | LogFlow(("Changing absolute PatchMem at %RRv from %RRv to %RRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC));
|
---|
1270 | *pFixup = (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC;
|
---|
1271 | }
|
---|
1272 | else
|
---|
1273 | /* Boldly ASSUMES:
|
---|
1274 | * 1. That pCPUMCtxGC is in the VM structure and that its location is
|
---|
1275 | * at the first page of the same 4 MB chunk.
|
---|
1276 | * 2. That the forced actions were in the first 32 bytes of the VM
|
---|
1277 | * structure.
|
---|
1278 | * 3. That the CPUM leafs are less than 8KB into the structure. */
|
---|
1279 | if ( ulSSMVersion <= PATM_SSM_VERSION_FIXUP_HACK
|
---|
1280 | && *pFixup - (patmInfo.pCPUMCtxGC & UINT32_C(0xffc00000)) < UINT32_C(32))
|
---|
1281 | {
|
---|
1282 | LogFlow(("Changing fLocalForcedActions fixup from %RRv to %RRv\n", *pFixup, pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions)));
|
---|
1283 | *pFixup = pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions);
|
---|
1284 | }
|
---|
1285 | else
|
---|
1286 | if ( ulSSMVersion <= PATM_SSM_VERSION_FIXUP_HACK
|
---|
1287 | && *pFixup - (patmInfo.pCPUMCtxGC & UINT32_C(0xffc00000)) < UINT32_C(8192))
|
---|
1288 | {
|
---|
1289 | static int cCpuidFixup = 0;
|
---|
1290 | #ifdef LOG_ENABLED
|
---|
1291 | RTRCPTR oldFixup = *pFixup;
|
---|
1292 | #endif
|
---|
1293 | /* very dirty assumptions about the cpuid patch and cpuid ordering. */
|
---|
1294 | switch(cCpuidFixup & 3)
|
---|
1295 | {
|
---|
1296 | case 0:
|
---|
1297 | *pFixup = CPUMR3GetGuestCpuIdDefRCPtr(pVM);
|
---|
1298 | break;
|
---|
1299 | case 1:
|
---|
1300 | *pFixup = CPUMR3GetGuestCpuIdStdRCPtr(pVM);
|
---|
1301 | break;
|
---|
1302 | case 2:
|
---|
1303 | *pFixup = CPUMR3GetGuestCpuIdExtRCPtr(pVM);
|
---|
1304 | break;
|
---|
1305 | case 3:
|
---|
1306 | *pFixup = CPUMR3GetGuestCpuIdCentaurRCPtr(pVM);
|
---|
1307 | break;
|
---|
1308 | }
|
---|
1309 | LogFlow(("Changing cpuid fixup %d from %RRv to %RRv\n", cCpuidFixup, oldFixup, *pFixup));
|
---|
1310 | cCpuidFixup++;
|
---|
1311 | }
|
---|
1312 | else
|
---|
1313 | if (ulSSMVersion >= PATM_SSM_VERSION_MEM)
|
---|
1314 | {
|
---|
1315 | #ifdef LOG_ENABLED
|
---|
1316 | RTRCPTR oldFixup = *pFixup;
|
---|
1317 | #endif
|
---|
1318 | /* Core.Key abused to store the type of fixup */
|
---|
1319 | switch ((uintptr_t)pRec->Core.Key)
|
---|
1320 | {
|
---|
1321 | case PATM_FIXUP_CPU_FF_ACTION:
|
---|
1322 | *pFixup = pVM->pVMRC + RT_OFFSETOF(VM, aCpus[0].fLocalForcedActions);
|
---|
1323 | LogFlow(("Changing cpu ff action fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1324 | break;
|
---|
1325 | case PATM_FIXUP_CPUID_DEFAULT:
|
---|
1326 | *pFixup = CPUMR3GetGuestCpuIdDefRCPtr(pVM);
|
---|
1327 | LogFlow(("Changing cpuid def fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1328 | break;
|
---|
1329 | case PATM_FIXUP_CPUID_STANDARD:
|
---|
1330 | *pFixup = CPUMR3GetGuestCpuIdStdRCPtr(pVM);
|
---|
1331 | LogFlow(("Changing cpuid std fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1332 | break;
|
---|
1333 | case PATM_FIXUP_CPUID_EXTENDED:
|
---|
1334 | *pFixup = CPUMR3GetGuestCpuIdExtRCPtr(pVM);
|
---|
1335 | LogFlow(("Changing cpuid ext fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1336 | break;
|
---|
1337 | case PATM_FIXUP_CPUID_CENTAUR:
|
---|
1338 | *pFixup = CPUMR3GetGuestCpuIdCentaurRCPtr(pVM);
|
---|
1339 | LogFlow(("Changing cpuid centaur fixup from %x to %x\n", oldFixup, *pFixup));
|
---|
1340 | break;
|
---|
1341 | default:
|
---|
1342 | AssertMsgFailed(("Unexpected fixup value %x\n", *pFixup));
|
---|
1343 | break;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | #ifdef RT_OS_WINDOWS
|
---|
1348 | AssertCompile(RT_OFFSETOF(VM, fGlobalForcedActions) < 32);
|
---|
1349 | #endif
|
---|
1350 | break;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | case FIXUP_REL_JMPTOPATCH:
|
---|
1354 | {
|
---|
1355 | RTRCPTR pTarget = (RTRCPTR)((RTRCINTPTR)pRec->pDest + delta);
|
---|
1356 |
|
---|
1357 | if ( pPatch->uState == PATCH_ENABLED
|
---|
1358 | && (pPatch->flags & PATMFL_PATCHED_GUEST_CODE))
|
---|
1359 | {
|
---|
1360 | uint8_t oldJump[SIZEOF_NEAR_COND_JUMP32];
|
---|
1361 | uint8_t temp[SIZEOF_NEAR_COND_JUMP32];
|
---|
1362 | RTRCPTR pJumpOffGC;
|
---|
1363 | RTRCINTPTR displ = (RTRCINTPTR)pTarget - (RTRCINTPTR)pRec->pSource;
|
---|
1364 | RTRCINTPTR displOld= (RTRCINTPTR)pRec->pDest - (RTRCINTPTR)pRec->pSource;
|
---|
1365 |
|
---|
1366 | Log(("Relative fixup (g2p) %08X -> %08X at %08X (source=%08x, target=%08x)\n", *(int32_t*)pRec->pRelocPos, displ, pRec->pRelocPos, pRec->pSource, pRec->pDest));
|
---|
1367 |
|
---|
1368 | Assert(pRec->pSource - pPatch->cbPatchJump == pPatch->pPrivInstrGC);
|
---|
1369 | #ifdef PATM_RESOLVE_CONFLICTS_WITH_JUMP_PATCHES
|
---|
1370 | if (pPatch->cbPatchJump == SIZEOF_NEAR_COND_JUMP32)
|
---|
1371 | {
|
---|
1372 | Assert(pPatch->flags & PATMFL_JUMP_CONFLICT);
|
---|
1373 |
|
---|
1374 | pJumpOffGC = pPatch->pPrivInstrGC + 2; //two byte opcode
|
---|
1375 | oldJump[0] = pPatch->aPrivInstr[0];
|
---|
1376 | oldJump[1] = pPatch->aPrivInstr[1];
|
---|
1377 | *(RTRCUINTPTR *)&oldJump[2] = displOld;
|
---|
1378 | }
|
---|
1379 | else
|
---|
1380 | #endif
|
---|
1381 | if (pPatch->cbPatchJump == SIZEOF_NEARJUMP32)
|
---|
1382 | {
|
---|
1383 | pJumpOffGC = pPatch->pPrivInstrGC + 1; //one byte opcode
|
---|
1384 | oldJump[0] = 0xE9;
|
---|
1385 | *(RTRCUINTPTR *)&oldJump[1] = displOld;
|
---|
1386 | }
|
---|
1387 | else
|
---|
1388 | {
|
---|
1389 | AssertMsgFailed(("Invalid patch jump size %d\n", pPatch->cbPatchJump));
|
---|
1390 | break;
|
---|
1391 | }
|
---|
1392 | Assert(pPatch->cbPatchJump <= sizeof(temp));
|
---|
1393 |
|
---|
1394 | /*
|
---|
1395 | * Read old patch jump and compare it to the one we previously installed
|
---|
1396 | */
|
---|
1397 | int rc = PGMPhysSimpleReadGCPtr(VMMGetCpu0(pVM), temp, pPatch->pPrivInstrGC, pPatch->cbPatchJump);
|
---|
1398 | Assert(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1399 |
|
---|
1400 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1401 | {
|
---|
1402 | RTRCPTR pPage = pPatch->pPrivInstrGC & PAGE_BASE_GC_MASK;
|
---|
1403 |
|
---|
1404 | rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_ALL, pPage, pPage + (PAGE_SIZE - 1) /* inclusive! */, 0, patmVirtPageHandler, "PATMGCMonitorPage", 0, "PATMMonitorPatchJump");
|
---|
1405 | Assert(RT_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT);
|
---|
1406 | }
|
---|
1407 | else
|
---|
1408 | if (memcmp(temp, oldJump, pPatch->cbPatchJump))
|
---|
1409 | {
|
---|
1410 | Log(("PATM: Patch jump was overwritten -> disabling patch!!\n"));
|
---|
1411 | /*
|
---|
1412 | * Disable patch; this is not a good solution
|
---|
1413 | */
|
---|
1414 | /* @todo hopefully it was completely overwritten (if the read was successful)!!!! */
|
---|
1415 | pPatch->uState = PATCH_DISABLED;
|
---|
1416 | }
|
---|
1417 | else
|
---|
1418 | if (RT_SUCCESS(rc))
|
---|
1419 | {
|
---|
1420 | rc = PGMPhysSimpleDirtyWriteGCPtr(VMMGetCpu0(pVM), pJumpOffGC, &displ, sizeof(displ));
|
---|
1421 | AssertRC(rc);
|
---|
1422 | }
|
---|
1423 | else
|
---|
1424 | AssertMsgFailed(("Unexpected error %d from MMR3PhysReadGCVirt\n", rc));
|
---|
1425 | }
|
---|
1426 | else
|
---|
1427 | Log(("Skip the guest jump to patch code for this disabled patch %08X\n", pRec->pRelocPos));
|
---|
1428 |
|
---|
1429 | pRec->pDest = pTarget;
|
---|
1430 | break;
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | case FIXUP_REL_JMPTOGUEST:
|
---|
1434 | {
|
---|
1435 | RTRCPTR pSource = (RTRCPTR)((RTRCINTPTR)pRec->pSource + delta);
|
---|
1436 | RTRCINTPTR displ = (RTRCINTPTR)pRec->pDest - (RTRCINTPTR)pSource;
|
---|
1437 |
|
---|
1438 | Assert(!(pPatch->flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
1439 | Log(("Relative fixup (p2g) %08X -> %08X at %08X (source=%08x, target=%08x)\n", *(int32_t*)pRec->pRelocPos, displ, pRec->pRelocPos, pRec->pSource, pRec->pDest));
|
---|
1440 | *(RTRCUINTPTR *)pRec->pRelocPos = displ;
|
---|
1441 | pRec->pSource = pSource;
|
---|
1442 | break;
|
---|
1443 |
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|