VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/CSAMInternal.h@ 5070

Last change on this file since 5070 was 4776, checked in by vboxsync, 17 years ago

Stricter type checking. Removed unnecessary code from the ring 0 module.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
1/* $Id: CSAMInternal.h 4776 2007-09-13 15:29:33Z vboxsync $ */
2/** @file
3 * CSAM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ___CSAMInternal_h
19#define ___CSAMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <VBox/csam.h>
24#include <VBox/dis.h>
25#include <VBox/log.h>
26
27#if !defined(IN_CSAM_R3) && !defined(IN_CSAM_R0) && !defined(IN_CSAM_GC)
28# error "Not in CSAM! This is an internal header!"
29#endif
30
31/** Page flags.
32 * These are placed in the three bits available for system programs in
33 * the page entries.
34 * @{ */
35#ifndef PGM_PTFLAGS_CSAM_VALIDATED
36/** Scanned and approved by CSAM (tm). */
37/** NOTE: Must be identical to the one defined in PGMInternal.h!! */
38#define PGM_PTFLAGS_CSAM_VALIDATED BIT64(11)
39#endif
40
41/** @} */
42
43#define CSAM_SSM_VERSION 14
44
45#define CSAM_PGDIRBMP_CHUNKS 1024
46
47#define CSAM_PAGE_BITMAP_SIZE (PAGE_SIZE/(sizeof(uint8_t)*8))
48
49/* Maximum nr of dirty page that are cached. */
50#define CSAM_MAX_DIRTY_PAGES 32
51
52/* Maximum number of cached addresses of dangerous instructions that have been scanned before. */
53#define CSAM_MAX_DANGR_INSTR 16 /* power of two! */
54#define CSAM_MAX_DANGR_INSTR_MASK (CSAM_MAX_DANGR_INSTR-1)
55
56/* Maximum number of possible dangerous code pages that we'll flush after a world switch */
57#define CSAM_MAX_CODE_PAGES_FLUSH 32
58
59#define CSAM_MAX_CALLEXIT_RET 16
60
61/* copy from PATMInternal.h */
62#define SIZEOF_NEARJUMP32 5 //opcode byte + 4 byte relative offset
63
64typedef struct
65{
66 RTGCPTR pInstrAfterRetGC[CSAM_MAX_CALLEXIT_RET];
67 uint32_t cInstrAfterRet;
68} CSAMCALLEXITREC, *PCSAMCALLEXITREC;
69
70typedef struct
71{
72 R3PTRTYPE(uint8_t *) pPageLocStartHC;
73 R3PTRTYPE(uint8_t *) pPageLocEndHC;
74 GCPTRTYPE(uint8_t *) pGuestLoc;
75 uint32_t depth; //call/jump depth
76
77 PCSAMCALLEXITREC pCallExitRec;
78} CSAMP2GLOOKUPREC, *PCSAMP2GLOOKUPREC;
79
80typedef struct
81{
82 RTGCPTR pPageGC;
83 RTGCPHYS GCPhys;
84 uint64_t fFlags;
85 uint32_t uSize;
86
87 uint8_t *pBitmap;
88
89 bool fCode32;
90 bool fMonitorActive;
91 bool fMonitorInvalidation;
92
93 CSAMTAG enmTag;
94
95 uint64_t u64Hash;
96} CSAMPAGE, *PCSAMPAGE;
97
98typedef struct
99{
100 // GC Patch pointer
101 RTGCPTR pInstrGC;
102
103 // Disassembly state for original instruction
104 DISCPUSTATE cpu;
105
106 uint32_t uState;
107
108 PCSAMPAGE pPage;
109} CSAMPATCH, *PCSAMPATCH;
110
111/**
112 * Lookup record for CSAM pages
113 */
114typedef struct CSAMPAGEREC
115{
116 /** The key is a GC virtual address. */
117 AVLPVNODECORE Core;
118 CSAMPAGE page;
119
120} CSAMPAGEREC, *PCSAMPAGEREC;
121
122/**
123 * Lookup record for patches
124 */
125typedef struct CSAMPATCHREC
126{
127 /** The key is a GC virtual address. */
128 AVLPVNODECORE Core;
129 CSAMPATCH patch;
130
131} CSAMPATCHREC, *PCSAMPATCHREC;
132
133
134/**
135 * CSAM VM Instance data.
136 * Changes to this must checked against the padding of the CSAM union in VM!
137 * @note change SSM version when changing it!!
138 */
139typedef struct CSAM
140{
141 /** Offset to the VM structure.
142 * See CSAM2VM(). */
143 RTINT offVM;
144#if HC_ARCH_BITS == 64
145 RTINT Alignment0; /**< Align pPageTree correctly. */
146#endif
147
148 R3PTRTYPE(PAVLPVNODECORE) pPageTree;
149
150 /* Array to store previously scanned dangerous instructions, so we don't need to
151 * switch back to ring 3 each time we encounter them in GC.
152 */
153 RTGCPTR aDangerousInstr[CSAM_MAX_DANGR_INSTR];
154 uint32_t cDangerousInstr;
155 uint32_t iDangerousInstr;
156
157 GCPTRTYPE(RTGCPTR *) pPDBitmapGC;
158 GCPTRTYPE(RTHCPTR *) pPDHCBitmapGC;
159 R3PTRTYPE(uint8_t **) pPDBitmapHC;
160 R3PTRTYPE(RTGCPTR *) pPDGCBitmapHC;
161
162 /* Temporary storage during load/save state */
163 struct
164 {
165 R3PTRTYPE(PSSMHANDLE) pSSM;
166 uint32_t cPageRecords;
167 uint32_t cPatchPageRecords;
168 } savedstate;
169
170 /* To keep track of dirty pages */
171 uint32_t cDirtyPages;
172 RTGCPTR pvDirtyBasePage[CSAM_MAX_DIRTY_PAGES];
173 RTGCPTR pvDirtyFaultPage[CSAM_MAX_DIRTY_PAGES];
174
175 /* To keep track of possible code pages */
176 uint32_t cPossibleCodePages;
177 RTGCPTR pvPossibleCodePage[CSAM_MAX_CODE_PAGES_FLUSH];
178
179 /* Set when scanning has started. */
180 bool fScanningStarted;
181
182 /* Set when the IDT gates have been checked for the first time. */
183 bool fGatesChecked;
184 bool Alignment1[HC_ARCH_BITS == 32 ? 4 : 6]; /**< Align the stats on an 8-byte boundrary. */
185
186 STAMCOUNTER StatNrTraps;
187 STAMCOUNTER StatNrPages;
188 STAMCOUNTER StatNrPagesInv;
189 STAMCOUNTER StatNrRemovedPages;
190 STAMCOUNTER StatNrPatchPages;
191 STAMCOUNTER StatNrPageNPHC;
192 STAMCOUNTER StatNrPageNPGC;
193 STAMCOUNTER StatNrFlushes;
194 STAMCOUNTER StatNrFlushesSkipped;
195 STAMCOUNTER StatNrKnownPagesHC;
196 STAMCOUNTER StatNrKnownPagesGC;
197 STAMCOUNTER StatNrInstr;
198 STAMCOUNTER StatNrBytesRead;
199 STAMCOUNTER StatNrOpcodeRead;
200 STAMPROFILE StatTime;
201 STAMPROFILE StatTimeCheckAddr;
202 STAMPROFILE StatTimeAddrConv;
203 STAMPROFILE StatTimeFlushPage;
204 STAMPROFILE StatTimeDisasm;
205 STAMPROFILE StatFlushDirtyPages;
206 STAMPROFILE StatCheckGates;
207 STAMCOUNTER StatCodePageModified;
208 STAMCOUNTER StatDangerousWrite;
209
210 STAMCOUNTER StatInstrCacheHit;
211 STAMCOUNTER StatInstrCacheMiss;
212
213 STAMCOUNTER StatPagePATM;
214 STAMCOUNTER StatPageCSAM;
215 STAMCOUNTER StatPageREM;
216 STAMCOUNTER StatNrUserPages;
217 STAMCOUNTER StatPageMonitor;
218 STAMCOUNTER StatPageRemoveREMFlush;
219
220 STAMCOUNTER StatBitmapAlloc;
221
222 STAMCOUNTER StatScanNextFunction;
223 STAMCOUNTER StatScanNextFunctionFailed;
224} CSAM, *PCSAM;
225
226/**
227 * Call for analyzing the instructions following the privileged instr. for compliance with our heuristics
228 *
229 * @returns VBox status code.
230 * @param pVM The VM to operate on.
231 * @param pCpu CPU disassembly state
232 * @param pInstrHC Guest context pointer to privileged instruction
233 * @param pCurInstrGC Guest context pointer to current instruction
234 * @param pUserData User pointer
235 *
236 */
237typedef int (VBOXCALL *PFN_CSAMR3ANALYSE)(PVM pVM, DISCPUSTATE *pCpu, GCPTRTYPE(uint8_t *) pInstrGC, GCPTRTYPE(uint8_t *) pCurInstrGC, PCSAMP2GLOOKUPREC pCacheRec, void *pUserData);
238
239/**
240 * Calculate the branch destination
241 *
242 * @returns branch destination or 0 if failed
243 * @param pCpu Disassembly state of instruction.
244 * @param pBranchInstrGC GC pointer of branch instruction
245 */
246inline RTGCPTR CSAMResolveBranch(PDISCPUSTATE pCpu, RTGCPTR pBranchInstrGC)
247{
248 uint32_t disp;
249 if (pCpu->param1.flags & USE_IMMEDIATE8_REL)
250 {
251 disp = (int32_t)(char)pCpu->param1.parval;
252 }
253 else
254 if (pCpu->param1.flags & USE_IMMEDIATE16_REL)
255 {
256 disp = (int32_t)(uint16_t)pCpu->param1.parval;
257 }
258 else
259 if (pCpu->param1.flags & USE_IMMEDIATE32_REL)
260 {
261 disp = (int32_t)pCpu->param1.parval;
262 }
263 else
264 {
265 Log(("We don't support far jumps here!! (%08X)\n", pCpu->param1.flags));
266 return 0;
267 }
268#ifdef IN_GC
269 return (RTGCPTR)((uint8_t *)pBranchInstrGC + pCpu->opsize + disp);
270#else
271 return pBranchInstrGC + pCpu->opsize + disp;
272#endif
273}
274
275__BEGIN_DECLS
276CSAMGCDECL(int) CSAMGCCodePageWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange);
277__END_DECLS
278
279#endif
Note: See TracBrowser for help on using the repository browser.

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