VirtualBox

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

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

Emulate rdmsr & wrmsr in GC as well.

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