xref: /reactos/sdk/include/xdk/amd64/ke.h (revision cdf90707)
1 $if (_WDMDDK_)
2 /** Kernel definitions for AMD64 **/
3 
4 /* Interrupt request levels */
5 #define PASSIVE_LEVEL           0
6 #define LOW_LEVEL               0
7 #define APC_LEVEL               1
8 #define DISPATCH_LEVEL          2
9 #define CMCI_LEVEL              5
10 #define CLOCK_LEVEL             13
11 #define IPI_LEVEL               14
12 #define DRS_LEVEL               14
13 #define POWER_LEVEL             14
14 #define PROFILE_LEVEL           15
15 #define HIGH_LEVEL              15
16 
17 #define KI_USER_SHARED_DATA     0xFFFFF78000000000ULL
18 #define SharedUserData          ((KUSER_SHARED_DATA * const)KI_USER_SHARED_DATA)
19 #define SharedInterruptTime     (KI_USER_SHARED_DATA + 0x8)
20 #define SharedSystemTime        (KI_USER_SHARED_DATA + 0x14)
21 #define SharedTickCount         (KI_USER_SHARED_DATA + 0x320)
22 
23 #define PAGE_SIZE               0x1000
24 #define PAGE_SHIFT              12L
25 
26 #define EFLAG_SIGN              0x8000
27 #define EFLAG_ZERO              0x4000
28 #define EFLAG_SELECT            (EFLAG_SIGN | EFLAG_ZERO)
29 
30 typedef struct _KFLOATING_SAVE
31 {
32     ULONG Dummy;
33 } KFLOATING_SAVE, *PKFLOATING_SAVE;
34 
35 typedef XSAVE_FORMAT XMM_SAVE_AREA32, *PXMM_SAVE_AREA32;
36 
37 #define KeQueryInterruptTime() \
38     (*(volatile ULONG64*)SharedInterruptTime)
39 
40 #define KeQuerySystemTime(CurrentCount) \
41     *(ULONG64*)(CurrentCount) = *(volatile ULONG64*)SharedSystemTime
42 
43 #define KeQueryTickCount(CurrentCount) \
44     *(ULONG64*)(CurrentCount) = *(volatile ULONG64*)SharedTickCount
45 
46 #define KeGetDcacheFillSize() 1L
47 
48 #define YieldProcessor _mm_pause
49 #define MemoryBarrier __faststorefence
50 #define FastFence __faststorefence
51 #define LoadFence _mm_lfence
52 #define MemoryFence _mm_mfence
53 #define StoreFence _mm_sfence
54 #define LFENCE_ACQUIRE() LoadFence()
55 
56 FORCEINLINE
57 VOID
58 KeMemoryBarrier(
59     VOID)
60 {
61     // FIXME: Do we really need lfence after the __faststorefence ?
62     FastFence();
63     LFENCE_ACQUIRE();
64 }
65 
66 #define KeMemoryBarrierWithoutFence() _ReadWriteBarrier()
67 
68 _IRQL_requires_max_(HIGH_LEVEL)
69 _IRQL_saves_
70 FORCEINLINE
71 KIRQL
72 KeGetCurrentIrql(VOID)
73 {
74     return (KIRQL)__readcr8();
75 }
76 
77 _IRQL_requires_max_(HIGH_LEVEL)
78 FORCEINLINE
79 VOID
80 KeLowerIrql(
81     _In_ _IRQL_restores_ _Notliteral_ KIRQL NewIrql)
82 {
83     //ASSERT((KIRQL)__readcr8() >= NewIrql);
84     __writecr8(NewIrql);
85 }
86 
87 _IRQL_requires_max_(HIGH_LEVEL)
88 _IRQL_raises_(NewIrql)
89 _IRQL_saves_
90 FORCEINLINE
91 KIRQL
92 KfRaiseIrql(
93     _In_ KIRQL NewIrql)
94 {
95     KIRQL OldIrql;
96 
97     OldIrql = (KIRQL)__readcr8();
98     //ASSERT(OldIrql <= NewIrql);
99     __writecr8(NewIrql);
100     return OldIrql;
101 }
102 #define KeRaiseIrql(a,b) *(b) = KfRaiseIrql(a)
103 
104 _IRQL_requires_max_(DISPATCH_LEVEL)
105 _IRQL_saves_
106 _IRQL_raises_(DISPATCH_LEVEL)
107 FORCEINLINE
108 KIRQL
109 KeRaiseIrqlToDpcLevel(
110     VOID)
111 {
112     return KfRaiseIrql(DISPATCH_LEVEL);
113 }
114 
115 FORCEINLINE
116 KIRQL
117 KeRaiseIrqlToSynchLevel(VOID)
118 {
119     return KfRaiseIrql(12); // SYNCH_LEVEL = IPI_LEVEL - 2
120 }
121 
122 FORCEINLINE
123 PKTHREAD
124 KeGetCurrentThread(VOID)
125 {
126     return (struct _KTHREAD *)__readgsqword(0x188);
127 }
128 
129 _Always_(_Post_satisfies_(return<=0))
130 _Must_inspect_result_
131 _IRQL_requires_max_(DISPATCH_LEVEL)
132 _Kernel_float_saved_
133 _At_(*FloatSave, _Kernel_requires_resource_not_held_(FloatState) _Kernel_acquires_resource_(FloatState))
134 FORCEINLINE
135 NTSTATUS
136 KeSaveFloatingPointState(
137     _Out_ PKFLOATING_SAVE FloatSave)
138 {
139     UNREFERENCED_PARAMETER(FloatSave);
140     return STATUS_SUCCESS;
141 }
142 
143 _Success_(1)
144 _Kernel_float_restored_
145 _At_(*FloatSave, _Kernel_requires_resource_held_(FloatState) _Kernel_releases_resource_(FloatState))
146 FORCEINLINE
147 NTSTATUS
148 KeRestoreFloatingPointState(
149     _In_ PKFLOATING_SAVE FloatSave)
150 {
151     UNREFERENCED_PARAMETER(FloatSave);
152     return STATUS_SUCCESS;
153 }
154 
155 #if (NTDDI_VERSION >= NTDDI_WIN7)
156 FORCEINLINE
157 ULONG
158 KeGetCurrentProcessorIndex(VOID)
159 {
160     return __readgsdword(0x1a4);
161 }
162 #endif
163 
164 /* VOID
165  * KeFlushIoBuffers(
166  *   IN PMDL Mdl,
167  *   IN BOOLEAN ReadOperation,
168  *   IN BOOLEAN DmaOperation)
169  */
170 #define KeFlushIoBuffers(_Mdl, _ReadOperation, _DmaOperation)
171 
172 /* x86 and x64 performs a 0x2C interrupt */
173 #define DbgRaiseAssertionFailure __int2c
174 
175 $endif /* _WDMDDK_ */
176 $if (_NTDDK_)
177 
178 #define PAUSE_PROCESSOR YieldProcessor();
179 
180 #define KERNEL_STACK_SIZE 0x6000
181 #define KERNEL_LARGE_STACK_SIZE 0x12000
182 #define KERNEL_LARGE_STACK_COMMIT KERNEL_STACK_SIZE
183 
184 #define KERNEL_MCA_EXCEPTION_STACK_SIZE 0x2000
185 
186 #define EXCEPTION_READ_FAULT    0
187 #define EXCEPTION_WRITE_FAULT   1
188 #define EXCEPTION_EXECUTE_FAULT 8
189 
190 #if !defined(RC_INVOKED)
191 
192 #define CONTEXT_AMD64 0x100000
193 
194 #define CONTEXT_CONTROL (CONTEXT_AMD64 | 0x1L)
195 #define CONTEXT_INTEGER (CONTEXT_AMD64 | 0x2L)
196 #define CONTEXT_SEGMENTS (CONTEXT_AMD64 | 0x4L)
197 #define CONTEXT_FLOATING_POINT (CONTEXT_AMD64 | 0x8L)
198 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_AMD64 | 0x10L)
199 
200 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
201 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS)
202 
203 #define CONTEXT_XSTATE (CONTEXT_AMD64 | 0x40L)
204 
205 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000
206 #define CONTEXT_SERVICE_ACTIVE 0x10000000
207 #define CONTEXT_EXCEPTION_REQUEST 0x40000000
208 #define CONTEXT_EXCEPTION_REPORTING 0x80000000
209 
210 #endif /* !defined(RC_INVOKED) */
211 
212 #define INITIAL_MXCSR                  0x1f80
213 #define INITIAL_FPCSR                  0x027f
214 
215 typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
216   ULONG64 P1Home;
217   ULONG64 P2Home;
218   ULONG64 P3Home;
219   ULONG64 P4Home;
220   ULONG64 P5Home;
221   ULONG64 P6Home;
222   ULONG ContextFlags;
223   ULONG MxCsr;
224   USHORT SegCs;
225   USHORT SegDs;
226   USHORT SegEs;
227   USHORT SegFs;
228   USHORT SegGs;
229   USHORT SegSs;
230   ULONG EFlags;
231   ULONG64 Dr0;
232   ULONG64 Dr1;
233   ULONG64 Dr2;
234   ULONG64 Dr3;
235   ULONG64 Dr6;
236   ULONG64 Dr7;
237   ULONG64 Rax;
238   ULONG64 Rcx;
239   ULONG64 Rdx;
240   ULONG64 Rbx;
241   ULONG64 Rsp;
242   ULONG64 Rbp;
243   ULONG64 Rsi;
244   ULONG64 Rdi;
245   ULONG64 R8;
246   ULONG64 R9;
247   ULONG64 R10;
248   ULONG64 R11;
249   ULONG64 R12;
250   ULONG64 R13;
251   ULONG64 R14;
252   ULONG64 R15;
253   ULONG64 Rip;
254   union {
255     XMM_SAVE_AREA32 FltSave;
256     struct {
257       M128A Header[2];
258       M128A Legacy[8];
259       M128A Xmm0;
260       M128A Xmm1;
261       M128A Xmm2;
262       M128A Xmm3;
263       M128A Xmm4;
264       M128A Xmm5;
265       M128A Xmm6;
266       M128A Xmm7;
267       M128A Xmm8;
268       M128A Xmm9;
269       M128A Xmm10;
270       M128A Xmm11;
271       M128A Xmm12;
272       M128A Xmm13;
273       M128A Xmm14;
274       M128A Xmm15;
275     } DUMMYSTRUCTNAME;
276   } DUMMYUNIONNAME;
277   M128A VectorRegister[26];
278   ULONG64 VectorControl;
279   ULONG64 DebugControl;
280   ULONG64 LastBranchToRip;
281   ULONG64 LastBranchFromRip;
282   ULONG64 LastExceptionToRip;
283   ULONG64 LastExceptionFromRip;
284 } CONTEXT;
285 
286 #define PCR_MINOR_VERSION 1
287 #define PCR_MAJOR_VERSION 1
288 
289 typedef struct _KPCR
290 {
291     _ANONYMOUS_UNION union
292     {
293         NT_TIB NtTib;
294         _ANONYMOUS_STRUCT struct
295         {
296             union _KGDTENTRY64 *GdtBase;
297             struct _KTSS64 *TssBase;
298             ULONG64 UserRsp;
299             struct _KPCR *Self;
300             struct _KPRCB *CurrentPrcb;
301             PKSPIN_LOCK_QUEUE LockArray;
302             PVOID Used_Self;
303         };
304     };
305     union _KIDTENTRY64 *IdtBase;
306     ULONG64 Unused[2];
307     KIRQL Irql;
308     UCHAR SecondLevelCacheAssociativity;
309     UCHAR ObsoleteNumber;
310     UCHAR Fill0;
311     ULONG Unused0[3];
312     USHORT MajorVersion;
313     USHORT MinorVersion;
314     ULONG StallScaleFactor;
315     PVOID Unused1[3];
316     ULONG KernelReserved[15];
317     ULONG SecondLevelCacheSize;
318     ULONG HalReserved[16];
319     ULONG Unused2;
320     PVOID KdVersionBlock;
321     PVOID Unused3;
322     ULONG PcrAlign1[24];
323 } KPCR, *PKPCR;
324 
325 FORCEINLINE
326 PKPCR
327 KeGetPcr(VOID)
328 {
329     return (PKPCR)__readgsqword(FIELD_OFFSET(KPCR, Self));
330 }
331 
332 #if (NTDDI_VERSION >= NTDDI_WIN7)
333 _CRT_DEPRECATE_TEXT("KeGetCurrentProcessorNumber is deprecated. Use KeGetCurrentProcessorNumberEx or KeGetCurrentProcessorIndex instead.")
334 #endif
335 FORCEINLINE
336 ULONG
337 KeGetCurrentProcessorNumber(VOID)
338 {
339     return __readgsbyte(0x184);
340 }
341 
342 $endif /* _NTDDK_ */
343