1d71f299f772901888498b8f1429634e0b2abd60
[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_TEMPSS (0x10)
31 #define KTRAP_FRAME_TEMPESP (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 _KIRQ_TRAPFRAME
115 {
116 ULONG Magic;
117 ULONG Gs;
118 ULONG Fs;
119 ULONG Es;
120 ULONG Ds;
121 ULONG Eax;
122 ULONG Ecx;
123 ULONG Edx;
124 ULONG Ebx;
125 ULONG Esp;
126 ULONG Ebp;
127 ULONG Esi;
128 ULONG Edi;
129 ULONG Eip;
130 ULONG Cs;
131 ULONG Eflags;
132 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
133
134 typedef struct _KGDTENTRY {
135 USHORT LimitLow;
136 USHORT BaseLow;
137 union {
138 struct {
139 UCHAR BaseMid;
140 UCHAR Flags1;
141 UCHAR Flags2;
142 UCHAR BaseHi;
143 } Bytes;
144 struct {
145 ULONG BaseMid : 8;
146 ULONG Type : 5;
147 ULONG Dpl : 2;
148 ULONG Pres : 1;
149 ULONG LimitHi : 4;
150 ULONG Sys : 1;
151 ULONG Reserved_0 : 1;
152 ULONG Default_Big : 1;
153 ULONG Granularity : 1;
154 ULONG BaseHi : 8;
155 } Bits;
156 } HighWord;
157 } KGDTENTRY, *PKGDTENTRY;
158
159 typedef struct _KIDTENTRY {
160 USHORT Offset;
161 USHORT Selector;
162 USHORT Access;
163 USHORT ExtendedOffset;
164 } KIDTENTRY, *PKIDTENTRY;
165
166 extern ULONG Ke386CacheAlignment;
167
168 struct _KPCR;
169 VOID
170 KiInitializeGdt(struct _KPCR* Pcr);
171 VOID
172 Ki386ApplicationProcessorInitializeTSS(VOID);
173 VOID
174 Ki386BootInitializeTSS(VOID);
175 VOID
176 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
177 VOID
178 Ki386InitializeLdt(VOID);
179 VOID
180 Ki386SetProcessorFeatures(VOID);
181 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
182 VOID KeFreeGdtSelector(ULONG Entry);
183 VOID
184 NtEarlyInitVdm(VOID);
185 VOID
186 KeApplicationProcessorInitDispatcher(VOID);
187 VOID
188 KeCreateApplicationProcessorIdleThread(ULONG Id);
189
190 typedef
191 VOID
192 (STDCALL*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
193 PVOID StartContext);
194
195 VOID
196 STDCALL
197 Ke386InitThreadWithContext(PKTHREAD Thread,
198 PKSYSTEM_ROUTINE SystemRoutine,
199 PKSTART_ROUTINE StartRoutine,
200 PVOID StartContext,
201 PCONTEXT Context);
202
203 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
204 VOID
205 STDCALL
206 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
207 PKSTART_ROUTINE StartRoutine,
208 PVOID StartContext,
209 BOOLEAN UserThread,
210 KTRAP_FRAME TrapFrame);
211 #endif
212
213 #ifdef CONFIG_SMP
214 #define LOCK "lock ; "
215 #else
216 #define LOCK ""
217 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
218 #endif
219
220 #if defined(__GNUC__)
221 #define Ke386DisableInterrupts() __asm__("cli\n\t");
222 #define Ke386EnableInterrupts() __asm__("sti\n\t");
223 #define Ke386HaltProcessor() __asm__("hlt\n\t");
224 #define Ke386GetPageTableDirectory(X) \
225 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
226 #define Ke386SetPageTableDirectory(X) \
227 __asm__("movl %0,%%cr3\n\t" \
228 : /* no outputs */ \
229 : "r" (X));
230 #define Ke386SetFileSelector(X) \
231 __asm__("movl %0,%%cr3\n\t" \
232 : /* no outputs */ \
233 : "r" (X));
234 #define Ke386SetLocalDescriptorTable(X) \
235 __asm__("lldt %0\n\t" \
236 : /* no outputs */ \
237 : "m" (X));
238 #define Ke386SetGlobalDescriptorTable(X) \
239 __asm__("lgdt %0\n\t" \
240 : /* no outputs */ \
241 : "m" (X));
242 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
243 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
244
245 #define _Ke386GetCr(N) ({ \
246 unsigned int __d; \
247 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
248 __d; \
249 })
250 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
251
252 #define Ke386GetCr0() _Ke386GetCr(0)
253 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
254 #define Ke386GetCr2() _Ke386GetCr(2)
255 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
256 #define Ke386GetCr4() _Ke386GetCr(4)
257 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
258
259 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
260 {
261 LONG OldBit;
262
263 __asm__ __volatile__(LOCK
264 "btrl %2,%1\n\t"
265 "sbbl %0,%0\n\t"
266 :"=r" (OldBit),"=m" (*Addr)
267 :"Ir" (BitPos)
268 : "memory");
269 return OldBit;
270 }
271
272 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
273 {
274 LONG OldBit;
275
276 __asm__ __volatile__(LOCK
277 "btsl %2,%1\n\t"
278 "sbbl %0,%0\n\t"
279 :"=r" (OldBit),"=m" (*Addr)
280 :"Ir" (BitPos)
281 : "memory");
282 return OldBit;
283 }
284
285
286 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
287 {
288 __asm__("cpuid"
289 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
290 : "0" (Op));
291 }
292
293 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
294
295 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
296
297
298 #elif defined(_MSC_VER)
299
300 #define Ke386DisableInterrupts() __asm cli
301 #define Ke386EnableInterrupts() __asm sti
302 #define Ke386HaltProcessor() __asm hlt
303 #define Ke386GetPageTableDirectory(X) \
304 __asm mov eax, cr3; \
305 __asm mov X, eax;
306 #define Ke386SetPageTableDirectory(X) \
307 __asm mov eax, X; \
308 __asm mov cr3, eax;
309 #else
310 #error Unknown compiler for inline assembler
311 #endif
312
313 #endif /* __ASM__ */
314
315 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
316
317 /* EOF */