xref: /reactos/sdk/include/xdk/x86/ke.h (revision 40462c92)
1 $if (_WDMDDK_)
2 /** Kernel definitions for x86 **/
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 PROFILE_LEVEL           27
11 #define CLOCK1_LEVEL            28
12 #define CLOCK2_LEVEL            28
13 #define IPI_LEVEL               29
14 #define POWER_LEVEL             30
15 #define HIGH_LEVEL              31
16 #define CLOCK_LEVEL             CLOCK2_LEVEL
17 
18 #define KIP0PCRADDRESS          0xffdff000
19 #define KI_USER_SHARED_DATA     0xffdf0000
20 #define SharedUserData          ((KUSER_SHARED_DATA * CONST)KI_USER_SHARED_DATA)
21 
22 #define PAGE_SIZE               0x1000
23 #define PAGE_SHIFT              12L
24 #define KeGetDcacheFillSize()   1L
25 
26 #define EFLAG_SIGN              0x8000
27 #define EFLAG_ZERO              0x4000
28 #define EFLAG_SELECT            (EFLAG_SIGN | EFLAG_ZERO)
29 
30 #define RESULT_NEGATIVE         ((EFLAG_SIGN & ~EFLAG_ZERO) & EFLAG_SELECT)
31 #define RESULT_ZERO             ((~EFLAG_SIGN & EFLAG_ZERO) & EFLAG_SELECT)
32 #define RESULT_POSITIVE         ((~EFLAG_SIGN & ~EFLAG_ZERO) & EFLAG_SELECT)
33 
34 
35 typedef struct _KFLOATING_SAVE {
36   ULONG ControlWord;
37   ULONG StatusWord;
38   ULONG ErrorOffset;
39   ULONG ErrorSelector;
40   ULONG DataOffset;
41   ULONG DataSelector;
42   ULONG Cr0NpxState;
43   ULONG Spare1;
44 } KFLOATING_SAVE, *PKFLOATING_SAVE;
45 
46 extern NTKERNELAPI volatile KSYSTEM_TIME KeTickCount;
47 
48 #define YieldProcessor _mm_pause
49 
50 FORCEINLINE
51 VOID
52 KeMemoryBarrier(VOID)
53 {
54   LONG Barrier, *Dummy = &Barrier;
55   UNREFERENCED_LOCAL_VARIABLE(Dummy);
56 
57 #if defined(__GNUC__)
58   __asm__ __volatile__ ("xchg %%eax, %0" : : "m" (Barrier) : "%eax");
59 #elif defined(_MSC_VER)
60   __asm xchg [Barrier], eax
61 #endif
62 }
63 
64 #define KeMemoryBarrierWithoutFence() _ReadWriteBarrier()
65 
66 _IRQL_requires_max_(HIGH_LEVEL)
67 _IRQL_saves_
68 NTHALAPI
69 KIRQL
70 NTAPI
71 KeGetCurrentIrql(VOID);
72 
73 _IRQL_requires_max_(HIGH_LEVEL)
74 NTHALAPI
75 VOID
76 FASTCALL
77 KfLowerIrql(
78   _In_ _IRQL_restores_ _Notliteral_ KIRQL NewIrql);
79 #define KeLowerIrql(a) KfLowerIrql(a)
80 
81 _IRQL_requires_max_(HIGH_LEVEL)
82 _IRQL_raises_(NewIrql)
83 _IRQL_saves_
84 NTHALAPI
85 KIRQL
86 FASTCALL
87 KfRaiseIrql(
88   _In_ KIRQL NewIrql);
89 #define KeRaiseIrql(a,b) *(b) = KfRaiseIrql(a)
90 
91 _IRQL_requires_max_(DISPATCH_LEVEL)
92 _IRQL_saves_
93 _IRQL_raises_(DISPATCH_LEVEL)
94 NTHALAPI
95 KIRQL
96 NTAPI
97 KeRaiseIrqlToDpcLevel(VOID);
98 
99 NTHALAPI
100 KIRQL
101 NTAPI
102 KeRaiseIrqlToSynchLevel(VOID);
103 
104 _Requires_lock_not_held_(*SpinLock)
105 _Acquires_lock_(*SpinLock)
106 _IRQL_requires_max_(DISPATCH_LEVEL)
107 _IRQL_saves_
108 _IRQL_raises_(DISPATCH_LEVEL)
109 NTHALAPI
110 KIRQL
111 FASTCALL
112 KfAcquireSpinLock(
113   _Inout_ PKSPIN_LOCK SpinLock);
114 #define KeAcquireSpinLock(a,b) *(b) = KfAcquireSpinLock(a)
115 
116 _Requires_lock_held_(*SpinLock)
117 _Releases_lock_(*SpinLock)
118 _IRQL_requires_(DISPATCH_LEVEL)
119 NTHALAPI
120 VOID
121 FASTCALL
122 KfReleaseSpinLock(
123   _Inout_ PKSPIN_LOCK SpinLock,
124   _In_ _IRQL_restores_ KIRQL NewIrql);
125 #define KeReleaseSpinLock(a,b) KfReleaseSpinLock(a,b)
126 
127 _Requires_lock_not_held_(*SpinLock)
128 _Acquires_lock_(*SpinLock)
129 _IRQL_requires_min_(DISPATCH_LEVEL)
130 NTKERNELAPI
131 VOID
132 FASTCALL
133 KefAcquireSpinLockAtDpcLevel(
134   _Inout_ PKSPIN_LOCK SpinLock);
135 #define KeAcquireSpinLockAtDpcLevel(SpinLock) KefAcquireSpinLockAtDpcLevel(SpinLock)
136 
137 _Requires_lock_held_(*SpinLock)
138 _Releases_lock_(*SpinLock)
139 _IRQL_requires_min_(DISPATCH_LEVEL)
140 NTKERNELAPI
141 VOID
142 FASTCALL
143 KefReleaseSpinLockFromDpcLevel(
144   _Inout_ PKSPIN_LOCK SpinLock);
145 #define KeReleaseSpinLockFromDpcLevel(SpinLock) KefReleaseSpinLockFromDpcLevel(SpinLock)
146 
147 NTSYSAPI
148 PKTHREAD
149 NTAPI
150 KeGetCurrentThread(VOID);
151 
152 _Always_(_Post_satisfies_(return<=0))
153 _Must_inspect_result_
154 _IRQL_requires_max_(DISPATCH_LEVEL)
155 _Kernel_float_saved_
156 _At_(*FloatSave, _Kernel_requires_resource_not_held_(FloatState) _Kernel_acquires_resource_(FloatState))
157 NTKERNELAPI
158 NTSTATUS
159 NTAPI
160 KeSaveFloatingPointState(
161   _Out_ PKFLOATING_SAVE FloatSave);
162 
163 _Success_(1)
164 _Kernel_float_restored_
165 _At_(*FloatSave, _Kernel_requires_resource_held_(FloatState) _Kernel_releases_resource_(FloatState))
166 NTKERNELAPI
167 NTSTATUS
168 NTAPI
169 KeRestoreFloatingPointState(
170   _In_ PKFLOATING_SAVE FloatSave);
171 
172 #if (NTDDI_VERSION >= NTDDI_WIN7)
173 FORCEINLINE
174 ULONG
175 NTAPI
176 KeGetCurrentProcessorIndex(VOID)
177 {
178     return __readfsbyte(0x51);
179 }
180 #endif
181 
182 /* VOID
183  * KeFlushIoBuffers(
184  *   IN PMDL Mdl,
185  *   IN BOOLEAN ReadOperation,
186  *   IN BOOLEAN DmaOperation)
187  */
188 #define KeFlushIoBuffers(_Mdl, _ReadOperation, _DmaOperation)
189 
190 /* x86 and x64 performs a 0x2C interrupt */
191 #define DbgRaiseAssertionFailure __int2c
192 
193 FORCEINLINE
194 VOID
195 _KeQueryTickCount(
196   OUT PLARGE_INTEGER CurrentCount)
197 {
198   for (;;) {
199 #ifdef NONAMELESSUNION
200     CurrentCount->s.HighPart = KeTickCount.High1Time;
201     CurrentCount->s.LowPart = KeTickCount.LowPart;
202     if (CurrentCount->s.HighPart == KeTickCount.High2Time) break;
203 #else
204     CurrentCount->HighPart = KeTickCount.High1Time;
205     CurrentCount->LowPart = KeTickCount.LowPart;
206     if (CurrentCount->HighPart == KeTickCount.High2Time) break;
207 #endif
208     YieldProcessor();
209   }
210 }
211 #define KeQueryTickCount(CurrentCount) _KeQueryTickCount(CurrentCount)
212 
213 $endif (_WDMDDK_)
214 $if (_NTDDK_)
215 
216 #define PAUSE_PROCESSOR YieldProcessor();
217 
218 #define KERNEL_STACK_SIZE                   12288
219 #define KERNEL_LARGE_STACK_SIZE             61440
220 #define KERNEL_LARGE_STACK_COMMIT           12288
221 
222 #define SIZE_OF_80387_REGISTERS   80
223 
224 #if !defined(RC_INVOKED)
225 
226 #define CONTEXT_i386               0x10000
227 #define CONTEXT_i486               0x10000
228 #define CONTEXT_CONTROL            (CONTEXT_i386|0x00000001L)
229 #define CONTEXT_INTEGER            (CONTEXT_i386|0x00000002L)
230 #define CONTEXT_SEGMENTS           (CONTEXT_i386|0x00000004L)
231 #define CONTEXT_FLOATING_POINT     (CONTEXT_i386|0x00000008L)
232 #define CONTEXT_DEBUG_REGISTERS    (CONTEXT_i386|0x00000010L)
233 #define CONTEXT_EXTENDED_REGISTERS (CONTEXT_i386|0x00000020L)
234 
235 #define CONTEXT_FULL (CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_SEGMENTS)
236 #define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS |  \
237                      CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS |      \
238                      CONTEXT_EXTENDED_REGISTERS)
239 
240 #define CONTEXT_XSTATE          (CONTEXT_i386 | 0x00000040L)
241 
242 #endif /* !defined(RC_INVOKED) */
243 
244 typedef struct _FLOATING_SAVE_AREA {
245   ULONG ControlWord;
246   ULONG StatusWord;
247   ULONG TagWord;
248   ULONG ErrorOffset;
249   ULONG ErrorSelector;
250   ULONG DataOffset;
251   ULONG DataSelector;
252   UCHAR RegisterArea[SIZE_OF_80387_REGISTERS];
253   ULONG Cr0NpxState;
254 } FLOATING_SAVE_AREA, *PFLOATING_SAVE_AREA;
255 
256 #include "pshpack4.h"
257 typedef struct _CONTEXT {
258   ULONG ContextFlags;
259   ULONG Dr0;
260   ULONG Dr1;
261   ULONG Dr2;
262   ULONG Dr3;
263   ULONG Dr6;
264   ULONG Dr7;
265   FLOATING_SAVE_AREA FloatSave;
266   ULONG SegGs;
267   ULONG SegFs;
268   ULONG SegEs;
269   ULONG SegDs;
270   ULONG Edi;
271   ULONG Esi;
272   ULONG Ebx;
273   ULONG Edx;
274   ULONG Ecx;
275   ULONG Eax;
276   ULONG Ebp;
277   ULONG Eip;
278   ULONG SegCs;
279   ULONG EFlags;
280   ULONG Esp;
281   ULONG SegSs;
282   UCHAR ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION];
283 } CONTEXT;
284 #include "poppack.h"
285 
286 #define KeGetPcr()                      PCR
287 
288 #define PCR_MINOR_VERSION 1
289 #define PCR_MAJOR_VERSION 1
290 
291 typedef struct _KPCR {
292   union {
293     NT_TIB NtTib;
294     struct {
295       struct _EXCEPTION_REGISTRATION_RECORD *Used_ExceptionList;
296       PVOID Used_StackBase;
297       PVOID Spare2;
298       PVOID TssCopy;
299       ULONG ContextSwitches;
300       KAFFINITY SetMemberCopy;
301       PVOID Used_Self;
302     };
303   };
304   struct _KPCR *SelfPcr;
305   struct _KPRCB *Prcb;
306   KIRQL Irql;
307   ULONG IRR;
308   ULONG IrrActive;
309   ULONG IDR;
310   PVOID KdVersionBlock;
311   struct _KIDTENTRY *IDT;
312   struct _KGDTENTRY *GDT;
313   struct _KTSS *TSS;
314   USHORT MajorVersion;
315   USHORT MinorVersion;
316   KAFFINITY SetMember;
317   ULONG StallScaleFactor;
318   UCHAR SpareUnused;
319   UCHAR Number;
320   UCHAR Spare0;
321   UCHAR SecondLevelCacheAssociativity;
322   ULONG VdmAlert;
323   ULONG KernelReserved[14];
324   ULONG SecondLevelCacheSize;
325   ULONG HalReserved[16];
326 } KPCR, *PKPCR;
327 
328 #if (NTDDI_VERSION >= NTDDI_WIN7)
329 _CRT_DEPRECATE_TEXT("KeGetCurrentProcessorNumber is deprecated. Use KeGetCurrentProcessorNumberEx or KeGetCurrentProcessorIndex instead.")
330 #endif
331 FORCEINLINE
332 ULONG
333 KeGetCurrentProcessorNumber(VOID)
334 {
335     return (ULONG)__readfsbyte(FIELD_OFFSET(KPCR, Number));
336 }
337 
338 $endif (_NTDDK_)
339 
340 
341 
342 
343