VirtualBox

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

Last change on this file since 53403 was 52771, checked in by vboxsync, 10 years ago

VMM/CSAM, PATM: rename SSM_VERSION macros to SAVED_STATE_VERSION macros.

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