VirtualBox

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

Last change on this file since 47660 was 47660, checked in by vboxsync, 11 years ago

VMM: Debug register handling redo. (only partly tested on AMD-V so far.)

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