* src/vm/jit/i386/darwin/md-os.c (md_replace_executionstate_read):
[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/executionstate.h"
49 #include "vm/jit/stacktrace.h"
50
51
52 /* md_signal_handler_sigsegv ***************************************************
53
54    Signal handler for hardware exceptions.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         ucontext_t     *_uc;
61         mcontext_t     *_mc;
62         u1             *pv;
63         u1             *sp;
64         u1             *ra;
65         u1             *xpc;
66         u1              opc;
67         u1              mod;
68         u1              rm;
69         s4              d;
70         s4              disp;
71         ptrint          val;
72         s4              type;
73         void           *p;
74         java_object_t  *o;
75
76         _uc = (ucontext_t *) _p;
77         _mc = &_uc->uc_mcontext;
78
79         pv  = NULL;                 /* is resolved during stackframeinfo creation */
80         sp  = (u1 *) _mc->gregs[REG_ESP];
81         xpc = (u1 *) _mc->gregs[REG_EIP];
82         ra  = xpc;                              /* return address is equal to XPC */
83
84         /* get exception-throwing instruction */
85
86         opc = M_ALD_MEM_GET_OPC(xpc);
87         mod = M_ALD_MEM_GET_MOD(xpc);
88         rm  = M_ALD_MEM_GET_RM(xpc);
89
90         /* for values see emit_mov_mem_reg and emit_mem */
91
92         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
93                 /* this was a hardware-exception */
94
95                 d    = M_ALD_MEM_GET_REG(xpc);
96                 disp = M_ALD_MEM_GET_DISP(xpc);
97
98                 /* we use the exception type as load displacement */
99
100                 type = disp;
101
102                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
103                    registers are reversed starting with number 4 for REG_EDI
104                    (see /usr/include/sys/ucontext.h).  We have to convert that
105                    here. */
106
107                 val = _mc->gregs[REG_EAX - d];
108
109                 if (type == EXCEPTION_HARDWARE_COMPILER) {
110                         /* The PV from the compiler stub is equal to the XPC. */
111
112                         pv = xpc;
113
114                         /* We use a framesize of zero here because the call pushed
115                            the return addres onto the stack. */
116
117                         ra = md_stacktrace_get_returnaddress(sp, 0);
118
119                         /* Skip the RA on the stack. */
120
121                         sp = sp + 1 * SIZEOF_VOID_P;
122
123                         /* The XPC is the RA minus 2, because the RA points to the
124                            instruction after the call. */
125
126                         xpc = ra - 2;
127                 }
128         }
129         else {
130                 /* this was a normal NPE */
131
132                 type = EXCEPTION_HARDWARE_NULLPOINTER;
133                 val  = 0;
134         }
135
136         /* Handle the type. */
137
138         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
139
140         /* Set registers. */
141
142         if (type == EXCEPTION_HARDWARE_COMPILER) {
143                 if (p == NULL) {
144                         o = builtin_retrieve_exception();
145
146                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
147
148                         _mc->gregs[REG_EAX] = (uintptr_t) o;
149                         _mc->gregs[REG_ECX] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
150                         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
151                 }
152                 else {
153                         _mc->gregs[REG_EIP] = (uintptr_t) p;
154                 }
155         }
156         else {
157                 _mc->gregs[REG_EAX] = (intptr_t) p;
158                 _mc->gregs[REG_ECX] = (intptr_t) xpc;                /* REG_ITMP2_XPC */
159                 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
160         }
161 }
162
163
164 /* md_signal_handler_sigfpe ****************************************************
165
166    ArithmeticException signal handler for hardware divide by zero
167    check.
168
169 *******************************************************************************/
170
171 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
172 {
173         ucontext_t     *_uc;
174         mcontext_t     *_mc;
175         u1             *pv;
176         u1             *sp;
177         u1             *ra;
178         u1             *xpc;
179         s4              type;
180         ptrint          val;
181         void           *p;
182
183         _uc = (ucontext_t *) _p;
184         _mc = &_uc->uc_mcontext;
185
186         pv  = NULL;                 /* is resolved during stackframeinfo creation */
187         sp  = (u1 *) _mc->gregs[REG_ESP];
188         xpc = (u1 *) _mc->gregs[REG_EIP];
189         ra  = xpc;                          /* return address is equal to xpc     */
190
191         /* this is an ArithmeticException */
192
193         type = EXCEPTION_HARDWARE_ARITHMETIC;
194         val  = 0;
195
196         /* Handle the type. */
197
198         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
199
200         /* set registers */
201
202         _mc->gregs[REG_EAX] = (intptr_t) p;
203         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
204         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
205 }
206
207
208 /* md_signal_handler_sigill ****************************************************
209
210    Signal handler for hardware patcher traps (ud2).
211
212 *******************************************************************************/
213
214 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
215 {
216         ucontext_t        *_uc;
217         mcontext_t        *_mc;
218         u1                *pv;
219         u1                *sp;
220         u1                *ra;
221         u1                *xpc;
222         s4                 type;
223         ptrint             val;
224         void              *p;
225
226         _uc = (ucontext_t *) _p;
227         _mc = &_uc->uc_mcontext;
228
229         pv  = NULL;                 /* is resolved during stackframeinfo creation */
230         sp  = (u1 *) _mc->gregs[REG_ESP];
231         xpc = (u1 *) _mc->gregs[REG_EIP];
232         ra  = xpc;                            /* return address is equal to xpc   */
233
234         /* this is an ArithmeticException */
235
236         type = EXCEPTION_HARDWARE_PATCHER;
237         val  = 0;
238
239         /* generate appropriate exception */
240
241         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
242
243         /* set registers (only if exception object ready) */
244
245         if (p != NULL) {
246                 _mc->gregs[REG_EAX] = (ptrint) p;
247                 _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
248                 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
249         }
250 }
251
252
253 /* md_signal_handler_sigusr1 ***************************************************
254
255    Signal handler for suspending threads.
256
257 *******************************************************************************/
258
259 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
260 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
261 {
262         ucontext_t *_uc;
263         mcontext_t *_mc;
264         u1         *pc;
265         u1         *sp;
266
267         _uc = (ucontext_t *) _p;
268         _mc = &_uc->uc_mcontext;
269
270         /* get the PC and SP for this thread */
271         pc = (u1 *) _mc->gregs[REG_EIP];
272         sp = (u1 *) _mc->gregs[REG_ESP];
273
274         /* now suspend the current thread */
275         threads_suspend_ack(pc, sp);
276 }
277 #endif
278
279
280 /* md_signal_handler_sigusr2 ***************************************************
281
282    Signal handler for profiling sampling.
283
284 *******************************************************************************/
285
286 #if defined(ENABLE_THREADS)
287 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
288 {
289         threadobject *t;
290         ucontext_t   *_uc;
291         mcontext_t   *_mc;
292         u1           *pc;
293
294         t = THREADOBJECT;
295
296         _uc = (ucontext_t *) _p;
297         _mc = &_uc->uc_mcontext;
298
299         pc = (u1 *) _mc->gregs[REG_EIP];
300
301         t->pc = pc;
302 }
303 #endif
304
305
306 /* md_executionstate_read ******************************************************
307
308    Read the given context into an executionstate for Replacement.
309
310 *******************************************************************************/
311
312 void md_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
335
336 /* md_executionstate_write *****************************************************
337
338    Write the given executionstate back to the context for Replacement.
339
340 *******************************************************************************/
341
342 void md_executionstate_write(executionstate_t *es, void *context)
343 {
344         ucontext_t *_uc;
345         mcontext_t *_mc;
346         s4          i;
347
348         _uc = (ucontext_t *) context;
349         _mc = &_uc->uc_mcontext;
350
351         /* write integer registers */
352         for (i = 0; i < INT_REG_CNT; i++)
353                 _mc->gregs[REG_EAX - i] = es->intregs[i];
354
355         /* write special registers */
356         _mc->gregs[REG_EIP] = (ptrint) es->pc;
357         _mc->gregs[REG_ESP] = (ptrint) es->sp;
358 }
359
360
361 /* md_critical_section_restart *************************************************
362
363    Search the critical sections tree for a matching section and set
364    the PC to the restart point, if necessary.
365
366 *******************************************************************************/
367
368 #if defined(ENABLE_THREADS)
369 void md_critical_section_restart(ucontext_t *_uc)
370 {
371         mcontext_t *_mc;
372         u1         *pc;
373         u1         *npc;
374
375         _mc = &_uc->uc_mcontext;
376
377         pc = (u1 *) _mc->gregs[REG_EIP];
378
379         npc = critical_find_restart_point(pc);
380
381         if (npc != NULL)
382                 _mc->gregs[REG_EIP] = (ptrint) npc;
383 }
384 #endif
385
386
387 /*
388  * These are local overrides for various environment variables in Emacs.
389  * Please do not remove this and leave it at the end of the file, where
390  * Emacs will automagically detect them.
391  * ---------------------------------------------------------------------
392  * Local variables:
393  * mode: c
394  * indent-tabs-mode: t
395  * c-basic-offset: 4
396  * tab-width: 4
397  * End:
398  */