Source code of Windows XP (NT5)
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.

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