VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/PATMGuest.cpp@ 17206

Last change on this file since 17206 was 13835, checked in by vboxsync, 16 years ago

s/VBOX_SUCCESS/RT_SUCCESS/g s/VBOX_FAILURE/RT_FAILURE/g - VBOX_SUCCESS and VBOX_FAILURE have *NOT* been retired (because old habbits die hard) just sligtly deprecated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1/* $Id: PATMGuest.cpp 13835 2008-11-05 02:34:43Z vboxsync $ */
2/** @file
3 * PATMGuest - Guest OS Patching Manager (non-generic)
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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_PATM
26#include <VBox/patm.h>
27#include <VBox/stam.h>
28#include <VBox/pgm.h>
29#include <VBox/cpum.h>
30#include <VBox/iom.h>
31#include <VBox/sup.h>
32#include <VBox/mm.h>
33#include <VBox/ssm.h>
34#include <VBox/pdm.h>
35#include <VBox/trpm.h>
36#include <VBox/param.h>
37#include <iprt/avl.h>
38#include "PATMInternal.h"
39#include <VBox/vm.h>
40#include <VBox/csam.h>
41
42#include <VBox/dbg.h>
43#include <VBox/err.h>
44#include <VBox/log.h>
45#include <iprt/assert.h>
46#include <iprt/string.h>
47#include <VBox/dis.h>
48#include <VBox/disopcode.h>
49
50/*
51 * ntdll!KiFastSystemCall:
52 * 7c90eb8b 8bd4 mov edx,esp
53 * 7c90eb8d 0f34 sysenter
54 * 7c90eb8f 90 nop
55 * 7c90eb90 90 nop
56 * 7c90eb91 90 nop
57 * 7c90eb92 90 nop
58 * 7c90eb93 90 nop
59 * ntdll!KiFastSystemCallRet:
60 * 7c90eb94 c3 ret
61 *
62 * ntdll!KiIntSystemCall:
63 * 7c90eba5 8d542408 lea edx,[esp+0x8]
64 * 7c90eba9 cd2e int 2e
65 * 7c90ebab c3 ret
66 *
67 */
68static uint8_t uFnKiFastSystemCall[7] = {0x8b, 0xd4, 0x0f, 0x34, 0x90, 0x90, 0x90};
69static uint8_t uFnKiIntSystemCall[7] = {0x8d, 0x54, 0x24, 0x08, 0xcd, 0x2e, 0xc3};
70
71/*
72 * OpenBSD 3.7 & 3.8:
73 *
74 * D0101B6D: push CS [0E]
75 * D0101B6E: push ESI [56]
76 * D0101B6F: cli [FA]
77 */
78static uint8_t uFnOpenBSDHandlerPrefix1[3] = { 0x0E, 0x56, 0xFA };
79/*
80 * OpenBSD 3.9 & 4.0
81 *
82 * D0101BD1: push CS [0E]
83 * D0101BD2: push ESI [56]
84 * D0101BD3: push 0x00 [6A 00]
85 * D0101BD4: push 0x03 [6A 03]
86 */
87static uint8_t uFnOpenBSDHandlerPrefix2[6] = { 0x0E, 0x56, 0x6A, 0x00, 0x6A, 0x03 };
88
89
90/**
91 * Check Windows XP sysenter heuristics and install patch
92 *
93 * @returns VBox status code.
94 * @param pVM The VM to operate on.
95 * @param pInstrGC GC Instruction pointer for sysenter
96 * @param pPatchRec Patch structure
97 *
98 */
99int PATMPatchSysenterXP(PVM pVM, RTGCPTR32 pInstrGC, PPATMPATCHREC pPatchRec)
100{
101 PPATCHINFO pPatch = &pPatchRec->patch;
102 uint8_t uTemp[16];
103 RTGCPTR32 lpfnKiFastSystemCall, lpfnKiIntSystemCall = 0; /* (initializing it to shut up warning.) */
104 int rc, i;
105
106 Assert(sizeof(uTemp) > sizeof(uFnKiIntSystemCall));
107 Assert(sizeof(uTemp) > sizeof(uFnKiFastSystemCall));
108
109 /* Guest OS specific patch; check heuristics first */
110
111 /* check the epilog of KiFastSystemCall */
112 lpfnKiFastSystemCall = pInstrGC - 2;
113 rc = PGMPhysSimpleReadGCPtr(pVM, uTemp, lpfnKiFastSystemCall, sizeof(uFnKiFastSystemCall));
114 if ( RT_FAILURE(rc)
115 || memcmp(uFnKiFastSystemCall, uTemp, sizeof(uFnKiFastSystemCall)))
116 {
117 return VERR_PATCHING_REFUSED;
118 }
119
120 /* Now search for KiIntSystemCall */
121 for (i=0;i<64;i++)
122 {
123 rc = PGMPhysSimpleReadGCPtr(pVM, uTemp, pInstrGC + i, sizeof(uFnKiIntSystemCall));
124 if(RT_FAILURE(rc))
125 {
126 break;
127 }
128 if(!memcmp(uFnKiIntSystemCall, uTemp, sizeof(uFnKiIntSystemCall)))
129 {
130 lpfnKiIntSystemCall = pInstrGC + i;
131 /* Found it! */
132 break;
133 }
134 }
135 if (i == 64)
136 {
137 Log(("KiIntSystemCall not found!!\n"));
138 return VERR_PATCHING_REFUSED;
139 }
140
141 if (PAGE_ADDRESS(lpfnKiFastSystemCall) != PAGE_ADDRESS(lpfnKiIntSystemCall))
142 {
143 Log(("KiFastSystemCall and KiIntSystemCall not in the same page!!\n"));
144 return VERR_PATCHING_REFUSED;
145 }
146
147 // make a copy of the guest code bytes that will be overwritten
148 rc = PGMPhysSimpleReadGCPtr(pVM, pPatch->aPrivInstr, pPatch->pPrivInstrGC, SIZEOF_NEARJUMP32);
149 AssertRC(rc);
150
151 /* Now we simply jump from the fast version to the 'old and slow' system call */
152 uTemp[0] = 0xE9;
153 *(RTGCPTR32 *)&uTemp[1] = lpfnKiIntSystemCall - (pInstrGC + SIZEOF_NEARJUMP32);
154 rc = PGMPhysSimpleDirtyWriteGCPtr(pVM, pInstrGC, uTemp, SIZEOF_NEARJUMP32);
155 if (RT_FAILURE(rc))
156 {
157 Log(("MMR3PhysWriteGCVirt failed with rc=%d!!\n", rc));
158 return VERR_PATCHING_REFUSED;
159 }
160
161#ifdef DEBUG
162 Log(("Sysenter Patch code ----------------------------------------------------------\n"));
163 patmr3DisasmCodeStream(pVM, pInstrGC, pInstrGC, patmr3DisasmCallback, pPatch);
164 Log(("Sysenter Patch code ends -----------------------------------------------------\n"));
165#endif
166
167 pPatch->uState = PATCH_ENABLED;
168 return VINF_SUCCESS;
169}
170
171/**
172 * Patch OpenBSD interrupt handler prefix
173 *
174 * @returns VBox status code.
175 * @param pVM The VM to operate on
176 * @param pCpu Disassembly state of instruction.
177 * @param pInstrGC GC Instruction pointer for instruction
178 * @param pInstrHC GC Instruction pointer for instruction
179 * @param pPatchRec Patch structure
180 *
181 */
182int PATMPatchOpenBSDHandlerPrefix(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
183{
184 uint8_t uTemp[16];
185 int rc;
186
187 Assert(sizeof(uTemp) > RT_MAX(sizeof(uFnOpenBSDHandlerPrefix1), sizeof(uFnOpenBSDHandlerPrefix2)));
188
189 /* Guest OS specific patch; check heuristics first */
190
191 rc = PGMPhysSimpleReadGCPtr(pVM, uTemp, pInstrGC, RT_MAX(sizeof(uFnOpenBSDHandlerPrefix1), sizeof(uFnOpenBSDHandlerPrefix2)));
192 if ( RT_FAILURE(rc)
193 || ( memcmp(uFnOpenBSDHandlerPrefix1, uTemp, sizeof(uFnOpenBSDHandlerPrefix1))
194 && memcmp(uFnOpenBSDHandlerPrefix2, uTemp, sizeof(uFnOpenBSDHandlerPrefix2))))
195 {
196 return VERR_PATCHING_REFUSED;
197 }
198 /* Found it; patch the push cs */
199 pPatchRec->patch.flags &= ~(PATMFL_GUEST_SPECIFIC); /* prevent a breakpoint from being triggered */
200 return PATMR3PatchInstrInt3(pVM, pInstrGC, pInstrHC, pCpu, &pPatchRec->patch);
201}
202
203/**
204 * Install guest OS specific patch
205 *
206 * @returns VBox status code.
207 * @param pVM The VM to operate on
208 * @param pCpu Disassembly state of instruction.
209 * @param pInstrGC GC Instruction pointer for instruction
210 * @param pInstrHC GC Instruction pointer for instruction
211 * @param pCallerGC GC address of caller; CODE32_UNKNOWN_CALLER if unknown
212 * @param pPatchRec Patch structure
213 *
214 */
215int PATMInstallGuestSpecificPatch(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
216{
217 int rc;
218
219 /** @todo might have to check if the patch crosses a page boundary. Currently not necessary, but that might change in the future!! */
220 switch (pCpu->pCurInstr->opcode)
221 {
222 case OP_SYSENTER:
223 pPatchRec->patch.flags |= PATMFL_SYSENTER_XP | PATMFL_USER_MODE | PATMFL_GUEST_SPECIFIC;
224
225 rc = PATMPatchSysenterXP(pVM, pInstrGC, pPatchRec);
226 if (RT_FAILURE(rc))
227 {
228 return VERR_PATCHING_REFUSED;
229 }
230 return VINF_SUCCESS;
231
232 case OP_PUSH:
233 /* OpenBSD guest specific patch for the following code block:
234 *
235 * pushf
236 * push cs <- dangerous because of DPL 0 tests
237 * push esi
238 * cli
239 */
240 if (pCpu->pCurInstr->param1 == OP_PARM_REG_CS)
241 return PATMPatchOpenBSDHandlerPrefix(pVM, pCpu, pInstrGC, pInstrHC, pPatchRec);
242
243 return VERR_PATCHING_REFUSED;
244
245 default:
246 AssertMsgFailed(("PATMInstallGuestSpecificPatch: unknown opcode %d\n", pCpu->pCurInstr->opcode));
247 return VERR_PATCHING_REFUSED;
248 }
249 return VERR_PATCHING_REFUSED;
250}
251
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