[PSEH3]
[reactos.git] / reactos / lib / pseh / i386 / pseh3.c
1 /*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GNU GPL - See COPYING in the top level directory
4 * PURPOSE: Support library for PSEH3
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
6 */
7
8 /*
9 * - Naming: To avoid naming conflicts, all internal identifiers are prefixed
10 * with _SEH3$_.
11 * - Frame graph: PSEH3 uses the same registration frame for every trylevel.
12 * Only the top trylevel is registered in FS:0, the inner trylevels are linked
13 * to the first trylevel frame. Only the first trylevel frame has the Handler
14 * member set, it's 0 for all others as an identification. The EndOfChain
15 * member of the FS:0 registered frame points to the last internal frame,
16 * which is the frame itself, when only 1 trylevel is present.
17 *
18 * The registration graph looks like this:
19 *
20 * newer handlers
21 * ---------------->
22 *
23 * fs:0 /----------------\
24 * |-----------|<-\ |-----------|<-\ / |----------|<-\ \->|----------|
25 * | <Next> | \-| <Next> | \--/--| <Next> | \---| <Next> |
26 * | <Handler> | | <Handler> | / | <NULL> | | <NULL> |
27 * |-----------| |-----------| / |----------| |----------|
28 * |EndOfChain |---/
29 * | ... |
30 * |-----------|
31 */
32
33 #include <stdarg.h>
34 #include <windef.h>
35 #include <winnt.h>
36
37 #include "pseh3.h"
38 #include "pseh3_asmdef.h"
39
40 /* Make sure the asm definitions match the structures */
41 C_ASSERT(SEH3_REGISTRATION_FRAME_Next == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Next));
42 C_ASSERT(SEH3_REGISTRATION_FRAME_Handler == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Handler));
43 C_ASSERT(SEH3_REGISTRATION_FRAME_EndOfChain == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, EndOfChain));
44 C_ASSERT(SEH3_REGISTRATION_FRAME_ScopeTable == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, ScopeTable));
45 C_ASSERT(SEH3_REGISTRATION_FRAME_ExceptionPointers == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, ExceptionPointers));
46 C_ASSERT(SEH3_REGISTRATION_FRAME_Esp == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Esp));
47 C_ASSERT(SEH3_REGISTRATION_FRAME_Ebp == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Ebp));
48 C_ASSERT(SEH3_SCOPE_TABLE_Filter == FIELD_OFFSET(SEH3$_SCOPE_TABLE, Filter));
49 C_ASSERT(SEH3_SCOPE_TABLE_Target == FIELD_OFFSET(SEH3$_SCOPE_TABLE, Target));
50
51 static inline
52 void _SEH3$_Unregister(
53 volatile SEH3$_REGISTRATION_FRAME *Frame)
54 {
55 if (Frame->Handler)
56 _SEH3$_UnregisterFrame(Frame);
57 else
58 _SEH3$_UnregisterTryLevel(Frame);
59 }
60
61 static inline
62 LONG
63 _SEH3$_InvokeFilter(
64 PVOID Record,
65 PVOID Filter)
66 {
67 LONG FilterResult;
68
69 asm volatile (
70 /* First call with param = 0 to get the frame layout */
71 "xorl %%ecx, %%ecx\n\t"
72 "xorl %%eax, %%eax\n\t"
73 "call *%[Filter]\n\t"
74
75 /* The result is the frame base address that we passed in (0) plus the
76 offset to the registration record. */
77 "negl %%eax\n\t"
78 "addl %[Record], %%eax\n\t"
79
80 /* Second call to get the filter result */
81 "mov $1, %%ecx\n\t"
82 "call *%[Filter]\n\t"
83 : "=a"(FilterResult)
84 : [Record] "m" (Record), [Filter] "m" (Filter)
85 : "ecx", "edx");
86
87 return FilterResult;
88 }
89
90 static inline
91 LONG
92 _SEH3$_GetFilterResult(
93 PSEH3$_REGISTRATION_FRAME Record)
94 {
95 PVOID Filter = Record->ScopeTable->Filter;
96 LONG Result;
97
98 /* Check for __finally frames */
99 if (Record->ScopeTable->Target == NULL)
100 {
101 return EXCEPTION_CONTINUE_SEARCH;
102 }
103
104 /* Check if we have a constant filter */
105 if (((ULONG)Filter & 0xFFFFFF00) == 0)
106 {
107 /* Lowest 8 bit are sign extended to give the result */
108 Result = (LONG)(CHAR)(ULONG)Filter;
109 }
110 else
111 {
112 /* Call the filter function */
113 Result = _SEH3$_InvokeFilter(Record, Filter);
114 }
115
116 /* Normalize the result */
117 if (Result < 0) return EXCEPTION_CONTINUE_EXECUTION;
118 else if (Result > 0) return EXCEPTION_EXECUTE_HANDLER;
119 else return EXCEPTION_CONTINUE_SEARCH;
120 }
121
122 static inline
123 VOID
124 _SEH3$_CallFinally(
125 PSEH3$_REGISTRATION_FRAME Record)
126 {
127 _SEH3$_InvokeFilter(Record, Record->ScopeTable->Filter);
128 }
129
130 __attribute__((noreturn))
131 static inline
132 void
133 _SEH3$_JumpToTarget(
134 PSEH3$_REGISTRATION_FRAME RegistrationFrame)
135 {
136 asm volatile (
137 /* Load the registers */
138 "movl 20(%%ecx), %%esp\n"
139 "movl 24(%%ecx), %%ebp\n"
140
141 /* Stack pointer is 4 off from the call to __SEH3$_RegisterFrame */
142 "addl $4, %%esp\n"
143
144 /* Jump into the exception handler */
145 "jmp *%[Target]\n"
146 : :
147 "c" (RegistrationFrame),
148 "a" (RegistrationFrame->ScopeTable),
149 [Target] "m" (RegistrationFrame->ScopeTable->Target)
150 );
151
152 __builtin_unreachable();
153 }
154
155 static inline
156 void
157 _SEH3$_CallRtlUnwind(
158 PSEH3$_REGISTRATION_FRAME RegistrationFrame)
159 {
160 LONG ClobberedEax;
161
162 asm volatile(
163 "push %%ebp\n"
164 "push $0\n"
165 "push $0\n"
166 "push $0\n"
167 "push %[TargetFrame]\n"
168 "call _RtlUnwind@16\n"
169 "pop %%ebp\n"
170 : "=a" (ClobberedEax)
171 : [TargetFrame] "a" (RegistrationFrame)
172 : "ebx", "ecx", "edx", "esi",
173 "edi", "flags", "memory");
174 }
175
176 EXCEPTION_DISPOSITION
177 __cdecl
178 __attribute__ ((__target__ ("cld")))
179 _SEH3$_except_handler(
180 struct _EXCEPTION_RECORD * ExceptionRecord,
181 PSEH3$_REGISTRATION_FRAME EstablisherFrame,
182 struct _CONTEXT * ContextRecord,
183 void * DispatcherContext)
184 {
185 PSEH3$_REGISTRATION_FRAME CurrentFrame, TargetFrame;
186 SEH3$_EXCEPTION_POINTERS ExceptionPointers;
187 LONG FilterResult;
188
189 /* Clear the direction flag. */
190 asm volatile ("cld\n" : : : "memory");
191
192 /* Check if this is an unwind */
193 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
194 {
195 /* Unwind all local frames */
196 TargetFrame = EstablisherFrame->Next;
197 }
198 else
199 {
200 /* Save the exception pointers on the stack */
201 ExceptionPointers.ExceptionRecord = ExceptionRecord;
202 ExceptionPointers.ContextRecord = ContextRecord;
203
204 /* Loop all frames for this registration */
205 CurrentFrame = EstablisherFrame->EndOfChain;
206 for (;;)
207 {
208 /* Check if we have an exception handler */
209 if (CurrentFrame->ScopeTable->Target != NULL)
210 {
211 /* Set exception pointers for this frame */
212 CurrentFrame->ExceptionPointers = &ExceptionPointers;
213
214 /* Get the filter result */
215 FilterResult = _SEH3$_GetFilterResult(CurrentFrame);
216
217 /* Check, if continuuing is requested */
218 if (FilterResult == EXCEPTION_CONTINUE_EXECUTION)
219 {
220 return ExceptionContinueExecution;
221 }
222
223 /* Check if the except handler shall be executed */
224 if (FilterResult == EXCEPTION_EXECUTE_HANDLER) break;
225 }
226
227 /* Bail out if this is the last handler */
228 if (CurrentFrame == EstablisherFrame)
229 return ExceptionContinueSearch;
230
231 /* Go to the next frame */
232 CurrentFrame = CurrentFrame->Next;
233 }
234
235 /* Call RtlUnwind to unwind the frames below this one */
236 _SEH3$_CallRtlUnwind(EstablisherFrame);
237
238 /* Do a local unwind up to this frame */
239 TargetFrame = CurrentFrame;
240 }
241
242 /* Loop frames up to the target frame */
243 for (CurrentFrame = EstablisherFrame->EndOfChain;
244 CurrentFrame != TargetFrame;
245 CurrentFrame = CurrentFrame->Next)
246 {
247 /* Manually unregister the frame */
248 _SEH3$_Unregister(CurrentFrame);
249
250 /* Check if this is an unwind frame */
251 if (CurrentFrame->ScopeTable->Target == NULL)
252 {
253 /* Set exception pointers for this frame */
254 CurrentFrame->ExceptionPointers = &ExceptionPointers;
255
256 /* Call the finally function */
257 _SEH3$_CallFinally(CurrentFrame);
258 }
259 }
260
261 /* Check if this was an unwind */
262 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
263 {
264 return ExceptionContinueSearch;
265 }
266
267 /* Unregister the frame. It will be unregistered again at the end of the
268 __except block, due to auto cleanup, but that doesn't hurt.
269 All we do is set either fs:[0] or EstablisherFrame->EndOfChain to
270 CurrentFrame->Next, which will not change it's value. */
271 _SEH3$_Unregister(CurrentFrame);
272
273 /* Jump to the __except block (does not return) */
274 _SEH3$_JumpToTarget(CurrentFrame);
275 }
276