Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3781 lines
74 KiB

  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. #define LOCK_OWNERSHIP_QUANTUM (WAIT_QUANTUM_DECREMENT * 4)
  19. //
  20. // Define the default ready skip and thread quantum values.
  21. //
  22. #define READY_SKIP_QUANTUM 2
  23. #define THREAD_QUANTUM (READY_SKIP_QUANTUM * CLOCK_QUANTUM_DECREMENT)
  24. //
  25. // Define the round trip decrement count.
  26. //
  27. #define ROUND_TRIP_DECREMENT_COUNT 16
  28. //
  29. // Performance data collection enable definitions.
  30. //
  31. // A definition turns on the respective data collection.
  32. //
  33. //#define _COLLECT_FLUSH_SINGLE_CALLDATA_ 1
  34. //#define _COLLECT_SET_EVENT_CALLDATA_ 1
  35. //#define _COLLECT_WAIT_SINGLE_CALLDATA_ 1
  36. //
  37. // Define thread switch performance data structure.
  38. //
  39. typedef struct _KTHREAD_SWITCH_COUNTERS {
  40. ULONG FindAny;
  41. ULONG FindIdeal;
  42. ULONG FindLast;
  43. ULONG IdleAny;
  44. ULONG IdleCurrent;
  45. ULONG IdleIdeal;
  46. ULONG IdleLast;
  47. ULONG PreemptAny;
  48. ULONG PreemptCurrent;
  49. ULONG PreemptLast;
  50. ULONG SwitchToIdle;
  51. } KTHREAD_SWITCH_COUNTERS, *PKTHREAD_SWITCH_COUNTERS;
  52. //
  53. // Public (external) constant definitions.
  54. //
  55. #define BASE_PRIORITY_THRESHOLD NORMAL_BASE_PRIORITY // fast path base threshold
  56. // begin_ntddk begin_wdm begin_ntosp
  57. #define THREAD_WAIT_OBJECTS 3 // Builtin usable wait blocks
  58. // end_ntddk end_wdm end_ntosp
  59. #define EVENT_WAIT_BLOCK 2 // Builtin event pair wait block
  60. #define SEMAPHORE_WAIT_BLOCK 2 // Builtin semaphore wait block
  61. #define TIMER_WAIT_BLOCK 3 // Builtin timer wait block
  62. #if (EVENT_WAIT_BLOCK != SEMAPHORE_WAIT_BLOCK)
  63. #error "wait event and wait semaphore must use same wait block"
  64. #endif
  65. //
  66. // Define timer table size.
  67. //
  68. #define TIMER_TABLE_SIZE 256
  69. //
  70. // Get APC environment of current thread.
  71. //
  72. #define KeGetCurrentApcEnvironment() \
  73. KeGetCurrentThread()->ApcStateIndex
  74. //
  75. // begin_ntddk begin_nthal begin_ntosp begin_ntifs
  76. //
  77. #if defined(_X86_)
  78. #define PAUSE_PROCESSOR _asm { rep nop }
  79. #else
  80. #define PAUSE_PROCESSOR
  81. #endif
  82. // end_ntddk end_nthal end_ntosp end_ntifs
  83. // begin_nthal begin_ntosp
  84. //
  85. // Define macro to generate an affinity mask.
  86. //
  87. #if defined(_NTHAL_) || defined(_NTOSP_)
  88. #define AFFINITY_MASK(n) ((ULONG_PTR)1 << (n))
  89. #else
  90. #if !defined(_WIN64)
  91. #define KiAffinityArray KiMask32Array
  92. #endif
  93. extern const ULONG_PTR KiAffinityArray[];
  94. #define AFFINITY_MASK(n) (KiAffinityArray[n])
  95. #endif
  96. // end_nthal end_ntosp
  97. //
  98. // Define macro to generate priority mask.
  99. //
  100. extern const ULONG KiMask32Array[];
  101. #define PRIORITY_MASK(n) (KiMask32Array[n])
  102. //
  103. // Define query system time macro.
  104. //
  105. // The following AMD64 code reads an unaligned quadword value. The quadword
  106. // value, however, is guaranteed to be within a cache line, and therefore,
  107. // the value will be read atomically.
  108. //
  109. #if defined(_AMD64_)
  110. #define KiQuerySystemTime(CurrentTime) \
  111. (CurrentTime)->QuadPart = *((LONG64 volatile *)(&SharedUserData->SystemTime))
  112. #else
  113. #define KiQuerySystemTime(CurrentTime) \
  114. while (TRUE) { \
  115. (CurrentTime)->HighPart = SharedUserData->SystemTime.High1Time; \
  116. (CurrentTime)->LowPart = SharedUserData->SystemTime.LowPart; \
  117. if ((CurrentTime)->HighPart == SharedUserData->SystemTime.High2Time) break; \
  118. PAUSE_PROCESSOR \
  119. }
  120. #endif
  121. #if defined(_AMD64_)
  122. #define KiQueryLowTickCount() SharedUserData->TickCount.LowPart
  123. #else
  124. #define KiQueryLowTickCount() KeTickCount.LowPart
  125. #endif
  126. //
  127. // Enumerated kernel types
  128. //
  129. // Kernel object types.
  130. //
  131. // N.B. There are really two types of event objects; NotificationEvent and
  132. // SynchronizationEvent. The type value for a notification event is 0,
  133. // and that for a synchronization event 1.
  134. //
  135. // N.B. There are two types of new timer objects; NotificationTimer and
  136. // SynchronizationTimer. The type value for a notification timer is
  137. // 8, and that for a synchronization timer is 9. These values are
  138. // very carefully chosen so that the dispatcher object type AND'ed
  139. // with 0x7 yields 0 or 1 for event objects and the timer objects.
  140. //
  141. #define DISPATCHER_OBJECT_TYPE_MASK 0x7
  142. typedef enum _KOBJECTS {
  143. EventNotificationObject = 0,
  144. EventSynchronizationObject = 1,
  145. MutantObject = 2,
  146. ProcessObject = 3,
  147. QueueObject = 4,
  148. SemaphoreObject = 5,
  149. ThreadObject = 6,
  150. Spare1Object = 7,
  151. TimerNotificationObject = 8,
  152. TimerSynchronizationObject = 9,
  153. Spare2Object = 10,
  154. Spare3Object = 11,
  155. Spare4Object = 12,
  156. Spare5Object = 13,
  157. Spare6Object = 14,
  158. Spare7Object = 15,
  159. Spare8Object = 16,
  160. Spare9Object = 17,
  161. ApcObject,
  162. DpcObject,
  163. DeviceQueueObject,
  164. EventPairObject,
  165. InterruptObject,
  166. ProfileObject,
  167. ThreadedDpcObject,
  168. MaximumKernelObject
  169. } KOBJECTS;
  170. #define KOBJECT_LOCK_BIT 0x80
  171. C_ASSERT((MaximumKernelObject & KOBJECT_LOCK_BIT) == 0);
  172. //
  173. // APC environments.
  174. //
  175. // begin_ntosp
  176. typedef enum _KAPC_ENVIRONMENT {
  177. OriginalApcEnvironment,
  178. AttachedApcEnvironment,
  179. CurrentApcEnvironment,
  180. InsertApcEnvironment
  181. } KAPC_ENVIRONMENT;
  182. // begin_ntddk begin_wdm begin_nthal begin_ntminiport begin_ntifs begin_ntndis
  183. //
  184. // Interrupt modes.
  185. //
  186. typedef enum _KINTERRUPT_MODE {
  187. LevelSensitive,
  188. Latched
  189. } KINTERRUPT_MODE;
  190. // end_ntddk end_wdm end_nthal end_ntminiport end_ntifs end_ntndis end_ntosp
  191. //
  192. // Process states.
  193. //
  194. typedef enum _KPROCESS_STATE {
  195. ProcessInMemory,
  196. ProcessOutOfMemory,
  197. ProcessInTransition,
  198. ProcessOutTransition,
  199. ProcessInSwap,
  200. ProcessOutSwap
  201. } KPROCESS_STATE;
  202. //
  203. // Thread scheduling states.
  204. //
  205. typedef enum _KTHREAD_STATE {
  206. Initialized,
  207. Ready,
  208. Running,
  209. Standby,
  210. Terminated,
  211. Waiting,
  212. Transition,
  213. DeferredReady
  214. } KTHREAD_STATE;
  215. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  216. //
  217. // Wait reasons
  218. //
  219. typedef enum _KWAIT_REASON {
  220. Executive,
  221. FreePage,
  222. PageIn,
  223. PoolAllocation,
  224. DelayExecution,
  225. Suspended,
  226. UserRequest,
  227. WrExecutive,
  228. WrFreePage,
  229. WrPageIn,
  230. WrPoolAllocation,
  231. WrDelayExecution,
  232. WrSuspended,
  233. WrUserRequest,
  234. WrEventPair,
  235. WrQueue,
  236. WrLpcReceive,
  237. WrLpcReply,
  238. WrVirtualMemory,
  239. WrPageOut,
  240. WrRendezvous,
  241. Spare2,
  242. Spare3,
  243. Spare4,
  244. Spare5,
  245. Spare6,
  246. WrKernel,
  247. WrResource,
  248. WrPushLock,
  249. WrMutex,
  250. WrQuantumEnd,
  251. WrDispatchInt,
  252. WrPreempted,
  253. WrYieldExecution,
  254. MaximumWaitReason
  255. } KWAIT_REASON;
  256. // end_ntddk end_wdm end_nthal
  257. //
  258. // Miscellaneous type definitions
  259. //
  260. // APC state
  261. //
  262. typedef struct _KAPC_STATE {
  263. LIST_ENTRY ApcListHead[MaximumMode];
  264. struct _KPROCESS *Process;
  265. BOOLEAN KernelApcInProgress;
  266. BOOLEAN KernelApcPending;
  267. BOOLEAN UserApcPending;
  268. } KAPC_STATE, *PKAPC_STATE, *RESTRICTED_POINTER PRKAPC_STATE;
  269. // end_ntifs end_ntosp
  270. //
  271. // Page frame
  272. //
  273. typedef ULONG KPAGE_FRAME;
  274. //
  275. // Wait block
  276. //
  277. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  278. typedef struct _KWAIT_BLOCK {
  279. LIST_ENTRY WaitListEntry;
  280. struct _KTHREAD *RESTRICTED_POINTER Thread;
  281. PVOID Object;
  282. struct _KWAIT_BLOCK *RESTRICTED_POINTER NextWaitBlock;
  283. USHORT WaitKey;
  284. USHORT WaitType;
  285. } KWAIT_BLOCK, *PKWAIT_BLOCK, *RESTRICTED_POINTER PRKWAIT_BLOCK;
  286. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  287. //
  288. // System service table descriptor.
  289. //
  290. // N.B. A system service number has a 12-bit service table offset and a
  291. // 3-bit service table number.
  292. //
  293. // N.B. Descriptor table entries must be a power of 2 in size. Currently
  294. // this is 16 bytes on a 32-bit system and 32 bytes on a 64-bit
  295. // system.
  296. //
  297. #define NUMBER_SERVICE_TABLES 4
  298. #define SERVICE_NUMBER_MASK ((1 << 12) - 1)
  299. #if defined(_WIN64)
  300. #define SERVICE_TABLE_SHIFT (12 - 5)
  301. #define SERVICE_TABLE_MASK (((1 << 2) - 1) << 5)
  302. #define SERVICE_TABLE_TEST (WIN32K_SERVICE_INDEX << 5)
  303. #else
  304. #define SERVICE_TABLE_SHIFT (12 - 4)
  305. #define SERVICE_TABLE_MASK (((1 << 2) - 1) << 4)
  306. #define SERVICE_TABLE_TEST (WIN32K_SERVICE_INDEX << 4)
  307. #endif
  308. typedef struct _KSERVICE_TABLE_DESCRIPTOR {
  309. PULONG_PTR Base;
  310. PULONG Count;
  311. ULONG Limit;
  312. #if defined(_IA64_)
  313. LONG TableBaseGpOffset;
  314. #endif
  315. PUCHAR Number;
  316. } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
  317. //
  318. // Procedure type definitions
  319. //
  320. // Debug routine
  321. //
  322. typedef
  323. BOOLEAN
  324. (*PKDEBUG_ROUTINE) (
  325. IN PKTRAP_FRAME TrapFrame,
  326. IN PKEXCEPTION_FRAME ExceptionFrame,
  327. IN PEXCEPTION_RECORD ExceptionRecord,
  328. IN PCONTEXT ContextRecord,
  329. IN KPROCESSOR_MODE PreviousMode,
  330. IN BOOLEAN SecondChance
  331. );
  332. BOOLEAN
  333. KdpStub (
  334. IN PKTRAP_FRAME TrapFrame,
  335. IN PKEXCEPTION_FRAME ExceptionFrame,
  336. IN PEXCEPTION_RECORD ExceptionRecord,
  337. IN PCONTEXT ContextRecord,
  338. IN KPROCESSOR_MODE PreviousMode,
  339. IN BOOLEAN SecondChance
  340. );
  341. typedef
  342. BOOLEAN
  343. (*PKDEBUG_SWITCH_ROUTINE) (
  344. IN PEXCEPTION_RECORD ExceptionRecord,
  345. IN PCONTEXT ContextRecord,
  346. IN BOOLEAN SecondChance
  347. );
  348. typedef enum {
  349. ContinueError = FALSE,
  350. ContinueSuccess = TRUE,
  351. ContinueProcessorReselected,
  352. ContinueNextProcessor
  353. } KCONTINUE_STATUS;
  354. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  355. //
  356. // Thread start function
  357. //
  358. typedef
  359. VOID
  360. (*PKSTART_ROUTINE) (
  361. IN PVOID StartContext
  362. );
  363. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  364. //
  365. // Thread system function
  366. //
  367. typedef
  368. VOID
  369. (*PKSYSTEM_ROUTINE) (
  370. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  371. IN PVOID StartContext OPTIONAL
  372. );
  373. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  374. //
  375. // Kernel object structure definitions
  376. //
  377. //
  378. // Device Queue object and entry
  379. //
  380. typedef struct _KDEVICE_QUEUE {
  381. CSHORT Type;
  382. CSHORT Size;
  383. LIST_ENTRY DeviceListHead;
  384. KSPIN_LOCK Lock;
  385. BOOLEAN Busy;
  386. } KDEVICE_QUEUE, *PKDEVICE_QUEUE, *RESTRICTED_POINTER PRKDEVICE_QUEUE;
  387. typedef struct _KDEVICE_QUEUE_ENTRY {
  388. LIST_ENTRY DeviceListEntry;
  389. ULONG SortKey;
  390. BOOLEAN Inserted;
  391. } KDEVICE_QUEUE_ENTRY, *PKDEVICE_QUEUE_ENTRY, *RESTRICTED_POINTER PRKDEVICE_QUEUE_ENTRY;
  392. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  393. //
  394. // Event pair object
  395. //
  396. typedef struct _KEVENT_PAIR {
  397. CSHORT Type;
  398. CSHORT Size;
  399. KEVENT EventLow;
  400. KEVENT EventHigh;
  401. } KEVENT_PAIR, *PKEVENT_PAIR, *RESTRICTED_POINTER PRKEVENT_PAIR;
  402. // begin_nthal begin_ntddk begin_wdm begin_ntifs begin_ntosp
  403. //
  404. // Define the interrupt service function type and the empty struct
  405. // type.
  406. //
  407. // end_ntddk end_wdm end_ntifs end_ntosp
  408. struct _KINTERRUPT;
  409. // begin_ntddk begin_wdm begin_ntifs begin_ntosp
  410. typedef
  411. BOOLEAN
  412. (*PKSERVICE_ROUTINE) (
  413. IN struct _KINTERRUPT *Interrupt,
  414. IN PVOID ServiceContext
  415. );
  416. // end_ntddk end_wdm end_ntifs end_ntosp
  417. //
  418. // Interrupt object
  419. //
  420. // N.B. The layout of this structure cannot change. It is exported to HALs
  421. // to short circuit interrupt dispatch.
  422. //
  423. typedef struct _KINTERRUPT {
  424. CSHORT Type;
  425. CSHORT Size;
  426. LIST_ENTRY InterruptListEntry;
  427. PKSERVICE_ROUTINE ServiceRoutine;
  428. PVOID ServiceContext;
  429. KSPIN_LOCK SpinLock;
  430. ULONG TickCount;
  431. PKSPIN_LOCK ActualLock;
  432. PKINTERRUPT_ROUTINE DispatchAddress;
  433. ULONG Vector;
  434. KIRQL Irql;
  435. KIRQL SynchronizeIrql;
  436. BOOLEAN FloatingSave;
  437. BOOLEAN Connected;
  438. CCHAR Number;
  439. BOOLEAN ShareVector;
  440. KINTERRUPT_MODE Mode;
  441. ULONG ServiceCount;
  442. ULONG DispatchCount;
  443. #if defined(_AMD64_)
  444. PKTRAP_FRAME TrapFrame;
  445. #endif
  446. ULONG DispatchCode[DISPATCH_LENGTH];
  447. } KINTERRUPT;
  448. typedef struct _KINTERRUPT *PKINTERRUPT, *RESTRICTED_POINTER PRKINTERRUPT; // ntndis ntosp
  449. // begin_ntifs begin_ntddk begin_wdm begin_ntosp
  450. //
  451. // Mutant object
  452. //
  453. typedef struct _KMUTANT {
  454. DISPATCHER_HEADER Header;
  455. LIST_ENTRY MutantListEntry;
  456. struct _KTHREAD *RESTRICTED_POINTER OwnerThread;
  457. BOOLEAN Abandoned;
  458. UCHAR ApcDisable;
  459. } KMUTANT, *PKMUTANT, *RESTRICTED_POINTER PRKMUTANT, KMUTEX, *PKMUTEX, *RESTRICTED_POINTER PRKMUTEX;
  460. // end_ntddk end_wdm end_ntosp
  461. //
  462. // Queue object
  463. //
  464. #define ASSERT_QUEUE(Q) ASSERT(((Q)->Header.Type & ~KOBJECT_LOCK_BIT) == QueueObject);
  465. // begin_ntosp
  466. typedef struct _KQUEUE {
  467. DISPATCHER_HEADER Header;
  468. LIST_ENTRY EntryListHead;
  469. ULONG CurrentCount;
  470. ULONG MaximumCount;
  471. LIST_ENTRY ThreadListHead;
  472. } KQUEUE, *PKQUEUE, *RESTRICTED_POINTER PRKQUEUE;
  473. // end_ntosp
  474. // begin_ntddk begin_wdm begin_ntosp
  475. //
  476. //
  477. // Semaphore object
  478. //
  479. typedef struct _KSEMAPHORE {
  480. DISPATCHER_HEADER Header;
  481. LONG Limit;
  482. } KSEMAPHORE, *PKSEMAPHORE, *RESTRICTED_POINTER PRKSEMAPHORE;
  483. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  484. #if !defined(_X86_)
  485. //
  486. // ALIGNMENT_EXCEPTION_TABLE is used to track alignment exceptions in
  487. // processes that are attached to a debugger.
  488. //
  489. #define ALIGNMENT_RECORDS_PER_TABLE 64
  490. #define MAXIMUM_ALIGNMENT_TABLES 16
  491. typedef struct _ALIGNMENT_EXCEPTION_RECORD {
  492. PVOID ProgramCounter;
  493. ULONG Count;
  494. BOOLEAN AutoFixup;
  495. } ALIGNMENT_EXCEPTION_RECORD, *PALIGNMENT_EXCEPTION_RECORD;
  496. typedef struct _ALIGNMENT_EXCEPTION_TABLE *PALIGNMENT_EXCEPTION_TABLE;
  497. typedef struct _ALIGNMENT_EXCEPTION_TABLE {
  498. PALIGNMENT_EXCEPTION_TABLE Next;
  499. ALIGNMENT_EXCEPTION_RECORD RecordArray[ ALIGNMENT_RECORDS_PER_TABLE ];
  500. } ALIGNMENT_EXCEPTION_TABLE;
  501. #endif
  502. // begin_nthal
  503. //
  504. // Define the maximum number of nodes supported.
  505. //
  506. // N.B. Node number must fit in the page color field of a PFN entry.
  507. //
  508. #define MAXIMUM_CCNUMA_NODES 16
  509. // end_nthal
  510. //
  511. // Define node structure for multinode systems.
  512. //
  513. #define KeGetCurrentNode() (KeGetCurrentPrcb()->ParentNode)
  514. typedef struct _KNODE {
  515. KAFFINITY ProcessorMask; // Physical & Logical CPUs
  516. ULONG Color; // Public 0 based node color
  517. ULONG MmShiftedColor; // MM private shifted color
  518. PFN_NUMBER FreeCount[2]; // # colored pages free
  519. SLIST_HEADER DeadStackList; // MM per node dead stack list
  520. SLIST_HEADER PfnDereferenceSListHead; // MM per node deferred PFN freelist
  521. PSLIST_ENTRY PfnDeferredList; // MM per node deferred PFN list
  522. UCHAR Seed; // Ideal Processor Seed
  523. UCHAR NodeNumber;
  524. struct _flags {
  525. BOOLEAN Removable; // Node can be removed
  526. } Flags;
  527. } KNODE, *PKNODE;
  528. extern PKNODE KeNodeBlock[];
  529. //
  530. // Process object structure definition
  531. //
  532. typedef struct _KPROCESS {
  533. //
  534. // The dispatch header and profile listhead are fairly infrequently
  535. // referenced.
  536. //
  537. DISPATCHER_HEADER Header;
  538. LIST_ENTRY ProfileListHead;
  539. //
  540. // The following fields are referenced during context switches.
  541. //
  542. ULONG_PTR DirectoryTableBase[2];
  543. #if defined(_X86_)
  544. KGDTENTRY LdtDescriptor;
  545. KIDTENTRY Int21Descriptor;
  546. USHORT IopmOffset;
  547. UCHAR Iopl;
  548. BOOLEAN Unused;
  549. #endif
  550. #if defined(_AMD64_)
  551. USHORT IopmOffset;
  552. #endif
  553. #if defined(_IA64_)
  554. REGION_MAP_INFO ProcessRegion;
  555. PREGION_MAP_INFO SessionMapInfo;
  556. ULONG_PTR SessionParentBase;
  557. #endif // _IA64_
  558. volatile KAFFINITY ActiveProcessors;
  559. //
  560. // The following fields are referenced during clock interrupts.
  561. //
  562. ULONG KernelTime;
  563. ULONG UserTime;
  564. //
  565. // The following fields are referenced infrequently.
  566. //
  567. LIST_ENTRY ReadyListHead;
  568. SINGLE_LIST_ENTRY SwapListEntry;
  569. #if defined(_X86_)
  570. PVOID VdmTrapcHandler;
  571. #else
  572. PVOID Reserved1;
  573. #endif
  574. LIST_ENTRY ThreadListHead;
  575. KSPIN_LOCK ProcessLock;
  576. KAFFINITY Affinity;
  577. USHORT StackCount;
  578. SCHAR BasePriority;
  579. SCHAR ThreadQuantum;
  580. BOOLEAN AutoAlignment;
  581. UCHAR State;
  582. UCHAR ThreadSeed;
  583. BOOLEAN DisableBoost;
  584. UCHAR PowerState;
  585. BOOLEAN DisableQuantum;
  586. UCHAR IdealNode;
  587. UCHAR Spare;
  588. #if !defined(_X86_)
  589. PALIGNMENT_EXCEPTION_TABLE AlignmentExceptionTable;
  590. #endif
  591. } KPROCESS, *PKPROCESS, *RESTRICTED_POINTER PRKPROCESS;
  592. //
  593. // Thread object
  594. //
  595. typedef enum _ADJUST_REASON {
  596. AdjustNone = 0,
  597. AdjustUnwait = 1,
  598. AdjustBoost = 2
  599. } ADJUST_REASON;
  600. typedef struct _KTHREAD {
  601. //
  602. // The dispatcher header and mutant listhead are fairly infrequently
  603. // referenced.
  604. //
  605. DISPATCHER_HEADER Header;
  606. LIST_ENTRY MutantListHead;
  607. //
  608. // The following fields are referenced during context switches and wait
  609. // operatings. They have been carefully laid out to get the best cache
  610. // hit ratios.
  611. //
  612. PVOID InitialStack;
  613. PVOID StackLimit;
  614. PVOID KernelStack;
  615. #if defined(_IA64_)
  616. PVOID InitialBStore;
  617. PVOID BStoreLimit;
  618. CCHAR Number; // must match the size of Number in KPCR
  619. // set to the processor number last time
  620. // this thread uses the high fp register set
  621. // see KiRestoreHighFPVolatile in trap.s for details
  622. BOOLEAN Spare3;
  623. PVOID KernelBStore;
  624. #endif
  625. KSPIN_LOCK ThreadLock;
  626. ULONG ContextSwitches;
  627. volatile UCHAR State;
  628. UCHAR NpxState;
  629. KIRQL WaitIrql;
  630. KPROCESSOR_MODE WaitMode;
  631. PVOID Teb;
  632. KAPC_STATE ApcState;
  633. KSPIN_LOCK ApcQueueLock;
  634. LONG_PTR WaitStatus;
  635. PRKWAIT_BLOCK WaitBlockList;
  636. BOOLEAN Alertable;
  637. BOOLEAN WaitNext;
  638. UCHAR WaitReason;
  639. SCHAR Priority;
  640. UCHAR EnableStackSwap;
  641. volatile UCHAR SwapBusy;
  642. BOOLEAN Alerted[MaximumMode];
  643. union {
  644. LIST_ENTRY WaitListEntry;
  645. SINGLE_LIST_ENTRY SwapListEntry;
  646. };
  647. PRKQUEUE Queue;
  648. ULONG WaitTime;
  649. union {
  650. struct {
  651. SHORT KernelApcDisable;
  652. SHORT SpecialApcDisable;
  653. };
  654. ULONG CombinedApcDisable;
  655. };
  656. KTIMER Timer;
  657. KWAIT_BLOCK WaitBlock[THREAD_WAIT_OBJECTS + 1];
  658. LIST_ENTRY QueueListEntry;
  659. //
  660. // The following fields are referenced during ready thread and wait
  661. // completion.
  662. //
  663. UCHAR ApcStateIndex;
  664. BOOLEAN ApcQueueable;
  665. BOOLEAN Preempted;
  666. BOOLEAN ProcessReadyQueue;
  667. BOOLEAN KernelStackResident;
  668. CHAR Saturation;
  669. UCHAR IdealProcessor;
  670. volatile UCHAR NextProcessor;
  671. SCHAR BasePriority;
  672. UCHAR Spare4;
  673. SCHAR PriorityDecrement;
  674. SCHAR Quantum;
  675. BOOLEAN SystemAffinityActive;
  676. CCHAR PreviousMode;
  677. UCHAR ResourceIndex;
  678. UCHAR DisableBoost;
  679. KAFFINITY UserAffinity;
  680. PKPROCESS Process;
  681. KAFFINITY Affinity;
  682. //
  683. // The below fields are infrequently referenced.
  684. //
  685. PVOID ServiceTable;
  686. PKAPC_STATE ApcStatePointer[2];
  687. KAPC_STATE SavedApcState;
  688. PVOID CallbackStack;
  689. #if defined(_IA64_)
  690. PVOID CallbackBStore;
  691. #endif
  692. PVOID Win32Thread;
  693. PKTRAP_FRAME TrapFrame;
  694. ULONG KernelTime;
  695. ULONG UserTime;
  696. PVOID StackBase;
  697. KAPC SuspendApc;
  698. KSEMAPHORE SuspendSemaphore;
  699. PVOID TlsArray;
  700. PVOID LegoData;
  701. LIST_ENTRY ThreadListEntry;
  702. UCHAR LargeStack;
  703. UCHAR PowerState;
  704. UCHAR NpxIrql;
  705. UCHAR Spare5;
  706. BOOLEAN AutoAlignment;
  707. UCHAR Iopl;
  708. CCHAR FreezeCount;
  709. CCHAR SuspendCount;
  710. UCHAR Spare0[1];
  711. UCHAR UserIdealProcessor;
  712. volatile UCHAR DeferredProcessor;
  713. UCHAR AdjustReason;
  714. SCHAR AdjustIncrement;
  715. UCHAR Spare2[3];
  716. } KTHREAD, *PKTHREAD, *RESTRICTED_POINTER PRKTHREAD;
  717. //
  718. // ccNUMA supported in multiprocessor PAE and WIN64 systems only.
  719. //
  720. #if (defined(_WIN64) || defined(_X86PAE_)) && !defined(NT_UP)
  721. #define KE_MULTINODE
  722. #endif
  723. //
  724. // Profile object structure definition
  725. //
  726. typedef struct _KPROFILE {
  727. CSHORT Type;
  728. CSHORT Size;
  729. LIST_ENTRY ProfileListEntry;
  730. PKPROCESS Process;
  731. PVOID RangeBase;
  732. PVOID RangeLimit;
  733. ULONG BucketShift;
  734. PVOID Buffer;
  735. ULONG Segment;
  736. KAFFINITY Affinity;
  737. CSHORT Source;
  738. BOOLEAN Started;
  739. } KPROFILE, *PKPROFILE, *RESTRICTED_POINTER PRKPROFILE;
  740. //
  741. // Kernel control object functions
  742. //
  743. // APC object
  744. //
  745. // begin_ntosp
  746. NTKERNELAPI
  747. VOID
  748. KeInitializeApc (
  749. IN PRKAPC Apc,
  750. IN PRKTHREAD Thread,
  751. IN KAPC_ENVIRONMENT Environment,
  752. IN PKKERNEL_ROUTINE KernelRoutine,
  753. IN PKRUNDOWN_ROUTINE RundownRoutine OPTIONAL,
  754. IN PKNORMAL_ROUTINE NormalRoutine OPTIONAL,
  755. IN KPROCESSOR_MODE ProcessorMode OPTIONAL,
  756. IN PVOID NormalContext OPTIONAL
  757. );
  758. PLIST_ENTRY
  759. KeFlushQueueApc (
  760. IN PKTHREAD Thread,
  761. IN KPROCESSOR_MODE ProcessorMode
  762. );
  763. NTKERNELAPI
  764. BOOLEAN
  765. KeInsertQueueApc (
  766. IN PRKAPC Apc,
  767. IN PVOID SystemArgument1,
  768. IN PVOID SystemArgument2,
  769. IN KPRIORITY Increment
  770. );
  771. BOOLEAN
  772. KeRemoveQueueApc (
  773. IN PKAPC Apc
  774. );
  775. VOID
  776. KeGenericCallDpc (
  777. IN PKDEFERRED_ROUTINE Routine,
  778. IN PVOID Context
  779. );
  780. VOID
  781. KeSignalCallDpcDone (
  782. IN PVOID SystemArgument1
  783. );
  784. LOGICAL
  785. KeSignalCallDpcSynchronize (
  786. IN PVOID SystemArgument2
  787. );
  788. // end_ntosp
  789. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  790. //
  791. // DPC object
  792. //
  793. NTKERNELAPI
  794. VOID
  795. KeInitializeDpc (
  796. IN PRKDPC Dpc,
  797. IN PKDEFERRED_ROUTINE DeferredRoutine,
  798. IN PVOID DeferredContext
  799. );
  800. // end_ntddk end_wdm end_nthal end_ntifs
  801. NTKERNELAPI
  802. VOID
  803. KeInitializeThreadedDpc (
  804. IN PRKDPC Dpc,
  805. IN PKDEFERRED_ROUTINE DeferredRoutine,
  806. IN PVOID DeferredContext
  807. );
  808. // begin_ntddk begin_wdm begin_nthal begin_ntifs
  809. NTKERNELAPI
  810. BOOLEAN
  811. KeInsertQueueDpc (
  812. IN PRKDPC Dpc,
  813. IN PVOID SystemArgument1,
  814. IN PVOID SystemArgument2
  815. );
  816. NTKERNELAPI
  817. BOOLEAN
  818. KeRemoveQueueDpc (
  819. IN PRKDPC Dpc
  820. );
  821. // end_wdm
  822. NTKERNELAPI
  823. VOID
  824. KeSetImportanceDpc (
  825. IN PRKDPC Dpc,
  826. IN KDPC_IMPORTANCE Importance
  827. );
  828. NTKERNELAPI
  829. VOID
  830. KeSetTargetProcessorDpc (
  831. IN PRKDPC Dpc,
  832. IN CCHAR Number
  833. );
  834. // begin_wdm
  835. NTKERNELAPI
  836. VOID
  837. KeFlushQueuedDpcs (
  838. VOID
  839. );
  840. //
  841. // Device queue object
  842. //
  843. NTKERNELAPI
  844. VOID
  845. KeInitializeDeviceQueue (
  846. IN PKDEVICE_QUEUE DeviceQueue
  847. );
  848. NTKERNELAPI
  849. BOOLEAN
  850. KeInsertDeviceQueue (
  851. IN PKDEVICE_QUEUE DeviceQueue,
  852. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  853. );
  854. NTKERNELAPI
  855. BOOLEAN
  856. KeInsertByKeyDeviceQueue (
  857. IN PKDEVICE_QUEUE DeviceQueue,
  858. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry,
  859. IN ULONG SortKey
  860. );
  861. NTKERNELAPI
  862. PKDEVICE_QUEUE_ENTRY
  863. KeRemoveDeviceQueue (
  864. IN PKDEVICE_QUEUE DeviceQueue
  865. );
  866. NTKERNELAPI
  867. PKDEVICE_QUEUE_ENTRY
  868. KeRemoveByKeyDeviceQueue (
  869. IN PKDEVICE_QUEUE DeviceQueue,
  870. IN ULONG SortKey
  871. );
  872. NTKERNELAPI
  873. PKDEVICE_QUEUE_ENTRY
  874. KeRemoveByKeyDeviceQueueIfBusy (
  875. IN PKDEVICE_QUEUE DeviceQueue,
  876. IN ULONG SortKey
  877. );
  878. NTKERNELAPI
  879. BOOLEAN
  880. KeRemoveEntryDeviceQueue (
  881. IN PKDEVICE_QUEUE DeviceQueue,
  882. IN PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
  883. );
  884. // end_ntddk end_wdm end_ntifs end_ntosp
  885. //
  886. // Interrupt object
  887. //
  888. NTKERNELAPI
  889. VOID
  890. KeInitializeInterrupt (
  891. IN PKINTERRUPT Interrupt,
  892. IN PKSERVICE_ROUTINE ServiceRoutine,
  893. IN PVOID ServiceContext,
  894. IN PKSPIN_LOCK SpinLock OPTIONAL,
  895. IN ULONG Vector,
  896. IN KIRQL Irql,
  897. IN KIRQL SynchronizeIrql,
  898. IN KINTERRUPT_MODE InterruptMode,
  899. IN BOOLEAN ShareVector,
  900. IN CCHAR ProcessorNumber,
  901. IN BOOLEAN FloatingSave
  902. );
  903. #if defined(_AMD64_)
  904. #define NO_INTERRUPT_SPINLOCK ((PKSPIN_LOCK)-1I64)
  905. #endif
  906. NTKERNELAPI
  907. BOOLEAN
  908. KeConnectInterrupt (
  909. IN PKINTERRUPT Interrupt
  910. );
  911. // end_nthal
  912. NTKERNELAPI
  913. BOOLEAN
  914. KeDisconnectInterrupt (
  915. IN PKINTERRUPT Interrupt
  916. );
  917. // begin_ntddk begin_wdm begin_nthal begin_ntosp
  918. NTKERNELAPI
  919. BOOLEAN
  920. KeSynchronizeExecution (
  921. IN PKINTERRUPT Interrupt,
  922. IN PKSYNCHRONIZE_ROUTINE SynchronizeRoutine,
  923. IN PVOID SynchronizeContext
  924. );
  925. NTKERNELAPI
  926. KIRQL
  927. KeAcquireInterruptSpinLock (
  928. IN PKINTERRUPT Interrupt
  929. );
  930. NTKERNELAPI
  931. VOID
  932. KeReleaseInterruptSpinLock (
  933. IN PKINTERRUPT Interrupt,
  934. IN KIRQL OldIrql
  935. );
  936. // end_ntddk end_wdm end_nthal end_ntosp
  937. //
  938. // Profile object
  939. //
  940. VOID
  941. KeInitializeProfile (
  942. IN PKPROFILE Profile,
  943. IN PKPROCESS Process OPTIONAL,
  944. IN PVOID RangeBase,
  945. IN SIZE_T RangeSize,
  946. IN ULONG BucketSize,
  947. IN ULONG Segment,
  948. IN KPROFILE_SOURCE ProfileSource,
  949. IN KAFFINITY Affinity
  950. );
  951. BOOLEAN
  952. KeStartProfile (
  953. IN PKPROFILE Profile,
  954. IN PULONG Buffer
  955. );
  956. BOOLEAN
  957. KeStopProfile (
  958. IN PKPROFILE Profile
  959. );
  960. VOID
  961. KeSetIntervalProfile (
  962. IN ULONG Interval,
  963. IN KPROFILE_SOURCE Source
  964. );
  965. ULONG
  966. KeQueryIntervalProfile (
  967. IN KPROFILE_SOURCE Source
  968. );
  969. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  970. //
  971. // Kernel dispatcher object functions
  972. //
  973. // Event Object
  974. //
  975. // end_wdm end_ntddk end_nthal end_ntifs end_ntosp
  976. #if defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTHAL_)
  977. // begin_wdm begin_ntddk begin_nthal begin_ntifs begin_ntosp
  978. NTKERNELAPI
  979. VOID
  980. KeInitializeEvent (
  981. IN PRKEVENT Event,
  982. IN EVENT_TYPE Type,
  983. IN BOOLEAN State
  984. );
  985. NTKERNELAPI
  986. VOID
  987. KeClearEvent (
  988. IN PRKEVENT Event
  989. );
  990. // end_wdm end_ntddk end_nthal end_ntifs end_ntosp
  991. #else
  992. #define KeInitializeEvent(_Event, _Type, _State) \
  993. (_Event)->Header.Type = (UCHAR)_Type; \
  994. (_Event)->Header.Size = sizeof(KEVENT) / sizeof(LONG); \
  995. (_Event)->Header.SignalState = _State; \
  996. InitializeListHead(&(_Event)->Header.WaitListHead)
  997. #define KeClearEvent(Event) (Event)->Header.SignalState = 0
  998. #endif
  999. // begin_ntddk begin_ntifs begin_ntosp
  1000. NTKERNELAPI
  1001. LONG
  1002. KePulseEvent (
  1003. IN PRKEVENT Event,
  1004. IN KPRIORITY Increment,
  1005. IN BOOLEAN Wait
  1006. );
  1007. // end_ntddk end_ntifs end_ntosp
  1008. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  1009. NTKERNELAPI
  1010. LONG
  1011. KeReadStateEvent (
  1012. IN PRKEVENT Event
  1013. );
  1014. NTKERNELAPI
  1015. LONG
  1016. KeResetEvent (
  1017. IN PRKEVENT Event
  1018. );
  1019. NTKERNELAPI
  1020. LONG
  1021. KeSetEvent (
  1022. IN PRKEVENT Event,
  1023. IN KPRIORITY Increment,
  1024. IN BOOLEAN Wait
  1025. );
  1026. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  1027. VOID
  1028. KeSetEventBoostPriority (
  1029. IN PRKEVENT Event,
  1030. IN PRKTHREAD *Thread OPTIONAL
  1031. );
  1032. VOID
  1033. KeInitializeEventPair (
  1034. IN PKEVENT_PAIR EventPair
  1035. );
  1036. #define KeSetHighEventPair(EventPair, Increment, Wait) \
  1037. KeSetEvent(&((EventPair)->EventHigh), \
  1038. Increment, \
  1039. Wait)
  1040. #define KeSetLowEventPair(EventPair, Increment, Wait) \
  1041. KeSetEvent(&((EventPair)->EventLow), \
  1042. Increment, \
  1043. Wait)
  1044. //
  1045. // Mutant object
  1046. //
  1047. // begin_ntifs
  1048. NTKERNELAPI
  1049. VOID
  1050. KeInitializeMutant (
  1051. IN PRKMUTANT Mutant,
  1052. IN BOOLEAN InitialOwner
  1053. );
  1054. LONG
  1055. KeReadStateMutant (
  1056. IN PRKMUTANT Mutant
  1057. );
  1058. NTKERNELAPI
  1059. LONG
  1060. KeReleaseMutant (
  1061. IN PRKMUTANT Mutant,
  1062. IN KPRIORITY Increment,
  1063. IN BOOLEAN Abandoned,
  1064. IN BOOLEAN Wait
  1065. );
  1066. // begin_ntddk begin_wdm begin_nthal begin_ntosp
  1067. //
  1068. // Mutex object
  1069. //
  1070. NTKERNELAPI
  1071. VOID
  1072. KeInitializeMutex (
  1073. IN PRKMUTEX Mutex,
  1074. IN ULONG Level
  1075. );
  1076. NTKERNELAPI
  1077. LONG
  1078. KeReadStateMutex (
  1079. IN PRKMUTEX Mutex
  1080. );
  1081. NTKERNELAPI
  1082. LONG
  1083. KeReleaseMutex (
  1084. IN PRKMUTEX Mutex,
  1085. IN BOOLEAN Wait
  1086. );
  1087. // end_ntddk end_wdm
  1088. //
  1089. // Queue Object.
  1090. //
  1091. NTKERNELAPI
  1092. VOID
  1093. KeInitializeQueue (
  1094. IN PRKQUEUE Queue,
  1095. IN ULONG Count OPTIONAL
  1096. );
  1097. NTKERNELAPI
  1098. LONG
  1099. KeReadStateQueue (
  1100. IN PRKQUEUE Queue
  1101. );
  1102. NTKERNELAPI
  1103. LONG
  1104. KeInsertQueue (
  1105. IN PRKQUEUE Queue,
  1106. IN PLIST_ENTRY Entry
  1107. );
  1108. NTKERNELAPI
  1109. LONG
  1110. KeInsertHeadQueue (
  1111. IN PRKQUEUE Queue,
  1112. IN PLIST_ENTRY Entry
  1113. );
  1114. NTKERNELAPI
  1115. PLIST_ENTRY
  1116. KeRemoveQueue (
  1117. IN PRKQUEUE Queue,
  1118. IN KPROCESSOR_MODE WaitMode,
  1119. IN PLARGE_INTEGER Timeout OPTIONAL
  1120. );
  1121. PLIST_ENTRY
  1122. KeRundownQueue (
  1123. IN PRKQUEUE Queue
  1124. );
  1125. // begin_ntddk begin_wdm
  1126. //
  1127. // Semaphore object
  1128. //
  1129. NTKERNELAPI
  1130. VOID
  1131. KeInitializeSemaphore (
  1132. IN PRKSEMAPHORE Semaphore,
  1133. IN LONG Count,
  1134. IN LONG Limit
  1135. );
  1136. NTKERNELAPI
  1137. LONG
  1138. KeReadStateSemaphore (
  1139. IN PRKSEMAPHORE Semaphore
  1140. );
  1141. NTKERNELAPI
  1142. LONG
  1143. KeReleaseSemaphore (
  1144. IN PRKSEMAPHORE Semaphore,
  1145. IN KPRIORITY Increment,
  1146. IN LONG Adjustment,
  1147. IN BOOLEAN Wait
  1148. );
  1149. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  1150. //
  1151. // Process object
  1152. //
  1153. VOID
  1154. KeInitializeProcess (
  1155. IN PRKPROCESS Process,
  1156. IN KPRIORITY Priority,
  1157. IN KAFFINITY Affinity,
  1158. IN ULONG_PTR DirectoryTableBase[2],
  1159. IN BOOLEAN Enable
  1160. );
  1161. LOGICAL
  1162. KeForceAttachProcess (
  1163. IN PKPROCESS Process
  1164. );
  1165. // begin_ntifs begin_ntosp
  1166. NTKERNELAPI
  1167. VOID
  1168. KeAttachProcess (
  1169. IN PRKPROCESS Process
  1170. );
  1171. NTKERNELAPI
  1172. VOID
  1173. KeDetachProcess (
  1174. VOID
  1175. );
  1176. NTKERNELAPI
  1177. VOID
  1178. KeStackAttachProcess (
  1179. IN PRKPROCESS PROCESS,
  1180. OUT PRKAPC_STATE ApcState
  1181. );
  1182. NTKERNELAPI
  1183. VOID
  1184. KeUnstackDetachProcess (
  1185. IN PRKAPC_STATE ApcState
  1186. );
  1187. // end_ntifs end_ntosp
  1188. #define KiIsAttachedProcess() \
  1189. (KeGetCurrentThread()->ApcStateIndex == AttachedApcEnvironment)
  1190. #if !defined(_NTOSP_)
  1191. #define KeIsAttachedProcess() KiIsAttachedProcess()
  1192. #else
  1193. // begin_ntosp
  1194. NTKERNELAPI
  1195. BOOLEAN
  1196. KeIsAttachedProcess(
  1197. VOID
  1198. );
  1199. // end_ntosp
  1200. #endif
  1201. LONG
  1202. KeReadStateProcess (
  1203. IN PRKPROCESS Process
  1204. );
  1205. BOOLEAN
  1206. KeSetAutoAlignmentProcess (
  1207. IN PRKPROCESS Process,
  1208. IN BOOLEAN Enable
  1209. );
  1210. LONG
  1211. KeSetProcess (
  1212. IN PRKPROCESS Process,
  1213. IN KPRIORITY Increment,
  1214. IN BOOLEAN Wait
  1215. );
  1216. KAFFINITY
  1217. KeSetAffinityProcess (
  1218. IN PKPROCESS Process,
  1219. IN KAFFINITY Affinity
  1220. );
  1221. KPRIORITY
  1222. KeSetPriorityProcess (
  1223. IN PKPROCESS Process,
  1224. IN KPRIORITY BasePriority
  1225. );
  1226. LOGICAL
  1227. KeSetDisableQuantumProcess (
  1228. IN PKPROCESS Process,
  1229. IN LOGICAL Disable
  1230. );
  1231. #define KeTerminateProcess(Process) \
  1232. (Process)->StackCount += 1;
  1233. //
  1234. // Thread object
  1235. //
  1236. NTSTATUS
  1237. KeInitializeThread (
  1238. IN PKTHREAD Thread,
  1239. IN PVOID KernelStack OPTIONAL,
  1240. IN PKSYSTEM_ROUTINE SystemRoutine,
  1241. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  1242. IN PVOID StartContext OPTIONAL,
  1243. IN PCONTEXT ContextFrame OPTIONAL,
  1244. IN PVOID Teb OPTIONAL,
  1245. IN PKPROCESS Process
  1246. );
  1247. NTSTATUS
  1248. KeInitThread (
  1249. IN PKTHREAD Thread,
  1250. IN PVOID KernelStack OPTIONAL,
  1251. IN PKSYSTEM_ROUTINE SystemRoutine,
  1252. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  1253. IN PVOID StartContext OPTIONAL,
  1254. IN PCONTEXT ContextFrame OPTIONAL,
  1255. IN PVOID Teb OPTIONAL,
  1256. IN PKPROCESS Process
  1257. );
  1258. VOID
  1259. KeUninitThread (
  1260. IN PKTHREAD Thread
  1261. );
  1262. VOID
  1263. KeStartThread (
  1264. IN PKTHREAD Thread
  1265. );
  1266. BOOLEAN
  1267. KeAlertThread (
  1268. IN PKTHREAD Thread,
  1269. IN KPROCESSOR_MODE ProcessorMode
  1270. );
  1271. ULONG
  1272. KeAlertResumeThread (
  1273. IN PKTHREAD Thread
  1274. );
  1275. VOID
  1276. KeBoostPriorityThread (
  1277. IN PKTHREAD Thread,
  1278. IN KPRIORITY Increment
  1279. );
  1280. // begin_ntosp
  1281. NTKERNELAPI // ntddk wdm nthal ntifs
  1282. NTSTATUS // ntddk wdm nthal ntifs
  1283. KeDelayExecutionThread ( // ntddk wdm nthal ntifs
  1284. IN KPROCESSOR_MODE WaitMode, // ntddk wdm nthal ntifs
  1285. IN BOOLEAN Alertable, // ntddk wdm nthal ntifs
  1286. IN PLARGE_INTEGER Interval // ntddk wdm nthal ntifs
  1287. ); // ntddk wdm nthal ntifs
  1288. // ntddk wdm nthal ntifs
  1289. // end_ntosp
  1290. LOGICAL
  1291. KeSetDisableBoostThread (
  1292. IN PKTHREAD Thread,
  1293. IN LOGICAL Disable
  1294. );
  1295. ULONG
  1296. KeForceResumeThread (
  1297. IN PKTHREAD Thread
  1298. );
  1299. VOID
  1300. KeFreezeAllThreads (
  1301. VOID
  1302. );
  1303. BOOLEAN
  1304. KeQueryAutoAlignmentThread (
  1305. IN PKTHREAD Thread
  1306. );
  1307. LONG
  1308. KeQueryBasePriorityThread (
  1309. IN PKTHREAD Thread
  1310. );
  1311. NTKERNELAPI // ntddk wdm nthal ntifs
  1312. KPRIORITY // ntddk wdm nthal ntifs
  1313. KeQueryPriorityThread ( // ntddk wdm nthal ntifs
  1314. IN PKTHREAD Thread // ntddk wdm nthal ntifs
  1315. ); // ntddk wdm nthal ntifs
  1316. // ntddk wdm nthal ntifs
  1317. NTKERNELAPI // ntddk wdm nthal ntifs
  1318. ULONG // ntddk wdm nthal ntifs
  1319. KeQueryRuntimeThread ( // ntddk wdm nthal ntifs
  1320. IN PKTHREAD Thread, // ntddk wdm nthal ntifs
  1321. OUT PULONG UserTime // ntddk wdm nthal ntifs
  1322. ); // ntddk wdm nthal ntifs
  1323. // ntddk wdm nthal ntifs
  1324. BOOLEAN
  1325. KeReadStateThread (
  1326. IN PKTHREAD Thread
  1327. );
  1328. VOID
  1329. KeReadyThread (
  1330. IN PKTHREAD Thread
  1331. );
  1332. ULONG
  1333. KeResumeThread (
  1334. IN PKTHREAD Thread
  1335. );
  1336. // begin_nthal begin_ntosp
  1337. VOID
  1338. KeRevertToUserAffinityThread (
  1339. VOID
  1340. );
  1341. // end_nthal end_ntosp
  1342. VOID
  1343. KeRundownThread (
  1344. VOID
  1345. );
  1346. KAFFINITY
  1347. KeSetAffinityThread (
  1348. IN PKTHREAD Thread,
  1349. IN KAFFINITY Affinity
  1350. );
  1351. // begin_nthal begin_ntosp
  1352. VOID
  1353. KeSetSystemAffinityThread (
  1354. IN KAFFINITY Affinity
  1355. );
  1356. // end_nthal end_ntosp
  1357. BOOLEAN
  1358. KeSetAutoAlignmentThread (
  1359. IN PKTHREAD Thread,
  1360. IN BOOLEAN Enable
  1361. );
  1362. NTKERNELAPI // ntddk nthal ntifs ntosp
  1363. LONG // ntddk nthal ntifs ntosp
  1364. KeSetBasePriorityThread ( // ntddk nthal ntifs ntosp
  1365. IN PKTHREAD Thread, // ntddk nthal ntifs ntosp
  1366. IN LONG Increment // ntddk nthal ntifs ntosp
  1367. ); // ntddk nthal ntifs ntosp
  1368. // ntddk nthal ntifs ntosp
  1369. // begin_ntifs
  1370. NTKERNELAPI
  1371. UCHAR
  1372. KeSetIdealProcessorThread (
  1373. IN PKTHREAD Thread,
  1374. IN UCHAR Processor
  1375. );
  1376. // begin_ntosp
  1377. NTKERNELAPI
  1378. BOOLEAN
  1379. KeSetKernelStackSwapEnable (
  1380. IN BOOLEAN Enable
  1381. );
  1382. // end_ntifs
  1383. NTKERNELAPI // ntddk wdm nthal ntifs
  1384. KPRIORITY // ntddk wdm nthal ntifs
  1385. KeSetPriorityThread ( // ntddk wdm nthal ntifs
  1386. IN PKTHREAD Thread, // ntddk wdm nthal ntifs
  1387. IN KPRIORITY Priority // ntddk wdm nthal ntifs
  1388. ); // ntddk wdm nthal ntifs
  1389. // ntddk wdm nthal ntifs
  1390. // end_ntosp
  1391. ULONG
  1392. KeSuspendThread (
  1393. IN PKTHREAD
  1394. );
  1395. NTKERNELAPI
  1396. VOID
  1397. KeTerminateThread (
  1398. IN KPRIORITY Increment
  1399. );
  1400. BOOLEAN
  1401. KeTestAlertThread (
  1402. IN KPROCESSOR_MODE
  1403. );
  1404. VOID
  1405. KeThawAllThreads (
  1406. VOID
  1407. );
  1408. // begin_ntddk begin_nthal begin_ntifs begin_ntosp
  1409. #if ((defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) ||defined(_NTHAL_)) && !defined(_NTSYSTEM_DRIVER_) || defined(_NTOSP_))
  1410. // begin_wdm
  1411. NTKERNELAPI
  1412. VOID
  1413. KeEnterCriticalRegion (
  1414. VOID
  1415. );
  1416. NTKERNELAPI
  1417. VOID
  1418. KeLeaveCriticalRegion (
  1419. VOID
  1420. );
  1421. NTKERNELAPI
  1422. BOOLEAN
  1423. KeAreApcsDisabled (
  1424. VOID
  1425. );
  1426. // end_wdm
  1427. #endif
  1428. // begin_wdm
  1429. //
  1430. // Timer object
  1431. //
  1432. NTKERNELAPI
  1433. VOID
  1434. KeInitializeTimer (
  1435. IN PKTIMER Timer
  1436. );
  1437. NTKERNELAPI
  1438. VOID
  1439. KeInitializeTimerEx (
  1440. IN PKTIMER Timer,
  1441. IN TIMER_TYPE Type
  1442. );
  1443. NTKERNELAPI
  1444. BOOLEAN
  1445. KeCancelTimer (
  1446. IN PKTIMER
  1447. );
  1448. NTKERNELAPI
  1449. BOOLEAN
  1450. KeReadStateTimer (
  1451. PKTIMER Timer
  1452. );
  1453. NTKERNELAPI
  1454. BOOLEAN
  1455. KeSetTimer (
  1456. IN PKTIMER Timer,
  1457. IN LARGE_INTEGER DueTime,
  1458. IN PKDPC Dpc OPTIONAL
  1459. );
  1460. NTKERNELAPI
  1461. BOOLEAN
  1462. KeSetTimerEx (
  1463. IN PKTIMER Timer,
  1464. IN LARGE_INTEGER DueTime,
  1465. IN LONG Period OPTIONAL,
  1466. IN PKDPC Dpc OPTIONAL
  1467. );
  1468. // end_ntddk end_nthal end_ntifs end_wdm end_ntosp
  1469. extern volatile KAFFINITY KiIdleSummary;
  1470. FORCEINLINE
  1471. BOOLEAN
  1472. KeIsSMTSetIdle (
  1473. IN PKPRCB Prcb
  1474. )
  1475. /*++
  1476. Routine Description:
  1477. This routine determines whether the complete SMT set associated with the
  1478. specified processor is idle.
  1479. Arguments:
  1480. Prcb - Supplies a pointer to a processor control block (PRCB).
  1481. Return Value:
  1482. If the specified SMT set is idle, then TRUE is returned. Otherwise, FALSE
  1483. is returned.
  1484. --*/
  1485. {
  1486. #if !defined(NT_UP) && defined(_X86_)
  1487. if ((KiIdleSummary & Prcb->MultiThreadProcessorSet) == Prcb->MultiThreadProcessorSet) {
  1488. return TRUE;
  1489. } else {
  1490. return FALSE;
  1491. }
  1492. #else
  1493. UNREFERENCED_PARAMETER(Prcb);
  1494. return TRUE;
  1495. #endif
  1496. }
  1497. /*++
  1498. KPROCESSOR_MODE
  1499. KeGetPreviousMode (
  1500. VOID
  1501. )
  1502. Routine Description:
  1503. This function gets the threads previous mode from the trap frame
  1504. Arguments:
  1505. None.
  1506. Return Value:
  1507. KPROCESSOR_MODE - Previous mode for this thread.
  1508. --*/
  1509. #define KeGetPreviousMode() (KeGetCurrentThread()->PreviousMode)
  1510. /*++
  1511. KPROCESSOR_MODE
  1512. KeGetPReviousModeByThread (
  1513. PKTHREAD xxCurrentThread
  1514. )
  1515. Routine Description:
  1516. This function gets the threads previous mode from the trap frame.
  1517. Arguments:
  1518. xxCurrentThread - Current thread.
  1519. N.B. This must be the current thread.
  1520. Return Value:
  1521. KPROCESSOR_MODE - Previous mode for this thread.
  1522. --*/
  1523. #define KeGetPreviousModeByThread(xxCurrentThread) \
  1524. (ASSERT (xxCurrentThread == KeGetCurrentThread ()), \
  1525. (xxCurrentThread)->PreviousMode)
  1526. VOID
  1527. KeCheckForTimer(
  1528. IN PVOID p,
  1529. IN SIZE_T Size
  1530. );
  1531. VOID
  1532. KeClearTimer (
  1533. IN PKTIMER Timer
  1534. );
  1535. ULONGLONG
  1536. KeQueryTimerDueTime (
  1537. IN PKTIMER Timer
  1538. );
  1539. //
  1540. // Wait functions
  1541. //
  1542. NTSTATUS
  1543. KiSetServerWaitClientEvent (
  1544. IN PKEVENT SeverEvent,
  1545. IN PKEVENT ClientEvent,
  1546. IN ULONG WaitMode
  1547. );
  1548. #define KeSetHighWaitLowEventPair(EventPair, WaitMode) \
  1549. KiSetServerWaitClientEvent(&((EventPair)->EventHigh), \
  1550. &((EventPair)->EventLow), \
  1551. WaitMode)
  1552. #define KeSetLowWaitHighEventPair(EventPair, WaitMode) \
  1553. KiSetServerWaitClientEvent(&((EventPair)->EventLow), \
  1554. &((EventPair)->EventHigh), \
  1555. WaitMode)
  1556. #define KeWaitForHighEventPair(EventPair, WaitMode, Alertable, TimeOut) \
  1557. KeWaitForSingleObject(&((EventPair)->EventHigh), \
  1558. WrEventPair, \
  1559. WaitMode, \
  1560. Alertable, \
  1561. TimeOut)
  1562. #define KeWaitForLowEventPair(EventPair, WaitMode, Alertable, TimeOut) \
  1563. KeWaitForSingleObject(&((EventPair)->EventLow), \
  1564. WrEventPair, \
  1565. WaitMode, \
  1566. Alertable, \
  1567. TimeOut)
  1568. FORCEINLINE
  1569. VOID
  1570. KeWaitForContextSwap (
  1571. IN PKTHREAD Thread
  1572. )
  1573. /*++
  1574. Routine Description:
  1575. This routine waits until context swap is idle for the specified thread.
  1576. Arguments:
  1577. Thread - Supplies a pointer to a dispatcher object of type thread.
  1578. Return Value:
  1579. None.
  1580. --*/
  1581. {
  1582. #if !defined(NT_UP)
  1583. while (Thread->SwapBusy != FALSE) {
  1584. KeYieldProcessor();
  1585. }
  1586. #else
  1587. UNREFERENCED_PARAMETER(Thread);
  1588. #endif
  1589. return;
  1590. }
  1591. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  1592. #define KeWaitForMutexObject KeWaitForSingleObject
  1593. NTKERNELAPI
  1594. NTSTATUS
  1595. KeWaitForMultipleObjects (
  1596. IN ULONG Count,
  1597. IN PVOID Object[],
  1598. IN WAIT_TYPE WaitType,
  1599. IN KWAIT_REASON WaitReason,
  1600. IN KPROCESSOR_MODE WaitMode,
  1601. IN BOOLEAN Alertable,
  1602. IN PLARGE_INTEGER Timeout OPTIONAL,
  1603. IN PKWAIT_BLOCK WaitBlockArray OPTIONAL
  1604. );
  1605. NTKERNELAPI
  1606. NTSTATUS
  1607. KeWaitForSingleObject (
  1608. IN PVOID Object,
  1609. IN KWAIT_REASON WaitReason,
  1610. IN KPROCESSOR_MODE WaitMode,
  1611. IN BOOLEAN Alertable,
  1612. IN PLARGE_INTEGER Timeout OPTIONAL
  1613. );
  1614. //
  1615. // Define interprocess interrupt generic call types.
  1616. //
  1617. typedef
  1618. ULONG_PTR
  1619. (*PKIPI_BROADCAST_WORKER)(
  1620. IN ULONG_PTR Argument
  1621. );
  1622. ULONG_PTR
  1623. KeIpiGenericCall (
  1624. IN PKIPI_BROADCAST_WORKER BroadcastFunction,
  1625. IN ULONG_PTR Context
  1626. );
  1627. // end_ntosp end_ntddk end_wdm end_nthal end_ntifs
  1628. //
  1629. // Define internal kernel functions.
  1630. //
  1631. // N.B. These definitions are not public and are used elsewhere only under
  1632. // very special circumstances.
  1633. //
  1634. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntndis begin_ntosp
  1635. //
  1636. // On X86 the following routines are defined in the HAL and imported by
  1637. // all other modules.
  1638. //
  1639. #if defined(_X86_) && !defined(_NTHAL_)
  1640. #define _DECL_HAL_KE_IMPORT __declspec(dllimport)
  1641. #else
  1642. #define _DECL_HAL_KE_IMPORT
  1643. #endif
  1644. // end_ntddk end_wdm end_nthal end_ntifs end_ntndis end_ntosp
  1645. #if defined(NT_UP)
  1646. #define KeTestForWaitersQueuedSpinLock(Number) FALSE
  1647. #define KeAcquireQueuedSpinLockRaiseToSynch(Number) \
  1648. KeRaiseIrqlToSynchLevel()
  1649. #define KeAcquireQueuedSpinLock(Number) \
  1650. KeRaiseIrqlToDpcLevel()
  1651. #define KeReleaseQueuedSpinLock(Number, OldIrql) \
  1652. KeLowerIrql(OldIrql)
  1653. #define KeTryToAcquireQueuedSpinLockRaiseToSynch(Number, OldIrql) \
  1654. (*(OldIrql) = KeRaiseIrqlToSynchLevel(), TRUE)
  1655. #define KeTryToAcquireQueuedSpinLock(Number, OldIrql) \
  1656. (KeRaiseIrql(DISPATCH_LEVEL, OldIrql), TRUE)
  1657. #define KeAcquireQueuedSpinLockAtDpcLevel(LockQueue)
  1658. #define KeReleaseQueuedSpinLockFromDpcLevel(LockQueue)
  1659. #define KeTryToAcquireQueuedSpinLockAtRaisedIrql(LockQueue) (TRUE)
  1660. #else // NT_UP
  1661. //
  1662. // Queued spin lock functions.
  1663. //
  1664. FORCEINLINE
  1665. LOGICAL
  1666. KeTestForWaitersQueuedSpinLock (
  1667. IN KSPIN_LOCK_QUEUE_NUMBER Number
  1668. )
  1669. {
  1670. PKSPIN_LOCK Spinlock;
  1671. PKPRCB Prcb;
  1672. Prcb = KeGetCurrentPrcb();
  1673. Spinlock =
  1674. (PKSPIN_LOCK)((ULONG_PTR)Prcb->LockQueue[Number].Lock & ~(LOCK_QUEUE_WAIT | LOCK_QUEUE_OWNER));
  1675. return (*Spinlock != 0);
  1676. }
  1677. VOID
  1678. FASTCALL
  1679. KeAcquireQueuedSpinLockAtDpcLevel (
  1680. IN PKSPIN_LOCK_QUEUE LockQueue
  1681. );
  1682. VOID
  1683. FASTCALL
  1684. KeReleaseQueuedSpinLockFromDpcLevel (
  1685. IN PKSPIN_LOCK_QUEUE LockQueue
  1686. );
  1687. LOGICAL
  1688. FASTCALL
  1689. KeTryToAcquireQueuedSpinLockAtRaisedIrql (
  1690. IN PKSPIN_LOCK_QUEUE QueuedLock
  1691. );
  1692. // begin_ntifs begin_ntosp
  1693. _DECL_HAL_KE_IMPORT
  1694. KIRQL
  1695. FASTCALL
  1696. KeAcquireQueuedSpinLock (
  1697. IN KSPIN_LOCK_QUEUE_NUMBER Number
  1698. );
  1699. _DECL_HAL_KE_IMPORT
  1700. VOID
  1701. FASTCALL
  1702. KeReleaseQueuedSpinLock (
  1703. IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1704. IN KIRQL OldIrql
  1705. );
  1706. _DECL_HAL_KE_IMPORT
  1707. LOGICAL
  1708. FASTCALL
  1709. KeTryToAcquireQueuedSpinLock(
  1710. IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1711. IN PKIRQL OldIrql
  1712. );
  1713. // end_ntifs end_ntosp
  1714. _DECL_HAL_KE_IMPORT
  1715. KIRQL
  1716. FASTCALL
  1717. KeAcquireQueuedSpinLockRaiseToSynch (
  1718. IN KSPIN_LOCK_QUEUE_NUMBER Number
  1719. );
  1720. _DECL_HAL_KE_IMPORT
  1721. LOGICAL
  1722. FASTCALL
  1723. KeTryToAcquireQueuedSpinLockRaiseToSynch(
  1724. IN KSPIN_LOCK_QUEUE_NUMBER Number,
  1725. IN PKIRQL OldIrql
  1726. );
  1727. #endif // NT_UP
  1728. #define KeQueuedSpinLockContext(n) (&(KeGetCurrentPrcb()->LockQueue[n]))
  1729. //
  1730. // On Uni-processor systems there is no real Dispatcher Database Lock
  1731. // so raising to SYNCH won't help get the lock released any sooner.
  1732. //
  1733. #if defined(NT_UP)
  1734. #if defined(_X86_)
  1735. #define KiLockDispatcherDatabase(OldIrql) \
  1736. *(OldIrql) = KeRaiseIrqlToDpcLevel()
  1737. #else
  1738. #define KiLockDispatcherDatabase(OldIrql) \
  1739. *(OldIrql) = KeRaiseIrqlToSynchLevel()
  1740. #endif
  1741. #else // NT_UP
  1742. #define KiLockDispatcherDatabase(OldIrql) \
  1743. *(OldIrql) = KeAcquireQueuedSpinLockRaiseToSynch(LockQueueDispatcherLock)
  1744. #endif // NT_UP
  1745. #if defined(NT_UP)
  1746. #define KiLockDispatcherDatabaseAtSynchLevel()
  1747. #define KiUnlockDispatcherDatabaseFromSynchLevel()
  1748. #else
  1749. #define KiLockDispatcherDatabaseAtSynchLevel() \
  1750. KeAcquireQueuedSpinLockAtDpcLevel(&KeGetCurrentPrcb()->LockQueue[LockQueueDispatcherLock])
  1751. #define KiUnlockDispatcherDatabaseFromSynchLevel() \
  1752. KeReleaseQueuedSpinLockFromDpcLevel(&KeGetCurrentPrcb()->LockQueue[LockQueueDispatcherLock])
  1753. #endif
  1754. VOID
  1755. FASTCALL
  1756. KiSetPriorityThread (
  1757. IN PRKTHREAD Thread,
  1758. IN KPRIORITY Priority
  1759. );
  1760. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntndis begin_ntosp
  1761. //
  1762. // spin lock functions
  1763. //
  1764. #if defined(_X86_) && (defined(_WDMDDK_) || defined(WIN9X_COMPAT_SPINLOCK))
  1765. NTKERNELAPI
  1766. VOID
  1767. NTAPI
  1768. KeInitializeSpinLock (
  1769. IN PKSPIN_LOCK SpinLock
  1770. );
  1771. #else
  1772. __inline
  1773. VOID
  1774. NTAPI
  1775. KeInitializeSpinLock (
  1776. IN PKSPIN_LOCK SpinLock
  1777. )
  1778. {
  1779. *SpinLock = 0;
  1780. }
  1781. #endif
  1782. #if defined(_X86_)
  1783. NTKERNELAPI
  1784. VOID
  1785. FASTCALL
  1786. KefAcquireSpinLockAtDpcLevel (
  1787. IN PKSPIN_LOCK SpinLock
  1788. );
  1789. NTKERNELAPI
  1790. VOID
  1791. FASTCALL
  1792. KefReleaseSpinLockFromDpcLevel (
  1793. IN PKSPIN_LOCK SpinLock
  1794. );
  1795. #define KeAcquireSpinLockAtDpcLevel(a) KefAcquireSpinLockAtDpcLevel(a)
  1796. #define KeReleaseSpinLockFromDpcLevel(a) KefReleaseSpinLockFromDpcLevel(a)
  1797. _DECL_HAL_KE_IMPORT
  1798. KIRQL
  1799. FASTCALL
  1800. KfAcquireSpinLock (
  1801. IN PKSPIN_LOCK SpinLock
  1802. );
  1803. _DECL_HAL_KE_IMPORT
  1804. VOID
  1805. FASTCALL
  1806. KfReleaseSpinLock (
  1807. IN PKSPIN_LOCK SpinLock,
  1808. IN KIRQL NewIrql
  1809. );
  1810. // end_wdm end_ntddk
  1811. _DECL_HAL_KE_IMPORT
  1812. KIRQL
  1813. FASTCALL
  1814. KeAcquireSpinLockRaiseToSynch (
  1815. IN PKSPIN_LOCK SpinLock
  1816. );
  1817. // begin_wdm begin_ntddk
  1818. #define KeAcquireSpinLock(a,b) *(b) = KfAcquireSpinLock(a)
  1819. #define KeReleaseSpinLock(a,b) KfReleaseSpinLock(a,b)
  1820. NTKERNELAPI
  1821. BOOLEAN
  1822. FASTCALL
  1823. KeTestSpinLock (
  1824. IN PKSPIN_LOCK SpinLock
  1825. );
  1826. NTKERNELAPI
  1827. BOOLEAN
  1828. FASTCALL
  1829. KeTryToAcquireSpinLockAtDpcLevel (
  1830. IN PKSPIN_LOCK SpinLock
  1831. );
  1832. #else
  1833. //
  1834. // These functions are imported for IA64, ntddk, ntifs, nthal, ntosp, and wdm.
  1835. // They can be inlined for the system on AMD64.
  1836. //
  1837. #define KeAcquireSpinLock(SpinLock, OldIrql) \
  1838. *(OldIrql) = KeAcquireSpinLockRaiseToDpc(SpinLock)
  1839. #if defined(_IA64_) || defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTHAL_) || defined(_NTOSP_) || defined(_WDMDDK_)
  1840. // end_wdm end_ntddk
  1841. NTKERNELAPI
  1842. KIRQL
  1843. FASTCALL
  1844. KeAcquireSpinLockRaiseToSynch (
  1845. IN PKSPIN_LOCK SpinLock
  1846. );
  1847. // begin_wdm begin_ntddk
  1848. NTKERNELAPI
  1849. VOID
  1850. KeAcquireSpinLockAtDpcLevel (
  1851. IN PKSPIN_LOCK SpinLock
  1852. );
  1853. NTKERNELAPI
  1854. KIRQL
  1855. KeAcquireSpinLockRaiseToDpc (
  1856. IN PKSPIN_LOCK SpinLock
  1857. );
  1858. NTKERNELAPI
  1859. VOID
  1860. KeReleaseSpinLock (
  1861. IN PKSPIN_LOCK SpinLock,
  1862. IN KIRQL NewIrql
  1863. );
  1864. NTKERNELAPI
  1865. VOID
  1866. KeReleaseSpinLockFromDpcLevel (
  1867. IN PKSPIN_LOCK SpinLock
  1868. );
  1869. NTKERNELAPI
  1870. BOOLEAN
  1871. FASTCALL
  1872. KeTestSpinLock (
  1873. IN PKSPIN_LOCK SpinLock
  1874. );
  1875. NTKERNELAPI
  1876. BOOLEAN
  1877. FASTCALL
  1878. KeTryToAcquireSpinLockAtDpcLevel (
  1879. IN PKSPIN_LOCK SpinLock
  1880. );
  1881. #else
  1882. #if defined(_AMD64_)
  1883. //
  1884. // The system version of these functions are defined in amd64.h for AMD64.
  1885. //
  1886. #endif
  1887. #endif
  1888. #endif
  1889. // end_wdm end_ntddk end_nthal end_ntifs
  1890. NTKERNELAPI
  1891. KIRQL
  1892. FASTCALL
  1893. KeAcquireSpinLockForDpc (
  1894. IN PKSPIN_LOCK SpinLock
  1895. );
  1896. NTKERNELAPI
  1897. VOID
  1898. FASTCALL
  1899. KeReleaseSpinLockForDpc (
  1900. IN PKSPIN_LOCK SpinLock,
  1901. IN KIRQL OldIrql
  1902. );
  1903. // end_ntndis end_ntosp
  1904. #if !defined(_AMD64_)
  1905. BOOLEAN
  1906. KeTryToAcquireSpinLock (
  1907. IN PKSPIN_LOCK SpinLock,
  1908. OUT PKIRQL OldIrql
  1909. );
  1910. #endif
  1911. //
  1912. // Enable and disable interrupts.
  1913. //
  1914. // begin_nthal
  1915. //
  1916. NTKERNELAPI
  1917. BOOLEAN
  1918. KeDisableInterrupts (
  1919. VOID
  1920. );
  1921. NTKERNELAPI
  1922. VOID
  1923. KeEnableInterrupts (
  1924. IN BOOLEAN Enable
  1925. );
  1926. // end_nthal
  1927. //
  1928. // Raise and lower IRQL functions.
  1929. //
  1930. #if defined(_NTDRIVER_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTHAL_) || !defined(_APIC_TPR_)
  1931. // begin_nthal begin_wdm begin_ntddk begin_ntifs begin_ntosp
  1932. #if defined(_X86_)
  1933. _DECL_HAL_KE_IMPORT
  1934. VOID
  1935. FASTCALL
  1936. KfLowerIrql (
  1937. IN KIRQL NewIrql
  1938. );
  1939. _DECL_HAL_KE_IMPORT
  1940. KIRQL
  1941. FASTCALL
  1942. KfRaiseIrql (
  1943. IN KIRQL NewIrql
  1944. );
  1945. // end_wdm
  1946. _DECL_HAL_KE_IMPORT
  1947. KIRQL
  1948. KeRaiseIrqlToDpcLevel(
  1949. VOID
  1950. );
  1951. // end_ntddk
  1952. _DECL_HAL_KE_IMPORT
  1953. KIRQL
  1954. KeRaiseIrqlToSynchLevel(
  1955. VOID
  1956. );
  1957. // begin_wdm begin_ntddk
  1958. #define KeLowerIrql(a) KfLowerIrql(a)
  1959. #define KeRaiseIrql(a,b) *(b) = KfRaiseIrql(a)
  1960. // end_wdm
  1961. // begin_wdm
  1962. #elif defined(_IA64_)
  1963. //
  1964. // These function are defined in IA64.h for the IA64 platform.
  1965. //
  1966. #elif defined(_AMD64_)
  1967. //
  1968. // These function are defined in amd64.h for the AMD64 platform.
  1969. //
  1970. #else
  1971. #error "no target architecture"
  1972. #endif
  1973. // end_nthal end_wdm end_ntddk end_ntifs end_ntosp
  1974. #else
  1975. extern PUCHAR HalpIRQLToTPR;
  1976. extern PUCHAR HalpVectorToIRQL;
  1977. #define APIC_TPR ((volatile ULONG *)0xFFFE0080)
  1978. #define KeGetCurrentIrql _KeGetCurrentIrql
  1979. #define KfLowerIrql _KfLowerIrql
  1980. #define KfRaiseIrql _KfRaiseIrql
  1981. KIRQL
  1982. FORCEINLINE
  1983. KeGetCurrentIrql (
  1984. VOID
  1985. )
  1986. {
  1987. ULONG tprValue;
  1988. KIRQL currentIrql;
  1989. tprValue = *APIC_TPR;
  1990. currentIrql = HalpVectorToIRQL[ tprValue / 16 ];
  1991. return currentIrql;
  1992. }
  1993. VOID
  1994. FORCEINLINE
  1995. KfLowerIrql (
  1996. IN KIRQL NewIrql
  1997. )
  1998. {
  1999. ULONG tprValue;
  2000. ASSERT( NewIrql <= KeGetCurrentIrql() );
  2001. tprValue = HalpIRQLToTPR[NewIrql];
  2002. KeMemoryBarrier();
  2003. *APIC_TPR = tprValue;
  2004. *APIC_TPR;
  2005. KeMemoryBarrier();
  2006. }
  2007. KIRQL
  2008. FORCEINLINE
  2009. KfRaiseIrql (
  2010. IN KIRQL NewIrql
  2011. )
  2012. {
  2013. KIRQL oldIrql;
  2014. ULONG tprValue;
  2015. oldIrql = KeGetCurrentIrql();
  2016. ASSERT( NewIrql >= oldIrql );
  2017. tprValue = HalpIRQLToTPR[NewIrql];
  2018. KeMemoryBarrier();
  2019. *APIC_TPR = tprValue;
  2020. KeMemoryBarrier();
  2021. return oldIrql;
  2022. }
  2023. KIRQL
  2024. FORCEINLINE
  2025. KeRaiseIrqlToDpcLevel (
  2026. VOID
  2027. )
  2028. {
  2029. return KfRaiseIrql(DISPATCH_LEVEL);
  2030. }
  2031. KIRQL
  2032. FORCEINLINE
  2033. KeRaiseIrqlToSynchLevel (
  2034. VOID
  2035. )
  2036. {
  2037. return KfRaiseIrql(SYNCH_LEVEL);
  2038. }
  2039. #define KeLowerIrql(a) KfLowerIrql(a)
  2040. #define KeRaiseIrql(a,b) *(b) = KfRaiseIrql(a)
  2041. #endif
  2042. // begin_ntddk begin_nthal begin_ntifs begin_ntosp
  2043. //
  2044. // Queued spin lock functions for "in stack" lock handles.
  2045. //
  2046. // The following three functions RAISE and LOWER IRQL when a queued
  2047. // in stack spin lock is acquired or released using these routines.
  2048. //
  2049. _DECL_HAL_KE_IMPORT
  2050. VOID
  2051. FASTCALL
  2052. KeAcquireInStackQueuedSpinLock (
  2053. IN PKSPIN_LOCK SpinLock,
  2054. IN PKLOCK_QUEUE_HANDLE LockHandle
  2055. );
  2056. // end_ntddk end_nthal end_ntifs end_ntosp
  2057. _DECL_HAL_KE_IMPORT
  2058. VOID
  2059. FASTCALL
  2060. KeAcquireInStackQueuedSpinLockRaiseToSynch (
  2061. IN PKSPIN_LOCK SpinLock,
  2062. IN PKLOCK_QUEUE_HANDLE LockHandle
  2063. );
  2064. // begin_ntddk begin_nthal begin_ntifs begin_ntosp
  2065. _DECL_HAL_KE_IMPORT
  2066. VOID
  2067. FASTCALL
  2068. KeReleaseInStackQueuedSpinLock (
  2069. IN PKLOCK_QUEUE_HANDLE LockHandle
  2070. );
  2071. //
  2072. // The following two functions do NOT raise or lower IRQL when a queued
  2073. // in stack spin lock is acquired or released using these functions.
  2074. //
  2075. NTKERNELAPI
  2076. VOID
  2077. FASTCALL
  2078. KeAcquireInStackQueuedSpinLockAtDpcLevel (
  2079. IN PKSPIN_LOCK SpinLock,
  2080. IN PKLOCK_QUEUE_HANDLE LockHandle
  2081. );
  2082. NTKERNELAPI
  2083. VOID
  2084. FASTCALL
  2085. KeReleaseInStackQueuedSpinLockFromDpcLevel (
  2086. IN PKLOCK_QUEUE_HANDLE LockHandle
  2087. );
  2088. // end_ntddk end_nthal end_ntifs
  2089. //
  2090. // The following two functions conditionally raise or lower IRQL when a
  2091. // queued in-stack spin lock is acquired or released using these functions.
  2092. //
  2093. NTKERNELAPI
  2094. VOID
  2095. FASTCALL
  2096. KeAcquireInStackQueuedSpinLockForDpc (
  2097. IN PKSPIN_LOCK SpinLock,
  2098. IN PKLOCK_QUEUE_HANDLE LockHandle
  2099. );
  2100. NTKERNELAPI
  2101. VOID
  2102. FASTCALL
  2103. KeReleaseInStackQueuedSpinLockForDpc (
  2104. IN PKLOCK_QUEUE_HANDLE LockHandle
  2105. );
  2106. // end_ntosp
  2107. //
  2108. // Initialize kernel in phase 1.
  2109. //
  2110. BOOLEAN
  2111. KeInitSystem(
  2112. VOID
  2113. );
  2114. VOID
  2115. KeNumaInitialize(
  2116. VOID
  2117. );
  2118. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  2119. //
  2120. // Miscellaneous kernel functions
  2121. //
  2122. typedef enum _KBUGCHECK_BUFFER_DUMP_STATE {
  2123. BufferEmpty,
  2124. BufferInserted,
  2125. BufferStarted,
  2126. BufferFinished,
  2127. BufferIncomplete
  2128. } KBUGCHECK_BUFFER_DUMP_STATE;
  2129. typedef
  2130. VOID
  2131. (*PKBUGCHECK_CALLBACK_ROUTINE) (
  2132. IN PVOID Buffer,
  2133. IN ULONG Length
  2134. );
  2135. typedef struct _KBUGCHECK_CALLBACK_RECORD {
  2136. LIST_ENTRY Entry;
  2137. PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine;
  2138. PVOID Buffer;
  2139. ULONG Length;
  2140. PUCHAR Component;
  2141. ULONG_PTR Checksum;
  2142. UCHAR State;
  2143. } KBUGCHECK_CALLBACK_RECORD, *PKBUGCHECK_CALLBACK_RECORD;
  2144. #define KeInitializeCallbackRecord(CallbackRecord) \
  2145. (CallbackRecord)->State = BufferEmpty
  2146. NTKERNELAPI
  2147. BOOLEAN
  2148. KeDeregisterBugCheckCallback (
  2149. IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord
  2150. );
  2151. NTKERNELAPI
  2152. BOOLEAN
  2153. KeRegisterBugCheckCallback (
  2154. IN PKBUGCHECK_CALLBACK_RECORD CallbackRecord,
  2155. IN PKBUGCHECK_CALLBACK_ROUTINE CallbackRoutine,
  2156. IN PVOID Buffer,
  2157. IN ULONG Length,
  2158. IN PUCHAR Component
  2159. );
  2160. typedef enum _KBUGCHECK_CALLBACK_REASON {
  2161. KbCallbackInvalid,
  2162. KbCallbackReserved1,
  2163. KbCallbackSecondaryDumpData,
  2164. KbCallbackDumpIo,
  2165. } KBUGCHECK_CALLBACK_REASON;
  2166. typedef
  2167. VOID
  2168. (*PKBUGCHECK_REASON_CALLBACK_ROUTINE) (
  2169. IN KBUGCHECK_CALLBACK_REASON Reason,
  2170. IN struct _KBUGCHECK_REASON_CALLBACK_RECORD* Record,
  2171. IN OUT PVOID ReasonSpecificData,
  2172. IN ULONG ReasonSpecificDataLength
  2173. );
  2174. typedef struct _KBUGCHECK_REASON_CALLBACK_RECORD {
  2175. LIST_ENTRY Entry;
  2176. PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine;
  2177. PUCHAR Component;
  2178. ULONG_PTR Checksum;
  2179. KBUGCHECK_CALLBACK_REASON Reason;
  2180. UCHAR State;
  2181. } KBUGCHECK_REASON_CALLBACK_RECORD, *PKBUGCHECK_REASON_CALLBACK_RECORD;
  2182. typedef struct _KBUGCHECK_SECONDARY_DUMP_DATA {
  2183. IN PVOID InBuffer;
  2184. IN ULONG InBufferLength;
  2185. IN ULONG MaximumAllowed;
  2186. OUT GUID Guid;
  2187. OUT PVOID OutBuffer;
  2188. OUT ULONG OutBufferLength;
  2189. } KBUGCHECK_SECONDARY_DUMP_DATA, *PKBUGCHECK_SECONDARY_DUMP_DATA;
  2190. typedef enum _KBUGCHECK_DUMP_IO_TYPE
  2191. {
  2192. KbDumpIoInvalid,
  2193. KbDumpIoHeader,
  2194. KbDumpIoBody,
  2195. KbDumpIoSecondaryData,
  2196. KbDumpIoComplete
  2197. } KBUGCHECK_DUMP_IO_TYPE;
  2198. typedef struct _KBUGCHECK_DUMP_IO {
  2199. IN ULONG64 Offset;
  2200. IN PVOID Buffer;
  2201. IN ULONG BufferLength;
  2202. IN KBUGCHECK_DUMP_IO_TYPE Type;
  2203. } KBUGCHECK_DUMP_IO, *PKBUGCHECK_DUMP_IO;
  2204. NTKERNELAPI
  2205. BOOLEAN
  2206. KeDeregisterBugCheckReasonCallback (
  2207. IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord
  2208. );
  2209. NTKERNELAPI
  2210. BOOLEAN
  2211. KeRegisterBugCheckReasonCallback (
  2212. IN PKBUGCHECK_REASON_CALLBACK_RECORD CallbackRecord,
  2213. IN PKBUGCHECK_REASON_CALLBACK_ROUTINE CallbackRoutine,
  2214. IN KBUGCHECK_CALLBACK_REASON Reason,
  2215. IN PUCHAR Component
  2216. );
  2217. typedef
  2218. BOOLEAN
  2219. (*PNMI_CALLBACK)(
  2220. IN PVOID Context,
  2221. IN BOOLEAN Handled
  2222. );
  2223. NTKERNELAPI
  2224. PVOID
  2225. KeRegisterNmiCallback(
  2226. PNMI_CALLBACK CallbackRoutine,
  2227. PVOID Context
  2228. );
  2229. NTSTATUS
  2230. KeDeregisterNmiCallback(
  2231. PVOID Handle
  2232. );
  2233. // end_wdm
  2234. NTKERNELAPI
  2235. DECLSPEC_NORETURN
  2236. VOID
  2237. NTAPI
  2238. KeBugCheck (
  2239. IN ULONG BugCheckCode
  2240. );
  2241. // end_ntddk end_nthal end_ntifs end_ntosp
  2242. VOID
  2243. KeBugCheck2(
  2244. IN ULONG BugCheckCode,
  2245. IN ULONG_PTR BugCheckParameter1,
  2246. IN ULONG_PTR BugCheckParameter2,
  2247. IN ULONG_PTR BugCheckParameter3,
  2248. IN ULONG_PTR BugCheckParameter4,
  2249. IN PVOID SaveDataPage
  2250. );
  2251. BOOLEAN
  2252. KeGetBugMessageText(
  2253. IN ULONG MessageId,
  2254. IN PANSI_STRING ReturnedString OPTIONAL
  2255. );
  2256. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  2257. NTKERNELAPI
  2258. DECLSPEC_NORETURN
  2259. VOID
  2260. KeBugCheckEx(
  2261. IN ULONG BugCheckCode,
  2262. IN ULONG_PTR BugCheckParameter1,
  2263. IN ULONG_PTR BugCheckParameter2,
  2264. IN ULONG_PTR BugCheckParameter3,
  2265. IN ULONG_PTR BugCheckParameter4
  2266. );
  2267. // end_ntddk end_wdm end_ntifs end_ntosp
  2268. NTKERNELAPI
  2269. VOID
  2270. KeEnterKernelDebugger (
  2271. VOID
  2272. );
  2273. // end_nthal
  2274. typedef
  2275. PCHAR
  2276. (*PKE_BUGCHECK_UNICODE_TO_ANSI) (
  2277. IN PUNICODE_STRING UnicodeString,
  2278. OUT PCHAR AnsiBuffer,
  2279. IN ULONG MaxAnsiLength
  2280. );
  2281. VOID
  2282. KeDumpMachineState (
  2283. IN PKPROCESSOR_STATE ProcessorState,
  2284. IN PCHAR Buffer,
  2285. IN PULONG_PTR BugCheckParameters,
  2286. IN ULONG NumberOfParameters,
  2287. IN PKE_BUGCHECK_UNICODE_TO_ANSI UnicodeToAnsiRoutine
  2288. );
  2289. VOID
  2290. KeContextFromKframes (
  2291. IN PKTRAP_FRAME TrapFrame,
  2292. IN PKEXCEPTION_FRAME ExceptionFrame,
  2293. IN OUT PCONTEXT ContextFrame
  2294. );
  2295. VOID
  2296. KeContextToKframes (
  2297. IN OUT PKTRAP_FRAME TrapFrame,
  2298. IN OUT PKEXCEPTION_FRAME ExceptionFrame,
  2299. IN PCONTEXT ContextFrame,
  2300. IN ULONG ContextFlags,
  2301. IN KPROCESSOR_MODE PreviousMode
  2302. );
  2303. // begin_nthal
  2304. VOID
  2305. __cdecl
  2306. KeSaveStateForHibernate(
  2307. IN PKPROCESSOR_STATE ProcessorState
  2308. );
  2309. // end_nthal
  2310. VOID
  2311. KeCopyTrapDispatcher (
  2312. VOID
  2313. );
  2314. BOOLEAN
  2315. FASTCALL
  2316. KeInvalidAccessAllowed (
  2317. IN PVOID TrapInformation OPTIONAL
  2318. );
  2319. //
  2320. // GDI TEB Batch Flush routine
  2321. //
  2322. typedef
  2323. VOID
  2324. (*PGDI_BATCHFLUSH_ROUTINE) (
  2325. VOID
  2326. );
  2327. //
  2328. // Find first set left in affinity mask.
  2329. //
  2330. #if defined(_WIN64)
  2331. #if defined(_AMD64_) && !defined(_X86AMD64_)
  2332. #define KeFindFirstSetLeftAffinity(Set, Member) BitScanReverse64(Member, Set)
  2333. #else
  2334. #define KeFindFirstSetLeftAffinity(Set, Member) { \
  2335. ULONG _Mask_; \
  2336. ULONG _Offset_ = 32; \
  2337. if ((_Mask_ = (ULONG)(Set >> 32)) == 0) { \
  2338. _Offset_ = 0; \
  2339. _Mask_ = (ULONG)Set; \
  2340. } \
  2341. KeFindFirstSetLeftMember(_Mask_, Member); \
  2342. *(Member) += _Offset_; \
  2343. }
  2344. #endif
  2345. #else
  2346. #define KeFindFirstSetLeftAffinity(Set, Member) \
  2347. KeFindFirstSetLeftMember(Set, Member)
  2348. #endif // defined(_WIN64)
  2349. //
  2350. // Find first set left in 32-bit set.
  2351. //
  2352. #if defined(_WIN64)
  2353. #if defined(_AMD64_) && !defined(_X86AMD64_)
  2354. #define KeFindFirstSetLeftMember(Set, Member) BitScanReverse(Member, Set)
  2355. #else
  2356. extern const CCHAR KiFindFirstSetLeft[];
  2357. #define KeFindFirstSetLeftMember(Set, Member) { \
  2358. ULONG _Mask; \
  2359. ULONG _Offset = 16; \
  2360. if ((_Mask = Set >> 16) == 0) { \
  2361. _Offset = 0; \
  2362. _Mask = Set; \
  2363. } \
  2364. if (_Mask >> 8) { \
  2365. _Offset += 8; \
  2366. } \
  2367. *(Member) = KiFindFirstSetLeft[Set >> _Offset] + _Offset; \
  2368. }
  2369. #endif
  2370. #else
  2371. FORCEINLINE
  2372. ULONG
  2373. KiFindFirstSetLeftMemberInt (
  2374. ULONG Set
  2375. )
  2376. {
  2377. __asm {
  2378. bsr eax, Set
  2379. }
  2380. }
  2381. FORCEINLINE
  2382. void
  2383. KeFindFirstSetLeftMember (
  2384. ULONG Set,
  2385. PULONG Member
  2386. )
  2387. {
  2388. *Member = KiFindFirstSetLeftMemberInt (Set);
  2389. }
  2390. #endif
  2391. ULONG
  2392. KeFindNextRightSetAffinity (
  2393. ULONG Number,
  2394. KAFFINITY Set
  2395. );
  2396. //
  2397. // Find first set right in 32-bit set.
  2398. //
  2399. extern const CCHAR KiFindFirstSetRight[];
  2400. #if defined(_X86_)
  2401. FORCEINLINE
  2402. ULONG
  2403. KeFindFirstSetRightMember (
  2404. ULONG Set
  2405. )
  2406. {
  2407. __asm {
  2408. bsf eax, Set
  2409. }
  2410. }
  2411. #else
  2412. #define KeFindFirstSetRightMember(Set) \
  2413. ((Set & 0xFF) ? KiFindFirstSetRight[Set & 0xFF] : \
  2414. ((Set & 0xFF00) ? KiFindFirstSetRight[(Set >> 8) & 0xFF] + 8 : \
  2415. ((Set & 0xFF0000) ? KiFindFirstSetRight[(Set >> 16) & 0xFF] + 16 : \
  2416. KiFindFirstSetRight[Set >> 24] + 24 )))
  2417. #endif
  2418. //
  2419. // TB Flush routines
  2420. //
  2421. extern volatile LONG KiTbFlushTimeStamp;
  2422. NTKERNELAPI
  2423. VOID
  2424. KeFlushEntireTb (
  2425. IN BOOLEAN Invalid,
  2426. IN BOOLEAN AllProcessors
  2427. );
  2428. #if (defined(_M_IX86) || defined(_M_AMD64)) && defined(NT_UP) && \
  2429. !defined(_NTDRIVER_) && !defined(_NTDDK_) && !defined(_NTIFS_) && !defined(_NTHAL_)
  2430. FORCEINLINE
  2431. VOID
  2432. KeFlushProcessTb (
  2433. IN BOOLEAN AllProcessors
  2434. )
  2435. {
  2436. UNREFERENCED_PARAMETER(AllProcessors);
  2437. KiFlushProcessTb();
  2438. return;
  2439. }
  2440. FORCEINLINE
  2441. VOID
  2442. FASTCALL
  2443. KeFlushSingleTb (
  2444. IN PVOID Virtual,
  2445. IN BOOLEAN AllProcesors
  2446. )
  2447. {
  2448. UNREFERENCED_PARAMETER (AllProcesors);
  2449. #if _MSC_FULL_VER >= 13008806
  2450. #if defined(_M_AMD64)
  2451. InvalidatePage(Virtual);
  2452. #else
  2453. __asm {
  2454. mov eax, Virtual
  2455. invlpg [eax]
  2456. }
  2457. #endif
  2458. #else
  2459. KiFlushSingleTb(Virtual);
  2460. #endif
  2461. return;
  2462. }
  2463. #define KeFlushMultipleTb(Number, Virtual, AllProcessors) \
  2464. { \
  2465. ULONG _Index_; \
  2466. PVOID _VA_; \
  2467. \
  2468. for (_Index_ = 0; _Index_ < (Number); _Index_ += 1) { \
  2469. _VA_ = (Virtual)[_Index_]; \
  2470. KiFlushSingleTb(_VA_); \
  2471. } \
  2472. }
  2473. #else
  2474. #if defined(_AMD64_) || defined(_X86_)
  2475. VOID
  2476. KeFlushProcessTb (
  2477. IN BOOLEAN AllProcessors
  2478. );
  2479. #else
  2480. #define KeFlushProcessTb(all) KeFlushEntireTb(FALSE, (all))
  2481. #endif
  2482. VOID
  2483. KeFlushMultipleTb (
  2484. IN ULONG Number,
  2485. IN PVOID *Virtual,
  2486. IN BOOLEAN AllProcesors
  2487. );
  2488. VOID
  2489. FASTCALL
  2490. KeFlushSingleTb (
  2491. IN PVOID Virtual,
  2492. IN BOOLEAN AllProcesors
  2493. );
  2494. #endif
  2495. #if defined(_IA64_)
  2496. VOID
  2497. KeFlushMultipleTb64 (
  2498. IN ULONG Number,
  2499. IN PULONG_PTR Virtual,
  2500. IN BOOLEAN AllProcesors
  2501. );
  2502. HARDWARE_PTE
  2503. KeFlushSingleTb64 (
  2504. IN ULONG_PTR Virtual,
  2505. IN BOOLEAN AllProcesors
  2506. );
  2507. #endif
  2508. // begin_nthal
  2509. BOOLEAN
  2510. KiIpiServiceRoutine (
  2511. IN struct _KTRAP_FRAME *TrapFrame,
  2512. IN struct _KEXCEPTION_FRAME *ExceptionFrame
  2513. );
  2514. // end_nthal
  2515. BOOLEAN
  2516. KeFreezeExecution (
  2517. IN PKTRAP_FRAME TrapFrame,
  2518. IN PKEXCEPTION_FRAME ExceptionFrame
  2519. );
  2520. KCONTINUE_STATUS
  2521. KeSwitchFrozenProcessor (
  2522. IN ULONG ProcessorNumber
  2523. );
  2524. VOID
  2525. KeGetNonVolatileContextPointers (
  2526. IN PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext
  2527. );
  2528. // begin_ntddk
  2529. #if defined(_AMD64_) || defined(_X86_)
  2530. NTKERNELAPI
  2531. BOOLEAN
  2532. KeInvalidateAllCaches (
  2533. VOID
  2534. );
  2535. #endif
  2536. // end_ntddk
  2537. #define DMA_READ_DCACHE_INVALIDATE 0x1 // nthal
  2538. #define DMA_READ_ICACHE_INVALIDATE 0x2 // nthal
  2539. #define DMA_WRITE_DCACHE_SNOOP 0x4 // nthal
  2540. // nthal
  2541. NTKERNELAPI // nthal
  2542. VOID // nthal
  2543. KeSetDmaIoCoherency ( // nthal
  2544. IN ULONG Attributes // nthal
  2545. ); // nthal
  2546. // nthal
  2547. #if defined(_AMD64_) || defined(_X86_)
  2548. NTKERNELAPI // nthal
  2549. VOID // nthal
  2550. KeSetProfileIrql ( // nthal
  2551. IN KIRQL ProfileIrql // nthal
  2552. ); // nthal
  2553. // nthal
  2554. #endif
  2555. #if defined(_IA64_)
  2556. ULONG
  2557. KeReadMbTimeStamp (
  2558. VOID
  2559. );
  2560. VOID
  2561. KeSynchronizeMemoryAccess (
  2562. VOID
  2563. );
  2564. #endif
  2565. //
  2566. // Interlocked read TB flush entire timestamp.
  2567. //
  2568. FORCEINLINE
  2569. ULONG
  2570. KeReadTbFlushTimeStamp (
  2571. VOID
  2572. )
  2573. {
  2574. #if defined(NT_UP)
  2575. return KiTbFlushTimeStamp;
  2576. #else
  2577. LONG Value;
  2578. //
  2579. // While the TB flush time stamp counter is being updated the high
  2580. // order bit of the time stamp value is set. Otherwise, the bit is
  2581. // clear.
  2582. //
  2583. KeMemoryBarrier();
  2584. do {
  2585. } while ((Value = KiTbFlushTimeStamp) < 0);
  2586. return Value;
  2587. #endif
  2588. }
  2589. VOID
  2590. KeSetSystemTime (
  2591. IN PLARGE_INTEGER NewTime,
  2592. OUT PLARGE_INTEGER OldTime,
  2593. IN BOOLEAN AdjustInterruptTime,
  2594. IN PLARGE_INTEGER HalTimeToSet OPTIONAL
  2595. );
  2596. #define SYSTEM_SERVICE_INDEX 0
  2597. // begin_ntosp
  2598. #define WIN32K_SERVICE_INDEX 1
  2599. #define IIS_SERVICE_INDEX 2
  2600. // end_ntosp
  2601. // begin_ntosp
  2602. NTKERNELAPI
  2603. BOOLEAN
  2604. KeAddSystemServiceTable(
  2605. IN PULONG_PTR Base,
  2606. IN PULONG Count OPTIONAL,
  2607. IN ULONG Limit,
  2608. IN PUCHAR Number,
  2609. IN ULONG Index
  2610. );
  2611. NTKERNELAPI
  2612. BOOLEAN
  2613. KeRemoveSystemServiceTable(
  2614. IN ULONG Index
  2615. );
  2616. // end_ntosp
  2617. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  2618. #if !defined(_AMD64_)
  2619. NTKERNELAPI
  2620. ULONGLONG
  2621. KeQueryInterruptTime (
  2622. VOID
  2623. );
  2624. NTKERNELAPI
  2625. VOID
  2626. KeQuerySystemTime (
  2627. OUT PLARGE_INTEGER CurrentTime
  2628. );
  2629. #endif
  2630. NTKERNELAPI
  2631. ULONG
  2632. KeQueryTimeIncrement (
  2633. VOID
  2634. );
  2635. NTKERNELAPI
  2636. ULONG
  2637. KeGetRecommendedSharedDataAlignment (
  2638. VOID
  2639. );
  2640. // end_wdm
  2641. NTKERNELAPI
  2642. KAFFINITY
  2643. KeQueryActiveProcessors (
  2644. VOID
  2645. );
  2646. // end_ntddk end_nthal end_ntifs end_ntosp
  2647. NTSTATUS
  2648. KeQueryLogicalProcessorInformation(
  2649. OUT PVOID SystemInformation,
  2650. IN ULONG SystemInformationLength,
  2651. OUT PULONG ReturnLength
  2652. );
  2653. PKPRCB
  2654. KeGetPrcb(
  2655. IN ULONG ProcessorNumber
  2656. );
  2657. BOOLEAN
  2658. KeAdjustInterruptTime (
  2659. IN LONGLONG TimeDelta
  2660. );
  2661. // begin_nthal
  2662. NTKERNELAPI
  2663. VOID
  2664. KeSetTimeIncrement (
  2665. IN ULONG MaximumIncrement,
  2666. IN ULONG MimimumIncrement
  2667. );
  2668. // end_nthal
  2669. VOID
  2670. KeThawExecution (
  2671. IN BOOLEAN Enable
  2672. );
  2673. // begin_nthal begin_ntosp
  2674. //
  2675. // Define the firmware routine types
  2676. //
  2677. typedef enum _FIRMWARE_REENTRY {
  2678. HalHaltRoutine,
  2679. HalPowerDownRoutine,
  2680. HalRestartRoutine,
  2681. HalRebootRoutine,
  2682. HalInteractiveModeRoutine,
  2683. HalMaximumRoutine
  2684. } FIRMWARE_REENTRY, *PFIRMWARE_REENTRY;
  2685. // end_nthal end_ntosp
  2686. VOID
  2687. KeStartAllProcessors (
  2688. VOID
  2689. );
  2690. //
  2691. // Balance set manager thread startup function.
  2692. //
  2693. VOID
  2694. KeBalanceSetManager (
  2695. IN PVOID Context
  2696. );
  2697. VOID
  2698. KeSwapProcessOrStack (
  2699. IN PVOID Context
  2700. );
  2701. //
  2702. // User mode callback.
  2703. //
  2704. // begin_ntosp
  2705. NTKERNELAPI
  2706. NTSTATUS
  2707. KeUserModeCallback (
  2708. IN ULONG ApiNumber,
  2709. IN PVOID InputBuffer,
  2710. IN ULONG InputLength,
  2711. OUT PVOID *OutputBuffer,
  2712. OUT PULONG OutputLength
  2713. );
  2714. // end_ntosp
  2715. #if defined(_IA64_)
  2716. PVOID
  2717. KeSwitchKernelStack (
  2718. IN PVOID StackBase,
  2719. IN PVOID StackLimit,
  2720. IN PVOID BStoreLimit
  2721. );
  2722. #else
  2723. PVOID
  2724. KeSwitchKernelStack (
  2725. IN PVOID StackBase,
  2726. IN PVOID StackLimit
  2727. );
  2728. #endif // defined(_IA64_)
  2729. NTSTATUS
  2730. KeRaiseUserException(
  2731. IN NTSTATUS ExceptionCode
  2732. );
  2733. // begin_nthal
  2734. //
  2735. // Find ARC configuration information function.
  2736. //
  2737. NTKERNELAPI
  2738. PCONFIGURATION_COMPONENT_DATA
  2739. KeFindConfigurationEntry (
  2740. IN PCONFIGURATION_COMPONENT_DATA Child,
  2741. IN CONFIGURATION_CLASS Class,
  2742. IN CONFIGURATION_TYPE Type,
  2743. IN PULONG Key OPTIONAL
  2744. );
  2745. NTKERNELAPI
  2746. PCONFIGURATION_COMPONENT_DATA
  2747. KeFindConfigurationNextEntry (
  2748. IN PCONFIGURATION_COMPONENT_DATA Child,
  2749. IN CONFIGURATION_CLASS Class,
  2750. IN CONFIGURATION_TYPE Type,
  2751. IN PULONG Key OPTIONAL,
  2752. IN PCONFIGURATION_COMPONENT_DATA *Resume
  2753. );
  2754. // end_nthal
  2755. //
  2756. // External references to public kernel data structures
  2757. //
  2758. extern KAFFINITY KeActiveProcessors;
  2759. extern LARGE_INTEGER KeBootTime;
  2760. extern ULONGLONG KeBootTimeBias;
  2761. extern ULONG KeThreadDpcEnable;
  2762. extern ULONG KeErrorMask;
  2763. extern ULONGLONG KeInterruptTimeBias;
  2764. extern LIST_ENTRY KeBugCheckCallbackListHead;
  2765. extern LIST_ENTRY KeBugCheckReasonCallbackListHead;
  2766. extern KSPIN_LOCK KeBugCheckCallbackLock;
  2767. extern PGDI_BATCHFLUSH_ROUTINE KeGdiFlushUserBatch;
  2768. extern PLOADER_PARAMETER_BLOCK KeLoaderBlock; // ntosp
  2769. extern ULONG KeMaximumIncrement;
  2770. extern ULONG KeMinimumIncrement;
  2771. extern NTSYSAPI CCHAR KeNumberProcessors; // nthal ntosp
  2772. extern UCHAR KeNumberNodes;
  2773. extern USHORT KeProcessorArchitecture;
  2774. extern USHORT KeProcessorLevel;
  2775. extern USHORT KeProcessorRevision;
  2776. extern ULONG KeFeatureBits;
  2777. extern KSPIN_LOCK KiDispatcherLock;
  2778. extern ULONG KiDPCTimeout;
  2779. extern PKPRCB KiProcessorBlock[];
  2780. extern ULONG KiSpinlockTimeout;
  2781. extern ULONG KiStackProtectTime;
  2782. extern KTHREAD_SWITCH_COUNTERS KeThreadSwitchCounters;
  2783. extern ULONG KeLargestCacheLine;
  2784. #if defined(_IA64_)
  2785. VOID KiNormalSystemCall(VOID);
  2786. //
  2787. // IA64 CPL CATCHER
  2788. //
  2789. extern PVOID KeCplCatcher;
  2790. #endif
  2791. #if !defined(NT_UP)
  2792. extern ULONG KeRegisteredProcessors;
  2793. extern ULONG KeLicensedProcessors;
  2794. extern UCHAR KeProcessNodeSeed;
  2795. #endif
  2796. extern PULONG KeServiceCountTable;
  2797. extern KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[NUMBER_SERVICE_TABLES];
  2798. extern KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[NUMBER_SERVICE_TABLES];
  2799. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  2800. #if defined(_IA64_)
  2801. extern volatile LARGE_INTEGER KeTickCount;
  2802. #elif defined(_X86_)
  2803. extern volatile KSYSTEM_TIME KeTickCount;
  2804. #endif
  2805. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  2806. // begin_nthal
  2807. extern PVOID KeUserApcDispatcher;
  2808. extern PVOID KeUserCallbackDispatcher;
  2809. extern PVOID KeUserExceptionDispatcher;
  2810. extern PVOID KeRaiseUserExceptionDispatcher;
  2811. extern ULONG KeTimeAdjustment;
  2812. extern ULONG KeTimeIncrement;
  2813. extern BOOLEAN KeTimeSynchronization;
  2814. // end_nthal
  2815. // begin_ntddk begin_wdm begin_nthal begin_ntifs begin_ntosp
  2816. typedef enum _MEMORY_CACHING_TYPE_ORIG {
  2817. MmFrameBufferCached = 2
  2818. } MEMORY_CACHING_TYPE_ORIG;
  2819. typedef enum _MEMORY_CACHING_TYPE {
  2820. MmNonCached = FALSE,
  2821. MmCached = TRUE,
  2822. MmWriteCombined = MmFrameBufferCached,
  2823. MmHardwareCoherentCached,
  2824. MmNonCachedUnordered, // IA64
  2825. MmUSWCCached,
  2826. MmMaximumCacheType
  2827. } MEMORY_CACHING_TYPE;
  2828. // end_ntddk end_wdm end_nthal end_ntifs end_ntosp
  2829. //
  2830. // Routine for setting memory type for physical address ranges.
  2831. //
  2832. #if defined(_X86_)
  2833. NTSTATUS
  2834. KeSetPhysicalCacheTypeRange (
  2835. IN PHYSICAL_ADDRESS PhysicalAddress,
  2836. IN ULONG NumberOfBytes,
  2837. IN MEMORY_CACHING_TYPE CacheType
  2838. );
  2839. #endif
  2840. //
  2841. // Routines for zeroing a physical page.
  2842. //
  2843. // These are defined as calls through a function pointer which is set to
  2844. // point at the optimal routine for this processor implementation.
  2845. //
  2846. #if defined(_X86_) || defined(_IA64_)
  2847. typedef
  2848. VOID
  2849. (FASTCALL *KE_ZERO_PAGE_ROUTINE)(
  2850. IN PVOID PageBase,
  2851. IN SIZE_T NumberOfBytes
  2852. );
  2853. extern KE_ZERO_PAGE_ROUTINE KeZeroPages;
  2854. extern KE_ZERO_PAGE_ROUTINE KeZeroPagesFromIdleThread;
  2855. #else
  2856. #define KeZeroPagesFromIdleThread KeZeroPages
  2857. VOID
  2858. KeZeroPages (
  2859. IN PVOID PageBase,
  2860. IN SIZE_T NumberOfBytes
  2861. );
  2862. #endif
  2863. #if defined(_IA64_)
  2864. VOID
  2865. KeEnableSessionSharing(
  2866. PREGION_MAP_INFO SessionMapInfo,
  2867. IN PFN_NUMBER SessionParentPage
  2868. );
  2869. VOID
  2870. KeDetachSessionSpace(
  2871. IN PREGION_MAP_INFO NullSessionMapInfo,
  2872. IN PFN_NUMBER SessionParentPage
  2873. );
  2874. VOID
  2875. KeAddSessionSpace(
  2876. IN PKPROCESS Process,
  2877. IN PREGION_MAP_INFO SessionMapInfo,
  2878. IN PFN_NUMBER SessionParentPage
  2879. );
  2880. VOID
  2881. KeAttachSessionSpace(
  2882. IN PREGION_MAP_INFO SessionMapInfo,
  2883. IN PFN_NUMBER SessionParentPage
  2884. );
  2885. VOID
  2886. KeDisableSessionSharing(
  2887. IN PREGION_MAP_INFO SessionMapInfo,
  2888. IN PFN_NUMBER SessionParentPage
  2889. );
  2890. NTSTATUS
  2891. KeFlushUserRseState (
  2892. IN PKTRAP_FRAME TrapFrame
  2893. );
  2894. VOID
  2895. KeSetLowPsrBit (
  2896. IN UCHAR BitPosition,
  2897. IN BOOLEAN Value
  2898. );
  2899. #endif
  2900. //
  2901. // Verifier functions
  2902. //
  2903. NTSTATUS
  2904. KevUtilAddressToFileHeader (
  2905. IN PVOID Address,
  2906. OUT UINT_PTR *OffsetIntoImage,
  2907. OUT PUNICODE_STRING *DriverName,
  2908. OUT BOOLEAN *InVerifierList
  2909. );
  2910. //
  2911. // Define guarded mutex structure.
  2912. //
  2913. typedef struct _KGUARDED_MUTEX {
  2914. LONG Count;
  2915. PKTHREAD Owner;
  2916. ULONG Contention;
  2917. KEVENT Event;
  2918. union {
  2919. struct {
  2920. SHORT KernelApcDisable;
  2921. SHORT SpecialApcDisable;
  2922. };
  2923. ULONG CombinedApcDisable;
  2924. };
  2925. } KGUARDED_MUTEX, *PKGUARDED_MUTEX;
  2926. #endif // _KE_