VirtualBox

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

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

Big step to separate VMM data structures for guest SMP. (pgm, em)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/* $Id: PATMGuest.cpp 18927 2009-04-16 11:41:38Z 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 PVMCPU pVCpu = VMMGetCpu0(pVM);
106
107 Assert(sizeof(uTemp) > sizeof(uFnKiIntSystemCall));
108 Assert(sizeof(uTemp) > sizeof(uFnKiFastSystemCall));
109
110 /* Guest OS specific patch; check heuristics first */
111
112 /* check the epilog of KiFastSystemCall */
113 lpfnKiFastSystemCall = pInstrGC - 2;
114 rc = PGMPhysSimpleReadGCPtr(pVCpu, uTemp, lpfnKiFastSystemCall, sizeof(uFnKiFastSystemCall));
115 if ( RT_FAILURE(rc)
116 || memcmp(uFnKiFastSystemCall, uTemp, sizeof(uFnKiFastSystemCall)))
117 {
118 return VERR_PATCHING_REFUSED;
119 }
120
121 /* Now search for KiIntSystemCall */
122 for (i=0;i<64;i++)
123 {
124 rc = PGMPhysSimpleReadGCPtr(pVCpu, uTemp, pInstrGC + i, sizeof(uFnKiIntSystemCall));
125 if(RT_FAILURE(rc))
126 {
127 break;
128 }
129 if(!memcmp(uFnKiIntSystemCall, uTemp, sizeof(uFnKiIntSystemCall)))
130 {
131 lpfnKiIntSystemCall = pInstrGC + i;
132 /* Found it! */
133 break;
134 }
135 }
136 if (i == 64)
137 {
138 Log(("KiIntSystemCall not found!!\n"));
139 return VERR_PATCHING_REFUSED;
140 }
141
142 if (PAGE_ADDRESS(lpfnKiFastSystemCall) != PAGE_ADDRESS(lpfnKiIntSystemCall))
143 {
144 Log(("KiFastSystemCall and KiIntSystemCall not in the same page!!\n"));
145 return VERR_PATCHING_REFUSED;
146 }
147
148 // make a copy of the guest code bytes that will be overwritten
149 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aPrivInstr, pPatch->pPrivInstrGC, SIZEOF_NEARJUMP32);
150 AssertRC(rc);
151
152 /* Now we simply jump from the fast version to the 'old and slow' system call */
153 uTemp[0] = 0xE9;
154 *(RTGCPTR32 *)&uTemp[1] = lpfnKiIntSystemCall - (pInstrGC + SIZEOF_NEARJUMP32);
155 rc = PGMPhysSimpleDirtyWriteGCPtr(pVCpu, pInstrGC, uTemp, SIZEOF_NEARJUMP32);
156 if (RT_FAILURE(rc))
157 {
158 Log(("MMR3PhysWriteGCVirt failed with rc=%d!!\n", rc));
159 return VERR_PATCHING_REFUSED;
160 }
161
162#ifdef DEBUG
163 Log(("Sysenter Patch code ----------------------------------------------------------\n"));
164 patmr3DisasmCodeStream(pVM, pInstrGC, pInstrGC, patmr3DisasmCallback, pPatch);
165 Log(("Sysenter Patch code ends -----------------------------------------------------\n"));
166#endif
167
168 pPatch->uState = PATCH_ENABLED;
169 return VINF_SUCCESS;
170}
171
172/**
173 * Patch OpenBSD interrupt handler prefix
174 *
175 * @returns VBox status code.
176 * @param pVM The VM to operate on
177 * @param pCpu Disassembly state of instruction.
178 * @param pInstrGC GC Instruction pointer for instruction
179 * @param pInstrHC GC Instruction pointer for instruction
180 * @param pPatchRec Patch structure
181 *
182 */
183int PATMPatchOpenBSDHandlerPrefix(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
184{
185 uint8_t uTemp[16];
186 int rc;
187
188 Assert(sizeof(uTemp) > RT_MAX(sizeof(uFnOpenBSDHandlerPrefix1), sizeof(uFnOpenBSDHandlerPrefix2)));
189
190 /* Guest OS specific patch; check heuristics first */
191
192 rc = PGMPhysSimpleReadGCPtr(VMMGetCpu0(pVM), uTemp, pInstrGC, RT_MAX(sizeof(uFnOpenBSDHandlerPrefix1), sizeof(uFnOpenBSDHandlerPrefix2)));
193 if ( RT_FAILURE(rc)
194 || ( memcmp(uFnOpenBSDHandlerPrefix1, uTemp, sizeof(uFnOpenBSDHandlerPrefix1))
195 && memcmp(uFnOpenBSDHandlerPrefix2, uTemp, sizeof(uFnOpenBSDHandlerPrefix2))))
196 {
197 return VERR_PATCHING_REFUSED;
198 }
199 /* Found it; patch the push cs */
200 pPatchRec->patch.flags &= ~(PATMFL_GUEST_SPECIFIC); /* prevent a breakpoint from being triggered */
201 return PATMR3PatchInstrInt3(pVM, pInstrGC, pInstrHC, pCpu, &pPatchRec->patch);
202}
203
204/**
205 * Install guest OS specific patch
206 *
207 * @returns VBox status code.
208 * @param pVM The VM to operate on
209 * @param pCpu Disassembly state of instruction.
210 * @param pInstrGC GC Instruction pointer for instruction
211 * @param pInstrHC GC Instruction pointer for instruction
212 * @param pCallerGC GC address of caller; CODE32_UNKNOWN_CALLER if unknown
213 * @param pPatchRec Patch structure
214 *
215 */
216int PATMInstallGuestSpecificPatch(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
217{
218 int rc;
219
220 /** @todo might have to check if the patch crosses a page boundary. Currently not necessary, but that might change in the future!! */
221 switch (pCpu->pCurInstr->opcode)
222 {
223 case OP_SYSENTER:
224 pPatchRec->patch.flags |= PATMFL_SYSENTER_XP | PATMFL_USER_MODE | PATMFL_GUEST_SPECIFIC;
225
226 rc = PATMPatchSysenterXP(pVM, pInstrGC, pPatchRec);
227 if (RT_FAILURE(rc))
228 {
229 return VERR_PATCHING_REFUSED;
230 }
231 return VINF_SUCCESS;
232
233 case OP_PUSH:
234 /* OpenBSD guest specific patch for the following code block:
235 *
236 * pushf
237 * push cs <- dangerous because of DPL 0 tests
238 * push esi
239 * cli
240 */
241 if (pCpu->pCurInstr->param1 == OP_PARM_REG_CS)
242 return PATMPatchOpenBSDHandlerPrefix(pVM, pCpu, pInstrGC, pInstrHC, pPatchRec);
243
244 return VERR_PATCHING_REFUSED;
245
246 default:
247 AssertMsgFailed(("PATMInstallGuestSpecificPatch: unknown opcode %d\n", pCpu->pCurInstr->opcode));
248 return VERR_PATCHING_REFUSED;
249 }
250 return VERR_PATCHING_REFUSED;
251}
252
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