2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/process.c
5 * PURPOSE: Memory functions related to Processes
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *****************************************************************/
14 #include <internal/debug.h>
16 extern ULONG NtMajorVersion
;
17 extern ULONG NtMinorVersion
;
18 extern ULONG NtOSCSDVersion
;
19 extern ULONG NtGlobalFlag
;
21 #define MM_HIGHEST_VAD_ADDRESS \
22 (PVOID)((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (16 * PAGE_SIZE))
24 /* FUNCTIONS *****************************************************************/
28 MiCreatePebOrTeb(PEPROCESS Process
,
32 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
33 PMEMORY_AREA MemoryArea
;
34 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
35 PVOID AllocatedBase
= BaseAddress
;
36 BoundaryAddressMultiple
.QuadPart
= 0;
38 /* Acquire the Lock */
39 MmLockAddressSpace(ProcessAddressSpace
);
42 * Create a Peb or Teb.
43 * Loop until it works, decreasing by PAGE_SIZE each time. The logic here
44 * is that a PEB allocation should never fail since the address is free,
45 * while TEB allocation can fail, and we should simply try the address
46 * below. Is there a nicer way of doing this automagically? (ie: findning)
47 * a gap region? -- Alex
50 DPRINT("Trying to allocate: %x\n", AllocatedBase
);
51 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
52 MEMORY_AREA_PEB_OR_TEB
,
59 BoundaryAddressMultiple
);
60 AllocatedBase
= RVA(AllocatedBase
, -PAGE_SIZE
);
61 } while (Status
!= STATUS_SUCCESS
);
63 /* Initialize the Region */
64 MmInitializeRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
69 /* Reserve the pages */
70 MmReserveSwapPages(PAGE_SIZE
);
72 /* Unlock Address Space */
73 DPRINT("Returning\n");
74 MmUnlockAddressSpace(ProcessAddressSpace
);
75 return RVA(AllocatedBase
, PAGE_SIZE
);
79 MiFreeStackPage(PVOID Context
,
80 MEMORY_AREA
* MemoryArea
,
86 ASSERT(SwapEntry
== 0);
87 if (Page
) MmReleasePageMemoryConsumer(MC_NPPOOL
, Page
);
92 MmDeleteKernelStack(PVOID Stack
,
95 /* Lock the Address Space */
96 MmLockAddressSpace(MmGetKernelAddressSpace());
98 /* Delete the Stack */
99 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
104 /* Unlock the Address Space */
105 MmUnlockAddressSpace(MmGetKernelAddressSpace());
110 MmDeleteTeb(PEPROCESS Process
,
113 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
114 PMEMORY_AREA MemoryArea
;
116 /* Lock the Address Space */
117 MmLockAddressSpace(ProcessAddressSpace
);
119 MemoryArea
= MmLocateMemoryAreaByAddress(ProcessAddressSpace
, (PVOID
)Teb
);
123 MmFreeVirtualMemory(Process
, MemoryArea
);
126 /* Unlock the Address Space */
127 MmUnlockAddressSpace(ProcessAddressSpace
);
132 MmCreateKernelStack(BOOLEAN GuiStack
)
134 PMEMORY_AREA StackArea
;
136 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
137 PFN_TYPE Page
[MM_STACK_SIZE
/ PAGE_SIZE
];
138 PVOID KernelStack
= NULL
;
141 /* Initialize the Boundary Address */
142 BoundaryAddressMultiple
.QuadPart
= 0;
144 /* Lock the Kernel Address Space */
145 MmLockAddressSpace(MmGetKernelAddressSpace());
147 /* Create a MAREA for the Kernel Stack */
148 Status
= MmCreateMemoryArea(MmGetKernelAddressSpace(),
149 MEMORY_AREA_KERNEL_STACK
,
156 BoundaryAddressMultiple
);
158 /* Unlock the Address Space */
159 MmUnlockAddressSpace(MmGetKernelAddressSpace());
161 /* Check for Success */
162 if (!NT_SUCCESS(Status
))
164 DPRINT1("Failed to create thread stack\n");
168 /* Mark the Stack in use */
169 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGE_SIZE
); i
++)
171 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
[i
]);
174 /* Create a Virtual Mapping for it */
175 Status
= MmCreateVirtualMapping(NULL
,
179 MM_STACK_SIZE
/ PAGE_SIZE
);
181 /* Check for success */
182 if (!NT_SUCCESS(Status
))
184 DPRINT1("Could not create Virtual Mapping for Kernel Stack\n");
193 MmCreatePeb(PEPROCESS Process
)
196 LARGE_INTEGER SectionOffset
;
198 PVOID TableBase
= NULL
;
199 PIMAGE_NT_HEADERS NtHeaders
;
200 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
202 KAFFINITY ProcessAffinityMask
= 0;
203 SectionOffset
.QuadPart
= (ULONGLONG
)0;
204 DPRINT("MmCreatePeb\n");
206 /* Allocate the PEB */
207 Peb
= MiCreatePebOrTeb(Process
,
208 (PVOID
)((ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1));
209 ASSERT(Peb
== (PVOID
)0x7FFDF000);
212 DPRINT("Mapping NLS\n");
213 Status
= MmMapViewOfSection(NlsSectionObject
,
223 if (!NT_SUCCESS(Status
))
225 DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status
);
228 DPRINT("TableBase %p ViewSize %lx\n", TableBase
, ViewSize
);
230 /* Attach to Process */
231 KeAttachProcess(&Process
->Pcb
);
233 /* Initialize the PEB */
234 DPRINT("Allocated: %x\n", Peb
);
235 RtlZeroMemory(Peb
, sizeof(PEB
));
238 DPRINT("Setting up PEB\n");
239 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
240 Peb
->InheritedAddressSpace
= 0;
244 Peb
->AnsiCodePageData
= (char*)TableBase
+ NlsAnsiTableOffset
;
245 Peb
->OemCodePageData
= (char*)TableBase
+ NlsOemTableOffset
;
246 Peb
->UnicodeCaseTableData
= (char*)TableBase
+ NlsUnicodeTableOffset
;
248 /* Default Version Data (could get changed below) */
249 Peb
->OSMajorVersion
= NtMajorVersion
;
250 Peb
->OSMinorVersion
= NtMinorVersion
;
251 Peb
->OSBuildNumber
= 2195;
252 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
253 Peb
->OSCSDVersion
= NtOSCSDVersion
;
255 /* Heap and Debug Data */
256 Peb
->NumberOfProcessors
= KeNumberProcessors
;
257 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
258 Peb
->NtGlobalFlag
= NtGlobalFlag
;
259 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
260 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
261 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
262 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;*/
263 Peb
->NumberOfHeaps
= 0;
264 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
265 Peb
->ProcessHeaps
= (PVOID
*)Peb
+ 1;
268 if ((NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
)))
270 /* Get the Image Config Data too */
271 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
273 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
276 /* Write subsystem data */
277 Peb
->ImageSubSystem
= NtHeaders
->OptionalHeader
.Subsystem
;
278 Peb
->ImageSubSystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
279 Peb
->ImageSubSystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
281 /* Write Version Data */
282 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
284 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
285 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
286 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
288 /* Lie about the version if requested */
289 if (ImageConfigData
&& ImageConfigData
->CSDVersion
)
291 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
294 /* Set the Platform ID */
295 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
298 /* Check for affinity override */
299 if (ImageConfigData
&& ImageConfigData
->ProcessAffinityMask
)
301 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
304 /* Check if the image is not safe for SMP */
305 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
307 /* FIXME: Choose one randomly */
308 Peb
->ImageProcessAffinityMask
= 1;
312 /* Use affinity from Image Header */
313 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
318 Peb
->SessionId
= Process
->Session
;
321 /* Detach from the Process */
324 DPRINT("MmCreatePeb: Peb created at %p\n", Peb
);
325 return STATUS_SUCCESS
;
330 MmCreateTeb(PEPROCESS Process
,
332 PINITIAL_TEB InitialTeb
)
335 BOOLEAN Attached
= FALSE
;
337 /* Attach to the process */
338 DPRINT("MmCreateTeb\n");
339 if (Process
!= PsGetCurrentProcess())
341 /* Attach to Target */
342 KeAttachProcess(&Process
->Pcb
);
346 /* Allocate the TEB */
347 Teb
= MiCreatePebOrTeb(Process
,
348 (PVOID
)((ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1));
350 /* Initialize the PEB */
351 RtlZeroMemory(Teb
, sizeof(TEB
));
354 Teb
->Tib
.ExceptionList
= (PVOID
)0xFFFFFFFF;
355 Teb
->Tib
.Version
= 1;
356 Teb
->Tib
.Self
= (PNT_TIB
)Teb
;
359 Teb
->Cid
= *ClientId
;
360 Teb
->RealClientId
= *ClientId
;
361 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
362 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
364 /* Store stack information from InitialTeb */
365 if(InitialTeb
!= NULL
)
367 Teb
->Tib
.StackBase
= InitialTeb
->StackBase
;
368 Teb
->Tib
.StackLimit
= InitialTeb
->StackLimit
;
369 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
372 /* Return TEB Address */
373 DPRINT("Allocated: %x\n", Teb
);
374 if (Attached
) KeDetachProcess();
380 MmCreateProcessAddressSpace(IN PEPROCESS Process
,
381 IN PSECTION_OBJECT Section OPTIONAL
)
384 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
386 PMEMORY_AREA MemoryArea
;
387 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
390 BoundaryAddressMultiple
.QuadPart
= 0;
392 /* Initialize the Addresss Space */
393 MmInitializeAddressSpace(Process
, ProcessAddressSpace
);
395 /* Acquire the Lock */
396 MmLockAddressSpace(ProcessAddressSpace
);
398 /* Protect the highest 64KB of the process address space */
399 BaseAddress
= (PVOID
)MmUserProbeAddress
;
400 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
401 MEMORY_AREA_NO_ACCESS
,
408 BoundaryAddressMultiple
);
409 if (!NT_SUCCESS(Status
))
411 DPRINT1("Failed to protect last 64KB\n");
415 /* Protect the 60KB above the shared user page */
416 BaseAddress
= (char*)USER_SHARED_DATA
+ PAGE_SIZE
;
417 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
418 MEMORY_AREA_NO_ACCESS
,
425 BoundaryAddressMultiple
);
426 if (!NT_SUCCESS(Status
))
428 DPRINT1("Failed to protect the memory above the shared user page\n");
432 /* Create the shared data page */
433 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
434 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
435 MEMORY_AREA_SHARED_DATA
,
442 BoundaryAddressMultiple
);
443 if (!NT_SUCCESS(Status
))
445 DPRINT1("Failed to create Shared User Data\n");
449 /* Check if there's a Section Object */
452 UNICODE_STRING FileName
;
457 /* Unlock the Address Space */
458 DPRINT("Unlocking\n");
459 MmUnlockAddressSpace(ProcessAddressSpace
);
461 DPRINT("Mapping process image. Section: %p, Process: %p, ImageBase: %p\n",
462 Section
, Process
, &ImageBase
);
463 Status
= MmMapViewOfSection(Section
,
473 if (!NT_SUCCESS(Status
))
475 DPRINT1("Failed to map process Image\n");
479 /* Save the pointer */
480 Process
->SectionBaseAddress
= ImageBase
;
482 /* Determine the image file name and save it to EPROCESS */
483 DPRINT("Getting Image name\n");
484 FileName
= Section
->FileObject
->FileName
;
485 szSrc
= (PWCHAR
)(FileName
.Buffer
+ (FileName
.Length
/ sizeof(WCHAR
)) - 1);
487 while(szSrc
>= FileName
.Buffer
)
501 /* Copy the to the process and truncate it to 15 characters if necessary */
502 DPRINT("Copying and truncating\n");
503 szDest
= Process
->ImageFileName
;
504 lnFName
= min(lnFName
, sizeof(Process
->ImageFileName
) - 1);
505 while(lnFName
-- > 0) *(szDest
++) = (UCHAR
)*(szSrc
++);
507 /* Return status to caller */
512 /* Unlock the Address Space */
513 DPRINT("Unlocking\n");
514 MmUnlockAddressSpace(ProcessAddressSpace
);
516 /* Return status to caller */