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 #line 15 "ARMĀ³::PROCSUP"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
19 extern MM_SYSTEMSIZE MmSystemSize
;
21 /* PRIVATE FUNCTIONS **********************************************************/
25 MiRosTakeOverPebTebRanges(IN PEPROCESS Process
)
28 PMEMORY_AREA MemoryArea
;
29 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
30 PVOID AllocatedBase
= (PVOID
)MI_LOWEST_VAD_ADDRESS
;
31 BoundaryAddressMultiple
.QuadPart
= 0;
33 Status
= MmCreateMemoryArea(&Process
->Vm
,
34 MEMORY_AREA_OWNED_BY_ARM3
,
36 ((ULONG_PTR
)MM_HIGHEST_USER_ADDRESS
- 1) -
37 (ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
,
42 BoundaryAddressMultiple
);
43 ASSERT(NT_SUCCESS(Status
));
48 MiCreatePebOrTeb(IN PEPROCESS Process
,
52 PETHREAD Thread
= PsGetCurrentThread();
56 ULONG_PTR StartAddress
, EndAddress
;
57 LARGE_INTEGER CurrentTime
;
60 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD_LONG
), 'ldaV');
61 if (!Vad
) return STATUS_NO_MEMORY
;
63 /* Setup the primary flags with the size, and make it commited, private, RW */
65 Vad
->u
.VadFlags
.CommitCharge
= BYTES_TO_PAGES(Size
);
66 Vad
->u
.VadFlags
.MemCommit
= TRUE
;
67 Vad
->u
.VadFlags
.PrivateMemory
= TRUE
;
68 Vad
->u
.VadFlags
.Protection
= MM_READWRITE
;
69 Vad
->u
.VadFlags
.NoChange
= TRUE
;
71 /* Setup the secondary flags to make it a secured, writable, long VAD */
72 Vad
->u2
.LongFlags2
= 0;
73 Vad
->u2
.VadFlags2
.OneSecured
= TRUE
;
74 Vad
->u2
.VadFlags2
.LongVad
= TRUE
;
75 Vad
->u2
.VadFlags2
.ReadOnly
= FALSE
;
77 /* Lock the process address space */
78 KeAcquireGuardedMutex(&Process
->AddressCreationLock
);
80 /* Check if this is a PEB creation */
81 if (Size
== sizeof(PEB
))
83 /* Start at the highest valid address */
84 StartAddress
= (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1;
86 /* Select the random coefficient */
87 KeQueryTickCount(&CurrentTime
);
88 CurrentTime
.LowPart
&= ((64 * _1KB
) >> PAGE_SHIFT
) - 1;
89 if (CurrentTime
.LowPart
<= 1) CurrentTime
.LowPart
= 2;
90 RandomCoeff
= CurrentTime
.LowPart
<< PAGE_SHIFT
;
92 /* Select the highest valid address minus the random coefficient */
93 StartAddress
-= RandomCoeff
;
94 EndAddress
= StartAddress
+ ROUND_TO_PAGES(Size
) - 1;
96 /* See if this VA range can be obtained */
97 if (!MiCheckForConflictingNode(StartAddress
>> PAGE_SHIFT
,
98 EndAddress
>> PAGE_SHIFT
,
101 /* No conflict, use this address */
102 *Base
= StartAddress
;
107 /* For TEBs, or if a PEB location couldn't be found, scan the VAD root */
108 Status
= MiFindEmptyAddressRangeDownTree(ROUND_TO_PAGES(Size
),
109 (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1,
113 ASSERT(NT_SUCCESS(Status
));
116 /* Validate that it came from the VAD ranges */
117 ASSERT(*Base
>= (ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
);
119 /* Build the rest of the VAD now */
120 Vad
->StartingVpn
= (*Base
) >> PAGE_SHIFT
;
121 Vad
->EndingVpn
= ((*Base
) + Size
- 1) >> PAGE_SHIFT
;
122 Vad
->u3
.Secured
.StartVpn
= *Base
;
123 Vad
->u3
.Secured
.EndVpn
= (Vad
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1);
125 /* FIXME: Should setup VAD bitmap */
126 Status
= STATUS_SUCCESS
;
128 /* Pretend as if we own the working set */
129 MiLockProcessWorkingSet(Process
, Thread
);
132 ASSERT(Vad
->EndingVpn
>= Vad
->StartingVpn
);
133 Process
->VadRoot
.NodeHint
= Vad
;
134 MiInsertNode((PVOID
)Vad
, &Process
->VadRoot
);
136 /* Release the working set */
137 MiUnlockProcessWorkingSet(Process
, Thread
);
139 /* Release the address space lock */
140 KeReleaseGuardedMutex(&Process
->AddressCreationLock
);
142 /* Return the status */
143 DPRINT("Allocated PEB/TEB at: 0x%p for %16s\n", *Base
, Process
->ImageFileName
);
149 MmDeleteTeb(IN PEPROCESS Process
,
153 DPRINT("Leaking 4KB at thread exit, this will be fixed later\n");
158 MmDeleteKernelStack(IN PVOID StackBase
,
162 PFN_NUMBER StackPages
, PageFrameNumber
;//, PageTableFrameNumber;
163 PMMPFN Pfn1
;//, Pfn2;
168 // This should be the guard page, so decrement by one
170 PointerPte
= MiAddressToPte(StackBase
);
174 // Calculate pages used
176 StackPages
= BYTES_TO_PAGES(GuiStack
?
177 KERNEL_LARGE_STACK_SIZE
: KERNEL_STACK_SIZE
);
179 /* Acquire the PFN lock */
180 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
185 for (i
= 0; i
< StackPages
; i
++)
188 // Check if this is a valid PTE
190 if (PointerPte
->u
.Hard
.Valid
== 1)
192 /* Get the PTE's page */
193 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
194 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
195 #if 0 // ARM3 might not own the page table, so don't take this risk. Leak it instead!
196 /* Now get the page of the page table mapping it */
197 PageTableFrameNumber
= Pfn1
->u4
.PteFrame
;
198 Pfn2
= MiGetPfnEntry(PageTableFrameNumber
);
200 /* Remove a shared reference, since the page is going away */
201 MiDecrementShareCount(Pfn2
, PageTableFrameNumber
);
203 /* Set the special pending delete marker */
204 Pfn1
->PteAddress
= (PMMPTE
)((ULONG_PTR
)Pfn1
->PteAddress
| 1);
206 /* And now delete the actual stack page */
207 MiDecrementShareCount(Pfn1
, PageFrameNumber
);
217 // We should be at the guard page now
219 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
221 /* Release the PFN lock */
222 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
227 MiReleaseSystemPtes(PointerPte
, StackPages
+ 1, SystemPteSpace
);
232 MmCreateKernelStack(IN BOOLEAN GuiStack
,
235 PFN_NUMBER StackPtes
, StackPages
;
236 PMMPTE PointerPte
, StackPte
;
238 MMPTE TempPte
, InvalidPte
;
240 PFN_NUMBER PageFrameIndex
;
244 // Calculate pages needed
249 // We'll allocate 64KB stack, but only commit 12K
251 StackPtes
= BYTES_TO_PAGES(KERNEL_LARGE_STACK_SIZE
);
252 StackPages
= BYTES_TO_PAGES(KERNEL_LARGE_STACK_COMMIT
);
258 // We'll allocate 12K and that's it
260 StackPtes
= BYTES_TO_PAGES(KERNEL_STACK_SIZE
);
261 StackPages
= StackPtes
;
265 // Reserve stack pages, plus a guard page
267 StackPte
= MiReserveSystemPtes(StackPtes
+ 1, SystemPteSpace
);
268 if (!StackPte
) return NULL
;
271 // Get the stack address
273 BaseAddress
= MiPteToAddress(StackPte
+ StackPtes
+ 1);
276 // Select the right PTE address where we actually start committing pages
278 PointerPte
= StackPte
;
279 if (GuiStack
) PointerPte
+= BYTES_TO_PAGES(KERNEL_LARGE_STACK_SIZE
-
280 KERNEL_LARGE_STACK_COMMIT
);
283 /* Setup the temporary invalid PTE */
284 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
286 /* Setup the template stack PTE */
287 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, PointerPte
+ 1, MM_READWRITE
, 0);
290 // Acquire the PFN DB lock
292 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
295 // Loop each stack page
297 for (i
= 0; i
< StackPages
; i
++)
304 /* Get a page and write the current invalid PTE */
305 PageFrameIndex
= MiRemoveAnyPage(0);
306 MI_WRITE_INVALID_PTE(PointerPte
, InvalidPte
);
308 /* Initialize the PFN entry for this page */
309 MiInitializePfn(PageFrameIndex
, PointerPte
, 1);
311 /* Write the valid PTE */
312 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameIndex
;
313 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
317 (VOID
)InterlockedExchangeAddUL(&MiMemoryConsumers
[MC_NPPOOL
].PagesUsed
, StackPages
);
320 // Release the PFN lock
322 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
325 // Return the stack address
332 MmGrowKernelStackEx(IN PVOID StackPointer
,
335 PKTHREAD Thread
= KeGetCurrentThread();
336 PMMPTE LimitPte
, NewLimitPte
, LastPte
;
337 PFN_NUMBER StackPages
;
339 MMPTE TempPte
, InvalidPte
;
340 PFN_NUMBER PageFrameIndex
;
343 // Make sure the stack did not overflow
345 ASSERT(((ULONG_PTR
)Thread
->StackBase
- (ULONG_PTR
)Thread
->StackLimit
) <=
346 (KERNEL_LARGE_STACK_SIZE
+ PAGE_SIZE
));
349 // Get the current stack limit
351 LimitPte
= MiAddressToPte(Thread
->StackLimit
);
352 ASSERT(LimitPte
->u
.Hard
.Valid
== 1);
355 // Get the new one and make sure this isn't a retarded request
357 NewLimitPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)StackPointer
- GrowSize
));
358 if (NewLimitPte
== LimitPte
) return STATUS_SUCCESS
;
361 // Now make sure you're not going past the reserved space
363 LastPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)Thread
->StackBase
-
364 KERNEL_LARGE_STACK_SIZE
));
365 if (NewLimitPte
< LastPte
)
370 DPRINT1("Thread wants too much stack\n");
371 return STATUS_STACK_OVERFLOW
;
375 // Calculate the number of new pages
378 StackPages
= (LimitPte
- NewLimitPte
+ 1);
380 /* Setup the temporary invalid PTE */
381 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
384 // Acquire the PFN DB lock
386 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
389 // Loop each stack page
391 while (LimitPte
>= NewLimitPte
)
393 /* Get a page and write the current invalid PTE */
394 PageFrameIndex
= MiRemoveAnyPage(0);
395 MI_WRITE_INVALID_PTE(LimitPte
, InvalidPte
);
397 /* Initialize the PFN entry for this page */
398 MiInitializePfn(PageFrameIndex
, LimitPte
, 1);
400 /* Setup the template stack PTE */
401 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, LimitPte
, MM_READWRITE
, PageFrameIndex
);
403 /* Write the valid PTE */
404 MI_WRITE_VALID_PTE(LimitPte
--, TempPte
);
408 // Release the PFN lock
410 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
415 Thread
->StackLimit
= (ULONG_PTR
)MiPteToAddress(NewLimitPte
);
416 return STATUS_SUCCESS
;
421 MmGrowKernelStack(IN PVOID StackPointer
)
424 // Call the extended version
426 return MmGrowKernelStackEx(StackPointer
, KERNEL_LARGE_STACK_COMMIT
);
431 MmSetMemoryPriorityProcess(IN PEPROCESS Process
,
432 IN UCHAR MemoryPriority
)
437 // Check if we have less then 16MB of Physical Memory
439 if ((MmSystemSize
== MmSmallSystem
) &&
440 (MmNumberOfPhysicalPages
< ((15 * 1024 * 1024) / PAGE_SIZE
)))
443 // Always use background priority
445 MemoryPriority
= MEMORY_PRIORITY_BACKGROUND
;
449 // Save the old priority and update it
451 OldPriority
= (UCHAR
)Process
->Vm
.Flags
.MemoryPriority
;
452 Process
->Vm
.Flags
.MemoryPriority
= MemoryPriority
;
455 // Return the old priority
462 MmGetSessionLocaleId(VOID
)
468 // Get the current process
470 Process
= PsGetCurrentProcess();
473 // Check if it's the Session Leader
475 if (Process
->Vm
.Flags
.SessionLeader
)
478 // Make sure it has a valid Session
480 if (Process
->Session
)
486 return ((PMM_SESSION_SPACE
)Process
->Session
)->LocaleId
;
492 // Not a session leader, return the default
494 return PsDefaultThreadLocaleId
;
499 MmCreatePeb(IN PEPROCESS Process
,
500 IN PINITIAL_PEB InitialPeb
,
504 LARGE_INTEGER SectionOffset
;
506 PVOID TableBase
= NULL
;
507 PIMAGE_NT_HEADERS NtHeaders
;
508 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
510 USHORT Characteristics
;
511 KAFFINITY ProcessAffinityMask
= 0;
512 SectionOffset
.QuadPart
= (ULONGLONG
)0;
518 KeAttachProcess(&Process
->Pcb
);
523 Status
= MiCreatePebOrTeb(Process
, sizeof(PEB
), (PULONG_PTR
)&Peb
);
524 ASSERT(NT_SUCCESS(Status
));
529 Status
= MmMapViewOfSection(ExpNlsSectionPointer
,
539 if (!NT_SUCCESS(Status
)) return Status
;
542 // Use SEH in case we can't load the PEB
547 // Initialize the PEB
549 RtlZeroMemory(Peb
, sizeof(PEB
));
554 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
555 Peb
->InheritedAddressSpace
= InitialPeb
->InheritedAddressSpace
;
556 Peb
->Mutant
= InitialPeb
->Mutant
;
557 Peb
->ImageUsesLargePages
= InitialPeb
->ImageUsesLargePages
;
562 Peb
->AnsiCodePageData
= (PCHAR
)TableBase
+ ExpAnsiCodePageDataOffset
;
563 Peb
->OemCodePageData
= (PCHAR
)TableBase
+ ExpOemCodePageDataOffset
;
564 Peb
->UnicodeCaseTableData
= (PCHAR
)TableBase
+ ExpUnicodeCaseTableDataOffset
;
567 // Default Version Data (could get changed below)
569 Peb
->OSMajorVersion
= NtMajorVersion
;
570 Peb
->OSMinorVersion
= NtMinorVersion
;
571 Peb
->OSBuildNumber
= (USHORT
)(NtBuildNumber
& 0x3FFF);
572 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
573 Peb
->OSCSDVersion
= (USHORT
)CmNtCSDVersion
;
576 // Heap and Debug Data
578 Peb
->NumberOfProcessors
= KeNumberProcessors
;
579 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
580 Peb
->NtGlobalFlag
= NtGlobalFlag
;
581 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
582 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
583 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
584 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;
585 Peb->CriticalSectionTimeout = MmCriticalSectionTimeout;
586 Peb->MinimumStackCommit = MmMinimumStackCommitInBytes;
588 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
589 Peb
->ProcessHeaps
= (PVOID
*)(Peb
+ 1);
594 if (Process
->Session
) Peb
->SessionId
= 0; // MmGetSessionId(Process);
596 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
602 _SEH2_YIELD(return _SEH2_GetExceptionCode());
607 // Use SEH in case we can't load the image
614 NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
);
615 Characteristics
= NtHeaders
->FileHeader
.Characteristics
;
617 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
623 _SEH2_YIELD(return STATUS_INVALID_IMAGE_PROTECT
);
633 // Use SEH in case we can't load the headers
638 // Get the Image Config Data too
640 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
642 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
649 ProbeForRead(ImageConfigData
,
650 sizeof(IMAGE_LOAD_CONFIG_DIRECTORY
),
655 // Write subsystem data
657 Peb
->ImageSubsystem
= NtHeaders
->OptionalHeader
.Subsystem
;
658 Peb
->ImageSubsystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
659 Peb
->ImageSubsystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
662 // Check for version data
664 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
667 // Extract values and write them
669 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
670 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
671 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
672 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
676 // Process the image config data overrides if specfied
678 if (ImageConfigData
!= NULL
)
681 // Process CSD version override
683 if (ImageConfigData
->CSDVersion
)
688 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
692 // Process affinity mask ovverride
694 if (ImageConfigData
->ProcessAffinityMask
)
699 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
704 // Check if this is a UP image
705 if (Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
708 // Force it to use CPU 0
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 ASSERT(NT_SUCCESS(Status
));
762 // Use SEH in case we can't load the TEB
767 // Initialize the PEB
769 RtlZeroMemory(Teb
, sizeof(TEB
));
774 Teb
->NtTib
.ExceptionList
= EXCEPTION_CHAIN_END
;
775 Teb
->NtTib
.Self
= (PNT_TIB
)Teb
;
778 // Identify this as an OS/2 V3.0 ("Cruiser") TIB
780 Teb
->NtTib
.Version
= 30 << 8;
785 Teb
->ClientId
= *ClientId
;
786 Teb
->RealClientId
= *ClientId
;
787 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
788 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
791 // Check if we have a grandparent TEB
793 if ((InitialTeb
->PreviousStackBase
== NULL
) &&
794 (InitialTeb
->PreviousStackLimit
== NULL
))
797 // Use initial TEB values
799 Teb
->NtTib
.StackBase
= InitialTeb
->StackBase
;
800 Teb
->NtTib
.StackLimit
= InitialTeb
->StackLimit
;
801 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
806 // Use grandparent TEB values
808 Teb
->NtTib
.StackBase
= InitialTeb
->PreviousStackBase
;
809 Teb
->NtTib
.StackLimit
= InitialTeb
->PreviousStackLimit
;
813 // Initialize the static unicode string
815 Teb
->StaticUnicodeString
.MaximumLength
= sizeof(Teb
->StaticUnicodeBuffer
);
816 Teb
->StaticUnicodeString
.Buffer
= Teb
->StaticUnicodeBuffer
;
818 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
823 Status
= _SEH2_GetExceptionCode();
837 MmInitializeProcessAddressSpace(IN PEPROCESS Process
,
838 IN PEPROCESS ProcessClone OPTIONAL
,
839 IN PVOID Section OPTIONAL
,
841 IN POBJECT_NAME_INFORMATION
*AuditName OPTIONAL
)
843 NTSTATUS Status
= STATUS_SUCCESS
;
846 PROS_SECTION_OBJECT SectionObject
= Section
;
850 PFN_NUMBER PageFrameNumber
;
851 UNICODE_STRING FileName
;
856 /* We should have a PDE */
857 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] != 0);
858 ASSERT(Process
->PdeUpdateNeeded
== FALSE
);
860 /* Attach to the process */
861 KeAttachProcess(&Process
->Pcb
);
863 /* The address space should now been in phase 1 or 0 */
864 ASSERT(Process
->AddressSpaceInitialized
<= 1);
865 Process
->AddressSpaceInitialized
= 2;
867 /* Initialize the Addresss Space lock */
868 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
869 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
871 /* Initialize AVL tree */
872 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
873 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
875 /* Lock PFN database */
876 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
878 /* Setup the PFN for the PDE base of this process */
879 PointerPte
= MiAddressToPte(PDE_BASE
);
880 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
881 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
883 /* Do the same for hyperspace */
884 PointerPde
= MiAddressToPde(HYPER_SPACE
);
885 PageFrameNumber
= PFN_FROM_PTE(PointerPde
);
886 MiInitializePfn(PageFrameNumber
, PointerPde
, TRUE
);
888 /* Release PFN lock */
889 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
891 /* Lock the VAD, ARM3-owned ranges away */
892 MiRosTakeOverPebTebRanges(Process
);
894 /* Check if there's a Section Object */
897 /* Determine the image file name and save it to EPROCESS */
898 FileName
= SectionObject
->FileObject
->FileName
;
899 Source
= (PWCHAR
)((PCHAR
)FileName
.Buffer
+ FileName
.Length
);
902 /* Loop the file name*/
903 while (Source
> FileName
.Buffer
)
905 /* Make sure this isn't a backslash */
906 if (*--Source
== OBJ_NAME_PATH_SEPARATOR
)
908 /* If so, stop it here */
914 /* Otherwise, keep going */
920 /* Copy the to the process and truncate it to 15 characters if necessary */
921 Destination
= Process
->ImageFileName
;
922 Length
= min(Length
, sizeof(Process
->ImageFileName
) - 1);
923 while (Length
--) *Destination
++ = (UCHAR
)*Source
++;
924 *Destination
= ANSI_NULL
;
926 /* Check if caller wants an audit name */
929 /* Setup the audit name */
930 Status
= SeInitializeProcessAuditName(SectionObject
->FileObject
,
933 if (!NT_SUCCESS(Status
))
941 /* Map the section */
942 Status
= MmMapViewOfSection(Section
,
953 /* Save the pointer */
954 Process
->SectionBaseAddress
= ImageBase
;
957 /* Be nice and detach */
960 /* Return status to caller */
966 MmInitializeHandBuiltProcess(IN PEPROCESS Process
,
967 IN PULONG_PTR DirectoryTableBase
)
969 /* Share the directory base with the idle process */
970 DirectoryTableBase
[0] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[0];
971 DirectoryTableBase
[1] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[1];
973 /* Initialize the Addresss Space */
974 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
975 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
976 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
977 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
978 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
981 Process
->HasAddressSpace
= TRUE
;//??
982 return STATUS_SUCCESS
;
987 MmInitializeHandBuiltProcess2(IN PEPROCESS Process
)
989 /* Lock the VAD, ARM3-owned ranges away */
990 MiRosTakeOverPebTebRanges(Process
);
991 return STATUS_SUCCESS
;
994 /* FIXME: Evaluate ways to make this portable yet arch-specific */
997 MmCreateProcessAddressSpace(IN ULONG MinWs
,
998 IN PEPROCESS Process
,
999 OUT PULONG_PTR DirectoryTableBase
)
1002 PFN_NUMBER PdeIndex
, HyperIndex
;
1004 MMPTE TempPte
, PdePte
;
1008 /* No page colors yet */
1009 Process
->NextPageColor
= 0;
1011 /* Setup the hyperspace lock */
1012 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
1014 /* Lock PFN database */
1015 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1017 /* Get a page for the PDE */
1018 PdeIndex
= MiRemoveAnyPage(0);
1019 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1020 MiZeroPhysicalPage(PdeIndex
);
1021 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1023 /* Get a page for hyperspace */
1024 HyperIndex
= MiRemoveAnyPage(0);
1025 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1026 MiZeroPhysicalPage(HyperIndex
);
1028 /* Switch to phase 1 initialization */
1029 ASSERT(Process
->AddressSpaceInitialized
== 0);
1030 Process
->AddressSpaceInitialized
= 1;
1032 /* Set the base directory pointers */
1033 DirectoryTableBase
[0] = PdeIndex
<< PAGE_SHIFT
;
1034 DirectoryTableBase
[1] = HyperIndex
<< PAGE_SHIFT
;
1036 /* Make sure we don't already have a page directory setup */
1037 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] == 0);
1039 /* Insert us into the Mm process list */
1040 InsertTailList(&MmProcessList
, &Process
->MmProcessLinks
);
1042 /* Get a PTE to map the page directory */
1043 PointerPte
= MiReserveSystemPtes(1, SystemPteSpace
);
1044 ASSERT(PointerPte
!= NULL
);
1047 MI_MAKE_HARDWARE_PTE_KERNEL(&PdePte
,
1052 /* Set it dirty and map it */
1053 PdePte
.u
.Hard
.Dirty
= TRUE
;
1054 MI_WRITE_VALID_PTE(PointerPte
, PdePte
);
1056 /* Now get the page directory (which we'll double map, so call it a page table */
1057 SystemTable
= MiPteToAddress(PointerPte
);
1059 /* Copy all the kernel mappings */
1060 PdeOffset
= MiGetPdeOffset(MmSystemRangeStart
);
1062 RtlCopyMemory(&SystemTable
[PdeOffset
],
1063 MiAddressToPde(MmSystemRangeStart
),
1064 PAGE_SIZE
- PdeOffset
* sizeof(MMPTE
));
1066 /* Now write the PTE/PDE entry for hyperspace itself */
1067 TempPte
= ValidKernelPte
;
1068 TempPte
.u
.Hard
.PageFrameNumber
= HyperIndex
;
1069 PdeOffset
= MiGetPdeOffset(HYPER_SPACE
);
1070 SystemTable
[PdeOffset
] = TempPte
;
1074 ASSERT(MiGetPdeOffset(MmHyperSpaceEnd
) >= PdeOffset
);
1076 /* Now do the x86 trick of making the PDE a page table itself */
1077 PdeOffset
= MiGetPdeOffset(PTE_BASE
);
1078 TempPte
.u
.Hard
.PageFrameNumber
= PdeIndex
;
1079 SystemTable
[PdeOffset
] = TempPte
;
1081 /* Let go of the system PTE */
1082 MiReleaseSystemPtes(PointerPte
, 1, SystemPteSpace
);
1088 MmCleanProcessAddressSpace(IN PEPROCESS Process
)
1091 PMM_AVL_TABLE VadTree
;
1092 PETHREAD Thread
= PsGetCurrentThread();
1094 /* Lock the process address space from changes */
1095 MmLockAddressSpace(&Process
->Vm
);
1097 /* Enumerate the VADs */
1098 VadTree
= &Process
->VadRoot
;
1099 DPRINT("Cleaning up VADs: %d\n", VadTree
->NumberGenericTableElements
);
1100 while (VadTree
->NumberGenericTableElements
)
1102 /* Grab the current VAD */
1103 Vad
= (PMMVAD
)VadTree
->BalancedRoot
.RightChild
;
1105 /* Lock the working set */
1106 MiLockProcessWorkingSet(Process
, Thread
);
1108 /* Remove this VAD from the tree */
1109 ASSERT(VadTree
->NumberGenericTableElements
>= 1);
1110 DPRINT("Removing node for VAD: %lx %lx\n", Vad
->StartingVpn
, Vad
->EndingVpn
);
1111 MiRemoveNode((PMMADDRESS_NODE
)Vad
, VadTree
);
1112 DPRINT("Moving on: %d\n", VadTree
->NumberGenericTableElements
);
1114 /* Check if this VAD was the hint */
1115 if (VadTree
->NodeHint
== Vad
)
1117 /* Get a new hint, unless we're empty now, in which case nothing */
1118 VadTree
->NodeHint
= VadTree
->BalancedRoot
.RightChild
;
1119 if (!VadTree
->NumberGenericTableElements
) VadTree
->NodeHint
= NULL
;
1122 /* Only PEB/TEB VADs supported for now */
1123 ASSERT(Vad
->u
.VadFlags
.PrivateMemory
== 1);
1124 ASSERT(Vad
->u
.VadFlags
.VadType
== VadNone
);
1126 /* Release the working set */
1127 MiUnlockProcessWorkingSet(Process
, Thread
);
1129 /* Free the VAD memory */
1133 /* Release the address space */
1134 MmUnlockAddressSpace(&Process
->Vm
);
1137 /* SYSTEM CALLS ***************************************************************/
1141 NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle
,
1142 IN OUT PULONG_PTR NumberOfPages
,
1143 IN OUT PULONG_PTR UserPfnArray
)
1146 return STATUS_NOT_IMPLEMENTED
;
1151 NtMapUserPhysicalPages(IN PVOID VirtualAddresses
,
1152 IN ULONG_PTR NumberOfPages
,
1153 IN OUT PULONG_PTR UserPfnArray
)
1156 return STATUS_NOT_IMPLEMENTED
;
1161 NtMapUserPhysicalPagesScatter(IN PVOID
*VirtualAddresses
,
1162 IN ULONG_PTR NumberOfPages
,
1163 IN OUT PULONG_PTR UserPfnArray
)
1166 return STATUS_NOT_IMPLEMENTED
;
1171 NtFreeUserPhysicalPages(IN HANDLE ProcessHandle
,
1172 IN OUT PULONG_PTR NumberOfPages
,
1173 IN OUT PULONG_PTR UserPfnArray
)
1176 return STATUS_NOT_IMPLEMENTED
;