Source code of Windows XP (NT5)
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.

750 lines
15 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. psp.h
  5. Abstract:
  6. Private Interfaces for process structure.
  7. Author:
  8. Mark Lucovsky (markl) 20-Apr-1989
  9. Revision History:
  10. --*/
  11. #ifndef _PSP_
  12. #define _PSP_
  13. #pragma warning(disable:4054) // Cast of function pointer to PVOID
  14. #pragma warning(disable:4055) // Cast of function pointer
  15. #pragma warning(disable:4115) // named type definition in parentheses
  16. #pragma warning(disable:4127) // condition expression is constant
  17. #pragma warning(disable:4152) // Casting function pointers
  18. #pragma warning(disable:4201) // nameless struct/union
  19. #pragma warning(disable:4214) // bit field types other than int
  20. #pragma warning(disable:4324) // alignment sensitive to declspec
  21. #pragma warning(disable:4327) // alignment on assignment
  22. #pragma warning(disable:4328) // alignment on assignment
  23. #include "ntos.h"
  24. #include "ntrtl.h"
  25. #include "nturtl.h"
  26. #include "zwapi.h"
  27. #include "ki.h"
  28. #if defined(_X86_)
  29. #include <vdmntos.h>
  30. #endif
  31. #define NOEXTAPI
  32. #include "wdbgexts.h"
  33. #include "ntdbg.h"
  34. #include <string.h>
  35. #if defined(_WIN64)
  36. #include <wow64t.h>
  37. #endif
  38. #ifdef POOL_TAGGING
  39. #define ExAllocatePool(a,b) ExAllocatePoolWithTag(a,b,' sP')
  40. #define ExAllocatePoolWithQuota(a,b) ExAllocatePoolWithQuotaTag(a,b,' sP')
  41. #endif
  42. //
  43. // Working Set Watcher is 8kb. This lets us watch about 4mb of working
  44. // set.
  45. //
  46. #define WS_CATCH_SIZE 8192
  47. #define WS_OVERHEAD 16
  48. #define MAX_WS_CATCH_INDEX (((WS_CATCH_SIZE-WS_OVERHEAD)/sizeof(PROCESS_WS_WATCH_INFORMATION)) - 2)
  49. //
  50. // Process Quota Charges:
  51. //
  52. // PagedPool
  53. // Directory Base Page - PAGE_SIZE
  54. //
  55. // NonPaged
  56. // Object Body - sizeof(EPROCESS)
  57. //
  58. #define PSP_PROCESS_PAGED_CHARGE (PAGE_SIZE)
  59. #define PSP_PROCESS_NONPAGED_CHARGE (sizeof(EPROCESS))
  60. //
  61. // Thread Quota Charges:
  62. //
  63. // PagedPool
  64. // Kernel Stack - 0
  65. //
  66. // NonPaged
  67. // Object Body - sizeof(ETHREAD)
  68. //
  69. #define PSP_THREAD_PAGED_CHARGE (0)
  70. #define PSP_THREAD_NONPAGED_CHARGE (sizeof(ETHREAD))
  71. typedef struct _GETSETCONTEXT {
  72. KAPC Apc;
  73. KPROCESSOR_MODE Mode;
  74. KEVENT OperationComplete;
  75. CONTEXT Context;
  76. KNONVOLATILE_CONTEXT_POINTERS NonVolatileContext;
  77. } GETSETCONTEXT, *PGETSETCONTEXT;
  78. typedef struct _SYSTEM_DLL {
  79. PVOID Section;
  80. PVOID DllBase;
  81. PKNORMAL_ROUTINE LoaderInitRoutine;
  82. } SYSTEM_DLL, PSYSTEM_DLL;
  83. typedef struct _JOB_WORKING_SET_CHANGE_HEAD {
  84. LIST_ENTRY Links;
  85. FAST_MUTEX Lock;
  86. SIZE_T MinimumWorkingSetSize;
  87. SIZE_T MaximumWorkingSetSize;
  88. } JOB_WORKING_SET_CHANGE_HEAD, *PJOB_WORKING_SET_CHANGE_HEAD;
  89. typedef struct _JOB_WORKING_SET_CHANGE_RECORD {
  90. LIST_ENTRY Links;
  91. PEPROCESS Process;
  92. } JOB_WORKING_SET_CHANGE_RECORD, *PJOB_WORKING_SET_CHANGE_RECORD;
  93. JOB_WORKING_SET_CHANGE_HEAD PspWorkingSetChangeHead;
  94. //
  95. // Private Entry Points
  96. //
  97. VOID
  98. PspProcessDump(
  99. IN PVOID Object,
  100. IN POB_DUMP_CONTROL Control OPTIONAL
  101. );
  102. VOID
  103. PspProcessDelete(
  104. IN PVOID Object
  105. );
  106. VOID
  107. PspThreadDump(
  108. IN PVOID Object,
  109. IN POB_DUMP_CONTROL Control OPTIONAL
  110. );
  111. VOID
  112. PspInheritQuota(
  113. IN PEPROCESS NewProcess,
  114. IN PEPROCESS ParentProcess
  115. );
  116. VOID
  117. PspDereferenceQuota(
  118. IN PEPROCESS Process
  119. );
  120. VOID
  121. PspThreadDelete(
  122. IN PVOID Object
  123. );
  124. //
  125. // Initialization and loader entrypoints
  126. //
  127. BOOLEAN
  128. PspInitPhase0 (
  129. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  130. );
  131. BOOLEAN
  132. PspInitPhase1 (
  133. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  134. );
  135. NTSTATUS
  136. PspInitializeSystemDll( VOID );
  137. NTSTATUS
  138. PspLookupSystemDllEntryPoint(
  139. IN PSZ EntryPointName,
  140. OUT PVOID *EntryPointAddress
  141. );
  142. NTSTATUS
  143. PspLookupKernelUserEntryPoints(
  144. VOID
  145. );
  146. USHORT
  147. PspNameToOrdinal(
  148. IN PSZ EntryPointName,
  149. IN ULONG DllBase,
  150. IN ULONG NumberOfNames,
  151. IN PULONG NameTableBase,
  152. IN PUSHORT OrdinalTableBase
  153. );
  154. NTSTATUS
  155. PspMapSystemDll(
  156. IN PEPROCESS Process,
  157. OUT PVOID *DllBase OPTIONAL
  158. );
  159. //
  160. // Internal Creation Functions
  161. //
  162. NTSTATUS
  163. PspCreateProcess(
  164. OUT PHANDLE ProcessHandle,
  165. IN ACCESS_MASK DesiredAccess,
  166. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  167. IN HANDLE ParentProcess OPTIONAL,
  168. IN ULONG Flags,
  169. IN HANDLE SectionHandle OPTIONAL,
  170. IN HANDLE DebugPort OPTIONAL,
  171. IN HANDLE ExceptionPort OPTIONAL,
  172. IN ULONG JobMemberLevel
  173. );
  174. #define PSP_MAX_CREATE_PROCESS_NOTIFY 8
  175. //
  176. // Define process callouts. These are of type PCREATE_PROCESS_NOTIFY_ROUTINE
  177. // Called on process create and delete.
  178. //
  179. ULONG PspCreateProcessNotifyRoutineCount;
  180. EX_CALLBACK PspCreateProcessNotifyRoutine[PSP_MAX_CREATE_PROCESS_NOTIFY];
  181. #define PSP_MAX_CREATE_THREAD_NOTIFY 8
  182. //
  183. // Define thread callouts. These are of type PCREATE_THREAD_NOTIFY_ROUTINE
  184. // Called on thread create and delete.
  185. //
  186. ULONG PspCreateThreadNotifyRoutineCount;
  187. EX_CALLBACK PspCreateThreadNotifyRoutine[PSP_MAX_CREATE_THREAD_NOTIFY];
  188. #define PSP_MAX_LOAD_IMAGE_NOTIFY 8
  189. //
  190. // Define image load callbacks. These are of type PLOAD_IMAGE_NOTIFY_ROUTINE
  191. // Called on image load.
  192. //
  193. ULONG PspLoadImageNotifyRoutineCount;
  194. EX_CALLBACK PspLoadImageNotifyRoutine[PSP_MAX_LOAD_IMAGE_NOTIFY];
  195. NTSTATUS
  196. PspCreateThread(
  197. OUT PHANDLE ThreadHandle,
  198. IN ACCESS_MASK DesiredAccess,
  199. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  200. IN HANDLE ProcessHandle,
  201. IN PEPROCESS ProcessPointer,
  202. OUT PCLIENT_ID ClientId OPTIONAL,
  203. IN PCONTEXT ThreadContext OPTIONAL,
  204. IN PINITIAL_TEB InitialTeb OPTIONAL,
  205. IN BOOLEAN CreateSuspended,
  206. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  207. IN PVOID StartContext
  208. );
  209. //
  210. // Startup Routines
  211. //
  212. VOID
  213. PspUserThreadStartup(
  214. IN PKSTART_ROUTINE StartRoutine,
  215. IN PVOID StartContext
  216. );
  217. VOID
  218. PspSystemThreadStartup(
  219. IN PKSTART_ROUTINE StartRoutine,
  220. IN PVOID StartContext
  221. );
  222. VOID
  223. PspReaper(
  224. IN PVOID StartContext
  225. );
  226. VOID
  227. PspNullSpecialApc(
  228. IN PKAPC Apc,
  229. IN OUT PKNORMAL_ROUTINE *NormalRoutine,
  230. IN OUT PVOID *NormalContext,
  231. IN OUT PVOID *SystemArgument1,
  232. IN OUT PVOID *SystemArgument2
  233. );
  234. //
  235. // Thread Exit Support
  236. //
  237. VOID
  238. PspExitApcRundown(
  239. IN PKAPC Apc
  240. );
  241. DECLSPEC_NORETURN
  242. VOID
  243. PspExitThread(
  244. IN NTSTATUS ExitStatus
  245. );
  246. NTSTATUS
  247. PspTerminateThreadByPointer(
  248. IN PETHREAD Thread,
  249. IN NTSTATUS ExitStatus
  250. );
  251. VOID
  252. PspExitSpecialApc(
  253. IN PKAPC Apc,
  254. IN OUT PKNORMAL_ROUTINE *NormalRoutine,
  255. IN OUT PVOID *NormalContext,
  256. IN OUT PVOID *SystemArgument1,
  257. IN OUT PVOID *SystemArgument2
  258. );
  259. VOID
  260. PspExitProcess(
  261. IN BOOLEAN TrimAddressSpace,
  262. IN PEPROCESS Process
  263. );
  264. NTSTATUS
  265. PspWaitForUsermodeExit(
  266. IN PEPROCESS Process
  267. );
  268. //
  269. // Context Management
  270. //
  271. VOID
  272. PspSetContext(
  273. OUT PKTRAP_FRAME TrapFrame,
  274. OUT PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext,
  275. IN PCONTEXT Context,
  276. KPROCESSOR_MODE Mode
  277. );
  278. VOID
  279. PspGetContext(
  280. IN PKTRAP_FRAME TrapFrame,
  281. IN PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext,
  282. IN OUT PCONTEXT Context
  283. );
  284. VOID
  285. PspGetSetContextSpecialApc(
  286. IN PKAPC Apc,
  287. IN OUT PKNORMAL_ROUTINE *NormalRoutine,
  288. IN OUT PVOID *NormalContext,
  289. IN OUT PVOID *SystemArgument1,
  290. IN OUT PVOID *SystemArgument2
  291. );
  292. VOID
  293. PspExitNormalApc(
  294. IN PVOID NormalContext,
  295. IN PVOID SystemArgument1,
  296. IN PVOID SystemArgument2
  297. );
  298. //
  299. // private security routines
  300. //
  301. NTSTATUS
  302. PspInitializeProcessSecurity(
  303. IN PEPROCESS Parent OPTIONAL,
  304. IN PEPROCESS Child
  305. );
  306. VOID
  307. PspDeleteProcessSecurity(
  308. IN PEPROCESS Process
  309. );
  310. VOID
  311. PspInitializeThreadSecurity(
  312. IN PEPROCESS Process,
  313. IN PETHREAD Thread
  314. );
  315. VOID
  316. PspDeleteThreadSecurity(
  317. IN PETHREAD Thread
  318. );
  319. NTSTATUS
  320. PspAssignPrimaryToken(
  321. IN PEPROCESS Process,
  322. IN HANDLE Token OPTIONAL,
  323. IN PACCESS_TOKEN TokenPointer OPTIONAL
  324. );
  325. NTSTATUS
  326. PspSetPrimaryToken(
  327. IN HANDLE ProcessHandle,
  328. IN PEPROCESS ProcessPointer OPTIONAL,
  329. IN HANDLE TokenHandle OPTIONAL,
  330. IN PACCESS_TOKEN TokenPointer OPTIONAL,
  331. IN BOOLEAN PrivilegeChecked
  332. );
  333. //
  334. // Ldt support routines
  335. //
  336. #if defined(i386)
  337. NTSTATUS
  338. PspLdtInitialize(
  339. );
  340. #endif
  341. //
  342. // Vdm support Routines
  343. #if defined(i386)
  344. NTSTATUS
  345. PspVdmInitialize(
  346. );
  347. #endif
  348. NTSTATUS
  349. PspQueryLdtInformation(
  350. IN PEPROCESS Process,
  351. OUT PVOID LdtInformation,
  352. IN ULONG LdtInformationLength,
  353. OUT PULONG ReturnLength
  354. );
  355. NTSTATUS
  356. PspSetLdtInformation(
  357. IN PEPROCESS Process,
  358. IN PVOID LdtInformation,
  359. IN ULONG LdtInformationLength
  360. );
  361. NTSTATUS
  362. PspSetLdtSize(
  363. IN PEPROCESS Process,
  364. IN PVOID LdtSize,
  365. IN ULONG LdtSizeLength
  366. );
  367. VOID
  368. PspDeleteLdt(
  369. IN PEPROCESS Process
  370. );
  371. //
  372. // Io handling support routines
  373. //
  374. NTSTATUS
  375. PspSetProcessIoHandlers(
  376. IN PEPROCESS Process,
  377. IN PVOID IoHandlerInformation,
  378. IN ULONG IoHandlerLength
  379. );
  380. VOID
  381. PspDeleteVdmObjects(
  382. IN PEPROCESS Process
  383. );
  384. NTSTATUS
  385. PspQueryDescriptorThread (
  386. PETHREAD Thread,
  387. PVOID ThreadInformation,
  388. ULONG ThreadInformationLength,
  389. PULONG ReturnLength
  390. );
  391. //
  392. // Job Object Support Routines
  393. //
  394. VOID
  395. PspInitializeJobStructures(
  396. );
  397. VOID
  398. PspInitializeJobStructuresPhase1(
  399. );
  400. VOID
  401. PspJobTimeLimitsWork(
  402. IN PVOID Context
  403. );
  404. VOID
  405. PspJobTimeLimitsDpcRoutine(
  406. IN PKDPC Dpc,
  407. IN PVOID DeferredContext,
  408. IN PVOID SystemArgument1,
  409. IN PVOID SystemArgument2
  410. );
  411. VOID
  412. PspJobDelete(
  413. IN PVOID Object
  414. );
  415. VOID
  416. PspJobClose (
  417. IN PEPROCESS Process,
  418. IN PVOID Object,
  419. IN ACCESS_MASK GrantedAccess,
  420. IN ULONG ProcessHandleCount,
  421. IN ULONG SystemHandleCount
  422. );
  423. NTSTATUS
  424. PspAddProcessToJob(
  425. PEJOB Job,
  426. PEPROCESS Process
  427. );
  428. VOID
  429. PspRemoveProcessFromJob(
  430. PEJOB Job,
  431. PEPROCESS Process
  432. );
  433. VOID
  434. PspExitProcessFromJob(
  435. PEJOB Job,
  436. PEPROCESS Process
  437. );
  438. VOID
  439. PspApplyJobLimitsToProcessSet(
  440. PEJOB Job
  441. );
  442. VOID
  443. PspApplyJobLimitsToProcess(
  444. PEJOB Job,
  445. PEPROCESS Process
  446. );
  447. BOOLEAN
  448. PspTerminateAllProcessesInJob(
  449. PEJOB Job,
  450. NTSTATUS Status,
  451. BOOLEAN IncCounter
  452. );
  453. VOID
  454. PspFoldProcessAccountingIntoJob(
  455. PEJOB Job,
  456. PEPROCESS Process
  457. );
  458. NTSTATUS
  459. PspCaptureTokenFilter(
  460. KPROCESSOR_MODE PreviousMode,
  461. PJOBOBJECT_SECURITY_LIMIT_INFORMATION SecurityLimitInfo,
  462. PPS_JOB_TOKEN_FILTER * TokenFilter
  463. );
  464. VOID
  465. PspShutdownJobLimits(
  466. VOID
  467. );
  468. NTSTATUS
  469. PspTerminateProcess(
  470. PEPROCESS Process,
  471. NTSTATUS Status
  472. );
  473. NTSTATUS
  474. PspGetJobFromSet (
  475. IN PEJOB ParentJob,
  476. IN ULONG JobMemberLevel,
  477. OUT PEJOB *pJob);
  478. NTSTATUS
  479. PspWin32SessionCallout(
  480. IN PKWIN32_JOB_CALLOUT CalloutRoutine,
  481. IN PKWIN32_JOBCALLOUT_PARAMETERS Parameters,
  482. IN ULONG SessionId
  483. );
  484. //
  485. // This test routine is called on checked systems to test this path
  486. //
  487. VOID
  488. PspImageNotifyTest(
  489. IN PUNICODE_STRING FullImageName,
  490. IN HANDLE ProcessId,
  491. IN PIMAGE_INFO ImageInfo
  492. );
  493. PEPROCESS
  494. PspGetNextJobProcess (
  495. IN PEJOB Job,
  496. IN PEPROCESS Process
  497. );
  498. VOID
  499. PspQuitNextJobProcess (
  500. IN PEPROCESS Process
  501. );
  502. VOID
  503. PspInsertQuotaBlock (
  504. IN PEPROCESS_QUOTA_BLOCK QuotaBlock
  505. );
  506. #define PspInitializeProcessLock(xProcess) { \
  507. ExInitializePushLock (&xProcess->ProcessLock); \
  508. }
  509. #define PspLockProcessExclusive(xProcess,xCurrentThread) { \
  510. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  511. ExAcquirePushLockExclusive (&xProcess->ProcessLock); \
  512. }
  513. #define PspLockProcessShared(xProcess,xCurrentThread) { \
  514. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  515. ExAcquirePushLockShared (&xProcess->ProcessLock); \
  516. }
  517. #define PspUnlockProcessShared(xProcess,xCurrentThread) { \
  518. ExReleasePushLockShared (&xProcess->ProcessLock); \
  519. KeLeaveCriticalRegionThread (&(xCurrentThread)->Tcb); \
  520. }
  521. #define PspUnlockProcessExclusive(xProcess,xCurrentThread) { \
  522. ExReleasePushLockExclusive (&xProcess->ProcessLock); \
  523. KeLeaveCriticalRegionThread (&(xCurrentThread)->Tcb); \
  524. }
  525. //
  526. // Define macros to lock the security fields of the process and thread
  527. //
  528. #define PspLockProcessSecurityExclusive(xProcess,xCurrentThread) \
  529. PspLockProcessExclusive (xProcess, xCurrentThread)
  530. #define PspLockProcessSecurityShared(xProcess,xCurrentThread) \
  531. PspLockProcessShared (xProcess, xCurrentThread)
  532. #define PspUnlockProcessSecurityShared(xProcess,xCurrentThread) \
  533. PspUnlockProcessShared (xProcess, xCurrentThread)
  534. #define PspUnlockProcessSecurityExclusive(xProcess,xCurrentThread) \
  535. PspUnlockProcessExclusive (xProcess, xCurrentThread)
  536. #define PspInitializeThreadLock(xThread) { \
  537. ExInitializePushLock (&xThread->ThreadLock); \
  538. }
  539. #define PspLockThreadSecurityExclusive(xThread,xCurrentThread) { \
  540. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  541. ExAcquirePushLockExclusive (&xThread->ThreadLock); \
  542. }
  543. #define PspLockThreadSecurityShared(xThread,xCurrentThread) { \
  544. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  545. ExAcquirePushLockShared (&xThread->ThreadLock); \
  546. }
  547. #define PspLockThreadSecurityExclusive(xThread,xCurrentThread) { \
  548. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  549. ExAcquirePushLockExclusive (&xThread->ThreadLock); \
  550. }
  551. #define PspUnlockThreadSecurityShared(xThread,xCurrentThread) { \
  552. ExReleasePushLockShared (&xThread->ThreadLock); \
  553. KeLeaveCriticalRegionThread (&(xCurrentThread)->Tcb); \
  554. }
  555. #define PspUnlockThreadSecurityExclusive(xThread,xCurrentThread) { \
  556. ExReleasePushLockExclusive (&xThread->ThreadLock); \
  557. KeLeaveCriticalRegionThread (&(xCurrentThread)->Tcb); \
  558. }
  559. //
  560. // Define macros to lock the global process list
  561. //
  562. #define PspLockProcessList(xCurrentThread) { \
  563. KeEnterCriticalRegionThread (&(xCurrentThread)->Tcb); \
  564. ExAcquireFastMutexUnsafe (&PspActiveProcessMutex); \
  565. }
  566. #define PspUnlockProcessList(xCurrentThread) { \
  567. ExReleaseFastMutexUnsafe (&PspActiveProcessMutex); \
  568. KeLeaveCriticalRegionThread (&(xCurrentThread)->Tcb); \
  569. }
  570. //
  571. //
  572. // Global Data
  573. //
  574. extern PHANDLE_TABLE PspCidTable;
  575. extern HANDLE PspInitialSystemProcessHandle;
  576. extern PACCESS_TOKEN PspBootAccessToken;
  577. extern KSPIN_LOCK PspEventPairLock;
  578. extern SYSTEM_DLL PspSystemDll;
  579. extern FAST_MUTEX PspActiveProcessMutex;
  580. extern PETHREAD PspShutdownThread;
  581. extern ULONG PspDefaultPagedLimit;
  582. extern ULONG PspDefaultNonPagedLimit;
  583. extern ULONG PspDefaultPagefileLimit;
  584. extern ULONG PsMinimumWorkingSet;
  585. extern EPROCESS_QUOTA_BLOCK PspDefaultQuotaBlock;
  586. extern BOOLEAN PspDoingGiveBacks;
  587. extern PKWIN32_PROCESS_CALLOUT PspW32ProcessCallout;
  588. extern PKWIN32_THREAD_CALLOUT PspW32ThreadCallout;
  589. extern PKWIN32_JOB_CALLOUT PspW32JobCallout;
  590. extern ULONG PspW32ProcessSize;
  591. extern ULONG PspW32ThreadSize;
  592. extern SCHAR PspForegroundQuantum[3];
  593. #define PSP_NUMBER_OF_SCHEDULING_CLASSES 10
  594. #define PSP_DEFAULT_SCHEDULING_CLASSES 5
  595. extern const SCHAR PspJobSchedulingClasses[PSP_NUMBER_OF_SCHEDULING_CLASSES];
  596. extern BOOLEAN PspUseJobSchedulingClasses;
  597. extern FAST_MUTEX PspJobListLock;
  598. extern LIST_ENTRY PspJobList;
  599. extern KDPC PspJobLimeLimitsDpc;
  600. extern KTIMER PspJobTimeLimitsTimer;
  601. extern WORK_QUEUE_ITEM PspJobTimeLimitsWorkItem;
  602. extern KSPIN_LOCK PspQuotaLock;
  603. #endif // _PSP_