_KernHelp_ wdk

来源:互联网 发布:mac全屏显示时间 编辑:程序博客网 时间:2024/06/05 01:01
/*    Copyright (c) 1998-2000 Microsoft Corporation.  All rights reserved.*/#ifndef _KernHelp_#define _KernHelp_// Use kernel mutex to implement critical section//typedef KMUTEX CRITICAL_SECTION;typedef CRITICAL_SECTION *LPCRITICAL_SECTION;VOID InitializeCriticalSection( LPCRITICAL_SECTION);VOID EnterCriticalSection(      LPCRITICAL_SECTION);VOID LeaveCriticalSection(      LPCRITICAL_SECTION);VOID DeleteCriticalSection(     LPCRITICAL_SECTION);// We have very little registry work to do, so just encapsulate the// entire process//int GetRegValueDword(__in LPTSTR RegPath,__in LPTSTR ValueName,PULONG Value);ULONG GetTheCurrentTime();PVOID KernHelpGetSysAddrForMdl(PMDL pMdl);#ifndef _NEW_DELETE_OPERATORS_#define _NEW_DELETE_OPERATORS_/***************************************************************************** * operator new() ***************************************************************************** * Overload new to allocate from PagedPool, with our pooltag. */inline void* __cdecl operator new(    size_t    iSize){    //  Replace 'ySkD' with a tag appropriate to your product.    PVOID result = ExAllocatePoolWithTag(PagedPool, iSize, 'ySkD');    if (result)    {        RtlZeroMemory(result, iSize);    }#if DBG    else    {        _DbgPrintF(DEBUGLVL_TERSE, ("Couldn't allocate paged pool: %d bytes", iSize));    }#endif // DBG    return result;}/***************************************************************************** * operator new ***************************************************************************** * Overload new to allocate with our pooltag. * Allocates from PagedPool or NonPagedPool, as specified. */inline PVOID operator new(    size_t    iSize,    POOL_TYPE poolType){    //  Replace 'ySkD' with a tag appropriate to your product.    PVOID result = ExAllocatePoolWithTag(PagedPool, iSize, 'ySkD');    if (result)    {        RtlZeroMemory(result, iSize);    }#if DBG    else    {        _DbgPrintF(DEBUGLVL_TERSE, ("Couldn't allocate poolType(%d): %d bytes", (ULONG)poolType, iSize));    }#endif // DBG    return result;}/***************************************************************************** * operator new() ***************************************************************************** * Overload new to allocate with a specified allocation tag. * Allocates from PagedPool or NonPagedPool, as specified. */inline PVOID operator new(    size_t      iSize,    POOL_TYPE   poolType,    ULONG       tag){    PVOID result = ExAllocatePoolWithTag(poolType, iSize, tag);    if (result)    {        RtlZeroMemory(result,iSize);    }#if DBG    else    {        _DbgPrintF(DEBUGLVL_TERSE, ("Couldn't allocate tagged poolType(%d): %d bytes",            (ULONG)poolType, iSize));    }#endif // DBG    return result;}/***************************************************************************** * operator delete() ***************************************************************************** * Delete function. */inline void __cdecl operator delete(    PVOID pVoid){    ExFreePool(pVoid);}#endif //!_NEW_DELETE_OPERATORS_// Debug trace facility//#define DM_DEBUG_CRITICAL       DEBUGLVL_ERROR   // Used to include critical messages#define DM_DEBUG_NON_CRITICAL   DEBUGLVL_TERSE   // Used to include level 1 plus important non-critical messages#define DM_DEBUG_STATUS         DEBUGLVL_VERBOSE // Used to include level 1 and level 2 plus status\state messages#define DM_DEBUG_FUNC_FLOW      DEBUGLVL_BLAB    // Used to include level 1, level 2 and level 3 plus function flow messages#define DM_DEBUG_ALL            DEBUGLVL_BLAB    // Used to include all debug messages#if DBG#define Trace#define Trace0(lvl, fstr) \    _DbgPrintF(lvl, (fstr))#define Trace1(lvl, fstr, arg1) \    _DbgPrintF(lvl, (fstr, arg1))#define Trace2(lvl, fstr, arg1, arg2) \    _DbgPrintF(lvl, (fstr, arg1, arg2))#define Trace3(lvl, fstr, arg1, arg2, arg3) \    _DbgPrintF(lvl, (fstr, arg1, arg2, arg3))#define Trace4(lvl, fstr, arg1, arg2, arg3, arg4) \    _DbgPrintF(lvl, (fstr, arg1, arg2, arg3, arg4))#else#define Trace#define Trace0#define Trace1#define Trace2#define Trace3#define Trace4#endif#define assert ASSERT// Paramter validation unused//#define V_INAME(x)#define V_BUFPTR_READ(p,cb)#endif // _KernHelp_

