PATMR3Dbg.cpp revision e3ddc294f1a80edb513f054d4c24ab79d7dfd3dd
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/* $Id$ */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/** @file
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * PATM - Dynamic Guest OS Patching Manager, Debugger Related Parts.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/*
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Copyright (C) 2006-2013 Oracle Corporation
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * available from http://www.virtualbox.org. This file is free software;
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * General Public License (GPL) as published by the Free Software
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/*******************************************************************************
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync* Header Files *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync*******************************************************************************/
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#define LOG_GROUP LOG_GROUP_PATM
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <VBox/vmm/patm.h>
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync#include <VBox/vmm/dbgf.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <VBox/vmm/hm.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include "PATMInternal.h"
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync#include "PATMA.h"
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <VBox/vmm/vm.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <VBox/err.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <VBox/log.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <iprt/assert.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <iprt/dbg.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#include <iprt/string.h>
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/*******************************************************************************
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync* Defined Constants And Macros *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync*******************************************************************************/
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/** Adds a structure member to a debug (pseudo) module as a symbol. */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#define ADD_MEMBER(a_hDbgMod, a_Struct, a_Member, a_pszName) \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync do { \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, RT_OFFSETOF(a_Struct, a_Member), \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RT_SIZEOFMEMB(a_Struct, a_Member), 0 /*fFlags*/, NULL /*piOrdinal*/); \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync AssertRC(rc); \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync } while (0)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/** Adds a structure member to a debug (pseudo) module as a symbol. */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync#define ADD_FUNC(a_hDbgMod, a_BaseRCPtr, a_FuncRCPtr, a_cbFunc, a_pszName) \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync do { \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync int rcAddFunc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync (RTRCUINTPTR)a_FuncRCPtr - (RTRCUINTPTR)(a_BaseRCPtr), \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync a_cbFunc, 0 /*fFlags*/, NULL /*piOrdinal*/); \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync AssertRC(rcAddFunc); \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync } while (0)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by PATMR3Init.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsyncvoid patmR3DbgInit(PVM pVM)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync{
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync pVM->patm.s.hDbgModPatchMem = NIL_RTDBGMOD;
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync}
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by PATMR3Term.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsyncvoid patmR3DbgTerm(PVM pVM)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync{
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync if (pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync {
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RTDbgModRelease(pVM->patm.s.hDbgModPatchMem);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync pVM->patm.s.hDbgModPatchMem = NIL_RTDBGMOD;
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync }
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync}
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by when the patch memory is reinitialized.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsyncvoid patmR3DbgReset(PVM pVM)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync{
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync if (pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync {
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RTDbgModRemoveAll(pVM->patm.s.hDbgModPatchMem, true);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync }
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync}
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsyncstatic size_t patmR3DbgDescribePatchAsSymbol(PPATMPATCHREC pPatchRec, char *pszName, size_t cbLeft)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync{
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync char * const pszNameStart = pszName;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync#define ADD_SZ(a_sz) \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync do { \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (cbLeft >= sizeof(a_sz)) \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync { \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync memcpy(pszName, a_sz, sizeof(a_sz)); \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pszName += sizeof(a_sz) - 1; \
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync cbLeft -= sizeof(a_sz) - 1;\
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync }\
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync } while (0)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /* Start the name off with the address of the guest code. */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t cch = RTStrPrintf(pszName, cbLeft, "Patch_%#08x", pPatchRec->patch.pPrivInstrGC);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync cbLeft -= cch;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pszName += cch;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /* Append flags. */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync uint64_t fFlags = pPatchRec->patch.flags;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_INTHANDLER)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_IntHandler");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_SYSENTER)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_SysEnter");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_GUEST_SPECIFIC)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_GuestSpecific");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_USER_MODE)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_UserMode");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_IDTHANDLER)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_IdtHnd");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_TRAPHANDLER)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_TrapHnd");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_DUPLICATE_FUNCTION)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_DupFunc");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_REPLACE_FUNCTION_CALL)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_ReplFunc");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_TRAPHANDLER_WITH_ERRORCODE)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_TrapHndErrCd");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_MMIO_ACCESS)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_MmioAccess");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_SYSENTER_XP)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_SysEnterXP");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_INT3_REPLACEMENT)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_Int3Repl");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_SUPPORT_CALLS)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_SupCalls");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_SUPPORT_INDIRECT_CALLS)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_SupIndirCalls");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_IDTHANDLER_WITHOUT_ENTRYPOINT)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_IdtHandlerWE");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_INHIBIT_IRQS)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_InhibitIrqs");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_RECOMPILE_NEXT)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_RecompileNext");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_CALLABLE_AS_FUNCTION)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_Callable");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_TRAMPOLINE)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_Trampoline");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_PATCHED_GUEST_CODE)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_PatchedGuestCode");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_MUST_INSTALL_PATCHJMP)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_MustInstallPatchJmp");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_INT3_REPLACEMENT_BLOCK)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_Int3ReplBlock");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_EXTERNAL_JUMP_INSIDE)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_ExtJmp");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (fFlags & PATMFL_CODE_REFERENCED)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync ADD_SZ("_CodeRefed");
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync return pszName - pszNameStart;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync}
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called when a new patch is added or when first populating the address space.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pPatchRec The patch record.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncvoid patmR3DbgAddPatch(PVM pVM, PPATMPATCHREC pPatchRec)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync{
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync if ( pVM->patm.s.hDbgModPatchMem != NIL_RTDBGMOD
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync && pPatchRec->patch.pPatchBlockOffset > 0
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync && !(pPatchRec->patch.flags & PATMFL_GLOBAL_FUNCTIONS))
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync {
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /** @todo find a cheap way of checking whether we've already added the patch.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Using a flag would be nice, except I don't want to consider saved
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * state considerations right now (I don't recall if we're still
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * depending on structure layout there or not). */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync char szName[256];
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, szName, sizeof(szName));
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /* If we have a symbol near the guest address, append that. */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (off + 8 <= sizeof(szName))
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync {
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync DBGFSYMBOL Symbol;
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync RTGCINTPTR offDisp;
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync int rc = DBGFR3SymbolByAddr(pVM, pPatchRec->patch.pPrivInstrGC, &offDisp, &Symbol);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync if (RT_SUCCESS(rc))
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync {
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync szName[off++] = '_';
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync szName[off++] = '_';
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync RTStrCopy(&szName[off], sizeof(szName) - off, Symbol.szName);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync }
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync }
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /* Add it (may fail due to enable/disable patches). */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync RTDbgModSymbolAdd(pVM->patm.s.hDbgModPatchMem, szName, 0 /*iSeg*/,
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync pPatchRec->patch.pPatchBlockOffset,
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync pPatchRec->patch.cbPatchBlockSize,
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync 0 /*fFlags*/, NULL /*piOrdinal*/);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync }
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync}
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Enumeration callback used by patmR3DbgAddPatches
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @returns 0 (continue enum)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pNode The patch record node.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pvUser The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncstatic DECLCALLBACK(int) patmR3DbgAddPatchCallback(PAVLOU32NODECORE pNode, void *pvUser)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync{
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync patmR3DbgAddPatch((PVM)pvUser, (PPATMPATCHREC)pNode);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync return 0;
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync}
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/**
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Populates an empty "patches" (hDbgModPatchMem) module with patch symbols.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync *
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param hDbgMod The debug module handle.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncstatic void patmR3DbgAddPatches(PVM pVM, RTDBGMOD hDbgMod)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync{
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /*
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Global functions.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, PATMLookupAndCallRecord.size, "PATMLookupAndCall");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, PATMRetFunctionRecord.size, "PATMRetFunction");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, PATMLookupAndJumpRecord.size, "PATMLookupAndJump");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, PATMIretFunctionRecord.size, "PATMIretFunction");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /*
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * The patches.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync}
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/**
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Populate DBGF_AS_RC with PATM symbols.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * accessed for the first time.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * @param pVM The cross context VM structure.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * @param hDbgAs The DBGF_AS_RC address space handle.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsyncVMMR3_INT_DECL(void) PATMR3DbgPopulateAddrSpace(PVM pVM, RTDBGAS hDbgAs)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync{
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync AssertReturnVoid(!HMIsEnabled(pVM));
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync /*
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Add a fake debug module for the PATMGCSTATE structure.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RTDBGMOD hDbgMod;
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync int rc = RTDbgModCreate(&hDbgMod, "patmgcstate", sizeof(PATMGCSTATE), 0 /*fFlags*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync if (RT_SUCCESS(rc))
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync {
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uVMFlags, "uVMFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uPendingAction, "uPendingAction");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uPatchCalls, "uPatchCalls");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uScratch, "uScratch");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEFlags, "uIretEFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretCS, "uIretCS");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEIP, "uIretEIP");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Psp, "Psp");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, fPIF, "fPIF");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCPtrInhibitInterrupts, "GCPtrInhibitInterrupts");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallPatchTargetAddr, "GCCallPatchTargetAddr");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallReturnAddr, "GCCallReturnAddr");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEAX, "Restore.uEAX");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uECX, "Restore.uECX");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEDI, "Restore.uEDI");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.eFlags, "Restore.eFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uFlags, "Restore.uFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pGCStateGC, 0 /*fFlags/*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync AssertLogRelRC(rc);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RTDbgModRelease(hDbgMod);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync }
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync /*
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Add a fake debug module for the patches.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem, 0 /*fFlags*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync if (RT_SUCCESS(rc))
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync {
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync pVM->patm.s.hDbgModPatchMem = hDbgMod;
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync patmR3DbgAddPatches(pVM, hDbgMod);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pPatchMemGC, 0 /*fFlags/*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync AssertLogRelRC(rc);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync }
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync}
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync/**
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Annotates an instruction if patched.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync *
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param pVM The VM handle.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param RCPtr The instruction address.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param cbInstr The instruction length.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param pszBuf The output buffer. This will be an empty string
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * if the instruction wasn't patched. If it's
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * patched, it will hold a symbol-like string
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * describing the patch.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param cbBuf The size of the output buffer.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsyncVMMR3_INT_DECL(void) PATMR3DbgAnnotatePatchedInstruction(PVM pVM, RTRCPTR RCPtr, uint8_t cbInstr, char *pszBuf, size_t cbBuf)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync{
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /*
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Always zero the buffer.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync AssertReturnVoid(cbBuf > 0);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync *pszBuf = '\0';
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /*
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Drop out immediately if it cannot be a patched instruction.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (!PATMIsEnabled(pVM))
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync return;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if ( RCPtr < pVM->patm.s.pPatchedInstrGCLowest
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync || RCPtr > pVM->patm.s.pPatchedInstrGCHighest)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync return;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /*
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Look for a patch record covering any part of the instruction.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync *
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * The first query results in a patched less or equal to RCPtr. While the
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * second results in one that's greater than RCPtr.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync PPATMPATCHREC pPatchRec;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, false /*fFromAbove*/);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if ( !pPatchRec
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync || RCPtr - pPatchRec->patch.pPrivInstrGC > pPatchRec->patch.cbPrivInstr)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync {
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, true /*fFromAbove*/);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if ( !pPatchRec
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync || (RTRCPTR)(RCPtr + cbInstr) < pPatchRec->patch.pPrivInstrGC )
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync return;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync }
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /*
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Lazy bird uses the symbol name generation code for describing the patch.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, pszBuf, cbBuf);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (off + 1 < cbBuf)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync {
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync const char *pszState;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync switch (pPatchRec->patch.uState)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync {
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_REFUSED: pszState = "Refused"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_DISABLED: pszState = "Disabled"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_ENABLED: pszState = "Enabled"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_UNUSABLE: pszState = "Unusable"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_DIRTY: pszState = "Dirty"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_DISABLE_PENDING: pszState = "DisablePending"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync default: pszState = "State???"; AssertFailed(); break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync }
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync if (pPatchRec->patch.cbPatchBlockSize > 0)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b) - %#x LB %#x",
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pszState, pPatchRec->patch.cbPatchJump,
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec->patch.pPatchBlockOffset + pVM->patm.s.pPatchMemGC,
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec->patch.cbPatchBlockSize);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync else
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b)", pszState, pPatchRec->patch.cbPatchJump);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync }
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync}
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync