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 MmInitialiseRegion(&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;
205 DPRINT("MmCreatePeb\n");
207 /* Allocate the PEB */
208 Peb
= MiCreatePebOrTeb(Process
, MM_HIGHEST_VAD_ADDRESS
);
209 if (Peb
!= MM_HIGHEST_VAD_ADDRESS
)
211 DPRINT1("MiCreatePebOrTeb() returned %x\n", Peb
);
212 return STATUS_UNSUCCESSFUL
;
216 DPRINT("Mapping NLS\n");
217 Status
= MmMapViewOfSection(NlsSectionObject
,
227 if (!NT_SUCCESS(Status
))
229 DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status
);
232 DPRINT("TableBase %p ViewSize %lx\n", TableBase
, ViewSize
);
234 /* Attach to Process */
235 KeAttachProcess(&Process
->Pcb
);
237 /* Initialize the PEB */
238 DPRINT("Allocated: %x\n", Peb
);
239 RtlZeroMemory(Peb
, sizeof(PEB
));
242 DPRINT("Setting up PEB\n");
243 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
244 Peb
->InheritedAddressSpace
= 0;
248 Peb
->AnsiCodePageData
= (char*)TableBase
+ NlsAnsiTableOffset
;
249 Peb
->OemCodePageData
= (char*)TableBase
+ NlsOemTableOffset
;
250 Peb
->UnicodeCaseTableData
= (char*)TableBase
+ NlsUnicodeTableOffset
;
252 /* Default Version Data (could get changed below) */
253 Peb
->OSMajorVersion
= NtMajorVersion
;
254 Peb
->OSMinorVersion
= NtMinorVersion
;
255 Peb
->OSBuildNumber
= 2195;
256 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
257 Peb
->OSCSDVersion
= NtOSCSDVersion
;
259 /* Heap and Debug Data */
260 Peb
->NumberOfProcessors
= KeNumberProcessors
;
261 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
262 Peb
->NtGlobalFlag
= NtGlobalFlag
;
263 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
264 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
265 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
266 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;*/
267 Peb
->NumberOfHeaps
= 0;
268 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
269 Peb
->ProcessHeaps
= (PVOID
*)Peb
+ 1;
272 if ((NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
)))
274 /* Get the Image Config Data too */
275 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
277 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
280 /* Write subsystem data */
281 Peb
->ImageSubSystem
= NtHeaders
->OptionalHeader
.Subsystem
;
282 Peb
->ImageSubSystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
283 Peb
->ImageSubSystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
285 /* Write Version Data */
286 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
288 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
289 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
290 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
292 /* Lie about the version if requested */
293 if (ImageConfigData
&& ImageConfigData
->CSDVersion
)
295 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
298 /* Set the Platform ID */
299 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
302 /* Check for affinity override */
303 if (ImageConfigData
&& ImageConfigData
->ProcessAffinityMask
)
305 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
308 /* Check if the image is not safe for SMP */
309 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
311 /* FIXME: Choose one randomly */
312 Peb
->ImageProcessAffinityMask
= 1;
316 /* Use affinity from Image Header */
317 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
322 Peb
->SessionId
= Process
->Session
;
325 /* Detach from the Process */
328 DPRINT("MmCreatePeb: Peb created at %p\n", Peb
);
329 return STATUS_SUCCESS
;
334 MmCreateTeb(PEPROCESS Process
,
336 PINITIAL_TEB InitialTeb
)
339 BOOLEAN Attached
= FALSE
;
341 /* Attach to the process */
342 DPRINT("MmCreateTeb\n");
343 if (Process
!= PsGetCurrentProcess())
345 /* Attach to Target */
346 KeAttachProcess(&Process
->Pcb
);
350 /* Allocate the TEB */
351 Teb
= MiCreatePebOrTeb(Process
, MM_HIGHEST_VAD_ADDRESS
);
353 /* Initialize the PEB */
354 RtlZeroMemory(Teb
, sizeof(TEB
));
357 Teb
->Tib
.ExceptionList
= (PVOID
)0xFFFFFFFF;
358 Teb
->Tib
.Version
= 1;
359 Teb
->Tib
.Self
= (PNT_TIB
)Teb
;
362 Teb
->Cid
= *ClientId
;
363 Teb
->RealClientId
= *ClientId
;
364 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
365 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
367 /* Store stack information from InitialTeb */
368 if(InitialTeb
!= NULL
)
370 Teb
->Tib
.StackBase
= InitialTeb
->StackBase
;
371 Teb
->Tib
.StackLimit
= InitialTeb
->StackLimit
;
372 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
375 /* Return TEB Address */
376 DPRINT("Allocated: %x\n", Teb
);
377 if (Attached
) KeDetachProcess();
383 MmCreateProcessAddressSpace(IN PEPROCESS Process
,
384 IN PSECTION_OBJECT Section OPTIONAL
)
387 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
389 PMEMORY_AREA MemoryArea
;
390 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
393 BoundaryAddressMultiple
.QuadPart
= 0;
395 /* Initialize the Addresss Space */
396 MmInitializeAddressSpace(Process
, ProcessAddressSpace
);
398 /* Acquire the Lock */
399 MmLockAddressSpace(ProcessAddressSpace
);
401 /* Protect the highest 64KB of the process address space */
402 BaseAddress
= (PVOID
)MmUserProbeAddress
;
403 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
404 MEMORY_AREA_NO_ACCESS
,
411 BoundaryAddressMultiple
);
412 if (!NT_SUCCESS(Status
))
414 DPRINT1("Failed to protect last 64KB\n");
418 /* Protect the 60KB above the shared user page */
419 BaseAddress
= (char*)USER_SHARED_DATA
+ PAGE_SIZE
;
420 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
421 MEMORY_AREA_NO_ACCESS
,
428 BoundaryAddressMultiple
);
429 if (!NT_SUCCESS(Status
))
431 DPRINT1("Failed to protect the memory above the shared user page\n");
435 /* Create the shared data page */
436 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
437 Status
= MmCreateMemoryArea(ProcessAddressSpace
,
438 MEMORY_AREA_SHARED_DATA
,
445 BoundaryAddressMultiple
);
446 if (!NT_SUCCESS(Status
))
448 DPRINT1("Failed to create Shared User Data\n");
452 /* Check if there's a Section Object */
455 UNICODE_STRING FileName
;
460 /* Unlock the Address Space */
461 DPRINT("Unlocking\n");
462 MmUnlockAddressSpace(ProcessAddressSpace
);
464 DPRINT("Mapping process image. Section: %p, Process: %p, ImageBase: %p\n",
465 Section
, Process
, &ImageBase
);
466 Status
= MmMapViewOfSection(Section
,
476 if (!NT_SUCCESS(Status
))
478 DPRINT1("Failed to map process Image\n");
482 /* Save the pointer */
483 Process
->SectionBaseAddress
= ImageBase
;
485 /* Determine the image file name and save it to EPROCESS */
486 DPRINT("Getting Image name\n");
487 FileName
= Section
->FileObject
->FileName
;
488 szSrc
= (PWCHAR
)(FileName
.Buffer
+ (FileName
.Length
/ sizeof(WCHAR
)) - 1);
490 while(szSrc
>= FileName
.Buffer
)
504 /* Copy the to the process and truncate it to 15 characters if necessary */
505 DPRINT("Copying and truncating\n");
506 szDest
= Process
->ImageFileName
;
507 lnFName
= min(lnFName
, sizeof(Process
->ImageFileName
) - 1);
508 while(lnFName
-- > 0) *(szDest
++) = (UCHAR
)*(szSrc
++);
510 /* Return status to caller */
515 /* Unlock the Address Space */
516 DPRINT("Unlocking\n");
517 MmUnlockAddressSpace(ProcessAddressSpace
);
519 /* Return status to caller */