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.
19 /* $Id: ps.h,v 1.59 2004/06/23 21:02:16 ion Exp $
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 #ifndef KeGetCurrentProcessorNumber
47 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
50 extern HANDLE SystemProcessHandle
;
52 extern LCID PsDefaultThreadLocaleId
;
53 extern LCID PsDefaultSystemLocaleId
;
59 typedef struct _KAPC_STATE
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
;
70 #endif /* __USE_W32API */
74 typedef struct _KTHREAD
76 /* For waiting on thread exit */
77 DISPATCHER_HEADER DispatcherHeader
; /* 00 */
79 /* List of mutants owned by the thread */
80 LIST_ENTRY MutantListHead
; /* 10 */
81 PVOID InitialStack
; /* 18 */
82 ULONG StackLimit
; /* 1C */
84 /* Pointer to the thread's environment block in user memory */
87 /* Pointer to the thread's TLS array */
88 PVOID TlsArray
; /* 24 */
89 PVOID KernelStack
; /* 28 */
90 UCHAR DebugActive
; /* 2C */
92 /* Thread state (one of THREAD_STATE_xxx constants below) */
94 UCHAR Alerted
[2]; /* 2E */
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 */
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 */
156 * Below here are thread structure members that are specific to ReactOS
159 /* Added by Phillip Susi for list of threads in a process */
160 LIST_ENTRY ProcessThreadListEntry
; /* 1B0 */
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)
172 typedef struct _TOP_LEVEL_IRP
175 ULONG TopLevelIrpConst
;
180 PACCESS_TOKEN Token
; // 0x0
181 BOOLEAN CopyOnOpen
; // 0x4
182 BOOLEAN EffectiveOnly
; // 0x5
184 SECURITY_IMPERSONATION_LEVEL Level
; // 0x8
185 } PS_IMPERSONATION_INFO
, *PPS_IMPERSONATION_INFO
;
187 #include <pshpack1.h>
189 typedef struct _ETHREAD
191 KTHREAD Tcb
; /* 000 */
192 TIME CreateTime
; /* 1B0/1B8 */
195 TIME ExitTime
; /* 1B8/1E4 */
196 LIST_ENTRY LpcReplyChain
; /* 1B8/1E4 */
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 */
223 LPTHREAD_START_ROUTINE Win32StartAddress
; /* 234/260 */
224 ULONG LpcReceiveMessageId
; /* 234/260 */
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 */
233 * Added by David Welch (welch@cwcom.net)
235 struct _EPROCESS
* OldProcess
; /* 240/26C */
237 struct _W32THREAD
* Win32Thread
;
246 typedef struct _ETHREAD
*PETHREAD
;
248 #endif /* __USE_W32API */
251 typedef struct _KPROCESS
253 /* So it's possible to wait for the process to terminate */
254 DISPATCHER_HEADER DispatcherHeader
; /* 000 */
256 * Presumably a list of profile objects associated with this process,
259 LIST_ENTRY ProfileListHead
; /* 010 */
261 * We use the first member of this array to hold the physical address of
262 * the page directory for this process.
264 PHYSICAL_ADDRESS DirectoryTableBase
; /* 018 */
266 * Presumably a descriptor for the process's LDT, currently unused.
268 ULONG LdtDescriptor
[2]; /* 020 */
270 * Virtual Dos Machine flag.
272 ULONG NtVdmFlag
; /* 028 */
273 ULONG VdmUnused
; /* 02C */
274 /* Is the i/o permission map enabled for the process. */
275 USHORT IopmOffset
; /* 030 */
277 * Presumably I/O privilege level to be used for this process, currently
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 */
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 */
317 typedef struct _KPROCESS
*PKPROCESS
;
319 #endif /* __USE_W32API */
323 /* Microkernel specific process state. */
324 KPROCESS Pcb
; /* 000 */
325 /* Exit status of the process. */
326 NTSTATUS ExitStatus
; /* 068 */
328 KEVENT LockEvent
; /* 06C */
330 ULONG LockCount
; /* 07C */
332 /* Time of process creation. */
334 LARGE_INTEGER CreateTime
; /* 080 */
336 TIME CreateTime
; /* 080 */
339 /* Time of process exit. */
340 TIME ExitTime
; /* 088 */
342 PVOID LockOwner
; /* 090 */
344 ULONG UniqueProcessId
; /* 094 */
346 LIST_ENTRY ActiveProcessLinks
; /* 098 */
348 ULONG QuotaPeakPoolUsage
[2]; /* 0A0 */
350 ULONG QuotaPoolUsage
[2]; /* 0A8 */
352 ULONG PagefileUsage
; /* 0B0 */
354 ULONG CommitCharge
; /* 0B4 */
356 ULONG PeakPagefileUsage
; /* 0B8 */
358 ULONG PeakVirtualSize
; /* 0BC */
360 LARGE_INTEGER VirtualSize
; /* 0C0 */
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
;
377 PVOID LastProtoPteFault
;
378 struct _EPORT
* DebugPort
;
379 struct _EPORT
* ExceptionPort
;
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
;
393 UCHAR ForkWasSuccessful
;
394 UCHAR MmAgressiveWsTrimMask
;
395 PKEVENT VmOperationEvent
;
396 PVOID PageDirectoryPte
;
397 ULONG LastFaultCount
;
401 ULONG NumberOfPrivatePages
;
402 ULONG NumberOfLockedPages
;
403 USHORT NextProcessColour
;
404 UCHAR ExitProcessCalled
;
405 UCHAR CreateProcessReported
;
406 HANDLE SectionHandle
;
408 PVOID SectionBaseAddress
;
410 NTSTATUS LastThreadExitStatus
;
411 PVOID WorkingSetWatch
;
412 HANDLE InheritedFromUniqueProcessId
;
413 ACCESS_MASK GrantedAccess
;
414 ULONG DefaultHardErrorProcessing
;
415 PVOID LdtInformation
;
418 KMUTANT ProcessMutant
;
419 CHAR ImageFileName
[16];
420 ULONG VmTrimFaultValue
;
421 UCHAR SetTimerResolution
;
423 UCHAR SubSystemMinorVersion
;
424 UCHAR SubSystemMajorVersion
;
425 USHORT SubSystemVersion
;
426 struct _W32PROCESS
* Win32Process
;
427 HANDLE Win32WindowStation
;
430 * Added by David Welch (welch@mcmail.com)
433 MADDRESS_SPACE AddressSpace
;
434 HANDLE_TABLE HandleTable
;
435 LIST_ENTRY ProcessListEntry
;
438 * Added by Philip Susi for list of threads in process
440 LIST_ENTRY ThreadListHead
;
443 #define PROCESS_STATE_TERMINATED (1)
444 #define PROCESS_STATE_ACTIVE (2)
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
);
464 PsInitializeThread(HANDLE ProcessHandle
,
466 PHANDLE ThreadHandle
,
467 ACCESS_MASK DesiredAccess
,
468 POBJECT_ATTRIBUTES ObjectAttributes
,
471 PACCESS_TOKEN
PsReferenceEffectiveToken(PETHREAD Thread
,
472 PTOKEN_TYPE TokenType
,
474 PSECURITY_IMPERSONATION_LEVEL Level
);
476 NTSTATUS
PsOpenTokenOfProcess(HANDLE ProcessHandle
,
477 PACCESS_TOKEN
* Token
);
479 NTSTATUS
PsSuspendThread(PETHREAD Thread
, PULONG PreviousCount
);
480 NTSTATUS
PsResumeThread(PETHREAD Thread
, PULONG PreviousCount
);
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)
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
499 #define PROCESS_PRIO_IDLE 3
500 #define PROCESS_PRIO_NORMAL 8
501 #define PROCESS_PRIO_HIGH 13
502 #define PROCESS_PRIO_RT 18
506 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
);
507 NTSTATUS
KeReleaseThread(PETHREAD Thread
);
511 KeStackAttachProcess (
512 IN PKPROCESS Process
,
513 OUT PRKAPC_STATE ApcState
518 KeUnstackDetachProcess (
519 IN PRKAPC_STATE ApcState
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
);
530 PsBlockThread(PNTSTATUS Status
, UCHAR Alertable
, ULONG WaitMode
,
531 BOOLEAN DispatcherLock
, KIRQL WaitIrql
, UCHAR WaitReason
);
533 PsUnblockThread(PETHREAD Thread
, PNTSTATUS WaitStatus
);
535 PsApplicationProcessorInit(VOID
);
537 PsPrepareForApplicationProcessorInit(ULONG Id
);
539 PsIdleThreadMain(PVOID Context
);
542 PiSuspendThreadRundownRoutine(PKAPC Apc
);
544 PiSuspendThreadKernelRoutine(PKAPC Apc
,
545 PKNORMAL_ROUTINE
* NormalRoutine
,
546 PVOID
* NormalContext
,
547 PVOID
* SystemArgument1
,
548 PVOID
* SystemArguemnt2
);
550 PiSuspendThreadNormalRoutine(PVOID NormalContext
,
551 PVOID SystemArgument1
,
552 PVOID SystemArgument2
);
554 PsDispatchThread(ULONG NewThreadStatus
);
556 PsInitialiseSuspendImplementation(VOID
);
558 extern ULONG PiNrThreadsAwaitingReaping
;
562 PsInitWin32Thread (PETHREAD Thread
);
565 PsTerminateWin32Process (PEPROCESS Process
);
568 PsTerminateWin32Thread (PETHREAD Thread
);
571 PsInitialiseW32Call(VOID
);
575 PspRunCreateThreadNotifyRoutines(PETHREAD
, BOOLEAN
);
579 PspRunCreateProcessNotifyRoutines(PEPROCESS
, BOOLEAN
);
581 #endif /* ASSEMBLER */
583 #endif /* __INCLUDE_INTERNAL_PS_H */