Merged branch subtype-trunk into default.
[cacao.git] / src / vm / jit / powerpc / darwin / md-os.c
1 /* src/vm/jit/powerpc/darwin/md-os.c - machine dependent PowerPC Darwin functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 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 <assert.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/powerpc/codegen.h"
36 #include "vm/jit/powerpc/darwin/md-abi.h"
37
38 #include "threads/thread.hpp"
39
40 #include "vm/jit/builtin.hpp"
41 #include "vm/global.h"
42 #include "vm/signallocal.hpp"
43
44 #include "vm/jit/asmpart.h"
45
46
47 /* md_signal_handler_sigsegv ***************************************************
48
49    NullPointerException signal handler for hardware null pointer
50    check.
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         ppc_thread_state_t *_ss;
59         ptrint             *gregs;
60         u1                 *pv;
61         u1                 *sp;
62         u1                 *ra;
63         u1                 *xpc;
64         u4                  mcode;
65         int                 s1;
66         int16_t             disp;
67         int                 d;
68         intptr_t            addr;
69         intptr_t            val;
70         int                 type;
71         void               *p;
72
73         _uc = (ucontext_t *) _p;
74         _mc = _uc->uc_mcontext;
75         _ss = &_mc->ss;
76
77         /* immitate a gregs array */
78
79         gregs = &_ss->r0;
80
81         /* get register values */
82
83         pv  = (u1 *) _ss->r13;
84         sp  = (u1 *) _ss->r1;
85         ra  = (u1 *) _ss->lr;                        /* this is correct for leafs */
86         xpc = (u1 *) _ss->srr0;
87
88         /* get exception-throwing instruction */
89
90         mcode = *((u4 *) xpc);
91
92         s1   = M_INSTR_OP2_IMM_A(mcode);
93         disp = M_INSTR_OP2_IMM_I(mcode);
94         d    = M_INSTR_OP2_IMM_D(mcode);
95
96         val  = gregs[d];
97
98         /* check for special-load */
99
100         if (s1 == REG_ZERO) {
101                 /* we use the exception type as load displacement */
102
103                 type = disp;
104
105                 if (type == EXCEPTION_HARDWARE_COMPILER) {
106                         /* The XPC is the RA minus 4, because the RA points to the
107                            instruction after the call. */
108
109                         xpc = ra - 4;
110                 }
111         }
112         else {
113                 /* This is a normal NPE: addr must be NULL and the NPE-type
114                    define is 0. */
115
116                 addr = gregs[s1];
117                 type = EXCEPTION_HARDWARE_NULLPOINTER;
118
119                 if (addr != 0)
120                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
121         }
122
123         /* Handle the type. */
124
125         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
126
127         /* Set registers. */
128
129         switch (type) {
130         case EXCEPTION_HARDWARE_COMPILER:
131                 if (p != NULL) {
132                         _ss->r13  = (uintptr_t) p;                              /* REG_PV */
133                         _ss->srr0 = (uintptr_t) p;
134                         break;
135                 }
136
137                 /* Get and set the PV from the parent Java method. */
138
139                 pv = md_codegen_get_pv_from_pc(ra);
140
141                 _ss->r13 = (uintptr_t) pv;
142
143                 /* Get the exception object. */
144
145                 p = builtin_retrieve_exception();
146
147                 assert(p != NULL);
148
149                 /* fall-through */
150
151         case EXCEPTION_HARDWARE_PATCHER:
152                 if (p == NULL)
153                         break;
154
155                 /* fall-through */
156                 
157         default:
158                 _ss->r11  = (uintptr_t) p;
159                 _ss->r12  = (uintptr_t) xpc;
160                 _ss->srr0 = (uintptr_t) asm_handle_exception;
161         }
162 }
163
164
165 /* md_signal_handler_sigtrap ***************************************************
166
167    Signal handler for hardware-traps.
168
169 *******************************************************************************/
170
171 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
172 {
173         ucontext_t         *_uc;
174         mcontext_t          _mc;
175         ppc_thread_state_t *_ss;
176         ptrint             *gregs;
177         u1                 *pv;
178         u1                 *sp;
179         u1                 *ra;
180         u1                 *xpc;
181         u4                  mcode;
182         int                 s1;
183         intptr_t            val;
184         int                 type;
185         void               *p;
186
187         _uc = (ucontext_t *) _p;
188         _mc = _uc->uc_mcontext;
189         _ss = &_mc->ss;
190
191         /* immitate a gregs array */
192
193         gregs = &_ss->r0;
194
195         /* get register values */
196
197         pv  = (u1 *) _ss->r13;
198         sp  = (u1 *) _ss->r1;
199         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
200         xpc = (u1 *) _ss->srr0;
201
202         /* get exception-throwing instruction */
203
204         mcode = *((u4 *) xpc);
205
206         s1 = M_OP3_GET_A(mcode);
207
208         /* for now we only handle ArrayIndexOutOfBoundsException */
209
210         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
211         val  = gregs[s1];
212
213         /* Handle the type. */
214
215         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
216
217         /* set registers */
218
219         _ss->r11  = (intptr_t) p;
220         _ss->r12  = (intptr_t) xpc;
221         _ss->srr0 = (intptr_t) asm_handle_exception;
222 }
223
224
225 /* md_signal_handler_sigusr2 ***************************************************
226
227    Signal handler for profiling sampling.
228
229 *******************************************************************************/
230
231 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
232 {
233         threadobject       *t;
234         ucontext_t         *_uc;
235         mcontext_t          _mc;
236         ppc_thread_state_t *_ss;
237         u1                 *pc;
238
239         t = THREADOBJECT;
240
241         _uc = (ucontext_t *) _p;
242         _mc = _uc->uc_mcontext;
243         _ss = &_mc->ss;
244
245         pc = (u1 *) _ss->srr0;
246
247         t->pc = pc;
248 }
249
250
251 /*
252  * These are local overrides for various environment variables in Emacs.
253  * Please do not remove this and leave it at the end of the file, where
254  * Emacs will automagically detect them.
255  * ---------------------------------------------------------------------
256  * Local variables:
257  * mode: c
258  * indent-tabs-mode: t
259  * c-basic-offset: 4
260  * tab-width: 4
261  * End:
262  */