xref: /reactos/sdk/include/ndk/amd64/ketypes.h (revision bed8b2a0)
1 /*++ NDK Version: 0098
2 
3 Copyright (c) Alex Ionescu.  All rights reserved.
4 
5 Header Name:
6 
7     ketypes.h (AMD64)
8 
9 Abstract:
10 
11     amd64 Type definitions for the Kernel services.
12 
13 Author:
14 
15     Alex Ionescu (alexi@tinykrnl.org) - Updated - 27-Feb-2006
16     Timo Kreuzer (timo.kreuzer@reactos.org) - Updated - 14-Aug-2008
17 
18 --*/
19 
20 #ifndef _AMD64_KETYPES_H
21 #define _AMD64_KETYPES_H
22 
23 //
24 // Dependencies
25 //
26 
27 //
28 // Kernel Feature Bits
29 // See https://www.geoffchappell.com/studies/windows/km/ntoskrnl/structs/kprcb/featurebits.htm?tx=61&ts=0,1400
30 //
31 #define KF_SMEP                         0x00000001 // Win 6.2
32 #define KF_RDTSC                        0x00000002 // From ks386.inc, ksamd64.inc
33 #define KF_CR4                          0x00000004 // From ks386.inc, ksamd64.inc
34 #define KF_CMOV                         0x00000008
35 #define KF_GLOBAL_PAGE                  0x00000010 // From ks386.inc, ksamd64.inc
36 #define KF_LARGE_PAGE                   0x00000020 // From ks386.inc, ksamd64.inc
37 #define KF_MTRR                         0x00000040
38 #define KF_CMPXCHG8B                    0x00000080 // From ks386.inc, ksamd64.inc
39 #define KF_MMX                          0x00000100
40 #define KF_DTS                          0x00000200 // Win 5.2-6.2
41 #define KF_PAT                          0x00000400
42 #define KF_FXSR                         0x00000800
43 #define KF_FAST_SYSCALL                 0x00001000 // From ks386.inc, ksamd64.inc
44 #define KF_XMMI                         0x00002000 // SSE
45 #define KF_3DNOW                        0x00004000
46 #define KF_AMDK6MTRR                    0x00008000 // Win 5.0-6.1
47 #define KF_XSAVEOPT                     0x00008000 // From KF_XSAVEOPT_BIT
48 #define KF_XMMI64                       0x00010000 // SSE2
49 #define KF_BRANCH                       0x00020000 // From ksamd64.inc, Win 6.1-6.2
50 #define KF_00040000                     0x00040000 // Unclear
51 #define KF_SSE3                         0x00080000 // Win 6.0+
52 #define KF_CMPXCHG16B                   0x00100000 // Win 6.0-6.2
53 #define KF_AUTHENTICAMD                 0x00200000 // Win 6.1+
54 #define KF_ACNT2                        0x00400000 // Win 6.1+
55 #define KF_XSTATE                       0x00800000 // From ksamd64.inc, Win 6.1+
56 #define KF_GENUINE_INTEL                0x01000000 // Win 6.1+
57 #define KF_02000000                     0x02000000 // Unclear
58 #define KF_SLAT                         0x04000000 // Win 6.2+, Intel: EPT supported
59 #define KF_VIRT_FIRMWARE_ENABLED        0x08000000 // Win 6.2+
60 #define KF_RDWRFSGSBASE                 0x10000000 // From ksamd64.inc KF_RDWRFSGSBASE_BIT (0x1C)
61 #define KF_NX_BIT                       0x20000000
62 #define KF_NX_DISABLED                  0x40000000
63 #define KF_NX_ENABLED                   0x80000000
64 #define KF_RDRAND               0x0000000100000000ULL // Win 10.0+
65 #define KF_SMAP                 0x0000000200000000ULL // From ksamd64.inc
66 #define KF_RDTSCP               0x0000000400000000ULL // Win 10.0+
67 #define KF_HUGEPAGE             0x0000002000000000ULL // Win 10.0 1607+
68 #define KF_XSAVES               0x0000004000000000ULL // From ksamd64.inc KF_XSAVES_BIT (0x26)
69 #define KF_FPU_LEAKAGE          0x0000020000000000ULL // From ksamd64.inc KF_FPU_LEAKAGE_BIT (0x29)
70 #define KF_CAT                  0x0000100000000000ULL // From ksamd64.inc KF_CAT_BIT (0x02C)
71 #define KF_CET_SS               0x0000400000000000ULL // From ksamd64.inc
72 #define KF_SSSE3                0x0000800000000000ULL
73 #define KF_SSE4_1               0x0001000000000000ULL
74 #define KF_SSE4_2               0x0002000000000000ULL
75 
76 #define KF_XSAVEOPT_BIT                 15 // From ksamd64.inc (0x0F -> 0x8000)
77 #define KF_XSTATE_BIT                   23 // From ksamd64.inc (0x17 -> 0x800000)
78 #define KF_RDWRFSGSBASE_BIT             28 // From ksamd64.inc (0x1C -> 0x10000000)
79 #define KF_XSAVES_BIT                   38 // From ksamd64.inc (0x26 -> 0x4000000000)
80 #define KF_FPU_LEAKAGE_BIT              41 // From ksamd64.inc (0x29 -> 0x20000000000)
81 #define KF_CAT_BIT                      44 // From ksamd64.inc (0x2C -> 0x100000000000)
82 
83 //
84 // Double fault stack size
85 //
86 #define DOUBLE_FAULT_STACK_SIZE 0x2000
87 
88 //
89 // CPU Vendors
90 //
91 typedef enum
92 {
93     CPU_UNKNOWN,
94     CPU_AMD,
95     CPU_INTEL,
96     CPU_VIA
97 } CPU_VENDORS;
98 
99 //
100 // Machine Types
101 //
102 #define MACHINE_TYPE_ISA        0x0000
103 #define MACHINE_TYPE_EISA       0x0001
104 #define MACHINE_TYPE_MCA        0x0002
105 
106 //
107 // X86 80386 Segment Types
108 //
109 #define I386_TASK_GATE          0x5
110 #define I386_TSS                0x9
111 #define I386_ACTIVE_TSS         0xB
112 #define I386_CALL_GATE          0xC
113 #define I386_INTERRUPT_GATE     0xE
114 #define I386_TRAP_GATE          0xF
115 
116 //
117 // Selector Names
118 //
119 #define RPL_MASK                0x0003
120 #define MODE_MASK               0x0001
121 #define KGDT64_NULL             0x0000
122 #define KGDT64_R0_CODE          0x0010
123 #define KGDT64_R0_DATA          0x0018
124 #define KGDT64_R3_CMCODE        0x0020
125 #define KGDT64_R3_DATA          0x0028
126 #define KGDT64_R3_CODE          0x0030
127 #define KGDT64_SYS_TSS          0x0040
128 #define KGDT64_R3_CMTEB         0x0050
129 #define KGDT64_R0_LDT           0x0060
130 
131 //
132 // CR4
133 //
134 #define CR4_VME                 0x1
135 #define CR4_PVI                 0x2
136 #define CR4_TSD                 0x4
137 #define CR4_DE                  0x8
138 #define CR4_PSE                 0x10
139 #define CR4_PAE                 0x20
140 #define CR4_MCE                 0x40
141 #define CR4_PGE                 0x80
142 #define CR4_FXSR                0x200
143 #define CR4_XMMEXCPT            0x400
144 #define CR4_CHANNELS            0x800
145 #define CR4_XSAVE               0x40000
146 
147 //
148 // DR7
149 //
150 #define DR7_LEGAL               0xFFFF0355
151 #define DR7_ACTIVE              0x00000355
152 #define DR7_TRACE_BRANCH        0x00000200
153 #define DR7_LAST_BRANCH         0x00000100
154 
155 //
156 // Debug flags
157 //
158 #define DEBUG_ACTIVE_DR7                        0x0001
159 #define DEBUG_ACTIVE_INSTRUMENTED               0x0002
160 #define DEBUG_ACTIVE_DBG_INSTRUMENTED           0x0003
161 #define DEBUG_ACTIVE_MINIMAL_THREAD             0x0004
162 #define DEBUG_ACTIVE_PRIMARY_THREAD             0x0080
163 #define DEBUG_ACTIVE_PRIMARY_THREAD_BIT         0x0007
164 #define DEBUG_ACTIVE_PRIMARY_THREAD_LOCK_BIT    0x001F
165 #define DEBUG_ACTIVE_SCHEDULED_THREAD           0x0040
166 #define DEBUG_ACTIVE_SCHEDULED_THREAD_BIT       0x0006
167 #define DEBUG_ACTIVE_SCHEDULED_THREAD_LOCK_BIT  0x001E
168 #define DEBUG_ACTIVE_SCHEDULED_THREAD_LOCK      0x40000000
169 
170 //
171 // EFlags
172 //
173 #define EFLAGS_CF               0x01L
174 #define EFLAGS_ZF               0x40L
175 #define EFLAGS_TF               0x100L
176 #define EFLAGS_INTERRUPT_MASK   0x200L
177 #define EFLAGS_DF               0x400L
178 #define EFLAGS_IOPL             0x3000L
179 #define EFLAGS_NESTED_TASK      0x4000L
180 //#define EFLAGS_NF               0x4000
181 #define EFLAGS_RF               0x10000
182 #define EFLAGS_V86_MASK         0x20000
183 #define EFLAGS_ALIGN_CHECK      0x40000
184 #define EFLAGS_VIF              0x80000
185 #define EFLAGS_VIP              0x100000
186 #define EFLAGS_ID               0x200000
187 #define EFLAGS_USER_SANITIZE    0x3F4DD7
188 #define EFLAG_SIGN              0x8000
189 #define EFLAG_ZERO              0x4000
190 #define EFLAGS_TF_MASK          0x0100
191 #define EFLAGS_TF_SHIFT         0x0008
192 #define EFLAGS_ID_MASK          0x200000
193 #define EFLAGS_IF_MASK          0x0200
194 #define EFLAGS_IF_SHIFT         0x0009
195 
196 //
197 // MXCSR Floating Control/Status Bit Masks
198 //
199 #define XSW_INVALID_OPERATION   0x0001
200 #define XSW_DENORMAL            0x0002
201 #define XSW_ZERO_DIVIDE         0x0004
202 #define XSW_OVERFLOW            0x0008
203 #define XSW_UNDERFLOW           0x0010
204 #define XSW_PRECISION           0x0020
205 #define XCW_INVALID_OPERATION   0x0080
206 #define XCW_DENORMAL            0x0100
207 #define XCW_ZERO_DIVIDE         0x0200
208 #define XCW_OVERFLOW            0x0400
209 #define XCW_UNDERFLOW           0x0800
210 #define XCW_PRECISION           0x1000
211 #define XCW_ROUND_CONTROL       0x6000
212 #define XCW_FLUSH_ZERO          0x8000
213 #define XSW_ERROR_MASK          0x003F
214 #define XSW_ERROR_SHIFT         7
215 
216 //
217 // Legacy floating status word bit masks.
218 //
219 #define FSW_INVALID_OPERATION   0x0001
220 #define FSW_DENORMAL            0x0002
221 #define FSW_ZERO_DIVIDE         0x0004
222 #define FSW_OVERFLOW            0x0008
223 #define FSW_UNDERFLOW           0x0010
224 #define FSW_PRECISION           0x0020
225 #define FSW_STACK_FAULT         0x0040
226 #define FSW_ERROR_SUMMARY       0x0080
227 #define FSW_CONDITION_CODE_0    0x0100
228 #define FSW_CONDITION_CODE_1    0x0200
229 #define FSW_CONDITION_CODE_2    0x0400
230 #define FSW_CONDITION_CODE_3    0x4000
231 #define FSW_ERROR_MASK          0x003F
232 
233 //
234 // Machine Specific Registers
235 //
236 #define MSR_EFER                0xC0000080
237 #define MSR_STAR                0xC0000081
238 #define MSR_LSTAR               0xC0000082
239 #define MSR_CSTAR               0xC0000083
240 #define MSR_SYSCALL_MASK        0xC0000084
241 #define MSR_FS_BASE             0xC0000100
242 #define MSR_GS_BASE             0xC0000101
243 #define MSR_GS_SWAP             0xC0000102
244 #define MSR_MCG_STATUS          0x017A
245 #define MSR_AMD_ACCESS          0x9C5A203A
246 #define MSR_IA32_MISC_ENABLE    0x000001A0
247 #define MSR_LAST_BRANCH_FROM    0x01DB
248 #define MSR_LAST_BRANCH_TO      0x01DC
249 #define MSR_LAST_EXCEPTION_FROM 0x01DD
250 #define MSR_LAST_EXCEPTION_TO   0x01DE
251 
252 //
253 // Caching values for the PAT MSR
254 //
255 #define PAT_UC                  0ULL
256 #define PAT_WC                  1ULL
257 #define PAT_WT                  4ULL
258 #define PAT_WP                  5ULL
259 #define PAT_WB                  6ULL
260 #define PAT_UCM                 7ULL
261 
262 //
263 // Flags in MSR_EFER
264 //
265 #define MSR_SCE                 0x0001
266 #define MSR_LME                 0x0100
267 #define MSR_LMA                 0x0400
268 #define MSR_NXE                 0x0800
269 #define MSR_PAT                 0x0277
270 #define MSR_DEBUG_CTL           0x01D9
271 
272 //
273 //  Flags in MSR_IA32_MISC_ENABLE
274 //
275 #define MSR_XD_ENABLE_MASK      0xFFFFFFFB
276 
277 //
278 //  Flags in MSR_DEBUG_CTL
279 //
280 #define MSR_DEBUG_CTL_LBR       0x0001
281 #define MSR_DEBUG_CTL_BTF       0x0002
282 
283 //
284 // IPI Types
285 //
286 #define IPI_APC                 1
287 #define IPI_DPC                 2
288 #define IPI_FREEZE              4
289 #define IPI_PACKET_READY        8
290 #define IPI_SYNCH_REQUEST       16
291 
292 //
293 // Flags for KPRCB::IpiFrozen
294 //
295 // Values shown with !ipi extension in WinDbg:
296 // 0 = [Running], 1 = [Unknown], 2 = [Frozen], 3 = [Thaw], 4 = [Freeze Owner]
297 // 5 = [Target Freeze], 6-15 = [Unknown]
298 // 0x20 = [Active] (flag)
299 //
300 #define IPI_FROZEN_STATE_RUNNING 0
301 #define IPI_FROZEN_STATE_FROZEN 2
302 #define IPI_FROZEN_STATE_THAW 3
303 #define IPI_FROZEN_STATE_OWNER 4
304 #define IPI_FROZEN_STATE_TARGET_FREEZE 5
305 #define IPI_FROZEN_FLAG_ACTIVE 0x20
306 
307 //
308 // PRCB Flags
309 //
310 #define PRCB_MINOR_VERSION      1
311 #define PRCB_MAJOR_VERSION      1
312 #define PRCB_BUILD_DEBUG        1
313 #define PRCB_BUILD_UNIPROCESSOR 2
314 
315 //
316 // Exception active flags
317 //
318 #define KEXCEPTION_ACTIVE_INTERRUPT_FRAME 0x0000
319 #define KEXCEPTION_ACTIVE_EXCEPTION_FRAME 0x0001
320 #define KEXCEPTION_ACTIVE_SERVICE_FRAME   0x0002
321 
322 //
323 // HAL Variables
324 //
325 #define INITIAL_STALL_COUNT     100
326 #define MM_HAL_VA_START         0xFFFFFFFFFFC00000ULL /* This is Vista+ */
327 #define MM_HAL_VA_END           0xFFFFFFFFFFFFFFFFULL
328 #define APIC_BASE               0xFFFFFFFFFFFE0000ULL
329 
330 //
331 // IOPM Definitions
332 //
333 #define IO_ACCESS_MAP_NONE      0
334 #define IOPM_OFFSET             FIELD_OFFSET(KTSS, IoMaps[0].IoMap)
335 #define KiComputeIopmOffset(MapNumber)              \
336     (MapNumber == IO_ACCESS_MAP_NONE) ?             \
337         (USHORT)(sizeof(KTSS)) :                    \
338         (USHORT)(FIELD_OFFSET(KTSS, IoMaps[MapNumber-1].IoMap))
339 
340 //
341 // Static Kernel-Mode Address start (use MM_KSEG0_BASE for actual)
342 //
343 #define KSEG0_BASE 0xfffff80000000000ULL
344 
345 #define NMI_STACK_SIZE 0x2000
346 #define ISR_STACK_SIZE 0x6000
347 
348 //
349 // Synchronization-level IRQL
350 //
351 #ifndef CONFIG_SMP
352 #define SYNCH_LEVEL             DISPATCH_LEVEL
353 #else
354 #define SYNCH_LEVEL             (IPI_LEVEL - 2)
355 #endif
356 
357 //
358 // Number of pool lookaside lists per pool in the PRCB
359 //
360 #define NUMBER_POOL_LOOKASIDE_LISTS 32
361 
362 //
363 // Structure for CPUID
364 //
365 typedef union _CPU_INFO
366 {
367     UINT32 AsUINT32[4];
368     struct
369     {
370         ULONG Eax;
371         ULONG Ebx;
372         ULONG Ecx;
373         ULONG Edx;
374     };
375 } CPU_INFO, *PCPU_INFO;
376 
377 //
378 // Trap Frame Definition
379 //
380 typedef struct _KTRAP_FRAME
381 {
382     UINT64 P1Home;
383     UINT64 P2Home;
384     UINT64 P3Home;
385     UINT64 P4Home;
386     UINT64 P5;
387     CHAR PreviousMode;
388     UCHAR PreviousIrql;
389     UCHAR FaultIndicator;
390     UCHAR ExceptionActive;
391     ULONG MxCsr;
392     UINT64 Rax;
393     UINT64 Rcx;
394     UINT64 Rdx;
395     UINT64 R8;
396     UINT64 R9;
397     UINT64 R10;
398     UINT64 R11;
399     union
400     {
401         UINT64 GsBase;
402         UINT64 GsSwap;
403     };
404     M128A Xmm0;
405     M128A Xmm1;
406     M128A Xmm2;
407     M128A Xmm3;
408     M128A Xmm4;
409     M128A Xmm5;
410     union
411     {
412         UINT64 FaultAddress;
413         UINT64 ContextRecord;
414         UINT64 TimeStampCKCL;
415     };
416     UINT64 Dr0;
417     UINT64 Dr1;
418     UINT64 Dr2;
419     UINT64 Dr3;
420     UINT64 Dr6;
421     UINT64 Dr7;
422     union
423     {
424         struct
425         {
426             UINT64 DebugControl;
427             UINT64 LastBranchToRip;
428             UINT64 LastBranchFromRip;
429             UINT64 LastExceptionToRip;
430             UINT64 LastExceptionFromRip;
431         };
432         struct
433         {
434             UINT64 LastBranchControl;
435             ULONG LastBranchMSR;
436         };
437     };
438     USHORT SegDs;
439     USHORT SegEs;
440     USHORT SegFs;
441     USHORT SegGs;
442     UINT64 TrapFrame;
443     UINT64 Rbx;
444     UINT64 Rdi;
445     UINT64 Rsi;
446     UINT64 Rbp;
447     union
448     {
449         UINT64 ErrorCode;
450         UINT64 ExceptionFrame;
451         UINT64 TimeStampKlog;
452     };
453     UINT64 Rip;
454     USHORT SegCs;
455     UCHAR Fill0;
456     UCHAR Logging;
457     USHORT Fill1[2];
458     ULONG EFlags;
459     ULONG Fill2;
460     UINT64 Rsp;
461     USHORT SegSs;
462     USHORT Fill3;
463     LONG CodePatchCycle;
464 } KTRAP_FRAME, *PKTRAP_FRAME;
465 
466 //
467 // Dummy LDT_ENTRY
468 //
469 #ifndef _LDT_ENTRY_DEFINED
470 #define _LDT_ENTRY_DEFINED
471 typedef ULONG LDT_ENTRY;
472 #endif
473 
474 //
475 // GDT Entry Definition
476 //
477 typedef union _KGDTENTRY64
478 {
479     struct
480     {
481         USHORT LimitLow;
482         USHORT BaseLow;
483         union
484         {
485             struct
486             {
487                 UCHAR BaseMiddle;
488                 UCHAR Flags1;
489                 UCHAR Flags2;
490                 UCHAR BaseHigh;
491             } Bytes;
492             struct
493             {
494                 ULONG BaseMiddle:8;
495                 ULONG Type:5;
496                 ULONG Dpl:2;
497                 ULONG Present:1;
498                 ULONG LimitHigh:4;
499                 ULONG System:1;
500                 ULONG LongMode:1;
501                 ULONG DefaultBig:1;
502                 ULONG Granularity:1;
503                 ULONG BaseHigh:8;
504             } Bits;
505         };
506         ULONG BaseUpper;
507         ULONG MustBeZero;
508     };
509     UINT64 Alignment;
510 } KGDTENTRY64, *PKGDTENTRY64;
511 #define KGDTENTRY KGDTENTRY64
512 #define PKGDTENTRY PKGDTENTRY64
513 
514 //
515 // IDT Entry Access Definition
516 //
517 typedef struct _KIDT_ACCESS
518 {
519     union
520     {
521         struct
522         {
523             UCHAR Reserved;
524             UCHAR SegmentType:4;
525             UCHAR SystemSegmentFlag:1;
526             UCHAR Dpl:2;
527             UCHAR Present:1;
528         };
529         USHORT Value;
530     };
531 } KIDT_ACCESS, *PKIDT_ACCESS;
532 
533 //
534 // IDT Entry Definition
535 //
536 typedef union _KIDTENTRY64
537 {
538     struct
539     {
540         USHORT OffsetLow;
541         USHORT Selector;
542         USHORT IstIndex:3;
543         USHORT Reserved0:5;
544         USHORT Type:5;
545         USHORT Dpl:2;
546         USHORT Present:1;
547         USHORT OffsetMiddle;
548         ULONG OffsetHigh;
549         ULONG Reserved1;
550     };
551     UINT64 Alignment;
552 } KIDTENTRY64, *PKIDTENTRY64;
553 #define KIDTENTRY KIDTENTRY64
554 #define PKIDTENTRY PKIDTENTRY64
555 
556 typedef struct _KDESCRIPTOR
557 {
558     USHORT Pad[3];
559     USHORT Limit;
560     PVOID Base;
561 } KDESCRIPTOR, *PKDESCRIPTOR;
562 
563 #ifndef NTOS_MODE_USER
564 
565 //
566 // Special Registers Structure (outside of CONTEXT)
567 //
568 typedef struct _KSPECIAL_REGISTERS
569 {
570     ULONG64 Cr0;
571     ULONG64 Cr2;
572     ULONG64 Cr3;
573     ULONG64 Cr4;
574     ULONG64 KernelDr0;
575     ULONG64 KernelDr1;
576     ULONG64 KernelDr2;
577     ULONG64 KernelDr3;
578     ULONG64 KernelDr6;
579     ULONG64 KernelDr7;
580     KDESCRIPTOR Gdtr;
581     KDESCRIPTOR Idtr;
582     USHORT Tr;
583     USHORT Ldtr;
584     ULONG MxCsr;
585     ULONG64 DebugControl;
586     ULONG64 LastBranchToRip;
587     ULONG64 LastBranchFromRip;
588     ULONG64 LastExceptionToRip;
589     ULONG64 LastExceptionFromRip;
590     ULONG64 Cr8;
591     ULONG64 MsrGsBase;
592     ULONG64 MsrGsSwap;
593     ULONG64 MsrStar;
594     ULONG64 MsrLStar;
595     ULONG64 MsrCStar;
596     ULONG64 MsrSyscallMask;
597 } KSPECIAL_REGISTERS, *PKSPECIAL_REGISTERS;
598 
599 //
600 // Processor State Data
601 //
602 typedef struct _KPROCESSOR_STATE
603 {
604     KSPECIAL_REGISTERS SpecialRegisters;
605     CONTEXT ContextFrame;
606 } KPROCESSOR_STATE, *PKPROCESSOR_STATE;
607 
608 #if (NTDDI_VERSION < NTDDI_LONGHORN)
609 #define GENERAL_LOOKASIDE_POOL PP_LOOKASIDE_LIST
610 #endif
611 
612 typedef struct _KREQUEST_PACKET
613 {
614     PVOID CurrentPacket[3];
615     PVOID WorkerRoutine;
616 } KREQUEST_PACKET, *PKREQUEST_PACKET;
617 
618 typedef struct _REQUEST_MAILBOX
619 {
620     INT64 RequestSummary;
621     KREQUEST_PACKET RequestPacket;
622     PVOID Virtual[7];
623 } REQUEST_MAILBOX, *PREQUEST_MAILBOX;
624 
625 //
626 // Processor Region Control Block
627 //
628 typedef struct _KPRCB
629 {
630     ULONG MxCsr;
631 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
632     USHORT Number;
633 #else
634     UCHAR Number;
635     UCHAR NestingLevel;
636 #endif
637     UCHAR InterruptRequest;
638     UCHAR IdleHalt;
639     struct _KTHREAD *CurrentThread;
640     struct _KTHREAD *NextThread;
641     struct _KTHREAD *IdleThread;
642 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
643     UCHAR NestingLevel;
644     UCHAR Group;
645     UCHAR PrcbPad00[6];
646 #else
647     UINT64 UserRsp;
648 #endif
649     UINT64 RspBase;
650     UINT64 PrcbLock;
651     UINT64 SetMember;
652     KPROCESSOR_STATE ProcessorState;
653     CHAR CpuType;
654     CHAR CpuID;
655 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
656     union
657     {
658         USHORT CpuStep;
659         struct
660         {
661             UCHAR CpuStepping;
662             UCHAR CpuModel;
663         };
664     };
665 #else
666     USHORT CpuStep;
667 #endif
668     ULONG MHz;
669     UINT64 HalReserved[8];
670     USHORT MinorVersion;
671     USHORT MajorVersion;
672     UCHAR BuildType;
673     UCHAR CpuVendor;
674 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
675     UCHAR CoresPerPhysicalProcessor;
676     UCHAR LogicalProcessorsPerCore;
677 #else
678     UCHAR InitialApicId;
679     UCHAR LogicalProcessorsPerPhysicalProcessor;
680 #endif
681     ULONG ApicMask;
682 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
683     ULONG CFlushSize;
684 #else
685     UCHAR CFlushSize;
686     UCHAR PrcbPad0x[3];
687 #endif
688     PVOID AcpiReserved;
689 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
690     ULONG InitialApicId;
691     ULONG Stride;
692     UINT64 PrcbPad01[3];
693 #else
694     UINT64 PrcbPad00[4];
695 #endif
696     KSPIN_LOCK_QUEUE LockQueue[LockQueueMaximumLock]; // 2003: 33, vista:49
697     PP_LOOKASIDE_LIST PPLookasideList[16];
698     GENERAL_LOOKASIDE_POOL PPNPagedLookasideList[NUMBER_POOL_LOOKASIDE_LISTS];
699     GENERAL_LOOKASIDE_POOL PPPagedLookasideList[NUMBER_POOL_LOOKASIDE_LISTS];
700     UINT64 PacketBarrier;
701     SINGLE_LIST_ENTRY DeferredReadyListHead;
702     LONG MmPageFaultCount;
703     LONG MmCopyOnWriteCount;
704     LONG MmTransitionCount;
705 #if (NTDDI_VERSION < NTDDI_LONGHORN)
706     LONG MmCacheTransitionCount;
707 #endif
708     LONG MmDemandZeroCount;
709     LONG MmPageReadCount;
710     LONG MmPageReadIoCount;
711 #if (NTDDI_VERSION < NTDDI_LONGHORN)
712     LONG MmCacheReadCount;
713     LONG MmCacheIoCount;
714 #endif
715     LONG MmDirtyPagesWriteCount;
716     LONG MmDirtyWriteIoCount;
717     LONG MmMappedPagesWriteCount;
718     LONG MmMappedWriteIoCount;
719 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
720     ULONG KeSystemCalls;
721     ULONG KeContextSwitches;
722     ULONG CcFastReadNoWait;
723     ULONG CcFastReadWait;
724     ULONG CcFastReadNotPossible;
725     ULONG CcCopyReadNoWait;
726     ULONG CcCopyReadWait;
727     ULONG CcCopyReadNoWaitMiss;
728     LONG LookasideIrpFloat;
729 #else
730     LONG LookasideIrpFloat;
731     ULONG KeSystemCalls;
732 #endif
733     LONG IoReadOperationCount;
734     LONG IoWriteOperationCount;
735     LONG IoOtherOperationCount;
736     LARGE_INTEGER IoReadTransferCount;
737     LARGE_INTEGER IoWriteTransferCount;
738     LARGE_INTEGER IoOtherTransferCount;
739 #if (NTDDI_VERSION < NTDDI_LONGHORN)
740     ULONG KeContextSwitches;
741     UCHAR PrcbPad2[12];
742 #endif
743     UINT64 TargetSet;
744     ULONG IpiFrozen;
745     UCHAR PrcbPad3[116];
746     REQUEST_MAILBOX RequestMailbox[64];
747     UINT64 SenderSummary;
748     UCHAR PrcbPad4[120];
749     KDPC_DATA DpcData[2];
750     PVOID DpcStack;
751 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
752     PVOID SparePtr0;
753 #else
754     PVOID SavedRsp;
755 #endif
756     LONG MaximumDpcQueueDepth;
757     ULONG DpcRequestRate;
758     ULONG MinimumDpcRate;
759     UCHAR DpcInterruptRequested;
760     UCHAR DpcThreadRequested;
761     UCHAR DpcRoutineActive;
762     UCHAR DpcThreadActive;
763     UINT64 TimerHand;
764     UINT64 TimerRequest;
765     LONG TickOffset;
766     LONG MasterOffset;
767     ULONG DpcLastCount;
768     UCHAR ThreadDpcEnable;
769     UCHAR QuantumEnd;
770     UCHAR PrcbPad50;
771     UCHAR IdleSchedule;
772     LONG DpcSetEventRequest;
773 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
774     ULONG KeExceptionDispatchCount;
775 #else
776     LONG PrcbPad40;
777     PVOID DpcThread;
778 #endif
779     KEVENT DpcEvent;
780 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
781     PVOID PrcbPad51;
782 #endif
783     KDPC CallDpc;
784 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
785     LONG ClockKeepAlive;
786     UCHAR ClockCheckSlot;
787     UCHAR ClockPollCycle;
788     UCHAR PrcbPad6[2];
789     LONG DpcWatchdogPeriod;
790     LONG DpcWatchdogCount;
791     UINT64 PrcbPad70[2];
792 #else
793     UINT64 PrcbPad7[4];
794 #endif
795     LIST_ENTRY WaitListHead;
796 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
797     UINT64 WaitLock;
798 #endif
799     ULONG ReadySummary;
800     ULONG QueueIndex;
801 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
802     UINT64 PrcbPad71[12];
803 #endif
804     LIST_ENTRY DispatcherReadyListHead[32];
805     ULONG InterruptCount;
806     ULONG KernelTime;
807     ULONG UserTime;
808     ULONG DpcTime;
809     ULONG InterruptTime;
810     ULONG AdjustDpcThreshold;
811     UCHAR SkipTick;
812     UCHAR DebuggerSavedIRQL;
813     UCHAR PollSlot;
814 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
815     UCHAR PrcbPad80[5];
816     ULONG DpcTimeCount;
817     ULONG DpcTimeLimit;
818     ULONG PeriodicCount;
819     ULONG PeriodicBias;
820     UINT64 PrcbPad81[2];
821 #else
822     UCHAR PrcbPad8[13];
823 #endif
824     struct _KNODE *ParentNode;
825     UINT64 MultiThreadProcessorSet;
826     struct _KPRCB *MultiThreadSetMaster;
827 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
828     UINT64 StartCycles;
829     LONG MmSpinLockOrdering;
830     ULONG PageColor;
831     ULONG NodeColor;
832     ULONG NodeShiftedColor;
833     ULONG SecondaryColorMask;
834 #endif
835     LONG Sleeping;
836 #if (NTDDI_VERSION >= NTDDI_LONGHORN)
837     UINT64 CycleTime;
838     ULONG CcFastMdlReadNoWait;
839     ULONG CcFastMdlReadWait;
840     ULONG CcFastMdlReadNotPossible;
841     ULONG CcMapDataNoWait;
842     ULONG CcMapDataWait;
843     ULONG CcPinMappedDataCount;
844     ULONG CcPinReadNoWait;
845     ULONG CcPinReadWait;
846     ULONG CcMdlReadNoWait;
847     ULONG CcMdlReadWait;
848     ULONG CcLazyWriteHotSpots;
849     ULONG CcLazyWriteIos;
850     ULONG CcLazyWritePages;
851     ULONG CcDataFlushes;
852     ULONG CcDataPages;
853     ULONG CcLostDelayedWrites;
854     ULONG CcFastReadResourceMiss;
855     ULONG CcCopyReadWaitMiss;
856     ULONG CcFastMdlReadResourceMiss;
857     ULONG CcMapDataNoWaitMiss;
858     ULONG CcMapDataWaitMiss;
859     ULONG CcPinReadNoWaitMiss;
860     ULONG CcPinReadWaitMiss;
861     ULONG CcMdlReadNoWaitMiss;
862     ULONG CcMdlReadWaitMiss;
863     ULONG CcReadAheadIos;
864     LONG MmCacheTransitionCount;
865     LONG MmCacheReadCount;
866     LONG MmCacheIoCount;
867     ULONG PrcbPad91[3];
868     PROCESSOR_POWER_STATE PowerState;
869     ULONG KeAlignmentFixupCount;
870     UCHAR VendorString[13];
871     UCHAR PrcbPad10[3];
872     ULONG FeatureBits;
873     LARGE_INTEGER UpdateSignature;
874     KDPC DpcWatchdogDpc;
875     KTIMER DpcWatchdogTimer;
876     CACHE_DESCRIPTOR Cache[5];
877     ULONG CacheCount;
878     ULONG CachedCommit;
879     ULONG CachedResidentAvailable;
880     PVOID HyperPte;
881     PVOID WheaInfo;
882     PVOID EtwSupport;
883     SLIST_HEADER InterruptObjectPool;
884     SLIST_HEADER HypercallPageList;
885     PVOID HypercallPageVirtual;
886     PVOID VirtualApicAssist;
887     UINT64* StatisticsPage;
888     PVOID RateControl;
889     UINT64 CacheProcessorMask[5];
890     UINT64 PackageProcessorSet;
891     UINT64 CoreProcessorSet;
892 #else
893     ULONG PrcbPad90[1];
894     ULONG DebugDpcTime;
895     ULONG PageColor;
896     ULONG NodeColor;
897     ULONG NodeShiftedColor;
898     ULONG SecondaryColorMask;
899     UCHAR PrcbPad9[12];
900     ULONG CcFastReadNoWait;
901     ULONG CcFastReadWait;
902     ULONG CcFastReadNotPossible;
903     ULONG CcCopyReadNoWait;
904     ULONG CcCopyReadWait;
905     ULONG CcCopyReadNoWaitMiss;
906     ULONG KeAlignmentFixupCount;
907     ULONG KeDcacheFlushCount;
908     ULONG KeExceptionDispatchCount;
909     ULONG KeFirstLevelTbFills;
910     ULONG KeFloatingEmulationCount;
911     ULONG KeIcacheFlushCount;
912     ULONG KeSecondLevelTbFills;
913     UCHAR VendorString[13];
914     UCHAR PrcbPad10[2];
915     ULONG FeatureBits;
916     LARGE_INTEGER UpdateSignature;
917     PROCESSOR_POWER_STATE PowerState;
918     CACHE_DESCRIPTOR Cache[5];
919     ULONG CacheCount;
920 #endif
921 #ifdef __REACTOS__
922     ULONG FeatureBitsHigh;
923 #endif
924 } KPRCB, *PKPRCB;
925 
926 //
927 // Processor Control Region
928 //
929 typedef struct _KIPCR
930 {
931     union
932     {
933         NT_TIB NtTib;
934         struct
935         {
936             union _KGDTENTRY64 *GdtBase;
937             struct _KTSS64 *TssBase;
938             ULONG64 UserRsp;
939             struct _KPCR *Self;
940             struct _KPRCB *CurrentPrcb;
941             PKSPIN_LOCK_QUEUE LockArray;
942             PVOID Used_Self;
943         };
944     };
945     union _KIDTENTRY64 *IdtBase;
946     ULONG64 Unused[2];
947     KIRQL Irql;
948     UCHAR SecondLevelCacheAssociativity;
949     UCHAR ObsoleteNumber;
950     UCHAR Fill0;
951     ULONG Unused0[3];
952     USHORT MajorVersion;
953     USHORT MinorVersion;
954     ULONG StallScaleFactor;
955     PVOID Unused1[3];
956     ULONG KernelReserved[15];
957     ULONG SecondLevelCacheSize;
958     ULONG HalReserved[16];
959     ULONG Unused2;
960     ULONG Fill1;
961     PVOID KdVersionBlock; // 0x108
962     PVOID Unused3;
963     ULONG PcrAlign1[24];
964     ULONG Fill2[2]; // 0x178
965     KPRCB Prcb; // 0x180
966 
967     // hack:
968     ULONG ContextSwitches;
969 
970 } KIPCR, *PKIPCR;
971 
972 //
973 // TSS Definition
974 //
975 typedef struct _KiIoAccessMap
976 {
977     UCHAR DirectionMap[32];
978     UCHAR IoMap[8196];
979 } KIIO_ACCESS_MAP;
980 
981 
982 #pragma pack(push,4)
983 typedef struct _KTSS64
984 {
985  /* 000 */  ULONG Reserved0;
986  /* 004 */  UINT64 Rsp0;
987  /* 00c */  UINT64 Rsp1;
988  /* 014 */  UINT64 Rsp2;
989  /* 01c */  UINT64 Ist[8];
990  /* 05c */  UINT64 Reserved1;
991  /* 064 */  USHORT Reserved2;
992  /* 066 */  USHORT IoMapBase;
993 } KTSS64, *PKTSS64;
994 #pragma pack(pop)
995 #define KTSS KTSS64
996 #define PKTSS PKTSS64
997 
998 //
999 // KEXCEPTION_FRAME
1000 //
1001 typedef struct _KEXCEPTION_FRAME
1002 {
1003     ULONG64 P1Home;
1004     ULONG64 P2Home;
1005     ULONG64 P3Home;
1006     ULONG64 P4Home;
1007     ULONG64 P5;
1008 #if (NTDDI_VERSION >= NTDDI_WIN8)
1009     ULONG64 Spare1;
1010 #else
1011     ULONG64 InitialStack;
1012 #endif
1013     M128A Xmm6;
1014     M128A Xmm7;
1015     M128A Xmm8;
1016     M128A Xmm9;
1017     M128A Xmm10;
1018     M128A Xmm11;
1019     M128A Xmm12;
1020     M128A Xmm13;
1021     M128A Xmm14;
1022     M128A Xmm15;
1023     ULONG64 TrapFrame;
1024 #if (NTDDI_VERSION < NTDDI_WIN8)
1025     ULONG64 CallbackStack;
1026 #endif
1027     ULONG64 OutputBuffer;
1028     ULONG64 OutputLength;
1029 #if (NTDDI_VERSION >= NTDDI_WIN8)
1030     ULONG64 Spare2;
1031 #endif
1032     ULONG64 MxCsr;
1033     ULONG64 Rbp;
1034     ULONG64 Rbx;
1035     ULONG64 Rdi;
1036     ULONG64 Rsi;
1037     ULONG64 R12;
1038     ULONG64 R13;
1039     ULONG64 R14;
1040     ULONG64 R15;
1041     ULONG64 Return;
1042 } KEXCEPTION_FRAME, *PKEXCEPTION_FRAME;
1043 
1044 typedef struct _MACHINE_FRAME
1045 {
1046     ULONG64 Rip;
1047     USHORT SegCs;
1048     USHORT Fill1[3];
1049     ULONG EFlags;
1050     ULONG Fill2;
1051     ULONG64 Rsp;
1052     USHORT SegSs;
1053     USHORT Fill3[3];
1054 } MACHINE_FRAME, *PMACHINE_FRAME;
1055 
1056 //
1057 // Defines the Callback Stack Layout for User Mode Callbacks
1058 //
1059 typedef KEXCEPTION_FRAME KCALLOUT_FRAME, *PKCALLOUT_FRAME;
1060 
1061 //
1062 // User side callout frame
1063 //
1064 typedef struct _UCALLOUT_FRAME
1065 {
1066     ULONG64 P1Home;
1067     ULONG64 P2Home;
1068     ULONG64 P3Home;
1069     ULONG64 P4Home;
1070     PVOID Buffer;
1071     ULONG Length;
1072     ULONG ApiNumber;
1073     MACHINE_FRAME MachineFrame;
1074 } UCALLOUT_FRAME, *PUCALLOUT_FRAME; // size = 0x0058
1075 
1076 //
1077 // Stack frame layout for KiUserExceptionDispatcher
1078 // The name is totally made up
1079 //
1080 typedef struct _KUSER_EXCEPTION_STACK
1081 {
1082     CONTEXT Context;
1083     EXCEPTION_RECORD ExceptionRecord;
1084     ULONG64 Alignment;
1085     MACHINE_FRAME MachineFrame;
1086 } KUSER_EXCEPTION_STACK, * PKUSER_EXCEPTION_STACK;
1087 
1088 typedef struct _DISPATCHER_CONTEXT
1089 {
1090     ULONG64 ControlPc;
1091     ULONG64 ImageBase;
1092     struct _RUNTIME_FUNCTION *FunctionEntry;
1093     ULONG64 EstablisherFrame;
1094     ULONG64 TargetIp;
1095     PCONTEXT ContextRecord;
1096     PEXCEPTION_ROUTINE LanguageHandler;
1097     PVOID HandlerData;
1098     struct _UNWIND_HISTORY_TABLE *HistoryTable;
1099     ULONG ScopeIndex;
1100     ULONG Fill0;
1101 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
1102 
1103 typedef struct _KSTART_FRAME
1104 {
1105     ULONG64 P1Home;
1106     ULONG64 P2Home;
1107     ULONG64 P3Home;
1108     ULONG64 P4Home;
1109     ULONG64 Reserved;
1110     ULONG64 Return;
1111 } KSTART_FRAME, *PKSTART_FRAME;
1112 
1113 typedef struct _KSWITCH_FRAME
1114 {
1115     ULONG64 P1Home;
1116     ULONG64 P2Home;
1117     ULONG64 P3Home;
1118     ULONG64 P4Home;
1119     ULONG64 P5Home;
1120     KIRQL ApcBypass;
1121     UCHAR Fill1[7];
1122     ULONG64 Rbp;
1123     ULONG64 Return;
1124 } KSWITCH_FRAME, *PKSWITCH_FRAME;
1125 
1126 #define PROCESSOR_START_FLAG_FORCE_ENABLE_NX 0x0001
1127 typedef struct _KPROCESSOR_START_BLOCK
1128 {
1129     ULONG CompletionFlag; // 0x0004
1130     ULONG Flags; // 0x0008
1131     ULONG Gdt32; // 0x000C
1132     ULONG Idt32; // 0x0012
1133     PVOID Gdt; // 0x0018
1134     // ???
1135     ULONG64 TiledMemoryMap; // 0x0058
1136     UCHAR PmTarget[6]; // 0x0060
1137     UCHAR LmIdentityTarget[6]; // 0x0066
1138     ULONG64 LmTarget; // 0x0070
1139     struct _KPROCESSOR_START_BLOCK *SelfMap; // 0x0078
1140     ULONG64 MsrPat; // 0x0080
1141     ULONG64 MsrEFER; // 0x0088
1142     KPROCESSOR_STATE ProcessorState; // 0x0090
1143 } KPROCESSOR_START_BLOCK, *PKPROCESSOR_START_BLOCK; // size 00640
1144 
1145 //
1146 // Inline function to get current KPRCB
1147 //
1148 FORCEINLINE
1149 struct _KPRCB *
KeGetCurrentPrcb(VOID)1150 KeGetCurrentPrcb(VOID)
1151 {
1152     return (struct _KPRCB *)__readgsqword(FIELD_OFFSET(KIPCR, CurrentPrcb));
1153 }
1154 
1155 #endif
1156 #endif
1157