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