- MSVC Build Fixes
[reactos.git] / reactos / ntoskrnl / include / internal / i386 / ke.h
1 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
2 #define __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
3
4 #if __GNUC__ >=3
5 #pragma GCC system_header
6 #endif
7
8 #define X86_EFLAGS_TF 0x00000100 /* Trap flag */
9 #define X86_EFLAGS_IF 0x00000200 /* Interrupt Enable flag */
10 #define X86_EFLAGS_IOPL 0x00003000 /* I/O Privilege Level bits */
11 #define X86_EFLAGS_NT 0x00004000 /* Nested Task flag */
12 #define X86_EFLAGS_RF 0x00010000 /* Resume flag */
13 #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
14 #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
15
16 #define X86_CR0_PE 0x00000001 /* enable Protected Mode */
17 #define X86_CR0_NE 0x00000020 /* enable native FPU error reporting */
18 #define X86_CR0_TS 0x00000008 /* enable exception on FPU instruction for task switch */
19 #define X86_CR0_EM 0x00000004 /* enable FPU emulation (disable FPU) */
20 #define X86_CR0_MP 0x00000002 /* enable FPU monitoring */
21 #define X86_CR0_WP 0x00010000 /* enable Write Protect (copy on write) */
22 #define X86_CR0_PG 0x80000000 /* enable Paging */
23
24 #define X86_CR4_PAE 0x00000020 /* enable physical address extensions */
25 #define X86_CR4_PGE 0x00000080 /* enable global pages */
26 #define X86_CR4_OSFXSR 0x00000200 /* enable FXSAVE/FXRSTOR instructions */
27 #define X86_CR4_OSXMMEXCPT 0x00000400 /* enable #XF exception */
28
29 #define X86_FEATURE_VME 0x00000002 /* Virtual 8086 Extensions are present */
30 #define X86_FEATURE_TSC 0x00000010 /* time stamp counters are present */
31 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
32 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
33 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
34 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
35 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
36 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
37 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
38 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
39 #define X86_FEATURE_HT 0x10000000 /* Hyper-Threading present */
40
41 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
42 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
43
44 #define DR7_ACTIVE 0x00000055 /* If any of these bits are set, a Dr is active */
45
46 #define FRAME_EDITED 0xFFF8
47
48 #define WE_DO_NOT_SPEAK_ABOUT_THE_V86_HACK 1
49
50 #ifndef __ASM__
51
52 extern ULONG Ke386CacheAlignment;
53
54 struct _KPCR;
55 VOID
56 KiInitializeGdt(struct _KPCR* Pcr);
57 VOID
58 Ki386ApplicationProcessorInitializeTSS(VOID);
59 VOID
60 NTAPI
61 Ki386InitializeTss(VOID);
62 VOID
63 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
64 VOID
65 Ki386InitializeLdt(VOID);
66 VOID
67 Ki386SetProcessorFeatures(VOID);
68
69 VOID
70 NTAPI
71 KiSetCR0Bits(VOID);
72
73 VOID
74 NTAPI
75 KiGetCacheInformation(VOID);
76
77 BOOLEAN
78 NTAPI
79 KiIsNpxPresent(
80 VOID
81 );
82
83 VOID
84 NTAPI
85 KiSetProcessorType(VOID);
86
87 ULONG
88 NTAPI
89 KiGetFeatureBits(VOID);
90
91 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
92 VOID KeFreeGdtSelector(ULONG Entry);
93 VOID
94 NtEarlyInitVdm(VOID);
95 VOID
96 KeApplicationProcessorInitDispatcher(VOID);
97 VOID
98 KeCreateApplicationProcessorIdleThread(ULONG Id);
99
100 typedef
101 VOID
102 (NTAPI*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
103 PVOID StartContext);
104
105 VOID
106 NTAPI
107 Ke386InitThreadWithContext(PKTHREAD Thread,
108 PKSYSTEM_ROUTINE SystemRoutine,
109 PKSTART_ROUTINE StartRoutine,
110 PVOID StartContext,
111 PCONTEXT Context);
112
113 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
114 VOID
115 NTAPI
116 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
117 PKSTART_ROUTINE StartRoutine,
118 PVOID StartContext,
119 BOOLEAN UserThread,
120 KTRAP_FRAME TrapFrame);
121 #endif
122
123 #ifdef CONFIG_SMP
124 #define LOCK "lock ; "
125 #else
126 #define LOCK ""
127 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
128 #endif
129
130 #if defined(__GNUC__)
131 #define Ke386DisableInterrupts() __asm__("cli\n\t");
132 #define Ke386EnableInterrupts() __asm__("sti\n\t");
133 #define Ke386HaltProcessor() __asm__("hlt\n\t");
134 #define Ke386GetPageTableDirectory(X) \
135 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
136 #define Ke386SetPageTableDirectory(X) \
137 __asm__("movl %0,%%cr3\n\t" \
138 : /* no outputs */ \
139 : "r" (X));
140 #define Ke386SetFileSelector(X) \
141 __asm__("movl %0,%%cr3\n\t" \
142 : /* no outputs */ \
143 : "r" (X));
144 #define Ke386SetLocalDescriptorTable(X) \
145 __asm__("lldt %0\n\t" \
146 : /* no outputs */ \
147 : "m" (X));
148 #define Ke386SetInterruptDescriptorTable(X) \
149 __asm__("lidt %0\n\t" \
150 : /* no outputs */ \
151 : "m" (X));
152 #define Ke386SetGlobalDescriptorTable(X) \
153 __asm__("lgdt %0\n\t" \
154 : /* no outputs */ \
155 : "m" (X));
156 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
157 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
158
159 #define _Ke386GetSeg(N) ({ \
160 unsigned int __d; \
161 __asm__("movl %%" #N ",%0\n\t" :"=r" (__d)); \
162 __d; \
163 })
164
165 #define _Ke386GetCr(N) ({ \
166 unsigned int __d; \
167 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
168 __d; \
169 })
170 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
171 #define Ke386SetTr(X) __asm__ __volatile__("ltr %%ax" : :"a" (X));
172
173 #define _Ke386SetSeg(N,X) __asm__ __volatile__("movl %0,%%" #N : :"r" (X));
174
175 #define Ke386GetCr0() _Ke386GetCr(0)
176 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
177 #define Ke386GetCr2() _Ke386GetCr(2)
178 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
179 #define Ke386GetCr4() _Ke386GetCr(4)
180 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
181 #define Ke386GetSs() _Ke386GetSeg(ss)
182 #define Ke386SetFs(X) _Ke386SetSeg(fs, X)
183 #define Ke386SetDs(X) _Ke386SetSeg(ds, X)
184 #define Ke386SetEs(X) _Ke386SetSeg(es, X)
185
186 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
187 {
188 LONG OldBit;
189
190 __asm__ __volatile__(LOCK
191 "btrl %2,%1\n\t"
192 "sbbl %0,%0\n\t"
193 :"=r" (OldBit),"=m" (*Addr)
194 :"Ir" (BitPos)
195 : "memory");
196 return OldBit;
197 }
198
199 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
200 {
201 LONG OldBit;
202
203 __asm__ __volatile__(LOCK
204 "btsl %2,%1\n\t"
205 "sbbl %0,%0\n\t"
206 :"=r" (OldBit),"=m" (*Addr)
207 :"Ir" (BitPos)
208 : "memory");
209 return OldBit;
210 }
211
212
213 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
214 {
215 __asm__("cpuid"
216 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
217 : "0" (Op));
218 }
219
220 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
221
222 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
223
224
225 #elif defined(_MSC_VER)
226
227 #define Ke386DisableInterrupts() __asm cli
228 #define Ke386EnableInterrupts() __asm sti
229 #define Ke386HaltProcessor() __asm hlt
230 #define Ke386GetPageTableDirectory(X) \
231 __asm mov eax, cr3; \
232 __asm mov X, eax;
233 static __forceinline void Ke386SetPageTableDirectory(ULONG X)
234 {
235 __asm mov eax, X
236 __asm mov cr3, eax
237 }
238 #else
239 #error Unknown compiler for inline assembler
240 #endif
241
242 FORCEINLINE
243 struct _KPCR *
244 KeGetCurrentKPCR(VOID)
245 {
246 ULONG Value;
247 #if defined(__GNUC__)
248 __asm__ __volatile__ ("movl %%fs:0x1C, %0\n\t"
249 : "=r" (Value)
250 : /* no inputs */
251 );
252 #elif defined(_MSC_VER)
253 __asm mov eax, fs:[1Ch]
254 __asm mov [Value], eax
255 #endif
256 return (struct _KPCR *) Value;
257 }
258
259 #ifdef __GNUC__
260 FORCEINLINE
261 struct _KPRCB *
262 KeGetCurrentPrcb(VOID)
263 {
264 ULONG Value;
265 #if defined(__GNUC__)
266 __asm__ __volatile__ ("movl %%fs:0x20, %0\n\t"
267 : "=r" (Value)
268 : /* no inputs */
269 );
270 #elif defined(_MSC_VER)
271 __asm mov eax, fs:[20h]
272 __asm mov [Value], eax
273 #endif
274 return (struct _KPRCB *) Value;
275 }
276 #endif
277
278 #endif
279 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
280
281 /* EOF */