1 /*++ NDK Version: 0098 2 3 Copyright (c) Alex Ionescu. All rights reserved. 4 5 Header Name: 6 7 kefuncs.h 8 9 Abstract: 10 11 Functions definitions for the Kernel services. 12 13 Author: 14 15 Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006 16 17 --*/ 18 19 #ifndef _KEFUNCS_H 20 #define _KEFUNCS_H 21 22 // 23 // Dependencies 24 // 25 #include <umtypes.h> 26 #include <ketypes.h> 27 #include <section_attribs.h> 28 29 #ifdef __cplusplus 30 extern "C" 31 { 32 #endif 33 34 #ifndef NTOS_MODE_USER 35 36 // 37 // Affinity helpers 38 // 39 FORCEINLINE KAFFINITY AFFINITY_MASK(ULONG Index) 40 { 41 ASSERT(Index < sizeof(KAFFINITY) * 8); 42 return (KAFFINITY)1 << Index; 43 } 44 45 FORCEINLINE BOOLEAN BitScanForwardAffinity(PULONG Index, KAFFINITY Mask) 46 { 47 #ifdef _WIN64 48 return BitScanForward64(Index, Mask); 49 #else 50 return BitScanForward(Index, Mask); 51 #endif 52 } 53 54 FORCEINLINE BOOLEAN BitScanReverseAffinity(PULONG Index, KAFFINITY Mask) 55 { 56 #ifdef _WIN64 57 return BitScanReverse64(Index, Mask); 58 #else 59 return BitScanReverse(Index, Mask); 60 #endif 61 } 62 63 FORCEINLINE BOOLEAN InterlockedBitTestAndSetAffinity(volatile KAFFINITY *Affinity, ULONG Index) 64 { 65 ASSERT(Index < sizeof(KAFFINITY) * 8); 66 #ifdef _WIN64 67 return InterlockedBitTestAndSet64((PLONG64)Affinity, Index); 68 #else 69 return InterlockedBitTestAndSet((PLONG)Affinity, Index); 70 #endif 71 } 72 73 FORCEINLINE BOOLEAN InterlockedBitTestAndResetAffinity(volatile KAFFINITY *Affinity, ULONG Index) 74 { 75 ASSERT(Index < sizeof(KAFFINITY) * 8); 76 #ifdef _WIN64 77 return InterlockedBitTestAndReset64((PLONG64)Affinity, Index); 78 #else 79 return InterlockedBitTestAndReset((PLONG)Affinity, Index); 80 #endif 81 } 82 83 // 84 // APC Functions 85 // 86 VOID 87 NTAPI 88 KeInitializeApc( 89 _In_ PKAPC Apc, 90 _In_ PKTHREAD Thread, 91 _In_ KAPC_ENVIRONMENT TargetEnvironment, 92 _In_ PKKERNEL_ROUTINE KernelRoutine, 93 _In_opt_ PKRUNDOWN_ROUTINE RundownRoutine, 94 _In_ PKNORMAL_ROUTINE NormalRoutine, 95 _In_ KPROCESSOR_MODE Mode, 96 _In_ PVOID Context 97 ); 98 99 BOOLEAN 100 NTAPI 101 KeInsertQueueApc( 102 _In_ PKAPC Apc, 103 _In_ PVOID SystemArgument1, 104 _In_ PVOID SystemArgument2, 105 _In_ KPRIORITY PriorityBoost 106 ); 107 108 VOID 109 NTAPI 110 KiDeliverApc( 111 _In_ KPROCESSOR_MODE PreviousMode, 112 #ifdef _M_AMD64 113 _In_ PKEXCEPTION_FRAME ExceptionFrame, 114 #else 115 _Reserved_ PKEXCEPTION_FRAME ExceptionFrame, 116 #endif 117 _In_ PKTRAP_FRAME TrapFrame 118 ); 119 120 // 121 // Process/Thread Functions 122 // 123 VOID 124 NTAPI 125 KeTerminateThread( 126 _In_ KPRIORITY Increment 127 ); 128 129 BOOLEAN 130 NTAPI 131 KeIsAttachedProcess( 132 VOID 133 ); 134 135 VOID 136 NTAPI 137 KeSetEventBoostPriority( 138 _In_ PKEVENT Event, 139 _In_opt_ PKTHREAD *Thread 140 ); 141 142 KAFFINITY 143 NTAPI 144 KeSetAffinityThread( 145 _Inout_ PKTHREAD Thread, 146 _In_ KAFFINITY Affinity 147 ); 148 149 PKPROCESS 150 NTAPI 151 KeGetCurrentProcess( 152 VOID 153 ); 154 155 BOOLEAN 156 NTAPI 157 KeAddSystemServiceTable( 158 _In_ PULONG_PTR Base, 159 _In_opt_ PULONG Count, 160 _In_ ULONG Limit, 161 _In_ PUCHAR Number, 162 _In_ ULONG Index 163 ); 164 165 // 166 // Spinlock Functions 167 // 168 VOID 169 FASTCALL 170 KiAcquireSpinLock( 171 _Inout_ PKSPIN_LOCK SpinLock 172 ); 173 174 VOID 175 FASTCALL 176 KiReleaseSpinLock( 177 _Inout_ PKSPIN_LOCK SpinLock 178 ); 179 180 KIRQL 181 FASTCALL 182 KeAcquireQueuedSpinLockRaiseToSynch( 183 _In_ KSPIN_LOCK_QUEUE_NUMBER LockNumber 184 ); 185 186 BOOLEAN 187 FASTCALL 188 KeTryToAcquireQueuedSpinLockRaiseToSynch( 189 _In_ KSPIN_LOCK_QUEUE_NUMBER LockNumber, 190 _In_ PKIRQL OldIrql 191 ); 192 193 VOID 194 FASTCALL 195 KeAcquireInStackQueuedSpinLockRaiseToSynch( 196 _In_ PKSPIN_LOCK SpinLock, 197 _In_ PKLOCK_QUEUE_HANDLE LockHandle 198 ); 199 200 201 // 202 // Interrupt Functions 203 // 204 VOID 205 NTAPI 206 KeInitializeInterrupt( 207 _Out_ PKINTERRUPT InterruptObject, 208 _In_ PKSERVICE_ROUTINE ServiceRoutine, 209 _In_ PVOID ServiceContext, 210 _In_ PKSPIN_LOCK SpinLock, 211 _In_ ULONG Vector, 212 _In_ KIRQL Irql, 213 _In_ KIRQL SynchronizeIrql, 214 _In_ KINTERRUPT_MODE InterruptMode, 215 _In_ BOOLEAN ShareVector, 216 _In_ CHAR ProcessorNumber, 217 _In_ BOOLEAN FloatingSave 218 ); 219 220 BOOLEAN 221 NTAPI 222 KeConnectInterrupt( 223 _Inout_ PKINTERRUPT InterruptObject 224 ); 225 226 BOOLEAN 227 NTAPI 228 KeDisconnectInterrupt( 229 _Inout_ PKINTERRUPT InterruptObject 230 ); 231 232 VOID 233 NTAPI 234 KiDispatchInterrupt( 235 VOID 236 ); 237 238 VOID 239 NTAPI 240 KiCoprocessorError( 241 VOID 242 ); 243 244 DECLSPEC_NORETURN 245 VOID 246 __cdecl 247 KiUnexpectedInterrupt( 248 VOID 249 ); 250 251 VOID 252 NTAPI 253 KeEnterKernelDebugger( 254 VOID 255 ); 256 257 BOOLEAN 258 NTAPI 259 KeIsExecutingDpc( 260 VOID 261 ); 262 263 BOOLEAN 264 NTAPI 265 KiIpiServiceRoutine( 266 _In_ PKTRAP_FRAME TrapFrame, 267 #ifdef _M_AMD64 268 _In_ PKEXCEPTION_FRAME ExceptionFrame 269 #else 270 _Reserved_ PKEXCEPTION_FRAME ExceptionFrame 271 #endif 272 ); 273 274 // 275 // Generic DPC Routines 276 // 277 VOID 278 NTAPI 279 KeGenericCallDpc( 280 _In_ PKDEFERRED_ROUTINE Routine, 281 _In_ PVOID Context 282 ); 283 284 VOID 285 NTAPI 286 KeSignalCallDpcDone( 287 _In_ PVOID SystemArgument1 288 ); 289 290 BOOLEAN 291 NTAPI 292 KeSignalCallDpcSynchronize( 293 _In_ PVOID SystemArgument2 294 ); 295 296 // 297 // ARC Configuration Functions. Only enabled if you have ARC Support 298 // 299 #ifdef _ARC_ 300 PCONFIGURATION_COMPONENT_DATA 301 NTAPI 302 KeFindConfigurationNextEntry( 303 _In_ PCONFIGURATION_COMPONENT_DATA Child, 304 _In_ CONFIGURATION_CLASS Class, 305 _In_ CONFIGURATION_TYPE Type, 306 _In_opt_ PULONG ComponentKey, 307 _In_ PCONFIGURATION_COMPONENT_DATA *NextLink 308 ); 309 310 PCONFIGURATION_COMPONENT_DATA 311 NTAPI 312 KeFindConfigurationEntry( 313 _In_ PCONFIGURATION_COMPONENT_DATA Child, 314 _In_ CONFIGURATION_CLASS Class, 315 _In_ CONFIGURATION_TYPE Type, 316 _In_opt_ PULONG ComponentKey 317 ); 318 #endif 319 320 // 321 // Low-level Hardware/CPU Control Functions 322 // 323 VOID 324 NTAPI 325 KeFlushEntireTb( 326 _In_ BOOLEAN Invalid, 327 _In_ BOOLEAN AllProcessors 328 ); 329 330 VOID 331 NTAPI 332 KeSetDmaIoCoherency( 333 _In_ ULONG Coherency 334 ); 335 336 VOID 337 KeSetGdtSelector( 338 _In_ ULONG Entry, 339 _In_ ULONG Value1, 340 _In_ ULONG Value2 341 ); 342 343 VOID 344 NTAPI 345 KeProfileInterrupt( 346 _In_ PKTRAP_FRAME TrapFrame 347 ); 348 349 VOID 350 NTAPI 351 KeProfileInterruptWithSource( 352 _In_ PKTRAP_FRAME TrapFrame, 353 _In_ KPROFILE_SOURCE Source 354 ); 355 356 VOID 357 NTAPI 358 KeSetProfileIrql( 359 _In_ KIRQL ProfileIrql 360 ); 361 362 VOID 363 NTAPI 364 KeSetTimeIncrement( 365 _In_ ULONG MaxIncrement, 366 _In_ ULONG MinIncrement 367 ); 368 369 NTSTATUS 370 NTAPI 371 Ke386CallBios( 372 _In_ ULONG BiosCommand, 373 _Inout_ PCONTEXT BiosArguments 374 ); 375 376 // 377 // Misc. Functions 378 // 379 NTSTATUS 380 NTAPI 381 KeUserModeCallback( 382 _In_ ULONG FunctionID, 383 _In_reads_opt_(InputLength) PVOID InputBuffer, 384 _In_ ULONG InputLength, 385 _Outptr_result_buffer_(*OutputLength) PVOID *OutputBuffer, 386 _Out_ PULONG OutputLength 387 ); 388 389 NTSTATUS 390 NTAPI 391 KeRaiseUserException( 392 _In_ NTSTATUS ExceptionCode 393 ); 394 395 #endif 396 397 // 398 // Native Calls 399 // 400 NTSYSCALLAPI 401 NTSTATUS 402 NTAPI 403 NtContinue( 404 _In_ PCONTEXT Context, 405 _In_ BOOLEAN TestAlert 406 ); 407 408 NTSYSCALLAPI 409 NTSTATUS 410 NTAPI 411 NtCallbackReturn( 412 _In_ PVOID Result, 413 _In_ ULONG ResultLength, 414 _In_ NTSTATUS Status 415 ); 416 417 NTSYSCALLAPI 418 NTSTATUS 419 NTAPI 420 NtCreateProfile( 421 _Out_ PHANDLE ProfileHandle, 422 _In_ HANDLE ProcessHandle, 423 _In_ PVOID ImageBase, 424 _In_ SIZE_T ImageSize, 425 _In_ ULONG Granularity, 426 _Out_ PVOID Buffer, 427 _In_ ULONG ProfilingSize, 428 _In_ KPROFILE_SOURCE Source, 429 _In_ KAFFINITY ProcessorMask 430 ); 431 432 NTSYSCALLAPI 433 NTSTATUS 434 NTAPI 435 NtCreateProfileEx( 436 _Out_ PHANDLE ProfileHandle, 437 _In_ HANDLE ProcessHandle, 438 _In_ PVOID ImageBase, 439 _In_ SIZE_T ImageSize, 440 _In_ ULONG Granularity, 441 _Out_ PVOID Buffer, 442 _In_ ULONG ProfilingSize, 443 _In_ KPROFILE_SOURCE Source, 444 _In_ USHORT GroupCount, 445 _In_reads_(GroupCount) PGROUP_AFFINITY Affinity 446 ); 447 448 NTSYSCALLAPI 449 NTSTATUS 450 NTAPI 451 NtDelayExecution( 452 _In_ BOOLEAN Alertable, 453 _In_ LARGE_INTEGER *Interval 454 ); 455 456 ULONG 457 NTAPI 458 NtGetCurrentProcessorNumber( 459 VOID 460 ); 461 462 NTSYSCALLAPI 463 NTSTATUS 464 NTAPI 465 NtGetContextThread( 466 _In_ HANDLE ThreadHandle, 467 _Out_ PCONTEXT Context 468 ); 469 470 NTSYSCALLAPI 471 ULONG 472 NTAPI 473 NtGetTickCount( 474 VOID 475 ); 476 477 NTSYSCALLAPI 478 NTSTATUS 479 NTAPI 480 NtQueryIntervalProfile( 481 _In_ KPROFILE_SOURCE ProfileSource, 482 _Out_ PULONG Interval 483 ); 484 485 NTSYSCALLAPI 486 NTSTATUS 487 NTAPI 488 NtQueryPerformanceCounter( 489 _Out_ PLARGE_INTEGER Counter, 490 _Out_opt_ PLARGE_INTEGER Frequency 491 ); 492 493 NTSYSCALLAPI 494 NTSTATUS 495 NTAPI 496 NtQuerySystemTime( 497 _Out_ PLARGE_INTEGER CurrentTime 498 ); 499 500 NTSYSCALLAPI 501 NTSTATUS 502 NTAPI 503 NtQueryTimerResolution( 504 _Out_ PULONG MinimumResolution, 505 _Out_ PULONG MaximumResolution, 506 _Out_ PULONG ActualResolution 507 ); 508 509 NTSYSCALLAPI 510 NTSTATUS 511 NTAPI 512 NtQueueApcThread( 513 _In_ HANDLE ThreadHandle, 514 _In_ PKNORMAL_ROUTINE ApcRoutine, 515 _In_opt_ PVOID NormalContext, 516 _In_opt_ PVOID SystemArgument1, 517 _In_opt_ PVOID SystemArgument2 518 ); 519 520 NTSYSCALLAPI 521 NTSTATUS 522 NTAPI 523 NtQueueApcThreadEx( 524 _In_ HANDLE ThreadHandle, 525 _In_opt_ HANDLE UserApcReserveHandle, 526 _In_ PKNORMAL_ROUTINE ApcRoutine, 527 _In_opt_ PVOID NormalContext, 528 _In_opt_ PVOID SystemArgument1, 529 _In_opt_ PVOID SystemArgument2 530 ); 531 532 NTSYSCALLAPI 533 NTSTATUS 534 NTAPI 535 NtRaiseException( 536 _In_ PEXCEPTION_RECORD ExceptionRecord, 537 _In_ PCONTEXT Context, 538 _In_ BOOLEAN SearchFrames 539 ); 540 541 NTSYSCALLAPI 542 NTSTATUS 543 NTAPI 544 NtSetContextThread( 545 _In_ HANDLE ThreadHandle, 546 _In_ PCONTEXT Context 547 ); 548 549 NTSYSCALLAPI 550 NTSTATUS 551 NTAPI 552 NtSetIntervalProfile( 553 _In_ ULONG Interval, 554 _In_ KPROFILE_SOURCE ClockSource 555 ); 556 557 NTSYSCALLAPI 558 NTSTATUS 559 NTAPI 560 NtSetLdtEntries( 561 _In_ ULONG Selector1, 562 _In_ LDT_ENTRY LdtEntry1, 563 _In_ ULONG Selector2, 564 _In_ LDT_ENTRY LdtEntry2 565 ); 566 567 NTSYSCALLAPI 568 NTSTATUS 569 NTAPI 570 NtSetSystemTime( 571 _In_ PLARGE_INTEGER SystemTime, 572 _In_opt_ PLARGE_INTEGER NewSystemTime 573 ); 574 575 NTSYSCALLAPI 576 NTSTATUS 577 NTAPI 578 NtSetTimerResolution( 579 _In_ ULONG RequestedResolution, 580 _In_ BOOLEAN SetOrUnset, 581 _Out_ PULONG ActualResolution 582 ); 583 584 NTSYSCALLAPI 585 NTSTATUS 586 NTAPI 587 NtStartProfile( 588 _In_ HANDLE ProfileHandle 589 ); 590 591 NTSYSCALLAPI 592 NTSTATUS 593 NTAPI 594 NtStopProfile( 595 _In_ HANDLE ProfileHandle 596 ); 597 598 NTSYSCALLAPI 599 NTSTATUS 600 NTAPI 601 NtTestAlert( 602 VOID 603 ); 604 605 NTSYSCALLAPI 606 NTSTATUS 607 NTAPI 608 NtVdmControl( 609 _In_ ULONG ControlCode, 610 _In_ PVOID ControlData 611 ); 612 613 NTSYSCALLAPI 614 NTSTATUS 615 NTAPI 616 NtW32Call( 617 _In_ ULONG RoutineIndex, 618 _In_ PVOID Argument, 619 _In_ ULONG ArgumentLength, 620 _Out_opt_ PVOID* Result, 621 _Out_opt_ PULONG ResultLength 622 ); 623 624 NTSYSCALLAPI 625 NTSTATUS 626 NTAPI 627 NtYieldExecution( 628 VOID 629 ); 630 631 NTSYSAPI 632 NTSTATUS 633 NTAPI 634 ZwContinue( 635 _In_ PCONTEXT Context, 636 _In_ BOOLEAN TestAlert 637 ); 638 639 NTSYSAPI 640 NTSTATUS 641 NTAPI 642 ZwCallbackReturn( 643 _In_ PVOID Result, 644 _In_ ULONG ResultLength, 645 _In_ NTSTATUS Status 646 ); 647 648 NTSYSAPI 649 NTSTATUS 650 NTAPI 651 ZwCreateProfile( 652 _Out_ PHANDLE ProfileHandle, 653 _In_ HANDLE ProcessHandle, 654 _In_ PVOID ImageBase, 655 _In_ ULONG ImageSize, 656 _In_ ULONG Granularity, 657 _Out_ PVOID Buffer, 658 _In_ ULONG ProfilingSize, 659 _In_ KPROFILE_SOURCE Source, 660 _In_ KAFFINITY ProcessorMask 661 ); 662 663 NTSYSAPI 664 NTSTATUS 665 NTAPI 666 ZwDelayExecution( 667 _In_ BOOLEAN Alertable, 668 _In_ LARGE_INTEGER *Interval 669 ); 670 671 NTSYSAPI 672 NTSTATUS 673 NTAPI 674 ZwFlushInstructionCache( 675 _In_ HANDLE ProcessHandle, 676 _In_ PVOID BaseAddress, 677 _In_ ULONG NumberOfBytesToFlush 678 ); 679 680 NTSYSAPI 681 NTSTATUS 682 NTAPI 683 ZwGetContextThread( 684 _In_ HANDLE ThreadHandle, 685 _Out_ PCONTEXT Context 686 ); 687 688 NTSYSAPI 689 ULONG 690 NTAPI 691 ZwGetTickCount( 692 VOID 693 ); 694 695 NTSYSAPI 696 NTSTATUS 697 NTAPI 698 ZwQueryIntervalProfile( 699 _In_ KPROFILE_SOURCE ProfileSource, 700 _Out_ PULONG Interval 701 ); 702 703 NTSYSAPI 704 NTSTATUS 705 NTAPI 706 ZwQueryPerformanceCounter( 707 _Out_ PLARGE_INTEGER Counter, 708 _Out_opt_ PLARGE_INTEGER Frequency 709 ); 710 711 NTSYSAPI 712 NTSTATUS 713 NTAPI 714 ZwQuerySystemTime( 715 _Out_ PLARGE_INTEGER CurrentTime 716 ); 717 718 NTSYSAPI 719 NTSTATUS 720 NTAPI 721 ZwQueryTimerResolution( 722 _Out_ PULONG MinimumResolution, 723 _Out_ PULONG MaximumResolution, 724 _Out_ PULONG ActualResolution 725 ); 726 727 NTSYSAPI 728 NTSTATUS 729 NTAPI 730 ZwQueueApcThread( 731 _In_ HANDLE ThreadHandle, 732 _In_ PKNORMAL_ROUTINE ApcRoutine, 733 _In_opt_ PVOID NormalContext, 734 _In_opt_ PVOID SystemArgument1, 735 _In_opt_ PVOID SystemArgument2 736 ); 737 738 NTSYSAPI 739 NTSTATUS 740 NTAPI 741 ZwRaiseException( 742 _In_ PEXCEPTION_RECORD ExceptionRecord, 743 _In_ PCONTEXT Context, 744 _In_ BOOLEAN SearchFrames 745 ); 746 747 NTSYSAPI 748 NTSTATUS 749 NTAPI 750 ZwSetContextThread( 751 _In_ HANDLE ThreadHandle, 752 _In_ PCONTEXT Context 753 ); 754 755 NTSYSAPI 756 NTSTATUS 757 NTAPI 758 ZwSetIntervalProfile( 759 _In_ ULONG Interval, 760 _In_ KPROFILE_SOURCE ClockSource 761 ); 762 763 NTSYSAPI 764 NTSTATUS 765 NTAPI 766 ZwSetLdtEntries( 767 _In_ ULONG Selector1, 768 _In_ LDT_ENTRY LdtEntry1, 769 _In_ ULONG Selector2, 770 _In_ LDT_ENTRY LdtEntry2 771 ); 772 773 NTSYSAPI 774 NTSTATUS 775 NTAPI 776 ZwSetSystemTime( 777 _In_ PLARGE_INTEGER SystemTime, 778 _In_opt_ PLARGE_INTEGER NewSystemTime 779 ); 780 781 NTSYSAPI 782 NTSTATUS 783 NTAPI 784 ZwSetTimerResolution( 785 _In_ ULONG RequestedResolution, 786 _In_ BOOLEAN SetOrUnset, 787 _Out_ PULONG ActualResolution 788 ); 789 790 NTSYSAPI 791 NTSTATUS 792 NTAPI 793 ZwStartProfile( 794 _In_ HANDLE ProfileHandle 795 ); 796 797 NTSYSAPI 798 NTSTATUS 799 NTAPI 800 ZwStopProfile( 801 _In_ HANDLE ProfileHandle 802 ); 803 804 NTSYSAPI 805 NTSTATUS 806 NTAPI 807 ZwTestAlert( 808 VOID 809 ); 810 811 NTSYSAPI 812 NTSTATUS 813 NTAPI 814 ZwVdmControl( 815 _In_ ULONG ControlCode, 816 _In_ PVOID ControlData 817 ); 818 819 NTSYSAPI 820 NTSTATUS 821 NTAPI 822 ZwW32Call( 823 _In_ ULONG RoutineIndex, 824 _In_ PVOID Argument, 825 _In_ ULONG ArgumentLength, 826 _Out_opt_ PVOID* Result, 827 _Out_opt_ PULONG ResultLength 828 ); 829 830 NTSYSAPI 831 NTSTATUS 832 NTAPI 833 ZwYieldExecution( 834 VOID 835 ); 836 #endif 837 838 #ifdef __cplusplus 839 } // extern "C" 840 #endif 841