- NDK 0.98, now with versionned headers. Too many changes to list, see the TinyKRNL...
[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 KF_RDTSC 0x00000002 /* 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 extern ULONG Ke386CacheAlignment;
55
56 struct _KPCR;
57 VOID
58 KiInitializeGdt(struct _KPCR* Pcr);
59 VOID
60 Ki386ApplicationProcessorInitializeTSS(VOID);
61 VOID
62 Ki386BootInitializeTSS(VOID);
63 VOID
64 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
65 VOID
66 Ki386InitializeLdt(VOID);
67 VOID
68 Ki386SetProcessorFeatures(VOID);
69 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
70 VOID KeFreeGdtSelector(ULONG Entry);
71 VOID
72 NtEarlyInitVdm(VOID);
73 VOID
74 KeApplicationProcessorInitDispatcher(VOID);
75 VOID
76 KeCreateApplicationProcessorIdleThread(ULONG Id);
77
78 typedef
79 VOID
80 (STDCALL*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
81 PVOID StartContext);
82
83 VOID
84 STDCALL
85 Ke386InitThreadWithContext(PKTHREAD Thread,
86 PKSYSTEM_ROUTINE SystemRoutine,
87 PKSTART_ROUTINE StartRoutine,
88 PVOID StartContext,
89 PCONTEXT Context);
90
91 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
92 VOID
93 STDCALL
94 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
95 PKSTART_ROUTINE StartRoutine,
96 PVOID StartContext,
97 BOOLEAN UserThread,
98 KTRAP_FRAME TrapFrame);
99 #endif
100
101 #ifdef CONFIG_SMP
102 #define LOCK "lock ; "
103 #else
104 #define LOCK ""
105 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
106 #endif
107
108 #if defined(__GNUC__)
109 #define Ke386DisableInterrupts() __asm__("cli\n\t");
110 #define Ke386EnableInterrupts() __asm__("sti\n\t");
111 #define Ke386HaltProcessor() __asm__("hlt\n\t");
112 #define Ke386GetPageTableDirectory(X) \
113 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
114 #define Ke386SetPageTableDirectory(X) \
115 __asm__("movl %0,%%cr3\n\t" \
116 : /* no outputs */ \
117 : "r" (X));
118 #define Ke386SetFileSelector(X) \
119 __asm__("movl %0,%%cr3\n\t" \
120 : /* no outputs */ \
121 : "r" (X));
122 #define Ke386SetLocalDescriptorTable(X) \
123 __asm__("lldt %0\n\t" \
124 : /* no outputs */ \
125 : "m" (X));
126 #define Ke386SetGlobalDescriptorTable(X) \
127 __asm__("lgdt %0\n\t" \
128 : /* no outputs */ \
129 : "m" (X));
130 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
131 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
132
133 #define _Ke386GetCr(N) ({ \
134 unsigned int __d; \
135 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
136 __d; \
137 })
138 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
139
140 #define Ke386GetCr0() _Ke386GetCr(0)
141 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
142 #define Ke386GetCr2() _Ke386GetCr(2)
143 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
144 #define Ke386GetCr4() _Ke386GetCr(4)
145 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
146
147 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
148 {
149 LONG OldBit;
150
151 __asm__ __volatile__(LOCK
152 "btrl %2,%1\n\t"
153 "sbbl %0,%0\n\t"
154 :"=r" (OldBit),"=m" (*Addr)
155 :"Ir" (BitPos)
156 : "memory");
157 return OldBit;
158 }
159
160 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
161 {
162 LONG OldBit;
163
164 __asm__ __volatile__(LOCK
165 "btsl %2,%1\n\t"
166 "sbbl %0,%0\n\t"
167 :"=r" (OldBit),"=m" (*Addr)
168 :"Ir" (BitPos)
169 : "memory");
170 return OldBit;
171 }
172
173
174 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
175 {
176 __asm__("cpuid"
177 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
178 : "0" (Op));
179 }
180
181 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
182
183 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
184
185
186 #elif defined(_MSC_VER)
187
188 #define Ke386DisableInterrupts() __asm cli
189 #define Ke386EnableInterrupts() __asm sti
190 #define Ke386HaltProcessor() __asm hlt
191 #define Ke386GetPageTableDirectory(X) \
192 __asm mov eax, cr3; \
193 __asm mov X, eax;
194 static __forceinline void Ke386SetPageTableDirectory(ULONG X)
195 {
196 __asm mov eax, X
197 __asm mov cr3, eax
198 }
199 #else
200 #error Unknown compiler for inline assembler
201 #endif
202
203 static __inline struct _KPCR * KeGetCurrentKPCR(
204 VOID)
205 {
206 ULONG Value;
207 #if defined(__GNUC__)
208 __asm__ __volatile__ ("movl %%fs:0x1C, %0\n\t"
209 : "=r" (Value)
210 : /* no inputs */
211 );
212 #elif defined(_MSC_VER)
213 __asm mov eax, fs:[1Ch]
214 __asm mov [Value], eax
215 #endif
216 return (struct _KPCR *) Value;
217 }
218
219 static __inline struct _KPRCB * KeGetCurrentPrcb(
220 VOID)
221 {
222 ULONG Value;
223 #if defined(__GNUC__)
224 __asm__ __volatile__ ("movl %%fs:0x20, %0\n\t"
225 : "=r" (Value)
226 : /* no inputs */
227 );
228 #elif defined(_MSC_VER)
229 __asm mov eax, fs:[20h]
230 __asm mov [Value], eax
231 #endif
232 return (struct _KPRCB *) Value;
233 }
234
235 #endif
236 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
237
238 /* EOF */