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