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
;
176 UINT64 ContextRecord
;
177 UINT64 TimeStampCKCL
;
185 UINT64 LastBranchToRip
;
186 UINT64 LastBranchFromRip
;
187 UINT64 LastExceptionToRip
;
188 UINT64 LastExceptionFromRip
;
189 UINT64 LastBranchControl
;
201 UINT64 ExceptionFrame
;
202 UINT64 TimeStampKlog
;
214 } KTRAP_FRAME
, *PKTRAP_FRAME
;
218 // GDT Entry Definition
220 typedef union _KGDTENTRY64
253 } KGDTENTRY64
, *PKGDTENTRY64
;
254 #define KGDTENTRY KGDTENTRY64
255 #define PKGDTENTRY PKGDTENTRY64
258 // IDT Entry Access Definition
260 typedef struct _KIDT_ACCESS
268 UCHAR SystemSegmentFlag
:1;
274 } KIDT_ACCESS
, *PKIDT_ACCESS
;
277 // IDT Entry Definition
279 typedef union _KIDTENTRY64
295 } KIDTENTRY64
, *PKIDTENTRY64
;
296 #define KIDTENTRY KIDTENTRY64
297 #define PKIDTENTRY PKIDTENTRY64
299 typedef struct _KDESCRIPTOR
304 } KDESCRIPTOR
, *PKDESCRIPTOR
;
306 #ifndef NTOS_MODE_USER
309 // Special Registers Structure (outside of CONTEXT)
311 typedef struct _KSPECIAL_REGISTERS
323 struct _KDESCRIPTOR Gdtr
;
324 struct _KDESCRIPTOR Idtr
;
329 UINT64 LastBranchToRip
;
330 UINT64 LastBranchFromRip
;
331 UINT64 LastExceptionToRip
;
332 UINT64 LastExceptionFromRip
;
339 UINT64 MsrSyscallMask
;
340 } KSPECIAL_REGISTERS
, *PKSPECIAL_REGISTERS
;
343 // Processor State Data
345 typedef struct _KPROCESSOR_STATE
347 KSPECIAL_REGISTERS SpecialRegisters
;
348 CONTEXT ContextFrame
;
349 } KPROCESSOR_STATE
, *PKPROCESSOR_STATE
;
351 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
352 typedef struct _GENERAL_LOOKASIDE_POOL
356 SLIST_HEADER ListHead
;
357 SINGLE_LIST_ENTRY SingleListHead
;
361 ULONG TotalAllocates
;
364 ULONG AllocateMisses
;
386 LIST_ENTRY ListEntry
;
387 ULONG LastTotalAllocates
;
390 ULONG LastAllocateMisses
;
391 ULONG LastAllocateHits
;
394 } GENERAL_LOOKASIDE_POOL
, *PGENERAL_LOOKASIDE_POOL
;
396 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
399 typedef struct _KREQUEST_PACKET
401 PVOID CurrentPacket
[3];
403 } KREQUEST_PACKET
, *PKREQUEST_PACKET
;
405 typedef struct _REQUEST_MAILBOX
407 INT64 RequestSummary
;
408 KREQUEST_PACKET RequestPacket
;
410 } REQUEST_MAILBOX
, *PREQUEST_MAILBOX
;
413 // Processor Region Control Block
416 typedef struct _KPRCB
419 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
425 UCHAR InterruptRequest
;
427 struct _KTHREAD
*CurrentThread
;
428 struct _KTHREAD
*NextThread
;
429 struct _KTHREAD
*IdleThread
;
430 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
440 KPROCESSOR_STATE ProcessorState
;
443 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
457 UINT64 HalReserved
[8];
462 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
463 UCHAR CoresPerPhysicalProcessor
;
464 UCHAR LogicalProcessorsPerCore
;
467 UCHAR LogicalProcessorsPerPhysicalProcessor
;
470 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
477 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
484 KSPIN_LOCK_QUEUE LockQueue
[LockQueueMaximumLock
]; // 2003: 33, vista:49
485 PP_LOOKASIDE_LIST PPLookasideList
[16];
486 GENERAL_LOOKASIDE_POOL PPNPagedLookasideList
[32];
487 GENERAL_LOOKASIDE_POOL PPPagedLookasideList
[32];
488 UINT64 PacketBarrier
;
489 SINGLE_LIST_ENTRY DeferredReadyListHead
;
490 LONG MmPageFaultCount
;
491 LONG MmCopyOnWriteCount
;
492 LONG MmTransitionCount
;
493 #if (NTDDI_VERSION < NTDDI_LONGHORN)
494 LONG MmCacheTransitionCount
;
496 LONG MmDemandZeroCount
;
497 LONG MmPageReadCount
;
498 LONG MmPageReadIoCount
;
499 #if (NTDDI_VERSION < NTDDI_LONGHORN)
500 LONG MmCacheReadCount
;
503 LONG MmDirtyPagesWriteCount
;
504 LONG MmDirtyWriteIoCount
;
505 LONG MmMappedPagesWriteCount
;
506 LONG MmMappedWriteIoCount
;
507 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
509 ULONG KeContextSwitches
;
510 ULONG CcFastReadNoWait
;
511 ULONG CcFastReadWait
;
512 ULONG CcFastReadNotPossible
;
513 ULONG CcCopyReadNoWait
;
514 ULONG CcCopyReadWait
;
515 ULONG CcCopyReadNoWaitMiss
;
516 LONG LookasideIrpFloat
;
518 LONG LookasideIrpFloat
;
521 LONG IoReadOperationCount
;
522 LONG IoWriteOperationCount
;
523 LONG IoOtherOperationCount
;
524 LARGE_INTEGER IoReadTransferCount
;
525 LARGE_INTEGER IoWriteTransferCount
;
526 LARGE_INTEGER IoOtherTransferCount
;
527 #if (NTDDI_VERSION < NTDDI_LONGHORN)
528 ULONG KeContextSwitches
;
534 REQUEST_MAILBOX RequestMailbox
[64];
535 UINT64 SenderSummary
;
537 KDPC_DATA DpcData
[2];
539 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
544 LONG MaximumDpcQueueDepth
;
545 ULONG DpcRequestRate
;
546 ULONG MinimumDpcRate
;
547 UCHAR DpcInterruptRequested
;
548 UCHAR DpcThreadRequested
;
549 UCHAR DpcRoutineActive
;
550 UCHAR DpcThreadActive
;
556 UCHAR ThreadDpcEnable
;
560 LONG DpcSetEventRequest
;
561 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
562 ULONG KeExceptionDispatchCount
;
568 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
572 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
574 UCHAR ClockCheckSlot
;
575 UCHAR ClockPollCycle
;
577 LONG DpcWatchdogPeriod
;
578 LONG DpcWatchdogCount
;
583 LIST_ENTRY WaitListHead
;
584 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
589 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
590 UINT64 PrcbPad71
[12];
592 LIST_ENTRY DispatcherReadyListHead
[32];
593 ULONG InterruptCount
;
598 ULONG AdjustDpcThreshold
;
600 UCHAR DebuggerSavedIRQL
;
602 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
612 struct _KNODE
*ParentNode
;
613 UINT64 MultiThreadProcessorSet
;
614 struct _KPRCB
*MultiThreadSetMaster
;
615 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
617 LONG MmSpinLockOrdering
;
620 ULONG NodeShiftedColor
;
621 ULONG SecondaryColorMask
;
624 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
626 ULONG CcFastMdlReadNoWait
;
627 ULONG CcFastMdlReadWait
;
628 ULONG CcFastMdlReadNotPossible
;
629 ULONG CcMapDataNoWait
;
631 ULONG CcPinMappedDataCount
;
632 ULONG CcPinReadNoWait
;
634 ULONG CcMdlReadNoWait
;
636 ULONG CcLazyWriteHotSpots
;
637 ULONG CcLazyWriteIos
;
638 ULONG CcLazyWritePages
;
641 ULONG CcLostDelayedWrites
;
642 ULONG CcFastReadResourceMiss
;
643 ULONG CcCopyReadWaitMiss
;
644 ULONG CcFastMdlReadResourceMiss
;
645 ULONG CcMapDataNoWaitMiss
;
646 ULONG CcMapDataWaitMiss
;
647 ULONG CcPinReadNoWaitMiss
;
648 ULONG CcPinReadWaitMiss
;
649 ULONG CcMdlReadNoWaitMiss
;
650 ULONG CcMdlReadWaitMiss
;
651 ULONG CcReadAheadIos
;
652 LONG MmCacheTransitionCount
;
653 LONG MmCacheReadCount
;
656 PROCESSOR_POWER_STATE PowerState
;
657 ULONG KeAlignmentFixupCount
;
658 UCHAR VendorString
[13];
661 LARGE_INTEGER UpdateSignature
;
663 KTIMER DpcWatchdogTimer
;
664 CACHE_DESCRIPTOR Cache
[5];
667 ULONG CachedResidentAvailable
;
671 SLIST_HEADER InterruptObjectPool
;
672 SLIST_HEADER HypercallPageList
;
673 PVOID HypercallPageVirtual
;
674 PVOID VirtualApicAssist
;
675 UINT64
* StatisticsPage
;
677 UINT64 CacheProcessorMask
[5];
678 UINT64 PackageProcessorSet
;
679 UINT64 CoreProcessorSet
;
685 ULONG NodeShiftedColor
;
686 ULONG SecondaryColorMask
;
688 ULONG CcFastReadNoWait
;
689 ULONG CcFastReadWait
;
690 ULONG CcFastReadNotPossible
;
691 ULONG CcCopyReadNoWait
;
692 ULONG CcCopyReadWait
;
693 ULONG CcCopyReadNoWaitMiss
;
694 ULONG KeAlignmentFixupCount
;
695 ULONG KeDcacheFlushCount
;
696 ULONG KeExceptionDispatchCount
;
697 ULONG KeFirstLevelTbFills
;
698 ULONG KeFloatingEmulationCount
;
699 ULONG KeIcacheFlushCount
;
700 ULONG KeSecondLevelTbFills
;
701 UCHAR VendorString
[13];
704 LARGE_INTEGER UpdateSignature
;
705 PROCESSOR_POWER_STATE PowerState
;
706 CACHE_DESCRIPTOR Cache
[5];
713 // Processor Control Region
715 typedef struct _KIPCR
722 union _KGDTENTRY64
*GdtBase
;
723 struct _KTSS64
*TssBase
;
726 struct _KPRCB
*CurrentPrcb
;
727 PKSPIN_LOCK_QUEUE LockArray
;
731 union _KIDTENTRY64
*IdtBase
;
734 UCHAR SecondLevelCacheAssociativity
;
735 UCHAR ObsoleteNumber
;
740 ULONG StallScaleFactor
;
742 ULONG KernelReserved
[15];
743 ULONG SecondLevelCacheSize
;
744 ULONG HalReserved
[16];
747 PVOID KdVersionBlock
; // 0x108
750 ULONG Fill2
[2]; // 0x178
754 ULONG ContextSwitches
;
762 typedef struct _KiIoAccessMap
764 UCHAR DirectionMap
[32];
770 typedef struct _KTSS64
772 /* 000 */ ULONG Reserved0
;
773 /* 004 */ UINT64 Rsp0
;
774 /* 00c */ UINT64 Rsp1
;
775 /* 014 */ UINT64 Rsp2
;
776 /* 01c */ UINT64 Ist
[8];
777 /* 05c */ UINT64 Reserved1
;
778 /* 064 */ USHORT Reserved2
;
779 /* 066 */ USHORT IoMapBase
;
783 #define PKTSS PKTSS64
786 // i386 CPUs don't have exception frames
788 typedef struct _KEXCEPTION_FRAME KEXCEPTION_FRAME
, *PKEXCEPTION_FRAME
;
791 // Inline function to get current KPRCB
795 KeGetCurrentPrcb(VOID
)
797 return (struct _KPRCB
*)__readgsqword(FIELD_OFFSET(KIPCR
, CurrentPrcb
));