VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMRC/TRPMRCHandlers.cpp@ 43530

Last change on this file since 43530 was 42779, checked in by vboxsync, 12 years ago

EM,TRPM: VINF_SELM_SYNC_GDT fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 51.8 KB
Line 
1/* $Id: TRPMRCHandlers.cpp 42779 2012-08-11 22:47:43Z vboxsync $ */
2/** @file
3 * TRPM - Raw-mode Context Trap Handlers, CPP part
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_TRPM
23#include <VBox/vmm/selm.h>
24#include <VBox/vmm/iom.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/pdmapi.h>
27#include <VBox/vmm/dbgf.h>
28#include <VBox/vmm/em.h>
29#include <VBox/vmm/csam.h>
30#include <VBox/vmm/patm.h>
31#include <VBox/vmm/mm.h>
32#include <VBox/vmm/cpum.h>
33#include "TRPMInternal.h"
34#include <VBox/vmm/vm.h>
35#include <VBox/vmm/vmm.h>
36#include <VBox/param.h>
37
38#include <VBox/err.h>
39#include <VBox/dis.h>
40#include <VBox/disopcode.h>
41#include <VBox/log.h>
42#include <VBox/vmm/tm.h>
43#include <iprt/asm.h>
44#include <iprt/asm-amd64-x86.h>
45#include <iprt/assert.h>
46#include <iprt/x86.h>
47
48
49/*******************************************************************************
50* Defined Constants And Macros *
51*******************************************************************************/
52/* still here. MODR/M byte parsing */
53#define X86_OPCODE_MODRM_MOD_MASK 0xc0
54#define X86_OPCODE_MODRM_REG_MASK 0x38
55#define X86_OPCODE_MODRM_RM_MASK 0x07
56
57/** @todo fix/remove/permanent-enable this when DIS/PATM handles invalid lock sequences. */
58#define DTRACE_EXPERIMENT
59
60#if 1
61# define TRPM_ENTER_DBG_HOOK(a_iVector) do {} while (0)
62# define TRPM_EXIT_DBG_HOOK(a_iVector) do {} while (0)
63# define TRPM_ENTER_DBG_HOOK_HYPER(a_iVector) do {} while (0)
64# define TRPM_EXIT_DBG_HOOK_HYPER(a_iVector) do {} while (0)
65#else
66# define TRPM_ENTER_DBG_HOOK(a_iVector) \
67 uint32_t const fDbgEFlags1 = CPUMRawGetEFlags(pVCpu); \
68 if (!(fDbgEFlags1 & X86_EFL_IF)) Log(("%s: IF=0 ##\n", __FUNCTION__)); \
69 else do {} while(0)
70# define TRPM_EXIT_DBG_HOOK(a_iVector) \
71 do { \
72 uint32_t const fDbgEFlags2 = CPUMRawGetEFlags(pVCpu); \
73 if ((fDbgEFlags1 ^ fDbgEFlags2) & (X86_EFL_IF | X86_EFL_IOPL)) \
74 Log(("%s: IF=%d->%d IOPL=%d->%d !#\n", __FUNCTION__, \
75 !!(fDbgEFlags1 & X86_EFL_IF), !!(fDbgEFlags2 & X86_EFL_IF), \
76 X86_EFL_GET_IOPL(fDbgEFlags1), X86_EFL_GET_IOPL(fDbgEFlags2) )); \
77 else if (!(fDbgEFlags2 & X86_EFL_IF)) Log(("%s: IF=0 [ret] ##\n", __FUNCTION__)); \
78 } while (0)
79# define TRPM_ENTER_DBG_HOOK_HYPER(a_iVector) do {} while (0)
80# define TRPM_EXIT_DBG_HOOK_HYPER(a_iVector) do {} while (0)
81#endif
82
83/*******************************************************************************
84* Structures and Typedefs *
85*******************************************************************************/
86/** Pointer to a readonly hypervisor trap record. */
87typedef const struct TRPMGCHYPER *PCTRPMGCHYPER;
88
89/**
90 * A hypervisor trap record.
91 * This contains information about a handler for a instruction range.
92 *
93 * @remark This must match what TRPM_HANDLER outputs.
94 */
95typedef struct TRPMGCHYPER
96{
97 /** The start address. */
98 uintptr_t uStartEIP;
99 /** The end address. (exclusive)
100 * If NULL the it's only for the instruction at pvStartEIP. */
101 uintptr_t uEndEIP;
102 /**
103 * The handler.
104 *
105 * @returns VBox status code
106 * VINF_SUCCESS means we've handled the trap.
107 * Any other error code means returning to the host context.
108 * @param pVM Pointer to the VM.
109 * @param pRegFrame The register frame.
110 * @param uUser The user argument.
111 */
112 DECLRCCALLBACKMEMBER(int, pfnHandler, (PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser));
113 /** Whatever the handler desires to put here. */
114 uintptr_t uUser;
115} TRPMGCHYPER;
116
117
118/*******************************************************************************
119* Global Variables *
120*******************************************************************************/
121RT_C_DECLS_BEGIN
122/** Defined in VMMGC0.asm or VMMGC99.asm.
123 * @{ */
124extern const TRPMGCHYPER g_aTrap0bHandlers[1];
125extern const TRPMGCHYPER g_aTrap0bHandlersEnd[1];
126extern const TRPMGCHYPER g_aTrap0dHandlers[1];
127extern const TRPMGCHYPER g_aTrap0dHandlersEnd[1];
128extern const TRPMGCHYPER g_aTrap0eHandlers[1];
129extern const TRPMGCHYPER g_aTrap0eHandlersEnd[1];
130/** @} */
131RT_C_DECLS_END
132
133
134/*******************************************************************************
135* Internal Functions *
136*******************************************************************************/
137RT_C_DECLS_BEGIN /* addressed from asm (not called so no DECLASM). */
138DECLCALLBACK(int) trpmRCTrapInGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser);
139RT_C_DECLS_END
140
141
142
143/**
144 * Exits the trap, called when exiting a trap handler.
145 *
146 * Will reset the trap if it's not a guest trap or the trap
147 * is already handled. Will process resume guest FFs.
148 *
149 * @returns rc, can be adjusted if its VINF_SUCCESS or something really bad
150 * happened.
151 * @param pVM Pointer to the VM.
152 * @param pVCpu Pointer to the VMCPU.
153 * @param rc The VBox status code to return.
154 * @param pRegFrame Pointer to the register frame for the trap.
155 *
156 * @remarks This must not be used for hypervisor traps, only guest traps.
157 */
158static int trpmGCExitTrap(PVM pVM, PVMCPU pVCpu, int rc, PCPUMCTXCORE pRegFrame)
159{
160 uint32_t uOldActiveVector = pVCpu->trpm.s.uActiveVector;
161 NOREF(uOldActiveVector);
162
163 /* Reset trap? */
164 if ( rc != VINF_EM_RAW_GUEST_TRAP
165 && rc != VINF_EM_RAW_RING_SWITCH_INT)
166 pVCpu->trpm.s.uActiveVector = UINT32_MAX;
167
168#ifdef VBOX_HIGH_RES_TIMERS_HACK
169 /*
170 * We should poll the timers occasionally.
171 * We must *NOT* do this too frequently as it adds a significant overhead
172 * and it'll kill us if the trap load is high. (See @bugref{1354}.)
173 * (The heuristic is not very intelligent, we should really check trap
174 * frequency etc. here, but alas, we lack any such information atm.)
175 */
176 static unsigned s_iTimerPoll = 0;
177 if (rc == VINF_SUCCESS)
178 {
179 if (!(++s_iTimerPoll & 0xf))
180 {
181 TMTimerPollVoid(pVM, pVCpu);
182 Log2(("TMTimerPoll at %08RX32 - VM_FF_TM_VIRTUAL_SYNC=%d VM_FF_TM_VIRTUAL_SYNC=%d\n", pRegFrame->eip,
183 VM_FF_ISPENDING(pVM, VM_FF_TM_VIRTUAL_SYNC), VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER)));
184 }
185 }
186 else
187 s_iTimerPoll = 0;
188#endif
189
190 /* Clear pending inhibit interrupt state if required. (necessary for dispatching interrupts later on) */
191 if (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
192 {
193 Log2(("VM_FF_INHIBIT_INTERRUPTS at %08RX32 successor %RGv\n", pRegFrame->eip, EMGetInhibitInterruptsPC(pVCpu)));
194 if (pRegFrame->eip != EMGetInhibitInterruptsPC(pVCpu))
195 {
196 /** @note we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here if the eip is the same as the inhibited instr address.
197 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
198 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
199 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
200 */
201 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
202 }
203 }
204
205 /*
206 * Pending resume-guest-FF?
207 * Or pending (A)PIC interrupt? Windows XP will crash if we delay APIC interrupts.
208 */
209 if ( rc == VINF_SUCCESS
210 && ( VM_FF_ISPENDING(pVM, VM_FF_TM_VIRTUAL_SYNC | VM_FF_REQUEST | VM_FF_PGM_NO_MEMORY | VM_FF_PDM_DMA)
211 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER | VMCPU_FF_TO_R3 | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC
212 | VMCPU_FF_REQUEST | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL
213 | VMCPU_FF_PDM_CRITSECT
214 | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_SELM_SYNC_TSS
215 )
216 )
217 )
218 {
219 /* The out of memory condition naturally outranks the others. */
220 if (RT_UNLIKELY(VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY)))
221 rc = VINF_EM_NO_MEMORY;
222 /* Pending Ring-3 action. */
223 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TO_R3 | VMCPU_FF_PDM_CRITSECT))
224 {
225 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
226 rc = VINF_EM_RAW_TO_R3;
227 }
228 /* Pending timer action. */
229 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER))
230 rc = VINF_EM_RAW_TIMER_PENDING;
231 /* The Virtual Sync clock has stopped. */
232 else if (VM_FF_ISPENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
233 rc = VINF_EM_RAW_TO_R3;
234 /* DMA work pending? */
235 else if (VM_FF_ISPENDING(pVM, VM_FF_PDM_DMA))
236 rc = VINF_EM_RAW_TO_R3;
237 /* Pending request packets might contain actions that need immediate
238 attention, such as pending hardware interrupts. */
239 else if ( VM_FF_ISPENDING(pVM, VM_FF_REQUEST)
240 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_REQUEST))
241 rc = VINF_EM_PENDING_REQUEST;
242 /* Pending GDT/LDT/TSS sync. */
243 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_SELM_SYNC_TSS))
244 rc = VINF_SELM_SYNC_GDT;
245 /* Pending interrupt: dispatch it. */
246 else if ( VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)
247 && !VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
248 && PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame)
249 )
250 {
251 uint8_t u8Interrupt;
252 rc = PDMGetInterrupt(pVCpu, &u8Interrupt);
253 Log(("trpmGCExitTrap: u8Interrupt=%d (%#x) rc=%Rrc\n", u8Interrupt, u8Interrupt, rc));
254 AssertFatalMsgRC(rc, ("PDMGetInterrupt failed with %Rrc\n", rc));
255 rc = TRPMForwardTrap(pVCpu, pRegFrame, (uint32_t)u8Interrupt, 0, TRPM_TRAP_NO_ERRORCODE, TRPM_HARDWARE_INT, uOldActiveVector);
256 /* can't return if successful */
257 Assert(rc != VINF_SUCCESS);
258
259 /* Stop the profile counter that was started in TRPMGCHandlersA.asm */
260 Assert(uOldActiveVector <= 16);
261 STAM_PROFILE_ADV_STOP(&pVM->trpm.s.aStatGCTraps[uOldActiveVector], a);
262
263 /* Assert the trap and go to the recompiler to dispatch it. */
264 TRPMAssertTrap(pVCpu, u8Interrupt, TRPM_HARDWARE_INT);
265
266 STAM_PROFILE_ADV_START(&pVM->trpm.s.aStatGCTraps[uOldActiveVector], a);
267 rc = VINF_EM_RAW_INTERRUPT_PENDING;
268 }
269 /*
270 * Try sync CR3?
271 */
272 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
273 {
274#if 1
275 PGMRZDynMapReleaseAutoSet(pVCpu);
276 PGMRZDynMapStartAutoSet(pVCpu);
277 rc = PGMSyncCR3(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR3(pVCpu), CPUMGetGuestCR4(pVCpu), VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
278#else
279 rc = VINF_PGM_SYNC_CR3;
280#endif
281 }
282 }
283
284 /* Note! TRPMRCHandlersA.asm performs sanity checks in debug builds.*/
285 PGMRZDynMapReleaseAutoSet(pVCpu);
286 return rc;
287}
288
289
290/**
291 * \#DB (Debug event) handler.
292 *
293 * @returns VBox status code.
294 * VINF_SUCCESS means we completely handled this trap,
295 * other codes are passed execution to host context.
296 *
297 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
298 * @param pRegFrame Pointer to the register frame for the trap.
299 * @internal
300 */
301DECLASM(int) TRPMGCTrap01Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
302{
303 RTGCUINTREG uDr6 = ASMGetAndClearDR6();
304 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
305 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
306 LogFlow(("TRPMGC01: cs:eip=%04x:%08x uDr6=%RTreg EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, uDr6, CPUMRawGetEFlags(pVCpu)));
307 TRPM_ENTER_DBG_HOOK(1);
308
309 /*
310 * We currently don't make use of the X86_DR7_GD bit, but
311 * there might come a time when we do.
312 */
313 AssertReleaseMsgReturn((uDr6 & X86_DR6_BD) != X86_DR6_BD,
314 ("X86_DR6_BD isn't used, but it's set! dr7=%RTreg(%RTreg) dr6=%RTreg\n",
315 ASMGetDR7(), CPUMGetHyperDR7(pVCpu), uDr6),
316 VERR_NOT_IMPLEMENTED);
317 AssertReleaseMsg(!(uDr6 & X86_DR6_BT), ("X86_DR6_BT is impossible!\n"));
318
319 /*
320 * Now leave the rest to the DBGF.
321 */
322 PGMRZDynMapStartAutoSet(pVCpu);
323 int rc = DBGFRZTrap01Handler(pVM, pVCpu, pRegFrame, uDr6);
324 if (rc == VINF_EM_RAW_GUEST_TRAP)
325 CPUMSetGuestDR6(pVCpu, uDr6);
326
327 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
328 Log6(("TRPMGC01: %Rrc (%04x:%08x %RTreg %EFlag=%#x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, uDr6, CPUMRawGetEFlags(pVCpu)));
329 TRPM_EXIT_DBG_HOOK(1);
330 return rc;
331}
332
333
334/**
335 * \#DB (Debug event) handler for the hypervisor code.
336 *
337 * This is mostly the same as TRPMGCTrap01Handler, but we skip the PGM auto
338 * mapping set as well as the default trap exit path since they are both really
339 * bad ideas in this context.
340 *
341 * @returns VBox status code.
342 * VINF_SUCCESS means we completely handled this trap,
343 * other codes are passed execution to host context.
344 *
345 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
346 * @param pRegFrame Pointer to the register frame for the trap.
347 * @internal
348 */
349DECLASM(int) TRPMGCHyperTrap01Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
350{
351 RTGCUINTREG uDr6 = ASMGetAndClearDR6();
352 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
353 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
354 TRPM_ENTER_DBG_HOOK_HYPER(1);
355 LogFlow(("TRPMGCHyper01: cs:eip=%04x:%08x uDr6=%RTreg\n", pRegFrame->cs.Sel, pRegFrame->eip, uDr6));
356
357 /*
358 * We currently don't make use of the X86_DR7_GD bit, but
359 * there might come a time when we do.
360 */
361 AssertReleaseMsgReturn((uDr6 & X86_DR6_BD) != X86_DR6_BD,
362 ("X86_DR6_BD isn't used, but it's set! dr7=%RTreg(%RTreg) dr6=%RTreg\n",
363 ASMGetDR7(), CPUMGetHyperDR7(pVCpu), uDr6),
364 VERR_NOT_IMPLEMENTED);
365 AssertReleaseMsg(!(uDr6 & X86_DR6_BT), ("X86_DR6_BT is impossible!\n"));
366
367 /*
368 * Now leave the rest to the DBGF.
369 */
370 int rc = DBGFRZTrap01Handler(pVM, pVCpu, pRegFrame, uDr6);
371 AssertStmt(rc != VINF_EM_RAW_GUEST_TRAP, rc = VERR_TRPM_IPE_1);
372
373 Log6(("TRPMGCHyper01: %Rrc (%04x:%08x %RTreg)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, uDr6));
374 TRPM_EXIT_DBG_HOOK_HYPER(1);
375 return rc;
376}
377
378
379/**
380 * NMI handler, for when we are using NMIs to debug things.
381 *
382 * @returns VBox status code.
383 * VINF_SUCCESS means we completely handled this trap,
384 * other codes are passed execution to host context.
385 *
386 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
387 * @param pRegFrame Pointer to the register frame for the trap.
388 * @internal
389 * @remark This is not hooked up unless you're building with VBOX_WITH_NMI defined.
390 */
391DECLASM(int) TRPMGCTrap02Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
392{
393 LogFlow(("TRPMGCTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs.Sel, pRegFrame->eip));
394 RTLogComPrintf("TRPMGCTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs.Sel, pRegFrame->eip);
395 NOREF(pTrpmCpu);
396 return VERR_TRPM_DONT_PANIC;
397}
398
399
400/**
401 * NMI handler, for when we are using NMIs to debug things.
402 *
403 * This is the handler we're most likely to hit when the NMI fires (it is
404 * unlikely that we'll be stuck in guest code).
405 *
406 * @returns VBox status code.
407 * VINF_SUCCESS means we completely handled this trap,
408 * other codes are passed execution to host context.
409 *
410 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
411 * @param pRegFrame Pointer to the register frame for the trap.
412 * @internal
413 * @remark This is not hooked up unless you're building with VBOX_WITH_NMI defined.
414 */
415DECLASM(int) TRPMGCHyperTrap02Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
416{
417 LogFlow(("TRPMGCHyperTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs.Sel, pRegFrame->eip));
418 RTLogComPrintf("TRPMGCHyperTrap02Handler: cs:eip=%04x:%08x\n", pRegFrame->cs.Sel, pRegFrame->eip);
419 NOREF(pTrpmCpu);
420 return VERR_TRPM_DONT_PANIC;
421}
422
423
424/**
425 * \#BP (Breakpoint) handler.
426 *
427 * @returns VBox status code.
428 * VINF_SUCCESS means we completely handled this trap,
429 * other codes are passed execution to host context.
430 *
431 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
432 * @param pRegFrame Pointer to the register frame for the trap.
433 * @internal
434 */
435DECLASM(int) TRPMGCTrap03Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
436{
437 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
438 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
439 int rc;
440 LogFlow(("TRPMGC03: %04x:%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
441 TRPM_ENTER_DBG_HOOK(3);
442 PGMRZDynMapStartAutoSet(pVCpu);
443
444 /*
445 * PATM is using INT3s, let them have a go first.
446 */
447 if ( (pRegFrame->ss.Sel & X86_SEL_RPL) == 1
448 && !pRegFrame->eflags.Bits.u1VM)
449 {
450 rc = PATMRCHandleInt3PatchTrap(pVM, pRegFrame);
451 if ( rc == VINF_SUCCESS
452 || rc == VINF_EM_RAW_EMULATE_INSTR
453 || rc == VINF_PATM_PATCH_INT3
454 || rc == VINF_PATM_DUPLICATE_FUNCTION)
455 {
456 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
457 Log6(("TRPMGC03: %Rrc (%04x:%08x EFL=%x) (PATM)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
458 TRPM_EXIT_DBG_HOOK(3);
459 return rc;
460 }
461 }
462 rc = DBGFRZTrap03Handler(pVM, pVCpu, pRegFrame);
463
464 /* anything we should do with this? Schedule it in GC? */
465 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
466 Log6(("TRPMGC03: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
467 TRPM_EXIT_DBG_HOOK(3);
468 return rc;
469}
470
471
472/**
473 * \#BP (Breakpoint) handler.
474 *
475 * This is similar to TRPMGCTrap03Handler but we bits which are potentially
476 * harmful to us (common trap exit and the auto mapping set).
477 *
478 * @returns VBox status code.
479 * VINF_SUCCESS means we completely handled this trap,
480 * other codes are passed execution to host context.
481 *
482 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
483 * @param pRegFrame Pointer to the register frame for the trap.
484 * @internal
485 */
486DECLASM(int) TRPMGCHyperTrap03Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
487{
488 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
489 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
490 LogFlow(("TRPMGCHyper03: %04x:%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
491 TRPM_ENTER_DBG_HOOK_HYPER(3);
492
493 /*
494 * Hand it over to DBGF.
495 */
496 int rc = DBGFRZTrap03Handler(pVM, pVCpu, pRegFrame);
497 AssertStmt(rc != VINF_EM_RAW_GUEST_TRAP, rc = VERR_TRPM_IPE_2);
498
499 Log6(("TRPMGCHyper03: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
500 TRPM_EXIT_DBG_HOOK_HYPER(3);
501 return rc;
502}
503
504
505/**
506 * Trap handler for illegal opcode fault (\#UD).
507 *
508 * @returns VBox status code.
509 * VINF_SUCCESS means we completely handled this trap,
510 * other codes are passed execution to host context.
511 *
512 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
513 * @param pRegFrame Pointer to the register frame for the trap.
514 * @internal
515 */
516DECLASM(int) TRPMGCTrap06Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
517{
518 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
519 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
520 int rc;
521 LogFlow(("TRPMGC06: %04x:%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, pRegFrame->eflags.u32, CPUMRawGetEFlags(pVCpu)));
522 TRPM_ENTER_DBG_HOOK(6);
523 PGMRZDynMapStartAutoSet(pVCpu);
524
525 if (CPUMGetGuestCPL(pVCpu) == 0)
526 {
527 /*
528 * Decode the instruction.
529 */
530 RTGCPTR PC;
531 rc = SELMValidateAndConvertCSAddr(pVCpu, pRegFrame->eflags, pRegFrame->ss.Sel, pRegFrame->cs.Sel, &pRegFrame->cs,
532 pRegFrame->rip, &PC);
533 if (RT_FAILURE(rc))
534 {
535 Log(("TRPMGCTrap06Handler: Failed to convert %RTsel:%RX32 (cpl=%d) - rc=%Rrc !!\n", pRegFrame->cs.Sel, pRegFrame->eip, pRegFrame->ss.Sel & X86_SEL_RPL, rc));
536 rc = trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_GUEST_TRAP, pRegFrame);
537 Log6(("TRPMGC06: %Rrc (%04x:%08x EFL=%x) (SELM)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
538 TRPM_EXIT_DBG_HOOK(6);
539 return rc;
540 }
541
542 DISCPUSTATE Cpu;
543 uint32_t cbOp;
544 rc = EMInterpretDisasOneEx(pVM, pVCpu, (RTGCUINTPTR)PC, pRegFrame, &Cpu, &cbOp);
545 if (RT_FAILURE(rc))
546 {
547 rc = trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
548 Log6(("TRPMGC06: %Rrc (%04x:%08x EFL=%x) (EM)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
549 TRPM_EXIT_DBG_HOOK(6);
550 return rc;
551 }
552
553 /*
554 * UD2 in a patch?
555 * Note! PATMGCHandleIllegalInstrTrap doesn't always return.
556 */
557 if ( Cpu.pCurInstr->uOpcode == OP_ILLUD2
558 && PATMIsPatchGCAddr(pVM, pRegFrame->eip))
559 {
560 LogFlow(("TRPMGCTrap06Handler: -> PATMRCHandleIllegalInstrTrap\n"));
561 rc = PATMRCHandleIllegalInstrTrap(pVM, pRegFrame);
562 /** @todo These tests are completely unnecessary, should just follow the
563 * flow and return at the end of the function. */
564 if ( rc == VINF_SUCCESS
565 || rc == VINF_EM_RAW_EMULATE_INSTR
566 || rc == VINF_PATM_DUPLICATE_FUNCTION
567 || rc == VINF_PATM_PENDING_IRQ_AFTER_IRET
568 || rc == VINF_EM_RESCHEDULE)
569 {
570 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
571 Log6(("TRPMGC06: %Rrc (%04x:%08x EFL=%x) (PATM)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
572 TRPM_EXIT_DBG_HOOK(6);
573 return rc;
574 }
575 }
576 /*
577 * Speed up dtrace and don't entrust invalid lock sequences to the recompiler.
578 */
579 else if (Cpu.fPrefix & DISPREFIX_LOCK)
580 {
581 Log(("TRPMGCTrap06Handler: pc=%08x op=%d\n", pRegFrame->eip, Cpu.pCurInstr->uOpcode));
582#ifdef DTRACE_EXPERIMENT /** @todo fix/remove/permanent-enable this when DIS/PATM handles invalid lock sequences. */
583 Assert(!PATMIsPatchGCAddr(pVM, pRegFrame->eip));
584 rc = TRPMForwardTrap(pVCpu, pRegFrame, 0x6, 0, TRPM_TRAP_NO_ERRORCODE, TRPM_TRAP, 0x6);
585 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
586#else
587 rc = VINF_EM_RAW_EMULATE_INSTR;
588#endif
589 }
590 /*
591 * Handle MONITOR - it causes an #UD exception instead of #GP when not executed in ring 0.
592 */
593 else if (Cpu.pCurInstr->uOpcode == OP_MONITOR)
594 {
595 LogFlow(("TRPMGCTrap06Handler: -> EMInterpretInstructionCPU\n"));
596 rc = EMInterpretInstructionDisasState(pVCpu, &Cpu, pRegFrame, PC, EMCODETYPE_SUPERVISOR);
597 }
598 /* Never generate a raw trap here; it might be an instruction, that requires emulation. */
599 else
600 {
601 LogFlow(("TRPMGCTrap06Handler: -> VINF_EM_RAW_EMULATE_INSTR\n"));
602 rc = VINF_EM_RAW_EMULATE_INSTR;
603 }
604 }
605 else
606 {
607 LogFlow(("TRPMGCTrap06Handler: -> TRPMForwardTrap\n"));
608 rc = TRPMForwardTrap(pVCpu, pRegFrame, 0x6, 0, TRPM_TRAP_NO_ERRORCODE, TRPM_TRAP, 0x6);
609 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
610 }
611
612 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
613 Log6(("TRPMGC06: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
614 TRPM_EXIT_DBG_HOOK(6);
615 return rc;
616}
617
618
619/**
620 * Trap handler for device not present fault (\#NM).
621 *
622 * Device not available, FP or (F)WAIT instruction.
623 *
624 * @returns VBox status code.
625 * VINF_SUCCESS means we completely handled this trap,
626 * other codes are passed execution to host context.
627 *
628 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
629 * @param pRegFrame Pointer to the register frame for the trap.
630 * @internal
631 */
632DECLASM(int) TRPMGCTrap07Handler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
633{
634 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
635 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
636 LogFlow(("TRPMGC07: %04x:%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
637 TRPM_ENTER_DBG_HOOK(7);
638 PGMRZDynMapStartAutoSet(pVCpu);
639
640 int rc = CPUMHandleLazyFPU(pVCpu);
641 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
642 Log6(("TRPMGC07: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
643 TRPM_EXIT_DBG_HOOK(7);
644 return rc;
645}
646
647
648/**
649 * \#NP ((segment) Not Present) handler.
650 *
651 * @returns VBox status code.
652 * VINF_SUCCESS means we completely handled this trap,
653 * other codes are passed execution to host context.
654 *
655 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
656 * @param pRegFrame Pointer to the register frame for the trap.
657 * @internal
658 */
659DECLASM(int) TRPMGCTrap0bHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
660{
661 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
662 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
663 LogFlow(("TRPMGC0b: %04x:%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
664 TRPM_ENTER_DBG_HOOK(0xb);
665 PGMRZDynMapStartAutoSet(pVCpu);
666
667 /*
668 * Try to detect instruction by opcode which caused trap.
669 * XXX note: this code may cause \#PF (trap e) or \#GP (trap d) while
670 * accessing user code. need to handle it somehow in future!
671 */
672 RTGCPTR GCPtr;
673 if ( SELMValidateAndConvertCSAddr(pVCpu, pRegFrame->eflags, pRegFrame->ss.Sel, pRegFrame->cs.Sel, &pRegFrame->cs,
674 (RTGCPTR)pRegFrame->eip, &GCPtr)
675 == VINF_SUCCESS)
676 {
677 uint8_t *pu8Code = (uint8_t *)(uintptr_t)GCPtr;
678
679 /*
680 * First skip possible instruction prefixes, such as:
681 * OS, AS
682 * CS:, DS:, ES:, SS:, FS:, GS:
683 * REPE, REPNE
684 *
685 * note: Currently we supports only up to 4 prefixes per opcode, more
686 * prefixes (normally not used anyway) will cause trap d in guest.
687 * note: Instruction length in IA-32 may be up to 15 bytes, we dont
688 * check this issue, its too hard.
689 */
690 for (unsigned i = 0; i < 4; i++)
691 {
692 if ( pu8Code[0] != 0xf2 /* REPNE/REPNZ */
693 && pu8Code[0] != 0xf3 /* REP/REPE/REPZ */
694 && pu8Code[0] != 0x2e /* CS: */
695 && pu8Code[0] != 0x36 /* SS: */
696 && pu8Code[0] != 0x3e /* DS: */
697 && pu8Code[0] != 0x26 /* ES: */
698 && pu8Code[0] != 0x64 /* FS: */
699 && pu8Code[0] != 0x65 /* GS: */
700 && pu8Code[0] != 0x66 /* OS */
701 && pu8Code[0] != 0x67 /* AS */
702 )
703 break;
704 pu8Code++;
705 }
706
707 /*
708 * Detect right switch using a callgate.
709 *
710 * We recognize the following causes for the trap 0b:
711 * CALL FAR, CALL FAR []
712 * JMP FAR, JMP FAR []
713 * IRET (may cause a task switch)
714 *
715 * Note: we can't detect whether the trap was caused by a call to a
716 * callgate descriptor or it is a real trap 0b due to a bad selector.
717 * In both situations we'll pass execution to our recompiler so we don't
718 * have to worry.
719 * If we wanted to do better detection, we have set GDT entries to callgate
720 * descriptors pointing to our own handlers.
721 */
722 /** @todo not sure about IRET, may generate Trap 0d (\#GP), NEED TO CHECK! */
723 if ( pu8Code[0] == 0x9a /* CALL FAR */
724 || ( pu8Code[0] == 0xff /* CALL FAR [] */
725 && (pu8Code[1] & X86_OPCODE_MODRM_REG_MASK) == 0x18)
726 || pu8Code[0] == 0xea /* JMP FAR */
727 || ( pu8Code[0] == 0xff /* JMP FAR [] */
728 && (pu8Code[1] & X86_OPCODE_MODRM_REG_MASK) == 0x28)
729 || pu8Code[0] == 0xcf /* IRET */
730 )
731 {
732 /*
733 * Got potential call to callgate.
734 * We simply return execution to the recompiler to do emulation
735 * starting from the instruction which caused the trap.
736 */
737 pTrpmCpu->uActiveVector = UINT32_MAX;
738 Log6(("TRPMGC0b: %Rrc (%04x:%08x EFL=%x) (CG)\n", VINF_EM_RAW_RING_SWITCH, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
739 TRPM_EXIT_DBG_HOOK(0xb);
740 PGMRZDynMapReleaseAutoSet(pVCpu);
741 return VINF_EM_RAW_RING_SWITCH;
742 }
743 }
744
745 /*
746 * Pass trap 0b as is to the recompiler in all other cases.
747 */
748 Log6(("TRPMGC0b: %Rrc (%04x:%08x EFL=%x)\n", VINF_EM_RAW_GUEST_TRAP, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
749 PGMRZDynMapReleaseAutoSet(pVCpu);
750 TRPM_EXIT_DBG_HOOK(0xb);
751 return VINF_EM_RAW_GUEST_TRAP;
752}
753
754
755/**
756 * \#GP (General Protection Fault) handler for Ring-0 privileged instructions.
757 *
758 * @returns VBox status code.
759 * VINF_SUCCESS means we completely handled this trap,
760 * other codes are passed execution to host context.
761 *
762 * @param pVM Pointer to the VM.
763 * @param pVCpu Pointer to the VMCPU.
764 * @param pRegFrame Pointer to the register frame for the trap.
765 * @param pCpu The opcode info.
766 * @param PC The program counter corresponding to cs:eip in pRegFrame.
767 */
768static int trpmGCTrap0dHandlerRing0(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR PC)
769{
770 int rc;
771 TRPM_ENTER_DBG_HOOK(0xd);
772
773 /*
774 * Try handle it here, if not return to HC and emulate/interpret it there.
775 */
776 switch (pCpu->pCurInstr->uOpcode)
777 {
778 case OP_INT3:
779 /*
780 * Little hack to make the code below not fail
781 */
782 pCpu->Param1.fUse = DISUSE_IMMEDIATE8;
783 pCpu->Param1.uValue = 3;
784 /* fallthru */
785 case OP_INT:
786 {
787 Assert(pCpu->Param1.fUse & DISUSE_IMMEDIATE8);
788 Assert(!(PATMIsPatchGCAddr(pVM, PC)));
789 if (pCpu->Param1.uValue == 3)
790 {
791 /* Int 3 replacement patch? */
792 if (PATMRCHandleInt3PatchTrap(pVM, pRegFrame) == VINF_SUCCESS)
793 {
794 AssertFailed();
795 return trpmGCExitTrap(pVM, pVCpu, VINF_SUCCESS, pRegFrame);
796 }
797 }
798 rc = TRPMForwardTrap(pVCpu, pRegFrame, (uint32_t)pCpu->Param1.uValue, pCpu->cbInstr, TRPM_TRAP_NO_ERRORCODE, TRPM_SOFTWARE_INT, 0xd);
799 if (RT_SUCCESS(rc) && rc != VINF_EM_RAW_GUEST_TRAP)
800 {
801 TRPM_EXIT_DBG_HOOK(0xd);
802 return trpmGCExitTrap(pVM, pVCpu, VINF_SUCCESS, pRegFrame);
803 }
804
805 pVCpu->trpm.s.uActiveVector = (pVCpu->trpm.s.uActiveErrorCode & X86_TRAP_ERR_SEL_MASK) >> X86_TRAP_ERR_SEL_SHIFT;
806 pVCpu->trpm.s.enmActiveType = TRPM_SOFTWARE_INT;
807 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_RING_SWITCH_INT, pRegFrame);
808 }
809
810#ifdef PATM_EMULATE_SYSENTER
811 case OP_SYSEXIT:
812 case OP_SYSRET:
813 rc = PATMSysCall(pVM, pRegFrame, pCpu);
814 TRPM_EXIT_DBG_HOOK(0xd);
815 return trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
816#endif
817
818 case OP_HLT:
819 /* If it's in patch code, defer to ring-3. */
820 if (PATMIsPatchGCAddr(pVM, PC))
821 break;
822
823 pRegFrame->eip += pCpu->cbInstr;
824 TRPM_EXIT_DBG_HOOK(0xd);
825 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_HALT, pRegFrame);
826
827
828 /*
829 * These instructions are used by PATM and CASM for finding
830 * dangerous non-trapping instructions. Thus, since all
831 * scanning and patching is done in ring-3 we'll have to
832 * return to ring-3 on the first encounter of these instructions.
833 */
834 case OP_MOV_CR:
835 case OP_MOV_DR:
836 /* We can safely emulate control/debug register move instructions in patched code. */
837 if ( !PATMIsPatchGCAddr(pVM, PC)
838 && !CSAMIsKnownDangerousInstr(pVM, PC))
839 break;
840 case OP_INVLPG:
841 case OP_LLDT:
842 case OP_STI:
843 case OP_RDTSC: /* just in case */
844 case OP_RDPMC:
845 case OP_CLTS:
846 case OP_WBINVD: /* nop */
847 case OP_RDMSR:
848 case OP_WRMSR:
849 {
850 rc = EMInterpretInstructionDisasState(pVCpu, pCpu, pRegFrame, PC, EMCODETYPE_SUPERVISOR);
851 if (rc == VERR_EM_INTERPRETER)
852 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
853 TRPM_EXIT_DBG_HOOK(0xd);
854 return trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
855 }
856 }
857
858 TRPM_EXIT_DBG_HOOK(0xd);
859 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EXCEPTION_PRIVILEGED, pRegFrame);
860}
861
862
863/**
864 * \#GP (General Protection Fault) handler for Ring-3.
865 *
866 * @returns VBox status code.
867 * VINF_SUCCESS means we completely handled this trap,
868 * other codes are passed execution to host context.
869 *
870 * @param pVM Pointer to the VM.
871 * @param pVCpu Pointer to the VMCPU.
872 * @param pRegFrame Pointer to the register frame for the trap.
873 * @param pCpu The opcode info.
874 * @param PC The program counter corresponding to cs:eip in pRegFrame.
875 */
876static int trpmGCTrap0dHandlerRing3(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR PC)
877{
878 int rc;
879 Assert(!pRegFrame->eflags.Bits.u1VM);
880 TRPM_ENTER_DBG_HOOK(0xd);
881
882 switch (pCpu->pCurInstr->uOpcode)
883 {
884 /*
885 * INT3 and INT xx are ring-switching.
886 * (The shadow IDT will have set the entries to DPL=0, that's why we're here.)
887 */
888 case OP_INT3:
889 /*
890 * Little hack to make the code below not fail
891 */
892 pCpu->Param1.fUse = DISUSE_IMMEDIATE8;
893 pCpu->Param1.uValue = 3;
894 /* fall thru */
895 case OP_INT:
896 {
897 Assert(pCpu->Param1.fUse & DISUSE_IMMEDIATE8);
898 rc = TRPMForwardTrap(pVCpu, pRegFrame, (uint32_t)pCpu->Param1.uValue, pCpu->cbInstr, TRPM_TRAP_NO_ERRORCODE, TRPM_SOFTWARE_INT, 0xd);
899 if (RT_SUCCESS(rc) && rc != VINF_EM_RAW_GUEST_TRAP)
900 {
901 TRPM_EXIT_DBG_HOOK(0xd);
902 return trpmGCExitTrap(pVM, pVCpu, VINF_SUCCESS, pRegFrame);
903 }
904
905 pVCpu->trpm.s.uActiveVector = (pVCpu->trpm.s.uActiveErrorCode & X86_TRAP_ERR_SEL_MASK) >> X86_TRAP_ERR_SEL_SHIFT;
906 pVCpu->trpm.s.enmActiveType = TRPM_SOFTWARE_INT;
907 TRPM_EXIT_DBG_HOOK(0xd);
908 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_RING_SWITCH_INT, pRegFrame);
909 }
910
911 /*
912 * SYSCALL, SYSENTER, INTO and BOUND are also ring-switchers.
913 */
914 case OP_SYSCALL:
915 case OP_SYSENTER:
916#ifdef PATM_EMULATE_SYSENTER
917 rc = PATMSysCall(pVM, pRegFrame, pCpu);
918 if (rc == VINF_SUCCESS)
919 {
920 TRPM_EXIT_DBG_HOOK(0xd);
921 return trpmGCExitTrap(pVM, pVCpu, VINF_SUCCESS, pRegFrame);
922 }
923 /* else no break; */
924#endif
925 case OP_BOUND:
926 case OP_INTO:
927 pVCpu->trpm.s.uActiveVector = UINT32_MAX;
928 TRPM_EXIT_DBG_HOOK(0xd);
929 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_RING_SWITCH, pRegFrame);
930
931 /*
932 * Handle virtualized TSC & PMC reads, just in case.
933 */
934 case OP_RDTSC:
935 case OP_RDPMC:
936 {
937 rc = EMInterpretInstructionDisasState(pVCpu, pCpu, pRegFrame, PC, EMCODETYPE_SUPERVISOR);
938 if (rc == VERR_EM_INTERPRETER)
939 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
940 TRPM_EXIT_DBG_HOOK(0xd);
941 return trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
942 }
943
944 /*
945 * STI and CLI are I/O privileged, i.e. if IOPL
946 */
947 case OP_STI:
948 case OP_CLI:
949 {
950 uint32_t efl = CPUMRawGetEFlags(pVCpu);
951 if (X86_EFL_GET_IOPL(efl) >= (unsigned)(pRegFrame->ss.Sel & X86_SEL_RPL))
952 {
953 LogFlow(("trpmGCTrap0dHandlerRing3: CLI/STI -> REM\n"));
954 TRPM_EXIT_DBG_HOOK(0xd);
955 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RESCHEDULE_REM, pRegFrame);
956 }
957 LogFlow(("trpmGCTrap0dHandlerRing3: CLI/STI -> #GP(0)\n"));
958 break;
959 }
960 }
961
962 /*
963 * A genuine guest fault.
964 */
965 TRPM_EXIT_DBG_HOOK(0xd);
966 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_GUEST_TRAP, pRegFrame);
967}
968
969
970/**
971 * Emulates RDTSC for the \#GP handler.
972 *
973 * @returns VINF_SUCCESS or VINF_EM_RAW_EMULATE_INSTR.
974 *
975 * @param pVM Pointer to the VM.
976 * @param pVCpu Pointer to the VMCPU.
977 * @param pRegFrame Pointer to the register frame for the trap.
978 * This will be updated on successful return.
979 */
980DECLINLINE(int) trpmGCTrap0dHandlerRdTsc(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame)
981{
982 STAM_COUNTER_INC(&pVM->trpm.s.StatTrap0dRdTsc);
983 TRPM_ENTER_DBG_HOOK(0xd);
984
985 if (CPUMGetGuestCR4(pVCpu) & X86_CR4_TSD)
986 {
987 TRPM_EXIT_DBG_HOOK(0xd);
988 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EMULATE_INSTR, pRegFrame); /* will trap (optimize later). */
989 }
990
991 uint64_t uTicks = TMCpuTickGet(pVCpu);
992 pRegFrame->eax = uTicks;
993 pRegFrame->edx = uTicks >> 32;
994 pRegFrame->eip += 2;
995 TRPM_EXIT_DBG_HOOK(0xd);
996 return trpmGCExitTrap(pVM, pVCpu, VINF_SUCCESS, pRegFrame);
997}
998
999
1000/**
1001 * \#GP (General Protection Fault) handler.
1002 *
1003 * @returns VBox status code.
1004 * VINF_SUCCESS means we completely handled this trap,
1005 * other codes are passed execution to host context.
1006 *
1007 * @param pVM Pointer to the VM.
1008 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1009 * @param pRegFrame Pointer to the register frame for the trap.
1010 */
1011static int trpmGCTrap0dHandler(PVM pVM, PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1012{
1013 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
1014 LogFlow(("trpmGCTrap0dHandler: cs:eip=%RTsel:%08RX32 uErr=%RGv EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, pTrpmCpu->uActiveErrorCode, CPUMRawGetEFlags(pVCpu)));
1015 TRPM_ENTER_DBG_HOOK(0xd);
1016
1017 /*
1018 * Convert and validate CS.
1019 */
1020 STAM_PROFILE_START(&pVM->trpm.s.StatTrap0dDisasm, a);
1021 RTGCPTR PC;
1022 int rc = SELMValidateAndConvertCSAddr(pVCpu, pRegFrame->eflags, pRegFrame->ss.Sel, pRegFrame->cs.Sel, &pRegFrame->cs,
1023 pRegFrame->rip, &PC);
1024 if (RT_FAILURE(rc))
1025 {
1026 Log(("trpmGCTrap0dHandler: Failed to convert %RTsel:%RX32 (cpl=%d) - rc=%Rrc !!\n",
1027 pRegFrame->cs.Sel, pRegFrame->eip, pRegFrame->ss.Sel & X86_SEL_RPL, rc));
1028 TRPM_EXIT_DBG_HOOK(0xd);
1029 STAM_PROFILE_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
1030 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
1031 }
1032
1033 /*
1034 * Disassemble the instruction.
1035 */
1036 DISCPUSTATE Cpu;
1037 uint32_t cbOp;
1038 rc = EMInterpretDisasOneEx(pVM, pVCpu, PC, pRegFrame, &Cpu, &cbOp);
1039 if (RT_FAILURE(rc))
1040 {
1041 AssertMsgFailed(("DISCoreOneEx failed to PC=%RGv rc=%Rrc\n", PC, rc));
1042 TRPM_EXIT_DBG_HOOK(0xd);
1043 STAM_PROFILE_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
1044 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
1045 }
1046 STAM_PROFILE_STOP(&pVM->trpm.s.StatTrap0dDisasm, a);
1047
1048 /*
1049 * Optimize RDTSC traps.
1050 * Some guests (like Solaris) are using RDTSC all over the place and
1051 * will end up trapping a *lot* because of that.
1052 *
1053 * Note: it's no longer safe to access the instruction opcode directly due to possible stale code TLB entries
1054 */
1055 if (Cpu.pCurInstr->uOpcode == OP_RDTSC)
1056 return trpmGCTrap0dHandlerRdTsc(pVM, pVCpu, pRegFrame);
1057
1058 /*
1059 * Deal with I/O port access.
1060 */
1061 if ( pVCpu->trpm.s.uActiveErrorCode == 0
1062 && (Cpu.pCurInstr->fOpType & DISOPTYPE_PORTIO))
1063 {
1064 VBOXSTRICTRC rcStrict = IOMRCIOPortHandler(pVM, pRegFrame, &Cpu);
1065 if (IOM_SUCCESS(rcStrict))
1066 pRegFrame->rip += cbOp;
1067 rc = VBOXSTRICTRC_TODO(rcStrict);
1068 TRPM_EXIT_DBG_HOOK(0xd);
1069 return trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
1070 }
1071
1072 /*
1073 * Deal with Ring-0 (privileged instructions)
1074 */
1075 if ( (pRegFrame->ss.Sel & X86_SEL_RPL) <= 1
1076 && !pRegFrame->eflags.Bits.u1VM)
1077 return trpmGCTrap0dHandlerRing0(pVM, pVCpu, pRegFrame, &Cpu, PC);
1078
1079 /*
1080 * Deal with Ring-3 GPs.
1081 */
1082 if (!pRegFrame->eflags.Bits.u1VM)
1083 return trpmGCTrap0dHandlerRing3(pVM, pVCpu, pRegFrame, &Cpu, PC);
1084
1085 /*
1086 * Deal with v86 code.
1087 *
1088 * We always set IOPL to zero which makes e.g. pushf fault in V86
1089 * mode. The guest might use IOPL=3 and therefore not expect a #GP.
1090 * Simply fall back to the recompiler to emulate this instruction if
1091 * that's the case. To get the correct we must use CPUMRawGetEFlags.
1092 */
1093 X86EFLAGS eflags;
1094 eflags.u32 = CPUMRawGetEFlags(pVCpu); /* Get the correct value. */
1095 Log3(("TRPM #GP V86: cs:eip=%04x:%08x IOPL=%d efl=%08x\n", pRegFrame->cs.Sel, pRegFrame->eip, eflags.Bits.u2IOPL, eflags.u));
1096 if (eflags.Bits.u2IOPL != 3)
1097 {
1098 Assert(eflags.Bits.u2IOPL == 0);
1099
1100 rc = TRPMForwardTrap(pVCpu, pRegFrame, 0xD, 0, TRPM_TRAP_HAS_ERRORCODE, TRPM_TRAP, 0xd);
1101 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
1102 TRPM_EXIT_DBG_HOOK(0xd);
1103 return trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
1104 }
1105 TRPM_EXIT_DBG_HOOK(0xd);
1106 return trpmGCExitTrap(pVM, pVCpu, VINF_EM_RAW_EMULATE_INSTR, pRegFrame);
1107}
1108
1109
1110/**
1111 * \#GP (General Protection Fault) handler.
1112 *
1113 * @returns VBox status code.
1114 * VINF_SUCCESS means we completely handled this trap,
1115 * other codes are passed execution to host context.
1116 *
1117 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1118 * @param pRegFrame Pointer to the register frame for the trap.
1119 * @internal
1120 */
1121DECLASM(int) TRPMGCTrap0dHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1122{
1123 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
1124 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
1125 LogFlow(("TRPMGC0d: %04x:%08x err=%x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, (uint32_t)pVCpu->trpm.s.uActiveErrorCode, CPUMRawGetEFlags(pVCpu)));
1126 TRPM_ENTER_DBG_HOOK(0xd);
1127
1128 PGMRZDynMapStartAutoSet(pVCpu);
1129 int rc = trpmGCTrap0dHandler(pVM, pTrpmCpu, pRegFrame);
1130 switch (rc)
1131 {
1132 case VINF_EM_RAW_GUEST_TRAP:
1133 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
1134 if (PATMIsPatchGCAddr(pVM, pRegFrame->eip))
1135 rc = VINF_PATM_PATCH_TRAP_GP;
1136 break;
1137
1138 case VINF_EM_RAW_INTERRUPT_PENDING:
1139 Assert(TRPMHasTrap(pVCpu));
1140 /* no break; */
1141 case VINF_PGM_SYNC_CR3:
1142 case VINF_EM_RAW_EMULATE_INSTR:
1143 case VINF_IOM_R3_IOPORT_READ:
1144 case VINF_IOM_R3_IOPORT_WRITE:
1145 case VINF_IOM_R3_MMIO_WRITE:
1146 case VINF_IOM_R3_MMIO_READ:
1147 case VINF_IOM_R3_MMIO_READ_WRITE:
1148 case VINF_PATM_PATCH_INT3:
1149 case VINF_EM_NO_MEMORY:
1150 case VINF_EM_RAW_TO_R3:
1151 case VINF_EM_RAW_TIMER_PENDING:
1152 case VINF_EM_PENDING_REQUEST:
1153 case VINF_EM_HALT:
1154 case VINF_SELM_SYNC_GDT:
1155 case VINF_SUCCESS:
1156 break;
1157
1158 default:
1159 AssertMsg(PATMIsPatchGCAddr(pVM, pRegFrame->eip) == false, ("return code %d\n", rc));
1160 break;
1161 }
1162 Log6(("TRPMGC0d: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
1163 TRPM_EXIT_DBG_HOOK(0xd);
1164 return rc;
1165}
1166
1167
1168/**
1169 * \#PF (Page Fault) handler.
1170 *
1171 * Calls PGM which does the actual handling.
1172 *
1173 *
1174 * @returns VBox status code.
1175 * VINF_SUCCESS means we completely handled this trap,
1176 * other codes are passed execution to host context.
1177 *
1178 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1179 * @param pRegFrame Pointer to the register frame for the trap.
1180 * @internal
1181 */
1182DECLASM(int) TRPMGCTrap0eHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1183{
1184 PVM pVM = TRPMCPU_2_VM(pTrpmCpu);
1185 PVMCPU pVCpu = TRPMCPU_2_VMCPU(pTrpmCpu);
1186 LogFlow(("TRPMGC0e: %04x:%08x err=%x cr2=%08x EFL=%x\n", pRegFrame->cs.Sel, pRegFrame->eip, (uint32_t)pVCpu->trpm.s.uActiveErrorCode, (uint32_t)pVCpu->trpm.s.uActiveCR2, CPUMRawGetEFlags(pVCpu)));
1187 TRPM_ENTER_DBG_HOOK(0xe);
1188
1189 /*
1190 * This is all PGM stuff.
1191 */
1192 PGMRZDynMapStartAutoSet(pVCpu);
1193 int rc = PGMTrap0eHandler(pVCpu, pVCpu->trpm.s.uActiveErrorCode, pRegFrame, (RTGCPTR)pVCpu->trpm.s.uActiveCR2);
1194 switch (rc)
1195 {
1196 case VINF_EM_RAW_EMULATE_INSTR:
1197 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
1198 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
1199 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
1200 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
1201 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
1202 if (PATMIsPatchGCAddr(pVM, pRegFrame->eip))
1203 rc = VINF_PATCH_EMULATE_INSTR;
1204 break;
1205
1206 case VINF_EM_RAW_GUEST_TRAP:
1207 if (PATMIsPatchGCAddr(pVM, pRegFrame->eip))
1208 {
1209 PGMRZDynMapReleaseAutoSet(pVCpu);
1210 TRPM_EXIT_DBG_HOOK(0xe);
1211 return VINF_PATM_PATCH_TRAP_PF;
1212 }
1213
1214 rc = TRPMForwardTrap(pVCpu, pRegFrame, 0xE, 0, TRPM_TRAP_HAS_ERRORCODE, TRPM_TRAP, 0xe);
1215 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
1216 break;
1217
1218 case VINF_EM_RAW_INTERRUPT_PENDING:
1219 Assert(TRPMHasTrap(pVCpu));
1220 /* no break; */
1221 case VINF_IOM_R3_MMIO_READ:
1222 case VINF_IOM_R3_MMIO_WRITE:
1223 case VINF_IOM_R3_MMIO_READ_WRITE:
1224 case VINF_PATM_HC_MMIO_PATCH_READ:
1225 case VINF_PATM_HC_MMIO_PATCH_WRITE:
1226 case VINF_SUCCESS:
1227 case VINF_EM_RAW_TO_R3:
1228 case VINF_EM_PENDING_REQUEST:
1229 case VINF_EM_RAW_TIMER_PENDING:
1230 case VINF_EM_NO_MEMORY:
1231 case VINF_CSAM_PENDING_ACTION:
1232 case VINF_PGM_SYNC_CR3: /** @todo Check this with Sander. */
1233 break;
1234
1235 default:
1236 AssertMsg(PATMIsPatchGCAddr(pVM, pRegFrame->eip) == false, ("Patch address for return code %d. eip=%08x\n", rc, pRegFrame->eip));
1237 break;
1238 }
1239 rc = trpmGCExitTrap(pVM, pVCpu, rc, pRegFrame);
1240 Log6(("TRPMGC0e: %Rrc (%04x:%08x EFL=%x)\n", rc, pRegFrame->cs.Sel, pRegFrame->eip, CPUMRawGetEFlags(pVCpu)));
1241 TRPM_EXIT_DBG_HOOK(0xe);
1242 return rc;
1243}
1244
1245
1246/**
1247 * Scans for the EIP in the specified array of trap handlers.
1248 *
1249 * If we don't fine the EIP, we'll panic.
1250 *
1251 * @returns VBox status code.
1252 *
1253 * @param pVM Pointer to the VM.
1254 * @param pRegFrame Pointer to the register frame for the trap.
1255 * @param paHandlers The array of trap handler records.
1256 * @param pEndRecord The end record (exclusive).
1257 */
1258static int trpmGCHyperGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, PCTRPMGCHYPER paHandlers, PCTRPMGCHYPER pEndRecord)
1259{
1260 uintptr_t uEip = (uintptr_t)pRegFrame->eip;
1261 Assert(paHandlers <= pEndRecord);
1262
1263 Log(("trpmGCHyperGeneric: uEip=%x %p-%p\n", uEip, paHandlers, pEndRecord));
1264
1265#if 0 /// @todo later
1266 /*
1267 * Start by doing a kind of binary search.
1268 */
1269 unsigned iStart = 0;
1270 unsigned iEnd = pEndRecord - paHandlers;
1271 unsigned i = iEnd / 2;
1272#endif
1273
1274 /*
1275 * Do a linear search now (in case the array wasn't properly sorted).
1276 */
1277 for (PCTRPMGCHYPER pCur = paHandlers; pCur < pEndRecord; pCur++)
1278 {
1279 if ( pCur->uStartEIP <= uEip
1280 && (pCur->uEndEIP ? pCur->uEndEIP > uEip : pCur->uStartEIP == uEip))
1281 return pCur->pfnHandler(pVM, pRegFrame, pCur->uUser);
1282 }
1283
1284 return VERR_TRPM_DONT_PANIC;
1285}
1286
1287
1288/**
1289 * Hypervisor \#NP ((segment) Not Present) handler.
1290 *
1291 * Scans for the EIP in the registered trap handlers.
1292 *
1293 * @returns VBox status code.
1294 * VINF_SUCCESS means we completely handled this trap,
1295 * other codes are passed back to host context.
1296 *
1297 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1298 * @param pRegFrame Pointer to the register frame for the trap.
1299 * @internal
1300 */
1301DECLASM(int) TRPMGCHyperTrap0bHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1302{
1303 return trpmGCHyperGeneric(TRPMCPU_2_VM(pTrpmCpu), pRegFrame, g_aTrap0bHandlers, g_aTrap0bHandlersEnd);
1304}
1305
1306
1307/**
1308 * Hypervisor \#GP (General Protection Fault) handler.
1309 *
1310 * Scans for the EIP in the registered trap handlers.
1311 *
1312 * @returns VBox status code.
1313 * VINF_SUCCESS means we completely handled this trap,
1314 * other codes are passed back to host context.
1315 *
1316 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1317 * @param pRegFrame Pointer to the register frame for the trap.
1318 * @internal
1319 */
1320DECLASM(int) TRPMGCHyperTrap0dHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1321{
1322 return trpmGCHyperGeneric(TRPMCPU_2_VM(pTrpmCpu), pRegFrame, g_aTrap0dHandlers, g_aTrap0dHandlersEnd);
1323}
1324
1325
1326/**
1327 * Hypervisor \#PF (Page Fault) handler.
1328 *
1329 * Scans for the EIP in the registered trap handlers.
1330 *
1331 * @returns VBox status code.
1332 * VINF_SUCCESS means we completely handled this trap,
1333 * other codes are passed back to host context.
1334 *
1335 * @param pTrpmCpu Pointer to TRPMCPU data (within VM).
1336 * @param pRegFrame Pointer to the register frame for the trap.
1337 * @internal
1338 */
1339DECLASM(int) TRPMGCHyperTrap0eHandler(PTRPMCPU pTrpmCpu, PCPUMCTXCORE pRegFrame)
1340{
1341 return trpmGCHyperGeneric(TRPMCPU_2_VM(pTrpmCpu), pRegFrame, g_aTrap0dHandlers, g_aTrap0dHandlersEnd);
1342}
1343
1344
1345/**
1346 * Deal with hypervisor traps occurring when resuming execution on a trap.
1347 *
1348 * There is a little problem with recursive RC (hypervisor) traps. We deal with
1349 * this by not allowing recursion without it being the subject of a guru
1350 * meditation. (We used to / tried to handle this but there isn't any reason
1351 * for it.)
1352 *
1353 * So, do NOT use this for handling RC traps!
1354 *
1355 * @returns VBox status code. (Anything but VINF_SUCCESS will cause guru.)
1356 * @param pVM Pointer to the VM.
1357 * @param pRegFrame Register frame.
1358 * @param uUser User arg.
1359 */
1360DECLCALLBACK(int) trpmRCTrapInGeneric(PVM pVM, PCPUMCTXCORE pRegFrame, uintptr_t uUser)
1361{
1362 Log(("********************************************************\n"));
1363 Log(("trpmRCTrapInGeneric: eip=%RX32 uUser=%#x\n", pRegFrame->eip, uUser));
1364 Log(("********************************************************\n"));
1365
1366 /*
1367 * This used to be kind of complicated, but since we stopped storing
1368 * the register frame on the stack and instead storing it directly
1369 * in the CPUMCPU::Guest structure, we just have to figure out which
1370 * status to hand on to the host and let the recompiler/IEM do its
1371 * job.
1372 */
1373 switch (uUser)
1374 {
1375 case TRPM_TRAP_IN_MOV_GS:
1376 case TRPM_TRAP_IN_MOV_FS:
1377 case TRPM_TRAP_IN_MOV_ES:
1378 case TRPM_TRAP_IN_MOV_DS:
1379 TRPMGCHyperReturnToHost(pVM, VINF_EM_RAW_STALE_SELECTOR);
1380 break;
1381
1382 case TRPM_TRAP_IN_IRET:
1383 case TRPM_TRAP_IN_IRET | TRPM_TRAP_IN_V86:
1384 TRPMGCHyperReturnToHost(pVM, VINF_EM_RAW_IRET_TRAP);
1385 break;
1386
1387 default:
1388 AssertMsgFailed(("Invalid uUser=%#x\n", uUser));
1389 return VERR_TRPM_BAD_TRAP_IN_OP;
1390 }
1391
1392 AssertMsgFailed(("Impossible!\n"));
1393 return VERR_TRPM_IPE_3;
1394}
1395
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