PATMGuest.cpp revision 5aefacb29c648cdea4211b783e04d52223ddb257
/* $Id$ */
/** @file
* PATMGuest - Guest OS Patching Manager (non-generic)
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PATM
#include "PATMInternal.h"
#include <VBox/disopcode.h>
/*
* ntdll!KiFastSystemCall:
* 7c90eb8b 8bd4 mov edx,esp
* 7c90eb8d 0f34 sysenter
* 7c90eb8f 90 nop
* 7c90eb90 90 nop
* 7c90eb91 90 nop
* 7c90eb92 90 nop
* 7c90eb93 90 nop
* ntdll!KiFastSystemCallRet:
* 7c90eb94 c3 ret
*
* ntdll!KiIntSystemCall:
* 7c90eba5 8d542408 lea edx,[esp+0x8]
* 7c90eba9 cd2e int 2e
* 7c90ebab c3 ret
*
*/
/*
* OpenBSD 3.7 & 3.8:
*
* D0101B6D: push CS [0E]
* D0101B6E: push ESI [56]
* D0101B6F: cli [FA]
*/
/*
* OpenBSD 3.9 & 4.0
*
* D0101BD1: push CS [0E]
* D0101BD2: push ESI [56]
* D0101BD3: push 0x00 [6A 00]
* D0101BD4: push 0x03 [6A 03]
*/
/**
* Check Windows XP sysenter heuristics and install patch
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param pInstrGC GC Instruction pointer for sysenter
* @param pPatchRec Patch structure
*
*/
{
RTGCPTR32 lpfnKiFastSystemCall, lpfnKiIntSystemCall = 0; /* (initializing it to shut up warning.) */
int rc, i;
/* Guest OS specific patch; check heuristics first */
/* check the epilog of KiFastSystemCall */
if ( RT_FAILURE(rc)
{
return VERR_PATCHING_REFUSED;
}
/* Now search for KiIntSystemCall */
for (i=0;i<64;i++)
{
if(RT_FAILURE(rc))
{
break;
}
{
lpfnKiIntSystemCall = pInstrGC + i;
/* Found it! */
break;
}
}
if (i == 64)
{
Log(("KiIntSystemCall not found!!\n"));
return VERR_PATCHING_REFUSED;
}
{
Log(("KiFastSystemCall and KiIntSystemCall not in the same page!!\n"));
return VERR_PATCHING_REFUSED;
}
// make a copy of the guest code bytes that will be overwritten
/* Now we simply jump from the fast version to the 'old and slow' system call */
uTemp[0] = 0xE9;
if (RT_FAILURE(rc))
{
return VERR_PATCHING_REFUSED;
}
#ifdef LOG_ENABLED
Log(("Sysenter Patch code ----------------------------------------------------------\n"));
/* Free leftover lock if any. */
Log(("Sysenter Patch code ends -----------------------------------------------------\n"));
#endif
return VINF_SUCCESS;
}
/**
* Patch OpenBSD interrupt handler prefix
*
* @returns VBox status code.
* @param pVM The VM to operate on
* @param pCpu Disassembly state of instruction.
* @param pInstrGC GC Instruction pointer for instruction
* @param pInstrHC GC Instruction pointer for instruction
* @param pPatchRec Patch structure
*
*/
int PATMPatchOpenBSDHandlerPrefix(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
{
int rc;
/* Guest OS specific patch; check heuristics first */
rc = PGMPhysSimpleReadGCPtr(VMMGetCpu0(pVM), uTemp, pInstrGC, RT_MAX(sizeof(uFnOpenBSDHandlerPrefix1), sizeof(uFnOpenBSDHandlerPrefix2)));
if ( RT_FAILURE(rc)
{
return VERR_PATCHING_REFUSED;
}
/* Found it; patch the push cs */
}
/**
* Install guest OS specific patch
*
* @returns VBox status code.
* @param pVM The VM to operate on
* @param pCpu Disassembly state of instruction.
* @param pInstrGC GC Instruction pointer for instruction
* @param pInstrHC GC Instruction pointer for instruction
* @param pCallerGC GC address of caller; CODE32_UNKNOWN_CALLER if unknown
* @param pPatchRec Patch structure
*
*/
int PATMInstallGuestSpecificPatch(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR32 pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec)
{
int rc;
/** @todo might have to check if the patch crosses a page boundary. Currently not necessary, but that might change in the future!! */
{
case OP_SYSENTER:
if (RT_FAILURE(rc))
{
return VERR_PATCHING_REFUSED;
}
return VINF_SUCCESS;
case OP_PUSH:
/* OpenBSD guest specific patch for the following code block:
*
* pushf
* push cs <- dangerous because of DPL 0 tests
* push esi
* cli
*/
return VERR_PATCHING_REFUSED;
default:
return VERR_PATCHING_REFUSED;
}
return VERR_PATCHING_REFUSED;
}