2 * COPYRIGHT: GPL, See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/amd64/init.c
5 * PURPOSE: Memory Manager Initialization for amd64
7 * PROGRAMMERS: Timo kreuzer (timo.kreuzer@reactos.org)
8 * ReactOS Portable Systems Group
11 /* INCLUDES ***************************************************************/
17 #include "../ARM3/miarm.h"
20 extern PMMPTE MmDebugPte
;
23 /* GLOBALS *****************************************************************/
25 /* Template PTE and PDE for a kernel page */
26 MMPTE ValidKernelPde
= {{PTE_VALID
|PTE_READWRITE
|PTE_DIRTY
|PTE_ACCESSED
}};
27 MMPTE ValidKernelPte
= {{PTE_VALID
|PTE_READWRITE
|PTE_DIRTY
|PTE_ACCESSED
}};
29 /* Template PDE for a demand-zero page */
30 MMPDE DemandZeroPde
= {{MM_READWRITE
<< MM_PTE_SOFTWARE_PROTECTION_BITS
}};
31 MMPTE DemandZeroPte
= {{MM_READWRITE
<< MM_PTE_SOFTWARE_PROTECTION_BITS
}};
33 /* Template PTE for prototype page */
34 MMPTE PrototypePte
= {{(MM_READWRITE
<< MM_PTE_SOFTWARE_PROTECTION_BITS
) |
35 PTE_PROTOTYPE
| (MI_PTE_LOOKUP_NEEDED
<< 32)}};
37 /* Template PTE for decommited page */
38 MMPTE MmDecommittedPte
= {{MM_DECOMMIT
<< MM_PTE_SOFTWARE_PROTECTION_BITS
}};
41 PVOID MiSessionViewEnd
;
42 PVOID MiSystemPteSpaceStart
;
43 PVOID MiSystemPteSpaceEnd
;
45 ULONG64 MxPfnSizeInBytes
;
46 BOOLEAN MiIncludeType
[LoaderMaximum
];
47 PFN_NUMBER MxFreePageBase
;
48 ULONG64 MxFreePageCount
= 0;
50 BOOLEAN MiPfnsInitialized
= FALSE
;
52 /* FUNCTIONS *****************************************************************/
57 MiInitializeSessionSpaceLayout()
59 MmSessionSize
= MI_SESSION_SIZE
;
60 MmSessionViewSize
= MI_SESSION_VIEW_SIZE
;
61 MmSessionPoolSize
= MI_SESSION_POOL_SIZE
;
62 MmSessionImageSize
= MI_SESSION_IMAGE_SIZE
;
63 MmSystemViewSize
= MI_SYSTEM_VIEW_SIZE
;
65 /* Set up session space */
66 MiSessionSpaceEnd
= (PVOID
)MI_SESSION_SPACE_END
;
68 /* This is where we will load Win32k.sys and the video driver */
69 MiSessionImageEnd
= MiSessionSpaceEnd
;
70 MiSessionImageStart
= (PCHAR
)MiSessionImageEnd
- MmSessionImageSize
;
72 /* The view starts right below the session working set (itself below
74 MiSessionViewEnd
= (PVOID
)MI_SESSION_VIEW_END
;
75 MiSessionViewStart
= (PCHAR
)MiSessionViewEnd
- MmSessionViewSize
;
76 ASSERT(IS_PAGE_ALIGNED(MiSessionViewStart
));
78 /* Session pool follows */
79 MiSessionPoolEnd
= MiSessionViewStart
;
80 MiSessionPoolStart
= (PCHAR
)MiSessionPoolEnd
- MmSessionPoolSize
;
81 ASSERT(IS_PAGE_ALIGNED(MiSessionPoolStart
));
83 /* And it all begins here */
84 MmSessionBase
= MiSessionPoolStart
;
86 /* System view space ends at session space, so now that we know where
87 * this is, we can compute the base address of system view space itself. */
88 MiSystemViewStart
= (PCHAR
)MmSessionBase
- MmSystemViewSize
;
89 ASSERT(IS_PAGE_ALIGNED(MiSystemViewStart
));
92 ASSERT(MiSessionViewEnd
<= MiSessionImageStart
);
93 ASSERT(MmSessionBase
<= MiSessionPoolStart
);
103 MMPDE TmplPde
= ValidKernelPde
;
106 for (PointerPpe
= MiAddressToPpe(StartAddress
);
107 PointerPpe
<= MiAddressToPpe(EndAddress
);
110 /* Check if its already mapped */
111 if (!PointerPpe
->u
.Hard
.Valid
)
114 TmplPde
.u
.Hard
.PageFrameNumber
= MxGetNextPage(1);
115 MI_WRITE_VALID_PTE(PointerPpe
, TmplPde
);
117 /* Zero out the page table */
118 RtlZeroMemory(MiPteToAddress(PointerPpe
), PAGE_SIZE
);
130 MMPDE TmplPde
= ValidKernelPde
;
133 for (PointerPde
= MiAddressToPde(StartAddress
);
134 PointerPde
<= MiAddressToPde(EndAddress
);
137 /* Check if its already mapped */
138 if (!PointerPde
->u
.Hard
.Valid
)
141 TmplPde
.u
.Hard
.PageFrameNumber
= MxGetNextPage(1);
142 MI_WRITE_VALID_PTE(PointerPde
, TmplPde
);
144 /* Zero out the page table */
145 RtlZeroMemory(MiPteToAddress(PointerPde
), PAGE_SIZE
);
157 MMPTE TmplPte
= ValidKernelPte
;
160 for (PointerPte
= MiAddressToPte(StartAddress
);
161 PointerPte
<= MiAddressToPte(EndAddress
);
164 /* Check if its already mapped */
165 if (!PointerPte
->u
.Hard
.Valid
)
168 TmplPte
.u
.Hard
.PageFrameNumber
= MxGetNextPage(1);
169 MI_WRITE_VALID_PTE(PointerPte
, TmplPte
);
171 /* Zero out the page (FIXME: not always neccessary) */
172 RtlZeroMemory(MiPteToAddress(PointerPte
), PAGE_SIZE
);
180 MiInitializePageTable()
182 ULONG64 PxePhysicalAddress
;
183 MMPTE TmplPte
, *PointerPxe
;
186 /* Get current directory base */
187 PxePfn
= ((PMMPTE
)PXE_SELFMAP
)->u
.Hard
.PageFrameNumber
;
188 PxePhysicalAddress
= PxePfn
<< PAGE_SHIFT
;
189 ASSERT(PxePhysicalAddress
== __readcr3());
191 /* Set directory base for the system process */
192 PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[0] = PxePhysicalAddress
;
194 /* Enable global pages */
195 __writecr4(__readcr4() | CR4_PGE
);
196 ASSERT(__readcr4() & CR4_PGE
);
198 /* Enable no execute */
199 __writemsr(X86_MSR_EFER
, __readmsr(X86_MSR_EFER
) | EFER_NXE
);
201 /* Loop the user mode PXEs */
202 for (PointerPxe
= MiAddressToPxe(0);
203 PointerPxe
<= MiAddressToPxe(MmHighestUserAddress
);
206 /* Zero the PXE, clear all mappings */
207 PointerPxe
->u
.Long
= 0;
213 /* Set up a template PTE */
215 TmplPte
.u
.Flush
.Valid
= 1;
216 TmplPte
.u
.Flush
.Write
= 1;
217 HyperTemplatePte
= TmplPte
;
219 /* Create PDPTs (72 KB) for shared system address space,
220 * skip page tables TODO: use global pages. */
223 for (PointerPxe
= MiAddressToPxe((PVOID
)HYPER_SPACE
);
224 PointerPxe
<= MiAddressToPxe(MI_HIGHEST_SYSTEM_ADDRESS
);
227 /* Is the PXE already valid? */
228 if (!PointerPxe
->u
.Hard
.Valid
)
230 /* It's not Initialize it */
231 TmplPte
.u
.Flush
.PageFrameNumber
= MxGetNextPage(1);
232 *PointerPxe
= TmplPte
;
234 /* Zero the page. The PXE is the PTE for the PDPT. */
235 RtlZeroMemory(MiPteToAddress(PointerPxe
), PAGE_SIZE
);
239 /* Map PPEs for paged pool */
240 MiMapPPEs(MmPagedPoolStart
, MmPagedPoolEnd
);
242 /* Setup 1 PPE for hyper space */
243 MiMapPPEs((PVOID
)HYPER_SPACE
, (PVOID
)HYPER_SPACE_END
);
245 /* Setup the mapping PDEs */
246 MiMapPDEs((PVOID
)MI_MAPPING_RANGE_START
, (PVOID
)MI_MAPPING_RANGE_END
);
248 /* Setup the mapping PTEs */
249 MmFirstReservedMappingPte
= MiAddressToPte((PVOID
)MI_MAPPING_RANGE_START
);
250 MmLastReservedMappingPte
= MiAddressToPte((PVOID
)MI_MAPPING_RANGE_END
);
251 MmFirstReservedMappingPte
->u
.Hard
.PageFrameNumber
= MI_HYPERSPACE_PTES
;
254 /* Setup debug mapping PTE */
255 MiMapPPEs((PVOID
)MI_DEBUG_MAPPING
, (PVOID
)MI_DEBUG_MAPPING
);
256 MiMapPDEs((PVOID
)MI_DEBUG_MAPPING
, (PVOID
)MI_DEBUG_MAPPING
);
257 MmDebugPte
= MiAddressToPte((PVOID
)MI_DEBUG_MAPPING
);
260 /* Setup PDE and PTEs for VAD bitmap and working set list */
261 MiMapPDEs((PVOID
)MI_VAD_BITMAP
, (PVOID
)(MI_WORKING_SET_LIST
+ PAGE_SIZE
- 1));
262 MiMapPTEs((PVOID
)MI_VAD_BITMAP
, (PVOID
)(MI_WORKING_SET_LIST
+ PAGE_SIZE
- 1));
268 MiBuildNonPagedPool(VOID
)
270 /* Check if this is a machine with less than 256MB of RAM, and no overide */
271 if ((MmNumberOfPhysicalPages
<= MI_MIN_PAGES_FOR_NONPAGED_POOL_TUNING
) &&
272 !(MmSizeOfNonPagedPoolInBytes
))
274 /* Force the non paged pool to be 2MB so we can reduce RAM usage */
275 MmSizeOfNonPagedPoolInBytes
= 2 * 1024 * 1024;
278 /* Check if the user gave a ridicuously large nonpaged pool RAM size */
279 if ((MmSizeOfNonPagedPoolInBytes
>> PAGE_SHIFT
) >
280 (MmNumberOfPhysicalPages
* 7 / 8))
282 /* More than 7/8ths of RAM was dedicated to nonpaged pool, ignore! */
283 MmSizeOfNonPagedPoolInBytes
= 0;
286 /* Check if no registry setting was set, or if the setting was too low */
287 if (MmSizeOfNonPagedPoolInBytes
< MmMinimumNonPagedPoolSize
)
289 /* Start with the minimum (256 KB) and add 32 KB for each MB above 4 */
290 MmSizeOfNonPagedPoolInBytes
= MmMinimumNonPagedPoolSize
;
291 MmSizeOfNonPagedPoolInBytes
+= (MmNumberOfPhysicalPages
- 1024) /
292 256 * MmMinAdditionNonPagedPoolPerMb
;
295 /* Check if the registy setting or our dynamic calculation was too high */
296 if (MmSizeOfNonPagedPoolInBytes
> MI_MAX_INIT_NONPAGED_POOL_SIZE
)
298 /* Set it to the maximum */
299 MmSizeOfNonPagedPoolInBytes
= MI_MAX_INIT_NONPAGED_POOL_SIZE
;
302 /* Check if a percentage cap was set through the registry */
303 if (MmMaximumNonPagedPoolPercent
)
305 /* Don't feel like supporting this right now */
309 /* Page-align the nonpaged pool size */
310 MmSizeOfNonPagedPoolInBytes
&= ~(PAGE_SIZE
- 1);
312 /* Now, check if there was a registry size for the maximum size */
313 if (!MmMaximumNonPagedPoolInBytes
)
315 /* Start with the default (1MB) and add 400 KB for each MB above 4 */
316 MmMaximumNonPagedPoolInBytes
= MmDefaultMaximumNonPagedPool
;
317 MmMaximumNonPagedPoolInBytes
+= (MmNumberOfPhysicalPages
- 1024) /
318 256 * MmMaxAdditionNonPagedPoolPerMb
;
321 /* Don't let the maximum go too high */
322 if (MmMaximumNonPagedPoolInBytes
> MI_MAX_NONPAGED_POOL_SIZE
)
324 /* Set it to the upper limit */
325 MmMaximumNonPagedPoolInBytes
= MI_MAX_NONPAGED_POOL_SIZE
;
328 /* Convert nonpaged pool size from bytes to pages */
329 MmMaximumNonPagedPoolInPages
= MmMaximumNonPagedPoolInBytes
>> PAGE_SHIFT
;
331 /* Non paged pool starts after the PFN database */
332 MmNonPagedPoolStart
= MmPfnDatabase
+ MxPfnAllocation
* PAGE_SIZE
;
334 /* Calculate the nonpaged pool expansion start region */
335 MmNonPagedPoolExpansionStart
= (PCHAR
)MmNonPagedPoolStart
+
336 MmSizeOfNonPagedPoolInBytes
;
337 ASSERT(IS_PAGE_ALIGNED(MmNonPagedPoolExpansionStart
));
339 /* And this is where the none paged pool ends */
340 MmNonPagedPoolEnd
= (PCHAR
)MmNonPagedPoolStart
+ MmMaximumNonPagedPoolInBytes
;
341 ASSERT(MmNonPagedPoolEnd
< (PVOID
)MM_HAL_VA_START
);
343 /* Map PPEs and PDEs for non paged pool (including expansion) */
344 MiMapPPEs(MmNonPagedPoolStart
, MmNonPagedPoolEnd
);
345 MiMapPDEs(MmNonPagedPoolStart
, MmNonPagedPoolEnd
);
347 /* Map the nonpaged pool PTEs (without expansion) */
348 MiMapPTEs(MmNonPagedPoolStart
, (PCHAR
)MmNonPagedPoolExpansionStart
- 1);
350 /* Initialize the ARM3 nonpaged pool */
351 MiInitializeNonPagedPool();
352 MiInitializeNonPagedPoolThresholds();
359 MiBuildSystemPteSpace()
363 /* Use the default numer of system PTEs */
364 MmNumberOfSystemPtes
= MI_NUMBER_SYSTEM_PTES
;
365 MiNonPagedSystemSize
= (MmNumberOfSystemPtes
+ 1) * PAGE_SIZE
;
367 /* Put system PTEs at the start of the system VA space */
368 MiSystemPteSpaceStart
= MmNonPagedSystemStart
;
369 MiSystemPteSpaceEnd
= (PUCHAR
)MiSystemPteSpaceStart
+ MiNonPagedSystemSize
;
371 /* Map the PPEs and PDEs for the system PTEs */
372 MiMapPPEs(MiSystemPteSpaceStart
, MiSystemPteSpaceEnd
);
373 MiMapPDEs(MiSystemPteSpaceStart
, MiSystemPteSpaceEnd
);
375 /* Initialize the system PTE space */
376 PointerPte
= MiAddressToPte(MiSystemPteSpaceStart
);
377 MiInitializeSystemPtes(PointerPte
, MmNumberOfSystemPtes
, SystemPteSpace
);
379 /* Reserve system PTEs for zeroing PTEs and clear them */
380 MiFirstReservedZeroingPte
= MiReserveSystemPtes(MI_ZERO_PTES
, SystemPteSpace
);
381 RtlZeroMemory(MiFirstReservedZeroingPte
, MI_ZERO_PTES
* sizeof(MMPTE
));
383 /* Set the counter to maximum */
384 MiFirstReservedZeroingPte
->u
.Hard
.PageFrameNumber
= MI_ZERO_PTES
- 1;
389 MiSetupPfnForPageTable(
390 PFN_NUMBER PageFrameIndex
,
396 /* Get the pfn entry for this page */
397 Pfn
= MiGetPfnEntry(PageFrameIndex
);
399 /* Check if it's valid memory */
400 if ((PageFrameIndex
<= MmHighestPhysicalPage
) &&
401 (MmIsAddressValid(Pfn
)) &&
402 (Pfn
->u3
.e1
.PageLocation
== ActiveAndValid
))
404 /* Setup the PFN entry */
406 Pfn
->u2
.ShareCount
++;
407 Pfn
->PteAddress
= PointerPte
;
408 Pfn
->OriginalPte
= *PointerPte
;
409 Pfn
->u3
.e1
.PageLocation
= ActiveAndValid
;
410 Pfn
->u3
.e1
.CacheAttribute
= MiNonCached
;
411 Pfn
->u3
.e2
.ReferenceCount
= 1;
412 Pfn
->u4
.PteFrame
= PFN_FROM_PTE(MiAddressToPte(PointerPte
));
415 /* Increase the shared count of the PFN entry for the PDE */
416 PointerPde
= MiAddressToPde(MiPteToAddress(PointerPte
));
417 Pfn
= MiGetPfnEntry(PFN_FROM_PTE(PointerPde
));
418 Pfn
->u2
.ShareCount
++;
423 MiBuildPfnDatabaseFromPageTables(VOID
)
425 PVOID Address
= NULL
;
426 PFN_NUMBER PageFrameIndex
;
431 #if (_MI_PAGING_LEVELS >= 3)
435 #if (_MI_PAGING_LEVELS == 4)
440 /* Manual setup of the top level page directory */
441 #if (_MI_PAGING_LEVELS == 4)
442 PageFrameIndex
= PFN_FROM_PTE(MiAddressToPte(PXE_BASE
));
443 #elif (_MI_PAGING_LEVELS == 3)
444 PageFrameIndex
= PFN_FROM_PTE(MiAddressToPte(PPE_BASE
));
446 PageFrameIndex
= PFN_FROM_PTE(MiAddressToPte(PDE_BASE
));
448 Pfn
= MiGetPfnEntry(PageFrameIndex
);
449 ASSERT(Pfn
->u3
.e1
.PageLocation
== ActiveAndValid
);
451 Pfn
->u2
.ShareCount
= 1;
452 Pfn
->PteAddress
= NULL
;
453 Pfn
->u3
.e1
.CacheAttribute
= MiNonCached
;
454 Pfn
->u3
.e2
.ReferenceCount
= 1;
455 Pfn
->u4
.PteFrame
= 0;
457 #if (_MI_PAGING_LEVELS == 4)
458 /* Loop all PXEs in the PML4 */
459 PointerPxe
= MiAddressToPxe(Address
);
460 for (i
= 0; i
< PXE_PER_PAGE
; i
++, PointerPxe
++)
462 /* Skip invalid PXEs */
463 if (!PointerPxe
->u
.Hard
.Valid
) continue;
466 PageFrameIndex
= PFN_FROM_PXE(PointerPxe
);
467 MiSetupPfnForPageTable(PageFrameIndex
, PointerPxe
);
469 /* Get starting VA for this PXE */
470 Address
= MiPxeToAddress(PointerPxe
);
472 #if (_MI_PAGING_LEVELS >= 3)
473 /* Loop all PPEs in this PDP */
474 PointerPpe
= MiAddressToPpe(Address
);
475 for (j
= 0; j
< PPE_PER_PAGE
; j
++, PointerPpe
++)
477 /* Skip invalid PPEs */
478 if (!PointerPpe
->u
.Hard
.Valid
) continue;
481 PageFrameIndex
= PFN_FROM_PPE(PointerPpe
);
482 MiSetupPfnForPageTable(PageFrameIndex
, PointerPpe
);
484 /* Get starting VA for this PPE */
485 Address
= MiPpeToAddress(PointerPpe
);
487 /* Loop all PDEs in this PD */
488 PointerPde
= MiAddressToPde(Address
);
489 for (k
= 0; k
< PDE_PER_PAGE
; k
++, PointerPde
++)
491 /* Skip invalid PDEs */
492 if (!PointerPde
->u
.Hard
.Valid
) continue;
495 PageFrameIndex
= PFN_FROM_PDE(PointerPde
);
496 MiSetupPfnForPageTable(PageFrameIndex
, PointerPde
);
498 /* Get starting VA for this PDE */
499 Address
= MiPdeToAddress(PointerPde
);
501 /* Loop all PTEs in this PT */
502 PointerPte
= MiAddressToPte(Address
);
503 for (l
= 0; l
< PTE_PER_PAGE
; l
++, PointerPte
++)
505 /* Skip invalid PTEs */
506 if (!PointerPte
->u
.Hard
.Valid
) continue;
509 PageFrameIndex
= PFN_FROM_PTE(PointerPte
);
510 MiSetupPfnForPageTable(PageFrameIndex
, PointerPte
);
513 #if (_MI_PAGING_LEVELS >= 3)
516 #if (_MI_PAGING_LEVELS == 4)
524 MiAddDescriptorToDatabase(
526 PFN_NUMBER PageCount
,
527 TYPE_OF_MEMORY MemoryType
)
531 ASSERT(!MiIsMemoryTypeInvisible(MemoryType
));
533 /* Check if the memory is free */
534 if (MiIsMemoryTypeFree(MemoryType
))
536 /* Get the last pfn of this descriptor. Note we loop backwards */
537 Pfn
= &MmPfnDatabase
[BasePage
+ PageCount
- 1];
542 /* Add it to the free list */
543 Pfn
->u3
.e1
.CacheAttribute
= MiNonCached
;
544 MiInsertPageInFreeList(BasePage
+ PageCount
);
546 /* Go to the previous page */
550 else if (MemoryType
== LoaderXIPRom
)
552 Pfn
= &MmPfnDatabase
[BasePage
];
555 /* Make it a pseudo-I/O ROM mapping */
558 Pfn
->u2
.ShareCount
= 0;
559 Pfn
->u3
.e1
.PageLocation
= 0;
560 Pfn
->u3
.e1
.CacheAttribute
= MiNonCached
;
562 Pfn
->u3
.e1
.PrototypePte
= 1;
563 Pfn
->u3
.e2
.ReferenceCount
= 0;
564 Pfn
->u4
.InPageError
= 0;
565 Pfn
->u4
.PteFrame
= 0;
571 else if (MemoryType
== LoaderBad
)
578 /* For now skip it */
579 DbgPrint("Skipping BasePage=0x%lx, PageCount=0x%lx, MemoryType=%lx\n",
580 BasePage
, PageCount
, MemoryType
);
581 Pfn
= &MmPfnDatabase
[BasePage
];
584 /* Make an active PFN */
585 Pfn
->u3
.e1
.PageLocation
= ActiveAndValid
;
596 MiBuildPfnDatabase(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
598 PLIST_ENTRY ListEntry
;
599 PMEMORY_ALLOCATION_DESCRIPTOR Descriptor
;
600 PFN_NUMBER BasePage
, PageCount
;
602 /* Map the PDEs and PPEs for the pfn database (ignore holes) */
603 #if (_MI_PAGING_LEVELS >= 3)
604 MiMapPPEs(MmPfnDatabase
, (PUCHAR
)MmPfnDatabase
+ MxPfnAllocation
- 1);
606 MiMapPDEs(MmPfnDatabase
, (PUCHAR
)MmPfnDatabase
+ MxPfnAllocation
- 1);
608 /* First initialize the color tables */
609 MiInitializeColorTables();
611 /* Loop the memory descriptors */
612 for (ListEntry
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
613 ListEntry
!= &LoaderBlock
->MemoryDescriptorListHead
;
614 ListEntry
= ListEntry
->Flink
)
616 /* Get the descriptor */
617 Descriptor
= CONTAINING_RECORD(ListEntry
,
618 MEMORY_ALLOCATION_DESCRIPTOR
,
621 /* Skip invisible memory */
622 if (MiIsMemoryTypeInvisible(Descriptor
->MemoryType
)) continue;
624 /* If this is the free descriptor, use the copy instead */
625 if (Descriptor
== MxFreeDescriptor
) Descriptor
= &MxOldFreeDescriptor
;
627 /* Get the range for this descriptor */
628 BasePage
= Descriptor
->BasePage
;
629 PageCount
= Descriptor
->PageCount
;
631 /* Map the pages for the database */
632 MiMapPTEs(&MmPfnDatabase
[BasePage
],
633 (PUCHAR
)(&MmPfnDatabase
[BasePage
+ PageCount
]) - 1);
635 /* If this was the free descriptor, skip the next step */
636 if (Descriptor
== &MxOldFreeDescriptor
) continue;
638 /* Add this descriptor to the database */
639 MiAddDescriptorToDatabase(BasePage
, PageCount
, Descriptor
->MemoryType
);
642 /* At this point the whole pfn database is mapped. We are about to add the
643 pages from the free descriptor to the database, so from now on we cannot
646 /* Now add the free descriptor */
647 BasePage
= MxFreeDescriptor
->BasePage
;
648 PageCount
= MxFreeDescriptor
->PageCount
;
649 MiAddDescriptorToDatabase(BasePage
, PageCount
, LoaderFree
);
651 /* And finally the memory we used */
652 BasePage
= MxOldFreeDescriptor
.BasePage
;
653 PageCount
= MxFreeDescriptor
->BasePage
- BasePage
;
654 MiAddDescriptorToDatabase(BasePage
, PageCount
, LoaderMemoryData
);
656 /* Reset the descriptor back so we can create the correct memory blocks */
657 *MxFreeDescriptor
= MxOldFreeDescriptor
;
663 MiInitMachineDependent(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
667 ASSERT(MxPfnAllocation
!= 0);
669 /* Set some hardcoded addresses */
670 MmHyperSpaceEnd
= (PVOID
)HYPER_SPACE_END
;
671 MmNonPagedSystemStart
= (PVOID
)MM_SYSTEM_SPACE_START
;
672 MmPfnDatabase
= (PVOID
)MI_PFN_DATABASE
;
673 MmWorkingSetList
= (PVOID
)MI_WORKING_SET_LIST
;
676 // PrototypePte.u.Proto.Valid = 1
677 // PrototypePte.u.ReadOnly
678 // PrototypePte.u.Prototype
679 // PrototypePte.u.Protection = MM_READWRITE;
680 // PrototypePte.u.ProtoAddress
681 PrototypePte
.u
.Soft
.PageFileHigh
= MI_PTE_LOOKUP_NEEDED
;
684 MiInitializePageTable();
686 MiBuildNonPagedPool();
688 MiBuildSystemPteSpace();
690 /* Need to be at DISPATCH_LEVEL for MiInsertPageInFreeList */
691 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
693 /* Map the PFN database pages */
694 MiBuildPfnDatabase(LoaderBlock
);
696 /* Now process the page tables */
697 MiBuildPfnDatabaseFromPageTables();
699 /* PFNs are initialized now! */
700 MiPfnsInitialized
= TRUE
;
702 //KeLowerIrql(OldIrql);
704 /* Need to be at DISPATCH_LEVEL for InitializePool */
705 //KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
707 /* Initialize the nonpaged pool */
708 InitializePool(NonPagedPool
, 0);
710 KeLowerIrql(OldIrql
);
712 /* Initialize the balancer */
713 MmInitializeBalancer((ULONG
)MmAvailablePages
, 0);
715 /* Make sure we have everything we need */
716 ASSERT(MmPfnDatabase
);
717 ASSERT(MmNonPagedSystemStart
);
718 ASSERT(MmNonPagedPoolStart
);
719 ASSERT(MmSizeOfNonPagedPoolInBytes
);
720 ASSERT(MmMaximumNonPagedPoolInBytes
);
721 ASSERT(MmNonPagedPoolExpansionStart
);
722 ASSERT(MmHyperSpaceEnd
);
723 ASSERT(MmNumberOfSystemPtes
);
724 ASSERT(MiAddressToPde(MmNonPagedPoolStart
)->u
.Hard
.Valid
);
725 ASSERT(MiAddressToPte(MmNonPagedPoolStart
)->u
.Hard
.Valid
);
727 return STATUS_SUCCESS
;