1 | /* $Id: TRPMRC.cpp 58170 2015-10-12 09:27:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TRPM - The Trap Monitor, Guest Context
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
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 (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_TRPM
|
---|
23 | #include <VBox/vmm/trpm.h>
|
---|
24 | #include <VBox/vmm/cpum.h>
|
---|
25 | #include <VBox/vmm/em.h>
|
---|
26 | #include <VBox/vmm/vmm.h>
|
---|
27 | #include "TRPMInternal.h"
|
---|
28 | #include <VBox/vmm/vm.h>
|
---|
29 |
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/x86.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <VBox/vmm/selm.h>
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Arms a temporary trap handler for traps in Hypervisor code.
|
---|
41 | *
|
---|
42 | * The operation is similar to a System V signal handler. I.e. when the handler
|
---|
43 | * is called it is first set to default action. So, if you need to handler more
|
---|
44 | * than one trap, you must reinstall the handler.
|
---|
45 | *
|
---|
46 | * To uninstall the temporary handler, call this function with pfnHandler set to NULL.
|
---|
47 | *
|
---|
48 | * @returns VBox status code.
|
---|
49 | * @param pVM The cross context VM structure.
|
---|
50 | * @param iTrap Trap number to install handler [0..255].
|
---|
51 | * @param pfnHandler Pointer to the handler. Use NULL for uninstalling the handler.
|
---|
52 | */
|
---|
53 | VMMRCDECL(int) TRPMGCSetTempHandler(PVM pVM, unsigned iTrap, PFNTRPMGCTRAPHANDLER pfnHandler)
|
---|
54 | {
|
---|
55 | /*
|
---|
56 | * Validate input.
|
---|
57 | */
|
---|
58 | if (iTrap >= RT_ELEMENTS(pVM->trpm.s.aTmpTrapHandlers))
|
---|
59 | {
|
---|
60 | AssertMsgFailed(("Trap handler iTrap=%u is out of range!\n", iTrap));
|
---|
61 | return VERR_INVALID_PARAMETER;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Install handler.
|
---|
66 | */
|
---|
67 | pVM->trpm.s.aTmpTrapHandlers[iTrap] = (RTRCPTR)(uintptr_t)pfnHandler;
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Return to host context from a hypervisor trap handler.
|
---|
74 | *
|
---|
75 | * This function will *never* return.
|
---|
76 | * It will also reset any traps that are pending.
|
---|
77 | *
|
---|
78 | * @param pVM The cross context VM structure.
|
---|
79 | * @param rc The return code for host context.
|
---|
80 | */
|
---|
81 | VMMRCDECL(void) TRPMGCHyperReturnToHost(PVM pVM, int rc)
|
---|
82 | {
|
---|
83 | PVMCPU pVCpu = VMMGetCpu0(pVM);
|
---|
84 |
|
---|
85 | LogFlow(("TRPMGCHyperReturnToHost: rc=%Rrc\n", rc));
|
---|
86 | TRPMResetTrap(pVCpu);
|
---|
87 | VMMRCGuestToHost(pVM, rc);
|
---|
88 | AssertReleaseFailed();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * @callback_method_impl{FNPGMRCVIRTPFHANDLER,
|
---|
94 | * \#PF Virtual Handler callback for Guest write access to the Guest's own current IDT.}
|
---|
95 | */
|
---|
96 | DECLEXPORT(VBOXSTRICTRC) trpmRCGuestIDTWritePfHandler(PVM pVM, PVMCPU pVCpu, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame,
|
---|
97 | RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange, void *pvUser)
|
---|
98 | {
|
---|
99 | uint16_t cbIDT;
|
---|
100 | RTGCPTR GCPtrIDT = (RTGCPTR)CPUMGetGuestIDTR(pVCpu, &cbIDT);
|
---|
101 | #ifdef VBOX_STRICT
|
---|
102 | RTGCPTR GCPtrIDTEnd = (RTGCPTR)((RTGCUINTPTR)GCPtrIDT + cbIDT + 1);
|
---|
103 | #endif
|
---|
104 | uint32_t iGate = ((RTGCUINTPTR)pvFault - (RTGCUINTPTR)GCPtrIDT)/sizeof(VBOXIDTE);
|
---|
105 |
|
---|
106 | AssertMsg(offRange < (uint32_t)cbIDT+1, ("pvFault=%RGv GCPtrIDT=%RGv-%RGv pvRange=%RGv\n", pvFault, GCPtrIDT, GCPtrIDTEnd, pvRange));
|
---|
107 | Assert((RTGCPTR)(RTRCUINTPTR)pvRange == GCPtrIDT);
|
---|
108 | NOREF(uErrorCode); NOREF(pvUser);
|
---|
109 |
|
---|
110 | #if 0
|
---|
111 | /* Note! this causes problems in Windows XP as instructions following the update can be dangerous (str eax has been seen) */
|
---|
112 | /* Note! not going back to ring 3 could make the code scanner miss them. */
|
---|
113 | /* Check if we can handle the write here. */
|
---|
114 | if ( iGate != 3 /* Gate 3 is handled differently; could do it here as well, but let ring 3 handle this case for now. */
|
---|
115 | && !ASMBitTest(&pVM->trpm.s.au32IdtPatched[0], iGate)) /* Passthru gates need special attention too. */
|
---|
116 | {
|
---|
117 | uint32_t cb;
|
---|
118 | int rc = EMInterpretInstructionEx(pVM, pVCpu, pRegFrame, pvFault, &cb);
|
---|
119 | if (RT_SUCCESS(rc) && cb)
|
---|
120 | {
|
---|
121 | uint32_t iGate1 = (offRange + cb - 1)/sizeof(VBOXIDTE);
|
---|
122 |
|
---|
123 | Log(("trpmRCGuestIDTWriteHandler: write to gate %x (%x) offset %x cb=%d\n", iGate, iGate1, offRange, cb));
|
---|
124 |
|
---|
125 | trpmClearGuestTrapHandler(pVM, iGate);
|
---|
126 | if (iGate != iGate1)
|
---|
127 | trpmClearGuestTrapHandler(pVM, iGate1);
|
---|
128 |
|
---|
129 | STAM_COUNTER_INC(&pVM->trpm.s.StatRCWriteGuestIDTHandled);
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | #else
|
---|
134 | NOREF(iGate);
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | Log(("trpmRCGuestIDTWritePfHandler: eip=%RGv write to gate %x offset %x\n", pRegFrame->eip, iGate, offRange));
|
---|
138 |
|
---|
139 | /** @todo Check which IDT entry and keep the update cost low in TRPMR3SyncIDT() and CSAMCheckGates(). */
|
---|
140 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TRPM_SYNC_IDT);
|
---|
141 |
|
---|
142 | STAM_COUNTER_INC(&pVM->trpm.s.StatRCWriteGuestIDTFault);
|
---|
143 | return VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * @callback_method_impl{FNPGMRCVIRTPFHANDLER,
|
---|
149 | * \#PF Virtual Handler callback for Guest write access to the VBox shadow IDT.}
|
---|
150 | */
|
---|
151 | DECLEXPORT(VBOXSTRICTRC) trpmRCShadowIDTWritePfHandler(PVM pVM, PVMCPU pVCpu, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame,
|
---|
152 | RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange, void *pvUser)
|
---|
153 | {
|
---|
154 | LogRel(("FATAL ERROR: trpmRCShadowIDTWritePfHandler: eip=%08X pvFault=%RGv pvRange=%08RGv\r\n", pRegFrame->eip, pvFault, pvRange));
|
---|
155 | NOREF(uErrorCode); NOREF(offRange); NOREF(pvUser);
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * If we ever get here, then the guest has *probably* executed an SIDT
|
---|
159 | * instruction that we failed to patch. In theory this could be very bad,
|
---|
160 | * but there are nasty applications out there that install device drivers
|
---|
161 | * that mess with the guest's IDT. In those cases, it's quite ok to simply
|
---|
162 | * ignore the writes and pretend success.
|
---|
163 | *
|
---|
164 | * Another posibility is that the guest is touching some page memory and
|
---|
165 | * it having nothing to do with our IDT or anything like that, just a
|
---|
166 | * potential conflict that we didn't discover in time.
|
---|
167 | */
|
---|
168 | DISSTATE Dis;
|
---|
169 | int rc = EMInterpretDisasCurrent(pVM, pVCpu, &Dis, NULL);
|
---|
170 | if (rc == VINF_SUCCESS)
|
---|
171 | {
|
---|
172 | /* Just ignore the write. */
|
---|
173 | pRegFrame->eip += Dis.cbInstr;
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return VERR_TRPM_SHADOW_IDT_WRITE;
|
---|
178 | }
|
---|
179 |
|
---|