1 | /* $Id: PATMSSM.cpp 11915 2008-09-01 12:48:25Z 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 | static void patmCorrectAbsoluteFixup(PVM pVM, PATM &patmInfo, int32_t offset, RTRCPTR *pFixup);
|
---|
59 |
|
---|
60 | #ifdef VBOX_STRICT
|
---|
61 | /**
|
---|
62 | * Callback function for RTAvlPVDoWithAll
|
---|
63 | *
|
---|
64 | * Counts the number of patches in the tree
|
---|
65 | *
|
---|
66 | * @returns VBox status code.
|
---|
67 | * @param pNode Current node
|
---|
68 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
69 | */
|
---|
70 | static DECLCALLBACK(int) patmCountLeafPV(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
71 | {
|
---|
72 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
73 | return VINF_SUCCESS;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Callback function for RTAvlU32DoWithAll
|
---|
78 | *
|
---|
79 | * Counts the number of patches in the tree
|
---|
80 | *
|
---|
81 | * @returns VBox status code.
|
---|
82 | * @param pNode Current node
|
---|
83 | * @param pcPatches Pointer to patch counter (uint32_t)
|
---|
84 | */
|
---|
85 | static DECLCALLBACK(int) patmCountLeaf(PAVLU32NODECORE pNode, void *pcPatches)
|
---|
86 | {
|
---|
87 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 | #endif /* VBOX_STRICT */
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Callback function for RTAvloU32DoWithAll
|
---|
94 | *
|
---|
95 | * Counts the number of patches in the tree
|
---|
96 | *
|
---|
97 | * @returns VBox status code.
|
---|
98 | * @param pNode Current node
|
---|
99 | * @param pcPatches Pointer to patch counter
|
---|
100 | */
|
---|
101 | static DECLCALLBACK(int) patmCountPatch(PAVLOU32NODECORE pNode, void *pcPatches)
|
---|
102 | {
|
---|
103 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
104 | return VINF_SUCCESS;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Callback function for RTAvlU32DoWithAll
|
---|
109 | *
|
---|
110 | * Saves all patch to guest lookup records.
|
---|
111 | *
|
---|
112 | * @returns VBox status code.
|
---|
113 | * @param pNode Current node
|
---|
114 | * @param pVM1 VM Handle
|
---|
115 | */
|
---|
116 | static DECLCALLBACK(int) patmSaveP2GLookupRecords(PAVLU32NODECORE pNode, void *pVM1)
|
---|
117 | {
|
---|
118 | PVM pVM = (PVM)pVM1;
|
---|
119 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
120 | PRECPATCHTOGUEST pPatchToGuestRec = (PRECPATCHTOGUEST)pNode;
|
---|
121 |
|
---|
122 | /* Save the lookup record. */
|
---|
123 | int rc = SSMR3PutMem(pSSM, pPatchToGuestRec, sizeof(RECPATCHTOGUEST));
|
---|
124 | AssertRCReturn(rc, rc);
|
---|
125 |
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Callback function for RTAvlPVDoWithAll
|
---|
131 | *
|
---|
132 | * Saves all patch to guest lookup records.
|
---|
133 | *
|
---|
134 | * @returns VBox status code.
|
---|
135 | * @param pNode Current node
|
---|
136 | * @param pVM1 VM Handle
|
---|
137 | */
|
---|
138 | static DECLCALLBACK(int) patmSaveFixupRecords(PAVLPVNODECORE pNode, void *pVM1)
|
---|
139 | {
|
---|
140 | PVM pVM = (PVM)pVM1;
|
---|
141 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
142 | RELOCREC rec = *(PRELOCREC)pNode;
|
---|
143 |
|
---|
144 | Assert(rec.pRelocPos);
|
---|
145 | PATM_SUBTRACT_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
146 |
|
---|
147 | /* Save the lookup record. */
|
---|
148 | int rc = SSMR3PutMem(pSSM, &rec, sizeof(rec));
|
---|
149 | AssertRCReturn(rc, rc);
|
---|
150 |
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Callback function for RTAvloU32DoWithAll
|
---|
157 | *
|
---|
158 | * Saves the state of the patch that's being enumerated
|
---|
159 | *
|
---|
160 | * @returns VBox status code.
|
---|
161 | * @param pNode Current node
|
---|
162 | * @param pVM1 VM Handle
|
---|
163 | */
|
---|
164 | static DECLCALLBACK(int) patmSavePatchState(PAVLOU32NODECORE pNode, void *pVM1)
|
---|
165 | {
|
---|
166 | PVM pVM = (PVM)pVM1;
|
---|
167 | PPATMPATCHREC pPatch = (PPATMPATCHREC)pNode;
|
---|
168 | PATMPATCHREC patch = *pPatch;
|
---|
169 | PSSMHANDLE pSSM = pVM->patm.s.savedstate.pSSM;
|
---|
170 | int rc;
|
---|
171 |
|
---|
172 | Assert(!(pPatch->patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
176 | */
|
---|
177 | AssertMsg(patch.patch.uState == PATCH_REFUSED || (patch.patch.pPatchBlockOffset || (patch.patch.flags & (PATMFL_SYSENTER_XP|PATMFL_INT3_REPLACEMENT))),
|
---|
178 | ("State = %x pPrivInstrHC=%08x pPatchBlockHC=%08x flags=%x\n", patch.patch.uState, patch.patch.pPrivInstrHC, PATCHCODE_PTR_HC(&patch.patch), patch.patch.flags));
|
---|
179 | Assert(pPatch->patch.JumpTree == 0);
|
---|
180 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->DisasmJumpTree == 0);
|
---|
181 | Assert(!pPatch->patch.pTempInfo || pPatch->patch.pTempInfo->IllegalInstrTree == 0);
|
---|
182 |
|
---|
183 | memset(&patch.patch.cacheRec, 0, sizeof(patch.patch.cacheRec));
|
---|
184 |
|
---|
185 | /* Save the patch record itself */
|
---|
186 | rc = SSMR3PutMem(pSSM, &patch, sizeof(patch));
|
---|
187 | AssertRCReturn(rc, rc);
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Reset HC pointers in fixup records and save them.
|
---|
191 | */
|
---|
192 | #ifdef VBOX_STRICT
|
---|
193 | uint32_t nrFixupRecs = 0;
|
---|
194 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmCountLeafPV, &nrFixupRecs);
|
---|
195 | AssertMsg((int32_t)nrFixupRecs == pPatch->patch.nrFixups, ("Fixup inconsistency! counted %d vs %d\n", nrFixupRecs, pPatch->patch.nrFixups));
|
---|
196 | #endif
|
---|
197 | RTAvlPVDoWithAll(&pPatch->patch.FixupTree, true, patmSaveFixupRecords, pVM);
|
---|
198 |
|
---|
199 | #ifdef VBOX_STRICT
|
---|
200 | uint32_t nrLookupRecords = 0;
|
---|
201 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmCountLeaf, &nrLookupRecords);
|
---|
202 | Assert(nrLookupRecords == pPatch->patch.nrPatch2GuestRecs);
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | RTAvlU32DoWithAll(&pPatch->patch.Patch2GuestAddrTree, true, patmSaveP2GLookupRecords, pVM);
|
---|
206 | return VINF_SUCCESS;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Execute state save operation.
|
---|
211 | *
|
---|
212 | * @returns VBox status code.
|
---|
213 | * @param pVM VM Handle.
|
---|
214 | * @param pSSM SSM operation handle.
|
---|
215 | */
|
---|
216 | DECLCALLBACK(int) patmr3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
217 | {
|
---|
218 | PATM patmInfo = pVM->patm.s;
|
---|
219 | int rc;
|
---|
220 |
|
---|
221 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Reset HC pointers that need to be recalculated when loading the state
|
---|
225 | */
|
---|
226 | patmInfo.pPatchMemHC = NULL;
|
---|
227 | patmInfo.pGCStateHC = 0;
|
---|
228 | patmInfo.pvFaultMonitor = 0;
|
---|
229 |
|
---|
230 | Assert(patmInfo.ulCallDepth == 0);
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Count the number of patches in the tree (feeling lazy)
|
---|
234 | */
|
---|
235 | patmInfo.savedstate.cPatches = 0;
|
---|
236 | RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmCountPatch, &patmInfo.savedstate.cPatches);
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Save PATM structure
|
---|
240 | */
|
---|
241 | rc = SSMR3PutMem(pSSM, &patmInfo, sizeof(patmInfo));
|
---|
242 | AssertRCReturn(rc, rc);
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Save patch memory contents
|
---|
246 | */
|
---|
247 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
248 | AssertRCReturn(rc, rc);
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Save GC state memory
|
---|
252 | */
|
---|
253 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
254 | AssertRCReturn(rc, rc);
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Save PATM stack page
|
---|
258 | */
|
---|
259 | rc = SSMR3PutMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
260 | AssertRCReturn(rc, rc);
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Save all patches
|
---|
264 | */
|
---|
265 | rc = RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true, patmSavePatchState, pVM);
|
---|
266 | AssertRCReturn(rc, rc);
|
---|
267 |
|
---|
268 | /** @note patch statistics are not saved. */
|
---|
269 |
|
---|
270 | return VINF_SUCCESS;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Execute state load operation.
|
---|
275 | *
|
---|
276 | * @returns VBox status code.
|
---|
277 | * @param pVM VM Handle.
|
---|
278 | * @param pSSM SSM operation handle.
|
---|
279 | * @param u32Version Data layout version.
|
---|
280 | */
|
---|
281 | DECLCALLBACK(int) patmr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
282 | {
|
---|
283 | PATM patmInfo;
|
---|
284 | int rc;
|
---|
285 |
|
---|
286 | if ( u32Version != PATM_SSM_VERSION
|
---|
287 | #ifdef PATM_WITH_NEW_SSM
|
---|
288 | && u32Version != PATM_SSM_VERSION_GETPUTMEM)
|
---|
289 | #else
|
---|
290 | )
|
---|
291 | #endif
|
---|
292 | {
|
---|
293 | AssertMsgFailed(("patmR3Load: Invalid version u32Version=%d!\n", u32Version));
|
---|
294 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
295 | }
|
---|
296 |
|
---|
297 | pVM->patm.s.savedstate.pSSM = pSSM;
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Restore PATM structure
|
---|
301 | */
|
---|
302 | #ifdef PATM_WITH_NEW_SSM
|
---|
303 | if (u32Version == PATM_SSM_VERSION_GETPUTMEM)
|
---|
304 | {
|
---|
305 | #endif
|
---|
306 | rc = SSMR3GetMem(pSSM, &patmInfo, sizeof(patmInfo));
|
---|
307 | AssertRCReturn(rc, rc);
|
---|
308 | #ifdef PATM_WITH_NEW_SSM
|
---|
309 | }
|
---|
310 | else
|
---|
311 | {
|
---|
312 | memset(&patmInfo, 0, sizeof(patmInfo));
|
---|
313 |
|
---|
314 | AssertCompile(sizeof(patmInfo.pGCStateGC) == sizeof(RTRCPTR));
|
---|
315 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pGCStateGC);
|
---|
316 | AssertRCReturn(rc, rc);
|
---|
317 |
|
---|
318 | AssertCompile(sizeof(patmInfo.pCPUMCtxGC) == sizeof(RTRCPTR));
|
---|
319 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pCPUMCtxGC);
|
---|
320 | AssertRCReturn(rc, rc);
|
---|
321 |
|
---|
322 | AssertCompile(sizeof(patmInfo.pStatsGC) == sizeof(RTRCPTR));
|
---|
323 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pStatsGC);
|
---|
324 | AssertRCReturn(rc, rc);
|
---|
325 |
|
---|
326 | AssertCompile(sizeof(patmInfo.pfnHelperCallGC) == sizeof(RTRCPTR));
|
---|
327 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperCallGC);
|
---|
328 | AssertRCReturn(rc, rc);
|
---|
329 |
|
---|
330 | AssertCompile(sizeof(patmInfo.pfnHelperRetGC) == sizeof(RTRCPTR));
|
---|
331 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperRetGC);
|
---|
332 | AssertRCReturn(rc, rc);
|
---|
333 |
|
---|
334 | AssertCompile(sizeof(patmInfo.pfnHelperJumpGC) == sizeof(RTRCPTR));
|
---|
335 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperJumpGC);
|
---|
336 | AssertRCReturn(rc, rc);
|
---|
337 |
|
---|
338 | AssertCompile(sizeof(patmInfo.pfnHelperIretGC) == sizeof(RTRCPTR));
|
---|
339 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnHelperIretGC);
|
---|
340 | AssertRCReturn(rc, rc);
|
---|
341 |
|
---|
342 | AssertCompile(sizeof(patmInfo.pPatchMemGC) == sizeof(RTRCPTR));
|
---|
343 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchMemGC);
|
---|
344 | AssertRCReturn(rc, rc);
|
---|
345 |
|
---|
346 | AssertCompile(sizeof(patmInfo.cbPatchMem) == sizeof(uint32_t));
|
---|
347 | rc = SSMR3GetU32(pSSM, &patmInfo.cbPatchMem);
|
---|
348 | AssertRCReturn(rc, rc);
|
---|
349 |
|
---|
350 | AssertCompile(sizeof(patmInfo.offPatchMem) == sizeof(uint32_t));
|
---|
351 | rc = SSMR3GetU32(pSSM, &patmInfo.offPatchMem);
|
---|
352 | AssertRCReturn(rc, rc);
|
---|
353 |
|
---|
354 | AssertCompile(sizeof(patmInfo.deltaReloc) == sizeof(int32_t));
|
---|
355 | rc = SSMR3GetS32(pSSM, &patmInfo.deltaReloc);
|
---|
356 | AssertRCReturn(rc, rc);
|
---|
357 |
|
---|
358 | AssertCompile(sizeof(patmInfo.uCurrentPatchIdx) == sizeof(uint32_t));
|
---|
359 | rc = SSMR3GetS32(pSSM, &patmInfo.uCurrentPatchIdx);
|
---|
360 | AssertRCReturn(rc, rc);
|
---|
361 |
|
---|
362 | AssertCompile(sizeof(patmInfo.pPatchedInstrGCLowest) == sizeof(RTRCPTR));
|
---|
363 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchedInstrGCLowest);
|
---|
364 | AssertRCReturn(rc, rc);
|
---|
365 |
|
---|
366 | AssertCompile(sizeof(patmInfo.pPatchedInstrGCHighest) == sizeof(RTRCPTR));
|
---|
367 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pPatchedInstrGCHighest);
|
---|
368 | AssertRCReturn(rc, rc);
|
---|
369 |
|
---|
370 | AssertCompile(sizeof(patmInfo.pfnSysEnterGC) == sizeof(RTRCPTR));
|
---|
371 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnSysEnterGC);
|
---|
372 | AssertRCReturn(rc, rc);
|
---|
373 |
|
---|
374 | AssertCompile(sizeof(patmInfo.pfnSysEnterPatchGC) == sizeof(RTRCPTR));
|
---|
375 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pfnSysEnterPatchGC);
|
---|
376 | AssertRCReturn(rc, rc);
|
---|
377 |
|
---|
378 | AssertCompile(sizeof(patmInfo.uSysEnterPatchIdx) == sizeof(uint32_t));
|
---|
379 | rc = SSMR3GetU32(pSSM, &patmInfo.uSysEnterPatchIdx);
|
---|
380 | AssertRCReturn(rc, rc);
|
---|
381 |
|
---|
382 | AssertCompile(sizeof(patmInfo.ulCallDepth) == sizeof(uint32_t));
|
---|
383 | rc = SSMR3GetU32(pSSM, &patmInfo.ulCallDepth);
|
---|
384 | AssertRCReturn(rc, rc);
|
---|
385 |
|
---|
386 | AssertCompile(sizeof(patmInfo.pGCStackGC) == sizeof(RTRCPTR));
|
---|
387 | rc = SSMR3GetRCPtr(pSSM, &patmInfo.pGCStackGC);
|
---|
388 | AssertRCReturn(rc, rc);
|
---|
389 |
|
---|
390 | AssertCompile(sizeof(patmInfo.cPageRecords) == sizeof(uint32_t));
|
---|
391 | rc = SSMR3GetU32(pSSM, &patmInfo.cPageRecords);
|
---|
392 | AssertRCReturn(rc, rc);
|
---|
393 |
|
---|
394 | AssertCompile(sizeof(patmInfo.fOutOfMemory) == sizeof(bool));
|
---|
395 | rc = SSMR3GetBool(pSSM, &patmInfo.fOutOfMemory);
|
---|
396 | AssertRCReturn(rc, rc);
|
---|
397 |
|
---|
398 | AssertCompile(sizeof(patmInfo.savedstate.cPatches) == sizeof(uint32_t));
|
---|
399 | rc = SSMR3GetU32(pSSM, &patmInfo.savedstate.cPatches);
|
---|
400 | AssertRCReturn(rc, rc);
|
---|
401 |
|
---|
402 | }
|
---|
403 | #endif
|
---|
404 |
|
---|
405 | /* Relative calls are made to the helper functions. Therefor their relative location must not change! */
|
---|
406 | /* Note: we reuse the saved global helpers and assume they are identical, which is kind of dangerous. */
|
---|
407 | if ( (pVM->patm.s.pfnHelperCallGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperCallGC - patmInfo.pPatchMemGC)
|
---|
408 | || (pVM->patm.s.pfnHelperRetGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperRetGC - patmInfo.pPatchMemGC)
|
---|
409 | || (pVM->patm.s.pfnHelperJumpGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperJumpGC - patmInfo.pPatchMemGC)
|
---|
410 | || (pVM->patm.s.pfnHelperIretGC - pVM->patm.s.pPatchMemGC) != (patmInfo.pfnHelperIretGC - patmInfo.pPatchMemGC))
|
---|
411 | {
|
---|
412 | AssertMsgFailed(("Helper function ptrs don't match!!!\n"));
|
---|
413 | return VERR_SSM_INVALID_STATE;
|
---|
414 | }
|
---|
415 |
|
---|
416 | if (pVM->patm.s.cbPatchMem != patmInfo.cbPatchMem)
|
---|
417 | {
|
---|
418 | AssertMsgFailed(("Patch memory ptrs and/or sizes don't match!!!\n"));
|
---|
419 | return VERR_SSM_INVALID_STATE;
|
---|
420 | }
|
---|
421 | pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
|
---|
422 | pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
|
---|
423 | pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
|
---|
424 | pVM->patm.s.fOutOfMemory = patmInfo.fOutOfMemory;
|
---|
425 |
|
---|
426 | /* Lowest and highest patched instruction */
|
---|
427 | pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
|
---|
428 | pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
|
---|
429 |
|
---|
430 | /* Sysenter handlers */
|
---|
431 | pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
|
---|
432 | pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
|
---|
433 | pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
|
---|
434 |
|
---|
435 | Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
|
---|
436 |
|
---|
437 | /** @note patch statistics are not restored. */
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * Restore patch memory contents
|
---|
441 | */
|
---|
442 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
|
---|
443 | AssertRCReturn(rc, rc);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Restore GC state memory
|
---|
447 | */
|
---|
448 | #ifdef PATM_WITH_NEW_SSM
|
---|
449 | if (u32Version == PATM_SSM_VERSION_GETPUTMEM)
|
---|
450 | {
|
---|
451 | #endif
|
---|
452 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
|
---|
453 | AssertRCReturn(rc, rc);
|
---|
454 | #ifdef PATM_WITH_NEW_SSM
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uVMFlags) == sizeof(uint32_t));
|
---|
459 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uVMFlags);
|
---|
460 | AssertRCReturn(rc, rc);
|
---|
461 |
|
---|
462 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uPendingAction) == sizeof(uint32_t));
|
---|
463 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uPendingAction);
|
---|
464 | AssertRCReturn(rc, rc);
|
---|
465 |
|
---|
466 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uPatchCalls) == sizeof(uint32_t));
|
---|
467 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uPatchCalls);
|
---|
468 | AssertRCReturn(rc, rc);
|
---|
469 |
|
---|
470 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uScratch) == sizeof(uint32_t));
|
---|
471 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uScratch);
|
---|
472 | AssertRCReturn(rc, rc);
|
---|
473 |
|
---|
474 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretEFlags) == sizeof(uint32_t));
|
---|
475 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretEFlags);
|
---|
476 | AssertRCReturn(rc, rc);
|
---|
477 |
|
---|
478 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretCS) == sizeof(uint32_t));
|
---|
479 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretCS);
|
---|
480 | AssertRCReturn(rc, rc);
|
---|
481 |
|
---|
482 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->uIretEIP) == sizeof(uint32_t));
|
---|
483 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->uIretEIP);
|
---|
484 | AssertRCReturn(rc, rc);
|
---|
485 |
|
---|
486 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Psp) == sizeof(uint32_t));
|
---|
487 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Psp);
|
---|
488 | AssertRCReturn(rc, rc);
|
---|
489 |
|
---|
490 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->fPIF) == sizeof(uint32_t));
|
---|
491 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->fPIF);
|
---|
492 | AssertRCReturn(rc, rc);
|
---|
493 |
|
---|
494 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCPtrInhibitInterrupts) == sizeof(RTRCPTR));
|
---|
495 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCPtrInhibitInterrupts);
|
---|
496 | AssertRCReturn(rc, rc);
|
---|
497 |
|
---|
498 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCCallPatchTargetAddr) == sizeof(RTRCPTR));
|
---|
499 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCCallPatchTargetAddr);
|
---|
500 | AssertRCReturn(rc, rc);
|
---|
501 |
|
---|
502 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->GCCallReturnAddr) == sizeof(RTRCPTR));
|
---|
503 | rc = SSMR3GetRCPtr(pSSM, &pVM->patm.s.pGCStateHC->GCCallReturnAddr);
|
---|
504 | AssertRCReturn(rc, rc);
|
---|
505 |
|
---|
506 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uEAX) == sizeof(uint32_t));
|
---|
507 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uEAX);
|
---|
508 | AssertRCReturn(rc, rc);
|
---|
509 |
|
---|
510 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uECX) == sizeof(uint32_t));
|
---|
511 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uECX);
|
---|
512 | AssertRCReturn(rc, rc);
|
---|
513 |
|
---|
514 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uEDI) == sizeof(uint32_t));
|
---|
515 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uEDI);
|
---|
516 | AssertRCReturn(rc, rc);
|
---|
517 |
|
---|
518 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.eFlags) == sizeof(uint32_t));
|
---|
519 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.eFlags);
|
---|
520 | AssertRCReturn(rc, rc);
|
---|
521 |
|
---|
522 | AssertCompile(sizeof(pVM->patm.s.pGCStateHC->Restore.uFlags) == sizeof(uint32_t));
|
---|
523 | rc = SSMR3GetU32(pSSM, &pVM->patm.s.pGCStateHC->Restore.uFlags);
|
---|
524 | AssertRCReturn(rc, rc);
|
---|
525 | }
|
---|
526 | #endif
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Restore PATM stack page
|
---|
530 | */
|
---|
531 | rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
|
---|
532 | AssertRCReturn(rc, rc);
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Load all patches
|
---|
536 | */
|
---|
537 | for (uint32_t i=0;i<patmInfo.savedstate.cPatches;i++)
|
---|
538 | {
|
---|
539 | PATMPATCHREC patch, *pPatchRec;
|
---|
540 |
|
---|
541 | rc = SSMR3GetMem(pSSM, &patch, sizeof(patch));
|
---|
542 | AssertRCReturn(rc, rc);
|
---|
543 |
|
---|
544 | Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
|
---|
545 |
|
---|
546 | rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
|
---|
547 | if (VBOX_FAILURE(rc))
|
---|
548 | {
|
---|
549 | AssertMsgFailed(("Out of memory!!!!\n"));
|
---|
550 | return VERR_NO_MEMORY;
|
---|
551 | }
|
---|
552 | /*
|
---|
553 | * Only restore the patch part of the tree record; not the internal data (except the key of course)
|
---|
554 | */
|
---|
555 | pPatchRec->patch = patch.patch;
|
---|
556 | pPatchRec->Core.Key = patch.Core.Key;
|
---|
557 | pPatchRec->CoreOffset.Key = patch.CoreOffset.Key;
|
---|
558 |
|
---|
559 | bool ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
|
---|
560 | Assert(ret);
|
---|
561 | if (pPatchRec->patch.uState != PATCH_REFUSED)
|
---|
562 | {
|
---|
563 | if (pPatchRec->patch.pPatchBlockOffset)
|
---|
564 | {
|
---|
565 | /* We actually generated code for this patch. */
|
---|
566 | ret = RTAvloU32Insert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
|
---|
567 | AssertMsg(ret, ("Inserting patch %VGv offset %VGv failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
|
---|
568 | }
|
---|
569 | }
|
---|
570 | /* Set to zero as we don't need it anymore. */
|
---|
571 | pPatchRec->patch.pTempInfo = 0;
|
---|
572 |
|
---|
573 | pPatchRec->patch.pPrivInstrHC = 0;
|
---|
574 | /* The GC virtual ptr is fixed, but we must convert it manually again to HC. */
|
---|
575 | rc = PGMPhysGCPtr2HCPtr(pVM, pPatchRec->patch.pPrivInstrGC, (PRTHCPTR)&pPatchRec->patch.pPrivInstrHC);
|
---|
576 | /* Can fail due to page or page table not present. */
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Restore fixup records and correct HC pointers in fixup records
|
---|
580 | */
|
---|
581 | pPatchRec->patch.FixupTree = 0;
|
---|
582 | pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
|
---|
583 | for (int i=0;i<patch.patch.nrFixups;i++)
|
---|
584 | {
|
---|
585 | RELOCREC rec;
|
---|
586 | int32_t offset;
|
---|
587 | RTRCPTR *pFixup;
|
---|
588 |
|
---|
589 | rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
|
---|
590 | AssertRCReturn(rc, rc);
|
---|
591 |
|
---|
592 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
593 | offset = (int32_t)rec.pRelocPos;
|
---|
594 | /* Convert to HC pointer again. */
|
---|
595 | PATM_ADD_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
|
---|
596 | pFixup = (RTRCPTR *)rec.pRelocPos;
|
---|
597 |
|
---|
598 | /* Correct absolute fixups that refer to PATM structures in the hypervisor region (their addresses might have changed). */
|
---|
599 | if (rec.uType == FIXUP_ABSOLUTE)
|
---|
600 | patmCorrectAbsoluteFixup(pVM, patmInfo, offset, pFixup);
|
---|
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 | /*
|
---|
636 | * Correct absolute fixups in the global patch. (helper functions)
|
---|
637 | * Bit of a mess. Uses the new patch record, but restored patch functions.
|
---|
638 | */
|
---|
639 | PRELOCREC pRec = 0;
|
---|
640 | AVLPVKEY key = 0;
|
---|
641 |
|
---|
642 | while (true)
|
---|
643 | {
|
---|
644 | int32_t offset;
|
---|
645 | RTRCPTR *pFixup;
|
---|
646 |
|
---|
647 | /* Get the record that's closest from above */
|
---|
648 | pRec = (PRELOCREC)RTAvlPVGetBestFit(&pVM->patm.s.pGlobalPatchRec->patch.FixupTree, key, true);
|
---|
649 | if (pRec == 0)
|
---|
650 | break;
|
---|
651 |
|
---|
652 | key = (AVLPVKEY)(pRec->pRelocPos + 1); /* search for the next record during the next round. */
|
---|
653 |
|
---|
654 | /* rec.pRelocPos now contains the relative position inside the hypervisor area. */
|
---|
655 | offset = (int32_t)(pRec->pRelocPos - pVM->patm.s.pPatchMemHC);
|
---|
656 | pFixup = (RTRCPTR *)pRec->pRelocPos;
|
---|
657 |
|
---|
658 | /* Correct absolute fixups that refer to PATM structures in the hypervisor region (their addresses might have changed). */
|
---|
659 | if (pRec->uType == FIXUP_ABSOLUTE)
|
---|
660 | patmCorrectAbsoluteFixup(pVM, patmInfo, offset, pFixup);
|
---|
661 | }
|
---|
662 |
|
---|
663 | #ifdef VBOX_WITH_STATISTICS
|
---|
664 | /*
|
---|
665 | * Restore relevant old statistics
|
---|
666 | */
|
---|
667 | pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
|
---|
668 | pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
|
---|
669 | pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
|
---|
670 | pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
|
---|
671 | #endif
|
---|
672 | return VINF_SUCCESS;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Correct absolute fixups to predefined hypervisor PATM regions. (their addresses might have changed)
|
---|
677 | *
|
---|
678 | * @returns VBox status code.
|
---|
679 | * @param pVM VM Handle.
|
---|
680 | * @param patmInfo Saved PATM structure
|
---|
681 | * @param offset Offset of referenced data/code
|
---|
682 | * @param pFixup Fixup address
|
---|
683 | */
|
---|
684 | static void patmCorrectAbsoluteFixup(PVM pVM, PATM &patmInfo, int32_t offset, RTRCPTR *pFixup)
|
---|
685 | {
|
---|
686 | if ( patmInfo.pPatchMemGC + offset >= patmInfo.pGCStateGC
|
---|
687 | && patmInfo.pPatchMemGC + offset < patmInfo.pGCStateGC + sizeof(PATMGCSTATE))
|
---|
688 | {
|
---|
689 | LogFlow(("Changing absolute GCState from %VRv (%VRv) to %VRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC));
|
---|
690 | *pFixup = (*pFixup - patmInfo.pGCStateGC) + pVM->patm.s.pGCStateGC;
|
---|
691 | }
|
---|
692 | else
|
---|
693 | if ( patmInfo.pPatchMemGC + offset >= patmInfo.pCPUMCtxGC
|
---|
694 | && patmInfo.pPatchMemGC + offset < patmInfo.pCPUMCtxGC + sizeof(CPUMCTX))
|
---|
695 | {
|
---|
696 | LogFlow(("Changing absolute CPUMCTX from %VRv (%VRv) to %VRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC));
|
---|
697 | *pFixup = (*pFixup - patmInfo.pCPUMCtxGC) + pVM->patm.s.pCPUMCtxGC;
|
---|
698 | }
|
---|
699 | else
|
---|
700 | if ( patmInfo.pPatchMemGC + offset >= patmInfo.pStatsGC
|
---|
701 | && patmInfo.pPatchMemGC + offset < patmInfo.pStatsGC + sizeof(CPUMCTX))
|
---|
702 | {
|
---|
703 | LogFlow(("Changing absolute Stats from %VRv (%VRv) to %VRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC));
|
---|
704 | *pFixup = (*pFixup - patmInfo.pStatsGC) + pVM->patm.s.pStatsGC;
|
---|
705 | }
|
---|
706 | else
|
---|
707 | if ( patmInfo.pPatchMemGC + offset >= patmInfo.pGCStackGC
|
---|
708 | && patmInfo.pPatchMemGC + offset < patmInfo.pGCStackGC + PATM_STACK_TOTAL_SIZE)
|
---|
709 | {
|
---|
710 | LogFlow(("Changing absolute Stats from %VRv (%VRv) to %VRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC));
|
---|
711 | *pFixup = (*pFixup - patmInfo.pGCStackGC) + pVM->patm.s.pGCStackGC;
|
---|
712 | }
|
---|
713 | else
|
---|
714 | if ( patmInfo.pPatchMemGC + offset >= patmInfo.pPatchMemGC
|
---|
715 | && patmInfo.pPatchMemGC + offset < patmInfo.pPatchMemGC + patmInfo.cbPatchMem)
|
---|
716 | {
|
---|
717 | LogFlow(("Changing absolute Stats from %VRv (%VRv) to %VRv\n", patmInfo.pPatchMemGC + offset, *pFixup, (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC));
|
---|
718 | *pFixup = (*pFixup - patmInfo.pPatchMemGC) + pVM->patm.s.pPatchMemGC;
|
---|
719 | }
|
---|
720 | else
|
---|
721 | AssertFailed();
|
---|
722 | }
|
---|