[NTOSKRNL]
[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 #ifndef __ASM__
5
6 #include "intrin_i.h"
7 #include "v86m.h"
8
9 extern ULONG Ke386CacheAlignment;
10
11 //
12 // Thread Dispatcher Header DebugActive Mask
13 //
14 #define DR_MASK(x) (1 << (x))
15 #define DR_REG_MASK 0x4F
16
17 #define IMAGE_FILE_MACHINE_ARCHITECTURE IMAGE_FILE_MACHINE_I386
18
19 //
20 // INT3 is 1 byte long
21 //
22 #define KD_BREAKPOINT_TYPE UCHAR
23 #define KD_BREAKPOINT_SIZE sizeof(UCHAR)
24 #define KD_BREAKPOINT_VALUE 0xCC
25
26 //
27 // Macros for getting and setting special purpose registers in portable code
28 //
29 #define KeGetContextPc(Context) \
30 ((Context)->Eip)
31
32 #define KeSetContextPc(Context, ProgramCounter) \
33 ((Context)->Eip = (ProgramCounter))
34
35 #define KeGetTrapFramePc(TrapFrame) \
36 ((TrapFrame)->Eip)
37
38 #define KeGetContextReturnRegister(Context) \
39 ((Context)->Eax)
40
41 #define KeSetContextReturnRegister(Context, ReturnValue) \
42 ((Context)->Eax = (ReturnValue))
43
44 //
45 // Macro to get trap and exception frame from a thread stack
46 //
47 #define KeGetTrapFrame(Thread) \
48 (PKTRAP_FRAME)((ULONG_PTR)((Thread)->InitialStack) - \
49 sizeof(KTRAP_FRAME) - \
50 sizeof(FX_SAVE_AREA))
51
52 #define KeGetExceptionFrame(Thread) \
53 NULL
54
55 //
56 // Macro to get context switches from the PRCB
57 // All architectures but x86 have it in the PRCB's KeContextSwitches
58 //
59 #define KeGetContextSwitches(Prcb) \
60 CONTAINING_RECORD(Prcb, KIPCR, PrcbData)->ContextSwitches
61
62 //
63 // Returns the Interrupt State from a Trap Frame.
64 // ON = TRUE, OFF = FALSE
65 //
66 #define KeGetTrapFrameInterruptState(TrapFrame) \
67 BooleanFlagOn((TrapFrame)->EFlags, EFLAGS_INTERRUPT_MASK)
68
69 //
70 // Invalidates the TLB entry for a specified address
71 //
72 FORCEINLINE
73 VOID
74 KeInvalidateTlbEntry(IN PVOID Address)
75 {
76 /* Invalidate the TLB entry for this address */
77 __invlpg(Address);
78 }
79
80 VOID
81 FASTCALL
82 Ki386InitializeTss(
83 IN PKTSS Tss,
84 IN PKIDTENTRY Idt,
85 IN PKGDTENTRY Gdt
86 );
87
88 VOID
89 NTAPI
90 KiSetCR0Bits(VOID);
91
92 VOID
93 NTAPI
94 KiGetCacheInformation(VOID);
95
96 BOOLEAN
97 NTAPI
98 KiIsNpxPresent(
99 VOID
100 );
101
102 BOOLEAN
103 NTAPI
104 KiIsNpxErrataPresent(
105 VOID
106 );
107
108 VOID
109 NTAPI
110 KiSetProcessorType(VOID);
111
112 ULONG
113 NTAPI
114 KiGetFeatureBits(VOID);
115
116 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
117 VOID
118 NTAPI
119 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
120 PKSTART_ROUTINE StartRoutine,
121 PVOID StartContext,
122 BOOLEAN UserThread,
123 KTRAP_FRAME TrapFrame);
124 #endif
125
126 NTSTATUS
127 NTAPI
128 Ke386GetGdtEntryThread(
129 IN PKTHREAD Thread,
130 IN ULONG Offset,
131 IN PKGDTENTRY Descriptor
132 );
133
134 VOID
135 NTAPI
136 KiFlushNPXState(
137 IN FLOATING_SAVE_AREA *SaveArea
138 );
139
140 VOID
141 NTAPI
142 Ki386AdjustEsp0(
143 IN PKTRAP_FRAME TrapFrame
144 );
145
146 VOID
147 NTAPI
148 Ki386SetupAndExitToV86Mode(
149 OUT PTEB VdmTeb
150 );
151
152 VOID
153 NTAPI
154 KeI386VdmInitialize(
155 VOID
156 );
157
158 ULONG_PTR
159 NTAPI
160 Ki386EnableGlobalPage(
161 IN volatile ULONG_PTR Context
162 );
163
164 VOID
165 NTAPI
166 KiI386PentiumLockErrataFixup(
167 VOID
168 );
169
170 VOID
171 NTAPI
172 KiInitializePAT(
173 VOID
174 );
175
176 VOID
177 NTAPI
178 KiInitializeMTRR(
179 IN BOOLEAN FinalCpu
180 );
181
182 VOID
183 NTAPI
184 KiAmdK6InitializeMTRR(
185 VOID
186 );
187
188 VOID
189 NTAPI
190 KiRestoreFastSyscallReturnState(
191 VOID
192 );
193
194 ULONG_PTR
195 NTAPI
196 Ki386EnableDE(
197 IN ULONG_PTR Context
198 );
199
200 ULONG_PTR
201 NTAPI
202 Ki386EnableFxsr(
203 IN ULONG_PTR Context
204 );
205
206 ULONG_PTR
207 NTAPI
208 Ki386EnableXMMIExceptions(
209 IN ULONG_PTR Context
210 );
211
212 //
213 // Global x86 only Kernel data
214 //
215 extern PVOID Ki386IopmSaveArea;
216 extern ULONG KeI386EFlagsAndMaskV86;
217 extern ULONG KeI386EFlagsOrMaskV86;
218 extern BOOLEAN KeI386VirtualIntExtensions;
219 extern KIDTENTRY KiIdt[MAXIMUM_IDTVECTOR];
220 extern KDESCRIPTOR KiIdtDescriptor;
221 extern ULONG Ke386GlobalPagesEnabled;
222 extern BOOLEAN KiI386PentiumLockErrataPresent;
223 extern ULONG KeI386NpxPresent;
224 extern ULONG KeI386XMMIPresent;
225 extern ULONG KeI386FxsrPresent;
226 extern ULONG KiMXCsrMask;
227 extern ULONG KeI386CpuType;
228 extern ULONG KeI386CpuStep;
229 extern UCHAR KiDebugRegisterTrapOffsets[9];
230 extern UCHAR KiDebugRegisterContextOffsets[9];
231 extern VOID __cdecl KiTrap2(VOID);
232 extern VOID __cdecl KiTrap8(VOID);
233 extern VOID __cdecl KiTrap19(VOID);
234 extern VOID __cdecl KiFastCallEntry(VOID);
235
236 //
237 // Sanitizes a selector
238 //
239 FORCEINLINE
240 ULONG
241 Ke386SanitizeSeg(IN ULONG Cs,
242 IN KPROCESSOR_MODE Mode)
243 {
244 //
245 // Check if we're in kernel-mode, and force CPL 0 if so.
246 // Otherwise, force CPL 3.
247 //
248 return ((Mode == KernelMode) ?
249 (Cs & (0xFFFF & ~RPL_MASK)) :
250 (RPL_MASK | (Cs & 0xFFFF)));
251 }
252
253 //
254 // Sanitizes EFLAGS
255 //
256 FORCEINLINE
257 ULONG
258 Ke386SanitizeFlags(IN ULONG Eflags,
259 IN KPROCESSOR_MODE Mode)
260 {
261 //
262 // Check if we're in kernel-mode, and sanitize EFLAGS if so.
263 // Otherwise, also force interrupt mask on.
264 //
265 return ((Mode == KernelMode) ?
266 (Eflags & (EFLAGS_USER_SANITIZE | EFLAGS_INTERRUPT_MASK)) :
267 (EFLAGS_INTERRUPT_MASK | (Eflags & EFLAGS_USER_SANITIZE)));
268 }
269
270 //
271 // Gets a DR register from a CONTEXT structure
272 //
273 FORCEINLINE
274 PVOID
275 KiDrFromContext(IN ULONG Dr,
276 IN PCONTEXT Context)
277 {
278 return *(PVOID*)((ULONG_PTR)Context + KiDebugRegisterContextOffsets[Dr]);
279 }
280
281 //
282 // Gets a DR register from a KTRAP_FRAME structure
283 //
284 FORCEINLINE
285 PVOID*
286 KiDrFromTrapFrame(IN ULONG Dr,
287 IN PKTRAP_FRAME TrapFrame)
288 {
289 return (PVOID*)((ULONG_PTR)TrapFrame + KiDebugRegisterTrapOffsets[Dr]);
290 }
291
292 //
293 // Sanitizes a Debug Register
294 //
295 FORCEINLINE
296 PVOID
297 Ke386SanitizeDr(IN PVOID DrAddress,
298 IN KPROCESSOR_MODE Mode)
299 {
300 //
301 // Check if we're in kernel-mode, and return the address directly if so.
302 // Otherwise, make sure it's not inside the kernel-mode address space.
303 // If it is, then clear the address.
304 //
305 return ((Mode == KernelMode) ? DrAddress :
306 (DrAddress <= MM_HIGHEST_USER_ADDRESS) ? DrAddress : 0);
307 }
308
309 #endif
310 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */