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");
206 /* Allocate the PEB */
207 Peb
= MiCreatePebOrTeb(Process
, (PVOID
)PEB_BASE
);
208 if (Peb
!= (PVOID
)PEB_BASE
)
210 DPRINT1("MiCreatePebOrTeb() returned %x\n", Peb
);
211 return STATUS_UNSUCCESSFUL
;
215 DPRINT("Mapping NLS\n");
216 Status
= MmMapViewOfSection(NlsSectionObject
,
226 if (!NT_SUCCESS(Status
))
228 DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status
);
231 DPRINT("TableBase %p ViewSize %lx\n", TableBase
, ViewSize
);
233 /* Attach to Process */
234 KeAttachProcess(&Process
->Pcb
);
236 /* Initialize the PEB */
237 DPRINT("Allocated: %x\n", Peb
);
238 RtlZeroMemory(Peb
, sizeof(PEB
));
241 DPRINT("Setting up PEB\n");
242 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
243 Peb
->InheritedAddressSpace
= 0;
247 Peb
->AnsiCodePageData
= (char*)TableBase
+ NlsAnsiTableOffset
;
248 Peb
->OemCodePageData
= (char*)TableBase
+ NlsOemTableOffset
;
249 Peb
->UnicodeCaseTableData
= (char*)TableBase
+ NlsUnicodeTableOffset
;
251 /* Default Version Data (could get changed below) */
252 Peb
->OSMajorVersion
= NtMajorVersion
;
253 Peb
->OSMinorVersion
= NtMinorVersion
;
254 Peb
->OSBuildNumber
= 2195;
255 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
256 Peb
->OSCSDVersion
= NtOSCSDVersion
;
258 /* Heap and Debug Data */
259 Peb
->NumberOfProcessors
= KeNumberProcessors
;
260 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
261 Peb
->NtGlobalFlag
= NtGlobalFlag
;
262 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
263 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
264 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
265 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;*/
266 Peb
->NumberOfHeaps
= 0;
267 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
268 Peb
->ProcessHeaps
= (PVOID
*)Peb
+ 1;
271 if ((NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
)))
273 /* Get the Image Config Data too */
274 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
276 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
279 /* Write subsystem data */
280 Peb
->ImageSubSystem
= NtHeaders
->OptionalHeader
.Subsystem
;
281 Peb
->ImageSubSystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
282 Peb
->ImageSubSystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
284 /* Write Version Data */
285 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
287 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
288 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
289 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
291 /* Lie about the version if requested */
292 if (ImageConfigData
&& ImageConfigData
->CSDVersion
)
294 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
297 /* Set the Platform ID */
298 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
301 /* Check for affinity override */
302 if (ImageConfigData
&& ImageConfigData
->ProcessAffinityMask
)
304 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
307 /* Check if the image is not safe for SMP */
308 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
310 /* FIXME: Choose one randomly */
311 Peb
->ImageProcessAffinityMask
= 1;
315 /* Use affinity from Image Header */
316 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
321 Peb
->SessionId
= Process
->Session
;
324 /* Detach from the Process */
327 DPRINT("MmCreatePeb: Peb created at %p\n", Peb
);
328 return STATUS_SUCCESS
;
333 MmCreateTeb(PEPROCESS Process
,
335 PINITIAL_TEB InitialTeb
)
338 BOOLEAN Attached
= FALSE
;
340 /* Attach to the process */
341 DPRINT("MmCreateTeb\n");
342 if (Process
!= PsGetCurrentProcess())
344 /* Attach to Target */
345 KeAttachProcess(&Process
->Pcb
);
349 /* Allocate the TEB */
350 Teb
= MiCreatePebOrTeb(Process
, (PVOID
)TEB_BASE
);
352 /* Initialize the PEB */
353 RtlZeroMemory(Teb
, sizeof(TEB
));
356 Teb
->Tib
.ExceptionList
= (PVOID
)0xFFFFFFFF;
357 Teb
->Tib
.Version
= 1;
358 Teb
->Tib
.Self
= (PNT_TIB
)Teb
;
361 Teb
->Cid
= *ClientId
;
362 Teb
->RealClientId
= *ClientId
;
363 Teb
->ProcessEnvironmentBlock
= Process
->Peb
;
364 Teb
->CurrentLocale
= PsDefaultThreadLocaleId
;
366 /* Store stack information from InitialTeb */
367 if(InitialTeb
!= NULL
)
369 Teb
->Tib
.StackBase
= InitialTeb
->StackBase
;
370 Teb
->Tib
.StackLimit
= InitialTeb
->StackLimit
;
371 Teb
->DeallocationStack
= InitialTeb
->AllocatedStackBase
;
374 /* Return TEB Address */
375 DPRINT("Allocated: %x\n", Teb
);
376 if (Attached
) KeDetachProcess();
382 MmCreateProcessAddressSpace(IN PEPROCESS Process
,
383 IN PSECTION_OBJECT Section OPTIONAL
)
386 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
388 PMEMORY_AREA MemoryArea
;
389 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
392 BoundaryAddressMultiple
.QuadPart
= 0;
394 /* Initialize the Addresss Space */
395 MmInitializeAddressSpace(Process
, ProcessAddressSpace
);
397 /* Acquire the Lock */
398 MmLockAddressSpace(ProcessAddressSpace
);
400 /* Protect the highest 64KB of the process address space */
401 BaseAddress
= (PVOID
)MmUserProbeAddress
;
402 Status
= MmCreateMemoryArea(Process
,
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(Process
,
422 MEMORY_AREA_NO_ACCESS
,
429 BoundaryAddressMultiple
);
430 if (!NT_SUCCESS(Status
))
432 DPRINT1("Failed to protect the memory above the shared user page\n");
436 /* Create the shared data page */
437 BaseAddress
= (PVOID
)USER_SHARED_DATA
;
438 Status
= MmCreateMemoryArea(Process
,
440 MEMORY_AREA_SHARED_DATA
,
447 BoundaryAddressMultiple
);
448 if (!NT_SUCCESS(Status
))
450 DPRINT1("Failed to create Shared User Data\n");
454 /* Check if there's a Section Object */
457 UNICODE_STRING FileName
;
462 /* Unlock the Address Space */
463 DPRINT("Unlocking\n");
464 MmUnlockAddressSpace(ProcessAddressSpace
);
466 DPRINT("Mapping process image. Section: %p, Process: %p, ImageBase: %p\n",
467 Section
, Process
, &ImageBase
);
468 Status
= MmMapViewOfSection(Section
,
478 if (!NT_SUCCESS(Status
))
480 DPRINT1("Failed to map process Image\n");
484 /* Save the pointer */
485 Process
->SectionBaseAddress
= ImageBase
;
487 /* Determine the image file name and save it to EPROCESS */
488 DPRINT("Getting Image name\n");
489 FileName
= Section
->FileObject
->FileName
;
490 szSrc
= (PWCHAR
)(FileName
.Buffer
+ (FileName
.Length
/ sizeof(WCHAR
)) - 1);
492 while(szSrc
>= FileName
.Buffer
)
506 /* Copy the to the process and truncate it to 15 characters if necessary */
507 DPRINT("Copying and truncating\n");
508 szDest
= Process
->ImageFileName
;
509 lnFName
= min(lnFName
, sizeof(Process
->ImageFileName
) - 1);
510 while(lnFName
-- > 0) *(szDest
++) = (UCHAR
)*(szSrc
++);
512 /* Return status to caller */
517 /* Unlock the Address Space */
518 DPRINT("Unlocking\n");
519 MmUnlockAddressSpace(ProcessAddressSpace
);
521 /* Return status to caller */