* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[cacao.git] / src / vm / jit / i386 / linux / md-os.c
1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
29
30 #include "config.h"
31
32 #include <stdint.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/i386/codegen.h"
38 #include "vm/jit/i386/md.h"
39
40 #include "threads/threads-common.h"
41
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45 #include "vm/stringlocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    Signal handler for hardware exceptions.
54
55 *******************************************************************************/
56
57 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
58 {
59         ucontext_t     *_uc;
60         mcontext_t     *_mc;
61         u1             *pv;
62         u1             *sp;
63         u1             *ra;
64         u1             *xpc;
65         u1              opc;
66         u1              mod;
67         u1              rm;
68         s4              d;
69         s4              disp;
70         ptrint          val;
71         s4              type;
72         void           *p;
73         java_object_t  *o;
74
75         _uc = (ucontext_t *) _p;
76         _mc = &_uc->uc_mcontext;
77
78         pv  = NULL;                 /* is resolved during stackframeinfo creation */
79         sp  = (u1 *) _mc->gregs[REG_ESP];
80         xpc = (u1 *) _mc->gregs[REG_EIP];
81         ra  = xpc;                              /* return address is equal to XPC */
82
83         /* get exception-throwing instruction */
84
85         opc = M_ALD_MEM_GET_OPC(xpc);
86         mod = M_ALD_MEM_GET_MOD(xpc);
87         rm  = M_ALD_MEM_GET_RM(xpc);
88
89         /* for values see emit_mov_mem_reg and emit_mem */
90
91         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
92                 /* this was a hardware-exception */
93
94                 d    = M_ALD_MEM_GET_REG(xpc);
95                 disp = M_ALD_MEM_GET_DISP(xpc);
96
97                 /* we use the exception type as load displacement */
98
99                 type = disp;
100
101                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
102                    registers are reversed starting with number 4 for REG_EDI
103                    (see /usr/include/sys/ucontext.h).  We have to convert that
104                    here. */
105
106                 val = _mc->gregs[REG_EAX - d];
107
108                 if (type == EXCEPTION_HARDWARE_COMPILER) {
109                         /* The PV from the compiler stub is equal to the XPC. */
110
111                         pv = xpc;
112
113                         /* We use a framesize of zero here because the call pushed
114                            the return addres onto the stack. */
115
116                         ra = md_stacktrace_get_returnaddress(sp, 0);
117
118                         /* Skip the RA on the stack. */
119
120                         sp = sp + 1 * SIZEOF_VOID_P;
121
122                         /* The XPC is the RA minus 2, because the RA points to the
123                            instruction after the call. */
124
125                         xpc = ra - 2;
126                 }
127         }
128         else {
129                 /* this was a normal NPE */
130
131                 type = EXCEPTION_HARDWARE_NULLPOINTER;
132                 val  = 0;
133         }
134
135         /* Handle the type. */
136
137         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
138
139         /* Set registers. */
140
141         if (type == EXCEPTION_HARDWARE_COMPILER) {
142                 if (p == NULL) {
143                         o = builtin_retrieve_exception();
144
145                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
146
147                         _mc->gregs[REG_EAX] = (uintptr_t) o;
148                         _mc->gregs[REG_ECX] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
149                         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
150                 }
151                 else {
152                         _mc->gregs[REG_EIP] = (uintptr_t) p;
153                 }
154         }
155         else {
156                 _mc->gregs[REG_EAX] = (intptr_t) p;
157                 _mc->gregs[REG_ECX] = (intptr_t) xpc;                /* REG_ITMP2_XPC */
158                 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
159         }
160 }
161
162
163 /* md_signal_handler_sigfpe ****************************************************
164
165    ArithmeticException signal handler for hardware divide by zero
166    check.
167
168 *******************************************************************************/
169
170 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
171 {
172         ucontext_t     *_uc;
173         mcontext_t     *_mc;
174         u1             *pv;
175         u1             *sp;
176         u1             *ra;
177         u1             *xpc;
178         s4              type;
179         ptrint          val;
180         void           *p;
181
182         _uc = (ucontext_t *) _p;
183         _mc = &_uc->uc_mcontext;
184
185         pv  = NULL;                 /* is resolved during stackframeinfo creation */
186         sp  = (u1 *) _mc->gregs[REG_ESP];
187         xpc = (u1 *) _mc->gregs[REG_EIP];
188         ra  = xpc;                          /* return address is equal to xpc     */
189
190         /* this is an ArithmeticException */
191
192         type = EXCEPTION_HARDWARE_ARITHMETIC;
193         val  = 0;
194
195         /* Handle the type. */
196
197         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
198
199         /* set registers */
200
201         _mc->gregs[REG_EAX] = (intptr_t) p;
202         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
203         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
204 }
205
206
207 /* md_signal_handler_sigill ****************************************************
208
209    Signal handler for hardware patcher traps (ud2).
210
211 *******************************************************************************/
212
213 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
214 {
215         ucontext_t        *_uc;
216         mcontext_t        *_mc;
217         u1                *pv;
218         u1                *sp;
219         u1                *ra;
220         u1                *xpc;
221         s4                 type;
222         ptrint             val;
223         void              *p;
224
225         _uc = (ucontext_t *) _p;
226         _mc = &_uc->uc_mcontext;
227
228         pv  = NULL;                 /* is resolved during stackframeinfo creation */
229         sp  = (u1 *) _mc->gregs[REG_ESP];
230         xpc = (u1 *) _mc->gregs[REG_EIP];
231         ra  = xpc;                            /* return address is equal to xpc   */
232
233         /* this is an ArithmeticException */
234
235         type = EXCEPTION_HARDWARE_PATCHER;
236         val  = 0;
237
238         /* generate appropriate exception */
239
240         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
241
242         /* set registers (only if exception object ready) */
243
244         if (p != NULL) {
245                 _mc->gregs[REG_EAX] = (ptrint) p;
246                 _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
247                 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
248         }
249 }
250
251
252 /* md_signal_handler_sigusr1 ***************************************************
253
254    Signal handler for suspending threads.
255
256 *******************************************************************************/
257
258 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
259 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
260 {
261         ucontext_t *_uc;
262         mcontext_t *_mc;
263         u1         *pc;
264         u1         *sp;
265
266         _uc = (ucontext_t *) _p;
267         _mc = &_uc->uc_mcontext;
268
269         /* get the PC and SP for this thread */
270         pc = (u1 *) _mc->gregs[REG_EIP];
271         sp = (u1 *) _mc->gregs[REG_ESP];
272
273         /* now suspend the current thread */
274         threads_suspend_ack(pc, sp);
275 }
276 #endif
277
278
279 /* md_signal_handler_sigusr2 ***************************************************
280
281    Signal handler for profiling sampling.
282
283 *******************************************************************************/
284
285 #if defined(ENABLE_THREADS)
286 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
287 {
288         threadobject *t;
289         ucontext_t   *_uc;
290         mcontext_t   *_mc;
291         u1           *pc;
292
293         t = THREADOBJECT;
294
295         _uc = (ucontext_t *) _p;
296         _mc = &_uc->uc_mcontext;
297
298         pc = (u1 *) _mc->gregs[REG_EIP];
299
300         t->pc = pc;
301 }
302 #endif
303
304
305 /* md_replace_executionstate_read **********************************************
306
307    Read the given context into an executionstate for Replacement.
308
309 *******************************************************************************/
310
311 #if defined(ENABLE_REPLACEMENT)
312 void md_replace_executionstate_read(executionstate_t *es, void *context)
313 {
314         ucontext_t *_uc;
315         mcontext_t *_mc;
316         s4          i;
317
318         _uc = (ucontext_t *) context;
319         _mc = &_uc->uc_mcontext;
320
321         /* read special registers */
322         es->pc = (u1 *) _mc->gregs[REG_EIP];
323         es->sp = (u1 *) _mc->gregs[REG_ESP];
324         es->pv = NULL;                   /* pv must be looked up via AVL tree */
325
326         /* read integer registers */
327         for (i = 0; i < INT_REG_CNT; i++)
328                 es->intregs[i] = _mc->gregs[REG_EAX - i];
329
330         /* read float registers */
331         for (i = 0; i < FLT_REG_CNT; i++)
332                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
333 }
334 #endif
335
336
337 /* md_replace_executionstate_write *********************************************
338
339    Write the given executionstate back to the context for Replacement.
340
341 *******************************************************************************/
342
343 #if defined(ENABLE_REPLACEMENT)
344 void md_replace_executionstate_write(executionstate_t *es, void *context)
345 {
346         ucontext_t *_uc;
347         mcontext_t *_mc;
348         s4          i;
349
350         _uc = (ucontext_t *) context;
351         _mc = &_uc->uc_mcontext;
352
353         /* write integer registers */
354         for (i = 0; i < INT_REG_CNT; i++)
355                 _mc->gregs[REG_EAX - i] = es->intregs[i];
356
357         /* write special registers */
358         _mc->gregs[REG_EIP] = (ptrint) es->pc;
359         _mc->gregs[REG_ESP] = (ptrint) es->sp;
360 }
361 #endif
362
363
364 /* md_critical_section_restart *************************************************
365
366    Search the critical sections tree for a matching section and set
367    the PC to the restart point, if necessary.
368
369 *******************************************************************************/
370
371 #if defined(ENABLE_THREADS)
372 void md_critical_section_restart(ucontext_t *_uc)
373 {
374         mcontext_t *_mc;
375         u1         *pc;
376         u1         *npc;
377
378         _mc = &_uc->uc_mcontext;
379
380         pc = (u1 *) _mc->gregs[REG_EIP];
381
382         npc = critical_find_restart_point(pc);
383
384         if (npc != NULL)
385                 _mc->gregs[REG_EIP] = (ptrint) npc;
386 }
387 #endif
388
389
390 /*
391  * These are local overrides for various environment variables in Emacs.
392  * Please do not remove this and leave it at the end of the file, where
393  * Emacs will automagically detect them.
394  * ---------------------------------------------------------------------
395  * Local variables:
396  * mode: c
397  * indent-tabs-mode: t
398  * c-basic-offset: 4
399  * tab-width: 4
400  * End:
401  */