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