VirtualBox

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

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

Preparation for new code page detection.

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