VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/* $Id: CSAMGC.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * CSAM - Guest OS Code Scanning and Analysis Manager - Any Context
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_CSAM
23#include <VBox/cpum.h>
24#include <VBox/stam.h>
25#include <VBox/patm.h>
26#include <VBox/csam.h>
27#include <VBox/pgm.h>
28#include <VBox/mm.h>
29#include <VBox/sup.h>
30#include <VBox/mm.h>
31#include <VBox/param.h>
32#include <iprt/avl.h>
33#include "CSAMInternal.h"
34#include <VBox/vm.h>
35#include <VBox/dbg.h>
36#include <VBox/err.h>
37#include <VBox/log.h>
38#include <iprt/assert.h>
39#include <VBox/dis.h>
40#include <VBox/disopcode.h>
41#include <iprt/string.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <iprt/asm.h>
45
46/**
47 * #PF Handler callback for virtual access handler ranges. (CSAM self-modifying code monitor)
48 *
49 * Important to realize that a physical page in a range can have aliases, and
50 * for ALL and WRITE handlers these will also trigger.
51 *
52 * @returns VBox status code (appropriate for GC return).
53 * @param pVM VM Handle.
54 * @param uErrorCode CPU Error code.
55 * @param pRegFrame Trap register frame.
56 * @param pvFault The fault address (cr2).
57 * @param pvRange The base address of the handled virtual range.
58 * @param offRange The offset of the access into this range.
59 * (If it's a EIP range this's the EIP, if not it's pvFault.)
60 */
61CSAMGCDECL(int) CSAMGCCodePageWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange)
62{
63 PPATMGCSTATE pPATMGCState;
64 bool fPatchCode = PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip);
65 int rc;
66
67 Assert(pVM->csam.s.cDirtyPages < CSAM_MAX_DIRTY_PAGES);
68
69 pPATMGCState = PATMQueryGCState(pVM);
70 Assert(pPATMGCState);
71
72 Assert(pPATMGCState->fPIF || fPatchCode);
73 /** When patch code is executing instructions that must complete, then we must *never* interrupt it. */
74 if (!pPATMGCState->fPIF && fPatchCode)
75 {
76 Log(("CSAMGCCodePageWriteHandler: fPIF=0 -> stack fault in patch generated code at %VGv!\n", pRegFrame->eip));
77 /** @note there are cases when pages previously used for code are now used for stack; patch generated code will fault (pushf))
78 * Just make the page r/w and continue.
79 */
80 /*
81 * Make this particular page R/W.
82 */
83 int rc = PGMShwModifyPage(pVM, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
84 AssertMsgRC(rc, ("PGMShwModifyPage -> rc=%Vrc\n", rc));
85 ASMInvalidatePage(pvFault);
86 return VINF_SUCCESS;
87 }
88
89 uint32_t cpl;
90
91 if (pRegFrame->eflags.Bits.u1VM)
92 cpl = 3;
93 else
94 cpl = (pRegFrame->ss & X86_SEL_RPL);
95
96 Log(("CSAMGCCodePageWriteHandler: code page write at %VGv original address %VGv (cpl=%d)\n", pvFault, (RTGCUINTPTR)pvRange + offRange, cpl));
97
98 /* 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. */
99 if (cpl != 3)
100 {
101 rc = PATMGCHandleWriteToPatchPage(pVM, pRegFrame, (RTGCPTR)((RTGCUINTPTR)pvRange + offRange), 4 /** @todo */);
102 if (rc == VINF_SUCCESS)
103 return rc;
104 if (rc == VINF_EM_RAW_EMULATE_INSTR)
105 {
106 STAM_COUNTER_INC(&pVM->csam.s.StatDangerousWrite);
107 return VINF_EM_RAW_EMULATE_INSTR;
108 }
109 Assert(rc == VERR_PATCH_NOT_FOUND);
110 }
111
112 VM_FF_SET(pVM, VM_FF_CSAM_PENDING_ACTION);
113
114 /* Note that pvFault might be a different address in case of aliases. So use pvRange + offset instead!. */
115 pVM->csam.s.pvDirtyBasePage[pVM->csam.s.cDirtyPages] = (RTGCPTR)((RTGCUINTPTR)pvRange + offRange);
116 pVM->csam.s.pvDirtyFaultPage[pVM->csam.s.cDirtyPages] = (RTGCPTR)((RTGCUINTPTR)pvRange + offRange);
117 if (++pVM->csam.s.cDirtyPages == CSAM_MAX_DIRTY_PAGES)
118 return VINF_CSAM_PENDING_ACTION;
119
120 /*
121 * Make this particular page R/W. The VM_FF_CSAM_FLUSH_DIRTY_PAGE handler will reset it to readonly again.
122 */
123 Log(("CSAMGCCodePageWriteHandler: enabled r/w for page %VGv\n", pvFault));
124 rc = PGMShwModifyPage(pVM, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
125 AssertMsgRC(rc, ("PGMShwModifyPage -> rc=%Vrc\n", rc));
126 ASMInvalidatePage(pvFault);
127
128 STAM_COUNTER_INC(&pVM->csam.s.StatCodePageModified);
129 return VINF_SUCCESS;
130}
131
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