Merged revisions 7940-8026 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    $Id: md-os.c 7990 2007-05-30 21:05:20Z twisti $
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         ucontext_t         *_uc;
64         mcontext_t          _mc;
65         ppc_thread_state_t *_ss;
66         ptrint             *gregs;
67         u1                 *pv;
68         u1                 *sp;
69         u1                 *ra;
70         u1                 *xpc;
71         u4                  mcode;
72         s4                  s1;
73         s4                  disp;
74         s4                  d;
75         ptrint             addr;
76         ptrint             val;
77         s4                 type;
78         java_objectheader *o;
79
80         _uc = (ucontext_t *) _p;
81         _mc = _uc->uc_mcontext;
82         _ss = &_mc->ss;
83
84         /* immitate a gregs array */
85
86         gregs = &_ss->r0;
87
88         /* get register values */
89
90         pv  = (u1 *) _ss->r13;
91         sp  = (u1 *) _ss->r1;
92         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
93         xpc = (u1 *) _ss->srr0;
94
95         /* get exception-throwing instruction */
96
97         mcode = *((u4 *) xpc);
98
99         s1   = M_INSTR_OP2_IMM_A(mcode);
100         disp = M_INSTR_OP2_IMM_I(mcode);
101         d    = M_INSTR_OP2_IMM_D(mcode);
102
103         val  = gregs[d];
104
105         /* check for special-load */
106
107         if (s1 == REG_ZERO) {
108                 /* we use the exception type as load displacement */
109
110                 type = disp;
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         /* generate appropriate exception */
124
125         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
126
127         /* set registers */
128
129         _ss->r11  = (ptrint) o;
130         _ss->r12  = (ptrint) xpc;
131         _ss->srr0 = (ptrint) asm_handle_exception;
132 }
133
134
135 /* md_signal_handler_sigtrap ***************************************************
136
137    Signal handler for hardware-traps.
138
139 *******************************************************************************/
140
141 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
142 {
143         ucontext_t         *_uc;
144         mcontext_t          _mc;
145         ppc_thread_state_t *_ss;
146         ptrint             *gregs;
147         u1                 *pv;
148         u1                 *sp;
149         u1                 *ra;
150         u1                 *xpc;
151         u4                  mcode;
152         s4                  s1;
153         ptrint              val;
154         s4                  type;
155         java_objectheader  *o;
156
157         _uc = (ucontext_t *) _p;
158         _mc = _uc->uc_mcontext;
159         _ss = &_mc->ss;
160
161         /* immitate a gregs array */
162
163         gregs = &_ss->r0;
164
165         /* get register values */
166
167         pv  = (u1 *) _ss->r13;
168         sp  = (u1 *) _ss->r1;
169         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
170         xpc = (u1 *) _ss->srr0;
171
172         /* get exception-throwing instruction */
173
174         mcode = *((u4 *) xpc);
175
176         s1 = M_OP3_GET_A(mcode);
177
178         /* for now we only handle ArrayIndexOutOfBoundsException */
179
180         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
181         val  = gregs[s1];
182
183         /* generate appropriate exception */
184
185         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
186
187         /* set registers */
188
189         _ss->r11  = (ptrint) o;
190         _ss->r12  = (ptrint) xpc;
191         _ss->srr0 = (ptrint) asm_handle_exception;
192 }
193
194
195 /* md_signal_handler_sigusr2 ***************************************************
196
197    Signal handler for profiling sampling.
198
199 *******************************************************************************/
200
201 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
202 {
203         threadobject       *t;
204         ucontext_t         *_uc;
205         mcontext_t          _mc;
206         ppc_thread_state_t *_ss;
207         u1                 *pc;
208
209         t = THREADOBJECT;
210
211         _uc = (ucontext_t *) _p;
212         _mc = _uc->uc_mcontext;
213         _ss = &_mc->ss;
214
215         pc = (u1 *) _ss->srr0;
216
217         t->pc = pc;
218 }
219
220
221 /* md_critical_section_restart *************************************************
222
223    Search the critical sections tree for a matching section and set
224    the PC to the restart point, if necessary.
225
226 *******************************************************************************/
227
228 #if defined(ENABLE_THREADS)
229 void md_critical_section_restart(ucontext_t *_uc)
230 {
231         mcontext_t          _mc;
232         ppc_thread_state_t *_ss;
233         u1                 *pc;
234         u1                 *npc;
235
236         _mc = _uc->uc_mcontext;
237         _ss = &_mc->ss;
238
239         pc = (u1 *) _ss->srr0;
240
241         npc = critical_find_restart_point(pc);
242
243         if (npc != NULL)
244                 _ss->srr0 = (ptrint) npc;
245 }
246 #endif
247
248
249 /*
250  * These are local overrides for various environment variables in Emacs.
251  * Please do not remove this and leave it at the end of the file, where
252  * Emacs will automagically detect them.
253  * ---------------------------------------------------------------------
254  * Local variables:
255  * mode: c
256  * indent-tabs-mode: t
257  * c-basic-offset: 4
258  * tab-width: 4
259  * End:
260  */