VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMGC/TRPMGCHandlers.cpp@ 2009

Last change on this file since 2009 was 2003, checked in by vboxsync, 18 years ago

Emulate monitor in ring 0.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 40.0 KB
Line 
1/* $Id: TRPMGCHandlers.cpp 2003 2007-04-10 09:30:52Z vboxsync $ */
2/** @file
3 * TRPM - Guest Context Trap Handlers, CPP part
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_TRPM
27#include <VBox/selm.h>
28#include <VBox/iom.h>
29#include <VBox/pgm.h>
30#include <VBox/pdm.h>
31#include <VBox/dbgf.h>
32#include <VBox/em.h>
33#include <VBox/csam.h>
34#include <VBox/patm.h>
35#include <VBox/mm.h>
36#include <VBox/cpum.h>
37#include "TRPMInternal.h"
38#include <VBox/vm.h>
39#include <VBox/param.h>
40
41#include <VBox/err.h>
42#include <VBox/dis.h>
43#include <VBox/disopcode.h>
44#include <VBox/x86.h>
45#include <VBox/log.h>
46#include <VBox/tm.h>
47#include <iprt/asm.h>
48#include <iprt/assert.h>
49
50/* still here. MODR/M byte parsing */
51#define X86_OPCODE_MODRM_MOD_MASK 0xc0
52#define X86_OPCODE_MODRM_REG_MASK 0x38
53#define X86_OPCODE_MODRM_RM_MASK 0x07
54
55/** Pointer to a readonly hypervisor trap record. */
56typedef const struct TRPMGCHYPER *PCTRPMGCHYPER;
57
58/**
59 * A hypervisor trap record.
60 * This contains information about a handler for a instruction range.
61 *
62 * @remark This must match what TRPM_HANDLER outputs.
63 */
64typedef struct TRPMGCHYPER
65{
66 /** The start address. */
67 uintptr_t uStartEIP;
68 /** The end address. (exclusive)
69 * If NULL the it's only for the instruction at pvStartEIP. */
70 uintptr_t uEndEIP;
71 /**
72 * The handler.
73 *
74 * @returns VBox status code
75 * VINF_SUCCESS means we've handled the trap.
76 * Any other error code means returning to the host context.
77 * @param pVM The VM handle.
78 * @param pRegFrame The register frame.
79 * @param uUser The user argument.
80 */
81 DECLCALLBACKMEMBER(int, pfnHandler)(PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser);
82 /** Whatever the handler desires to put here. */
83 uintptr_t uUser;
84} TRPMGCHYPER;
85
86
87/*******************************************************************************
88* Global Variables *
89*******************************************************************************/
90__BEGIN_DECLS
91/** Defined in VMMGC0.asm or VMMGC99.asm.
92 * @{ */
93extern const TRPMGCHYPER g_aTrap0bHandlers[1];
94extern const TRPMGCHYPER g_aTrap0bHandlersEnd[1];
95extern const TRPMGCHYPER g_aTrap0dHandlers[1];
96extern const TRPMGCHYPER g_aTrap0dHandlersEnd[1];
97extern const TRPMGCHYPER g_aTrap0eHandlers[1];
98extern const TRPMGCHYPER g_aTrap0eHandlersEnd[1];
99/** @} */
100__END_DECLS
101
102
103/*******************************************************************************
104* Internal Functions *
105*******************************************************************************/
106__BEGIN_DECLS /* addressed from asm (not called so no DECLASM). */
107DECLCALLBACK(int) trpmGCTrapInGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser);
108__END_DECLS
109
110
111
112/**
113 * Exits the trap, called when exiting a trap handler.
114 *
115 * Will reset the trap if it's not a guest trap or the trap
116 * is already handled. Will process resume guest FFs.
117 *
118 * @returns rc.
119 * @param pVM VM handle.
120 * @param rc The VBox status code to return.
121 * @param pRegFrame Pointer to the register frame for the trap.
122 */
123static int trpmGCExitTrap(PVM pVM, int rc, PCPUMCTXCORE pRegFrame)
124{
125 uint32_t uOldActiveVector = pVM->trpm.s.uActiveVector;
126 NOREF(uOldActiveVector);
127
128 /* Reset trap? */
129 if ( rc != VINF_EM_RAW_GUEST_TRAP
130 && rc != VINF_EM_RAW_RING_SWITCH_INT)
131 pVM->trpm.s.uActiveVector = ~0;
132
133#ifdef VBOX_HIGH_RES_TIMERS_HACK
134 /*
135 * Occationally we should poll timers.
136 * We must *NOT* do this too frequently as it adds a significant overhead
137 * and it'll kill us if the trap load is high. (See #1354.)
138 * (The heuristic is not very intelligent, we should really check trap
139 * frequency etc. here, but alas, we lack any such information atm.)
140 */
141 static unsigned s_iTimerPoll = 0;
142 if (rc == VINF_SUCCESS)
143 {
144 if (!(++s_iTimerPoll & 0xf))
145 {
146 uint64_t cTicks = TMTimerPoll(pVM); NOREF(cTicks);
147 Log2(("TMTimerPoll at %VGv returned %RX64 (VM_FF_TIMER=%d)\n", pRegFrame->eip, cTicks, VM_FF_ISPENDING(pVM, VM_FF_TIMER)));
148 }
149 }
150 else
151 s_iTimerPoll = 0;
152#endif
153
154 /* Clear pending inhibit interrupt state if required. (necessary for dispatching interrupts later on) */
155 if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
156 {
157 Log2(("VM_FF_INHIBIT_INTERRUPTS at %VGv successor %VGv\n", pRegFrame->eip, EMGetInhibitInterruptsPC(pVM)));
158 if (pRegFrame->eip != EMGetInhibitInterruptsPC(pVM))
159 {
160 /** @note we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here if the eip is the same as the inhibited instr address.
161 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
162 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
163 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
164 */
165 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
166 }
167 }
168
169 /*
170 * Pending resume-guest-FF?
171 * Or pending (A)PIC interrupt? Windows XP will crash if we delay APIC interrupts.
172 */
173 if ( rc == VINF_SUCCESS
174 && VM_FF_ISPENDING(pVM, VM_FF_TO_R3 | VM_FF_TIMER | VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC | VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL | VM_FF_REQUEST))
175 {
176 /* Pending Ring-3 action. */
177 if (VM_FF_ISPENDING(pVM, VM_FF_TO_R3))
178 {
179 VM_FF_CLEAR(pVM, VM_FF_TO_R3);
180 rc = VINF_EM_RAW_TO_R3;
181 }
182 /* Pending timer action. */
183 else if (VM_FF_ISPENDING(pVM, VM_FF_TIMER))
184 rc = VINF_EM_RAW_TIMER_PENDING;
185 /* Pending interrupt: dispatch it. */
186 else if ( VM_FF_ISPENDING(pVM, VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC)
187 && !VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS)
188 && PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame)
189 )
190 {
191 uint8_t u8Interrupt;
192 rc = PDMGetInterrupt(pVM, &u8Interrupt);
193 Log(("trpmGCExitTrap: u8Interrupt=%d (%#x) rc=%Vrc\n", u8Interrupt, u8Interrupt, rc));
194 AssertFatalMsgRC(rc, ("PDMGetInterrupt failed with %Vrc\n", rc));
195 rc = TRPMForwardTrap(pVM, pRegFrame, (uint32_t)u8Interrupt, 0, TRPM_TRAP_NO_ERRORCODE, TRPM_HARDWARE_INT);
196 /* can't return if successful */
197 Assert(rc != VINF_SUCCESS);
198
199 /* Stop the profile counter that was started in TRPMGCHandlersA.asm */
200 Assert(uOldActiveVector <= 16);
201 STAM_PROFILE_ADV_STOP(&pVM->trpm.s.aStatGCTraps[uOldActiveVector], a);
202
203 /* Assert the trap and go to the recompiler to dispatch it. */
204 TRPMAssertTrap(pVM, u8Interrupt, false);
205
206 STAM_PROFILE_ADV_START(&pVM->trpm.s.aStatGCTraps[uOldActiveVector], a);
207 rc = VINF_EM_RAW_INTERRUPT_PENDING;
208 }
209 /*
210 * Try sync CR3?
211 * This ASSUMES that the MOV CRx, x emulation doesn't return with VINF_PGM_SYNC_CR3. (a bit hackish)
212 */
213 else if (VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
214#if 1
215 rc = PGMSyncCR3(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
216#else
217 rc = VINF_PGM_SYNC_CR3;
218#endif
219 /* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
220 else if (VM_FF_ISPENDING(pVM, VM_FF_REQUEST))
221 rc = VINF_EM_PENDING_REQUEST;
222 }
223
224 AssertMsg( rc != VINF_SUCCESS
225 || ( pRegFrame->eflags.Bits.u1IF
226 && ( pRegFrame->eflags.Bits.u2IOPL < (unsigned)(pRegFrame->ss & X86_SEL_RPL) || pRegFrame->eflags.Bits.u1VM))
227 , ("rc = %VGv\neflags=%RX32 ss=%RTsel IOPL=%d\n", rc, pRegFrame->eflags.u32, pRegFrame->ss, pRegFrame->eflags.Bits.u2IOPL));
228 return rc;
229}
230
231
232/**
233 * \#DB (Debug event) handler.
234 *
235 * @returns VBox status code.
236 * VINF_SUCCESS means we completely handled this trap,
237 * other codes are passed execution to host context.
238 *
239 * @param pTrpm Pointer to TRPM data (within VM).
240 * @param pRegFrame Pointer to the register frame for the trap.
241 * @internal
242 */
243DECLASM(int) TRPMGCTrap01Handler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
244{
245 RTGCUINTREG uDr6 = ASMGetAndClearDR6();
246 PVM pVM = TRPM2VM(pTrpm);
247 LogFlow(("TRPMGCTrap01Handler: cs:eip=%04x:%08x uDr6=%RTreg\n", pRegFrame->cs, pRegFrame->eip, uDr6));
248
249 /*
250 * We currently don't make sure of the X86_DR7_GD bit, but
251 * there might come a time when we do.
252 */
253 if ((uDr6 & X86_DR6_BD) == X86_DR6_BD)
254 {
255 AssertReleaseMsgFailed(("X86_DR6_BD isn't used, but it's set! dr7=%RTreg(%RTreg) dr6=%RTreg\n",
256 ASMGetDR7(), CPUMGetHyperDR7(pVM), uDr6));
257 return VERR_NOT_IMPLEMENTED;
258 }
259
260 AssertReleaseMsg(!(uDr6 & X86_DR6_BT), ("X86_DR6_BT is impossible!\n"));
261
262 /*
263 * Now leave the rest to the DBGF.
264 */
265 int rc = DBGFGCTrap01Handler(pVM, pRegFrame, uDr6);
266 if (rc == VINF_EM_RAW_GUEST_TRAP)
267 CPUMSetGuestDR6(pVM, uDr6);
268
269 return trpmGCExitTrap(pVM, rc, pRegFrame);
270}
271
272
273
274/**
275 * NMI handler, for when we are using NMIs to debug things.
276 *
277 * @returns VBox status code.
278 * VINF_SUCCESS means we completely handled this trap,
279 * other codes are passed execution to host context.
280 *
281 * @param pTrpm Pointer to TRPM data (within VM).
282 * @param pRegFrame Pointer to the register frame for the trap.
283 * @internal
284 * @remark This is not hooked up unless you're building with VBOX_WITH_NMI defined.
285 */
286DECLASM(int) TRPMGCTrap02Handler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
287{
288 LogFlow(("TRPMGCTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs, pRegFrame->eip));
289 RTLogComPrintf("TRPMGCTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs, pRegFrame->eip);
290 return VERR_TRPM_DONT_PANIC;
291}
292
293
294/**
295 * \#BP (Breakpoint) handler.
296 *
297 * @returns VBox status code.
298 * VINF_SUCCESS means we completely handled this trap,
299 * other codes are passed execution to host context.
300 *
301 * @param pTrpm Pointer to TRPM data (within VM).
302 * @param pRegFrame Pointer to the register frame for the trap.
303 * @internal
304 */
305DECLASM(int) TRPMGCTrap03Handler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
306{
307 LogFlow(("TRPMGCTrap03Handler: cs:eip=%04x:%08x\n", pRegFrame->cs, pRegFrame->eip));
308 PVM pVM = TRPM2VM(pTrpm);
309 int rc;
310
311 /*
312 * Both PATM are using INT3s, let them have a go first.
313 */
314 if ( (pRegFrame->ss & X86_SEL_RPL) == 1
315 && !pRegFrame->eflags.Bits.u1VM)
316 {
317 rc = PATMHandleInt3PatchTrap(pVM, pRegFrame);
318 if (rc == VINF_SUCCESS || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_PATM_PATCH_INT3 || rc == VINF_PATM_DUPLICATE_FUNCTION)
319 return trpmGCExitTrap(pVM, rc, pRegFrame);
320 }
321 rc = DBGFGCTrap03Handler(pVM, pRegFrame);
322 /* anything we should do with this? Schedule it in GC? */
323 return trpmGCExitTrap(pVM, rc, pRegFrame);
324}
325
326
327/**
328 * Trap handler for illegal opcode fault (\#UD).
329 *
330 * @returns VBox status code.
331 * VINF_SUCCESS means we completely handled this trap,
332 * other codes are passed execution to host context.
333 *
334 * @param pTrpm Pointer to TRPM data (within VM).
335 * @param pRegFrame Pointer to the register frame for the trap.
336 * @internal
337 */
338DECLASM(int) TRPMGCTrap06Handler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
339{
340 PVM pVM = TRPM2VM(pTrpm);
341 int rc;
342
343 LogFlow(("TRPMGCTrap06Handler %VGv eflags=%x\n", pRegFrame->eip, pRegFrame->eflags.u32));
344
345 if (CPUMGetGuestCPL(pVM, pRegFrame) == 0)
346 {
347 /*
348 * Decode the instruction.
349 */
350 RTGCPTR PC;
351 rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &PC);
352 if (VBOX_FAILURE(rc))
353 {
354 Log(("TRPMGCTrap06Handler: Failed to convert %RTsel:%RX32 (cpl=%d) - rc=%Vrc !!\n", pRegFrame->cs, pRegFrame->eip, pRegFrame->ss & X86_SEL_RPL, rc));
355 return trpmGCExitTrap(pVM, VINF_EM_RAW_GUEST_TRAP, pRegFrame);
356 }
357
358 DISCPUSTATE Cpu;
359 uint32_t cbOp;
360 rc = EMInterpretDisasOneEx(pVM, (RTGCUINTPTR)PC, pRegFrame, &Cpu, &cbOp);
361 if (VBOX_FAILURE(rc))
362 return trpmGCExitTrap(pVM, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
363
364 if ( PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip)
365 && Cpu.pCurInstr->opcode == OP_ILLUD2)
366 {
367 rc = PATMGCHandleIllegalInstrTrap(pVM, pRegFrame);
368 if (rc == VINF_SUCCESS || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_PATM_DUPLICATE_FUNCTION || rc == VINF_PATM_PENDING_IRQ_AFTER_IRET || rc == VINF_EM_RESCHEDULE)
369 return trpmGCExitTrap(pVM, rc, pRegFrame);
370 }
371 else
372 /** Note: monitor causes an #UD exception instead of #GP when not executed in ring 0. */
373 if (Cpu.pCurInstr->opcode == OP_MONITOR)
374 {
375 uint32_t cbIgnored;
376 rc = EMInterpretInstructionCPU(pVM, &Cpu, pRegFrame, PC, &cbIgnored);
377 }
378 else
379 /* Never generate a raw trap here; it might be an instruction, that requires emulation. */
380 rc = VINF_EM_RAW_EMULATE_INSTR;
381 }
382 else
383 if (pRegFrame->eflags.Bits.u1VM)
384 {
385 rc = TRPMForwardTrap(pVM, pRegFrame, 0x6, 0, TRPM_TRAP_NO_ERRORCODE, TRPM_TRAP);
386 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
387 }
388 else
389 /* Never generate a raw trap here; it might be an instruction, that requires emulation. */
390 rc = VINF_EM_RAW_EMULATE_INSTR;
391
392 return trpmGCExitTrap(pVM, rc, pRegFrame);
393}
394
395
396/**
397 * Trap handler for device not present fault (\#NM).
398 *
399 * Device not available, FP or (F)WAIT instruction.
400 *
401 * @returns VBox status code.
402 * VINF_SUCCESS means we completely handled this trap,
403 * other codes are passed execution to host context.
404 *
405 * @param pTrpm Pointer to TRPM data (within VM).
406 * @param pRegFrame Pointer to the register frame for the trap.
407 * @internal
408 */
409DECLASM(int) TRPMGCTrap07Handler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
410{
411 PVM pVM = TRPM2VM(pTrpm);
412
413 LogFlow(("TRPMTrap07HandlerGC: eip=%VGv\n", pRegFrame->eip));
414 return CPUMHandleLazyFPU(pVM);
415}
416
417
418/**
419 * \#NP ((segment) Not Present) handler.
420 *
421 * @returns VBox status code.
422 * VINF_SUCCESS means we completely handled this trap,
423 * other codes are passed execution to host context.
424 *
425 * @param pTrpm Pointer to TRPM data (within VM).
426 * @param pRegFrame Pointer to the register frame for the trap.
427 * @internal
428 */
429DECLASM(int) TRPMGCTrap0bHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
430{
431 LogFlow(("TRPMGCTrap0bHandler: eip=%VGv\n", pRegFrame->eip));
432 PVM pVM = TRPM2VM(pTrpm);
433
434 /*
435 * Try to detect instruction by opcode which caused trap.
436 * XXX note: this code may cause \#PF (trap e) or \#GP (trap d) while
437 * accessing user code. need to handle it somehow in future!
438 */
439 uint8_t *pu8Code;
440 if (SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, (PRTGCPTR)&pu8Code) == VINF_SUCCESS)
441 {
442 /*
443 * First skip possible instruction prefixes, such as:
444 * OS, AS
445 * CS:, DS:, ES:, SS:, FS:, GS:
446 * REPE, REPNE
447 *
448 * note: Currently we supports only up to 4 prefixes per opcode, more
449 * prefixes (normally not used anyway) will cause trap d in guest.
450 * note: Instruction length in IA-32 may be up to 15 bytes, we dont
451 * check this issue, its too hard.
452 */
453 for (unsigned i = 0; i < 4; i++)
454 {
455 if ( pu8Code[0] != 0xf2 /* REPNE/REPNZ */
456 && pu8Code[0] != 0xf3 /* REP/REPE/REPZ */
457 && pu8Code[0] != 0x2e /* CS: */
458 && pu8Code[0] != 0x36 /* SS: */
459 && pu8Code[0] != 0x3e /* DS: */
460 && pu8Code[0] != 0x26 /* ES: */
461 && pu8Code[0] != 0x64 /* FS: */
462 && pu8Code[0] != 0x65 /* GS: */
463 && pu8Code[0] != 0x66 /* OS */
464 && pu8Code[0] != 0x67 /* AS */
465 )
466 break;
467 pu8Code++;
468 }
469
470 /*
471 * Detect right switch using a callgate.
472 *
473 * We recognize the following causes for the trap 0b:
474 * CALL FAR, CALL FAR []
475 * JMP FAR, JMP FAR []
476 * IRET (may cause a task switch)
477 *
478 * Note: we can't detect whether the trap was caused by a call to a
479 * callgate descriptor or it is a real trap 0b due to a bad selector.
480 * In both situations we'll pass execution to our recompiler so we don't
481 * have to worry.
482 * If we wanted to do better detection, we have set GDT entries to callgate
483 * descriptors pointing to our own handlers.
484 */
485 /** @todo not sure about IRET, may generate Trap 0d (\#GP), NEED TO CHECK! */
486 if ( pu8Code[0] == 0x9a /* CALL FAR */
487 || ( pu8Code[0] == 0xff /* CALL FAR [] */
488 && (pu8Code[1] & X86_OPCODE_MODRM_REG_MASK) == 0x18)
489 || pu8Code[0] == 0xea /* JMP FAR */
490 || ( pu8Code[0] == 0xff /* JMP FAR [] */
491 && (pu8Code[1] & X86_OPCODE_MODRM_REG_MASK) == 0x28)
492 || pu8Code[0] == 0xcf /* IRET */
493 )
494 {
495 /*
496 * Got potential call to callgate.
497 * We simply return execution to the recompiler to do emulation
498 * starting from the instruction which caused the trap.
499 */
500 pTrpm->uActiveVector = ~0;
501 return VINF_EM_RAW_RING_SWITCH;
502 }
503 }
504
505 /*
506 * Pass trap 0b as is to the recompiler in all other cases.
507 */
508 return VINF_EM_RAW_GUEST_TRAP;
509}
510
511
512/**
513 * \#GP (General Protection Fault) handler for Ring-0 privileged instructions.
514 *
515 * @returns VBox status code.
516 * VINF_SUCCESS means we completely handled this trap,
517 * other codes are passed execution to host context.
518 *
519 * @param pVM The VM handle.
520 * @param pRegFrame Pointer to the register frame for the trap.
521 * @param pCpu The opcode info.
522 * @param PC The program counter corresponding to cs:eip in pRegFrame.
523 */
524static int trpmGCTrap0dHandlerRing0(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR PC)
525{
526 int rc;
527
528 /*
529 * Try handle it here, if not return to HC and emulate/interpret it there.
530 */
531 switch (pCpu->pCurInstr->opcode)
532 {
533 case OP_INT3:
534 /*
535 * Little hack to make the code below not fail
536 */
537 pCpu->param1.flags = USE_IMMEDIATE8;
538 pCpu->param1.parval = 3;
539 /* fallthru */
540 case OP_INT:
541 {
542 Assert(pCpu->param1.flags & USE_IMMEDIATE8);
543 Assert(!(PATMIsPatchGCAddr(pVM, PC)));
544 if (pCpu->param1.parval == 3)
545 {
546 /* Obsolete!! */
547 /* Int 3 replacement patch? */
548 if (PATMHandleInt3PatchTrap(pVM, pRegFrame) == VINF_SUCCESS)
549 {
550 AssertFailed();
551 return trpmGCExitTrap(pVM, VINF_SUCCESS, pRegFrame);
552 }
553 }
554 rc = TRPMForwardTrap(pVM, pRegFrame, (uint32_t)pCpu->param1.parval, pCpu->opsize, TRPM_TRAP_NO_ERRORCODE, TRPM_SOFTWARE_INT);
555 if (VBOX_SUCCESS(rc) && rc != VINF_EM_RAW_GUEST_TRAP)
556 return trpmGCExitTrap(pVM, VINF_SUCCESS, pRegFrame);
557
558 pVM->trpm.s.uActiveVector = (pVM->trpm.s.uActiveErrorCode & X86_TRAP_ERR_SEL_MASK) >> X86_TRAP_ERR_SEL_SHIFT;
559 pVM->trpm.s.fActiveSoftwareInterrupt = true;
560 return trpmGCExitTrap(pVM, VINF_EM_RAW_RING_SWITCH_INT, pRegFrame);
561 }
562
563#ifdef PATM_EMULATE_SYSENTER
564 case OP_SYSEXIT:
565 case OP_SYSRET:
566 rc = PATMSysCall(pVM, pRegFrame, pCpu);
567 return trpmGCExitTrap(pVM, rc, pRegFrame);
568#endif
569
570 case OP_HLT:
571 /* If it's in patch code, defer to ring-3. */
572 if (PATMIsPatchGCAddr(pVM, PC))
573 break;
574
575 pRegFrame->eip += pCpu->opsize;
576 return trpmGCExitTrap(pVM, VINF_EM_HALT, pRegFrame);
577
578
579 /*
580 * These instructions are used by PATM and CASM for finding
581 * dangerous non-trapping instructions. Thus, since all
582 * scanning and patching is done in ring-3 we'll have to
583 * return to ring-3 on the first encounter of these instructions.
584 */
585 case OP_MOV_CR:
586 case OP_MOV_DR:
587 /* We can safely emulate control/debug register move instructions in patched code. */
588 if ( !PATMIsPatchGCAddr(pVM, PC)
589 && !CSAMIsKnownDangerousInstr(pVM, PC))
590 break;
591 case OP_INVLPG:
592 case OP_LLDT:
593 case OP_STI:
594 case OP_RDTSC:
595 {
596 uint32_t cbIgnored;
597 rc = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, PC, &cbIgnored);
598 if (VBOX_SUCCESS(rc))
599 pRegFrame->eip += pCpu->opsize;
600 else if (rc == VERR_EM_INTERPRETER)
601 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
602 return trpmGCExitTrap(pVM, rc, pRegFrame);
603 }
604 }
605
606 return trpmGCExitTrap(pVM, VINF_EM_RAW_EXCEPTION_PRIVILEGED, pRegFrame);
607}
608
609
610/**
611 * \#GP (General Protection Fault) handler for Ring-3.
612 *
613 * @returns VBox status code.
614 * VINF_SUCCESS means we completely handled this trap,
615 * other codes are passed execution to host context.
616 *
617 * @param pVM The VM handle.
618 * @param pRegFrame Pointer to the register frame for the trap.
619 * @param pCpu The opcode info.
620 * @param PC The program counter corresponding to cs:eip in pRegFrame.
621 */
622static int trpmGCTrap0dHandlerRing3(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR PC)
623{
624 int rc;
625
626 Assert(!pRegFrame->eflags.Bits.u1VM);
627
628 switch (pCpu->pCurInstr->opcode)
629 {
630 /*
631 * STI and CLI are I/O privileged, i.e. if IOPL
632 */
633 case OP_STI:
634 case OP_CLI:
635 {
636 uint32_t efl = CPUMRawGetEFlags(pVM, pRegFrame);
637 if (X86_EFL_GET_IOPL(efl) >= (unsigned)(pRegFrame->ss & X86_SEL_RPL))
638 {
639 LogFlow(("trpmGCTrap0dHandlerRing3: CLI/STI -> REM\n"));
640 return trpmGCExitTrap(pVM, VINF_EM_RESCHEDULE_REM, pRegFrame);
641 }
642 LogFlow(("trpmGCTrap0dHandlerRing3: CLI/STI -> #GP(0)\n"));
643 break;
644 }
645
646 /*
647 * INT3 and INT xx are ring-switching.
648 * (The shadow IDT will have set the entries to DPL=0, that's why we're here.)
649 */
650 case OP_INT3:
651 /*
652 * Little hack to make the code below not fail
653 */
654 pCpu->param1.flags = USE_IMMEDIATE8;
655 pCpu->param1.parval = 3;
656 /* fall thru */
657 case OP_INT:
658 {
659 Assert(pCpu->param1.flags & USE_IMMEDIATE8);
660 rc = TRPMForwardTrap(pVM, pRegFrame, (uint32_t)pCpu->param1.parval, pCpu->opsize, TRPM_TRAP_NO_ERRORCODE, TRPM_SOFTWARE_INT);
661 if (VBOX_SUCCESS(rc) && rc != VINF_EM_RAW_GUEST_TRAP)
662 return trpmGCExitTrap(pVM, VINF_SUCCESS, pRegFrame);
663
664 pVM->trpm.s.uActiveVector = (pVM->trpm.s.uActiveErrorCode & X86_TRAP_ERR_SEL_MASK) >> X86_TRAP_ERR_SEL_SHIFT;
665 pVM->trpm.s.fActiveSoftwareInterrupt = true;
666 return trpmGCExitTrap(pVM, VINF_EM_RAW_RING_SWITCH_INT, pRegFrame);
667 }
668
669 /*
670 * SYSCALL, SYSENTER, INTO and BOUND are also ring-switchers.
671 */
672 case OP_SYSCALL:
673 case OP_SYSENTER:
674#ifdef PATM_EMULATE_SYSENTER
675 rc = PATMSysCall(pVM, pRegFrame, pCpu);
676 if (rc == VINF_SUCCESS)
677 return trpmGCExitTrap(pVM, VINF_SUCCESS, pRegFrame);
678 /* else no break; */
679#endif
680 case OP_BOUND:
681 case OP_INTO:
682 pVM->trpm.s.uActiveVector = ~0;
683 return trpmGCExitTrap(pVM, VINF_EM_RAW_RING_SWITCH, pRegFrame);
684
685 /*
686 * Handle virtualized TSC reads.
687 * (EMInterpretInstructionCPU isn't usable here because it rejects cpl != 0.)
688 */
689 case OP_RDTSC:
690 {
691 /** @todo statistics */
692 if (CPUMGetGuestCR4(pVM) & X86_CR4_TSD)
693 rc = VINF_EM_RAW_EMULATE_INSTR;
694 else
695 {
696 uint64_t Tsc = TMCpuTickGet(pVM);
697 pRegFrame->eax = Tsc;
698 pRegFrame->edx = Tsc >> 32;
699 pRegFrame->eip += pCpu->opsize;
700 rc = VINF_SUCCESS;
701 }
702 return trpmGCExitTrap(pVM, rc, pRegFrame);
703 }
704 }
705
706 /*
707 * A genuine guest fault.
708 */
709 return trpmGCExitTrap(pVM, VINF_EM_RAW_GUEST_TRAP, pRegFrame);
710}
711
712
713/**
714 * \#GP (General Protection Fault) handler.
715 *
716 * @returns VBox status code.
717 * VINF_SUCCESS means we completely handled this trap,
718 * other codes are passed execution to host context.
719 *
720 * @param pVM The VM handle.
721 * @param pTrpm Pointer to TRPM data (within VM).
722 * @param pRegFrame Pointer to the register frame for the trap.
723 */
724static int trpmGCTrap0dHandler(PVM pVM, PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
725{
726 LogFlow(("trpmGCTrap0dHandler: cs:eip=%RTsel:%VGv uErr=%RX32\n", pRegFrame->ss, pRegFrame->eip, pTrpm->uActiveErrorCode));
727
728#if 0 /* not right for iret. Shouldn't really be needed as SELMValidateAndConvertCSAddr deals with invalid cs. */
729 /*
730 * Filter out selector problems first as these may mean that the
731 * instruction isn't safe to read. If we're here because CS is NIL
732 * the flattening of cs:eip will deal with that.
733 */
734 if ( !(pTrpm->uActiveErrorCode & (X86_TRAP_ERR_IDT | X86_TRAP_ERR_EXTERNAL))
735 && (pTrpm->uActiveErrorCode & X86_TRAP_ERR_SEL_MASK))
736 {
737 /* It's a guest trap. */
738 return trpmGCExitTrap(pVM, VINF_EM_RAW_GUEST_TRAP, pRegFrame);
739 }
740#endif
741
742 STAM_PROFILE_ADV_START(&pVM->trpm.s.StatTrap0dDisasm, a);
743 /*
744 * Decode the instruction.
745 */
746 RTGCPTR PC;
747 int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &PC);
748 if (VBOX_FAILURE(rc))
749 {
750 Log(("trpmGCTrap0dHandler: Failed to convert %RTsel:%RX32 (cpl=%d) - rc=%Vrc !!\n",
751 pRegFrame->cs, pRegFrame->eip, pRegFrame->ss & X86_SEL_RPL, rc));
752 STAM_PROFILE_ADV_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
753 return trpmGCExitTrap(pVM, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
754 }
755
756 DISCPUSTATE Cpu;
757 uint32_t cbOp;
758 rc = EMInterpretDisasOneEx(pVM, (RTGCUINTPTR)PC, pRegFrame, &Cpu, &cbOp);
759 if (VBOX_FAILURE(rc))
760 {
761 STAM_PROFILE_ADV_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
762 return trpmGCExitTrap(pVM, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
763 }
764 STAM_PROFILE_ADV_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
765
766 /*
767 * Deal with I/O port access.
768 */
769 if ( pVM->trpm.s.uActiveErrorCode == 0
770 && (Cpu.pCurInstr->optype & OPTYPE_PORTIO))
771 {
772 rc = EMInterpretPortIO(pVM, pRegFrame, &Cpu, cbOp);
773 return trpmGCExitTrap(pVM, rc, pRegFrame);
774 }
775
776
777 /*
778 * Deal with Ring-0 (privileged instructions)
779 */
780 if ( (pRegFrame->ss & X86_SEL_RPL) <= 1
781 && !pRegFrame->eflags.Bits.u1VM)
782 return trpmGCTrap0dHandlerRing0(pVM, pRegFrame, &Cpu, PC);
783
784 /*
785 * Deal with Ring-3 GPs.
786 */
787 if (!pRegFrame->eflags.Bits.u1VM)
788 return trpmGCTrap0dHandlerRing3(pVM, pRegFrame, &Cpu, PC);
789
790 /*
791 * Deal with v86 code.
792 */
793
794 /* We always set IOPL to zero which makes e.g. pushf fault in V86 mode. The guest might use IOPL=3 and therefor not expect a #GP.
795 * Simply fall back to the recompiler to emulate this instruction.
796 */
797 /* Retrieve the eflags including the virtualized bits. */
798 /** @note hackish as the cpumctxcore structure doesn't contain the right value */
799 X86EFLAGS eflags;
800 eflags.u32 = CPUMRawGetEFlags(pVM, pRegFrame);
801 if (eflags.Bits.u2IOPL != 3)
802 {
803 Assert(eflags.Bits.u2IOPL == 0);
804
805 int rc = TRPMForwardTrap(pVM, pRegFrame, 0xD, 0, TRPM_TRAP_HAS_ERRORCODE, TRPM_TRAP);
806 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
807 return trpmGCExitTrap(pVM, rc, pRegFrame);
808 }
809 return trpmGCExitTrap(pVM, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
810}
811
812
813/**
814 * \#GP (General Protection Fault) handler.
815 *
816 * @returns VBox status code.
817 * VINF_SUCCESS means we completely handled this trap,
818 * other codes are passed execution to host context.
819 *
820 * @param pTrpm Pointer to TRPM data (within VM).
821 * @param pRegFrame Pointer to the register frame for the trap.
822 * @internal
823 */
824DECLASM(int) TRPMGCTrap0dHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
825{
826 LogFlow(("TRPMGCTrap0dHandler: eip=%RGv\n", pRegFrame->eip));
827 PVM pVM = TRPM2VM(pTrpm);
828
829 int rc = trpmGCTrap0dHandler(pVM, pTrpm, pRegFrame);
830 switch (rc)
831 {
832 case VINF_EM_RAW_GUEST_TRAP:
833 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
834 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
835 rc = VINF_PATM_PATCH_TRAP_GP;
836 break;
837
838 case VINF_EM_RAW_INTERRUPT_PENDING:
839 Assert(TRPMHasTrap(pVM));
840 /* no break; */
841 case VINF_PGM_SYNC_CR3: /** @todo Check this with Sander. */
842 case VINF_IOM_HC_IOPORT_READWRITE:
843 case VINF_IOM_HC_IOPORT_READ:
844 case VINF_IOM_HC_IOPORT_WRITE:
845 case VINF_IOM_HC_MMIO_WRITE:
846 case VINF_IOM_HC_MMIO_READ:
847 case VINF_IOM_HC_MMIO_READ_WRITE:
848 case VINF_PATM_PATCH_INT3:
849 case VINF_EM_RAW_TO_R3:
850 case VINF_EM_RAW_TIMER_PENDING:
851 case VINF_EM_PENDING_REQUEST:
852 case VINF_EM_HALT:
853 case VINF_SUCCESS:
854 break;
855
856 default:
857 AssertMsg(PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip) == false, ("return code %d\n", rc));
858 break;
859 }
860 return rc;
861}
862
863/**
864 * \#PF (Page Fault) handler.
865 *
866 * Calls PGM which does the actual handling.
867 *
868 *
869 * @returns VBox status code.
870 * VINF_SUCCESS means we completely handled this trap,
871 * other codes are passed execution to host context.
872 *
873 * @param pTrpm Pointer to TRPM data (within VM).
874 * @param pRegFrame Pointer to the register frame for the trap.
875 * @internal
876 */
877DECLASM(int) TRPMGCTrap0eHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
878{
879 LogBird(("TRPMGCTrap0eHandler: eip=%RGv\n", pRegFrame->eip));
880 PVM pVM = TRPM2VM(pTrpm);
881
882 /*
883 * This is all PGM stuff.
884 */
885 int rc = PGMTrap0eHandler(pVM, pTrpm->uActiveErrorCode, pRegFrame, (RTGCPTR)pTrpm->uActiveCR2);
886
887 switch (rc)
888 {
889 case VINF_EM_RAW_EMULATE_INSTR:
890 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
891 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
892 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
893 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
894 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
895 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
896 rc = VINF_PATCH_EMULATE_INSTR;
897 break;
898
899 case VINF_EM_RAW_GUEST_TRAP:
900 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
901 return VINF_PATM_PATCH_TRAP_PF;
902
903 rc = TRPMForwardTrap(pVM, pRegFrame, 0xE, 0, TRPM_TRAP_HAS_ERRORCODE, TRPM_TRAP);
904 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
905 break;
906
907 case VINF_EM_RAW_INTERRUPT_PENDING:
908 Assert(TRPMHasTrap(pVM));
909 /* no break; */
910 case VINF_IOM_HC_MMIO_READ:
911 case VINF_IOM_HC_MMIO_WRITE:
912 case VINF_IOM_HC_MMIO_READ_WRITE:
913 case VINF_PATM_HC_MMIO_PATCH_READ:
914 case VINF_PATM_HC_MMIO_PATCH_WRITE:
915 case VINF_SUCCESS:
916 case VINF_EM_RAW_TO_R3:
917 case VINF_EM_PENDING_REQUEST:
918 case VINF_EM_RAW_TIMER_PENDING:
919 case VINF_CSAM_PENDING_ACTION:
920 case VINF_PGM_SYNC_CR3: /** @todo Check this with Sander. */
921 break;
922
923 default:
924 AssertMsg(PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip) == false, ("Patch address for return code %d. eip=%08x\n", rc, pRegFrame->eip));
925 break;
926 }
927 return trpmGCExitTrap(pVM, rc, pRegFrame);
928}
929
930
931/**
932 * Scans for the EIP in the specified array of trap handlers.
933 *
934 * If we don't fine the EIP, we'll panic.
935 *
936 * @returns VBox status code.
937 *
938 * @param pVM The VM handle.
939 * @param pRegFrame Pointer to the register frame for the trap.
940 * @param paHandlers The array of trap handler records.
941 * @param pEndRecord The end record (exclusive).
942 */
943static int trpmGCHyperGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, PCTRPMGCHYPER paHandlers, PCTRPMGCHYPER pEndRecord)
944{
945 uintptr_t uEip = (uintptr_t)pRegFrame->eip;
946 Assert(paHandlers <= pEndRecord);
947
948 Log(("trpmGCHyperGeneric: uEip=%x %p-%p\n", uEip, paHandlers, pEndRecord));
949
950#if 0 /// @todo later
951 /*
952 * Start by doing a kind of binary search.
953 */
954 unsigned iStart = 0;
955 unsigned iEnd = pEndRecord - paHandlers;
956 unsigned i = iEnd / 2;
957#endif
958
959 /*
960 * Do a linear search now (in case the array wasn't properly sorted).
961 */
962 for (PCTRPMGCHYPER pCur = paHandlers; pCur < pEndRecord; pCur++)
963 {
964 if ( pCur->uStartEIP <= uEip
965 && (pCur->uEndEIP ? pCur->uEndEIP > uEip : pCur->uStartEIP == uEip))
966 return pCur->pfnHandler(pVM, pRegFrame, pCur->uUser);
967 }
968
969 return VERR_TRPM_DONT_PANIC;
970}
971
972
973/**
974 * Hypervisor \#NP ((segment) Not Present) handler.
975 *
976 * Scans for the EIP in the registered trap handlers.
977 *
978 * @returns VBox status code.
979 * VINF_SUCCESS means we completely handled this trap,
980 * other codes are passed back to host context.
981 *
982 * @param pTrpm Pointer to TRPM data (within VM).
983 * @param pRegFrame Pointer to the register frame for the trap.
984 * @internal
985 */
986DECLASM(int) TRPMGCHyperTrap0bHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
987{
988 return trpmGCHyperGeneric(TRPM2VM(pTrpm), pRegFrame, g_aTrap0bHandlers, g_aTrap0bHandlersEnd);
989}
990
991
992/**
993 * Hypervisor \#GP (General Protection Fault) handler.
994 *
995 * Scans for the EIP in the registered trap handlers.
996 *
997 * @returns VBox status code.
998 * VINF_SUCCESS means we completely handled this trap,
999 * other codes are passed back to host context.
1000 *
1001 * @param pTrpm Pointer to TRPM data (within VM).
1002 * @param pRegFrame Pointer to the register frame for the trap.
1003 * @internal
1004 */
1005DECLASM(int) TRPMGCHyperTrap0dHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
1006{
1007 return trpmGCHyperGeneric(TRPM2VM(pTrpm), pRegFrame, g_aTrap0dHandlers, g_aTrap0dHandlersEnd);
1008}
1009
1010
1011/**
1012 * Hypervisor \#PF (Page Fault) handler.
1013 *
1014 * Scans for the EIP in the registered trap handlers.
1015 *
1016 * @returns VBox status code.
1017 * VINF_SUCCESS means we completely handled this trap,
1018 * other codes are passed back to host context.
1019 *
1020 * @param pTrpm Pointer to TRPM data (within VM).
1021 * @param pRegFrame Pointer to the register frame for the trap.
1022 * @internal
1023 */
1024DECLASM(int) TRPMGCHyperTrap0eHandler(PTRPM pTrpm, PCPUMCTXCORE pRegFrame)
1025{
1026 return trpmGCHyperGeneric(TRPM2VM(pTrpm), pRegFrame, g_aTrap0dHandlers, g_aTrap0dHandlersEnd);
1027}
1028
1029
1030/**
1031 * Deal with hypervisor traps occuring when resuming execution on a trap.
1032 *
1033 * @returns VBox status code.
1034 * @param pVM The VM handle.
1035 * @param pRegFrame Register frame.
1036 * @param uUser User arg.
1037 */
1038DECLCALLBACK(int) trpmGCTrapInGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser)
1039{
1040 Log(("********************************************************\n"));
1041 Log(("trpmGCTrapInGeneric: eip=%RX32 uUser=%#x\n", pRegFrame->eip, uUser));
1042 Log(("********************************************************\n"));
1043
1044 if (uUser & TRPM_TRAP_IN_HYPER)
1045 {
1046 /*
1047 * Check that there is still some stack left, if not we'll flag
1048 * a guru meditation (the alternative is a triple fault).
1049 */
1050 RTGCUINTPTR cbStackUsed = (RTGCUINTPTR)VMMGetStackGC(pVM) - pRegFrame->esp;
1051 if (cbStackUsed > VMM_STACK_SIZE - _1K)
1052 {
1053 LogRel(("trpmGCTrapInGeneric: ran out of stack: esp=#x cbStackUsed=%#x\n", pRegFrame->esp, cbStackUsed));
1054 return VERR_TRPM_DONT_PANIC;
1055 }
1056
1057 /*
1058 * Just zero the register containing the selector in question.
1059 * We'll deal with the actual stale or troublesome selector value in
1060 * the outermost trap frame.
1061 */
1062 switch (uUser & TRPM_TRAP_IN_OP_MASK)
1063 {
1064 case TRPM_TRAP_IN_MOV_GS:
1065 pRegFrame->eax = 0;
1066 pRegFrame->gs = 0; /* prevent recursive trouble. */
1067 break;
1068 case TRPM_TRAP_IN_MOV_FS:
1069 pRegFrame->eax = 0;
1070 pRegFrame->fs = 0; /* prevent recursive trouble. */
1071 return VINF_SUCCESS;
1072
1073 default:
1074 AssertMsgFailed(("Invalid uUser=%#x\n", uUser));
1075 return VERR_INTERNAL_ERROR;
1076 }
1077 }
1078 else
1079 {
1080 /*
1081 * Reconstruct the guest context and switch to the recompiler.
1082 * We ASSUME we're only at
1083 */
1084 CPUMCTXCORE CtxCore = *pRegFrame;
1085 uint32_t *pEsp = (uint32_t *)pRegFrame->esp;
1086 int rc;
1087
1088 switch (uUser)
1089 {
1090 /*
1091 * This will only occur when resuming guest code in a trap handler!
1092 */
1093 /* @note ASSUMES esp points to the temporary guest CPUMCTXCORE!!! */
1094 case TRPM_TRAP_IN_MOV_GS:
1095 case TRPM_TRAP_IN_MOV_FS:
1096 case TRPM_TRAP_IN_MOV_ES:
1097 case TRPM_TRAP_IN_MOV_DS:
1098 {
1099 PCPUMCTXCORE pTempGuestCtx = (PCPUMCTXCORE)pEsp;
1100
1101 /* Just copy the whole thing; several selector registers, eip (etc) and eax are not yet in pRegFrame. */
1102 CtxCore = *pTempGuestCtx;
1103 rc = VINF_EM_RAW_STALE_SELECTOR;
1104 break;
1105 }
1106
1107 /*
1108 * This will only occur when resuming guest code!
1109 */
1110 case TRPM_TRAP_IN_IRET:
1111 CtxCore.eip = *pEsp++;
1112 CtxCore.cs = (RTSEL)*pEsp++;
1113 CtxCore.eflags.u32 = *pEsp++;
1114 CtxCore.esp = *pEsp++;
1115 CtxCore.ss = (RTSEL)*pEsp++;
1116 rc = VINF_EM_RAW_IRET_TRAP;
1117 break;
1118
1119 /*
1120 * This will only occur when resuming V86 guest code!
1121 */
1122 case TRPM_TRAP_IN_IRET | TRPM_TRAP_IN_V86:
1123 CtxCore.eip = *pEsp++;
1124 CtxCore.cs = (RTSEL)*pEsp++;
1125 CtxCore.eflags.u32 = *pEsp++;
1126 CtxCore.esp = *pEsp++;
1127 CtxCore.ss = (RTSEL)*pEsp++;
1128 CtxCore.es = (RTSEL)*pEsp++;
1129 CtxCore.ds = (RTSEL)*pEsp++;
1130 CtxCore.fs = (RTSEL)*pEsp++;
1131 CtxCore.gs = (RTSEL)*pEsp++;
1132 rc = VINF_EM_RAW_IRET_TRAP;
1133 break;
1134
1135 default:
1136 AssertMsgFailed(("Invalid uUser=%#x\n", uUser));
1137 return VERR_INTERNAL_ERROR;
1138 }
1139
1140
1141 CPUMSetGuestCtxCore(pVM, &CtxCore);
1142 TRPMGCHyperReturnToHost(pVM, rc);
1143 }
1144
1145 AssertMsgFailed(("Impossible!\n"));
1146 return VERR_INTERNAL_ERROR;
1147}
1148
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