VirtualBox

source: vbox/trunk/src/VBox/VMM/include/EMInternal.h@ 80014

Last change on this file since 80014 was 80007, checked in by vboxsync, 6 years ago

VMM: Kicking out raw-mode (work in progress). bugref:9517

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

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