VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/CSAM.cpp@ 1995

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

Update

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette