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