VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PATMSSM.cpp@ 45665

Last change on this file since 45665 was 45620, checked in by vboxsync, 12 years ago

CSAM,PATM: Don't bother initializing anything if HMIsEnabled(). Also, don't allow the components to be enabled.

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

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