* Removed all Id tags.
[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 */
26
27
28 #define _GNU_SOURCE
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/x86_64/codegen.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/threads.h"
43 #endif
44
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/stacktrace.h"
50
51
52 /* md_signal_handler_sigsegv ***************************************************
53
54    Signal handler for hardware exception.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         stackframeinfo  sfi;
61         ucontext_t     *_uc;
62         mcontext_t     *_mc;
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         int             type;
72         intptr_t        val;
73         void           *p;
74
75         _uc = (ucontext_t *) _p;
76         _mc = &_uc->uc_mcontext;
77
78         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
79            different to the ones in <ucontext.h>. */
80
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         else {
155                 /* this was a normal NPE */
156
157                 type = EXCEPTION_HARDWARE_NULLPOINTER;
158                 val  = 0;
159         }
160
161         /* create stackframeinfo */
162
163         stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, xpc);
164
165         /* Handle the type. */
166
167         p = signal_handle(xpc, type, val);
168
169         /* remove stackframeinfo */
170
171         stacktrace_remove_stackframeinfo(&sfi);
172
173         /* set registers */
174
175         _mc->gregs[REG_RAX] = (intptr_t) p;
176         _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
177         _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
178 }
179
180
181 /* md_signal_handler_sigfpe ****************************************************
182
183    ArithmeticException signal handler for hardware divide by zero
184    check.
185
186 *******************************************************************************/
187
188 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
189 {
190         stackframeinfo  sfi;
191         ucontext_t     *_uc;
192         mcontext_t     *_mc;
193         u1             *pv;
194         u1             *sp;
195         u1             *ra;
196         u1             *xpc;
197         int             type;
198         intptr_t        val;
199         void           *p;
200
201         _uc = (ucontext_t *) _p;
202         _mc = &_uc->uc_mcontext;
203
204         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
205            different to the ones in <ucontext.h>. */
206
207         pv  = NULL;
208         sp  = (u1 *) _mc->gregs[REG_RSP];
209         xpc = (u1 *) _mc->gregs[REG_RIP];
210         ra  = xpc;                          /* return address is equal to xpc     */
211
212         /* this is an ArithmeticException */
213
214         type = EXCEPTION_HARDWARE_ARITHMETIC;
215         val  = 0;
216
217         /* create stackframeinfo */
218
219         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
220
221         /* Handle the type. */
222
223         p = signal_handle(xpc, type, val);
224
225         /* remove stackframeinfo */
226
227         stacktrace_remove_stackframeinfo(&sfi);
228
229         /* set registers */
230
231         _mc->gregs[REG_RAX] = (intptr_t) p;
232         _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
233         _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
234 }
235
236
237 /* md_signal_handler_sigusr2 ***************************************************
238
239    Signal handler for profiling sampling.
240
241 *******************************************************************************/
242
243 #if defined(ENABLE_THREADS)
244 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
245 {
246         threadobject *t;
247         ucontext_t   *_uc;
248         mcontext_t   *_mc;
249         u1           *pc;
250
251         t = THREADOBJECT;
252
253         _uc = (ucontext_t *) _p;
254         _mc = &_uc->uc_mcontext;
255
256         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
257            different to the ones in <ucontext.h>. */
258
259         pc = (u1 *) _mc->gregs[REG_RIP];
260
261         t->pc = pc;
262 }
263 #endif
264
265
266 /* md_critical_section_restart *************************************************
267
268    Search the critical sections tree for a matching section and set
269    the PC to the restart point, if necessary.
270
271 *******************************************************************************/
272
273 #if defined(ENABLE_THREADS)
274 void md_critical_section_restart(ucontext_t *_uc)
275 {
276         mcontext_t *_mc;
277         u1         *pc;
278         u1         *npc;
279
280         _mc = &_uc->uc_mcontext;
281
282         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
283            different to the ones in <ucontext.h>. */
284
285         pc = (u1 *) _mc->gregs[REG_RIP];
286
287         npc = critical_find_restart_point(pc);
288
289         if (npc != NULL)
290                 _mc->gregs[REG_RIP] = (ptrint) npc;
291 }
292 #endif
293
294
295 /*
296  * These are local overrides for various environment variables in Emacs.
297  * Please do not remove this and leave it at the end of the file, where
298  * Emacs will automagically detect them.
299  * ---------------------------------------------------------------------
300  * Local variables:
301  * mode: c
302  * indent-tabs-mode: t
303  * c-basic-offset: 4
304  * tab-width: 4
305  * End:
306  * vim:noexpandtab:sw=4:ts=4:
307  */