ke.h
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 63k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /*++ BUILD Version: 0028    // Increment this if a change has global effects
  2. Copyright (c) 1989  Microsoft Corporation
  3. Module Name:
  4.     ke.h
  5. Abstract:
  6.     This module contains the public (external) header file for the kernel.
  7. Author:
  8.     David N. Cutler (davec) 27-Feb-1989
  9. Revision History:
  10. --*/
  11. #ifndef _KE_
  12. #define _KE_
  13. //
  14. // Define the default quantum decrement values.
  15. //
  16. #define CLOCK_QUANTUM_DECREMENT 3
  17. #define WAIT_QUANTUM_DECREMENT 1
  18. //
  19. // Define the default ready skip and thread quantum values.
  20. //
  21. #define READY_SKIP_QUANTUM 2
  22. #define THREAD_QUANTUM (READY_SKIP_QUANTUM * CLOCK_QUANTUM_DECREMENT)
  23. //
  24. // Define the round trip decrement count.
  25. //
  26. #define ROUND_TRIP_DECREMENT_COUNT 16
  27. //
  28. // Performance data collection enable definitions.
  29. //
  30. // A definition turns on the respective data collection.
  31. //
  32. //#define _COLLECT_FLUSH_SINGLE_CALLDATA_ 1
  33. //#define _COLLECT_SET_EVENT_CALLDATA_ 1
  34. //#define _COLLECT_WAIT_SINGLE_CALLDATA_ 1
  35. //
  36. // Define thread switch performance data structure.
  37. //
  38. typedef struct _KTHREAD_SWITCH_COUNTERS {
  39.     ULONG FindAny;
  40.     ULONG FindIdeal;
  41.     ULONG FindLast;
  42.     ULONG IdleAny;
  43.     ULONG IdleCurrent;
  44.     ULONG IdleIdeal;
  45.     ULONG IdleLast;
  46.     ULONG PreemptAny;
  47.     ULONG PreemptCurrent;
  48.     ULONG PreemptLast;
  49.     ULONG SwitchToIdle;
  50. } KTHREAD_SWITCH_COUNTERS, *PKTHREAD_SWITCH_COUNTERS;
  51. //
  52. // Public (external) constant definitions.
  53. //
  54. #define BASE_PRIORITY_THRESHOLD NORMAL_BASE_PRIORITY // fast path base threshold
  55. // begin_ntddk begin_wdm
  56. #define THREAD_WAIT_OBJECTS 3           // Builtin usable wait blocks
  57. // end_ntddk end_wdm
  58. #define EVENT_WAIT_BLOCK 2              // Builtin event pair wait block
  59. #define SEMAPHORE_WAIT_BLOCK 2          // Builtin semaphore wait block
  60. #define TIMER_WAIT_BLOCK 3              // Builtin timer wait block
  61. #if (EVENT_WAIT_BLOCK != SEMAPHORE_WAIT_BLOCK)
  62. #error "wait event and wait semaphore must use same wait block"
  63. #endif
  64. //
  65. // Define timer table size.
  66. //
  67. #define TIMER_TABLE_SIZE 128
  68. //
  69. // Get APC environment of current thread.
  70. //
  71. #define KeGetCurrentApcEnvironment() 
  72.     KeGetCurrentThread()->ApcStateIndex
  73. //
  74. // Enumerated kernel types
  75. //
  76. // Kernel object types.
  77. //
  78. //  N.B. There are really two types of event objects; NotificationEvent and
  79. //       SynchronizationEvent. The type value for a notification event is 0,
  80. //       and that for a synchronization event 1.
  81. //
  82. //  N.B. There are two types of new timer objects; NotificationTimer and
  83. //       SynchronizationTimer. The type value for a notification timer is
  84. //       8, and that for a synchronization timer is 9. These values are
  85. //       very carefully chosen so that the dispatcher object type AND'ed
  86. //       with 0x7 yields 0 or 1 for event objects and the timer objects.
  87. //
  88. #define DISPATCHER_OBJECT_TYPE_MASK 0x7
  89. typedef enum _KOBJECTS {
  90.     EventNotificationObject = 0,
  91.     EventSynchronizationObject = 1,
  92.     MutantObject = 2,
  93.     ProcessObject = 3,
  94.     QueueObject = 4,
  95.     SemaphoreObject = 5,
  96.     ThreadObject = 6,
  97.     Spare1Object = 7,
  98.     TimerNotificationObject = 8,
  99.     TimerSynchronizationObject = 9,
  100.     Spare2Object = 10,
  101.     Spare3Object = 11,
  102.     Spare4Object = 12,
  103.     Spare5Object = 13,
  104.     Spare6Object = 14,
  105.     Spare7Object = 15,
  106.     Spare8Object = 16,
  107.     Spare9Object = 17,
  108.     ApcObject,
  109.     DpcObject,
  110.     DeviceQueueObject,
  111.     EventPairObject,
  112.     InterruptObject,
  113.     ProfileObject
  114.     } KOBJECTS;
  115. //
  116. // APC environments.
  117. //
  118. typedef enum _KAPC_ENVIRONMENT {
  119.     OriginalApcEnvironment,
  120.     AttachedApcEnvironment,
  121.     CurrentApcEnvironment
  122.     } KAPC_ENVIRONMENT;
  123. // begin_ntddk begin_wdm begin_nthal begin_ntminiport begin_ntifs begin_ntndis
  124. //
  125. // Interrupt modes.
  126. //
  127. typedef enum _KINTERRUPT_MODE {
  128.     LevelSensitive,
  129.     Latched
  130.     } KINTERRUPT_MODE;
  131. // end_ntddk end_wdm end_nthal end_ntminiport end_ntifs end_ntndis
  132. //
  133. // Process states.
  134. //
  135. typedef enum _KPROCESS_STATE {
  136.     ProcessInMemory,
  137.     ProcessOutOfMemory,
  138.     ProcessInTransition,
  139.     ProcessInSwap,
  140.     ProcessOutSwap
  141.     } KPROCESS_STATE;
  142. //
  143. // Thread scheduling states.
  144. //
  145. typedef enum _KTHREAD_STATE {
  146.     Initialized,
  147.     Ready,
  148.     Running,
  149.     Standby,
  150.     Terminated,
  151.     Waiting,
  152.     Transition
  153.     } KTHREAD_STATE;
  154. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  155. //
  156. // Wait reasons
  157. //
  158. typedef enum _KWAIT_REASON {
  159.     Executive,
  160.     FreePage,
  161.     PageIn,
  162.     PoolAllocation,
  163.     DelayExecution,
  164.     Suspended,
  165.     UserRequest,
  166.     WrExecutive,
  167.     WrFreePage,
  168.     WrPageIn,
  169.     WrPoolAllocation,
  170.     WrDelayExecution,
  171.     WrSuspended,
  172.     WrUserRequest,
  173.     WrEventPair,
  174.     WrQueue,
  175.     WrLpcReceive,
  176.     WrLpcReply,
  177.     WrVirtualMemory,
  178.     WrPageOut,
  179.     WrRendezvous,
  180.     Spare2,
  181.     Spare3,
  182.     Spare4,
  183.     Spare5,
  184.     Spare6,
  185.     WrKernel,
  186.     MaximumWaitReason
  187.     } KWAIT_REASON;
  188. // end_ntddk end_wdm end_nthal
  189. //
  190. // Miscellaneous type definitions
  191. //
  192. // APC state
  193. //
  194. typedef struct _KAPC_STATE {
  195.     LIST_ENTRY ApcListHead[MaximumMode];
  196.     struct _KPROCESS *Process;
  197.     BOOLEAN KernelApcInProgress;
  198.     BOOLEAN KernelApcPending;
  199.     BOOLEAN UserApcPending;
  200. } KAPC_STATE, *PKAPC_STATE, *RESTRICTED_POINTER PRKAPC_STATE;
  201. // begin_ntddk begin_wdm begin_nthal begin_ntndis
  202. //
  203. // Common dispatcher object header
  204. //
  205. // N.B. The size field contains the number of dwords in the structure.
  206. //
  207. typedef struct _DISPATCHER_HEADER {
  208.     UCHAR Type;
  209.     UCHAR Absolute;
  210.     UCHAR Size;
  211.     UCHAR Inserted;
  212.     LONG SignalState;
  213.     LIST_ENTRY WaitListHead;
  214. } DISPATCHER_HEADER;
  215. // end_ntddk end_wdm end_nthal end_ntifs end_ntndis
  216. //
  217. // Page frame
  218. //
  219. typedef ULONG KPAGE_FRAME;
  220. //
  221. // Wait block
  222. //
  223. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  224. typedef struct _KWAIT_BLOCK {
  225.     LIST_ENTRY WaitListEntry;
  226.     struct _KTHREAD *RESTRICTED_POINTER Thread;
  227.     PVOID Object;
  228.     struct _KWAIT_BLOCK *RESTRICTED_POINTER NextWaitBlock;
  229.     USHORT WaitKey;
  230.     USHORT WaitType;
  231. } KWAIT_BLOCK, *PKWAIT_BLOCK, *RESTRICTED_POINTER PRKWAIT_BLOCK;
  232. // end_ntddk end_wdm end_nthal end_ntifs
  233. //
  234. // System service table descriptor.
  235. //
  236. // N.B. A system service number has a 12-bit service table offset and a
  237. //      3-bit service table number.
  238. //
  239. // N.B. Descriptor table entries must be a power of 2 in size. Currently
  240. //      this is 16 bytes on a 32-bit system and 32 bytes on a 64-bit
  241. //      system.
  242. //
  243. #define NUMBER_SERVICE_TABLES 4
  244. #define SERVICE_NUMBER_MASK ((1 << 12) -  1)
  245. #if defined(_WIN64)
  246. #define SERVICE_TABLE_SHIFT (12 - 5)
  247. #define SERVICE_TABLE_MASK (((1 << 2) - 1) << 5)
  248. #define SERVICE_TABLE_TEST (WIN32K_SERVICE_INDEX << 5)
  249. #else
  250. #define SERVICE_TABLE_SHIFT (12 - 4)
  251. #define SERVICE_TABLE_MASK (((1 << 2) - 1) << 4)
  252. #define SERVICE_TABLE_TEST (WIN32K_SERVICE_INDEX << 4)
  253. #endif
  254. typedef struct _KSERVICE_TABLE_DESCRIPTOR {
  255.     PULONG_PTR Base;
  256.     PULONG Count;
  257.     ULONG Limit;
  258. #if defined(_IA64_)
  259.     LONG TableBaseGpOffset;
  260. #endif
  261.     PUCHAR Number;
  262. } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
  263. //
  264. // Procedure type definitions
  265. //
  266. // Debug routine
  267. //
  268. typedef
  269. BOOLEAN
  270. (*PKDEBUG_ROUTINE) (
  271.     IN PKTRAP_FRAME TrapFrame,
  272.     IN PKEXCEPTION_FRAME ExceptionFrame,
  273.     IN PEXCEPTION_RECORD ExceptionRecord,
  274.     IN PCONTEXT ContextRecord,
  275.     IN KPROCESSOR_MODE PreviousMode,
  276.     IN BOOLEAN SecondChance
  277.     );
  278. typedef
  279. BOOLEAN
  280. (*PKDEBUG_SWITCH_ROUTINE) (
  281.     IN PEXCEPTION_RECORD ExceptionRecord,
  282.     IN PCONTEXT ContextRecord,
  283.     IN BOOLEAN SecondChance
  284.     );
  285. typedef enum {
  286.     ContinueError = FALSE,
  287.     ContinueSuccess = TRUE,
  288.     ContinueProcessorReselected,
  289.     ContinueNextProcessor
  290. } KCONTINUE_STATUS;
  291. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  292. //
  293. // Thread start function
  294. //
  295. typedef
  296. VOID
  297. (*PKSTART_ROUTINE) (
  298.     IN PVOID StartContext
  299.     );
  300. // end_ntddk end_wdm end_nthal end_ntifs
  301. //
  302. // Thread system function
  303. //
  304. typedef
  305. VOID
  306. (*PKSYSTEM_ROUTINE) (
  307.     IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  308.     IN PVOID StartContext OPTIONAL
  309.     );
  310. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  311. //
  312. // Kernel object structure definitions
  313. //
  314. //
  315. // Device Queue object and entry
  316. //
  317. typedef struct _KDEVICE_QUEUE {
  318.     CSHORT Type;
  319.     CSHORT Size;
  320.     LIST_ENTRY DeviceListHead;
  321.     KSPIN_LOCK Lock;
  322.     BOOLEAN Busy;
  323. } KDEVICE_QUEUE, *PKDEVICE_QUEUE, *RESTRICTED_POINTER PRKDEVICE_QUEUE;
  324. typedef struct _KDEVICE_QUEUE_ENTRY {
  325.     LIST_ENTRY DeviceListEntry;
  326.     ULONG SortKey;
  327.     BOOLEAN Inserted;
  328. } KDEVICE_QUEUE_ENTRY, *PKDEVICE_QUEUE_ENTRY, *RESTRICTED_POINTER PRKDEVICE_QUEUE_ENTRY;
  329. // begin_ntndis
  330. //
  331. // Event object
  332. //
  333. typedef struct _KEVENT {
  334.     DISPATCHER_HEADER Header;
  335. } KEVENT, *PKEVENT, *RESTRICTED_POINTER PRKEVENT;
  336. // end_ntddk end_wdm end_nthal end_ntifs end_ntndis
  337. //
  338. // Event pair object
  339. //
  340. typedef struct _KEVENT_PAIR {
  341.     CSHORT Type;
  342.     CSHORT Size;
  343.     KEVENT EventLow;
  344.     KEVENT EventHigh;
  345. } KEVENT_PAIR, *PKEVENT_PAIR, *RESTRICTED_POINTER PRKEVENT_PAIR;
  346. // begin_nthal begin_ntddk begin_wdm begin_ntifs
  347. //
  348. // Define the interrupt service function type and the empty struct
  349. // type.
  350. //
  351. // end_ntddk end_wdm end_ntifs
  352. struct _KINTERRUPT;
  353. // begin_ntddk begin_wdm begin_ntifs
  354. typedef
  355. BOOLEAN
  356. (*PKSERVICE_ROUTINE) (
  357.     IN struct _KINTERRUPT *Interrupt,
  358.     IN PVOID ServiceContext
  359.     );
  360. // end_ntddk end_wdm end_ntifs
  361. //
  362. // Interrupt object
  363. //
  364. // N.B. The layout of this structure cannot change. It is exported to HALs
  365. //      to short circuit interrupt dispatch.
  366. //
  367. typedef struct _KINTERRUPT {
  368.     CSHORT Type;
  369.     CSHORT Size;
  370.     LIST_ENTRY InterruptListEntry;
  371.     PKSERVICE_ROUTINE ServiceRoutine;
  372.     PVOID ServiceContext;
  373.     KSPIN_LOCK SpinLock;
  374.     ULONG Spare1;
  375.     PKSPIN_LOCK ActualLock;
  376.     PKINTERRUPT_ROUTINE DispatchAddress;
  377.     ULONG Vector;
  378.     KIRQL Irql;
  379.     KIRQL SynchronizeIrql;
  380.     BOOLEAN FloatingSave;
  381.     BOOLEAN Connected;
  382.     CCHAR Number;
  383.     BOOLEAN ShareVector;
  384.     KINTERRUPT_MODE Mode;
  385.     ULONG ServiceCount;
  386.     ULONG Spare3;
  387.     ULONG DispatchCode[DISPATCH_LENGTH];
  388. } KINTERRUPT;
  389. typedef struct _KINTERRUPT *PKINTERRUPT, *RESTRICTED_POINTER PRKINTERRUPT; // ntndis
  390. // begin_ntifs begin_ntddk begin_wdm
  391. //
  392. // Mutant object
  393. //
  394. typedef struct _KMUTANT {
  395.     DISPATCHER_HEADER Header;
  396.     LIST_ENTRY MutantListEntry;
  397.     struct _KTHREAD *RESTRICTED_POINTER OwnerThread;
  398.     BOOLEAN Abandoned;
  399.     UCHAR ApcDisable;
  400. } KMUTANT, *PKMUTANT, *RESTRICTED_POINTER PRKMUTANT, KMUTEX, *PKMUTEX, *RESTRICTED_POINTER PRKMUTEX;
  401. // end_ntddk end_wdm
  402. //
  403. // Queue object
  404. //
  405. typedef struct _KQUEUE {
  406.     DISPATCHER_HEADER Header;
  407.     LIST_ENTRY EntryListHead;
  408.     ULONG CurrentCount;
  409.     ULONG MaximumCount;
  410.     LIST_ENTRY ThreadListHead;
  411. } KQUEUE, *PKQUEUE, *RESTRICTED_POINTER PRKQUEUE;
  412. // begin_ntddk begin_wdm
  413. //
  414. //
  415. // Semaphore object
  416. //
  417. typedef struct _KSEMAPHORE {
  418.     DISPATCHER_HEADER Header;
  419.     LONG Limit;
  420. } KSEMAPHORE, *PKSEMAPHORE, *RESTRICTED_POINTER PRKSEMAPHORE;
  421. // begin_ntndis
  422. //
  423. // Timer object
  424. //
  425. typedef struct _KTIMER {
  426.     DISPATCHER_HEADER Header;
  427.     ULARGE_INTEGER DueTime;
  428.     LIST_ENTRY TimerListEntry;
  429.     struct _KDPC *Dpc;
  430.     LONG Period;
  431. } KTIMER, *PKTIMER, *RESTRICTED_POINTER PRKTIMER;
  432. // end_ntddk end_wdm end_nthal end_ntifs end_ntndis
  433. //
  434. // Thread object
  435. //
  436. struct _ECHANNEL;
  437. typedef struct _KTHREAD {
  438.     //
  439.     // The dispatcher header and mutant listhead are fairly infrequently
  440.     // referenced, but pad the thread to a 32-byte boundary (assumption
  441.     // that pool allocation is in units of 32-bytes).
  442.     //
  443.     DISPATCHER_HEADER Header;
  444.     LIST_ENTRY MutantListHead;
  445.     //
  446.     // The following fields are referenced during trap, interrupts, or
  447.     // context switches.
  448.     //
  449.     // N.B. The Teb address and TlsArray are loaded as a quadword quantity
  450.     //      on MIPS and therefore must be on a quadword boundary.
  451.     //
  452.     PVOID InitialStack;
  453.     PVOID StackLimit;
  454. #if defined(_IA64_)
  455.     PVOID InitialBStore;
  456.     PVOID BStoreLimit;
  457. #endif
  458.     PVOID Teb;
  459.     PVOID TlsArray;
  460.     PVOID KernelStack;
  461. #if defined(_IA64_)
  462.     PVOID KernelBStore;
  463. #endif
  464.     BOOLEAN DebugActive;
  465.     UCHAR State;
  466.     BOOLEAN Alerted[MaximumMode];
  467.     UCHAR Iopl;
  468.     UCHAR NpxState;
  469.     CHAR Saturation;
  470.     SCHAR Priority;
  471.     KAPC_STATE ApcState;
  472.     ULONG ContextSwitches;
  473.     //
  474.     // The following fields are referenced during wait operations.
  475.     //
  476.     LONG_PTR WaitStatus;
  477.     KIRQL WaitIrql;
  478.     KPROCESSOR_MODE WaitMode;
  479.     BOOLEAN WaitNext;
  480.     UCHAR WaitReason;
  481.     PRKWAIT_BLOCK WaitBlockList;
  482.     LIST_ENTRY WaitListEntry;
  483.     ULONG WaitTime;
  484.     SCHAR BasePriority;
  485.     UCHAR DecrementCount;
  486.     SCHAR PriorityDecrement;
  487.     SCHAR Quantum;
  488.     KWAIT_BLOCK WaitBlock[THREAD_WAIT_OBJECTS + 1];
  489.     PVOID LegoData;
  490.     ULONG KernelApcDisable;
  491.     KAFFINITY UserAffinity;
  492.     BOOLEAN SystemAffinityActive;
  493.     UCHAR PowerState;
  494.     UCHAR NpxIrql;
  495.     UCHAR Pad[1];
  496.     PVOID ServiceTable;
  497. //    struct _ECHANNEL *Channel;
  498. //    PVOID Section;
  499. //    PCHANNEL_MESSAGE SystemView;
  500. //    PCHANNEL_MESSAGE ThreadView;
  501.     //
  502.     // The following fields are referenced during queue operations.
  503.     //
  504.     PRKQUEUE Queue;
  505.     KSPIN_LOCK ApcQueueLock;
  506.     KTIMER Timer;
  507.     LIST_ENTRY QueueListEntry;
  508.     //
  509.     // The following fields are referenced during read and find ready
  510.     // thread.
  511.     //
  512.     KAFFINITY Affinity;
  513.     BOOLEAN Preempted;
  514.     BOOLEAN ProcessReadyQueue;
  515.     BOOLEAN KernelStackResident;
  516.     UCHAR NextProcessor;
  517.     //
  518.     // The following fields are referenced during system calls.
  519.     //
  520.     PVOID CallbackStack;
  521. #if defined(_IA64_)
  522.     PVOID CallbackBStore;
  523. #endif
  524.     PVOID Win32Thread;
  525.     PKTRAP_FRAME TrapFrame;
  526.     PKAPC_STATE ApcStatePointer[2];
  527.     CCHAR PreviousMode;
  528.     UCHAR EnableStackSwap;
  529.     UCHAR LargeStack;
  530.     UCHAR ResourceIndex;
  531.     //
  532.     // The following entries are referenced during clock interrupts.
  533.     //
  534.     ULONG KernelTime;
  535.     ULONG UserTime;
  536.     //
  537.     // The following fields are referenced during APC queuing and process
  538.     // attach/detach.
  539.     //
  540.     KAPC_STATE SavedApcState;
  541.     BOOLEAN Alertable;
  542.     UCHAR ApcStateIndex;
  543.     BOOLEAN ApcQueueable;
  544.     BOOLEAN AutoAlignment;
  545.     //
  546.     // The following fields are referenced when the thread is initialized
  547.     // and very infrequently thereafter.
  548.     //
  549.     PVOID StackBase;
  550.     KAPC SuspendApc;
  551.     KSEMAPHORE SuspendSemaphore;
  552.     LIST_ENTRY ThreadListEntry;
  553.     //
  554.     // N.B. The below four UCHARs share the same DWORD and are modified
  555.     //      by other threads. Therefore, they must ALWAYS be modified
  556.     //      under the dispatcher lock to prevent granularity problems
  557.     //      on Alpha machines.
  558.     //
  559.     CCHAR FreezeCount;
  560.     CCHAR SuspendCount;
  561.     UCHAR IdealProcessor;
  562.     UCHAR DisableBoost;
  563. } KTHREAD, *PKTHREAD, *RESTRICTED_POINTER PRKTHREAD;
  564. //
  565. // Process object structure definition
  566. //
  567. typedef struct _KPROCESS {
  568.     //
  569.     // The dispatch header and profile listhead are fairly infrequently
  570.     // referenced, but pad the process to a 32-byte boundary (assumption
  571.     // that pool block allocation is in units of 32-bytes).
  572.     //
  573.     DISPATCHER_HEADER Header;
  574.     LIST_ENTRY ProfileListHead;
  575.     //
  576.     // The following fields are referenced during context switches.
  577.     //
  578.     ULONG_PTR DirectoryTableBase[2];
  579. #if defined(_X86_)
  580.     KGDTENTRY LdtDescriptor;
  581.     KIDTENTRY Int21Descriptor;
  582.     USHORT IopmOffset;
  583.     UCHAR Iopl;
  584.     BOOLEAN VdmFlag;
  585. #endif
  586. #if defined(_IA64_)
  587.     KGDTENTRY LdtDescriptor;
  588.     ULONGLONG UnscrambledLdtDescriptor;
  589.     KIDTENTRY Int21Descriptor;
  590.     BOOLEAN VdmFlag;
  591.     REGION_MAP_INFO ProcessRegion;
  592.     REGION_MAP_INFO SessionRegion;
  593.     PREGION_MAP_INFO SessionMapInfo;
  594.     ULONG_PTR SessionParentBase;
  595. #endif // _IA64_
  596. #if defined(_ALPHA_)
  597.     union {
  598.         struct {
  599.             KAFFINITY ActiveProcessors;
  600.             KAFFINITY RunOnProcessors;
  601.         };
  602.         ULONGLONG Alignment;
  603.     };
  604.     ULONGLONG ProcessSequence;
  605.     ULONG ProcessAsn;
  606. #else
  607.     KAFFINITY ActiveProcessors;
  608. #endif
  609.     //
  610.     // The following fields are referenced during clock interrupts.
  611.     //
  612.     ULONG KernelTime;
  613.     ULONG UserTime;
  614.     //
  615.     // The following fields are referenced infrequently.
  616.     //
  617.     LIST_ENTRY ReadyListHead;
  618.     LIST_ENTRY SwapListEntry;
  619.     LIST_ENTRY ThreadListHead;
  620.     KSPIN_LOCK ProcessLock;
  621.     KAFFINITY Affinity;
  622.     USHORT StackCount;
  623.     SCHAR BasePriority;
  624.     SCHAR ThreadQuantum;
  625.     BOOLEAN AutoAlignment;
  626.     UCHAR State;
  627.     UCHAR ThreadSeed;
  628.     BOOLEAN DisableBoost;
  629.     UCHAR PowerState;
  630.     BOOLEAN DisableQuantum;
  631.     UCHAR Spare[2];
  632. } KPROCESS, *PKPROCESS, *RESTRICTED_POINTER PRKPROCESS;
  633. //
  634. // Profile object structure definition
  635. //
  636. typedef struct _KPROFILE {
  637.     CSHORT Type;
  638.     CSHORT Size;
  639.     LIST_ENTRY ProfileListEntry;
  640.     PKPROCESS Process;
  641.     PVOID RangeBase;
  642.     PVOID RangeLimit;
  643.     ULONG BucketShift;
  644.     PVOID Buffer;
  645.     ULONG Segment;
  646.     KAFFINITY Affinity;
  647.     CSHORT Source;
  648.     BOOLEAN Started;
  649. } KPROFILE, *PKPROFILE, *RESTRICTED_POINTER PRKPROFILE;
  650. //
  651. // Define kernel channel object structure and types.
  652. //
  653. #define LISTEN_CHANNEL 0x1
  654. #define MESSAGE_CHANNEL 0x2
  655. typedef enum _ECHANNEL_STATE {
  656.     ClientIdle,
  657.     ClientSendWaitReply,
  658.     ClientShutdown,
  659.     ServerIdle,
  660.     ServerReceiveMessage,
  661.     ServerShutdown
  662. } ECHANNEL_STATE;
  663. typedef struct _ECHANNEL {
  664.     USHORT Type;
  665.     USHORT State;
  666.     PKPROCESS OwnerProcess;
  667.     PKTHREAD ClientThread;
  668.     PKTHREAD ServerThread;
  669.     PVOID ServerContext;
  670.     struct _ECHANNEL *ServerChannel;
  671.     KEVENT ReceiveEvent;
  672.     KEVENT ClearToSendEvent;
  673. } ECHANNEL, *PECHANNEL, *RESTRICTED_POINTER PRECHANNEL;
  674. //
  675. // Kernel control object functions
  676. //
  677. // APC object
  678. //
  679. NTKERNELAPI
  680. VOID
  681. KeInitializeApc (
  682.     IN PRKAPC Apc,
  683.     IN PRKTHREAD Thread,
  684.     IN KAPC_ENVIRONMENT Environment,
  685.     IN PKKERNEL_ROUTINE KernelRoutine,
  686.     IN PKRUNDOWN_ROUTINE RundownRoutine OPTIONAL,
  687.     IN PKNORMAL_ROUTINE NormalRoutine OPTIONAL,
  688.     IN KPROCESSOR_MODE ProcessorMode OPTIONAL,
  689.     IN PVOID NormalContext OPTIONAL
  690.     );
  691. PLIST_ENTRY
  692. KeFlushQueueApc (
  693.     IN PKTHREAD Thread,
  694.     IN KPROCESSOR_MODE ProcessorMode
  695.     );
  696. NTKERNELAPI
  697. BOOLEAN
  698. KeInsertQueueApc (
  699.     IN PRKAPC Apc,
  700.     IN PVOID SystemArgument1,
  701.     IN PVOID SystemArgument2,
  702.     IN KPRIORITY Increment
  703.     );
  704. BOOLEAN
  705. KeRemoveQueueApc (
  706.     IN PKAPC Apc
  707.     );
  708. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  709. //
  710. // DPC object
  711. //
  712. NTKERNELAPI
  713. VOID
  714. KeInitializeDpc (
  715.     IN PRKDPC Dpc,
  716.     IN PKDEFERRED_ROUTINE DeferredRoutine,
  717.     IN PVOID DeferredContext
  718.     );
  719. NTKERNELAPI
  720. BOOLEAN
  721. KeInsertQueueDpc (
  722.     IN PRKDPC Dpc,
  723.     IN PVOID SystemArgument1,
  724.     IN PVOID SystemArgument2
  725.     );
  726. NTKERNELAPI
  727. BOOLEAN
  728. KeRemoveQueueDpc (
  729.     IN PRKDPC Dpc
  730.     );
  731. // end_wdm
  732. NTKERNELAPI
  733. VOID
  734. KeSetImportanceDpc (
  735.     IN PRKDPC Dpc,
  736.     IN KDPC_IMPORTANCE Importance
  737.     );
  738. NTKERNELAPI
  739. VOID
  740. KeSetTargetProcessorDpc (
  741.     IN PRKDPC Dpc,
  742.     IN CCHAR Number
  743.     );
  744. // begin_wdm
  745. //
  746. // Device queue object
  747. //
  748. NTKERNELAPI
  749. VOID
  750. KeInitializeDeviceQueue (
  751.     IN PKDEVICE_QUEUE DeviceQueue
  752.     );
  753. NTKERNELAPI
  754. BOOLEAN
  755. KeInsertDeviceQueue (
  756.     IN PKDEVICE_QUEUE DeviceQueue,
  757.     IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  758.     );
  759. NTKERNELAPI
  760. BOOLEAN
  761. KeInsertByKeyDeviceQueue (
  762.     IN PKDEVICE_QUEUE DeviceQueue,
  763.     IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry,
  764.     IN ULONG SortKey
  765.     );
  766. NTKERNELAPI
  767. PKDEVICE_QUEUE_ENTRY
  768. KeRemoveDeviceQueue (
  769.     IN PKDEVICE_QUEUE DeviceQueue
  770.     );
  771. NTKERNELAPI
  772. PKDEVICE_QUEUE_ENTRY
  773. KeRemoveByKeyDeviceQueue (
  774.     IN PKDEVICE_QUEUE DeviceQueue,
  775.     IN ULONG SortKey
  776.     );
  777. NTKERNELAPI
  778. BOOLEAN
  779. KeRemoveEntryDeviceQueue (
  780.     IN PKDEVICE_QUEUE DeviceQueue,
  781.     IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  782.     );
  783. // end_ntddk end_wdm end_nthal end_ntifs
  784. //
  785. // Interrupt object
  786. //
  787. NTKERNELAPI                                         // nthal
  788. VOID                                                // nthal
  789. KeInitializeInterrupt (                             // nthal
  790.     IN PKINTERRUPT Interrupt,                       // nthal
  791.     IN PKSERVICE_ROUTINE ServiceRoutine,            // nthal
  792.     IN PVOID ServiceContext,                        // nthal
  793.     IN PKSPIN_LOCK SpinLock OPTIONAL,               // nthal
  794.     IN ULONG Vector,                                // nthal
  795.     IN KIRQL Irql,                                  // nthal
  796.     IN KIRQL SynchronizeIrql,                       // nthal
  797.     IN KINTERRUPT_MODE InterruptMode,               // nthal
  798.     IN BOOLEAN ShareVector,                         // nthal
  799.     IN CCHAR ProcessorNumber,                       // nthal
  800.     IN BOOLEAN FloatingSave                         // nthal
  801.     );                                              // nthal
  802.                                                     // nthal
  803. NTKERNELAPI                                         // nthal
  804. BOOLEAN                                             // nthal
  805. KeConnectInterrupt (                                // nthal
  806.     IN PKINTERRUPT Interrupt                        // nthal
  807.     );                                              // nthal
  808.                                                     // nthal
  809. NTKERNELAPI
  810. BOOLEAN
  811. KeDisconnectInterrupt (
  812.     IN PKINTERRUPT Interrupt
  813.     );
  814. NTKERNELAPI                                         // ntddk wdm nthal
  815. BOOLEAN                                             // ntddk wdm nthal
  816. KeSynchronizeExecution (                            // ntddk wdm nthal
  817.     IN PKINTERRUPT Interrupt,                       // ntddk wdm nthal
  818.     IN PKSYNCHRONIZE_ROUTINE SynchronizeRoutine,    // ntddk wdm nthal
  819.     IN PVOID SynchronizeContext                     // ntddk wdm nthal
  820.     );                                              // ntddk wdm nthal
  821.                                                     // ntddk wdm nthal
  822. //
  823. // Profile object
  824. //
  825. VOID
  826. KeInitializeProfile (
  827.     IN PKPROFILE Profile,
  828.     IN PKPROCESS Process OPTIONAL,
  829.     IN PVOID RangeBase,
  830.     IN SIZE_T RangeSize,
  831.     IN ULONG BucketSize,
  832.     IN ULONG Segment,
  833.     IN KPROFILE_SOURCE ProfileSource,
  834.     IN KAFFINITY Affinity
  835.     );
  836. BOOLEAN
  837. KeStartProfile (
  838.     IN PKPROFILE Profile,
  839.     IN PULONG Buffer
  840.     );
  841. BOOLEAN
  842. KeStopProfile (
  843.     IN PKPROFILE Profile
  844.     );
  845. VOID
  846. KeSetIntervalProfile (
  847.     IN ULONG Interval,
  848.     IN KPROFILE_SOURCE Source
  849.     );
  850. ULONG
  851. KeQueryIntervalProfile (
  852.     IN KPROFILE_SOURCE Source
  853.     );
  854. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  855. //
  856. // Kernel dispatcher object functions
  857. //
  858. // Event Object
  859. //
  860. //  end_wdm end_ntddk end_nthal end_ntifs
  861. #if defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTHAL_)
  862. //  begin_wdm begin_ntddk begin_nthal begin_ntifs
  863. NTKERNELAPI
  864. VOID
  865. KeInitializeEvent (
  866.     IN PRKEVENT Event,
  867.     IN EVENT_TYPE Type,
  868.     IN BOOLEAN State
  869.     );
  870. NTKERNELAPI
  871. VOID
  872. KeClearEvent (
  873.     IN PRKEVENT Event
  874.     );
  875. //  end_wdm end_ntddk end_nthal end_ntifs
  876. #else
  877. #define KeInitializeEvent(_Event, _Type, _State)            
  878.     (_Event)->Header.Type = (UCHAR)_Type;                   
  879.     (_Event)->Header.Size =  sizeof(KEVENT) / sizeof(LONG); 
  880.     (_Event)->Header.SignalState = _State;                  
  881.     InitializeListHead(&(_Event)->Header.WaitListHead)
  882. #define KeClearEvent(Event) (Event)->Header.SignalState = 0
  883. #endif
  884. // begin_ntddk begin_ntifs
  885. NTKERNELAPI
  886. LONG
  887. KePulseEvent (
  888.     IN PRKEVENT Event,
  889.     IN KPRIORITY Increment,
  890.     IN BOOLEAN Wait
  891.     );
  892. // end_ntddk end_ntifs
  893. // begin_ntddk begin_nthal begin_ntifs
  894. NTKERNELAPI
  895. LONG
  896. KeReadStateEvent (
  897.     IN PRKEVENT Event
  898.     );
  899. //  begin_wdm
  900. NTKERNELAPI
  901. LONG
  902. KeResetEvent (
  903.     IN PRKEVENT Event
  904.     );
  905. NTKERNELAPI
  906. LONG
  907. KeSetEvent (
  908.     IN PRKEVENT Event,
  909.     IN KPRIORITY Increment,
  910.     IN BOOLEAN Wait
  911.     );
  912. // end_ntddk end_wdm end_nthal end_ntifs
  913. VOID
  914. KeSetEventBoostPriority (
  915.     IN PRKEVENT Event,
  916.     IN PRKTHREAD *Thread OPTIONAL
  917.     );
  918. VOID
  919. KeInitializeEventPair (
  920.     IN PKEVENT_PAIR EventPair
  921.     );
  922. #define KeSetHighEventPair(EventPair, Increment, Wait) 
  923.     KeSetEvent(&((EventPair)->EventHigh),              
  924.                Increment,                              
  925.                Wait)
  926. #define KeSetLowEventPair(EventPair, Increment, Wait)  
  927.     KeSetEvent(&((EventPair)->EventLow),               
  928.                Increment,                              
  929.                Wait)
  930. //
  931. // Mutant object
  932. //
  933. NTKERNELAPI
  934. VOID
  935. KeInitializeMutant (
  936.     IN PRKMUTANT Mutant,
  937.     IN BOOLEAN InitialOwner
  938.     );
  939. LONG
  940. KeReadStateMutant (
  941.     IN PRKMUTANT
  942.     );
  943. NTKERNELAPI
  944. LONG
  945. KeReleaseMutant (
  946.     IN PRKMUTANT Mutant,
  947.     IN KPRIORITY Increment,
  948.     IN BOOLEAN Abandoned,
  949.     IN BOOLEAN Wait
  950.     );
  951. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  952. //
  953. // Mutex object
  954. //
  955. NTKERNELAPI
  956. VOID
  957. KeInitializeMutex (
  958.     IN PRKMUTEX Mutex,
  959.     IN ULONG Level
  960.     );
  961. #define KeReadStateMutex(Mutex) KeReadStateMutant(Mutex)
  962. NTKERNELAPI
  963. LONG
  964. KeReleaseMutex (
  965.     IN PRKMUTEX Mutex,
  966.     IN BOOLEAN Wait
  967.     );
  968. // end_ntddk end_wdm
  969. //
  970. // Queue Object.
  971. //
  972. NTKERNELAPI
  973. VOID
  974. KeInitializeQueue (
  975.     IN PRKQUEUE Queue,
  976.     IN ULONG Count OPTIONAL
  977.     );
  978. NTKERNELAPI
  979. LONG
  980. KeReadStateQueue (
  981.     IN PRKQUEUE Queue
  982.     );
  983. NTKERNELAPI
  984. LONG
  985. KeInsertQueue (
  986.     IN PRKQUEUE Queue,
  987.     IN PLIST_ENTRY Entry
  988.     );
  989. NTKERNELAPI
  990. LONG
  991. KeInsertHeadQueue (
  992.     IN PRKQUEUE Queue,
  993.     IN PLIST_ENTRY Entry
  994.     );
  995. NTKERNELAPI
  996. PLIST_ENTRY
  997. KeRemoveQueue (
  998.     IN PRKQUEUE Queue,
  999.     IN KPROCESSOR_MODE WaitMode,
  1000.     IN PLARGE_INTEGER Timeout OPTIONAL
  1001.     );
  1002. PLIST_ENTRY
  1003. KeRundownQueue (
  1004.     IN PRKQUEUE Queue
  1005.     );
  1006. // begin_ntddk begin_wdm
  1007. //
  1008. // Semaphore object
  1009. //
  1010. NTKERNELAPI
  1011. VOID
  1012. KeInitializeSemaphore (
  1013.     IN PRKSEMAPHORE Semaphore,
  1014.     IN LONG Count,
  1015.     IN LONG Limit
  1016.     );
  1017. NTKERNELAPI
  1018. LONG
  1019. KeReadStateSemaphore (
  1020.     IN PRKSEMAPHORE Semaphore
  1021.     );
  1022. NTKERNELAPI
  1023. LONG
  1024. KeReleaseSemaphore (
  1025.     IN PRKSEMAPHORE Semaphore,
  1026.     IN KPRIORITY Increment,
  1027.     IN LONG Adjustment,
  1028.     IN BOOLEAN Wait
  1029.     );
  1030. // end_ntddk end_wdm end_nthal end_ntifs
  1031. //
  1032. // Process object
  1033. //
  1034. VOID
  1035. KeInitializeProcess (
  1036.     IN PRKPROCESS Process,
  1037.     IN KPRIORITY Priority,
  1038.     IN KAFFINITY Affinity,
  1039.     IN ULONG_PTR DirectoryTableBase[2],
  1040.     IN BOOLEAN Enable
  1041.     );
  1042. LOGICAL
  1043. KeForceAttachProcess (
  1044.     IN PKPROCESS Process
  1045.     );
  1046. // begin_ntifs
  1047. NTKERNELAPI
  1048. VOID
  1049. KeAttachProcess (
  1050.     IN PRKPROCESS Process
  1051.     );
  1052. NTKERNELAPI
  1053. VOID
  1054. KeDetachProcess (
  1055.     VOID
  1056.     );
  1057. NTKERNELAPI
  1058. VOID
  1059. KeStackAttachProcess (
  1060.     IN PRKPROCESS PROCESS,
  1061.     OUT PRKAPC_STATE ApcState
  1062.     );
  1063. NTKERNELAPI
  1064. VOID
  1065. KeUnstackDetachProcess (
  1066.     IN PRKAPC_STATE ApcState
  1067.     );
  1068. // end_ntifs
  1069. #define KeIsAttachedProcess() 
  1070.     (KeGetCurrentThread()->ApcStateIndex == AttachedApcEnvironment)
  1071. LONG
  1072. KeReadStateProcess (
  1073.     IN PRKPROCESS Process
  1074.     );
  1075. BOOLEAN
  1076. KeSetAutoAlignmentProcess (
  1077.     IN PRKPROCESS Process,
  1078.     IN BOOLEAN Enable
  1079.     );
  1080. LONG
  1081. KeSetProcess (
  1082.     IN PRKPROCESS Process,
  1083.     IN KPRIORITY Increment,
  1084.     IN BOOLEAN Wait
  1085.     );
  1086. KPRIORITY
  1087. KeSetPriorityProcess (
  1088.     IN PKPROCESS Process,
  1089.     IN KPRIORITY BasePriority
  1090.     );
  1091. LOGICAL
  1092. KeSetDisableQuantumProcess (
  1093.     IN PKPROCESS Process,
  1094.     IN LOGICAL Disable
  1095.     );
  1096. #define KeTerminateProcess(Process) 
  1097.     (Process)->StackCount += 1;
  1098. //
  1099. // Thread object
  1100. //
  1101. VOID
  1102. KeInitializeThread (
  1103.     IN PKTHREAD Thread,
  1104.     IN PVOID KernelStack,
  1105.     IN PKSYSTEM_ROUTINE SystemRoutine,
  1106.     IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  1107.     IN PVOID StartContext OPTIONAL,
  1108.     IN PCONTEXT ContextFrame OPTIONAL,
  1109.     IN PVOID Teb OPTIONAL,
  1110.     IN PKPROCESS Process
  1111.     );
  1112. BOOLEAN
  1113. KeAlertThread (
  1114.     IN PKTHREAD Thread,
  1115.     IN KPROCESSOR_MODE ProcessorMode
  1116.     );
  1117. ULONG
  1118. KeAlertResumeThread (
  1119.     IN PKTHREAD Thread
  1120.     );
  1121. NTKERNELAPI
  1122. VOID
  1123. KeBoostCurrentThread (
  1124.     VOID
  1125.     );
  1126. VOID
  1127. KeBoostPriorityThread (
  1128.     IN PKTHREAD Thread,
  1129.     IN KPRIORITY Increment
  1130.     );
  1131. KAFFINITY
  1132. KeConfineThread (
  1133.     VOID
  1134.     );
  1135. NTKERNELAPI                                         // ntddk wdm nthal ntifs
  1136. NTSTATUS                                            // ntddk wdm nthal ntifs
  1137. KeDelayExecutionThread (                            // ntddk wdm nthal ntifs
  1138.     IN KPROCESSOR_MODE WaitMode,                    // ntddk wdm nthal ntifs
  1139.     IN BOOLEAN Alertable,                           // ntddk wdm nthal ntifs
  1140.     IN PLARGE_INTEGER Interval                      // ntddk wdm nthal ntifs
  1141.     );                                              // ntddk wdm nthal ntifs
  1142.                                                     // ntddk wdm nthal ntifs
  1143. BOOLEAN
  1144. KeDisableApcQueuingThread (
  1145.     IN PKTHREAD Thread
  1146.     );
  1147. BOOLEAN
  1148. KeEnableApcQueuingThread (
  1149.     IN PKTHREAD
  1150.     );
  1151. LOGICAL
  1152. KeSetDisableBoostThread (
  1153.     IN PKTHREAD Thread,
  1154.     IN LOGICAL Disable
  1155.     );
  1156. ULONG
  1157. KeForceResumeThread (
  1158.     IN PKTHREAD Thread
  1159.     );
  1160. VOID
  1161. KeFreezeAllThreads (
  1162.     VOID
  1163.     );
  1164. BOOLEAN
  1165. KeQueryAutoAlignmentThread (
  1166.     IN PKTHREAD Thread
  1167.     );
  1168. LONG
  1169. KeQueryBasePriorityThread (
  1170.     IN PKTHREAD Thread
  1171.     );
  1172. NTKERNELAPI                                         // ntddk wdm nthal ntifs
  1173. KPRIORITY                                           // ntddk wdm nthal ntifs
  1174. KeQueryPriorityThread (                             // ntddk wdm nthal ntifs
  1175.     IN PKTHREAD Thread                              // ntddk wdm nthal ntifs
  1176.     );                                              // ntddk wdm nthal ntifs
  1177.                                                     // ntddk wdm nthal ntifs
  1178. BOOLEAN
  1179. KeReadStateThread (
  1180.     IN PKTHREAD Thread
  1181.     );
  1182. VOID
  1183. KeReadyThread (
  1184.     IN PKTHREAD Thread
  1185.     );
  1186. ULONG
  1187. KeResumeThread (
  1188.     IN PKTHREAD Thread
  1189.     );
  1190. VOID                                                // nthal
  1191. KeRevertToUserAffinityThread (                      // nthal
  1192.     VOID                                            // nthal
  1193.     );                                              // nthal
  1194. VOID
  1195. KeRundownThread (
  1196.     VOID
  1197.     );
  1198. KAFFINITY
  1199. KeSetAffinityThread (
  1200.     IN PKTHREAD Thread,
  1201.     IN KAFFINITY Affinity
  1202.     );
  1203. VOID                                                // nthal
  1204. KeSetSystemAffinityThread (                         // nthal
  1205.     IN KAFFINITY Affinity                           // nthal
  1206.     );                                              // nthal
  1207. BOOLEAN
  1208. KeSetAutoAlignmentThread (
  1209.     IN PKTHREAD Thread,
  1210.     IN BOOLEAN Enable
  1211.     );
  1212. NTKERNELAPI                                         // ntddk nthal ntifs
  1213. LONG                                                // ntddk nthal ntifs
  1214. KeSetBasePriorityThread (                           // ntddk nthal ntifs
  1215.     IN PKTHREAD Thread,                             // ntddk nthal ntifs
  1216.     IN LONG Increment                               // ntddk nthal ntifs
  1217.     );                                              // ntddk nthal ntifs
  1218.                                                     // ntddk nthal ntifs
  1219. // begin_ntsrv
  1220. NTKERNELAPI
  1221. CCHAR
  1222. KeSetIdealProcessorThread (
  1223.     IN PKTHREAD Thread,
  1224.     IN CCHAR Processor
  1225.     );
  1226. // end_ntsrv
  1227. NTKERNELAPI
  1228. BOOLEAN
  1229. KeSetKernelStackSwapEnable (
  1230.     IN BOOLEAN Enable
  1231.     );
  1232. NTKERNELAPI                                         // ntddk wdm nthal ntifs
  1233. KPRIORITY                                           // ntddk wdm nthal ntifs
  1234. KeSetPriorityThread (                               // ntddk wdm nthal ntifs
  1235.     IN PKTHREAD Thread,                             // ntddk wdm nthal ntifs
  1236.     IN KPRIORITY Priority                           // ntddk wdm nthal ntifs
  1237.     );                                              // ntddk wdm nthal ntifs
  1238.                                                     // ntddk wdm nthal ntifs
  1239. ULONG
  1240. KeSuspendThread (
  1241.     IN PKTHREAD
  1242.     );
  1243. NTKERNELAPI
  1244. VOID
  1245. KeTerminateThread (
  1246.     IN KPRIORITY Increment
  1247.     );
  1248. BOOLEAN
  1249. KeTestAlertThread (
  1250.     IN KPROCESSOR_MODE
  1251.     );
  1252. VOID
  1253. KeThawAllThreads (
  1254.     VOID
  1255.     );
  1256. //
  1257. // Define leave critical region macro used for inline and function code
  1258. // generation.
  1259. //
  1260. // Warning: assembly versions of this code are included directly in
  1261. // ntgdi assembly routines mutexs.s for MIPS and locka.asm for i386.
  1262. // Any changes made to KeEnterCriticalRegion/KeEnterCriticalRegion
  1263. // must be reflected in these routines.
  1264. //
  1265. #define KiLeaveCriticalRegion() {                                       
  1266.     PKTHREAD Thread;                                                    
  1267.     Thread = KeGetCurrentThread();                                      
  1268.     if (((*((volatile ULONG *)&Thread->KernelApcDisable) += 1) == 0) && 
  1269.         (((volatile LIST_ENTRY *)&Thread->ApcState.ApcListHead[KernelMode])->Flink != 
  1270.          &Thread->ApcState.ApcListHead[KernelMode])) {                  
  1271.         Thread->ApcState.KernelApcPending = TRUE;                       
  1272.         KiRequestSoftwareInterrupt(APC_LEVEL);                          
  1273.     }                                                                   
  1274. }
  1275. // begin_ntddk begin_nthal begin_ntifs
  1276. #if (defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTHAL_)) && !defined(_NTSYSTEM_DRIVER_)
  1277. // begin_wdm
  1278. NTKERNELAPI
  1279. VOID
  1280. KeEnterCriticalRegion (
  1281.     VOID
  1282.     );
  1283. NTKERNELAPI
  1284. VOID
  1285. KeLeaveCriticalRegion (
  1286.     VOID
  1287.     );
  1288. // end_wdm
  1289. #else
  1290. //++
  1291. //
  1292. // VOID
  1293. // KeEnterCriticalRegion (
  1294. //    VOID
  1295. //    )
  1296. //
  1297. //
  1298. // Routine Description:
  1299. //
  1300. //    This function disables kernel APC's.
  1301. //
  1302. //    N.B. The following code does not require any interlocks. There are
  1303. //         two cases of interest: 1) On an MP system, the thread cannot
  1304. //         be running on two processors as once, and 2) if the thread is
  1305. //         is interrupted to deliver a kernel mode APC which also calls
  1306. //         this routine, the values read and stored will stack and unstack
  1307. //         properly.
  1308. //
  1309. // Arguments:
  1310. //
  1311. //    None.
  1312. //
  1313. // Return Value:
  1314. //
  1315. //    None.
  1316. //--
  1317. #define KeEnterCriticalRegion() KeGetCurrentThread()->KernelApcDisable -= 1;
  1318. //++
  1319. //
  1320. // VOID
  1321. // KeLeaveCriticalRegion (
  1322. //    VOID
  1323. //    )
  1324. //
  1325. //
  1326. // Routine Description:
  1327. //
  1328. //    This function enables kernel APC's.
  1329. //
  1330. //    N.B. The following code does not require any interlocks. There are
  1331. //         two cases of interest: 1) On an MP system, the thread cannot
  1332. //         be running on two processors as once, and 2) if the thread is
  1333. //         is interrupted to deliver a kernel mode APC which also calls
  1334. //         this routine, the values read and stored will stack and unstack
  1335. //         properly.
  1336. //
  1337. // Arguments:
  1338. //
  1339. //    None.
  1340. //
  1341. // Return Value:
  1342. //
  1343. //    None.
  1344. //--
  1345. #define KeLeaveCriticalRegion() KiLeaveCriticalRegion()
  1346. #endif
  1347. //  begin_wdm
  1348. //
  1349. // Timer object
  1350. //
  1351. NTKERNELAPI
  1352. VOID
  1353. KeInitializeTimer (
  1354.     IN PKTIMER Timer
  1355.     );
  1356. NTKERNELAPI
  1357. VOID
  1358. KeInitializeTimerEx (
  1359.     IN PKTIMER Timer,
  1360.     IN TIMER_TYPE Type
  1361.     );
  1362. NTKERNELAPI
  1363. BOOLEAN
  1364. KeCancelTimer (
  1365.     IN PKTIMER
  1366.     );
  1367. NTKERNELAPI
  1368. BOOLEAN
  1369. KeReadStateTimer (
  1370.     PKTIMER Timer
  1371.     );
  1372. NTKERNELAPI
  1373. BOOLEAN
  1374. KeSetTimer (
  1375.     IN PKTIMER Timer,
  1376.     IN LARGE_INTEGER DueTime,
  1377.     IN PKDPC Dpc OPTIONAL
  1378.     );
  1379. NTKERNELAPI
  1380. BOOLEAN
  1381. KeSetTimerEx (
  1382.     IN PKTIMER Timer,
  1383.     IN LARGE_INTEGER DueTime,
  1384.     IN LONG Period OPTIONAL,
  1385.     IN PKDPC Dpc OPTIONAL
  1386.     );
  1387. // end_ntddk end_nthal end_ntifs end_wdm
  1388. PVOID
  1389. KeCheckForTimer(
  1390.     IN PVOID p,
  1391.     IN ULONG Size
  1392.     );
  1393. VOID
  1394. KeClearTimer (
  1395.     IN PKTIMER Timer
  1396.     );
  1397. ULONGLONG
  1398. KeQueryTimerDueTime (
  1399.     IN PKTIMER Timer
  1400.     );
  1401. //
  1402. // Wait functions
  1403. //
  1404. NTSTATUS
  1405. KiSetServerWaitClientEvent (
  1406.     IN PKEVENT SeverEvent,
  1407.     IN PKEVENT ClientEvent,
  1408.     IN ULONG WaitMode
  1409.     );
  1410. #if 0
  1411. NTSTATUS
  1412. KeReleaseWaitForSemaphore (
  1413.     IN PKSEMAPHORE Server,
  1414.     IN PKSEMAPHORE Client,
  1415.     IN ULONG WaitReason,
  1416.     IN ULONG WaitMode
  1417.     );
  1418. #endif
  1419. #define KeSetHighWaitLowEventPair(EventPair, WaitMode)                  
  1420.     KiSetServerWaitClientEvent(&((EventPair)->EventHigh),               
  1421.                                &((EventPair)->EventLow),                
  1422.                                WaitMode)
  1423. #define KeSetLowWaitHighEventPair(EventPair, WaitMode)                  
  1424.     KiSetServerWaitClientEvent(&((EventPair)->EventLow),                
  1425.                                &((EventPair)->EventHigh),               
  1426.                                WaitMode)
  1427. #define KeWaitForHighEventPair(EventPair, WaitMode, Alertable, TimeOut) 
  1428.     KeWaitForSingleObject(&((EventPair)->EventHigh),                    
  1429.                           WrEventPair,                                  
  1430.                           WaitMode,                                     
  1431.                           Alertable,                                    
  1432.                           TimeOut)
  1433. #define KeWaitForLowEventPair(EventPair, WaitMode, Alertable, TimeOut)  
  1434.     KeWaitForSingleObject(&((EventPair)->EventLow),                     
  1435.                           WrEventPair,                                  
  1436.                           WaitMode,                                     
  1437.                           Alertable,                                    
  1438.                           TimeOut)
  1439. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  1440. #define KeWaitForMutexObject KeWaitForSingleObject
  1441. NTKERNELAPI
  1442. NTSTATUS
  1443. KeWaitForMultipleObjects (
  1444.     IN ULONG Count,
  1445.     IN PVOID Object[],
  1446.     IN WAIT_TYPE WaitType,
  1447.     IN KWAIT_REASON WaitReason,
  1448.     IN KPROCESSOR_MODE WaitMode,
  1449.     IN BOOLEAN Alertable,
  1450.     IN PLARGE_INTEGER Timeout OPTIONAL,
  1451.     IN PKWAIT_BLOCK WaitBlockArray OPTIONAL
  1452.     );
  1453. NTKERNELAPI
  1454. NTSTATUS
  1455. KeWaitForSingleObject (
  1456.     IN PVOID Object,
  1457.     IN KWAIT_REASON WaitReason,
  1458.     IN KPROCESSOR_MODE WaitMode,
  1459.     IN BOOLEAN Alertable,
  1460.     IN PLARGE_INTEGER Timeout OPTIONAL
  1461.     );
  1462. // end_ntddk end_wdm end_nthal end_ntifs
  1463. //
  1464. // Define internal kernel functions.
  1465. //
  1466. // N.B. These definitions are not public and are used elsewhere only under
  1467. //      very special circumstances.
  1468. //
  1469. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntndis
  1470. //
  1471. // On X86 the following routines are defined in the HAL and imported by
  1472. // all other modules.
  1473. //
  1474. #if defined(_X86_) && !defined(_NTHAL_)
  1475. #define _DECL_HAL_KE_IMPORT  __declspec(dllimport)
  1476. #else
  1477. #define _DECL_HAL_KE_IMPORT
  1478. #endif
  1479. // end_ntddk end_wdm end_nthal end_ntifs end_ntndis
  1480. #if defined(_ALPHA_) || defined(_X86_)
  1481. #if defined(NT_UP)
  1482. #define KeAcquireQueuedSpinLockRaiseToSynch(Number) 
  1483.     KeRaiseIrqlToSynchLevel()
  1484. #define KeAcquireQueuedSpinLock(Number) 
  1485.     KfRaiseIrql(DISPATCH_LEVEL)
  1486. #define KeReleaseQueuedSpinLock(Number, OldIrql) 
  1487.     KeLowerIrql(OldIrql)
  1488. #define KeTryToAcquireQueuedSpinLockRaiseToSynch(Number, OldIrql) 
  1489.     (*(OldIrql) = KeRaiseIrqlToSynchLevel(), TRUE)
  1490. #define KeTryToAcquireQueuedSpinLock(Number, OldIrql) 
  1491.     (KeRaiseIrql(DISPATCH_LEVEL, OldIrql), TRUE)
  1492. #define KiAcquireQueuedSpinLock(LockQueue)
  1493. #define KiReleaseQueuedSpinLock(LockQueue)
  1494. #else // NT_UP
  1495. VOID
  1496. FASTCALL
  1497. KiAcquireQueuedSpinLock (
  1498.     IN PKSPIN_LOCK_QUEUE LockQueue
  1499.     );
  1500. VOID
  1501. FASTCALL
  1502. KiReleaseQueuedSpinLock (
  1503.     IN PKSPIN_LOCK_QUEUE LockQueue
  1504.     );
  1505. _DECL_HAL_KE_IMPORT
  1506. KIRQL
  1507. FASTCALL
  1508. KeAcquireQueuedSpinLockRaiseToSynch (
  1509.     IN KSPIN_LOCK_QUEUE_NUMBER Number
  1510.     );
  1511. _DECL_HAL_KE_IMPORT
  1512. KIRQL
  1513. FASTCALL
  1514. KeAcquireQueuedSpinLock (
  1515.     IN KSPIN_LOCK_QUEUE_NUMBER Number
  1516.     );
  1517. _DECL_HAL_KE_IMPORT
  1518. VOID
  1519. FASTCALL
  1520. KeReleaseQueuedSpinLock (
  1521.     IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1522.     IN KIRQL OldIrql
  1523.     );
  1524. _DECL_HAL_KE_IMPORT
  1525. LOGICAL
  1526. FASTCALL
  1527. KeTryToAcquireQueuedSpinLockRaiseToSynch(
  1528.     IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1529.     IN PKIRQL OldIrql
  1530.     );
  1531. _DECL_HAL_KE_IMPORT
  1532. LOGICAL
  1533. FASTCALL
  1534. KeTryToAcquireQueuedSpinLock(
  1535.     IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1536.     IN PKIRQL OldIrql
  1537.     );
  1538. #endif  // NT_UP
  1539. #endif  // _ALPHA_ || _X86_
  1540. #if defined(NT_UP)
  1541. //
  1542. // On Uni-processor systems there is no real Dispatcher Database Lock
  1543. // so raising to SYNCH won't help get the lock released any sooner.
  1544. // On X86, these functions are implemented in the HAL and don't use
  1545. // the KiSynchLevel variable, on other platforms, KiSynchLevel can
  1546. // be set appropriately. 
  1547. //
  1548. #if defined(_X86_)
  1549. #define KiLockDispatcherDatabase(OldIrql) 
  1550.     *(OldIrql) = KeRaiseIrqlToDpcLevel()
  1551. #else
  1552. #define KiLockDispatcherDatabase(OldIrql) 
  1553.     *(OldIrql) = KeRaiseIrqlToSynchLevel()
  1554. #endif
  1555. #else   // NT_UP
  1556. #if defined(_ALPHA_) || defined(_X86_)
  1557. #define KiLockDispatcherDatabase(OldIrql) 
  1558.     *(OldIrql) = KeAcquireQueuedSpinLockRaiseToSynch(LockQueueDispatcherLock)
  1559. #else   // _ALPHA_ || _X86_
  1560. #define KiLockDispatcherDatabase(OldIrql) 
  1561.     *(OldIrql) = KeAcquireSpinLockRaiseToSynch(&KiDispatcherLock)
  1562. #endif  // _ALPHA_ || _X86_
  1563. #endif  // NT_UP
  1564. VOID
  1565. FASTCALL
  1566. KiSetPriorityThread (
  1567.     IN PRKTHREAD Thread,
  1568.     IN KPRIORITY Priority
  1569.     );
  1570. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntndis
  1571. //
  1572. // spin lock functions
  1573. //
  1574. NTKERNELAPI
  1575. VOID
  1576. NTAPI
  1577. KeInitializeSpinLock (
  1578.     IN PKSPIN_LOCK SpinLock
  1579.     );
  1580. #if defined(_X86_)
  1581. NTKERNELAPI
  1582. VOID
  1583. FASTCALL
  1584. KefAcquireSpinLockAtDpcLevel (
  1585.     IN PKSPIN_LOCK SpinLock
  1586.     );
  1587. NTKERNELAPI
  1588. VOID
  1589. FASTCALL
  1590. KefReleaseSpinLockFromDpcLevel (
  1591.     IN PKSPIN_LOCK SpinLock
  1592.     );
  1593. #define KeAcquireSpinLockAtDpcLevel(a)      KefAcquireSpinLockAtDpcLevel(a)
  1594. #define KeReleaseSpinLockFromDpcLevel(a)    KefReleaseSpinLockFromDpcLevel(a)
  1595. _DECL_HAL_KE_IMPORT
  1596. KIRQL
  1597. FASTCALL
  1598. KfAcquireSpinLock (
  1599.     IN PKSPIN_LOCK SpinLock
  1600.     );
  1601. _DECL_HAL_KE_IMPORT
  1602. VOID
  1603. FASTCALL
  1604. KfReleaseSpinLock (
  1605.     IN PKSPIN_LOCK SpinLock,
  1606.     IN KIRQL NewIrql
  1607.     );
  1608. // end_wdm
  1609. _DECL_HAL_KE_IMPORT
  1610. KIRQL
  1611. FASTCALL
  1612. KeAcquireSpinLockRaiseToSynch (
  1613.     IN PKSPIN_LOCK SpinLock
  1614.     );
  1615. // begin_wdm
  1616. #define KeAcquireSpinLock(a,b)  *(b) = KfAcquireSpinLock(a)
  1617. #define KeReleaseSpinLock(a,b)  KfReleaseSpinLock(a,b)
  1618. #else
  1619. NTKERNELAPI
  1620. KIRQL
  1621. FASTCALL
  1622. KeAcquireSpinLockRaiseToSynch (
  1623.     IN PKSPIN_LOCK SpinLock
  1624.     );
  1625. NTKERNELAPI
  1626. VOID
  1627. KeAcquireSpinLockAtDpcLevel (
  1628.     IN PKSPIN_LOCK SpinLock
  1629.     );
  1630. NTKERNELAPI
  1631. VOID
  1632. KeReleaseSpinLockFromDpcLevel (
  1633.     IN PKSPIN_LOCK SpinLock
  1634.     );
  1635. NTKERNELAPI
  1636. KIRQL
  1637. KeAcquireSpinLockRaiseToDpc (
  1638.     IN PKSPIN_LOCK SpinLock
  1639.     );
  1640. #define KeAcquireSpinLock(SpinLock, OldIrql) 
  1641.     *(OldIrql) = KeAcquireSpinLockRaiseToDpc(SpinLock)
  1642. NTKERNELAPI
  1643. VOID
  1644. KeReleaseSpinLock (
  1645.     IN PKSPIN_LOCK SpinLock,
  1646.     IN KIRQL NewIrql
  1647.     );
  1648. #endif
  1649. //  end_wdm end_ntddk end_nthal end_ntifs end_ntndis
  1650. BOOLEAN
  1651. KeTryToAcquireSpinLock (
  1652.     IN PKSPIN_LOCK SpinLock,
  1653.     OUT PKIRQL OldIrql
  1654.     );
  1655. //
  1656. // Raise and lower IRQL functions.
  1657. //
  1658. //  begin_nthal begin_wdm begin_ntddk begin_ntifs
  1659. #if defined(_X86_)
  1660. _DECL_HAL_KE_IMPORT
  1661. VOID
  1662. FASTCALL
  1663. KfLowerIrql (
  1664.     IN KIRQL NewIrql
  1665.     );
  1666. _DECL_HAL_KE_IMPORT
  1667. KIRQL
  1668. FASTCALL
  1669. KfRaiseIrql (
  1670.     IN KIRQL NewIrql
  1671.     );
  1672. // end_wdm
  1673. _DECL_HAL_KE_IMPORT
  1674. KIRQL
  1675. KeRaiseIrqlToDpcLevel(
  1676.     VOID
  1677.     );
  1678. _DECL_HAL_KE_IMPORT
  1679. KIRQL
  1680. KeRaiseIrqlToSynchLevel(
  1681.     VOID
  1682.     );
  1683. // begin_wdm
  1684. #define KeLowerIrql(a)      KfLowerIrql(a)
  1685. #define KeRaiseIrql(a,b)    *(b) = KfRaiseIrql(a)
  1686. // end_wdm
  1687. // begin_wdm
  1688. #elif defined(_ALPHA_)
  1689. #define KeLowerIrql(a)      __swpirql(a)
  1690. #define KeRaiseIrql(a,b)    *(b) = __swpirql(a)
  1691. // end_wdm
  1692. #define KfRaiseIrql(a)      __swpirql(a)
  1693. #define KeRaiseIrqlToDpcLevel() __swpirql(DISPATCH_LEVEL)
  1694. #define KeRaiseIrqlToSynchLevel() __swpirql((UCHAR)KiSynchIrql)
  1695. // begin_wdm
  1696. #elif defined(_IA64_)
  1697. VOID
  1698. KeLowerIrql (
  1699.     IN KIRQL NewIrql
  1700.     );
  1701. VOID
  1702. KeRaiseIrql (
  1703.     IN KIRQL NewIrql,
  1704.     OUT PKIRQL OldIrql
  1705.     );
  1706. // end_wdm
  1707. KIRQL
  1708. KeRaiseIrqlToDpcLevel (
  1709.     VOID
  1710.     );
  1711. KIRQL
  1712. KeRaiseIrqlToSynchLevel (
  1713.     VOID
  1714.     );
  1715. // begin_wdm
  1716. #endif
  1717. //  end_nthal end_wdm end_ntddk end_ntifs
  1718. //
  1719. // Initialize kernel in phase 1.
  1720. //
  1721. BOOLEAN
  1722. KeInitSystem(
  1723.     VOID
  1724.     );
  1725. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  1726. //
  1727. // Miscellaneous kernel functions
  1728. //
  1729. // end_wdm
  1730. BOOLEAN
  1731. KeGetBugMessageText(
  1732.     IN ULONG MessageId,
  1733.     IN PANSI_STRING ReturnedString OPTIONAL
  1734.     );
  1735. typedef enum _KBUGCHECK_BUFFER_DUMP_STATE {
  1736.     BufferEmpty,
  1737.     BufferInserted,
  1738.     BufferStarted,
  1739.     BufferFinished,
  1740.     BufferIncomplete
  1741. } KBUGCHECK_BUFFER_DUMP_STATE;
  1742. typedef
  1743. VOID
  1744. (*PKBUGCHECK_CALLBACK_ROUTINE) (
  1745.     IN PVOID Buffer,
  1746.     IN ULONG Length
  1747.     );
  1748. typedef struct _KBUGCHECK_CALLBACK_RECORD {
  1749.     LIST_ENTRY Entry;
  1750.     PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine;
  1751.     PVOID Buffer;
  1752.     ULONG Length;
  1753.     PUCHAR Component;
  1754.     ULONG_PTR Checksum;
  1755.     UCHAR State;
  1756. } KBUGCHECK_CALLBACK_RECORD, *PKBUGCHECK_CALLBACK_RECORD;
  1757. NTKERNELAPI
  1758. DECLSPEC_NORETURN
  1759. VOID
  1760. NTAPI
  1761. KeBugCheck (
  1762.     IN ULONG BugCheckCode
  1763.     );
  1764. // begin_wdm
  1765. NTKERNELAPI
  1766. DECLSPEC_NORETURN
  1767. VOID
  1768. KeBugCheckEx(
  1769.     IN ULONG BugCheckCode,
  1770.     IN ULONG_PTR BugCheckParameter1,
  1771.     IN ULONG_PTR BugCheckParameter2,
  1772.     IN ULONG_PTR BugCheckParameter3,
  1773.     IN ULONG_PTR BugCheckParameter4
  1774.     );
  1775. // end_wdm
  1776. #define KeInitializeCallbackRecord(CallbackRecord) 
  1777.     (CallbackRecord)->State = BufferEmpty
  1778. NTKERNELAPI
  1779. BOOLEAN
  1780. KeDeregisterBugCheckCallback (
  1781.     IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
  1782.     );
  1783. NTKERNELAPI
  1784. BOOLEAN
  1785. KeRegisterBugCheckCallback (
  1786.     IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord,
  1787.     IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine,
  1788.     IN PVOID Buffer,
  1789.     IN ULONG Length,
  1790.     IN PUCHAR Component
  1791.     );
  1792. NTKERNELAPI
  1793. VOID
  1794. KeEnterKernelDebugger (
  1795.     VOID
  1796.     );
  1797. // end_ntddk end_nthal end_ntifs
  1798. typedef
  1799. PCHAR
  1800. (*PKE_BUGCHECK_UNICODE_TO_ANSI) (
  1801.     IN PUNICODE_STRING UnicodeString,
  1802.     OUT PCHAR AnsiBuffer,
  1803.     IN ULONG MaxAnsiLength
  1804.     );
  1805. VOID
  1806. KeDumpMachineState (
  1807.     IN PKPROCESSOR_STATE ProcessorState,
  1808.     IN PCHAR Buffer,
  1809.     IN PULONG_PTR BugCheckParameters,
  1810.     IN ULONG NumberOfParameters,
  1811.     IN PKE_BUGCHECK_UNICODE_TO_ANSI UnicodeToAnsiRoutine
  1812.     );
  1813. VOID
  1814. KeContextFromKframes (
  1815.     IN PKTRAP_FRAME TrapFrame,
  1816.     IN PKEXCEPTION_FRAME ExceptionFrame,
  1817.     IN OUT PCONTEXT ContextFrame
  1818.     );
  1819. VOID
  1820. KeContextToKframes (
  1821.     IN OUT PKTRAP_FRAME TrapFrame,
  1822.     IN OUT PKEXCEPTION_FRAME ExceptionFrame,
  1823.     IN PCONTEXT ContextFrame,
  1824.     IN ULONG ContextFlags,
  1825.     IN KPROCESSOR_MODE PreviousMode
  1826.     );
  1827. // begin_nthal
  1828. VOID
  1829. __cdecl
  1830. KeSaveStateForHibernate(
  1831.     IN PKPROCESSOR_STATE ProcessorState
  1832.     );
  1833. // end_nthal
  1834. VOID
  1835. KeCopyTrapDispatcher (
  1836.     VOID
  1837.     );
  1838. BOOLEAN
  1839. FASTCALL
  1840. KeInvalidAccessAllowed (
  1841.     IN PVOID TrapInformation OPTIONAL
  1842.     );
  1843. //
  1844. //  GDI TEB Batch Flush routine
  1845. //
  1846. typedef
  1847. VOID
  1848. (*PGDI_BATCHFLUSH_ROUTINE) (
  1849.     VOID
  1850.     );
  1851. NTKERNELAPI                                         // nthal
  1852. VOID                                                // nthal
  1853. KeFlushCurrentTb (                                  // nthal
  1854.     VOID                                            // nthal
  1855.     );                                              // nthal
  1856.                                                     // nthal
  1857. //
  1858. // UCHAR
  1859. // FindFirstSetRightMember(Set)
  1860. //
  1861. // This function only works for MAXIMUM_PROCESSORS (which is currently 32),
  1862. // and it assumes at least one bit is set
  1863. //
  1864. #define KeFindFirstSetRightMember(Set) 
  1865.     ((Set & 0xFF) ? KiFindFirstSetRight[Set & 0xFF] : 
  1866.     ((Set & 0xFF00) ? KiFindFirstSetRight[(Set >> 8) & 0xFF] + 8 : 
  1867.     ((Set & 0xFF0000) ? KiFindFirstSetRight[(Set >> 16) & 0xFF] + 16 : 
  1868.                            KiFindFirstSetRight[Set >> 24] + 24 )))
  1869. //
  1870. // TB Flush routines
  1871. //
  1872. #if defined(_M_IX86)
  1873. #if !defined (_X86PAE_)
  1874. #define KI_FILL_PTE(_PointerPte, _PteContents)                          
  1875.         *(_PointerPte) = (_PteContents);
  1876. #define KI_SWAP_PTE(_PointerPte, _PteContents, _OldPte)                 
  1877.         (_OldPte) = *(_PointerPte);                                     
  1878.         *(_PointerPte) = (_PteContents);
  1879. #else
  1880. HARDWARE_PTE
  1881. KeInterlockedSwapPte (
  1882.     IN PHARDWARE_PTE PtePointer,
  1883.     IN PHARDWARE_PTE NewPteContents
  1884.     );
  1885. #define KI_FILL_PTE(_PointerPte, _PteContents) {                            
  1886.         if ((_PointerPte)->Valid == 0) {                                    
  1887.             (_PointerPte)->HighPart = ((_PteContents).HighPart);            
  1888.             (_PointerPte)->LowPart = ((_PteContents).LowPart);              
  1889.         }                                                                   
  1890.         else if ((_PteContents).Valid == 0) {                               
  1891.             (_PointerPte)->LowPart = ((_PteContents).LowPart);              
  1892.             (_PointerPte)->HighPart = ((_PteContents).HighPart);            
  1893.         }                                                                   
  1894.         else {                                                              
  1895.             (VOID) KeInterlockedSwapPte((_PointerPte), &(_PteContents));    
  1896.         }                                                                   
  1897.         }
  1898. #define KI_SWAP_PTE(_PointerPte, _PteContents, _OldPte) {                   
  1899.         (_OldPte) = *(_PointerPte);                                         
  1900.         if ((_PointerPte)->Valid == 0) {                                    
  1901.             (_PointerPte)->HighPart = (_PteContents).HighPart;              
  1902.             (_PointerPte)->LowPart = (_PteContents).LowPart;                
  1903.         }                                                                   
  1904.         else if ((_PteContents).Valid == 0) {                               
  1905.             (_PointerPte)->LowPart = (_PteContents).LowPart;                
  1906.             (_PointerPte)->HighPart = (_PteContents).HighPart;              
  1907.         }                                                                   
  1908.         else {                                                              
  1909.             (_OldPte) = KeInterlockedSwapPte(_PointerPte, &(_PteContents)); 
  1910.         }                                                                   
  1911.         }
  1912. #endif
  1913. #endif
  1914. #if defined(_ALPHA_) && defined(NT_UP) &&           
  1915.     !defined(_NTDRIVER_) && !defined(_NTDDK_) && !defined(_NTIFS_) && !defined(_NTHAL_)
  1916. #define KeFlushEntireTb(Invalid, AllProcessors) __tbia()
  1917. #define KeFlushMultipleTb(Number, Virtual, Invalid, AllProcessors, PtePointer, PteValue) 
  1918. {                                                                                        
  1919.     ULONG _Index_;                                                                       
  1920.                                                                                          
  1921.     if (ARGUMENT_PRESENT(PtePointer)) {                                                  
  1922.         for (_Index_ = 0; _Index_ < (Number); _Index_ += 1) {                            
  1923.             *((PHARDWARE_PTE *)(PtePointer))[_Index_] = (PteValue);                      
  1924.         }                                                                                
  1925.     }                                                                                    
  1926.     KiFlushMultipleTb((Invalid), &(Virtual)[0], (Number));                               
  1927. }
  1928. __inline
  1929. HARDWARE_PTE
  1930. KeFlushSingleTb(
  1931.     IN PVOID Virtual,
  1932.     IN BOOLEAN Invalid,
  1933.     IN BOOLEAN AllProcesors,
  1934.     IN PHARDWARE_PTE PtePointer,
  1935.     IN HARDWARE_PTE PteValue
  1936.     )
  1937. {
  1938.     HARDWARE_PTE OldPte;
  1939.     OldPte = *PtePointer;
  1940.     *PtePointer = PteValue;
  1941.     __tbis(Virtual);
  1942.     return(OldPte);
  1943. }
  1944. #elif defined(_M_IX86) && defined(NT_UP) &&           
  1945.     !defined(_NTDRIVER_) && !defined(_NTDDK_) && !defined(_NTIFS_) && !defined(_NTHAL_)
  1946. #define KeFlushEntireTb(Invalid, AllProcessors) KeFlushCurrentTb()
  1947. __inline
  1948. HARDWARE_PTE
  1949. KeFlushSingleTb(
  1950.     IN PVOID Virtual,
  1951.     IN BOOLEAN Invalid,
  1952.     IN BOOLEAN AllProcesors,
  1953.     IN PHARDWARE_PTE PtePointer,
  1954.     IN HARDWARE_PTE PteValue
  1955.     )
  1956. {
  1957.     HARDWARE_PTE OldPte;
  1958.     KI_SWAP_PTE (PtePointer, PteValue, OldPte);
  1959.     __asm {
  1960.         mov eax, Virtual
  1961.         invlpg [eax]
  1962.     }
  1963.     return(OldPte);
  1964. }
  1965. #define KeFlushMultipleTb(Number, Virtual, Invalid, AllProcessors, PtePointer, PteValue) 
  1966. {                                                                                        
  1967.     ULONG _Index_;                                                                       
  1968.     PVOID _VA_;                                                                          
  1969.                                                                                          
  1970.     for (_Index_ = 0; _Index_ < (Number); _Index_ += 1) {                                
  1971.         if (ARGUMENT_PRESENT(PtePointer)) {                                              
  1972.             KI_FILL_PTE ((((PHARDWARE_PTE *)(PtePointer))[_Index_]), (PteValue));                      
  1973.         }                                                                                
  1974.         _VA_ = (Virtual)[_Index_];                                                       
  1975.         __asm { mov eax, _VA_ }                                                          
  1976.         __asm { invlpg [eax] }                                                           
  1977.     }                                                                                    
  1978. }
  1979. #else
  1980. NTKERNELAPI
  1981. VOID
  1982. KeFlushEntireTb (
  1983.     IN BOOLEAN Invalid,
  1984.     IN BOOLEAN AllProcessors
  1985.     );
  1986. VOID
  1987. KeFlushMultipleTb (
  1988.     IN ULONG Number,
  1989.     IN PVOID *Virtual,
  1990.     IN BOOLEAN Invalid,
  1991.     IN BOOLEAN AllProcesors,
  1992.     IN PHARDWARE_PTE *PtePointer OPTIONAL,
  1993.     IN HARDWARE_PTE PteValue
  1994.     );
  1995. HARDWARE_PTE
  1996. KeFlushSingleTb (
  1997.     IN PVOID Virtual,
  1998.     IN BOOLEAN Invalid,
  1999.     IN BOOLEAN AllProcesors,
  2000.     IN PHARDWARE_PTE PtePointer,
  2001.     IN HARDWARE_PTE PteValue
  2002.     );
  2003. #endif
  2004. #if defined(_ALPHA_) || defined(_IA64_)
  2005. VOID
  2006. KeFlushMultipleTb64 (
  2007.     IN ULONG Number,
  2008.     IN PULONG_PTR Virtual,
  2009.     IN BOOLEAN Invalid,
  2010.     IN BOOLEAN AllProcesors,
  2011.     IN PHARDWARE_PTE *PtePointer OPTIONAL,
  2012.     IN HARDWARE_PTE PteValue
  2013.     );
  2014. HARDWARE_PTE
  2015. KeFlushSingleTb64 (
  2016.     IN ULONG_PTR Virtual,
  2017.     IN BOOLEAN Invalid,
  2018.     IN BOOLEAN AllProcesors,
  2019.     IN PHARDWARE_PTE PtePointer,
  2020.     IN HARDWARE_PTE PteValue
  2021.     );
  2022. #endif
  2023. BOOLEAN
  2024. KeFreezeExecution (
  2025.     IN PKTRAP_FRAME TrapFrame,
  2026.     IN PKEXCEPTION_FRAME ExceptionFrame
  2027.     );
  2028. KCONTINUE_STATUS
  2029. KeSwitchFrozenProcessor (
  2030.     IN ULONG ProcessorNumber
  2031.     );
  2032. VOID
  2033. KeGetNonVolatileContextPointers (
  2034.     IN PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext
  2035.     );
  2036. #define DMA_READ_DCACHE_INVALIDATE 0x1              // nthal
  2037. #define DMA_READ_ICACHE_INVALIDATE 0x2              // nthal
  2038. #define DMA_WRITE_DCACHE_SNOOP 0x4                  // nthal
  2039.                                                     // nthal
  2040. NTKERNELAPI                                         // nthal
  2041. VOID                                                // nthal
  2042. KeSetDmaIoCoherency (                               // nthal
  2043.     IN ULONG Attributes                             // nthal
  2044.     );                                              // nthal
  2045.                                                     // nthal
  2046. #if defined(i386)
  2047. NTKERNELAPI                                         // nthal
  2048. VOID                                                // nthal
  2049. KeSetProfileIrql (                                  // nthal
  2050.     IN KIRQL ProfileIrql                            // nthal
  2051.     );                                              // nthal
  2052.                                                     // nthal
  2053. #endif
  2054. #if defined(_ALPHA_)
  2055. NTKERNELAPI                                         // nthal
  2056. VOID                                                // nthal
  2057. KeSetSynchIrql (                                    // nthal
  2058.     IN KIRQL SynchIrql                              // nthal
  2059.     );                                              // nthal
  2060.                                                     // nthal
  2061. ULONG
  2062. KeReadMbTimeStamp (
  2063.     VOID
  2064.     );
  2065. VOID
  2066. KeSynchronizeMemoryAccess (
  2067.     VOID
  2068.     );
  2069. #endif
  2070. VOID
  2071. KeSetSystemTime (
  2072.     IN PLARGE_INTEGER NewTime,
  2073.     OUT PLARGE_INTEGER OldTime,
  2074.     IN BOOLEAN AdjustInterruptTime,
  2075.     IN PLARGE_INTEGER HalTimeToSet OPTIONAL
  2076.     );
  2077. #define SYSTEM_SERVICE_INDEX 0
  2078. #define WIN32K_SERVICE_INDEX 1
  2079. #define IIS_SERVICE_INDEX 2
  2080. NTKERNELAPI
  2081. BOOLEAN
  2082. KeAddSystemServiceTable(
  2083.     IN PULONG_PTR Base,
  2084.     IN PULONG Count OPTIONAL,
  2085.     IN ULONG Limit,
  2086.     IN PUCHAR Number,
  2087.     IN ULONG Index
  2088.     );
  2089. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  2090. NTKERNELAPI
  2091. ULONGLONG
  2092. KeQueryInterruptTime (
  2093.     VOID
  2094.     );
  2095. NTKERNELAPI
  2096. VOID
  2097. KeQuerySystemTime (
  2098.     OUT PLARGE_INTEGER CurrentTime
  2099.     );
  2100. NTKERNELAPI
  2101. ULONG
  2102. KeQueryTimeIncrement (
  2103.     VOID
  2104.     );
  2105. // end_wdm
  2106. NTKERNELAPI
  2107. KAFFINITY
  2108. KeQueryActiveProcessors (
  2109.     VOID
  2110.     );
  2111. // end_ntddk end_nthal end_ntifs
  2112. // begin_nthal
  2113. NTKERNELAPI
  2114. VOID
  2115. KeSetTimeIncrement (
  2116.     IN ULONG MaximumIncrement,
  2117.     IN ULONG MimimumIncrement
  2118.     );
  2119. // end_nthal
  2120. VOID
  2121. KeThawExecution (
  2122.     IN BOOLEAN Enable
  2123.     );
  2124. // begin_nthal
  2125. //
  2126. // Define the firmware routine types
  2127. //
  2128. typedef enum _FIRMWARE_REENTRY {
  2129.     HalHaltRoutine,
  2130.     HalPowerDownRoutine,
  2131.     HalRestartRoutine,
  2132.     HalRebootRoutine,
  2133.     HalInteractiveModeRoutine,
  2134.     HalMaximumRoutine
  2135. } FIRMWARE_REENTRY, *PFIRMWARE_REENTRY;
  2136. // end_nthal
  2137. VOID
  2138. KeReturnToFirmware (
  2139.     IN FIRMWARE_REENTRY Routine
  2140.     );
  2141. VOID
  2142. KeStartAllProcessors (
  2143.     VOID
  2144.     );
  2145. //
  2146. // Balance set manager thread startup function.
  2147. //
  2148. VOID
  2149. KeBalanceSetManager (
  2150.     IN PVOID Context
  2151.     );
  2152. VOID
  2153. KeSwapProcessOrStack (
  2154.     IN PVOID Context
  2155.     );
  2156. //
  2157. // User mode callback.
  2158. //
  2159. NTKERNELAPI
  2160. NTSTATUS
  2161. KeUserModeCallback (
  2162.     IN ULONG ApiNumber,
  2163.     IN PVOID InputBuffer,
  2164.     IN ULONG InputLength,
  2165.     OUT PVOID *OutputBuffer,
  2166.     OUT PULONG OutputLength
  2167.     );
  2168. #if defined(_IA64_)
  2169. PVOID
  2170. KeSwitchKernelStack (
  2171.     IN PVOID StackBase,
  2172.     IN PVOID StackLimit,
  2173.     IN PVOID BStoreLimit
  2174.     );
  2175. #else
  2176. PVOID
  2177. KeSwitchKernelStack (
  2178.     IN PVOID StackBase,
  2179.     IN PVOID StackLimit
  2180.     );
  2181. #endif // defined(_IA64_)
  2182. NTSTATUS
  2183. KeRaiseUserException(
  2184.     IN NTSTATUS ExceptionCode
  2185.     );
  2186. // begin_nthal
  2187. //
  2188. // Find ARC configuration information function.
  2189. //
  2190. NTKERNELAPI
  2191. PCONFIGURATION_COMPONENT_DATA
  2192. KeFindConfigurationEntry (
  2193.     IN PCONFIGURATION_COMPONENT_DATA Child,
  2194.     IN CONFIGURATION_CLASS Class,
  2195.     IN CONFIGURATION_TYPE Type,
  2196.     IN PULONG Key OPTIONAL
  2197.     );
  2198. NTKERNELAPI
  2199. PCONFIGURATION_COMPONENT_DATA
  2200. KeFindConfigurationNextEntry (
  2201.     IN PCONFIGURATION_COMPONENT_DATA Child,
  2202.     IN CONFIGURATION_CLASS Class,
  2203.     IN CONFIGURATION_TYPE Type,
  2204.     IN PULONG Key OPTIONAL,
  2205.     IN PCONFIGURATION_COMPONENT_DATA *Resume
  2206.     );
  2207. // end_nthal
  2208. //
  2209. // begin_ntddk begin_nthal begin_ntifs
  2210. //
  2211. // Context swap notify routine.
  2212. //
  2213. typedef
  2214. VOID
  2215. (FASTCALL *PSWAP_CONTEXT_NOTIFY_ROUTINE)(
  2216.     IN HANDLE OldThreadId,
  2217.     IN HANDLE NewThreadId
  2218.     );
  2219. NTKERNELAPI
  2220. VOID
  2221. FASTCALL
  2222. KeSetSwapContextNotifyRoutine(
  2223.     IN PSWAP_CONTEXT_NOTIFY_ROUTINE NotifyRoutine
  2224.     );
  2225. //
  2226. // Thread select notify routine.
  2227. //
  2228. typedef
  2229. LOGICAL
  2230. (FASTCALL *PTHREAD_SELECT_NOTIFY_ROUTINE)(
  2231.     IN HANDLE ThreadId
  2232.     );
  2233. NTKERNELAPI
  2234. VOID
  2235. FASTCALL
  2236. KeSetThreadSelectNotifyRoutine(
  2237.     IN PTHREAD_SELECT_NOTIFY_ROUTINE NotifyRoutine
  2238.     );
  2239. //
  2240. // Time update notify routine.
  2241. //
  2242. typedef
  2243. VOID
  2244. (FASTCALL *PTIME_UPDATE_NOTIFY_ROUTINE)(
  2245.     IN HANDLE ThreadId,
  2246.     IN KPROCESSOR_MODE Mode
  2247.     );
  2248. NTKERNELAPI
  2249. VOID
  2250. FASTCALL
  2251. KeSetTimeUpdateNotifyRoutine(
  2252.     IN PTIME_UPDATE_NOTIFY_ROUTINE NotifyRoutine
  2253.     );
  2254. // end_ntddk end_nthal end_ntifs
  2255. //
  2256. // External references to public kernel data structures
  2257. //
  2258. extern KAFFINITY KeActiveProcessors;
  2259. extern LARGE_INTEGER KeBootTime;
  2260. extern ULONGLONG KeBootTimeBias;
  2261. extern ULONGLONG KeInterruptTimeBias;
  2262. extern LIST_ENTRY KeBugCheckCallbackListHead;
  2263. extern KSPIN_LOCK KeBugCheckCallbackLock;
  2264. extern PGDI_BATCHFLUSH_ROUTINE KeGdiFlushUserBatch;
  2265. extern PLOADER_PARAMETER_BLOCK KeLoaderBlock;
  2266. extern ULONG KeMaximumIncrement;
  2267. extern ULONG KeMinimumIncrement;
  2268. extern CCHAR KeNumberProcessors;                    // nthal
  2269. extern USHORT KeProcessorArchitecture;
  2270. extern USHORT KeProcessorLevel;
  2271. extern USHORT KeProcessorRevision;
  2272. extern ULONG KeFeatureBits;
  2273. extern CCHAR KiFindFirstSetRight[256];
  2274. extern PKPRCB KiProcessorBlock[];
  2275. extern ULONG KiStackProtectTime;
  2276. extern KTHREAD_SWITCH_COUNTERS KeThreadSwitchCounters;
  2277. #if !defined(NT_UP)
  2278. extern ULONG KeRegisteredProcessors;
  2279. extern ULONG KeLicensedProcessors;
  2280. #endif
  2281. extern PULONG KeServiceCountTable;
  2282. extern KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[NUMBER_SERVICE_TABLES];
  2283. extern KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[NUMBER_SERVICE_TABLES];
  2284. extern volatile KSYSTEM_TIME KeTickCount;           // ntddk wdm nthal ntifs
  2285. // begin_nthal
  2286. #if defined(_ALPHA_)
  2287. extern ULONG KeNumberProcessIds;
  2288. extern ULONG KeNumberTbEntries;
  2289. #endif
  2290. extern PVOID KeUserApcDispatcher;
  2291. extern PVOID KeUserCallbackDispatcher;
  2292. extern PVOID KeUserExceptionDispatcher;
  2293. extern PVOID KeRaiseUserExceptionDispatcher;
  2294. extern ULONG KeTimeAdjustment;
  2295. extern ULONG KeTimeIncrement;
  2296. extern BOOLEAN KeTimeSynchronization;
  2297. // end_nthal
  2298. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  2299. typedef enum _MEMORY_CACHING_TYPE_ORIG {
  2300.     MmFrameBufferCached = 2
  2301. } MEMORY_CACHING_TYPE_ORIG;
  2302. typedef enum _MEMORY_CACHING_TYPE {
  2303.     MmNonCached = FALSE,
  2304.     MmCached = TRUE,
  2305.     MmWriteCombined = MmFrameBufferCached,
  2306.     MmHardwareCoherentCached,
  2307.     MmNonCachedUnordered,       // IA64
  2308.     MmUSWCCached,
  2309.     MmMaximumCacheType
  2310. } MEMORY_CACHING_TYPE;
  2311. // end_ntddk end_wdm end_nthal end_ntifs
  2312. #if defined(_X86_)
  2313. //
  2314. // Routine for setting memory type for physical address ranges
  2315. //
  2316. NTSTATUS
  2317. KeSetPhysicalCacheTypeRange (
  2318.     IN PHYSICAL_ADDRESS PhysicalAddress,
  2319.     IN ULONG NumberOfBytes,
  2320.     IN MEMORY_CACHING_TYPE CacheType
  2321.     );
  2322. #endif
  2323. #if defined(_X86_)
  2324. //
  2325. // Routine for zeroing a physical page.   These are defined
  2326. // as calls through a function pointer which is set to point
  2327. // at the optimal routine for this processor implementation.
  2328. //
  2329. typedef
  2330. VOID
  2331. (FASTCALL *KE_ZERO_PAGE_ROUTINE)(
  2332.     IN PVOID PageBase
  2333.     );
  2334. extern KE_ZERO_PAGE_ROUTINE KeZeroPage;
  2335. extern KE_ZERO_PAGE_ROUTINE KeZeroPageFromIdleThread;
  2336. #endif
  2337. #if defined(_IA64_)
  2338. VOID
  2339. KeEnableSessionSharing(
  2340.     PREGION_MAP_INFO SessionMapInfo
  2341.     );
  2342. VOID 
  2343. KeDetachSessionSpace(
  2344.     VOID 
  2345.     );
  2346. VOID
  2347. KeAddSessionSpace(
  2348.     PKPROCESS Process,
  2349.     PREGION_MAP_INFO SessionMapInfo
  2350.     );
  2351. VOID
  2352. KeAttachSessionSpace(
  2353.     PREGION_MAP_INFO SessionMapInfo
  2354.     );
  2355. VOID
  2356. KeDisableSessionSharing(
  2357.     PREGION_MAP_INFO SessionMapInfo
  2358.     );
  2359. #endif
  2360. #endif // _KE_