VirtualBox

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

Last change on this file since 847 was 624, checked in by vboxsync, 18 years ago

More statistics.

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