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
80 #define CR4_FXSR 0x200
81 #define CR4_XMMEXCPT 0x400
86 #define EFLAGS_CF 0x01L
87 #define EFLAGS_ZF 0x40L
88 #define EFLAGS_TF 0x100L
89 #define EFLAGS_INTERRUPT_MASK 0x200L
90 #define EFLAGS_DF 0x400L
91 #define EFLAGS_NESTED_TASK 0x4000L
92 #define EFLAGS_V86_MASK 0x20000
93 #define EFLAGS_ALIGN_CHECK 0x40000
94 #define EFLAGS_VIF 0x80000
95 #define EFLAGS_VIP 0x100000
96 #define EFLAGS_USER_SANITIZE 0x3F4DD7
97 #define EFLAG_SIGN 0x8000
98 #define EFLAG_ZERO 0x4000
106 #define IPI_PACKET_READY 8
107 #define IPI_SYNCH_REQUEST 16
112 #define PRCB_MAJOR_VERSION 1
113 #define PRCB_BUILD_DEBUG 1
114 #define PRCB_BUILD_UNIPROCESSOR 2
119 #define INITIAL_STALL_COUNT 0x64
124 #define IO_ACCESS_MAP_NONE 0
125 #define IOPM_OFFSET FIELD_OFFSET(KTSS, IoMaps[0].IoMap)
126 #define KiComputeIopmOffset(MapNumber) \
127 (MapNumber == IO_ACCESS_MAP_NONE) ? \
128 (USHORT)(sizeof(KTSS)) : \
129 (USHORT)(FIELD_OFFSET(KTSS, IoMaps[MapNumber-1].IoMap))
132 // Static Kernel-Mode Address start (use MM_KSEG0_BASE for actual)
134 #define KSEG0_BASE 0xfffff80000000000ULL
137 // Synchronization-level IRQL
140 #define SYNCH_LEVEL DISPATCH_LEVEL
142 #define SYNCH_LEVEL (IPI_LEVEL - 2)
146 // Trap Frame Definition
148 typedef struct _KTRAP_FRAME
157 UCHAR FaultIndicator
;
158 UCHAR ExceptionActive
;
181 UINT64 ContextRecord
;
182 UINT64 TimeStampCKCL
;
195 UINT64 LastBranchToRip
;
196 UINT64 LastBranchFromRip
;
197 UINT64 LastExceptionToRip
;
198 UINT64 LastExceptionFromRip
;
202 UINT64 LastBranchControl
;
218 UINT64 ExceptionFrame
;
219 UINT64 TimeStampKlog
;
232 } KTRAP_FRAME
, *PKTRAP_FRAME
;
237 typedef ULONG LDT_ENTRY
;
240 // GDT Entry Definition
242 typedef union _KGDTENTRY64
275 } KGDTENTRY64
, *PKGDTENTRY64
;
276 #define KGDTENTRY KGDTENTRY64
277 #define PKGDTENTRY PKGDTENTRY64
280 // IDT Entry Access Definition
282 typedef struct _KIDT_ACCESS
290 UCHAR SystemSegmentFlag
:1;
296 } KIDT_ACCESS
, *PKIDT_ACCESS
;
299 // IDT Entry Definition
301 typedef union _KIDTENTRY64
317 } KIDTENTRY64
, *PKIDTENTRY64
;
318 #define KIDTENTRY KIDTENTRY64
319 #define PKIDTENTRY PKIDTENTRY64
321 typedef struct _KDESCRIPTOR
326 } KDESCRIPTOR
, *PKDESCRIPTOR
;
328 #ifndef NTOS_MODE_USER
331 // Special Registers Structure (outside of CONTEXT)
333 typedef struct _KSPECIAL_REGISTERS
345 struct _KDESCRIPTOR Gdtr
;
346 struct _KDESCRIPTOR Idtr
;
351 UINT64 LastBranchToRip
;
352 UINT64 LastBranchFromRip
;
353 UINT64 LastExceptionToRip
;
354 UINT64 LastExceptionFromRip
;
361 UINT64 MsrSyscallMask
;
362 } KSPECIAL_REGISTERS
, *PKSPECIAL_REGISTERS
;
365 // Processor State Data
367 typedef struct _KPROCESSOR_STATE
369 KSPECIAL_REGISTERS SpecialRegisters
;
370 CONTEXT ContextFrame
;
371 } KPROCESSOR_STATE
, *PKPROCESSOR_STATE
;
373 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
374 typedef struct _GENERAL_LOOKASIDE_POOL
378 SLIST_HEADER ListHead
;
379 SINGLE_LIST_ENTRY SingleListHead
;
383 ULONG TotalAllocates
;
386 ULONG AllocateMisses
;
408 LIST_ENTRY ListEntry
;
409 ULONG LastTotalAllocates
;
412 ULONG LastAllocateMisses
;
413 ULONG LastAllocateHits
;
416 } GENERAL_LOOKASIDE_POOL
, *PGENERAL_LOOKASIDE_POOL
;
418 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
421 typedef struct _KREQUEST_PACKET
423 PVOID CurrentPacket
[3];
425 } KREQUEST_PACKET
, *PKREQUEST_PACKET
;
427 typedef struct _REQUEST_MAILBOX
429 INT64 RequestSummary
;
430 KREQUEST_PACKET RequestPacket
;
432 } REQUEST_MAILBOX
, *PREQUEST_MAILBOX
;
435 // Processor Region Control Block
438 typedef struct _KPRCB
441 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
447 UCHAR InterruptRequest
;
449 struct _KTHREAD
*CurrentThread
;
450 struct _KTHREAD
*NextThread
;
451 struct _KTHREAD
*IdleThread
;
452 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
462 KPROCESSOR_STATE ProcessorState
;
465 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
479 UINT64 HalReserved
[8];
484 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
485 UCHAR CoresPerPhysicalProcessor
;
486 UCHAR LogicalProcessorsPerCore
;
489 UCHAR LogicalProcessorsPerPhysicalProcessor
;
492 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
499 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
506 KSPIN_LOCK_QUEUE LockQueue
[LockQueueMaximumLock
]; // 2003: 33, vista:49
507 PP_LOOKASIDE_LIST PPLookasideList
[16];
508 GENERAL_LOOKASIDE_POOL PPNPagedLookasideList
[32];
509 GENERAL_LOOKASIDE_POOL PPPagedLookasideList
[32];
510 UINT64 PacketBarrier
;
511 SINGLE_LIST_ENTRY DeferredReadyListHead
;
512 LONG MmPageFaultCount
;
513 LONG MmCopyOnWriteCount
;
514 LONG MmTransitionCount
;
515 #if (NTDDI_VERSION < NTDDI_LONGHORN)
516 LONG MmCacheTransitionCount
;
518 LONG MmDemandZeroCount
;
519 LONG MmPageReadCount
;
520 LONG MmPageReadIoCount
;
521 #if (NTDDI_VERSION < NTDDI_LONGHORN)
522 LONG MmCacheReadCount
;
525 LONG MmDirtyPagesWriteCount
;
526 LONG MmDirtyWriteIoCount
;
527 LONG MmMappedPagesWriteCount
;
528 LONG MmMappedWriteIoCount
;
529 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
531 ULONG KeContextSwitches
;
532 ULONG CcFastReadNoWait
;
533 ULONG CcFastReadWait
;
534 ULONG CcFastReadNotPossible
;
535 ULONG CcCopyReadNoWait
;
536 ULONG CcCopyReadWait
;
537 ULONG CcCopyReadNoWaitMiss
;
538 LONG LookasideIrpFloat
;
540 LONG LookasideIrpFloat
;
543 LONG IoReadOperationCount
;
544 LONG IoWriteOperationCount
;
545 LONG IoOtherOperationCount
;
546 LARGE_INTEGER IoReadTransferCount
;
547 LARGE_INTEGER IoWriteTransferCount
;
548 LARGE_INTEGER IoOtherTransferCount
;
549 #if (NTDDI_VERSION < NTDDI_LONGHORN)
550 ULONG KeContextSwitches
;
556 REQUEST_MAILBOX RequestMailbox
[64];
557 UINT64 SenderSummary
;
559 KDPC_DATA DpcData
[2];
561 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
566 LONG MaximumDpcQueueDepth
;
567 ULONG DpcRequestRate
;
568 ULONG MinimumDpcRate
;
569 UCHAR DpcInterruptRequested
;
570 UCHAR DpcThreadRequested
;
571 UCHAR DpcRoutineActive
;
572 UCHAR DpcThreadActive
;
578 UCHAR ThreadDpcEnable
;
582 LONG DpcSetEventRequest
;
583 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
584 ULONG KeExceptionDispatchCount
;
590 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
594 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
596 UCHAR ClockCheckSlot
;
597 UCHAR ClockPollCycle
;
599 LONG DpcWatchdogPeriod
;
600 LONG DpcWatchdogCount
;
605 LIST_ENTRY WaitListHead
;
606 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
611 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
612 UINT64 PrcbPad71
[12];
614 LIST_ENTRY DispatcherReadyListHead
[32];
615 ULONG InterruptCount
;
620 ULONG AdjustDpcThreshold
;
622 UCHAR DebuggerSavedIRQL
;
624 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
634 struct _KNODE
*ParentNode
;
635 UINT64 MultiThreadProcessorSet
;
636 struct _KPRCB
*MultiThreadSetMaster
;
637 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
639 LONG MmSpinLockOrdering
;
642 ULONG NodeShiftedColor
;
643 ULONG SecondaryColorMask
;
646 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
648 ULONG CcFastMdlReadNoWait
;
649 ULONG CcFastMdlReadWait
;
650 ULONG CcFastMdlReadNotPossible
;
651 ULONG CcMapDataNoWait
;
653 ULONG CcPinMappedDataCount
;
654 ULONG CcPinReadNoWait
;
656 ULONG CcMdlReadNoWait
;
658 ULONG CcLazyWriteHotSpots
;
659 ULONG CcLazyWriteIos
;
660 ULONG CcLazyWritePages
;
663 ULONG CcLostDelayedWrites
;
664 ULONG CcFastReadResourceMiss
;
665 ULONG CcCopyReadWaitMiss
;
666 ULONG CcFastMdlReadResourceMiss
;
667 ULONG CcMapDataNoWaitMiss
;
668 ULONG CcMapDataWaitMiss
;
669 ULONG CcPinReadNoWaitMiss
;
670 ULONG CcPinReadWaitMiss
;
671 ULONG CcMdlReadNoWaitMiss
;
672 ULONG CcMdlReadWaitMiss
;
673 ULONG CcReadAheadIos
;
674 LONG MmCacheTransitionCount
;
675 LONG MmCacheReadCount
;
678 PROCESSOR_POWER_STATE PowerState
;
679 ULONG KeAlignmentFixupCount
;
680 UCHAR VendorString
[13];
683 LARGE_INTEGER UpdateSignature
;
685 KTIMER DpcWatchdogTimer
;
686 CACHE_DESCRIPTOR Cache
[5];
689 ULONG CachedResidentAvailable
;
693 SLIST_HEADER InterruptObjectPool
;
694 SLIST_HEADER HypercallPageList
;
695 PVOID HypercallPageVirtual
;
696 PVOID VirtualApicAssist
;
697 UINT64
* StatisticsPage
;
699 UINT64 CacheProcessorMask
[5];
700 UINT64 PackageProcessorSet
;
701 UINT64 CoreProcessorSet
;
707 ULONG NodeShiftedColor
;
708 ULONG SecondaryColorMask
;
710 ULONG CcFastReadNoWait
;
711 ULONG CcFastReadWait
;
712 ULONG CcFastReadNotPossible
;
713 ULONG CcCopyReadNoWait
;
714 ULONG CcCopyReadWait
;
715 ULONG CcCopyReadNoWaitMiss
;
716 ULONG KeAlignmentFixupCount
;
717 ULONG KeDcacheFlushCount
;
718 ULONG KeExceptionDispatchCount
;
719 ULONG KeFirstLevelTbFills
;
720 ULONG KeFloatingEmulationCount
;
721 ULONG KeIcacheFlushCount
;
722 ULONG KeSecondLevelTbFills
;
723 UCHAR VendorString
[13];
726 LARGE_INTEGER UpdateSignature
;
727 PROCESSOR_POWER_STATE PowerState
;
728 CACHE_DESCRIPTOR Cache
[5];
735 // Processor Control Region
737 typedef struct _KIPCR
744 union _KGDTENTRY64
*GdtBase
;
745 struct _KTSS64
*TssBase
;
748 struct _KPRCB
*CurrentPrcb
;
749 PKSPIN_LOCK_QUEUE LockArray
;
753 union _KIDTENTRY64
*IdtBase
;
756 UCHAR SecondLevelCacheAssociativity
;
757 UCHAR ObsoleteNumber
;
762 ULONG StallScaleFactor
;
764 ULONG KernelReserved
[15];
765 ULONG SecondLevelCacheSize
;
766 ULONG HalReserved
[16];
769 PVOID KdVersionBlock
; // 0x108
772 ULONG Fill2
[2]; // 0x178
776 ULONG ContextSwitches
;
784 typedef struct _KiIoAccessMap
786 UCHAR DirectionMap
[32];
792 typedef struct _KTSS64
794 /* 000 */ ULONG Reserved0
;
795 /* 004 */ UINT64 Rsp0
;
796 /* 00c */ UINT64 Rsp1
;
797 /* 014 */ UINT64 Rsp2
;
798 /* 01c */ UINT64 Ist
[8];
799 /* 05c */ UINT64 Reserved1
;
800 /* 064 */ USHORT Reserved2
;
801 /* 066 */ USHORT IoMapBase
;
805 #define PKTSS PKTSS64
808 // i386 CPUs don't have exception frames
810 typedef struct _KEXCEPTION_FRAME KEXCEPTION_FRAME
, *PKEXCEPTION_FRAME
;
813 // Inline function to get current KPRCB
817 KeGetCurrentPrcb(VOID
)
819 return (struct _KPRCB
*)__readgsqword(FIELD_OFFSET(KIPCR
, CurrentPrcb
));