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 KGDT64_NULL 0x0000
62 #define KGDT64_R0_CODE 0x0010
63 #define KGDT64_R0_DATA 0x0018
64 #define KGDT64_R3_CMCODE 0x0020
65 #define KGDT64_R3_DATA 0x0028
66 #define KGDT64_R3_CODE 0x0030
67 #define KGDT64_SYS_TSS 0x0040
68 #define KGDT64_R3_CMTEB 0x0050
82 #define CR4_FXSR 0x200
83 #define CR4_XMMEXCPT 0x400
88 #define EFLAGS_CF 0x01L
89 #define EFLAGS_ZF 0x40L
90 #define EFLAGS_TF 0x100L
91 #define EFLAGS_INTERRUPT_MASK 0x200L
92 #define EFLAGS_DF 0x400L
93 #define EFLAGS_NESTED_TASK 0x4000L
94 #define EFLAGS_V86_MASK 0x20000
95 #define EFLAGS_ALIGN_CHECK 0x40000
96 #define EFLAGS_VIF 0x80000
97 #define EFLAGS_VIP 0x100000
98 #define EFLAGS_USER_SANITIZE 0x3F4DD7
99 #define EFLAG_SIGN 0x8000
100 #define EFLAG_ZERO 0x4000
108 #define IPI_PACKET_READY 8
109 #define IPI_SYNCH_REQUEST 16
114 #define PRCB_MAJOR_VERSION 1
115 #define PRCB_BUILD_DEBUG 1
116 #define PRCB_BUILD_UNIPROCESSOR 2
121 #define INITIAL_STALL_COUNT 100
126 #define IO_ACCESS_MAP_NONE 0
127 #define IOPM_OFFSET FIELD_OFFSET(KTSS, IoMaps[0].IoMap)
128 #define KiComputeIopmOffset(MapNumber) \
129 (MapNumber == IO_ACCESS_MAP_NONE) ? \
130 (USHORT)(sizeof(KTSS)) : \
131 (USHORT)(FIELD_OFFSET(KTSS, IoMaps[MapNumber-1].IoMap))
134 // Static Kernel-Mode Address start (use MM_KSEG0_BASE for actual)
136 #define KSEG0_BASE 0xfffff80000000000ULL
139 // Synchronization-level IRQL
142 #define SYNCH_LEVEL DISPATCH_LEVEL
144 #define SYNCH_LEVEL (IPI_LEVEL - 2)
148 // Trap Frame Definition
150 typedef struct _KTRAP_FRAME
159 UCHAR FaultIndicator
;
160 UCHAR ExceptionActive
;
183 UINT64 ContextRecord
;
184 UINT64 TimeStampCKCL
;
197 UINT64 LastBranchToRip
;
198 UINT64 LastBranchFromRip
;
199 UINT64 LastExceptionToRip
;
200 UINT64 LastExceptionFromRip
;
204 UINT64 LastBranchControl
;
220 UINT64 ExceptionFrame
;
221 UINT64 TimeStampKlog
;
234 } KTRAP_FRAME
, *PKTRAP_FRAME
;
237 // Defines the Callback Stack Layout for User Mode Callbacks
239 typedef struct _KCALLOUT_FRAME
241 ULONG64 InitialStack
;
243 ULONG64 CallbackStack
;
248 ULONG64 ReturnAddress
;
250 ULONG64 ResultLength
;
251 } KCALLOUT_FRAME
, *PKCALLOUT_FRAME
;
256 #ifndef _LDT_ENTRY_DEFINED
257 #define _LDT_ENTRY_DEFINED
258 typedef ULONG LDT_ENTRY
;
262 // GDT Entry Definition
264 typedef union _KGDTENTRY64
297 } KGDTENTRY64
, *PKGDTENTRY64
;
298 #define KGDTENTRY KGDTENTRY64
299 #define PKGDTENTRY PKGDTENTRY64
302 // IDT Entry Access Definition
304 typedef struct _KIDT_ACCESS
312 UCHAR SystemSegmentFlag
:1;
318 } KIDT_ACCESS
, *PKIDT_ACCESS
;
321 // IDT Entry Definition
323 typedef union _KIDTENTRY64
339 } KIDTENTRY64
, *PKIDTENTRY64
;
340 #define KIDTENTRY KIDTENTRY64
341 #define PKIDTENTRY PKIDTENTRY64
343 typedef struct _KDESCRIPTOR
348 } KDESCRIPTOR
, *PKDESCRIPTOR
;
350 #ifndef NTOS_MODE_USER
353 // Special Registers Structure (outside of CONTEXT)
355 typedef struct _KSPECIAL_REGISTERS
367 struct _KDESCRIPTOR Gdtr
;
368 struct _KDESCRIPTOR Idtr
;
373 UINT64 LastBranchToRip
;
374 UINT64 LastBranchFromRip
;
375 UINT64 LastExceptionToRip
;
376 UINT64 LastExceptionFromRip
;
383 UINT64 MsrSyscallMask
;
384 } KSPECIAL_REGISTERS
, *PKSPECIAL_REGISTERS
;
387 // Processor State Data
389 typedef struct _KPROCESSOR_STATE
391 KSPECIAL_REGISTERS SpecialRegisters
;
392 CONTEXT ContextFrame
;
393 } KPROCESSOR_STATE
, *PKPROCESSOR_STATE
;
395 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
396 typedef struct _GENERAL_LOOKASIDE_POOL
400 SLIST_HEADER ListHead
;
401 SINGLE_LIST_ENTRY SingleListHead
;
405 ULONG TotalAllocates
;
408 ULONG AllocateMisses
;
430 LIST_ENTRY ListEntry
;
431 ULONG LastTotalAllocates
;
434 ULONG LastAllocateMisses
;
435 ULONG LastAllocateHits
;
438 } GENERAL_LOOKASIDE_POOL
, *PGENERAL_LOOKASIDE_POOL
;
440 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
443 typedef struct _KREQUEST_PACKET
445 PVOID CurrentPacket
[3];
447 } KREQUEST_PACKET
, *PKREQUEST_PACKET
;
449 typedef struct _REQUEST_MAILBOX
451 INT64 RequestSummary
;
452 KREQUEST_PACKET RequestPacket
;
454 } REQUEST_MAILBOX
, *PREQUEST_MAILBOX
;
457 // Processor Region Control Block
460 typedef struct _KPRCB
463 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
469 UCHAR InterruptRequest
;
471 struct _KTHREAD
*CurrentThread
;
472 struct _KTHREAD
*NextThread
;
473 struct _KTHREAD
*IdleThread
;
474 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
484 KPROCESSOR_STATE ProcessorState
;
487 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
501 UINT64 HalReserved
[8];
506 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
507 UCHAR CoresPerPhysicalProcessor
;
508 UCHAR LogicalProcessorsPerCore
;
511 UCHAR LogicalProcessorsPerPhysicalProcessor
;
514 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
521 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
528 KSPIN_LOCK_QUEUE LockQueue
[LockQueueMaximumLock
]; // 2003: 33, vista:49
529 PP_LOOKASIDE_LIST PPLookasideList
[16];
530 GENERAL_LOOKASIDE_POOL PPNPagedLookasideList
[32];
531 GENERAL_LOOKASIDE_POOL PPPagedLookasideList
[32];
532 UINT64 PacketBarrier
;
533 SINGLE_LIST_ENTRY DeferredReadyListHead
;
534 LONG MmPageFaultCount
;
535 LONG MmCopyOnWriteCount
;
536 LONG MmTransitionCount
;
537 #if (NTDDI_VERSION < NTDDI_LONGHORN)
538 LONG MmCacheTransitionCount
;
540 LONG MmDemandZeroCount
;
541 LONG MmPageReadCount
;
542 LONG MmPageReadIoCount
;
543 #if (NTDDI_VERSION < NTDDI_LONGHORN)
544 LONG MmCacheReadCount
;
547 LONG MmDirtyPagesWriteCount
;
548 LONG MmDirtyWriteIoCount
;
549 LONG MmMappedPagesWriteCount
;
550 LONG MmMappedWriteIoCount
;
551 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
553 ULONG KeContextSwitches
;
554 ULONG CcFastReadNoWait
;
555 ULONG CcFastReadWait
;
556 ULONG CcFastReadNotPossible
;
557 ULONG CcCopyReadNoWait
;
558 ULONG CcCopyReadWait
;
559 ULONG CcCopyReadNoWaitMiss
;
560 LONG LookasideIrpFloat
;
562 LONG LookasideIrpFloat
;
565 LONG IoReadOperationCount
;
566 LONG IoWriteOperationCount
;
567 LONG IoOtherOperationCount
;
568 LARGE_INTEGER IoReadTransferCount
;
569 LARGE_INTEGER IoWriteTransferCount
;
570 LARGE_INTEGER IoOtherTransferCount
;
571 #if (NTDDI_VERSION < NTDDI_LONGHORN)
572 ULONG KeContextSwitches
;
578 REQUEST_MAILBOX RequestMailbox
[64];
579 UINT64 SenderSummary
;
581 KDPC_DATA DpcData
[2];
583 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
588 LONG MaximumDpcQueueDepth
;
589 ULONG DpcRequestRate
;
590 ULONG MinimumDpcRate
;
591 UCHAR DpcInterruptRequested
;
592 UCHAR DpcThreadRequested
;
593 UCHAR DpcRoutineActive
;
594 UCHAR DpcThreadActive
;
600 UCHAR ThreadDpcEnable
;
604 LONG DpcSetEventRequest
;
605 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
606 ULONG KeExceptionDispatchCount
;
612 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
616 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
618 UCHAR ClockCheckSlot
;
619 UCHAR ClockPollCycle
;
621 LONG DpcWatchdogPeriod
;
622 LONG DpcWatchdogCount
;
627 LIST_ENTRY WaitListHead
;
628 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
633 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
634 UINT64 PrcbPad71
[12];
636 LIST_ENTRY DispatcherReadyListHead
[32];
637 ULONG InterruptCount
;
642 ULONG AdjustDpcThreshold
;
644 UCHAR DebuggerSavedIRQL
;
646 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
656 struct _KNODE
*ParentNode
;
657 UINT64 MultiThreadProcessorSet
;
658 struct _KPRCB
*MultiThreadSetMaster
;
659 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
661 LONG MmSpinLockOrdering
;
664 ULONG NodeShiftedColor
;
665 ULONG SecondaryColorMask
;
668 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
670 ULONG CcFastMdlReadNoWait
;
671 ULONG CcFastMdlReadWait
;
672 ULONG CcFastMdlReadNotPossible
;
673 ULONG CcMapDataNoWait
;
675 ULONG CcPinMappedDataCount
;
676 ULONG CcPinReadNoWait
;
678 ULONG CcMdlReadNoWait
;
680 ULONG CcLazyWriteHotSpots
;
681 ULONG CcLazyWriteIos
;
682 ULONG CcLazyWritePages
;
685 ULONG CcLostDelayedWrites
;
686 ULONG CcFastReadResourceMiss
;
687 ULONG CcCopyReadWaitMiss
;
688 ULONG CcFastMdlReadResourceMiss
;
689 ULONG CcMapDataNoWaitMiss
;
690 ULONG CcMapDataWaitMiss
;
691 ULONG CcPinReadNoWaitMiss
;
692 ULONG CcPinReadWaitMiss
;
693 ULONG CcMdlReadNoWaitMiss
;
694 ULONG CcMdlReadWaitMiss
;
695 ULONG CcReadAheadIos
;
696 LONG MmCacheTransitionCount
;
697 LONG MmCacheReadCount
;
700 PROCESSOR_POWER_STATE PowerState
;
701 ULONG KeAlignmentFixupCount
;
702 UCHAR VendorString
[13];
705 LARGE_INTEGER UpdateSignature
;
707 KTIMER DpcWatchdogTimer
;
708 CACHE_DESCRIPTOR Cache
[5];
711 ULONG CachedResidentAvailable
;
715 SLIST_HEADER InterruptObjectPool
;
716 SLIST_HEADER HypercallPageList
;
717 PVOID HypercallPageVirtual
;
718 PVOID VirtualApicAssist
;
719 UINT64
* StatisticsPage
;
721 UINT64 CacheProcessorMask
[5];
722 UINT64 PackageProcessorSet
;
723 UINT64 CoreProcessorSet
;
729 ULONG NodeShiftedColor
;
730 ULONG SecondaryColorMask
;
732 ULONG CcFastReadNoWait
;
733 ULONG CcFastReadWait
;
734 ULONG CcFastReadNotPossible
;
735 ULONG CcCopyReadNoWait
;
736 ULONG CcCopyReadWait
;
737 ULONG CcCopyReadNoWaitMiss
;
738 ULONG KeAlignmentFixupCount
;
739 ULONG KeDcacheFlushCount
;
740 ULONG KeExceptionDispatchCount
;
741 ULONG KeFirstLevelTbFills
;
742 ULONG KeFloatingEmulationCount
;
743 ULONG KeIcacheFlushCount
;
744 ULONG KeSecondLevelTbFills
;
745 UCHAR VendorString
[13];
748 LARGE_INTEGER UpdateSignature
;
749 PROCESSOR_POWER_STATE PowerState
;
750 CACHE_DESCRIPTOR Cache
[5];
757 // Processor Control Region
759 typedef struct _KIPCR
766 union _KGDTENTRY64
*GdtBase
;
767 struct _KTSS64
*TssBase
;
770 struct _KPRCB
*CurrentPrcb
;
771 PKSPIN_LOCK_QUEUE LockArray
;
775 union _KIDTENTRY64
*IdtBase
;
778 UCHAR SecondLevelCacheAssociativity
;
779 UCHAR ObsoleteNumber
;
784 ULONG StallScaleFactor
;
786 ULONG KernelReserved
[15];
787 ULONG SecondLevelCacheSize
;
788 ULONG HalReserved
[16];
791 PVOID KdVersionBlock
; // 0x108
794 ULONG Fill2
[2]; // 0x178
798 ULONG ContextSwitches
;
806 typedef struct _KiIoAccessMap
808 UCHAR DirectionMap
[32];
814 typedef struct _KTSS64
816 /* 000 */ ULONG Reserved0
;
817 /* 004 */ UINT64 Rsp0
;
818 /* 00c */ UINT64 Rsp1
;
819 /* 014 */ UINT64 Rsp2
;
820 /* 01c */ UINT64 Ist
[8];
821 /* 05c */ UINT64 Reserved1
;
822 /* 064 */ USHORT Reserved2
;
823 /* 066 */ USHORT IoMapBase
;
827 #define PKTSS PKTSS64
832 typedef struct _KEXCEPTION_FRAME
851 UINT64 CallbackStack
;
864 } KEXCEPTION_FRAME
, *PKEXCEPTION_FRAME
;
867 // Inline function to get current KPRCB
871 KeGetCurrentPrcb(VOID
)
873 return (struct _KPRCB
*)__readgsqword(FIELD_OFFSET(KIPCR
, CurrentPrcb
));