Files
nt-lite/code/public/ntlite/ddk/memory.h
T

187 lines
5.6 KiB
C

// Public nt-lite DDK memory declarations.
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <ntlite/status.h>
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096ULL
#endif
#ifndef PAGE_MASK
#define PAGE_MASK (~(PAGE_SIZE - 1ULL))
#endif
#define NTL_DDK_PAGE_SIZE PAGE_SIZE
#define NTL_DDK_PAGE_PRESENT (1ULL << 0)
#define NTL_DDK_PAGE_WRITE (1ULL << 1)
#define NTL_DDK_PAGE_WRITETHROUGH (1ULL << 3)
#define NTL_DDK_PAGE_NOCACHE (1ULL << 4)
#define NTL_DDK_PAGE_NX (1ULL << 63)
#ifndef PAGE_PRESENT
#define PAGE_PRESENT (1ULL << 0)
#endif
#ifndef PAGE_WRITE
#define PAGE_WRITE (1ULL << 1)
#endif
#ifndef PAGE_USER
#define PAGE_USER (1ULL << 2)
#endif
#ifndef PAGE_WRITETHROUGH
#define PAGE_WRITETHROUGH (1ULL << 3)
#endif
#ifndef PAGE_NOCACHE
#define PAGE_NOCACHE (1ULL << 4)
#endif
#ifndef PAGE_ACCESSED
#define PAGE_ACCESSED (1ULL << 5)
#endif
#ifndef PAGE_DIRTY
#define PAGE_DIRTY (1ULL << 6)
#endif
#ifndef PAGE_NX
#define PAGE_NX (1ULL << 63)
#endif
#ifndef NTL_PHYSICAL_ADDRESS_DEFINED
typedef uint64_t PHYSICAL_ADDRESS;
#define NTL_PHYSICAL_ADDRESS_DEFINED 1
#endif
typedef enum NTL_DDK_MAP_MODE {
NtlDdkMapFailIfPresent = 0,
NtlDdkMapReplaceExisting = 1,
} NTL_DDK_MAP_MODE;
#ifndef NTL_MM_PTE_MAP_MODE_DEFINED
typedef enum MM_PTE_MAP_MODE {
MmPteMapFailIfPresent = 0,
MmPteMapReplaceExisting = 1,
} MM_PTE_MAP_MODE;
#define NTL_MM_PTE_MAP_MODE_DEFINED 1
#endif
#ifndef NTL_MM_PFN_TYPES_DEFINED
typedef enum MM_PFN_STATE {
MmPfnStateInvalid = 0,
MmPfnStateReserved,
MmPfnStateFree,
MmPfnStateZeroed,
MmPfnStateActive,
MmPfnStateStandby,
MmPfnStateModified,
} MM_PFN_STATE;
typedef enum MM_PFN_USAGE {
MmPfnUseNone = 0,
MmPfnUseKernel,
MmPfnUsePageTable,
MmPfnUseUser,
MmPfnUseCleanFileCache,
MmPfnUseImage,
MmPfnUseMappedFile,
MmPfnUseAnonymous,
MmPfnUseCow,
MmPfnUseSurface,
MmPfnUseDriver,
MmPfnUseDma,
MmPfnUseBootloader,
MmPfnUsePfnDatabase,
MmPfnUseMmio,
} MM_PFN_USAGE;
typedef struct MM_PFN {
uint16_t State;
uint16_t Usage;
uint16_t RefCount;
uint16_t PinCount;
uint32_t Flags;
uint32_t MappedCount;
void* Owner;
uint64_t BackingPage;
} MM_PFN;
#define NTL_MM_PFN_TYPES_DEFINED 1
#endif
#define MM_POOL_TAG(a, b, c, d) \
((uint32_t)(uint8_t)(a) | ((uint32_t)(uint8_t)(b) << 8) | \
((uint32_t)(uint8_t)(c) << 16) | ((uint32_t)(uint8_t)(d) << 24))
// Driver-visible memory services. These are intentionally narrower than the
// kernel memory-manager surface.
typedef struct NTL_DDK_MEMORY {
void* (*PhysToVirt)(uint64_t physicalAddress);
KSTATUS (*MapPage)(uintptr_t virtualAddress,
uint64_t physicalAddress,
uint64_t pageFlags,
NTL_DDK_MAP_MODE mode);
} NTL_DDK_MEMORY;
// Allocates from kernel pool for current kernel-mode drivers.
__declspec(dllexport) void* MmAllocatePool(size_t size);
// Allocates tagged memory from kernel pool for current kernel-mode drivers.
__declspec(dllexport) void* MmAllocatePoolTag(size_t size, uint32_t tag);
// Frees kernel pool memory.
__declspec(dllexport) void MmFreePool(void* ptr);
// Reallocates kernel pool memory.
__declspec(dllexport) void* MmReallocatePool(void* ptr, size_t newSize);
// Allocates one physical frame for a specific driver-visible usage.
__declspec(dllexport) PHYSICAL_ADDRESS MmPfnAllocate(MM_PFN_USAGE usage);
// Allocates one physical frame below maxExclusive.
__declspec(dllexport) PHYSICAL_ADDRESS MmPfnAllocateBelow(PHYSICAL_ADDRESS maxExclusive, MM_PFN_USAGE usage);
// Allocates one physical frame in a range.
__declspec(dllexport) PHYSICAL_ADDRESS MmPfnAllocateInRange(PHYSICAL_ADDRESS minInclusive,
PHYSICAL_ADDRESS maxExclusive,
MM_PFN_USAGE usage);
// Allocates contiguous physical frames.
__declspec(dllexport) PHYSICAL_ADDRESS MmPfnAllocateContiguous(size_t count, MM_PFN_USAGE usage);
// Releases a frame reference.
__declspec(dllexport) KSTATUS MmPfnFree(PHYSICAL_ADDRESS frame);
// Retains a physical frame.
__declspec(dllexport) KSTATUS MmPfnRetain(PHYSICAL_ADDRESS frame);
// Releases a retained physical frame.
__declspec(dllexport) KSTATUS MmPfnRelease(PHYSICAL_ADDRESS frame);
// Pins a physical frame.
__declspec(dllexport) KSTATUS MmPfnPin(PHYSICAL_ADDRESS frame);
// Unpins a physical frame.
__declspec(dllexport) KSTATUS MmPfnUnpin(PHYSICAL_ADDRESS frame);
// Returns the PFN database entry for a frame.
__declspec(dllexport) MM_PFN* MmPfnGet(PHYSICAL_ADDRESS frame);
// Returns the direct-map alias for a physical frame.
__declspec(dllexport) void* MmPfnToVirtual(PHYSICAL_ADDRESS frame);
// Maps one page into the shared kernel address space.
__declspec(dllexport) KSTATUS MmPteMapPage(uintptr_t virt,
PHYSICAL_ADDRESS phys,
uint64_t flags,
MM_PTE_MAP_MODE mode);
// Removes one page mapping from the shared kernel address space.
__declspec(dllexport) KSTATUS MmPteUnmapPage(uintptr_t virt);
// Replaces flags on a mapped range.
__declspec(dllexport) KSTATUS MmPteProtectRange(uintptr_t virt, size_t size, uint64_t flags);
// Translates a virtual address through the shared kernel page tables.
__declspec(dllexport) PHYSICAL_ADDRESS MmPteTranslate(uintptr_t virt);
// Returns the physical address and flags for a mapped page.
__declspec(dllexport) KSTATUS MmPteQueryPage(uintptr_t virt, PHYSICAL_ADDRESS* physOut, uint64_t* flagsOut);