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