* Sync up to trunk r55544.
[reactos.git] / include / xdk / amd64 / ke.h
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 ((PKUSER_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 ULONG Dummy;
32 } KFLOATING_SAVE, *PKFLOATING_SAVE;
33
34 typedef XSAVE_FORMAT XMM_SAVE_AREA32, *PXMM_SAVE_AREA32;
35
36 #define KeQueryInterruptTime() \
37 (*(volatile ULONG64*)SharedInterruptTime)
38
39 #define KeQuerySystemTime(CurrentCount) \
40 *(ULONG64*)(CurrentCount) = *(volatile ULONG64*)SharedSystemTime
41
42 #define KeQueryTickCount(CurrentCount) \
43 *(ULONG64*)(CurrentCount) = *(volatile ULONG64*)SharedTickCount
44
45 #define KeGetDcacheFillSize() 1L
46
47 #define YieldProcessor _mm_pause
48 #define FastFence __faststorefence
49 #define LoadFence _mm_lfence
50 #define MemoryFence _mm_mfence
51 #define StoreFence _mm_sfence
52 #define LFENCE_ACQUIRE() LoadFence()
53
54 FORCEINLINE
55 VOID
56 KeMemoryBarrier(VOID)
57 {
58 // FIXME: Do we really need lfence after the __faststorefence ?
59 FastFence();
60 LFENCE_ACQUIRE();
61 }
62
63 #define KeMemoryBarrierWithoutFence() _ReadWriteBarrier()
64
65 FORCEINLINE
66 KIRQL
67 KeGetCurrentIrql(VOID)
68 {
69 return (KIRQL)__readcr8();
70 }
71
72 FORCEINLINE
73 VOID
74 KeLowerIrql(IN KIRQL NewIrql)
75 {
76 ASSERT((KIRQL)__readcr8() >= NewIrql);
77 __writecr8(NewIrql);
78 }
79
80 FORCEINLINE
81 KIRQL
82 KfRaiseIrql(IN KIRQL NewIrql)
83 {
84 KIRQL OldIrql;
85
86 OldIrql = (KIRQL)__readcr8();
87 ASSERT(OldIrql <= NewIrql);
88 __writecr8(NewIrql);
89 return OldIrql;
90 }
91 #define KeRaiseIrql(a,b) *(b) = KfRaiseIrql(a)
92
93 FORCEINLINE
94 KIRQL
95 KeRaiseIrqlToDpcLevel(VOID)
96 {
97 return KfRaiseIrql(DISPATCH_LEVEL);
98 }
99
100 FORCEINLINE
101 KIRQL
102 KeRaiseIrqlToSynchLevel(VOID)
103 {
104 return KfRaiseIrql(12); // SYNCH_LEVEL = IPI_LEVEL - 2
105 }
106
107 FORCEINLINE
108 PKTHREAD
109 KeGetCurrentThread(VOID)
110 {
111 return (struct _KTHREAD *)__readgsqword(0x188);
112 }
113
114 FORCEINLINE
115 NTSTATUS
116 KeSaveFloatingPointState(PVOID FloatingState)
117 {
118 UNREFERENCED_PARAMETER(FloatingState);
119 return STATUS_SUCCESS;
120 }
121
122 FORCEINLINE
123 NTSTATUS
124 KeRestoreFloatingPointState(PVOID FloatingState)
125 {
126 UNREFERENCED_PARAMETER(FloatingState);
127 return STATUS_SUCCESS;
128 }
129
130 /* VOID
131 * KeFlushIoBuffers(
132 * IN PMDL Mdl,
133 * IN BOOLEAN ReadOperation,
134 * IN BOOLEAN DmaOperation)
135 */
136 #define KeFlushIoBuffers(_Mdl, _ReadOperation, _DmaOperation)
137
138 /* x86 and x64 performs a 0x2C interrupt */
139 #define DbgRaiseAssertionFailure __int2c
140
141 $endif /* _WDMDDK_ */
142 $if (_NTDDK_)
143
144 #define PAUSE_PROCESSOR YieldProcessor();
145
146 #define KERNEL_STACK_SIZE 0x6000
147 #define KERNEL_LARGE_STACK_SIZE 0x12000
148 #define KERNEL_LARGE_STACK_COMMIT KERNEL_STACK_SIZE
149
150 #define KERNEL_MCA_EXCEPTION_STACK_SIZE 0x2000
151
152 #define EXCEPTION_READ_FAULT 0
153 #define EXCEPTION_WRITE_FAULT 1
154 #define EXCEPTION_EXECUTE_FAULT 8
155
156 #if !defined(RC_INVOKED)
157
158 #define CONTEXT_AMD64 0x100000
159
160 #define CONTEXT_CONTROL (CONTEXT_AMD64 | 0x1L)
161 #define CONTEXT_INTEGER (CONTEXT_AMD64 | 0x2L)
162 #define CONTEXT_SEGMENTS (CONTEXT_AMD64 | 0x4L)
163 #define CONTEXT_FLOATING_POINT (CONTEXT_AMD64 | 0x8L)
164 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_AMD64 | 0x10L)
165
166 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
167 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS)
168
169 #define CONTEXT_XSTATE (CONTEXT_AMD64 | 0x20L)
170
171 #define CONTEXT_EXCEPTION_ACTIVE 0x8000000
172 #define CONTEXT_SERVICE_ACTIVE 0x10000000
173 #define CONTEXT_EXCEPTION_REQUEST 0x40000000
174 #define CONTEXT_EXCEPTION_REPORTING 0x80000000
175
176 #endif /* !defined(RC_INVOKED) */
177
178 #define INITIAL_MXCSR 0x1f80
179 #define INITIAL_FPCSR 0x027f
180
181 typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
182 ULONG64 P1Home;
183 ULONG64 P2Home;
184 ULONG64 P3Home;
185 ULONG64 P4Home;
186 ULONG64 P5Home;
187 ULONG64 P6Home;
188 ULONG ContextFlags;
189 ULONG MxCsr;
190 USHORT SegCs;
191 USHORT SegDs;
192 USHORT SegEs;
193 USHORT SegFs;
194 USHORT SegGs;
195 USHORT SegSs;
196 ULONG EFlags;
197 ULONG64 Dr0;
198 ULONG64 Dr1;
199 ULONG64 Dr2;
200 ULONG64 Dr3;
201 ULONG64 Dr6;
202 ULONG64 Dr7;
203 ULONG64 Rax;
204 ULONG64 Rcx;
205 ULONG64 Rdx;
206 ULONG64 Rbx;
207 ULONG64 Rsp;
208 ULONG64 Rbp;
209 ULONG64 Rsi;
210 ULONG64 Rdi;
211 ULONG64 R8;
212 ULONG64 R9;
213 ULONG64 R10;
214 ULONG64 R11;
215 ULONG64 R12;
216 ULONG64 R13;
217 ULONG64 R14;
218 ULONG64 R15;
219 ULONG64 Rip;
220 union {
221 XMM_SAVE_AREA32 FltSave;
222 struct {
223 M128A Header[2];
224 M128A Legacy[8];
225 M128A Xmm0;
226 M128A Xmm1;
227 M128A Xmm2;
228 M128A Xmm3;
229 M128A Xmm4;
230 M128A Xmm5;
231 M128A Xmm6;
232 M128A Xmm7;
233 M128A Xmm8;
234 M128A Xmm9;
235 M128A Xmm10;
236 M128A Xmm11;
237 M128A Xmm12;
238 M128A Xmm13;
239 M128A Xmm14;
240 M128A Xmm15;
241 } DUMMYSTRUCTNAME;
242 } DUMMYUNIONNAME;
243 M128A VectorRegister[26];
244 ULONG64 VectorControl;
245 ULONG64 DebugControl;
246 ULONG64 LastBranchToRip;
247 ULONG64 LastBranchFromRip;
248 ULONG64 LastExceptionToRip;
249 ULONG64 LastExceptionFromRip;
250 } CONTEXT;
251
252 #define PCR_MINOR_VERSION 1
253 #define PCR_MAJOR_VERSION 1
254
255 typedef struct _KPCR
256 {
257 _ANONYMOUS_UNION union
258 {
259 NT_TIB NtTib;
260 _ANONYMOUS_STRUCT struct
261 {
262 union _KGDTENTRY64 *GdtBase;
263 struct _KTSS64 *TssBase;
264 ULONG64 UserRsp;
265 struct _KPCR *Self;
266 struct _KPRCB *CurrentPrcb;
267 PKSPIN_LOCK_QUEUE LockArray;
268 PVOID Used_Self;
269 };
270 };
271 union _KIDTENTRY64 *IdtBase;
272 ULONG64 Unused[2];
273 KIRQL Irql;
274 UCHAR SecondLevelCacheAssociativity;
275 UCHAR ObsoleteNumber;
276 UCHAR Fill0;
277 ULONG Unused0[3];
278 USHORT MajorVersion;
279 USHORT MinorVersion;
280 ULONG StallScaleFactor;
281 PVOID Unused1[3];
282 ULONG KernelReserved[15];
283 ULONG SecondLevelCacheSize;
284 ULONG HalReserved[16];
285 ULONG Unused2;
286 PVOID KdVersionBlock;
287 PVOID Unused3;
288 ULONG PcrAlign1[24];
289 } KPCR, *PKPCR;
290
291 FORCEINLINE
292 PKPCR
293 KeGetPcr(VOID)
294 {
295 return (PKPCR)__readgsqword(FIELD_OFFSET(KPCR, Self));
296 }
297
298 FORCEINLINE
299 ULONG
300 KeGetCurrentProcessorNumber(VOID)
301 {
302 return (ULONG)__readgsword(0x184);
303 }
304
305 $endif /* _NTDDK_ */