1 | /* $Id: CSAMInternal.h 41676 2012-06-12 20:53:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * CSAM - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 (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.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___CSAMInternal_h
|
---|
19 | #define ___CSAMInternal_h
|
---|
20 |
|
---|
21 | #include <VBox/cdefs.h>
|
---|
22 | #include <VBox/types.h>
|
---|
23 | #include <VBox/vmm/csam.h>
|
---|
24 | #include <VBox/dis.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | /** @name Page flags.
|
---|
30 | * These are placed in the three bits available for system programs in
|
---|
31 | * the page entries.
|
---|
32 | * @{ */
|
---|
33 | #ifndef PGM_PTFLAGS_CSAM_VALIDATED
|
---|
34 | /** Scanned and approved by CSAM (tm). */
|
---|
35 | /** NOTE: Must be identical to the one defined in PGMInternal.h!! */
|
---|
36 | #define PGM_PTFLAGS_CSAM_VALIDATED RT_BIT_64(11)
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /** @} */
|
---|
40 |
|
---|
41 | #define CSAM_SSM_VERSION 14
|
---|
42 |
|
---|
43 | #define CSAM_PGDIRBMP_CHUNKS 1024
|
---|
44 |
|
---|
45 | #define CSAM_PAGE_BITMAP_SIZE (PAGE_SIZE/(sizeof(uint8_t)*8))
|
---|
46 |
|
---|
47 | /* Maximum nr of dirty page that are cached. */
|
---|
48 | #define CSAM_MAX_DIRTY_PAGES 32
|
---|
49 |
|
---|
50 | /* Maximum number of cached addresses of dangerous instructions that have been scanned before. */
|
---|
51 | #define CSAM_MAX_DANGR_INSTR 16 /* power of two! */
|
---|
52 | #define CSAM_MAX_DANGR_INSTR_MASK (CSAM_MAX_DANGR_INSTR-1)
|
---|
53 |
|
---|
54 | /* Maximum number of possible dangerous code pages that we'll flush after a world switch */
|
---|
55 | #define CSAM_MAX_CODE_PAGES_FLUSH 32
|
---|
56 |
|
---|
57 | #define CSAM_MAX_CALLEXIT_RET 16
|
---|
58 |
|
---|
59 | /* copy from PATMInternal.h */
|
---|
60 | #define SIZEOF_NEARJUMP32 5 //opcode byte + 4 byte relative offset
|
---|
61 |
|
---|
62 | typedef struct
|
---|
63 | {
|
---|
64 | RTRCPTR pInstrAfterRetGC[CSAM_MAX_CALLEXIT_RET];
|
---|
65 | uint32_t cInstrAfterRet;
|
---|
66 | } CSAMCALLEXITREC, *PCSAMCALLEXITREC;
|
---|
67 |
|
---|
68 | typedef struct
|
---|
69 | {
|
---|
70 | R3PTRTYPE(uint8_t *) pPageLocStartHC;
|
---|
71 | R3PTRTYPE(uint8_t *) pPageLocEndHC;
|
---|
72 | RCPTRTYPE(uint8_t *) pGuestLoc;
|
---|
73 | uint32_t depth; //call/jump depth
|
---|
74 |
|
---|
75 | PCSAMCALLEXITREC pCallExitRec;
|
---|
76 |
|
---|
77 | PGMPAGEMAPLOCK Lock;
|
---|
78 | } CSAMP2GLOOKUPREC, *PCSAMP2GLOOKUPREC;
|
---|
79 |
|
---|
80 | typedef struct
|
---|
81 | {
|
---|
82 | RTRCPTR 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 |
|
---|
98 | typedef struct
|
---|
99 | {
|
---|
100 | // GC Patch pointer
|
---|
101 | RTRCPTR 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 | */
|
---|
114 | typedef 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 | */
|
---|
125 | typedef 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 | */
|
---|
139 | typedef 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 | RTRCPTR aDangerousInstr[CSAM_MAX_DANGR_INSTR];
|
---|
154 | uint32_t cDangerousInstr;
|
---|
155 | uint32_t iDangerousInstr;
|
---|
156 |
|
---|
157 | RCPTRTYPE(RTRCPTR *) pPDBitmapGC;
|
---|
158 | RCPTRTYPE(RTHCPTR *) pPDHCBitmapGC;
|
---|
159 | R3PTRTYPE(uint8_t **) pPDBitmapHC;
|
---|
160 | R3PTRTYPE(RTRCPTR *) 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 | RTRCPTR pvDirtyBasePage[CSAM_MAX_DIRTY_PAGES];
|
---|
173 | RTRCPTR pvDirtyFaultPage[CSAM_MAX_DIRTY_PAGES];
|
---|
174 |
|
---|
175 | /* To keep track of possible code pages */
|
---|
176 | uint32_t cPossibleCodePages;
|
---|
177 | RTRCPTR pvPossibleCodePage[CSAM_MAX_CODE_PAGES_FLUSH];
|
---|
178 |
|
---|
179 | /* call addresses reported by the recompiler */
|
---|
180 | RTRCPTR pvCallInstruction[16];
|
---|
181 | RTUINT iCallInstruction;
|
---|
182 |
|
---|
183 | /* Set when scanning has started. */
|
---|
184 | bool fScanningStarted;
|
---|
185 |
|
---|
186 | /* Set when the IDT gates have been checked for the first time. */
|
---|
187 | bool fGatesChecked;
|
---|
188 | bool Alignment1[HC_ARCH_BITS == 32 ? 6 : 2]; /**< Align the stats on an 8-byte boundary. */
|
---|
189 |
|
---|
190 | STAMCOUNTER StatNrTraps;
|
---|
191 | STAMCOUNTER StatNrPages;
|
---|
192 | STAMCOUNTER StatNrPagesInv;
|
---|
193 | STAMCOUNTER StatNrRemovedPages;
|
---|
194 | STAMCOUNTER StatNrPatchPages;
|
---|
195 | STAMCOUNTER StatNrPageNPHC;
|
---|
196 | STAMCOUNTER StatNrPageNPGC;
|
---|
197 | STAMCOUNTER StatNrFlushes;
|
---|
198 | STAMCOUNTER StatNrFlushesSkipped;
|
---|
199 | STAMCOUNTER StatNrKnownPagesHC;
|
---|
200 | STAMCOUNTER StatNrKnownPagesGC;
|
---|
201 | STAMCOUNTER StatNrInstr;
|
---|
202 | STAMCOUNTER StatNrBytesRead;
|
---|
203 | STAMCOUNTER StatNrOpcodeRead;
|
---|
204 | STAMPROFILE StatTime;
|
---|
205 | STAMPROFILE StatTimeCheckAddr;
|
---|
206 | STAMPROFILE StatTimeAddrConv;
|
---|
207 | STAMPROFILE StatTimeFlushPage;
|
---|
208 | STAMPROFILE StatTimeDisasm;
|
---|
209 | STAMPROFILE StatFlushDirtyPages;
|
---|
210 | STAMPROFILE StatCheckGates;
|
---|
211 | STAMCOUNTER StatCodePageModified;
|
---|
212 | STAMCOUNTER StatDangerousWrite;
|
---|
213 |
|
---|
214 | STAMCOUNTER StatInstrCacheHit;
|
---|
215 | STAMCOUNTER StatInstrCacheMiss;
|
---|
216 |
|
---|
217 | STAMCOUNTER StatPagePATM;
|
---|
218 | STAMCOUNTER StatPageCSAM;
|
---|
219 | STAMCOUNTER StatPageREM;
|
---|
220 | STAMCOUNTER StatNrUserPages;
|
---|
221 | STAMCOUNTER StatPageMonitor;
|
---|
222 | STAMCOUNTER StatPageRemoveREMFlush;
|
---|
223 |
|
---|
224 | STAMCOUNTER StatBitmapAlloc;
|
---|
225 |
|
---|
226 | STAMCOUNTER StatScanNextFunction;
|
---|
227 | STAMCOUNTER StatScanNextFunctionFailed;
|
---|
228 | } CSAM, *PCSAM;
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Call for analyzing the instructions following the privileged instr. for compliance with our heuristics
|
---|
232 | *
|
---|
233 | * @returns VBox status code.
|
---|
234 | * @param pVM The VM to operate on.
|
---|
235 | * @param pCpu CPU disassembly state
|
---|
236 | * @param pInstrHC Guest context pointer to privileged instruction
|
---|
237 | * @param pCurInstrGC Guest context pointer to current instruction
|
---|
238 | * @param pUserData User pointer
|
---|
239 | *
|
---|
240 | */
|
---|
241 | typedef int (VBOXCALL *PFN_CSAMR3ANALYSE)(PVM pVM, DISCPUSTATE *pCpu, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, PCSAMP2GLOOKUPREC pCacheRec, void *pUserData);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Calculate the branch destination
|
---|
245 | *
|
---|
246 | * @returns branch destination or 0 if failed
|
---|
247 | * @param pCpu Disassembly state of instruction.
|
---|
248 | * @param pBranchInstrGC GC pointer of branch instruction
|
---|
249 | */
|
---|
250 | inline RTRCPTR CSAMResolveBranch(PDISCPUSTATE pCpu, RTRCPTR pBranchInstrGC)
|
---|
251 | {
|
---|
252 | uint32_t disp;
|
---|
253 | if (pCpu->param1.flags & DISUSE_IMMEDIATE8_REL)
|
---|
254 | {
|
---|
255 | disp = (int32_t)(char)pCpu->param1.parval;
|
---|
256 | }
|
---|
257 | else
|
---|
258 | if (pCpu->param1.flags & DISUSE_IMMEDIATE16_REL)
|
---|
259 | {
|
---|
260 | disp = (int32_t)(uint16_t)pCpu->param1.parval;
|
---|
261 | }
|
---|
262 | else
|
---|
263 | if (pCpu->param1.flags & DISUSE_IMMEDIATE32_REL)
|
---|
264 | {
|
---|
265 | disp = (int32_t)pCpu->param1.parval;
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | Log(("We don't support far jumps here!! (%08X)\n", pCpu->param1.flags));
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 | #ifdef IN_RC
|
---|
273 | return (RTRCPTR)((uint8_t *)pBranchInstrGC + pCpu->opsize + disp);
|
---|
274 | #else
|
---|
275 | return pBranchInstrGC + pCpu->opsize + disp;
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 |
|
---|
279 | RT_C_DECLS_BEGIN
|
---|
280 | VMMRCDECL(int) CSAMGCCodePageWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange);
|
---|
281 | RT_C_DECLS_END
|
---|
282 |
|
---|
283 | #endif
|
---|