2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/procsup.c
5 * PURPOSE: Memory functions related to Processes
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *****************************************************************/
16 VOID NTAPI
MiRosTakeOverPebTebRanges(IN PEPROCESS Process
);
18 /* FUNCTIONS *****************************************************************/
22 MmInitializeHandBuiltProcess2(IN PEPROCESS Process
)
25 PMEMORY_AREA MemoryArea
;
26 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
28 PMMSUPPORT ProcessAddressSpace
= &Process
->Vm
;
29 BoundaryAddressMultiple
.QuadPart
= 0;
31 /* Create the shared data page */
32 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
33 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
34 MEMORY_AREA_SHARED_DATA
,
41 BoundaryAddressMultiple
);
43 /* Lock the VAD, ARM3-owned ranges away */
44 MiRosTakeOverPebTebRanges(Process
);
50 MmInitializeProcessAddressSpace(IN PEPROCESS Process
,
51 IN PEPROCESS ProcessClone OPTIONAL
,
52 IN PVOID Section OPTIONAL
,
54 IN POBJECT_NAME_INFORMATION
*AuditName OPTIONAL
)
57 PMMSUPPORT ProcessAddressSpace
= &Process
->Vm
;
59 PMEMORY_AREA MemoryArea
;
60 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
63 PROS_SECTION_OBJECT SectionObject
= Section
;
64 BoundaryAddressMultiple
.QuadPart
= 0;
66 /* Initialize the Addresss Space lock */
67 KeInitializeGuardedMutex(&Process
->AddressCreationLock
);
68 Process
->Vm
.WorkingSetExpansionLinks
.Flink
= NULL
;
70 /* Initialize AVL tree */
71 ASSERT(Process
->VadRoot
.NumberGenericTableElements
== 0);
72 Process
->VadRoot
.BalancedRoot
.u1
.Parent
= &Process
->VadRoot
.BalancedRoot
;
74 /* Acquire the Lock */
75 MmLockAddressSpace(ProcessAddressSpace
);
77 /* Protect the highest 64KB of the process address space */
78 BaseAddress
= (PVOID
)MmUserProbeAddress
;
79 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
80 MEMORY_AREA_NO_ACCESS
,
87 BoundaryAddressMultiple
);
88 if (!NT_SUCCESS(Status
))
90 DPRINT1("Failed to protect last 64KB\n");
94 /* Protect the 60KB above the shared user page */
95 BaseAddress
= (char*)USER_SHARED_DATA
+ PAGE_SIZE
;
96 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
97 MEMORY_AREA_NO_ACCESS
,
104 BoundaryAddressMultiple
);
105 if (!NT_SUCCESS(Status
))
107 DPRINT1("Failed to protect the memory above the shared user page\n");
111 /* Create the shared data page */
112 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
113 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
114 MEMORY_AREA_SHARED_DATA
,
121 BoundaryAddressMultiple
);
122 if (!NT_SUCCESS(Status
))
124 DPRINT1("Failed to create Shared User Data\n");
128 /* Lock the VAD, ARM3-owned ranges away */
129 MiRosTakeOverPebTebRanges(Process
);
131 /* The process now has an address space */
132 Process
->HasAddressSpace
= TRUE
;
134 /* Check if there's a Section Object */
137 UNICODE_STRING FileName
;
142 /* Unlock the Address Space */
143 DPRINT("Unlocking\n");
144 MmUnlockAddressSpace(ProcessAddressSpace
);
146 DPRINT("Mapping process image. Section: %p, Process: %p, ImageBase: %p\n",
147 SectionObject
, Process
, &ImageBase
);
148 Status
= MmMapViewOfSection(Section
,
158 if (!NT_SUCCESS(Status
))
160 DPRINT1("Failed to map process Image\n");
164 /* Save the pointer */
165 Process
->SectionBaseAddress
= ImageBase
;
167 /* Determine the image file name and save it to EPROCESS */
168 DPRINT("Getting Image name\n");
169 FileName
= SectionObject
->FileObject
->FileName
;
170 szSrc
= (PWCHAR
)((PCHAR
)FileName
.Buffer
+ FileName
.Length
);
173 /* Loop the file name*/
174 while (szSrc
> FileName
.Buffer
)
176 /* Make sure this isn't a backslash */
177 if (*--szSrc
== OBJ_NAME_PATH_SEPARATOR
)
179 /* If so, stop it here */
185 /* Otherwise, keep going */
191 /* Copy the to the process and truncate it to 15 characters if necessary */
192 szDest
= Process
->ImageFileName
;
193 lnFName
= min(lnFName
, sizeof(Process
->ImageFileName
) - 1);
194 while (lnFName
--) *szDest
++ = (UCHAR
)*szSrc
++;
197 /* Check if caller wants an audit name */
200 /* Setup the audit name */
201 SeInitializeProcessAuditName(SectionObject
->FileObject
,
206 /* Return status to caller */
211 /* Unlock the Address Space */
212 DPRINT("Unlocking\n");
213 MmUnlockAddressSpace(ProcessAddressSpace
);
215 /* Return status to caller */
221 MmCleanProcessAddressSpace(IN PEPROCESS Process
)
223 /* FIXME: Add part of MmDeleteProcessAddressSpace here */
228 MmDeleteProcessAddressSpace(PEPROCESS Process
)
231 PMEMORY_AREA MemoryArea
;
233 DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process
,
234 Process
->ImageFileName
);
236 MmLockAddressSpace(&Process
->Vm
);
238 while ((MemoryArea
= (PMEMORY_AREA
)Process
->Vm
.WorkingSetExpansionLinks
.Flink
) != NULL
)
240 switch (MemoryArea
->Type
)
242 case MEMORY_AREA_SECTION_VIEW
:
243 Address
= (PVOID
)MemoryArea
->StartingAddress
;
244 MmUnlockAddressSpace(&Process
->Vm
);
245 MmUnmapViewOfSection(Process
, Address
);
246 MmLockAddressSpace(&Process
->Vm
);
249 case MEMORY_AREA_VIRTUAL_MEMORY
:
250 case MEMORY_AREA_PEB_OR_TEB
:
251 MmFreeVirtualMemory(Process
, MemoryArea
);
254 case MEMORY_AREA_SHARED_DATA
:
255 case MEMORY_AREA_NO_ACCESS
:
256 case MEMORY_AREA_OWNED_BY_ARM3
:
257 MmFreeMemoryArea(&Process
->Vm
,
263 case MEMORY_AREA_MDL_MAPPING
:
264 KeBugCheck(PROCESS_HAS_LOCKED_PAGES
);
268 KeBugCheck(MEMORY_MANAGEMENT
);
272 Mmi386ReleaseMmInfo(Process
);
274 MmUnlockAddressSpace(&Process
->Vm
);
276 DPRINT("Finished MmReleaseMmInfo()\n");
277 return(STATUS_SUCCESS
);