3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
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.
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.
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.
21 * FILE: ntoskrnl/ke/kthread.c
22 * PURPOSE: Process manager definitions
23 * PROGRAMMER: David Welch (welch@cwcom.net)
28 #ifndef __INCLUDE_INTERNAL_PS_H
29 #define __INCLUDE_INTERNAL_PS_H
33 /* Forward declarations. */
39 #include <internal/arch/ps.h>
43 #include <internal/mm.h>
46 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
48 extern HANDLE SystemProcessHandle;
50 extern LCID PsDefaultThreadLocaleId;
51 extern LCID PsDefaultSystemLocaleId;
53 typedef struct _KAPC_STATE
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;
62 typedef struct _KTHREAD
64 /* For waiting on thread exit */
65 DISPATCHER_HEADER DispatcherHeader; /* 00 */
67 /* List of mutants owned by the thread */
68 LIST_ENTRY MutantListHead; /* 10 */
69 PVOID InitialStack; /* 18 */
70 ULONG StackLimit; /* 1C */
72 /* Pointer to the thread's environment block in user memory */
75 /* Pointer to the thread's TLS array */
76 PVOID TlsArray; /* 24 */
77 PVOID KernelStack; /* 28 */
78 UCHAR DebugActive; /* 2C */
80 /* Thread state (one of THREAD_STATE_xxx constants below) */
82 UCHAR Alerted[2]; /* 2E */
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 */
141 * Below here are thread structure members that are specific to ReactOS
144 /* Added by Phillip Susi for list of threads in a process */
145 LIST_ENTRY ProcessThreadListEntry; /* 1B0 */
146 } __attribute__((packed)) KTHREAD, *PKTHREAD;
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)
155 typedef struct _TOP_LEVEL_IRP
158 ULONG TopLevelIrpConst;
163 PACCESS_TOKEN Token; // 0x0
164 UCHAR Unknown1; // 0x4
165 UCHAR Unknown2; // 0x5
167 SECURITY_IMPERSONATION_LEVEL Level; // 0x8
168 } PS_IMPERSONATION_INFO, *PPS_IMPERSONATION_INFO;
170 typedef struct _ETHREAD
172 KTHREAD Tcb; /* 000 */
173 TIME CreateTime; /* 1B0/1B8 */
176 TIME ExitTime; /* 1B8/1E4 */
177 LIST_ENTRY LpcReplyChain; /* 1B8/1E4 */
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 */
204 LPTHREAD_START_ROUTINE Win32StartAddress; /* 234/260 */
205 ULONG LpcReceiveMessageId; /* 234/260 */
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 */
214 * Added by David Welch (welch@cwcom.net)
216 struct _EPROCESS* OldProcess; /* 240/26C */
218 struct _W32THREAD* Win32Thread;
220 } __attribute__((packed)) ETHREAD, *PETHREAD;
223 typedef struct _KPROCESS
225 /* So it's possible to wait for the process to terminate */
226 DISPATCHER_HEADER DispatcherHeader; /* 000 */
228 * Presumably a list of profile objects associated with this process,
231 LIST_ENTRY ProfileListHead; /* 010 */
233 * We use the first member of this array to hold the physical address of
234 * the page directory for this process.
236 PHYSICAL_ADDRESS DirectoryTableBase; /* 018 */
238 * Presumably a descriptor for the process's LDT, currently unused.
240 ULONG LdtDescriptor[2]; /* 020 */
242 * Presumably for processing int 0x21 from V86 mode DOS, currently
245 ULONG Int21Descriptor[2]; /* 028 */
247 USHORT IopmOffset; /* 030 */
249 * Presumably I/O privilege level to be used for this process, currently
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 */
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;
289 /* Microkernel specific process state. */
290 KPROCESS Pcb; /* 000 */
291 /* Exit status of the process. */
292 NTSTATUS ExitStatus; /* 068 */
294 KEVENT LockEvent; /* 06C */
296 ULONG LockCount; /* 07C */
297 /* Time of process creation. */
298 TIME CreateTime; /* 080 */
299 /* Time of process exit. */
300 TIME ExitTime; /* 088 */
302 PVOID LockOwner; /* 090 */
304 ULONG UniqueProcessId; /* 094 */
306 LIST_ENTRY ActiveProcessLinks; /* 098 */
308 ULONG QuotaPeakPoolUsage[2]; /* 0A0 */
310 ULONG QuotaPoolUsage[2]; /* 0A8 */
312 ULONG PagefileUsage; /* 0B0 */
314 ULONG CommitCharge; /* 0B4 */
316 ULONG PeakPagefileUsage; /* 0B8 */
318 ULONG PeakVirtualSize; /* 0BC */
320 LARGE_INTEGER VirtualSize; /* 0C0 */
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;
337 PVOID LastProtoPteFault;
338 struct _EPORT* DebugPort;
339 struct _EPORT* ExceptionPort;
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;
353 UCHAR ForkWasSuccessful;
354 UCHAR MmAgressiveWsTrimMask;
355 PKEVENT VmOperationEvent;
356 PVOID PageDirectoryPte;
357 ULONG LastFaultCount;
361 ULONG NumberOfPrivatePages;
362 ULONG NumberOfLockedPages;
363 USHORT NextProcessColour;
364 UCHAR ExitProcessCalled;
365 UCHAR CreateProcessReported;
366 HANDLE SectionHandle;
368 PVOID SectionBaseAddress;
370 NTSTATUS LastThreadExitStatus;
371 PVOID WorkingSetWatch;
372 HANDLE InheritedFromUniqueProcessId;
373 ACCESS_MASK GrantedAccess;
374 ULONG DefaultHardErrorProcessing;
375 PVOID LdtInformation;
378 KMUTANT ProcessMutant;
379 CHAR ImageFileName[16];
380 ULONG VmTrimFaultValue;
381 UCHAR SetTimerResolution;
383 UCHAR SubSystemMinorVersion;
384 UCHAR SubSystemMajorVersion;
385 USHORT SubSystemVersion;
386 struct _W32PROCESS* Win32Process;
387 HANDLE Win32WindowStation;
390 * Added by David Welch (welch@mcmail.com)
393 MADDRESS_SPACE AddressSpace;
394 HANDLE_TABLE HandleTable;
395 LIST_ENTRY ProcessListEntry;
398 * Added by Philip Susi for list of threads in process
400 LIST_ENTRY ThreadListHead;
403 #define PROCESS_STATE_TERMINATED (1)
404 #define PROCESS_STATE_ACTIVE (2)
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);
424 PsInitializeThread(HANDLE ProcessHandle,
426 PHANDLE ThreadHandle,
427 ACCESS_MASK DesiredAccess,
428 POBJECT_ATTRIBUTES ObjectAttributes,
431 PACCESS_TOKEN PsReferenceEffectiveToken(PETHREAD Thread,
432 PTOKEN_TYPE TokenType,
434 PSECURITY_IMPERSONATION_LEVEL Level);
436 NTSTATUS PsOpenTokenOfProcess(HANDLE ProcessHandle,
437 PACCESS_TOKEN* Token);
439 NTSTATUS PsSuspendThread(PETHREAD Thread, PULONG PreviousCount);
440 NTSTATUS PsResumeThread(PETHREAD Thread, PULONG PreviousCount);
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)
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
459 #define PROCESS_PRIO_IDLE 3
460 #define PROCESS_PRIO_NORMAL 8
461 #define PROCESS_PRIO_HIGH 13
462 #define PROCESS_PRIO_RT 18
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);
476 PsBlockThread(PNTSTATUS Status, UCHAR Alertable, ULONG WaitMode,
477 BOOLEAN DispatcherLock, KIRQL WaitIrql, UCHAR WaitReason);
479 PsUnblockThread(PETHREAD Thread, PNTSTATUS WaitStatus);
481 PsApplicationProcessorInit(VOID);
483 PsPrepareForApplicationProcessorInit(ULONG Id);
485 PsIdleThreadMain(PVOID Context);
488 PiSuspendThreadRundownRoutine(PKAPC Apc);
490 PiSuspendThreadKernelRoutine(PKAPC Apc,
491 PKNORMAL_ROUTINE* NormalRoutine,
492 PVOID* NormalContext,
493 PVOID* SystemArgument1,
494 PVOID* SystemArguemnt2);
496 PiSuspendThreadNormalRoutine(PVOID NormalContext,
497 PVOID SystemArgument1,
498 PVOID SystemArgument2);
500 PsDispatchThread(ULONG NewThreadStatus);
502 PsInitialiseSuspendImplementation(VOID);
504 extern ULONG PiNrThreadsAwaitingReaping;
506 #endif /* ASSEMBLER */
508 #endif /* __INCLUDE_INTERNAL_PS_H */