1 | /* $Id: TRPMGC.cpp 1329 2007-03-08 13:07:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TRPM - The Trap Monitor, Guest 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/cpum.h>
|
---|
29 | #include <VBox/vmm.h>
|
---|
30 | #include "TRPMInternal.h"
|
---|
31 | #include <VBox/vm.h>
|
---|
32 |
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/x86.h>
|
---|
35 | #include <VBox/em.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <VBox/log.h>
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Arms a temporary trap handler for traps in Hypervisor code.
|
---|
44 | *
|
---|
45 | * The operation is similar to a System V signal handler. I.e. when the handler
|
---|
46 | * is called it is first set to default action. So, if you need to handler more
|
---|
47 | * than one trap, you must reinstall the handler.
|
---|
48 | *
|
---|
49 | * To uninstall the temporary handler, call this function with pfnHandler set to NULL.
|
---|
50 | *
|
---|
51 | * @returns VBox status.
|
---|
52 | * @param pVM VM handle.
|
---|
53 | * @param iTrap Trap number to install handler [0..255].
|
---|
54 | * @param pfnHandler Pointer to the handler. Use NULL for uninstalling the handler.
|
---|
55 | */
|
---|
56 | TRPMGCDECL(int) TRPMGCSetTempHandler(PVM pVM, unsigned iTrap, PFNTRPMGCTRAPHANDLER pfnHandler)
|
---|
57 | {
|
---|
58 | /*
|
---|
59 | * Validate input.
|
---|
60 | */
|
---|
61 | if (iTrap >= ELEMENTS(pVM->trpm.s.aTmpTrapHandlers))
|
---|
62 | {
|
---|
63 | AssertMsgFailed(("Trap handler iTrap=%u is out of range!\n", iTrap));
|
---|
64 | return VERR_INVALID_PARAMETER;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Install handler.
|
---|
69 | */
|
---|
70 | pVM->trpm.s.aTmpTrapHandlers[iTrap] = (RTGCPTR)(RTGCUINTPTR)pfnHandler;
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Return to host context from a hypervisor trap handler.
|
---|
77 | *
|
---|
78 | * This function will *never* return.
|
---|
79 | * It will also reset any traps that are pending.
|
---|
80 | *
|
---|
81 | * @param pVM The VM handle.
|
---|
82 | * @param rc The return code for host context.
|
---|
83 | */
|
---|
84 | TRPMGCDECL(void) TRPMGCHyperReturnToHost(PVM pVM, int rc)
|
---|
85 | {
|
---|
86 | LogFlow(("TRPMGCHyperReturnToHost: rc=%Vrc\n", rc));
|
---|
87 | TRPMResetTrap(pVM);
|
---|
88 | CPUMHyperSetCtxCore(pVM, NULL);
|
---|
89 | VMMGCGuestToHost(pVM, rc);
|
---|
90 | AssertReleaseFailed();
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * \#PF Virtual Handler callback for Guest write access to the Guest's own current IDT.
|
---|
96 | *
|
---|
97 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
98 | * @param pVM VM Handle.
|
---|
99 | * @param uErrorCode CPU Error code.
|
---|
100 | * @param pRegFrame Trap register frame.
|
---|
101 | * @param pvFault The fault address (cr2).
|
---|
102 | * @param pvRange The base address of the handled virtual range.
|
---|
103 | * @param offRange The offset of the access into this range.
|
---|
104 | * (If it's a EIP range this's the EIP, if not it's pvFault.)
|
---|
105 | */
|
---|
106 | TRPMGCDECL(int) trpmgcGuestIDTWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, void *pvRange, uintptr_t offRange)
|
---|
107 | {
|
---|
108 | uint16_t cbIDT;
|
---|
109 | RTGCPTR GCPtrIDT = (RTGCPTR)CPUMGetGuestIDTR(pVM, &cbIDT);
|
---|
110 | #ifdef VBOX_STRICT
|
---|
111 | RTGCPTR GCPtrIDTEnd = (RTGCPTR)((RTGCUINTPTR)GCPtrIDT + cbIDT + 1);
|
---|
112 | #endif
|
---|
113 | uint32_t iTrap = ((RTGCUINTPTR)pvFault - (RTGCUINTPTR)GCPtrIDT)/sizeof(VBOXIDTE);
|
---|
114 |
|
---|
115 | Assert(pvFault >= GCPtrIDT && pvFault < GCPtrIDTEnd);
|
---|
116 | Log(("trpmgcGuestIDTWriteHandler: write to gate %x\n", iTrap));
|
---|
117 |
|
---|
118 | /* Check if we can handle the write here. */
|
---|
119 | if ( iTrap != 3 /* Gate 3 is handled differently; could do it here as well, but let ring 3 handle this case for now. */
|
---|
120 | && !ASMBitTest(&pVM->trpm.s.au32IdtPatched[0], iTrap)) /* Passthru gates need special attention too. */
|
---|
121 | {
|
---|
122 | uint32_t cb;
|
---|
123 | int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
|
---|
124 | if (VBOX_SUCCESS(rc) && cb)
|
---|
125 | {
|
---|
126 | trpmClearGuestTrapHandler(pVM, iTrap);
|
---|
127 | STAM_COUNTER_INC(&pVM->trpm.s.StatGCWriteGuestIDTHandled);
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** @todo Check which IDT entry and keep the update cost low in TRPMR3SyncIDT() and CSAMCheckGates(). */
|
---|
133 | VM_FF_SET(pVM, VM_FF_TRPM_SYNC_IDT);
|
---|
134 |
|
---|
135 | STAM_COUNTER_INC(&pVM->trpm.s.StatGCWriteGuestIDTFault);
|
---|
136 | return VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * \#PF Virtual Handler callback for Guest write access to the VBox shadow IDT.
|
---|
142 | *
|
---|
143 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
144 | * @param pVM VM Handle.
|
---|
145 | * @param uErrorCode CPU Error code.
|
---|
146 | * @param pRegFrame Trap register frame.
|
---|
147 | * @param pvFault The fault address (cr2).
|
---|
148 | * @param pvRange The base address of the handled virtual range.
|
---|
149 | * @param offRange The offset of the access into this range.
|
---|
150 | * (If it's a EIP range this's the EIP, if not it's pvFault.)
|
---|
151 | */
|
---|
152 | TRPMGCDECL(int) trpmgcShadowIDTWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, void *pvRange, uintptr_t offRange)
|
---|
153 | {
|
---|
154 | ////LogCom(("trpmgcShadowIDTWriteHandler: eip=%08X pvFault=%08X pvRange=%08X\r\n", pRegFrame->eip, pvFault, pvRange));
|
---|
155 | return VERR_TRPM_SHADOW_IDT_WRITE;
|
---|
156 | }
|
---|
157 |
|
---|