1 | /* $Id: EMInternal.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VMM_INCLUDED_SRC_include_EMInternal_h
|
---|
29 | #define VMM_INCLUDED_SRC_include_EMInternal_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/cdefs.h>
|
---|
35 | #include <VBox/types.h>
|
---|
36 | #include <VBox/vmm/em.h>
|
---|
37 | #include <VBox/vmm/stam.h>
|
---|
38 | #include <VBox/dis.h>
|
---|
39 | #include <VBox/vmm/pdmcritsect.h>
|
---|
40 | #include <iprt/avl.h>
|
---|
41 | #include <iprt/setjmp-without-sigmask.h>
|
---|
42 |
|
---|
43 | RT_C_DECLS_BEGIN
|
---|
44 |
|
---|
45 |
|
---|
46 | /** @defgroup grp_em_int Internal
|
---|
47 | * @ingroup grp_em
|
---|
48 | * @internal
|
---|
49 | * @{
|
---|
50 | */
|
---|
51 |
|
---|
52 | /** The saved state version. */
|
---|
53 | #define EM_SAVED_STATE_VERSION 5
|
---|
54 | #define EM_SAVED_STATE_VERSION_PRE_IEM 4
|
---|
55 | #define EM_SAVED_STATE_VERSION_PRE_MWAIT 3
|
---|
56 | #define EM_SAVED_STATE_VERSION_PRE_SMP 2
|
---|
57 |
|
---|
58 |
|
---|
59 | /** @name MWait state flags.
|
---|
60 | * @{
|
---|
61 | */
|
---|
62 | /** MWait activated. */
|
---|
63 | #define EMMWAIT_FLAG_ACTIVE RT_BIT(0)
|
---|
64 | /** MWait will continue when an interrupt is pending even when IF=0. */
|
---|
65 | #define EMMWAIT_FLAG_BREAKIRQIF0 RT_BIT(1)
|
---|
66 | /** Monitor instruction was executed previously. */
|
---|
67 | #define EMMWAIT_FLAG_MONITOR_ACTIVE RT_BIT(2)
|
---|
68 | /** @} */
|
---|
69 |
|
---|
70 | /** EM time slice in ms; used for capping execution time. */
|
---|
71 | #define EM_TIME_SLICE 100
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Cli node structure
|
---|
75 | */
|
---|
76 | typedef struct CLISTAT
|
---|
77 | {
|
---|
78 | /** The key is the cli address. */
|
---|
79 | AVLGCPTRNODECORE Core;
|
---|
80 | #if HC_ARCH_BITS == 32 && !defined(RT_OS_WINDOWS)
|
---|
81 | /** Padding. */
|
---|
82 | uint32_t u32Padding;
|
---|
83 | #endif
|
---|
84 | /** Occurrences. */
|
---|
85 | STAMCOUNTER Counter;
|
---|
86 | } CLISTAT, *PCLISTAT;
|
---|
87 | #ifdef IN_RING3
|
---|
88 | AssertCompileMemberAlignment(CLISTAT, Counter, 8);
|
---|
89 | #endif
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Exit history entry.
|
---|
94 | *
|
---|
95 | * @remarks We could perhaps trim this down a little bit by assuming uFlatPC
|
---|
96 | * only needs 48 bits (currently true but will change) and stuffing
|
---|
97 | * the flags+type in the available 16 bits made available. The
|
---|
98 | * timestamp could likewise be shortened to accomodate the index, or
|
---|
99 | * we might skip the index entirely. However, since we will have to
|
---|
100 | * deal with 56-bit wide PC address before long, there's not point.
|
---|
101 | *
|
---|
102 | * On the upside, there are unused bits in both uFlagsAndType and the
|
---|
103 | * idxSlot fields if needed for anything.
|
---|
104 | */
|
---|
105 | typedef struct EMEXITENTRY
|
---|
106 | {
|
---|
107 | /** The flat PC (CS:EIP/RIP) address of the exit.
|
---|
108 | * UINT64_MAX if not available. */
|
---|
109 | uint64_t uFlatPC;
|
---|
110 | /** The EMEXIT_MAKE_FLAGS_AND_TYPE */
|
---|
111 | uint32_t uFlagsAndType;
|
---|
112 | /** The index into the exit slot hash table.
|
---|
113 | * UINT32_MAX if too many collisions and not entered into it. */
|
---|
114 | uint32_t idxSlot;
|
---|
115 | /** The TSC timestamp of the exit.
|
---|
116 | * This is 0 if not timestamped. */
|
---|
117 | uint64_t uTimestamp;
|
---|
118 | } EMEXITENTRY;
|
---|
119 | /** Pointer to an exit history entry. */
|
---|
120 | typedef EMEXITENTRY *PEMEXITENTRY;
|
---|
121 | /** Pointer to a const exit history entry. */
|
---|
122 | typedef EMEXITENTRY const *PCEMEXITENTRY;
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * EM VM Instance data.
|
---|
127 | */
|
---|
128 | typedef struct EM
|
---|
129 | {
|
---|
130 | /** Whether IEM executes everything. */
|
---|
131 | bool fIemExecutesAll;
|
---|
132 | /** Whether IEM execution (pure) is recompiled (true) or interpreted (false). */
|
---|
133 | bool fIemRecompiled;
|
---|
134 | /** Whether a triple fault triggers a guru. */
|
---|
135 | bool fGuruOnTripleFault;
|
---|
136 | /** Alignment padding. */
|
---|
137 | bool afPadding[5];
|
---|
138 | } EM;
|
---|
139 | /** Pointer to EM VM instance data. */
|
---|
140 | typedef EM *PEM;
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * EM VMCPU Instance data.
|
---|
145 | */
|
---|
146 | typedef struct EMCPU
|
---|
147 | {
|
---|
148 | /** Execution Manager State. */
|
---|
149 | EMSTATE volatile enmState;
|
---|
150 |
|
---|
151 | /** The state prior to the suspending of the VM. */
|
---|
152 | EMSTATE enmPrevState;
|
---|
153 |
|
---|
154 | /** Set if hypercall instruction VMMCALL (AMD) & VMCALL (Intel) are enabled.
|
---|
155 | * GIM sets this and the execution managers queries it. Not saved, as GIM
|
---|
156 | * takes care of that bit too. */
|
---|
157 | bool fHypercallEnabled;
|
---|
158 |
|
---|
159 | /** Explicit padding. */
|
---|
160 | uint8_t abPadding0[7];
|
---|
161 |
|
---|
162 | /** Start of the current time slice in ms (RTTimeMilliTS). */
|
---|
163 | uint64_t msTimeSliceStart;
|
---|
164 | /** The sum of the RTThreadGetExecutionTimeMilli() values at the start of the
|
---|
165 | * current slice. */
|
---|
166 | uint64_t cMsTimeSliceStartExec;
|
---|
167 | /** Number of milliseconds into the current time slice last we checked.
|
---|
168 | * This is in terms of the RTThreadGetExecutionTimeMilli() total, like for
|
---|
169 | * cMsTimeSliceStartExec. */
|
---|
170 | uint64_t cMsTimeSliceExec;
|
---|
171 |
|
---|
172 | /** Pending ring-3 I/O port access (VINF_EM_PENDING_R3_IOPORT_READ / VINF_EM_PENDING_R3_IOPORT_WRITE). */
|
---|
173 | struct
|
---|
174 | {
|
---|
175 | RTIOPORT uPort; /**< The I/O port number.*/
|
---|
176 | uint8_t cbValue; /**< The value size in bytes. Zero when not pending. */
|
---|
177 | uint8_t cbInstr; /**< The instruction length. */
|
---|
178 | uint32_t uValue; /**< The value to write. */
|
---|
179 | } PendingIoPortAccess;
|
---|
180 |
|
---|
181 | /** MWait halt state. */
|
---|
182 | struct
|
---|
183 | {
|
---|
184 | uint32_t fWait; /**< Type of mwait; see EMMWAIT_FLAG_*. */
|
---|
185 | uint32_t u32Padding;
|
---|
186 | RTGCPTR uMWaitRAX; /**< MWAIT hints. */
|
---|
187 | RTGCPTR uMWaitRCX; /**< MWAIT extensions. */
|
---|
188 | RTGCPTR uMonitorRAX; /**< Monitored address. */
|
---|
189 | RTGCPTR uMonitorRCX; /**< Monitor extension. */
|
---|
190 | RTGCPTR uMonitorRDX; /**< Monitor hint. */
|
---|
191 | } MWait;
|
---|
192 |
|
---|
193 | #if 0
|
---|
194 | /** Make sure the jmp_buf is at a 32-byte boundrary. */
|
---|
195 | uint64_t au64Padding1[4];
|
---|
196 | #endif
|
---|
197 | union
|
---|
198 | {
|
---|
199 | /** Padding used in the other rings.
|
---|
200 | * This must be larger than jmp_buf on any supported platform. */
|
---|
201 | #if defined(RT_OS_LINUX) && defined(RT_ARCH_ARM64)
|
---|
202 | char achPaddingFatalLongJump[512];
|
---|
203 | #else
|
---|
204 | char achPaddingFatalLongJump[256];
|
---|
205 | #endif
|
---|
206 | #ifdef IN_RING3
|
---|
207 | /** Long buffer jump for fatal VM errors.
|
---|
208 | * It will jump to before the outer EM loop is entered. */
|
---|
209 | jmp_buf FatalLongJump;
|
---|
210 | #endif
|
---|
211 | } u;
|
---|
212 |
|
---|
213 | /** For saving stack space, the disassembler state is allocated here instead of
|
---|
214 | * on the stack. */
|
---|
215 | DISSTATE Dis;
|
---|
216 |
|
---|
217 | /** @name Execution profiling.
|
---|
218 | * @{ */
|
---|
219 | STAMPROFILE StatForcedActions;
|
---|
220 | STAMPROFILE StatHalted;
|
---|
221 | STAMPROFILEADV StatCapped;
|
---|
222 | STAMPROFILEADV StatHMEntry;
|
---|
223 | STAMPROFILE StatHMExec;
|
---|
224 | STAMPROFILE StatIEMEmu;
|
---|
225 | STAMPROFILE StatIEMThenREM;
|
---|
226 | STAMPROFILEADV StatNEMEntry;
|
---|
227 | STAMPROFILE StatNEMExec;
|
---|
228 | STAMPROFILE StatREMExec;
|
---|
229 | STAMPROFILE StatREMTotal;
|
---|
230 | STAMPROFILEADV StatTotal;
|
---|
231 | /** @} */
|
---|
232 |
|
---|
233 | /** R3: Profiling of emR3RawExecuteIOInstruction. */
|
---|
234 | STAMPROFILE StatIOEmu;
|
---|
235 | STAMCOUNTER StatIoRestarted;
|
---|
236 | STAMCOUNTER StatIoIem;
|
---|
237 | /** R3: Profiling of emR3RawPrivileged. */
|
---|
238 | STAMPROFILE StatPrivEmu;
|
---|
239 | /** R3: Number of times emR3HmExecute is called. */
|
---|
240 | STAMCOUNTER StatHMExecuteCalled;
|
---|
241 | /** R3: Number of times emR3NEMExecute is called. */
|
---|
242 | STAMCOUNTER StatNEMExecuteCalled;
|
---|
243 |
|
---|
244 | /** Align the next member at a 32-byte boundrary. */
|
---|
245 | uint64_t au64Padding2[1+2];
|
---|
246 |
|
---|
247 | /** Exit history table (6KB). */
|
---|
248 | EMEXITENTRY aExitHistory[256];
|
---|
249 | /** Where to store the next exit history entry.
|
---|
250 | * Since aExitHistory is 256 items longs, we'll just increment this and
|
---|
251 | * mask it when using it. That help the readers detect whether we've
|
---|
252 | * wrapped around or not. */
|
---|
253 | uint64_t iNextExit;
|
---|
254 |
|
---|
255 | /** Index into aExitRecords set by EMHistoryExec when returning to ring-3.
|
---|
256 | * This is UINT16_MAX if not armed. */
|
---|
257 | uint16_t volatile idxContinueExitRec;
|
---|
258 | /** Whether exit optimizations are enabled or not (in general). */
|
---|
259 | bool fExitOptimizationEnabled : 1;
|
---|
260 | /** Whether exit optimizations are enabled for ring-0 (in general). */
|
---|
261 | bool fExitOptimizationEnabledR0 : 1;
|
---|
262 | /** Whether exit optimizations are enabled for ring-0 when preemption is disabled. */
|
---|
263 | bool fExitOptimizationEnabledR0PreemptDisabled : 1;
|
---|
264 | /** Explicit padding. */
|
---|
265 | bool fPadding2;
|
---|
266 | /** Max number of instructions to execute. */
|
---|
267 | uint16_t cHistoryExecMaxInstructions;
|
---|
268 | /** Min number of instructions to execute while probing. */
|
---|
269 | uint16_t cHistoryProbeMinInstructions;
|
---|
270 | /** Max number of instructions to execute without an exit before giving up probe. */
|
---|
271 | uint16_t cHistoryProbeMaxInstructionsWithoutExit;
|
---|
272 | uint16_t uPadding3;
|
---|
273 | /** Number of exit records in use. */
|
---|
274 | uint32_t cExitRecordUsed;
|
---|
275 | /** Profiling the EMHistoryExec when executing (not probing). */
|
---|
276 | STAMPROFILE StatHistoryExec;
|
---|
277 | /** Number of saved exits. */
|
---|
278 | STAMCOUNTER StatHistoryExecSavedExits;
|
---|
279 | /** Number of instructions executed by EMHistoryExec. */
|
---|
280 | STAMCOUNTER StatHistoryExecInstructions;
|
---|
281 | uint64_t uPadding4;
|
---|
282 | /** Number of instructions executed by EMHistoryExec when probing. */
|
---|
283 | STAMCOUNTER StatHistoryProbeInstructions;
|
---|
284 | /** Number of times probing resulted in EMEXITACTION_NORMAL_PROBED. */
|
---|
285 | STAMCOUNTER StatHistoryProbedNormal;
|
---|
286 | /** Number of times probing resulted in EMEXITACTION_EXEC_WITH_MAX. */
|
---|
287 | STAMCOUNTER StatHistoryProbedExecWithMax;
|
---|
288 | /** Number of times probing resulted in ring-3 continuation. */
|
---|
289 | STAMCOUNTER StatHistoryProbedToRing3;
|
---|
290 | /** Profiling the EMHistoryExec when probing.*/
|
---|
291 | STAMPROFILE StatHistoryProbe;
|
---|
292 | /** Hit statistics for each lookup step. */
|
---|
293 | STAMCOUNTER aStatHistoryRecHits[16];
|
---|
294 | /** Type change statistics for each lookup step. */
|
---|
295 | STAMCOUNTER aStatHistoryRecTypeChanged[16];
|
---|
296 | /** Replacement statistics for each lookup step. */
|
---|
297 | STAMCOUNTER aStatHistoryRecReplaced[16];
|
---|
298 | /** New record statistics for each lookup step. */
|
---|
299 | STAMCOUNTER aStatHistoryRecNew[16];
|
---|
300 |
|
---|
301 | /** Exit records (32KB). (Aligned on 32 byte boundrary.) */
|
---|
302 | EMEXITREC aExitRecords[1024];
|
---|
303 | } EMCPU;
|
---|
304 | /** Pointer to EM VM instance data. */
|
---|
305 | typedef EMCPU *PEMCPU;
|
---|
306 |
|
---|
307 | /** @} */
|
---|
308 |
|
---|
309 | int emR3InitDbg(PVM pVM);
|
---|
310 |
|
---|
311 | int emR3HmExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
312 | VBOXSTRICTRC emR3NemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
313 | int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
|
---|
314 |
|
---|
315 | EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu);
|
---|
316 | int emR3ForcedActions(PVM pVM, PVMCPU pVCpu, int rc);
|
---|
317 | VBOXSTRICTRC emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, VBOXSTRICTRC rc);
|
---|
318 |
|
---|
319 | int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu);
|
---|
320 | int emR3RawStep(PVM pVM, PVMCPU pVCpu);
|
---|
321 |
|
---|
322 | VBOXSTRICTRC emR3NemSingleInstruction(PVM pVM, PVMCPU pVCpu, uint32_t fFlags);
|
---|
323 |
|
---|
324 | bool emR3IsExecutionAllowedSlow(PVM pVM, PVMCPU pVCpu);
|
---|
325 |
|
---|
326 | VBOXSTRICTRC emR3ExecutePendingIoPortWrite(PVM pVM, PVMCPU pVCpu);
|
---|
327 | VBOXSTRICTRC emR3ExecutePendingIoPortRead(PVM pVM, PVMCPU pVCpu);
|
---|
328 | VBOXSTRICTRC emR3ExecuteSplitLockInstruction(PVM pVM, PVMCPU pVCpu);
|
---|
329 |
|
---|
330 | RT_C_DECLS_END
|
---|
331 |
|
---|
332 | #endif /* !VMM_INCLUDED_SRC_include_EMInternal_h */
|
---|
333 |
|
---|