2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/include/trap_x.h
5 * PURPOSE: Internal Inlined Functions for the Trap Handling Code
6 * PROGRAMMERS: ReactOS Portable Systems Group
14 KiDumpTrapFrame(IN PKTRAP_FRAME TrapFrame
)
16 /* Dump the whole thing */
17 DPRINT1("DbgEbp: %x\n", TrapFrame
->DbgEbp
);
18 DPRINT1("DbgEip: %x\n", TrapFrame
->DbgEip
);
19 DPRINT1("DbgArgMark: %x\n", TrapFrame
->DbgArgMark
);
20 DPRINT1("DbgArgPointer: %x\n", TrapFrame
->DbgArgPointer
);
21 DPRINT1("TempSegCs: %x\n", TrapFrame
->TempSegCs
);
22 DPRINT1("TempEsp: %x\n", TrapFrame
->TempEsp
);
23 DPRINT1("Dr0: %x\n", TrapFrame
->Dr0
);
24 DPRINT1("Dr1: %x\n", TrapFrame
->Dr1
);
25 DPRINT1("Dr2: %x\n", TrapFrame
->Dr2
);
26 DPRINT1("Dr3: %x\n", TrapFrame
->Dr3
);
27 DPRINT1("Dr6: %x\n", TrapFrame
->Dr6
);
28 DPRINT1("Dr7: %x\n", TrapFrame
->Dr7
);
29 DPRINT1("SegGs: %x\n", TrapFrame
->SegGs
);
30 DPRINT1("SegEs: %x\n", TrapFrame
->SegEs
);
31 DPRINT1("SegDs: %x\n", TrapFrame
->SegDs
);
32 DPRINT1("Edx: %x\n", TrapFrame
->Edx
);
33 DPRINT1("Ecx: %x\n", TrapFrame
->Ecx
);
34 DPRINT1("Eax: %x\n", TrapFrame
->Eax
);
35 DPRINT1("PreviousPreviousMode: %x\n", TrapFrame
->PreviousPreviousMode
);
36 DPRINT1("ExceptionList: %x\n", TrapFrame
->ExceptionList
);
37 DPRINT1("SegFs: %x\n", TrapFrame
->SegFs
);
38 DPRINT1("Edi: %x\n", TrapFrame
->Edi
);
39 DPRINT1("Esi: %x\n", TrapFrame
->Esi
);
40 DPRINT1("Ebx: %x\n", TrapFrame
->Ebx
);
41 DPRINT1("Ebp: %x\n", TrapFrame
->Ebp
);
42 DPRINT1("ErrCode: %x\n", TrapFrame
->ErrCode
);
43 DPRINT1("Eip: %x\n", TrapFrame
->Eip
);
44 DPRINT1("SegCs: %x\n", TrapFrame
->SegCs
);
45 DPRINT1("EFlags: %x\n", TrapFrame
->EFlags
);
46 DPRINT1("HardwareEsp: %x\n", TrapFrame
->HardwareEsp
);
47 DPRINT1("HardwareSegSs: %x\n", TrapFrame
->HardwareSegSs
);
48 DPRINT1("V86Es: %x\n", TrapFrame
->V86Es
);
49 DPRINT1("V86Ds: %x\n", TrapFrame
->V86Ds
);
50 DPRINT1("V86Fs: %x\n", TrapFrame
->V86Fs
);
51 DPRINT1("V86Gs: %x\n", TrapFrame
->V86Gs
);
57 KiFillTrapFrameDebug(IN PKTRAP_FRAME TrapFrame
)
59 /* Set the debug information */
60 TrapFrame
->DbgArgPointer
= TrapFrame
->Edx
;
61 TrapFrame
->DbgArgMark
= 0xBADB0D00;
62 TrapFrame
->DbgEip
= TrapFrame
->Eip
;
63 TrapFrame
->DbgEbp
= TrapFrame
->Ebp
;
68 KiExitTrapDebugChecks(IN PKTRAP_FRAME TrapFrame
,
69 IN KTRAP_STATE_BITS SkipBits
)
71 /* Make sure interrupts are disabled */
72 if (__readeflags() & EFLAGS_INTERRUPT_MASK
)
74 DPRINT1("Exiting with interrupts enabled: %lx\n", __readeflags());
78 /* Make sure this is a real trap frame */
79 if (TrapFrame
->DbgArgMark
!= 0xBADB0D00)
81 DPRINT1("Exiting with an invalid trap frame? (No MAGIC in trap frame)\n");
82 KiDumpTrapFrame(TrapFrame
);
86 /* Make sure we're not in user-mode or something */
87 if (Ke386GetFs() != KGDT_R0_PCR
)
89 DPRINT1("Exiting with an invalid FS: %lx\n", Ke386GetFs());
93 /* Make sure we have a valid SEH chain */
94 if (KeGetPcr()->Tib
.ExceptionList
== 0)
96 DPRINT1("Exiting with NULL exception chain: %p\n", KeGetPcr()->Tib
.ExceptionList
);
100 /* Make sure we're restoring a valid SEH chain */
101 if (TrapFrame
->ExceptionList
== 0)
103 DPRINT1("Entered a trap with a NULL exception chain: %p\n", TrapFrame
->ExceptionList
);
107 /* If we're ignoring previous mode, make sure caller doesn't actually want it */
108 if ((SkipBits
.SkipPreviousMode
) && (TrapFrame
->PreviousPreviousMode
!= -1))
110 DPRINT1("Exiting a trap witout restoring previous mode, yet previous mode seems valid: %lx", TrapFrame
->PreviousPreviousMode
);
117 KiExitSystemCallDebugChecks(IN ULONG SystemCall
,
118 IN PKTRAP_FRAME TrapFrame
)
122 /* Check if this was a user call */
123 if (KiUserMode(TrapFrame
))
125 /* Make sure we are not returning with elevated IRQL */
126 OldIrql
= KeGetCurrentIrql();
127 if (OldIrql
!= PASSIVE_LEVEL
)
129 /* Forcibly put us in a sane state */
130 KeGetPcr()->CurrentIrql
= PASSIVE_LEVEL
;
134 KeBugCheckEx(IRQL_GT_ZERO_AT_SYSTEM_SERVICE
,
141 /* Make sure we're not attached and that APCs are not disabled */
142 if ((KeGetCurrentThread()->ApcStateIndex
!= CurrentApcEnvironment
) ||
143 (KeGetCurrentThread()->CombinedApcDisable
!= 0))
146 KeBugCheckEx(APC_INDEX_MISMATCH
,
148 KeGetCurrentThread()->ApcStateIndex
,
149 KeGetCurrentThread()->CombinedApcDisable
,
155 #define KiExitTrapDebugChecks(x, y)
156 #define KiFillTrapFrameDebug(x)
157 #define KiExitSystemCallDebugChecks(x, y)
165 KiUserTrap(IN PKTRAP_FRAME TrapFrame
)
167 /* Anything else but Ring 0 is Ring 3 */
168 return (TrapFrame
->SegCs
& MODE_MASK
);
173 KiVdmTrap(IN PKTRAP_FRAME TrapFrame
)
175 /* Either the V8086 flag is on, or this is user-mode with a VDM */
176 return ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) ||
177 ((KiUserTrap(TrapFrame
)) && (PsGetCurrentProcess()->VdmObjects
)));
182 KiCheckForApcDelivery(IN PKTRAP_FRAME TrapFrame
)
187 /* Check for V8086 or user-mode trap */
188 if ((TrapFrame
->EFlags
& EFLAGS_V86_MASK
) ||
189 (KiUserTrap(TrapFrame
)))
192 Thread
= KeGetCurrentThread();
195 /* Turn off the alerted state for kernel mode */
196 Thread
->Alerted
[KernelMode
] = FALSE
;
198 /* Are there pending user APCs? */
199 if (!Thread
->ApcState
.UserApcPending
) break;
201 /* Raise to APC level and enable interrupts */
202 OldIrql
= KfRaiseIrql(APC_LEVEL
);
206 KiDeliverApc(UserMode
, NULL
, TrapFrame
);
208 /* Restore IRQL and disable interrupts once again */
209 KfLowerIrql(OldIrql
);
218 KiDispatchException0Args(IN NTSTATUS Code
,
219 IN ULONG_PTR Address
,
220 IN PKTRAP_FRAME TrapFrame
)
222 /* Helper for exceptions with no arguments */
223 KiDispatchExceptionFromTrapFrame(Code
, Address
, 0, 0, 0, 0, TrapFrame
);
229 KiDispatchException1Args(IN NTSTATUS Code
,
230 IN ULONG_PTR Address
,
232 IN PKTRAP_FRAME TrapFrame
)
234 /* Helper for exceptions with no arguments */
235 KiDispatchExceptionFromTrapFrame(Code
, Address
, 1, P1
, 0, 0, TrapFrame
);
241 KiDispatchException2Args(IN NTSTATUS Code
,
242 IN ULONG_PTR Address
,
245 IN PKTRAP_FRAME TrapFrame
)
247 /* Helper for exceptions with no arguments */
248 KiDispatchExceptionFromTrapFrame(Code
, Address
, 2, P1
, P2
, 0, TrapFrame
);
254 KiSystemCallReturn(IN PKTRAP_FRAME TrapFrame
)
256 /* Restore nonvolatiles, EAX, and do a "jump" back to the kernel caller */
260 "movl %c[b](%%esp), %%ebx\n"
261 "movl %c[s](%%esp), %%esi\n"
262 "movl %c[i](%%esp), %%edi\n"
263 "movl %c[p](%%esp), %%ebp\n"
264 "movl %c[a](%%esp), %%eax\n"
265 "movl %c[e](%%esp), %%edx\n"
266 "addl $%c[v],%%esp\n" /* A WHOLE *KERNEL* frame since we're not IRET'ing */
270 [b
] "i"(KTRAP_FRAME_EBX
),
271 [s
] "i"(KTRAP_FRAME_ESI
),
272 [i
] "i"(KTRAP_FRAME_EDI
),
273 [p
] "i"(KTRAP_FRAME_EBP
),
274 [a
] "i"(KTRAP_FRAME_EAX
),
275 [e
] "i"(KTRAP_FRAME_EIP
),
276 [v
] "i"(KTRAP_FRAME_ESP
)
285 KiSystemCallTrapReturn(IN PKTRAP_FRAME TrapFrame
)
287 /* Regular interrupt exit, but we only restore EAX as a volatile */
291 "movl %c[b](%%esp), %%ebx\n"
292 "movl %c[s](%%esp), %%esi\n"
293 "movl %c[i](%%esp), %%edi\n"
294 "movl %c[p](%%esp), %%ebp\n"
295 "movl %c[a](%%esp), %%eax\n"
296 "addl $%c[e],%%esp\n"
300 [b
] "i"(KTRAP_FRAME_EBX
),
301 [s
] "i"(KTRAP_FRAME_ESI
),
302 [i
] "i"(KTRAP_FRAME_EDI
),
303 [p
] "i"(KTRAP_FRAME_EBP
),
304 [a
] "i"(KTRAP_FRAME_EAX
),
305 [e
] "i"(KTRAP_FRAME_EIP
)
314 KiSystemCallSysExitReturn(IN PKTRAP_FRAME TrapFrame
)
316 /* Restore nonvolatiles, EAX, and do a SYSEXIT back to the user caller */
320 "movl %c[s](%%esp), %%esi\n"
321 "movl %c[b](%%esp), %%ebx\n"
322 "movl %c[i](%%esp), %%edi\n"
323 "movl %c[p](%%esp), %%ebp\n"
324 "movl %c[a](%%esp), %%eax\n"
325 "movl %c[e](%%esp), %%edx\n" /* SYSEXIT says EIP in EDX */
326 "movl %c[x](%%esp), %%ecx\n" /* SYSEXIT says ESP in ECX */
327 "addl $%c[v],%%esp\n" /* A WHOLE *USER* frame since we're not IRET'ing */
331 [b
] "i"(KTRAP_FRAME_EBX
),
332 [s
] "i"(KTRAP_FRAME_ESI
),
333 [i
] "i"(KTRAP_FRAME_EDI
),
334 [p
] "i"(KTRAP_FRAME_EBP
),
335 [a
] "i"(KTRAP_FRAME_EAX
),
336 [e
] "i"(KTRAP_FRAME_EIP
),
337 [x
] "i"(KTRAP_FRAME_ESP
),
338 [v
] "i"(KTRAP_FRAME_V86_ES
)
347 KiTrapReturn(IN PKTRAP_FRAME TrapFrame
)
349 /* Regular interrupt exit */
353 "movl %c[a](%%esp), %%eax\n"
354 "movl %c[b](%%esp), %%ebx\n"
355 "movl %c[c](%%esp), %%ecx\n"
356 "movl %c[d](%%esp), %%edx\n"
357 "movl %c[s](%%esp), %%esi\n"
358 "movl %c[i](%%esp), %%edi\n"
359 "movl %c[p](%%esp), %%ebp\n"
360 "addl $%c[e],%%esp\n"
364 [a
] "i"(KTRAP_FRAME_EAX
),
365 [b
] "i"(KTRAP_FRAME_EBX
),
366 [c
] "i"(KTRAP_FRAME_ECX
),
367 [d
] "i"(KTRAP_FRAME_EDX
),
368 [s
] "i"(KTRAP_FRAME_ESI
),
369 [i
] "i"(KTRAP_FRAME_EDI
),
370 [p
] "i"(KTRAP_FRAME_EBP
),
371 [e
] "i"(KTRAP_FRAME_EIP
)
380 KiEditedTrapReturn(IN PKTRAP_FRAME TrapFrame
)
382 /* Regular interrupt exit */
386 "movl %c[a](%%esp), %%eax\n"
387 "movl %c[b](%%esp), %%ebx\n"
388 "movl %c[c](%%esp), %%ecx\n"
389 "movl %c[d](%%esp), %%edx\n"
390 "movl %c[s](%%esp), %%esi\n"
391 "movl %c[i](%%esp), %%edi\n"
392 "movl %c[p](%%esp), %%ebp\n"
393 "addl $%c[e],%%esp\n"
394 "movl (%%esp), %%esp\n"
398 [a
] "i"(KTRAP_FRAME_EAX
),
399 [b
] "i"(KTRAP_FRAME_EBX
),
400 [c
] "i"(KTRAP_FRAME_ECX
),
401 [d
] "i"(KTRAP_FRAME_EDX
),
402 [s
] "i"(KTRAP_FRAME_ESI
),
403 [i
] "i"(KTRAP_FRAME_EDI
),
404 [p
] "i"(KTRAP_FRAME_EBP
),
405 [e
] "i"(KTRAP_FRAME_ERROR_CODE
) /* We *WANT* the error code since ESP is there! */
413 KiSystemCallTrampoline(IN PVOID Handler
,
420 * This sequence does a RtlCopyMemory(Stack - StackBytes, Arguments, StackBytes)
421 * and then calls the function associated with the system call.
423 * It's done in assembly for two reasons: we need to muck with the stack,
424 * and the call itself restores the stack back for us. The only way to do
425 * this in C is to do manual C handlers for every possible number of args on
426 * the stack, and then have the handler issue a call by pointer. This is
427 * wasteful since it'll basically push the values twice and require another
428 * level of call indirection.
430 * The ARM kernel currently does this, but it should probably be changed
431 * later to function like this as well.
437 "movl %%esp, %%edi\n"
447 : "%esp", "%esi", "%edi"
455 KiConvertToGuiThread(VOID
)
461 * Converting to a GUI thread safely updates ESP in-place as well as the
462 * current Thread->TrapFrame and EBP when KeSwitchKernelStack is called.
464 * However, PsConvertToGuiThread "helpfully" restores EBP to the original
465 * caller's value, since it is considered a nonvolatile register. As such,
466 * as soon as we're back after the conversion and we try to store the result
467 * which will probably be in some stack variable (EBP-based), we'll crash as
468 * we are touching the de-allocated non-expanded stack.
470 * Thus we need a way to update our EBP before EBP is touched, and the only
471 * way to guarantee this is to do the call itself in assembly, use the EAX
472 * register to store the result, fixup EBP, and then let the C code continue
480 "call _PsConvertToGuiThread@0\n"
484 : "=r"(Result
), "=r"(StackFrame
)
486 : "%esp", "%ecx", "%edx"
494 KiSwitchToBootStack(IN ULONG_PTR InitialStack
)
496 /* We have to switch to a new stack before continuing kernel initialization */
502 "jmp _KiSystemStartupBootStack@0\n"
505 "i"(NPX_FRAME_LENGTH
+ KTRAP_FRAME_ALIGN
+ KTRAP_FRAME_LENGTH
),
506 "i"(CR0_EM
| CR0_TS
| CR0_MP
)