* src/vm/jit/stacktrace.c: Moved to .cpp.
[cacao.git] / src / vm / jit / i386 / solaris / md-os.c
1 /* src/vm/jit/i386/solaris/md-os.c - machine dependent i386 Solaris functions
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <ucontext.h>
30
31 #include "vm/types.h"
32
33 #include "vm/jit/i386/codegen.h"
34 #include "vm/jit/i386/md.h"
35
36 #include "threads/thread.h"
37
38 #include "vm/builtin.h"
39 #include "vm/signallocal.h"
40 #include "vm/stringlocal.h"
41
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/executionstate.h"
44 #include "vm/jit/stacktrace.hpp"
45 #include "vm/jit/trap.h"
46
47
48 /* md_signal_handler_sigsegv ***************************************************
49
50    Signal handler for hardware exceptions.
51
52 *******************************************************************************/
53
54 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
55 {
56         ucontext_t     *_uc;
57         mcontext_t     *_mc;
58         u1             *pv;
59         u1             *sp;
60         u1             *ra;
61         u1             *xpc;
62         u1              opc;
63         u1              mod;
64         u1              rm;
65         s4              d;
66         s4              disp;
67         ptrint          val;
68         s4              type;
69         void           *p;
70         java_object_t  *o;
71
72         _uc = (ucontext_t *) _p;
73         _mc = &_uc->uc_mcontext;
74
75         pv  = NULL;                 /* is resolved during stackframeinfo creation */
76         sp  = (u1 *) _mc->gregs[ESP];
77         xpc = (u1 *) _mc->gregs[EIP];
78         ra  = xpc;                              /* return address is equal to XPC */
79
80         /* get exception-throwing instruction */
81
82         opc = M_ALD_MEM_GET_OPC(xpc);
83         mod = M_ALD_MEM_GET_MOD(xpc);
84         rm  = M_ALD_MEM_GET_RM(xpc);
85
86         /* for values see emit_mov_mem_reg and emit_mem */
87
88         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
89                 /* this was a hardware-exception */
90
91                 d    = M_ALD_MEM_GET_REG(xpc);
92                 disp = M_ALD_MEM_GET_DISP(xpc);
93
94                 /* we use the exception type as load displacement */
95
96                 type = disp;
97
98                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
99                    registers are reversed starting with number 4 for REG_EDI
100                    (see /usr/include/sys/ucontext.h).  We have to convert that
101                    here. */
102
103                 val = _mc->gregs[EAX - d];
104
105                 if (type == TRAP_COMPILER) {
106                         /* The PV from the compiler stub is equal to the XPC. */
107
108                         pv = xpc;
109
110                         /* We use a framesize of zero here because the call pushed
111                            the return addres onto the stack. */
112
113                         ra = md_stacktrace_get_returnaddress(sp, 0);
114
115                         /* Skip the RA on the stack. */
116
117                         sp = sp + 1 * SIZEOF_VOID_P;
118
119                         /* The XPC is the RA minus 2, because the RA points to the
120                            instruction after the call. */
121
122                         xpc = ra - 2;
123                 }
124         }
125         else {
126                 /* this was a normal NPE */
127
128                 type = TRAP_NullPointerException;
129                 val  = 0;
130         }
131
132         /* Handle the trap. */
133
134         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
135
136         /* Set registers. */
137
138         if (type == TRAP_COMPILER) {
139                 if (p == NULL) {
140                         o = builtin_retrieve_exception();
141
142                         _mc->gregs[ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
143
144                         _mc->gregs[EAX] = (uintptr_t) o;
145                         _mc->gregs[ECX] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
146                         _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
147                 }
148                 else {
149                         _mc->gregs[EIP] = (uintptr_t) p;
150                 }
151         }
152         else {
153                 _mc->gregs[EAX] = (uintptr_t) p;
154                 _mc->gregs[ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
155                 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
156         }
157 }
158
159
160 /* md_signal_handler_sigfpe ****************************************************
161
162    ArithmeticException signal handler for hardware divide by zero
163    check.
164
165 *******************************************************************************/
166
167 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
168 {
169         ucontext_t *_uc;
170         mcontext_t *_mc;
171         u1         *pv;
172         u1         *sp;
173         u1         *ra;
174         u1         *xpc;
175         s4          type;
176         ptrint      val;
177         void       *p;
178
179         _uc = (ucontext_t *) _p;
180         _mc = &_uc->uc_mcontext;
181
182         pv  = NULL;                 /* is resolved during stackframeinfo creation */
183         sp  = (u1 *) _mc->gregs[ESP];
184         xpc = (u1 *) _mc->gregs[EIP];
185         ra  = xpc;                          /* return address is equal to xpc     */
186
187         /* This is an ArithmeticException. */
188
189         type = TRAP_ArithmeticException;
190         val  = 0;
191
192         /* Handle the trap. */
193
194         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
195
196         /* Set registers. */
197
198         _mc->gregs[EAX] = (uintptr_t) p;
199         _mc->gregs[ECX] = (uintptr_t) xpc;                   /* REG_ITMP2_XPC */
200         _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
201 }
202
203
204 /* md_signal_handler_sigill ****************************************************
205
206    Signal handler for hardware patcher traps (ud2).
207
208 *******************************************************************************/
209
210 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
211 {
212         ucontext_t *_uc;
213         mcontext_t *_mc;
214         u1         *pv;
215         u1         *sp;
216         u1         *ra;
217         u1         *xpc;
218         s4          type;
219         ptrint      val;
220         void       *p;
221
222         _uc = (ucontext_t *) _p;
223         _mc = &_uc->uc_mcontext;
224
225         pv  = NULL;                 /* is resolved during stackframeinfo creation */
226         sp  = (u1 *) _mc->gregs[ESP];
227         xpc = (u1 *) _mc->gregs[EIP];
228         ra  = xpc;                            /* return address is equal to xpc   */
229
230         type = TRAP_PATCHER;
231         val  = 0;
232
233         /* Handle the trap. */
234
235         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
236
237         /* Set registers. */
238
239         if (p != NULL) {
240                 _mc->gregs[EAX] = (uintptr_t) p;
241                 _mc->gregs[ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
242                 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
243         }
244 }
245
246
247 /* md_signal_handler_sigusr1 ***************************************************
248
249    Signal handler for suspending threads.
250
251 *******************************************************************************/
252
253 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
254 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
255 {
256         ucontext_t *_uc;
257         mcontext_t *_mc;
258         u1         *pc;
259         u1         *sp;
260
261         _uc = (ucontext_t *) _p;
262         _mc = &_uc->uc_mcontext;
263
264         /* get the PC and SP for this thread */
265         pc = (u1 *) _mc->gregs[EIP];
266         sp = (u1 *) _mc->gregs[ESP];
267
268         /* now suspend the current thread */
269         threads_suspend_ack(pc, sp);
270 }
271 #endif
272
273
274 /* md_signal_handler_sigusr2 ***************************************************
275
276    Signal handler for profiling sampling.
277
278 *******************************************************************************/
279
280 #if defined(ENABLE_THREADS)
281 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
282 {
283         threadobject *t;
284         ucontext_t   *_uc;
285         mcontext_t   *_mc;
286         u1           *pc;
287
288         t = THREADOBJECT;
289
290         _uc = (ucontext_t *) _p;
291         _mc = &_uc->uc_mcontext;
292
293         pc = (u1 *) _mc->gregs[EIP];
294
295         t->pc = pc;
296 }
297 #endif
298
299
300 /* md_executionstate_read ******************************************************
301
302    Read the given context into an executionstate for Replacement.
303
304 *******************************************************************************/
305
306 void md_executionstate_read(executionstate_t *es, void *context)
307 {
308         ucontext_t *_uc;
309         mcontext_t *_mc;
310         s4          i;
311
312         _uc = (ucontext_t *) context;
313         _mc = &_uc->uc_mcontext;
314
315         /* read special registers */
316         es->pc = (u1 *) _mc->gregs[EIP];
317         es->sp = (u1 *) _mc->gregs[ESP];
318         es->pv = NULL;                   /* pv must be looked up via AVL tree */
319
320         /* read integer registers */
321         for (i = 0; i < INT_REG_CNT; i++)
322                 es->intregs[i] = _mc->gregs[EAX - i];
323
324         /* read float registers */
325         for (i = 0; i < FLT_REG_CNT; i++)
326                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
327 }
328
329
330 /* md_executionstate_write *****************************************************
331
332    Write the given executionstate back to the context for Replacement.
333
334 *******************************************************************************/
335
336 void md_executionstate_write(executionstate_t *es, void *context)
337 {
338         ucontext_t *_uc;
339         mcontext_t *_mc;
340         s4          i;
341
342         _uc = (ucontext_t *) context;
343         _mc = &_uc->uc_mcontext;
344
345         /* write integer registers */
346         for (i = 0; i < INT_REG_CNT; i++)
347                 _mc->gregs[EAX - i] = es->intregs[i];
348
349         /* write special registers */
350         _mc->gregs[EIP] = (ptrint) es->pc;
351         _mc->gregs[ESP] = (ptrint) es->sp;
352 }
353
354
355 /*
356  * These are local overrides for various environment variables in Emacs.
357  * Please do not remove this and leave it at the end of the file, where
358  * Emacs will automagically detect them.
359  * ---------------------------------------------------------------------
360  * Local variables:
361  * mode: c
362  * indent-tabs-mode: t
363  * c-basic-offset: 4
364  * tab-width: 4
365  * End:
366  */