* src/vm/jit/powerpc/emit.c (emit_patcher_traps): Formerly known
[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 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 8216 2007-07-19 13:51:21Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <signal.h>
34 #include <ucontext.h>
35
36 #include "vm/types.h"
37
38 #include "vm/jit/powerpc/codegen.h"
39 #include "vm/jit/powerpc/darwin/md-abi.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/threads.h"
43 #endif
44
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/signallocal.h"
48 #include "vm/stringlocal.h"
49
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55
56    NullPointerException signal handler for hardware null pointer
57    check.
58
59 *******************************************************************************/
60
61 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
62 {
63         stackframeinfo      sfi;
64         ucontext_t         *_uc;
65         mcontext_t          _mc;
66         ppc_thread_state_t *_ss;
67         ptrint             *gregs;
68         u1                 *pv;
69         u1                 *sp;
70         u1                 *ra;
71         u1                 *xpc;
72         u4                  mcode;
73         s4                  s1;
74         s4                  disp;
75         s4                  d;
76         ptrint             addr;
77         ptrint             val;
78         s4                 type;
79         java_objectheader *o;
80
81         _uc = (ucontext_t *) _p;
82         _mc = _uc->uc_mcontext;
83         _ss = &_mc->ss;
84
85         /* immitate a gregs array */
86
87         gregs = &_ss->r0;
88
89         /* get register values */
90
91         pv  = (u1 *) _ss->r13;
92         sp  = (u1 *) _ss->r1;
93         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
94         xpc = (u1 *) _ss->srr0;
95
96         /* get exception-throwing instruction */
97
98         mcode = *((u4 *) xpc);
99
100         s1   = M_INSTR_OP2_IMM_A(mcode);
101         disp = M_INSTR_OP2_IMM_I(mcode);
102         d    = M_INSTR_OP2_IMM_D(mcode);
103
104         val  = gregs[d];
105
106         /* check for special-load */
107
108         if (s1 == REG_ZERO) {
109                 /* we use the exception type as load displacement */
110
111                 type = disp;
112         }
113         else {
114                 /* This is a normal NPE: addr must be NULL and the NPE-type
115                    define is 0. */
116
117                 addr = gregs[s1];
118                 type = EXCEPTION_HARDWARE_NULLPOINTER;
119
120                 if (addr != 0)
121                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
122         }
123
124         /* generate appropriate exception */
125
126         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
127
128         /* set registers (only if exception object ready) */
129
130         if (o != NULL) {
131                 _ss->r11  = (ptrint) o;
132                 _ss->r12  = (ptrint) xpc;
133                 _ss->srr0 = (ptrint) asm_handle_exception;
134         }
135 }
136
137
138 /* md_signal_handler_sigtrap ***************************************************
139
140    Signal handler for hardware-traps.
141
142 *******************************************************************************/
143
144 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
145 {
146         stackframeinfo      sfi;
147         ucontext_t         *_uc;
148         mcontext_t          _mc;
149         ppc_thread_state_t *_ss;
150         ptrint             *gregs;
151         u1                 *pv;
152         u1                 *sp;
153         u1                 *ra;
154         u1                 *xpc;
155         u4                  mcode;
156         s4                  s1;
157         ptrint              val;
158         s4                  type;
159         java_objectheader  *o;
160
161         _uc = (ucontext_t *) _p;
162         _mc = _uc->uc_mcontext;
163         _ss = &_mc->ss;
164
165         /* immitate a gregs array */
166
167         gregs = &_ss->r0;
168
169         /* get register values */
170
171         pv  = (u1 *) _ss->r13;
172         sp  = (u1 *) _ss->r1;
173         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
174         xpc = (u1 *) _ss->srr0;
175
176         /* get exception-throwing instruction */
177
178         mcode = *((u4 *) xpc);
179
180         s1 = M_OP3_GET_A(mcode);
181
182         /* for now we only handle ArrayIndexOutOfBoundsException */
183
184         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
185         val  = gregs[s1];
186
187         /* generate appropriate exception */
188
189         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
190
191         /* set registers */
192
193         _ss->r11  = (ptrint) o;
194         _ss->r12  = (ptrint) xpc;
195         _ss->srr0 = (ptrint) asm_handle_exception;
196 }
197
198
199 /* md_signal_handler_sigusr2 ***************************************************
200
201    Signal handler for profiling sampling.
202
203 *******************************************************************************/
204
205 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
206 {
207         threadobject       *t;
208         ucontext_t         *_uc;
209         mcontext_t          _mc;
210         ppc_thread_state_t *_ss;
211         u1                 *pc;
212
213         t = THREADOBJECT;
214
215         _uc = (ucontext_t *) _p;
216         _mc = _uc->uc_mcontext;
217         _ss = &_mc->ss;
218
219         pc = (u1 *) _ss->srr0;
220
221         t->pc = pc;
222 }
223
224
225 /* md_critical_section_restart *************************************************
226
227    Search the critical sections tree for a matching section and set
228    the PC to the restart point, if necessary.
229
230 *******************************************************************************/
231
232 #if defined(ENABLE_THREADS)
233 void md_critical_section_restart(ucontext_t *_uc)
234 {
235         mcontext_t          _mc;
236         ppc_thread_state_t *_ss;
237         u1                 *pc;
238         u1                 *npc;
239
240         _mc = _uc->uc_mcontext;
241         _ss = &_mc->ss;
242
243         pc = (u1 *) _ss->srr0;
244
245         npc = critical_find_restart_point(pc);
246
247         if (npc != NULL)
248                 _ss->srr0 = (ptrint) npc;
249 }
250 #endif
251
252
253 /*
254  * These are local overrides for various environment variables in Emacs.
255  * Please do not remove this and leave it at the end of the file, where
256  * Emacs will automagically detect them.
257  * ---------------------------------------------------------------------
258  * Local variables:
259  * mode: c
260  * indent-tabs-mode: t
261  * c-basic-offset: 4
262  * tab-width: 4
263  * End:
264  */