VirtualBox

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

Last change on this file since 57329 was 57009, checked in by vboxsync, 9 years ago

PATM: Fixed assertion in patmSaveFixupRecords when re-saving a restored machine.

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