2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/procsup.c
5 * PURPOSE: ARM Memory Manager Process Related Management
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #define MODULE_INVOLVED_IN_ARM3
16 #include <mm/ARM3/miarm.h>
18 /* GLOBALS ********************************************************************/
20 ULONG MmProcessColorSeed
= 0x12345678;
21 PMMWSL MmWorkingSetList
;
22 ULONG MmMaximumDeadKernelStacks
= 5;
23 SLIST_HEADER MmDeadStackSListHead
;
25 /* PRIVATE FUNCTIONS **********************************************************/
29 MiCreatePebOrTeb(IN PEPROCESS Process
,
31 OUT PULONG_PTR BaseAddress
)
35 ULONG_PTR HighestAddress
, RandomBase
;
37 LARGE_INTEGER CurrentTime
;
40 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD_LONG
), 'ldaV');
41 if (!Vad
) return STATUS_NO_MEMORY
;
43 /* Setup the primary flags with the size, and make it commited, private, RW */
45 Vad
->u
.VadFlags
.CommitCharge
= BYTES_TO_PAGES(Size
);
46 Vad
->u
.VadFlags
.MemCommit
= TRUE
;
47 Vad
->u
.VadFlags
.PrivateMemory
= TRUE
;
48 Vad
->u
.VadFlags
.Protection
= MM_READWRITE
;
49 Vad
->u
.VadFlags
.NoChange
= TRUE
;
50 Vad
->u1
.Parent
= NULL
;
52 /* Setup the secondary flags to make it a secured, writable, long VAD */
53 Vad
->u2
.LongFlags2
= 0;
54 Vad
->u2
.VadFlags2
.OneSecured
= TRUE
;
55 Vad
->u2
.VadFlags2
.LongVad
= TRUE
;
56 Vad
->u2
.VadFlags2
.ReadOnly
= FALSE
;
58 Vad
->ControlArea
= NULL
; // For Memory-Area hack
59 Vad
->FirstPrototypePte
= NULL
;
61 /* Check if this is a PEB creation */
62 ASSERT(sizeof(TEB
) != sizeof(PEB
));
63 if (Size
== sizeof(PEB
))
65 /* Create a random value to select one page in a 64k region */
66 KeQueryTickCount(&CurrentTime
);
67 CurrentTime
.LowPart
&= (_64K
/ PAGE_SIZE
) - 1;
69 /* Calculate a random base address */
70 RandomBase
= (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1;
71 RandomBase
-= CurrentTime
.LowPart
<< PAGE_SHIFT
;
73 /* Make sure the base address is not too high */
74 AlignedSize
= ROUND_TO_PAGES(Size
);
75 if ((RandomBase
+ AlignedSize
) > (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1)
77 RandomBase
= (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1 - AlignedSize
;
80 /* Calculate the highest allowed address */
81 HighestAddress
= RandomBase
+ AlignedSize
- 1;
85 HighestAddress
= (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
;
89 Status
= MiInsertVadEx((PMMVAD
)Vad
,
95 if (!NT_SUCCESS(Status
))
97 ExFreePoolWithTag(Vad
, 'ldaV');
98 return STATUS_NO_MEMORY
;
102 return STATUS_SUCCESS
;
107 MmDeleteTeb(IN PEPROCESS Process
,
111 PETHREAD Thread
= PsGetCurrentThread();
113 PMM_AVL_TABLE VadTree
= &Process
->VadRoot
;
114 DPRINT("Deleting TEB: %p in %16s\n", Teb
, Process
->ImageFileName
);
116 /* TEB is one page */
117 TebEnd
= (ULONG_PTR
)Teb
+ ROUND_TO_PAGES(sizeof(TEB
)) - 1;
119 /* Attach to the process */
120 KeAttachProcess(&Process
->Pcb
);
122 /* Lock the process address space */
123 KeAcquireGuardedMutex(&Process
->AddressCreationLock
);
125 /* Find the VAD, make sure it's a TEB VAD */
126 Vad
= MiLocateAddress(Teb
);
127 DPRINT("Removing node for VAD: %lx %lx\n", Vad
->StartingVpn
, Vad
->EndingVpn
);
129 if (Vad
->StartingVpn
!= ((ULONG_PTR
)Teb
>> PAGE_SHIFT
))
131 /* Bug in the AVL code? */
132 DPRINT1("Corrupted VAD!\n");
136 /* Sanity checks for a valid TEB VAD */
137 ASSERT((Vad
->StartingVpn
== ((ULONG_PTR
)Teb
>> PAGE_SHIFT
) &&
138 (Vad
->EndingVpn
== (TebEnd
>> PAGE_SHIFT
))));
139 ASSERT(Vad
->u
.VadFlags
.NoChange
== TRUE
);
140 ASSERT(Vad
->u2
.VadFlags2
.OneSecured
== TRUE
);
141 ASSERT(Vad
->u2
.VadFlags2
.MultipleSecured
== FALSE
);
143 /* Lock the working set */
144 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
146 /* Remove this VAD from the tree */
147 ASSERT(VadTree
->NumberGenericTableElements
>= 1);
148 MiRemoveNode((PMMADDRESS_NODE
)Vad
, VadTree
);
150 /* Delete the pages */
151 MiDeleteVirtualAddresses((ULONG_PTR
)Teb
, TebEnd
, NULL
);
153 /* Release the working set */
154 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
160 /* Release the address space lock */
161 KeReleaseGuardedMutex(&Process
->AddressCreationLock
);
169 MmDeleteKernelStack(IN PVOID StackBase
,
173 PFN_NUMBER PageFrameNumber
, PageTableFrameNumber
;
174 PFN_COUNT StackPages
;
178 PSLIST_ENTRY SListEntry
;
181 // This should be the guard page, so decrement by one
183 PointerPte
= MiAddressToPte(StackBase
);
187 // If this is a small stack, just push the stack onto the dead stack S-LIST
191 if (ExQueryDepthSList(&MmDeadStackSListHead
) < MmMaximumDeadKernelStacks
)
193 SListEntry
= ((PSLIST_ENTRY
)StackBase
) - 1;
194 InterlockedPushEntrySList(&MmDeadStackSListHead
, SListEntry
);
200 // Calculate pages used
202 StackPages
= BYTES_TO_PAGES(GuiStack
?
203 MmLargeStackSize
: KERNEL_STACK_SIZE
);
205 /* Acquire the PFN lock */
206 OldIrql
= MiAcquirePfnLock();
211 for (i
= 0; i
< StackPages
; i
++)
214 // Check if this is a valid PTE
216 if (PointerPte
->u
.Hard
.Valid
== 1)
218 /* Get the PTE's page */
219 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
220 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
222 /* Now get the page of the page table mapping it */
223 PageTableFrameNumber
= Pfn1
->u4
.PteFrame
;
224 Pfn2
= MiGetPfnEntry(PageTableFrameNumber
);
226 /* Remove a shared reference, since the page is going away */
227 MiDecrementShareCount(Pfn2
, PageTableFrameNumber
);
229 /* Set the special pending delete marker */
230 MI_SET_PFN_DELETED(Pfn1
);
232 /* And now delete the actual stack page */
233 MiDecrementShareCount(Pfn1
, PageFrameNumber
);
243 // We should be at the guard page now
245 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
247 /* Release the PFN lock */
248 MiReleasePfnLock(OldIrql
);
253 MiReleaseSystemPtes(PointerPte
, StackPages
+ 1, SystemPteSpace
);
258 MmCreateKernelStack(IN BOOLEAN GuiStack
,
261 PFN_COUNT StackPtes
, StackPages
;
262 PMMPTE PointerPte
, StackPte
;
264 MMPTE TempPte
, InvalidPte
;
266 PFN_NUMBER PageFrameIndex
;
268 PSLIST_ENTRY SListEntry
;
271 // Calculate pages needed
276 // We'll allocate 64KB stack, but only commit 12K
278 StackPtes
= BYTES_TO_PAGES(MmLargeStackSize
);
279 StackPages
= BYTES_TO_PAGES(KERNEL_LARGE_STACK_COMMIT
);
284 // If the dead stack S-LIST has a stack on it, use it instead of allocating
285 // new system PTEs for this stack
287 if (ExQueryDepthSList(&MmDeadStackSListHead
))
289 SListEntry
= InterlockedPopEntrySList(&MmDeadStackSListHead
);
290 if (SListEntry
!= NULL
)
292 BaseAddress
= (SListEntry
+ 1);
298 // We'll allocate 12K and that's it
300 StackPtes
= BYTES_TO_PAGES(KERNEL_STACK_SIZE
);
301 StackPages
= StackPtes
;
305 // Reserve stack pages, plus a guard page
307 StackPte
= MiReserveSystemPtes(StackPtes
+ 1, SystemPteSpace
);
308 if (!StackPte
) return NULL
;
311 // Get the stack address
313 BaseAddress
= MiPteToAddress(StackPte
+ StackPtes
+ 1);
316 // Select the right PTE address where we actually start committing pages
318 PointerPte
= StackPte
;
319 if (GuiStack
) PointerPte
+= BYTES_TO_PAGES(MmLargeStackSize
-
320 KERNEL_LARGE_STACK_COMMIT
);
323 /* Setup the temporary invalid PTE */
324 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
326 /* Setup the template stack PTE */
327 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, PointerPte
+ 1, MM_READWRITE
, 0);
330 // Acquire the PFN DB lock
332 OldIrql
= MiAcquirePfnLock();
335 // Loop each stack page
337 for (i
= 0; i
< StackPages
; i
++)
344 /* Get a page and write the current invalid PTE */
345 MI_SET_USAGE(MI_USAGE_KERNEL_STACK
);
346 MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName
);
347 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
348 MI_WRITE_INVALID_PTE(PointerPte
, InvalidPte
);
350 /* Initialize the PFN entry for this page */
351 MiInitializePfn(PageFrameIndex
, PointerPte
, 1);
353 /* Write the valid PTE */
354 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameIndex
;
355 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
359 // Release the PFN lock
361 MiReleasePfnLock(OldIrql
);
364 // Return the stack address
371 MmGrowKernelStackEx(IN PVOID StackPointer
,
374 PKTHREAD Thread
= KeGetCurrentThread();
375 PMMPTE LimitPte
, NewLimitPte
, LastPte
;
377 MMPTE TempPte
, InvalidPte
;
378 PFN_NUMBER PageFrameIndex
;
381 // Make sure the stack did not overflow
383 ASSERT(((ULONG_PTR
)Thread
->StackBase
- (ULONG_PTR
)Thread
->StackLimit
) <=
384 (MmLargeStackSize
+ PAGE_SIZE
));
387 // Get the current stack limit
389 LimitPte
= MiAddressToPte(Thread
->StackLimit
);
390 ASSERT(LimitPte
->u
.Hard
.Valid
== 1);
393 // Get the new one and make sure this isn't a retarded request
395 NewLimitPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)StackPointer
- GrowSize
));
396 if (NewLimitPte
== LimitPte
) return STATUS_SUCCESS
;
399 // Now make sure you're not going past the reserved space
401 LastPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)Thread
->StackBase
-
403 if (NewLimitPte
< LastPte
)
408 DPRINT1("Thread wants too much stack\n");
409 return STATUS_STACK_OVERFLOW
;
413 // Calculate the number of new pages
417 /* Setup the temporary invalid PTE */
418 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
421 // Acquire the PFN DB lock
423 OldIrql
= MiAcquirePfnLock();
426 // Loop each stack page
428 while (LimitPte
>= NewLimitPte
)
430 /* Get a page and write the current invalid PTE */
431 MI_SET_USAGE(MI_USAGE_KERNEL_STACK_EXPANSION
);
432 MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName
);
433 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
434 MI_WRITE_INVALID_PTE(LimitPte
, InvalidPte
);
436 /* Initialize the PFN entry for this page */
437 MiInitializePfn(PageFrameIndex
, LimitPte
, 1);
439 /* Setup the template stack PTE */
440 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, LimitPte
, MM_READWRITE
, PageFrameIndex
);
442 /* Write the valid PTE */
443 MI_WRITE_VALID_PTE(LimitPte
--, TempPte
);
447 // Release the PFN lock
449 MiReleasePfnLock(OldIrql
);
454 Thread
->StackLimit
= (ULONG_PTR
)MiPteToAddress(NewLimitPte
);
455 return STATUS_SUCCESS
;
460 MmGrowKernelStack(IN PVOID StackPointer
)
463 // Call the extended version
465 return MmGrowKernelStackEx(StackPointer
, KERNEL_LARGE_STACK_COMMIT
);
470 MmSetMemoryPriorityProcess(IN PEPROCESS Process
,
471 IN UCHAR MemoryPriority
)
476 // Check if we have less then 16MB of Physical Memory
478 if ((MmSystemSize
== MmSmallSystem
) &&
479 (MmNumberOfPhysicalPages
< ((15 * 1024 * 1024) / PAGE_SIZE
)))
482 // Always use background priority
484 MemoryPriority
= MEMORY_PRIORITY_BACKGROUND
;
488 // Save the old priority and update it
490 OldPriority
= (UCHAR
)Process
->Vm
.Flags
.MemoryPriority
;
491 Process
->Vm
.Flags
.MemoryPriority
= MemoryPriority
;
494 // Return the old priority
501 MmCreatePeb(IN PEPROCESS Process
,
502 IN PINITIAL_PEB InitialPeb
,
506 LARGE_INTEGER SectionOffset
;
508 PVOID TableBase
= NULL
;
509 PIMAGE_NT_HEADERS NtHeaders
;
510 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
512 USHORT Characteristics
;
513 KAFFINITY ProcessAffinityMask
= 0;
514 SectionOffset
.QuadPart
= (ULONGLONG
)0;
520 KeAttachProcess(&Process
->Pcb
);
525 Status
= MmMapViewOfSection(ExpNlsSectionPointer
,
535 DPRINT("NLS Tables at: %p\n", TableBase
);
536 if (!NT_SUCCESS(Status
))
538 /* Cleanup and exit */
546 Status
= MiCreatePebOrTeb(Process
, sizeof(PEB
), (PULONG_PTR
)&Peb
);
547 DPRINT("PEB at: %p\n", Peb
);
548 if (!NT_SUCCESS(Status
))
550 /* Cleanup and exit */
556 // Use SEH in case we can't load the PEB
561 // Initialize the PEB
563 RtlZeroMemory(Peb
, sizeof(PEB
));
568 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
569 Peb
->InheritedAddressSpace
= InitialPeb
->InheritedAddressSpace
;
570 Peb
->Mutant
= InitialPeb
->Mutant
;
571 Peb
->ImageUsesLargePages
= InitialPeb
->ImageUsesLargePages
;
576 Peb
->AnsiCodePageData
= (PCHAR
)TableBase
+ ExpAnsiCodePageDataOffset
;
577 Peb
->OemCodePageData
= (PCHAR
)TableBase
+ ExpOemCodePageDataOffset
;
578 Peb
->UnicodeCaseTableData
= (PCHAR
)TableBase
+ ExpUnicodeCaseTableDataOffset
;
581 // Default Version Data (could get changed below)
583 Peb
->OSMajorVersion
= NtMajorVersion
;
584 Peb
->OSMinorVersion
= NtMinorVersion
;
585 Peb
->OSBuildNumber
= (USHORT
)(NtBuildNumber
& 0x3FFF);
586 Peb
->OSPlatformId
= VER_PLATFORM_WIN32_NT
;
587 Peb
->OSCSDVersion
= (USHORT
)CmNtCSDVersion
;
590 // Heap and Debug Data
592 Peb
->NumberOfProcessors
= KeNumberProcessors
;
593 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
);
594 Peb
->NtGlobalFlag
= NtGlobalFlag
;
595 Peb
->HeapSegmentReserve
= MmHeapSegmentReserve
;
596 Peb
->HeapSegmentCommit
= MmHeapSegmentCommit
;
597 Peb
->HeapDeCommitTotalFreeThreshold
= MmHeapDeCommitTotalFreeThreshold
;
598 Peb
->HeapDeCommitFreeBlockThreshold
= MmHeapDeCommitFreeBlockThreshold
;
599 Peb
->CriticalSectionTimeout
= MmCriticalSectionTimeout
;
600 Peb
->MinimumStackCommit
= MmMinimumStackCommitInBytes
;
601 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
602 Peb
->ProcessHeaps
= (PVOID
*)(Peb
+ 1);
607 if (Process
->Session
) Peb
->SessionId
= MmGetSessionId(Process
);
609 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
615 _SEH2_YIELD(return _SEH2_GetExceptionCode());
620 // Use SEH in case we can't load the image
627 NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
);
628 Characteristics
= NtHeaders
->FileHeader
.Characteristics
;
630 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
636 _SEH2_YIELD(return STATUS_INVALID_IMAGE_PROTECT
);
646 // Use SEH in case we can't load the headers
651 // Get the Image Config Data too
653 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
655 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
662 ProbeForRead(ImageConfigData
,
663 sizeof(IMAGE_LOAD_CONFIG_DIRECTORY
),
668 // Write subsystem data
670 Peb
->ImageSubsystem
= NtHeaders
->OptionalHeader
.Subsystem
;
671 Peb
->ImageSubsystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
672 Peb
->ImageSubsystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
675 // Check for version data
677 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
680 // Extract values and write them
682 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
683 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
684 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
685 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
687 /* Process CSD version override */
688 if ((ImageConfigData
) && (ImageConfigData
->CSDVersion
))
690 /* Take the value from the image configuration directory */
691 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
695 /* Process optional process affinity mask override */
696 if ((ImageConfigData
) && (ImageConfigData
->ProcessAffinityMask
))
698 /* Take the value from the image configuration directory */
699 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
703 // Check if this is a UP image
704 if (Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
707 // Force it to use CPU 0
709 /* FIXME: this should use the MmRotatingUniprocessorNumber */
710 Peb
->ImageProcessAffinityMask
= 0;
715 // Whatever was configured
717 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
720 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
726 _SEH2_YIELD(return STATUS_INVALID_IMAGE_PROTECT
);
732 // Detach from the Process
736 return STATUS_SUCCESS
;
741 MmCreateTeb(IN PEPROCESS Process
,
742 IN PCLIENT_ID ClientId
,
743 IN PINITIAL_TEB InitialTeb
,
747 NTSTATUS Status
= STATUS_SUCCESS
;
753 KeAttachProcess(&Process
->Pcb
);
758 Status
= MiCreatePebOrTeb(Process
, sizeof(TEB
), (PULONG_PTR
)&Teb
);
759 if (!NT_SUCCESS(Status
))
761 /* Cleanup and exit */
767 // Use SEH in case we can't load the TEB
772 // Initialize the PEB
774 RtlZeroMemory(Teb
, sizeof(TEB
));
780 Teb
->NtTib
.ExceptionList
= NULL
;
782 Teb
->NtTib
.ExceptionList
= EXCEPTION_CHAIN_END
;
784 Teb
->NtTib
.Self
= (PNT_TIB
)Teb
;
787 // Identify this as an OS/2 V3.0 ("Cruiser") TIB
789 Teb
->NtTib
.Version
= 30 << 8;
794 Teb
->ClientId
= *ClientId
;
795 Teb
->RealClientId
= *ClientId
;
796 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
797 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
800 // Check if we have a grandparent TEB
802 if ((InitialTeb
->PreviousStackBase
== NULL
) &&
803 (InitialTeb
->PreviousStackLimit
== NULL
))
806 // Use initial TEB values
808 Teb
->NtTib
.StackBase
= InitialTeb
->StackBase
;
809 Teb
->NtTib
.StackLimit
= InitialTeb
->StackLimit
;
810 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
815 // Use grandparent TEB values
817 Teb
->NtTib
.StackBase
= InitialTeb
->PreviousStackBase
;
818 Teb
->NtTib
.StackLimit
= InitialTeb
->PreviousStackLimit
;
822 // Initialize the static unicode string
824 Teb
->StaticUnicodeString
.MaximumLength
= sizeof(Teb
->StaticUnicodeBuffer
);
825 Teb
->StaticUnicodeString
.Buffer
= Teb
->StaticUnicodeBuffer
;
827 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
832 Status
= _SEH2_GetExceptionCode();
846 MiInitializeWorkingSetList(IN PEPROCESS CurrentProcess
)
852 /* Setup some bogus list data */
853 MmWorkingSetList
->LastEntry
= CurrentProcess
->Vm
.MinimumWorkingSetSize
;
854 MmWorkingSetList
->HashTable
= NULL
;
855 MmWorkingSetList
->HashTableSize
= 0;
856 MmWorkingSetList
->NumberOfImageWaiters
= 0;
857 MmWorkingSetList
->Wsle
= (PVOID
)(ULONG_PTR
)0xDEADBABEDEADBABEULL
;
858 MmWorkingSetList
->VadBitMapHint
= 1;
859 MmWorkingSetList
->HashTableStart
= (PVOID
)(ULONG_PTR
)0xBADAB00BBADAB00BULL
;
860 MmWorkingSetList
->HighestPermittedHashAddress
= (PVOID
)(ULONG_PTR
)0xCAFEBABECAFEBABEULL
;
861 MmWorkingSetList
->FirstFree
= 1;
862 MmWorkingSetList
->FirstDynamic
= 2;
863 MmWorkingSetList
->NextSlot
= 3;
864 MmWorkingSetList
->LastInitializedWsle
= 4;
866 /* The rule is that the owner process is always in the FLINK of the PDE's PFN entry */
867 Pfn1
= MiGetPfnEntry(CurrentProcess
->Pcb
.DirectoryTableBase
[0] >> PAGE_SHIFT
);
868 ASSERT(Pfn1
->u4
.PteFrame
== MiGetPfnEntryIndex(Pfn1
));
869 Pfn1
->u1
.Event
= (PKEVENT
)CurrentProcess
;
871 /* Map the process working set in kernel space */
872 sysPte
= MiReserveSystemPtes(1, SystemPteSpace
);
873 MI_MAKE_HARDWARE_PTE_KERNEL(&tempPte
, sysPte
, MM_READWRITE
, CurrentProcess
->WorkingSetPage
);
874 MI_WRITE_VALID_PTE(sysPte
, tempPte
);
875 CurrentProcess
->Vm
.VmWorkingSetList
= MiPteToAddress(sysPte
);
880 MmInitializeProcessAddressSpace(IN PEPROCESS Process
,
881 IN PEPROCESS ProcessClone OPTIONAL
,
882 IN PVOID Section OPTIONAL
,
884 IN POBJECT_NAME_INFORMATION
*AuditName OPTIONAL
)
886 NTSTATUS Status
= STATUS_SUCCESS
;
889 PROS_SECTION_OBJECT SectionObject
= Section
;
893 PFN_NUMBER PageFrameNumber
;
894 UNICODE_STRING FileName
;
900 /* We should have a PDE */
901 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] != 0);
902 ASSERT(Process
->PdeUpdateNeeded
== FALSE
);
904 /* Attach to the process */
905 KeAttachProcess(&Process
->Pcb
);
907 /* The address space should now been in phase 1 or 0 */
908 ASSERT(Process
->AddressSpaceInitialized
<= 1);
909 Process
->AddressSpaceInitialized
= 2;
911 /* Initialize the Addresss Space lock */
912 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
913 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
915 /* Initialize AVL tree */
916 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
917 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
919 /* Lock PFN database */
920 OldIrql
= MiAcquirePfnLock();
922 /* Setup the PFN for the PDE base of this process */
924 PointerPte
= MiAddressToPte(PXE_BASE
);
926 PointerPte
= MiAddressToPte(PDE_BASE
);
928 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
929 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] == PageFrameNumber
* PAGE_SIZE
);
930 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
932 /* Do the same for hyperspace */
934 PointerPde
= MiAddressToPxe((PVOID
)HYPER_SPACE
);
936 PointerPde
= MiAddressToPde(HYPER_SPACE
);
938 PageFrameNumber
= PFN_FROM_PTE(PointerPde
);
939 //ASSERT(Process->Pcb.DirectoryTableBase[0] == PageFrameNumber * PAGE_SIZE); // we're not lucky
940 MiInitializePfn(PageFrameNumber
, (PMMPTE
)PointerPde
, TRUE
);
942 /* Setup the PFN for the PTE for the working set */
943 PointerPte
= MiAddressToPte(MI_WORKING_SET_LIST
);
944 MI_MAKE_HARDWARE_PTE(&TempPte
, PointerPte
, MM_READWRITE
, 0);
945 ASSERT(PointerPte
->u
.Long
!= 0);
946 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
947 MI_WRITE_INVALID_PTE(PointerPte
, DemandZeroPte
);
948 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
949 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
950 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
952 /* Now initialize the working set list */
953 MiInitializeWorkingSetList(Process
);
956 ASSERT(Process
->PhysicalVadRoot
== NULL
);
958 /* Release PFN lock */
959 MiReleasePfnLock(OldIrql
);
961 /* Check if there's a Section Object */
964 /* Determine the image file name and save it to EPROCESS */
965 FileName
= SectionObject
->FileObject
->FileName
;
966 Source
= (PWCHAR
)((PCHAR
)FileName
.Buffer
+ FileName
.Length
);
969 /* Loop the file name*/
970 while (Source
> FileName
.Buffer
)
972 /* Make sure this isn't a backslash */
973 if (*--Source
== OBJ_NAME_PATH_SEPARATOR
)
975 /* If so, stop it here */
981 /* Otherwise, keep going */
987 /* Copy the to the process and truncate it to 15 characters if necessary */
988 Destination
= Process
->ImageFileName
;
989 Length
= min(Length
, sizeof(Process
->ImageFileName
) - 1);
990 while (Length
--) *Destination
++ = (UCHAR
)*Source
++;
991 *Destination
= ANSI_NULL
;
993 /* Check if caller wants an audit name */
996 /* Setup the audit name */
997 Status
= SeInitializeProcessAuditName(SectionObject
->FileObject
,
1000 if (!NT_SUCCESS(Status
))
1008 /* Map the section */
1009 Status
= MmMapViewOfSection(Section
,
1020 /* Save the pointer */
1021 Process
->SectionBaseAddress
= ImageBase
;
1024 /* Be nice and detach */
1027 /* Return status to caller */
1034 MmInitializeHandBuiltProcess(IN PEPROCESS Process
,
1035 IN PULONG_PTR DirectoryTableBase
)
1037 /* Share the directory base with the idle process */
1038 DirectoryTableBase
[0] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[0];
1039 DirectoryTableBase
[1] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[1];
1041 /* Initialize the Addresss Space */
1042 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
1043 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
1044 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
1045 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
1046 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
1048 /* Use idle process Working set */
1049 Process
->Vm
.VmWorkingSetList
= PsGetCurrentProcess()->Vm
.VmWorkingSetList
;
1052 Process
->HasAddressSpace
= TRUE
;//??
1053 return STATUS_SUCCESS
;
1059 MmInitializeHandBuiltProcess2(IN PEPROCESS Process
)
1061 /* Lock the VAD, ARM3-owned ranges away */
1062 return STATUS_SUCCESS
;
1066 /* FIXME: Evaluate ways to make this portable yet arch-specific */
1069 MmCreateProcessAddressSpace(IN ULONG MinWs
,
1070 IN PEPROCESS Process
,
1071 OUT PULONG_PTR DirectoryTableBase
)
1074 PFN_NUMBER PdeIndex
, HyperIndex
, WsListIndex
;
1076 MMPTE TempPte
, PdePte
;
1078 PMMPTE SystemTable
, HyperTable
;
1082 /* Choose a process color */
1083 Process
->NextPageColor
= (USHORT
)RtlRandom(&MmProcessColorSeed
);
1085 /* Setup the hyperspace lock */
1086 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
1088 /* Lock PFN database */
1089 OldIrql
= MiAcquirePfnLock();
1091 /* Get a zero page for the PDE, if possible */
1092 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1093 MI_SET_USAGE(MI_USAGE_PAGE_DIRECTORY
);
1094 PdeIndex
= MiRemoveZeroPageSafe(Color
);
1097 /* No zero pages, grab a free one */
1098 PdeIndex
= MiRemoveAnyPage(Color
);
1100 /* Zero it outside the PFN lock */
1101 MiReleasePfnLock(OldIrql
);
1102 MiZeroPhysicalPage(PdeIndex
);
1103 OldIrql
= MiAcquirePfnLock();
1106 /* Get a zero page for hyperspace, if possible */
1107 MI_SET_USAGE(MI_USAGE_PAGE_DIRECTORY
);
1108 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1109 HyperIndex
= MiRemoveZeroPageSafe(Color
);
1112 /* No zero pages, grab a free one */
1113 HyperIndex
= MiRemoveAnyPage(Color
);
1115 /* Zero it outside the PFN lock */
1116 MiReleasePfnLock(OldIrql
);
1117 MiZeroPhysicalPage(HyperIndex
);
1118 OldIrql
= MiAcquirePfnLock();
1121 /* Get a zero page for the woring set list, if possible */
1122 MI_SET_USAGE(MI_USAGE_PAGE_TABLE
);
1123 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1124 WsListIndex
= MiRemoveZeroPageSafe(Color
);
1127 /* No zero pages, grab a free one */
1128 WsListIndex
= MiRemoveAnyPage(Color
);
1130 /* Zero it outside the PFN lock */
1131 MiReleasePfnLock(OldIrql
);
1132 MiZeroPhysicalPage(WsListIndex
);
1136 /* Release the PFN lock */
1137 MiReleasePfnLock(OldIrql
);
1140 /* Switch to phase 1 initialization */
1141 ASSERT(Process
->AddressSpaceInitialized
== 0);
1142 Process
->AddressSpaceInitialized
= 1;
1144 /* Set the base directory pointers */
1145 Process
->WorkingSetPage
= WsListIndex
;
1146 DirectoryTableBase
[0] = PdeIndex
<< PAGE_SHIFT
;
1147 DirectoryTableBase
[1] = HyperIndex
<< PAGE_SHIFT
;
1149 /* Make sure we don't already have a page directory setup */
1150 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] == 0);
1152 /* Get a PTE to map hyperspace */
1153 PointerPte
= MiReserveSystemPtes(1, SystemPteSpace
);
1154 ASSERT(PointerPte
!= NULL
);
1157 MI_MAKE_HARDWARE_PTE_KERNEL(&PdePte
,
1162 /* Set it dirty and map it */
1163 MI_MAKE_DIRTY_PAGE(&PdePte
);
1164 MI_WRITE_VALID_PTE(PointerPte
, PdePte
);
1166 /* Now get hyperspace's page table */
1167 HyperTable
= MiPteToAddress(PointerPte
);
1169 /* Now write the PTE/PDE entry for the working set list index itself */
1170 TempPte
= ValidKernelPteLocal
;
1171 TempPte
.u
.Hard
.PageFrameNumber
= WsListIndex
;
1172 PdeOffset
= MiAddressToPteOffset(MmWorkingSetList
);
1173 HyperTable
[PdeOffset
] = TempPte
;
1175 /* Let go of the system PTE */
1176 MiReleaseSystemPtes(PointerPte
, 1, SystemPteSpace
);
1178 /* Save the PTE address of the page directory itself */
1179 Pfn1
= MiGetPfnEntry(PdeIndex
);
1180 Pfn1
->PteAddress
= (PMMPTE
)PDE_BASE
;
1182 /* Insert us into the Mm process list */
1183 OldIrql
= MiAcquireExpansionLock();
1184 InsertTailList(&MmProcessList
, &Process
->MmProcessLinks
);
1185 MiReleaseExpansionLock(OldIrql
);
1187 /* Get a PTE to map the page directory */
1188 PointerPte
= MiReserveSystemPtes(1, SystemPteSpace
);
1189 ASSERT(PointerPte
!= NULL
);
1192 MI_MAKE_HARDWARE_PTE_KERNEL(&PdePte
,
1197 /* Set it dirty and map it */
1198 MI_MAKE_DIRTY_PAGE(&PdePte
);
1199 MI_WRITE_VALID_PTE(PointerPte
, PdePte
);
1201 /* Now get the page directory (which we'll double map, so call it a page table */
1202 SystemTable
= MiPteToAddress(PointerPte
);
1204 /* Copy all the kernel mappings */
1205 PdeOffset
= MiGetPdeOffset(MmSystemRangeStart
);
1206 RtlCopyMemory(&SystemTable
[PdeOffset
],
1207 MiAddressToPde(MmSystemRangeStart
),
1208 PAGE_SIZE
- PdeOffset
* sizeof(MMPTE
));
1210 /* Now write the PTE/PDE entry for hyperspace itself */
1211 TempPte
= ValidKernelPteLocal
;
1212 TempPte
.u
.Hard
.PageFrameNumber
= HyperIndex
;
1213 PdeOffset
= MiGetPdeOffset(HYPER_SPACE
);
1214 SystemTable
[PdeOffset
] = TempPte
;
1218 ASSERT(MiGetPdeOffset(MmHyperSpaceEnd
) >= PdeOffset
);
1220 /* Now do the x86 trick of making the PDE a page table itself */
1221 PdeOffset
= MiGetPdeOffset(PTE_BASE
);
1222 TempPte
.u
.Hard
.PageFrameNumber
= PdeIndex
;
1223 SystemTable
[PdeOffset
] = TempPte
;
1225 /* Let go of the system PTE */
1226 MiReleaseSystemPtes(PointerPte
, 1, SystemPteSpace
);
1228 /* Add the process to the session */
1229 MiSessionAddProcess(Process
);
1236 MmCleanProcessAddressSpace(IN PEPROCESS Process
)
1239 PMM_AVL_TABLE VadTree
;
1240 PETHREAD Thread
= PsGetCurrentThread();
1242 /* Only support this */
1243 ASSERT(Process
->AddressSpaceInitialized
== 2);
1245 /* Remove from the session */
1246 MiSessionRemoveProcess();
1248 /* Lock the process address space from changes */
1249 MmLockAddressSpace(&Process
->Vm
);
1250 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1252 /* VM is deleted now */
1253 Process
->VmDeleted
= TRUE
;
1254 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1256 /* Enumerate the VADs */
1257 VadTree
= &Process
->VadRoot
;
1258 while (VadTree
->NumberGenericTableElements
)
1260 /* Grab the current VAD */
1261 Vad
= (PMMVAD
)VadTree
->BalancedRoot
.RightChild
;
1263 /* Check for old-style memory areas */
1264 if (Vad
->u
.VadFlags
.Spare
== 1)
1266 /* Let RosMm handle this */
1267 MiRosCleanupMemoryArea(Process
, Vad
);
1271 /* Lock the working set */
1272 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1274 /* Remove this VAD from the tree */
1275 ASSERT(VadTree
->NumberGenericTableElements
>= 1);
1276 MiRemoveNode((PMMADDRESS_NODE
)Vad
, VadTree
);
1278 /* Only regular VADs supported for now */
1279 ASSERT(Vad
->u
.VadFlags
.VadType
== VadNone
);
1281 /* Check if this is a section VAD */
1282 if (!(Vad
->u
.VadFlags
.PrivateMemory
) && (Vad
->ControlArea
))
1284 /* Remove the view */
1285 MiRemoveMappedView(Process
, Vad
);
1289 /* Delete the addresses */
1290 MiDeleteVirtualAddresses(Vad
->StartingVpn
<< PAGE_SHIFT
,
1291 (Vad
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1),
1294 /* Release the working set */
1295 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1298 /* Skip ARM3 fake VADs, they'll be freed by MmDeleteProcessAddresSpace */
1299 if (Vad
->u
.VadFlags
.Spare
== 1)
1301 /* Set a flag so MmDeleteMemoryArea knows to free, but not to remove */
1302 Vad
->u
.VadFlags
.Spare
= 2;
1306 /* Free the VAD memory */
1310 /* Lock the working set */
1311 MiLockProcessWorkingSetUnsafe(Process
, Thread
);
1312 ASSERT(Process
->CloneRoot
== NULL
);
1313 ASSERT(Process
->PhysicalVadRoot
== NULL
);
1315 /* Delete the shared user data section */
1316 MiDeleteVirtualAddresses(USER_SHARED_DATA
, USER_SHARED_DATA
, NULL
);
1318 /* Release the working set */
1319 MiUnlockProcessWorkingSetUnsafe(Process
, Thread
);
1321 /* Release the address space */
1322 MmUnlockAddressSpace(&Process
->Vm
);
1327 MmDeleteProcessAddressSpace2(IN PEPROCESS Process
)
1331 PFN_NUMBER PageFrameIndex
;
1333 //ASSERT(Process->CommitCharge == 0);
1335 /* Acquire the PFN lock */
1336 OldIrql
= MiAcquirePfnLock();
1338 /* Check for fully initialized process */
1339 if (Process
->AddressSpaceInitialized
== 2)
1341 /* Map the working set page and its page table */
1342 Pfn1
= MiGetPfnEntry(Process
->WorkingSetPage
);
1343 Pfn2
= MiGetPfnEntry(Pfn1
->u4
.PteFrame
);
1346 MI_SET_PFN_DELETED(Pfn1
);
1347 MiDecrementShareCount(Pfn2
, Pfn1
->u4
.PteFrame
);
1348 MiDecrementShareCount(Pfn1
, Process
->WorkingSetPage
);
1349 ASSERT((Pfn1
->u3
.e2
.ReferenceCount
== 0) || (Pfn1
->u3
.e1
.WriteInProgress
));
1350 MiReleaseSystemPtes(MiAddressToPte(Process
->Vm
.VmWorkingSetList
), 1, SystemPteSpace
);
1352 /* Now map hyperspace and its page table */
1353 PageFrameIndex
= Process
->Pcb
.DirectoryTableBase
[1] >> PAGE_SHIFT
;
1354 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
1355 Pfn2
= MiGetPfnEntry(Pfn1
->u4
.PteFrame
);
1358 MI_SET_PFN_DELETED(Pfn1
);
1359 MiDecrementShareCount(Pfn2
, Pfn1
->u4
.PteFrame
);
1360 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1361 ASSERT((Pfn1
->u3
.e2
.ReferenceCount
== 0) || (Pfn1
->u3
.e1
.WriteInProgress
));
1363 /* Finally, nuke the PDE itself */
1364 PageFrameIndex
= Process
->Pcb
.DirectoryTableBase
[0] >> PAGE_SHIFT
;
1365 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
1366 MI_SET_PFN_DELETED(Pfn1
);
1367 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1368 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1370 /* Page table is now dead. Bye bye... */
1371 ASSERT((Pfn1
->u3
.e2
.ReferenceCount
== 0) || (Pfn1
->u3
.e1
.WriteInProgress
));
1375 /* A partly-initialized process should never exit through here */
1379 /* Release the PFN lock */
1380 MiReleasePfnLock(OldIrql
);
1382 /* Drop a reference on the session */
1383 if (Process
->Session
) MiReleaseProcessReferenceToSessionDataPage(Process
->Session
);
1385 /* Clear out the PDE pages */
1386 Process
->Pcb
.DirectoryTableBase
[0] = 0;
1387 Process
->Pcb
.DirectoryTableBase
[1] = 0;
1391 /* SYSTEM CALLS ***************************************************************/
1395 NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle
,
1396 IN OUT PULONG_PTR NumberOfPages
,
1397 IN OUT PULONG_PTR UserPfnArray
)
1400 return STATUS_NOT_IMPLEMENTED
;
1405 NtMapUserPhysicalPages(IN PVOID VirtualAddresses
,
1406 IN ULONG_PTR NumberOfPages
,
1407 IN OUT PULONG_PTR UserPfnArray
)
1410 return STATUS_NOT_IMPLEMENTED
;
1415 NtMapUserPhysicalPagesScatter(IN PVOID
*VirtualAddresses
,
1416 IN ULONG_PTR NumberOfPages
,
1417 IN OUT PULONG_PTR UserPfnArray
)
1420 return STATUS_NOT_IMPLEMENTED
;
1425 NtFreeUserPhysicalPages(IN HANDLE ProcessHandle
,
1426 IN OUT PULONG_PTR NumberOfPages
,
1427 IN OUT PULONG_PTR UserPfnArray
)
1430 return STATUS_NOT_IMPLEMENTED
;