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 "../ARM3/miarm.h"
18 /* GLOBALS ********************************************************************/
20 ULONG MmProcessColorSeed
= 0x12345678;
21 PMMWSL MmWorkingSetList
;
23 /* PRIVATE FUNCTIONS **********************************************************/
27 MiRosTakeOverSharedUserPage(IN PEPROCESS Process
)
30 PMEMORY_AREA MemoryArea
;
31 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
32 PVOID AllocatedBase
= (PVOID
)MM_SHARED_USER_DATA_VA
;
33 BoundaryAddressMultiple
.QuadPart
= 0;
35 Status
= MmCreateMemoryArea(&Process
->Vm
,
36 MEMORY_AREA_OWNED_BY_ARM3
,
43 BoundaryAddressMultiple
);
44 ASSERT(NT_SUCCESS(Status
));
49 MiCreatePebOrTeb(IN PEPROCESS Process
,
53 PETHREAD Thread
= PsGetCurrentThread();
57 ULONG_PTR StartAddress
, EndAddress
;
58 LARGE_INTEGER CurrentTime
;
59 TABLE_SEARCH_RESULT Result
= TableFoundNode
;
60 PMMADDRESS_NODE Parent
;
63 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD_LONG
), 'ldaV');
64 if (!Vad
) return STATUS_NO_MEMORY
;
66 /* Setup the primary flags with the size, and make it commited, private, RW */
68 Vad
->u
.VadFlags
.CommitCharge
= BYTES_TO_PAGES(Size
);
69 Vad
->u
.VadFlags
.MemCommit
= TRUE
;
70 Vad
->u
.VadFlags
.PrivateMemory
= TRUE
;
71 Vad
->u
.VadFlags
.Protection
= MM_READWRITE
;
72 Vad
->u
.VadFlags
.NoChange
= TRUE
;
74 /* Setup the secondary flags to make it a secured, writable, long VAD */
75 Vad
->u2
.LongFlags2
= 0;
76 Vad
->u2
.VadFlags2
.OneSecured
= TRUE
;
77 Vad
->u2
.VadFlags2
.LongVad
= TRUE
;
78 Vad
->u2
.VadFlags2
.ReadOnly
= FALSE
;
80 /* Lock the process address space */
81 KeAcquireGuardedMutex(&Process
->AddressCreationLock
);
83 /* Check if this is a PEB creation */
84 if (Size
== sizeof(PEB
))
86 /* Start at the highest valid address */
87 StartAddress
= (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1;
89 /* Select the random coefficient */
90 KeQueryTickCount(&CurrentTime
);
91 CurrentTime
.LowPart
&= ((64 * _1KB
) >> PAGE_SHIFT
) - 1;
92 if (CurrentTime
.LowPart
<= 1) CurrentTime
.LowPart
= 2;
93 RandomCoeff
= CurrentTime
.LowPart
<< PAGE_SHIFT
;
95 /* Select the highest valid address minus the random coefficient */
96 StartAddress
-= RandomCoeff
;
97 EndAddress
= StartAddress
+ ROUND_TO_PAGES(Size
) - 1;
99 /* Try to find something below the random upper margin */
100 Result
= MiFindEmptyAddressRangeDownTree(ROUND_TO_PAGES(Size
),
108 /* Check for success. TableFoundNode means nothing free. */
109 if (Result
== TableFoundNode
)
111 /* For TEBs, or if a PEB location couldn't be found, scan the VAD root */
112 Result
= MiFindEmptyAddressRangeDownTree(ROUND_TO_PAGES(Size
),
113 (ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1,
118 /* Bail out, if still nothing free was found */
119 if (Result
== TableFoundNode
) return STATUS_NO_MEMORY
;
122 /* Validate that it came from the VAD ranges */
123 ASSERT(*Base
>= (ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
);
125 /* Build the rest of the VAD now */
126 Vad
->StartingVpn
= (*Base
) >> PAGE_SHIFT
;
127 Vad
->EndingVpn
= ((*Base
) + Size
- 1) >> PAGE_SHIFT
;
128 Vad
->u3
.Secured
.StartVpn
= *Base
;
129 Vad
->u3
.Secured
.EndVpn
= (Vad
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1);
130 Vad
->u1
.Parent
= NULL
;
132 /* FIXME: Should setup VAD bitmap */
133 Status
= STATUS_SUCCESS
;
135 /* Pretend as if we own the working set */
136 MiLockProcessWorkingSet(Process
, Thread
);
139 ASSERT(Vad
->EndingVpn
>= Vad
->StartingVpn
);
140 Process
->VadRoot
.NodeHint
= Vad
;
141 Vad
->ControlArea
= NULL
; // For Memory-Area hack
142 Vad
->FirstPrototypePte
= NULL
;
143 DPRINT("VAD: %p\n", Vad
);
144 DPRINT("Allocated PEB/TEB at: 0x%p for %16s\n", *Base
, Process
->ImageFileName
);
145 MiInsertNode(&Process
->VadRoot
, (PVOID
)Vad
, Parent
, Result
);
147 /* Release the working set */
148 MiUnlockProcessWorkingSet(Process
, Thread
);
150 /* Release the address space lock */
151 KeReleaseGuardedMutex(&Process
->AddressCreationLock
);
153 /* Return the status */
159 MmDeleteTeb(IN PEPROCESS Process
,
163 PETHREAD Thread
= PsGetCurrentThread();
165 PMM_AVL_TABLE VadTree
= &Process
->VadRoot
;
166 DPRINT("Deleting TEB: %p in %16s\n", Teb
, Process
->ImageFileName
);
168 /* TEB is one page */
169 TebEnd
= (ULONG_PTR
)Teb
+ ROUND_TO_PAGES(sizeof(TEB
)) - 1;
171 /* Attach to the process */
172 KeAttachProcess(&Process
->Pcb
);
174 /* Lock the process address space */
175 KeAcquireGuardedMutex(&Process
->AddressCreationLock
);
177 /* Find the VAD, make sure it's a TEB VAD */
178 Vad
= MiLocateAddress(Teb
);
179 DPRINT("Removing node for VAD: %lx %lx\n", Vad
->StartingVpn
, Vad
->EndingVpn
);
181 if (Vad
->StartingVpn
!= ((ULONG_PTR
)Teb
>> PAGE_SHIFT
))
183 /* Bug in the AVL code? */
184 DPRINT1("Corrupted VAD!\n");
188 /* Sanity checks for a valid TEB VAD */
189 ASSERT((Vad
->StartingVpn
== ((ULONG_PTR
)Teb
>> PAGE_SHIFT
) &&
190 (Vad
->EndingVpn
== (TebEnd
>> PAGE_SHIFT
))));
191 ASSERT(Vad
->u
.VadFlags
.NoChange
== TRUE
);
192 ASSERT(Vad
->u2
.VadFlags2
.OneSecured
== TRUE
);
193 ASSERT(Vad
->u2
.VadFlags2
.MultipleSecured
== FALSE
);
195 /* Lock the working set */
196 MiLockProcessWorkingSet(Process
, Thread
);
198 /* Remove this VAD from the tree */
199 ASSERT(VadTree
->NumberGenericTableElements
>= 1);
200 MiRemoveNode((PMMADDRESS_NODE
)Vad
, VadTree
);
202 /* Delete the pages */
203 MiDeleteVirtualAddresses((ULONG_PTR
)Teb
, TebEnd
, NULL
);
205 /* Release the working set */
206 MiUnlockProcessWorkingSet(Process
, Thread
);
212 /* Release the address space lock */
213 KeReleaseGuardedMutex(&Process
->AddressCreationLock
);
221 MmDeleteKernelStack(IN PVOID StackBase
,
225 PFN_NUMBER PageFrameNumber
, PageTableFrameNumber
;
226 PFN_COUNT StackPages
;
227 PMMPFN Pfn1
;//, Pfn2;
232 // This should be the guard page, so decrement by one
234 PointerPte
= MiAddressToPte(StackBase
);
238 // Calculate pages used
240 StackPages
= BYTES_TO_PAGES(GuiStack
?
241 KERNEL_LARGE_STACK_SIZE
: KERNEL_STACK_SIZE
);
243 /* Acquire the PFN lock */
244 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
249 for (i
= 0; i
< StackPages
; i
++)
252 // Check if this is a valid PTE
254 if (PointerPte
->u
.Hard
.Valid
== 1)
256 /* Get the PTE's page */
257 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
258 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
259 #if 1 // ARM3 might not own the page table, so don't take this risk. Leak it instead!
260 /* Now get the page of the page table mapping it */
261 PageTableFrameNumber
= Pfn1
->u4
.PteFrame
;
262 //Pfn2 = MiGetPfnEntry(PageTableFrameNumber);
264 /* Remove a shared reference, since the page is going away */
265 DPRINT("SystemPTE PDE: %lx\n", PageTableFrameNumber
);
266 //MiDecrementShareCount(Pfn2, PageTableFrameNumber);
268 /* Set the special pending delete marker */
269 MI_SET_PFN_DELETED(Pfn1
);
271 /* And now delete the actual stack page */
272 MiDecrementShareCount(Pfn1
, PageFrameNumber
);
282 // We should be at the guard page now
284 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
286 /* Release the PFN lock */
287 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
292 MiReleaseSystemPtes(PointerPte
, StackPages
+ 1, SystemPteSpace
);
297 MmCreateKernelStack(IN BOOLEAN GuiStack
,
300 PFN_COUNT StackPtes
, StackPages
;
301 PMMPTE PointerPte
, StackPte
;
303 MMPTE TempPte
, InvalidPte
;
305 PFN_NUMBER PageFrameIndex
;
309 // Calculate pages needed
314 // We'll allocate 64KB stack, but only commit 12K
316 StackPtes
= BYTES_TO_PAGES(KERNEL_LARGE_STACK_SIZE
);
317 StackPages
= BYTES_TO_PAGES(KERNEL_LARGE_STACK_COMMIT
);
323 // We'll allocate 12K and that's it
325 StackPtes
= BYTES_TO_PAGES(KERNEL_STACK_SIZE
);
326 StackPages
= StackPtes
;
330 // Reserve stack pages, plus a guard page
332 StackPte
= MiReserveSystemPtes(StackPtes
+ 1, SystemPteSpace
);
333 if (!StackPte
) return NULL
;
336 // Get the stack address
338 BaseAddress
= MiPteToAddress(StackPte
+ StackPtes
+ 1);
341 // Select the right PTE address where we actually start committing pages
343 PointerPte
= StackPte
;
344 if (GuiStack
) PointerPte
+= BYTES_TO_PAGES(KERNEL_LARGE_STACK_SIZE
-
345 KERNEL_LARGE_STACK_COMMIT
);
348 /* Setup the temporary invalid PTE */
349 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
351 /* Setup the template stack PTE */
352 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, PointerPte
+ 1, MM_READWRITE
, 0);
355 // Acquire the PFN DB lock
357 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
360 // Loop each stack page
362 for (i
= 0; i
< StackPages
; i
++)
369 /* Get a page and write the current invalid PTE */
370 MI_SET_USAGE(MI_USAGE_KERNEL_STACK
);
371 MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName
);
372 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
373 MI_WRITE_INVALID_PTE(PointerPte
, InvalidPte
);
375 /* Initialize the PFN entry for this page */
376 MiInitializePfn(PageFrameIndex
, PointerPte
, 1);
378 /* Write the valid PTE */
379 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameIndex
;
380 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
384 // Release the PFN lock
386 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
389 // Return the stack address
396 MmGrowKernelStackEx(IN PVOID StackPointer
,
399 PKTHREAD Thread
= KeGetCurrentThread();
400 PMMPTE LimitPte
, NewLimitPte
, LastPte
;
402 MMPTE TempPte
, InvalidPte
;
403 PFN_NUMBER PageFrameIndex
;
406 // Make sure the stack did not overflow
408 ASSERT(((ULONG_PTR
)Thread
->StackBase
- (ULONG_PTR
)Thread
->StackLimit
) <=
409 (KERNEL_LARGE_STACK_SIZE
+ PAGE_SIZE
));
412 // Get the current stack limit
414 LimitPte
= MiAddressToPte(Thread
->StackLimit
);
415 ASSERT(LimitPte
->u
.Hard
.Valid
== 1);
418 // Get the new one and make sure this isn't a retarded request
420 NewLimitPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)StackPointer
- GrowSize
));
421 if (NewLimitPte
== LimitPte
) return STATUS_SUCCESS
;
424 // Now make sure you're not going past the reserved space
426 LastPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)Thread
->StackBase
-
427 KERNEL_LARGE_STACK_SIZE
));
428 if (NewLimitPte
< LastPte
)
433 DPRINT1("Thread wants too much stack\n");
434 return STATUS_STACK_OVERFLOW
;
438 // Calculate the number of new pages
442 /* Setup the temporary invalid PTE */
443 MI_MAKE_SOFTWARE_PTE(&InvalidPte
, MM_NOACCESS
);
446 // Acquire the PFN DB lock
448 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
451 // Loop each stack page
453 while (LimitPte
>= NewLimitPte
)
455 /* Get a page and write the current invalid PTE */
456 MI_SET_USAGE(MI_USAGE_KERNEL_STACK_EXPANSION
);
457 MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName
);
458 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_COLOR());
459 MI_WRITE_INVALID_PTE(LimitPte
, InvalidPte
);
461 /* Initialize the PFN entry for this page */
462 MiInitializePfn(PageFrameIndex
, LimitPte
, 1);
464 /* Setup the template stack PTE */
465 MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte
, LimitPte
, MM_READWRITE
, PageFrameIndex
);
467 /* Write the valid PTE */
468 MI_WRITE_VALID_PTE(LimitPte
--, TempPte
);
472 // Release the PFN lock
474 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
479 Thread
->StackLimit
= (ULONG_PTR
)MiPteToAddress(NewLimitPte
);
480 return STATUS_SUCCESS
;
485 MmGrowKernelStack(IN PVOID StackPointer
)
488 // Call the extended version
490 return MmGrowKernelStackEx(StackPointer
, KERNEL_LARGE_STACK_COMMIT
);
495 MmSetMemoryPriorityProcess(IN PEPROCESS Process
,
496 IN UCHAR MemoryPriority
)
501 // Check if we have less then 16MB of Physical Memory
503 if ((MmSystemSize
== MmSmallSystem
) &&
504 (MmNumberOfPhysicalPages
< ((15 * 1024 * 1024) / PAGE_SIZE
)))
507 // Always use background priority
509 MemoryPriority
= MEMORY_PRIORITY_BACKGROUND
;
513 // Save the old priority and update it
515 OldPriority
= (UCHAR
)Process
->Vm
.Flags
.MemoryPriority
;
516 Process
->Vm
.Flags
.MemoryPriority
= MemoryPriority
;
519 // Return the old priority
526 MmGetSessionLocaleId(VOID
)
532 // Get the current process
534 Process
= PsGetCurrentProcess();
537 // Check if it's the Session Leader
539 if (Process
->Vm
.Flags
.SessionLeader
)
542 // Make sure it has a valid Session
544 if (Process
->Session
)
550 return ((PMM_SESSION_SPACE
)Process
->Session
)->LocaleId
;
556 // Not a session leader, return the default
558 return PsDefaultThreadLocaleId
;
563 MmCreatePeb(IN PEPROCESS Process
,
564 IN PINITIAL_PEB InitialPeb
,
568 LARGE_INTEGER SectionOffset
;
570 PVOID TableBase
= NULL
;
571 PIMAGE_NT_HEADERS NtHeaders
;
572 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
574 USHORT Characteristics
;
575 KAFFINITY ProcessAffinityMask
= 0;
576 SectionOffset
.QuadPart
= (ULONGLONG
)0;
582 KeAttachProcess(&Process
->Pcb
);
587 Status
= MmMapViewOfSection(ExpNlsSectionPointer
,
597 DPRINT("NLS Tables at: %p\n", TableBase
);
598 if (!NT_SUCCESS(Status
))
600 /* Cleanup and exit */
608 Status
= MiCreatePebOrTeb(Process
, sizeof(PEB
), (PULONG_PTR
)&Peb
);
609 DPRINT("PEB at: %p\n", Peb
);
610 if (!NT_SUCCESS(Status
))
612 /* Cleanup and exit */
618 // Use SEH in case we can't load the PEB
623 // Initialize the PEB
625 RtlZeroMemory(Peb
, sizeof(PEB
));
630 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
631 Peb
->InheritedAddressSpace
= InitialPeb
->InheritedAddressSpace
;
632 Peb
->Mutant
= InitialPeb
->Mutant
;
633 Peb
->ImageUsesLargePages
= InitialPeb
->ImageUsesLargePages
;
638 Peb
->AnsiCodePageData
= (PCHAR
)TableBase
+ ExpAnsiCodePageDataOffset
;
639 Peb
->OemCodePageData
= (PCHAR
)TableBase
+ ExpOemCodePageDataOffset
;
640 Peb
->UnicodeCaseTableData
= (PCHAR
)TableBase
+ ExpUnicodeCaseTableDataOffset
;
643 // Default Version Data (could get changed below)
645 Peb
->OSMajorVersion
= NtMajorVersion
;
646 Peb
->OSMinorVersion
= NtMinorVersion
;
647 Peb
->OSBuildNumber
= (USHORT
)(NtBuildNumber
& 0x3FFF);
648 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
649 Peb
->OSCSDVersion
= (USHORT
)CmNtCSDVersion
;
652 // Heap and Debug Data
654 Peb
->NumberOfProcessors
= KeNumberProcessors
;
655 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
);
656 Peb
->NtGlobalFlag
= NtGlobalFlag
;
657 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
658 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
659 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
660 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;
661 Peb->CriticalSectionTimeout = MmCriticalSectionTimeout;
662 Peb->MinimumStackCommit = MmMinimumStackCommitInBytes;
664 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
665 Peb
->ProcessHeaps
= (PVOID
*)(Peb
+ 1);
670 if (Process
->Session
) Peb
->SessionId
= 0; // MmGetSessionId(Process);
672 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
678 _SEH2_YIELD(return _SEH2_GetExceptionCode());
683 // Use SEH in case we can't load the image
690 NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
);
691 Characteristics
= NtHeaders
->FileHeader
.Characteristics
;
693 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
699 _SEH2_YIELD(return STATUS_INVALID_IMAGE_PROTECT
);
709 // Use SEH in case we can't load the headers
714 // Get the Image Config Data too
716 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
718 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
725 ProbeForRead(ImageConfigData
,
726 sizeof(IMAGE_LOAD_CONFIG_DIRECTORY
),
731 // Write subsystem data
733 Peb
->ImageSubsystem
= NtHeaders
->OptionalHeader
.Subsystem
;
734 Peb
->ImageSubsystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
735 Peb
->ImageSubsystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
738 // Check for version data
740 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
743 // Extract values and write them
745 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
746 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
747 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
748 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
750 /* Process CSD version override */
751 if ((ImageConfigData
) && (ImageConfigData
->CSDVersion
))
753 /* Take the value from the image configuration directory */
754 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
758 /* Process optional process affinity mask override */
759 if ((ImageConfigData
) && (ImageConfigData
->ProcessAffinityMask
))
761 /* Take the value from the image configuration directory */
762 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
766 // Check if this is a UP image
767 if (Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
770 // Force it to use CPU 0
772 /* FIXME: this should use the MmRotatingUniprocessorNumber */
773 Peb
->ImageProcessAffinityMask
= 0;
778 // Whatever was configured
780 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
783 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
789 _SEH2_YIELD(return STATUS_INVALID_IMAGE_PROTECT
);
795 // Detach from the Process
799 return STATUS_SUCCESS
;
804 MmCreateTeb(IN PEPROCESS Process
,
805 IN PCLIENT_ID ClientId
,
806 IN PINITIAL_TEB InitialTeb
,
810 NTSTATUS Status
= STATUS_SUCCESS
;
816 KeAttachProcess(&Process
->Pcb
);
821 Status
= MiCreatePebOrTeb(Process
, sizeof(TEB
), (PULONG_PTR
)&Teb
);
822 ASSERT(NT_SUCCESS(Status
));
825 // Use SEH in case we can't load the TEB
830 // Initialize the PEB
832 RtlZeroMemory(Teb
, sizeof(TEB
));
838 Teb
->NtTib
.ExceptionList
= NULL
;
840 Teb
->NtTib
.ExceptionList
= EXCEPTION_CHAIN_END
;
842 Teb
->NtTib
.Self
= (PNT_TIB
)Teb
;
845 // Identify this as an OS/2 V3.0 ("Cruiser") TIB
847 Teb
->NtTib
.Version
= 30 << 8;
852 Teb
->ClientId
= *ClientId
;
853 Teb
->RealClientId
= *ClientId
;
854 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
855 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
858 // Check if we have a grandparent TEB
860 if ((InitialTeb
->PreviousStackBase
== NULL
) &&
861 (InitialTeb
->PreviousStackLimit
== NULL
))
864 // Use initial TEB values
866 Teb
->NtTib
.StackBase
= InitialTeb
->StackBase
;
867 Teb
->NtTib
.StackLimit
= InitialTeb
->StackLimit
;
868 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
873 // Use grandparent TEB values
875 Teb
->NtTib
.StackBase
= InitialTeb
->PreviousStackBase
;
876 Teb
->NtTib
.StackLimit
= InitialTeb
->PreviousStackLimit
;
880 // Initialize the static unicode string
882 Teb
->StaticUnicodeString
.MaximumLength
= sizeof(Teb
->StaticUnicodeBuffer
);
883 Teb
->StaticUnicodeString
.Buffer
= Teb
->StaticUnicodeBuffer
;
885 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
890 Status
= _SEH2_GetExceptionCode();
904 MiInitializeWorkingSetList(IN PEPROCESS CurrentProcess
)
908 /* Setup some bogus list data */
909 MmWorkingSetList
->LastEntry
= CurrentProcess
->Vm
.MinimumWorkingSetSize
;
910 MmWorkingSetList
->HashTable
= NULL
;
911 MmWorkingSetList
->HashTableSize
= 0;
912 MmWorkingSetList
->NumberOfImageWaiters
= 0;
913 MmWorkingSetList
->Wsle
= (PVOID
)0xDEADBABE;
914 MmWorkingSetList
->VadBitMapHint
= 1;
915 MmWorkingSetList
->HashTableStart
= (PVOID
)0xBADAB00B;
916 MmWorkingSetList
->HighestPermittedHashAddress
= (PVOID
)0xCAFEBABE;
917 MmWorkingSetList
->FirstFree
= 1;
918 MmWorkingSetList
->FirstDynamic
= 2;
919 MmWorkingSetList
->NextSlot
= 3;
920 MmWorkingSetList
->LastInitializedWsle
= 4;
922 /* The rule is that the owner process is always in the FLINK of the PDE's PFN entry */
923 Pfn1
= MiGetPfnEntry(CurrentProcess
->Pcb
.DirectoryTableBase
[0] >> PAGE_SHIFT
);
924 ASSERT(Pfn1
->u4
.PteFrame
== MiGetPfnEntryIndex(Pfn1
));
925 Pfn1
->u1
.Event
= (PKEVENT
)CurrentProcess
;
930 MmInitializeProcessAddressSpace(IN PEPROCESS Process
,
931 IN PEPROCESS ProcessClone OPTIONAL
,
932 IN PVOID Section OPTIONAL
,
934 IN POBJECT_NAME_INFORMATION
*AuditName OPTIONAL
)
936 NTSTATUS Status
= STATUS_SUCCESS
;
939 PROS_SECTION_OBJECT SectionObject
= Section
;
943 PFN_NUMBER PageFrameNumber
;
944 UNICODE_STRING FileName
;
950 /* We should have a PDE */
951 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] != 0);
952 ASSERT(Process
->PdeUpdateNeeded
== FALSE
);
954 /* Attach to the process */
955 KeAttachProcess(&Process
->Pcb
);
957 /* The address space should now been in phase 1 or 0 */
958 ASSERT(Process
->AddressSpaceInitialized
<= 1);
959 Process
->AddressSpaceInitialized
= 2;
961 /* Initialize the Addresss Space lock */
962 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
963 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
965 /* Initialize AVL tree */
966 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
967 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
969 /* Lock PFN database */
970 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
972 /* Setup the PFN for the PDE base of this process */
974 PointerPte
= MiAddressToPte(PXE_BASE
);
976 PointerPte
= MiAddressToPte(PDE_BASE
);
978 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
979 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] == PageFrameNumber
* PAGE_SIZE
);
980 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
982 /* Do the same for hyperspace */
984 PointerPde
= MiAddressToPxe((PVOID
)HYPER_SPACE
);
986 PointerPde
= MiAddressToPde(HYPER_SPACE
);
988 PageFrameNumber
= PFN_FROM_PTE(PointerPde
);
989 //ASSERT(Process->Pcb.DirectoryTableBase[0] == PageFrameNumber * PAGE_SIZE); // we're not lucky
990 MiInitializePfn(PageFrameNumber
, (PMMPTE
)PointerPde
, TRUE
);
992 /* Setup the PFN for the PTE for the working set */
993 PointerPte
= MiAddressToPte(MI_WORKING_SET_LIST
);
994 MI_MAKE_HARDWARE_PTE(&TempPte
, PointerPte
, MM_READWRITE
, 0);
995 ASSERT(PointerPte
->u
.Long
!= 0);
996 PageFrameNumber
= PFN_FROM_PTE(PointerPte
);
997 MI_WRITE_INVALID_PTE(PointerPte
, DemandZeroPte
);
998 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
999 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
1000 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
1002 /* Now initialize the working set list */
1003 MiInitializeWorkingSetList(Process
);
1006 ASSERT(Process
->PhysicalVadRoot
== NULL
);
1008 /* Release PFN lock */
1009 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1011 /* Lock the VAD, ARM3-owned ranges away */
1012 MiRosTakeOverSharedUserPage(Process
);
1014 /* Check if there's a Section Object */
1017 /* Determine the image file name and save it to EPROCESS */
1018 FileName
= SectionObject
->FileObject
->FileName
;
1019 Source
= (PWCHAR
)((PCHAR
)FileName
.Buffer
+ FileName
.Length
);
1020 if (FileName
.Buffer
)
1022 /* Loop the file name*/
1023 while (Source
> FileName
.Buffer
)
1025 /* Make sure this isn't a backslash */
1026 if (*--Source
== OBJ_NAME_PATH_SEPARATOR
)
1028 /* If so, stop it here */
1034 /* Otherwise, keep going */
1040 /* Copy the to the process and truncate it to 15 characters if necessary */
1041 Destination
= Process
->ImageFileName
;
1042 Length
= min(Length
, sizeof(Process
->ImageFileName
) - 1);
1043 while (Length
--) *Destination
++ = (UCHAR
)*Source
++;
1044 *Destination
= ANSI_NULL
;
1046 /* Check if caller wants an audit name */
1049 /* Setup the audit name */
1050 Status
= SeInitializeProcessAuditName(SectionObject
->FileObject
,
1053 if (!NT_SUCCESS(Status
))
1061 /* Map the section */
1062 Status
= MmMapViewOfSection(Section
,
1073 /* Save the pointer */
1074 Process
->SectionBaseAddress
= ImageBase
;
1077 /* Be nice and detach */
1080 /* Return status to caller */
1087 MmInitializeHandBuiltProcess(IN PEPROCESS Process
,
1088 IN PULONG_PTR DirectoryTableBase
)
1090 /* Share the directory base with the idle process */
1091 DirectoryTableBase
[0] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[0];
1092 DirectoryTableBase
[1] = PsGetCurrentProcess()->Pcb
.DirectoryTableBase
[1];
1094 /* Initialize the Addresss Space */
1095 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
1096 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
1097 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
1098 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
1099 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
1102 Process
->HasAddressSpace
= TRUE
;//??
1103 return STATUS_SUCCESS
;
1109 MmInitializeHandBuiltProcess2(IN PEPROCESS Process
)
1111 /* Lock the VAD, ARM3-owned ranges away */
1112 MiRosTakeOverSharedUserPage(Process
);
1113 return STATUS_SUCCESS
;
1117 /* FIXME: Evaluate ways to make this portable yet arch-specific */
1120 MmCreateProcessAddressSpace(IN ULONG MinWs
,
1121 IN PEPROCESS Process
,
1122 OUT PULONG_PTR DirectoryTableBase
)
1125 PFN_NUMBER PdeIndex
, HyperIndex
, WsListIndex
;
1127 MMPTE TempPte
, PdePte
;
1129 PMMPTE SystemTable
, HyperTable
;
1133 /* Choose a process color */
1134 Process
->NextPageColor
= (USHORT
)RtlRandom(&MmProcessColorSeed
);
1136 /* Setup the hyperspace lock */
1137 KeInitializeSpinLock(&Process
->HyperSpaceLock
);
1139 /* Lock PFN database */
1140 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1142 /* Get a zero page for the PDE, if possible */
1143 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1144 MI_SET_USAGE(MI_USAGE_PAGE_DIRECTORY
);
1145 PdeIndex
= MiRemoveZeroPageSafe(Color
);
1148 /* No zero pages, grab a free one */
1149 PdeIndex
= MiRemoveAnyPage(Color
);
1151 /* Zero it outside the PFN lock */
1152 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1153 MiZeroPhysicalPage(PdeIndex
);
1154 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1157 /* Get a zero page for hyperspace, if possible */
1158 MI_SET_USAGE(MI_USAGE_PAGE_DIRECTORY
);
1159 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1160 HyperIndex
= MiRemoveZeroPageSafe(Color
);
1163 /* No zero pages, grab a free one */
1164 HyperIndex
= MiRemoveAnyPage(Color
);
1166 /* Zero it outside the PFN lock */
1167 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1168 MiZeroPhysicalPage(HyperIndex
);
1169 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1172 /* Get a zero page for the woring set list, if possible */
1173 MI_SET_USAGE(MI_USAGE_PAGE_TABLE
);
1174 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1175 WsListIndex
= MiRemoveZeroPageSafe(Color
);
1178 /* No zero pages, grab a free one */
1179 WsListIndex
= MiRemoveAnyPage(Color
);
1181 /* Zero it outside the PFN lock */
1182 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1183 MiZeroPhysicalPage(WsListIndex
);
1187 /* Release the PFN lock */
1188 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1191 /* Switch to phase 1 initialization */
1192 ASSERT(Process
->AddressSpaceInitialized
== 0);
1193 Process
->AddressSpaceInitialized
= 1;
1195 /* Set the base directory pointers */
1196 Process
->WorkingSetPage
= WsListIndex
;
1197 DirectoryTableBase
[0] = PdeIndex
<< PAGE_SHIFT
;
1198 DirectoryTableBase
[1] = HyperIndex
<< PAGE_SHIFT
;
1200 /* Make sure we don't already have a page directory setup */
1201 ASSERT(Process
->Pcb
.DirectoryTableBase
[0] == 0);
1203 /* Get a PTE to map hyperspace */
1204 PointerPte
= MiReserveSystemPtes(1, SystemPteSpace
);
1205 ASSERT(PointerPte
!= NULL
);
1208 MI_MAKE_HARDWARE_PTE_KERNEL(&PdePte
,
1213 /* Set it dirty and map it */
1214 MI_MAKE_DIRTY_PAGE(&PdePte
);
1215 MI_WRITE_VALID_PTE(PointerPte
, PdePte
);
1217 /* Now get hyperspace's page table */
1218 HyperTable
= MiPteToAddress(PointerPte
);
1220 /* Now write the PTE/PDE entry for the working set list index itself */
1221 TempPte
= ValidKernelPte
;
1222 TempPte
.u
.Hard
.PageFrameNumber
= WsListIndex
;
1223 PdeOffset
= MiAddressToPteOffset(MmWorkingSetList
);
1224 HyperTable
[PdeOffset
] = TempPte
;
1226 /* Let go of the system PTE */
1227 MiReleaseSystemPtes(PointerPte
, 1, SystemPteSpace
);
1229 /* Save the PTE address of the page directory itself */
1230 Pfn1
= MiGetPfnEntry(PdeIndex
);
1231 Pfn1
->PteAddress
= (PMMPTE
)PDE_BASE
;
1233 /* Insert us into the Mm process list */
1234 InsertTailList(&MmProcessList
, &Process
->MmProcessLinks
);
1236 /* Get a PTE to map the page directory */
1237 PointerPte
= MiReserveSystemPtes(1, SystemPteSpace
);
1238 ASSERT(PointerPte
!= NULL
);
1241 MI_MAKE_HARDWARE_PTE_KERNEL(&PdePte
,
1246 /* Set it dirty and map it */
1247 MI_MAKE_DIRTY_PAGE(&PdePte
);
1248 MI_WRITE_VALID_PTE(PointerPte
, PdePte
);
1250 /* Now get the page directory (which we'll double map, so call it a page table */
1251 SystemTable
= MiPteToAddress(PointerPte
);
1253 /* Copy all the kernel mappings */
1254 PdeOffset
= MiGetPdeOffset(MmSystemRangeStart
);
1255 RtlCopyMemory(&SystemTable
[PdeOffset
],
1256 MiAddressToPde(MmSystemRangeStart
),
1257 PAGE_SIZE
- PdeOffset
* sizeof(MMPTE
));
1259 /* Now write the PTE/PDE entry for hyperspace itself */
1260 TempPte
= ValidKernelPte
;
1261 TempPte
.u
.Hard
.PageFrameNumber
= HyperIndex
;
1262 PdeOffset
= MiGetPdeOffset(HYPER_SPACE
);
1263 SystemTable
[PdeOffset
] = TempPte
;
1267 ASSERT(MiGetPdeOffset(MmHyperSpaceEnd
) >= PdeOffset
);
1269 /* Now do the x86 trick of making the PDE a page table itself */
1270 PdeOffset
= MiGetPdeOffset(PTE_BASE
);
1271 TempPte
.u
.Hard
.PageFrameNumber
= PdeIndex
;
1272 SystemTable
[PdeOffset
] = TempPte
;
1274 /* Let go of the system PTE */
1275 MiReleaseSystemPtes(PointerPte
, 1, SystemPteSpace
);
1282 MmCleanProcessAddressSpace(IN PEPROCESS Process
)
1285 PMM_AVL_TABLE VadTree
;
1286 PETHREAD Thread
= PsGetCurrentThread();
1288 /* Only support this */
1289 ASSERT(Process
->AddressSpaceInitialized
== 2);
1291 /* Lock the process address space from changes */
1292 MmLockAddressSpace(&Process
->Vm
);
1294 /* VM is deleted now */
1295 Process
->VmDeleted
= TRUE
;
1297 /* Enumerate the VADs */
1298 VadTree
= &Process
->VadRoot
;
1299 while (VadTree
->NumberGenericTableElements
)
1301 /* Grab the current VAD */
1302 Vad
= (PMMVAD
)VadTree
->BalancedRoot
.RightChild
;
1304 /* Lock the working set */
1305 MiLockProcessWorkingSet(Process
, Thread
);
1307 /* Remove this VAD from the tree */
1308 ASSERT(VadTree
->NumberGenericTableElements
>= 1);
1309 MiRemoveNode((PMMADDRESS_NODE
)Vad
, VadTree
);
1311 /* Only regular VADs supported for now */
1312 ASSERT(Vad
->u
.VadFlags
.VadType
== VadNone
);
1314 /* Check if this is a section VAD */
1315 if (!(Vad
->u
.VadFlags
.PrivateMemory
) && (Vad
->ControlArea
))
1317 /* Remove the view */
1318 MiRemoveMappedView(Process
, Vad
);
1322 /* Delete the addresses */
1323 MiDeleteVirtualAddresses(Vad
->StartingVpn
<< PAGE_SHIFT
,
1324 (Vad
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1),
1327 /* Release the working set */
1328 MiUnlockProcessWorkingSet(Process
, Thread
);
1331 /* Skip ARM3 fake VADs, they'll be freed by MmDeleteProcessAddresSpace */
1332 if (Vad
->u
.VadFlags
.Spare
== 1)
1334 /* Set a flag so MmDeleteMemoryArea knows to free, but not to remove */
1335 Vad
->u
.VadFlags
.Spare
= 2;
1339 /* Free the VAD memory */
1343 /* Release the address space */
1344 MmUnlockAddressSpace(&Process
->Vm
);
1349 MmDeleteProcessAddressSpace2(IN PEPROCESS Process
)
1353 PFN_NUMBER PageFrameIndex
;
1357 //ASSERT(Process->CommitCharge == 0);
1359 /* Delete the shared user data section (Should be done in clean, not delete) */
1360 ASSERT(MmHighestUserAddress
> (PVOID
)USER_SHARED_DATA
);
1361 KeAttachProcess(&Process
->Pcb
);
1362 //DPRINT1("Killing shared user data page no longer works -- has someone changed ARM3 in a way to make this fail now?\n");
1363 //MiDeleteVirtualAddresses(USER_SHARED_DATA, USER_SHARED_DATA, NULL);
1364 //DPRINT1("Done\n");
1367 /* Acquire the PFN lock */
1368 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1370 /* Check for fully initialized process */
1371 if (Process
->AddressSpaceInitialized
== 2)
1374 PageDir
= MmCreateHyperspaceMapping(Process
->Pcb
.DirectoryTableBase
[0] >> PAGE_SHIFT
);
1375 for (i
= 0; i
< ADDR_TO_PDE_OFFSET(MmSystemRangeStart
); i
++)
1377 /* Loop all page tables */
1378 if (PageDir
[i
] != 0)
1380 /* Check if they are RosMm-owned */
1381 if (MI_IS_ROS_PFN(MiGetPfnEntry(PageDir
[i
] >> PAGE_SHIFT
)))
1383 /* Free them through the RosMm ABI */
1384 //DPRINT1("Freeing MC_SYSTEM: %lx\n", PageDir[i]);
1385 MmReleasePageMemoryConsumer(MC_SYSTEM
, PageDir
[i
] >> PAGE_SHIFT
);
1391 //DPRINT1("Done\n");
1392 MmDeleteHyperspaceMapping(PageDir
);
1394 /* Map the working set page and its page table */
1395 Pfn1
= MiGetPfnEntry(Process
->WorkingSetPage
);
1396 Pfn2
= MiGetPfnEntry(Pfn1
->u4
.PteFrame
);
1399 MI_SET_PFN_DELETED(Pfn1
);
1400 MiDecrementShareCount(Pfn2
, Pfn1
->u4
.PteFrame
);
1401 MiDecrementShareCount(Pfn1
, Process
->WorkingSetPage
);
1402 ASSERT((Pfn1
->u3
.e2
.ReferenceCount
== 0) || (Pfn1
->u3
.e1
.WriteInProgress
));
1404 /* Now map hyperspace and its page table */
1405 PageFrameIndex
= Process
->Pcb
.DirectoryTableBase
[1] >> PAGE_SHIFT
;
1406 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
1407 Pfn2
= MiGetPfnEntry(Pfn1
->u4
.PteFrame
);
1410 MI_SET_PFN_DELETED(Pfn1
);
1411 MiDecrementShareCount(Pfn2
, Pfn1
->u4
.PteFrame
);
1412 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1413 ASSERT((Pfn1
->u3
.e2
.ReferenceCount
== 0) || (Pfn1
->u3
.e1
.WriteInProgress
));
1415 /* Finally, nuke the PDE itself */
1416 PageFrameIndex
= Process
->Pcb
.DirectoryTableBase
[0] >> PAGE_SHIFT
;
1417 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
1418 MI_SET_PFN_DELETED(Pfn1
);
1419 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1420 MiDecrementShareCount(Pfn1
, PageFrameIndex
);
1422 /* HACK: In Richard's original patch this ASSERT did work */
1423 //DPRINT1("Ref count: %lx %lx\n", Pfn1->u3.e2.ReferenceCount, Pfn1->u2.ShareCount);
1424 //ASSERT((Pfn1->u3.e2.ReferenceCount == 0) || (Pfn1->u3.e1.WriteInProgress));
1428 /* A partly-initialized process should never exit through here */
1432 /* Release the PFN lock */
1433 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1435 /* No support for sessions yet */
1436 ASSERT(Process
->Session
== 0);
1438 /* Clear out the PDE pages */
1439 Process
->Pcb
.DirectoryTableBase
[0] = 0;
1440 Process
->Pcb
.DirectoryTableBase
[1] = 0;
1443 /* SESSION CODE TO MOVE TO SESSION.C ******************************************/
1445 KGUARDED_MUTEX MiSessionIdMutex
;
1446 PRTL_BITMAP MiSessionIdBitmap
;
1447 volatile LONG MiSessionLeaderExists
;
1451 MiInitializeSessionIds(VOID
)
1453 /* FIXME: Other stuff should go here */
1455 /* Initialize the lock */
1456 KeInitializeGuardedMutex(&MiSessionIdMutex
);
1458 /* Allocate the bitmap */
1459 MiSessionIdBitmap
= ExAllocatePoolWithTag(PagedPool
,
1460 sizeof(RTL_BITMAP
) + ((64 + 31) / 32) * 4,
1462 if (MiSessionIdBitmap
)
1464 /* Free all the bits */
1465 RtlInitializeBitMap(MiSessionIdBitmap
, (PVOID
)(MiSessionIdBitmap
+ 1), 64);
1466 RtlClearAllBits(MiSessionIdBitmap
);
1470 /* Die if we couldn't allocate the bitmap */
1471 KeBugCheckEx(INSTALL_MORE_MEMORY
,
1472 MmNumberOfPhysicalPages
,
1473 MmLowestPhysicalPage
,
1474 MmHighestPhysicalPage
,
1481 MiSessionLeader(IN PEPROCESS Process
)
1485 /* Set the flag while under the expansion lock */
1486 OldIrql
= KeAcquireQueuedSpinLock(LockQueueExpansionLock
);
1487 Process
->Vm
.Flags
.SessionLeader
= TRUE
;
1488 KeReleaseQueuedSpinLock(LockQueueExpansionLock
, OldIrql
);
1493 MiSessionCreateInternal(OUT PULONG SessionId
)
1495 PEPROCESS Process
= PsGetCurrentProcess();
1496 ULONG NewFlags
, Flags
;
1498 /* Loop so we can set the session-is-creating flag */
1499 Flags
= Process
->Flags
;
1502 /* Check if it's already set */
1503 if (Flags
& PSF_SESSION_CREATION_UNDERWAY_BIT
)
1506 DPRINT1("Lost session race\n");
1507 return STATUS_ALREADY_COMMITTED
;
1510 /* Now try to set it */
1511 NewFlags
= InterlockedCompareExchange((PLONG
)&Process
->Flags
,
1512 Flags
| PSF_SESSION_CREATION_UNDERWAY_BIT
,
1514 if (NewFlags
== Flags
) break;
1516 /* It changed, try again */
1520 /* Now we should own the flag */
1521 ASSERT(Process
->Flags
& PSF_SESSION_CREATION_UNDERWAY_BIT
);
1523 /* Allocate a new Session ID */
1524 KeAcquireGuardedMutex(&MiSessionIdMutex
);
1525 *SessionId
= RtlFindClearBitsAndSet(MiSessionIdBitmap
, 1, 0);
1526 if (*SessionId
== 0xFFFFFFFF)
1528 DPRINT1("Too many sessions created. Expansion not yet supported\n");
1529 return STATUS_NO_MEMORY
;
1531 KeReleaseGuardedMutex(&MiSessionIdMutex
);
1533 /* We're done, clear the flag */
1534 ASSERT(Process
->Flags
& PSF_SESSION_CREATION_UNDERWAY_BIT
);
1535 PspClearProcessFlag(Process
, PSF_SESSION_CREATION_UNDERWAY_BIT
);
1536 return STATUS_SUCCESS
;
1541 MmSessionCreate(OUT PULONG SessionId
)
1543 PEPROCESS Process
= PsGetCurrentProcess();
1544 ULONG SessionLeaderExists
;
1547 /* Fail if the process is already in a session */
1548 if (Process
->Flags
& PSF_PROCESS_IN_SESSION_BIT
)
1550 DPRINT1("Process already in session\n");
1551 return STATUS_ALREADY_COMMITTED
;
1554 /* Check if the process is already the session leader */
1555 if (!Process
->Vm
.Flags
.SessionLeader
)
1557 /* Atomically set it as the leader */
1558 SessionLeaderExists
= InterlockedCompareExchange(&MiSessionLeaderExists
, 1, 0);
1559 if (SessionLeaderExists
)
1561 DPRINT1("Session leader race\n");
1562 return STATUS_INVALID_SYSTEM_SERVICE
;
1565 /* Do the work required to upgrade him */
1566 MiSessionLeader(Process
);
1569 /* FIXME: Actually create a session */
1570 KeEnterCriticalRegion();
1571 Status
= MiSessionCreateInternal(SessionId
);
1572 KeLeaveCriticalRegion();
1574 /* Set and assert the flags, and return */
1575 PspSetProcessFlag(Process
, PSF_PROCESS_IN_SESSION_BIT
);
1576 ASSERT(MiSessionLeaderExists
== 1);
1577 if (NT_SUCCESS(Status
)) DPRINT1("New session created: %lx\n", *SessionId
);
1583 MmSessionDelete(IN ULONG SessionId
)
1585 PEPROCESS Process
= PsGetCurrentProcess();
1587 /* Process must be in a session */
1588 if (!(Process
->Flags
& PSF_PROCESS_IN_SESSION_BIT
))
1590 DPRINT1("Not in a session!\n");
1591 return STATUS_UNABLE_TO_FREE_VM
;
1594 /* It must be the session leader */
1595 if (!Process
->Vm
.Flags
.SessionLeader
)
1597 DPRINT1("Not a session leader!\n");
1598 return STATUS_UNABLE_TO_FREE_VM
;
1601 /* Remove one reference count */
1602 KeEnterCriticalRegion();
1604 KeLeaveCriticalRegion();
1607 return STATUS_SUCCESS
;
1610 /* SYSTEM CALLS ***************************************************************/
1614 NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle
,
1615 IN OUT PULONG_PTR NumberOfPages
,
1616 IN OUT PULONG_PTR UserPfnArray
)
1619 return STATUS_NOT_IMPLEMENTED
;
1624 NtMapUserPhysicalPages(IN PVOID VirtualAddresses
,
1625 IN ULONG_PTR NumberOfPages
,
1626 IN OUT PULONG_PTR UserPfnArray
)
1629 return STATUS_NOT_IMPLEMENTED
;
1634 NtMapUserPhysicalPagesScatter(IN PVOID
*VirtualAddresses
,
1635 IN ULONG_PTR NumberOfPages
,
1636 IN OUT PULONG_PTR UserPfnArray
)
1639 return STATUS_NOT_IMPLEMENTED
;
1644 NtFreeUserPhysicalPages(IN HANDLE ProcessHandle
,
1645 IN OUT PULONG_PTR NumberOfPages
,
1646 IN OUT PULONG_PTR UserPfnArray
)
1649 return STATUS_NOT_IMPLEMENTED
;