ea2f8bc286f46c472dde399fb656a056ea04bc43
[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 // Returns the Interrupt State from a Trap Frame.
46 // ON = TRUE, OFF = FALSE
47 //
48 #define KeGetTrapFrameInterruptState(TrapFrame) \
49 BooleanFlagOn((TrapFrame)->EFlags, EFLAGS_INTERRUPT_MASK)
50
51 //
52 // Invalidates the TLB entry for a specified address
53 //
54 FORCEINLINE
55 VOID
56 KeInvalidateTlbEntry(IN PVOID Address)
57 {
58 /* Invalidate the TLB entry for this address */
59 __invlpg(Address);
60 }
61
62 VOID
63 FASTCALL
64 Ki386InitializeTss(
65 IN PKTSS Tss,
66 IN PKIDTENTRY Idt,
67 IN PKGDTENTRY Gdt
68 );
69
70 VOID
71 NTAPI
72 KiSetCR0Bits(VOID);
73
74 VOID
75 NTAPI
76 KiGetCacheInformation(VOID);
77
78 BOOLEAN
79 NTAPI
80 KiIsNpxPresent(
81 VOID
82 );
83
84 BOOLEAN
85 NTAPI
86 KiIsNpxErrataPresent(
87 VOID
88 );
89
90 VOID
91 NTAPI
92 KiSetProcessorType(VOID);
93
94 ULONG
95 NTAPI
96 KiGetFeatureBits(VOID);
97
98 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
99 VOID
100 NTAPI
101 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
102 PKSTART_ROUTINE StartRoutine,
103 PVOID StartContext,
104 BOOLEAN UserThread,
105 KTRAP_FRAME TrapFrame);
106 #endif
107
108 NTSTATUS
109 NTAPI
110 Ke386GetGdtEntryThread(
111 IN PKTHREAD Thread,
112 IN ULONG Offset,
113 IN PKGDTENTRY Descriptor
114 );
115
116 VOID
117 NTAPI
118 KiFlushNPXState(
119 IN FLOATING_SAVE_AREA *SaveArea
120 );
121
122 VOID
123 NTAPI
124 Ki386AdjustEsp0(
125 IN PKTRAP_FRAME TrapFrame
126 );
127
128 VOID
129 NTAPI
130 Ki386SetupAndExitToV86Mode(
131 OUT PTEB VdmTeb
132 );
133
134 VOID
135 NTAPI
136 KeI386VdmInitialize(
137 VOID
138 );
139
140 ULONG_PTR
141 NTAPI
142 Ki386EnableGlobalPage(
143 IN volatile ULONG_PTR Context
144 );
145
146 VOID
147 NTAPI
148 KiI386PentiumLockErrataFixup(
149 VOID
150 );
151
152 VOID
153 NTAPI
154 KiInitializePAT(
155 VOID
156 );
157
158 VOID
159 NTAPI
160 KiInitializeMTRR(
161 IN BOOLEAN FinalCpu
162 );
163
164 VOID
165 NTAPI
166 KiAmdK6InitializeMTRR(
167 VOID
168 );
169
170 VOID
171 NTAPI
172 KiRestoreFastSyscallReturnState(
173 VOID
174 );
175
176 ULONG_PTR
177 NTAPI
178 Ki386EnableDE(
179 IN ULONG_PTR Context
180 );
181
182 ULONG_PTR
183 NTAPI
184 Ki386EnableFxsr(
185 IN ULONG_PTR Context
186 );
187
188 ULONG_PTR
189 NTAPI
190 Ki386EnableXMMIExceptions(
191 IN ULONG_PTR Context
192 );
193
194 //
195 // Global x86 only Kernel data
196 //
197 extern PVOID Ki386IopmSaveArea;
198 extern ULONG KeI386EFlagsAndMaskV86;
199 extern ULONG KeI386EFlagsOrMaskV86;
200 extern BOOLEAN KeI386VirtualIntExtensions;
201 extern KIDTENTRY KiIdt[MAXIMUM_IDTVECTOR];
202 extern KDESCRIPTOR KiIdtDescriptor;
203 extern ULONG Ke386GlobalPagesEnabled;
204 extern BOOLEAN KiI386PentiumLockErrataPresent;
205 extern ULONG KeI386NpxPresent;
206 extern ULONG KeI386XMMIPresent;
207 extern ULONG KeI386FxsrPresent;
208 extern ULONG KiMXCsrMask;
209 extern ULONG KeI386CpuType;
210 extern ULONG KeI386CpuStep;
211 extern UCHAR KiDebugRegisterTrapOffsets[9];
212 extern UCHAR KiDebugRegisterContextOffsets[9];
213 extern VOID __cdecl KiTrap2(VOID);
214 extern VOID __cdecl KiTrap8(VOID);
215 extern VOID __cdecl KiTrap19(VOID);
216 extern VOID __cdecl KiFastCallEntry(VOID);
217
218 //
219 // Sanitizes a selector
220 //
221 FORCEINLINE
222 ULONG
223 Ke386SanitizeSeg(IN ULONG Cs,
224 IN KPROCESSOR_MODE Mode)
225 {
226 //
227 // Check if we're in kernel-mode, and force CPL 0 if so.
228 // Otherwise, force CPL 3.
229 //
230 return ((Mode == KernelMode) ?
231 (Cs & (0xFFFF & ~RPL_MASK)) :
232 (RPL_MASK | (Cs & 0xFFFF)));
233 }
234
235 //
236 // Sanitizes EFLAGS
237 //
238 FORCEINLINE
239 ULONG
240 Ke386SanitizeFlags(IN ULONG Eflags,
241 IN KPROCESSOR_MODE Mode)
242 {
243 //
244 // Check if we're in kernel-mode, and sanitize EFLAGS if so.
245 // Otherwise, also force interrupt mask on.
246 //
247 return ((Mode == KernelMode) ?
248 (Eflags & (EFLAGS_USER_SANITIZE | EFLAGS_INTERRUPT_MASK)) :
249 (EFLAGS_INTERRUPT_MASK | (Eflags & EFLAGS_USER_SANITIZE)));
250 }
251
252 //
253 // Gets a DR register from a CONTEXT structure
254 //
255 FORCEINLINE
256 PVOID
257 KiDrFromContext(IN ULONG Dr,
258 IN PCONTEXT Context)
259 {
260 return *(PVOID*)((ULONG_PTR)Context + KiDebugRegisterContextOffsets[Dr]);
261 }
262
263 //
264 // Gets a DR register from a KTRAP_FRAME structure
265 //
266 FORCEINLINE
267 PVOID*
268 KiDrFromTrapFrame(IN ULONG Dr,
269 IN PKTRAP_FRAME TrapFrame)
270 {
271 return (PVOID*)((ULONG_PTR)TrapFrame + KiDebugRegisterTrapOffsets[Dr]);
272 }
273
274 //
275 // Sanitizes a Debug Register
276 //
277 FORCEINLINE
278 PVOID
279 Ke386SanitizeDr(IN PVOID DrAddress,
280 IN KPROCESSOR_MODE Mode)
281 {
282 //
283 // Check if we're in kernel-mode, and return the address directly if so.
284 // Otherwise, make sure it's not inside the kernel-mode address space.
285 // If it is, then clear the address.
286 //
287 return ((Mode == KernelMode) ? DrAddress :
288 (DrAddress <= MM_HIGHEST_USER_ADDRESS) ? DrAddress : 0);
289 }
290
291 #endif
292 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */