VirtualBox

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

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

EM.cpp: Redo emR3RawForcedActions after CSAMR3CheckCodeEx if it becomes necessary, it may register virtual access handlers which require a SyncCR3. More strictness.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 154.2 KB
Line 
1/* $Id: EM.cpp 18288 2009-03-26 05:00:02Z 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 Log(("Force recompiler switch due to cr0 (%RGp) update rip=%RGv -> %RGv (enmState=%d)\n", pCtx->cr0, pCtx->rip, pOrgInstrGC, enmState));
2100 if (enmState == PATMTRANS_OVERWRITTEN)
2101 {
2102 rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
2103 Assert(rc == VERR_PATCH_DISABLED);
2104 /* Conflict detected, patch disabled */
2105 Log(("emR3RawPrivileged: detected conflict -> disabled patch at %RGv\n", (RTGCPTR)pCtx->rip));
2106 enmState = PATMTRANS_SAFE;
2107 }
2108 /* The translation had better be successful. Otherwise we can't recover. */
2109 AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %RGv\n", (RTGCPTR)pCtx->rip));
2110 if (enmState != PATMTRANS_OVERWRITTEN)
2111 pCtx->rip = pOrgInstrGC;
2112 }
2113
2114 /* Reschedule is necessary as the execution/paging mode might have changed. */
2115 return VINF_EM_RESCHEDULE;
2116 }
2117 return rc; /* can return VINF_EM_HALT as well. */
2118 }
2119 AssertMsgReturn(rc == VERR_EM_INTERPRETER, ("%Rrc\n", rc), rc);
2120 break; /* fall back to the recompiler */
2121 }
2122 STAM_PROFILE_STOP(&pVM->em.s.StatPrivEmu, a);
2123 }
2124 }
2125
2126 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
2127 return emR3PatchTrap(pVM, pCtx, VINF_PATM_PATCH_TRAP_GP);
2128
2129 return emR3RawExecuteInstruction(pVM, "PRIV");
2130}
2131
2132
2133/**
2134 * Update the forced rawmode execution modifier.
2135 *
2136 * This function is called when we're returning from the raw-mode loop(s). If we're
2137 * in patch code, it will set a flag forcing execution to be resumed in raw-mode,
2138 * if not in patch code, the flag will be cleared.
2139 *
2140 * We should never interrupt patch code while it's being executed. Cli patches can
2141 * contain big code blocks, but they are always executed with IF=0. Other patches
2142 * replace single instructions and should be atomic.
2143 *
2144 * @returns Updated rc.
2145 *
2146 * @param pVM The VM handle.
2147 * @param pCtx The guest CPU context.
2148 * @param rc The result code.
2149 */
2150DECLINLINE(int) emR3RawUpdateForceFlag(PVM pVM, PCPUMCTX pCtx, int rc)
2151{
2152 if (PATMIsPatchGCAddr(pVM, pCtx->eip)) /** @todo check cs selector base/type */
2153 {
2154 /* ignore reschedule attempts. */
2155 switch (rc)
2156 {
2157 case VINF_EM_RESCHEDULE:
2158 case VINF_EM_RESCHEDULE_REM:
2159 LogFlow(("emR3RawUpdateForceFlag: patch address -> force raw reschedule\n"));
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 * (MM)IO intensive code block detected; fall back to the recompiler for better performance
2360 */
2361 case VINF_EM_RAW_EMULATE_IO_BLOCK:
2362 rc =HWACCMR3EmulateIoBlock(pVM, pCtx);
2363 break;
2364
2365 /*
2366 * Execute instruction.
2367 */
2368 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
2369 rc = emR3RawExecuteInstruction(pVM, "LDT FAULT: ");
2370 break;
2371 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
2372 rc = emR3RawExecuteInstruction(pVM, "GDT FAULT: ");
2373 break;
2374 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
2375 rc = emR3RawExecuteInstruction(pVM, "IDT FAULT: ");
2376 break;
2377 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
2378 rc = emR3RawExecuteInstruction(pVM, "TSS FAULT: ");
2379 break;
2380 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
2381 rc = emR3RawExecuteInstruction(pVM, "PD FAULT: ");
2382 break;
2383
2384 case VINF_EM_RAW_EMULATE_INSTR_HLT:
2385 /** @todo skip instruction and go directly to the halt state. (see REM for implementation details) */
2386 rc = emR3RawPrivileged(pVM);
2387 break;
2388
2389 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
2390 rc = emR3RawExecuteInstruction(pVM, "EMUL: ", VINF_PATM_PENDING_IRQ_AFTER_IRET);
2391 break;
2392
2393 case VINF_EM_RAW_EMULATE_INSTR:
2394 case VINF_PATCH_EMULATE_INSTR:
2395 rc = emR3RawExecuteInstruction(pVM, "EMUL: ");
2396 break;
2397
2398 /*
2399 * Stale selector and iret traps => REM.
2400 */
2401 case VINF_EM_RAW_STALE_SELECTOR:
2402 case VINF_EM_RAW_IRET_TRAP:
2403 /* We will not go to the recompiler if EIP points to patch code. */
2404 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
2405 {
2406 pCtx->eip = PATMR3PatchToGCPtr(pVM, (RTGCPTR)pCtx->eip, 0);
2407 }
2408 LogFlow(("emR3RawHandleRC: %Rrc -> %Rrc\n", rc, VINF_EM_RESCHEDULE_REM));
2409 rc = VINF_EM_RESCHEDULE_REM;
2410 break;
2411
2412 /*
2413 * Up a level.
2414 */
2415 case VINF_EM_TERMINATE:
2416 case VINF_EM_OFF:
2417 case VINF_EM_RESET:
2418 case VINF_EM_SUSPEND:
2419 case VINF_EM_HALT:
2420 case VINF_EM_RESUME:
2421 case VINF_EM_RESCHEDULE:
2422 case VINF_EM_RESCHEDULE_REM:
2423 break;
2424
2425 /*
2426 * Up a level and invoke the debugger.
2427 */
2428 case VINF_EM_DBG_STEPPED:
2429 case VINF_EM_DBG_BREAKPOINT:
2430 case VINF_EM_DBG_STEP:
2431 case VINF_EM_DBG_HYPER_BREAKPOINT:
2432 case VINF_EM_DBG_HYPER_STEPPED:
2433 case VINF_EM_DBG_HYPER_ASSERTION:
2434 case VINF_EM_DBG_STOP:
2435 break;
2436
2437 /*
2438 * Up a level, dump and debug.
2439 */
2440 case VERR_TRPM_DONT_PANIC:
2441 case VERR_TRPM_PANIC:
2442 case VERR_VMM_RING0_ASSERTION:
2443 break;
2444
2445 /*
2446 * Up a level, after HwAccM have done some release logging.
2447 */
2448 case VERR_VMX_INVALID_VMCS_FIELD:
2449 case VERR_VMX_INVALID_VMCS_PTR:
2450 case VERR_VMX_INVALID_VMXON_PTR:
2451 case VERR_VMX_UNEXPECTED_INTERRUPTION_EXIT_CODE:
2452 case VERR_VMX_UNEXPECTED_EXCEPTION:
2453 case VERR_VMX_UNEXPECTED_EXIT_CODE:
2454 case VERR_VMX_INVALID_GUEST_STATE:
2455 case VERR_VMX_UNABLE_TO_START_VM:
2456 case VERR_VMX_UNABLE_TO_RESUME_VM:
2457 HWACCMR3CheckError(pVM, rc);
2458 break;
2459 /*
2460 * Anything which is not known to us means an internal error
2461 * and the termination of the VM!
2462 */
2463 default:
2464 AssertMsgFailed(("Unknown GC return code: %Rra\n", rc));
2465 break;
2466 }
2467 return rc;
2468}
2469
2470
2471/**
2472 * Check for pending raw actions
2473 *
2474 * @returns VBox status code.
2475 * @param pVM The VM to operate on.
2476 */
2477VMMR3DECL(int) EMR3CheckRawForcedActions(PVM pVM)
2478{
2479 return emR3RawForcedActions(pVM, pVM->em.s.pCtx);
2480}
2481
2482
2483/**
2484 * Process raw-mode specific forced actions.
2485 *
2486 * This function is called when any FFs in the VM_FF_HIGH_PRIORITY_PRE_RAW_MASK is pending.
2487 *
2488 * @returns VBox status code.
2489 * Only the normal success/failure stuff, no VINF_EM_*.
2490 * @param pVM The VM handle.
2491 * @param pCtx The guest CPUM register context.
2492 */
2493static int emR3RawForcedActions(PVM pVM, PCPUMCTX pCtx)
2494{
2495 /*
2496 * Note that the order is *vitally* important!
2497 * Also note that SELMR3UpdateFromCPUM may trigger VM_FF_SELM_SYNC_TSS.
2498 */
2499
2500
2501 /*
2502 * Sync selector tables.
2503 */
2504 if (VM_FF_ISPENDING(pVM, VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT))
2505 {
2506 int rc = SELMR3UpdateFromCPUM(pVM);
2507 if (RT_FAILURE(rc))
2508 return rc;
2509 }
2510
2511 /*
2512 * Sync IDT.
2513 */
2514 if (VM_FF_ISSET(pVM, VM_FF_TRPM_SYNC_IDT))
2515 {
2516 int rc = TRPMR3SyncIDT(pVM);
2517 if (RT_FAILURE(rc))
2518 return rc;
2519 }
2520
2521 /*
2522 * Sync TSS.
2523 */
2524 if (VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_TSS))
2525 {
2526 int rc = SELMR3SyncTSS(pVM);
2527 if (RT_FAILURE(rc))
2528 return rc;
2529 }
2530
2531 /*
2532 * Sync page directory.
2533 */
2534 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
2535 {
2536 int rc = PGMSyncCR3(pVM, pCtx->cr0, pCtx->cr3, pCtx->cr4, VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
2537 if (RT_FAILURE(rc))
2538 return rc;
2539
2540 Assert(!VM_FF_ISPENDING(pVM, VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT));
2541
2542 /* Prefetch pages for EIP and ESP */
2543 /** @todo This is rather expensive. Should investigate if it really helps at all. */
2544 rc = PGMPrefetchPage(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->rip));
2545 if (rc == VINF_SUCCESS)
2546 rc = PGMPrefetchPage(pVM, SELMToFlat(pVM, DIS_SELREG_SS, CPUMCTX2CORE(pCtx), pCtx->rsp));
2547 if (rc != VINF_SUCCESS)
2548 {
2549 if (rc != VINF_PGM_SYNC_CR3)
2550 return rc;
2551 rc = PGMSyncCR3(pVM, pCtx->cr0, pCtx->cr3, pCtx->cr4, VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
2552 if (RT_FAILURE(rc))
2553 return rc;
2554 }
2555 /** @todo maybe prefetch the supervisor stack page as well */
2556 Assert(!VM_FF_ISPENDING(pVM, VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT));
2557 }
2558
2559 /*
2560 * Allocate handy pages (just in case the above actions have consumed some pages).
2561 */
2562 if (VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
2563 {
2564 int rc = PGMR3PhysAllocateHandyPages(pVM);
2565 if (RT_FAILURE(rc))
2566 return rc;
2567 }
2568
2569 return VINF_SUCCESS;
2570}
2571
2572
2573/**
2574 * Executes raw code.
2575 *
2576 * This function contains the raw-mode version of the inner
2577 * execution loop (the outer loop being in EMR3ExecuteVM()).
2578 *
2579 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
2580 * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
2581 *
2582 * @param pVM VM handle.
2583 * @param pfFFDone Where to store an indicator telling whether or not
2584 * FFs were done before returning.
2585 */
2586static int emR3RawExecute(PVM pVM, bool *pfFFDone)
2587{
2588 STAM_REL_PROFILE_ADV_START(&pVM->em.s.StatRAWTotal, a);
2589
2590 int rc = VERR_INTERNAL_ERROR;
2591 PCPUMCTX pCtx = pVM->em.s.pCtx;
2592 LogFlow(("emR3RawExecute: (cs:eip=%04x:%08x)\n", pCtx->cs, pCtx->eip));
2593 pVM->em.s.fForceRAW = false;
2594 *pfFFDone = false;
2595
2596
2597 /*
2598 *
2599 * Spin till we get a forced action or raw mode status code resulting in
2600 * in anything but VINF_SUCCESS or VINF_EM_RESCHEDULE_RAW.
2601 *
2602 */
2603 for (;;)
2604 {
2605 STAM_PROFILE_ADV_START(&pVM->em.s.StatRAWEntry, b);
2606
2607 /*
2608 * Check various preconditions.
2609 */
2610#ifdef VBOX_STRICT
2611 Assert(REMR3QueryPendingInterrupt(pVM) == REM_NO_PENDING_IRQ);
2612 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) == 3 || (pCtx->ss & X86_SEL_RPL) == 0);
2613 AssertMsg( (pCtx->eflags.u32 & X86_EFL_IF)
2614 || PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip),
2615 ("Tried to execute code with IF at EIP=%08x!\n", pCtx->eip));
2616 if ( !VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL)
2617 && PGMMapHasConflicts(pVM))
2618 {
2619 PGMMapCheck(pVM);
2620 AssertMsgFailed(("We should not get conflicts any longer!!!\n"));
2621 return VERR_INTERNAL_ERROR;
2622 }
2623#endif /* VBOX_STRICT */
2624
2625 /*
2626 * Process high priority pre-execution raw-mode FFs.
2627 */
2628 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2629 {
2630 rc = emR3RawForcedActions(pVM, pCtx);
2631 if (RT_FAILURE(rc))
2632 break;
2633 }
2634
2635 /*
2636 * If we're going to execute ring-0 code, the guest state needs to
2637 * be modified a bit and some of the state components (IF, SS/CS RPL,
2638 * and perhaps EIP) needs to be stored with PATM.
2639 */
2640 rc = CPUMRawEnter(pVM, NULL);
2641 if (rc != VINF_SUCCESS)
2642 {
2643 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatRAWEntry, b);
2644 break;
2645 }
2646
2647 /*
2648 * Scan code before executing it. Don't bother with user mode or V86 code
2649 */
2650 if ( (pCtx->ss & X86_SEL_RPL) <= 1
2651 && !pCtx->eflags.Bits.u1VM
2652 && !PATMIsPatchGCAddr(pVM, pCtx->eip))
2653 {
2654 STAM_PROFILE_ADV_SUSPEND(&pVM->em.s.StatRAWEntry, b);
2655 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
2656 STAM_PROFILE_ADV_RESUME(&pVM->em.s.StatRAWEntry, b);
2657 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2658 {
2659 rc = emR3RawForcedActions(pVM, pCtx);
2660 if (RT_FAILURE(rc))
2661 break;
2662 }
2663 }
2664
2665#ifdef LOG_ENABLED
2666 /*
2667 * Log important stuff before entering GC.
2668 */
2669 PPATMGCSTATE pGCState = PATMR3QueryGCStateHC(pVM);
2670 if (pCtx->eflags.Bits.u1VM)
2671 Log(("RV86: %04X:%08X IF=%d VMFlags=%x\n", pCtx->cs, pCtx->eip, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
2672 else if ((pCtx->ss & X86_SEL_RPL) == 1)
2673 {
2674 bool fCSAMScanned = CSAMIsPageScanned(pVM, (RTGCPTR)pCtx->eip);
2675 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));
2676 }
2677 else if ((pCtx->ss & X86_SEL_RPL) == 3)
2678 Log(("RR3: %08X ESP=%08X IF=%d VMFlags=%x\n", pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
2679#endif /* LOG_ENABLED */
2680
2681
2682
2683 /*
2684 * Execute the code.
2685 */
2686 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatRAWEntry, b);
2687 STAM_PROFILE_START(&pVM->em.s.StatRAWExec, c);
2688 VMMR3Unlock(pVM);
2689 rc = VMMR3RawRunGC(pVM);
2690 VMMR3Lock(pVM);
2691 STAM_PROFILE_STOP(&pVM->em.s.StatRAWExec, c);
2692 STAM_PROFILE_ADV_START(&pVM->em.s.StatRAWTail, d);
2693
2694 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)));
2695 LogFlow(("VMMR3RawRunGC returned %Rrc\n", rc));
2696
2697
2698
2699 /*
2700 * Restore the real CPU state and deal with high priority post
2701 * execution FFs before doing anything else.
2702 */
2703 rc = CPUMRawLeave(pVM, NULL, rc);
2704 VM_FF_CLEAR(pVM, VM_FF_RESUME_GUEST_MASK);
2705 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK))
2706 rc = emR3HighPriorityPostForcedActions(pVM, rc);
2707
2708#ifdef VBOX_STRICT
2709 /*
2710 * Assert TSS consistency & rc vs patch code.
2711 */
2712 if ( !VM_FF_ISPENDING(pVM, VM_FF_SELM_SYNC_TSS | VM_FF_SELM_SYNC_GDT) /* GDT implies TSS at the moment. */
2713 && EMIsRawRing0Enabled(pVM))
2714 SELMR3CheckTSS(pVM);
2715 switch (rc)
2716 {
2717 case VINF_SUCCESS:
2718 case VINF_EM_RAW_INTERRUPT:
2719 case VINF_PATM_PATCH_TRAP_PF:
2720 case VINF_PATM_PATCH_TRAP_GP:
2721 case VINF_PATM_PATCH_INT3:
2722 case VINF_PATM_CHECK_PATCH_PAGE:
2723 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
2724 case VINF_EM_RAW_GUEST_TRAP:
2725 case VINF_EM_RESCHEDULE_RAW:
2726 break;
2727
2728 default:
2729 if (PATMIsPatchGCAddr(pVM, pCtx->eip) && !(pCtx->eflags.u32 & X86_EFL_TF))
2730 LogIt(NULL, 0, LOG_GROUP_PATM, ("Patch code interrupted at %RRv for reason %Rrc\n", (RTRCPTR)CPUMGetGuestEIP(pVM), rc));
2731 break;
2732 }
2733 /*
2734 * Let's go paranoid!
2735 */
2736 if ( !VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL)
2737 && PGMMapHasConflicts(pVM))
2738 {
2739 PGMMapCheck(pVM);
2740 AssertMsgFailed(("We should not get conflicts any longer!!! rc=%Rrc\n", rc));
2741 return VERR_INTERNAL_ERROR;
2742 }
2743#endif /* VBOX_STRICT */
2744
2745 /*
2746 * Process the returned status code.
2747 */
2748 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
2749 {
2750 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatRAWTail, d);
2751 break;
2752 }
2753 rc = emR3RawHandleRC(pVM, pCtx, rc);
2754 if (rc != VINF_SUCCESS)
2755 {
2756 rc = emR3RawUpdateForceFlag(pVM, pCtx, rc);
2757 if (rc != VINF_SUCCESS)
2758 {
2759 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatRAWTail, d);
2760 break;
2761 }
2762 }
2763
2764 /*
2765 * Check and execute forced actions.
2766 */
2767#ifdef VBOX_HIGH_RES_TIMERS_HACK
2768 TMTimerPoll(pVM);
2769#endif
2770 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatRAWTail, d);
2771 if (VM_FF_ISPENDING(pVM, ~VM_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2772 {
2773 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) != 1);
2774
2775 STAM_REL_PROFILE_ADV_SUSPEND(&pVM->em.s.StatRAWTotal, a);
2776 rc = emR3ForcedActions(pVM, rc);
2777 STAM_REL_PROFILE_ADV_RESUME(&pVM->em.s.StatRAWTotal, a);
2778 if ( rc != VINF_SUCCESS
2779 && rc != VINF_EM_RESCHEDULE_RAW)
2780 {
2781 rc = emR3RawUpdateForceFlag(pVM, pCtx, rc);
2782 if (rc != VINF_SUCCESS)
2783 {
2784 *pfFFDone = true;
2785 break;
2786 }
2787 }
2788 }
2789 }
2790
2791 /*
2792 * Return to outer loop.
2793 */
2794#if defined(LOG_ENABLED) && defined(DEBUG)
2795 RTLogFlush(NULL);
2796#endif
2797 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatRAWTotal, a);
2798 return rc;
2799}
2800
2801
2802/**
2803 * Executes hardware accelerated raw code. (Intel VMX & AMD SVM)
2804 *
2805 * This function contains the raw-mode version of the inner
2806 * execution loop (the outer loop being in EMR3ExecuteVM()).
2807 *
2808 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE, VINF_EM_RESCHEDULE_RAW,
2809 * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
2810 *
2811 * @param pVM VM handle.
2812 * @param idCpu VMCPU id.
2813 * @param pfFFDone Where to store an indicator telling whether or not
2814 * FFs were done before returning.
2815 */
2816static int emR3HwAccExecute(PVM pVM, RTCPUID idCpu, bool *pfFFDone)
2817{
2818 int rc = VERR_INTERNAL_ERROR;
2819 PCPUMCTX pCtx = pVM->em.s.pCtx;
2820
2821 LogFlow(("emR3HwAccExecute%d: (cs:eip=%04x:%RGv)\n", idCpu, pCtx->cs, (RTGCPTR)pCtx->rip));
2822 *pfFFDone = false;
2823
2824 STAM_COUNTER_INC(&pVM->em.s.StatHwAccExecuteEntry);
2825
2826#ifdef EM_NOTIFY_HWACCM
2827 HWACCMR3NotifyScheduled(&pVM->aCpus[idCpu]);
2828#endif
2829
2830 /*
2831 * Spin till we get a forced action which returns anything but VINF_SUCCESS.
2832 */
2833 for (;;)
2834 {
2835 STAM_PROFILE_ADV_START(&pVM->em.s.StatHwAccEntry, a);
2836
2837 /*
2838 * Check various preconditions.
2839 */
2840 VM_FF_CLEAR(pVM, (VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT | VM_FF_TRPM_SYNC_IDT | VM_FF_SELM_SYNC_TSS));
2841
2842 /*
2843 * Process high priority pre-execution raw-mode FFs.
2844 */
2845 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK))
2846 {
2847 rc = emR3RawForcedActions(pVM, pCtx);
2848 if (RT_FAILURE(rc))
2849 break;
2850 }
2851
2852#ifdef LOG_ENABLED
2853 /*
2854 * Log important stuff before entering GC.
2855 */
2856 if (TRPMHasTrap(pVM))
2857 Log(("Pending hardware interrupt=0x%x cs:rip=%04X:%RGv\n", TRPMGetTrapNo(pVM), pCtx->cs, (RTGCPTR)pCtx->rip));
2858
2859 uint32_t cpl = CPUMGetGuestCPL(pVM, CPUMCTX2CORE(pCtx));
2860 if (pCtx->eflags.Bits.u1VM)
2861 Log(("HWV86: %08X IF=%d\n", pCtx->eip, pCtx->eflags.Bits.u1IF));
2862 else if (CPUMIsGuestIn64BitCode(pVM, CPUMCTX2CORE(pCtx)))
2863 Log(("HWR%d: %04X:%RGv ESP=%RGv IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs, (RTGCPTR)pCtx->rip, pCtx->rsp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
2864 else
2865 Log(("HWR%d: %04X:%08X ESP=%08X IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
2866#endif /* LOG_ENABLED */
2867
2868 /*
2869 * Execute the code.
2870 */
2871 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatHwAccEntry, a);
2872 STAM_PROFILE_START(&pVM->em.s.StatHwAccExec, x);
2873 VMMR3Unlock(pVM);
2874 rc = VMMR3HwAccRunGC(pVM, idCpu);
2875 VMMR3Lock(pVM);
2876 STAM_PROFILE_STOP(&pVM->em.s.StatHwAccExec, x);
2877
2878 /*
2879 * Deal with high priority post execution FFs before doing anything else.
2880 */
2881 VM_FF_CLEAR(pVM, VM_FF_RESUME_GUEST_MASK);
2882 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK))
2883 rc = emR3HighPriorityPostForcedActions(pVM, rc);
2884
2885 /*
2886 * Process the returned status code.
2887 */
2888 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
2889 break;
2890
2891 rc = emR3RawHandleRC(pVM, pCtx, rc);
2892 if (rc != VINF_SUCCESS)
2893 break;
2894
2895 /*
2896 * Check and execute forced actions.
2897 */
2898#ifdef VBOX_HIGH_RES_TIMERS_HACK
2899 TMTimerPoll(pVM);
2900#endif
2901 if (VM_FF_ISPENDING(pVM, VM_FF_ALL_MASK))
2902 {
2903 rc = emR3ForcedActions(pVM, rc);
2904 if ( rc != VINF_SUCCESS
2905 && rc != VINF_EM_RESCHEDULE_HWACC)
2906 {
2907 *pfFFDone = true;
2908 break;
2909 }
2910 }
2911 }
2912
2913 /*
2914 * Return to outer loop.
2915 */
2916#if defined(LOG_ENABLED) && defined(DEBUG)
2917 RTLogFlush(NULL);
2918#endif
2919 return rc;
2920}
2921
2922
2923/**
2924 * Decides whether to execute RAW, HWACC or REM.
2925 *
2926 * @returns new EM state
2927 * @param pVM The VM.
2928 * @param pCtx The CPU context.
2929 */
2930static EMSTATE emR3Reschedule(PVM pVM, PCPUMCTX pCtx)
2931{
2932 /*
2933 * When forcing raw-mode execution, things are simple.
2934 */
2935 if (pVM->em.s.fForceRAW)
2936 return EMSTATE_RAW;
2937
2938 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
2939 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
2940 /* !!! THIS MUST BE IN SYNC WITH remR3CanExecuteRaw !!! */
2941
2942 X86EFLAGS EFlags = pCtx->eflags;
2943 if (HWACCMIsEnabled(pVM))
2944 {
2945 /* Hardware accelerated raw-mode:
2946 *
2947 * Typically only 32-bits protected mode, with paging enabled, code is allowed here.
2948 */
2949 if (HWACCMR3CanExecuteGuest(pVM, pCtx) == true)
2950 return EMSTATE_HWACC;
2951
2952 /* Note: Raw mode and hw accelerated mode are incompatible. The latter turns
2953 * off monitoring features essential for raw mode! */
2954 return EMSTATE_REM;
2955 }
2956
2957 /*
2958 * Standard raw-mode:
2959 *
2960 * Here we only support 16 & 32 bits protected mode ring 3 code that has no IO privileges
2961 * or 32 bits protected mode ring 0 code
2962 *
2963 * The tests are ordered by the likelyhood of being true during normal execution.
2964 */
2965 if (EFlags.u32 & (X86_EFL_TF /* | HF_INHIBIT_IRQ_MASK*/))
2966 {
2967 Log2(("raw mode refused: EFlags=%#x\n", EFlags.u32));
2968 return EMSTATE_REM;
2969 }
2970
2971#ifndef VBOX_RAW_V86
2972 if (EFlags.u32 & X86_EFL_VM) {
2973 Log2(("raw mode refused: VM_MASK\n"));
2974 return EMSTATE_REM;
2975 }
2976#endif
2977
2978 /** @todo check up the X86_CR0_AM flag in respect to raw mode!!! We're probably not emulating it right! */
2979 uint32_t u32CR0 = pCtx->cr0;
2980 if ((u32CR0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
2981 {
2982 //Log2(("raw mode refused: %s%s%s\n", (u32CR0 & X86_CR0_PG) ? "" : " !PG", (u32CR0 & X86_CR0_PE) ? "" : " !PE", (u32CR0 & X86_CR0_AM) ? "" : " !AM"));
2983 return EMSTATE_REM;
2984 }
2985
2986 if (pCtx->cr4 & X86_CR4_PAE)
2987 {
2988 uint32_t u32Dummy, u32Features;
2989
2990 CPUMGetGuestCpuId(pVM, 1, &u32Dummy, &u32Dummy, &u32Dummy, &u32Features);
2991 if (!(u32Features & X86_CPUID_FEATURE_EDX_PAE))
2992 return EMSTATE_REM;
2993 }
2994
2995 unsigned uSS = pCtx->ss;
2996 if ( pCtx->eflags.Bits.u1VM
2997 || (uSS & X86_SEL_RPL) == 3)
2998 {
2999 if (!EMIsRawRing3Enabled(pVM))
3000 return EMSTATE_REM;
3001
3002 if (!(EFlags.u32 & X86_EFL_IF))
3003 {
3004 Log2(("raw mode refused: IF (RawR3)\n"));
3005 return EMSTATE_REM;
3006 }
3007
3008 if (!(u32CR0 & X86_CR0_WP) && EMIsRawRing0Enabled(pVM))
3009 {
3010 Log2(("raw mode refused: CR0.WP + RawR0\n"));
3011 return EMSTATE_REM;
3012 }
3013 }
3014 else
3015 {
3016 if (!EMIsRawRing0Enabled(pVM))
3017 return EMSTATE_REM;
3018
3019 /* Only ring 0 supervisor code. */
3020 if ((uSS & X86_SEL_RPL) != 0)
3021 {
3022 Log2(("raw r0 mode refused: CPL %d\n", uSS & X86_SEL_RPL));
3023 return EMSTATE_REM;
3024 }
3025
3026 // Let's start with pure 32 bits ring 0 code first
3027 /** @todo What's pure 32-bit mode? flat? */
3028 if ( !(pCtx->ssHid.Attr.n.u1DefBig)
3029 || !(pCtx->csHid.Attr.n.u1DefBig))
3030 {
3031 Log2(("raw r0 mode refused: SS/CS not 32bit\n"));
3032 return EMSTATE_REM;
3033 }
3034
3035 /* Write protection must be turned on, or else the guest can overwrite our hypervisor code and data. */
3036 if (!(u32CR0 & X86_CR0_WP))
3037 {
3038 Log2(("raw r0 mode refused: CR0.WP=0!\n"));
3039 return EMSTATE_REM;
3040 }
3041
3042 if (PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip))
3043 {
3044 Log2(("raw r0 mode forced: patch code\n"));
3045 return EMSTATE_RAW;
3046 }
3047
3048#if !defined(VBOX_ALLOW_IF0) && !defined(VBOX_RUN_INTERRUPT_GATE_HANDLERS)
3049 if (!(EFlags.u32 & X86_EFL_IF))
3050 {
3051 ////Log2(("R0: IF=0 VIF=%d %08X\n", eip, pVMeflags));
3052 //Log2(("RR0: Interrupts turned off; fall back to emulation\n"));
3053 return EMSTATE_REM;
3054 }
3055#endif
3056
3057 /** @todo still necessary??? */
3058 if (EFlags.Bits.u2IOPL != 0)
3059 {
3060 Log2(("raw r0 mode refused: IOPL %d\n", EFlags.Bits.u2IOPL));
3061 return EMSTATE_REM;
3062 }
3063 }
3064
3065 Assert(PGMPhysIsA20Enabled(pVM));
3066 return EMSTATE_RAW;
3067}
3068
3069
3070/**
3071 * Executes all high priority post execution force actions.
3072 *
3073 * @returns rc or a fatal status code.
3074 *
3075 * @param pVM VM handle.
3076 * @param rc The current rc.
3077 */
3078static int emR3HighPriorityPostForcedActions(PVM pVM, int rc)
3079{
3080 if (VM_FF_ISSET(pVM, VM_FF_PDM_CRITSECT))
3081 PDMR3CritSectFF(pVM);
3082
3083 if (VM_FF_ISSET(pVM, VM_FF_CSAM_PENDING_ACTION))
3084 CSAMR3DoPendingAction(pVM);
3085
3086 return rc;
3087}
3088
3089
3090/**
3091 * Executes all pending forced actions.
3092 *
3093 * Forced actions can cause execution delays and execution
3094 * rescheduling. The first we deal with using action priority, so
3095 * that for instance pending timers aren't scheduled and ran until
3096 * right before execution. The rescheduling we deal with using
3097 * return codes. The same goes for VM termination, only in that case
3098 * we exit everything.
3099 *
3100 * @returns VBox status code of equal or greater importance/severity than rc.
3101 * The most important ones are: VINF_EM_RESCHEDULE,
3102 * VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
3103 *
3104 * @param pVM VM handle.
3105 * @param rc The current rc.
3106 *
3107 */
3108static int emR3ForcedActions(PVM pVM, int rc)
3109{
3110 STAM_REL_PROFILE_START(&pVM->em.s.StatForcedActions, a);
3111#ifdef VBOX_STRICT
3112 int rcIrq = VINF_SUCCESS;
3113#endif
3114 int rc2;
3115#define UPDATE_RC() \
3116 do { \
3117 AssertMsg(rc2 <= 0 || (rc2 >= VINF_EM_FIRST && rc2 <= VINF_EM_LAST), ("Invalid FF return code: %Rra\n", rc2)); \
3118 if (rc2 == VINF_SUCCESS || rc < VINF_SUCCESS) \
3119 break; \
3120 if (!rc || rc2 < rc) \
3121 rc = rc2; \
3122 } while (0)
3123
3124 /*
3125 * Post execution chunk first.
3126 */
3127 if (VM_FF_ISPENDING(pVM, VM_FF_NORMAL_PRIORITY_POST_MASK))
3128 {
3129 /*
3130 * Termination request.
3131 */
3132 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
3133 {
3134 Log2(("emR3ForcedActions: returns VINF_EM_TERMINATE\n"));
3135 STAM_REL_PROFILE_STOP(&pVM->em.s.StatForcedActions, a);
3136 return VINF_EM_TERMINATE;
3137 }
3138
3139 /*
3140 * Debugger Facility polling.
3141 */
3142 if (VM_FF_ISSET(pVM, VM_FF_DBGF))
3143 {
3144 rc2 = DBGFR3VMMForcedAction(pVM);
3145 UPDATE_RC();
3146 }
3147
3148 /*
3149 * Postponed reset request.
3150 */
3151 if (VM_FF_ISSET(pVM, VM_FF_RESET))
3152 {
3153 rc2 = VMR3Reset(pVM);
3154 UPDATE_RC();
3155 VM_FF_CLEAR(pVM, VM_FF_RESET);
3156 }
3157
3158 /*
3159 * CSAM page scanning.
3160 */
3161 if (VM_FF_ISSET(pVM, VM_FF_CSAM_SCAN_PAGE))
3162 {
3163 PCPUMCTX pCtx = pVM->em.s.pCtx;
3164
3165 /** @todo: check for 16 or 32 bits code! (D bit in the code selector) */
3166 Log(("Forced action VM_FF_CSAM_SCAN_PAGE\n"));
3167
3168 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
3169 VM_FF_CLEAR(pVM, VM_FF_CSAM_SCAN_PAGE);
3170 }
3171
3172 /* check that we got them all */
3173 Assert(!(VM_FF_NORMAL_PRIORITY_POST_MASK & ~(VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_CSAM_SCAN_PAGE)));
3174 }
3175
3176 /*
3177 * Normal priority then.
3178 * (Executed in no particular order.)
3179 */
3180 if (VM_FF_ISPENDING(pVM, VM_FF_NORMAL_PRIORITY_MASK))
3181 {
3182 /*
3183 * PDM Queues are pending.
3184 */
3185 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
3186 PDMR3QueueFlushAll(pVM);
3187
3188 /*
3189 * PDM DMA transfers are pending.
3190 */
3191 if (VM_FF_ISSET(pVM, VM_FF_PDM_DMA))
3192 PDMR3DmaRun(pVM);
3193
3194 /*
3195 * Requests from other threads.
3196 */
3197 if (VM_FF_ISSET(pVM, VM_FF_REQUEST))
3198 {
3199 rc2 = VMR3ReqProcessU(pVM->pUVM, VMREQDEST_ANY);
3200 if (rc2 == VINF_EM_OFF || rc2 == VINF_EM_TERMINATE)
3201 {
3202 Log2(("emR3ForcedActions: returns %Rrc\n", rc2));
3203 STAM_REL_PROFILE_STOP(&pVM->em.s.StatForcedActions, a);
3204 return rc2;
3205 }
3206 UPDATE_RC();
3207 }
3208
3209 /* Replay the handler notification changes. */
3210 if (VM_FF_ISSET(pVM, VM_FF_REM_HANDLER_NOTIFY))
3211 REMR3ReplayHandlerNotifications(pVM);
3212
3213 /* check that we got them all */
3214 Assert(!(VM_FF_NORMAL_PRIORITY_MASK & ~(VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_REM_HANDLER_NOTIFY)));
3215 }
3216
3217 /*
3218 * Execute polling function ever so often.
3219 * THIS IS A HACK, IT WILL BE *REPLACED* BY PROPER ASYNC NETWORKING "SOON"!
3220 */
3221 static unsigned cLast = 0;
3222 if (!((++cLast) % 4))
3223 PDMR3Poll(pVM);
3224
3225 /*
3226 * High priority pre execution chunk last.
3227 * (Executed in ascending priority order.)
3228 */
3229 if (VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_MASK))
3230 {
3231 /*
3232 * Timers before interrupts.
3233 */
3234 if (VM_FF_ISSET(pVM, VM_FF_TIMER))
3235 TMR3TimerQueuesDo(pVM);
3236
3237 /*
3238 * The instruction following an emulated STI should *always* be executed!
3239 */
3240 if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
3241 {
3242 Log(("VM_FF_EMULATED_STI at %RGv successor %RGv\n", (RTGCPTR)CPUMGetGuestRIP(pVM), EMGetInhibitInterruptsPC(pVM)));
3243 if (CPUMGetGuestEIP(pVM) != EMGetInhibitInterruptsPC(pVM))
3244 {
3245 /* Note: we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here if the eip is the same as the inhibited instr address.
3246 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
3247 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
3248 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
3249 */
3250 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
3251 }
3252 if (HWACCMR3IsActive(pVM))
3253 rc2 = VINF_EM_RESCHEDULE_HWACC;
3254 else
3255 rc2 = PATMAreInterruptsEnabled(pVM) ? VINF_EM_RESCHEDULE_RAW : VINF_EM_RESCHEDULE_REM;
3256
3257 UPDATE_RC();
3258 }
3259
3260 /*
3261 * Interrupts.
3262 */
3263 if ( !VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS)
3264 && (!rc || rc >= VINF_EM_RESCHEDULE_HWACC)
3265 && !TRPMHasTrap(pVM) /* an interrupt could already be scheduled for dispatching in the recompiler. */
3266 && PATMAreInterruptsEnabled(pVM)
3267 && !HWACCMR3IsEventPending(pVM))
3268 {
3269 if (VM_FF_ISPENDING(pVM, VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC))
3270 {
3271 /* Note: it's important to make sure the return code from TRPMR3InjectEvent isn't ignored! */
3272 /** @todo this really isn't nice, should properly handle this */
3273 rc2 = TRPMR3InjectEvent(pVM, TRPM_HARDWARE_INT);
3274#ifdef VBOX_STRICT
3275 rcIrq = rc2;
3276#endif
3277 UPDATE_RC();
3278 }
3279 /** @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. */
3280 else if (REMR3QueryPendingInterrupt(pVM) != REM_NO_PENDING_IRQ)
3281 {
3282 rc2 = VINF_EM_RESCHEDULE_REM;
3283 UPDATE_RC();
3284 }
3285 }
3286
3287 /*
3288 * Allocate handy pages.
3289 */
3290 if (VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
3291 {
3292 rc2 = PGMR3PhysAllocateHandyPages(pVM);
3293 UPDATE_RC();
3294 }
3295
3296 /*
3297 * Debugger Facility request.
3298 */
3299 if (VM_FF_ISSET(pVM, VM_FF_DBGF))
3300 {
3301 rc2 = DBGFR3VMMForcedAction(pVM);
3302 UPDATE_RC();
3303 }
3304
3305 /*
3306 * Termination request.
3307 */
3308 if (VM_FF_ISSET(pVM, VM_FF_TERMINATE))
3309 {
3310 Log2(("emR3ForcedActions: returns VINF_EM_TERMINATE\n"));
3311 STAM_REL_PROFILE_STOP(&pVM->em.s.StatForcedActions, a);
3312 return VINF_EM_TERMINATE;
3313 }
3314
3315#ifdef DEBUG
3316 /*
3317 * Debug, pause the VM.
3318 */
3319 if (VM_FF_ISSET(pVM, VM_FF_DEBUG_SUSPEND))
3320 {
3321 VM_FF_CLEAR(pVM, VM_FF_DEBUG_SUSPEND);
3322 Log(("emR3ForcedActions: returns VINF_EM_SUSPEND\n"));
3323 return VINF_EM_SUSPEND;
3324 }
3325
3326#endif
3327 /* check that we got them all */
3328 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)));
3329 }
3330
3331#undef UPDATE_RC
3332 Log2(("emR3ForcedActions: returns %Rrc\n", rc));
3333 STAM_REL_PROFILE_STOP(&pVM->em.s.StatForcedActions, a);
3334 Assert(rcIrq == VINF_SUCCESS || rcIrq == rc);
3335 return rc;
3336}
3337
3338
3339/**
3340 * Execute VM.
3341 *
3342 * This function is the main loop of the VM. The emulation thread
3343 * calls this function when the VM has been successfully constructed
3344 * and we're ready for executing the VM.
3345 *
3346 * Returning from this function means that the VM is turned off or
3347 * suspended (state already saved) and deconstruction in next in line.
3348 *
3349 * All interaction from other thread are done using forced actions
3350 * and signaling of the wait object.
3351 *
3352 * @returns VBox status code, informational status codes may indicate failure.
3353 * @param pVM The VM to operate on.
3354 * @param idCpu VMCPU id.
3355 */
3356VMMR3DECL(int) EMR3ExecuteVM(PVM pVM, RTCPUID idCpu)
3357{
3358 LogFlow(("EMR3ExecuteVM: pVM=%p enmVMState=%d enmState=%d (%s) fForceRAW=%d\n", pVM, pVM->enmVMState,
3359 pVM->em.s.enmState, EMR3GetStateName(pVM->em.s.enmState), pVM->em.s.fForceRAW));
3360 VM_ASSERT_EMT(pVM);
3361 Assert(pVM->em.s.enmState == EMSTATE_NONE || pVM->em.s.enmState == EMSTATE_SUSPENDED);
3362
3363 VMMR3Lock(pVM);
3364
3365 int rc = setjmp(pVM->em.s.u.FatalLongJump);
3366 if (rc == 0)
3367 {
3368 /*
3369 * Start the virtual time.
3370 */
3371 rc = TMVirtualResume(pVM);
3372 Assert(rc == VINF_SUCCESS);
3373 rc = TMCpuTickResume(pVM);
3374 Assert(rc == VINF_SUCCESS);
3375
3376 /*
3377 * The Outer Main Loop.
3378 */
3379 bool fFFDone = false;
3380
3381 /* Reschedule right away to start in the right state. */
3382 rc = VINF_SUCCESS;
3383 pVM->em.s.enmState = emR3Reschedule(pVM, pVM->em.s.pCtx);
3384
3385 STAM_REL_PROFILE_ADV_START(&pVM->em.s.StatTotal, x);
3386 for (;;)
3387 {
3388 /*
3389 * Before we can schedule anything (we're here because
3390 * scheduling is required) we must service any pending
3391 * forced actions to avoid any pending action causing
3392 * immediate rescheduling upon entering an inner loop
3393 *
3394 * Do forced actions.
3395 */
3396 if ( !fFFDone
3397 && rc != VINF_EM_TERMINATE
3398 && rc != VINF_EM_OFF
3399 && VM_FF_ISPENDING(pVM, VM_FF_ALL_BUT_RAW_MASK))
3400 {
3401 rc = emR3ForcedActions(pVM, rc);
3402 if ( ( rc == VINF_EM_RESCHEDULE_REM
3403 || rc == VINF_EM_RESCHEDULE_HWACC)
3404 && pVM->em.s.fForceRAW)
3405 rc = VINF_EM_RESCHEDULE_RAW;
3406 }
3407 else if (fFFDone)
3408 fFFDone = false;
3409
3410 /*
3411 * Now what to do?
3412 */
3413 Log2(("EMR3ExecuteVM: rc=%Rrc\n", rc));
3414 switch (rc)
3415 {
3416 /*
3417 * Keep doing what we're currently doing.
3418 */
3419 case VINF_SUCCESS:
3420 break;
3421
3422 /*
3423 * Reschedule - to raw-mode execution.
3424 */
3425 case VINF_EM_RESCHEDULE_RAW:
3426 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_RAW: %d -> %d (EMSTATE_RAW)\n", pVM->em.s.enmState, EMSTATE_RAW));
3427 pVM->em.s.enmState = EMSTATE_RAW;
3428 break;
3429
3430 /*
3431 * Reschedule - to hardware accelerated raw-mode execution.
3432 */
3433 case VINF_EM_RESCHEDULE_HWACC:
3434 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_HWACC: %d -> %d (EMSTATE_HWACC)\n", pVM->em.s.enmState, EMSTATE_HWACC));
3435 Assert(!pVM->em.s.fForceRAW);
3436 pVM->em.s.enmState = EMSTATE_HWACC;
3437 break;
3438
3439 /*
3440 * Reschedule - to recompiled execution.
3441 */
3442 case VINF_EM_RESCHEDULE_REM:
3443 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_REM: %d -> %d (EMSTATE_REM)\n", pVM->em.s.enmState, EMSTATE_REM));
3444 pVM->em.s.enmState = EMSTATE_REM;
3445 break;
3446
3447#ifdef VBOX_WITH_VMI
3448 /*
3449 * Reschedule - parav call.
3450 */
3451 case VINF_EM_RESCHEDULE_PARAV:
3452 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE_PARAV: %d -> %d (EMSTATE_PARAV)\n", pVM->em.s.enmState, EMSTATE_PARAV));
3453 pVM->em.s.enmState = EMSTATE_PARAV;
3454 break;
3455#endif
3456
3457 /*
3458 * Resume.
3459 */
3460 case VINF_EM_RESUME:
3461 Log2(("EMR3ExecuteVM: VINF_EM_RESUME: %d -> VINF_EM_RESCHEDULE\n", pVM->em.s.enmState));
3462 /* fall through and get scheduled. */
3463
3464 /*
3465 * Reschedule.
3466 */
3467 case VINF_EM_RESCHEDULE:
3468 {
3469 EMSTATE enmState = emR3Reschedule(pVM, pVM->em.s.pCtx);
3470 Log2(("EMR3ExecuteVM: VINF_EM_RESCHEDULE: %d -> %d (%s)\n", pVM->em.s.enmState, enmState, EMR3GetStateName(enmState)));
3471 pVM->em.s.enmState = enmState;
3472 break;
3473 }
3474
3475 /*
3476 * Halted.
3477 */
3478 case VINF_EM_HALT:
3479 Log2(("EMR3ExecuteVM: VINF_EM_HALT: %d -> %d\n", pVM->em.s.enmState, EMSTATE_HALTED));
3480 pVM->em.s.enmState = EMSTATE_HALTED;
3481 break;
3482
3483 /*
3484 * Suspend.
3485 */
3486 case VINF_EM_SUSPEND:
3487 Log2(("EMR3ExecuteVM: VINF_EM_SUSPEND: %d -> %d\n", pVM->em.s.enmState, EMSTATE_SUSPENDED));
3488 pVM->em.s.enmState = EMSTATE_SUSPENDED;
3489 break;
3490
3491 /*
3492 * Reset.
3493 * We might end up doing a double reset for now, we'll have to clean up the mess later.
3494 */
3495 case VINF_EM_RESET:
3496 {
3497 EMSTATE enmState = emR3Reschedule(pVM, pVM->em.s.pCtx);
3498 Log2(("EMR3ExecuteVM: VINF_EM_RESET: %d -> %d (%s)\n", pVM->em.s.enmState, enmState, EMR3GetStateName(enmState)));
3499 pVM->em.s.enmState = enmState;
3500 break;
3501 }
3502
3503 /*
3504 * Power Off.
3505 */
3506 case VINF_EM_OFF:
3507 pVM->em.s.enmState = EMSTATE_TERMINATING;
3508 Log2(("EMR3ExecuteVM: returns VINF_EM_OFF (%d -> %d)\n", pVM->em.s.enmState, EMSTATE_TERMINATING));
3509 TMVirtualPause(pVM);
3510 TMCpuTickPause(pVM);
3511 VMMR3Unlock(pVM);
3512 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3513 return rc;
3514
3515 /*
3516 * Terminate the VM.
3517 */
3518 case VINF_EM_TERMINATE:
3519 pVM->em.s.enmState = EMSTATE_TERMINATING;
3520 Log(("EMR3ExecuteVM returns VINF_EM_TERMINATE (%d -> %d)\n", pVM->em.s.enmState, EMSTATE_TERMINATING));
3521 TMVirtualPause(pVM);
3522 TMCpuTickPause(pVM);
3523 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3524 return rc;
3525
3526 /*
3527 * Guest debug events.
3528 */
3529 case VINF_EM_DBG_STEPPED:
3530 AssertMsgFailed(("VINF_EM_DBG_STEPPED cannot be here!"));
3531 case VINF_EM_DBG_STOP:
3532 case VINF_EM_DBG_BREAKPOINT:
3533 case VINF_EM_DBG_STEP:
3534 if (pVM->em.s.enmState == EMSTATE_RAW)
3535 {
3536 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVM->em.s.enmState, EMSTATE_DEBUG_GUEST_RAW));
3537 pVM->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
3538 }
3539 else
3540 {
3541 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVM->em.s.enmState, EMSTATE_DEBUG_GUEST_REM));
3542 pVM->em.s.enmState = EMSTATE_DEBUG_GUEST_REM;
3543 }
3544 break;
3545
3546 /*
3547 * Hypervisor debug events.
3548 */
3549 case VINF_EM_DBG_HYPER_STEPPED:
3550 case VINF_EM_DBG_HYPER_BREAKPOINT:
3551 case VINF_EM_DBG_HYPER_ASSERTION:
3552 Log2(("EMR3ExecuteVM: %Rrc: %d -> %d\n", rc, pVM->em.s.enmState, EMSTATE_DEBUG_HYPER));
3553 pVM->em.s.enmState = EMSTATE_DEBUG_HYPER;
3554 break;
3555
3556 /*
3557 * Guru mediations.
3558 */
3559 case VERR_VMM_RING0_ASSERTION:
3560 Log(("EMR3ExecuteVM: %Rrc: %d -> %d (EMSTATE_GURU_MEDITATION)\n", rc, pVM->em.s.enmState, EMSTATE_GURU_MEDITATION));
3561 pVM->em.s.enmState = EMSTATE_GURU_MEDITATION;
3562 break;
3563
3564 /*
3565 * Any error code showing up here other than the ones we
3566 * know and process above are considered to be FATAL.
3567 *
3568 * Unknown warnings and informational status codes are also
3569 * included in this.
3570 */
3571 default:
3572 if (RT_SUCCESS(rc))
3573 {
3574 AssertMsgFailed(("Unexpected warning or informational status code %Rra!\n", rc));
3575 rc = VERR_EM_INTERNAL_ERROR;
3576 }
3577 pVM->em.s.enmState = EMSTATE_GURU_MEDITATION;
3578 Log(("EMR3ExecuteVM returns %d\n", rc));
3579 break;
3580 }
3581
3582
3583 /*
3584 * Any waiters can now be woken up
3585 */
3586 VMMR3Unlock(pVM);
3587 VMMR3Lock(pVM);
3588
3589 STAM_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x); /* (skip this in release) */
3590 STAM_PROFILE_ADV_START(&pVM->em.s.StatTotal, x);
3591
3592 /*
3593 * Act on the state.
3594 */
3595 switch (pVM->em.s.enmState)
3596 {
3597 /*
3598 * Execute raw.
3599 */
3600 case EMSTATE_RAW:
3601 rc = emR3RawExecute(pVM, &fFFDone);
3602 break;
3603
3604 /*
3605 * Execute hardware accelerated raw.
3606 */
3607 case EMSTATE_HWACC:
3608 rc = emR3HwAccExecute(pVM, idCpu, &fFFDone);
3609 break;
3610
3611 /*
3612 * Execute recompiled.
3613 */
3614 case EMSTATE_REM:
3615 rc = emR3RemExecute(pVM, &fFFDone);
3616 Log2(("EMR3ExecuteVM: emR3RemExecute -> %Rrc\n", rc));
3617 break;
3618
3619#ifdef VBOX_WITH_VMI
3620 /*
3621 * Execute PARAV function.
3622 */
3623 case EMSTATE_PARAV:
3624 rc = PARAVCallFunction(pVM);
3625 pVM->em.s.enmState = EMSTATE_REM;
3626 break;
3627#endif
3628
3629 /*
3630 * hlt - execution halted until interrupt.
3631 */
3632 case EMSTATE_HALTED:
3633 {
3634 STAM_REL_PROFILE_START(&pVM->em.s.StatHalted, y);
3635 rc = VMR3WaitHalted(pVM, !(CPUMGetGuestEFlags(pVM) & X86_EFL_IF));
3636 STAM_REL_PROFILE_STOP(&pVM->em.s.StatHalted, y);
3637 break;
3638 }
3639
3640 /*
3641 * Suspended - return to VM.cpp.
3642 */
3643 case EMSTATE_SUSPENDED:
3644 TMVirtualPause(pVM);
3645 TMCpuTickPause(pVM);
3646 VMMR3Unlock(pVM);
3647 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3648 return VINF_EM_SUSPEND;
3649
3650 /*
3651 * Debugging in the guest.
3652 */
3653 case EMSTATE_DEBUG_GUEST_REM:
3654 case EMSTATE_DEBUG_GUEST_RAW:
3655 TMVirtualPause(pVM);
3656 TMCpuTickPause(pVM);
3657 rc = emR3Debug(pVM, rc);
3658 TMVirtualResume(pVM);
3659 TMCpuTickResume(pVM);
3660 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVM->em.s.enmState));
3661 break;
3662
3663 /*
3664 * Debugging in the hypervisor.
3665 */
3666 case EMSTATE_DEBUG_HYPER:
3667 {
3668 TMVirtualPause(pVM);
3669 TMCpuTickPause(pVM);
3670 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3671
3672 rc = emR3Debug(pVM, rc);
3673 Log2(("EMR3ExecuteVM: enmr3Debug -> %Rrc (state %d)\n", rc, pVM->em.s.enmState));
3674 if (rc != VINF_SUCCESS)
3675 {
3676 /* switch to guru meditation mode */
3677 pVM->em.s.enmState = EMSTATE_GURU_MEDITATION;
3678 VMMR3FatalDump(pVM, rc);
3679 return rc;
3680 }
3681
3682 STAM_REL_PROFILE_ADV_START(&pVM->em.s.StatTotal, x);
3683 TMVirtualResume(pVM);
3684 TMCpuTickResume(pVM);
3685 break;
3686 }
3687
3688 /*
3689 * Guru meditation takes place in the debugger.
3690 */
3691 case EMSTATE_GURU_MEDITATION:
3692 {
3693 TMVirtualPause(pVM);
3694 TMCpuTickPause(pVM);
3695 VMMR3FatalDump(pVM, rc);
3696 emR3Debug(pVM, rc);
3697 VMMR3Unlock(pVM);
3698 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3699 return rc;
3700 }
3701
3702 /*
3703 * The states we don't expect here.
3704 */
3705 case EMSTATE_NONE:
3706 case EMSTATE_TERMINATING:
3707 default:
3708 AssertMsgFailed(("EMR3ExecuteVM: Invalid state %d!\n", pVM->em.s.enmState));
3709 pVM->em.s.enmState = EMSTATE_GURU_MEDITATION;
3710 TMVirtualPause(pVM);
3711 TMCpuTickPause(pVM);
3712 VMMR3Unlock(pVM);
3713 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3714 return VERR_EM_INTERNAL_ERROR;
3715 }
3716 } /* The Outer Main Loop */
3717 }
3718 else
3719 {
3720 /*
3721 * Fatal error.
3722 */
3723 LogFlow(("EMR3ExecuteVM: returns %Rrc (longjmp / fatal error)\n", rc));
3724 TMVirtualPause(pVM);
3725 TMCpuTickPause(pVM);
3726 VMMR3FatalDump(pVM, rc);
3727 emR3Debug(pVM, rc);
3728 VMMR3Unlock(pVM);
3729 STAM_REL_PROFILE_ADV_STOP(&pVM->em.s.StatTotal, x);
3730 /** @todo change the VM state! */
3731 return rc;
3732 }
3733
3734 /* (won't ever get here). */
3735 AssertFailed();
3736}
3737
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