VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/PATMSSM.cpp@ 9026

Last change on this file since 9026 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.4 KB
Line 
1/* $Id: PATMSSM.cpp 8155 2008-04-18 15:16:47Z 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 */
68static 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 */
83static 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 RTAvloGCPtrDoWithAll
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 */
99static DECLCALLBACK(int) patmCountPatch(PAVLOGCPTRNODECORE 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 */
114static 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 */
136static 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 RTAvloGCPtrDoWithAll
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 */
162static DECLCALLBACK(int) patmSavePatchState(PAVLOGCPTRNODECORE 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 */
214DECLCALLBACK(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 RTAvloGCPtrDoWithAll(&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 = RTAvloGCPtrDoWithAll(&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 */
279DECLCALLBACK(int) patmr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
280{
281 PATM patmInfo;
282 int rc;
283
284 if (u32Version != PATM_SSM_VERSION)
285 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
286
287 pVM->patm.s.savedstate.pSSM = pSSM;
288
289 /*
290 * Restore PATM structure
291 */
292 rc = SSMR3GetMem(pSSM, &patmInfo, sizeof(patmInfo));
293 AssertRCReturn(rc, rc);
294
295 /** @todo this restriction could be removed as we relocate when loading the saved state,.. */
296 if ( pVM->patm.s.pGCStateGC != patmInfo.pGCStateGC
297 || pVM->patm.s.pCPUMCtxGC != patmInfo.pCPUMCtxGC
298 || pVM->patm.s.pStatsGC != patmInfo.pStatsGC)
299 {
300 if (SSMR3HandleGetAfter(pSSM) == SSMAFTER_DEBUG_IT) /* hack for x86 / amd64 mix. */
301 return VINF_SUCCESS;
302 AssertMsgFailed(("GC state, stat or cpum ptrs don't match!!!\n"));
303 return VERR_SSM_INVALID_STATE;
304 }
305
306 /* Relative calls are made to the helper functions. Therefor their location must not change! */
307 if ( pVM->patm.s.pfnHelperCallGC != patmInfo.pfnHelperCallGC
308 || pVM->patm.s.pfnHelperRetGC != patmInfo.pfnHelperRetGC
309 || pVM->patm.s.pfnHelperJumpGC != patmInfo.pfnHelperJumpGC
310 || pVM->patm.s.pfnHelperIretGC != patmInfo.pfnHelperIretGC)
311 {
312 AssertMsgFailed(("Helper function ptrs don't match!!!\n"));
313 return VERR_SSM_INVALID_STATE;
314 }
315
316 if ( pVM->patm.s.pPatchMemGC != patmInfo.pPatchMemGC
317 || pVM->patm.s.cbPatchMem != patmInfo.cbPatchMem)
318 {
319 AssertMsgFailed(("Patch memory ptrs and/or sizes don't match!!!\n"));
320 return VERR_SSM_INVALID_STATE;
321 }
322 pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
323 pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
324 pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
325
326 /* Lowest and highest patched instruction */
327 pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
328 pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
329
330 /* Sysenter handlers */
331 pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
332 pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
333 pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
334
335 Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
336
337 /** @note patch statistics are not restored. */
338
339 /*
340 * Restore patch memory contents
341 */
342 rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
343 AssertRCReturn(rc, rc);
344
345 /*
346 * Restore GC state memory
347 */
348 if (pVM->patm.s.pGCStateGC != patmInfo.pGCStateGC)
349 {
350 AssertMsgFailed(("GC patch state ptrs don't match!!!\n"));
351 return VERR_SSM_INVALID_STATE;
352 }
353 rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
354 AssertRCReturn(rc, rc);
355
356 /*
357 * Restore PATM stack page
358 */
359 if (pVM->patm.s.pGCStackGC != patmInfo.pGCStackGC)
360 {
361 AssertMsgFailed(("GC patch stack ptrs don't match!!!\n"));
362 return VERR_SSM_INVALID_STATE;
363 }
364 rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
365 AssertRCReturn(rc, rc);
366
367 /*
368 * Load all patches
369 */
370 for (uint32_t i=0;i<patmInfo.savedstate.cPatches;i++)
371 {
372 PATMPATCHREC patch, *pPatchRec;
373
374 rc = SSMR3GetMem(pSSM, &patch, sizeof(patch));
375 AssertRCReturn(rc, rc);
376
377 Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
378
379 rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
380 if (VBOX_FAILURE(rc))
381 {
382 AssertMsgFailed(("Out of memory!!!!\n"));
383 return VERR_NO_MEMORY;
384 }
385 /*
386 * Only restore the patch part of the tree record; not the internal data (except the key of course)
387 */
388 pPatchRec->patch = patch.patch;
389 pPatchRec->Core.Key = patch.Core.Key;
390 pPatchRec->CoreOffset.Key = patch.CoreOffset.Key;
391
392 bool ret = RTAvloGCPtrInsert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
393 Assert(ret);
394 if (pPatchRec->patch.uState != PATCH_REFUSED)
395 {
396 if (pPatchRec->patch.pPatchBlockOffset)
397 {
398 /* We actually generated code for this patch. */
399 ret = RTAvloGCPtrInsert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
400 AssertMsg(ret, ("Inserting patch %VGv offset %VGv failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
401 }
402 }
403 /* Set to zero as we don't need it anymore. */
404 pPatchRec->patch.pTempInfo = 0;
405
406 pPatchRec->patch.pPrivInstrHC = 0;
407 /* The GC virtual ptr is fixed, but we must convert it manually again to HC. */
408 rc = PGMPhysGCPtr2HCPtr(pVM, pPatchRec->patch.pPrivInstrGC, (PRTHCPTR)&pPatchRec->patch.pPrivInstrHC);
409 /* Can fail due to page or page table not present. */
410
411 /*
412 * Restore fixup records and correct HC pointers in fixup records
413 */
414 pPatchRec->patch.FixupTree = 0;
415 pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
416 for (int i=0;i<patch.patch.nrFixups;i++)
417 {
418 RELOCREC rec;
419
420 rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
421 AssertRCReturn(rc, rc);
422 PATM_ADD_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
423
424 rc = patmPatchAddReloc32(pVM, &pPatchRec->patch, rec.pRelocPos, rec.uType, rec.pSource, rec.pDest);
425 AssertRCReturn(rc, rc);
426 }
427
428 /* And all patch to guest lookup records */
429 Assert(pPatchRec->patch.nrPatch2GuestRecs || pPatchRec->patch.uState == PATCH_REFUSED || (pPatchRec->patch.flags & (PATMFL_SYSENTER_XP | PATMFL_IDTHANDLER | PATMFL_TRAPHANDLER | PATMFL_INT3_REPLACEMENT)));
430
431 pPatchRec->patch.Patch2GuestAddrTree = 0;
432 pPatchRec->patch.Guest2PatchAddrTree = 0;
433 if (pPatchRec->patch.nrPatch2GuestRecs)
434 {
435 RECPATCHTOGUEST rec;
436 uint32_t nrPatch2GuestRecs = pPatchRec->patch.nrPatch2GuestRecs;
437
438 pPatchRec->patch.nrPatch2GuestRecs = 0; /* incremented by patmr3AddP2GLookupRecord */
439 for (uint32_t i=0;i<nrPatch2GuestRecs;i++)
440 {
441 rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
442 AssertRCReturn(rc, rc);
443
444 patmr3AddP2GLookupRecord(pVM, &pPatchRec->patch, (uintptr_t)rec.Core.Key + pVM->patm.s.pPatchMemHC, rec.pOrgInstrGC, rec.enmType, rec.fDirty);
445 }
446 Assert(pPatchRec->patch.Patch2GuestAddrTree);
447 }
448
449 if (pPatchRec->patch.flags & PATMFL_CODE_MONITORED)
450 {
451 /* Insert the guest page lookup records (for detection self-modifying code) */
452 rc = patmInsertPatchPages(pVM, &pPatchRec->patch);
453 AssertRCReturn(rc, rc);
454 }
455 }
456
457#ifdef VBOX_WITH_STATISTICS
458 /*
459 * Restore relevant old statistics
460 */
461 pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
462 pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
463 pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
464 pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
465#endif
466 return VINF_SUCCESS;
467}
468
469
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