Merged revisions 8137-8178 via svnmerge from
[cacao.git] / src / vm / jit / powerpc / linux / md-os.c
1 /* src/vm/jit/powerpc/linux/md-os.c - machine dependent PowerPC Linux 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 8178 2007-07-05 11:13:20Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/powerpc/codegen.h"
38 #include "vm/jit/powerpc/linux/md-abi.h"
39
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/threads.h"
42 #endif
43
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48
49 #if defined(ENABLE_PROFILING)
50 # include "vm/jit/optimizing/profile.h"
51 #endif
52
53 #include "vm/jit/stacktrace.h"
54
55
56 /* md_signal_handler_sigsegv ***************************************************
57
58    Signal handler for hardware-exceptions.
59
60 *******************************************************************************/
61
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 {
64         stackframeinfo     sfi;
65         ucontext_t        *_uc;
66         mcontext_t        *_mc;
67         unsigned long     *_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
83 #if defined(__UCLIBC__)
84         _mc    = &(_uc->uc_mcontext);
85         _gregs = _mc->regs->gpr;
86 #else
87         _mc    = _uc->uc_mcontext.uc_regs;
88         _gregs = _mc->gregs;
89 #endif
90
91         pv  = (u1 *) _gregs[REG_PV];
92         sp  = (u1 *) _gregs[REG_SP];
93         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
94         xpc = (u1 *) _gregs[PT_NIP];
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 */
129
130         _gregs[REG_ITMP1_XPTR] = (ptrint) o;
131         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
132         _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
133 }
134
135
136 /* md_signal_handler_sigtrap ***************************************************
137
138    Signal handler for hardware-traps.
139
140 *******************************************************************************/
141
142 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
143 {
144         stackframeinfo     sfi;
145         ucontext_t        *_uc;
146         mcontext_t        *_mc;
147         unsigned long     *_gregs;
148         u1                *pv;
149         u1                *sp;
150         u1                *ra;
151         u1                *xpc;
152         u4                 mcode;
153         s4                 s1;
154         ptrint             val;
155         s4                 type;
156         java_objectheader *o;
157
158         _uc = (ucontext_t *) _p;
159
160 #if defined(__UCLIBC__)
161         _mc    = &(_uc->uc_mcontext);
162         _gregs = _mc->regs->gpr;
163 #else
164         _mc    = _uc->uc_mcontext.uc_regs;
165         _gregs = _mc->gregs;
166 #endif
167
168         pv  = (u1 *) _gregs[REG_PV];
169         sp  = (u1 *) _gregs[REG_SP];
170         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
171         xpc = (u1 *) _gregs[PT_NIP];
172
173         /* get exception-throwing instruction */
174
175         mcode = *((u4 *) xpc);
176
177         s1 = M_OP3_GET_A(mcode);
178
179         /* for now we only handle ArrayIndexOutOfBoundsException */
180
181         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
182         val  = _gregs[s1];
183
184         /* generate appropriate exception */
185
186         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
187
188         /* set registers */
189
190         _gregs[REG_ITMP1_XPTR] = (ptrint) o;
191         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
192         _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
193 }
194
195
196 /* md_signal_handler_sigusr2 ***************************************************
197
198    Signal handler for profiling sampling.
199
200 *******************************************************************************/
201
202 #if defined(ENABLE_THREADS)
203 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
204 {
205         threadobject  *tobj;
206         ucontext_t    *_uc;
207         mcontext_t    *_mc;
208         unsigned long *_gregs;
209         u1            *pc;
210
211         tobj = THREADOBJECT;
212
213         _uc = (ucontext_t *) _p;
214
215 #if defined(__UCLIBC__)
216         _mc    = &(_uc->uc_mcontext);
217         _gregs = _mc->regs->gpr;
218 #else
219         _mc    = _uc->uc_mcontext.uc_regs;
220         _gregs = _mc->gregs;
221 #endif
222
223         pc = (u1 *) _gregs[PT_NIP];
224
225         tobj->pc = pc;
226 }
227 #endif
228
229
230 /* md_critical_section_restart *************************************************
231
232    Search the critical sections tree for a matching section and set
233    the PC to the restart point, if necessary.
234
235 *******************************************************************************/
236
237 #if defined(ENABLE_THREADS)
238 void md_critical_section_restart(ucontext_t *_uc)
239 {
240         mcontext_t    *_mc;
241         unsigned long *_gregs;
242         u1            *pc;
243         u1            *npc;
244
245 #if defined(__UCLIBC__)
246         _mc    = &(_uc->uc_mcontext);
247         _gregs = _mc->regs->gpr;
248 #else
249         _mc    = _uc->uc_mcontext.uc_regs;
250         _gregs = _mc->gregs;
251 #endif
252
253         pc = (u1 *) _gregs[PT_NIP];
254
255         npc = critical_find_restart_point(pc);
256
257         if (npc != NULL)
258                 _gregs[PT_NIP] = (ptrint) npc;
259 }
260 #endif
261
262
263 /*
264  * These are local overrides for various environment variables in Emacs.
265  * Please do not remove this and leave it at the end of the file, where
266  * Emacs will automagically detect them.
267  * ---------------------------------------------------------------------
268  * Local variables:
269  * mode: c
270  * indent-tabs-mode: t
271  * c-basic-offset: 4
272  * tab-width: 4
273  * End:
274  */