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