xref: /reactos/ntoskrnl/include/internal/ps.h (revision 86e0d5e9)
1 /*
2  * PROJECT:         ReactOS Kernel
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            ntoskrnl/include/internal/ps.h
5  * PURPOSE:         Internal header for the Process Manager
6  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
7  */
8 
9 //
10 // Define this if you want debugging support
11 //
12 #define _PS_DEBUG_                                      0x00
13 
14 //
15 // These define the Debug Masks Supported
16 //
17 #define PS_THREAD_DEBUG                                 0x01
18 #define PS_PROCESS_DEBUG                                0x02
19 #define PS_SECURITY_DEBUG                               0x04
20 #define PS_JOB_DEBUG                                    0x08
21 #define PS_NOTIFICATIONS_DEBUG                          0x10
22 #define PS_WIN32K_DEBUG                                 0x20
23 #define PS_STATE_DEBUG                                  0x40
24 #define PS_QUOTA_DEBUG                                  0x80
25 #define PS_KILL_DEBUG                                   0x100
26 #define PS_REF_DEBUG                                    0x200
27 
28 //
29 // Debug/Tracing support
30 //
31 #if _PS_DEBUG_
32 #ifdef NEW_DEBUG_SYSTEM_IMPLEMENTED // enable when Debug Filters are implemented
33 #define PSTRACE(x, ...)                                     \
34     {                                                       \
35         DbgPrintEx("%s [%.16s] - ",                         \
36                    __FUNCTION__,                            \
37                    PsGetCurrentProcess()->ImageFileName);   \
38         DbgPrintEx(__VA_ARGS__);                            \
39     }
40 #else
41 #define PSTRACE(x, ...)                                     \
42     if (x & PspTraceLevel)                                  \
43     {                                                       \
44         DbgPrint("%s [%.16s] - ",                           \
45                  __FUNCTION__,                              \
46                  PsGetCurrentProcess()->ImageFileName);     \
47         DbgPrint(__VA_ARGS__);                              \
48     }
49 #endif
50 #define PSREFTRACE(x)                                       \
51     PSTRACE(PS_REF_DEBUG,                                   \
52             "Pointer Count [%p] @%d: %lx\n",                \
53             x,                                              \
54             __LINE__,                                       \
55             OBJECT_TO_OBJECT_HEADER(x)->PointerCount)
56 #else
57 #define PSTRACE(x, fmt, ...) DPRINT(fmt, ##__VA_ARGS__)
58 #define PSREFTRACE(x)
59 #endif
60 
61 //
62 // Maximum Count of Notification Routines
63 //
64 #define PSP_MAX_CREATE_THREAD_NOTIFY            8
65 #define PSP_MAX_LOAD_IMAGE_NOTIFY               8
66 #define PSP_MAX_CREATE_PROCESS_NOTIFY           8
67 
68 //
69 // Maximum Job Scheduling Classes
70 //
71 #define PSP_JOB_SCHEDULING_CLASSES              10
72 
73 //
74 // Process Quota Threshold Values
75 //
76 #define PSP_NON_PAGED_POOL_QUOTA_THRESHOLD              0x10000
77 #define PSP_PAGED_POOL_QUOTA_THRESHOLD                  0x80000
78 
79 //
80 // Thread "Set/Get Context" Context Structure
81 //
82 typedef struct _GET_SET_CTX_CONTEXT
83 {
84     KAPC Apc;
85     KEVENT Event;
86     KPROCESSOR_MODE Mode;
87     CONTEXT Context;
88 } GET_SET_CTX_CONTEXT, *PGET_SET_CTX_CONTEXT;
89 
90 //
91 // Initialization Functions
92 //
93 VOID
94 NTAPI
95 PspShutdownProcessManager(
96     VOID
97 );
98 
99 CODE_SEG("INIT")
100 BOOLEAN
101 NTAPI
102 PsInitSystem(
103     IN PLOADER_PARAMETER_BLOCK LoaderBlock
104 );
105 
106 //
107 // Utility Routines
108 //
109 PETHREAD
110 NTAPI
111 PsGetNextProcessThread(
112     IN PEPROCESS Process,
113     IN PETHREAD Thread OPTIONAL
114 );
115 
116 PEPROCESS
117 NTAPI
118 PsGetNextProcess(
119     IN PEPROCESS OldProcess OPTIONAL
120 );
121 
122 NTSTATUS
123 NTAPI
124 PspMapSystemDll(
125     IN PEPROCESS Process,
126     OUT PVOID *DllBase,
127     IN BOOLEAN UseLargePages
128 );
129 
130 CODE_SEG("INIT")
131 NTSTATUS
132 NTAPI
133 PsLocateSystemDll(
134     VOID
135 );
136 
137 VOID
138 NTAPI
139 PsChangeQuantumTable(
140     IN BOOLEAN Immediate,
141     IN ULONG PrioritySeparation
142 );
143 
144 NTSTATUS
145 NTAPI
146 PsReferenceProcessFilePointer(
147     IN PEPROCESS Process,
148     OUT PFILE_OBJECT *FileObject
149 );
150 
151 //
152 // Process Routines
153 //
154 NTSTATUS
155 NTAPI
156 PspCreateProcess(
157     OUT PHANDLE ProcessHandle,
158     IN ACCESS_MASK DesiredAccess,
159     IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
160     IN HANDLE ParentProcess OPTIONAL,
161     IN ULONG Flags,
162     IN HANDLE SectionHandle OPTIONAL,
163     IN HANDLE DebugPort OPTIONAL,
164     IN HANDLE ExceptionPort OPTIONAL,
165     IN BOOLEAN InJob
166 );
167 
168 //
169 // Security Routines
170 //
171 PACCESS_TOKEN
172 NTAPI
173 PsReferenceEffectiveToken(
174     IN PETHREAD Thread,
175     OUT IN PTOKEN_TYPE TokenType,
176     OUT PBOOLEAN EffectiveOnly,
177     OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
178 );
179 
180 NTSTATUS
181 NTAPI
182 PsOpenTokenOfProcess(
183     IN HANDLE ProcessHandle,
184     OUT PACCESS_TOKEN* Token
185 );
186 
187 NTSTATUS
188 NTAPI
189 PspSetPrimaryToken(
190     IN PEPROCESS Process,
191     IN HANDLE TokenHandle OPTIONAL,
192     IN PACCESS_TOKEN Token OPTIONAL
193 );
194 
195 NTSTATUS
196 NTAPI
197 PspInitializeProcessSecurity(
198     IN PEPROCESS Process,
199     IN PEPROCESS Parent OPTIONAL
200 );
201 
202 VOID
203 NTAPI
204 PspDeleteProcessSecurity(
205     IN PEPROCESS Process
206 );
207 
208 VOID
209 NTAPI
210 PspDeleteThreadSecurity(
211     IN PETHREAD Thread
212 );
213 
214 //
215 // Reaping and Deletion
216 //
217 VOID
218 NTAPI
219 PsExitSpecialApc(
220     PKAPC Apc,
221     PKNORMAL_ROUTINE *NormalRoutine,
222     PVOID *NormalContext,
223     PVOID *SystemArgument1,
224     PVOID *SystemArgument2
225 );
226 
227 VOID
228 NTAPI
229 PspReapRoutine(
230     IN PVOID Context
231 );
232 
233 VOID
234 NTAPI
235 PspExitThread(
236     IN NTSTATUS ExitStatus
237 );
238 
239 NTSTATUS
240 NTAPI
241 PspTerminateThreadByPointer(
242     IN PETHREAD Thread,
243     IN NTSTATUS ExitStatus,
244     IN BOOLEAN bSelf
245 );
246 
247 VOID
248 NTAPI
249 PspExitProcess(
250     IN BOOLEAN LastThread,
251     IN PEPROCESS Process
252 );
253 
254 NTSTATUS
255 NTAPI
256 PsTerminateProcess(
257     IN PEPROCESS Process,
258     IN NTSTATUS ExitStatus
259 );
260 
261 VOID
262 NTAPI
263 PspDeleteProcess(
264     IN PVOID ObjectBody
265 );
266 
267 VOID
268 NTAPI
269 PspDeleteThread(
270     IN PVOID ObjectBody
271 );
272 
273 //
274 // Thread/Process Startup
275 //
276 VOID
277 NTAPI
278 PspSystemThreadStartup(
279     PKSTART_ROUTINE StartRoutine,
280     PVOID StartContext
281 );
282 
283 VOID
284 NTAPI
285 PsIdleThreadMain(
286     IN PVOID Context
287 );
288 
289 //
290 // Quota Support
291 //
292 VOID
293 NTAPI
294 PspInheritQuota(
295     _In_ PEPROCESS Process,
296     _In_ PEPROCESS ParentProcess
297 );
298 
299 VOID
300 NTAPI
301 PspDereferenceQuotaBlock(
302     _In_opt_ PEPROCESS Process,
303     _In_ PEPROCESS_QUOTA_BLOCK QuotaBlock
304 );
305 
306 NTSTATUS
307 NTAPI
308 PsReturnProcessPageFileQuota(
309     _In_ PEPROCESS Process,
310     _In_ SIZE_T Amount
311 );
312 
313 NTSTATUS
314 NTAPI
315 PsChargeProcessPageFileQuota(
316     _In_ PEPROCESS Process,
317     _In_ SIZE_T Amount
318 );
319 
320 VOID
321 NTAPI
322 PsReturnSharedPoolQuota(
323     _In_ PEPROCESS_QUOTA_BLOCK QuotaBlock,
324     _In_ SIZE_T AmountToReturnPaged,
325     _In_ SIZE_T AmountToReturnNonPaged
326 );
327 
328 PEPROCESS_QUOTA_BLOCK
329 NTAPI
330 PsChargeSharedPoolQuota(
331     _In_ PEPROCESS Process,
332     _In_ SIZE_T AmountToChargePaged,
333     _In_ SIZE_T AmountToChargeNonPaged
334 );
335 
336 NTSTATUS
337 NTAPI
338 PspSetQuotaLimits(
339     _In_ PEPROCESS Process,
340     _In_ ULONG Unused,
341     _In_ PVOID QuotaLimits,
342     _In_ ULONG QuotaLimitsLength,
343     _In_ KPROCESSOR_MODE PreviousMode);
344 
345 #if defined(_X86_)
346 //
347 // VDM and LDT Support
348 //
349 VOID
350 NTAPI
351 PspDeleteLdt(
352     IN PEPROCESS Process
353 );
354 
355 VOID
356 NTAPI
357 PspDeleteVdmObjects(
358     IN PEPROCESS Process
359 );
360 
361 NTSTATUS
362 NTAPI
363 PspQueryDescriptorThread(
364     IN PETHREAD Thread,
365     IN PVOID ThreadInformation,
366     IN ULONG ThreadInformationLength,
367     OUT PULONG ReturnLength OPTIONAL
368 );
369 #endif
370 
371 //
372 // Job Routines
373 //
374 VOID
375 NTAPI
376 PspExitProcessFromJob(
377     IN PEJOB Job,
378     IN PEPROCESS Process
379 );
380 
381 VOID
382 NTAPI
383 PspRemoveProcessFromJob(
384     IN PEPROCESS Process,
385     IN PEJOB Job
386 );
387 
388 CODE_SEG("INIT")
389 VOID
390 NTAPI
391 PspInitializeJobStructures(
392     VOID
393 );
394 
395 VOID
396 NTAPI
397 PspDeleteJob(
398     IN PVOID ObjectBody
399 );
400 
401 //
402 // State routines
403 //
404 NTSTATUS
405 NTAPI
406 PsResumeThread(
407     IN PETHREAD Thread,
408     OUT PULONG PreviousCount OPTIONAL
409 );
410 
411 NTSTATUS
412 NTAPI
413 PsSuspendThread(
414     IN PETHREAD Thread,
415     OUT PULONG PreviousCount OPTIONAL
416 );
417 
418 VOID
419 NTAPI
420 PspGetOrSetContextKernelRoutine(
421     IN PKAPC Apc,
422     IN OUT PKNORMAL_ROUTINE* NormalRoutine,
423     IN OUT PVOID* NormalContext,
424     IN OUT PVOID* SystemArgument1,
425     IN OUT PVOID* SystemArgument2
426 );
427 
428 BOOLEAN
429 NTAPI
430 PspIsProcessExiting(IN PEPROCESS Process);
431 
432 //
433 // Apphelp functions
434 //
435 CODE_SEG("INIT")
436 NTSTATUS
437 NTAPI
438 ApphelpCacheInitialize(VOID);
439 
440 VOID
441 NTAPI
442 ApphelpCacheShutdown(VOID);
443 
444 //
445 // Global data inside the Process Manager
446 //
447 extern ULONG PspTraceLevel;
448 extern LCID PsDefaultThreadLocaleId;
449 extern LCID PsDefaultSystemLocaleId;
450 extern LIST_ENTRY PspReaperListHead;
451 extern WORK_QUEUE_ITEM PspReaperWorkItem;
452 extern BOOLEAN PspReaping;
453 extern PEPROCESS PsIdleProcess;
454 extern LIST_ENTRY PsActiveProcessHead;
455 extern KGUARDED_MUTEX PspActiveProcessMutex;
456 extern LARGE_INTEGER ShortPsLockDelay;
457 extern EPROCESS_QUOTA_BLOCK PspDefaultQuotaBlock;
458 extern PHANDLE_TABLE PspCidTable;
459 extern EX_CALLBACK PspThreadNotifyRoutine[PSP_MAX_CREATE_THREAD_NOTIFY];
460 extern EX_CALLBACK PspProcessNotifyRoutine[PSP_MAX_CREATE_PROCESS_NOTIFY];
461 extern EX_CALLBACK PspLoadImageNotifyRoutine[PSP_MAX_LOAD_IMAGE_NOTIFY];
462 extern PLEGO_NOTIFY_ROUTINE PspLegoNotifyRoutine;
463 extern ULONG PspThreadNotifyRoutineCount, PspProcessNotifyRoutineCount;
464 extern BOOLEAN PsImageNotifyEnabled;
465 extern PKWIN32_PROCESS_CALLOUT PspW32ProcessCallout;
466 extern PKWIN32_THREAD_CALLOUT PspW32ThreadCallout;
467 extern PVOID PspSystemDllEntryPoint;
468 extern PVOID PspSystemDllBase;
469 extern BOOLEAN PspUseJobSchedulingClasses;
470 extern CHAR PspJobSchedulingClasses[PSP_JOB_SCHEDULING_CLASSES];
471 extern ULONG PsRawPrioritySeparation;
472 extern ULONG PsPrioritySeparation;
473 extern POBJECT_TYPE _PsThreadType, _PsProcessType;
474 extern PTOKEN PspBootAccessToken;
475 extern GENERIC_MAPPING PspJobMapping;
476 extern POBJECT_TYPE PsJobType;
477 extern LARGE_INTEGER ShortPsLockDelay;
478 extern UNICODE_STRING PsNtDllPathName;
479 extern LIST_ENTRY PsLoadedModuleList;
480 extern KSPIN_LOCK PsLoadedModuleSpinLock;
481 extern ERESOURCE PsLoadedModuleResource;
482 extern ULONG_PTR PsNtosImageBase;
483 
484 //
485 // Inlined Functions
486 //
487 #include "ps_x.h"
488