2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/i386/page.c
5 * PURPOSE: Low level memory managment manipulation
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
10 /* INCLUDES ***************************************************************/
16 #if defined (ALLOC_PRAGMA)
17 #pragma alloc_text(INIT, MmInitGlobalKernelPageDirectory)
18 #pragma alloc_text(INIT, MiInitPageDirectoryMap)
22 /* GLOBALS *****************************************************************/
24 #define PA_BIT_PRESENT (0)
25 #define PA_BIT_READWRITE (1)
26 #define PA_BIT_USER (2)
29 #define PA_BIT_ACCESSED (5)
30 #define PA_BIT_DIRTY (6)
31 #define PA_BIT_GLOBAL (8)
33 #define PA_PRESENT (1 << PA_BIT_PRESENT)
34 #define PA_READWRITE (1 << PA_BIT_READWRITE)
35 #define PA_USER (1 << PA_BIT_USER)
36 #define PA_DIRTY (1 << PA_BIT_DIRTY)
37 #define PA_WT (1 << PA_BIT_WT)
38 #define PA_CD (1 << PA_BIT_CD)
39 #define PA_ACCESSED (1 << PA_BIT_ACCESSED)
40 #define PA_GLOBAL (1 << PA_BIT_GLOBAL)
42 #define HYPERSPACE (0xc0400000)
43 #define IS_HYPERSPACE(v) (((ULONG)(v) >= HYPERSPACE && (ULONG)(v) < HYPERSPACE + 0x400000))
45 ULONG MmGlobalKernelPageDirectory
[1024];
47 #define PTE_TO_PFN(X) ((X) >> PAGE_SHIFT)
48 #define PFN_TO_PTE(X) ((X) << PAGE_SHIFT)
51 #define PTE_TO_PAGE(X) ((LARGE_INTEGER)(LONGLONG)(PAGE_MASK(X)))
53 __inline LARGE_INTEGER
PTE_TO_PAGE(ULONG npage
)
56 dummy
.QuadPart
= (LONGLONG
)(PAGE_MASK(npage
));
61 /* FUNCTIONS ***************************************************************/
63 BOOLEAN
MmUnmapPageTable(PULONG Pt
);
66 MiFlushTlb(PULONG Pt
, PVOID Address
)
68 if ((Pt
&& MmUnmapPageTable(Pt
)) || Address
>= MmSystemRangeStart
)
70 KeInvalidateTlbEntry(Address
);
75 ProtectToPTE(ULONG flProtect
)
79 if (flProtect
& (PAGE_NOACCESS
|PAGE_GUARD
))
83 else if (flProtect
& PAGE_IS_WRITABLE
)
85 Attributes
= PA_PRESENT
| PA_READWRITE
;
87 else if (flProtect
& (PAGE_IS_READABLE
| PAGE_IS_EXECUTABLE
))
89 Attributes
= PA_PRESENT
;
93 DPRINT1("Unknown main protection type.\n");
94 KeBugCheck(MEMORY_MANAGEMENT
);
97 if (flProtect
& PAGE_SYSTEM
)
102 Attributes
= Attributes
| PA_USER
;
104 if (flProtect
& PAGE_NOCACHE
)
106 Attributes
= Attributes
| PA_CD
;
108 if (flProtect
& PAGE_WRITETHROUGH
)
110 Attributes
= Attributes
| PA_WT
;
117 Mmi386ReleaseMmInfo(PEPROCESS Process
)
119 PUSHORT LdtDescriptor
;
124 DPRINT("Mmi386ReleaseMmInfo(Process %x)\n",Process
);
126 LdtDescriptor
= (PUSHORT
) &Process
->Pcb
.LdtDescriptor
;
127 LdtBase
= LdtDescriptor
[1] |
128 ((LdtDescriptor
[2] & 0xff) << 16) |
129 ((LdtDescriptor
[3] & ~0xff) << 16);
131 DPRINT("LdtBase: %x\n", LdtBase
);
135 ExFreePool((PVOID
) LdtBase
);
138 PageDir
= MmCreateHyperspaceMapping(PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
139 for (i
= 0; i
< ADDR_TO_PDE_OFFSET(MmSystemRangeStart
); i
++)
143 MiZeroPage(PTE_TO_PFN(PageDir
[i
]));
144 MmReleasePageMemoryConsumer(MC_NPPOOL
, PTE_TO_PFN(PageDir
[i
]));
147 MmReleasePageMemoryConsumer(MC_NPPOOL
, PTE_TO_PFN(PageDir
[ADDR_TO_PDE_OFFSET(HYPERSPACE
)]));
148 MmDeleteHyperspaceMapping(PageDir
);
149 MmReleasePageMemoryConsumer(MC_NPPOOL
, PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
151 Process
->Pcb
.DirectoryTableBase
[0] = 0;
152 Process
->Pcb
.DirectoryTableBase
[1] = 0;
154 DPRINT("Finished Mmi386ReleaseMmInfo()\n");
155 return(STATUS_SUCCESS
);
160 MmCreateProcessAddressSpace(IN ULONG MinWs
,
161 IN PEPROCESS Process
,
162 IN PULONG DirectoryTableBase
)
167 PULONG PageDirectory
;
169 DPRINT("MmCopyMmInfo(Src %x, Dest %x)\n", MinWs
, Process
);
171 for (i
= 0; i
< 2; i
++)
173 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
[i
]);
174 if (!NT_SUCCESS(Status
))
176 for (j
= 0; j
< i
; j
++)
178 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
[j
]);
185 PageDirectory
= MmCreateHyperspaceMapping(Pfn
[0]);
187 memcpy(PageDirectory
+ ADDR_TO_PDE_OFFSET(MmSystemRangeStart
),
188 MmGlobalKernelPageDirectory
+ ADDR_TO_PDE_OFFSET(MmSystemRangeStart
),
189 (1024 - ADDR_TO_PDE_OFFSET(MmSystemRangeStart
)) * sizeof(ULONG
));
191 DPRINT("Addr %x\n",ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
));
192 PageDirectory
[ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
)] = PFN_TO_PTE(Pfn
[0]) | PA_PRESENT
| PA_READWRITE
;
193 PageDirectory
[ADDR_TO_PDE_OFFSET(HYPERSPACE
)] = PFN_TO_PTE(Pfn
[1]) | PA_PRESENT
| PA_READWRITE
;
195 MmDeleteHyperspaceMapping(PageDirectory
);
197 DirectoryTableBase
[0] = PFN_TO_PTE(Pfn
[0]);
198 DirectoryTableBase
[1] = 0;
199 DPRINT("Finished MmCopyMmInfo(): 0x%x\n", DirectoryTableBase
[0]);
204 MmGetPageTableForProcess(PEPROCESS Process
, PVOID Address
, BOOLEAN Create
)
206 ULONG PdeOffset
= ADDR_TO_PDE_OFFSET(Address
);
212 if (Address
< MmSystemRangeStart
&& Process
&& Process
!= PsGetCurrentProcess())
214 PageDir
= MmCreateHyperspaceMapping(PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
217 KeBugCheck(MEMORY_MANAGEMENT
);
219 if (0 == InterlockedCompareExchangePte(&PageDir
[PdeOffset
], 0, 0))
223 MmDeleteHyperspaceMapping(PageDir
);
226 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
);
227 if (!NT_SUCCESS(Status
) || Pfn
== 0)
229 KeBugCheck(MEMORY_MANAGEMENT
);
231 Entry
= InterlockedCompareExchangePte(&PageDir
[PdeOffset
], PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
| PA_USER
, 0);
234 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
235 Pfn
= PTE_TO_PFN(Entry
);
240 Pfn
= PTE_TO_PFN(PageDir
[PdeOffset
]);
242 MmDeleteHyperspaceMapping(PageDir
);
243 Pt
= MmCreateHyperspaceMapping(Pfn
);
246 KeBugCheck(MEMORY_MANAGEMENT
);
248 return Pt
+ ADDR_TO_PTE_OFFSET(Address
);
250 PageDir
= (PULONG
)MiAddressToPde(Address
);
251 if (0 == InterlockedCompareExchangePte(PageDir
, 0, 0))
253 if (Address
>= MmSystemRangeStart
)
255 if (0 == InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory
[PdeOffset
], 0, 0))
261 Status
= MmRequestPageMemoryConsumer(MC_SYSTEM
, FALSE
, &Pfn
);
262 if (!NT_SUCCESS(Status
) || Pfn
== 0)
264 KeBugCheck(MEMORY_MANAGEMENT
);
266 Entry
= PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
;
267 if(0 != InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory
[PdeOffset
], Entry
, 0))
269 MmReleasePageMemoryConsumer(MC_SYSTEM
, Pfn
);
271 InterlockedExchangePte(PageDir
, MmGlobalKernelPageDirectory
[PdeOffset
]);
272 RtlZeroMemory(MiPteToAddress(PageDir
), PAGE_SIZE
);
273 return (PULONG
)MiAddressToPte(Address
);
275 InterlockedExchangePte(PageDir
, MmGlobalKernelPageDirectory
[PdeOffset
]);
283 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
);
284 if (!NT_SUCCESS(Status
) || Pfn
== 0)
286 KeBugCheck(MEMORY_MANAGEMENT
);
288 Entry
= InterlockedCompareExchangePte(PageDir
, PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
| PA_USER
, 0);
291 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
295 return (PULONG
)MiAddressToPte(Address
);
298 BOOLEAN
MmUnmapPageTable(PULONG Pt
)
300 if (Pt
>= (PULONG
)PAGETABLE_MAP
&& Pt
< (PULONG
)PAGETABLE_MAP
+ 1024*1024)
307 MmDeleteHyperspaceMapping((PVOID
)PAGE_ROUND_DOWN(Pt
));
312 static ULONG
MmGetPageEntryForProcess(PEPROCESS Process
, PVOID Address
)
317 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
321 MmUnmapPageTable(Pt
);
329 MmGetPfnForProcess(PEPROCESS Process
,
333 Entry
= MmGetPageEntryForProcess(Process
, Address
);
334 if (!(Entry
& PA_PRESENT
))
338 return(PTE_TO_PFN(Entry
));
343 MmDisableVirtualMapping(PEPROCESS Process
, PVOID Address
, BOOLEAN
* WasDirty
, PPFN_NUMBER Page
)
345 * FUNCTION: Delete a virtual mapping
352 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
355 KeBugCheck(MEMORY_MANAGEMENT
);
358 * Atomically disable the present bit and get the old value.
363 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
& ~PA_PRESENT
, Pte
));
365 MiFlushTlb(Pt
, Address
);
366 WasValid
= (PAGE_MASK(Pte
) != 0);
369 KeBugCheck(MEMORY_MANAGEMENT
);
373 * Return some information to the caller
375 if (WasDirty
!= NULL
)
377 *WasDirty
= Pte
& PA_DIRTY
;
381 *Page
= PTE_TO_PFN(Pte
);
387 MmRawDeleteVirtualMapping(PVOID Address
)
391 Pt
= MmGetPageTableForProcess(NULL
, Address
, FALSE
);
395 * Set the entry to zero
397 InterlockedExchangePte(Pt
, 0);
398 MiFlushTlb(Pt
, Address
);
404 MmDeleteVirtualMapping(PEPROCESS Process
, PVOID Address
, BOOLEAN FreePage
,
405 BOOLEAN
* WasDirty
, PPFN_NUMBER Page
)
407 * FUNCTION: Delete a virtual mapping
410 BOOLEAN WasValid
= FALSE
;
415 DPRINT("MmDeleteVirtualMapping(%x, %x, %d, %x, %x)\n",
416 Process
, Address
, FreePage
, WasDirty
, Page
);
418 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
422 if (WasDirty
!= NULL
)
434 * Atomically set the entry to zero and get the old value.
436 Pte
= InterlockedExchangePte(Pt
, 0);
438 MiFlushTlb(Pt
, Address
);
440 WasValid
= (PAGE_MASK(Pte
) != 0);
443 Pfn
= PTE_TO_PFN(Pte
);
450 if (FreePage
&& WasValid
)
452 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
456 * Return some information to the caller
458 if (WasDirty
!= NULL
)
460 *WasDirty
= Pte
& PA_DIRTY
? TRUE
: FALSE
;
470 MmDeletePageFileMapping(PEPROCESS Process
, PVOID Address
,
471 SWAPENTRY
* SwapEntry
)
473 * FUNCTION: Delete a virtual mapping
479 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
488 * Atomically set the entry to zero and get the old value.
490 Pte
= InterlockedExchangePte(Pt
, 0);
492 MiFlushTlb(Pt
, Address
);
495 * Return some information to the caller
497 *SwapEntry
= Pte
>> 1;
501 Mmi386MakeKernelPageTableGlobal(PVOID PAddress
)
504 Pde
= (PULONG
)MiAddressToPde(PAddress
);
507 Pt
= MmGetPageTableForProcess(NULL
, PAddress
, FALSE
);
518 MmIsDirtyPage(PEPROCESS Process
, PVOID Address
)
520 return MmGetPageEntryForProcess(Process
, Address
) & PA_DIRTY
? TRUE
: FALSE
;
525 MmSetCleanPage(PEPROCESS Process
, PVOID Address
)
530 if (Address
< MmSystemRangeStart
&& Process
== NULL
)
532 DPRINT1("MmSetCleanPage is called for user space without a process.\n");
533 KeBugCheck(MEMORY_MANAGEMENT
);
536 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
540 KeBugCheck(MEMORY_MANAGEMENT
);
546 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
& ~PA_DIRTY
, Pte
));
550 MiFlushTlb(Pt
, Address
);
554 MmUnmapPageTable(Pt
);
560 MmSetDirtyPage(PEPROCESS Process
, PVOID Address
)
565 if (Address
< MmSystemRangeStart
&& Process
== NULL
)
567 DPRINT1("MmSetDirtyPage is called for user space without a process.\n");
568 KeBugCheck(MEMORY_MANAGEMENT
);
571 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
574 KeBugCheck(MEMORY_MANAGEMENT
);
580 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
| PA_DIRTY
, Pte
));
581 if (!(Pte
& PA_DIRTY
))
583 MiFlushTlb(Pt
, Address
);
587 MmUnmapPageTable(Pt
);
593 MmEnableVirtualMapping(PEPROCESS Process
, PVOID Address
)
598 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
601 KeBugCheck(MEMORY_MANAGEMENT
);
607 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
| PA_PRESENT
, Pte
));
608 if (!(Pte
& PA_PRESENT
))
610 MiFlushTlb(Pt
, Address
);
614 MmUnmapPageTable(Pt
);
620 MmIsPagePresent(PEPROCESS Process
, PVOID Address
)
622 return MmGetPageEntryForProcess(Process
, Address
) & PA_PRESENT
;
627 MmIsPageSwapEntry(PEPROCESS Process
, PVOID Address
)
630 Entry
= MmGetPageEntryForProcess(Process
, Address
);
631 return !(Entry
& PA_PRESENT
) && (Entry
& 0x800) && Entry
!= 0;
636 MmCreatePageFileMapping(PEPROCESS Process
,
643 if (Process
== NULL
&& Address
< MmSystemRangeStart
)
645 DPRINT1("No process\n");
646 KeBugCheck(MEMORY_MANAGEMENT
);
648 if (Process
!= NULL
&& Address
>= MmSystemRangeStart
)
650 DPRINT1("Setting kernel address with process context\n");
651 KeBugCheck(MEMORY_MANAGEMENT
);
654 if (SwapEntry
& (1 << 31))
656 KeBugCheck(MEMORY_MANAGEMENT
);
659 Pt
= MmGetPageTableForProcess(Process
, Address
, TRUE
);
662 KeBugCheck(MEMORY_MANAGEMENT
);
665 InterlockedExchangePte(Pt
, SwapEntry
<< 1);
668 MiFlushTlb(Pt
, Address
);
672 MmUnmapPageTable(Pt
);
675 return(STATUS_SUCCESS
);
681 MmCreateVirtualMappingUnsafe(PEPROCESS Process
,
690 ULONG oldPdeOffset
, PdeOffset
;
693 BOOLEAN NoExecute
= FALSE
;
695 DPRINT("MmCreateVirtualMappingUnsafe(%x, %x, %x, %x (%x), %d)\n",
696 Process
, Address
, flProtect
, Pages
, *Pages
, PageCount
);
700 if (Address
< MmSystemRangeStart
)
702 DPRINT1("No process\n");
703 KeBugCheck(MEMORY_MANAGEMENT
);
705 if (PageCount
> 0x10000 ||
706 (ULONG_PTR
) Address
/ PAGE_SIZE
+ PageCount
> 0x100000)
708 DPRINT1("Page count too large\n");
709 KeBugCheck(MEMORY_MANAGEMENT
);
714 if (Address
>= MmSystemRangeStart
)
716 DPRINT1("Setting kernel address with process context\n");
717 KeBugCheck(MEMORY_MANAGEMENT
);
719 if (PageCount
> (ULONG_PTR
)MmSystemRangeStart
/ PAGE_SIZE
||
720 (ULONG_PTR
) Address
/ PAGE_SIZE
+ PageCount
>
721 (ULONG_PTR
)MmSystemRangeStart
/ PAGE_SIZE
)
723 DPRINT1("Page Count too large\n");
724 KeBugCheck(MEMORY_MANAGEMENT
);
728 Attributes
= ProtectToPTE(flProtect
);
729 if (Attributes
& 0x80000000)
734 if (Address
>= MmSystemRangeStart
)
736 Attributes
&= ~PA_USER
;
740 Attributes
|= PA_USER
;
744 oldPdeOffset
= ADDR_TO_PDE_OFFSET(Addr
) + 1;
745 for (i
= 0; i
< PageCount
; i
++, Addr
= (PVOID
)((ULONG_PTR
)Addr
+ PAGE_SIZE
))
747 if (!(Attributes
& PA_PRESENT
) && Pages
[i
] != 0)
749 DPRINT1("Setting physical address but not allowing access at address "
750 "0x%.8X with attributes %x/%x.\n",
751 Addr
, Attributes
, flProtect
);
752 KeBugCheck(MEMORY_MANAGEMENT
);
754 PdeOffset
= ADDR_TO_PDE_OFFSET(Addr
);
755 if (oldPdeOffset
!= PdeOffset
)
757 MmUnmapPageTable(Pt
);
758 Pt
= MmGetPageTableForProcess(Process
, Addr
, TRUE
);
761 KeBugCheck(MEMORY_MANAGEMENT
);
768 oldPdeOffset
= PdeOffset
;
771 if (PAGE_MASK(Pte
) != 0 && !(Pte
& PA_PRESENT
) && (Pte
& 0x800))
773 DPRINT1("Bad PTE %lx\n", Pte
);
774 KeBugCheck(MEMORY_MANAGEMENT
);
776 InterlockedExchangePte(Pt
, PFN_TO_PTE(Pages
[i
]) | Attributes
);
779 if (Address
> MmSystemRangeStart
||
780 (Pt
>= (PULONG
)PAGETABLE_MAP
&& Pt
< (PULONG
)PAGETABLE_MAP
+ 1024*1024))
782 MiFlushTlb(Pt
, Address
);
788 MmUnmapPageTable(Pt
);
791 return(STATUS_SUCCESS
);
796 MmCreateVirtualMapping(PEPROCESS Process
,
804 for (i
= 0; i
< PageCount
; i
++)
806 if (!MmIsPageInUse(Pages
[i
]))
808 DPRINT1("Page at address %x not in use\n", PFN_TO_PTE(Pages
[i
]));
809 KeBugCheck(MEMORY_MANAGEMENT
);
813 return(MmCreateVirtualMappingUnsafe(Process
,
822 MmGetPageProtect(PEPROCESS Process
, PVOID Address
)
827 Entry
= MmGetPageEntryForProcess(Process
, Address
);
830 if (!(Entry
& PA_PRESENT
))
832 Protect
= PAGE_NOACCESS
;
836 if (Entry
& PA_READWRITE
)
838 Protect
= PAGE_READWRITE
;
842 Protect
= PAGE_EXECUTE_READ
;
846 Protect
|= PAGE_NOCACHE
;
850 Protect
|= PAGE_WRITETHROUGH
;
852 if (!(Entry
& PA_USER
))
854 Protect
|= PAGE_SYSTEM
;
863 MmSetPageProtect(PEPROCESS Process
, PVOID Address
, ULONG flProtect
)
865 ULONG Attributes
= 0;
866 BOOLEAN NoExecute
= FALSE
;
869 DPRINT("MmSetPageProtect(Process %x Address %x flProtect %x)\n",
870 Process
, Address
, flProtect
);
872 Attributes
= ProtectToPTE(flProtect
);
874 if (Attributes
& 0x80000000)
879 if (Address
>= MmSystemRangeStart
)
881 Attributes
&= ~PA_USER
;
885 Attributes
|= PA_USER
;
888 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
891 KeBugCheck(MEMORY_MANAGEMENT
);
893 InterlockedExchangePte(Pt
, PAGE_MASK(*Pt
) | Attributes
| (*Pt
& (PA_ACCESSED
|PA_DIRTY
)));
894 MiFlushTlb(Pt
, Address
);
900 PHYSICAL_ADDRESS NTAPI
901 MmGetPhysicalAddress(PVOID vaddr
)
903 * FUNCTION: Returns the physical address corresponding to a virtual address
909 DPRINT("MmGetPhysicalAddress(vaddr %x)\n", vaddr
);
910 Pte
= MmGetPageEntryForProcess(NULL
, vaddr
);
911 if (Pte
!= 0 && Pte
& PA_PRESENT
)
913 p
.QuadPart
= PAGE_MASK(Pte
);
914 p
.u
.LowPart
|= (ULONG_PTR
)vaddr
& (PAGE_SIZE
- 1);
925 MmUpdatePageDir(PEPROCESS Process
, PVOID Address
, ULONG Size
)
927 ULONG StartOffset
, EndOffset
, Offset
;
931 // Check if the process isn't there anymore
932 // This is probably a bad sign, since it means the caller is setting cr3 to
935 if ((PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]) == 0) && (Process
!= PsGetCurrentProcess()))
937 DPRINT1("Process: %16s is dead: %p\n", Process
->ImageFileName
, Process
->Pcb
.DirectoryTableBase
[0]);
942 if (Address
< MmSystemRangeStart
)
944 KeBugCheck(MEMORY_MANAGEMENT
);
947 StartOffset
= ADDR_TO_PDE_OFFSET(Address
);
948 EndOffset
= ADDR_TO_PDE_OFFSET((PVOID
)((ULONG_PTR
)Address
+ Size
));
950 if (Process
!= NULL
&& Process
!= PsGetCurrentProcess())
952 Pde
= MmCreateHyperspaceMapping(PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
956 Pde
= (PULONG
)PAGEDIRECTORY_MAP
;
958 for (Offset
= StartOffset
; Offset
<= EndOffset
; Offset
++)
960 if (Offset
!= ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
))
962 InterlockedCompareExchangePte(&Pde
[Offset
], MmGlobalKernelPageDirectory
[Offset
], 0);
965 if (Pde
!= (PULONG
)PAGEDIRECTORY_MAP
)
967 MmDeleteHyperspaceMapping(Pde
);
974 MmInitGlobalKernelPageDirectory(VOID
)
977 PULONG CurrentPageDirectory
= (PULONG
)PAGEDIRECTORY_MAP
;
979 DPRINT("MmInitGlobalKernelPageDirectory()\n");
981 for (i
= ADDR_TO_PDE_OFFSET(MmSystemRangeStart
); i
< 1024; i
++)
983 if (i
!= ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
) &&
984 i
!= ADDR_TO_PDE_OFFSET(HYPERSPACE
) &&
985 0 == MmGlobalKernelPageDirectory
[i
] && 0 != CurrentPageDirectory
[i
])
987 MmGlobalKernelPageDirectory
[i
] = CurrentPageDirectory
[i
];