d68d95e8318e5472dafd7f64c1c110828a8b456d
[reactos.git] / reactos / ntoskrnl / include / internal / i386 / ke.h
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
20 #define __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
21
22 #if __GNUC__ >=3
23 #pragma GCC system_header
24 #endif
25
26 #define KTRAP_FRAME_DEBUGEBP (0x0)
27 #define KTRAP_FRAME_DEBUGEIP (0x4)
28 #define KTRAP_FRAME_DEBUGARGMARK (0x8)
29 #define KTRAP_FRAME_DEBUGPOINTER (0xC)
30 #define KTRAP_FRAME_TEMPCS (0x10)
31 #define KTRAP_FRAME_TEMPEIP (0x14)
32 #define KTRAP_FRAME_DR0 (0x18)
33 #define KTRAP_FRAME_DR1 (0x1C)
34 #define KTRAP_FRAME_DR2 (0x20)
35 #define KTRAP_FRAME_DR3 (0x24)
36 #define KTRAP_FRAME_DR6 (0x28)
37 #define KTRAP_FRAME_DR7 (0x2C)
38 #define KTRAP_FRAME_GS (0x30)
39 #define KTRAP_FRAME_RESERVED1 (0x32)
40 #define KTRAP_FRAME_ES (0x34)
41 #define KTRAP_FRAME_RESERVED2 (0x36)
42 #define KTRAP_FRAME_DS (0x38)
43 #define KTRAP_FRAME_RESERVED3 (0x3A)
44 #define KTRAP_FRAME_EDX (0x3C)
45 #define KTRAP_FRAME_ECX (0x40)
46 #define KTRAP_FRAME_EAX (0x44)
47 #define KTRAP_FRAME_PREVIOUS_MODE (0x48)
48 #define KTRAP_FRAME_EXCEPTION_LIST (0x4C)
49 #define KTRAP_FRAME_FS (0x50)
50 #define KTRAP_FRAME_RESERVED4 (0x52)
51 #define KTRAP_FRAME_EDI (0x54)
52 #define KTRAP_FRAME_ESI (0x58)
53 #define KTRAP_FRAME_EBX (0x5C)
54 #define KTRAP_FRAME_EBP (0x60)
55 #define KTRAP_FRAME_ERROR_CODE (0x64)
56 #define KTRAP_FRAME_EIP (0x68)
57 #define KTRAP_FRAME_CS (0x6C)
58 #define KTRAP_FRAME_EFLAGS (0x70)
59 #define KTRAP_FRAME_ESP (0x74)
60 #define KTRAP_FRAME_SS (0x78)
61 #define KTRAP_FRAME_RESERVED5 (0x7A)
62 #define KTRAP_FRAME_V86_ES (0x7C)
63 #define KTRAP_FRAME_RESERVED6 (0x7E)
64 #define KTRAP_FRAME_V86_DS (0x80)
65 #define KTRAP_FRAME_RESERVED7 (0x82)
66 #define KTRAP_FRAME_V86_FS (0x84)
67 #define KTRAP_FRAME_RESERVED8 (0x86)
68 #define KTRAP_FRAME_V86_GS (0x88)
69 #define KTRAP_FRAME_RESERVED9 (0x8A)
70 #define KTRAP_FRAME_SIZE (0x8C)
71
72 #define X86_EFLAGS_TF 0x00000100 /* Trap flag */
73 #define X86_EFLAGS_IF 0x00000200 /* Interrupt Enable flag */
74 #define X86_EFLAGS_IOPL 0x00003000 /* I/O Privilege Level bits */
75 #define X86_EFLAGS_NT 0x00004000 /* Nested Task flag */
76 #define X86_EFLAGS_RF 0x00010000 /* Resume flag */
77 #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
78 #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
79
80 #define X86_CR0_PE 0x00000001 /* enable Protected Mode */
81 #define X86_CR0_NE 0x00000020 /* enable native FPU error reporting */
82 #define X86_CR0_TS 0x00000008 /* enable exception on FPU instruction for task switch */
83 #define X86_CR0_EM 0x00000004 /* enable FPU emulation (disable FPU) */
84 #define X86_CR0_MP 0x00000002 /* enable FPU monitoring */
85 #define X86_CR0_WP 0x00010000 /* enable Write Protect (copy on write) */
86 #define X86_CR0_PG 0x80000000 /* enable Paging */
87
88 #define X86_CR4_PAE 0x00000020 /* enable physical address extensions */
89 #define X86_CR4_PGE 0x00000080 /* enable global pages */
90 #define X86_CR4_OSFXSR 0x00000200 /* enable FXSAVE/FXRSTOR instructions */
91 #define X86_CR4_OSXMMEXCPT 0x00000400 /* enable #XF exception */
92
93 #define X86_FEATURE_TSC 0x00000010 /* time stamp counters are present */
94 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
95 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
96 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
97 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
98 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
99 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
100 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
101 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
102 #define X86_FEATURE_HT 0x10000000 /* Hyper-Threading present */
103
104 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
105 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
106
107 /* Possible values for KTHREAD's NpxState */
108 #define NPX_STATE_INVALID 0x01
109 #define NPX_STATE_VALID 0x02
110 #define NPX_STATE_DIRTY 0x04
111
112 #ifndef __ASM__
113
114 typedef struct _KTRAP_FRAME
115 {
116 PVOID DebugEbp;
117 PVOID DebugEip;
118 PVOID DebugArgMark;
119 PVOID DebugPointer;
120 PVOID TempCs;
121 PVOID TempEip;
122 ULONG Dr0;
123 ULONG Dr1;
124 ULONG Dr2;
125 ULONG Dr3;
126 ULONG Dr6;
127 ULONG Dr7;
128 USHORT Gs;
129 USHORT Reserved1;
130 USHORT Es;
131 USHORT Reserved2;
132 USHORT Ds;
133 USHORT Reserved3;
134 ULONG Edx;
135 ULONG Ecx;
136 ULONG Eax;
137 ULONG PreviousMode;
138 PVOID ExceptionList;
139 USHORT Fs;
140 USHORT Reserved4;
141 ULONG Edi;
142 ULONG Esi;
143 ULONG Ebx;
144 ULONG Ebp;
145 ULONG ErrorCode;
146 ULONG Eip;
147 ULONG Cs;
148 ULONG Eflags;
149 ULONG Esp;
150 USHORT Ss;
151 USHORT Reserved5;
152 USHORT V86_Es;
153 USHORT Reserved6;
154 USHORT V86_Ds;
155 USHORT Reserved7;
156 USHORT V86_Fs;
157 USHORT Reserved8;
158 USHORT V86_Gs;
159 USHORT Reserved9;
160 } KTRAP_FRAME, *PKTRAP_FRAME;
161
162 typedef struct _KIRQ_TRAPFRAME
163 {
164 ULONG Magic;
165 ULONG Gs;
166 ULONG Fs;
167 ULONG Es;
168 ULONG Ds;
169 ULONG Eax;
170 ULONG Ecx;
171 ULONG Edx;
172 ULONG Ebx;
173 ULONG Esp;
174 ULONG Ebp;
175 ULONG Esi;
176 ULONG Edi;
177 ULONG Eip;
178 ULONG Cs;
179 ULONG Eflags;
180 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
181
182 extern ULONG Ke386CacheAlignment;
183
184 struct _KPCR;
185 VOID
186 KiInitializeGdt(struct _KPCR* Pcr);
187 VOID
188 Ki386ApplicationProcessorInitializeTSS(VOID);
189 VOID
190 Ki386BootInitializeTSS(VOID);
191 VOID
192 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
193 VOID
194 Ki386InitializeLdt(VOID);
195 VOID
196 Ki386SetProcessorFeatures(VOID);
197 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
198 VOID KeFreeGdtSelector(ULONG Entry);
199 VOID
200 NtEarlyInitVdm(VOID);
201 VOID
202 KeApplicationProcessorInitDispatcher(VOID);
203 VOID
204 KeCreateApplicationProcessorIdleThread(ULONG Id);
205
206 typedef
207 VOID
208 STDCALL
209 (*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
210 PVOID StartContext);
211
212 VOID
213 STDCALL
214 Ke386InitThreadWithContext(PKTHREAD Thread,
215 PKSYSTEM_ROUTINE SystemRoutine,
216 PKSTART_ROUTINE StartRoutine,
217 PVOID StartContext,
218 PCONTEXT Context);
219
220 VOID
221 STDCALL
222 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
223 PKSTART_ROUTINE StartRoutine,
224 PVOID StartContext,
225 BOOLEAN UserThread,
226 KTRAP_FRAME TrapFrame);
227
228 #ifdef CONFIG_SMP
229 #define LOCK "lock ; "
230 #else
231 #define LOCK ""
232 #define KeGetCurrentIrql(X) (((PKPCR)KPCR_BASE)->Irql)
233 #endif
234
235 #if defined(__GNUC__)
236 #define Ke386DisableInterrupts() __asm__("cli\n\t");
237 #define Ke386EnableInterrupts() __asm__("sti\n\t");
238 #define Ke386HaltProcessor() __asm__("hlt\n\t");
239 #define Ke386GetPageTableDirectory(X) \
240 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
241 #define Ke386SetPageTableDirectory(X) \
242 __asm__("movl %0,%%cr3\n\t" \
243 : /* no outputs */ \
244 : "r" (X));
245 #define Ke386SetFileSelector(X) \
246 __asm__("movl %0,%%cr3\n\t" \
247 : /* no outputs */ \
248 : "r" (X));
249 #define Ke386SetLocalDescriptorTable(X) \
250 __asm__("lldt %0\n\t" \
251 : /* no outputs */ \
252 : "m" (X));
253 #define Ke386SetGlobalDescriptorTable(X) \
254 __asm__("lgdt %0\n\t" \
255 : /* no outputs */ \
256 : "m" (X));
257 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
258 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
259
260 #define _Ke386GetCr(N) ({ \
261 unsigned int __d; \
262 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
263 __d; \
264 })
265 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
266
267 #define Ke386GetCr0() _Ke386GetCr(0)
268 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
269 #define Ke386GetCr2() _Ke386GetCr(2)
270 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
271 #define Ke386GetCr4() _Ke386GetCr(4)
272 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
273
274 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
275 {
276 LONG OldBit;
277
278 __asm__ __volatile__(LOCK
279 "btrl %2,%1\n\t"
280 "sbbl %0,%0\n\t"
281 :"=r" (OldBit),"=m" (*Addr)
282 :"Ir" (BitPos)
283 : "memory");
284 return OldBit;
285 }
286
287 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
288 {
289 LONG OldBit;
290
291 __asm__ __volatile__(LOCK
292 "btsl %2,%1\n\t"
293 "sbbl %0,%0\n\t"
294 :"=r" (OldBit),"=m" (*Addr)
295 :"Ir" (BitPos)
296 : "memory");
297 return OldBit;
298 }
299
300
301 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
302 {
303 __asm__("cpuid"
304 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
305 : "0" (Op));
306 }
307
308 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
309
310 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
311
312
313 #elif defined(_MSC_VER)
314
315 #define Ke386DisableInterrupts() __asm cli
316 #define Ke386EnableInterrupts() __asm sti
317 #define Ke386HaltProcessor() __asm hlt
318 #define Ke386GetPageTableDirectory(X) \
319 __asm mov eax, cr3; \
320 __asm mov X, eax;
321 #define Ke386SetPageTableDirectory(X) \
322 __asm mov eax, X; \
323 __asm mov cr3, eax;
324 #else
325 #error Unknown compiler for inline assembler
326 #endif
327
328 #endif /* __ASM__ */
329
330 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
331
332 /* EOF */