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