VirtualBox

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

Last change on this file since 44360 was 44340, checked in by vboxsync, 12 years ago

VMM,Main,Debugger,REM: VM API cleanup, prefering PUVM over PVM so we can use real reference counting and not have the memory backing the VM structure disappear on us.

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