* src/vm/exceptions.c (exceptions_new_hardware_exception): Do not create sfi.
[cacao.git] / src / vm / jit / i386 / linux / md-os.c
1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux functions
2
3    Copyright (C) 1996-2005, 2006, 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-os.c 8243 2007-07-31 08:57:54Z michi $
26
27 */
28
29
30 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
31
32 #include "config.h"
33
34 #include <ucontext.h>
35
36 #include "vm/types.h"
37
38 #include "vm/jit/i386/codegen.h"
39
40 #include "threads/threads-common.h"
41
42 #include "vm/exceptions.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/stacktrace.h"
48
49
50 /* md_signal_handler_sigsegv ***************************************************
51
52    Signal handler for hardware exceptions.
53
54 *******************************************************************************/
55
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         stackframeinfo     sfi;
59         ucontext_t        *_uc;
60         mcontext_t        *_mc;
61         u1                *pv;
62         u1                *sp;
63         u1                *ra;
64         u1                *xpc;
65         u1                 opc;
66         u1                 mod;
67         u1                 rm;
68         s4                 d;
69         s4                 disp;
70         ptrint             val;
71         s4                 type;
72         java_objectheader *e;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         pv  = NULL;                 /* is resolved during stackframeinfo creation */
78         sp  = (u1 *) _mc->gregs[REG_ESP];
79         xpc = (u1 *) _mc->gregs[REG_EIP];
80         ra  = xpc;                              /* return address is equal to XPC */
81
82         /* get exception-throwing instruction */
83
84         opc = M_ALD_MEM_GET_OPC(xpc);
85         mod = M_ALD_MEM_GET_MOD(xpc);
86         rm  = M_ALD_MEM_GET_RM(xpc);
87
88         /* for values see emit_mov_mem_reg and emit_mem */
89
90         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
91                 /* this was a hardware-exception */
92
93                 d    = M_ALD_MEM_GET_REG(xpc);
94                 disp = M_ALD_MEM_GET_DISP(xpc);
95
96                 /* we use the exception type as load displacement */
97
98                 type = disp;
99
100                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
101                    registers are reversed starting with number 4 for REG_EDI
102                    (see /usr/include/sys/ucontext.h).  We have to convert that
103                    here. */
104
105                 val = _mc->gregs[REG_EAX - d];
106         }
107         else {
108                 /* this was a normal NPE */
109
110                 type = EXCEPTION_HARDWARE_NULLPOINTER;
111                 val  = 0;
112         }
113
114         /* create stackframeinfo */
115
116         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
117
118         /* generate appropriate exception */
119
120         e = exceptions_new_hardware_exception(xpc, type, val);
121
122         /* remove stackframeinfo */
123
124         stacktrace_remove_stackframeinfo(&sfi);
125
126         /* set registers */
127
128         _mc->gregs[REG_EAX] = (ptrint) e;
129         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
130         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
131 }
132
133
134 /* md_signal_handler_sigfpe ****************************************************
135
136    ArithmeticException signal handler for hardware divide by zero
137    check.
138
139 *******************************************************************************/
140
141 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
142 {
143         stackframeinfo     sfi;
144         ucontext_t        *_uc;
145         mcontext_t        *_mc;
146         u1                *pv;
147         u1                *sp;
148         u1                *ra;
149         u1                *xpc;
150         s4                 type;
151         ptrint             val;
152         java_objectheader *e;
153
154         _uc = (ucontext_t *) _p;
155         _mc = &_uc->uc_mcontext;
156
157         pv  = NULL;                 /* is resolved during stackframeinfo creation */
158         sp  = (u1 *) _mc->gregs[REG_ESP];
159         xpc = (u1 *) _mc->gregs[REG_EIP];
160         ra  = xpc;                          /* return address is equal to xpc     */
161
162         /* this is an ArithmeticException */
163
164         type = EXCEPTION_HARDWARE_ARITHMETIC;
165         val  = 0;
166
167         /* create stackframeinfo */
168
169         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
170
171         /* generate appropriate exception */
172
173         e = exceptions_new_hardware_exception(xpc, type, val);
174
175         /* remove stackframeinfo */
176
177         stacktrace_remove_stackframeinfo(&sfi);
178
179         _mc->gregs[REG_EAX] = (ptrint) e;
180         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
181         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
182 }
183
184
185 /* md_signal_handler_sigusr2 ***************************************************
186
187    Signal handler for profiling sampling.
188
189 *******************************************************************************/
190
191 #if defined(ENABLE_THREADS)
192 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
193 {
194         threadobject *t;
195         ucontext_t   *_uc;
196         mcontext_t   *_mc;
197         u1           *pc;
198
199         t = THREADOBJECT;
200
201         _uc = (ucontext_t *) _p;
202         _mc = &_uc->uc_mcontext;
203
204         pc = (u1 *) _mc->gregs[REG_EIP];
205
206         t->pc = pc;
207 }
208 #endif
209
210
211 /* md_critical_section_restart *************************************************
212
213    Search the critical sections tree for a matching section and set
214    the PC to the restart point, if necessary.
215
216 *******************************************************************************/
217
218 #if defined(ENABLE_THREADS)
219 void md_critical_section_restart(ucontext_t *_uc)
220 {
221         mcontext_t *_mc;
222         u1         *pc;
223         u1         *npc;
224
225         _mc = &_uc->uc_mcontext;
226
227         pc = (u1 *) _mc->gregs[REG_EIP];
228
229         npc = critical_find_restart_point(pc);
230
231         if (npc != NULL)
232                 _mc->gregs[REG_EIP] = (ptrint) npc;
233 }
234 #endif
235
236
237 /*
238  * These are local overrides for various environment variables in Emacs.
239  * Please do not remove this and leave it at the end of the file, where
240  * Emacs will automagically detect them.
241  * ---------------------------------------------------------------------
242  * Local variables:
243  * mode: c
244  * indent-tabs-mode: t
245  * c-basic-offset: 4
246  * tab-width: 4
247  * End:
248  */