Copy makefile
[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_IF 0x00000200 /* Interrupt Enable flag */
73 #define X86_EFLAGS_IOPL 0x00003000 /* I/O Privilege Level bits */
74 #define X86_EFLAGS_NT 0x00004000 /* Nested Task flag */
75 #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
76 #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
77
78 #define X86_CR0_PE 0x00000001 /* enable Protected Mode */
79 #define X86_CR0_NE 0x00000020 /* enable native FPU error reporting */
80 #define X86_CR0_TS 0x00000008 /* enable exception on FPU instruction for task switch */
81 #define X86_CR0_EM 0x00000004 /* enable FPU emulation (disable FPU) */
82 #define X86_CR0_MP 0x00000002 /* enable FPU monitoring */
83 #define X86_CR0_WP 0x00010000 /* enable Write Protect (copy on write) */
84 #define X86_CR0_PG 0x80000000 /* enable Paging */
85
86 #define X86_CR4_PAE 0x00000020 /* enable physical address extensions */
87 #define X86_CR4_PGE 0x00000080 /* enable global pages */
88 #define X86_CR4_OSFXSR 0x00000200 /* enable FXSAVE/FXRSTOR instructions */
89 #define X86_CR4_OSXMMEXCPT 0x00000400 /* enable #XF exception */
90
91 #define X86_FEATURE_TSC 0x00000010 /* time stamp counters are present */
92 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
93 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
94 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
95 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
96 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
97 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
98 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
99 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
100
101 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
102 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
103
104 /* Possible values for KTHREAD's NpxState */
105 #define NPX_STATE_INVALID 0x01
106 #define NPX_STATE_VALID 0x02
107 #define NPX_STATE_DIRTY 0x04
108
109 #ifndef __ASM__
110
111 typedef struct _KTRAP_FRAME
112 {
113 PVOID DebugEbp;
114 PVOID DebugEip;
115 PVOID DebugArgMark;
116 PVOID DebugPointer;
117 PVOID TempCs;
118 PVOID TempEip;
119 ULONG Dr0;
120 ULONG Dr1;
121 ULONG Dr2;
122 ULONG Dr3;
123 ULONG Dr6;
124 ULONG Dr7;
125 USHORT Gs;
126 USHORT Reserved1;
127 USHORT Es;
128 USHORT Reserved2;
129 USHORT Ds;
130 USHORT Reserved3;
131 ULONG Edx;
132 ULONG Ecx;
133 ULONG Eax;
134 ULONG PreviousMode;
135 PVOID ExceptionList;
136 USHORT Fs;
137 USHORT Reserved4;
138 ULONG Edi;
139 ULONG Esi;
140 ULONG Ebx;
141 ULONG Ebp;
142 ULONG ErrorCode;
143 ULONG Eip;
144 ULONG Cs;
145 ULONG Eflags;
146 ULONG Esp;
147 USHORT Ss;
148 USHORT Reserved5;
149 USHORT V86_Es;
150 USHORT Reserved6;
151 USHORT V86_Ds;
152 USHORT Reserved7;
153 USHORT V86_Fs;
154 USHORT Reserved8;
155 USHORT V86_Gs;
156 USHORT Reserved9;
157 } KTRAP_FRAME, *PKTRAP_FRAME;
158
159 typedef struct _KIRQ_TRAPFRAME
160 {
161 ULONG Magic;
162 ULONG Gs;
163 ULONG Fs;
164 ULONG Es;
165 ULONG Ds;
166 ULONG Eax;
167 ULONG Ecx;
168 ULONG Edx;
169 ULONG Ebx;
170 ULONG Esp;
171 ULONG Ebp;
172 ULONG Esi;
173 ULONG Edi;
174 ULONG Eip;
175 ULONG Cs;
176 ULONG Eflags;
177 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
178
179 extern ULONG Ke386CacheAlignment;
180
181 struct _KPCR;
182 VOID
183 KiInitializeGdt(struct _KPCR* Pcr);
184 VOID
185 Ki386ApplicationProcessorInitializeTSS(VOID);
186 VOID
187 Ki386BootInitializeTSS(VOID);
188 VOID
189 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
190 VOID
191 Ki386InitializeLdt(VOID);
192 VOID
193 Ki386SetProcessorFeatures(VOID);
194 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
195 VOID KeFreeGdtSelector(ULONG Entry);
196 VOID
197 NtEarlyInitVdm(VOID);
198
199 #ifdef CONFIG_SMP
200 #define LOCK "lock ; "
201 #else
202 #define LOCK ""
203 #endif
204
205
206 #if defined(__GNUC__)
207 #define Ke386DisableInterrupts() __asm__("cli\n\t");
208 #define Ke386EnableInterrupts() __asm__("sti\n\t");
209 #define Ke386HaltProcessor() __asm__("hlt\n\t");
210 #define Ke386GetPageTableDirectory(X) \
211 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
212 #define Ke386SetPageTableDirectory(X) \
213 __asm__("movl %0,%%cr3\n\t" \
214 : /* no outputs */ \
215 : "r" (X));
216 #define Ke386SetFileSelector(X) \
217 __asm__("movl %0,%%cr3\n\t" \
218 : /* no outputs */ \
219 : "r" (X));
220 #define Ke386SetLocalDescriptorTable(X) \
221 __asm__("lldt %0\n\t" \
222 : /* no outputs */ \
223 : "m" (X));
224 #define Ke386SetGlobalDescriptorTable(X) \
225 __asm__("lgdt %0\n\t" \
226 : /* no outputs */ \
227 : "m" (X));
228 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
229 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
230
231 #define _Ke386GetCr(N) ({ \
232 unsigned int __d; \
233 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
234 __d; \
235 })
236 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
237
238 #define Ke386GetCr0() _Ke386GetCr(0)
239 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
240 #define Ke386GetCr2() _Ke386GetCr(2)
241 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
242 #define Ke386GetCr4() _Ke386GetCr(4)
243 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
244
245 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
246 {
247 LONG OldBit;
248
249 __asm__ __volatile__(LOCK
250 "btrl %2,%1\n\t"
251 "sbbl %0,%0\n\t"
252 :"=r" (OldBit),"=m" (*Addr)
253 :"Ir" (BitPos)
254 : "memory");
255 return OldBit;
256 }
257
258 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
259 {
260 LONG OldBit;
261
262 __asm__ __volatile__(LOCK
263 "btsl %2,%1\n\t"
264 "sbbl %0,%0\n\t"
265 :"=r" (OldBit),"=m" (*Addr)
266 :"Ir" (BitPos)
267 : "memory");
268 return OldBit;
269 }
270
271
272 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
273 {
274 __asm__("cpuid"
275 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
276 : "0" (Op));
277 }
278
279 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
280
281 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
282
283
284 #elif defined(_MSC_VER)
285
286 #define Ke386DisableInterrupts() __asm cli
287 #define Ke386EnableInterrupts() __asm sti
288 #define Ke386HaltProcessor() __asm hlt
289 #define Ke386GetPageTableDirectory(X) \
290 __asm mov eax, cr3; \
291 __asm mov X, eax;
292 #define Ke386SetPageTableDirectory(X) \
293 __asm mov eax, X; \
294 __asm mov cr3, eax;
295 #else
296 #error Unknown compiler for inline assembler
297 #endif
298
299 #endif /* __ASM__ */
300
301 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
302
303 /* EOF */