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 0x01L
86 #define EFLAGS_ZF 0x40L
87 #define EFLAGS_TF 0x100L
88 #define EFLAGS_INTERRUPT_MASK 0x200L
89 #define EFLAGS_DF 0x400L
90 #define EFLAGS_NESTED_TASK 0x4000L
91 #define EFLAGS_V86_MASK 0x20000
92 #define EFLAGS_ALIGN_CHECK 0x40000
93 #define EFLAGS_VIF 0x80000
94 #define EFLAGS_VIP 0x100000
95 #define EFLAGS_USER_SANITIZE 0x3F4DD7
96 #define EFLAG_SIGN 0x8000
97 #define EFLAG_ZERO 0x4000
105 #define IPI_PACKET_READY 8
106 #define IPI_SYNCH_REQUEST 16
111 #define PRCB_MAJOR_VERSION 1
112 #define PRCB_BUILD_DEBUG 1
113 #define PRCB_BUILD_UNIPROCESSOR 2
118 #define INITIAL_STALL_COUNT 0x64
123 #define IO_ACCESS_MAP_NONE 0
124 #define IOPM_OFFSET FIELD_OFFSET(KTSS, IoMaps[0].IoMap)
125 #define KiComputeIopmOffset(MapNumber) \
126 (MapNumber == IO_ACCESS_MAP_NONE) ? \
127 (USHORT)(sizeof(KTSS)) : \
128 (USHORT)(FIELD_OFFSET(KTSS, IoMaps[MapNumber-1].IoMap))
131 // Static Kernel-Mode Address start (use MM_KSEG0_BASE for actual)
133 #define KSEG0_BASE 0xfffff80000000000ULL
136 // Synchronization-level IRQL
139 #define SYNCH_LEVEL DISPATCH_LEVEL
141 #define SYNCH_LEVEL (IPI_LEVEL - 2)
145 // Trap Frame Definition
147 typedef struct _KTRAP_FRAME
156 UCHAR FaultIndicator
;
157 UCHAR ExceptionActive
;
180 UINT64 ContextRecord
;
181 UINT64 TimeStampCKCL
;
194 UINT64 LastBranchToRip
;
195 UINT64 LastBranchFromRip
;
196 UINT64 LastExceptionToRip
;
197 UINT64 LastExceptionFromRip
;
201 UINT64 LastBranchControl
;
217 UINT64 ExceptionFrame
;
218 UINT64 TimeStampKlog
;
231 } KTRAP_FRAME
, *PKTRAP_FRAME
;
236 #ifndef _LDT_ENTRY_DEFINED
237 #define _LDT_ENTRY_DEFINED
238 typedef ULONG LDT_ENTRY
;
242 // GDT Entry Definition
244 typedef union _KGDTENTRY64
277 } KGDTENTRY64
, *PKGDTENTRY64
;
278 #define KGDTENTRY KGDTENTRY64
279 #define PKGDTENTRY PKGDTENTRY64
282 // IDT Entry Access Definition
284 typedef struct _KIDT_ACCESS
292 UCHAR SystemSegmentFlag
:1;
298 } KIDT_ACCESS
, *PKIDT_ACCESS
;
301 // IDT Entry Definition
303 typedef union _KIDTENTRY64
319 } KIDTENTRY64
, *PKIDTENTRY64
;
320 #define KIDTENTRY KIDTENTRY64
321 #define PKIDTENTRY PKIDTENTRY64
323 typedef struct _KDESCRIPTOR
328 } KDESCRIPTOR
, *PKDESCRIPTOR
;
330 #ifndef NTOS_MODE_USER
333 // Special Registers Structure (outside of CONTEXT)
335 typedef struct _KSPECIAL_REGISTERS
347 struct _KDESCRIPTOR Gdtr
;
348 struct _KDESCRIPTOR Idtr
;
353 UINT64 LastBranchToRip
;
354 UINT64 LastBranchFromRip
;
355 UINT64 LastExceptionToRip
;
356 UINT64 LastExceptionFromRip
;
363 UINT64 MsrSyscallMask
;
364 } KSPECIAL_REGISTERS
, *PKSPECIAL_REGISTERS
;
367 // Processor State Data
369 typedef struct _KPROCESSOR_STATE
371 KSPECIAL_REGISTERS SpecialRegisters
;
372 CONTEXT ContextFrame
;
373 } KPROCESSOR_STATE
, *PKPROCESSOR_STATE
;
375 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
376 typedef struct _GENERAL_LOOKASIDE_POOL
380 SLIST_HEADER ListHead
;
381 SINGLE_LIST_ENTRY SingleListHead
;
385 ULONG TotalAllocates
;
388 ULONG AllocateMisses
;
410 LIST_ENTRY ListEntry
;
411 ULONG LastTotalAllocates
;
414 ULONG LastAllocateMisses
;
415 ULONG LastAllocateHits
;
418 } GENERAL_LOOKASIDE_POOL
, *PGENERAL_LOOKASIDE_POOL
;
420 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
423 typedef struct _KREQUEST_PACKET
425 PVOID CurrentPacket
[3];
427 } KREQUEST_PACKET
, *PKREQUEST_PACKET
;
429 typedef struct _REQUEST_MAILBOX
431 INT64 RequestSummary
;
432 KREQUEST_PACKET RequestPacket
;
434 } REQUEST_MAILBOX
, *PREQUEST_MAILBOX
;
437 // Processor Region Control Block
440 typedef struct _KPRCB
443 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
449 UCHAR InterruptRequest
;
451 struct _KTHREAD
*CurrentThread
;
452 struct _KTHREAD
*NextThread
;
453 struct _KTHREAD
*IdleThread
;
454 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
464 KPROCESSOR_STATE ProcessorState
;
467 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
481 UINT64 HalReserved
[8];
486 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
487 UCHAR CoresPerPhysicalProcessor
;
488 UCHAR LogicalProcessorsPerCore
;
491 UCHAR LogicalProcessorsPerPhysicalProcessor
;
494 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
501 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
508 KSPIN_LOCK_QUEUE LockQueue
[LockQueueMaximumLock
]; // 2003: 33, vista:49
509 PP_LOOKASIDE_LIST PPLookasideList
[16];
510 GENERAL_LOOKASIDE_POOL PPNPagedLookasideList
[32];
511 GENERAL_LOOKASIDE_POOL PPPagedLookasideList
[32];
512 UINT64 PacketBarrier
;
513 SINGLE_LIST_ENTRY DeferredReadyListHead
;
514 LONG MmPageFaultCount
;
515 LONG MmCopyOnWriteCount
;
516 LONG MmTransitionCount
;
517 #if (NTDDI_VERSION < NTDDI_LONGHORN)
518 LONG MmCacheTransitionCount
;
520 LONG MmDemandZeroCount
;
521 LONG MmPageReadCount
;
522 LONG MmPageReadIoCount
;
523 #if (NTDDI_VERSION < NTDDI_LONGHORN)
524 LONG MmCacheReadCount
;
527 LONG MmDirtyPagesWriteCount
;
528 LONG MmDirtyWriteIoCount
;
529 LONG MmMappedPagesWriteCount
;
530 LONG MmMappedWriteIoCount
;
531 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
533 ULONG KeContextSwitches
;
534 ULONG CcFastReadNoWait
;
535 ULONG CcFastReadWait
;
536 ULONG CcFastReadNotPossible
;
537 ULONG CcCopyReadNoWait
;
538 ULONG CcCopyReadWait
;
539 ULONG CcCopyReadNoWaitMiss
;
540 LONG LookasideIrpFloat
;
542 LONG LookasideIrpFloat
;
545 LONG IoReadOperationCount
;
546 LONG IoWriteOperationCount
;
547 LONG IoOtherOperationCount
;
548 LARGE_INTEGER IoReadTransferCount
;
549 LARGE_INTEGER IoWriteTransferCount
;
550 LARGE_INTEGER IoOtherTransferCount
;
551 #if (NTDDI_VERSION < NTDDI_LONGHORN)
552 ULONG KeContextSwitches
;
558 REQUEST_MAILBOX RequestMailbox
[64];
559 UINT64 SenderSummary
;
561 KDPC_DATA DpcData
[2];
563 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
568 LONG MaximumDpcQueueDepth
;
569 ULONG DpcRequestRate
;
570 ULONG MinimumDpcRate
;
571 UCHAR DpcInterruptRequested
;
572 UCHAR DpcThreadRequested
;
573 UCHAR DpcRoutineActive
;
574 UCHAR DpcThreadActive
;
580 UCHAR ThreadDpcEnable
;
584 LONG DpcSetEventRequest
;
585 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
586 ULONG KeExceptionDispatchCount
;
592 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
596 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
598 UCHAR ClockCheckSlot
;
599 UCHAR ClockPollCycle
;
601 LONG DpcWatchdogPeriod
;
602 LONG DpcWatchdogCount
;
607 LIST_ENTRY WaitListHead
;
608 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
613 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
614 UINT64 PrcbPad71
[12];
616 LIST_ENTRY DispatcherReadyListHead
[32];
617 ULONG InterruptCount
;
622 ULONG AdjustDpcThreshold
;
624 UCHAR DebuggerSavedIRQL
;
626 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
636 struct _KNODE
*ParentNode
;
637 UINT64 MultiThreadProcessorSet
;
638 struct _KPRCB
*MultiThreadSetMaster
;
639 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
641 LONG MmSpinLockOrdering
;
644 ULONG NodeShiftedColor
;
645 ULONG SecondaryColorMask
;
648 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
650 ULONG CcFastMdlReadNoWait
;
651 ULONG CcFastMdlReadWait
;
652 ULONG CcFastMdlReadNotPossible
;
653 ULONG CcMapDataNoWait
;
655 ULONG CcPinMappedDataCount
;
656 ULONG CcPinReadNoWait
;
658 ULONG CcMdlReadNoWait
;
660 ULONG CcLazyWriteHotSpots
;
661 ULONG CcLazyWriteIos
;
662 ULONG CcLazyWritePages
;
665 ULONG CcLostDelayedWrites
;
666 ULONG CcFastReadResourceMiss
;
667 ULONG CcCopyReadWaitMiss
;
668 ULONG CcFastMdlReadResourceMiss
;
669 ULONG CcMapDataNoWaitMiss
;
670 ULONG CcMapDataWaitMiss
;
671 ULONG CcPinReadNoWaitMiss
;
672 ULONG CcPinReadWaitMiss
;
673 ULONG CcMdlReadNoWaitMiss
;
674 ULONG CcMdlReadWaitMiss
;
675 ULONG CcReadAheadIos
;
676 LONG MmCacheTransitionCount
;
677 LONG MmCacheReadCount
;
680 PROCESSOR_POWER_STATE PowerState
;
681 ULONG KeAlignmentFixupCount
;
682 UCHAR VendorString
[13];
685 LARGE_INTEGER UpdateSignature
;
687 KTIMER DpcWatchdogTimer
;
688 CACHE_DESCRIPTOR Cache
[5];
691 ULONG CachedResidentAvailable
;
695 SLIST_HEADER InterruptObjectPool
;
696 SLIST_HEADER HypercallPageList
;
697 PVOID HypercallPageVirtual
;
698 PVOID VirtualApicAssist
;
699 UINT64
* StatisticsPage
;
701 UINT64 CacheProcessorMask
[5];
702 UINT64 PackageProcessorSet
;
703 UINT64 CoreProcessorSet
;
709 ULONG NodeShiftedColor
;
710 ULONG SecondaryColorMask
;
712 ULONG CcFastReadNoWait
;
713 ULONG CcFastReadWait
;
714 ULONG CcFastReadNotPossible
;
715 ULONG CcCopyReadNoWait
;
716 ULONG CcCopyReadWait
;
717 ULONG CcCopyReadNoWaitMiss
;
718 ULONG KeAlignmentFixupCount
;
719 ULONG KeDcacheFlushCount
;
720 ULONG KeExceptionDispatchCount
;
721 ULONG KeFirstLevelTbFills
;
722 ULONG KeFloatingEmulationCount
;
723 ULONG KeIcacheFlushCount
;
724 ULONG KeSecondLevelTbFills
;
725 UCHAR VendorString
[13];
728 LARGE_INTEGER UpdateSignature
;
729 PROCESSOR_POWER_STATE PowerState
;
730 CACHE_DESCRIPTOR Cache
[5];
737 // Processor Control Region
739 typedef struct _KIPCR
746 union _KGDTENTRY64
*GdtBase
;
747 struct _KTSS64
*TssBase
;
750 struct _KPRCB
*CurrentPrcb
;
751 PKSPIN_LOCK_QUEUE LockArray
;
755 union _KIDTENTRY64
*IdtBase
;
758 UCHAR SecondLevelCacheAssociativity
;
759 UCHAR ObsoleteNumber
;
764 ULONG StallScaleFactor
;
766 ULONG KernelReserved
[15];
767 ULONG SecondLevelCacheSize
;
768 ULONG HalReserved
[16];
771 PVOID KdVersionBlock
; // 0x108
774 ULONG Fill2
[2]; // 0x178
778 ULONG ContextSwitches
;
786 typedef struct _KiIoAccessMap
788 UCHAR DirectionMap
[32];
794 typedef struct _KTSS64
796 /* 000 */ ULONG Reserved0
;
797 /* 004 */ UINT64 Rsp0
;
798 /* 00c */ UINT64 Rsp1
;
799 /* 014 */ UINT64 Rsp2
;
800 /* 01c */ UINT64 Ist
[8];
801 /* 05c */ UINT64 Reserved1
;
802 /* 064 */ USHORT Reserved2
;
803 /* 066 */ USHORT IoMapBase
;
807 #define PKTSS PKTSS64
810 // i386 CPUs don't have exception frames
812 typedef struct _KEXCEPTION_FRAME KEXCEPTION_FRAME
, *PKEXCEPTION_FRAME
;
815 // Inline function to get current KPRCB
819 KeGetCurrentPrcb(VOID
)
821 return (struct _KPRCB
*)__readgsqword(FIELD_OFFSET(KIPCR
, CurrentPrcb
));