VirtualBox

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

Last change on this file since 8778 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

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