1 | /* $Id: EMInternal.h 73022 2018-07-10 09:24:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 ___EMInternal_h
|
---|
19 | #define ___EMInternal_h
|
---|
20 |
|
---|
21 | #include <VBox/cdefs.h>
|
---|
22 | #include <VBox/types.h>
|
---|
23 | #include <VBox/vmm/em.h>
|
---|
24 | #include <VBox/vmm/stam.h>
|
---|
25 | #include <VBox/vmm/patm.h>
|
---|
26 | #include <VBox/dis.h>
|
---|
27 | #include <VBox/vmm/pdmcritsect.h>
|
---|
28 | #include <iprt/avl.h>
|
---|
29 | #include <setjmp.h>
|
---|
30 |
|
---|
31 | RT_C_DECLS_BEGIN
|
---|
32 |
|
---|
33 |
|
---|
34 | /** @defgroup grp_em_int Internal
|
---|
35 | * @ingroup grp_em
|
---|
36 | * @internal
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /** The saved state version. */
|
---|
41 | #define EM_SAVED_STATE_VERSION 5
|
---|
42 | #define EM_SAVED_STATE_VERSION_PRE_IEM 4
|
---|
43 | #define EM_SAVED_STATE_VERSION_PRE_MWAIT 3
|
---|
44 | #define EM_SAVED_STATE_VERSION_PRE_SMP 2
|
---|
45 |
|
---|
46 |
|
---|
47 | /** @name MWait state flags.
|
---|
48 | * @{
|
---|
49 | */
|
---|
50 | /** MWait activated. */
|
---|
51 | #define EMMWAIT_FLAG_ACTIVE RT_BIT(0)
|
---|
52 | /** MWait will continue when an interrupt is pending even when IF=0. */
|
---|
53 | #define EMMWAIT_FLAG_BREAKIRQIF0 RT_BIT(1)
|
---|
54 | /** Monitor instruction was executed previously. */
|
---|
55 | #define EMMWAIT_FLAG_MONITOR_ACTIVE RT_BIT(2)
|
---|
56 | /** @} */
|
---|
57 |
|
---|
58 | /** EM time slice in ms; used for capping execution time. */
|
---|
59 | #define EM_TIME_SLICE 100
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Cli node structure
|
---|
63 | */
|
---|
64 | typedef struct CLISTAT
|
---|
65 | {
|
---|
66 | /** The key is the cli address. */
|
---|
67 | AVLGCPTRNODECORE Core;
|
---|
68 | #if HC_ARCH_BITS == 32 && !defined(RT_OS_WINDOWS)
|
---|
69 | /** Padding. */
|
---|
70 | uint32_t u32Padding;
|
---|
71 | #endif
|
---|
72 | /** Occurrences. */
|
---|
73 | STAMCOUNTER Counter;
|
---|
74 | } CLISTAT, *PCLISTAT;
|
---|
75 | #ifdef IN_RING3
|
---|
76 | AssertCompileMemberAlignment(CLISTAT, Counter, 8);
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Excessive (used to be) EM statistics.
|
---|
82 | */
|
---|
83 | typedef struct EMSTATS
|
---|
84 | {
|
---|
85 | #if 1 /* rawmode only? */
|
---|
86 | /** @name Privileged Instructions Ending Up In HC.
|
---|
87 | * @{ */
|
---|
88 | STAMCOUNTER StatIoRestarted;
|
---|
89 | STAMCOUNTER StatIoIem;
|
---|
90 | STAMCOUNTER StatCli;
|
---|
91 | STAMCOUNTER StatSti;
|
---|
92 | STAMCOUNTER StatInvlpg;
|
---|
93 | STAMCOUNTER StatHlt;
|
---|
94 | STAMCOUNTER StatMovReadCR[DISCREG_CR4 + 1];
|
---|
95 | STAMCOUNTER StatMovWriteCR[DISCREG_CR4 + 1];
|
---|
96 | STAMCOUNTER StatMovDRx;
|
---|
97 | STAMCOUNTER StatIret;
|
---|
98 | STAMCOUNTER StatMovLgdt;
|
---|
99 | STAMCOUNTER StatMovLldt;
|
---|
100 | STAMCOUNTER StatMovLidt;
|
---|
101 | STAMCOUNTER StatMisc;
|
---|
102 | STAMCOUNTER StatSysEnter;
|
---|
103 | STAMCOUNTER StatSysExit;
|
---|
104 | STAMCOUNTER StatSysCall;
|
---|
105 | STAMCOUNTER StatSysRet;
|
---|
106 | /** @} */
|
---|
107 | #endif
|
---|
108 | } EMSTATS;
|
---|
109 | /** Pointer to the excessive EM statistics. */
|
---|
110 | typedef EMSTATS *PEMSTATS;
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Exit history entry.
|
---|
115 | *
|
---|
116 | * @remarks We could perhaps trim this down a little bit by assuming uFlatPC
|
---|
117 | * only needs 48 bits (currently true but will change) and stuffing
|
---|
118 | * the flags+type in the available 16 bits made available. The
|
---|
119 | * timestamp could likewise be shortened to accomodate the index, or
|
---|
120 | * we might skip the index entirely. However, since we will have to
|
---|
121 | * deal with 56-bit wide PC address before long, there's not point.
|
---|
122 | *
|
---|
123 | * On the upside, there are unused bits in both uFlagsAndType and the
|
---|
124 | * idxSlot fields if needed for anything.
|
---|
125 | */
|
---|
126 | typedef struct EMEXITENTRY
|
---|
127 | {
|
---|
128 | /** The flat PC (CS:EIP/RIP) address of the exit.
|
---|
129 | * UINT64_MAX if not available. */
|
---|
130 | uint64_t uFlatPC;
|
---|
131 | /** The EMEXIT_MAKE_FLAGS_AND_TYPE */
|
---|
132 | uint32_t uFlagsAndType;
|
---|
133 | /** The index into the exit slot hash table.
|
---|
134 | * UINT32_MAX if too many collisions and not entered into it. */
|
---|
135 | uint32_t idxSlot;
|
---|
136 | /** The TSC timestamp of the exit.
|
---|
137 | * This is 0 if not timestamped. */
|
---|
138 | uint64_t uTimestamp;
|
---|
139 | } EMEXITENTRY;
|
---|
140 | /** Pointer to an exit history entry. */
|
---|
141 | typedef EMEXITENTRY *PEMEXITENTRY;
|
---|
142 | /** Pointer to a const exit history entry. */
|
---|
143 | typedef EMEXITENTRY const *PCEMEXITENTRY;
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Converts a EM pointer into a VM pointer.
|
---|
148 | * @returns Pointer to the VM structure the EM is part of.
|
---|
149 | * @param pEM Pointer to EM instance data.
|
---|
150 | */
|
---|
151 | #define EM2VM(pEM) ( (PVM)((char*)pEM - pEM->offVM) )
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * EM VM Instance data.
|
---|
155 | * Changes to this must checked against the padding of the cfgm union in VM!
|
---|
156 | */
|
---|
157 | typedef struct EM
|
---|
158 | {
|
---|
159 | /** Offset to the VM structure.
|
---|
160 | * See EM2VM(). */
|
---|
161 | RTUINT offVM;
|
---|
162 |
|
---|
163 | /** Whether IEM executes everything. */
|
---|
164 | bool fIemExecutesAll;
|
---|
165 | /** Whether a triple fault triggers a guru. */
|
---|
166 | bool fGuruOnTripleFault;
|
---|
167 | /** Alignment padding. */
|
---|
168 | bool afPadding[6];
|
---|
169 |
|
---|
170 | /** Id of the VCPU that last executed code in the recompiler. */
|
---|
171 | VMCPUID idLastRemCpu;
|
---|
172 |
|
---|
173 | #ifdef VBOX_WITH_REM
|
---|
174 | /** REM critical section.
|
---|
175 | * This protects recompiler usage
|
---|
176 | */
|
---|
177 | PDMCRITSECT CritSectREM;
|
---|
178 | #endif
|
---|
179 | } EM;
|
---|
180 | /** Pointer to EM VM instance data. */
|
---|
181 | typedef EM *PEM;
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * EM VMCPU Instance data.
|
---|
186 | */
|
---|
187 | typedef struct EMCPU
|
---|
188 | {
|
---|
189 | /** Execution Manager State. */
|
---|
190 | EMSTATE volatile enmState;
|
---|
191 |
|
---|
192 | /** The state prior to the suspending of the VM. */
|
---|
193 | EMSTATE enmPrevState;
|
---|
194 |
|
---|
195 | /** Force raw-mode execution.
|
---|
196 | * This is used to prevent REM from trying to execute patch code.
|
---|
197 | * The flag is cleared upon entering emR3RawExecute() and updated in certain return paths. */
|
---|
198 | bool fForceRAW;
|
---|
199 |
|
---|
200 | /** Set if hypercall instruction VMMCALL (AMD) & VMCALL (Intel) are enabled.
|
---|
201 | * GIM sets this and the execution managers queries it. Not saved, as GIM
|
---|
202 | * takes care of that bit too. */
|
---|
203 | bool fHypercallEnabled;
|
---|
204 |
|
---|
205 | /** Explicit padding. */
|
---|
206 | uint8_t abPadding[2];
|
---|
207 |
|
---|
208 | /** The number of instructions we've executed in IEM since switching to the
|
---|
209 | * EMSTATE_IEM_THEN_REM state. */
|
---|
210 | uint32_t cIemThenRemInstructions;
|
---|
211 |
|
---|
212 | /** Inhibit interrupts for this instruction. Valid only when VM_FF_INHIBIT_INTERRUPTS is set. */
|
---|
213 | RTGCUINTPTR GCPtrInhibitInterrupts;
|
---|
214 |
|
---|
215 | #ifdef VBOX_WITH_RAW_MODE
|
---|
216 | /** Pointer to the PATM status structure. (R3 Ptr) */
|
---|
217 | R3PTRTYPE(PPATMGCSTATE) pPatmGCState;
|
---|
218 | #else
|
---|
219 | RTR3PTR R3PtrPaddingNoRaw;
|
---|
220 | #endif
|
---|
221 | RTR3PTR R3PtrNullPadding; /**< Used to be pCtx. */
|
---|
222 | #if GC_ARCH_BITS == 64
|
---|
223 | RTGCPTR aPadding1;
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | /** Start of the current time slice in ms. */
|
---|
227 | uint64_t u64TimeSliceStart;
|
---|
228 | /** Start of the current time slice in thread execution time (ms). */
|
---|
229 | uint64_t u64TimeSliceStartExec;
|
---|
230 | /** Current time slice value. */
|
---|
231 | uint64_t u64TimeSliceExec;
|
---|
232 |
|
---|
233 | /** Pending ring-3 I/O port access (VINF_EM_PENDING_R3_IOPORT_READ / VINF_EM_PENDING_R3_IOPORT_WRITE). */
|
---|
234 | struct
|
---|
235 | {
|
---|
236 | RTIOPORT uPort; /**< The I/O port number.*/
|
---|
237 | uint8_t cbValue; /**< The value size in bytes. Zero when not pending. */
|
---|
238 | uint8_t cbInstr; /**< The instruction length. */
|
---|
239 | uint32_t uValue; /**< The value to write. */
|
---|
240 | } PendingIoPortAccess;
|
---|
241 |
|
---|
242 | /** MWait halt state. */
|
---|
243 | struct
|
---|
244 | {
|
---|
245 | uint32_t fWait; /**< Type of mwait; see EMMWAIT_FLAG_*. */
|
---|
246 | uint32_t u32Padding;
|
---|
247 | RTGCPTR uMWaitRAX; /**< MWAIT hints. */
|
---|
248 | RTGCPTR uMWaitRCX; /**< MWAIT extensions. */
|
---|
249 | RTGCPTR uMonitorRAX; /**< Monitored address. */
|
---|
250 | RTGCPTR uMonitorRCX; /**< Monitor extension. */
|
---|
251 | RTGCPTR uMonitorRDX; /**< Monitor hint. */
|
---|
252 | } MWait;
|
---|
253 |
|
---|
254 | union
|
---|
255 | {
|
---|
256 | /** Padding used in the other rings.
|
---|
257 | * This must be larger than jmp_buf on any supported platform. */
|
---|
258 | char achPaddingFatalLongJump[HC_ARCH_BITS == 32 ? 176 : 256];
|
---|
259 | #ifdef IN_RING3
|
---|
260 | /** Long buffer jump for fatal VM errors.
|
---|
261 | * It will jump to before the outer EM loop is entered. */
|
---|
262 | jmp_buf FatalLongJump;
|
---|
263 | #endif
|
---|
264 | } u;
|
---|
265 |
|
---|
266 | /** For saving stack space, the disassembler state is allocated here instead of
|
---|
267 | * on the stack. */
|
---|
268 | DISCPUSTATE DisState;
|
---|
269 |
|
---|
270 | /** @name Execution profiling.
|
---|
271 | * @{ */
|
---|
272 | STAMPROFILE StatForcedActions;
|
---|
273 | STAMPROFILE StatHalted;
|
---|
274 | STAMPROFILEADV StatCapped;
|
---|
275 | STAMPROFILEADV StatHMEntry;
|
---|
276 | STAMPROFILE StatHMExec;
|
---|
277 | STAMPROFILE StatIEMEmu;
|
---|
278 | STAMPROFILE StatIEMThenREM;
|
---|
279 | STAMPROFILEADV StatNEMEntry;
|
---|
280 | STAMPROFILE StatNEMExec;
|
---|
281 | STAMPROFILE StatREMEmu;
|
---|
282 | STAMPROFILE StatREMExec;
|
---|
283 | STAMPROFILE StatREMSync;
|
---|
284 | STAMPROFILEADV StatREMTotal;
|
---|
285 | STAMPROFILE StatRAWExec;
|
---|
286 | STAMPROFILEADV StatRAWEntry;
|
---|
287 | STAMPROFILEADV StatRAWTail;
|
---|
288 | STAMPROFILEADV StatRAWTotal;
|
---|
289 | STAMPROFILEADV StatTotal;
|
---|
290 | /** @} */
|
---|
291 |
|
---|
292 | /** R3: Profiling of emR3RawExecuteIOInstruction. */
|
---|
293 | STAMPROFILE StatIOEmu;
|
---|
294 | /** R3: Profiling of emR3RawPrivileged. */
|
---|
295 | STAMPROFILE StatPrivEmu;
|
---|
296 | /** R3: Number of times emR3HmExecute is called. */
|
---|
297 | STAMCOUNTER StatHMExecuteCalled;
|
---|
298 | /** R3: Number of times emR3NEMExecute is called. */
|
---|
299 | STAMCOUNTER StatNEMExecuteCalled;
|
---|
300 |
|
---|
301 | /** More statistics (R3). */
|
---|
302 | R3PTRTYPE(PEMSTATS) pStatsR3;
|
---|
303 | /** More statistics (R0). */
|
---|
304 | R0PTRTYPE(PEMSTATS) pStatsR0;
|
---|
305 | /** More statistics (RC). */
|
---|
306 | RCPTRTYPE(PEMSTATS) pStatsRC;
|
---|
307 | #if HC_ARCH_BITS == 64
|
---|
308 | RTRCPTR padding0;
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | /** Tree for keeping track of cli occurrences (debug only). */
|
---|
312 | R3PTRTYPE(PAVLGCPTRNODECORE) pCliStatTree;
|
---|
313 | STAMCOUNTER StatTotalClis;
|
---|
314 | #if HC_ARCH_BITS == 32
|
---|
315 | uint64_t padding1;
|
---|
316 | #endif
|
---|
317 |
|
---|
318 | /** Exit history table (6KB). */
|
---|
319 | EMEXITENTRY aExitHistory[256];
|
---|
320 | /** Where to store the next exit history entry.
|
---|
321 | * Since aExitHistory is 256 items longs, we'll just increment this and
|
---|
322 | * mask it when using it. That help the readers detect whether we've
|
---|
323 | * wrapped around or not. */
|
---|
324 | uint64_t iNextExit;
|
---|
325 |
|
---|
326 | /** Index into aExitRecords set by EMHistoryExec when returning to ring-3.
|
---|
327 | * This is UINT16_MAX if not armed. */
|
---|
328 | uint16_t volatile idxContinueExitRec;
|
---|
329 | /** Whether exit optimizations are enabled or not (in general). */
|
---|
330 | bool fExitOptimizationEnabled : 1;
|
---|
331 | /** Whether exit optimizations are enabled for ring-0 (in general). */
|
---|
332 | bool fExitOptimizationEnabledR0 : 1;
|
---|
333 | /** Whether exit optimizations are enabled for ring-0 when preemption is disabled. */
|
---|
334 | bool fExitOptimizationEnabledR0PreemptDisabled : 1;
|
---|
335 | /** Explicit padding. */
|
---|
336 | bool fPadding2;
|
---|
337 | /** Max number of instructions to execute. */
|
---|
338 | uint16_t cHistoryExecMaxInstructions;
|
---|
339 | /** Min number of instructions to execute while probing. */
|
---|
340 | uint16_t cHistoryProbeMinInstructions;
|
---|
341 | /** Max number of instructions to execute without an exit before giving up probe. */
|
---|
342 | uint16_t cHistoryProbeMaxInstructionsWithoutExit;
|
---|
343 | uint16_t uPadding3;
|
---|
344 | /** Number of exit records in use. */
|
---|
345 | uint32_t cExitRecordUsed;
|
---|
346 | /** Profiling the EMHistoryExec when executing (not probing). */
|
---|
347 | STAMPROFILE StatHistoryExec;
|
---|
348 | /** Number of saved exits. */
|
---|
349 | STAMCOUNTER StatHistoryExecSavedExits;
|
---|
350 | /** Number of instructions executed by EMHistoryExec. */
|
---|
351 | STAMCOUNTER StatHistoryExecInstructions;
|
---|
352 | uint64_t uPadding4;
|
---|
353 | /** Number of instructions executed by EMHistoryExec when probing. */
|
---|
354 | STAMCOUNTER StatHistoryProbeInstructions;
|
---|
355 | /** Number of times probing resulted in EMEXITACTION_NORMAL_PROBED. */
|
---|
356 | STAMCOUNTER StatHistoryProbedNormal;
|
---|
357 | /** Number of times probing resulted in EMEXITACTION_EXEC_WITH_MAX. */
|
---|
358 | STAMCOUNTER StatHistoryProbedExecWithMax;
|
---|
359 | /** Number of times probing resulted in ring-3 continuation. */
|
---|
360 | STAMCOUNTER StatHistoryProbedToRing3;
|
---|
361 | /** Profiling the EMHistoryExec when probing.*/
|
---|
362 | STAMPROFILE StatHistoryProbe;
|
---|
363 | /** Hit statistics for each lookup step. */
|
---|
364 | STAMCOUNTER aStatHistoryRecHits[16];
|
---|
365 | /** Type change statistics for each lookup step. */
|
---|
366 | STAMCOUNTER aStatHistoryRecTypeChanged[16];
|
---|
367 | /** Replacement statistics for each lookup step. */
|
---|
368 | STAMCOUNTER aStatHistoryRecReplaced[16];
|
---|
369 | /** New record statistics for each lookup step. */
|
---|
370 | STAMCOUNTER aStatHistoryRecNew[16];
|
---|
371 |
|
---|
372 | /** Exit records (32KB). (Aligned on 32 byte boundrary.) */
|
---|
373 | EMEXITREC aExitRecords[1024];
|
---|
374 | } EMCPU;
|
---|
375 | /** Pointer to EM VM instance data. */
|
---|
376 | typedef EMCPU *PEMCPU;
|
---|
377 |
|
---|
378 | /** @} */
|
---|
379 |
|
---|
380 | int emR3InitDbg(PVM pVM);
|
---|
381 |
|
---|
382 | int emR3HmExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
383 | VBOXSTRICTRC emR3NemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
384 | int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
385 |
|
---|
386 | EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu);
|
---|
387 | int emR3ForcedActions(PVM pVM, PVMCPU pVCpu, int rc);
|
---|
388 | VBOXSTRICTRC emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, VBOXSTRICTRC rc);
|
---|
389 |
|
---|
390 | int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu);
|
---|
391 | int emR3RawStep(PVM pVM, PVMCPU pVCpu);
|
---|
392 |
|
---|
393 | VBOXSTRICTRC emR3NemSingleInstruction(PVM pVM, PVMCPU pVCpu, uint32_t fFlags);
|
---|
394 |
|
---|
395 | int emR3SingleStepExecRem(PVM pVM, PVMCPU pVCpu, uint32_t cIterations);
|
---|
396 |
|
---|
397 | bool emR3IsExecutionAllowed(PVM pVM, PVMCPU pVCpu);
|
---|
398 |
|
---|
399 | VBOXSTRICTRC emR3ExecutePendingIoPortWrite(PVM pVM, PVMCPU pVCpu);
|
---|
400 | VBOXSTRICTRC emR3ExecutePendingIoPortRead(PVM pVM, PVMCPU pVCpu);
|
---|
401 |
|
---|
402 | RT_C_DECLS_END
|
---|
403 |
|
---|
404 | #endif
|
---|
405 |
|
---|