/*    Copyright (c) 1998-2000 Microsoft Corporation.  All rights reserved.*///// KernHelp.cpp//// Wrappers for kernel functions to make synth core cross compilable//#define STR_MODULENAME "DDKSynth.sys:KernHelp: "extern "C" {#include <wdm.h>};#include "ksdebug.h"#include "KernHelp.h"#pragma code_seg()/***************************************************************************** * InitializeCriticalSection() ***************************************************************************** * In kernel mode, we use a KMUTEX to implement our critical section. * Initialize the KMUTEX. */VOID InitializeCriticalSection(LPCRITICAL_SECTION CritSect){    KeInitializeMutex((PKMUTEX)CritSect, 1);}/***************************************************************************** * EnterCriticalSection() ***************************************************************************** * In kernel mode, we use a KMUTEX to implement our critical section. * Grab (wait for) the KMUTEX. */VOID EnterCriticalSection(LPCRITICAL_SECTION CritSect){    KeWaitForSingleObject((PKMUTEX)CritSect,                          Executive,                          KernelMode,                          FALSE,                          0);}/***************************************************************************** * LeaveCriticalSection() ***************************************************************************** * In kernel mode, we use a KMUTEX to implement our critical section. * Release the KMUTEX. */VOID LeaveCriticalSection(LPCRITICAL_SECTION CritSect){    KeReleaseMutex((PKMUTEX)CritSect, FALSE);}/***************************************************************************** * DeleteCriticalSection() ***************************************************************************** * In kernel mode, we use a KMUTEX to implement our critical section. * No need to delete anything. */VOID DeleteCriticalSection(LPCRITICAL_SECTION CritSect){    // NOP in kernel    //}// GetRegValueDword//// Must be called at passive level///***************************************************************************** * GetRegValueDword() ***************************************************************************** * Convenience function to encapsulate registry reads. */int GetRegValueDword(__in LPTSTR RegPath,__in LPTSTR ValueName,PULONG Value){    int                             ReturnValue = 0;    NTSTATUS                        Status;    OBJECT_ATTRIBUTES               ObjectAttributes;    HANDLE                          KeyHandle;    KEY_VALUE_PARTIAL_INFORMATION   *Information;    ULONG                           InformationSize;    UNICODE_STRING                  UnicodeRegPath;    UNICODE_STRING                  UnicodeValueName;    RtlInitUnicodeString(&UnicodeRegPath, RegPath);    RtlInitUnicodeString(&UnicodeValueName, ValueName);    InitializeObjectAttributes(&ObjectAttributes,                               &UnicodeRegPath,                               OBJ_KERNEL_HANDLE,           // Flags                               NULL,        // Root directory                               NULL);       // Security descriptor    Status = ZwOpenKey(&KeyHandle,                       KEY_QUERY_VALUE,                       &ObjectAttributes);    if (Status != STATUS_SUCCESS)    {        return 0;    }    InformationSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG);    Information = (KEY_VALUE_PARTIAL_INFORMATION*)ExAllocatePoolWithTag(PagedPool, InformationSize,'ISmD'); //  DmSI    if (Information == NULL)    {        ZwClose(KeyHandle);        return 0;    }    Status = ZwQueryValueKey(KeyHandle,                             &UnicodeValueName,                             KeyValuePartialInformation,                             Information,                             sizeof(Information),                             &InformationSize);    if (Status == STATUS_SUCCESS)    {        if (Information->Type == REG_DWORD && Information->DataLength == sizeof(ULONG))        {            RtlCopyMemory(Value, Information->Data, sizeof(ULONG));            ReturnValue = 1;        }    }    ExFreePool(Information);    ZwClose(KeyHandle);    return ReturnValue;}/***************************************************************************** * GetTheCurrentTime() ***************************************************************************** * Get the current time, in milliseconds (KeQuerySystemTime returns units of * 100ns each). */ULONG GetTheCurrentTime(){    LARGE_INTEGER Time;    KeQuerySystemTime(&Time);    return (ULONG)(Time.QuadPart / (10 * 1000));}/***************************************************************************** * KernHelpGetSysAddrForMdl() ***************************************************************************** * Safely map the MDL to system address space. This mapping * may fail "when the system runs out of system PTEs", and * without the flag set below, this condition causes a bugcheck * rather than a NULL return. */PVOID KernHelpGetSysAddrForMdl(PMDL pMdl){    PVOID MappedAddress;#if UNDER_NT    MappedAddress = MmGetSystemAddressForMdlSafe(pMdl,NormalPagePriority);#else // !UNDER_NT    CSHORT LocalCopyOfMdlFlagBit;    //    // Note the manipulation of the MDL flags is only done if needed.    // The driver is responsible for ensuring that it is not simultaneously    // modifying this field anywhere else and synchronizing if needed.    //    LocalCopyOfMdlFlagBit = (pMdl->MdlFlags & MDL_MAPPING_CAN_FAIL);    if (LocalCopyOfMdlFlagBit == 0)    {        pMdl->MdlFlags |= MDL_MAPPING_CAN_FAIL;    }    MappedAddress = MmGetSystemAddressForMdl(pMdl);    //    // Carefully restore only the single "can-fail" bit state.  This is    // because the call above will change the state of other flag bits and    // we don't want this restore to wipe out those changes.  Wiping out the    // other changes will cause not-so-obvious effects like eventually    // exhausting the system PTE pool and other resources, which will crash    // the entire system.    //    if (LocalCopyOfMdlFlagBit == 0)    {        pMdl->MdlFlags &= ~MDL_MAPPING_CAN_FAIL;    }#endif // !UNDER_NT    return MappedAddress;}


0 0
原创粉丝点击