1 | /* $Id: EMHM.cpp 44340 2013-01-23 16:20:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Execution Monitor / Manager - hardware virtualization
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_EM
|
---|
22 | #include <VBox/vmm/em.h>
|
---|
23 | #include <VBox/vmm/vmm.h>
|
---|
24 | #include <VBox/vmm/csam.h>
|
---|
25 | #include <VBox/vmm/selm.h>
|
---|
26 | #include <VBox/vmm/trpm.h>
|
---|
27 | #include <VBox/vmm/iem.h>
|
---|
28 | #include <VBox/vmm/iom.h>
|
---|
29 | #include <VBox/vmm/dbgf.h>
|
---|
30 | #include <VBox/vmm/pgm.h>
|
---|
31 | #ifdef VBOX_WITH_REM
|
---|
32 | # include <VBox/vmm/rem.h>
|
---|
33 | #endif
|
---|
34 | #include <VBox/vmm/tm.h>
|
---|
35 | #include <VBox/vmm/mm.h>
|
---|
36 | #include <VBox/vmm/ssm.h>
|
---|
37 | #include <VBox/vmm/pdmapi.h>
|
---|
38 | #include <VBox/vmm/pdmcritsect.h>
|
---|
39 | #include <VBox/vmm/pdmqueue.h>
|
---|
40 | #include <VBox/vmm/hm.h>
|
---|
41 | #include "EMInternal.h"
|
---|
42 | #include <VBox/vmm/vm.h>
|
---|
43 | #include <VBox/vmm/cpumdis.h>
|
---|
44 | #include <VBox/dis.h>
|
---|
45 | #include <VBox/disopcode.h>
|
---|
46 | #include <VBox/vmm/dbgf.h>
|
---|
47 | #include "VMMTracing.h"
|
---|
48 |
|
---|
49 | #include <iprt/asm.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | /*******************************************************************************
|
---|
53 | * Defined Constants And Macros *
|
---|
54 | *******************************************************************************/
|
---|
55 | #if 0 /* Disabled till after 2.1.0 when we've time to test it. */
|
---|
56 | #define EM_NOTIFY_HM
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | /*******************************************************************************
|
---|
61 | * Internal Functions *
|
---|
62 | *******************************************************************************/
|
---|
63 | DECLINLINE(int) emR3ExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC = VINF_SUCCESS);
|
---|
64 | static int emR3ExecuteIOInstruction(PVM pVM, PVMCPU pVCpu);
|
---|
65 | static int emR3HmForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
66 |
|
---|
67 | #define EMHANDLERC_WITH_HM
|
---|
68 | #include "EMHandleRCTmpl.h"
|
---|
69 |
|
---|
70 |
|
---|
71 | #if defined(DEBUG) && defined(SOME_UNUSED_FUNCTIONS)
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Steps hardware accelerated mode.
|
---|
75 | *
|
---|
76 | * @returns VBox status code.
|
---|
77 | * @param pVM Pointer to the VM.
|
---|
78 | * @param pVCpu Pointer to the VMCPU.
|
---|
79 | */
|
---|
80 | static int emR3HmStep(PVM pVM, PVMCPU pVCpu)
|
---|
81 | {
|
---|
82 | Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HM);
|
---|
83 |
|
---|
84 | int rc;
|
---|
85 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
86 | VMCPU_FF_CLEAR(pVCpu, (VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_TSS));
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Check vital forced actions, but ignore pending interrupts and timers.
|
---|
90 | */
|
---|
91 | if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
|
---|
92 | || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
93 | {
|
---|
94 | rc = emR3HmForcedActions(pVM, pVCpu, pCtx);
|
---|
95 | if (rc != VINF_SUCCESS)
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 | /*
|
---|
99 | * Set flags for single stepping.
|
---|
100 | */
|
---|
101 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Single step.
|
---|
105 | * We do not start time or anything, if anything we should just do a few nanoseconds.
|
---|
106 | */
|
---|
107 | do
|
---|
108 | {
|
---|
109 | rc = VMMR3HmRunGC(pVM, pVCpu);
|
---|
110 | } while ( rc == VINF_SUCCESS
|
---|
111 | || rc == VINF_EM_RAW_INTERRUPT);
|
---|
112 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Make sure the trap flag is cleared.
|
---|
116 | * (Too bad if the guest is trying to single step too.)
|
---|
117 | */
|
---|
118 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Deal with the return codes.
|
---|
122 | */
|
---|
123 | rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
|
---|
124 | rc = emR3HmHandleRC(pVM, pVCpu, pCtx, rc);
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | static int emR3SingleStepExecHm(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
|
---|
130 | {
|
---|
131 | int rc = VINF_SUCCESS;
|
---|
132 | EMSTATE enmOldState = pVCpu->em.s.enmState;
|
---|
133 | pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_HM;
|
---|
134 |
|
---|
135 | Log(("Single step BEGIN:\n"));
|
---|
136 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
137 | {
|
---|
138 | DBGFR3PrgStep(pVCpu);
|
---|
139 | DBGFR3DisasInstrCurrentLog(pVCpu, "RSS: ");
|
---|
140 | rc = emR3HmStep(pVM, pVCpu);
|
---|
141 | if ( rc != VINF_SUCCESS
|
---|
142 | || !HMR3CanExecuteGuest(pVM, pVCpu->em.s.pCtx))
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | Log(("Single step END: rc=%Rrc\n", rc));
|
---|
146 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
|
---|
147 | pVCpu->em.s.enmState = enmOldState;
|
---|
148 | return rc == VINF_SUCCESS ? VINF_EM_RESCHEDULE_REM : rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | #endif /* DEBUG */
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Executes one (or perhaps a few more) instruction(s).
|
---|
156 | *
|
---|
157 | * @returns VBox status code suitable for EM.
|
---|
158 | *
|
---|
159 | * @param pVM Pointer to the VM.
|
---|
160 | * @param pVCpu Pointer to the VMCPU.
|
---|
161 | * @param rcRC Return code from RC.
|
---|
162 | * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
|
---|
163 | * instruction and prefix the log output with this text.
|
---|
164 | */
|
---|
165 | #ifdef LOG_ENABLED
|
---|
166 | static int emR3ExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcRC, const char *pszPrefix)
|
---|
167 | #else
|
---|
168 | static int emR3ExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcRC)
|
---|
169 | #endif
|
---|
170 | {
|
---|
171 | #ifdef LOG_ENABLED
|
---|
172 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
173 | #endif
|
---|
174 | int rc;
|
---|
175 | NOREF(rcRC);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | *
|
---|
179 | * The simple solution is to use the recompiler.
|
---|
180 | * The better solution is to disassemble the current instruction and
|
---|
181 | * try handle as many as possible without using REM.
|
---|
182 | *
|
---|
183 | */
|
---|
184 |
|
---|
185 | #ifdef LOG_ENABLED
|
---|
186 | /*
|
---|
187 | * Disassemble the instruction if requested.
|
---|
188 | */
|
---|
189 | if (pszPrefix)
|
---|
190 | {
|
---|
191 | DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
|
---|
192 | DBGFR3DisasInstrCurrentLog(pVCpu, pszPrefix);
|
---|
193 | }
|
---|
194 | #endif /* LOG_ENABLED */
|
---|
195 |
|
---|
196 | #if 0
|
---|
197 | /* Try our own instruction emulator before falling back to the recompiler. */
|
---|
198 | DISCPUSTATE Cpu;
|
---|
199 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "GEN EMU");
|
---|
200 | if (RT_SUCCESS(rc))
|
---|
201 | {
|
---|
202 | switch (Cpu.pCurInstr->uOpcode)
|
---|
203 | {
|
---|
204 | /* @todo we can do more now */
|
---|
205 | case OP_MOV:
|
---|
206 | case OP_AND:
|
---|
207 | case OP_OR:
|
---|
208 | case OP_XOR:
|
---|
209 | case OP_POP:
|
---|
210 | case OP_INC:
|
---|
211 | case OP_DEC:
|
---|
212 | case OP_XCHG:
|
---|
213 | STAM_PROFILE_START(&pVCpu->em.s.StatMiscEmu, a);
|
---|
214 | rc = EMInterpretInstructionCpuUpdtPC(pVM, pVCpu, &Cpu, CPUMCTX2CORE(pCtx), 0);
|
---|
215 | if (RT_SUCCESS(rc))
|
---|
216 | {
|
---|
217 | #ifdef EM_NOTIFY_HM
|
---|
218 | if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HM)
|
---|
219 | HMR3NotifyEmulated(pVCpu);
|
---|
220 | #endif
|
---|
221 | STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 | if (rc != VERR_EM_INTERPRETER)
|
---|
225 | AssertMsgFailedReturn(("rc=%Rrc\n", rc), rc);
|
---|
226 | STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | #endif /* 0 */
|
---|
231 | STAM_PROFILE_START(&pVCpu->em.s.StatREMEmu, a);
|
---|
232 | Log(("EMINS: %04x:%RGv RSP=%RGv\n", pCtx->cs.Sel, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
|
---|
233 | #ifdef VBOX_WITH_REM
|
---|
234 | EMRemLock(pVM);
|
---|
235 | /* Flush the recompiler TLB if the VCPU has changed. */
|
---|
236 | if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
|
---|
237 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
|
---|
238 | pVM->em.s.idLastRemCpu = pVCpu->idCpu;
|
---|
239 |
|
---|
240 | rc = REMR3EmulateInstruction(pVM, pVCpu);
|
---|
241 | EMRemUnlock(pVM);
|
---|
242 | #else
|
---|
243 | rc = VBOXSTRICTRC_TODO(IEMExecOne(pVCpu)); NOREF(pVM);
|
---|
244 | #endif
|
---|
245 | STAM_PROFILE_STOP(&pVCpu->em.s.StatREMEmu, a);
|
---|
246 |
|
---|
247 | #ifdef EM_NOTIFY_HM
|
---|
248 | if (pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_HM)
|
---|
249 | HMR3NotifyEmulated(pVCpu);
|
---|
250 | #endif
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Executes one (or perhaps a few more) instruction(s).
|
---|
257 | * This is just a wrapper for discarding pszPrefix in non-logging builds.
|
---|
258 | *
|
---|
259 | * @returns VBox status code suitable for EM.
|
---|
260 | * @param pVM Pointer to the VM.
|
---|
261 | * @param pVCpu Pointer to the VMCPU.
|
---|
262 | * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
|
---|
263 | * instruction and prefix the log output with this text.
|
---|
264 | * @param rcGC GC return code
|
---|
265 | */
|
---|
266 | DECLINLINE(int) emR3ExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC)
|
---|
267 | {
|
---|
268 | #ifdef LOG_ENABLED
|
---|
269 | return emR3ExecuteInstructionWorker(pVM, pVCpu, rcGC, pszPrefix);
|
---|
270 | #else
|
---|
271 | return emR3ExecuteInstructionWorker(pVM, pVCpu, rcGC);
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Executes one (or perhaps a few more) IO instruction(s).
|
---|
277 | *
|
---|
278 | * @returns VBox status code suitable for EM.
|
---|
279 | * @param pVM Pointer to the VM.
|
---|
280 | * @param pVCpu Pointer to the VMCPU.
|
---|
281 | */
|
---|
282 | static int emR3ExecuteIOInstruction(PVM pVM, PVMCPU pVCpu)
|
---|
283 | {
|
---|
284 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
285 |
|
---|
286 | STAM_PROFILE_START(&pVCpu->em.s.StatIOEmu, a);
|
---|
287 |
|
---|
288 | /* Try to restart the io instruction that was refused in ring-0. */
|
---|
289 | VBOXSTRICTRC rcStrict = HMR3RestartPendingIOInstr(pVM, pVCpu, pCtx);
|
---|
290 | if (IOM_SUCCESS(rcStrict))
|
---|
291 | {
|
---|
292 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIoRestarted);
|
---|
293 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
294 | return VBOXSTRICTRC_TODO(rcStrict); /* rip already updated. */
|
---|
295 | }
|
---|
296 | AssertMsgReturn(rcStrict == VERR_NOT_FOUND, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)),
|
---|
297 | RT_SUCCESS_NP(rcStrict) ? VERR_IPE_UNEXPECTED_INFO_STATUS : VBOXSTRICTRC_TODO(rcStrict));
|
---|
298 |
|
---|
299 | /** @todo probably we should fall back to the recompiler; otherwise we'll go back and forth between HC & GC
|
---|
300 | * as io instructions tend to come in packages of more than one
|
---|
301 | */
|
---|
302 | DISCPUSTATE Cpu;
|
---|
303 | int rc2 = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "IO EMU");
|
---|
304 | if (RT_SUCCESS(rc2))
|
---|
305 | {
|
---|
306 | rcStrict = VINF_EM_RAW_EMULATE_INSTR;
|
---|
307 |
|
---|
308 | if (!(Cpu.fPrefix & (DISPREFIX_REP | DISPREFIX_REPNE)))
|
---|
309 | {
|
---|
310 | switch (Cpu.pCurInstr->uOpcode)
|
---|
311 | {
|
---|
312 | case OP_IN:
|
---|
313 | {
|
---|
314 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
|
---|
315 | rcStrict = IOMInterpretIN(pVM, CPUMCTX2CORE(pCtx), &Cpu);
|
---|
316 | break;
|
---|
317 | }
|
---|
318 |
|
---|
319 | case OP_OUT:
|
---|
320 | {
|
---|
321 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
|
---|
322 | rcStrict = IOMInterpretOUT(pVM, CPUMCTX2CORE(pCtx), &Cpu);
|
---|
323 | break;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 | else if (Cpu.fPrefix & DISPREFIX_REP)
|
---|
328 | {
|
---|
329 | switch (Cpu.pCurInstr->uOpcode)
|
---|
330 | {
|
---|
331 | case OP_INSB:
|
---|
332 | case OP_INSWD:
|
---|
333 | {
|
---|
334 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
|
---|
335 | rcStrict = IOMInterpretINS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
|
---|
336 | break;
|
---|
337 | }
|
---|
338 |
|
---|
339 | case OP_OUTSB:
|
---|
340 | case OP_OUTSWD:
|
---|
341 | {
|
---|
342 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
|
---|
343 | rcStrict = IOMInterpretOUTS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
|
---|
344 | break;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Handled the I/O return codes.
|
---|
351 | * (The unhandled cases end up with rcStrict == VINF_EM_RAW_EMULATE_INSTR.)
|
---|
352 | */
|
---|
353 | if (IOM_SUCCESS(rcStrict))
|
---|
354 | {
|
---|
355 | pCtx->rip += Cpu.cbInstr;
|
---|
356 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
357 | return VBOXSTRICTRC_TODO(rcStrict);
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
|
---|
361 | {
|
---|
362 | /* The active trap will be dispatched. */
|
---|
363 | Assert(TRPMHasTrap(pVCpu));
|
---|
364 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
365 | return VINF_SUCCESS;
|
---|
366 | }
|
---|
367 | AssertMsg(rcStrict != VINF_TRPM_XCPT_DISPATCHED, ("Handle VINF_TRPM_XCPT_DISPATCHED\n"));
|
---|
368 |
|
---|
369 | if (RT_FAILURE(rcStrict))
|
---|
370 | {
|
---|
371 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
372 | return VBOXSTRICTRC_TODO(rcStrict);
|
---|
373 | }
|
---|
374 | AssertMsg(rcStrict == VINF_EM_RAW_EMULATE_INSTR || rcStrict == VINF_EM_RESCHEDULE_REM, ("rcStrict=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
375 | }
|
---|
376 |
|
---|
377 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
378 | return emR3ExecuteInstruction(pVM, pVCpu, "IO: ");
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Process raw-mode specific forced actions.
|
---|
384 | *
|
---|
385 | * This function is called when any FFs in the VM_FF_HIGH_PRIORITY_PRE_RAW_MASK is pending.
|
---|
386 | *
|
---|
387 | * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
|
---|
388 | * EM statuses.
|
---|
389 | * @param pVM Pointer to the VM.
|
---|
390 | * @param pVCpu Pointer to the VMCPU.
|
---|
391 | * @param pCtx Pointer to the guest CPU context.
|
---|
392 | */
|
---|
393 | static int emR3HmForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
|
---|
394 | {
|
---|
395 | /*
|
---|
396 | * Sync page directory.
|
---|
397 | */
|
---|
398 | if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
|
---|
399 | {
|
---|
400 | Assert(pVCpu->em.s.enmState != EMSTATE_WAIT_SIPI);
|
---|
401 | int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
402 | if (RT_FAILURE(rc))
|
---|
403 | return rc;
|
---|
404 |
|
---|
405 | Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
|
---|
406 |
|
---|
407 | /* Prefetch pages for EIP and ESP. */
|
---|
408 | /** @todo This is rather expensive. Should investigate if it really helps at all. */
|
---|
409 | rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DISSELREG_CS, CPUMCTX2CORE(pCtx), pCtx->rip));
|
---|
410 | if (rc == VINF_SUCCESS)
|
---|
411 | rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DISSELREG_SS, CPUMCTX2CORE(pCtx), pCtx->rsp));
|
---|
412 | if (rc != VINF_SUCCESS)
|
---|
413 | {
|
---|
414 | if (rc != VINF_PGM_SYNC_CR3)
|
---|
415 | {
|
---|
416 | AssertLogRelMsgReturn(RT_FAILURE(rc), ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
417 | return rc;
|
---|
418 | }
|
---|
419 | rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
420 | if (RT_FAILURE(rc))
|
---|
421 | return rc;
|
---|
422 | }
|
---|
423 | /** @todo maybe prefetch the supervisor stack page as well */
|
---|
424 | Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
|
---|
425 | }
|
---|
426 |
|
---|
427 | /*
|
---|
428 | * Allocate handy pages (just in case the above actions have consumed some pages).
|
---|
429 | */
|
---|
430 | if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
|
---|
431 | {
|
---|
432 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
433 | if (RT_FAILURE(rc))
|
---|
434 | return rc;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Check whether we're out of memory now.
|
---|
439 | *
|
---|
440 | * This may stem from some of the above actions or operations that has been executed
|
---|
441 | * since we ran FFs. The allocate handy pages must for instance always be followed by
|
---|
442 | * this check.
|
---|
443 | */
|
---|
444 | if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
|
---|
445 | return VINF_EM_NO_MEMORY;
|
---|
446 |
|
---|
447 | return VINF_SUCCESS;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Executes hardware accelerated raw code. (Intel VT-x & AMD-V)
|
---|
453 | *
|
---|
454 | * This function contains the raw-mode version of the inner
|
---|
455 | * execution loop (the outer loop being in EMR3ExecuteVM()).
|
---|
456 | *
|
---|
457 | * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE, VINF_EM_RESCHEDULE_RAW,
|
---|
458 | * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
|
---|
459 | *
|
---|
460 | * @param pVM Pointer to the VM.
|
---|
461 | * @param pVCpu Pointer to the VMCPU.
|
---|
462 | * @param pfFFDone Where to store an indicator telling whether or not
|
---|
463 | * FFs were done before returning.
|
---|
464 | */
|
---|
465 | int emR3HmExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
|
---|
466 | {
|
---|
467 | int rc = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
468 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
469 |
|
---|
470 | LogFlow(("emR3HmExecute%d: (cs:eip=%04x:%RGv)\n", pVCpu->idCpu, pCtx->cs.Sel, (RTGCPTR)pCtx->rip));
|
---|
471 | *pfFFDone = false;
|
---|
472 |
|
---|
473 | STAM_COUNTER_INC(&pVCpu->em.s.StatHmExecuteEntry);
|
---|
474 |
|
---|
475 | #ifdef EM_NOTIFY_HM
|
---|
476 | HMR3NotifyScheduled(pVCpu);
|
---|
477 | #endif
|
---|
478 |
|
---|
479 | /*
|
---|
480 | * Spin till we get a forced action which returns anything but VINF_SUCCESS.
|
---|
481 | */
|
---|
482 | for (;;)
|
---|
483 | {
|
---|
484 | STAM_PROFILE_ADV_START(&pVCpu->em.s.StatHmEntry, a);
|
---|
485 |
|
---|
486 | /* Check if a forced reschedule is pending. */
|
---|
487 | if (HMR3IsRescheduleRequired(pVM, pCtx))
|
---|
488 | {
|
---|
489 | rc = VINF_EM_RESCHEDULE;
|
---|
490 | break;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * Process high priority pre-execution raw-mode FFs.
|
---|
495 | */
|
---|
496 | VMCPU_FF_CLEAR(pVCpu, (VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_TSS)); /* not relevant in HM mode; shouldn't be set really. */
|
---|
497 | if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
|
---|
498 | || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
499 | {
|
---|
500 | rc = emR3HmForcedActions(pVM, pVCpu, pCtx);
|
---|
501 | if (rc != VINF_SUCCESS)
|
---|
502 | break;
|
---|
503 | }
|
---|
504 |
|
---|
505 | #ifdef LOG_ENABLED
|
---|
506 | /*
|
---|
507 | * Log important stuff before entering GC.
|
---|
508 | */
|
---|
509 | if (TRPMHasTrap(pVCpu))
|
---|
510 | Log(("CPU%d: Pending hardware interrupt=0x%x cs:rip=%04X:%RGv\n", pVCpu->idCpu, TRPMGetTrapNo(pVCpu), pCtx->cs.Sel, (RTGCPTR)pCtx->rip));
|
---|
511 |
|
---|
512 | uint32_t cpl = CPUMGetGuestCPL(pVCpu);
|
---|
513 |
|
---|
514 | if (pVM->cCpus == 1)
|
---|
515 | {
|
---|
516 | if (pCtx->eflags.Bits.u1VM)
|
---|
517 | Log(("HWV86: %08X IF=%d\n", pCtx->eip, pCtx->eflags.Bits.u1IF));
|
---|
518 | else if (CPUMIsGuestIn64BitCodeEx(pCtx))
|
---|
519 | Log(("HWR%d: %04X:%RGv ESP=%RGv IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs.Sel, (RTGCPTR)pCtx->rip, pCtx->rsp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
|
---|
520 | else
|
---|
521 | Log(("HWR%d: %04X:%08X ESP=%08X IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pCtx->cs.Sel, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
|
---|
522 | }
|
---|
523 | else
|
---|
524 | {
|
---|
525 | if (pCtx->eflags.Bits.u1VM)
|
---|
526 | Log(("HWV86-CPU%d: %08X IF=%d\n", pVCpu->idCpu, pCtx->eip, pCtx->eflags.Bits.u1IF));
|
---|
527 | else if (CPUMIsGuestIn64BitCodeEx(pCtx))
|
---|
528 | Log(("HWR%d-CPU%d: %04X:%RGv ESP=%RGv IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pVCpu->idCpu, pCtx->cs.Sel, (RTGCPTR)pCtx->rip, pCtx->rsp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
|
---|
529 | else
|
---|
530 | Log(("HWR%d-CPU%d: %04X:%08X ESP=%08X IF=%d IOPL=%d CR0=%x CR4=%x EFER=%x\n", cpl, pVCpu->idCpu, pCtx->cs.Sel, pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pCtx->eflags.Bits.u2IOPL, (uint32_t)pCtx->cr0, (uint32_t)pCtx->cr4, (uint32_t)pCtx->msrEFER));
|
---|
531 | }
|
---|
532 | #endif /* LOG_ENABLED */
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Execute the code.
|
---|
536 | */
|
---|
537 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatHmEntry, a);
|
---|
538 |
|
---|
539 | if (RT_LIKELY(EMR3IsExecutionAllowed(pVM, pVCpu)))
|
---|
540 | {
|
---|
541 | STAM_PROFILE_START(&pVCpu->em.s.StatHmExec, x);
|
---|
542 | rc = VMMR3HmRunGC(pVM, pVCpu);
|
---|
543 | STAM_PROFILE_STOP(&pVCpu->em.s.StatHmExec, x);
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | /* Give up this time slice; virtual time continues */
|
---|
548 | STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatCapped, u);
|
---|
549 | RTThreadSleep(5);
|
---|
550 | STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatCapped, u);
|
---|
551 | rc = VINF_SUCCESS;
|
---|
552 | }
|
---|
553 |
|
---|
554 |
|
---|
555 | /*
|
---|
556 | * Deal with high priority post execution FFs before doing anything else.
|
---|
557 | */
|
---|
558 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
|
---|
559 | if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
|
---|
560 | || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
|
---|
561 | rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Process the returned status code.
|
---|
565 | */
|
---|
566 | if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
|
---|
567 | break;
|
---|
568 |
|
---|
569 | rc = emR3HmHandleRC(pVM, pVCpu, pCtx, rc);
|
---|
570 | if (rc != VINF_SUCCESS)
|
---|
571 | break;
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Check and execute forced actions.
|
---|
575 | */
|
---|
576 | #ifdef VBOX_HIGH_RES_TIMERS_HACK
|
---|
577 | TMTimerPollVoid(pVM, pVCpu);
|
---|
578 | #endif
|
---|
579 | if ( VM_FF_ISPENDING(pVM, VM_FF_ALL_MASK)
|
---|
580 | || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_ALL_MASK))
|
---|
581 | {
|
---|
582 | rc = emR3ForcedActions(pVM, pVCpu, rc);
|
---|
583 | VBOXVMM_EM_FF_ALL_RET(pVCpu, rc);
|
---|
584 | if ( rc != VINF_SUCCESS
|
---|
585 | && rc != VINF_EM_RESCHEDULE_HM)
|
---|
586 | {
|
---|
587 | *pfFFDone = true;
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Return to outer loop.
|
---|
595 | */
|
---|
596 | #if defined(LOG_ENABLED) && defined(DEBUG)
|
---|
597 | RTLogFlush(NULL);
|
---|
598 | #endif
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|