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