2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NT Library
4 * FILE: lib/rtl/i386/except.S
5 * PURPOSE: User-mode exception support for IA-32
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 * Casper S. Hornstrup (chorns@users.sourceforge.net)
10 /* INCLUDES ******************************************************************/
13 .intel_syntax noprefix
15 #define EXCEPTION_UNWINDING 2
16 #define EXCEPTION_EXIT_UNWIND 4
17 #define EXCEPTION_UNWIND (EXCEPTION_UNWINDING + EXCEPTION_EXIT_UNWIND)
19 #define ExceptionContinueExecution 0
20 #define ExceptionContinueSearch 1
21 #define ExceptionNestedException 2
22 #define ExceptionCollidedUnwind 3
24 /* FUNCTIONS ****************************************************************/
26 .globl _RtlpGetExceptionList@0
27 _RtlpGetExceptionList@0:
29 /* Return the exception list */
30 mov eax, [fs:TEB_EXCEPTION_LIST]
33 .globl _RtlpSetExceptionList@4
34 _RtlpSetExceptionList@4:
36 /* Get the new list */
41 mov [fs:TEB_EXCEPTION_LIST], ecx
46 .globl _RtlpGetExceptionAddress@0
47 _RtlpGetExceptionAddress@0:
49 /* Return the address from the stack */
55 .globl _RtlCaptureContext@4
58 /* Preserve EBX and put the context in it */
62 /* Save the basic register context */
63 mov [ebx+CONTEXT_EAX], eax
64 mov [ebx+CONTEXT_ECX], ecx
65 mov [ebx+CONTEXT_EDX], edx
66 mov eax, [esp] /* We pushed EBX, remember? ;) */
67 mov [ebx+CONTEXT_EBX], eax
68 mov [ebx+CONTEXT_ESI], esi
69 mov [ebx+CONTEXT_EDI], edi
71 /* Capture the other regs */
74 .globl _RtlpCaptureContext@4
75 _RtlpCaptureContext@4:
77 /* Preserve EBX and put the context in it */
81 /* Clear the basic register context */
82 mov dword ptr [ebx+CONTEXT_EAX], 0
83 mov dword ptr [ebx+CONTEXT_ECX], 0
84 mov dword ptr [ebx+CONTEXT_EDX], 0
85 mov dword ptr [ebx+CONTEXT_EBX], 0
86 mov dword ptr [ebx+CONTEXT_ESI], 0
87 mov dword ptr [ebx+CONTEXT_EDI], 0
90 /* Capture the segment registers */
91 mov [ebx+CONTEXT_SEGCS], cs
92 mov [ebx+CONTEXT_SEGDS], ds
93 mov [ebx+CONTEXT_SEGES], es
94 mov [ebx+CONTEXT_SEGFS], fs
95 mov [ebx+CONTEXT_SEGGS], gs
96 mov [ebx+CONTEXT_SEGSS], ss
100 pop [ebx+CONTEXT_EFLAGS]
102 /* The return address should be in [ebp+4] */
104 mov [ebx+CONTEXT_EIP], eax
108 mov [ebx+CONTEXT_EBP], eax
112 mov [ebx+CONTEXT_ESP], eax
114 /* Return to the caller */
118 .globl _RtlpExecuteHandlerForException@20
119 _RtlpExecuteHandlerForException@20:
121 /* Copy the routine in EDX */
122 mov edx, offset _RtlpExceptionProtector
124 /* Jump to common routine */
125 jmp _RtlpExecuteHandler@20
127 .globl _RtlpExecuteHandlerForUnwind@20
128 _RtlpExecuteHandlerForUnwind@20:
130 /* Copy the routine in EDX */
131 mov edx, offset _RtlpExceptionProtector
133 /* Run the common routine */
134 _RtlpExecuteHandler@20:
136 /* Save non-volatile */
141 /* Clear registers */
147 /* Call the 2nd-stage executer */
153 call _RtlpExecuteHandler2@20
155 /* Restore non-volatile */
161 .globl _RtlpExecuteHandler2@20
162 _RtlpExecuteHandler2@20:
164 /* Set up stack frame */
171 /* Push handler address */
174 /* Push the exception list */
175 push [fs:TEB_EXCEPTION_LIST]
178 mov [fs:TEB_EXCEPTION_LIST], esp
180 /* Call the handler */
189 mov esp, [fs:TEB_EXCEPTION_LIST]
192 pop [fs:TEB_EXCEPTION_LIST]
194 /* Undo stack frame and return */
199 _RtlpExceptionProtector:
201 /* Assume we'll continue */
202 mov eax, ExceptionContinueSearch
204 /* Put the exception record in ECX and check the Flags */
206 test dword ptr [ecx+EXCEPTION_RECORD_EXCEPTION_FLAGS], EXCEPTION_UNWIND
209 /* Save the frame in ECX and Context in EDX */
213 /* Get the nested frame */
216 /* Set it as the dispatcher context */
219 /* Return nested exception */
220 mov eax, ExceptionNestedException
225 _RtlpUnwindProtector:
226 /* Assume we'll continue */
227 mov eax, ExceptionContinueSearch
229 /* Put the exception record in ECX and check the Flags */
231 test dword ptr [ecx+EXCEPTION_RECORD_EXCEPTION_FLAGS], EXCEPTION_UNWIND
234 /* Save the frame in ECX and Context in EDX */
238 /* Get the nested frame */
241 /* Set it as the dispatcher context */
244 /* Return collided unwind */
245 mov eax, ExceptionCollidedUnwind