7e051d645deda93bc2e08480d1fcad764910946c
[reactos.git] / reactos / include / reactos / libs / pseh / pseh3.h
1 /*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GNU GPL - See COPYING in the top level directory
4 * PURPOSE: Header for PSEH3
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
6 */
7
8 /* For additional information see pseh3.c in the related library. */
9
10 #pragma once
11 #define _PSEH3_H_
12
13 #include "excpt.h"
14
15 typedef struct _SEH3$_SCOPE_TABLE
16 {
17 void *Target;
18 void *Filter;
19 } SEH3$_SCOPE_TABLE, *PSEH3$_SCOPE_TABLE;
20
21 typedef struct _SEH3$_EXCEPTION_POINTERS
22 {
23 struct _EXCEPTION_RECORD *ExceptionRecord;
24 struct _CONTEXT *ContextRecord;
25 } SEH3$_EXCEPTION_POINTERS, *PSEH3$_EXCEPTION_POINTERS;
26
27 typedef struct _SEH3$_REGISTRATION_FRAME
28 {
29 /* First the Windows base record. Don't move this! */
30 struct _SEH3$_REGISTRATION_FRAME *Next;
31 void *Handler;
32
33 /* Points to the end of the internal registration chain */
34 struct _SEH3$_REGISTRATION_FRAME *EndOfChain;
35
36 /* Pointer to the static scope table */
37 PSEH3$_SCOPE_TABLE ScopeTable;
38
39 /* Except handler stores pointer to exception pointers here */
40 PSEH3$_EXCEPTION_POINTERS volatile ExceptionPointers;
41
42 /* Registers that we need to save */
43 unsigned long Esp;
44 unsigned long Ebp;
45
46 } SEH3$_REGISTRATION_FRAME ,*PSEH3$_REGISTRATION_FRAME;
47
48 /* Prevent gcc from inlining functions that use SEH. */
49 static inline __attribute__((always_inline)) __attribute__((returns_twice)) void _SEH3$_PreventInlining() {}
50
51 extern inline __attribute__((always_inline,gnu_inline))
52 void _SEH3$_UnregisterFrame(volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame)
53 {
54 asm volatile ("movl %k[NewHead], %%fs:0"
55 : : [NewHead] "ir" (RegistrationFrame->Next) : "memory");
56 }
57
58 extern inline __attribute__((always_inline,gnu_inline))
59 void _SEH3$_UnregisterTryLevel(
60 volatile SEH3$_REGISTRATION_FRAME *TrylevelFrame)
61 {
62 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame;
63 asm volatile ("movl %%fs:0, %k[RegistrationFrame]"
64 : [RegistrationFrame] "=r" (RegistrationFrame) : );
65 RegistrationFrame->EndOfChain = TrylevelFrame->Next;
66 }
67
68 enum
69 {
70 _SEH3$_TryLevel = 0,
71 };
72
73 /* These are global dummy definitions, that get overwritten in the local context of __finally / __except blocks */
74 int __cdecl __attribute__((error ("Can only be used inside a __finally block."))) _abnormal_termination(void);
75 unsigned long __cdecl __attribute__((error("Can only be used inside an exception filter or __except block."))) _exception_code(void);
76 void * __cdecl __attribute__((error("Can only be used inside an exception filter."))) _exception_info(void);
77
78 /* Define the registers that get clobbered, when reaching the __except block.
79 We specify ebp on optimized builds without frame pointer, since it will be
80 used by GCC as a general purpose register then. */
81 #if defined(__OPTIMIZE__) && defined(_ALLOW_OMIT_FRAME_POINTER)
82 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebp", "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
83 #else
84 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
85 #endif
86
87 /* This attribute allows automatic cleanup of the registered frames */
88 #define _SEH3$_AUTO_CLEANUP __attribute__((cleanup(_SEH3$_AutoCleanup)))
89
90 #define _SEH3$_ASM_GOTO(_Asm, _Label, ...) asm goto (_Asm : : : "memory", ## __VA_ARGS__ : _Label)
91
92 #define _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
93 inline __attribute__((always_inline, gnu_inline)) \
94 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; }
95
96 /* This is an asm wrapper around _SEH3$_RegisterFrame */
97 #define _SEH3$_RegisterFrame(_TrylevelFrame, _DataTable, _Target) \
98 asm goto ("leal %0, %%ecx\n" \
99 "call __SEH3$_RegisterFrame\n" \
100 : \
101 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
102 : "ecx", "edx", "memory" \
103 : _Target)
104
105 /* This is an asm wrapper around _SEH3$_EnterTryLevel */
106 #define _SEH3$_RegisterTryLevel(_TrylevelFrame, _DataTable, _Target) \
107 asm goto ("leal %0, %%ecx\n" \
108 "call __SEH3$_RegisterTryLevel\n" \
109 : \
110 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
111 : "ecx", "edx", "memory" \
112 : _Target)
113
114 /* On GCC the filter function is a nested function with __fastcall calling
115 convention. The eax register contains a base address the function uses
116 to address the callers stack frame. __fastcall is chosen, because it gives
117 us an effective was of passing one argument to the function, that we need
118 to tell the function in a first pass to return informtion about the frame
119 base address. Since this is something GCC chooses arbitrarily, we call
120 the function with an arbitrary base address in eax first and then use the
121 result to calculate the correct address for a second call to the function. */
122 #define _SEH3$_DECLARE_FILTER_FUNC(_Name) \
123 auto int __fastcall _Name(int Action)
124
125 #define _SEH3$_NESTED_FUNC_OPEN(_Name) \
126 int __fastcall _Name(int Action) \
127 { \
128 /* This is a fancy way to get information about the frame layout */ \
129 if (Action == 0) return (int)&_SEH3$_TrylevelFrame;
130
131 #define _SEH3$_DEFINE_FILTER_FUNC(_Name, expression) \
132 _SEH3$_NESTED_FUNC_OPEN(_Name) \
133 /* Declare the intrinsics for exception filters */ \
134 inline __attribute__((always_inline, gnu_inline)) \
135 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; } \
136 inline __attribute__((always_inline, gnu_inline)) \
137 void * _exception_info() { return _SEH3$_TrylevelFrame.ExceptionPointers; } \
138 \
139 /* Now handle the actual filter expression */ \
140 return (expression); \
141 }
142
143 #define _SEH3$_FINALLY_FUNC_OPEN(_Name) \
144 _SEH3$_NESTED_FUNC_OPEN(_Name) \
145 /* Declare the intrinsics for the finally function */ \
146 inline __attribute__((always_inline, gnu_inline)) \
147 int _abnormal_termination() { return (_SEH3$_TrylevelFrame.ScopeTable != 0); }
148
149 #define _SEH3$_FILTER(_Filter, _FilterExpression) \
150 (__builtin_constant_p(_FilterExpression) ? (void*)(unsigned long)(unsigned char)(unsigned long)(_FilterExpression) : _Filter)
151
152 #define _SEH3$_DEFINE_DUMMY_FINALLY(_Name) \
153 auto inline __attribute__((always_inline,gnu_inline)) int _Name(int Action) { return 0; }
154
155 #define _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
156 auto inline __attribute__((always_inline,gnu_inline)) void _Name(volatile SEH3$_REGISTRATION_FRAME *p)
157
158 #define _SEH3$_DEFINE_CLEANUP_FUNC(_Name) \
159 _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
160 { \
161 /* Unregister the frame */ \
162 if (_SEH3$_TryLevel == 1) _SEH3$_UnregisterFrame(&_SEH3$_TrylevelFrame); \
163 else _SEH3$_UnregisterTryLevel(&_SEH3$_TrylevelFrame); \
164 \
165 /* Invoke the finally function (an inline dummy in the __except case) */ \
166 _SEH3$_FinallyFunction(1); \
167 }
168
169 /* This construct scares GCC so much, that it will stop moving code
170 around into places that are never executed. */
171 #define _SEH3$_SCARE_GCC() \
172 void *plabel; \
173 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_BeforeTry); \
174 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_HandlerTarget); \
175 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
176 asm volatile ("#" : "=a"(plabel) : "p"(&&_SEH3$_l_BeforeTry), "p"(&&_SEH3$_l_HandlerTarget), "p"(&&_SEH3$_l_OnException) \
177 : _SEH3$_CLOBBER_ON_EXCEPTION ); \
178 goto _SEH3$_l_OnException;
179
180
181 #define _SEH3_TRY \
182 _SEH3$_PreventInlining(); \
183 /* Enter the outer scope */ \
184 do { \
185 /* Declare local labels */ \
186 __label__ _SEH3$_l_BeforeTry; \
187 __label__ _SEH3$_l_DoTry; \
188 __label__ _SEH3$_l_AfterTry; \
189 __label__ _SEH3$_l_EndTry; \
190 __label__ _SEH3$_l_HandlerTarget; \
191 __label__ _SEH3$_l_OnException; \
192 \
193 /* Count the try level. Outside of any __try, _SEH3$_TryLevel is 0 */ \
194 enum { \
195 _SEH3$_PreviousTryLevel = _SEH3$_TryLevel, \
196 _SEH3$_TryLevel = _SEH3$_PreviousTryLevel + 1, \
197 }; \
198 \
199 /* Forward declaration of the auto cleanup function */ \
200 _SEH3$_DECLARE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
201 \
202 /* Allocate a registration frame */ \
203 volatile SEH3$_REGISTRATION_FRAME _SEH3$_AUTO_CLEANUP _SEH3$_TrylevelFrame; \
204 \
205 goto _SEH3$_l_BeforeTry; \
206 /* Silence warning */ goto _SEH3$_l_AfterTry; \
207 \
208 _SEH3$_l_DoTry: \
209 do
210
211
212 #define _SEH3_EXCEPT(...) \
213 /* End the try block */ \
214 while (0); \
215 _SEH3$_l_AfterTry: (void)0; \
216 goto _SEH3$_l_EndTry; \
217 \
218 _SEH3$_l_BeforeTry: (void)0; \
219 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
220 \
221 /* Forward declaration of the filter function */ \
222 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FilterFunction); \
223 \
224 /* Create a static data table that contains the jump target and filter function */ \
225 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { &&_SEH3$_l_HandlerTarget, _SEH3$_FILTER(&_SEH3$_FilterFunction, (__VA_ARGS__)) }; \
226 \
227 /* Register the registration record. */ \
228 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
229 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
230 \
231 /* Emit the filter function */ \
232 _SEH3$_DEFINE_FILTER_FUNC(_SEH3$_FilterFunction, (__VA_ARGS__)) \
233 \
234 /* Define an empty inline finally function */ \
235 _SEH3$_DEFINE_DUMMY_FINALLY(_SEH3$_FinallyFunction) \
236 \
237 /* Allow intrinsics for __except to be used */ \
238 _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
239 \
240 goto _SEH3$_l_DoTry; \
241 \
242 _SEH3$_l_HandlerTarget: (void)0; \
243 \
244 if (1) \
245 { \
246 do
247
248
249 #define _SEH3_FINALLY \
250 /* End the try block */ \
251 while (0); \
252 _SEH3$_l_AfterTry: (void)0; \
253 /* Set ScopeTable to 0, this is used by _abnormal_termination() */ \
254 _SEH3$_TrylevelFrame.ScopeTable = 0; \
255 \
256 goto _SEH3$_l_EndTry; \
257 \
258 _SEH3$_l_BeforeTry: (void)0; \
259 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
260 \
261 /* Forward declaration of the finally function */ \
262 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FinallyFunction); \
263 \
264 /* Create a static data table that contains the finally function */ \
265 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { 0, &_SEH3$_FinallyFunction }; \
266 \
267 /* Register the registration record. */ \
268 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
269 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
270 \
271 goto _SEH3$_l_DoTry; \
272 \
273 _SEH3$_l_HandlerTarget: (void)0; \
274 \
275 _SEH3$_FINALLY_FUNC_OPEN(_SEH3$_FinallyFunction) \
276 /* This construct makes sure that the finally function returns */ \
277 /* a proper value at the end */ \
278 for (; ; (void)({return 0; 0;}))
279
280
281 #define _SEH3_END \
282 while (0); \
283 }; \
284 goto _SEH3$_l_EndTry; \
285 \
286 _SEH3$_l_OnException: (void)0; \
287 /* Force GCC to create proper code pathes */ \
288 _SEH3$_SCARE_GCC() \
289 \
290 _SEH3$_l_EndTry:(void)0; \
291 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
292 \
293 /* Implementation of the auto cleanup function */ \
294 _SEH3$_DEFINE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
295 \
296 /* Close the outer scope */ \
297 } while (0);
298
299 #define _SEH3_LEAVE goto _SEH3$_l_AfterTry
300
301 #define _SEH3_VOLATILE volatile