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 MmInitializeHandBuiltProcess(IN PEPROCESS Process
,
161 IN PULONG DirectoryTableBase
)
163 /* Share the directory base with the idle process */
164 DirectoryTableBase
[0] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[0];
165 DirectoryTableBase
[1] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[1];
167 /* Initialize the Addresss Space */
168 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
169 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
170 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
171 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
173 /* The process now has an address space */
174 Process
->HasAddressSpace
= TRUE
;
175 return STATUS_SUCCESS
;
180 MmCreateProcessAddressSpace(IN ULONG MinWs
,
181 IN PEPROCESS Process
,
182 IN PULONG DirectoryTableBase
)
187 PULONG PageDirectory
;
189 DPRINT("MmCopyMmInfo(Src %x, Dest %x)\n", MinWs
, Process
);
191 for (i
= 0; i
< 2; i
++)
193 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
[i
]);
194 if (!NT_SUCCESS(Status
))
196 for (j
= 0; j
< i
; j
++)
198 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
[j
]);
205 PageDirectory
= MmCreateHyperspaceMapping(Pfn
[0]);
207 memcpy(PageDirectory
+ ADDR_TO_PDE_OFFSET(MmSystemRangeStart
),
208 MmGlobalKernelPageDirectory
+ ADDR_TO_PDE_OFFSET(MmSystemRangeStart
),
209 (1024 - ADDR_TO_PDE_OFFSET(MmSystemRangeStart
)) * sizeof(ULONG
));
211 DPRINT("Addr %x\n",ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
));
212 PageDirectory
[ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
)] = PFN_TO_PTE(Pfn
[0]) | PA_PRESENT
| PA_READWRITE
;
213 PageDirectory
[ADDR_TO_PDE_OFFSET(HYPERSPACE
)] = PFN_TO_PTE(Pfn
[1]) | PA_PRESENT
| PA_READWRITE
;
215 MmDeleteHyperspaceMapping(PageDirectory
);
217 DirectoryTableBase
[0] = PFN_TO_PTE(Pfn
[0]);
218 DirectoryTableBase
[1] = 0;
219 DPRINT("Finished MmCopyMmInfo(): 0x%x\n", DirectoryTableBase
[0]);
224 MmGetPageTableForProcess(PEPROCESS Process
, PVOID Address
, BOOLEAN Create
)
226 ULONG PdeOffset
= ADDR_TO_PDE_OFFSET(Address
);
232 if (Address
< MmSystemRangeStart
&& Process
&& Process
!= PsGetCurrentProcess())
234 PageDir
= MmCreateHyperspaceMapping(PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
237 KeBugCheck(MEMORY_MANAGEMENT
);
239 if (0 == InterlockedCompareExchangePte(&PageDir
[PdeOffset
], 0, 0))
243 MmDeleteHyperspaceMapping(PageDir
);
246 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
);
247 if (!NT_SUCCESS(Status
) || Pfn
== 0)
249 KeBugCheck(MEMORY_MANAGEMENT
);
251 Entry
= InterlockedCompareExchangePte(&PageDir
[PdeOffset
], PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
| PA_USER
, 0);
254 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
255 Pfn
= PTE_TO_PFN(Entry
);
260 Pfn
= PTE_TO_PFN(PageDir
[PdeOffset
]);
262 MmDeleteHyperspaceMapping(PageDir
);
263 Pt
= MmCreateHyperspaceMapping(Pfn
);
266 KeBugCheck(MEMORY_MANAGEMENT
);
268 return Pt
+ ADDR_TO_PTE_OFFSET(Address
);
270 PageDir
= (PULONG
)MiAddressToPde(Address
);
271 if (0 == InterlockedCompareExchangePte(PageDir
, 0, 0))
273 if (Address
>= MmSystemRangeStart
)
275 if (0 == InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory
[PdeOffset
], 0, 0))
281 Status
= MmRequestPageMemoryConsumer(MC_SYSTEM
, FALSE
, &Pfn
);
282 if (!NT_SUCCESS(Status
) || Pfn
== 0)
284 KeBugCheck(MEMORY_MANAGEMENT
);
286 Entry
= PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
;
287 if(0 != InterlockedCompareExchangePte(&MmGlobalKernelPageDirectory
[PdeOffset
], Entry
, 0))
289 MmReleasePageMemoryConsumer(MC_SYSTEM
, Pfn
);
291 InterlockedExchangePte(PageDir
, MmGlobalKernelPageDirectory
[PdeOffset
]);
292 RtlZeroMemory(MiPteToAddress(PageDir
), PAGE_SIZE
);
293 return (PULONG
)MiAddressToPte(Address
);
295 InterlockedExchangePte(PageDir
, MmGlobalKernelPageDirectory
[PdeOffset
]);
303 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, FALSE
, &Pfn
);
304 if (!NT_SUCCESS(Status
) || Pfn
== 0)
306 KeBugCheck(MEMORY_MANAGEMENT
);
308 Entry
= InterlockedCompareExchangePte(PageDir
, PFN_TO_PTE(Pfn
) | PA_PRESENT
| PA_READWRITE
| PA_USER
, 0);
311 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
315 return (PULONG
)MiAddressToPte(Address
);
318 BOOLEAN
MmUnmapPageTable(PULONG Pt
)
320 if (Pt
>= (PULONG
)PAGETABLE_MAP
&& Pt
< (PULONG
)PAGETABLE_MAP
+ 1024*1024)
327 MmDeleteHyperspaceMapping((PVOID
)PAGE_ROUND_DOWN(Pt
));
332 static ULONG
MmGetPageEntryForProcess(PEPROCESS Process
, PVOID Address
)
337 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
341 MmUnmapPageTable(Pt
);
349 MmGetPfnForProcess(PEPROCESS Process
,
353 Entry
= MmGetPageEntryForProcess(Process
, Address
);
354 if (!(Entry
& PA_PRESENT
))
358 return(PTE_TO_PFN(Entry
));
363 MmDisableVirtualMapping(PEPROCESS Process
, PVOID Address
, BOOLEAN
* WasDirty
, PPFN_TYPE Page
)
365 * FUNCTION: Delete a virtual mapping
372 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
375 KeBugCheck(MEMORY_MANAGEMENT
);
378 * Atomically disable the present bit and get the old value.
383 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
& ~PA_PRESENT
, Pte
));
385 MiFlushTlb(Pt
, Address
);
386 WasValid
= (PAGE_MASK(Pte
) != 0);
389 KeBugCheck(MEMORY_MANAGEMENT
);
393 * Return some information to the caller
395 if (WasDirty
!= NULL
)
397 *WasDirty
= Pte
& PA_DIRTY
;
401 *Page
= PTE_TO_PFN(Pte
);
407 MmRawDeleteVirtualMapping(PVOID Address
)
411 Pt
= MmGetPageTableForProcess(NULL
, Address
, FALSE
);
415 * Set the entry to zero
417 InterlockedExchangePte(Pt
, 0);
418 MiFlushTlb(Pt
, Address
);
424 MmDeleteVirtualMapping(PEPROCESS Process
, PVOID Address
, BOOLEAN FreePage
,
425 BOOLEAN
* WasDirty
, PPFN_TYPE Page
)
427 * FUNCTION: Delete a virtual mapping
430 BOOLEAN WasValid
= FALSE
;
435 DPRINT("MmDeleteVirtualMapping(%x, %x, %d, %x, %x)\n",
436 Process
, Address
, FreePage
, WasDirty
, Page
);
438 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
442 if (WasDirty
!= NULL
)
454 * Atomically set the entry to zero and get the old value.
456 Pte
= InterlockedExchangePte(Pt
, 0);
458 MiFlushTlb(Pt
, Address
);
460 WasValid
= (PAGE_MASK(Pte
) != 0);
463 Pfn
= PTE_TO_PFN(Pte
);
470 if (FreePage
&& WasValid
)
472 MmReleasePageMemoryConsumer(MC_NPPOOL
, Pfn
);
476 * Return some information to the caller
478 if (WasDirty
!= NULL
)
480 *WasDirty
= Pte
& PA_DIRTY
? TRUE
: FALSE
;
490 MmDeletePageFileMapping(PEPROCESS Process
, PVOID Address
,
491 SWAPENTRY
* SwapEntry
)
493 * FUNCTION: Delete a virtual mapping
499 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
508 * Atomically set the entry to zero and get the old value.
510 Pte
= InterlockedExchangePte(Pt
, 0);
512 MiFlushTlb(Pt
, Address
);
515 * Return some information to the caller
517 *SwapEntry
= Pte
>> 1;
521 Mmi386MakeKernelPageTableGlobal(PVOID PAddress
)
524 Pde
= (PULONG
)MiAddressToPde(PAddress
);
527 Pt
= MmGetPageTableForProcess(NULL
, PAddress
, FALSE
);
538 MmIsDirtyPage(PEPROCESS Process
, PVOID Address
)
540 return MmGetPageEntryForProcess(Process
, Address
) & PA_DIRTY
? TRUE
: FALSE
;
545 MmSetCleanPage(PEPROCESS Process
, PVOID Address
)
550 if (Address
< MmSystemRangeStart
&& Process
== NULL
)
552 DPRINT1("MmSetCleanPage is called for user space without a process.\n");
553 KeBugCheck(MEMORY_MANAGEMENT
);
556 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
560 KeBugCheck(MEMORY_MANAGEMENT
);
566 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
& ~PA_DIRTY
, Pte
));
570 MiFlushTlb(Pt
, Address
);
574 MmUnmapPageTable(Pt
);
580 MmSetDirtyPage(PEPROCESS Process
, PVOID Address
)
585 if (Address
< MmSystemRangeStart
&& Process
== NULL
)
587 DPRINT1("MmSetDirtyPage is called for user space without a process.\n");
588 KeBugCheck(MEMORY_MANAGEMENT
);
591 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
594 KeBugCheck(MEMORY_MANAGEMENT
);
600 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
| PA_DIRTY
, Pte
));
601 if (!(Pte
& PA_DIRTY
))
603 MiFlushTlb(Pt
, Address
);
607 MmUnmapPageTable(Pt
);
613 MmEnableVirtualMapping(PEPROCESS Process
, PVOID Address
)
618 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
621 KeBugCheck(MEMORY_MANAGEMENT
);
627 } while (Pte
!= InterlockedCompareExchangePte(Pt
, Pte
| PA_PRESENT
, Pte
));
628 if (!(Pte
& PA_PRESENT
))
630 MiFlushTlb(Pt
, Address
);
634 MmUnmapPageTable(Pt
);
640 MmIsPagePresent(PEPROCESS Process
, PVOID Address
)
642 return MmGetPageEntryForProcess(Process
, Address
) & PA_PRESENT
;
647 MmIsPageSwapEntry(PEPROCESS Process
, PVOID Address
)
650 Entry
= MmGetPageEntryForProcess(Process
, Address
);
651 return !(Entry
& PA_PRESENT
) && (Entry
& 0x800) && Entry
!= 0;
656 MmCreatePageFileMapping(PEPROCESS Process
,
663 if (Process
== NULL
&& Address
< MmSystemRangeStart
)
665 DPRINT1("No process\n");
666 KeBugCheck(MEMORY_MANAGEMENT
);
668 if (Process
!= NULL
&& Address
>= MmSystemRangeStart
)
670 DPRINT1("Setting kernel address with process context\n");
671 KeBugCheck(MEMORY_MANAGEMENT
);
674 if (SwapEntry
& (1 << 31))
676 KeBugCheck(MEMORY_MANAGEMENT
);
679 Pt
= MmGetPageTableForProcess(Process
, Address
, TRUE
);
682 KeBugCheck(MEMORY_MANAGEMENT
);
685 InterlockedExchangePte(Pt
, SwapEntry
<< 1);
688 MiFlushTlb(Pt
, Address
);
692 MmUnmapPageTable(Pt
);
695 return(STATUS_SUCCESS
);
701 MmCreateVirtualMappingUnsafe(PEPROCESS Process
,
710 ULONG oldPdeOffset
, PdeOffset
;
713 BOOLEAN NoExecute
= FALSE
;
715 DPRINT("MmCreateVirtualMappingUnsafe(%x, %x, %x, %x (%x), %d)\n",
716 Process
, Address
, flProtect
, Pages
, *Pages
, PageCount
);
720 if (Address
< MmSystemRangeStart
)
722 DPRINT1("No process\n");
723 KeBugCheck(MEMORY_MANAGEMENT
);
725 if (PageCount
> 0x10000 ||
726 (ULONG_PTR
) Address
/ PAGE_SIZE
+ PageCount
> 0x100000)
728 DPRINT1("Page count too large\n");
729 KeBugCheck(MEMORY_MANAGEMENT
);
734 if (Address
>= MmSystemRangeStart
)
736 DPRINT1("Setting kernel address with process context\n");
737 KeBugCheck(MEMORY_MANAGEMENT
);
739 if (PageCount
> (ULONG_PTR
)MmSystemRangeStart
/ PAGE_SIZE
||
740 (ULONG_PTR
) Address
/ PAGE_SIZE
+ PageCount
>
741 (ULONG_PTR
)MmSystemRangeStart
/ PAGE_SIZE
)
743 DPRINT1("Page Count too large\n");
744 KeBugCheck(MEMORY_MANAGEMENT
);
748 Attributes
= ProtectToPTE(flProtect
);
749 if (Attributes
& 0x80000000)
754 if (Address
>= MmSystemRangeStart
)
756 Attributes
&= ~PA_USER
;
760 Attributes
|= PA_USER
;
764 oldPdeOffset
= ADDR_TO_PDE_OFFSET(Addr
) + 1;
765 for (i
= 0; i
< PageCount
; i
++, Addr
= (PVOID
)((ULONG_PTR
)Addr
+ PAGE_SIZE
))
767 if (!(Attributes
& PA_PRESENT
) && Pages
[i
] != 0)
769 DPRINT1("Setting physical address but not allowing access at address "
770 "0x%.8X with attributes %x/%x.\n",
771 Addr
, Attributes
, flProtect
);
772 KeBugCheck(MEMORY_MANAGEMENT
);
774 PdeOffset
= ADDR_TO_PDE_OFFSET(Addr
);
775 if (oldPdeOffset
!= PdeOffset
)
777 MmUnmapPageTable(Pt
);
778 Pt
= MmGetPageTableForProcess(Process
, Addr
, TRUE
);
781 KeBugCheck(MEMORY_MANAGEMENT
);
788 oldPdeOffset
= PdeOffset
;
791 if (PAGE_MASK(Pte
) != 0 && !(Pte
& PA_PRESENT
) && (Pte
& 0x800))
793 DPRINT1("Bad PTE %lx\n", Pte
);
794 KeBugCheck(MEMORY_MANAGEMENT
);
796 InterlockedExchangePte(Pt
, PFN_TO_PTE(Pages
[i
]) | Attributes
);
799 if (Address
> MmSystemRangeStart
||
800 (Pt
>= (PULONG
)PAGETABLE_MAP
&& Pt
< (PULONG
)PAGETABLE_MAP
+ 1024*1024))
802 MiFlushTlb(Pt
, Address
);
808 MmUnmapPageTable(Pt
);
811 return(STATUS_SUCCESS
);
816 MmCreateVirtualMapping(PEPROCESS Process
,
824 for (i
= 0; i
< PageCount
; i
++)
826 if (!MmIsPageInUse(Pages
[i
]))
828 DPRINT1("Page at address %x not in use\n", PFN_TO_PTE(Pages
[i
]));
829 KeBugCheck(MEMORY_MANAGEMENT
);
833 return(MmCreateVirtualMappingUnsafe(Process
,
842 MmGetPageProtect(PEPROCESS Process
, PVOID Address
)
847 Entry
= MmGetPageEntryForProcess(Process
, Address
);
850 if (!(Entry
& PA_PRESENT
))
852 Protect
= PAGE_NOACCESS
;
856 if (Entry
& PA_READWRITE
)
858 Protect
= PAGE_READWRITE
;
862 Protect
= PAGE_EXECUTE_READ
;
866 Protect
|= PAGE_NOCACHE
;
870 Protect
|= PAGE_WRITETHROUGH
;
872 if (!(Entry
& PA_USER
))
874 Protect
|= PAGE_SYSTEM
;
883 MmSetPageProtect(PEPROCESS Process
, PVOID Address
, ULONG flProtect
)
885 ULONG Attributes
= 0;
886 BOOLEAN NoExecute
= FALSE
;
889 DPRINT("MmSetPageProtect(Process %x Address %x flProtect %x)\n",
890 Process
, Address
, flProtect
);
892 Attributes
= ProtectToPTE(flProtect
);
894 if (Attributes
& 0x80000000)
899 if (Address
>= MmSystemRangeStart
)
901 Attributes
&= ~PA_USER
;
905 Attributes
|= PA_USER
;
908 Pt
= MmGetPageTableForProcess(Process
, Address
, FALSE
);
911 KeBugCheck(MEMORY_MANAGEMENT
);
913 InterlockedExchangePte(Pt
, PAGE_MASK(*Pt
) | Attributes
| (*Pt
& (PA_ACCESSED
|PA_DIRTY
)));
914 MiFlushTlb(Pt
, Address
);
920 PHYSICAL_ADDRESS NTAPI
921 MmGetPhysicalAddress(PVOID vaddr
)
923 * FUNCTION: Returns the physical address corresponding to a virtual address
929 DPRINT("MmGetPhysicalAddress(vaddr %x)\n", vaddr
);
930 Pte
= MmGetPageEntryForProcess(NULL
, vaddr
);
931 if (Pte
!= 0 && Pte
& PA_PRESENT
)
933 p
.QuadPart
= PAGE_MASK(Pte
);
934 p
.u
.LowPart
|= (ULONG_PTR
)vaddr
& (PAGE_SIZE
- 1);
945 MmUpdatePageDir(PEPROCESS Process
, PVOID Address
, ULONG Size
)
947 ULONG StartOffset
, EndOffset
, Offset
;
951 // Check if the process isn't there anymore
952 // This is probably a bad sign, since it means the caller is setting cr3 to
955 if ((PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]) == 0) && (Process
!= PsGetCurrentProcess()))
957 DPRINT1("Process: %16s is dead: %p\n", Process
->ImageFileName
, Process
->Pcb
.DirectoryTableBase
[0]);
962 if (Address
< MmSystemRangeStart
)
964 KeBugCheck(MEMORY_MANAGEMENT
);
967 StartOffset
= ADDR_TO_PDE_OFFSET(Address
);
968 EndOffset
= ADDR_TO_PDE_OFFSET((PVOID
)((ULONG_PTR
)Address
+ Size
));
970 if (Process
!= NULL
&& Process
!= PsGetCurrentProcess())
972 Pde
= MmCreateHyperspaceMapping(PTE_TO_PFN(Process
->Pcb
.DirectoryTableBase
[0]));
976 Pde
= (PULONG
)PAGEDIRECTORY_MAP
;
978 for (Offset
= StartOffset
; Offset
<= EndOffset
; Offset
++)
980 if (Offset
!= ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
))
982 InterlockedCompareExchangePte(&Pde
[Offset
], MmGlobalKernelPageDirectory
[Offset
], 0);
985 if (Pde
!= (PULONG
)PAGEDIRECTORY_MAP
)
987 MmDeleteHyperspaceMapping(Pde
);
994 MmInitGlobalKernelPageDirectory(VOID
)
997 PULONG CurrentPageDirectory
= (PULONG
)PAGEDIRECTORY_MAP
;
999 DPRINT("MmInitGlobalKernelPageDirectory()\n");
1001 for (i
= ADDR_TO_PDE_OFFSET(MmSystemRangeStart
); i
< 1024; i
++)
1003 if (i
!= ADDR_TO_PDE_OFFSET(PAGETABLE_MAP
) &&
1004 i
!= ADDR_TO_PDE_OFFSET(HYPERSPACE
) &&
1005 0 == MmGlobalKernelPageDirectory
[i
] && 0 != CurrentPageDirectory
[i
])
1007 MmGlobalKernelPageDirectory
[i
] = CurrentPageDirectory
[i
];