VirtualBox

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

Last change on this file since 40266 was 39944, checked in by vboxsync, 13 years ago

VERR_IEM_INSTR_NOT_IMPLEMENTED and VERR_IEM_ASPECT_NOT_IMPLEMENTED.

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

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