ReactOS  0.4.15-dev-4927-gfe8f806
procsup.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
#include <mm/ARM3/miarm.h>
Include dependency graph for procsup.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define MODULE_INVOLVED_IN_ARM3
 

Functions

NTSTATUS NTAPI MiCreatePebOrTeb (IN PEPROCESS Process, IN ULONG Size, OUT PULONG_PTR BaseAddress)
 
VOID NTAPI MmDeleteTeb (IN PEPROCESS Process, IN PTEB Teb)
 
VOID NTAPI MmDeleteKernelStack (IN PVOID StackBase, IN BOOLEAN GuiStack)
 
PVOID NTAPI MmCreateKernelStack (IN BOOLEAN GuiStack, IN UCHAR Node)
 
NTSTATUS NTAPI MmGrowKernelStackEx (IN PVOID StackPointer, IN ULONG GrowSize)
 
NTSTATUS NTAPI MmGrowKernelStack (IN PVOID StackPointer)
 
NTSTATUS NTAPI MmSetMemoryPriorityProcess (IN PEPROCESS Process, IN UCHAR MemoryPriority)
 
NTSTATUS NTAPI MmCreatePeb (IN PEPROCESS Process, IN PINITIAL_PEB InitialPeb, OUT PPEB *BasePeb)
 
NTSTATUS NTAPI MmCreateTeb (IN PEPROCESS Process, IN PCLIENT_ID ClientId, IN PINITIAL_TEB InitialTeb, OUT PTEB *BaseTeb)
 
NTSTATUS NTAPI MmInitializeProcessAddressSpace (IN PEPROCESS Process, IN PEPROCESS ProcessClone OPTIONAL, IN PVOID Section OPTIONAL, IN OUT PULONG Flags, IN POBJECT_NAME_INFORMATION *AuditName OPTIONAL)
 
NTSTATUS NTAPI MmInitializeHandBuiltProcess (IN PEPROCESS Process, IN PULONG_PTR DirectoryTableBase)
 
NTSTATUS NTAPI MmInitializeHandBuiltProcess2 (IN PEPROCESS Process)
 
BOOLEAN NTAPI MmCreateProcessAddressSpace (IN ULONG MinWs, IN PEPROCESS Process, OUT PULONG_PTR DirectoryTableBase)
 
VOID NTAPI MmCleanProcessAddressSpace (IN PEPROCESS Process)
 
VOID NTAPI MmDeleteProcessAddressSpace (IN PEPROCESS Process)
 
