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(ProcessAddressSpace
,
49 MEMORY_AREA_PEB_OR_TEB
,
56 BoundaryAddressMultiple
);
57 AllocatedBase
= RVA(AllocatedBase
, -PAGE_SIZE
);
58 } while (Status
!= STATUS_SUCCESS
);
60 /* Initialize the Region */
61 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
66 /* Reserve the pages */
67 MmReserveSwapPages(PAGE_SIZE
);
69 /* Unlock Address Space */
70 DPRINT("Returning\n");
71 MmUnlockAddressSpace(ProcessAddressSpace
);
72 return RVA(AllocatedBase
, PAGE_SIZE
);
76 MiFreeStackPage(PVOID Context
,
77 MEMORY_AREA
* MemoryArea
,
83 ASSERT(SwapEntry
== 0);
84 if (Page
) MmReleasePageMemoryConsumer(MC_NPPOOL
, Page
);
89 MmDeleteKernelStack(PVOID Stack
,
92 /* Lock the Address Space */
93 MmLockAddressSpace(MmGetKernelAddressSpace());
95 /* Delete the Stack */
96 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
101 /* Unlock the Address Space */
102 MmUnlockAddressSpace(MmGetKernelAddressSpace());
107 MmDeleteTeb(PEPROCESS Process
,
110 PMADDRESS_SPACE ProcessAddressSpace
= &Process
->AddressSpace
;
111 PMEMORY_AREA MemoryArea
;
113 /* Lock the Address Space */
114 MmLockAddressSpace(ProcessAddressSpace
);
116 MemoryArea
= MmLocateMemoryAreaByAddress(ProcessAddressSpace
, (PVOID
)Teb
);
120 MmFreeVirtualMemory(Process
, MemoryArea
);
123 /* Unlock the Address Space */
124 MmUnlockAddressSpace(ProcessAddressSpace
);
129 MmCreateKernelStack(BOOLEAN GuiStack
)
131 PMEMORY_AREA StackArea
;
133 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
134 PFN_TYPE Page
[MM_STACK_SIZE
/ PAGE_SIZE
];
135 PVOID KernelStack
= NULL
;
138 /* Initialize the Boundary Address */
139 BoundaryAddressMultiple
.QuadPart
= 0;
141 /* Lock the Kernel Address Space */
142 MmLockAddressSpace(MmGetKernelAddressSpace());
144 /* Create a MAREA for the Kernel Stack */
145 Status
= MmCreateMemoryArea(MmGetKernelAddressSpace(),
146 MEMORY_AREA_KERNEL_STACK
,
153 BoundaryAddressMultiple
);
155 /* Unlock the Address Space */
156 MmUnlockAddressSpace(MmGetKernelAddressSpace());
158 /* Check for Success */
159 if (!NT_SUCCESS(Status
))
161 DPRINT1("Failed to create thread stack\n");
165 /* Mark the Stack in use */
166 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGE_SIZE
); i
++)
168 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
[i
]);
171 /* Create a Virtual Mapping for it */
172 Status
= MmCreateVirtualMapping(NULL
,
176 MM_STACK_SIZE
/ PAGE_SIZE
);
178 /* Check for success */
179 if (!NT_SUCCESS(Status
))
181 DPRINT1("Could not create Virtual Mapping for Kernel Stack\n");
190 MmCreatePeb(PEPROCESS Process
)
193 LARGE_INTEGER SectionOffset
;
195 PVOID TableBase
= NULL
;
196 PIMAGE_NT_HEADERS NtHeaders
;
197 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData
;
199 KAFFINITY ProcessAffinityMask
= 0;
200 SectionOffset
.QuadPart
= (ULONGLONG
)0;
202 DPRINT("MmCreatePeb\n");
204 /* Allocate the PEB */
205 Peb
= MiCreatePebOrTeb(Process
, (PVOID
)PEB_BASE
);
206 if (Peb
!= (PVOID
)PEB_BASE
)
208 DPRINT1("MiCreatePebOrTeb() returned %x\n", Peb
);
209 return STATUS_UNSUCCESSFUL
;
213 DPRINT("Mapping NLS\n");
214 Status
= MmMapViewOfSection(NlsSectionObject
,
224 if (!NT_SUCCESS(Status
))
226 DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status
);
229 DPRINT("TableBase %p ViewSize %lx\n", TableBase
, ViewSize
);
231 /* Attach to Process */
232 KeAttachProcess(&Process
->Pcb
);
234 /* Initialize the PEB */
235 DPRINT("Allocated: %x\n", Peb
);
236 RtlZeroMemory(Peb
, sizeof(PEB
));
239 DPRINT("Setting up PEB\n");
240 Peb
->ImageBaseAddress
= Process
->SectionBaseAddress
;
241 Peb
->InheritedAddressSpace
= 0;
245 Peb
->AnsiCodePageData
= (char*)TableBase
+ NlsAnsiTableOffset
;
246 Peb
->OemCodePageData
= (char*)TableBase
+ NlsOemTableOffset
;
247 Peb
->UnicodeCaseTableData
= (char*)TableBase
+ NlsUnicodeTableOffset
;
249 /* Default Version Data (could get changed below) */
250 Peb
->OSMajorVersion
= NtMajorVersion
;
251 Peb
->OSMinorVersion
= NtMinorVersion
;
252 Peb
->OSBuildNumber
= 2195;
253 Peb
->OSPlatformId
= 2; /* VER_PLATFORM_WIN32_NT */
254 Peb
->OSCSDVersion
= NtOSCSDVersion
;
256 /* Heap and Debug Data */
257 Peb
->NumberOfProcessors
= KeNumberProcessors
;
258 Peb
->BeingDebugged
= (BOOLEAN
)(Process
->DebugPort
!= NULL
? TRUE
: FALSE
);
259 Peb
->NtGlobalFlag
= NtGlobalFlag
;
260 /*Peb->HeapSegmentReserve = MmHeapSegmentReserve;
261 Peb->HeapSegmentCommit = MmHeapSegmentCommit;
262 Peb->HeapDeCommitTotalFreeThreshold = MmHeapDeCommitTotalFreeThreshold;
263 Peb->HeapDeCommitFreeBlockThreshold = MmHeapDeCommitFreeBlockThreshold;*/
264 Peb
->NumberOfHeaps
= 0;
265 Peb
->MaximumNumberOfHeaps
= (PAGE_SIZE
- sizeof(PEB
)) / sizeof(PVOID
);
266 Peb
->ProcessHeaps
= (PVOID
*)Peb
+ 1;
269 if ((NtHeaders
= RtlImageNtHeader(Peb
->ImageBaseAddress
)))
271 /* Get the Image Config Data too */
272 ImageConfigData
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
274 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
277 /* Write subsystem data */
278 Peb
->ImageSubSystem
= NtHeaders
->OptionalHeader
.Subsystem
;
279 Peb
->ImageSubSystemMajorVersion
= NtHeaders
->OptionalHeader
.MajorSubsystemVersion
;
280 Peb
->ImageSubSystemMinorVersion
= NtHeaders
->OptionalHeader
.MinorSubsystemVersion
;
282 /* Write Version Data */
283 if (NtHeaders
->OptionalHeader
.Win32VersionValue
)
285 Peb
->OSMajorVersion
= NtHeaders
->OptionalHeader
.Win32VersionValue
& 0xFF;
286 Peb
->OSMinorVersion
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 8) & 0xFF;
287 Peb
->OSBuildNumber
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 16) & 0x3FFF;
289 /* Lie about the version if requested */
290 if (ImageConfigData
&& ImageConfigData
->CSDVersion
)
292 Peb
->OSCSDVersion
= ImageConfigData
->CSDVersion
;
295 /* Set the Platform ID */
296 Peb
->OSPlatformId
= (NtHeaders
->OptionalHeader
.Win32VersionValue
>> 30) ^ 2;
299 /* Check for affinity override */
300 if (ImageConfigData
&& ImageConfigData
->ProcessAffinityMask
)
302 ProcessAffinityMask
= ImageConfigData
->ProcessAffinityMask
;
305 /* Check if the image is not safe for SMP */
306 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
)
308 /* FIXME: Choose one randomly */
309 Peb
->ImageProcessAffinityMask
= 1;
313 /* Use affinity from Image Header */
314 Peb
->ImageProcessAffinityMask
= ProcessAffinityMask
;
319 Peb
->SessionId
= Process
->Session
;
322 /* Detach from the Process */
325 DPRINT("MmCreatePeb: Peb created at %p\n", Peb
);
326 return STATUS_SUCCESS
;
331 MmCreateTeb(PEPROCESS Process
,
333 PINITIAL_TEB InitialTeb
)
336 BOOLEAN Attached
= FALSE
;
338 /* Attach to the process */
339 DPRINT("MmCreateTeb\n");
340 if (Process
!= PsGetCurrentProcess())
342 /* Attach to Target */
343 KeAttachProcess(&Process
->Pcb
);
347 /* Allocate the TEB */
348 Teb
= MiCreatePebOrTeb(Process
, (PVOID
)TEB_BASE
);
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 */