@@ -415,36 +415,44 @@ PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
415
415
{
416
416
const FREELDR_MEMORY_DESCRIPTOR * MemoryDescriptor = NULL ;
417
417
SIZE_T PageLookupTableSize ;
418
- PFN_NUMBER PageLookupTablePages ;
419
- PFN_NUMBER PageLookupTableStartPage = 0 ;
418
+ PFN_NUMBER RequiredPages ;
419
+ PFN_NUMBER CandidateBasePage = 0 ;
420
+ PFN_NUMBER CandidatePageCount ;
421
+ PFN_NUMBER PageLookupTableEndPage ;
420
422
PVOID PageLookupTableMemAddress = NULL ;
421
423
422
424
// Calculate how much pages we need to keep the page lookup table
423
425
PageLookupTableSize = TotalPageCount * sizeof (PAGE_LOOKUP_TABLE_ITEM );
424
- PageLookupTablePages = PageLookupTableSize / MM_PAGE_SIZE ;
426
+ RequiredPages = PageLookupTableSize / MM_PAGE_SIZE ;
425
427
426
428
// Search the highest memory block big enough to contain lookup table
427
429
while ((MemoryDescriptor = ArcGetMemoryDescriptor (MemoryDescriptor )) != NULL )
428
430
{
429
431
// Continue, if memory is not free
430
432
if (MemoryDescriptor -> MemoryType != LoaderFree ) continue ;
431
433
432
- // Continue, if the block is not big enough?
433
- if (MemoryDescriptor -> PageCount < PageLookupTablePages ) continue ;
434
+ // Continue, if the block is not big enough
435
+ if (MemoryDescriptor -> PageCount < RequiredPages ) continue ;
434
436
435
437
// Continue, if it is not at a higher address than previous address
436
- if (MemoryDescriptor -> BasePage < PageLookupTableStartPage ) continue ;
438
+ if (MemoryDescriptor -> BasePage < CandidateBasePage ) continue ;
437
439
438
440
// Continue, if the address is too high
439
- if (MemoryDescriptor -> BasePage >= MM_MAX_PAGE ) continue ;
441
+ if (MemoryDescriptor -> BasePage + RequiredPages >= MM_MAX_PAGE ) continue ;
440
442
441
443
// Memory block is more suitable than the previous one
442
- PageLookupTableStartPage = MemoryDescriptor -> BasePage ;
443
- PageLookupTableMemAddress = (PVOID )((ULONG_PTR )
444
- (MemoryDescriptor -> BasePage + MemoryDescriptor -> PageCount ) * MM_PAGE_SIZE
445
- - PageLookupTableSize );
444
+ CandidateBasePage = MemoryDescriptor -> BasePage ;
445
+ CandidatePageCount = MemoryDescriptor -> PageCount ;
446
446
}
447
447
448
+ // Calculate the end address for the lookup table
449
+ PageLookupTableEndPage = min (CandidateBasePage + CandidatePageCount ,
450
+ MM_MAX_PAGE );
451
+
452
+ // Calculate the virtual address
453
+ PageLookupTableMemAddress = (PVOID )((PageLookupTableEndPage * PAGE_SIZE )
454
+ - PageLookupTableSize );
455
+
448
456
TRACE ("MmFindLocationForPageLookupTable() returning 0x%x\n" , PageLookupTableMemAddress );
449
457
450
458
return PageLookupTableMemAddress ;
0 commit comments