NTSTATUS NTAPI NtAllocateUserPhysicalPages (IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
 
NTSTATUS NTAPI NtMapUserPhysicalPages (IN PVOID VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
 
NTSTATUS NTAPI NtMapUserPhysicalPagesScatter (IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
 
NTSTATUS NTAPI NtFreeUserPhysicalPages (IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
 

Variables

ULONG MmProcessColorSeed = 0x12345678
 
ULONG MmMaximumDeadKernelStacks = 5
 
SLIST_HEADER MmDeadStackSListHead
 

Macro Definition Documentation

◆ MODULE_INVOLVED_IN_ARM3

#define MODULE_INVOLVED_IN_ARM3

Definition at line 15 of file procsup.c.

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file procsup.c.

Function Documentation

◆ MiCreatePebOrTeb()

NTSTATUS NTAPI MiCreatePebOrTeb ( IN PEPROCESS  Process,
IN ULONG  Size,
OUT PULONG_PTR  BaseAddress 
)

Definition at line 28 of file procsup.c.

31 {
32  PMMVAD_LONG Vad;
34  ULONG_PTR HighestAddress, RandomBase;
35  ULONG AlignedSize;
36  LARGE_INTEGER CurrentTime;
37 
39  if (!NT_SUCCESS(Status))
40  return Status;
41 
42  /* Allocate a VAD */
43  Vad = ExAllocatePoolWithTag(NonPagedPool, sizeof(MMVAD_LONG), 'ldaV');
44  if (!Vad)
45  {
47  goto FailPath;
48  }
49 
50  /* Setup the primary flags with the size, and make it commited, private, RW */
51  Vad->u.LongFlags = 0;
53  Vad->u.VadFlags.MemCommit = TRUE;
56  Vad->u.VadFlags.NoChange = TRUE;
57  Vad->u1.Parent = NULL;
58 
59  /* Setup the secondary flags to make it a secured, writable, long VAD */
60  Vad->u2.LongFlags2 = 0;
61  Vad->u2.VadFlags2.OneSecured = TRUE;
62  Vad->u2.VadFlags2.LongVad = TRUE;
63  Vad->u2.VadFlags2.ReadOnly = FALSE;
64 
65  Vad->ControlArea = NULL; // For Memory-Area hack
66  Vad->FirstPrototypePte = NULL;
67 
68  /* Check if this is a PEB creation */
69  ASSERT(sizeof(TEB) != sizeof(PEB));
70  if (Size == sizeof(PEB))
71  {
72  /* Create a random value to select one page in a 64k region */
73  KeQueryTickCount(&CurrentTime);
74  CurrentTime.LowPart &= (_64K / PAGE_SIZE) - 1;
75 
76  /* Calculate a random base address */
77  RandomBase = (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS + 1;
78  RandomBase -= CurrentTime.LowPart << PAGE_SHIFT;
79 
80  /* Make sure the base address is not too high */
81  AlignedSize = ROUND_TO_PAGES(Size);
82  if ((RandomBase + AlignedSize) > (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS + 1)
83  {
84  RandomBase = (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS + 1 - AlignedSize;
85  }
86 
87  /* Calculate the highest allowed address */
88  HighestAddress = RandomBase + AlignedSize - 1;
89  }
90  else
91  {
92  HighestAddress = (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS;
93  }
94 
95  *BaseAddress = 0;
98  Size,
99  HighestAddress,
100  PAGE_SIZE,
101  MEM_TOP_DOWN);
102  if (!NT_SUCCESS(Status))
103  {
104  ExFreePoolWithTag(Vad, 'ldaV');
106  goto FailPath;
107  }
108 
109 
110  /* Success */
111  return STATUS_SUCCESS;
112 
113 FailPath:
115 
116  return Status;
117 }
union _MMVAD_LONG::@2566 u
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
ULONG ReadOnly
Definition: mmtypes.h:707
NTSTATUS NTAPI PsChargeProcessNonPagedPoolQuota(_In_ PEPROCESS Process, _In_ SIZE_T Amount)
Charges the non paged pool quota of a given process.
Definition: quota.c:811
PMMVAD Parent
Definition: mmtypes.h:751
ULONG OneSecured
Definition: mmtypes.h:705
#define TRUE
Definition: types.h:120
union _MMVAD_LONG::@2565 u1
MMVAD_FLAGS VadFlags
Definition: mmtypes.h:760
ULONG_PTR CommitCharge
Definition: mmtypes.h:688
LONG NTSTATUS
Definition: precomp.h:26
#define MM_READWRITE
Definition: bootanim.c:19
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
PCONTROL_AREA ControlArea
Definition: mmtypes.h:762
ULONG_PTR Protection
Definition: mmtypes.h:693
ULONG LongVad
Definition: mmtypes.h:708
ULONG_PTR LongFlags
Definition: mmtypes.h:759
ULONG_PTR NoChange
Definition: mmtypes.h:690
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define FALSE
Definition: types.h:117
ULONG LongFlags2
Definition: mmtypes.h:767
VOID NTAPI KeQueryTickCount(IN PLARGE_INTEGER TickCount)
Definition: clock.c:165
ULONG_PTR MemCommit
Definition: mmtypes.h:692
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
ULONG_PTR PrivateMemory
Definition: mmtypes.h:695
NTSTATUS NTAPI MiInsertVadEx(_Inout_ PMMVAD Vad, _In_ ULONG_PTR *BaseAddress, _In_ SIZE_T ViewSize, _In_ ULONG_PTR HighestAddress, _In_ ULONG_PTR Alignment, _In_ ULONG AllocationType)
Definition: vadnode.c:204
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
MMVAD_FLAGS2 VadFlags2
Definition: mmtypes.h:768
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define BYTES_TO_PAGES(Size)
ULONG LowPart
Definition: typedefs.h:106
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define _64K
Definition: miarm.h:23
Definition: compat.h:694
#define ROUND_TO_PAGES(Size)
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define NULL
Definition: types.h:112
#define MM_HIGHEST_VAD_ADDRESS
Definition: mm.h:46
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
unsigned int ULONG
Definition: retypes.h:1
PMMPTE FirstPrototypePte
Definition: mmtypes.h:763
#define ULONG_PTR
Definition: config.h:101
#define STATUS_SUCCESS
Definition: shellext.h:65
#define MEM_TOP_DOWN
Definition: nt_native.h:1321
VOID NTAPI PsReturnProcessNonPagedPoolQuota(_In_ PEPROCESS Process, _In_ SIZE_T Amount)
Returns the non paged quota pool that the process was taking up.
Definition: quota.c:938
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
union _MMVAD_LONG::@2567 u2

Referenced by MmCreatePeb(), and MmCreateTeb().

◆ MmCleanProcessAddressSpace()

VOID NTAPI MmCleanProcessAddressSpace ( IN PEPROCESS  Process)

Definition at line 1267 of file procsup.c.

1268 {
1269  PMMVAD Vad;
1270  PMM_AVL_TABLE VadTree;
1272 
1273  /* Only support this */
1274  ASSERT(Process->AddressSpaceInitialized == 2);
1275 
1276  /* Remove from the session */
1278 
1279  /* Lock the process address space from changes */
1282 
1283  /* VM is deleted now */
1284  Process->VmDeleted = TRUE;
1286 
1287  /* Enumerate the VADs */
1288  VadTree = &Process->VadRoot;
1289  while (VadTree->NumberGenericTableElements)
1290  {
1291  /* Grab the current VAD */
1292  Vad = (PMMVAD)VadTree->BalancedRoot.RightChild;
1293 
1294  /* Check for old-style memory areas */
1295  if (Vad->u.VadFlags.Spare == 1)
1296  {
1297  /* Let RosMm handle this */
1299  continue;
1300  }
1301 
1302  /* Lock the working set */
1304 
1305  /* Remove this VAD from the tree */
1306  ASSERT(VadTree->NumberGenericTableElements >= 1);
1307  MiRemoveNode((PMMADDRESS_NODE)Vad, VadTree);
1308 
1309  /* Only regular VADs supported for now */
1310  ASSERT(Vad->u.VadFlags.VadType == VadNone);
1311 
1312  /* Check if this is a section VAD */
1313  if (!(Vad->u.VadFlags.PrivateMemory) && (Vad->ControlArea))
1314  {
1315  /* Remove the view */
1317  }
1318  else
1319  {
1320  /* Delete the addresses */
1322  (Vad->EndingVpn << PAGE_SHIFT) | (PAGE_SIZE - 1),
1323  Vad);
1324 
1325  /* Release the working set */
1327  }
1328 
1329  /* Skip ARM3 fake VADs, they'll be freed by MmDeleteProcessAddresSpace */
1330  if (Vad->u.VadFlags.Spare == 1)
1331  {
1332  /* Set a flag so MmDeleteMemoryArea knows to free, but not to remove */
1333  Vad->u.VadFlags.Spare = 2;
1334  continue;
1335  }
1336 
1337  /* Free the VAD memory */
1338  ExFreePool(Vad);
1339 
1340  /* Return the quota the VAD used */
1342  }
1343 
1344  /* Lock the working set */
1346  ASSERT(Process->CloneRoot == NULL);
1347  ASSERT(Process->PhysicalVadRoot == NULL);
1348 
1349  /* Delete the shared user data section */
1351 
1352  /* Release the working set */
1354 
1355  /* Release the address space */
1357 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
VOID NTAPI MiSessionRemoveProcess(VOID)
Definition: session.c:392
FORCEINLINE VOID MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1244
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
#define TRUE
Definition: types.h:120
FORCEINLINE VOID MmUnlockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1639
ULONG_PTR NumberGenericTableElements
Definition: mmtypes.h:665
if(dx==0 &&dy==0)
Definition: linetemp.h:174
VOID NTAPI MiRosCleanupMemoryArea(PEPROCESS Process, PMMVAD Vad)
Definition: marea.c:512
MMVAD_FLAGS VadFlags
Definition: mmtypes.h:731
MMADDRESS_NODE BalancedRoot
Definition: mmtypes.h:659
ULONG_PTR EndingVpn
Definition: mmtypes.h:727
PCONTROL_AREA ControlArea
Definition: mmtypes.h:733
#define USER_SHARED_DATA
Definition: pstypes.h:51
ULONG_PTR StartingVpn
Definition: mmtypes.h:726
FORCEINLINE VOID MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1174
ULONG_PTR PrivateMemory
Definition: mmtypes.h:695
#define ASSERT(a)
Definition: mode.c:44
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
union _MMVAD::@2563 u
struct _MMADDRESS_NODE * RightChild
Definition: mmtypes.h:649
#define PAGE_SIZE
Definition: env_spec_w32.h:49
VOID NTAPI MiRemoveNode(IN PMMADDRESS_NODE Node, IN PMM_AVL_TABLE Table)
Definition: vadnode.c:360
VOID NTAPI MiDeleteVirtualAddresses(IN ULONG_PTR Va, IN ULONG_PTR EndingAddress, IN PMMVAD Vad)
Definition: virtual.c:530
#define NULL
Definition: types.h:112
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
ULONG_PTR Spare
Definition: mmtypes.h:694
VOID NTAPI MiRemoveMappedView(IN PEPROCESS CurrentProcess, IN PMMVAD Vad)
Definition: section.c:784
VOID NTAPI PsReturnProcessNonPagedPoolQuota(_In_ PEPROCESS Process, _In_ SIZE_T Amount)
Returns the non paged quota pool that the process was taking up.
Definition: quota.c:938
FORCEINLINE VOID MmLockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1632
struct _MMVAD * PMMVAD
ULONG_PTR VadType
Definition: mmtypes.h:691
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

Referenced by PspExitProcess(), and PspExitThread().

◆ MmCreateKernelStack()

PVOID NTAPI MmCreateKernelStack ( IN BOOLEAN  GuiStack,
IN UCHAR  Node 
)

Definition at line 275 of file procsup.c.

277 {
278  PFN_COUNT StackPtes, StackPages;
279  PMMPTE PointerPte, StackPte;
281  MMPTE TempPte, InvalidPte;
282  KIRQL OldIrql;
283  PFN_NUMBER PageFrameIndex;
284  ULONG i;
285  PSLIST_ENTRY SListEntry;
286 
287  //
288  // Calculate pages needed
289  //
290  if (GuiStack)
291  {
292  //
293  // We'll allocate 64KB stack, but only commit 12K
294  //
295  StackPtes = BYTES_TO_PAGES(MmLargeStackSize);
297  }
298  else
299  {
300  //
301  // If the dead stack S-LIST has a stack on it, use it instead of allocating
302  // new system PTEs for this stack
303  //
305  {
307  if (SListEntry != NULL)
308  {
309  BaseAddress = (SListEntry + 1);
310  return BaseAddress;
311  }
312  }
313 
314  //
315  // We'll allocate 12K and that's it
316  //
317  StackPtes = BYTES_TO_PAGES(KERNEL_STACK_SIZE);
318  StackPages = StackPtes;
319  }
320 
321  //
322  // Reserve stack pages, plus a guard page
323  //
324  StackPte = MiReserveSystemPtes(StackPtes + 1, SystemPteSpace);
325  if (!StackPte) return NULL;
326 
327  //
328  // Get the stack address
329  //
330  BaseAddress = MiPteToAddress(StackPte + StackPtes + 1);
331 
332  //
333  // Select the right PTE address where we actually start committing pages
334  //
335  PointerPte = StackPte;
336  if (GuiStack) PointerPte += BYTES_TO_PAGES(MmLargeStackSize -
338 
339 
340  /* Setup the temporary invalid PTE */
341  MI_MAKE_SOFTWARE_PTE(&InvalidPte, MM_NOACCESS);
342 
343  /* Setup the template stack PTE */
344  MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte, PointerPte + 1, MM_READWRITE, 0);
345 
346  //
347  // Acquire the PFN DB lock
348  //
349  OldIrql = MiAcquirePfnLock();
350 
351  //
352  // Loop each stack page
353  //
354  for (i = 0; i < StackPages; i++)
355  {
356  //
357  // Next PTE
358  //
359  PointerPte++;
360 
361  /* Get a page and write the current invalid PTE */
363  MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName);
364  PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
365  MI_WRITE_INVALID_PTE(PointerPte, InvalidPte);
366 
367  /* Initialize the PFN entry for this page */
368  MiInitializePfn(PageFrameIndex, PointerPte, 1);
369 
370  /* Write the valid PTE */
371  TempPte.u.Hard.PageFrameNumber = PageFrameIndex;
372  MI_WRITE_VALID_PTE(PointerPte, TempPte);
373  }
374 
375  //
376  // Release the PFN lock
377  //
378  MiReleasePfnLock(OldIrql);
379 
380  //
381  // Return the stack address
382  //
383  return BaseAddress;
384 }
#define MI_MAKE_SOFTWARE_PTE(p, x)
Definition: miarm.h:189
VOID NTAPI MiInitializePfn(IN PFN_NUMBER PageFrameIndex, IN PMMPTE PointerPte, IN BOOLEAN Modified)
Definition: pfnlist.c:962
ULONG PFN_COUNT
Definition: mmtypes.h:102
PMMPTE NTAPI MiReserveSystemPtes(IN ULONG NumberOfPtes, IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType)
Definition: syspte.c:246
#define MI_GET_NEXT_COLOR()
Definition: miarm.h:237
#define MM_READWRITE
Definition: bootanim.c:19
#define MM_NOACCESS
Definition: miarm.h:65
#define KERNEL_LARGE_STACK_COMMIT
ULONG MmLargeStackSize
Definition: mminit.c:262
SLIST_HEADER MmDeadStackSListHead
Definition: procsup.c:22
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG PFN_NUMBER
Definition: ke.h:9
FORCEINLINE VOID MI_WRITE_VALID_PTE(IN PMMPTE PointerPte, IN MMPTE TempPte)
Definition: miarm.h:964
#define MI_SET_PROCESS2(x)
Definition: mm.h:319
#define PsGetCurrentProcess
Definition: psfuncs.h:17
#define MI_SET_USAGE(x)
Definition: mm.h:317
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
#define PSLIST_ENTRY
Definition: rtltypes.h:134
FORCEINLINE USHORT ExQueryDepthSList(_In_ PSLIST_HEADER SListHead)
Definition: exfuncs.h:153
#define KERNEL_STACK_SIZE
#define BYTES_TO_PAGES(Size)
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
PFN_NUMBER NTAPI MiRemoveAnyPage(IN ULONG Color)
Definition: pfnlist.c:477
FORCEINLINE VOID MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte, IN MMPTE InvalidPte)
Definition: miarm.h:997
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
FORCEINLINE VOID MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte, IN PMMPTE MappingPte, IN ULONG_PTR ProtectionMask, IN PFN_NUMBER PageFrameNumber)
Definition: miarm.h:778
#define InterlockedPopEntrySList(SListHead)
Definition: rtlfuncs.h:3392
FORCEINLINE PVOID MiPteToAddress(PMMPTE PointerPte)
Definition: mm.h:208
ULONG PageFrameNumber
Definition: mmtypes.h:109

◆ MmCreatePeb()

NTSTATUS NTAPI MmCreatePeb ( IN PEPROCESS  Process,
IN PINITIAL_PEB  InitialPeb,
OUT PPEB BasePeb 
)

Definition at line 517 of file procsup.c.

520 {
521  PPEB Peb = NULL;
523  SIZE_T ViewSize = 0;
524  PVOID TableBase = NULL;
525  PIMAGE_NT_HEADERS NtHeaders;
526  PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigData;
528  USHORT Characteristics;
530  SectionOffset.QuadPart = (ULONGLONG)0;
531  *BasePeb = NULL;
532 
533  //
534  // Attach to Process
535  //
536  KeAttachProcess(&Process->Pcb);
537 
538  //
539  // Map NLS Tables
540  //
543  &TableBase,
544  0,
545  0,
546  &SectionOffset,
547  &ViewSize,
548  ViewShare,
549  MEM_TOP_DOWN,
550  PAGE_READONLY);
551  DPRINT("NLS Tables at: %p\n", TableBase);
552  if (!NT_SUCCESS(Status))
553  {
554  /* Cleanup and exit */
555  KeDetachProcess();
556  return Status;
557  }
558 
559  //
560  // Allocate the PEB
561  //
563  DPRINT("PEB at: %p\n", Peb);
564  if (!NT_SUCCESS(Status))
565  {
566  /* Cleanup and exit */
567  KeDetachProcess();
568  return Status;
569  }
570 
571  //
572  // Use SEH in case we can't load the PEB
573  //
574  _SEH2_TRY
575  {
576  //
577  // Initialize the PEB
578  //
579  RtlZeroMemory(Peb, sizeof(PEB));
580 
581  //
582  // Set up data
583  //
584  Peb->ImageBaseAddress = Process->SectionBaseAddress;
585  Peb->InheritedAddressSpace = InitialPeb->InheritedAddressSpace;
586  Peb->Mutant = InitialPeb->Mutant;
587  Peb->ImageUsesLargePages = InitialPeb->ImageUsesLargePages;
588 
589  //
590  // NLS
591  //
595 
596  //
597  // Default Version Data (could get changed below)
598  //
601  Peb->OSBuildNumber = (USHORT)(NtBuildNumber & 0x3FFF);
603  Peb->OSCSDVersion = (USHORT)CmNtCSDVersion;
604 
605  //
606  // Heap and Debug Data
607  //
609  Peb->BeingDebugged = (BOOLEAN)(Process->DebugPort != NULL);
617  Peb->MaximumNumberOfHeaps = (PAGE_SIZE - sizeof(PEB)) / sizeof(PVOID);
618  Peb->ProcessHeaps = (PVOID*)(Peb + 1);
619 
620  //
621  // Session ID
622  //
623  if (Process->Session) Peb->SessionId = MmGetSessionId(Process);
624  }
626  {
627  //
628  // Fail
629  //
630  KeDetachProcess();
632  }
633  _SEH2_END;
634 
635  //
636  // Use SEH in case we can't load the image
637  //
638  _SEH2_TRY
639  {
640  //
641  // Get NT Headers
642  //
643  NtHeaders = RtlImageNtHeader(Peb->ImageBaseAddress);
644  Characteristics = NtHeaders->FileHeader.Characteristics;
645  }
647  {
648  //
649  // Fail
650  //
651  KeDetachProcess();
653  }
654  _SEH2_END;
655 
656  //
657  // Parse the headers
658  //
659  if (NtHeaders)
660  {
661  //
662  // Use SEH in case we can't load the headers
663  //
664  _SEH2_TRY
665  {
666  //
667  // Get the Image Config Data too
668  //
670  TRUE,
672  (PULONG)&ViewSize);
673  if (ImageConfigData)
674  {
675  //
676  // Probe it
677  //
678  ProbeForRead(ImageConfigData,
680  sizeof(ULONG));
681  }
682 
683  //
684  // Write subsystem data
685  //
689 
690  //
691  // Check for version data
692  //
693  if (NtHeaders->OptionalHeader.Win32VersionValue)
694  {
695  //
696  // Extract values and write them
697  //
698  Peb->OSMajorVersion = NtHeaders->OptionalHeader.Win32VersionValue & 0xFF;
699  Peb->OSMinorVersion = (NtHeaders->OptionalHeader.Win32VersionValue >> 8) & 0xFF;
700  Peb->OSBuildNumber = (NtHeaders->OptionalHeader.Win32VersionValue >> 16) & 0x3FFF;
701  Peb->OSPlatformId = (NtHeaders->OptionalHeader.Win32VersionValue >> 30) ^ 2;
702 
703  /* Process CSD version override */
704  if ((ImageConfigData) && (ImageConfigData->CSDVersion))
705  {
706  /* Take the value from the image configuration directory */
707  Peb->OSCSDVersion = ImageConfigData->CSDVersion;
708  }
709  }
710 
711  /* Process optional process affinity mask override */
712  if ((ImageConfigData) && (ImageConfigData->ProcessAffinityMask))
713  {
714  /* Take the value from the image configuration directory */
715  ProcessAffinityMask = ImageConfigData->ProcessAffinityMask;
716  }
717 
718  //
719  // Check if this is a UP image
720  if (Characteristics & IMAGE_FILE_UP_SYSTEM_ONLY)
721  {
722  //
723  // Force it to use CPU 0
724  //
725  /* FIXME: this should use the MmRotatingUniprocessorNumber */
727  }
728  else
729  {
730  //
731  // Whatever was configured
732  //
734  }
735  }
737  {
738  //
739  // Fail
740  //
741  KeDetachProcess();
743  }
744  _SEH2_END;
745  }
746 
747  //
748  // Detach from the Process
749  //
750  KeDetachProcess();
751  *BasePeb = Peb;
752  return STATUS_SUCCESS;
753 }
ULONG ImageSubsystemMajorVersion
Definition: ntddk_ex.h:305
#define VER_PLATFORM_WIN32_NT
Definition: rtltypes.h:238
SIZE_T MmHeapDeCommitTotalFreeThreshold
Definition: mminit.c:368
PPEB Peb
Definition: dllmain.c:27
ULONG ImageProcessAffinityMask
Definition: ntddk_ex.h:307
ULONG OSPlatformId
Definition: ntddk_ex.h:303
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
PVOID ImageBaseAddress
Definition: ntddk_ex.h:245
BYTE BeingDebugged
Definition: btrfs_drv.h:1899
SIZE_T MmMinimumStackCommitInBytes
Definition: mminit.c:370
ULONG NtMajorVersion
Definition: init.c:45
_SEH2_TRY
Definition: create.c:4226
ULONG NtGlobalFlag
Definition: ntddk_ex.h:270
HANDLE Mutant
Definition: ntddk_ex.h:243
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
Definition: compat.h:153
LARGE_INTEGER MmCriticalSectionTimeout
Definition: mminit.c:388
NTSTATUS NTAPI MmMapViewOfSection(IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3907
void * PVOID
Definition: retypes.h:9
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
ULONG HeapSegmentCommit
Definition: ntddk_ex.h:276
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define PCHAR
Definition: match.c:90
Status
Definition: gdiplustypes.h:24
#define STATUS_INVALID_IMAGE_PROTECT
Definition: ntstatus.h:540
ULONG HeapSegmentReserve
Definition: ntddk_ex.h:275
PVOID * ProcessHeaps
Definition: ntddk_ex.h:288
ULONG MaximumNumberOfHeaps
Definition: ntddk_ex.h:287
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
SIZE_T MmHeapDeCommitFreeBlockThreshold
Definition: mminit.c:369
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
uint64_t ULONGLONG
Definition: typedefs.h:67
NTSTATUS NTAPI MiCreatePebOrTeb(IN PEPROCESS Process, IN ULONG Size, OUT PULONG_PTR BaseAddress)
Definition: procsup.c:28
PVOID ExpNlsSectionPointer
Definition: init.c:89
ULONG NtMinorVersion
Definition: init.c:46
LARGE_INTEGER CriticalSectionTimeout
Definition: ntddk_ex.h:274
SIZE_T MinimumStackCommit
Definition: winternl.h:358
ULONG NtBuildNumber
Definition: init.c:50
VOID NTAPI KeDetachProcess(VOID)
Definition: procobj.c:621
SIZE_T MmHeapSegmentReserve
Definition: mminit.c:366
ULONG OSBuildNumber
Definition: ntddk_ex.h:302
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
ULONG SessionId
Definition: btrfs_drv.h:1909
#define RtlImageDirectoryEntryToData
Definition: compat.h:668
#define IMAGE_FILE_UP_SYSTEM_ONLY
Definition: pedump.c:170
ULONG ExpUnicodeCaseTableDataOffset
Definition: init.c:86
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PVOID UnicodeCaseTableData
Definition: ntddk_ex.h:266
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
ULONG ExpOemCodePageDataOffset
Definition: init.c:85
ULONG_PTR SIZE_T
Definition: typedefs.h:80
ULONG OSMinorVersion
Definition: ntddk_ex.h:301
_SEH2_END
Definition: create.c:4400
unsigned short USHORT
Definition: pedump.c:61
ULONG_PTR KAFFINITY
Definition: compat.h:85
VOID NTAPI KeAttachProcess(IN PKPROCESS Process)
Definition: procobj.c:582
SIZE_T MmHeapSegmentCommit
Definition: mminit.c:367
ULONG NumberOfProcessors
Definition: ntddk_ex.h:269
unsigned int * PULONG
Definition: retypes.h:1
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
#define RtlImageNtHeader
Definition: compat.h:665
PVOID OemCodePageData
Definition: ntddk_ex.h:265
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define BOOLEAN
Definition: pedump.c:73
ULONG CmNtCSDVersion
Definition: init.c:59
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t * PULONG_PTR
Definition: typedefs.h:65
ULONG NTAPI MmGetSessionId(IN PEPROCESS Process)
Definition: session.c:179
ULONG HeapDeCommitFreeBlockThreshold
Definition: ntddk_ex.h:278
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
ULONG OSMajorVersion
Definition: ntddk_ex.h:300
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
BOOLEAN InheritedAddressSpace
Definition: ntddk_ex.h:239
struct _PEB PEB
#define DPRINT
Definition: sndvol32.h:71
#define MEM_TOP_DOWN
Definition: nt_native.h:1321
ULONG NtGlobalFlag
Definition: init.c:54
ULONG ExpAnsiCodePageDataOffset
Definition: init.c:85
PVOID AnsiCodePageData
Definition: ntddk_ex.h:264
ULONG ImageSubsystemMinorVersion
Definition: ntddk_ex.h:306
ULONG HeapDeCommitTotalFreeThreshold
Definition: ntddk_ex.h:277
ULONG ImageSubsystem
Definition: ntddk_ex.h:304

Referenced by PspCreateProcess().

◆ MmCreateProcessAddressSpace()

BOOLEAN NTAPI MmCreateProcessAddressSpace ( IN ULONG  MinWs,
IN PEPROCESS  Process,
OUT PULONG_PTR  DirectoryTableBase 
)

Definition at line 1165 of file procsup.c.

1168 {
1169  KIRQL OldIrql;
1170  PFN_NUMBER TableBaseIndex, HyperIndex, WsListIndex;
1171  ULONG Color;
1172 
1173  /* Make sure we don't already have a page directory setup */
1174  ASSERT(Process->Pcb.DirectoryTableBase[0] == 0);
1175  ASSERT(Process->Pcb.DirectoryTableBase[1] == 0);
1176  ASSERT(Process->WorkingSetPage == 0);
1177 
1178  /* Choose a process color */
1179  Process->NextPageColor = (USHORT)RtlRandom(&MmProcessColorSeed);
1180 
1181  /* Setup the hyperspace lock */
1182  KeInitializeSpinLock(&Process->HyperSpaceLock);
1183 
1184  /* Lock PFN database */
1185  OldIrql = MiAcquirePfnLock();
1186 
1187  /*
1188  * Get a page for the table base, one for hyper space & one for the working set list.
1189  * The PFNs for these pages will be initialized in MmInitializeProcessAddressSpace,
1190  * when we are already attached to the process.
1191  * The other pages (if any) are allocated in the arch-specific part.
1192  */
1195  TableBaseIndex = MiRemoveZeroPageSafe(Color);
1196  if (!TableBaseIndex)
1197  {
1198  /* No zero pages, grab a free one */
1199  TableBaseIndex = MiRemoveAnyPage(Color);
1200 
1201  /* Zero it outside the PFN lock */
1202  MiReleasePfnLock(OldIrql);
1203  MiZeroPhysicalPage(TableBaseIndex);
1204  OldIrql = MiAcquirePfnLock();
1205  }
1208  HyperIndex = MiRemoveZeroPageSafe(Color);
1209  if (!HyperIndex)
1210  {
1211  /* No zero pages, grab a free one */
1212  HyperIndex = MiRemoveAnyPage(Color);
1213 
1214  /* Zero it outside the PFN lock */
1215  MiReleasePfnLock(OldIrql);
1216  MiZeroPhysicalPage(HyperIndex);
1217  OldIrql = MiAcquirePfnLock();
1218  }
1221  WsListIndex = MiRemoveZeroPageSafe(Color);
1222  if (!WsListIndex)
1223  {
1224  /* No zero pages, grab a free one */
1225  WsListIndex = MiRemoveAnyPage(Color);
1226 
1227  /* Zero it outside the PFN lock */
1228  MiReleasePfnLock(OldIrql);
1229  MiZeroPhysicalPage(WsListIndex);
1230  }
1231  else
1232  {
1233  /* Release the PFN lock */
1234  MiReleasePfnLock(OldIrql);
1235  }
1236 
1237  /* Set the base directory pointers */
1238  Process->WorkingSetPage = WsListIndex;
1239  DirectoryTableBase[0] = TableBaseIndex << PAGE_SHIFT;
1240  DirectoryTableBase[1] = HyperIndex << PAGE_SHIFT;
1241 
1242  /* Perform the arch-specific parts */
1243  if (!MiArchCreateProcessAddressSpace(Process, DirectoryTableBase))
1244  {
1245  OldIrql = MiAcquirePfnLock();
1246  MiInsertPageInFreeList(WsListIndex);
1247  MiInsertPageInFreeList(HyperIndex);
1248  MiInsertPageInFreeList(TableBaseIndex);
1249  MiReleasePfnLock(OldIrql);
1250  Process->WorkingSetPage = 0;
1251  DirectoryTableBase[0] = 0;
1252  DirectoryTableBase[1] = 0;
1253  return FALSE;
1254  }
1255 
1256  /* Switch to phase 1 initialization */
1257  ASSERT(Process->AddressSpaceInitialized == 0);
1258  Process->AddressSpaceInitialized = 1;
1259 
1260  /* Add the process to the session */
1262  return TRUE;
1263 }
NTSYSAPI ULONG NTAPI RtlRandom(_Inout_ PULONG Seed)
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
VOID NTAPI MiInsertPageInFreeList(IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:603
#define TRUE
Definition: types.h:120
VOID NTAPI MiZeroPhysicalPage(IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:122
struct Color Color
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG PFN_NUMBER
Definition: ke.h:9
#define FALSE
Definition: types.h:117
ULONG MmProcessColorSeed
Definition: procsup.c:20
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:240
FORCEINLINE PFN_NUMBER MiRemoveZeroPageSafe(IN ULONG Color)
Definition: miarm.h:2420
#define MI_SET_USAGE(x)
Definition: mm.h:317
#define ASSERT(a)
Definition: mode.c:44
#define MI_GET_NEXT_PROCESS_COLOR(x)
Definition: miarm.h:238
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
PFN_NUMBER NTAPI MiRemoveAnyPage(IN ULONG Color)
Definition: pfnlist.c:477
unsigned short USHORT
Definition: pedump.c:61
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
VOID NTAPI MiSessionAddProcess(IN PEPROCESS NewProcess)
Definition: session.c:423
unsigned int ULONG
Definition: retypes.h:1
BOOLEAN MiArchCreateProcessAddressSpace(_In_ PEPROCESS Process, _In_ PULONG_PTR DirectoryTableBase)
Definition: procsup.c:21

◆ MmCreateTeb()

NTSTATUS NTAPI MmCreateTeb ( IN PEPROCESS  Process,
IN PCLIENT_ID  ClientId,
IN PINITIAL_TEB  InitialTeb,
OUT PTEB BaseTeb 
)

Definition at line 757 of file procsup.c.

761 {
762  PTEB Teb;
764  *BaseTeb = NULL;
765 
766  //
767  // Attach to Target
768  //
769  KeAttachProcess(&Process->Pcb);
770 
771  //
772  // Allocate the TEB
773  //
774  Status = MiCreatePebOrTeb(Process, sizeof(TEB), (PULONG_PTR)&Teb);
775  if (!NT_SUCCESS(Status))
776  {
777  /* Cleanup and exit */
778  KeDetachProcess();
779  return Status;
780  }
781 
782  //
783  // Use SEH in case we can't load the TEB
784  //
785  _SEH2_TRY
786  {
787  //
788  // Initialize the PEB
789  //
790  RtlZeroMemory(Teb, sizeof(TEB));
791 
792  //
793  // Set TIB Data
794  //
795 #ifdef _M_AMD64
796  Teb->NtTib.ExceptionList = NULL;
797 #else
799 #endif
800  Teb->NtTib.Self = (PNT_TIB)Teb;
801 
802  //
803  // Identify this as an OS/2 V3.0 ("Cruiser") TIB
804  //
805  Teb->NtTib.Version = 30 << 8;
806 
807  //
808  // Set TEB Data
809  //
810  Teb->ClientId = *ClientId;
811  Teb->RealClientId = *ClientId;
812  Teb->ProcessEnvironmentBlock = Process->Peb;
814 
815  //
816  // Check if we have a grandparent TEB
817  //
818  if ((InitialTeb->PreviousStackBase == NULL) &&
819  (InitialTeb->PreviousStackLimit == NULL))
820  {
821  //
822  // Use initial TEB values
823  //
824  Teb->NtTib.StackBase = InitialTeb->StackBase;
825  Teb->NtTib.StackLimit = InitialTeb->StackLimit;
826  Teb->DeallocationStack = InitialTeb->AllocatedStackBase;
827  }
828  else
829  {
830  //
831  // Use grandparent TEB values
832  //
833  Teb->NtTib.StackBase = InitialTeb->PreviousStackBase;
834  Teb->NtTib.StackLimit = InitialTeb->PreviousStackLimit;
835  }
836 
837  //
838  // Initialize the static unicode string
839  //
842  }
844  {
845  //
846  // Get error code
847  //
849  }
850  _SEH2_END;
851 
852  //
853  // Return
854  //
855  KeDetachProcess();
856  *BaseTeb = Teb;
857  return Status;
858 }
struct _NT_TIB * Self
Definition: compat.h:579
WCHAR StaticUnicodeBuffer[261]
Definition: compat.h:736
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
LCID PsDefaultThreadLocaleId
Definition: locale.c:24
_SEH2_TRY
Definition: create.c:4226
CLIENT_ID ClientId
Definition: compat.h:698
UNICODE_STRING StaticUnicodeString
Definition: compat.h:735
ULONG CurrentLocale
Definition: compat.h:708
Status
Definition: gdiplustypes.h:24
DWORD Version
Definition: compat.h:576
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1165
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PVOID DeallocationStack
Definition: compat.h:737
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
NTSTATUS NTAPI MiCreatePebOrTeb(IN PEPROCESS Process, IN ULONG Size, OUT PULONG_PTR BaseAddress)
Definition: procsup.c:28
struct _NT_TIB * PNT_TIB
struct _EXCEPTION_REGISTRATION_RECORD * ExceptionList
Definition: compat.h:570
CLIENT_ID RealClientId
Definition: compat.h:720
VOID NTAPI KeDetachProcess(VOID)
Definition: procobj.c:621
Definition: compat.h:694
_SEH2_END
Definition: create.c:4400
#define EXCEPTION_CHAIN_END
Definition: rtltypes.h:63
PVOID StackBase
Definition: compat.h:571
VOID NTAPI KeAttachProcess(IN PKPROCESS Process)
Definition: procobj.c:582
#define NULL
Definition: types.h:112
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t * PULONG_PTR
Definition: typedefs.h:65
PVOID StackLimit
Definition: compat.h:572
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
NT_TIB NtTib
Definition: ntddk_ex.h:332

Referenced by PspCreateThread().

◆ MmDeleteKernelStack()

VOID NTAPI MmDeleteKernelStack ( IN PVOID  StackBase,
IN BOOLEAN  GuiStack 
)

Definition at line 186 of file procsup.c.

188 {
189  PMMPTE PointerPte;
190  PFN_NUMBER PageFrameNumber, PageTableFrameNumber;
191  PFN_COUNT StackPages;
192  PMMPFN Pfn1, Pfn2;
193  ULONG i;
194  KIRQL OldIrql;
195  PSLIST_ENTRY SListEntry;
196 
197  //
198  // This should be the guard page, so decrement by one
199  //
200  PointerPte = MiAddressToPte(StackBase);
201  PointerPte--;
202 
203  //
204  // If this is a small stack, just push the stack onto the dead stack S-LIST
205  //
206  if (!GuiStack)
207  {
209  {
210  SListEntry = ((PSLIST_ENTRY)StackBase) - 1;
212  return;
213  }
214  }
215 
216  //
217  // Calculate pages used
218  //
219  StackPages = BYTES_TO_PAGES(GuiStack ?
221 
222  /* Acquire the PFN lock */
223  OldIrql = MiAcquirePfnLock();
224 
225  //
226  // Loop them
227  //
228  for (i = 0; i < StackPages; i++)
229  {
230  //
231  // Check if this is a valid PTE
232  //
233  if (PointerPte->u.Hard.Valid == 1)
234  {
235  /* Get the PTE's page */
236  PageFrameNumber = PFN_FROM_PTE(PointerPte);
237  Pfn1 = MiGetPfnEntry(PageFrameNumber);
238 
239  /* Now get the page of the page table mapping it */
240  PageTableFrameNumber = Pfn1->u4.PteFrame;
241  Pfn2 = MiGetPfnEntry(PageTableFrameNumber);
242 
243  /* Remove a shared reference, since the page is going away */
244  MiDecrementShareCount(Pfn2, PageTableFrameNumber);
245 
246  /* Set the special pending delete marker */
247  MI_SET_PFN_DELETED(Pfn1);
248 
249  /* And now delete the actual stack page */
250  MiDecrementShareCount(Pfn1, PageFrameNumber);
251  }
252 
253  //
254  // Next one
255  //
256  PointerPte--;
257  }
258 
259  //
260  // We should be at the guard page now
261  //
262  ASSERT(PointerPte->u.Hard.Valid == 0);
263 
264  /* Release the PFN lock */
265  MiReleasePfnLock(OldIrql);
266 
267  //
268  // Release the PTEs
269  //
270  MiReleaseSystemPtes(PointerPte, StackPages + 1, SystemPteSpace);
271 }
ULONG PFN_COUNT
Definition: mmtypes.h:102
ULONG MmLargeStackSize
Definition: mminit.c:262
SLIST_HEADER MmDeadStackSListHead
Definition: procsup.c:22
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:9
ULONG MmMaximumDeadKernelStacks
Definition: procsup.c:21
VOID NTAPI MiDecrementShareCount(IN PMMPFN Pfn1, IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:1133
VOID NTAPI MiReleaseSystemPtes(IN PMMPTE StartingPte, IN ULONG NumberOfPtes, IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType)
Definition: syspte.c:264
#define ASSERT(a)
Definition: mode.c:44
ULONG64 Valid
Definition: mmtypes.h:150
union _MMPFN::@1754 u4
#define PSLIST_ENTRY
Definition: rtltypes.h:134
FORCEINLINE USHORT ExQueryDepthSList(_In_ PSLIST_HEADER SListHead)
Definition: exfuncs.h:153
#define KERNEL_STACK_SIZE
#define BYTES_TO_PAGES(Size)
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
#define InterlockedPushEntrySList(SListHead, SListEntry)
Definition: rtlfuncs.h:3389
Definition: mm.h:373
FORCEINLINE PMMPFN MiGetPfnEntry(IN PFN_NUMBER Pfn)
Definition: mm.h:1020
union _MMPTE::@2282 u
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
#define MI_SET_PFN_DELETED(x)
Definition: miarm.h:194
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
ULONG_PTR PteFrame
Definition: mm.h:418
unsigned int ULONG
Definition: retypes.h:1
#define PFN_FROM_PTE(v)
Definition: mm.h:92

◆ MmDeleteProcessAddressSpace()

VOID NTAPI MmDeleteProcessAddressSpace ( IN PEPROCESS  Process)

Definition at line 1361 of file procsup.c.

1362 {
1363  PMMPFN Pfn1, Pfn2;
1364  KIRQL OldIrql;
1365  PFN_NUMBER PageFrameIndex;
1366 
1367 #ifndef _M_AMD64
1369  RemoveEntryList(&Process->MmProcessLinks);
1371 #endif
1372 
1373  //ASSERT(Process->CommitCharge == 0);
1374 
1375  /* Remove us from the list */
1377  RemoveEntryList(&Process->Vm.WorkingSetExpansionLinks);
1379 
1380  /* Acquire the PFN lock */
1381  OldIrql = MiAcquirePfnLock();
1382 
1383  /* Check for fully initialized process */
1384  if (Process->AddressSpaceInitialized == 2)
1385  {
1386  /* Map the working set page and its page table */
1387  Pfn1 = MiGetPfnEntry(Process->WorkingSetPage);
1388  Pfn2 = MiGetPfnEntry(Pfn1->u4.PteFrame);
1389 
1390  /* Nuke it */
1391  MI_SET_PFN_DELETED(Pfn1);
1392  MiDecrementShareCount(Pfn2, Pfn1->u4.PteFrame);
1393  MiDecrementShareCount(Pfn1, Process->WorkingSetPage);
1394  ASSERT((Pfn1->u3.e2.ReferenceCount == 0) || (Pfn1->u3.e1.WriteInProgress));
1395 
1396  /* Now map hyperspace and its page table */
1397  PageFrameIndex = Process->Pcb.DirectoryTableBase[1] >> PAGE_SHIFT;
1398  Pfn1 = MiGetPfnEntry(PageFrameIndex);
1399  Pfn2 = MiGetPfnEntry(Pfn1->u4.PteFrame);
1400 
1401  /* Nuke it */
1402  MI_SET_PFN_DELETED(Pfn1);
1403  MiDecrementShareCount(Pfn2, Pfn1->u4.PteFrame);
1404  MiDecrementShareCount(Pfn1, PageFrameIndex);
1405  ASSERT((Pfn1->u3.e2.ReferenceCount == 0) || (Pfn1->u3.e1.WriteInProgress));
1406 
1407  /* Finally, nuke the PDE itself */
1408  PageFrameIndex = Process->Pcb.DirectoryTableBase[0] >> PAGE_SHIFT;
1409  Pfn1 = MiGetPfnEntry(PageFrameIndex);
1410  MI_SET_PFN_DELETED(Pfn1);
1411  MiDecrementShareCount(Pfn1, PageFrameIndex);
1412  MiDecrementShareCount(Pfn1, PageFrameIndex);
1413 
1414  /* Page table is now dead. Bye bye... */
1415  ASSERT((Pfn1->u3.e2.ReferenceCount == 0) || (Pfn1->u3.e1.WriteInProgress));
1416  }
1417  else
1418  {
1419  /* A partly-initialized process should never exit through here */
1420  ASSERT(FALSE);
1421  }
1422 
1423  /* Release the PFN lock */
1424  MiReleasePfnLock(OldIrql);
1425 
1426  /* Drop a reference on the session */
1428 
1429  /* Clear out the PDE pages */
1430  Process->Pcb.DirectoryTableBase[0] = 0;
1431  Process->Pcb.DirectoryTableBase[1] = 0;
1432 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
FORCEINLINE KIRQL MiAcquireExpansionLock(VOID)
Definition: miarm.h:1536
VOID NTAPI MiReleaseProcessReferenceToSessionDataPage(IN PMM_SESSION_SPACE SessionGlobal)
Definition: session.c:209
struct _MMPFN::@1751::@1757 e2
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
UCHAR KIRQL
Definition: env_spec_w32.h:591
MMPFNENTRY e1
Definition: mm.h:397
ULONG PFN_NUMBER
Definition: ke.h:9
#define FALSE
Definition: types.h:117
VOID NTAPI MiDecrementShareCount(IN PMMPFN Pfn1, IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:1133
USHORT WriteInProgress
Definition: mm.h:362
union _MMPFN::@1751 u3
#define ASSERT(a)
Definition: mode.c:44
union _MMPFN::@1754 u4
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
Definition: mm.h:373
FORCEINLINE PMMPFN MiGetPfnEntry(IN PFN_NUMBER Pfn)
Definition: mm.h:1020
#define MI_SET_PFN_DELETED(x)
Definition: miarm.h:194
ULONG_PTR PteFrame
Definition: mm.h:418
FORCEINLINE VOID MiReleaseExpansionLock(KIRQL OldIrql)
Definition: miarm.h:1549
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219

Referenced by PspDeleteProcess().

◆ MmDeleteTeb()

VOID NTAPI MmDeleteTeb ( IN PEPROCESS  Process,
IN PTEB  Teb 
)

Definition at line 121 of file procsup.c.

123 {
124  ULONG_PTR TebEnd;
126  PMMVAD Vad;
127  PMM_AVL_TABLE VadTree = &Process->VadRoot;
128  DPRINT("Deleting TEB: %p in %16s\n", Teb, Process->ImageFileName);
129 
130  /* TEB is one page */
131  TebEnd = (ULONG_PTR)Teb + ROUND_TO_PAGES(sizeof(TEB)) - 1;
132 
133  /* Attach to the process */
134  KeAttachProcess(&Process->Pcb);
135 
136  /* Lock the process address space */
137  KeAcquireGuardedMutex(&Process->AddressCreationLock);
138 
139  /* Find the VAD, make sure it's a TEB VAD */
140  Vad = MiLocateAddress(Teb);
141  DPRINT("Removing node for VAD: %lx %lx\n", Vad->StartingVpn, Vad->EndingVpn);
142  ASSERT(Vad != NULL);
143  if (Vad->StartingVpn != ((ULONG_PTR)Teb >> PAGE_SHIFT))
144  {
145  /* Bug in the AVL code? */
146  DPRINT1("Corrupted VAD!\n");
147  }
148  else
149  {
150  /* Sanity checks for a valid TEB VAD */
151  ASSERT((Vad->StartingVpn == ((ULONG_PTR)Teb >> PAGE_SHIFT) &&
152  (Vad->EndingVpn == (TebEnd >> PAGE_SHIFT))));
153  ASSERT(Vad->u.VadFlags.NoChange == TRUE);
154  ASSERT(Vad->u2.VadFlags2.OneSecured == TRUE);
156 
157  /* Lock the working set */
159 
160  /* Remove this VAD from the tree */
161  ASSERT(VadTree->NumberGenericTableElements >= 1);
162  MiRemoveNode((PMMADDRESS_NODE)Vad, VadTree);
163 
164  /* Delete the pages */
165  MiDeleteVirtualAddresses((ULONG_PTR)Teb, TebEnd, NULL);
166 
167  /* Release the working set */
169 
170  /* Remove the VAD */
171  ExFreePool(Vad);
172 
173  /* Return the quota the VAD used */
175  }
176 
177  /* Release the address space lock */
178  KeReleaseGuardedMutex(&Process->AddressCreationLock);
179 
180  /* Detach */
181  KeDetachProcess();
182 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
FORCEINLINE VOID MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1244
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
VOID FASTCALL KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:42
ULONG OneSecured
Definition: mmtypes.h:705
#define TRUE
Definition: types.h:120
ULONG_PTR NumberGenericTableElements
Definition: mmtypes.h:665
ULONG_PTR NoChange
Definition: mmtypes.h:690
MMVAD_FLAGS VadFlags
Definition: mmtypes.h:731
uint32_t ULONG_PTR
Definition: typedefs.h:65
ULONG_PTR EndingVpn
Definition: mmtypes.h:727
union _MMVAD::@2564 u2
#define FALSE
Definition: types.h:117
PMMVAD NTAPI MiLocateAddress(IN PVOID VirtualAddress)
Definition: vadnode.c:48
ULONG_PTR StartingVpn
Definition: mmtypes.h:726
MMVAD_FLAGS2 VadFlags2
Definition: mmtypes.h:739
ULONG MultipleSecured
Definition: mmtypes.h:706
FORCEINLINE VOID MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1174
#define ASSERT(a)
Definition: mode.c:44
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
union _MMVAD::@2563 u
VOID NTAPI KeDetachProcess(VOID)
Definition: procobj.c:621
VOID NTAPI MiRemoveNode(IN PMMADDRESS_NODE Node, IN PMM_AVL_TABLE Table)
Definition: vadnode.c:360
VOID NTAPI MiDeleteVirtualAddresses(IN ULONG_PTR Va, IN ULONG_PTR EndingAddress, IN PMMVAD Vad)
Definition: virtual.c:530
Definition: compat.h:694
#define ROUND_TO_PAGES(Size)
VOID NTAPI KeAttachProcess(IN PKPROCESS Process)
Definition: procobj.c:582
#define NULL
Definition: types.h:112
VOID FASTCALL KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:53
#define DPRINT1
Definition: precomp.h:8
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define ULONG_PTR
Definition: config.h:101
#define DPRINT
Definition: sndvol32.h:71
VOID NTAPI PsReturnProcessNonPagedPoolQuota(_In_ PEPROCESS Process, _In_ SIZE_T Amount)
Returns the non paged quota pool that the process was taking up.
Definition: quota.c:938
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

◆ MmGrowKernelStack()

NTSTATUS NTAPI MmGrowKernelStack ( IN PVOID  StackPointer)

Definition at line 476 of file procsup.c.

477 {
478  //
479  // Call the extended version
480  //
481  return MmGrowKernelStackEx(StackPointer, KERNEL_LARGE_STACK_COMMIT);
482 }
#define KERNEL_LARGE_STACK_COMMIT
NTSTATUS NTAPI MmGrowKernelStackEx(IN PVOID StackPointer, IN ULONG GrowSize)
Definition: procsup.c:388

Referenced by KiUserModeCallout().

◆ MmGrowKernelStackEx()

NTSTATUS NTAPI MmGrowKernelStackEx ( IN PVOID  StackPointer,
IN ULONG  GrowSize 
)

Definition at line 388 of file procsup.c.

390 {
392  PMMPTE LimitPte, NewLimitPte, LastPte;
393  KIRQL OldIrql;
394  MMPTE TempPte, InvalidPte;
395  PFN_NUMBER PageFrameIndex;
396 
397  //
398  // Make sure the stack did not overflow
399  //
400  ASSERT(((ULONG_PTR)Thread->StackBase - (ULONG_PTR)Thread->StackLimit) <=
402 
403  //
404  // Get the current stack limit
405  //
406  LimitPte = MiAddressToPte(Thread->StackLimit);
407  ASSERT(LimitPte->u.Hard.Valid == 1);
408 
409  //
410  // Get the new one and make sure this isn't a retarded request
411  //
412  NewLimitPte = MiAddressToPte((PVOID)((ULONG_PTR)StackPointer - GrowSize));
413  if (NewLimitPte == LimitPte) return STATUS_SUCCESS;
414 
415  //
416  // Now make sure you're not going past the reserved space
417  //
418  LastPte = MiAddressToPte((PVOID)((ULONG_PTR)Thread->StackBase -
420  if (NewLimitPte < LastPte)
421  {
422  //
423  // Sorry!
424  //
425  return STATUS_STACK_OVERFLOW;
426  }
427 
428  //
429  // Calculate the number of new pages
430  //
431  LimitPte--;
432 
433  /* Setup the temporary invalid PTE */
434  MI_MAKE_SOFTWARE_PTE(&InvalidPte, MM_NOACCESS);
435 
436  //
437  // Acquire the PFN DB lock
438  //
439  OldIrql = MiAcquirePfnLock();
440 
441  //
442  // Loop each stack page
443  //
444  while (LimitPte >= NewLimitPte)
445  {
446  /* Get a page and write the current invalid PTE */
448  MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName);
449  PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
450  MI_WRITE_INVALID_PTE(LimitPte, InvalidPte);
451 
452  /* Initialize the PFN entry for this page */
453  MiInitializePfn(PageFrameIndex, LimitPte, 1);
454 
455  /* Setup the template stack PTE */
456  MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte, LimitPte, MM_READWRITE, PageFrameIndex);
457 
458  /* Write the valid PTE */
459  MI_WRITE_VALID_PTE(LimitPte--, TempPte);
460  }
461 
462  //
463  // Release the PFN lock
464  //
465  MiReleasePfnLock(OldIrql);
466 
467  //
468  // Set the new limit
469  //
470  Thread->StackLimit = (ULONG_PTR)MiPteToAddress(NewLimitPte);
471  return STATUS_SUCCESS;
472 }
#define MI_MAKE_SOFTWARE_PTE(p, x)
Definition: miarm.h:189
VOID NTAPI MiInitializePfn(IN PFN_NUMBER PageFrameIndex, IN PMMPTE PointerPte, IN BOOLEAN Modified)
Definition: pfnlist.c:962
#define MI_GET_NEXT_COLOR()
Definition: miarm.h:237
#define MM_READWRITE
Definition: bootanim.c:19
#define MM_NOACCESS
Definition: miarm.h:65
ULONG MmLargeStackSize
Definition: mminit.c:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:9
FORCEINLINE VOID MI_WRITE_VALID_PTE(IN PMMPTE PointerPte, IN MMPTE TempPte)
Definition: miarm.h:964
#define MI_SET_PROCESS2(x)
Definition: mm.h:319
#define PsGetCurrentProcess
Definition: psfuncs.h:17
#define MI_SET_USAGE(x)
Definition: mm.h:317
#define ASSERT(a)
Definition: mode.c:44
ULONG64 Valid
Definition: mmtypes.h:150
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
#define PAGE_SIZE
Definition: env_spec_w32.h:49
union _MMPTE::@2282 u
PFN_NUMBER NTAPI MiRemoveAnyPage(IN ULONG Color)
Definition: pfnlist.c:477
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
FORCEINLINE VOID MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte, IN MMPTE InvalidPte)
Definition: miarm.h:997
#define STATUS_STACK_OVERFLOW
Definition: ntstatus.h:489
#define ULONG_PTR
Definition: config.h:101
#define STATUS_SUCCESS
Definition: shellext.h:65
FORCEINLINE VOID MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte, IN PMMPTE MappingPte, IN ULONG_PTR ProtectionMask, IN PFN_NUMBER PageFrameNumber)
Definition: miarm.h:778
#define KeGetCurrentThread
Definition: hal.h:55
FORCEINLINE PVOID MiPteToAddress(PMMPTE PointerPte)
Definition: mm.h:208

Referenced by MmGrowKernelStack().

◆ MmInitializeHandBuiltProcess()

NTSTATUS NTAPI MmInitializeHandBuiltProcess ( IN PEPROCESS  Process,
IN PULONG_PTR  DirectoryTableBase 
)

Definition at line 1131 of file procsup.c.

1133 {
1134  /* Share the directory base with the idle process */
1135  DirectoryTableBase[0] = PsGetCurrentProcess()->Pcb.DirectoryTableBase[0];
1136  DirectoryTableBase[1] = PsGetCurrentProcess()->Pcb.DirectoryTableBase[1];
1137 
1138  /* Initialize the Addresss Space */
1139  KeInitializeGuardedMutex(&Process->AddressCreationLock);
1140  KeInitializeSpinLock(&Process->HyperSpaceLock);
1141  Process->Vm.WorkingSetExpansionLinks.Flink = NULL;
1142  ASSERT(Process->VadRoot.NumberGenericTableElements == 0);
1143  Process->VadRoot.BalancedRoot.u1.Parent = &Process->VadRoot.BalancedRoot;
1144 
1145  /* Use idle process Working set */
1146  Process->Vm.VmWorkingSetList = PsGetCurrentProcess()->Vm.VmWorkingSetList;
1147  Process->WorkingSetPage = PsGetCurrentProcess()->WorkingSetPage;
1148 
1149  /* Done */
1150  Process->HasAddressSpace = TRUE;//??
1151  return STATUS_SUCCESS;
1152 }
#define TRUE
Definition: types.h:120
#define PsGetCurrentProcess
Definition: psfuncs.h:17
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:240
#define ASSERT(a)
Definition: mode.c:44
VOID FASTCALL KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:31
#define NULL
Definition: types.h:112
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define STATUS_SUCCESS
Definition: shellext.h:65

Referenced by PspCreateProcess().

◆ MmInitializeHandBuiltProcess2()

NTSTATUS NTAPI MmInitializeHandBuiltProcess2 ( IN PEPROCESS  Process)

Definition at line 1157 of file procsup.c.

1158 {
1159  /* Lock the VAD, ARM3-owned ranges away */
1160  return STATUS_SUCCESS;
1161 }
#define STATUS_SUCCESS
Definition: shellext.h:65

Referenced by PspCreateProcess().

◆ MmInitializeProcessAddressSpace()

NTSTATUS NTAPI MmInitializeProcessAddressSpace ( IN PEPROCESS  Process,
IN PEPROCESS ProcessClone  OPTIONAL,
IN PVOID Section  OPTIONAL,
IN OUT PULONG  Flags,
IN POBJECT_NAME_INFORMATION *AuditName  OPTIONAL 
)

Definition at line 936 of file procsup.c.

941 {
943  SIZE_T ViewSize = 0;
944  PVOID ImageBase = 0;
945  PMMPTE PointerPte;
946  KIRQL OldIrql;
947  PMMPDE PointerPde;
948  PMMPFN Pfn;
949  PFN_NUMBER PageFrameNumber;
951  PWCHAR Source;
953  USHORT Length = 0;
954 
955 #if (_MI_PAGING_LEVELS >= 3)
956  PMMPPE PointerPpe;
957 #endif
958 #if (_MI_PAGING_LEVELS == 4)
959  PMMPXE PointerPxe;
960 #endif
961 
962  /* We should have a PDE */
963  ASSERT(Process->Pcb.DirectoryTableBase[0] != 0);
964  ASSERT(Process->PdeUpdateNeeded == FALSE);
965 
966  /* Attach to the process */
967  KeAttachProcess(&Process->Pcb);
968 
969  /* The address space should now been in phase 1 or 0 */
970  ASSERT(Process->AddressSpaceInitialized <= 1);
971  Process->AddressSpaceInitialized = 2;
972 
973  /* Initialize the Addresss Space lock */
974  KeInitializeGuardedMutex(&Process->AddressCreationLock);
975  Process->Vm.WorkingSetExpansionLinks.Flink = NULL;
976 
977  /* Initialize AVL tree */
978  ASSERT(Process->VadRoot.NumberGenericTableElements == 0);
979  Process->VadRoot.BalancedRoot.u1.Parent = &Process->VadRoot.BalancedRoot;
980 
981  /* Lock our working set */
983 
984  /* Lock PFN database */
985  OldIrql = MiAcquirePfnLock();
986 
987  /* Setup the PFN for the PDE base of this process */
988 #if (_MI_PAGING_LEVELS == 4)
989  PointerPte = MiAddressToPte(PXE_BASE);
990 #elif (_MI_PAGING_LEVELS == 3)
991  PointerPte = MiAddressToPte(PPE_BASE);
992 #else
993  PointerPte = MiAddressToPte(PDE_BASE);
994 #endif
995  PageFrameNumber = PFN_FROM_PTE(PointerPte);
996  ASSERT(Process->Pcb.DirectoryTableBase[0] == PageFrameNumber * PAGE_SIZE);
997  MiInitializePfn(PageFrameNumber, PointerPte, TRUE);
998 
999  /* Do the same for hyperspace */
1000  PointerPde = MiAddressToPde(HYPER_SPACE);
1001  PageFrameNumber = PFN_FROM_PTE(PointerPde);
1002  MiInitializePfn(PageFrameNumber, (PMMPTE)PointerPde, TRUE);
1003 #if (_MI_PAGING_LEVELS == 2)
1004  ASSERT(Process->Pcb.DirectoryTableBase[1] == PageFrameNumber * PAGE_SIZE);
1005 #endif
1006 
1007 #if (_MI_PAGING_LEVELS >= 3)
1008  PointerPpe = MiAddressToPpe((PVOID)HYPER_SPACE);
1009  PageFrameNumber = PFN_FROM_PTE(PointerPpe);
1010  MiInitializePfn(PageFrameNumber, PointerPpe, TRUE);
1011 #if (_MI_PAGING_LEVELS == 3)
1012  ASSERT(Process->Pcb.DirectoryTableBase[1] == PageFrameNumber * PAGE_SIZE);
1013 #endif
1014 #endif
1015 #if (_MI_PAGING_LEVELS == 4)
1016  PointerPxe = MiAddressToPxe((PVOID)HYPER_SPACE);
1017  PageFrameNumber = PFN_FROM_PTE(PointerPxe);
1018  MiInitializePfn(PageFrameNumber, PointerPxe, TRUE);
1019  ASSERT(Process->Pcb.DirectoryTableBase[1] == PageFrameNumber * PAGE_SIZE);
1020 #endif
1021 
1022  /* Do the same for the Working set list */
1023  PointerPte = MiAddressToPte(MmWorkingSetList);
1024  PageFrameNumber = PFN_FROM_PTE(PointerPte);
1025  MiInitializePfn(PageFrameNumber, PointerPte, TRUE);
1026 
1027  /* All our pages are now active & valid. Release the lock. */
1028  MiReleasePfnLock(OldIrql);
1029 
1030  /* This should be in hyper space, but not in the mapping range */
1031  Process->Vm.VmWorkingSetList = MmWorkingSetList;
1033 
1034  /* Now initialize the working set list */
1036 
1037  /* The rule is that the owner process is always in the FLINK of the PDE's PFN entry */
1038  Pfn = MiGetPfnEntry(Process->Pcb.DirectoryTableBase[0] >> PAGE_SHIFT);
1039  ASSERT(Pfn->u4.PteFrame == MiGetPfnEntryIndex(Pfn));
1040  ASSERT(Pfn->u1.WsIndex == 0);
1041  Pfn->u1.Event = (PKEVENT)Process;
1042 
1043  /* Sanity check */
1044  ASSERT(Process->PhysicalVadRoot == NULL);
1045 
1046  /* Release the process working set */
1048 
1049 #ifdef _M_AMD64
1050  /* On x64 we need a VAD for the shared user page */
1051  Status = MiInsertSharedUserPageVad(Process);
1052  if (!NT_SUCCESS(Status))
1053  {
1054  DPRINT1("MiCreateSharedUserPageVad() failed: 0x%lx\n", Status);
1055  return Status;
1056  }
1057 #endif
1058 
1059  /* Check if there's a Section Object */
1060  if (Section)
1061  {
1062  /* Determine the image file name and save it to EPROCESS */
1064  FileName = FileObject->FileName;
1065  Source = (PWCHAR)((PCHAR)FileName.Buffer + FileName.Length);
1066  if (FileName.Buffer)
1067  {
1068  /* Loop the file name*/
1069  while (Source > FileName.Buffer)
1070  {
1071  /* Make sure this isn't a backslash */
1072  if (*--Source == OBJ_NAME_PATH_SEPARATOR)
1073  {
1074  /* If so, stop it here */
1075  Source++;
1076  break;
1077  }
1078  else
1079  {
1080  /* Otherwise, keep going */
1081  Length++;
1082  }
1083  }
1084  }
1085 
1086  /* Copy the to the process and truncate it to 15 characters if necessary */
1087  Destination = Process->ImageFileName;
1088  Length = min(Length, sizeof(Process->ImageFileName) - 1);
1089  while (Length--) *Destination++ = (UCHAR)*Source++;
1091 
1092  /* Check if caller wants an audit name */
1093  if (AuditName)
1094  {
1095  /* Setup the audit name */
1097  if (!NT_SUCCESS(Status))
1098  {
1099  /* Fail */
1100  KeDetachProcess();
1101  return Status;
1102  }
1103  }
1104 
1105  /* Map the section */
1106  Status = MmMapViewOfSection(Section,
1107  Process,
1108  (PVOID*)&ImageBase,
1109  0,
1110  0,
1111  NULL,
1112  &ViewSize,
1113  0,
1114  MEM_COMMIT,
1115  PAGE_READWRITE);
1116 
1117  /* Save the pointer */
1118  Process->SectionBaseAddress = ImageBase;
1119  }
1120 
1121  /* Be nice and detach */
1122  KeDetachProcess();
1123 
1124  /* Return status to caller */
1125  return Status;
1126 }
signed char * PCHAR
Definition: retypes.h:7
PFILE_OBJECT NTAPI MmGetFileObjectForSection(IN PVOID Section)
Definition: section.c:1755
VOID NTAPI MiInitializePfn(IN PFN_NUMBER PageFrameIndex, IN PMMPTE PointerPte, IN BOOLEAN Modified)
Definition: pfnlist.c:962
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
struct _KEVENT * PKEVENT
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define MiAddressToPde(x)
Definition: mmx86.c:20
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
FORCEINLINE PMMPTE MiAddressToPpe(PVOID Address)
Definition: mm.h:161
FORCEINLINE VOID MiLockProcessWorkingSet(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1129
uint16_t * PWCHAR
Definition: typedefs.h:56
_Use_decl_annotations_ VOID NTAPI MiInitializeWorkingSetList(_Inout_ PMMSUPPORT WorkingSet)
Definition: wslist.cpp:369
#define MEM_COMMIT
Definition: nt_native.h:1313
uint32_t ULONG_PTR
Definition: typedefs.h:65
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:9
#define FALSE
Definition: types.h:117
#define ANSI_NULL
#define PDE_BASE
Definition: winldr.c:21
#define OBJ_NAME_PATH_SEPARATOR
Definition: arcname_tests.c:25
PKEVENT Event
Definition: mm.h:379
NTSTATUS NTAPI MmMapViewOfSection(IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3907
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:547
Status
Definition: gdiplustypes.h:24
FORCEINLINE PMMPTE MiAddressToPxe(PVOID Address)
Definition: mm.h:171
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MI_MAPPING_RANGE_END
Definition: mm.h:48
union _MMPFN::@1754 u4
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2977
#define PPE_BASE
* PFILE_OBJECT
Definition: iotypes.h:1998
#define HYPER_SPACE_END
Definition: mm.h:15
VOID NTAPI KeDetachProcess(VOID)
Definition: procobj.c:621
unsigned char UCHAR
Definition: xmlstorage.h:181
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
Definition: mm.h:373
#define PAGE_SIZE
Definition: env_spec_w32.h:49
NTSTATUS NTAPI SeInitializeProcessAuditName(_In_ PFILE_OBJECT FileObject, _In_ BOOLEAN DoAudit, _Out_ POBJECT_NAME_INFORMATION *AuditInfo)
Initializes a process audit name and returns it to the caller.
Definition: audit.c:105
FORCEINLINE PMMPFN MiGetPfnEntry(IN PFN_NUMBER Pfn)
Definition: mm.h:1020
PMMWSL MmWorkingSetList
Definition: wslist.cpp:19
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define PXE_BASE
struct _FileName FileName
Definition: fatprocs.h:893
VOID FASTCALL KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:31
unsigned short USHORT
Definition: pedump.c:61
ULONG_PTR PteFrame
Definition: mm.h:418
VOID NTAPI KeAttachProcess(IN PKPROCESS Process)
Definition: procobj.c:582
#define min(a, b)
Definition: monoChain.cc:55
#define NULL
Definition: types.h:112
union _MMPFN::@1749 u1
#define DPRINT1
Definition: precomp.h:8
ULONG WsIndex
Definition: mm.h:378
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
FORCEINLINE PFN_NUMBER MiGetPfnEntryIndex(IN PMMPFN Pfn1)
Definition: mm.h:1040
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
#define HYPER_SPACE
Definition: mm.h:14
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
#define STATUS_SUCCESS
Definition: shellext.h:65
#define PFN_FROM_PTE(v)
Definition: mm.h:92
FORCEINLINE VOID MiUnlockProcessWorkingSet(IN PEPROCESS Process, IN PETHREAD Thread)
Definition: miarm.h:1199
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ MmSetMemoryPriorityProcess()

NTSTATUS NTAPI MmSetMemoryPriorityProcess ( IN PEPROCESS  Process,
IN UCHAR  MemoryPriority 
)

Definition at line 486 of file procsup.c.

488 {
489  UCHAR OldPriority;
490 
491  //
492  // Check if we have less then 16MB of Physical Memory
493  //
494  if ((MmSystemSize == MmSmallSystem) &&
495  (MmNumberOfPhysicalPages < ((15 * 1024 * 1024) / PAGE_SIZE)))
496  {
497  //
498  // Always use background priority
499  //
500  MemoryPriority = MEMORY_PRIORITY_BACKGROUND;
501  }
502 
503  //
504  // Save the old priority and update it
505  //
506  OldPriority = (UCHAR)Process->Vm.Flags.MemoryPriority;
507  Process->Vm.Flags.MemoryPriority = MemoryPriority;
508 
509  //
510  // Return the old priority
511  //
512  return OldPriority;
513 }
#define MEMORY_PRIORITY_BACKGROUND
Definition: pstypes.h:124
unsigned char UCHAR
Definition: xmlstorage.h:181
#define PAGE_SIZE
Definition: env_spec_w32.h:49
MM_SYSTEMSIZE MmSystemSize
Definition: mminit.c:326
PFN_COUNT MmNumberOfPhysicalPages
Definition: init.c:48
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219

Referenced by NtSetInformationProcess(), and PspComputeQuantumAndPriority().

◆ NtAllocateUserPhysicalPages()

NTSTATUS NTAPI NtAllocateUserPhysicalPages ( IN HANDLE  ProcessHandle,
IN OUT PULONG_PTR  NumberOfPages,
IN OUT PULONG_PTR  UserPfnArray 
)

Definition at line 1439 of file procsup.c.

1442 {
1443  UNIMPLEMENTED;
1444  return STATUS_NOT_IMPLEMENTED;
1445 }
return STATUS_NOT_IMPLEMENTED
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by AllocateUserPhysicalPages().

◆ NtFreeUserPhysicalPages()

NTSTATUS NTAPI NtFreeUserPhysicalPages ( IN HANDLE  ProcessHandle,
IN OUT PULONG_PTR  NumberOfPages,
IN OUT PULONG_PTR  UserPfnArray 
)

Definition at line 1469 of file procsup.c.

1472 {
1473  UNIMPLEMENTED;
1474  return STATUS_NOT_IMPLEMENTED;
1475 }
return STATUS_NOT_IMPLEMENTED
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by FreeUserPhysicalPages().

◆ NtMapUserPhysicalPages()

NTSTATUS NTAPI NtMapUserPhysicalPages ( IN PVOID  VirtualAddresses,
IN ULONG_PTR  NumberOfPages,
IN OUT PULONG_PTR  UserPfnArray 
)

Definition at line 1449 of file procsup.c.

1452 {
1453  UNIMPLEMENTED;
1454  return STATUS_NOT_IMPLEMENTED;
1455 }
return STATUS_NOT_IMPLEMENTED
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by MapUserPhysicalPages().

◆ NtMapUserPhysicalPagesScatter()

NTSTATUS NTAPI NtMapUserPhysicalPagesScatter ( IN PVOID VirtualAddresses,
IN ULONG_PTR  NumberOfPages,
IN OUT PULONG_PTR  UserPfnArray 
)

Definition at line 1459 of file procsup.c.

1462 {
1463  UNIMPLEMENTED;
1464  return STATUS_NOT_IMPLEMENTED;
1465 }
return STATUS_NOT_IMPLEMENTED
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by MapUserPhysicalPagesScatter().

Variable Documentation

◆ MmDeadStackSListHead

SLIST_HEADER MmDeadStackSListHead

Definition at line 22 of file procsup.c.

Referenced by MmArmInitSystem(), MmCreateKernelStack(), and MmDeleteKernelStack().

◆ MmMaximumDeadKernelStacks

ULONG MmMaximumDeadKernelStacks = 5

Definition at line 21 of file procsup.c.

Referenced by MmArmInitSystem(), and MmDeleteKernelStack().

◆ MmProcessColorSeed

ULONG MmProcessColorSeed = 0x12345678

Definition at line 20 of file procsup.c.

Referenced by MmCreateProcessAddressSpace().