3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
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.
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.
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.
20 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_KE_H
21 #define __NTOSKRNL_INCLUDE_INTERNAL_KE_H
23 /* INCLUDES *****************************************************************/
26 #include <ddk/ntifs.h>
28 #endif /* not __ASM__ */
32 /* INTERNAL KERNEL TYPES ****************************************************/
38 typedef struct _KPROCESS
*PKPROCESS
;
39 typedef struct _DISPATCHER_HEADER
*PDISPATCHER_HEADER
;
41 #endif /* __USE_W32API */
43 typedef struct _HARDWARE_PTE_X86
{
47 ULONG WriteThrough
: 1;
48 ULONG CacheDisable
: 1;
53 ULONG CopyOnWrite
: 1;
56 ULONG PageFrameNumber
: 20;
57 } HARDWARE_PTE_X86
, *PHARDWARE_PTE_X86
;
59 typedef struct _WOW64_PROCESS
62 } WOW64_PROCESS
, *PWOW64_PROCESS
;
66 typedef struct _KTHREAD
68 /* For waiting on thread exit */
69 DISPATCHER_HEADER DispatcherHeader
; /* 00 */
71 /* List of mutants owned by the thread */
72 LIST_ENTRY MutantListHead
; /* 10 */
73 PVOID InitialStack
; /* 18 */
74 ULONG_PTR StackLimit
; /* 1C */
76 /* Pointer to the thread's environment block in user memory */
77 struct _TEB
*Teb
; /* 20 */
79 /* Pointer to the thread's TLS array */
80 PVOID TlsArray
; /* 24 */
81 PVOID KernelStack
; /* 28 */
82 UCHAR DebugActive
; /* 2C */
84 /* Thread state (one of THREAD_STATE_xxx constants below) */
86 BOOLEAN Alerted
[2]; /* 2E */
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 */
99 PKWAIT_BLOCK WaitBlockList
; /* 58 */
100 PKGATE GateObject
; /* 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 */
112 USHORT KernelApcDisable
;
113 USHORT SpecialApcDisable
;
115 ULONG CombinedApcDisable
; /* D0 */
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 */
160 typedef struct _KEXECUTE_OPTIONS
162 UCHAR ExecuteDisable
:1;
163 UCHAR ExecuteEnable
:1;
164 UCHAR DisableThunkEmulation
:1;
166 UCHAR ExecuteDispatchEnable
:1;
167 UCHAR ImageDispatchEnable
:1;
169 } KEXECUTE_OPTIONS
, *PKEXECUTE_OPTIONS
;
173 * DESCRIPTION: Internal Kernel Process Structure.
174 * PORTABILITY: Architecture Dependent.
175 * KERNEL VERSION: 5.2
176 * DOCUMENTATION: http://reactos.com/wiki/index.php/KPROCESS
178 typedef struct _KPROCESS
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 */
199 ULONG AutoAlignment
:1; /* 060.0 */
200 ULONG DisableBoost
:1; /* 060.1 */
201 ULONG DisableQuantum
:1; /* 060.2 */
202 ULONG ReservedFlags
:29; /* 060.3 */
204 ULONG ProcessFlags
; /* 060 */
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 */
218 /* INTERNAL KERNEL FUNCTIONS ************************************************/
221 struct _KPROCESS
* KeGetCurrentProcess(VOID
);
222 VOID
KeSetGdtSelector(ULONG Entry
, ULONG Value1
, ULONG Value2
);
225 struct _KIRQ_TRAPFRAME
;
228 struct _KEXCEPTION_FRAME
;
230 #define IPI_REQUEST_FUNCTIONCALL 0
231 #define IPI_REQUEST_APC 1
232 #define IPI_REQUEST_DPC 2
233 #define IPI_REQUEST_FREEZE 3
236 typedef enum _KTHREAD_STATE
{
245 } THREAD_STATE
, *PTHREAD_STATE
;
248 /* MACROS *************************************************************************/
250 #define KeEnterCriticalRegion(X) \
252 PKTHREAD _Thread = KeGetCurrentThread(); \
253 if (_Thread) _Thread->KernelApcDisable--; \
256 #define KeLeaveCriticalRegion(X) \
258 PKTHREAD _Thread = KeGetCurrentThread(); \
259 if((_Thread) && (++_Thread->KernelApcDisable == 0)) \
261 if (!IsListEmpty(&_Thread->ApcState.ApcListHead[KernelMode])) \
263 KiKernelApcDeliveryCheck(); \
269 #define KeGetCurrentProcessorNumber() (KeGetCurrentKPCR()->ProcessorNumber)
272 /* threadsch.c ********************************************************************/
274 /* Thread Scheduler Functions */
276 /* Readies a Thread for Execution. */
279 KiDispatchThreadNoLock(ULONG NewThreadStatus
);
281 /* Readies a Thread for Execution. */
284 KiDispatchThread(ULONG NewThreadStatus
);
286 /* Puts a Thread into a block state. */
289 KiBlockThread(PNTSTATUS Status
,
294 /* Removes a thread out of a block state. */
297 KiUnblockThread(PKTHREAD Thread
,
298 PNTSTATUS WaitStatus
,
299 KPRIORITY Increment
);
303 KeSuspendThread(PKTHREAD Thread
);
307 KiSwapContext(PKTHREAD NewThread
);
309 /* gmutex.c ********************************************************************/
313 KiAcquireGuardedMutexContented(PKGUARDED_MUTEX GuardedMutex
);
315 /* gate.c **********************************************************************/
319 KeInitializeGate(PKGATE Gate
);
323 KeSignalGateBoostPriority(PKGATE Gate
);
327 KeWaitForGate(PKGATE Gate
,
328 KWAIT_REASON WaitReason
,
329 KPROCESSOR_MODE WaitMode
);
331 /* ipi.c ********************************************************************/
334 KiIpiServiceRoutine(IN PKTRAP_FRAME TrapFrame
,
335 IN
struct _KEXCEPTION_FRAME
* ExceptionFrame
);
338 KiIpiSendRequest(ULONG TargetSet
,
342 KeIpiGenericCall(VOID (STDCALL
*WorkerRoutine
)(PVOID
),
345 /* next file ***************************************************************/
347 typedef struct _KPROFILE_SOURCE_OBJECT
{
348 KPROFILE_SOURCE Source
;
349 LIST_ENTRY ListEntry
;
350 } KPROFILE_SOURCE_OBJECT
, *PKPROFILE_SOURCE_OBJECT
;
352 typedef struct _KPROFILE
{
355 LIST_ENTRY ListEntry
;
363 struct _KPROCESS
*Process
;
364 } KPROFILE
, *PKPROFILE
;
366 /* Cached modules from the loader block */
367 typedef enum _CACHED_MODULE_TYPE
{
373 MaximumCachedModuleType
,
374 } CACHED_MODULE_TYPE
, *PCACHED_MODULE_TYPE
;
375 extern PLOADER_MODULE CachedModules
[MaximumCachedModuleType
];
378 DbgBreakPointNoBugCheck(VOID
);
382 KeInitializeProfile(struct _KPROFILE
* Profile
,
383 struct _KPROCESS
* Process
,
387 KPROFILE_SOURCE ProfileSource
,
392 KeStartProfile(struct _KPROFILE
* Profile
,
397 KeStopProfile(struct _KPROFILE
* Profile
);
401 KeQueryIntervalProfile(KPROFILE_SOURCE ProfileSource
);
405 KeSetIntervalProfile(KPROFILE_SOURCE ProfileSource
,
411 PKTRAP_FRAME TrapFrame
416 KeProfileInterruptWithSource(
417 IN PKTRAP_FRAME TrapFrame
,
418 IN KPROFILE_SOURCE Source
423 KiRosPrintAddress(PVOID Address
);
425 VOID STDCALL
KeUpdateSystemTime(PKTRAP_FRAME TrapFrame
, KIRQL Irql
);
426 VOID STDCALL
KeUpdateRunTime(PKTRAP_FRAME TrapFrame
, KIRQL Irql
);
428 VOID STDCALL
KiExpireTimers(PKDPC Dpc
, PVOID DeferredContext
, PVOID SystemArgument1
, PVOID SystemArgument2
);
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
);
437 KeInitializeThread(struct _KPROCESS
* Process
,
439 PKSYSTEM_ROUTINE SystemRoutine
,
440 PKSTART_ROUTINE StartRoutine
,
448 KeRundownThread(VOID
);
450 NTSTATUS
KeReleaseThread(PKTHREAD Thread
);
454 KeStackAttachProcess (
455 IN
struct _KPROCESS
* Process
,
456 OUT PKAPC_STATE ApcState
461 KeUnstackDetachProcess (
462 IN PKAPC_STATE ApcState
465 BOOLEAN
KiDispatcherObjectWake(DISPATCHER_HEADER
* hdr
, KPRIORITY increment
);
466 VOID STDCALL
KeExpireTimers(PKDPC Apc
,
470 VOID
inline FASTCALL
KeInitializeDispatcherHeader(DISPATCHER_HEADER
* Header
, ULONG Type
,
471 ULONG Size
, ULONG SignalState
);
472 VOID
KeDumpStackFrames(PULONG Frame
);
473 BOOLEAN
KiTestAlert(VOID
);
477 KiAbortWaitThread(PKTHREAD Thread
,
479 KPRIORITY Increment
);
483 KeInitializeProcess(struct _KPROCESS
*Process
,
486 LARGE_INTEGER DirectoryTableBase
);
490 KeForceResumeThread(IN PKTHREAD Thread
);
492 BOOLEAN STDCALL
KiInsertTimer(PKTIMER Timer
, LARGE_INTEGER DueTime
);
494 VOID
inline FASTCALL
KiSatisfyObjectWait(PDISPATCHER_HEADER Object
, PKTHREAD Thread
);
496 BOOLEAN
inline FASTCALL
KiIsObjectSignaled(PDISPATCHER_HEADER Object
, PKTHREAD Thread
);
498 VOID
inline FASTCALL
KiSatisifyMultipleObjectWaits(PKWAIT_BLOCK WaitBlock
);
500 VOID FASTCALL
KiWaitTest(PDISPATCHER_HEADER Object
, KPRIORITY Increment
);
502 PULONG
KeGetStackTopThread(struct _ETHREAD
* Thread
);
503 BOOLEAN STDCALL
KeContextToTrapFrame(PCONTEXT Context
, PKTRAP_FRAME TrapFrame
);
504 VOID STDCALL
KiDeliverApc(KPROCESSOR_MODE PreviousMode
,
506 PKTRAP_FRAME TrapFrame
);
509 KiKernelApcDeliveryCheck(VOID
);
512 KiInsertQueue(IN PKQUEUE Queue
,
513 IN PLIST_ENTRY Entry
,
518 KeSetProcess(struct _KPROCESS
* Process
,
519 KPRIORITY Increment
);
522 VOID STDCALL
KeInitializeEventPair(PKEVENT_PAIR EventPair
);
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
);
531 VOID STDCALL
KiAttachProcess(struct _KTHREAD
*Thread
, struct _KPROCESS
*Process
, KIRQL ApcLock
, struct _KAPC_STATE
*SavedApcState
);
533 VOID STDCALL
KiSwapProcess(struct _KPROCESS
*NewProcess
, struct _KPROCESS
*OldProcess
);
537 KeTestAlertThread(IN KPROCESSOR_MODE AlertMode
);
539 BOOLEAN STDCALL
KeRemoveQueueApc (PKAPC Apc
);
540 VOID FASTCALL
KiWakeQueue(IN PKQUEUE Queue
);
541 PLIST_ENTRY STDCALL
KeRundownQueue(IN PKQUEUE Queue
);
543 extern LARGE_INTEGER SystemBootTime
;
545 /* INITIALIZATION FUNCTIONS *************************************************/
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
);
557 VOID
KeInit1(PCHAR CommandLine
, PULONG LastKernelAddress
);
560 BOOLEAN
KiDeliverUserApc(PKTRAP_FRAME TrapFrame
);
564 KiMoveApcState (PKAPC_STATE OldState
,
565 PKAPC_STATE NewState
);
568 KiAddProfileEvent(KPROFILE_SOURCE Source
, ULONG Pc
);
570 KiDispatchException(PEXCEPTION_RECORD ExceptionRecord
,
573 KPROCESSOR_MODE PreviousMode
,
574 BOOLEAN SearchFrames
);
575 VOID
KeTrapFrameToContext(PKTRAP_FRAME TrapFrame
,
578 KeApplicationProcessorInit(VOID
);
580 KePrepareForApplicationProcessorInit(ULONG id
);
582 KiUserTrapHandler(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, PVOID Cr2
);
584 KePushAndStackSwitchAndSysRet(ULONG Push
, PVOID NewStack
);
586 KeStackSwitchAndRet(PVOID NewStack
);
588 KeBugCheckWithTf(ULONG BugCheckCode
,
589 ULONG BugCheckParameter1
,
590 ULONG BugCheckParameter2
,
591 ULONG BugCheckParameter3
,
592 ULONG BugCheckParameter4
,
594 #define KEBUGCHECKWITHTF(a,b,c,d,e,f) DbgPrint("KeBugCheckWithTf at %s:%i\n",__FILE__,__LINE__), KeBugCheckWithTf(a,b,c,d,e,f)
596 KiDumpTrapFrame(PKTRAP_FRAME Tf
, ULONG ExceptionNr
, ULONG cr2
);
600 KeFlushCurrentTb(VOID
);
603 KiSetSystemTime(PLARGE_INTEGER NewSystemTime
);
605 #endif /* not __ASM__ */
607 #define MAXIMUM_PROCESSORS 32
609 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_KE_H */