Merged revisions 7766-7796 via svnmerge from
[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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    $Id: md-os.c 7770 2007-04-19 19:39:06Z twisti $
30
31 */
32
33
34 #include "config.h"
35
36 #include <assert.h>
37 #include <signal.h>
38 #include <ucontext.h>
39
40 #include "vm/types.h"
41
42 #include "vm/jit/powerpc/codegen.h"
43 #include "vm/jit/powerpc/darwin/md-abi.h"
44
45 #if defined(ENABLE_THREADS)
46 # include "threads/native/threads.h"
47 #endif
48
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/signallocal.h"
52 #include "vm/stringlocal.h"
53
54 #include "vm/jit/asmpart.h"
55 #include "vm/jit/stacktrace.h"
56
57
58 /* md_signal_handler_sigsegv ***************************************************
59
60    NullPointerException signal handler for hardware null pointer
61    check.
62
63 *******************************************************************************/
64
65 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
66 {
67         ucontext_t         *_uc;
68         mcontext_t          _mc;
69         ppc_thread_state_t *_ss;
70         ptrint             *gregs;
71         u1                 *pv;
72         u1                 *sp;
73         u1                 *ra;
74         u1                 *xpc;
75         u4                  mcode;
76         s4                  s1;
77         s4                  disp;
78         s4                  d;
79         ptrint             addr;
80         ptrint             val;
81         s4                 type;
82         java_objectheader *o;
83
84         _uc = (ucontext_t *) _p;
85         _mc = _uc->uc_mcontext;
86         _ss = &_mc->ss;
87
88         /* immitate a gregs array */
89
90         gregs = &_ss->r0;
91
92         /* get register values */
93
94         pv  = (u1 *) _ss->r13;
95         sp  = (u1 *) _ss->r1;
96         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
97         xpc = (u1 *) _ss->srr0;
98
99         /* get exception-throwing instruction */
100
101         mcode = *((u4 *) xpc);
102
103         s1   = M_INSTR_OP2_IMM_A(mcode);
104         disp = M_INSTR_OP2_IMM_I(mcode);
105         d    = M_INSTR_OP2_IMM_D(mcode);
106
107         val  = gregs[d];
108
109         /* check for special-load */
110
111         if (s1 == REG_ZERO) {
112                 /* we use the exception type as load displacement */
113
114                 type = disp;
115         }
116         else {
117                 /* This is a normal NPE: addr must be NULL and the NPE-type
118                    define is 0. */
119
120                 addr = gregs[s1];
121                 type = EXCEPTION_HARDWARE_NULLPOINTER;
122
123                 if (addr != 0)
124                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
125         }
126
127         /* generate appropriate exception */
128
129         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
130
131         /* set registers */
132
133         _ss->r11  = (ptrint) o;
134         _ss->r12  = (ptrint) xpc;
135         _ss->srr0 = (ptrint) asm_handle_exception;
136 }
137
138
139 /* md_signal_handler_sigtrap ***************************************************
140
141    Signal handler for hardware-traps.
142
143 *******************************************************************************/
144
145 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
146 {
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);
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 #if defined(ENABLE_THREADS)
226 void thread_restartcriticalsection(ucontext_t *_uc)
227 {
228         mcontext_t          _mc;
229         ppc_thread_state_t *_ss;
230         void               *critical;
231
232         _mc = _uc->uc_mcontext;
233         _ss = &_mc->ss;
234
235         critical = critical_find_restart_point((void *) _ss->srr0);
236
237         if (critical)
238                 _ss->srr0 = (ptrint) critical;
239 }
240 #endif
241
242
243 /*
244  * These are local overrides for various environment variables in Emacs.
245  * Please do not remove this and leave it at the end of the file, where
246  * Emacs will automagically detect them.
247  * ---------------------------------------------------------------------
248  * Local variables:
249  * mode: c
250  * indent-tabs-mode: t
251  * c-basic-offset: 4
252  * tab-width: 4
253  * End:
254  */