HyperDbg/hyperdbg/hyperkd/header/debugger/memory/PoolManager.h

143 lines
3.2 KiB
C
Raw Permalink Normal View History

2020-04-11 06:52:24 -07:00
/**
* @file PoolManager.h
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-04-11 06:52:24 -07:00
* @brief Headers of pool manager
* @details
* @version 0.1
* @date 2020-04-11
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
* @copyright This project is released under the GNU Public License v3.
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
2020-03-24 06:35:02 -07:00
#pragma once
//////////////////////////////////////////////////
// Definition //
//////////////////////////////////////////////////
2020-08-28 04:03:12 -07:00
/**
* @brief Maximum Pool Requests (while not allocated)
2023-01-18 20:23:40 +09:00
*
2020-08-28 04:03:12 -07:00
*/
#define MaximumRequestsQueueDepth 300
2020-04-10 00:57:32 -07:00
#define NumberOfPreAllocatedBuffers 10
2020-03-24 06:35:02 -07:00
//////////////////////////////////////////////////
// Structures //
//////////////////////////////////////////////////
2020-04-11 06:52:24 -07:00
/**
* @brief Table of holding pools detail structure
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
2020-03-24 06:35:02 -07:00
typedef struct _POOL_TABLE
{
2020-04-10 00:57:32 -07:00
UINT64 Address; // Should be the start of the list as we compute it as the start address
SIZE_T Size;
POOL_ALLOCATION_INTENTION Intention;
LIST_ENTRY PoolsList;
BOOLEAN IsBusy;
BOOLEAN ShouldBeFreed;
2020-08-28 04:03:12 -07:00
BOOLEAN AlreadyFreed;
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
} POOL_TABLE, *PPOOL_TABLE;
2020-03-24 06:35:02 -07:00
2020-04-11 06:52:24 -07:00
/**
* @brief Manage the requests for new allocations
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
2020-03-24 06:35:02 -07:00
typedef struct _REQUEST_NEW_ALLOCATION
{
2020-08-28 04:03:12 -07:00
SIZE_T Size;
UINT32 Count;
POOL_ALLOCATION_INTENTION Intention;
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
} REQUEST_NEW_ALLOCATION, *PREQUEST_NEW_ALLOCATION;
2020-03-24 06:35:02 -07:00
//////////////////////////////////////////////////
// Variables //
//////////////////////////////////////////////////
2020-04-11 06:52:24 -07:00
/**
* @brief If sb wants allocation from vmx root, adds it's request to this structure
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
2020-08-28 04:03:12 -07:00
REQUEST_NEW_ALLOCATION * g_RequestNewAllocation;
2020-04-11 06:52:24 -07:00
2020-08-28 04:03:12 -07:00
/**
* @brief Request allocation Spinlock
2023-01-18 20:23:40 +09:00
*
2020-08-28 04:03:12 -07:00
*/
2020-04-11 08:40:02 -07:00
volatile LONG LockForRequestAllocation;
2020-08-28 04:03:12 -07:00
/**
* @brief Spinlock for reading pool
2023-01-18 20:23:40 +09:00
*
2020-08-28 04:03:12 -07:00
*/
2020-04-11 08:40:02 -07:00
volatile LONG LockForReadingPool;
2020-04-11 06:52:24 -07:00
2026-06-05 20:12:30 +02:00
/**
* @brief Pool manager memory allocator initialized
*
*/
BOOLEAN g_PoolManagerInitialized;
2020-04-11 06:52:24 -07:00
/**
* @brief We set it when there is a new allocation
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
BOOLEAN g_IsNewRequestForAllocationReceived;
2020-08-28 04:03:12 -07:00
/**
* @brief We set it when there is a new allocation
2023-01-18 20:23:40 +09:00
*
2020-08-28 04:03:12 -07:00
*/
BOOLEAN g_IsNewRequestForDeAllocation;
2020-04-11 06:52:24 -07:00
/**
* @brief Create a list from all pools
2023-01-18 20:23:40 +09:00
*
2020-04-11 06:52:24 -07:00
*/
2020-08-28 04:03:12 -07:00
LIST_ENTRY g_ListOfAllocatedPoolsHead;
2020-03-24 06:35:02 -07:00
//////////////////////////////////////////////////
// Functions //
//////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// Private Interfaces
//
/// @brief Allocate global requesting variable
/// @param NumberOfBytes
/// @return
static BOOLEAN
PlmgrAllocateRequestNewAllocation(SIZE_T NumberOfBytes);
static VOID PlmgrFreeRequestNewAllocation(VOID);
// ----------------------------------------------------------------------------
// Public Interfaces
//
2020-04-10 00:57:32 -07:00
BOOLEAN
PoolManagerInitialize();
2020-08-28 04:03:12 -07:00
2020-04-10 00:57:32 -07:00
VOID
2020-04-10 01:32:59 -07:00
PoolManagerUninitialize();
VOID
PoolManagerShowPreAllocatedPools();
BOOLEAN
PoolManagerCheckAndPerformAllocationAndDeallocation();
BOOLEAN
PoolManagerRequestAllocation(SIZE_T Size, UINT32 Count, POOL_ALLOCATION_INTENTION Intention);
UINT64
PoolManagerRequestPool(POOL_ALLOCATION_INTENTION Intention, BOOLEAN RequestNewPool, UINT32 Size);
BOOLEAN
PoolManagerFreePool(UINT64 AddressToFree);