- Misc janitorial fixes by Brezenbak to fix some MSVC compile errors.
[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_TSC 0x00000010 /* time stamp counters are present */
30 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
31 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
32 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
33 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
34 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
35 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
36 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
37 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
38 #define X86_FEATURE_HT 0x10000000 /* Hyper-Threading present */
39
40 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
41 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
42
43 #define DR7_ACTIVE 0x00000055 /* If any of these bits are set, a Dr is active */
44
45 /* Possible values for KTHREAD's NpxState */
46 #define NPX_STATE_INVALID 0x01
47 #define NPX_STATE_VALID 0x02
48 #define NPX_STATE_DIRTY 0x04
49
50 #define FRAME_EDITED 0xFFF8
51
52 #ifndef __ASM__
53
54 typedef struct _KIRQ_TRAPFRAME
55 {
56 ULONG Magic;
57 ULONG Gs;
58 ULONG Fs;
59 ULONG Es;
60 ULONG Ds;
61 ULONG Eax;
62 ULONG Ecx;
63 ULONG Edx;
64 ULONG Ebx;
65 ULONG Esp;
66 ULONG Ebp;
67 ULONG Esi;
68 ULONG Edi;
69 ULONG Eip;
70 ULONG Cs;
71 ULONG Eflags;
72 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
73
74 /* Emulate cli/sti instructions */
75 #define KV86M_EMULATE_CLI_STI (0x1)
76 /* Allow the v86 mode code to access i/o ports */
77 #define KV86M_ALLOW_IO_PORT_ACCESS (0x2)
78
79 typedef struct _KV86M_REGISTERS
80 {
81 /*
82 * General purpose registers
83 */
84 ULONG Ebp;
85 ULONG Edi;
86 ULONG Esi;
87 ULONG Edx;
88 ULONG Ecx;
89 ULONG Ebx;
90 ULONG Eax;
91 ULONG Ds;
92 ULONG Es;
93 ULONG Fs;
94 ULONG Gs;
95
96 /*
97 * Control registers
98 */
99 ULONG Eip;
100 ULONG Cs;
101 ULONG Eflags;
102 ULONG Esp;
103 ULONG Ss;
104
105 /*
106 * Control structures
107 */
108 ULONG RecoveryAddress;
109 UCHAR RecoveryInstruction[4];
110 ULONG Vif;
111 ULONG Flags;
112 PNTSTATUS PStatus;
113 } KV86M_REGISTERS, *PKV86M_REGISTERS;
114
115 typedef struct _KV86M_TRAP_FRAME
116 {
117 KTRAP_FRAME Tf;
118
119 ULONG SavedExceptionStack;
120
121 /*
122 * These are put on the top of the stack by the routine that entered
123 * v86 mode so the exception handlers can find the control information
124 */
125 struct _KV86M_REGISTERS* regs;
126 ULONG orig_ebp;
127 } KV86M_TRAP_FRAME, *PKV86M_TRAP_FRAME;
128
129 extern ULONG Ke386CacheAlignment;
130
131 struct _KPCR;
132 VOID
133 KiInitializeGdt(struct _KPCR* Pcr);
134 VOID
135 Ki386ApplicationProcessorInitializeTSS(VOID);
136 VOID
137 Ki386BootInitializeTSS(VOID);
138 VOID
139 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
140 VOID
141 Ki386InitializeLdt(VOID);
142 VOID
143 Ki386SetProcessorFeatures(VOID);
144 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
145 VOID KeFreeGdtSelector(ULONG Entry);
146 VOID
147 NtEarlyInitVdm(VOID);
148 VOID
149 KeApplicationProcessorInitDispatcher(VOID);
150 VOID
151 KeCreateApplicationProcessorIdleThread(ULONG Id);
152
153 typedef
154 VOID
155 (STDCALL*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
156 PVOID StartContext);
157
158 VOID
159 STDCALL
160 Ke386InitThreadWithContext(PKTHREAD Thread,
161 PKSYSTEM_ROUTINE SystemRoutine,
162 PKSTART_ROUTINE StartRoutine,
163 PVOID StartContext,
164 PCONTEXT Context);
165
166 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
167 VOID
168 STDCALL
169 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
170 PKSTART_ROUTINE StartRoutine,
171 PVOID StartContext,
172 BOOLEAN UserThread,
173 KTRAP_FRAME TrapFrame);
174 #endif
175
176 #ifdef CONFIG_SMP
177 #define LOCK "lock ; "
178 #else
179 #define LOCK ""
180 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
181 #endif
182
183 #if defined(__GNUC__)
184 #define Ke386DisableInterrupts() __asm__("cli\n\t");
185 #define Ke386EnableInterrupts() __asm__("sti\n\t");
186 #define Ke386HaltProcessor() __asm__("hlt\n\t");
187 #define Ke386GetPageTableDirectory(X) \
188 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
189 #define Ke386SetPageTableDirectory(X) \
190 __asm__("movl %0,%%cr3\n\t" \
191 : /* no outputs */ \
192 : "r" (X));
193 #define Ke386SetFileSelector(X) \
194 __asm__("movl %0,%%cr3\n\t" \
195 : /* no outputs */ \
196 : "r" (X));
197 #define Ke386SetLocalDescriptorTable(X) \
198 __asm__("lldt %0\n\t" \
199 : /* no outputs */ \
200 : "m" (X));
201 #define Ke386SetGlobalDescriptorTable(X) \
202 __asm__("lgdt %0\n\t" \
203 : /* no outputs */ \
204 : "m" (X));
205 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
206 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
207
208 #define _Ke386GetCr(N) ({ \
209 unsigned int __d; \
210 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
211 __d; \
212 })
213 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
214
215 #define Ke386GetCr0() _Ke386GetCr(0)
216 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
217 #define Ke386GetCr2() _Ke386GetCr(2)
218 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
219 #define Ke386GetCr4() _Ke386GetCr(4)
220 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
221
222 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
223 {
224 LONG OldBit;
225
226 __asm__ __volatile__(LOCK
227 "btrl %2,%1\n\t"
228 "sbbl %0,%0\n\t"
229 :"=r" (OldBit),"=m" (*Addr)
230 :"Ir" (BitPos)
231 : "memory");
232 return OldBit;
233 }
234
235 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
236 {
237 LONG OldBit;
238
239 __asm__ __volatile__(LOCK
240 "btsl %2,%1\n\t"
241 "sbbl %0,%0\n\t"
242 :"=r" (OldBit),"=m" (*Addr)
243 :"Ir" (BitPos)
244 : "memory");
245 return OldBit;
246 }
247
248
249 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
250 {
251 __asm__("cpuid"
252 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
253 : "0" (Op));
254 }
255
256 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
257
258 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
259
260
261 #elif defined(_MSC_VER)
262
263 #define Ke386DisableInterrupts() __asm cli
264 #define Ke386EnableInterrupts() __asm sti
265 #define Ke386HaltProcessor() __asm hlt
266 #define Ke386GetPageTableDirectory(X) \
267 __asm mov eax, cr3; \
268 __asm mov X, eax;
269 static __forceinline void Ke386SetPageTableDirectory(ULONG X)
270 {
271 __asm mov eax, X
272 __asm mov cr3, eax
273 }
274 #else
275 #error Unknown compiler for inline assembler
276 #endif
277
278 #endif
279 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
280
281 /* EOF */