- Mm-rewrite:

* Get rid of a hack-based heap allocation in FreeLdr, which makes real data interleaved with temporary data, all that unfreeable and unmanageable.
 * Introduce new APIs MmHeapAlloc / MmHeapFree to alloc/free memory from a pre-allocated heap.
 * Add BGET (public domain heap implementation), hook it up to the MmHeapAlloc / MmHeapFree APIs.
 * MmAllocateMemory still is a backward-compatibility API, will be removed soon.
- Change most of allocations to the heap-based routines, add frees where necessary (I must admit Brian already used MmFreeMemory in a lot of places, so I just had to change those to the corresponding MmHeapFree API).
- Remove unneeded and wrong IDT filling, which was commented out.
- Temporary disable caching support, because it needs a standalone place to keep its data in. For compatibility's sake it's better to leave it when WinLdr is more mature.

svn path=/branches/winldr/; revision=29518
This commit is contained in:
Aleksey Bragin
2007-10-11 20:40:38 +00:00
parent 7160af7593
commit 532ee4eb59
18 changed files with 1835 additions and 353 deletions
+3 -3
View File
@@ -581,7 +581,7 @@ DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA ComponentRoot,
(int)DiskCount, (DiskCount == 1) ? "": "s"));
/* Create DiskController */
DiskComponentData = (PCONFIGURATION_COMPONENT_DATA)MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA));
DiskComponentData = (PCONFIGURATION_COMPONENT_DATA)MmHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA));
RtlZeroMemory(DiskComponentData, sizeof(CONFIGURATION_COMPONENT_DATA));
DiskComponent = &DiskComponentData->ComponentEntry;
@@ -634,7 +634,7 @@ DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA ComponentRoot,
}
/* Get harddisk Int13 geometry data */
Int13Drives = MmAllocateMemory(sizeof(CM_INT13_DRIVE_PARAMETER) * DiskCount);
Int13Drives = MmHeapAlloc(sizeof(CM_INT13_DRIVE_PARAMETER) * DiskCount);
memset(Int13Drives, 0, sizeof(CM_INT13_DRIVE_PARAMETER) * DiskCount);
for (i = 0; i < DiskCount; i++)
@@ -744,7 +744,7 @@ DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA ComponentRoot,
DeviceData = (PVOID)((ULONG_PTR)ResourceList + sizeof(CM_PARTIAL_RESOURCE_LIST));
memcpy(DeviceData, (PVOID)Int13Drives, DeviceDataSize);
MmFreeMemory(Int13Drives);
MmHeapFree(Int13Drives);
/* Now fill the 2nd partial resource descriptor */
ResourceDescriptor = (PCM_PARTIAL_RESOURCE_DESCRIPTOR)((ULONG_PTR)ResourceList +
+4 -4
View File
@@ -98,7 +98,7 @@ PCACHE_BLOCK CacheInternalAddBlockToCache(PCACHE_DRIVE CacheDrive, ULONG BlockNu
// We will need to add the block to the
// drive's list of cached blocks. So allocate
// the block memory.
CacheBlock = MmAllocateMemory(sizeof(CACHE_BLOCK));
CacheBlock = MmHeapAlloc(sizeof(CACHE_BLOCK));
if (CacheBlock == NULL)
{
return NULL;
@@ -111,7 +111,7 @@ PCACHE_BLOCK CacheInternalAddBlockToCache(PCACHE_DRIVE CacheDrive, ULONG BlockNu
CacheBlock->BlockData = MmAllocateMemory(CacheDrive->BlockSize * CacheDrive->BytesPerSector);
if (CacheBlock->BlockData ==NULL)
{
MmFreeMemory(CacheBlock);
MmHeapFree(CacheBlock);
return NULL;
}
@@ -119,7 +119,7 @@ PCACHE_BLOCK CacheInternalAddBlockToCache(PCACHE_DRIVE CacheDrive, ULONG BlockNu
if (!MachDiskReadLogicalSectors(CacheDrive->DriveNumber, (BlockNumber * CacheDrive->BlockSize), CacheDrive->BlockSize, (PVOID)DISKREADBUFFER))
{
MmFreeMemory(CacheBlock->BlockData);
MmFreeMemory(CacheBlock);
MmHeapFree(CacheBlock);
return NULL;
}
RtlCopyMemory(CacheBlock->BlockData, (PVOID)DISKREADBUFFER, CacheDrive->BlockSize * CacheDrive->BytesPerSector);
@@ -177,7 +177,7 @@ BOOLEAN CacheInternalFreeBlock(PCACHE_DRIVE CacheDrive)
// Free the block memory and the block structure
MmFreeMemory(CacheBlockToFree->BlockData);
MmFreeMemory(CacheBlockToFree);
MmHeapFree(CacheBlockToFree);
// Update the cache data
CacheBlockCount--;
+1
View File
@@ -49,6 +49,7 @@
<file>reactos.c</file>
</directory>
<directory name="rtl">
<file>bget.c</file>
<file>libsupp.c</file>
<file>list.c</file>
</directory>
+44 -15
View File
@@ -51,7 +51,7 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
//
// Allocate the memory to hold the boot sector
//
FatVolumeBootSector = (PFAT_BOOTSECTOR) MmAllocateMemory(512);
FatVolumeBootSector = (PFAT_BOOTSECTOR) MmHeapAlloc(512);
Fat32VolumeBootSector = (PFAT32_BOOTSECTOR) FatVolumeBootSector;
FatXVolumeBootSector = (PFATX_BOOTSECTOR) FatVolumeBootSector;
@@ -68,7 +68,7 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
// If this fails then abort
if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, (PVOID)DISKREADBUFFER))
{
MmFreeMemory(FatVolumeBootSector);
MmHeapFree(FatVolumeBootSector);
return FALSE;
}
RtlCopyMemory(FatVolumeBootSector, (PVOID)DISKREADBUFFER, 512);
@@ -164,7 +164,7 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
sprintf(ErrMsg, "Invalid boot sector magic on drive 0x%x (expected 0xaa55 found 0x%x)",
DriveNumber, FatVolumeBootSector->BootSectorMagic);
FileSystemError(ErrMsg);
MmFreeMemory(FatVolumeBootSector);
MmHeapFree(FatVolumeBootSector);
return FALSE;
}
@@ -176,7 +176,7 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
(! ISFATX(FatType) && 64 * 1024 < FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector))
{
FileSystemError("This file system has cluster sizes bigger than 64k.\nFreeLoader does not support this.");
MmFreeMemory(FatVolumeBootSector);
MmHeapFree(FatVolumeBootSector);
return FALSE;
}
@@ -249,8 +249,9 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
return FALSE;
}
}
MmFreeMemory(FatVolumeBootSector);
MmHeapFree(FatVolumeBootSector);
#ifdef CACHE_ENABLED
//
// Initialize the disk cache for this drive
//
@@ -271,6 +272,20 @@ BOOLEAN FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector, ULONG Partitio
return FALSE;
}
}
#else
{
GEOMETRY DriveGeometry;
ULONG BlockSize;
// Initialize drive by getting its geometry
if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry))
{
return FALSE;
}
BlockSize = MachDiskGetCacheableBlockCount(DriveNumber);
}
#endif
return TRUE;
}
@@ -362,7 +377,7 @@ PVOID FatBufferDirectory(ULONG DirectoryStartCluster, ULONG *DirectorySize, BOOL
// Attempt to allocate memory for directory buffer
//
DbgPrint((DPRINT_FILESYSTEM, "Trying to allocate (DirectorySize) %d bytes.\n", *DirectorySize));
DirectoryBuffer = MmAllocateMemory(*DirectorySize);
DirectoryBuffer = MmHeapAlloc(*DirectorySize);
if (DirectoryBuffer == NULL)
{
@@ -376,7 +391,7 @@ PVOID FatBufferDirectory(ULONG DirectoryStartCluster, ULONG *DirectorySize, BOOL
{
if (!FatReadVolumeSectors(FatDriveNumber, RootDirSectorStart, RootDirSectors, DirectoryBuffer))
{
MmFreeMemory(DirectoryBuffer);
MmHeapFree(DirectoryBuffer);
return NULL;
}
}
@@ -384,7 +399,7 @@ PVOID FatBufferDirectory(ULONG DirectoryStartCluster, ULONG *DirectorySize, BOOL
{
if (!FatReadClusterChain(DirectoryStartCluster, 0xFFFFFFFF, DirectoryBuffer))
{
MmFreeMemory(DirectoryBuffer);
MmHeapFree(DirectoryBuffer);
return NULL;
}
}
@@ -727,7 +742,7 @@ BOOLEAN FatLookupFile(PCSTR FileName, PFAT_FILE_INFO FatFileInfoPointer)
{
if (!FatXSearchDirectoryBufferForFile(DirectoryBuffer, DirectorySize, PathPart, &FatFileInfo))
{
MmFreeMemory(DirectoryBuffer);
MmHeapFree(DirectoryBuffer);
return FALSE;
}
}
@@ -735,12 +750,12 @@ BOOLEAN FatLookupFile(PCSTR FileName, PFAT_FILE_INFO FatFileInfoPointer)
{
if (!FatSearchDirectoryBufferForFile(DirectoryBuffer, DirectorySize, PathPart, &FatFileInfo))
{
MmFreeMemory(DirectoryBuffer);
MmHeapFree(DirectoryBuffer);
return FALSE;
}
}
MmFreeMemory(DirectoryBuffer);
MmHeapFree(DirectoryBuffer);
//
// If we have another sub-directory to go then
@@ -749,7 +764,7 @@ BOOLEAN FatLookupFile(PCSTR FileName, PFAT_FILE_INFO FatFileInfoPointer)
if ((i+1) < NumberOfPathParts)
{
DirectoryStartCluster = FatFileInfo.FileFatChain[0];
MmFreeMemory(FatFileInfo.FileFatChain);
MmHeapFree(FatFileInfo.FileFatChain);
}
}
@@ -914,7 +929,7 @@ FILE* FatOpenFile(PCSTR FileName)
return NULL;
}
FileHandle = MmAllocateMemory(sizeof(FAT_FILE_INFO));
FileHandle = MmHeapAlloc(sizeof(FAT_FILE_INFO));
if (FileHandle == NULL)
{
@@ -978,7 +993,7 @@ ULONG* FatGetClusterChainArray(ULONG StartCluster)
//
// Allocate array memory
//
ArrayPointer = MmAllocateMemory(ArraySize);
ArrayPointer = MmHeapAlloc(ArraySize);
if (ArrayPointer == NULL)
{
@@ -1011,7 +1026,7 @@ ULONG* FatGetClusterChainArray(ULONG StartCluster)
//
if (!FatGetFatEntry(StartCluster, &StartCluster))
{
MmFreeMemory(ArrayPointer);
MmHeapFree(ArrayPointer);
return NULL;
}
}
@@ -1312,5 +1327,19 @@ ULONG FatGetFilePointer(FILE *FileHandle)
BOOLEAN FatReadVolumeSectors(ULONG DriveNumber, ULONG SectorNumber, ULONG SectorCount, PVOID Buffer)
{
#ifdef CACHE_ENABLED
return CacheReadDiskSectors(DriveNumber, SectorNumber + FatVolumeStartSector, SectorCount, Buffer);
#else
// Now try to read in the block
if (!MachDiskReadLogicalSectors(DriveNumber, SectorNumber + FatVolumeStartSector, SectorCount, (PVOID)DISKREADBUFFER))
{
return FALSE;
}
// Copy data to the caller
RtlCopyMemory(Buffer, (PVOID)DISKREADBUFFER, SectorCount * BytesPerSector);
// Return success
return TRUE;
#endif
}
+30
View File
@@ -0,0 +1,30 @@
/*
Interface definitions for bget.c, the memory management package.
*/
#ifndef _
#ifdef PROTOTYPES
#define _(x) x /* If compiler knows prototypes */
#else
#define _(x) () /* It it doesn't */
#endif /* PROTOTYPES */
#endif
typedef long bufsize;
void bpool _((void *buffer, bufsize len));
void *bget _((bufsize size));
void *bgetz _((bufsize size));
void *bgetr _((void *buffer, bufsize newsize));
void brel _((void *buf));
void bectl _((int (*compact)(bufsize sizereq, int sequence),
void *(*acquire)(bufsize size),
void (*release)(void *buf), bufsize pool_incr));
void bstats _((bufsize *curalloc, bufsize *totfree, bufsize *maxfree,
long *nget, long *nrel));
void bstatse _((bufsize *pool_incr, long *npool, long *npget,
long *nprel, long *ndget, long *ndrel));
void bufdump _((void *buf));
void bpoold _((void *pool, int dumpalloc, int dumpfree));
int bpoolv _((void *pool));
+1
View File
@@ -74,6 +74,7 @@
#include <keycodes.h>
#include <ver.h>
#include <cmdline.h>
#include <bget.h>
/* Needed by boot manager */
#include <bootmgr.h>
#include <oslist.h>
+8
View File
@@ -52,6 +52,10 @@ typedef struct
//
#define LOADER_HIGH_ZONE ((16*1024*1024) >> MM_PAGE_SHIFT) //16Mb page
// HEAP and STACK size
#define HEAP_PAGES 0x100//0x18
#define STACK_PAGES 0x00
typedef struct
{
TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
@@ -97,6 +101,7 @@ PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(ULONG *NoEntries); // Returns a pointer
//BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
BOOLEAN MmInitializeMemoryManager(VOID);
VOID MmInitializeHeap(PVOID PageLookupTable);
PVOID MmAllocateMemory(ULONG MemorySize);
PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType);
VOID MmFreeMemory(PVOID MemoryPointer);
@@ -106,4 +111,7 @@ VOID MmChangeAllocationPolicy(BOOLEAN PolicyAllocatePagesFromEnd);
PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
PVOID MmHeapAlloc(ULONG MemorySize);
VOID MmHeapFree(PVOID MemoryPointer);
#endif // defined __MEMORY_H
+37
View File
@@ -44,6 +44,9 @@ ULONG TotalPagesInLookupTable = 0;
ULONG FreePagesInLookupTable = 0;
ULONG LastFreePageHint = 0;
extern ULONG_PTR MmHeapPointer;
extern ULONG_PTR MmHeapStart;
BOOLEAN MmInitializeMemoryManager(VOID)
{
BIOS_MEMORY_MAP BiosMemoryMap[32];
@@ -110,10 +113,42 @@ BOOLEAN MmInitializeMemoryManager(VOID)
FreePagesInLookupTable = MmCountFreePagesInLookupTable(PageLookupTableAddress, TotalPagesInLookupTable);
MmInitializeHeap(PageLookupTableAddress);
DbgPrint((DPRINT_MEMORY, "Memory Manager initialized. %d pages available.\n", FreePagesInLookupTable));
return TRUE;
}
VOID MmInitializeHeap(PVOID PageLookupTable)
{
ULONG PagesNeeded;
ULONG HeapStart;
// HACK: Make it so it doesn't overlap kernel space
MmMarkPagesInLookupTable(PageLookupTableAddress, 0x100, 0xFF, LoaderSystemCode);
// Find contigious memory block for HEAP:STACK
PagesNeeded = HEAP_PAGES + STACK_PAGES;
HeapStart = MmFindAvailablePages(PageLookupTable, TotalPagesInLookupTable, PagesNeeded, FALSE);
// Unapply the hack
MmMarkPagesInLookupTable(PageLookupTableAddress, 0x100, 0xFF, LoaderFree);
if (HeapStart == 0)
{
UiMessageBox("Critical error: Can't allocate heap!");
return;
}
// Initialize BGET
bpool(HeapStart << MM_PAGE_SHIFT, PagesNeeded << MM_PAGE_SHIFT);
// Mark those pages as used
MmMarkPagesInLookupTable(PageLookupTableAddress, HeapStart, PagesNeeded, LoaderOsloaderHeap);
DbgPrint((DPRINT_MEMORY, "Heap initialized, base 0x%08x, pages %d\n", (HeapStart << MM_PAGE_SHIFT), PagesNeeded));
}
#ifdef DBG
PUCHAR MmGetSystemMemoryMapTypeString(ULONG Type)
{
@@ -295,10 +330,12 @@ VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG Page
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
#if 0
if ((Index <= (StartPage + 16)) || (Index >= (StartPage+PageCount-16)))
{
DbgPrint((DPRINT_MEMORY, "Index = %d StartPage = %d PageCount = %d\n", Index, StartPage, PageCount));
}
#endif
RealPageLookupTable[Index].PageAllocated = PageAllocated;
RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
}
+44 -183
View File
@@ -21,36 +21,11 @@
#include <freeldr.h>
#include <debug.h>
ULONG AllocationCount = 0;
#ifdef DBG
VOID VerifyHeap(VOID);
VOID DumpMemoryAllocMap(VOID);
VOID IncrementAllocationCount(VOID);
VOID DecrementAllocationCount(VOID);
VOID MemAllocTest(VOID);
#endif // DBG
/*
* Hack alert
* Normally, we allocate whole pages. This is ofcourse wastefull for small
* allocations (a few bytes). So, for small allocations (smaller than a page)
* we sub-allocate. When the first small allocation is done, a page is
* requested. We keep a pointer to that page in SubAllocationPage. The alloc
* is satisfied by returning a pointer to the beginning of the page. We also
* keep track of how many bytes are still available in the page in SubAllocationRest.
* When the next small request comes in, we try to allocate it just after the
* memory previously allocated. If it won't fit, we allocate a new page and
* the whole process starts again.
* Note that suballocations are done back-to-back, there's no bookkeeping at all.
* That also means that we cannot really free suballocations. So, when a free is
* done and it is determined that this might be a free of a sub-allocation, we
* just no-op the free.
* Perhaps we should use the heap routines from ntdll here.
*/
static PVOID SubAllocationPage = NULL;
static unsigned SubAllocationRest = 0;
BOOLEAN AllocateFromEnd = TRUE;
VOID MmChangeAllocationPolicy(BOOLEAN PolicyAllocatePagesFromEnd)
@@ -72,12 +47,6 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
}
MemorySize = ROUND_UP(MemorySize, 4);
if (MemorySize <= SubAllocationRest)
{
MemPointer = (PVOID)((ULONG_PTR)SubAllocationPage + MM_PAGE_SIZE - SubAllocationRest);
SubAllocationRest -= MemorySize;
return MemPointer;
}
// Find out how many blocks it will take to
// satisfy this allocation
@@ -87,7 +56,7 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
// then return NULL
if (FreePagesInLookupTable < PagesNeeded)
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
UiMessageBoxCritical("Memory allocation failed: out of memory.");
return NULL;
}
@@ -96,7 +65,7 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
if (FirstFreePageFromEnd == (ULONG)-1)
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
UiMessageBoxCritical("Memory allocation failed: out of memory.");
return NULL;
}
@@ -106,16 +75,8 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
FreePagesInLookupTable -= PagesNeeded;
MemPointer = (PVOID)(FirstFreePageFromEnd * MM_PAGE_SIZE);
if (MemorySize < MM_PAGE_SIZE)
{
SubAllocationPage = MemPointer;
SubAllocationRest = MM_PAGE_SIZE - MemorySize;
}
#ifdef DBG
IncrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize, PagesNeeded, FirstFreePageFromEnd, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
//VerifyHeap();
#endif // DBG
@@ -124,10 +85,42 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
return MemPointer;
}
PVOID MmHeapAlloc(ULONG MemorySize)
{
PVOID Result;
LONG CurAlloc, TotalFree, MaxFree, NumberOfGets, NumberOfRels;
if (MemorySize > MM_PAGE_SIZE)
{
DbgPrint((DPRINT_MEMORY, "Consider using other functions to allocate %d bytes of memory!\n", MemorySize));
}
// Get the buffer from BGET pool
Result = bget(MemorySize);
if (Result == NULL)
{
DbgPrint((DPRINT_MEMORY, "Heap allocation for %d bytes failed\n", MemorySize));
}
// Gather some stats
bstats(&CurAlloc, &TotalFree, &MaxFree, &NumberOfGets, &NumberOfRels);
DbgPrint((DPRINT_MEMORY, "Current alloced %d bytes, free %d bytes, allocs %d, frees %d\n",
CurAlloc, TotalFree, NumberOfGets, NumberOfRels));
return Result;
}
VOID MmHeapFree(PVOID MemoryPointer)
{
// Release the buffer to the pool
brel(MemoryPointer);
}
PVOID MmAllocateMemory(ULONG MemorySize)
{
// Allocate it as "heap"
// Temporary forwarder...
return MmAllocateMemoryWithType(MemorySize, LoaderOsloaderHeap);
}
@@ -157,7 +150,7 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
"Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
"AllocationCount: %d\n", MemorySize, PagesNeeded, FreePagesInLookupTable, AllocationCount));
"\n", MemorySize, PagesNeeded, FreePagesInLookupTable));
UiMessageBoxCritical("Memory allocation failed: out of memory.");
return NULL;
}
@@ -165,8 +158,8 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
if (MmAreMemoryPagesAvailable(PageLookupTableAddress, TotalPagesInLookupTable, DesiredAddress, PagesNeeded) == FALSE)
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
"Not enough free memory to allocate %d bytes at address %p. AllocationCount: %d\n",
MemorySize, DesiredAddress, AllocationCount));
"Not enough free memory to allocate %d bytes at address %p.\n",
MemorySize, DesiredAddress));
// Don't tell this to user since caller should try to alloc this memory
// at a different address
@@ -180,8 +173,7 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
MemPointer = (PVOID)(StartPageNumber * MM_PAGE_SIZE);
#ifdef DBG
IncrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize, PagesNeeded, StartPageNumber, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber));
DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
//VerifyHeap();
#endif // DBG
@@ -215,7 +207,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
// then return NULL
if (FreePagesInLookupTable < PagesNeeded)
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
UiMessageBoxCritical("Memory allocation failed: out of memory.");
return NULL;
}
@@ -224,7 +216,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
if (FirstFreePageFromEnd == 0)
{
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
UiMessageBoxCritical("Memory allocation failed: out of memory.");
return NULL;
}
@@ -235,8 +227,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
MemPointer = (PVOID)(FirstFreePageFromEnd * MM_PAGE_SIZE);
#ifdef DBG
IncrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize, PagesNeeded, FirstFreePageFromEnd, AllocationCount));
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
//VerifyHeap();
#endif // DBG
@@ -247,129 +238,9 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
VOID MmFreeMemory(PVOID MemoryPointer)
{
ULONG PageNumber;
ULONG PageCount;
ULONG Idx;
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTableAddress;
#ifdef DBG
// Make sure we didn't get a bogus pointer
if (MemoryPointer >= (PVOID)(TotalPagesInLookupTable * MM_PAGE_SIZE))
{
BugCheck((DPRINT_MEMORY, "Bogus memory pointer (0x%x) passed to MmFreeMemory()\n", MemoryPointer));
}
#endif // DBG
// Find out the page number of the first
// page of memory they allocated
PageNumber = MmGetPageNumberFromAddress(MemoryPointer);
PageCount = RealPageLookupTable[PageNumber].PageAllocationLength;
#ifdef DBG
// Make sure we didn't get a bogus pointer
if ((PageCount < 1) || (PageCount > (TotalPagesInLookupTable - PageNumber)))
{
BugCheck((DPRINT_MEMORY, "Invalid page count in lookup table. PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber, RealPageLookupTable[PageNumber].PageAllocationLength));
}
// Loop through our array check all the pages
// to make sure they are allocated with a length of 0
for (Idx=PageNumber+1; Idx<(PageNumber + PageCount); Idx++)
{
if ((RealPageLookupTable[Idx].PageAllocated == LoaderFree) ||
(RealPageLookupTable[Idx].PageAllocationLength != 0))
{
BugCheck((DPRINT_MEMORY, "Invalid page entry in lookup table, PageAllocated should = 1 and PageAllocationLength should = 0 because this is not the first block in the run. PageLookupTable[%d].PageAllocated = %d PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber, RealPageLookupTable[PageNumber].PageAllocated, PageNumber, RealPageLookupTable[PageNumber].PageAllocationLength));
}
}
#endif
/* If this allocation is only a single page, it could be a sub-allocated page.
* Just don't free it */
if (1 == PageCount)
{
return;
}
// Loop through our array and mark all the
// blocks as free
for (Idx=PageNumber; Idx<(PageNumber + PageCount); Idx++)
{
RealPageLookupTable[Idx].PageAllocated = LoaderFree;
RealPageLookupTable[Idx].PageAllocationLength = 0;
}
FreePagesInLookupTable += PageCount;
#ifdef DBG
DecrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Freed %d pages of memory starting at page %d. AllocationCount: %d\n", PageCount, PageNumber, AllocationCount));
//VerifyHeap();
#endif // DBG
}
#ifdef DBG
VOID VerifyHeap(VOID)
{
ULONG Idx;
ULONG Idx2;
ULONG Count;
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTableAddress;
if (DUMP_MEM_MAP_ON_VERIFY)
{
DumpMemoryAllocMap();
}
// Loop through the array and verify that
// everything is kosher
for (Idx=0; Idx<TotalPagesInLookupTable; Idx++)
{
// Check if this block is allocated
if (RealPageLookupTable[Idx].PageAllocated != LoaderFree)
{
// This is the first block in the run so it
// had better have a length that is within range
if ((RealPageLookupTable[Idx].PageAllocationLength < 1) || (RealPageLookupTable[Idx].PageAllocationLength > (TotalPagesInLookupTable - Idx)))
{
BugCheck((DPRINT_MEMORY, "Allocation length out of range in heap table. PageLookupTable[Idx].PageAllocationLength = %d\n", RealPageLookupTable[Idx].PageAllocationLength));
}
// Now go through and verify that the rest of
// this run has the blocks marked allocated
// with a length of zero but don't check the
// first one because we already did
Count = RealPageLookupTable[Idx].PageAllocationLength;
for (Idx2=1; Idx2<Count; Idx2++)
{
// Make sure it's allocated
if (RealPageLookupTable[Idx + Idx2].PageAllocated == LoaderFree)
{
BugCheck((DPRINT_MEMORY, "Lookup table indicates hole in memory allocation. RealPageLookupTable[Idx + Idx2].PageAllocated == 0\n"));
}
// Make sure the length is zero
if (RealPageLookupTable[Idx + Idx2].PageAllocationLength != 0)
{
BugCheck((DPRINT_MEMORY, "Allocation chain has non-zero value in non-first block in lookup table. RealPageLookupTable[Idx + Idx2].PageAllocationLength != 0\n"));
}
}
// Move on to the next run
Idx += (Count - 1);
}
else
{
// Nope, not allocated so make sure the length is zero
if (RealPageLookupTable[Idx].PageAllocationLength != 0)
{
BugCheck((DPRINT_MEMORY, "Free block is start of memory allocation. RealPageLookupTable[Idx].PageAllocationLength != 0\n"));
}
}
}
}
VOID DumpMemoryAllocMap(VOID)
{
@@ -446,16 +317,6 @@ VOID DumpMemoryAllocMap(VOID)
DbgPrint((DPRINT_MEMORY, "\n"));
}
VOID IncrementAllocationCount(VOID)
{
AllocationCount++;
}
VOID DecrementAllocationCount(VOID)
{
AllocationCount--;
}
VOID MemAllocTest(VOID)
{
PVOID MemPtr1;
@@ -473,7 +334,7 @@ VOID MemAllocTest(VOID)
MemPtr3 = MmAllocateMemory(4096);
printf("MemPtr3: 0x%x\n", (int)MemPtr3);
DumpMemoryAllocMap();
VerifyHeap();
//VerifyHeap();
MachConsGetCh();
MmFreeMemory(MemPtr2);
+10 -10
View File
@@ -29,7 +29,7 @@ static PVOID
NTAPI
CmpAllocate (SIZE_T Size, BOOLEAN Paged)
{
return MmAllocateMemory(Size);
return MmHeapAlloc(Size);
}
@@ -37,7 +37,7 @@ static VOID
NTAPI
CmpFree (PVOID Ptr)
{
return MmFreeMemory(Ptr);
return MmHeapFree(Ptr);
}
@@ -538,7 +538,7 @@ RegImportValue (PHHIVE Hive,
if (ValueCell->Flags & REG_VALUE_NAME_PACKED)
{
wName = MmAllocateMemory ((ValueCell->NameSize + 1)*sizeof(WCHAR));
wName = MmHeapAlloc((ValueCell->NameSize + 1)*sizeof(WCHAR));
for (i = 0; i < ValueCell->NameSize; i++)
{
wName[i] = ((PCHAR)ValueCell->Name)[i];
@@ -547,7 +547,7 @@ RegImportValue (PHHIVE Hive,
}
else
{
wName = MmAllocateMemory (ValueCell->NameSize + sizeof(WCHAR));
wName = MmHeapAlloc(ValueCell->NameSize + sizeof(WCHAR));
memcpy (wName,
ValueCell->Name,
ValueCell->NameSize);
@@ -569,7 +569,7 @@ RegImportValue (PHHIVE Hive,
if (Error != ERROR_SUCCESS)
{
DbgPrint((DPRINT_REGISTRY, "RegSetValue() failed!\n"));
MmFreeMemory (wName);
MmHeapFree(wName);
return FALSE;
}
}
@@ -587,12 +587,12 @@ RegImportValue (PHHIVE Hive,
if (Error != ERROR_SUCCESS)
{
DbgPrint((DPRINT_REGISTRY, "RegSetValue() failed!\n"));
MmFreeMemory (wName);
MmHeapFree(wName);
return FALSE;
}
}
MmFreeMemory (wName);
MmHeapFree (wName);
return TRUE;
}
@@ -623,7 +623,7 @@ RegImportSubKey(PHHIVE Hive,
if (KeyCell->Flags & REG_KEY_NAME_PACKED)
{
wName = MmAllocateMemory ((KeyCell->NameSize + 1) * sizeof(WCHAR));
wName = MmHeapAlloc ((KeyCell->NameSize + 1) * sizeof(WCHAR));
for (i = 0; i < KeyCell->NameSize; i++)
{
wName[i] = ((PCHAR)KeyCell->Name)[i];
@@ -632,7 +632,7 @@ RegImportSubKey(PHHIVE Hive,
}
else
{
wName = MmAllocateMemory (KeyCell->NameSize + sizeof(WCHAR));
wName = MmHeapAlloc (KeyCell->NameSize + sizeof(WCHAR));
memcpy (wName,
KeyCell->Name,
KeyCell->NameSize);
@@ -645,7 +645,7 @@ RegImportSubKey(PHHIVE Hive,
Error = RegCreateKey (ParentKey,
wName,
&SubKey);
MmFreeMemory (wName);
MmHeapFree (wName);
if (Error != ERROR_SUCCESS)
{
DbgPrint((DPRINT_REGISTRY, "RegCreateKey() failed!\n"));
+8 -8
View File
@@ -33,7 +33,7 @@ RegInitializeRegistry (VOID)
#endif
/* Create root key */
RootKey = (FRLDRHKEY) MmAllocateMemory (sizeof(KEY));
RootKey = (FRLDRHKEY) MmHeapAlloc (sizeof(KEY));
InitializeListHead (&RootKey->SubKeyList);
InitializeListHead (&RootKey->ValueList);
@@ -43,7 +43,7 @@ RegInitializeRegistry (VOID)
RootKey->ValueCount = 0;
RootKey->NameSize = 4;
RootKey->Name = MmAllocateMemory (4);
RootKey->Name = MmHeapAlloc (4);
wcscpy (RootKey->Name, L"\\");
RootKey->DataType = 0;
@@ -282,7 +282,7 @@ RegCreateKey(FRLDRHKEY ParentKey,
if (CmpResult != 0)
{
/* no key found -> create new subkey */
NewKey = (FRLDRHKEY)MmAllocateMemory(sizeof(KEY));
NewKey = (FRLDRHKEY)MmHeapAlloc(sizeof(KEY));
if (NewKey == NULL)
return(ERROR_OUTOFMEMORY);
@@ -300,7 +300,7 @@ RegCreateKey(FRLDRHKEY ParentKey,
CurrentKey->SubKeyCount++;
NewKey->NameSize = NameSize;
NewKey->Name = (PWCHAR)MmAllocateMemory(NewKey->NameSize);
NewKey->Name = (PWCHAR)MmHeapAlloc(NewKey->NameSize);
if (NewKey->Name == NULL)
return(ERROR_OUTOFMEMORY);
memcpy(NewKey->Name, name, NewKey->NameSize - sizeof(WCHAR));
@@ -517,7 +517,7 @@ RegSetValue(FRLDRHKEY Key,
}
else
{
Key->Data = MmAllocateMemory(DataSize);
Key->Data = MmHeapAlloc(DataSize);
Key->DataSize = DataSize;
Key->DataType = Type;
memcpy(Key->Data, Data, DataSize);
@@ -546,7 +546,7 @@ RegSetValue(FRLDRHKEY Key,
/* add new value */
DbgPrint((DPRINT_REGISTRY, "No value found - adding new value\n"));
Value = (PVALUE)MmAllocateMemory(sizeof(VALUE));
Value = (PVALUE)MmHeapAlloc(sizeof(VALUE));
if (Value == NULL)
return(ERROR_OUTOFMEMORY);
@@ -554,7 +554,7 @@ RegSetValue(FRLDRHKEY Key,
Key->ValueCount++;
Value->NameSize = (wcslen(ValueName)+1)*sizeof(WCHAR);
Value->Name = (PWCHAR)MmAllocateMemory(Value->NameSize);
Value->Name = (PWCHAR)MmHeapAlloc(Value->NameSize);
if (Value->Name == NULL)
return(ERROR_OUTOFMEMORY);
wcscpy(Value->Name, ValueName);
@@ -577,7 +577,7 @@ RegSetValue(FRLDRHKEY Key,
}
else
{
Value->Data = MmAllocateMemory(DataSize);
Value->Data = MmHeapAlloc(DataSize);
if (Value->Data == NULL)
return(ERROR_OUTOFMEMORY);
Value->DataType = Type;
+1593
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -33,7 +33,7 @@ STDCALL
RtlpAllocateMemory(ULONG Bytes,
ULONG Tag)
{
return MmAllocateMemory(Bytes);
return MmHeapAlloc(Bytes);
}
@@ -42,5 +42,5 @@ STDCALL
RtlpFreeMemory(PVOID Mem,
ULONG Tag)
{
return MmFreeMemory(Mem);
return MmHeapFree(Mem);
}
+2 -2
View File
@@ -604,7 +604,7 @@ VOID UiShowMessageBoxesInSection(PCSTR SectionName)
//if (MessageBoxTextSize > 0)
{
// Allocate enough memory to hold the text
MessageBoxText = MmAllocateMemory(MessageBoxTextSize);
MessageBoxText = MmHeapAlloc(MessageBoxTextSize);
if (MessageBoxText)
{
@@ -618,7 +618,7 @@ VOID UiShowMessageBoxesInSection(PCSTR SectionName)
UiMessageBox(MessageBoxText);
// Free the memory
MmFreeMemory(MessageBoxText);
MmHeapFree(MessageBoxText);
}
}
}
+9 -3
View File
@@ -170,7 +170,7 @@ WinLdrAllocateDataTableEntry(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock,
USHORT Length;
/* Allocate memory for a data table entry, zero-initialize it */
DataTableEntry = (PLDR_DATA_TABLE_ENTRY)MmAllocateMemory(sizeof(LDR_DATA_TABLE_ENTRY));
DataTableEntry = (PLDR_DATA_TABLE_ENTRY)MmHeapAlloc(sizeof(LDR_DATA_TABLE_ENTRY));
if (DataTableEntry == NULL)
return FALSE;
RtlZeroMemory(DataTableEntry, sizeof(LDR_DATA_TABLE_ENTRY));
@@ -188,9 +188,12 @@ WinLdrAllocateDataTableEntry(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock,
/* Initialize BaseDllName field (UNICODE_STRING) from the Ansi BaseDllName
by simple conversion - copying each character */
Length = (USHORT)(strlen(BaseDllName) * sizeof(WCHAR));
Buffer = (PWSTR)MmAllocateMemory(Length);
Buffer = (PWSTR)MmHeapAlloc(Length);
if (Buffer == NULL)
{
MmHeapFree(DataTableEntry);
return FALSE;
}
RtlZeroMemory(Buffer, Length);
DataTableEntry->BaseDllName.Length = Length;
@@ -204,9 +207,12 @@ WinLdrAllocateDataTableEntry(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock,
/* Initialize FullDllName field (UNICODE_STRING) from the Ansi FullDllName
using the same method */
Length = (USHORT)(strlen(FullDllName) * sizeof(WCHAR));
Buffer = (PWSTR)MmAllocateMemory(Length);
Buffer = (PWSTR)MmHeapAlloc(Length);
if (Buffer == NULL)
{
MmHeapFree(DataTableEntry);
return FALSE;
}
RtlZeroMemory(Buffer, Length);
DataTableEntry->FullDllName.Length = Length;
+10 -10
View File
@@ -51,7 +51,7 @@ AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
PLOADER_PARAMETER_BLOCK LoaderBlock;
/* Allocate and zero-init the LPB */
LoaderBlock = MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK));
LoaderBlock = MmHeapAlloc(sizeof(LOADER_PARAMETER_BLOCK));
RtlZeroMemory(LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
/* Init three critical lists, used right away */
@@ -60,7 +60,7 @@ AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
InitializeListHead(&LoaderBlock->BootDriverListHead);
/* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
LoaderBlock->NlsData = MmAllocateMemory(sizeof(NLS_DATA_BLOCK));
LoaderBlock->NlsData = MmHeapAlloc(sizeof(NLS_DATA_BLOCK));
if (LoaderBlock->NlsData == NULL)
{
UiMessageBox("Failed to allocate memory for NLS table data!");
@@ -104,32 +104,32 @@ WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
DbgPrint((DPRINT_WINDOWS, "Options: %s\n", Options));
/* Fill Arc BootDevice */
LoaderBlock->ArcBootDeviceName = MmAllocateMemory(strlen(ArcBoot)+1);
LoaderBlock->ArcBootDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
strcpy(LoaderBlock->ArcBootDeviceName, ArcBoot);
LoaderBlock->ArcBootDeviceName = PaToVa(LoaderBlock->ArcBootDeviceName);
/* Fill Arc HalDevice, it matches ArcBoot path */
LoaderBlock->ArcHalDeviceName = MmAllocateMemory(strlen(ArcBoot)+1);
LoaderBlock->ArcHalDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
strcpy(LoaderBlock->ArcHalDeviceName, ArcBoot);
LoaderBlock->ArcHalDeviceName = PaToVa(LoaderBlock->ArcHalDeviceName);
/* Fill SystemRoot */
LoaderBlock->NtBootPathName = MmAllocateMemory(strlen(SystemRoot)+1);
LoaderBlock->NtBootPathName = MmHeapAlloc(strlen(SystemRoot)+1);
strcpy(LoaderBlock->NtBootPathName, SystemRoot);
LoaderBlock->NtBootPathName = PaToVa(LoaderBlock->NtBootPathName);
/* Fill NtHalPathName */
LoaderBlock->NtHalPathName = MmAllocateMemory(strlen(HalPath)+1);
LoaderBlock->NtHalPathName = MmHeapAlloc(strlen(HalPath)+1);
strcpy(LoaderBlock->NtHalPathName, HalPath);
LoaderBlock->NtHalPathName = PaToVa(LoaderBlock->NtHalPathName);
/* Fill load options */
LoaderBlock->LoadOptions = MmAllocateMemory(strlen(Options)+1);
LoaderBlock->LoadOptions = MmHeapAlloc(strlen(Options)+1);
strcpy(LoaderBlock->LoadOptions, Options);
LoaderBlock->LoadOptions = PaToVa(LoaderBlock->LoadOptions);
/* Arc devices */
LoaderBlock->ArcDiskInformation = (PARC_DISK_INFORMATION)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION));
LoaderBlock->ArcDiskInformation = (PARC_DISK_INFORMATION)MmHeapAlloc(sizeof(ARC_DISK_INFORMATION));
InitializeListHead(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead);
/* Convert ARC disk information from freeldr to a correct format */
@@ -138,7 +138,7 @@ WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
PARC_DISK_SIGNATURE ArcDiskInfo;
/* Get the ARC structure */
ArcDiskInfo = (PARC_DISK_SIGNATURE)MmAllocateMemory(sizeof(ARC_DISK_SIGNATURE));//&BldrDiskInfo[i];
ArcDiskInfo = (PARC_DISK_SIGNATURE)MmHeapAlloc(sizeof(ARC_DISK_SIGNATURE));//&BldrDiskInfo[i];
RtlZeroMemory(ArcDiskInfo, sizeof(ARC_DISK_SIGNATURE));
/* Copy the data over */
@@ -179,7 +179,7 @@ WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
List_PaToVa(&LoaderBlock->BootDriverListHead);
/* Initialize Extension now */
Extension = MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION));
Extension = MmHeapAlloc(sizeof(LOADER_PARAMETER_EXTENSION));
if (Extension == NULL)
{
UiMessageBox("Failed to allocate LPB Extension!");
+3 -109
View File
@@ -162,6 +162,9 @@ MempAllocatePageTables()
PhysicalPageTablesBuffer = &Buffer[MM_PAGE_SIZE*2];
KernelPageTablesBuffer = PhysicalPageTablesBuffer + NumPageTables*MM_PAGE_SIZE;
// Mark physical PTE's buffer as FirmwareTemporary
//MmSetMemoryType(KernelPageTablesBuffer, NumPageTables*MM_PAGE_SIZE, LoaderFirmwareTemporary);
// Zero counters of page tables used
PhysicalPageTables = 0;
KernelPageTables = 0;
@@ -842,118 +845,9 @@ WinLdrSetProcessorContext(PVOID GdtIdt, IN ULONG Pcr, IN ULONG Tss)
// Some unused descriptors should go here
// ...
//
// Fill IDT with Traps
//
#if 0
pIdt[0].Offset = (i386DivideByZero | KSEG0_BASE) & 0xFFFF;
pIdt[0].ExtendedOffset = 0x8; // Selector
pIdt[0].Access = 0x8F00;
pIdt[0].Selector = (i386DivideByZero | KSEG0_BASE) >> 16; // Extended Offset
pIdt[1].Offset = (i386DebugException | KSEG0_BASE) & 0xFFFF;
pIdt[1].ExtendedOffset = 0x8; // Selector
pIdt[1].Access = 0x8F00;
pIdt[1].Selector = (i386DebugException | KSEG0_BASE) >> 16; // Extended Offset
pIdt[2].Offset = (i386NMIException | KSEG0_BASE) & 0xFFFF;
pIdt[2].ExtendedOffset = 0x8; // Selector
pIdt[2].Access = 0x8F00;
pIdt[2].Selector = (i386NMIException | KSEG0_BASE) >> 16; // Extended Offset
pIdt[3].Offset = (i386Breakpoint | KSEG0_BASE) & 0xFFFF;
pIdt[3].ExtendedOffset = 0x8; // Selector
pIdt[3].Access = 0x8F00;
pIdt[3].Selector = (i386Breakpoint | KSEG0_BASE) >> 16; // Extended Offset
pIdt[4].Offset = (i386Overflow | KSEG0_BASE) & 0xFFFF;
pIdt[4].ExtendedOffset = 0x8; // Selector
pIdt[4].Access = 0x8F00;
pIdt[4].Selector = (i386Overflow | KSEG0_BASE) >> 16; // Extended Offset
pIdt[5].Selector = (i386BoundException | KSEG0_BASE) >> 16; // Extended Offset
pIdt[5].Offset = (i386BoundException | KSEG0_BASE) & 0xFFFF;
pIdt[5].ExtendedOffset = 0x8; // Selector
pIdt[5].Access = 0x8F00;
pIdt[6].Selector = (i386InvalidOpcode | KSEG0_BASE) >> 16; // Extended Offset
pIdt[6].Offset = (i386InvalidOpcode | KSEG0_BASE) & 0xFFFF;
pIdt[6].ExtendedOffset = 0x8; // Selector
pIdt[6].Access = 0x8F00;
pIdt[7].Selector = (i386FPUNotAvailable | KSEG0_BASE) >> 16; // Extended Offset
pIdt[7].Offset = (i386FPUNotAvailable | KSEG0_BASE) & 0xFFFF;
pIdt[7].ExtendedOffset = 0x8; // Selector
pIdt[7].Access = 0x8F00;
pIdt[8].Selector = (i386DoubleFault | KSEG0_BASE) >> 16; // Extended Offset
pIdt[8].Offset = (i386DoubleFault | KSEG0_BASE) & 0xFFFF;
pIdt[8].ExtendedOffset = 0x8; // Selector
pIdt[8].Access = 0x8F00;
pIdt[9].Selector = (i386CoprocessorSegment | KSEG0_BASE) >> 16; // Extended Offset
pIdt[9].Offset = (i386CoprocessorSegment | KSEG0_BASE) & 0xFFFF;
pIdt[9].ExtendedOffset = 0x8; // Selector
pIdt[9].Access = 0x8F00;
pIdt[10].Selector = (i386InvalidTSS | KSEG0_BASE) >> 16; // Extended Offset
pIdt[10].Offset = (i386InvalidTSS | KSEG0_BASE) & 0xFFFF;
pIdt[10].ExtendedOffset = 0x8; // Selector
pIdt[10].Access = 0x8F00;
pIdt[11].Selector = (i386SegmentNotPresent | KSEG0_BASE) >> 16; // Extended Offset
pIdt[11].Offset = (i386SegmentNotPresent | KSEG0_BASE) & 0xFFFF;
pIdt[11].ExtendedOffset = 0x8; // Selector
pIdt[11].Access = 0x8F00;
pIdt[12].Selector = (i386StackException | KSEG0_BASE) >> 16; // Extended Offset
pIdt[12].Offset = (i386StackException | KSEG0_BASE) & 0xFFFF;
pIdt[12].ExtendedOffset = 0x8; // Selector
pIdt[12].Access = 0x8F00;
pIdt[13].Selector = (i386GeneralProtectionFault | KSEG0_BASE) >> 16; // Extended Offset
pIdt[13].Offset = (i386GeneralProtectionFault | KSEG0_BASE) & 0xFFFF;
pIdt[13].ExtendedOffset = 0x8; // Selector
pIdt[13].Access = 0x8F00;
pIdt[14].Selector = (i386PageFault | KSEG0_BASE) >> 16; // Extended Offset
pIdt[14].Offset = (i386PageFault | KSEG0_BASE) & 0xFFFF;
pIdt[14].ExtendedOffset = 0x8; // Selector
pIdt[14].Access = 0x8F00;
pIdt[15].Selector = 0; // Extended Offset
pIdt[15].Offset = 0;
pIdt[15].ExtendedOffset = 0; // Selector
pIdt[15].Access = 0;
pIdt[16].Selector = (i386CoprocessorError | KSEG0_BASE) >> 16; // Extended Offset
pIdt[16].Offset = (i386CoprocessorError | KSEG0_BASE) & 0xFFFF;
pIdt[16].ExtendedOffset = 0x8; // Selector
pIdt[16].Access = 0x8F00;
pIdt[17].Selector = (i386AlignmentCheck | KSEG0_BASE) >> 16; // Extended Offset
pIdt[17].Offset = (i386AlignmentCheck | KSEG0_BASE) & 0xFFFF;
pIdt[17].ExtendedOffset = 0x8; // Selector
pIdt[17].Access = 0x8F00;
#endif
/*for (i=0; i<16; i++)
{
//pIdt[i].Offset = ((ULONG_PTR)i386GeneralProtectionFault | KSEG0_BASE) & 0xFFFF;
//pIdt[i].ExtendedOffset = 0x8; // Selector
//pIdt[i].Access = 0x8F00;
//pIdt[i].Selector = ((ULONG_PTR)i386GeneralProtectionFault | KSEG0_BASE) >> 16; // Extended Offset
pIdt[i].Offset = ((ULONG_PTR)i386GeneralProtectionFault | KSEG0_BASE) & 0xFFFF;
pIdt[i].ExtendedOffset = ((ULONG_PTR)i386GeneralProtectionFault | KSEG0_BASE) >> 16; // Extended Offset
pIdt[i].Access = 0x8F00;
pIdt[i].Selector = 0x8;
}*/
// Copy the old IDT
RtlCopyMemory(pIdt, (PVOID)OldIdt.Base, OldIdt.Limit);
// Mask interrupts
//asm("cli\n"); // they are already masked before enabling paged mode
+26 -4
View File
@@ -669,7 +669,7 @@ WinLdrAddDriverToList(LIST_ENTRY *BootDriverListHead,
NTSTATUS Status;
ULONG PathLength;
BootDriverEntry = MmAllocateMemory(sizeof(BOOT_DRIVER_LIST_ENTRY));
BootDriverEntry = MmHeapAlloc(sizeof(BOOT_DRIVER_LIST_ENTRY));
if (!BootDriverEntry)
return FALSE;
@@ -686,14 +686,21 @@ WinLdrAddDriverToList(LIST_ENTRY *BootDriverListHead,
BootDriverEntry->FilePath.Length = 0;
BootDriverEntry->FilePath.MaximumLength = PathLength + sizeof(WCHAR);
BootDriverEntry->FilePath.Buffer = MmAllocateMemory(PathLength);
BootDriverEntry->FilePath.Buffer = MmHeapAlloc(PathLength);
if (!BootDriverEntry->FilePath.Buffer)
{
MmHeapFree(BootDriverEntry);
return FALSE;
}
Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, ImagePath);
if (!NT_SUCCESS(Status))
{
MmHeapFree(BootDriverEntry->FilePath.Buffer);
MmHeapFree(BootDriverEntry);
return FALSE;
}
}
else
{
@@ -701,29 +708,44 @@ WinLdrAddDriverToList(LIST_ENTRY *BootDriverListHead,
PathLength = wcslen(ServiceName)*sizeof(WCHAR) + sizeof(L"system32\\drivers\\.sys");;
BootDriverEntry->FilePath.Length = 0;
BootDriverEntry->FilePath.MaximumLength = PathLength+sizeof(WCHAR);
BootDriverEntry->FilePath.Buffer = MmAllocateMemory(PathLength);
BootDriverEntry->FilePath.Buffer = MmHeapAlloc(PathLength);
if (!BootDriverEntry->FilePath.Buffer)
{
MmHeapFree(BootDriverEntry);
return FALSE;
}
Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, L"system32\\drivers\\");
if (!NT_SUCCESS(Status))
{
MmHeapFree(BootDriverEntry->FilePath.Buffer);
MmHeapFree(BootDriverEntry);
return FALSE;
}
Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, ServiceName);
if (!NT_SUCCESS(Status))
{
MmHeapFree(BootDriverEntry->FilePath.Buffer);
MmHeapFree(BootDriverEntry);
return FALSE;
}
Status = RtlAppendUnicodeToString(&BootDriverEntry->FilePath, L".sys");
if (!NT_SUCCESS(Status))
{
MmHeapFree(BootDriverEntry->FilePath.Buffer);
MmHeapFree(BootDriverEntry);
return FALSE;
}
}
// Add registry path
PathLength = (wcslen(RegistryPath)+wcslen(ServiceName))*sizeof(WCHAR);
BootDriverEntry->RegistryPath.Length = 0;
BootDriverEntry->RegistryPath.MaximumLength = PathLength;//+sizeof(WCHAR);
BootDriverEntry->RegistryPath.Buffer = MmAllocateMemory(PathLength);
BootDriverEntry->RegistryPath.Buffer = MmHeapAlloc(PathLength);
if (!BootDriverEntry->RegistryPath.Buffer)
return FALSE;