508a55b5717b6e005bfdf32b3aedaec3913db75b
[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 /* Possible values for KTHREAD's NpxState */
44 #define NPX_STATE_INVALID 0x01
45 #define NPX_STATE_VALID 0x02
46 #define NPX_STATE_DIRTY 0x04
47
48 #ifndef __ASM__
49
50 typedef struct _KIRQ_TRAPFRAME
51 {
52 ULONG Magic;
53 ULONG Gs;
54 ULONG Fs;
55 ULONG Es;
56 ULONG Ds;
57 ULONG Eax;
58 ULONG Ecx;
59 ULONG Edx;
60 ULONG Ebx;
61 ULONG Esp;
62 ULONG Ebp;
63 ULONG Esi;
64 ULONG Edi;
65 ULONG Eip;
66 ULONG Cs;
67 ULONG Eflags;
68 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
69
70 extern ULONG Ke386CacheAlignment;
71
72 struct _KPCR;
73 VOID
74 KiInitializeGdt(struct _KPCR* Pcr);
75 VOID
76 Ki386ApplicationProcessorInitializeTSS(VOID);
77 VOID
78 Ki386BootInitializeTSS(VOID);
79 VOID
80 KiGdtPrepareForApplicationProcessorInit(ULONG Id);
81 VOID
82 Ki386InitializeLdt(VOID);
83 VOID
84 Ki386SetProcessorFeatures(VOID);
85 ULONG KeAllocateGdtSelector(ULONG Desc[2]);
86 VOID KeFreeGdtSelector(ULONG Entry);
87 VOID
88 NtEarlyInitVdm(VOID);
89 VOID
90 KeApplicationProcessorInitDispatcher(VOID);
91 VOID
92 KeCreateApplicationProcessorIdleThread(ULONG Id);
93
94 typedef
95 VOID
96 (STDCALL*PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine,
97 PVOID StartContext);
98
99 VOID
100 STDCALL
101 Ke386InitThreadWithContext(PKTHREAD Thread,
102 PKSYSTEM_ROUTINE SystemRoutine,
103 PKSTART_ROUTINE StartRoutine,
104 PVOID StartContext,
105 PCONTEXT Context);
106
107 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
108 VOID
109 STDCALL
110 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
111 PKSTART_ROUTINE StartRoutine,
112 PVOID StartContext,
113 BOOLEAN UserThread,
114 KTRAP_FRAME TrapFrame);
115 #endif
116
117 #ifdef CONFIG_SMP
118 #define LOCK "lock ; "
119 #else
120 #define LOCK ""
121 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
122 #endif
123
124 #if defined(__GNUC__)
125 #define Ke386DisableInterrupts() __asm__("cli\n\t");
126 #define Ke386EnableInterrupts() __asm__("sti\n\t");
127 #define Ke386HaltProcessor() __asm__("hlt\n\t");
128 #define Ke386GetPageTableDirectory(X) \
129 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
130 #define Ke386SetPageTableDirectory(X) \
131 __asm__("movl %0,%%cr3\n\t" \
132 : /* no outputs */ \
133 : "r" (X));
134 #define Ke386SetFileSelector(X) \
135 __asm__("movl %0,%%cr3\n\t" \
136 : /* no outputs */ \
137 : "r" (X));
138 #define Ke386SetLocalDescriptorTable(X) \
139 __asm__("lldt %0\n\t" \
140 : /* no outputs */ \
141 : "m" (X));
142 #define Ke386SetGlobalDescriptorTable(X) \
143 __asm__("lgdt %0\n\t" \
144 : /* no outputs */ \
145 : "m" (X));
146 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
147 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
148
149 #define _Ke386GetCr(N) ({ \
150 unsigned int __d; \
151 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
152 __d; \
153 })
154 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
155
156 #define Ke386GetCr0() _Ke386GetCr(0)
157 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
158 #define Ke386GetCr2() _Ke386GetCr(2)
159 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
160 #define Ke386GetCr4() _Ke386GetCr(4)
161 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
162
163 static inline LONG Ke386TestAndClearBit(ULONG BitPos, volatile PULONG Addr)
164 {
165 LONG OldBit;
166
167 __asm__ __volatile__(LOCK
168 "btrl %2,%1\n\t"
169 "sbbl %0,%0\n\t"
170 :"=r" (OldBit),"=m" (*Addr)
171 :"Ir" (BitPos)
172 : "memory");
173 return OldBit;
174 }
175
176 static inline LONG Ke386TestAndSetBit(ULONG BitPos, volatile PULONG Addr)
177 {
178 LONG OldBit;
179
180 __asm__ __volatile__(LOCK
181 "btsl %2,%1\n\t"
182 "sbbl %0,%0\n\t"
183 :"=r" (OldBit),"=m" (*Addr)
184 :"Ir" (BitPos)
185 : "memory");
186 return OldBit;
187 }
188
189
190 static inline void Ki386Cpuid(ULONG Op, PULONG Eax, PULONG Ebx, PULONG Ecx, PULONG Edx)
191 {
192 __asm__("cpuid"
193 : "=a" (*Eax), "=b" (*Ebx), "=c" (*Ecx), "=d" (*Edx)
194 : "0" (Op));
195 }
196
197 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
198
199 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
200
201
202 #elif defined(_MSC_VER)
203
204 #define Ke386DisableInterrupts() __asm cli
205 #define Ke386EnableInterrupts() __asm sti
206 #define Ke386HaltProcessor() __asm hlt
207 #define Ke386GetPageTableDirectory(X) \
208 __asm mov eax, cr3; \
209 __asm mov X, eax;
210 #define Ke386SetPageTableDirectory(X) \
211 __asm mov eax, X; \
212 __asm mov cr3, eax;
213 #else
214 #error Unknown compiler for inline assembler
215 #endif
216
217 #endif
218 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */
219
220 /* EOF */