VirtualBox

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

Last change on this file since 19920 was 19873, checked in by vboxsync, 16 years ago

Removed annoying assertion (hit when rebooting)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 164.5 KB
Line 
1/* $Id: EM.cpp 19873 2009-05-20 15:17:26Z vboxsync $ */
2/** @file
3 * EM - Execution Monitor / Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/** @page pg_em EM - The Execution Monitor / Manager
23 *
24 * The Execution Monitor/Manager is responsible for running the VM, scheduling
25 * the right kind of execution (Raw-mode, Hardware Assisted, Recompiled or
26 * Interpreted), and keeping the CPU states in sync. The function
27 * EMR3ExecuteVM() is the 'main-loop' of the VM, while each of the execution
28 * modes has different inner loops (emR3RawExecute, emR3HwAccExecute, and
29 * emR3RemExecute).
30 *
31 * The interpreted execution is only used to avoid switching between
32 * raw-mode/hwaccm and the recompiler when fielding virtualization traps/faults.
33 * The interpretation is thus implemented as part of EM.
34 *
35 * @see grp_em
36 */
37
38/*******************************************************************************
39* Header Files *
40*******************************************************************************/
41#define LOG_GROUP LOG_GROUP_EM
42#include <VBox/em.h>
43#include <VBox/vmm.h>
44#ifdef VBOX_WITH_VMI
45# include <VBox/parav.h>
46#endif
47#include <VBox/patm.h>
48#include <VBox/csam.h>
49#include <VBox/selm.h>
50#include <VBox/trpm.h>
51#include <VBox/iom.h>
52#include <VBox/dbgf.h>
53#include <VBox/pgm.h>
54#include <VBox/rem.h>
55#include <VBox/tm.h>
56#include <VBox/mm.h>
57#include <VBox/ssm.h>
58#include <VBox/pdmapi.h>
59#include <VBox/pdmcritsect.h>
60#include <VBox/pdmqueue.h>
61#include <VBox/hwaccm.h>
62#include <VBox/patm.h>
63#include "EMInternal.h"
64#include <VBox/vm.h>
65#include <VBox/cpumdis.h>
66#include <VBox/dis.h>
67#include <VBox/disopcode.h>
68#include <VBox/dbgf.h>
69
70#include <VBox/log.h>
71#include <iprt/thread.h>
72#include <iprt/assert.h>
73#include <iprt/asm.h>
74#include <iprt/semaphore.h>
75#include <iprt/string.h>
76#include <iprt/avl.h>
77#include <iprt/stream.h>
78#include <VBox/param.h>
79#include <VBox/err.h>
80
81
82/*******************************************************************************
83* Defined Constants And Macros *
84*******************************************************************************/
85#if 0 /* Disabled till after 2.1.0 when we've time to test it. */
86#define EM_NOTIFY_HWACCM
87#endif
88
89
90/*******************************************************************************
91* Internal Functions *
92*******************************************************************************/
93static DECLCALLBACK(int) emR3Save(PVM pVM, PSSMHANDLE pSSM);
94static DECLCALLBACK(int) emR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
95static int emR3Debug(PVM pVM, PVMCPU pVCpu, int rc);
96static int emR3RemStep(PVM pVM, PVMCPU pVCpu);
97static int emR3RemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
98static int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu);
99static int emR3RawStep(PVM pVM, PVMCPU pVCpu);
100DECLINLINE(int) emR3RawHandleRC(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc);
101DECLINLINE(int) emR3RawUpdateForceFlag(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc);
102static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
103static int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone);
104DECLINLINE(int) emR3RawExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC = VINF_SUCCESS);
105static int emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, int rc);
106static int emR3ForcedActions(PVM pVM, PVMCPU pVCpu, int rc);
107static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu);
108static int emR3PatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret);
109static int emR3SingleStepExecRem(PVM pVM, uint32_t cIterations);
110static EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
111static void emR3RemLock(PVM pVM);
112static void emR3RemUnlock(PVM pVM);
113
114/**
115 * Initializes the EM.
116 *
117 * @returns VBox status code.
118 * @param pVM The VM to operate on.
119 */
120VMMR3DECL(int) EMR3Init(PVM pVM)
121{
122 LogFlow(("EMR3Init\n"));
123 /*
124 * Assert alignment and sizes.
125 */
126 AssertCompileMemberAlignment(VM, em.s, 32);
127 AssertCompile(sizeof(pVM->em.s) <= sizeof(pVM->em.padding));
128 AssertCompile(sizeof(pVM->aCpus[0].em.s.u.FatalLongJump) <= sizeof(pVM->aCpus[0].em.s.u.achPaddingFatalLongJump));
129 AssertCompileMemberAlignment(EM, CritSectREM, sizeof(uintptr_t));
130
131 /*
132 * Init the structure.
133 */
134 pVM->em.s.offVM = RT_OFFSETOF(VM, em.s);
135 int rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RawR3Enabled", &pVM->fRawR3Enabled);
136 if (RT_FAILURE(rc))
137 pVM->fRawR3Enabled = true;
138 rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RawR0Enabled", &pVM->fRawR0Enabled);
139 if (RT_FAILURE(rc))
140 pVM->fRawR0Enabled = true;
141 Log(("EMR3Init: fRawR3Enabled=%d fRawR0Enabled=%d\n", pVM->fRawR3Enabled, pVM->fRawR0Enabled));
142
143 /*
144 * Initialize the REM critical section.
145 */
146 rc = PDMR3CritSectInit(pVM, &pVM->em.s.CritSectREM, "EM-REM");
147 AssertRCReturn(rc, rc);
148
149 /*
150 * Saved state.
151 */
152 rc = SSMR3RegisterInternal(pVM, "em", 0, EM_SAVED_STATE_VERSION, 16,
153 NULL, emR3Save, NULL,
154 NULL, emR3Load, NULL);
155 if (RT_FAILURE(rc))
156 return rc;
157
158 for (unsigned i=0;i<pVM->cCPUs;i++)
159 {
160 PVMCPU pVCpu = &pVM->aCpus[i];
161
162 pVCpu->em.s.offVMCPU = RT_OFFSETOF(VMCPU, em.s);
163
164 pVCpu->em.s.enmState = (i == 0) ? EMSTATE_NONE : EMSTATE_WAIT_SIPI;
165 pVCpu->em.s.enmPrevState = EMSTATE_NONE;
166 pVCpu->em.s.fForceRAW = false;
167
168 pVCpu->em.s.pCtx = CPUMQueryGuestCtxPtr(pVCpu);
169 pVCpu->em.s.pPatmGCState = PATMR3QueryGCStateHC(pVM);
170 AssertMsg(pVCpu->em.s.pPatmGCState, ("PATMR3QueryGCStateHC failed!\n"));
171
172# define EM_REG_COUNTER(a, b, c) \
173 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, c, b, i); \
174 AssertRC(rc);
175
176# define EM_REG_COUNTER_USED(a, b, c) \
177 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, c, b, i); \
178 AssertRC(rc);
179
180# define EM_REG_PROFILE(a, b, c) \
181 rc = STAMR3RegisterF(pVM, a, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, c, b, i); \
182 AssertRC(rc);
183
184# define EM_REG_PROFILE_ADV(a, b, c) \
185 rc = STAMR3RegisterF(pVM, a, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, c, b, i); \
186 AssertRC(rc);
187
188 /*
189 * Statistics.
190 */
191#ifdef VBOX_WITH_STATISTICS
192 PEMSTATS pStats;
193 rc = MMHyperAlloc(pVM, sizeof(*pStats), 0, MM_TAG_EM, (void **)&pStats);
194 if (RT_FAILURE(rc))
195 return rc;
196
197 pVCpu->em.s.pStatsR3 = pStats;
198 pVCpu->em.s.pStatsR0 = MMHyperR3ToR0(pVM, pStats);
199 pVCpu->em.s.pStatsRC = MMHyperR3ToRC(pVM, pStats);
200
201 EM_REG_PROFILE(&pStats->StatRZEmulate, "/EM/CPU%d/RZ/Interpret", "Profiling of EMInterpretInstruction.");
202 EM_REG_PROFILE(&pStats->StatR3Emulate, "/EM/CPU%d/R3/Interpret", "Profiling of EMInterpretInstruction.");
203
204 EM_REG_PROFILE(&pStats->StatRZInterpretSucceeded, "/EM/CPU%d/RZ/Interpret/Success", "The number of times an instruction was successfully interpreted.");
205 EM_REG_PROFILE(&pStats->StatR3InterpretSucceeded, "/EM/CPU%d/R3/Interpret/Success", "The number of times an instruction was successfully interpreted.");
206
207 EM_REG_COUNTER_USED(&pStats->StatRZAnd, "/EM/CPU%d/RZ/Interpret/Success/And", "The number of times AND was successfully interpreted.");
208 EM_REG_COUNTER_USED(&pStats->StatR3And, "/EM/CPU%d/R3/Interpret/Success/And", "The number of times AND was successfully interpreted.");
209 EM_REG_COUNTER_USED(&pStats->StatRZAdd, "/EM/CPU%d/RZ/Interpret/Success/Add", "The number of times ADD was successfully interpreted.");
210 EM_REG_COUNTER_USED(&pStats->StatR3Add, "/EM/CPU%d/R3/Interpret/Success/Add", "The number of times ADD was successfully interpreted.");
211 EM_REG_COUNTER_USED(&pStats->StatRZAdc, "/EM/CPU%d/RZ/Interpret/Success/Adc", "The number of times ADC was successfully interpreted.");
212 EM_REG_COUNTER_USED(&pStats->StatR3Adc, "/EM/CPU%d/R3/Interpret/Success/Adc", "The number of times ADC was successfully interpreted.");
213 EM_REG_COUNTER_USED(&pStats->StatRZSub, "/EM/CPU%d/RZ/Interpret/Success/Sub", "The number of times SUB was successfully interpreted.");
214 EM_REG_COUNTER_USED(&pStats->StatR3Sub, "/EM/CPU%d/R3/Interpret/Success/Sub", "The number of times SUB was successfully interpreted.");
215 EM_REG_COUNTER_USED(&pStats->StatRZCpuId, "/EM/CPU%d/RZ/Interpret/Success/CpuId", "The number of times CPUID was successfully interpreted.");
216 EM_REG_COUNTER_USED(&pStats->StatR3CpuId, "/EM/CPU%d/R3/Interpret/Success/CpuId", "The number of times CPUID was successfully interpreted.");
217 EM_REG_COUNTER_USED(&pStats->StatRZDec, "/EM/CPU%d/RZ/Interpret/Success/Dec", "The number of times DEC was successfully interpreted.");
218 EM_REG_COUNTER_USED(&pStats->StatR3Dec, "/EM/CPU%d/R3/Interpret/Success/Dec", "The number of times DEC was successfully interpreted.");
219 EM_REG_COUNTER_USED(&pStats->StatRZHlt, "/EM/CPU%d/RZ/Interpret/Success/Hlt", "The number of times HLT was successfully interpreted.");
220 EM_REG_COUNTER_USED(&pStats->StatR3Hlt, "/EM/CPU%d/R3/Interpret/Success/Hlt", "The number of times HLT was successfully interpreted.");
221 EM_REG_COUNTER_USED(&pStats->StatRZInc, "/EM/CPU%d/RZ/Interpret/Success/Inc", "The number of times INC was successfully interpreted.");
222 EM_REG_COUNTER_USED(&pStats->StatR3Inc, "/EM/CPU%d/R3/Interpret/Success/Inc", "The number of times INC was successfully interpreted.");
223 EM_REG_COUNTER_USED(&pStats->StatRZInvlPg, "/EM/CPU%d/RZ/Interpret/Success/Invlpg", "The number of times INVLPG was successfully interpreted.");
224 EM_REG_COUNTER_USED(&pStats->StatR3InvlPg, "/EM/CPU%d/R3/Interpret/Success/Invlpg", "The number of times INVLPG was successfully interpreted.");
225 EM_REG_COUNTER_USED(&pStats->StatRZIret, "/EM/CPU%d/RZ/Interpret/Success/Iret", "The number of times IRET was successfully interpreted.");
226 EM_REG_COUNTER_USED(&pStats->StatR3Iret, "/EM/CPU%d/R3/Interpret/Success/Iret", "The number of times IRET was successfully interpreted.");
227 EM_REG_COUNTER_USED(&pStats->StatRZLLdt, "/EM/CPU%d/RZ/Interpret/Success/LLdt", "The number of times LLDT was successfully interpreted.");
228 EM_REG_COUNTER_USED(&pStats->StatR3LLdt, "/EM/CPU%d/R3/Interpret/Success/LLdt", "The number of times LLDT was successfully interpreted.");
229 EM_REG_COUNTER_USED(&pStats->StatRZLIdt, "/EM/CPU%d/RZ/Interpret/Success/LIdt", "The number of times LIDT was successfully interpreted.");
230 EM_REG_COUNTER_USED(&pStats->StatR3LIdt, "/EM/CPU%d/R3/Interpret/Success/LIdt", "The number of times LIDT was successfully interpreted.");
231 EM_REG_COUNTER_USED(&pStats->StatRZLGdt, "/EM/CPU%d/RZ/Interpret/Success/LGdt", "The number of times LGDT was successfully interpreted.");
232 EM_REG_COUNTER_USED(&pStats->StatR3LGdt, "/EM/CPU%d/R3/Interpret/Success/LGdt", "The number of times LGDT was successfully interpreted.");
233 EM_REG_COUNTER_USED(&pStats->StatRZMov, "/EM/CPU%d/RZ/Interpret/Success/Mov", "The number of times MOV was successfully interpreted.");
234 EM_REG_COUNTER_USED(&pStats->StatR3Mov, "/EM/CPU%d/R3/Interpret/Success/Mov", "The number of times MOV was successfully interpreted.");
235 EM_REG_COUNTER_USED(&pStats->StatRZMovCRx, "/EM/CPU%d/RZ/Interpret/Success/MovCRx", "The number of times MOV CRx was successfully interpreted.");
236 EM_REG_COUNTER_USED(&pStats->StatR3MovCRx, "/EM/CPU%d/R3/Interpret/Success/MovCRx", "The number of times MOV CRx was successfully interpreted.");
237 EM_REG_COUNTER_USED(&pStats->StatRZMovDRx, "/EM/CPU%d/RZ/Interpret/Success/MovDRx", "The number of times MOV DRx was successfully interpreted.");
238 EM_REG_COUNTER_USED(&pStats->StatR3MovDRx, "/EM/CPU%d/R3/Interpret/Success/MovDRx", "The number of times MOV DRx was successfully interpreted.");
239 EM_REG_COUNTER_USED(&pStats->StatRZOr, "/EM/CPU%d/RZ/Interpret/Success/Or", "The number of times OR was successfully interpreted.");
240 EM_REG_COUNTER_USED(&pStats->StatR3Or, "/EM/CPU%d/R3/Interpret/Success/Or", "The number of times OR was successfully interpreted.");
241 EM_REG_COUNTER_USED(&pStats->StatRZPop, "/EM/CPU%d/RZ/Interpret/Success/Pop", "The number of times POP was successfully interpreted.");
242 EM_REG_COUNTER_USED(&pStats->StatR3Pop, "/EM/CPU%d/R3/Interpret/Success/Pop", "The number of times POP was successfully interpreted.");
243 EM_REG_COUNTER_USED(&pStats->StatRZRdtsc, "/EM/CPU%d/RZ/Interpret/Success/Rdtsc", "The number of times RDTSC was successfully interpreted.");
244 EM_REG_COUNTER_USED(&pStats->StatR3Rdtsc, "/EM/CPU%d/R3/Interpret/Success/Rdtsc", "The number of times RDTSC was successfully interpreted.");
245 EM_REG_COUNTER_USED(&pStats->StatRZRdpmc, "/EM/CPU%d/RZ/Interpret/Success/Rdpmc", "The number of times RDPMC was successfully interpreted.");
246 EM_REG_COUNTER_USED(&pStats->StatR3Rdpmc, "/EM/CPU%d/R3/Interpret/Success/Rdpmc", "The number of times RDPMC was successfully interpreted.");
247 EM_REG_COUNTER_USED(&pStats->StatRZSti, "/EM/CPU%d/RZ/Interpret/Success/Sti", "The number of times STI was successfully interpreted.");
248 EM_REG_COUNTER_USED(&pStats->StatR3Sti, "/EM/CPU%d/R3/Interpret/Success/Sti", "The number of times STI was successfully interpreted.");
249 EM_REG_COUNTER_USED(&pStats->StatRZXchg, "/EM/CPU%d/RZ/Interpret/Success/Xchg", "The number of times XCHG was successfully interpreted.");
250 EM_REG_COUNTER_USED(&pStats->StatR3Xchg, "/EM/CPU%d/R3/Interpret/Success/Xchg", "The number of times XCHG was successfully interpreted.");
251 EM_REG_COUNTER_USED(&pStats->StatRZXor, "/EM/CPU%d/RZ/Interpret/Success/Xor", "The number of times XOR was successfully interpreted.");
252 EM_REG_COUNTER_USED(&pStats->StatR3Xor, "/EM/CPU%d/R3/Interpret/Success/Xor", "The number of times XOR was successfully interpreted.");
253 EM_REG_COUNTER_USED(&pStats->StatRZMonitor, "/EM/CPU%d/RZ/Interpret/Success/Monitor", "The number of times MONITOR was successfully interpreted.");
254 EM_REG_COUNTER_USED(&pStats->StatR3Monitor, "/EM/CPU%d/R3/Interpret/Success/Monitor", "The number of times MONITOR was successfully interpreted.");
255 EM_REG_COUNTER_USED(&pStats->StatRZMWait, "/EM/CPU%d/RZ/Interpret/Success/MWait", "The number of times MWAIT was successfully interpreted.");
256 EM_REG_COUNTER_USED(&pStats->StatR3MWait, "/EM/CPU%d/R3/Interpret/Success/MWait", "The number of times MWAIT was successfully interpreted.");
257 EM_REG_COUNTER_USED(&pStats->StatRZBtr, "/EM/CPU%d/RZ/Interpret/Success/Btr", "The number of times BTR was successfully interpreted.");
258 EM_REG_COUNTER_USED(&pStats->StatR3Btr, "/EM/CPU%d/R3/Interpret/Success/Btr", "The number of times BTR was successfully interpreted.");
259 EM_REG_COUNTER_USED(&pStats->StatRZBts, "/EM/CPU%d/RZ/Interpret/Success/Bts", "The number of times BTS was successfully interpreted.");
260 EM_REG_COUNTER_USED(&pStats->StatR3Bts, "/EM/CPU%d/R3/Interpret/Success/Bts", "The number of times BTS was successfully interpreted.");
261 EM_REG_COUNTER_USED(&pStats->StatRZBtc, "/EM/CPU%d/RZ/Interpret/Success/Btc", "The number of times BTC was successfully interpreted.");
262 EM_REG_COUNTER_USED(&pStats->StatR3Btc, "/EM/CPU%d/R3/Interpret/Success/Btc", "The number of times BTC was successfully interpreted.");
263 EM_REG_COUNTER_USED(&pStats->StatRZCmpXchg, "/EM/CPU%d/RZ/Interpret/Success/CmpXchg", "The number of times CMPXCHG was successfully interpreted.");
264 EM_REG_COUNTER_USED(&pStats->StatR3CmpXchg, "/EM/CPU%d/R3/Interpret/Success/CmpXchg", "The number of times CMPXCHG was successfully interpreted.");
265 EM_REG_COUNTER_USED(&pStats->StatRZCmpXchg8b, "/EM/CPU%d/RZ/Interpret/Success/CmpXchg8b", "The number of times CMPXCHG8B was successfully interpreted.");
266 EM_REG_COUNTER_USED(&pStats->StatR3CmpXchg8b, "/EM/CPU%d/R3/Interpret/Success/CmpXchg8b", "The number of times CMPXCHG8B was successfully interpreted.");
267 EM_REG_COUNTER_USED(&pStats->StatRZXAdd, "/EM/CPU%d/RZ/Interpret/Success/XAdd", "The number of times XADD was successfully interpreted.");
268 EM_REG_COUNTER_USED(&pStats->StatR3XAdd, "/EM/CPU%d/R3/Interpret/Success/XAdd", "The number of times XADD was successfully interpreted.");
269 EM_REG_COUNTER_USED(&pStats->StatR3Rdmsr, "/EM/CPU%d/R3/Interpret/Success/Rdmsr", "The number of times RDMSR was successfully interpreted.");
270 EM_REG_COUNTER_USED(&pStats->StatRZRdmsr, "/EM/CPU%d/RZ/Interpret/Success/Rdmsr", "The number of times RDMSR was successfully interpreted.");
271 EM_REG_COUNTER_USED(&pStats->StatR3Wrmsr, "/EM/CPU%d/R3/Interpret/Success/Wrmsr", "The number of times WRMSR was successfully interpreted.");
272 EM_REG_COUNTER_USED(&pStats->StatRZWrmsr, "/EM/CPU%d/RZ/Interpret/Success/Wrmsr", "The number of times WRMSR was successfully interpreted.");
273 EM_REG_COUNTER_USED(&pStats->StatR3StosWD, "/EM/CPU%d/R3/Interpret/Success/Stoswd", "The number of times STOSWD was successfully interpreted.");
274 EM_REG_COUNTER_USED(&pStats->StatRZStosWD, "/EM/CPU%d/RZ/Interpret/Success/Stoswd", "The number of times STOSWD was successfully interpreted.");
275 EM_REG_COUNTER_USED(&pStats->StatRZWbInvd, "/EM/CPU%d/RZ/Interpret/Success/WbInvd", "The number of times WBINVD was successfully interpreted.");
276 EM_REG_COUNTER_USED(&pStats->StatR3WbInvd, "/EM/CPU%d/R3/Interpret/Success/WbInvd", "The number of times WBINVD was successfully interpreted.");
277 EM_REG_COUNTER_USED(&pStats->StatRZLmsw, "/EM/CPU%d/RZ/Interpret/Success/Lmsw", "The number of times LMSW was successfully interpreted.");
278 EM_REG_COUNTER_USED(&pStats->StatR3Lmsw, "/EM/CPU%d/R3/Interpret/Success/Lmsw", "The number of times LMSW was successfully interpreted.");
279
280 EM_REG_COUNTER(&pStats->StatRZInterpretFailed, "/EM/CPU%d/RZ/Interpret/Failed", "The number of times an instruction was not interpreted.");
281 EM_REG_COUNTER(&pStats->StatR3InterpretFailed, "/EM/CPU%d/R3/Interpret/Failed", "The number of times an instruction was not interpreted.");
282
283 EM_REG_COUNTER_USED(&pStats->StatRZFailedAnd, "/EM/CPU%d/RZ/Interpret/Failed/And", "The number of times AND was not interpreted.");
284 EM_REG_COUNTER_USED(&pStats->StatR3FailedAnd, "/EM/CPU%d/R3/Interpret/Failed/And", "The number of times AND was not interpreted.");
285 EM_REG_COUNTER_USED(&pStats->StatRZFailedCpuId, "/EM/CPU%d/RZ/Interpret/Failed/CpuId", "The number of times CPUID was not interpreted.");
286 EM_REG_COUNTER_USED(&pStats->StatR3FailedCpuId, "/EM/CPU%d/R3/Interpret/Failed/CpuId", "The number of times CPUID was not interpreted.");
287 EM_REG_COUNTER_USED(&pStats->StatRZFailedDec, "/EM/CPU%d/RZ/Interpret/Failed/Dec", "The number of times DEC was not interpreted.");
288 EM_REG_COUNTER_USED(&pStats->StatR3FailedDec, "/EM/CPU%d/R3/Interpret/Failed/Dec", "The number of times DEC was not interpreted.");
289 EM_REG_COUNTER_USED(&pStats->StatRZFailedHlt, "/EM/CPU%d/RZ/Interpret/Failed/Hlt", "The number of times HLT was not interpreted.");
290 EM_REG_COUNTER_USED(&pStats->StatR3FailedHlt, "/EM/CPU%d/R3/Interpret/Failed/Hlt", "The number of times HLT was not interpreted.");
291 EM_REG_COUNTER_USED(&pStats->StatRZFailedInc, "/EM/CPU%d/RZ/Interpret/Failed/Inc", "The number of times INC was not interpreted.");
292 EM_REG_COUNTER_USED(&pStats->StatR3FailedInc, "/EM/CPU%d/R3/Interpret/Failed/Inc", "The number of times INC was not interpreted.");
293 EM_REG_COUNTER_USED(&pStats->StatRZFailedInvlPg, "/EM/CPU%d/RZ/Interpret/Failed/InvlPg", "The number of times INVLPG was not interpreted.");
294 EM_REG_COUNTER_USED(&pStats->StatR3FailedInvlPg, "/EM/CPU%d/R3/Interpret/Failed/InvlPg", "The number of times INVLPG was not interpreted.");
295 EM_REG_COUNTER_USED(&pStats->StatRZFailedIret, "/EM/CPU%d/RZ/Interpret/Failed/Iret", "The number of times IRET was not interpreted.");
296 EM_REG_COUNTER_USED(&pStats->StatR3FailedIret, "/EM/CPU%d/R3/Interpret/Failed/Iret", "The number of times IRET was not interpreted.");
297 EM_REG_COUNTER_USED(&pStats->StatRZFailedLLdt, "/EM/CPU%d/RZ/Interpret/Failed/LLdt", "The number of times LLDT was not interpreted.");
298 EM_REG_COUNTER_USED(&pStats->StatR3FailedLLdt, "/EM/CPU%d/R3/Interpret/Failed/LLdt", "The number of times LLDT was not interpreted.");
299 EM_REG_COUNTER_USED(&pStats->StatRZFailedLIdt, "/EM/CPU%d/RZ/Interpret/Failed/LIdt", "The number of times LIDT was not interpreted.");
300 EM_REG_COUNTER_USED(&pStats->StatR3FailedLIdt, "/EM/CPU%d/R3/Interpret/Failed/LIdt", "The number of times LIDT was not interpreted.");
301 EM_REG_COUNTER_USED(&pStats->StatRZFailedLGdt, "/EM/CPU%d/RZ/Interpret/Failed/LGdt", "The number of times LGDT was not interpreted.");
302 EM_REG_COUNTER_USED(&pStats->StatR3FailedLGdt, "/EM/CPU%d/R3/Interpret/Failed/LGdt", "The number of times LGDT was not interpreted.");
303 EM_REG_COUNTER_USED(&pStats->StatRZFailedMov, "/EM/CPU%d/RZ/Interpret/Failed/Mov", "The number of times MOV was not interpreted.");
304 EM_REG_COUNTER_USED(&pStats->StatR3FailedMov, "/EM/CPU%d/R3/Interpret/Failed/Mov", "The number of times MOV was not interpreted.");
305 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovCRx, "/EM/CPU%d/RZ/Interpret/Failed/MovCRx", "The number of times MOV CRx was not interpreted.");
306 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovCRx, "/EM/CPU%d/R3/Interpret/Failed/MovCRx", "The number of times MOV CRx was not interpreted.");
307 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovDRx, "/EM/CPU%d/RZ/Interpret/Failed/MovDRx", "The number of times MOV DRx was not interpreted.");
308 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovDRx, "/EM/CPU%d/R3/Interpret/Failed/MovDRx", "The number of times MOV DRx was not interpreted.");
309 EM_REG_COUNTER_USED(&pStats->StatRZFailedOr, "/EM/CPU%d/RZ/Interpret/Failed/Or", "The number of times OR was not interpreted.");
310 EM_REG_COUNTER_USED(&pStats->StatR3FailedOr, "/EM/CPU%d/R3/Interpret/Failed/Or", "The number of times OR was not interpreted.");
311 EM_REG_COUNTER_USED(&pStats->StatRZFailedPop, "/EM/CPU%d/RZ/Interpret/Failed/Pop", "The number of times POP was not interpreted.");
312 EM_REG_COUNTER_USED(&pStats->StatR3FailedPop, "/EM/CPU%d/R3/Interpret/Failed/Pop", "The number of times POP was not interpreted.");
313 EM_REG_COUNTER_USED(&pStats->StatRZFailedSti, "/EM/CPU%d/RZ/Interpret/Failed/Sti", "The number of times STI was not interpreted.");
314 EM_REG_COUNTER_USED(&pStats->StatR3FailedSti, "/EM/CPU%d/R3/Interpret/Failed/Sti", "The number of times STI was not interpreted.");
315 EM_REG_COUNTER_USED(&pStats->StatRZFailedXchg, "/EM/CPU%d/RZ/Interpret/Failed/Xchg", "The number of times XCHG was not interpreted.");
316 EM_REG_COUNTER_USED(&pStats->StatR3FailedXchg, "/EM/CPU%d/R3/Interpret/Failed/Xchg", "The number of times XCHG was not interpreted.");
317 EM_REG_COUNTER_USED(&pStats->StatRZFailedXor, "/EM/CPU%d/RZ/Interpret/Failed/Xor", "The number of times XOR was not interpreted.");
318 EM_REG_COUNTER_USED(&pStats->StatR3FailedXor, "/EM/CPU%d/R3/Interpret/Failed/Xor", "The number of times XOR was not interpreted.");
319 EM_REG_COUNTER_USED(&pStats->StatRZFailedMonitor, "/EM/CPU%d/RZ/Interpret/Failed/Monitor", "The number of times MONITOR was not interpreted.");
320 EM_REG_COUNTER_USED(&pStats->StatR3FailedMonitor, "/EM/CPU%d/R3/Interpret/Failed/Monitor", "The number of times MONITOR was not interpreted.");
321 EM_REG_COUNTER_USED(&pStats->StatRZFailedMWait, "/EM/CPU%d/RZ/Interpret/Failed/MWait", "The number of times MONITOR was not interpreted.");
322 EM_REG_COUNTER_USED(&pStats->StatR3FailedMWait, "/EM/CPU%d/R3/Interpret/Failed/MWait", "The number of times MONITOR was not interpreted.");
323 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdtsc, "/EM/CPU%d/RZ/Interpret/Failed/Rdtsc", "The number of times RDTSC was not interpreted.");
324 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdtsc, "/EM/CPU%d/R3/Interpret/Failed/Rdtsc", "The number of times RDTSC was not interpreted.");
325 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdpmc, "/EM/CPU%d/RZ/Interpret/Failed/Rdpmc", "The number of times RDPMC was not interpreted.");
326 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdpmc, "/EM/CPU%d/R3/Interpret/Failed/Rdpmc", "The number of times RDPMC was not interpreted.");
327 EM_REG_COUNTER_USED(&pStats->StatRZFailedRdmsr, "/EM/CPU%d/RZ/Interpret/Failed/Rdmsr", "The number of times RDMSR was not interpreted.");
328 EM_REG_COUNTER_USED(&pStats->StatR3FailedRdmsr, "/EM/CPU%d/R3/Interpret/Failed/Rdmsr", "The number of times RDMSR was not interpreted.");
329 EM_REG_COUNTER_USED(&pStats->StatRZFailedWrmsr, "/EM/CPU%d/RZ/Interpret/Failed/Wrmsr", "The number of times WRMSR was not interpreted.");
330 EM_REG_COUNTER_USED(&pStats->StatR3FailedWrmsr, "/EM/CPU%d/R3/Interpret/Failed/Wrmsr", "The number of times WRMSR was not interpreted.");
331 EM_REG_COUNTER_USED(&pStats->StatRZFailedLmsw, "/EM/CPU%d/RZ/Interpret/Failed/Lmsw", "The number of times LMSW was not interpreted.");
332 EM_REG_COUNTER_USED(&pStats->StatR3FailedLmsw, "/EM/CPU%d/R3/Interpret/Failed/Lmsw", "The number of times LMSW was not interpreted.");
333
334 EM_REG_COUNTER_USED(&pStats->StatRZFailedMisc, "/EM/CPU%d/RZ/Interpret/Failed/Misc", "The number of times some misc instruction was encountered.");
335 EM_REG_COUNTER_USED(&pStats->StatR3FailedMisc, "/EM/CPU%d/R3/Interpret/Failed/Misc", "The number of times some misc instruction was encountered.");
336 EM_REG_COUNTER_USED(&pStats->StatRZFailedAdd, "/EM/CPU%d/RZ/Interpret/Failed/Add", "The number of times ADD was not interpreted.");
337 EM_REG_COUNTER_USED(&pStats->StatR3FailedAdd, "/EM/CPU%d/R3/Interpret/Failed/Add", "The number of times ADD was not interpreted.");
338 EM_REG_COUNTER_USED(&pStats->StatRZFailedAdc, "/EM/CPU%d/RZ/Interpret/Failed/Adc", "The number of times ADC was not interpreted.");
339 EM_REG_COUNTER_USED(&pStats->StatR3FailedAdc, "/EM/CPU%d/R3/Interpret/Failed/Adc", "The number of times ADC was not interpreted.");
340 EM_REG_COUNTER_USED(&pStats->StatRZFailedBtr, "/EM/CPU%d/RZ/Interpret/Failed/Btr", "The number of times BTR was not interpreted.");
341 EM_REG_COUNTER_USED(&pStats->StatR3FailedBtr, "/EM/CPU%d/R3/Interpret/Failed/Btr", "The number of times BTR was not interpreted.");
342 EM_REG_COUNTER_USED(&pStats->StatRZFailedBts, "/EM/CPU%d/RZ/Interpret/Failed/Bts", "The number of times BTS was not interpreted.");
343 EM_REG_COUNTER_USED(&pStats->StatR3FailedBts, "/EM/CPU%d/R3/Interpret/Failed/Bts", "The number of times BTS was not interpreted.");
344 EM_REG_COUNTER_USED(&pStats->StatRZFailedBtc, "/EM/CPU%d/RZ/Interpret/Failed/Btc", "The number of times BTC was not interpreted.");
345 EM_REG_COUNTER_USED(&pStats->StatR3FailedBtc, "/EM/CPU%d/R3/Interpret/Failed/Btc", "The number of times BTC was not interpreted.");
346 EM_REG_COUNTER_USED(&pStats->StatRZFailedCli, "/EM/CPU%d/RZ/Interpret/Failed/Cli", "The number of times CLI was not interpreted.");
347 EM_REG_COUNTER_USED(&pStats->StatR3FailedCli, "/EM/CPU%d/R3/Interpret/Failed/Cli", "The number of times CLI was not interpreted.");
348 EM_REG_COUNTER_USED(&pStats->StatRZFailedCmpXchg, "/EM/CPU%d/RZ/Interpret/Failed/CmpXchg", "The number of times CMPXCHG was not interpreted.");
349 EM_REG_COUNTER_USED(&pStats->StatR3FailedCmpXchg, "/EM/CPU%d/R3/Interpret/Failed/CmpXchg", "The number of times CMPXCHG was not interpreted.");
350 EM_REG_COUNTER_USED(&pStats->StatRZFailedCmpXchg8b, "/EM/CPU%d/RZ/Interpret/Failed/CmpXchg8b", "The number of times CMPXCHG8B was not interpreted.");
351 EM_REG_COUNTER_USED(&pStats->StatR3FailedCmpXchg8b, "/EM/CPU%d/R3/Interpret/Failed/CmpXchg8b", "The number of times CMPXCHG8B was not interpreted.");
352 EM_REG_COUNTER_USED(&pStats->StatRZFailedXAdd, "/EM/CPU%d/RZ/Interpret/Failed/XAdd", "The number of times XADD was not interpreted.");
353 EM_REG_COUNTER_USED(&pStats->StatR3FailedXAdd, "/EM/CPU%d/R3/Interpret/Failed/XAdd", "The number of times XADD was not interpreted.");
354 EM_REG_COUNTER_USED(&pStats->StatRZFailedMovNTPS, "/EM/CPU%d/RZ/Interpret/Failed/MovNTPS", "The number of times MOVNTPS was not interpreted.");
355 EM_REG_COUNTER_USED(&pStats->StatR3FailedMovNTPS, "/EM/CPU%d/R3/Interpret/Failed/MovNTPS", "The number of times MOVNTPS was not interpreted.");
356 EM_REG_COUNTER_USED(&pStats->StatRZFailedStosWD, "/EM/CPU%d/RZ/Interpret/Failed/StosWD", "The number of times STOSWD was not interpreted.");
357 EM_REG_COUNTER_USED(&pStats->StatR3FailedStosWD, "/EM/CPU%d/R3/Interpret/Failed/StosWD", "The number of times STOSWD was not interpreted.");
358 EM_REG_COUNTER_USED(&pStats->StatRZFailedSub, "/EM/CPU%d/RZ/Interpret/Failed/Sub", "The number of times SUB was not interpreted.");
359 EM_REG_COUNTER_USED(&pStats->StatR3FailedSub, "/EM/CPU%d/R3/Interpret/Failed/Sub", "The number of times SUB was not interpreted.");
360 EM_REG_COUNTER_USED(&pStats->StatRZFailedWbInvd, "/EM/CPU%d/RZ/Interpret/Failed/WbInvd", "The number of times WBINVD was not interpreted.");
361 EM_REG_COUNTER_USED(&pStats->StatR3FailedWbInvd, "/EM/CPU%d/R3/Interpret/Failed/WbInvd", "The number of times WBINVD was not interpreted.");
362
363 EM_REG_COUNTER_USED(&pStats->StatRZFailedUserMode, "/EM/CPU%d/RZ/Interpret/Failed/UserMode", "The number of rejections because of CPL.");
364 EM_REG_COUNTER_USED(&pStats->StatR3FailedUserMode, "/EM/CPU%d/R3/Interpret/Failed/UserMode", "The number of rejections because of CPL.");
365 EM_REG_COUNTER_USED(&pStats->StatRZFailedPrefix, "/EM/CPU%d/RZ/Interpret/Failed/Prefix", "The number of rejections because of prefix .");
366 EM_REG_COUNTER_USED(&pStats->StatR3FailedPrefix, "/EM/CPU%d/R3/Interpret/Failed/Prefix", "The number of rejections because of prefix .");
367
368 EM_REG_COUNTER_USED(&pStats->StatCli, "/EM/CPU%d/R3/PrivInst/Cli", "Number of cli instructions.");
369 EM_REG_COUNTER_USED(&pStats->StatSti, "/EM/CPU%d/R3/PrivInst/Sti", "Number of sli instructions.");
370 EM_REG_COUNTER_USED(&pStats->StatIn, "/EM/CPU%d/R3/PrivInst/In", "Number of in instructions.");
371 EM_REG_COUNTER_USED(&pStats->StatOut, "/EM/CPU%d/R3/PrivInst/Out", "Number of out instructions.");
372 EM_REG_COUNTER_USED(&pStats->StatHlt, "/EM/CPU%d/R3/PrivInst/Hlt", "Number of hlt instructions not handled in GC because of PATM.");
373 EM_REG_COUNTER_USED(&pStats->StatInvlpg, "/EM/CPU%d/R3/PrivInst/Invlpg", "Number of invlpg instructions.");
374 EM_REG_COUNTER_USED(&pStats->StatMisc, "/EM/CPU%d/R3/PrivInst/Misc", "Number of misc. instructions.");
375 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[0], "/EM/CPU%d/R3/PrivInst/Mov CR0, X", "Number of mov CR0 read instructions.");
376 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[1], "/EM/CPU%d/R3/PrivInst/Mov CR1, X", "Number of mov CR1 read instructions.");
377 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[2], "/EM/CPU%d/R3/PrivInst/Mov CR2, X", "Number of mov CR2 read instructions.");
378 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[3], "/EM/CPU%d/R3/PrivInst/Mov CR3, X", "Number of mov CR3 read instructions.");
379 EM_REG_COUNTER_USED(&pStats->StatMovWriteCR[4], "/EM/CPU%d/R3/PrivInst/Mov CR4, X", "Number of mov CR4 read instructions.");
380 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[0], "/EM/CPU%d/R3/PrivInst/Mov X, CR0", "Number of mov CR0 write instructions.");
381 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[1], "/EM/CPU%d/R3/PrivInst/Mov X, CR1", "Number of mov CR1 write instructions.");
382 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[2], "/EM/CPU%d/R3/PrivInst/Mov X, CR2", "Number of mov CR2 write instructions.");
383 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[3], "/EM/CPU%d/R3/PrivInst/Mov X, CR3", "Number of mov CR3 write instructions.");
384 EM_REG_COUNTER_USED(&pStats->StatMovReadCR[4], "/EM/CPU%d/R3/PrivInst/Mov X, CR4", "Number of mov CR4 write instructions.");
385 EM_REG_COUNTER_USED(&pStats->StatMovDRx, "/EM/CPU%d/R3/PrivInst/MovDRx", "Number of mov DRx instructions.");
386 EM_REG_COUNTER_USED(&pStats->StatIret, "/EM/CPU%d/R3/PrivInst/Iret", "Number of iret instructions.");
387 EM_REG_COUNTER_USED(&pStats->StatMovLgdt, "/EM/CPU%d/R3/PrivInst/Lgdt", "Number of lgdt instructions.");
388 EM_REG_COUNTER_USED(&pStats->StatMovLidt, "/EM/CPU%d/R3/PrivInst/Lidt", "Number of lidt instructions.");
389 EM_REG_COUNTER_USED(&pStats->StatMovLldt, "/EM/CPU%d/R3/PrivInst/Lldt", "Number of lldt instructions.");
390 EM_REG_COUNTER_USED(&pStats->StatSysEnter, "/EM/CPU%d/R3/PrivInst/Sysenter", "Number of sysenter instructions.");
391 EM_REG_COUNTER_USED(&pStats->StatSysExit, "/EM/CPU%d/R3/PrivInst/Sysexit", "Number of sysexit instructions.");
392 EM_REG_COUNTER_USED(&pStats->StatSysCall, "/EM/CPU%d/R3/PrivInst/Syscall", "Number of syscall instructions.");
393 EM_REG_COUNTER_USED(&pStats->StatSysRet, "/EM/CPU%d/R3/PrivInst/Sysret", "Number of sysret instructions.");
394
395 EM_REG_COUNTER(&pVCpu->em.s.StatTotalClis, "/EM/CPU%d/Cli/Total", "Total number of cli instructions executed.");
396 pVCpu->em.s.pCliStatTree = 0;
397
398 /* these should be considered for release statistics. */
399 EM_REG_COUNTER(&pVCpu->em.s.StatIOEmu, "/PROF/CPU%d/EM/Emulation/IO", "Profiling of emR3RawExecuteIOInstruction.");
400 EM_REG_COUNTER(&pVCpu->em.s.StatPrivEmu, "/PROF/CPU%d/EM/Emulation/Priv", "Profiling of emR3RawPrivileged.");
401 EM_REG_COUNTER(&pVCpu->em.s.StatMiscEmu, "/PROF/CPU%d/EM/Emulation/Misc", "Profiling of emR3RawExecuteInstruction.");
402 EM_REG_PROFILE(&pVCpu->em.s.StatHwAccEntry, "/PROF/CPU%d/EM/HwAccEnter", "Profiling Hardware Accelerated Mode entry overhead.");
403 EM_REG_PROFILE(&pVCpu->em.s.StatHwAccExec, "/PROF/CPU%d/EM/HwAccExec", "Profiling Hardware Accelerated Mode execution.");
404 EM_REG_PROFILE(&pVCpu->em.s.StatREMEmu, "/PROF/CPU%d/EM/REMEmuSingle", "Profiling single instruction REM execution.");
405 EM_REG_PROFILE(&pVCpu->em.s.StatREMExec, "/PROF/CPU%d/EM/REMExec", "Profiling REM execution.");
406 EM_REG_PROFILE(&pVCpu->em.s.StatREMSync, "/PROF/CPU%d/EM/REMSync", "Profiling REM context syncing.");
407 EM_REG_PROFILE(&pVCpu->em.s.StatRAWEntry, "/PROF/CPU%d/EM/RAWEnter", "Profiling Raw Mode entry overhead.");
408 EM_REG_PROFILE(&pVCpu->em.s.StatRAWExec, "/PROF/CPU%d/EM/RAWExec", "Profiling Raw Mode execution.");
409 EM_REG_PROFILE(&pVCpu->em.s.StatRAWTail, "/PROF/CPU%d/EM/RAWTail", "Profiling Raw Mode tail overhead.");
410
411#endif /* VBOX_WITH_STATISTICS */
412
413 EM_REG_COUNTER(&pVCpu->em.s.StatForcedActions, "/PROF/CPU%d/EM/ForcedActions", "Profiling forced action execution.");
414 EM_REG_COUNTER(&pVCpu->em.s.StatHalted, "/PROF/CPU%d/EM/Halted", "Profiling halted state (VMR3WaitHalted).");
415 EM_REG_COUNTER(&pVCpu->em.s.StatREMTotal, "/PROF/CPU%d/EM/REMTotal", "Profiling emR3RemExecute (excluding FFs).");
416 EM_REG_COUNTER(&pVCpu->em.s.StatRAWTotal, "/PROF/CPU%d/EM/RAWTotal", "Profiling emR3RawExecute (excluding FFs).");
417
418 EM_REG_PROFILE_ADV(&pVCpu->em.s.StatTotal, "/PROF/CPU%d/EM/Total", "Profiling EMR3ExecuteVM.");
419 }
420
421 return VINF_SUCCESS;
422}
423
424
425/**
426 * Initializes the per-VCPU EM.
427 *
428 * @returns VBox status code.
429 * @param pVM The VM to operate on.
430 */
431VMMR3DECL(int) EMR3InitCPU(PVM pVM)
432{
433 LogFlow(("EMR3InitCPU\n"));
434 return VINF_SUCCESS;
435}
436
437
438/**
439 * Applies relocations to data and code managed by this
440 * component. This function will be called at init and
441 * whenever the VMM need to relocate it self inside the GC.
442 *
443 * @param pVM The VM.
444 */
445VMMR3DECL(void) EMR3Relocate(PVM pVM)
446{
447 LogFlow(("EMR3Relocate\n"));
448 for (unsigned i=0;i<pVM->cCPUs;i++)
449 {
450 PVMCPU pVCpu = &pVM->aCpus[i];
451
452 if (pVCpu->em.s.pStatsR3)
453 pVCpu->em.s.pStatsRC = MMHyperR3ToRC(pVM, pVCpu->em.s.pStatsR3);
454 }
455}
456
457
458/**
459 * Reset notification.
460 *
461 * @param pVM
462 */
463VMMR3DECL(void) EMR3Reset(PVM pVM)
464{
465 LogFlow(("EMR3Reset: \n"));
466 for (unsigned i=0;i<pVM->cCPUs;i++)
467 {
468 PVMCPU pVCpu = &pVM->aCpus[i];
469
470 pVCpu->em.s.fForceRAW = false;
471 }
472}
473
474
475/**
476 * Terminates the EM.
477 *
478 * Termination means cleaning up and freeing all resources,
479 * the VM it self is at this point powered off or suspended.
480 *
481 * @returns VBox status code.
482 * @param pVM The VM to operate on.
483 */
484VMMR3DECL(int) EMR3Term(PVM pVM)
485{
486 AssertMsg(pVM->em.s.offVM, ("bad init order!\n"));
487
488 PDMR3CritSectDelete(&pVM->em.s.CritSectREM);
489 return VINF_SUCCESS;
490}
491
492/**
493 * Terminates the per-VCPU EM.
494 *
495 * Termination means cleaning up and freeing all resources,
496 * the VM it self is at this point powered off or suspended.
497 *
498 * @returns VBox status code.
499 * @param pVM The VM to operate on.
500 */
501VMMR3DECL(int) EMR3TermCPU(PVM pVM)
502{
503 return 0;
504}
505
506/**
507 * Execute state save operation.
508 *
509 * @returns VBox status code.
510 * @param pVM VM Handle.
511 * @param pSSM SSM operation handle.
512 */
513static DECLCALLBACK(int) emR3Save(PVM pVM, PSSMHANDLE pSSM)
514{
515 for (unsigned i=0;i<pVM->cCPUs;i++)
516 {
517 PVMCPU pVCpu = &pVM->aCpus[i];
518
519 int rc = SSMR3PutBool(pSSM, pVCpu->em.s.fForceRAW);
520 AssertRCReturn(rc, rc);
521
522 Assert(pVCpu->em.s.enmState == EMSTATE_SUSPENDED);
523 Assert(pVCpu->em.s.enmPrevState != EMSTATE_SUSPENDED);
524 rc = SSMR3PutU32(pSSM, pVCpu->em.s.enmPrevState);
525 AssertRCReturn(rc, rc);
526 }
527 return VINF_SUCCESS;
528}
529
530
531/**
532 * Execute state load operation.
533 *
534 * @returns VBox status code.
535 * @param pVM VM Handle.
536 * @param pSSM SSM operation handle.
537 * @param u32Version Data layout version.
538 */
539static DECLCALLBACK(int) emR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
540{
541 int rc = VINF_SUCCESS;
542
543 /*
544 * Validate version.
545 */
546 if ( u32Version != EM_SAVED_STATE_VERSION
547 && u32Version != EM_SAVED_STATE_VERSION_PRE_SMP)
548 {
549 AssertMsgFailed(("emR3Load: Invalid version u32Version=%d (current %d)!\n", u32Version, EM_SAVED_STATE_VERSION));
550 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
551 }
552
553 /*
554 * Load the saved state.
555 */
556 for (unsigned i=0;i<pVM->cCPUs;i++)
557 {
558 PVMCPU pVCpu = &pVM->aCpus[i];
559
560 rc = SSMR3GetBool(pSSM, &pVCpu->em.s.fForceRAW);
561 if (RT_FAILURE(rc))
562 pVCpu->em.s.fForceRAW = false;
563
564 if (u32Version > EM_SAVED_STATE_VERSION_PRE_SMP)
565 {
566 EMSTATE enmState;
567 uint32_t u32;
568 rc = SSMR3GetU32(pSSM, &u32);
569 AssertRCReturn(rc, rc);
570
571 enmState = (EMSTATE)u32;
572 Assert(enmState != EMSTATE_SUSPENDED);
573 pVCpu->em.s.enmState = enmState;
574 pVCpu->em.s.enmPrevState = EMSTATE_NONE;
575 }
576 Assert(!pVCpu->em.s.pCliStatTree);
577 }
578 return rc;
579}
580
581
582/**
583 * Enables or disables a set of raw-mode execution modes.
584 *
585 * @returns VINF_SUCCESS on success.
586 * @returns VINF_RESCHEDULE if a rescheduling might be required.
587 * @returns VERR_INVALID_PARAMETER on an invalid enmMode value.
588 *
589 * @param pVM The VM to operate on.
590 * @param enmMode The execution mode change.
591 * @thread The emulation thread.
592 */
593VMMR3DECL(int) EMR3RawSetMode(PVM pVM, EMRAWMODE enmMode)
594{
595 switch (enmMode)
596 {
597 case EMRAW_NONE:
598 pVM->fRawR3Enabled = false;
599 pVM->fRawR0Enabled = false;
600 break;
601 case EMRAW_RING3_ENABLE:
602 pVM->fRawR3Enabled = true;
603 break;
604 case EMRAW_RING3_DISABLE:
605 pVM->fRawR3Enabled = false;
606 break;
607 case EMRAW_RING0_ENABLE:
608 pVM->fRawR0Enabled = true;
609 break;
610 case EMRAW_RING0_DISABLE:
611 pVM->fRawR0Enabled = false;
612 break;
613 default:
614 AssertMsgFailed(("Invalid enmMode=%d\n", enmMode));
615 return VERR_INVALID_PARAMETER;
616 }
617 Log(("EMR3SetRawMode: fRawR3Enabled=%RTbool fRawR0Enabled=%RTbool\n",
618 pVM->fRawR3Enabled, pVM->fRawR0Enabled));
619 return pVM->aCpus[0].em.s.enmState == EMSTATE_RAW ? VINF_EM_RESCHEDULE : VINF_SUCCESS;
620}
621
622
623/**
624 * Raise a fatal error.
625 *
626 * Safely terminate the VM with full state report and stuff. This function
627 * will naturally never return.
628 *
629 * @param pVCpu VMCPU handle.
630 * @param rc VBox status code.
631 */
632VMMR3DECL(void) EMR3FatalError(PVMCPU pVCpu, int rc)
633{
634 longjmp(pVCpu->em.s.u.FatalLongJump, rc);
635 AssertReleaseMsgFailed(("longjmp returned!\n"));
636}
637
638
639/**
640 * Gets the EM state name.
641 *
642 * @returns pointer to read only state name,
643 * @param enmState The state.
644 */
645VMMR3DECL(const char *) EMR3GetStateName(EMSTATE enmState)
646{
647 switch (enmState)
648 {
649 case EMSTATE_NONE: return "EMSTATE_NONE";
650 case EMSTATE_RAW: return "EMSTATE_RAW";
651 case EMSTATE_HWACC: return "EMSTATE_HWACC";
652 case EMSTATE_REM: return "EMSTATE_REM";
653 case EMSTATE_PARAV: return "EMSTATE_PARAV";
654 case EMSTATE_HALTED: return "EMSTATE_HALTED";
655 case EMSTATE_WAIT_SIPI: return "EMSTATE_WAIT_SIPI";
656 case EMSTATE_SUSPENDED: return "EMSTATE_SUSPENDED";
657 case EMSTATE_TERMINATING: return "EMSTATE_TERMINATING";
658 case EMSTATE_DEBUG_GUEST_RAW: return "EMSTATE_DEBUG_GUEST_RAW";
659 case EMSTATE_DEBUG_GUEST_REM: return "EMSTATE_DEBUG_GUEST_REM";
660 case EMSTATE_DEBUG_HYPER: return "EMSTATE_DEBUG_HYPER";
661 case EMSTATE_GURU_MEDITATION: return "EMSTATE_GURU_MEDITATION";
662 default: return "Unknown!";
663 }
664}
665
666
667#ifdef VBOX_WITH_STATISTICS
668/**
669 * Just a braindead function to keep track of cli addresses.
670 * @param pVM VM handle.
671 * @param pVMCPU VMCPU handle.
672 * @param GCPtrInstr The EIP of the cli instruction.
673 */
674static void emR3RecordCli(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtrInstr)
675{
676 PCLISTAT pRec;
677
678 pRec = (PCLISTAT)RTAvlPVGet(&pVCpu->em.s.pCliStatTree, (AVLPVKEY)GCPtrInstr);
679 if (!pRec)
680 {
681 /* New cli instruction; insert into the tree. */
682 pRec = (PCLISTAT)MMR3HeapAllocZ(pVM, MM_TAG_EM, sizeof(*pRec));
683 Assert(pRec);
684 if (!pRec)
685 return;
686 pRec->Core.Key = (AVLPVKEY)GCPtrInstr;
687
688 char szCliStatName[32];
689 RTStrPrintf(szCliStatName, sizeof(szCliStatName), "/EM/Cli/0x%RGv", GCPtrInstr);
690 STAM_REG(pVM, &pRec->Counter, STAMTYPE_COUNTER, szCliStatName, STAMUNIT_OCCURENCES, "Number of times cli was executed.");
691
692 bool fRc = RTAvlPVInsert(&pVCpu->em.s.pCliStatTree, &pRec->Core);
693 Assert(fRc); NOREF(fRc);
694 }
695 STAM_COUNTER_INC(&pRec->Counter);
696 STAM_COUNTER_INC(&pVCpu->em.s.StatTotalClis);
697}
698#endif /* VBOX_WITH_STATISTICS */
699
700
701/**
702 * Debug loop.
703 *
704 * @returns VBox status code for EM.
705 * @param pVM VM handle.
706 * @param pVCpu VMCPU handle.
707 * @param rc Current EM VBox status code..
708 */
709static int emR3Debug(PVM pVM, PVMCPU pVCpu, int rc)
710{
711 for (;;)
712 {
713 Log(("emR3Debug: rc=%Rrc\n", rc));
714 const int rcLast = rc;
715
716 /*
717 * Debug related RC.
718 */
719 switch (rc)
720 {
721 /*
722 * Single step an instruction.
723 */
724 case VINF_EM_DBG_STEP:
725 if ( pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_RAW
726 || pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER
727 || pVCpu->em.s.fForceRAW /* paranoia */)
728 rc = emR3RawStep(pVM, pVCpu);
729 else
730 {
731 Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_REM);
732 rc = emR3RemStep(pVM, pVCpu);
733 }
734 break;
735
736 /*
737 * Simple events: stepped, breakpoint, stop/assertion.
738 */
739 case VINF_EM_DBG_STEPPED:
740 rc = DBGFR3Event(pVM, DBGFEVENT_STEPPED);
741 break;
742
743 case VINF_EM_DBG_BREAKPOINT:
744 rc = DBGFR3EventBreakpoint(pVM, DBGFEVENT_BREAKPOINT);
745 break;
746
747 case VINF_EM_DBG_STOP:
748 rc = DBGFR3EventSrc(pVM, DBGFEVENT_DEV_STOP, NULL, 0, NULL, NULL);
749 break;
750
751 case VINF_EM_DBG_HYPER_STEPPED:
752 rc = DBGFR3Event(pVM, DBGFEVENT_STEPPED_HYPER);
753 break;
754
755 case VINF_EM_DBG_HYPER_BREAKPOINT:
756 rc = DBGFR3EventBreakpoint(pVM, DBGFEVENT_BREAKPOINT_HYPER);
757 break;
758
759 case VINF_EM_DBG_HYPER_ASSERTION:
760 RTPrintf("\nVINF_EM_DBG_HYPER_ASSERTION:\n%s%s\n", VMMR3GetRZAssertMsg1(pVM), VMMR3GetRZAssertMsg2(pVM));
761 rc = DBGFR3EventAssertion(pVM, DBGFEVENT_ASSERTION_HYPER, VMMR3GetRZAssertMsg1(pVM), VMMR3GetRZAssertMsg2(pVM));
762 break;
763
764 /*
765 * Guru meditation.
766 */
767 case VERR_VMM_RING0_ASSERTION: /** @todo Make a guru meditation event! */
768 rc = DBGFR3EventSrc(pVM, DBGFEVENT_FATAL_ERROR, "VERR_VMM_RING0_ASSERTION", 0, NULL, NULL);
769 break;
770 case VERR_REM_TOO_MANY_TRAPS: /** @todo Make a guru meditation event! */
771 rc = DBGFR3EventSrc(pVM, DBGFEVENT_DEV_STOP, "VERR_REM_TOO_MANY_TRAPS", 0, NULL, NULL);
772 break;
773
774 default: /** @todo don't use default for guru, but make special errors code! */
775 rc = DBGFR3Event(pVM, DBGFEVENT_FATAL_ERROR);
776 break;
777 }
778
779 /*
780 * Process the result.
781 */
782 do
783 {
784 switch (rc)
785 {
786 /*
787 * Continue the debugging loop.
788 */
789 case VINF_EM_DBG_STEP:
790 case VINF_EM_DBG_STOP:
791 case VINF_EM_DBG_STEPPED:
792 case VINF_EM_DBG_BREAKPOINT:
793 case VINF_EM_DBG_HYPER_STEPPED:
794 case VINF_EM_DBG_HYPER_BREAKPOINT:
795 case VINF_EM_DBG_HYPER_ASSERTION:
796 break;
797
798 /*
799 * Resuming execution (in some form) has to be done here if we got
800 * a hypervisor debug event.
801 */
802 case VINF_SUCCESS:
803 case VINF_EM_RESUME:
804 case VINF_EM_SUSPEND:
805 case VINF_EM_RESCHEDULE:
806 case VINF_EM_RESCHEDULE_RAW:
807 case VINF_EM_RESCHEDULE_REM:
808 case VINF_EM_HALT:
809 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
810 {
811 rc = emR3RawResumeHyper(pVM, pVCpu);
812 if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
813 continue;
814 }
815 if (rc == VINF_SUCCESS)
816 rc = VINF_EM_RESCHEDULE;
817 return rc;
818
819 /*
820 * The debugger isn't attached.
821 * We'll simply turn the thing off since that's the easiest thing to do.
822 */
823 case VERR_DBGF_NOT_ATTACHED:
824 switch (rcLast)
825 {
826 case VINF_EM_DBG_HYPER_STEPPED:
827 case VINF_EM_DBG_HYPER_BREAKPOINT:
828 case VINF_EM_DBG_HYPER_ASSERTION:
829 case VERR_TRPM_PANIC:
830 case VERR_TRPM_DONT_PANIC:
831 case VERR_VMM_RING0_ASSERTION:
832 return rcLast;
833 }
834 return VINF_EM_OFF;
835
836 /*
837 * Status codes terminating the VM in one or another sense.
838 */
839 case VINF_EM_TERMINATE:
840 case VINF_EM_OFF:
841 case VINF_EM_RESET:
842 case VINF_EM_NO_MEMORY:
843 case VINF_EM_RAW_STALE_SELECTOR:
844 case VINF_EM_RAW_IRET_TRAP:
845 case VERR_TRPM_PANIC:
846 case VERR_TRPM_DONT_PANIC:
847 case VERR_VMM_RING0_ASSERTION:
848 case VERR_INTERNAL_ERROR:
849 case VERR_INTERNAL_ERROR_2:
850 case VERR_INTERNAL_ERROR_3:
851 case VERR_INTERNAL_ERROR_4:
852 case VERR_INTERNAL_ERROR_5:
853 case VERR_IPE_UNEXPECTED_STATUS:
854 case VERR_IPE_UNEXPECTED_INFO_STATUS:
855 case VERR_IPE_UNEXPECTED_ERROR_STATUS:
856 return rc;
857
858 /*
859 * The rest is unexpected, and will keep us here.
860 */
861 default:
862 AssertMsgFailed(("Unxpected rc %Rrc!\n", rc));
863 break;
864 }
865 } while (false);
866 } /* debug for ever */
867}
868
869/**
870 * Locks REM execution to a single VCpu
871 *
872 * @param pVM VM handle.
873 */
874static void emR3RemLock(PVM pVM)
875{
876 int rc = PDMCritSectEnter(&pVM->em.s.CritSectREM, VERR_SEM_BUSY);
877 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
878}
879
880/**
881 * Unlocks REM execution
882 *
883 * @param pVM VM handle.
884 */
885static void emR3RemUnlock(PVM pVM)
886{
887 PDMCritSectLeave(&pVM->em.s.CritSectREM);
888}
889
890/**
891 * Steps recompiled code.
892 *
893 * @returns VBox status code. The most important ones are: VINF_EM_STEP_EVENT,
894 * VINF_EM_RESCHEDULE, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
895 *
896 * @param pVM VM handle.
897 * @param pVCpu VMCPU handle.
898 */
899static int emR3RemStep(PVM pVM, PVMCPU pVCpu)
900{
901 LogFlow(("emR3RemStep: cs:eip=%04x:%08x\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
902
903 emR3RemLock(pVM);
904
905 /*
906 * Switch to REM, step instruction, switch back.
907 */
908 int rc = REMR3State(pVM, pVCpu);
909 if (RT_SUCCESS(rc))
910 {
911 rc = REMR3Step(pVM, pVCpu);
912 REMR3StateBack(pVM, pVCpu);
913 }
914 emR3RemUnlock(pVM);
915
916 LogFlow(("emR3RemStep: returns %Rrc cs:eip=%04x:%08x\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
917 return rc;
918}
919
920
921/**
922 * Executes recompiled code.
923 *
924 * This function contains the recompiler version of the inner
925 * execution loop (the outer loop being in EMR3ExecuteVM()).
926 *
927 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
928 * VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
929 *
930 * @param pVM VM handle.
931 * @param pVCpu VMCPU handle.
932 * @param pfFFDone Where to store an indicator telling wheter or not
933 * FFs were done before returning.
934 *
935 */
936static int emR3RemExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
937{
938#ifdef LOG_ENABLED
939 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
940 uint32_t cpl = CPUMGetGuestCPL(pVCpu, CPUMCTX2CORE(pCtx));
941
942 if (pCtx->eflags.Bits.u1VM)
943 Log(("EMV86: %04X:%08X IF=%d\n", pCtx->cs, pCtx->eip, pCtx->eflags.Bits.u1IF));
944 else
945 Log(("EMR%d: %04X:%08X ESP=%08X IF=%d CR0=%x\n", cpl, pCtx->cs, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, (uint32_t)pCtx->cr0));
946#endif
947 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatREMTotal, a);
948
949#if defined(VBOX_STRICT) && defined(DEBUG_bird)
950 AssertMsg( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
951 || !MMHyperIsInsideArea(pVM, CPUMGetGuestEIP(pVCpu)), /** @todo #1419 - get flat address. */
952 ("cs:eip=%RX16:%RX32\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
953#endif
954
955 /* Big lock, but you are not supposed to own any lock when coming in here. */
956 emR3RemLock(pVM);
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
966 /* Flush the recompiler TLB if the VCPU has changed. */
967 if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
968 {
969 REMFlushTBs(pVM);
970 /* Also sync the entire state. */
971 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
972 }
973 pVM->em.s.idLastRemCpu = pVCpu->idCpu;
974
975 for (;;)
976 {
977 /*
978 * Update REM state if not already in sync.
979 */
980 if (!fInREMState)
981 {
982 STAM_PROFILE_START(&pVCpu->em.s.StatREMSync, b);
983 rc = REMR3State(pVM, pVCpu);
984 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, b);
985 if (RT_FAILURE(rc))
986 break;
987 fInREMState = true;
988
989 /*
990 * We might have missed the raising of VMREQ, TIMER and some other
991 * imporant FFs while we were busy switching the state. So, check again.
992 */
993 if ( VM_FF_ISPENDING(pVM, VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_DBGF | VM_FF_TERMINATE | VM_FF_RESET)
994 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER | VMCPU_FF_REQUEST))
995 {
996 LogFlow(("emR3RemExecute: Skipping run, because FF is set. %#x\n", pVM->fGlobalForcedActions));
997 goto l_REMDoForcedActions;
998 }
999 }
1000
1001
1002 /*
1003 * Execute REM.
1004 */
1005 STAM_PROFILE_START(&pVCpu->em.s.StatREMExec, c);
1006 rc = REMR3Run(pVM, pVCpu);
1007 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMExec, c);
1008
1009
1010 /*
1011 * Deal with high priority post execution FFs before doing anything else.
1012 */
1013 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
1014 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
1015 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1016
1017 /*
1018 * Process the returned status code.
1019 * (Try keep this short! Call functions!)
1020 */
1021 if (rc != VINF_SUCCESS)
1022 {
1023 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
1024 break;
1025 if (rc != VINF_REM_INTERRUPED_FF)
1026 {
1027 /*
1028 * Anything which is not known to us means an internal error
1029 * and the termination of the VM!
1030 */
1031 AssertMsg(rc == VERR_REM_TOO_MANY_TRAPS, ("Unknown GC return code: %Rra\n", rc));
1032 break;
1033 }
1034 }
1035
1036
1037 /*
1038 * Check and execute forced actions.
1039 * Sync back the VM state before calling any of these.
1040 */
1041#ifdef VBOX_HIGH_RES_TIMERS_HACK
1042 TMTimerPollVoid(pVM, pVCpu);
1043#endif
1044 AssertCompile((VMCPU_FF_ALL_BUT_RAW_MASK & ~(VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_CSAM_SCAN_PAGE)) & VMCPU_FF_TIMER);
1045 if ( VM_FF_ISPENDING(pVM, VM_FF_ALL_BUT_RAW_MASK)
1046 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_ALL_BUT_RAW_MASK & ~(VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_CSAM_SCAN_PAGE)))
1047 {
1048l_REMDoForcedActions:
1049 if (fInREMState)
1050 {
1051 STAM_PROFILE_START(&pVCpu->em.s.StatREMSync, d);
1052 REMR3StateBack(pVM, pVCpu);
1053 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, d);
1054 fInREMState = false;
1055 }
1056 STAM_REL_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatREMTotal, a);
1057 rc = emR3ForcedActions(pVM, pVCpu, rc);
1058 STAM_REL_PROFILE_ADV_RESUME(&pVCpu->em.s.StatREMTotal, a);
1059 if ( rc != VINF_SUCCESS
1060 && rc != VINF_EM_RESCHEDULE_REM)
1061 {
1062 *pfFFDone = true;
1063 break;
1064 }
1065 }
1066
1067 } /* The Inner Loop, recompiled execution mode version. */
1068
1069
1070 /*
1071 * Returning. Sync back the VM state if required.
1072 */
1073 if (fInREMState)
1074 {
1075 STAM_PROFILE_START(&pVCpu->em.s.StatREMSync, e);
1076 REMR3StateBack(pVM, pVCpu);
1077 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, e);
1078 }
1079 emR3RemUnlock(pVM);
1080
1081 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatREMTotal, a);
1082 return rc;
1083}
1084
1085
1086/**
1087 * Resumes executing hypervisor after a debug event.
1088 *
1089 * This is kind of special since our current guest state is
1090 * potentially out of sync.
1091 *
1092 * @returns VBox status code.
1093 * @param pVM The VM handle.
1094 * @param pVCpu The VMCPU handle.
1095 */
1096static int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu)
1097{
1098 int rc;
1099 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1100 Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER);
1101 Log(("emR3RawResumeHyper: cs:eip=%RTsel:%RGr efl=%RGr\n", pCtx->cs, pCtx->eip, pCtx->eflags));
1102
1103 /*
1104 * Resume execution.
1105 */
1106 CPUMRawEnter(pVCpu, NULL);
1107 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_RF);
1108 rc = VMMR3ResumeHyper(pVM, pVCpu);
1109 Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr - returned from GC with rc=%Rrc\n", pCtx->cs, pCtx->eip, pCtx->eflags, rc));
1110 rc = CPUMRawLeave(pVCpu, NULL, rc);
1111 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
1112
1113 /*
1114 * Deal with the return code.
1115 */
1116 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1117 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
1118 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
1119 return rc;
1120}
1121
1122
1123/**
1124 * Steps rawmode.
1125 *
1126 * @returns VBox status code.
1127 * @param pVM The VM handle.
1128 * @param pVCpu The VMCPU handle.
1129 */
1130static int emR3RawStep(PVM pVM, PVMCPU pVCpu)
1131{
1132 Assert( pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER
1133 || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_RAW
1134 || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_REM);
1135 int rc;
1136 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1137 bool fGuest = pVCpu->em.s.enmState != EMSTATE_DEBUG_HYPER;
1138#ifndef DEBUG_sandervl
1139 Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
1140 fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu)));
1141#endif
1142 if (fGuest)
1143 {
1144 /*
1145 * Check vital forced actions, but ignore pending interrupts and timers.
1146 */
1147 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
1148 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
1149 {
1150 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
1151 if (rc != VINF_SUCCESS)
1152 return rc;
1153 }
1154
1155 /*
1156 * Set flags for single stepping.
1157 */
1158 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
1159 }
1160 else
1161 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
1162
1163 /*
1164 * Single step.
1165 * We do not start time or anything, if anything we should just do a few nanoseconds.
1166 */
1167 CPUMRawEnter(pVCpu, NULL);
1168 do
1169 {
1170 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
1171 rc = VMMR3ResumeHyper(pVM, pVCpu);
1172 else
1173 rc = VMMR3RawRunGC(pVM, pVCpu);
1174#ifndef DEBUG_sandervl
1175 Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr - GC rc %Rrc\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
1176 fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu), rc));
1177#endif
1178 } while ( rc == VINF_SUCCESS
1179 || rc == VINF_EM_RAW_INTERRUPT);
1180 rc = CPUMRawLeave(pVCpu, NULL, rc);
1181 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
1182
1183 /*
1184 * Make sure the trap flag is cleared.
1185 * (Too bad if the guest is trying to single step too.)
1186 */
1187 if (fGuest)
1188 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1189 else
1190 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) & ~X86_EFL_TF);
1191
1192 /*
1193 * Deal with the return codes.
1194 */
1195 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1196 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
1197 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
1198 return rc;
1199}
1200
1201
1202#ifdef DEBUG
1203
1204/**
1205 * Steps hardware accelerated mode.
1206 *
1207 * @returns VBox status code.
1208 * @param pVM The VM handle.
1209 * @param pVCpu The VMCPU handle.
1210 */
1211static int emR3HwAccStep(PVM pVM, PVMCPU pVCpu)
1212{
1213 Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HWACC);
1214
1215 int rc;
1216 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1217 VMCPU_FF_CLEAR(pVCpu, (VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_TSS));
1218
1219 /*
1220 * Check vital forced actions, but ignore pending interrupts and timers.
1221 */
1222 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
1223 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
1224 {
1225 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
1226 if (rc != VINF_SUCCESS)
1227 return rc;
1228 }
1229 /*
1230 * Set flags for single stepping.
1231 */
1232 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
1233
1234 /*
1235 * Single step.
1236 * We do not start time or anything, if anything we should just do a few nanoseconds.
1237 */
1238 do
1239 {
1240 rc = VMMR3HwAccRunGC(pVM, pVCpu);
1241 } while ( rc == VINF_SUCCESS
1242 || rc == VINF_EM_RAW_INTERRUPT);
1243 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
1244
1245 /*
1246 * Make sure the trap flag is cleared.
1247 * (Too bad if the guest is trying to single step too.)
1248 */
1249 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1250
1251 /*
1252 * Deal with the return codes.
1253 */
1254 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1255 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
1256 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
1257 return rc;
1258}
1259
1260
1261int emR3SingleStepExecRaw(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
1262{
1263 int rc = VINF_SUCCESS;
1264 EMSTATE enmOldState = pVCpu->em.s.enmState;
1265 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
1266
1267 Log(("Single step BEGIN:\n"));
1268 for (uint32_t i = 0; i < cIterations; i++)
1269 {
1270 DBGFR3PrgStep(pVCpu);
1271 DBGFR3DisasInstrCurrentLog(pVCpu, "RSS: ");
1272 rc = emR3RawStep(pVM, pVCpu);
1273 if (rc != VINF_SUCCESS)
1274 break;
1275 }
1276 Log(("Single step END: rc=%Rrc\n", rc));
1277 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1278 pVCpu->em.s.enmState = enmOldState;
1279 return rc;
1280}
1281
1282
1283static int emR3SingleStepExecHwAcc(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
1284{
1285 int rc = VINF_SUCCESS;
1286 EMSTATE enmOldState = pVCpu->em.s.enmState;
1287 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_HWACC;
1288
1289 Log(("Single step BEGIN:\n"));
1290 for (uint32_t i = 0; i < cIterations; i++)
1291 {
1292 DBGFR3PrgStep(pVCpu);
1293 DBGFR3DisasInstrCurrentLog(pVCpu, "RSS: ");
1294 rc = emR3HwAccStep(pVM, pVCpu);
1295 if ( rc != VINF_SUCCESS
1296 || !HWACCMR3CanExecuteGuest(pVM, pVCpu->em.s.pCtx))
1297 break;
1298 }
1299 Log(("Single step END: rc=%Rrc\n", rc));
1300 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1301 pVCpu->em.s.enmState = enmOldState;
1302 return rc == VINF_SUCCESS ? VINF_EM_RESCHEDULE_REM : rc;
1303}
1304
1305
1306static int emR3SingleStepExecRem(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
1307{
1308 EMSTATE enmOldState = pVCpu->em.s.enmState;
1309
1310 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_REM;
1311
1312 Log(("Single step BEGIN:\n"));
1313 for (uint32_t i = 0; i < cIterations; i++)
1314 {
1315 DBGFR3PrgStep(pVCpu);
1316 DBGFR3DisasInstrCurrentLog(pVCpu, "RSS: ");
1317 emR3RemStep(pVM, pVCpu);
1318 if (emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx) != EMSTATE_REM)
1319 break;
1320 }
1321 Log(("Single step END:\n"));
1322 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
1323 pVCpu->em.s.enmState = enmOldState;
1324 return VINF_EM_RESCHEDULE;
1325}
1326
1327#endif /* DEBUG */
1328
1329
1330/**
1331 * Executes one (or perhaps a few more) instruction(s).
1332 *
1333 * @returns VBox status code suitable for EM.
1334 *
1335 * @param pVM VM handle.
1336 * @param pVCpu VMCPU handle
1337 * @param rcGC GC return code
1338 * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
1339 * instruction and prefix the log output with this text.
1340 */
1341#ifdef LOG_ENABLED
1342static int emR3RawExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC, const char *pszPrefix)
1343#else
1344static int emR3RawExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC)
1345#endif
1346{
1347 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1348 int rc;
1349
1350 /*
1351 *
1352 * The simple solution is to use the recompiler.
1353 * The better solution is to disassemble the current instruction and
1354 * try handle as many as possible without using REM.
1355 *
1356 */
1357
1358#ifdef LOG_ENABLED
1359 /*
1360 * Disassemble the instruction if requested.
1361 */
1362 if (pszPrefix)
1363 {
1364 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
1365 DBGFR3DisasInstrCurrentLog(pVCpu, pszPrefix);
1366 }
1367#endif /* LOG_ENABLED */
1368
1369 /*
1370 * PATM is making life more interesting.
1371 * We cannot hand anything to REM which has an EIP inside patch code. So, we'll
1372 * tell PATM there is a trap in this code and have it take the appropriate actions
1373 * to allow us execute the code in REM.
1374 */
1375 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
1376 {
1377 Log(("emR3RawExecuteInstruction: In patch block. eip=%RRv\n", (RTRCPTR)pCtx->eip));
1378
1379 RTGCPTR pNewEip;
1380 rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &pNewEip);
1381 switch (rc)
1382 {
1383 /*
1384 * It's not very useful to emulate a single instruction and then go back to raw
1385 * mode; just execute the whole block until IF is set again.
1386 */
1387 case VINF_SUCCESS:
1388 Log(("emR3RawExecuteInstruction: Executing instruction starting at new address %RGv IF=%d VMIF=%x\n",
1389 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
1390 pCtx->eip = pNewEip;
1391 Assert(pCtx->eip);
1392
1393 if (pCtx->eflags.Bits.u1IF)
1394 {
1395 /*
1396 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
1397 */
1398 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
1399 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
1400 }
1401 else if (rcGC == VINF_PATM_PENDING_IRQ_AFTER_IRET)
1402 {
1403 /* special case: iret, that sets IF, detected a pending irq/event */
1404 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIRET");
1405 }
1406 return VINF_EM_RESCHEDULE_REM;
1407
1408 /*
1409 * One instruction.
1410 */
1411 case VINF_PATCH_EMULATE_INSTR:
1412 Log(("emR3RawExecuteInstruction: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
1413 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
1414 pCtx->eip = pNewEip;
1415 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
1416
1417 /*
1418 * The patch was disabled, hand it to the REM.
1419 */
1420 case VERR_PATCH_DISABLED:
1421 Log(("emR3RawExecuteInstruction: Disabled patch -> new eip %RGv IF=%d VMIF=%x\n",
1422 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
1423 pCtx->eip = pNewEip;
1424 if (pCtx->eflags.Bits.u1IF)
1425 {
1426 /*
1427 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
1428 */
1429 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
1430 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
1431 }
1432 return VINF_EM_RESCHEDULE_REM;
1433
1434 /* Force continued patch exection; usually due to write monitored stack. */
1435 case VINF_PATCH_CONTINUE:
1436 return VINF_SUCCESS;
1437
1438 default:
1439 AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap\n", rc));
1440 return VERR_IPE_UNEXPECTED_STATUS;
1441 }
1442 }
1443
1444#if 0
1445 /* Try our own instruction emulator before falling back to the recompiler. */
1446 DISCPUSTATE Cpu;
1447 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "GEN EMU");
1448 if (RT_SUCCESS(rc))
1449 {
1450 uint32_t size;
1451
1452 switch (Cpu.pCurInstr->opcode)
1453 {
1454 /* @todo we can do more now */
1455 case OP_MOV:
1456 case OP_AND:
1457 case OP_OR:
1458 case OP_XOR:
1459 case OP_POP:
1460 case OP_INC:
1461 case OP_DEC:
1462 case OP_XCHG:
1463 STAM_PROFILE_START(&pVCpu->em.s.StatMiscEmu, a);
1464 rc = EMInterpretInstructionCPU(pVM, &Cpu, CPUMCTX2CORE(pCtx), 0, &size);
1465 if (RT_SUCCESS(rc))
1466 {
1467 pCtx->rip += Cpu.opsize;
1468#ifdef EM_NOTIFY_HWACCM
1469 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HWACC)
1470 HWACCMR3NotifyEmulated(pVCpu);
1471#endif
1472 STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
1473 return rc;
1474 }
1475 if (rc != VERR_EM_INTERPRETER)
1476 AssertMsgFailedReturn(("rc=%Rrc\n", rc), rc);
1477 STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
1478 break;
1479 }
1480 }
1481#endif /* 0 */
1482 STAM_PROFILE_START(&pVCpu->em.s.StatREMEmu, a);
1483 Log(("EMINS: %04x:%RGv RSP=%RGv\n", pCtx->cs, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
1484 emR3RemLock(pVM);
1485 /* Flush the recompiler TLB if the VCPU has changed. */
1486 if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
1487 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
1488 pVM->em.s.idLastRemCpu = pVCpu->idCpu;
1489
1490 rc = REMR3EmulateInstruction(pVM, pVCpu);
1491 emR3RemUnlock(pVM);
1492 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMEmu, a);
1493
1494#ifdef EM_NOTIFY_HWACCM
1495 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HWACC)
1496 HWACCMR3NotifyEmulated(pVCpu);
1497#endif
1498 return rc;
1499}
1500
1501
1502/**
1503 * Executes one (or perhaps a few more) instruction(s).
1504 * This is just a wrapper for discarding pszPrefix in non-logging builds.
1505 *
1506 * @returns VBox status code suitable for EM.
1507 * @param pVM VM handle.
1508 * @param pVCpu VMCPU handle.
1509 * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
1510 * instruction and prefix the log output with this text.
1511 * @param rcGC GC return code
1512 */
1513DECLINLINE(int) emR3RawExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC)
1514{
1515#ifdef LOG_ENABLED
1516 return emR3RawExecuteInstructionWorker(pVM, pVCpu, rcGC, pszPrefix);
1517#else
1518 return emR3RawExecuteInstructionWorker(pVM, pVCpu, rcGC);
1519#endif
1520}
1521
1522/**
1523 * Executes one (or perhaps a few more) IO instruction(s).
1524 *
1525 * @returns VBox status code suitable for EM.
1526 * @param pVM VM handle.
1527 * @param pVCpu VMCPU handle.
1528 */
1529int emR3RawExecuteIOInstruction(PVM pVM, PVMCPU pVCpu)
1530{
1531 int rc;
1532 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1533
1534 STAM_PROFILE_START(&pVCpu->em.s.StatIOEmu, a);
1535
1536 /** @todo probably we should fall back to the recompiler; otherwise we'll go back and forth between HC & GC
1537 * as io instructions tend to come in packages of more than one
1538 */
1539 DISCPUSTATE Cpu;
1540 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "IO EMU");
1541 if (RT_SUCCESS(rc))
1542 {
1543 rc = VINF_EM_RAW_EMULATE_INSTR;
1544
1545 if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
1546 {
1547 switch (Cpu.pCurInstr->opcode)
1548 {
1549 case OP_IN:
1550 {
1551 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
1552 rc = IOMInterpretIN(pVM, CPUMCTX2CORE(pCtx), &Cpu);
1553 break;
1554 }
1555
1556 case OP_OUT:
1557 {
1558 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
1559 rc = IOMInterpretOUT(pVM, CPUMCTX2CORE(pCtx), &Cpu);
1560 break;
1561 }
1562 }
1563 }
1564 else if (Cpu.prefix & PREFIX_REP)
1565 {
1566 switch (Cpu.pCurInstr->opcode)
1567 {
1568 case OP_INSB:
1569 case OP_INSWD:
1570 {
1571 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
1572 rc = IOMInterpretINS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
1573 break;
1574 }
1575
1576 case OP_OUTSB:
1577 case OP_OUTSWD:
1578 {
1579 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
1580 rc = IOMInterpretOUTS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
1581 break;
1582 }
1583 }
1584 }
1585
1586 /*
1587 * Handled the I/O return codes.
1588 * (The unhandled cases end up with rc == VINF_EM_RAW_EMULATE_INSTR.)
1589 */
1590 if (IOM_SUCCESS(rc))
1591 {
1592 pCtx->rip += Cpu.opsize;
1593 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
1594 return rc;
1595 }
1596
1597 if (rc == VINF_EM_RAW_GUEST_TRAP)
1598 {
1599 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
1600 rc = emR3RawGuestTrap(pVM, pVCpu);
1601 return rc;
1602 }
1603 AssertMsg(rc != VINF_TRPM_XCPT_DISPATCHED, ("Handle VINF_TRPM_XCPT_DISPATCHED\n"));
1604
1605 if (RT_FAILURE(rc))
1606 {
1607 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
1608 return rc;
1609 }
1610 AssertMsg(rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RESCHEDULE_REM, ("rc=%Rrc\n", rc));
1611 }
1612 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
1613 return emR3RawExecuteInstruction(pVM, pVCpu, "IO: ");
1614}
1615
1616
1617/**
1618 * Handle a guest context trap.
1619 *
1620 * @returns VBox status code suitable for EM.
1621 * @param pVM VM handle.
1622 * @param pVCpu VMCPU handle.
1623 */
1624static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu)
1625{
1626 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1627
1628 /*
1629 * Get the trap info.
1630 */
1631 uint8_t u8TrapNo;
1632 TRPMEVENT enmType;
1633 RTGCUINT uErrorCode;
1634 RTGCUINTPTR uCR2;
1635 int rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2);
1636 if (RT_FAILURE(rc))
1637 {
1638 AssertReleaseMsgFailed(("No trap! (rc=%Rrc)\n", rc));
1639 return rc;
1640 }
1641
1642 /*
1643 * Traps can be directly forwarded in hardware accelerated mode.
1644 */
1645 if (HWACCMIsEnabled(pVM))
1646 {
1647#ifdef LOGGING_ENABLED
1648 DBGFR3InfoLog(pVM, "cpumguest", "Guest trap");
1649 DBGFR3DisasInstrCurrentLog(pVCpu, "Guest trap");
1650#endif
1651 return VINF_EM_RESCHEDULE_HWACC;
1652 }
1653
1654#if 1 /* Experimental: Review, disable if it causes trouble. */
1655 /*
1656 * Handle traps in patch code first.
1657 *
1658 * We catch a few of these cases in RC before returning to R3 (#PF, #GP, #BP)
1659 * but several traps isn't handled specially by TRPM in RC and we end up here
1660 * instead. One example is #DE.
1661 */
1662 uint32_t uCpl = CPUMGetGuestCPL(pVCpu, CPUMCTX2CORE(pCtx));
1663 if ( uCpl == 0
1664 && PATMIsPatchGCAddr(pVM, (RTGCPTR)pCtx->eip))
1665 {
1666 LogFlow(("emR3RawGuestTrap: trap %#x in patch code; eip=%08x\n", u8TrapNo, pCtx->eip));
1667 return emR3PatchTrap(pVM, pVCpu, pCtx, rc);
1668 }
1669#endif
1670
1671 /*
1672 * If the guest gate is marked unpatched, then we will check again if we can patch it.
1673 * (This assumes that we've already tried and failed to dispatch the trap in
1674 * RC for the gates that already has been patched. Which is true for most high
1675 * volume traps, because these are handled specially, but not for odd ones like #DE.)
1676 */
1677 if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) == TRPM_INVALID_HANDLER)
1678 {
1679 CSAMR3CheckGates(pVM, u8TrapNo, 1);
1680 Log(("emR3RawHandleRC: recheck gate %x -> valid=%d\n", u8TrapNo, TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER));
1681
1682 /* If it was successful, then we could go back to raw mode. */
1683 if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER)
1684 {
1685 /* Must check pending forced actions as our IDT or GDT might be out of sync. */
1686 rc = EMR3CheckRawForcedActions(pVM, pVCpu);
1687 AssertRCReturn(rc, rc);
1688
1689 TRPMERRORCODE enmError = uErrorCode != ~0U
1690 ? TRPM_TRAP_HAS_ERRORCODE
1691 : TRPM_TRAP_NO_ERRORCODE;
1692 rc = TRPMForwardTrap(pVCpu, CPUMCTX2CORE(pCtx), u8TrapNo, uErrorCode, enmError, TRPM_TRAP, -1);
1693 if (rc == VINF_SUCCESS /* Don't use RT_SUCCESS */)
1694 {
1695 TRPMResetTrap(pVCpu);
1696 return VINF_EM_RESCHEDULE_RAW;
1697 }
1698 AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP, ("%Rrc\n", rc));
1699 }
1700 }
1701
1702 /*
1703 * Scan kernel code that traps; we might not get another chance.
1704 */
1705 /** @todo move this up before the dispatching? */
1706 if ( (pCtx->ss & X86_SEL_RPL) <= 1
1707 && !pCtx->eflags.Bits.u1VM)
1708 {
1709 Assert(!PATMIsPatchGCAddr(pVM, pCtx->eip));
1710 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
1711 }
1712
1713 /*
1714 * Trap specific handling.
1715 */
1716 if (u8TrapNo == 6) /* (#UD) Invalid opcode. */
1717 {
1718 /*
1719 * If MONITOR & MWAIT are supported, then interpret them here.
1720 */
1721 DISCPUSTATE cpu;
1722 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap (#UD): ");
1723 if ( RT_SUCCESS(rc)
1724 && (cpu.pCurInstr->opcode == OP_MONITOR || cpu.pCurInstr->opcode == OP_MWAIT))
1725 {
1726 uint32_t u32Dummy, u32Features, u32ExtFeatures;
1727 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Features);
1728 if (u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR)
1729 {
1730 rc = TRPMResetTrap(pVCpu);
1731 AssertRC(rc);
1732
1733 uint32_t opsize;
1734 rc = EMInterpretInstructionCPU(pVM, pVCpu, &cpu, CPUMCTX2CORE(pCtx), 0, &opsize);
1735 if (RT_SUCCESS(rc))
1736 {
1737 pCtx->rip += cpu.opsize;
1738#ifdef EM_NOTIFY_HWACCM
1739 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HWACC)
1740 HWACCMR3NotifyEmulated(pVCpu);
1741#endif
1742 return rc;
1743 }
1744 return emR3RawExecuteInstruction(pVM, pVCpu, "Monitor: ");
1745 }
1746 }
1747 }
1748 else if (u8TrapNo == 13) /* (#GP) Privileged exception */
1749 {
1750 /*
1751 * Handle I/O bitmap?
1752 */
1753 /** @todo We're not supposed to be here with a false guest trap concerning
1754 * I/O access. We can easily handle those in RC. */
1755 DISCPUSTATE cpu;
1756 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap: ");
1757 if ( RT_SUCCESS(rc)
1758 && (cpu.pCurInstr->optype & OPTYPE_PORTIO))
1759 {
1760 /*
1761 * We should really check the TSS for the IO bitmap, but it's not like this
1762 * lazy approach really makes things worse.
1763 */
1764 rc = TRPMResetTrap(pVCpu);
1765 AssertRC(rc);
1766 return emR3RawExecuteInstruction(pVM, pVCpu, "IO Guest Trap: ");
1767 }
1768 }
1769
1770#ifdef LOG_ENABLED
1771 DBGFR3InfoLog(pVM, "cpumguest", "Guest trap");
1772 DBGFR3DisasInstrCurrentLog(pVCpu, "Guest trap");
1773
1774 /* Get guest page information. */
1775 uint64_t fFlags = 0;
1776 RTGCPHYS GCPhys = 0;
1777 int rc2 = PGMGstGetPage(pVCpu, uCR2, &fFlags, &GCPhys);
1778 Log(("emR3RawGuestTrap: cs:eip=%04x:%08x: trap=%02x err=%08x cr2=%08x cr0=%08x%s: Phys=%RGp fFlags=%08llx %s %s %s%s rc2=%d\n",
1779 pCtx->cs, pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0, (enmType == TRPM_SOFTWARE_INT) ? " software" : "", GCPhys, fFlags,
1780 fFlags & X86_PTE_P ? "P " : "NP", fFlags & X86_PTE_US ? "U" : "S",
1781 fFlags & X86_PTE_RW ? "RW" : "R0", fFlags & X86_PTE_G ? " G" : "", rc2));
1782#endif
1783
1784 /*
1785 * #PG has CR2.
1786 * (Because of stuff like above we must set CR2 in a delayed fashion.)
1787 */
1788 if (u8TrapNo == 14 /* #PG */)
1789 pCtx->cr2 = uCR2;
1790
1791 return VINF_EM_RESCHEDULE_REM;
1792}
1793
1794
1795/**
1796 * Handle a ring switch trap.
1797 * Need to do statistics and to install patches. The result is going to REM.
1798 *
1799 * @returns VBox status code suitable for EM.
1800 * @param pVM VM handle.
1801 * @param pVCpu VMCPU handle.
1802 */
1803int emR3RawRingSwitch(PVM pVM, PVMCPU pVCpu)
1804{
1805 int rc;
1806 DISCPUSTATE Cpu;
1807 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1808
1809 /*
1810 * sysenter, syscall & callgate
1811 */
1812 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "RSWITCH: ");
1813 if (RT_SUCCESS(rc))
1814 {
1815 if (Cpu.pCurInstr->opcode == OP_SYSENTER)
1816 {
1817 if (pCtx->SysEnter.cs != 0)
1818 {
1819 rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
1820 (SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT) ? PATMFL_CODE32 : 0);
1821 if (RT_SUCCESS(rc))
1822 {
1823 DBGFR3DisasInstrCurrentLog(pVCpu, "Patched sysenter instruction");
1824 return VINF_EM_RESCHEDULE_RAW;
1825 }
1826 }
1827 }
1828
1829#ifdef VBOX_WITH_STATISTICS
1830 switch (Cpu.pCurInstr->opcode)
1831 {
1832 case OP_SYSENTER:
1833 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysEnter);
1834 break;
1835 case OP_SYSEXIT:
1836 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysExit);
1837 break;
1838 case OP_SYSCALL:
1839 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysCall);
1840 break;
1841 case OP_SYSRET:
1842 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysRet);
1843 break;
1844 }
1845#endif
1846 }
1847 else
1848 AssertRC(rc);
1849
1850 /* go to the REM to emulate a single instruction */
1851 return emR3RawExecuteInstruction(pVM, pVCpu, "RSWITCH: ");
1852}
1853
1854
1855/**
1856 * Handle a trap (\#PF or \#GP) in patch code
1857 *
1858 * @returns VBox status code suitable for EM.
1859 * @param pVM VM handle.
1860 * @param pVCpu VMCPU handle.
1861 * @param pCtx CPU context
1862 * @param gcret GC return code
1863 */
1864static int emR3PatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret)
1865{
1866 uint8_t u8TrapNo;
1867 int rc;
1868 TRPMEVENT enmType;
1869 RTGCUINT uErrorCode;
1870 RTGCUINTPTR uCR2;
1871
1872 Assert(PATMIsPatchGCAddr(pVM, pCtx->eip));
1873
1874 if (gcret == VINF_PATM_PATCH_INT3)
1875 {
1876 u8TrapNo = 3;
1877 uCR2 = 0;
1878 uErrorCode = 0;
1879 }
1880 else if (gcret == VINF_PATM_PATCH_TRAP_GP)
1881 {
1882 /* No active trap in this case. Kind of ugly. */
1883 u8TrapNo = X86_XCPT_GP;
1884 uCR2 = 0;
1885 uErrorCode = 0;
1886 }
1887 else
1888 {
1889 rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2);
1890 if (RT_FAILURE(rc))
1891 {
1892 AssertReleaseMsgFailed(("emR3PatchTrap: no trap! (rc=%Rrc) gcret=%Rrc\n", rc, gcret));
1893 return rc;
1894 }
1895 /* Reset the trap as we'll execute the original instruction again. */
1896 TRPMResetTrap(pVCpu);
1897 }
1898
1899 /*
1900 * Deal with traps inside patch code.
1901 * (This code won't run outside GC.)
1902 */
1903 if (u8TrapNo != 1)
1904 {
1905#ifdef LOG_ENABLED
1906 DBGFR3InfoLog(pVM, "cpumguest", "Trap in patch code");
1907 DBGFR3DisasInstrCurrentLog(pVCpu, "Patch code");
1908
1909 DISCPUSTATE Cpu;
1910 int rc;
1911
1912 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->eip, &Cpu, "Patch code: ");
1913 if ( RT_SUCCESS(rc)
1914 && Cpu.pCurInstr->opcode == OP_IRET)
1915 {
1916 uint32_t eip, selCS, uEFlags;
1917
1918 /* Iret crashes are bad as we have already changed the flags on the stack */
1919 rc = PGMPhysSimpleReadGCPtr(pVCpu, &eip, pCtx->esp, 4);
1920 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selCS, pCtx->esp+4, 4);
1921 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &uEFlags, pCtx->esp+8, 4);
1922 if (rc == VINF_SUCCESS)
1923 {
1924 if ( (uEFlags & X86_EFL_VM)
1925 || (selCS & X86_SEL_RPL) == 3)
1926 {
1927 uint32_t selSS, esp;
1928
1929 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &esp, pCtx->esp + 12, 4);
1930 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selSS, pCtx->esp + 16, 4);
1931
1932 if (uEFlags & X86_EFL_VM)
1933 {
1934 uint32_t selDS, selES, selFS, selGS;
1935 rc = PGMPhysSimpleReadGCPtr(pVCpu, &selES, pCtx->esp + 20, 4);
1936 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selDS, pCtx->esp + 24, 4);
1937 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selFS, pCtx->esp + 28, 4);
1938 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selGS, pCtx->esp + 32, 4);
1939 if (rc == VINF_SUCCESS)
1940 {
1941 Log(("Patch code: IRET->VM stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
1942 Log(("Patch code: IRET->VM stack frame: DS=%04X ES=%04X FS=%04X GS=%04X\n", selDS, selES, selFS, selGS));
1943 }
1944 }
1945 else
1946 Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
1947 }
1948 else
1949 Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x\n", selCS, eip, uEFlags));
1950 }
1951 }
1952#endif /* LOG_ENABLED */
1953 Log(("emR3PatchTrap: in patch: eip=%08x: trap=%02x err=%08x cr2=%08x cr0=%08x\n",
1954 pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0));
1955
1956 RTGCPTR pNewEip;
1957 rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &pNewEip);
1958 switch (rc)
1959 {
1960 /*
1961 * Execute the faulting instruction.
1962 */
1963 case VINF_SUCCESS:
1964 {
1965 /** @todo execute a whole block */
1966 Log(("emR3PatchTrap: Executing faulting instruction at new address %RGv\n", pNewEip));
1967 if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
1968 Log(("emR3PatchTrap: Virtual IF flag disabled!!\n"));
1969
1970 pCtx->eip = pNewEip;
1971 AssertRelease(pCtx->eip);
1972
1973 if (pCtx->eflags.Bits.u1IF)
1974 {
1975 /* Windows XP lets irets fault intentionally and then takes action based on the opcode; an
1976 * int3 patch overwrites it and leads to blue screens. Remove the patch in this case.
1977 */
1978 if ( u8TrapNo == X86_XCPT_GP
1979 && PATMIsInt3Patch(pVM, pCtx->eip, NULL, NULL))
1980 {
1981 /** @todo move to PATMR3HandleTrap */
1982 Log(("Possible Windows XP iret fault at %08RX32\n", pCtx->eip));
1983 PATMR3RemovePatch(pVM, pCtx->eip);
1984 }
1985
1986 /** @todo Knoppix 5 regression when returning VINF_SUCCESS here and going back to raw mode. */
1987 /* Note: possibly because a reschedule is required (e.g. iret to V86 code) */
1988
1989 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
1990 /* Interrupts are enabled; just go back to the original instruction.
1991 return VINF_SUCCESS; */
1992 }
1993 return VINF_EM_RESCHEDULE_REM;
1994 }
1995
1996 /*
1997 * One instruction.
1998 */
1999 case VINF_PATCH_EMULATE_INSTR:
2000 Log(("emR3PatchTrap: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
2001 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
2002 pCtx->eip = pNewEip;
2003 AssertRelease(pCtx->eip);
2004 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHEMUL: ");
2005
2006 /*
2007 * The patch was disabled, hand it to the REM.
2008 */
2009 case VERR_PATCH_DISABLED:
2010 if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
2011 Log(("emR3PatchTrap: Virtual IF flag disabled!!\n"));
2012 pCtx->eip = pNewEip;
2013 AssertRelease(pCtx->eip);
2014
2015 if (pCtx->eflags.Bits.u1IF)
2016 {
2017 /*
2018 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
2019 */
2020 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
2021 return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
2022 }
2023 return VINF_EM_RESCHEDULE_REM;
2024
2025 /* Force continued patch exection; usually due to write monitored stack. */
2026 case VINF_PATCH_CONTINUE:
2027 return VINF_SUCCESS;
2028
2029 /*
2030 * Anything else is *fatal*.
2031 */
2032 default:
2033 AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap!\n", rc));
2034 return VERR_IPE_UNEXPECTED_STATUS;
2035 }
2036 }
2037 return VINF_SUCCESS;
2038}
2039
2040
2041/**
2042 * Handle a privileged instruction.
2043 *
2044 * @returns VBox status code suitable for EM.
2045 * @param pVM VM handle.
2046 * @param pVCpu VMCPU handle;
2047 */
2048int emR3RawPrivileged(PVM pVM, PVMCPU pVCpu)
2049{
2050 STAM_PROFILE_START(&pVCpu->em.s.StatPrivEmu, a);
2051 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
2052
2053 Assert(!pCtx->eflags.Bits.u1VM);
2054
2055 if (PATMIsEnabled(pVM))
2056 {
2057 /*
2058 * Check if in patch code.
2059 */
2060 if (PATMR3IsInsidePatchJump(pVM, pCtx->eip, NULL))
2061 {
2062#ifdef LOG_ENABLED
2063 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
2064#endif
2065 AssertMsgFailed(("FATAL ERROR: executing random instruction inside generated patch jump %08X\n", pCtx->eip));
2066 return VERR_EM_RAW_PATCH_CONFLICT;
2067 }
2068 if ( (pCtx->ss & X86_SEL_RPL) == 0
2069 && !pCtx->eflags.Bits.u1VM
2070 && !PATMIsPatchGCAddr(pVM, pCtx->eip))
2071 {
2072 int rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
2073 (SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT) ? PATMFL_CODE32 : 0);
2074 if (RT_SUCCESS(rc))
2075 {
2076#ifdef LOG_ENABLED
2077 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
2078#endif
2079 DBGFR3DisasInstrCurrentLog(pVCpu, "Patched privileged instruction");
2080 return VINF_SUCCESS;
2081 }
2082 }
2083 }
2084
2085#ifdef LOG_ENABLED
2086 if (!PATMIsPatchGCAddr(pVM, pCtx->eip))
2087 {
2088 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
2089 DBGFR3DisasInstrCurrentLog(pVCpu, "Privileged instr: ");
2090 }
2091#endif
2092
2093 /*
2094 * Instruction statistics and logging.
2095 */
2096 DISCPUSTATE Cpu;
2097 int rc;
2098
2099 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "PRIV: ");
2100 if (RT_SUCCESS(rc))
2101 {
2102#ifdef VBOX_WITH_STATISTICS
2103 PEMSTATS pStats = pVCpu->em.s.CTX_SUFF(pStats);
2104 switch (Cpu.pCurInstr->opcode)
2105 {
2106 case OP_INVLPG:
2107 STAM_COUNTER_INC(&pStats->StatInvlpg);
2108 break;
2109 case OP_IRET:
2110 STAM_COUNTER_INC(&pStats->StatIret);
2111 break;
2112 case OP_CLI:
2113 STAM_COUNTER_INC(&pStats->StatCli);
2114 emR3RecordCli(pVM, pVCpu, pCtx->rip);
2115 break;
2116 case OP_STI:
2117 STAM_COUNTER_INC(&pStats->StatSti);
2118 break;
2119 case OP_INSB:
2120 case OP_INSWD:
2121 case OP_IN:
2122 case OP_OUTSB:
2123 case OP_OUTSWD:
2124 case OP_OUT:
2125 AssertMsgFailed(("Unexpected privileged exception due to port IO\n"));
2126 break;
2127
2128 case OP_MOV_CR:
2129 if (Cpu.param1.flags & USE_REG_GEN32)
2130 {
2131 //read
2132 Assert(Cpu.param2.flags & USE_REG_CR);
2133 Assert(Cpu.param2.base.reg_ctrl <= USE_REG_CR4);
2134 STAM_COUNTER_INC(&pStats->StatMovReadCR[Cpu.param2.base.reg_ctrl]);
2135 }
2136 else
2137 {
2138 //write
2139 Assert(Cpu.param1.flags & USE_REG_CR);
2140 Assert(Cpu.param1.base.reg_ctrl <= USE_REG_CR4);
2141 STAM_COUNTER_INC(&pStats->StatMovWriteCR[Cpu.param1.base.reg_ctrl]);
2142 }
2143 break;
2144
2145 case OP_MOV_DR:
2146 STAM_COUNTER_INC(&pStats->StatMovDRx);
2147 break;
2148 case OP_LLDT:
2149 STAM_COUNTER_INC(&pStats->StatMovLldt);
2150 break;
2151 case OP_LIDT:
2152 STAM_COUNTER_INC(&pStats->StatMovLidt);
2153 break;
2154 case OP_LGDT:
2155 STAM_COUNTER_INC(&pStats->StatMovLgdt);
2156 break;
2157 case OP_SYSENTER:
2158 STAM_COUNTER_INC(&pStats->StatSysEnter);
2159 break;
2160 case OP_SYSEXIT:
2161 STAM_COUNTER_INC(&pStats->StatSysExit);
2162 break;
2163 case OP_SYSCALL:
2164 STAM_COUNTER_INC(&pStats->StatSysCall);
2165 break;
2166 case OP_SYSRET:
2167 STAM_COUNTER_INC(&pStats->StatSysRet);
2168 break;
2169 case OP_HLT:
2170 STAM_COUNTER_INC(&pStats->StatHlt);
2171 break;
2172 default:
2173 STAM_COUNTER_INC(&pStats->StatMisc);
2174 Log4(("emR3RawPrivileged: opcode=%d\n", Cpu.pCurInstr->opcode));
2175 break;
2176 }
2177#endif /* VBOX_WITH_STATISTICS */
2178 if ( (pCtx->ss & X86_SEL_RPL) == 0
2179 && !pCtx->eflags.Bits.u1VM
2180 && SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT)
2181 {
2182 uint32_t size;
2183
2184 STAM_PROFILE_START(&pVCpu->em.s.StatPrivEmu, a);
2185 switch (Cpu.pCurInstr->opcode)
2186 {
2187 case OP_CLI:
2188 pCtx->eflags.u32 &= ~X86_EFL_IF;
2189 Assert(Cpu.opsize == 1);
2190 pCtx->rip += Cpu.opsize;
2191 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
2192 return VINF_EM_RESCHEDULE_REM; /* must go to the recompiler now! */
2193
2194 case OP_STI:
2195 pCtx->eflags.u32 |= X86_EFL_IF;
2196 EMSetInhibitInterruptsPC(pVCpu, pCtx->rip + Cpu.opsize);
2197 Assert(Cpu.opsize == 1);
2198 pCtx->rip += Cpu.opsize;
2199 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
2200 return VINF_SUCCESS;
2201
2202 case OP_HLT:
2203 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pCtx->eip))
2204 {
2205 PATMTRANSSTATE enmState;
2206 RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->eip, &enmState);
2207
2208 if (enmState == PATMTRANS_OVERWRITTEN)
2209 {
2210 rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
2211 Assert(rc == VERR_PATCH_DISABLED);
2212 /* Conflict detected, patch disabled */
2213 Log(("emR3RawPrivileged: detected conflict -> disabled patch at %08RX32\n", pCtx->eip));
2214
2215 enmState = PATMTRANS_SAFE;
2216 }
2217
2218 /* The translation had better be successful. Otherwise we can't recover. */
2219 AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %08RX32\n", pCtx->eip));
2220 if (enmState != PATMTRANS_OVERWRITTEN)
2221 pCtx->eip = pOrgInstrGC;
2222 }
2223 /* no break; we could just return VINF_EM_HALT here */
2224
2225 case OP_MOV_CR:
2226 case OP_MOV_DR:
2227#ifdef LOG_ENABLED
2228 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
2229 {
2230 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
2231 DBGFR3DisasInstrCurrentLog(pVCpu, "Privileged instr: ");
2232 }
2233#endif
2234
2235 rc = EMInterpretInstructionCPU(pVM, pVCpu, &Cpu, CPUMCTX2CORE(pCtx), 0, &size);
2236 if (RT_SUCCESS(rc))
2237 {
2238 pCtx->rip += Cpu.opsize;
2239#ifdef EM_NOTIFY_HWACCM
2240 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HWACC)
2241 HWACCMR3NotifyEmulated(pVCpu);
2242#endif
2243 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
2244
2245 if ( Cpu.pCurInstr->opcode == OP_MOV_CR
2246 && Cpu.param1.flags == USE_REG_CR /* write */
2247 )
2248 {
2249 /* Deal with CR0 updates inside patch code that force
2250 * us to go to the recompiler.
2251 */
2252 if ( PATMIsPatchGCAddr(pVM, pCtx->rip)
2253 && (pCtx->cr0 & (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE)) != (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE))
2254 {
2255 PATMTRANSSTATE enmState;
2256 RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->rip, &enmState);
2257
2258 Log(("Force recompiler switch due to cr0 (%RGp) update rip=%RGv -> %RGv (enmState=%d)\n", pCtx->cr0, pCtx->rip, pOrgInstrGC, enmState));
2259 if (enmState == PATMTRANS_OVERWRITTEN)
2260 {
2261 rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
2262 Assert(rc == VERR_PATCH_DISABLED);
2263 /* Conflict detected, patch disabled */
2264 Log(("emR3RawPrivileged: detected conflict -> disabled patch at %RGv\n", (RTGCPTR)pCtx->rip));
2265 enmState = PATMTRANS_SAFE;
2266 }
2267 /* The translation had better be successful. Otherwise we can't recover. */
2268 AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %RGv\n", (RTGCPTR)pCtx->rip));
2269 if (enmState != PATMTRANS_OVERWRITTEN)
2270 pCtx->rip = pOrgInstrGC;
2271 }
2272
2273 /* Reschedule is necessary as the execution/paging mode might have changed. */
2274 return VINF_EM_RESCHEDULE;
2275 }
2276 return rc; /* can return VINF_EM_HALT as well. */
2277 }
2278 AssertMsgReturn(rc == VERR_EM_INTERPRETER, ("%Rrc\n", rc), rc);
2279 break; /* fall back to the recompiler */
2280 }
2281 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
2282 }
2283 }
2284
2285 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
2286 return emR3PatchTrap(pVM, pVCpu, pCtx, VINF_PATM_PATCH_TRAP_GP);
2287
2288 return emR3RawExecuteInstruction(pVM, pVCpu, "PRIV");
2289}
2290
2291
2292/**
2293 * Update the forced rawmode execution modifier.
2294 *
2295 * This function is called when we're returning from the raw-mode loop(s). If we're
2296 * in patch code, it will set a flag forcing execution to be resumed in raw-mode,
2297 * if not in patch code, the flag will be cleared.
2298 *
2299 * We should never interrupt patch code while it's being executed. Cli patches can
2300 * contain big code blocks, but they are always executed with IF=0. Other patches
2301 * replace single instructions and should be atomic.
2302 *
2303 * @returns Updated rc.
2304 *
2305 * @param pVM The VM handle.
2306 * @param pVCpu The VMCPU handle.
2307 * @param pCtx The guest CPU context.
2308 * @param rc The result code.
2309 */
2310DECLINLINE(int) emR3RawUpdateForceFlag(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc)
2311{
2312 if (PATMIsPatchGCAddr(pVM, pCtx->eip)) /** @todo check cs selector base/type */
2313 {
2314 /* ignore reschedule attempts. */
2315 switch (rc)
2316 {
2317 case VINF_EM_RESCHEDULE:
2318 case VINF_EM_RESCHEDULE_REM:
2319 LogFlow(("emR3RawUpdateForceFlag: patch address -> force raw reschedule\n"));
2320 rc = VINF_SUCCESS;
2321 break;
2322 }
2323 pVCpu->em.s.fForceRAW = true;
2324 }
2325 else
2326 pVCpu->em.s.fForceRAW = false;
2327 return rc;
2328}
2329
2330
2331/**
2332 * Process a subset of the raw-mode return code.
2333 *
2334 * Since we have to share this with raw-mode single stepping, this inline
2335 * function has been created to avoid code duplication.
2336 *
2337 * @returns VINF_SUCCESS if it's ok to continue raw mode.
2338 * @returns VBox status code to return to the EM main loop.
2339 *
2340 * @param pVM The VM handle
2341 * @param pVCpu The VMCPU handle
2342 * @param rc The return code.
2343 * @param pCtx The guest cpu context.
2344 */
2345DECLINLINE(int) emR3RawHandleRC(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc)
2346{
2347 switch (rc)
2348 {
2349 /*
2350 * Common & simple ones.
2351 */
2352 case VINF_SUCCESS:
2353 break;
2354 case VINF_EM_RESCHEDULE_RAW:
2355 case VINF_EM_RESCHEDULE_HWACC:
2356 case VINF_EM_RAW_INTERRUPT:
2357 case VINF_EM_RAW_TO_R3:
2358 case VINF_EM_RAW_TIMER_PENDING:
2359 case VINF_EM_PENDING_REQUEST:
2360 rc = VINF_SUCCESS;
2361 break;
2362
2363 /*
2364 * Privileged instruction.
2365 */
2366 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
2367 case VINF_PATM_PATCH_TRAP_GP:
2368 rc = emR3RawPrivileged(pVM, pVCpu);
2369 break;
2370
2371 /*
2372 * Got a trap which needs dispatching.
2373 */
2374 case VINF_EM_RAW_GUEST_TRAP:
2375 if (PATMR3IsInsidePatchJump(pVM, pCtx->eip, NULL))
2376 {
2377 AssertReleaseMsgFailed(("FATAL ERROR: executing random instruction inside generated patch jump %08X\n", CPUMGetGuestEIP(pVCpu)));
2378 rc = VERR_EM_RAW_PATCH_CONFLICT;
2379 break;
2380 }
2381 rc = emR3RawGuestTrap(pVM, pVCpu);
2382 break;
2383
2384 /*
2385 * Trap in patch code.
2386 */
2387 case VINF_PATM_PATCH_TRAP_PF:
2388 case VINF_PATM_PATCH_INT3:
2389 rc = emR3PatchTrap(pVM, pVCpu, pCtx, rc);
2390 break;
2391
2392 case VINF_PATM_DUPLICATE_FUNCTION:
2393 Assert(PATMIsPatchGCAddr(pVM, (RTGCPTR)pCtx->eip));
2394 rc = PATMR3DuplicateFunctionRequest(pVM, pCtx);
2395 AssertRC(rc);
2396 rc = VINF_SUCCESS;
2397 break;
2398
2399 case VINF_PATM_CHECK_PATCH_PAGE:
2400 rc = PATMR3HandleMonitoredPage(pVM);
2401 AssertRC(rc);
2402 rc = VINF_SUCCESS;
2403 break;
2404
2405 /*
2406 * Patch manager.
2407 */
2408 case VERR_EM_RAW_PATCH_CONFLICT:
2409 AssertReleaseMsgFailed(("%Rrc handling is not yet implemented\n", rc));
2410 break;
2411
2412#ifdef VBOX_WITH_VMI
2413 /*
2414 * PARAV function.
2415 */
2416 case VINF_EM_RESCHEDULE_PARAV:
2417 rc = PARAVCallFunction(pVM);
2418 break;
2419#endif
2420
2421 /*
2422 * Memory mapped I/O access - attempt to patch the instruction
2423 */
2424 case VINF_PATM_HC_MMIO_PATCH_READ:
2425 rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
2426 PATMFL_MMIO_ACCESS | ((SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT) ? PATMFL_CODE32 : 0));
2427 if (RT_FAILURE(rc))
2428 rc = emR3RawExecuteInstruction(pVM, pVCpu, "MMIO");
2429 break;
2430
2431 case VINF_PATM_HC_MMIO_PATCH_WRITE:
2432 AssertFailed(); /* not yet implemented. */
2433 rc = emR3RawExecuteInstruction(pVM, pVCpu, "MMIO");
2434 break;
2435
2436 /*
2437 * Conflict or out of page tables.
2438 *
2439 * VM_FF_PGM_SYNC_CR3 is set by the hypervisor and all we need to
2440 * do here is to execute the pending forced actions.
2441 */
2442 case VINF_PGM_SYNC_CR3:
2443 AssertMsg(VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL),
2444 ("VINF_PGM_SYNC_CR3 and no VMCPU_FF_PGM_SYNC_CR3*!\n"));
2445 rc = VINF_SUCCESS;
2446 break;
2447
2448 /*
2449 * Paging mode change.
2450 */
2451 case VINF_PGM_CHANGE_MODE:
2452 rc = PGMChangeMode(pVCpu, pCtx->cr0, pCtx->cr4, pCtx->msrEFER);
2453 if (rc == VINF_SUCCESS)
2454 rc = VINF_EM_RESCHEDULE;
2455 AssertMsg(RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST), ("%Rrc\n", rc));
2456 break;
2457
2458 /*
2459 * CSAM wants to perform a task in ring-3. It has set an FF action flag.
2460 */
2461 case VINF_CSAM_PENDING_ACTION:
2462 rc = VINF_SUCCESS;
2463 break;
2464
2465 /*
2466 * Invoked Interrupt gate - must directly (!) go to the recompiler.
2467 */
2468 case VINF_EM_RAW_INTERRUPT_PENDING:
2469 case VINF_EM_RAW_RING_SWITCH_INT:
2470 Assert(TRPMHasTrap(pVCpu));
2471 Assert(!PATMIsPatchGCAddr(pVM, (RTGCPTR)pCtx->eip));
2472
2473 if (TRPMHasTrap(pVCpu))
2474 {
2475 /* If the guest gate is marked unpatched, then we will check again if we can patch it. */
2476 uint8_t u8Interrupt = TRPMGetTrapNo(pVCpu);
2477 if (TRPMR3GetGuestTrapHandler(pVM, u8Interrupt) == TRPM_INVALID_HANDLER)
2478 {
2479 CSAMR3CheckGates(pVM, u8Interrupt, 1);
2480 Log(("emR3RawHandleRC: recheck gate %x -> valid=%d\n", u8Interrupt, TRPMR3GetGuestTrapHandler(pVM, u8Interrupt) != TRPM_INVALID_HANDLER));
2481 /* Note: If it was successful, then we could go back to raw mode, but let's keep things simple for now. */
2482 }
2483 }
2484 rc = VINF_EM_RESCHEDULE_REM;
2485 break;
2486
2487 /*
2488 * Other ring switch types.
2489 */
2490 case VINF_EM_RAW_RING_SWITCH:
2491 rc = emR3RawRingSwitch(pVM, pVCpu);
2492 break;
2493
2494 /*
2495 * REMGCNotifyInvalidatePage() failed because of overflow.
2496 */
2497 case VERR_REM_FLUSHED_PAGES_OVERFLOW:
2498 Assert((pCtx->ss & X86_SEL_RPL) != 1);
2499 emR3RemLock(pVM);
2500 REMR3ReplayInvalidatedPages(pVM, pVCpu);
2501 emR3RemUnlock(pVM);
2502 rc = VINF_SUCCESS;
2503 break;
2504
2505 /*
2506 * I/O Port access - emulate the instruction.
2507 */
2508 case VINF_IOM_HC_IOPORT_READ:
2509 case VINF_IOM_HC_IOPORT_WRITE:
2510 rc = emR3RawExecuteIOInstruction(pVM, pVCpu);
2511 break;
2512
2513 /*
2514 * Memory mapped I/O access - emulate the instruction.
2515 */
2516 case VINF_IOM_HC_MMIO_READ:
2517 case VINF_IOM_HC_MMIO_WRITE:
2518 case VINF_IOM_HC_MMIO_READ_WRITE:
2519 rc = emR3RawExecuteInstruction(pVM, pVCpu, "MMIO");
2520 break;
2521
2522 /*
2523 * (MM)IO intensive code block detected; fall back to the recompiler for better performance
2524 */
2525 case VINF_EM_RAW_EMULATE_IO_BLOCK:
2526 rc = HWACCMR3EmulateIoBlock(pVM, pCtx);
2527 break;
2528
2529 /*
2530 * Execute instruction.
2531 */
2532 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
2533 rc = emR3RawExecuteInstruction(pVM, pVCpu, "LDT FAULT: ");
2534 break;
2535 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
2536 rc = emR3RawExecuteInstruction(pVM, pVCpu, "GDT FAULT: ");
2537 break;
2538 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
2539 rc = emR3RawExecuteInstruction(pVM, pVCpu, "IDT FAULT: ");
2540 break;
2541 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
2542 rc = emR3RawExecuteInstruction(pVM, pVCpu, "TSS FAULT: ");
2543 break;
2544 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
2545 rc = emR3RawExecuteInstruction(pVM, pVCpu, "PD FAULT: ");
2546 break;
2547
2548 case VINF_EM_RAW_EMULATE_INSTR_HLT:
2549 /** @todo skip instruction and go directly to the halt state. (see REM for implementation details) */
2550 rc = emR3RawPrivileged(pVM, pVCpu);
2551 break;
2552
2553 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
2554 rc = emR3RawExecuteInstruction(pVM, pVCpu, "EMUL: ", VINF_PATM_PENDING_IRQ_AFTER_IRET);
2555 break;
2556
2557 case VINF_EM_RAW_EMULATE_INSTR:
2558 case VINF_PATCH_EMULATE_INSTR:
2559 rc = emR3RawExecuteInstruction(pVM, pVCpu, "EMUL: ");
2560 break;
2561
2562 /*
2563 * Stale selector and iret traps => REM.
2564 */
2565 case VINF_EM_RAW_STALE_SELECTOR:
2566 case VINF_EM_RAW_IRET_TRAP:
2567 /* We will not go to the recompiler if EIP points to patch code. */
2568 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
2569 {
2570 pCtx->eip = PATMR3PatchToGCPtr(pVM, (RTGCPTR)pCtx->eip, 0);
2571 }
2572 LogFlow(("emR3RawHandleRC: %Rrc -> %Rrc\n", rc, VINF_EM_RESCHEDULE_REM));
2573 rc = VINF_EM_RESCHEDULE_REM;
2574 break;
2575
2576 /*
2577 * Up a level.
2578 */
2579 case VINF_EM_TERMINATE:
2580 case VINF_EM_OFF:
2581 case VINF_EM_RESET:
2582 case VINF_EM_SUSPEND:
2583 case VINF_EM_HALT:
2584 case VINF_EM_RESUME:
2585 case VINF_EM_NO_MEMORY:
2586 case VINF_EM_RESCHEDULE:
2587 case VINF_EM_RESCHEDULE_REM:
2588 case VINF_EM_WAIT_SIPI:
2589 break;
2590
2591 /*
2592 * Up a level and invoke the debugger.
2593 */
2594 case VINF_EM_DBG_STEPPED:
2595 case VINF_EM_DBG_BREAKPOINT:
2596 case VINF_EM_DBG_STEP:
2597 case VINF_EM_DBG_HYPER_BREAKPOINT:
2598 case VINF_EM_DBG_HYPER_STEPPED:
2599 case VINF_EM_DBG_HYPER_ASSERTION:
2600 case VINF_EM_DBG_STOP:
2601 break;
2602
2603 /*
2604 * Up a level, dump and debug.
2605 */
2606 case VERR_TRPM_DONT_PANIC:
2607 case VERR_TRPM_PANIC:
2608 case VERR_VMM_RING0_ASSERTION:
2609 break;
2610
2611 /*
2612 * Up a level, after HwAccM have done some release logging.
2613 */
2614 case VERR_VMX_INVALID_VMCS_FIELD:
2615 case VERR_VMX_INVALID_VMCS_PTR:
2616 case VERR_VMX_INVALID_VMXON_PTR:
2617 case VERR_VMX_UNEXPECTED_INTERRUPTION_EXIT_CODE:
2618 case VERR_VMX_UNEXPECTED_EXCEPTION:
2619 case VERR_VMX_UNEXPECTED_EXIT_CODE:
2620 case VERR_VMX_INVALID_GUEST_STATE:
2621 case VERR_VMX_UNABLE_TO_START_VM:
2622 case VERR_VMX_UNABLE_TO_RESUME_VM:
2623 HWACCMR3CheckError(pVM, rc);
2624 break;
2625 /*
2626 * Anything which is not known to us means an internal error
2627 * and the termination of the VM!
2628 */
2629 default:
2630 AssertMsgFailed(("Unknown GC return code: %Rra\n", rc));
2631 break;
2632 }
2633 return rc;
2634}
2635
2636
2637/**
2638 * Check for pending raw actions
2639 *
2640 * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
2641 * EM statuses.
2642 * @param pVM The VM to operate on.
2643 * @param pVCpu The VMCPU handle.
2644 */
2645VMMR3DECL(int) EMR3CheckRawForcedActions(PVM pVM, PVMCPU pVCpu)
2646{
2647 return emR3RawForcedActions(pVM, pVCpu, pVCpu->em.s.pCtx);
2648}
2649
2650
2651/**
2652 * Process raw-mode specific forced actions.
2653 *
2654 * This function is called when any FFs in the VM_FF_HIGH_PRIORITY_PRE_RAW_MASK is pending.
2655 *
2656 * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
2657 * EM statuses.
2658 * @param pVM The VM handle.
2659 * @param pVCpu The VMCPU handle.
2660 * @param pCtx The guest CPUM register context.
2661 */
2662static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2663{
2664 /*
2665 * Note that the order is *vitally* important!
2666 * Also note that SELMR3UpdateFromCPUM may trigger VM_FF_SELM_SYNC_TSS.
2667 */
2668
2669
2670 /*
2671 * Sync selector tables.
2672 */
2673 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT))
2674 {
2675 int rc = SELMR3UpdateFromCPUM(pVM, pVCpu);
2676 if (RT_FAILURE(rc))
2677 return rc;
2678 }
2679
2680 /*
2681 * Sync IDT.
2682 *
2683 * The CSAMR3CheckGates call in TRPMR3SyncIDT may call PGMPrefetchPage
2684 * and PGMShwModifyPage, so we're in for trouble if for instance a
2685 * PGMSyncCR3+pgmPoolClearAll is pending.
2686 */
2687 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TRPM_SYNC_IDT))
2688 {
2689 if ( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
2690 && EMIsRawRing0Enabled(pVM)
2691 && CSAMIsEnabled(pVM))
2692 {
2693 int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
2694 if (RT_FAILURE(rc))
2695 return rc;
2696 }
2697
2698 int rc = TRPMR3SyncIDT(pVM, pVCpu);
2699 if (RT_FAILURE(rc))
2700 return rc;
2701 }
2702
2703 /*
2704 * Sync TSS.
2705 */
2706 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS))
2707 {
2708 int rc = SELMR3SyncTSS(pVM, pVCpu);
2709 if (RT_FAILURE(rc))
2710 return rc;
2711 }
2712
2713 /*
2714 * Sync page directory.
2715 */
2716 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
2717 {
2718 int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
2719 if (RT_FAILURE(rc))
2720 return rc;
2721
2722 Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
2723
2724 /* Prefetch pages for EIP and ESP. */
2725 /** @todo This is rather expensive. Should investigate if it really helps at all. */
2726 rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->rip));
2727 if (rc == VINF_SUCCESS)
2728 rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DIS_SELREG_SS, CPUMCTX2CORE(pCtx), pCtx->rsp));
2729 if (rc != VINF_SUCCESS)
2730 {
2731 if (rc != VINF_PGM_SYNC_CR3)
2732 {
2733 AssertLogRelMsgReturn(RT_FAILURE(rc), ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2734 return rc;
2735 }
2736 rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
2737 if (RT_FAILURE(rc))
2738 return rc;
2739 }
2740 /** @todo maybe prefetch the supervisor stack page as well */
2741 Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
2742 }
2743
2744 /*
2745 * Allocate handy pages (just in case the above actions have consumed some pages).
2746 */
2747 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
2748 {
2749 int rc = PGMR3PhysAllocateHandyPages(pVM);
2750 if (RT_FAILURE(rc))
2751 return rc;
2752 }
2753
2754 /*
2755 * Check whether we're out of memory now.
2756 *
2757 * This may stem from some of the above actions or operations that has been executed
2758 * since we ran FFs. The allocate handy pages must for instance always be followed by
2759 * this check.
2760 */
2761 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
2762 return VINF_EM_NO_MEMORY;
2763
2764 return VINF_SUCCESS;
2765}
2766
2767
2768/**
2769 * Executes raw code.
2770 *
2771 * This function contains the raw-mode version of the inner
2772 * execution loop (the outer loop being in EMR3ExecuteVM()).
2773 *
2774 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
2775 * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
2776 *
2777 * @param pVM VM handle.
2778 * @param pVCpu VMCPU handle.
2779 * @param pfFFDone Where to store an indicator telling whether or not
2780 * FFs were done before returning.
2781 */
2782static int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
2783{
2784 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTotal, a);
2785
2786 int rc = VERR_INTERNAL_ERROR;
2787 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
2788 LogFlow(("emR3RawExecute: (cs:eip=%04x:%08x)\n", pCtx->cs, pCtx->eip));
2789 pVCpu->em.s.fForceRAW = false;
2790 *pfFFDone = false;
2791
2792
2793 /*
2794 *
2795 * Spin till we get a forced action or raw mode status code resulting in
2796 * in anything but VINF_SUCCESS or VINF_EM_RESCHEDULE_RAW.
2797 *
2798 */
2799 for (;;)
2800 {
2801 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWEntry, b);
2802
2803 /*
2804 * Check various preconditions.
2805 */
2806#ifdef VBOX_STRICT
2807 Assert(REMR3QueryPendingInterrupt(pVM, pVCpu) == REM_NO_PENDING_IRQ);
2808 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) == 3 || (pCtx->ss & X86_SEL_RPL) == 0);
2809 AssertMsg( (pCtx->eflags.u32 & X86_EFL_IF)
2810 || PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip),
2811 ("Tried to execute code with IF at EIP=%08x!\n", pCtx->eip));
2812 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
2813 && PGMMapHasConflicts(pVM))
2814 {
2815 PGMMapCheck(pVM);
2816 AssertMsgFailed(("We should not get conflicts any longer!!!\n"));
2817 return VERR_INTERNAL_ERROR;
2818 }
2819#endif /* VBOX_STRICT */
2820
2821 /*
2822 * Process high priority pre-execution raw-mode FFs.
2823 */
2824 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
2825 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2826 {
2827 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
2828 if (rc != VINF_SUCCESS)
2829 break;
2830 }
2831
2832 /*
2833 * If we're going to execute ring-0 code, the guest state needs to
2834 * be modified a bit and some of the state components (IF, SS/CS RPL,
2835 * and perhaps EIP) needs to be stored with PATM.
2836 */
2837 rc = CPUMRawEnter(pVCpu, NULL);
2838 if (rc != VINF_SUCCESS)
2839 {
2840 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
2841 break;
2842 }
2843
2844 /*
2845 * Scan code before executing it. Don't bother with user mode or V86 code
2846 */
2847 if ( (pCtx->ss & X86_SEL_RPL) <= 1
2848 && !pCtx->eflags.Bits.u1VM
2849 && !PATMIsPatchGCAddr(pVM, pCtx->eip))
2850 {
2851 STAM_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWEntry, b);
2852 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
2853 STAM_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWEntry, b);
2854 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
2855 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2856 {
2857 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
2858 if (rc != VINF_SUCCESS)
2859 {
2860 rc = CPUMRawLeave(pVCpu, NULL, rc);
2861 break;
2862 }
2863 }
2864 }
2865
2866#ifdef LOG_ENABLED
2867 /*
2868 * Log important stuff before entering GC.
2869 */
2870 PPATMGCSTATE pGCState = PATMR3QueryGCStateHC(pVM);
2871 if (pCtx->eflags.Bits.u1VM)
2872 Log(("RV86: %04X:%08X IF=%d VMFlags=%x\n", pCtx->cs, pCtx->eip, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
2873 else if ((pCtx->ss & X86_SEL_RPL) == 1)
2874 {
2875 bool fCSAMScanned = CSAMIsPageScanned(pVM, (RTGCPTR)pCtx->eip);
2876 Log(("RR0: %08X ESP=%08X IF=%d VMFlags=%x PIF=%d CPL=%d (Scanned=%d)\n", pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags, pGCState->fPIF, (pCtx->ss & X86_SEL_RPL), fCSAMScanned));
2877 }
2878 else if ((pCtx->ss & X86_SEL_RPL) == 3)
2879 Log(("RR3: %08X ESP=%08X IF=%d VMFlags=%x\n", pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
2880#endif /* LOG_ENABLED */
2881
2882
2883
2884 /*
2885 * Execute the code.
2886 */
2887 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
2888 STAM_PROFILE_START(&pVCpu->em.s.StatRAWExec, c);
2889 rc = VMMR3RawRunGC(pVM, pVCpu);
2890 STAM_PROFILE_STOP(&pVCpu->em.s.StatRAWExec, c);
2891 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTail, d);
2892
2893 LogFlow(("RR0-E: %08X ESP=%08X IF=%d VMFlags=%x PIF=%d CPL=%d\n", pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags, pGCState->fPIF, (pCtx->ss & X86_SEL_RPL)));
2894 LogFlow(("VMMR3RawRunGC returned %Rrc\n", rc));
2895
2896
2897
2898 /*
2899 * Restore the real CPU state and deal with high priority post
2900 * execution FFs before doing anything else.
2901 */
2902 rc = CPUMRawLeave(pVCpu, NULL, rc);
2903 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
2904 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
2905 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
2906 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
2907
2908#ifdef VBOX_STRICT
2909 /*
2910 * Assert TSS consistency & rc vs patch code.
2911 */
2912 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_SELM_SYNC_GDT) /* GDT implies TSS at the moment. */
2913 && EMIsRawRing0Enabled(pVM))
2914 SELMR3CheckTSS(pVM);
2915 switch (rc)
2916 {
2917 case VINF_SUCCESS:
2918 case VINF_EM_RAW_INTERRUPT:
2919 case VINF_PATM_PATCH_TRAP_PF:
2920 case VINF_PATM_PATCH_TRAP_GP:
2921 case VINF_PATM_PATCH_INT3:
2922 case VINF_PATM_CHECK_PATCH_PAGE:
2923 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
2924 case VINF_EM_RAW_GUEST_TRAP:
2925 case VINF_EM_RESCHEDULE_RAW:
2926 break;
2927
2928 default:
2929 if (PATMIsPatchGCAddr(pVM, pCtx->eip) && !(pCtx->eflags.u32 & X86_EFL_TF))
2930 LogIt(NULL, 0, LOG_GROUP_PATM, ("Patch code interrupted at %RRv for reason %Rrc\n", (RTRCPTR)CPUMGetGuestEIP(pVCpu), rc));
2931 break;
2932 }
2933 /*
2934 * Let's go paranoid!
2935 */
2936 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
2937 && PGMMapHasConflicts(pVM))
2938 {
2939 PGMMapCheck(pVM);
2940 AssertMsgFailed(("We should not get conflicts any longer!!! rc=%Rrc\n", rc));
2941 return VERR_INTERNAL_ERROR;
2942 }
2943#endif /* VBOX_STRICT */
2944
2945 /*
2946 * Process the returned status code.
2947 */
2948 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
2949 {
2950 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
2951 break;
2952 }
2953 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
2954 if (rc != VINF_SUCCESS)
2955 {
2956 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
2957 if (rc != VINF_SUCCESS)
2958 {
2959 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
2960 break;
2961 }
2962 }
2963
2964 /*
2965 * Check and execute forced actions.
2966 */
2967#ifdef VBOX_HIGH_RES_TIMERS_HACK
2968 TMTimerPollVoid(pVM, pVCpu);
2969#endif
2970 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
2971 if ( VM_FF_ISPENDING(pVM, ~VM_FF_HIGH_PRIORITY_PRE_RAW_MASK | VM_FF_PGM_NO_MEMORY)
2972 || VMCPU_FF_ISPENDING(pVCpu, ~VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2973 {
2974 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) != 1);
2975
2976 STAM_REL_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWTotal, a);
2977 rc = emR3ForcedActions(pVM, pVCpu, rc);
2978 STAM_REL_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWTotal, a);
2979 if ( rc != VINF_SUCCESS
2980 && rc != VINF_EM_RESCHEDULE_RAW)
2981 {
2982 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
2983 if (rc != VINF_SUCCESS)
2984 {
2985 *pfFFDone = true;
2986 break;
2987 }
2988 }
2989 }
2990 }
2991
2992 /*
2993 * Return to outer loop.
2994 */
2995#if defined(LOG_ENABLED) && defined(DEBUG)
2996 RTLogFlush(NULL);
2997#endif
2998 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTotal, a);
2999 return rc;
3000}
3001
3002
3003/**
3004 * Executes hardware accelerated raw code. (Intel VMX & AMD SVM)
3005 *
3006 * This function contains the raw-mode version of the inner
3007 * execution loop (the outer loop being in EMR3ExecuteVM()).
3008 *
3009 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE, VINF_EM_RESCHEDULE_RAW,
3010 * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
3011 *
3012 * @param pVM VM handle.
3013 * @param pVCpu VMCPU handle.
3014 * @param pfFFDone Where to store an indicator telling whether or not
3015 * FFs were done before returning.
3016 */
3017static int emR3HwAccExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
3018{
3019 int rc = VERR_INTERNAL_ERROR;
3020 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
3021
3022 LogFlow(("emR3HwAccExecute%d: (cs:eip=%04x:%RGv)\n", pVCpu->idCpu, pCtx->cs, (RTGCPTR)pCtx->rip));
3023 *pfFFDone = false;
3024
3025 STAM_COUNTER_INC(&pVCpu->em.s.StatHwAccExecuteEntry);
3026
3027#ifdef EM_NOTIFY_HWACCM
3028 HWACCMR3NotifyScheduled(pVCpu);
3029#endif
3030
3031 /*
3032 * Spin till we get a forced action which returns anything but VINF_SUCCESS.
3033 */
3034 for (;;)
3035 {
3036 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatHwAccEntry, a);
3037
3038 /*
3039 * Process high priority pre-execution raw-mode FFs.
3040 */
3041 VMCPU_FF_CLEAR(pVCpu, (VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_TSS)); /* not relevant in HWACCM mode; shouldn't be set really. */
3042 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
3043 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
3044 {
3045 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
3046 if (rc != VINF_SUCCESS)
3047 break;
3048 }
3049
3050#ifdef LOG_ENABLED
3051 /*
3052 * Log important stuff before entering GC.
3053 */
3054 if (TRPMHasTrap(pVCpu))
3055 Log(("CPU%d: Pending hardware interrupt=0x%x cs:rip=%04X:%RGv\n", pVCpu->idCpu, TRPMGetTrapNo(pVCpu), pCtx->cs, (RTGCPTR)pCtx->rip));
3056
3057 uint32_t cpl = CPUMGetGuestCPL(pVCpu, CPUMCTX2CORE(pCtx));
3058
3059 if (pVM->cCPUs == 1)
3060 {
3061 if (pCtx->eflags.Bits.u1VM)
3062 Log(("HWV86: %08X IF=%d\n", pCtx->eip, pCtx->eflags.Bits.u1IF));
3063 else if (CPUMIsGuestIn64BitCodeEx(pCtx))
3064 Log(("HWR%d: %04X:%RGv ESP=%RGv IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs, (RTGCPTR)pCtx->rip, pCtx->rsp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
3065 else
3066 Log(("HWR%d: %04X:%08X ESP=%08X IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
3067 }
3068 else
3069 {
3070 if (pCtx->eflags.Bits.u1VM)
3071 Log(("HWV86-CPU%d: %08X IF=%d\n", pVCpu->idCpu, pCtx->eip, pCtx->eflags.Bits.u1IF));
3072 else if (CPUMIsGuestIn64BitCodeEx(pCtx))
3073 Log(("HWR%d-CPU%d: %04X:%RGv ESP=%RGv IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pVCpu->idCpu, pCtx->cs, (RTGCPTR)pCtx->rip, pCtx->rsp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
3074 else
3075 Log(("HWR%d-CPU%d: %04X:%08X ESP=%08X IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pVCpu->idCpu, pCtx->cs, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
3076 }
3077#endif /* LOG_ENABLED */
3078
3079 /*
3080 * Execute the code.
3081 */
3082 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatHwAccEntry, a);
3083 STAM_PROFILE_START(&pVCpu->em.s.StatHwAccExec, x);
3084 rc = VMMR3HwAccRunGC(pVM, pVCpu);
3085 STAM_PROFILE_STOP(&pVCpu->em.s.StatHwAccExec, x);
3086
3087 /*
3088 * Deal with high priority post execution FFs before doing anything else.
3089 */
3090 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
3091 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
3092 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
3093 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
3094
3095 /*
3096 * Process the returned status code.
3097 */
3098 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
3099 break;
3100
3101 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
3102 if (rc != VINF_SUCCESS)
3103 break;
3104
3105 /*
3106 * Check and execute forced actions.
3107 */
3108#ifdef VBOX_HIGH_RES_TIMERS_HACK
3109 TMTimerPollVoid(pVM, pVCpu);
3110#endif
3111 if ( VM_FF_ISPENDING(pVM, VM_FF_ALL_MASK)
3112 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_ALL_MASK))
3113 {
3114 rc = emR3ForcedActions(pVM, pVCpu, rc);
3115 if ( rc != VINF_SUCCESS
3116 && rc != VINF_EM_RESCHEDULE_HWACC)
3117 {
3118 *pfFFDone = true;
3119 break;
3120 }
3121 }
3122 }
3123
3124 /*
3125 * Return to outer loop.
3126 */
3127#if defined(LOG_ENABLED) && defined(DEBUG)
3128 RTLogFlush(NULL);
3129#endif
3130 return rc;
3131}
3132
3133
3134/**
3135 * Decides whether to execute RAW, HWACC or REM.
3136 *
3137 * @returns new EM state
3138 * @param pVM The VM.
3139 * @param pVCpu The VMCPU handle.
3140 * @param pCtx The CPU context.
3141 */
3142static EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
3143{
3144 /*
3145 * When forcing raw-mode execution, things are simple.
3146 */
3147 if (pVCpu->em.s.fForceRAW)
3148 return EMSTATE_RAW;
3149
3150 /*
3151 * We stay in the wait for SIPI state unless explicitly told otherwise.
3152 */
3153 if (pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI)
3154 return EMSTATE_WAIT_SIPI;
3155
3156 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
3157 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
3158 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
3159
3160 X86EFLAGS EFlags = pCtx->eflags;
3161 if (HWACCMIsEnabled(pVM))
3162 {
3163 /* Hardware accelerated raw-mode:
3164 *
3165 * Typically only 32-bits protected mode, with paging enabled, code is allowed here.
3166 */
3167 if (HWACCMR3CanExecuteGuest(pVM, pCtx) == true)
3168 return EMSTATE_HWACC;
3169
3170 /* Note: Raw mode and hw accelerated mode are incompatible. The latter turns
3171 * off monitoring features essential for raw mode! */
3172 return EMSTATE_REM;
3173 }
3174
3175 /*
3176 * Standard raw-mode:
3177 *
3178 * Here we only support 16 & 32 bits protected mode ring 3 code that has no IO privileges
3179 * or 32 bits protected mode ring 0 code
3180 *
3181 * The tests are ordered by the likelyhood of being true during normal execution.
3182 */
3183 if (EFlags.u32 & (X86_EFL_TF /* | HF_INHIBIT_IRQ_MASK*/))
3184 {
3185 Log2(("raw mode refused: EFlags=%#x\n", EFlags.u32));
3186 return EMSTATE_REM;
3187 }
3188
3189#ifndef VBOX_RAW_V86
3190 if (EFlags.u32 & X86_EFL_VM) {
3191 Log2(("raw mode refused: VM_MASK\n"));
3192 return EMSTATE_REM;
3193 }
3194#endif
3195
3196 /** @todo check up the X86_CR0_AM flag in respect to raw mode!!! We're probably not emulating it right! */
3197 uint32_t u32CR0 = pCtx->cr0;
3198 if ((u32CR0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
3199 {
3200 //Log2(("raw mode refused: %s%s%s\n", (u32CR0 & X86_CR0_PG) ? "" : " !PG", (u32CR0 & X86_CR0_PE) ? "" : " !PE", (u32CR0 & X86_CR0_AM) ? "" : " !AM"));
3201 return EMSTATE_REM;
3202 }
3203
3204 if (pCtx->cr4 & X86_CR4_PAE)
3205 {
3206 uint32_t u32Dummy, u32Features;
3207
3208 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32Dummy, &u32Features);
3209 if (!(u32Features & X86_CPUID_FEATURE_EDX_PAE))
3210 return EMSTATE_REM;
3211 }
3212
3213 unsigned uSS = pCtx->ss;
3214 if ( pCtx->eflags.Bits.u1VM
3215 || (uSS & X86_SEL_RPL) == 3)
3216 {
3217 if (!EMIsRawRing3Enabled(pVM))
3218 return EMSTATE_REM;
3219
3220 if (!(EFlags.u32 & X86_EFL_IF))
3221 {
3222 Log2(("raw mode refused: IF (RawR3)\n"));
3223 return EMSTATE_REM;
3224 }
3225
3226 if (!(u32CR0 & X86_CR0_WP) && EMIsRawRing0Enabled(pVM))
3227 {
3228 Log2(("raw mode refused: CR0.WP + RawR0\n"));
3229 return EMSTATE_REM;
3230 }
3231 }
3232 else
3233 {
3234 if (!EMIsRawRing0Enabled(pVM))
3235 return EMSTATE_REM;
3236
3237 /* Only ring 0 supervisor code. */
3238 if ((uSS & X86_SEL_RPL) != 0)
3239 {
3240 Log2(("raw r0 mode refused: CPL %d\n", uSS & X86_SEL_RPL));
3241 return EMSTATE_REM;
3242 }
3243
3244 // Let's start with pure 32 bits ring 0 code first
3245 /** @todo What's pure 32-bit mode? flat? */
3246 if ( !(pCtx->ssHid.Attr.n.u1DefBig)
3247 || !(pCtx->csHid.Attr.n.u1DefBig))
3248 {
3249 Log2(("raw r0 mode refused: SS/CS not 32bit\n"));
3250 return EMSTATE_REM;
3251 }
3252
3253 /* Write protection must be turned on, or else the guest can overwrite our hypervisor code and data. */
3254 if (!(u32CR0 & X86_CR0_WP))
3255 {
3256 Log2(("raw r0 mode refused: CR0.WP=0!\n"));
3257 return EMSTATE_REM;
3258 }
3259
3260 if (PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip))
3261 {
3262 Log2(("raw r0 mode forced: patch code\n"));
3263 return EMSTATE_RAW;
3264 }
3265
3266#if !defined(VBOX_ALLOW_IF0) && !defined(VBOX_RUN_INTERRUPT_GATE_HANDLERS)
3267 if (!(EFlags.u32 & X86_EFL_IF))
3268 {
3269 ////Log2(("R0: IF=0 VIF=%d %08X\n", eip, pVMeflags));
3270 //Log2(("RR0: Interrupts turned off; fall back to emulation\n"));
3271 return EMSTATE_REM;
3272 }
3273#endif
3274
3275 /** @todo still necessary??? */
3276 if (EFlags.Bits.u2IOPL != 0)
3277 {
3278 Log2(("raw r0 mode refused: IOPL %d\n", EFlags.Bits.u2IOPL));
3279 return EMSTATE_REM;
3280 }
3281 }
3282
3283 Assert(PGMPhysIsA20Enabled(pVCpu));
3284 return EMSTATE_RAW;
3285}
3286
3287
3288/**
3289 * Executes all high priority post execution force actions.
3290 *
3291 * @returns rc or a fatal status code.
3292 *
3293 * @param pVM VM handle.
3294 * @param pVCpu VMCPU handle.
3295 * @param rc The current rc.
3296 */
3297static int emR3HighPriorityPostForcedActions(PVM pVM, PVMCPU pVCpu, int rc)
3298{
3299 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
3300 PDMR3CritSectFF(pVCpu);
3301
3302 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_CSAM_PENDING_ACTION))
3303 CSAMR3DoPendingAction(pVM, pVCpu);
3304
3305 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
3306 {
3307 if ( rc > VINF_EM_NO_MEMORY
3308 && rc <= VINF_EM_LAST)
3309 rc = VINF_EM_NO_MEMORY;
3310 }
3311
3312 return rc;
3313}
3314
3315
3316/**
3317 * Executes all pending forced actions.
3318 *
3319 * Forced actions can cause execution delays and execution
3320 * rescheduling. The first we deal with using action priority, so
3321 * that for instance pending timers aren't scheduled and ran until
3322 * right before execution. The rescheduling we deal with using
3323 * return codes. The same goes for VM termination, only in that case
3324 * we exit everything.
3325 *
3326 * @returns VBox status code of equal or greater importance/severity than rc.
3327 * The most important ones are: VINF_EM_RESCHEDULE,
3328 * VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
3329 *
3330 * @param pVM VM handle.
3331 * @param pVCpu VMCPU handle.
3332 * @param rc The current rc.
3333 *
3334 */
3335static int emR3ForcedActions(PVM pVM, PVMCPU pVCpu, int rc)
3336{
3337 STAM_REL_PROFILE_START(&pVCpu->em.s.StatForcedActions, a);
3338#ifdef VBOX_STRICT
3339 int rcIrq = VINF_SUCCESS;
3340#endif
3341 int rc2;
3342#define UPDATE_RC() \
3343 do { \
3344 AssertMsg(rc2 <= 0 || (rc2 >= VINF_EM_FIRST && rc2 <= VINF_EM_LAST), ("Invalid FF return code: %Rra\n", rc2)); \
3345 if (rc2 == VINF_SUCCESS || rc < VINF_SUCCESS) \
3346 break; \
3347 if (!rc || rc2 < rc) \
3348 rc = rc2; \
3349 } while (0)
3350
3351 /*
3352 * Post execution chunk first.
3353 */
3354 if ( VM_FF_ISPENDING(pVM, VM_FF_NORMAL_PRIORITY_POST_MASK)
3355 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_NORMAL_PRIORITY_POST_MASK))
3356 {
3357 /*
3358 * Termination request.
3359 */
3360 if (VM_FF_ISPENDING(pVM, VM_FF_TERMINATE))
3361 {
3362 Log2(("emR3ForcedActions: returns VINF_EM_TERMINATE\n"));
3363 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
3364 return VINF_EM_TERMINATE;
3365 }
3366
3367 /*
3368 * Debugger Facility polling.
3369 */
3370 if (VM_FF_ISPENDING(pVM, VM_FF_DBGF))
3371 {
3372 rc2 = DBGFR3VMMForcedAction(pVM);
3373 UPDATE_RC();
3374 }
3375
3376 /*
3377 * Postponed reset request.
3378 */
3379 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_RESET_BIT))
3380 {
3381 rc2 = VMR3Reset(pVM);
3382 UPDATE_RC();
3383 }
3384
3385 /*
3386 * CSAM page scanning.
3387 */
3388 if ( !VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY)
3389 && VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_CSAM_SCAN_PAGE))
3390 {
3391 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
3392
3393 /** @todo: check for 16 or 32 bits code! (D bit in the code selector) */
3394 Log(("Forced action VMCPU_FF_CSAM_SCAN_PAGE\n"));
3395
3396 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
3397 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_CSAM_SCAN_PAGE);
3398 }
3399
3400 /*
3401 * Out of memory? Putting this after CSAM as it may in theory cause us to run out of memory.
3402 */
3403 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
3404 {
3405 rc2 = PGMR3PhysAllocateHandyPages(pVM);
3406 UPDATE_RC();
3407 if (rc == VINF_EM_NO_MEMORY)
3408 return rc;
3409 }
3410
3411 /* check that we got them all */
3412 AssertCompile(VM_FF_NORMAL_PRIORITY_POST_MASK == (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_PGM_NO_MEMORY));
3413 AssertCompile(VMCPU_FF_NORMAL_PRIORITY_POST_MASK == VMCPU_FF_CSAM_SCAN_PAGE);
3414 }
3415
3416 /*
3417 * Normal priority then.
3418 * (Executed in no particular order.)
3419 */
3420 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_NORMAL_PRIORITY_MASK, VM_FF_PGM_NO_MEMORY))
3421 {
3422 /*
3423 * PDM Queues are pending.
3424 */
3425 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PDM_QUEUES, VM_FF_PGM_NO_MEMORY))
3426 PDMR3QueueFlushAll(pVM);
3427
3428 /*
3429 * PDM DMA transfers are pending.
3430 */
3431 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PDM_DMA, VM_FF_PGM_NO_MEMORY))
3432 PDMR3DmaRun(pVM);
3433
3434 /*
3435 * Requests from other threads.
3436 */
3437 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_REQUEST, VM_FF_PGM_NO_MEMORY))
3438 {
3439 rc2 = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
3440 if (rc2 == VINF_EM_OFF || rc2 == VINF_EM_TERMINATE)
3441 {
3442 Log2(("emR3ForcedActions: returns %Rrc\n", rc2));
3443 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
3444 return rc2;
3445 }
3446 UPDATE_RC();
3447 }
3448
3449 /* Replay the handler notification changes. */
3450 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_REM_HANDLER_NOTIFY, VM_FF_PGM_NO_MEMORY))
3451 {
3452 emR3RemLock(pVM);
3453 REMR3ReplayHandlerNotifications(pVM);
3454 emR3RemUnlock(pVM);
3455 }
3456
3457 /* check that we got them all */
3458 AssertCompile(VM_FF_NORMAL_PRIORITY_MASK == (VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_REM_HANDLER_NOTIFY));
3459 }
3460
3461 /*
3462 * Normal priority then. (per-VCPU)
3463 * (Executed in no particular order.)
3464 */
3465 if ( !VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY)
3466 && VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_NORMAL_PRIORITY_MASK))
3467 {
3468 /*
3469 * Requests from other threads.
3470 */
3471 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
3472 {
3473 rc2 = VMR3ReqProcessU(pVM->pUVM, pVCpu->idCpu);
3474 if (rc2 == VINF_EM_OFF || rc2 == VINF_EM_TERMINATE)
3475 {
3476 Log2(("emR3ForcedActions: returns %Rrc\n", rc2));
3477 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
3478 return rc2;
3479 }
3480 UPDATE_RC();
3481 }
3482
3483 /* check that we got them all */
3484 Assert(!(VMCPU_FF_NORMAL_PRIORITY_MASK & ~(VMCPU_FF_REQUEST)));
3485 }
3486
3487 /*
3488 * High priority pre execution chunk last.
3489 * (Executed in ascending priority order.)
3490 */
3491 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_MASK)
3492 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_MASK))
3493 {
3494 /*
3495 * Timers before interrupts.
3496 */
3497 if ( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER)
3498 && !VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
3499 TMR3TimerQueuesDo(pVM);
3500
3501 /*
3502 * The instruction following an emulated STI should *always* be executed!
3503 */
3504 if ( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
3505 && !VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
3506 {
3507 Log(("VM_FF_EMULATED_STI at %RGv successor %RGv\n", (RTGCPTR)CPUMGetGuestRIP(pVCpu), EMGetInhibitInterruptsPC(pVCpu)));
3508 if (CPUMGetGuestEIP(pVCpu) != EMGetInhibitInterruptsPC(pVCpu))
3509 {
3510 /* Note: we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here if the eip is the same as the inhibited instr address.
3511 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
3512 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
3513 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
3514 */
3515 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
3516 }
3517 if (HWACCMR3IsActive(pVCpu))
3518 rc2 = VINF_EM_RESCHEDULE_HWACC;
3519 else
3520 rc2 = PATMAreInterruptsEnabled(pVM) ? VINF_EM_RESCHEDULE_RAW : VINF_EM_RESCHEDULE_REM;
3521
3522 UPDATE_RC();
3523 }
3524
3525 /*
3526 * Interrupts.
3527 */
3528 if ( !VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY)
3529 && !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
3530 && (!rc || rc >= VINF_EM_RESCHEDULE_HWACC)
3531 && !TRPMHasTrap(pVCpu) /* an interrupt could already be scheduled for dispatching in the recompiler. */
3532 && PATMAreInterruptsEnabled(pVM)
3533 && !HWACCMR3IsEventPending(pVM))
3534 {
3535 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
3536 {
3537 /* Note: it's important to make sure the return code from TRPMR3InjectEvent isn't ignored! */
3538 /** @todo this really isn't nice, should properly handle this */
3539 rc2 = TRPMR3InjectEvent(pVM, pVCpu, TRPM_HARDWARE_INT);
3540#ifdef VBOX_STRICT
3541 rcIrq = rc2;
3542#endif
3543 UPDATE_RC();
3544 }
3545 /** @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. */
3546 else if (REMR3QueryPendingInterrupt(pVM, pVCpu) != REM_NO_PENDING_IRQ)
3547 {
3548 rc2 = VINF_EM_RESCHEDULE_REM;
3549 UPDATE_RC();
3550 }
3551 }
3552
3553 /*
3554 * Allocate handy pages.
3555 */
3556 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
3557 {
3558 rc2 = PGMR3PhysAllocateHandyPages(pVM);
3559 UPDATE_RC();
3560 }
3561
3562 /*
3563 * Debugger Facility request.
3564 */
3565 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_DBGF, VM_FF_PGM_NO_MEMORY))
3566 {
3567 rc2 = DBGFR3VMMForcedAction(pVM);
3568 UPDATE_RC();
3569 }
3570
3571 /*
3572 * Termination request.
3573 */
3574 if (VM_FF_ISPENDING(pVM, VM_FF_TERMINATE))
3575 {
3576 Log2(("emR3ForcedActions: returns VINF_EM_TERMINATE\n"));
3577 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
3578 return VINF_EM_TERMINATE;
3579 }
3580
3581 /*
3582 * Out of memory? Since most of our fellow high priority actions may cause us
3583 * to run out of memory, we're employing VM_FF_IS_PENDING_EXCEPT and putting this
3584 * at the end rather than the start. Also, VM_FF_TERMINATE has higher priority
3585 * than us since we can terminate without allocating more memory.
3586 */
3587 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
3588 {
3589 rc2 = PGMR3PhysAllocateHandyPages(pVM);
3590 UPDATE_RC();
3591 if (rc == VINF_EM_NO_MEMORY)
3592 return rc;
3593 }
3594
3595 /*
3596 * If the virtual sync clock is still stopped, make TM restart it.
3597 */
3598 if (VM_FF_ISPENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
3599 TMR3VirtualSyncFF(pVM, pVCpu);
3600
3601#ifdef DEBUG
3602 /*
3603 * Debug, pause the VM.
3604 */
3605 if (VM_FF_ISPENDING(pVM, VM_FF_DEBUG_SUSPEND))
3606 {
3607 VM_FF_CLEAR(pVM, VM_FF_DEBUG_SUSPEND);
3608 Log(("emR3ForcedActions: returns VINF_EM_SUSPEND\n"));
3609 return VINF_EM_SUSPEND;
3610 }
3611#endif
3612
3613 /* check that we got them all */
3614 AssertCompile(VM_FF_HIGH_PRIORITY_PRE_MASK == (VM_FF_TM_VIRTUAL_SYNC | VM_FF_DBGF | VM_FF_TERMINATE | VM_FF_DEBUG_SUSPEND | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY));
3615 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));
3616 }
3617
3618#undef UPDATE_RC
3619 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
3620 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatForcedActions, a);
3621 Assert(rcIrq == VINF_SUCCESS || rcIrq == rc);
3622 return rc;
3623}
3624
3625/**
3626 * Release the IOM lock if owned by the current VCPU
3627 *
3628 * @param pVM The VM to operate on.
3629 */
3630VMMR3DECL(void) EMR3ReleaseOwnedLocks(PVM pVM)
3631{
3632 while (PDMCritSectIsOwner(&pVM->em.s.CritSectREM))
3633 PDMCritSectLeave(&pVM->em.s.CritSectREM);
3634}
3635
3636
3637/**
3638 * Execute VM.
3639 *
3640 * This function is the main loop of the VM. The emulation thread
3641 * calls this function when the VM has been successfully constructed
3642 * and we're ready for executing the VM.
3643 *
3644 * Returning from this function means that the VM is turned off or
3645 * suspended (state already saved) and deconstruction in next in line.
3646 *
3647 * All interaction from other thread are done using forced actions
3648 * and signaling of the wait object.
3649 *
3650 * @returns VBox status code, informational status codes may indicate failure.
3651 * @param pVM The VM to operate on.
3652 * @param pVCpu The VMCPU to operate on.
3653 */
3654VMMR3DECL(int) EMR3ExecuteVM(PVM pVM, PVMCPU pVCpu)
3655{
3656 LogFlow(("EMR3ExecuteVM: pVM=%p enmVMState=%d enmState=%d (%s) fForceRAW=%d\n", pVM, pVM->enmVMState,
3657 pVCpu->em.s.enmState, EMR3GetStateName(pVCpu->em.s.enmState), pVCpu->em.s.fForceRAW));
3658 VM_ASSERT_EMT(pVM);
3659 Assert(pVCpu->em.s.enmState == EMSTATE_NONE || pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI || pVCpu->em.s.enmState == EMSTATE_SUSPENDED);
3660
3661 int rc = setjmp(pVCpu->em.s.u.FatalLongJump);
3662 if (rc == 0)
3663 {
3664 /*
3665 * Start the virtual time.
3666 */
3667 TMR3NotifyResume(pVM, pVCpu);
3668
3669 /*
3670 * The Outer Main Loop.
3671 */
3672 bool fFFDone = false;
3673
3674 /* Reschedule right away to start in the right state. */
3675 rc = VINF_SUCCESS;
3676
3677 if ( pVCpu->em.s.enmState == EMSTATE_SUSPENDED
3678 && ( pVCpu->em.s.enmPrevState == EMSTATE_WAIT_SIPI
3679 || pVCpu->em.s.enmPrevState == EMSTATE_HALTED))
3680 {
3681 /* Pause->Resume: Restore the old wait state or else we'll start executing code. */
3682 pVCpu->em.s.enmState = pVCpu->em.s.enmPrevState;
3683 }
3684 else
3685 pVCpu->em.s.enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
3686
3687 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
3688 for (;;)
3689 {
3690 /*
3691 * Before we can schedule anything (we're here because
3692 * scheduling is required) we must service any pending
3693 * forced actions to avoid any pending action causing
3694 * immediate rescheduling upon entering an inner loop
3695 *
3696 * Do forced actions.
3697 */
3698 if ( !fFFDone
3699 && rc != VINF_EM_TERMINATE
3700 && rc != VINF_EM_OFF
3701 && ( VM_FF_ISPENDING(pVM, VM_FF_ALL_BUT_RAW_MASK)
3702 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_ALL_BUT_RAW_MASK)))
3703 {
3704 rc = emR3ForcedActions(pVM, pVCpu, rc);
3705 if ( ( rc == VINF_EM_RESCHEDULE_REM
3706 || rc == VINF_EM_RESCHEDULE_HWACC)
3707 && pVCpu->em.s.fForceRAW)
3708 rc = VINF_EM_RESCHEDULE_RAW;
3709 }
3710 else if (fFFDone)
3711 fFFDone = false;
3712
3713 /*
3714 * Now what to do?
3715 */
3716 Log2(("EMR3ExecuteVM: rc=%Rrc\n", rc));
3717 switch (rc)
3718 {
3719 /*
3720 * Keep doing what we're currently doing.
3721 */
3722 case VINF_SUCCESS:
3723 break;
3724
3725 /*
3726 * Reschedule - to raw-mode execution.
3727 */
3728 case VINF_EM_RESCHEDULE_RAW:
3729 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_RAW: %d -> %d (EMSTATE_RAW)\n", pVCpu->em.s.enmState, EMSTATE_RAW));
3730 pVCpu->em.s.enmState = EMSTATE_RAW;
3731 break;
3732
3733 /*
3734 * Reschedule - to hardware accelerated raw-mode execution.
3735 */
3736 case VINF_EM_RESCHEDULE_HWACC:
3737 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_HWACC: %d -> %d (EMSTATE_HWACC)\n", pVCpu->em.s.enmState, EMSTATE_HWACC));
3738 Assert(!pVCpu->em.s.fForceRAW);
3739 pVCpu->em.s.enmState = EMSTATE_HWACC;
3740 break;
3741
3742 /*
3743 * Reschedule - to recompiled execution.
3744 */
3745 case VINF_EM_RESCHEDULE_REM:
3746 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_REM: %d -> %d (EMSTATE_REM)\n", pVCpu->em.s.enmState, EMSTATE_REM));
3747 pVCpu->em.s.enmState = EMSTATE_REM;
3748 break;
3749
3750#ifdef VBOX_WITH_VMI
3751 /*
3752 * Reschedule - parav call.
3753 */
3754 case VINF_EM_RESCHEDULE_PARAV:
3755 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_PARAV: %d -> %d (EMSTATE_PARAV)\n", pVCpu->em.s.enmState, EMSTATE_PARAV));
3756 pVCpu->em.s.enmState = EMSTATE_PARAV;
3757 break;
3758#endif
3759
3760 /*
3761 * Resume.
3762 */
3763 case VINF_EM_RESUME:
3764 Log2(("EMR3ExecuteVM: VINF_EM_RESUME: %d -> VINF_EM_RESCHEDULE\n", pVCpu->em.s.enmState));
3765 /* Don't reschedule in the halted or wait for SIPI case. */
3766 if ( pVCpu->em.s.enmPrevState == EMSTATE_WAIT_SIPI
3767 || pVCpu->em.s.enmPrevState == EMSTATE_HALTED)
3768 break;
3769 /* fall through and get scheduled. */
3770
3771 /*
3772 * Reschedule.
3773 */
3774 case VINF_EM_RESCHEDULE:
3775 {
3776 EMSTATE enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
3777 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE: %d -> %d (%s)\n", pVCpu->em.s.enmState, enmState, EMR3GetStateName(enmState)));
3778 pVCpu->em.s.enmState = enmState;
3779 break;
3780 }
3781
3782 /*
3783 * Halted.
3784 */
3785 case VINF_EM_HALT:
3786 Log2(("EMR3ExecuteVM: VINF_EM_HALT: %d -> %d\n", pVCpu->em.s.enmState, EMSTATE_HALTED));
3787 pVCpu->em.s.enmState = EMSTATE_HALTED;
3788 break;
3789
3790 /*
3791 * Switch to the wait for SIPI state (application processor only)
3792 */
3793 case VINF_EM_WAIT_SIPI:
3794 Assert(pVCpu->idCpu != 0);
3795 Log2(("EMR3ExecuteVM: VINF_EM_WAIT_SIPI: %d -> %d\n", pVCpu->em.s.enmState, EMSTATE_WAIT_SIPI));
3796 pVCpu->em.s.enmState = EMSTATE_WAIT_SIPI;
3797 break;
3798
3799
3800 /*
3801 * Suspend.
3802 */
3803 case VINF_EM_SUSPEND:
3804 Log2(("EMR3ExecuteVM: VINF_EM_SUSPEND: %d -> %d\n", pVCpu->em.s.enmState, EMSTATE_SUSPENDED));
3805 pVCpu->em.s.enmPrevState = pVCpu->em.s.enmState;
3806 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
3807 break;
3808
3809 /*
3810 * Reset.
3811 * We might end up doing a double reset for now, we'll have to clean up the mess later.
3812 */
3813 case VINF_EM_RESET:
3814 {
3815 if (pVCpu->idCpu == 0)
3816 {
3817 EMSTATE enmState = emR3Reschedule(pVM, pVCpu, pVCpu->em.s.pCtx);
3818 Log2(("EMR3ExecuteVM: VINF_EM_RESET: %d -> %d (%s)\n", pVCpu->em.s.enmState, enmState, EMR3GetStateName(enmState)));
3819 pVCpu->em.s.enmState = enmState;
3820 }
3821 else
3822 {
3823 /* All other VCPUs go into the wait for SIPI state. */
3824 pVCpu->em.s.enmState = EMSTATE_WAIT_SIPI;
3825 }
3826 break;
3827 }
3828
3829 /*
3830 * Power Off.
3831 */
3832 case VINF_EM_OFF:
3833 pVCpu->em.s.enmState = EMSTATE_TERMINATING;
3834 Log2(("EMR3ExecuteVM: returns VINF_EM_OFF (%d -> %d)\n", pVCpu->em.s.enmState, EMSTATE_TERMINATING));
3835 TMR3NotifySuspend(pVM, pVCpu);
3836 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
3837 return rc;
3838
3839 /*
3840 * Terminate the VM.
3841 */
3842 case VINF_EM_TERMINATE:
3843 pVCpu->em.s.enmState = EMSTATE_TERMINATING;
3844 Log(("EMR3ExecuteVM returns VINF_EM_TERMINATE (%d -> %d)\n", pVCpu->em.s.enmState, EMSTATE_TERMINATING));
3845 TMR3NotifySuspend(pVM, pVCpu);
3846 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
3847 return rc;
3848
3849
3850 /*
3851 * Out of memory, suspend the VM and stuff.
3852 */
3853 case VINF_EM_NO_MEMORY:
3854 Log2(("EMR3ExecuteVM: VINF_EM_NO_MEMORY: %d -> %d\n", pVCpu->em.s.enmState, EMSTATE_SUSPENDED));
3855 pVCpu->em.s.enmState = EMSTATE_SUSPENDED;
3856 TMR3NotifySuspend(pVM, pVCpu);
3857 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
3858
3859 rc = VMSetRuntimeError(pVM, VMSETRTERR_FLAGS_SUSPEND, "HostMemoryLow",
3860 N_("Unable to allocate and lock memory. The virtual machine will be paused. Please close applications to free up memory or close the VM"));
3861 if (rc != VINF_EM_SUSPEND)
3862 {
3863 if (RT_SUCCESS_NP(rc))
3864 {
3865 AssertLogRelMsgFailed(("%Rrc\n", rc));
3866 rc = VERR_EM_INTERNAL_ERROR;
3867 }
3868 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
3869 }
3870 return rc;
3871
3872 /*
3873 * Guest debug events.
3874 */
3875 case VINF_EM_DBG_STEPPED:
3876 AssertMsgFailed(("VINF_EM_DBG_STEPPED cannot be here!"));
3877 case VINF_EM_DBG_STOP:
3878 case VINF_EM_DBG_BREAKPOINT:
3879 case VINF_EM_DBG_STEP:
3880 if (pVCpu->em.s.enmState == EMSTATE_RAW)
3881 {
3882 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVCpu->em.s.enmState, EMSTATE_DEBUG_GUEST_RAW));
3883 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
3884 }
3885 else
3886 {
3887 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVCpu->em.s.enmState, EMSTATE_DEBUG_GUEST_REM));
3888 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_REM;
3889 }
3890 break;
3891
3892 /*
3893 * Hypervisor debug events.
3894 */
3895 case VINF_EM_DBG_HYPER_STEPPED:
3896 case VINF_EM_DBG_HYPER_BREAKPOINT:
3897 case VINF_EM_DBG_HYPER_ASSERTION:
3898 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVCpu->em.s.enmState, EMSTATE_DEBUG_HYPER));
3899 pVCpu->em.s.enmState = EMSTATE_DEBUG_HYPER;
3900 break;
3901
3902 /*
3903 * Guru mediations.
3904 */
3905 case VERR_VMM_RING0_ASSERTION:
3906 Log(("EMR3ExecuteVM: %Rrc: %d -> %d (EMSTATE_GURU_MEDITATION)\n", rc, pVCpu->em.s.enmState, EMSTATE_GURU_MEDITATION));
3907 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
3908 break;
3909
3910 /*
3911 * Any error code showing up here other than the ones we
3912 * know and process above are considered to be FATAL.
3913 *
3914 * Unknown warnings and informational status codes are also
3915 * included in this.
3916 */
3917 default:
3918 if (RT_SUCCESS_NP(rc))
3919 {
3920 AssertMsgFailed(("Unexpected warning or informational status code %Rra!\n", rc));
3921 rc = VERR_EM_INTERNAL_ERROR;
3922 }
3923 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
3924 Log(("EMR3ExecuteVM returns %d\n", rc));
3925 break;
3926 }
3927
3928 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x); /* (skip this in release) */
3929 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
3930
3931 /*
3932 * Act on the state.
3933 */
3934 switch (pVCpu->em.s.enmState)
3935 {
3936 /*
3937 * Execute raw.
3938 */
3939 case EMSTATE_RAW:
3940 rc = emR3RawExecute(pVM, pVCpu, &fFFDone);
3941 break;
3942
3943 /*
3944 * Execute hardware accelerated raw.
3945 */
3946 case EMSTATE_HWACC:
3947 rc = emR3HwAccExecute(pVM, pVCpu, &fFFDone);
3948 break;
3949
3950 /*
3951 * Execute recompiled.
3952 */
3953 case EMSTATE_REM:
3954 rc = emR3RemExecute(pVM, pVCpu, &fFFDone);
3955 Log2(("EMR3ExecuteVM: emR3RemExecute -> %Rrc\n", rc));
3956 break;
3957
3958#ifdef VBOX_WITH_VMI
3959 /*
3960 * Execute PARAV function.
3961 */
3962 case EMSTATE_PARAV:
3963 rc = PARAVCallFunction(pVM);
3964 pVCpu->em.s.enmState = EMSTATE_REM;
3965 break;
3966#endif
3967
3968 /*
3969 * Application processor execution halted until SIPI.
3970 */
3971 case EMSTATE_WAIT_SIPI:
3972 /* no break */
3973 /*
3974 * hlt - execution halted until interrupt.
3975 */
3976 case EMSTATE_HALTED:
3977 {
3978 STAM_REL_PROFILE_START(&pVCpu->em.s.StatHalted, y);
3979 rc = VMR3WaitHalted(pVM, pVCpu, !(CPUMGetGuestEFlags(pVCpu) & X86_EFL_IF));
3980 STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatHalted, y);
3981 break;
3982 }
3983
3984 /*
3985 * Suspended - return to VM.cpp.
3986 */
3987 case EMSTATE_SUSPENDED:
3988 TMR3NotifySuspend(pVM, pVCpu);
3989 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
3990 return VINF_EM_SUSPEND;
3991
3992 /*
3993 * Debugging in the guest.
3994 */
3995 case EMSTATE_DEBUG_GUEST_REM:
3996 case EMSTATE_DEBUG_GUEST_RAW:
3997 TMR3NotifySuspend(pVM, pVCpu);
3998 rc = emR3Debug(pVM, pVCpu, rc);
3999 TMR3NotifyResume(pVM, pVCpu);
4000 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVCpu->em.s.enmState));
4001 break;
4002
4003 /*
4004 * Debugging in the hypervisor.
4005 */
4006 case EMSTATE_DEBUG_HYPER:
4007 {
4008 TMR3NotifySuspend(pVM, pVCpu);
4009 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
4010
4011 rc = emR3Debug(pVM, pVCpu, rc);
4012 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVCpu->em.s.enmState));
4013 if (rc != VINF_SUCCESS)
4014 {
4015 /* switch to guru meditation mode */
4016 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
4017 VMMR3FatalDump(pVM, pVCpu, rc);
4018 return rc;
4019 }
4020
4021 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatTotal, x);
4022 TMR3NotifyResume(pVM, pVCpu);
4023 break;
4024 }
4025
4026 /*
4027 * Guru meditation takes place in the debugger.
4028 */
4029 case EMSTATE_GURU_MEDITATION:
4030 {
4031 TMR3NotifySuspend(pVM, pVCpu);
4032 VMMR3FatalDump(pVM, pVCpu, rc);
4033 emR3Debug(pVM, pVCpu, rc);
4034 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
4035 return rc;
4036 }
4037
4038 /*
4039 * The states we don't expect here.
4040 */
4041 case EMSTATE_NONE:
4042 case EMSTATE_TERMINATING:
4043 default:
4044 AssertMsgFailed(("EMR3ExecuteVM: Invalid state %d!\n", pVCpu->em.s.enmState));
4045 pVCpu->em.s.enmState = EMSTATE_GURU_MEDITATION;
4046 TMR3NotifySuspend(pVM, pVCpu);
4047 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
4048 return VERR_EM_INTERNAL_ERROR;
4049 }
4050 } /* The Outer Main Loop */
4051 }
4052 else
4053 {
4054 /*
4055 * Fatal error.
4056 */
4057 LogFlow(("EMR3ExecuteVM: returns %Rrc (longjmp / fatal error)\n", rc));
4058 TMR3NotifySuspend(pVM, pVCpu);
4059 VMMR3FatalDump(pVM, pVCpu, rc);
4060 emR3Debug(pVM, pVCpu, rc);
4061 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatTotal, x);
4062 /** @todo change the VM state! */
4063 return rc;
4064 }
4065
4066 /* (won't ever get here). */
4067 AssertFailed();
4068}
4069
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