8902e3dffb46ed22a3c34026a5b7ca1143ccc0cf
[reactos.git] / reactos / ntoskrnl / include / internal / ps.h
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
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.
9 *
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.
14 *
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.
18 */
19 /*
20 * FILE: ntoskrnl/ke/kthread.c
21 * PURPOSE: Process manager definitions
22 * PROGRAMMER: David Welch (welch@cwcom.net)
23 * UPDATE HISTORY:
24 * Created 22/05/98
25 */
26
27 #ifndef __INCLUDE_INTERNAL_PS_H
28 #define __INCLUDE_INTERNAL_PS_H
29
30 /*
31 * Defines for accessing KPCR and KTHREAD structure members
32 */
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
38
39 #define ETHREAD_THREADS_PROCESS 0x258
40
41 #define KPROCESS_PAGE_TABLE_DIRECTORY 0x10
42
43 #define KPCR_BASE 0xFFDFF000
44
45 #define KPCR_EXCEPTION_LIST 0x0
46 #define KPCR_CURRENT_THREAD 0x124
47
48 #ifndef __ASM__
49
50 #include <internal/mm.h>
51
52 struct _KTHREAD;
53 struct _KTRAPFRAME;
54
55 /*
56 * Processor Control Region
57 */
58 typedef struct _KPCR
59 {
60 PVOID ExceptionList; /* 00 */
61 PVOID StackBase; /* 04 */
62 PVOID StackLimit; /* 08 */
63 PVOID SubSystemTib; /* 0C */
64 PVOID Reserved1; /* 10 */
65 PVOID ArbitraryUserPointer; /* 14 */
66 struct _KPCR* Self; /* 18 */
67 UCHAR Reserved2[0x108]; /* 1C */
68 struct _KTHREAD* CurrentThread; /* 124 */
69 } KPCR, *PKPCR;
70
71 #define CURRENT_KPCR ((PKPCR)KPCR_BASE)
72
73 extern HANDLE SystemProcessHandle;
74
75 typedef struct _KAPC_STATE
76 {
77 LIST_ENTRY ApcListHead[2];
78 struct _KPROCESS* Process;
79 UCHAR KernelApcInProgress;
80 UCHAR KernelApcPending;
81 USHORT UserApcPending;
82 } __attribute__((packed)) KAPC_STATE, *PKAPC_STATE;
83
84 typedef struct _KTHREAD
85 {
86 /* For waiting on thread exit */
87 DISPATCHER_HEADER DispatcherHeader; /* 00 */
88
89 /* List of mutants owned by the thread */
90 LIST_ENTRY MutantListHead; /* 10 */
91 PVOID InitialStack; /* 18 */
92 ULONG StackLimit; /* 1C */
93
94 /* Pointer to the thread's environment block in user memory */
95 NT_TEB* Teb; /* 20 */
96
97 /* Pointer to the thread's TLS array */
98 PVOID TlsArray; /* 24 */
99 PVOID KernelStack; /* 28 */
100 UCHAR DebugActive; /* 2C */
101
102 /* Thread state (one of THREAD_STATE_xxx constants below) */
103 UCHAR State; /* 2D */
104 UCHAR Alerted[2]; /* 2E */
105 UCHAR Iopl; /* 30 */
106 UCHAR NpxState; /* 31 */
107 UCHAR Saturation; /* 32 */
108 CHAR Priority; /* 33 */
109 KAPC_STATE ApcState; /* 34 */
110 ULONG ContextSwitches; /* 4C */
111 ULONG WaitStatus; /* 50 */
112 KIRQL WaitIrql; /* 54 */
113 UCHAR WaitMode; /* 55 */
114 UCHAR WaitNext; /* 56 */
115 UCHAR WaitReason; /* 57 */
116 PKWAIT_BLOCK WaitBlockList; /* 58 */
117 LIST_ENTRY WaitListEntry; /* 5C */
118 ULONG WaitTime; /* 64 */
119 CHAR BasePriority; /* 68 */
120 UCHAR DecrementCount; /* 69 */
121 UCHAR PriorityDecrement; /* 6A */
122 UCHAR Quantum; /* 6B */
123 KWAIT_BLOCK WaitBlock[4]; /* 6C */
124 PVOID LegoData; /* CC */
125 LONG KernelApcDisable; /* D0 */
126 KAFFINITY UserAffinity; /* D4 */
127 UCHAR SystemAffinityActive;/* D8 */
128 UCHAR Pad[7]; /* D9 */
129 PKQUEUE Queue; /* E0 */
130 KSPIN_LOCK ApcQueueLock; /* E4 */
131 KTIMER Timer; /* E8 */
132 LIST_ENTRY QueueListEntry; /* 110 */
133 KAFFINITY Affinity; /* 118 */
134 UCHAR Preempted; /* 11C */
135 UCHAR ProcessReadyQueue; /* 11D */
136 UCHAR KernelStackResident; /* 11E */
137 UCHAR NextProcessor; /* 11F */
138 PVOID CallbackStack; /* 120 */
139 BOOL Win32Thread; /* 124 */
140 struct _KTRAP_FRAME* TrapFrame; /* 128 */
141 PVOID ApcStatePointer[2]; /* 12C */
142 UCHAR EnableStackSwap; /* 134 */
143 UCHAR LargeStack; /* 135 */
144 UCHAR ResourceIndex; /* 136 */
145 UCHAR PreviousMode; /* 137 */
146 ULONG KernelTime; /* 138 */
147 ULONG UserTime; /* 13C */
148 KAPC_STATE SavedApcState; /* 140 */
149 UCHAR Alertable; /* 158 */
150 UCHAR ApcStateIndex; /* 159 */
151 UCHAR ApcQueueable; /* 15A */
152 UCHAR AutoAlignment; /* 15B */
153 PVOID StackBase; /* 15C */
154 KAPC SuspendApc; /* 160 */
155 KSEMAPHORE SuspendSemaphore; /* 190 */
156 LIST_ENTRY ThreadListEntry; /* 1A4 */
157 CHAR FreezeCount; /* 1AC */
158 UCHAR SuspendCount; /* 1AD */
159 UCHAR IdealProcessor; /* 1AE */
160 UCHAR DisableBoost; /* 1AF */
161
162 /*
163 * Below here are thread structure members that are specific to ReactOS
164 */
165
166 /* Added by Phillip Susi for list of threads in a process */
167 LIST_ENTRY ProcessThreadListEntry; /* 1B0 */
168
169 /* Added by Phillip Susi for internal KeAddThreadTimeout() implementation */
170 KDPC TimerDpc; /* 1B8 */
171
172 /* Record the last EIP value when the thread is suspended */
173 ULONG LastEip; /* 1D8 */
174 } __attribute__((packed)) KTHREAD, *PKTHREAD;
175
176 // According to documentation the stack should have a commited [ 1 page ] and
177 // a reserved part [ 1 M ] but can be specified otherwise in the image file.
178
179
180
181
182
183
184
185 // TopLevelIrp can be one of the following values:
186 // FIXME I belong somewhere else
187
188 #define FSRTL_FSP_TOP_LEVEL_IRP (0x01)
189 #define FSRTL_CACHE_TOP_LEVEL_IRP (0x02)
190 #define FSRTL_MOD_WRITE_TOP_LEVEL_IRP (0x03)
191 #define FSRTL_FAST_IO_TOP_LEVEL_IRP (0x04)
192 #define FSRTL_MAX_TOP_LEVEL_IRP_FLAG (0x04)
193
194 typedef struct _TOP_LEVEL_IRP
195 {
196 PIRP TopLevelIrp;
197 ULONG TopLevelIrpConst;
198 } TOP_LEVEL_IRP;
199
200 typedef struct
201 {
202 PACCESS_TOKEN Token; // 0x0
203 UCHAR Unknown1; // 0x4
204 UCHAR Unknown2; // 0x5
205 UCHAR Pad[2]; // 0x6
206 SECURITY_IMPERSONATION_LEVEL Level; // 0x8
207 } PS_IMPERSONATION_INFO, *PPS_IMPERSONATION_INFO;
208
209 struct _WIN32THREADDATA;
210
211 typedef struct _ETHREAD
212 {
213 KTHREAD Tcb; /* 000 */
214 TIME CreateTime; /* 1B0/1DC */
215 union
216 {
217 TIME ExitTime; /* 1B8/1E4 */
218 LIST_ENTRY LpcReplyChain; /* 1B8/1E4 */
219 } u1;
220 NTSTATUS ExitStatus; /* 1C0/1EC */
221 LIST_ENTRY PostBlockList; /* 1C4/1F0 */
222 LIST_ENTRY TerminationPortList; /* 1CC/1F8 */
223 KSPIN_LOCK ActiveTimerListLock; /* 1D4/200 */
224 LIST_ENTRY ActiveTimerListHead; /* 1D8/204 */
225 CLIENT_ID Cid; /* 1E0/20C */
226 KSEMAPHORE LpcReplySemaphore; /* 1E8/214 */
227 PVOID LpcReplyMessage; /* 1FC/228 */
228 PLARGE_INTEGER LpcReplyMessageId; /* 200/22C */
229 ULONG PerformanceCounterLow; /* 204/230 */
230 PPS_IMPERSONATION_INFO ImpersonationInfo; /* 208/234 */
231 LIST_ENTRY IrpList; /* 20C/238 */
232 TOP_LEVEL_IRP* TopLevelIrp; /* 214/240 */
233 PDEVICE_OBJECT DeviceToVerify; /* 218/244 */
234 ULONG ReadClusterSize; /* 21C/248 */
235 UCHAR ForwardClusterOnly; /* 220/24C */
236 UCHAR DisablePageFaultClustering; /* 221/24D */
237 UCHAR DeadThread; /* 222/24E */
238 UCHAR HasTerminated; /* 223/24F */
239 PVOID EventPair; /* 224/250 */
240 ACCESS_MASK GrantedAccess; /* 228/254 */
241 struct _EPROCESS* ThreadsProcess; /* 22C/258 */
242 PKSTART_ROUTINE StartAddress; /* 230/25C */
243 union
244 {
245 LPTHREAD_START_ROUTINE Win32StartAddress; /* 234/260 */
246 ULONG LpcReceiveMessageId; /* 234/260 */
247 } u2;
248 UCHAR LpcExitThreadCalled; /* 238/264 */
249 UCHAR HardErrorsAreDisabled; /* 239/265 */
250 UCHAR LpcReceivedMsgIdValid; /* 23A/266 */
251 UCHAR ActiveImpersonationInfo; /* 23B/267 */
252 ULONG PerformanceCountHigh; /* 23C/268 */
253
254 /*
255 * Added by David Welch (welch@cwcom.net)
256 */
257 struct _EPROCESS* OldProcess; /* 240/26C */
258 struct _WIN32THREADDATA *Win32ThreadData; // Pointer to win32 private thread data
259
260 } __attribute__((packed)) ETHREAD, *PETHREAD;
261
262
263 typedef struct _KPROCESS
264 {
265 DISPATCHER_HEADER DispatcherHeader; /* 000 */
266 PVOID PageTableDirectory; /* 010 */
267 TIME ElapsedTime;
268 TIME KernelTime;
269 TIME UserTime;
270 LIST_ENTRY InMemoryList;
271 LIST_ENTRY SwappedOutList;
272 KSPIN_LOCK SpinLock;
273 KAFFINITY Affinity;
274 ULONG StackCount;
275 KPRIORITY BasePriority;
276 ULONG DefaultThreadQuantum;
277 UCHAR ProcessState;
278 ULONG ThreadSeed;
279 UCHAR DisableBoost;
280 } KPROCESS, *PKPROCESS;
281
282 struct _WIN32PROCESSDATA;
283
284 typedef struct _EPROCESS
285 {
286 KPROCESS Pcb;
287 NTSTATUS ExitStatus;
288 KEVENT LockEvent;
289 ULONG LockCount;
290 TIME CreateTime;
291 TIME ExitTime;
292 PVOID LockOwner;
293 ULONG UniqueProcessId;
294 LIST_ENTRY ActiveProcessLinks;
295 ULONG QuotaPeakPoolUsage[2];
296 ULONG QuotaPoolUsage[2];
297 ULONG PagefileUsage;
298 ULONG CommitCharge;
299 ULONG PeakPagefileUsage;
300 ULONG PeakVirtualUsage;
301 LARGE_INTEGER VirtualSize;
302 PVOID Vm; // Actually 48 bytes
303 PVOID LastProtoPteFault;
304 struct _EPORT* DebugPort;
305 struct _EPORT* ExceptionPort;
306 PVOID ObjectTable;
307 PVOID Token;
308 KMUTEX WorkingSetLock;
309 PVOID WorkingSetPage;
310 UCHAR ProcessOutswapEnabled;
311 UCHAR ProcessOutswapped;
312 UCHAR AddressSpaceInitialized;
313 UCHAR AddressSpaceDeleted;
314 KMUTEX AddressCreationLock;
315 PVOID ForkInProgress;
316 PVOID VmOperation;
317 PKEVENT VmOperationEvent;
318 PVOID PageDirectoryPte;
319 LARGE_INTEGER LastFaultCount;
320 PVOID VadRoot;
321 PVOID VadHint;
322 PVOID CloneRoot;
323 ULONG NumberOfPrivatePages;
324 ULONG NumberOfLockedPages;
325 UCHAR ForkWasSuccessFul;
326 UCHAR ExitProcessCalled;
327 UCHAR CreateProcessReported;
328 HANDLE SectionHandle;
329 PPEB Peb;
330 PVOID SectionBaseAddress;
331 PVOID QuotaBlock;
332 NTSTATUS LastThreadExitStatus;
333 LARGE_INTEGER WorkingSetWatch; //
334 ULONG InheritedFromUniqueProcessId;
335 ACCESS_MASK GrantedAccess;
336 ULONG DefaultHardErrorProcessing;
337 PVOID LdtInformation;
338 ULONG VadFreeHint;
339 PVOID VdmObjects;
340 KMUTANT ProcessMutant;
341 CHAR ImageFileName[16];
342 LARGE_INTEGER VmTrimFaultValue;
343 struct _WIN32PROCESSDATA *Win32Process;
344
345 /*
346 * Added by David Welch (welch@mcmail.com)
347 */
348 MADDRESS_SPACE AddressSpace;
349 HANDLE_TABLE HandleTable;
350 LIST_ENTRY ProcessListEntry;
351
352 /*
353 * Added by Philip Susi for list of threads in process
354 */
355 LIST_ENTRY ThreadListHead;
356 } EPROCESS, *PEPROCESS;
357
358 #define PROCESS_STATE_TERMINATED (1)
359 #define PROCESS_STATE_ACTIVE (2)
360
361 VOID PiInitProcessManager(VOID);
362 VOID PiShutdownProcessManager(VOID);
363 VOID PsInitThreadManagment(VOID);
364 VOID PsInitProcessManagment(VOID);
365 VOID PsInitIdleThread(VOID);
366 VOID PsDispatchThread(ULONG NewThreadStatus);
367 VOID PsDispatchThreadNoLock(ULONG NewThreadStatus);
368 VOID PiTerminateProcessThreads(PEPROCESS Process, NTSTATUS ExitStatus);
369 VOID PsTerminateOtherThread(PETHREAD Thread, NTSTATUS ExitStatus);
370 VOID PsReleaseThread(PETHREAD Thread);
371 VOID PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext);
372 VOID PsBeginThreadWithContextInternal(VOID);
373 VOID PiKillMostProcesses(VOID);
374 NTSTATUS STDCALL PiTerminateProcess(PEPROCESS Process, NTSTATUS ExitStatus);
375 ULONG PsUnfreezeThread(PETHREAD Thread, PNTSTATUS WaitStatus);
376 ULONG PsFreezeThread(PETHREAD Thread, PNTSTATUS WaitStatus,
377 UCHAR Alertable, ULONG WaitMode);
378 VOID PiInitApcManagement(VOID);
379 VOID PiDeleteThread(PVOID ObjectBody);
380 VOID PiCloseThread(PVOID ObjectBody, ULONG HandleCount);
381 VOID PsReapThreads(VOID);
382 NTSTATUS
383 PsInitializeThread(HANDLE ProcessHandle,
384 PETHREAD* ThreadPtr,
385 PHANDLE ThreadHandle,
386 ACCESS_MASK DesiredAccess,
387 POBJECT_ATTRIBUTES ObjectAttributes,
388 BOOLEAN First);
389
390 PACCESS_TOKEN PsReferenceEffectiveToken(PETHREAD Thread,
391 PTOKEN_TYPE TokenType,
392 PUCHAR b,
393 PSECURITY_IMPERSONATION_LEVEL Level);
394
395 NTSTATUS PsOpenTokenOfProcess(HANDLE ProcessHandle,
396 PACCESS_TOKEN* Token);
397
398 NTSTATUS PsSuspendThread(PETHREAD Thread, PULONG PreviousCount);
399 NTSTATUS PsResumeThread(PETHREAD Thread, PULONG PreviousCount);
400
401
402 #define THREAD_STATE_INVALID (0)
403 #define THREAD_STATE_RUNNABLE (1)
404 #define THREAD_STATE_RUNNING (2)
405 #define THREAD_STATE_SUSPENDED (3)
406 #define THREAD_STATE_FROZEN (4)
407 #define THREAD_STATE_TERMINATED_1 (5)
408 #define THREAD_STATE_TERMINATED_2 (6)
409 #define THREAD_STATE_BLOCKED (7)
410 #define THREAD_STATE_MAX (8)
411
412
413 /*
414 * Internal thread priorities, added by Phillip Susi
415 * TODO: rebalence these to make use of all priorities... the ones above 16 can not all be used right now
416 */
417
418 #define PROCESS_PRIO_IDLE 3
419 #define PROCESS_PRIO_NORMAL 8
420 #define PROCESS_PRIO_HIGH 13
421 #define PROCESS_PRIO_RT 18
422
423
424 VOID
425 KeInitializeThread(PKPROCESS Process, PKTHREAD Thread, BOOLEAN First);
426
427 VOID HalInitFirstTask(PETHREAD thread);
428 NTSTATUS
429 Ke386InitThread(PKTHREAD thread, PKSTART_ROUTINE fn, PVOID StartContext);
430 VOID HalTaskSwitch(PKTHREAD thread);
431 NTSTATUS
432 Ke386InitThreadWithContext(PKTHREAD Thread, PCONTEXT Context);
433 NTSTATUS HalReleaseTask(PETHREAD Thread);
434 VOID PiDeleteProcess(PVOID ObjectBody);
435 VOID PsReapThreads(VOID);
436 VOID PsUnfreezeOtherThread(PETHREAD Thread);
437 VOID PsFreezeOtherThread(PETHREAD Thread);
438 VOID PsFreezeProcessThreads(PEPROCESS Process);
439 VOID PsUnfreezeProcessThreads(PEPROCESS Process);
440 PEPROCESS PsGetNextProcess(PEPROCESS OldProcess);
441 VOID
442 Ki386ContextSwitch(PKTHREAD NewThread, PKTHREAD OldThread);
443 VOID
444 PsBlockThread(PNTSTATUS Status, UCHAR Alertable, ULONG WaitMode,
445 BOOLEAN DispatcherLock, KIRQL WaitIrql);
446 VOID
447 PsUnblockThread(PETHREAD Thread, PNTSTATUS WaitStatus);
448
449 #endif /* ASSEMBLER */
450
451 #endif /* __INCLUDE_INTERNAL_PS_H */