VirtualBox

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

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

Minor update

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