Use emergency TEB selector to avoid debugger crashes when stepping
[wine] / include / sig_context.h
1 /*
2  * Signal context definitions
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
7 #ifndef __WINE_SIG_CONTEXT_H
8 #define __WINE_SIG_CONTEXT_H
9
10 #ifdef __i386__
11
12 #ifdef linux
13 typedef struct
14 {
15     unsigned short sc_gs, __gsh;
16     unsigned short sc_fs, __fsh;
17     unsigned short sc_es, __esh;
18     unsigned short sc_ds, __dsh;
19     unsigned long sc_edi;
20     unsigned long sc_esi;
21     unsigned long sc_ebp;
22     unsigned long sc_esp;
23     unsigned long sc_ebx;
24     unsigned long sc_edx;
25     unsigned long sc_ecx;
26     unsigned long sc_eax;
27     unsigned long sc_trapno;
28     unsigned long sc_err;
29     unsigned long sc_eip;
30     unsigned short sc_cs, __csh;
31     unsigned long sc_eflags;
32     unsigned long esp_at_signal;
33     unsigned short sc_ss, __ssh;
34     unsigned long i387;
35     unsigned long oldmask;
36     unsigned long cr2;
37 } SIGCONTEXT;
38
39 #define HANDLER_DEF(name) void name (int signal, SIGCONTEXT context)
40 #define HANDLER_CONTEXT (&context)
41
42 #endif  /* linux */
43
44 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
45
46 #include <signal.h>
47 typedef struct sigcontext SIGCONTEXT;
48
49 #define HANDLER_DEF(name) void name(int signal, int code, SIGCONTEXT *context)
50 #define HANDLER_CONTEXT context
51
52 #endif  /* FreeBSD */
53
54 #if defined(__svr4__) || defined(_SCO_DS)
55
56 #include <signal.h>
57 #ifdef _SCO_DS
58 #include <sys/regset.h>
59 #endif
60 #include <sys/ucontext.h>
61 typedef struct ucontext SIGCONTEXT;
62
63 #define HANDLER_DEF(name) void name(int signal, void *siginfo, SIGCONTEXT *context)
64 #define HANDLER_CONTEXT context
65
66 #endif  /* svr4 || SCO_DS */
67
68 #ifdef __EMX__
69 typedef unsigned long ULONG;
70 typedef unsigned short USHORT;
71 typedef struct _fpreg           /* Note 1 */
72 {
73   ULONG  losig;
74   ULONG  hisig;
75   USHORT signexp;
76 } FPREG;
77 typedef FPREG *PFPREG;
78
79 typedef struct _CONTEXT         /* Note 1 */
80 {
81   ULONG ContextFlags;
82   ULONG ctx_env[7];
83   FPREG ctx_stack[8];
84   ULONG ctx_SegGs;
85   ULONG ctx_SegFs;
86   ULONG ctx_SegEs;
87   ULONG ctx_SegDs;
88   ULONG ctx_RegEdi;
89   ULONG ctx_RegEsi;
90   ULONG ctx_RegEax;
91   ULONG ctx_RegEbx;
92   ULONG ctx_RegEcx;
93   ULONG ctx_RegEdx;
94   ULONG ctx_RegEbp;
95   ULONG ctx_RegEip;
96   ULONG ctx_SegCs;
97   ULONG ctx_EFlags;
98   ULONG ctx_RegEsp;
99   ULONG ctx_SegSs;
100 } SIGCONTEXT;
101 /*typedef CONTEXTRECORD *PCONTEXTRECORD;*/
102
103 #endif  /* __EMX__ */
104
105
106 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) \
107  || defined(__OpenBSD__)
108
109 #define EAX_sig(context)     ((context)->sc_eax)
110 #define EBX_sig(context)     ((context)->sc_ebx)
111 #define ECX_sig(context)     ((context)->sc_ecx)
112 #define EDX_sig(context)     ((context)->sc_edx)
113 #define ESI_sig(context)     ((context)->sc_esi)
114 #define EDI_sig(context)     ((context)->sc_edi)
115 #define EBP_sig(context)     ((context)->sc_ebp)
116                             
117 #define CS_sig(context)      ((context)->sc_cs)
118 #define DS_sig(context)      ((context)->sc_ds)
119 #define ES_sig(context)      ((context)->sc_es)
120 #define SS_sig(context)      ((context)->sc_ss)
121                             
122 #ifdef linux
123 /* fs and gs are not supported on *BSD. */
124 #define FS_sig(context)      ((context)->sc_fs)
125 #define GS_sig(context)      ((context)->sc_gs)
126 #define CR2_sig(context)     ((context)->cr2)
127 #define TRAP_sig(context)    ((context)->sc_trapno)
128 #endif
129                             
130 #ifndef __FreeBSD__         
131 #define EFL_sig(context)     ((context)->sc_eflags)
132 #else                       
133 #define EFL_sig(context)     ((context)->sc_efl)
134 #endif                      
135                             
136 #define EIP_sig(context)     (*((unsigned long*)&(context)->sc_eip))
137 #define ESP_sig(context)     (*((unsigned long*)&(context)->sc_esp))
138
139 #endif  /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
140
141 #if defined(__svr4__) || defined(_SCO_DS)
142
143 #ifdef _SCO_DS
144 #define gregs regs
145 #endif
146
147 #define EAX_sig(context)     ((context)->uc_mcontext.gregs[EAX])
148 #define EBX_sig(context)     ((context)->uc_mcontext.gregs[EBX])
149 #define ECX_sig(context)     ((context)->uc_mcontext.gregs[ECX])
150 #define EDX_sig(context)     ((context)->uc_mcontext.gregs[EDX])
151 #define ESI_sig(context)     ((context)->uc_mcontext.gregs[ESI])
152 #define EDI_sig(context)     ((context)->uc_mcontext.gregs[EDI])
153 #define EBP_sig(context)     ((context)->uc_mcontext.gregs[EBP])
154                             
155 #define CS_sig(context)      ((context)->uc_mcontext.gregs[CS])
156 #define DS_sig(context)      ((context)->uc_mcontext.gregs[DS])
157 #define ES_sig(context)      ((context)->uc_mcontext.gregs[ES])
158 #define SS_sig(context)      ((context)->uc_mcontext.gregs[SS])
159                             
160 #define FS_sig(context)      ((context)->uc_mcontext.gregs[FS])
161 #define GS_sig(context)      ((context)->uc_mcontext.gregs[GS])
162
163 #define EFL_sig(context)     ((context)->uc_mcontext.gregs[EFL])
164                             
165 #define EIP_sig(context)     ((context)->uc_mcontext.gregs[EIP])
166 #ifdef R_ESP
167 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[R_ESP])
168 #else
169 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[ESP])
170 #endif
171
172 #endif  /* svr4 || SCO_DS */
173                             
174 #ifdef __EMX__
175
176 #define EAX_sig(context)     ((context)->ctx_RegEax)
177 #define EBX_sig(context)     ((context)->ctx_RegEbx)
178 #define ECX_sig(context)     ((context)->ctx_RegEcx)
179 #define EDX_sig(context)     ((context)->ctx_RegEdx)
180 #define ESI_sig(context)     ((context)->ctx_RegEsi)
181 #define EDI_sig(context)     ((context)->ctx_RegEdi)
182 #define EBP_sig(context)     ((context)->ctx_RegEbp)
183 #define ESP_sig(context)     ((context)->ctx_RegEsp)
184 #define CS_sig(context)      ((context)->ctx_SegCs)
185 #define DS_sig(context)      ((context)->ctx_SegDs)
186 #define ES_sig(context)      ((context)->ctx_SegEs)
187 #define SS_sig(context)      ((context)->ctx_SegSs)
188 #define FS_sig(context)      ((context)->ctx_SegFs)
189 #define GS_sig(context)      ((context)->ctx_SegGs)
190 #define EFL_sig(context)     ((context)->ctx_EFlags)
191 #define EIP_sig(context)     ((context)->ctx_RegEip)
192
193 #endif  /* __EMX__ */
194
195 /* Generic definitions */
196
197 #define AX_sig(context)      (*(WORD*)&EAX_sig(context))
198 #define BX_sig(context)      (*(WORD*)&EBX_sig(context))
199 #define CX_sig(context)      (*(WORD*)&ECX_sig(context))
200 #define DX_sig(context)      (*(WORD*)&EDX_sig(context))
201 #define SI_sig(context)      (*(WORD*)&ESI_sig(context))
202 #define DI_sig(context)      (*(WORD*)&EDI_sig(context))
203 #define BP_sig(context)      (*(WORD*)&EBP_sig(context))
204
205 #define AL_sig(context)      (*(BYTE*)&EAX_sig(context))
206 #define AH_sig(context)      (*((BYTE*)&EAX_sig(context)+1))
207 #define BL_sig(context)      (*(BYTE*)&EBX_sig(context))
208 #define BH_sig(context)      (*((BYTE*)&EBX_sig(context)+1))
209 #define CL_sig(context)      (*(BYTE*)&ECX_sig(context))
210 #define CH_sig(context)      (*((BYTE*)&ECX_sig(context)+1))
211 #define DL_sig(context)      (*(BYTE*)&EDX_sig(context))
212 #define DH_sig(context)      (*((BYTE*)&EDX_sig(context)+1))
213                             
214 #define IP_sig(context)      (*(WORD*)&EIP_sig(context))
215 #define SP_sig(context)      (*(WORD*)&ESP_sig(context))
216                             
217 #define FL_sig(context)      (*(WORD*)&EFL_sig(context))
218
219 #ifdef FS_sig
220 #include "syslevel.h"
221 #define HANDLER_INIT() \
222     do { int fs = IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT)) ?       \
223                   FS_sig(HANDLER_CONTEXT) : SYSLEVEL_Win16CurrentTeb; \
224          if (!fs) fs = SYSLEVEL_EmergencyTeb;                         \
225          SET_FS(fs);                           } while (0)
226 #else
227 #define HANDLER_INIT() /* nothing */
228 #endif
229
230 #else /* __i386__ */
231
232 #define HANDLER_DEF(name) void name (int signal)
233 #define HANDLER_INIT() /* nothing */
234
235 #endif /* __i386__ */
236
237 #endif /* __WINE_SIG_CONTEXT_H */