VirtualBox

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

Last change on this file since 49794 was 49794, checked in by vboxsync, 11 years ago

PATMSSM.cpp: RTAvlU32DoWithAll rc fix.

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