VirtualBox

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

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

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