VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/VMMGC/CSAMGC.cpp@ 21116

Last change on this file since 21116 was 19141, checked in by vboxsync, 16 years ago

Action flags breakup.
Fixed PGM saved state loading of 2.2.2 images.
Reduced hacks in PATM state loading (fixups).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/* $Id: CSAMGC.cpp 19141 2009-04-23 13:52:18Z vboxsync $ */
2/** @file
3 * CSAM - Guest OS Code Scanning and Analysis Manager - Any Context
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_CSAM
27#include <VBox/cpum.h>
28#include <VBox/stam.h>
29#include <VBox/patm.h>
30#include <VBox/csam.h>
31#include <VBox/pgm.h>
32#include <VBox/mm.h>
33#include <VBox/sup.h>
34#include <VBox/mm.h>
35#include <VBox/rem.h>
36#include <VBox/param.h>
37#include <iprt/avl.h>
38#include "CSAMInternal.h"
39#include <VBox/vm.h>
40#include <VBox/dbg.h>
41#include <VBox/err.h>
42#include <VBox/log.h>
43#include <iprt/assert.h>
44#include <VBox/dis.h>
45#include <VBox/disopcode.h>
46#include <iprt/string.h>
47#include <iprt/asm.h>
48
49/**
50 * #PF Handler callback for virtual access handler ranges. (CSAM self-modifying code monitor)
51 *
52 * Important to realize that a physical page in a range can have aliases, and
53 * for ALL and WRITE handlers these will also trigger.
54 *
55 * @returns VBox status code (appropriate for GC return).
56 * @param pVM VM Handle.
57 * @param uErrorCode CPU Error code.
58 * @param pRegFrame Trap register frame.
59 * @param pvFault The fault address (cr2).
60 * @param pvRange The base address of the handled virtual range.
61 * @param offRange The offset of the access into this range.
62 * (If it's a EIP range this's the EIP, if not it's pvFault.)
63 */
64VMMRCDECL(int) CSAMGCCodePageWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange)
65{
66 PPATMGCSTATE pPATMGCState;
67 bool fPatchCode = PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip);
68 int rc;
69 PVMCPU pVCpu = VMMGetCpu0(pVM);
70
71 Assert(pVM->csam.s.cDirtyPages < CSAM_MAX_DIRTY_PAGES);
72
73 /* Flush the recompilers translation block cache as the guest seems to be modifying instructions. */
74 REMFlushTBs(pVM);
75
76 pPATMGCState = PATMQueryGCState(pVM);
77 Assert(pPATMGCState);
78
79 Assert(pPATMGCState->fPIF || fPatchCode);
80 /** When patch code is executing instructions that must complete, then we must *never* interrupt it. */
81 if (!pPATMGCState->fPIF && fPatchCode)
82 {
83 Log(("CSAMGCCodePageWriteHandler: fPIF=0 -> stack fault in patch generated code at %08RX32!\n", pRegFrame->eip));
84 /** @note there are cases when pages previously used for code are now used for stack; patch generated code will fault (pushf))
85 * Just make the page r/w and continue.
86 */
87 /*
88 * Make this particular page R/W.
89 */
90 int rc = PGMShwModifyPage(pVCpu, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
91 AssertMsgRC(rc, ("PGMShwModifyPage -> rc=%Rrc\n", rc));
92 ASMInvalidatePage((void *)pvFault);
93 return VINF_SUCCESS;
94 }
95
96 uint32_t cpl;
97
98 if (pRegFrame->eflags.Bits.u1VM)
99 cpl = 3;
100 else
101 cpl = (pRegFrame->ss & X86_SEL_RPL);
102
103 Log(("CSAMGCCodePageWriteHandler: code page write at %RGv original address %RGv (cpl=%d)\n", pvFault, (RTGCUINTPTR)pvRange + offRange, cpl));
104
105 /* If user code is modifying one of our monitored pages, then we can safely make it r/w as it's no longer being used for supervisor code. */
106 if (cpl != 3)
107 {
108 rc = PATMGCHandleWriteToPatchPage(pVM, pRegFrame, (RTRCPTR)((RTRCUINTPTR)pvRange + offRange), 4 /** @todo */);
109 if (rc == VINF_SUCCESS)
110 return rc;
111 if (rc == VINF_EM_RAW_EMULATE_INSTR)
112 {
113 STAM_COUNTER_INC(&pVM->csam.s.StatDangerousWrite);
114 return VINF_EM_RAW_EMULATE_INSTR;
115 }
116 Assert(rc == VERR_PATCH_NOT_FOUND);
117 }
118
119 VMCPU_FF_SET(pVCpu, VMCPU_FF_CSAM_PENDING_ACTION);
120
121 /* Note that pvFault might be a different address in case of aliases. So use pvRange + offset instead!. */
122 pVM->csam.s.pvDirtyBasePage[pVM->csam.s.cDirtyPages] = (RTRCPTR)((RTGCUINTPTR)pvRange + offRange);
123 pVM->csam.s.pvDirtyFaultPage[pVM->csam.s.cDirtyPages] = (RTRCPTR)((RTGCUINTPTR)pvRange + offRange);
124 if (++pVM->csam.s.cDirtyPages == CSAM_MAX_DIRTY_PAGES)
125 return VINF_CSAM_PENDING_ACTION;
126
127 /*
128 * Make this particular page R/W. The VM_FF_CSAM_FLUSH_DIRTY_PAGE handler will reset it to readonly again.
129 */
130 Log(("CSAMGCCodePageWriteHandler: enabled r/w for page %RGv\n", pvFault));
131 rc = PGMShwModifyPage(pVCpu, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
132 AssertMsgRC(rc, ("PGMShwModifyPage -> rc=%Rrc\n", rc));
133 ASMInvalidatePage((void *)pvFault);
134
135 STAM_COUNTER_INC(&pVM->csam.s.StatCodePageModified);
136 return VINF_SUCCESS;
137}
138
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