VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/EM.cpp@ 46474

Last change on this file since 46474 was 46423, checked in by vboxsync, 12 years ago

VMM,Main: Introduced a execute-all-in-IEM debug mode.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 113.5 KB
Line 
1/* $Id: EM.cpp 46423 2013-06-06 19:48:27Z vboxsync $ */
2/** @file
3 * EM - Execution Monitor / Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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/** @page pg_em EM - The Execution Monitor / Manager
19 *
20 * The Execution Monitor/Manager is responsible for running the VM, scheduling
21 * the right kind of execution (Raw-mode, Hardware Assisted, Recompiled or
22 * Interpreted), and keeping the CPU states in sync. The function
23 * EMR3ExecuteVM() is the 'main-loop' of the VM, while each of the execution
24 * modes has different inner loops (emR3RawExecute, emR3HmExecute, and
25 * emR3RemExecute).
26 *
27 * The interpreted execution is only used to avoid switching between
28 * raw-mode/hm and the recompiler when fielding virtualization traps/faults.
29 * The interpretation is thus implemented as part of EM.
30 *
31 * @see grp_em
32 */
33
34/*******************************************************************************
35* Header Files *
36*******************************************************************************/
37#define LOG_GROUP LOG_GROUP_EM
38#include <VBox/vmm/em.h>
39#include <VBox/vmm/vmm.h>
40#include <VBox/vmm/patm.h>
41#include <VBox/vmm/csam.h>
42#include <VBox/vmm/selm.h>
43#include <VBox/vmm/trpm.h>
44#include <VBox/vmm/iem.h>
45#include <VBox/vmm/iom.h>
46#include <VBox/vmm/dbgf.h>
47#include <VBox/vmm/pgm.h>
48#ifdef VBOX_WITH_REM
49# include <VBox/vmm/rem.h>
50#endif
51#include <VBox/vmm/tm.h>
52#include <VBox/vmm/mm.h>
53#include <VBox/vmm/ssm.h>
54#include <VBox/vmm/pdmapi.h>
55#include <VBox/vmm/pdmcritsect.h>
56#include <VBox/vmm/pdmqueue.h>
57#include <VBox/vmm/hm.h>
58#include <VBox/vmm/patm.h>
59#ifdef IEM_VERIFICATION_MODE
60# include <VBox/vmm/iem.h>
61#endif
62#include "EMInternal.h"
63#include <VBox/vmm/vm.h>
64#include <VBox/vmm/uvm.h>
65#include <VBox/vmm/cpumdis.h>
66#include <VBox/dis.h>
67#include <VBox/disopcode.h>
68#include <VBox/vmm/dbgf.h>
69#include "VMMTracing.h"
70
71#include <iprt/asm.h>
72#include <iprt/string.h>
73#include <iprt/stream.h>
74#include <iprt/thread.h>
75
76
77/*******************************************************************************
78* Defined Constants And Macros *
79*******************************************************************************/
80#if 0 /* Disabled till after 2.1.0 when we've time to test it. */
81#define EM_NOTIFY_HM
82#endif
83
84
85/*******************************************************************************
86* Internal Functions *
87*******************************************************************************/
88static DECLCALLBACK(int) emR3Save(PVM pVM, PSSMHANDLE pSSM);
89static DECLCALLBACK(int) emR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
90#if defined(LOG_ENABLED) || defined(VBOX_STRICT)
91static const char *emR3GetStateName(EMSTATE enmState);
92#endif
93static int emR3Debug(PVM pVM, PVMCPU pVCpu, int rc);
94static int emR3RemStep(PVM pVM, PVMCPU pVCpu);
95static int emR3RemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
96int emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, int rc);
97
98
99/**
100 * Initializes the EM.
101 *
102 * @returns VBox status code.
103 * @param pVM Pointer to the VM.
104 */
105VMMR3_INT_DECL(int) EMR3Init(PVM pVM)
106{
107 LogFlow(("EMR3Init\n"));
108 /*
109 * Assert alignment and sizes.
110 */
111 AssertCompileMemberAlignment(VM, em.s, 32);
112 AssertCompile(sizeof(pVM->em.s) <= sizeof(pVM->em.padding));
113 AssertCompile(sizeof(pVM->aCpus[0].em.s.u.FatalLongJump) <= sizeof(pVM->aCpus[0].em.s.u.achPaddingFatalLongJump));
114
115 /*
116 * Init the structure.
117 */
118 pVM->em.s.offVM = RT_OFFSETOF(VM, em.s);
119 PCFGMNODE pCfgRoot = CFGMR3GetRoot(pVM);
120 PCFGMNODE pCfgEM = CFGMR3GetChild(pCfgRoot, "EM");
121
122 bool fEnabled;
123 int rc = CFGMR3QueryBoolDef(pCfgRoot, "RawR3Enabled", &fEnabled, true);
124 AssertLogRelRCReturn(rc, rc);
125 pVM->fRecompileUser = !fEnabled;
126
127 rc = CFGMR3QueryBoolDef(pCfgRoot, "RawR0Enabled", &fEnabled, true);
128 AssertLogRelRCReturn(rc, rc);
129 pVM->fRecompileSupervisor = !fEnabled;
130
131#ifdef VBOX_WITH_RAW_RING1
132 rc = CFGMR3QueryBoolDef(pCfgRoot, "RawR1Enabled", &pVM->fRawRing1Enabled, false);
133 AssertLogRelRCReturn(rc, rc);
134#else
135 pVM->fRawRing1Enabled = false; /* Disabled by default. */
136#endif
137
138 rc = CFGMR3QueryBoolDef(pCfgEM, "IemExecutesAll", &pVM->em.s.fIemExecutesAll, false);
139 AssertLogRelRCReturn(rc, rc);
140
141 Log(("EMR3Init: fRecompileUser=%RTbool fRecompileSupervisor=%RTbool fRawRing1Enabled=%RTbool fIemExecutesAll=%RTbool\n",
142 pVM->fRecompileUser, pVM->fRecompileSupervisor, pVM->fRawRing1Enabled, pVM->em.s.fIemExecutesAll));
143
144#ifdef VBOX_WITH_REM
145 /*
146 * Initialize the REM critical section.
147 */
148 AssertCompileMemberAlignment(EM, CritSectREM, sizeof(uintptr_t));
149 rc = PDMR3CritSectInit(pVM, &pVM->em.s.CritSectREM, RT_SRC_POS, "EM-REM");
150 AssertRCReturn(rc, rc);
151#endif
152
153 /*
154 * Saved state.
155 */
156 rc = SSMR3RegisterInternal(pVM, "em", 0, EM_SAVED_STATE_VERSION, 16,
157 NULL, NULL, NULL,
158 NULL, emR3Save, NULL,
159 NULL, emR3Load, NULL);
160 if (RT_FAILURE(rc))
161 return rc;
162
163 for (VMCPUID i = 0; i < pVM->cCpus; i++)
164 {
165 PVMCPU pVCpu = &pVM->aCpus[i];
166
167 pVCpu->em.s.offVMCPU = RT_OFFSETOF(VMCPU, em.s);
168
169 pVCpu->em.s.enmState = (i == 0) ? EMSTATE_NONE : EMSTATE_WAIT_SIPI;
170 pVCpu->em.s.enmPrevState = EMSTATE_NONE;
171 pVCpu->em.s.fForceRAW = false;
172
173 pVCpu->em.s.pCtx = CPUMQueryGuestCtxPtr(pVCpu);
174#ifdef VBOX_WITH_RAW_MODE
175 if (!HMIsEnabled(pVM))
176 {
177 pVCpu->em.s.pPatmGCState = PATMR3QueryGCStateHC(pVM);
178 AssertMsg(pVCpu->em.s.pPatmGCState, ("PATMR3QueryGCStateHC failed!\n"));
179 }
180#endif
181
182 /* Force reset of the time slice. */
183 pVCpu->em.s.u64TimeSliceStart = 0;
184
185# define EM_REG_COUNTER(a, b, c) \
186 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, c, b, i); \
187 AssertRC(rc);
188
189# define EM_REG_COUNTER_USED(a, b, c) \
190 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, c, b, i); \
191 AssertRC(rc);
192
193# define EM_REG_PROFILE(a, b, c) \
194 rc = STAMR3RegisterF(pVM, a, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, c, b, i); \
195 AssertRC(rc);
196
197# define EM_REG_PROFILE_ADV(a, b, c) \
198 rc = STAMR3RegisterF(pVM, a, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, c, b, i); \
199 AssertRC(rc);
200
201 /*
202 * Statistics.
203 */
204#ifdef VBOX_WITH_STATISTICS
205 PEMSTATS pStats;
206 rc = MMHyperAlloc(pVM, sizeof(*pStats), 0, MM_TAG_EM, (void **)&pStats);
207 if (RT_FAILURE(rc))
208 return rc;
209
210 pVCpu->em.s.pStatsR3 = pStats;
211 pVCpu->em.s.pStatsR0 = MMHyperR3ToR0(pVM, pStats);
212 pVCpu->em.s.pStatsRC = MMHyperR3ToRC(pVM, pStats);
213
214 EM_REG_PROFILE(&pStats->StatRZEmulate, "/EM/CPU%d/RZ/Interpret", "Profiling of EMInterpretInstruction.");
215 EM_REG_PROFILE(&pStats->StatR3Emulate, "/EM/CPU%d/R3/Interpret", "Profiling of EMInterpretInstruction.");
216
217 EM_REG_PROFILE(&pStats->StatRZInterpretSucceeded, "/EM/CPU%d/RZ/Interpret/Success", "The number of times an instruction was successfully interpreted.");
218 EM_REG_PROFILE(&pStats->StatR3InterpretSucceeded, "/EM/CPU%d/R3/Interpret/Success", "The number of times an instruction was successfully interpreted.");
219
220 EM_REG_COUNTER_USED(&pStats->StatRZAnd, "/EM/CPU%d/RZ/Interpret/Success/And", "The number of times AND was successfully interpreted.");
221 EM_REG_COUNTER_USED(&pStats->StatR3And, "/EM/CPU%d/R3/Interpret/Success/And", "The number of times AND was successfully interpreted.");
222 EM_REG_COUNTER_USED(&pStats->StatRZAdd, "/EM/CPU%d/RZ/Interpret/Success/Add", "The number of times ADD was successfully interpreted.");
223 EM_REG_COUNTER_USED(&pStats->StatR3Add, "/EM/CPU%d/R3/Interpret/Success/Add", "The number of times ADD was successfully interpreted.");
224 EM_REG_COUNTER_USED(&pStats->StatRZAdc, "/EM/CPU%d/RZ/Interpret/Success/Adc", "The number of times ADC was successfully interpreted.");
225 EM_REG_COUNTER_USED(&pStats->StatR3Adc, "/EM/CPU%d/R3/Interpret/Success/Adc", "The number of times ADC was successfully interpreted.");
226 EM_REG_COUNTER_USED(&pStats->StatRZSub, "/EM/CPU%d/RZ/Interpret/Success/Sub", "The number of times SUB was successfully interpreted.");
227 EM_REG_COUNTER_USED(&pStats->StatR3Sub, "/EM/CPU%d/R3/Interpret/Success/Sub", "The number of times SUB was successfully interpreted.");
228 EM_REG_COUNTER_USED(&pStats->StatRZCpuId, "/EM/CPU%d/RZ/Interpret/Success/CpuId", "The number of times CPUID was successfully interpreted.");
229 EM_REG_COUNTER_USED(&pStats->StatR3CpuId, "/EM/CPU%d/R3/Interpret/Success/CpuId", "The number of times CPUID was successfully interpreted.");
230 EM_REG_COUNTER_USED(&pStats->StatRZDec, "/EM/CPU%d/RZ/Interpret/Success/Dec", "The number of times DEC was successfully interpreted.");
231 EM_REG_COUNTER_USED(&pStats->StatR3Dec, "/EM/CPU%d/R3/Interpret/Success/Dec", "The number of times DEC was successfully interpreted.");
232 EM_REG_COUNTER_USED(&pStats->StatRZHlt, "/EM/CPU%d/RZ/Interpret/Success/Hlt", "The number of times HLT was successfully interpreted.");
233 EM_REG_COUNTER_USED(&pStats->StatR3Hlt, "/EM/CPU%d/R3/Interpret/Success/Hlt", "The number of times HLT was successfully interpreted.");
234 EM_REG_COUNTER_USED(&pStats->StatRZInc, "/EM/CPU%d/RZ/Interpret/Success/Inc", "The number of times INC was successfully interpreted.");
235 EM_REG_COUNTER_USED(&pStats->StatR3Inc, "/EM/CPU%d/R3/Interpret/Success/Inc", "The number of times INC was successfully interpreted.");
236 EM_REG_COUNTER_USED(&pStats->StatRZInvlPg, "/EM/CPU%d/RZ/Interpret/Success/Invlpg", "The number of times INVLPG was successfully interpreted.");
237 EM_REG_COUNTER_USED(&pStats->StatR3InvlPg, "/EM/CPU%d/R3/Interpret/Success/Invlpg", "The number of times INVLPG was successfully interpreted.");
238 EM_REG_COUNTER_USED(&pStats->StatRZIret, "/EM/CPU%d/RZ/Interpret/Success/Iret", "The number of times IRET was successfully interpreted.");
239 EM_REG_COUNTER_USED(&pStats->StatR3Iret, "/EM/CPU%d/R3/Interpret/Success/Iret", "The number of times IRET was successfully interpreted.");
240 EM_REG_COUNTER_USED(&pStats->StatRZLLdt, "/EM/CPU%d/RZ/Interpret/Success/LLdt", "The number of times LLDT was successfully interpreted.");
241 EM_REG_COUNTER_USED(&pStats->StatR3LLdt, "/EM/CPU%d/R3/Interpret/Success/LLdt", "The number of times LLDT was successfully interpreted.");
242 EM_REG_COUNTER_USED(&pStats->StatRZLIdt, "/EM/CPU%d/RZ/Interpret/Success/LIdt", "The number of times LIDT was successfully interpreted.");
243 EM_REG_COUNTER_USED(&pStats->StatR3LIdt, "/EM/CPU%d/R3/Interpret/Success/LIdt", "The number of times LIDT was successfully interpreted.");
244 EM_REG_COUNTER_USED(&pStats->StatRZLGdt, "/EM/CPU%d/RZ/Interpret/Success/LGdt", "The number of times LGDT was successfully interpreted.");
245 EM_REG_COUNTER_USED(&pStats->StatR3LGdt, "/EM/CPU%d/R3/Interpret/Success/LGdt", "The number of times LGDT was successfully interpreted.");
246 EM_REG_COUNTER_USED(&pStats->StatRZMov, "/EM/CPU%d/RZ/Interpret/Success/Mov", "The number of times MOV was successfully interpreted.");
247 EM_REG_COUNTER_USED(&pStats->StatR3Mov, "/EM/CPU%d/R3/Interpret/Success/Mov", "The number of times MOV was successfully interpreted.");
248 EM_REG_COUNTER_USED(&pStats->StatRZMovCRx, "/EM/CPU%d/RZ/Interpret/Success/MovCRx", "The number of times MOV CRx was successfully interpreted.");
249 EM_REG_COUNTER_USED(&pStats->StatR3MovCRx, "/EM/CPU%d/R3/Interpret/Success/MovCRx", "The number of times MOV CRx was successfully interpreted.");
250 EM_REG_COUNTER_USED(&pStats->StatRZMovDRx, "/EM/CPU%d/RZ/Interpret/Success/MovDRx", "The number of times MOV DRx was successfully interpreted.");
251 EM_REG_COUNTER_USED(&pStats->StatR3MovDRx, "/EM/CPU%d/R3/Interpret/Success/MovDRx", "The number of times MOV DRx was successfully interpreted.");
252 EM_REG_COUNTER_USED(&pStats->StatRZOr, "/EM/CPU%d/RZ/Interpret/Success/Or", "The number of times OR was successfully interpreted.");
253 EM_REG_COUNTER_USED(&pStats->StatR3Or, "/EM/CPU%d/R3/Interpret/Success/Or", "The number of times OR was successfully interpreted.");
254 EM_REG_COUNTER_USED(&pStats->StatRZPop, "/EM/CPU%d/RZ/Interpret/Success/Pop", "The number of times POP was successfully interpreted.");
255 EM_REG_COUNTER_USED(&pStats->StatR3Pop, "/EM/CPU%d/R3/Interpret/Success/Pop", "The number of times POP was successfully interpreted.");
256 EM_REG_COUNTER_USED(&pStats->StatRZRdtsc, "/EM/CPU%d/RZ/Interpret/Success/Rdtsc", "The number of times RDTSC was successfully interpreted.");
257 EM_REG_COUNTER_USED(&pStats->StatR3Rdtsc, "/EM/CPU%d/R3/Interpret/Success/Rdtsc", "The number of times RDTSC was successfully interpreted.");
258 EM_REG_COUNTER_USED(&pStats->StatRZRdpmc, "/EM/CPU%d/RZ/Interpret/Success/Rdpmc", "The number of times RDPMC was successfully interpreted.");
259 EM_REG_COUNTER_USED(&pStats->StatR3Rdpmc, "/EM/CPU%d/R3/Interpret/Success/Rdpmc", "The number of times RDPMC was successfully interpreted.");
260 EM_REG_COUNTER_USED(&pStats->StatRZSti, "/EM/CPU%d/RZ/Interpret/Success/Sti", "The number of times STI was successfully interpreted.");
261 EM_REG_COUNTER_USED(&pStats->StatR3Sti, "/EM/CPU%d/R3/Interpret/Success/Sti", "The number of times STI was successfully interpreted.");
262 EM_REG_COUNTER_USED(&pStats->StatRZXchg, "/EM/CPU%d/RZ/Interpret/Success/Xchg", "The number of times XCHG was successfully interpreted.");
263 EM_REG_COUNTER_USED(&pStats->StatR3Xchg, "/EM/CPU%d/R3/Interpret/Success/Xchg", "The number of times XCHG was successfully interpreted.");
264 EM_REG_COUNTER_USED(&pStats->StatRZXor, "/EM/CPU%d/RZ/Interpret/Success/Xor", "The number of times XOR was successfully interpreted.");
265 EM_REG_COUNTER_USED(&pStats->StatR3Xor, "/EM/CPU%d/R3/Interpret/Success/Xor", "The number of times XOR was successfully interpreted.");
266 EM_REG_COUNTER_USED(&pStats->StatRZMonitor, "/EM/CPU%d/RZ/Interpret/Success/Monitor", "The number of times MONITOR was successfully interpreted.");
267 EM_REG_COUNTER_USED(&pStats->StatR3Monitor, "/EM/CPU%d/R3/Interpret/Success/Monitor", "The number of times MONITOR was successfully interpreted.");
268 EM_REG_COUNTER_USED(&pStats->StatRZMWait, "/EM/CPU%d/RZ/Interpret/Success/MWait", "The number of times MWAIT was successfully interpreted.");
269 EM_REG_COUNTER_USED(&pStats->StatR3MWait, "/EM/CPU%d/R3/Interpret/Success/MWait", "The number of times MWAIT was successfully interpreted.");
270 EM_REG_COUNTER_USED(&pStats->StatRZBtr, "/EM/CPU%d/RZ/Interpret/Success/Btr", "The number of times BTR was successfully interpreted.");
271 EM_REG_COUNTER_USED(&pStats->StatR3Btr, "/EM/CPU%d/R3/Interpret/Success/Btr", "The number of times BTR was successfully interpreted.");
272 EM_REG_COUNTER_USED(&pStats->StatRZBts, "/EM/CPU%d/RZ/Interpret/Success/Bts", "The number of times BTS was successfully interpreted.");
273 EM_REG_COUNTER_USED(&pStats->StatR3Bts, "/EM/CPU%d/R3/Interpret/Success/Bts", "The number of times BTS was successfully interpreted.");
274 EM_REG_COUNTER_USED(&pStats->StatRZBtc, "/EM/CPU%d/RZ/Interpret/Success/Btc", "The number of times BTC was successfully interpreted.");
275 EM_REG_COUNTER_USED(&pStats->StatR3Btc, "/EM/CPU%d/R3/Interpret/Success/Btc", "The number of times BTC was successfully interpreted.");
276 EM_REG_COUNTER_USED(&pStats->StatRZCmpXchg, "/EM/CPU%d/RZ/Interpret/Success/CmpXchg", "The number of times CMPXCHG was successfully interpreted.");
277 EM_REG_COUNTER_USED(&pStats->StatR3CmpXchg, "/EM/CPU%d/R3/Interpret/Success/CmpXchg", "The number of times CMPXCHG was successfully interpreted.");
278 EM_REG_COUNTER_USED(&pStats->StatRZCmpXchg8b, "/EM/CPU%d/RZ/Interpret/Success/CmpXchg8b", "The number of times CMPXCHG8B was successfully interpreted.");
279 EM_REG_COUNTER_USED(&pStats->StatR3CmpXchg8b, "/EM/CPU%d/R3/Interpret/Success/CmpXchg8b", "The number of times CMPXCHG8B was successfully interpreted.");
280 EM_REG_COUNTER_USED(&pStats->StatRZXAdd, "/EM/CPU%d/RZ/Interpret/Success/XAdd", "The number of times XADD was successfully interpreted.");
281 EM_REG_COUNTER_USED(&pStats->StatR3XAdd, "/EM/CPU%d/R3/Interpret/Success/XAdd", "The number of times XADD was successfully interpreted.");
282 EM_REG_COUNTER_USED(&pStats->StatR3Rdmsr, "/EM/CPU%d/R3/Interpret/Success/Rdmsr", "The number of times RDMSR was successfully interpreted.");
283 EM_REG_COUNTER_USED(&pStats->StatRZRdmsr, "/EM/CPU%d/RZ/Interpret/Success/Rdmsr", "The number of times RDMSR was successfully interpreted.");
284 EM_REG_COUNTER_USED(&pStats->StatR3Wrmsr, "/EM/CPU%d/R3/Interpret/Success/Wrmsr", "The number of times WRMSR was successfully interpreted.");
285 EM_REG_COUNTER_USED(&pStats->StatRZWrmsr, "/EM/CPU%d/RZ/Interpret/Success/Wrmsr", "The number of times WRMSR was successfully interpreted.");
286 EM_REG_COUNTER_USED(&pStats->StatR3StosWD, "/EM/CPU%d/R3/Interpret/Success/Stoswd", "The number of times STOSWD was successfully interpreted.");
287 EM_REG_COUNTER_USED(&pStats->StatRZStosWD, "/EM/CPU%d/RZ/Interpret/Success/Stoswd", "The number of times STOSWD was successfully interpreted.");
288 EM_REG_COUNTER_USED(&pStats->StatRZWbInvd, "/EM/CPU%d/RZ/Interpret/Success/WbInvd", "The number of times WBINVD was successfully interpreted.");
289 EM_REG_COUNTER_USED(&pStats->StatR3WbInvd, "/EM/CPU%d/R3/Interpret/Success/WbInvd", "The number of times WBINVD was successfully interpreted.");
290 EM_REG_COUNTER_USED(&pStats->StatRZLmsw, "/EM/CPU%d/RZ/Interpret/Success/Lmsw", "The number of times LMSW was successfully interpreted.");
291 EM_REG_COUNTER_USED(&pStats->StatR3Lmsw, "/EM/CPU%d/R3/Interpret/Success/Lmsw", "The number of times LMSW was successfully interpreted.");
292 EM_REG_COUNTER_USED(&pStats->StatRZSmsw, "/EM/CPU%d/RZ/Interpret/Success/Smsw", "The number of times SMSW was successfully interpreted.");
293 EM_REG_COUNTER_USED(&pStats->StatR3Smsw, "/EM/CPU%d/R3/Interpret/Success/Smsw", "The number of times SMSW was successfully interpreted.");
294
295 EM_REG_COUNTER(&pStats->StatRZInterpretFailed, "/EM/CPU%d/RZ/Interpret/Failed", "The number of times an instruction was not interpreted.");
296 EM_REG_COUNTER(&pStats->StatR3InterpretFailed, "/EM/CPU%d/R3/Interpret/Failed", "The number of times an instruction was not interpreted.");
297
298 EM_REG_COUNTER_USED(&pStats->StatRZFailedAnd, "/EM/CPU%d/RZ/Interpret/Failed/And", "The number of times AND was not interpreted.");
299 EM_REG_COUNTER_USED(&pStats->StatR3FailedAnd, "/EM/CPU%d/R3/Interpret/Failed/And", "The number of times AND was not interpreted.");
300 EM_REG_COUNTER_USED(&pStats->StatRZFailedCpuId, "/EM/CPU%d/RZ/Interpret/Failed/CpuId", "The number of times CPUID was not interpreted.");
301 EM_REG_COUNTER_USED(&pStats->StatR3FailedCpuId, "/EM/CPU%d/R3/Interpret/Failed/CpuId", "The number of times CPUID was not interpreted.");
302 EM_REG_COUNTER_USED(&pStats->StatRZFailedDec, "/EM/CPU%d/RZ/Interpret/Failed/Dec", "The number of times DEC was not interpreted.");
303 EM_REG_COUNTER_USED(&pStats->StatR3FailedDec, "/EM/CPU%d/R3/Interpret/Failed/Dec", "The number of times DEC was not interpreted.");
304 EM_REG_COUNTER_USED(&pStats->StatRZFailedHlt, "/EM/CPU%d/RZ/Interpret/Failed/Hlt", "The number of times HLT was not interpreted.");
305 EM_REG_COUNTER_USED(&pStats->StatR3FailedHlt, "/EM/CPU%d/R3/Interpret/Failed/Hlt", "The number of times HLT was not interpreted.");
306 EM_REG_COUNTER_USED(&pStats->StatRZFailedInc, "/EM/CPU%d/RZ/Interpret/Failed/Inc", "The number of times INC was not interpreted.");
307 EM_REG_COUNTER_USED(&pStats->StatR3FailedInc, "/EM/CPU%d/R3/Interpret/Failed/Inc", "The number of times INC was not interpreted.");
308 EM_REG_COUNTER_USED(&pStats->StatRZFailedInvlPg, "/EM/CPU%d/RZ/Interpret/Failed/InvlPg", "The number of times INVLPG was not interpreted.");
309 EM_REG_COUNTER_USED(&pStats->StatR3FailedInvlPg, "/EM/CPU%d/R3/Interpret/Failed/InvlPg", "The number of times INVLPG was not interpreted.");
310 EM_REG_COUNTER_USED(&pStats->StatRZFailedIret, "/EM/CPU%d/RZ/Interpret/Failed/Iret", "The number of times IRET was not interpreted.");
311 EM_REG_COUNTER_USED(&pStats->StatR3FailedIret, "/EM/CPU%d/R3/Interpret/Failed/Iret", "The number of times IRET was not interpreted.");
312 EM_REG_COUNTER_USED(&pStats->StatRZFailedLLdt, "/EM/CPU%d/RZ/Interpret/Failed/LLdt", "The number of times LLDT was not interpreted.");
313 EM_REG_COUNTER_USED(&pStats->StatR3FailedLLdt, "/EM/CPU%d/R3/Interpret/Failed/LLdt", "The number of times LLDT was not interpreted.");
314 EM_REG_COUNTER_USED(&pStats->StatRZFailedLIdt, "/EM/CPU%d/RZ/Interpret/Failed/LIdt", "The number of times LIDT was not interpreted.");
315 EM_REG_COUNTER_USED(&pStats->StatR3FailedLIdt, "/EM/CPU%d/R3/Interpret/Failed/LIdt", "The number of times LIDT was not interpreted.");
316 EM_REG_COUNTER_USED(&pStats->StatRZFailedLGdt, "/EM/CPU%d/RZ/Interpret/Failed/LGdt", "The number of times LGDT was not interpreted.");
317 EM_REG_COUNTER_USED(&pStats->StatR3FailedLGdt, "/EM/CPU%d/R3/Interpret/Failed/LGdt", "The number of times LGDT was not interpreted.");
318 EM_REG_COUNTER_USED(&pStats->StatRZFailedMov, "/EM/CPU%d/RZ/Interpret/Failed/Mov", "The number of times MOV was not interpreted.");
319 EM_REG_COUNTER_USED(&pStats->StatR3FailedMov, "/EM/CPU%d/R3/Interpret/Failed/Mov", "The number of times MOV was not interpreted.");
320 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovCRx, "/EM/CPU%d/RZ/Interpret/Failed/MovCRx", "The number of times MOV CRx was not interpreted.");
321 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovCRx, "/EM/CPU%d/R3/Interpret/Failed/MovCRx", "The number of times MOV CRx was not interpreted.");
322 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovDRx, "/EM/CPU%d/RZ/Interpret/Failed/MovDRx", "The number of times MOV DRx was not interpreted.");
323 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovDRx, "/EM/CPU%d/R3/Interpret/Failed/MovDRx", "The number of times MOV DRx was not interpreted.");
324 EM_REG_COUNTER_USED(&pStats->StatRZFailedOr, "/EM/CPU%d/RZ/Interpret/Failed/Or", "The number of times OR was not interpreted.");
325 EM_REG_COUNTER_USED(&pStats->StatR3FailedOr, "/EM/CPU%d/R3/Interpret/Failed/Or", "The number of times OR was not interpreted.");
326 EM_REG_COUNTER_USED(&pStats->StatRZFailedPop, "/EM/CPU%d/RZ/Interpret/Failed/Pop", "The number of times POP was not interpreted.");
327 EM_REG_COUNTER_USED(&pStats->StatR3FailedPop, "/EM/CPU%d/R3/Interpret/Failed/Pop", "The number of times POP was not interpreted.");
328 EM_REG_COUNTER_USED(&pStats->StatRZFailedSti, "/EM/CPU%d/RZ/Interpret/Failed/Sti", "The number of times STI was not interpreted.");
329 EM_REG_COUNTER_USED(&pStats->StatR3FailedSti, "/EM/CPU%d/R3/Interpret/Failed/Sti", "The number of times STI was not interpreted.");
330 EM_REG_COUNTER_USED(&pStats->StatRZFailedXchg, "/EM/CPU%d/RZ/Interpret/Failed/Xchg", "The number of times XCHG was not interpreted.");
331 EM_REG_COUNTER_USED(&pStats->StatR3FailedXchg, "/EM/CPU%d/R3/Interpret/Failed/Xchg", "The number of times XCHG was not interpreted.");
332 EM_REG_COUNTER_USED(&pStats->StatRZFailedXor, "/EM/CPU%d/RZ/Interpret/Failed/Xor", "The number of times XOR was not interpreted.");
333 EM_REG_COUNTER_USED(&pStats->StatR3FailedXor, "/EM/CPU%d/R3/Interpret/Failed/Xor", "The number of times XOR was not interpreted.");
334 EM_REG_COUNTER_USED(&pStats->StatRZFailedMonitor, "/EM/CPU%d/RZ/Interpret/Failed/Monitor", "The number of times MONITOR was not interpreted.");
335 EM_REG_COUNTER_USED(&pStats->StatR3FailedMonitor, "/EM/CPU%d/R3/Interpret/Failed/Monitor", "The number of times MONITOR was not interpreted.");
336 EM_REG_COUNTER_USED(&pStats->StatRZFailedMWait, "/EM/CPU%d/RZ/Interpret/Failed/MWait", "The number of times MWAIT was not interpreted.");
337 EM_REG_COUNTER_USED(&pStats->StatR3FailedMWait, "/EM/CPU%d/R3/Interpret/Failed/MWait", "The number of times MWAIT was not interpreted.");
338 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdtsc, "/EM/CPU%d/RZ/Interpret/Failed/Rdtsc", "The number of times RDTSC was not interpreted.");
339 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdtsc, "/EM/CPU%d/R3/Interpret/Failed/Rdtsc", "The number of times RDTSC was not interpreted.");
340 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdpmc, "/EM/CPU%d/RZ/Interpret/Failed/Rdpmc", "The number of times RDPMC was not interpreted.");
341 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdpmc, "/EM/CPU%d/R3/Interpret/Failed/Rdpmc", "The number of times RDPMC was not interpreted.");
342 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdmsr, "/EM/CPU%d/RZ/Interpret/Failed/Rdmsr", "The number of times RDMSR was not interpreted.");
343 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdmsr, "/EM/CPU%d/R3/Interpret/Failed/Rdmsr", "The number of times RDMSR was not interpreted.");
344 EM_REG_COUNTER_USED(&pStats->StatRZFailedWrmsr, "/EM/CPU%d/RZ/Interpret/Failed/Wrmsr", "The number of times WRMSR was not interpreted.");
345 EM_REG_COUNTER_USED(&pStats->StatR3FailedWrmsr, "/EM/CPU%d/R3/Interpret/Failed/Wrmsr", "The number of times WRMSR was not interpreted.");
346 EM_REG_COUNTER_USED(&pStats->StatRZFailedLmsw, "/EM/CPU%d/RZ/Interpret/Failed/Lmsw", "The number of times LMSW was not interpreted.");
347 EM_REG_COUNTER_USED(&pStats->StatR3FailedLmsw, "/EM/CPU%d/R3/Interpret/Failed/Lmsw", "The number of times LMSW was not interpreted.");
348 EM_REG_COUNTER_USED(&pStats->StatRZFailedSmsw, "/EM/CPU%d/RZ/Interpret/Failed/Smsw", "The number of times SMSW was not interpreted.");
349 EM_REG_COUNTER_USED(&pStats->StatR3FailedSmsw, "/EM/CPU%d/R3/Interpret/Failed/Smsw", "The number of times SMSW was not interpreted.");
350
351 EM_REG_COUNTER_USED(&pStats->StatRZFailedMisc, "/EM/CPU%d/RZ/Interpret/Failed/Misc", "The number of times some misc instruction was encountered.");
352 EM_REG_COUNTER_USED(&pStats->StatR3FailedMisc, "/EM/CPU%d/R3/Interpret/Failed/Misc", "The number of times some misc instruction was encountered.");
353 EM_REG_COUNTER_USED(&pStats->StatRZFailedAdd, "/EM/CPU%d/RZ/Interpret/Failed/Add", "The number of times ADD was not interpreted.");
354 EM_REG_COUNTER_USED(&pStats->StatR3FailedAdd, "/EM/CPU%d/R3/Interpret/Failed/Add", "The number of times ADD was not interpreted.");
355 EM_REG_COUNTER_USED(&pStats->StatRZFailedAdc, "/EM/CPU%d/RZ/Interpret/Failed/Adc", "The number of times ADC was not interpreted.");
356 EM_REG_COUNTER_USED(&pStats->StatR3FailedAdc, "/EM/CPU%d/R3/Interpret/Failed/Adc", "The number of times ADC was not interpreted.");
357 EM_REG_COUNTER_USED(&pStats->StatRZFailedBtr, "/EM/CPU%d/RZ/Interpret/Failed/Btr", "The number of times BTR was not interpreted.");
358 EM_REG_COUNTER_USED(&pStats->StatR3FailedBtr, "/EM/CPU%d/R3/Interpret/Failed/Btr", "The number of times BTR was not interpreted.");
359 EM_REG_COUNTER_USED(&pStats->StatRZFailedBts, "/EM/CPU%d/RZ/Interpret/Failed/Bts", "The number of times BTS was not interpreted.");
360 EM_REG_COUNTER_USED(&pStats->StatR3FailedBts, "/EM/CPU%d/R3/Interpret/Failed/Bts", "The number of times BTS was not interpreted.");
361 EM_REG_COUNTER_USED(&pStats->StatRZFailedBtc, "/EM/CPU%d/RZ/Interpret/Failed/Btc", "The number of times BTC was not interpreted.");
362 EM_REG_COUNTER_USED(&pStats->StatR3FailedBtc, "/EM/CPU%d/R3/Interpret/Failed/Btc", "The number of times BTC was not interpreted.");
363 EM_REG_COUNTER_USED(&pStats->StatRZFailedCli, "/EM/CPU%d/RZ/Interpret/Failed/Cli", "The number of times CLI was not interpreted.");
364 EM_REG_COUNTER_USED(&pStats->StatR3FailedCli, "/EM/CPU%d/R3/Interpret/Failed/Cli", "The number of times CLI was not interpreted.");
365 EM_REG_COUNTER_USED(&pStats->StatRZFailedCmpXchg, "/EM/CPU%d/RZ/Interpret/Failed/CmpXchg", "The number of times CMPXCHG was not interpreted.");
366 EM_REG_COUNTER_USED(&pStats->StatR3FailedCmpXchg, "/EM/CPU%d/R3/Interpret/Failed/CmpXchg", "The number of times CMPXCHG was not interpreted.");
367 EM_REG_COUNTER_USED(&pStats->StatRZFailedCmpXchg8b, "/EM/CPU%d/RZ/Interpret/Failed/CmpXchg8b", "The number of times CMPXCHG8B was not interpreted.");
368 EM_REG_COUNTER_USED(&pStats->StatR3FailedCmpXchg8b, "/EM/CPU%d/R3/Interpret/Failed/CmpXchg8b", "The number of times CMPXCHG8B was not interpreted.");
369 EM_REG_COUNTER_USED(&pStats->StatRZFailedXAdd, "/EM/CPU%d/RZ/Interpret/Failed/XAdd", "The number of times XADD was not interpreted.");
370 EM_REG_COUNTER_USED(&pStats->StatR3FailedXAdd, "/EM/CPU%d/R3/Interpret/Failed/XAdd", "The number of times XADD was not interpreted.");
371 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovNTPS, "/EM/CPU%d/RZ/Interpret/Failed/MovNTPS", "The number of times MOVNTPS was not interpreted.");
372 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovNTPS, "/EM/CPU%d/R3/Interpret/Failed/MovNTPS", "The number of times MOVNTPS was not interpreted.");
373 EM_REG_COUNTER_USED(&pStats->StatRZFailedStosWD, "/EM/CPU%d/RZ/Interpret/Failed/StosWD", "The number of times STOSWD was not interpreted.");
374 EM_REG_COUNTER_USED(&pStats->StatR3FailedStosWD, "/EM/CPU%d/R3/Interpret/Failed/StosWD", "The number of times STOSWD was not interpreted.");
375 EM_REG_COUNTER_USED(&pStats->StatRZFailedSub, "/EM/CPU%d/RZ/Interpret/Failed/Sub", "The number of times SUB was not interpreted.");
376 EM_REG_COUNTER_USED(&pStats->StatR3FailedSub, "/EM/CPU%d/R3/Interpret/Failed/Sub", "The number of times SUB was not interpreted.");
377 EM_REG_COUNTER_USED(&pStats->StatRZFailedWbInvd, "/EM/CPU%d/RZ/Interpret/Failed/WbInvd", "The number of times WBINVD was not interpreted.");
378 EM_REG_COUNTER_USED(&pStats->StatR3FailedWbInvd, "/EM/CPU%d/R3/Interpret/Failed/WbInvd", "The number of times WBINVD was not interpreted.");
379
380 EM_REG_COUNTER_USED(&pStats->StatRZFailedUserMode, "/EM/CPU%d/RZ/Interpret/Failed/UserMode", "The number of rejections because of CPL.");
381 EM_REG_COUNTER_USED(&pStats->StatR3FailedUserMode, "/EM/CPU%d/R3/Interpret/Failed/UserMode", "The number of rejections because of CPL.");
382 EM_REG_COUNTER_USED(&pStats->StatRZFailedPrefix, "/EM/CPU%d/RZ/Interpret/Failed/Prefix", "The number of rejections because of prefix .");
383 EM_REG_COUNTER_USED(&pStats->StatR3FailedPrefix, "/EM/CPU%d/R3/Interpret/Failed/Prefix", "The number of rejections because of prefix .");
384
385 EM_REG_COUNTER_USED(&pStats->StatCli, "/EM/CPU%d/R3/PrivInst/Cli", "Number of cli instructions.");
386 EM_REG_COUNTER_USED(&pStats->StatSti, "/EM/CPU%d/R3/PrivInst/Sti", "Number of sli instructions.");
387 EM_REG_COUNTER_USED(&pStats->StatIn, "/EM/CPU%d/R3/PrivInst/In", "Number of in instructions.");
388 EM_REG_COUNTER_USED(&pStats->StatOut, "/EM/CPU%d/R3/PrivInst/Out", "Number of out instructions.");
389 EM_REG_COUNTER_USED(&pStats->StatIoRestarted, "/EM/CPU%d/R3/PrivInst/IoRestarted", "Number of restarted i/o instructions.");
390 EM_REG_COUNTER_USED(&pStats->StatHlt, "/EM/CPU%d/R3/PrivInst/Hlt", "Number of hlt instructions not handled in GC because of PATM.");
391 EM_REG_COUNTER_USED(&pStats->StatInvlpg, "/EM/CPU%d/R3/PrivInst/Invlpg", "Number of invlpg instructions.");
392 EM_REG_COUNTER_USED(&pStats->StatMisc, "/EM/CPU%d/R3/PrivInst/Misc", "Number of misc. instructions.");
393 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[0], "/EM/CPU%d/R3/PrivInst/Mov CR0, X", "Number of mov CR0 write instructions.");
394 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[1], "/EM/CPU%d/R3/PrivInst/Mov CR1, X", "Number of mov CR1 write instructions.");
395 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[2], "/EM/CPU%d/R3/PrivInst/Mov CR2, X", "Number of mov CR2 write instructions.");
396 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[3], "/EM/CPU%d/R3/PrivInst/Mov CR3, X", "Number of mov CR3 write instructions.");
397 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[4], "/EM/CPU%d/R3/PrivInst/Mov CR4, X", "Number of mov CR4 write instructions.");
398 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[0], "/EM/CPU%d/R3/PrivInst/Mov X, CR0", "Number of mov CR0 read instructions.");
399 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[1], "/EM/CPU%d/R3/PrivInst/Mov X, CR1", "Number of mov CR1 read instructions.");
400 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[2], "/EM/CPU%d/R3/PrivInst/Mov X, CR2", "Number of mov CR2 read instructions.");
401 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[3], "/EM/CPU%d/R3/PrivInst/Mov X, CR3", "Number of mov CR3 read instructions.");
402 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[4], "/EM/CPU%d/R3/PrivInst/Mov X, CR4", "Number of mov CR4 read instructions.");
403 EM_REG_COUNTER_USED(&pStats->StatMovDRx, "/EM/CPU%d/R3/PrivInst/MovDRx", "Number of mov DRx instructions.");
404 EM_REG_COUNTER_USED(&pStats->StatIret, "/EM/CPU%d/R3/PrivInst/Iret", "Number of iret instructions.");
405 EM_REG_COUNTER_USED(&pStats->StatMovLgdt, "/EM/CPU%d/R3/PrivInst/Lgdt", "Number of lgdt instructions.");
406 EM_REG_COUNTER_USED(&pStats->StatMovLidt, "/EM/CPU%d/R3/PrivInst/Lidt", "Number of lidt instructions.");
407 EM_REG_COUNTER_USED(&pStats->StatMovLldt, "/EM/CPU%d/R3/PrivInst/Lldt", "Number of lldt instructions.");
408 EM_REG_COUNTER_USED(&pStats->StatSysEnter, "/EM/CPU%d/R3/PrivInst/Sysenter", "Number of sysenter instructions.");
409 EM_REG_COUNTER_USED(&pStats->StatSysExit, "/EM/CPU%d/R3/PrivInst/Sysexit", "Number of sysexit instructions.");
410 EM_REG_COUNTER_USED(&pStats->StatSysCall, "/EM/CPU%d/R3/PrivInst/Syscall", "Number of syscall instructions.");
411 EM_REG_COUNTER_USED(&pStats->StatSysRet, "/EM/CPU%d/R3/PrivInst/Sysret", "Number of sysret instructions.");
412
413 EM_REG_COUNTER(&pVCpu->em.s.StatTotalClis, "/EM/CPU%d/Cli/Total", "Total number of cli instructions executed.");
414 pVCpu->em.s.pCliStatTree = 0;
415
416 /* these should be considered for release statistics. */
417 EM_REG_COUNTER(&pVCpu->em.s.StatIOEmu, "/PROF/CPU%d/EM/Emulation/IO", "Profiling of emR3RawExecuteIOInstruction.");
418 EM_REG_COUNTER(&pVCpu->em.s.StatPrivEmu, "/PROF/CPU%d/EM/Emulation/Priv", "Profiling of emR3RawPrivileged.");
419 EM_REG_PROFILE(&pVCpu->em.s.StatHmEntry, "/PROF/CPU%d/EM/HmEnter", "Profiling Hardware Accelerated Mode entry overhead.");
420 EM_REG_PROFILE(&pVCpu->em.s.StatHmExec, "/PROF/CPU%d/EM/HmExec", "Profiling Hardware Accelerated Mode execution.");
421 EM_REG_PROFILE(&pVCpu->em.s.StatREMEmu, "/PROF/CPU%d/EM/REMEmuSingle", "Profiling single instruction REM execution.");
422 EM_REG_PROFILE(&pVCpu->em.s.StatREMExec, "/PROF/CPU%d/EM/REMExec", "Profiling REM execution.");
423 EM_REG_PROFILE(&pVCpu->em.s.StatREMSync, "/PROF/CPU%d/EM/REMSync", "Profiling REM context syncing.");
424 EM_REG_PROFILE(&pVCpu->em.s.StatRAWEntry, "/PROF/CPU%d/EM/RAWEnter", "Profiling Raw Mode entry overhead.");
425 EM_REG_PROFILE(&pVCpu->em.s.StatRAWExec, "/PROF/CPU%d/EM/RAWExec", "Profiling Raw Mode execution.");
426 EM_REG_PROFILE(&pVCpu->em.s.StatRAWTail, "/PROF/CPU%d/EM/RAWTail", "Profiling Raw Mode tail overhead.");
427
428#endif /* VBOX_WITH_STATISTICS */
429
430 EM_REG_COUNTER(&pVCpu->em.s.StatForcedActions, "/PROF/CPU%d/EM/ForcedActions", "Profiling forced action execution.");
431 EM_REG_COUNTER(&pVCpu->em.s.StatHalted, "/PROF/CPU%d/EM/Halted", "Profiling halted state (VMR3WaitHalted).");
432 EM_REG_PROFILE_ADV(&pVCpu->em.s.StatCapped, "/PROF/CPU%d/EM/Capped", "Profiling capped state (sleep).");
433 EM_REG_COUNTER(&pVCpu->em.s.StatREMTotal, "/PROF/CPU%d/EM/REMTotal", "Profiling emR3RemExecute (excluding FFs).");
434 EM_REG_COUNTER(&pVCpu->em.s.StatRAWTotal, "/PROF/CPU%d/EM/RAWTotal", "Profiling emR3RawExecute (excluding FFs).");
435
436 EM_REG_PROFILE_ADV(&pVCpu->em.s.StatTotal, "/PROF/CPU%d/EM/Total", "Profiling EMR3ExecuteVM.");
437 }
438
439 emR3InitDbg(pVM);
440 return VINF_SUCCESS;
441}
442
443
444/**
445 * Applies relocations to data and code managed by this
446 * component. This function will be called at init and
447 * whenever the VMM need to relocate it self inside the GC.
448 *
449 * @param pVM Pointer to the VM.
450 */
451VMMR3_INT_DECL(void) EMR3Relocate(PVM pVM)
452{
453 LogFlow(("EMR3Relocate\n"));
454 for (VMCPUID i = 0; i < pVM->cCpus; i++)
455 {
456 PVMCPU pVCpu = &pVM->aCpus[i];
457 if (pVCpu->em.s.pStatsR3)
458 pVCpu->em.s.pStatsRC = MMHyperR3ToRC(pVM, pVCpu->em.s.pStatsR3);
459 }
460}
461
462
463/**
464 * Reset the EM state for a CPU.
465 *
466 * Called by EMR3Reset and hot plugging.
467 *
468 * @param pVCpu Pointer to the VMCPU.
469 */
470VMMR3_INT_DECL(void) EMR3ResetCpu(PVMCPU pVCpu)
471{
472 pVCpu->em.s.fForceRAW = false;
473
474 /* VMR3Reset may return VINF_EM_RESET or VINF_EM_SUSPEND, so transition
475 out of the HALTED state here so that enmPrevState doesn't end up as
476 HALTED when EMR3Execute returns. */
477 if (pVCpu->em.s.enmState == EMSTATE_HALTED)
478 {
479 Log(("EMR3ResetCpu: Cpu#%u %s -> %s\n", pVCpu->idCpu, emR3GetStateName(pVCpu->em.s.enmState), pVCpu->idCpu == 0 ? "EMSTATE_NONE" : "EMSTATE_WAIT_SIPI"));
480 pVCpu->em.s.enmState = pVCpu->idCpu == 0 ? EMSTATE_NONE : EMSTATE_WAIT_SIPI;
481 }
482}
483
484
485/**
486 * Reset notification.
487 *
488 * @param pVM Pointer to the VM.
489 */
490VMMR3_INT_DECL(void) EMR3Reset(PVM pVM)
491{
492 Log(("EMR3Reset: \n"));
493 for (VMCPUID i = 0; i < pVM->cCpus; i++)
494 EMR3ResetCpu(&pVM->aCpus[i]);
495}
496
497
498/**
499 * Terminates the EM.
500 *
501 * Termination means cleaning up and freeing all resources,
502 * the VM it self is at this point powered off or suspended.
503 *
504 * @returns VBox status code.
505 * @param pVM Pointer to the VM.
506 */
507VMMR3_INT_DECL(int) EMR3Term(PVM pVM)
508{
509 AssertMsg(pVM->em.s.offVM, ("bad init order!\n"));
510
511#ifdef VBOX_WITH_REM
512 PDMR3CritSectDelete(&pVM->em.s.CritSectREM);
513#endif
514 return VINF_SUCCESS;
515}
516
517
518/**
519 * Execute state save operation.
520 *
521 * @returns VBox status code.
522 * @param pVM Pointer to the VM.
523 * @param pSSM SSM operation handle.
524 */
525static DECLCALLBACK(int) emR3Save(PVM pVM, PSSMHANDLE pSSM)
526{
527 for (VMCPUID i = 0; i < pVM->cCpus; i++)
528 {
529 PVMCPU pVCpu = &pVM->aCpus[i];
530
531 int rc = SSMR3PutBool(pSSM, pVCpu->em.s.fForceRAW);
532 AssertRCReturn(rc, rc);
533
534 Assert(pVCpu->em.s.enmState == EMSTATE_SUSPENDED);
535 Assert(pVCpu->em.s.enmPrevState != EMSTATE_SUSPENDED);
536 rc = SSMR3PutU32(pSSM, pVCpu->em.s.enmPrevState);
537 AssertRCReturn(rc, rc);
538
539 /* Save mwait state. */
540 rc = SSMR3PutU32(pSSM, pVCpu->em.s.MWait.fWait);
541 AssertRCReturn(rc, rc);
542 rc = SSMR3PutGCPtr(pSSM, pVCpu->em.s.MWait.uMWaitRAX);
543 AssertRCReturn(rc, rc);
544 rc = SSMR3PutGCPtr(pSSM, pVCpu->em.s.MWait.uMWaitRCX);
545 AssertRCReturn(rc, rc);
546 rc = SSMR3PutGCPtr(pSSM, pVCpu->em.s.MWait.uMonitorRAX);
547 AssertRCReturn(rc, rc);
548 rc = SSMR3PutGCPtr(pSSM, pVCpu->em.s.MWait.uMonitorRCX);
549 AssertRCReturn(rc, rc);
550 rc = SSMR3PutGCPtr(pSSM, pVCpu->em.s.MWait.uMonitorRDX);
551 AssertRCReturn(rc, rc);
552 }
553 return VINF_SUCCESS;
554}
555
556
557/**
558 * Execute state load operation.
559 *
560 * @returns VBox status code.
561 * @param pVM Pointer to the VM.
562 * @param pSSM SSM operation handle.
563 * @param uVersion Data layout version.
564 * @param uPass The data pass.
565 */
566static DECLCALLBACK(int) emR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
567{
568 /*
569 * Validate version.
570 */
571 if ( uVersion > EM_SAVED_STATE_VERSION
572 || uVersion < EM_SAVED_STATE_VERSION_PRE_SMP)
573 {
574 AssertMsgFailed(("emR3Load: Invalid version uVersion=%d (current %d)!\n", uVersion, EM_SAVED_STATE_VERSION));
575 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
576 }
577 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
578
579 /*
580 * Load the saved state.
581 */
582 for (VMCPUID i = 0; i < pVM->cCpus; i++)
583 {
584 PVMCPU pVCpu = &pVM->aCpus[i];
585
586 int rc = SSMR3GetBool(pSSM, &pVCpu->em.s.fForceRAW);
587 if (RT_FAILURE(rc))
588 pVCpu->em.s.fForceRAW = false;
589 AssertRCReturn(rc, rc);
590
591 if (uVersion > EM_SAVED_STATE_VERSION_PRE_SMP)
592 {
593 AssertCompile(sizeof(pVCpu->em.s.enmPrevState) == sizeof(uint32_t));
594 rc = SSMR3GetU32(pSSM, (uint32_t *)&pVCpu->em.s.enmPrevState);
595 AssertRCReturn(rc, rc);
596 Assert(pVCpu->em.s.enmPrevState != EMSTATE_SUSPENDED);
597
598 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
599 }
600 if (uVersion > EM_SAVED_STATE_VERSION_PRE_MWAIT)
601 {
602 /* Load mwait state. */
603 rc = SSMR3GetU32(pSSM, &pVCpu->em.s.MWait.fWait);
604 AssertRCReturn(rc, rc);
605 rc = SSMR3GetGCPtr(pSSM, &pVCpu->em.s.MWait.uMWaitRAX);
606 AssertRCReturn(rc, rc);
607 rc = SSMR3GetGCPtr(pSSM, &pVCpu->em.s.MWait.uMWaitRCX);
608 AssertRCReturn(rc, rc);
609 rc = SSMR3GetGCPtr(pSSM, &pVCpu->em.s.MWait.uMonitorRAX);
610 AssertRCReturn(rc, rc);
611 rc = SSMR3GetGCPtr(pSSM, &pVCpu->em.s.MWait.uMonitorRCX);
612 AssertRCReturn(rc, rc);
613 rc = SSMR3GetGCPtr(pSSM, &pVCpu->em.s.MWait.uMonitorRDX);
614 AssertRCReturn(rc, rc);
615 }
616
617 Assert(!pVCpu->em.s.pCliStatTree);
618 }
619 return VINF_SUCCESS;
620}
621
622
623/**
624 * Argument packet for emR3SetExecutionPolicy.
625 */
626struct EMR3SETEXECPOLICYARGS
627{
628 EMEXECPOLICY enmPolicy;
629 bool fEnforce;
630};
631
632
633/**
634 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Rendezvous callback for EMR3SetExecutionPolicy.}
635 */
636static DECLCALLBACK(VBOXSTRICTRC) emR3SetExecutionPolicy(PVM pVM, PVMCPU pVCpu, void *pvUser)
637{
638 /*
639 * Only the first CPU changes the variables.
640 */
641 if (pVCpu->idCpu == 0)
642 {
643 struct EMR3SETEXECPOLICYARGS *pArgs = (struct EMR3SETEXECPOLICYARGS *)pvUser;
644 switch (pArgs->enmPolicy)
645 {
646 case EMEXECPOLICY_RECOMPILE_RING0:
647 pVM->fRecompileSupervisor = pArgs->fEnforce;
648 break;
649 case EMEXECPOLICY_RECOMPILE_RING3:
650 pVM->fRecompileUser = pArgs->fEnforce;
651 break;
652 case EMEXECPOLICY_IEM_ALL:
653 pVM->em.s.fIemExecutesAll = pArgs->fEnforce;
654 break;
655 default:
656 AssertFailedReturn(VERR_INVALID_PARAMETER);
657 }
658 Log(("emR3SetExecutionPolicy: fRecompileUser=%RTbool fRecompileSupervisor=%RTbool fIemExecutesAll=%RTbool\n",
659 pVM->fRecompileUser, pVM->fRecompileSupervisor, pVM->em.s.fIemExecutesAll));
660 }
661
662 /*
663 * Force rescheduling if in RAW, HM, IEM, or REM.
664 */
665 return pVCpu->em.s.enmState == EMSTATE_RAW
666 || pVCpu->em.s.enmState == EMSTATE_HM
667 || pVCpu->em.s.enmState == EMSTATE_IEM
668 || pVCpu->em.s.enmState == EMSTATE_REM
669 ? VINF_EM_RESCHEDULE
670 : VINF_SUCCESS;
671}
672
673
674/**
675 * Changes an execution scheduling policy parameter.
676 *
677 * This is used to enable or disable raw-mode / hardware-virtualization
678 * execution of user and supervisor code.
679 *
680 * @returns VINF_SUCCESS on success.
681 * @returns VINF_RESCHEDULE if a rescheduling might be required.
682 * @returns VERR_INVALID_PARAMETER on an invalid enmMode value.
683 *
684 * @param pUVM The user mode VM handle.
685 * @param enmPolicy The scheduling policy to change.
686 * @param fEnforce Whether to enforce the policy or not.
687 */
688VMMR3DECL(int) EMR3SetExecutionPolicy(PUVM pUVM, EMEXECPOLICY enmPolicy, bool fEnforce)
689{
690 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
691 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
692 AssertReturn(enmPolicy > EMEXECPOLICY_INVALID && enmPolicy < EMEXECPOLICY_END, VERR_INVALID_PARAMETER);
693
694 struct EMR3SETEXECPOLICYARGS Args = { enmPolicy, fEnforce };
695 return VMMR3EmtRendezvous(pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING, emR3SetExecutionPolicy, &Args);
696}
697
698
699/**
700 * Queries an execution scheduling policy parameter.
701 *
702 * @returns VBox status code
703 * @param pUVM The user mode VM handle.
704 * @param enmPolicy The scheduling policy to query.
705 * @param pfEnforced Where to return the current value.
706 */
707VMMR3DECL(int) EMR3QueryExecutionPolicy(PUVM pUVM, EMEXECPOLICY enmPolicy, bool *pfEnforced)
708{
709 AssertReturn(enmPolicy > EMEXECPOLICY_INVALID && enmPolicy < EMEXECPOLICY_END, VERR_INVALID_PARAMETER);
710 AssertPtrReturn(pfEnforced, VERR_INVALID_POINTER);
711 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
712 PVM pVM = pUVM->pVM;
713 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
714
715 /* No need to bother EMTs with a query. */
716 switch (enmPolicy)
717 {
718 case EMEXECPOLICY_RECOMPILE_RING0:
719 *pfEnforced = pVM->fRecompileSupervisor;
720 break;
721 case EMEXECPOLICY_RECOMPILE_RING3:
722 *pfEnforced = pVM->fRecompileUser;
723 break;
724 case EMEXECPOLICY_IEM_ALL:
725 *pfEnforced = pVM->em.s.fIemExecutesAll;
726 break;
727 default:
728 AssertFailedReturn(VERR_INTERNAL_ERROR_2);
729 }
730
731 return VINF_SUCCESS;
732}
733
734
735/**
736 * Raise a fatal error.
737 *
738 * Safely terminate the VM with full state report and stuff. This function
739 * will naturally never return.
740 *
741 * @param pVCpu Pointer to the VMCPU.
742 * @param rc VBox status code.
743 */
744VMMR3DECL(void) EMR3FatalError(PVMCPU pVCpu, int rc)
745{
746 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
747 longjmp(pVCpu->em.s.u.FatalLongJump, rc);
748 AssertReleaseMsgFailed(("longjmp returned!\n"));
749}
750
751
752#if defined(LOG_ENABLED) || defined(VBOX_STRICT)
753/**
754 * Gets the EM state name.
755 *
756 * @returns pointer to read only state name,
757 * @param enmState The state.
758 */
759static const char *emR3GetStateName(EMSTATE enmState)
760{
761 switch (enmState)
762 {
763 case EMSTATE_NONE: return "EMSTATE_NONE";
764 case EMSTATE_RAW: return "EMSTATE_RAW";
765 case EMSTATE_HM: return "EMSTATE_HM";
766 case EMSTATE_IEM: return "EMSTATE_IEM";
767 case EMSTATE_REM: return "EMSTATE_REM";
768 case EMSTATE_HALTED: return "EMSTATE_HALTED";
769 case EMSTATE_WAIT_SIPI: return "EMSTATE_WAIT_SIPI";
770 case EMSTATE_SUSPENDED: return "EMSTATE_SUSPENDED";
771 case EMSTATE_TERMINATING: return "EMSTATE_TERMINATING";
772 case EMSTATE_DEBUG_GUEST_RAW: return "EMSTATE_DEBUG_GUEST_RAW";
773 case EMSTATE_DEBUG_GUEST_REM: return "EMSTATE_DEBUG_GUEST_REM";
774 case EMSTATE_DEBUG_HYPER: return "EMSTATE_DEBUG_HYPER";
775 case EMSTATE_GURU_MEDITATION: return "EMSTATE_GURU_MEDITATION";
776 default: return "Unknown!";
777 }
778}
779#endif /* LOG_ENABLED || VBOX_STRICT */
780
781
782/**
783 * Debug loop.
784 *
785 * @returns VBox status code for EM.
786 * @param pVM Pointer to the VM.
787 * @param pVCpu Pointer to the VMCPU.
788 * @param rc Current EM VBox status code.
789 */
790static int emR3Debug(PVM pVM, PVMCPU pVCpu, int rc)
791{
792 for (;;)
793 {
794 Log(("emR3Debug: rc=%Rrc\n", rc));
795 const int rcLast = rc;
796
797 /*
798 * Debug related RC.
799 */
800 switch (rc)
801 {
802 /*
803 * Single step an instruction.
804 */
805 case VINF_EM_DBG_STEP:
806#ifdef VBOX_WITH_RAW_MODE
807 if ( pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_RAW
808 || pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER
809 || pVCpu->em.s.fForceRAW /* paranoia */)
810 rc = emR3RawStep(pVM, pVCpu);
811 else
812 {
813 Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_REM);
814 rc = emR3RemStep(pVM, pVCpu);
815 }
816#else
817 AssertLogRelMsgFailed(("%Rrc\n", rc));
818 rc = VERR_EM_INTERNAL_ERROR;
819#endif
820 break;
821
822 /*
823 * Simple events: stepped, breakpoint, stop/assertion.
824 */
825 case VINF_EM_DBG_STEPPED:
826 rc = DBGFR3Event(pVM, DBGFEVENT_STEPPED);
827 break;
828
829 case VINF_EM_DBG_BREAKPOINT:
830 rc = DBGFR3EventBreakpoint(pVM, DBGFEVENT_BREAKPOINT);
831 break;
832
833 case VINF_EM_DBG_STOP:
834 rc = DBGFR3EventSrc(pVM, DBGFEVENT_DEV_STOP, NULL, 0, NULL, NULL);
835 break;
836
837 case VINF_EM_DBG_HYPER_STEPPED:
838 rc = DBGFR3Event(pVM, DBGFEVENT_STEPPED_HYPER);
839 break;
840
841 case VINF_EM_DBG_HYPER_BREAKPOINT:
842 rc = DBGFR3EventBreakpoint(pVM, DBGFEVENT_BREAKPOINT_HYPER);
843 break;
844
845 case VINF_EM_DBG_HYPER_ASSERTION:
846 RTPrintf("\nVINF_EM_DBG_HYPER_ASSERTION:\n%s%s\n", VMMR3GetRZAssertMsg1(pVM), VMMR3GetRZAssertMsg2(pVM));
847 RTLogFlush(NULL);
848 rc = DBGFR3EventAssertion(pVM, DBGFEVENT_ASSERTION_HYPER, VMMR3GetRZAssertMsg1(pVM), VMMR3GetRZAssertMsg2(pVM));
849 break;
850
851 /*
852 * Guru meditation.
853 */
854 case VERR_VMM_RING0_ASSERTION: /** @todo Make a guru meditation event! */
855 rc = DBGFR3EventSrc(pVM, DBGFEVENT_FATAL_ERROR, "VERR_VMM_RING0_ASSERTION", 0, NULL, NULL);
856 break;
857 case VERR_REM_TOO_MANY_TRAPS: /** @todo Make a guru meditation event! */
858 rc = DBGFR3EventSrc(pVM, DBGFEVENT_DEV_STOP, "VERR_REM_TOO_MANY_TRAPS", 0, NULL, NULL);
859 break;
860
861 default: /** @todo don't use default for guru, but make special errors code! */
862 rc = DBGFR3Event(pVM, DBGFEVENT_FATAL_ERROR);
863 break;
864 }
865
866 /*
867 * Process the result.
868 */
869 do
870 {
871 switch (rc)
872 {
873 /*
874 * Continue the debugging loop.
875 */
876 case VINF_EM_DBG_STEP:
877 case VINF_EM_DBG_STOP:
878 case VINF_EM_DBG_STEPPED:
879 case VINF_EM_DBG_BREAKPOINT:
880 case VINF_EM_DBG_HYPER_STEPPED:
881 case VINF_EM_DBG_HYPER_BREAKPOINT:
882 case VINF_EM_DBG_HYPER_ASSERTION:
883 break;
884
885 /*
886 * Resuming execution (in some form) has to be done here if we got
887 * a hypervisor debug event.
888 */
889 case VINF_SUCCESS:
890 case VINF_EM_RESUME:
891 case VINF_EM_SUSPEND:
892 case VINF_EM_RESCHEDULE:
893 case VINF_EM_RESCHEDULE_RAW:
894 case VINF_EM_RESCHEDULE_REM:
895 case VINF_EM_HALT:
896 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
897 {
898#ifdef VBOX_WITH_RAW_MODE
899 rc = emR3RawResumeHyper(pVM, pVCpu);
900 if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
901 continue;
902#else
903 AssertLogRelMsgFailedReturn(("Not implemented\n", rc), VERR_EM_INTERNAL_ERROR);
904#endif
905 }
906 if (rc == VINF_SUCCESS)
907 rc = VINF_EM_RESCHEDULE;
908 return rc;
909
910 /*
911 * The debugger isn't attached.
912 * We'll simply turn the thing off since that's the easiest thing to do.
913 */
914 case VERR_DBGF_NOT_ATTACHED:
915 switch (rcLast)
916 {
917 case VINF_EM_DBG_HYPER_STEPPED:
918 case VINF_EM_DBG_HYPER_BREAKPOINT:
919 case VINF_EM_DBG_HYPER_ASSERTION:
920 case VERR_TRPM_PANIC:
921 case VERR_TRPM_DONT_PANIC:
922 case VERR_VMM_RING0_ASSERTION:
923 case VERR_VMM_HYPER_CR3_MISMATCH:
924 case VERR_VMM_RING3_CALL_DISABLED:
925 return rcLast;
926 }
927 return VINF_EM_OFF;
928
929 /*
930 * Status codes terminating the VM in one or another sense.
931 */
932 case VINF_EM_TERMINATE:
933 case VINF_EM_OFF:
934 case VINF_EM_RESET:
935 case VINF_EM_NO_MEMORY:
936 case VINF_EM_RAW_STALE_SELECTOR:
937 case VINF_EM_RAW_IRET_TRAP:
938 case VERR_TRPM_PANIC:
939 case VERR_TRPM_DONT_PANIC:
940 case VERR_IEM_INSTR_NOT_IMPLEMENTED:
941 case VERR_IEM_ASPECT_NOT_IMPLEMENTED:
942 case VERR_VMM_RING0_ASSERTION:
943 case VERR_VMM_HYPER_CR3_MISMATCH:
944 case VERR_VMM_RING3_CALL_DISABLED:
945 case VERR_INTERNAL_ERROR:
946 case VERR_INTERNAL_ERROR_2:
947 case VERR_INTERNAL_ERROR_3:
948 case VERR_INTERNAL_ERROR_4:
949 case VERR_INTERNAL_ERROR_5:
950 case VERR_IPE_UNEXPECTED_STATUS:
951 case VERR_IPE_UNEXPECTED_INFO_STATUS:
952 case VERR_IPE_UNEXPECTED_ERROR_STATUS:
953 return rc;
954
955 /*
956 * The rest is unexpected, and will keep us here.
957 */
958 default:
959 AssertMsgFailed(("Unexpected rc %Rrc!\n", rc));
960 break;
961 }
962 } while (false);
963 } /* debug for ever */
964}
965
966
967/**
968 * Steps recompiled code.
969 *
970 * @returns VBox status code. The most important ones are: VINF_EM_STEP_EVENT,
971 * VINF_EM_RESCHEDULE, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
972 *
973 * @param pVM Pointer to the VM.
974 * @param pVCpu Pointer to the VMCPU.
975 */
976static int emR3RemStep(PVM pVM, PVMCPU pVCpu)
977{
978 Log3(("emR3RemStep: cs:eip=%04x:%08x\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
979
980#ifdef VBOX_WITH_REM
981 EMRemLock(pVM);
982
983 /*
984 * Switch to REM, step instruction, switch back.
985 */
986 int rc = REMR3State(pVM, pVCpu);
987 if (RT_SUCCESS(rc))
988 {
989 rc = REMR3Step(pVM, pVCpu);
990 REMR3StateBack(pVM, pVCpu);
991 }
992 EMRemUnlock(pVM);
993
994#else
995 int rc = VBOXSTRICTRC_TODO(IEMExecOne(pVCpu)); NOREF(pVM);
996#endif
997
998 Log3(("emR3RemStep: returns %Rrc cs:eip=%04x:%08x\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
999 return rc;
1000}
1001
1002
1003/**
1004 * emR3RemExecute helper that syncs the state back from REM and leave the REM
1005 * critical section.
1006 *
1007 * @returns false - new fInREMState value.
1008 * @param pVM Pointer to the VM.
1009 * @param pVCpu Pointer to the VMCPU.
1010 */
1011DECLINLINE(bool) emR3RemExecuteSyncBack(PVM pVM, PVMCPU pVCpu)
1012{
1013#ifdef VBOX_WITH_REM
1014 STAM_PROFILE_START(&pVCpu->em.s.StatREMSync, a);
1015 REMR3StateBack(pVM, pVCpu);
1016 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, a);
1017
1018 EMRemUnlock(pVM);
1019#endif
1020 return false;
1021}
1022
1023
1024/**
1025 * Executes recompiled code.
1026 *
1027 * This function contains the recompiler version of the inner
1028 * execution loop (the outer loop being in EMR3ExecuteVM()).
1029 *
1030 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
1031 * VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
1032 *
1033 * @param pVM Pointer to the VM.
1034 * @param pVCpu Pointer to the VMCPU.
1035 * @param pfFFDone Where to store an indicator telling whether or not
1036 * FFs were done before returning.
1037 *
1038 */
1039static int emR3RemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
1040{
1041#ifdef LOG_ENABLED
1042 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1043 uint32_t cpl = CPUMGetGuestCPL(pVCpu);
1044
1045 if (pCtx->eflags.Bits.u1VM)
1046 Log(("EMV86: %04X:%08X IF=%d\n", pCtx->cs.Sel, pCtx->eip, pCtx->eflags.Bits.u1IF));
1047 else
1048 Log(("EMR%d: %04X:%08X ESP=%08X IF=%d CR0=%x eflags=%x\n", cpl, pCtx->cs.Sel, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, (uint32_t)pCtx->cr0, pCtx->eflags.u));
1049#endif
1050 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatREMTotal, a);
1051
1052#if defined(VBOX_STRICT) && defined(DEBUG_bird)
1053 AssertMsg( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
1054 || !MMHyperIsInsideArea(pVM, CPUMGetGuestEIP(pVCpu)), /** @todo @bugref{1419} - get flat address. */
1055 ("cs:eip=%RX16:%RX32\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1056#endif
1057
1058 /*
1059 * Spin till we get a forced action which returns anything but VINF_SUCCESS
1060 * or the REM suggests raw-mode execution.
1061 */
1062 *pfFFDone = false;
1063#ifdef VBOX_WITH_REM
1064 bool fInREMState = false;
1065#endif
1066 int rc = VINF_SUCCESS;
1067 for (;;)
1068 {
1069#ifdef VBOX_WITH_REM
1070 /*
1071 * Lock REM and update the state if not already in sync.
1072 *
1073 * Note! Big lock, but you are not supposed to own any lock when
1074 * coming in here.
1075 */
1076 if (!fInREMState)
1077 {
1078 EMRemLock(pVM);
1079 STAM_PROFILE_START(&pVCpu->em.s.StatREMSync, b);
1080
1081 /* Flush the recompiler translation blocks if the VCPU has changed,
1082 also force a full CPU state resync. */
1083 if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
1084 {
1085 REMFlushTBs(pVM);
1086 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
1087 }
1088 pVM->em.s.idLastRemCpu = pVCpu->idCpu;
1089
1090 rc = REMR3State(pVM, pVCpu);
1091
1092 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, b);
1093 if (RT_FAILURE(rc))
1094 break;
1095 fInREMState = true;
1096
1097 /*
1098 * We might have missed the raising of VMREQ, TIMER and some other
1099 * important FFs while we were busy switching the state. So, check again.
1100 */
1101 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_DBGF | VM_FF_CHECK_VM_STATE | VM_FF_RESET)
1102 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TIMER | VMCPU_FF_REQUEST))
1103 {
1104 LogFlow(("emR3RemExecute: Skipping run, because FF is set. %#x\n", pVM->fGlobalForcedActions));
1105 goto l_REMDoForcedActions;
1106 }
1107 }
1108#endif
1109
1110 /*
1111 * Execute REM.
1112 */
1113 if (RT_LIKELY(emR3IsExecutionAllowed(pVM, pVCpu)))
1114 {
1115 STAM_PROFILE_START(&pVCpu->em.s.StatREMExec, c);
1116#ifdef VBOX_WITH_REM
1117 rc = REMR3Run(pVM, pVCpu);
1118#else
1119 rc = VBOXSTRICTRC_TODO(IEMExecLots(pVCpu));
1120#endif
1121 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMExec, c);
1122 }
1123 else
1124 {
1125 /* Give up this time slice; virtual time continues */
1126 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatCapped, u);
1127 RTThreadSleep(5);
1128 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatCapped, u);
1129 rc = VINF_SUCCESS;
1130 }
1131
1132 /*
1133 * Deal with high priority post execution FFs before doing anything
1134 * else. Sync back the state and leave the lock to be on the safe side.
1135 */
1136 if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
1137 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
1138 {
1139#ifdef VBOX_WITH_REM
1140 fInREMState = emR3RemExecuteSyncBack(pVM, pVCpu);
1141#endif
1142 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1143 }
1144
1145 /*
1146 * Process the returned status code.
1147 */
1148 if (rc != VINF_SUCCESS)
1149 {
1150 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
1151 break;
1152 if (rc != VINF_REM_INTERRUPED_FF)
1153 {
1154 /*
1155 * Anything which is not known to us means an internal error
1156 * and the termination of the VM!
1157 */
1158 AssertMsg(rc == VERR_REM_TOO_MANY_TRAPS, ("Unknown GC return code: %Rra\n", rc));
1159 break;
1160 }
1161 }
1162
1163
1164 /*
1165 * Check and execute forced actions.
1166 *
1167 * Sync back the VM state and leave the lock before calling any of
1168 * these, you never know what's going to happen here.
1169 */
1170#ifdef VBOX_HIGH_RES_TIMERS_HACK
1171 TMTimerPollVoid(pVM, pVCpu);
1172#endif
1173 AssertCompile(VMCPU_FF_ALL_REM_MASK & VMCPU_FF_TIMER);
1174 if ( VM_FF_IS_PENDING(pVM, VM_FF_ALL_REM_MASK)
1175 || VMCPU_FF_IS_PENDING(pVCpu,
1176 VMCPU_FF_ALL_REM_MASK
1177 & VM_WHEN_RAW_MODE(~(VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_CSAM_SCAN_PAGE), UINT32_MAX)) )
1178 {
1179l_REMDoForcedActions:
1180#ifdef VBOX_WITH_REM
1181 if (fInREMState)
1182 fInREMState = emR3RemExecuteSyncBack(pVM, pVCpu);
1183#endif
1184 STAM_REL_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatREMTotal, a);
1185 rc = emR3ForcedActions(pVM, pVCpu, rc);
1186 VBOXVMM_EM_FF_ALL_RET(pVCpu, rc);
1187 STAM_REL_PROFILE_ADV_RESUME(&pVCpu->em.s.StatREMTotal, a);
1188 if ( rc != VINF_SUCCESS
1189 && rc != VINF_EM_RESCHEDULE_REM)
1190 {
1191 *pfFFDone = true;
1192 break;
1193 }
1194 }
1195
1196 } /* The Inner Loop, recompiled execution mode version. */
1197
1198
1199#ifdef VBOX_WITH_REM
1200 /*
1201 * Returning. Sync back the VM state if required.
1202 */
1203 if (fInREMState)
1204 fInREMState = emR3RemExecuteSyncBack(pVM, pVCpu);
1205#endif
1206
1207 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatREMTotal, a);
1208 return rc;
1209}
1210
1211
1212#ifdef DEBUG
1213
1214int emR3SingleStepExecRem(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
1215{
1216 EMSTATE enmOldState = pVCpu->em.s.enmState;
1217
1218 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_REM;
1219
1220 Log(("Single step BEGIN:\n"));
1221 for (uint32_t i = 0; i < cIterations; i++)
1222 {
1223 DBGFR3PrgStep(pVCpu);
1224 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "RSS");
1225 emR3RemStep(pVM, pVCpu);
1226 if (emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx) != EMSTATE_REM)
1227 break;
1228 }
1229 Log(("Single step END:\n"));
1230 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1231 pVCpu->em.s.enmState = enmOldState;
1232 return VINF_EM_RESCHEDULE;
1233}
1234
1235#endif /* DEBUG */
1236
1237
1238/**
1239 * Decides whether to execute RAW, HWACC or REM.
1240 *
1241 * @returns new EM state
1242 * @param pVM Pointer to the VM.
1243 * @param pVCpu Pointer to the VMCPU.
1244 * @param pCtx Pointer to the guest CPU context.
1245 */
1246EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1247{
1248#ifdef IEM_VERIFICATION_MODE
1249 return EMSTATE_REM;
1250#else
1251
1252 /*
1253 * When forcing raw-mode execution, things are simple.
1254 */
1255 if (pVCpu->em.s.fForceRAW)
1256 return EMSTATE_RAW;
1257
1258 /*
1259 * We stay in the wait for SIPI state unless explicitly told otherwise.
1260 */
1261 if (pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI)
1262 return EMSTATE_WAIT_SIPI;
1263
1264 /*
1265 * Execute everything in IEM?
1266 */
1267 if (pVM->em.s.fIemExecutesAll)
1268 return EMSTATE_IEM;
1269
1270 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
1271 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
1272 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
1273
1274 X86EFLAGS EFlags = pCtx->eflags;
1275 if (HMIsEnabled(pVM))
1276 {
1277 /*
1278 * Hardware accelerated raw-mode:
1279 */
1280 if ( EMIsHwVirtExecutionEnabled(pVM)
1281 && HMR3CanExecuteGuest(pVM, pCtx))
1282 return EMSTATE_HM;
1283
1284 /*
1285 * Note! Raw mode and hw accelerated mode are incompatible. The latter
1286 * turns off monitoring features essential for raw mode!
1287 */
1288 return EMSTATE_REM;
1289 }
1290
1291 /*
1292 * Standard raw-mode:
1293 *
1294 * Here we only support 16 & 32 bits protected mode ring 3 code that has no IO privileges
1295 * or 32 bits protected mode ring 0 code
1296 *
1297 * The tests are ordered by the likelihood of being true during normal execution.
1298 */
1299 if (EFlags.u32 & (X86_EFL_TF /* | HF_INHIBIT_IRQ_MASK*/))
1300 {
1301 Log2(("raw mode refused: EFlags=%#x\n", EFlags.u32));
1302 return EMSTATE_REM;
1303 }
1304
1305# ifndef VBOX_RAW_V86
1306 if (EFlags.u32 & X86_EFL_VM) {
1307 Log2(("raw mode refused: VM_MASK\n"));
1308 return EMSTATE_REM;
1309 }
1310# endif
1311
1312 /** @todo check up the X86_CR0_AM flag in respect to raw mode!!! We're probably not emulating it right! */
1313 uint32_t u32CR0 = pCtx->cr0;
1314 if ((u32CR0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
1315 {
1316 //Log2(("raw mode refused: %s%s%s\n", (u32CR0 & X86_CR0_PG) ? "" : " !PG", (u32CR0 & X86_CR0_PE) ? "" : " !PE", (u32CR0 & X86_CR0_AM) ? "" : " !AM"));
1317 return EMSTATE_REM;
1318 }
1319
1320 if (pCtx->cr4 & X86_CR4_PAE)
1321 {
1322 uint32_t u32Dummy, u32Features;
1323
1324 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32Dummy, &u32Features);
1325 if (!(u32Features & X86_CPUID_FEATURE_EDX_PAE))
1326 return EMSTATE_REM;
1327 }
1328
1329 unsigned uSS = pCtx->ss.Sel;
1330 if ( pCtx->eflags.Bits.u1VM
1331 || (uSS & X86_SEL_RPL) == 3)
1332 {
1333 if (!EMIsRawRing3Enabled(pVM))
1334 return EMSTATE_REM;
1335
1336 if (!(EFlags.u32 & X86_EFL_IF))
1337 {
1338 Log2(("raw mode refused: IF (RawR3)\n"));
1339 return EMSTATE_REM;
1340 }
1341
1342 if (!(u32CR0 & X86_CR0_WP) && EMIsRawRing0Enabled(pVM))
1343 {
1344 Log2(("raw mode refused: CR0.WP + RawR0\n"));
1345 return EMSTATE_REM;
1346 }
1347 }
1348 else
1349 {
1350 if (!EMIsRawRing0Enabled(pVM))
1351 return EMSTATE_REM;
1352
1353 if (EMIsRawRing1Enabled(pVM))
1354 {
1355 /* Only ring 0 and 1 supervisor code. */
1356 if ((uSS & X86_SEL_RPL) == 2) /* ring 1 code is moved into ring 2, so we can't support ring-2 in that case. */
1357 {
1358 Log2(("raw r0 mode refused: CPL %d\n", uSS & X86_SEL_RPL));
1359 return EMSTATE_REM;
1360 }
1361 }
1362 /* Only ring 0 supervisor code. */
1363 else if ((uSS & X86_SEL_RPL) != 0)
1364 {
1365 Log2(("raw r0 mode refused: CPL %d\n", uSS & X86_SEL_RPL));
1366 return EMSTATE_REM;
1367 }
1368
1369 // Let's start with pure 32 bits ring 0 code first
1370 /** @todo What's pure 32-bit mode? flat? */
1371 if ( !(pCtx->ss.Attr.n.u1DefBig)
1372 || !(pCtx->cs.Attr.n.u1DefBig))
1373 {
1374 Log2(("raw r0 mode refused: SS/CS not 32bit\n"));
1375 return EMSTATE_REM;
1376 }
1377
1378 /* Write protection must be turned on, or else the guest can overwrite our hypervisor code and data. */
1379 if (!(u32CR0 & X86_CR0_WP))
1380 {
1381 Log2(("raw r0 mode refused: CR0.WP=0!\n"));
1382 return EMSTATE_REM;
1383 }
1384
1385# ifdef VBOX_WITH_RAW_MODE
1386 if (PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip))
1387 {
1388 Log2(("raw r0 mode forced: patch code\n"));
1389# ifdef VBOX_WITH_SAFE_STR
1390 Assert(pCtx->tr.Sel);
1391# endif
1392 return EMSTATE_RAW;
1393 }
1394# endif /* VBOX_WITH_RAW_MODE */
1395
1396# if !defined(VBOX_ALLOW_IF0) && !defined(VBOX_RUN_INTERRUPT_GATE_HANDLERS)
1397 if (!(EFlags.u32 & X86_EFL_IF))
1398 {
1399 ////Log2(("R0: IF=0 VIF=%d %08X\n", eip, pVMeflags));
1400 //Log2(("RR0: Interrupts turned off; fall back to emulation\n"));
1401 return EMSTATE_REM;
1402 }
1403# endif
1404
1405# ifndef VBOX_WITH_RAW_RING1
1406 /** @todo still necessary??? */
1407 if (EFlags.Bits.u2IOPL != 0)
1408 {
1409 Log2(("raw r0 mode refused: IOPL %d\n", EFlags.Bits.u2IOPL));
1410 return EMSTATE_REM;
1411 }
1412# endif
1413 }
1414
1415 /*
1416 * Stale hidden selectors means raw-mode is unsafe (being very careful).
1417 */
1418 if (pCtx->cs.fFlags & CPUMSELREG_FLAGS_STALE)
1419 {
1420 Log2(("raw mode refused: stale CS\n"));
1421 return EMSTATE_REM;
1422 }
1423 if (pCtx->ss.fFlags & CPUMSELREG_FLAGS_STALE)
1424 {
1425 Log2(("raw mode refused: stale SS\n"));
1426 return EMSTATE_REM;
1427 }
1428 if (pCtx->ds.fFlags & CPUMSELREG_FLAGS_STALE)
1429 {
1430 Log2(("raw mode refused: stale DS\n"));
1431 return EMSTATE_REM;
1432 }
1433 if (pCtx->es.fFlags & CPUMSELREG_FLAGS_STALE)
1434 {
1435 Log2(("raw mode refused: stale ES\n"));
1436 return EMSTATE_REM;
1437 }
1438 if (pCtx->fs.fFlags & CPUMSELREG_FLAGS_STALE)
1439 {
1440 Log2(("raw mode refused: stale FS\n"));
1441 return EMSTATE_REM;
1442 }
1443 if (pCtx->gs.fFlags & CPUMSELREG_FLAGS_STALE)
1444 {
1445 Log2(("raw mode refused: stale GS\n"));
1446 return EMSTATE_REM;
1447 }
1448
1449# ifdef VBOX_WITH_SAFE_STR
1450 if (pCtx->tr.Sel == 0)
1451 {
1452 Log(("Raw mode refused -> TR=0\n"));
1453 return EMSTATE_REM;
1454 }
1455# endif
1456
1457 /*Assert(PGMPhysIsA20Enabled(pVCpu));*/
1458 return EMSTATE_RAW;
1459#endif /* !IEM_VERIFICATION_MODE */
1460
1461}
1462
1463
1464/**
1465 * Executes all high priority post execution force actions.
1466 *
1467 * @returns rc or a fatal status code.
1468 *
1469 * @param pVM Pointer to the VM.
1470 * @param pVCpu Pointer to the VMCPU.
1471 * @param rc The current rc.
1472 */
1473int emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, int rc)
1474{
1475 VBOXVMM_EM_FF_HIGH(pVCpu, pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions, rc);
1476
1477 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
1478 PDMCritSectBothFF(pVCpu);
1479
1480 /* Update CR3 (Nested Paging case for HM). */
1481 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_CR3))
1482 {
1483 int rc2 = PGMUpdateCR3(pVCpu, CPUMGetGuestCR3(pVCpu));
1484 if (RT_FAILURE(rc2))
1485 return rc2;
1486 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_CR3));
1487 }
1488
1489 /* Update PAE PDPEs. This must be done *after* PGMUpdateCR3() and used only by the Nested Paging case for HM. */
1490 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_PAE_PDPES))
1491 {
1492 if (CPUMIsGuestInPAEMode(pVCpu))
1493 {
1494 PX86PDPE pPdpes = HMGetPaePdpes(pVCpu);
1495 AssertPtr(pPdpes);
1496
1497 int rc2 = PGMGstUpdatePaePdpes(pVCpu, pPdpes);
1498 if (RT_FAILURE(rc2))
1499 return rc2;
1500 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_PAE_PDPES));
1501 }
1502 else
1503 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_HM_UPDATE_PAE_PDPES);
1504 }
1505
1506#ifdef VBOX_WITH_RAW_MODE
1507 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_CSAM_PENDING_ACTION))
1508 CSAMR3DoPendingAction(pVM, pVCpu);
1509#endif
1510
1511 if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
1512 {
1513 if ( rc > VINF_EM_NO_MEMORY
1514 && rc <= VINF_EM_LAST)
1515 rc = VINF_EM_NO_MEMORY;
1516 }
1517
1518 return rc;
1519}
1520
1521
1522/**
1523 * Executes all pending forced actions.
1524 *
1525 * Forced actions can cause execution delays and execution
1526 * rescheduling. The first we deal with using action priority, so
1527 * that for instance pending timers aren't scheduled and ran until
1528 * right before execution. The rescheduling we deal with using
1529 * return codes. The same goes for VM termination, only in that case
1530 * we exit everything.
1531 *
1532 * @returns VBox status code of equal or greater importance/severity than rc.
1533 * The most important ones are: VINF_EM_RESCHEDULE,
1534 * VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
1535 *
1536 * @param pVM Pointer to the VM.
1537 * @param pVCpu Pointer to the VMCPU.
1538 * @param rc The current rc.
1539 *
1540 */
1541int emR3ForcedActions(PVM pVM, PVMCPU pVCpu, int rc)
1542{
1543 STAM_REL_PROFILE_START(&pVCpu->em.s.StatForcedActions, a);
1544#ifdef VBOX_STRICT
1545 int rcIrq = VINF_SUCCESS;
1546#endif
1547 int rc2;
1548#define UPDATE_RC() \
1549 do { \
1550 AssertMsg(rc2 <= 0 || (rc2 >= VINF_EM_FIRST && rc2 <= VINF_EM_LAST), ("Invalid FF return code: %Rra\n", rc2)); \
1551 if (rc2 == VINF_SUCCESS || rc < VINF_SUCCESS) \
1552 break; \
1553 if (!rc || rc2 < rc) \
1554 rc = rc2; \
1555 } while (0)
1556 VBOXVMM_EM_FF_ALL(pVCpu, pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions, rc);
1557
1558 /*
1559 * Post execution chunk first.
1560 */
1561 if ( VM_FF_IS_PENDING(pVM, VM_FF_NORMAL_PRIORITY_POST_MASK)
1562 || (VMCPU_FF_NORMAL_PRIORITY_POST_MASK && VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_NORMAL_PRIORITY_POST_MASK)) )
1563 {
1564 /*
1565 * EMT Rendezvous (must be serviced before termination).
1566 */
1567 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
1568 {
1569 rc2 = VMMR3EmtRendezvousFF(pVM, pVCpu);
1570 UPDATE_RC();
1571 /** @todo HACK ALERT! The following test is to make sure EM+TM
1572 * thinks the VM is stopped/reset before the next VM state change
1573 * is made. We need a better solution for this, or at least make it
1574 * possible to do: (rc >= VINF_EM_FIRST && rc <=
1575 * VINF_EM_SUSPEND). */
1576 if (RT_UNLIKELY(rc == VINF_EM_SUSPEND || rc == VINF_EM_RESET || rc == VINF_EM_OFF))
1577 {
1578 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1579 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1580 return rc;
1581 }
1582 }
1583
1584 /*
1585 * State change request (cleared by vmR3SetStateLocked).
1586 */
1587 if (VM_FF_IS_PENDING(pVM, VM_FF_CHECK_VM_STATE))
1588 {
1589 VMSTATE enmState = VMR3GetState(pVM);
1590 switch (enmState)
1591 {
1592 case VMSTATE_FATAL_ERROR:
1593 case VMSTATE_FATAL_ERROR_LS:
1594 Log2(("emR3ForcedActions: %s -> VINF_EM_SUSPEND\n", VMGetStateName(enmState) ));
1595 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1596 return VINF_EM_SUSPEND;
1597
1598 case VMSTATE_DESTROYING:
1599 Log2(("emR3ForcedActions: %s -> VINF_EM_TERMINATE\n", VMGetStateName(enmState) ));
1600 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1601 return VINF_EM_TERMINATE;
1602
1603 default:
1604 AssertMsgFailed(("%s\n", VMGetStateName(enmState)));
1605 }
1606 }
1607
1608 /*
1609 * Debugger Facility polling.
1610 */
1611 if (VM_FF_IS_PENDING(pVM, VM_FF_DBGF))
1612 {
1613 rc2 = DBGFR3VMMForcedAction(pVM);
1614 UPDATE_RC();
1615 }
1616
1617 /*
1618 * Postponed reset request.
1619 */
1620 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_RESET))
1621 {
1622 rc2 = VMR3Reset(pVM->pUVM);
1623 UPDATE_RC();
1624 }
1625
1626#ifdef VBOX_WITH_RAW_MODE
1627 /*
1628 * CSAM page scanning.
1629 */
1630 if ( !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY)
1631 && VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_CSAM_SCAN_PAGE))
1632 {
1633 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1634
1635 /** @todo: check for 16 or 32 bits code! (D bit in the code selector) */
1636 Log(("Forced action VMCPU_FF_CSAM_SCAN_PAGE\n"));
1637
1638 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
1639 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_CSAM_SCAN_PAGE);
1640 }
1641#endif
1642
1643 /*
1644 * Out of memory? Putting this after CSAM as it may in theory cause us to run out of memory.
1645 */
1646 if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
1647 {
1648 rc2 = PGMR3PhysAllocateHandyPages(pVM);
1649 UPDATE_RC();
1650 if (rc == VINF_EM_NO_MEMORY)
1651 return rc;
1652 }
1653
1654 /* check that we got them all */
1655 AssertCompile(VM_FF_NORMAL_PRIORITY_POST_MASK == (VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_PGM_NO_MEMORY | VM_FF_EMT_RENDEZVOUS));
1656 AssertCompile(VMCPU_FF_NORMAL_PRIORITY_POST_MASK == VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_SCAN_PAGE, 0));
1657 }
1658
1659 /*
1660 * Normal priority then.
1661 * (Executed in no particular order.)
1662 */
1663 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_NORMAL_PRIORITY_MASK, VM_FF_PGM_NO_MEMORY))
1664 {
1665 /*
1666 * PDM Queues are pending.
1667 */
1668 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PDM_QUEUES, VM_FF_PGM_NO_MEMORY))
1669 PDMR3QueueFlushAll(pVM);
1670
1671 /*
1672 * PDM DMA transfers are pending.
1673 */
1674 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PDM_DMA, VM_FF_PGM_NO_MEMORY))
1675 PDMR3DmaRun(pVM);
1676
1677 /*
1678 * EMT Rendezvous (make sure they are handled before the requests).
1679 */
1680 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
1681 {
1682 rc2 = VMMR3EmtRendezvousFF(pVM, pVCpu);
1683 UPDATE_RC();
1684 /** @todo HACK ALERT! The following test is to make sure EM+TM
1685 * thinks the VM is stopped/reset before the next VM state change
1686 * is made. We need a better solution for this, or at least make it
1687 * possible to do: (rc >= VINF_EM_FIRST && rc <=
1688 * VINF_EM_SUSPEND). */
1689 if (RT_UNLIKELY(rc == VINF_EM_SUSPEND || rc == VINF_EM_RESET || rc == VINF_EM_OFF))
1690 {
1691 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1692 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1693 return rc;
1694 }
1695 }
1696
1697 /*
1698 * Requests from other threads.
1699 */
1700 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_REQUEST, VM_FF_PGM_NO_MEMORY))
1701 {
1702 rc2 = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, false /*fPriorityOnly*/);
1703 if (rc2 == VINF_EM_OFF || rc2 == VINF_EM_TERMINATE) /** @todo this shouldn't be necessary */
1704 {
1705 Log2(("emR3ForcedActions: returns %Rrc\n", rc2));
1706 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1707 return rc2;
1708 }
1709 UPDATE_RC();
1710 /** @todo HACK ALERT! The following test is to make sure EM+TM
1711 * thinks the VM is stopped/reset before the next VM state change
1712 * is made. We need a better solution for this, or at least make it
1713 * possible to do: (rc >= VINF_EM_FIRST && rc <=
1714 * VINF_EM_SUSPEND). */
1715 if (RT_UNLIKELY(rc == VINF_EM_SUSPEND || rc == VINF_EM_RESET || rc == VINF_EM_OFF))
1716 {
1717 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1718 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1719 return rc;
1720 }
1721 }
1722
1723#ifdef VBOX_WITH_REM
1724 /* Replay the handler notification changes. */
1725 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_REM_HANDLER_NOTIFY, VM_FF_PGM_NO_MEMORY))
1726 {
1727 /* Try not to cause deadlocks. */
1728 if ( pVM->cCpus == 1
1729 || ( !PGMIsLockOwner(pVM)
1730 && !IOMIsLockWriteOwner(pVM))
1731 )
1732 {
1733 EMRemLock(pVM);
1734 REMR3ReplayHandlerNotifications(pVM);
1735 EMRemUnlock(pVM);
1736 }
1737 }
1738#endif
1739
1740 /* check that we got them all */
1741 AssertCompile(VM_FF_NORMAL_PRIORITY_MASK == (VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_REM_HANDLER_NOTIFY | VM_FF_EMT_RENDEZVOUS));
1742 }
1743
1744 /*
1745 * Normal priority then. (per-VCPU)
1746 * (Executed in no particular order.)
1747 */
1748 if ( !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY)
1749 && VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_NORMAL_PRIORITY_MASK))
1750 {
1751 /*
1752 * Requests from other threads.
1753 */
1754 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
1755 {
1756 rc2 = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu, false /*fPriorityOnly*/);
1757 if (rc2 == VINF_EM_OFF || rc2 == VINF_EM_TERMINATE || rc2 == VINF_EM_RESET)
1758 {
1759 Log2(("emR3ForcedActions: returns %Rrc\n", rc2));
1760 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1761 return rc2;
1762 }
1763 UPDATE_RC();
1764 /** @todo HACK ALERT! The following test is to make sure EM+TM
1765 * thinks the VM is stopped/reset before the next VM state change
1766 * is made. We need a better solution for this, or at least make it
1767 * possible to do: (rc >= VINF_EM_FIRST && rc <=
1768 * VINF_EM_SUSPEND). */
1769 if (RT_UNLIKELY(rc == VINF_EM_SUSPEND || rc == VINF_EM_RESET || rc == VINF_EM_OFF))
1770 {
1771 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1772 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1773 return rc;
1774 }
1775 }
1776
1777 /* check that we got them all */
1778 Assert(!(VMCPU_FF_NORMAL_PRIORITY_MASK & ~(VMCPU_FF_REQUEST)));
1779 }
1780
1781 /*
1782 * High priority pre execution chunk last.
1783 * (Executed in ascending priority order.)
1784 */
1785 if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_MASK)
1786 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_MASK))
1787 {
1788 /*
1789 * Timers before interrupts.
1790 */
1791 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TIMER)
1792 && !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
1793 TMR3TimerQueuesDo(pVM);
1794
1795 /*
1796 * The instruction following an emulated STI should *always* be executed!
1797 *
1798 * Note! We intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here if
1799 * the eip is the same as the inhibited instr address. Before we
1800 * are able to execute this instruction in raw mode (iret to
1801 * guest code) an external interrupt might force a world switch
1802 * again. Possibly allowing a guest interrupt to be dispatched
1803 * in the process. This could break the guest. Sounds very
1804 * unlikely, but such timing sensitive problem are not as rare as
1805 * you might think.
1806 */
1807 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1808 && !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
1809 {
1810 if (CPUMGetGuestRIP(pVCpu) != EMGetInhibitInterruptsPC(pVCpu))
1811 {
1812 Log(("Clearing VMCPU_FF_INHIBIT_INTERRUPTS at %RGv - successor %RGv\n", (RTGCPTR)CPUMGetGuestRIP(pVCpu), EMGetInhibitInterruptsPC(pVCpu)));
1813 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
1814 }
1815 else
1816 Log(("Leaving VMCPU_FF_INHIBIT_INTERRUPTS set at %RGv\n", (RTGCPTR)CPUMGetGuestRIP(pVCpu)));
1817 }
1818
1819 /*
1820 * Interrupts.
1821 */
1822 bool fWakeupPending = false;
1823 if ( !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY)
1824 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1825 && (!rc || rc >= VINF_EM_RESCHEDULE_HM)
1826 && !TRPMHasTrap(pVCpu) /* an interrupt could already be scheduled for dispatching in the recompiler. */
1827#ifdef VBOX_WITH_RAW_MODE
1828 && PATMAreInterruptsEnabled(pVM)
1829#else
1830 && (pVCpu->em.s.pCtx->eflags.u32 & X86_EFL_IF)
1831#endif
1832 && !HMR3IsEventPending(pVCpu))
1833 {
1834 Assert(pVCpu->em.s.enmState != EMSTATE_WAIT_SIPI);
1835 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
1836 {
1837 /* Note: it's important to make sure the return code from TRPMR3InjectEvent isn't ignored! */
1838 /** @todo this really isn't nice, should properly handle this */
1839 rc2 = TRPMR3InjectEvent(pVM, pVCpu, TRPM_HARDWARE_INT);
1840#ifdef VBOX_STRICT
1841 rcIrq = rc2;
1842#endif
1843 UPDATE_RC();
1844 /* Reschedule required: We must not miss the wakeup below! */
1845 fWakeupPending = true;
1846 }
1847#ifdef VBOX_WITH_REM
1848 /** @todo really ugly; if we entered the hlt state when exiting the recompiler and an interrupt was pending, we previously got stuck in the halted state. */
1849 else if (REMR3QueryPendingInterrupt(pVM, pVCpu) != REM_NO_PENDING_IRQ)
1850 {
1851 rc2 = VINF_EM_RESCHEDULE_REM;
1852 UPDATE_RC();
1853 }
1854#endif
1855 }
1856
1857 /*
1858 * Allocate handy pages.
1859 */
1860 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
1861 {
1862 rc2 = PGMR3PhysAllocateHandyPages(pVM);
1863 UPDATE_RC();
1864 }
1865
1866 /*
1867 * Debugger Facility request.
1868 */
1869 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_DBGF, VM_FF_PGM_NO_MEMORY))
1870 {
1871 rc2 = DBGFR3VMMForcedAction(pVM);
1872 UPDATE_RC();
1873 }
1874
1875 /*
1876 * EMT Rendezvous (must be serviced before termination).
1877 */
1878 if ( !fWakeupPending /* don't miss the wakeup from EMSTATE_HALTED! */
1879 && VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
1880 {
1881 rc2 = VMMR3EmtRendezvousFF(pVM, pVCpu);
1882 UPDATE_RC();
1883 /** @todo HACK ALERT! The following test is to make sure EM+TM thinks the VM is
1884 * stopped/reset before the next VM state change is made. We need a better
1885 * solution for this, or at least make it possible to do: (rc >= VINF_EM_FIRST
1886 * && rc >= VINF_EM_SUSPEND). */
1887 if (RT_UNLIKELY(rc == VINF_EM_SUSPEND || rc == VINF_EM_RESET || rc == VINF_EM_OFF))
1888 {
1889 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1890 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1891 return rc;
1892 }
1893 }
1894
1895 /*
1896 * State change request (cleared by vmR3SetStateLocked).
1897 */
1898 if ( !fWakeupPending /* don't miss the wakeup from EMSTATE_HALTED! */
1899 && VM_FF_IS_PENDING(pVM, VM_FF_CHECK_VM_STATE))
1900 {
1901 VMSTATE enmState = VMR3GetState(pVM);
1902 switch (enmState)
1903 {
1904 case VMSTATE_FATAL_ERROR:
1905 case VMSTATE_FATAL_ERROR_LS:
1906 Log2(("emR3ForcedActions: %s -> VINF_EM_SUSPEND\n", VMGetStateName(enmState) ));
1907 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1908 return VINF_EM_SUSPEND;
1909
1910 case VMSTATE_DESTROYING:
1911 Log2(("emR3ForcedActions: %s -> VINF_EM_TERMINATE\n", VMGetStateName(enmState) ));
1912 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1913 return VINF_EM_TERMINATE;
1914
1915 default:
1916 AssertMsgFailed(("%s\n", VMGetStateName(enmState)));
1917 }
1918 }
1919
1920 /*
1921 * Out of memory? Since most of our fellow high priority actions may cause us
1922 * to run out of memory, we're employing VM_FF_IS_PENDING_EXCEPT and putting this
1923 * at the end rather than the start. Also, VM_FF_TERMINATE has higher priority
1924 * than us since we can terminate without allocating more memory.
1925 */
1926 if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
1927 {
1928 rc2 = PGMR3PhysAllocateHandyPages(pVM);
1929 UPDATE_RC();
1930 if (rc == VINF_EM_NO_MEMORY)
1931 return rc;
1932 }
1933
1934 /*
1935 * If the virtual sync clock is still stopped, make TM restart it.
1936 */
1937 if (VM_FF_IS_PENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
1938 TMR3VirtualSyncFF(pVM, pVCpu);
1939
1940#ifdef DEBUG
1941 /*
1942 * Debug, pause the VM.
1943 */
1944 if (VM_FF_IS_PENDING(pVM, VM_FF_DEBUG_SUSPEND))
1945 {
1946 VM_FF_CLEAR(pVM, VM_FF_DEBUG_SUSPEND);
1947 Log(("emR3ForcedActions: returns VINF_EM_SUSPEND\n"));
1948 return VINF_EM_SUSPEND;
1949 }
1950#endif
1951
1952 /* check that we got them all */
1953 AssertCompile(VM_FF_HIGH_PRIORITY_PRE_MASK == (VM_FF_TM_VIRTUAL_SYNC | VM_FF_DBGF | VM_FF_CHECK_VM_STATE | VM_FF_DEBUG_SUSPEND | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY | VM_FF_EMT_RENDEZVOUS));
1954 AssertCompile(VMCPU_FF_HIGH_PRIORITY_PRE_MASK == (VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_INHIBIT_INTERRUPTS | VM_WHEN_RAW_MODE(VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT, 0)));
1955 }
1956
1957#undef UPDATE_RC
1958 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
1959 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
1960 Assert(rcIrq == VINF_SUCCESS || rcIrq == rc);
1961 return rc;
1962}
1963
1964
1965/**
1966 * Check if the preset execution time cap restricts guest execution scheduling.
1967 *
1968 * @returns true if allowed, false otherwise
1969 * @param pVM Pointer to the VM.
1970 * @param pVCpu Pointer to the VMCPU.
1971 */
1972bool emR3IsExecutionAllowed(PVM pVM, PVMCPU pVCpu)
1973{
1974 uint64_t u64UserTime, u64KernelTime;
1975
1976 if ( pVM->uCpuExecutionCap != 100
1977 && RT_SUCCESS(RTThreadGetExecutionTimeMilli(&u64KernelTime, &u64UserTime)))
1978 {
1979 uint64_t u64TimeNow = RTTimeMilliTS();
1980 if (pVCpu->em.s.u64TimeSliceStart + EM_TIME_SLICE < u64TimeNow)
1981 {
1982 /* New time slice. */
1983 pVCpu->em.s.u64TimeSliceStart = u64TimeNow;
1984 pVCpu->em.s.u64TimeSliceStartExec = u64KernelTime + u64UserTime;
1985 pVCpu->em.s.u64TimeSliceExec = 0;
1986 }
1987 pVCpu->em.s.u64TimeSliceExec = u64KernelTime + u64UserTime - pVCpu->em.s.u64TimeSliceStartExec;
1988
1989 Log2(("emR3IsExecutionAllowed: start=%RX64 startexec=%RX64 exec=%RX64 (cap=%x)\n", pVCpu->em.s.u64TimeSliceStart, pVCpu->em.s.u64TimeSliceStartExec, pVCpu->em.s.u64TimeSliceExec, (EM_TIME_SLICE * pVM->uCpuExecutionCap) / 100));
1990 if (pVCpu->em.s.u64TimeSliceExec >= (EM_TIME_SLICE * pVM->uCpuExecutionCap) / 100)
1991 return false;
1992 }
1993 return true;
1994}
1995
1996
1997/**
1998 * Execute VM.
1999 *
2000 * This function is the main loop of the VM. The emulation thread
2001 * calls this function when the VM has been successfully constructed
2002 * and we're ready for executing the VM.
2003 *
2004 * Returning from this function means that the VM is turned off or
2005 * suspended (state already saved) and deconstruction is next in line.
2006 *
2007 * All interaction from other thread are done using forced actions
2008 * and signaling of the wait object.
2009 *
2010 * @returns VBox status code, informational status codes may indicate failure.
2011 * @param pVM Pointer to the VM.
2012 * @param pVCpu Pointer to the VMCPU.
2013 */
2014VMMR3_INT_DECL(int) EMR3ExecuteVM(PVM pVM, PVMCPU pVCpu)
2015{
2016 Log(("EMR3ExecuteVM: pVM=%p enmVMState=%d (%s) enmState=%d (%s) enmPrevState=%d (%s) fForceRAW=%RTbool\n",
2017 pVM,
2018 pVM->enmVMState, VMR3GetStateName(pVM->enmVMState),
2019 pVCpu->em.s.enmState, emR3GetStateName(pVCpu->em.s.enmState),
2020 pVCpu->em.s.enmPrevState, emR3GetStateName(pVCpu->em.s.enmPrevState),
2021 pVCpu->em.s.fForceRAW));
2022 VM_ASSERT_EMT(pVM);
2023 AssertMsg( pVCpu->em.s.enmState == EMSTATE_NONE
2024 || pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI
2025 || pVCpu->em.s.enmState == EMSTATE_SUSPENDED,
2026 ("%s\n", emR3GetStateName(pVCpu->em.s.enmState)));
2027
2028 int rc = setjmp(pVCpu->em.s.u.FatalLongJump);
2029 if (rc == 0)
2030 {
2031 /*
2032 * Start the virtual time.
2033 */
2034 TMR3NotifyResume(pVM, pVCpu);
2035
2036 /*
2037 * The Outer Main Loop.
2038 */
2039 bool fFFDone = false;
2040
2041 /* Reschedule right away to start in the right state. */
2042 rc = VINF_SUCCESS;
2043
2044 /* If resuming after a pause or a state load, restore the previous
2045 state or else we'll start executing code. Else, just reschedule. */
2046 if ( pVCpu->em.s.enmState == EMSTATE_SUSPENDED
2047 && ( pVCpu->em.s.enmPrevState == EMSTATE_WAIT_SIPI
2048 || pVCpu->em.s.enmPrevState == EMSTATE_HALTED))
2049 pVCpu->em.s.enmState = pVCpu->em.s.enmPrevState;
2050 else
2051 pVCpu->em.s.enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
2052
2053 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
2054 for (;;)
2055 {
2056 /*
2057 * Before we can schedule anything (we're here because
2058 * scheduling is required) we must service any pending
2059 * forced actions to avoid any pending action causing
2060 * immediate rescheduling upon entering an inner loop
2061 *
2062 * Do forced actions.
2063 */
2064 if ( !fFFDone
2065 && RT_SUCCESS(rc)
2066 && rc != VINF_EM_TERMINATE
2067 && rc != VINF_EM_OFF
2068 && ( VM_FF_IS_PENDING(pVM, VM_FF_ALL_REM_MASK)
2069 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_ALL_REM_MASK)))
2070 {
2071 rc = emR3ForcedActions(pVM, pVCpu, rc);
2072 VBOXVMM_EM_FF_ALL_RET(pVCpu, rc);
2073 if ( ( rc == VINF_EM_RESCHEDULE_REM
2074 || rc == VINF_EM_RESCHEDULE_HM)
2075 && pVCpu->em.s.fForceRAW)
2076 rc = VINF_EM_RESCHEDULE_RAW;
2077 }
2078 else if (fFFDone)
2079 fFFDone = false;
2080
2081 /*
2082 * Now what to do?
2083 */
2084 Log2(("EMR3ExecuteVM: rc=%Rrc\n", rc));
2085 EMSTATE const enmOldState = pVCpu->em.s.enmState;
2086 switch (rc)
2087 {
2088 /*
2089 * Keep doing what we're currently doing.
2090 */
2091 case VINF_SUCCESS:
2092 break;
2093
2094 /*
2095 * Reschedule - to raw-mode execution.
2096 */
2097 case VINF_EM_RESCHEDULE_RAW:
2098 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_RAW: %d -> %d (EMSTATE_RAW)\n", enmOldState, EMSTATE_RAW));
2099 pVCpu->em.s.enmState = EMSTATE_RAW;
2100 break;
2101
2102 /*
2103 * Reschedule - to hardware accelerated raw-mode execution.
2104 */
2105 case VINF_EM_RESCHEDULE_HM:
2106 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_HM: %d -> %d (EMSTATE_HM)\n", enmOldState, EMSTATE_HM));
2107 Assert(!pVCpu->em.s.fForceRAW);
2108 pVCpu->em.s.enmState = EMSTATE_HM;
2109 break;
2110
2111 /*
2112 * Reschedule - to recompiled execution.
2113 */
2114 case VINF_EM_RESCHEDULE_REM:
2115 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_REM: %d -> %d (EMSTATE_REM)\n", enmOldState, EMSTATE_REM));
2116 pVCpu->em.s.enmState = EMSTATE_REM;
2117 break;
2118
2119 /*
2120 * Resume.
2121 */
2122 case VINF_EM_RESUME:
2123 Log2(("EMR3ExecuteVM: VINF_EM_RESUME: %d -> VINF_EM_RESCHEDULE\n", enmOldState));
2124 /* Don't reschedule in the halted or wait for SIPI case. */
2125 if ( pVCpu->em.s.enmPrevState == EMSTATE_WAIT_SIPI
2126 || pVCpu->em.s.enmPrevState == EMSTATE_HALTED)
2127 {
2128 pVCpu->em.s.enmState = pVCpu->em.s.enmPrevState;
2129 break;
2130 }
2131 /* fall through and get scheduled. */
2132
2133 /*
2134 * Reschedule.
2135 */
2136 case VINF_EM_RESCHEDULE:
2137 {
2138 EMSTATE enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
2139 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE: %d -> %d (%s)\n", enmOldState, enmState, emR3GetStateName(enmState)));
2140 pVCpu->em.s.enmState = enmState;
2141 break;
2142 }
2143
2144 /*
2145 * Halted.
2146 */
2147 case VINF_EM_HALT:
2148 Log2(("EMR3ExecuteVM: VINF_EM_HALT: %d -> %d\n", enmOldState, EMSTATE_HALTED));
2149 pVCpu->em.s.enmState = EMSTATE_HALTED;
2150 break;
2151
2152 /*
2153 * Switch to the wait for SIPI state (application processor only)
2154 */
2155 case VINF_EM_WAIT_SIPI:
2156 Assert(pVCpu->idCpu != 0);
2157 Log2(("EMR3ExecuteVM: VINF_EM_WAIT_SIPI: %d -> %d\n", enmOldState, EMSTATE_WAIT_SIPI));
2158 pVCpu->em.s.enmState = EMSTATE_WAIT_SIPI;
2159 break;
2160
2161
2162 /*
2163 * Suspend.
2164 */
2165 case VINF_EM_SUSPEND:
2166 Log2(("EMR3ExecuteVM: VINF_EM_SUSPEND: %d -> %d\n", enmOldState, EMSTATE_SUSPENDED));
2167 Assert(enmOldState != EMSTATE_SUSPENDED);
2168 pVCpu->em.s.enmPrevState = enmOldState;
2169 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
2170 break;
2171
2172 /*
2173 * Reset.
2174 * We might end up doing a double reset for now, we'll have to clean up the mess later.
2175 */
2176 case VINF_EM_RESET:
2177 {
2178 if (pVCpu->idCpu == 0)
2179 {
2180 EMSTATE enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
2181 Log2(("EMR3ExecuteVM: VINF_EM_RESET: %d -> %d (%s)\n", enmOldState, enmState, emR3GetStateName(enmState)));
2182 pVCpu->em.s.enmState = enmState;
2183 }
2184 else
2185 {
2186 /* All other VCPUs go into the wait for SIPI state. */
2187 pVCpu->em.s.enmState = EMSTATE_WAIT_SIPI;
2188 }
2189 break;
2190 }
2191
2192 /*
2193 * Power Off.
2194 */
2195 case VINF_EM_OFF:
2196 pVCpu->em.s.enmState = EMSTATE_TERMINATING;
2197 Log2(("EMR3ExecuteVM: returns VINF_EM_OFF (%d -> %d)\n", enmOldState, EMSTATE_TERMINATING));
2198 TMR3NotifySuspend(pVM, pVCpu);
2199 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2200 return rc;
2201
2202 /*
2203 * Terminate the VM.
2204 */
2205 case VINF_EM_TERMINATE:
2206 pVCpu->em.s.enmState = EMSTATE_TERMINATING;
2207 Log(("EMR3ExecuteVM returns VINF_EM_TERMINATE (%d -> %d)\n", enmOldState, EMSTATE_TERMINATING));
2208 if (pVM->enmVMState < VMSTATE_DESTROYING) /* ugly */
2209 TMR3NotifySuspend(pVM, pVCpu);
2210 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2211 return rc;
2212
2213
2214 /*
2215 * Out of memory, suspend the VM and stuff.
2216 */
2217 case VINF_EM_NO_MEMORY:
2218 Log2(("EMR3ExecuteVM: VINF_EM_NO_MEMORY: %d -> %d\n", enmOldState, EMSTATE_SUSPENDED));
2219 Assert(enmOldState != EMSTATE_SUSPENDED);
2220 pVCpu->em.s.enmPrevState = enmOldState;
2221 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
2222 TMR3NotifySuspend(pVM, pVCpu);
2223 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2224
2225 rc = VMSetRuntimeError(pVM, VMSETRTERR_FLAGS_SUSPEND, "HostMemoryLow",
2226 N_("Unable to allocate and lock memory. The virtual machine will be paused. Please close applications to free up memory or close the VM"));
2227 if (rc != VINF_EM_SUSPEND)
2228 {
2229 if (RT_SUCCESS_NP(rc))
2230 {
2231 AssertLogRelMsgFailed(("%Rrc\n", rc));
2232 rc = VERR_EM_INTERNAL_ERROR;
2233 }
2234 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
2235 }
2236 return rc;
2237
2238 /*
2239 * Guest debug events.
2240 */
2241 case VINF_EM_DBG_STEPPED:
2242 AssertMsgFailed(("VINF_EM_DBG_STEPPED cannot be here!"));
2243 case VINF_EM_DBG_STOP:
2244 case VINF_EM_DBG_BREAKPOINT:
2245 case VINF_EM_DBG_STEP:
2246 if (enmOldState == EMSTATE_RAW)
2247 {
2248 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, enmOldState, EMSTATE_DEBUG_GUEST_RAW));
2249 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
2250 }
2251 else
2252 {
2253 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, enmOldState, EMSTATE_DEBUG_GUEST_REM));
2254 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_REM;
2255 }
2256 break;
2257
2258 /*
2259 * Hypervisor debug events.
2260 */
2261 case VINF_EM_DBG_HYPER_STEPPED:
2262 case VINF_EM_DBG_HYPER_BREAKPOINT:
2263 case VINF_EM_DBG_HYPER_ASSERTION:
2264 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, enmOldState, EMSTATE_DEBUG_HYPER));
2265 pVCpu->em.s.enmState = EMSTATE_DEBUG_HYPER;
2266 break;
2267
2268 /*
2269 * Guru mediations.
2270 */
2271 case VERR_VMM_RING0_ASSERTION:
2272 case VINF_EM_TRIPLE_FAULT:
2273 Log(("EMR3ExecuteVM: %Rrc: %d -> %d (EMSTATE_GURU_MEDITATION)\n", rc, enmOldState, EMSTATE_GURU_MEDITATION));
2274 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
2275 break;
2276
2277 /*
2278 * Any error code showing up here other than the ones we
2279 * know and process above are considered to be FATAL.
2280 *
2281 * Unknown warnings and informational status codes are also
2282 * included in this.
2283 */
2284 default:
2285 if (RT_SUCCESS_NP(rc))
2286 {
2287 AssertMsgFailed(("Unexpected warning or informational status code %Rra!\n", rc));
2288 rc = VERR_EM_INTERNAL_ERROR;
2289 }
2290 Log(("EMR3ExecuteVM: %Rrc: %d -> %d (EMSTATE_GURU_MEDITATION)\n", rc, enmOldState, EMSTATE_GURU_MEDITATION));
2291 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
2292 break;
2293 }
2294
2295 /*
2296 * Act on state transition.
2297 */
2298 EMSTATE const enmNewState = pVCpu->em.s.enmState;
2299 if (enmOldState != enmNewState)
2300 {
2301 VBOXVMM_EM_STATE_CHANGED(pVCpu, enmOldState, enmNewState, rc);
2302
2303 /* Clear MWait flags. */
2304 if ( enmOldState == EMSTATE_HALTED
2305 && (pVCpu->em.s.MWait.fWait & EMMWAIT_FLAG_ACTIVE)
2306 && ( enmNewState == EMSTATE_RAW
2307 || enmNewState == EMSTATE_HM
2308 || enmNewState == EMSTATE_REM
2309 || enmNewState == EMSTATE_DEBUG_GUEST_RAW
2310 || enmNewState == EMSTATE_DEBUG_GUEST_HM
2311 || enmNewState == EMSTATE_DEBUG_GUEST_REM) )
2312 {
2313 LogFlow(("EMR3ExecuteVM: Clearing MWAIT\n"));
2314 pVCpu->em.s.MWait.fWait &= ~(EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0);
2315 }
2316 }
2317 else
2318 VBOXVMM_EM_STATE_UNCHANGED(pVCpu, enmNewState, rc);
2319
2320 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x); /* (skip this in release) */
2321 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
2322
2323 /*
2324 * Act on the new state.
2325 */
2326 switch (enmNewState)
2327 {
2328 /*
2329 * Execute raw.
2330 */
2331 case EMSTATE_RAW:
2332#ifndef IEM_VERIFICATION_MODE /* remove later */
2333# ifdef VBOX_WITH_RAW_MODE
2334 rc = emR3RawExecute(pVM, pVCpu, &fFFDone);
2335# else
2336 AssertLogRelMsgFailed(("%Rrc\n", rc));
2337 rc = VERR_EM_INTERNAL_ERROR;
2338# endif
2339 break;
2340#endif
2341
2342 /*
2343 * Execute hardware accelerated raw.
2344 */
2345 case EMSTATE_HM:
2346#ifndef IEM_VERIFICATION_MODE /* remove later */
2347 rc = emR3HmExecute(pVM, pVCpu, &fFFDone);
2348 break;
2349#endif
2350
2351 /*
2352 * Execute recompiled.
2353 */
2354 case EMSTATE_REM:
2355#ifdef IEM_VERIFICATION_MODE
2356# if 1
2357 rc = VBOXSTRICTRC_TODO(IEMExecOne(pVCpu)); fFFDone = false;
2358# else
2359 rc = VBOXSTRICTRC_TODO(REMR3EmulateInstruction(pVM, pVCpu)); fFFDone = false;
2360 if (rc == VINF_EM_RESCHEDULE)
2361 rc = VINF_SUCCESS;
2362# endif
2363#else
2364 rc = emR3RemExecute(pVM, pVCpu, &fFFDone);
2365#endif
2366 Log2(("EMR3ExecuteVM: emR3RemExecute -> %Rrc\n", rc));
2367 break;
2368
2369 /*
2370 * Execute in the interpreter.
2371 */
2372 case EMSTATE_IEM:
2373 rc = VBOXSTRICTRC_TODO(IEMExecLots(pVCpu));
2374 fFFDone = false;
2375 break;
2376
2377 /*
2378 * Application processor execution halted until SIPI.
2379 */
2380 case EMSTATE_WAIT_SIPI:
2381 /* no break */
2382 /*
2383 * hlt - execution halted until interrupt.
2384 */
2385 case EMSTATE_HALTED:
2386 {
2387 STAM_REL_PROFILE_START(&pVCpu->em.s.StatHalted, y);
2388 /* MWAIT has a special extension where it's woken up when
2389 an interrupt is pending even when IF=0. */
2390 if ( (pVCpu->em.s.MWait.fWait & (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0))
2391 == (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0))
2392 {
2393 rc = VMR3WaitHalted(pVM, pVCpu, false /*fIgnoreInterrupts*/);
2394 if ( rc == VINF_SUCCESS
2395 && VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
2396 {
2397 Log(("EMR3ExecuteVM: Triggering reschedule on pending IRQ after MWAIT\n"));
2398 rc = VINF_EM_RESCHEDULE;
2399 }
2400 }
2401 else
2402 rc = VMR3WaitHalted(pVM, pVCpu, !(CPUMGetGuestEFlags(pVCpu) & X86_EFL_IF));
2403
2404 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatHalted, y);
2405 break;
2406 }
2407
2408 /*
2409 * Suspended - return to VM.cpp.
2410 */
2411 case EMSTATE_SUSPENDED:
2412 TMR3NotifySuspend(pVM, pVCpu);
2413 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2414 Log(("EMR3ExecuteVM: actually returns %Rrc (state %s / %s)\n", rc, emR3GetStateName(pVCpu->em.s.enmState), emR3GetStateName(enmOldState)));
2415 return VINF_EM_SUSPEND;
2416
2417 /*
2418 * Debugging in the guest.
2419 */
2420 case EMSTATE_DEBUG_GUEST_REM:
2421 case EMSTATE_DEBUG_GUEST_RAW:
2422 TMR3NotifySuspend(pVM, pVCpu);
2423 rc = emR3Debug(pVM, pVCpu, rc);
2424 TMR3NotifyResume(pVM, pVCpu);
2425 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVCpu->em.s.enmState));
2426 break;
2427
2428 /*
2429 * Debugging in the hypervisor.
2430 */
2431 case EMSTATE_DEBUG_HYPER:
2432 {
2433 TMR3NotifySuspend(pVM, pVCpu);
2434 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2435
2436 rc = emR3Debug(pVM, pVCpu, rc);
2437 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVCpu->em.s.enmState));
2438 if (rc != VINF_SUCCESS)
2439 {
2440 if (rc == VINF_EM_OFF || rc == VINF_EM_TERMINATE)
2441 pVCpu->em.s.enmState = EMSTATE_TERMINATING;
2442 else
2443 {
2444 /* switch to guru meditation mode */
2445 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
2446 VMMR3FatalDump(pVM, pVCpu, rc);
2447 }
2448 Log(("EMR3ExecuteVM: actually returns %Rrc (state %s / %s)\n", rc, emR3GetStateName(pVCpu->em.s.enmState), emR3GetStateName(enmOldState)));
2449 return rc;
2450 }
2451
2452 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
2453 TMR3NotifyResume(pVM, pVCpu);
2454 break;
2455 }
2456
2457 /*
2458 * Guru meditation takes place in the debugger.
2459 */
2460 case EMSTATE_GURU_MEDITATION:
2461 {
2462 TMR3NotifySuspend(pVM, pVCpu);
2463 VMMR3FatalDump(pVM, pVCpu, rc);
2464 emR3Debug(pVM, pVCpu, rc);
2465 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2466 Log(("EMR3ExecuteVM: actually returns %Rrc (state %s / %s)\n", rc, emR3GetStateName(pVCpu->em.s.enmState), emR3GetStateName(enmOldState)));
2467 return rc;
2468 }
2469
2470 /*
2471 * The states we don't expect here.
2472 */
2473 case EMSTATE_NONE:
2474 case EMSTATE_TERMINATING:
2475 default:
2476 AssertMsgFailed(("EMR3ExecuteVM: Invalid state %d!\n", pVCpu->em.s.enmState));
2477 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
2478 TMR3NotifySuspend(pVM, pVCpu);
2479 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2480 Log(("EMR3ExecuteVM: actually returns %Rrc (state %s / %s)\n", rc, emR3GetStateName(pVCpu->em.s.enmState), emR3GetStateName(enmOldState)));
2481 return VERR_EM_INTERNAL_ERROR;
2482 }
2483 } /* The Outer Main Loop */
2484 }
2485 else
2486 {
2487 /*
2488 * Fatal error.
2489 */
2490 Log(("EMR3ExecuteVM: returns %Rrc because of longjmp / fatal error; (state %s / %s)\n", rc, emR3GetStateName(pVCpu->em.s.enmState), emR3GetStateName(pVCpu->em.s.enmPrevState)));
2491 TMR3NotifySuspend(pVM, pVCpu);
2492 VMMR3FatalDump(pVM, pVCpu, rc);
2493 emR3Debug(pVM, pVCpu, rc);
2494 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
2495 /** @todo change the VM state! */
2496 return rc;
2497 }
2498
2499 /* (won't ever get here). */
2500 AssertFailed();
2501}
2502
2503/**
2504 * Notify EM of a state change (used by FTM)
2505 *
2506 * @param pVM Pointer to the VM.
2507 */
2508VMMR3_INT_DECL(int) EMR3NotifySuspend(PVM pVM)
2509{
2510 PVMCPU pVCpu = VMMGetCpu(pVM);
2511
2512 TMR3NotifySuspend(pVM, pVCpu); /* Stop the virtual time. */
2513 pVCpu->em.s.enmPrevState = pVCpu->em.s.enmState;
2514 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
2515 return VINF_SUCCESS;
2516}
2517
2518/**
2519 * Notify EM of a state change (used by FTM)
2520 *
2521 * @param pVM Pointer to the VM.
2522 */
2523VMMR3_INT_DECL(int) EMR3NotifyResume(PVM pVM)
2524{
2525 PVMCPU pVCpu = VMMGetCpu(pVM);
2526 EMSTATE enmCurState = pVCpu->em.s.enmState;
2527
2528 TMR3NotifyResume(pVM, pVCpu); /* Resume the virtual time. */
2529 pVCpu->em.s.enmState = pVCpu->em.s.enmPrevState;
2530 pVCpu->em.s.enmPrevState = enmCurState;
2531 return VINF_SUCCESS;
2532}
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