1 | /* $Id: CSAM.cpp 13532 2008-10-23 12:39:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * CSAM - Guest OS Code Scanning and Analysis Manager
|
---|
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 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_CSAM
|
---|
26 | #include <VBox/cpum.h>
|
---|
27 | #include <VBox/stam.h>
|
---|
28 | #include <VBox/patm.h>
|
---|
29 | #include <VBox/csam.h>
|
---|
30 | #include <VBox/cpumdis.h>
|
---|
31 | #include <VBox/pgm.h>
|
---|
32 | #include <VBox/iom.h>
|
---|
33 | #include <VBox/sup.h>
|
---|
34 | #include <VBox/mm.h>
|
---|
35 | #include <VBox/em.h>
|
---|
36 | #include <VBox/rem.h>
|
---|
37 | #include <VBox/selm.h>
|
---|
38 | #include <VBox/trpm.h>
|
---|
39 | #include <VBox/cfgm.h>
|
---|
40 | #include <VBox/param.h>
|
---|
41 | #include <iprt/avl.h>
|
---|
42 | #include <iprt/asm.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include "CSAMInternal.h"
|
---|
45 | #include <VBox/vm.h>
|
---|
46 | #include <VBox/dbg.h>
|
---|
47 | #include <VBox/err.h>
|
---|
48 | #include <VBox/ssm.h>
|
---|
49 | #include <VBox/log.h>
|
---|
50 | #include <iprt/assert.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <VBox/dis.h>
|
---|
53 | #include <VBox/disopcode.h>
|
---|
54 | #include <stdlib.h>
|
---|
55 | #include <stdio.h>
|
---|
56 |
|
---|
57 |
|
---|
58 | /* Enabled by default */
|
---|
59 | #define CSAM_ENABLE
|
---|
60 |
|
---|
61 | /* Enable to monitor code pages for self-modifying code. */
|
---|
62 | #define CSAM_MONITOR_CODE_PAGES
|
---|
63 | /* Enable to monitor all scanned pages
|
---|
64 | #define CSAM_MONITOR_CSAM_CODE_PAGES */
|
---|
65 | /* Enable to scan beyond ret instructions.
|
---|
66 | #define CSAM_ANALYSE_BEYOND_RET */
|
---|
67 |
|
---|
68 | /*******************************************************************************
|
---|
69 | * Internal Functions *
|
---|
70 | *******************************************************************************/
|
---|
71 | static DECLCALLBACK(int) csamr3Save(PVM pVM, PSSMHANDLE pSSM);
|
---|
72 | static DECLCALLBACK(int) csamr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
|
---|
73 | static DECLCALLBACK(int) CSAMCodePageWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
|
---|
74 | static DECLCALLBACK(int) CSAMCodePageInvalidate(PVM pVM, RTGCPTR GCPtr);
|
---|
75 |
|
---|
76 | bool csamIsCodeScanned(PVM pVM, RTRCPTR pInstr, PCSAMPAGE *pPage);
|
---|
77 | int csamR3CheckPageRecord(PVM pVM, RTRCPTR pInstr);
|
---|
78 | static PCSAMPAGE csamCreatePageRecord(PVM pVM, RTRCPTR GCPtr, CSAMTAG enmTag, bool fCode32, bool fMonitorInvalidation = false);
|
---|
79 | static int csamRemovePageRecord(PVM pVM, RTRCPTR GCPtr);
|
---|
80 | static int csamReinit(PVM pVM);
|
---|
81 | static void csamMarkCode(PVM pVM, PCSAMPAGE pPage, RTRCPTR pInstr, uint32_t opsize, bool fScanned);
|
---|
82 | static int csamAnalyseCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
83 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec);
|
---|
84 |
|
---|
85 | /** @todo Temporary for debugging. */
|
---|
86 | static bool fInCSAMCodePageInvalidate = false;
|
---|
87 |
|
---|
88 | /*******************************************************************************
|
---|
89 | * Global Variables *
|
---|
90 | *******************************************************************************/
|
---|
91 | #ifdef VBOX_WITH_DEBUGGER
|
---|
92 | static DECLCALLBACK(int) csamr3CmdOn(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
|
---|
93 | static DECLCALLBACK(int) csamr3CmdOff(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
|
---|
94 |
|
---|
95 | /** Command descriptors. */
|
---|
96 | static const DBGCCMD g_aCmds[] =
|
---|
97 | {
|
---|
98 | /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, pResultDesc, fFlags, pfnHandler pszSyntax, ....pszDescription */
|
---|
99 | { "csamon", 0, 0, NULL, 0, NULL, 0, csamr3CmdOn, "", "Enable CSAM code scanning." },
|
---|
100 | { "csamoff", 0, 0, NULL, 0, NULL, 0, csamr3CmdOff, "", "Disable CSAM code scanning." },
|
---|
101 | };
|
---|
102 | #endif
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Initializes the CSAM.
|
---|
107 | *
|
---|
108 | * @returns VBox status code.
|
---|
109 | * @param pVM The VM to operate on.
|
---|
110 | */
|
---|
111 | VMMR3DECL(int) CSAMR3Init(PVM pVM)
|
---|
112 | {
|
---|
113 | int rc;
|
---|
114 |
|
---|
115 | LogFlow(("CSAMR3Init\n"));
|
---|
116 |
|
---|
117 | /* Allocate bitmap for the page directory. */
|
---|
118 | rc = MMR3HyperAllocOnceNoRel(pVM, CSAM_PGDIRBMP_CHUNKS*sizeof(RTHCPTR), 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDBitmapHC);
|
---|
119 | AssertRCReturn(rc, rc);
|
---|
120 | rc = MMR3HyperAllocOnceNoRel(pVM, CSAM_PGDIRBMP_CHUNKS*sizeof(RTRCPTR), 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDGCBitmapHC);
|
---|
121 | AssertRCReturn(rc, rc);
|
---|
122 | pVM->csam.s.pPDBitmapGC = MMHyperHC2GC(pVM, pVM->csam.s.pPDGCBitmapHC);
|
---|
123 | pVM->csam.s.pPDHCBitmapGC = MMHyperHC2GC(pVM, pVM->csam.s.pPDBitmapHC);
|
---|
124 |
|
---|
125 | rc = csamReinit(pVM);
|
---|
126 | AssertRCReturn(rc, rc);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Register save and load state notificators.
|
---|
130 | */
|
---|
131 | rc = SSMR3RegisterInternal(pVM, "CSAM", 0, CSAM_SSM_VERSION, sizeof(pVM->csam.s) + PAGE_SIZE*16,
|
---|
132 | NULL, csamr3Save, NULL,
|
---|
133 | NULL, csamr3Load, NULL);
|
---|
134 | AssertRCReturn(rc, rc);
|
---|
135 |
|
---|
136 | STAM_REG(pVM, &pVM->csam.s.StatNrTraps, STAMTYPE_COUNTER, "/CSAM/PageTraps", STAMUNIT_OCCURENCES, "The number of CSAM page traps.");
|
---|
137 | STAM_REG(pVM, &pVM->csam.s.StatDangerousWrite, STAMTYPE_COUNTER, "/CSAM/DangerousWrites", STAMUNIT_OCCURENCES, "The number of dangerous writes that cause a context switch.");
|
---|
138 |
|
---|
139 | STAM_REG(pVM, &pVM->csam.s.StatNrPageNPHC, STAMTYPE_COUNTER, "/CSAM/HC/PageNotPresent", STAMUNIT_OCCURENCES, "The number of CSAM pages marked not present.");
|
---|
140 | STAM_REG(pVM, &pVM->csam.s.StatNrPageNPGC, STAMTYPE_COUNTER, "/CSAM/GC/PageNotPresent", STAMUNIT_OCCURENCES, "The number of CSAM pages marked not present.");
|
---|
141 | STAM_REG(pVM, &pVM->csam.s.StatNrPages, STAMTYPE_COUNTER, "/CSAM/PageRec/AddedRW", STAMUNIT_OCCURENCES, "The number of CSAM page records (RW monitoring).");
|
---|
142 | STAM_REG(pVM, &pVM->csam.s.StatNrPagesInv, STAMTYPE_COUNTER, "/CSAM/PageRec/AddedRWI", STAMUNIT_OCCURENCES, "The number of CSAM page records (RW & invalidation monitoring).");
|
---|
143 | STAM_REG(pVM, &pVM->csam.s.StatNrRemovedPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Removed", STAMUNIT_OCCURENCES, "The number of removed CSAM page records.");
|
---|
144 | STAM_REG(pVM, &pVM->csam.s.StatPageRemoveREMFlush,STAMTYPE_COUNTER, "/CSAM/PageRec/Removed/REMFlush", STAMUNIT_OCCURENCES, "The number of removed CSAM page records that caused a REM flush.");
|
---|
145 |
|
---|
146 | STAM_REG(pVM, &pVM->csam.s.StatNrPatchPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Patch", STAMUNIT_OCCURENCES, "The number of CSAM patch page records.");
|
---|
147 | STAM_REG(pVM, &pVM->csam.s.StatNrUserPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Ignore/User", STAMUNIT_OCCURENCES, "The number of CSAM user page records (ignored).");
|
---|
148 | STAM_REG(pVM, &pVM->csam.s.StatPagePATM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/PATM", STAMUNIT_OCCURENCES, "The number of PATM page records.");
|
---|
149 | STAM_REG(pVM, &pVM->csam.s.StatPageCSAM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/CSAM", STAMUNIT_OCCURENCES, "The number of CSAM page records.");
|
---|
150 | STAM_REG(pVM, &pVM->csam.s.StatPageREM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/REM", STAMUNIT_OCCURENCES, "The number of REM page records.");
|
---|
151 | STAM_REG(pVM, &pVM->csam.s.StatPageMonitor, STAMTYPE_COUNTER, "/CSAM/PageRec/Monitored", STAMUNIT_OCCURENCES, "The number of monitored pages.");
|
---|
152 |
|
---|
153 | STAM_REG(pVM, &pVM->csam.s.StatCodePageModified, STAMTYPE_COUNTER, "/CSAM/Monitor/DirtyPage", STAMUNIT_OCCURENCES, "The number of code page modifications.");
|
---|
154 |
|
---|
155 | STAM_REG(pVM, &pVM->csam.s.StatNrFlushes, STAMTYPE_COUNTER, "/CSAM/PageFlushes", STAMUNIT_OCCURENCES, "The number of CSAM page flushes.");
|
---|
156 | STAM_REG(pVM, &pVM->csam.s.StatNrFlushesSkipped, STAMTYPE_COUNTER, "/CSAM/PageFlushesSkipped", STAMUNIT_OCCURENCES, "The number of CSAM page flushes that were skipped.");
|
---|
157 | STAM_REG(pVM, &pVM->csam.s.StatNrKnownPagesHC, STAMTYPE_COUNTER, "/CSAM/HC/KnownPageRecords", STAMUNIT_OCCURENCES, "The number of known CSAM page records.");
|
---|
158 | STAM_REG(pVM, &pVM->csam.s.StatNrKnownPagesGC, STAMTYPE_COUNTER, "/CSAM/GC/KnownPageRecords", STAMUNIT_OCCURENCES, "The number of known CSAM page records.");
|
---|
159 | STAM_REG(pVM, &pVM->csam.s.StatNrInstr, STAMTYPE_COUNTER, "/CSAM/ScannedInstr", STAMUNIT_OCCURENCES, "The number of scanned instructions.");
|
---|
160 | STAM_REG(pVM, &pVM->csam.s.StatNrBytesRead, STAMTYPE_COUNTER, "/CSAM/BytesRead", STAMUNIT_OCCURENCES, "The number of bytes read for scanning.");
|
---|
161 | STAM_REG(pVM, &pVM->csam.s.StatNrOpcodeRead, STAMTYPE_COUNTER, "/CSAM/OpcodeBytesRead", STAMUNIT_OCCURENCES, "The number of opcode bytes read by the recompiler.");
|
---|
162 |
|
---|
163 | STAM_REG(pVM, &pVM->csam.s.StatBitmapAlloc, STAMTYPE_COUNTER, "/CSAM/Alloc/PageBitmap", STAMUNIT_OCCURENCES, "The number of page bitmap allocations.");
|
---|
164 |
|
---|
165 | STAM_REG(pVM, &pVM->csam.s.StatInstrCacheHit, STAMTYPE_COUNTER, "/CSAM/Cache/Hit", STAMUNIT_OCCURENCES, "The number of dangerous instruction cache hits.");
|
---|
166 | STAM_REG(pVM, &pVM->csam.s.StatInstrCacheMiss, STAMTYPE_COUNTER, "/CSAM/Cache/Miss", STAMUNIT_OCCURENCES, "The number of dangerous instruction cache misses.");
|
---|
167 |
|
---|
168 | STAM_REG(pVM, &pVM->csam.s.StatScanNextFunction, STAMTYPE_COUNTER, "/CSAM/Function/Scan/Success", STAMUNIT_OCCURENCES, "The number of found functions beyond the ret border.");
|
---|
169 | STAM_REG(pVM, &pVM->csam.s.StatScanNextFunctionFailed, STAMTYPE_COUNTER, "/CSAM/Function/Scan/Failed", STAMUNIT_OCCURENCES, "The number of refused functions beyond the ret border.");
|
---|
170 |
|
---|
171 | STAM_REG(pVM, &pVM->csam.s.StatTime, STAMTYPE_PROFILE, "/PROF/CSAM/Scan", STAMUNIT_TICKS_PER_CALL, "Scanning overhead.");
|
---|
172 | STAM_REG(pVM, &pVM->csam.s.StatTimeCheckAddr, STAMTYPE_PROFILE, "/PROF/CSAM/CheckAddr", STAMUNIT_TICKS_PER_CALL, "Address check overhead.");
|
---|
173 | STAM_REG(pVM, &pVM->csam.s.StatTimeAddrConv, STAMTYPE_PROFILE, "/PROF/CSAM/AddrConv", STAMUNIT_TICKS_PER_CALL, "Address conversion overhead.");
|
---|
174 | STAM_REG(pVM, &pVM->csam.s.StatTimeFlushPage, STAMTYPE_PROFILE, "/PROF/CSAM/FlushPage", STAMUNIT_TICKS_PER_CALL, "Page flushing overhead.");
|
---|
175 | STAM_REG(pVM, &pVM->csam.s.StatTimeDisasm, STAMTYPE_PROFILE, "/PROF/CSAM/Disasm", STAMUNIT_TICKS_PER_CALL, "Disassembly overhead.");
|
---|
176 | STAM_REG(pVM, &pVM->csam.s.StatFlushDirtyPages, STAMTYPE_PROFILE, "/PROF/CSAM/FlushDirtyPage", STAMUNIT_TICKS_PER_CALL, "Dirty page flushing overhead.");
|
---|
177 | STAM_REG(pVM, &pVM->csam.s.StatCheckGates, STAMTYPE_PROFILE, "/PROF/CSAM/CheckGates", STAMUNIT_TICKS_PER_CALL, "CSAMR3CheckGates overhead.");
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Check CFGM option and enable/disable CSAM.
|
---|
181 | */
|
---|
182 | bool fEnabled;
|
---|
183 | rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "CSAMEnabled", &fEnabled);
|
---|
184 | if (VBOX_FAILURE(rc))
|
---|
185 | #ifdef CSAM_ENABLE
|
---|
186 | fEnabled = true;
|
---|
187 | #else
|
---|
188 | fEnabled = false;
|
---|
189 | #endif
|
---|
190 | if (fEnabled)
|
---|
191 | CSAMEnableScanning(pVM);
|
---|
192 |
|
---|
193 | #ifdef VBOX_WITH_DEBUGGER
|
---|
194 | /*
|
---|
195 | * Debugger commands.
|
---|
196 | */
|
---|
197 | static bool fRegisteredCmds = false;
|
---|
198 | if (!fRegisteredCmds)
|
---|
199 | {
|
---|
200 | int rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
|
---|
201 | if (VBOX_SUCCESS(rc))
|
---|
202 | fRegisteredCmds = true;
|
---|
203 | }
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | return VINF_SUCCESS;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * (Re)initializes CSAM
|
---|
211 | *
|
---|
212 | * @param pVM The VM.
|
---|
213 | */
|
---|
214 | static int csamReinit(PVM pVM)
|
---|
215 | {
|
---|
216 | /*
|
---|
217 | * Assert alignment and sizes.
|
---|
218 | */
|
---|
219 | AssertRelease(!(RT_OFFSETOF(VM, csam.s) & 31));
|
---|
220 | AssertRelease(sizeof(pVM->csam.s) <= sizeof(pVM->csam.padding));
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Setup any fixed pointers and offsets.
|
---|
224 | */
|
---|
225 | pVM->csam.s.offVM = RT_OFFSETOF(VM, patm);
|
---|
226 |
|
---|
227 | pVM->csam.s.fGatesChecked = false;
|
---|
228 | pVM->csam.s.fScanningStarted = false;
|
---|
229 |
|
---|
230 | VM_FF_CLEAR(pVM, VM_FF_CSAM_PENDING_ACTION);
|
---|
231 | pVM->csam.s.cDirtyPages = 0;
|
---|
232 | /* not necessary */
|
---|
233 | memset(pVM->csam.s.pvDirtyBasePage, 0, sizeof(pVM->csam.s.pvDirtyBasePage));
|
---|
234 | memset(pVM->csam.s.pvDirtyFaultPage, 0, sizeof(pVM->csam.s.pvDirtyFaultPage));
|
---|
235 |
|
---|
236 | memset(&pVM->csam.s.aDangerousInstr, 0, sizeof(pVM->csam.s.aDangerousInstr));
|
---|
237 | pVM->csam.s.cDangerousInstr = 0;
|
---|
238 | pVM->csam.s.iDangerousInstr = 0;
|
---|
239 |
|
---|
240 | memset(pVM->csam.s.pvCallInstruction, 0, sizeof(pVM->csam.s.pvCallInstruction));
|
---|
241 | pVM->csam.s.iCallInstruction = 0;
|
---|
242 |
|
---|
243 | /** @note never mess with the pgdir bitmap here! */
|
---|
244 | return VINF_SUCCESS;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Applies relocations to data and code managed by this
|
---|
249 | * component. This function will be called at init and
|
---|
250 | * whenever the VMM need to relocate itself inside the GC.
|
---|
251 | *
|
---|
252 | * The csam will update the addresses used by the switcher.
|
---|
253 | *
|
---|
254 | * @param pVM The VM.
|
---|
255 | * @param offDelta Relocation delta.
|
---|
256 | */
|
---|
257 | VMMR3DECL(void) CSAMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
258 | {
|
---|
259 | if (offDelta)
|
---|
260 | {
|
---|
261 | /* Adjust pgdir and page bitmap pointers. */
|
---|
262 | pVM->csam.s.pPDBitmapGC = MMHyperHC2GC(pVM, pVM->csam.s.pPDGCBitmapHC);
|
---|
263 | pVM->csam.s.pPDHCBitmapGC = MMHyperHC2GC(pVM, pVM->csam.s.pPDBitmapHC);
|
---|
264 |
|
---|
265 | for(int i=0;i<CSAM_PGDIRBMP_CHUNKS;i++)
|
---|
266 | {
|
---|
267 | if (pVM->csam.s.pPDGCBitmapHC[i])
|
---|
268 | {
|
---|
269 | pVM->csam.s.pPDGCBitmapHC[i] += offDelta;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | return;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Terminates the csam.
|
---|
278 | *
|
---|
279 | * Termination means cleaning up and freeing all resources,
|
---|
280 | * the VM it self is at this point powered off or suspended.
|
---|
281 | *
|
---|
282 | * @returns VBox status code.
|
---|
283 | * @param pVM The VM to operate on.
|
---|
284 | */
|
---|
285 | VMMR3DECL(int) CSAMR3Term(PVM pVM)
|
---|
286 | {
|
---|
287 | int rc;
|
---|
288 |
|
---|
289 | rc = CSAMR3Reset(pVM);
|
---|
290 | AssertRC(rc);
|
---|
291 |
|
---|
292 | /* @todo triggers assertion in MMHyperFree */
|
---|
293 | #if 0
|
---|
294 | for(int i=0;i<CSAM_PAGEBMP_CHUNKS;i++)
|
---|
295 | {
|
---|
296 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
297 | MMHyperFree(pVM, pVM->csam.s.pPDBitmapHC[i]);
|
---|
298 | }
|
---|
299 | #endif
|
---|
300 |
|
---|
301 | return VINF_SUCCESS;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * CSAM reset callback.
|
---|
306 | *
|
---|
307 | * @returns VBox status code.
|
---|
308 | * @param pVM The VM which is reset.
|
---|
309 | */
|
---|
310 | VMMR3DECL(int) CSAMR3Reset(PVM pVM)
|
---|
311 | {
|
---|
312 | /* Clear page bitmaps. */
|
---|
313 | for(int i=0;i<CSAM_PGDIRBMP_CHUNKS;i++)
|
---|
314 | {
|
---|
315 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
316 | {
|
---|
317 | Assert((CSAM_PAGE_BITMAP_SIZE& 3) == 0);
|
---|
318 | ASMMemZero32(pVM->csam.s.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | /* Remove all CSAM page records. */
|
---|
323 | while(true)
|
---|
324 | {
|
---|
325 | PCSAMPAGEREC pPageRec = (PCSAMPAGEREC)RTAvlPVGetBestFit(&pVM->csam.s.pPageTree, 0, true);
|
---|
326 | if (pPageRec)
|
---|
327 | {
|
---|
328 | csamRemovePageRecord(pVM, pPageRec->page.pPageGC);
|
---|
329 | }
|
---|
330 | else
|
---|
331 | break;
|
---|
332 | }
|
---|
333 | Assert(!pVM->csam.s.pPageTree);
|
---|
334 |
|
---|
335 | csamReinit(pVM);
|
---|
336 |
|
---|
337 | return VINF_SUCCESS;
|
---|
338 | }
|
---|
339 |
|
---|
340 | #define CSAM_SUBTRACT_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) - (uintptr_t)(b)
|
---|
341 | #define CSAM_ADD_PTR(a, b) *(uintptr_t *)&(a) = (uintptr_t)(a) + (uintptr_t)(b)
|
---|
342 |
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Callback function for RTAvlPVDoWithAll
|
---|
346 | *
|
---|
347 | * Counts the number of records in the tree
|
---|
348 | *
|
---|
349 | * @returns VBox status code.
|
---|
350 | * @param pNode Current node
|
---|
351 | * @param pcPatches Pointer to patch counter
|
---|
352 | */
|
---|
353 | static DECLCALLBACK(int) CountRecord(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
354 | {
|
---|
355 | *(uint32_t *)pcPatches = *(uint32_t *)pcPatches + 1;
|
---|
356 | return VINF_SUCCESS;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Callback function for RTAvlPVDoWithAll
|
---|
361 | *
|
---|
362 | * Saves the state of the page record
|
---|
363 | *
|
---|
364 | * @returns VBox status code.
|
---|
365 | * @param pNode Current node
|
---|
366 | * @param pVM1 VM Handle
|
---|
367 | */
|
---|
368 | static DECLCALLBACK(int) SavePageState(PAVLPVNODECORE pNode, void *pVM1)
|
---|
369 | {
|
---|
370 | PVM pVM = (PVM)pVM1;
|
---|
371 | PCSAMPAGEREC pPage = (PCSAMPAGEREC)pNode;
|
---|
372 | CSAMPAGEREC page = *pPage;
|
---|
373 | PSSMHANDLE pSSM = pVM->csam.s.savedstate.pSSM;
|
---|
374 | int rc;
|
---|
375 |
|
---|
376 | /* Save the page record itself */
|
---|
377 | rc = SSMR3PutMem(pSSM, &page, sizeof(page));
|
---|
378 | AssertRCReturn(rc, rc);
|
---|
379 |
|
---|
380 | if (page.page.pBitmap)
|
---|
381 | {
|
---|
382 | rc = SSMR3PutMem(pSSM, page.page.pBitmap, CSAM_PAGE_BITMAP_SIZE);
|
---|
383 | AssertRCReturn(rc, rc);
|
---|
384 | }
|
---|
385 |
|
---|
386 | return VINF_SUCCESS;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Execute state save operation.
|
---|
391 | *
|
---|
392 | * @returns VBox status code.
|
---|
393 | * @param pVM VM Handle.
|
---|
394 | * @param pSSM SSM operation handle.
|
---|
395 | */
|
---|
396 | static DECLCALLBACK(int) csamr3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
397 | {
|
---|
398 | CSAM csamInfo = pVM->csam.s;
|
---|
399 | int rc;
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * Count the number of page records in the tree (feeling lazy)
|
---|
403 | */
|
---|
404 | csamInfo.savedstate.cPageRecords = 0;
|
---|
405 | RTAvlPVDoWithAll(&pVM->csam.s.pPageTree, true, CountRecord, &csamInfo.savedstate.cPageRecords);
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * Save CSAM structure
|
---|
409 | */
|
---|
410 | pVM->csam.s.savedstate.pSSM = pSSM;
|
---|
411 | rc = SSMR3PutMem(pSSM, &csamInfo, sizeof(csamInfo));
|
---|
412 | AssertRCReturn(rc, rc);
|
---|
413 |
|
---|
414 | /* Save pgdir bitmap */
|
---|
415 | rc = SSMR3PutMem(pSSM, csamInfo.pPDBitmapHC, CSAM_PGDIRBMP_CHUNKS*sizeof(RTHCPTR));
|
---|
416 | AssertRCReturn(rc, rc);
|
---|
417 |
|
---|
418 | for (unsigned i=0;i<CSAM_PGDIRBMP_CHUNKS;i++)
|
---|
419 | {
|
---|
420 | if(csamInfo.pPDBitmapHC[i])
|
---|
421 | {
|
---|
422 | /* Save the page bitmap. */
|
---|
423 | rc = SSMR3PutMem(pSSM, csamInfo.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
424 | AssertRCReturn(rc, rc);
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Save page records
|
---|
430 | */
|
---|
431 | rc = RTAvlPVDoWithAll(&pVM->csam.s.pPageTree, true, SavePageState, pVM);
|
---|
432 | AssertRCReturn(rc, rc);
|
---|
433 |
|
---|
434 | /** @note we don't restore aDangerousInstr; it will be recreated automatically. */
|
---|
435 | return VINF_SUCCESS;
|
---|
436 | }
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Execute state load operation.
|
---|
440 | *
|
---|
441 | * @returns VBox status code.
|
---|
442 | * @param pVM VM Handle.
|
---|
443 | * @param pSSM SSM operation handle.
|
---|
444 | * @param u32Version Data layout version.
|
---|
445 | */
|
---|
446 | static DECLCALLBACK(int) csamr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
447 | {
|
---|
448 | int rc;
|
---|
449 | CSAM csamInfo;
|
---|
450 |
|
---|
451 | if (u32Version != CSAM_SSM_VERSION)
|
---|
452 | {
|
---|
453 | AssertMsgFailed(("csamR3Load: Invalid version u32Version=%d!\n", u32Version));
|
---|
454 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
455 | }
|
---|
456 |
|
---|
457 | pVM->csam.s.savedstate.pSSM = pSSM;
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * Restore CSAM structure
|
---|
461 | */
|
---|
462 | rc = SSMR3GetMem(pSSM, &csamInfo, sizeof(csamInfo));
|
---|
463 | AssertRCReturn(rc, rc);
|
---|
464 |
|
---|
465 | pVM->csam.s.fGatesChecked = csamInfo.fGatesChecked;
|
---|
466 | pVM->csam.s.fScanningStarted = csamInfo.fScanningStarted;
|
---|
467 |
|
---|
468 | /* Restore dirty code page info. */
|
---|
469 | pVM->csam.s.cDirtyPages = csamInfo.cDirtyPages;
|
---|
470 | memcpy(pVM->csam.s.pvDirtyBasePage, csamInfo.pvDirtyBasePage, sizeof(pVM->csam.s.pvDirtyBasePage));
|
---|
471 | memcpy(pVM->csam.s.pvDirtyFaultPage, csamInfo.pvDirtyFaultPage, sizeof(pVM->csam.s.pvDirtyFaultPage));
|
---|
472 |
|
---|
473 | /* Restore possible code page */
|
---|
474 | pVM->csam.s.cPossibleCodePages = csamInfo.cPossibleCodePages;
|
---|
475 | memcpy(pVM->csam.s.pvPossibleCodePage, csamInfo.pvPossibleCodePage, sizeof(pVM->csam.s.pvPossibleCodePage));
|
---|
476 |
|
---|
477 | /* Restore pgdir bitmap (we'll change the pointers next). */
|
---|
478 | rc = SSMR3GetMem(pSSM, pVM->csam.s.pPDBitmapHC, CSAM_PGDIRBMP_CHUNKS*sizeof(RTHCPTR));
|
---|
479 | AssertRCReturn(rc, rc);
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * Restore page bitmaps
|
---|
483 | */
|
---|
484 | for (unsigned i=0;i<CSAM_PGDIRBMP_CHUNKS;i++)
|
---|
485 | {
|
---|
486 | if(pVM->csam.s.pPDBitmapHC[i])
|
---|
487 | {
|
---|
488 | rc = MMHyperAlloc(pVM, CSAM_PAGE_BITMAP_SIZE, 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDBitmapHC[i]);
|
---|
489 | if (VBOX_FAILURE(rc))
|
---|
490 | {
|
---|
491 | Log(("MMR3HyperAlloc failed with %d\n", rc));
|
---|
492 | return rc;
|
---|
493 | }
|
---|
494 | /* Convert to GC pointer. */
|
---|
495 | pVM->csam.s.pPDGCBitmapHC[i] = MMHyperHC2GC(pVM, pVM->csam.s.pPDBitmapHC[i]);
|
---|
496 | Assert(pVM->csam.s.pPDGCBitmapHC[i]);
|
---|
497 |
|
---|
498 | /* Restore the bitmap. */
|
---|
499 | rc = SSMR3GetMem(pSSM, pVM->csam.s.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
500 | AssertRCReturn(rc, rc);
|
---|
501 | }
|
---|
502 | else
|
---|
503 | {
|
---|
504 | Assert(!pVM->csam.s.pPDGCBitmapHC[i]);
|
---|
505 | pVM->csam.s.pPDGCBitmapHC[i] = 0;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * Restore page records
|
---|
511 | */
|
---|
512 | for (uint32_t i=0;i<csamInfo.savedstate.cPageRecords + csamInfo.savedstate.cPatchPageRecords;i++)
|
---|
513 | {
|
---|
514 | CSAMPAGEREC page;
|
---|
515 | PCSAMPAGE pPage;
|
---|
516 |
|
---|
517 | rc = SSMR3GetMem(pSSM, &page, sizeof(page));
|
---|
518 | AssertRCReturn(rc, rc);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Recreate the page record
|
---|
522 | */
|
---|
523 | pPage = csamCreatePageRecord(pVM, page.page.pPageGC, page.page.enmTag, page.page.fCode32, page.page.fMonitorInvalidation);
|
---|
524 | AssertReturn(pPage, VERR_NO_MEMORY);
|
---|
525 |
|
---|
526 | pPage->GCPhys = page.page.GCPhys;
|
---|
527 | pPage->fFlags = page.page.fFlags;
|
---|
528 | pPage->u64Hash = page.page.u64Hash;
|
---|
529 |
|
---|
530 | if (page.page.pBitmap)
|
---|
531 | {
|
---|
532 | rc = SSMR3GetMem(pSSM, pPage->pBitmap, CSAM_PAGE_BITMAP_SIZE);
|
---|
533 | AssertRCReturn(rc, rc);
|
---|
534 | }
|
---|
535 | else
|
---|
536 | {
|
---|
537 | MMR3HeapFree(pPage->pBitmap);
|
---|
538 | pPage->pBitmap = 0;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | /** @note we don't restore aDangerousInstr; it will be recreated automatically. */
|
---|
543 | memset(&pVM->csam.s.aDangerousInstr, 0, sizeof(pVM->csam.s.aDangerousInstr));
|
---|
544 | pVM->csam.s.cDangerousInstr = 0;
|
---|
545 | pVM->csam.s.iDangerousInstr = 0;
|
---|
546 | return VINF_SUCCESS;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Convert guest context address to host context pointer
|
---|
551 | *
|
---|
552 | * @returns VBox status code.
|
---|
553 | * @param pVM The VM to operate on.
|
---|
554 | * @param pCacheRec Address conversion cache record
|
---|
555 | * @param pGCPtr Guest context pointer
|
---|
556 | *
|
---|
557 | * @returns Host context pointer or NULL in case of an error
|
---|
558 | *
|
---|
559 | */
|
---|
560 | static R3PTRTYPE(void *) CSAMGCVirtToHCVirt(PVM pVM, PCSAMP2GLOOKUPREC pCacheRec, RCPTRTYPE(uint8_t *) pGCPtr)
|
---|
561 | {
|
---|
562 | int rc;
|
---|
563 | R3PTRTYPE(void *) pHCPtr;
|
---|
564 |
|
---|
565 | STAM_PROFILE_START(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
566 |
|
---|
567 | pHCPtr = PATMR3GCPtrToHCPtr(pVM, pGCPtr);
|
---|
568 | if (pHCPtr) return pHCPtr;
|
---|
569 |
|
---|
570 | if (pCacheRec->pPageLocStartHC)
|
---|
571 | {
|
---|
572 | uint32_t offset = pGCPtr & PAGE_OFFSET_MASK;
|
---|
573 | if (pCacheRec->pGuestLoc == (pGCPtr & PAGE_BASE_GC_MASK))
|
---|
574 | {
|
---|
575 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
576 | return pCacheRec->pPageLocStartHC + offset;
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | rc = PGMPhysGCPtr2HCPtr(pVM, pGCPtr, &pHCPtr);
|
---|
581 | if (rc != VINF_SUCCESS)
|
---|
582 | {
|
---|
583 | //// AssertMsgRC(rc, ("MMR3PhysGCVirt2HCVirtEx failed for %VRv\n", pGCPtr));
|
---|
584 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
585 | return NULL;
|
---|
586 | }
|
---|
587 | ////invalid? Assert(sizeof(R3PTRTYPE(uint8_t*)) == sizeof(uint32_t));
|
---|
588 |
|
---|
589 | pCacheRec->pPageLocStartHC = (R3PTRTYPE(uint8_t*))((RTHCUINTPTR)pHCPtr & PAGE_BASE_HC_MASK);
|
---|
590 | pCacheRec->pGuestLoc = pGCPtr & PAGE_BASE_GC_MASK;
|
---|
591 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
592 | return pHCPtr;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /**
|
---|
596 | * Read callback for disassembly function; supports reading bytes that cross a page boundary
|
---|
597 | *
|
---|
598 | * @returns VBox status code.
|
---|
599 | * @param pSrc GC source pointer
|
---|
600 | * @param pDest HC destination pointer
|
---|
601 | * @param size Number of bytes to read
|
---|
602 | * @param dwUserdata Callback specific user data (pCpu)
|
---|
603 | *
|
---|
604 | */
|
---|
605 | static DECLCALLBACK(int) CSAMR3ReadBytes(RTUINTPTR pSrc, uint8_t *pDest, unsigned size, void *pvUserdata)
|
---|
606 | {
|
---|
607 | DISCPUSTATE *pCpu = (DISCPUSTATE *)pvUserdata;
|
---|
608 | PVM pVM = (PVM)pCpu->apvUserData[0];
|
---|
609 | RTHCUINTPTR pInstrHC = (RTHCUINTPTR)pCpu->apvUserData[1];
|
---|
610 | RTGCUINTPTR32 pInstrGC = (uintptr_t)pCpu->apvUserData[2];
|
---|
611 | int orgsize = size;
|
---|
612 |
|
---|
613 | /* We are not interested in patched instructions, so read the original opcode bytes. */
|
---|
614 | /** @note single instruction patches (int3) are checked in CSAMR3AnalyseCallback */
|
---|
615 | for (int i=0;i<orgsize;i++)
|
---|
616 | {
|
---|
617 | int rc = PATMR3QueryOpcode(pVM, (RTRCPTR)pSrc, pDest);
|
---|
618 | if (VBOX_SUCCESS(rc))
|
---|
619 | {
|
---|
620 | pSrc++;
|
---|
621 | pDest++;
|
---|
622 | size--;
|
---|
623 | }
|
---|
624 | else
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | if (size == 0)
|
---|
628 | return VINF_SUCCESS;
|
---|
629 |
|
---|
630 | if (PAGE_ADDRESS(pInstrGC) != PAGE_ADDRESS(pSrc + size - 1) && !PATMIsPatchGCAddr(pVM, pSrc))
|
---|
631 | {
|
---|
632 | return PGMPhysSimpleReadGCPtr(pVM, pDest, pSrc, size);
|
---|
633 | }
|
---|
634 | else
|
---|
635 | {
|
---|
636 | Assert(pInstrHC);
|
---|
637 |
|
---|
638 | /* pInstrHC is the base address; adjust according to the GC pointer. */
|
---|
639 | pInstrHC = pInstrHC + (pSrc - pInstrGC);
|
---|
640 |
|
---|
641 | memcpy(pDest, (void *)pInstrHC, size);
|
---|
642 | }
|
---|
643 |
|
---|
644 | return VINF_SUCCESS;
|
---|
645 | }
|
---|
646 |
|
---|
647 | inline int CSAMR3DISInstr(PVM pVM, DISCPUSTATE *pCpu, RTRCPTR InstrGC, uint8_t *InstrHC, uint32_t *pOpsize, char *pszOutput)
|
---|
648 | {
|
---|
649 | (pCpu)->pfnReadBytes = CSAMR3ReadBytes;
|
---|
650 | (pCpu)->apvUserData[0] = pVM;
|
---|
651 | (pCpu)->apvUserData[1] = InstrHC;
|
---|
652 | (pCpu)->apvUserData[2] = (void *)InstrGC; Assert(sizeof(InstrGC) <= sizeof(pCpu->apvUserData[0]));
|
---|
653 | #ifdef DEBUG
|
---|
654 | return DISInstrEx(pCpu, InstrGC, 0, pOpsize, pszOutput, OPTYPE_ALL);
|
---|
655 | #else
|
---|
656 | /* We are interested in everything except harmless stuff */
|
---|
657 | return DISInstrEx(pCpu, InstrGC, 0, pOpsize, pszOutput, ~(OPTYPE_INVALID | OPTYPE_HARMLESS | OPTYPE_RRM_MASK));
|
---|
658 | #endif
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Analyses the instructions following the cli for compliance with our heuristics for cli
|
---|
663 | *
|
---|
664 | * @returns VBox status code.
|
---|
665 | * @param pVM The VM to operate on.
|
---|
666 | * @param pCpu CPU disassembly state
|
---|
667 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
668 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
669 | * @param pCacheRec GC to HC cache record
|
---|
670 | * @param pUserData User pointer (callback specific)
|
---|
671 | *
|
---|
672 | */
|
---|
673 | static int CSAMR3AnalyseCallback(PVM pVM, DISCPUSTATE *pCpu, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC,
|
---|
674 | PCSAMP2GLOOKUPREC pCacheRec, void *pUserData)
|
---|
675 | {
|
---|
676 | PCSAMPAGE pPage = (PCSAMPAGE)pUserData;
|
---|
677 | int rc;
|
---|
678 |
|
---|
679 | switch(pCpu->pCurInstr->opcode)
|
---|
680 | {
|
---|
681 | case OP_INT:
|
---|
682 | Assert(pCpu->param1.flags & USE_IMMEDIATE8);
|
---|
683 | if (pCpu->param1.parval == 3)
|
---|
684 | {
|
---|
685 | //two byte int 3
|
---|
686 | return VINF_SUCCESS;
|
---|
687 | }
|
---|
688 | break;
|
---|
689 |
|
---|
690 | case OP_ILLUD2:
|
---|
691 | /* This appears to be some kind of kernel panic in Linux 2.4; no point to continue. */
|
---|
692 | case OP_RETN:
|
---|
693 | case OP_INT3:
|
---|
694 | case OP_INVALID:
|
---|
695 | #if 1
|
---|
696 | /* removing breaks win2k guests? */
|
---|
697 | case OP_IRET:
|
---|
698 | #endif
|
---|
699 | return VINF_SUCCESS;
|
---|
700 | }
|
---|
701 |
|
---|
702 | // Check for exit points
|
---|
703 | switch (pCpu->pCurInstr->opcode)
|
---|
704 | {
|
---|
705 | /* It's not a good idea to patch pushf instructions:
|
---|
706 | * - increases the chance of conflicts (code jumping to the next instruction)
|
---|
707 | * - better to patch the cli
|
---|
708 | * - code that branches before the cli will likely hit an int 3
|
---|
709 | * - in general doesn't offer any benefits as we don't allow nested patch blocks (IF is always 1)
|
---|
710 | */
|
---|
711 | case OP_PUSHF:
|
---|
712 | case OP_POPF:
|
---|
713 | break;
|
---|
714 |
|
---|
715 | case OP_CLI:
|
---|
716 | {
|
---|
717 | uint32_t cbInstr = 0;
|
---|
718 | uint32_t opsize = pCpu->opsize;
|
---|
719 |
|
---|
720 | PATMR3AddHint(pVM, pCurInstrGC, (pPage->fCode32) ? PATMFL_CODE32 : 0);
|
---|
721 |
|
---|
722 | /* Make sure the instructions that follow the cli have not been encountered before. */
|
---|
723 | while (true)
|
---|
724 | {
|
---|
725 | DISCPUSTATE cpu;
|
---|
726 | uint8_t *pCurInstrHC = 0;
|
---|
727 |
|
---|
728 | if (cbInstr + opsize >= SIZEOF_NEARJUMP32)
|
---|
729 | break;
|
---|
730 |
|
---|
731 | if (csamIsCodeScanned(pVM, pCurInstrGC + opsize, &pPage) == true)
|
---|
732 | {
|
---|
733 | /* We've scanned the next instruction(s) already. This means we've followed a branch that ended up there before -> dangerous!! */
|
---|
734 | PATMR3DetectConflict(pVM, pCurInstrGC, pCurInstrGC + opsize);
|
---|
735 | break;
|
---|
736 | }
|
---|
737 | pCurInstrGC += opsize;
|
---|
738 | cbInstr += opsize;
|
---|
739 |
|
---|
740 | pCurInstrHC = (uint8_t *)CSAMGCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
741 | if (pCurInstrHC == NULL)
|
---|
742 | {
|
---|
743 | Log(("CSAMGCVirtToHCVirt failed for %VRv\n", pCurInstrGC));
|
---|
744 | break;
|
---|
745 | }
|
---|
746 | Assert(VALID_PTR(pCurInstrHC));
|
---|
747 |
|
---|
748 | cpu.mode = (pPage->fCode32) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
749 | rc = CSAMR3DISInstr(pVM, &cpu, pCurInstrGC, pCurInstrHC, &opsize, NULL);
|
---|
750 | Assert(VBOX_SUCCESS(rc));
|
---|
751 | if (VBOX_FAILURE(rc))
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | break;
|
---|
755 | }
|
---|
756 |
|
---|
757 | case OP_PUSH:
|
---|
758 | if (pCpu->pCurInstr->param1 != OP_PARM_REG_CS)
|
---|
759 | break;
|
---|
760 |
|
---|
761 | /* no break */
|
---|
762 | case OP_STR:
|
---|
763 | case OP_LSL:
|
---|
764 | case OP_LAR:
|
---|
765 | case OP_SGDT:
|
---|
766 | case OP_SLDT:
|
---|
767 | case OP_SIDT:
|
---|
768 | case OP_SMSW:
|
---|
769 | case OP_VERW:
|
---|
770 | case OP_VERR:
|
---|
771 | case OP_CPUID:
|
---|
772 | case OP_IRET:
|
---|
773 | #ifdef DEBUG
|
---|
774 | switch(pCpu->pCurInstr->opcode)
|
---|
775 | {
|
---|
776 | case OP_STR:
|
---|
777 | Log(("Privileged instruction at %VRv: str!!\n", pCurInstrGC));
|
---|
778 | break;
|
---|
779 | case OP_LSL:
|
---|
780 | Log(("Privileged instruction at %VRv: lsl!!\n", pCurInstrGC));
|
---|
781 | break;
|
---|
782 | case OP_LAR:
|
---|
783 | Log(("Privileged instruction at %VRv: lar!!\n", pCurInstrGC));
|
---|
784 | break;
|
---|
785 | case OP_SGDT:
|
---|
786 | Log(("Privileged instruction at %VRv: sgdt!!\n", pCurInstrGC));
|
---|
787 | break;
|
---|
788 | case OP_SLDT:
|
---|
789 | Log(("Privileged instruction at %VRv: sldt!!\n", pCurInstrGC));
|
---|
790 | break;
|
---|
791 | case OP_SIDT:
|
---|
792 | Log(("Privileged instruction at %VRv: sidt!!\n", pCurInstrGC));
|
---|
793 | break;
|
---|
794 | case OP_SMSW:
|
---|
795 | Log(("Privileged instruction at %VRv: smsw!!\n", pCurInstrGC));
|
---|
796 | break;
|
---|
797 | case OP_VERW:
|
---|
798 | Log(("Privileged instruction at %VRv: verw!!\n", pCurInstrGC));
|
---|
799 | break;
|
---|
800 | case OP_VERR:
|
---|
801 | Log(("Privileged instruction at %VRv: verr!!\n", pCurInstrGC));
|
---|
802 | break;
|
---|
803 | case OP_CPUID:
|
---|
804 | Log(("Privileged instruction at %VRv: cpuid!!\n", pCurInstrGC));
|
---|
805 | break;
|
---|
806 | case OP_PUSH:
|
---|
807 | Log(("Privileged instruction at %VRv: push cs!!\n", pCurInstrGC));
|
---|
808 | break;
|
---|
809 | case OP_IRET:
|
---|
810 | Log(("Privileged instruction at %VRv: iret!!\n", pCurInstrGC));
|
---|
811 | break;
|
---|
812 | }
|
---|
813 | #endif
|
---|
814 |
|
---|
815 | if (PATMR3HasBeenPatched(pVM, pCurInstrGC) == false)
|
---|
816 | {
|
---|
817 | rc = PATMR3InstallPatch(pVM, pCurInstrGC, (pPage->fCode32) ? PATMFL_CODE32 : 0);
|
---|
818 | if (VBOX_FAILURE(rc))
|
---|
819 | {
|
---|
820 | Log(("PATMR3InstallPatch failed with %d\n", rc));
|
---|
821 | return VWRN_CONTINUE_ANALYSIS;
|
---|
822 | }
|
---|
823 | }
|
---|
824 | if (pCpu->pCurInstr->opcode == OP_IRET)
|
---|
825 | return VINF_SUCCESS; /* Look no further in this branch. */
|
---|
826 |
|
---|
827 | return VWRN_CONTINUE_ANALYSIS;
|
---|
828 |
|
---|
829 | case OP_JMP:
|
---|
830 | case OP_CALL:
|
---|
831 | {
|
---|
832 | // return or jump/call through a jump table
|
---|
833 | if (OP_PARM_VTYPE(pCpu->pCurInstr->param1) != OP_PARM_J)
|
---|
834 | {
|
---|
835 | #ifdef DEBUG
|
---|
836 | switch(pCpu->pCurInstr->opcode)
|
---|
837 | {
|
---|
838 | case OP_JMP:
|
---|
839 | Log(("Control Flow instruction at %VRv: jmp!!\n", pCurInstrGC));
|
---|
840 | break;
|
---|
841 | case OP_CALL:
|
---|
842 | Log(("Control Flow instruction at %VRv: call!!\n", pCurInstrGC));
|
---|
843 | break;
|
---|
844 | }
|
---|
845 | #endif
|
---|
846 | return VWRN_CONTINUE_ANALYSIS;
|
---|
847 | }
|
---|
848 | return VWRN_CONTINUE_ANALYSIS;
|
---|
849 | }
|
---|
850 |
|
---|
851 | }
|
---|
852 |
|
---|
853 | return VWRN_CONTINUE_ANALYSIS;
|
---|
854 | }
|
---|
855 |
|
---|
856 | #ifdef CSAM_ANALYSE_BEYOND_RET
|
---|
857 | /**
|
---|
858 | * Wrapper for csamAnalyseCodeStream for call instructions.
|
---|
859 | *
|
---|
860 | * @returns VBox status code.
|
---|
861 | * @param pVM The VM to operate on.
|
---|
862 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
863 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
864 | * @param fCode32 16 or 32 bits code
|
---|
865 | * @param pfnCSAMR3Analyse Callback for testing the disassembled instruction
|
---|
866 | * @param pUserData User pointer (callback specific)
|
---|
867 | *
|
---|
868 | */
|
---|
869 | static int csamAnalyseCallCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
870 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec)
|
---|
871 | {
|
---|
872 | int rc;
|
---|
873 | CSAMCALLEXITREC CallExitRec;
|
---|
874 | PCSAMCALLEXITREC pOldCallRec;
|
---|
875 | PCSAMPAGE pPage = 0;
|
---|
876 | uint32_t i;
|
---|
877 |
|
---|
878 | CallExitRec.cInstrAfterRet = 0;
|
---|
879 |
|
---|
880 | pOldCallRec = pCacheRec->pCallExitRec;
|
---|
881 | pCacheRec->pCallExitRec = &CallExitRec;
|
---|
882 |
|
---|
883 | rc = csamAnalyseCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
884 |
|
---|
885 | for (i=0;i<CallExitRec.cInstrAfterRet;i++)
|
---|
886 | {
|
---|
887 | PCSAMPAGE pPage = 0;
|
---|
888 |
|
---|
889 | pCurInstrGC = CallExitRec.pInstrAfterRetGC[i];
|
---|
890 |
|
---|
891 | /* Check if we've previously encountered the instruction after the ret. */
|
---|
892 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
893 | {
|
---|
894 | DISCPUSTATE cpu;
|
---|
895 | uint32_t opsize;
|
---|
896 | uint8_t *pCurInstrHC = 0;
|
---|
897 | int rc2;
|
---|
898 | #ifdef DEBUG
|
---|
899 | char szOutput[256];
|
---|
900 | #endif
|
---|
901 | if (pPage == NULL)
|
---|
902 | {
|
---|
903 | /* New address; let's take a look at it. */
|
---|
904 | pPage = csamCreatePageRecord(pVM, pCurInstrGC, CSAM_TAG_CSAM, fCode32);
|
---|
905 | if (pPage == NULL)
|
---|
906 | {
|
---|
907 | rc = VERR_NO_MEMORY;
|
---|
908 | goto done;
|
---|
909 | }
|
---|
910 | }
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Some generic requirements for recognizing an adjacent function:
|
---|
914 | * - alignment fillers that consist of:
|
---|
915 | * - nop
|
---|
916 | * - lea genregX, [genregX (+ 0)]
|
---|
917 | * - push ebp after the filler (can extend this later); aligned at at least a 4 byte boundary
|
---|
918 | */
|
---|
919 | for (int j=0;j<16;j++)
|
---|
920 | {
|
---|
921 | pCurInstrHC = (uint8_t *)CSAMGCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
922 | if (pCurInstrHC == NULL)
|
---|
923 | {
|
---|
924 | Log(("CSAMGCVirtToHCVirt failed for %VRv\n", pCurInstrGC));
|
---|
925 | goto done;
|
---|
926 | }
|
---|
927 | Assert(VALID_PTR(pCurInstrHC));
|
---|
928 |
|
---|
929 | cpu.mode = (fCode32) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
930 | STAM_PROFILE_START(&pVM->csam.s.StatTimeDisasm, a);
|
---|
931 | #ifdef DEBUG
|
---|
932 | rc2 = CSAMR3DISInstr(pVM, &cpu, pCurInstrGC, pCurInstrHC, &opsize, szOutput);
|
---|
933 | if (VBOX_SUCCESS(rc2)) Log(("CSAM Call Analysis: %s", szOutput));
|
---|
934 | #else
|
---|
935 | rc2 = CSAMR3DISInstr(pVM, &cpu, pCurInstrGC, pCurInstrHC, &opsize, NULL);
|
---|
936 | #endif
|
---|
937 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeDisasm, a);
|
---|
938 | if (VBOX_FAILURE(rc2))
|
---|
939 | {
|
---|
940 | Log(("Disassembly failed at %VRv with %Vrc (probably page not present) -> return to caller\n", pCurInstrGC, rc2));
|
---|
941 | goto done;
|
---|
942 | }
|
---|
943 |
|
---|
944 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrBytesRead, opsize);
|
---|
945 |
|
---|
946 | RCPTRTYPE(uint8_t *) addr = 0;
|
---|
947 | PCSAMPAGE pJmpPage = NULL;
|
---|
948 |
|
---|
949 | if (PAGE_ADDRESS(pCurInstrGC) != PAGE_ADDRESS(pCurInstrGC + opsize - 1))
|
---|
950 | {
|
---|
951 | if (!PGMGstIsPagePresent(pVM, pCurInstrGC + opsize - 1))
|
---|
952 | {
|
---|
953 | /// @todo fault in the page
|
---|
954 | Log(("Page for current instruction %VRv is not present!!\n", pCurInstrGC));
|
---|
955 | goto done;
|
---|
956 | }
|
---|
957 | //all is fine, let's continue
|
---|
958 | csamR3CheckPageRecord(pVM, pCurInstrGC + opsize - 1);
|
---|
959 | }
|
---|
960 |
|
---|
961 | switch (cpu.pCurInstr->opcode)
|
---|
962 | {
|
---|
963 | case OP_NOP:
|
---|
964 | case OP_INT3:
|
---|
965 | break; /* acceptable */
|
---|
966 |
|
---|
967 | case OP_LEA:
|
---|
968 | /* Must be similar to:
|
---|
969 | *
|
---|
970 | * lea esi, [esi]
|
---|
971 | * lea esi, [esi+0]
|
---|
972 | * Any register is allowed as long as source and destination are identical.
|
---|
973 | */
|
---|
974 | if ( cpu.param1.flags != USE_REG_GEN32
|
---|
975 | || ( cpu.param2.flags != USE_REG_GEN32
|
---|
976 | && ( !(cpu.param2.flags & USE_REG_GEN32)
|
---|
977 | || !(cpu.param2.flags & (USE_DISPLACEMENT8|USE_DISPLACEMENT16|USE_DISPLACEMENT32))
|
---|
978 | || cpu.param2.parval != 0
|
---|
979 | )
|
---|
980 | )
|
---|
981 | || cpu.param1.base.reg_gen32 != cpu.param2.base.reg_gen32
|
---|
982 | )
|
---|
983 | {
|
---|
984 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
985 | goto next_function;
|
---|
986 | }
|
---|
987 | break;
|
---|
988 |
|
---|
989 | case OP_PUSH:
|
---|
990 | {
|
---|
991 | if ( (pCurInstrGC & 0x3) != 0
|
---|
992 | || cpu.param1.flags != USE_REG_GEN32
|
---|
993 | || cpu.param1.base.reg_gen32 != USE_REG_EBP
|
---|
994 | )
|
---|
995 | {
|
---|
996 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
997 | goto next_function;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
1001 | {
|
---|
1002 | CSAMCALLEXITREC CallExitRec2;
|
---|
1003 | CallExitRec2.cInstrAfterRet = 0;
|
---|
1004 |
|
---|
1005 | pCacheRec->pCallExitRec = &CallExitRec2;
|
---|
1006 |
|
---|
1007 | /* Analyse the function. */
|
---|
1008 | Log(("Found new function at %VRv\n", pCurInstrGC));
|
---|
1009 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunction);
|
---|
1010 | csamAnalyseCallCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
1011 | }
|
---|
1012 | goto next_function;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | case OP_SUB:
|
---|
1016 | {
|
---|
1017 | if ( (pCurInstrGC & 0x3) != 0
|
---|
1018 | || cpu.param1.flags != USE_REG_GEN32
|
---|
1019 | || cpu.param1.base.reg_gen32 != USE_REG_ESP
|
---|
1020 | )
|
---|
1021 | {
|
---|
1022 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
1023 | goto next_function;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
1027 | {
|
---|
1028 | CSAMCALLEXITREC CallExitRec2;
|
---|
1029 | CallExitRec2.cInstrAfterRet = 0;
|
---|
1030 |
|
---|
1031 | pCacheRec->pCallExitRec = &CallExitRec2;
|
---|
1032 |
|
---|
1033 | /* Analyse the function. */
|
---|
1034 | Log(("Found new function at %VRv\n", pCurInstrGC));
|
---|
1035 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunction);
|
---|
1036 | csamAnalyseCallCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
1037 | }
|
---|
1038 | goto next_function;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | default:
|
---|
1042 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
1043 | goto next_function;
|
---|
1044 | }
|
---|
1045 | /* Mark it as scanned. */
|
---|
1046 | csamMarkCode(pVM, pPage, pCurInstrGC, opsize, true);
|
---|
1047 | pCurInstrGC += opsize;
|
---|
1048 | } /* for at most 16 instructions */
|
---|
1049 | next_function:
|
---|
1050 | ; /* MSVC complains otherwise */
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | done:
|
---|
1054 | pCacheRec->pCallExitRec = pOldCallRec;
|
---|
1055 | return rc;
|
---|
1056 | }
|
---|
1057 | #else
|
---|
1058 | #define csamAnalyseCallCodeStream csamAnalyseCodeStream
|
---|
1059 | #endif
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Disassembles the code stream until the callback function detects a failure or decides everything is acceptable
|
---|
1063 | *
|
---|
1064 | * @returns VBox status code.
|
---|
1065 | * @param pVM The VM to operate on.
|
---|
1066 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
1067 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
1068 | * @param fCode32 16 or 32 bits code
|
---|
1069 | * @param pfnCSAMR3Analyse Callback for testing the disassembled instruction
|
---|
1070 | * @param pUserData User pointer (callback specific)
|
---|
1071 | *
|
---|
1072 | */
|
---|
1073 | static int csamAnalyseCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
1074 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec)
|
---|
1075 | {
|
---|
1076 | DISCPUSTATE cpu;
|
---|
1077 | PCSAMPAGE pPage = (PCSAMPAGE)pUserData;
|
---|
1078 | int rc = VWRN_CONTINUE_ANALYSIS;
|
---|
1079 | uint32_t opsize;
|
---|
1080 | R3PTRTYPE(uint8_t *) pCurInstrHC = 0;
|
---|
1081 | int rc2;
|
---|
1082 |
|
---|
1083 | #ifdef DEBUG
|
---|
1084 | char szOutput[256];
|
---|
1085 | #endif
|
---|
1086 |
|
---|
1087 | LogFlow(("csamAnalyseCodeStream: code at %VRv depth=%d\n", pCurInstrGC, pCacheRec->depth));
|
---|
1088 |
|
---|
1089 | pVM->csam.s.fScanningStarted = true;
|
---|
1090 |
|
---|
1091 | pCacheRec->depth++;
|
---|
1092 | /*
|
---|
1093 | * Limit the call depth. (rather arbitrary upper limit; too low and we won't detect certain
|
---|
1094 | * cpuid instructions in Linux kernels; too high and we waste too much time scanning code)
|
---|
1095 | * (512 is necessary to detect cpuid instructions in Red Hat EL4; see defect 1355)
|
---|
1096 | * @note we are using a lot of stack here. couple of 100k when we go to the full depth (!)
|
---|
1097 | */
|
---|
1098 | if (pCacheRec->depth > 512)
|
---|
1099 | {
|
---|
1100 | LogFlow(("CSAM: maximum calldepth reached for %VRv\n", pCurInstrGC));
|
---|
1101 | pCacheRec->depth--;
|
---|
1102 | return VINF_SUCCESS; //let's not go on forever
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | Assert(!PATMIsPatchGCAddr(pVM, pCurInstrGC));
|
---|
1106 | csamR3CheckPageRecord(pVM, pCurInstrGC);
|
---|
1107 |
|
---|
1108 | while(rc == VWRN_CONTINUE_ANALYSIS)
|
---|
1109 | {
|
---|
1110 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
1111 | {
|
---|
1112 | if (pPage == NULL)
|
---|
1113 | {
|
---|
1114 | /* New address; let's take a look at it. */
|
---|
1115 | pPage = csamCreatePageRecord(pVM, pCurInstrGC, CSAM_TAG_CSAM, fCode32);
|
---|
1116 | if (pPage == NULL)
|
---|
1117 | {
|
---|
1118 | rc = VERR_NO_MEMORY;
|
---|
1119 | goto done;
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 | else
|
---|
1124 | {
|
---|
1125 | LogFlow(("Code at %VRv has been scanned before\n", pCurInstrGC));
|
---|
1126 | rc = VINF_SUCCESS;
|
---|
1127 | goto done;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | pCurInstrHC = (uint8_t *)CSAMGCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
1131 | if (pCurInstrHC == NULL)
|
---|
1132 | {
|
---|
1133 | Log(("CSAMGCVirtToHCVirt failed for %VRv\n", pCurInstrGC));
|
---|
1134 | rc = VERR_PATCHING_REFUSED;
|
---|
1135 | goto done;
|
---|
1136 | }
|
---|
1137 | Assert(VALID_PTR(pCurInstrHC));
|
---|
1138 |
|
---|
1139 | cpu.mode = (fCode32) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
1140 | STAM_PROFILE_START(&pVM->csam.s.StatTimeDisasm, a);
|
---|
1141 | #ifdef DEBUG
|
---|
1142 | rc2 = CSAMR3DISInstr(pVM, &cpu, pCurInstrGC, pCurInstrHC, &opsize, szOutput);
|
---|
1143 | if (VBOX_SUCCESS(rc2)) Log(("CSAM Analysis: %s", szOutput));
|
---|
1144 | #else
|
---|
1145 | rc2 = CSAMR3DISInstr(pVM, &cpu, pCurInstrGC, pCurInstrHC, &opsize, NULL);
|
---|
1146 | #endif
|
---|
1147 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeDisasm, a);
|
---|
1148 | if (VBOX_FAILURE(rc2))
|
---|
1149 | {
|
---|
1150 | Log(("Disassembly failed at %VRv with %Vrc (probably page not present) -> return to caller\n", pCurInstrGC, rc2));
|
---|
1151 | rc = VINF_SUCCESS;
|
---|
1152 | goto done;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrBytesRead, opsize);
|
---|
1156 |
|
---|
1157 | csamMarkCode(pVM, pPage, pCurInstrGC, opsize, true);
|
---|
1158 |
|
---|
1159 | RCPTRTYPE(uint8_t *) addr = 0;
|
---|
1160 | PCSAMPAGE pJmpPage = NULL;
|
---|
1161 |
|
---|
1162 | if (PAGE_ADDRESS(pCurInstrGC) != PAGE_ADDRESS(pCurInstrGC + opsize - 1))
|
---|
1163 | {
|
---|
1164 | if (!PGMGstIsPagePresent(pVM, pCurInstrGC + opsize - 1))
|
---|
1165 | {
|
---|
1166 | /// @todo fault in the page
|
---|
1167 | Log(("Page for current instruction %VRv is not present!!\n", pCurInstrGC));
|
---|
1168 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
1169 | goto next_please;
|
---|
1170 | }
|
---|
1171 | //all is fine, let's continue
|
---|
1172 | csamR3CheckPageRecord(pVM, pCurInstrGC + opsize - 1);
|
---|
1173 | }
|
---|
1174 | /*
|
---|
1175 | * If it's harmless, then don't bother checking it (the disasm tables had better be accurate!)
|
---|
1176 | */
|
---|
1177 | if ((cpu.pCurInstr->optype & ~OPTYPE_RRM_MASK) == OPTYPE_HARMLESS)
|
---|
1178 | {
|
---|
1179 | AssertMsg(pfnCSAMR3Analyse(pVM, &cpu, pInstrGC, pCurInstrGC, pCacheRec, (void *)pPage) == VWRN_CONTINUE_ANALYSIS, ("Instruction incorrectly marked harmless?!?!?\n"));
|
---|
1180 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
1181 | goto next_please;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | #ifdef CSAM_ANALYSE_BEYOND_RET
|
---|
1185 | /* Remember the address of the instruction following the ret in case the parent instruction was a call. */
|
---|
1186 | if ( pCacheRec->pCallExitRec
|
---|
1187 | && cpu.pCurInstr->opcode == OP_RETN
|
---|
1188 | && pCacheRec->pCallExitRec->cInstrAfterRet < CSAM_MAX_CALLEXIT_RET)
|
---|
1189 | {
|
---|
1190 | pCacheRec->pCallExitRec->pInstrAfterRetGC[pCacheRec->pCallExitRec->cInstrAfterRet] = pCurInstrGC + opsize;
|
---|
1191 | pCacheRec->pCallExitRec->cInstrAfterRet++;
|
---|
1192 | }
|
---|
1193 | #endif
|
---|
1194 |
|
---|
1195 | rc = pfnCSAMR3Analyse(pVM, &cpu, pInstrGC, pCurInstrGC, pCacheRec, (void *)pPage);
|
---|
1196 | if (rc == VINF_SUCCESS)
|
---|
1197 | goto done;
|
---|
1198 |
|
---|
1199 | // For our first attempt, we'll handle only simple relative jumps and calls (immediate offset coded in instruction)
|
---|
1200 | if ( ((cpu.pCurInstr->optype & OPTYPE_CONTROLFLOW) && (OP_PARM_VTYPE(cpu.pCurInstr->param1) == OP_PARM_J))
|
---|
1201 | || (cpu.pCurInstr->opcode == OP_CALL && cpu.param1.flags == USE_DISPLACEMENT32)) /* simple indirect call (call dword ptr [address]) */
|
---|
1202 | {
|
---|
1203 | /* We need to parse 'call dword ptr [address]' type of calls to catch cpuid instructions in some recent Linux distributions (e.g. OpenSuse 10.3) */
|
---|
1204 | if ( cpu.pCurInstr->opcode == OP_CALL
|
---|
1205 | && cpu.param1.flags == USE_DISPLACEMENT32)
|
---|
1206 | {
|
---|
1207 | addr = 0;
|
---|
1208 | PGMPhysSimpleReadGCPtr(pVM, &addr, (RTRCUINTPTR)cpu.param1.disp32, sizeof(addr));
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | addr = CSAMResolveBranch(&cpu, pCurInstrGC);
|
---|
1212 |
|
---|
1213 | if (addr == 0)
|
---|
1214 | {
|
---|
1215 | Log(("We don't support far jumps here!! (%08X)\n", cpu.param1.flags));
|
---|
1216 | rc = VINF_SUCCESS;
|
---|
1217 | break;
|
---|
1218 | }
|
---|
1219 | Assert(!PATMIsPatchGCAddr(pVM, addr));
|
---|
1220 |
|
---|
1221 | /* If the target address lies in a patch generated jump, then special action needs to be taken. */
|
---|
1222 | PATMR3DetectConflict(pVM, pCurInstrGC, addr);
|
---|
1223 |
|
---|
1224 | /* Same page? */
|
---|
1225 | if (PAGE_ADDRESS(addr) != PAGE_ADDRESS(pCurInstrGC ))
|
---|
1226 | {
|
---|
1227 | if (!PGMGstIsPagePresent(pVM, addr))
|
---|
1228 | {
|
---|
1229 | Log(("Page for current instruction %VRv is not present!!\n", addr));
|
---|
1230 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
1231 | goto next_please;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* All is fine, let's continue. */
|
---|
1235 | csamR3CheckPageRecord(pVM, addr);
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | pJmpPage = NULL;
|
---|
1239 | if (csamIsCodeScanned(pVM, addr, &pJmpPage) == false)
|
---|
1240 | {
|
---|
1241 | if (pJmpPage == NULL)
|
---|
1242 | {
|
---|
1243 | /* New branch target; let's take a look at it. */
|
---|
1244 | pJmpPage = csamCreatePageRecord(pVM, addr, CSAM_TAG_CSAM, fCode32);
|
---|
1245 | if (pJmpPage == NULL)
|
---|
1246 | {
|
---|
1247 | rc = VERR_NO_MEMORY;
|
---|
1248 | goto done;
|
---|
1249 | }
|
---|
1250 | Assert(pPage);
|
---|
1251 | }
|
---|
1252 | if (cpu.pCurInstr->opcode == OP_CALL)
|
---|
1253 | rc = csamAnalyseCallCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
1254 | else
|
---|
1255 | rc = csamAnalyseCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
1256 |
|
---|
1257 | if (rc != VINF_SUCCESS) {
|
---|
1258 | goto done;
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 | if (cpu.pCurInstr->opcode == OP_JMP)
|
---|
1262 | {//unconditional jump; return to caller
|
---|
1263 | rc = VINF_SUCCESS;
|
---|
1264 | goto done;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
1268 | } //if ((cpu.pCurInstr->optype & OPTYPE_CONTROLFLOW) && (OP_PARM_VTYPE(cpu.pCurInstr->param1) == OP_PARM_J))
|
---|
1269 | #ifdef CSAM_SCAN_JUMP_TABLE
|
---|
1270 | else
|
---|
1271 | if ( cpu.pCurInstr->opcode == OP_JMP
|
---|
1272 | && (cpu.param1.flags & (USE_DISPLACEMENT32|USE_INDEX|USE_SCALE)) == (USE_DISPLACEMENT32|USE_INDEX|USE_SCALE)
|
---|
1273 | )
|
---|
1274 | {
|
---|
1275 | RTRCPTR pJumpTableGC = (RTRCPTR)cpu.param1.disp32;
|
---|
1276 | uint8_t *pJumpTableHC;
|
---|
1277 | int rc2;
|
---|
1278 |
|
---|
1279 | Log(("Jump through jump table\n"));
|
---|
1280 |
|
---|
1281 | rc2 = PGMPhysGCPtr2HCPtr(pVM, pJumpTableGC, (PRTHCPTR)&pJumpTableHC);
|
---|
1282 | if (rc2 == VINF_SUCCESS)
|
---|
1283 | {
|
---|
1284 | for (uint32_t i=0;i<2;i++)
|
---|
1285 | {
|
---|
1286 | uint64_t fFlags;
|
---|
1287 |
|
---|
1288 | addr = pJumpTableGC + cpu.param1.scale * i;
|
---|
1289 | /* Same page? */
|
---|
1290 | if (PAGE_ADDRESS(addr) != PAGE_ADDRESS(pJumpTableGC))
|
---|
1291 | break;
|
---|
1292 |
|
---|
1293 | addr = *(RTRCPTR *)(pJumpTableHC + cpu.param1.scale * i);
|
---|
1294 |
|
---|
1295 | rc2 = PGMGstGetPage(pVM, addr, &fFlags, NULL);
|
---|
1296 | if ( rc2 != VINF_SUCCESS
|
---|
1297 | || (fFlags & X86_PTE_US)
|
---|
1298 | || !(fFlags & X86_PTE_P)
|
---|
1299 | )
|
---|
1300 | break;
|
---|
1301 |
|
---|
1302 | Log(("Jump to %VRv\n", addr));
|
---|
1303 |
|
---|
1304 | pJmpPage = NULL;
|
---|
1305 | if (csamIsCodeScanned(pVM, addr, &pJmpPage) == false)
|
---|
1306 | {
|
---|
1307 | if (pJmpPage == NULL)
|
---|
1308 | {
|
---|
1309 | /* New branch target; let's take a look at it. */
|
---|
1310 | pJmpPage = csamCreatePageRecord(pVM, addr, CSAM_TAG_CSAM, fCode32);
|
---|
1311 | if (pJmpPage == NULL)
|
---|
1312 | {
|
---|
1313 | rc = VERR_NO_MEMORY;
|
---|
1314 | goto done;
|
---|
1315 | }
|
---|
1316 | Assert(pPage);
|
---|
1317 | }
|
---|
1318 | rc = csamAnalyseCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
1319 | if (rc != VINF_SUCCESS) {
|
---|
1320 | goto done;
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 | }
|
---|
1324 | }
|
---|
1325 | }
|
---|
1326 | #endif
|
---|
1327 | if (rc != VWRN_CONTINUE_ANALYSIS) {
|
---|
1328 | break; //done!
|
---|
1329 | }
|
---|
1330 | next_please:
|
---|
1331 | if (cpu.pCurInstr->opcode == OP_JMP)
|
---|
1332 | {
|
---|
1333 | rc = VINF_SUCCESS;
|
---|
1334 | goto done;
|
---|
1335 | }
|
---|
1336 | pCurInstrGC += opsize;
|
---|
1337 | }
|
---|
1338 | done:
|
---|
1339 | pCacheRec->depth--;
|
---|
1340 | return rc;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 |
|
---|
1344 | /**
|
---|
1345 | * Calculates the 64 bits hash value for the current page
|
---|
1346 | *
|
---|
1347 | * @returns hash value
|
---|
1348 | * @param pVM The VM to operate on.
|
---|
1349 | * @param pInstr Page address
|
---|
1350 | */
|
---|
1351 | uint64_t csamR3CalcPageHash(PVM pVM, RTRCPTR pInstr)
|
---|
1352 | {
|
---|
1353 | uint64_t hash = 0;
|
---|
1354 | uint32_t val[5];
|
---|
1355 | int rc;
|
---|
1356 |
|
---|
1357 | Assert((pInstr & PAGE_OFFSET_MASK) == 0);
|
---|
1358 |
|
---|
1359 | rc = PGMPhysSimpleReadGCPtr(pVM, &val[0], pInstr, sizeof(val[0]));
|
---|
1360 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1361 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1362 | {
|
---|
1363 | Log(("csamR3CalcPageHash: page %VRv not present!!\n", pInstr));
|
---|
1364 | return ~0ULL;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | rc = PGMPhysSimpleReadGCPtr(pVM, &val[1], pInstr+1024, sizeof(val[0]));
|
---|
1368 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1369 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1370 | {
|
---|
1371 | Log(("csamR3CalcPageHash: page %VRv not present!!\n", pInstr));
|
---|
1372 | return ~0ULL;
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | rc = PGMPhysSimpleReadGCPtr(pVM, &val[2], pInstr+2048, sizeof(val[0]));
|
---|
1376 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1377 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1378 | {
|
---|
1379 | Log(("csamR3CalcPageHash: page %VRv not present!!\n", pInstr));
|
---|
1380 | return ~0ULL;
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | rc = PGMPhysSimpleReadGCPtr(pVM, &val[3], pInstr+3072, sizeof(val[0]));
|
---|
1384 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1385 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1386 | {
|
---|
1387 | Log(("csamR3CalcPageHash: page %VRv not present!!\n", pInstr));
|
---|
1388 | return ~0ULL;
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | rc = PGMPhysSimpleReadGCPtr(pVM, &val[4], pInstr+4092, sizeof(val[0]));
|
---|
1392 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1393 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1394 | {
|
---|
1395 | Log(("csamR3CalcPageHash: page %VRv not present!!\n", pInstr));
|
---|
1396 | return ~0ULL;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | // don't want to get division by zero traps
|
---|
1400 | val[2] |= 1;
|
---|
1401 | val[4] |= 1;
|
---|
1402 |
|
---|
1403 | hash = (uint64_t)val[0] * (uint64_t)val[1] / (uint64_t)val[2] + (val[3]%val[4]);
|
---|
1404 | return (hash == ~0ULL) ? hash - 1 : hash;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 |
|
---|
1408 | /**
|
---|
1409 | * Notify CSAM of a page flush
|
---|
1410 | *
|
---|
1411 | * @returns VBox status code
|
---|
1412 | * @param pVM The VM to operate on.
|
---|
1413 | * @param addr GC address of the page to flush
|
---|
1414 | * @param fRemovePage Page removal flag
|
---|
1415 | */
|
---|
1416 | static int csamFlushPage(PVM pVM, RTRCPTR addr, bool fRemovePage)
|
---|
1417 | {
|
---|
1418 | PCSAMPAGEREC pPageRec;
|
---|
1419 | int rc;
|
---|
1420 | RTGCPHYS GCPhys = 0;
|
---|
1421 | uint64_t fFlags = 0;
|
---|
1422 |
|
---|
1423 | if (!CSAMIsEnabled(pVM))
|
---|
1424 | return VINF_SUCCESS;
|
---|
1425 |
|
---|
1426 | STAM_PROFILE_START(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1427 |
|
---|
1428 | addr = addr & PAGE_BASE_GC_MASK;
|
---|
1429 |
|
---|
1430 | /*
|
---|
1431 | * Note: searching for the page in our tree first is more expensive (skipped flushes are two orders of magnitude more common)
|
---|
1432 | */
|
---|
1433 | if (pVM->csam.s.pPageTree == NULL)
|
---|
1434 | {
|
---|
1435 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1436 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | rc = PGMGstGetPage(pVM, addr, &fFlags, &GCPhys);
|
---|
1440 | /* Returned at a very early stage (no paging yet presumably). */
|
---|
1441 | if (rc == VERR_NOT_SUPPORTED)
|
---|
1442 | {
|
---|
1443 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1444 | return rc;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | if (VBOX_SUCCESS(rc))
|
---|
1448 | {
|
---|
1449 | if ( (fFlags & X86_PTE_US)
|
---|
1450 | || rc == VERR_PGM_PHYS_PAGE_RESERVED
|
---|
1451 | )
|
---|
1452 | {
|
---|
1453 | /* User page -> not relevant for us. */
|
---|
1454 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
|
---|
1455 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1456 | return VINF_SUCCESS;
|
---|
1457 | }
|
---|
1458 | }
|
---|
1459 | else
|
---|
1460 | if (rc != VERR_PAGE_NOT_PRESENT && rc != VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1461 | AssertMsgFailed(("PGMR3GetPage %VRv failed with %Vrc\n", addr, rc));
|
---|
1462 |
|
---|
1463 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)addr);
|
---|
1464 | if (pPageRec)
|
---|
1465 | {
|
---|
1466 | if ( GCPhys == pPageRec->page.GCPhys
|
---|
1467 | && (fFlags & X86_PTE_P))
|
---|
1468 | {
|
---|
1469 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
|
---|
1470 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1471 | return VINF_SUCCESS;
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | Log(("CSAMR3FlushPage: page %VRv has changed -> FLUSH (rc=%Vrc) (Phys: %VGp vs %VGp)\n", addr, rc, GCPhys, pPageRec->page.GCPhys));
|
---|
1475 |
|
---|
1476 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushes, 1);
|
---|
1477 |
|
---|
1478 | if (fRemovePage)
|
---|
1479 | csamRemovePageRecord(pVM, addr);
|
---|
1480 | else
|
---|
1481 | {
|
---|
1482 | CSAMMarkPage(pVM, addr, false);
|
---|
1483 | pPageRec->page.GCPhys = 0;
|
---|
1484 | pPageRec->page.fFlags = 0;
|
---|
1485 | rc = PGMGstGetPage(pVM, addr, &pPageRec->page.fFlags, &pPageRec->page.GCPhys);
|
---|
1486 | if (rc == VINF_SUCCESS)
|
---|
1487 | pPageRec->page.u64Hash = csamR3CalcPageHash(pVM, addr);
|
---|
1488 |
|
---|
1489 | if (pPageRec->page.pBitmap == NULL)
|
---|
1490 | {
|
---|
1491 | pPageRec->page.pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, CSAM_PAGE_BITMAP_SIZE);
|
---|
1492 | Assert(pPageRec->page.pBitmap);
|
---|
1493 | if (pPageRec->page.pBitmap == NULL)
|
---|
1494 | return VERR_NO_MEMORY;
|
---|
1495 | }
|
---|
1496 | else
|
---|
1497 | memset(pPageRec->page.pBitmap, 0, CSAM_PAGE_BITMAP_SIZE);
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 |
|
---|
1501 | /*
|
---|
1502 | * Inform patch manager about the flush; no need to repeat the above check twice.
|
---|
1503 | */
|
---|
1504 | PATMR3FlushPage(pVM, addr);
|
---|
1505 |
|
---|
1506 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1507 | return VINF_SUCCESS;
|
---|
1508 | }
|
---|
1509 | else
|
---|
1510 | {
|
---|
1511 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
1512 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | /**
|
---|
1517 | * Notify CSAM of a page flush
|
---|
1518 | *
|
---|
1519 | * @returns VBox status code
|
---|
1520 | * @param pVM The VM to operate on.
|
---|
1521 | * @param addr GC address of the page to flush
|
---|
1522 | */
|
---|
1523 | VMMR3DECL(int) CSAMR3FlushPage(PVM pVM, RTRCPTR addr)
|
---|
1524 | {
|
---|
1525 | return csamFlushPage(pVM, addr, true /* remove page record */);
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | /**
|
---|
1529 | * Remove a CSAM monitored page. Use with care!
|
---|
1530 | *
|
---|
1531 | * @returns VBox status code
|
---|
1532 | * @param pVM The VM to operate on.
|
---|
1533 | * @param addr GC address of the page to flush
|
---|
1534 | */
|
---|
1535 | VMMR3DECL(int) CSAMR3RemovePage(PVM pVM, RTRCPTR addr)
|
---|
1536 | {
|
---|
1537 | PCSAMPAGEREC pPageRec;
|
---|
1538 | int rc;
|
---|
1539 |
|
---|
1540 | addr = addr & PAGE_BASE_GC_MASK;
|
---|
1541 |
|
---|
1542 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)addr);
|
---|
1543 | if (pPageRec)
|
---|
1544 | {
|
---|
1545 | rc = csamRemovePageRecord(pVM, addr);
|
---|
1546 | if (VBOX_SUCCESS(rc))
|
---|
1547 | PATMR3FlushPage(pVM, addr);
|
---|
1548 | return VINF_SUCCESS;
|
---|
1549 | }
|
---|
1550 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /**
|
---|
1554 | * Check a page record in case a page has been changed
|
---|
1555 | *
|
---|
1556 | * @returns VBox status code. (trap handled or not)
|
---|
1557 | * @param pVM The VM to operate on.
|
---|
1558 | * @param pInstrGC GC instruction pointer
|
---|
1559 | */
|
---|
1560 | int csamR3CheckPageRecord(PVM pVM, RTRCPTR pInstrGC)
|
---|
1561 | {
|
---|
1562 | PCSAMPAGEREC pPageRec;
|
---|
1563 | uint64_t u64hash;
|
---|
1564 |
|
---|
1565 | pInstrGC = pInstrGC & PAGE_BASE_GC_MASK;
|
---|
1566 |
|
---|
1567 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pInstrGC);
|
---|
1568 | if (pPageRec)
|
---|
1569 | {
|
---|
1570 | u64hash = csamR3CalcPageHash(pVM, pInstrGC);
|
---|
1571 | if (u64hash != pPageRec->page.u64Hash)
|
---|
1572 | csamFlushPage(pVM, pInstrGC, false /* don't remove page record */);
|
---|
1573 | }
|
---|
1574 | else
|
---|
1575 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
1576 |
|
---|
1577 | return VINF_SUCCESS;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | /**
|
---|
1581 | * Returns monitor description based on CSAM tag
|
---|
1582 | *
|
---|
1583 | * @return description string
|
---|
1584 | * @param enmTag Owner tag
|
---|
1585 | */
|
---|
1586 | const char *csamGetMonitorDescription(CSAMTAG enmTag)
|
---|
1587 | {
|
---|
1588 | if (enmTag == CSAM_TAG_PATM)
|
---|
1589 | return "CSAM-PATM self-modifying code monitor handler";
|
---|
1590 | else
|
---|
1591 | if (enmTag == CSAM_TAG_REM)
|
---|
1592 | return "CSAM-REM self-modifying code monitor handler";
|
---|
1593 | Assert(enmTag == CSAM_TAG_CSAM);
|
---|
1594 | return "CSAM self-modifying code monitor handler";
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | /**
|
---|
1598 | * Adds page record to our lookup tree
|
---|
1599 | *
|
---|
1600 | * @returns CSAMPAGE ptr or NULL if failure
|
---|
1601 | * @param pVM The VM to operate on.
|
---|
1602 | * @param GCPtr Page address
|
---|
1603 | * @param enmTag Owner tag
|
---|
1604 | * @param fCode32 16 or 32 bits code
|
---|
1605 | * @param fMonitorInvalidation Monitor page invalidation flag
|
---|
1606 | */
|
---|
1607 | static PCSAMPAGE csamCreatePageRecord(PVM pVM, RTRCPTR GCPtr, CSAMTAG enmTag, bool fCode32, bool fMonitorInvalidation)
|
---|
1608 | {
|
---|
1609 | PCSAMPAGEREC pPage;
|
---|
1610 | int rc;
|
---|
1611 | bool ret;
|
---|
1612 |
|
---|
1613 | Log(("New page record for %VRv\n", GCPtr & PAGE_BASE_GC_MASK));
|
---|
1614 |
|
---|
1615 | pPage = (PCSAMPAGEREC)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, sizeof(CSAMPAGEREC));
|
---|
1616 | if (pPage == NULL)
|
---|
1617 | {
|
---|
1618 | AssertMsgFailed(("csamCreatePageRecord: Out of memory!!!!\n"));
|
---|
1619 | return NULL;
|
---|
1620 | }
|
---|
1621 | /* Round down to page boundary. */
|
---|
1622 | GCPtr = (GCPtr & PAGE_BASE_GC_MASK);
|
---|
1623 | pPage->Core.Key = (AVLPVKEY)GCPtr;
|
---|
1624 | pPage->page.pPageGC = GCPtr;
|
---|
1625 | pPage->page.fCode32 = fCode32;
|
---|
1626 | pPage->page.fMonitorInvalidation = fMonitorInvalidation;
|
---|
1627 | pPage->page.enmTag = enmTag;
|
---|
1628 | pPage->page.fMonitorActive = false;
|
---|
1629 | pPage->page.pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, PAGE_SIZE/sizeof(uint8_t));
|
---|
1630 | rc = PGMGstGetPage(pVM, GCPtr, &pPage->page.fFlags, &pPage->page.GCPhys);
|
---|
1631 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1632 |
|
---|
1633 | pPage->page.u64Hash = csamR3CalcPageHash(pVM, GCPtr);
|
---|
1634 | ret = RTAvlPVInsert(&pVM->csam.s.pPageTree, &pPage->Core);
|
---|
1635 | Assert(ret);
|
---|
1636 |
|
---|
1637 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
1638 | AssertRelease(!fInCSAMCodePageInvalidate);
|
---|
1639 |
|
---|
1640 | switch (enmTag)
|
---|
1641 | {
|
---|
1642 | case CSAM_TAG_PATM:
|
---|
1643 | case CSAM_TAG_REM:
|
---|
1644 | #ifdef CSAM_MONITOR_CSAM_CODE_PAGES
|
---|
1645 | case CSAM_TAG_CSAM:
|
---|
1646 | #endif
|
---|
1647 | {
|
---|
1648 | int rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, GCPtr, GCPtr + (PAGE_SIZE - 1) /* inclusive! */,
|
---|
1649 | (fMonitorInvalidation) ? CSAMCodePageInvalidate : 0, CSAMCodePageWriteHandler, "CSAMGCCodePageWriteHandler", 0,
|
---|
1650 | csamGetMonitorDescription(enmTag));
|
---|
1651 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT, ("PGMR3HandlerVirtualRegisterEx %VRv failed with %Vrc\n", GCPtr, rc));
|
---|
1652 | if (VBOX_FAILURE(rc))
|
---|
1653 | Log(("PGMR3HandlerVirtualRegisterEx for %VRv failed with %Vrc\n", GCPtr, rc));
|
---|
1654 |
|
---|
1655 | /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
|
---|
1656 |
|
---|
1657 | /* Prefetch it in case it's not there yet. */
|
---|
1658 | rc = PGMPrefetchPage(pVM, GCPtr);
|
---|
1659 | AssertRC(rc);
|
---|
1660 |
|
---|
1661 | rc = PGMShwModifyPage(pVM, GCPtr, 1, 0, ~(uint64_t)X86_PTE_RW);
|
---|
1662 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1663 |
|
---|
1664 | pPage->page.fMonitorActive = true;
|
---|
1665 | STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
|
---|
1666 | break;
|
---|
1667 | }
|
---|
1668 | default:
|
---|
1669 | break; /* to shut up GCC */
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | Log(("csamCreatePageRecord %VRv HCPhys=%VGp\n", GCPtr, pPage->page.GCPhys));
|
---|
1673 |
|
---|
1674 | #ifdef VBOX_WITH_STATISTICS
|
---|
1675 | switch (enmTag)
|
---|
1676 | {
|
---|
1677 | case CSAM_TAG_CSAM:
|
---|
1678 | STAM_COUNTER_INC(&pVM->csam.s.StatPageCSAM);
|
---|
1679 | break;
|
---|
1680 | case CSAM_TAG_PATM:
|
---|
1681 | STAM_COUNTER_INC(&pVM->csam.s.StatPagePATM);
|
---|
1682 | break;
|
---|
1683 | case CSAM_TAG_REM:
|
---|
1684 | STAM_COUNTER_INC(&pVM->csam.s.StatPageREM);
|
---|
1685 | break;
|
---|
1686 | default:
|
---|
1687 | break; /* to shut up GCC */
|
---|
1688 | }
|
---|
1689 | #endif
|
---|
1690 |
|
---|
1691 | #endif
|
---|
1692 |
|
---|
1693 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPages);
|
---|
1694 | if (fMonitorInvalidation)
|
---|
1695 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
|
---|
1696 |
|
---|
1697 | return &pPage->page;
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | /**
|
---|
1701 | * Monitors a code page (if not already monitored)
|
---|
1702 | *
|
---|
1703 | * @returns VBox status code
|
---|
1704 | * @param pVM The VM to operate on.
|
---|
1705 | * @param pPageAddrGC The page to monitor
|
---|
1706 | * @param enmTag Monitor tag
|
---|
1707 | */
|
---|
1708 | VMMR3DECL(int) CSAMR3MonitorPage(PVM pVM, RTRCPTR pPageAddrGC, CSAMTAG enmTag)
|
---|
1709 | {
|
---|
1710 | PCSAMPAGEREC pPageRec = NULL;
|
---|
1711 | int rc;
|
---|
1712 | bool fMonitorInvalidation;
|
---|
1713 |
|
---|
1714 | /* Dirty pages must be handled before calling this function!. */
|
---|
1715 | Assert(!pVM->csam.s.cDirtyPages);
|
---|
1716 |
|
---|
1717 | if (pVM->csam.s.fScanningStarted == false)
|
---|
1718 | return VINF_SUCCESS; /* too early */
|
---|
1719 |
|
---|
1720 | pPageAddrGC &= PAGE_BASE_GC_MASK;
|
---|
1721 |
|
---|
1722 | Log(("CSAMR3MonitorPage %VRv %d\n", pPageAddrGC, enmTag));
|
---|
1723 |
|
---|
1724 | /** @todo implicit assumption */
|
---|
1725 | fMonitorInvalidation = (enmTag == CSAM_TAG_PATM);
|
---|
1726 |
|
---|
1727 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pPageAddrGC);
|
---|
1728 | if (pPageRec == NULL)
|
---|
1729 | {
|
---|
1730 | uint64_t fFlags;
|
---|
1731 |
|
---|
1732 | rc = PGMGstGetPage(pVM, pPageAddrGC, &fFlags, NULL);
|
---|
1733 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
1734 | if ( rc == VINF_SUCCESS
|
---|
1735 | && (fFlags & X86_PTE_US))
|
---|
1736 | {
|
---|
1737 | /* We don't care about user pages. */
|
---|
1738 | STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
|
---|
1739 | return VINF_SUCCESS;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | csamCreatePageRecord(pVM, pPageAddrGC, enmTag, true /* 32 bits code */, fMonitorInvalidation);
|
---|
1743 |
|
---|
1744 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pPageAddrGC);
|
---|
1745 | Assert(pPageRec);
|
---|
1746 | }
|
---|
1747 | /** @todo reference count */
|
---|
1748 |
|
---|
1749 | #ifdef CSAM_MONITOR_CSAM_CODE_PAGES
|
---|
1750 | Assert(pPageRec->page.fMonitorActive);
|
---|
1751 | #endif
|
---|
1752 |
|
---|
1753 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
1754 | if (!pPageRec->page.fMonitorActive)
|
---|
1755 | {
|
---|
1756 | Log(("CSAMR3MonitorPage: activate monitoring for %VRv\n", pPageAddrGC));
|
---|
1757 |
|
---|
1758 | rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, pPageAddrGC, pPageAddrGC + (PAGE_SIZE - 1) /* inclusive! */,
|
---|
1759 | (fMonitorInvalidation) ? CSAMCodePageInvalidate : 0, CSAMCodePageWriteHandler, "CSAMGCCodePageWriteHandler", 0,
|
---|
1760 | csamGetMonitorDescription(enmTag));
|
---|
1761 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT, ("PGMR3HandlerVirtualRegisterEx %VRv failed with %Vrc\n", pPageAddrGC, rc));
|
---|
1762 | if (VBOX_FAILURE(rc))
|
---|
1763 | Log(("PGMR3HandlerVirtualRegisterEx for %VRv failed with %Vrc\n", pPageAddrGC, rc));
|
---|
1764 |
|
---|
1765 | /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
|
---|
1766 |
|
---|
1767 | /* Prefetch it in case it's not there yet. */
|
---|
1768 | rc = PGMPrefetchPage(pVM, pPageAddrGC);
|
---|
1769 | AssertRC(rc);
|
---|
1770 |
|
---|
1771 | rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
|
---|
1772 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1773 |
|
---|
1774 | STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
|
---|
1775 |
|
---|
1776 | pPageRec->page.fMonitorActive = true;
|
---|
1777 | pPageRec->page.fMonitorInvalidation = fMonitorInvalidation;
|
---|
1778 | }
|
---|
1779 | else
|
---|
1780 | if ( !pPageRec->page.fMonitorInvalidation
|
---|
1781 | && fMonitorInvalidation)
|
---|
1782 | {
|
---|
1783 | Assert(pPageRec->page.fMonitorActive);
|
---|
1784 | PGMHandlerVirtualChangeInvalidateCallback(pVM, pPageRec->page.pPageGC, CSAMCodePageInvalidate);
|
---|
1785 | pPageRec->page.fMonitorInvalidation = true;
|
---|
1786 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
|
---|
1787 |
|
---|
1788 | /* Prefetch it in case it's not there yet. */
|
---|
1789 | rc = PGMPrefetchPage(pVM, pPageAddrGC);
|
---|
1790 | AssertRC(rc);
|
---|
1791 |
|
---|
1792 | /* Make sure it's readonly. Page invalidation may have modified the attributes. */
|
---|
1793 | rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
|
---|
1794 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | #if 0 /* def VBOX_STRICT -> very annoying) */
|
---|
1798 | if (pPageRec->page.fMonitorActive)
|
---|
1799 | {
|
---|
1800 | uint64_t fPageShw;
|
---|
1801 | RTHCPHYS GCPhys;
|
---|
1802 | rc = PGMShwGetPage(pVM, pPageAddrGC, &fPageShw, &GCPhys);
|
---|
1803 | // AssertMsg( (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1804 | // || !(fPageShw & X86_PTE_RW)
|
---|
1805 | // || (pPageRec->page.GCPhys == 0), ("Shadow page flags for %VRv (%VHp) aren't readonly (%VX64)!!\n", pPageAddrGC, GCPhys, fPageShw));
|
---|
1806 | }
|
---|
1807 | #endif
|
---|
1808 |
|
---|
1809 | if (pPageRec->page.GCPhys == 0)
|
---|
1810 | {
|
---|
1811 | /* Prefetch it in case it's not there yet. */
|
---|
1812 | rc = PGMPrefetchPage(pVM, pPageAddrGC);
|
---|
1813 | AssertRC(rc);
|
---|
1814 | /* The page was changed behind our back. It won't be made read-only until the next SyncCR3, so force it here. */
|
---|
1815 | rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
|
---|
1816 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1817 | }
|
---|
1818 | #endif /* CSAM_MONITOR_CODE_PAGES */
|
---|
1819 | return VINF_SUCCESS;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | /**
|
---|
1823 | * Unmonitors a code page
|
---|
1824 | *
|
---|
1825 | * @returns VBox status code
|
---|
1826 | * @param pVM The VM to operate on.
|
---|
1827 | * @param pPageAddrGC The page to monitor
|
---|
1828 | * @param enmTag Monitor tag
|
---|
1829 | */
|
---|
1830 | VMMR3DECL(int) CSAMR3UnmonitorPage(PVM pVM, RTRCPTR pPageAddrGC, CSAMTAG enmTag)
|
---|
1831 | {
|
---|
1832 | pPageAddrGC &= PAGE_BASE_GC_MASK;
|
---|
1833 |
|
---|
1834 | Log(("CSAMR3UnmonitorPage %VRv %d\n", pPageAddrGC, enmTag));
|
---|
1835 |
|
---|
1836 | Assert(enmTag == CSAM_TAG_REM);
|
---|
1837 |
|
---|
1838 | #ifdef VBOX_STRICT
|
---|
1839 | PCSAMPAGEREC pPageRec;
|
---|
1840 |
|
---|
1841 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pPageAddrGC);
|
---|
1842 | Assert(pPageRec && pPageRec->page.enmTag == enmTag);
|
---|
1843 | #endif
|
---|
1844 | return CSAMR3RemovePage(pVM, pPageAddrGC);
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | /**
|
---|
1848 | * Removes a page record from our lookup tree
|
---|
1849 | *
|
---|
1850 | * @returns VBox status code
|
---|
1851 | * @param pVM The VM to operate on.
|
---|
1852 | * @param GCPtr Page address
|
---|
1853 | */
|
---|
1854 | static int csamRemovePageRecord(PVM pVM, RTRCPTR GCPtr)
|
---|
1855 | {
|
---|
1856 | PCSAMPAGEREC pPageRec;
|
---|
1857 |
|
---|
1858 | Log(("csamRemovePageRecord %VRv\n", GCPtr));
|
---|
1859 | pPageRec = (PCSAMPAGEREC)RTAvlPVRemove(&pVM->csam.s.pPageTree, (AVLPVKEY)GCPtr);
|
---|
1860 |
|
---|
1861 | if (pPageRec)
|
---|
1862 | {
|
---|
1863 | STAM_COUNTER_INC(&pVM->csam.s.StatNrRemovedPages);
|
---|
1864 |
|
---|
1865 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
1866 | if (pPageRec->page.fMonitorActive)
|
---|
1867 | {
|
---|
1868 | /* @todo -> this is expensive (cr3 reload)!!!
|
---|
1869 | * if this happens often, then reuse it instead!!!
|
---|
1870 | */
|
---|
1871 | Assert(!fInCSAMCodePageInvalidate);
|
---|
1872 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageMonitor);
|
---|
1873 | PGMHandlerVirtualDeregister(pVM, GCPtr);
|
---|
1874 | }
|
---|
1875 | if (pPageRec->page.enmTag == CSAM_TAG_PATM)
|
---|
1876 | {
|
---|
1877 | /* Make sure the recompiler flushes its cache as this page is no longer monitored. */
|
---|
1878 | STAM_COUNTER_INC(&pVM->csam.s.StatPageRemoveREMFlush);
|
---|
1879 | CPUMSetChangedFlags(pVM, CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
1880 | }
|
---|
1881 | #endif
|
---|
1882 |
|
---|
1883 | #ifdef VBOX_WITH_STATISTICS
|
---|
1884 | switch (pPageRec->page.enmTag)
|
---|
1885 | {
|
---|
1886 | case CSAM_TAG_CSAM:
|
---|
1887 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageCSAM);
|
---|
1888 | break;
|
---|
1889 | case CSAM_TAG_PATM:
|
---|
1890 | STAM_COUNTER_DEC(&pVM->csam.s.StatPagePATM);
|
---|
1891 | break;
|
---|
1892 | case CSAM_TAG_REM:
|
---|
1893 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageREM);
|
---|
1894 | break;
|
---|
1895 | default:
|
---|
1896 | break; /* to shut up GCC */
|
---|
1897 | }
|
---|
1898 | #endif
|
---|
1899 |
|
---|
1900 | if (pPageRec->page.pBitmap) MMR3HeapFree(pPageRec->page.pBitmap);
|
---|
1901 | MMR3HeapFree(pPageRec);
|
---|
1902 | }
|
---|
1903 | else
|
---|
1904 | AssertFailed();
|
---|
1905 |
|
---|
1906 | return VINF_SUCCESS;
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | /**
|
---|
1910 | * Callback for delayed writes from non-EMT threads
|
---|
1911 | *
|
---|
1912 | * @param pVM VM Handle.
|
---|
1913 | * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
|
---|
1914 | * @param cbBuf How much it's reading/writing.
|
---|
1915 | */
|
---|
1916 | static DECLCALLBACK(void) CSAMDelayedWriteHandler(PVM pVM, RTRCPTR GCPtr, size_t cbBuf)
|
---|
1917 | {
|
---|
1918 | int rc = PATMR3PatchWrite(pVM, GCPtr, cbBuf);
|
---|
1919 | AssertRC(rc);
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | /**
|
---|
1923 | * #PF Handler callback for virtual access handler ranges.
|
---|
1924 | *
|
---|
1925 | * Important to realize that a physical page in a range can have aliases, and
|
---|
1926 | * for ALL and WRITE handlers these will also trigger.
|
---|
1927 | *
|
---|
1928 | * @returns VINF_SUCCESS if the handler have carried out the operation.
|
---|
1929 | * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
|
---|
1930 | * @param pVM VM Handle.
|
---|
1931 | * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
|
---|
1932 | * @param pvPtr The HC mapping of that address.
|
---|
1933 | * @param pvBuf What the guest is reading/writing.
|
---|
1934 | * @param cbBuf How much it's reading/writing.
|
---|
1935 | * @param enmAccessType The access type.
|
---|
1936 | * @param pvUser User argument.
|
---|
1937 | */
|
---|
1938 | static DECLCALLBACK(int) CSAMCodePageWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
|
---|
1939 | {
|
---|
1940 | int rc;
|
---|
1941 |
|
---|
1942 | Assert(enmAccessType == PGMACCESSTYPE_WRITE);
|
---|
1943 | Log(("CSAMCodePageWriteHandler: write to %VGv size=%d\n", GCPtr, cbBuf));
|
---|
1944 |
|
---|
1945 | if (VM_IS_EMT(pVM))
|
---|
1946 | {
|
---|
1947 | rc = PATMR3PatchWrite(pVM, GCPtr, cbBuf);
|
---|
1948 | }
|
---|
1949 | else
|
---|
1950 | {
|
---|
1951 | /* Queue the write instead otherwise we'll get concurrency issues. */
|
---|
1952 | /** @note in theory not correct to let it write the data first before disabling a patch!
|
---|
1953 | * (if it writes the same data as the patch jump and we replace it with obsolete opcodes)
|
---|
1954 | */
|
---|
1955 | Log(("CSAMCodePageWriteHandler: delayed write!\n"));
|
---|
1956 | AssertCompileSize(RTRCPTR, 4);
|
---|
1957 | rc = VMR3ReqCallEx(pVM, NULL, 0, VMREQFLAGS_NO_WAIT | VMREQFLAGS_VOID,
|
---|
1958 | (PFNRT)CSAMDelayedWriteHandler, 3, pVM, (RTRCPTR)GCPtr, cbBuf);
|
---|
1959 | }
|
---|
1960 | AssertRC(rc);
|
---|
1961 |
|
---|
1962 | return VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 | /**
|
---|
1966 | * #PF Handler callback for invalidation of virtual access handler ranges.
|
---|
1967 | *
|
---|
1968 | * @param pVM VM Handle.
|
---|
1969 | * @param GCPtr The virtual address the guest has changed.
|
---|
1970 | */
|
---|
1971 | static DECLCALLBACK(int) CSAMCodePageInvalidate(PVM pVM, RTGCPTR GCPtr)
|
---|
1972 | {
|
---|
1973 | fInCSAMCodePageInvalidate = true;
|
---|
1974 | LogFlow(("CSAMCodePageInvalidate %VGv\n", GCPtr));
|
---|
1975 | /** @todo We can't remove the page (which unregisters the virtual handler) as we are called from a DoWithAll on the virtual handler tree. Argh. */
|
---|
1976 | csamFlushPage(pVM, GCPtr, false /* don't remove page! */);
|
---|
1977 | fInCSAMCodePageInvalidate = false;
|
---|
1978 | return VINF_SUCCESS;
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | /**
|
---|
1982 | * Check if the current instruction has already been checked before
|
---|
1983 | *
|
---|
1984 | * @returns VBox status code. (trap handled or not)
|
---|
1985 | * @param pVM The VM to operate on.
|
---|
1986 | * @param pInstr Instruction pointer
|
---|
1987 | * @param pPage CSAM patch structure pointer
|
---|
1988 | */
|
---|
1989 | bool csamIsCodeScanned(PVM pVM, RTRCPTR pInstr, PCSAMPAGE *pPage)
|
---|
1990 | {
|
---|
1991 | PCSAMPAGEREC pPageRec;
|
---|
1992 | uint32_t offset;
|
---|
1993 |
|
---|
1994 | STAM_PROFILE_START(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
1995 |
|
---|
1996 | offset = pInstr & PAGE_OFFSET_MASK;
|
---|
1997 | pInstr = pInstr & PAGE_BASE_GC_MASK;
|
---|
1998 |
|
---|
1999 | Assert(pPage);
|
---|
2000 |
|
---|
2001 | if (*pPage && (*pPage)->pPageGC == pInstr)
|
---|
2002 | {
|
---|
2003 | if ((*pPage)->pBitmap == NULL || ASMBitTest((*pPage)->pBitmap, offset))
|
---|
2004 | {
|
---|
2005 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
|
---|
2006 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
2007 | return true;
|
---|
2008 | }
|
---|
2009 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
2010 | return false;
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pInstr);
|
---|
2014 | if (pPageRec)
|
---|
2015 | {
|
---|
2016 | if (pPage) *pPage= &pPageRec->page;
|
---|
2017 | if (pPageRec->page.pBitmap == NULL || ASMBitTest(pPageRec->page.pBitmap, offset))
|
---|
2018 | {
|
---|
2019 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
|
---|
2020 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
2021 | return true;
|
---|
2022 | }
|
---|
2023 | }
|
---|
2024 | else
|
---|
2025 | {
|
---|
2026 | if (pPage) *pPage = NULL;
|
---|
2027 | }
|
---|
2028 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
2029 | return false;
|
---|
2030 | }
|
---|
2031 |
|
---|
2032 | /**
|
---|
2033 | * Mark an instruction in a page as scanned/not scanned
|
---|
2034 | *
|
---|
2035 | * @param pVM The VM to operate on.
|
---|
2036 | * @param pPage Patch structure pointer
|
---|
2037 | * @param pInstr Instruction pointer
|
---|
2038 | * @param opsize Instruction size
|
---|
2039 | * @param fScanned Mark as scanned or not
|
---|
2040 | */
|
---|
2041 | static void csamMarkCode(PVM pVM, PCSAMPAGE pPage, RTRCPTR pInstr, uint32_t opsize, bool fScanned)
|
---|
2042 | {
|
---|
2043 | LogFlow(("csamMarkCodeAsScanned %VRv opsize=%d\n", pInstr, opsize));
|
---|
2044 | CSAMMarkPage(pVM, pInstr, fScanned);
|
---|
2045 |
|
---|
2046 | /** @todo should recreate empty bitmap if !fScanned */
|
---|
2047 | if (pPage->pBitmap == NULL)
|
---|
2048 | return;
|
---|
2049 |
|
---|
2050 | if (fScanned)
|
---|
2051 | {
|
---|
2052 | // retn instructions can be scanned more than once
|
---|
2053 | if (ASMBitTest(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK) == 0)
|
---|
2054 | {
|
---|
2055 | pPage->uSize += opsize;
|
---|
2056 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrInstr, 1);
|
---|
2057 | }
|
---|
2058 | if (pPage->uSize >= PAGE_SIZE)
|
---|
2059 | {
|
---|
2060 | Log(("Scanned full page (%VRv) -> free bitmap\n", pInstr & PAGE_BASE_GC_MASK));
|
---|
2061 | MMR3HeapFree(pPage->pBitmap);
|
---|
2062 | pPage->pBitmap = NULL;
|
---|
2063 | }
|
---|
2064 | else
|
---|
2065 | ASMBitSet(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
|
---|
2066 | }
|
---|
2067 | else
|
---|
2068 | ASMBitClear(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | /**
|
---|
2072 | * Mark an instruction in a page as scanned/not scanned
|
---|
2073 | *
|
---|
2074 | * @returns VBox status code.
|
---|
2075 | * @param pVM The VM to operate on.
|
---|
2076 | * @param pInstr Instruction pointer
|
---|
2077 | * @param opsize Instruction size
|
---|
2078 | * @param fScanned Mark as scanned or not
|
---|
2079 | */
|
---|
2080 | VMMR3DECL(int) CSAMR3MarkCode(PVM pVM, RTRCPTR pInstr, uint32_t opsize, bool fScanned)
|
---|
2081 | {
|
---|
2082 | PCSAMPAGE pPage = 0;
|
---|
2083 |
|
---|
2084 | Assert(!fScanned); /* other case not implemented. */
|
---|
2085 | Assert(!PATMIsPatchGCAddr(pVM, pInstr));
|
---|
2086 |
|
---|
2087 | if (csamIsCodeScanned(pVM, pInstr, &pPage) == false)
|
---|
2088 | {
|
---|
2089 | Assert(fScanned == true); /* other case should not be possible */
|
---|
2090 | return VINF_SUCCESS;
|
---|
2091 | }
|
---|
2092 |
|
---|
2093 | Log(("CSAMR3MarkCode: %VRv size=%d fScanned=%d\n", pInstr, opsize, fScanned));
|
---|
2094 | csamMarkCode(pVM, pPage, pInstr, opsize, fScanned);
|
---|
2095 | return VINF_SUCCESS;
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 |
|
---|
2099 | /**
|
---|
2100 | * Scan and analyse code
|
---|
2101 | *
|
---|
2102 | * @returns VBox status code.
|
---|
2103 | * @param pVM The VM to operate on.
|
---|
2104 | * @param pCtxCore CPU context
|
---|
2105 | * @param pInstrGC Instruction pointer
|
---|
2106 | */
|
---|
2107 | VMMR3DECL(int) CSAMR3CheckCodeEx(PVM pVM, PCPUMCTXCORE pCtxCore, RTRCPTR pInstrGC)
|
---|
2108 | {
|
---|
2109 | if (EMIsRawRing0Enabled(pVM) == false || PATMIsPatchGCAddr(pVM, pInstrGC) == true)
|
---|
2110 | {
|
---|
2111 | // No use
|
---|
2112 | return VINF_SUCCESS;
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | if (CSAMIsEnabled(pVM))
|
---|
2116 | {
|
---|
2117 | /* Assuming 32 bits code for now. */
|
---|
2118 | Assert(SELMGetCpuModeFromSelector(pVM, pCtxCore->eflags, pCtxCore->cs, &pCtxCore->csHid) == CPUMODE_32BIT);
|
---|
2119 |
|
---|
2120 | pInstrGC = SELMToFlat(pVM, DIS_SELREG_CS, pCtxCore, pInstrGC);
|
---|
2121 | return CSAMR3CheckCode(pVM, pInstrGC);
|
---|
2122 | }
|
---|
2123 | return VINF_SUCCESS;
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | /**
|
---|
2127 | * Scan and analyse code
|
---|
2128 | *
|
---|
2129 | * @returns VBox status code.
|
---|
2130 | * @param pVM The VM to operate on.
|
---|
2131 | * @param pInstrGC Instruction pointer (0:32 virtual address)
|
---|
2132 | */
|
---|
2133 | VMMR3DECL(int) CSAMR3CheckCode(PVM pVM, RTRCPTR pInstrGC)
|
---|
2134 | {
|
---|
2135 | int rc;
|
---|
2136 | PCSAMPAGE pPage = NULL;
|
---|
2137 |
|
---|
2138 | if (EMIsRawRing0Enabled(pVM) == false || PATMIsPatchGCAddr(pVM, pInstrGC) == true)
|
---|
2139 | {
|
---|
2140 | // No use
|
---|
2141 | return VINF_SUCCESS;
|
---|
2142 | }
|
---|
2143 |
|
---|
2144 | if (CSAMIsEnabled(pVM))
|
---|
2145 | {
|
---|
2146 | // Cache record for PATMGCVirtToHCVirt
|
---|
2147 | CSAMP2GLOOKUPREC cacheRec = {0};
|
---|
2148 |
|
---|
2149 | STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
|
---|
2150 | rc = csamAnalyseCallCodeStream(pVM, pInstrGC, pInstrGC, true /* 32 bits code */, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
2151 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
|
---|
2152 | if (rc != VINF_SUCCESS)
|
---|
2153 | {
|
---|
2154 | Log(("csamAnalyseCodeStream failed with %d\n", rc));
|
---|
2155 | return rc;
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 | return VINF_SUCCESS;
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | /**
|
---|
2162 | * Flush dirty code pages
|
---|
2163 | *
|
---|
2164 | * @returns VBox status code.
|
---|
2165 | * @param pVM The VM to operate on.
|
---|
2166 | */
|
---|
2167 | static int csamR3FlushDirtyPages(PVM pVM)
|
---|
2168 | {
|
---|
2169 | STAM_PROFILE_START(&pVM->csam.s.StatFlushDirtyPages, a);
|
---|
2170 |
|
---|
2171 | for (uint32_t i=0;i<pVM->csam.s.cDirtyPages;i++)
|
---|
2172 | {
|
---|
2173 | int rc;
|
---|
2174 | PCSAMPAGEREC pPageRec;
|
---|
2175 | RTRCPTR GCPtr = pVM->csam.s.pvDirtyBasePage[i];
|
---|
2176 |
|
---|
2177 | GCPtr = GCPtr & PAGE_BASE_GC_MASK;
|
---|
2178 |
|
---|
2179 | /* Notify the recompiler that this page has been changed. */
|
---|
2180 | REMR3NotifyCodePageChanged(pVM, GCPtr);
|
---|
2181 |
|
---|
2182 | /* Enable write protection again. (use the fault address as it might be an alias) */
|
---|
2183 | rc = PGMShwModifyPage(pVM, pVM->csam.s.pvDirtyFaultPage[i], 1, 0, ~(uint64_t)X86_PTE_RW);
|
---|
2184 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
2185 |
|
---|
2186 | Log(("CSAMR3FlushDirtyPages: flush %VRv (modifypage rc=%Vrc)\n", pVM->csam.s.pvDirtyBasePage[i], rc));
|
---|
2187 |
|
---|
2188 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)GCPtr);
|
---|
2189 | if (pPageRec && pPageRec->page.enmTag == CSAM_TAG_REM)
|
---|
2190 | {
|
---|
2191 | uint64_t fFlags;
|
---|
2192 |
|
---|
2193 | rc = PGMGstGetPage(pVM, GCPtr, &fFlags, NULL);
|
---|
2194 | AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
|
---|
2195 | if ( rc == VINF_SUCCESS
|
---|
2196 | && (fFlags & X86_PTE_US))
|
---|
2197 | {
|
---|
2198 | /* We don't care about user pages. */
|
---|
2199 | csamRemovePageRecord(pVM, GCPtr);
|
---|
2200 | STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
|
---|
2201 | }
|
---|
2202 | }
|
---|
2203 | }
|
---|
2204 | pVM->csam.s.cDirtyPages = 0;
|
---|
2205 | STAM_PROFILE_STOP(&pVM->csam.s.StatFlushDirtyPages, a);
|
---|
2206 | return VINF_SUCCESS;
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | /**
|
---|
2210 | * Flush potential new code pages
|
---|
2211 | *
|
---|
2212 | * @returns VBox status code.
|
---|
2213 | * @param pVM The VM to operate on.
|
---|
2214 | */
|
---|
2215 | static int csamR3FlushCodePages(PVM pVM)
|
---|
2216 | {
|
---|
2217 | for (uint32_t i=0;i<pVM->csam.s.cPossibleCodePages;i++)
|
---|
2218 | {
|
---|
2219 | RTRCPTR GCPtr = pVM->csam.s.pvPossibleCodePage[i];
|
---|
2220 |
|
---|
2221 | GCPtr = GCPtr & PAGE_BASE_GC_MASK;
|
---|
2222 |
|
---|
2223 | Log(("csamR3FlushCodePages: %VRv\n", GCPtr));
|
---|
2224 | PGMShwSetPage(pVM, GCPtr, 1, 0);
|
---|
2225 | /* Resync the page to make sure instruction fetch will fault */
|
---|
2226 | CSAMMarkPage(pVM, GCPtr, false);
|
---|
2227 | }
|
---|
2228 | pVM->csam.s.cPossibleCodePages = 0;
|
---|
2229 | return VINF_SUCCESS;
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 | /**
|
---|
2233 | * Perform any pending actions
|
---|
2234 | *
|
---|
2235 | * @returns VBox status code.
|
---|
2236 | * @param pVM The VM to operate on.
|
---|
2237 | */
|
---|
2238 | VMMR3DECL(int) CSAMR3DoPendingAction(PVM pVM)
|
---|
2239 | {
|
---|
2240 | csamR3FlushDirtyPages(pVM);
|
---|
2241 | csamR3FlushCodePages(pVM);
|
---|
2242 |
|
---|
2243 | VM_FF_CLEAR(pVM, VM_FF_CSAM_PENDING_ACTION);
|
---|
2244 | return VINF_SUCCESS;
|
---|
2245 | }
|
---|
2246 |
|
---|
2247 | /**
|
---|
2248 | * Analyse interrupt and trap gates
|
---|
2249 | *
|
---|
2250 | * @returns VBox status code.
|
---|
2251 | * @param pVM The VM to operate on.
|
---|
2252 | * @param iGate Start gate
|
---|
2253 | * @param cGates Number of gates to check
|
---|
2254 | */
|
---|
2255 | VMMR3DECL(int) CSAMR3CheckGates(PVM pVM, uint32_t iGate, uint32_t cGates)
|
---|
2256 | {
|
---|
2257 | uint16_t cbIDT;
|
---|
2258 | RTRCPTR GCPtrIDT = CPUMGetGuestIDTR(pVM, &cbIDT);
|
---|
2259 | uint32_t iGateEnd;
|
---|
2260 | uint32_t maxGates;
|
---|
2261 | VBOXIDTE aIDT[256];
|
---|
2262 | PVBOXIDTE pGuestIdte;
|
---|
2263 | int rc;
|
---|
2264 |
|
---|
2265 | if (EMIsRawRing0Enabled(pVM) == false)
|
---|
2266 | {
|
---|
2267 | /* Enabling interrupt gates only works when raw ring 0 is enabled. */
|
---|
2268 | //AssertFailed();
|
---|
2269 | return VINF_SUCCESS;
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | /* We only check all gates once during a session */
|
---|
2273 | if ( !pVM->csam.s.fGatesChecked
|
---|
2274 | && cGates != 256)
|
---|
2275 | return VINF_SUCCESS; /* too early */
|
---|
2276 |
|
---|
2277 | /* We only check all gates once during a session */
|
---|
2278 | if ( pVM->csam.s.fGatesChecked
|
---|
2279 | && cGates != 1)
|
---|
2280 | return VINF_SUCCESS; /* ignored */
|
---|
2281 |
|
---|
2282 | Assert(cGates <= 256);
|
---|
2283 | if (!GCPtrIDT || cGates > 256)
|
---|
2284 | return VERR_INVALID_PARAMETER;
|
---|
2285 |
|
---|
2286 | if (cGates != 1)
|
---|
2287 | {
|
---|
2288 | pVM->csam.s.fGatesChecked = true;
|
---|
2289 | for (unsigned i=0;i<RT_ELEMENTS(pVM->csam.s.pvCallInstruction);i++)
|
---|
2290 | {
|
---|
2291 | RTRCPTR pHandler = pVM->csam.s.pvCallInstruction[i];
|
---|
2292 |
|
---|
2293 | if (pHandler)
|
---|
2294 | {
|
---|
2295 | CSAMP2GLOOKUPREC cacheRec = {0}; /* Cache record for PATMGCVirtToHCVirt. */
|
---|
2296 | PCSAMPAGE pPage = NULL;
|
---|
2297 |
|
---|
2298 | Log(("CSAMCheckGates: checking previous call instruction %VRv\n", pHandler));
|
---|
2299 | STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
|
---|
2300 | rc = csamAnalyseCodeStream(pVM, pHandler, pHandler, true, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
2301 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
|
---|
2302 | if (rc != VINF_SUCCESS)
|
---|
2303 | {
|
---|
2304 | Log(("CSAMCheckGates: csamAnalyseCodeStream failed with %d\n", rc));
|
---|
2305 | continue;
|
---|
2306 | }
|
---|
2307 | }
|
---|
2308 | }
|
---|
2309 | }
|
---|
2310 |
|
---|
2311 | /* Determine valid upper boundary. */
|
---|
2312 | maxGates = (cbIDT+1) / sizeof(VBOXIDTE);
|
---|
2313 | Assert(iGate < maxGates);
|
---|
2314 | if (iGate > maxGates)
|
---|
2315 | return VERR_INVALID_PARAMETER;
|
---|
2316 |
|
---|
2317 | if (iGate + cGates > maxGates)
|
---|
2318 | cGates = maxGates - iGate;
|
---|
2319 |
|
---|
2320 | GCPtrIDT = GCPtrIDT + iGate * sizeof(VBOXIDTE);
|
---|
2321 | iGateEnd = iGate + cGates;
|
---|
2322 |
|
---|
2323 | STAM_PROFILE_START(&pVM->csam.s.StatCheckGates, a);
|
---|
2324 |
|
---|
2325 | /*
|
---|
2326 | * Get IDT entries.
|
---|
2327 | */
|
---|
2328 | if (PAGE_ADDRESS(GCPtrIDT) == PAGE_ADDRESS(GCPtrIDT+cGates*sizeof(VBOXIDTE)))
|
---|
2329 | {
|
---|
2330 | /* Just convert the IDT address to a HC pointer. The whole IDT fits in one page. */
|
---|
2331 | rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrIDT, (PRTHCPTR)&pGuestIdte);
|
---|
2332 | if (VBOX_FAILURE(rc))
|
---|
2333 | {
|
---|
2334 | AssertMsgRC(rc, ("Failed to read IDTE! rc=%Vrc\n", rc));
|
---|
2335 | STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
|
---|
2336 | return rc;
|
---|
2337 | }
|
---|
2338 | }
|
---|
2339 | else
|
---|
2340 | {
|
---|
2341 | /* Slow method when it crosses a page boundary. */
|
---|
2342 | rc = PGMPhysSimpleReadGCPtr(pVM, aIDT, GCPtrIDT, cGates*sizeof(VBOXIDTE));
|
---|
2343 | if (VBOX_FAILURE(rc))
|
---|
2344 | {
|
---|
2345 | AssertMsgRC(rc, ("Failed to read IDTE! rc=%Vrc\n", rc));
|
---|
2346 | STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
|
---|
2347 | return rc;
|
---|
2348 | }
|
---|
2349 | pGuestIdte = &aIDT[0];
|
---|
2350 | }
|
---|
2351 |
|
---|
2352 | for (/*iGate*/; iGate<iGateEnd; iGate++, pGuestIdte++)
|
---|
2353 | {
|
---|
2354 | Assert(TRPMR3GetGuestTrapHandler(pVM, iGate) == TRPM_INVALID_HANDLER);
|
---|
2355 |
|
---|
2356 | if ( pGuestIdte->Gen.u1Present
|
---|
2357 | && (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
|
---|
2358 | && (pGuestIdte->Gen.u2DPL == 3 || pGuestIdte->Gen.u2DPL == 0)
|
---|
2359 | )
|
---|
2360 | {
|
---|
2361 | RTRCPTR pHandler;
|
---|
2362 | CSAMP2GLOOKUPREC cacheRec = {0}; /* Cache record for PATMGCVirtToHCVirt. */
|
---|
2363 | PCSAMPAGE pPage = NULL;
|
---|
2364 | SELMSELINFO selInfo;
|
---|
2365 |
|
---|
2366 | pHandler = VBOXIDTE_OFFSET(*pGuestIdte);
|
---|
2367 | pHandler = SELMToFlatBySel(pVM, pGuestIdte->Gen.u16SegSel, pHandler);
|
---|
2368 |
|
---|
2369 | rc = SELMR3GetSelectorInfo(pVM, pGuestIdte->Gen.u16SegSel, &selInfo);
|
---|
2370 | if ( VBOX_FAILURE(rc)
|
---|
2371 | || selInfo.GCPtrBase != 0
|
---|
2372 | || selInfo.cbLimit != ~0U
|
---|
2373 | )
|
---|
2374 | {
|
---|
2375 | /* Refuse to patch a handler whose idt cs selector isn't wide open. */
|
---|
2376 | Log(("CSAMCheckGates: check gate %d failed due to rc %Vrc GCPtrBase=%VRv limit=%x\n", iGate, rc, selInfo.GCPtrBase, selInfo.cbLimit));
|
---|
2377 | continue;
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 |
|
---|
2381 | if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
|
---|
2382 | {
|
---|
2383 | Log(("CSAMCheckGates: check trap gate %d at %04X:%08X (flat %VRv)\n", iGate, pGuestIdte->Gen.u16SegSel, VBOXIDTE_OFFSET(*pGuestIdte), pHandler));
|
---|
2384 | }
|
---|
2385 | else
|
---|
2386 | {
|
---|
2387 | Log(("CSAMCheckGates: check interrupt gate %d at %04X:%08X (flat %VRv)\n", iGate, pGuestIdte->Gen.u16SegSel, VBOXIDTE_OFFSET(*pGuestIdte), pHandler));
|
---|
2388 | }
|
---|
2389 |
|
---|
2390 | STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
|
---|
2391 | rc = csamAnalyseCodeStream(pVM, pHandler, pHandler, true, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
2392 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
|
---|
2393 | if (rc != VINF_SUCCESS)
|
---|
2394 | {
|
---|
2395 | Log(("CSAMCheckGates: csamAnalyseCodeStream failed with %d\n", rc));
|
---|
2396 | continue;
|
---|
2397 | }
|
---|
2398 | /* OpenBSD guest specific patch test. */
|
---|
2399 | if (iGate >= 0x20)
|
---|
2400 | {
|
---|
2401 | PCPUMCTX pCtx;
|
---|
2402 | DISCPUSTATE cpu;
|
---|
2403 | RTGCUINTPTR32 aOpenBsdPushCSOffset[3] = {0x03, /* OpenBSD 3.7 & 3.8 */
|
---|
2404 | 0x2B, /* OpenBSD 4.0 installation ISO */
|
---|
2405 | 0x2F}; /* OpenBSD 4.0 after install */
|
---|
2406 |
|
---|
2407 | pCtx = CPUMQueryGuestCtxPtr(pVM);
|
---|
2408 |
|
---|
2409 | for (unsigned i=0;i<RT_ELEMENTS(aOpenBsdPushCSOffset);i++)
|
---|
2410 | {
|
---|
2411 | rc = CPUMR3DisasmInstrCPU(pVM, pCtx, pHandler - aOpenBsdPushCSOffset[i], &cpu, NULL);
|
---|
2412 | if ( rc == VINF_SUCCESS
|
---|
2413 | && cpu.pCurInstr->opcode == OP_PUSH
|
---|
2414 | && cpu.pCurInstr->param1 == OP_PARM_REG_CS)
|
---|
2415 | {
|
---|
2416 | rc = PATMR3InstallPatch(pVM, pHandler - aOpenBsdPushCSOffset[i], PATMFL_CODE32 | PATMFL_GUEST_SPECIFIC);
|
---|
2417 | if (VBOX_SUCCESS(rc))
|
---|
2418 | Log(("Installed OpenBSD interrupt handler prefix instruction (push cs) patch\n"));
|
---|
2419 | }
|
---|
2420 | }
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | /* Trap gates and certain interrupt gates. */
|
---|
2424 | uint32_t fPatchFlags = PATMFL_CODE32 | PATMFL_IDTHANDLER;
|
---|
2425 |
|
---|
2426 | if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
|
---|
2427 | fPatchFlags |= PATMFL_TRAPHANDLER;
|
---|
2428 | else
|
---|
2429 | fPatchFlags |= PATMFL_INTHANDLER;
|
---|
2430 |
|
---|
2431 | switch (iGate) {
|
---|
2432 | case 8:
|
---|
2433 | case 10:
|
---|
2434 | case 11:
|
---|
2435 | case 12:
|
---|
2436 | case 13:
|
---|
2437 | case 14:
|
---|
2438 | case 17:
|
---|
2439 | fPatchFlags |= PATMFL_TRAPHANDLER_WITH_ERRORCODE;
|
---|
2440 | break;
|
---|
2441 | default:
|
---|
2442 | /* No error code. */
|
---|
2443 | break;
|
---|
2444 | }
|
---|
2445 |
|
---|
2446 | Log(("Installing %s gate handler for 0x%X at %VRv\n", (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32) ? "trap" : "intr", iGate, pHandler));
|
---|
2447 |
|
---|
2448 | rc = PATMR3InstallPatch(pVM, pHandler, fPatchFlags);
|
---|
2449 | if (VBOX_SUCCESS(rc) || rc == VERR_PATM_ALREADY_PATCHED)
|
---|
2450 | {
|
---|
2451 | Log(("Gate handler 0x%X is SAFE!\n", iGate));
|
---|
2452 |
|
---|
2453 | RTRCPTR pNewHandlerGC = PATMR3QueryPatchGCPtr(pVM, pHandler);
|
---|
2454 | if (pNewHandlerGC)
|
---|
2455 | {
|
---|
2456 | rc = TRPMR3SetGuestTrapHandler(pVM, iGate, pNewHandlerGC);
|
---|
2457 | if (VBOX_FAILURE(rc))
|
---|
2458 | Log(("TRPMR3SetGuestTrapHandler %d failed with %Vrc\n", iGate, rc));
|
---|
2459 | }
|
---|
2460 | }
|
---|
2461 | }
|
---|
2462 | } /* for */
|
---|
2463 | STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
|
---|
2464 | return VINF_SUCCESS;
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | /**
|
---|
2468 | * Record previous call instruction addresses
|
---|
2469 | *
|
---|
2470 | * @returns VBox status code.
|
---|
2471 | * @param pVM The VM to operate on.
|
---|
2472 | * @param GCPtrCall Call address
|
---|
2473 | */
|
---|
2474 | VMMR3DECL(int) CSAMR3RecordCallAddress(PVM pVM, RTRCPTR GCPtrCall)
|
---|
2475 | {
|
---|
2476 | for (unsigned i=0;i<RT_ELEMENTS(pVM->csam.s.pvCallInstruction);i++)
|
---|
2477 | {
|
---|
2478 | if (pVM->csam.s.pvCallInstruction[i] == GCPtrCall)
|
---|
2479 | return VINF_SUCCESS;
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | Log(("CSAMR3RecordCallAddress %VRv\n", GCPtrCall));
|
---|
2483 |
|
---|
2484 | pVM->csam.s.pvCallInstruction[pVM->csam.s.iCallInstruction++] = GCPtrCall;
|
---|
2485 | if (pVM->csam.s.iCallInstruction >= RT_ELEMENTS(pVM->csam.s.pvCallInstruction))
|
---|
2486 | pVM->csam.s.iCallInstruction = 0;
|
---|
2487 |
|
---|
2488 | return VINF_SUCCESS;
|
---|
2489 | }
|
---|
2490 |
|
---|
2491 |
|
---|
2492 | /**
|
---|
2493 | * Query CSAM state (enabled/disabled)
|
---|
2494 | *
|
---|
2495 | * @returns 0 - disabled, 1 - enabled
|
---|
2496 | * @param pVM The VM to operate on.
|
---|
2497 | */
|
---|
2498 | VMMR3DECL(int) CSAMR3IsEnabled(PVM pVM)
|
---|
2499 | {
|
---|
2500 | return pVM->fCSAMEnabled;
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | #ifdef VBOX_WITH_DEBUGGER
|
---|
2504 | /**
|
---|
2505 | * The '.csamoff' command.
|
---|
2506 | *
|
---|
2507 | * @returns VBox status.
|
---|
2508 | * @param pCmd Pointer to the command descriptor (as registered).
|
---|
2509 | * @param pCmdHlp Pointer to command helper functions.
|
---|
2510 | * @param pVM Pointer to the current VM (if any).
|
---|
2511 | * @param paArgs Pointer to (readonly) array of arguments.
|
---|
2512 | * @param cArgs Number of arguments in the array.
|
---|
2513 | */
|
---|
2514 | static DECLCALLBACK(int) csamr3CmdOff(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
|
---|
2515 | {
|
---|
2516 | /*
|
---|
2517 | * Validate input.
|
---|
2518 | */
|
---|
2519 | if (!pVM)
|
---|
2520 | return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
|
---|
2521 |
|
---|
2522 | CSAMDisableScanning(pVM);
|
---|
2523 | return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "CSAM Scanning disabled\n");
|
---|
2524 | }
|
---|
2525 |
|
---|
2526 | /**
|
---|
2527 | * The '.csamon' command.
|
---|
2528 | *
|
---|
2529 | * @returns VBox status.
|
---|
2530 | * @param pCmd Pointer to the command descriptor (as registered).
|
---|
2531 | * @param pCmdHlp Pointer to command helper functions.
|
---|
2532 | * @param pVM Pointer to the current VM (if any).
|
---|
2533 | * @param paArgs Pointer to (readonly) array of arguments.
|
---|
2534 | * @param cArgs Number of arguments in the array.
|
---|
2535 | */
|
---|
2536 | static DECLCALLBACK(int) csamr3CmdOn(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
|
---|
2537 | {
|
---|
2538 | /*
|
---|
2539 | * Validate input.
|
---|
2540 | */
|
---|
2541 | if (!pVM)
|
---|
2542 | return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
|
---|
2543 |
|
---|
2544 | CSAMEnableScanning(pVM);
|
---|
2545 | return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "CSAM Scanning enabled\n");
|
---|
2546 | }
|
---|
2547 | #endif
|
---|