VirtualBox

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

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

SSM,*: Renamed phase to pass (uPhase/SSM_PHASE_FINAL) and wrote the remainder of the live snapshot / migration SSM code.

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