VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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