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