3 Copyright (c) Alex Ionescu. All rights reserved.
4 Copyright (c) Timo Kreuzer. All rights reserved.
12 amd64 Type definitions for the Kernel services.
16 Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006
17 Timo Kreuzer (timo.kreuzer@reactos.org) - Updated - 14-Aug-2008
21 #ifndef _AMD64_KETYPES_H
22 #define _AMD64_KETYPES_H
29 // KPCR Access for non-IA64 builds
31 //#define K0IPCR ((ULONG_PTR)(KIP0PCRADDRESS))
32 //#define PCR ((volatile KPCR * const)K0IPCR)
33 #define PCR ((volatile KPCR * const)__readgsqword(FIELD_OFFSET(KPCR, Self)))
34 //#if defined(CONFIG_SMP) || defined(NT_BUILD)
36 //#define KeGetPcr() ((volatile KPCR * const)__readfsdword(0x1C))
42 #define MACHINE_TYPE_ISA 0x0000
43 #define MACHINE_TYPE_EISA 0x0001
44 #define MACHINE_TYPE_MCA 0x0002
47 // X86 80386 Segment Types
49 #define I386_TASK_GATE 0x5
51 #define I386_ACTIVE_TSS 0xB
52 #define I386_CALL_GATE 0xC
53 #define I386_INTERRUPT_GATE 0xE
54 #define I386_TRAP_GATE 0xF
59 #define RPL_MASK 0x0003
60 #define MODE_MASK 0x0001
61 #define KGDT_64_R0_CODE 0x0010
62 #define KGDT_64_R0_SS 0x0018
63 #define KGDT_64_DATA 0x0028 // 2b
64 #define KGDT_64_R3_CODE 0x0030 // 33
65 #define KGDT_TSS 0x0040
66 #define KGDT_32_R3_TEB 0x0050 // 53
79 #define CR4_FXSR 0x200
80 #define CR4_XMMEXCPT 0x400
85 #define EFLAGS_CF 0x01
86 #define EFLAGS_PF 0x04
87 #define EFLAGS_AF 0x10
88 #define EFLAGS_ZF 0x40
89 #define EFLAGS_SF 0x80
90 #define EFLAGS_TF 0x100
91 #define EFLAGS_INTERRUPT_MASK 0x200
92 #define EFLAGS_DF 0x400
93 #define EFLAGS_OF 0x800
94 #define EFLAGS_IOPL_MASK 0x3000
95 #define EFLAGS_NESTED_TASK 0x4000
96 #define EFLAGS_RF 0x10000
97 #define EFLAGS_VM 0x20000
98 #define EFLAGS_ALIGN_CHECK 0x40000
99 #define EFLAGS_VIF 0x80000
100 #define EFLAGS_VIP 0x100000
101 #define EFLAGS_ID 0x200000
102 #define EFLAGS_USER_SANITIZE 0x3F4DD7
110 #define IPI_PACKET_READY 8
111 #define IPI_SYNCH_REQUEST 16
116 #define PRCB_MAJOR_VERSION 1
117 #define PRCB_BUILD_DEBUG 1
118 #define PRCB_BUILD_UNIPROCESSOR 2
123 #define INITIAL_STALL_COUNT 100
128 #define IO_ACCESS_MAP_NONE 0
129 #define IOPM_OFFSET FIELD_OFFSET(KTSS, IoMaps[0].IoMap)
130 #define KiComputeIopmOffset(MapNumber) \
131 (MapNumber == IO_ACCESS_MAP_NONE) ? \
132 (USHORT)(sizeof(KTSS)) : \
133 (USHORT)(FIELD_OFFSET(KTSS, IoMaps[MapNumber-1].IoMap))
136 // Static Kernel-Mode Address start (use MM_KSEG0_BASE for actual)
138 #define KSEG0_BASE 0xfffff80000000000ULL
141 // Synchronization-level IRQL
144 #define SYNCH_LEVEL DISPATCH_LEVEL
146 #define SYNCH_LEVEL (IPI_LEVEL - 2)
150 // Trap Frame Definition
152 typedef struct _KTRAP_FRAME
161 UCHAR FaultIndicator
;
162 UCHAR ExceptionActive
;
185 UINT64 ContextRecord
;
186 UINT64 TimeStampCKCL
;
199 UINT64 LastBranchToRip
;
200 UINT64 LastBranchFromRip
;
201 UINT64 LastExceptionToRip
;
202 UINT64 LastExceptionFromRip
;
206 UINT64 LastBranchControl
;
222 UINT64 ExceptionFrame
;
223 UINT64 TimeStampKlog
;
236 } KTRAP_FRAME
, *PKTRAP_FRAME
;
239 // Defines the Callback Stack Layout for User Mode Callbacks
241 typedef struct _KCALLOUT_FRAME
243 ULONG64 InitialStack
;
245 ULONG64 CallbackStack
;
250 ULONG64 ReturnAddress
;
252 ULONG64 ResultLength
;
253 } KCALLOUT_FRAME
, *PKCALLOUT_FRAME
;
258 #ifndef _LDT_ENTRY_DEFINED
259 #define _LDT_ENTRY_DEFINED
260 typedef ULONG LDT_ENTRY
;
264 // GDT Entry Definition
266 typedef union _KGDTENTRY64
299 } KGDTENTRY64
, *PKGDTENTRY64
;
300 #define KGDTENTRY KGDTENTRY64
301 #define PKGDTENTRY PKGDTENTRY64
304 // IDT Entry Access Definition
306 typedef struct _KIDT_ACCESS
314 UCHAR SystemSegmentFlag
:1;
320 } KIDT_ACCESS
, *PKIDT_ACCESS
;
323 // IDT Entry Definition
325 typedef union _KIDTENTRY64
341 } KIDTENTRY64
, *PKIDTENTRY64
;
342 #define KIDTENTRY KIDTENTRY64
343 #define PKIDTENTRY PKIDTENTRY64
345 typedef struct _KDESCRIPTOR
350 } KDESCRIPTOR
, *PKDESCRIPTOR
;
352 #ifndef NTOS_MODE_USER
355 // Special Registers Structure (outside of CONTEXT)
357 typedef struct _KSPECIAL_REGISTERS
369 struct _KDESCRIPTOR Gdtr
;
370 struct _KDESCRIPTOR Idtr
;
375 UINT64 LastBranchToRip
;
376 UINT64 LastBranchFromRip
;
377 UINT64 LastExceptionToRip
;
378 UINT64 LastExceptionFromRip
;
385 UINT64 MsrSyscallMask
;
386 } KSPECIAL_REGISTERS
, *PKSPECIAL_REGISTERS
;
389 // Processor State Data
391 typedef struct _KPROCESSOR_STATE
393 KSPECIAL_REGISTERS SpecialRegisters
;
394 CONTEXT ContextFrame
;
395 } KPROCESSOR_STATE
, *PKPROCESSOR_STATE
;
397 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
398 typedef struct _GENERAL_LOOKASIDE_POOL
402 SLIST_HEADER ListHead
;
403 SINGLE_LIST_ENTRY SingleListHead
;
407 ULONG TotalAllocates
;
410 ULONG AllocateMisses
;
432 LIST_ENTRY ListEntry
;
433 ULONG LastTotalAllocates
;
436 ULONG LastAllocateMisses
;
437 ULONG LastAllocateHits
;
440 } GENERAL_LOOKASIDE_POOL
, *PGENERAL_LOOKASIDE_POOL
;
442 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
445 typedef struct _KREQUEST_PACKET
447 PVOID CurrentPacket
[3];
449 } KREQUEST_PACKET
, *PKREQUEST_PACKET
;
451 typedef struct _REQUEST_MAILBOX
453 INT64 RequestSummary
;
454 KREQUEST_PACKET RequestPacket
;
456 } REQUEST_MAILBOX
, *PREQUEST_MAILBOX
;
459 // Processor Region Control Block
462 typedef struct _KPRCB
465 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
471 UCHAR InterruptRequest
;
473 struct _KTHREAD
*CurrentThread
;
474 struct _KTHREAD
*NextThread
;
475 struct _KTHREAD
*IdleThread
;
476 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
486 KPROCESSOR_STATE ProcessorState
;
489 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
503 UINT64 HalReserved
[8];
508 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
509 UCHAR CoresPerPhysicalProcessor
;
510 UCHAR LogicalProcessorsPerCore
;
513 UCHAR LogicalProcessorsPerPhysicalProcessor
;
516 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
523 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
530 KSPIN_LOCK_QUEUE LockQueue
[LockQueueMaximumLock
]; // 2003: 33, vista:49
531 PP_LOOKASIDE_LIST PPLookasideList
[16];
532 GENERAL_LOOKASIDE_POOL PPNPagedLookasideList
[32];
533 GENERAL_LOOKASIDE_POOL PPPagedLookasideList
[32];
534 UINT64 PacketBarrier
;
535 SINGLE_LIST_ENTRY DeferredReadyListHead
;
536 LONG MmPageFaultCount
;
537 LONG MmCopyOnWriteCount
;
538 LONG MmTransitionCount
;
539 #if (NTDDI_VERSION < NTDDI_LONGHORN)
540 LONG MmCacheTransitionCount
;
542 LONG MmDemandZeroCount
;
543 LONG MmPageReadCount
;
544 LONG MmPageReadIoCount
;
545 #if (NTDDI_VERSION < NTDDI_LONGHORN)
546 LONG MmCacheReadCount
;
549 LONG MmDirtyPagesWriteCount
;
550 LONG MmDirtyWriteIoCount
;
551 LONG MmMappedPagesWriteCount
;
552 LONG MmMappedWriteIoCount
;
553 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
555 ULONG KeContextSwitches
;
556 ULONG CcFastReadNoWait
;
557 ULONG CcFastReadWait
;
558 ULONG CcFastReadNotPossible
;
559 ULONG CcCopyReadNoWait
;
560 ULONG CcCopyReadWait
;
561 ULONG CcCopyReadNoWaitMiss
;
562 LONG LookasideIrpFloat
;
564 LONG LookasideIrpFloat
;
567 LONG IoReadOperationCount
;
568 LONG IoWriteOperationCount
;
569 LONG IoOtherOperationCount
;
570 LARGE_INTEGER IoReadTransferCount
;
571 LARGE_INTEGER IoWriteTransferCount
;
572 LARGE_INTEGER IoOtherTransferCount
;
573 #if (NTDDI_VERSION < NTDDI_LONGHORN)
574 ULONG KeContextSwitches
;
580 REQUEST_MAILBOX RequestMailbox
[64];
581 UINT64 SenderSummary
;
583 KDPC_DATA DpcData
[2];
585 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
590 LONG MaximumDpcQueueDepth
;
591 ULONG DpcRequestRate
;
592 ULONG MinimumDpcRate
;
593 UCHAR DpcInterruptRequested
;
594 UCHAR DpcThreadRequested
;
595 UCHAR DpcRoutineActive
;
596 UCHAR DpcThreadActive
;
602 UCHAR ThreadDpcEnable
;
606 LONG DpcSetEventRequest
;
607 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
608 ULONG KeExceptionDispatchCount
;
614 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
618 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
620 UCHAR ClockCheckSlot
;
621 UCHAR ClockPollCycle
;
623 LONG DpcWatchdogPeriod
;
624 LONG DpcWatchdogCount
;
629 LIST_ENTRY WaitListHead
;
630 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
635 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
636 UINT64 PrcbPad71
[12];
638 LIST_ENTRY DispatcherReadyListHead
[32];
639 ULONG InterruptCount
;
644 ULONG AdjustDpcThreshold
;
646 UCHAR DebuggerSavedIRQL
;
648 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
658 struct _KNODE
*ParentNode
;
659 UINT64 MultiThreadProcessorSet
;
660 struct _KPRCB
*MultiThreadSetMaster
;
661 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
663 LONG MmSpinLockOrdering
;
666 ULONG NodeShiftedColor
;
667 ULONG SecondaryColorMask
;
670 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
672 ULONG CcFastMdlReadNoWait
;
673 ULONG CcFastMdlReadWait
;
674 ULONG CcFastMdlReadNotPossible
;
675 ULONG CcMapDataNoWait
;
677 ULONG CcPinMappedDataCount
;
678 ULONG CcPinReadNoWait
;
680 ULONG CcMdlReadNoWait
;
682 ULONG CcLazyWriteHotSpots
;
683 ULONG CcLazyWriteIos
;
684 ULONG CcLazyWritePages
;
687 ULONG CcLostDelayedWrites
;
688 ULONG CcFastReadResourceMiss
;
689 ULONG CcCopyReadWaitMiss
;
690 ULONG CcFastMdlReadResourceMiss
;
691 ULONG CcMapDataNoWaitMiss
;
692 ULONG CcMapDataWaitMiss
;
693 ULONG CcPinReadNoWaitMiss
;
694 ULONG CcPinReadWaitMiss
;
695 ULONG CcMdlReadNoWaitMiss
;
696 ULONG CcMdlReadWaitMiss
;
697 ULONG CcReadAheadIos
;
698 LONG MmCacheTransitionCount
;
699 LONG MmCacheReadCount
;
702 PROCESSOR_POWER_STATE PowerState
;
703 ULONG KeAlignmentFixupCount
;
704 UCHAR VendorString
[13];
707 LARGE_INTEGER UpdateSignature
;
709 KTIMER DpcWatchdogTimer
;
710 CACHE_DESCRIPTOR Cache
[5];
713 ULONG CachedResidentAvailable
;
717 SLIST_HEADER InterruptObjectPool
;
718 SLIST_HEADER HypercallPageList
;
719 PVOID HypercallPageVirtual
;
720 PVOID VirtualApicAssist
;
721 UINT64
* StatisticsPage
;
723 UINT64 CacheProcessorMask
[5];
724 UINT64 PackageProcessorSet
;
725 UINT64 CoreProcessorSet
;
731 ULONG NodeShiftedColor
;
732 ULONG SecondaryColorMask
;
734 ULONG CcFastReadNoWait
;
735 ULONG CcFastReadWait
;
736 ULONG CcFastReadNotPossible
;
737 ULONG CcCopyReadNoWait
;
738 ULONG CcCopyReadWait
;
739 ULONG CcCopyReadNoWaitMiss
;
740 ULONG KeAlignmentFixupCount
;
741 ULONG KeDcacheFlushCount
;
742 ULONG KeExceptionDispatchCount
;
743 ULONG KeFirstLevelTbFills
;
744 ULONG KeFloatingEmulationCount
;
745 ULONG KeIcacheFlushCount
;
746 ULONG KeSecondLevelTbFills
;
747 UCHAR VendorString
[13];
750 LARGE_INTEGER UpdateSignature
;
751 PROCESSOR_POWER_STATE PowerState
;
752 CACHE_DESCRIPTOR Cache
[5];
759 // Processor Control Region
761 typedef struct _KIPCR
768 union _KGDTENTRY64
*GdtBase
;
769 struct _KTSS64
*TssBase
;
772 struct _KPRCB
*CurrentPrcb
;
773 PKSPIN_LOCK_QUEUE LockArray
;
777 union _KIDTENTRY64
*IdtBase
;
780 UCHAR SecondLevelCacheAssociativity
;
781 UCHAR ObsoleteNumber
;
786 ULONG StallScaleFactor
;
788 ULONG KernelReserved
[15];
789 ULONG SecondLevelCacheSize
;
790 ULONG HalReserved
[16];
793 PVOID KdVersionBlock
; // 0x108
796 ULONG Fill2
[2]; // 0x178
800 ULONG ContextSwitches
;
808 typedef struct _KiIoAccessMap
810 UCHAR DirectionMap
[32];
816 typedef struct _KTSS64
818 /* 000 */ ULONG Reserved0
;
819 /* 004 */ UINT64 Rsp0
;
820 /* 00c */ UINT64 Rsp1
;
821 /* 014 */ UINT64 Rsp2
;
822 /* 01c */ UINT64 Ist
[8];
823 /* 05c */ UINT64 Reserved1
;
824 /* 064 */ USHORT Reserved2
;
825 /* 066 */ USHORT IoMapBase
;
829 #define PKTSS PKTSS64
834 typedef struct _KEXCEPTION_FRAME
853 UINT64 CallbackStack
;
866 } KEXCEPTION_FRAME
, *PKEXCEPTION_FRAME
;
869 // Inline function to get current KPRCB
873 KeGetCurrentPrcb(VOID
)
875 return (struct _KPRCB
*)__readgsqword(FIELD_OFFSET(KIPCR
, CurrentPrcb
));