VirtualBox

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

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.4 KB
Line 
1/** @file
2 *
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)&((VBOXDESC *)gdtr.pGdt)[GuestIdte.Gen.u16SegSel >> X86_SEL_SHIFT];
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 SELMGetRing1Stack(pVM, &ss_r0, &esp_r0);
541 Assert((ss_r0 & X86_SEL_RPL) == 1);
542
543 if ( !esp_r0
544 || !ss_r0
545 || (ss_r0 & X86_SEL_RPL) != ((dpl == 0) ? 1 : dpl)
546 || SELMToFlatEx(pVM, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS
547 )
548 {
549 Log(("Invalid ring 0 stack %04X:%VGv\n", ss_r0, esp_r0));
550 goto failure;
551 }
552 }
553 else
554 if (fConforming || dpl == cpl) /* to the same privilege level */
555 {
556 ss_r0 = pRegFrame->ss;
557 esp_r0 = pRegFrame->esp;
558
559 if ( eflags.Bits.u1VM /* illegal */
560 || SELMToFlatEx(pVM, ss_r0, (RTGCPTR)esp_r0, SELMTOFLAT_FLAGS_CPL1, (PRTGCPTR)&pTrapStackGC, NULL) != VINF_SUCCESS)
561 {
562 AssertMsgFailed(("Invalid stack %04X:%VGv???\n", ss_r0, esp_r0));
563 goto failure;
564 }
565 }
566 else
567 {
568 Log(("Invalid cpl-dpl combo %d vs %d\n", cpl, dpl));
569 goto failure;
570 }
571 /*
572 * Build trap stack frame on guest handler's stack
573 */
574#ifdef IN_GC
575 Assert((pRegFrame->ss & X86_SEL_RPL) != 0);
576 rc = PGMVerifyAccess(pVM, (RTGCUINTPTR)pTrapStackGC - 6*sizeof(uint32_t), 6 * sizeof(uint32_t), X86_PTE_RW);
577#else
578 Assert((pRegFrame->ss & X86_SEL_RPL) == 0 || (pRegFrame->ss & X86_SEL_RPL) == 3);
579 if ( PAGE_ADDRESS(pTrapStackGC) != PAGE_ADDRESS(pTrapStackGC - 6*sizeof(uint32_t)) /* fail if we cross a page boundary */
580 || VBOX_FAILURE((rc = PGMPhysGCPtr2HCPtr(pVM, pTrapStackGC, (PRTHCPTR)&pTrapStackHC)))
581 )
582 {
583 AssertRC(rc);
584 goto failure;
585 }
586#endif
587 if (rc == VINF_SUCCESS)
588 {
589 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));
590
591 if (fConforming == false && dpl < cpl)
592 {
593 if ((pRegFrame->ss & X86_SEL_RPL) == 1)
594 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss & ~1; /* Mask away traces of raw ring execution (ring 1). */
595 else
596 CTXSUFF(pTrapStack)[--idx] = pRegFrame->ss;
597
598 CTXSUFF(pTrapStack)[--idx] = pRegFrame->esp;
599 }
600
601 /* @note we use the original eflags, not the copy that includes the virtualized bits! */
602 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eflags.u32;
603
604 if ((pRegFrame->cs & X86_SEL_RPL) == 1)
605 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs & ~1; /* Mask away traces of raw ring execution (ring 1). */
606 else
607 CTXSUFF(pTrapStack)[--idx] = pRegFrame->cs;
608
609 if (enmType == TRPM_SOFTWARE_INT)
610 {
611 Assert(opsize);
612 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip + opsize; /* return address = next instruction */
613 }
614 else
615 CTXSUFF(pTrapStack)[--idx] = pRegFrame->eip;
616
617 if (enmError == TRPM_TRAP_HAS_ERRORCODE)
618 {
619 CTXSUFF(pTrapStack)[--idx] = pVM->trpm.s.uActiveErrorCode;
620 }
621
622 Assert(esp_r0 > -idx*sizeof(uint32_t));
623 /* Adjust ESP accordingly */
624 esp_r0 += idx*sizeof(uint32_t);
625
626 /* Mask away dangerous flags for the trap/interrupt handler. */
627 eflags.u32 &= ~(X86_EFL_TF | X86_EFL_VM | X86_EFL_RF | X86_EFL_NT);
628#ifdef DEBUG
629 for (int j=idx;j<0;j++)
630 {
631 LogFlow(("Stack %VGv pos %02d: %08x\n", &CTXSUFF(pTrapStack)[j], j, CTXSUFF(pTrapStack)[j]));
632 }
633 char *pszPrefix = "";
634 char *szEFlags = "";
635
636 LogFlow(( "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
637 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
638 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %seflags=%08x\n",
639 pszPrefix, pRegFrame->eax, pszPrefix, pRegFrame->ebx, pszPrefix, pRegFrame->ecx, pszPrefix, pRegFrame->edx, pszPrefix, pRegFrame->esi, pszPrefix, pRegFrame->edi,
640 pszPrefix, pRegFrame->eip, pszPrefix, pRegFrame->esp, pszPrefix, pRegFrame->ebp, pszPrefix, eflags.Bits.u2IOPL, *pszPrefix ? 33 : 31, szEFlags,
641 pszPrefix, (RTSEL)pRegFrame->cs, pszPrefix, (RTSEL)pRegFrame->ds, pszPrefix, (RTSEL)pRegFrame->es,
642 pszPrefix, (RTSEL)pRegFrame->fs, pszPrefix, (RTSEL)pRegFrame->gs, pszPrefix, eflags.u32));
643#endif
644
645 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));
646
647//// AssertMsg(ASMGetCR2() == pTrpm->uActiveCR2, ("current cr2=%08x trpm cr2=%08x\n", ASMGetCR2(), pTrpm->uActiveCR2));
648
649 /* Make sure the internal guest context structure is up-to-date. */
650 CPUMSetGuestCR2(pVM, pVM->trpm.s.uActiveCR2);
651
652#ifdef IN_GC
653 /** @note shouldn't be necessary */
654 ASMSetCR2(pVM->trpm.s.uActiveCR2);
655
656 /* Turn off interrupts for interrupt gates. */
657 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
658 CPUMRawSetEFlags(pVM, pRegFrame, eflags.u32 & ~X86_EFL_IF);
659
660 /* The virtualized bits must be removed again!! */
661 eflags.Bits.u1IF = 1;
662 eflags.Bits.u2IOPL = 0;
663
664 STAM_COUNTER_INC(&pVM->trpm.s.CTXALLSUFF(paStatForwardedIRQ)[iGate]);
665 Assert(eflags.Bits.u1IF);
666 Assert(eflags.Bits.u2IOPL == 0);
667 CPUMGCCallGuestTrapHandler(pRegFrame, GuestIdte.Gen.u16SegSel | 1, pVM->trpm.s.aGuestTrapHandler[iGate], eflags.u32, ss_r0, (RTGCPTR)esp_r0);
668 /* does not return */
669#else
670 /* Turn off interrupts for interrupt gates. */
671 if (GuestIdte.Gen.u5Type2 == VBOX_IDTE_TYPE2_INT_32)
672 eflags.Bits.u1IF = 0;
673
674 pRegFrame->eflags.u32 = eflags.u32;
675
676 pRegFrame->eip = pVM->trpm.s.aGuestTrapHandler[iGate];
677 pRegFrame->cs = GuestIdte.Gen.u16SegSel;
678 pRegFrame->esp = esp_r0;
679 pRegFrame->ss = ss_r0 & ~X86_SEL_RPL; /* set rpl to ring 0 */
680 return VINF_SUCCESS;
681#endif
682 }
683 else
684 Log(("TRAP%02X: PGMVerifyAccess %VGv failed with %Vrc -> forward to REM\n", iGate, pTrapStackGC, rc));
685 }
686 else
687 Log(("SELMValidateAndConvertCSAddr failed with %Vrc\n", rc));
688 }
689 else
690 Log(("MMRamRead %VGv size %d failed with %Vrc\n", (RTGCUINTPTR)GCPtrIDT + sizeof(VBOXIDTE) * iGate, sizeof(GuestIdte), rc));
691 }
692 else
693 {
694 Log(("Refused to forward trap: eflags=%08x IF=%d\n", eflags.u32, eflags.Bits.u1IF));
695#ifdef VBOX_WITH_STATISTICS
696 if (pVM->trpm.s.aGuestTrapHandler[iGate] == TRPM_INVALID_HANDLER)
697 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailNoHandler);
698 else
699 if (PATMIsPatchGCAddr(pVM, (RTGCPTR)pRegFrame->eip))
700 STAM_COUNTER_INC(&pVM->trpm.s.StatForwardFailPatchAddr);
701#endif
702 }
703failure:
704 STAM_COUNTER_INC(&CTXSUFF(pVM->trpm.s.StatForwardFail));
705
706 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]));
707#endif
708 return VINF_EM_RAW_GUEST_TRAP;
709}
710
711
712/**
713 * Raises a cpu exception which doesn't take an error code.
714 *
715 * This function may or may not dispatch the exception before returning.
716 *
717 * @returns VBox status code fit for scheduling.
718 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
719 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
720 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
721 *
722 * @param pVM The VM handle.
723 * @param pCtxCore The CPU context core.
724 * @param enmXcpt The exception.
725 */
726TRPMDECL(int) TRPMRaiseXcpt(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt)
727{
728 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x\n", pCtxCore->cs, pCtxCore->eip, enmXcpt));
729/** @todo dispatch the trap. */
730 pVM->trpm.s.uActiveVector = enmXcpt;
731 pVM->trpm.s.fActiveSoftwareInterrupt = false;
732 pVM->trpm.s.uActiveErrorCode = 0xdeadbeef;
733 pVM->trpm.s.uActiveCR2 = 0xdeadface;
734 return VINF_EM_RAW_GUEST_TRAP;
735}
736
737
738/**
739 * Raises a cpu exception with an errorcode.
740 *
741 * This function may or may not dispatch the exception before returning.
742 *
743 * @returns VBox status code fit for scheduling.
744 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
745 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
746 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
747 *
748 * @param pVM The VM handle.
749 * @param pCtxCore The CPU context core.
750 * @param enmXcpt The exception.
751 * @param uErr The error code.
752 */
753TRPMDECL(int) TRPMRaiseXcptErr(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr)
754{
755 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr));
756/** @todo dispatch the trap. */
757 pVM->trpm.s.uActiveVector = enmXcpt;
758 pVM->trpm.s.fActiveSoftwareInterrupt = false;
759 pVM->trpm.s.uActiveErrorCode = uErr;
760 pVM->trpm.s.uActiveCR2 = 0xdeadface;
761 return VINF_EM_RAW_GUEST_TRAP;
762}
763
764
765/**
766 * Raises a cpu exception with an errorcode and CR2.
767 *
768 * This function may or may not dispatch the exception before returning.
769 *
770 * @returns VBox status code fit for scheduling.
771 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
772 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
773 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
774 *
775 * @param pVM The VM handle.
776 * @param pCtxCore The CPU context core.
777 * @param enmXcpt The exception.
778 * @param uErr The error code.
779 * @param uCR2 The CR2 value.
780 */
781TRPMDECL(int) TRPMRaiseXcptErrCR2(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2)
782{
783 LogFlow(("TRPMRaiseXcptErr: cs:eip=%RTsel:%RX32 enmXcpt=%#x uErr=%RX32 uCR2=%RGv\n", pCtxCore->cs, pCtxCore->eip, enmXcpt, uErr, uCR2));
784/** @todo dispatch the trap. */
785 pVM->trpm.s.uActiveVector = enmXcpt;
786 pVM->trpm.s.fActiveSoftwareInterrupt = false;
787 pVM->trpm.s.uActiveErrorCode = uErr;
788 pVM->trpm.s.uActiveCR2 = uCR2;
789 return VINF_EM_RAW_GUEST_TRAP;
790}
791
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