Accidently removed one line in the last commit ... gotta love this header hell. :/
[reactos.git] / reactos / ntoskrnl / include / internal / ke.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 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_KE_H
21 #define __NTOSKRNL_INCLUDE_INTERNAL_KE_H
22
23 /* INCLUDES *****************************************************************/
24
25 #ifndef __ASM__
26 #include <ddk/ntifs.h>
27 #include <stdarg.h>
28 #endif /* not __ASM__ */
29
30 #include "arch/ke.h"
31
32 /* INTERNAL KERNEL TYPES ****************************************************/
33
34 #ifndef __ASM__
35
36 #ifndef __USE_W32API
37
38 typedef struct _KPROCESS *PKPROCESS;
39 typedef struct _DISPATCHER_HEADER *PDISPATCHER_HEADER;
40
41 #else
42
43 typedef struct _KEVENT_PAIR *PKEVENT_PAIR;
44
45 #endif /* __USE_W32API */
46
47 typedef struct _HARDWARE_PTE_X86 {
48 ULONG Valid : 1;
49 ULONG Write : 1;
50 ULONG Owner : 1;
51 ULONG WriteThrough : 1;
52 ULONG CacheDisable : 1;
53 ULONG Accessed : 1;
54 ULONG Dirty : 1;
55 ULONG LargePage : 1;
56 ULONG Global : 1;
57 ULONG CopyOnWrite : 1;
58 ULONG Prototype : 1;
59 ULONG reserved : 1;
60 ULONG PageFrameNumber : 20;
61 } HARDWARE_PTE_X86, *PHARDWARE_PTE_X86;
62
63 typedef struct _WOW64_PROCESS
64 {
65 PVOID Wow64;
66 } WOW64_PROCESS, *PWOW64_PROCESS;
67
68 #include <pshpack1.h>
69
70 typedef struct _KTHREAD
71 {
72 /* For waiting on thread exit */
73 DISPATCHER_HEADER DispatcherHeader; /* 00 */
74
75 /* List of mutants owned by the thread */
76 LIST_ENTRY MutantListHead; /* 10 */
77 PVOID InitialStack; /* 18 */
78 ULONG_PTR StackLimit; /* 1C */
79
80 /* Pointer to the thread's environment block in user memory */
81 struct _TEB *Teb; /* 20 */
82
83 /* Pointer to the thread's TLS array */
84 PVOID TlsArray; /* 24 */
85 PVOID KernelStack; /* 28 */
86 UCHAR DebugActive; /* 2C */
87
88 /* Thread state (one of THREAD_STATE_xxx constants below) */
89 UCHAR State; /* 2D */
90 BOOLEAN Alerted[2]; /* 2E */
91 UCHAR Iopl; /* 30 */
92 UCHAR NpxState; /* 31 */
93 CHAR Saturation; /* 32 */
94 CHAR Priority; /* 33 */
95 KAPC_STATE ApcState; /* 34 */
96 ULONG ContextSwitches; /* 4C */
97 LONG WaitStatus; /* 50 */
98 KIRQL WaitIrql; /* 54 */
99 CHAR WaitMode; /* 55 */
100 UCHAR WaitNext; /* 56 */
101 UCHAR WaitReason; /* 57 */
102 union { /* 58 */
103 PKWAIT_BLOCK WaitBlockList; /* 58 */
104 PKGATE GateObject; /* 58 */
105 }; /* 58 */
106 LIST_ENTRY WaitListEntry; /* 5C */
107 ULONG WaitTime; /* 64 */
108 CHAR BasePriority; /* 68 */
109 UCHAR DecrementCount; /* 69 */
110 UCHAR PriorityDecrement; /* 6A */
111 CHAR Quantum; /* 6B */
112 KWAIT_BLOCK WaitBlock[4]; /* 6C */
113 PVOID LegoData; /* CC */
114 union {
115 struct {
116 USHORT KernelApcDisable;
117 USHORT SpecialApcDisable;
118 };
119 ULONG CombinedApcDisable; /* D0 */
120 };
121 KAFFINITY UserAffinity; /* D4 */
122 UCHAR SystemAffinityActive;/* D8 */
123 UCHAR PowerState; /* D9 */
124 UCHAR NpxIrql; /* DA */
125 UCHAR Pad[1]; /* DB */
126 PVOID ServiceTable; /* DC */
127 PKQUEUE Queue; /* E0 */
128 KSPIN_LOCK ApcQueueLock; /* E4 */
129 KTIMER Timer; /* E8 */
130 LIST_ENTRY QueueListEntry; /* 110 */
131 KAFFINITY Affinity; /* 118 */
132 UCHAR Preempted; /* 11C */
133 UCHAR ProcessReadyQueue; /* 11D */
134 UCHAR KernelStackResident; /* 11E */
135 UCHAR NextProcessor; /* 11F */
136 PVOID CallbackStack; /* 120 */
137 struct _W32THREAD *Win32Thread; /* 124 */
138 struct _KTRAP_FRAME *TrapFrame; /* 128 */
139 PKAPC_STATE ApcStatePointer[2]; /* 12C */
140 UCHAR EnableStackSwap; /* 134 */
141 UCHAR LargeStack; /* 135 */
142 UCHAR ResourceIndex; /* 136 */
143 UCHAR PreviousMode; /* 137 */
144 ULONG KernelTime; /* 138 */
145 ULONG UserTime; /* 13C */
146 KAPC_STATE SavedApcState; /* 140 */
147 UCHAR Alertable; /* 158 */
148 UCHAR ApcStateIndex; /* 159 */
149 UCHAR ApcQueueable; /* 15A */
150 UCHAR AutoAlignment; /* 15B */
151 PVOID StackBase; /* 15C */
152 KAPC SuspendApc; /* 160 */
153 KSEMAPHORE SuspendSemaphore; /* 190 */
154 LIST_ENTRY ThreadListEntry; /* 1A4 */
155 CHAR FreezeCount; /* 1AC */
156 UCHAR SuspendCount; /* 1AD */
157 UCHAR IdealProcessor; /* 1AE */
158 UCHAR DisableBoost; /* 1AF */
159 UCHAR QuantumReset; /* 1B0 */
160 } KTHREAD;
161
162 #include <poppack.h>
163
164 typedef struct _KEXECUTE_OPTIONS
165 {
166 UCHAR ExecuteDisable:1;
167 UCHAR ExecuteEnable:1;
168 UCHAR DisableThunkEmulation:1;
169 UCHAR Permanent:1;
170 UCHAR ExecuteDispatchEnable:1;
171 UCHAR ImageDispatchEnable:1;
172 UCHAR Spare:2;
173 } KEXECUTE_OPTIONS, *PKEXECUTE_OPTIONS;
174
175 /*
176 * NAME: KPROCESS
177 * DESCRIPTION: Internal Kernel Process Structure.
178 * PORTABILITY: Architecture Dependent.
179 * KERNEL VERSION: 5.2
180 * DOCUMENTATION: http://reactos.com/wiki/index.php/KPROCESS
181 */
182 typedef struct _KPROCESS
183 {
184 DISPATCHER_HEADER Header; /* 000 */
185 LIST_ENTRY ProfileListHead; /* 010 */
186 PHYSICAL_ADDRESS DirectoryTableBase; /* 018 */
187 KGDTENTRY LdtDescriptor; /* 020 */
188 KIDTENTRY Int21Descriptor; /* 028 */
189 USHORT IopmOffset; /* 030 */
190 UCHAR Iopl; /* 032 */
191 UCHAR Unused; /* 033 */
192 ULONG ActiveProcessors; /* 034 */
193 ULONG KernelTime; /* 038 */
194 ULONG UserTime; /* 03C */
195 LIST_ENTRY ReadyListHead; /* 040 */
196 LIST_ENTRY SwapListEntry; /* 048 */
197 PVOID VdmTrapcHandler; /* 04C */
198 LIST_ENTRY ThreadListHead; /* 050 */
199 KSPIN_LOCK ProcessLock; /* 058 */
200 KAFFINITY Affinity; /* 05C */
201 union {
202 struct {
203 ULONG AutoAlignment:1; /* 060.0 */
204 ULONG DisableBoost:1; /* 060.1 */
205 ULONG DisableQuantum:1; /* 060.2 */
206 ULONG ReservedFlags:29; /* 060.3 */
207 };
208 ULONG ProcessFlags; /* 060 */
209 };
210 CHAR BasePriority; /* 064 */
211 CHAR QuantumReset; /* 065 */
212 UCHAR State; /* 066 */
213 UCHAR ThreadSeed; /* 067 */
214 UCHAR PowerState; /* 068 */
215 UCHAR IdealNode; /* 069 */
216 UCHAR Visited; /* 06A */
217 KEXECUTE_OPTIONS Flags; /* 06B */
218 ULONG StackCount; /* 06C */
219 LIST_ENTRY ProcessListEntry; /* 070 */
220 } KPROCESS;
221
222 /* INTERNAL KERNEL FUNCTIONS ************************************************/
223
224 #ifdef __USE_W32API
225 struct _KPROCESS* KeGetCurrentProcess(VOID);
226 VOID KeSetGdtSelector(ULONG Entry, ULONG Value1, ULONG Value2);
227 #endif
228
229 struct _KIRQ_TRAPFRAME;
230 struct _KPCR;
231 struct _KPRCB;
232 struct _KEXCEPTION_FRAME;
233
234 #define IPI_REQUEST_FUNCTIONCALL 0
235 #define IPI_REQUEST_APC 1
236 #define IPI_REQUEST_DPC 2
237 #define IPI_REQUEST_FREEZE 3
238
239 #ifndef __USE_W32API
240 typedef enum _KTHREAD_STATE {
241 Initialized,
242 Ready,
243 Running,
244 Standby,
245 Terminated,
246 Waiting,
247 Transition,
248 DeferredReady,
249 } THREAD_STATE, *PTHREAD_STATE;
250 #endif
251
252 /* MACROS *************************************************************************/
253
254 #define KeEnterCriticalRegion(X) \
255 { \
256 PKTHREAD _Thread = KeGetCurrentThread(); \
257 if (_Thread) _Thread->KernelApcDisable--; \
258 }
259
260 #define KeLeaveCriticalRegion(X) \
261 { \
262 PKTHREAD _Thread = KeGetCurrentThread(); \
263 if((_Thread) && (++_Thread->KernelApcDisable == 0)) \
264 { \
265 if (!IsListEmpty(&_Thread->ApcState.ApcListHead[KernelMode])) \
266 { \
267 KiKernelApcDeliveryCheck(); \
268 } \
269 } \
270 }
271
272 #ifndef __USE_W32API
273 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->Number)
274 #endif
275
276 /* threadsch.c ********************************************************************/
277
278 /* Thread Scheduler Functions */
279
280 /* Readies a Thread for Execution. */
281 VOID
282 STDCALL
283 KiDispatchThreadNoLock(ULONG NewThreadStatus);
284
285 /* Readies a Thread for Execution. */
286 VOID
287 STDCALL
288 KiDispatchThread(ULONG NewThreadStatus);
289
290 /* Puts a Thread into a block state. */
291 VOID
292 STDCALL
293 KiBlockThread(PNTSTATUS Status,
294 UCHAR Alertable,
295 ULONG WaitMode,
296 UCHAR WaitReason);
297
298 /* Removes a thread out of a block state. */
299 VOID
300 STDCALL
301 KiUnblockThread(PKTHREAD Thread,
302 PNTSTATUS WaitStatus,
303 KPRIORITY Increment);
304
305 NTSTATUS
306 STDCALL
307 KeSuspendThread(PKTHREAD Thread);
308
309 NTSTATUS
310 FASTCALL
311 KiSwapContext(PKTHREAD NewThread);
312
313 /* gmutex.c ********************************************************************/
314
315 VOID
316 FASTCALL
317 KiAcquireGuardedMutexContented(PKGUARDED_MUTEX GuardedMutex);
318
319 /* gate.c **********************************************************************/
320
321 VOID
322 FASTCALL
323 KeInitializeGate(PKGATE Gate);
324
325 VOID
326 FASTCALL
327 KeSignalGateBoostPriority(PKGATE Gate);
328
329 VOID
330 FASTCALL
331 KeWaitForGate(PKGATE Gate,
332 KWAIT_REASON WaitReason,
333 KPROCESSOR_MODE WaitMode);
334
335 /* ipi.c ********************************************************************/
336
337 BOOLEAN STDCALL
338 KiIpiServiceRoutine(IN PKTRAP_FRAME TrapFrame,
339 IN struct _KEXCEPTION_FRAME* ExceptionFrame);
340
341 VOID
342 KiIpiSendRequest(ULONG TargetSet,
343 ULONG IpiRequest);
344
345 VOID
346 KeIpiGenericCall(VOID (STDCALL *WorkerRoutine)(PVOID),
347 PVOID Argument);
348
349 /* next file ***************************************************************/
350
351 typedef struct _KPROFILE_SOURCE_OBJECT {
352 KPROFILE_SOURCE Source;
353 LIST_ENTRY ListEntry;
354 } KPROFILE_SOURCE_OBJECT, *PKPROFILE_SOURCE_OBJECT;
355
356 typedef struct _KPROFILE {
357 CSHORT Type;
358 CSHORT Size;
359 LIST_ENTRY ListEntry;
360 PVOID RegionStart;
361 PVOID RegionEnd;
362 ULONG BucketShift;
363 PVOID Buffer;
364 CSHORT Source;
365 ULONG Affinity;
366 BOOLEAN Active;
367 struct _KPROCESS *Process;
368 } KPROFILE, *PKPROFILE;
369
370 /* Cached modules from the loader block */
371 typedef enum _CACHED_MODULE_TYPE {
372 AnsiCodepage,
373 OemCodepage,
374 UnicodeCasemap,
375 SystemRegistry,
376 HardwareRegistry,
377 MaximumCachedModuleType,
378 } CACHED_MODULE_TYPE, *PCACHED_MODULE_TYPE;
379 extern PLOADER_MODULE CachedModules[MaximumCachedModuleType];
380
381 VOID STDCALL
382 DbgBreakPointNoBugCheck(VOID);
383
384 STDCALL
385 VOID
386 KeInitializeProfile(struct _KPROFILE* Profile,
387 struct _KPROCESS* Process,
388 PVOID ImageBase,
389 ULONG ImageSize,
390 ULONG BucketSize,
391 KPROFILE_SOURCE ProfileSource,
392 KAFFINITY Affinity);
393
394 STDCALL
395 VOID
396 KeStartProfile(struct _KPROFILE* Profile,
397 PVOID Buffer);
398
399 STDCALL
400 VOID
401 KeStopProfile(struct _KPROFILE* Profile);
402
403 STDCALL
404 ULONG
405 KeQueryIntervalProfile(KPROFILE_SOURCE ProfileSource);
406
407 STDCALL
408 VOID
409 KeSetIntervalProfile(KPROFILE_SOURCE ProfileSource,
410 ULONG Interval);
411
412 VOID
413 STDCALL
414 KeProfileInterrupt(
415 PKTRAP_FRAME TrapFrame
416 );
417
418 VOID
419 STDCALL
420 KeProfileInterruptWithSource(
421 IN PKTRAP_FRAME TrapFrame,
422 IN KPROFILE_SOURCE Source
423 );
424
425 BOOLEAN
426 STDCALL
427 KiRosPrintAddress(PVOID Address);
428
429 VOID STDCALL KeUpdateSystemTime(PKTRAP_FRAME TrapFrame, KIRQL Irql);
430 VOID STDCALL KeUpdateRunTime(PKTRAP_FRAME TrapFrame, KIRQL Irql);
431
432 VOID STDCALL KiExpireTimers(PKDPC Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2);
433
434 KIRQL inline FASTCALL KeAcquireDispatcherDatabaseLock(VOID);
435 VOID inline FASTCALL KeAcquireDispatcherDatabaseLockAtDpcLevel(VOID);
436 VOID inline FASTCALL KeReleaseDispatcherDatabaseLock(KIRQL Irql);
437 VOID inline FASTCALL KeReleaseDispatcherDatabaseLockFromDpcLevel(VOID);
438
439 VOID
440 STDCALL
441 KeInitializeThread(struct _KPROCESS* Process,
442 PKTHREAD Thread,
443 PKSYSTEM_ROUTINE SystemRoutine,
444 PKSTART_ROUTINE StartRoutine,
445 PVOID StartContext,
446 PCONTEXT Context,
447 PVOID Teb,
448 PVOID KernelStack);
449
450 VOID
451 STDCALL
452 KeRundownThread(VOID);
453
454 NTSTATUS KeReleaseThread(PKTHREAD Thread);
455
456 VOID
457 STDCALL
458 KeStackAttachProcess (
459 IN struct _KPROCESS* Process,
460 OUT PKAPC_STATE ApcState
461 );
462
463 VOID
464 STDCALL
465 KeUnstackDetachProcess (
466 IN PKAPC_STATE ApcState
467 );
468
469 BOOLEAN KiDispatcherObjectWake(DISPATCHER_HEADER* hdr, KPRIORITY increment);
470 VOID STDCALL KeExpireTimers(PKDPC Apc,
471 PVOID Arg1,
472 PVOID Arg2,
473 PVOID Arg3);
474 VOID inline FASTCALL KeInitializeDispatcherHeader(DISPATCHER_HEADER* Header, ULONG Type,
475 ULONG Size, ULONG SignalState);
476 VOID KeDumpStackFrames(PULONG Frame);
477 BOOLEAN KiTestAlert(VOID);
478
479 VOID
480 FASTCALL
481 KiAbortWaitThread(PKTHREAD Thread,
482 NTSTATUS WaitStatus,
483 KPRIORITY Increment);
484
485 VOID
486 STDCALL
487 KeInitializeProcess(struct _KPROCESS *Process,
488 KPRIORITY Priority,
489 KAFFINITY Affinity,
490 LARGE_INTEGER DirectoryTableBase);
491
492 ULONG
493 STDCALL
494 KeForceResumeThread(IN PKTHREAD Thread);
495
496 BOOLEAN STDCALL KiInsertTimer(PKTIMER Timer, LARGE_INTEGER DueTime);
497
498 VOID inline FASTCALL KiSatisfyObjectWait(PDISPATCHER_HEADER Object, PKTHREAD Thread);
499
500 BOOLEAN inline FASTCALL KiIsObjectSignaled(PDISPATCHER_HEADER Object, PKTHREAD Thread);
501
502 VOID inline FASTCALL KiSatisifyMultipleObjectWaits(PKWAIT_BLOCK WaitBlock);
503
504 VOID FASTCALL KiWaitTest(PDISPATCHER_HEADER Object, KPRIORITY Increment);
505
506 PULONG KeGetStackTopThread(struct _ETHREAD* Thread);
507 BOOLEAN STDCALL KeContextToTrapFrame(PCONTEXT Context, PKTRAP_FRAME TrapFrame);
508 VOID STDCALL KiDeliverApc(KPROCESSOR_MODE PreviousMode,
509 PVOID Reserved,
510 PKTRAP_FRAME TrapFrame);
511 VOID
512 STDCALL
513 KiKernelApcDeliveryCheck(VOID);
514 LONG
515 STDCALL
516 KiInsertQueue(IN PKQUEUE Queue,
517 IN PLIST_ENTRY Entry,
518 BOOLEAN Head);
519
520 ULONG
521 STDCALL
522 KeSetProcess(struct _KPROCESS* Process,
523 KPRIORITY Increment);
524
525
526 VOID STDCALL KeInitializeEventPair(PKEVENT_PAIR EventPair);
527
528 VOID STDCALL KiInitializeUserApc(IN PVOID Reserved,
529 IN PKTRAP_FRAME TrapFrame,
530 IN PKNORMAL_ROUTINE NormalRoutine,
531 IN PVOID NormalContext,
532 IN PVOID SystemArgument1,
533 IN PVOID SystemArgument2);
534
535 VOID STDCALL KiAttachProcess(struct _KTHREAD *Thread, struct _KPROCESS *Process, KIRQL ApcLock, struct _KAPC_STATE *SavedApcState);
536
537 VOID STDCALL KiSwapProcess(struct _KPROCESS *NewProcess, struct _KPROCESS *OldProcess);
538
539 BOOLEAN
540 STDCALL
541 KeTestAlertThread(IN KPROCESSOR_MODE AlertMode);
542
543 BOOLEAN STDCALL KeRemoveQueueApc (PKAPC Apc);
544 VOID FASTCALL KiWakeQueue(IN PKQUEUE Queue);
545 PLIST_ENTRY STDCALL KeRundownQueue(IN PKQUEUE Queue);
546
547 extern LARGE_INTEGER SystemBootTime;
548
549 /* INITIALIZATION FUNCTIONS *************************************************/
550
551 VOID KeInitExceptions(VOID);
552 VOID KeInitInterrupts(VOID);
553 VOID KeInitTimer(VOID);
554 VOID KeInitDpc(struct _KPRCB* Prcb);
555 VOID KeInitDispatcher(VOID);
556 VOID inline FASTCALL KeInitializeDispatcher(VOID);
557 VOID KiInitializeSystemClock(VOID);
558 VOID KiInitializeBugCheck(VOID);
559 VOID Phase1Initialization(PVOID Context);
560
561 VOID KeInit1(PCHAR CommandLine, PULONG LastKernelAddress);
562 VOID KeInit2(VOID);
563
564 BOOLEAN KiDeliverUserApc(PKTRAP_FRAME TrapFrame);
565
566 VOID
567 STDCALL
568 KiMoveApcState (PKAPC_STATE OldState,
569 PKAPC_STATE NewState);
570
571 VOID
572 KiAddProfileEvent(KPROFILE_SOURCE Source, ULONG Pc);
573 VOID
574 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord,
575 PCONTEXT Context,
576 PKTRAP_FRAME Tf,
577 KPROCESSOR_MODE PreviousMode,
578 BOOLEAN SearchFrames);
579 VOID KeTrapFrameToContext(PKTRAP_FRAME TrapFrame,
580 PCONTEXT Context);
581 VOID
582 KeApplicationProcessorInit(VOID);
583 VOID
584 KePrepareForApplicationProcessorInit(ULONG id);
585 ULONG
586 KiUserTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr, PVOID Cr2);
587 VOID STDCALL
588 KePushAndStackSwitchAndSysRet(ULONG Push, PVOID NewStack);
589 VOID STDCALL
590 KeStackSwitchAndRet(PVOID NewStack);
591 VOID STDCALL
592 KeBugCheckWithTf(ULONG BugCheckCode,
593 ULONG BugCheckParameter1,
594 ULONG BugCheckParameter2,
595 ULONG BugCheckParameter3,
596 ULONG BugCheckParameter4,
597 PKTRAP_FRAME Tf);
598 #define KEBUGCHECKWITHTF(a,b,c,d,e,f) DbgPrint("KeBugCheckWithTf at %s:%i\n",__FILE__,__LINE__), KeBugCheckWithTf(a,b,c,d,e,f)
599 VOID
600 KiDumpTrapFrame(PKTRAP_FRAME Tf, ULONG ExceptionNr, ULONG cr2);
601
602 VOID
603 STDCALL
604 KeFlushCurrentTb(VOID);
605
606 VOID
607 KiSetSystemTime(PLARGE_INTEGER NewSystemTime);
608
609 #endif /* not __ASM__ */
610
611 #define MAXIMUM_PROCESSORS 32
612
613 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_KE_H */