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