VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TRPMAll.cpp@ 1507

Last change on this file since 1507 was 1500, checked in by vboxsync, 18 years ago

Added virtual interrupt redirection bitmap to our TSS

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 32.1 KB
Line 
1/* $Id: TRPMAll.cpp 1500 2007-03-15 09:20:43Z vboxsync $ */
2/** @file
3 * TRPM - Trap Monitor - Any Context.
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/trpm.h>
28#include <VBox/pgm.h>
29#include <VBox/mm.h>
30#include <VBox/patm.h>
31#include <VBox/selm.h>
32#include "TRPMInternal.h"
33#include <VBox/vm.h>
34#include <VBox/err.h>
35#include <VBox/x86.h>
36#include <VBox/em.h>
37
38#include <VBox/log.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/param.h>
42
43
44
45/**
46 * Query info about the current active trap/interrupt.
47 * If no trap is active active an error code is returned.
48 *
49 * @returns VBox status code.
50 * @param pVM The virtual machine.
51 * @param pu8TrapNo Where to store the trap number.
52 * @param pfSoftwareInterrupt Where to store the software interrupt indicator.
53 */
54TRPMDECL(int) TRPMQueryTrap(PVM pVM, uint8_t *pu8TrapNo, bool *pfSoftwareInterrupt)
55{
56 /*
57 * Check if we have a trap at present.
58 */
59 if (pVM->trpm.s.uActiveVector != ~0U)
60 {
61 if (pu8TrapNo)
62 *pu8TrapNo = (uint8_t)pVM->trpm.s.uActiveVector;
63 if (pfSoftwareInterrupt)
64 *pfSoftwareInterrupt = !!pVM->trpm.s.fActiveSoftwareInterrupt;
65 return VINF_SUCCESS;
66 }
67
68 return VERR_TRPM_NO_ACTIVE_TRAP;
69}
70
71
72/**
73 * Gets the trap number for the current trap.
74 *
75 * The caller is responsible for making sure there is an active trap which
76 * takes an error code when making this request.
77 *
78 * @returns The current trap number.
79 * @param pVM VM handle.
80 */
81TRPMDECL(uint8_t) TRPMGetTrapNo(PVM pVM)
82{
83 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
84 return (uint8_t)pVM->trpm.s.uActiveVector;
85}
86
87
88/**
89 * Gets the error code for the current trap.
90 *
91 * The caller is responsible for making sure there is an active trap which
92 * takes an error code when making this request.
93 *
94 * @returns Error code.
95 * @param pVM VM handle.
96 */
97TRPMDECL(RTGCUINT) TRPMGetErrorCode(PVM pVM)
98{
99 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
100#ifdef VBOX_STRICT
101 switch (pVM->trpm.s.uActiveVector)
102 {
103 case 0x0a:
104 case 0x0b:
105 case 0x0c:
106 case 0x0d:
107 case 0x0e:
108 case 0x11:
109 case 0x08:
110 break;
111 default:
112 AssertMsgFailed(("This trap (%#x) doesn't have any error code\n", pVM->trpm.s.uActiveVector));
113 break;
114 }
115#endif
116 return pVM->trpm.s.uActiveErrorCode;
117}
118
119
120/**
121 * Gets the fault address for the current trap.
122 *
123 * The caller is responsible for making sure there is an active trap 0x0e when
124 * making this request.
125 *
126 * @returns Fault address associated with the trap.
127 * @param pVM VM handle.
128 */
129TRPMDECL(RTGCUINTPTR) TRPMGetFaultAddress(PVM pVM)
130{
131 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
132 AssertMsg(pVM->trpm.s.uActiveVector == 0xe, ("Not trap 0e!\n"));
133 return pVM->trpm.s.uActiveCR2;
134}
135
136
137/**
138 * Clears the current active trap/exception/interrupt.
139 *
140 * The caller is responsible for making sure there is an active trap
141 * when making this request.
142 *
143 * @returns VBox status code.
144 * @param pVM The virtual machine handle.
145 */
146TRPMDECL(int) TRPMResetTrap(PVM pVM)
147{
148 /*
149 * Cannot reset non-existing trap!
150 */
151 if (pVM->trpm.s.uActiveVector == ~0U)
152 {
153 AssertMsgFailed(("No active trap!\n"));
154 return VERR_TRPM_NO_ACTIVE_TRAP;
155 }
156
157 /*
158 * Reset it.
159 */
160 pVM->trpm.s.uActiveVector = ~0U;
161 return VINF_SUCCESS;
162}
163
164
165/**
166 * Assert trap/exception/interrupt.
167 *
168 * The caller is responsible for making sure there is no active trap
169 * when making this request.
170 *
171 * @returns VBox status code.
172 * @param pVM The virtual machine.
173 * @param u8TrapNo The trap vector to assert.
174 * @param fSoftwareInterrupt Indicate if it's a software interrupt or not.
175 */
176TRPMDECL(int) TRPMAssertTrap(PVM pVM, uint8_t u8TrapNo, bool fSoftwareInterrupt)
177{
178 Log2(("TRPMAssertTrap: u8TrapNo=%02x fSoftwareInterrupt=%d\n", u8TrapNo, fSoftwareInterrupt));
179
180 /*
181 * Cannot assert a trap when one is already active.
182 */
183 if (pVM->trpm.s.uActiveVector != ~0U)
184 {
185 AssertMsgFailed(("Active trap %#x\n", pVM->trpm.s.uActiveVector));
186 return VERR_TRPM_ACTIVE_TRAP;
187 }
188
189 pVM->trpm.s.uActiveVector = u8TrapNo;
190 pVM->trpm.s.fActiveSoftwareInterrupt = fSoftwareInterrupt;
191 pVM->trpm.s.uActiveErrorCode = ~0;
192 pVM->trpm.s.uActiveCR2 = 0xdeadface;
193 return VINF_SUCCESS;
194}
195
196
197/**
198 * Sets the error code of the current trap.
199 * (This function is for use in trap handlers and such.)
200 *
201 * The caller is responsible for making sure there is an active trap
202 * which takes an errorcode when making this request.
203 *
204 * @param pVM The virtual machine.
205 * @param uErrorCode The new error code.
206 */
207TRPMDECL(void) TRPMSetErrorCode(PVM pVM, RTGCUINT uErrorCode)
208{
209 Log2(("TRPMSetErrorCode: uErrorCode=%VGv\n", uErrorCode));
210 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
211 pVM->trpm.s.uActiveErrorCode = uErrorCode;
212#ifdef VBOX_STRICT
213 switch (pVM->trpm.s.uActiveVector)
214 {
215 case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e:
216 AssertMsg(uErrorCode != ~(RTGCUINT)0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVM->trpm.s.uActiveVector));
217 break;
218 case 0x11: case 0x08:
219 AssertMsg(uErrorCode == 0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVM->trpm.s.uActiveVector));
220 break;
221 default:
222 AssertMsg(uErrorCode == ~(RTGCUINT)0, ("Invalid uErrorCode=%#x u8TrapNo=%d\n", uErrorCode, pVM->trpm.s.uActiveVector));
223 break;
224 }
225#endif
226}
227
228
229/**
230 * Sets the error code of the current trap.
231 * (This function is for use in trap handlers and such.)
232 *
233 * The caller is responsible for making sure there is an active trap 0e
234 * when making this request.
235 *
236 * @param pVM The virtual machine.
237 * @param uCR2 The new fault address (cr2 register).
238 */
239TRPMDECL(void) TRPMSetFaultAddress(PVM pVM, RTGCUINTPTR uCR2)
240{
241 Log2(("TRPMSetFaultAddress: uCR2=%VGv\n", uCR2));
242 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
243 AssertMsg(pVM->trpm.s.uActiveVector == 0xe, ("Not trap 0e!\n"));
244 pVM->trpm.s.uActiveCR2 = uCR2;
245}
246
247
248/**
249 * Checks if the current active trap/interrupt/exception/fault/whatever is a software
250 * interrupt or not.
251 *
252 * The caller is responsible for making sure there is an active trap
253 * when making this request.
254 *
255 * @returns true if software interrupt, false if not.
256 *
257 * @param pVM VM handle.
258 */
259TRPMDECL(bool) TRPMIsSoftwareInterrupt(PVM pVM)
260{
261 AssertMsg(pVM->trpm.s.uActiveVector != ~0U, ("No active trap!\n"));
262 return !!pVM->trpm.s.fActiveSoftwareInterrupt;
263}
264
265
266/**
267 * Check if there is an active trap.
268 *
269 * @returns true if trap active, false if not.
270 * @param pVM The virtual machine.
271 */
272TRPMDECL(bool) TRPMHasTrap(PVM pVM)
273{
274 return pVM->trpm.s.uActiveVector != ~0U;
275}
276
277
278/**
279 * Query all info about the current active trap/interrupt.
280 * If no trap is active active an error code is returned.
281 *
282 * @returns VBox status code.
283 * @param pVM The virtual machine.
284 * @param pu8TrapNo Where to store the trap number.
285 * @param pfSoftwareInterrupt Where to store the software interrupt indicator.
286 * @param puErrorCode Where to store the error code associated with some traps.
287 * ~0U is stored if the trap have no error code.
288 * @param puCR2 Where to store the CR2 associated with a trap 0E.
289 */
290TRPMDECL(int) TRPMQueryTrapAll(PVM pVM, uint8_t *pu8TrapNo, bool *pfSoftwareInterrupt, PRTGCUINT puErrorCode, PRTGCUINTPTR puCR2)
291{
292 /*
293 * Check if we have a trap at present.
294 */
295 if (pVM->trpm.s.uActiveVector == ~0U)
296 return VERR_TRPM_NO_ACTIVE_TRAP;
297
298 if (pu8TrapNo)
299 *pu8TrapNo = (uint8_t)pVM->trpm.s.uActiveVector;
300 if (pfSoftwareInterrupt)
301 *pfSoftwareInterrupt = !!pVM->trpm.s.fActiveSoftwareInterrupt;
302 if (puErrorCode)
303 *puErrorCode = pVM->trpm.s.uActiveErrorCode;
304 if (puCR2)
305 *puCR2 = pVM->trpm.s.uActiveCR2;
306
307 return VINF_SUCCESS;
308}
309
310
311/**
312 * Save the active trap.
313 *
314 * This routine useful when doing try/catch in the hypervisor.
315 * Any function which uses temporary trap handlers should
316 * probably also use this facility to save the original trap.
317 *
318 * @param pVM VM handle.
319 */
320TRPMDECL(void) TRPMSaveTrap(PVM pVM)
321{
322 pVM->trpm.s.uSavedVector = pVM->trpm.s.uActiveVector;
323 pVM->trpm.s.fSavedSoftwareInterrupt = pVM->trpm.s.fActiveSoftwareInterrupt;
324 pVM->trpm.s.uSavedErrorCode = pVM->trpm.s.uActiveErrorCode;
325 pVM->trpm.s.uSavedCR2 = pVM->trpm.s.uActiveCR2;
326}
327
328
329/**
330 * Restore a saved trap.
331 *
332 * Multiple restores of a saved trap is possible.
333 *
334 * @param pVM VM handle.
335 */
336TRPMDECL(void) TRPMRestoreTrap(PVM pVM)
337{
338 pVM->trpm.s.uActiveVector = pVM->trpm.s.uSavedVector;
339 pVM->trpm.s.fActiveSoftwareInterrupt = pVM->trpm.s.fSavedSoftwareInterrupt;
340 pVM->trpm.s.uActiveErrorCode = pVM->trpm.s.uSavedErrorCode;
341 pVM->trpm.s.uActiveCR2 = pVM->trpm.s.uSavedCR2;
342}
343
344
345/**
346 * Forward trap or interrupt to the guest's handler
347 *
348 *
349 * @returns VBox status code.
350 * or does not return at all (when the trap is actually forwarded)
351 *
352 * @param pVM The VM to operate on.
353 * @param pRegFrame Pointer to the register frame for the trap.
354 * @param iGate Trap or interrupt gate number
355 * @param opsize Instruction size (only relevant for software interrupts)
356 * @param enmError TRPM_TRAP_HAS_ERRORCODE or TRPM_TRAP_NO_ERRORCODE.
357 * @param enmType TRPM event type
358 * @internal
359 */
360TRPMDECL(int) TRPMForwardTrap(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t iGate, uint32_t opsize, TRPMERRORCODE enmError, TRPMEVENT enmType)
361{
362#ifdef TRPM_FORWARD_TRAPS_IN_GC
363 X86EFLAGS eflags;
364
365 STAM_PROFILE_ADV_START(CTXSUFF(&pVM->trpm.s.StatForwardProf), a);
366
367#ifdef DEBUG
368 if (pRegFrame->eflags.Bits.u1VM)
369 Log(("TRPMForwardTrap-VM: eip=%04X:%04X iGate=%d\n", pRegFrame->cs, pRegFrame->eip, iGate));
370 else
371 Log(("TRPMForwardTrap: eip=%04X:%VGv iGate=%d\n", pRegFrame->cs, pRegFrame->eip, iGate));
372
373 switch (iGate) {
374 case 14:
375 if (pRegFrame->eip == pVM->trpm.s.uActiveCR2)
376 {
377 int rc;
378 RTGCPTR pCallerGC;
379#ifdef IN_GC
380 rc = MMGCRamRead(pVM, &pCallerGC, (RTGCPTR)pRegFrame->esp, sizeof(pCallerGC));
381#else
382 rc = PGMPhysReadGCPtr(pVM, &pCallerGC, (RTGCPTR)pRegFrame->esp, sizeof(pCallerGC));
383#endif
384 if (VBOX_SUCCESS(rc))
385 {
386 Log(("TRPMForwardTrap: caller=%VGv\n", pCallerGC));
387 }
388 }
389 /* no break */
390 case 8:
391 case 10:
392 case 11:
393 case 12:
394 case 13:
395 case 17:
396 Assert(enmError == TRPM_TRAP_HAS_ERRORCODE);
397 break;
398 default:
399 Assert(enmError == TRPM_TRAP_NO_ERRORCODE);
400 break;
401 }
402#endif /* DEBUG */
403
404 /* Retrieve the eflags including the virtualized bits. */
405 /** @note hackish as the cpumctxcore structure doesn't contain the right value */
406 eflags.u32 = CPUMRawGetEFlags(pVM, pRegFrame);
407
408 /* VM_FF_INHIBIT_INTERRUPTS should be cleared upfront or don't call this function at all for dispatching hardware interrupts. */
409 Assert(enmType != TRPM_HARDWARE_INT || !VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
410
411 /*
412 * If it's a real guest trap and the guest's page fault handler is marked as safe for GC execution, then we call it directly.
413 * Well, only if the IF flag is set.
414 */
415 /*
416 * @todo if the trap handler was modified and marked invalid, then we should *now* go back to the host context and install a new patch.
417 *
418 */
419 if ( pVM->trpm.s.aGuestTrapHandler[iGate]
420 && (eflags.Bits.u1IF)
421#ifndef VBOX_RAW_V86
422 && !(eflags.Bits.u1VM) /* @todo implement when needed (illegal for same privilege level transfers). */
423#endif
424 && !PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip)
425 )
426 {
427 uint16_t cbIDT;
428 RTGCPTR GCPtrIDT = (RTGCPTR)CPUMGetGuestIDTR(pVM, &cbIDT);
429 uint32_t cpl;
430 VBOXIDTE GuestIdte;
431 RTGCPTR pIDTEntry;
432 int rc;
433
434 Assert(PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame));
435
436 /* Must get the CPL from the SS selector (CS might be conforming) */
437 if (eflags.Bits.u1VM)
438 cpl = 3;
439 else
440 if ((pRegFrame->ss & X86_SEL_RPL) == 1)
441 cpl = 0;
442 else
443 cpl = (pRegFrame->ss & X86_SEL_RPL);
444
445 if (GCPtrIDT && iGate * sizeof(VBOXIDTE) >= cbIDT)
446 goto failure;
447
448 /*
449 * BIG TODO: The checks are not complete. see trap and interrupt dispatching section in Intel docs for details
450 * All very obscure, but still necessary.
451 * Currently only some CS & TSS selector checks are missing.
452 *
453 */
454 pIDTEntry = (RTGCPTR)((RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate);
455#ifdef IN_GC
456 rc = MMGCRamRead(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
457#else
458 rc = PGMPhysReadGCPtr(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
459#endif
460 if (VBOX_FAILURE(rc))
461 {
462 /* The page might be out of sync. (@todo might cross a page boundary) */
463 Log(("Page %VGv out of sync -> prefetch and try again\n", pIDTEntry));
464 rc = PGMPrefetchPage(pVM, pIDTEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
465 if (rc != VINF_SUCCESS)
466 {
467 Log(("TRPMForwardTrap: PGMPrefetchPage failed with rc=%Vrc\n", rc));
468 goto failure;
469 }
470#ifdef IN_GC
471 rc = MMGCRamRead(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
472#else
473 rc = PGMPhysReadGCPtr(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
474#endif
475 }
476 if ( VBOX_SUCCESS(rc)
477 && GuestIdte.Gen.u1Present
478 && (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
479 && (GuestIdte.Gen.u2DPL == 3 || GuestIdte.Gen.u2DPL == 0)
480 && (GuestIdte.Gen.u16SegSel & 0xfffc) /* must not be zero */
481 && (enmType == TRPM_TRAP || enmType == TRPM_HARDWARE_INT || cpl <= GuestIdte.Gen.u2DPL) /* CPL <= DPL if software int */
482 )
483 {
484 RTGCPTR pHandler, dummy;
485 GCPTRTYPE(uint32_t *)pTrapStackGC;
486#ifndef IN_GC
487 HCPTRTYPE(uint32_t *)pTrapStackHC;
488#endif
489
490 pHandler = (RTGCPTR)((GuestIdte.Gen.u16OffsetHigh << 16) | GuestIdte.Gen.u16OffsetLow);
491
492 /* Note: SELMValidateAndConvertCSAddr checks for code type, memory type, selector validity. */
493 /** @todo dpl <= cpl else GPF */
494
495 /** @note don't use current eflags as we might be in V86 mode and the IDT always contains protected mode selectors */
496 X86EFLAGS fakeflags;
497 fakeflags.u32 = 0;
498
499 rc = SELMValidateAndConvertCSAddr(pVM, fakeflags, 0, GuestIdte.Gen.u16SegSel, NULL, pHandler, &dummy);
500 if (rc == VINF_SUCCESS)
501 {
502 VBOXGDTR gdtr = {0};
503 bool fConforming = false;
504 int idx = 0;
505 uint32_t dpl;
506 uint32_t ss_r0;
507 uint32_t esp_r0;
508 VBOXDESC Desc;
509 RTGCPTR pGdtEntry;
510
511 CPUMGetGuestGDTR(pVM, &gdtr);
512 Assert(gdtr.pGdt && gdtr.cbGdt > GuestIdte.Gen.u16SegSel);
513
514 if (!gdtr.pGdt)
515 goto failure;
516
517 pGdtEntry = (RTGCPTR)(uintptr_t)&((VBOXDESC *)gdtr.pGdt)[GuestIdte.Gen.u16SegSel >> X86_SEL_SHIFT]; /// @todo fix this
518#ifdef IN_GC
519 rc = MMGCRamRead(pVM, &Desc, pGdtEntry, sizeof(Desc));
520#else
521 rc = PGMPhysReadGCPtr(pVM, &Desc, pGdtEntry, sizeof(Desc));
522#endif
523 if (VBOX_FAILURE(rc))
524 {
525 /* The page might be out of sync. (@todo might cross a page boundary) */
526 Log(("Page %VGv out of sync -> prefetch and try again\n", pGdtEntry));
527 rc = PGMPrefetchPage(pVM, pGdtEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
528 if (rc != VINF_SUCCESS)
529 {
530 Log(("PGMPrefetchPage failed with rc=%Vrc\n", rc));
531 goto failure;
532 }
533#ifdef IN_GC
534 rc = MMGCRamRead(pVM, &Desc, pGdtEntry, sizeof(Desc));
535#else
536 rc = PGMPhysReadGCPtr(pVM, &Desc, pGdtEntry, sizeof(Desc));
537#endif
538 if (VBOX_FAILURE(rc))
539 {
540 Log(("MMGCRamRead failed with %Vrc\n", rc));
541 goto failure;
542 }
543 }
544
545 if (Desc.Gen.u4Type & X86_SEL_TYPE_CONF)
546 {
547 Log(("Conforming code selector\n"));
548 fConforming = true;
549 }
550 /** @todo check descriptor type!! */
551
552 dpl = Desc.Gen.u2Dpl;
553
554 if (!fConforming && dpl < cpl) /* to inner privilege level */
555 {
556 rc = SELMGetRing1Stack(pVM, &ss_r0, &esp_r0);
557 if (VBOX_FAILURE(rc))
558 goto failure;
559
560 Assert((ss_r0 & X86_SEL_RPL) == 1);
561
562 if ( !esp_r0
563 || !ss_r0
564 || (ss_r0 & X86_SEL_RPL) != ((dpl == 0) ? 1 : dpl)
565 || SELMToFlatEx(pVM, fakeflags, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS
566 )
567 {
568 Log(("Invalid ring 0 stack %04X:%VGv\n", ss_r0, esp_r0));
569 goto failure;
570 }
571 }
572 else
573 if (fConforming || dpl == cpl) /* to the same privilege level */
574 {
575 ss_r0 = pRegFrame->ss;
576 esp_r0 = pRegFrame->esp;
577
578 if ( eflags.Bits.u1VM /* illegal */
579 || SELMToFlatEx(pVM, fakeflags, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS)
580 {
581 AssertMsgFailed(("Invalid stack %04X:%VGv??? (VM=%d)\n", ss_r0, esp_r0, eflags.Bits.u1VM));
582 goto failure;
583 }
584 }
585 else
586 {
587 Log(("Invalid cpl-dpl combo %d vs %d\n", cpl, dpl));
588 goto failure;
589 }
590 /*
591 * Build trap stack frame on guest handler's stack
592 */
593#ifdef IN_GC
594 Assert(eflags.Bits.u1VM || (pRegFrame->ss & X86_SEL_RPL) != 0);
595 /* Check maximum amount we need (10 when executing in V86 mode) */
596 rc = PGMVerifyAccess(pVM, (RTGCUINTPTR)pTrapStackGC - 10*sizeof(uint32_t), 10 * sizeof(uint32_t), X86_PTE_RW);
597#else
598 Assert(eflags.Bits.u1VM || (pRegFrame->ss & X86_SEL_RPL) == 0 || (pRegFrame->ss & X86_SEL_RPL) == 3);
599 /* Check maximum amount we need (10 when executing in V86 mode) */
600 if ( PAGE_ADDRESS(pTrapStackGC) != PAGE_ADDRESS(pTrapStackGC - 10*sizeof(uint32_t)) /* fail if we cross a page boundary */
601 || VBOX_FAILURE((rc = PGMPhysGCPtr2HCPtr(pVM, pTrapStackGC, (PRTHCPTR)&pTrapStackHC)))
602 )
603 {
604 AssertRC(rc);
605 goto failure;
606 }
607#endif
608 if (rc == VINF_SUCCESS)
609 {
610 /** if eflags.Bits.u1VM then push gs, fs, ds, es */
611 if (eflags.Bits.u1VM)
612 {
613 Log(("TRAP%02X: (VM) Handler %04X:%08X Stack %04X:%08X RPL=%d CR2=%08X\n", iGate, GuestIdte.Gen.u16SegSel, pHandler, ss_r0, esp_r0, (pRegFrame->ss & X86_SEL_RPL), pVM->trpm.s.uActiveCR2));
614 CTXSUFF(pTrapStack)[--idx] = pRegFrame->gs;
615 CTXSUFF(pTrapStack)[--idx] = pRegFrame->fs;
616 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ds;
617 CTXSUFF(pTrapStack)[--idx] = pRegFrame->es;
618
619 /* clear ds, es, fs & gs in current context */
620 pRegFrame->ds = pRegFrame->es = pRegFrame->fs = pRegFrame->gs = 0;
621 }
622 else
623 Log(("TRAP%02X: Handler %04X:%08X Stack %04X:%08X RPL=%d CR2=%08X\n", iGate, GuestIdte.Gen.u16SegSel, pHandler, ss_r0, esp_r0, (pRegFrame->ss & X86_SEL_RPL), pVM->trpm.s.uActiveCR2));
624
625 if (fConforming == false && dpl < cpl)
626 {
627 if ((pRegFrame->ss & X86_SEL_RPL) == 1 && !eflags.Bits.u1VM)
628 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss & ~1; /* Mask away traces of raw ring execution (ring 1). */
629 else
630 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss;
631
632 CTXSUFF(pTrapStack)[--idx] = pRegFrame->esp;
633 }
634
635 /** @note we use the eflags copy, that includes the virtualized bits! */
636 /** @note not really necessary as we grab include those bits in the trap/irq handler trampoline */
637 CTXSUFF(pTrapStack)[--idx] = eflags.u32;
638
639 if ((pRegFrame->cs & X86_SEL_RPL) == 1 && !eflags.Bits.u1VM)
640 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs & ~1; /* Mask away traces of raw ring execution (ring 1). */
641 else
642 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs;
643
644 if (enmType == TRPM_SOFTWARE_INT)
645 {
646 Assert(opsize);
647 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip + opsize; /* return address = next instruction */
648 }
649 else
650 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip;
651
652 if (enmError == TRPM_TRAP_HAS_ERRORCODE)
653 {
654 CTXSUFF(pTrapStack)[--idx] = pVM->trpm.s.uActiveErrorCode;
655 }
656
657 Assert(esp_r0 > -idx*sizeof(uint32_t));
658 /* Adjust ESP accordingly */
659 esp_r0 += idx*sizeof(uint32_t);
660
661 /* Mask away dangerous flags for the trap/interrupt handler. */
662 eflags.u32 &= ~(X86_EFL_TF | X86_EFL_VM | X86_EFL_RF | X86_EFL_NT);
663#ifdef DEBUG
664 for (int j=idx;j<0;j++)
665 {
666 LogFlow(("Stack %VGv pos %02d: %08x\n", &CTXSUFF(pTrapStack)[j], j, CTXSUFF(pTrapStack)[j]));
667 }
668 char *pszPrefix = "";
669 char *szEFlags = "";
670
671 LogFlow(( "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
672 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
673 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %seflags=%08x\n",
674 pszPrefix, pRegFrame->eax, pszPrefix, pRegFrame->ebx, pszPrefix, pRegFrame->ecx, pszPrefix, pRegFrame->edx, pszPrefix, pRegFrame->esi, pszPrefix, pRegFrame->edi,
675 pszPrefix, pRegFrame->eip, pszPrefix, pRegFrame->esp, pszPrefix, pRegFrame->ebp, pszPrefix, eflags.Bits.u2IOPL, *pszPrefix ? 33 : 31, szEFlags,
676 pszPrefix, (RTSEL)pRegFrame->cs, pszPrefix, (RTSEL)pRegFrame->ds, pszPrefix, (RTSEL)pRegFrame->es,
677 pszPrefix, (RTSEL)pRegFrame->fs, pszPrefix, (RTSEL)pRegFrame->gs, pszPrefix, eflags.u32));
678#endif
679
680 Log(("PATM Handler %VGv Adjusted stack %08X new EFLAGS=%08X idx=%d dpl=%d cpl=%d\n", pVM->trpm.s.aGuestTrapHandler[iGate], esp_r0, eflags.u32, idx, dpl, cpl));
681
682 /* Make sure the internal guest context structure is up-to-date. */
683 CPUMSetGuestCR2(pVM, pVM->trpm.s.uActiveCR2);
684
685#ifdef IN_GC
686 /** @note shouldn't be necessary */
687 ASMSetCR2(pVM->trpm.s.uActiveCR2);
688
689 /* Turn off interrupts for interrupt gates. */
690 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
691 CPUMRawSetEFlags(pVM, pRegFrame, eflags.u32 & ~X86_EFL_IF);
692
693 /* The virtualized bits must be removed again!! */
694 eflags.Bits.u1IF = 1;
695 eflags.Bits.u2IOPL = 0;
696
697 Assert(eflags.Bits.u1IF);
698 Assert(eflags.Bits.u2IOPL == 0);
699 STAM_COUNTER_INC(&pVM->trpm.s.CTXALLSUFF(paStatForwardedIRQ)[iGate]);
700 STAM_PROFILE_ADV_STOP(CTXSUFF(&pVM->trpm.s.StatForwardProf), a);
701
702 CPUMGCCallGuestTrapHandler(pRegFrame, GuestIdte.Gen.u16SegSel | 1, pVM->trpm.s.aGuestTrapHandler[iGate], eflags.u32, ss_r0, (RTGCPTR)esp_r0);
703 /* does not return */
704#else
705 /* Turn off interrupts for interrupt gates. */
706 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
707 eflags.Bits.u1IF = 0;
708
709 pRegFrame->eflags.u32 = eflags.u32;
710
711 pRegFrame->eip = pVM->trpm.s.aGuestTrapHandler[iGate];
712 pRegFrame->cs = GuestIdte.Gen.u16SegSel;
713 pRegFrame->esp = esp_r0;
714 pRegFrame->ss = ss_r0 & ~X86_SEL_RPL; /* set rpl to ring 0 */
715 STAM_PROFILE_ADV_STOP(CTXSUFF(&pVM->trpm.s.StatForwardProf), a);
716 return VINF_SUCCESS;
717#endif
718 }
719 else
720 Log(("TRAP%02X: PGMVerifyAccess %VGv failed with %Vrc -> forward to REM\n", iGate, pTrapStackGC, rc));
721 }
722 else
723 Log(("SELMValidateAndConvertCSAddr failed with %Vrc\n", rc));
724 }
725 else
726 Log(("MMRamRead %VGv size %d failed with %Vrc\n", (RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate, sizeof(GuestIdte), rc));
727 }
728 else
729 {
730 Log(("Refused to forward trap: eflags=%08x IF=%d\n", eflags.u32, eflags.Bits.u1IF));
731#ifdef VBOX_WITH_STATISTICS
732 if (pVM->trpm.s.aGuestTrapHandler[iGate] == TRPM_INVALID_HANDLER)
733 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailNoHandler);
734 else
735 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
736 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailPatchAddr);
737#endif
738 }
739failure:
740 STAM_COUNTER_INC(&CTXSUFF(pVM->trpm.s.StatForwardFail));
741 STAM_PROFILE_ADV_STOP(CTXSUFF(&pVM->trpm.s.StatForwardProf), a);
742
743 Log(("TRAP%02X: forwarding to REM (ss rpl=%d eflags=%08X VMIF=%d handler=%08X\n", iGate, pRegFrame->ss & X86_SEL_RPL, pRegFrame->eflags.u32, PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame), pVM->trpm.s.aGuestTrapHandler[iGate]));
744#endif
745 return VINF_EM_RAW_GUEST_TRAP;
746}
747
748
749/**
750 * Raises a cpu exception which doesn't take an error code.
751 *
752 * This function may or may not dispatch the exception before returning.
753 *
754 * @returns VBox status code fit for scheduling.
755 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
756 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
757 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
758 *
759 * @param pVM The VM handle.
760 * @param pCtxCore The CPU context core.
761 * @param enmXcpt The exception.
762 */
763TRPMDECL(int) TRPMRaiseXcpt(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt)
764{
765 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x\n", pCtxCore->cs, pCtxCore->eip, enmXcpt));
766/** @todo dispatch the trap. */
767 pVM->trpm.s.uActiveVector = enmXcpt;
768 pVM->trpm.s.fActiveSoftwareInterrupt = false;
769 pVM->trpm.s.uActiveErrorCode = 0xdeadbeef;
770 pVM->trpm.s.uActiveCR2 = 0xdeadface;
771 return VINF_EM_RAW_GUEST_TRAP;
772}
773
774
775/**
776 * Raises a cpu exception with an errorcode.
777 *
778 * This function may or may not dispatch the exception before returning.
779 *
780 * @returns VBox status code fit for scheduling.
781 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
782 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
783 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
784 *
785 * @param pVM The VM handle.
786 * @param pCtxCore The CPU context core.
787 * @param enmXcpt The exception.
788 * @param uErr The error code.
789 */
790TRPMDECL(int) TRPMRaiseXcptErr(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr)
791{
792 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr));
793/** @todo dispatch the trap. */
794 pVM->trpm.s.uActiveVector = enmXcpt;
795 pVM->trpm.s.fActiveSoftwareInterrupt = false;
796 pVM->trpm.s.uActiveErrorCode = uErr;
797 pVM->trpm.s.uActiveCR2 = 0xdeadface;
798 return VINF_EM_RAW_GUEST_TRAP;
799}
800
801
802/**
803 * Raises a cpu exception with an errorcode and CR2.
804 *
805 * This function may or may not dispatch the exception before returning.
806 *
807 * @returns VBox status code fit for scheduling.
808 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
809 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
810 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
811 *
812 * @param pVM The VM handle.
813 * @param pCtxCore The CPU context core.
814 * @param enmXcpt The exception.
815 * @param uErr The error code.
816 * @param uCR2 The CR2 value.
817 */
818TRPMDECL(int) TRPMRaiseXcptErrCR2(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2)
819{
820 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32 uCR2=%RGv\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr, uCR2));
821/** @todo dispatch the trap. */
822 pVM->trpm.s.uActiveVector = enmXcpt;
823 pVM->trpm.s.fActiveSoftwareInterrupt = false;
824 pVM->trpm.s.uActiveErrorCode = uErr;
825 pVM->trpm.s.uActiveCR2 = uCR2;
826 return VINF_EM_RAW_GUEST_TRAP;
827}
828
829
830/**
831 * Clear guest trap/interrupt gate handler
832 *
833 * @returns VBox status code.
834 * @param pVM The VM to operate on.
835 * @param iTrap Interrupt/trap number.
836 */
837TRPMDECL(int) trpmClearGuestTrapHandler(PVM pVM, unsigned iTrap)
838{
839 /*
840 * Validate.
841 */
842 if (iTrap >= ELEMENTS(pVM->trpm.s.aIdt))
843 {
844 AssertMsg(iTrap < TRPM_HANDLER_INT_BASE, ("Illegal gate number %d!\n", iTrap));
845 return VERR_INVALID_PARAMETER;
846 }
847
848 if (ASMBitTest(&pVM->trpm.s.au32IdtPatched[0], iTrap))
849#ifdef IN_RING3
850 trpmR3ClearPassThroughHandler(pVM, iTrap);
851#else
852 AssertFailed();
853#endif
854
855 pVM->trpm.s.aGuestTrapHandler[iTrap] = TRPM_INVALID_HANDLER;
856 return VINF_SUCCESS;
857}
858
859
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