Merged revisions 7797-7917 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         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         s4                 type;
72         ptrint             val;
73         java_objectheader *o;
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         }
159
160         /* generate appropriate exception */
161
162         o = exceptions_new_hardware_exception(NULL, sp, ra, xpc, type, val);
163
164         /* set registers */
165
166         _mc->gregs[REG_RAX] = (ptrint) o;
167         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
168         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
169 }
170
171
172 /* md_signal_handler_sigfpe ****************************************************
173
174    ArithmeticException signal handler for hardware divide by zero
175    check.
176
177 *******************************************************************************/
178
179 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
180 {
181         ucontext_t        *_uc;
182         mcontext_t        *_mc;
183         u1                *pv;
184         u1                *sp;
185         u1                *ra;
186         u1                *xpc;
187         s4                 type;
188         ptrint             val;
189         java_objectheader *o;
190
191         _uc = (ucontext_t *) _p;
192         _mc = &_uc->uc_mcontext;
193
194         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
195            different to the ones in <ucontext.h>. */
196
197         pv  = NULL;
198         sp  = (u1 *) _mc->gregs[REG_RSP];
199         xpc = (u1 *) _mc->gregs[REG_RIP];
200         ra  = xpc;                          /* return address is equal to xpc     */
201
202         /* this is an ArithmeticException */
203
204         type = EXCEPTION_HARDWARE_ARITHMETIC;
205         val  = 0;
206
207         /* generate appropriate exception */
208
209         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
210
211         /* set registers */
212
213         _mc->gregs[REG_RAX] = (ptrint) o;
214         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
215         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
216 }
217
218
219 /* md_signal_handler_sigusr2 ***************************************************
220
221    Signal handler for profiling sampling.
222
223 *******************************************************************************/
224
225 #if defined(ENABLE_THREADS)
226 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
227 {
228         threadobject *t;
229         ucontext_t   *_uc;
230         mcontext_t   *_mc;
231         u1           *pc;
232
233         t = THREADOBJECT;
234
235         _uc = (ucontext_t *) _p;
236         _mc = &_uc->uc_mcontext;
237
238         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
239            different to the ones in <ucontext.h>. */
240
241         pc = (u1 *) _mc->gregs[REG_RIP];
242
243         t->pc = pc;
244 }
245 #endif
246
247
248 /* md_critical_section_restart *************************************************
249
250    Search the critical sections tree for a matching section and set
251    the PC to the restart point, if necessary.
252
253 *******************************************************************************/
254
255 #if defined(ENABLE_THREADS)
256 void md_critical_section_restart(ucontext_t *_uc)
257 {
258         mcontext_t *_mc;
259         u1         *pc;
260         void       *npc;
261
262         _mc = &_uc->uc_mcontext;
263
264         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
265            different to the ones in <ucontext.h>. */
266
267         pc = (u1 *) _mc->gregs[REG_RIP];
268
269         npc = critical_find_restart_point(pc);
270
271         if (npc != NULL) {
272                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
273                 _mc->gregs[REG_RIP] = (ptrint) npc;
274         }
275 }
276 #endif
277
278
279 /*
280  * These are local overrides for various environment variables in Emacs.
281  * Please do not remove this and leave it at the end of the file, where
282  * Emacs will automagically detect them.
283  * ---------------------------------------------------------------------
284  * Local variables:
285  * mode: c
286  * indent-tabs-mode: t
287  * c-basic-offset: 4
288  * tab-width: 4
289  * End:
290  * vim:noexpandtab:sw=4:ts=4:
291  */