VirtualBox

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

Last change on this file since 37529 was 36315, checked in by vboxsync, 14 years ago

VMM: more correct MSR_IA32_MISC_ENABLE emulation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 106.7 KB
Line 
1/* $Id: EMAll.cpp 36315 2011-03-18 15:49:23Z vboxsync $ */
2/** @file
3 * EM - Execution Monitor(/Manager) - All contexts
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_EM
22#include <VBox/vmm/em.h>
23#include <VBox/vmm/mm.h>
24#include <VBox/vmm/selm.h>
25#include <VBox/vmm/patm.h>
26#include <VBox/vmm/csam.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/iom.h>
29#include <VBox/vmm/stam.h>
30#include "EMInternal.h"
31#include <VBox/vmm/vm.h>
32#include <VBox/vmm/vmm.h>
33#include <VBox/vmm/hwaccm.h>
34#include <VBox/vmm/tm.h>
35#include <VBox/vmm/pdmapi.h>
36#include <VBox/param.h>
37#include <VBox/err.h>
38#include <VBox/dis.h>
39#include <VBox/disopcode.h>
40#include <VBox/log.h>
41#include "internal/pgm.h"
42#include <iprt/assert.h>
43#include <iprt/asm.h>
44#include <iprt/string.h>
45
46
47/*******************************************************************************
48* Defined Constants And Macros *
49*******************************************************************************/
50/** @def EM_ASSERT_FAULT_RETURN
51 * Safety check.
52 *
53 * Could in theory misfire on a cross page boundary access...
54 *
55 * Currently disabled because the CSAM (+ PATM) patch monitoring occasionally
56 * turns up an alias page instead of the original faulting one and annoying the
57 * heck out of anyone running a debug build. See @bugref{2609} and @bugref{1931}.
58 */
59#if 0
60# define EM_ASSERT_FAULT_RETURN(expr, rc) AssertReturn(expr, rc)
61#else
62# define EM_ASSERT_FAULT_RETURN(expr, rc) do { } while (0)
63#endif
64
65/* Used to pass information during instruction disassembly. */
66typedef struct
67{
68 PVM pVM;
69 PVMCPU pVCpu;
70 RTGCPTR GCPtr;
71 uint8_t aOpcode[8];
72} EMDISSTATE, *PEMDISSTATE;
73
74/*******************************************************************************
75* Internal Functions *
76*******************************************************************************/
77DECLINLINE(VBOXSTRICTRC) emInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
78 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize);
79
80
81
82/**
83 * Get the current execution manager status.
84 *
85 * @returns Current status.
86 * @param pVCpu The VMCPU to operate on.
87 */
88VMMDECL(EMSTATE) EMGetState(PVMCPU pVCpu)
89{
90 return pVCpu->em.s.enmState;
91}
92
93/**
94 * Sets the current execution manager status. (use only when you know what you're doing!)
95 *
96 * @param pVCpu The VMCPU to operate on.
97 */
98VMMDECL(void) EMSetState(PVMCPU pVCpu, EMSTATE enmNewState)
99{
100 /* Only allowed combination: */
101 Assert(pVCpu->em.s.enmState == EMSTATE_WAIT_SIPI && enmNewState == EMSTATE_HALTED);
102 pVCpu->em.s.enmState = enmNewState;
103}
104
105
106/**
107 * Read callback for disassembly function; supports reading bytes that cross a page boundary
108 *
109 * @returns VBox status code.
110 * @param pSrc GC source pointer
111 * @param pDest HC destination pointer
112 * @param cb Number of bytes to read
113 * @param dwUserdata Callback specific user data (pDis)
114 *
115 */
116DECLCALLBACK(int) EMReadBytes(RTUINTPTR pSrc, uint8_t *pDest, unsigned cb, void *pvUserdata)
117{
118 PDISCPUSTATE pDis = (PDISCPUSTATE)pvUserdata;
119 PEMDISSTATE pState = (PEMDISSTATE)pDis->apvUserData[0];
120 PVM pVM = pState->pVM;
121 PVMCPU pVCpu = pState->pVCpu;
122
123# ifdef IN_RING0
124 int rc;
125
126 if ( pState->GCPtr
127 && pSrc + cb <= pState->GCPtr + sizeof(pState->aOpcode))
128 {
129 unsigned offset = pSrc - pState->GCPtr;
130
131 Assert(pSrc >= pState->GCPtr);
132
133 for (unsigned i=0; i<cb; i++)
134 {
135 pDest[i] = pState->aOpcode[offset + i];
136 }
137 return VINF_SUCCESS;
138 }
139
140 rc = PGMPhysSimpleReadGCPtr(pVCpu, pDest, pSrc, cb);
141 AssertMsgRC(rc, ("PGMPhysSimpleReadGCPtr failed for pSrc=%RGv cb=%x rc=%d\n", pSrc, cb, rc));
142# elif defined(IN_RING3)
143 if (!PATMIsPatchGCAddr(pVM, pSrc))
144 {
145 int rc = PGMPhysSimpleReadGCPtr(pVCpu, pDest, pSrc, cb);
146 AssertRC(rc);
147 }
148 else
149 memcpy(pDest, PATMR3GCPtrToHCPtr(pVM, pSrc), cb);
150
151# elif defined(IN_RC)
152 if (!PATMIsPatchGCAddr(pVM, pSrc))
153 {
154 int rc = MMGCRamRead(pVM, pDest, (void *)(uintptr_t)pSrc, cb);
155 if (rc == VERR_ACCESS_DENIED)
156 {
157 /* Recently flushed; access the data manually. */
158 rc = PGMPhysSimpleReadGCPtr(pVCpu, pDest, pSrc, cb);
159 AssertRC(rc);
160 }
161 }
162 else /* the hypervisor region is always present. */
163 memcpy(pDest, (RTRCPTR)(uintptr_t)pSrc, cb);
164
165# endif /* IN_RING3 */
166 return VINF_SUCCESS;
167}
168
169
170#ifndef IN_RC
171DECLINLINE(int) emDisCoreOne(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
172{
173 EMDISSTATE State;
174
175 State.pVM = pVM;
176 State.pVCpu = pVCpu;
177 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &State.aOpcode, InstrGC, sizeof(State.aOpcode));
178 if (RT_SUCCESS(rc))
179 {
180 State.GCPtr = InstrGC;
181 }
182 else
183 {
184 if (PAGE_ADDRESS(InstrGC) == PAGE_ADDRESS(InstrGC + sizeof(State.aOpcode) - 1))
185 {
186 if (rc == VERR_PAGE_TABLE_NOT_PRESENT)
187 HWACCMInvalidatePage(pVCpu, InstrGC);
188
189 Log(("emDisCoreOne: read failed with %d\n", rc));
190 return rc;
191 }
192 State.GCPtr = NIL_RTGCPTR;
193 }
194 return DISCoreOneEx(InstrGC, pDis->mode, EMReadBytes, &State, pDis, pOpsize);
195}
196
197#else /* IN_RC */
198
199DECLINLINE(int) emDisCoreOne(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
200{
201 EMDISSTATE State;
202
203 State.pVM = pVM;
204 State.pVCpu = pVCpu;
205 State.GCPtr = InstrGC;
206
207 return DISCoreOneEx(InstrGC, pDis->mode, EMReadBytes, &State, pDis, pOpsize);
208}
209
210#endif /* IN_RC */
211
212
213/**
214 * Disassembles one instruction.
215 *
216 * @returns VBox status code, see SELMToFlatEx and EMInterpretDisasOneEx for
217 * details.
218 * @retval VERR_INTERNAL_ERROR on DISCoreOneEx failure.
219 *
220 * @param pVM The VM handle.
221 * @param pVCpu The VMCPU handle.
222 * @param pCtxCore The context core (used for both the mode and instruction).
223 * @param pDis Where to return the parsed instruction info.
224 * @param pcbInstr Where to return the instruction size. (optional)
225 */
226VMMDECL(int) EMInterpretDisasOne(PVM pVM, PVMCPU pVCpu, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, unsigned *pcbInstr)
227{
228 RTGCPTR GCPtrInstr;
229 int rc = SELMToFlatEx(pVM, DIS_SELREG_CS, pCtxCore, pCtxCore->rip, 0, &GCPtrInstr);
230 if (RT_FAILURE(rc))
231 {
232 Log(("EMInterpretDisasOne: Failed to convert %RTsel:%RGv (cpl=%d) - rc=%Rrc !!\n",
233 pCtxCore->cs, (RTGCPTR)pCtxCore->rip, pCtxCore->ss & X86_SEL_RPL, rc));
234 return rc;
235 }
236 return EMInterpretDisasOneEx(pVM, pVCpu, (RTGCUINTPTR)GCPtrInstr, pCtxCore, pDis, pcbInstr);
237}
238
239
240/**
241 * Disassembles one instruction.
242 *
243 * This is used by internally by the interpreter and by trap/access handlers.
244 *
245 * @returns VBox status code.
246 * @retval VERR_INTERNAL_ERROR on DISCoreOneEx failure.
247 *
248 * @param pVM The VM handle.
249 * @param pVCpu The VMCPU handle.
250 * @param GCPtrInstr The flat address of the instruction.
251 * @param pCtxCore The context core (used to determine the cpu mode).
252 * @param pDis Where to return the parsed instruction info.
253 * @param pcbInstr Where to return the instruction size. (optional)
254 */
255VMMDECL(int) EMInterpretDisasOneEx(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, unsigned *pcbInstr)
256{
257 int rc;
258 EMDISSTATE State;
259
260 State.pVM = pVM;
261 State.pVCpu = pVCpu;
262
263#ifdef IN_RC
264 State.GCPtr = GCPtrInstr;
265#else /* ring 0/3 */
266 rc = PGMPhysSimpleReadGCPtr(pVCpu, &State.aOpcode, GCPtrInstr, sizeof(State.aOpcode));
267 if (RT_SUCCESS(rc))
268 {
269 State.GCPtr = GCPtrInstr;
270 }
271 else
272 {
273 if (PAGE_ADDRESS(GCPtrInstr) == PAGE_ADDRESS(GCPtrInstr + sizeof(State.aOpcode) - 1))
274 {
275 if (rc == VERR_PAGE_TABLE_NOT_PRESENT)
276 HWACCMInvalidatePage(pVCpu, GCPtrInstr);
277
278 Log(("EMInterpretDisasOneEx: read failed with %d\n", rc));
279 return rc;
280 }
281 State.GCPtr = NIL_RTGCPTR;
282 }
283#endif
284
285 rc = DISCoreOneEx(GCPtrInstr, SELMGetCpuModeFromSelector(pVM, pCtxCore->eflags, pCtxCore->cs, (PCPUMSELREGHID)&pCtxCore->csHid),
286 EMReadBytes, &State,
287 pDis, pcbInstr);
288 if (RT_SUCCESS(rc))
289 return VINF_SUCCESS;
290 AssertMsgFailed(("DISCoreOne failed to GCPtrInstr=%RGv rc=%Rrc\n", GCPtrInstr, rc));
291 return VERR_INTERNAL_ERROR;
292}
293
294
295/**
296 * Interprets the current instruction.
297 *
298 * @returns VBox status code.
299 * @retval VINF_* Scheduling instructions.
300 * @retval VERR_EM_INTERPRETER Something we can't cope with.
301 * @retval VERR_* Fatal errors.
302 *
303 * @param pVM The VM handle.
304 * @param pVCpu The VMCPU handle.
305 * @param pRegFrame The register frame.
306 * Updates the EIP if an instruction was executed successfully.
307 * @param pvFault The fault address (CR2).
308 * @param pcbSize Size of the write (if applicable).
309 *
310 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
311 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
312 * to worry about e.g. invalid modrm combinations (!)
313 */
314VMMDECL(VBOXSTRICTRC) EMInterpretInstruction(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
315{
316 RTGCPTR pbCode;
317
318 LogFlow(("EMInterpretInstruction %RGv fault %RGv\n", (RTGCPTR)pRegFrame->rip, pvFault));
319 VBOXSTRICTRC rc = SELMToFlatEx(pVM, DIS_SELREG_CS, pRegFrame, pRegFrame->rip, 0, &pbCode);
320 if (RT_SUCCESS(rc))
321 {
322 uint32_t cbOp;
323 PDISCPUSTATE pDis = &pVCpu->em.s.DisState;
324 pDis->mode = SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid);
325 rc = emDisCoreOne(pVM, pVCpu, pDis, (RTGCUINTPTR)pbCode, &cbOp);
326 if (RT_SUCCESS(rc))
327 {
328 Assert(cbOp == pDis->opsize);
329 rc = EMInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_SUPERVISOR, pcbSize);
330 if (RT_SUCCESS(rc))
331 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
332
333 return rc;
334 }
335 }
336 return VERR_EM_INTERPRETER;
337}
338
339
340/**
341 * Interprets the current instruction using the supplied DISCPUSTATE structure.
342 *
343 * EIP is *NOT* updated!
344 *
345 * @returns VBox strict status code.
346 * @retval VINF_* Scheduling instructions. When these are returned, it
347 * starts to get a bit tricky to know whether code was
348 * executed or not... We'll address this when it becomes a problem.
349 * @retval VERR_EM_INTERPRETER Something we can't cope with.
350 * @retval VERR_* Fatal errors.
351 *
352 * @param pVM The VM handle.
353 * @param pVCpu The VMCPU handle.
354 * @param pDis The disassembler cpu state for the instruction to be
355 * interpreted.
356 * @param pRegFrame The register frame. EIP is *NOT* changed!
357 * @param pvFault The fault address (CR2).
358 * @param pcbSize Size of the write (if applicable).
359 * @param enmCodeType Code type (user/supervisor)
360 *
361 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
362 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
363 * to worry about e.g. invalid modrm combinations (!)
364 *
365 * @todo At this time we do NOT check if the instruction overwrites vital information.
366 * Make sure this can't happen!! (will add some assertions/checks later)
367 */
368VMMDECL(VBOXSTRICTRC) EMInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
369 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize)
370{
371 STAM_PROFILE_START(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Emulate), a);
372 VBOXSTRICTRC rc = emInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, enmCodeType, pcbSize);
373 STAM_PROFILE_STOP(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Emulate), a);
374 if (RT_SUCCESS(rc))
375 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,InterpretSucceeded));
376 else
377 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,InterpretFailed));
378 return rc;
379}
380
381
382/**
383 * Interpret a port I/O instruction.
384 *
385 * @returns VBox status code suitable for scheduling.
386 * @param pVM The VM handle.
387 * @param pVCpu The VMCPU handle.
388 * @param pCtxCore The context core. This will be updated on successful return.
389 * @param pDis The instruction to interpret.
390 * @param cbOp The size of the instruction.
391 * @remark This may raise exceptions.
392 */
393VMMDECL(VBOXSTRICTRC) EMInterpretPortIO(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, PDISCPUSTATE pDis, uint32_t cbOp)
394{
395 /*
396 * Hand it on to IOM.
397 */
398#ifdef IN_RC
399 VBOXSTRICTRC rcStrict = IOMGCIOPortHandler(pVM, pCtxCore, pDis);
400 if (IOM_SUCCESS(rcStrict))
401 pCtxCore->rip += cbOp;
402 return rcStrict;
403#else
404 AssertReleaseMsgFailed(("not implemented\n"));
405 return VERR_NOT_IMPLEMENTED;
406#endif
407}
408
409
410DECLINLINE(int) emRamRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCPTR GCPtrSrc, uint32_t cb)
411{
412#ifdef IN_RC
413 int rc = MMGCRamRead(pVM, pvDst, (void *)(uintptr_t)GCPtrSrc, cb);
414 if (RT_LIKELY(rc != VERR_ACCESS_DENIED))
415 return rc;
416 /*
417 * The page pool cache may end up here in some cases because it
418 * flushed one of the shadow mappings used by the trapping
419 * instruction and it either flushed the TLB or the CPU reused it.
420 */
421#endif
422 return PGMPhysInterpretedReadNoHandlers(pVCpu, pCtxCore, pvDst, GCPtrSrc, cb, /*fMayTrap*/ false);
423}
424
425
426DECLINLINE(int) emRamWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, uint32_t cb)
427{
428 /* Don't use MMGCRamWrite here as it does not respect zero pages, shared
429 pages or write monitored pages. */
430 return PGMPhysInterpretedWriteNoHandlers(pVCpu, pCtxCore, GCPtrDst, pvSrc, cb, /*fMayTrap*/ false);
431}
432
433
434/** Convert sel:addr to a flat GC address. */
435DECLINLINE(RTGCPTR) emConvertToFlatAddr(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, POP_PARAMETER pParam, RTGCPTR pvAddr)
436{
437 DIS_SELREG enmPrefixSeg = DISDetectSegReg(pDis, pParam);
438 return SELMToFlat(pVM, enmPrefixSeg, pRegFrame, pvAddr);
439}
440
441
442#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
443/**
444 * Get the mnemonic for the disassembled instruction.
445 *
446 * GC/R0 doesn't include the strings in the DIS tables because
447 * of limited space.
448 */
449static const char *emGetMnemonic(PDISCPUSTATE pDis)
450{
451 switch (pDis->pCurInstr->opcode)
452 {
453 case OP_XCHG: return "Xchg";
454 case OP_DEC: return "Dec";
455 case OP_INC: return "Inc";
456 case OP_POP: return "Pop";
457 case OP_OR: return "Or";
458 case OP_AND: return "And";
459 case OP_MOV: return "Mov";
460 case OP_INVLPG: return "InvlPg";
461 case OP_CPUID: return "CpuId";
462 case OP_MOV_CR: return "MovCRx";
463 case OP_MOV_DR: return "MovDRx";
464 case OP_LLDT: return "LLdt";
465 case OP_LGDT: return "LGdt";
466 case OP_LIDT: return "LIdt";
467 case OP_CLTS: return "Clts";
468 case OP_MONITOR: return "Monitor";
469 case OP_MWAIT: return "MWait";
470 case OP_RDMSR: return "Rdmsr";
471 case OP_WRMSR: return "Wrmsr";
472 case OP_ADD: return "Add";
473 case OP_ADC: return "Adc";
474 case OP_SUB: return "Sub";
475 case OP_SBB: return "Sbb";
476 case OP_RDTSC: return "Rdtsc";
477 case OP_STI: return "Sti";
478 case OP_CLI: return "Cli";
479 case OP_XADD: return "XAdd";
480 case OP_HLT: return "Hlt";
481 case OP_IRET: return "Iret";
482 case OP_MOVNTPS: return "MovNTPS";
483 case OP_STOSWD: return "StosWD";
484 case OP_WBINVD: return "WbInvd";
485 case OP_XOR: return "Xor";
486 case OP_BTR: return "Btr";
487 case OP_BTS: return "Bts";
488 case OP_BTC: return "Btc";
489 case OP_LMSW: return "Lmsw";
490 case OP_SMSW: return "Smsw";
491 case OP_CMPXCHG: return pDis->prefix & PREFIX_LOCK ? "Lock CmpXchg" : "CmpXchg";
492 case OP_CMPXCHG8B: return pDis->prefix & PREFIX_LOCK ? "Lock CmpXchg8b" : "CmpXchg8b";
493
494 default:
495 Log(("Unknown opcode %d\n", pDis->pCurInstr->opcode));
496 return "???";
497 }
498}
499#endif /* VBOX_STRICT || LOG_ENABLED */
500
501
502/**
503 * XCHG instruction emulation.
504 */
505static int emInterpretXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
506{
507 OP_PARAMVAL param1, param2;
508
509 /* Source to make DISQueryParamVal read the register value - ugly hack */
510 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
511 if(RT_FAILURE(rc))
512 return VERR_EM_INTERPRETER;
513
514 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
515 if(RT_FAILURE(rc))
516 return VERR_EM_INTERPRETER;
517
518#ifdef IN_RC
519 if (TRPMHasTrap(pVCpu))
520 {
521 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
522 {
523#endif
524 RTGCPTR pParam1 = 0, pParam2 = 0;
525 uint64_t valpar1, valpar2;
526
527 AssertReturn(pDis->param1.size == pDis->param2.size, VERR_EM_INTERPRETER);
528 switch(param1.type)
529 {
530 case PARMTYPE_IMMEDIATE: /* register type is translated to this one too */
531 valpar1 = param1.val.val64;
532 break;
533
534 case PARMTYPE_ADDRESS:
535 pParam1 = (RTGCPTR)param1.val.val64;
536 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
537 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
538 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
539 if (RT_FAILURE(rc))
540 {
541 AssertMsgFailed(("MMGCRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
542 return VERR_EM_INTERPRETER;
543 }
544 break;
545
546 default:
547 AssertFailed();
548 return VERR_EM_INTERPRETER;
549 }
550
551 switch(param2.type)
552 {
553 case PARMTYPE_ADDRESS:
554 pParam2 = (RTGCPTR)param2.val.val64;
555 pParam2 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param2, pParam2);
556 EM_ASSERT_FAULT_RETURN(pParam2 == pvFault, VERR_EM_INTERPRETER);
557 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar2, pParam2, param2.size);
558 if (RT_FAILURE(rc))
559 {
560 AssertMsgFailed(("MMGCRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
561 }
562 break;
563
564 case PARMTYPE_IMMEDIATE:
565 valpar2 = param2.val.val64;
566 break;
567
568 default:
569 AssertFailed();
570 return VERR_EM_INTERPRETER;
571 }
572
573 /* Write value of parameter 2 to parameter 1 (reg or memory address) */
574 if (pParam1 == 0)
575 {
576 Assert(param1.type == PARMTYPE_IMMEDIATE); /* register actually */
577 switch(param1.size)
578 {
579 case 1: //special case for AH etc
580 rc = DISWriteReg8(pRegFrame, pDis->param1.base.reg_gen, (uint8_t )valpar2); break;
581 case 2: rc = DISWriteReg16(pRegFrame, pDis->param1.base.reg_gen, (uint16_t)valpar2); break;
582 case 4: rc = DISWriteReg32(pRegFrame, pDis->param1.base.reg_gen, (uint32_t)valpar2); break;
583 case 8: rc = DISWriteReg64(pRegFrame, pDis->param1.base.reg_gen, valpar2); break;
584 default: AssertFailedReturn(VERR_EM_INTERPRETER);
585 }
586 if (RT_FAILURE(rc))
587 return VERR_EM_INTERPRETER;
588 }
589 else
590 {
591 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar2, param1.size);
592 if (RT_FAILURE(rc))
593 {
594 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
595 return VERR_EM_INTERPRETER;
596 }
597 }
598
599 /* Write value of parameter 1 to parameter 2 (reg or memory address) */
600 if (pParam2 == 0)
601 {
602 Assert(param2.type == PARMTYPE_IMMEDIATE); /* register actually */
603 switch(param2.size)
604 {
605 case 1: //special case for AH etc
606 rc = DISWriteReg8(pRegFrame, pDis->param2.base.reg_gen, (uint8_t )valpar1); break;
607 case 2: rc = DISWriteReg16(pRegFrame, pDis->param2.base.reg_gen, (uint16_t)valpar1); break;
608 case 4: rc = DISWriteReg32(pRegFrame, pDis->param2.base.reg_gen, (uint32_t)valpar1); break;
609 case 8: rc = DISWriteReg64(pRegFrame, pDis->param2.base.reg_gen, valpar1); break;
610 default: AssertFailedReturn(VERR_EM_INTERPRETER);
611 }
612 if (RT_FAILURE(rc))
613 return VERR_EM_INTERPRETER;
614 }
615 else
616 {
617 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam2, &valpar1, param2.size);
618 if (RT_FAILURE(rc))
619 {
620 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
621 return VERR_EM_INTERPRETER;
622 }
623 }
624
625 *pcbSize = param2.size;
626 return VINF_SUCCESS;
627#ifdef IN_RC
628 }
629 }
630#endif
631 return VERR_EM_INTERPRETER;
632}
633
634
635/**
636 * INC and DEC emulation.
637 */
638static int emInterpretIncDec(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
639 PFNEMULATEPARAM2 pfnEmulate)
640{
641 OP_PARAMVAL param1;
642
643 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
644 if(RT_FAILURE(rc))
645 return VERR_EM_INTERPRETER;
646
647#ifdef IN_RC
648 if (TRPMHasTrap(pVCpu))
649 {
650 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
651 {
652#endif
653 RTGCPTR pParam1 = 0;
654 uint64_t valpar1;
655
656 if (param1.type == PARMTYPE_ADDRESS)
657 {
658 pParam1 = (RTGCPTR)param1.val.val64;
659 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
660#ifdef IN_RC
661 /* Safety check (in theory it could cross a page boundary and fault there though) */
662 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
663#endif
664 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
665 if (RT_FAILURE(rc))
666 {
667 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
668 return VERR_EM_INTERPRETER;
669 }
670 }
671 else
672 {
673 AssertFailed();
674 return VERR_EM_INTERPRETER;
675 }
676
677 uint32_t eflags;
678
679 eflags = pfnEmulate(&valpar1, param1.size);
680
681 /* Write result back */
682 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
683 if (RT_FAILURE(rc))
684 {
685 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
686 return VERR_EM_INTERPRETER;
687 }
688
689 /* Update guest's eflags and finish. */
690 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
691 | (eflags & (X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
692
693 /* All done! */
694 *pcbSize = param1.size;
695 return VINF_SUCCESS;
696#ifdef IN_RC
697 }
698 }
699#endif
700 return VERR_EM_INTERPRETER;
701}
702
703
704/**
705 * POP Emulation.
706 */
707static int emInterpretPop(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
708{
709 Assert(pDis->mode != CPUMODE_64BIT); /** @todo check */
710 OP_PARAMVAL param1;
711 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
712 if(RT_FAILURE(rc))
713 return VERR_EM_INTERPRETER;
714
715#ifdef IN_RC
716 if (TRPMHasTrap(pVCpu))
717 {
718 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
719 {
720#endif
721 RTGCPTR pParam1 = 0;
722 uint32_t valpar1;
723 RTGCPTR pStackVal;
724
725 /* Read stack value first */
726 if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->ss, &pRegFrame->ssHid) == CPUMODE_16BIT)
727 return VERR_EM_INTERPRETER; /* No legacy 16 bits stuff here, please. */
728
729 /* Convert address; don't bother checking limits etc, as we only read here */
730 pStackVal = SELMToFlat(pVM, DIS_SELREG_SS, pRegFrame, (RTGCPTR)pRegFrame->esp);
731 if (pStackVal == 0)
732 return VERR_EM_INTERPRETER;
733
734 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pStackVal, param1.size);
735 if (RT_FAILURE(rc))
736 {
737 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
738 return VERR_EM_INTERPRETER;
739 }
740
741 if (param1.type == PARMTYPE_ADDRESS)
742 {
743 pParam1 = (RTGCPTR)param1.val.val64;
744
745 /* pop [esp+xx] uses esp after the actual pop! */
746 AssertCompile(USE_REG_ESP == USE_REG_SP);
747 if ( (pDis->param1.flags & USE_BASE)
748 && (pDis->param1.flags & (USE_REG_GEN16|USE_REG_GEN32))
749 && pDis->param1.base.reg_gen == USE_REG_ESP
750 )
751 pParam1 = (RTGCPTR)((RTGCUINTPTR)pParam1 + param1.size);
752
753 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
754 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault || (RTGCPTR)pRegFrame->esp == pvFault, VERR_EM_INTERPRETER);
755 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
756 if (RT_FAILURE(rc))
757 {
758 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
759 return VERR_EM_INTERPRETER;
760 }
761
762 /* Update ESP as the last step */
763 pRegFrame->esp += param1.size;
764 }
765 else
766 {
767#ifndef DEBUG_bird // annoying assertion.
768 AssertFailed();
769#endif
770 return VERR_EM_INTERPRETER;
771 }
772
773 /* All done! */
774 *pcbSize = param1.size;
775 return VINF_SUCCESS;
776#ifdef IN_RC
777 }
778 }
779#endif
780 return VERR_EM_INTERPRETER;
781}
782
783
784/**
785 * XOR/OR/AND Emulation.
786 */
787static int emInterpretOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
788 PFNEMULATEPARAM3 pfnEmulate)
789{
790 OP_PARAMVAL param1, param2;
791
792 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
793 if(RT_FAILURE(rc))
794 return VERR_EM_INTERPRETER;
795
796 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
797 if(RT_FAILURE(rc))
798 return VERR_EM_INTERPRETER;
799
800#ifdef IN_RC
801 if (TRPMHasTrap(pVCpu))
802 {
803 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
804 {
805#endif
806 RTGCPTR pParam1;
807 uint64_t valpar1, valpar2;
808
809 if (pDis->param1.size != pDis->param2.size)
810 {
811 if (pDis->param1.size < pDis->param2.size)
812 {
813 AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->param2.size)); /* should never happen! */
814 return VERR_EM_INTERPRETER;
815 }
816 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
817 pDis->param2.size = pDis->param1.size;
818 param2.size = param1.size;
819 }
820
821 /* The destination is always a virtual address */
822 if (param1.type == PARMTYPE_ADDRESS)
823 {
824 pParam1 = (RTGCPTR)param1.val.val64;
825 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
826 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
827 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
828 if (RT_FAILURE(rc))
829 {
830 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
831 return VERR_EM_INTERPRETER;
832 }
833 }
834 else
835 {
836 AssertFailed();
837 return VERR_EM_INTERPRETER;
838 }
839
840 /* Register or immediate data */
841 switch(param2.type)
842 {
843 case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
844 valpar2 = param2.val.val64;
845 break;
846
847 default:
848 AssertFailed();
849 return VERR_EM_INTERPRETER;
850 }
851
852 LogFlow(("emInterpretOrXorAnd %s %RGv %RX64 - %RX64 size %d (%d)\n", emGetMnemonic(pDis), pParam1, valpar1, valpar2, param2.size, param1.size));
853
854 /* Data read, emulate instruction. */
855 uint32_t eflags = pfnEmulate(&valpar1, valpar2, param2.size);
856
857 LogFlow(("emInterpretOrXorAnd %s result %RX64\n", emGetMnemonic(pDis), valpar1));
858
859 /* Update guest's eflags and finish. */
860 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
861 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
862
863 /* And write it back */
864 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
865 if (RT_SUCCESS(rc))
866 {
867 /* All done! */
868 *pcbSize = param2.size;
869 return VINF_SUCCESS;
870 }
871#ifdef IN_RC
872 }
873 }
874#endif
875 return VERR_EM_INTERPRETER;
876}
877
878
879/**
880 * LOCK XOR/OR/AND Emulation.
881 */
882static int emInterpretLockOrXorAnd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
883 uint32_t *pcbSize, PFNEMULATELOCKPARAM3 pfnEmulate)
884{
885 void *pvParam1;
886 OP_PARAMVAL param1, param2;
887
888#if HC_ARCH_BITS == 32 && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0)
889 Assert(pDis->param1.size <= 4);
890#endif
891
892 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
893 if(RT_FAILURE(rc))
894 return VERR_EM_INTERPRETER;
895
896 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
897 if(RT_FAILURE(rc))
898 return VERR_EM_INTERPRETER;
899
900 if (pDis->param1.size != pDis->param2.size)
901 {
902 AssertMsgReturn(pDis->param1.size >= pDis->param2.size, /* should never happen! */
903 ("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->param2.size),
904 VERR_EM_INTERPRETER);
905
906 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
907 pDis->param2.size = pDis->param1.size;
908 param2.size = param1.size;
909 }
910
911#ifdef IN_RC
912 /* Safety check (in theory it could cross a page boundary and fault there though) */
913 Assert( TRPMHasTrap(pVCpu)
914 && (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW));
915 EM_ASSERT_FAULT_RETURN(GCPtrPar1 == pvFault, VERR_EM_INTERPRETER);
916#endif
917
918 /* Register and immediate data == PARMTYPE_IMMEDIATE */
919 AssertReturn(param2.type == PARMTYPE_IMMEDIATE, VERR_EM_INTERPRETER);
920 RTGCUINTREG ValPar2 = param2.val.val64;
921
922 /* The destination is always a virtual address */
923 AssertReturn(param1.type == PARMTYPE_ADDRESS, VERR_EM_INTERPRETER);
924
925 RTGCPTR GCPtrPar1 = param1.val.val64;
926 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, GCPtrPar1);
927 PGMPAGEMAPLOCK Lock;
928 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
929 AssertRCReturn(rc, VERR_EM_INTERPRETER);
930
931 /* Try emulate it with a one-shot #PF handler in place. (RC) */
932 Log2(("%s %RGv imm%d=%RX64\n", emGetMnemonic(pDis), GCPtrPar1, pDis->param2.size*8, ValPar2));
933
934 RTGCUINTREG32 eflags = 0;
935 rc = pfnEmulate(pvParam1, ValPar2, pDis->param2.size, &eflags);
936 PGMPhysReleasePageMappingLock(pVM, &Lock);
937 if (RT_FAILURE(rc))
938 {
939 Log(("%s %RGv imm%d=%RX64-> emulation failed due to page fault!\n", emGetMnemonic(pDis), GCPtrPar1, pDis->param2.size*8, ValPar2));
940 return VERR_EM_INTERPRETER;
941 }
942
943 /* Update guest's eflags and finish. */
944 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
945 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
946
947 *pcbSize = param2.size;
948 return VINF_SUCCESS;
949}
950
951
952/**
953 * ADD, ADC & SUB Emulation.
954 */
955static int emInterpretAddSub(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
956 PFNEMULATEPARAM3 pfnEmulate)
957{
958 OP_PARAMVAL param1, param2;
959 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
960 if(RT_FAILURE(rc))
961 return VERR_EM_INTERPRETER;
962
963 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
964 if(RT_FAILURE(rc))
965 return VERR_EM_INTERPRETER;
966
967#ifdef IN_RC
968 if (TRPMHasTrap(pVCpu))
969 {
970 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
971 {
972#endif
973 RTGCPTR pParam1;
974 uint64_t valpar1, valpar2;
975
976 if (pDis->param1.size != pDis->param2.size)
977 {
978 if (pDis->param1.size < pDis->param2.size)
979 {
980 AssertMsgFailed(("%s at %RGv parameter mismatch %d vs %d!!\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip, pDis->param1.size, pDis->param2.size)); /* should never happen! */
981 return VERR_EM_INTERPRETER;
982 }
983 /* Or %Ev, Ib -> just a hack to save some space; the data width of the 1st parameter determines the real width */
984 pDis->param2.size = pDis->param1.size;
985 param2.size = param1.size;
986 }
987
988 /* The destination is always a virtual address */
989 if (param1.type == PARMTYPE_ADDRESS)
990 {
991 pParam1 = (RTGCPTR)param1.val.val64;
992 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
993 EM_ASSERT_FAULT_RETURN(pParam1 == pvFault, VERR_EM_INTERPRETER);
994 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, param1.size);
995 if (RT_FAILURE(rc))
996 {
997 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
998 return VERR_EM_INTERPRETER;
999 }
1000 }
1001 else
1002 {
1003#ifndef DEBUG_bird
1004 AssertFailed();
1005#endif
1006 return VERR_EM_INTERPRETER;
1007 }
1008
1009 /* Register or immediate data */
1010 switch(param2.type)
1011 {
1012 case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
1013 valpar2 = param2.val.val64;
1014 break;
1015
1016 default:
1017 AssertFailed();
1018 return VERR_EM_INTERPRETER;
1019 }
1020
1021 /* Data read, emulate instruction. */
1022 uint32_t eflags = pfnEmulate(&valpar1, valpar2, param2.size);
1023
1024 /* Update guest's eflags and finish. */
1025 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
1026 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
1027
1028 /* And write it back */
1029 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, param1.size);
1030 if (RT_SUCCESS(rc))
1031 {
1032 /* All done! */
1033 *pcbSize = param2.size;
1034 return VINF_SUCCESS;
1035 }
1036#ifdef IN_RC
1037 }
1038 }
1039#endif
1040 return VERR_EM_INTERPRETER;
1041}
1042
1043
1044/**
1045 * ADC Emulation.
1046 */
1047static int emInterpretAdc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1048{
1049 if (pRegFrame->eflags.Bits.u1CF)
1050 return emInterpretAddSub(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, EMEmulateAdcWithCarrySet);
1051 else
1052 return emInterpretAddSub(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, EMEmulateAdd);
1053}
1054
1055
1056/**
1057 * BTR/C/S Emulation.
1058 */
1059static int emInterpretBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize,
1060 PFNEMULATEPARAM2UINT32 pfnEmulate)
1061{
1062 OP_PARAMVAL param1, param2;
1063 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
1064 if(RT_FAILURE(rc))
1065 return VERR_EM_INTERPRETER;
1066
1067 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
1068 if(RT_FAILURE(rc))
1069 return VERR_EM_INTERPRETER;
1070
1071#ifdef IN_RC
1072 if (TRPMHasTrap(pVCpu))
1073 {
1074 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
1075 {
1076#endif
1077 RTGCPTR pParam1;
1078 uint64_t valpar1 = 0, valpar2;
1079 uint32_t eflags;
1080
1081 /* The destination is always a virtual address */
1082 if (param1.type != PARMTYPE_ADDRESS)
1083 return VERR_EM_INTERPRETER;
1084
1085 pParam1 = (RTGCPTR)param1.val.val64;
1086 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
1087
1088 /* Register or immediate data */
1089 switch(param2.type)
1090 {
1091 case PARMTYPE_IMMEDIATE: /* both immediate data and register (ugly) */
1092 valpar2 = param2.val.val64;
1093 break;
1094
1095 default:
1096 AssertFailed();
1097 return VERR_EM_INTERPRETER;
1098 }
1099
1100 Log2(("emInterpret%s: pvFault=%RGv pParam1=%RGv val2=%x\n", emGetMnemonic(pDis), pvFault, pParam1, valpar2));
1101 pParam1 = (RTGCPTR)((RTGCUINTPTR)pParam1 + valpar2/8);
1102 EM_ASSERT_FAULT_RETURN((RTGCPTR)((RTGCUINTPTR)pParam1 & ~3) == pvFault, VERR_EM_INTERPRETER);
1103 rc = emRamRead(pVM, pVCpu, pRegFrame, &valpar1, pParam1, 1);
1104 if (RT_FAILURE(rc))
1105 {
1106 AssertMsgFailed(("emRamRead %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
1107 return VERR_EM_INTERPRETER;
1108 }
1109
1110 Log2(("emInterpretBtx: val=%x\n", valpar1));
1111 /* Data read, emulate bit test instruction. */
1112 eflags = pfnEmulate(&valpar1, valpar2 & 0x7);
1113
1114 Log2(("emInterpretBtx: val=%x CF=%d\n", valpar1, !!(eflags & X86_EFL_CF)));
1115
1116 /* Update guest's eflags and finish. */
1117 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
1118 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
1119
1120 /* And write it back */
1121 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &valpar1, 1);
1122 if (RT_SUCCESS(rc))
1123 {
1124 /* All done! */
1125 *pcbSize = 1;
1126 return VINF_SUCCESS;
1127 }
1128#ifdef IN_RC
1129 }
1130 }
1131#endif
1132 return VERR_EM_INTERPRETER;
1133}
1134
1135
1136/**
1137 * LOCK BTR/C/S Emulation.
1138 */
1139static int emInterpretLockBitTest(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
1140 uint32_t *pcbSize, PFNEMULATELOCKPARAM2 pfnEmulate)
1141{
1142 void *pvParam1;
1143
1144 OP_PARAMVAL param1, param2;
1145 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
1146 if(RT_FAILURE(rc))
1147 return VERR_EM_INTERPRETER;
1148
1149 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
1150 if(RT_FAILURE(rc))
1151 return VERR_EM_INTERPRETER;
1152
1153 /* The destination is always a virtual address */
1154 if (param1.type != PARMTYPE_ADDRESS)
1155 return VERR_EM_INTERPRETER;
1156
1157 /* Register and immediate data == PARMTYPE_IMMEDIATE */
1158 AssertReturn(param2.type == PARMTYPE_IMMEDIATE, VERR_EM_INTERPRETER);
1159 uint64_t ValPar2 = param2.val.val64;
1160
1161 /* Adjust the parameters so what we're dealing with is a bit within the byte pointed to. */
1162 RTGCPTR GCPtrPar1 = param1.val.val64;
1163 GCPtrPar1 = (GCPtrPar1 + ValPar2 / 8);
1164 ValPar2 &= 7;
1165
1166 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, GCPtrPar1);
1167#ifdef IN_RC
1168 Assert(TRPMHasTrap(pVCpu));
1169 EM_ASSERT_FAULT_RETURN((RTGCPTR)((RTGCUINTPTR)GCPtrPar1 & ~(RTGCUINTPTR)3) == pvFault, VERR_EM_INTERPRETER);
1170#endif
1171
1172 PGMPAGEMAPLOCK Lock;
1173 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
1174 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1175
1176 Log2(("emInterpretLockBitTest %s: pvFault=%RGv GCPtrPar1=%RGv imm=%RX64\n", emGetMnemonic(pDis), pvFault, GCPtrPar1, ValPar2));
1177
1178 /* Try emulate it with a one-shot #PF handler in place. (RC) */
1179 RTGCUINTREG32 eflags = 0;
1180 rc = pfnEmulate(pvParam1, ValPar2, &eflags);
1181 PGMPhysReleasePageMappingLock(pVM, &Lock);
1182 if (RT_FAILURE(rc))
1183 {
1184 Log(("emInterpretLockBitTest %s: %RGv imm%d=%RX64 -> emulation failed due to page fault!\n",
1185 emGetMnemonic(pDis), GCPtrPar1, pDis->param2.size*8, ValPar2));
1186 return VERR_EM_INTERPRETER;
1187 }
1188
1189 Log2(("emInterpretLockBitTest %s: GCPtrPar1=%RGv imm=%RX64 CF=%d\n", emGetMnemonic(pDis), GCPtrPar1, ValPar2, !!(eflags & X86_EFL_CF)));
1190
1191 /* Update guest's eflags and finish. */
1192 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
1193 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
1194
1195 *pcbSize = 1;
1196 return VINF_SUCCESS;
1197}
1198
1199
1200/**
1201 * MOV emulation.
1202 */
1203static int emInterpretMov(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1204{
1205 OP_PARAMVAL param1, param2;
1206 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_DEST);
1207 if(RT_FAILURE(rc))
1208 return VERR_EM_INTERPRETER;
1209
1210 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
1211 if(RT_FAILURE(rc))
1212 return VERR_EM_INTERPRETER;
1213
1214#ifdef IN_RC
1215 if (TRPMHasTrap(pVCpu))
1216 {
1217 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
1218 {
1219#else
1220 /** @todo Make this the default and don't rely on TRPM information. */
1221 if (param1.type == PARMTYPE_ADDRESS)
1222 {
1223#endif
1224 RTGCPTR pDest;
1225 uint64_t val64;
1226
1227 switch(param1.type)
1228 {
1229 case PARMTYPE_IMMEDIATE:
1230 if(!(param1.flags & (PARAM_VAL32|PARAM_VAL64)))
1231 return VERR_EM_INTERPRETER;
1232 /* fallthru */
1233
1234 case PARMTYPE_ADDRESS:
1235 pDest = (RTGCPTR)param1.val.val64;
1236 pDest = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pDest);
1237 break;
1238
1239 default:
1240 AssertFailed();
1241 return VERR_EM_INTERPRETER;
1242 }
1243
1244 switch(param2.type)
1245 {
1246 case PARMTYPE_IMMEDIATE: /* register type is translated to this one too */
1247 val64 = param2.val.val64;
1248 break;
1249
1250 default:
1251 Log(("emInterpretMov: unexpected type=%d rip=%RGv\n", param2.type, (RTGCPTR)pRegFrame->rip));
1252 return VERR_EM_INTERPRETER;
1253 }
1254#ifdef LOG_ENABLED
1255 if (pDis->mode == CPUMODE_64BIT)
1256 LogFlow(("EMInterpretInstruction at %RGv: OP_MOV %RGv <- %RX64 (%d) &val64=%RHv\n", (RTGCPTR)pRegFrame->rip, pDest, val64, param2.size, &val64));
1257 else
1258 LogFlow(("EMInterpretInstruction at %08RX64: OP_MOV %RGv <- %08X (%d) &val64=%RHv\n", pRegFrame->rip, pDest, (uint32_t)val64, param2.size, &val64));
1259#endif
1260
1261 Assert(param2.size <= 8 && param2.size > 0);
1262 EM_ASSERT_FAULT_RETURN(pDest == pvFault, VERR_EM_INTERPRETER);
1263 rc = emRamWrite(pVM, pVCpu, pRegFrame, pDest, &val64, param2.size);
1264 if (RT_FAILURE(rc))
1265 return VERR_EM_INTERPRETER;
1266
1267 *pcbSize = param2.size;
1268 }
1269 else
1270 { /* read fault */
1271 RTGCPTR pSrc;
1272 uint64_t val64;
1273
1274 /* Source */
1275 switch(param2.type)
1276 {
1277 case PARMTYPE_IMMEDIATE:
1278 if(!(param2.flags & (PARAM_VAL32|PARAM_VAL64)))
1279 return VERR_EM_INTERPRETER;
1280 /* fallthru */
1281
1282 case PARMTYPE_ADDRESS:
1283 pSrc = (RTGCPTR)param2.val.val64;
1284 pSrc = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param2, pSrc);
1285 break;
1286
1287 default:
1288 return VERR_EM_INTERPRETER;
1289 }
1290
1291 Assert(param1.size <= 8 && param1.size > 0);
1292 EM_ASSERT_FAULT_RETURN(pSrc == pvFault, VERR_EM_INTERPRETER);
1293 rc = emRamRead(pVM, pVCpu, pRegFrame, &val64, pSrc, param1.size);
1294 if (RT_FAILURE(rc))
1295 return VERR_EM_INTERPRETER;
1296
1297 /* Destination */
1298 switch(param1.type)
1299 {
1300 case PARMTYPE_REGISTER:
1301 switch(param1.size)
1302 {
1303 case 1: rc = DISWriteReg8(pRegFrame, pDis->param1.base.reg_gen, (uint8_t) val64); break;
1304 case 2: rc = DISWriteReg16(pRegFrame, pDis->param1.base.reg_gen, (uint16_t)val64); break;
1305 case 4: rc = DISWriteReg32(pRegFrame, pDis->param1.base.reg_gen, (uint32_t)val64); break;
1306 case 8: rc = DISWriteReg64(pRegFrame, pDis->param1.base.reg_gen, val64); break;
1307 default:
1308 return VERR_EM_INTERPRETER;
1309 }
1310 if (RT_FAILURE(rc))
1311 return rc;
1312 break;
1313
1314 default:
1315 return VERR_EM_INTERPRETER;
1316 }
1317#ifdef LOG_ENABLED
1318 if (pDis->mode == CPUMODE_64BIT)
1319 LogFlow(("EMInterpretInstruction: OP_MOV %RGv -> %RX64 (%d)\n", pSrc, val64, param1.size));
1320 else
1321 LogFlow(("EMInterpretInstruction: OP_MOV %RGv -> %08X (%d)\n", pSrc, (uint32_t)val64, param1.size));
1322#endif
1323 }
1324 return VINF_SUCCESS;
1325#ifdef IN_RC
1326 }
1327#endif
1328 return VERR_EM_INTERPRETER;
1329}
1330
1331
1332#ifndef IN_RC
1333/**
1334 * [REP] STOSWD emulation
1335 */
1336static int emInterpretStosWD(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1337{
1338 int rc;
1339 RTGCPTR GCDest, GCOffset;
1340 uint32_t cbSize;
1341 uint64_t cTransfers;
1342 int offIncrement;
1343
1344 /* Don't support any but these three prefix bytes. */
1345 if ((pDis->prefix & ~(PREFIX_ADDRSIZE|PREFIX_OPSIZE|PREFIX_REP|PREFIX_REX)))
1346 return VERR_EM_INTERPRETER;
1347
1348 switch (pDis->addrmode)
1349 {
1350 case CPUMODE_16BIT:
1351 GCOffset = pRegFrame->di;
1352 cTransfers = pRegFrame->cx;
1353 break;
1354 case CPUMODE_32BIT:
1355 GCOffset = pRegFrame->edi;
1356 cTransfers = pRegFrame->ecx;
1357 break;
1358 case CPUMODE_64BIT:
1359 GCOffset = pRegFrame->rdi;
1360 cTransfers = pRegFrame->rcx;
1361 break;
1362 default:
1363 AssertFailed();
1364 return VERR_EM_INTERPRETER;
1365 }
1366
1367 GCDest = SELMToFlat(pVM, DIS_SELREG_ES, pRegFrame, GCOffset);
1368 switch (pDis->opmode)
1369 {
1370 case CPUMODE_16BIT:
1371 cbSize = 2;
1372 break;
1373 case CPUMODE_32BIT:
1374 cbSize = 4;
1375 break;
1376 case CPUMODE_64BIT:
1377 cbSize = 8;
1378 break;
1379 default:
1380 AssertFailed();
1381 return VERR_EM_INTERPRETER;
1382 }
1383
1384 offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cbSize : (signed)cbSize;
1385
1386 if (!(pDis->prefix & PREFIX_REP))
1387 {
1388 LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d\n", pRegFrame->es, GCOffset, GCDest, cbSize));
1389
1390 rc = emRamWrite(pVM, pVCpu, pRegFrame, GCDest, &pRegFrame->rax, cbSize);
1391 if (RT_FAILURE(rc))
1392 return VERR_EM_INTERPRETER;
1393 Assert(rc == VINF_SUCCESS);
1394
1395 /* Update (e/r)di. */
1396 switch (pDis->addrmode)
1397 {
1398 case CPUMODE_16BIT:
1399 pRegFrame->di += offIncrement;
1400 break;
1401 case CPUMODE_32BIT:
1402 pRegFrame->edi += offIncrement;
1403 break;
1404 case CPUMODE_64BIT:
1405 pRegFrame->rdi += offIncrement;
1406 break;
1407 default:
1408 AssertFailed();
1409 return VERR_EM_INTERPRETER;
1410 }
1411
1412 }
1413 else
1414 {
1415 if (!cTransfers)
1416 return VINF_SUCCESS;
1417
1418 /*
1419 * Do *not* try emulate cross page stuff here because we don't know what might
1420 * be waiting for us on the subsequent pages. The caller has only asked us to
1421 * ignore access handlers fro the current page.
1422 * This also fends off big stores which would quickly kill PGMR0DynMap.
1423 */
1424 if ( cbSize > PAGE_SIZE
1425 || cTransfers > PAGE_SIZE
1426 || (GCDest >> PAGE_SHIFT) != ((GCDest + offIncrement * cTransfers) >> PAGE_SHIFT))
1427 {
1428 Log(("STOSWD is crosses pages, chicken out to the recompiler; GCDest=%RGv cbSize=%#x offIncrement=%d cTransfers=%#x\n",
1429 GCDest, cbSize, offIncrement, cTransfers));
1430 return VERR_EM_INTERPRETER;
1431 }
1432
1433 LogFlow(("emInterpretStosWD dest=%04X:%RGv (%RGv) cbSize=%d cTransfers=%x DF=%d\n", pRegFrame->es, GCOffset, GCDest, cbSize, cTransfers, pRegFrame->eflags.Bits.u1DF));
1434 /* Access verification first; we currently can't recover properly from traps inside this instruction */
1435 rc = PGMVerifyAccess(pVCpu, GCDest - ((offIncrement > 0) ? 0 : ((cTransfers-1) * cbSize)),
1436 cTransfers * cbSize,
1437 X86_PTE_RW | (CPUMGetGuestCPL(pVCpu, pRegFrame) == 3 ? X86_PTE_US : 0));
1438 if (rc != VINF_SUCCESS)
1439 {
1440 Log(("STOSWD will generate a trap -> recompiler, rc=%d\n", rc));
1441 return VERR_EM_INTERPRETER;
1442 }
1443
1444 /* REP case */
1445 while (cTransfers)
1446 {
1447 rc = emRamWrite(pVM, pVCpu, pRegFrame, GCDest, &pRegFrame->rax, cbSize);
1448 if (RT_FAILURE(rc))
1449 {
1450 rc = VERR_EM_INTERPRETER;
1451 break;
1452 }
1453
1454 Assert(rc == VINF_SUCCESS);
1455 GCOffset += offIncrement;
1456 GCDest += offIncrement;
1457 cTransfers--;
1458 }
1459
1460 /* Update the registers. */
1461 switch (pDis->addrmode)
1462 {
1463 case CPUMODE_16BIT:
1464 pRegFrame->di = GCOffset;
1465 pRegFrame->cx = cTransfers;
1466 break;
1467 case CPUMODE_32BIT:
1468 pRegFrame->edi = GCOffset;
1469 pRegFrame->ecx = cTransfers;
1470 break;
1471 case CPUMODE_64BIT:
1472 pRegFrame->rdi = GCOffset;
1473 pRegFrame->rcx = cTransfers;
1474 break;
1475 default:
1476 AssertFailed();
1477 return VERR_EM_INTERPRETER;
1478 }
1479 }
1480
1481 *pcbSize = cbSize;
1482 return rc;
1483}
1484#endif /* !IN_RC */
1485
1486
1487/**
1488 * [LOCK] CMPXCHG emulation.
1489 */
1490static int emInterpretCmpXchg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1491{
1492 OP_PARAMVAL param1, param2;
1493
1494#if HC_ARCH_BITS == 32 && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0)
1495 Assert(pDis->param1.size <= 4);
1496#endif
1497
1498 /* Source to make DISQueryParamVal read the register value - ugly hack */
1499 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
1500 if(RT_FAILURE(rc))
1501 return VERR_EM_INTERPRETER;
1502
1503 rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param2, &param2, PARAM_SOURCE);
1504 if(RT_FAILURE(rc))
1505 return VERR_EM_INTERPRETER;
1506
1507 uint64_t valpar;
1508 switch(param2.type)
1509 {
1510 case PARMTYPE_IMMEDIATE: /* register actually */
1511 valpar = param2.val.val64;
1512 break;
1513
1514 default:
1515 return VERR_EM_INTERPRETER;
1516 }
1517
1518 PGMPAGEMAPLOCK Lock;
1519 RTGCPTR GCPtrPar1;
1520 void *pvParam1;
1521 uint64_t eflags;
1522
1523 AssertReturn(pDis->param1.size == pDis->param2.size, VERR_EM_INTERPRETER);
1524 switch(param1.type)
1525 {
1526 case PARMTYPE_ADDRESS:
1527 GCPtrPar1 = param1.val.val64;
1528 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, GCPtrPar1);
1529
1530 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
1531 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1532 break;
1533
1534 default:
1535 return VERR_EM_INTERPRETER;
1536 }
1537
1538 LogFlow(("%s %RGv rax=%RX64 %RX64\n", emGetMnemonic(pDis), GCPtrPar1, pRegFrame->rax, valpar));
1539
1540 if (pDis->prefix & PREFIX_LOCK)
1541 eflags = EMEmulateLockCmpXchg(pvParam1, &pRegFrame->rax, valpar, pDis->param2.size);
1542 else
1543 eflags = EMEmulateCmpXchg(pvParam1, &pRegFrame->rax, valpar, pDis->param2.size);
1544
1545 LogFlow(("%s %RGv rax=%RX64 %RX64 ZF=%d\n", emGetMnemonic(pDis), GCPtrPar1, pRegFrame->rax, valpar, !!(eflags & X86_EFL_ZF)));
1546
1547 /* Update guest's eflags and finish. */
1548 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
1549 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
1550
1551 *pcbSize = param2.size;
1552 PGMPhysReleasePageMappingLock(pVM, &Lock);
1553 return VINF_SUCCESS;
1554}
1555
1556
1557/**
1558 * [LOCK] CMPXCHG8B emulation.
1559 */
1560static int emInterpretCmpXchg8b(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1561{
1562 Assert(pDis->mode != CPUMODE_64BIT); /** @todo check */
1563 OP_PARAMVAL param1;
1564
1565 /* Source to make DISQueryParamVal read the register value - ugly hack */
1566 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
1567 if(RT_FAILURE(rc))
1568 return VERR_EM_INTERPRETER;
1569
1570 RTGCPTR GCPtrPar1;
1571 void *pvParam1;
1572 uint64_t eflags;
1573 PGMPAGEMAPLOCK Lock;
1574
1575 AssertReturn(pDis->param1.size == 8, VERR_EM_INTERPRETER);
1576 switch(param1.type)
1577 {
1578 case PARMTYPE_ADDRESS:
1579 GCPtrPar1 = param1.val.val64;
1580 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, GCPtrPar1);
1581
1582 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
1583 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1584 break;
1585
1586 default:
1587 return VERR_EM_INTERPRETER;
1588 }
1589
1590 LogFlow(("%s %RGv=%08x eax=%08x\n", emGetMnemonic(pDis), pvParam1, pRegFrame->eax));
1591
1592 if (pDis->prefix & PREFIX_LOCK)
1593 eflags = EMEmulateLockCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
1594 else
1595 eflags = EMEmulateCmpXchg8b(pvParam1, &pRegFrame->eax, &pRegFrame->edx, pRegFrame->ebx, pRegFrame->ecx);
1596
1597 LogFlow(("%s %RGv=%08x eax=%08x ZF=%d\n", emGetMnemonic(pDis), pvParam1, pRegFrame->eax, !!(eflags & X86_EFL_ZF)));
1598
1599 /* Update guest's eflags and finish; note that *only* ZF is affected. */
1600 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_ZF))
1601 | (eflags & (X86_EFL_ZF));
1602
1603 *pcbSize = 8;
1604 PGMPhysReleasePageMappingLock(pVM, &Lock);
1605 return VINF_SUCCESS;
1606}
1607
1608
1609#ifdef IN_RC /** @todo test+enable for HWACCM as well. */
1610/**
1611 * [LOCK] XADD emulation.
1612 */
1613static int emInterpretXAdd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1614{
1615 Assert(pDis->mode != CPUMODE_64BIT); /** @todo check */
1616 OP_PARAMVAL param1;
1617 void *pvParamReg2;
1618 size_t cbParamReg2;
1619
1620 /* Source to make DISQueryParamVal read the register value - ugly hack */
1621 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
1622 if(RT_FAILURE(rc))
1623 return VERR_EM_INTERPRETER;
1624
1625 rc = DISQueryParamRegPtr(pRegFrame, pDis, &pDis->param2, &pvParamReg2, &cbParamReg2);
1626 Assert(cbParamReg2 <= 4);
1627 if(RT_FAILURE(rc))
1628 return VERR_EM_INTERPRETER;
1629
1630#ifdef IN_RC
1631 if (TRPMHasTrap(pVCpu))
1632 {
1633 if (TRPMGetErrorCode(pVCpu) & X86_TRAP_PF_RW)
1634 {
1635#endif
1636 RTGCPTR GCPtrPar1;
1637 void *pvParam1;
1638 uint32_t eflags;
1639 PGMPAGEMAPLOCK Lock;
1640
1641 AssertReturn(pDis->param1.size == pDis->param2.size, VERR_EM_INTERPRETER);
1642 switch(param1.type)
1643 {
1644 case PARMTYPE_ADDRESS:
1645 GCPtrPar1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, (RTRCUINTPTR)param1.val.val64);
1646#ifdef IN_RC
1647 EM_ASSERT_FAULT_RETURN(GCPtrPar1 == pvFault, VERR_EM_INTERPRETER);
1648#endif
1649
1650 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrPar1, &pvParam1, &Lock);
1651 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1652 break;
1653
1654 default:
1655 return VERR_EM_INTERPRETER;
1656 }
1657
1658 LogFlow(("XAdd %RGv=%p reg=%08llx\n", GCPtrPar1, pvParam1, *(uint64_t *)pvParamReg2));
1659
1660 if (pDis->prefix & PREFIX_LOCK)
1661 eflags = EMEmulateLockXAdd(pvParam1, pvParamReg2, cbParamReg2);
1662 else
1663 eflags = EMEmulateXAdd(pvParam1, pvParamReg2, cbParamReg2);
1664
1665 LogFlow(("XAdd %RGv=%p reg=%08llx ZF=%d\n", GCPtrPar1, pvParam1, *(uint64_t *)pvParamReg2, !!(eflags & X86_EFL_ZF) ));
1666
1667 /* Update guest's eflags and finish. */
1668 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
1669 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
1670
1671 *pcbSize = cbParamReg2;
1672 PGMPhysReleasePageMappingLock(pVM, &Lock);
1673 return VINF_SUCCESS;
1674#ifdef IN_RC
1675 }
1676 }
1677
1678 return VERR_EM_INTERPRETER;
1679#endif
1680}
1681#endif /* IN_RC */
1682
1683
1684#ifdef IN_RC
1685/**
1686 * Interpret IRET (currently only to V86 code)
1687 *
1688 * @returns VBox status code.
1689 * @param pVM The VM handle.
1690 * @param pVCpu The VMCPU handle.
1691 * @param pRegFrame The register frame.
1692 *
1693 */
1694VMMDECL(int) EMInterpretIret(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1695{
1696 RTGCUINTPTR pIretStack = (RTGCUINTPTR)pRegFrame->esp;
1697 RTGCUINTPTR eip, cs, esp, ss, eflags, ds, es, fs, gs, uMask;
1698 int rc;
1699
1700 Assert(!CPUMIsGuestIn64BitCode(pVCpu, pRegFrame));
1701
1702 rc = emRamRead(pVM, pVCpu, pRegFrame, &eip, (RTGCPTR)pIretStack , 4);
1703 rc |= emRamRead(pVM, pVCpu, pRegFrame, &cs, (RTGCPTR)(pIretStack + 4), 4);
1704 rc |= emRamRead(pVM, pVCpu, pRegFrame, &eflags, (RTGCPTR)(pIretStack + 8), 4);
1705 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1706 AssertReturn(eflags & X86_EFL_VM, VERR_EM_INTERPRETER);
1707
1708 rc |= emRamRead(pVM, pVCpu, pRegFrame, &esp, (RTGCPTR)(pIretStack + 12), 4);
1709 rc |= emRamRead(pVM, pVCpu, pRegFrame, &ss, (RTGCPTR)(pIretStack + 16), 4);
1710 rc |= emRamRead(pVM, pVCpu, pRegFrame, &es, (RTGCPTR)(pIretStack + 20), 4);
1711 rc |= emRamRead(pVM, pVCpu, pRegFrame, &ds, (RTGCPTR)(pIretStack + 24), 4);
1712 rc |= emRamRead(pVM, pVCpu, pRegFrame, &fs, (RTGCPTR)(pIretStack + 28), 4);
1713 rc |= emRamRead(pVM, pVCpu, pRegFrame, &gs, (RTGCPTR)(pIretStack + 32), 4);
1714 AssertRCReturn(rc, VERR_EM_INTERPRETER);
1715
1716 pRegFrame->eip = eip & 0xffff;
1717 pRegFrame->cs = cs;
1718
1719 /* Mask away all reserved bits */
1720 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;
1721 eflags &= uMask;
1722
1723#ifndef IN_RING0
1724 CPUMRawSetEFlags(pVCpu, pRegFrame, eflags);
1725#endif
1726 Assert((pRegFrame->eflags.u32 & (X86_EFL_IF|X86_EFL_IOPL)) == X86_EFL_IF);
1727
1728 pRegFrame->esp = esp;
1729 pRegFrame->ss = ss;
1730 pRegFrame->ds = ds;
1731 pRegFrame->es = es;
1732 pRegFrame->fs = fs;
1733 pRegFrame->gs = gs;
1734
1735 return VINF_SUCCESS;
1736}
1737#endif /* IN_RC */
1738
1739
1740/**
1741 * IRET Emulation.
1742 */
1743static int emInterpretIret(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1744{
1745 /* only allow direct calls to EMInterpretIret for now */
1746 return VERR_EM_INTERPRETER;
1747}
1748
1749/**
1750 * WBINVD Emulation.
1751 */
1752static int emInterpretWbInvd(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1753{
1754 /* Nothing to do. */
1755 return VINF_SUCCESS;
1756}
1757
1758
1759/**
1760 * Interpret INVLPG
1761 *
1762 * @returns VBox status code.
1763 * @param pVM The VM handle.
1764 * @param pVCpu The VMCPU handle.
1765 * @param pRegFrame The register frame.
1766 * @param pAddrGC Operand address
1767 *
1768 */
1769VMMDECL(VBOXSTRICTRC) EMInterpretInvlpg(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pAddrGC)
1770{
1771 /** @todo is addr always a flat linear address or ds based
1772 * (in absence of segment override prefixes)????
1773 */
1774#ifdef IN_RC
1775 LogFlow(("RC: EMULATE: invlpg %RGv\n", pAddrGC));
1776#endif
1777 VBOXSTRICTRC rc = PGMInvalidatePage(pVCpu, pAddrGC);
1778 if ( rc == VINF_SUCCESS
1779 || rc == VINF_PGM_SYNC_CR3 /* we can rely on the FF */)
1780 return VINF_SUCCESS;
1781 AssertMsgReturn(rc == VINF_EM_RAW_EMULATE_INSTR,
1782 ("%Rrc addr=%RGv\n", VBOXSTRICTRC_VAL(rc), pAddrGC),
1783 VERR_EM_INTERPRETER);
1784 return rc;
1785}
1786
1787
1788/**
1789 * INVLPG Emulation.
1790 */
1791static VBOXSTRICTRC emInterpretInvlPg(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1792{
1793 OP_PARAMVAL param1;
1794 RTGCPTR addr;
1795
1796 VBOXSTRICTRC rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
1797 if(RT_FAILURE(rc))
1798 return VERR_EM_INTERPRETER;
1799
1800 switch(param1.type)
1801 {
1802 case PARMTYPE_IMMEDIATE:
1803 case PARMTYPE_ADDRESS:
1804 if(!(param1.flags & (PARAM_VAL32|PARAM_VAL64)))
1805 return VERR_EM_INTERPRETER;
1806 addr = (RTGCPTR)param1.val.val64;
1807 break;
1808
1809 default:
1810 return VERR_EM_INTERPRETER;
1811 }
1812
1813 /** @todo is addr always a flat linear address or ds based
1814 * (in absence of segment override prefixes)????
1815 */
1816#ifdef IN_RC
1817 LogFlow(("RC: EMULATE: invlpg %RGv\n", addr));
1818#endif
1819 rc = PGMInvalidatePage(pVCpu, addr);
1820 if ( rc == VINF_SUCCESS
1821 || rc == VINF_PGM_SYNC_CR3 /* we can rely on the FF */)
1822 return VINF_SUCCESS;
1823 AssertMsgReturn(rc == VINF_EM_RAW_EMULATE_INSTR,
1824 ("%Rrc addr=%RGv\n", VBOXSTRICTRC_VAL(rc), addr),
1825 VERR_EM_INTERPRETER);
1826 return rc;
1827}
1828
1829/** @todo change all these EMInterpretXXX methods to VBOXSTRICTRC. */
1830
1831/**
1832 * Interpret CPUID given the parameters in the CPU context
1833 *
1834 * @returns VBox status code.
1835 * @param pVM The VM handle.
1836 * @param pVCpu The VMCPU handle.
1837 * @param pRegFrame The register frame.
1838 *
1839 */
1840VMMDECL(int) EMInterpretCpuId(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
1841{
1842 uint32_t iLeaf = pRegFrame->eax;
1843
1844 /* cpuid clears the high dwords of the affected 64 bits registers. */
1845 pRegFrame->rax = 0;
1846 pRegFrame->rbx = 0;
1847 pRegFrame->rcx &= UINT64_C(0x00000000ffffffff);
1848 pRegFrame->rdx = 0;
1849
1850 /* Note: operates the same in 64 and non-64 bits mode. */
1851 CPUMGetGuestCpuId(pVCpu, iLeaf, &pRegFrame->eax, &pRegFrame->ebx, &pRegFrame->ecx, &pRegFrame->edx);
1852 Log(("Emulate: CPUID %x -> %08x %08x %08x %08x\n", iLeaf, pRegFrame->eax, pRegFrame->ebx, pRegFrame->ecx, pRegFrame->edx));
1853 return VINF_SUCCESS;
1854}
1855
1856
1857/**
1858 * CPUID Emulation.
1859 */
1860static int emInterpretCpuId(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1861{
1862 int rc = EMInterpretCpuId(pVM, pVCpu, pRegFrame);
1863 return rc;
1864}
1865
1866
1867/**
1868 * Interpret CRx read
1869 *
1870 * @returns VBox status code.
1871 * @param pVM The VM handle.
1872 * @param pVCpu The VMCPU handle.
1873 * @param pRegFrame The register frame.
1874 * @param DestRegGen General purpose register index (USE_REG_E**))
1875 * @param SrcRegCRx CRx register index (USE_REG_CR*)
1876 *
1877 */
1878VMMDECL(int) EMInterpretCRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegCrx)
1879{
1880 uint64_t val64;
1881 int rc = CPUMGetGuestCRx(pVCpu, SrcRegCrx, &val64);
1882 AssertMsgRCReturn(rc, ("CPUMGetGuestCRx %d failed\n", SrcRegCrx), VERR_EM_INTERPRETER);
1883
1884 if (CPUMIsGuestIn64BitCode(pVCpu, pRegFrame))
1885 rc = DISWriteReg64(pRegFrame, DestRegGen, val64);
1886 else
1887 rc = DISWriteReg32(pRegFrame, DestRegGen, val64);
1888
1889 if (RT_SUCCESS(rc))
1890 {
1891 LogFlow(("MOV_CR: gen32=%d CR=%d val=%RX64\n", DestRegGen, SrcRegCrx, val64));
1892 return VINF_SUCCESS;
1893 }
1894 return VERR_EM_INTERPRETER;
1895}
1896
1897
1898
1899/**
1900 * Interpret CLTS
1901 *
1902 * @returns VBox status code.
1903 * @param pVM The VM handle.
1904 * @param pVCpu The VMCPU handle.
1905 *
1906 */
1907VMMDECL(int) EMInterpretCLTS(PVM pVM, PVMCPU pVCpu)
1908{
1909 uint64_t cr0 = CPUMGetGuestCR0(pVCpu);
1910 if (!(cr0 & X86_CR0_TS))
1911 return VINF_SUCCESS;
1912 return CPUMSetGuestCR0(pVCpu, cr0 & ~X86_CR0_TS);
1913}
1914
1915/**
1916 * CLTS Emulation.
1917 */
1918static int emInterpretClts(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
1919{
1920 return EMInterpretCLTS(pVM, pVCpu);
1921}
1922
1923
1924/**
1925 * Update CRx
1926 *
1927 * @returns VBox status code.
1928 * @param pVM The VM handle.
1929 * @param pVCpu The VMCPU handle.
1930 * @param pRegFrame The register frame.
1931 * @param DestRegCRx CRx register index (USE_REG_CR*)
1932 * @param val New CRx value
1933 *
1934 */
1935static int emUpdateCRx(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint64_t val)
1936{
1937 uint64_t oldval;
1938 uint64_t msrEFER;
1939 int rc, rc2;
1940
1941 /** @todo Clean up this mess. */
1942 LogFlow(("EMInterpretCRxWrite at %RGv CR%d <- %RX64\n", (RTGCPTR)pRegFrame->rip, DestRegCrx, val));
1943 switch (DestRegCrx)
1944 {
1945 case USE_REG_CR0:
1946 oldval = CPUMGetGuestCR0(pVCpu);
1947#ifdef IN_RC
1948 /* CR0.WP and CR0.AM changes require a reschedule run in ring 3. */
1949 if ( (val & (X86_CR0_WP | X86_CR0_AM))
1950 != (oldval & (X86_CR0_WP | X86_CR0_AM)))
1951 return VERR_EM_INTERPRETER;
1952#endif
1953 rc = VINF_SUCCESS;
1954 CPUMSetGuestCR0(pVCpu, val);
1955 val = CPUMGetGuestCR0(pVCpu);
1956 if ( (oldval & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
1957 != (val & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
1958 {
1959 /* global flush */
1960 rc = PGMFlushTLB(pVCpu, CPUMGetGuestCR3(pVCpu), true /* global */);
1961 AssertRCReturn(rc, rc);
1962 }
1963
1964 /* Deal with long mode enabling/disabling. */
1965 msrEFER = CPUMGetGuestEFER(pVCpu);
1966 if (msrEFER & MSR_K6_EFER_LME)
1967 {
1968 if ( !(oldval & X86_CR0_PG)
1969 && (val & X86_CR0_PG))
1970 {
1971 /* Illegal to have an active 64 bits CS selector (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
1972 if (pRegFrame->csHid.Attr.n.u1Long)
1973 {
1974 AssertMsgFailed(("Illegal enabling of paging with CS.u1Long = 1!!\n"));
1975 return VERR_EM_INTERPRETER; /* @todo generate #GP(0) */
1976 }
1977
1978 /* Illegal to switch to long mode before activating PAE first (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
1979 if (!(CPUMGetGuestCR4(pVCpu) & X86_CR4_PAE))
1980 {
1981 AssertMsgFailed(("Illegal enabling of paging with PAE disabled!!\n"));
1982 return VERR_EM_INTERPRETER; /* @todo generate #GP(0) */
1983 }
1984 msrEFER |= MSR_K6_EFER_LMA;
1985 }
1986 else
1987 if ( (oldval & X86_CR0_PG)
1988 && !(val & X86_CR0_PG))
1989 {
1990 msrEFER &= ~MSR_K6_EFER_LMA;
1991 /* @todo Do we need to cut off rip here? High dword of rip is undefined, so it shouldn't really matter. */
1992 }
1993 CPUMSetGuestEFER(pVCpu, msrEFER);
1994 }
1995 rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
1996 return rc2 == VINF_SUCCESS ? rc : rc2;
1997
1998 case USE_REG_CR2:
1999 rc = CPUMSetGuestCR2(pVCpu, val); AssertRC(rc);
2000 return VINF_SUCCESS;
2001
2002 case USE_REG_CR3:
2003 /* Reloading the current CR3 means the guest just wants to flush the TLBs */
2004 rc = CPUMSetGuestCR3(pVCpu, val); AssertRC(rc);
2005 if (CPUMGetGuestCR0(pVCpu) & X86_CR0_PG)
2006 {
2007 /* flush */
2008 rc = PGMFlushTLB(pVCpu, val, !(CPUMGetGuestCR4(pVCpu) & X86_CR4_PGE));
2009 AssertRC(rc);
2010 }
2011 return rc;
2012
2013 case USE_REG_CR4:
2014 oldval = CPUMGetGuestCR4(pVCpu);
2015 rc = CPUMSetGuestCR4(pVCpu, val); AssertRC(rc);
2016 val = CPUMGetGuestCR4(pVCpu);
2017
2018 /* Illegal to disable PAE when long mode is active. (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
2019 msrEFER = CPUMGetGuestEFER(pVCpu);
2020 if ( (msrEFER & MSR_K6_EFER_LMA)
2021 && (oldval & X86_CR4_PAE)
2022 && !(val & X86_CR4_PAE))
2023 {
2024 return VERR_EM_INTERPRETER; /** @todo generate #GP(0) */
2025 }
2026
2027 rc = VINF_SUCCESS;
2028 if ( (oldval & (X86_CR4_PGE|X86_CR4_PAE|X86_CR4_PSE))
2029 != (val & (X86_CR4_PGE|X86_CR4_PAE|X86_CR4_PSE)))
2030 {
2031 /* global flush */
2032 rc = PGMFlushTLB(pVCpu, CPUMGetGuestCR3(pVCpu), true /* global */);
2033 AssertRCReturn(rc, rc);
2034 }
2035
2036 /* Feeling extremely lazy. */
2037# ifdef IN_RC
2038 if ( (oldval & (X86_CR4_OSFSXR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME))
2039 != (val & (X86_CR4_OSFSXR|X86_CR4_OSXMMEEXCPT|X86_CR4_PCE|X86_CR4_MCE|X86_CR4_PAE|X86_CR4_DE|X86_CR4_TSD|X86_CR4_PVI|X86_CR4_VME)))
2040 {
2041 Log(("emInterpretMovCRx: CR4: %#RX64->%#RX64 => R3\n", oldval, val));
2042 VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
2043 }
2044# endif
2045 if ((val ^ oldval) & X86_CR4_VME)
2046 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
2047
2048 rc2 = PGMChangeMode(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR4(pVCpu), CPUMGetGuestEFER(pVCpu));
2049 return rc2 == VINF_SUCCESS ? rc : rc2;
2050
2051 case USE_REG_CR8:
2052 return PDMApicSetTPR(pVCpu, val << 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
2053
2054 default:
2055 AssertFailed();
2056 case USE_REG_CR1: /* illegal op */
2057 break;
2058 }
2059 return VERR_EM_INTERPRETER;
2060}
2061
2062/**
2063 * Interpret CRx write
2064 *
2065 * @returns VBox status code.
2066 * @param pVM The VM handle.
2067 * @param pVCpu The VMCPU handle.
2068 * @param pRegFrame The register frame.
2069 * @param DestRegCRx CRx register index (USE_REG_CR*)
2070 * @param SrcRegGen General purpose register index (USE_REG_E**))
2071 *
2072 */
2073VMMDECL(int) EMInterpretCRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint32_t SrcRegGen)
2074{
2075 uint64_t val;
2076 int rc;
2077
2078 if (CPUMIsGuestIn64BitCode(pVCpu, pRegFrame))
2079 {
2080 rc = DISFetchReg64(pRegFrame, SrcRegGen, &val);
2081 }
2082 else
2083 {
2084 uint32_t val32;
2085 rc = DISFetchReg32(pRegFrame, SrcRegGen, &val32);
2086 val = val32;
2087 }
2088
2089 if (RT_SUCCESS(rc))
2090 return emUpdateCRx(pVM, pVCpu, pRegFrame, DestRegCrx, val);
2091
2092 return VERR_EM_INTERPRETER;
2093}
2094
2095/**
2096 * Interpret LMSW
2097 *
2098 * @returns VBox status code.
2099 * @param pVM The VM handle.
2100 * @param pVCpu The VMCPU handle.
2101 * @param pRegFrame The register frame.
2102 * @param u16Data LMSW source data.
2103 *
2104 */
2105VMMDECL(int) EMInterpretLMSW(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint16_t u16Data)
2106{
2107 uint64_t OldCr0 = CPUMGetGuestCR0(pVCpu);
2108
2109 /* Only PE, MP, EM and TS can be changed; note that PE can't be cleared by this instruction. */
2110 uint64_t NewCr0 = ( OldCr0 & ~( X86_CR0_MP | X86_CR0_EM | X86_CR0_TS))
2111 | (u16Data & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS));
2112
2113 return emUpdateCRx(pVM, pVCpu, pRegFrame, USE_REG_CR0, NewCr0);
2114}
2115
2116/**
2117 * LMSW Emulation.
2118 */
2119static int emInterpretLmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2120{
2121 OP_PARAMVAL param1;
2122 uint32_t val;
2123
2124 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
2125 if(RT_FAILURE(rc))
2126 return VERR_EM_INTERPRETER;
2127
2128 switch(param1.type)
2129 {
2130 case PARMTYPE_IMMEDIATE:
2131 case PARMTYPE_ADDRESS:
2132 if(!(param1.flags & PARAM_VAL16))
2133 return VERR_EM_INTERPRETER;
2134 val = param1.val.val32;
2135 break;
2136
2137 default:
2138 return VERR_EM_INTERPRETER;
2139 }
2140
2141 LogFlow(("emInterpretLmsw %x\n", val));
2142 return EMInterpretLMSW(pVM, pVCpu, pRegFrame, val);
2143}
2144
2145#ifdef EM_EMULATE_SMSW
2146/**
2147 * SMSW Emulation.
2148 */
2149static int emInterpretSmsw(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2150{
2151 OP_PARAMVAL param1;
2152 uint64_t cr0 = CPUMGetGuestCR0(pVCpu);
2153
2154 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
2155 if(RT_FAILURE(rc))
2156 return VERR_EM_INTERPRETER;
2157
2158 switch(param1.type)
2159 {
2160 case PARMTYPE_IMMEDIATE:
2161 if(param1.size != sizeof(uint16_t))
2162 return VERR_EM_INTERPRETER;
2163 LogFlow(("emInterpretSmsw %d <- cr0 (%x)\n", pDis->param1.base.reg_gen, cr0));
2164 rc = DISWriteReg16(pRegFrame, pDis->param1.base.reg_gen, cr0);
2165 break;
2166
2167 case PARMTYPE_ADDRESS:
2168 {
2169 RTGCPTR pParam1;
2170
2171 /* Actually forced to 16 bits regardless of the operand size. */
2172 if(param1.size != sizeof(uint16_t))
2173 return VERR_EM_INTERPRETER;
2174
2175 pParam1 = (RTGCPTR)param1.val.val64;
2176 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, pParam1);
2177 LogFlow(("emInterpretSmsw %RGv <- cr0 (%x)\n", pParam1, cr0));
2178
2179 rc = emRamWrite(pVM, pVCpu, pRegFrame, pParam1, &cr0, sizeof(uint16_t));
2180 if (RT_FAILURE(rc))
2181 {
2182 AssertMsgFailed(("emRamWrite %RGv size=%d failed with %Rrc\n", pParam1, param1.size, rc));
2183 return VERR_EM_INTERPRETER;
2184 }
2185 break;
2186 }
2187
2188 default:
2189 return VERR_EM_INTERPRETER;
2190 }
2191
2192 LogFlow(("emInterpretSmsw %x\n", cr0));
2193 return rc;
2194}
2195#endif
2196
2197/**
2198 * MOV CRx
2199 */
2200static int emInterpretMovCRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2201{
2202 if ((pDis->param1.flags == USE_REG_GEN32 || pDis->param1.flags == USE_REG_GEN64) && pDis->param2.flags == USE_REG_CR)
2203 return EMInterpretCRxRead(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_gen, pDis->param2.base.reg_ctrl);
2204
2205 if (pDis->param1.flags == USE_REG_CR && (pDis->param2.flags == USE_REG_GEN32 || pDis->param2.flags == USE_REG_GEN64))
2206 return EMInterpretCRxWrite(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_ctrl, pDis->param2.base.reg_gen);
2207
2208 AssertMsgFailedReturn(("Unexpected control register move\n"), VERR_EM_INTERPRETER);
2209 return VERR_EM_INTERPRETER;
2210}
2211
2212
2213/**
2214 * Interpret DRx write
2215 *
2216 * @returns VBox status code.
2217 * @param pVM The VM handle.
2218 * @param pVCpu The VMCPU handle.
2219 * @param pRegFrame The register frame.
2220 * @param DestRegDRx DRx register index (USE_REG_DR*)
2221 * @param SrcRegGen General purpose register index (USE_REG_E**))
2222 *
2223 */
2224VMMDECL(int) EMInterpretDRxWrite(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen)
2225{
2226 uint64_t val;
2227 int rc;
2228
2229 if (CPUMIsGuestIn64BitCode(pVCpu, pRegFrame))
2230 {
2231 rc = DISFetchReg64(pRegFrame, SrcRegGen, &val);
2232 }
2233 else
2234 {
2235 uint32_t val32;
2236 rc = DISFetchReg32(pRegFrame, SrcRegGen, &val32);
2237 val = val32;
2238 }
2239
2240 if (RT_SUCCESS(rc))
2241 {
2242 /** @todo we don't fail if illegal bits are set/cleared for e.g. dr7 */
2243 rc = CPUMSetGuestDRx(pVCpu, DestRegDrx, val);
2244 if (RT_SUCCESS(rc))
2245 return rc;
2246 AssertMsgFailed(("CPUMSetGuestDRx %d failed\n", DestRegDrx));
2247 }
2248 return VERR_EM_INTERPRETER;
2249}
2250
2251
2252/**
2253 * Interpret DRx read
2254 *
2255 * @returns VBox status code.
2256 * @param pVM The VM handle.
2257 * @param pVCpu The VMCPU handle.
2258 * @param pRegFrame The register frame.
2259 * @param DestRegGen General purpose register index (USE_REG_E**))
2260 * @param SrcRegDRx DRx register index (USE_REG_DR*)
2261 *
2262 */
2263VMMDECL(int) EMInterpretDRxRead(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx)
2264{
2265 uint64_t val64;
2266
2267 int rc = CPUMGetGuestDRx(pVCpu, SrcRegDrx, &val64);
2268 AssertMsgRCReturn(rc, ("CPUMGetGuestDRx %d failed\n", SrcRegDrx), VERR_EM_INTERPRETER);
2269 if (CPUMIsGuestIn64BitCode(pVCpu, pRegFrame))
2270 {
2271 rc = DISWriteReg64(pRegFrame, DestRegGen, val64);
2272 }
2273 else
2274 rc = DISWriteReg32(pRegFrame, DestRegGen, (uint32_t)val64);
2275
2276 if (RT_SUCCESS(rc))
2277 return VINF_SUCCESS;
2278
2279 return VERR_EM_INTERPRETER;
2280}
2281
2282
2283/**
2284 * MOV DRx
2285 */
2286static int emInterpretMovDRx(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2287{
2288 int rc = VERR_EM_INTERPRETER;
2289
2290 if((pDis->param1.flags == USE_REG_GEN32 || pDis->param1.flags == USE_REG_GEN64) && pDis->param2.flags == USE_REG_DBG)
2291 {
2292 rc = EMInterpretDRxRead(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_gen, pDis->param2.base.reg_dbg);
2293 }
2294 else
2295 if(pDis->param1.flags == USE_REG_DBG && (pDis->param2.flags == USE_REG_GEN32 || pDis->param2.flags == USE_REG_GEN64))
2296 {
2297 rc = EMInterpretDRxWrite(pVM, pVCpu, pRegFrame, pDis->param1.base.reg_dbg, pDis->param2.base.reg_gen);
2298 }
2299 else
2300 AssertMsgFailed(("Unexpected debug register move\n"));
2301
2302 return rc;
2303}
2304
2305
2306/**
2307 * LLDT Emulation.
2308 */
2309static int emInterpretLLdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2310{
2311 OP_PARAMVAL param1;
2312 RTSEL sel;
2313
2314 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
2315 if(RT_FAILURE(rc))
2316 return VERR_EM_INTERPRETER;
2317
2318 switch(param1.type)
2319 {
2320 case PARMTYPE_ADDRESS:
2321 return VERR_EM_INTERPRETER; //feeling lazy right now
2322
2323 case PARMTYPE_IMMEDIATE:
2324 if(!(param1.flags & PARAM_VAL16))
2325 return VERR_EM_INTERPRETER;
2326 sel = (RTSEL)param1.val.val16;
2327 break;
2328
2329 default:
2330 return VERR_EM_INTERPRETER;
2331 }
2332
2333#ifdef IN_RING0
2334 /* Only for the VT-x real-mode emulation case. */
2335 AssertReturn(CPUMIsGuestInRealMode(pVCpu), VERR_EM_INTERPRETER);
2336 CPUMSetGuestLDTR(pVCpu, sel);
2337 return VINF_SUCCESS;
2338#else
2339 if (sel == 0)
2340 {
2341 if (CPUMGetHyperLDTR(pVCpu) == 0)
2342 {
2343 // this simple case is most frequent in Windows 2000 (31k - boot & shutdown)
2344 return VINF_SUCCESS;
2345 }
2346 }
2347 //still feeling lazy
2348 return VERR_EM_INTERPRETER;
2349#endif
2350}
2351
2352#ifdef IN_RING0
2353/**
2354 * LIDT/LGDT Emulation.
2355 */
2356static int emInterpretLIGdt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2357{
2358 OP_PARAMVAL param1;
2359 RTGCPTR pParam1;
2360 X86XDTR32 dtr32;
2361
2362 Log(("Emulate %s at %RGv\n", emGetMnemonic(pDis), (RTGCPTR)pRegFrame->rip));
2363
2364 /* Only for the VT-x real-mode emulation case. */
2365 AssertReturn(CPUMIsGuestInRealMode(pVCpu), VERR_EM_INTERPRETER);
2366
2367 int rc = DISQueryParamVal(pRegFrame, pDis, &pDis->param1, &param1, PARAM_SOURCE);
2368 if(RT_FAILURE(rc))
2369 return VERR_EM_INTERPRETER;
2370
2371 switch(param1.type)
2372 {
2373 case PARMTYPE_ADDRESS:
2374 pParam1 = emConvertToFlatAddr(pVM, pRegFrame, pDis, &pDis->param1, param1.val.val16);
2375 break;
2376
2377 default:
2378 return VERR_EM_INTERPRETER;
2379 }
2380
2381 rc = emRamRead(pVM, pVCpu, pRegFrame, &dtr32, pParam1, sizeof(dtr32));
2382 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2383
2384 if (!(pDis->prefix & PREFIX_OPSIZE))
2385 dtr32.uAddr &= 0xffffff; /* 16 bits operand size */
2386
2387 if (pDis->pCurInstr->opcode == OP_LIDT)
2388 CPUMSetGuestIDTR(pVCpu, dtr32.uAddr, dtr32.cb);
2389 else
2390 CPUMSetGuestGDTR(pVCpu, dtr32.uAddr, dtr32.cb);
2391
2392 return VINF_SUCCESS;
2393}
2394#endif
2395
2396
2397#ifdef IN_RC
2398/**
2399 * STI Emulation.
2400 *
2401 * @remark the instruction following sti is guaranteed to be executed before any interrupts are dispatched
2402 */
2403static int emInterpretSti(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2404{
2405 PPATMGCSTATE pGCState = PATMQueryGCState(pVM);
2406
2407 if(!pGCState)
2408 {
2409 Assert(pGCState);
2410 return VERR_EM_INTERPRETER;
2411 }
2412 pGCState->uVMFlags |= X86_EFL_IF;
2413
2414 Assert(pRegFrame->eflags.u32 & X86_EFL_IF);
2415 Assert(pvFault == SELMToFlat(pVM, DIS_SELREG_CS, pRegFrame, (RTGCPTR)pRegFrame->rip));
2416
2417 pVCpu->em.s.GCPtrInhibitInterrupts = pRegFrame->eip + pDis->opsize;
2418 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2419
2420 return VINF_SUCCESS;
2421}
2422#endif /* IN_RC */
2423
2424
2425/**
2426 * HLT Emulation.
2427 */
2428static VBOXSTRICTRC
2429emInterpretHlt(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2430{
2431 return VINF_EM_HALT;
2432}
2433
2434
2435/**
2436 * Interpret RDTSC
2437 *
2438 * @returns VBox status code.
2439 * @param pVM The VM handle.
2440 * @param pVCpu The VMCPU handle.
2441 * @param pRegFrame The register frame.
2442 *
2443 */
2444VMMDECL(int) EMInterpretRdtsc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2445{
2446 unsigned uCR4 = CPUMGetGuestCR4(pVCpu);
2447
2448 if (uCR4 & X86_CR4_TSD)
2449 return VERR_EM_INTERPRETER; /* genuine #GP */
2450
2451 uint64_t uTicks = TMCpuTickGet(pVCpu);
2452
2453 /* Same behaviour in 32 & 64 bits mode */
2454 pRegFrame->rax = (uint32_t)uTicks;
2455 pRegFrame->rdx = (uTicks >> 32ULL);
2456
2457 return VINF_SUCCESS;
2458}
2459
2460/**
2461 * Interpret RDTSCP
2462 *
2463 * @returns VBox status code.
2464 * @param pVM The VM handle.
2465 * @param pVCpu The VMCPU handle.
2466 * @param pCtx The CPU context.
2467 *
2468 */
2469VMMDECL(int) EMInterpretRdtscp(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2470{
2471 unsigned uCR4 = CPUMGetGuestCR4(pVCpu);
2472
2473 if (!CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP))
2474 {
2475 AssertFailed();
2476 return VERR_EM_INTERPRETER; /* genuine #UD */
2477 }
2478
2479 if (uCR4 & X86_CR4_TSD)
2480 return VERR_EM_INTERPRETER; /* genuine #GP */
2481
2482 uint64_t uTicks = TMCpuTickGet(pVCpu);
2483
2484 /* Same behaviour in 32 & 64 bits mode */
2485 pCtx->rax = (uint32_t)uTicks;
2486 pCtx->rdx = (uTicks >> 32ULL);
2487 /* Low dword of the TSC_AUX msr only. */
2488 CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pCtx->rcx);
2489 pCtx->rcx &= UINT32_C(0xffffffff);
2490
2491 return VINF_SUCCESS;
2492}
2493
2494/**
2495 * RDTSC Emulation.
2496 */
2497static int emInterpretRdtsc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2498{
2499 return EMInterpretRdtsc(pVM, pVCpu, pRegFrame);
2500}
2501
2502/**
2503 * Interpret RDPMC
2504 *
2505 * @returns VBox status code.
2506 * @param pVM The VM handle.
2507 * @param pVCpu The VMCPU handle.
2508 * @param pRegFrame The register frame.
2509 *
2510 */
2511VMMDECL(int) EMInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2512{
2513 unsigned uCR4 = CPUMGetGuestCR4(pVCpu);
2514
2515 /* If X86_CR4_PCE is not set, then CPL must be zero. */
2516 if ( !(uCR4 & X86_CR4_PCE)
2517 && CPUMGetGuestCPL(pVCpu, pRegFrame) != 0)
2518 {
2519 Assert(CPUMGetGuestCR0(pVCpu) & X86_CR0_PE);
2520 return VERR_EM_INTERPRETER; /* genuine #GP */
2521 }
2522
2523 /* Just return zero here; rather tricky to properly emulate this, especially as the specs are a mess. */
2524 pRegFrame->rax = 0;
2525 pRegFrame->rdx = 0;
2526 /** @todo We should trigger a #GP here if the cpu doesn't support the index in ecx. */
2527 return VINF_SUCCESS;
2528}
2529
2530/**
2531 * RDPMC Emulation
2532 */
2533static int emInterpretRdpmc(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2534{
2535 return EMInterpretRdpmc(pVM, pVCpu, pRegFrame);
2536}
2537
2538/**
2539 * MONITOR Emulation.
2540 */
2541VMMDECL(int) EMInterpretMonitor(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2542{
2543 uint32_t u32Dummy, u32ExtFeatures, cpl;
2544
2545 if (pRegFrame->ecx != 0)
2546 {
2547 Log(("emInterpretMonitor: unexpected ecx=%x -> recompiler!!\n", pRegFrame->ecx));
2548 return VERR_EM_INTERPRETER; /* illegal value. */
2549 }
2550
2551 /* Get the current privilege level. */
2552 cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
2553 if (cpl != 0)
2554 return VERR_EM_INTERPRETER; /* supervisor only */
2555
2556 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
2557 if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
2558 return VERR_EM_INTERPRETER; /* not supported */
2559
2560 pVCpu->em.s.mwait.uMonitorEAX = pRegFrame->rax;
2561 pVCpu->em.s.mwait.uMonitorECX = pRegFrame->rcx;
2562 pVCpu->em.s.mwait.uMonitorEDX = pRegFrame->rdx;
2563 pVCpu->em.s.mwait.fWait |= EMMWAIT_FLAG_MONITOR_ACTIVE;
2564 return VINF_SUCCESS;
2565}
2566
2567static int emInterpretMonitor(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2568{
2569 return EMInterpretMonitor(pVM, pVCpu, pRegFrame);
2570}
2571
2572
2573/**
2574 * MWAIT Emulation.
2575 */
2576VMMDECL(VBOXSTRICTRC) EMInterpretMWait(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2577{
2578 uint32_t u32Dummy, u32ExtFeatures, cpl, u32MWaitFeatures;
2579
2580 /* Get the current privilege level. */
2581 cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
2582 if (cpl != 0)
2583 return VERR_EM_INTERPRETER; /* supervisor only */
2584
2585 CPUMGetGuestCpuId(pVCpu, 1, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Dummy);
2586 if (!(u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR))
2587 return VERR_EM_INTERPRETER; /* not supported */
2588
2589 /*
2590 * CPUID.05H.ECX[0] defines support for power management extensions (eax)
2591 * CPUID.05H.ECX[1] defines support for interrupts as break events for mwait even when IF=0
2592 */
2593 CPUMGetGuestCpuId(pVCpu, 5, &u32Dummy, &u32Dummy, &u32MWaitFeatures, &u32Dummy);
2594 if (pRegFrame->ecx > 1)
2595 {
2596 Log(("EMInterpretMWait: unexpected ecx value %x -> recompiler\n", pRegFrame->ecx));
2597 return VERR_EM_INTERPRETER; /* illegal value. */
2598 }
2599
2600 if (pRegFrame->ecx)
2601 {
2602 if (!(u32MWaitFeatures & X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
2603 {
2604 Log(("EMInterpretMWait: unsupported X86_CPUID_MWAIT_ECX_BREAKIRQIF0 -> recompiler\n"));
2605 return VERR_EM_INTERPRETER; /* illegal value. */
2606 }
2607
2608 pVCpu->em.s.mwait.fWait = EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0;
2609 }
2610 else
2611 pVCpu->em.s.mwait.fWait = EMMWAIT_FLAG_ACTIVE;
2612
2613 pVCpu->em.s.mwait.uMWaitEAX = pRegFrame->rax;
2614 pVCpu->em.s.mwait.uMWaitECX = pRegFrame->rcx;
2615
2616 /** @todo not completely correct */
2617 return VINF_EM_HALT;
2618}
2619
2620static VBOXSTRICTRC emInterpretMWait(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2621{
2622 return EMInterpretMWait(pVM, pVCpu, pRegFrame);
2623}
2624
2625
2626#ifdef LOG_ENABLED
2627static const char *emMSRtoString(uint32_t uMsr)
2628{
2629 switch (uMsr)
2630 {
2631 case MSR_IA32_APICBASE:
2632 return "MSR_IA32_APICBASE";
2633 case MSR_IA32_CR_PAT:
2634 return "MSR_IA32_CR_PAT";
2635 case MSR_IA32_SYSENTER_CS:
2636 return "MSR_IA32_SYSENTER_CS";
2637 case MSR_IA32_SYSENTER_EIP:
2638 return "MSR_IA32_SYSENTER_EIP";
2639 case MSR_IA32_SYSENTER_ESP:
2640 return "MSR_IA32_SYSENTER_ESP";
2641 case MSR_K6_EFER:
2642 return "MSR_K6_EFER";
2643 case MSR_K8_SF_MASK:
2644 return "MSR_K8_SF_MASK";
2645 case MSR_K6_STAR:
2646 return "MSR_K6_STAR";
2647 case MSR_K8_LSTAR:
2648 return "MSR_K8_LSTAR";
2649 case MSR_K8_CSTAR:
2650 return "MSR_K8_CSTAR";
2651 case MSR_K8_FS_BASE:
2652 return "MSR_K8_FS_BASE";
2653 case MSR_K8_GS_BASE:
2654 return "MSR_K8_GS_BASE";
2655 case MSR_K8_KERNEL_GS_BASE:
2656 return "MSR_K8_KERNEL_GS_BASE";
2657 case MSR_K8_TSC_AUX:
2658 return "MSR_K8_TSC_AUX";
2659 case MSR_IA32_BIOS_SIGN_ID:
2660 return "Unsupported MSR_IA32_BIOS_SIGN_ID";
2661 case MSR_IA32_PLATFORM_ID:
2662 return "Unsupported MSR_IA32_PLATFORM_ID";
2663 case MSR_IA32_BIOS_UPDT_TRIG:
2664 return "Unsupported MSR_IA32_BIOS_UPDT_TRIG";
2665 case MSR_IA32_TSC:
2666 return "MSR_IA32_TSC";
2667 case MSR_IA32_MISC_ENABLE:
2668 return "MSR_IA32_MISC_ENABLE";
2669 case MSR_IA32_MTRR_CAP:
2670 return "Unsupported MSR_IA32_MTRR_CAP";
2671 case MSR_IA32_MCP_CAP:
2672 return "Unsupported MSR_IA32_MCP_CAP";
2673 case MSR_IA32_MCP_STATUS:
2674 return "Unsupported MSR_IA32_MCP_STATUS";
2675 case MSR_IA32_MCP_CTRL:
2676 return "Unsupported MSR_IA32_MCP_CTRL";
2677 case MSR_IA32_MTRR_DEF_TYPE:
2678 return "Unsupported MSR_IA32_MTRR_DEF_TYPE";
2679 case MSR_K7_EVNTSEL0:
2680 return "Unsupported MSR_K7_EVNTSEL0";
2681 case MSR_K7_EVNTSEL1:
2682 return "Unsupported MSR_K7_EVNTSEL1";
2683 case MSR_K7_EVNTSEL2:
2684 return "Unsupported MSR_K7_EVNTSEL2";
2685 case MSR_K7_EVNTSEL3:
2686 return "Unsupported MSR_K7_EVNTSEL3";
2687 case MSR_IA32_MC0_CTL:
2688 return "Unsupported MSR_IA32_MC0_CTL";
2689 case MSR_IA32_MC0_STATUS:
2690 return "Unsupported MSR_IA32_MC0_STATUS";
2691 case MSR_IA32_PERFEVTSEL0:
2692 return "Unsupported MSR_IA32_PERFEVTSEL0";
2693 case MSR_IA32_PERFEVTSEL1:
2694 return "Unsupported MSR_IA32_PERFEVTSEL1";
2695 case MSR_IA32_PERF_STATUS:
2696 return "MSR_IA32_PERF_STATUS";
2697 case MSR_IA32_PLATFORM_INFO:
2698 return "MSR_IA32_PLATFORM_INFO";
2699 case MSR_IA32_PERF_CTL:
2700 return "Unsupported MSR_IA32_PERF_CTL";
2701 case MSR_K7_PERFCTR0:
2702 return "Unsupported MSR_K7_PERFCTR0";
2703 case MSR_K7_PERFCTR1:
2704 return "Unsupported MSR_K7_PERFCTR1";
2705 case MSR_K7_PERFCTR2:
2706 return "Unsupported MSR_K7_PERFCTR2";
2707 case MSR_K7_PERFCTR3:
2708 return "Unsupported MSR_K7_PERFCTR3";
2709 case MSR_IA32_PMC0:
2710 return "Unsupported MSR_IA32_PMC0";
2711 case MSR_IA32_PMC1:
2712 return "Unsupported MSR_IA32_PMC1";
2713 case MSR_IA32_PMC2:
2714 return "Unsupported MSR_IA32_PMC2";
2715 case MSR_IA32_PMC3:
2716 return "Unsupported MSR_IA32_PMC3";
2717 }
2718 return "Unknown MSR";
2719}
2720#endif /* LOG_ENABLED */
2721
2722
2723/**
2724 * Interpret RDMSR
2725 *
2726 * @returns VBox status code.
2727 * @param pVM The VM handle.
2728 * @param pVCpu The VMCPU handle.
2729 * @param pRegFrame The register frame.
2730 */
2731VMMDECL(int) EMInterpretRdmsr(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2732{
2733 /** @todo According to the Intel manuals, there's a REX version of RDMSR that is slightly different.
2734 * That version clears the high dwords of both RDX & RAX */
2735
2736 /* Get the current privilege level. */
2737 if (CPUMGetGuestCPL(pVCpu, pRegFrame) != 0)
2738 return VERR_EM_INTERPRETER; /* supervisor only */
2739
2740 uint64_t uValue;
2741 int rc = CPUMQueryGuestMsr(pVCpu, pRegFrame->ecx, &uValue);
2742 if (RT_UNLIKELY(rc != VINF_SUCCESS))
2743 {
2744 Assert(rc == VERR_CPUM_RAISE_GP_0);
2745 return VERR_EM_INTERPRETER;
2746 }
2747 pRegFrame->rax = (uint32_t) uValue;
2748 pRegFrame->rdx = (uint32_t)(uValue >> 32);
2749 LogFlow(("EMInterpretRdmsr %s (%x) -> %RX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx, uValue));
2750 return rc;
2751}
2752
2753
2754/**
2755 * RDMSR Emulation.
2756 */
2757static int emInterpretRdmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2758{
2759 /* Note: The Intel manual claims there's a REX version of RDMSR that's slightly
2760 different, so we play safe by completely disassembling the instruction. */
2761 Assert(!(pDis->prefix & PREFIX_REX));
2762 return EMInterpretRdmsr(pVM, pVCpu, pRegFrame);
2763}
2764
2765
2766/**
2767 * Interpret WRMSR
2768 *
2769 * @returns VBox status code.
2770 * @param pVM The VM handle.
2771 * @param pVCpu The VMCPU handle.
2772 * @param pRegFrame The register frame.
2773 */
2774VMMDECL(int) EMInterpretWrmsr(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
2775{
2776 /* Check the current privilege level, this instruction is supervisor only. */
2777 if (CPUMGetGuestCPL(pVCpu, pRegFrame) != 0)
2778 return VERR_EM_INTERPRETER; /** @todo raise \#GP(0) */
2779
2780 int rc = CPUMSetGuestMsr(pVCpu, pRegFrame->ecx, RT_MAKE_U64(pRegFrame->eax, pRegFrame->edx));
2781 if (rc != VINF_SUCCESS)
2782 {
2783 Assert(rc == VERR_CPUM_RAISE_GP_0);
2784 return VERR_EM_INTERPRETER;
2785 }
2786 LogFlow(("EMInterpretWrmsr %s (%x) val=%RX64\n", emMSRtoString(pRegFrame->ecx), pRegFrame->ecx,
2787 RT_MAKE_U64(pRegFrame->eax, pRegFrame->edx)));
2788 return rc;
2789}
2790
2791
2792/**
2793 * WRMSR Emulation.
2794 */
2795static int emInterpretWrmsr(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize)
2796{
2797 return EMInterpretWrmsr(pVM, pVCpu, pRegFrame);
2798}
2799
2800
2801/**
2802 * Internal worker.
2803 * @copydoc EMInterpretInstructionCPU
2804 */
2805DECLINLINE(VBOXSTRICTRC) emInterpretInstructionCPU(PVM pVM, PVMCPU pVCpu, PDISCPUSTATE pDis, PCPUMCTXCORE pRegFrame,
2806 RTGCPTR pvFault, EMCODETYPE enmCodeType, uint32_t *pcbSize)
2807{
2808 Assert(enmCodeType == EMCODETYPE_SUPERVISOR || enmCodeType == EMCODETYPE_ALL);
2809 Assert(pcbSize);
2810 *pcbSize = 0;
2811
2812 if (enmCodeType == EMCODETYPE_SUPERVISOR)
2813 {
2814 /*
2815 * Only supervisor guest code!!
2816 * And no complicated prefixes.
2817 */
2818 /* Get the current privilege level. */
2819 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
2820 if ( cpl != 0
2821 && pDis->pCurInstr->opcode != OP_RDTSC) /* rdtsc requires emulation in ring 3 as well */
2822 {
2823 Log(("WARNING: refusing instruction emulation for user-mode code!!\n"));
2824 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedUserMode));
2825 return VERR_EM_INTERPRETER;
2826 }
2827 }
2828 else
2829 Log2(("emInterpretInstructionCPU allowed to interpret user-level code!!\n"));
2830
2831#ifdef IN_RC
2832 if ( (pDis->prefix & (PREFIX_REPNE | PREFIX_REP))
2833 || ( (pDis->prefix & PREFIX_LOCK)
2834 && pDis->pCurInstr->opcode != OP_CMPXCHG
2835 && pDis->pCurInstr->opcode != OP_CMPXCHG8B
2836 && pDis->pCurInstr->opcode != OP_XADD
2837 && pDis->pCurInstr->opcode != OP_OR
2838 && pDis->pCurInstr->opcode != OP_AND
2839 && pDis->pCurInstr->opcode != OP_XOR
2840 && pDis->pCurInstr->opcode != OP_BTR
2841 )
2842 )
2843#else
2844 if ( (pDis->prefix & PREFIX_REPNE)
2845 || ( (pDis->prefix & PREFIX_REP)
2846 && pDis->pCurInstr->opcode != OP_STOSWD
2847 )
2848 || ( (pDis->prefix & PREFIX_LOCK)
2849 && pDis->pCurInstr->opcode != OP_OR
2850 && pDis->pCurInstr->opcode != OP_AND
2851 && pDis->pCurInstr->opcode != OP_XOR
2852 && pDis->pCurInstr->opcode != OP_BTR
2853 && pDis->pCurInstr->opcode != OP_CMPXCHG
2854 && pDis->pCurInstr->opcode != OP_CMPXCHG8B
2855 )
2856 )
2857#endif
2858 {
2859 //Log(("EMInterpretInstruction: wrong prefix!!\n"));
2860 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedPrefix));
2861 return VERR_EM_INTERPRETER;
2862 }
2863
2864#if HC_ARCH_BITS == 32
2865 /*
2866 * Unable to emulate most >4 bytes accesses in 32 bits mode.
2867 * Whitelisted instructions are safe.
2868 */
2869 if ( pDis->param1.size > 4
2870 && CPUMIsGuestIn64BitCode(pVCpu, pRegFrame))
2871 {
2872 uint32_t uOpCode = pDis->pCurInstr->opcode;
2873 if ( uOpCode != OP_STOSWD
2874 && uOpCode != OP_MOV
2875 && uOpCode != OP_CMPXCHG8B
2876 && uOpCode != OP_XCHG
2877 && uOpCode != OP_BTS
2878 && uOpCode != OP_BTR
2879 && uOpCode != OP_BTC
2880# ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
2881 && uOpCode != OP_CMPXCHG /* solaris */
2882 && uOpCode != OP_AND /* windows */
2883 && uOpCode != OP_OR /* windows */
2884 && uOpCode != OP_XOR /* because we can */
2885 && uOpCode != OP_ADD /* windows (dripple) */
2886 && uOpCode != OP_ADC /* because we can */
2887 && uOpCode != OP_SUB /* because we can */
2888 /** @todo OP_BTS or is that a different kind of failure? */
2889# endif
2890 )
2891 {
2892# ifdef VBOX_WITH_STATISTICS
2893 switch (pDis->pCurInstr->opcode)
2894 {
2895# define INTERPRET_FAILED_CASE(opcode, Instr) \
2896 case opcode: STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); break;
2897 INTERPRET_FAILED_CASE(OP_XCHG,Xchg);
2898 INTERPRET_FAILED_CASE(OP_DEC,Dec);
2899 INTERPRET_FAILED_CASE(OP_INC,Inc);
2900 INTERPRET_FAILED_CASE(OP_POP,Pop);
2901 INTERPRET_FAILED_CASE(OP_OR, Or);
2902 INTERPRET_FAILED_CASE(OP_XOR,Xor);
2903 INTERPRET_FAILED_CASE(OP_AND,And);
2904 INTERPRET_FAILED_CASE(OP_MOV,Mov);
2905 INTERPRET_FAILED_CASE(OP_STOSWD,StosWD);
2906 INTERPRET_FAILED_CASE(OP_INVLPG,InvlPg);
2907 INTERPRET_FAILED_CASE(OP_CPUID,CpuId);
2908 INTERPRET_FAILED_CASE(OP_MOV_CR,MovCRx);
2909 INTERPRET_FAILED_CASE(OP_MOV_DR,MovDRx);
2910 INTERPRET_FAILED_CASE(OP_LLDT,LLdt);
2911 INTERPRET_FAILED_CASE(OP_LIDT,LIdt);
2912 INTERPRET_FAILED_CASE(OP_LGDT,LGdt);
2913 INTERPRET_FAILED_CASE(OP_LMSW,Lmsw);
2914 INTERPRET_FAILED_CASE(OP_CLTS,Clts);
2915 INTERPRET_FAILED_CASE(OP_MONITOR,Monitor);
2916 INTERPRET_FAILED_CASE(OP_MWAIT,MWait);
2917 INTERPRET_FAILED_CASE(OP_RDMSR,Rdmsr);
2918 INTERPRET_FAILED_CASE(OP_WRMSR,Wrmsr);
2919 INTERPRET_FAILED_CASE(OP_ADD,Add);
2920 INTERPRET_FAILED_CASE(OP_SUB,Sub);
2921 INTERPRET_FAILED_CASE(OP_ADC,Adc);
2922 INTERPRET_FAILED_CASE(OP_BTR,Btr);
2923 INTERPRET_FAILED_CASE(OP_BTS,Bts);
2924 INTERPRET_FAILED_CASE(OP_BTC,Btc);
2925 INTERPRET_FAILED_CASE(OP_RDTSC,Rdtsc);
2926 INTERPRET_FAILED_CASE(OP_CMPXCHG, CmpXchg);
2927 INTERPRET_FAILED_CASE(OP_STI, Sti);
2928 INTERPRET_FAILED_CASE(OP_XADD,XAdd);
2929 INTERPRET_FAILED_CASE(OP_CMPXCHG8B,CmpXchg8b);
2930 INTERPRET_FAILED_CASE(OP_HLT, Hlt);
2931 INTERPRET_FAILED_CASE(OP_IRET,Iret);
2932 INTERPRET_FAILED_CASE(OP_WBINVD,WbInvd);
2933 INTERPRET_FAILED_CASE(OP_MOVNTPS,MovNTPS);
2934# undef INTERPRET_FAILED_CASE
2935 default:
2936 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedMisc));
2937 break;
2938 }
2939# endif /* VBOX_WITH_STATISTICS */
2940 return VERR_EM_INTERPRETER;
2941 }
2942 }
2943#endif
2944
2945 VBOXSTRICTRC rc;
2946#if (defined(VBOX_STRICT) || defined(LOG_ENABLED))
2947 LogFlow(("emInterpretInstructionCPU %s\n", emGetMnemonic(pDis)));
2948#endif
2949 switch (pDis->pCurInstr->opcode)
2950 {
2951 /*
2952 * Macros for generating the right case statements.
2953 */
2954# define INTERPRET_CASE_EX_LOCK_PARAM3(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock) \
2955 case opcode:\
2956 if (pDis->prefix & PREFIX_LOCK) \
2957 rc = emInterpretLock##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulateLock); \
2958 else \
2959 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulate); \
2960 if (RT_SUCCESS(rc)) \
2961 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
2962 else \
2963 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
2964 return rc
2965#define INTERPRET_CASE_EX_PARAM3(opcode, Instr, InstrFn, pfnEmulate) \
2966 case opcode:\
2967 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize, pfnEmulate); \
2968 if (RT_SUCCESS(rc)) \
2969 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
2970 else \
2971 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
2972 return rc
2973
2974#define INTERPRET_CASE_EX_PARAM2(opcode, Instr, InstrFn, pfnEmulate) \
2975 INTERPRET_CASE_EX_PARAM3(opcode, Instr, InstrFn, pfnEmulate)
2976#define INTERPRET_CASE_EX_LOCK_PARAM2(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock) \
2977 INTERPRET_CASE_EX_LOCK_PARAM3(opcode, Instr, InstrFn, pfnEmulate, pfnEmulateLock)
2978
2979#define INTERPRET_CASE(opcode, Instr) \
2980 case opcode:\
2981 rc = emInterpret##Instr(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize); \
2982 if (RT_SUCCESS(rc)) \
2983 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
2984 else \
2985 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
2986 return rc
2987
2988#define INTERPRET_CASE_EX_DUAL_PARAM2(opcode, Instr, InstrFn) \
2989 case opcode:\
2990 rc = emInterpret##InstrFn(pVM, pVCpu, pDis, pRegFrame, pvFault, pcbSize); \
2991 if (RT_SUCCESS(rc)) \
2992 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Instr)); \
2993 else \
2994 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); \
2995 return rc
2996
2997#define INTERPRET_STAT_CASE(opcode, Instr) \
2998 case opcode: STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,Failed##Instr)); return VERR_EM_INTERPRETER;
2999
3000 /*
3001 * The actual case statements.
3002 */
3003 INTERPRET_CASE(OP_XCHG,Xchg);
3004 INTERPRET_CASE_EX_PARAM2(OP_DEC,Dec, IncDec, EMEmulateDec);
3005 INTERPRET_CASE_EX_PARAM2(OP_INC,Inc, IncDec, EMEmulateInc);
3006 INTERPRET_CASE(OP_POP,Pop);
3007 INTERPRET_CASE_EX_LOCK_PARAM3(OP_OR, Or, OrXorAnd, EMEmulateOr, EMEmulateLockOr);
3008 INTERPRET_CASE_EX_LOCK_PARAM3(OP_XOR,Xor, OrXorAnd, EMEmulateXor, EMEmulateLockXor);
3009 INTERPRET_CASE_EX_LOCK_PARAM3(OP_AND,And, OrXorAnd, EMEmulateAnd, EMEmulateLockAnd);
3010 INTERPRET_CASE(OP_MOV,Mov);
3011#ifndef IN_RC
3012 INTERPRET_CASE(OP_STOSWD,StosWD);
3013#endif
3014 INTERPRET_CASE(OP_INVLPG,InvlPg);
3015 INTERPRET_CASE(OP_CPUID,CpuId);
3016 INTERPRET_CASE(OP_MOV_CR,MovCRx);
3017 INTERPRET_CASE(OP_MOV_DR,MovDRx);
3018#ifdef IN_RING0
3019 INTERPRET_CASE_EX_DUAL_PARAM2(OP_LIDT, LIdt, LIGdt);
3020 INTERPRET_CASE_EX_DUAL_PARAM2(OP_LGDT, LGdt, LIGdt);
3021#endif
3022 INTERPRET_CASE(OP_LLDT,LLdt);
3023 INTERPRET_CASE(OP_LMSW,Lmsw);
3024#ifdef EM_EMULATE_SMSW
3025 INTERPRET_CASE(OP_SMSW,Smsw);
3026#endif
3027 INTERPRET_CASE(OP_CLTS,Clts);
3028 INTERPRET_CASE(OP_MONITOR, Monitor);
3029 INTERPRET_CASE(OP_MWAIT, MWait);
3030 INTERPRET_CASE(OP_RDMSR, Rdmsr);
3031 INTERPRET_CASE(OP_WRMSR, Wrmsr);
3032 INTERPRET_CASE_EX_PARAM3(OP_ADD,Add, AddSub, EMEmulateAdd);
3033 INTERPRET_CASE_EX_PARAM3(OP_SUB,Sub, AddSub, EMEmulateSub);
3034 INTERPRET_CASE(OP_ADC,Adc);
3035 INTERPRET_CASE_EX_LOCK_PARAM2(OP_BTR,Btr, BitTest, EMEmulateBtr, EMEmulateLockBtr);
3036 INTERPRET_CASE_EX_PARAM2(OP_BTS,Bts, BitTest, EMEmulateBts);
3037 INTERPRET_CASE_EX_PARAM2(OP_BTC,Btc, BitTest, EMEmulateBtc);
3038 INTERPRET_CASE(OP_RDPMC,Rdpmc);
3039 INTERPRET_CASE(OP_RDTSC,Rdtsc);
3040 INTERPRET_CASE(OP_CMPXCHG, CmpXchg);
3041#ifdef IN_RC
3042 INTERPRET_CASE(OP_STI,Sti);
3043 INTERPRET_CASE(OP_XADD, XAdd);
3044#endif
3045 INTERPRET_CASE(OP_CMPXCHG8B, CmpXchg8b);
3046 INTERPRET_CASE(OP_HLT,Hlt);
3047 INTERPRET_CASE(OP_IRET,Iret);
3048 INTERPRET_CASE(OP_WBINVD,WbInvd);
3049#ifdef VBOX_WITH_STATISTICS
3050# ifndef IN_RC
3051 INTERPRET_STAT_CASE(OP_XADD, XAdd);
3052# endif
3053 INTERPRET_STAT_CASE(OP_MOVNTPS,MovNTPS);
3054#endif
3055
3056 default:
3057 Log3(("emInterpretInstructionCPU: opcode=%d\n", pDis->pCurInstr->opcode));
3058 STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,FailedMisc));
3059 return VERR_EM_INTERPRETER;
3060
3061#undef INTERPRET_CASE_EX_PARAM2
3062#undef INTERPRET_STAT_CASE
3063#undef INTERPRET_CASE_EX
3064#undef INTERPRET_CASE
3065 } /* switch (opcode) */
3066 AssertFailed();
3067 return VERR_INTERNAL_ERROR;
3068}
3069
3070
3071/**
3072 * Sets the PC for which interrupts should be inhibited.
3073 *
3074 * @param pVCpu The VMCPU handle.
3075 * @param PC The PC.
3076 */
3077VMMDECL(void) EMSetInhibitInterruptsPC(PVMCPU pVCpu, RTGCUINTPTR PC)
3078{
3079 pVCpu->em.s.GCPtrInhibitInterrupts = PC;
3080 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
3081}
3082
3083
3084/**
3085 * Gets the PC for which interrupts should be inhibited.
3086 *
3087 * There are a few instructions which inhibits or delays interrupts
3088 * for the instruction following them. These instructions are:
3089 * - STI
3090 * - MOV SS, r/m16
3091 * - POP SS
3092 *
3093 * @returns The PC for which interrupts should be inhibited.
3094 * @param pVCpu The VMCPU handle.
3095 *
3096 */
3097VMMDECL(RTGCUINTPTR) EMGetInhibitInterruptsPC(PVMCPU pVCpu)
3098{
3099 return pVCpu->em.s.GCPtrInhibitInterrupts;
3100}
3101
3102/**
3103 * Locks REM execution to a single VCpu
3104 *
3105 * @param pVM VM handle.
3106 */
3107VMMDECL(void) EMRemLock(PVM pVM)
3108{
3109 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
3110 return; /* early init */
3111
3112 Assert(!PGMIsLockOwner(pVM) && !IOMIsLockOwner(pVM));
3113 int rc = PDMCritSectEnter(&pVM->em.s.CritSectREM, VERR_SEM_BUSY);
3114 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
3115}
3116
3117/**
3118 * Unlocks REM execution
3119 *
3120 * @param pVM VM handle.
3121 */
3122VMMDECL(void) EMRemUnlock(PVM pVM)
3123{
3124 if (!PDMCritSectIsInitialized(&pVM->em.s.CritSectREM))
3125 return; /* early init */
3126
3127 PDMCritSectLeave(&pVM->em.s.CritSectREM);
3128}
3129
3130/**
3131 * Check if this VCPU currently owns the REM lock.
3132 *
3133 * @returns bool owner/not owner
3134 * @param pVM The VM to operate on.
3135 */
3136VMMDECL(bool) EMRemIsLockOwner(PVM pVM)
3137{
3138 return PDMCritSectIsOwner(&pVM->em.s.CritSectREM);
3139}
3140
3141/**
3142 * Try to acquire the REM lock.
3143 *
3144 * @returns VBox status code
3145 * @param pVM The VM to operate on.
3146 */
3147VMMDECL(int) EMTryEnterRemLock(PVM pVM)
3148{
3149 return PDMCritSectTryEnter(&pVM->em.s.CritSectREM);
3150}
3151
3152/**
3153 * Determine if we should continue after encountering a hlt or mwait instruction
3154 *
3155 * @returns boolean
3156 * @param pVCpu The VMCPU to operate on.
3157 * @param pCtx Current CPU context
3158 */
3159VMMDECL(bool) EMShouldContinueAfterHalt(PVMCPU pVCpu, PCPUMCTX pCtx)
3160{
3161 if ( pCtx->eflags.Bits.u1IF
3162 || ((pVCpu->em.s.mwait.fWait & (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)) == (EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0)))
3163 {
3164 pVCpu->em.s.mwait.fWait &= ~(EMMWAIT_FLAG_ACTIVE | EMMWAIT_FLAG_BREAKIRQIF0);
3165 return !!VMCPU_FF_ISPENDING(pVCpu, (VMCPU_FF_INTERRUPT_APIC|VMCPU_FF_INTERRUPT_PIC));
3166 }
3167
3168 return false;
3169}
3170
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