VirtualBox

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

Last change on this file since 8042 was 7996, checked in by vboxsync, 17 years ago

Relax tests for PAE (rem vs raw)

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette