3 /* INCLUDES *****************************************************************/
7 /* INTERNAL KERNEL TYPES ****************************************************/
9 typedef struct _WOW64_PROCESS
12 } WOW64_PROCESS
, *PWOW64_PROCESS
;
14 typedef struct _KPROFILE_SOURCE_OBJECT
16 KPROFILE_SOURCE Source
;
18 } KPROFILE_SOURCE_OBJECT
, *PKPROFILE_SOURCE_OBJECT
;
20 typedef enum _CONNECT_TYPE
26 } CONNECT_TYPE
, *PCONNECT_TYPE
;
28 typedef struct _DISPATCH_INFO
31 PKINTERRUPT Interrupt
;
32 PKINTERRUPT_ROUTINE NoDispatch
;
33 PKINTERRUPT_ROUTINE InterruptDispatch
;
34 PKINTERRUPT_ROUTINE FloatingDispatch
;
35 PKINTERRUPT_ROUTINE ChainedDispatch
;
36 PKINTERRUPT_ROUTINE
*FlatDispatch
;
37 } DISPATCH_INFO
, *PDISPATCH_INFO
;
39 typedef struct _DEFERRED_REVERSE_BARRIER
42 ULONG TotalProcessors
;
43 } DEFERRED_REVERSE_BARRIER
, *PDEFERRED_REVERSE_BARRIER
;
45 typedef struct _KI_SAMPLE_MAP
47 LARGE_INTEGER PerfStart
;
48 LARGE_INTEGER PerfEnd
;
50 LARGE_INTEGER PerfFreq
;
55 } KI_SAMPLE_MAP
, *PKI_SAMPLE_MAP
;
57 typedef struct _KTIMER_TABLE_ENTRY
61 } KTIMER_TABLE_ENTRY
, *PKTIMER_TABLE_ENTRY
;
63 #define MAX_TIMER_DPCS 16
65 typedef struct _DPC_QUEUE_ENTRY
68 PKDEFERRED_ROUTINE Routine
;
70 } DPC_QUEUE_ENTRY
, *PDPC_QUEUE_ENTRY
;
72 typedef struct _KNMI_HANDLER_CALLBACK
74 struct _KNMI_HANDLER_CALLBACK
* Next
;
75 PNMI_CALLBACK Callback
;
78 } KNMI_HANDLER_CALLBACK
, *PKNMI_HANDLER_CALLBACK
;
81 (NTAPI
*PKE_BUGCHECK_UNICODE_TO_ANSI
)(
82 IN PUNICODE_STRING Unicode
,
87 extern PKNMI_HANDLER_CALLBACK KiNmiCallbackListHead
;
88 extern KSPIN_LOCK KiNmiCallbackListLock
;
89 extern PVOID KeUserApcDispatcher
;
90 extern PVOID KeUserCallbackDispatcher
;
91 extern PVOID KeUserExceptionDispatcher
;
92 extern PVOID KeRaiseUserExceptionDispatcher
;
93 extern LARGE_INTEGER KeBootTime
;
94 extern ULONGLONG KeBootTimeBias
;
95 extern BOOLEAN ExCmosClockIsSane
;
96 extern USHORT KeProcessorArchitecture
;
97 extern USHORT KeProcessorLevel
;
98 extern USHORT KeProcessorRevision
;
99 extern ULONG KeFeatureBits
;
100 extern KNODE KiNode0
;
101 extern PKNODE KeNodeBlock
[1];
102 extern UCHAR KeNumberNodes
;
103 extern UCHAR KeProcessNodeSeed
;
104 extern ETHREAD KiInitialThread
;
105 extern EPROCESS KiInitialProcess
;
106 extern PULONG KiInterruptTemplateObject
;
107 extern PULONG KiInterruptTemplateDispatch
;
108 extern PULONG KiInterruptTemplate2ndDispatch
;
109 extern ULONG KiUnexpectedEntrySize
;
110 extern ULONG_PTR KiDoubleFaultStack
;
111 extern EX_PUSH_LOCK KernelAddressSpaceLock
;
112 extern ULONG KiMaximumDpcQueueDepth
;
113 extern ULONG KiMinimumDpcRate
;
114 extern ULONG KiAdjustDpcThreshold
;
115 extern ULONG KiIdealDpcRate
;
116 extern BOOLEAN KeThreadDpcEnable
;
117 extern LARGE_INTEGER KiTimeIncrementReciprocal
;
118 extern UCHAR KiTimeIncrementShiftCount
;
119 extern ULONG KiTimeLimitIsrMicroseconds
;
120 extern ULONG KiServiceLimit
;
121 extern LIST_ENTRY KeBugcheckCallbackListHead
, KeBugcheckReasonCallbackListHead
;
122 extern KSPIN_LOCK BugCheckCallbackLock
;
123 extern KDPC KiTimerExpireDpc
;
124 extern KTIMER_TABLE_ENTRY KiTimerTableListHead
[TIMER_TABLE_SIZE
];
125 extern FAST_MUTEX KiGenericCallDpcMutex
;
126 extern LIST_ENTRY KiProfileListHead
, KiProfileSourceListHead
;
127 extern KSPIN_LOCK KiProfileLock
;
128 extern LIST_ENTRY KiProcessListHead
;
129 extern LIST_ENTRY KiProcessInSwapListHead
, KiProcessOutSwapListHead
;
130 extern LIST_ENTRY KiStackInSwapListHead
;
131 extern KEVENT KiSwapEvent
;
132 extern PKPRCB KiProcessorBlock
[];
133 extern ULONG KiMask32Array
[MAXIMUM_PRIORITY
];
134 extern ULONG_PTR KiIdleSummary
;
135 extern PVOID KeUserApcDispatcher
;
136 extern PVOID KeUserCallbackDispatcher
;
137 extern PVOID KeUserExceptionDispatcher
;
138 extern PVOID KeRaiseUserExceptionDispatcher
;
139 extern ULONG KeTimeIncrement
;
140 extern ULONG KeTimeAdjustment
;
141 extern BOOLEAN KiTimeAdjustmentEnabled
;
142 extern LONG KiTickOffset
;
143 extern ULONG_PTR KiBugCheckData
[5];
144 extern ULONG KiFreezeFlag
;
145 extern ULONG KiDPCTimeout
;
146 extern PGDI_BATCHFLUSH_ROUTINE KeGdiFlushUserBatch
;
147 extern ULONGLONG BootCycles
, BootCyclesEnd
;
148 extern ULONG ProcessCount
;
149 extern VOID __cdecl
KiInterruptTemplate(VOID
);
151 /* MACROS *************************************************************************/
153 #define AFFINITY_MASK(Id) KiMask32Array[Id]
154 #define PRIORITY_MASK(Id) KiMask32Array[Id]
156 /* Tells us if the Timer or Event is a Syncronization or Notification Object */
157 #define TIMER_OR_EVENT_TYPE 0x7L
159 /* One of the Reserved Wait Blocks, this one is for the Thread's Timer */
160 #define TIMER_WAIT_BLOCK 0x3L
162 #ifdef _M_ARM // FIXME: remove this once our headers are cleaned up
164 // A system call ID is formatted as such:
165 // .________________________________________________________________.
166 // | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
167 // |--------------|-------------------------------------------------|
168 // | TABLE NUMBER | TABLE OFFSET |
169 // \----------------------------------------------------------------/
172 // The table number is then used as an index into the service descriptor table.
173 #define TABLE_NUMBER_BITS 3
174 #define TABLE_OFFSET_BITS 12
177 // There are 2 tables (kernel and shadow, used by Win32K)
179 #define NUMBER_SERVICE_TABLES 2
180 #define NTOS_SERVICE_INDEX 0
181 #define WIN32K_SERVICE_INDEX 1
184 // NB. From assembly code, the table number must be computed as an offset into
185 // the service descriptor table.
187 // Each entry into the table is 16 bytes long on 32-bit architectures, and
188 // 32 bytes long on 64-bit architectures.
190 // Thus, Table Number 1 is offset 16 (0x10) on x86, and offset 32 (0x20) on
194 #define BITS_PER_ENTRY 5 // (1 << 5) = 32 bytes
196 #define BITS_PER_ENTRY 4 // (1 << 4) = 16 bytes
200 // We want the table number, but leave some extra bits to we can have the offset
201 // into the descriptor table.
203 #define SERVICE_TABLE_SHIFT (12 - BITS_PER_ENTRY)
206 // Now the table number (as an offset) is corrupted with part of the table offset
207 // This mask will remove the extra unwanted bits, and give us the offset into the
208 // descriptor table proper.
210 #define SERVICE_TABLE_MASK (((1 << TABLE_NUMBER_BITS) - 1) << BITS_PER_ENTRY)
213 // To get the table offset (ie: the service call number), just keep the 12 bits
215 #define SERVICE_NUMBER_MASK ((1 << TABLE_OFFSET_BITS) - 1)
218 // We'll often need to check if this is a graphics call. This is done by comparing
219 // the table number offset with the known Win32K table number offset.
220 // This is usually index 1, so table number offset 0x10 (x86) or 0x20 (x64)
222 #define SERVICE_TABLE_TEST (WIN32K_SERVICE_INDEX << BITS_PER_ENTRY)
226 #define KTS_SYSCALL_BIT (((KTRAP_STATE_BITS) { { .SystemCall = TRUE } }).Bits)
227 #define KTS_PM_BIT (((KTRAP_STATE_BITS) { { .PreviousMode = TRUE } }).Bits)
228 #define KTS_SEG_BIT (((KTRAP_STATE_BITS) { { .Segments = TRUE } }).Bits)
229 #define KTS_VOL_BIT (((KTRAP_STATE_BITS) { { .Volatiles = TRUE } }).Bits)
230 #define KTS_FULL_BIT (((KTRAP_STATE_BITS) { { .Full = TRUE } }).Bits)
232 /* INTERNAL KERNEL FUNCTIONS ************************************************/
238 OUT PULONG CpuInfoEax
,
239 OUT PULONG CpuInfoEbx
,
240 OUT PULONG CpuInfoEcx
,
241 OUT PULONG CpuInfoEdx
257 /* Finds a new thread to run */
273 KeSetDisableBoostThread(
274 IN OUT PKTHREAD Thread
,
280 KeSetDisableBoostProcess(
281 IN PKPROCESS Process
,
287 KeSetAutoAlignmentProcess(
288 IN PKPROCESS Process
,
294 KeSetAffinityProcess(
295 IN PKPROCESS Process
,
296 IN KAFFINITY Affinity
301 KeBoostPriorityThread(
303 IN KPRIORITY Increment
308 KeBalanceSetManager(IN PVOID Context
);
312 KiReadyThread(IN PKTHREAD Thread
);
316 KeSuspendThread(PKTHREAD Thread
);
320 KeReadStateThread(IN PKTHREAD Thread
);
326 IN PKTHREAD CurrentThread
331 KiAdjustQuantumThread(IN PKTHREAD Thread
);
335 KiExitDispatcher(KIRQL OldIrql
);
339 KiDeferredReadyThread(IN PKTHREAD Thread
);
349 KiProcessDeferredReadyList(
357 IN KAFFINITY Affinity
376 IN PLIST_ENTRY ExpiredListHead
,
384 IN LARGE_INTEGER Interval
391 IN PKSPIN_LOCK_QUEUE LockQueue
394 /* gmutex.c ********************************************************************/
398 KiAcquireGuardedMutex(
399 IN OUT PKGUARDED_MUTEX GuardedMutex
405 IN PFAST_MUTEX FastMutex
408 /* gate.c **********************************************************************/
412 KeInitializeGate(PKGATE Gate
);
416 KeSignalGateBoostPriority(PKGATE Gate
);
422 KWAIT_REASON WaitReason
,
423 KPROCESSOR_MODE WaitMode
426 /* ipi.c ********************************************************************/
438 IN KAFFINITY TargetProcessors
,
439 IN PKIPI_WORKER WorkerFunction
,
440 IN PKIPI_BROADCAST_WORKER BroadcastFunction
,
441 IN ULONG_PTR Context
,
447 KiIpiSignalPacketDone(
448 IN PKIPI_CONTEXT PacketContext
453 KiIpiSignalPacketDoneAndStall(
454 IN PKIPI_CONTEXT PacketContext
,
455 IN
volatile PULONG ReverseStall
458 /* next file ***************************************************************/
462 KeFindNextRightSetAffinity(
469 DbgBreakPointNoBugCheck(VOID
);
474 struct _KPROFILE
* Profile
,
475 struct _KPROCESS
* Process
,
479 KPROFILE_SOURCE ProfileSource
,
486 struct _KPROFILE
* Profile
,
492 KeStopProfile(struct _KPROFILE
* Profile
);
496 KeQueryIntervalProfile(KPROFILE_SOURCE ProfileSource
);
500 KeSetIntervalProfile(
501 KPROFILE_SOURCE ProfileSource
,
508 PKTRAP_FRAME TrapFrame
513 KeProfileInterruptWithSource(
514 IN PKTRAP_FRAME TrapFrame
,
515 IN KPROFILE_SOURCE Source
521 PKTRAP_FRAME TrapFrame
,
529 PVOID DeferredContext
,
530 PVOID SystemArgument1
,
531 PVOID SystemArgument2
537 IN PKPROCESS Process
,
538 IN OUT PKTHREAD Thread
,
539 IN PKSYSTEM_ROUTINE SystemRoutine
,
540 IN PKSTART_ROUTINE StartRoutine
,
541 IN PVOID StartContext
,
556 IN OUT PKTHREAD Thread
,
557 IN PVOID KernelStack
,
558 IN PKSYSTEM_ROUTINE SystemRoutine
,
559 IN PKSTART_ROUTINE StartRoutine
,
560 IN PVOID StartContext
,
568 KiInitializeContextThread(
570 PKSYSTEM_ROUTINE SystemRoutine
,
571 PKSTART_ROUTINE StartRoutine
,
579 IN OUT PKTHREAD Thread
586 IN KPROCESSOR_MODE AlertMode
610 KeRundownThread(VOID
);
614 KeReleaseThread(PKTHREAD Thread
);
626 IN PKNORMAL_ROUTINE
*NormalRoutine
,
627 IN PVOID
*NormalContext
,
628 IN PVOID
*SystemArgument1
,
629 IN PVOID
*SystemArgument2
635 IN PVOID NormalContext
,
636 IN PVOID SystemArgument1
,
637 IN PVOID SystemArgument2
642 KeQueryBasePriorityThread(IN PKTHREAD Thread
);
648 IN KPRIORITY Priority
655 IN LONG_PTR WaitStatus
660 KeDumpStackFrames(PULONG Frame
);
670 IN LONG_PTR WaitStatus
,
671 IN KPRIORITY Increment
677 struct _KPROCESS
*Process
,
680 PULONG_PTR DirectoryTableBase
,
687 IN PKPROCESS Process
,
693 KeSetPriorityAndQuantumProcess(
694 IN PKPROCESS Process
,
695 IN KPRIORITY Priority
,
696 IN UCHAR Quantum OPTIONAL
701 KeForceResumeThread(IN PKTHREAD Thread
);
717 KeDisableThreadApcQueueing(IN PKTHREAD Thread
);
728 KeContextToTrapFrame(
730 PKEXCEPTION_FRAME ExeptionFrame
,
731 PKTRAP_FRAME TrapFrame
,
733 KPROCESSOR_MODE PreviousMode
742 KiCheckForKernelApcDelivery(VOID
);
748 IN PLIST_ENTRY Entry
,
756 IN PVOID DeferredContext
,
757 IN PVOID SystemArgument1
,
758 IN PVOID SystemArgument2
764 struct _KPROCESS
* Process
,
771 KeInitializeEventPair(PKEVENT_PAIR EventPair
);
776 IN PKEXCEPTION_FRAME Reserved
,
777 IN PKTRAP_FRAME TrapFrame
,
778 IN PKNORMAL_ROUTINE NormalRoutine
,
779 IN PVOID NormalContext
,
780 IN PVOID SystemArgument1
,
781 IN PVOID SystemArgument2
788 IN KPROCESSOR_MODE PreviousMode
794 struct _KTHREAD
*Thread
,
795 struct _KPROCESS
*Process
,
796 PKLOCK_QUEUE_HANDLE ApcLock
,
797 struct _KAPC_STATE
*SavedApcState
803 struct _KPROCESS
*NewProcess
,
804 struct _KPROCESS
*OldProcess
809 KeTestAlertThread(IN KPROCESSOR_MODE AlertMode
);
813 KeRemoveQueueApc(PKAPC Apc
);
817 KiActivateWaiterQueue(IN PKQUEUE Queue
);
821 KeQueryRuntimeProcess(IN PKPROCESS Process
,
822 OUT PULONG UserTime
);
824 /* INITIALIZATION FUNCTIONS *************************************************/
832 KeInitExceptions(VOID
);
836 KeInitInterrupts(VOID
);
840 KiInitializeBugCheck(VOID
);
845 IN PLOADER_PARAMETER_BLOCK LoaderBlock
850 KiDeliverUserApc(PKTRAP_FRAME TrapFrame
);
855 PKAPC_STATE OldState
,
862 KPROFILE_SOURCE Source
,
869 PEXCEPTION_RECORD ExceptionRecord
,
870 PKEXCEPTION_FRAME ExceptionFrame
,
872 KPROCESSOR_MODE PreviousMode
,
878 KeTrapFrameToContext(
879 IN PKTRAP_FRAME TrapFrame
,
880 IN PKEXCEPTION_FRAME ExceptionFrame
,
881 IN OUT PCONTEXT Context
889 ULONG_PTR BugCheckParameter1
,
890 ULONG_PTR BugCheckParameter2
,
891 ULONG_PTR BugCheckParameter3
,
892 ULONG_PTR BugCheckParameter4
,
902 KeFlushCurrentTb(VOID
);
906 KeInvalidateAllCaches(VOID
);
910 KeZeroPages(IN PVOID Address
,
915 KeInvalidAccessAllowed(IN PVOID TrapInformation OPTIONAL
);
919 KeRosDumpStackFrames(
927 IN PLARGE_INTEGER NewSystemTime
,
928 OUT PLARGE_INTEGER OldSystemTime
,
929 IN BOOLEAN FixInterruptTime
,
930 IN PLARGE_INTEGER HalTime
943 KiStartUnexpectedRange(
949 KiEndUnexpectedRange(
956 IN PEXCEPTION_RECORD ExceptionRecord
,
958 IN PKEXCEPTION_FRAME ExceptionFrame
,
959 IN PKTRAP_FRAME TrapFrame
,
960 IN BOOLEAN SearchFrames
967 IN PKEXCEPTION_FRAME ExceptionFrame
,
968 IN PKTRAP_FRAME TrapFrame
975 IN PKTRAP_FRAME TrapFrame
,
983 IN PKTRAP_FRAME TrapFrame
990 IN PKTRAP_FRAME TrapFrame
,
991 IN PKINTERRUPT Interrupt
998 IN PKTRAP_FRAME TrapFrame
,
999 IN PKINTERRUPT Interrupt
1004 KiInitializeMachineType(
1010 KiSetupStackAndInitializeKernel(
1011 IN PKPROCESS InitProcess
,
1012 IN PKTHREAD InitThread
,
1016 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1028 KiComputeReciprocal(
1043 IN KPRIORITY PriorityBoost
1049 IN PVOID
*OutputBuffer
,
1050 IN PULONG OutputLength
1063 KiInitMachineDependent(VOID
);
1067 KeFreezeExecution(IN PKTRAP_FRAME TrapFrame
,
1068 IN PKEXCEPTION_FRAME ExceptionFrame
);
1072 KeThawExecution(IN BOOLEAN Enable
);
1076 KeAcquireQueuedSpinLockAtDpcLevel(
1077 IN OUT PKSPIN_LOCK_QUEUE LockQueue
1082 KeReleaseQueuedSpinLockFromDpcLevel(
1083 IN OUT PKSPIN_LOCK_QUEUE LockQueue
1088 KiRestoreProcessorControlState(
1089 IN PKPROCESSOR_STATE ProcessorState
1094 KiSaveProcessorControlState(
1095 OUT PKPROCESSOR_STATE ProcessorState
1100 KiSaveProcessorState(
1101 IN PKTRAP_FRAME TrapFrame
,
1102 IN PKEXCEPTION_FRAME ExceptionFrame
1126 KiSystemFatalException(
1127 IN ULONG ExceptionCode
,
1128 IN PKTRAP_FRAME TrapFrame
1133 KiPcToFileHeader(IN PVOID Eip
,
1134 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
,
1135 IN BOOLEAN DriversOnly
,
1136 OUT PBOOLEAN InKernel
);
1140 KiRosPcToUserFileHeader(IN PVOID Eip
,
1141 OUT PLDR_DATA_TABLE_ENTRY
*LdrEntry
);
1145 KeBugCheckUnicodeToAnsi(
1146 IN PUNICODE_STRING Unicode
,