VirtualBox

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

Last change on this file since 13565 was 13565, checked in by vboxsync, 16 years ago

#1865: REM (VMM bits) - moved EMFlushREMTBs to REMFlushTBs, deleted dead REMGC.cpp.

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