VirtualBox

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

Last change on this file since 44124 was 43872, checked in by vboxsync, 12 years ago

Make VBOX_WITH_RAW_MODE= link.

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

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