VirtualBox

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

Last change on this file since 552 was 98, checked in by vboxsync, 18 years ago

Reapply fixed 17508 changeset

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.6 KB
Line 
1/* $Id: TRPMAll.cpp 98 2007-01-17 13:47:34Z 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 X86EFLAGS eflags;
362
363#ifdef TRPM_FORWARD_TRAPS_IN_GC
364 Log(("TRPMForwardTrap: eip=%VGv iGate=%d\n", pRegFrame->eip, iGate));
365
366#ifdef DEBUG
367 switch (iGate) {
368 case 14:
369 if (pRegFrame->eip == pVM->trpm.s.uActiveCR2)
370 {
371 int rc;
372 RTGCPTR pCallerGC;
373#ifdef IN_GC
374 rc = MMGCRamRead(pVM, &pCallerGC, (RTGCPTR)pRegFrame->esp, sizeof(pCallerGC));
375#else
376 rc = PGMPhysReadGCPtr(pVM, &pCallerGC, (RTGCPTR)pRegFrame->esp, sizeof(pCallerGC));
377#endif
378 if (VBOX_SUCCESS(rc))
379 {
380 Log(("TRPMForwardTrap: caller=%VGv\n", pCallerGC));
381 }
382 }
383 /* no break */
384 case 8:
385 case 10:
386 case 11:
387 case 12:
388 case 13:
389 case 17:
390 Assert(enmError == TRPM_TRAP_HAS_ERRORCODE);
391 break;
392 default:
393 Assert(enmError == TRPM_TRAP_NO_ERRORCODE);
394 break;
395 }
396#endif /* DEBUG */
397
398 /* Retrieve the eflags including the virtualized bits. */
399 /** @note hackish as the cpumctxcore structure doesn't contain the right value */
400 eflags.u32 = CPUMRawGetEFlags(pVM, pRegFrame);
401
402 /* VM_FF_INHIBIT_INTERRUPTS should be cleared upfront or don't call this function at all for dispatching hardware interrupts. */
403 Assert(enmType != TRPM_HARDWARE_INT || !VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
404
405 /*
406 * 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.
407 * Well, only if the IF flag is set.
408 */
409 /*
410 * @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.
411 *
412 */
413 if ( pVM->trpm.s.aGuestTrapHandler[iGate]
414 && (eflags.Bits.u1IF)
415 && !(eflags.Bits.u1VM) /* @todo implement when needed (illegal for same privilege level transfers). */
416 && !PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip)
417 )
418 {
419 uint16_t cbIDT;
420 RTGCPTR GCPtrIDT = (RTGCPTR)CPUMGetGuestIDTR(pVM, &cbIDT);
421 uint32_t cpl;
422 VBOXIDTE GuestIdte;
423 RTGCPTR pIDTEntry;
424 int rc;
425
426 Assert(PATMAreInterruptsEnabledByCtxCore(pVM, pRegFrame));
427
428 /* Must get the CPL from the SS selector (CS might be conforming) */
429 if ((pRegFrame->ss & X86_SEL_RPL) == 1)
430 cpl = 0;
431 else
432 cpl = (pRegFrame->ss & X86_SEL_RPL);
433
434 if (GCPtrIDT && iGate * sizeof(VBOXIDTE) >= cbIDT)
435 goto failure;
436
437 /*
438 * BIG TODO: The checks are not complete. see trap and interrupt dispatching section in Intel docs for details
439 * All very obscure, but still necessary.
440 * Currently only some CS & TSS selector checks are missing.
441 *
442 */
443 pIDTEntry = (RTGCPTR)((RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate);
444#ifdef IN_GC
445 rc = MMGCRamRead(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
446#else
447 rc = PGMPhysReadGCPtr(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
448#endif
449 if (VBOX_FAILURE(rc))
450 {
451 /* The page might be out of sync. (@todo might cross a page boundary) */
452 Log(("Page %VGv out of sync -> prefetch and try again\n", pIDTEntry));
453 rc = PGMPrefetchPage(pVM, pIDTEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
454 if (rc != VINF_SUCCESS)
455 {
456 Log(("TRPMForwardTrap: PGMPrefetchPage failed with rc=%Vrc\n", rc));
457 goto failure;
458 }
459#ifdef IN_GC
460 rc = MMGCRamRead(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
461#else
462 rc = PGMPhysReadGCPtr(pVM, &GuestIdte, pIDTEntry, sizeof(GuestIdte));
463#endif
464 }
465 if ( VBOX_SUCCESS(rc)
466 && GuestIdte.Gen.u1Present
467 && (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_TRAP_32 || GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
468 && (GuestIdte.Gen.u2DPL == 3 || GuestIdte.Gen.u2DPL == 0)
469 && (GuestIdte.Gen.u16SegSel & 0xfffc) /* must not be zero */
470 && (enmType == TRPM_TRAP || enmType == TRPM_HARDWARE_INT || cpl <= GuestIdte.Gen.u2DPL) /* CPL <= DPL if software int */
471 )
472 {
473 RTGCPTR pHandler, dummy;
474 GCPTRTYPE(uint32_t *)pTrapStackGC;
475#ifndef IN_GC
476 HCPTRTYPE(uint32_t *)pTrapStackHC;
477#endif
478
479 pHandler = (RTGCPTR)((GuestIdte.Gen.u16OffsetHigh << 16) | GuestIdte.Gen.u16OffsetLow);
480
481 /* Note: SELMValidateAndConvertCSAddr checks for code type, memory type, selector validity. */
482 /** @todo dpl <= cpl else GPF */
483 rc = SELMValidateAndConvertCSAddr(pVM, 0, GuestIdte.Gen.u16SegSel, NULL, pHandler, &dummy);
484 if (rc == VINF_SUCCESS)
485 {
486 VBOXGDTR gdtr = {0};
487 bool fConforming = false;
488 int idx = 0;
489 uint32_t dpl;
490 uint32_t ss_r0;
491 uint32_t esp_r0;
492 VBOXDESC Desc;
493 RTGCPTR pGdtEntry;
494
495 CPUMGetGuestGDTR(pVM, &gdtr);
496 Assert(gdtr.pGdt && gdtr.cbGdt > GuestIdte.Gen.u16SegSel);
497
498 if (!gdtr.pGdt)
499 goto failure;
500
501 pGdtEntry = (RTGCPTR)(uintptr_t)&((VBOXDESC *)gdtr.pGdt)[GuestIdte.Gen.u16SegSel >> X86_SEL_SHIFT]; /// @todo fix this
502#ifdef IN_GC
503 rc = MMGCRamRead(pVM, &Desc, pGdtEntry, sizeof(Desc));
504#else
505 rc = PGMPhysReadGCPtr(pVM, &Desc, pGdtEntry, sizeof(Desc));
506#endif
507 if (VBOX_FAILURE(rc))
508 {
509 /* The page might be out of sync. (@todo might cross a page boundary) */
510 Log(("Page %VGv out of sync -> prefetch and try again\n", pGdtEntry));
511 rc = PGMPrefetchPage(pVM, pGdtEntry); /** @todo r=bird: rainy day: this isn't entirely safe because of access bit virtualiziation and CSAM. */
512 if (rc != VINF_SUCCESS)
513 {
514 Log(("PGMPrefetchPage failed with rc=%Vrc\n", rc));
515 return rc;
516 }
517#ifdef IN_GC
518 rc = MMGCRamRead(pVM, &Desc, pGdtEntry, sizeof(Desc));
519#else
520 rc = PGMPhysReadGCPtr(pVM, &Desc, pGdtEntry, sizeof(Desc));
521#endif
522 if (VBOX_FAILURE(rc))
523 {
524 Log(("MMGCRamRead failed with %Vrc\n", rc));
525 goto failure;
526 }
527 }
528
529 if (Desc.Gen.u4Type & X86_SEL_TYPE_CONF)
530 {
531 Log(("Conforming code selector\n"));
532 fConforming = true;
533 }
534 /** @todo check descriptor type!! */
535
536 dpl = Desc.Gen.u2Dpl;
537
538 if (!fConforming && dpl < cpl) /* to inner privilege level */
539 {
540 rc = SELMGetRing1Stack(pVM, &ss_r0, &esp_r0);
541 if (VBOX_FAILURE(rc))
542 goto failure;
543
544 Assert((ss_r0 & X86_SEL_RPL) == 1);
545
546 if ( !esp_r0
547 || !ss_r0
548 || (ss_r0 & X86_SEL_RPL) != ((dpl == 0) ? 1 : dpl)
549 || SELMToFlatEx(pVM, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS
550 )
551 {
552 Log(("Invalid ring 0 stack %04X:%VGv\n", ss_r0, esp_r0));
553 goto failure;
554 }
555 }
556 else
557 if (fConforming || dpl == cpl) /* to the same privilege level */
558 {
559 ss_r0 = pRegFrame->ss;
560 esp_r0 = pRegFrame->esp;
561
562 if ( eflags.Bits.u1VM /* illegal */
563 || SELMToFlatEx(pVM, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS)
564 {
565 AssertMsgFailed(("Invalid stack %04X:%VGv???\n", ss_r0, esp_r0));
566 goto failure;
567 }
568 }
569 else
570 {
571 Log(("Invalid cpl-dpl combo %d vs %d\n", cpl, dpl));
572 goto failure;
573 }
574 /*
575 * Build trap stack frame on guest handler's stack
576 */
577#ifdef IN_GC
578 Assert((pRegFrame->ss & X86_SEL_RPL) != 0);
579 rc = PGMVerifyAccess(pVM, (RTGCUINTPTR)pTrapStackGC - 6*sizeof(uint32_t), 6 * sizeof(uint32_t), X86_PTE_RW);
580#else
581 Assert((pRegFrame->ss & X86_SEL_RPL) == 0 || (pRegFrame->ss & X86_SEL_RPL) == 3);
582 if ( PAGE_ADDRESS(pTrapStackGC) != PAGE_ADDRESS(pTrapStackGC - 6*sizeof(uint32_t)) /* fail if we cross a page boundary */
583 || VBOX_FAILURE((rc = PGMPhysGCPtr2HCPtr(pVM, pTrapStackGC, (PRTHCPTR)&pTrapStackHC)))
584 )
585 {
586 AssertRC(rc);
587 goto failure;
588 }
589#endif
590 if (rc == VINF_SUCCESS)
591 {
592 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));
593
594 if (fConforming == false && dpl < cpl)
595 {
596 if ((pRegFrame->ss & X86_SEL_RPL) == 1)
597 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss & ~1; /* Mask away traces of raw ring execution (ring 1). */
598 else
599 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss;
600
601 CTXSUFF(pTrapStack)[--idx] = pRegFrame->esp;
602 }
603
604 /* @note we use the original eflags, not the copy that includes the virtualized bits! */
605 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eflags.u32;
606
607 if ((pRegFrame->cs & X86_SEL_RPL) == 1)
608 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs & ~1; /* Mask away traces of raw ring execution (ring 1). */
609 else
610 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs;
611
612 if (enmType == TRPM_SOFTWARE_INT)
613 {
614 Assert(opsize);
615 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip + opsize; /* return address = next instruction */
616 }
617 else
618 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip;
619
620 if (enmError == TRPM_TRAP_HAS_ERRORCODE)
621 {
622 CTXSUFF(pTrapStack)[--idx] = pVM->trpm.s.uActiveErrorCode;
623 }
624
625 Assert(esp_r0 > -idx*sizeof(uint32_t));
626 /* Adjust ESP accordingly */
627 esp_r0 += idx*sizeof(uint32_t);
628
629 /* Mask away dangerous flags for the trap/interrupt handler. */
630 eflags.u32 &= ~(X86_EFL_TF | X86_EFL_VM | X86_EFL_RF | X86_EFL_NT);
631#ifdef DEBUG
632 for (int j=idx;j<0;j++)
633 {
634 LogFlow(("Stack %VGv pos %02d: %08x\n", &CTXSUFF(pTrapStack)[j], j, CTXSUFF(pTrapStack)[j]));
635 }
636 char *pszPrefix = "";
637 char *szEFlags = "";
638
639 LogFlow(( "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
640 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
641 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %seflags=%08x\n",
642 pszPrefix, pRegFrame->eax, pszPrefix, pRegFrame->ebx, pszPrefix, pRegFrame->ecx, pszPrefix, pRegFrame->edx, pszPrefix, pRegFrame->esi, pszPrefix, pRegFrame->edi,
643 pszPrefix, pRegFrame->eip, pszPrefix, pRegFrame->esp, pszPrefix, pRegFrame->ebp, pszPrefix, eflags.Bits.u2IOPL, *pszPrefix ? 33 : 31, szEFlags,
644 pszPrefix, (RTSEL)pRegFrame->cs, pszPrefix, (RTSEL)pRegFrame->ds, pszPrefix, (RTSEL)pRegFrame->es,
645 pszPrefix, (RTSEL)pRegFrame->fs, pszPrefix, (RTSEL)pRegFrame->gs, pszPrefix, eflags.u32));
646#endif
647
648 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));
649
650//// AssertMsg(ASMGetCR2() == pTrpm->uActiveCR2, ("current cr2=%08x trpm cr2=%08x\n", ASMGetCR2(), pTrpm->uActiveCR2));
651
652 /* Make sure the internal guest context structure is up-to-date. */
653 CPUMSetGuestCR2(pVM, pVM->trpm.s.uActiveCR2);
654
655#ifdef IN_GC
656 /** @note shouldn't be necessary */
657 ASMSetCR2(pVM->trpm.s.uActiveCR2);
658
659 /* Turn off interrupts for interrupt gates. */
660 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
661 CPUMRawSetEFlags(pVM, pRegFrame, eflags.u32 & ~X86_EFL_IF);
662
663 /* The virtualized bits must be removed again!! */
664 eflags.Bits.u1IF = 1;
665 eflags.Bits.u2IOPL = 0;
666
667 STAM_COUNTER_INC(&pVM->trpm.s.CTXALLSUFF(paStatForwardedIRQ)[iGate]);
668 Assert(eflags.Bits.u1IF);
669 Assert(eflags.Bits.u2IOPL == 0);
670 CPUMGCCallGuestTrapHandler(pRegFrame, GuestIdte.Gen.u16SegSel | 1, pVM->trpm.s.aGuestTrapHandler[iGate], eflags.u32, ss_r0, (RTGCPTR)esp_r0);
671 /* does not return */
672#else
673 /* Turn off interrupts for interrupt gates. */
674 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
675 eflags.Bits.u1IF = 0;
676
677 pRegFrame->eflags.u32 = eflags.u32;
678
679 pRegFrame->eip = pVM->trpm.s.aGuestTrapHandler[iGate];
680 pRegFrame->cs = GuestIdte.Gen.u16SegSel;
681 pRegFrame->esp = esp_r0;
682 pRegFrame->ss = ss_r0 & ~X86_SEL_RPL; /* set rpl to ring 0 */
683 return VINF_SUCCESS;
684#endif
685 }
686 else
687 Log(("TRAP%02X: PGMVerifyAccess %VGv failed with %Vrc -> forward to REM\n", iGate, pTrapStackGC, rc));
688 }
689 else
690 Log(("SELMValidateAndConvertCSAddr failed with %Vrc\n", rc));
691 }
692 else
693 Log(("MMRamRead %VGv size %d failed with %Vrc\n", (RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate, sizeof(GuestIdte), rc));
694 }
695 else
696 {
697 Log(("Refused to forward trap: eflags=%08x IF=%d\n", eflags.u32, eflags.Bits.u1IF));
698#ifdef VBOX_WITH_STATISTICS
699 if (pVM->trpm.s.aGuestTrapHandler[iGate] == TRPM_INVALID_HANDLER)
700 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailNoHandler);
701 else
702 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
703 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailPatchAddr);
704#endif
705 }
706failure:
707 STAM_COUNTER_INC(&CTXSUFF(pVM->trpm.s.StatForwardFail));
708
709 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]));
710#endif
711 return VINF_EM_RAW_GUEST_TRAP;
712}
713
714
715/**
716 * Raises a cpu exception which doesn't take an error code.
717 *
718 * This function may or may not dispatch the exception before returning.
719 *
720 * @returns VBox status code fit for scheduling.
721 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
722 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
723 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
724 *
725 * @param pVM The VM handle.
726 * @param pCtxCore The CPU context core.
727 * @param enmXcpt The exception.
728 */
729TRPMDECL(int) TRPMRaiseXcpt(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt)
730{
731 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x\n", pCtxCore->cs, pCtxCore->eip, enmXcpt));
732/** @todo dispatch the trap. */
733 pVM->trpm.s.uActiveVector = enmXcpt;
734 pVM->trpm.s.fActiveSoftwareInterrupt = false;
735 pVM->trpm.s.uActiveErrorCode = 0xdeadbeef;
736 pVM->trpm.s.uActiveCR2 = 0xdeadface;
737 return VINF_EM_RAW_GUEST_TRAP;
738}
739
740
741/**
742 * Raises a cpu exception with an errorcode.
743 *
744 * This function may or may not dispatch the exception before returning.
745 *
746 * @returns VBox status code fit for scheduling.
747 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
748 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
749 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
750 *
751 * @param pVM The VM handle.
752 * @param pCtxCore The CPU context core.
753 * @param enmXcpt The exception.
754 * @param uErr The error code.
755 */
756TRPMDECL(int) TRPMRaiseXcptErr(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr)
757{
758 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr));
759/** @todo dispatch the trap. */
760 pVM->trpm.s.uActiveVector = enmXcpt;
761 pVM->trpm.s.fActiveSoftwareInterrupt = false;
762 pVM->trpm.s.uActiveErrorCode = uErr;
763 pVM->trpm.s.uActiveCR2 = 0xdeadface;
764 return VINF_EM_RAW_GUEST_TRAP;
765}
766
767
768/**
769 * Raises a cpu exception with an errorcode and CR2.
770 *
771 * This function may or may not dispatch the exception before returning.
772 *
773 * @returns VBox status code fit for scheduling.
774 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
775 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
776 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
777 *
778 * @param pVM The VM handle.
779 * @param pCtxCore The CPU context core.
780 * @param enmXcpt The exception.
781 * @param uErr The error code.
782 * @param uCR2 The CR2 value.
783 */
784TRPMDECL(int) TRPMRaiseXcptErrCR2(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2)
785{
786 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32 uCR2=%RGv\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr, uCR2));
787/** @todo dispatch the trap. */
788 pVM->trpm.s.uActiveVector = enmXcpt;
789 pVM->trpm.s.fActiveSoftwareInterrupt = false;
790 pVM->trpm.s.uActiveErrorCode = uErr;
791 pVM->trpm.s.uActiveCR2 = uCR2;
792 return VINF_EM_RAW_GUEST_TRAP;
793}
794
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette