VirtualBox

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

最後變更 在這個檔案從44358是 44340,由 vboxsync 提交於 12 年 前

VMM,Main,Debugger,REM: VM API cleanup, prefering PUVM over PVM so we can use real reference counting and not have the memory backing the VM structure disappear on us.

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

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