VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/EMAll.cpp@ 66891

Last change on this file since 66891 was 66848, checked in by vboxsync, 8 years ago

VMM: use RT_LO/HI_U32 at certain places

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 144.1 KB
Line 
1/* $Id: EMAll.cpp 66848 2017-05-09 13:04:57Z vboxsync $ */
2/** @file
3 * EM - Execution Monitor(/Manager) - All contexts
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 VBOX_WITH_IEM
23#define LOG_GROUP LOG_GROUP_EM
24#include <VBox/vmm/em.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/selm.h>
27#include <VBox/vmm/patm.h>
28#include <VBox/vmm/csam.h>
29#include <VBox/vmm/pgm.h>
30#ifdef VBOX_WITH_IEM
31# include <VBox/vmm/iem.h>
32#endif
33#include <VBox/vmm/iom.h>
34#include <VBox/vmm/stam.h>
35#include "EMInternal.h"
36#include <VBox/vmm/vm.h>
37#include <VBox/vmm/vmm.h>
38#include <VBox/vmm/hm.h>
39#include <VBox/vmm/tm.h>
40#include <VBox/vmm/pdmapi.h>
41#include <VBox/param.h>
42#include <VBox/err.h>
43#include <VBox/dis.h>
44#include <VBox/disopcode.h>
45#include <VBox/log.h>
46#include <iprt/assert.h>
47#include <iprt/asm.h>
48#include <iprt/string.h>
49
50#ifdef VBOX_WITH_IEM
51//# define VBOX_COMPARE_IEM_AND_EM /* debugging... */
52//# define VBOX_SAME_AS_EM
53//# define VBOX_COMPARE_IEM_LAST
54#endif
55
56#ifdef VBOX_WITH_RAW_RING1
57# define EM_EMULATE_SMSW
58#endif
59
60
61/*********************************************************************************************************************************
62* Defined Constants And Macros *
63*********************************************************************************************************************************/
64/** @def EM_ASSERT_FAULT_RETURN
65 * Safety check.
66 *
67 * Could in theory misfire on a cross page boundary access...
68 *
69 * Currently disabled because the CSAM (+ PATM) patch monitoring occasionally
70 * turns up an alias page instead of the original faulting one and annoying the
71 * heck out of anyone running a debug build. See @bugref{2609} and @bugref{1931}.
72 */
73#if 0
74# define EM_ASSERT_FAULT_RETURN(expr, rc) AssertReturn(expr, rc)
75#else
76# define EM_ASSERT_FAULT_RETURN(expr, rc) do { } while (0)
77#endif
78
79
80/*********************************************************************************************************************************
81* Internal Functions *
82*********************************************************************************************************************************/
83#if !defined(VBOX_WITH_IEM) || defined(VBOX_COMPARE_IEM_AND_EM)
84DECLINLINE(VBOXSTRICTRC) emInterpretInstructionCPUOuter(PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
85 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize);
86#endif
87
88
89/*********************************************************************************************************************************
90* Global Variables *
91*********************************************************************************************************************************/
92#ifdef VBOX_COMPARE_IEM_AND_EM
93static const uint32_t g_fInterestingFFs = VMCPU_FF_TO_R3
94 | VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_CSAM_SCAN_PAGE | VMCPU_FF_INHIBIT_INTERRUPTS
95 | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT
96 | VMCPU_FF_TLB_FLUSH | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL;
97static uint32_t g_fIncomingFFs;
98static CPUMCTX g_IncomingCtx;
99static bool g_fIgnoreRaxRdx = false;
100
101static uint32_t g_fEmFFs;
102static CPUMCTX g_EmCtx;
103static uint8_t g_abEmWrote[256];
104static size_t g_cbEmWrote;
105
106static uint32_t g_fIemFFs;
107static CPUMCTX g_IemCtx;
108extern uint8_t g_abIemWrote[256];
109#if defined(VBOX_COMPARE_IEM_FIRST) || defined(VBOX_COMPARE_IEM_LAST)
110extern size_t g_cbIemWrote;
111#else
112static size_t g_cbIemWrote;
113#endif
114#endif
115
116
117/**
118 * Get the current execution manager status.
119 *
120 * @returns Current status.
121 * @param pVCpu The cross context virtual CPU structure.
122 */
123VMM_INT_DECL(EMSTATE) EMGetState(PVMCPU pVCpu)
124{
125 return pVCpu->em.s.enmState;
126}
127
128
129/**
130 * Sets the current execution manager status. (use only when you know what you're doing!)
131 *
132 * @param pVCpu The cross context virtual CPU structure.
133 * @param enmNewState The new state, EMSTATE_WAIT_SIPI or EMSTATE_HALTED.
134 */
135VMM_INT_DECL(void) EMSetState(PVMCPU pVCpu, EMSTATE enmNewState)
136{
137 /* Only allowed combination: */
138 Assert(pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI && enmNewState == EMSTATE_HALTED);
139 pVCpu->em.s.enmState = enmNewState;
140}
141
142
143/**
144 * Sets the PC for which interrupts should be inhibited.
145 *
146 * @param pVCpu The cross context virtual CPU structure.
147 * @param PC The PC.
148 */
149VMMDECL(void) EMSetInhibitInterruptsPC(PVMCPU pVCpu, RTGCUINTPTR PC)
150{
151 pVCpu->em.s.GCPtrInhibitInterrupts = PC;
152 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
153}
154
155
156/**
157 * Gets the PC for which interrupts should be inhibited.
158 *
159 * There are a few instructions which inhibits or delays interrupts
160 * for the instruction following them. These instructions are:
161 * - STI
162 * - MOV SS, r/m16
163 * - POP SS
164 *
165 * @returns The PC for which interrupts should be inhibited.
166 * @param pVCpu The cross context virtual CPU structure.
167 *
168 */
169VMMDECL(RTGCUINTPTR) EMGetInhibitInterruptsPC(PVMCPU pVCpu)
170{
171 return pVCpu->em.s.GCPtrInhibitInterrupts;
172}
173
174
175/**
176 * Prepare an MWAIT - essentials of the MONITOR instruction.
177 *
178 * @returns VINF_SUCCESS
179 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
180 * @param rax The content of RAX.
181 * @param rcx The content of RCX.
182 * @param rdx The content of RDX.
183 * @param GCPhys The physical address corresponding to rax.
184 */
185VMM_INT_DECL(int) EMMonitorWaitPrepare(PVMCPU pVCpu, uint64_t rax, uint64_t rcx, uint64_t rdx, RTGCPHYS GCPhys)
186{
187 pVCpu->em.s.MWait.uMonitorRAX = rax;
188 pVCpu->em.s.MWait.uMonitorRCX = rcx;
189 pVCpu->em.s.MWait.uMonitorRDX = rdx;
190 pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_MONITOR_ACTIVE;
191 /** @todo Make use of GCPhys. */
192 NOREF(GCPhys);
193 /** @todo Complete MONITOR implementation. */
194 return VINF_SUCCESS;
195}
196
197
198/**
199 * Checks if the monitor hardware is armed / active.
200 *
201 * @returns true if armed, false otherwise.
202 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
203 */
204VMM_INT_DECL(bool) EMMonitorIsArmed(PVMCPU pVCpu)
205{
206 return RT_BOOL(pVCpu->em.s.MWait.fWait & EMMWAIT_FLAG_MONITOR_ACTIVE);
207}
208
209
210/**
211 * Performs an MWAIT.
212 *
213 * @returns VINF_SUCCESS
214 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
215 * @param rax The content of RAX.
216 * @param rcx The content of RCX.
217 */
218VMM_INT_DECL(int) EMMonitorWaitPerform(PVMCPU pVCpu, uint64_t rax, uint64_t rcx)
219{
220 pVCpu->em.s.MWait.uMWaitRAX = rax;
221 pVCpu->em.s.MWait.uMWaitRCX = rcx;
222 pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_ACTIVE;
223 if (rcx)
224 pVCpu->em.s.MWait.fWait |= EMMWAIT_FLAG_BREAKIRQIF0;
225 else
226 pVCpu->em.s.MWait.fWait &= ~EMMWAIT_FLAG_BREAKIRQIF0;
227 /** @todo not completely correct?? */
228 return VINF_EM_HALT;
229}
230
231
232
233/**
234 * Determine if we should continue execution in HM after encountering an mwait
235 * instruction.
236 *
237 * Clears MWAIT flags if returning @c true.
238 *
239 * @returns true if we should continue, false if we should halt.
240 * @param pVCpu The cross context virtual CPU structure.
241 * @param pCtx Current CPU context.
242 */
243VMM_INT_DECL(bool) EMMonitorWaitShouldContinue(PVMCPU pVCpu, PCPUMCTX pCtx)
244{
245 if ( pCtx->eflags.Bits.u1IF
246 || ( (pVCpu->em.s.MWait.fWait & (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0))
247 == (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)) )
248 {
249 if (VMCPU_FF_IS_PENDING(pVCpu, (VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)))
250 {
251 pVCpu->em.s.MWait.fWait &= ~(EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0);
252 return true;
253 }
254 }
255
256 return false;
257}
258
259
260/**
261 * Determine if we should continue execution in HM after encountering a hlt
262 * instruction.
263 *
264 * @returns true if we should continue, false if we should halt.
265 * @param pVCpu The cross context virtual CPU structure.
266 * @param pCtx Current CPU context.
267 */
268VMM_INT_DECL(bool) EMShouldContinueAfterHalt(PVMCPU pVCpu, PCPUMCTX pCtx)
269{
270 if (pCtx->eflags.Bits.u1IF)
271 return !!VMCPU_FF_IS_PENDING(pVCpu, (VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC));
272 return false;
273}
274
275
276/**
277 * Unhalts and wakes up the given CPU.
278 *
279 * This is an API for assisting the KVM hypercall API in implementing KICK_CPU.
280 * It sets VMCPU_FF_UNHALT for @a pVCpuDst and makes sure it is woken up. If
281 * the CPU isn't currently in a halt, the next HLT instruction it executes will
282 * be affected.
283 *
284 * @returns GVMMR0SchedWakeUpEx result or VINF_SUCCESS depending on context.
285 * @param pVM The cross context VM structure.
286 * @param pVCpuDst The cross context virtual CPU structure of the
287 * CPU to unhalt and wake up. This is usually not the
288 * same as the caller.
289 * @thread EMT
290 */
291VMM_INT_DECL(int) EMUnhaltAndWakeUp(PVM pVM, PVMCPU pVCpuDst)
292{
293 /*
294 * Flag the current(/next) HLT to unhalt immediately.
295 */
296 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_UNHALT);
297
298 /*
299 * Wake up the EMT (technically should be abstracted by VMM/VMEmt, but
300 * just do it here for now).
301 */
302#ifdef IN_RING0
303 /* We might be here with preemption disabled or enabled (i.e. depending on
304 thread-context hooks being used), so don't try obtaining the GVMMR0 used
305 lock here. See @bugref{7270#c148}. */
306 int rc = GVMMR0SchedWakeUpEx(pVM, pVCpuDst->idCpu, false /* fTakeUsedLock */);
307 AssertRC(rc);
308
309#elif defined(IN_RING3)
310 int rc = SUPR3CallVMMR0(pVM->pVMR0, pVCpuDst->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, NULL /* pvArg */);
311 AssertRC(rc);
312
313#else
314 /* Nothing to do for raw-mode, shouldn't really be used by raw-mode guests anyway. */
315 Assert(pVM->cCpus == 1); NOREF(pVM);
316 int rc = VINF_SUCCESS;
317#endif
318 return rc;
319}
320
321
322/**
323 * Locks REM execution to a single VCPU.
324 *
325 * @param pVM The cross context VM structure.
326 */
327VMMDECL(void) EMRemLock(PVM pVM)
328{
329#ifdef VBOX_WITH_REM
330 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
331 return; /* early init */
332
333 Assert(!PGMIsLockOwner(pVM));
334 Assert(!IOMIsLockWriteOwner(pVM));
335 int rc = PDMCritSectEnter(&pVM->em.s.CritSectREM, VERR_SEM_BUSY);
336 AssertRCSuccess(rc);
337#else
338 RT_NOREF(pVM);
339#endif
340}
341
342
343/**
344 * Unlocks REM execution
345 *
346 * @param pVM The cross context VM structure.
347 */
348VMMDECL(void) EMRemUnlock(PVM pVM)
349{
350#ifdef VBOX_WITH_REM
351 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
352 return; /* early init */
353
354 PDMCritSectLeave(&pVM->em.s.CritSectREM);
355#else
356 RT_NOREF(pVM);
357#endif
358}
359
360
361/**
362 * Check if this VCPU currently owns the REM lock.
363 *
364 * @returns bool owner/not owner
365 * @param pVM The cross context VM structure.
366 */
367VMMDECL(bool) EMRemIsLockOwner(PVM pVM)
368{
369#ifdef VBOX_WITH_REM
370 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
371 return true; /* early init */
372
373 return PDMCritSectIsOwner(&pVM->em.s.CritSectREM);
374#else
375 RT_NOREF(pVM);
376 return true;
377#endif
378}
379
380
381/**
382 * Try to acquire the REM lock.
383 *
384 * @returns VBox status code
385 * @param pVM The cross context VM structure.
386 */
387VMM_INT_DECL(int) EMRemTryLock(PVM pVM)
388{
389#ifdef VBOX_WITH_REM
390 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
391 return VINF_SUCCESS; /* early init */
392
393 return PDMCritSectTryEnter(&pVM->em.s.CritSectREM);
394#else
395 RT_NOREF(pVM);
396 return VINF_SUCCESS;
397#endif
398}
399
400
401/**
402 * @callback_method_impl{FNDISREADBYTES}
403 */
404static DECLCALLBACK(int) emReadBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
405{
406 PVMCPU pVCpu = (PVMCPU)pDis->pvUser;
407#if defined(VBOX_WITH_RAW_MODE) && (defined(IN_RC) || defined(IN_RING3))
408 PVM pVM = pVCpu->CTX_SUFF(pVM);
409#endif
410 RTUINTPTR uSrcAddr = pDis->uInstrAddr + offInstr;
411 int rc;
412
413 /*
414 * Figure how much we can or must read.
415 */
416 size_t cbToRead = PAGE_SIZE - (uSrcAddr & PAGE_OFFSET_MASK);
417 if (cbToRead > cbMaxRead)
418 cbToRead = cbMaxRead;
419 else if (cbToRead < cbMinRead)
420 cbToRead = cbMinRead;
421
422#if defined(VBOX_WITH_RAW_MODE) && (defined(IN_RC) || defined(IN_RING3))
423 /*
424 * We might be called upon to interpret an instruction in a patch.
425 */
426 if (PATMIsPatchGCAddr(pVM, uSrcAddr))
427 {
428# ifdef IN_RC
429 memcpy(&pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
430# else
431 memcpy(&pDis->abInstr[offInstr], PATMR3GCPtrToHCPtr(pVM, uSrcAddr), cbToRead);
432# endif
433 rc = VINF_SUCCESS;
434 }
435 else
436#endif
437 {
438# ifdef IN_RC
439 /*
440 * Try access it thru the shadow page tables first. Fall back on the
441 * slower PGM method if it fails because the TLB or page table was
442 * modified recently.
443 */
444 rc = MMGCRamRead(pVCpu->pVMRC, &pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
445 if (rc == VERR_ACCESS_DENIED && cbToRead > cbMinRead)
446 {
447 cbToRead = cbMinRead;
448 rc = MMGCRamRead(pVCpu->pVMRC, &pDis->abInstr[offInstr], (void *)(uintptr_t)uSrcAddr, cbToRead);
449 }
450 if (rc == VERR_ACCESS_DENIED)
451#endif
452 {
453 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pDis->abInstr[offInstr], uSrcAddr, cbToRead);
454 if (RT_FAILURE(rc))
455 {
456 if (cbToRead > cbMinRead)
457 {
458 cbToRead = cbMinRead;
459 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pDis->abInstr[offInstr], uSrcAddr, cbToRead);
460 }
461 if (RT_FAILURE(rc))
462 {
463#ifndef IN_RC
464 /*
465 * If we fail to find the page via the guest's page tables
466 * we invalidate the page in the host TLB (pertaining to
467 * the guest in the NestedPaging case). See @bugref{6043}.
468 */
469 if (rc == VERR_PAGE_TABLE_NOT_PRESENT || rc == VERR_PAGE_NOT_PRESENT)
470 {
471 HMInvalidatePage(pVCpu, uSrcAddr);
472 if (((uSrcAddr + cbToRead - 1) >> PAGE_SHIFT) != (uSrcAddr >> PAGE_SHIFT))
473 HMInvalidatePage(pVCpu, uSrcAddr + cbToRead - 1);
474 }
475#endif
476 }
477 }
478 }
479 }
480
481 pDis->cbCachedInstr = offInstr + (uint8_t)cbToRead;
482 return rc;
483}
484
485
486#if !defined(VBOX_WITH_IEM) || defined(VBOX_COMPARE_IEM_AND_EM)
487DECLINLINE(int) emDisCoreOne(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
488{
489 NOREF(pVM);
490 return DISInstrWithReader(InstrGC, (DISCPUMODE)pDis->uCpuMode, emReadBytes, pVCpu, pDis, pOpsize);
491}
492#endif
493
494
495/**
496 * Disassembles the current instruction.
497 *
498 * @returns VBox status code, see SELMToFlatEx and EMInterpretDisasOneEx for
499 * details.
500 *
501 * @param pVM The cross context VM structure.
502 * @param pVCpu The cross context virtual CPU structure.
503 * @param pDis Where to return the parsed instruction info.
504 * @param pcbInstr Where to return the instruction size. (optional)
505 */
506VMM_INT_DECL(int) EMInterpretDisasCurrent(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, unsigned *pcbInstr)
507{
508 PCPUMCTXCORE pCtxCore = CPUMCTX2CORE(CPUMQueryGuestCtxPtr(pVCpu));
509 RTGCPTR GCPtrInstr;
510#if 0
511 int rc = SELMToFlatEx(pVCpu, DISSELREG_CS, pCtxCore, pCtxCore->rip, 0, &GCPtrInstr);
512#else
513/** @todo Get the CPU mode as well while we're at it! */
514 int rc = SELMValidateAndConvertCSAddr(pVCpu, pCtxCore->eflags, pCtxCore->ss.Sel, pCtxCore->cs.Sel, &pCtxCore->cs,
515 pCtxCore->rip, &GCPtrInstr);
516#endif
517 if (RT_FAILURE(rc))
518 {
519 Log(("EMInterpretDisasOne: Failed to convert %RTsel:%RGv (cpl=%d) - rc=%Rrc !!\n",
520 pCtxCore->cs.Sel, (RTGCPTR)pCtxCore->rip, pCtxCore->ss.Sel & X86_SEL_RPL, rc));
521 return rc;
522 }
523 return EMInterpretDisasOneEx(pVM, pVCpu, (RTGCUINTPTR)GCPtrInstr, pCtxCore, pDis, pcbInstr);
524}
525
526
527/**
528 * Disassembles one instruction.
529 *
530 * This is used by internally by the interpreter and by trap/access handlers.
531 *
532 * @returns VBox status code.
533 *
534 * @param pVM The cross context VM structure.
535 * @param pVCpu The cross context virtual CPU structure.
536 * @param GCPtrInstr The flat address of the instruction.
537 * @param pCtxCore The context core (used to determine the cpu mode).
538 * @param pDis Where to return the parsed instruction info.
539 * @param pcbInstr Where to return the instruction size. (optional)
540 */
541VMM_INT_DECL(int) EMInterpretDisasOneEx(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore,
542 PDISCPUSTATE pDis, unsigned *pcbInstr)
543{
544 NOREF(pVM);
545 Assert(pCtxCore == CPUMGetGuestCtxCore(pVCpu)); NOREF(pCtxCore);
546 DISCPUMODE enmCpuMode = CPUMGetGuestDisMode(pVCpu);
547 /** @todo Deal with too long instruction (=> \#GP), opcode read errors (=>
548 * \#PF, \#GP, \#??), undefined opcodes (=> \#UD), and such. */
549 int rc = DISInstrWithReader(GCPtrInstr, enmCpuMode, emReadBytes, pVCpu, pDis, pcbInstr);
550 if (RT_SUCCESS(rc))
551 return VINF_SUCCESS;
552 AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("DISCoreOne failed to GCPtrInstr=%RGv rc=%Rrc\n", GCPtrInstr, rc));
553 return rc;
554}
555
556
557#if defined(VBOX_COMPARE_IEM_FIRST) || defined(VBOX_COMPARE_IEM_LAST)
558static void emCompareWithIem(PVMCPU pVCpu, PCCPUMCTX pEmCtx, PCCPUMCTX pIemCtx,
559 VBOXSTRICTRC rcEm, VBOXSTRICTRC rcIem,
560 uint32_t cbEm, uint32_t cbIem)
561{
562 /* Quick compare. */
563 if ( rcEm == rcIem
564 && cbEm == cbIem
565 && g_cbEmWrote == g_cbIemWrote
566 && memcmp(g_abIemWrote, g_abEmWrote, g_cbIemWrote) == 0
567 && memcmp(pIemCtx, pEmCtx, sizeof(*pIemCtx)) == 0
568 && (g_fEmFFs & g_fInterestingFFs) == (g_fIemFFs & g_fInterestingFFs)
569 )
570 return;
571
572 /* Report exact differences. */
573 RTLogPrintf("! EM and IEM differs at %04x:%08RGv !\n", g_IncomingCtx.cs.Sel, g_IncomingCtx.rip);
574 if (rcEm != rcIem)
575 RTLogPrintf(" * rcIem=%Rrc rcEm=%Rrc\n", VBOXSTRICTRC_VAL(rcIem), VBOXSTRICTRC_VAL(rcEm));
576 else if (cbEm != cbIem)
577 RTLogPrintf(" * cbIem=%#x cbEm=%#x\n", cbIem, cbEm);
578
579 if (RT_SUCCESS(rcEm) && RT_SUCCESS(rcIem))
580 {
581 if (g_cbIemWrote != g_cbEmWrote)
582 RTLogPrintf("!! g_cbIemWrote=%#x g_cbEmWrote=%#x\n", g_cbIemWrote, g_cbEmWrote);
583 else if (memcmp(g_abIemWrote, g_abEmWrote, g_cbIemWrote))
584 {
585 RTLogPrintf("!! IemWrote %.*Rhxs\n", RT_MIN(RT_MAX(1, g_cbIemWrote), 64), g_abIemWrote);
586 RTLogPrintf("!! EemWrote %.*Rhxs\n", RT_MIN(RT_MAX(1, g_cbIemWrote), 64), g_abIemWrote);
587 }
588
589 if ((g_fEmFFs & g_fInterestingFFs) != (g_fIemFFs & g_fInterestingFFs))
590 RTLogPrintf("!! g_fIemFFs=%#x g_fEmFFs=%#x (diff=%#x)\n", g_fIemFFs & g_fInterestingFFs,
591 g_fEmFFs & g_fInterestingFFs, (g_fIemFFs ^ g_fEmFFs) & g_fInterestingFFs);
592
593# define CHECK_FIELD(a_Field) \
594 do \
595 { \
596 if (pEmCtx->a_Field != pIemCtx->a_Field) \
597 { \
598 switch (sizeof(pEmCtx->a_Field)) \
599 { \
600 case 1: RTLogPrintf("!! %8s differs - iem=%02x - em=%02x\n", #a_Field, pIemCtx->a_Field, pEmCtx->a_Field); break; \
601 case 2: RTLogPrintf("!! %8s differs - iem=%04x - em=%04x\n", #a_Field, pIemCtx->a_Field, pEmCtx->a_Field); break; \
602 case 4: RTLogPrintf("!! %8s differs - iem=%08x - em=%08x\n", #a_Field, pIemCtx->a_Field, pEmCtx->a_Field); break; \
603 case 8: RTLogPrintf("!! %8s differs - iem=%016llx - em=%016llx\n", #a_Field, pIemCtx->a_Field, pEmCtx->a_Field); break; \
604 default: RTLogPrintf("!! %8s differs\n", #a_Field); break; \
605 } \
606 cDiffs++; \
607 } \
608 } while (0)
609
610# define CHECK_BIT_FIELD(a_Field) \
611 do \
612 { \
613 if (pEmCtx->a_Field != pIemCtx->a_Field) \
614 { \
615 RTLogPrintf("!! %8s differs - iem=%02x - em=%02x\n", #a_Field, pIemCtx->a_Field, pEmCtx->a_Field); \
616 cDiffs++; \
617 } \
618 } while (0)
619
620# define CHECK_SEL(a_Sel) \
621 do \
622 { \
623 CHECK_FIELD(a_Sel.Sel); \
624 CHECK_FIELD(a_Sel.Attr.u); \
625 CHECK_FIELD(a_Sel.u64Base); \
626 CHECK_FIELD(a_Sel.u32Limit); \
627 CHECK_FIELD(a_Sel.fFlags); \
628 } while (0)
629
630 unsigned cDiffs = 0;
631 if (memcmp(&pEmCtx->fpu, &pIemCtx->fpu, sizeof(pIemCtx->fpu)))
632 {
633 RTLogPrintf(" the FPU state differs\n");
634 cDiffs++;
635 CHECK_FIELD(fpu.FCW);
636 CHECK_FIELD(fpu.FSW);
637 CHECK_FIELD(fpu.FTW);
638 CHECK_FIELD(fpu.FOP);
639 CHECK_FIELD(fpu.FPUIP);
640 CHECK_FIELD(fpu.CS);
641 CHECK_FIELD(fpu.Rsrvd1);
642 CHECK_FIELD(fpu.FPUDP);
643 CHECK_FIELD(fpu.DS);
644 CHECK_FIELD(fpu.Rsrvd2);
645 CHECK_FIELD(fpu.MXCSR);
646 CHECK_FIELD(fpu.MXCSR_MASK);
647 CHECK_FIELD(fpu.aRegs[0].au64[0]); CHECK_FIELD(fpu.aRegs[0].au64[1]);
648 CHECK_FIELD(fpu.aRegs[1].au64[0]); CHECK_FIELD(fpu.aRegs[1].au64[1]);
649 CHECK_FIELD(fpu.aRegs[2].au64[0]); CHECK_FIELD(fpu.aRegs[2].au64[1]);
650 CHECK_FIELD(fpu.aRegs[3].au64[0]); CHECK_FIELD(fpu.aRegs[3].au64[1]);
651 CHECK_FIELD(fpu.aRegs[4].au64[0]); CHECK_FIELD(fpu.aRegs[4].au64[1]);
652 CHECK_FIELD(fpu.aRegs[5].au64[0]); CHECK_FIELD(fpu.aRegs[5].au64[1]);
653 CHECK_FIELD(fpu.aRegs[6].au64[0]); CHECK_FIELD(fpu.aRegs[6].au64[1]);
654 CHECK_FIELD(fpu.aRegs[7].au64[0]); CHECK_FIELD(fpu.aRegs[7].au64[1]);
655 CHECK_FIELD(fpu.aXMM[ 0].au64[0]); CHECK_FIELD(fpu.aXMM[ 0].au64[1]);
656 CHECK_FIELD(fpu.aXMM[ 1].au64[0]); CHECK_FIELD(fpu.aXMM[ 1].au64[1]);
657 CHECK_FIELD(fpu.aXMM[ 2].au64[0]); CHECK_FIELD(fpu.aXMM[ 2].au64[1]);
658 CHECK_FIELD(fpu.aXMM[ 3].au64[0]); CHECK_FIELD(fpu.aXMM[ 3].au64[1]);
659 CHECK_FIELD(fpu.aXMM[ 4].au64[0]); CHECK_FIELD(fpu.aXMM[ 4].au64[1]);
660 CHECK_FIELD(fpu.aXMM[ 5].au64[0]); CHECK_FIELD(fpu.aXMM[ 5].au64[1]);
661 CHECK_FIELD(fpu.aXMM[ 6].au64[0]); CHECK_FIELD(fpu.aXMM[ 6].au64[1]);
662 CHECK_FIELD(fpu.aXMM[ 7].au64[0]); CHECK_FIELD(fpu.aXMM[ 7].au64[1]);
663 CHECK_FIELD(fpu.aXMM[ 8].au64[0]); CHECK_FIELD(fpu.aXMM[ 8].au64[1]);
664 CHECK_FIELD(fpu.aXMM[ 9].au64[0]); CHECK_FIELD(fpu.aXMM[ 9].au64[1]);
665 CHECK_FIELD(fpu.aXMM[10].au64[0]); CHECK_FIELD(fpu.aXMM[10].au64[1]);
666 CHECK_FIELD(fpu.aXMM[11].au64[0]); CHECK_FIELD(fpu.aXMM[11].au64[1]);
667 CHECK_FIELD(fpu.aXMM[12].au64[0]); CHECK_FIELD(fpu.aXMM[12].au64[1]);
668 CHECK_FIELD(fpu.aXMM[13].au64[0]); CHECK_FIELD(fpu.aXMM[13].au64[1]);
669 CHECK_FIELD(fpu.aXMM[14].au64[0]); CHECK_FIELD(fpu.aXMM[14].au64[1]);
670 CHECK_FIELD(fpu.aXMM[15].au64[0]); CHECK_FIELD(fpu.aXMM[15].au64[1]);
671 for (unsigned i = 0; i < RT_ELEMENTS(pEmCtx->fpu.au32RsrvdRest); i++)
672 CHECK_FIELD(fpu.au32RsrvdRest[i]);
673 }
674 CHECK_FIELD(rip);
675 if (pEmCtx->rflags.u != pIemCtx->rflags.u)
676 {
677 RTLogPrintf("!! rflags differs - iem=%08llx em=%08llx\n", pIemCtx->rflags.u, pEmCtx->rflags.u);
678 CHECK_BIT_FIELD(rflags.Bits.u1CF);
679 CHECK_BIT_FIELD(rflags.Bits.u1Reserved0);
680 CHECK_BIT_FIELD(rflags.Bits.u1PF);
681 CHECK_BIT_FIELD(rflags.Bits.u1Reserved1);
682 CHECK_BIT_FIELD(rflags.Bits.u1AF);
683 CHECK_BIT_FIELD(rflags.Bits.u1Reserved2);
684 CHECK_BIT_FIELD(rflags.Bits.u1ZF);
685 CHECK_BIT_FIELD(rflags.Bits.u1SF);
686 CHECK_BIT_FIELD(rflags.Bits.u1TF);
687 CHECK_BIT_FIELD(rflags.Bits.u1IF);
688 CHECK_BIT_FIELD(rflags.Bits.u1DF);
689 CHECK_BIT_FIELD(rflags.Bits.u1OF);
690 CHECK_BIT_FIELD(rflags.Bits.u2IOPL);
691 CHECK_BIT_FIELD(rflags.Bits.u1NT);
692 CHECK_BIT_FIELD(rflags.Bits.u1Reserved3);
693 CHECK_BIT_FIELD(rflags.Bits.u1RF);
694 CHECK_BIT_FIELD(rflags.Bits.u1VM);
695 CHECK_BIT_FIELD(rflags.Bits.u1AC);
696 CHECK_BIT_FIELD(rflags.Bits.u1VIF);
697 CHECK_BIT_FIELD(rflags.Bits.u1VIP);
698 CHECK_BIT_FIELD(rflags.Bits.u1ID);
699 }
700
701 if (!g_fIgnoreRaxRdx)
702 CHECK_FIELD(rax);
703 CHECK_FIELD(rcx);
704 if (!g_fIgnoreRaxRdx)
705 CHECK_FIELD(rdx);
706 CHECK_FIELD(rbx);
707 CHECK_FIELD(rsp);
708 CHECK_FIELD(rbp);
709 CHECK_FIELD(rsi);
710 CHECK_FIELD(rdi);
711 CHECK_FIELD(r8);
712 CHECK_FIELD(r9);
713 CHECK_FIELD(r10);
714 CHECK_FIELD(r11);
715 CHECK_FIELD(r12);
716 CHECK_FIELD(r13);
717 CHECK_SEL(cs);
718 CHECK_SEL(ss);
719 CHECK_SEL(ds);
720 CHECK_SEL(es);
721 CHECK_SEL(fs);
722 CHECK_SEL(gs);
723 CHECK_FIELD(cr0);
724 CHECK_FIELD(cr2);
725 CHECK_FIELD(cr3);
726 CHECK_FIELD(cr4);
727 CHECK_FIELD(dr[0]);
728 CHECK_FIELD(dr[1]);
729 CHECK_FIELD(dr[2]);
730 CHECK_FIELD(dr[3]);
731 CHECK_FIELD(dr[6]);
732 CHECK_FIELD(dr[7]);
733 CHECK_FIELD(gdtr.cbGdt);
734 CHECK_FIELD(gdtr.pGdt);
735 CHECK_FIELD(idtr.cbIdt);
736 CHECK_FIELD(idtr.pIdt);
737 CHECK_SEL(ldtr);
738 CHECK_SEL(tr);
739 CHECK_FIELD(SysEnter.cs);
740 CHECK_FIELD(SysEnter.eip);
741 CHECK_FIELD(SysEnter.esp);
742 CHECK_FIELD(msrEFER);
743 CHECK_FIELD(msrSTAR);
744 CHECK_FIELD(msrPAT);
745 CHECK_FIELD(msrLSTAR);
746 CHECK_FIELD(msrCSTAR);
747 CHECK_FIELD(msrSFMASK);
748 CHECK_FIELD(msrKERNELGSBASE);
749
750# undef CHECK_FIELD
751# undef CHECK_BIT_FIELD
752 }
753}
754#endif /* VBOX_COMPARE_IEM_AND_EM */
755
756
757/**
758 * Interprets the current instruction.
759 *
760 * @returns VBox status code.
761 * @retval VINF_* Scheduling instructions.
762 * @retval VERR_EM_INTERPRETER Something we can't cope with.
763 * @retval VERR_* Fatal errors.
764 *
765 * @param pVCpu The cross context virtual CPU structure.
766 * @param pRegFrame The register frame.
767 * Updates the EIP if an instruction was executed successfully.
768 * @param pvFault The fault address (CR2).
769 *
770 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
771 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
772 * to worry about e.g. invalid modrm combinations (!)
773 */
774VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstruction(PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
775{
776 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
777 LogFlow(("EMInterpretInstruction %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
778#ifdef VBOX_WITH_IEM
779 NOREF(pvFault);
780
781# ifdef VBOX_COMPARE_IEM_AND_EM
782 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
783 g_IncomingCtx = *pCtx;
784 g_fIncomingFFs = pVCpu->fLocalForcedActions;
785 g_cbEmWrote = g_cbIemWrote = 0;
786
787# ifdef VBOX_COMPARE_IEM_FIRST
788 /* IEM */
789 VBOXSTRICTRC rcIem = IEMExecOneBypassEx(pVCpu, pRegFrame, NULL);
790 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
791 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
792 rcIem = VERR_EM_INTERPRETER;
793 g_IemCtx = *pCtx;
794 g_fIemFFs = pVCpu->fLocalForcedActions;
795 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
796 *pCtx = g_IncomingCtx;
797# endif
798
799 /* EM */
800 RTGCPTR pbCode;
801 VBOXSTRICTRC rcEm = SELMToFlatEx(pVCpu, DISSELREG_CS, pRegFrame, pRegFrame->rip, 0, &pbCode);
802 if (RT_SUCCESS(rcEm))
803 {
804 uint32_t cbOp;
805 PDISCPUSTATE pDis = &pVCpu->em.s.DisState;
806 pDis->uCpuMode = CPUMGetGuestDisMode(pVCpu);
807 rcEm = emDisCoreOne(pVCpu->CTX_SUFF(pVM), pVCpu, pDis, (RTGCUINTPTR)pbCode, &cbOp);
808 if (RT_SUCCESS(rcEm))
809 {
810 Assert(cbOp == pDis->cbInstr);
811 uint32_t cbIgnored;
812 rcEm = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_SUPERVISOR, &cbIgnored);
813 if (RT_SUCCESS(rcEm))
814 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
815
816 }
817 rcEm = VERR_EM_INTERPRETER;
818 }
819 else
820 rcEm = VERR_EM_INTERPRETER;
821# ifdef VBOX_SAME_AS_EM
822 if (rcEm == VERR_EM_INTERPRETER)
823 {
824 Log(("EMInterpretInstruction: returns %Rrc\n", VBOXSTRICTRC_VAL(rcEm)));
825 return rcEm;
826 }
827# endif
828 g_EmCtx = *pCtx;
829 g_fEmFFs = pVCpu->fLocalForcedActions;
830 VBOXSTRICTRC rc = rcEm;
831
832# ifdef VBOX_COMPARE_IEM_LAST
833 /* IEM */
834 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
835 *pCtx = g_IncomingCtx;
836 VBOXSTRICTRC rcIem = IEMExecOneBypassEx(pVCpu, pRegFrame, NULL);
837 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
838 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
839 rcIem = VERR_EM_INTERPRETER;
840 g_IemCtx = *pCtx;
841 g_fIemFFs = pVCpu->fLocalForcedActions;
842 rc = rcIem;
843# endif
844
845# if defined(VBOX_COMPARE_IEM_LAST) || defined(VBOX_COMPARE_IEM_FIRST)
846 emCompareWithIem(pVCpu, &g_EmCtx, &g_IemCtx, rcEm, rcIem, 0, 0);
847# endif
848
849# else
850 VBOXSTRICTRC rc = IEMExecOneBypassEx(pVCpu, pRegFrame, NULL);
851 if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
852 || rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
853 rc = VERR_EM_INTERPRETER;
854# endif
855 if (rc != VINF_SUCCESS)
856 Log(("EMInterpretInstruction: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
857
858 return rc;
859#else
860 RTGCPTR pbCode;
861 VBOXSTRICTRC rc = SELMToFlatEx(pVCpu, DISSELREG_CS, pRegFrame, pRegFrame->rip, 0, &pbCode);
862 if (RT_SUCCESS(rc))
863 {
864 uint32_t cbOp;
865 PDISCPUSTATE pDis = &pVCpu->em.s.DisState;
866 pDis->uCpuMode = CPUMGetGuestDisMode(pVCpu);
867 rc = emDisCoreOne(pVCpu->CTX_SUFF(pVM), pVCpu, pDis, (RTGCUINTPTR)pbCode, &cbOp);
868 if (RT_SUCCESS(rc))
869 {
870 Assert(cbOp == pDis->cbInstr);
871 uint32_t cbIgnored;
872 rc = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_SUPERVISOR, &cbIgnored);
873 if (RT_SUCCESS(rc))
874 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
875
876 return rc;
877 }
878 }
879 return VERR_EM_INTERPRETER;
880#endif
881}
882
883
884/**
885 * Interprets the current instruction.
886 *
887 * @returns VBox status code.
888 * @retval VINF_* Scheduling instructions.
889 * @retval VERR_EM_INTERPRETER Something we can't cope with.
890 * @retval VERR_* Fatal errors.
891 *
892 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
893 * @param pRegFrame The register frame.
894 * Updates the EIP if an instruction was executed successfully.
895 * @param pvFault The fault address (CR2).
896 * @param pcbWritten Size of the write (if applicable).
897 *
898 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
899 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
900 * to worry about e.g. invalid modrm combinations (!)
901 */
902VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstructionEx(PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbWritten)
903{
904 LogFlow(("EMInterpretInstructionEx %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
905 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
906#ifdef VBOX_WITH_IEM
907 NOREF(pvFault);
908
909# ifdef VBOX_COMPARE_IEM_AND_EM
910 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
911 g_IncomingCtx = *pCtx;
912 g_fIncomingFFs = pVCpu->fLocalForcedActions;
913 g_cbEmWrote = g_cbIemWrote = 0;
914
915# ifdef VBOX_COMPARE_IEM_FIRST
916 /* IEM */
917 uint32_t cbIemWritten = 0;
918 VBOXSTRICTRC rcIem = IEMExecOneBypassEx(pVCpu, pRegFrame, &cbIemWritten);
919 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
920 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
921 rcIem = VERR_EM_INTERPRETER;
922 g_IemCtx = *pCtx;
923 g_fIemFFs = pVCpu->fLocalForcedActions;
924 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
925 *pCtx = g_IncomingCtx;
926# endif
927
928 /* EM */
929 uint32_t cbEmWritten = 0;
930 RTGCPTR pbCode;
931 VBOXSTRICTRC rcEm = SELMToFlatEx(pVCpu, DISSELREG_CS, pRegFrame, pRegFrame->rip, 0, &pbCode);
932 if (RT_SUCCESS(rcEm))
933 {
934 uint32_t cbOp;
935 PDISCPUSTATE pDis = &pVCpu->em.s.DisState;
936 pDis->uCpuMode = CPUMGetGuestDisMode(pVCpu);
937 rcEm = emDisCoreOne(pVCpu->CTX_SUFF(pVM), pVCpu, pDis, (RTGCUINTPTR)pbCode, &cbOp);
938 if (RT_SUCCESS(rcEm))
939 {
940 Assert(cbOp == pDis->cbInstr);
941 rcEm = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_SUPERVISOR, &cbEmWritten);
942 if (RT_SUCCESS(rcEm))
943 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
944
945 }
946 else
947 rcEm = VERR_EM_INTERPRETER;
948 }
949 else
950 rcEm = VERR_EM_INTERPRETER;
951# ifdef VBOX_SAME_AS_EM
952 if (rcEm == VERR_EM_INTERPRETER)
953 {
954 Log(("EMInterpretInstruction: returns %Rrc\n", VBOXSTRICTRC_VAL(rcEm)));
955 return rcEm;
956 }
957# endif
958 g_EmCtx = *pCtx;
959 g_fEmFFs = pVCpu->fLocalForcedActions;
960 *pcbWritten = cbEmWritten;
961 VBOXSTRICTRC rc = rcEm;
962
963# ifdef VBOX_COMPARE_IEM_LAST
964 /* IEM */
965 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
966 *pCtx = g_IncomingCtx;
967 uint32_t cbIemWritten = 0;
968 VBOXSTRICTRC rcIem = IEMExecOneBypassEx(pVCpu, pRegFrame, &cbIemWritten);
969 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
970 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
971 rcIem = VERR_EM_INTERPRETER;
972 g_IemCtx = *pCtx;
973 g_fIemFFs = pVCpu->fLocalForcedActions;
974 *pcbWritten = cbIemWritten;
975 rc = rcIem;
976# endif
977
978# if defined(VBOX_COMPARE_IEM_LAST) || defined(VBOX_COMPARE_IEM_FIRST)
979 emCompareWithIem(pVCpu, &g_EmCtx, &g_IemCtx, rcEm, rcIem, cbEmWritten, cbIemWritten);
980# endif
981
982# else
983 VBOXSTRICTRC rc = IEMExecOneBypassEx(pVCpu, pRegFrame, pcbWritten);
984 if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
985 || rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
986 rc = VERR_EM_INTERPRETER;
987# endif
988 if (rc != VINF_SUCCESS)
989 Log(("EMInterpretInstructionEx: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
990
991 return rc;
992#else
993 RTGCPTR pbCode;
994 VBOXSTRICTRC rc = SELMToFlatEx(pVCpu, DISSELREG_CS, pRegFrame, pRegFrame->rip, 0, &pbCode);
995 if (RT_SUCCESS(rc))
996 {
997 uint32_t cbOp;
998 PDISCPUSTATE pDis = &pVCpu->em.s.DisState;
999 pDis->uCpuMode = CPUMGetGuestDisMode(pVCpu);
1000 rc = emDisCoreOne(pVCpu->CTX_SUFF(pVM), pVCpu, pDis, (RTGCUINTPTR)pbCode, &cbOp);
1001 if (RT_SUCCESS(rc))
1002 {
1003 Assert(cbOp == pDis->cbInstr);
1004 rc = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_SUPERVISOR, pcbWritten);
1005 if (RT_SUCCESS(rc))
1006 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
1007
1008 return rc;
1009 }
1010 }
1011 return VERR_EM_INTERPRETER;
1012#endif
1013}
1014
1015
1016/**
1017 * Interprets the current instruction using the supplied DISCPUSTATE structure.
1018 *
1019 * IP/EIP/RIP *IS* updated!
1020 *
1021 * @returns VBox strict status code.
1022 * @retval VINF_* Scheduling instructions. When these are returned, it
1023 * starts to get a bit tricky to know whether code was
1024 * executed or not... We'll address this when it becomes a problem.
1025 * @retval VERR_EM_INTERPRETER Something we can't cope with.
1026 * @retval VERR_* Fatal errors.
1027 *
1028 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1029 * @param pDis The disassembler cpu state for the instruction to be
1030 * interpreted.
1031 * @param pRegFrame The register frame. IP/EIP/RIP *IS* changed!
1032 * @param pvFault The fault address (CR2).
1033 * @param enmCodeType Code type (user/supervisor)
1034 *
1035 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
1036 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
1037 * to worry about e.g. invalid modrm combinations (!)
1038 *
1039 * @todo At this time we do NOT check if the instruction overwrites vital information.
1040 * Make sure this can't happen!! (will add some assertions/checks later)
1041 */
1042VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInstructionDisasState(PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
1043 RTGCPTR pvFault, EMCODETYPE enmCodeType)
1044{
1045 LogFlow(("EMInterpretInstructionDisasState %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
1046 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1047#ifdef VBOX_WITH_IEM
1048 NOREF(pDis); NOREF(pvFault); NOREF(enmCodeType);
1049
1050# ifdef VBOX_COMPARE_IEM_AND_EM
1051 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1052 g_IncomingCtx = *pCtx;
1053 g_fIncomingFFs = pVCpu->fLocalForcedActions;
1054 g_cbEmWrote = g_cbIemWrote = 0;
1055
1056# ifdef VBOX_COMPARE_IEM_FIRST
1057 VBOXSTRICTRC rcIem = IEMExecOneBypassWithPrefetchedByPC(pVCpu, pRegFrame, pRegFrame->rip, pDis->abInstr, pDis->cbCachedInstr);
1058 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
1059 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
1060 rcIem = VERR_EM_INTERPRETER;
1061 g_IemCtx = *pCtx;
1062 g_fIemFFs = pVCpu->fLocalForcedActions;
1063 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
1064 *pCtx = g_IncomingCtx;
1065# endif
1066
1067 /* EM */
1068 uint32_t cbIgnored;
1069 VBOXSTRICTRC rcEm = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, enmCodeType, &cbIgnored);
1070 if (RT_SUCCESS(rcEm))
1071 pRegFrame->rip += pDis->cbInstr; /* Move on to the next instruction. */
1072# ifdef VBOX_SAME_AS_EM
1073 if (rcEm == VERR_EM_INTERPRETER)
1074 {
1075 Log(("EMInterpretInstruction: returns %Rrc\n", VBOXSTRICTRC_VAL(rcEm)));
1076 return rcEm;
1077 }
1078# endif
1079 g_EmCtx = *pCtx;
1080 g_fEmFFs = pVCpu->fLocalForcedActions;
1081 VBOXSTRICTRC rc = rcEm;
1082
1083# ifdef VBOX_COMPARE_IEM_LAST
1084 /* IEM */
1085 pVCpu->fLocalForcedActions = (pVCpu->fLocalForcedActions & ~g_fInterestingFFs) | (g_fIncomingFFs & g_fInterestingFFs);
1086 *pCtx = g_IncomingCtx;
1087 VBOXSTRICTRC rcIem = IEMExecOneBypassWithPrefetchedByPC(pVCpu, pRegFrame, pRegFrame->rip, pDis->abInstr, pDis->cbCachedInstr);
1088 if (RT_UNLIKELY( rcIem == VERR_IEM_ASPECT_NOT_IMPLEMENTED
1089 || rcIem == VERR_IEM_INSTR_NOT_IMPLEMENTED))
1090 rcIem = VERR_EM_INTERPRETER;
1091 g_IemCtx = *pCtx;
1092 g_fIemFFs = pVCpu->fLocalForcedActions;
1093 rc = rcIem;
1094# endif
1095
1096# if defined(VBOX_COMPARE_IEM_LAST) || defined(VBOX_COMPARE_IEM_FIRST)
1097 emCompareWithIem(pVCpu, &g_EmCtx, &g_IemCtx, rcEm, rcIem, 0, 0);
1098# endif
1099
1100# else
1101 VBOXSTRICTRC rc = IEMExecOneBypassWithPrefetchedByPC(pVCpu, pRegFrame, pRegFrame->rip, pDis->abInstr, pDis->cbCachedInstr);
1102 if (RT_UNLIKELY( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
1103 || rc == VERR_IEM_INSTR_NOT_IMPLEMENTED))
1104 rc = VERR_EM_INTERPRETER;
1105# endif
1106
1107 if (rc != VINF_SUCCESS)
1108 Log(("EMInterpretInstructionDisasState: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
1109
1110 return rc;
1111#else
1112 uint32_t cbIgnored;
1113 VBOXSTRICTRC rc = emInterpretInstructionCPUOuter(pVCpu, pDis, pRegFrame, pvFault, enmCodeType, &cbIgnored);
1114 if (RT_SUCCESS(rc))
1115 pRegFrame->rip += pDis->cbInstr; /* Move on to the next instruction. */
1116 return rc;
1117#endif
1118}
1119
1120#ifdef IN_RC
1121
1122DECLINLINE(int) emRCStackRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCPTR GCPtrSrc, uint32_t cb)
1123{
1124 int rc = MMGCRamRead(pVM, pvDst, (void *)(uintptr_t)GCPtrSrc, cb);
1125 if (RT_LIKELY(rc != VERR_ACCESS_DENIED))
1126 return rc;
1127 return PGMPhysInterpretedReadNoHandlers(pVCpu, pCtxCore, pvDst, GCPtrSrc, cb, /*fMayTrap*/ false);
1128}
1129
1130
1131/**
1132 * Interpret IRET (currently only to V86 code) - PATM only.
1133 *
1134 * @returns VBox status code.
1135 * @param pVM The cross context VM structure.
1136 * @param pVCpu The cross context virtual CPU structure.
1137 * @param pRegFrame The register frame.
1138 *
1139 */
1140VMM_INT_DECL(int) EMInterpretIretV86ForPatm(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1141{
1142 RTGCUINTPTR pIretStack = (RTGCUINTPTR)pRegFrame->esp;
1143 RTGCUINTPTR eip, cs, esp, ss, eflags, ds, es, fs, gs, uMask;
1144 int rc;
1145
1146 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1147 Assert(!CPUMIsGuestIn64BitCode(pVCpu));
1148 /** @todo Rainy day: Test what happens when VERR_EM_INTERPRETER is returned by
1149 * this function. Fear that it may guru on us, thus not converted to
1150 * IEM. */
1151
1152 rc = emRCStackRead(pVM, pVCpu, pRegFrame, &eip, (RTGCPTR)pIretStack , 4);
1153 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &cs, (RTGCPTR)(pIretStack + 4), 4);
1154 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &eflags, (RTGCPTR)(pIretStack + 8), 4);
1155 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1156 AssertReturn(eflags & X86_EFL_VM, VERR_EM_INTERPRETER);
1157
1158 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &esp, (RTGCPTR)(pIretStack + 12), 4);
1159 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &ss, (RTGCPTR)(pIretStack + 16), 4);
1160 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &es, (RTGCPTR)(pIretStack + 20), 4);
1161 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &ds, (RTGCPTR)(pIretStack + 24), 4);
1162 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &fs, (RTGCPTR)(pIretStack + 28), 4);
1163 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &gs, (RTGCPTR)(pIretStack + 32), 4);
1164 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1165
1166 pRegFrame->eip = eip & 0xffff;
1167 pRegFrame->cs.Sel = cs;
1168
1169 /* Mask away all reserved bits */
1170 uMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT | X86_EFL_RF | X86_EFL_VM | X86_EFL_AC | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_ID;
1171 eflags &= uMask;
1172
1173 CPUMRawSetEFlags(pVCpu, eflags);
1174 Assert((pRegFrame->eflags.u32 & (X86_EFL_IF|X86_EFL_IOPL)) == X86_EFL_IF);
1175
1176 pRegFrame->esp = esp;
1177 pRegFrame->ss.Sel = ss;
1178 pRegFrame->ds.Sel = ds;
1179 pRegFrame->es.Sel = es;
1180 pRegFrame->fs.Sel = fs;
1181 pRegFrame->gs.Sel = gs;
1182
1183 return VINF_SUCCESS;
1184}
1185
1186# ifndef VBOX_WITH_IEM
1187/**
1188 * IRET Emulation.
1189 */
1190static int emInterpretIret(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1191{
1192#ifdef VBOX_WITH_RAW_RING1
1193 NOREF(pvFault); NOREF(pcbSize); NOREF(pDis);
1194 if (EMIsRawRing1Enabled(pVM))
1195 {
1196 RTGCUINTPTR pIretStack = (RTGCUINTPTR)pRegFrame->esp;
1197 RTGCUINTPTR eip, cs, esp, ss, eflags, uMask;
1198 int rc;
1199 uint32_t cpl, rpl;
1200
1201 /* We only execute 32-bits protected mode code in raw mode, so no need to bother to check for 16-bits code here. */
1202 /** @todo we don't verify all the edge cases that generate #GP faults */
1203
1204 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1205 Assert(!CPUMIsGuestIn64BitCode(pVCpu));
1206 /** @todo Rainy day: Test what happens when VERR_EM_INTERPRETER is returned by
1207 * this function. Fear that it may guru on us, thus not converted to
1208 * IEM. */
1209
1210 rc = emRCStackRead(pVM, pVCpu, pRegFrame, &eip, (RTGCPTR)pIretStack , 4);
1211 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &cs, (RTGCPTR)(pIretStack + 4), 4);
1212 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &eflags, (RTGCPTR)(pIretStack + 8), 4);
1213 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1214 AssertReturn(eflags & X86_EFL_VM, VERR_EM_INTERPRETER);
1215
1216 /* Deal with V86 above. */
1217 if (eflags & X86_EFL_VM)
1218 return EMInterpretIretV86ForPatm(pVM, pVCpu, pRegFrame);
1219
1220 cpl = CPUMRCGetGuestCPL(pVCpu, pRegFrame);
1221 rpl = cs & X86_SEL_RPL;
1222
1223 Log(("emInterpretIret: iret to CS:EIP=%04X:%08X eflags=%x\n", cs, eip, eflags));
1224 if (rpl != cpl)
1225 {
1226 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &esp, (RTGCPTR)(pIretStack + 12), 4);
1227 rc |= emRCStackRead(pVM, pVCpu, pRegFrame, &ss, (RTGCPTR)(pIretStack + 16), 4);
1228 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1229 Log(("emInterpretIret: return to different privilege level (rpl=%d cpl=%d)\n", rpl, cpl));
1230 Log(("emInterpretIret: SS:ESP=%04x:%08x\n", ss, esp));
1231 pRegFrame->ss.Sel = ss;
1232 pRegFrame->esp = esp;
1233 }
1234 pRegFrame->cs.Sel = cs;
1235 pRegFrame->eip = eip;
1236
1237 /* Adjust CS & SS as required. */
1238 CPUMRCRecheckRawState(pVCpu, pRegFrame);
1239
1240 /* Mask away all reserved bits */
1241 uMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT | X86_EFL_RF | X86_EFL_VM | X86_EFL_AC | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_ID;
1242 eflags &= uMask;
1243
1244 CPUMRawSetEFlags(pVCpu, eflags);
1245 Assert((pRegFrame->eflags.u32 & (X86_EFL_IF|X86_EFL_IOPL)) == X86_EFL_IF);
1246 return VINF_SUCCESS;
1247 }
1248#else
1249 NOREF(pVM); NOREF(pVCpu); NOREF(pDis); NOREF(pRegFrame); NOREF(pvFault); NOREF(pcbSize);
1250#endif
1251 return VERR_EM_INTERPRETER;
1252}
1253# endif /* !VBOX_WITH_IEM */
1254
1255#endif /* IN_RC */
1256
1257
1258
1259/*
1260 *
1261 * Old interpreter primitives used by HM, move/eliminate later.
1262 * Old interpreter primitives used by HM, move/eliminate later.
1263 * Old interpreter primitives used by HM, move/eliminate later.
1264 * Old interpreter primitives used by HM, move/eliminate later.
1265 * Old interpreter primitives used by HM, move/eliminate later.
1266 *
1267 */
1268
1269
1270/**
1271 * Interpret CPUID given the parameters in the CPU context.
1272 *
1273 * @returns VBox status code.
1274 * @param pVM The cross context VM structure.
1275 * @param pVCpu The cross context virtual CPU structure.
1276 * @param pRegFrame The register frame.
1277 *
1278 */
1279VMM_INT_DECL(int) EMInterpretCpuId(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1280{
1281 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1282 uint32_t iLeaf = pRegFrame->eax;
1283 uint32_t iSubLeaf = pRegFrame->ecx;
1284 NOREF(pVM);
1285
1286 /* cpuid clears the high dwords of the affected 64 bits registers. */
1287 pRegFrame->rax = 0;
1288 pRegFrame->rbx = 0;
1289 pRegFrame->rcx = 0;
1290 pRegFrame->rdx = 0;
1291
1292 /* Note: operates the same in 64 and non-64 bits mode. */
1293 CPUMGetGuestCpuId(pVCpu, iLeaf, iSubLeaf, &pRegFrame->eax, &pRegFrame->ebx, &pRegFrame->ecx, &pRegFrame->edx);
1294 Log(("Emulate: CPUID %x -> %08x %08x %08x %08x\n", iLeaf, pRegFrame->eax, pRegFrame->ebx, pRegFrame->ecx, pRegFrame->edx));
1295 return VINF_SUCCESS;
1296}
1297
1298
1299/**
1300 * Interpret RDTSC.
1301 *
1302 * @returns VBox status code.
1303 * @param pVM The cross context VM structure.
1304 * @param pVCpu The cross context virtual CPU structure.
1305 * @param pRegFrame The register frame.
1306 *
1307 */
1308VMM_INT_DECL(int) EMInterpretRdtsc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1309{
1310 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1311 unsigned uCR4 = CPUMGetGuestCR4(pVCpu);
1312
1313 if (uCR4 & X86_CR4_TSD)
1314 return VERR_EM_INTERPRETER; /* genuine #GP */
1315
1316 uint64_t uTicks = TMCpuTickGet(pVCpu);
1317
1318 /* Same behaviour in 32 & 64 bits mode */
1319 pRegFrame->rax = RT_LO_U32(uTicks);
1320 pRegFrame->rdx = RT_HI_U32(uTicks);
1321#ifdef VBOX_COMPARE_IEM_AND_EM
1322 g_fIgnoreRaxRdx = true;
1323#endif
1324
1325 NOREF(pVM);
1326 return VINF_SUCCESS;
1327}
1328
1329/**
1330 * Interpret RDTSCP.
1331 *
1332 * @returns VBox status code.
1333 * @param pVM The cross context VM structure.
1334 * @param pVCpu The cross context virtual CPU structure.
1335 * @param pCtx The CPU context.
1336 *
1337 */
1338VMM_INT_DECL(int) EMInterpretRdtscp(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1339{
1340 Assert(pCtx == CPUMQueryGuestCtxPtr(pVCpu));
1341 uint32_t uCR4 = CPUMGetGuestCR4(pVCpu);
1342
1343 if (!pVM->cpum.ro.GuestFeatures.fRdTscP)
1344 {
1345 AssertFailed();
1346 return VERR_EM_INTERPRETER; /* genuine #UD */
1347 }
1348
1349 if (uCR4 & X86_CR4_TSD)
1350 return VERR_EM_INTERPRETER; /* genuine #GP */
1351
1352 uint64_t uTicks = TMCpuTickGet(pVCpu);
1353
1354 /* Same behaviour in 32 & 64 bits mode */
1355 pCtx->rax = RT_LO_U32(uTicks);
1356 pCtx->rdx = RT_HI_U32(uTicks);
1357#ifdef VBOX_COMPARE_IEM_AND_EM
1358 g_fIgnoreRaxRdx = true;
1359#endif
1360 /* Low dword of the TSC_AUX msr only. */
1361 VBOXSTRICTRC rc2 = CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pCtx->rcx); Assert(rc2 == VINF_SUCCESS); NOREF(rc2);
1362 pCtx->rcx &= UINT32_C(0xffffffff);
1363
1364 return VINF_SUCCESS;
1365}
1366
1367/**
1368 * Interpret RDPMC.
1369 *
1370 * @returns VBox status code.
1371 * @param pVM The cross context VM structure.
1372 * @param pVCpu The cross context virtual CPU structure.
1373 * @param pRegFrame The register frame.
1374 *
1375 */
1376VMM_INT_DECL(int) EMInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1377{
1378 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1379 uint32_t uCR4 = CPUMGetGuestCR4(pVCpu);
1380
1381 /* If X86_CR4_PCE is not set, then CPL must be zero. */
1382 if ( !(uCR4 & X86_CR4_PCE)
1383 && CPUMGetGuestCPL(pVCpu) != 0)
1384 {
1385 Assert(CPUMGetGuestCR0(pVCpu) & X86_CR0_PE);
1386 return VERR_EM_INTERPRETER; /* genuine #GP */
1387 }
1388
1389 /* Just return zero here; rather tricky to properly emulate this, especially as the specs are a mess. */
1390 pRegFrame->rax = 0;
1391 pRegFrame->rdx = 0;
1392 /** @todo We should trigger a \#GP here if the CPU doesn't support the index in
1393 * ecx but see @bugref{3472}! */
1394
1395 NOREF(pVM);
1396 return VINF_SUCCESS;
1397}
1398
1399
1400/**
1401 * MWAIT Emulation.
1402 */
1403VMM_INT_DECL(VBOXSTRICTRC) EMInterpretMWait(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1404{
1405 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1406 uint32_t u32Dummy, u32ExtFeatures, cpl, u32MWaitFeatures;
1407 NOREF(pVM);
1408
1409 /* Get the current privilege level. */
1410 cpl = CPUMGetGuestCPL(pVCpu);
1411 if (cpl != 0)
1412 return VERR_EM_INTERPRETER; /* supervisor only */
1413
1414 CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
1415 if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
1416 return VERR_EM_INTERPRETER; /* not supported */
1417
1418 /*
1419 * CPUID.05H.ECX[0] defines support for power management extensions (eax)
1420 * CPUID.05H.ECX[1] defines support for interrupts as break events for mwait even when IF=0
1421 */
1422 CPUMGetGuestCpuId(pVCpu, 5, 0, &u32Dummy, &u32Dummy, &u32MWaitFeatures, &u32Dummy);
1423 if (pRegFrame->ecx > 1)
1424 {
1425 Log(("EMInterpretMWait: unexpected ecx value %x -> recompiler\n", pRegFrame->ecx));
1426 return VERR_EM_INTERPRETER; /* illegal value. */
1427 }
1428
1429 if (pRegFrame->ecx && !(u32MWaitFeatures & X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
1430 {
1431 Log(("EMInterpretMWait: unsupported X86_CPUID_MWAIT_ECX_BREAKIRQIF0 -> recompiler\n"));
1432 return VERR_EM_INTERPRETER; /* illegal value. */
1433 }
1434
1435 return EMMonitorWaitPerform(pVCpu, pRegFrame->rax, pRegFrame->rcx);
1436}
1437
1438
1439/**
1440 * MONITOR Emulation.
1441 */
1442VMM_INT_DECL(int) EMInterpretMonitor(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1443{
1444 uint32_t u32Dummy, u32ExtFeatures, cpl;
1445 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1446 NOREF(pVM);
1447
1448 if (pRegFrame->ecx != 0)
1449 {
1450 Log(("emInterpretMonitor: unexpected ecx=%x -> recompiler!!\n", pRegFrame->ecx));
1451 return VERR_EM_INTERPRETER; /* illegal value. */
1452 }
1453
1454 /* Get the current privilege level. */
1455 cpl = CPUMGetGuestCPL(pVCpu);
1456 if (cpl != 0)
1457 return VERR_EM_INTERPRETER; /* supervisor only */
1458
1459 CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
1460 if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
1461 return VERR_EM_INTERPRETER; /* not supported */
1462
1463 EMMonitorWaitPrepare(pVCpu, pRegFrame->rax, pRegFrame->rcx, pRegFrame->rdx, NIL_RTGCPHYS);
1464 return VINF_SUCCESS;
1465}
1466
1467
1468/* VT-x only: */
1469
1470/**
1471 * Interpret INVLPG.
1472 *
1473 * @returns VBox status code.
1474 * @param pVM The cross context VM structure.
1475 * @param pVCpu The cross context virtual CPU structure.
1476 * @param pRegFrame The register frame.
1477 * @param pAddrGC Operand address.
1478 *
1479 */
1480VMM_INT_DECL(VBOXSTRICTRC) EMInterpretInvlpg(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pAddrGC)
1481{
1482 /** @todo is addr always a flat linear address or ds based
1483 * (in absence of segment override prefixes)????
1484 */
1485 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1486 NOREF(pVM); NOREF(pRegFrame);
1487#ifdef IN_RC
1488 LogFlow(("RC: EMULATE: invlpg %RGv\n", pAddrGC));
1489#endif
1490 VBOXSTRICTRC rc = PGMInvalidatePage(pVCpu, pAddrGC);
1491 if ( rc == VINF_SUCCESS
1492 || rc == VINF_PGM_SYNC_CR3 /* we can rely on the FF */)
1493 return VINF_SUCCESS;
1494 AssertMsgReturn(rc == VINF_EM_RAW_EMULATE_INSTR,
1495 ("%Rrc addr=%RGv\n", VBOXSTRICTRC_VAL(rc), pAddrGC),
1496 VERR_EM_INTERPRETER);
1497 return rc;
1498}
1499
1500
1501#ifdef LOG_ENABLED
1502static const char *emMSRtoString(uint32_t uMsr)
1503{
1504 switch (uMsr)
1505 {
1506 case MSR_IA32_APICBASE: return "MSR_IA32_APICBASE";
1507 case MSR_IA32_CR_PAT: return "MSR_IA32_CR_PAT";
1508 case MSR_IA32_SYSENTER_CS: return "MSR_IA32_SYSENTER_CS";
1509 case MSR_IA32_SYSENTER_EIP: return "MSR_IA32_SYSENTER_EIP";
1510 case MSR_IA32_SYSENTER_ESP: return "MSR_IA32_SYSENTER_ESP";
1511 case MSR_K6_EFER: return "MSR_K6_EFER";
1512 case MSR_K8_SF_MASK: return "MSR_K8_SF_MASK";
1513 case MSR_K6_STAR: return "MSR_K6_STAR";
1514 case MSR_K8_LSTAR: return "MSR_K8_LSTAR";
1515 case MSR_K8_CSTAR: return "MSR_K8_CSTAR";
1516 case MSR_K8_FS_BASE: return "MSR_K8_FS_BASE";
1517 case MSR_K8_GS_BASE: return "MSR_K8_GS_BASE";
1518 case MSR_K8_KERNEL_GS_BASE: return "MSR_K8_KERNEL_GS_BASE";
1519 case MSR_K8_TSC_AUX: return "MSR_K8_TSC_AUX";
1520 case MSR_IA32_BIOS_SIGN_ID: return "Unsupported MSR_IA32_BIOS_SIGN_ID";
1521 case MSR_IA32_PLATFORM_ID: return "Unsupported MSR_IA32_PLATFORM_ID";
1522 case MSR_IA32_BIOS_UPDT_TRIG: return "Unsupported MSR_IA32_BIOS_UPDT_TRIG";
1523 case MSR_IA32_TSC: return "MSR_IA32_TSC";
1524 case MSR_IA32_MISC_ENABLE: return "MSR_IA32_MISC_ENABLE";
1525 case MSR_IA32_MTRR_CAP: return "MSR_IA32_MTRR_CAP";
1526 case MSR_IA32_MCG_CAP: return "Unsupported MSR_IA32_MCG_CAP";
1527 case MSR_IA32_MCG_STATUS: return "Unsupported MSR_IA32_MCG_STATUS";
1528 case MSR_IA32_MCG_CTRL: return "Unsupported MSR_IA32_MCG_CTRL";
1529 case MSR_IA32_MTRR_DEF_TYPE: return "MSR_IA32_MTRR_DEF_TYPE";
1530 case MSR_K7_EVNTSEL0: return "Unsupported MSR_K7_EVNTSEL0";
1531 case MSR_K7_EVNTSEL1: return "Unsupported MSR_K7_EVNTSEL1";
1532 case MSR_K7_EVNTSEL2: return "Unsupported MSR_K7_EVNTSEL2";
1533 case MSR_K7_EVNTSEL3: return "Unsupported MSR_K7_EVNTSEL3";
1534 case MSR_IA32_MC0_CTL: return "Unsupported MSR_IA32_MC0_CTL";
1535 case MSR_IA32_MC0_STATUS: return "Unsupported MSR_IA32_MC0_STATUS";
1536 case MSR_IA32_PERFEVTSEL0: return "Unsupported MSR_IA32_PERFEVTSEL0";
1537 case MSR_IA32_PERFEVTSEL1: return "Unsupported MSR_IA32_PERFEVTSEL1";
1538 case MSR_IA32_PERF_STATUS: return "MSR_IA32_PERF_STATUS";
1539 case MSR_IA32_PLATFORM_INFO: return "MSR_IA32_PLATFORM_INFO";
1540 case MSR_IA32_PERF_CTL: return "Unsupported MSR_IA32_PERF_CTL";
1541 case MSR_K7_PERFCTR0: return "Unsupported MSR_K7_PERFCTR0";
1542 case MSR_K7_PERFCTR1: return "Unsupported MSR_K7_PERFCTR1";
1543 case MSR_K7_PERFCTR2: return "Unsupported MSR_K7_PERFCTR2";
1544 case MSR_K7_PERFCTR3: return "Unsupported MSR_K7_PERFCTR3";
1545 case MSR_IA32_PMC0: return "Unsupported MSR_IA32_PMC0";
1546 case MSR_IA32_PMC1: return "Unsupported MSR_IA32_PMC1";
1547 case MSR_IA32_PMC2: return "Unsupported MSR_IA32_PMC2";
1548 case MSR_IA32_PMC3: return "Unsupported MSR_IA32_PMC3";
1549 }
1550 return "Unknown MSR";
1551}
1552#endif /* LOG_ENABLED */
1553
1554
1555/**
1556 * Interpret RDMSR
1557 *
1558 * @returns VBox status code.
1559 * @param pVM The cross context VM structure.
1560 * @param pVCpu The cross context virtual CPU structure.
1561 * @param pRegFrame The register frame.
1562 */
1563VMM_INT_DECL(int) EMInterpretRdmsr(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1564{
1565 NOREF(pVM);
1566
1567 /* Get the current privilege level. */
1568 if (CPUMGetGuestCPL(pVCpu) != 0)
1569 {
1570 Log4(("EM: Refuse RDMSR: CPL != 0\n"));
1571 return VERR_EM_INTERPRETER; /* supervisor only */
1572 }
1573
1574 uint64_t uValue;
1575 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pRegFrame->ecx, &uValue);
1576 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
1577 {
1578 Log4(("EM: Refuse RDMSR: rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1579 Assert(rcStrict == VERR_CPUM_RAISE_GP_0 || rcStrict == VERR_EM_INTERPRETER || rcStrict == VINF_CPUM_R3_MSR_READ);
1580 return VERR_EM_INTERPRETER;
1581 }
1582 pRegFrame->rax = RT_LO_U32(uValue);
1583 pRegFrame->rdx = RT_HI_U32(uValue);
1584 LogFlow(("EMInterpretRdmsr %s (%x) -> %RX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx, uValue));
1585 return VINF_SUCCESS;
1586}
1587
1588
1589/**
1590 * Interpret WRMSR
1591 *
1592 * @returns VBox status code.
1593 * @param pVM The cross context VM structure.
1594 * @param pVCpu The cross context virtual CPU structure.
1595 * @param pRegFrame The register frame.
1596 */
1597VMM_INT_DECL(int) EMInterpretWrmsr(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1598{
1599 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1600
1601 /* Check the current privilege level, this instruction is supervisor only. */
1602 if (CPUMGetGuestCPL(pVCpu) != 0)
1603 {
1604 Log4(("EM: Refuse WRMSR: CPL != 0\n"));
1605 return VERR_EM_INTERPRETER; /** @todo raise \#GP(0) */
1606 }
1607
1608 VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, pRegFrame->ecx, RT_MAKE_U64(pRegFrame->eax, pRegFrame->edx));
1609 if (rcStrict != VINF_SUCCESS)
1610 {
1611 Log4(("EM: Refuse WRMSR: CPUMSetGuestMsr returned %Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1612 Assert(rcStrict == VERR_CPUM_RAISE_GP_0 || rcStrict == VERR_EM_INTERPRETER || rcStrict == VINF_CPUM_R3_MSR_WRITE);
1613 return VERR_EM_INTERPRETER;
1614 }
1615 LogFlow(("EMInterpretWrmsr %s (%x) val=%RX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx,
1616 RT_MAKE_U64(pRegFrame->eax, pRegFrame->edx)));
1617 NOREF(pVM);
1618 return VINF_SUCCESS;
1619}
1620
1621
1622/**
1623 * Interpret DRx write.
1624 *
1625 * @returns VBox status code.
1626 * @param pVM The cross context VM structure.
1627 * @param pVCpu The cross context virtual CPU structure.
1628 * @param pRegFrame The register frame.
1629 * @param DestRegDrx DRx register index (USE_REG_DR*)
1630 * @param SrcRegGen General purpose register index (USE_REG_E**))
1631 *
1632 */
1633VMM_INT_DECL(int) EMInterpretDRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen)
1634{
1635 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1636 uint64_t uNewDrX;
1637 int rc;
1638 NOREF(pVM);
1639
1640 if (CPUMIsGuestIn64BitCode(pVCpu))
1641 rc = DISFetchReg64(pRegFrame, SrcRegGen, &uNewDrX);
1642 else
1643 {
1644 uint32_t val32;
1645 rc = DISFetchReg32(pRegFrame, SrcRegGen, &val32);
1646 uNewDrX = val32;
1647 }
1648
1649 if (RT_SUCCESS(rc))
1650 {
1651 if (DestRegDrx == 6)
1652 {
1653 uNewDrX |= X86_DR6_RA1_MASK;
1654 uNewDrX &= ~X86_DR6_RAZ_MASK;
1655 }
1656 else if (DestRegDrx == 7)
1657 {
1658 uNewDrX |= X86_DR7_RA1_MASK;
1659 uNewDrX &= ~X86_DR7_RAZ_MASK;
1660 }
1661
1662 /** @todo we don't fail if illegal bits are set/cleared for e.g. dr7 */
1663 rc = CPUMSetGuestDRx(pVCpu, DestRegDrx, uNewDrX);
1664 if (RT_SUCCESS(rc))
1665 return rc;
1666 AssertMsgFailed(("CPUMSetGuestDRx %d failed\n", DestRegDrx));
1667 }
1668 return VERR_EM_INTERPRETER;
1669}
1670
1671
1672/**
1673 * Interpret DRx read.
1674 *
1675 * @returns VBox status code.
1676 * @param pVM The cross context VM structure.
1677 * @param pVCpu The cross context virtual CPU structure.
1678 * @param pRegFrame The register frame.
1679 * @param DestRegGen General purpose register index (USE_REG_E**))
1680 * @param SrcRegDrx DRx register index (USE_REG_DR*)
1681 */
1682VMM_INT_DECL(int) EMInterpretDRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx)
1683{
1684 uint64_t val64;
1685 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
1686 NOREF(pVM);
1687
1688 int rc = CPUMGetGuestDRx(pVCpu, SrcRegDrx, &val64);
1689 AssertMsgRCReturn(rc, ("CPUMGetGuestDRx %d failed\n", SrcRegDrx), VERR_EM_INTERPRETER);
1690 if (CPUMIsGuestIn64BitCode(pVCpu))
1691 rc = DISWriteReg64(pRegFrame, DestRegGen, val64);
1692 else
1693 rc = DISWriteReg32(pRegFrame, DestRegGen, (uint32_t)val64);
1694
1695 if (RT_SUCCESS(rc))
1696 return VINF_SUCCESS;
1697
1698 return VERR_EM_INTERPRETER;
1699}
1700
1701
1702#if !defined(VBOX_WITH_IEM) || defined(VBOX_COMPARE_IEM_AND_EM)
1703
1704
1705
1706
1707
1708
1709/*
1710 *
1711 * The old interpreter.
1712 * The old interpreter.
1713 * The old interpreter.
1714 * The old interpreter.
1715 * The old interpreter.
1716 *
1717 */
1718
1719DECLINLINE(int) emRamRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCPTR GCPtrSrc, uint32_t cb)
1720{
1721#ifdef IN_RC
1722 int rc = MMGCRamRead(pVM, pvDst, (void *)(uintptr_t)GCPtrSrc, cb);
1723 if (RT_LIKELY(rc != VERR_ACCESS_DENIED))
1724 return rc;
1725 /*
1726 * The page pool cache may end up here in some cases because it
1727 * flushed one of the shadow mappings used by the trapping
1728 * instruction and it either flushed the TLB or the CPU reused it.
1729 */
1730#else
1731 NOREF(pVM);
1732#endif
1733 return PGMPhysInterpretedReadNoHandlers(pVCpu, pCtxCore, pvDst, GCPtrSrc, cb, /*fMayTrap*/ false);
1734}
1735
1736
1737DECLINLINE(int) emRamWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, uint32_t cb)
1738{
1739 /* Don't use MMGCRamWrite here as it does not respect zero pages, shared
1740 pages or write monitored pages. */
1741 NOREF(pVM);
1742#if !defined(VBOX_COMPARE_IEM_AND_EM) || !defined(VBOX_COMPARE_IEM_LAST)
1743 int rc = PGMPhysInterpretedWriteNoHandlers(pVCpu, pCtxCore, GCPtrDst, pvSrc, cb, /*fMayTrap*/ false);
1744#else
1745 int rc = VINF_SUCCESS;
1746#endif
1747#ifdef VBOX_COMPARE_IEM_AND_EM
1748 Log(("EM Wrote: %RGv %.*Rhxs rc=%Rrc\n", GCPtrDst, RT_MAX(RT_MIN(cb, 64), 1), pvSrc, rc));
1749 g_cbEmWrote = cb;
1750 memcpy(g_abEmWrote, pvSrc, RT_MIN(cb, sizeof(g_abEmWrote)));
1751#endif
1752 return rc;
1753}
1754
1755
1756/** Convert sel:addr to a flat GC address. */
1757DECLINLINE(RTGCPTR) emConvertToFlatAddr(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, PDISOPPARAM pParam, RTGCPTR pvAddr)
1758{
1759 DISSELREG enmPrefixSeg = DISDetectSegReg(pDis, pParam);
1760 return SELMToFlat(pVM, enmPrefixSeg, pRegFrame, pvAddr);
1761}
1762
1763
1764#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
1765/**
1766 * Get the mnemonic for the disassembled instruction.
1767 *
1768 * GC/R0 doesn't include the strings in the DIS tables because
1769 * of limited space.
1770 */
1771static const char *emGetMnemonic(PDISCPUSTATE pDis)
1772{
1773 switch (pDis->pCurInstr->uOpcode)
1774 {
1775 case OP_XCHG: return "Xchg";
1776 case OP_DEC: return "Dec";
1777 case OP_INC: return "Inc";
1778 case OP_POP: return "Pop";
1779 case OP_OR: return "Or";
1780 case OP_AND: return "And";
1781 case OP_MOV: return "Mov";
1782 case OP_INVLPG: return "InvlPg";
1783 case OP_CPUID: return "CpuId";
1784 case OP_MOV_CR: return "MovCRx";
1785 case OP_MOV_DR: return "MovDRx";
1786 case OP_LLDT: return "LLdt";
1787 case OP_LGDT: return "LGdt";
1788 case OP_LIDT: return "LIdt";
1789 case OP_CLTS: return "Clts";
1790 case OP_MONITOR: return "Monitor";
1791 case OP_MWAIT: return "MWait";
1792 case OP_RDMSR: return "Rdmsr";
1793 case OP_WRMSR: return "Wrmsr";
1794 case OP_ADD: return "Add";
1795 case OP_ADC: return "Adc";
1796 case OP_SUB: return "Sub";
1797 case OP_SBB: return "Sbb";
1798 case OP_RDTSC: return "Rdtsc";
1799 case OP_STI: return "Sti";
1800 case OP_CLI: return "Cli";
1801 case OP_XADD: return "XAdd";
1802 case OP_HLT: return "Hlt";
1803 case OP_IRET: return "Iret";
1804 case OP_MOVNTPS: return "MovNTPS";
1805 case OP_STOSWD: return "StosWD";
1806 case OP_WBINVD: return "WbInvd";
1807 case OP_XOR: return "Xor";
1808 case OP_BTR: return "Btr";
1809 case OP_BTS: return "Bts";
1810 case OP_BTC: return "Btc";
1811 case OP_LMSW: return "Lmsw";
1812 case OP_SMSW: return "Smsw";
1813 case OP_CMPXCHG: return pDis->fPrefix & DISPREFIX_LOCK ? "Lock CmpXchg" : "CmpXchg";
1814 case OP_CMPXCHG8B: return pDis->fPrefix & DISPREFIX_LOCK ? "Lock CmpXchg8b" : "CmpXchg8b";
1815
1816 default:
1817 Log(("Unknown opcode %d\n", pDis->pCurInstr->uOpcode));
1818 return "???";
1819 }
1820}
1821#endif /* VBOX_STRICT || LOG_ENABLED */
1822
1823
1824/**
1825 * XCHG instruction emulation.
1826 */
1827static int emInterpretXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1828{
1829 DISQPVPARAMVAL param1, param2;
1830 NOREF(pvFault);
1831
1832 /* Source to make DISQueryParamVal read the register value - ugly hack */
1833 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
1834 if(RT_FAILURE(rc))
1835 return VERR_EM_INTERPRETER;
1836
1837 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
1838 if(RT_FAILURE(rc))
1839 return VERR_EM_INTERPRETER;
1840
1841#ifdef IN_RC
1842 if (TRPMHasTrap(pVCpu))
1843 {
1844 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
1845 {
1846#endif
1847 RTGCPTR pParam1 = 0, pParam2 = 0;
1848 uint64_t valpar1, valpar2;
1849
1850 AssertReturn(pDis->Param1.cb == pDis->Param2.cb, VERR_EM_INTERPRETER);
1851 switch(param1.type)
1852 {
1853 case DISQPV_TYPE_IMMEDIATE: /* register type is translated to this one too */
1854 valpar1 = param1.val.val64;
1855 break;
1856
1857 case DISQPV_TYPE_ADDRESS:
1858 pParam1 = (RTGCPTR)param1.val.val64;
1859 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
1860 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
1861 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
1862 if (RT_FAILURE(rc))
1863 {
1864 AssertMsgFailed(("MMGCRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1865 return VERR_EM_INTERPRETER;
1866 }
1867 break;
1868
1869 default:
1870 AssertFailed();
1871 return VERR_EM_INTERPRETER;
1872 }
1873
1874 switch(param2.type)
1875 {
1876 case DISQPV_TYPE_ADDRESS:
1877 pParam2 = (RTGCPTR)param2.val.val64;
1878 pParam2 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param2, pParam2);
1879 EM_ASSERT_FAULT_RETURN(pParam2 == pvFault, VERR_EM_INTERPRETER);
1880 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar2, pParam2, param2.size);
1881 if (RT_FAILURE(rc))
1882 {
1883 AssertMsgFailed(("MMGCRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1884 }
1885 break;
1886
1887 case DISQPV_TYPE_IMMEDIATE:
1888 valpar2 = param2.val.val64;
1889 break;
1890
1891 default:
1892 AssertFailed();
1893 return VERR_EM_INTERPRETER;
1894 }
1895
1896 /* Write value of parameter 2 to parameter 1 (reg or memory address) */
1897 if (pParam1 == 0)
1898 {
1899 Assert(param1.type == DISQPV_TYPE_IMMEDIATE); /* register actually */
1900 switch(param1.size)
1901 {
1902 case 1: //special case for AH etc
1903 rc = DISWriteReg8(pRegFrame, pDis->Param1.Base.idxGenReg, (uint8_t )valpar2); break;
1904 case 2: rc = DISWriteReg16(pRegFrame, pDis->Param1.Base.idxGenReg, (uint16_t)valpar2); break;
1905 case 4: rc = DISWriteReg32(pRegFrame, pDis->Param1.Base.idxGenReg, (uint32_t)valpar2); break;
1906 case 8: rc = DISWriteReg64(pRegFrame, pDis->Param1.Base.idxGenReg, valpar2); break;
1907 default: AssertFailedReturn(VERR_EM_INTERPRETER);
1908 }
1909 if (RT_FAILURE(rc))
1910 return VERR_EM_INTERPRETER;
1911 }
1912 else
1913 {
1914 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar2, param1.size);
1915 if (RT_FAILURE(rc))
1916 {
1917 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1918 return VERR_EM_INTERPRETER;
1919 }
1920 }
1921
1922 /* Write value of parameter 1 to parameter 2 (reg or memory address) */
1923 if (pParam2 == 0)
1924 {
1925 Assert(param2.type == DISQPV_TYPE_IMMEDIATE); /* register actually */
1926 switch(param2.size)
1927 {
1928 case 1: //special case for AH etc
1929 rc = DISWriteReg8(pRegFrame, pDis->Param2.Base.idxGenReg, (uint8_t )valpar1); break;
1930 case 2: rc = DISWriteReg16(pRegFrame, pDis->Param2.Base.idxGenReg, (uint16_t)valpar1); break;
1931 case 4: rc = DISWriteReg32(pRegFrame, pDis->Param2.Base.idxGenReg, (uint32_t)valpar1); break;
1932 case 8: rc = DISWriteReg64(pRegFrame, pDis->Param2.Base.idxGenReg, valpar1); break;
1933 default: AssertFailedReturn(VERR_EM_INTERPRETER);
1934 }
1935 if (RT_FAILURE(rc))
1936 return VERR_EM_INTERPRETER;
1937 }
1938 else
1939 {
1940 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam2, &valpar1, param2.size);
1941 if (RT_FAILURE(rc))
1942 {
1943 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1944 return VERR_EM_INTERPRETER;
1945 }
1946 }
1947
1948 *pcbSize = param2.size;
1949 return VINF_SUCCESS;
1950#ifdef IN_RC
1951 }
1952 }
1953 return VERR_EM_INTERPRETER;
1954#endif
1955}
1956
1957
1958/**
1959 * INC and DEC emulation.
1960 */
1961static int emInterpretIncDec(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
1962 PFNEMULATEPARAM2 pfnEmulate)
1963{
1964 DISQPVPARAMVAL param1;
1965 NOREF(pvFault);
1966
1967 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
1968 if(RT_FAILURE(rc))
1969 return VERR_EM_INTERPRETER;
1970
1971#ifdef IN_RC
1972 if (TRPMHasTrap(pVCpu))
1973 {
1974 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
1975 {
1976#endif
1977 RTGCPTR pParam1 = 0;
1978 uint64_t valpar1;
1979
1980 if (param1.type == DISQPV_TYPE_ADDRESS)
1981 {
1982 pParam1 = (RTGCPTR)param1.val.val64;
1983 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
1984#ifdef IN_RC
1985 /* Safety check (in theory it could cross a page boundary and fault there though) */
1986 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
1987#endif
1988 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
1989 if (RT_FAILURE(rc))
1990 {
1991 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1992 return VERR_EM_INTERPRETER;
1993 }
1994 }
1995 else
1996 {
1997 AssertFailed();
1998 return VERR_EM_INTERPRETER;
1999 }
2000
2001 uint32_t eflags;
2002
2003 eflags = pfnEmulate(&valpar1, param1.size);
2004
2005 /* Write result back */
2006 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
2007 if (RT_FAILURE(rc))
2008 {
2009 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2010 return VERR_EM_INTERPRETER;
2011 }
2012
2013 /* Update guest's eflags and finish. */
2014 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2015 | (eflags & (X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2016
2017 /* All done! */
2018 *pcbSize = param1.size;
2019 return VINF_SUCCESS;
2020#ifdef IN_RC
2021 }
2022 }
2023 return VERR_EM_INTERPRETER;
2024#endif
2025}
2026
2027
2028/**
2029 * POP Emulation.
2030 */
2031static int emInterpretPop(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2032{
2033 Assert(pDis->uCpuMode != DISCPUMODE_64BIT); /** @todo check */
2034 DISQPVPARAMVAL param1;
2035 NOREF(pvFault);
2036
2037 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2038 if(RT_FAILURE(rc))
2039 return VERR_EM_INTERPRETER;
2040
2041#ifdef IN_RC
2042 if (TRPMHasTrap(pVCpu))
2043 {
2044 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
2045 {
2046#endif
2047 RTGCPTR pParam1 = 0;
2048 uint32_t valpar1;
2049 RTGCPTR pStackVal;
2050
2051 /* Read stack value first */
2052 if (CPUMGetGuestCodeBits(pVCpu) == 16)
2053 return VERR_EM_INTERPRETER; /* No legacy 16 bits stuff here, please. */
2054
2055 /* Convert address; don't bother checking limits etc, as we only read here */
2056 pStackVal = SELMToFlat(pVM, DISSELREG_SS, pRegFrame, (RTGCPTR)pRegFrame->esp);
2057 if (pStackVal == 0)
2058 return VERR_EM_INTERPRETER;
2059
2060 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pStackVal, param1.size);
2061 if (RT_FAILURE(rc))
2062 {
2063 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2064 return VERR_EM_INTERPRETER;
2065 }
2066
2067 if (param1.type == DISQPV_TYPE_ADDRESS)
2068 {
2069 pParam1 = (RTGCPTR)param1.val.val64;
2070
2071 /* pop [esp+xx] uses esp after the actual pop! */
2072 AssertCompile(DISGREG_ESP == DISGREG_SP);
2073 if ( (pDis->Param1.fUse & DISUSE_BASE)
2074 && (pDis->Param1.fUse & (DISUSE_REG_GEN16|DISUSE_REG_GEN32))
2075 && pDis->Param1.Base.idxGenReg == DISGREG_ESP
2076 )
2077 pParam1 = (RTGCPTR)((RTGCUINTPTR)pParam1 + param1.size);
2078
2079 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
2080 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault || (RTGCPTR)pRegFrame->esp == pvFault, VERR_EM_INTERPRETER);
2081 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
2082 if (RT_FAILURE(rc))
2083 {
2084 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2085 return VERR_EM_INTERPRETER;
2086 }
2087
2088 /* Update ESP as the last step */
2089 pRegFrame->esp += param1.size;
2090 }
2091 else
2092 {
2093#ifndef DEBUG_bird // annoying assertion.
2094 AssertFailed();
2095#endif
2096 return VERR_EM_INTERPRETER;
2097 }
2098
2099 /* All done! */
2100 *pcbSize = param1.size;
2101 return VINF_SUCCESS;
2102#ifdef IN_RC
2103 }
2104 }
2105 return VERR_EM_INTERPRETER;
2106#endif
2107}
2108
2109
2110/**
2111 * XOR/OR/AND Emulation.
2112 */
2113static int emInterpretOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
2114 PFNEMULATEPARAM3 pfnEmulate)
2115{
2116 DISQPVPARAMVAL param1, param2;
2117 NOREF(pvFault);
2118
2119 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2120 if(RT_FAILURE(rc))
2121 return VERR_EM_INTERPRETER;
2122
2123 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2124 if(RT_FAILURE(rc))
2125 return VERR_EM_INTERPRETER;
2126
2127#ifdef IN_RC
2128 if (TRPMHasTrap(pVCpu))
2129 {
2130 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
2131 {
2132#endif
2133 RTGCPTR pParam1;
2134 uint64_t valpar1, valpar2;
2135
2136 if (pDis->Param1.cb != pDis->Param2.cb)
2137 {
2138 if (pDis->Param1.cb < pDis->Param2.cb)
2139 {
2140 AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->Param1.cb, pDis->Param2.cb)); /* should never happen! */
2141 return VERR_EM_INTERPRETER;
2142 }
2143 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
2144 pDis->Param2.cb = pDis->Param1.cb;
2145 param2.size = param1.size;
2146 }
2147
2148 /* The destination is always a virtual address */
2149 if (param1.type == DISQPV_TYPE_ADDRESS)
2150 {
2151 pParam1 = (RTGCPTR)param1.val.val64;
2152 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
2153 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
2154 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
2155 if (RT_FAILURE(rc))
2156 {
2157 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2158 return VERR_EM_INTERPRETER;
2159 }
2160 }
2161 else
2162 {
2163 AssertFailed();
2164 return VERR_EM_INTERPRETER;
2165 }
2166
2167 /* Register or immediate data */
2168 switch(param2.type)
2169 {
2170 case DISQPV_TYPE_IMMEDIATE: /* both immediate data and register (ugly) */
2171 valpar2 = param2.val.val64;
2172 break;
2173
2174 default:
2175 AssertFailed();
2176 return VERR_EM_INTERPRETER;
2177 }
2178
2179 LogFlow(("emInterpretOrXorAnd %s %RGv %RX64 - %RX64 size %d (%d)\n", emGetMnemonic(pDis), pParam1, valpar1, valpar2, param2.size, param1.size));
2180
2181 /* Data read, emulate instruction. */
2182 uint32_t eflags = pfnEmulate(&valpar1, valpar2, param2.size);
2183
2184 LogFlow(("emInterpretOrXorAnd %s result %RX64\n", emGetMnemonic(pDis), valpar1));
2185
2186 /* Update guest's eflags and finish. */
2187 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2188 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2189
2190 /* And write it back */
2191 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
2192 if (RT_SUCCESS(rc))
2193 {
2194 /* All done! */
2195 *pcbSize = param2.size;
2196 return VINF_SUCCESS;
2197 }
2198#ifdef IN_RC
2199 }
2200 }
2201#endif
2202 return VERR_EM_INTERPRETER;
2203}
2204
2205
2206#ifndef VBOX_COMPARE_IEM_AND_EM
2207/**
2208 * LOCK XOR/OR/AND Emulation.
2209 */
2210static int emInterpretLockOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
2211 uint32_t *pcbSize, PFNEMULATELOCKPARAM3 pfnEmulate)
2212{
2213 void *pvParam1;
2214 DISQPVPARAMVAL param1, param2;
2215 NOREF(pvFault);
2216
2217#if HC_ARCH_BITS == 32
2218 Assert(pDis->Param1.cb <= 4);
2219#endif
2220
2221 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2222 if(RT_FAILURE(rc))
2223 return VERR_EM_INTERPRETER;
2224
2225 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2226 if(RT_FAILURE(rc))
2227 return VERR_EM_INTERPRETER;
2228
2229 if (pDis->Param1.cb != pDis->Param2.cb)
2230 {
2231 AssertMsgReturn(pDis->Param1.cb >= pDis->Param2.cb, /* should never happen! */
2232 ("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->Param1.cb, pDis->Param2.cb),
2233 VERR_EM_INTERPRETER);
2234
2235 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
2236 pDis->Param2.cb = pDis->Param1.cb;
2237 param2.size = param1.size;
2238 }
2239
2240#ifdef IN_RC
2241 /* Safety check (in theory it could cross a page boundary and fault there though) */
2242 Assert( TRPMHasTrap(pVCpu)
2243 && (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW));
2244 EM_ASSERT_FAULT_RETURN(GCPtrPar1 == pvFault, VERR_EM_INTERPRETER);
2245#endif
2246
2247 /* Register and immediate data == DISQPV_TYPE_IMMEDIATE */
2248 AssertReturn(param2.type == DISQPV_TYPE_IMMEDIATE, VERR_EM_INTERPRETER);
2249 RTGCUINTREG ValPar2 = param2.val.val64;
2250
2251 /* The destination is always a virtual address */
2252 AssertReturn(param1.type == DISQPV_TYPE_ADDRESS, VERR_EM_INTERPRETER);
2253
2254 RTGCPTR GCPtrPar1 = param1.val.val64;
2255 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, GCPtrPar1);
2256 PGMPAGEMAPLOCK Lock;
2257 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
2258 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2259
2260 /* Try emulate it with a one-shot #PF handler in place. (RC) */
2261 Log2(("%s %RGv imm%d=%RX64\n", emGetMnemonic(pDis), GCPtrPar1, pDis->Param2.cb*8, ValPar2));
2262
2263 RTGCUINTREG32 eflags = 0;
2264 rc = pfnEmulate(pvParam1, ValPar2, pDis->Param2.cb, &eflags);
2265 PGMPhysReleasePageMappingLock(pVM, &Lock);
2266 if (RT_FAILURE(rc))
2267 {
2268 Log(("%s %RGv imm%d=%RX64-> emulation failed due to page fault!\n", emGetMnemonic(pDis), GCPtrPar1, pDis->Param2.cb*8, ValPar2));
2269 return VERR_EM_INTERPRETER;
2270 }
2271
2272 /* Update guest's eflags and finish. */
2273 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2274 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2275
2276 *pcbSize = param2.size;
2277 return VINF_SUCCESS;
2278}
2279#endif /* !VBOX_COMPARE_IEM_AND_EM */
2280
2281
2282/**
2283 * ADD, ADC & SUB Emulation.
2284 */
2285static int emInterpretAddSub(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
2286 PFNEMULATEPARAM3 pfnEmulate)
2287{
2288 NOREF(pvFault);
2289 DISQPVPARAMVAL param1, param2;
2290 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2291 if(RT_FAILURE(rc))
2292 return VERR_EM_INTERPRETER;
2293
2294 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2295 if(RT_FAILURE(rc))
2296 return VERR_EM_INTERPRETER;
2297
2298#ifdef IN_RC
2299 if (TRPMHasTrap(pVCpu))
2300 {
2301 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
2302 {
2303#endif
2304 RTGCPTR pParam1;
2305 uint64_t valpar1, valpar2;
2306
2307 if (pDis->Param1.cb != pDis->Param2.cb)
2308 {
2309 if (pDis->Param1.cb < pDis->Param2.cb)
2310 {
2311 AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->Param1.cb, pDis->Param2.cb)); /* should never happen! */
2312 return VERR_EM_INTERPRETER;
2313 }
2314 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
2315 pDis->Param2.cb = pDis->Param1.cb;
2316 param2.size = param1.size;
2317 }
2318
2319 /* The destination is always a virtual address */
2320 if (param1.type == DISQPV_TYPE_ADDRESS)
2321 {
2322 pParam1 = (RTGCPTR)param1.val.val64;
2323 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
2324 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
2325 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
2326 if (RT_FAILURE(rc))
2327 {
2328 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2329 return VERR_EM_INTERPRETER;
2330 }
2331 }
2332 else
2333 {
2334#ifndef DEBUG_bird
2335 AssertFailed();
2336#endif
2337 return VERR_EM_INTERPRETER;
2338 }
2339
2340 /* Register or immediate data */
2341 switch(param2.type)
2342 {
2343 case DISQPV_TYPE_IMMEDIATE: /* both immediate data and register (ugly) */
2344 valpar2 = param2.val.val64;
2345 break;
2346
2347 default:
2348 AssertFailed();
2349 return VERR_EM_INTERPRETER;
2350 }
2351
2352 /* Data read, emulate instruction. */
2353 uint32_t eflags = pfnEmulate(&valpar1, valpar2, param2.size);
2354
2355 /* Update guest's eflags and finish. */
2356 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2357 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2358
2359 /* And write it back */
2360 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
2361 if (RT_SUCCESS(rc))
2362 {
2363 /* All done! */
2364 *pcbSize = param2.size;
2365 return VINF_SUCCESS;
2366 }
2367#ifdef IN_RC
2368 }
2369 }
2370#endif
2371 return VERR_EM_INTERPRETER;
2372}
2373
2374
2375/**
2376 * ADC Emulation.
2377 */
2378static int emInterpretAdc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2379{
2380 if (pRegFrame->eflags.Bits.u1CF)
2381 return emInterpretAddSub(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, EMEmulateAdcWithCarrySet);
2382 else
2383 return emInterpretAddSub(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, EMEmulateAdd);
2384}
2385
2386
2387/**
2388 * BTR/C/S Emulation.
2389 */
2390static int emInterpretBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
2391 PFNEMULATEPARAM2UINT32 pfnEmulate)
2392{
2393 DISQPVPARAMVAL param1, param2;
2394 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2395 if(RT_FAILURE(rc))
2396 return VERR_EM_INTERPRETER;
2397
2398 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2399 if(RT_FAILURE(rc))
2400 return VERR_EM_INTERPRETER;
2401
2402#ifdef IN_RC
2403 if (TRPMHasTrap(pVCpu))
2404 {
2405 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
2406 {
2407#endif
2408 RTGCPTR pParam1;
2409 uint64_t valpar1 = 0, valpar2;
2410 uint32_t eflags;
2411
2412 /* The destination is always a virtual address */
2413 if (param1.type != DISQPV_TYPE_ADDRESS)
2414 return VERR_EM_INTERPRETER;
2415
2416 pParam1 = (RTGCPTR)param1.val.val64;
2417 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
2418
2419 /* Register or immediate data */
2420 switch(param2.type)
2421 {
2422 case DISQPV_TYPE_IMMEDIATE: /* both immediate data and register (ugly) */
2423 valpar2 = param2.val.val64;
2424 break;
2425
2426 default:
2427 AssertFailed();
2428 return VERR_EM_INTERPRETER;
2429 }
2430
2431 Log2(("emInterpret%s: pvFault=%RGv pParam1=%RGv val2=%x\n", emGetMnemonic(pDis), pvFault, pParam1, valpar2));
2432 pParam1 = (RTGCPTR)((RTGCUINTPTR)pParam1 + valpar2/8);
2433 EM_ASSERT_FAULT_RETURN((RTGCPTR)((RTGCUINTPTR)pParam1 & ~3) == pvFault, VERR_EM_INTERPRETER); NOREF(pvFault);
2434 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, 1);
2435 if (RT_FAILURE(rc))
2436 {
2437 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2438 return VERR_EM_INTERPRETER;
2439 }
2440
2441 Log2(("emInterpretBtx: val=%x\n", valpar1));
2442 /* Data read, emulate bit test instruction. */
2443 eflags = pfnEmulate(&valpar1, valpar2 & 0x7);
2444
2445 Log2(("emInterpretBtx: val=%x CF=%d\n", valpar1, !!(eflags & X86_EFL_CF)));
2446
2447 /* Update guest's eflags and finish. */
2448 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2449 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2450
2451 /* And write it back */
2452 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, 1);
2453 if (RT_SUCCESS(rc))
2454 {
2455 /* All done! */
2456 *pcbSize = 1;
2457 return VINF_SUCCESS;
2458 }
2459#ifdef IN_RC
2460 }
2461 }
2462#endif
2463 return VERR_EM_INTERPRETER;
2464}
2465
2466
2467#ifndef VBOX_COMPARE_IEM_AND_EM
2468/**
2469 * LOCK BTR/C/S Emulation.
2470 */
2471static int emInterpretLockBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
2472 uint32_t *pcbSize, PFNEMULATELOCKPARAM2 pfnEmulate)
2473{
2474 void *pvParam1;
2475
2476 DISQPVPARAMVAL param1, param2;
2477 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2478 if(RT_FAILURE(rc))
2479 return VERR_EM_INTERPRETER;
2480
2481 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2482 if(RT_FAILURE(rc))
2483 return VERR_EM_INTERPRETER;
2484
2485 /* The destination is always a virtual address */
2486 if (param1.type != DISQPV_TYPE_ADDRESS)
2487 return VERR_EM_INTERPRETER;
2488
2489 /* Register and immediate data == DISQPV_TYPE_IMMEDIATE */
2490 AssertReturn(param2.type == DISQPV_TYPE_IMMEDIATE, VERR_EM_INTERPRETER);
2491 uint64_t ValPar2 = param2.val.val64;
2492
2493 /* Adjust the parameters so what we're dealing with is a bit within the byte pointed to. */
2494 RTGCPTR GCPtrPar1 = param1.val.val64;
2495 GCPtrPar1 = (GCPtrPar1 + ValPar2 / 8);
2496 ValPar2 &= 7;
2497
2498 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, GCPtrPar1);
2499#ifdef IN_RC
2500 Assert(TRPMHasTrap(pVCpu));
2501 EM_ASSERT_FAULT_RETURN((RTGCPTR)((RTGCUINTPTR)GCPtrPar1 & ~(RTGCUINTPTR)3) == pvFault, VERR_EM_INTERPRETER);
2502#endif
2503
2504 PGMPAGEMAPLOCK Lock;
2505 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
2506 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2507
2508 Log2(("emInterpretLockBitTest %s: pvFault=%RGv GCPtrPar1=%RGv imm=%RX64\n", emGetMnemonic(pDis), pvFault, GCPtrPar1, ValPar2));
2509 NOREF(pvFault);
2510
2511 /* Try emulate it with a one-shot #PF handler in place. (RC) */
2512 RTGCUINTREG32 eflags = 0;
2513 rc = pfnEmulate(pvParam1, ValPar2, &eflags);
2514 PGMPhysReleasePageMappingLock(pVM, &Lock);
2515 if (RT_FAILURE(rc))
2516 {
2517 Log(("emInterpretLockBitTest %s: %RGv imm%d=%RX64 -> emulation failed due to page fault!\n",
2518 emGetMnemonic(pDis), GCPtrPar1, pDis->Param2.cb*8, ValPar2));
2519 return VERR_EM_INTERPRETER;
2520 }
2521
2522 Log2(("emInterpretLockBitTest %s: GCPtrPar1=%RGv imm=%RX64 CF=%d\n", emGetMnemonic(pDis), GCPtrPar1, ValPar2, !!(eflags & X86_EFL_CF)));
2523
2524 /* Update guest's eflags and finish. */
2525 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2526 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2527
2528 *pcbSize = 1;
2529 return VINF_SUCCESS;
2530}
2531#endif /* !VBOX_COMPARE_IEM_AND_EM */
2532
2533
2534/**
2535 * MOV emulation.
2536 */
2537static int emInterpretMov(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2538{
2539 NOREF(pvFault);
2540 DISQPVPARAMVAL param1, param2;
2541 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_DST);
2542 if(RT_FAILURE(rc))
2543 return VERR_EM_INTERPRETER;
2544
2545 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2546 if(RT_FAILURE(rc))
2547 return VERR_EM_INTERPRETER;
2548
2549 /* If destination is a segment register, punt. We can't handle it here.
2550 * NB: Source can be a register and still trigger a #PF!
2551 */
2552 if (RT_UNLIKELY(pDis->Param1.fUse == DISUSE_REG_SEG))
2553 return VERR_EM_INTERPRETER;
2554
2555 if (param1.type == DISQPV_TYPE_ADDRESS)
2556 {
2557 RTGCPTR pDest;
2558 uint64_t val64;
2559
2560 switch(param1.type)
2561 {
2562 case DISQPV_TYPE_IMMEDIATE:
2563 if(!(param1.flags & (DISQPV_FLAG_32|DISQPV_FLAG_64)))
2564 return VERR_EM_INTERPRETER;
2565 /* fallthru */
2566
2567 case DISQPV_TYPE_ADDRESS:
2568 pDest = (RTGCPTR)param1.val.val64;
2569 pDest = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pDest);
2570 break;
2571
2572 default:
2573 AssertFailed();
2574 return VERR_EM_INTERPRETER;
2575 }
2576
2577 switch(param2.type)
2578 {
2579 case DISQPV_TYPE_IMMEDIATE: /* register type is translated to this one too */
2580 val64 = param2.val.val64;
2581 break;
2582
2583 default:
2584 Log(("emInterpretMov: unexpected type=%d rip=%RGv\n", param2.type, (RTGCPTR)pRegFrame->rip));
2585 return VERR_EM_INTERPRETER;
2586 }
2587#ifdef LOG_ENABLED
2588 if (pDis->uCpuMode == DISCPUMODE_64BIT)
2589 LogFlow(("EMInterpretInstruction at %RGv: OP_MOV %RGv <- %RX64 (%d) &val64=%RHv\n", (RTGCPTR)pRegFrame->rip, pDest, val64, param2.size, &val64));
2590 else
2591 LogFlow(("EMInterpretInstruction at %08RX64: OP_MOV %RGv <- %08X (%d) &val64=%RHv\n", pRegFrame->rip, pDest, (uint32_t)val64, param2.size, &val64));
2592#endif
2593
2594 Assert(param2.size <= 8 && param2.size > 0);
2595 EM_ASSERT_FAULT_RETURN(pDest == pvFault, VERR_EM_INTERPRETER);
2596 rc = emRamWrite(pVM, pVCpu, pRegFrame, pDest, &val64, param2.size);
2597 if (RT_FAILURE(rc))
2598 return VERR_EM_INTERPRETER;
2599
2600 *pcbSize = param2.size;
2601 }
2602#if defined(IN_RC) && defined(VBOX_WITH_RAW_RING1)
2603 /* mov xx, cs instruction is dangerous in raw mode and replaced by an 'int3' by csam/patm. */
2604 else if ( param1.type == DISQPV_TYPE_REGISTER
2605 && param2.type == DISQPV_TYPE_REGISTER)
2606 {
2607 AssertReturn((pDis->Param1.fUse & (DISUSE_REG_GEN8|DISUSE_REG_GEN16|DISUSE_REG_GEN32)), VERR_EM_INTERPRETER);
2608 AssertReturn(pDis->Param2.fUse == DISUSE_REG_SEG, VERR_EM_INTERPRETER);
2609 AssertReturn(pDis->Param2.Base.idxSegReg == DISSELREG_CS, VERR_EM_INTERPRETER);
2610
2611 uint32_t u32Cpl = CPUMRCGetGuestCPL(pVCpu, pRegFrame);
2612 uint32_t uValCS = (pRegFrame->cs.Sel & ~X86_SEL_RPL) | u32Cpl;
2613
2614 Log(("EMInterpretInstruction: OP_MOV cs=%x->%x\n", pRegFrame->cs.Sel, uValCS));
2615 switch (param1.size)
2616 {
2617 case 1: rc = DISWriteReg8(pRegFrame, pDis->Param1.Base.idxGenReg, (uint8_t) uValCS); break;
2618 case 2: rc = DISWriteReg16(pRegFrame, pDis->Param1.Base.idxGenReg, (uint16_t)uValCS); break;
2619 case 4: rc = DISWriteReg32(pRegFrame, pDis->Param1.Base.idxGenReg, (uint32_t)uValCS); break;
2620 default:
2621 AssertFailed();
2622 return VERR_EM_INTERPRETER;
2623 }
2624 AssertRCReturn(rc, rc);
2625 }
2626#endif
2627 else
2628 { /* read fault */
2629 RTGCPTR pSrc;
2630 uint64_t val64;
2631
2632 /* Source */
2633 switch(param2.type)
2634 {
2635 case DISQPV_TYPE_IMMEDIATE:
2636 if(!(param2.flags & (DISQPV_FLAG_32|DISQPV_FLAG_64)))
2637 return VERR_EM_INTERPRETER;
2638 /* fallthru */
2639
2640 case DISQPV_TYPE_ADDRESS:
2641 pSrc = (RTGCPTR)param2.val.val64;
2642 pSrc = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param2, pSrc);
2643 break;
2644
2645 default:
2646 return VERR_EM_INTERPRETER;
2647 }
2648
2649 Assert(param1.size <= 8 && param1.size > 0);
2650 EM_ASSERT_FAULT_RETURN(pSrc == pvFault, VERR_EM_INTERPRETER);
2651 rc = emRamRead(pVM, pVCpu, pRegFrame, &val64, pSrc, param1.size);
2652 if (RT_FAILURE(rc))
2653 return VERR_EM_INTERPRETER;
2654
2655 /* Destination */
2656 switch(param1.type)
2657 {
2658 case DISQPV_TYPE_REGISTER:
2659 switch(param1.size)
2660 {
2661 case 1: rc = DISWriteReg8(pRegFrame, pDis->Param1.Base.idxGenReg, (uint8_t) val64); break;
2662 case 2: rc = DISWriteReg16(pRegFrame, pDis->Param1.Base.idxGenReg, (uint16_t)val64); break;
2663 case 4: rc = DISWriteReg32(pRegFrame, pDis->Param1.Base.idxGenReg, (uint32_t)val64); break;
2664 case 8: rc = DISWriteReg64(pRegFrame, pDis->Param1.Base.idxGenReg, val64); break;
2665 default:
2666 return VERR_EM_INTERPRETER;
2667 }
2668 if (RT_FAILURE(rc))
2669 return rc;
2670 break;
2671
2672 default:
2673 return VERR_EM_INTERPRETER;
2674 }
2675#ifdef LOG_ENABLED
2676 if (pDis->uCpuMode == DISCPUMODE_64BIT)
2677 LogFlow(("EMInterpretInstruction: OP_MOV %RGv -> %RX64 (%d)\n", pSrc, val64, param1.size));
2678 else
2679 LogFlow(("EMInterpretInstruction: OP_MOV %RGv -> %08X (%d)\n", pSrc, (uint32_t)val64, param1.size));
2680#endif
2681 }
2682 return VINF_SUCCESS;
2683}
2684
2685
2686#ifndef IN_RC
2687/**
2688 * [REP] STOSWD emulation
2689 */
2690static int emInterpretStosWD(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2691{
2692 int rc;
2693 RTGCPTR GCDest, GCOffset;
2694 uint32_t cbSize;
2695 uint64_t cTransfers;
2696 int offIncrement;
2697 NOREF(pvFault);
2698
2699 /* Don't support any but these three prefix bytes. */
2700 if ((pDis->fPrefix & ~(DISPREFIX_ADDRSIZE|DISPREFIX_OPSIZE|DISPREFIX_REP|DISPREFIX_REX)))
2701 return VERR_EM_INTERPRETER;
2702
2703 switch (pDis->uAddrMode)
2704 {
2705 case DISCPUMODE_16BIT:
2706 GCOffset = pRegFrame->di;
2707 cTransfers = pRegFrame->cx;
2708 break;
2709 case DISCPUMODE_32BIT:
2710 GCOffset = pRegFrame->edi;
2711 cTransfers = pRegFrame->ecx;
2712 break;
2713 case DISCPUMODE_64BIT:
2714 GCOffset = pRegFrame->rdi;
2715 cTransfers = pRegFrame->rcx;
2716 break;
2717 default:
2718 AssertFailed();
2719 return VERR_EM_INTERPRETER;
2720 }
2721
2722 GCDest = SELMToFlat(pVM, DISSELREG_ES, pRegFrame, GCOffset);
2723 switch (pDis->uOpMode)
2724 {
2725 case DISCPUMODE_16BIT:
2726 cbSize = 2;
2727 break;
2728 case DISCPUMODE_32BIT:
2729 cbSize = 4;
2730 break;
2731 case DISCPUMODE_64BIT:
2732 cbSize = 8;
2733 break;
2734 default:
2735 AssertFailed();
2736 return VERR_EM_INTERPRETER;
2737 }
2738
2739 offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cbSize : (signed)cbSize;
2740
2741 if (!(pDis->fPrefix & DISPREFIX_REP))
2742 {
2743 LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d\n", pRegFrame->es.Sel, GCOffset, GCDest, cbSize));
2744
2745 rc = emRamWrite(pVM, pVCpu, pRegFrame, GCDest, &pRegFrame->rax, cbSize);
2746 if (RT_FAILURE(rc))
2747 return VERR_EM_INTERPRETER;
2748 Assert(rc == VINF_SUCCESS);
2749
2750 /* Update (e/r)di. */
2751 switch (pDis->uAddrMode)
2752 {
2753 case DISCPUMODE_16BIT:
2754 pRegFrame->di += offIncrement;
2755 break;
2756 case DISCPUMODE_32BIT:
2757 pRegFrame->edi += offIncrement;
2758 break;
2759 case DISCPUMODE_64BIT:
2760 pRegFrame->rdi += offIncrement;
2761 break;
2762 default:
2763 AssertFailed();
2764 return VERR_EM_INTERPRETER;
2765 }
2766
2767 }
2768 else
2769 {
2770 if (!cTransfers)
2771 return VINF_SUCCESS;
2772
2773 /*
2774 * Do *not* try emulate cross page stuff here because we don't know what might
2775 * be waiting for us on the subsequent pages. The caller has only asked us to
2776 * ignore access handlers fro the current page.
2777 * This also fends off big stores which would quickly kill PGMR0DynMap.
2778 */
2779 if ( cbSize > PAGE_SIZE
2780 || cTransfers > PAGE_SIZE
2781 || (GCDest >> PAGE_SHIFT) != ((GCDest + offIncrement * cTransfers) >> PAGE_SHIFT))
2782 {
2783 Log(("STOSWD is crosses pages, chicken out to the recompiler; GCDest=%RGv cbSize=%#x offIncrement=%d cTransfers=%#x\n",
2784 GCDest, cbSize, offIncrement, cTransfers));
2785 return VERR_EM_INTERPRETER;
2786 }
2787
2788 LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d cTransfers=%x DF=%d\n", pRegFrame->es.Sel, GCOffset, GCDest, cbSize, cTransfers, pRegFrame->eflags.Bits.u1DF));
2789 /* Access verification first; we currently can't recover properly from traps inside this instruction */
2790 rc = PGMVerifyAccess(pVCpu, GCDest - ((offIncrement > 0) ? 0 : ((cTransfers-1) * cbSize)),
2791 cTransfers * cbSize,
2792 X86_PTE_RW | (CPUMGetGuestCPL(pVCpu) == 3 ? X86_PTE_US : 0));
2793 if (rc != VINF_SUCCESS)
2794 {
2795 Log(("STOSWD will generate a trap -> recompiler, rc=%d\n", rc));
2796 return VERR_EM_INTERPRETER;
2797 }
2798
2799 /* REP case */
2800 while (cTransfers)
2801 {
2802 rc = emRamWrite(pVM, pVCpu, pRegFrame, GCDest, &pRegFrame->rax, cbSize);
2803 if (RT_FAILURE(rc))
2804 {
2805 rc = VERR_EM_INTERPRETER;
2806 break;
2807 }
2808
2809 Assert(rc == VINF_SUCCESS);
2810 GCOffset += offIncrement;
2811 GCDest += offIncrement;
2812 cTransfers--;
2813 }
2814
2815 /* Update the registers. */
2816 switch (pDis->uAddrMode)
2817 {
2818 case DISCPUMODE_16BIT:
2819 pRegFrame->di = GCOffset;
2820 pRegFrame->cx = cTransfers;
2821 break;
2822 case DISCPUMODE_32BIT:
2823 pRegFrame->edi = GCOffset;
2824 pRegFrame->ecx = cTransfers;
2825 break;
2826 case DISCPUMODE_64BIT:
2827 pRegFrame->rdi = GCOffset;
2828 pRegFrame->rcx = cTransfers;
2829 break;
2830 default:
2831 AssertFailed();
2832 return VERR_EM_INTERPRETER;
2833 }
2834 }
2835
2836 *pcbSize = cbSize;
2837 return rc;
2838}
2839#endif /* !IN_RC */
2840
2841
2842/**
2843 * [LOCK] CMPXCHG emulation.
2844 */
2845static int emInterpretCmpXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2846{
2847 DISQPVPARAMVAL param1, param2;
2848 NOREF(pvFault);
2849
2850#if HC_ARCH_BITS == 32
2851 Assert(pDis->Param1.cb <= 4);
2852#endif
2853
2854 /* Source to make DISQueryParamVal read the register value - ugly hack */
2855 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
2856 if(RT_FAILURE(rc))
2857 return VERR_EM_INTERPRETER;
2858
2859 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param2, &param2, DISQPVWHICH_SRC);
2860 if(RT_FAILURE(rc))
2861 return VERR_EM_INTERPRETER;
2862
2863 uint64_t valpar;
2864 switch(param2.type)
2865 {
2866 case DISQPV_TYPE_IMMEDIATE: /* register actually */
2867 valpar = param2.val.val64;
2868 break;
2869
2870 default:
2871 return VERR_EM_INTERPRETER;
2872 }
2873
2874 PGMPAGEMAPLOCK Lock;
2875 RTGCPTR GCPtrPar1;
2876 void *pvParam1;
2877 uint64_t eflags;
2878
2879 AssertReturn(pDis->Param1.cb == pDis->Param2.cb, VERR_EM_INTERPRETER);
2880 switch(param1.type)
2881 {
2882 case DISQPV_TYPE_ADDRESS:
2883 GCPtrPar1 = param1.val.val64;
2884 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, GCPtrPar1);
2885
2886 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
2887 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2888 break;
2889
2890 default:
2891 return VERR_EM_INTERPRETER;
2892 }
2893
2894 LogFlow(("%s %RGv rax=%RX64 %RX64\n", emGetMnemonic(pDis), GCPtrPar1, pRegFrame->rax, valpar));
2895
2896#ifndef VBOX_COMPARE_IEM_AND_EM
2897 if (pDis->fPrefix & DISPREFIX_LOCK)
2898 eflags = EMEmulateLockCmpXchg(pvParam1, &pRegFrame->rax, valpar, pDis->Param2.cb);
2899 else
2900 eflags = EMEmulateCmpXchg(pvParam1, &pRegFrame->rax, valpar, pDis->Param2.cb);
2901#else /* VBOX_COMPARE_IEM_AND_EM */
2902 uint64_t u64;
2903 switch (pDis->Param2.cb)
2904 {
2905 case 1: u64 = *(uint8_t *)pvParam1; break;
2906 case 2: u64 = *(uint16_t *)pvParam1; break;
2907 case 4: u64 = *(uint32_t *)pvParam1; break;
2908 default:
2909 case 8: u64 = *(uint64_t *)pvParam1; break;
2910 }
2911 eflags = EMEmulateCmpXchg(&u64, &pRegFrame->rax, valpar, pDis->Param2.cb);
2912 int rc2 = emRamWrite(pVM, pVCpu, pRegFrame, GCPtrPar1, &u64, pDis->Param2.cb); AssertRCSuccess(rc2);
2913#endif /* VBOX_COMPARE_IEM_AND_EM */
2914
2915 LogFlow(("%s %RGv rax=%RX64 %RX64 ZF=%d\n", emGetMnemonic(pDis), GCPtrPar1, pRegFrame->rax, valpar, !!(eflags & X86_EFL_ZF)));
2916
2917 /* Update guest's eflags and finish. */
2918 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
2919 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
2920
2921 *pcbSize = param2.size;
2922 PGMPhysReleasePageMappingLock(pVM, &Lock);
2923 return VINF_SUCCESS;
2924}
2925
2926
2927/**
2928 * [LOCK] CMPXCHG8B emulation.
2929 */
2930static int emInterpretCmpXchg8b(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2931{
2932 DISQPVPARAMVAL param1;
2933 NOREF(pvFault);
2934
2935 /* Source to make DISQueryParamVal read the register value - ugly hack */
2936 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
2937 if(RT_FAILURE(rc))
2938 return VERR_EM_INTERPRETER;
2939
2940 RTGCPTR GCPtrPar1;
2941 void *pvParam1;
2942 uint64_t eflags;
2943 PGMPAGEMAPLOCK Lock;
2944
2945 AssertReturn(pDis->Param1.cb == 8, VERR_EM_INTERPRETER);
2946 switch(param1.type)
2947 {
2948 case DISQPV_TYPE_ADDRESS:
2949 GCPtrPar1 = param1.val.val64;
2950 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, GCPtrPar1);
2951
2952 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
2953 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2954 break;
2955
2956 default:
2957 return VERR_EM_INTERPRETER;
2958 }
2959
2960 LogFlow(("%s %RGv=%p eax=%08x\n", emGetMnemonic(pDis), GCPtrPar1, pvParam1, pRegFrame->eax));
2961
2962#ifndef VBOX_COMPARE_IEM_AND_EM
2963 if (pDis->fPrefix & DISPREFIX_LOCK)
2964 eflags = EMEmulateLockCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
2965 else
2966 eflags = EMEmulateCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
2967#else /* VBOX_COMPARE_IEM_AND_EM */
2968 uint64_t u64 = *(uint64_t *)pvParam1;
2969 eflags = EMEmulateCmpXchg8b(&u64, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
2970 int rc2 = emRamWrite(pVM, pVCpu, pRegFrame, GCPtrPar1, &u64, sizeof(u64)); AssertRCSuccess(rc2);
2971#endif /* VBOX_COMPARE_IEM_AND_EM */
2972
2973 LogFlow(("%s %RGv=%p eax=%08x ZF=%d\n", emGetMnemonic(pDis), GCPtrPar1, pvParam1, pRegFrame->eax, !!(eflags & X86_EFL_ZF)));
2974
2975 /* Update guest's eflags and finish; note that *only* ZF is affected. */
2976 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_ZF))
2977 | (eflags & (X86_EFL_ZF));
2978
2979 *pcbSize = 8;
2980 PGMPhysReleasePageMappingLock(pVM, &Lock);
2981 return VINF_SUCCESS;
2982}
2983
2984
2985#ifdef IN_RC /** @todo test+enable for HM as well. */
2986/**
2987 * [LOCK] XADD emulation.
2988 */
2989static int emInterpretXAdd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2990{
2991 Assert(pDis->uCpuMode != DISCPUMODE_64BIT); /** @todo check */
2992 DISQPVPARAMVAL param1;
2993 void *pvParamReg2;
2994 size_t cbParamReg2;
2995 NOREF(pvFault);
2996
2997 /* Source to make DISQueryParamVal read the register value - ugly hack */
2998 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
2999 if(RT_FAILURE(rc))
3000 return VERR_EM_INTERPRETER;
3001
3002 rc = DISQueryParamRegPtr(pRegFrame, pDis, &pDis->Param2, &pvParamReg2, &cbParamReg2);
3003 Assert(cbParamReg2 <= 4);
3004 if(RT_FAILURE(rc))
3005 return VERR_EM_INTERPRETER;
3006
3007#ifdef IN_RC
3008 if (TRPMHasTrap(pVCpu))
3009 {
3010 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
3011 {
3012#endif
3013 RTGCPTR GCPtrPar1;
3014 void *pvParam1;
3015 uint32_t eflags;
3016 PGMPAGEMAPLOCK Lock;
3017
3018 AssertReturn(pDis->Param1.cb == pDis->Param2.cb, VERR_EM_INTERPRETER);
3019 switch(param1.type)
3020 {
3021 case DISQPV_TYPE_ADDRESS:
3022 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, (RTRCUINTPTR)param1.val.val64);
3023#ifdef IN_RC
3024 EM_ASSERT_FAULT_RETURN(GCPtrPar1 == pvFault, VERR_EM_INTERPRETER);
3025#endif
3026
3027 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
3028 AssertRCReturn(rc, VERR_EM_INTERPRETER);
3029 break;
3030
3031 default:
3032 return VERR_EM_INTERPRETER;
3033 }
3034
3035 LogFlow(("XAdd %RGv=%p reg=%08llx\n", GCPtrPar1, pvParam1, *(uint64_t *)pvParamReg2));
3036
3037#ifndef VBOX_COMPARE_IEM_AND_EM
3038 if (pDis->fPrefix & DISPREFIX_LOCK)
3039 eflags = EMEmulateLockXAdd(pvParam1, pvParamReg2, cbParamReg2);
3040 else
3041 eflags = EMEmulateXAdd(pvParam1, pvParamReg2, cbParamReg2);
3042#else /* VBOX_COMPARE_IEM_AND_EM */
3043 uint64_t u64;
3044 switch (cbParamReg2)
3045 {
3046 case 1: u64 = *(uint8_t *)pvParam1; break;
3047 case 2: u64 = *(uint16_t *)pvParam1; break;
3048 case 4: u64 = *(uint32_t *)pvParam1; break;
3049 default:
3050 case 8: u64 = *(uint64_t *)pvParam1; break;
3051 }
3052 eflags = EMEmulateXAdd(&u64, pvParamReg2, cbParamReg2);
3053 int rc2 = emRamWrite(pVM, pVCpu, pRegFrame, GCPtrPar1, &u64, pDis->Param2.cb); AssertRCSuccess(rc2);
3054#endif /* VBOX_COMPARE_IEM_AND_EM */
3055
3056 LogFlow(("XAdd %RGv=%p reg=%08llx ZF=%d\n", GCPtrPar1, pvParam1, *(uint64_t *)pvParamReg2, !!(eflags & X86_EFL_ZF) ));
3057
3058 /* Update guest's eflags and finish. */
3059 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
3060 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
3061
3062 *pcbSize = cbParamReg2;
3063 PGMPhysReleasePageMappingLock(pVM, &Lock);
3064 return VINF_SUCCESS;
3065#ifdef IN_RC
3066 }
3067 }
3068
3069 return VERR_EM_INTERPRETER;
3070#endif
3071}
3072#endif /* IN_RC */
3073
3074
3075/**
3076 * WBINVD Emulation.
3077 */
3078static int emInterpretWbInvd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3079{
3080 /* Nothing to do. */
3081 NOREF(pVM); NOREF(pVCpu); NOREF(pDis); NOREF(pRegFrame); NOREF(pvFault); NOREF(pcbSize);
3082 return VINF_SUCCESS;
3083}
3084
3085
3086/**
3087 * INVLPG Emulation.
3088 */
3089static VBOXSTRICTRC emInterpretInvlPg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3090{
3091 DISQPVPARAMVAL param1;
3092 RTGCPTR addr;
3093 NOREF(pvFault); NOREF(pVM); NOREF(pcbSize);
3094
3095 VBOXSTRICTRC rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
3096 if(RT_FAILURE(rc))
3097 return VERR_EM_INTERPRETER;
3098
3099 switch(param1.type)
3100 {
3101 case DISQPV_TYPE_IMMEDIATE:
3102 case DISQPV_TYPE_ADDRESS:
3103 if(!(param1.flags & (DISQPV_FLAG_32|DISQPV_FLAG_64)))
3104 return VERR_EM_INTERPRETER;
3105 addr = (RTGCPTR)param1.val.val64;
3106 break;
3107
3108 default:
3109 return VERR_EM_INTERPRETER;
3110 }
3111
3112 /** @todo is addr always a flat linear address or ds based
3113 * (in absence of segment override prefixes)????
3114 */
3115#ifdef IN_RC
3116 LogFlow(("RC: EMULATE: invlpg %RGv\n", addr));
3117#endif
3118 rc = PGMInvalidatePage(pVCpu, addr);
3119 if ( rc == VINF_SUCCESS
3120 || rc == VINF_PGM_SYNC_CR3 /* we can rely on the FF */)
3121 return VINF_SUCCESS;
3122 AssertMsgReturn(rc == VINF_EM_RAW_EMULATE_INSTR,
3123 ("%Rrc addr=%RGv\n", VBOXSTRICTRC_VAL(rc), addr),
3124 VERR_EM_INTERPRETER);
3125 return rc;
3126}
3127
3128/** @todo change all these EMInterpretXXX methods to VBOXSTRICTRC. */
3129
3130/**
3131 * CPUID Emulation.
3132 */
3133static int emInterpretCpuId(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3134{
3135 NOREF(pVM); NOREF(pVCpu); NOREF(pDis); NOREF(pRegFrame); NOREF(pvFault); NOREF(pcbSize);
3136 int rc = EMInterpretCpuId(pVM, pVCpu, pRegFrame);
3137 return rc;
3138}
3139
3140
3141/**
3142 * CLTS Emulation.
3143 */
3144static int emInterpretClts(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3145{
3146 NOREF(pVM); NOREF(pDis); NOREF(pRegFrame); NOREF(pvFault); NOREF(pcbSize);
3147
3148 uint64_t cr0 = CPUMGetGuestCR0(pVCpu);
3149 if (!(cr0 & X86_CR0_TS))
3150 return VINF_SUCCESS;
3151 return CPUMSetGuestCR0(pVCpu, cr0 & ~X86_CR0_TS);
3152}
3153
3154
3155/**
3156 * Update CRx.
3157 *
3158 * @returns VBox status code.
3159 * @param pVM The cross context VM structure.
3160 * @param pVCpu The cross context virtual CPU structure.
3161 * @param pRegFrame The register frame.
3162 * @param DestRegCrx CRx register index (DISUSE_REG_CR*)
3163 * @param val New CRx value
3164 *
3165 */
3166static int emUpdateCRx(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint64_t val)
3167{
3168 uint64_t oldval;
3169 uint64_t msrEFER;
3170 uint32_t fValid;
3171 int rc, rc2;
3172 NOREF(pVM);
3173
3174 /** @todo Clean up this mess. */
3175 LogFlow(("emInterpretCRxWrite at %RGv CR%d <- %RX64\n", (RTGCPTR)pRegFrame->rip, DestRegCrx, val));
3176 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
3177 switch (DestRegCrx)
3178 {
3179 case DISCREG_CR0:
3180 oldval = CPUMGetGuestCR0(pVCpu);
3181#ifdef IN_RC
3182 /* CR0.WP and CR0.AM changes require a reschedule run in ring 3. */
3183 if ( (val & (X86_CR0_WP | X86_CR0_AM))
3184 != (oldval & (X86_CR0_WP | X86_CR0_AM)))
3185 return VERR_EM_INTERPRETER;
3186#endif
3187 rc = VINF_SUCCESS;
3188#if !defined(VBOX_COMPARE_IEM_AND_EM) || !defined(VBOX_COMPARE_IEM_LAST)
3189 CPUMSetGuestCR0(pVCpu, val);
3190#else
3191 CPUMQueryGuestCtxPtr(pVCpu)->cr0 = val | X86_CR0_ET;
3192#endif
3193 val = CPUMGetGuestCR0(pVCpu);
3194 if ( (oldval & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
3195 != (val & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
3196 {
3197 /* global flush */
3198 rc = PGMFlushTLB(pVCpu, CPUMGetGuestCR3(pVCpu), true /* global */);
3199 AssertRCReturn(rc, rc);
3200 }
3201
3202 /* Deal with long mode enabling/disabling. */
3203 msrEFER = CPUMGetGuestEFER(pVCpu);
3204 if (msrEFER & MSR_K6_EFER_LME)
3205 {
3206 if ( !(oldval & X86_CR0_PG)
3207 && (val & X86_CR0_PG))
3208 {
3209 /* Illegal to have an active 64 bits CS selector (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
3210 if (pRegFrame->cs.Attr.n.u1Long)
3211 {
3212 AssertMsgFailed(("Illegal enabling of paging with CS.u1Long = 1!!\n"));
3213 return VERR_EM_INTERPRETER; /** @todo generate \#GP(0) */
3214 }
3215
3216 /* Illegal to switch to long mode before activating PAE first (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
3217 if (!(CPUMGetGuestCR4(pVCpu) & X86_CR4_PAE))
3218 {
3219 AssertMsgFailed(("Illegal enabling of paging with PAE disabled!!\n"));
3220 return VERR_EM_INTERPRETER; /** @todo generate \#GP(0) */
3221 }
3222 msrEFER |= MSR_K6_EFER_LMA;
3223 }
3224 else
3225 if ( (oldval & X86_CR0_PG)
3226 && !(val & X86_CR0_PG))
3227 {
3228 msrEFER &= ~MSR_K6_EFER_LMA;
3229 /** @todo Do we need to cut off rip here? High dword of rip is undefined, so it shouldn't really matter. */
3230 }
3231 CPUMSetGuestEFER(pVCpu, msrEFER);
3232 }
3233 rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
3234 return rc2 == VINF_SUCCESS ? rc : rc2;
3235
3236 case DISCREG_CR2:
3237 rc = CPUMSetGuestCR2(pVCpu, val); AssertRC(rc);
3238 return VINF_SUCCESS;
3239
3240 case DISCREG_CR3:
3241 /* Reloading the current CR3 means the guest just wants to flush the TLBs */
3242 rc = CPUMSetGuestCR3(pVCpu, val); AssertRC(rc);
3243 if (CPUMGetGuestCR0(pVCpu) & X86_CR0_PG)
3244 {
3245 /* flush */
3246 rc = PGMFlushTLB(pVCpu, val, !(CPUMGetGuestCR4(pVCpu) & X86_CR4_PGE));
3247 AssertRC(rc);
3248 }
3249 return rc;
3250
3251 case DISCREG_CR4:
3252 oldval = CPUMGetGuestCR4(pVCpu);
3253 rc = CPUMSetGuestCR4(pVCpu, val); AssertRC(rc);
3254 val = CPUMGetGuestCR4(pVCpu);
3255
3256 /* Illegal to disable PAE when long mode is active. (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
3257 msrEFER = CPUMGetGuestEFER(pVCpu);
3258 if ( (msrEFER & MSR_K6_EFER_LMA)
3259 && (oldval & X86_CR4_PAE)
3260 && !(val & X86_CR4_PAE))
3261 {
3262 return VERR_EM_INTERPRETER; /** @todo generate \#GP(0) */
3263 }
3264
3265 /* From IEM iemCImpl_load_CrX. */
3266 /** @todo Check guest CPUID bits for determining corresponding valid bits. */
3267 fValid = X86_CR4_VME | X86_CR4_PVI
3268 | X86_CR4_TSD | X86_CR4_DE
3269 | X86_CR4_PSE | X86_CR4_PAE
3270 | X86_CR4_MCE | X86_CR4_PGE
3271 | X86_CR4_PCE | X86_CR4_OSFXSR
3272 | X86_CR4_OSXMMEEXCPT;
3273 //if (xxx)
3274 // fValid |= X86_CR4_VMXE;
3275 //if (xxx)
3276 // fValid |= X86_CR4_OSXSAVE;
3277 if (val & ~(uint64_t)fValid)
3278 {
3279 Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", val, val & ~(uint64_t)fValid));
3280 return VERR_EM_INTERPRETER; /** @todo generate \#GP(0) */
3281 }
3282
3283 rc = VINF_SUCCESS;
3284 if ( (oldval & (X86_CR4_PGE|X86_CR4_PAE|X86_CR4_PSE))
3285 != (val & (X86_CR4_PGE|X86_CR4_PAE|X86_CR4_PSE)))
3286 {
3287 /* global flush */
3288 rc = PGMFlushTLB(pVCpu, CPUMGetGuestCR3(pVCpu), true /* global */);
3289 AssertRCReturn(rc, rc);
3290 }
3291
3292 /* Feeling extremely lazy. */
3293# ifdef IN_RC
3294 if ( (oldval & (X86_CR4_OSFXSR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME))
3295 != (val & (X86_CR4_OSFXSR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME)))
3296 {
3297 Log(("emInterpretMovCRx: CR4: %#RX64->%#RX64 => R3\n", oldval, val));
3298 VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
3299 }
3300# endif
3301# ifdef VBOX_WITH_RAW_MODE
3302 if (((val ^ oldval) & X86_CR4_VME) && !HMIsEnabled(pVM))
3303 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
3304# endif
3305
3306 rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
3307 return rc2 == VINF_SUCCESS ? rc : rc2;
3308
3309 case DISCREG_CR8:
3310 return APICSetTpr(pVCpu, val << 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
3311
3312 default:
3313 AssertFailed();
3314 case DISCREG_CR1: /* illegal op */
3315 break;
3316 }
3317 return VERR_EM_INTERPRETER;
3318}
3319
3320
3321/**
3322 * LMSW Emulation.
3323 */
3324static int emInterpretLmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3325{
3326 DISQPVPARAMVAL param1;
3327 uint32_t val;
3328 NOREF(pvFault); NOREF(pcbSize);
3329 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
3330
3331 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
3332 if(RT_FAILURE(rc))
3333 return VERR_EM_INTERPRETER;
3334
3335 switch(param1.type)
3336 {
3337 case DISQPV_TYPE_IMMEDIATE:
3338 case DISQPV_TYPE_ADDRESS:
3339 if(!(param1.flags & DISQPV_FLAG_16))
3340 return VERR_EM_INTERPRETER;
3341 val = param1.val.val32;
3342 break;
3343
3344 default:
3345 return VERR_EM_INTERPRETER;
3346 }
3347
3348 LogFlow(("emInterpretLmsw %x\n", val));
3349 uint64_t OldCr0 = CPUMGetGuestCR0(pVCpu);
3350
3351 /* Only PE, MP, EM and TS can be changed; note that PE can't be cleared by this instruction. */
3352 uint64_t NewCr0 = ( OldCr0 & ~( X86_CR0_MP | X86_CR0_EM | X86_CR0_TS))
3353 | (val & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS));
3354
3355 return emUpdateCRx(pVM, pVCpu, pRegFrame, DISCREG_CR0, NewCr0);
3356
3357}
3358
3359#ifdef EM_EMULATE_SMSW
3360/**
3361 * SMSW Emulation.
3362 */
3363static int emInterpretSmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3364{
3365 NOREF(pvFault); NOREF(pcbSize);
3366 DISQPVPARAMVAL param1;
3367 uint64_t cr0 = CPUMGetGuestCR0(pVCpu);
3368
3369 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
3370 if(RT_FAILURE(rc))
3371 return VERR_EM_INTERPRETER;
3372
3373 switch(param1.type)
3374 {
3375 case DISQPV_TYPE_IMMEDIATE:
3376 if(param1.size != sizeof(uint16_t))
3377 return VERR_EM_INTERPRETER;
3378 LogFlow(("emInterpretSmsw %d <- cr0 (%x)\n", pDis->Param1.Base.idxGenReg, cr0));
3379 rc = DISWriteReg16(pRegFrame, pDis->Param1.Base.idxGenReg, cr0);
3380 break;
3381
3382 case DISQPV_TYPE_ADDRESS:
3383 {
3384 RTGCPTR pParam1;
3385
3386 /* Actually forced to 16 bits regardless of the operand size. */
3387 if(param1.size != sizeof(uint16_t))
3388 return VERR_EM_INTERPRETER;
3389
3390 pParam1 = (RTGCPTR)param1.val.val64;
3391 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, pParam1);
3392 LogFlow(("emInterpretSmsw %RGv <- cr0 (%x)\n", pParam1, cr0));
3393
3394 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &cr0, sizeof(uint16_t));
3395 if (RT_FAILURE(rc))
3396 {
3397 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
3398 return VERR_EM_INTERPRETER;
3399 }
3400 break;
3401 }
3402
3403 default:
3404 return VERR_EM_INTERPRETER;
3405 }
3406
3407 LogFlow(("emInterpretSmsw %x\n", cr0));
3408 return rc;
3409}
3410#endif
3411
3412
3413/**
3414 * Interpret CRx read.
3415 *
3416 * @returns VBox status code.
3417 * @param pVM The cross context VM structure.
3418 * @param pVCpu The cross context virtual CPU structure.
3419 * @param pRegFrame The register frame.
3420 * @param DestRegGen General purpose register index (USE_REG_E**))
3421 * @param SrcRegCrx CRx register index (DISUSE_REG_CR*)
3422 *
3423 */
3424static int emInterpretCRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegCrx)
3425{
3426 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
3427 uint64_t val64;
3428 int rc = CPUMGetGuestCRx(pVCpu, SrcRegCrx, &val64);
3429 AssertMsgRCReturn(rc, ("CPUMGetGuestCRx %d failed\n", SrcRegCrx), VERR_EM_INTERPRETER);
3430 NOREF(pVM);
3431
3432 if (CPUMIsGuestIn64BitCode(pVCpu))
3433 rc = DISWriteReg64(pRegFrame, DestRegGen, val64);
3434 else
3435 rc = DISWriteReg32(pRegFrame, DestRegGen, val64);
3436
3437 if (RT_SUCCESS(rc))
3438 {
3439 LogFlow(("MOV_CR: gen32=%d CR=%d val=%RX64\n", DestRegGen, SrcRegCrx, val64));
3440 return VINF_SUCCESS;
3441 }
3442 return VERR_EM_INTERPRETER;
3443}
3444
3445
3446/**
3447 * Interpret CRx write.
3448 *
3449 * @returns VBox status code.
3450 * @param pVM The cross context VM structure.
3451 * @param pVCpu The cross context virtual CPU structure.
3452 * @param pRegFrame The register frame.
3453 * @param DestRegCrx CRx register index (DISUSE_REG_CR*)
3454 * @param SrcRegGen General purpose register index (USE_REG_E**))
3455 *
3456 */
3457static int emInterpretCRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint32_t SrcRegGen)
3458{
3459 uint64_t val;
3460 int rc;
3461 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
3462
3463 if (CPUMIsGuestIn64BitCode(pVCpu))
3464 rc = DISFetchReg64(pRegFrame, SrcRegGen, &val);
3465 else
3466 {
3467 uint32_t val32;
3468 rc = DISFetchReg32(pRegFrame, SrcRegGen, &val32);
3469 val = val32;
3470 }
3471
3472 if (RT_SUCCESS(rc))
3473 return emUpdateCRx(pVM, pVCpu, pRegFrame, DestRegCrx, val);
3474
3475 return VERR_EM_INTERPRETER;
3476}
3477
3478
3479/**
3480 * MOV CRx
3481 */
3482static int emInterpretMovCRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3483{
3484 NOREF(pvFault); NOREF(pcbSize);
3485 if ((pDis->Param1.fUse == DISUSE_REG_GEN32 || pDis->Param1.fUse == DISUSE_REG_GEN64) && pDis->Param2.fUse == DISUSE_REG_CR)
3486 return emInterpretCRxRead(pVM, pVCpu, pRegFrame, pDis->Param1.Base.idxGenReg, pDis->Param2.Base.idxCtrlReg);
3487
3488 if (pDis->Param1.fUse == DISUSE_REG_CR && (pDis->Param2.fUse == DISUSE_REG_GEN32 || pDis->Param2.fUse == DISUSE_REG_GEN64))
3489 return emInterpretCRxWrite(pVM, pVCpu, pRegFrame, pDis->Param1.Base.idxCtrlReg, pDis->Param2.Base.idxGenReg);
3490
3491 AssertMsgFailedReturn(("Unexpected control register move\n"), VERR_EM_INTERPRETER);
3492}
3493
3494
3495/**
3496 * MOV DRx
3497 */
3498static int emInterpretMovDRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3499{
3500 int rc = VERR_EM_INTERPRETER;
3501 NOREF(pvFault); NOREF(pcbSize);
3502
3503 if((pDis->Param1.fUse == DISUSE_REG_GEN32 || pDis->Param1.fUse == DISUSE_REG_GEN64) && pDis->Param2.fUse == DISUSE_REG_DBG)
3504 {
3505 rc = EMInterpretDRxRead(pVM, pVCpu, pRegFrame, pDis->Param1.Base.idxGenReg, pDis->Param2.Base.idxDbgReg);
3506 }
3507 else
3508 if(pDis->Param1.fUse == DISUSE_REG_DBG && (pDis->Param2.fUse == DISUSE_REG_GEN32 || pDis->Param2.fUse == DISUSE_REG_GEN64))
3509 {
3510 rc = EMInterpretDRxWrite(pVM, pVCpu, pRegFrame, pDis->Param1.Base.idxDbgReg, pDis->Param2.Base.idxGenReg);
3511 }
3512 else
3513 AssertMsgFailed(("Unexpected debug register move\n"));
3514
3515 return rc;
3516}
3517
3518
3519/**
3520 * LLDT Emulation.
3521 */
3522static int emInterpretLLdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3523{
3524 DISQPVPARAMVAL param1;
3525 RTSEL sel;
3526 NOREF(pVM); NOREF(pvFault); NOREF(pcbSize);
3527
3528 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
3529 if(RT_FAILURE(rc))
3530 return VERR_EM_INTERPRETER;
3531
3532 switch(param1.type)
3533 {
3534 case DISQPV_TYPE_ADDRESS:
3535 return VERR_EM_INTERPRETER; //feeling lazy right now
3536
3537 case DISQPV_TYPE_IMMEDIATE:
3538 if(!(param1.flags & DISQPV_FLAG_16))
3539 return VERR_EM_INTERPRETER;
3540 sel = (RTSEL)param1.val.val16;
3541 break;
3542
3543 default:
3544 return VERR_EM_INTERPRETER;
3545 }
3546
3547#ifdef IN_RING0
3548 /* Only for the VT-x real-mode emulation case. */
3549 AssertReturn(CPUMIsGuestInRealMode(pVCpu), VERR_EM_INTERPRETER);
3550 CPUMSetGuestLDTR(pVCpu, sel);
3551 return VINF_SUCCESS;
3552#else
3553 if (sel == 0)
3554 {
3555 if (CPUMGetHyperLDTR(pVCpu) == 0)
3556 {
3557 // this simple case is most frequent in Windows 2000 (31k - boot & shutdown)
3558 return VINF_SUCCESS;
3559 }
3560 }
3561 //still feeling lazy
3562 return VERR_EM_INTERPRETER;
3563#endif
3564}
3565
3566#ifdef IN_RING0
3567/**
3568 * LIDT/LGDT Emulation.
3569 */
3570static int emInterpretLIGdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3571{
3572 DISQPVPARAMVAL param1;
3573 RTGCPTR pParam1;
3574 X86XDTR32 dtr32;
3575 NOREF(pvFault); NOREF(pcbSize);
3576
3577 Log(("Emulate %s at %RGv\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip));
3578
3579 /* Only for the VT-x real-mode emulation case. */
3580 AssertReturn(CPUMIsGuestInRealMode(pVCpu), VERR_EM_INTERPRETER);
3581
3582 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->Param1, &param1, DISQPVWHICH_SRC);
3583 if(RT_FAILURE(rc))
3584 return VERR_EM_INTERPRETER;
3585
3586 switch(param1.type)
3587 {
3588 case DISQPV_TYPE_ADDRESS:
3589 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->Param1, param1.val.val16);
3590 break;
3591
3592 default:
3593 return VERR_EM_INTERPRETER;
3594 }
3595
3596 rc = emRamRead(pVM, pVCpu, pRegFrame, &dtr32, pParam1, sizeof(dtr32));
3597 AssertRCReturn(rc, VERR_EM_INTERPRETER);
3598
3599 if (!(pDis->fPrefix & DISPREFIX_OPSIZE))
3600 dtr32.uAddr &= 0xffffff; /* 16 bits operand size */
3601
3602 if (pDis->pCurInstr->uOpcode == OP_LIDT)
3603 CPUMSetGuestIDTR(pVCpu, dtr32.uAddr, dtr32.cb);
3604 else
3605 CPUMSetGuestGDTR(pVCpu, dtr32.uAddr, dtr32.cb);
3606
3607 return VINF_SUCCESS;
3608}
3609#endif
3610
3611
3612#ifdef IN_RC
3613/**
3614 * STI Emulation.
3615 *
3616 * @remark the instruction following sti is guaranteed to be executed before any interrupts are dispatched
3617 */
3618static int emInterpretSti(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3619{
3620 NOREF(pcbSize);
3621 PPATMGCSTATE pGCState = PATMGetGCState(pVM);
3622
3623 if(!pGCState)
3624 {
3625 Assert(pGCState);
3626 return VERR_EM_INTERPRETER;
3627 }
3628 pGCState->uVMFlags |= X86_EFL_IF;
3629
3630 Assert(pRegFrame->eflags.u32 & X86_EFL_IF);
3631 Assert(pvFault == SELMToFlat(pVM, DISSELREG_CS, pRegFrame, (RTGCPTR)pRegFrame->rip));
3632
3633 pVCpu->em.s.GCPtrInhibitInterrupts = pRegFrame->eip + pDis->cbInstr;
3634 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
3635
3636 return VINF_SUCCESS;
3637}
3638#endif /* IN_RC */
3639
3640
3641/**
3642 * HLT Emulation.
3643 */
3644static VBOXSTRICTRC
3645emInterpretHlt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3646{
3647 NOREF(pVM); NOREF(pVCpu); NOREF(pDis); NOREF(pRegFrame); NOREF(pvFault); NOREF(pcbSize);
3648 return VINF_EM_HALT;
3649}
3650
3651
3652/**
3653 * RDTSC Emulation.
3654 */
3655static int emInterpretRdtsc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3656{
3657 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3658 return EMInterpretRdtsc(pVM, pVCpu, pRegFrame);
3659}
3660
3661/**
3662 * RDPMC Emulation
3663 */
3664static int emInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3665{
3666 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3667 return EMInterpretRdpmc(pVM, pVCpu, pRegFrame);
3668}
3669
3670
3671static int emInterpretMonitor(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3672{
3673 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3674 return EMInterpretMonitor(pVM, pVCpu, pRegFrame);
3675}
3676
3677
3678static VBOXSTRICTRC emInterpretMWait(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3679{
3680 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3681 return EMInterpretMWait(pVM, pVCpu, pRegFrame);
3682}
3683
3684
3685/**
3686 * RDMSR Emulation.
3687 */
3688static int emInterpretRdmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3689{
3690 /* Note: The Intel manual claims there's a REX version of RDMSR that's slightly
3691 different, so we play safe by completely disassembling the instruction. */
3692 Assert(!(pDis->fPrefix & DISPREFIX_REX));
3693 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3694 return EMInterpretRdmsr(pVM, pVCpu, pRegFrame);
3695}
3696
3697
3698/**
3699 * WRMSR Emulation.
3700 */
3701static int emInterpretWrmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
3702{
3703 NOREF(pDis); NOREF(pvFault); NOREF(pcbSize);
3704 return EMInterpretWrmsr(pVM, pVCpu, pRegFrame);
3705}
3706
3707
3708/**
3709 * Internal worker.
3710 * @copydoc emInterpretInstructionCPUOuter
3711 * @param pVM The cross context VM structure.
3712 */
3713DECLINLINE(VBOXSTRICTRC) emInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
3714 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize)
3715{
3716 Assert(pRegFrame == CPUMGetGuestCtxCore(pVCpu));
3717 Assert(enmCodeType == EMCODETYPE_SUPERVISOR || enmCodeType == EMCODETYPE_ALL);
3718 Assert(pcbSize);
3719 *pcbSize = 0;
3720
3721 if (enmCodeType == EMCODETYPE_SUPERVISOR)
3722 {
3723 /*
3724 * Only supervisor guest code!!
3725 * And no complicated prefixes.
3726 */
3727 /* Get the current privilege level. */
3728 uint32_t cpl = CPUMGetGuestCPL(pVCpu);
3729#ifdef VBOX_WITH_RAW_RING1
3730 if ( !EMIsRawRing1Enabled(pVM)
3731 || cpl > 1
3732 || pRegFrame->eflags.Bits.u2IOPL > cpl
3733 )
3734#endif
3735 {
3736 if ( cpl != 0
3737 && pDis->pCurInstr->uOpcode != OP_RDTSC) /* rdtsc requires emulation in ring 3 as well */
3738 {
3739 Log(("WARNING: refusing instruction emulation for user-mode code!!\n"));
3740 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedUserMode));
3741 return VERR_EM_INTERPRETER;
3742 }
3743 }
3744 }
3745 else
3746 Log2(("emInterpretInstructionCPU allowed to interpret user-level code!!\n"));
3747
3748#ifdef IN_RC
3749 if ( (pDis->fPrefix & (DISPREFIX_REPNE | DISPREFIX_REP))
3750 || ( (pDis->fPrefix & DISPREFIX_LOCK)
3751 && pDis->pCurInstr->uOpcode != OP_CMPXCHG
3752 && pDis->pCurInstr->uOpcode != OP_CMPXCHG8B
3753 && pDis->pCurInstr->uOpcode != OP_XADD
3754 && pDis->pCurInstr->uOpcode != OP_OR
3755 && pDis->pCurInstr->uOpcode != OP_AND
3756 && pDis->pCurInstr->uOpcode != OP_XOR
3757 && pDis->pCurInstr->uOpcode != OP_BTR
3758 )
3759 )
3760#else
3761 if ( (pDis->fPrefix & DISPREFIX_REPNE)
3762 || ( (pDis->fPrefix & DISPREFIX_REP)
3763 && pDis->pCurInstr->uOpcode != OP_STOSWD
3764 )
3765 || ( (pDis->fPrefix & DISPREFIX_LOCK)
3766 && pDis->pCurInstr->uOpcode != OP_OR
3767 && pDis->pCurInstr->uOpcode != OP_AND
3768 && pDis->pCurInstr->uOpcode != OP_XOR
3769 && pDis->pCurInstr->uOpcode != OP_BTR
3770 && pDis->pCurInstr->uOpcode != OP_CMPXCHG
3771 && pDis->pCurInstr->uOpcode != OP_CMPXCHG8B
3772 )
3773 )
3774#endif
3775 {
3776 //Log(("EMInterpretInstruction: wrong prefix!!\n"));
3777 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedPrefix));
3778 Log4(("EM: Refuse %u on REP/REPNE/LOCK prefix grounds\n", pDis->pCurInstr->uOpcode));
3779 return VERR_EM_INTERPRETER;
3780 }
3781
3782#if HC_ARCH_BITS == 32
3783 /*
3784 * Unable to emulate most >4 bytes accesses in 32 bits mode.
3785 * Whitelisted instructions are safe.
3786 */
3787 if ( pDis->Param1.cb > 4
3788 && CPUMIsGuestIn64BitCode(pVCpu))
3789 {
3790 uint32_t uOpCode = pDis->pCurInstr->uOpcode;
3791 if ( uOpCode != OP_STOSWD
3792 && uOpCode != OP_MOV
3793 && uOpCode != OP_CMPXCHG8B
3794 && uOpCode != OP_XCHG
3795 && uOpCode != OP_BTS
3796 && uOpCode != OP_BTR
3797 && uOpCode != OP_BTC
3798 )
3799 {
3800# ifdef VBOX_WITH_STATISTICS
3801 switch (pDis->pCurInstr->uOpcode)
3802 {
3803# define INTERPRET_FAILED_CASE(opcode, Instr) \
3804 case opcode: STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); break;
3805 INTERPRET_FAILED_CASE(OP_XCHG,Xchg);
3806 INTERPRET_FAILED_CASE(OP_DEC,Dec);
3807 INTERPRET_FAILED_CASE(OP_INC,Inc);
3808 INTERPRET_FAILED_CASE(OP_POP,Pop);
3809 INTERPRET_FAILED_CASE(OP_OR, Or);
3810 INTERPRET_FAILED_CASE(OP_XOR,Xor);
3811 INTERPRET_FAILED_CASE(OP_AND,And);
3812 INTERPRET_FAILED_CASE(OP_MOV,Mov);
3813 INTERPRET_FAILED_CASE(OP_STOSWD,StosWD);
3814 INTERPRET_FAILED_CASE(OP_INVLPG,InvlPg);
3815 INTERPRET_FAILED_CASE(OP_CPUID,CpuId);
3816 INTERPRET_FAILED_CASE(OP_MOV_CR,MovCRx);
3817 INTERPRET_FAILED_CASE(OP_MOV_DR,MovDRx);
3818 INTERPRET_FAILED_CASE(OP_LLDT,LLdt);
3819 INTERPRET_FAILED_CASE(OP_LIDT,LIdt);
3820 INTERPRET_FAILED_CASE(OP_LGDT,LGdt);
3821 INTERPRET_FAILED_CASE(OP_LMSW,Lmsw);
3822 INTERPRET_FAILED_CASE(OP_CLTS,Clts);
3823 INTERPRET_FAILED_CASE(OP_MONITOR,Monitor);
3824 INTERPRET_FAILED_CASE(OP_MWAIT,MWait);
3825 INTERPRET_FAILED_CASE(OP_RDMSR,Rdmsr);
3826 INTERPRET_FAILED_CASE(OP_WRMSR,Wrmsr);
3827 INTERPRET_FAILED_CASE(OP_ADD,Add);
3828 INTERPRET_FAILED_CASE(OP_SUB,Sub);
3829 INTERPRET_FAILED_CASE(OP_ADC,Adc);
3830 INTERPRET_FAILED_CASE(OP_BTR,Btr);
3831 INTERPRET_FAILED_CASE(OP_BTS,Bts);
3832 INTERPRET_FAILED_CASE(OP_BTC,Btc);
3833 INTERPRET_FAILED_CASE(OP_RDTSC,Rdtsc);
3834 INTERPRET_FAILED_CASE(OP_CMPXCHG, CmpXchg);
3835 INTERPRET_FAILED_CASE(OP_STI, Sti);
3836 INTERPRET_FAILED_CASE(OP_XADD,XAdd);
3837 INTERPRET_FAILED_CASE(OP_CMPXCHG8B,CmpXchg8b);
3838 INTERPRET_FAILED_CASE(OP_HLT, Hlt);
3839 INTERPRET_FAILED_CASE(OP_IRET,Iret);
3840 INTERPRET_FAILED_CASE(OP_WBINVD,WbInvd);
3841 INTERPRET_FAILED_CASE(OP_MOVNTPS,MovNTPS);
3842# undef INTERPRET_FAILED_CASE
3843 default:
3844 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedMisc));
3845 break;
3846 }
3847# endif /* VBOX_WITH_STATISTICS */
3848 Log4(("EM: Refuse %u on grounds of accessing %u bytes\n", pDis->pCurInstr->uOpcode, pDis->Param1.cb));
3849 return VERR_EM_INTERPRETER;
3850 }
3851 }
3852#endif
3853
3854 VBOXSTRICTRC rc;
3855#if (defined(VBOX_STRICT) || defined(LOG_ENABLED))
3856 LogFlow(("emInterpretInstructionCPU %s\n", emGetMnemonic(pDis)));
3857#endif
3858 switch (pDis->pCurInstr->uOpcode)
3859 {
3860 /*
3861 * Macros for generating the right case statements.
3862 */
3863# ifndef VBOX_COMPARE_IEM_AND_EM
3864# define INTERPRET_CASE_EX_LOCK_PARAM3(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock) \
3865 case opcode:\
3866 if (pDis->fPrefix & DISPREFIX_LOCK) \
3867 rc = emInterpretLock##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulateLock); \
3868 else \
3869 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulate); \
3870 if (RT_SUCCESS(rc)) \
3871 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
3872 else \
3873 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
3874 return rc
3875# else /* VBOX_COMPARE_IEM_AND_EM */
3876# define INTERPRET_CASE_EX_LOCK_PARAM3(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock) \
3877 case opcode:\
3878 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulate); \
3879 if (RT_SUCCESS(rc)) \
3880 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
3881 else \
3882 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
3883 return rc
3884# endif /* VBOX_COMPARE_IEM_AND_EM */
3885
3886#define INTERPRET_CASE_EX_PARAM3(opcode, Instr, InstrFn, pfnEmulate) \
3887 case opcode:\
3888 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulate); \
3889 if (RT_SUCCESS(rc)) \
3890 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
3891 else \
3892 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
3893 return rc
3894
3895#define INTERPRET_CASE_EX_PARAM2(opcode, Instr, InstrFn, pfnEmulate) \
3896 INTERPRET_CASE_EX_PARAM3(opcode, Instr, InstrFn, pfnEmulate)
3897#define INTERPRET_CASE_EX_LOCK_PARAM2(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock) \
3898 INTERPRET_CASE_EX_LOCK_PARAM3(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock)
3899
3900#define INTERPRET_CASE(opcode, Instr) \
3901 case opcode:\
3902 rc = emInterpret##Instr(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize); \
3903 if (RT_SUCCESS(rc)) \
3904 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
3905 else \
3906 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
3907 return rc
3908
3909#define INTERPRET_CASE_EX_DUAL_PARAM2(opcode, Instr, InstrFn) \
3910 case opcode:\
3911 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize); \
3912 if (RT_SUCCESS(rc)) \
3913 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
3914 else \
3915 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
3916 return rc
3917
3918#define INTERPRET_STAT_CASE(opcode, Instr) \
3919 case opcode: STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); return VERR_EM_INTERPRETER;
3920
3921 /*
3922 * The actual case statements.
3923 */
3924 INTERPRET_CASE(OP_XCHG,Xchg);
3925 INTERPRET_CASE_EX_PARAM2(OP_DEC,Dec, IncDec, EMEmulateDec);
3926 INTERPRET_CASE_EX_PARAM2(OP_INC,Inc, IncDec, EMEmulateInc);
3927 INTERPRET_CASE(OP_POP,Pop);
3928 INTERPRET_CASE_EX_LOCK_PARAM3(OP_OR, Or, OrXorAnd, EMEmulateOr, EMEmulateLockOr);
3929 INTERPRET_CASE_EX_LOCK_PARAM3(OP_XOR,Xor, OrXorAnd, EMEmulateXor, EMEmulateLockXor);
3930 INTERPRET_CASE_EX_LOCK_PARAM3(OP_AND,And, OrXorAnd, EMEmulateAnd, EMEmulateLockAnd);
3931 INTERPRET_CASE(OP_MOV,Mov);
3932#ifndef IN_RC
3933 INTERPRET_CASE(OP_STOSWD,StosWD);
3934#endif
3935 INTERPRET_CASE(OP_INVLPG,InvlPg);
3936 INTERPRET_CASE(OP_CPUID,CpuId);
3937 INTERPRET_CASE(OP_MOV_CR,MovCRx);
3938 INTERPRET_CASE(OP_MOV_DR,MovDRx);
3939#ifdef IN_RING0
3940 INTERPRET_CASE_EX_DUAL_PARAM2(OP_LIDT, LIdt, LIGdt);
3941 INTERPRET_CASE_EX_DUAL_PARAM2(OP_LGDT, LGdt, LIGdt);
3942#endif
3943 INTERPRET_CASE(OP_LLDT,LLdt);
3944 INTERPRET_CASE(OP_LMSW,Lmsw);
3945#ifdef EM_EMULATE_SMSW
3946 INTERPRET_CASE(OP_SMSW,Smsw);
3947#endif
3948 INTERPRET_CASE(OP_CLTS,Clts);
3949 INTERPRET_CASE(OP_MONITOR, Monitor);
3950 INTERPRET_CASE(OP_MWAIT, MWait);
3951 INTERPRET_CASE(OP_RDMSR, Rdmsr);
3952 INTERPRET_CASE(OP_WRMSR, Wrmsr);
3953 INTERPRET_CASE_EX_PARAM3(OP_ADD,Add, AddSub, EMEmulateAdd);
3954 INTERPRET_CASE_EX_PARAM3(OP_SUB,Sub, AddSub, EMEmulateSub);
3955 INTERPRET_CASE(OP_ADC,Adc);
3956 INTERPRET_CASE_EX_LOCK_PARAM2(OP_BTR,Btr, BitTest, EMEmulateBtr, EMEmulateLockBtr);
3957 INTERPRET_CASE_EX_PARAM2(OP_BTS,Bts, BitTest, EMEmulateBts);
3958 INTERPRET_CASE_EX_PARAM2(OP_BTC,Btc, BitTest, EMEmulateBtc);
3959 INTERPRET_CASE(OP_RDPMC,Rdpmc);
3960 INTERPRET_CASE(OP_RDTSC,Rdtsc);
3961 INTERPRET_CASE(OP_CMPXCHG, CmpXchg);
3962#ifdef IN_RC
3963 INTERPRET_CASE(OP_STI,Sti);
3964 INTERPRET_CASE(OP_XADD, XAdd);
3965 INTERPRET_CASE(OP_IRET,Iret);
3966#endif
3967 INTERPRET_CASE(OP_CMPXCHG8B, CmpXchg8b);
3968 INTERPRET_CASE(OP_HLT,Hlt);
3969 INTERPRET_CASE(OP_WBINVD,WbInvd);
3970#ifdef VBOX_WITH_STATISTICS
3971# ifndef IN_RC
3972 INTERPRET_STAT_CASE(OP_XADD, XAdd);
3973# endif
3974 INTERPRET_STAT_CASE(OP_MOVNTPS,MovNTPS);
3975#endif
3976
3977 default:
3978 Log3(("emInterpretInstructionCPU: opcode=%d\n", pDis->pCurInstr->uOpcode));
3979 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedMisc));
3980 return VERR_EM_INTERPRETER;
3981
3982#undef INTERPRET_CASE_EX_PARAM2
3983#undef INTERPRET_STAT_CASE
3984#undef INTERPRET_CASE_EX
3985#undef INTERPRET_CASE
3986 } /* switch (opcode) */
3987 /* not reached */
3988}
3989
3990/**
3991 * Interprets the current instruction using the supplied DISCPUSTATE structure.
3992 *
3993 * EIP is *NOT* updated!
3994 *
3995 * @returns VBox strict status code.
3996 * @retval VINF_* Scheduling instructions. When these are returned, it
3997 * starts to get a bit tricky to know whether code was
3998 * executed or not... We'll address this when it becomes a problem.
3999 * @retval VERR_EM_INTERPRETER Something we can't cope with.
4000 * @retval VERR_* Fatal errors.
4001 *
4002 * @param pVCpu The cross context virtual CPU structure.
4003 * @param pDis The disassembler cpu state for the instruction to be
4004 * interpreted.
4005 * @param pRegFrame The register frame. EIP is *NOT* changed!
4006 * @param pvFault The fault address (CR2).
4007 * @param pcbSize Size of the write (if applicable).
4008 * @param enmCodeType Code type (user/supervisor)
4009 *
4010 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
4011 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
4012 * to worry about e.g. invalid modrm combinations (!)
4013 *
4014 * @todo At this time we do NOT check if the instruction overwrites vital information.
4015 * Make sure this can't happen!! (will add some assertions/checks later)
4016 */
4017DECLINLINE(VBOXSTRICTRC) emInterpretInstructionCPUOuter(PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
4018 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize)
4019{
4020 STAM_PROFILE_START(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Emulate), a);
4021 VBOXSTRICTRC rc = emInterpretInstructionCPU(pVCpu->CTX_SUFF(pVM), pVCpu, pDis, pRegFrame, pvFault, enmCodeType, pcbSize);
4022 STAM_PROFILE_STOP(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Emulate), a);
4023 if (RT_SUCCESS(rc))
4024 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,InterpretSucceeded));
4025 else
4026 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,InterpretFailed));
4027 return rc;
4028}
4029
4030
4031#endif /* !VBOX_WITH_IEM */
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