5f4a6b37fb5fd5aa874dc5b8e77baaef34f0e7e3
[reactos.git] / reactos / ntoskrnl / include / internal / ke.h
1 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_KE_H
2 #define __NTOSKRNL_INCLUDE_INTERNAL_KE_H
3
4 /* INCLUDES *****************************************************************/
5
6 #include "arch/ke.h"
7
8 /* INTERNAL KERNEL TYPES ****************************************************/
9
10 typedef struct _WOW64_PROCESS
11 {
12 PVOID Wow64;
13 } WOW64_PROCESS, *PWOW64_PROCESS;
14
15 typedef struct _KPROFILE_SOURCE_OBJECT
16 {
17 KPROFILE_SOURCE Source;
18 LIST_ENTRY ListEntry;
19 } KPROFILE_SOURCE_OBJECT, *PKPROFILE_SOURCE_OBJECT;
20
21 /* Cached modules from the loader block */
22 typedef enum _CACHED_MODULE_TYPE
23 {
24 AnsiCodepage,
25 OemCodepage,
26 UnicodeCasemap,
27 SystemRegistry,
28 HardwareRegistry,
29 MaximumCachedModuleType,
30 } CACHED_MODULE_TYPE, *PCACHED_MODULE_TYPE;
31 extern PLOADER_MODULE CachedModules[MaximumCachedModuleType];
32
33 struct _KIRQ_TRAPFRAME;
34 struct _KPCR;
35 struct _KPRCB;
36 struct _KEXCEPTION_FRAME;
37
38 extern PVOID KeUserApcDispatcher;
39 extern PVOID KeUserCallbackDispatcher;
40 extern PVOID KeUserExceptionDispatcher;
41 extern PVOID KeRaiseUserExceptionDispatcher;
42 extern LARGE_INTEGER SystemBootTime;
43 extern ULONG_PTR KERNEL_BASE;
44 extern ULONG KeI386NpxPresent;
45 extern ULONG KeI386XMMIPresent;
46 extern ULONG KeI386FxsrPresent;
47
48 /* MACROS *************************************************************************/
49
50 /*
51 * On UP machines, we don't actually have a spinlock, we merely raise
52 * IRQL to DPC level.
53 */
54 #ifdef CONFIG_SMP
55 #define KeInitializeDispatcher() KeInitializeSpinLock(&DispatcherDatabaseLock);
56 #define KeAcquireDispatcherDatabaseLock() KfAcquireSpinLock(&DispatcherDatabaseLock);
57 #define KeAcquireDispatcherDatabaseLockAtDpcLevel() \
58 KeAcquireSpinLockAtDpcLevel (&DispatcherDatabaseLock);
59 #define KeReleaseDispatcherDatabaseLockFromDpcLevel() \
60 KeReleaseSpinLockFromDpcLevel(&DispatcherDatabaseLock);
61 #define KeReleaseDispatcherDatabaseLock(OldIrql) \
62 KiExitDispatcher(OldIrql);
63 #else
64 #define KeInitializeDispatcher()
65 #define KeAcquireDispatcherDatabaseLock() KeRaiseIrqlToDpcLevel();
66 #define KeReleaseDispatcherDatabaseLock(OldIrql) KiExitDispatcher(OldIrql);
67 #define KeAcquireDispatcherDatabaseLockAtDpcLevel()
68 #define KeReleaseDispatcherDatabaseLockFromDpcLevel()
69 #endif
70
71 /* The following macro initializes a dispatcher object's header */
72 #define KeInitializeDispatcherHeader(Header, t, s, State) \
73 { \
74 (Header)->Type = t; \
75 (Header)->Absolute = 0; \
76 (Header)->Inserted = 0; \
77 (Header)->Size = s; \
78 (Header)->SignalState = State; \
79 InitializeListHead(&((Header)->WaitListHead)); \
80 }
81
82 /* The following macro satisfies the wait of any dispatcher object */
83 #define KiSatisfyObjectWait(Object, Thread) \
84 { \
85 /* Special case for Mutants */ \
86 if ((Object)->Header.Type == MutantObject) \
87 { \
88 /* Decrease the Signal State */ \
89 (Object)->Header.SignalState--; \
90 \
91 /* Check if it's now non-signaled */ \
92 if (!(Object)->Header.SignalState) \
93 { \
94 /* Set the Owner Thread */ \
95 (Object)->OwnerThread = Thread; \
96 \
97 /* Disable APCs if needed */ \
98 Thread->KernelApcDisable -= (Object)->ApcDisable; \
99 \
100 /* Check if it's abandoned */ \
101 if ((Object)->Abandoned) \
102 { \
103 /* Unabandon it */ \
104 (Object)->Abandoned = FALSE; \
105 \
106 /* Return Status */ \
107 Thread->WaitStatus = STATUS_ABANDONED; \
108 } \
109 \
110 /* Insert it into the Mutant List */ \
111 InsertHeadList(&Thread->MutantListHead, \
112 &(Object)->MutantListEntry); \
113 } \
114 } \
115 else if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
116 EventSynchronizationObject) \
117 { \
118 /* Synchronization Timers and Events just get un-signaled */ \
119 (Object)->Header.SignalState = 0; \
120 } \
121 else if ((Object)->Header.Type == SemaphoreObject) \
122 { \
123 /* These ones can have multiple states, so we only decrease it */ \
124 (Object)->Header.SignalState--; \
125 } \
126 }
127
128 /* The following macro satisfies the wait of a mutant dispatcher object */
129 #define KiSatisfyMutantWait(Object, Thread) \
130 { \
131 /* Decrease the Signal State */ \
132 (Object)->Header.SignalState--; \
133 \
134 /* Check if it's now non-signaled */ \
135 if (!(Object)->Header.SignalState) \
136 { \
137 /* Set the Owner Thread */ \
138 (Object)->OwnerThread = Thread; \
139 \
140 /* Disable APCs if needed */ \
141 Thread->KernelApcDisable -= (Object)->ApcDisable; \
142 \
143 /* Check if it's abandoned */ \
144 if ((Object)->Abandoned) \
145 { \
146 /* Unabandon it */ \
147 (Object)->Abandoned = FALSE; \
148 \
149 /* Return Status */ \
150 Thread->WaitStatus = STATUS_ABANDONED; \
151 } \
152 \
153 /* Insert it into the Mutant List */ \
154 InsertHeadList(&Thread->MutantListHead, \
155 &(Object)->MutantListEntry); \
156 } \
157 }
158
159 /* The following macro satisfies the wait of any nonmutant dispatcher object */
160 #define KiSatisfyNonMutantWait(Object, Thread) \
161 { \
162 if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
163 EventSynchronizationObject) \
164 { \
165 /* Synchronization Timers and Events just get un-signaled */ \
166 (Object)->Header.SignalState = 0; \
167 } \
168 else if ((Object)->Header.Type == SemaphoreObject) \
169 { \
170 /* These ones can have multiple states, so we only decrease it */ \
171 (Object)->Header.SignalState--; \
172 } \
173 }
174
175 extern KSPIN_LOCK DispatcherDatabaseLock;
176
177 #define KeEnterCriticalRegion() \
178 { \
179 PKTHREAD _Thread = KeGetCurrentThread(); \
180 if (_Thread) _Thread->KernelApcDisable--; \
181 }
182
183 #define KeLeaveCriticalRegion() \
184 { \
185 PKTHREAD _Thread = KeGetCurrentThread(); \
186 if((_Thread) && (++_Thread->KernelApcDisable == 0)) \
187 { \
188 if (!IsListEmpty(&_Thread->ApcState.ApcListHead[KernelMode]) && \
189 (_Thread->SpecialApcDisable == 0)) \
190 { \
191 KiCheckForKernelApcDelivery(); \
192 } \
193 } \
194 }
195
196 #define KEBUGCHECKWITHTF(a,b,c,d,e,f) \
197 DbgPrint("KeBugCheckWithTf at %s:%i\n",__FILE__,__LINE__), \
198 KeBugCheckWithTf(a,b,c,d,e,f)
199
200 /* Tells us if the Timer or Event is a Syncronization or Notification Object */
201 #define TIMER_OR_EVENT_TYPE 0x7L
202
203 /* One of the Reserved Wait Blocks, this one is for the Thread's Timer */
204 #define TIMER_WAIT_BLOCK 0x3L
205
206 /* INTERNAL KERNEL FUNCTIONS ************************************************/
207
208 /* threadsch.c ********************************************************************/
209
210 /* Thread Scheduler Functions */
211
212 /* Readies a Thread for Execution. */
213 VOID
214 STDCALL
215 KiDispatchThreadNoLock(ULONG NewThreadStatus);
216
217 /* Readies a Thread for Execution. */
218 VOID
219 STDCALL
220 KiDispatchThread(ULONG NewThreadStatus);
221
222 /* Finds a new thread to run */
223 NTSTATUS
224 NTAPI
225 KiSwapThread(
226 VOID
227 );
228
229 /* Removes a thread out of a block state. */
230 VOID
231 STDCALL
232 KiUnblockThread(
233 PKTHREAD Thread,
234 PNTSTATUS WaitStatus,
235 KPRIORITY Increment
236 );
237
238 NTSTATUS
239 STDCALL
240 KeSuspendThread(PKTHREAD Thread);
241
242 NTSTATUS
243 FASTCALL
244 KiSwapContext(PKTHREAD NewThread);
245
246 VOID
247 STDCALL
248 KiAdjustQuantumThread(IN PKTHREAD Thread);
249
250 VOID
251 FASTCALL
252 KiExitDispatcher(KIRQL OldIrql);
253
254 /* gmutex.c ********************************************************************/
255
256 VOID
257 FASTCALL
258 KiAcquireGuardedMutexContented(PKGUARDED_MUTEX GuardedMutex);
259
260 /* gate.c **********************************************************************/
261
262 VOID
263 FASTCALL
264 KeInitializeGate(PKGATE Gate);
265
266 VOID
267 FASTCALL
268 KeSignalGateBoostPriority(PKGATE Gate);
269
270 VOID
271 FASTCALL
272 KeWaitForGate(
273 PKGATE Gate,
274 KWAIT_REASON WaitReason,
275 KPROCESSOR_MODE WaitMode
276 );
277
278 /* ipi.c ********************************************************************/
279
280 BOOLEAN
281 STDCALL
282 KiIpiServiceRoutine(
283 IN PKTRAP_FRAME TrapFrame,
284 IN struct _KEXCEPTION_FRAME* ExceptionFrame
285 );
286
287 VOID
288 NTAPI
289 KiIpiSendRequest(
290 KAFFINITY TargetSet,
291 ULONG IpiRequest
292 );
293
294 VOID
295 NTAPI
296 KeIpiGenericCall(
297 VOID (STDCALL *WorkerRoutine)(PVOID),
298 PVOID Argument
299 );
300
301 /* next file ***************************************************************/
302
303 VOID
304 STDCALL
305 DbgBreakPointNoBugCheck(VOID);
306
307 VOID
308 STDCALL
309 KeInitializeProfile(
310 struct _KPROFILE* Profile,
311 struct _KPROCESS* Process,
312 PVOID ImageBase,
313 ULONG ImageSize,
314 ULONG BucketSize,
315 KPROFILE_SOURCE ProfileSource,
316 KAFFINITY Affinity
317 );
318
319 VOID
320 STDCALL
321 KeStartProfile(
322 struct _KPROFILE* Profile,
323 PVOID Buffer
324 );
325
326 BOOLEAN
327 STDCALL
328 KeStopProfile(struct _KPROFILE* Profile);
329
330 ULONG
331 STDCALL
332 KeQueryIntervalProfile(KPROFILE_SOURCE ProfileSource);
333
334 VOID
335 STDCALL
336 KeSetIntervalProfile(
337 KPROFILE_SOURCE ProfileSource,
338 ULONG Interval
339 );
340
341 VOID
342 STDCALL
343 KeProfileInterrupt(
344 PKTRAP_FRAME TrapFrame
345 );
346
347 VOID
348 STDCALL
349 KeProfileInterruptWithSource(
350 IN PKTRAP_FRAME TrapFrame,
351 IN KPROFILE_SOURCE Source
352 );
353
354 BOOLEAN
355 STDCALL
356 KiRosPrintAddress(PVOID Address);
357
358 VOID
359 STDCALL
360 KeUpdateSystemTime(
361 PKTRAP_FRAME TrapFrame,
362 KIRQL Irql
363 );
364
365 VOID
366 STDCALL
367 KeUpdateRunTime(
368 PKTRAP_FRAME TrapFrame,
369 KIRQL Irql
370 );
371
372 VOID
373 STDCALL
374 KiExpireTimers(
375 PKDPC Dpc,
376 PVOID DeferredContext,
377 PVOID SystemArgument1,
378 PVOID SystemArgument2
379 );
380
381 VOID
382 STDCALL
383 KeInitializeThread(
384 struct _KPROCESS* Process,
385 PKTHREAD Thread,
386 PKSYSTEM_ROUTINE SystemRoutine,
387 PKSTART_ROUTINE StartRoutine,
388 PVOID StartContext,
389 PCONTEXT Context,
390 PVOID Teb,
391 PVOID KernelStack
392 );
393
394 VOID
395 STDCALL
396 KeRundownThread(VOID);
397
398 NTSTATUS
399 NTAPI
400 KeReleaseThread(PKTHREAD Thread);
401
402 LONG
403 STDCALL
404 KeQueryBasePriorityThread(IN PKTHREAD Thread);
405
406 VOID
407 STDCALL
408 KiSetPriorityThread(
409 PKTHREAD Thread,
410 KPRIORITY Priority,
411 PBOOLEAN Released
412 );
413
414 BOOLEAN
415 NTAPI
416 KiDispatcherObjectWake(
417 DISPATCHER_HEADER* hdr,
418 KPRIORITY increment
419 );
420
421 VOID
422 STDCALL
423 KeExpireTimers(
424 PKDPC Apc,
425 PVOID Arg1,
426 PVOID Arg2,
427 PVOID Arg3
428 );
429
430 VOID
431 NTAPI
432 KeDumpStackFrames(PULONG Frame);
433
434 BOOLEAN
435 NTAPI
436 KiTestAlert(VOID);
437
438 VOID
439 FASTCALL
440 KiAbortWaitThread(
441 PKTHREAD Thread,
442 NTSTATUS WaitStatus,
443 KPRIORITY Increment
444 );
445
446 VOID
447 STDCALL
448 KeInitializeProcess(
449 struct _KPROCESS *Process,
450 KPRIORITY Priority,
451 KAFFINITY Affinity,
452 LARGE_INTEGER DirectoryTableBase
453 );
454
455 ULONG
456 STDCALL
457 KeForceResumeThread(IN PKTHREAD Thread);
458
459 BOOLEAN
460 STDCALL
461 KeDisableThreadApcQueueing(IN PKTHREAD Thread);
462
463 BOOLEAN
464 STDCALL
465 KiInsertTimer(
466 PKTIMER Timer,
467 LARGE_INTEGER DueTime
468 );
469
470 VOID
471 FASTCALL
472 KiWaitTest(
473 PVOID Object,
474 KPRIORITY Increment
475 );
476
477 PULONG
478 NTAPI
479 KeGetStackTopThread(struct _ETHREAD* Thread);
480
481 VOID
482 STDCALL
483 KeContextToTrapFrame(
484 PCONTEXT Context,
485 PKEXCEPTION_FRAME ExeptionFrame,
486 PKTRAP_FRAME TrapFrame,
487 ULONG ContextFlags,
488 KPROCESSOR_MODE PreviousMode
489 );
490
491 VOID
492 STDCALL
493 KiDeliverApc(
494 KPROCESSOR_MODE PreviousMode,
495 PVOID Reserved,
496 PKTRAP_FRAME TrapFrame
497 );
498
499 VOID
500 STDCALL
501 KiCheckForKernelApcDelivery(VOID);
502
503 LONG
504 STDCALL
505 KiInsertQueue(
506 IN PKQUEUE Queue,
507 IN PLIST_ENTRY Entry,
508 BOOLEAN Head
509 );
510
511 ULONG
512 STDCALL
513 KeSetProcess(
514 struct _KPROCESS* Process,
515 KPRIORITY Increment
516 );
517
518 VOID
519 STDCALL
520 KeInitializeEventPair(PKEVENT_PAIR EventPair);
521
522 VOID
523 STDCALL
524 KiInitializeUserApc(
525 IN PKEXCEPTION_FRAME Reserved,
526 IN PKTRAP_FRAME TrapFrame,
527 IN PKNORMAL_ROUTINE NormalRoutine,
528 IN PVOID NormalContext,
529 IN PVOID SystemArgument1,
530 IN PVOID SystemArgument2
531 );
532
533 PLIST_ENTRY
534 STDCALL
535 KeFlushQueueApc(
536 IN PKTHREAD Thread,
537 IN KPROCESSOR_MODE PreviousMode
538 );
539
540 VOID
541 STDCALL
542 KiAttachProcess(
543 struct _KTHREAD *Thread,
544 struct _KPROCESS *Process,
545 KIRQL ApcLock,
546 struct _KAPC_STATE *SavedApcState
547 );
548
549 VOID
550 STDCALL
551 KiSwapProcess(
552 struct _KPROCESS *NewProcess,
553 struct _KPROCESS *OldProcess
554 );
555
556 BOOLEAN
557 STDCALL
558 KeTestAlertThread(IN KPROCESSOR_MODE AlertMode);
559
560 BOOLEAN
561 STDCALL
562 KeRemoveQueueApc(PKAPC Apc);
563
564 VOID
565 FASTCALL
566 KiWakeQueue(IN PKQUEUE Queue);
567
568 /* INITIALIZATION FUNCTIONS *************************************************/
569
570 VOID
571 NTAPI
572 KeInitExceptions(VOID);
573
574 VOID
575 NTAPI
576 KeInitInterrupts(VOID);
577
578 VOID
579 NTAPI
580 KeInitTimer(VOID);
581
582 VOID
583 NTAPI
584 KeInitDpc(struct _KPRCB* Prcb);
585
586 VOID
587 NTAPI
588 KeInitDispatcher(VOID);
589
590 VOID
591 NTAPI
592 KiInitializeSystemClock(VOID);
593
594 VOID
595 NTAPI
596 KiInitializeBugCheck(VOID);
597
598 VOID
599 NTAPI
600 Phase1Initialization(PVOID Context);
601
602 VOID
603 NTAPI
604 KeInit1(
605 PCHAR CommandLine,
606 PULONG LastKernelAddress
607 );
608
609 VOID
610 NTAPI
611 KeInit2(VOID);
612
613 BOOLEAN
614 NTAPI
615 KiDeliverUserApc(PKTRAP_FRAME TrapFrame);
616
617 VOID
618 STDCALL
619 KiMoveApcState(
620 PKAPC_STATE OldState,
621 PKAPC_STATE NewState
622 );
623
624 VOID
625 NTAPI
626 KiAddProfileEvent(
627 KPROFILE_SOURCE Source,
628 ULONG Pc
629 );
630
631 VOID
632 NTAPI
633 KiDispatchException(
634 PEXCEPTION_RECORD ExceptionRecord,
635 PKEXCEPTION_FRAME ExceptionFrame,
636 PKTRAP_FRAME Tf,
637 KPROCESSOR_MODE PreviousMode,
638 BOOLEAN SearchFrames
639 );
640
641 VOID
642 NTAPI
643 KeTrapFrameToContext(
644 IN PKTRAP_FRAME TrapFrame,
645 IN PKEXCEPTION_FRAME ExceptionFrame,
646 IN OUT PCONTEXT Context
647 );
648
649 VOID
650 NTAPI
651 KeApplicationProcessorInit(VOID);
652
653 VOID
654 NTAPI
655 KePrepareForApplicationProcessorInit(ULONG id);
656
657 ULONG
658 NTAPI
659 KiUserTrapHandler(
660 PKTRAP_FRAME Tf,
661 ULONG ExceptionNr,
662 PVOID Cr2
663 );
664
665 VOID
666 STDCALL
667 KePushAndStackSwitchAndSysRet(
668 ULONG Push,
669 PVOID NewStack
670 );
671
672 VOID
673 STDCALL
674 KeStackSwitchAndRet(PVOID NewStack);
675
676 VOID
677 STDCALL
678 KeBugCheckWithTf(
679 ULONG BugCheckCode,
680 ULONG BugCheckParameter1,
681 ULONG BugCheckParameter2,
682 ULONG BugCheckParameter3,
683 ULONG BugCheckParameter4,
684 PKTRAP_FRAME Tf
685 );
686
687 VOID
688 STDCALL
689 KiDumpTrapFrame(
690 PKTRAP_FRAME Tf,
691 ULONG ExceptionNr,
692 ULONG cr2
693 );
694
695 VOID
696 STDCALL
697 KeFlushCurrentTb(VOID);
698
699 VOID
700 STDCALL
701 KeRosDumpStackFrames(
702 PULONG Frame,
703 ULONG FrameCount
704 );
705
706 ULONG
707 STDCALL
708 KeRosGetStackFrames(
709 PULONG Frames,
710 ULONG FrameCount
711 );
712
713 VOID
714 NTAPI
715 KiSetSystemTime(PLARGE_INTEGER NewSystemTime);
716
717 NTSTATUS
718 STDCALL
719 Ke386CallBios(
720 UCHAR Int,
721 PKV86M_REGISTERS Regs
722 );
723
724 ULONG
725 NTAPI
726 KeV86Exception(
727 ULONG ExceptionNr,
728 PKTRAP_FRAME Tf,
729 ULONG address
730 );
731
732 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_KE_H */