Added Ex Prototypes
[reactos.git] / reactos / ntoskrnl / include / internal / ps.h
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: ps.h,v 1.59 2004/06/23 21:02:16 ion Exp $
20 *
21 * FILE: ntoskrnl/ke/kthread.c
22 * PURPOSE: Process manager definitions
23 * PROGRAMMER: David Welch (welch@cwcom.net)
24 * UPDATE HISTORY:
25 * Created 22/05/98
26 */
27
28 #ifndef __INCLUDE_INTERNAL_PS_H
29 #define __INCLUDE_INTERNAL_PS_H
30
31 #ifndef __ASM__
32
33 /* Forward declarations. */
34 struct _KTHREAD;
35 struct _KTRAPFRAME;
36
37 #endif /* __ASM__ */
38
39 #include <internal/arch/ps.h>
40
41 #ifndef __ASM__
42
43 #include <internal/mm.h>
44 #include <napi/teb.h>
45
46 #ifndef KeGetCurrentProcessorNumber
47 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
48 #endif
49
50 extern HANDLE SystemProcessHandle;
51
52 extern LCID PsDefaultThreadLocaleId;
53 extern LCID PsDefaultSystemLocaleId;
54
55 #ifndef __USE_W32API
56
57 #include <pshpack1.h>
58
59 typedef struct _KAPC_STATE
60 {
61 LIST_ENTRY ApcListHead[2];
62 struct _KPROCESS* Process;
63 UCHAR KernelApcInProgress;
64 UCHAR KernelApcPending;
65 USHORT UserApcPending;
66 } KAPC_STATE, *PKAPC_STATE, *__restrict PRKAPC_STATE;
67
68 #include <poppack.h>
69
70 #endif /* __USE_W32API */
71
72 #include <pshpack1.h>
73
74 typedef struct _KTHREAD
75 {
76 /* For waiting on thread exit */
77 DISPATCHER_HEADER DispatcherHeader; /* 00 */
78
79 /* List of mutants owned by the thread */
80 LIST_ENTRY MutantListHead; /* 10 */
81 PVOID InitialStack; /* 18 */
82 ULONG StackLimit; /* 1C */
83
84 /* Pointer to the thread's environment block in user memory */
85 PTEB Teb; /* 20 */
86
87 /* Pointer to the thread's TLS array */
88 PVOID TlsArray; /* 24 */
89 PVOID KernelStack; /* 28 */
90 UCHAR DebugActive; /* 2C */
91
92 /* Thread state (one of THREAD_STATE_xxx constants below) */
93 UCHAR State; /* 2D */
94 UCHAR Alerted[2]; /* 2E */
95 UCHAR Iopl; /* 30 */
96 UCHAR NpxState; /* 31 */
97 UCHAR Saturation; /* 32 */
98 CHAR Priority; /* 33 */
99 KAPC_STATE ApcState; /* 34 */
100 ULONG ContextSwitches; /* 4C */
101 ULONG WaitStatus; /* 50 */
102 KIRQL WaitIrql; /* 54 */
103 UCHAR WaitMode; /* 55 */
104 UCHAR WaitNext; /* 56 */
105 UCHAR WaitReason; /* 57 */
106 PKWAIT_BLOCK WaitBlockList; /* 58 */
107 LIST_ENTRY WaitListEntry; /* 5C */
108 ULONG WaitTime; /* 64 */
109 CHAR BasePriority; /* 68 */
110 UCHAR DecrementCount; /* 69 */
111 UCHAR PriorityDecrement; /* 6A */
112 UCHAR Quantum; /* 6B */
113 KWAIT_BLOCK WaitBlock[4]; /* 6C */
114 PVOID LegoData; /* CC */
115 LONG KernelApcDisable; /* D0 */
116 KAFFINITY UserAffinity; /* D4 */
117 UCHAR SystemAffinityActive;/* D8 */
118 UCHAR PowerState; /* D9 */
119 UCHAR NpxIrql; /* DA */
120 UCHAR Pad; /* DB */
121 SSDT_ENTRY *ServiceTable; /* DC */
122 PKQUEUE Queue; /* E0 */
123 KSPIN_LOCK ApcQueueLock; /* E4 */
124 KTIMER Timer; /* E8 */
125 LIST_ENTRY QueueListEntry; /* 110 */
126 KAFFINITY Affinity; /* 118 */
127 UCHAR Preempted; /* 11C */
128 UCHAR ProcessReadyQueue; /* 11D */
129 UCHAR KernelStackResident; /* 11E */
130 UCHAR NextProcessor; /* 11F */
131 PVOID CallbackStack; /* 120 */
132 BOOL Win32Thread; /* 124 */
133 struct _KTRAP_FRAME* TrapFrame; /* 128 */
134 PKAPC_STATE ApcStatePointer[2]; /* 12C */
135 UCHAR EnableStackSwap; /* 134 */
136 UCHAR LargeStack; /* 135 */
137 UCHAR ResourceIndex; /* 136 */
138 UCHAR PreviousMode; /* 137 */
139 ULONG KernelTime; /* 138 */
140 ULONG UserTime; /* 13C */
141 KAPC_STATE SavedApcState; /* 140 */
142 UCHAR Alertable; /* 158 */
143 UCHAR ApcStateIndex; /* 159 */
144 UCHAR ApcQueueable; /* 15A */
145 UCHAR AutoAlignment; /* 15B */
146 PVOID StackBase; /* 15C */
147 KAPC SuspendApc; /* 160 */
148 KSEMAPHORE SuspendSemaphore; /* 190 */
149 LIST_ENTRY ThreadListEntry; /* 1A4 */
150 CHAR FreezeCount; /* 1AC */
151 UCHAR SuspendCount; /* 1AD */
152 UCHAR IdealProcessor; /* 1AE */
153 UCHAR DisableBoost; /* 1AF */
154
155 /*
156 * Below here are thread structure members that are specific to ReactOS
157 */
158
159 /* Added by Phillip Susi for list of threads in a process */
160 LIST_ENTRY ProcessThreadListEntry; /* 1B0 */
161 } KTHREAD;
162
163 #include <poppack.h>
164
165 /* Top level irp definitions. */
166 #define FSRTL_FSP_TOP_LEVEL_IRP (0x01)
167 #define FSRTL_CACHE_TOP_LEVEL_IRP (0x02)
168 #define FSRTL_MOD_WRITE_TOP_LEVEL_IRP (0x03)
169 #define FSRTL_FAST_IO_TOP_LEVEL_IRP (0x04)
170 #define FSRTL_MAX_TOP_LEVEL_IRP_FLAG (0x04)
171
172 typedef struct _TOP_LEVEL_IRP
173 {
174 PIRP TopLevelIrp;
175 ULONG TopLevelIrpConst;
176 } TOP_LEVEL_IRP;
177
178 typedef struct
179 {
180 PACCESS_TOKEN Token; // 0x0
181 BOOLEAN CopyOnOpen; // 0x4
182 BOOLEAN EffectiveOnly; // 0x5
183 UCHAR Pad[2]; // 0x6
184 SECURITY_IMPERSONATION_LEVEL Level; // 0x8
185 } PS_IMPERSONATION_INFO, *PPS_IMPERSONATION_INFO;
186
187 #include <pshpack1.h>
188
189 typedef struct _ETHREAD
190 {
191 KTHREAD Tcb; /* 000 */
192 TIME CreateTime; /* 1B0/1B8 */
193 union
194 {
195 TIME ExitTime; /* 1B8/1E4 */
196 LIST_ENTRY LpcReplyChain; /* 1B8/1E4 */
197 } u1;
198 NTSTATUS ExitStatus; /* 1C0/1EC */
199 LIST_ENTRY PostBlockList; /* 1C4/1F0 */
200 LIST_ENTRY TerminationPortList; /* 1CC/1F8 */
201 KSPIN_LOCK ActiveTimerListLock; /* 1D4/200 */
202 LIST_ENTRY ActiveTimerListHead; /* 1D8/204 */
203 CLIENT_ID Cid; /* 1E0/20C */
204 KSEMAPHORE LpcReplySemaphore; /* 1E8/214 */
205 PVOID LpcReplyMessage; /* 1FC/228 */
206 PLARGE_INTEGER LpcReplyMessageId; /* 200/22C */
207 ULONG PerformanceCounterLow; /* 204/230 */
208 PPS_IMPERSONATION_INFO ImpersonationInfo; /* 208/234 */
209 LIST_ENTRY IrpList; /* 20C/238 */
210 TOP_LEVEL_IRP* TopLevelIrp; /* 214/240 */
211 PDEVICE_OBJECT DeviceToVerify; /* 218/244 */
212 ULONG ReadClusterSize; /* 21C/248 */
213 UCHAR ForwardClusterOnly; /* 220/24C */
214 UCHAR DisablePageFaultClustering; /* 221/24D */
215 UCHAR DeadThread; /* 222/24E */
216 UCHAR HasTerminated; /* 223/24F */
217 PVOID EventPair; /* 224/250 */
218 ACCESS_MASK GrantedAccess; /* 228/254 */
219 struct _EPROCESS* ThreadsProcess; /* 22C/258 */
220 PKSTART_ROUTINE StartAddress; /* 230/25C */
221 union
222 {
223 LPTHREAD_START_ROUTINE Win32StartAddress; /* 234/260 */
224 ULONG LpcReceiveMessageId; /* 234/260 */
225 } u2;
226 UCHAR LpcExitThreadCalled; /* 238/264 */
227 UCHAR HardErrorsAreDisabled; /* 239/265 */
228 UCHAR LpcReceivedMsgIdValid; /* 23A/266 */
229 BOOLEAN ActiveImpersonationInfo; /* 23B/267 */
230 ULONG PerformanceCountHigh; /* 23C/268 */
231
232 /*
233 * Added by David Welch (welch@cwcom.net)
234 */
235 struct _EPROCESS* OldProcess; /* 240/26C */
236
237 struct _W32THREAD* Win32Thread;
238
239 } ETHREAD;
240
241 #include <poppack.h>
242
243
244 #ifndef __USE_W32API
245
246 typedef struct _ETHREAD *PETHREAD;
247
248 #endif /* __USE_W32API */
249
250
251 typedef struct _KPROCESS
252 {
253 /* So it's possible to wait for the process to terminate */
254 DISPATCHER_HEADER DispatcherHeader; /* 000 */
255 /*
256 * Presumably a list of profile objects associated with this process,
257 * currently unused.
258 */
259 LIST_ENTRY ProfileListHead; /* 010 */
260 /*
261 * We use the first member of this array to hold the physical address of
262 * the page directory for this process.
263 */
264 PHYSICAL_ADDRESS DirectoryTableBase; /* 018 */
265 /*
266 * Presumably a descriptor for the process's LDT, currently unused.
267 */
268 ULONG LdtDescriptor[2]; /* 020 */
269 /*
270 * Virtual Dos Machine flag.
271 */
272 ULONG NtVdmFlag; /* 028 */
273 ULONG VdmUnused; /* 02C */
274 /* Is the i/o permission map enabled for the process. */
275 USHORT IopmOffset; /* 030 */
276 /*
277 * Presumably I/O privilege level to be used for this process, currently
278 * unused.
279 */
280 UCHAR Iopl; /* 032 */
281 /* Set if this process is a virtual dos machine? */
282 UCHAR VdmFlag; /* 033 */
283 /* Bitmask of the processors being used by this process's threads? */
284 ULONG ActiveProcessors; /* 034 */
285 /* Aggregate of the time this process's threads have spent in kernel mode? */
286 ULONG KernelTime; /* 038 */
287 /* Aggregate of the time this process's threads have spent in user mode? */
288 ULONG UserTime; /* 03C */
289 /* List of this process's threads that are ready for execution? */
290 LIST_ENTRY ReadyListHead; /* 040 */
291 /* List of this process's threads that have their stacks swapped out? */
292 LIST_ENTRY SwapListEntry; /* 048 */
293 /* List of this process's threads? */
294 LIST_ENTRY ThreadListHead; /* 050 */
295 /* Maybe a lock for this data structure, the type is assumed. */
296 KSPIN_LOCK ProcessLock; /* 058 */
297 /* Default affinity mask for this process's threads? */
298 ULONG Affinity; /* 05C */
299 /* Count of the stacks allocated for this process's threads? */
300 USHORT StackCount; /* 060 */
301 /* Base priority for this process's threads? */
302 KPRIORITY BasePriority; /* 062 */
303 /* Default quantum for this process's threads */
304 UCHAR ThreadQuantum; /* 063 */
305 /* Unknown. */
306 UCHAR AutoAlignment; /* 064 */
307 /* Process execution state, currently either active or terminated. */
308 UCHAR State; /* 065 */
309 /* Seed for generating thread ids for this process's threads? */
310 UCHAR ThreadSeed; /* 066 */
311 /* Disable priority boosts? */
312 UCHAR DisableBoost; /* 067 */
313 } KPROCESS;
314
315 #ifndef __USE_W32API
316
317 typedef struct _KPROCESS *PKPROCESS;
318
319 #endif /* __USE_W32API */
320
321 struct _EPROCESS
322 {
323 /* Microkernel specific process state. */
324 KPROCESS Pcb; /* 000 */
325 /* Exit status of the process. */
326 NTSTATUS ExitStatus; /* 068 */
327 /* Unknown. */
328 KEVENT LockEvent; /* 06C */
329 /* Unknown. */
330 ULONG LockCount; /* 07C */
331
332 /* Time of process creation. */
333 #ifdef __USE_W32API
334 LARGE_INTEGER CreateTime; /* 080 */
335 #else
336 TIME CreateTime; /* 080 */
337 #endif
338
339 /* Time of process exit. */
340 TIME ExitTime; /* 088 */
341 /* Unknown. */
342 PVOID LockOwner; /* 090 */
343 /* Process id. */
344 ULONG UniqueProcessId; /* 094 */
345 /* Unknown. */
346 LIST_ENTRY ActiveProcessLinks; /* 098 */
347 /* Unknown. */
348 ULONG QuotaPeakPoolUsage[2]; /* 0A0 */
349 /* Unknown. */
350 ULONG QuotaPoolUsage[2]; /* 0A8 */
351 /* Unknown. */
352 ULONG PagefileUsage; /* 0B0 */
353 /* Unknown. */
354 ULONG CommitCharge; /* 0B4 */
355 /* Unknown. */
356 ULONG PeakPagefileUsage; /* 0B8 */
357 /* Unknown. */
358 ULONG PeakVirtualSize; /* 0BC */
359 /* Unknown. */
360 LARGE_INTEGER VirtualSize; /* 0C0 */
361 struct
362 {
363 ULONG LastTrimTime;
364 ULONG LastTrimFaultCount;
365 ULONG PageFaultCount;
366 ULONG PeakWorkingSetSize;
367 ULONG WorkingSetSize;
368 ULONG MinimumWorkingSetSize;
369 ULONG MaximumWorkingSetSize;
370 ULONG VmWorkingSetList;
371 LIST_ENTRY WorkingSetExpansionList;
372 UCHAR AllowWorkingSetAdjustment;
373 UCHAR AddressSpaceBeingDeleted;
374 UCHAR ForegroundPrioritySwitch;
375 UCHAR MemoryPriority;
376 } Vm;
377 PVOID LastProtoPteFault;
378 struct _EPORT* DebugPort;
379 struct _EPORT* ExceptionPort;
380 PVOID ObjectTable;
381 PVOID Token;
382 /* FAST_MUTEX WorkingSetLock; */
383 KMUTEX WorkingSetLock;
384 PVOID WorkingSetPage;
385 UCHAR ProcessOutswapEnabled;
386 UCHAR ProcessOutswapped;
387 UCHAR AddressSpaceInitialized;
388 UCHAR AddressSpaceDeleted;
389 FAST_MUTEX AddressCreationLock;
390 KSPIN_LOCK HyperSpaceLock;
391 PETHREAD ForkInProgress;
392 USHORT VmOperation;
393 UCHAR ForkWasSuccessful;
394 UCHAR MmAgressiveWsTrimMask;
395 PKEVENT VmOperationEvent;
396 PVOID PageDirectoryPte;
397 ULONG LastFaultCount;
398 PVOID VadRoot;
399 PVOID VadHint;
400 PVOID CloneRoot;
401 ULONG NumberOfPrivatePages;
402 ULONG NumberOfLockedPages;
403 USHORT NextProcessColour;
404 UCHAR ExitProcessCalled;
405 UCHAR CreateProcessReported;
406 HANDLE SectionHandle;
407 PPEB Peb;
408 PVOID SectionBaseAddress;
409 PVOID QuotaBlock;
410 NTSTATUS LastThreadExitStatus;
411 PVOID WorkingSetWatch;
412 HANDLE InheritedFromUniqueProcessId;
413 ACCESS_MASK GrantedAccess;
414 ULONG DefaultHardErrorProcessing;
415 PVOID LdtInformation;
416 ULONG VadFreeHint;
417 PVOID VdmObjects;
418 KMUTANT ProcessMutant;
419 CHAR ImageFileName[16];
420 ULONG VmTrimFaultValue;
421 UCHAR SetTimerResolution;
422 UCHAR PriorityClass;
423 UCHAR SubSystemMinorVersion;
424 UCHAR SubSystemMajorVersion;
425 USHORT SubSystemVersion;
426 struct _W32PROCESS* Win32Process;
427 HANDLE Win32WindowStation;
428
429 /*
430 * Added by David Welch (welch@mcmail.com)
431 */
432 HANDLE Win32Desktop;
433 MADDRESS_SPACE AddressSpace;
434 HANDLE_TABLE HandleTable;
435 LIST_ENTRY ProcessListEntry;
436
437 /*
438 * Added by Philip Susi for list of threads in process
439 */
440 LIST_ENTRY ThreadListHead;
441 };
442
443 #define PROCESS_STATE_TERMINATED (1)
444 #define PROCESS_STATE_ACTIVE (2)
445
446 VOID PiInitDefaultLocale(VOID);
447 VOID PiInitProcessManager(VOID);
448 VOID PiShutdownProcessManager(VOID);
449 VOID PsInitThreadManagment(VOID);
450 VOID PsInitProcessManagment(VOID);
451 VOID PsInitIdleThread(VOID);
452 VOID PsDispatchThreadNoLock(ULONG NewThreadStatus);
453 VOID PiTerminateProcessThreads(PEPROCESS Process, NTSTATUS ExitStatus);
454 VOID PsTerminateOtherThread(PETHREAD Thread, NTSTATUS ExitStatus);
455 VOID PsReleaseThread(PETHREAD Thread);
456 VOID PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext);
457 VOID PsBeginThreadWithContextInternal(VOID);
458 VOID PiKillMostProcesses(VOID);
459 NTSTATUS STDCALL PiTerminateProcess(PEPROCESS Process, NTSTATUS ExitStatus);
460 VOID PiInitApcManagement(VOID);
461 VOID STDCALL PiDeleteThread(PVOID ObjectBody);
462 VOID PsReapThreads(VOID);
463 NTSTATUS
464 PsInitializeThread(HANDLE ProcessHandle,
465 PETHREAD* ThreadPtr,
466 PHANDLE ThreadHandle,
467 ACCESS_MASK DesiredAccess,
468 POBJECT_ATTRIBUTES ObjectAttributes,
469 BOOLEAN First);
470
471 PACCESS_TOKEN PsReferenceEffectiveToken(PETHREAD Thread,
472 PTOKEN_TYPE TokenType,
473 PUCHAR b,
474 PSECURITY_IMPERSONATION_LEVEL Level);
475
476 NTSTATUS PsOpenTokenOfProcess(HANDLE ProcessHandle,
477 PACCESS_TOKEN* Token);
478
479 NTSTATUS PsSuspendThread(PETHREAD Thread, PULONG PreviousCount);
480 NTSTATUS PsResumeThread(PETHREAD Thread, PULONG PreviousCount);
481
482
483 #define THREAD_STATE_INITIALIZED (0)
484 #define THREAD_STATE_READY (1)
485 #define THREAD_STATE_RUNNING (2)
486 #define THREAD_STATE_SUSPENDED (3)
487 #define THREAD_STATE_FROZEN (4)
488 #define THREAD_STATE_TERMINATED_1 (5)
489 #define THREAD_STATE_TERMINATED_2 (6)
490 #define THREAD_STATE_BLOCKED (7)
491 #define THREAD_STATE_MAX (8)
492
493
494 /*
495 * Internal thread priorities, added by Phillip Susi
496 * TODO: rebalence these to make use of all priorities... the ones above 16
497 * can not all be used right now
498 */
499 #define PROCESS_PRIO_IDLE 3
500 #define PROCESS_PRIO_NORMAL 8
501 #define PROCESS_PRIO_HIGH 13
502 #define PROCESS_PRIO_RT 18
503
504
505 VOID
506 KeInitializeThread(PKPROCESS Process, PKTHREAD Thread, BOOLEAN First);
507 NTSTATUS KeReleaseThread(PETHREAD Thread);
508
509 STDCALL
510 VOID
511 KeStackAttachProcess (
512 IN PKPROCESS Process,
513 OUT PRKAPC_STATE ApcState
514 );
515
516 STDCALL
517 VOID
518 KeUnstackDetachProcess (
519 IN PRKAPC_STATE ApcState
520 );
521
522 VOID STDCALL PiDeleteProcess(PVOID ObjectBody);
523 VOID PsReapThreads(VOID);
524 VOID PsUnfreezeOtherThread(PETHREAD Thread);
525 VOID PsFreezeOtherThread(PETHREAD Thread);
526 VOID PsFreezeProcessThreads(PEPROCESS Process);
527 VOID PsUnfreezeProcessThreads(PEPROCESS Process);
528 PEPROCESS PsGetNextProcess(PEPROCESS OldProcess);
529 VOID
530 PsBlockThread(PNTSTATUS Status, UCHAR Alertable, ULONG WaitMode,
531 BOOLEAN DispatcherLock, KIRQL WaitIrql, UCHAR WaitReason);
532 VOID
533 PsUnblockThread(PETHREAD Thread, PNTSTATUS WaitStatus);
534 VOID
535 PsApplicationProcessorInit(VOID);
536 VOID
537 PsPrepareForApplicationProcessorInit(ULONG Id);
538 VOID STDCALL
539 PsIdleThreadMain(PVOID Context);
540
541 VOID STDCALL
542 PiSuspendThreadRundownRoutine(PKAPC Apc);
543 VOID STDCALL
544 PiSuspendThreadKernelRoutine(PKAPC Apc,
545 PKNORMAL_ROUTINE* NormalRoutine,
546 PVOID* NormalContext,
547 PVOID* SystemArgument1,
548 PVOID* SystemArguemnt2);
549 VOID STDCALL
550 PiSuspendThreadNormalRoutine(PVOID NormalContext,
551 PVOID SystemArgument1,
552 PVOID SystemArgument2);
553 VOID STDCALL
554 PsDispatchThread(ULONG NewThreadStatus);
555 VOID
556 PsInitialiseSuspendImplementation(VOID);
557
558 extern ULONG PiNrThreadsAwaitingReaping;
559
560
561 NTSTATUS
562 PsInitWin32Thread (PETHREAD Thread);
563
564 VOID
565 PsTerminateWin32Process (PEPROCESS Process);
566
567 VOID
568 PsTerminateWin32Thread (PETHREAD Thread);
569
570 VOID
571 PsInitialiseW32Call(VOID);
572
573 VOID
574 STDCALL
575 PspRunCreateThreadNotifyRoutines(PETHREAD, BOOLEAN);
576
577 VOID
578 STDCALL
579 PspRunCreateProcessNotifyRoutines(PEPROCESS, BOOLEAN);
580
581 #endif /* ASSEMBLER */
582
583 #endif /* __INCLUDE_INTERNAL_PS_H */