[23] | 1 | /* $Id: CSAM.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * CSAM - Guest OS Code Scanning and Analysis Manager
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[76553] | 7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
[58396] | 18 | /** @page pg_csam CSAM - Code Scanning Analysis Manager
|
---|
| 19 | *
|
---|
| 20 | * The CSAM is responsible for scanning and marking guest OS kernel code paths
|
---|
| 21 | * to making safe raw-mode execution possible.
|
---|
| 22 | *
|
---|
| 23 | * It works tightly with the @ref pg_patm "patch manager" to patch code
|
---|
| 24 | * sequences that we could otherwise not execute in raw-mode.
|
---|
| 25 | *
|
---|
| 26 | * @sa @ref grp_csam
|
---|
| 27 | */
|
---|
[57358] | 28 |
|
---|
[62460] | 29 |
|
---|
[57358] | 30 | /*********************************************************************************************************************************
|
---|
| 31 | * Header Files *
|
---|
| 32 | *********************************************************************************************************************************/
|
---|
[1] | 33 | #define LOG_GROUP LOG_GROUP_CSAM
|
---|
[35346] | 34 | #include <VBox/vmm/cpum.h>
|
---|
| 35 | #include <VBox/vmm/stam.h>
|
---|
| 36 | #include <VBox/vmm/patm.h>
|
---|
| 37 | #include <VBox/vmm/csam.h>
|
---|
| 38 | #include <VBox/vmm/cpumdis.h>
|
---|
| 39 | #include <VBox/vmm/pgm.h>
|
---|
| 40 | #include <VBox/vmm/iom.h>
|
---|
| 41 | #include <VBox/vmm/mm.h>
|
---|
| 42 | #include <VBox/vmm/em.h>
|
---|
[45620] | 43 | #include <VBox/vmm/hm.h>
|
---|
[40274] | 44 | #ifdef VBOX_WITH_REM
|
---|
| 45 | # include <VBox/vmm/rem.h>
|
---|
| 46 | #endif
|
---|
[35346] | 47 | #include <VBox/vmm/selm.h>
|
---|
| 48 | #include <VBox/vmm/trpm.h>
|
---|
| 49 | #include <VBox/vmm/cfgm.h>
|
---|
[44373] | 50 | #include <VBox/vmm/ssm.h>
|
---|
[1] | 51 | #include <VBox/param.h>
|
---|
| 52 | #include <iprt/avl.h>
|
---|
| 53 | #include <iprt/asm.h>
|
---|
| 54 | #include <iprt/thread.h>
|
---|
| 55 | #include "CSAMInternal.h"
|
---|
[35346] | 56 | #include <VBox/vmm/vm.h>
|
---|
[44373] | 57 | #include <VBox/vmm/uvm.h>
|
---|
[45620] | 58 |
|
---|
[1] | 59 | #include <VBox/dbg.h>
|
---|
[45620] | 60 | #include <VBox/sup.h>
|
---|
[1] | 61 | #include <VBox/err.h>
|
---|
| 62 | #include <VBox/log.h>
|
---|
[57008] | 63 | #include <VBox/version.h>
|
---|
[45620] | 64 |
|
---|
| 65 | #include <VBox/dis.h>
|
---|
| 66 | #include <VBox/disopcode.h>
|
---|
[1] | 67 | #include <iprt/assert.h>
|
---|
[23] | 68 | #include <iprt/string.h>
|
---|
[1] | 69 |
|
---|
| 70 |
|
---|
| 71 | /* Enabled by default */
|
---|
| 72 | #define CSAM_ENABLE
|
---|
| 73 |
|
---|
| 74 | /* Enable to monitor code pages for self-modifying code. */
|
---|
| 75 | #define CSAM_MONITOR_CODE_PAGES
|
---|
[23] | 76 | /* Enable to monitor all scanned pages
|
---|
[1] | 77 | #define CSAM_MONITOR_CSAM_CODE_PAGES */
|
---|
[7628] | 78 | /* Enable to scan beyond ret instructions.
|
---|
[1859] | 79 | #define CSAM_ANALYSE_BEYOND_RET */
|
---|
[1] | 80 |
|
---|
[57358] | 81 |
|
---|
| 82 | /*********************************************************************************************************************************
|
---|
| 83 | * Internal Functions *
|
---|
| 84 | *********************************************************************************************************************************/
|
---|
[55895] | 85 | static DECLCALLBACK(int) csamR3Save(PVM pVM, PSSMHANDLE pSSM);
|
---|
| 86 | static DECLCALLBACK(int) csamR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
|
---|
| 87 | static FNPGMR3VIRTINVALIDATE csamR3CodePageInvalidate;
|
---|
[1] | 88 |
|
---|
[9344] | 89 | bool csamIsCodeScanned(PVM pVM, RTRCPTR pInstr, PCSAMPAGE *pPage);
|
---|
| 90 | int csamR3CheckPageRecord(PVM pVM, RTRCPTR pInstr);
|
---|
[57008] | 91 | static PCSAMPAGE csamR3CreatePageRecord(PVM pVM, RTRCPTR GCPtr, CSAMTAG enmTag, bool fCode32, bool fMonitorInvalidation = false);
|
---|
[9344] | 92 | static int csamRemovePageRecord(PVM pVM, RTRCPTR GCPtr);
|
---|
[1] | 93 | static int csamReinit(PVM pVM);
|
---|
[9344] | 94 | static void csamMarkCode(PVM pVM, PCSAMPAGE pPage, RTRCPTR pInstr, uint32_t opsize, bool fScanned);
|
---|
[9212] | 95 | static int csamAnalyseCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
[1] | 96 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec);
|
---|
| 97 |
|
---|
[55895] | 98 | /** @todo "Temporary" for debugging. */
|
---|
| 99 | static bool g_fInCsamR3CodePageInvalidate = false;
|
---|
[1] | 100 |
|
---|
[44399] | 101 | #ifdef VBOX_WITH_DEBUGGER
|
---|
| 102 | static FNDBGCCMD csamr3CmdOn;
|
---|
| 103 | static FNDBGCCMD csamr3CmdOff;
|
---|
| 104 | #endif
|
---|
| 105 |
|
---|
| 106 |
|
---|
[57358] | 107 | /*********************************************************************************************************************************
|
---|
| 108 | * Global Variables *
|
---|
| 109 | *********************************************************************************************************************************/
|
---|
[1] | 110 | #ifdef VBOX_WITH_DEBUGGER
|
---|
| 111 | /** Command descriptors. */
|
---|
| 112 | static const DBGCCMD g_aCmds[] =
|
---|
| 113 | {
|
---|
[35696] | 114 | /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
|
---|
| 115 | { "csamon", 0, 0, NULL, 0, 0, csamr3CmdOn, "", "Enable CSAM code scanning." },
|
---|
| 116 | { "csamoff", 0, 0, NULL, 0, 0, csamr3CmdOff, "", "Disable CSAM code scanning." },
|
---|
[1] | 117 | };
|
---|
| 118 | #endif
|
---|
| 119 |
|
---|
[23765] | 120 | /**
|
---|
[57008] | 121 | * SSM descriptor table for the CSAM structure (save + restore).
|
---|
[23765] | 122 | */
|
---|
| 123 | static const SSMFIELD g_aCsamFields[] =
|
---|
| 124 | {
|
---|
[57008] | 125 | SSMFIELD_ENTRY( CSAM, aDangerousInstr), /* didn't used to restored */
|
---|
| 126 | SSMFIELD_ENTRY( CSAM, cDangerousInstr), /* didn't used to restored */
|
---|
| 127 | SSMFIELD_ENTRY( CSAM, iDangerousInstr), /* didn't used to restored */
|
---|
| 128 | SSMFIELD_ENTRY( CSAM, savedstate.cPageRecords),
|
---|
| 129 | SSMFIELD_ENTRY( CSAM, savedstate.cPatchPageRecords),
|
---|
| 130 | SSMFIELD_ENTRY( CSAM, cDirtyPages),
|
---|
| 131 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyBasePage),
|
---|
| 132 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyFaultPage),
|
---|
| 133 | SSMFIELD_ENTRY( CSAM, cPossibleCodePages),
|
---|
| 134 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvPossibleCodePage),
|
---|
| 135 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvCallInstruction), /* didn't used to be restored */
|
---|
| 136 | SSMFIELD_ENTRY( CSAM, iCallInstruction), /* didn't used to be restored */
|
---|
| 137 | SSMFIELD_ENTRY( CSAM, fScanningStarted),
|
---|
| 138 | SSMFIELD_ENTRY( CSAM, fGatesChecked),
|
---|
| 139 | SSMFIELD_ENTRY_TERM()
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
| 143 | * SSM descriptor table for the version 5.0.0 CSAM structure.
|
---|
| 144 | */
|
---|
| 145 | static const SSMFIELD g_aCsamFields500[] =
|
---|
| 146 | {
|
---|
| 147 | SSMFIELD_ENTRY_IGNORE( CSAM, offVM),
|
---|
| 148 | SSMFIELD_ENTRY_PAD_HC64( CSAM, Alignment0, sizeof(uint32_t)),
|
---|
| 149 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPageTree),
|
---|
| 150 | SSMFIELD_ENTRY( CSAM, aDangerousInstr),
|
---|
| 151 | SSMFIELD_ENTRY( CSAM, cDangerousInstr),
|
---|
| 152 | SSMFIELD_ENTRY( CSAM, iDangerousInstr),
|
---|
| 153 | SSMFIELD_ENTRY_RCPTR( CSAM, pPDBitmapGC), /// @todo ignore this?
|
---|
| 154 | SSMFIELD_ENTRY_RCPTR( CSAM, pPDHCBitmapGC), /// @todo ignore this?
|
---|
| 155 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPDBitmapHC),
|
---|
| 156 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPDGCBitmapHC),
|
---|
| 157 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, savedstate.pSSM),
|
---|
| 158 | SSMFIELD_ENTRY( CSAM, savedstate.cPageRecords),
|
---|
| 159 | SSMFIELD_ENTRY( CSAM, savedstate.cPatchPageRecords),
|
---|
| 160 | SSMFIELD_ENTRY( CSAM, cDirtyPages),
|
---|
| 161 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyBasePage),
|
---|
| 162 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyFaultPage),
|
---|
| 163 | SSMFIELD_ENTRY( CSAM, cPossibleCodePages),
|
---|
| 164 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvPossibleCodePage),
|
---|
| 165 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvCallInstruction),
|
---|
| 166 | SSMFIELD_ENTRY( CSAM, iCallInstruction),
|
---|
| 167 | SSMFIELD_ENTRY_IGNORE( CSAM, hCodePageWriteType), /* added in 5.0 */
|
---|
| 168 | SSMFIELD_ENTRY_IGNORE( CSAM, hCodePageWriteAndInvPgType), /* added in 5.0 */
|
---|
| 169 | SSMFIELD_ENTRY( CSAM, fScanningStarted),
|
---|
| 170 | SSMFIELD_ENTRY( CSAM, fGatesChecked),
|
---|
| 171 | SSMFIELD_ENTRY_PAD_HC( CSAM, Alignment1, 6, 2),
|
---|
| 172 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrTraps),
|
---|
| 173 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPages),
|
---|
| 174 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPagesInv),
|
---|
| 175 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrRemovedPages),
|
---|
| 176 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPatchPages),
|
---|
| 177 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPageNPHC),
|
---|
| 178 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPageNPGC),
|
---|
| 179 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrFlushes),
|
---|
| 180 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrFlushesSkipped),
|
---|
| 181 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrKnownPagesHC),
|
---|
| 182 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrKnownPagesGC),
|
---|
| 183 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrInstr),
|
---|
| 184 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrBytesRead),
|
---|
| 185 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrOpcodeRead),
|
---|
| 186 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTime),
|
---|
| 187 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeCheckAddr),
|
---|
| 188 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeAddrConv),
|
---|
| 189 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeFlushPage),
|
---|
| 190 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeDisasm),
|
---|
| 191 | SSMFIELD_ENTRY_IGNORE( CSAM, StatFlushDirtyPages),
|
---|
| 192 | SSMFIELD_ENTRY_IGNORE( CSAM, StatCheckGates),
|
---|
| 193 | SSMFIELD_ENTRY_IGNORE( CSAM, StatCodePageModified),
|
---|
| 194 | SSMFIELD_ENTRY_IGNORE( CSAM, StatDangerousWrite),
|
---|
| 195 | SSMFIELD_ENTRY_IGNORE( CSAM, StatInstrCacheHit),
|
---|
| 196 | SSMFIELD_ENTRY_IGNORE( CSAM, StatInstrCacheMiss),
|
---|
| 197 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPagePATM),
|
---|
| 198 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageCSAM),
|
---|
| 199 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageREM),
|
---|
| 200 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrUserPages),
|
---|
| 201 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageMonitor),
|
---|
| 202 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageRemoveREMFlush),
|
---|
| 203 | SSMFIELD_ENTRY_IGNORE( CSAM, StatBitmapAlloc),
|
---|
| 204 | SSMFIELD_ENTRY_IGNORE( CSAM, StatScanNextFunction),
|
---|
| 205 | SSMFIELD_ENTRY_IGNORE( CSAM, StatScanNextFunctionFailed),
|
---|
| 206 | SSMFIELD_ENTRY_TERM()
|
---|
| 207 | };
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * SSM descriptor table for the pre 5.0.0 CSAM structure.
|
---|
| 211 | */
|
---|
| 212 | static const SSMFIELD g_aCsamFieldsBefore500[] =
|
---|
| 213 | {
|
---|
[23778] | 214 | /** @todo there are more fields that can be ignored here. */
|
---|
[23765] | 215 | SSMFIELD_ENTRY_IGNORE( CSAM, offVM),
|
---|
| 216 | SSMFIELD_ENTRY_PAD_HC64( CSAM, Alignment0, sizeof(uint32_t)),
|
---|
[23777] | 217 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPageTree),
|
---|
[23765] | 218 | SSMFIELD_ENTRY( CSAM, aDangerousInstr),
|
---|
| 219 | SSMFIELD_ENTRY( CSAM, cDangerousInstr),
|
---|
| 220 | SSMFIELD_ENTRY( CSAM, iDangerousInstr),
|
---|
[23778] | 221 | SSMFIELD_ENTRY_RCPTR( CSAM, pPDBitmapGC), /// @todo ignore this?
|
---|
| 222 | SSMFIELD_ENTRY_RCPTR( CSAM, pPDHCBitmapGC), /// @todo ignore this?
|
---|
[23777] | 223 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPDBitmapHC),
|
---|
| 224 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, pPDGCBitmapHC),
|
---|
| 225 | SSMFIELD_ENTRY_IGN_HCPTR( CSAM, savedstate.pSSM),
|
---|
[23765] | 226 | SSMFIELD_ENTRY( CSAM, savedstate.cPageRecords),
|
---|
| 227 | SSMFIELD_ENTRY( CSAM, savedstate.cPatchPageRecords),
|
---|
| 228 | SSMFIELD_ENTRY( CSAM, cDirtyPages),
|
---|
| 229 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyBasePage),
|
---|
| 230 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvDirtyFaultPage),
|
---|
| 231 | SSMFIELD_ENTRY( CSAM, cPossibleCodePages),
|
---|
| 232 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvPossibleCodePage),
|
---|
| 233 | SSMFIELD_ENTRY_RCPTR_ARRAY( CSAM, pvCallInstruction),
|
---|
| 234 | SSMFIELD_ENTRY( CSAM, iCallInstruction),
|
---|
| 235 | SSMFIELD_ENTRY( CSAM, fScanningStarted),
|
---|
| 236 | SSMFIELD_ENTRY( CSAM, fGatesChecked),
|
---|
[23770] | 237 | SSMFIELD_ENTRY_PAD_HC( CSAM, Alignment1, 6, 2),
|
---|
[23765] | 238 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrTraps),
|
---|
| 239 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPages),
|
---|
| 240 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPagesInv),
|
---|
| 241 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrRemovedPages),
|
---|
| 242 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPatchPages),
|
---|
| 243 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPageNPHC),
|
---|
| 244 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrPageNPGC),
|
---|
| 245 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrFlushes),
|
---|
| 246 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrFlushesSkipped),
|
---|
| 247 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrKnownPagesHC),
|
---|
| 248 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrKnownPagesGC),
|
---|
| 249 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrInstr),
|
---|
| 250 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrBytesRead),
|
---|
| 251 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrOpcodeRead),
|
---|
| 252 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTime),
|
---|
| 253 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeCheckAddr),
|
---|
| 254 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeAddrConv),
|
---|
| 255 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeFlushPage),
|
---|
| 256 | SSMFIELD_ENTRY_IGNORE( CSAM, StatTimeDisasm),
|
---|
| 257 | SSMFIELD_ENTRY_IGNORE( CSAM, StatFlushDirtyPages),
|
---|
| 258 | SSMFIELD_ENTRY_IGNORE( CSAM, StatCheckGates),
|
---|
| 259 | SSMFIELD_ENTRY_IGNORE( CSAM, StatCodePageModified),
|
---|
| 260 | SSMFIELD_ENTRY_IGNORE( CSAM, StatDangerousWrite),
|
---|
| 261 | SSMFIELD_ENTRY_IGNORE( CSAM, StatInstrCacheHit),
|
---|
| 262 | SSMFIELD_ENTRY_IGNORE( CSAM, StatInstrCacheMiss),
|
---|
| 263 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPagePATM),
|
---|
| 264 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageCSAM),
|
---|
| 265 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageREM),
|
---|
| 266 | SSMFIELD_ENTRY_IGNORE( CSAM, StatNrUserPages),
|
---|
| 267 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageMonitor),
|
---|
| 268 | SSMFIELD_ENTRY_IGNORE( CSAM, StatPageRemoveREMFlush),
|
---|
| 269 | SSMFIELD_ENTRY_IGNORE( CSAM, StatBitmapAlloc),
|
---|
| 270 | SSMFIELD_ENTRY_IGNORE( CSAM, StatScanNextFunction),
|
---|
| 271 | SSMFIELD_ENTRY_IGNORE( CSAM, StatScanNextFunctionFailed),
|
---|
| 272 | SSMFIELD_ENTRY_TERM()
|
---|
| 273 | };
|
---|
[1] | 274 |
|
---|
[57008] | 275 |
|
---|
[23765] | 276 | /** Fake type to simplify g_aCsamPDBitmapArray construction. */
|
---|
| 277 | typedef struct
|
---|
| 278 | {
|
---|
| 279 | uint8_t *a[CSAM_PGDIRBMP_CHUNKS];
|
---|
| 280 | } CSAMPDBITMAPARRAY;
|
---|
| 281 |
|
---|
[1] | 282 | /**
|
---|
[23765] | 283 | * SSM descriptor table for the CSAM::pPDBitmapHC array.
|
---|
| 284 | */
|
---|
| 285 | static SSMFIELD const g_aCsamPDBitmapArray[] =
|
---|
| 286 | {
|
---|
| 287 | SSMFIELD_ENTRY_HCPTR_NI_ARRAY(CSAMPDBITMAPARRAY, a),
|
---|
| 288 | SSMFIELD_ENTRY_TERM()
|
---|
| 289 | };
|
---|
| 290 |
|
---|
[57008] | 291 |
|
---|
[23765] | 292 | /**
|
---|
[57008] | 293 | * SSM descriptor table for the CSAMPAGE structure.
|
---|
[23765] | 294 | */
|
---|
[57008] | 295 | static const SSMFIELD g_aCsamPageFields[] =
|
---|
| 296 | {
|
---|
| 297 | SSMFIELD_ENTRY_RCPTR( CSAMPAGE, pPageGC),
|
---|
| 298 | SSMFIELD_ENTRY_GCPHYS( CSAMPAGE, GCPhys),
|
---|
| 299 | SSMFIELD_ENTRY( CSAMPAGE, fFlags),
|
---|
| 300 | SSMFIELD_ENTRY( CSAMPAGE, uSize),
|
---|
| 301 | SSMFIELD_ENTRY_HCPTR_NI( CSAMPAGE, pBitmap),
|
---|
| 302 | SSMFIELD_ENTRY( CSAMPAGE, fCode32),
|
---|
| 303 | SSMFIELD_ENTRY( CSAMPAGE, fMonitorActive),
|
---|
| 304 | SSMFIELD_ENTRY( CSAMPAGE, fMonitorInvalidation),
|
---|
| 305 | SSMFIELD_ENTRY( CSAMPAGE, enmTag),
|
---|
| 306 | SSMFIELD_ENTRY( CSAMPAGE, u64Hash),
|
---|
| 307 | SSMFIELD_ENTRY_TERM()
|
---|
| 308 | };
|
---|
| 309 |
|
---|
| 310 | /**
|
---|
| 311 | * SSM descriptor table for the CSAMPAGEREC structure, putmem fashion.
|
---|
| 312 | */
|
---|
[23765] | 313 | static const SSMFIELD g_aCsamPageRecFields[] =
|
---|
| 314 | {
|
---|
[23777] | 315 | SSMFIELD_ENTRY_IGN_HCPTR( CSAMPAGEREC, Core.Key),
|
---|
| 316 | SSMFIELD_ENTRY_IGN_HCPTR( CSAMPAGEREC, Core.pLeft),
|
---|
| 317 | SSMFIELD_ENTRY_IGN_HCPTR( CSAMPAGEREC, Core.pRight),
|
---|
[23765] | 318 | SSMFIELD_ENTRY_IGNORE( CSAMPAGEREC, Core.uchHeight),
|
---|
| 319 | SSMFIELD_ENTRY_PAD_HC_AUTO( 3, 7),
|
---|
| 320 | SSMFIELD_ENTRY_RCPTR( CSAMPAGEREC, page.pPageGC),
|
---|
| 321 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
[23772] | 322 | SSMFIELD_ENTRY_PAD_MSC32_AUTO( 4),
|
---|
[23765] | 323 | SSMFIELD_ENTRY_GCPHYS( CSAMPAGEREC, page.GCPhys),
|
---|
| 324 | SSMFIELD_ENTRY( CSAMPAGEREC, page.fFlags),
|
---|
| 325 | SSMFIELD_ENTRY( CSAMPAGEREC, page.uSize),
|
---|
| 326 | SSMFIELD_ENTRY_PAD_HC_AUTO( 0, 4),
|
---|
| 327 | SSMFIELD_ENTRY_HCPTR_NI( CSAMPAGEREC, page.pBitmap),
|
---|
| 328 | SSMFIELD_ENTRY( CSAMPAGEREC, page.fCode32),
|
---|
| 329 | SSMFIELD_ENTRY( CSAMPAGEREC, page.fMonitorActive),
|
---|
| 330 | SSMFIELD_ENTRY( CSAMPAGEREC, page.fMonitorInvalidation),
|
---|
| 331 | SSMFIELD_ENTRY_PAD_HC_AUTO( 1, 1),
|
---|
| 332 | SSMFIELD_ENTRY( CSAMPAGEREC, page.enmTag),
|
---|
| 333 | SSMFIELD_ENTRY( CSAMPAGEREC, page.u64Hash),
|
---|
| 334 | SSMFIELD_ENTRY_TERM()
|
---|
| 335 | };
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
[1] | 339 | * Initializes the CSAM.
|
---|
| 340 | *
|
---|
| 341 | * @returns VBox status code.
|
---|
[58122] | 342 | * @param pVM The cross context VM structure.
|
---|
[1] | 343 | */
|
---|
[44385] | 344 | VMMR3_INT_DECL(int) CSAMR3Init(PVM pVM)
|
---|
[1] | 345 | {
|
---|
| 346 | int rc;
|
---|
| 347 |
|
---|
[45620] | 348 | /*
|
---|
| 349 | * We only need a saved state dummy loader if HM is enabled.
|
---|
| 350 | */
|
---|
[70948] | 351 | if (!VM_IS_RAW_MODE_ENABLED(pVM))
|
---|
[45620] | 352 | {
|
---|
| 353 | pVM->fCSAMEnabled = false;
|
---|
[50575] | 354 | return SSMR3RegisterStub(pVM, "CSAM", 0);
|
---|
[45620] | 355 | }
|
---|
| 356 |
|
---|
| 357 | /*
|
---|
| 358 | * Raw-mode.
|
---|
| 359 | */
|
---|
[1] | 360 | LogFlow(("CSAMR3Init\n"));
|
---|
| 361 |
|
---|
| 362 | /* Allocate bitmap for the page directory. */
|
---|
| 363 | rc = MMR3HyperAllocOnceNoRel(pVM, CSAM_PGDIRBMP_CHUNKS*sizeof(RTHCPTR), 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDBitmapHC);
|
---|
| 364 | AssertRCReturn(rc, rc);
|
---|
[9344] | 365 | rc = MMR3HyperAllocOnceNoRel(pVM, CSAM_PGDIRBMP_CHUNKS*sizeof(RTRCPTR), 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDGCBitmapHC);
|
---|
[1] | 366 | AssertRCReturn(rc, rc);
|
---|
[13827] | 367 | pVM->csam.s.pPDBitmapGC = MMHyperR3ToRC(pVM, pVM->csam.s.pPDGCBitmapHC);
|
---|
| 368 | pVM->csam.s.pPDHCBitmapGC = MMHyperR3ToRC(pVM, pVM->csam.s.pPDBitmapHC);
|
---|
[1] | 369 |
|
---|
| 370 | rc = csamReinit(pVM);
|
---|
| 371 | AssertRCReturn(rc, rc);
|
---|
| 372 |
|
---|
| 373 | /*
|
---|
[55889] | 374 | * Register virtual handler types.
|
---|
| 375 | */
|
---|
| 376 | rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_WRITE, false /*fRelocUserRC*/,
|
---|
| 377 | NULL /*pfnInvalidateR3 */,
|
---|
[55937] | 378 | csamCodePageWriteHandler,
|
---|
[56052] | 379 | "csamCodePageWriteHandler", "csamRCCodePageWritePfHandler",
|
---|
[55889] | 380 | "CSAM code page write handler",
|
---|
| 381 | &pVM->csam.s.hCodePageWriteType);
|
---|
| 382 | AssertLogRelRCReturn(rc, rc);
|
---|
| 383 | rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_WRITE, false /*fRelocUserRC*/,
|
---|
[55895] | 384 | csamR3CodePageInvalidate,
|
---|
[55937] | 385 | csamCodePageWriteHandler,
|
---|
[56052] | 386 | "csamCodePageWriteHandler", "csamRCCodePageWritePfHandler",
|
---|
[55889] | 387 | "CSAM code page write and invlpg handler",
|
---|
| 388 | &pVM->csam.s.hCodePageWriteAndInvPgType);
|
---|
| 389 | AssertLogRelRCReturn(rc, rc);
|
---|
| 390 |
|
---|
| 391 | /*
|
---|
[33540] | 392 | * Register save and load state notifiers.
|
---|
[1] | 393 | */
|
---|
[52771] | 394 | rc = SSMR3RegisterInternal(pVM, "CSAM", 0, CSAM_SAVED_STATE_VERSION, sizeof(pVM->csam.s) + PAGE_SIZE*16,
|
---|
[22480] | 395 | NULL, NULL, NULL,
|
---|
[55895] | 396 | NULL, csamR3Save, NULL,
|
---|
| 397 | NULL, csamR3Load, NULL);
|
---|
[1] | 398 | AssertRCReturn(rc, rc);
|
---|
| 399 |
|
---|
| 400 | STAM_REG(pVM, &pVM->csam.s.StatNrTraps, STAMTYPE_COUNTER, "/CSAM/PageTraps", STAMUNIT_OCCURENCES, "The number of CSAM page traps.");
|
---|
| 401 | STAM_REG(pVM, &pVM->csam.s.StatDangerousWrite, STAMTYPE_COUNTER, "/CSAM/DangerousWrites", STAMUNIT_OCCURENCES, "The number of dangerous writes that cause a context switch.");
|
---|
| 402 |
|
---|
| 403 | STAM_REG(pVM, &pVM->csam.s.StatNrPageNPHC, STAMTYPE_COUNTER, "/CSAM/HC/PageNotPresent", STAMUNIT_OCCURENCES, "The number of CSAM pages marked not present.");
|
---|
| 404 | STAM_REG(pVM, &pVM->csam.s.StatNrPageNPGC, STAMTYPE_COUNTER, "/CSAM/GC/PageNotPresent", STAMUNIT_OCCURENCES, "The number of CSAM pages marked not present.");
|
---|
| 405 | STAM_REG(pVM, &pVM->csam.s.StatNrPages, STAMTYPE_COUNTER, "/CSAM/PageRec/AddedRW", STAMUNIT_OCCURENCES, "The number of CSAM page records (RW monitoring).");
|
---|
| 406 | STAM_REG(pVM, &pVM->csam.s.StatNrPagesInv, STAMTYPE_COUNTER, "/CSAM/PageRec/AddedRWI", STAMUNIT_OCCURENCES, "The number of CSAM page records (RW & invalidation monitoring).");
|
---|
| 407 | STAM_REG(pVM, &pVM->csam.s.StatNrRemovedPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Removed", STAMUNIT_OCCURENCES, "The number of removed CSAM page records.");
|
---|
| 408 | 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.");
|
---|
[23] | 409 |
|
---|
[1] | 410 | STAM_REG(pVM, &pVM->csam.s.StatNrPatchPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Patch", STAMUNIT_OCCURENCES, "The number of CSAM patch page records.");
|
---|
| 411 | STAM_REG(pVM, &pVM->csam.s.StatNrUserPages, STAMTYPE_COUNTER, "/CSAM/PageRec/Ignore/User", STAMUNIT_OCCURENCES, "The number of CSAM user page records (ignored).");
|
---|
| 412 | STAM_REG(pVM, &pVM->csam.s.StatPagePATM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/PATM", STAMUNIT_OCCURENCES, "The number of PATM page records.");
|
---|
| 413 | STAM_REG(pVM, &pVM->csam.s.StatPageCSAM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/CSAM", STAMUNIT_OCCURENCES, "The number of CSAM page records.");
|
---|
| 414 | STAM_REG(pVM, &pVM->csam.s.StatPageREM, STAMTYPE_COUNTER, "/CSAM/PageRec/Type/REM", STAMUNIT_OCCURENCES, "The number of REM page records.");
|
---|
| 415 | STAM_REG(pVM, &pVM->csam.s.StatPageMonitor, STAMTYPE_COUNTER, "/CSAM/PageRec/Monitored", STAMUNIT_OCCURENCES, "The number of monitored pages.");
|
---|
| 416 |
|
---|
| 417 | STAM_REG(pVM, &pVM->csam.s.StatCodePageModified, STAMTYPE_COUNTER, "/CSAM/Monitor/DirtyPage", STAMUNIT_OCCURENCES, "The number of code page modifications.");
|
---|
| 418 |
|
---|
| 419 | STAM_REG(pVM, &pVM->csam.s.StatNrFlushes, STAMTYPE_COUNTER, "/CSAM/PageFlushes", STAMUNIT_OCCURENCES, "The number of CSAM page flushes.");
|
---|
| 420 | STAM_REG(pVM, &pVM->csam.s.StatNrFlushesSkipped, STAMTYPE_COUNTER, "/CSAM/PageFlushesSkipped", STAMUNIT_OCCURENCES, "The number of CSAM page flushes that were skipped.");
|
---|
| 421 | STAM_REG(pVM, &pVM->csam.s.StatNrKnownPagesHC, STAMTYPE_COUNTER, "/CSAM/HC/KnownPageRecords", STAMUNIT_OCCURENCES, "The number of known CSAM page records.");
|
---|
| 422 | STAM_REG(pVM, &pVM->csam.s.StatNrKnownPagesGC, STAMTYPE_COUNTER, "/CSAM/GC/KnownPageRecords", STAMUNIT_OCCURENCES, "The number of known CSAM page records.");
|
---|
| 423 | STAM_REG(pVM, &pVM->csam.s.StatNrInstr, STAMTYPE_COUNTER, "/CSAM/ScannedInstr", STAMUNIT_OCCURENCES, "The number of scanned instructions.");
|
---|
| 424 | STAM_REG(pVM, &pVM->csam.s.StatNrBytesRead, STAMTYPE_COUNTER, "/CSAM/BytesRead", STAMUNIT_OCCURENCES, "The number of bytes read for scanning.");
|
---|
| 425 | STAM_REG(pVM, &pVM->csam.s.StatNrOpcodeRead, STAMTYPE_COUNTER, "/CSAM/OpcodeBytesRead", STAMUNIT_OCCURENCES, "The number of opcode bytes read by the recompiler.");
|
---|
| 426 |
|
---|
| 427 | STAM_REG(pVM, &pVM->csam.s.StatBitmapAlloc, STAMTYPE_COUNTER, "/CSAM/Alloc/PageBitmap", STAMUNIT_OCCURENCES, "The number of page bitmap allocations.");
|
---|
| 428 |
|
---|
| 429 | STAM_REG(pVM, &pVM->csam.s.StatInstrCacheHit, STAMTYPE_COUNTER, "/CSAM/Cache/Hit", STAMUNIT_OCCURENCES, "The number of dangerous instruction cache hits.");
|
---|
| 430 | STAM_REG(pVM, &pVM->csam.s.StatInstrCacheMiss, STAMTYPE_COUNTER, "/CSAM/Cache/Miss", STAMUNIT_OCCURENCES, "The number of dangerous instruction cache misses.");
|
---|
| 431 |
|
---|
| 432 | STAM_REG(pVM, &pVM->csam.s.StatScanNextFunction, STAMTYPE_COUNTER, "/CSAM/Function/Scan/Success", STAMUNIT_OCCURENCES, "The number of found functions beyond the ret border.");
|
---|
| 433 | STAM_REG(pVM, &pVM->csam.s.StatScanNextFunctionFailed, STAMTYPE_COUNTER, "/CSAM/Function/Scan/Failed", STAMUNIT_OCCURENCES, "The number of refused functions beyond the ret border.");
|
---|
[23] | 434 |
|
---|
[1] | 435 | STAM_REG(pVM, &pVM->csam.s.StatTime, STAMTYPE_PROFILE, "/PROF/CSAM/Scan", STAMUNIT_TICKS_PER_CALL, "Scanning overhead.");
|
---|
| 436 | STAM_REG(pVM, &pVM->csam.s.StatTimeCheckAddr, STAMTYPE_PROFILE, "/PROF/CSAM/CheckAddr", STAMUNIT_TICKS_PER_CALL, "Address check overhead.");
|
---|
| 437 | STAM_REG(pVM, &pVM->csam.s.StatTimeAddrConv, STAMTYPE_PROFILE, "/PROF/CSAM/AddrConv", STAMUNIT_TICKS_PER_CALL, "Address conversion overhead.");
|
---|
| 438 | STAM_REG(pVM, &pVM->csam.s.StatTimeFlushPage, STAMTYPE_PROFILE, "/PROF/CSAM/FlushPage", STAMUNIT_TICKS_PER_CALL, "Page flushing overhead.");
|
---|
| 439 | STAM_REG(pVM, &pVM->csam.s.StatTimeDisasm, STAMTYPE_PROFILE, "/PROF/CSAM/Disasm", STAMUNIT_TICKS_PER_CALL, "Disassembly overhead.");
|
---|
| 440 | STAM_REG(pVM, &pVM->csam.s.StatFlushDirtyPages, STAMTYPE_PROFILE, "/PROF/CSAM/FlushDirtyPage", STAMUNIT_TICKS_PER_CALL, "Dirty page flushing overhead.");
|
---|
| 441 | STAM_REG(pVM, &pVM->csam.s.StatCheckGates, STAMTYPE_PROFILE, "/PROF/CSAM/CheckGates", STAMUNIT_TICKS_PER_CALL, "CSAMR3CheckGates overhead.");
|
---|
| 442 |
|
---|
[7628] | 443 | /*
|
---|
[452] | 444 | * Check CFGM option and enable/disable CSAM.
|
---|
| 445 | */
|
---|
| 446 | bool fEnabled;
|
---|
| 447 | rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "CSAMEnabled", &fEnabled);
|
---|
[13816] | 448 | if (RT_FAILURE(rc))
|
---|
[1] | 449 | #ifdef CSAM_ENABLE
|
---|
[452] | 450 | fEnabled = true;
|
---|
| 451 | #else
|
---|
| 452 | fEnabled = false;
|
---|
[1] | 453 | #endif
|
---|
[452] | 454 | if (fEnabled)
|
---|
| 455 | CSAMEnableScanning(pVM);
|
---|
[1] | 456 |
|
---|
| 457 | #ifdef VBOX_WITH_DEBUGGER
|
---|
| 458 | /*
|
---|
| 459 | * Debugger commands.
|
---|
| 460 | */
|
---|
| 461 | static bool fRegisteredCmds = false;
|
---|
| 462 | if (!fRegisteredCmds)
|
---|
| 463 | {
|
---|
[25273] | 464 | rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
|
---|
[13816] | 465 | if (RT_SUCCESS(rc))
|
---|
[1] | 466 | fRegisteredCmds = true;
|
---|
| 467 | }
|
---|
| 468 | #endif
|
---|
| 469 |
|
---|
| 470 | return VINF_SUCCESS;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | /**
|
---|
| 474 | * (Re)initializes CSAM
|
---|
| 475 | *
|
---|
[58122] | 476 | * @param pVM The cross context VM structure.
|
---|
[1] | 477 | */
|
---|
| 478 | static int csamReinit(PVM pVM)
|
---|
| 479 | {
|
---|
| 480 | /*
|
---|
| 481 | * Assert alignment and sizes.
|
---|
| 482 | */
|
---|
[73097] | 483 | AssertRelease(!(RT_UOFFSETOF(VM, csam.s) & 31));
|
---|
[1] | 484 | AssertRelease(sizeof(pVM->csam.s) <= sizeof(pVM->csam.padding));
|
---|
[70948] | 485 | AssertRelease(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[1] | 486 |
|
---|
| 487 | /*
|
---|
| 488 | * Setup any fixed pointers and offsets.
|
---|
| 489 | */
|
---|
[73097] | 490 | pVM->csam.s.offVM = RT_UOFFSETOF(VM, patm);
|
---|
[1] | 491 |
|
---|
| 492 | pVM->csam.s.fGatesChecked = false;
|
---|
| 493 | pVM->csam.s.fScanningStarted = false;
|
---|
| 494 |
|
---|
[19141] | 495 | PVMCPU pVCpu = &pVM->aCpus[0]; /* raw mode implies 1 VPCU */
|
---|
| 496 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_CSAM_PENDING_ACTION);
|
---|
[1] | 497 | pVM->csam.s.cDirtyPages = 0;
|
---|
| 498 | /* not necessary */
|
---|
| 499 | memset(pVM->csam.s.pvDirtyBasePage, 0, sizeof(pVM->csam.s.pvDirtyBasePage));
|
---|
| 500 | memset(pVM->csam.s.pvDirtyFaultPage, 0, sizeof(pVM->csam.s.pvDirtyFaultPage));
|
---|
| 501 |
|
---|
| 502 | memset(&pVM->csam.s.aDangerousInstr, 0, sizeof(pVM->csam.s.aDangerousInstr));
|
---|
| 503 | pVM->csam.s.cDangerousInstr = 0;
|
---|
| 504 | pVM->csam.s.iDangerousInstr = 0;
|
---|
| 505 |
|
---|
[5198] | 506 | memset(pVM->csam.s.pvCallInstruction, 0, sizeof(pVM->csam.s.pvCallInstruction));
|
---|
| 507 | pVM->csam.s.iCallInstruction = 0;
|
---|
| 508 |
|
---|
[1] | 509 | /** @note never mess with the pgdir bitmap here! */
|
---|
| 510 | return VINF_SUCCESS;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /**
|
---|
| 514 | * Applies relocations to data and code managed by this
|
---|
| 515 | * component. This function will be called at init and
|
---|
| 516 | * whenever the VMM need to relocate itself inside the GC.
|
---|
| 517 | *
|
---|
| 518 | * The csam will update the addresses used by the switcher.
|
---|
| 519 | *
|
---|
[58122] | 520 | * @param pVM The cross context VM structure.
|
---|
[1] | 521 | * @param offDelta Relocation delta.
|
---|
| 522 | */
|
---|
[44385] | 523 | VMMR3_INT_DECL(void) CSAMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
[1] | 524 | {
|
---|
[70948] | 525 | if (offDelta && VM_IS_RAW_MODE_ENABLED(pVM))
|
---|
[1] | 526 | {
|
---|
| 527 | /* Adjust pgdir and page bitmap pointers. */
|
---|
[13827] | 528 | pVM->csam.s.pPDBitmapGC = MMHyperR3ToRC(pVM, pVM->csam.s.pPDGCBitmapHC);
|
---|
| 529 | pVM->csam.s.pPDHCBitmapGC = MMHyperR3ToRC(pVM, pVM->csam.s.pPDBitmapHC);
|
---|
[1] | 530 |
|
---|
| 531 | for(int i=0;i<CSAM_PGDIRBMP_CHUNKS;i++)
|
---|
| 532 | {
|
---|
| 533 | if (pVM->csam.s.pPDGCBitmapHC[i])
|
---|
| 534 | {
|
---|
| 535 | pVM->csam.s.pPDGCBitmapHC[i] += offDelta;
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 | }
|
---|
| 539 | return;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | /**
|
---|
| 543 | * Terminates the csam.
|
---|
| 544 | *
|
---|
| 545 | * Termination means cleaning up and freeing all resources,
|
---|
| 546 | * the VM it self is at this point powered off or suspended.
|
---|
| 547 | *
|
---|
| 548 | * @returns VBox status code.
|
---|
[58122] | 549 | * @param pVM The cross context VM structure.
|
---|
[1] | 550 | */
|
---|
[44385] | 551 | VMMR3_INT_DECL(int) CSAMR3Term(PVM pVM)
|
---|
[1] | 552 | {
|
---|
[70948] | 553 | if (!VM_IS_RAW_MODE_ENABLED(pVM))
|
---|
[45620] | 554 | return VINF_SUCCESS;
|
---|
| 555 |
|
---|
[1] | 556 | int rc;
|
---|
| 557 |
|
---|
| 558 | rc = CSAMR3Reset(pVM);
|
---|
| 559 | AssertRC(rc);
|
---|
| 560 |
|
---|
[63560] | 561 | /** @todo triggers assertion in MMHyperFree */
|
---|
[1] | 562 | #if 0
|
---|
| 563 | for(int i=0;i<CSAM_PAGEBMP_CHUNKS;i++)
|
---|
| 564 | {
|
---|
| 565 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
| 566 | MMHyperFree(pVM, pVM->csam.s.pPDBitmapHC[i]);
|
---|
| 567 | }
|
---|
| 568 | #endif
|
---|
| 569 |
|
---|
| 570 | return VINF_SUCCESS;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | /**
|
---|
| 574 | * CSAM reset callback.
|
---|
| 575 | *
|
---|
| 576 | * @returns VBox status code.
|
---|
[58122] | 577 | * @param pVM The cross context VM structure.
|
---|
[1] | 578 | */
|
---|
[44385] | 579 | VMMR3_INT_DECL(int) CSAMR3Reset(PVM pVM)
|
---|
[1] | 580 | {
|
---|
[70948] | 581 | if (!VM_IS_RAW_MODE_ENABLED(pVM))
|
---|
[45620] | 582 | return VINF_SUCCESS;
|
---|
| 583 |
|
---|
[1] | 584 | /* Clear page bitmaps. */
|
---|
[45620] | 585 | for (int i = 0; i < CSAM_PGDIRBMP_CHUNKS; i++)
|
---|
[1] | 586 | {
|
---|
| 587 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
| 588 | {
|
---|
| 589 | Assert((CSAM_PAGE_BITMAP_SIZE& 3) == 0);
|
---|
| 590 | ASMMemZero32(pVM->csam.s.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
| 591 | }
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | /* Remove all CSAM page records. */
|
---|
[45620] | 595 | for (;;)
|
---|
[1] | 596 | {
|
---|
| 597 | PCSAMPAGEREC pPageRec = (PCSAMPAGEREC)RTAvlPVGetBestFit(&pVM->csam.s.pPageTree, 0, true);
|
---|
[45620] | 598 | if (!pPageRec)
|
---|
[1] | 599 | break;
|
---|
[45620] | 600 | csamRemovePageRecord(pVM, pPageRec->page.pPageGC);
|
---|
[1] | 601 | }
|
---|
| 602 | Assert(!pVM->csam.s.pPageTree);
|
---|
| 603 |
|
---|
| 604 | csamReinit(pVM);
|
---|
| 605 |
|
---|
| 606 | return VINF_SUCCESS;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 |
|
---|
| 610 | /**
|
---|
| 611 | * Callback function for RTAvlPVDoWithAll
|
---|
| 612 | *
|
---|
| 613 | * Counts the number of records in the tree
|
---|
| 614 | *
|
---|
| 615 | * @returns VBox status code.
|
---|
| 616 | * @param pNode Current node
|
---|
| 617 | * @param pcPatches Pointer to patch counter
|
---|
| 618 | */
|
---|
[57008] | 619 | static DECLCALLBACK(int) csamR3SaveCountRecord(PAVLPVNODECORE pNode, void *pcPatches)
|
---|
[1] | 620 | {
|
---|
[39078] | 621 | NOREF(pNode);
|
---|
[57008] | 622 | *(uint32_t *)pcPatches += 1;
|
---|
[1] | 623 | return VINF_SUCCESS;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | /**
|
---|
[57008] | 627 | * Callback function for RTAvlPVDoWithAll for saving a page record.
|
---|
[1] | 628 | *
|
---|
| 629 | * @returns VBox status code.
|
---|
| 630 | * @param pNode Current node
|
---|
[57008] | 631 | * @param pvVM Pointer to the VM
|
---|
[1] | 632 | */
|
---|
[57008] | 633 | static DECLCALLBACK(int) csamR3SavePageState(PAVLPVNODECORE pNode, void *pvVM)
|
---|
[1] | 634 | {
|
---|
| 635 | PCSAMPAGEREC pPage = (PCSAMPAGEREC)pNode;
|
---|
[57008] | 636 | PVM pVM = (PVM)pvVM;
|
---|
[1] | 637 | PSSMHANDLE pSSM = pVM->csam.s.savedstate.pSSM;
|
---|
| 638 |
|
---|
[57008] | 639 | int rc = SSMR3PutStructEx(pSSM, &pPage->page, sizeof(pPage->page), 0 /*fFlags*/, &g_aCsamPageFields[0], NULL);
|
---|
| 640 | AssertLogRelRCReturn(rc, rc);
|
---|
[1] | 641 |
|
---|
[57008] | 642 | if (pPage->page.pBitmap)
|
---|
| 643 | SSMR3PutMem(pSSM, pPage->page.pBitmap, CSAM_PAGE_BITMAP_SIZE);
|
---|
[1] | 644 |
|
---|
| 645 | return VINF_SUCCESS;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | /**
|
---|
| 649 | * Execute state save operation.
|
---|
| 650 | *
|
---|
| 651 | * @returns VBox status code.
|
---|
[58122] | 652 | * @param pVM The cross context VM structure.
|
---|
[1] | 653 | * @param pSSM SSM operation handle.
|
---|
| 654 | */
|
---|
[55895] | 655 | static DECLCALLBACK(int) csamR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
[1] | 656 | {
|
---|
| 657 | int rc;
|
---|
| 658 |
|
---|
| 659 | /*
|
---|
| 660 | * Count the number of page records in the tree (feeling lazy)
|
---|
| 661 | */
|
---|
[57008] | 662 | pVM->csam.s.savedstate.cPageRecords = 0;
|
---|
| 663 | RTAvlPVDoWithAll(&pVM->csam.s.pPageTree, true, csamR3SaveCountRecord, &pVM->csam.s.savedstate.cPageRecords);
|
---|
[1] | 664 |
|
---|
| 665 | /*
|
---|
[57008] | 666 | * Save CSAM structure.
|
---|
[1] | 667 | */
|
---|
| 668 | pVM->csam.s.savedstate.pSSM = pSSM;
|
---|
[57008] | 669 | rc = SSMR3PutStructEx(pSSM, &pVM->csam.s, sizeof(pVM->csam.s), 0 /*fFlags*/, g_aCsamFields, NULL);
|
---|
| 670 | AssertLogRelRCReturn(rc, rc);
|
---|
[1] | 671 |
|
---|
[57008] | 672 | /*
|
---|
| 673 | * Save pgdir bitmap.
|
---|
| 674 | */
|
---|
| 675 | SSMR3PutU32(pSSM, CSAM_PGDIRBMP_CHUNKS);
|
---|
| 676 | SSMR3PutU32(pSSM, CSAM_PAGE_BITMAP_SIZE);
|
---|
| 677 | for (uint32_t i = 0; i < CSAM_PGDIRBMP_CHUNKS; i++)
|
---|
| 678 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
[1] | 679 | {
|
---|
[57008] | 680 | SSMR3PutU32(pSSM, i);
|
---|
| 681 | SSMR3PutMem(pSSM, pVM->csam.s.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
[1] | 682 | }
|
---|
[57008] | 683 | SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
|
---|
[1] | 684 |
|
---|
| 685 | /*
|
---|
| 686 | * Save page records
|
---|
| 687 | */
|
---|
[57008] | 688 | pVM->csam.s.savedstate.pSSM = pSSM;
|
---|
| 689 | rc = RTAvlPVDoWithAll(&pVM->csam.s.pPageTree, true, csamR3SavePageState, pVM);
|
---|
[1] | 690 | AssertRCReturn(rc, rc);
|
---|
| 691 |
|
---|
[57008] | 692 | pVM->csam.s.savedstate.pSSM = NULL;
|
---|
[1] | 693 | return VINF_SUCCESS;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[45620] | 696 |
|
---|
[1] | 697 | /**
|
---|
| 698 | * Execute state load operation.
|
---|
| 699 | *
|
---|
| 700 | * @returns VBox status code.
|
---|
[58122] | 701 | * @param pVM The cross context VM structure.
|
---|
[1] | 702 | * @param pSSM SSM operation handle.
|
---|
[22480] | 703 | * @param uVersion Data layout version.
|
---|
[22793] | 704 | * @param uPass The data pass.
|
---|
[1] | 705 | */
|
---|
[55895] | 706 | static DECLCALLBACK(int) csamR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
[1] | 707 | {
|
---|
[57008] | 708 | int rc;
|
---|
[1] | 709 |
|
---|
| 710 | /*
|
---|
[57008] | 711 | * Check preconditions.
|
---|
[1] | 712 | */
|
---|
[57008] | 713 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
| 714 | Assert(pVM->csam.s.savedstate.pSSM == NULL);
|
---|
| 715 | AssertLogRelMsgReturn(uVersion >= CSAM_SAVED_STATE_VERSION_PUT_MEM && uVersion <= CSAM_SAVED_STATE_VERSION,
|
---|
[61122] | 716 | ("uVersion=%d (%#x)\n", uVersion, uVersion),
|
---|
[57008] | 717 | VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
|
---|
[1] | 718 |
|
---|
[57008] | 719 | if (uVersion >= CSAM_SAVED_STATE_VERSION_PUT_STRUCT)
|
---|
| 720 | {
|
---|
| 721 | /*
|
---|
| 722 | * Restore the SSMR3PutStructEx fashioned state.
|
---|
| 723 | */
|
---|
| 724 | rc = SSMR3GetStructEx(pSSM, &pVM->csam.s, sizeof(pVM->csam.s), 0 /*fFlags*/, &g_aCsamFields[0], NULL);
|
---|
[1] | 725 |
|
---|
[57008] | 726 | /*
|
---|
| 727 | * Restore page bitmaps
|
---|
| 728 | */
|
---|
| 729 | uint32_t cPgDirBmpChunks = 0;
|
---|
| 730 | rc = SSMR3GetU32(pSSM, &cPgDirBmpChunks);
|
---|
| 731 | uint32_t cbPgDirBmpChunk = 0;
|
---|
| 732 | rc = SSMR3GetU32(pSSM, &cbPgDirBmpChunk);
|
---|
| 733 | AssertRCReturn(rc, rc);
|
---|
| 734 | AssertLogRelMsgReturn(cPgDirBmpChunks <= CSAM_PGDIRBMP_CHUNKS,
|
---|
| 735 | ("cPgDirBmpChunks=%#x (vs %#x)\n", cPgDirBmpChunks, CSAM_PGDIRBMP_CHUNKS),
|
---|
| 736 | VERR_SSM_UNEXPECTED_DATA);
|
---|
| 737 | AssertLogRelMsgReturn(cbPgDirBmpChunk <= CSAM_PAGE_BITMAP_SIZE,
|
---|
| 738 | ("cbPgDirBmpChunk=%#x (vs %#x)\n", cbPgDirBmpChunk, CSAM_PAGE_BITMAP_SIZE),
|
---|
| 739 | VERR_SSM_UNEXPECTED_DATA);
|
---|
| 740 | for (uint32_t i = 0; i < CSAM_PGDIRBMP_CHUNKS; i++)
|
---|
| 741 | {
|
---|
| 742 | Assert(!pVM->csam.s.pPDBitmapHC[i]);
|
---|
| 743 | Assert(!pVM->csam.s.pPDGCBitmapHC[i]);
|
---|
| 744 | }
|
---|
| 745 | for (uint32_t iNext = 0;;)
|
---|
| 746 | {
|
---|
| 747 | uint32_t iThis;
|
---|
| 748 | rc = SSMR3GetU32(pSSM, &iThis);
|
---|
| 749 | AssertLogRelRCReturn(rc, rc);
|
---|
| 750 | AssertLogRelMsgReturn(iThis >= iNext, ("iThis=%#x iNext=%#x\n", iThis, iNext), VERR_SSM_UNEXPECTED_DATA);
|
---|
| 751 | if (iThis == UINT32_MAX)
|
---|
| 752 | break;
|
---|
[1] | 753 |
|
---|
[57008] | 754 | rc = MMHyperAlloc(pVM, CSAM_PAGE_BITMAP_SIZE, 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDBitmapHC[iThis]);
|
---|
| 755 | AssertLogRelRCReturn(rc, rc);
|
---|
| 756 | pVM->csam.s.pPDGCBitmapHC[iThis] = MMHyperR3ToRC(pVM, pVM->csam.s.pPDBitmapHC[iThis]);
|
---|
[1830] | 757 |
|
---|
[57008] | 758 | rc = SSMR3GetMem(pSSM, pVM->csam.s.pPDBitmapHC[iThis], CSAM_PAGE_BITMAP_SIZE);
|
---|
| 759 | AssertLogRelRCReturn(rc, rc);
|
---|
| 760 | iNext = iThis + 1;
|
---|
| 761 | }
|
---|
[1] | 762 |
|
---|
[57008] | 763 | /*
|
---|
| 764 | * Restore page records
|
---|
| 765 | */
|
---|
| 766 | uint32_t const cPageRecords = pVM->csam.s.savedstate.cPageRecords + pVM->csam.s.savedstate.cPatchPageRecords;
|
---|
| 767 | for (uint32_t iPageRec = 0; iPageRec < cPageRecords; iPageRec++)
|
---|
[1] | 768 | {
|
---|
[57008] | 769 | CSAMPAGE PageRec;
|
---|
| 770 | RT_ZERO(PageRec);
|
---|
| 771 | rc = SSMR3GetStructEx(pSSM, &PageRec, sizeof(PageRec), 0 /*fFlags*/, &g_aCsamPageFields[0], NULL);
|
---|
| 772 | AssertLogRelRCReturn(rc, rc);
|
---|
| 773 |
|
---|
| 774 | /* Recreate the page record. */
|
---|
| 775 | PCSAMPAGE pPage = csamR3CreatePageRecord(pVM, PageRec.pPageGC, PageRec.enmTag, PageRec.fCode32,
|
---|
| 776 | PageRec.fMonitorInvalidation);
|
---|
| 777 | AssertReturn(pPage, VERR_NO_MEMORY);
|
---|
| 778 | pPage->GCPhys = PageRec.GCPhys;
|
---|
| 779 | pPage->fFlags = PageRec.fFlags;
|
---|
| 780 | pPage->u64Hash = PageRec.u64Hash;
|
---|
| 781 | if (PageRec.pBitmap)
|
---|
[1] | 782 | {
|
---|
[57008] | 783 | rc = SSMR3GetMem(pSSM, pPage->pBitmap, CSAM_PAGE_BITMAP_SIZE);
|
---|
| 784 | AssertLogRelRCReturn(rc, rc);
|
---|
[1] | 785 | }
|
---|
[57008] | 786 | else
|
---|
| 787 | {
|
---|
| 788 | MMR3HeapFree(pPage->pBitmap);
|
---|
| 789 | pPage->pBitmap = NULL;
|
---|
| 790 | }
|
---|
[1] | 791 | }
|
---|
| 792 | }
|
---|
[57008] | 793 | else
|
---|
[1] | 794 | {
|
---|
[57008] | 795 | /*
|
---|
| 796 | * Restore the old SSMR3PutMem fashioned state.
|
---|
| 797 | */
|
---|
[1] | 798 |
|
---|
[57008] | 799 | /* CSAM structure first. */
|
---|
| 800 | CSAM csamInfo;
|
---|
| 801 | RT_ZERO(csamInfo);
|
---|
| 802 | if ( SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(4, 3, 51)
|
---|
| 803 | && SSMR3HandleRevision(pSSM) >= 100346)
|
---|
| 804 | rc = SSMR3GetStructEx(pSSM, &csamInfo, sizeof(csamInfo), SSMSTRUCT_FLAGS_MEM_BAND_AID,
|
---|
| 805 | &g_aCsamFields500[0], NULL);
|
---|
| 806 | else
|
---|
| 807 | rc = SSMR3GetStructEx(pSSM, &csamInfo, sizeof(csamInfo), SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED,
|
---|
| 808 | &g_aCsamFieldsBefore500[0], NULL);
|
---|
[1] | 809 | AssertRCReturn(rc, rc);
|
---|
| 810 |
|
---|
[57008] | 811 | pVM->csam.s.fGatesChecked = csamInfo.fGatesChecked;
|
---|
| 812 | pVM->csam.s.fScanningStarted = csamInfo.fScanningStarted;
|
---|
| 813 |
|
---|
| 814 | /* Restore dirty code page info. */
|
---|
| 815 | pVM->csam.s.cDirtyPages = csamInfo.cDirtyPages;
|
---|
| 816 | memcpy(pVM->csam.s.pvDirtyBasePage, csamInfo.pvDirtyBasePage, sizeof(pVM->csam.s.pvDirtyBasePage));
|
---|
| 817 | memcpy(pVM->csam.s.pvDirtyFaultPage, csamInfo.pvDirtyFaultPage, sizeof(pVM->csam.s.pvDirtyFaultPage));
|
---|
| 818 |
|
---|
| 819 | /* Restore possible code page */
|
---|
| 820 | pVM->csam.s.cPossibleCodePages = csamInfo.cPossibleCodePages;
|
---|
| 821 | memcpy(pVM->csam.s.pvPossibleCodePage, csamInfo.pvPossibleCodePage, sizeof(pVM->csam.s.pvPossibleCodePage));
|
---|
| 822 |
|
---|
[1] | 823 | /*
|
---|
[57008] | 824 | * Restore pgdir bitmap (we'll change the pointers next).
|
---|
[1] | 825 | */
|
---|
[57008] | 826 | rc = SSMR3GetStructEx(pSSM, pVM->csam.s.pPDBitmapHC, sizeof(uint8_t *) * CSAM_PGDIRBMP_CHUNKS,
|
---|
| 827 | SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED, &g_aCsamPDBitmapArray[0], NULL);
|
---|
| 828 | AssertRCReturn(rc, rc);
|
---|
[1] | 829 |
|
---|
[57008] | 830 | /*
|
---|
| 831 | * Restore page bitmaps
|
---|
| 832 | */
|
---|
| 833 | for (unsigned i = 0; i < CSAM_PGDIRBMP_CHUNKS; i++)
|
---|
| 834 | if (pVM->csam.s.pPDBitmapHC[i])
|
---|
| 835 | {
|
---|
| 836 | rc = MMHyperAlloc(pVM, CSAM_PAGE_BITMAP_SIZE, 0, MM_TAG_CSAM, (void **)&pVM->csam.s.pPDBitmapHC[i]);
|
---|
| 837 | AssertLogRelRCReturn(rc, rc);
|
---|
| 838 | pVM->csam.s.pPDGCBitmapHC[i] = MMHyperR3ToRC(pVM, pVM->csam.s.pPDBitmapHC[i]);
|
---|
[1] | 839 |
|
---|
[57008] | 840 | /* Restore the bitmap. */
|
---|
| 841 | rc = SSMR3GetMem(pSSM, pVM->csam.s.pPDBitmapHC[i], CSAM_PAGE_BITMAP_SIZE);
|
---|
| 842 | AssertRCReturn(rc, rc);
|
---|
| 843 | }
|
---|
| 844 | else
|
---|
| 845 | {
|
---|
| 846 | Assert(!pVM->csam.s.pPDGCBitmapHC[i]);
|
---|
| 847 | pVM->csam.s.pPDGCBitmapHC[i] = 0;
|
---|
| 848 | }
|
---|
| 849 |
|
---|
| 850 | /*
|
---|
| 851 | * Restore page records
|
---|
| 852 | */
|
---|
| 853 | for (uint32_t i=0;i<csamInfo.savedstate.cPageRecords + csamInfo.savedstate.cPatchPageRecords;i++)
|
---|
[1] | 854 | {
|
---|
[57008] | 855 | CSAMPAGEREC page;
|
---|
| 856 | PCSAMPAGE pPage;
|
---|
| 857 |
|
---|
| 858 | RT_ZERO(page);
|
---|
| 859 | rc = SSMR3GetStructEx(pSSM, &page, sizeof(page), SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED, &g_aCsamPageRecFields[0], NULL);
|
---|
[1] | 860 | AssertRCReturn(rc, rc);
|
---|
[57008] | 861 |
|
---|
| 862 | /*
|
---|
| 863 | * Recreate the page record
|
---|
| 864 | */
|
---|
| 865 | pPage = csamR3CreatePageRecord(pVM, page.page.pPageGC, page.page.enmTag, page.page.fCode32, page.page.fMonitorInvalidation);
|
---|
| 866 | AssertReturn(pPage, VERR_NO_MEMORY);
|
---|
| 867 |
|
---|
| 868 | pPage->GCPhys = page.page.GCPhys;
|
---|
| 869 | pPage->fFlags = page.page.fFlags;
|
---|
| 870 | pPage->u64Hash = page.page.u64Hash;
|
---|
| 871 |
|
---|
| 872 | if (page.page.pBitmap)
|
---|
| 873 | {
|
---|
| 874 | rc = SSMR3GetMem(pSSM, pPage->pBitmap, CSAM_PAGE_BITMAP_SIZE);
|
---|
| 875 | AssertRCReturn(rc, rc);
|
---|
| 876 | }
|
---|
| 877 | else
|
---|
| 878 | {
|
---|
| 879 | MMR3HeapFree(pPage->pBitmap);
|
---|
| 880 | pPage->pBitmap = NULL;
|
---|
| 881 | }
|
---|
[1] | 882 | }
|
---|
[57008] | 883 |
|
---|
| 884 | /* Note: we don't restore aDangerousInstr; it will be recreated automatically. */
|
---|
| 885 | memset(&pVM->csam.s.aDangerousInstr, 0, sizeof(pVM->csam.s.aDangerousInstr));
|
---|
| 886 | pVM->csam.s.cDangerousInstr = 0;
|
---|
| 887 | pVM->csam.s.iDangerousInstr = 0;
|
---|
[1] | 888 | }
|
---|
| 889 | return VINF_SUCCESS;
|
---|
| 890 | }
|
---|
| 891 |
|
---|
| 892 | /**
|
---|
| 893 | * Convert guest context address to host context pointer
|
---|
| 894 | *
|
---|
[44362] | 895 | * @returns Byte pointer (ring-3 context) corresponding to pGCPtr on success,
|
---|
| 896 | * NULL on failure.
|
---|
[58122] | 897 | * @param pVM The cross context VM structure.
|
---|
[1] | 898 | * @param pCacheRec Address conversion cache record
|
---|
| 899 | * @param pGCPtr Guest context pointer
|
---|
| 900 | * @returns Host context pointer or NULL in case of an error
|
---|
| 901 | *
|
---|
| 902 | */
|
---|
[44362] | 903 | static uint8_t *csamR3GCVirtToHCVirt(PVM pVM, PCSAMP2GLOOKUPREC pCacheRec, RCPTRTYPE(uint8_t *) pGCPtr)
|
---|
[1] | 904 | {
|
---|
| 905 | int rc;
|
---|
[44362] | 906 | void *pHCPtr;
|
---|
[22890] | 907 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 908 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 909 |
|
---|
| 910 | STAM_PROFILE_START(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
| 911 |
|
---|
| 912 | pHCPtr = PATMR3GCPtrToHCPtr(pVM, pGCPtr);
|
---|
[32431] | 913 | if (pHCPtr)
|
---|
[44362] | 914 | return (uint8_t *)pHCPtr;
|
---|
[1] | 915 |
|
---|
| 916 | if (pCacheRec->pPageLocStartHC)
|
---|
| 917 | {
|
---|
| 918 | uint32_t offset = pGCPtr & PAGE_OFFSET_MASK;
|
---|
| 919 | if (pCacheRec->pGuestLoc == (pGCPtr & PAGE_BASE_GC_MASK))
|
---|
| 920 | {
|
---|
| 921 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
| 922 | return pCacheRec->pPageLocStartHC + offset;
|
---|
| 923 | }
|
---|
| 924 | }
|
---|
| 925 |
|
---|
[30500] | 926 | /* Release previous lock if any. */
|
---|
| 927 | if (pCacheRec->Lock.pvMap)
|
---|
| 928 | {
|
---|
| 929 | PGMPhysReleasePageMappingLock(pVM, &pCacheRec->Lock);
|
---|
| 930 | pCacheRec->Lock.pvMap = NULL;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, pGCPtr, (const void **)&pHCPtr, &pCacheRec->Lock);
|
---|
[1] | 934 | if (rc != VINF_SUCCESS)
|
---|
| 935 | {
|
---|
[13822] | 936 | //// AssertMsgRC(rc, ("MMR3PhysGCVirt2HCVirtEx failed for %RRv\n", pGCPtr));
|
---|
[1] | 937 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
| 938 | return NULL;
|
---|
| 939 | }
|
---|
| 940 |
|
---|
[44362] | 941 | pCacheRec->pPageLocStartHC = (uint8_t*)((uintptr_t)pHCPtr & PAGE_BASE_HC_MASK);
|
---|
[18927] | 942 | pCacheRec->pGuestLoc = pGCPtr & PAGE_BASE_GC_MASK;
|
---|
[1] | 943 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeAddrConv, a);
|
---|
[44362] | 944 | return (uint8_t *)pHCPtr;
|
---|
[1] | 945 | }
|
---|
| 946 |
|
---|
[41791] | 947 |
|
---|
| 948 | /** For csamR3ReadBytes. */
|
---|
| 949 | typedef struct CSAMDISINFO
|
---|
| 950 | {
|
---|
| 951 | PVM pVM;
|
---|
| 952 | uint8_t const *pbSrcInstr; /* aka pInstHC */
|
---|
| 953 | } CSAMDISINFO, *PCSAMDISINFO;
|
---|
| 954 |
|
---|
| 955 |
|
---|
[1] | 956 | /**
|
---|
[41658] | 957 | * @callback_method_impl{FNDISREADBYTES}
|
---|
[1] | 958 | */
|
---|
[41769] | 959 | static DECLCALLBACK(int) csamR3ReadBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
[1] | 960 | {
|
---|
[41791] | 961 | PCSAMDISINFO pDisInfo = (PCSAMDISINFO)pDis->pvUser;
|
---|
[1] | 962 |
|
---|
[41769] | 963 | /*
|
---|
| 964 | * We are not interested in patched instructions, so read the original opcode bytes.
|
---|
| 965 | *
|
---|
| 966 | * Note! single instruction patches (int3) are checked in CSAMR3AnalyseCallback
|
---|
| 967 | *
|
---|
| 968 | * Since we're decoding one instruction at the time, we don't need to be
|
---|
| 969 | * concerned about any patched instructions following the first one. We
|
---|
| 970 | * could in fact probably skip this PATM call for offInstr != 0.
|
---|
| 971 | */
|
---|
| 972 | size_t cbRead = cbMaxRead;
|
---|
| 973 | RTUINTPTR uSrcAddr = pDis->uInstrAddr + offInstr;
|
---|
[41791] | 974 | int rc = PATMR3ReadOrgInstr(pDisInfo->pVM, pDis->uInstrAddr + offInstr, &pDis->abInstr[offInstr], cbRead, &cbRead);
|
---|
[41769] | 975 | if (RT_SUCCESS(rc))
|
---|
| 976 | {
|
---|
| 977 | if (cbRead >= cbMinRead)
|
---|
| 978 | {
|
---|
[41771] | 979 | pDis->cbCachedInstr = offInstr + (uint8_t)cbRead;
|
---|
[41769] | 980 | return rc;
|
---|
| 981 | }
|
---|
[41760] | 982 |
|
---|
[41771] | 983 | cbMinRead -= (uint8_t)cbRead;
|
---|
| 984 | cbMaxRead -= (uint8_t)cbRead;
|
---|
| 985 | offInstr += (uint8_t)cbRead;
|
---|
[41769] | 986 | uSrcAddr += cbRead;
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 | /*
|
---|
| 990 | * The current byte isn't a patch instruction byte.
|
---|
| 991 | */
|
---|
[41791] | 992 | AssertPtr(pDisInfo->pbSrcInstr);
|
---|
[41769] | 993 | if ((pDis->uInstrAddr >> PAGE_SHIFT) == ((uSrcAddr + cbMaxRead - 1) >> PAGE_SHIFT))
|
---|
[1] | 994 | {
|
---|
[41791] | 995 | memcpy(&pDis->abInstr[offInstr], &pDisInfo->pbSrcInstr[offInstr], cbMaxRead);
|
---|
[41769] | 996 | offInstr += cbMaxRead;
|
---|
| 997 | rc = VINF_SUCCESS;
|
---|
[1] | 998 | }
|
---|
[41769] | 999 | else if ( (pDis->uInstrAddr >> PAGE_SHIFT) == ((uSrcAddr + cbMinRead - 1) >> PAGE_SHIFT)
|
---|
[41791] | 1000 | || PATMIsPatchGCAddr(pDisInfo->pVM, uSrcAddr) /** @todo does CSAM actually analyze patch code, or is this just a copy&past check? */
|
---|
[41769] | 1001 | )
|
---|
[41760] | 1002 | {
|
---|
[41873] | 1003 | memcpy(&pDis->abInstr[offInstr], &pDisInfo->pbSrcInstr[offInstr], cbMinRead);
|
---|
[41769] | 1004 | offInstr += cbMinRead;
|
---|
| 1005 | rc = VINF_SUCCESS;
|
---|
[41760] | 1006 | }
|
---|
[41769] | 1007 | else
|
---|
| 1008 | {
|
---|
| 1009 | /* Crossed page boundrary, pbSrcInstr is no good... */
|
---|
[41791] | 1010 | rc = PGMPhysSimpleReadGCPtr(VMMGetCpu0(pDisInfo->pVM), &pDis->abInstr[offInstr], uSrcAddr, cbMinRead);
|
---|
[41769] | 1011 | offInstr += cbMinRead;
|
---|
| 1012 | }
|
---|
[1] | 1013 |
|
---|
[41760] | 1014 | pDis->cbCachedInstr = offInstr;
|
---|
| 1015 | return rc;
|
---|
[1] | 1016 | }
|
---|
| 1017 |
|
---|
[41769] | 1018 | DECLINLINE(int) csamR3DISInstr(PVM pVM, RTRCPTR InstrGC, uint8_t *InstrHC, DISCPUMODE enmCpuMode,
|
---|
[41671] | 1019 | PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
[1] | 1020 | {
|
---|
[41791] | 1021 | CSAMDISINFO DisInfo = { pVM, InstrHC };
|
---|
[1] | 1022 | #ifdef DEBUG
|
---|
[41810] | 1023 | return DISInstrToStrEx(InstrGC, enmCpuMode, csamR3ReadBytes, &DisInfo, DISOPTYPE_ALL,
|
---|
[41671] | 1024 | pCpu, pcbInstr, pszOutput, cbOutput);
|
---|
[1] | 1025 | #else
|
---|
| 1026 | /* We are interested in everything except harmless stuff */
|
---|
[41671] | 1027 | if (pszOutput)
|
---|
[41810] | 1028 | return DISInstrToStrEx(InstrGC, enmCpuMode, csamR3ReadBytes, &DisInfo,
|
---|
| 1029 | ~(DISOPTYPE_INVALID | DISOPTYPE_HARMLESS | DISOPTYPE_RRM_MASK),
|
---|
[41671] | 1030 | pCpu, pcbInstr, pszOutput, cbOutput);
|
---|
[41886] | 1031 | return DISInstrEx(InstrGC, enmCpuMode, ~(DISOPTYPE_INVALID | DISOPTYPE_HARMLESS | DISOPTYPE_RRM_MASK),
|
---|
| 1032 | csamR3ReadBytes, &DisInfo, pCpu, pcbInstr);
|
---|
[1] | 1033 | #endif
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | /**
|
---|
| 1037 | * Analyses the instructions following the cli for compliance with our heuristics for cli
|
---|
| 1038 | *
|
---|
| 1039 | * @returns VBox status code.
|
---|
[58122] | 1040 | * @param pVM The cross context VM structure.
|
---|
[1] | 1041 | * @param pCpu CPU disassembly state
|
---|
| 1042 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
| 1043 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
| 1044 | * @param pCacheRec GC to HC cache record
|
---|
| 1045 | * @param pUserData User pointer (callback specific)
|
---|
| 1046 | *
|
---|
| 1047 | */
|
---|
[57389] | 1048 | static DECLCALLBACK(int) CSAMR3AnalyseCallback(PVM pVM, DISCPUSTATE *pCpu, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC,
|
---|
| 1049 | PCSAMP2GLOOKUPREC pCacheRec, void *pUserData)
|
---|
[1] | 1050 | {
|
---|
| 1051 | PCSAMPAGE pPage = (PCSAMPAGE)pUserData;
|
---|
| 1052 | int rc;
|
---|
[39078] | 1053 | NOREF(pInstrGC);
|
---|
[1] | 1054 |
|
---|
[41737] | 1055 | switch (pCpu->pCurInstr->uOpcode)
|
---|
[1] | 1056 | {
|
---|
| 1057 | case OP_INT:
|
---|
[41739] | 1058 | Assert(pCpu->Param1.fUse & DISUSE_IMMEDIATE8);
|
---|
[41741] | 1059 | if (pCpu->Param1.uValue == 3)
|
---|
[1] | 1060 | {
|
---|
| 1061 | //two byte int 3
|
---|
| 1062 | return VINF_SUCCESS;
|
---|
| 1063 | }
|
---|
| 1064 | break;
|
---|
| 1065 |
|
---|
[45276] | 1066 | /* removing breaks win2k guests? */
|
---|
| 1067 | case OP_IRET:
|
---|
| 1068 | if (EMIsRawRing1Enabled(pVM))
|
---|
| 1069 | break;
|
---|
[69046] | 1070 | RT_FALL_THRU();
|
---|
[45276] | 1071 |
|
---|
[1] | 1072 | case OP_ILLUD2:
|
---|
| 1073 | /* This appears to be some kind of kernel panic in Linux 2.4; no point to continue. */
|
---|
| 1074 | case OP_RETN:
|
---|
| 1075 | case OP_INT3:
|
---|
| 1076 | case OP_INVALID:
|
---|
| 1077 | return VINF_SUCCESS;
|
---|
| 1078 | }
|
---|
| 1079 |
|
---|
| 1080 | // Check for exit points
|
---|
[41737] | 1081 | switch (pCpu->pCurInstr->uOpcode)
|
---|
[1] | 1082 | {
|
---|
| 1083 | /* It's not a good idea to patch pushf instructions:
|
---|
| 1084 | * - increases the chance of conflicts (code jumping to the next instruction)
|
---|
| 1085 | * - better to patch the cli
|
---|
| 1086 | * - code that branches before the cli will likely hit an int 3
|
---|
| 1087 | * - in general doesn't offer any benefits as we don't allow nested patch blocks (IF is always 1)
|
---|
| 1088 | */
|
---|
| 1089 | case OP_PUSHF:
|
---|
| 1090 | case OP_POPF:
|
---|
| 1091 | break;
|
---|
| 1092 |
|
---|
| 1093 | case OP_CLI:
|
---|
| 1094 | {
|
---|
[41732] | 1095 | uint32_t cbInstrs = 0;
|
---|
| 1096 | uint32_t cbCurInstr = pCpu->cbInstr;
|
---|
| 1097 | bool fCode32 = pPage->fCode32;
|
---|
[1] | 1098 |
|
---|
[21443] | 1099 | Assert(fCode32);
|
---|
[1] | 1100 |
|
---|
[21443] | 1101 | PATMR3AddHint(pVM, pCurInstrGC, (fCode32) ? PATMFL_CODE32 : 0);
|
---|
| 1102 |
|
---|
[1] | 1103 | /* Make sure the instructions that follow the cli have not been encountered before. */
|
---|
| 1104 | while (true)
|
---|
| 1105 | {
|
---|
| 1106 | DISCPUSTATE cpu;
|
---|
| 1107 |
|
---|
[41732] | 1108 | if (cbInstrs + cbCurInstr >= SIZEOF_NEARJUMP32)
|
---|
[1] | 1109 | break;
|
---|
| 1110 |
|
---|
[41732] | 1111 | if (csamIsCodeScanned(pVM, pCurInstrGC + cbCurInstr, &pPage) == true)
|
---|
[1] | 1112 | {
|
---|
[42782] | 1113 | /* We've scanned the next instruction(s) already. This means we've
|
---|
| 1114 | followed a branch that ended up there before -> dangerous!! */
|
---|
[41732] | 1115 | PATMR3DetectConflict(pVM, pCurInstrGC, pCurInstrGC + cbCurInstr);
|
---|
[1] | 1116 | break;
|
---|
| 1117 | }
|
---|
[41732] | 1118 | pCurInstrGC += cbCurInstr;
|
---|
| 1119 | cbInstrs += cbCurInstr;
|
---|
[1] | 1120 |
|
---|
[30500] | 1121 | { /* Force pCurInstrHC out of scope after we stop using it (page lock!) */
|
---|
[44362] | 1122 | uint8_t *pCurInstrHC = csamR3GCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
[30497] | 1123 | if (pCurInstrHC == NULL)
|
---|
| 1124 | {
|
---|
[44362] | 1125 | Log(("csamR3GCVirtToHCVirt failed for %RRv\n", pCurInstrGC));
|
---|
[30497] | 1126 | break;
|
---|
| 1127 | }
|
---|
| 1128 | Assert(VALID_PTR(pCurInstrHC));
|
---|
| 1129 |
|
---|
[41769] | 1130 | rc = csamR3DISInstr(pVM, pCurInstrGC, pCurInstrHC, (fCode32) ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
|
---|
[41732] | 1131 | &cpu, &cbCurInstr, NULL, 0);
|
---|
[1] | 1132 | }
|
---|
[25226] | 1133 | AssertRC(rc);
|
---|
[13816] | 1134 | if (RT_FAILURE(rc))
|
---|
[1] | 1135 | break;
|
---|
| 1136 | }
|
---|
| 1137 | break;
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
[45276] | 1140 | #ifdef VBOX_WITH_RAW_RING1
|
---|
| 1141 | case OP_MOV:
|
---|
| 1142 | /* mov xx, CS is a dangerous instruction as our raw ring usage leaks through. */
|
---|
| 1143 | if ( EMIsRawRing1Enabled(pVM)
|
---|
[48936] | 1144 | && (pCpu->Param2.fUse & DISUSE_REG_SEG)
|
---|
[45276] | 1145 | && (pCpu->Param2.Base.idxSegReg == DISSELREG_CS))
|
---|
| 1146 | {
|
---|
| 1147 | Log(("CSAM: Patching dangerous 'mov xx, cs' instruction at %RGv with an int3\n", pCurInstrGC));
|
---|
| 1148 | if (PATMR3HasBeenPatched(pVM, pCurInstrGC) == false)
|
---|
| 1149 | {
|
---|
| 1150 | rc = PATMR3InstallPatch(pVM, pCurInstrGC, (pPage->fCode32) ? PATMFL_CODE32 : 0);
|
---|
| 1151 | if (RT_FAILURE(rc))
|
---|
| 1152 | {
|
---|
| 1153 | Log(("PATMR3InstallPatch failed with %d\n", rc));
|
---|
| 1154 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1155 | }
|
---|
| 1156 | }
|
---|
| 1157 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1158 | }
|
---|
| 1159 | break;
|
---|
| 1160 | #endif
|
---|
| 1161 |
|
---|
[1] | 1162 | case OP_PUSH:
|
---|
[45276] | 1163 | /** @todo broken comparison!! should be if ((pCpu->Param1.fUse & DISUSE_REG_SEG) && (pCpu->Param1.Base.idxSegReg == DISSELREG_SS)) */
|
---|
[41738] | 1164 | if (pCpu->pCurInstr->fParam1 != OP_PARM_REG_CS)
|
---|
[1] | 1165 | break;
|
---|
| 1166 |
|
---|
[45276] | 1167 | #ifndef VBOX_WITH_SAFE_STR
|
---|
[69046] | 1168 | RT_FALL_THRU();
|
---|
[1] | 1169 | case OP_STR:
|
---|
[45276] | 1170 | #endif
|
---|
[69046] | 1171 | RT_FALL_THRU();
|
---|
[1] | 1172 | case OP_LSL:
|
---|
| 1173 | case OP_LAR:
|
---|
| 1174 | case OP_SGDT:
|
---|
| 1175 | case OP_SLDT:
|
---|
| 1176 | case OP_SIDT:
|
---|
| 1177 | case OP_SMSW:
|
---|
| 1178 | case OP_VERW:
|
---|
| 1179 | case OP_VERR:
|
---|
| 1180 | case OP_CPUID:
|
---|
| 1181 | case OP_IRET:
|
---|
| 1182 | #ifdef DEBUG
|
---|
[41737] | 1183 | switch(pCpu->pCurInstr->uOpcode)
|
---|
[1] | 1184 | {
|
---|
| 1185 | case OP_STR:
|
---|
[13822] | 1186 | Log(("Privileged instruction at %RRv: str!!\n", pCurInstrGC));
|
---|
[1] | 1187 | break;
|
---|
| 1188 | case OP_LSL:
|
---|
[13822] | 1189 | Log(("Privileged instruction at %RRv: lsl!!\n", pCurInstrGC));
|
---|
[1] | 1190 | break;
|
---|
| 1191 | case OP_LAR:
|
---|
[13822] | 1192 | Log(("Privileged instruction at %RRv: lar!!\n", pCurInstrGC));
|
---|
[1] | 1193 | break;
|
---|
| 1194 | case OP_SGDT:
|
---|
[13822] | 1195 | Log(("Privileged instruction at %RRv: sgdt!!\n", pCurInstrGC));
|
---|
[1] | 1196 | break;
|
---|
| 1197 | case OP_SLDT:
|
---|
[13822] | 1198 | Log(("Privileged instruction at %RRv: sldt!!\n", pCurInstrGC));
|
---|
[1] | 1199 | break;
|
---|
| 1200 | case OP_SIDT:
|
---|
[13822] | 1201 | Log(("Privileged instruction at %RRv: sidt!!\n", pCurInstrGC));
|
---|
[1] | 1202 | break;
|
---|
| 1203 | case OP_SMSW:
|
---|
[13822] | 1204 | Log(("Privileged instruction at %RRv: smsw!!\n", pCurInstrGC));
|
---|
[1] | 1205 | break;
|
---|
| 1206 | case OP_VERW:
|
---|
[13822] | 1207 | Log(("Privileged instruction at %RRv: verw!!\n", pCurInstrGC));
|
---|
[1] | 1208 | break;
|
---|
| 1209 | case OP_VERR:
|
---|
[13822] | 1210 | Log(("Privileged instruction at %RRv: verr!!\n", pCurInstrGC));
|
---|
[1] | 1211 | break;
|
---|
| 1212 | case OP_CPUID:
|
---|
[13822] | 1213 | Log(("Privileged instruction at %RRv: cpuid!!\n", pCurInstrGC));
|
---|
[1] | 1214 | break;
|
---|
| 1215 | case OP_PUSH:
|
---|
[13822] | 1216 | Log(("Privileged instruction at %RRv: push cs!!\n", pCurInstrGC));
|
---|
[1] | 1217 | break;
|
---|
| 1218 | case OP_IRET:
|
---|
[13822] | 1219 | Log(("Privileged instruction at %RRv: iret!!\n", pCurInstrGC));
|
---|
[1] | 1220 | break;
|
---|
| 1221 | }
|
---|
| 1222 | #endif
|
---|
| 1223 |
|
---|
| 1224 | if (PATMR3HasBeenPatched(pVM, pCurInstrGC) == false)
|
---|
| 1225 | {
|
---|
| 1226 | rc = PATMR3InstallPatch(pVM, pCurInstrGC, (pPage->fCode32) ? PATMFL_CODE32 : 0);
|
---|
[13816] | 1227 | if (RT_FAILURE(rc))
|
---|
[1] | 1228 | {
|
---|
| 1229 | Log(("PATMR3InstallPatch failed with %d\n", rc));
|
---|
| 1230 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1231 | }
|
---|
| 1232 | }
|
---|
[41737] | 1233 | if (pCpu->pCurInstr->uOpcode == OP_IRET)
|
---|
[1] | 1234 | return VINF_SUCCESS; /* Look no further in this branch. */
|
---|
| 1235 |
|
---|
| 1236 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1237 |
|
---|
| 1238 | case OP_JMP:
|
---|
| 1239 | case OP_CALL:
|
---|
| 1240 | {
|
---|
| 1241 | // return or jump/call through a jump table
|
---|
[41738] | 1242 | if (OP_PARM_VTYPE(pCpu->pCurInstr->fParam1) != OP_PARM_J)
|
---|
[1] | 1243 | {
|
---|
| 1244 | #ifdef DEBUG
|
---|
[41737] | 1245 | switch(pCpu->pCurInstr->uOpcode)
|
---|
[1] | 1246 | {
|
---|
| 1247 | case OP_JMP:
|
---|
[13822] | 1248 | Log(("Control Flow instruction at %RRv: jmp!!\n", pCurInstrGC));
|
---|
[1] | 1249 | break;
|
---|
| 1250 | case OP_CALL:
|
---|
[13822] | 1251 | Log(("Control Flow instruction at %RRv: call!!\n", pCurInstrGC));
|
---|
[1] | 1252 | break;
|
---|
| 1253 | }
|
---|
| 1254 | #endif
|
---|
| 1255 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1256 | }
|
---|
| 1257 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
| 1260 | }
|
---|
| 1261 |
|
---|
| 1262 | return VWRN_CONTINUE_ANALYSIS;
|
---|
| 1263 | }
|
---|
| 1264 |
|
---|
| 1265 | #ifdef CSAM_ANALYSE_BEYOND_RET
|
---|
| 1266 | /**
|
---|
| 1267 | * Wrapper for csamAnalyseCodeStream for call instructions.
|
---|
| 1268 | *
|
---|
| 1269 | * @returns VBox status code.
|
---|
[58122] | 1270 | * @param pVM The cross context VM structure.
|
---|
[1] | 1271 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
| 1272 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
| 1273 | * @param fCode32 16 or 32 bits code
|
---|
| 1274 | * @param pfnCSAMR3Analyse Callback for testing the disassembled instruction
|
---|
| 1275 | * @param pUserData User pointer (callback specific)
|
---|
| 1276 | *
|
---|
| 1277 | */
|
---|
[9212] | 1278 | static int csamAnalyseCallCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
[1] | 1279 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec)
|
---|
| 1280 | {
|
---|
| 1281 | int rc;
|
---|
| 1282 | CSAMCALLEXITREC CallExitRec;
|
---|
| 1283 | PCSAMCALLEXITREC pOldCallRec;
|
---|
| 1284 | PCSAMPAGE pPage = 0;
|
---|
| 1285 | uint32_t i;
|
---|
| 1286 |
|
---|
| 1287 | CallExitRec.cInstrAfterRet = 0;
|
---|
| 1288 |
|
---|
| 1289 | pOldCallRec = pCacheRec->pCallExitRec;
|
---|
| 1290 | pCacheRec->pCallExitRec = &CallExitRec;
|
---|
| 1291 |
|
---|
| 1292 | rc = csamAnalyseCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
| 1293 |
|
---|
| 1294 | for (i=0;i<CallExitRec.cInstrAfterRet;i++)
|
---|
| 1295 | {
|
---|
| 1296 | PCSAMPAGE pPage = 0;
|
---|
| 1297 |
|
---|
| 1298 | pCurInstrGC = CallExitRec.pInstrAfterRetGC[i];
|
---|
| 1299 |
|
---|
| 1300 | /* Check if we've previously encountered the instruction after the ret. */
|
---|
| 1301 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
| 1302 | {
|
---|
| 1303 | DISCPUSTATE cpu;
|
---|
[41732] | 1304 | uint32_t cbInstr;
|
---|
[4953] | 1305 | int rc2;
|
---|
[1] | 1306 | #ifdef DEBUG
|
---|
| 1307 | char szOutput[256];
|
---|
| 1308 | #endif
|
---|
| 1309 | if (pPage == NULL)
|
---|
| 1310 | {
|
---|
| 1311 | /* New address; let's take a look at it. */
|
---|
[57008] | 1312 | pPage = csamR3CreatePageRecord(pVM, pCurInstrGC, CSAM_TAG_CSAM, fCode32);
|
---|
[1] | 1313 | if (pPage == NULL)
|
---|
| 1314 | {
|
---|
| 1315 | rc = VERR_NO_MEMORY;
|
---|
| 1316 | goto done;
|
---|
| 1317 | }
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | /**
|
---|
| 1321 | * Some generic requirements for recognizing an adjacent function:
|
---|
| 1322 | * - alignment fillers that consist of:
|
---|
| 1323 | * - nop
|
---|
| 1324 | * - lea genregX, [genregX (+ 0)]
|
---|
| 1325 | * - push ebp after the filler (can extend this later); aligned at at least a 4 byte boundary
|
---|
| 1326 | */
|
---|
[41658] | 1327 | for (int j = 0; j < 16; j++)
|
---|
[1] | 1328 | {
|
---|
[44362] | 1329 | uint8_t *pCurInstrHC = csamR3GCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
[1] | 1330 | if (pCurInstrHC == NULL)
|
---|
| 1331 | {
|
---|
[44362] | 1332 | Log(("csamR3GCVirtToHCVirt failed for %RRv\n", pCurInstrGC));
|
---|
[1] | 1333 | goto done;
|
---|
| 1334 | }
|
---|
| 1335 | Assert(VALID_PTR(pCurInstrHC));
|
---|
| 1336 |
|
---|
| 1337 | STAM_PROFILE_START(&pVM->csam.s.StatTimeDisasm, a);
|
---|
| 1338 | #ifdef DEBUG
|
---|
[41769] | 1339 | rc2 = csamR3DISInstr(pVM, pCurInstrGC, pCurInstrHC, (fCode32) ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
|
---|
[41732] | 1340 | &cpu, &cbInstr, szOutput, sizeof(szOutput));
|
---|
[13816] | 1341 | if (RT_SUCCESS(rc2)) Log(("CSAM Call Analysis: %s", szOutput));
|
---|
[1] | 1342 | #else
|
---|
[41769] | 1343 | rc2 = csamR3DISInstr(pVM, pCurInstrGC, pCurInstrHC, (fCode32) ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
|
---|
[41732] | 1344 | &cpu, &cbInstr, NULL, 0);
|
---|
[1] | 1345 | #endif
|
---|
| 1346 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeDisasm, a);
|
---|
[13816] | 1347 | if (RT_FAILURE(rc2))
|
---|
[1] | 1348 | {
|
---|
[13822] | 1349 | Log(("Disassembly failed at %RRv with %Rrc (probably page not present) -> return to caller\n", pCurInstrGC, rc2));
|
---|
[1] | 1350 | goto done;
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
[41732] | 1353 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrBytesRead, cbInstr);
|
---|
[1] | 1354 |
|
---|
[9212] | 1355 | RCPTRTYPE(uint8_t *) addr = 0;
|
---|
[1] | 1356 | PCSAMPAGE pJmpPage = NULL;
|
---|
| 1357 |
|
---|
[41732] | 1358 | if (PAGE_ADDRESS(pCurInstrGC) != PAGE_ADDRESS(pCurInstrGC + cbInstr - 1))
|
---|
[1] | 1359 | {
|
---|
[41732] | 1360 | if (!PGMGstIsPagePresent(pVM, pCurInstrGC + cbInstr - 1))
|
---|
[1] | 1361 | {
|
---|
| 1362 | /// @todo fault in the page
|
---|
[13822] | 1363 | Log(("Page for current instruction %RRv is not present!!\n", pCurInstrGC));
|
---|
[1] | 1364 | goto done;
|
---|
| 1365 | }
|
---|
| 1366 | //all is fine, let's continue
|
---|
[41732] | 1367 | csamR3CheckPageRecord(pVM, pCurInstrGC + cbInstr - 1);
|
---|
[1] | 1368 | }
|
---|
| 1369 |
|
---|
[41737] | 1370 | switch (cpu.pCurInstr->uOpcode)
|
---|
[1] | 1371 | {
|
---|
| 1372 | case OP_NOP:
|
---|
[1859] | 1373 | case OP_INT3:
|
---|
[1] | 1374 | break; /* acceptable */
|
---|
| 1375 |
|
---|
| 1376 | case OP_LEA:
|
---|
| 1377 | /* Must be similar to:
|
---|
| 1378 | *
|
---|
| 1379 | * lea esi, [esi]
|
---|
| 1380 | * lea esi, [esi+0]
|
---|
[23] | 1381 | * Any register is allowed as long as source and destination are identical.
|
---|
[1] | 1382 | */
|
---|
[41739] | 1383 | if ( cpu.Param1.fUse != DISUSE_REG_GEN32
|
---|
| 1384 | || ( cpu.Param2.flags != DISUSE_REG_GEN32
|
---|
| 1385 | && ( !(cpu.Param2.flags & DISUSE_REG_GEN32)
|
---|
| 1386 | || !(cpu.Param2.flags & (DISUSE_DISPLACEMENT8|DISUSE_DISPLACEMENT16|DISUSE_DISPLACEMENT32))
|
---|
[41741] | 1387 | || cpu.Param2.uValue != 0
|
---|
[1] | 1388 | )
|
---|
| 1389 | )
|
---|
[41739] | 1390 | || cpu.Param1.base.reg_gen32 != cpu.Param2.base.reg_gen32
|
---|
[1] | 1391 | )
|
---|
| 1392 | {
|
---|
| 1393 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
| 1394 | goto next_function;
|
---|
| 1395 | }
|
---|
| 1396 | break;
|
---|
| 1397 |
|
---|
| 1398 | case OP_PUSH:
|
---|
| 1399 | {
|
---|
| 1400 | if ( (pCurInstrGC & 0x3) != 0
|
---|
[41739] | 1401 | || cpu.Param1.fUse != DISUSE_REG_GEN32
|
---|
| 1402 | || cpu.Param1.base.reg_gen32 != USE_REG_EBP
|
---|
[1] | 1403 | )
|
---|
| 1404 | {
|
---|
| 1405 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
| 1406 | goto next_function;
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
| 1410 | {
|
---|
| 1411 | CSAMCALLEXITREC CallExitRec2;
|
---|
| 1412 | CallExitRec2.cInstrAfterRet = 0;
|
---|
| 1413 |
|
---|
| 1414 | pCacheRec->pCallExitRec = &CallExitRec2;
|
---|
| 1415 |
|
---|
| 1416 | /* Analyse the function. */
|
---|
[13822] | 1417 | Log(("Found new function at %RRv\n", pCurInstrGC));
|
---|
[1] | 1418 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunction);
|
---|
| 1419 | csamAnalyseCallCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
| 1420 | }
|
---|
| 1421 | goto next_function;
|
---|
| 1422 | }
|
---|
| 1423 |
|
---|
| 1424 | case OP_SUB:
|
---|
| 1425 | {
|
---|
| 1426 | if ( (pCurInstrGC & 0x3) != 0
|
---|
[41739] | 1427 | || cpu.Param1.fUse != DISUSE_REG_GEN32
|
---|
| 1428 | || cpu.Param1.base.reg_gen32 != USE_REG_ESP
|
---|
[1] | 1429 | )
|
---|
| 1430 | {
|
---|
| 1431 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
| 1432 | goto next_function;
|
---|
| 1433 | }
|
---|
| 1434 |
|
---|
| 1435 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
| 1436 | {
|
---|
| 1437 | CSAMCALLEXITREC CallExitRec2;
|
---|
| 1438 | CallExitRec2.cInstrAfterRet = 0;
|
---|
| 1439 |
|
---|
| 1440 | pCacheRec->pCallExitRec = &CallExitRec2;
|
---|
| 1441 |
|
---|
| 1442 | /* Analyse the function. */
|
---|
[13822] | 1443 | Log(("Found new function at %RRv\n", pCurInstrGC));
|
---|
[1] | 1444 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunction);
|
---|
| 1445 | csamAnalyseCallCodeStream(pVM, pInstrGC, pCurInstrGC, fCode32, pfnCSAMR3Analyse, pUserData, pCacheRec);
|
---|
| 1446 | }
|
---|
| 1447 | goto next_function;
|
---|
| 1448 | }
|
---|
| 1449 |
|
---|
| 1450 | default:
|
---|
| 1451 | STAM_COUNTER_INC(&pVM->csam.s.StatScanNextFunctionFailed);
|
---|
| 1452 | goto next_function;
|
---|
| 1453 | }
|
---|
| 1454 | /* Mark it as scanned. */
|
---|
[41732] | 1455 | csamMarkCode(pVM, pPage, pCurInstrGC, cbInstr, true);
|
---|
| 1456 | pCurInstrGC += cbInstr;
|
---|
[1] | 1457 | } /* for at most 16 instructions */
|
---|
| 1458 | next_function:
|
---|
| 1459 | ; /* MSVC complains otherwise */
|
---|
| 1460 | }
|
---|
| 1461 | }
|
---|
| 1462 | done:
|
---|
| 1463 | pCacheRec->pCallExitRec = pOldCallRec;
|
---|
| 1464 | return rc;
|
---|
| 1465 | }
|
---|
| 1466 | #else
|
---|
| 1467 | #define csamAnalyseCallCodeStream csamAnalyseCodeStream
|
---|
| 1468 | #endif
|
---|
| 1469 |
|
---|
| 1470 | /**
|
---|
| 1471 | * Disassembles the code stream until the callback function detects a failure or decides everything is acceptable
|
---|
| 1472 | *
|
---|
| 1473 | * @returns VBox status code.
|
---|
[58122] | 1474 | * @param pVM The cross context VM structure.
|
---|
[1] | 1475 | * @param pInstrGC Guest context pointer to privileged instruction
|
---|
| 1476 | * @param pCurInstrGC Guest context pointer to the current instruction
|
---|
| 1477 | * @param fCode32 16 or 32 bits code
|
---|
| 1478 | * @param pfnCSAMR3Analyse Callback for testing the disassembled instruction
|
---|
| 1479 | * @param pUserData User pointer (callback specific)
|
---|
[58116] | 1480 | * @param pCacheRec GC to HC cache record.
|
---|
[1] | 1481 | */
|
---|
[9212] | 1482 | static int csamAnalyseCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, bool fCode32,
|
---|
[1] | 1483 | PFN_CSAMR3ANALYSE pfnCSAMR3Analyse, void *pUserData, PCSAMP2GLOOKUPREC pCacheRec)
|
---|
| 1484 | {
|
---|
| 1485 | DISCPUSTATE cpu;
|
---|
| 1486 | PCSAMPAGE pPage = (PCSAMPAGE)pUserData;
|
---|
| 1487 | int rc = VWRN_CONTINUE_ANALYSIS;
|
---|
[41732] | 1488 | uint32_t cbInstr;
|
---|
[4953] | 1489 | int rc2;
|
---|
[22890] | 1490 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 1491 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 1492 |
|
---|
| 1493 | #ifdef DEBUG
|
---|
| 1494 | char szOutput[256];
|
---|
| 1495 | #endif
|
---|
| 1496 |
|
---|
[13822] | 1497 | LogFlow(("csamAnalyseCodeStream: code at %RRv depth=%d\n", pCurInstrGC, pCacheRec->depth));
|
---|
[1] | 1498 |
|
---|
| 1499 | pVM->csam.s.fScanningStarted = true;
|
---|
| 1500 |
|
---|
| 1501 | pCacheRec->depth++;
|
---|
| 1502 | /*
|
---|
| 1503 | * Limit the call depth. (rather arbitrary upper limit; too low and we won't detect certain
|
---|
| 1504 | * cpuid instructions in Linux kernels; too high and we waste too much time scanning code)
|
---|
| 1505 | * (512 is necessary to detect cpuid instructions in Red Hat EL4; see defect 1355)
|
---|
| 1506 | * @note we are using a lot of stack here. couple of 100k when we go to the full depth (!)
|
---|
| 1507 | */
|
---|
| 1508 | if (pCacheRec->depth > 512)
|
---|
| 1509 | {
|
---|
[13822] | 1510 | LogFlow(("CSAM: maximum calldepth reached for %RRv\n", pCurInstrGC));
|
---|
[1] | 1511 | pCacheRec->depth--;
|
---|
| 1512 | return VINF_SUCCESS; //let's not go on forever
|
---|
| 1513 | }
|
---|
| 1514 |
|
---|
| 1515 | Assert(!PATMIsPatchGCAddr(pVM, pCurInstrGC));
|
---|
| 1516 | csamR3CheckPageRecord(pVM, pCurInstrGC);
|
---|
| 1517 |
|
---|
| 1518 | while(rc == VWRN_CONTINUE_ANALYSIS)
|
---|
| 1519 | {
|
---|
| 1520 | if (csamIsCodeScanned(pVM, pCurInstrGC, &pPage) == false)
|
---|
| 1521 | {
|
---|
| 1522 | if (pPage == NULL)
|
---|
| 1523 | {
|
---|
| 1524 | /* New address; let's take a look at it. */
|
---|
[57008] | 1525 | pPage = csamR3CreatePageRecord(pVM, pCurInstrGC, CSAM_TAG_CSAM, fCode32);
|
---|
[1] | 1526 | if (pPage == NULL)
|
---|
| 1527 | {
|
---|
| 1528 | rc = VERR_NO_MEMORY;
|
---|
| 1529 | goto done;
|
---|
| 1530 | }
|
---|
| 1531 | }
|
---|
| 1532 | }
|
---|
| 1533 | else
|
---|
| 1534 | {
|
---|
[13822] | 1535 | LogFlow(("Code at %RRv has been scanned before\n", pCurInstrGC));
|
---|
[1] | 1536 | rc = VINF_SUCCESS;
|
---|
| 1537 | goto done;
|
---|
| 1538 | }
|
---|
| 1539 |
|
---|
[30500] | 1540 | { /* Force pCurInstrHC out of scope after we stop using it (page lock!) */
|
---|
[44362] | 1541 | uint8_t *pCurInstrHC = csamR3GCVirtToHCVirt(pVM, pCacheRec, pCurInstrGC);
|
---|
[30497] | 1542 | if (pCurInstrHC == NULL)
|
---|
| 1543 | {
|
---|
[44362] | 1544 | Log(("csamR3GCVirtToHCVirt failed for %RRv\n", pCurInstrGC));
|
---|
[30497] | 1545 | rc = VERR_PATCHING_REFUSED;
|
---|
| 1546 | goto done;
|
---|
| 1547 | }
|
---|
| 1548 | Assert(VALID_PTR(pCurInstrHC));
|
---|
[1] | 1549 |
|
---|
[30497] | 1550 | STAM_PROFILE_START(&pVM->csam.s.StatTimeDisasm, a);
|
---|
[1] | 1551 | #ifdef DEBUG
|
---|
[41769] | 1552 | rc2 = csamR3DISInstr(pVM, pCurInstrGC, pCurInstrHC, fCode32 ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
|
---|
[41732] | 1553 | &cpu, &cbInstr, szOutput, sizeof(szOutput));
|
---|
[30497] | 1554 | if (RT_SUCCESS(rc2)) Log(("CSAM Analysis: %s", szOutput));
|
---|
[1] | 1555 | #else
|
---|
[41769] | 1556 | rc2 = csamR3DISInstr(pVM, pCurInstrGC, pCurInstrHC, fCode32 ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
|
---|
[41732] | 1557 | &cpu, &cbInstr, NULL, 0);
|
---|
[1] | 1558 | #endif
|
---|
[30497] | 1559 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeDisasm, a);
|
---|
| 1560 | }
|
---|
[13816] | 1561 | if (RT_FAILURE(rc2))
|
---|
[1] | 1562 | {
|
---|
[13822] | 1563 | Log(("Disassembly failed at %RRv with %Rrc (probably page not present) -> return to caller\n", pCurInstrGC, rc2));
|
---|
[1] | 1564 | rc = VINF_SUCCESS;
|
---|
| 1565 | goto done;
|
---|
| 1566 | }
|
---|
| 1567 |
|
---|
[41732] | 1568 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrBytesRead, cbInstr);
|
---|
[1] | 1569 |
|
---|
[41732] | 1570 | csamMarkCode(pVM, pPage, pCurInstrGC, cbInstr, true);
|
---|
[1] | 1571 |
|
---|
[9212] | 1572 | RCPTRTYPE(uint8_t *) addr = 0;
|
---|
[1] | 1573 | PCSAMPAGE pJmpPage = NULL;
|
---|
| 1574 |
|
---|
[41732] | 1575 | if (PAGE_ADDRESS(pCurInstrGC) != PAGE_ADDRESS(pCurInstrGC + cbInstr - 1))
|
---|
[1] | 1576 | {
|
---|
[41732] | 1577 | if (!PGMGstIsPagePresent(pVCpu, pCurInstrGC + cbInstr - 1))
|
---|
[1] | 1578 | {
|
---|
| 1579 | /// @todo fault in the page
|
---|
[13822] | 1580 | Log(("Page for current instruction %RRv is not present!!\n", pCurInstrGC));
|
---|
[1] | 1581 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
| 1582 | goto next_please;
|
---|
| 1583 | }
|
---|
| 1584 | //all is fine, let's continue
|
---|
[41732] | 1585 | csamR3CheckPageRecord(pVM, pCurInstrGC + cbInstr - 1);
|
---|
[1] | 1586 | }
|
---|
| 1587 | /*
|
---|
| 1588 | * If it's harmless, then don't bother checking it (the disasm tables had better be accurate!)
|
---|
| 1589 | */
|
---|
[41738] | 1590 | if ((cpu.pCurInstr->fOpType & ~DISOPTYPE_RRM_MASK) == DISOPTYPE_HARMLESS)
|
---|
[1] | 1591 | {
|
---|
| 1592 | AssertMsg(pfnCSAMR3Analyse(pVM, &cpu, pInstrGC, pCurInstrGC, pCacheRec, (void *)pPage) == VWRN_CONTINUE_ANALYSIS, ("Instruction incorrectly marked harmless?!?!?\n"));
|
---|
| 1593 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
| 1594 | goto next_please;
|
---|
| 1595 | }
|
---|
| 1596 |
|
---|
| 1597 | #ifdef CSAM_ANALYSE_BEYOND_RET
|
---|
| 1598 | /* Remember the address of the instruction following the ret in case the parent instruction was a call. */
|
---|
[23] | 1599 | if ( pCacheRec->pCallExitRec
|
---|
[41737] | 1600 | && cpu.pCurInstr->uOpcode == OP_RETN
|
---|
[1] | 1601 | && pCacheRec->pCallExitRec->cInstrAfterRet < CSAM_MAX_CALLEXIT_RET)
|
---|
| 1602 | {
|
---|
[41732] | 1603 | pCacheRec->pCallExitRec->pInstrAfterRetGC[pCacheRec->pCallExitRec->cInstrAfterRet] = pCurInstrGC + cbInstr;
|
---|
[1] | 1604 | pCacheRec->pCallExitRec->cInstrAfterRet++;
|
---|
| 1605 | }
|
---|
| 1606 | #endif
|
---|
| 1607 |
|
---|
| 1608 | rc = pfnCSAMR3Analyse(pVM, &cpu, pInstrGC, pCurInstrGC, pCacheRec, (void *)pPage);
|
---|
| 1609 | if (rc == VINF_SUCCESS)
|
---|
| 1610 | goto done;
|
---|
| 1611 |
|
---|
| 1612 | // For our first attempt, we'll handle only simple relative jumps and calls (immediate offset coded in instruction)
|
---|
[41738] | 1613 | if ( ((cpu.pCurInstr->fOpType & DISOPTYPE_CONTROLFLOW) && (OP_PARM_VTYPE(cpu.pCurInstr->fParam1) == OP_PARM_J))
|
---|
[41739] | 1614 | || (cpu.pCurInstr->uOpcode == OP_CALL && cpu.Param1.fUse == DISUSE_DISPLACEMENT32)) /* simple indirect call (call dword ptr [address]) */
|
---|
[1] | 1615 | {
|
---|
[5205] | 1616 | /* We need to parse 'call dword ptr [address]' type of calls to catch cpuid instructions in some recent Linux distributions (e.g. OpenSuse 10.3) */
|
---|
[41737] | 1617 | if ( cpu.pCurInstr->uOpcode == OP_CALL
|
---|
[41739] | 1618 | && cpu.Param1.fUse == DISUSE_DISPLACEMENT32)
|
---|
[5205] | 1619 | {
|
---|
| 1620 | addr = 0;
|
---|
[41739] | 1621 | PGMPhysSimpleReadGCPtr(pVCpu, &addr, (RTRCUINTPTR)cpu.Param1.uDisp.i32, sizeof(addr));
|
---|
[5205] | 1622 | }
|
---|
| 1623 | else
|
---|
| 1624 | addr = CSAMResolveBranch(&cpu, pCurInstrGC);
|
---|
| 1625 |
|
---|
[1] | 1626 | if (addr == 0)
|
---|
| 1627 | {
|
---|
[41739] | 1628 | Log(("We don't support far jumps here!! (%08X)\n", cpu.Param1.fUse));
|
---|
[1] | 1629 | rc = VINF_SUCCESS;
|
---|
| 1630 | break;
|
---|
| 1631 | }
|
---|
| 1632 | Assert(!PATMIsPatchGCAddr(pVM, addr));
|
---|
| 1633 |
|
---|
| 1634 | /* If the target address lies in a patch generated jump, then special action needs to be taken. */
|
---|
| 1635 | PATMR3DetectConflict(pVM, pCurInstrGC, addr);
|
---|
| 1636 |
|
---|
| 1637 | /* Same page? */
|
---|
| 1638 | if (PAGE_ADDRESS(addr) != PAGE_ADDRESS(pCurInstrGC ))
|
---|
| 1639 | {
|
---|
[18988] | 1640 | if (!PGMGstIsPagePresent(pVCpu, addr))
|
---|
[1] | 1641 | {
|
---|
[13822] | 1642 | Log(("Page for current instruction %RRv is not present!!\n", addr));
|
---|
[1] | 1643 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
| 1644 | goto next_please;
|
---|
| 1645 | }
|
---|
| 1646 |
|
---|
| 1647 | /* All is fine, let's continue. */
|
---|
| 1648 | csamR3CheckPageRecord(pVM, addr);
|
---|
| 1649 | }
|
---|
| 1650 |
|
---|
| 1651 | pJmpPage = NULL;
|
---|
| 1652 | if (csamIsCodeScanned(pVM, addr, &pJmpPage) == false)
|
---|
| 1653 | {
|
---|
| 1654 | if (pJmpPage == NULL)
|
---|
| 1655 | {
|
---|
| 1656 | /* New branch target; let's take a look at it. */
|
---|
[57008] | 1657 | pJmpPage = csamR3CreatePageRecord(pVM, addr, CSAM_TAG_CSAM, fCode32);
|
---|
[1] | 1658 | if (pJmpPage == NULL)
|
---|
| 1659 | {
|
---|
| 1660 | rc = VERR_NO_MEMORY;
|
---|
| 1661 | goto done;
|
---|
| 1662 | }
|
---|
| 1663 | Assert(pPage);
|
---|
| 1664 | }
|
---|
[41737] | 1665 | if (cpu.pCurInstr->uOpcode == OP_CALL)
|
---|
[1] | 1666 | rc = csamAnalyseCallCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
| 1667 | else
|
---|
| 1668 | rc = csamAnalyseCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
| 1669 |
|
---|
| 1670 | if (rc != VINF_SUCCESS) {
|
---|
| 1671 | goto done;
|
---|
| 1672 | }
|
---|
| 1673 | }
|
---|
[41737] | 1674 | if (cpu.pCurInstr->uOpcode == OP_JMP)
|
---|
[1] | 1675 | {//unconditional jump; return to caller
|
---|
| 1676 | rc = VINF_SUCCESS;
|
---|
| 1677 | goto done;
|
---|
| 1678 | }
|
---|
| 1679 |
|
---|
| 1680 | rc = VWRN_CONTINUE_ANALYSIS;
|
---|
[41738] | 1681 | } //if ((cpu.pCurInstr->fOpType & DISOPTYPE_CONTROLFLOW) && (OP_PARM_VTYPE(cpu.pCurInstr->fParam1) == OP_PARM_J))
|
---|
[1] | 1682 | #ifdef CSAM_SCAN_JUMP_TABLE
|
---|
| 1683 | else
|
---|
[41737] | 1684 | if ( cpu.pCurInstr->uOpcode == OP_JMP
|
---|
[41739] | 1685 | && (cpu.Param1.fUse & (DISUSE_DISPLACEMENT32|DISUSE_INDEX|DISUSE_SCALE)) == (DISUSE_DISPLACEMENT32|DISUSE_INDEX|DISUSE_SCALE)
|
---|
[1] | 1686 | )
|
---|
| 1687 | {
|
---|
[41739] | 1688 | RTRCPTR pJumpTableGC = (RTRCPTR)cpu.Param1.disp32;
|
---|
[1] | 1689 | uint8_t *pJumpTableHC;
|
---|
| 1690 | int rc2;
|
---|
| 1691 |
|
---|
| 1692 | Log(("Jump through jump table\n"));
|
---|
| 1693 |
|
---|
[30497] | 1694 | rc2 = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, pJumpTableGC, (PRTHCPTR)&pJumpTableHC, missing page lock);
|
---|
[1] | 1695 | if (rc2 == VINF_SUCCESS)
|
---|
| 1696 | {
|
---|
| 1697 | for (uint32_t i=0;i<2;i++)
|
---|
| 1698 | {
|
---|
| 1699 | uint64_t fFlags;
|
---|
| 1700 |
|
---|
[41739] | 1701 | addr = pJumpTableGC + cpu.Param1.scale * i;
|
---|
[1] | 1702 | /* Same page? */
|
---|
| 1703 | if (PAGE_ADDRESS(addr) != PAGE_ADDRESS(pJumpTableGC))
|
---|
| 1704 | break;
|
---|
[23] | 1705 |
|
---|
[41739] | 1706 | addr = *(RTRCPTR *)(pJumpTableHC + cpu.Param1.scale * i);
|
---|
[1] | 1707 |
|
---|
[18988] | 1708 | rc2 = PGMGstGetPage(pVCpu, addr, &fFlags, NULL);
|
---|
[23] | 1709 | if ( rc2 != VINF_SUCCESS
|
---|
[1] | 1710 | || (fFlags & X86_PTE_US)
|
---|
| 1711 | || !(fFlags & X86_PTE_P)
|
---|
| 1712 | )
|
---|
| 1713 | break;
|
---|
| 1714 |
|
---|
[13822] | 1715 | Log(("Jump to %RRv\n", addr));
|
---|
[1] | 1716 |
|
---|
| 1717 | pJmpPage = NULL;
|
---|
| 1718 | if (csamIsCodeScanned(pVM, addr, &pJmpPage) == false)
|
---|
| 1719 | {
|
---|
| 1720 | if (pJmpPage == NULL)
|
---|
| 1721 | {
|
---|
| 1722 | /* New branch target; let's take a look at it. */
|
---|
[57008] | 1723 | pJmpPage = csamR3CreatePageRecord(pVM, addr, CSAM_TAG_CSAM, fCode32);
|
---|
[1] | 1724 | if (pJmpPage == NULL)
|
---|
| 1725 | {
|
---|
| 1726 | rc = VERR_NO_MEMORY;
|
---|
| 1727 | goto done;
|
---|
| 1728 | }
|
---|
| 1729 | Assert(pPage);
|
---|
| 1730 | }
|
---|
| 1731 | rc = csamAnalyseCodeStream(pVM, pInstrGC, addr, fCode32, pfnCSAMR3Analyse, (void *)pJmpPage, pCacheRec);
|
---|
| 1732 | if (rc != VINF_SUCCESS) {
|
---|
| 1733 | goto done;
|
---|
| 1734 | }
|
---|
| 1735 | }
|
---|
| 1736 | }
|
---|
| 1737 | }
|
---|
| 1738 | }
|
---|
| 1739 | #endif
|
---|
| 1740 | if (rc != VWRN_CONTINUE_ANALYSIS) {
|
---|
| 1741 | break; //done!
|
---|
| 1742 | }
|
---|
| 1743 | next_please:
|
---|
[41737] | 1744 | if (cpu.pCurInstr->uOpcode == OP_JMP)
|
---|
[1] | 1745 | {
|
---|
| 1746 | rc = VINF_SUCCESS;
|
---|
| 1747 | goto done;
|
---|
| 1748 | }
|
---|
[41732] | 1749 | pCurInstrGC += cbInstr;
|
---|
[1] | 1750 | }
|
---|
| 1751 | done:
|
---|
| 1752 | pCacheRec->depth--;
|
---|
| 1753 | return rc;
|
---|
| 1754 | }
|
---|
| 1755 |
|
---|
| 1756 |
|
---|
| 1757 | /**
|
---|
| 1758 | * Calculates the 64 bits hash value for the current page
|
---|
| 1759 | *
|
---|
| 1760 | * @returns hash value
|
---|
[58122] | 1761 | * @param pVM The cross context VM structure.
|
---|
[1] | 1762 | * @param pInstr Page address
|
---|
| 1763 | */
|
---|
[9344] | 1764 | uint64_t csamR3CalcPageHash(PVM pVM, RTRCPTR pInstr)
|
---|
[1] | 1765 | {
|
---|
| 1766 | uint64_t hash = 0;
|
---|
| 1767 | uint32_t val[5];
|
---|
| 1768 | int rc;
|
---|
[22890] | 1769 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 1770 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 1771 |
|
---|
| 1772 | Assert((pInstr & PAGE_OFFSET_MASK) == 0);
|
---|
| 1773 |
|
---|
[18927] | 1774 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &val[0], pInstr, sizeof(val[0]));
|
---|
[61390] | 1775 | if (RT_SUCCESS(rc))
|
---|
| 1776 | { /* likely */ }
|
---|
| 1777 | else
|
---|
[1] | 1778 | {
|
---|
[61390] | 1779 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT || rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS)
|
---|
| 1780 | {
|
---|
[61391] | 1781 | Log(("csamR3CalcPageHash: page %RRv not present/invalid!!\n", pInstr));
|
---|
[61390] | 1782 | return ~0ULL;
|
---|
| 1783 | }
|
---|
[61391] | 1784 | AssertMsgFailed(("rc = %Rrc %RRv\n", rc, pInstr));
|
---|
[1] | 1785 | }
|
---|
| 1786 |
|
---|
[18927] | 1787 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &val[1], pInstr+1024, sizeof(val[0]));
|
---|
[13818] | 1788 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 1789 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
| 1790 | {
|
---|
[13822] | 1791 | Log(("csamR3CalcPageHash: page %RRv not present!!\n", pInstr));
|
---|
[1] | 1792 | return ~0ULL;
|
---|
| 1793 | }
|
---|
| 1794 |
|
---|
[18927] | 1795 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &val[2], pInstr+2048, sizeof(val[0]));
|
---|
[13818] | 1796 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 1797 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
| 1798 | {
|
---|
[13822] | 1799 | Log(("csamR3CalcPageHash: page %RRv not present!!\n", pInstr));
|
---|
[1] | 1800 | return ~0ULL;
|
---|
| 1801 | }
|
---|
| 1802 |
|
---|
[18927] | 1803 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &val[3], pInstr+3072, sizeof(val[0]));
|
---|
[13818] | 1804 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 1805 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
| 1806 | {
|
---|
[13822] | 1807 | Log(("csamR3CalcPageHash: page %RRv not present!!\n", pInstr));
|
---|
[1] | 1808 | return ~0ULL;
|
---|
| 1809 | }
|
---|
| 1810 |
|
---|
[18927] | 1811 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &val[4], pInstr+4092, sizeof(val[0]));
|
---|
[13818] | 1812 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 1813 | if (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
| 1814 | {
|
---|
[13822] | 1815 | Log(("csamR3CalcPageHash: page %RRv not present!!\n", pInstr));
|
---|
[1] | 1816 | return ~0ULL;
|
---|
| 1817 | }
|
---|
| 1818 |
|
---|
| 1819 | // don't want to get division by zero traps
|
---|
| 1820 | val[2] |= 1;
|
---|
| 1821 | val[4] |= 1;
|
---|
| 1822 |
|
---|
| 1823 | hash = (uint64_t)val[0] * (uint64_t)val[1] / (uint64_t)val[2] + (val[3]%val[4]);
|
---|
| 1824 | return (hash == ~0ULL) ? hash - 1 : hash;
|
---|
| 1825 | }
|
---|
| 1826 |
|
---|
| 1827 |
|
---|
| 1828 | /**
|
---|
| 1829 | * Notify CSAM of a page flush
|
---|
| 1830 | *
|
---|
| 1831 | * @returns VBox status code
|
---|
[58122] | 1832 | * @param pVM The cross context VM structure.
|
---|
[1] | 1833 | * @param addr GC address of the page to flush
|
---|
| 1834 | * @param fRemovePage Page removal flag
|
---|
| 1835 | */
|
---|
[9344] | 1836 | static int csamFlushPage(PVM pVM, RTRCPTR addr, bool fRemovePage)
|
---|
[1] | 1837 | {
|
---|
| 1838 | PCSAMPAGEREC pPageRec;
|
---|
| 1839 | int rc;
|
---|
| 1840 | RTGCPHYS GCPhys = 0;
|
---|
| 1841 | uint64_t fFlags = 0;
|
---|
[22890] | 1842 | Assert(pVM->cCpus == 1 || !CSAMIsEnabled(pVM));
|
---|
[1] | 1843 |
|
---|
| 1844 | if (!CSAMIsEnabled(pVM))
|
---|
| 1845 | return VINF_SUCCESS;
|
---|
[70948] | 1846 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[1] | 1847 |
|
---|
[19263] | 1848 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
| 1849 |
|
---|
[1] | 1850 | STAM_PROFILE_START(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1851 |
|
---|
| 1852 | addr = addr & PAGE_BASE_GC_MASK;
|
---|
| 1853 |
|
---|
| 1854 | /*
|
---|
| 1855 | * Note: searching for the page in our tree first is more expensive (skipped flushes are two orders of magnitude more common)
|
---|
| 1856 | */
|
---|
| 1857 | if (pVM->csam.s.pPageTree == NULL)
|
---|
| 1858 | {
|
---|
| 1859 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1860 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
| 1861 | }
|
---|
| 1862 |
|
---|
[18988] | 1863 | rc = PGMGstGetPage(pVCpu, addr, &fFlags, &GCPhys);
|
---|
[1] | 1864 | /* Returned at a very early stage (no paging yet presumably). */
|
---|
| 1865 | if (rc == VERR_NOT_SUPPORTED)
|
---|
| 1866 | {
|
---|
| 1867 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1868 | return rc;
|
---|
| 1869 | }
|
---|
| 1870 |
|
---|
[13816] | 1871 | if (RT_SUCCESS(rc))
|
---|
[1] | 1872 | {
|
---|
| 1873 | if ( (fFlags & X86_PTE_US)
|
---|
| 1874 | || rc == VERR_PGM_PHYS_PAGE_RESERVED
|
---|
| 1875 | )
|
---|
| 1876 | {
|
---|
| 1877 | /* User page -> not relevant for us. */
|
---|
| 1878 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
|
---|
| 1879 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1880 | return VINF_SUCCESS;
|
---|
| 1881 | }
|
---|
| 1882 | }
|
---|
| 1883 | else
|
---|
| 1884 | if (rc != VERR_PAGE_NOT_PRESENT && rc != VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
[13822] | 1885 | AssertMsgFailed(("PGMR3GetPage %RRv failed with %Rrc\n", addr, rc));
|
---|
[1] | 1886 |
|
---|
[36912] | 1887 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)addr);
|
---|
[1] | 1888 | if (pPageRec)
|
---|
| 1889 | {
|
---|
| 1890 | if ( GCPhys == pPageRec->page.GCPhys
|
---|
| 1891 | && (fFlags & X86_PTE_P))
|
---|
| 1892 | {
|
---|
| 1893 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushesSkipped, 1);
|
---|
| 1894 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1895 | return VINF_SUCCESS;
|
---|
| 1896 | }
|
---|
| 1897 |
|
---|
[13824] | 1898 | Log(("CSAMR3FlushPage: page %RRv has changed -> FLUSH (rc=%Rrc) (Phys: %RGp vs %RGp)\n", addr, rc, GCPhys, pPageRec->page.GCPhys));
|
---|
[1] | 1899 |
|
---|
| 1900 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrFlushes, 1);
|
---|
| 1901 |
|
---|
| 1902 | if (fRemovePage)
|
---|
| 1903 | csamRemovePageRecord(pVM, addr);
|
---|
| 1904 | else
|
---|
| 1905 | {
|
---|
| 1906 | CSAMMarkPage(pVM, addr, false);
|
---|
| 1907 | pPageRec->page.GCPhys = 0;
|
---|
| 1908 | pPageRec->page.fFlags = 0;
|
---|
[18988] | 1909 | rc = PGMGstGetPage(pVCpu, addr, &pPageRec->page.fFlags, &pPageRec->page.GCPhys);
|
---|
[1] | 1910 | if (rc == VINF_SUCCESS)
|
---|
| 1911 | pPageRec->page.u64Hash = csamR3CalcPageHash(pVM, addr);
|
---|
[760] | 1912 |
|
---|
| 1913 | if (pPageRec->page.pBitmap == NULL)
|
---|
| 1914 | {
|
---|
| 1915 | pPageRec->page.pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, CSAM_PAGE_BITMAP_SIZE);
|
---|
| 1916 | Assert(pPageRec->page.pBitmap);
|
---|
| 1917 | if (pPageRec->page.pBitmap == NULL)
|
---|
| 1918 | return VERR_NO_MEMORY;
|
---|
| 1919 | }
|
---|
| 1920 | else
|
---|
| 1921 | memset(pPageRec->page.pBitmap, 0, CSAM_PAGE_BITMAP_SIZE);
|
---|
[1] | 1922 | }
|
---|
| 1923 |
|
---|
| 1924 |
|
---|
| 1925 | /*
|
---|
| 1926 | * Inform patch manager about the flush; no need to repeat the above check twice.
|
---|
| 1927 | */
|
---|
| 1928 | PATMR3FlushPage(pVM, addr);
|
---|
| 1929 |
|
---|
| 1930 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1931 | return VINF_SUCCESS;
|
---|
| 1932 | }
|
---|
| 1933 | else
|
---|
| 1934 | {
|
---|
| 1935 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeFlushPage, a);
|
---|
| 1936 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
| 1937 | }
|
---|
| 1938 | }
|
---|
| 1939 |
|
---|
| 1940 | /**
|
---|
| 1941 | * Notify CSAM of a page flush
|
---|
| 1942 | *
|
---|
| 1943 | * @returns VBox status code
|
---|
[58122] | 1944 | * @param pVM The cross context VM structure.
|
---|
[1] | 1945 | * @param addr GC address of the page to flush
|
---|
| 1946 | */
|
---|
[44385] | 1947 | VMMR3_INT_DECL(int) CSAMR3FlushPage(PVM pVM, RTRCPTR addr)
|
---|
[1] | 1948 | {
|
---|
| 1949 | return csamFlushPage(pVM, addr, true /* remove page record */);
|
---|
| 1950 | }
|
---|
| 1951 |
|
---|
| 1952 | /**
|
---|
[1994] | 1953 | * Remove a CSAM monitored page. Use with care!
|
---|
| 1954 | *
|
---|
| 1955 | * @returns VBox status code
|
---|
[58122] | 1956 | * @param pVM The cross context VM structure.
|
---|
[1994] | 1957 | * @param addr GC address of the page to flush
|
---|
| 1958 | */
|
---|
[44385] | 1959 | VMMR3_INT_DECL(int) CSAMR3RemovePage(PVM pVM, RTRCPTR addr)
|
---|
[1994] | 1960 | {
|
---|
[1996] | 1961 | PCSAMPAGEREC pPageRec;
|
---|
| 1962 | int rc;
|
---|
| 1963 |
|
---|
[70948] | 1964 | AssertReturn(VM_IS_RAW_MODE_ENABLED(pVM), VERR_CSAM_HM_IPE);
|
---|
[45620] | 1965 |
|
---|
[1996] | 1966 | addr = addr & PAGE_BASE_GC_MASK;
|
---|
| 1967 |
|
---|
[36912] | 1968 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)addr);
|
---|
[1996] | 1969 | if (pPageRec)
|
---|
| 1970 | {
|
---|
| 1971 | rc = csamRemovePageRecord(pVM, addr);
|
---|
[13816] | 1972 | if (RT_SUCCESS(rc))
|
---|
[1996] | 1973 | PATMR3FlushPage(pVM, addr);
|
---|
| 1974 | return VINF_SUCCESS;
|
---|
| 1975 | }
|
---|
| 1976 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
[1994] | 1977 | }
|
---|
| 1978 |
|
---|
| 1979 | /**
|
---|
[1] | 1980 | * Check a page record in case a page has been changed
|
---|
| 1981 | *
|
---|
| 1982 | * @returns VBox status code. (trap handled or not)
|
---|
[58122] | 1983 | * @param pVM The cross context VM structure.
|
---|
[1] | 1984 | * @param pInstrGC GC instruction pointer
|
---|
| 1985 | */
|
---|
[9344] | 1986 | int csamR3CheckPageRecord(PVM pVM, RTRCPTR pInstrGC)
|
---|
[1] | 1987 | {
|
---|
| 1988 | PCSAMPAGEREC pPageRec;
|
---|
| 1989 | uint64_t u64hash;
|
---|
| 1990 |
|
---|
| 1991 | pInstrGC = pInstrGC & PAGE_BASE_GC_MASK;
|
---|
| 1992 |
|
---|
[36912] | 1993 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)pInstrGC);
|
---|
[1] | 1994 | if (pPageRec)
|
---|
| 1995 | {
|
---|
| 1996 | u64hash = csamR3CalcPageHash(pVM, pInstrGC);
|
---|
| 1997 | if (u64hash != pPageRec->page.u64Hash)
|
---|
[760] | 1998 | csamFlushPage(pVM, pInstrGC, false /* don't remove page record */);
|
---|
[1] | 1999 | }
|
---|
| 2000 | else
|
---|
| 2001 | return VWRN_CSAM_PAGE_NOT_FOUND;
|
---|
[760] | 2002 |
|
---|
[1] | 2003 | return VINF_SUCCESS;
|
---|
| 2004 | }
|
---|
| 2005 |
|
---|
| 2006 | /**
|
---|
| 2007 | * Returns monitor description based on CSAM tag
|
---|
| 2008 | *
|
---|
| 2009 | * @return description string
|
---|
| 2010 | * @param enmTag Owner tag
|
---|
| 2011 | */
|
---|
| 2012 | const char *csamGetMonitorDescription(CSAMTAG enmTag)
|
---|
| 2013 | {
|
---|
| 2014 | if (enmTag == CSAM_TAG_PATM)
|
---|
| 2015 | return "CSAM-PATM self-modifying code monitor handler";
|
---|
| 2016 | else
|
---|
| 2017 | if (enmTag == CSAM_TAG_REM)
|
---|
| 2018 | return "CSAM-REM self-modifying code monitor handler";
|
---|
| 2019 | Assert(enmTag == CSAM_TAG_CSAM);
|
---|
| 2020 | return "CSAM self-modifying code monitor handler";
|
---|
| 2021 | }
|
---|
| 2022 |
|
---|
| 2023 | /**
|
---|
| 2024 | * Adds page record to our lookup tree
|
---|
| 2025 | *
|
---|
| 2026 | * @returns CSAMPAGE ptr or NULL if failure
|
---|
[58122] | 2027 | * @param pVM The cross context VM structure.
|
---|
[1] | 2028 | * @param GCPtr Page address
|
---|
| 2029 | * @param enmTag Owner tag
|
---|
| 2030 | * @param fCode32 16 or 32 bits code
|
---|
| 2031 | * @param fMonitorInvalidation Monitor page invalidation flag
|
---|
| 2032 | */
|
---|
[57008] | 2033 | static PCSAMPAGE csamR3CreatePageRecord(PVM pVM, RTRCPTR GCPtr, CSAMTAG enmTag, bool fCode32, bool fMonitorInvalidation)
|
---|
[1] | 2034 | {
|
---|
| 2035 | PCSAMPAGEREC pPage;
|
---|
| 2036 | int rc;
|
---|
| 2037 | bool ret;
|
---|
[22890] | 2038 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 2039 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 2040 |
|
---|
[13822] | 2041 | Log(("New page record for %RRv\n", GCPtr & PAGE_BASE_GC_MASK));
|
---|
[1] | 2042 |
|
---|
| 2043 | pPage = (PCSAMPAGEREC)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, sizeof(CSAMPAGEREC));
|
---|
| 2044 | if (pPage == NULL)
|
---|
| 2045 | {
|
---|
[57008] | 2046 | AssertMsgFailed(("csamR3CreatePageRecord: Out of memory!!!!\n"));
|
---|
[1] | 2047 | return NULL;
|
---|
| 2048 | }
|
---|
| 2049 | /* Round down to page boundary. */
|
---|
| 2050 | GCPtr = (GCPtr & PAGE_BASE_GC_MASK);
|
---|
[36912] | 2051 | pPage->Core.Key = (AVLPVKEY)(uintptr_t)GCPtr;
|
---|
[1] | 2052 | pPage->page.pPageGC = GCPtr;
|
---|
| 2053 | pPage->page.fCode32 = fCode32;
|
---|
| 2054 | pPage->page.fMonitorInvalidation = fMonitorInvalidation;
|
---|
| 2055 | pPage->page.enmTag = enmTag;
|
---|
| 2056 | pPage->page.fMonitorActive = false;
|
---|
| 2057 | pPage->page.pBitmap = (uint8_t *)MMR3HeapAllocZ(pVM, MM_TAG_CSAM_PATCH, PAGE_SIZE/sizeof(uint8_t));
|
---|
[18988] | 2058 | rc = PGMGstGetPage(pVCpu, GCPtr, &pPage->page.fFlags, &pPage->page.GCPhys);
|
---|
[13818] | 2059 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 2060 |
|
---|
| 2061 | pPage->page.u64Hash = csamR3CalcPageHash(pVM, GCPtr);
|
---|
| 2062 | ret = RTAvlPVInsert(&pVM->csam.s.pPageTree, &pPage->Core);
|
---|
| 2063 | Assert(ret);
|
---|
| 2064 |
|
---|
| 2065 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
[55895] | 2066 | AssertRelease(!g_fInCsamR3CodePageInvalidate);
|
---|
[1] | 2067 |
|
---|
| 2068 | switch (enmTag)
|
---|
| 2069 | {
|
---|
| 2070 | case CSAM_TAG_PATM:
|
---|
| 2071 | case CSAM_TAG_REM:
|
---|
[55889] | 2072 | # ifdef CSAM_MONITOR_CSAM_CODE_PAGES
|
---|
[1] | 2073 | case CSAM_TAG_CSAM:
|
---|
[55889] | 2074 | # endif
|
---|
[1] | 2075 | {
|
---|
[55889] | 2076 | rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, fMonitorInvalidation
|
---|
| 2077 | ? pVM->csam.s.hCodePageWriteAndInvPgType : pVM->csam.s.hCodePageWriteType,
|
---|
| 2078 | GCPtr, GCPtr + (PAGE_SIZE - 1) /* inclusive! */,
|
---|
| 2079 | pPage, NIL_RTRCPTR, csamGetMonitorDescription(enmTag));
|
---|
| 2080 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT,
|
---|
| 2081 | ("PGMR3HandlerVirtualRegister %RRv failed with %Rrc\n", GCPtr, rc));
|
---|
[13816] | 2082 | if (RT_FAILURE(rc))
|
---|
[55889] | 2083 | Log(("PGMR3HandlerVirtualRegister for %RRv failed with %Rrc\n", GCPtr, rc));
|
---|
[1] | 2084 |
|
---|
| 2085 | /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
|
---|
| 2086 |
|
---|
| 2087 | /* Prefetch it in case it's not there yet. */
|
---|
[18992] | 2088 | rc = PGMPrefetchPage(pVCpu, GCPtr);
|
---|
[1] | 2089 | AssertRC(rc);
|
---|
| 2090 |
|
---|
[30326] | 2091 | rc = PGMShwMakePageReadonly(pVCpu, GCPtr, 0 /*fFlags*/);
|
---|
[1] | 2092 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
| 2093 |
|
---|
| 2094 | pPage->page.fMonitorActive = true;
|
---|
| 2095 | STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
|
---|
| 2096 | break;
|
---|
| 2097 | }
|
---|
| 2098 | default:
|
---|
| 2099 | break; /* to shut up GCC */
|
---|
| 2100 | }
|
---|
| 2101 |
|
---|
[57008] | 2102 | Log(("csamR3CreatePageRecord %RRv GCPhys=%RGp\n", GCPtr, pPage->page.GCPhys));
|
---|
[1] | 2103 |
|
---|
[55889] | 2104 | # ifdef VBOX_WITH_STATISTICS
|
---|
[1] | 2105 | switch (enmTag)
|
---|
| 2106 | {
|
---|
| 2107 | case CSAM_TAG_CSAM:
|
---|
| 2108 | STAM_COUNTER_INC(&pVM->csam.s.StatPageCSAM);
|
---|
| 2109 | break;
|
---|
| 2110 | case CSAM_TAG_PATM:
|
---|
| 2111 | STAM_COUNTER_INC(&pVM->csam.s.StatPagePATM);
|
---|
| 2112 | break;
|
---|
| 2113 | case CSAM_TAG_REM:
|
---|
| 2114 | STAM_COUNTER_INC(&pVM->csam.s.StatPageREM);
|
---|
| 2115 | break;
|
---|
| 2116 | default:
|
---|
| 2117 | break; /* to shut up GCC */
|
---|
| 2118 | }
|
---|
[55889] | 2119 | # endif
|
---|
[1] | 2120 |
|
---|
| 2121 | #endif
|
---|
| 2122 |
|
---|
| 2123 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPages);
|
---|
| 2124 | if (fMonitorInvalidation)
|
---|
| 2125 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
|
---|
| 2126 |
|
---|
| 2127 | return &pPage->page;
|
---|
| 2128 | }
|
---|
| 2129 |
|
---|
| 2130 | /**
|
---|
| 2131 | * Monitors a code page (if not already monitored)
|
---|
| 2132 | *
|
---|
| 2133 | * @returns VBox status code
|
---|
[58122] | 2134 | * @param pVM The cross context VM structure.
|
---|
[1] | 2135 | * @param pPageAddrGC The page to monitor
|
---|
| 2136 | * @param enmTag Monitor tag
|
---|
| 2137 | */
|
---|
[12989] | 2138 | VMMR3DECL(int) CSAMR3MonitorPage(PVM pVM, RTRCPTR pPageAddrGC, CSAMTAG enmTag)
|
---|
[1] | 2139 | {
|
---|
[55889] | 2140 | ;
|
---|
[1] | 2141 | int rc;
|
---|
| 2142 | bool fMonitorInvalidation;
|
---|
[22890] | 2143 | Assert(pVM->cCpus == 1);
|
---|
[45620] | 2144 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[70948] | 2145 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[1] | 2146 |
|
---|
| 2147 | /* Dirty pages must be handled before calling this function!. */
|
---|
| 2148 | Assert(!pVM->csam.s.cDirtyPages);
|
---|
| 2149 |
|
---|
| 2150 | if (pVM->csam.s.fScanningStarted == false)
|
---|
| 2151 | return VINF_SUCCESS; /* too early */
|
---|
| 2152 |
|
---|
| 2153 | pPageAddrGC &= PAGE_BASE_GC_MASK;
|
---|
| 2154 |
|
---|
[13822] | 2155 | Log(("CSAMR3MonitorPage %RRv %d\n", pPageAddrGC, enmTag));
|
---|
[1] | 2156 |
|
---|
| 2157 | /** @todo implicit assumption */
|
---|
| 2158 | fMonitorInvalidation = (enmTag == CSAM_TAG_PATM);
|
---|
| 2159 |
|
---|
[55889] | 2160 | PCSAMPAGEREC pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)pPageAddrGC);
|
---|
[1] | 2161 | if (pPageRec == NULL)
|
---|
| 2162 | {
|
---|
| 2163 | uint64_t fFlags;
|
---|
| 2164 |
|
---|
[18988] | 2165 | rc = PGMGstGetPage(pVCpu, pPageAddrGC, &fFlags, NULL);
|
---|
[13818] | 2166 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 2167 | if ( rc == VINF_SUCCESS
|
---|
| 2168 | && (fFlags & X86_PTE_US))
|
---|
| 2169 | {
|
---|
| 2170 | /* We don't care about user pages. */
|
---|
| 2171 | STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
|
---|
| 2172 | return VINF_SUCCESS;
|
---|
| 2173 | }
|
---|
| 2174 |
|
---|
[57008] | 2175 | csamR3CreatePageRecord(pVM, pPageAddrGC, enmTag, true /* 32 bits code */, fMonitorInvalidation);
|
---|
[1] | 2176 |
|
---|
[36912] | 2177 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)pPageAddrGC);
|
---|
[1] | 2178 | Assert(pPageRec);
|
---|
| 2179 | }
|
---|
| 2180 | /** @todo reference count */
|
---|
| 2181 |
|
---|
| 2182 | #ifdef CSAM_MONITOR_CSAM_CODE_PAGES
|
---|
| 2183 | Assert(pPageRec->page.fMonitorActive);
|
---|
| 2184 | #endif
|
---|
| 2185 |
|
---|
| 2186 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
| 2187 | if (!pPageRec->page.fMonitorActive)
|
---|
| 2188 | {
|
---|
[13822] | 2189 | Log(("CSAMR3MonitorPage: activate monitoring for %RRv\n", pPageAddrGC));
|
---|
[1] | 2190 |
|
---|
[55889] | 2191 | rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, fMonitorInvalidation
|
---|
| 2192 | ? pVM->csam.s.hCodePageWriteAndInvPgType : pVM->csam.s.hCodePageWriteType,
|
---|
| 2193 | pPageAddrGC, pPageAddrGC + (PAGE_SIZE - 1) /* inclusive! */,
|
---|
| 2194 | pPageRec, NIL_RTRCPTR /*pvUserRC*/, csamGetMonitorDescription(enmTag));
|
---|
| 2195 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT,
|
---|
| 2196 | ("PGMR3HandlerVirtualRegister %RRv failed with %Rrc\n", pPageAddrGC, rc));
|
---|
[13816] | 2197 | if (RT_FAILURE(rc))
|
---|
[55889] | 2198 | Log(("PGMR3HandlerVirtualRegister for %RRv failed with %Rrc\n", pPageAddrGC, rc));
|
---|
[1] | 2199 |
|
---|
| 2200 | /* Could fail, because it's already monitored. Don't treat that condition as fatal. */
|
---|
| 2201 |
|
---|
| 2202 | /* Prefetch it in case it's not there yet. */
|
---|
[18992] | 2203 | rc = PGMPrefetchPage(pVCpu, pPageAddrGC);
|
---|
[1] | 2204 | AssertRC(rc);
|
---|
| 2205 |
|
---|
[30326] | 2206 | rc = PGMShwMakePageReadonly(pVCpu, pPageAddrGC, 0 /*fFlags*/);
|
---|
[1] | 2207 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
| 2208 |
|
---|
| 2209 | STAM_COUNTER_INC(&pVM->csam.s.StatPageMonitor);
|
---|
| 2210 |
|
---|
| 2211 | pPageRec->page.fMonitorActive = true;
|
---|
| 2212 | pPageRec->page.fMonitorInvalidation = fMonitorInvalidation;
|
---|
| 2213 | }
|
---|
| 2214 | else
|
---|
| 2215 | if ( !pPageRec->page.fMonitorInvalidation
|
---|
| 2216 | && fMonitorInvalidation)
|
---|
| 2217 | {
|
---|
| 2218 | Assert(pPageRec->page.fMonitorActive);
|
---|
[55889] | 2219 | rc = PGMHandlerVirtualChangeType(pVM, pPageRec->page.pPageGC, pVM->csam.s.hCodePageWriteAndInvPgType);
|
---|
| 2220 | AssertRC(rc);
|
---|
[1] | 2221 | pPageRec->page.fMonitorInvalidation = true;
|
---|
| 2222 | STAM_COUNTER_INC(&pVM->csam.s.StatNrPagesInv);
|
---|
| 2223 |
|
---|
| 2224 | /* Prefetch it in case it's not there yet. */
|
---|
[18992] | 2225 | rc = PGMPrefetchPage(pVCpu, pPageAddrGC);
|
---|
[1] | 2226 | AssertRC(rc);
|
---|
| 2227 |
|
---|
| 2228 | /* Make sure it's readonly. Page invalidation may have modified the attributes. */
|
---|
[30326] | 2229 | rc = PGMShwMakePageReadonly(pVCpu, pPageAddrGC, 0 /*fFlags*/);
|
---|
[1] | 2230 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
| 2231 | }
|
---|
| 2232 |
|
---|
[27] | 2233 | #if 0 /* def VBOX_STRICT -> very annoying) */
|
---|
[1] | 2234 | if (pPageRec->page.fMonitorActive)
|
---|
| 2235 | {
|
---|
| 2236 | uint64_t fPageShw;
|
---|
| 2237 | RTHCPHYS GCPhys;
|
---|
[18988] | 2238 | rc = PGMShwGetPage(pVCpu, pPageAddrGC, &fPageShw, &GCPhys);
|
---|
[208] | 2239 | // AssertMsg( (rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
| 2240 | // || !(fPageShw & X86_PTE_RW)
|
---|
[13825] | 2241 | // || (pPageRec->page.GCPhys == 0), ("Shadow page flags for %RRv (%RHp) aren't readonly (%RX64)!!\n", pPageAddrGC, GCPhys, fPageShw));
|
---|
[1] | 2242 | }
|
---|
| 2243 | #endif
|
---|
| 2244 |
|
---|
| 2245 | if (pPageRec->page.GCPhys == 0)
|
---|
| 2246 | {
|
---|
| 2247 | /* Prefetch it in case it's not there yet. */
|
---|
[18992] | 2248 | rc = PGMPrefetchPage(pVCpu, pPageAddrGC);
|
---|
[1] | 2249 | AssertRC(rc);
|
---|
| 2250 | /* The page was changed behind our back. It won't be made read-only until the next SyncCR3, so force it here. */
|
---|
[30326] | 2251 | rc = PGMShwMakePageReadonly(pVCpu, pPageAddrGC, 0 /*fFlags*/);
|
---|
[1] | 2252 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
| 2253 | }
|
---|
| 2254 | #endif /* CSAM_MONITOR_CODE_PAGES */
|
---|
| 2255 | return VINF_SUCCESS;
|
---|
| 2256 | }
|
---|
| 2257 |
|
---|
| 2258 | /**
|
---|
[8217] | 2259 | * Unmonitors a code page
|
---|
| 2260 | *
|
---|
| 2261 | * @returns VBox status code
|
---|
[58122] | 2262 | * @param pVM The cross context VM structure.
|
---|
[8217] | 2263 | * @param pPageAddrGC The page to monitor
|
---|
| 2264 | * @param enmTag Monitor tag
|
---|
| 2265 | */
|
---|
[12989] | 2266 | VMMR3DECL(int) CSAMR3UnmonitorPage(PVM pVM, RTRCPTR pPageAddrGC, CSAMTAG enmTag)
|
---|
[8217] | 2267 | {
|
---|
[70948] | 2268 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[45620] | 2269 |
|
---|
[8217] | 2270 | pPageAddrGC &= PAGE_BASE_GC_MASK;
|
---|
| 2271 |
|
---|
[13822] | 2272 | Log(("CSAMR3UnmonitorPage %RRv %d\n", pPageAddrGC, enmTag));
|
---|
[8217] | 2273 |
|
---|
[62647] | 2274 | Assert(enmTag == CSAM_TAG_REM); RT_NOREF_PV(enmTag);
|
---|
[8217] | 2275 |
|
---|
| 2276 | #ifdef VBOX_STRICT
|
---|
| 2277 | PCSAMPAGEREC pPageRec;
|
---|
| 2278 |
|
---|
[36969] | 2279 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)pPageAddrGC);
|
---|
[8217] | 2280 | Assert(pPageRec && pPageRec->page.enmTag == enmTag);
|
---|
| 2281 | #endif
|
---|
| 2282 | return CSAMR3RemovePage(pVM, pPageAddrGC);
|
---|
| 2283 | }
|
---|
| 2284 |
|
---|
| 2285 | /**
|
---|
[1] | 2286 | * Removes a page record from our lookup tree
|
---|
| 2287 | *
|
---|
| 2288 | * @returns VBox status code
|
---|
[58122] | 2289 | * @param pVM The cross context VM structure.
|
---|
[1] | 2290 | * @param GCPtr Page address
|
---|
| 2291 | */
|
---|
[9344] | 2292 | static int csamRemovePageRecord(PVM pVM, RTRCPTR GCPtr)
|
---|
[1] | 2293 | {
|
---|
| 2294 | PCSAMPAGEREC pPageRec;
|
---|
[22890] | 2295 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 2296 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 2297 |
|
---|
[13822] | 2298 | Log(("csamRemovePageRecord %RRv\n", GCPtr));
|
---|
[36912] | 2299 | pPageRec = (PCSAMPAGEREC)RTAvlPVRemove(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)GCPtr);
|
---|
[1] | 2300 |
|
---|
| 2301 | if (pPageRec)
|
---|
| 2302 | {
|
---|
| 2303 | STAM_COUNTER_INC(&pVM->csam.s.StatNrRemovedPages);
|
---|
| 2304 |
|
---|
| 2305 | #ifdef CSAM_MONITOR_CODE_PAGES
|
---|
| 2306 | if (pPageRec->page.fMonitorActive)
|
---|
| 2307 | {
|
---|
[63560] | 2308 | /** @todo -> this is expensive (cr3 reload)!!!
|
---|
[1] | 2309 | * if this happens often, then reuse it instead!!!
|
---|
| 2310 | */
|
---|
[55895] | 2311 | Assert(!g_fInCsamR3CodePageInvalidate);
|
---|
[1] | 2312 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageMonitor);
|
---|
[55889] | 2313 | PGMHandlerVirtualDeregister(pVM, pVCpu, GCPtr, false /*fHypervisor*/);
|
---|
[1] | 2314 | }
|
---|
| 2315 | if (pPageRec->page.enmTag == CSAM_TAG_PATM)
|
---|
| 2316 | {
|
---|
| 2317 | /* Make sure the recompiler flushes its cache as this page is no longer monitored. */
|
---|
| 2318 | STAM_COUNTER_INC(&pVM->csam.s.StatPageRemoveREMFlush);
|
---|
[18927] | 2319 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
[1] | 2320 | }
|
---|
| 2321 | #endif
|
---|
| 2322 |
|
---|
| 2323 | #ifdef VBOX_WITH_STATISTICS
|
---|
| 2324 | switch (pPageRec->page.enmTag)
|
---|
| 2325 | {
|
---|
| 2326 | case CSAM_TAG_CSAM:
|
---|
| 2327 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageCSAM);
|
---|
| 2328 | break;
|
---|
| 2329 | case CSAM_TAG_PATM:
|
---|
| 2330 | STAM_COUNTER_DEC(&pVM->csam.s.StatPagePATM);
|
---|
| 2331 | break;
|
---|
| 2332 | case CSAM_TAG_REM:
|
---|
| 2333 | STAM_COUNTER_DEC(&pVM->csam.s.StatPageREM);
|
---|
| 2334 | break;
|
---|
| 2335 | default:
|
---|
| 2336 | break; /* to shut up GCC */
|
---|
| 2337 | }
|
---|
| 2338 | #endif
|
---|
| 2339 |
|
---|
| 2340 | if (pPageRec->page.pBitmap) MMR3HeapFree(pPageRec->page.pBitmap);
|
---|
| 2341 | MMR3HeapFree(pPageRec);
|
---|
| 2342 | }
|
---|
| 2343 | else
|
---|
| 2344 | AssertFailed();
|
---|
| 2345 |
|
---|
| 2346 | return VINF_SUCCESS;
|
---|
| 2347 | }
|
---|
| 2348 |
|
---|
[62647] | 2349 | #if 0 /* Unused */
|
---|
[1] | 2350 | /**
|
---|
| 2351 | * Callback for delayed writes from non-EMT threads
|
---|
| 2352 | *
|
---|
[58122] | 2353 | * @param pVM The cross context VM structure.
|
---|
[1] | 2354 | * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
|
---|
| 2355 | * @param cbBuf How much it's reading/writing.
|
---|
| 2356 | */
|
---|
[9344] | 2357 | static DECLCALLBACK(void) CSAMDelayedWriteHandler(PVM pVM, RTRCPTR GCPtr, size_t cbBuf)
|
---|
[1] | 2358 | {
|
---|
[14075] | 2359 | int rc = PATMR3PatchWrite(pVM, GCPtr, (uint32_t)cbBuf);
|
---|
[1] | 2360 | AssertRC(rc);
|
---|
| 2361 | }
|
---|
[62647] | 2362 | #endif
|
---|
[1] | 2363 |
|
---|
| 2364 | /**
|
---|
[25647] | 2365 | * \#PF Handler callback for invalidation of virtual access handler ranges.
|
---|
[1] | 2366 | *
|
---|
[58123] | 2367 | * @param pVM The cross context VM structure.
|
---|
| 2368 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
| 2369 | * @param GCPtr The virtual address the guest has changed.
|
---|
| 2370 | * @param pvUser Ignored.
|
---|
[56054] | 2371 | *
|
---|
| 2372 | * @remarks Not currently called by PGM. It was actually only called for a month
|
---|
| 2373 | * back in 2006...
|
---|
[1] | 2374 | */
|
---|
[55900] | 2375 | static DECLCALLBACK(int) csamR3CodePageInvalidate(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtr, void *pvUser)
|
---|
[1] | 2376 | {
|
---|
[62647] | 2377 | RT_NOREF2(pVCpu, pvUser);
|
---|
| 2378 |
|
---|
[55895] | 2379 | g_fInCsamR3CodePageInvalidate = true;
|
---|
| 2380 | LogFlow(("csamR3CodePageInvalidate %RGv\n", GCPtr));
|
---|
[1] | 2381 | /** @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. */
|
---|
| 2382 | csamFlushPage(pVM, GCPtr, false /* don't remove page! */);
|
---|
[55895] | 2383 | g_fInCsamR3CodePageInvalidate = false;
|
---|
[62647] | 2384 |
|
---|
[1] | 2385 | return VINF_SUCCESS;
|
---|
| 2386 | }
|
---|
| 2387 |
|
---|
| 2388 | /**
|
---|
| 2389 | * Check if the current instruction has already been checked before
|
---|
| 2390 | *
|
---|
| 2391 | * @returns VBox status code. (trap handled or not)
|
---|
[58122] | 2392 | * @param pVM The cross context VM structure.
|
---|
[1] | 2393 | * @param pInstr Instruction pointer
|
---|
| 2394 | * @param pPage CSAM patch structure pointer
|
---|
| 2395 | */
|
---|
[9344] | 2396 | bool csamIsCodeScanned(PVM pVM, RTRCPTR pInstr, PCSAMPAGE *pPage)
|
---|
[1] | 2397 | {
|
---|
| 2398 | PCSAMPAGEREC pPageRec;
|
---|
| 2399 | uint32_t offset;
|
---|
| 2400 |
|
---|
| 2401 | STAM_PROFILE_START(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
| 2402 |
|
---|
| 2403 | offset = pInstr & PAGE_OFFSET_MASK;
|
---|
| 2404 | pInstr = pInstr & PAGE_BASE_GC_MASK;
|
---|
| 2405 |
|
---|
| 2406 | Assert(pPage);
|
---|
| 2407 |
|
---|
| 2408 | if (*pPage && (*pPage)->pPageGC == pInstr)
|
---|
| 2409 | {
|
---|
| 2410 | if ((*pPage)->pBitmap == NULL || ASMBitTest((*pPage)->pBitmap, offset))
|
---|
| 2411 | {
|
---|
| 2412 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
|
---|
| 2413 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
| 2414 | return true;
|
---|
| 2415 | }
|
---|
| 2416 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
| 2417 | return false;
|
---|
| 2418 | }
|
---|
| 2419 |
|
---|
[36912] | 2420 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)pInstr);
|
---|
[1] | 2421 | if (pPageRec)
|
---|
| 2422 | {
|
---|
| 2423 | if (pPage) *pPage= &pPageRec->page;
|
---|
| 2424 | if (pPageRec->page.pBitmap == NULL || ASMBitTest(pPageRec->page.pBitmap, offset))
|
---|
| 2425 | {
|
---|
| 2426 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrKnownPagesHC, 1);
|
---|
| 2427 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
| 2428 | return true;
|
---|
| 2429 | }
|
---|
| 2430 | }
|
---|
| 2431 | else
|
---|
| 2432 | {
|
---|
| 2433 | if (pPage) *pPage = NULL;
|
---|
| 2434 | }
|
---|
| 2435 | STAM_PROFILE_STOP(&pVM->csam.s.StatTimeCheckAddr, a);
|
---|
| 2436 | return false;
|
---|
| 2437 | }
|
---|
| 2438 |
|
---|
| 2439 | /**
|
---|
| 2440 | * Mark an instruction in a page as scanned/not scanned
|
---|
| 2441 | *
|
---|
[58122] | 2442 | * @param pVM The cross context VM structure.
|
---|
[1] | 2443 | * @param pPage Patch structure pointer
|
---|
| 2444 | * @param pInstr Instruction pointer
|
---|
[41732] | 2445 | * @param cbInstr Instruction size
|
---|
[1] | 2446 | * @param fScanned Mark as scanned or not
|
---|
| 2447 | */
|
---|
[41732] | 2448 | static void csamMarkCode(PVM pVM, PCSAMPAGE pPage, RTRCPTR pInstr, uint32_t cbInstr, bool fScanned)
|
---|
[1] | 2449 | {
|
---|
[41732] | 2450 | LogFlow(("csamMarkCodeAsScanned %RRv cbInstr=%d\n", pInstr, cbInstr));
|
---|
[1] | 2451 | CSAMMarkPage(pVM, pInstr, fScanned);
|
---|
| 2452 |
|
---|
| 2453 | /** @todo should recreate empty bitmap if !fScanned */
|
---|
| 2454 | if (pPage->pBitmap == NULL)
|
---|
| 2455 | return;
|
---|
| 2456 |
|
---|
| 2457 | if (fScanned)
|
---|
| 2458 | {
|
---|
| 2459 | // retn instructions can be scanned more than once
|
---|
| 2460 | if (ASMBitTest(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK) == 0)
|
---|
| 2461 | {
|
---|
[41732] | 2462 | pPage->uSize += cbInstr;
|
---|
[1] | 2463 | STAM_COUNTER_ADD(&pVM->csam.s.StatNrInstr, 1);
|
---|
| 2464 | }
|
---|
| 2465 | if (pPage->uSize >= PAGE_SIZE)
|
---|
| 2466 | {
|
---|
[13822] | 2467 | Log(("Scanned full page (%RRv) -> free bitmap\n", pInstr & PAGE_BASE_GC_MASK));
|
---|
[1] | 2468 | MMR3HeapFree(pPage->pBitmap);
|
---|
| 2469 | pPage->pBitmap = NULL;
|
---|
| 2470 | }
|
---|
| 2471 | else
|
---|
| 2472 | ASMBitSet(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
|
---|
| 2473 | }
|
---|
| 2474 | else
|
---|
| 2475 | ASMBitClear(pPage->pBitmap, pInstr & PAGE_OFFSET_MASK);
|
---|
| 2476 | }
|
---|
| 2477 |
|
---|
| 2478 | /**
|
---|
| 2479 | * Mark an instruction in a page as scanned/not scanned
|
---|
| 2480 | *
|
---|
| 2481 | * @returns VBox status code.
|
---|
[58122] | 2482 | * @param pVM The cross context VM structure.
|
---|
[1] | 2483 | * @param pInstr Instruction pointer
|
---|
[41732] | 2484 | * @param cbInstr Instruction size
|
---|
[1] | 2485 | * @param fScanned Mark as scanned or not
|
---|
| 2486 | */
|
---|
[44385] | 2487 | VMMR3_INT_DECL(int) CSAMR3MarkCode(PVM pVM, RTRCPTR pInstr, uint32_t cbInstr, bool fScanned)
|
---|
[1] | 2488 | {
|
---|
| 2489 | PCSAMPAGE pPage = 0;
|
---|
| 2490 |
|
---|
| 2491 | Assert(!fScanned); /* other case not implemented. */
|
---|
| 2492 | Assert(!PATMIsPatchGCAddr(pVM, pInstr));
|
---|
[70948] | 2493 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[1] | 2494 |
|
---|
| 2495 | if (csamIsCodeScanned(pVM, pInstr, &pPage) == false)
|
---|
| 2496 | {
|
---|
| 2497 | Assert(fScanned == true); /* other case should not be possible */
|
---|
| 2498 | return VINF_SUCCESS;
|
---|
| 2499 | }
|
---|
| 2500 |
|
---|
[41732] | 2501 | Log(("CSAMR3MarkCode: %RRv size=%d fScanned=%d\n", pInstr, cbInstr, fScanned));
|
---|
| 2502 | csamMarkCode(pVM, pPage, pInstr, cbInstr, fScanned);
|
---|
[1] | 2503 | return VINF_SUCCESS;
|
---|
| 2504 | }
|
---|
| 2505 |
|
---|
| 2506 |
|
---|
| 2507 | /**
|
---|
[882] | 2508 | * Scan and analyse code
|
---|
[1] | 2509 | *
|
---|
[882] | 2510 | * @returns VBox status code.
|
---|
[58122] | 2511 | * @param pVM The cross context VM structure.
|
---|
[55001] | 2512 | * @param pCtx Guest CPU context.
|
---|
| 2513 | * @param pInstrGC Instruction pointer.
|
---|
[1] | 2514 | */
|
---|
[55001] | 2515 | VMMR3_INT_DECL(int) CSAMR3CheckCodeEx(PVM pVM, PCPUMCTX pCtx, RTRCPTR pInstrGC)
|
---|
[1] | 2516 | {
|
---|
[70948] | 2517 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[70953] | 2518 | if (!EMIsRawRing0Enabled(pVM) || PATMIsPatchGCAddr(pVM, pInstrGC) == true)
|
---|
[1] | 2519 | {
|
---|
| 2520 | // No use
|
---|
| 2521 | return VINF_SUCCESS;
|
---|
| 2522 | }
|
---|
| 2523 |
|
---|
| 2524 | if (CSAMIsEnabled(pVM))
|
---|
| 2525 | {
|
---|
[9658] | 2526 | /* Assuming 32 bits code for now. */
|
---|
[42186] | 2527 | Assert(CPUMGetGuestCodeBits(VMMGetCpu0(pVM)) == 32);
|
---|
[1359] | 2528 |
|
---|
[55001] | 2529 | pInstrGC = SELMToFlat(pVM, DISSELREG_CS, CPUMCTX2CORE(pCtx), pInstrGC);
|
---|
[1086] | 2530 | return CSAMR3CheckCode(pVM, pInstrGC);
|
---|
| 2531 | }
|
---|
| 2532 | return VINF_SUCCESS;
|
---|
| 2533 | }
|
---|
| 2534 |
|
---|
| 2535 | /**
|
---|
| 2536 | * Scan and analyse code
|
---|
| 2537 | *
|
---|
| 2538 | * @returns VBox status code.
|
---|
[58122] | 2539 | * @param pVM The cross context VM structure.
|
---|
[1086] | 2540 | * @param pInstrGC Instruction pointer (0:32 virtual address)
|
---|
| 2541 | */
|
---|
[44385] | 2542 | VMMR3_INT_DECL(int) CSAMR3CheckCode(PVM pVM, RTRCPTR pInstrGC)
|
---|
[1086] | 2543 | {
|
---|
| 2544 | int rc;
|
---|
| 2545 | PCSAMPAGE pPage = NULL;
|
---|
[70948] | 2546 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[1086] | 2547 |
|
---|
[70953] | 2548 | if ( !EMIsRawRing0Enabled(pVM)
|
---|
[30572] | 2549 | || PATMIsPatchGCAddr(pVM, pInstrGC) == true)
|
---|
[1086] | 2550 | {
|
---|
[30572] | 2551 | /* Not active. */
|
---|
[1086] | 2552 | return VINF_SUCCESS;
|
---|
| 2553 | }
|
---|
| 2554 |
|
---|
| 2555 | if (CSAMIsEnabled(pVM))
|
---|
| 2556 | {
|
---|
[44362] | 2557 | /* Cache record for csamR3GCVirtToHCVirt */
|
---|
[26263] | 2558 | CSAMP2GLOOKUPREC cacheRec;
|
---|
| 2559 | RT_ZERO(cacheRec);
|
---|
[1086] | 2560 |
|
---|
[1] | 2561 | STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
|
---|
[1086] | 2562 | rc = csamAnalyseCallCodeStream(pVM, pInstrGC, pInstrGC, true /* 32 bits code */, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
[1] | 2563 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
|
---|
[30500] | 2564 | if (cacheRec.Lock.pvMap)
|
---|
| 2565 | PGMPhysReleasePageMappingLock(pVM, &cacheRec.Lock);
|
---|
| 2566 |
|
---|
[1] | 2567 | if (rc != VINF_SUCCESS)
|
---|
| 2568 | {
|
---|
| 2569 | Log(("csamAnalyseCodeStream failed with %d\n", rc));
|
---|
| 2570 | return rc;
|
---|
| 2571 | }
|
---|
| 2572 | }
|
---|
| 2573 | return VINF_SUCCESS;
|
---|
| 2574 | }
|
---|
| 2575 |
|
---|
| 2576 | /**
|
---|
| 2577 | * Flush dirty code pages
|
---|
| 2578 | *
|
---|
| 2579 | * @returns VBox status code.
|
---|
[58122] | 2580 | * @param pVM The cross context VM structure.
|
---|
[1] | 2581 | */
|
---|
[1830] | 2582 | static int csamR3FlushDirtyPages(PVM pVM)
|
---|
[1] | 2583 | {
|
---|
[22890] | 2584 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 2585 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
| 2586 |
|
---|
[1] | 2587 | STAM_PROFILE_START(&pVM->csam.s.StatFlushDirtyPages, a);
|
---|
| 2588 |
|
---|
[55937] | 2589 | for (uint32_t i = 0; i < pVM->csam.s.cDirtyPages; i++)
|
---|
[1] | 2590 | {
|
---|
| 2591 | int rc;
|
---|
| 2592 | PCSAMPAGEREC pPageRec;
|
---|
[55937] | 2593 | RTRCPTR GCPtr = pVM->csam.s.pvDirtyBasePage[i] & PAGE_BASE_GC_MASK;
|
---|
[1] | 2594 |
|
---|
[40274] | 2595 | #ifdef VBOX_WITH_REM
|
---|
| 2596 | /* Notify the recompiler that this page has been changed. */
|
---|
[18927] | 2597 | REMR3NotifyCodePageChanged(pVM, pVCpu, GCPtr);
|
---|
[55937] | 2598 | if (pVM->csam.s.pvDirtyFaultPage[i] != pVM->csam.s.pvDirtyBasePage[i])
|
---|
| 2599 | REMR3NotifyCodePageChanged(pVM, pVCpu, pVM->csam.s.pvDirtyFaultPage[i] & PAGE_BASE_GC_MASK);
|
---|
[40274] | 2600 | #endif
|
---|
[1] | 2601 |
|
---|
| 2602 | /* Enable write protection again. (use the fault address as it might be an alias) */
|
---|
[30326] | 2603 | rc = PGMShwMakePageReadonly(pVCpu, pVM->csam.s.pvDirtyFaultPage[i], 0 /*fFlags*/);
|
---|
[1] | 2604 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
| 2605 |
|
---|
[13822] | 2606 | Log(("CSAMR3FlushDirtyPages: flush %RRv (modifypage rc=%Rrc)\n", pVM->csam.s.pvDirtyBasePage[i], rc));
|
---|
[1] | 2607 |
|
---|
[36912] | 2608 | pPageRec = (PCSAMPAGEREC)RTAvlPVGet(&pVM->csam.s.pPageTree, (AVLPVKEY)(uintptr_t)GCPtr);
|
---|
[1] | 2609 | if (pPageRec && pPageRec->page.enmTag == CSAM_TAG_REM)
|
---|
| 2610 | {
|
---|
| 2611 | uint64_t fFlags;
|
---|
| 2612 |
|
---|
[18988] | 2613 | rc = PGMGstGetPage(pVCpu, GCPtr, &fFlags, NULL);
|
---|
[13818] | 2614 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc = %Rrc\n", rc));
|
---|
[1] | 2615 | if ( rc == VINF_SUCCESS
|
---|
| 2616 | && (fFlags & X86_PTE_US))
|
---|
| 2617 | {
|
---|
| 2618 | /* We don't care about user pages. */
|
---|
| 2619 | csamRemovePageRecord(pVM, GCPtr);
|
---|
| 2620 | STAM_COUNTER_INC(&pVM->csam.s.StatNrUserPages);
|
---|
| 2621 | }
|
---|
| 2622 | }
|
---|
| 2623 | }
|
---|
| 2624 | pVM->csam.s.cDirtyPages = 0;
|
---|
| 2625 | STAM_PROFILE_STOP(&pVM->csam.s.StatFlushDirtyPages, a);
|
---|
| 2626 | return VINF_SUCCESS;
|
---|
| 2627 | }
|
---|
| 2628 |
|
---|
| 2629 | /**
|
---|
[1830] | 2630 | * Flush potential new code pages
|
---|
| 2631 | *
|
---|
| 2632 | * @returns VBox status code.
|
---|
[58122] | 2633 | * @param pVM The cross context VM structure.
|
---|
[1830] | 2634 | */
|
---|
| 2635 | static int csamR3FlushCodePages(PVM pVM)
|
---|
| 2636 | {
|
---|
[22890] | 2637 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 2638 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
| 2639 |
|
---|
[1830] | 2640 | for (uint32_t i=0;i<pVM->csam.s.cPossibleCodePages;i++)
|
---|
| 2641 | {
|
---|
[18927] | 2642 | RTRCPTR GCPtr = pVM->csam.s.pvPossibleCodePage[i];
|
---|
[1830] | 2643 |
|
---|
| 2644 | GCPtr = GCPtr & PAGE_BASE_GC_MASK;
|
---|
| 2645 |
|
---|
[13822] | 2646 | Log(("csamR3FlushCodePages: %RRv\n", GCPtr));
|
---|
[30326] | 2647 | PGMShwMakePageNotPresent(pVCpu, GCPtr, 0 /*fFlags*/);
|
---|
[1833] | 2648 | /* Resync the page to make sure instruction fetch will fault */
|
---|
[1830] | 2649 | CSAMMarkPage(pVM, GCPtr, false);
|
---|
| 2650 | }
|
---|
| 2651 | pVM->csam.s.cPossibleCodePages = 0;
|
---|
| 2652 | return VINF_SUCCESS;
|
---|
| 2653 | }
|
---|
| 2654 |
|
---|
| 2655 | /**
|
---|
| 2656 | * Perform any pending actions
|
---|
| 2657 | *
|
---|
| 2658 | * @returns VBox status code.
|
---|
[58122] | 2659 | * @param pVM The cross context VM structure.
|
---|
[58123] | 2660 | * @param pVCpu The cross context virtual CPU structure.
|
---|
[1830] | 2661 | */
|
---|
[44385] | 2662 | VMMR3_INT_DECL(int) CSAMR3DoPendingAction(PVM pVM, PVMCPU pVCpu)
|
---|
[1830] | 2663 | {
|
---|
[70948] | 2664 | AssertReturn(VM_IS_RAW_MODE_ENABLED(pVM), VERR_CSAM_HM_IPE);
|
---|
[45620] | 2665 |
|
---|
[1830] | 2666 | csamR3FlushDirtyPages(pVM);
|
---|
| 2667 | csamR3FlushCodePages(pVM);
|
---|
| 2668 |
|
---|
[19141] | 2669 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_CSAM_PENDING_ACTION);
|
---|
[1830] | 2670 | return VINF_SUCCESS;
|
---|
| 2671 | }
|
---|
| 2672 |
|
---|
| 2673 | /**
|
---|
[1] | 2674 | * Analyse interrupt and trap gates
|
---|
| 2675 | *
|
---|
| 2676 | * @returns VBox status code.
|
---|
[58122] | 2677 | * @param pVM The cross context VM structure.
|
---|
[1] | 2678 | * @param iGate Start gate
|
---|
| 2679 | * @param cGates Number of gates to check
|
---|
| 2680 | */
|
---|
[44385] | 2681 | VMMR3_INT_DECL(int) CSAMR3CheckGates(PVM pVM, uint32_t iGate, uint32_t cGates)
|
---|
[1] | 2682 | {
|
---|
[43872] | 2683 | #ifdef VBOX_WITH_RAW_MODE
|
---|
[22890] | 2684 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 2685 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
[1] | 2686 | uint16_t cbIDT;
|
---|
[18927] | 2687 | RTRCPTR GCPtrIDT = CPUMGetGuestIDTR(pVCpu, &cbIDT);
|
---|
[1] | 2688 | uint32_t iGateEnd;
|
---|
| 2689 | uint32_t maxGates;
|
---|
| 2690 | VBOXIDTE aIDT[256];
|
---|
| 2691 | PVBOXIDTE pGuestIdte;
|
---|
| 2692 | int rc;
|
---|
| 2693 |
|
---|
[70948] | 2694 | AssertReturn(VM_IS_RAW_MODE_ENABLED(pVM), VERR_CSAM_HM_IPE);
|
---|
[70953] | 2695 | if (!EMIsRawRing0Enabled(pVM))
|
---|
[1] | 2696 | {
|
---|
| 2697 | /* Enabling interrupt gates only works when raw ring 0 is enabled. */
|
---|
[455] | 2698 | //AssertFailed();
|
---|
[1] | 2699 | return VINF_SUCCESS;
|
---|
| 2700 | }
|
---|
| 2701 |
|
---|
[347] | 2702 | /* We only check all gates once during a session */
|
---|
| 2703 | if ( !pVM->csam.s.fGatesChecked
|
---|
| 2704 | && cGates != 256)
|
---|
| 2705 | return VINF_SUCCESS; /* too early */
|
---|
| 2706 |
|
---|
| 2707 | /* We only check all gates once during a session */
|
---|
| 2708 | if ( pVM->csam.s.fGatesChecked
|
---|
| 2709 | && cGates != 1)
|
---|
| 2710 | return VINF_SUCCESS; /* ignored */
|
---|
| 2711 |
|
---|
[1974] | 2712 | Assert(cGates <= 256);
|
---|
| 2713 | if (!GCPtrIDT || cGates > 256)
|
---|
| 2714 | return VERR_INVALID_PARAMETER;
|
---|
| 2715 |
|
---|
[347] | 2716 | if (cGates != 1)
|
---|
[5198] | 2717 | {
|
---|
[347] | 2718 | pVM->csam.s.fGatesChecked = true;
|
---|
[5198] | 2719 | for (unsigned i=0;i<RT_ELEMENTS(pVM->csam.s.pvCallInstruction);i++)
|
---|
| 2720 | {
|
---|
[9344] | 2721 | RTRCPTR pHandler = pVM->csam.s.pvCallInstruction[i];
|
---|
[347] | 2722 |
|
---|
[5200] | 2723 | if (pHandler)
|
---|
[5198] | 2724 | {
|
---|
[5200] | 2725 | PCSAMPAGE pPage = NULL;
|
---|
[44362] | 2726 | CSAMP2GLOOKUPREC cacheRec; /* Cache record for csamR3GCVirtToHCVirt. */
|
---|
[26263] | 2727 | RT_ZERO(cacheRec);
|
---|
[5200] | 2728 |
|
---|
[13822] | 2729 | Log(("CSAMCheckGates: checking previous call instruction %RRv\n", pHandler));
|
---|
[5200] | 2730 | STAM_PROFILE_START(&pVM->csam.s.StatTime, a);
|
---|
| 2731 | rc = csamAnalyseCodeStream(pVM, pHandler, pHandler, true, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
| 2732 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, a);
|
---|
[30500] | 2733 | if (cacheRec.Lock.pvMap)
|
---|
| 2734 | PGMPhysReleasePageMappingLock(pVM, &cacheRec.Lock);
|
---|
| 2735 |
|
---|
[5200] | 2736 | if (rc != VINF_SUCCESS)
|
---|
| 2737 | {
|
---|
| 2738 | Log(("CSAMCheckGates: csamAnalyseCodeStream failed with %d\n", rc));
|
---|
| 2739 | continue;
|
---|
| 2740 | }
|
---|
[5198] | 2741 | }
|
---|
| 2742 | }
|
---|
| 2743 | }
|
---|
| 2744 |
|
---|
[1] | 2745 | /* Determine valid upper boundary. */
|
---|
| 2746 | maxGates = (cbIDT+1) / sizeof(VBOXIDTE);
|
---|
| 2747 | Assert(iGate < maxGates);
|
---|
| 2748 | if (iGate > maxGates)
|
---|
| 2749 | return VERR_INVALID_PARAMETER;
|
---|
| 2750 |
|
---|
| 2751 | if (iGate + cGates > maxGates)
|
---|
| 2752 | cGates = maxGates - iGate;
|
---|
[23] | 2753 |
|
---|
[1] | 2754 | GCPtrIDT = GCPtrIDT + iGate * sizeof(VBOXIDTE);
|
---|
| 2755 | iGateEnd = iGate + cGates;
|
---|
| 2756 |
|
---|
| 2757 | STAM_PROFILE_START(&pVM->csam.s.StatCheckGates, a);
|
---|
| 2758 |
|
---|
| 2759 | /*
|
---|
| 2760 | * Get IDT entries.
|
---|
| 2761 | */
|
---|
[30495] | 2762 | rc = PGMPhysSimpleReadGCPtr(pVCpu, aIDT, GCPtrIDT, cGates*sizeof(VBOXIDTE));
|
---|
| 2763 | if (RT_FAILURE(rc))
|
---|
[1] | 2764 | {
|
---|
[30495] | 2765 | AssertMsgRC(rc, ("Failed to read IDTE! rc=%Rrc\n", rc));
|
---|
| 2766 | STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
|
---|
| 2767 | return rc;
|
---|
[1] | 2768 | }
|
---|
[30495] | 2769 | pGuestIdte = &aIDT[0];
|
---|
[1] | 2770 |
|
---|
| 2771 | for (/*iGate*/; iGate<iGateEnd; iGate++, pGuestIdte++)
|
---|
| 2772 | {
|
---|
[347] | 2773 | Assert(TRPMR3GetGuestTrapHandler(pVM, iGate) == TRPM_INVALID_HANDLER);
|
---|
[1] | 2774 |
|
---|
| 2775 | if ( pGuestIdte->Gen.u1Present
|
---|
| 2776 | && (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
|
---|
| 2777 | && (pGuestIdte->Gen.u2DPL == 3 || pGuestIdte->Gen.u2DPL == 0)
|
---|
| 2778 | )
|
---|
| 2779 | {
|
---|
[9344] | 2780 | RTRCPTR pHandler;
|
---|
[1] | 2781 | PCSAMPAGE pPage = NULL;
|
---|
[19334] | 2782 | DBGFSELINFO selInfo;
|
---|
[44362] | 2783 | CSAMP2GLOOKUPREC cacheRec; /* Cache record for csamR3GCVirtToHCVirt. */
|
---|
[26263] | 2784 | RT_ZERO(cacheRec);
|
---|
[1] | 2785 |
|
---|
[9412] | 2786 | pHandler = VBOXIDTE_OFFSET(*pGuestIdte);
|
---|
[9675] | 2787 | pHandler = SELMToFlatBySel(pVM, pGuestIdte->Gen.u16SegSel, pHandler);
|
---|
[1] | 2788 |
|
---|
[18927] | 2789 | rc = SELMR3GetSelectorInfo(pVM, pVCpu, pGuestIdte->Gen.u16SegSel, &selInfo);
|
---|
[13816] | 2790 | if ( RT_FAILURE(rc)
|
---|
[19334] | 2791 | || (selInfo.fFlags & (DBGFSELINFO_FLAGS_NOT_PRESENT | DBGFSELINFO_FLAGS_INVALID))
|
---|
[1799] | 2792 | || selInfo.GCPtrBase != 0
|
---|
| 2793 | || selInfo.cbLimit != ~0U
|
---|
| 2794 | )
|
---|
| 2795 | {
|
---|
| 2796 | /* Refuse to patch a handler whose idt cs selector isn't wide open. */
|
---|
[13822] | 2797 | Log(("CSAMCheckGates: check gate %d failed due to rc %Rrc GCPtrBase=%RRv limit=%x\n", iGate, rc, selInfo.GCPtrBase, selInfo.cbLimit));
|
---|
[1799] | 2798 | continue;
|
---|
| 2799 | }
|
---|
| 2800 |
|
---|
[7628] | 2801 |
|
---|
[1] | 2802 | if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
|
---|
| 2803 | {
|
---|
[13822] | 2804 | Log(("CSAMCheckGates: check trap gate %d at %04X:%08X (flat %RRv)\n", iGate, pGuestIdte->Gen.u16SegSel, VBOXIDTE_OFFSET(*pGuestIdte), pHandler));
|
---|
[1] | 2805 | }
|
---|
| 2806 | else
|
---|
| 2807 | {
|
---|
[13822] | 2808 | Log(("CSAMCheckGates: check interrupt gate %d at %04X:%08X (flat %RRv)\n", iGate, pGuestIdte->Gen.u16SegSel, VBOXIDTE_OFFSET(*pGuestIdte), pHandler));
|
---|
[1] | 2809 | }
|
---|
| 2810 |
|
---|
[25273] | 2811 | STAM_PROFILE_START(&pVM->csam.s.StatTime, b);
|
---|
[1] | 2812 | rc = csamAnalyseCodeStream(pVM, pHandler, pHandler, true, CSAMR3AnalyseCallback, pPage, &cacheRec);
|
---|
[25273] | 2813 | STAM_PROFILE_STOP(&pVM->csam.s.StatTime, b);
|
---|
[30500] | 2814 | if (cacheRec.Lock.pvMap)
|
---|
| 2815 | PGMPhysReleasePageMappingLock(pVM, &cacheRec.Lock);
|
---|
| 2816 |
|
---|
[1] | 2817 | if (rc != VINF_SUCCESS)
|
---|
| 2818 | {
|
---|
| 2819 | Log(("CSAMCheckGates: csamAnalyseCodeStream failed with %d\n", rc));
|
---|
[347] | 2820 | continue;
|
---|
[1] | 2821 | }
|
---|
[846] | 2822 | /* OpenBSD guest specific patch test. */
|
---|
[347] | 2823 | if (iGate >= 0x20)
|
---|
[1] | 2824 | {
|
---|
[846] | 2825 | PCPUMCTX pCtx;
|
---|
| 2826 | DISCPUSTATE cpu;
|
---|
[9220] | 2827 | RTGCUINTPTR32 aOpenBsdPushCSOffset[3] = {0x03, /* OpenBSD 3.7 & 3.8 */
|
---|
[846] | 2828 | 0x2B, /* OpenBSD 4.0 installation ISO */
|
---|
| 2829 | 0x2F}; /* OpenBSD 4.0 after install */
|
---|
| 2830 |
|
---|
[18927] | 2831 | pCtx = CPUMQueryGuestCtxPtr(pVCpu);
|
---|
[846] | 2832 |
|
---|
[11311] | 2833 | for (unsigned i=0;i<RT_ELEMENTS(aOpenBsdPushCSOffset);i++)
|
---|
[846] | 2834 | {
|
---|
[18927] | 2835 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pHandler - aOpenBsdPushCSOffset[i], &cpu, NULL);
|
---|
[846] | 2836 | if ( rc == VINF_SUCCESS
|
---|
[41737] | 2837 | && cpu.pCurInstr->uOpcode == OP_PUSH
|
---|
[41738] | 2838 | && cpu.pCurInstr->fParam1 == OP_PARM_REG_CS)
|
---|
[846] | 2839 | {
|
---|
| 2840 | rc = PATMR3InstallPatch(pVM, pHandler - aOpenBsdPushCSOffset[i], PATMFL_CODE32 | PATMFL_GUEST_SPECIFIC);
|
---|
[13816] | 2841 | if (RT_SUCCESS(rc))
|
---|
[846] | 2842 | Log(("Installed OpenBSD interrupt handler prefix instruction (push cs) patch\n"));
|
---|
| 2843 | }
|
---|
| 2844 | }
|
---|
[347] | 2845 | }
|
---|
[208] | 2846 |
|
---|
[347] | 2847 | /* Trap gates and certain interrupt gates. */
|
---|
| 2848 | uint32_t fPatchFlags = PATMFL_CODE32 | PATMFL_IDTHANDLER;
|
---|
[1] | 2849 |
|
---|
[347] | 2850 | if (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32)
|
---|
| 2851 | fPatchFlags |= PATMFL_TRAPHANDLER;
|
---|
| 2852 | else
|
---|
| 2853 | fPatchFlags |= PATMFL_INTHANDLER;
|
---|
[1] | 2854 |
|
---|
[347] | 2855 | switch (iGate) {
|
---|
| 2856 | case 8:
|
---|
| 2857 | case 10:
|
---|
| 2858 | case 11:
|
---|
| 2859 | case 12:
|
---|
| 2860 | case 13:
|
---|
| 2861 | case 14:
|
---|
| 2862 | case 17:
|
---|
| 2863 | fPatchFlags |= PATMFL_TRAPHANDLER_WITH_ERRORCODE;
|
---|
| 2864 | break;
|
---|
| 2865 | default:
|
---|
| 2866 | /* No error code. */
|
---|
| 2867 | break;
|
---|
| 2868 | }
|
---|
[1] | 2869 |
|
---|
[13822] | 2870 | Log(("Installing %s gate handler for 0x%X at %RRv\n", (pGuestIdte->Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32) ? "trap" : "intr", iGate, pHandler));
|
---|
[1] | 2871 |
|
---|
[347] | 2872 | rc = PATMR3InstallPatch(pVM, pHandler, fPatchFlags);
|
---|
[48936] | 2873 | if ( RT_SUCCESS(rc)
|
---|
[45276] | 2874 | || rc == VERR_PATM_ALREADY_PATCHED)
|
---|
[347] | 2875 | {
|
---|
| 2876 | Log(("Gate handler 0x%X is SAFE!\n", iGate));
|
---|
| 2877 |
|
---|
[9344] | 2878 | RTRCPTR pNewHandlerGC = PATMR3QueryPatchGCPtr(pVM, pHandler);
|
---|
[347] | 2879 | if (pNewHandlerGC)
|
---|
[208] | 2880 | {
|
---|
[347] | 2881 | rc = TRPMR3SetGuestTrapHandler(pVM, iGate, pNewHandlerGC);
|
---|
[13816] | 2882 | if (RT_FAILURE(rc))
|
---|
[13818] | 2883 | Log(("TRPMR3SetGuestTrapHandler %d failed with %Rrc\n", iGate, rc));
|
---|
[1] | 2884 | }
|
---|
| 2885 | }
|
---|
| 2886 | }
|
---|
[347] | 2887 | } /* for */
|
---|
[1] | 2888 | STAM_PROFILE_STOP(&pVM->csam.s.StatCheckGates, a);
|
---|
[43872] | 2889 | #endif /* VBOX_WITH_RAW_MODE */
|
---|
[1] | 2890 | return VINF_SUCCESS;
|
---|
| 2891 | }
|
---|
| 2892 |
|
---|
| 2893 | /**
|
---|
[5198] | 2894 | * Record previous call instruction addresses
|
---|
| 2895 | *
|
---|
| 2896 | * @returns VBox status code.
|
---|
[58122] | 2897 | * @param pVM The cross context VM structure.
|
---|
[5198] | 2898 | * @param GCPtrCall Call address
|
---|
| 2899 | */
|
---|
[12989] | 2900 | VMMR3DECL(int) CSAMR3RecordCallAddress(PVM pVM, RTRCPTR GCPtrCall)
|
---|
[5198] | 2901 | {
|
---|
[70948] | 2902 | Assert(VM_IS_RAW_MODE_ENABLED(pVM));
|
---|
[5198] | 2903 | for (unsigned i=0;i<RT_ELEMENTS(pVM->csam.s.pvCallInstruction);i++)
|
---|
| 2904 | {
|
---|
| 2905 | if (pVM->csam.s.pvCallInstruction[i] == GCPtrCall)
|
---|
| 2906 | return VINF_SUCCESS;
|
---|
| 2907 | }
|
---|
| 2908 |
|
---|
[13822] | 2909 | Log(("CSAMR3RecordCallAddress %RRv\n", GCPtrCall));
|
---|
[5198] | 2910 |
|
---|
| 2911 | pVM->csam.s.pvCallInstruction[pVM->csam.s.iCallInstruction++] = GCPtrCall;
|
---|
| 2912 | if (pVM->csam.s.iCallInstruction >= RT_ELEMENTS(pVM->csam.s.pvCallInstruction))
|
---|
| 2913 | pVM->csam.s.iCallInstruction = 0;
|
---|
| 2914 |
|
---|
| 2915 | return VINF_SUCCESS;
|
---|
| 2916 | }
|
---|
| 2917 |
|
---|
| 2918 |
|
---|
| 2919 | /**
|
---|
[1] | 2920 | * Query CSAM state (enabled/disabled)
|
---|
| 2921 | *
|
---|
[44373] | 2922 | * @returns true if enabled, false otherwise.
|
---|
| 2923 | * @param pUVM The user mode VM handle.
|
---|
[1] | 2924 | */
|
---|
[44373] | 2925 | VMMR3DECL(bool) CSAMR3IsEnabled(PUVM pUVM)
|
---|
[1] | 2926 | {
|
---|
[44373] | 2927 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
|
---|
| 2928 | PVM pVM = pUVM->pVM;
|
---|
| 2929 | VM_ASSERT_VALID_EXT_RETURN(pVM, false);
|
---|
| 2930 | return CSAMIsEnabled(pVM);
|
---|
[1] | 2931 | }
|
---|
| 2932 |
|
---|
[44385] | 2933 |
|
---|
| 2934 | /**
|
---|
| 2935 | * Enables or disables code scanning.
|
---|
| 2936 | *
|
---|
| 2937 | * @returns VBox status code.
|
---|
| 2938 | * @param pUVM The user mode VM handle.
|
---|
| 2939 | * @param fEnabled Whether to enable or disable scanning.
|
---|
| 2940 | */
|
---|
| 2941 | VMMR3DECL(int) CSAMR3SetScanningEnabled(PUVM pUVM, bool fEnabled)
|
---|
| 2942 | {
|
---|
| 2943 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
| 2944 | PVM pVM = pUVM->pVM;
|
---|
| 2945 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
| 2946 |
|
---|
[70948] | 2947 | if (!VM_IS_RAW_MODE_ENABLED(pVM))
|
---|
[45620] | 2948 | {
|
---|
| 2949 | Assert(!pVM->fCSAMEnabled);
|
---|
| 2950 | return VINF_SUCCESS;
|
---|
| 2951 | }
|
---|
| 2952 |
|
---|
[44385] | 2953 | int rc;
|
---|
| 2954 | if (fEnabled)
|
---|
| 2955 | rc = CSAMEnableScanning(pVM);
|
---|
| 2956 | else
|
---|
| 2957 | rc = CSAMDisableScanning(pVM);
|
---|
| 2958 | return rc;
|
---|
| 2959 | }
|
---|
| 2960 |
|
---|
| 2961 |
|
---|
[1] | 2962 | #ifdef VBOX_WITH_DEBUGGER
|
---|
[35694] | 2963 |
|
---|
[1] | 2964 | /**
|
---|
[44399] | 2965 | * @callback_method_impl{FNDBGCCMD, The '.csamoff' command.}
|
---|
[1] | 2966 | */
|
---|
[44399] | 2967 | static DECLCALLBACK(int) csamr3CmdOff(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
|
---|
[1] | 2968 | {
|
---|
[44399] | 2969 | DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
|
---|
[39078] | 2970 | NOREF(cArgs); NOREF(paArgs);
|
---|
[1] | 2971 |
|
---|
[45620] | 2972 | if (HMR3IsEnabled(pUVM))
|
---|
| 2973 | return DBGCCmdHlpPrintf(pCmdHlp, "CSAM is permanently disabled by HM.\n");
|
---|
| 2974 |
|
---|
[44399] | 2975 | int rc = CSAMR3SetScanningEnabled(pUVM, false);
|
---|
[35694] | 2976 | if (RT_FAILURE(rc))
|
---|
[44399] | 2977 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "CSAMR3SetScanningEnabled");
|
---|
[35694] | 2978 | return DBGCCmdHlpPrintf(pCmdHlp, "CSAM Scanning disabled\n");
|
---|
[1] | 2979 | }
|
---|
| 2980 |
|
---|
| 2981 | /**
|
---|
[44399] | 2982 | * @callback_method_impl{FNDBGCCMD, The '.csamon' command.}
|
---|
[1] | 2983 | */
|
---|
[44399] | 2984 | static DECLCALLBACK(int) csamr3CmdOn(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
|
---|
[1] | 2985 | {
|
---|
[44399] | 2986 | DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
|
---|
[39078] | 2987 | NOREF(cArgs); NOREF(paArgs);
|
---|
[1] | 2988 |
|
---|
[45620] | 2989 | if (HMR3IsEnabled(pUVM))
|
---|
| 2990 | return DBGCCmdHlpPrintf(pCmdHlp, "CSAM is permanently disabled by HM.\n");
|
---|
| 2991 |
|
---|
[44399] | 2992 | int rc = CSAMR3SetScanningEnabled(pUVM, true);
|
---|
[35694] | 2993 | if (RT_FAILURE(rc))
|
---|
[44399] | 2994 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "CSAMR3SetScanningEnabled");
|
---|
[35694] | 2995 | return DBGCCmdHlpPrintf(pCmdHlp, "CSAM Scanning enabled\n");
|
---|
[1] | 2996 | }
|
---|
[35694] | 2997 |
|
---|
| 2998 | #endif /* VBOX_WITH_DEBUGGER */
|
---|