VirtualBox

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

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

FE/Qt,FE/BFE,MachineDebugger,EM: Added execution scheduling options to the Qt GUI and reworked the main/VMM interface.

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

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