1 |
/* |
2 |
* interlocked.h - Implementation of interlocked functions. |
3 |
* |
4 |
* Copyright (C) 2004 Southern Storm Software, Pty Ltd. |
5 |
* |
6 |
* Authors: Thong Nguyen (tum@veridicus.com) |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
#include "interrupt.h" |
24 |
|
25 |
#ifdef IL_INTERRUPT_POSIX |
26 |
|
27 |
#include "thr_defs.h" |
28 |
#include "il_thread.h" |
29 |
|
30 |
#ifdef __cplusplus |
31 |
extern "C" { |
32 |
#endif |
33 |
|
34 |
#if defined(linux) || defined(__linux) || defined(__linux__) |
35 |
#define __USE_GNU |
36 |
#endif |
37 |
|
38 |
#if defined(IL_INTERRUPT_HAVE_X86_CONTEXT) |
39 |
#include <sys/ucontext.h> |
40 |
|
41 |
/* older glibc's (<=2.1.2) have EAX instead of REG_EAX */ |
42 |
#if !defined(REG_EAX) && defined(EAX) |
43 |
#define REG_EAX EAX |
44 |
#endif |
45 |
#if !defined(REG_EBX) && defined(EBX) |
46 |
#define REG_EBX EBX |
47 |
#endif |
48 |
#if !defined(REG_ECX) && defined(ECX) |
49 |
#define REG_ECX ECX |
50 |
#endif |
51 |
#if !defined(REG_EDX) && defined(EDX) |
52 |
#define REG_EDX EDX |
53 |
#endif |
54 |
#if !defined(REG_EDI) && defined(EDI) |
55 |
#define REG_EDI EDI |
56 |
#endif |
57 |
#if !defined(REG_ESI) && defined(ESI) |
58 |
#define REG_ESI ESI |
59 |
#endif |
60 |
#if !defined(REG_EBP) && defined(EBP) |
61 |
#define REG_EBP EBP |
62 |
#endif |
63 |
#if !defined(REG_EIP) && defined(EIP) |
64 |
#define REG_EIP EIP |
65 |
#endif |
66 |
#if !defined(REG_ESP) && defined(ESP) |
67 |
#define REG_ESP ESP |
68 |
#endif |
69 |
#endif |
70 |
|
71 |
#if defined(IL_INTERRUPT_SUPPORTS) |
72 |
|
73 |
#if defined(HAVE_SIGACTION) |
74 |
|
75 |
static void __sigaction_handler(int signo, siginfo_t *info, void *ctx) |
76 |
{ |
77 |
ILThread *thread; |
78 |
ILInterruptContext context; |
79 |
#if defined(IL_INTERRUPT_HAVE_X86_CONTEXT) |
80 |
ucontext_t *uc; |
81 |
uc = (ucontext_t *)ctx; |
82 |
#endif |
83 |
|
84 |
thread = ILThreadSelf(); |
85 |
|
86 |
#if defined(IL_INTERRUPT_HAVE_X86_CONTEXT) |
87 |
|
88 |
#if defined(linux) || defined(__linux) || defined(__linux__) |
89 |
/* Integer registers */ |
90 |
context.Eax = uc->uc_mcontext.gregs[REG_EAX]; |
91 |
context.Ebx = uc->uc_mcontext.gregs[REG_EBX]; |
92 |
context.Ecx = uc->uc_mcontext.gregs[REG_ECX]; |
93 |
context.Edx = uc->uc_mcontext.gregs[REG_EDX]; |
94 |
context.Edi = uc->uc_mcontext.gregs[REG_EDI]; |
95 |
context.Esi = uc->uc_mcontext.gregs[REG_ESI]; |
96 |
|
97 |
/* Control registers */ |
98 |
context.Ebp = uc->uc_mcontext.gregs[REG_EBP]; |
99 |
context.Eip = uc->uc_mcontext.gregs[REG_EIP]; |
100 |
context.Esp = uc->uc_mcontext.gregs[REG_ESP]; |
101 |
|
102 |
#elif defined(__FreeBSD__) |
103 |
/* Integer registers */ |
104 |
context.Eax = uc->uc_mcontext.mc_eax; |
105 |
context.Ebx = uc->uc_mcontext.mc_ebx; |
106 |
context.Ecx = uc->uc_mcontext.mc_ecx; |
107 |
context.Edx = uc->uc_mcontext.mc_edx; |
108 |
context.Edi = uc->uc_mcontext.mc_edi; |
109 |
context.Esi = uc->uc_mcontext.mc_esi; |
110 |
|
111 |
/* Control registers */ |
112 |
context.Ebp = uc->uc_mcontext.mc_ebp; |
113 |
context.Eip = uc->uc_mcontext.mc_eip; |
114 |
context.Esp = uc->uc_mcontext.mc_esp; |
115 |
#endif |
116 |
|
117 |
context.instructionAddress = (void *)context.Eip; |
118 |
#else |
119 |
context.instructionAddress = 0; |
120 |
#endif |
121 |
|
122 |
switch (signo) |
123 |
{ |
124 |
#if defined(IL_INTERRUPT_SUPPORTS_ILLEGAL_MEMORY_ACCESS) |
125 |
case SIGSEGV: |
126 |
case SIGBUS: |
127 |
|
128 |
thread = ILThreadSelf(); |
129 |
|
130 |
context.memoryAddress = info->si_addr; |
131 |
context.type = IL_INTERRUPT_TYPE_ILLEGAL_MEMORY_ACCESS; |
132 |
|
133 |
thread->interruptHandler(&context); |
134 |
|
135 |
break; |
136 |
#endif |
137 |
|
138 |
#if defined(IL_INTERRUPT_SUPPORTS_ANY_ARITH) |
139 |
case SIGFPE: |
140 |
|
141 |
switch (info->si_code) |
142 |
{ |
143 |
#if defined(IL_INTERRUPT_SUPPORTS_INT_DIVIDE_BY_ZERO) |
144 |
case FPE_INTDIV: |
145 |
context.type = IL_INTERRUPT_TYPE_INT_DIVIDE_BY_ZERO; |
146 |
context.instructionAddress = info->si_addr; |
147 |
context.memoryAddress = 0; |
148 |
thread->interruptHandler(&context); |
149 |
break; |
150 |
#endif |
151 |
|
152 |
#if defined(IL_INTERRUPT_SUPPORTS_INT_OVERFLOW) |
153 |
case FPE_INTOVF: |
154 |
context.type = IL_INTERRUPT_TYPE_INT_OVERFLOW; |
155 |
context.instructionAddress = info->si_addr; |
156 |
context.memoryAddress = 0; |
157 |
thread->interruptHandler(&context); |
158 |
break; |
159 |
#endif |
160 |
} |
161 |
|
162 |
break; |
163 |
#endif |
164 |
} |
165 |
|
166 |
} |
167 |
|
168 |
#elif defined(HAVE_SIGNAL) |
169 |
|
170 |
static void __signal_handler(int signal) |
171 |
{ |
172 |
ILThread *thread; |
173 |
ILInterruptContext context; |
174 |
|
175 |
thread = ILThreadSelf(); |
176 |
|
177 |
switch (signal) |
178 |
{ |
179 |
#if defined(IL_INTERRUPT_SUPPORTS_ILLEGAL_MEMORY_ACCESS) |
180 |
case SIGSEGV: |
181 |
case SIGBUS: |
182 |
context.memoryAddress = 0 |
183 |
context.instructionAddress = 0 |
184 |
context.type = IL_INTERRUPT_TYPE_ILLEGAL_MEMORY_ACCESS; |
185 |
thread->interruptHandler(&context); |
186 |
break; |
187 |
#endif |
188 |
} |
189 |
} |
190 |
|
191 |
#endif |
192 |
|
193 |
#endif /* IL_INTERRUPT_SUPPORTS_ILLEGAL_MEMORY_ACCESS */ |
194 |
|
195 |
void _ILInterruptInit() |
196 |
{ |
197 |
#if defined(IL_INTERRUPT_SUPPORTS) |
198 |
#if defined(HAVE_SIGACTION) |
199 |
|
200 |
/* Use SIGACTION */ |
201 |
struct sigaction sa; |
202 |
|
203 |
sa.sa_sigaction = __sigaction_handler; |
204 |
sigemptyset(&sa.sa_mask); |
205 |
sa.sa_flags = SA_SIGINFO; |
206 |
|
207 |
/* Registers memory violation handlers */ |
208 |
#ifdef IL_INTERRUPT_SUPPORTS_ILLEGAL_MEMORY_ACCESS |
209 |
sigaction(SIGSEGV, &sa, 0); |
210 |
sigaction(SIGBUS, &sa, 0); |
211 |
#endif |
212 |
|
213 |
/* Registers FPE exception handlers */ |
214 |
#if defined(IL_INTERRUPT_SUPPORTS_ANY_ARITH) |
215 |
sigemptyset(&sa.sa_mask); |
216 |
sa.sa_flags = SA_SIGINFO | SA_NODEFER; |
217 |
sigaction(SIGFPE, &sa, 0); |
218 |
#endif |
219 |
#elif defined(HAVE_SIGNAL) |
220 |
|
221 |
/* Use SIGNAL */ |
222 |
#warning sigaction() not available, using signal() which may be inaccurate |
223 |
|
224 |
/* Register memory violation handlers */ |
225 |
signal(SIGSEGV, __signal_handler); |
226 |
signal(SIGBUS, __signal_handler); |
227 |
#endif |
228 |
#endif |
229 |
} |
230 |
|
231 |
void _ILInterruptDeinit() |
232 |
{ |
233 |
} |
234 |
|
235 |
#ifdef __cplusplus |
236 |
}; |
237 |
#endif |
238 |
|
239 |
#endif /* IL_INTERRUPT_POSIX */ |
240 |
|