VirtualBox

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

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

EM: burn fix.

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