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.35 2002/07/04 19:56:35 dwelch 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
32 * Defines for accessing KPCR and KTHREAD structure members
34 #define KTHREAD_INITIAL_STACK 0x18
35 #define KTHREAD_STACK_LIMIT 0x1C
36 #define KTHREAD_TEB 0x20
37 #define KTHREAD_KERNEL_STACK 0x28
38 #define KTHREAD_PREVIOUS_MODE 0x137
39 #define KTHREAD_TRAP_FRAME 0x128
40 #define KTHREAD_CALLBACK_STACK 0x120
42 #define ETHREAD_THREADS_PROCESS 0x258
44 #define KPROCESS_DIRECTORY_TABLE_BASE 0x18
46 #define KPCR_BASE 0xFF000000
48 #define KPCR_EXCEPTION_LIST 0x0
49 #define KPCR_SELF 0x18
51 #define KPCR_CURRENT_THREAD 0x124
55 #include <internal/mm.h>
61 /* FIXME: This does not work if we have more than 24 IRQs (ie. more than one I/O APIC) */
62 #define VECTOR2IRQ(vector) (((vector) - 0x31) / 8)
63 #define VECTOR2IRQL(vector) (4 + VECTOR2IRQ(vector))
66 * Processor Control Region
70 PVOID ExceptionList
; /* 00 */
71 PVOID StackBase
; /* 04 */
72 PVOID StackLimit
; /* 08 */
73 PVOID SubSystemTib
; /* 0C */
74 PVOID Reserved1
; /* 10 */
75 PVOID ArbitraryUserPointer
; /* 14 */
76 struct _KPCR
* Self
; /* 18 */
77 UCHAR ProcessorNumber
; /* 1C */
79 UCHAR Reserved2
[0x2]; /* 1E */
83 UCHAR Reserved3
[0xF8]; /* 2C */
84 struct _KTHREAD
* CurrentThread
; /* 124 */
85 } __attribute__((packed
)) KPCR
, *PKPCR
;
87 static inline PKPCR
KeGetCurrentKPCR(VOID
)
91 __asm__
__volatile__ ("movl %%fs:0x18, %0\n\t"
98 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
100 extern HANDLE SystemProcessHandle
;
102 extern LCID PsDefaultThreadLocaleId
;
103 extern LCID PsDefaultSystemLocaleId
;
106 typedef struct _KAPC_STATE
108 LIST_ENTRY ApcListHead
[2];
109 struct _KPROCESS
* Process
;
110 UCHAR KernelApcInProgress
;
111 UCHAR KernelApcPending
;
112 USHORT UserApcPending
;
113 } __attribute__((packed
)) KAPC_STATE
, *PKAPC_STATE
;
115 typedef struct _KTHREAD
117 /* For waiting on thread exit */
118 DISPATCHER_HEADER DispatcherHeader
; /* 00 */
120 /* List of mutants owned by the thread */
121 LIST_ENTRY MutantListHead
; /* 10 */
122 PVOID InitialStack
; /* 18 */
123 ULONG StackLimit
; /* 1C */
125 /* Pointer to the thread's environment block in user memory */
128 /* Pointer to the thread's TLS array */
129 PVOID TlsArray
; /* 24 */
130 PVOID KernelStack
; /* 28 */
131 UCHAR DebugActive
; /* 2C */
133 /* Thread state (one of THREAD_STATE_xxx constants below) */
134 UCHAR State
; /* 2D */
135 UCHAR Alerted
[2]; /* 2E */
137 UCHAR NpxState
; /* 31 */
138 UCHAR Saturation
; /* 32 */
139 CHAR Priority
; /* 33 */
140 KAPC_STATE ApcState
; /* 34 */
141 ULONG ContextSwitches
; /* 4C */
142 ULONG WaitStatus
; /* 50 */
143 KIRQL WaitIrql
; /* 54 */
144 UCHAR WaitMode
; /* 55 */
145 UCHAR WaitNext
; /* 56 */
146 UCHAR WaitReason
; /* 57 */
147 PKWAIT_BLOCK WaitBlockList
; /* 58 */
148 LIST_ENTRY WaitListEntry
; /* 5C */
149 ULONG WaitTime
; /* 64 */
150 CHAR BasePriority
; /* 68 */
151 UCHAR DecrementCount
; /* 69 */
152 UCHAR PriorityDecrement
; /* 6A */
153 UCHAR Quantum
; /* 6B */
154 KWAIT_BLOCK WaitBlock
[4]; /* 6C */
155 PVOID LegoData
; /* CC */
156 LONG KernelApcDisable
; /* D0 */
157 KAFFINITY UserAffinity
; /* D4 */
158 UCHAR SystemAffinityActive
;/* D8 */
159 UCHAR Pad
[7]; /* D9 */
160 PKQUEUE Queue
; /* E0 */
161 KSPIN_LOCK ApcQueueLock
; /* E4 */
162 KTIMER Timer
; /* E8 */
163 LIST_ENTRY QueueListEntry
; /* 110 */
164 KAFFINITY Affinity
; /* 118 */
165 UCHAR Preempted
; /* 11C */
166 UCHAR ProcessReadyQueue
; /* 11D */
167 UCHAR KernelStackResident
; /* 11E */
168 UCHAR NextProcessor
; /* 11F */
169 PVOID CallbackStack
; /* 120 */
170 BOOL Win32Thread
; /* 124 */
171 struct _KTRAP_FRAME
* TrapFrame
; /* 128 */
172 PVOID ApcStatePointer
[2]; /* 12C */
173 UCHAR EnableStackSwap
; /* 134 */
174 UCHAR LargeStack
; /* 135 */
175 UCHAR ResourceIndex
; /* 136 */
176 UCHAR PreviousMode
; /* 137 */
177 ULONG KernelTime
; /* 138 */
178 ULONG UserTime
; /* 13C */
179 KAPC_STATE SavedApcState
; /* 140 */
180 UCHAR Alertable
; /* 158 */
181 UCHAR ApcStateIndex
; /* 159 */
182 UCHAR ApcQueueable
; /* 15A */
183 UCHAR AutoAlignment
; /* 15B */
184 PVOID StackBase
; /* 15C */
185 KAPC SuspendApc
; /* 160 */
186 KSEMAPHORE SuspendSemaphore
; /* 190 */
187 LIST_ENTRY ThreadListEntry
; /* 1A4 */
188 CHAR FreezeCount
; /* 1AC */
189 UCHAR SuspendCount
; /* 1AD */
190 UCHAR IdealProcessor
; /* 1AE */
191 UCHAR DisableBoost
; /* 1AF */
194 * Below here are thread structure members that are specific to ReactOS
197 /* Added by Phillip Susi for list of threads in a process */
198 LIST_ENTRY ProcessThreadListEntry
; /* 1B0 */
200 /* Added by Phillip Susi for internal KeAddThreadTimeout() implementation */
201 KDPC TimerDpc
; /* 1B8 */
203 /* Record the last EIP value when the thread is suspended */
204 ULONG LastEip
; /* 1D8 */
205 } __attribute__((packed
)) KTHREAD
, *PKTHREAD
;
207 // According to documentation the stack should have a commited [ 1 page ] and
208 // a reserved part [ 1 M ] but can be specified otherwise in the image file.
216 // TopLevelIrp can be one of the following values:
217 // FIXME I belong somewhere else
219 #define FSRTL_FSP_TOP_LEVEL_IRP (0x01)
220 #define FSRTL_CACHE_TOP_LEVEL_IRP (0x02)
221 #define FSRTL_MOD_WRITE_TOP_LEVEL_IRP (0x03)
222 #define FSRTL_FAST_IO_TOP_LEVEL_IRP (0x04)
223 #define FSRTL_MAX_TOP_LEVEL_IRP_FLAG (0x04)
225 typedef struct _TOP_LEVEL_IRP
228 ULONG TopLevelIrpConst
;
233 PACCESS_TOKEN Token
; // 0x0
234 UCHAR Unknown1
; // 0x4
235 UCHAR Unknown2
; // 0x5
237 SECURITY_IMPERSONATION_LEVEL Level
; // 0x8
238 } PS_IMPERSONATION_INFO
, *PPS_IMPERSONATION_INFO
;
241 typedef struct _ETHREAD
243 KTHREAD Tcb
; /* 000 */
244 TIME CreateTime
; /* 1B0/1DC */
247 TIME ExitTime
; /* 1B8/1E4 */
248 LIST_ENTRY LpcReplyChain
; /* 1B8/1E4 */
250 NTSTATUS ExitStatus
; /* 1C0/1EC */
251 LIST_ENTRY PostBlockList
; /* 1C4/1F0 */
252 LIST_ENTRY TerminationPortList
; /* 1CC/1F8 */
253 KSPIN_LOCK ActiveTimerListLock
; /* 1D4/200 */
254 LIST_ENTRY ActiveTimerListHead
; /* 1D8/204 */
255 CLIENT_ID Cid
; /* 1E0/20C */
256 KSEMAPHORE LpcReplySemaphore
; /* 1E8/214 */
257 PVOID LpcReplyMessage
; /* 1FC/228 */
258 PLARGE_INTEGER LpcReplyMessageId
; /* 200/22C */
259 ULONG PerformanceCounterLow
; /* 204/230 */
260 PPS_IMPERSONATION_INFO ImpersonationInfo
; /* 208/234 */
261 LIST_ENTRY IrpList
; /* 20C/238 */
262 TOP_LEVEL_IRP
* TopLevelIrp
; /* 214/240 */
263 PDEVICE_OBJECT DeviceToVerify
; /* 218/244 */
264 ULONG ReadClusterSize
; /* 21C/248 */
265 UCHAR ForwardClusterOnly
; /* 220/24C */
266 UCHAR DisablePageFaultClustering
; /* 221/24D */
267 UCHAR DeadThread
; /* 222/24E */
268 UCHAR HasTerminated
; /* 223/24F */
269 PVOID EventPair
; /* 224/250 */
270 ACCESS_MASK GrantedAccess
; /* 228/254 */
271 struct _EPROCESS
* ThreadsProcess
; /* 22C/258 */
272 PKSTART_ROUTINE StartAddress
; /* 230/25C */
275 LPTHREAD_START_ROUTINE Win32StartAddress
; /* 234/260 */
276 ULONG LpcReceiveMessageId
; /* 234/260 */
278 UCHAR LpcExitThreadCalled
; /* 238/264 */
279 UCHAR HardErrorsAreDisabled
; /* 239/265 */
280 UCHAR LpcReceivedMsgIdValid
; /* 23A/266 */
281 UCHAR ActiveImpersonationInfo
; /* 23B/267 */
282 ULONG PerformanceCountHigh
; /* 23C/268 */
285 * Added by David Welch (welch@cwcom.net)
287 struct _EPROCESS
* OldProcess
; /* 240/26C */
289 struct _W32THREAD
* Win32Thread
;
291 } __attribute__((packed
)) ETHREAD
, *PETHREAD
;
294 typedef struct _KPROCESS
296 /* So it's possible to wait for the process to terminate */
297 DISPATCHER_HEADER DispatcherHeader
; /* 000 */
299 * Presumably a list of profile objects associated with this process,
302 LIST_ENTRY ProfileListHead
; /* 010 */
304 * We use the first member of this array to hold the physical address of
305 * the page directory for this process.
307 PHYSICAL_ADDRESS DirectoryTableBase
; /* 018 */
309 * Presumably a descriptor for the process's LDT, currently unused.
311 ULONG LdtDescriptor
[2]; /* 020 */
313 * Presumably for processing int 0x21 from V86 mode DOS, currently
316 ULONG Int21Descriptor
[2]; /* 028 */
318 USHORT IopmOffset
; /* 030 */
320 * Presumably I/O privilege level to be used for this process, currently
323 UCHAR Iopl
; /* 032 */
324 /* Set if this process is a virtual dos machine? */
325 UCHAR VdmFlag
; /* 033 */
326 /* Bitmask of the processors being used by this process's threads? */
327 ULONG ActiveProcessors
; /* 034 */
328 /* Aggregate of the time this process's threads have spent in kernel mode? */
329 ULONG KernelTime
; /* 038 */
330 /* Aggregate of the time this process's threads have spent in user mode? */
331 ULONG UserTime
; /* 03C */
332 /* List of this process's threads that are ready for execution? */
333 LIST_ENTRY ReadyListHead
; /* 040 */
334 /* List of this process's threads that have their stacks swapped out? */
335 LIST_ENTRY SwapListEntry
; /* 048 */
336 /* List of this process's threads? */
337 LIST_ENTRY ThreadListHead
; /* 050 */
338 /* Maybe a lock for this data structure, the type is assumed. */
339 KSPIN_LOCK ProcessLock
; /* 058 */
340 /* Default affinity mask for this process's threads? */
341 ULONG Affinity
; /* 05C */
342 /* Count of the stacks allocated for this process's threads? */
343 USHORT StackCount
; /* 060 */
344 /* Base priority for this process's threads? */
345 KPRIORITY BasePriority
; /* 062 */
346 /* Default quantum for this process's threads */
347 UCHAR ThreadQuantum
; /* 063 */
349 UCHAR AutoAlignment
; /* 064 */
350 /* Process execution state, currently either active or terminated. */
351 UCHAR State
; /* 065 */
352 /* Seed for generating thread ids for this process's threads? */
353 UCHAR ThreadSeed
; /* 066 */
354 /* Disable priority boosts? */
355 UCHAR DisableBoost
; /* 067 */
356 } KPROCESS
, *PKPROCESS
;
360 /* Microkernel specific process state. */
361 KPROCESS Pcb
; /* 000 */
362 /* Exit status of the process. */
363 NTSTATUS ExitStatus
; /* 068 */
365 KEVENT LockEvent
; /* 06C */
367 ULONG LockCount
; /* 07C */
368 /* Time of process creation. */
369 TIME CreateTime
; /* 080 */
370 /* Time of process exit. */
371 TIME ExitTime
; /* 088 */
373 PVOID LockOwner
; /* 090 */
375 ULONG UniqueProcessId
; /* 094 */
377 LIST_ENTRY ActiveProcessLinks
; /* 098 */
379 ULONG QuotaPeakPoolUsage
[2]; /* 0A0 */
381 ULONG QuotaPoolUsage
[2]; /* 0A8 */
383 ULONG PagefileUsage
; /* 0B0 */
385 ULONG CommitCharge
; /* 0B4 */
387 ULONG PeakPagefileUsage
; /* 0B8 */
389 ULONG PeakVirtualSize
; /* 0BC */
391 LARGE_INTEGER VirtualSize
; /* 0C0 */
395 ULONG LastTrimFaultCount
;
396 ULONG PageFaultCount
;
397 ULONG PeakWorkingSetSize
;
398 ULONG WorkingSetSize
;
399 ULONG MinimumWorkingSetSize
;
400 ULONG MaximumWorkingSetSize
;
401 ULONG VmWorkingSetList
;
402 LIST_ENTRY WorkingSetExpansionList
;
403 UCHAR AllowWorkingSetAdjustment
;
404 UCHAR AddressSpaceBeingDeleted
;
405 UCHAR ForegroundPrioritySwitch
;
406 UCHAR MemoryPriority
;
408 PVOID LastProtoPteFault
;
409 struct _EPORT
* DebugPort
;
410 struct _EPORT
* ExceptionPort
;
413 // FAST_MUTEX WorkingSetLock;
414 KMUTEX WorkingSetLock
;
415 PVOID WorkingSetPage
;
416 UCHAR ProcessOutswapEnabled
;
417 UCHAR ProcessOutswapped
;
418 UCHAR AddressSpaceInitialized
;
419 UCHAR AddressSpaceDeleted
;
420 FAST_MUTEX AddressCreationLock
;
421 KSPIN_LOCK HyperSpaceLock
;
422 PETHREAD ForkInProgress
;
424 UCHAR ForkWasSuccessful
;
425 UCHAR MmAgressiveWsTrimMask
;
426 PKEVENT VmOperationEvent
;
427 PVOID PageDirectoryPte
;
428 ULONG LastFaultCount
;
432 ULONG NumberOfPrivatePages
;
433 ULONG NumberOfLockedPages
;
434 USHORT NextProcessColour
;
435 UCHAR ExitProcessCalled
;
436 UCHAR CreateProcessReported
;
437 HANDLE SectionHandle
;
439 PVOID SectionBaseAddress
;
441 NTSTATUS LastThreadExitStatus
;
442 PVOID WorkingSetWatch
;
443 HANDLE InheritedFromUniqueProcessId
;
444 ACCESS_MASK GrantedAccess
;
445 ULONG DefaultHardErrorProcessing
;
446 PVOID LdtInformation
;
449 KMUTANT ProcessMutant
;
450 CHAR ImageFileName
[16];
451 ULONG VmTrimFaultValue
;
452 UCHAR SetTimerResolution
;
454 UCHAR SubSystemMinorVersion
;
455 UCHAR SubSystemMajorVersion
;
456 USHORT SubSystemVersion
;
457 struct _W32PROCESS
* Win32Process
;
458 HANDLE Win32WindowStation
;
461 * Added by David Welch (welch@mcmail.com)
464 MADDRESS_SPACE AddressSpace
;
465 HANDLE_TABLE HandleTable
;
466 LIST_ENTRY ProcessListEntry
;
469 * Added by Philip Susi for list of threads in process
471 LIST_ENTRY ThreadListHead
;
474 #define PROCESS_STATE_TERMINATED (1)
475 #define PROCESS_STATE_ACTIVE (2)
477 VOID
PiInitDefaultLocale(VOID
);
478 VOID
PiInitProcessManager(VOID
);
479 VOID
PiShutdownProcessManager(VOID
);
480 VOID
PsInitThreadManagment(VOID
);
481 VOID
PsInitProcessManagment(VOID
);
482 VOID
PsInitIdleThread(VOID
);
483 VOID
PsDispatchThreadNoLock(ULONG NewThreadStatus
);
484 VOID
PiTerminateProcessThreads(PEPROCESS Process
, NTSTATUS ExitStatus
);
485 VOID
PsTerminateOtherThread(PETHREAD Thread
, NTSTATUS ExitStatus
);
486 VOID
PsReleaseThread(PETHREAD Thread
);
487 VOID
PsBeginThread(PKSTART_ROUTINE StartRoutine
, PVOID StartContext
);
488 VOID
PsBeginThreadWithContextInternal(VOID
);
489 VOID
PiKillMostProcesses(VOID
);
490 NTSTATUS STDCALL
PiTerminateProcess(PEPROCESS Process
, NTSTATUS ExitStatus
);
491 ULONG
PsUnfreezeThread(PETHREAD Thread
, PNTSTATUS WaitStatus
);
492 ULONG
PsFreezeThread(PETHREAD Thread
, PNTSTATUS WaitStatus
,
493 UCHAR Alertable
, ULONG WaitMode
);
494 VOID
PiInitApcManagement(VOID
);
495 VOID STDCALL
PiDeleteThread(PVOID ObjectBody
);
496 VOID STDCALL
PiCloseThread(PVOID ObjectBody
, ULONG HandleCount
);
497 VOID
PsReapThreads(VOID
);
499 PsInitializeThread(HANDLE ProcessHandle
,
501 PHANDLE ThreadHandle
,
502 ACCESS_MASK DesiredAccess
,
503 POBJECT_ATTRIBUTES ObjectAttributes
,
506 PACCESS_TOKEN
PsReferenceEffectiveToken(PETHREAD Thread
,
507 PTOKEN_TYPE TokenType
,
509 PSECURITY_IMPERSONATION_LEVEL Level
);
511 NTSTATUS
PsOpenTokenOfProcess(HANDLE ProcessHandle
,
512 PACCESS_TOKEN
* Token
);
514 NTSTATUS
PsSuspendThread(PETHREAD Thread
, PULONG PreviousCount
);
515 NTSTATUS
PsResumeThread(PETHREAD Thread
, PULONG PreviousCount
);
518 #define THREAD_STATE_INVALID (0)
519 #define THREAD_STATE_RUNNABLE (1)
520 #define THREAD_STATE_RUNNING (2)
521 #define THREAD_STATE_SUSPENDED (3)
522 #define THREAD_STATE_FROZEN (4)
523 #define THREAD_STATE_TERMINATED_1 (5)
524 #define THREAD_STATE_TERMINATED_2 (6)
525 #define THREAD_STATE_BLOCKED (7)
526 #define THREAD_STATE_MAX (8)
530 * Internal thread priorities, added by Phillip Susi
531 * TODO: rebalence these to make use of all priorities... the ones above 16 can not all be used right now
534 #define PROCESS_PRIO_IDLE 3
535 #define PROCESS_PRIO_NORMAL 8
536 #define PROCESS_PRIO_HIGH 13
537 #define PROCESS_PRIO_RT 18
541 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
);
542 NTSTATUS
KeReleaseThread(PETHREAD Thread
);
544 Ke386InitThread(PKTHREAD Thread
, PKSTART_ROUTINE fn
, PVOID StartContext
);
546 Ke386InitThreadWithContext(PKTHREAD Thread
, PCONTEXT Context
);
547 VOID STDCALL
PiDeleteProcess(PVOID ObjectBody
);
548 VOID
PsReapThreads(VOID
);
549 VOID
PsUnfreezeOtherThread(PETHREAD Thread
);
550 VOID
PsFreezeOtherThread(PETHREAD Thread
);
551 VOID
PsFreezeProcessThreads(PEPROCESS Process
);
552 VOID
PsUnfreezeProcessThreads(PEPROCESS Process
);
553 PEPROCESS
PsGetNextProcess(PEPROCESS OldProcess
);
555 Ki386ContextSwitch(PKTHREAD NewThread
, PKTHREAD OldThread
);
557 PsBlockThread(PNTSTATUS Status
, UCHAR Alertable
, ULONG WaitMode
,
558 BOOLEAN DispatcherLock
, KIRQL WaitIrql
);
560 PsUnblockThread(PETHREAD Thread
, PNTSTATUS WaitStatus
);
562 PsApplicationProcessorInit(VOID
);
564 PsPrepareForApplicationProcessorInit(ULONG Id
);
566 PsIdleThreadMain(PVOID Context
);
569 PiSuspendThreadRundownRoutine(PKAPC Apc
);
571 PiSuspendThreadKernelRoutine(PKAPC Apc
,
572 PKNORMAL_ROUTINE
* NormalRoutine
,
573 PVOID
* NormalContext
,
574 PVOID
* SystemArgument1
,
575 PVOID
* SystemArguemnt2
);
577 PiSuspendThreadNormalRoutine(PVOID NormalContext
,
578 PVOID SystemArgument1
,
579 PVOID SystemArgument2
);
582 PiTimeoutThread(struct _KDPC
*dpc
,
588 PsDispatchThread(ULONG NewThreadStatus
);
590 #endif /* ASSEMBLER */
592 #endif /* __INCLUDE_INTERNAL_PS_H */