VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMGC/TRPMGC.cpp@ 17106

Last change on this file since 17106 was 13823, checked in by vboxsync, 16 years ago

VMM: %VGv -> %RGv

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1/* $Id: TRPMGC.cpp 13823 2008-11-05 01:10:20Z vboxsync $ */
2/** @file
3 * TRPM - The Trap Monitor, Guest Context
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
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#include <VBox/selm.h>
40
41
42
43/**
44 * Arms a temporary trap handler for traps in Hypervisor code.
45 *
46 * The operation is similar to a System V signal handler. I.e. when the handler
47 * is called it is first set to default action. So, if you need to handler more
48 * than one trap, you must reinstall the handler.
49 *
50 * To uninstall the temporary handler, call this function with pfnHandler set to NULL.
51 *
52 * @returns VBox status.
53 * @param pVM VM handle.
54 * @param iTrap Trap number to install handler [0..255].
55 * @param pfnHandler Pointer to the handler. Use NULL for uninstalling the handler.
56 */
57VMMRCDECL(int) TRPMGCSetTempHandler(PVM pVM, unsigned iTrap, PFNTRPMGCTRAPHANDLER pfnHandler)
58{
59 /*
60 * Validate input.
61 */
62 if (iTrap >= RT_ELEMENTS(pVM->trpm.s.aTmpTrapHandlers))
63 {
64 AssertMsgFailed(("Trap handler iTrap=%u is out of range!\n", iTrap));
65 return VERR_INVALID_PARAMETER;
66 }
67
68 /*
69 * Install handler.
70 */
71 pVM->trpm.s.aTmpTrapHandlers[iTrap] = (RTRCPTR)(uintptr_t)pfnHandler;
72 return VINF_SUCCESS;
73}
74
75
76/**
77 * Return to host context from a hypervisor trap handler.
78 *
79 * This function will *never* return.
80 * It will also reset any traps that are pending.
81 *
82 * @param pVM The VM handle.
83 * @param rc The return code for host context.
84 */
85VMMRCDECL(void) TRPMGCHyperReturnToHost(PVM pVM, int rc)
86{
87 LogFlow(("TRPMGCHyperReturnToHost: rc=%Rrc\n", rc));
88 TRPMResetTrap(pVM);
89 CPUMHyperSetCtxCore(pVM, NULL);
90 VMMGCGuestToHost(pVM, rc);
91 AssertReleaseFailed();
92}
93
94
95/**
96 * \#PF Virtual Handler callback for Guest write access to the Guest's own current IDT.
97 *
98 * @returns VBox status code (appropriate for trap handling and GC return).
99 * @param pVM VM Handle.
100 * @param uErrorCode CPU Error code.
101 * @param pRegFrame Trap register frame.
102 * @param pvFault The fault address (cr2).
103 * @param pvRange The base address of the handled virtual range.
104 * @param offRange The offset of the access into this range.
105 * (If it's a EIP range this's the EIP, if not it's pvFault.)
106 */
107VMMRCDECL(int) trpmRCGuestIDTWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange)
108{
109 uint16_t cbIDT;
110 RTGCPTR GCPtrIDT = (RTGCPTR)CPUMGetGuestIDTR(pVM, &cbIDT);
111#ifdef VBOX_STRICT
112 RTGCPTR GCPtrIDTEnd = (RTGCPTR)((RTGCUINTPTR)GCPtrIDT + cbIDT + 1);
113#endif
114 uint32_t iGate = ((RTGCUINTPTR)pvFault - (RTGCUINTPTR)GCPtrIDT)/sizeof(VBOXIDTE);
115
116 AssertMsg(offRange < (uint32_t)cbIDT+1, ("pvFault=%RGv GCPtrIDT=%RGv-%RGv pvRange=%RGv\n", pvFault, GCPtrIDT, GCPtrIDTEnd, pvRange));
117 Assert((RTGCPTR)(RTRCUINTPTR)pvRange == GCPtrIDT);
118
119#if 0
120 /* Note! this causes problems in Windows XP as instructions following the update can be dangerous (str eax has been seen) */
121 /* Note! not going back to ring 3 could make the code scanner miss them. */
122 /* Check if we can handle the write here. */
123 if ( iGate != 3 /* Gate 3 is handled differently; could do it here as well, but let ring 3 handle this case for now. */
124 && !ASMBitTest(&pVM->trpm.s.au32IdtPatched[0], iGate)) /* Passthru gates need special attention too. */
125 {
126 uint32_t cb;
127 int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
128 if (RT_SUCCESS(rc) && cb)
129 {
130 uint32_t iGate1 = (offRange + cb - 1)/sizeof(VBOXIDTE);
131
132 Log(("trpmRCGuestIDTWriteHandler: write to gate %x (%x) offset %x cb=%d\n", iGate, iGate1, offRange, cb));
133
134 trpmClearGuestTrapHandler(pVM, iGate);
135 if (iGate != iGate1)
136 trpmClearGuestTrapHandler(pVM, iGate1);
137
138 STAM_COUNTER_INC(&pVM->trpm.s.StatRCWriteGuestIDTHandled);
139 return VINF_SUCCESS;
140 }
141 }
142#else
143 NOREF(iGate);
144#endif
145
146 Log(("trpmRCGuestIDTWriteHandler: eip=%RGv write to gate %x offset %x\n", pRegFrame->eip, iGate, offRange));
147
148 /** @todo Check which IDT entry and keep the update cost low in TRPMR3SyncIDT() and CSAMCheckGates(). */
149 VM_FF_SET(pVM, VM_FF_TRPM_SYNC_IDT);
150
151 STAM_COUNTER_INC(&pVM->trpm.s.StatRCWriteGuestIDTFault);
152 return VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT;
153}
154
155
156/**
157 * \#PF Virtual Handler callback for Guest write access to the VBox shadow IDT.
158 *
159 * @returns VBox status code (appropriate for trap handling and GC return).
160 * @param pVM VM Handle.
161 * @param uErrorCode CPU Error code.
162 * @param pRegFrame Trap register frame.
163 * @param pvFault The fault address (cr2).
164 * @param pvRange The base address of the handled virtual range.
165 * @param offRange The offset of the access into this range.
166 * (If it's a EIP range this's the EIP, if not it's pvFault.)
167 */
168VMMRCDECL(int) trpmRCShadowIDTWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange)
169{
170 LogRel(("FATAL ERROR: trpmRCShadowIDTWriteHandler: eip=%08X pvFault=%RGv pvRange=%08X\r\n", pRegFrame->eip, pvFault, pvRange));
171
172 /* If we ever get here, then the guest has executed an sidt instruction that we failed to patch. In theory this could be very bad, but
173 * there are nasty applications out there that install device drivers that mess with the guest's IDT. In those cases, it's quite ok
174 * to simply ignore the writes and pretend success.
175 */
176 RTGCPTR PC;
177 int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &PC);
178 if (rc == VINF_SUCCESS)
179 {
180 DISCPUSTATE Cpu;
181 uint32_t cbOp;
182 rc = EMInterpretDisasOneEx(pVM, (RTGCUINTPTR)PC, pRegFrame, &Cpu, &cbOp);
183 if (rc == VINF_SUCCESS)
184 {
185 /* Just ignore the write. */
186 pRegFrame->eip += Cpu.opsize;
187 return VINF_SUCCESS;
188 }
189 }
190
191 return VERR_TRPM_SHADOW_IDT_WRITE;
192}
193
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