VirtualBox

source: vbox/trunk/src/VBox/VMM/EMRaw.cpp@ 30441

Last change on this file since 30441 was 30263, checked in by vboxsync, 15 years ago

VMM,REM: Only invalidate hidden registers when using raw-mode. Fixes save restore during mode switching code like the windows boot menu. (#5057)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.4 KB
Line 
1/* $Id: EMRaw.cpp 30263 2010-06-16 18:31:42Z vboxsync $ */
2/** @file
3 * EM - Execution Monitor / Manager - software virtualization
4 */
5
6/*
7 * Copyright (C) 2006-2009 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/** @page pg_em EM - The Execution Monitor / Manager
19 *
20 * The Execution Monitor/Manager is responsible for running the VM, scheduling
21 * the right kind of execution (Raw-mode, Hardware Assisted, Recompiled or
22 * Interpreted), and keeping the CPU states in sync. The function
23 * EMR3ExecuteVM() is the 'main-loop' of the VM, while each of the execution
24 * modes has different inner loops (emR3RawExecute, emR3HwAccExecute, and
25 * emR3RemExecute).
26 *
27 * The interpreted execution is only used to avoid switching between
28 * raw-mode/hwaccm and the recompiler when fielding virtualization traps/faults.
29 * The interpretation is thus implemented as part of EM.
30 *
31 * @see grp_em
32 */
33
34/*******************************************************************************
35* Header Files *
36*******************************************************************************/
37#define LOG_GROUP LOG_GROUP_EM
38#include <VBox/em.h>
39#include <VBox/vmm.h>
40#include <VBox/patm.h>
41#include <VBox/csam.h>
42#include <VBox/selm.h>
43#include <VBox/trpm.h>
44#include <VBox/iom.h>
45#include <VBox/dbgf.h>
46#include <VBox/pgm.h>
47#include <VBox/rem.h>
48#include <VBox/tm.h>
49#include <VBox/mm.h>
50#include <VBox/ssm.h>
51#include <VBox/pdmapi.h>
52#include <VBox/pdmcritsect.h>
53#include <VBox/pdmqueue.h>
54#include <VBox/patm.h>
55#include "EMInternal.h"
56#include <VBox/vm.h>
57#include <VBox/cpumdis.h>
58#include <VBox/dis.h>
59#include <VBox/disopcode.h>
60#include <VBox/dbgf.h>
61
62#include <VBox/log.h>
63#include <iprt/asm.h>
64#include <iprt/string.h>
65#include <iprt/stream.h>
66
67
68/*******************************************************************************
69* Defined Constants And Macros *
70*******************************************************************************/
71
72
73/*******************************************************************************
74* Internal Functions *
75*******************************************************************************/
76static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
77DECLINLINE(int) emR3ExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC = VINF_SUCCESS);
78static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu);
79static int emR3PatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret);
80static int emR3SingleStepExecRem(PVM pVM, uint32_t cIterations);
81static int emR3RawPrivileged(PVM pVM, PVMCPU pVCpu);
82static int emR3ExecuteIOInstruction(PVM pVM, PVMCPU pVCpu);
83static int emR3RawRingSwitch(PVM pVM, PVMCPU pVCpu);
84
85#define EMHANDLERC_WITH_PATM
86#include "EMHandleRCTmpl.h"
87
88/**
89 * Enables or disables a set of raw-mode execution modes.
90 *
91 * @returns VINF_SUCCESS on success.
92 * @returns VINF_RESCHEDULE if a rescheduling might be required.
93 * @returns VERR_INVALID_PARAMETER on an invalid enmMode value.
94 *
95 * @param pVM The VM to operate on.
96 * @param enmMode The execution mode change.
97 * @thread The emulation thread.
98 */
99VMMR3DECL(int) EMR3RawSetMode(PVM pVM, EMRAWMODE enmMode)
100{
101 switch (enmMode)
102 {
103 case EMRAW_NONE:
104 pVM->fRawR3Enabled = false;
105 pVM->fRawR0Enabled = false;
106 break;
107 case EMRAW_RING3_ENABLE:
108 pVM->fRawR3Enabled = true;
109 break;
110 case EMRAW_RING3_DISABLE:
111 pVM->fRawR3Enabled = false;
112 break;
113 case EMRAW_RING0_ENABLE:
114 pVM->fRawR0Enabled = true;
115 break;
116 case EMRAW_RING0_DISABLE:
117 pVM->fRawR0Enabled = false;
118 break;
119 default:
120 AssertMsgFailed(("Invalid enmMode=%d\n", enmMode));
121 return VERR_INVALID_PARAMETER;
122 }
123 Log(("EMR3SetRawMode: fRawR3Enabled=%RTbool fRawR0Enabled=%RTbool\n",
124 pVM->fRawR3Enabled, pVM->fRawR0Enabled));
125 return pVM->aCpus[0].em.s.enmState == EMSTATE_RAW ? VINF_EM_RESCHEDULE : VINF_SUCCESS;
126}
127
128
129
130#ifdef VBOX_WITH_STATISTICS
131/**
132 * Just a braindead function to keep track of cli addresses.
133 * @param pVM VM handle.
134 * @param pVMCPU VMCPU handle.
135 * @param GCPtrInstr The EIP of the cli instruction.
136 */
137static void emR3RecordCli(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtrInstr)
138{
139 PCLISTAT pRec;
140
141 pRec = (PCLISTAT)RTAvlGCPtrGet(&pVCpu->em.s.pCliStatTree, GCPtrInstr);
142 if (!pRec)
143 {
144 /* New cli instruction; insert into the tree. */
145 pRec = (PCLISTAT)MMR3HeapAllocZ(pVM, MM_TAG_EM, sizeof(*pRec));
146 Assert(pRec);
147 if (!pRec)
148 return;
149 pRec->Core.Key = GCPtrInstr;
150
151 char szCliStatName[32];
152 RTStrPrintf(szCliStatName, sizeof(szCliStatName), "/EM/Cli/0x%RGv", GCPtrInstr);
153 STAM_REG(pVM, &pRec->Counter, STAMTYPE_COUNTER, szCliStatName, STAMUNIT_OCCURENCES, "Number of times cli was executed.");
154
155 bool fRc = RTAvlGCPtrInsert(&pVCpu->em.s.pCliStatTree, &pRec->Core);
156 Assert(fRc); NOREF(fRc);
157 }
158 STAM_COUNTER_INC(&pRec->Counter);
159 STAM_COUNTER_INC(&pVCpu->em.s.StatTotalClis);
160}
161#endif /* VBOX_WITH_STATISTICS */
162
163
164
165/**
166 * Resumes executing hypervisor after a debug event.
167 *
168 * This is kind of special since our current guest state is
169 * potentially out of sync.
170 *
171 * @returns VBox status code.
172 * @param pVM The VM handle.
173 * @param pVCpu The VMCPU handle.
174 */
175int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu)
176{
177 int rc;
178 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
179 Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER);
180 Log(("emR3RawResumeHyper: cs:eip=%RTsel:%RGr efl=%RGr\n", pCtx->cs, pCtx->eip, pCtx->eflags));
181
182 /*
183 * Resume execution.
184 */
185 CPUMR3RawEnter(pVCpu, NULL);
186 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_RF);
187 rc = VMMR3ResumeHyper(pVM, pVCpu);
188 Log(("emR3RawResumeHyper: cs:eip=%RTsel:%RGr efl=%RGr - returned from GC with rc=%Rrc\n", pCtx->cs, pCtx->eip, pCtx->eflags, rc));
189 rc = CPUMR3RawLeave(pVCpu, NULL, rc);
190 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
191
192 /*
193 * Deal with the return code.
194 */
195 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
196 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
197 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
198 return rc;
199}
200
201
202/**
203 * Steps rawmode.
204 *
205 * @returns VBox status code.
206 * @param pVM The VM handle.
207 * @param pVCpu The VMCPU handle.
208 */
209int emR3RawStep(PVM pVM, PVMCPU pVCpu)
210{
211 Assert( pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER
212 || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_RAW
213 || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_REM);
214 int rc;
215 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
216 bool fGuest = pVCpu->em.s.enmState != EMSTATE_DEBUG_HYPER;
217#ifndef DEBUG_sandervl
218 Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
219 fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu)));
220#endif
221 if (fGuest)
222 {
223 /*
224 * Check vital forced actions, but ignore pending interrupts and timers.
225 */
226 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
227 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
228 {
229 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
230 if (rc != VINF_SUCCESS)
231 return rc;
232 }
233
234 /*
235 * Set flags for single stepping.
236 */
237 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
238 }
239 else
240 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
241
242 /*
243 * Single step.
244 * We do not start time or anything, if anything we should just do a few nanoseconds.
245 */
246 CPUMR3RawEnter(pVCpu, NULL);
247 do
248 {
249 if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
250 rc = VMMR3ResumeHyper(pVM, pVCpu);
251 else
252 rc = VMMR3RawRunGC(pVM, pVCpu);
253#ifndef DEBUG_sandervl
254 Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr - GC rc %Rrc\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
255 fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu), rc));
256#endif
257 } while ( rc == VINF_SUCCESS
258 || rc == VINF_EM_RAW_INTERRUPT);
259 rc = CPUMR3RawLeave(pVCpu, NULL, rc);
260 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
261
262 /*
263 * Make sure the trap flag is cleared.
264 * (Too bad if the guest is trying to single step too.)
265 */
266 if (fGuest)
267 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
268 else
269 CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) & ~X86_EFL_TF);
270
271 /*
272 * Deal with the return codes.
273 */
274 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
275 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
276 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
277 return rc;
278}
279
280
281#ifdef DEBUG
282
283
284int emR3SingleStepExecRaw(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
285{
286 int rc = VINF_SUCCESS;
287 EMSTATE enmOldState = pVCpu->em.s.enmState;
288 pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
289
290 Log(("Single step BEGIN:\n"));
291 for (uint32_t i = 0; i < cIterations; i++)
292 {
293 DBGFR3PrgStep(pVCpu);
294 DBGFR3DisasInstrCurrentLog(pVCpu, "RSS: ");
295 rc = emR3RawStep(pVM, pVCpu);
296 if (rc != VINF_SUCCESS)
297 break;
298 }
299 Log(("Single step END: rc=%Rrc\n", rc));
300 CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
301 pVCpu->em.s.enmState = enmOldState;
302 return rc;
303}
304
305#endif /* DEBUG */
306
307
308/**
309 * Executes one (or perhaps a few more) instruction(s).
310 *
311 * @returns VBox status code suitable for EM.
312 *
313 * @param pVM VM handle.
314 * @param pVCpu VMCPU handle
315 * @param rcGC GC return code
316 * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
317 * instruction and prefix the log output with this text.
318 */
319#ifdef LOG_ENABLED
320static int emR3ExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC, const char *pszPrefix)
321#else
322static int emR3ExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC)
323#endif
324{
325 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
326 int rc;
327
328 /*
329 *
330 * The simple solution is to use the recompiler.
331 * The better solution is to disassemble the current instruction and
332 * try handle as many as possible without using REM.
333 *
334 */
335
336#ifdef LOG_ENABLED
337 /*
338 * Disassemble the instruction if requested.
339 */
340 if (pszPrefix)
341 {
342 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
343 DBGFR3DisasInstrCurrentLog(pVCpu, pszPrefix);
344 }
345#endif /* LOG_ENABLED */
346
347 /*
348 * PATM is making life more interesting.
349 * We cannot hand anything to REM which has an EIP inside patch code. So, we'll
350 * tell PATM there is a trap in this code and have it take the appropriate actions
351 * to allow us execute the code in REM.
352 */
353 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
354 {
355 Log(("emR3ExecuteInstruction: In patch block. eip=%RRv\n", (RTRCPTR)pCtx->eip));
356
357 RTGCPTR pNewEip;
358 rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &pNewEip);
359 switch (rc)
360 {
361 /*
362 * It's not very useful to emulate a single instruction and then go back to raw
363 * mode; just execute the whole block until IF is set again.
364 */
365 case VINF_SUCCESS:
366 Log(("emR3ExecuteInstruction: Executing instruction starting at new address %RGv IF=%d VMIF=%x\n",
367 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
368 pCtx->eip = pNewEip;
369 Assert(pCtx->eip);
370
371 if (pCtx->eflags.Bits.u1IF)
372 {
373 /*
374 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
375 */
376 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
377 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIR");
378 }
379 else if (rcGC == VINF_PATM_PENDING_IRQ_AFTER_IRET)
380 {
381 /* special case: iret, that sets IF, detected a pending irq/event */
382 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIRET");
383 }
384 return VINF_EM_RESCHEDULE_REM;
385
386 /*
387 * One instruction.
388 */
389 case VINF_PATCH_EMULATE_INSTR:
390 Log(("emR3ExecuteInstruction: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
391 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
392 pCtx->eip = pNewEip;
393 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIR");
394
395 /*
396 * The patch was disabled, hand it to the REM.
397 */
398 case VERR_PATCH_DISABLED:
399 Log(("emR3ExecuteInstruction: Disabled patch -> new eip %RGv IF=%d VMIF=%x\n",
400 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
401 pCtx->eip = pNewEip;
402 if (pCtx->eflags.Bits.u1IF)
403 {
404 /*
405 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
406 */
407 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
408 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIR");
409 }
410 return VINF_EM_RESCHEDULE_REM;
411
412 /* Force continued patch exection; usually due to write monitored stack. */
413 case VINF_PATCH_CONTINUE:
414 return VINF_SUCCESS;
415
416 default:
417 AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap\n", rc));
418 return VERR_IPE_UNEXPECTED_STATUS;
419 }
420 }
421
422#if 0
423 /* Try our own instruction emulator before falling back to the recompiler. */
424 DISCPUSTATE Cpu;
425 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "GEN EMU");
426 if (RT_SUCCESS(rc))
427 {
428 uint32_t size;
429
430 switch (Cpu.pCurInstr->opcode)
431 {
432 /* @todo we can do more now */
433 case OP_MOV:
434 case OP_AND:
435 case OP_OR:
436 case OP_XOR:
437 case OP_POP:
438 case OP_INC:
439 case OP_DEC:
440 case OP_XCHG:
441 STAM_PROFILE_START(&pVCpu->em.s.StatMiscEmu, a);
442 rc = EMInterpretInstructionCPU(pVM, &Cpu, CPUMCTX2CORE(pCtx), 0, &size);
443 if (RT_SUCCESS(rc))
444 {
445 pCtx->rip += Cpu.opsize;
446 STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
447 return rc;
448 }
449 if (rc != VERR_EM_INTERPRETER)
450 AssertMsgFailedReturn(("rc=%Rrc\n", rc), rc);
451 STAM_PROFILE_STOP(&pVCpu->em.s.StatMiscEmu, a);
452 break;
453 }
454 }
455#endif /* 0 */
456 STAM_PROFILE_START(&pVCpu->em.s.StatREMEmu, a);
457 Log(("EMINS: %04x:%RGv RSP=%RGv\n", pCtx->cs, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
458 EMRemLock(pVM);
459 /* Flush the recompiler TLB if the VCPU has changed. */
460 if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
461 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
462 pVM->em.s.idLastRemCpu = pVCpu->idCpu;
463
464 rc = REMR3EmulateInstruction(pVM, pVCpu);
465 EMRemUnlock(pVM);
466 STAM_PROFILE_STOP(&pVCpu->em.s.StatREMEmu, a);
467
468 return rc;
469}
470
471
472/**
473 * Executes one (or perhaps a few more) instruction(s).
474 * This is just a wrapper for discarding pszPrefix in non-logging builds.
475 *
476 * @returns VBox status code suitable for EM.
477 * @param pVM VM handle.
478 * @param pVCpu VMCPU handle.
479 * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
480 * instruction and prefix the log output with this text.
481 * @param rcGC GC return code
482 */
483DECLINLINE(int) emR3ExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC)
484{
485#ifdef LOG_ENABLED
486 return emR3ExecuteInstructionWorker(pVM, pVCpu, rcGC, pszPrefix);
487#else
488 return emR3ExecuteInstructionWorker(pVM, pVCpu, rcGC);
489#endif
490}
491
492/**
493 * Executes one (or perhaps a few more) IO instruction(s).
494 *
495 * @returns VBox status code suitable for EM.
496 * @param pVM VM handle.
497 * @param pVCpu VMCPU handle.
498 */
499static int emR3ExecuteIOInstruction(PVM pVM, PVMCPU pVCpu)
500{
501 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
502
503 STAM_PROFILE_START(&pVCpu->em.s.StatIOEmu, a);
504
505 /** @todo probably we should fall back to the recompiler; otherwise we'll go back and forth between HC & GC
506 * as io instructions tend to come in packages of more than one
507 */
508 DISCPUSTATE Cpu;
509 int rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "IO EMU");
510 if (RT_SUCCESS(rc))
511 {
512 VBOXSTRICTRC rcStrict = VINF_EM_RAW_EMULATE_INSTR;
513
514 if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
515 {
516 switch (Cpu.pCurInstr->opcode)
517 {
518 case OP_IN:
519 {
520 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
521 rcStrict = IOMInterpretIN(pVM, CPUMCTX2CORE(pCtx), &Cpu);
522 break;
523 }
524
525 case OP_OUT:
526 {
527 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
528 rcStrict = IOMInterpretOUT(pVM, CPUMCTX2CORE(pCtx), &Cpu);
529 break;
530 }
531 }
532 }
533 else if (Cpu.prefix & PREFIX_REP)
534 {
535 switch (Cpu.pCurInstr->opcode)
536 {
537 case OP_INSB:
538 case OP_INSWD:
539 {
540 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIn);
541 rcStrict = IOMInterpretINS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
542 break;
543 }
544
545 case OP_OUTSB:
546 case OP_OUTSWD:
547 {
548 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatOut);
549 rcStrict = IOMInterpretOUTS(pVM, CPUMCTX2CORE(pCtx), &Cpu);
550 break;
551 }
552 }
553 }
554
555 /*
556 * Handled the I/O return codes.
557 * (The unhandled cases end up with rcStrict == VINF_EM_RAW_EMULATE_INSTR.)
558 */
559 if (IOM_SUCCESS(rcStrict))
560 {
561 pCtx->rip += Cpu.opsize;
562 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
563 return VBOXSTRICTRC_TODO(rcStrict);
564 }
565
566 if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
567 {
568 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
569 rcStrict = emR3RawGuestTrap(pVM, pVCpu);
570 return VBOXSTRICTRC_TODO(rcStrict);
571 }
572 AssertMsg(rcStrict != VINF_TRPM_XCPT_DISPATCHED, ("Handle VINF_TRPM_XCPT_DISPATCHED\n"));
573
574 if (RT_FAILURE(rcStrict))
575 {
576 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
577 return VBOXSTRICTRC_TODO(rcStrict);
578 }
579 AssertMsg(rcStrict == VINF_EM_RAW_EMULATE_INSTR || rcStrict == VINF_EM_RESCHEDULE_REM, ("rcStrict=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
580 }
581 STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
582 return emR3ExecuteInstruction(pVM, pVCpu, "IO: ");
583}
584
585
586/**
587 * Handle a guest context trap.
588 *
589 * @returns VBox status code suitable for EM.
590 * @param pVM VM handle.
591 * @param pVCpu VMCPU handle.
592 */
593static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu)
594{
595 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
596
597 /*
598 * Get the trap info.
599 */
600 uint8_t u8TrapNo;
601 TRPMEVENT enmType;
602 RTGCUINT uErrorCode;
603 RTGCUINTPTR uCR2;
604 int rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2);
605 if (RT_FAILURE(rc))
606 {
607 AssertReleaseMsgFailed(("No trap! (rc=%Rrc)\n", rc));
608 return rc;
609 }
610
611
612#if 1 /* Experimental: Review, disable if it causes trouble. */
613 /*
614 * Handle traps in patch code first.
615 *
616 * We catch a few of these cases in RC before returning to R3 (#PF, #GP, #BP)
617 * but several traps isn't handled specially by TRPM in RC and we end up here
618 * instead. One example is #DE.
619 */
620 uint32_t uCpl = CPUMGetGuestCPL(pVCpu, CPUMCTX2CORE(pCtx));
621 if ( uCpl == 0
622 && PATMIsPatchGCAddr(pVM, pCtx->eip))
623 {
624 LogFlow(("emR3RawGuestTrap: trap %#x in patch code; eip=%08x\n", u8TrapNo, pCtx->eip));
625 return emR3PatchTrap(pVM, pVCpu, pCtx, rc);
626 }
627#endif
628
629 /*
630 * If the guest gate is marked unpatched, then we will check again if we can patch it.
631 * (This assumes that we've already tried and failed to dispatch the trap in
632 * RC for the gates that already has been patched. Which is true for most high
633 * volume traps, because these are handled specially, but not for odd ones like #DE.)
634 */
635 if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) == TRPM_INVALID_HANDLER)
636 {
637 CSAMR3CheckGates(pVM, u8TrapNo, 1);
638 Log(("emR3RawHandleRC: recheck gate %x -> valid=%d\n", u8TrapNo, TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER));
639
640 /* If it was successful, then we could go back to raw mode. */
641 if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER)
642 {
643 /* Must check pending forced actions as our IDT or GDT might be out of sync. */
644 rc = EMR3CheckRawForcedActions(pVM, pVCpu);
645 AssertRCReturn(rc, rc);
646
647 TRPMERRORCODE enmError = uErrorCode != ~0U
648 ? TRPM_TRAP_HAS_ERRORCODE
649 : TRPM_TRAP_NO_ERRORCODE;
650 rc = TRPMForwardTrap(pVCpu, CPUMCTX2CORE(pCtx), u8TrapNo, uErrorCode, enmError, TRPM_TRAP, -1);
651 if (rc == VINF_SUCCESS /* Don't use RT_SUCCESS */)
652 {
653 TRPMResetTrap(pVCpu);
654 return VINF_EM_RESCHEDULE_RAW;
655 }
656 AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP, ("%Rrc\n", rc));
657 }
658 }
659
660 /*
661 * Scan kernel code that traps; we might not get another chance.
662 */
663 /** @todo move this up before the dispatching? */
664 if ( (pCtx->ss & X86_SEL_RPL) <= 1
665 && !pCtx->eflags.Bits.u1VM)
666 {
667 Assert(!PATMIsPatchGCAddr(pVM, pCtx->eip));
668 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
669 }
670
671 /*
672 * Trap specific handling.
673 */
674 if (u8TrapNo == 6) /* (#UD) Invalid opcode. */
675 {
676 /*
677 * If MONITOR & MWAIT are supported, then interpret them here.
678 */
679 DISCPUSTATE cpu;
680 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap (#UD): ");
681 if ( RT_SUCCESS(rc)
682 && (cpu.pCurInstr->opcode == OP_MONITOR || cpu.pCurInstr->opcode == OP_MWAIT))
683 {
684 uint32_t u32Dummy, u32Features, u32ExtFeatures;
685 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Features);
686 if (u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR)
687 {
688 rc = TRPMResetTrap(pVCpu);
689 AssertRC(rc);
690
691 uint32_t opsize;
692 rc = EMInterpretInstructionCPU(pVM, pVCpu, &cpu, CPUMCTX2CORE(pCtx), 0, &opsize);
693 if (RT_SUCCESS(rc))
694 {
695 pCtx->rip += cpu.opsize;
696 return rc;
697 }
698 return emR3ExecuteInstruction(pVM, pVCpu, "Monitor: ");
699 }
700 }
701 }
702 else if (u8TrapNo == 13) /* (#GP) Privileged exception */
703 {
704 /*
705 * Handle I/O bitmap?
706 */
707 /** @todo We're not supposed to be here with a false guest trap concerning
708 * I/O access. We can easily handle those in RC. */
709 DISCPUSTATE cpu;
710 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap: ");
711 if ( RT_SUCCESS(rc)
712 && (cpu.pCurInstr->optype & OPTYPE_PORTIO))
713 {
714 /*
715 * We should really check the TSS for the IO bitmap, but it's not like this
716 * lazy approach really makes things worse.
717 */
718 rc = TRPMResetTrap(pVCpu);
719 AssertRC(rc);
720 return emR3ExecuteInstruction(pVM, pVCpu, "IO Guest Trap: ");
721 }
722 }
723
724#ifdef LOG_ENABLED
725 DBGFR3InfoLog(pVM, "cpumguest", "Guest trap");
726 DBGFR3DisasInstrCurrentLog(pVCpu, "Guest trap");
727
728 /* Get guest page information. */
729 uint64_t fFlags = 0;
730 RTGCPHYS GCPhys = 0;
731 int rc2 = PGMGstGetPage(pVCpu, uCR2, &fFlags, &GCPhys);
732 Log(("emR3RawGuestTrap: cs:eip=%04x:%08x: trap=%02x err=%08x cr2=%08x cr0=%08x%s: Phys=%RGp fFlags=%08llx %s %s %s%s rc2=%d\n",
733 pCtx->cs, pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0, (enmType == TRPM_SOFTWARE_INT) ? " software" : "", GCPhys, fFlags,
734 fFlags & X86_PTE_P ? "P " : "NP", fFlags & X86_PTE_US ? "U" : "S",
735 fFlags & X86_PTE_RW ? "RW" : "R0", fFlags & X86_PTE_G ? " G" : "", rc2));
736#endif
737
738 /*
739 * #PG has CR2.
740 * (Because of stuff like above we must set CR2 in a delayed fashion.)
741 */
742 if (u8TrapNo == 14 /* #PG */)
743 pCtx->cr2 = uCR2;
744
745 return VINF_EM_RESCHEDULE_REM;
746}
747
748
749/**
750 * Handle a ring switch trap.
751 * Need to do statistics and to install patches. The result is going to REM.
752 *
753 * @returns VBox status code suitable for EM.
754 * @param pVM VM handle.
755 * @param pVCpu VMCPU handle.
756 */
757static int emR3RawRingSwitch(PVM pVM, PVMCPU pVCpu)
758{
759 int rc;
760 DISCPUSTATE Cpu;
761 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
762
763 /*
764 * sysenter, syscall & callgate
765 */
766 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "RSWITCH: ");
767 if (RT_SUCCESS(rc))
768 {
769 if (Cpu.pCurInstr->opcode == OP_SYSENTER)
770 {
771 if (pCtx->SysEnter.cs != 0)
772 {
773 rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
774 (SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT) ? PATMFL_CODE32 : 0);
775 if (RT_SUCCESS(rc))
776 {
777 DBGFR3DisasInstrCurrentLog(pVCpu, "Patched sysenter instruction");
778 return VINF_EM_RESCHEDULE_RAW;
779 }
780 }
781 }
782
783#ifdef VBOX_WITH_STATISTICS
784 switch (Cpu.pCurInstr->opcode)
785 {
786 case OP_SYSENTER:
787 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysEnter);
788 break;
789 case OP_SYSEXIT:
790 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysExit);
791 break;
792 case OP_SYSCALL:
793 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysCall);
794 break;
795 case OP_SYSRET:
796 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysRet);
797 break;
798 }
799#endif
800 }
801 else
802 AssertRC(rc);
803
804 /* go to the REM to emulate a single instruction */
805 return emR3ExecuteInstruction(pVM, pVCpu, "RSWITCH: ");
806}
807
808
809/**
810 * Handle a trap (\#PF or \#GP) in patch code
811 *
812 * @returns VBox status code suitable for EM.
813 * @param pVM VM handle.
814 * @param pVCpu VMCPU handle.
815 * @param pCtx CPU context
816 * @param gcret GC return code
817 */
818static int emR3PatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret)
819{
820 uint8_t u8TrapNo;
821 int rc;
822 TRPMEVENT enmType;
823 RTGCUINT uErrorCode;
824 RTGCUINTPTR uCR2;
825
826 Assert(PATMIsPatchGCAddr(pVM, pCtx->eip));
827
828 if (gcret == VINF_PATM_PATCH_INT3)
829 {
830 u8TrapNo = 3;
831 uCR2 = 0;
832 uErrorCode = 0;
833 }
834 else if (gcret == VINF_PATM_PATCH_TRAP_GP)
835 {
836 /* No active trap in this case. Kind of ugly. */
837 u8TrapNo = X86_XCPT_GP;
838 uCR2 = 0;
839 uErrorCode = 0;
840 }
841 else
842 {
843 rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2);
844 if (RT_FAILURE(rc))
845 {
846 AssertReleaseMsgFailed(("emR3PatchTrap: no trap! (rc=%Rrc) gcret=%Rrc\n", rc, gcret));
847 return rc;
848 }
849 /* Reset the trap as we'll execute the original instruction again. */
850 TRPMResetTrap(pVCpu);
851 }
852
853 /*
854 * Deal with traps inside patch code.
855 * (This code won't run outside GC.)
856 */
857 if (u8TrapNo != 1)
858 {
859#ifdef LOG_ENABLED
860 DBGFR3InfoLog(pVM, "cpumguest", "Trap in patch code");
861 DBGFR3DisasInstrCurrentLog(pVCpu, "Patch code");
862
863 DISCPUSTATE Cpu;
864 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->eip, &Cpu, "Patch code: ");
865 if ( RT_SUCCESS(rc)
866 && Cpu.pCurInstr->opcode == OP_IRET)
867 {
868 uint32_t eip, selCS, uEFlags;
869
870 /* Iret crashes are bad as we have already changed the flags on the stack */
871 rc = PGMPhysSimpleReadGCPtr(pVCpu, &eip, pCtx->esp, 4);
872 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selCS, pCtx->esp+4, 4);
873 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &uEFlags, pCtx->esp+8, 4);
874 if (rc == VINF_SUCCESS)
875 {
876 if ( (uEFlags & X86_EFL_VM)
877 || (selCS & X86_SEL_RPL) == 3)
878 {
879 uint32_t selSS, esp;
880
881 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &esp, pCtx->esp + 12, 4);
882 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selSS, pCtx->esp + 16, 4);
883
884 if (uEFlags & X86_EFL_VM)
885 {
886 uint32_t selDS, selES, selFS, selGS;
887 rc = PGMPhysSimpleReadGCPtr(pVCpu, &selES, pCtx->esp + 20, 4);
888 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selDS, pCtx->esp + 24, 4);
889 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selFS, pCtx->esp + 28, 4);
890 rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selGS, pCtx->esp + 32, 4);
891 if (rc == VINF_SUCCESS)
892 {
893 Log(("Patch code: IRET->VM stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
894 Log(("Patch code: IRET->VM stack frame: DS=%04X ES=%04X FS=%04X GS=%04X\n", selDS, selES, selFS, selGS));
895 }
896 }
897 else
898 Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
899 }
900 else
901 Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x\n", selCS, eip, uEFlags));
902 }
903 }
904#endif /* LOG_ENABLED */
905 Log(("emR3PatchTrap: in patch: eip=%08x: trap=%02x err=%08x cr2=%08x cr0=%08x\n",
906 pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0));
907
908 RTGCPTR pNewEip;
909 rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &pNewEip);
910 switch (rc)
911 {
912 /*
913 * Execute the faulting instruction.
914 */
915 case VINF_SUCCESS:
916 {
917 /** @todo execute a whole block */
918 Log(("emR3PatchTrap: Executing faulting instruction at new address %RGv\n", pNewEip));
919 if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
920 Log(("emR3PatchTrap: Virtual IF flag disabled!!\n"));
921
922 pCtx->eip = pNewEip;
923 AssertRelease(pCtx->eip);
924
925 if (pCtx->eflags.Bits.u1IF)
926 {
927 /* Windows XP lets irets fault intentionally and then takes action based on the opcode; an
928 * int3 patch overwrites it and leads to blue screens. Remove the patch in this case.
929 */
930 if ( u8TrapNo == X86_XCPT_GP
931 && PATMIsInt3Patch(pVM, pCtx->eip, NULL, NULL))
932 {
933 /** @todo move to PATMR3HandleTrap */
934 Log(("Possible Windows XP iret fault at %08RX32\n", pCtx->eip));
935 PATMR3RemovePatch(pVM, pCtx->eip);
936 }
937
938 /** @todo Knoppix 5 regression when returning VINF_SUCCESS here and going back to raw mode. */
939 /* Note: possibly because a reschedule is required (e.g. iret to V86 code) */
940
941 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIR");
942 /* Interrupts are enabled; just go back to the original instruction.
943 return VINF_SUCCESS; */
944 }
945 return VINF_EM_RESCHEDULE_REM;
946 }
947
948 /*
949 * One instruction.
950 */
951 case VINF_PATCH_EMULATE_INSTR:
952 Log(("emR3PatchTrap: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
953 pNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
954 pCtx->eip = pNewEip;
955 AssertRelease(pCtx->eip);
956 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHEMUL: ");
957
958 /*
959 * The patch was disabled, hand it to the REM.
960 */
961 case VERR_PATCH_DISABLED:
962 if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
963 Log(("emR3PatchTrap: Virtual IF flag disabled!!\n"));
964 pCtx->eip = pNewEip;
965 AssertRelease(pCtx->eip);
966
967 if (pCtx->eflags.Bits.u1IF)
968 {
969 /*
970 * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
971 */
972 Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
973 return emR3ExecuteInstruction(pVM, pVCpu, "PATCHIR");
974 }
975 return VINF_EM_RESCHEDULE_REM;
976
977 /* Force continued patch exection; usually due to write monitored stack. */
978 case VINF_PATCH_CONTINUE:
979 return VINF_SUCCESS;
980
981 /*
982 * Anything else is *fatal*.
983 */
984 default:
985 AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap!\n", rc));
986 return VERR_IPE_UNEXPECTED_STATUS;
987 }
988 }
989 return VINF_SUCCESS;
990}
991
992
993/**
994 * Handle a privileged instruction.
995 *
996 * @returns VBox status code suitable for EM.
997 * @param pVM VM handle.
998 * @param pVCpu VMCPU handle;
999 */
1000static int emR3RawPrivileged(PVM pVM, PVMCPU pVCpu)
1001{
1002 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1003
1004 Assert(!pCtx->eflags.Bits.u1VM);
1005
1006 if (PATMIsEnabled(pVM))
1007 {
1008 /*
1009 * Check if in patch code.
1010 */
1011 if (PATMR3IsInsidePatchJump(pVM, pCtx->eip, NULL))
1012 {
1013#ifdef LOG_ENABLED
1014 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
1015#endif
1016 AssertMsgFailed(("FATAL ERROR: executing random instruction inside generated patch jump %08X\n", pCtx->eip));
1017 return VERR_EM_RAW_PATCH_CONFLICT;
1018 }
1019 if ( (pCtx->ss & X86_SEL_RPL) == 0
1020 && !pCtx->eflags.Bits.u1VM
1021 && !PATMIsPatchGCAddr(pVM, pCtx->eip))
1022 {
1023 int rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
1024 (SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT) ? PATMFL_CODE32 : 0);
1025 if (RT_SUCCESS(rc))
1026 {
1027#ifdef LOG_ENABLED
1028 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
1029#endif
1030 DBGFR3DisasInstrCurrentLog(pVCpu, "Patched privileged instruction");
1031 return VINF_SUCCESS;
1032 }
1033 }
1034 }
1035
1036#ifdef LOG_ENABLED
1037 if (!PATMIsPatchGCAddr(pVM, pCtx->eip))
1038 {
1039 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
1040 DBGFR3DisasInstrCurrentLog(pVCpu, "Privileged instr: ");
1041 }
1042#endif
1043
1044 /*
1045 * Instruction statistics and logging.
1046 */
1047 DISCPUSTATE Cpu;
1048 int rc;
1049
1050 rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "PRIV: ");
1051 if (RT_SUCCESS(rc))
1052 {
1053#ifdef VBOX_WITH_STATISTICS
1054 PEMSTATS pStats = pVCpu->em.s.CTX_SUFF(pStats);
1055 switch (Cpu.pCurInstr->opcode)
1056 {
1057 case OP_INVLPG:
1058 STAM_COUNTER_INC(&pStats->StatInvlpg);
1059 break;
1060 case OP_IRET:
1061 STAM_COUNTER_INC(&pStats->StatIret);
1062 break;
1063 case OP_CLI:
1064 STAM_COUNTER_INC(&pStats->StatCli);
1065 emR3RecordCli(pVM, pVCpu, pCtx->rip);
1066 break;
1067 case OP_STI:
1068 STAM_COUNTER_INC(&pStats->StatSti);
1069 break;
1070 case OP_INSB:
1071 case OP_INSWD:
1072 case OP_IN:
1073 case OP_OUTSB:
1074 case OP_OUTSWD:
1075 case OP_OUT:
1076 AssertMsgFailed(("Unexpected privileged exception due to port IO\n"));
1077 break;
1078
1079 case OP_MOV_CR:
1080 if (Cpu.param1.flags & USE_REG_GEN32)
1081 {
1082 //read
1083 Assert(Cpu.param2.flags & USE_REG_CR);
1084 Assert(Cpu.param2.base.reg_ctrl <= USE_REG_CR4);
1085 STAM_COUNTER_INC(&pStats->StatMovReadCR[Cpu.param2.base.reg_ctrl]);
1086 }
1087 else
1088 {
1089 //write
1090 Assert(Cpu.param1.flags & USE_REG_CR);
1091 Assert(Cpu.param1.base.reg_ctrl <= USE_REG_CR4);
1092 STAM_COUNTER_INC(&pStats->StatMovWriteCR[Cpu.param1.base.reg_ctrl]);
1093 }
1094 break;
1095
1096 case OP_MOV_DR:
1097 STAM_COUNTER_INC(&pStats->StatMovDRx);
1098 break;
1099 case OP_LLDT:
1100 STAM_COUNTER_INC(&pStats->StatMovLldt);
1101 break;
1102 case OP_LIDT:
1103 STAM_COUNTER_INC(&pStats->StatMovLidt);
1104 break;
1105 case OP_LGDT:
1106 STAM_COUNTER_INC(&pStats->StatMovLgdt);
1107 break;
1108 case OP_SYSENTER:
1109 STAM_COUNTER_INC(&pStats->StatSysEnter);
1110 break;
1111 case OP_SYSEXIT:
1112 STAM_COUNTER_INC(&pStats->StatSysExit);
1113 break;
1114 case OP_SYSCALL:
1115 STAM_COUNTER_INC(&pStats->StatSysCall);
1116 break;
1117 case OP_SYSRET:
1118 STAM_COUNTER_INC(&pStats->StatSysRet);
1119 break;
1120 case OP_HLT:
1121 STAM_COUNTER_INC(&pStats->StatHlt);
1122 break;
1123 default:
1124 STAM_COUNTER_INC(&pStats->StatMisc);
1125 Log4(("emR3RawPrivileged: opcode=%d\n", Cpu.pCurInstr->opcode));
1126 break;
1127 }
1128#endif /* VBOX_WITH_STATISTICS */
1129 if ( (pCtx->ss & X86_SEL_RPL) == 0
1130 && !pCtx->eflags.Bits.u1VM
1131 && SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid) == CPUMODE_32BIT)
1132 {
1133 uint32_t size;
1134
1135 STAM_PROFILE_START(&pVCpu->em.s.StatPrivEmu, a);
1136 switch (Cpu.pCurInstr->opcode)
1137 {
1138 case OP_CLI:
1139 pCtx->eflags.u32 &= ~X86_EFL_IF;
1140 Assert(Cpu.opsize == 1);
1141 pCtx->rip += Cpu.opsize;
1142 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
1143 return VINF_EM_RESCHEDULE_REM; /* must go to the recompiler now! */
1144
1145 case OP_STI:
1146 pCtx->eflags.u32 |= X86_EFL_IF;
1147 EMSetInhibitInterruptsPC(pVCpu, pCtx->rip + Cpu.opsize);
1148 Assert(Cpu.opsize == 1);
1149 pCtx->rip += Cpu.opsize;
1150 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
1151 return VINF_SUCCESS;
1152
1153 case OP_HLT:
1154 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
1155 {
1156 PATMTRANSSTATE enmState;
1157 RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->eip, &enmState);
1158
1159 if (enmState == PATMTRANS_OVERWRITTEN)
1160 {
1161 rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
1162 Assert(rc == VERR_PATCH_DISABLED);
1163 /* Conflict detected, patch disabled */
1164 Log(("emR3RawPrivileged: detected conflict -> disabled patch at %08RX32\n", pCtx->eip));
1165
1166 enmState = PATMTRANS_SAFE;
1167 }
1168
1169 /* The translation had better be successful. Otherwise we can't recover. */
1170 AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %08RX32\n", pCtx->eip));
1171 if (enmState != PATMTRANS_OVERWRITTEN)
1172 pCtx->eip = pOrgInstrGC;
1173 }
1174 /* no break; we could just return VINF_EM_HALT here */
1175
1176 case OP_MOV_CR:
1177 case OP_MOV_DR:
1178#ifdef LOG_ENABLED
1179 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
1180 {
1181 DBGFR3InfoLog(pVM, "cpumguest", "PRIV");
1182 DBGFR3DisasInstrCurrentLog(pVCpu, "Privileged instr: ");
1183 }
1184#endif
1185
1186 rc = EMInterpretInstructionCPU(pVM, pVCpu, &Cpu, CPUMCTX2CORE(pCtx), 0, &size);
1187 if (RT_SUCCESS(rc))
1188 {
1189 pCtx->rip += Cpu.opsize;
1190 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
1191
1192 if ( Cpu.pCurInstr->opcode == OP_MOV_CR
1193 && Cpu.param1.flags == USE_REG_CR /* write */
1194 )
1195 {
1196 /* Deal with CR0 updates inside patch code that force
1197 * us to go to the recompiler.
1198 */
1199 if ( PATMIsPatchGCAddr(pVM, pCtx->rip)
1200 && (pCtx->cr0 & (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE)) != (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE))
1201 {
1202 PATMTRANSSTATE enmState;
1203 RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->rip, &enmState);
1204
1205 Log(("Force recompiler switch due to cr0 (%RGp) update rip=%RGv -> %RGv (enmState=%d)\n", pCtx->cr0, pCtx->rip, pOrgInstrGC, enmState));
1206 if (enmState == PATMTRANS_OVERWRITTEN)
1207 {
1208 rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
1209 Assert(rc == VERR_PATCH_DISABLED);
1210 /* Conflict detected, patch disabled */
1211 Log(("emR3RawPrivileged: detected conflict -> disabled patch at %RGv\n", (RTGCPTR)pCtx->rip));
1212 enmState = PATMTRANS_SAFE;
1213 }
1214 /* The translation had better be successful. Otherwise we can't recover. */
1215 AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %RGv\n", (RTGCPTR)pCtx->rip));
1216 if (enmState != PATMTRANS_OVERWRITTEN)
1217 pCtx->rip = pOrgInstrGC;
1218 }
1219
1220 /* Reschedule is necessary as the execution/paging mode might have changed. */
1221 return VINF_EM_RESCHEDULE;
1222 }
1223 return rc; /* can return VINF_EM_HALT as well. */
1224 }
1225 AssertMsgReturn(rc == VERR_EM_INTERPRETER, ("%Rrc\n", rc), rc);
1226 break; /* fall back to the recompiler */
1227 }
1228 STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
1229 }
1230 }
1231
1232 if (PATMIsPatchGCAddr(pVM, pCtx->eip))
1233 return emR3PatchTrap(pVM, pVCpu, pCtx, VINF_PATM_PATCH_TRAP_GP);
1234
1235 return emR3ExecuteInstruction(pVM, pVCpu, "PRIV");
1236}
1237
1238
1239/**
1240 * Update the forced rawmode execution modifier.
1241 *
1242 * This function is called when we're returning from the raw-mode loop(s). If we're
1243 * in patch code, it will set a flag forcing execution to be resumed in raw-mode,
1244 * if not in patch code, the flag will be cleared.
1245 *
1246 * We should never interrupt patch code while it's being executed. Cli patches can
1247 * contain big code blocks, but they are always executed with IF=0. Other patches
1248 * replace single instructions and should be atomic.
1249 *
1250 * @returns Updated rc.
1251 *
1252 * @param pVM The VM handle.
1253 * @param pVCpu The VMCPU handle.
1254 * @param pCtx The guest CPU context.
1255 * @param rc The result code.
1256 */
1257int emR3RawUpdateForceFlag(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc)
1258{
1259 if (PATMIsPatchGCAddr(pVM, pCtx->eip)) /** @todo check cs selector base/type */
1260 {
1261 /* ignore reschedule attempts. */
1262 switch (rc)
1263 {
1264 case VINF_EM_RESCHEDULE:
1265 case VINF_EM_RESCHEDULE_REM:
1266 LogFlow(("emR3RawUpdateForceFlag: patch address -> force raw reschedule\n"));
1267 rc = VINF_SUCCESS;
1268 break;
1269 }
1270 pVCpu->em.s.fForceRAW = true;
1271 }
1272 else
1273 pVCpu->em.s.fForceRAW = false;
1274 return rc;
1275}
1276
1277
1278/**
1279 * Check for pending raw actions
1280 *
1281 * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
1282 * EM statuses.
1283 * @param pVM The VM to operate on.
1284 * @param pVCpu The VMCPU handle.
1285 */
1286VMMR3DECL(int) EMR3CheckRawForcedActions(PVM pVM, PVMCPU pVCpu)
1287{
1288 return emR3RawForcedActions(pVM, pVCpu, pVCpu->em.s.pCtx);
1289}
1290
1291
1292/**
1293 * Process raw-mode specific forced actions.
1294 *
1295 * This function is called when any FFs in the VM_FF_HIGH_PRIORITY_PRE_RAW_MASK is pending.
1296 *
1297 * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
1298 * EM statuses.
1299 * @param pVM The VM handle.
1300 * @param pVCpu The VMCPU handle.
1301 * @param pCtx The guest CPUM register context.
1302 */
1303static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1304{
1305 /*
1306 * Note that the order is *vitally* important!
1307 * Also note that SELMR3UpdateFromCPUM may trigger VM_FF_SELM_SYNC_TSS.
1308 */
1309
1310
1311 /*
1312 * Sync selector tables.
1313 */
1314 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT))
1315 {
1316 int rc = SELMR3UpdateFromCPUM(pVM, pVCpu);
1317 if (RT_FAILURE(rc))
1318 return rc;
1319 }
1320
1321 /*
1322 * Sync IDT.
1323 *
1324 * The CSAMR3CheckGates call in TRPMR3SyncIDT may call PGMPrefetchPage
1325 * and PGMShwModifyPage, so we're in for trouble if for instance a
1326 * PGMSyncCR3+pgmR3PoolClearAll is pending.
1327 */
1328 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TRPM_SYNC_IDT))
1329 {
1330 if ( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
1331 && EMIsRawRing0Enabled(pVM)
1332 && CSAMIsEnabled(pVM))
1333 {
1334 int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
1335 if (RT_FAILURE(rc))
1336 return rc;
1337 }
1338
1339 int rc = TRPMR3SyncIDT(pVM, pVCpu);
1340 if (RT_FAILURE(rc))
1341 return rc;
1342 }
1343
1344 /*
1345 * Sync TSS.
1346 */
1347 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS))
1348 {
1349 int rc = SELMR3SyncTSS(pVM, pVCpu);
1350 if (RT_FAILURE(rc))
1351 return rc;
1352 }
1353
1354 /*
1355 * Sync page directory.
1356 */
1357 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
1358 {
1359 Assert(pVCpu->em.s.enmState != EMSTATE_WAIT_SIPI);
1360 int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
1361 if (RT_FAILURE(rc))
1362 return rc;
1363
1364 Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
1365
1366 /* Prefetch pages for EIP and ESP. */
1367 /** @todo This is rather expensive. Should investigate if it really helps at all. */
1368 rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DIS_SELREG_CS, CPUMCTX2CORE(pCtx), pCtx->rip));
1369 if (rc == VINF_SUCCESS)
1370 rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DIS_SELREG_SS, CPUMCTX2CORE(pCtx), pCtx->rsp));
1371 if (rc != VINF_SUCCESS)
1372 {
1373 if (rc != VINF_PGM_SYNC_CR3)
1374 {
1375 AssertLogRelMsgReturn(RT_FAILURE(rc), ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
1376 return rc;
1377 }
1378 rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
1379 if (RT_FAILURE(rc))
1380 return rc;
1381 }
1382 /** @todo maybe prefetch the supervisor stack page as well */
1383 Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
1384 }
1385
1386 /*
1387 * Allocate handy pages (just in case the above actions have consumed some pages).
1388 */
1389 if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
1390 {
1391 int rc = PGMR3PhysAllocateHandyPages(pVM);
1392 if (RT_FAILURE(rc))
1393 return rc;
1394 }
1395
1396 /*
1397 * Check whether we're out of memory now.
1398 *
1399 * This may stem from some of the above actions or operations that has been executed
1400 * since we ran FFs. The allocate handy pages must for instance always be followed by
1401 * this check.
1402 */
1403 if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY))
1404 return VINF_EM_NO_MEMORY;
1405
1406 return VINF_SUCCESS;
1407}
1408
1409
1410/**
1411 * Executes raw code.
1412 *
1413 * This function contains the raw-mode version of the inner
1414 * execution loop (the outer loop being in EMR3ExecuteVM()).
1415 *
1416 * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
1417 * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
1418 *
1419 * @param pVM VM handle.
1420 * @param pVCpu VMCPU handle.
1421 * @param pfFFDone Where to store an indicator telling whether or not
1422 * FFs were done before returning.
1423 */
1424int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
1425{
1426 STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTotal, a);
1427
1428 int rc = VERR_INTERNAL_ERROR;
1429 PCPUMCTX pCtx = pVCpu->em.s.pCtx;
1430 LogFlow(("emR3RawExecute: (cs:eip=%04x:%08x)\n", pCtx->cs, pCtx->eip));
1431 pVCpu->em.s.fForceRAW = false;
1432 *pfFFDone = false;
1433
1434
1435 /*
1436 *
1437 * Spin till we get a forced action or raw mode status code resulting in
1438 * in anything but VINF_SUCCESS or VINF_EM_RESCHEDULE_RAW.
1439 *
1440 */
1441 for (;;)
1442 {
1443 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWEntry, b);
1444
1445 /*
1446 * Check various preconditions.
1447 */
1448#ifdef VBOX_STRICT
1449 Assert(REMR3QueryPendingInterrupt(pVM, pVCpu) == REM_NO_PENDING_IRQ);
1450 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) == 3 || (pCtx->ss & X86_SEL_RPL) == 0);
1451 AssertMsg( (pCtx->eflags.u32 & X86_EFL_IF)
1452 || PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip),
1453 ("Tried to execute code with IF at EIP=%08x!\n", pCtx->eip));
1454 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
1455 && PGMMapHasConflicts(pVM))
1456 {
1457 PGMMapCheck(pVM);
1458 AssertMsgFailed(("We should not get conflicts any longer!!!\n"));
1459 return VERR_INTERNAL_ERROR;
1460 }
1461#endif /* VBOX_STRICT */
1462
1463 /*
1464 * Process high priority pre-execution raw-mode FFs.
1465 */
1466 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
1467 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
1468 {
1469 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
1470 if (rc != VINF_SUCCESS)
1471 break;
1472 }
1473
1474 /*
1475 * If we're going to execute ring-0 code, the guest state needs to
1476 * be modified a bit and some of the state components (IF, SS/CS RPL,
1477 * and perhaps EIP) needs to be stored with PATM.
1478 */
1479 rc = CPUMR3RawEnter(pVCpu, NULL);
1480 if (rc != VINF_SUCCESS)
1481 {
1482 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
1483 break;
1484 }
1485
1486 /*
1487 * Scan code before executing it. Don't bother with user mode or V86 code
1488 */
1489 if ( (pCtx->ss & X86_SEL_RPL) <= 1
1490 && !pCtx->eflags.Bits.u1VM
1491 && !PATMIsPatchGCAddr(pVM, pCtx->eip))
1492 {
1493 STAM_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWEntry, b);
1494 CSAMR3CheckCodeEx(pVM, CPUMCTX2CORE(pCtx), pCtx->eip);
1495 STAM_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWEntry, b);
1496 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
1497 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
1498 {
1499 rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
1500 if (rc != VINF_SUCCESS)
1501 {
1502 rc = CPUMR3RawLeave(pVCpu, NULL, rc);
1503 break;
1504 }
1505 }
1506 }
1507
1508#ifdef LOG_ENABLED
1509 /*
1510 * Log important stuff before entering GC.
1511 */
1512 PPATMGCSTATE pGCState = PATMR3QueryGCStateHC(pVM);
1513 if (pCtx->eflags.Bits.u1VM)
1514 Log(("RV86: %04X:%08X IF=%d VMFlags=%x\n", pCtx->cs, pCtx->eip, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
1515 else if ((pCtx->ss & X86_SEL_RPL) == 1)
1516 {
1517 bool fCSAMScanned = CSAMIsPageScanned(pVM, (RTGCPTR)pCtx->eip);
1518 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));
1519 }
1520 else if ((pCtx->ss & X86_SEL_RPL) == 3)
1521 Log(("RR3: %08X ESP=%08X IF=%d VMFlags=%x\n", pCtx->eip, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
1522#endif /* LOG_ENABLED */
1523
1524
1525
1526 /*
1527 * Execute the code.
1528 */
1529 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
1530 STAM_PROFILE_START(&pVCpu->em.s.StatRAWExec, c);
1531 rc = VMMR3RawRunGC(pVM, pVCpu);
1532 STAM_PROFILE_STOP(&pVCpu->em.s.StatRAWExec, c);
1533 STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTail, d);
1534
1535 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)));
1536 LogFlow(("VMMR3RawRunGC returned %Rrc\n", rc));
1537
1538
1539
1540 /*
1541 * Restore the real CPU state and deal with high priority post
1542 * execution FFs before doing anything else.
1543 */
1544 rc = CPUMR3RawLeave(pVCpu, NULL, rc);
1545 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
1546 if ( VM_FF_ISPENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
1547 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
1548 rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
1549
1550#ifdef VBOX_STRICT
1551 /*
1552 * Assert TSS consistency & rc vs patch code.
1553 */
1554 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_SELM_SYNC_GDT) /* GDT implies TSS at the moment. */
1555 && EMIsRawRing0Enabled(pVM))
1556 SELMR3CheckTSS(pVM);
1557 switch (rc)
1558 {
1559 case VINF_SUCCESS:
1560 case VINF_EM_RAW_INTERRUPT:
1561 case VINF_PATM_PATCH_TRAP_PF:
1562 case VINF_PATM_PATCH_TRAP_GP:
1563 case VINF_PATM_PATCH_INT3:
1564 case VINF_PATM_CHECK_PATCH_PAGE:
1565 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
1566 case VINF_EM_RAW_GUEST_TRAP:
1567 case VINF_EM_RESCHEDULE_RAW:
1568 break;
1569
1570 default:
1571 if (PATMIsPatchGCAddr(pVM, pCtx->eip) && !(pCtx->eflags.u32 & X86_EFL_TF))
1572 LogIt(NULL, 0, LOG_GROUP_PATM, ("Patch code interrupted at %RRv for reason %Rrc\n", (RTRCPTR)CPUMGetGuestEIP(pVCpu), rc));
1573 break;
1574 }
1575 /*
1576 * Let's go paranoid!
1577 */
1578 if ( !VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
1579 && PGMMapHasConflicts(pVM))
1580 {
1581 PGMMapCheck(pVM);
1582 AssertMsgFailed(("We should not get conflicts any longer!!! rc=%Rrc\n", rc));
1583 return VERR_INTERNAL_ERROR;
1584 }
1585#endif /* VBOX_STRICT */
1586
1587 /*
1588 * Process the returned status code.
1589 */
1590 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
1591 {
1592 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
1593 break;
1594 }
1595 rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
1596 if (rc != VINF_SUCCESS)
1597 {
1598 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
1599 if (rc != VINF_SUCCESS)
1600 {
1601 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
1602 break;
1603 }
1604 }
1605
1606 /*
1607 * Check and execute forced actions.
1608 */
1609#ifdef VBOX_HIGH_RES_TIMERS_HACK
1610 TMTimerPollVoid(pVM, pVCpu);
1611#endif
1612 STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
1613 if ( VM_FF_ISPENDING(pVM, ~VM_FF_HIGH_PRIORITY_PRE_RAW_MASK | VM_FF_PGM_NO_MEMORY)
1614 || VMCPU_FF_ISPENDING(pVCpu, ~VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
1615 {
1616 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss & X86_SEL_RPL) != 1);
1617
1618 STAM_REL_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWTotal, a);
1619 rc = emR3ForcedActions(pVM, pVCpu, rc);
1620 STAM_REL_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWTotal, a);
1621 if ( rc != VINF_SUCCESS
1622 && rc != VINF_EM_RESCHEDULE_RAW)
1623 {
1624 rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
1625 if (rc != VINF_SUCCESS)
1626 {
1627 *pfFFDone = true;
1628 break;
1629 }
1630 }
1631 }
1632 }
1633
1634 /*
1635 * Return to outer loop.
1636 */
1637#if defined(LOG_ENABLED) && defined(DEBUG)
1638 RTLogFlush(NULL);
1639#endif
1640 STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTotal, a);
1641 return rc;
1642}
1643
Note: See TracBrowser for help on using the repository browser.

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