VirtualBox

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

Last change on this file since 1122 was 302, checked in by vboxsync, 18 years ago

Handle iret to ring 0 code with IF cleared. Warning: possibly dangerous change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
1/* $Id: PATMSSM.cpp 302 2007-01-25 14:48:55Z 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 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
15 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
16 * distribution. VirtualBox OSE is distributed in the hope that it will
17 * be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * If you received this file as part of a commercial VirtualBox
20 * distribution, then only the terms of your commercial VirtualBox
21 * license agreement apply instead of the previous paragraph.
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 AssertMsgFailed(("GC state, stat or cpum ptrs don't match!!!\n"));
301 return VERR_SSM_INVALID_STATE;
302 }
303
304 /* Relative calls are made to the helper functions. Therefor their location must not change! */
305 if ( pVM->patm.s.pfnHelperCallGC != patmInfo.pfnHelperCallGC
306 || pVM->patm.s.pfnHelperRetGC != patmInfo.pfnHelperRetGC
307 || pVM->patm.s.pfnHelperJumpGC != patmInfo.pfnHelperJumpGC
308 || pVM->patm.s.pfnHelperIretGC != patmInfo.pfnHelperIretGC)
309 {
310 AssertMsgFailed(("Helper function ptrs don't match!!!\n"));
311 return VERR_SSM_INVALID_STATE;
312 }
313
314 if ( pVM->patm.s.pPatchMemGC != patmInfo.pPatchMemGC
315 || pVM->patm.s.cbPatchMem != patmInfo.cbPatchMem)
316 {
317 AssertMsgFailed(("Patch memory ptrs and/or sizes don't match!!!\n"));
318 return VERR_SSM_INVALID_STATE;
319 }
320 pVM->patm.s.offPatchMem = patmInfo.offPatchMem;
321 pVM->patm.s.deltaReloc = patmInfo.deltaReloc;
322 pVM->patm.s.uCurrentPatchIdx = patmInfo.uCurrentPatchIdx;
323
324 /* Lowest and highest patched instruction */
325 pVM->patm.s.pPatchedInstrGCLowest = patmInfo.pPatchedInstrGCLowest;
326 pVM->patm.s.pPatchedInstrGCHighest = patmInfo.pPatchedInstrGCHighest;
327
328 /* Sysenter handlers */
329 pVM->patm.s.pfnSysEnterGC = patmInfo.pfnSysEnterGC;
330 pVM->patm.s.pfnSysEnterPatchGC = patmInfo.pfnSysEnterPatchGC;
331 pVM->patm.s.uSysEnterPatchIdx = patmInfo.uSysEnterPatchIdx;
332
333 Assert(patmInfo.ulCallDepth == 0 && pVM->patm.s.ulCallDepth == 0);
334
335 /** @note patch statistics are not restored. */
336
337 /*
338 * Restore patch memory contents
339 */
340 rc = SSMR3GetMem(pSSM, pVM->patm.s.pPatchMemHC, pVM->patm.s.cbPatchMem);
341 AssertRCReturn(rc, rc);
342
343 /*
344 * Restore GC state memory
345 */
346 if (pVM->patm.s.pGCStateGC != patmInfo.pGCStateGC)
347 {
348 AssertMsgFailed(("GC patch state ptrs don't match!!!\n"));
349 return VERR_SSM_INVALID_STATE;
350 }
351 rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStateHC, sizeof(PATMGCSTATE));
352 AssertRCReturn(rc, rc);
353
354 /*
355 * Restore PATM stack page
356 */
357 if (pVM->patm.s.pGCStackGC != patmInfo.pGCStackGC)
358 {
359 AssertMsgFailed(("GC patch stack ptrs don't match!!!\n"));
360 return VERR_SSM_INVALID_STATE;
361 }
362 rc = SSMR3GetMem(pSSM, pVM->patm.s.pGCStackHC, PATM_STACK_TOTAL_SIZE);
363 AssertRCReturn(rc, rc);
364
365 /*
366 * Load all patches
367 */
368 for (uint32_t i=0;i<patmInfo.savedstate.cPatches;i++)
369 {
370 PATMPATCHREC patch, *pPatchRec;
371
372 rc = SSMR3GetMem(pSSM, &patch, sizeof(patch));
373 AssertRCReturn(rc, rc);
374
375 Assert(!(patch.patch.flags & PATMFL_GLOBAL_FUNCTIONS));
376
377 rc = MMHyperAlloc(pVM, sizeof(PATMPATCHREC), 0, MM_TAG_PATM_PATCH, (void **)&pPatchRec);
378 if (VBOX_FAILURE(rc))
379 {
380 AssertMsgFailed(("Out of memory!!!!\n"));
381 return VERR_NO_MEMORY;
382 }
383 /*
384 * Only restore the patch part of the tree record; not the internal data (except the key of course)
385 */
386 pPatchRec->patch = patch.patch;
387 pPatchRec->Core.Key = patch.Core.Key;
388 pPatchRec->CoreOffset.Key = patch.CoreOffset.Key;
389
390 bool ret = RTAvloGCPtrInsert(&pVM->patm.s.PatchLookupTreeHC->PatchTree, &pPatchRec->Core);
391 Assert(ret);
392 if (pPatchRec->patch.uState != PATCH_REFUSED)
393 {
394 if (pPatchRec->patch.pPatchBlockOffset)
395 {
396 /* We actually generated code for this patch. */
397 ret = RTAvloGCPtrInsert(&pVM->patm.s.PatchLookupTreeHC->PatchTreeByPatchAddr, &pPatchRec->CoreOffset);
398 AssertMsg(ret, ("Inserting patch %VGv offset %VGv failed!!\n", pPatchRec->patch.pPrivInstrGC, pPatchRec->CoreOffset.Key));
399 }
400 }
401 /* Set to zero as we don't need it anymore. */
402 pPatchRec->patch.pTempInfo = 0;
403
404 pPatchRec->patch.pPrivInstrHC = 0;
405 /* The GC virtual ptr is fixed, but we must convert it manually again to HC. */
406 rc = PGMPhysGCPtr2HCPtr(pVM, pPatchRec->patch.pPrivInstrGC, (PRTHCPTR)&pPatchRec->patch.pPrivInstrHC);
407 /* Can fail due to page or page table not present. */
408
409 /*
410 * Restore fixup records and correct HC pointers in fixup records
411 */
412 pPatchRec->patch.FixupTree = 0;
413 pPatchRec->patch.nrFixups = 0; /* increased by patmPatchAddReloc32 */
414 for (int i=0;i<patch.patch.nrFixups;i++)
415 {
416 RELOCREC rec;
417
418 rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
419 AssertRCReturn(rc, rc);
420 PATM_ADD_PTR(rec.pRelocPos, pVM->patm.s.pPatchMemHC);
421
422 rc = patmPatchAddReloc32(pVM, &pPatchRec->patch, rec.pRelocPos, rec.uType, rec.pSource, rec.pDest);
423 AssertRCReturn(rc, rc);
424 }
425
426 /* And all patch to guest lookup records */
427 Assert(pPatchRec->patch.nrPatch2GuestRecs || pPatchRec->patch.uState == PATCH_REFUSED || (pPatchRec->patch.flags & (PATMFL_SYSENTER_XP | PATMFL_IDTHANDLER | PATMFL_TRAPHANDLER | PATMFL_INT3_REPLACEMENT)));
428
429 pPatchRec->patch.Patch2GuestAddrTree = 0;
430 pPatchRec->patch.Guest2PatchAddrTree = 0;
431 if (pPatchRec->patch.nrPatch2GuestRecs)
432 {
433 RECPATCHTOGUEST rec;
434 uint32_t nrPatch2GuestRecs = pPatchRec->patch.nrPatch2GuestRecs;
435
436 pPatchRec->patch.nrPatch2GuestRecs = 0; /* incremented by patmr3AddP2GLookupRecord */
437 for (uint32_t i=0;i<nrPatch2GuestRecs;i++)
438 {
439 rc = SSMR3GetMem(pSSM, &rec, sizeof(rec));
440 AssertRCReturn(rc, rc);
441
442 patmr3AddP2GLookupRecord(pVM, &pPatchRec->patch, (uintptr_t)rec.Core.Key + pVM->patm.s.pPatchMemHC, rec.pOrgInstrGC, rec.enmType, rec.fDirty);
443 }
444 Assert(pPatchRec->patch.Patch2GuestAddrTree);
445 }
446
447 if (pPatchRec->patch.flags & PATMFL_CODE_MONITORED)
448 {
449 /* Insert the guest page lookup records (for detection self-modifying code) */
450 rc = patmInsertPatchPages(pVM, &pPatchRec->patch);
451 AssertRCReturn(rc, rc);
452 }
453 }
454
455#ifdef VBOX_WITH_STATISTICS
456 /*
457 * Restore relevant old statistics
458 */
459 pVM->patm.s.StatDisabled = patmInfo.StatDisabled;
460 pVM->patm.s.StatUnusable = patmInfo.StatUnusable;
461 pVM->patm.s.StatEnabled = patmInfo.StatEnabled;
462 pVM->patm.s.StatInstalled = patmInfo.StatInstalled;
463#endif
464 return VINF_SUCCESS;
465}
466
467
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