8a3be57e03f4e3d1b847e47b5072eee23c16258e
[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 /* We need the full structure with all non-volatile */
38 #define _SEH3$_FRAME_ALL_NONVOLATILES 1
39 #include "pseh3.h"
40 #include "pseh3_asmdef.h"
41
42 /* Make sure the asm definitions match the structures */
43 C_ASSERT(SEH3_REGISTRATION_FRAME_Next == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Next));
44 C_ASSERT(SEH3_REGISTRATION_FRAME_Handler == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Handler));
45 C_ASSERT(SEH3_REGISTRATION_FRAME_EndOfChain == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, EndOfChain));
46 C_ASSERT(SEH3_REGISTRATION_FRAME_ScopeTable == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, ScopeTable));
47 C_ASSERT(SEH3_REGISTRATION_FRAME_ExceptionPointers == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, ExceptionPointers));
48 C_ASSERT(SEH3_REGISTRATION_FRAME_Esp == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Esp));
49 C_ASSERT(SEH3_REGISTRATION_FRAME_Ebp == FIELD_OFFSET(SEH3$_REGISTRATION_FRAME, Ebp));
50 C_ASSERT(SEH3_SCOPE_TABLE_Filter == FIELD_OFFSET(SEH3$_SCOPE_TABLE, Filter));
51 C_ASSERT(SEH3_SCOPE_TABLE_Target == FIELD_OFFSET(SEH3$_SCOPE_TABLE, Target));
52
53 void
54 __attribute__((regparm(1)))
55 _SEH3$_Unregister(
56 volatile SEH3$_REGISTRATION_FRAME *Frame)
57 {
58 if (Frame->Handler)
59 _SEH3$_UnregisterFrame(Frame);
60 else
61 _SEH3$_UnregisterTryLevel(Frame);
62 }
63
64 static inline
65 LONG
66 _SEH3$_InvokeNestedFunctionFilter(
67 PSEH3$_REGISTRATION_FRAME RegistrationFrame,
68 PVOID Filter)
69 {
70 LONG FilterResult;
71
72 asm volatile (
73 /* First call with param = 0 to get the frame layout */
74 "xorl %%ecx, %%ecx\n\t"
75 "xorl %%eax, %%eax\n\t"
76 "call *%[Filter]\n\t"
77
78 /* The result is the frame base address that we passed in (0) plus the
79 offset to the registration record. */
80 "negl %%eax\n\t"
81 "addl %[RegistrationFrame], %%eax\n\t"
82
83 /* Second call to get the filter result */
84 "mov $1, %%ecx\n\t"
85 "call *%[Filter]"
86 : "=a" (FilterResult)
87 : [RegistrationFrame] "m" (RegistrationFrame), [Filter] "m" (Filter)
88 : "ecx", "edx");
89
90 return FilterResult;
91 }
92
93 long
94 __attribute__((regparm(1)))
95 _SEH3$_InvokeEmbeddedFilter(
96 PSEH3$_REGISTRATION_FRAME RegistrationFrame);
97
98 long
99 __attribute__((regparm(1)))
100 _SEH3$_InvokeEmbeddedFilterFromRegistration(
101 PSEH3$_REGISTRATION_FRAME RegistrationFrame);
102
103 static inline
104 LONG
105 _SEH3$_InvokeFilter(
106 PSEH3$_REGISTRATION_FRAME RegistrationFrame,
107 PVOID Filter)
108 {
109 LONG FilterResult;
110
111 if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_NESTED_HANDLER)
112 {
113 return _SEH3$_InvokeNestedFunctionFilter(RegistrationFrame, Filter);
114 }
115 else if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CPP_HANDLER)
116 {
117 /* Call the embedded filter function */
118 return _SEH3$_InvokeEmbeddedFilter(RegistrationFrame);
119 }
120 else if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CLANG_HANDLER)
121 {
122 return _SEH3$_InvokeEmbeddedFilterFromRegistration(RegistrationFrame);
123 }
124 else
125 {
126 /* Should not happen! Skip this handler */
127 FilterResult = EXCEPTION_CONTINUE_SEARCH;
128 }
129
130 return FilterResult;
131 }
132
133 void
134 __attribute__((regparm(1)))
135 _SEH3$_AutoCleanup(
136 SEH3$_REGISTRATION_FRAME *Frame)
137 {
138 /* Check for __finally frames */
139 if (Frame->ScopeTable->Target == NULL)
140 {
141 _SEH3$_InvokeFilter(Frame, Frame->ScopeTable->Filter);
142 }
143
144 if (Frame->Handler)
145 _SEH3$_UnregisterFrame(Frame);
146 else
147 _SEH3$_UnregisterTryLevel(Frame);
148 }
149
150 static inline
151 LONG
152 _SEH3$_GetFilterResult(
153 PSEH3$_REGISTRATION_FRAME Record)
154 {
155 PVOID Filter = Record->ScopeTable->Filter;
156 LONG Result;
157
158 /* Check for __finally frames */
159 if (Record->ScopeTable->Target == NULL)
160 {
161 return EXCEPTION_CONTINUE_SEARCH;
162 }
163
164 /* Check if we have a constant filter */
165 if (((ULONG)Filter & 0xFFFFFF00) == 0)
166 {
167 /* Lowest 8 bit are sign extended to give the result */
168 Result = (LONG)(CHAR)(ULONG)Filter;
169 }
170 else
171 {
172 /* Call the filter function */
173 Result = _SEH3$_InvokeFilter(Record, Filter);
174 }
175
176 /* Normalize the result */
177 if (Result < 0) return EXCEPTION_CONTINUE_EXECUTION;
178 else if (Result > 0) return EXCEPTION_EXECUTE_HANDLER;
179 else return EXCEPTION_CONTINUE_SEARCH;
180 }
181
182 static inline
183 VOID
184 _SEH3$_CallFinally(
185 PSEH3$_REGISTRATION_FRAME Record)
186 {
187 _SEH3$_InvokeFilter(Record, Record->ScopeTable->Filter);
188 }
189
190 __attribute__((noreturn))
191 static inline
192 void
193 _SEH3$_JumpToTarget(
194 PSEH3$_REGISTRATION_FRAME RegistrationFrame)
195 {
196 if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CLANG_HANDLER)
197 {
198 asm volatile (
199 /* Load the registers */
200 "movl 24(%%ecx), %%esp\n\t"
201 "movl 28(%%ecx), %%ebp\n\t"
202
203 /* Stack pointer is 4 off from the call to __SEH3$_RegisterFrame */
204 "addl $4, %%esp\n\t"
205
206 /* Jump into the exception handler */
207 "jmp *%[Target]"
208 : :
209 "c" (RegistrationFrame),
210 "a" (RegistrationFrame->ScopeTable),
211 [Target] "m" (RegistrationFrame->ScopeTable->Target)
212 );
213 }
214 else
215 {
216 asm volatile (
217 /* Load the registers */
218 "movl 24(%%ecx), %%esp\n\t"
219 "movl 28(%%ecx), %%ebp\n\t"
220
221 /* Stack pointer is 4 off from the call to __SEH3$_RegisterFrame */
222 "addl $4, %%esp\n\t"
223
224 /* Jump into the exception handler */
225 "jmp *%[Target]"
226 : :
227 "c" (RegistrationFrame),
228 "a" (RegistrationFrame->ScopeTable),
229 [Target] "m" (RegistrationFrame->ScopeTable->Target)
230 );
231 }
232
233 __builtin_unreachable();
234 }
235
236 void
237 __fastcall
238 _SEH3$_CallRtlUnwind(
239 PSEH3$_REGISTRATION_FRAME RegistrationFrame);
240
241
242 EXCEPTION_DISPOSITION
243 __cdecl
244 __attribute__ ((__target__ ("cld")))
245 _SEH3$_except_handler(
246 struct _EXCEPTION_RECORD * ExceptionRecord,
247 PSEH3$_REGISTRATION_FRAME EstablisherFrame,
248 struct _CONTEXT * ContextRecord,
249 void * DispatcherContext)
250 {
251 PSEH3$_REGISTRATION_FRAME CurrentFrame, TargetFrame;
252 SEH3$_EXCEPTION_POINTERS ExceptionPointers;
253 LONG FilterResult;
254
255 /* Clear the direction flag. */
256 asm volatile ("cld" : : : "memory");
257
258 /* Save the exception pointers on the stack */
259 ExceptionPointers.ExceptionRecord = ExceptionRecord;
260 ExceptionPointers.ContextRecord = ContextRecord;
261
262 /* Check if this is an unwind */
263 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
264 {
265 /* Unwind all local frames */
266 TargetFrame = EstablisherFrame->Next;
267 }
268 else
269 {
270 /* Loop all frames for this registration */
271 CurrentFrame = EstablisherFrame->EndOfChain;
272 for (;;)
273 {
274 /* Check if we have an exception handler */
275 if (CurrentFrame->ScopeTable->Target != NULL)
276 {
277 /* Set exception pointers and code for this frame */
278 CurrentFrame->ExceptionPointers = &ExceptionPointers;
279 CurrentFrame->ExceptionCode = ExceptionRecord->ExceptionCode;
280
281 /* Get the filter result */
282 FilterResult = _SEH3$_GetFilterResult(CurrentFrame);
283
284 /* Check, if continuuing is requested */
285 if (FilterResult == EXCEPTION_CONTINUE_EXECUTION)
286 {
287 return ExceptionContinueExecution;
288 }
289
290 /* Check if the except handler shall be executed */
291 if (FilterResult == EXCEPTION_EXECUTE_HANDLER) break;
292 }
293
294 /* Bail out if this is the last handler */
295 if (CurrentFrame == EstablisherFrame)
296 return ExceptionContinueSearch;
297
298 /* Go to the next frame */
299 CurrentFrame = CurrentFrame->Next;
300 }
301
302 /* Call RtlUnwind to unwind the frames below this one */
303 _SEH3$_CallRtlUnwind(EstablisherFrame);
304
305 /* Do a local unwind up to this frame */
306 TargetFrame = CurrentFrame;
307 }
308
309 /* Loop frames up to the target frame */
310 for (CurrentFrame = EstablisherFrame->EndOfChain;
311 CurrentFrame != TargetFrame;
312 CurrentFrame = CurrentFrame->Next)
313 {
314 /* Manually unregister the frame */
315 _SEH3$_Unregister(CurrentFrame);
316
317 /* Check if this is an unwind frame */
318 if (CurrentFrame->ScopeTable->Target == NULL)
319 {
320 /* Set exception pointers and code for this frame */
321 CurrentFrame->ExceptionPointers = &ExceptionPointers;
322 CurrentFrame->ExceptionCode = ExceptionRecord->ExceptionCode;
323
324 /* Call the finally function */
325 _SEH3$_CallFinally(CurrentFrame);
326 }
327 }
328
329 /* Check if this was an unwind */
330 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
331 {
332 return ExceptionContinueSearch;
333 }
334
335 /* Unregister the frame. It will be unregistered again at the end of the
336 __except block, due to auto cleanup, but that doesn't hurt.
337 All we do is set either fs:[0] or EstablisherFrame->EndOfChain to
338 CurrentFrame->Next, which will not change it's value. */
339 _SEH3$_Unregister(CurrentFrame);
340
341 /* Jump to the __except block (does not return) */
342 _SEH3$_JumpToTarget(CurrentFrame);
343 }
344