diff --git a/sdk/xtdk/mmtypes.h b/sdk/xtdk/mmtypes.h index bae2fb4..22dac32 100644 --- a/sdk/xtdk/mmtypes.h +++ b/sdk/xtdk/mmtypes.h @@ -17,12 +17,18 @@ /* Number of hyper space pages */ #define MM_HYPERSPACE_PAGE_COUNT 255 +/* Number of free page list heads */ +#define MM_MAX_FREE_PAGE_LIST_HEADS 4 + /* Number of paging colors */ #define MM_PAGING_COLORS 64 /* PTE frame mask definition */ #define MM_PFN_PTE_FRAME (((ULONG_PTR)1 << MM_PTE_FRAME_BITS) - 1) +/* Memory manager pool type mask definition */ +#define MM_POOL_TYPE_MASK 1 + /* Number of reserved zeroed PTEs */ #define MM_RESERVED_ZERO_PTES 32 @@ -48,6 +54,24 @@ typedef enum _MMPFN_CACHE_ATTRIBUTE PfnNotMapped } MMPFN_CACHE_ATTRIBUTE, *PMMPFN_CACHE_ATTRIBUTE; +/* Memory Manager pool types */ +typedef enum _MMPOOL_TYPE +{ + NonPagedPool = 0, + PagedPool = 1, + NonPagedPoolMustSucceed = 2, + NonPagedPoolCacheAligned = 4, + PagedPoolCacheAligned = 5, + NonPagedPoolCacheAlignedMustS = 6, + MaxPoolType = 7, + NonPagedPoolSession = 32, + PagedPoolSession = 33, + NonPagedPoolMustSucceedSession = 34, + NonPagedPoolCacheAlignedSession = 36, + PagedPoolCacheAlignedSession = 37, + NonPagedPoolCacheAlignedMustSSession = 38 +} MMPOOL_TYPE, *PMMPOOL_TYPE; + /* Page table pool types */ typedef enum _MMSYSTEM_PTE_POOL_TYPE { @@ -73,6 +97,13 @@ typedef struct _MMCOLOR_TABLES ULONG_PTR Count; } MMCOLOR_TABLES, *PMMCOLOR_TABLES; +/* Free pool entry structure definition */ +typedef struct _MMFREE_POOL_ENTRY +{ + LIST_ENTRY List; + PFN_COUNT Size; +} MMFREE_POOL_ENTRY, *PMMFREE_POOL_ENTRY; + /* Memory layout structure definition */ typedef struct _MMMEMORY_LAYOUT { diff --git a/sdk/xtdk/xtstruct.h b/sdk/xtdk/xtstruct.h index 50c57c0..7f95d8e 100644 --- a/sdk/xtdk/xtstruct.h +++ b/sdk/xtdk/xtstruct.h @@ -50,6 +50,7 @@ typedef enum _KUBSAN_DATA_TYPE KUBSAN_DATA_TYPE, *PKUBSAN_DATA_TYPE; typedef enum _LOADER_MEMORY_TYPE LOADER_MEMORY_TYPE, *PLOADER_MEMORY_TYPE; typedef enum _MMPAGELISTS MMPAGELISTS, *PMMPAGELISTS; typedef enum _MMPFN_CACHE_ATTRIBUTE MMPFN_CACHE_ATTRIBUTE, *PMMPFN_CACHE_ATTRIBUTE; +typedef enum _MMPOOL_TYPE MMPOOL_TYPE, *PMMPOOL_TYPE; typedef enum _MMSYSTEM_PTE_POOL_TYPE MMSYSTEM_PTE_POOL_TYPE, *PMMSYSTEM_PTE_POOL_TYPE; typedef enum _MODE MODE, *PMODE; typedef enum _RTL_VARIABLE_TYPE RTL_VARIABLE_TYPE, *PRTL_VARIABLE_TYPE; @@ -277,6 +278,7 @@ typedef struct _LOADER_INFORMATION_BLOCK LOADER_INFORMATION_BLOCK, *PLOADER_INFO typedef struct _LOADER_MEMORY_DESCRIPTOR LOADER_MEMORY_DESCRIPTOR, *PLOADER_MEMORY_DESCRIPTOR; typedef struct _M128 M128, *PM128; typedef struct _MMCOLOR_TABLES MMCOLOR_TABLES, *PMMCOLOR_TABLES; +typedef struct _MMFREE_POOL_ENTRY MMFREE_POOL_ENTRY, *PMMFREE_POOL_ENTRY; typedef struct _MMMEMORY_LAYOUT MMMEMORY_LAYOUT, *PMMMEMORY_LAYOUT; typedef struct _MMPFNENTRY MMPFNENTRY, *PMMPFNENTRY; typedef struct _MMPFNLIST MMPFNLIST, *PMMPFNLIST; diff --git a/xtoskrnl/includes/mm/alloc.hh b/xtoskrnl/includes/mm/alloc.hh index dc88bea..a2f133a 100644 --- a/xtoskrnl/includes/mm/alloc.hh +++ b/xtoskrnl/includes/mm/alloc.hh @@ -18,7 +18,18 @@ namespace MM class Allocator { public: + STATIC XTAPI XTSTATUS AllocatePages(IN MMPOOL_TYPE PoolType, + IN SIZE_T Bytes, + OUT PVOID *Memory); STATIC XTAPI VOID InitializeNonPagedPool(VOID); + STATIC XTAPI VOID InitializePagedPool(VOID); + + private: + STATIC XTAPI XTSTATUS AllocateNonPagedPoolPages(IN PFN_COUNT Pages, + OUT PVOID *Memory); + STATIC XTAPI XTSTATUS AllocatePagedPoolPages(IN PFN_COUNT Pages, + OUT PVOID *Memory); + STATIC XTAPI VOID MapNonPagedPool(VOID); }; } diff --git a/xtoskrnl/mm/alloc.cc b/xtoskrnl/mm/alloc.cc index 306b3fb..8bcd572 100644 --- a/xtoskrnl/mm/alloc.cc +++ b/xtoskrnl/mm/alloc.cc @@ -9,3 +9,133 @@ #include +/** + * Allocates pages from the non-paged pool. + * + * @param Pages + * Specifies the number of pages to allocate. + * + * @param Memory + * Supplies a pointer to the allocated pool. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +MM::Allocator::AllocateNonPagedPoolPages(IN PFN_COUNT Pages, + OUT PVOID *Memory) +{ + UNIMPLEMENTED; + + /* Return not implemented status code */ + return STATUS_NOT_IMPLEMENTED; +} + +/** + * Allocates pages from the paged pool. + * + * @param Pages + * Specifies the number of pages to allocate. + * + * @param Memory + * Supplies a pointer to the allocated pool. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +MM::Allocator::AllocatePagedPoolPages(IN PFN_COUNT Pages, + OUT PVOID *Memory) +{ + UNIMPLEMENTED; + + /* Return not implemented status code */ + return STATUS_NOT_IMPLEMENTED; +} + +/** + * Allocates pages from the specified pool type. + * + * @param PoolType + * Specifies the type of pool to allocate pages from. + * + * @param Bytes + * Specifies the number of bytes to allocate. + * + * @param Memory + * Supplies a pointer to the allocated pool. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +MM::Allocator::AllocatePages(IN MMPOOL_TYPE PoolType, + IN SIZE_T Bytes, + OUT PVOID *Memory) +{ + PFN_COUNT Pages; + + /* Initialize the output parameter */ + *Memory = NULLPTR; + + /* Convert bytes to pages */ + Pages = SIZE_TO_PAGES(Bytes); + + /* Check if there are any pages to allocate */ + if(!Pages) + { + /* Nothing to allocate, return NULLPTR */ + return STATUS_INVALID_PARAMETER; + } + + /* Switch on pool type */ + switch(PoolType & MM_POOL_TYPE_MASK) + { + case NonPagedPool: + /* Allocate non-paged pool */ + return AllocateNonPagedPoolPages(Pages, Memory); + case PagedPool: + /* Allocate paged pool */ + return AllocatePagedPoolPages(Pages, Memory); + } + + /* Invalid pool type specified, return error */ + return STATUS_INVALID_PARAMETER; +} + +/** + * Initializes the non-paged pool for memory allocator. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MM::Allocator::InitializeNonPagedPool(VOID) +{ + UNIMPLEMENTED; + + /* Map PTEs for the non-paged pool */ + MapNonPagedPool(); +} + +/** + * Initializes the non-paged pool for memory allocator. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MM::Allocator::InitializePagedPool(VOID) +{ + UNIMPLEMENTED; +} diff --git a/xtoskrnl/mm/amd64/alloc.cc b/xtoskrnl/mm/amd64/alloc.cc index 8cdfab0..bc2be3c 100644 --- a/xtoskrnl/mm/amd64/alloc.cc +++ b/xtoskrnl/mm/amd64/alloc.cc @@ -9,14 +9,19 @@ #include +/** + * Maps the PTE for the base of the non-paged pool. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ XTAPI VOID -MM::Allocator::InitializeNonPagedPool(VOID) +MM::Allocator::MapNonPagedPool(VOID) { PMMMEMORY_LAYOUT MemoryLayout; - UNIMPLEMENTED; - /* Retrieve memory layout */ MemoryLayout = MM::Manager::GetMemoryLayout(); diff --git a/xtoskrnl/mm/i686/alloc.cc b/xtoskrnl/mm/i686/alloc.cc index 1189eef..79b0abf 100644 --- a/xtoskrnl/mm/i686/alloc.cc +++ b/xtoskrnl/mm/i686/alloc.cc @@ -9,12 +9,18 @@ #include +/** + * Maps the PTE for the base of the non-paged pool. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ XTAPI VOID -MM::Allocator::InitializeNonPagedPool(VOID) +MM::Allocator::MapNonPagedPool(VOID) { PMMMEMORY_LAYOUT MemoryLayout; - UNIMPLEMENTED; /* Retrieve memory layout */ MemoryLayout = MM::Manager::GetMemoryLayout();