* src/vmcore/linker.c (build_display_inner): Use MNEW instead of malloc.
[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.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/global.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    NullPointerException signal handler for hardware null pointer
53    check.
54
55 *******************************************************************************/
56
57 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
58 {
59         ucontext_t         *_uc;
60         mcontext_t          _mc;
61         ppc_thread_state_t *_ss;
62         ptrint             *gregs;
63         u1                 *pv;
64         u1                 *sp;
65         u1                 *ra;
66         u1                 *xpc;
67         u4                  mcode;
68         int                 s1;
69         int16_t             disp;
70         int                 d;
71         intptr_t            addr;
72         intptr_t            val;
73         int                 type;
74         void               *p;
75
76         _uc = (ucontext_t *) _p;
77         _mc = _uc->uc_mcontext;
78         _ss = &_mc->ss;
79
80         /* immitate a gregs array */
81
82         gregs = &_ss->r0;
83
84         /* get register values */
85
86         pv  = (u1 *) _ss->r13;
87         sp  = (u1 *) _ss->r1;
88         ra  = (u1 *) _ss->lr;                        /* this is correct for leafs */
89         xpc = (u1 *) _ss->srr0;
90
91         /* get exception-throwing instruction */
92
93         mcode = *((u4 *) xpc);
94
95         s1   = M_INSTR_OP2_IMM_A(mcode);
96         disp = M_INSTR_OP2_IMM_I(mcode);
97         d    = M_INSTR_OP2_IMM_D(mcode);
98
99         val  = gregs[d];
100
101         /* check for special-load */
102
103         if (s1 == REG_ZERO) {
104                 /* we use the exception type as load displacement */
105
106                 type = disp;
107
108                 if (type == EXCEPTION_HARDWARE_COMPILER) {
109                         /* The XPC is the RA minus 4, because the RA points to the
110                            instruction after the call. */
111
112                         xpc = ra - 4;
113                 }
114         }
115         else {
116                 /* This is a normal NPE: addr must be NULL and the NPE-type
117                    define is 0. */
118
119                 addr = gregs[s1];
120                 type = EXCEPTION_HARDWARE_NULLPOINTER;
121
122                 if (addr != 0)
123                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
124         }
125
126         /* Handle the type. */
127
128         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
129
130         /* Set registers. */
131
132         switch (type) {
133         case EXCEPTION_HARDWARE_COMPILER:
134                 if (p != NULL) {
135                         _ss->r13  = (uintptr_t) p;                              /* REG_PV */
136                         _ss->srr0 = (uintptr_t) p;
137                         break;
138                 }
139
140                 /* Get and set the PV from the parent Java method. */
141
142                 pv = md_codegen_get_pv_from_pc(ra);
143
144                 _ss->r13 = (uintptr_t) pv;
145
146                 /* Get the exception object. */
147
148                 p = builtin_retrieve_exception();
149
150                 assert(p != NULL);
151
152                 /* fall-through */
153
154         case EXCEPTION_HARDWARE_PATCHER:
155                 if (p == NULL)
156                         break;
157
158                 /* fall-through */
159                 
160         default:
161                 _ss->r11  = (uintptr_t) p;
162                 _ss->r12  = (uintptr_t) xpc;
163                 _ss->srr0 = (uintptr_t) asm_handle_exception;
164         }
165 }
166
167
168 /* md_signal_handler_sigtrap ***************************************************
169
170    Signal handler for hardware-traps.
171
172 *******************************************************************************/
173
174 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
175 {
176         ucontext_t         *_uc;
177         mcontext_t          _mc;
178         ppc_thread_state_t *_ss;
179         ptrint             *gregs;
180         u1                 *pv;
181         u1                 *sp;
182         u1                 *ra;
183         u1                 *xpc;
184         u4                  mcode;
185         int                 s1;
186         intptr_t            val;
187         int                 type;
188         void               *p;
189
190         _uc = (ucontext_t *) _p;
191         _mc = _uc->uc_mcontext;
192         _ss = &_mc->ss;
193
194         /* immitate a gregs array */
195
196         gregs = &_ss->r0;
197
198         /* get register values */
199
200         pv  = (u1 *) _ss->r13;
201         sp  = (u1 *) _ss->r1;
202         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
203         xpc = (u1 *) _ss->srr0;
204
205         /* get exception-throwing instruction */
206
207         mcode = *((u4 *) xpc);
208
209         s1 = M_OP3_GET_A(mcode);
210
211         /* for now we only handle ArrayIndexOutOfBoundsException */
212
213         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
214         val  = gregs[s1];
215
216         /* Handle the type. */
217
218         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
219
220         /* set registers */
221
222         _ss->r11  = (intptr_t) p;
223         _ss->r12  = (intptr_t) xpc;
224         _ss->srr0 = (intptr_t) asm_handle_exception;
225 }
226
227
228 /* md_signal_handler_sigusr2 ***************************************************
229
230    Signal handler for profiling sampling.
231
232 *******************************************************************************/
233
234 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
235 {
236         threadobject       *t;
237         ucontext_t         *_uc;
238         mcontext_t          _mc;
239         ppc_thread_state_t *_ss;
240         u1                 *pc;
241
242         t = THREADOBJECT;
243
244         _uc = (ucontext_t *) _p;
245         _mc = _uc->uc_mcontext;
246         _ss = &_mc->ss;
247
248         pc = (u1 *) _ss->srr0;
249
250         t->pc = pc;
251 }
252
253
254 /* md_critical_section_restart *************************************************
255
256    Search the critical sections tree for a matching section and set
257    the PC to the restart point, if necessary.
258
259 *******************************************************************************/
260
261 #if defined(ENABLE_THREADS)
262 void md_critical_section_restart(ucontext_t *_uc)
263 {
264         mcontext_t          _mc;
265         ppc_thread_state_t *_ss;
266         u1                 *pc;
267         u1                 *npc;
268
269         _mc = _uc->uc_mcontext;
270         _ss = &_mc->ss;
271
272         pc = (u1 *) _ss->srr0;
273
274         npc = critical_find_restart_point(pc);
275
276         if (npc != NULL)
277                 _ss->srr0 = (ptrint) npc;
278 }
279 #endif
280
281
282 /*
283  * These are local overrides for various environment variables in Emacs.
284  * Please do not remove this and leave it at the end of the file, where
285  * Emacs will automagically detect them.
286  * ---------------------------------------------------------------------
287  * Local variables:
288  * mode: c
289  * indent-tabs-mode: t
290  * c-basic-offset: 4
291  * tab-width: 4
292  * End:
293  */