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 /* FUNCTIONS *****************************************************************/
25 MiCreatePebOrTeb(PEPROCESS Process
,
29 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
30 PMEMORY_AREA MemoryArea
;
31 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
32 PVOID AllocatedBase
= BaseAddress
;
33 BoundaryAddressMultiple
.QuadPart
= 0;
35 /* Acquire the Lock */
36 MmLockAddressSpace(ProcessAddressSpace
);
39 * Create a Peb or Teb.
40 * Loop until it works, decreasing by PAGE_SIZE each time. The logic here
41 * is that a PEB allocation should never fail since the address is free,
42 * while TEB allocation can fail, and we should simply try the address
43 * below. Is there a nicer way of doing this automagically? (ie: findning)
44 * a gap region? -- Alex
47 DPRINT("Trying to allocate: %x\n", AllocatedBase
);
48 Status
= MmCreateMemoryArea(Process
,
50 MEMORY_AREA_PEB_OR_TEB
,
57 BoundaryAddressMultiple
);
58 AllocatedBase
= RVA(AllocatedBase
, -PAGE_SIZE
);
59 } while (Status
!= STATUS_SUCCESS
);
61 /* Initialize the Region */
62 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
67 /* Reserve the pages */
68 MmReserveSwapPages(PAGE_SIZE
);
70 /* Unlock Address Space */
71 DPRINT("Returning\n");
72 MmUnlockAddressSpace(ProcessAddressSpace
);
73 return RVA(AllocatedBase
, PAGE_SIZE
);
77 MiFreeStackPage(PVOID Context
,
78 MEMORY_AREA
* MemoryArea
,
84 ASSERT(SwapEntry
== 0);
85 if (Page
) MmReleasePageMemoryConsumer(MC_NPPOOL
, Page
);
90 MmDeleteKernelStack(PVOID Stack
,
93 /* Lock the Address Space */
94 MmLockAddressSpace(MmGetKernelAddressSpace());
96 /* Delete the Stack */
97 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
102 /* Unlock the Address Space */
103 MmUnlockAddressSpace(MmGetKernelAddressSpace());
108 MmDeleteTeb(PEPROCESS Process
,
111 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
112 PMEMORY_AREA MemoryArea
;
114 /* Lock the Address Space */
115 MmLockAddressSpace(ProcessAddressSpace
);
117 MemoryArea
= MmLocateMemoryAreaByAddress(ProcessAddressSpace
, (PVOID
)Teb
);
121 MmFreeVirtualMemory(Process
, MemoryArea
);
124 /* Unlock the Address Space */
125 MmUnlockAddressSpace(ProcessAddressSpace
);
130 MmCreateKernelStack(BOOLEAN GuiStack
)
132 PMEMORY_AREA StackArea
;
134 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
135 PFN_TYPE Page
[MM_STACK_SIZE
/ PAGE_SIZE
];
136 PVOID KernelStack
= NULL
;
139 /* Initialize the Boundary Address */
140 BoundaryAddressMultiple
.QuadPart
= 0;
142 /* Lock the Kernel Address Space */
143 MmLockAddressSpace(MmGetKernelAddressSpace());
145 /* Create a MAREA for the Kernel Stack */
146 Status
= MmCreateMemoryArea(NULL
,
147 MmGetKernelAddressSpace(),
148 MEMORY_AREA_KERNEL_STACK
,
155 BoundaryAddressMultiple
);
157 /* Unlock the Address Space */
158 MmUnlockAddressSpace(MmGetKernelAddressSpace());
160 /* Check for Success */
161 if (!NT_SUCCESS(Status
))
163 DPRINT1("Failed to create thread stack\n");
167 /* Mark the Stack in use */
168 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGE_SIZE
); i
++)
170 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
[i
]);
173 /* Create a Virtual Mapping for it */
174 Status
= MmCreateVirtualMapping(NULL
,
178 MM_STACK_SIZE
/ PAGE_SIZE
);
180 /* Check for success */
181 if (!NT_SUCCESS(Status
))
183 DPRINT1("Could not create Virtual Mapping for Kernel Stack\n");
192 MmCreatePeb(PEPROCESS Process
)
195 LARGE_INTEGER SectionOffset
;
197 PVOID TableBase
= NULL
;
198 PIMAGE_NT_HEADERS NtHeaders
;
199 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
201 KAFFINITY ProcessAffinityMask
= 0;
202 SectionOffset
.QuadPart
= (ULONGLONG
)0;
204 DPRINT("MmCreatePeb\n");
207 DPRINT("Mapping NLS\n");
208 Status
= MmMapViewOfSection(NlsSectionObject
,
218 if (!NT_SUCCESS(Status
))
220 DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status
);
223 DPRINT("TableBase %p ViewSize %lx\n", TableBase
, ViewSize
);
225 /* Attach to Process */
226 KeAttachProcess(&Process
->Pcb
);
228 /* Allocate the PEB */
229 Peb
= MiCreatePebOrTeb(Process
, (PVOID
)PEB_BASE
);
231 /* Initialize the PEB */
232 DPRINT("Allocated: %x\n", Peb
);
233 RtlZeroMemory(Peb
, sizeof(PEB
));
236 DPRINT("Setting up PEB\n");
237 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
238 Peb
->InheritedAddressSpace
= 0;
242 Peb
->AnsiCodePageData
= (char*)TableBase
+ NlsAnsiTableOffset
;
243 Peb
->OemCodePageData
= (char*)TableBase
+ NlsOemTableOffset
;
244 Peb
->UnicodeCaseTableData
= (char*)TableBase
+ NlsUnicodeTableOffset
;
246 /* Default Version Data (could get changed below) */
247 Peb
->OSMajorVersion
= NtMajorVersion
;
248 Peb
->OSMinorVersion
= NtMinorVersion
;
249 Peb
->OSBuildNumber
= 2195;
250 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
251 Peb
->OSCSDVersion
= NtOSCSDVersion
;
253 /* Heap and Debug Data */
254 Peb
->NumberOfProcessors
= KeNumberProcessors
;
255 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
256 Peb
->NtGlobalFlag
= NtGlobalFlag
;
257 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
258 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
259 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
260 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;*/
261 Peb
->NumberOfHeaps
= 0;
262 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
263 Peb
->ProcessHeaps
= (PVOID
*)Peb
+ 1;
266 if ((NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
)))
268 /* Get the Image Config Data too */
269 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
271 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
274 /* Write subsystem data */
275 Peb
->ImageSubSystem
= NtHeaders
->OptionalHeader
.Subsystem
;
276 Peb
->ImageSubSystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
277 Peb
->ImageSubSystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
279 /* Write Version Data */
280 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
282 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
283 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
284 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
286 /* Lie about the version if requested */
287 if (ImageConfigData
&& ImageConfigData
->CSDVersion
)
289 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
292 /* Set the Platform ID */
293 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
296 /* Check for affinity override */
297 if (ImageConfigData
&& ImageConfigData
->ProcessAffinityMask
)
299 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
302 /* Check if the image is not safe for SMP */
303 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
305 /* FIXME: Choose one randomly */
306 Peb
->ImageProcessAffinityMask
= 1;
310 /* Use affinity from Image Header */
311 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
316 Peb
->SessionId
= Process
->Session
;
319 /* Detach from the Process */
322 DPRINT("MmCreatePeb: Peb created at %p\n", Peb
);
323 return STATUS_SUCCESS
;
328 MmCreateTeb(PEPROCESS Process
,
330 PINITIAL_TEB InitialTeb
)
333 BOOLEAN Attached
= FALSE
;
335 /* Attach to the process */
336 DPRINT("MmCreateTeb\n");
337 if (Process
!= PsGetCurrentProcess())
339 /* Attach to Target */
340 KeAttachProcess(&Process
->Pcb
);
344 /* Allocate the TEB */
345 Teb
= MiCreatePebOrTeb(Process
, (PVOID
)TEB_BASE
);
347 /* Initialize the PEB */
348 RtlZeroMemory(Teb
, sizeof(TEB
));
351 Teb
->Tib
.ExceptionList
= (PVOID
)0xFFFFFFFF;
352 Teb
->Tib
.Version
= 1;
353 Teb
->Tib
.Self
= (PNT_TIB
)Teb
;
356 Teb
->Cid
= *ClientId
;
357 Teb
->RealClientId
= *ClientId
;
358 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
359 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
361 /* Store stack information from InitialTeb */
362 if(InitialTeb
!= NULL
)
364 Teb
->Tib
.StackBase
= InitialTeb
->StackBase
;
365 Teb
->Tib
.StackLimit
= InitialTeb
->StackLimit
;
366 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
369 /* Return TEB Address */
370 DPRINT("Allocated: %x\n", Teb
);
371 if (Attached
) KeDetachProcess();
377 MmCreateProcessAddressSpace(IN PEPROCESS Process
,
378 IN PSECTION_OBJECT Section OPTIONAL
)
381 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
383 PMEMORY_AREA MemoryArea
;
384 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
387 BoundaryAddressMultiple
.QuadPart
= 0;
389 /* Initialize the Addresss Space */
390 MmInitializeAddressSpace(Process
, ProcessAddressSpace
);
392 /* Acquire the Lock */
393 MmLockAddressSpace(ProcessAddressSpace
);
395 /* Protect the highest 64KB of the process address space */
396 BaseAddress
= (PVOID
)MmUserProbeAddress
;
397 Status
= MmCreateMemoryArea(Process
,
399 MEMORY_AREA_NO_ACCESS
,
406 BoundaryAddressMultiple
);
407 if (!NT_SUCCESS(Status
))
409 DPRINT1("Failed to protect last 64KB\n");
413 /* Protect the 60KB above the shared user page */
414 BaseAddress
= (char*)USER_SHARED_DATA
+ PAGE_SIZE
;
415 Status
= MmCreateMemoryArea(Process
,
417 MEMORY_AREA_NO_ACCESS
,
424 BoundaryAddressMultiple
);
425 if (!NT_SUCCESS(Status
))
427 DPRINT1("Failed to protect the memory above the shared user page\n");
431 /* Create the shared data page */
432 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
433 Status
= MmCreateMemoryArea(Process
,
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");
476 ObDereferenceObject(Section
);
479 ObDereferenceObject(Section
);
481 /* Save the pointer */
482 Process
->SectionBaseAddress
= ImageBase
;
484 /* Determine the image file name and save it to EPROCESS */
485 DPRINT("Getting Image name\n");
486 FileName
= Section
->FileObject
->FileName
;
487 szSrc
= (PWCHAR
)(FileName
.Buffer
+ (FileName
.Length
/ sizeof(WCHAR
)) - 1);
489 while(szSrc
>= FileName
.Buffer
)
503 /* Copy the to the process and truncate it to 15 characters if necessary */
504 DPRINT("Copying and truncating\n");
505 szDest
= Process
->ImageFileName
;
506 lnFName
= min(lnFName
, sizeof(Process
->ImageFileName
) - 1);
507 while(lnFName
-- > 0) *(szDest
++) = (UCHAR
)*(szSrc
++);
509 /* Return status to caller */
514 /* Unlock the Address Space */
515 DPRINT("Unlocking\n");
516 MmUnlockAddressSpace(ProcessAddressSpace
);
518 /* Return status to caller */