608add2f775fbda0c9f9d71c5eafe6e5ffa7b3a0
[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 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    $Id: md.c 7249 2007-01-29 19:32:52Z twisti $
26
27 */
28
29
30 #define _GNU_SOURCE
31
32 #include "config.h"
33
34 #include <assert.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <ucontext.h>
38
39 #include "vm/types.h"
40
41 #include "vm/jit/x86_64/codegen.h"
42
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/threads.h"
45 #endif
46
47 #include "vm/exceptions.h"
48 #include "vm/signallocal.h"
49
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55
56    Signal handler for hardware exception.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         stackframeinfo  sfi;
63         ucontext_t     *_uc;
64         mcontext_t     *_mc;
65         u1             *sp;
66         u1             *ra;
67         u1             *xpc;
68         u1              opc;
69         u1              mod;
70         u1              rm;
71         s4              d;
72         s4              disp;
73         int             type;
74         intptr_t        val;
75         void           *p;
76
77         _uc = (ucontext_t *) _p;
78         _mc = &_uc->uc_mcontext;
79
80         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
81            different to the ones in <ucontext.h>. */
82
83         sp  = (u1 *) _mc->gregs[REG_RSP];
84         xpc = (u1 *) _mc->gregs[REG_RIP];
85         ra  = xpc;                          /* return address is equal to xpc     */
86
87 #if 0
88         /* check for StackOverflowException */
89
90         threads_check_stackoverflow(sp);
91 #endif
92
93         /* get exception-throwing instruction */
94
95         opc = M_ALD_MEM_GET_OPC(xpc);
96         mod = M_ALD_MEM_GET_MOD(xpc);
97         rm  = M_ALD_MEM_GET_RM(xpc);
98
99         /* for values see emit_mov_mem_reg and emit_mem */
100
101         if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
102                 /* this was a hardware-exception */
103
104                 d    = M_ALD_MEM_GET_REG(xpc);
105                 disp = M_ALD_MEM_GET_DISP(xpc);
106
107                 /* we use the exception type as load displacement */
108
109                 type = disp;
110
111                 /* XXX FIX ME! */
112
113                 /* ATTENTION: The _mc->gregs layout is even worse than on
114                    i386! See /usr/include/sys/ucontext.h.  We need a
115                    switch-case here... */
116
117                 switch (d) {
118                 case 0:  /* REG_RAX == 13 */
119                         d = REG_RAX;
120                         break;
121                 case 1:  /* REG_RCX == 14 */
122                         d = REG_RCX;
123                         break;
124                 case 2:  /* REG_RDX == 12 */
125                         d = REG_RDX;
126                         break;
127                 case 3:  /* REG_RBX == 11 */
128                         d = REG_RBX;
129                         break;
130                 case 4:  /* REG_RSP == 15 */
131                         d = REG_RSP;
132                         break;
133                 case 5:  /* REG_RBP == 10 */
134                         d = REG_RBP;
135                         break;
136                 case 6:  /* REG_RSI == 9  */
137                         d = REG_RSI;
138                         break;
139                 case 7:  /* REG_RDI == 8  */
140                         d = REG_RDI;
141                         break;
142                 case 8:  /* REG_R8  == 0  */
143                 case 9:  /* REG_R9  == 1  */
144                 case 10: /* REG_R10 == 2  */
145                 case 11: /* REG_R11 == 3  */
146                 case 12: /* REG_R12 == 4  */
147                 case 13: /* REG_R13 == 5  */
148                 case 14: /* REG_R14 == 6  */
149                 case 15: /* REG_R15 == 7  */
150                         d = d - 8;
151                         break;
152                 }
153
154                 val = _mc->gregs[d];
155         }
156         else {
157                 /* this was a normal NPE */
158
159                 type = EXCEPTION_HARDWARE_NULLPOINTER;
160                 val  = 0;
161         }
162
163         /* create stackframeinfo */
164
165         stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, xpc);
166
167         /* Handle the type. */
168
169         p = signal_handle(xpc, type, val);
170
171         /* remove stackframeinfo */
172
173         stacktrace_remove_stackframeinfo(&sfi);
174
175         /* set registers */
176
177         _mc->gregs[REG_RAX] = (intptr_t) p;
178         _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
179         _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
180 }
181
182
183 /* md_signal_handler_sigfpe ****************************************************
184
185    ArithmeticException signal handler for hardware divide by zero
186    check.
187
188 *******************************************************************************/
189
190 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
191 {
192         stackframeinfo  sfi;
193         ucontext_t     *_uc;
194         mcontext_t     *_mc;
195         u1             *pv;
196         u1             *sp;
197         u1             *ra;
198         u1             *xpc;
199         int             type;
200         intptr_t        val;
201         void           *p;
202
203         _uc = (ucontext_t *) _p;
204         _mc = &_uc->uc_mcontext;
205
206         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
207            different to the ones in <ucontext.h>. */
208
209         pv  = NULL;
210         sp  = (u1 *) _mc->gregs[REG_RSP];
211         xpc = (u1 *) _mc->gregs[REG_RIP];
212         ra  = xpc;                          /* return address is equal to xpc     */
213
214         /* this is an ArithmeticException */
215
216         type = EXCEPTION_HARDWARE_ARITHMETIC;
217         val  = 0;
218
219         /* create stackframeinfo */
220
221         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
222
223         /* Handle the type. */
224
225         p = signal_handle(xpc, type, val);
226
227         /* remove stackframeinfo */
228
229         stacktrace_remove_stackframeinfo(&sfi);
230
231         /* set registers */
232
233         _mc->gregs[REG_RAX] = (intptr_t) p;
234         _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
235         _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
236 }
237
238
239 /* md_signal_handler_sigusr2 ***************************************************
240
241    Signal handler for profiling sampling.
242
243 *******************************************************************************/
244
245 #if defined(ENABLE_THREADS)
246 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
247 {
248         threadobject *t;
249         ucontext_t   *_uc;
250         mcontext_t   *_mc;
251         u1           *pc;
252
253         t = THREADOBJECT;
254
255         _uc = (ucontext_t *) _p;
256         _mc = &_uc->uc_mcontext;
257
258         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
259            different to the ones in <ucontext.h>. */
260
261         pc = (u1 *) _mc->gregs[REG_RIP];
262
263         t->pc = pc;
264 }
265 #endif
266
267
268 /* md_critical_section_restart *************************************************
269
270    Search the critical sections tree for a matching section and set
271    the PC to the restart point, if necessary.
272
273 *******************************************************************************/
274
275 #if defined(ENABLE_THREADS)
276 void md_critical_section_restart(ucontext_t *_uc)
277 {
278         mcontext_t *_mc;
279         u1         *pc;
280         u1         *npc;
281
282         _mc = &_uc->uc_mcontext;
283
284         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
285            different to the ones in <ucontext.h>. */
286
287         pc = (u1 *) _mc->gregs[REG_RIP];
288
289         npc = critical_find_restart_point(pc);
290
291         if (npc != NULL)
292                 _mc->gregs[REG_RIP] = (ptrint) npc;
293 }
294 #endif
295
296
297 /*
298  * These are local overrides for various environment variables in Emacs.
299  * Please do not remove this and leave it at the end of the file, where
300  * Emacs will automagically detect them.
301  * ---------------------------------------------------------------------
302  * Local variables:
303  * mode: c
304  * indent-tabs-mode: t
305  * c-basic-offset: 4
306  * tab-width: 4
307  * End:
308  * vim:noexpandtab:sw=4:ts=4:
309  */