Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2193 lines
47 KiB

  1. /*++ BUILD Version: 0009 // Increment this if a change has global effects
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ps.h
  5. Abstract:
  6. This module contains the process structure public data structures and
  7. procedure prototypes to be used within the NT system.
  8. Author:
  9. Mark Lucovsky 16-Feb-1989
  10. Revision History:
  11. --*/
  12. #ifndef _PS_
  13. #define _PS_
  14. //
  15. // Process Object
  16. //
  17. //
  18. // Process object body. A pointer to this structure is returned when a handle
  19. // to a process object is referenced. This structure contains a process control
  20. // block (PCB) which is the kernel's representation of a process.
  21. //
  22. #define MEMORY_PRIORITY_BACKGROUND 0
  23. #define MEMORY_PRIORITY_WASFOREGROUND 1
  24. #define MEMORY_PRIORITY_FOREGROUND 2
  25. typedef struct _MMSUPPORT_FLAGS {
  26. //
  27. // The next 8 bits are protected by the expansion lock.
  28. //
  29. UCHAR SessionSpace : 1;
  30. UCHAR BeingTrimmed : 1;
  31. UCHAR SessionLeader : 1;
  32. UCHAR TrimHard : 1;
  33. UCHAR MaximumWorkingSetHard : 1;
  34. UCHAR ForceTrim : 1;
  35. UCHAR MinimumWorkingSetHard : 1;
  36. UCHAR Available0 : 1;
  37. UCHAR MemoryPriority : 8;
  38. //
  39. // The next 16 bits are protected by the working set mutex.
  40. //
  41. USHORT GrowWsleHash : 1;
  42. USHORT AcquiredUnsafe : 1;
  43. USHORT Available : 14;
  44. } MMSUPPORT_FLAGS;
  45. typedef ULONG WSLE_NUMBER, *PWSLE_NUMBER;
  46. typedef struct _MMSUPPORT {
  47. LIST_ENTRY WorkingSetExpansionLinks;
  48. LARGE_INTEGER LastTrimTime;
  49. MMSUPPORT_FLAGS Flags;
  50. ULONG PageFaultCount;
  51. WSLE_NUMBER PeakWorkingSetSize;
  52. WSLE_NUMBER GrowthSinceLastEstimate;
  53. WSLE_NUMBER MinimumWorkingSetSize;
  54. WSLE_NUMBER MaximumWorkingSetSize;
  55. struct _MMWSL *VmWorkingSetList;
  56. WSLE_NUMBER Claim;
  57. WSLE_NUMBER NextEstimationSlot;
  58. WSLE_NUMBER NextAgingSlot;
  59. WSLE_NUMBER EstimatedAvailable;
  60. WSLE_NUMBER WorkingSetSize;
  61. KGUARDED_MUTEX WorkingSetMutex;
  62. } MMSUPPORT, *PMMSUPPORT;
  63. typedef struct _MMADDRESS_NODE {
  64. union {
  65. LONG_PTR Balance : 2;
  66. struct _MMADDRESS_NODE *Parent;
  67. } u1;
  68. struct _MMADDRESS_NODE *LeftChild;
  69. struct _MMADDRESS_NODE *RightChild;
  70. ULONG_PTR StartingVpn;
  71. ULONG_PTR EndingVpn;
  72. } MMADDRESS_NODE, *PMMADDRESS_NODE;
  73. //
  74. // A pair of macros to deal with the packing of parent & balance in the
  75. // MMADDRESS_NODE.
  76. //
  77. #define SANITIZE_PARENT_NODE(Parent) ((PMMADDRESS_NODE)(((ULONG_PTR)(Parent)) & ~0x3))
  78. //
  79. // Macro to carefully preserve the balance while updating the parent.
  80. //
  81. #define MI_MAKE_PARENT(ParentNode,ExistingBalance) \
  82. (PMMADDRESS_NODE)((ULONG_PTR)(ParentNode) | ((ExistingBalance) & 0x3))
  83. typedef struct _MM_AVL_TABLE {
  84. MMADDRESS_NODE BalancedRoot;
  85. ULONG_PTR DepthOfTree: 5;
  86. ULONG_PTR Unused: 3;
  87. #if defined (_WIN64)
  88. ULONG_PTR NumberGenericTableElements: 56;
  89. #else
  90. ULONG_PTR NumberGenericTableElements: 24;
  91. #endif
  92. PVOID NodeHint;
  93. PVOID NodeFreeHint;
  94. } MM_AVL_TABLE, *PMM_AVL_TABLE;
  95. //
  96. // Client impersonation information.
  97. //
  98. typedef struct _PS_IMPERSONATION_INFORMATION {
  99. PACCESS_TOKEN Token;
  100. BOOLEAN CopyOnOpen;
  101. BOOLEAN EffectiveOnly;
  102. SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
  103. } PS_IMPERSONATION_INFORMATION, *PPS_IMPERSONATION_INFORMATION;
  104. //
  105. // Audit Information structure: this is a member of the EPROCESS structure
  106. // and currently contains only the name of the exec'ed image file.
  107. //
  108. typedef struct _SE_AUDIT_PROCESS_CREATION_INFO {
  109. POBJECT_NAME_INFORMATION ImageFileName;
  110. } SE_AUDIT_PROCESS_CREATION_INFO, *PSE_AUDIT_PROCESS_CREATION_INFO;
  111. typedef enum _PS_QUOTA_TYPE {
  112. PsNonPagedPool = 0,
  113. PsPagedPool = 1,
  114. PsPageFile = 2,
  115. PsQuotaTypes = 3
  116. } PS_QUOTA_TYPE, *PPS_QUOTA_TYPE;
  117. typedef struct _EPROCESS_QUOTA_ENTRY {
  118. SIZE_T Usage; // Current usage count
  119. SIZE_T Limit; // Unhidered progress may be made to this point
  120. SIZE_T Peak; // Peak quota usage
  121. SIZE_T Return; // Quota value to return to the pool once its big enough
  122. } EPROCESS_QUOTA_ENTRY, *PEPROCESS_QUOTA_ENTRY;
  123. //#define PS_TRACK_QUOTA 1
  124. #define EPROCESS_QUOTA_TRACK_MAX 10000
  125. typedef struct _EPROCESS_QUOTA_TRACK {
  126. SIZE_T Charge;
  127. PVOID Caller;
  128. PVOID FreeCaller;
  129. PVOID Process;
  130. } EPROCESS_QUOTA_TRACK, *PEPROCESS_QUOTA_TRACK;
  131. typedef struct _EPROCESS_QUOTA_BLOCK {
  132. EPROCESS_QUOTA_ENTRY QuotaEntry[PsQuotaTypes];
  133. LIST_ENTRY QuotaList; // All additional quota blocks are chained through here
  134. ULONG ReferenceCount;
  135. ULONG ProcessCount; // Total number of processes still referencing this block
  136. #if defined (PS_TRACK_QUOTA)
  137. EPROCESS_QUOTA_TRACK Tracker[2][EPROCESS_QUOTA_TRACK_MAX];
  138. #endif
  139. } EPROCESS_QUOTA_BLOCK, *PEPROCESS_QUOTA_BLOCK;
  140. //
  141. // Pagefault monitoring.
  142. //
  143. typedef struct _PAGEFAULT_HISTORY {
  144. ULONG CurrentIndex;
  145. ULONG MaxIndex;
  146. KSPIN_LOCK SpinLock;
  147. PVOID Reserved;
  148. PROCESS_WS_WATCH_INFORMATION WatchInfo[1];
  149. } PAGEFAULT_HISTORY, *PPAGEFAULT_HISTORY;
  150. #define PS_WS_TRIM_FROM_EXE_HEADER 1
  151. #define PS_WS_TRIM_BACKGROUND_ONLY_APP 2
  152. //
  153. // Wow64 process stucture.
  154. //
  155. typedef struct _WOW64_PROCESS {
  156. PVOID Wow64;
  157. #if defined(_IA64_)
  158. KGUARDED_MUTEX AlternateTableLock;
  159. PULONG AltPermBitmap;
  160. #endif
  161. } WOW64_PROCESS, *PWOW64_PROCESS;
  162. #if defined (_WIN64)
  163. #define PS_GET_WOW64_PROCESS(Process) ((Process)->Wow64Process)
  164. #else
  165. #define PS_GET_WOW64_PROCESS(Process) ((Process), ((PWOW64_PROCESS)NULL))
  166. #endif
  167. #define PS_SET_BITS(Flags, Flag) \
  168. RtlInterlockedSetBitsDiscardReturn (Flags, Flag)
  169. #define PS_TEST_SET_BITS(Flags, Flag) \
  170. RtlInterlockedSetBits (Flags, Flag)
  171. #define PS_CLEAR_BITS(Flags, Flag) \
  172. RtlInterlockedClearBitsDiscardReturn (Flags, Flag)
  173. #define PS_TEST_CLEAR_BITS(Flags, Flag) \
  174. RtlInterlockedClearBits (Flags, Flag)
  175. #define PS_SET_CLEAR_BITS(Flags, sFlag, cFlag) \
  176. RtlInterlockedSetClearBits (Flags, sFlag, cFlag)
  177. #define PS_TEST_ALL_BITS_SET(Flags, Bits) \
  178. ((Flags&(Bits)) == (Bits))
  179. // Process structure.
  180. //
  181. // If you remove a field from this structure, please also
  182. // remove the reference to it from within the kernel debugger
  183. // (nt\private\sdktools\ntsd\ntkext.c)
  184. //
  185. typedef struct _EPROCESS {
  186. KPROCESS Pcb;
  187. //
  188. // Lock used to protect:
  189. // The list of threads in the process.
  190. // Process token.
  191. // Win32 process field.
  192. // Process and thread affinity setting.
  193. //
  194. EX_PUSH_LOCK ProcessLock;
  195. LARGE_INTEGER CreateTime;
  196. LARGE_INTEGER ExitTime;
  197. //
  198. // Structure to allow lock free cross process access to the process
  199. // handle table, process section and address space. Acquire rundown
  200. // protection with this if you do cross process handle table, process
  201. // section or address space references.
  202. //
  203. EX_RUNDOWN_REF RundownProtect;
  204. HANDLE UniqueProcessId;
  205. //
  206. // Global list of all processes in the system. Processes are removed
  207. // from this list in the object deletion routine. References to
  208. // processes in this list must be done with ObReferenceObjectSafe
  209. // because of this.
  210. //
  211. LIST_ENTRY ActiveProcessLinks;
  212. //
  213. // Quota Fields.
  214. //
  215. SIZE_T QuotaUsage[PsQuotaTypes];
  216. SIZE_T QuotaPeak[PsQuotaTypes];
  217. SIZE_T CommitCharge;
  218. //
  219. // VmCounters.
  220. //
  221. SIZE_T PeakVirtualSize;
  222. SIZE_T VirtualSize;
  223. LIST_ENTRY SessionProcessLinks;
  224. PVOID DebugPort;
  225. PVOID ExceptionPort;
  226. PHANDLE_TABLE ObjectTable;
  227. //
  228. // Security.
  229. //
  230. EX_FAST_REF Token;
  231. PFN_NUMBER WorkingSetPage;
  232. KGUARDED_MUTEX AddressCreationLock;
  233. KSPIN_LOCK HyperSpaceLock;
  234. struct _ETHREAD *ForkInProgress;
  235. ULONG_PTR HardwareTrigger;
  236. PMM_AVL_TABLE PhysicalVadRoot;
  237. PVOID CloneRoot;
  238. PFN_NUMBER NumberOfPrivatePages;
  239. PFN_NUMBER NumberOfLockedPages;
  240. PVOID Win32Process;
  241. struct _EJOB *Job;
  242. PVOID SectionObject;
  243. PVOID SectionBaseAddress;
  244. PEPROCESS_QUOTA_BLOCK QuotaBlock;
  245. PPAGEFAULT_HISTORY WorkingSetWatch;
  246. HANDLE Win32WindowStation;
  247. HANDLE InheritedFromUniqueProcessId;
  248. PVOID LdtInformation;
  249. PVOID VadFreeHint;
  250. PVOID VdmObjects;
  251. PVOID DeviceMap;
  252. PVOID Spare0[3];
  253. union {
  254. HARDWARE_PTE PageDirectoryPte;
  255. ULONGLONG Filler;
  256. };
  257. PVOID Session;
  258. UCHAR ImageFileName[ 16 ];
  259. LIST_ENTRY JobLinks;
  260. PVOID LockedPagesList;
  261. LIST_ENTRY ThreadListHead;
  262. //
  263. // Used by rdr/security for authentication.
  264. //
  265. PVOID SecurityPort;
  266. #ifdef _WIN64
  267. PWOW64_PROCESS Wow64Process;
  268. #else
  269. PVOID PaeTop;
  270. #endif
  271. ULONG ActiveThreads;
  272. ACCESS_MASK GrantedAccess;
  273. ULONG DefaultHardErrorProcessing;
  274. NTSTATUS LastThreadExitStatus;
  275. //
  276. // Peb
  277. //
  278. PPEB Peb;
  279. //
  280. // Pointer to the prefetches trace block.
  281. //
  282. EX_FAST_REF PrefetchTrace;
  283. LARGE_INTEGER ReadOperationCount;
  284. LARGE_INTEGER WriteOperationCount;
  285. LARGE_INTEGER OtherOperationCount;
  286. LARGE_INTEGER ReadTransferCount;
  287. LARGE_INTEGER WriteTransferCount;
  288. LARGE_INTEGER OtherTransferCount;
  289. SIZE_T CommitChargeLimit;
  290. SIZE_T CommitChargePeak;
  291. PVOID AweInfo;
  292. //
  293. // This is used for SeAuditProcessCreation.
  294. // It contains the full path to the image file.
  295. //
  296. SE_AUDIT_PROCESS_CREATION_INFO SeAuditProcessCreationInfo;
  297. MMSUPPORT Vm;
  298. #if !defined(_WIN64)
  299. LIST_ENTRY MmProcessLinks;
  300. #else
  301. ULONG Spares[2];
  302. #endif
  303. ULONG ModifiedPageCount;
  304. #define PS_JOB_STATUS_NOT_REALLY_ACTIVE 0x00000001UL
  305. #define PS_JOB_STATUS_ACCOUNTING_FOLDED 0x00000002UL
  306. #define PS_JOB_STATUS_NEW_PROCESS_REPORTED 0x00000004UL
  307. #define PS_JOB_STATUS_EXIT_PROCESS_REPORTED 0x00000008UL
  308. #define PS_JOB_STATUS_REPORT_COMMIT_CHANGES 0x00000010UL
  309. #define PS_JOB_STATUS_LAST_REPORT_MEMORY 0x00000020UL
  310. #define PS_JOB_STATUS_REPORT_PHYSICAL_PAGE_CHANGES 0x00000040UL
  311. ULONG JobStatus;
  312. //
  313. // Process flags. Use interlocked operations with PS_SET_BITS, etc
  314. // to modify these.
  315. //
  316. #define PS_PROCESS_FLAGS_CREATE_REPORTED 0x00000001UL // Create process debug call has occurred
  317. #define PS_PROCESS_FLAGS_NO_DEBUG_INHERIT 0x00000002UL // Don't inherit debug port
  318. #define PS_PROCESS_FLAGS_PROCESS_EXITING 0x00000004UL // PspExitProcess entered
  319. #define PS_PROCESS_FLAGS_PROCESS_DELETE 0x00000008UL // Delete process has been issued
  320. #define PS_PROCESS_FLAGS_WOW64_SPLIT_PAGES 0x00000010UL // Wow64 split pages
  321. #define PS_PROCESS_FLAGS_VM_DELETED 0x00000020UL // VM is deleted
  322. #define PS_PROCESS_FLAGS_OUTSWAP_ENABLED 0x00000040UL // Outswap enabled
  323. #define PS_PROCESS_FLAGS_OUTSWAPPED 0x00000080UL // Outswapped
  324. #define PS_PROCESS_FLAGS_FORK_FAILED 0x00000100UL // Fork status
  325. #define PS_PROCESS_FLAGS_WOW64_4GB_VA_SPACE 0x00000200UL // Wow64 process with 4gb virtual address space
  326. #define PS_PROCESS_FLAGS_ADDRESS_SPACE1 0x00000400UL // Addr space state1
  327. #define PS_PROCESS_FLAGS_ADDRESS_SPACE2 0x00000800UL // Addr space state2
  328. #define PS_PROCESS_FLAGS_SET_TIMER_RESOLUTION 0x00001000UL // SetTimerResolution has been called
  329. #define PS_PROCESS_FLAGS_BREAK_ON_TERMINATION 0x00002000UL // Break on process termination
  330. #define PS_PROCESS_FLAGS_CREATING_SESSION 0x00004000UL // Process is creating a session
  331. #define PS_PROCESS_FLAGS_USING_WRITE_WATCH 0x00008000UL // Process is using the write watch APIs
  332. #define PS_PROCESS_FLAGS_IN_SESSION 0x00010000UL // Process is in a session
  333. #define PS_PROCESS_FLAGS_OVERRIDE_ADDRESS_SPACE 0x00020000UL // Process must use native address space (Win64 only)
  334. #define PS_PROCESS_FLAGS_HAS_ADDRESS_SPACE 0x00040000UL // This process has an address space
  335. #define PS_PROCESS_FLAGS_LAUNCH_PREFETCHED 0x00080000UL // Process launch was prefetched
  336. #define PS_PROCESS_INJECT_INPAGE_ERRORS 0x00100000UL // Process should be given inpage errors - hardcoded in trap.asm too
  337. #define PS_PROCESS_FLAGS_VM_TOP_DOWN 0x00200000UL // Process memory allocations default to top-down
  338. #define PS_PROCESS_FLAGS_IMAGE_NOTIFY_DONE 0x00400000UL // We have sent a message for this image
  339. #define PS_PROCESS_FLAGS_PDE_UPDATE_NEEDED 0x00800000UL // The system PDEs need updating for this process (NT32 only)
  340. #define PS_PROCESS_FLAGS_VDM_ALLOWED 0x01000000UL // Process allowed to invoke NTVDM support
  341. union {
  342. ULONG Flags;
  343. //
  344. // Fields can only be set by the PS_SET_BITS and other interlocked
  345. // macros. Reading fields is best done via the bit definitions so
  346. // references are easy to locate.
  347. //
  348. struct {
  349. ULONG CreateReported : 1;
  350. ULONG NoDebugInherit : 1;
  351. ULONG ProcessExiting : 1;
  352. ULONG ProcessDelete : 1;
  353. ULONG Wow64SplitPages : 1;
  354. ULONG VmDeleted : 1;
  355. ULONG OutswapEnabled : 1;
  356. ULONG Outswapped : 1;
  357. ULONG ForkFailed : 1;
  358. ULONG Wow64VaSpace4Gb : 1;
  359. ULONG AddressSpaceInitialized : 2;
  360. ULONG SetTimerResolution : 1;
  361. ULONG BreakOnTermination : 1;
  362. ULONG SessionCreationUnderway : 1;
  363. ULONG WriteWatch : 1;
  364. ULONG ProcessInSession : 1;
  365. ULONG OverrideAddressSpace : 1;
  366. ULONG HasAddressSpace : 1;
  367. ULONG LaunchPrefetched : 1;
  368. ULONG InjectInpageErrors : 1;
  369. ULONG VmTopDown : 1;
  370. ULONG ImageNotifyDone : 1;
  371. ULONG PdeUpdateNeeded : 1; // NT32 only
  372. ULONG VdmAllowed : 1;
  373. ULONG Unused : 7;
  374. };
  375. };
  376. NTSTATUS ExitStatus;
  377. USHORT NextPageColor;
  378. union {
  379. struct {
  380. UCHAR SubSystemMinorVersion;
  381. UCHAR SubSystemMajorVersion;
  382. };
  383. USHORT SubSystemVersion;
  384. };
  385. UCHAR PriorityClass;
  386. MM_AVL_TABLE VadRoot;
  387. } EPROCESS;
  388. typedef EPROCESS *PEPROCESS;
  389. //
  390. // Thread termination port
  391. //
  392. typedef struct _TERMINATION_PORT {
  393. struct _TERMINATION_PORT *Next;
  394. PVOID Port;
  395. } TERMINATION_PORT, *PTERMINATION_PORT;
  396. // Thread Object
  397. //
  398. // Thread object body. A pointer to this structure is returned when a handle
  399. // to a thread object is referenced. This structure contains a thread control
  400. // block (TCB) which is the kernel's representation of a thread.
  401. //
  402. //
  403. // The upper 4 bits of the CreateTime should be zero on initialization so
  404. // that the shift doesn't destroy anything.
  405. //
  406. #define PS_GET_THREAD_CREATE_TIME(Thread) ((Thread)->CreateTime.QuadPart >> 3)
  407. #define PS_SET_THREAD_CREATE_TIME(Thread, InputCreateTime) \
  408. ((Thread)->CreateTime.QuadPart = (InputCreateTime.QuadPart << 3))
  409. //
  410. // Macro to return TRUE if the specified thread is impersonating.
  411. //
  412. #define PS_IS_THREAD_IMPERSONATING(Thread) (((Thread)->CrossThreadFlags&PS_CROSS_THREAD_FLAGS_IMPERSONATING) != 0)
  413. typedef struct _ETHREAD {
  414. KTHREAD Tcb;
  415. union {
  416. //
  417. // The fact that this is a union means that all accesses to CreateTime
  418. // must be sanitized using the two macros above.
  419. //
  420. LARGE_INTEGER CreateTime;
  421. //
  422. // These fields are accessed only by the owning thread, but can be
  423. // accessed from within a special kernel APC so IRQL protection must
  424. // be applied.
  425. //
  426. struct {
  427. unsigned NestedFaultCount : 2;
  428. unsigned ApcNeeded : 1;
  429. };
  430. };
  431. union {
  432. LARGE_INTEGER ExitTime;
  433. LIST_ENTRY LpcReplyChain;
  434. LIST_ENTRY KeyedWaitChain;
  435. };
  436. union {
  437. NTSTATUS ExitStatus;
  438. PVOID OfsChain;
  439. };
  440. //
  441. // Registry
  442. //
  443. LIST_ENTRY PostBlockList;
  444. //
  445. // Single linked list of termination blocks
  446. //
  447. union {
  448. //
  449. // List of termination ports
  450. //
  451. PTERMINATION_PORT TerminationPort;
  452. //
  453. // List of threads to be reaped. Only used at thread exit
  454. //
  455. struct _ETHREAD *ReaperLink;
  456. //
  457. // Keyvalue being waited for
  458. //
  459. PVOID KeyedWaitValue;
  460. };
  461. KSPIN_LOCK ActiveTimerListLock;
  462. LIST_ENTRY ActiveTimerListHead;
  463. CLIENT_ID Cid;
  464. //
  465. // Lpc
  466. //
  467. union {
  468. KSEMAPHORE LpcReplySemaphore;
  469. KSEMAPHORE KeyedWaitSemaphore;
  470. };
  471. union {
  472. PVOID LpcReplyMessage; // -> Message that contains the reply
  473. PVOID LpcWaitingOnPort;
  474. };
  475. //
  476. // Security
  477. //
  478. //
  479. // Client - If non null, indicates the thread is impersonating
  480. // a client.
  481. //
  482. PPS_IMPERSONATION_INFORMATION ImpersonationInfo;
  483. //
  484. // Io
  485. //
  486. LIST_ENTRY IrpList;
  487. //
  488. // File Systems
  489. //
  490. ULONG_PTR TopLevelIrp; // either NULL, an Irp or a flag defined in FsRtl.h
  491. struct _DEVICE_OBJECT *DeviceToVerify;
  492. PEPROCESS ThreadsProcess;
  493. PVOID StartAddress;
  494. union {
  495. PVOID Win32StartAddress;
  496. ULONG LpcReceivedMessageId;
  497. };
  498. //
  499. // Ps
  500. //
  501. LIST_ENTRY ThreadListEntry;
  502. //
  503. // Rundown protection structure. Acquire this to do cross thread
  504. // TEB, TEB32 or stack references.
  505. //
  506. EX_RUNDOWN_REF RundownProtect;
  507. //
  508. // Lock to protect thread impersonation information
  509. //
  510. EX_PUSH_LOCK ThreadLock;
  511. ULONG LpcReplyMessageId; // MessageId this thread is waiting for reply to
  512. ULONG ReadClusterSize;
  513. //
  514. // Client/server
  515. //
  516. ACCESS_MASK GrantedAccess;
  517. //
  518. // Flags for cross thread access. Use interlocked operations
  519. // via PS_SET_BITS etc.
  520. //
  521. //
  522. // Used to signify that the delete APC has been queued or the
  523. // thread has called PspExitThread itself.
  524. //
  525. #define PS_CROSS_THREAD_FLAGS_TERMINATED 0x00000001UL
  526. //
  527. // Thread create failed
  528. //
  529. #define PS_CROSS_THREAD_FLAGS_DEADTHREAD 0x00000002UL
  530. //
  531. // Debugger isn't shown this thread
  532. //
  533. #define PS_CROSS_THREAD_FLAGS_HIDEFROMDBG 0x00000004UL
  534. //
  535. // Thread is impersonating
  536. //
  537. #define PS_CROSS_THREAD_FLAGS_IMPERSONATING 0x00000008UL
  538. //
  539. // This is a system thread
  540. //
  541. #define PS_CROSS_THREAD_FLAGS_SYSTEM 0x00000010UL
  542. //
  543. // Hard errors are disabled for this thread
  544. //
  545. #define PS_CROSS_THREAD_FLAGS_HARD_ERRORS_DISABLED 0x00000020UL
  546. //
  547. // We should break in when this thread is terminated
  548. //
  549. #define PS_CROSS_THREAD_FLAGS_BREAK_ON_TERMINATION 0x00000040UL
  550. //
  551. // This thread should skip sending its create thread message
  552. //
  553. #define PS_CROSS_THREAD_FLAGS_SKIP_CREATION_MSG 0x00000080UL
  554. //
  555. // This thread should skip sending its final thread termination message
  556. //
  557. #define PS_CROSS_THREAD_FLAGS_SKIP_TERMINATION_MSG 0x00000100UL
  558. union {
  559. ULONG CrossThreadFlags;
  560. //
  561. // The following fields are for the debugger only. Do not use.
  562. // Use the bit definitions instead.
  563. //
  564. struct {
  565. ULONG Terminated : 1;
  566. ULONG DeadThread : 1;
  567. ULONG HideFromDebugger : 1;
  568. ULONG ActiveImpersonationInfo : 1;
  569. ULONG SystemThread : 1;
  570. ULONG HardErrorsAreDisabled : 1;
  571. ULONG BreakOnTermination : 1;
  572. ULONG SkipCreationMsg : 1;
  573. ULONG SkipTerminationMsg : 1;
  574. };
  575. };
  576. //
  577. // Flags to be accessed in this thread's context only at PASSIVE
  578. // level -- no need to use interlocked operations.
  579. //
  580. union {
  581. ULONG SameThreadPassiveFlags;
  582. struct {
  583. //
  584. // This thread is an active Ex worker thread; it should
  585. // not terminate.
  586. //
  587. ULONG ActiveExWorker : 1;
  588. ULONG ExWorkerCanWaitUser : 1;
  589. ULONG MemoryMaker : 1;
  590. //
  591. // Thread is active inthe keyed event code. LPC should not run above this in an APC.
  592. //
  593. ULONG KeyedEventInUse : 1;
  594. };
  595. };
  596. //
  597. // Flags to be accessed in this thread's context only at APC_LEVEL.
  598. // No need to use interlocked operations.
  599. //
  600. union {
  601. ULONG SameThreadApcFlags;
  602. struct {
  603. //
  604. // The stored thread's MSGID is valid. This is only accessed
  605. // while the LPC mutex is held so it's an APC_LEVEL flag.
  606. //
  607. BOOLEAN LpcReceivedMsgIdValid : 1;
  608. BOOLEAN LpcExitThreadCalled : 1;
  609. BOOLEAN AddressSpaceOwner : 1;
  610. };
  611. };
  612. BOOLEAN ForwardClusterOnly;
  613. BOOLEAN DisablePageFaultClustering;
  614. #if defined (PERF_DATA)
  615. ULONG PerformanceCountLow;
  616. LONG PerformanceCountHigh;
  617. #endif
  618. } ETHREAD;
  619. typedef ETHREAD *PETHREAD;
  620. //
  621. // The following two inline functions allow a thread or process object to
  622. // be converted into a kernel thread or process, respectively, without
  623. // having to expose the ETHREAD and EPROCESS definitions to the world.
  624. //
  625. // These functions take advantage of the fact that the kernel structures
  626. // appear as the first element in the respective object structures.
  627. //
  628. // The C_ASSERTs that follow ensure that this is the case.
  629. //
  630. // begin_ntosp
  631. PKTHREAD
  632. FORCEINLINE
  633. PsGetKernelThread(
  634. IN PETHREAD ThreadObject
  635. )
  636. {
  637. return (PKTHREAD)ThreadObject;
  638. }
  639. PKPROCESS
  640. FORCEINLINE
  641. PsGetKernelProcess(
  642. IN PEPROCESS ProcessObject
  643. )
  644. {
  645. return (PKPROCESS)ProcessObject;
  646. }
  647. NTSTATUS
  648. PsGetContextThread(
  649. IN PETHREAD Thread,
  650. IN OUT PCONTEXT ThreadContext,
  651. IN KPROCESSOR_MODE Mode
  652. );
  653. NTSTATUS
  654. PsSetContextThread(
  655. IN PETHREAD Thread,
  656. IN PCONTEXT ThreadContext,
  657. IN KPROCESSOR_MODE Mode
  658. );
  659. // end_ntosp
  660. C_ASSERT( FIELD_OFFSET(ETHREAD,Tcb) == 0 );
  661. C_ASSERT( FIELD_OFFSET(EPROCESS,Pcb) == 0 );
  662. //
  663. // Initial PEB
  664. //
  665. typedef struct _INITIAL_PEB {
  666. BOOLEAN InheritedAddressSpace; // These four fields cannot change unless the
  667. BOOLEAN ReadImageFileExecOptions; //
  668. BOOLEAN BeingDebugged; //
  669. BOOLEAN SpareBool; //
  670. HANDLE Mutant; // PEB structure is also updated.
  671. } INITIAL_PEB, *PINITIAL_PEB;
  672. typedef struct _PS_JOB_TOKEN_FILTER {
  673. ULONG CapturedSidCount ;
  674. PSID_AND_ATTRIBUTES CapturedSids ;
  675. ULONG CapturedSidsLength ;
  676. ULONG CapturedGroupCount ;
  677. PSID_AND_ATTRIBUTES CapturedGroups ;
  678. ULONG CapturedGroupsLength ;
  679. ULONG CapturedPrivilegeCount ;
  680. PLUID_AND_ATTRIBUTES CapturedPrivileges ;
  681. ULONG CapturedPrivilegesLength ;
  682. } PS_JOB_TOKEN_FILTER, * PPS_JOB_TOKEN_FILTER ;
  683. //
  684. // Job Object
  685. //
  686. typedef struct _EJOB {
  687. KEVENT Event;
  688. //
  689. // All jobs are chained together via this list.
  690. // Protected by the global lock PspJobListLock
  691. //
  692. LIST_ENTRY JobLinks;
  693. //
  694. // All processes within this job. Processes are removed from this
  695. // list at last dereference. Safe object referencing needs to be done.
  696. // Protected by the joblock.
  697. //
  698. LIST_ENTRY ProcessListHead;
  699. ERESOURCE JobLock;
  700. //
  701. // Accounting Info
  702. //
  703. LARGE_INTEGER TotalUserTime;
  704. LARGE_INTEGER TotalKernelTime;
  705. LARGE_INTEGER ThisPeriodTotalUserTime;
  706. LARGE_INTEGER ThisPeriodTotalKernelTime;
  707. ULONG TotalPageFaultCount;
  708. ULONG TotalProcesses;
  709. ULONG ActiveProcesses;
  710. ULONG TotalTerminatedProcesses;
  711. //
  712. // Limitable Attributes
  713. //
  714. LARGE_INTEGER PerProcessUserTimeLimit;
  715. LARGE_INTEGER PerJobUserTimeLimit;
  716. ULONG LimitFlags;
  717. SIZE_T MinimumWorkingSetSize;
  718. SIZE_T MaximumWorkingSetSize;
  719. ULONG ActiveProcessLimit;
  720. KAFFINITY Affinity;
  721. UCHAR PriorityClass;
  722. //
  723. // UI restrictions
  724. //
  725. ULONG UIRestrictionsClass;
  726. //
  727. // Security Limitations: write once, read always
  728. //
  729. ULONG SecurityLimitFlags;
  730. PACCESS_TOKEN Token;
  731. PPS_JOB_TOKEN_FILTER Filter;
  732. //
  733. // End Of Job Time Limit
  734. //
  735. ULONG EndOfJobTimeAction;
  736. PVOID CompletionPort;
  737. PVOID CompletionKey;
  738. ULONG SessionId;
  739. ULONG SchedulingClass;
  740. ULONGLONG ReadOperationCount;
  741. ULONGLONG WriteOperationCount;
  742. ULONGLONG OtherOperationCount;
  743. ULONGLONG ReadTransferCount;
  744. ULONGLONG WriteTransferCount;
  745. ULONGLONG OtherTransferCount;
  746. //
  747. // Extended Limits
  748. //
  749. IO_COUNTERS IoInfo; // not used yet
  750. SIZE_T ProcessMemoryLimit;
  751. SIZE_T JobMemoryLimit;
  752. SIZE_T PeakProcessMemoryUsed;
  753. SIZE_T PeakJobMemoryUsed;
  754. SIZE_T CurrentJobMemoryUsed;
  755. KGUARDED_MUTEX MemoryLimitsLock;
  756. //
  757. // List of jobs in a job set. Processes within a job in a job set
  758. // can create processes in the same or higher members of the jobset.
  759. // Protected by the global lock PspJobListLock
  760. //
  761. LIST_ENTRY JobSetLinks;
  762. //
  763. // Member level for this job in the jobset.
  764. //
  765. ULONG MemberLevel;
  766. //
  767. // This job has had its last handle closed.
  768. //
  769. #define PS_JOB_FLAGS_CLOSE_DONE 0x1UL
  770. ULONG JobFlags;
  771. } EJOB;
  772. typedef EJOB *PEJOB;
  773. //
  774. // Global Variables
  775. //
  776. extern ULONG PsPrioritySeperation;
  777. extern ULONG PsRawPrioritySeparation;
  778. extern LIST_ENTRY PsActiveProcessHead;
  779. extern const UNICODE_STRING PsNtDllPathName;
  780. extern PVOID PsSystemDllBase;
  781. extern PEPROCESS PsInitialSystemProcess;
  782. extern PVOID PsNtosImageBase;
  783. extern PVOID PsHalImageBase;
  784. #if defined(_AMD64_) || defined(_IA64_)
  785. extern INVERTED_FUNCTION_TABLE PsInvertedFunctionTable;
  786. #endif
  787. extern LIST_ENTRY PsLoadedModuleList;
  788. extern ERESOURCE PsLoadedModuleResource;
  789. extern KSPIN_LOCK PsLoadedModuleSpinLock;
  790. extern LCID PsDefaultSystemLocaleId;
  791. extern LCID PsDefaultThreadLocaleId;
  792. extern LANGID PsDefaultUILanguageId;
  793. extern LANGID PsInstallUILanguageId;
  794. extern PEPROCESS PsIdleProcess;
  795. extern SINGLE_LIST_ENTRY PsReaperListHead;
  796. extern WORK_QUEUE_ITEM PsReaperWorkItem;
  797. #define PS_EMBEDDED_NO_USERMODE 1 // no user mode code will run on the system
  798. extern ULONG PsEmbeddedNTMask;
  799. BOOLEAN
  800. PsChangeJobMemoryUsage(
  801. IN ULONG Flags,
  802. IN SSIZE_T Amount
  803. );
  804. VOID
  805. PsReportProcessMemoryLimitViolation(
  806. VOID
  807. );
  808. #define THREAD_HIT_SLOTS 750
  809. extern ULONG PsThreadHits[THREAD_HIT_SLOTS];
  810. VOID
  811. PsThreadHit(
  812. IN PETHREAD Thread
  813. );
  814. VOID
  815. PsEnforceExecutionTimeLimits(
  816. VOID
  817. );
  818. BOOLEAN
  819. PsInitSystem (
  820. IN ULONG Phase,
  821. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  822. );
  823. NTSTATUS
  824. PsMapSystemDll (
  825. IN PEPROCESS Process,
  826. OUT PVOID *DllBase OPTIONAL
  827. );
  828. VOID
  829. PsInitializeQuotaSystem (
  830. VOID
  831. );
  832. LOGICAL
  833. PsShutdownSystem (
  834. VOID
  835. );
  836. BOOLEAN
  837. PsWaitForAllProcesses (
  838. VOID);
  839. NTSTATUS
  840. PsLocateSystemDll (
  841. BOOLEAN ReplaceExisting
  842. );
  843. VOID
  844. PsChangeQuantumTable(
  845. BOOLEAN ModifyActiveProcesses,
  846. ULONG PrioritySeparation
  847. );
  848. //
  849. // Get Gurrent Prototypes
  850. //
  851. #define THREAD_TO_PROCESS(Thread) ((Thread)->ThreadsProcess)
  852. #define IS_SYSTEM_THREAD(Thread) (((Thread)->CrossThreadFlags&PS_CROSS_THREAD_FLAGS_SYSTEM) != 0)
  853. #define _PsGetCurrentProcess() (CONTAINING_RECORD(((KeGetCurrentThread())->ApcState.Process),EPROCESS,Pcb))
  854. #define PsGetCurrentProcessByThread(xCurrentThread) (ASSERT((xCurrentThread) == PsGetCurrentThread ()),CONTAINING_RECORD(((xCurrentThread)->Tcb.ApcState.Process),EPROCESS,Pcb))
  855. #define _PsGetCurrentThread() (CONTAINING_RECORD((KeGetCurrentThread()),ETHREAD,Tcb))
  856. #if defined(_NTOSP_)
  857. // begin_ntosp
  858. NTKERNELAPI
  859. PEPROCESS
  860. PsGetCurrentProcess(
  861. VOID
  862. );
  863. NTKERNELAPI
  864. PETHREAD
  865. PsGetCurrentThread(
  866. VOID
  867. );
  868. // end_ntosp
  869. #else
  870. #define PsGetCurrentProcess() _PsGetCurrentProcess()
  871. #define PsGetCurrentThread() _PsGetCurrentThread()
  872. #endif
  873. //
  874. // Exit kernel mode APC routine.
  875. //
  876. VOID
  877. PsExitSpecialApc(
  878. IN PKAPC Apc,
  879. IN PKNORMAL_ROUTINE *NormalRoutine,
  880. IN PVOID *NormalContext,
  881. IN PVOID *SystemArgument1,
  882. IN PVOID *SystemArgument2
  883. );
  884. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  885. //
  886. // System Thread and Process Creation and Termination
  887. //
  888. NTKERNELAPI
  889. NTSTATUS
  890. PsCreateSystemThread(
  891. OUT PHANDLE ThreadHandle,
  892. IN ULONG DesiredAccess,
  893. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  894. IN HANDLE ProcessHandle OPTIONAL,
  895. OUT PCLIENT_ID ClientId OPTIONAL,
  896. IN PKSTART_ROUTINE StartRoutine,
  897. IN PVOID StartContext
  898. );
  899. NTKERNELAPI
  900. NTSTATUS
  901. PsTerminateSystemThread(
  902. IN NTSTATUS ExitStatus
  903. );
  904. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  905. NTSTATUS
  906. PsCreateSystemProcess(
  907. OUT PHANDLE ProcessHandle,
  908. IN ULONG DesiredAccess,
  909. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
  910. );
  911. typedef
  912. VOID (*PLEGO_NOTIFY_ROUTINE)(
  913. PKTHREAD Thread
  914. );
  915. ULONG
  916. PsSetLegoNotifyRoutine(
  917. PLEGO_NOTIFY_ROUTINE LegoNotifyRoutine
  918. );
  919. // begin_ntifs begin_ntddk
  920. typedef
  921. VOID
  922. (*PCREATE_PROCESS_NOTIFY_ROUTINE)(
  923. IN HANDLE ParentId,
  924. IN HANDLE ProcessId,
  925. IN BOOLEAN Create
  926. );
  927. NTSTATUS
  928. PsSetCreateProcessNotifyRoutine(
  929. IN PCREATE_PROCESS_NOTIFY_ROUTINE NotifyRoutine,
  930. IN BOOLEAN Remove
  931. );
  932. typedef
  933. VOID
  934. (*PCREATE_THREAD_NOTIFY_ROUTINE)(
  935. IN HANDLE ProcessId,
  936. IN HANDLE ThreadId,
  937. IN BOOLEAN Create
  938. );
  939. NTSTATUS
  940. PsSetCreateThreadNotifyRoutine(
  941. IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine
  942. );
  943. NTSTATUS
  944. PsRemoveCreateThreadNotifyRoutine (
  945. IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine
  946. );
  947. //
  948. // Structures for Load Image Notify
  949. //
  950. typedef struct _IMAGE_INFO {
  951. union {
  952. ULONG Properties;
  953. struct {
  954. ULONG ImageAddressingMode : 8; // code addressing mode
  955. ULONG SystemModeImage : 1; // system mode image
  956. ULONG ImageMappedToAllPids : 1; // image mapped into all processes
  957. ULONG Reserved : 22;
  958. };
  959. };
  960. PVOID ImageBase;
  961. ULONG ImageSelector;
  962. SIZE_T ImageSize;
  963. ULONG ImageSectionNumber;
  964. } IMAGE_INFO, *PIMAGE_INFO;
  965. #define IMAGE_ADDRESSING_MODE_32BIT 3
  966. typedef
  967. VOID
  968. (*PLOAD_IMAGE_NOTIFY_ROUTINE)(
  969. IN PUNICODE_STRING FullImageName,
  970. IN HANDLE ProcessId, // pid into which image is being mapped
  971. IN PIMAGE_INFO ImageInfo
  972. );
  973. NTSTATUS
  974. PsSetLoadImageNotifyRoutine(
  975. IN PLOAD_IMAGE_NOTIFY_ROUTINE NotifyRoutine
  976. );
  977. NTSTATUS
  978. PsRemoveLoadImageNotifyRoutine(
  979. IN PLOAD_IMAGE_NOTIFY_ROUTINE NotifyRoutine
  980. );
  981. // end_ntddk
  982. //
  983. // Security Support
  984. //
  985. NTSTATUS
  986. PsAssignImpersonationToken(
  987. IN PETHREAD Thread,
  988. IN HANDLE Token
  989. );
  990. // begin_ntosp
  991. NTKERNELAPI
  992. PACCESS_TOKEN
  993. PsReferencePrimaryToken(
  994. IN PEPROCESS Process
  995. );
  996. VOID
  997. PsDereferencePrimaryToken(
  998. IN PACCESS_TOKEN PrimaryToken
  999. );
  1000. VOID
  1001. PsDereferenceImpersonationToken(
  1002. IN PACCESS_TOKEN ImpersonationToken
  1003. );
  1004. // end_ntifs
  1005. // end_ntosp
  1006. #define PsDereferencePrimaryTokenEx(P,T) (ObFastDereferenceObject (&P->Token,(T)))
  1007. #define PsDereferencePrimaryToken(T) (ObDereferenceObject((T)))
  1008. #define PsDereferenceImpersonationToken(T) \
  1009. {if (ARGUMENT_PRESENT((T))) { \
  1010. (ObDereferenceObject((T))); \
  1011. } else { \
  1012. ; \
  1013. } \
  1014. }
  1015. #define PsProcessAuditId(Process) ((Process)->UniqueProcessId)
  1016. // begin_ntosp
  1017. // begin_ntifs
  1018. NTKERNELAPI
  1019. PACCESS_TOKEN
  1020. PsReferenceImpersonationToken(
  1021. IN PETHREAD Thread,
  1022. OUT PBOOLEAN CopyOnOpen,
  1023. OUT PBOOLEAN EffectiveOnly,
  1024. OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
  1025. );
  1026. // end_ntifs
  1027. PACCESS_TOKEN
  1028. PsReferenceEffectiveToken(
  1029. IN PETHREAD Thread,
  1030. OUT PTOKEN_TYPE TokenType,
  1031. OUT PBOOLEAN EffectiveOnly,
  1032. OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
  1033. );
  1034. // begin_ntifs
  1035. LARGE_INTEGER
  1036. PsGetProcessExitTime(
  1037. VOID
  1038. );
  1039. // end_ntifs
  1040. // end_ntosp
  1041. #if defined(_NTDDK_) || defined(_NTIFS_)
  1042. // begin_ntifs begin_ntosp
  1043. BOOLEAN
  1044. PsIsThreadTerminating(
  1045. IN PETHREAD Thread
  1046. );
  1047. // end_ntifs end_ntosp
  1048. #else
  1049. //
  1050. // BOOLEAN
  1051. // PsIsThreadTerminating(
  1052. // IN PETHREAD Thread
  1053. // )
  1054. //
  1055. // Returns TRUE if thread is in the process of terminating.
  1056. //
  1057. #define PsIsThreadTerminating(T) \
  1058. (((T)->CrossThreadFlags&PS_CROSS_THREAD_FLAGS_TERMINATED) != 0)
  1059. #endif
  1060. extern BOOLEAN PsImageNotifyEnabled;
  1061. VOID
  1062. PsCallImageNotifyRoutines(
  1063. IN PUNICODE_STRING FullImageName,
  1064. IN HANDLE ProcessId, // pid into which image is being mapped
  1065. IN PIMAGE_INFO ImageInfo
  1066. );
  1067. // begin_ntifs
  1068. // begin_ntosp
  1069. NTSTATUS
  1070. PsImpersonateClient(
  1071. IN PETHREAD Thread,
  1072. IN PACCESS_TOKEN Token,
  1073. IN BOOLEAN CopyOnOpen,
  1074. IN BOOLEAN EffectiveOnly,
  1075. IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
  1076. );
  1077. // end_ntosp
  1078. BOOLEAN
  1079. PsDisableImpersonation(
  1080. IN PETHREAD Thread,
  1081. IN PSE_IMPERSONATION_STATE ImpersonationState
  1082. );
  1083. VOID
  1084. PsRestoreImpersonation(
  1085. IN PETHREAD Thread,
  1086. IN PSE_IMPERSONATION_STATE ImpersonationState
  1087. );
  1088. // end_ntifs
  1089. // begin_ntosp begin_ntifs
  1090. NTKERNELAPI
  1091. VOID
  1092. PsRevertToSelf(
  1093. VOID
  1094. );
  1095. // end_ntifs
  1096. NTKERNELAPI
  1097. VOID
  1098. PsRevertThreadToSelf(
  1099. PETHREAD Thread
  1100. );
  1101. // end_ntosp
  1102. NTSTATUS
  1103. PsOpenTokenOfThread(
  1104. IN HANDLE ThreadHandle,
  1105. IN BOOLEAN OpenAsSelf,
  1106. OUT PACCESS_TOKEN *Token,
  1107. OUT PBOOLEAN CopyOnOpen,
  1108. OUT PBOOLEAN EffectiveOnly,
  1109. OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
  1110. );
  1111. NTSTATUS
  1112. PsOpenTokenOfProcess(
  1113. IN HANDLE ProcessHandle,
  1114. OUT PACCESS_TOKEN *Token
  1115. );
  1116. NTSTATUS
  1117. PsOpenTokenOfJob(
  1118. IN HANDLE JobHandle,
  1119. OUT PACCESS_TOKEN * Token
  1120. );
  1121. //
  1122. // Cid
  1123. //
  1124. NTSTATUS
  1125. PsLookupProcessThreadByCid(
  1126. IN PCLIENT_ID Cid,
  1127. OUT PEPROCESS *Process OPTIONAL,
  1128. OUT PETHREAD *Thread
  1129. );
  1130. // begin_ntosp
  1131. NTKERNELAPI
  1132. NTSTATUS
  1133. PsLookupProcessByProcessId(
  1134. IN HANDLE ProcessId,
  1135. OUT PEPROCESS *Process
  1136. );
  1137. NTKERNELAPI
  1138. NTSTATUS
  1139. PsLookupThreadByThreadId(
  1140. IN HANDLE ThreadId,
  1141. OUT PETHREAD *Thread
  1142. );
  1143. // begin_ntifs
  1144. //
  1145. // Quota Operations
  1146. //
  1147. VOID
  1148. PsChargePoolQuota(
  1149. IN PEPROCESS Process,
  1150. IN POOL_TYPE PoolType,
  1151. IN ULONG_PTR Amount
  1152. );
  1153. NTSTATUS
  1154. PsChargeProcessPoolQuota(
  1155. IN PEPROCESS Process,
  1156. IN POOL_TYPE PoolType,
  1157. IN ULONG_PTR Amount
  1158. );
  1159. VOID
  1160. PsReturnPoolQuota(
  1161. IN PEPROCESS Process,
  1162. IN POOL_TYPE PoolType,
  1163. IN ULONG_PTR Amount
  1164. );
  1165. // end_ntifs
  1166. // end_ntosp
  1167. NTSTATUS
  1168. PsChargeProcessQuota (
  1169. IN PEPROCESS Process,
  1170. IN PS_QUOTA_TYPE QuotaType,
  1171. IN SIZE_T Amount
  1172. );
  1173. VOID
  1174. PsReturnProcessQuota (
  1175. IN PEPROCESS Process,
  1176. IN PS_QUOTA_TYPE QuotaType,
  1177. IN SIZE_T Amount
  1178. );
  1179. NTSTATUS
  1180. PsChargeProcessNonPagedPoolQuota(
  1181. IN PEPROCESS Process,
  1182. IN SIZE_T Amount
  1183. );
  1184. VOID
  1185. PsReturnProcessNonPagedPoolQuota(
  1186. IN PEPROCESS Process,
  1187. IN SIZE_T Amount
  1188. );
  1189. NTSTATUS
  1190. PsChargeProcessPagedPoolQuota(
  1191. IN PEPROCESS Process,
  1192. IN SIZE_T Amount
  1193. );
  1194. VOID
  1195. PsReturnProcessPagedPoolQuota(
  1196. IN PEPROCESS Process,
  1197. IN SIZE_T Amount
  1198. );
  1199. NTSTATUS
  1200. PsChargeProcessPageFileQuota(
  1201. IN PEPROCESS Process,
  1202. IN SIZE_T Amount
  1203. );
  1204. VOID
  1205. PsReturnProcessPageFileQuota(
  1206. IN PEPROCESS Process,
  1207. IN SIZE_T Amount
  1208. );
  1209. //
  1210. // Context Management
  1211. //
  1212. VOID
  1213. PspContextToKframes(
  1214. OUT PKTRAP_FRAME TrapFrame,
  1215. OUT PKEXCEPTION_FRAME ExceptionFrame,
  1216. IN PCONTEXT Context
  1217. );
  1218. VOID
  1219. PspContextFromKframes(
  1220. OUT PKTRAP_FRAME TrapFrame,
  1221. OUT PKEXCEPTION_FRAME ExceptionFrame,
  1222. IN PCONTEXT Context
  1223. );
  1224. VOID
  1225. PsReturnSharedPoolQuota(
  1226. IN PEPROCESS_QUOTA_BLOCK QuotaBlock,
  1227. IN ULONG_PTR PagedAmount,
  1228. IN ULONG_PTR NonPagedAmount
  1229. );
  1230. PEPROCESS_QUOTA_BLOCK
  1231. PsChargeSharedPoolQuota(
  1232. IN PEPROCESS Process,
  1233. IN ULONG_PTR PagedAmount,
  1234. IN ULONG_PTR NonPagedAmount
  1235. );
  1236. //
  1237. // Exception Handling
  1238. //
  1239. BOOLEAN
  1240. PsForwardException (
  1241. IN PEXCEPTION_RECORD ExceptionRecord,
  1242. IN BOOLEAN DebugException,
  1243. IN BOOLEAN SecondChance
  1244. );
  1245. // begin_ntosp
  1246. typedef
  1247. NTSTATUS
  1248. (*PKWIN32_PROCESS_CALLOUT) (
  1249. IN PEPROCESS Process,
  1250. IN BOOLEAN Initialize
  1251. );
  1252. typedef enum _PSW32JOBCALLOUTTYPE {
  1253. PsW32JobCalloutSetInformation,
  1254. PsW32JobCalloutAddProcess,
  1255. PsW32JobCalloutTerminate
  1256. } PSW32JOBCALLOUTTYPE;
  1257. typedef struct _WIN32_JOBCALLOUT_PARAMETERS {
  1258. PVOID Job;
  1259. PSW32JOBCALLOUTTYPE CalloutType;
  1260. IN PVOID Data;
  1261. } WIN32_JOBCALLOUT_PARAMETERS, *PKWIN32_JOBCALLOUT_PARAMETERS;
  1262. typedef
  1263. NTSTATUS
  1264. (*PKWIN32_JOB_CALLOUT) (
  1265. IN PKWIN32_JOBCALLOUT_PARAMETERS Parm
  1266. );
  1267. typedef enum _PSW32THREADCALLOUTTYPE {
  1268. PsW32ThreadCalloutInitialize,
  1269. PsW32ThreadCalloutExit
  1270. } PSW32THREADCALLOUTTYPE;
  1271. typedef
  1272. NTSTATUS
  1273. (*PKWIN32_THREAD_CALLOUT) (
  1274. IN PETHREAD Thread,
  1275. IN PSW32THREADCALLOUTTYPE CalloutType
  1276. );
  1277. typedef enum _PSPOWEREVENTTYPE {
  1278. PsW32FullWake,
  1279. PsW32EventCode,
  1280. PsW32PowerPolicyChanged,
  1281. PsW32SystemPowerState,
  1282. PsW32SystemTime,
  1283. PsW32DisplayState,
  1284. PsW32CapabilitiesChanged,
  1285. PsW32SetStateFailed,
  1286. PsW32GdiOff,
  1287. PsW32GdiOn
  1288. } PSPOWEREVENTTYPE;
  1289. typedef struct _WIN32_POWEREVENT_PARAMETERS {
  1290. PSPOWEREVENTTYPE EventNumber;
  1291. ULONG_PTR Code;
  1292. } WIN32_POWEREVENT_PARAMETERS, *PKWIN32_POWEREVENT_PARAMETERS;
  1293. typedef enum _POWERSTATETASK {
  1294. PowerState_BlockSessionSwitch,
  1295. PowerState_Init,
  1296. PowerState_QueryApps,
  1297. PowerState_QueryFailed,
  1298. PowerState_SuspendApps,
  1299. PowerState_ShowUI,
  1300. PowerState_NotifyWL,
  1301. PowerState_ResumeApps,
  1302. PowerState_UnBlockSessionSwitch
  1303. } POWERSTATETASK;
  1304. typedef struct _WIN32_POWERSTATE_PARAMETERS {
  1305. BOOLEAN Promotion;
  1306. POWER_ACTION SystemAction;
  1307. SYSTEM_POWER_STATE MinSystemState;
  1308. ULONG Flags;
  1309. BOOLEAN fQueryDenied;
  1310. POWERSTATETASK PowerStateTask;
  1311. } WIN32_POWERSTATE_PARAMETERS, *PKWIN32_POWERSTATE_PARAMETERS;
  1312. typedef
  1313. NTSTATUS
  1314. (*PKWIN32_POWEREVENT_CALLOUT) (
  1315. IN PKWIN32_POWEREVENT_PARAMETERS Parm
  1316. );
  1317. typedef
  1318. NTSTATUS
  1319. (*PKWIN32_POWERSTATE_CALLOUT) (
  1320. IN PKWIN32_POWERSTATE_PARAMETERS Parm
  1321. );
  1322. typedef
  1323. NTSTATUS
  1324. (*PKWIN32_OBJECT_CALLOUT) (
  1325. IN PVOID Parm
  1326. );
  1327. typedef struct _WIN32_CALLOUTS_FPNS {
  1328. PKWIN32_PROCESS_CALLOUT ProcessCallout;
  1329. PKWIN32_THREAD_CALLOUT ThreadCallout;
  1330. PKWIN32_GLOBALATOMTABLE_CALLOUT GlobalAtomTableCallout;
  1331. PKWIN32_POWEREVENT_CALLOUT PowerEventCallout;
  1332. PKWIN32_POWERSTATE_CALLOUT PowerStateCallout;
  1333. PKWIN32_JOB_CALLOUT JobCallout;
  1334. PVOID BatchFlushRoutine;
  1335. PKWIN32_OBJECT_CALLOUT DesktopOpenProcedure;
  1336. PKWIN32_OBJECT_CALLOUT DesktopOkToCloseProcedure;
  1337. PKWIN32_OBJECT_CALLOUT DesktopCloseProcedure;
  1338. PKWIN32_OBJECT_CALLOUT DesktopDeleteProcedure;
  1339. PKWIN32_OBJECT_CALLOUT WindowStationOkToCloseProcedure;
  1340. PKWIN32_OBJECT_CALLOUT WindowStationCloseProcedure;
  1341. PKWIN32_OBJECT_CALLOUT WindowStationDeleteProcedure;
  1342. PKWIN32_OBJECT_CALLOUT WindowStationParseProcedure;
  1343. PKWIN32_OBJECT_CALLOUT WindowStationOpenProcedure;
  1344. } WIN32_CALLOUTS_FPNS, *PKWIN32_CALLOUTS_FPNS;
  1345. NTKERNELAPI
  1346. VOID
  1347. PsEstablishWin32Callouts(
  1348. IN PKWIN32_CALLOUTS_FPNS pWin32Callouts
  1349. );
  1350. typedef enum _PSPROCESSPRIORITYMODE {
  1351. PsProcessPriorityBackground,
  1352. PsProcessPriorityForeground,
  1353. PsProcessPrioritySpinning
  1354. } PSPROCESSPRIORITYMODE;
  1355. NTKERNELAPI
  1356. VOID
  1357. PsSetProcessPriorityByClass(
  1358. IN PEPROCESS Process,
  1359. IN PSPROCESSPRIORITYMODE PriorityMode
  1360. );
  1361. // end_ntosp
  1362. VOID
  1363. PsWatchWorkingSet(
  1364. IN NTSTATUS Status,
  1365. IN PVOID PcValue,
  1366. IN PVOID Va
  1367. );
  1368. // begin_ntddk begin_nthal begin_ntifs begin_ntosp
  1369. HANDLE
  1370. PsGetCurrentProcessId( VOID );
  1371. HANDLE
  1372. PsGetCurrentThreadId( VOID );
  1373. // end_ntosp
  1374. BOOLEAN
  1375. PsGetVersion(
  1376. PULONG MajorVersion OPTIONAL,
  1377. PULONG MinorVersion OPTIONAL,
  1378. PULONG BuildNumber OPTIONAL,
  1379. PUNICODE_STRING CSDVersion OPTIONAL
  1380. );
  1381. // end_ntddk end_nthal end_ntifs
  1382. // begin_ntosp
  1383. NTKERNELAPI
  1384. ULONG
  1385. PsGetCurrentProcessSessionId(
  1386. VOID
  1387. );
  1388. NTKERNELAPI
  1389. PVOID
  1390. PsGetCurrentThreadStackLimit(
  1391. VOID
  1392. );
  1393. NTKERNELAPI
  1394. PVOID
  1395. PsGetCurrentThreadStackBase(
  1396. VOID
  1397. );
  1398. NTKERNELAPI
  1399. CCHAR
  1400. PsGetCurrentThreadPreviousMode(
  1401. VOID
  1402. );
  1403. NTKERNELAPI
  1404. PERESOURCE
  1405. PsGetJobLock(
  1406. PEJOB Job
  1407. );
  1408. NTKERNELAPI
  1409. ULONG
  1410. PsGetJobSessionId(
  1411. PEJOB Job
  1412. );
  1413. NTKERNELAPI
  1414. ULONG
  1415. PsGetJobUIRestrictionsClass(
  1416. PEJOB Job
  1417. );
  1418. NTKERNELAPI
  1419. LONGLONG
  1420. PsGetProcessCreateTimeQuadPart(
  1421. PEPROCESS Process
  1422. );
  1423. NTKERNELAPI
  1424. PVOID
  1425. PsGetProcessDebugPort(
  1426. PEPROCESS Process
  1427. );
  1428. BOOLEAN
  1429. PsIsProcessBeingDebugged(
  1430. PEPROCESS Process
  1431. );
  1432. NTKERNELAPI
  1433. BOOLEAN
  1434. PsGetProcessExitProcessCalled(
  1435. PEPROCESS Process
  1436. );
  1437. NTKERNELAPI
  1438. NTSTATUS
  1439. PsGetProcessExitStatus(
  1440. PEPROCESS Process
  1441. );
  1442. NTKERNELAPI
  1443. HANDLE
  1444. PsGetProcessId(
  1445. PEPROCESS Process
  1446. );
  1447. NTKERNELAPI
  1448. UCHAR *
  1449. PsGetProcessImageFileName(
  1450. PEPROCESS Process
  1451. );
  1452. #define PsGetCurrentProcessImageFileName() PsGetProcessImageFileName(PsGetCurrentProcess())
  1453. NTKERNELAPI
  1454. HANDLE
  1455. PsGetProcessInheritedFromUniqueProcessId(
  1456. PEPROCESS Process
  1457. );
  1458. NTKERNELAPI
  1459. PEJOB
  1460. PsGetProcessJob(
  1461. PEPROCESS Process
  1462. );
  1463. NTKERNELAPI
  1464. ULONG
  1465. PsGetProcessSessionId(
  1466. PEPROCESS Process
  1467. );
  1468. NTKERNELAPI
  1469. ULONG
  1470. PsGetProcessSessionIdEx(
  1471. PEPROCESS Process
  1472. );
  1473. NTKERNELAPI
  1474. PVOID
  1475. PsGetProcessSectionBaseAddress(
  1476. PEPROCESS Process
  1477. );
  1478. #define PsGetProcessPcb(Process) ((PKPROCESS)(Process))
  1479. NTKERNELAPI
  1480. PPEB
  1481. PsGetProcessPeb(
  1482. PEPROCESS Process
  1483. );
  1484. NTKERNELAPI
  1485. UCHAR
  1486. PsGetProcessPriorityClass(
  1487. PEPROCESS Process
  1488. );
  1489. NTKERNELAPI
  1490. HANDLE
  1491. PsGetProcessWin32WindowStation(
  1492. PEPROCESS Process
  1493. );
  1494. #define PsGetCurrentProcessWin32WindowStation() PsGetProcessWin32WindowStation(PsGetCurrentProcess())
  1495. NTKERNELAPI
  1496. PVOID
  1497. PsGetProcessWin32Process(
  1498. PEPROCESS Process
  1499. );
  1500. #define PsGetCurrentProcessWin32Process() PsGetProcessWin32Process(PsGetCurrentProcess())
  1501. #if defined(_WIN64)
  1502. NTKERNELAPI
  1503. PVOID
  1504. PsGetProcessWow64Process(
  1505. PEPROCESS Process
  1506. );
  1507. #endif
  1508. NTKERNELAPI
  1509. HANDLE
  1510. PsGetThreadId(
  1511. PETHREAD Thread
  1512. );
  1513. NTKERNELAPI
  1514. CCHAR
  1515. PsGetThreadFreezeCount(
  1516. PETHREAD Thread
  1517. );
  1518. NTKERNELAPI
  1519. BOOLEAN
  1520. PsGetThreadHardErrorsAreDisabled(
  1521. PETHREAD Thread);
  1522. NTKERNELAPI
  1523. PEPROCESS
  1524. PsGetThreadProcess(
  1525. PETHREAD Thread
  1526. );
  1527. #define PsGetCurrentThreadProcess() PsGetThreadProcess(PsGetCurrentThread())
  1528. NTKERNELAPI
  1529. HANDLE
  1530. PsGetThreadProcessId(
  1531. PETHREAD Thread
  1532. );
  1533. #define PsGetCurrentThreadProcessId() PsGetThreadProcessId(PsGetCurrentThread())
  1534. NTKERNELAPI
  1535. ULONG
  1536. PsGetThreadSessionId(
  1537. PETHREAD Thread
  1538. );
  1539. #define PsGetThreadTcb(Thread) ((PKTHREAD)(Thread))
  1540. NTKERNELAPI
  1541. PVOID
  1542. PsGetThreadTeb(
  1543. PETHREAD Thread
  1544. );
  1545. #define PsGetCurrentThreadTeb() PsGetThreadTeb(PsGetCurrentThread())
  1546. NTKERNELAPI
  1547. PVOID
  1548. PsGetThreadWin32Thread(
  1549. PETHREAD Thread
  1550. );
  1551. #define PsGetCurrentThreadWin32Thread() PsGetThreadWin32Thread(PsGetCurrentThread())
  1552. NTKERNELAPI //ntifs
  1553. BOOLEAN //ntifs
  1554. PsIsSystemThread( //ntifs
  1555. PETHREAD Thread //ntifs
  1556. ); //ntifs
  1557. NTKERNELAPI
  1558. BOOLEAN
  1559. PsIsThreadImpersonating (
  1560. IN PETHREAD Thread
  1561. );
  1562. NTSTATUS
  1563. PsReferenceProcessFilePointer (
  1564. IN PEPROCESS Process,
  1565. OUT PVOID *pFilePointer
  1566. );
  1567. NTKERNELAPI
  1568. VOID
  1569. PsSetJobUIRestrictionsClass(
  1570. PEJOB Job,
  1571. ULONG UIRestrictionsClass
  1572. );
  1573. NTKERNELAPI
  1574. VOID
  1575. PsSetProcessPriorityClass(
  1576. PEPROCESS Process,
  1577. UCHAR PriorityClass
  1578. );
  1579. NTKERNELAPI
  1580. NTSTATUS
  1581. PsSetProcessWin32Process(
  1582. PEPROCESS Process,
  1583. PVOID Win32Process,
  1584. PVOID PrevWin32Proces
  1585. );
  1586. NTKERNELAPI
  1587. VOID
  1588. PsSetProcessWindowStation(
  1589. PEPROCESS Process,
  1590. HANDLE Win32WindowStation
  1591. );
  1592. NTKERNELAPI
  1593. VOID
  1594. PsSetThreadHardErrorsAreDisabled(
  1595. PETHREAD Thread,
  1596. BOOLEAN HardErrorsAreDisabled
  1597. );
  1598. NTKERNELAPI
  1599. VOID
  1600. PsSetThreadWin32Thread(
  1601. PETHREAD Thread,
  1602. PVOID Win32Thread,
  1603. PVOID PrevWin32Thread
  1604. );
  1605. NTKERNELAPI
  1606. PVOID
  1607. PsGetProcessSecurityPort(
  1608. PEPROCESS Process
  1609. );
  1610. NTKERNELAPI
  1611. NTSTATUS
  1612. PsSetProcessSecurityPort(
  1613. PEPROCESS Process,
  1614. PVOID Port
  1615. );
  1616. typedef
  1617. NTSTATUS
  1618. (*PROCESS_ENUM_ROUTINE)(
  1619. IN PEPROCESS Process,
  1620. IN PVOID Context
  1621. );
  1622. typedef
  1623. NTSTATUS
  1624. (*THREAD_ENUM_ROUTINE)(
  1625. IN PEPROCESS Process,
  1626. IN PETHREAD Thread,
  1627. IN PVOID Context
  1628. );
  1629. NTSTATUS
  1630. PsEnumProcesses (
  1631. IN PROCESS_ENUM_ROUTINE CallBack,
  1632. IN PVOID Context
  1633. );
  1634. NTSTATUS
  1635. PsEnumProcessThreads (
  1636. IN PEPROCESS Process,
  1637. IN THREAD_ENUM_ROUTINE CallBack,
  1638. IN PVOID Context
  1639. );
  1640. PEPROCESS
  1641. PsGetNextProcess (
  1642. IN PEPROCESS Process
  1643. );
  1644. PETHREAD
  1645. PsGetNextProcessThread (
  1646. IN PEPROCESS Process,
  1647. IN PETHREAD Thread
  1648. );
  1649. VOID
  1650. PsQuitNextProcess (
  1651. IN PEPROCESS Process
  1652. );
  1653. VOID
  1654. PsQuitNextProcessThread (
  1655. IN PETHREAD Thread
  1656. );
  1657. PEJOB
  1658. PsGetNextJob (
  1659. IN PEJOB Job
  1660. );
  1661. PEPROCESS
  1662. PsGetNextJobProcess (
  1663. IN PEJOB Job,
  1664. IN PEPROCESS Process
  1665. );
  1666. VOID
  1667. PsQuitNextJob (
  1668. IN PEJOB Job
  1669. );
  1670. VOID
  1671. PsQuitNextJobProcess (
  1672. IN PEPROCESS Process
  1673. );
  1674. NTSTATUS
  1675. PsSuspendProcess (
  1676. IN PEPROCESS Process
  1677. );
  1678. NTSTATUS
  1679. PsResumeProcess (
  1680. IN PEPROCESS Process
  1681. );
  1682. NTSTATUS
  1683. PsTerminateProcess(
  1684. IN PEPROCESS Process,
  1685. IN NTSTATUS Status
  1686. );
  1687. NTSTATUS
  1688. PsSuspendThread (
  1689. IN PETHREAD Thread,
  1690. OUT PULONG PreviousSuspendCount OPTIONAL
  1691. );
  1692. NTSTATUS
  1693. PsResumeThread (
  1694. IN PETHREAD Thread,
  1695. OUT PULONG PreviousSuspendCount OPTIONAL
  1696. );
  1697. #ifndef _WIN64
  1698. NTSTATUS
  1699. PsSetLdtEntries (
  1700. IN ULONG Selector0,
  1701. IN ULONG Entry0Low,
  1702. IN ULONG Entry0Hi,
  1703. IN ULONG Selector1,
  1704. IN ULONG Entry1Low,
  1705. IN ULONG Entry1Hi
  1706. );
  1707. NTSTATUS
  1708. PsSetProcessLdtInfo (
  1709. IN PPROCESS_LDT_INFORMATION LdtInformation,
  1710. IN ULONG LdtInformationLength
  1711. );
  1712. #endif
  1713. // end_ntosp
  1714. #endif // _PS_P