1 | /* $Id: PATMSSM.cpp 10141 2008-07-03 08:58: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-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_PATM
|
---|
28 | #include <VBox/patm.h>
|
---|
29 | #include <VBox/stam.h>
|
---|
30 | #include <VBox/pgm.h>
|
---|
31 | #include <VBox/cpum.h>
|
---|
32 | #include <VBox/iom.h>
|
---|
33 | #include <VBox/sup.h>
|
---|
34 | #include <VBox/mm.h>
|
---|
35 | #include <VBox/ssm.h>
|
---|
36 | #include <VBox/pdm.h>
|
---|
37 | #include <VBox/trpm.h>
|
---|
38 | #include <VBox/param.h>
|
---|
39 | #include <iprt/avl.h>
|
---|
40 | #include "PATMInternal.h"
|
---|
41 | #include "PATMPatch.h"
|
---|
42 | #include "PATMA.h"
|
---|
43 | #include <VBox/vm.h>
|
---|
44 | #include <VBox/csam.h>
|
---|
45 |
|
---|
46 | #include <VBox/dbg.h>
|
---|
47 | #include <VBox/err.h>
|
---|
48 | #include <VBox/log.h>
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/asm.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <VBox/dis.h>
|
---|
53 | #include <VBox/disopcode.h>
|
---|
54 |
|
---|
55 | #define PATM_SUBTRACT_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) - (uintptr_t)(b)
|
---|
56 | #define PATM_ADD_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) + (uintptr_t)(b)
|
---|
57 |
|
---|
58 | #ifdef VBOX_STRICT
|
---|
59 | /**
|
---|
60 | * Callback function for RTAvlPVDoWithAll
|
---|
61 | *
|
---|
62 | * Counts the number of patches in the tree
|
---|
63 | *
|
---|
64 | * @returns VBox status code.
|
---|
65 | * @param pNode Current node
|
---|
66 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
67 | */
|
---|
68 | static DECLCALLBACK(int) patmCountLeafPV(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
69 | {
|
---|
70 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Callback function for RTAvlU32DoWithAll
|
---|
76 | *
|
---|
77 | * Counts the number of patches in the tree
|
---|
78 | *
|
---|
79 | * @returns VBox status code.
|
---|
80 | * @param pNode Current node
|
---|
81 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
82 | */
|
---|
83 | static DECLCALLBACK(int) patmCountLeaf(PAVLU32NODECORE pNode, void *pcPatches)
|
---|
84 | {
|
---|
85 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | }
|
---|
88 | #endif /* VBOX_STRICT */
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Callback function for RTAvloU32DoWithAll
|
---|
92 | *
|
---|
93 | * Counts the number of patches in the tree
|
---|
94 | *
|
---|
95 | * @returns VBox status code.
|
---|
96 | * @param pNode Current node
|
---|
97 | * @param pcPatches Pointer to patch counter
|
---|
98 | */
|
---|
99 | static DECLCALLBACK(int) patmCountPatch(PAVLOU32NODECORE pNode, void *pcPatches)
|
---|
100 | {
|
---|
101 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Callback function for RTAvlU32DoWithAll
|
---|
107 | *
|
---|
108 | * Saves all patch to guest lookup records.
|
---|
109 | *
|
---|
110 | * @returns VBox status code.
|
---|
111 | * @param pNode Current node
|
---|
112 | * @param pVM1 VM Handle
|
---|
113 | */
|
---|
114 | static DECLCALLBACK(int) patmSaveP2GLookupRecords(PAVLU32NODECORE pNode, void *pVM1)
|
---|
115 | {
|
---|
116 | PVM pVM = (PVM)pVM1;
|
---|
117 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
118 | PRECPATCHTOGUEST pPatchToGuestRec = (PRECPATCHTOGUEST)pNode;
|
---|
119 |
|
---|
120 | /* Save the lookup record. */
|
---|
121 | int rc = SSMR3PutMem(pSSM, pPatchToGuestRec, sizeof(RECPATCHTOGUEST));
|
---|
122 | AssertRCReturn(rc, rc);
|
---|
123 |
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Callback function for RTAvlPVDoWithAll
|
---|
129 | *
|
---|
130 | * Saves all patch to guest lookup records.
|
---|
131 | *
|
---|
132 | * @returns VBox status code.
|
---|
133 | * @param pNode Current node
|
---|
134 | * @param pVM1 VM Handle
|
---|
135 | */
|
---|
136 | static DECLCALLBACK(int) patmSaveFixupRecords(PAVLPVNODECORE pNode, void *pVM1)
|
---|
137 | {
|
---|
138 | PVM pVM = (PVM)pVM1;
|
---|
139 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
140 | RELOCREC rec = *(PRELOCREC)pNode;
|
---|
141 |
|
---|
142 | Assert(rec.pRelocPos);
|
---|
143 | PATM_SUBTRACT_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
144 |
|
---|
145 | /* Save the lookup record. */
|
---|
146 | int rc = SSMR3PutMem(pSSM, &rec, sizeof(rec));
|
---|
147 | AssertRCReturn(rc, rc);
|
---|
148 |
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Callback function for RTAvloU32DoWithAll
|
---|
155 | *
|
---|
156 | * Saves the state of the patch that's being enumerated
|
---|
157 | *
|
---|
158 | * @returns VBox status code.
|
---|
159 | * @param pNode Current node
|
---|
160 | * @param pVM1 VM Handle
|
---|
161 | */
|
---|
162 | static DECLCALLBACK(int) patmSavePatchState(PAVLOU32NODECORE pNode, void *pVM1)
|
---|
163 | {
|
---|
164 | PVM pVM = (PVM)pVM1;
|
---|
165 | PPATMPATCHREC pPatch = (PPATMPATCHREC)pNode;
|
---|
166 | PATMPATCHREC patch = *pPatch;
|
---|
167 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
168 | int rc;
|
---|
169 |
|
---|
170 | Assert(!(pPatch->patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
174 | */
|
---|
175 | AssertMsg(patch.patch.uState == PATCH_REFUSED || (patch.patch.pPatchBlockOffset || (patch.patch.flags & (PATMFL_SYSENTER_XP|PATMFL_INT3_REPLACEMENT))),
|
---|
176 | ("State = %x pPrivInstrHC=%08x pPatchBlockHC=%08x flags=%x\n", patch.patch.uState, patch.patch.pPrivInstrHC, PATCHCODE_PTR_HC(&patch.patch), patch.patch.flags));
|
---|
177 | Assert(pPatch->patch.JumpTree == 0);
|
---|
178 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->DisasmJumpTree == 0);
|
---|
179 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->IllegalInstrTree == 0);
|
---|
180 |
|
---|
181 | memset(&patch.patch.cacheRec, 0, sizeof(patch.patch.cacheRec));
|
---|
182 |
|
---|
183 | /* Save the patch record itself */
|
---|
184 | rc = SSMR3PutMem(pSSM, &patch, sizeof(patch));
|
---|
185 | AssertRCReturn(rc, rc);
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Reset HC pointers in fixup records and save them.
|
---|
189 | */
|
---|
190 | #ifdef VBOX_STRICT
|
---|
191 | uint32_t nrFixupRecs = 0;
|
---|
192 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmCountLeafPV, &nrFixupRecs);
|
---|
193 | AssertMsg((int32_t)nrFixupRecs == pPatch->patch.nrFixups, ("Fixup inconsistency! counted %d vs %d\n", nrFixupRecs, pPatch->patch.nrFixups));
|
---|
194 | #endif
|
---|
195 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmSaveFixupRecords, pVM);
|
---|
196 |
|
---|
197 | #ifdef VBOX_STRICT
|
---|
198 | uint32_t nrLookupRecords = 0;
|
---|
199 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmCountLeaf, &nrLookupRecords);
|
---|
200 | Assert(nrLookupRecords == pPatch->patch.nrPatch2GuestRecs);
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmSaveP2GLookupRecords, pVM);
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Execute state save operation.
|
---|
209 | *
|
---|
210 | * @returns VBox status code.
|
---|
211 | * @param pVM VM Handle.
|
---|
212 | * @param pSSM SSM operation handle.
|
---|
213 | */
|
---|
214 | DECLCALLBACK(int) patmr3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
215 | {
|
---|
216 | PATM patmInfo = pVM->patm.s;
|
---|
217 | int rc;
|
---|
218 |
|
---|
219 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
223 | */
|
---|
224 | patmInfo.pPatchMemHC = NULL;
|
---|
225 | patmInfo.pGCStateHC = 0;
|
---|
226 | patmInfo.pvFaultMonitor = 0;
|
---|
227 |
|
---|
228 | Assert(patmInfo.ulCallDepth == 0);
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Count the number of patches in the tree (feeling lazy)
|
---|
232 | */
|
---|
233 | patmInfo.savedstate.cPatches = 0;
|
---|
234 | RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmCountPatch, &patmInfo.savedstate.cPatches);
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Save PATM structure
|
---|
238 | */
|
---|
239 | rc = SSMR3PutMem(pSSM, &patmInfo, sizeof(patmInfo));
|
---|
240 | AssertRCReturn(rc, rc);
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Save patch memory contents
|
---|
244 | */
|
---|
245 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
246 | AssertRCReturn(rc, rc);
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Save GC state memory
|
---|
250 | */
|
---|
251 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
252 | AssertRCReturn(rc, rc);
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * Save PATM stack page
|
---|
256 | */
|
---|
257 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
258 | AssertRCReturn(rc, rc);
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * Save all patches
|
---|
262 | */
|
---|
263 | rc = RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmSavePatchState, pVM);
|
---|
264 | AssertRCReturn(rc, rc);
|
---|
265 |
|
---|
266 | /** @note patch statistics are not saved. */
|
---|
267 |
|
---|
268 | return VINF_SUCCESS;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Execute state load operation.
|
---|
273 | *
|
---|
274 | * @returns VBox status code.
|
---|
275 | * @param pVM VM Handle.
|
---|
276 | * @param pSSM SSM operation handle.
|
---|
277 | * @param u32Version Data layout version.
|
---|
278 | */
|
---|
279 | DECLCALLBACK(int) patmr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
280 | {
|
---|
281 | PATM patmInfo;
|
---|
282 | int rc;
|
---|
283 |
|
---|
284 | if ( u32Version != PATM_SSM_VERSION
|
---|
285 | #ifdef PATM_WITH_NEW_SSM
|
---|
286 | && u32Version != PATM_SSM_VERSION_GETPUTMEM)
|
---|
287 | #else
|
---|
288 | )
|
---|
289 | #endif
|
---|
290 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
291 |
|
---|
292 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * Restore PATM structure
|
---|
296 | */
|
---|
297 | #ifdef PATM_WITH_NEW_SSM
|
---|
298 | if (u32Version == PATM_SSM_VERSION_GETPUTMEM)
|
---|
299 | {
|
---|
300 | #endif
|
---|
301 | rc = SSMR3GetMem(pSSM, &patmInfo, sizeof(patmInfo));
|
---|
302 | AssertRCReturn(rc, rc);
|
---|
303 | #ifdef PATM_WITH_NEW_SSM
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | memset(&patmInfo, 0, sizeof(patmInfo));
|
---|
308 |
|
---|
309 | AssertCompile(sizeof(patmInfo.pGCStateGC) == sizeof(RTRCPTR));
|
---|
310 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pGCStateGC);
|
---|
311 | AssertRCReturn(rc, rc);
|
---|
312 |
|
---|
313 | AssertCompile(sizeof(patmInfo.pCPUMCtxGC) == sizeof(RTRCPTR));
|
---|
314 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pCPUMCtxGC);
|
---|
315 | AssertRCReturn(rc, rc);
|
---|
316 |
|
---|
317 | AssertCompile(sizeof(patmInfo.pStatsGC) == sizeof(RTRCPTR));
|
---|
318 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pStatsGC);
|
---|
319 | AssertRCReturn(rc, rc);
|
---|
320 |
|
---|
321 | AssertCompile(sizeof(patmInfo.pfnHelperCallGC) == sizeof(RTRCPTR));
|
---|
322 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperCallGC);
|
---|
323 | AssertRCReturn(rc, rc);
|
---|
324 |
|
---|
325 | AssertCompile(sizeof(patmInfo.pfnHelperRetGC) == sizeof(RTRCPTR));
|
---|
326 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperRetGC);
|
---|
327 | AssertRCReturn(rc, rc);
|
---|
328 |
|
---|
329 | AssertCompile(sizeof(patmInfo.pfnHelperJumpGC) == sizeof(RTRCPTR));
|
---|
330 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperJumpGC);
|
---|
331 | AssertRCReturn(rc, rc);
|
---|
332 |
|
---|
333 | AssertCompile(sizeof(patmInfo.pfnHelperIretGC) == sizeof(RTRCPTR));
|
---|
334 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperIretGC);
|
---|
335 | AssertRCReturn(rc, rc);
|
---|
336 |
|
---|
337 | AssertCompile(sizeof(patmInfo.pPatchMemGC) == sizeof(RTRCPTR));
|
---|
338 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchMemGC);
|
---|
339 | AssertRCReturn(rc, rc);
|
---|
340 |
|
---|
341 | AssertCompile(sizeof(patmInfo.cbPatchMem) == sizeof(uint32_t));
|
---|
342 | rc = SSMR3GetU32(pSSM, &patmInfo.cbPatchMem);
|
---|
343 | AssertRCReturn(rc, rc);
|
---|
344 |
|
---|
345 | AssertCompile(sizeof(patmInfo.offPatchMem) == sizeof(uint32_t));
|
---|
346 | rc = SSMR3GetU32(pSSM, &patmInfo.offPatchMem);
|
---|
347 | AssertRCReturn(rc, rc);
|
---|
348 |
|
---|
349 | AssertCompile(sizeof(patmInfo.deltaReloc) == sizeof(int32_t));
|
---|
350 | rc = SSMR3GetS32(pSSM, &patmInfo.deltaReloc);
|
---|
351 | AssertRCReturn(rc, rc);
|
---|
352 |
|
---|
353 | AssertCompile(sizeof(patmInfo.uCurrentPatchIdx) == sizeof(uint32_t));
|
---|
354 | rc = SSMR3GetS32(pSSM, &patmInfo.uCurrentPatchIdx);
|
---|
355 | AssertRCReturn(rc, rc);
|
---|
356 |
|
---|
357 | AssertCompile(sizeof(patmInfo.pPatchedInstrGCLowest) == sizeof(RTRCPTR));
|
---|
358 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchedInstrGCLowest);
|
---|
359 | AssertRCReturn(rc, rc);
|
---|
360 |
|
---|
361 | AssertCompile(sizeof(patmInfo.pPatchedInstrGCHighest) == sizeof(RTRCPTR));
|
---|
362 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchedInstrGCHighest);
|
---|
363 | AssertRCReturn(rc, rc);
|
---|
364 |
|
---|
365 | AssertCompile(sizeof(patmInfo.pfnSysEnterGC) == sizeof(RTRCPTR));
|
---|
366 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnSysEnterGC);
|
---|
367 | AssertRCReturn(rc, rc);
|
---|
368 |
|
---|
369 | AssertCompile(sizeof(patmInfo.pfnSysEnterPatchGC) == sizeof(RTRCPTR));
|
---|
370 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnSysEnterPatchGC);
|
---|
371 | AssertRCReturn(rc, rc);
|
---|
372 |
|
---|
373 | AssertCompile(sizeof(patmInfo.uSysEnterPatchIdx) == sizeof(uint32_t));
|
---|
374 | rc = SSMR3GetU32(pSSM, &patmInfo.uSysEnterPatchIdx);
|
---|
375 | AssertRCReturn(rc, rc);
|
---|
376 |
|
---|
377 | AssertCompile(sizeof(patmInfo.ulCallDepth) == sizeof(uint32_t));
|
---|
378 | rc = SSMR3GetU32(pSSM, &patmInfo.ulCallDepth);
|
---|
379 | AssertRCReturn(rc, rc);
|
---|
380 |
|
---|
381 | AssertCompile(sizeof(patmInfo.pGCStackGC) == sizeof(RTRCPTR));
|
---|
382 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pGCStackGC);
|
---|
383 | AssertRCReturn(rc, rc);
|
---|
384 |
|
---|
385 | AssertCompile(sizeof(patmInfo.cPageRecords) == sizeof(uint32_t));
|
---|
386 | rc = SSMR3GetU32(pSSM, &patmInfo.cPageRecords);
|
---|
387 | AssertRCReturn(rc, rc);
|
---|
388 |
|
---|
389 | AssertCompile(sizeof(patmInfo.fOutOfMemory) == sizeof(bool));
|
---|
390 | rc = SSMR3GetBool(pSSM, &patmInfo.fOutOfMemory);
|
---|
391 | AssertRCReturn(rc, rc);
|
---|
392 |
|
---|
393 | AssertCompile(sizeof(patmInfo.savedstate.cPatches) == sizeof(uint32_t));
|
---|
394 | rc = SSMR3GetU32(pSSM, &patmInfo.savedstate.cPatches);
|
---|
395 | AssertRCReturn(rc, rc);
|
---|
396 |
|
---|
397 | }
|
---|
398 | #endif
|
---|
399 |
|
---|
400 | /** @todo this restriction could be removed as we relocate when loading the saved state,.. */
|
---|
401 | if ( pVM->patm.s.pGCStateGC != patmInfo.pGCStateGC
|
---|
402 | || pVM->patm.s.pCPUMCtxGC != patmInfo.pCPUMCtxGC
|
---|
403 | || pVM->patm.s.pStatsGC != patmInfo.pStatsGC)
|
---|
404 | {
|
---|
405 | if (SSMR3HandleGetAfter(pSSM) == SSMAFTER_DEBUG_IT) /* hack for x86 / amd64 mix. */
|
---|
406 | return VINF_SUCCESS;
|
---|
407 | AssertMsgFailed(("GC state, stat or cpum ptrs don't match!!!\n"));
|
---|
408 | return VERR_SSM_INVALID_STATE;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* Relative calls are made to the helper functions. Therefor their location must not change! */
|
---|
412 | if ( pVM->patm.s.pfnHelperCallGC != patmInfo.pfnHelperCallGC
|
---|
413 | || pVM->patm.s.pfnHelperRetGC != patmInfo.pfnHelperRetGC
|
---|
414 | || pVM->patm.s.pfnHelperJumpGC != patmInfo.pfnHelperJumpGC
|
---|
415 | || pVM->patm.s.pfnHelperIretGC != patmInfo.pfnHelperIretGC)
|
---|
416 | {
|
---|
417 | AssertMsgFailed(("Helper function ptrs don't match!!!\n"));
|
---|
418 | return VERR_SSM_INVALID_STATE;
|
---|
419 | }
|
---|
420 |
|
---|
421 | if ( pVM->patm.s.pPatchMemGC != patmInfo.pPatchMemGC
|
---|
422 | || pVM->patm.s.cbPatchMem != patmInfo.cbPatchMem)
|
---|
423 | {
|
---|
424 | AssertMsgFailed(("Patch memory ptrs and/or sizes don't match!!!\n"));
|
---|
425 | return VERR_SSM_INVALID_STATE;
|
---|
426 | }
|
---|
427 | pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
|
---|
428 | pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
|
---|
429 | pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
|
---|
430 | pVM->patm.s.fOutOfMemory = patmInfo.fOutOfMemory;
|
---|
431 |
|
---|
432 | /* Lowest and highest patched instruction */
|
---|
433 | pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
|
---|
434 | pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
|
---|
435 |
|
---|
436 | /* Sysenter handlers */
|
---|
437 | pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
|
---|
438 | pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
|
---|
439 | pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
|
---|
440 |
|
---|
441 | Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
|
---|
442 |
|
---|
443 | /** @note patch statistics are not restored. */
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Restore patch memory contents
|
---|
447 | */
|
---|
448 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
449 | AssertRCReturn(rc, rc);
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Restore GC state memory
|
---|
453 | */
|
---|
454 | #ifdef PATM_WITH_NEW_SSM
|
---|
455 | if (u32Version == PATM_SSM_VERSION_GETPUTMEM)
|
---|
456 | {
|
---|
457 | #endif
|
---|
458 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
459 | AssertRCReturn(rc, rc);
|
---|
460 | #ifdef PATM_WITH_NEW_SSM
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uVMFlags) == sizeof(uint32_t));
|
---|
465 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uVMFlags);
|
---|
466 | AssertRCReturn(rc, rc);
|
---|
467 |
|
---|
468 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uPendingAction) == sizeof(uint32_t));
|
---|
469 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uPendingAction);
|
---|
470 | AssertRCReturn(rc, rc);
|
---|
471 |
|
---|
472 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uPatchCalls) == sizeof(uint32_t));
|
---|
473 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uPatchCalls);
|
---|
474 | AssertRCReturn(rc, rc);
|
---|
475 |
|
---|
476 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uScratch) == sizeof(uint32_t));
|
---|
477 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uScratch);
|
---|
478 | AssertRCReturn(rc, rc);
|
---|
479 |
|
---|
480 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretEFlags) == sizeof(uint32_t));
|
---|
481 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretEFlags);
|
---|
482 | AssertRCReturn(rc, rc);
|
---|
483 |
|
---|
484 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretCS) == sizeof(uint32_t));
|
---|
485 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretCS);
|
---|
486 | AssertRCReturn(rc, rc);
|
---|
487 |
|
---|
488 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretEIP) == sizeof(uint32_t));
|
---|
489 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretEIP);
|
---|
490 | AssertRCReturn(rc, rc);
|
---|
491 |
|
---|
492 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Psp) == sizeof(uint32_t));
|
---|
493 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Psp);
|
---|
494 | AssertRCReturn(rc, rc);
|
---|
495 |
|
---|
496 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->fPIF) == sizeof(uint32_t));
|
---|
497 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->fPIF);
|
---|
498 | AssertRCReturn(rc, rc);
|
---|
499 |
|
---|
500 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCPtrInhibitInterrupts) == sizeof(RTRCPTR));
|
---|
501 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCPtrInhibitInterrupts);
|
---|
502 | AssertRCReturn(rc, rc);
|
---|
503 |
|
---|
504 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCCallPatchTargetAddr) == sizeof(RTRCPTR));
|
---|
505 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCCallPatchTargetAddr);
|
---|
506 | AssertRCReturn(rc, rc);
|
---|
507 |
|
---|
508 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCCallReturnAddr) == sizeof(RTRCPTR));
|
---|
509 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCCallReturnAddr);
|
---|
510 | AssertRCReturn(rc, rc);
|
---|
511 |
|
---|
512 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uEAX) == sizeof(uint32_t));
|
---|
513 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uEAX);
|
---|
514 | AssertRCReturn(rc, rc);
|
---|
515 |
|
---|
516 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uECX) == sizeof(uint32_t));
|
---|
517 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uECX);
|
---|
518 | AssertRCReturn(rc, rc);
|
---|
519 |
|
---|
520 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uEDI) == sizeof(uint32_t));
|
---|
521 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uEDI);
|
---|
522 | AssertRCReturn(rc, rc);
|
---|
523 |
|
---|
524 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.eFlags) == sizeof(uint32_t));
|
---|
525 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.eFlags);
|
---|
526 | AssertRCReturn(rc, rc);
|
---|
527 |
|
---|
528 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uFlags) == sizeof(uint32_t));
|
---|
529 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uFlags);
|
---|
530 | AssertRCReturn(rc, rc);
|
---|
531 | }
|
---|
532 | #endif
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Restore PATM stack page
|
---|
536 | */
|
---|
537 | if (pVM->patm.s.pGCStackGC != patmInfo.pGCStackGC)
|
---|
538 | {
|
---|
539 | AssertMsgFailed(("GC patch stack ptrs don't match!!!\n"));
|
---|
540 | return VERR_SSM_INVALID_STATE;
|
---|
541 | }
|
---|
542 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
543 | AssertRCReturn(rc, rc);
|
---|
544 |
|
---|
545 | /*
|
---|
546 | * Load all patches
|
---|
547 | */
|
---|
548 | for (uint32_t i=0;i<patmInfo.savedstate.cPatches;i++)
|
---|
549 | {
|
---|
550 | PATMPATCHREC patch, *pPatchRec;
|
---|
551 |
|
---|
552 | rc = SSMR3GetMem(pSSM, &patch, sizeof(patch));
|
---|
553 | AssertRCReturn(rc, rc);
|
---|
554 |
|
---|
555 | Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
556 |
|
---|
557 | rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
|
---|
558 | if (VBOX_FAILURE(rc))
|
---|
559 | {
|
---|
560 | AssertMsgFailed(("Out of memory!!!!\n"));
|
---|
561 | return VERR_NO_MEMORY;
|
---|
562 | }
|
---|
563 | /*
|
---|
564 | * Only restore the patch part of the tree record; not the internal data (except the key of course)
|
---|
565 | */
|
---|
566 | pPatchRec->patch = patch.patch;
|
---|
567 | pPatchRec->Core.Key = patch.Core.Key;
|
---|
568 | pPatchRec->CoreOffset.Key = patch.CoreOffset.Key;
|
---|
569 |
|
---|
570 | bool ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
|
---|
571 | Assert(ret);
|
---|
572 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
573 | {
|
---|
574 | if (pPatchRec->patch.pPatchBlockOffset)
|
---|
575 | {
|
---|
576 | /* We actually generated code for this patch. */
|
---|
577 | ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
|
---|
578 | AssertMsg(ret, ("Inserting patch %VGv offset %VGv failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
|
---|
579 | }
|
---|
580 | }
|
---|
581 | /* Set to zero as we don't need it anymore. */
|
---|
582 | pPatchRec->patch.pTempInfo = 0;
|
---|
583 |
|
---|
584 | pPatchRec->patch.pPrivInstrHC = 0;
|
---|
585 | /* The GC virtual ptr is fixed, but we must convert it manually again to HC. */
|
---|
586 | rc = PGMPhysGCPtr2HCPtr(pVM, pPatchRec->patch.pPrivInstrGC, (PRTHCPTR)&pPatchRec->patch.pPrivInstrHC);
|
---|
587 | /* Can fail due to page or page table not present. */
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Restore fixup records and correct HC pointers in fixup records
|
---|
591 | */
|
---|
592 | pPatchRec->patch.FixupTree = 0;
|
---|
593 | pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
|
---|
594 | for (int i=0;i<patch.patch.nrFixups;i++)
|
---|
595 | {
|
---|
596 | RELOCREC rec;
|
---|
597 |
|
---|
598 | rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
|
---|
599 | AssertRCReturn(rc, rc);
|
---|
600 | PATM_ADD_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
601 |
|
---|
602 | rc = patmPatchAddReloc32(pVM, &pPatchRec->patch, rec.pRelocPos, rec.uType, rec.pSource, rec.pDest);
|
---|
603 | AssertRCReturn(rc, rc);
|
---|
604 | }
|
---|
605 |
|
---|
606 | /* And all patch to guest lookup records */
|
---|
607 | Assert(pPatchRec->patch.nrPatch2GuestRecs || pPatchRec->patch.uState == PATCH_REFUSED || (pPatchRec->patch.flags & (PATMFL_SYSENTER_XP | PATMFL_IDTHANDLER | PATMFL_TRAPHANDLER | PATMFL_INT3_REPLACEMENT)));
|
---|
608 |
|
---|
609 | pPatchRec->patch.Patch2GuestAddrTree = 0;
|
---|
610 | pPatchRec->patch.Guest2PatchAddrTree = 0;
|
---|
611 | if (pPatchRec->patch.nrPatch2GuestRecs)
|
---|
612 | {
|
---|
613 | RECPATCHTOGUEST rec;
|
---|
614 | uint32_t nrPatch2GuestRecs = pPatchRec->patch.nrPatch2GuestRecs;
|
---|
615 |
|
---|
616 | pPatchRec->patch.nrPatch2GuestRecs = 0; /* incremented by patmr3AddP2GLookupRecord */
|
---|
617 | for (uint32_t i=0;i<nrPatch2GuestRecs;i++)
|
---|
618 | {
|
---|
619 | rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
|
---|
620 | AssertRCReturn(rc, rc);
|
---|
621 |
|
---|
622 | patmr3AddP2GLookupRecord(pVM, &pPatchRec->patch, (uintptr_t)rec.Core.Key + pVM->patm.s.pPatchMemHC, rec.pOrgInstrGC, rec.enmType, rec.fDirty);
|
---|
623 | }
|
---|
624 | Assert(pPatchRec->patch.Patch2GuestAddrTree);
|
---|
625 | }
|
---|
626 |
|
---|
627 | if (pPatchRec->patch.flags & PATMFL_CODE_MONITORED)
|
---|
628 | {
|
---|
629 | /* Insert the guest page lookup records (for detection self-modifying code) */
|
---|
630 | rc = patmInsertPatchPages(pVM, &pPatchRec->patch);
|
---|
631 | AssertRCReturn(rc, rc);
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | #ifdef VBOX_WITH_STATISTICS
|
---|
636 | /*
|
---|
637 | * Restore relevant old statistics
|
---|
638 | */
|
---|
639 | pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
|
---|
640 | pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
|
---|
641 | pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
|
---|
642 | pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
|
---|
643 | #endif
|
---|
644 | return VINF_SUCCESS;
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|