1 | /* $Id: CSAMAll.cpp 8155 2008-04-18 15:16:47Z 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/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 <iprt/asm.h>
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Check if this page needs to be analysed by CSAM
|
---|
50 | *
|
---|
51 | * @returns VBox status code
|
---|
52 | * @param pVM The VM to operate on.
|
---|
53 | * @param pvFault Fault address
|
---|
54 | */
|
---|
55 | CSAMDECL(int) CSAMExecFault(PVM pVM, RTGCPTR pvFault)
|
---|
56 | {
|
---|
57 | if(!CSAMIsEnabled(pVM))
|
---|
58 | return VINF_SUCCESS;
|
---|
59 |
|
---|
60 | LogFlow(("CSAMGCExecFault: for page %08X scanned=%d\n", pvFault, CSAMIsPageScanned(pVM, pvFault)));
|
---|
61 |
|
---|
62 | if(CSAMIsPageScanned(pVM, pvFault))
|
---|
63 | {
|
---|
64 | // Already checked!
|
---|
65 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesGC, 1);
|
---|
66 | return VINF_SUCCESS;
|
---|
67 | }
|
---|
68 |
|
---|
69 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrTraps, 1);
|
---|
70 | VM_FF_SET(pVM, VM_FF_CSAM_SCAN_PAGE);
|
---|
71 | return VINF_CSAM_PENDING_ACTION;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Check if this page was previously scanned by CSAM
|
---|
77 | *
|
---|
78 | * @returns true -> scanned, false -> not scanned
|
---|
79 | * @param pVM The VM to operate on.
|
---|
80 | * @param pPage GC page address
|
---|
81 | */
|
---|
82 | CSAMDECL(bool) CSAMIsPageScanned(PVM pVM, RTGCPTR pPage)
|
---|
83 | {
|
---|
84 | int pgdir, bit;
|
---|
85 | uintptr_t page;
|
---|
86 |
|
---|
87 | page = (uintptr_t)pPage;
|
---|
88 | pgdir = page >> X86_PAGE_4M_SHIFT;
|
---|
89 | bit = (page & X86_PAGE_4M_OFFSET_MASK) >> X86_PAGE_4K_SHIFT;
|
---|
90 |
|
---|
91 | Assert(pgdir < CSAM_PGDIRBMP_CHUNKS);
|
---|
92 | Assert(bit < PAGE_SIZE);
|
---|
93 |
|
---|
94 | return pVM->csam.s.CTXSUFF(pPDBitmap)[pgdir] && ASMBitTest(pVM->csam.s.CTXSUFF(pPDBitmap)[pgdir], bit);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Mark a page as scanned/not scanned
|
---|
101 | *
|
---|
102 | * @note: we always mark it as scanned, even if we haven't completely done so
|
---|
103 | *
|
---|
104 | * @returns VBox status code.
|
---|
105 | * @param pVM The VM to operate on.
|
---|
106 | * @param pPage GC page address (not necessarily aligned)
|
---|
107 | * @param fScanned Mark as scanned or not scanned
|
---|
108 | *
|
---|
109 | */
|
---|
110 | CSAMDECL(int) CSAMMarkPage(PVM pVM, RTGCPTR pPage, bool fScanned)
|
---|
111 | {
|
---|
112 | int pgdir, bit;
|
---|
113 | uintptr_t page;
|
---|
114 |
|
---|
115 | #ifdef LOG_ENABLED
|
---|
116 | if (fScanned && !CSAMIsPageScanned(pVM, pPage))
|
---|
117 | Log(("CSAMMarkPage %VGv\n", pPage));
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | if(!CSAMIsEnabled(pVM))
|
---|
121 | return VINF_SUCCESS;
|
---|
122 |
|
---|
123 | page = (uintptr_t)pPage;
|
---|
124 | pgdir = page >> X86_PAGE_4M_SHIFT;
|
---|
125 | bit = (page & X86_PAGE_4M_OFFSET_MASK) >> X86_PAGE_4K_SHIFT;
|
---|
126 |
|
---|
127 | Assert(pgdir < CSAM_PGDIRBMP_CHUNKS);
|
---|
128 | Assert(bit < PAGE_SIZE);
|
---|
129 |
|
---|
130 | if(!CTXSUFF(pVM->csam.s.pPDBitmap)[pgdir])
|
---|
131 | {
|
---|
132 | STAM_COUNTER_INC(&pVM->csam.s.StatBitmapAlloc);
|
---|
133 | int rc = MMHyperAlloc(pVM, CSAM_PAGE_BITMAP_SIZE, 0, MM_TAG_CSAM, (void **)&pVM->csam.s.CTXSUFF(pPDBitmap)[pgdir]);
|
---|
134 | if (VBOX_FAILURE(rc))
|
---|
135 | {
|
---|
136 | Log(("MMR3HyperAlloc failed with %d\n", rc));
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 | #ifdef IN_GC
|
---|
140 | pVM->csam.s.pPDHCBitmapGC[pgdir] = MMHyperGC2HC(pVM, pVM->csam.s.pPDBitmapGC[pgdir]);
|
---|
141 | if (!pVM->csam.s.pPDHCBitmapGC[pgdir])
|
---|
142 | {
|
---|
143 | Log(("MMHyperHC2GC failed for %VGv\n", pVM->csam.s.pPDBitmapGC[pgdir]));
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 | #else
|
---|
147 | pVM->csam.s.pPDGCBitmapHC[pgdir] = MMHyperHC2GC(pVM, pVM->csam.s.pPDBitmapHC[pgdir]);
|
---|
148 | if (!pVM->csam.s.pPDGCBitmapHC[pgdir])
|
---|
149 | {
|
---|
150 | Log(("MMHyperHC2GC failed for %VHv\n", pVM->csam.s.pPDBitmapHC[pgdir]));
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 | if(fScanned)
|
---|
156 | ASMBitSet(pVM->csam.s.CTXSUFF(pPDBitmap)[pgdir], bit);
|
---|
157 | else
|
---|
158 | ASMBitClear(pVM->csam.s.CTXSUFF(pPDBitmap)[pgdir], bit);
|
---|
159 |
|
---|
160 | return VINF_SUCCESS;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Check if this page needs to be analysed by CSAM.
|
---|
165 | *
|
---|
166 | * This function should only be called for supervisor pages and
|
---|
167 | * only when CSAM is enabled. Leaving these selection criteria
|
---|
168 | * to the caller simplifies the interface (PTE passing).
|
---|
169 | *
|
---|
170 | * Note the the page has not yet been synced, so the TLB trick
|
---|
171 | * (which wasn't ever active anyway) cannot be applied.
|
---|
172 | *
|
---|
173 | * @returns true if the page should be marked not present because
|
---|
174 | * CSAM want need to scan it.
|
---|
175 | * @returns false if the page was already scanned.
|
---|
176 | * @param pVM The VM to operate on.
|
---|
177 | * @param GCPtr GC pointer of page
|
---|
178 | */
|
---|
179 | CSAMDECL(bool) CSAMDoesPageNeedScanning(PVM pVM, RTGCPTR GCPtr)
|
---|
180 | {
|
---|
181 | if(!CSAMIsEnabled(pVM))
|
---|
182 | return false;
|
---|
183 |
|
---|
184 | if(CSAMIsPageScanned(pVM, GCPtr))
|
---|
185 | {
|
---|
186 | /* Already checked! */
|
---|
187 | STAM_COUNTER_ADD(&CTXSUFF(pVM->csam.s.StatNrKnownPages), 1);
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 | STAM_COUNTER_ADD(&CTXSUFF(pVM->csam.s.StatNrPageNP), 1);
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Remember a possible code page for later inspection
|
---|
197 | *
|
---|
198 | * @returns VBox status code.
|
---|
199 | * @param pVM The VM to operate on.
|
---|
200 | * @param GCPtr GC pointer of page
|
---|
201 | */
|
---|
202 | CSAMDECL(void) CSAMMarkPossibleCodePage(PVM pVM, RTGCPTR GCPtr)
|
---|
203 | {
|
---|
204 | if (pVM->csam.s.cPossibleCodePages < RT_ELEMENTS(pVM->csam.s.pvPossibleCodePage))
|
---|
205 | {
|
---|
206 | pVM->csam.s.pvPossibleCodePage[pVM->csam.s.cPossibleCodePages++] = GCPtr;
|
---|
207 | VM_FF_SET(pVM, VM_FF_CSAM_PENDING_ACTION);
|
---|
208 | }
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Turn on code scanning
|
---|
215 | *
|
---|
216 | * @returns VBox status code.
|
---|
217 | * @param pVM The VM to operate on.
|
---|
218 | */
|
---|
219 | CSAMDECL(int) CSAMEnableScanning(PVM pVM)
|
---|
220 | {
|
---|
221 | pVM->fCSAMEnabled = true;
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Turn off code scanning
|
---|
227 | *
|
---|
228 | * @returns VBox status code.
|
---|
229 | * @param pVM The VM to operate on.
|
---|
230 | */
|
---|
231 | CSAMDECL(int) CSAMDisableScanning(PVM pVM)
|
---|
232 | {
|
---|
233 | pVM->fCSAMEnabled = false;
|
---|
234 | return VINF_SUCCESS;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Check if we've scanned this instruction before. If true, then we can emulate
|
---|
240 | * it instead of returning to ring 3.
|
---|
241 | *
|
---|
242 | * Using a simple array here as there are generally few mov crx instructions and
|
---|
243 | * tree lookup is likely to be more expensive. (as it would also have to be offset based)
|
---|
244 | *
|
---|
245 | * @returns boolean
|
---|
246 | * @param pVM The VM to operate on.
|
---|
247 | * @param GCPtr GC pointer of page table entry
|
---|
248 | */
|
---|
249 | CSAMDECL(bool) CSAMIsKnownDangerousInstr(PVM pVM, RTGCPTR GCPtr)
|
---|
250 | {
|
---|
251 | for (uint32_t i=0;i<pVM->csam.s.cDangerousInstr;i++)
|
---|
252 | {
|
---|
253 | if (pVM->csam.s.aDangerousInstr[i] == GCPtr)
|
---|
254 | {
|
---|
255 | STAM_COUNTER_INC(&pVM->csam.s.StatInstrCacheHit);
|
---|
256 | return true;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | /* Record that we're about to process it in ring 3. */
|
---|
260 | pVM->csam.s.aDangerousInstr[pVM->csam.s.iDangerousInstr++] = GCPtr;
|
---|
261 | pVM->csam.s.iDangerousInstr &= CSAM_MAX_DANGR_INSTR_MASK;
|
---|
262 |
|
---|
263 | if (++pVM->csam.s.cDangerousInstr > CSAM_MAX_DANGR_INSTR)
|
---|
264 | pVM->csam.s.cDangerousInstr = CSAM_MAX_DANGR_INSTR;
|
---|
265 |
|
---|
266 | STAM_COUNTER_INC(&pVM->csam.s.StatInstrCacheMiss);
|
---|
267 | return false;
|
---|
268 | }
|
---|