VirtualBox

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

Last change on this file since 16596 was 16132, checked in by vboxsync, 16 years ago

EM.cpp: Added a debug log statement for REMR3EmulationInstruction.

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