* src/vm/jit/trap.c (trap_handle) [__X86_64__]: Use executionstate for
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    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., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #define _GNU_SOURCE
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/x86_64/codegen.h"
38 #include "vm/jit/x86_64/md.h"
39
40 #include "threads/thread.hpp"
41
42 #include "vm/jit/builtin.hpp"
43 #include "vm/signallocal.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47 #include "vm/jit/trap.h"
48 #include "vm/jit/stacktrace.hpp"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    Signal handler for hardware exception.
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         void           *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         int             type;
71         intptr_t        val;
72         void           *p;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
78            different to the ones in <ucontext.h>. */
79
80         pv  = NULL;                 /* is resolved during stackframeinfo creation */
81         sp  = (u1 *) _mc->gregs[REG_RSP];
82         xpc = (u1 *) _mc->gregs[REG_RIP];
83         ra  = xpc;                              /* return address is equal to XPC */
84
85 #if 0
86         /* check for StackOverflowException */
87
88         threads_check_stackoverflow(sp);
89 #endif
90
91         /* get exception-throwing instruction */
92
93         opc = M_ALD_MEM_GET_OPC(xpc);
94         mod = M_ALD_MEM_GET_MOD(xpc);
95         rm  = M_ALD_MEM_GET_RM(xpc);
96
97         /* for values see emit_mov_mem_reg and emit_mem */
98
99         if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
100                 /* this was a hardware-exception */
101
102                 d    = M_ALD_MEM_GET_REG(xpc);
103                 disp = M_ALD_MEM_GET_DISP(xpc);
104
105                 /* we use the exception type as load displacement */
106
107                 type = disp;
108
109                 /* XXX FIX ME! */
110
111                 /* ATTENTION: The _mc->gregs layout is even worse than on
112                    i386! See /usr/include/sys/ucontext.h.  We need a
113                    switch-case here... */
114
115                 switch (d) {
116                 case 0:  /* REG_RAX == 13 */
117                         d = REG_RAX;
118                         break;
119                 case 1:  /* REG_RCX == 14 */
120                         d = REG_RCX;
121                         break;
122                 case 2:  /* REG_RDX == 12 */
123                         d = REG_RDX;
124                         break;
125                 case 3:  /* REG_RBX == 11 */
126                         d = REG_RBX;
127                         break;
128                 case 4:  /* REG_RSP == 15 */
129                         d = REG_RSP;
130                         break;
131                 case 5:  /* REG_RBP == 10 */
132                         d = REG_RBP;
133                         break;
134                 case 6:  /* REG_RSI == 9  */
135                         d = REG_RSI;
136                         break;
137                 case 7:  /* REG_RDI == 8  */
138                         d = REG_RDI;
139                         break;
140                 case 8:  /* REG_R8  == 0  */
141                 case 9:  /* REG_R9  == 1  */
142                 case 10: /* REG_R10 == 2  */
143                 case 11: /* REG_R11 == 3  */
144                 case 12: /* REG_R12 == 4  */
145                 case 13: /* REG_R13 == 5  */
146                 case 14: /* REG_R14 == 6  */
147                 case 15: /* REG_R15 == 7  */
148                         d = d - 8;
149                         break;
150                 }
151
152                 val = _mc->gregs[d];
153
154                 if (type == TRAP_COMPILER) {
155                         /* The PV from the compiler stub is equal to the XPC. */
156
157                         pv = xpc;
158
159                         /* We use a framesize of zero here because the call pushed
160                            the return addres onto the stack. */
161
162                         ra = md_stacktrace_get_returnaddress(sp, 0);
163
164                         /* Skip the RA on the stack. */
165
166                         sp = sp + 1 * SIZEOF_VOID_P;
167
168                         /* The XPC is the RA minus 1, because the RA points to the
169                            instruction after the call. */
170
171                         xpc = ra - 3;
172                 }
173         }
174         else {
175                 /* this was a normal NPE */
176
177                 type = TRAP_NullPointerException;
178                 val  = 0;
179         }
180
181         /* Handle the trap. */
182
183         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
184
185         /* Set registers. */
186
187         if (type == TRAP_COMPILER) {
188                 if (p == NULL) {
189                         _mc->gregs[REG_RSP] = (uintptr_t) sp;    /* Remove RA from stack. */
190                 }
191         }
192 }
193
194
195 /* md_signal_handler_sigfpe ****************************************************
196
197    ArithmeticException signal handler for hardware divide by zero
198    check.
199
200 *******************************************************************************/
201
202 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
203 {
204         ucontext_t     *_uc;
205         mcontext_t     *_mc;
206         u1             *pv;
207         u1             *sp;
208         u1             *ra;
209         u1             *xpc;
210         int             type;
211         intptr_t        val;
212
213         _uc = (ucontext_t *) _p;
214         _mc = &_uc->uc_mcontext;
215
216         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
217            different to the ones in <ucontext.h>. */
218
219         pv  = NULL;
220         sp  = (u1 *) _mc->gregs[REG_RSP];
221         xpc = (u1 *) _mc->gregs[REG_RIP];
222         ra  = xpc;                          /* return address is equal to xpc     */
223
224         /* This is an ArithmeticException. */
225
226         type = TRAP_ArithmeticException;
227         val  = 0;
228
229         /* Handle the trap. */
230
231         trap_handle(type, val, pv, sp, ra, xpc, _p);
232 }
233
234
235 /* md_signal_handler_sigill ****************************************************
236
237    Signal handler for patchers.
238
239 *******************************************************************************/
240
241 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
242 {
243         ucontext_t     *_uc;
244         mcontext_t     *_mc;
245         u1             *pv;
246         u1             *sp;
247         u1             *ra;
248         u1             *xpc;
249         int             type;
250         intptr_t        val;
251
252         _uc = (ucontext_t *) _p;
253         _mc = &_uc->uc_mcontext;
254
255         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
256            different to the ones in <ucontext.h>. */
257
258         pv  = NULL;
259         sp  = (u1 *) _mc->gregs[REG_RSP];
260         xpc = (u1 *) _mc->gregs[REG_RIP];
261         ra  = xpc;                          /* return address is equal to xpc     */
262
263         /* This is a patcher. */
264
265         type = TRAP_PATCHER;
266         val  = 0;
267
268         /* Handle the trap. */
269
270         trap_handle(type, val, pv, sp, ra, xpc, _p);
271 }
272
273
274 /* md_signal_handler_sigusr1 ***************************************************
275
276    Signal handler for suspending threads.
277
278 *******************************************************************************/
279
280 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
281 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
282 {
283         ucontext_t *_uc;
284         mcontext_t *_mc;
285         u1         *pc;
286         u1         *sp;
287
288         _uc = (ucontext_t *) _p;
289         _mc = &_uc->uc_mcontext;
290
291         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
292            different to the ones in <ucontext.h>. */
293
294         /* get the PC and SP for this thread */
295         pc = (u1 *) _mc->gregs[REG_RIP];
296         sp = (u1 *) _mc->gregs[REG_RSP];
297
298         /* now suspend the current thread */
299         threads_suspend_ack(pc, sp);
300 }
301 #endif
302
303
304 /* md_signal_handler_sigusr2 ***************************************************
305
306    Signal handler for profiling sampling.
307
308 *******************************************************************************/
309
310 #if defined(ENABLE_THREADS)
311 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
312 {
313         threadobject *t;
314         ucontext_t   *_uc;
315         mcontext_t   *_mc;
316         u1           *pc;
317
318         t = THREADOBJECT;
319
320         _uc = (ucontext_t *) _p;
321         _mc = &_uc->uc_mcontext;
322
323         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
324            different to the ones in <ucontext.h>. */
325
326         pc = (u1 *) _mc->gregs[REG_RIP];
327
328         t->pc = pc;
329 }
330 #endif
331
332
333 /* md_executionstate_read ******************************************************
334
335    Read the given context into an executionstate.
336
337 *******************************************************************************/
338
339 void md_executionstate_read(executionstate_t *es, void *context)
340 {
341         ucontext_t *_uc;
342         mcontext_t *_mc;
343         s4          i;
344         s4          d;
345
346         _uc = (ucontext_t *) context;
347         _mc = &_uc->uc_mcontext;
348
349         /* read special registers */
350         es->pc = (u1 *) _mc->gregs[REG_RIP];
351         es->sp = (u1 *) _mc->gregs[REG_RSP];
352         es->pv = NULL;
353
354         /* read integer registers */
355         for (i = 0; i < INT_REG_CNT; i++) {
356                 /* XXX FIX ME! */
357
358                 switch (i) {
359                 case 0:  /* REG_RAX == 13 */
360                         d = REG_RAX;
361                         break;
362                 case 1:  /* REG_RCX == 14 */
363                         d = REG_RCX;
364                         break;
365                 case 2:  /* REG_RDX == 12 */
366                         d = REG_RDX;
367                         break;
368                 case 3:  /* REG_RBX == 11 */
369                         d = REG_RBX;
370                         break;
371                 case 4:  /* REG_RSP == 15 */
372                         d = REG_RSP;
373                         break;
374                 case 5:  /* REG_RBP == 10 */
375                         d = REG_RBP;
376                         break;
377                 case 6:  /* REG_RSI == 9  */
378                         d = REG_RSI;
379                         break;
380                 case 7:  /* REG_RDI == 8  */
381                         d = REG_RDI;
382                         break;
383                 case 8:  /* REG_R8  == 0  */
384                 case 9:  /* REG_R9  == 1  */
385                 case 10: /* REG_R10 == 2  */
386                 case 11: /* REG_R11 == 3  */
387                 case 12: /* REG_R12 == 4  */
388                 case 13: /* REG_R13 == 5  */
389                 case 14: /* REG_R14 == 6  */
390                 case 15: /* REG_R15 == 7  */
391                         d = i - 8;
392                         break;
393                 }
394
395                 es->intregs[i] = _mc->gregs[d];
396         }
397
398         /* read float registers */
399         for (i = 0; i < FLT_REG_CNT; i++)
400                 es->fltregs[i] = 0xdeadbeefdeadbeefL;
401 }
402
403
404 /* md_executionstate_write *****************************************************
405
406    Write the given executionstate back to the context.
407
408 *******************************************************************************/
409
410 void md_executionstate_write(executionstate_t *es, void *context)
411 {
412         ucontext_t *_uc;
413         mcontext_t *_mc;
414         s4          i;
415         s4          d;
416
417         _uc = (ucontext_t *) context;
418         _mc = &_uc->uc_mcontext;
419
420         /* write integer registers */
421         for (i = 0; i < INT_REG_CNT; i++) {
422                 /* XXX FIX ME! */
423
424                 switch (i) {
425                 case 0:  /* REG_RAX == 13 */
426                         d = REG_RAX;
427                         break;
428                 case 1:  /* REG_RCX == 14 */
429                         d = REG_RCX;
430                         break;
431                 case 2:  /* REG_RDX == 12 */
432                         d = REG_RDX;
433                         break;
434                 case 3:  /* REG_RBX == 11 */
435                         d = REG_RBX;
436                         break;
437                 case 4:  /* REG_RSP == 15 */
438                         d = REG_RSP;
439                         break;
440                 case 5:  /* REG_RBP == 10 */
441                         d = REG_RBP;
442                         break;
443                 case 6:  /* REG_RSI == 9  */
444                         d = REG_RSI;
445                         break;
446                 case 7:  /* REG_RDI == 8  */
447                         d = REG_RDI;
448                         break;
449                 case 8:  /* REG_R8  == 0  */
450                 case 9:  /* REG_R9  == 1  */
451                 case 10: /* REG_R10 == 2  */
452                 case 11: /* REG_R11 == 3  */
453                 case 12: /* REG_R12 == 4  */
454                 case 13: /* REG_R13 == 5  */
455                 case 14: /* REG_R14 == 6  */
456                 case 15: /* REG_R15 == 7  */
457                         d = i - 8;
458                         break;
459                 }
460
461                 _mc->gregs[d] = es->intregs[i];
462         }
463
464         /* write special registers */
465         _mc->gregs[REG_RIP] = (ptrint) es->pc;
466         _mc->gregs[REG_RSP] = (ptrint) es->sp;
467 }
468
469
470 /*
471  * These are local overrides for various environment variables in Emacs.
472  * Please do not remove this and leave it at the end of the file, where
473  * Emacs will automagically detect them.
474  * ---------------------------------------------------------------------
475  * Local variables:
476  * mode: c
477  * indent-tabs-mode: t
478  * c-basic-offset: 4
479  * tab-width: 4
480  * End:
481  * vim:noexpandtab:sw=4:ts=4:
482  */