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.71 2004/10/13 01:42:14 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. */
40 #include <internal/arch/ps.h>
44 #include <internal/mm.h>
47 #ifndef KeGetCurrentProcessorNumber
48 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
51 extern HANDLE SystemProcessHandle
;
53 extern LCID PsDefaultThreadLocaleId
;
54 extern LCID PsDefaultSystemLocaleId
;
60 typedef struct _KAPC_STATE
62 LIST_ENTRY ApcListHead
[2];
63 struct _KPROCESS
* Process
;
64 UCHAR KernelApcInProgress
;
65 UCHAR KernelApcPending
;
68 } KAPC_STATE
, *PKAPC_STATE
, *__restrict PRKAPC_STATE
;
72 #endif /* __USE_W32API */
76 typedef struct _KTHREAD
78 /* For waiting on thread exit */
79 DISPATCHER_HEADER DispatcherHeader
; /* 00 */
81 /* List of mutants owned by the thread */
82 LIST_ENTRY MutantListHead
; /* 10 */
83 PVOID InitialStack
; /* 18 */
84 ULONG StackLimit
; /* 1C */
86 /* Pointer to the thread's environment block in user memory */
89 /* Pointer to the thread's TLS array */
90 PVOID TlsArray
; /* 24 */
91 PVOID KernelStack
; /* 28 */
92 UCHAR DebugActive
; /* 2C */
94 /* Thread state (one of THREAD_STATE_xxx constants below) */
96 UCHAR Alerted
[2]; /* 2E */
98 UCHAR NpxState
; /* 31 */
99 CHAR Saturation
; /* 32 */
100 CHAR Priority
; /* 33 */
101 KAPC_STATE ApcState
; /* 34 */
102 ULONG ContextSwitches
; /* 4C */
103 LONG WaitStatus
; /* 50 */
104 KIRQL WaitIrql
; /* 54 */
105 CHAR WaitMode
; /* 55 */
106 UCHAR WaitNext
; /* 56 */
107 UCHAR WaitReason
; /* 57 */
108 PKWAIT_BLOCK WaitBlockList
; /* 58 */
109 LIST_ENTRY WaitListEntry
; /* 5C */
110 ULONG WaitTime
; /* 64 */
111 CHAR BasePriority
; /* 68 */
112 UCHAR DecrementCount
; /* 69 */
113 UCHAR PriorityDecrement
; /* 6A */
114 UCHAR Quantum
; /* 6B */
115 KWAIT_BLOCK WaitBlock
[4]; /* 6C */
116 PVOID LegoData
; /* CC */
117 ULONG KernelApcDisable
; /* D0 */
118 KAFFINITY UserAffinity
; /* D4 */
119 UCHAR SystemAffinityActive
;/* D8 */
120 UCHAR PowerState
; /* D9 */
121 UCHAR NpxIrql
; /* DA */
122 UCHAR Pad
[1]; /* DB */
123 SSDT_ENTRY
*ServiceTable
; /* DC */
124 PKQUEUE Queue
; /* E0 */
125 KSPIN_LOCK ApcQueueLock
; /* E4 */
126 KTIMER Timer
; /* E8 */
127 LIST_ENTRY QueueListEntry
; /* 110 */
128 KAFFINITY Affinity
; /* 118 */
129 UCHAR Preempted
; /* 11C */
130 UCHAR ProcessReadyQueue
; /* 11D */
131 UCHAR KernelStackResident
; /* 11E */
132 UCHAR NextProcessor
; /* 11F */
133 PVOID CallbackStack
; /* 120 */
134 struct _W32THREAD
*Win32Thread
; /* 124 */
135 struct _KTRAP_FRAME
*TrapFrame
; /* 128 */
136 PKAPC_STATE ApcStatePointer
[2]; /* 12C */
137 UCHAR EnableStackSwap
; /* 134 */
138 UCHAR LargeStack
; /* 135 */
139 UCHAR ResourceIndex
; /* 136 */
140 UCHAR PreviousMode
; /* 137 */
141 ULONG KernelTime
; /* 138 */
142 ULONG UserTime
; /* 13C */
143 KAPC_STATE SavedApcState
; /* 140 */
144 UCHAR Alertable
; /* 158 */
145 UCHAR ApcStateIndex
; /* 159 */
146 UCHAR ApcQueueable
; /* 15A */
147 UCHAR AutoAlignment
; /* 15B */
148 PVOID StackBase
; /* 15C */
149 KAPC SuspendApc
; /* 160 */
150 KSEMAPHORE SuspendSemaphore
; /* 190 */
151 LIST_ENTRY ThreadListEntry
; /* 1A4 */
152 CHAR FreezeCount
; /* 1AC */
153 UCHAR SuspendCount
; /* 1AD */
154 UCHAR IdealProcessor
; /* 1AE */
155 UCHAR DisableBoost
; /* 1AF */
160 /* Top level irp definitions. */
161 #define FSRTL_FSP_TOP_LEVEL_IRP (0x01)
162 #define FSRTL_CACHE_TOP_LEVEL_IRP (0x02)
163 #define FSRTL_MOD_WRITE_TOP_LEVEL_IRP (0x03)
164 #define FSRTL_FAST_IO_TOP_LEVEL_IRP (0x04)
165 #define FSRTL_MAX_TOP_LEVEL_IRP_FLAG (0x04)
172 BOOLEAN EffectiveOnly
;
173 SECURITY_IMPERSONATION_LEVEL Level
;
174 } PS_IMPERSONATION_INFORMATION
, *PPS_IMPERSONATION_INFORMATION
;
177 #include <pshpack1.h>
179 typedef struct _ETHREAD
183 USHORT NestedFaultCount
;
186 LIST_ENTRY LpcReplyChain
;
189 LIST_ENTRY PostBlockList
;
190 LIST_ENTRY TerminationPortList
;
191 KSPIN_LOCK ActiveTimerListLock
;
192 LIST_ENTRY ActiveTimerListHead
;
194 KSEMAPHORE LpcReplySemaphore
;
195 PVOID LpcReplyMessage
;
196 ULONG LpcReplyMessageId
;
197 ULONG PerformanceCountLow
;
198 PPS_IMPERSONATION_INFORMATION ImpersonationInfo
;
201 PDEVICE_OBJECT DeviceToVerify
;
202 ULONG ReadClusterSize
;
203 UCHAR ForwardClusterOnly
;
204 UCHAR DisablePageFaultClustering
;
206 UCHAR HideFromDebugger
;
208 #ifdef _ENABLE_THRDEVTPAIR
210 #endif /* _ENABLE_THRDEVTPAIR */
211 ACCESS_MASK GrantedAccess
;
212 struct _EPROCESS
*ThreadsProcess
;
213 PKSTART_ROUTINE StartAddress
;
214 LPTHREAD_START_ROUTINE Win32StartAddress
;
215 ULONG LpcReceivedMessageId
;
216 UCHAR LpcExitThreadCalled
;
217 UCHAR HardErrorsAreDisabled
;
218 UCHAR LpcReceivedMsgIdValid
;
219 UCHAR ActiveImpersonationInfo
;
220 ULONG PerformanceCountHigh
;
221 LIST_ENTRY ThreadListEntry
;
229 typedef struct _ETHREAD
*PETHREAD
;
231 #endif /* __USE_W32API */
234 typedef struct _KPROCESS
236 /* So it's possible to wait for the process to terminate */
237 DISPATCHER_HEADER DispatcherHeader
; /* 000 */
239 * Presumably a list of profile objects associated with this process,
242 LIST_ENTRY ProfileListHead
; /* 010 */
244 * We use the first member of this array to hold the physical address of
245 * the page directory for this process.
247 PHYSICAL_ADDRESS DirectoryTableBase
; /* 018 */
249 * Presumably a descriptor for the process's LDT, currently unused.
251 ULONG LdtDescriptor
[2]; /* 020 */
253 * Virtual Dos Machine flag.
255 ULONG NtVdmFlag
; /* 028 */
256 ULONG VdmUnused
; /* 02C */
257 /* Is the i/o permission map enabled for the process. */
258 USHORT IopmOffset
; /* 030 */
260 * Presumably I/O privilege level to be used for this process, currently
263 UCHAR Iopl
; /* 032 */
264 /* Set if this process is a virtual dos machine? */
265 UCHAR VdmFlag
; /* 033 */
266 /* Bitmask of the processors being used by this process's threads? */
267 ULONG ActiveProcessors
; /* 034 */
268 /* Aggregate of the time this process's threads have spent in kernel mode? */
269 ULONG KernelTime
; /* 038 */
270 /* Aggregate of the time this process's threads have spent in user mode? */
271 ULONG UserTime
; /* 03C */
272 /* List of this process's threads that are ready for execution? */
273 LIST_ENTRY ReadyListHead
; /* 040 */
274 /* List of this process's threads that have their stacks swapped out? */
275 LIST_ENTRY SwapListEntry
; /* 048 */
276 /* List of this process's threads? */
277 LIST_ENTRY ThreadListHead
; /* 050 */
278 /* Maybe a lock for this data structure, the type is assumed. */
279 KSPIN_LOCK ProcessLock
; /* 058 */
280 /* Default affinity mask for this process's threads? */
281 ULONG Affinity
; /* 05C */
282 /* Count of the stacks allocated for this process's threads? */
283 USHORT StackCount
; /* 060 */
284 /* Base priority for this process's threads? */
285 KPRIORITY BasePriority
; /* 062 */
286 /* Default quantum for this process's threads */
287 UCHAR ThreadQuantum
; /* 063 */
289 UCHAR AutoAlignment
; /* 064 */
290 /* Process execution state, currently either active or terminated. */
291 UCHAR State
; /* 065 */
292 /* Seed for generating thread ids for this process's threads? */
293 UCHAR ThreadSeed
; /* 066 */
294 /* Disable priority boosts? */
295 UCHAR DisableBoost
; /* 067 */
300 typedef struct _KPROCESS
*PKPROCESS
;
302 #endif /* __USE_W32API */
306 /* Microkernel specific process state. */
307 KPROCESS Pcb
; /* 000 */
308 /* Exit status of the process. */
309 NTSTATUS ExitStatus
; /* 068 */
311 KEVENT LockEvent
; /* 06C */
313 ULONG LockCount
; /* 07C */
315 /* Time of process creation. */
317 LARGE_INTEGER CreateTime
; /* 080 */
319 TIME CreateTime
; /* 080 */
322 /* Time of process exit. */
323 TIME ExitTime
; /* 088 */
325 PVOID LockOwner
; /* 090 */
327 ULONG UniqueProcessId
; /* 094 */
329 LIST_ENTRY ActiveProcessLinks
; /* 098 */
331 ULONG QuotaPeakPoolUsage
[2]; /* 0A0 */
333 ULONG QuotaPoolUsage
[2]; /* 0A8 */
335 ULONG PagefileUsage
; /* 0B0 */
337 ULONG CommitCharge
; /* 0B4 */
339 ULONG PeakPagefileUsage
; /* 0B8 */
341 ULONG PeakVirtualSize
; /* 0BC */
343 LARGE_INTEGER VirtualSize
; /* 0C0 */
347 ULONG LastTrimFaultCount
;
348 ULONG PageFaultCount
;
349 ULONG PeakWorkingSetSize
;
350 ULONG WorkingSetSize
;
351 ULONG MinimumWorkingSetSize
;
352 ULONG MaximumWorkingSetSize
;
353 ULONG VmWorkingSetList
;
354 LIST_ENTRY WorkingSetExpansionList
;
355 UCHAR AllowWorkingSetAdjustment
;
356 UCHAR AddressSpaceBeingDeleted
;
357 UCHAR ForegroundPrioritySwitch
;
358 UCHAR MemoryPriority
;
360 PVOID LastProtoPteFault
;
361 struct _EPORT
* DebugPort
;
362 struct _EPORT
* ExceptionPort
;
365 /* FAST_MUTEX WorkingSetLock; */
366 KMUTEX WorkingSetLock
;
367 PVOID WorkingSetPage
;
368 UCHAR ProcessOutswapEnabled
;
369 UCHAR ProcessOutswapped
;
370 UCHAR AddressSpaceInitialized
;
371 UCHAR AddressSpaceDeleted
;
372 FAST_MUTEX AddressCreationLock
;
373 KSPIN_LOCK HyperSpaceLock
;
374 PETHREAD ForkInProgress
;
376 UCHAR ForkWasSuccessful
;
377 UCHAR MmAgressiveWsTrimMask
;
378 PKEVENT VmOperationEvent
;
379 PVOID PageDirectoryPte
;
380 ULONG LastFaultCount
;
384 ULONG NumberOfPrivatePages
;
385 ULONG NumberOfLockedPages
;
386 USHORT NextProcessColour
;
387 UCHAR ExitProcessCalled
;
388 UCHAR CreateProcessReported
;
389 HANDLE SectionHandle
;
391 PVOID SectionBaseAddress
;
392 PEPROCESS_QUOTA_BLOCK QuotaBlock
;
393 NTSTATUS LastThreadExitStatus
;
394 PVOID WorkingSetWatch
;
395 HANDLE InheritedFromUniqueProcessId
;
396 ACCESS_MASK GrantedAccess
;
397 ULONG DefaultHardErrorProcessing
;
398 PVOID LdtInformation
;
401 KMUTANT ProcessMutant
;
402 CHAR ImageFileName
[16];
403 ULONG VmTrimFaultValue
;
404 UCHAR SetTimerResolution
;
406 UCHAR SubSystemMinorVersion
;
407 UCHAR SubSystemMajorVersion
;
408 USHORT SubSystemVersion
;
409 struct _W32PROCESS
* Win32Process
;
410 HANDLE Win32WindowStation
;
413 * Added by David Welch (welch@mcmail.com)
416 MADDRESS_SPACE AddressSpace
;
417 HANDLE_TABLE HandleTable
;
418 LIST_ENTRY ProcessListEntry
;
421 * Added by Philip Susi for list of threads in process
423 LIST_ENTRY ThreadListHead
;
425 /* Added by Alex Ionescu (alex@relsoft.net)*/
427 struct _EPORT
* SecurityPort
;
434 PVOID TebLastAllocated
;
437 #define PROCESS_STATE_TERMINATED (1)
438 #define PROCESS_STATE_ACTIVE (2)
440 VOID
PiInitDefaultLocale(VOID
);
441 VOID
PiInitProcessManager(VOID
);
442 VOID
PiShutdownProcessManager(VOID
);
443 VOID
PsInitThreadManagment(VOID
);
444 VOID
PsInitProcessManagment(VOID
);
445 VOID
PsInitIdleThread(VOID
);
446 VOID
PsDispatchThreadNoLock(ULONG NewThreadStatus
);
447 VOID
PiTerminateProcessThreads(PEPROCESS Process
, NTSTATUS ExitStatus
);
448 VOID
PsTerminateCurrentThread(NTSTATUS ExitStatus
);
449 VOID
PsTerminateOtherThread(PETHREAD Thread
, NTSTATUS ExitStatus
);
450 VOID
PsReleaseThread(PETHREAD Thread
);
451 VOID
PsBeginThread(PKSTART_ROUTINE StartRoutine
, PVOID StartContext
);
452 VOID
PsBeginThreadWithContextInternal(VOID
);
453 VOID
PiKillMostProcesses(VOID
);
454 NTSTATUS STDCALL
PiTerminateProcess(PEPROCESS Process
, NTSTATUS ExitStatus
);
455 VOID
PiInitApcManagement(VOID
);
456 VOID STDCALL
PiDeleteThread(PVOID ObjectBody
);
457 VOID
PsReapThreads(VOID
);
458 VOID
PsInitializeThreadReaper(VOID
);
459 VOID
PsQueueThreadReap(PETHREAD Thread
);
461 PsInitializeThread(PEPROCESS Process
,
463 PHANDLE ThreadHandle
,
464 ACCESS_MASK DesiredAccess
,
465 POBJECT_ATTRIBUTES ObjectAttributes
,
468 PACCESS_TOKEN
PsReferenceEffectiveToken(PETHREAD Thread
,
469 PTOKEN_TYPE TokenType
,
471 PSECURITY_IMPERSONATION_LEVEL Level
);
473 NTSTATUS
PsOpenTokenOfProcess(HANDLE ProcessHandle
,
474 PACCESS_TOKEN
* Token
);
476 NTSTATUS
PsSuspendThread(PETHREAD Thread
, PULONG PreviousCount
);
477 NTSTATUS
PsResumeThread(PETHREAD Thread
, PULONG PreviousCount
);
480 #define THREAD_STATE_INITIALIZED (0)
481 #define THREAD_STATE_READY (1)
482 #define THREAD_STATE_RUNNING (2)
483 #define THREAD_STATE_SUSPENDED (3)
484 #define THREAD_STATE_FROZEN (4)
485 #define THREAD_STATE_TERMINATED_1 (5)
486 #define THREAD_STATE_TERMINATED_2 (6)
487 #define THREAD_STATE_BLOCKED (7)
488 #define THREAD_STATE_MAX (8)
492 * Internal thread priorities, added by Phillip Susi
493 * TODO: rebalence these to make use of all priorities... the ones above 16
494 * can not all be used right now
496 #define PROCESS_PRIO_IDLE 3
497 #define PROCESS_PRIO_NORMAL 8
498 #define PROCESS_PRIO_HIGH 13
499 #define PROCESS_PRIO_RT 18
503 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
);
504 NTSTATUS
KeReleaseThread(PKTHREAD Thread
);
508 KeStackAttachProcess (
509 IN PKPROCESS Process
,
510 OUT PKAPC_STATE ApcState
515 KeUnstackDetachProcess (
516 IN PKAPC_STATE ApcState
519 VOID STDCALL
PiDeleteProcess(PVOID ObjectBody
);
520 VOID
PsReapThreads(VOID
);
521 VOID
PsInitializeThreadReaper(VOID
);
522 VOID
PsQueueThreadReap(PETHREAD Thread
);
523 VOID
PsUnfreezeOtherThread(PETHREAD Thread
);
524 VOID
PsFreezeOtherThread(PETHREAD Thread
);
525 VOID
PsFreezeProcessThreads(PEPROCESS Process
);
526 VOID
PsUnfreezeProcessThreads(PEPROCESS Process
);
527 ULONG
PsEnumThreadsByProcess(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 LONG PiNrThreadsAwaitingReaping
;
561 PsInitWin32Thread (PETHREAD Thread
);
564 PsTerminateWin32Process (PEPROCESS Process
);
567 PsTerminateWin32Thread (PETHREAD Thread
);
570 PsInitialiseW32Call(VOID
);
574 PspRunCreateThreadNotifyRoutines(PETHREAD
, BOOLEAN
);
578 PspRunCreateProcessNotifyRoutines(PEPROCESS
, BOOLEAN
);
580 #include <pshpack1.h>
581 typedef struct _PS_JOB_TOKEN_FILTER
583 UINT CapturedSidCount
;
584 PSID_AND_ATTRIBUTES CapturedSids
;
585 UINT CapturedSidsLength
;
586 UINT CapturedGroupCount
;
587 PSID_AND_ATTRIBUTES CapturedGroups
;
588 UINT CapturedGroupsLength
;
589 UINT CapturedPrivilegeCount
;
590 PLUID_AND_ATTRIBUTES CapturedPrivileges
;
591 UINT CapturedPrivilegesLength
;
592 } PS_JOB_TOKEN_FILTER
, *PPS_JOB_TOKEN_FILTER
;
595 #include <pshpack1.h>
600 LIST_ENTRY ProcessListHead
;
602 LARGE_INTEGER TotalUserTime
;
603 LARGE_INTEGER TotalKernelTime
;
604 LARGE_INTEGER ThisPeriodTotalUserTime
;
605 LARGE_INTEGER ThisPeriodTotalKernelTime
;
606 UINT TotalPageFaultCount
;
608 UINT ActiveProcesses
;
609 UINT TotalTerminatedProcesses
;
610 LARGE_INTEGER PerProcessUserTimeLimit
;
611 LARGE_INTEGER PerJobUserTimeLimit
;
613 UINT MinimumWorkingSetSize
;
614 UINT MaximumWorkingSetSize
;
615 UINT ActiveProcessLimit
;
618 UINT UIRestrictionsClass
;
619 UINT SecurityLimitFlags
;
621 PPS_JOB_TOKEN_FILTER Filter
;
622 UINT EndOfJobTimeAction
;
623 PVOID CompletionPort
;
626 UINT SchedulingClass
;
627 ULONGLONG ReadOperationCount
;
628 ULONGLONG WriteOperationCount
;
629 ULONGLONG OtherOperationCount
;
630 ULONGLONG ReadTransferCount
;
631 ULONGLONG WriteTransferCount
;
632 ULONGLONG OtherTransferCount
;
634 UINT ProcessMemoryLimit
;
636 UINT PeakProcessMemoryUsed
;
637 UINT PeakJobMemoryUsed
;
638 UINT CurrentJobMemoryUsed
;
639 FAST_MUTEX MemoryLimitsLock
;
643 VOID INIT_FUNCTION
PsInitJobManagment(VOID
);
647 typedef struct _CID_OBJECT
655 struct _EPROCESS
*Process
;
656 struct _ETHREAD
*Thread
;
659 } CID_OBJECT
, *PCID_OBJECT
;
661 NTSTATUS
PsCreateCidHandle(PVOID Object
, POBJECT_TYPE ObjectType
, PHANDLE Handle
);
662 NTSTATUS
PsDeleteCidHandle(HANDLE CidHandle
, POBJECT_TYPE ObjectType
);
663 PCID_OBJECT
PsLockCidHandle(HANDLE CidHandle
, POBJECT_TYPE ObjectType
);
664 VOID
PsUnlockCidObject(PCID_OBJECT CidObject
);
666 #define ETHREAD_TO_KTHREAD(pEThread) (&(pEThread)->Tcb)
667 #define KTHREAD_TO_ETHREAD(pKThread) (CONTAINING_RECORD((pKThread), ETHREAD, Tcb))
668 #define EPROCESS_TO_KPROCESS(pEProcess) (&(pEProcess)->Pcb)
669 #define KPROCESS_TO_EPROCESS(pKProcess) (CONTAINING_RECORD((pKProcess), EPROCESS, Pcb))
671 #endif /* ASSEMBLER */
673 #endif /* __INCLUDE_INTERNAL_PS_H */