VirtualBox

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

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

update physical address of flushed page

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 84.1 KB
Line 
1/* $Id: CSAM.cpp 758 2007-02-07 17:45:10Z 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 RTGCPHYS oldPhys = pPage->GCPhys;
1399 Log(("csamR3FlushPageRecord: page %VGv has changed -> FLUSH\n", pPage->pPageGC));
1400
1401 int rc = PGMGstGetPage(pVM, pPage->pPageGC, &pPage->fFlags, &pPage->GCPhys);
1402 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
1403
1404 Log(("Old GCPhys %VGp new %VGp\n", oldPhys, pPage->GCPhys));
1405 if (pPage->pBitmap == NULL)
1406 {
1407 pPage->pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, CSAM_PAGE_BITMAP_SIZE);
1408 Assert(pPage->pBitmap);
1409 if (pPage->pBitmap == NULL)
1410 {
1411 return VERR_NO_MEMORY;
1412 }
1413 }
1414 else
1415 {
1416 memset(pPage->pBitmap, 0, CSAM_PAGE_BITMAP_SIZE);
1417 }
1418 pPage->u64Hash = csamR3CalcPageHash(pVM, pPage->pPageGC);
1419
1420 return VINF_SUCCESS;
1421}
1422
1423/**
1424 * Notify CSAM of a page flush
1425 *
1426 * @returns VBox status code
1427 * @param pVM The VM to operate on.
1428 * @param addr GC address of the page to flush
1429 * @param fRemovePage Page removal flag
1430 */
1431static int csamFlushPage(PVM pVM, RTGCPTR addr, bool fRemovePage)
1432{
1433 PCSAMPAGEREC pPageRec;
1434 int rc;
1435 RTGCPHYS GCPhys = 0;
1436 uint64_t fFlags = 0;
1437
1438 if (!CSAMIsEnabled(pVM))
1439 return VINF_SUCCESS;
1440
1441 STAM_PROFILE_START(&pVM->csam.s.StatTimeFlushPage, a);
1442
1443 addr = addr & PAGE_BASE_GC_MASK;
1444
1445 /*
1446 * Note: searching for the page in our tree first is more expensive (skipped flushes are two orders of magnitude more common)
1447 */
1448 if (pVM->csam.s.pPageTree == NULL)
1449 {
1450 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1451 return VWRN_CSAM_PAGE_NOT_FOUND;
1452 }
1453
1454 rc = PGMGstGetPage(pVM, addr, &fFlags, &GCPhys);
1455 /* Returned at a very early stage (no paging yet presumably). */
1456 if (rc == VERR_NOT_SUPPORTED)
1457 {
1458 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1459 return rc;
1460 }
1461
1462 if (VBOX_SUCCESS(rc))
1463 {
1464 if ( (fFlags & X86_PTE_US)
1465 || rc == VERR_PGM_PHYS_PAGE_RESERVED
1466 )
1467 {
1468 /* User page -> not relevant for us. */
1469 STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
1470 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1471 return VINF_SUCCESS;
1472 }
1473 }
1474 else
1475 if (rc != VERR_PAGE_NOT_PRESENT && rc != VERR_PAGE_TABLE_NOT_PRESENT)
1476 AssertMsgFailed(("PGMR3GetPage %VGv failed with %Vrc\n", addr, rc));
1477
1478 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)addr);
1479 if (pPageRec)
1480 {
1481 if ( GCPhys == pPageRec->page.GCPhys
1482 && (fFlags & X86_PTE_P))
1483 {
1484 STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
1485 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1486 return VINF_SUCCESS;
1487 }
1488
1489 Log(("CSAMR3FlushPage: page %VGv has changed -> FLUSH (rc=%Vrc) (Phys: %VGp vs %VGp)\n", addr, rc, GCPhys, pPageRec->page.GCPhys));
1490
1491 STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushes, 1);
1492
1493 if (fRemovePage)
1494 csamRemovePageRecord(pVM, addr);
1495 else
1496 {
1497 CSAMMarkPage(pVM, addr, false);
1498 pPageRec->page.GCPhys = 0;
1499 pPageRec->page.fFlags = 0;
1500 rc = PGMGstGetPage(pVM, addr, &pPageRec->page.fFlags, &pPageRec->page.GCPhys);
1501 if (rc == VINF_SUCCESS)
1502 pPageRec->page.u64Hash = csamR3CalcPageHash(pVM, addr);
1503 }
1504
1505
1506 /*
1507 * Inform patch manager about the flush; no need to repeat the above check twice.
1508 */
1509 PATMR3FlushPage(pVM, addr);
1510
1511 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1512 return VINF_SUCCESS;
1513 }
1514 else
1515 {
1516 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
1517 return VWRN_CSAM_PAGE_NOT_FOUND;
1518 }
1519}
1520
1521/**
1522 * Notify CSAM of a page flush
1523 *
1524 * @returns VBox status code
1525 * @param pVM The VM to operate on.
1526 * @param addr GC address of the page to flush
1527 */
1528CSAMR3DECL(int) CSAMR3FlushPage(PVM pVM, RTGCPTR addr)
1529{
1530 return csamFlushPage(pVM, addr, true /* remove page record */);
1531}
1532
1533/**
1534 * Check a page record in case a page has been changed
1535 *
1536 * @returns VBox status code. (trap handled or not)
1537 * @param pVM The VM to operate on.
1538 * @param pInstrGC GC instruction pointer
1539 */
1540int csamR3CheckPageRecord(PVM pVM, RTGCPTR pInstrGC)
1541{
1542 PCSAMPAGEREC pPageRec;
1543 uint64_t u64hash;
1544
1545 pInstrGC = pInstrGC & PAGE_BASE_GC_MASK;
1546
1547 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pInstrGC);
1548 if (pPageRec)
1549 {
1550 u64hash = csamR3CalcPageHash(pVM, pInstrGC);
1551 if (u64hash != pPageRec->page.u64Hash)
1552 {
1553 csamR3FlushPageRecord(pVM, &pPageRec->page);
1554 }
1555 }
1556 else
1557 {
1558 return VWRN_CSAM_PAGE_NOT_FOUND;
1559 }
1560 return VINF_SUCCESS;
1561}
1562
1563/**
1564 * Returns monitor description based on CSAM tag
1565 *
1566 * @return description string
1567 * @param enmTag Owner tag
1568 */
1569const char *csamGetMonitorDescription(CSAMTAG enmTag)
1570{
1571 if (enmTag == CSAM_TAG_PATM)
1572 return "CSAM-PATM self-modifying code monitor handler";
1573 else
1574 if (enmTag == CSAM_TAG_REM)
1575 return "CSAM-REM self-modifying code monitor handler";
1576 Assert(enmTag == CSAM_TAG_CSAM);
1577 return "CSAM self-modifying code monitor handler";
1578}
1579
1580/**
1581 * Adds page record to our lookup tree
1582 *
1583 * @returns CSAMPAGE ptr or NULL if failure
1584 * @param pVM The VM to operate on.
1585 * @param GCPtr Page address
1586 * @param enmTag Owner tag
1587 * @param fCode32 16 or 32 bits code
1588 * @param fMonitorInvalidation Monitor page invalidation flag
1589 */
1590static PCSAMPAGE csamCreatePageRecord(PVM pVM, RTGCPTR GCPtr, CSAMTAG enmTag, bool fCode32, bool fMonitorInvalidation)
1591{
1592 PCSAMPAGEREC pPage;
1593 int rc;
1594 bool ret;
1595
1596 Log(("New page record for %VGv\n", GCPtr & PAGE_BASE_GC_MASK));
1597
1598 pPage = (PCSAMPAGEREC)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, sizeof(CSAMPAGEREC));
1599 if (pPage == NULL)
1600 {
1601 AssertMsgFailed(("csamCreatePageRecord: Out of memory!!!!\n"));
1602 return NULL;
1603 }
1604 /* Round down to page boundary. */
1605 GCPtr = (GCPtr & PAGE_BASE_GC_MASK);
1606 pPage->Core.Key = (AVLPVKEY)GCPtr;
1607 pPage->page.pPageGC = GCPtr;
1608 pPage->page.fCode32 = fCode32;
1609 pPage->page.fMonitorInvalidation = fMonitorInvalidation;
1610 pPage->page.enmTag = enmTag;
1611 pPage->page.fMonitorActive = false;
1612 pPage->page.pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, PAGE_SIZE/sizeof(uint8_t));
1613 rc = PGMGstGetPage(pVM, GCPtr, &pPage->page.fFlags, &pPage->page.GCPhys);
1614 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
1615
1616 pPage->page.u64Hash = csamR3CalcPageHash(pVM, GCPtr);
1617 ret = RTAvlPVInsert(&pVM->csam.s.pPageTree, &pPage->Core);
1618 Assert(ret);
1619
1620#ifdef CSAM_MONITOR_CODE_PAGES
1621 AssertRelease(!fInCSAMCodePageInvalidate);
1622
1623 switch (enmTag)
1624 {
1625 case CSAM_TAG_PATM:
1626 case CSAM_TAG_REM:
1627#ifdef CSAM_MONITOR_CSAM_CODE_PAGES
1628 case CSAM_TAG_CSAM:
1629#endif
1630 {
1631 int rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, GCPtr, GCPtr + (PAGE_SIZE - 1) /* inclusive! */,
1632 (fMonitorInvalidation) ? CSAMCodePageInvalidate : 0, CSAMCodePageWriteHandler, "CSAMGCCodePageWriteHandler", 0,
1633 csamGetMonitorDescription(enmTag));
1634 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT, ("PGMR3HandlerVirtualRegisterEx %VGv failed with %Vrc\n", GCPtr, rc));
1635 if (VBOX_FAILURE(rc))
1636 Log(("PGMR3HandlerVirtualRegisterEx for %VGv failed with %Vrc\n", GCPtr, rc));
1637
1638 /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
1639
1640 /* Prefetch it in case it's not there yet. */
1641 rc = PGMPrefetchPage(pVM, GCPtr);
1642 AssertRC(rc);
1643
1644 rc = PGMShwModifyPage(pVM, GCPtr, 1, 0, ~(uint64_t)X86_PTE_RW);
1645 Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
1646
1647 pPage->page.fMonitorActive = true;
1648 STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
1649 break;
1650 }
1651 default:
1652 break; /* to shut up GCC */
1653 }
1654
1655 Log(("csamCreatePageRecord %VGv HCPhys=%VGp\n", GCPtr, pPage->page.GCPhys));
1656
1657#ifdef VBOX_WITH_STATISTICS
1658 switch (enmTag)
1659 {
1660 case CSAM_TAG_CSAM:
1661 STAM_COUNTER_INC(&pVM->csam.s.StatPageCSAM);
1662 break;
1663 case CSAM_TAG_PATM:
1664 STAM_COUNTER_INC(&pVM->csam.s.StatPagePATM);
1665 break;
1666 case CSAM_TAG_REM:
1667 STAM_COUNTER_INC(&pVM->csam.s.StatPageREM);
1668 break;
1669 default:
1670 break; /* to shut up GCC */
1671 }
1672#endif
1673
1674#endif
1675
1676 STAM_COUNTER_INC(&pVM->csam.s.StatNrPages);
1677 if (fMonitorInvalidation)
1678 STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
1679
1680 return &pPage->page;
1681}
1682
1683/**
1684 * Monitors a code page (if not already monitored)
1685 *
1686 * @returns VBox status code
1687 * @param pVM The VM to operate on.
1688 * @param pPageAddrGC The page to monitor
1689 * @param enmTag Monitor tag
1690 */
1691CSAMR3DECL(int) CSAMR3MonitorPage(PVM pVM, RTGCPTR pPageAddrGC, CSAMTAG enmTag)
1692{
1693 PCSAMPAGEREC pPageRec = NULL;
1694 int rc;
1695 bool fMonitorInvalidation;
1696
1697 /* Dirty pages must be handled before calling this function!. */
1698 Assert(!pVM->csam.s.cDirtyPages);
1699
1700 if (pVM->csam.s.fScanningStarted == false)
1701 return VINF_SUCCESS; /* too early */
1702
1703 pPageAddrGC &= PAGE_BASE_GC_MASK;
1704
1705 Log(("CSAMR3MonitorPage %VGv %d\n", pPageAddrGC, enmTag));
1706
1707 /** @todo implicit assumption */
1708 fMonitorInvalidation = (enmTag == CSAM_TAG_PATM);
1709
1710 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pPageAddrGC);
1711 if (pPageRec == NULL)
1712 {
1713 uint64_t fFlags;
1714
1715 rc = PGMGstGetPage(pVM, pPageAddrGC, &fFlags, NULL);
1716 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
1717 if ( rc == VINF_SUCCESS
1718 && (fFlags & X86_PTE_US))
1719 {
1720 /* We don't care about user pages. */
1721 STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
1722 return VINF_SUCCESS;
1723 }
1724
1725 csamCreatePageRecord(pVM, pPageAddrGC, enmTag, true /* 32 bits code */, fMonitorInvalidation);
1726
1727 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pPageAddrGC);
1728 Assert(pPageRec);
1729 }
1730 /** @todo reference count */
1731
1732#ifdef CSAM_MONITOR_CSAM_CODE_PAGES
1733 Assert(pPageRec->page.fMonitorActive);
1734#endif
1735
1736#ifdef CSAM_MONITOR_CODE_PAGES
1737 if (!pPageRec->page.fMonitorActive)
1738 {
1739 Log(("CSAMR3MonitorPage: activate monitoring for %VGv\n", pPageAddrGC));
1740
1741 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, pPageAddrGC, pPageAddrGC + (PAGE_SIZE - 1) /* inclusive! */,
1742 (fMonitorInvalidation) ? CSAMCodePageInvalidate : 0, CSAMCodePageWriteHandler, "CSAMGCCodePageWriteHandler", 0,
1743 csamGetMonitorDescription(enmTag));
1744 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT, ("PGMR3HandlerVirtualRegisterEx %VGv failed with %Vrc\n", pPageAddrGC, rc));
1745 if (VBOX_FAILURE(rc))
1746 Log(("PGMR3HandlerVirtualRegisterEx for %VGv failed with %Vrc\n", pPageAddrGC, rc));
1747
1748 /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
1749
1750 /* Prefetch it in case it's not there yet. */
1751 rc = PGMPrefetchPage(pVM, pPageAddrGC);
1752 AssertRC(rc);
1753
1754 rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
1755 Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
1756
1757 STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
1758
1759 pPageRec->page.fMonitorActive = true;
1760 pPageRec->page.fMonitorInvalidation = fMonitorInvalidation;
1761 }
1762 else
1763 if ( !pPageRec->page.fMonitorInvalidation
1764 && fMonitorInvalidation)
1765 {
1766 Assert(pPageRec->page.fMonitorActive);
1767 PGMHandlerVirtualChangeInvalidateCallback(pVM, pPageRec->page.pPageGC, CSAMCodePageInvalidate);
1768 pPageRec->page.fMonitorInvalidation = true;
1769 STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
1770
1771 /* Prefetch it in case it's not there yet. */
1772 rc = PGMPrefetchPage(pVM, pPageAddrGC);
1773 AssertRC(rc);
1774
1775 /* Make sure it's readonly. Page invalidation may have modified the attributes. */
1776 rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
1777 Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
1778 }
1779
1780#if 0 /* def VBOX_STRICT -> very annoying) */
1781 if (pPageRec->page.fMonitorActive)
1782 {
1783 uint64_t fPageShw;
1784 RTHCPHYS GCPhys;
1785 rc = PGMShwGetPage(pVM, pPageAddrGC, &fPageShw, &GCPhys);
1786// AssertMsg( (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
1787// || !(fPageShw & X86_PTE_RW)
1788// || (pPageRec->page.GCPhys == 0), ("Shadow page flags for %VGv (%VHp) aren't readonly (%VX64)!!\n", pPageAddrGC, GCPhys, fPageShw));
1789 }
1790#endif
1791
1792 if (pPageRec->page.GCPhys == 0)
1793 {
1794 /* Prefetch it in case it's not there yet. */
1795 rc = PGMPrefetchPage(pVM, pPageAddrGC);
1796 AssertRC(rc);
1797 /* The page was changed behind our back. It won't be made read-only until the next SyncCR3, so force it here. */
1798 rc = PGMShwModifyPage(pVM, pPageAddrGC, 1, 0, ~(uint64_t)X86_PTE_RW);
1799 Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
1800 }
1801#endif /* CSAM_MONITOR_CODE_PAGES */
1802 return VINF_SUCCESS;
1803}
1804
1805/**
1806 * Removes a page record from our lookup tree
1807 *
1808 * @returns VBox status code
1809 * @param pVM The VM to operate on.
1810 * @param GCPtr Page address
1811 */
1812static int csamRemovePageRecord(PVM pVM, RTGCPTR GCPtr)
1813{
1814 PCSAMPAGEREC pPageRec;
1815
1816 Log(("csamRemovePageRecord %VGv\n", GCPtr));
1817 pPageRec = (PCSAMPAGEREC)RTAvlPVRemove(&pVM->csam.s.pPageTree, (AVLPVKEY)GCPtr);
1818
1819 if (pPageRec)
1820 {
1821 STAM_COUNTER_INC(&pVM->csam.s.StatNrRemovedPages);
1822
1823#ifdef CSAM_MONITOR_CODE_PAGES
1824 if (pPageRec->page.fMonitorActive)
1825 {
1826 /* @todo -> this is expensive (cr3 reload)!!!
1827 * if this happens often, then reuse it instead!!!
1828 */
1829 Assert(!fInCSAMCodePageInvalidate);
1830 STAM_COUNTER_DEC(&pVM->csam.s.StatPageMonitor);
1831 PGMHandlerVirtualDeregister(pVM, GCPtr);
1832 }
1833 if (pPageRec->page.enmTag == CSAM_TAG_PATM)
1834 {
1835 /* Make sure the recompiler flushes its cache as this page is no longer monitored. */
1836 STAM_COUNTER_INC(&pVM->csam.s.StatPageRemoveREMFlush);
1837 CPUMSetChangedFlags(pVM, CPUM_CHANGED_GLOBAL_TLB_FLUSH);
1838 }
1839#endif
1840
1841#ifdef VBOX_WITH_STATISTICS
1842 switch (pPageRec->page.enmTag)
1843 {
1844 case CSAM_TAG_CSAM:
1845 STAM_COUNTER_DEC(&pVM->csam.s.StatPageCSAM);
1846 break;
1847 case CSAM_TAG_PATM:
1848 STAM_COUNTER_DEC(&pVM->csam.s.StatPagePATM);
1849 break;
1850 case CSAM_TAG_REM:
1851 STAM_COUNTER_DEC(&pVM->csam.s.StatPageREM);
1852 break;
1853 default:
1854 break; /* to shut up GCC */
1855 }
1856#endif
1857
1858 if (pPageRec->page.pBitmap) MMR3HeapFree(pPageRec->page.pBitmap);
1859 MMR3HeapFree(pPageRec);
1860 }
1861 else
1862 AssertFailed();
1863
1864 return VINF_SUCCESS;
1865}
1866
1867/**
1868 * Callback for delayed writes from non-EMT threads
1869 *
1870 * @param pVM VM Handle.
1871 * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
1872 * @param cbBuf How much it's reading/writing.
1873 */
1874static DECLCALLBACK(void) CSAMDelayedWriteHandler(PVM pVM, RTGCPTR GCPtr, size_t cbBuf)
1875{
1876 int rc = PATMR3PatchWrite(pVM, GCPtr, cbBuf);
1877 AssertRC(rc);
1878}
1879
1880/**
1881 * #PF Handler callback for virtual access handler ranges.
1882 *
1883 * Important to realize that a physical page in a range can have aliases, and
1884 * for ALL and WRITE handlers these will also trigger.
1885 *
1886 * @returns VINF_SUCCESS if the handler have carried out the operation.
1887 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
1888 * @param pVM VM Handle.
1889 * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
1890 * @param pvPtr The HC mapping of that address.
1891 * @param pvBuf What the guest is reading/writing.
1892 * @param cbBuf How much it's reading/writing.
1893 * @param enmAccessType The access type.
1894 * @param pvUser User argument.
1895 */
1896static DECLCALLBACK(int) CSAMCodePageWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
1897{
1898 int rc;
1899
1900 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
1901 Log(("CSAMCodePageWriteHandler: write to %VGv size=%d\n", GCPtr, cbBuf));
1902
1903 if (VM_IS_EMT(pVM))
1904 {
1905 rc = PATMR3PatchWrite(pVM, GCPtr, cbBuf);
1906 }
1907 else
1908 {
1909 /* Queue the write instead otherwise we'll get concurrency issues. */
1910 /** @note in theory not correct to let it write the data first before disabling a patch!
1911 * (if it writes the same data as the patch jump and we replace it with obsolete opcodes)
1912 */
1913 Log(("CSAMCodePageWriteHandler: delayed write!\n"));
1914 rc = VMR3ReqCallEx(pVM, NULL, 0, VMREQFLAGS_NO_WAIT | VMREQFLAGS_VOID,
1915 (PFNRT)CSAMDelayedWriteHandler, 3, pVM, GCPtr, cbBuf);
1916 }
1917 AssertRC(rc);
1918
1919 return VINF_PGM_HANDLER_DO_DEFAULT;
1920}
1921
1922/**
1923 * #PF Handler callback for invalidation of virtual access handler ranges.
1924 *
1925 * @param pVM VM Handle.
1926 * @param GCPtr The virtual address the guest has changed.
1927 */
1928static DECLCALLBACK(int) CSAMCodePageInvalidate(PVM pVM, RTGCPTR GCPtr)
1929{
1930 fInCSAMCodePageInvalidate = true;
1931 LogFlow(("CSAMCodePageInvalidate %VGv\n", GCPtr));
1932 /** @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. */
1933 csamFlushPage(pVM, GCPtr, false /* don't remove page! */);
1934 fInCSAMCodePageInvalidate = false;
1935 return VINF_SUCCESS;
1936}
1937
1938/**
1939 * Check if the current instruction has already been checked before
1940 *
1941 * @returns VBox status code. (trap handled or not)
1942 * @param pVM The VM to operate on.
1943 * @param pInstr Instruction pointer
1944 * @param pPage CSAM patch structure pointer
1945 */
1946bool csamIsCodeScanned(PVM pVM, RTGCPTR pInstr, PCSAMPAGE *pPage)
1947{
1948 PCSAMPAGEREC pPageRec;
1949 uint32_t offset;
1950
1951 STAM_PROFILE_START(&pVM->csam.s.StatTimeCheckAddr, a);
1952
1953 offset = pInstr & PAGE_OFFSET_MASK;
1954 pInstr = pInstr & PAGE_BASE_GC_MASK;
1955
1956 Assert(pPage);
1957
1958 if (*pPage && (*pPage)->pPageGC == pInstr)
1959 {
1960 if ((*pPage)->pBitmap == NULL || ASMBitTest((*pPage)->pBitmap, offset))
1961 {
1962 STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
1963 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
1964 return true;
1965 }
1966 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
1967 return false;
1968 }
1969
1970 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)pInstr);
1971 if (pPageRec)
1972 {
1973 if (pPage) *pPage= &pPageRec->page;
1974 if (pPageRec->page.pBitmap == NULL || ASMBitTest(pPageRec->page.pBitmap, offset))
1975 {
1976 STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
1977 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
1978 return true;
1979 }
1980 }
1981 else
1982 {
1983 if (pPage) *pPage = NULL;
1984 }
1985 STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
1986 return false;
1987}
1988
1989/**
1990 * Mark an instruction in a page as scanned/not scanned
1991 *
1992 * @param pVM The VM to operate on.
1993 * @param pPage Patch structure pointer
1994 * @param pInstr Instruction pointer
1995 * @param opsize Instruction size
1996 * @param fScanned Mark as scanned or not
1997 */
1998static void csamMarkCode(PVM pVM, PCSAMPAGE pPage, RTGCPTR pInstr, uint32_t opsize, bool fScanned)
1999{
2000 LogFlow(("csamMarkCodeAsScanned %VGv opsize=%d\n", pInstr, opsize));
2001 CSAMMarkPage(pVM, pInstr, fScanned);
2002
2003 /** @todo should recreate empty bitmap if !fScanned */
2004 if (pPage->pBitmap == NULL)
2005 return;
2006
2007 if (fScanned)
2008 {
2009 // retn instructions can be scanned more than once
2010 if (ASMBitTest(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK) == 0)
2011 {
2012 pPage->uSize += opsize;
2013 STAM_COUNTER_ADD(&pVM->csam.s.StatNrInstr, 1);
2014 }
2015 if (pPage->uSize >= PAGE_SIZE)
2016 {
2017 Log(("Scanned full page (%VGv) -> free bitmap\n", pInstr & PAGE_BASE_GC_MASK));
2018 MMR3HeapFree(pPage->pBitmap);
2019 pPage->pBitmap = NULL;
2020 }
2021 else
2022 ASMBitSet(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
2023 }
2024 else
2025 ASMBitClear(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
2026}
2027
2028/**
2029 * Mark an instruction in a page as scanned/not scanned
2030 *
2031 * @returns VBox status code.
2032 * @param pVM The VM to operate on.
2033 * @param pInstr Instruction pointer
2034 * @param opsize Instruction size
2035 * @param fScanned Mark as scanned or not
2036 */
2037CSAMDECL(int) CSAMR3MarkCode(PVM pVM, RTGCPTR pInstr, uint32_t opsize, bool fScanned)
2038{
2039 PCSAMPAGE pPage = 0;
2040
2041 Assert(!fScanned); /* other case not implemented. */
2042 Assert(!PATMIsPatchGCAddr(pVM, pInstr));
2043
2044 if (csamIsCodeScanned(pVM, pInstr, &pPage) == false)
2045 {
2046 Assert(fScanned == true); /* other case should not be possible */
2047 return VINF_SUCCESS;
2048 }
2049
2050 Log(("CSAMR3MarkCode: %VGv size=%d fScanned=%d\n", pInstr, opsize, fScanned));
2051 csamMarkCode(pVM, pPage, pInstr, opsize, fScanned);
2052 return VINF_SUCCESS;
2053}
2054
2055
2056/**
2057 * Scan and analyse code starting at specified EIP
2058 *
2059 * @returns VBox status code. (trap handled or not)
2060 * @param pVM The VM to operate on.
2061 * @param pEip Instruction pointer
2062 * @param fCode32 16 of 32 bits code
2063 */
2064CSAMR3DECL(int) CSAMR3CheckEIP(PVM pVM, RTGCPTR pEip, bool fCode32)
2065{
2066 int rc;
2067 PCSAMPAGE pPage = NULL;
2068
2069 if (EMIsRawRing0Enabled(pVM) == false || PATMIsPatchGCAddr(pVM, pEip) == true)
2070 {
2071 // No use
2072 return VINF_SUCCESS;
2073 }
2074
2075 if (CSAMIsEnabled(pVM))
2076 {
2077 // Cache record for PATMGCVirtToHCVirt
2078 CSAMP2GLOOKUPREC cacheRec = {0};
2079
2080 //assuming 32 bits code for now
2081 Assert(fCode32);
2082
2083 STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
2084 rc = csamAnalyseCallCodeStream(pVM, pEip, pEip, fCode32, CSAMR3AnalyseCallback, pPage, &cacheRec);
2085 STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
2086 if (rc != VINF_SUCCESS)
2087 {
2088 Log(("csamAnalyseCodeStream failed with %d\n", rc));
2089 return rc;
2090 }
2091 else
2092 {
2093 // Log(("CSAMR3CheckEIP: already scanned page at %VGv\n", pEip));
2094 }
2095 }
2096 return VINF_SUCCESS;
2097}
2098
2099/**
2100 * Flush dirty code pages
2101 *
2102 * @returns VBox status code.
2103 * @param pVM The VM to operate on.
2104 */
2105CSAMR3DECL(int) CSAMR3FlushDirtyPages(PVM pVM)
2106{
2107 STAM_PROFILE_START(&pVM->csam.s.StatFlushDirtyPages, a);
2108
2109 for (uint32_t i=0;i<pVM->csam.s.cDirtyPages;i++)
2110 {
2111 int rc;
2112 PCSAMPAGEREC pPageRec;
2113 RTGCPTR GCPtr = pVM->csam.s.pvDirtyBasePage[i];
2114
2115 GCPtr = GCPtr & PAGE_BASE_GC_MASK;
2116
2117 /* Notify the recompiler that this page has been changed. */
2118 REMR3NotifyCodePageChanged(pVM, GCPtr);
2119
2120 /* Enable write protection again. (use the fault address as it might be an alias) */
2121 rc = PGMShwModifyPage(pVM, pVM->csam.s.pvDirtyFaultPage[i], 1, 0, ~(uint64_t)X86_PTE_RW);
2122 Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
2123
2124 Log(("CSAMR3FlushDirtyPages: flush %VGv (modifypage rc=%Vrc)\n", pVM->csam.s.pvDirtyBasePage[i], rc));
2125
2126 pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)GCPtr);
2127 if (pPageRec && pPageRec->page.enmTag == CSAM_TAG_REM)
2128 {
2129 uint64_t fFlags;
2130
2131 rc = PGMGstGetPage(pVM, GCPtr, &fFlags, NULL);
2132 AssertMsg(VBOX_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Vrc\n", rc));
2133 if ( rc == VINF_SUCCESS
2134 && (fFlags & X86_PTE_US))
2135 {
2136 /* We don't care about user pages. */
2137 csamRemovePageRecord(pVM, GCPtr);
2138 STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
2139 }
2140 }
2141 }
2142 pVM->csam.s.cDirtyPages = 0;
2143 VM_FF_CLEAR(pVM, VM_FF_CSAM_FLUSH_DIRTY_PAGE);
2144 STAM_PROFILE_STOP(&pVM->csam.s.StatFlushDirtyPages, a);
2145 return VINF_SUCCESS;
2146}
2147
2148/**
2149 * Analyse interrupt and trap gates
2150 *
2151 * @returns VBox status code.
2152 * @param pVM The VM to operate on.
2153 * @param iGate Start gate
2154 * @param cGates Number of gates to check
2155 */
2156CSAMR3DECL(int) CSAMR3CheckGates(PVM pVM, uint32_t iGate, uint32_t cGates)
2157{
2158 uint16_t cbIDT;
2159 RTGCPTR GCPtrIDT = CPUMGetGuestIDTR(pVM, &cbIDT);
2160 uint32_t iGateEnd;
2161 uint32_t maxGates;
2162 VBOXIDTE aIDT[256];
2163 PVBOXIDTE pGuestIdte;
2164 int rc;
2165
2166 if (EMIsRawRing0Enabled(pVM) == false)
2167 {
2168 /* Enabling interrupt gates only works when raw ring 0 is enabled. */
2169 //AssertFailed();
2170 return VINF_SUCCESS;
2171 }
2172
2173 /* We only check all gates once during a session */
2174 if ( !pVM->csam.s.fGatesChecked
2175 && cGates != 256)
2176 return VINF_SUCCESS; /* too early */
2177
2178 /* We only check all gates once during a session */
2179 if ( pVM->csam.s.fGatesChecked
2180 && cGates != 1)
2181 return VINF_SUCCESS; /* ignored */
2182
2183 if (cGates != 1)
2184 pVM->csam.s.fGatesChecked = true;
2185
2186 Assert(GCPtrIDT && cGates <= 256);
2187 if (!GCPtrIDT || cGates > 256)
2188 return VERR_INVALID_PARAMETER;
2189
2190 /* Determine valid upper boundary. */
2191 maxGates = (cbIDT+1) / sizeof(VBOXIDTE);
2192 Assert(iGate < maxGates);
2193 if (iGate > maxGates)
2194 return VERR_INVALID_PARAMETER;
2195
2196 if (iGate + cGates > maxGates)
2197 cGates = maxGates - iGate;
2198
2199 GCPtrIDT = GCPtrIDT + iGate * sizeof(VBOXIDTE);
2200 iGateEnd = iGate + cGates;
2201
2202 STAM_PROFILE_START(&pVM->csam.s.StatCheckGates, a);
2203
2204 /*
2205 * Get IDT entries.
2206 */
2207 if (PAGE_ADDRESS(GCPtrIDT) == PAGE_ADDRESS(GCPtrIDT+cGates*sizeof(VBOXIDTE)))
2208 {
2209 /* Just convert the IDT address to a HC pointer. The whole IDT fits in one page. */
2210 rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrIDT, (PRTHCPTR)&pGuestIdte);
2211 if (VBOX_FAILURE(rc))
2212 {
2213 AssertMsgRC(rc, ("Failed to read IDTE! rc=%Vrc\n", rc));
2214 STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
2215 return rc;
2216 }
2217 }
2218 else
2219 {
2220 /* Slow method when it crosses a page boundary. */
2221 rc = PGMPhysReadGCPtr(pVM, aIDT, GCPtrIDT, cGates*sizeof(VBOXIDTE));
2222 if (VBOX_FAILURE(rc))
2223 {
2224 AssertMsgRC(rc, ("Failed to read IDTE! rc=%Vrc\n", rc));
2225 STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
2226 return rc;
2227 }
2228 pGuestIdte = &aIDT[0];
2229 }
2230
2231 for (/*iGate*/; iGate<iGateEnd; iGate++, pGuestIdte++)
2232 {
2233 Assert(TRPMR3GetGuestTrapHandler(pVM, iGate) == TRPM_INVALID_HANDLER);
2234
2235 if ( pGuestIdte->Gen.u1Present
2236 && (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
2237 && (pGuestIdte->Gen.u2DPL == 3 || pGuestIdte->Gen.u2DPL == 0)
2238 )
2239 {
2240 RTGCPTR pHandler;
2241 CSAMP2GLOOKUPREC cacheRec = {0}; /* Cache record for PATMGCVirtToHCVirt. */
2242 PCSAMPAGE pPage = NULL;
2243
2244 pHandler = (pGuestIdte->Gen.u16OffsetHigh << 16) | pGuestIdte->Gen.u16OffsetLow;
2245 pHandler = SELMToFlat(pVM, pGuestIdte->Gen.u16SegSel, 0, pHandler);
2246
2247 if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
2248 {
2249 Log(("CSAMCheckGates: check trap gate %d at %04X:%08X (flat %VGv)\n", iGate, pGuestIdte->Gen.u16SegSel, (pGuestIdte->Gen.u16OffsetHigh << 16) | pGuestIdte->Gen.u16OffsetLow, pHandler));
2250 }
2251 else
2252 {
2253 Log(("CSAMCheckGates: check interrupt gate %d at %04X:%08X (flat %VGv)\n", iGate, pGuestIdte->Gen.u16SegSel, (pGuestIdte->Gen.u16OffsetHigh << 16) | pGuestIdte->Gen.u16OffsetLow, pHandler));
2254 }
2255
2256 STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
2257 rc = csamAnalyseCodeStream(pVM, pHandler, pHandler, true, CSAMR3AnalyseCallback, pPage, &cacheRec);
2258 STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
2259 if (rc != VINF_SUCCESS)
2260 {
2261 Log(("CSAMCheckGates: csamAnalyseCodeStream failed with %d\n", rc));
2262 continue;
2263 }
2264 if (iGate >= 0x20)
2265 {
2266 /* OpenBSD guest specific patch test (3.7 & 3.8) */
2267 rc = PATMR3InstallPatch(pVM, pHandler - 3, PATMFL_CODE32 | PATMFL_GUEST_SPECIFIC);
2268 if (VBOX_FAILURE(rc))
2269 /* OpenBSD guest specific patch test (3.9 & 4.0) */
2270 rc = PATMR3InstallPatch(pVM, pHandler - 0x2B, PATMFL_CODE32 | PATMFL_GUEST_SPECIFIC);
2271 if (VBOX_SUCCESS(rc))
2272 Log(("Installed OpenBSD interrupt handler prefix instruction (push cs) patch\n"));
2273 }
2274
2275 /* Trap gates and certain interrupt gates. */
2276 uint32_t fPatchFlags = PATMFL_CODE32 | PATMFL_IDTHANDLER;
2277
2278 if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
2279 fPatchFlags |= PATMFL_TRAPHANDLER;
2280 else
2281 fPatchFlags |= PATMFL_INTHANDLER;
2282
2283 switch (iGate) {
2284 case 8:
2285 case 10:
2286 case 11:
2287 case 12:
2288 case 13:
2289 case 14:
2290 case 17:
2291 fPatchFlags |= PATMFL_TRAPHANDLER_WITH_ERRORCODE;
2292 break;
2293 default:
2294 /* No error code. */
2295 break;
2296 }
2297
2298 Log(("Installing %s gate handler for 0x%X at %VGv\n", (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32) ? "trap" : "intr", iGate, pHandler));
2299
2300 rc = PATMR3InstallPatch(pVM, pHandler, fPatchFlags);
2301 if (VBOX_SUCCESS(rc) || rc == VERR_PATM_ALREADY_PATCHED)
2302 {
2303 Log(("Gate handler 0x%X is SAFE!\n", iGate));
2304
2305 RTGCPTR pNewHandlerGC = PATMR3QueryPatchGCPtr(pVM, pHandler);
2306 if (pNewHandlerGC)
2307 {
2308 rc = TRPMR3SetGuestTrapHandler(pVM, iGate, pNewHandlerGC);
2309 if (VBOX_FAILURE(rc))
2310 Log(("TRPMR3SetGuestTrapHandler %d failed with %Vrc\n", iGate, rc));
2311 }
2312 }
2313 }
2314 } /* for */
2315 STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
2316 return VINF_SUCCESS;
2317}
2318
2319/**
2320 * Query CSAM state (enabled/disabled)
2321 *
2322 * @returns 0 - disabled, 1 - enabled
2323 * @param pVM The VM to operate on.
2324 */
2325CSAMR3DECL(int) CSAMR3IsEnabled(PVM pVM)
2326{
2327 return pVM->fCSAMEnabled;
2328}
2329
2330#ifdef VBOX_WITH_DEBUGGER
2331/**
2332 * The '.csamoff' command.
2333 *
2334 * @returns VBox status.
2335 * @param pCmd Pointer to the command descriptor (as registered).
2336 * @param pCmdHlp Pointer to command helper functions.
2337 * @param pVM Pointer to the current VM (if any).
2338 * @param paArgs Pointer to (readonly) array of arguments.
2339 * @param cArgs Number of arguments in the array.
2340 */
2341static DECLCALLBACK(int) csamr3CmdOff(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2342{
2343 /*
2344 * Validate input.
2345 */
2346 if (!pVM)
2347 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
2348
2349 CSAMDisableScanning(pVM);
2350 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "CSAM Scanning disabled\n");
2351}
2352
2353/**
2354 * The '.csamon' command.
2355 *
2356 * @returns VBox status.
2357 * @param pCmd Pointer to the command descriptor (as registered).
2358 * @param pCmdHlp Pointer to command helper functions.
2359 * @param pVM Pointer to the current VM (if any).
2360 * @param paArgs Pointer to (readonly) array of arguments.
2361 * @param cArgs Number of arguments in the array.
2362 */
2363static DECLCALLBACK(int) csamr3CmdOn(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2364{
2365 /*
2366 * Validate input.
2367 */
2368 if (!pVM)
2369 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
2370
2371 CSAMEnableScanning(pVM);
2372 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "CSAM Scanning enabled\n");
2373}
2374#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