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