* src/vm/jit/powerpc/linux/md-os.c (md_signal_handler_sigsegv): Fixed
[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 7617 2007-03-29 23:22:07Z twisti $
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         ucontext_t        *_uc;
65         mcontext_t        *_mc;
66         u1                *pv;
67         u1                *sp;
68         u1                *ra;
69         u1                *xpc;
70         u4                 mcode;
71         s4                 s1;
72         s4                 disp;
73         s4                 d;
74         ptrint             addr;
75         ptrint             val;
76         s4                 type;
77         java_objectheader *o;
78
79         _uc = (ucontext_t *) _p;
80         _mc = _uc->uc_mcontext.uc_regs;
81
82         pv  = (u1 *) _mc->gregs[REG_PV];
83         sp  = (u1 *) _mc->gregs[REG_SP];
84         ra  = (u1 *) _mc->gregs[PT_LNK];         /* this is correct for leafs */
85         xpc = (u1 *) _mc->gregs[PT_NIP];
86
87         /* get exception-throwing instruction */
88
89         mcode = *((u4 *) xpc);
90
91         s1   = M_INSTR_OP2_IMM_A(mcode);
92         disp = M_INSTR_OP2_IMM_I(mcode);
93         d    = M_INSTR_OP2_IMM_D(mcode);
94
95         val  = _mc->gregs[d];
96
97         /* check for special-load */
98
99         if (s1 == REG_ZERO) {
100                 /* we use the exception type as load displacement */
101
102                 type = disp;
103         }
104         else {
105                 /* This is a normal NPE: addr must be NULL and the NPE-type
106                    define is 0. */
107
108                 addr = _mc->gregs[s1];
109                 type = EXCEPTION_HARDWARE_NULLPOINTER;
110
111                 if (addr != 0)
112                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
113         }
114
115         /* generate appropriate exception */
116
117         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
118
119         /* set registers */
120
121         _mc->gregs[REG_ITMP1_XPTR] = (ptrint) o;
122         _mc->gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
123         _mc->gregs[PT_NIP]         = (ptrint) asm_handle_exception;
124 }
125
126
127 /* md_signal_handler_sigtrap ***************************************************
128
129    Signal handler for hardware-traps.
130
131 *******************************************************************************/
132
133 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
134 {
135         ucontext_t        *_uc;
136         mcontext_t        *_mc;
137         u1                *pv;
138         u1                *sp;
139         u1                *ra;
140         u1                *xpc;
141         u4                 mcode;
142         s4                 s1;
143         ptrint             val;
144         s4                 type;
145         java_objectheader *o;
146
147         _uc = (ucontext_t *) _p;
148         _mc = _uc->uc_mcontext.uc_regs;
149
150         pv  = (u1 *) _mc->gregs[REG_PV];
151         sp  = (u1 *) _mc->gregs[REG_SP];
152         ra  = (u1 *) _mc->gregs[PT_LNK];         /* this is correct for leafs */
153         xpc = (u1 *) _mc->gregs[PT_NIP];
154
155         /* get exception-throwing instruction */
156
157         mcode = *((u4 *) xpc);
158
159         s1 = M_OP3_GET_A(mcode);
160
161         /* for now we only handle ArrayIndexOutOfBoundsException */
162
163         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
164         val  = _mc->gregs[s1];
165
166         /* generate appropriate exception */
167
168         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
169
170         /* set registers */
171
172         _mc->gregs[REG_ITMP1_XPTR] = (ptrint) o;
173         _mc->gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
174         _mc->gregs[PT_NIP]         = (ptrint) asm_handle_exception;
175 }
176
177
178 /* md_signal_handler_sigusr2 ***************************************************
179
180    Signal handler for profiling sampling.
181
182 *******************************************************************************/
183
184 #if defined(ENABLE_THREADS)
185 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
186 {
187         threadobject *tobj;
188         ucontext_t   *_uc;
189         mcontext_t   *_mc;
190         u1           *pc;
191
192         tobj = THREADOBJECT;
193
194         _uc = (ucontext_t *) _p;
195         _mc = _uc->uc_mcontext.uc_regs;
196
197         pc = (u1 *) _mc->gregs[PT_NIP];
198
199         tobj->pc = pc;
200 }
201
202
203 void thread_restartcriticalsection(ucontext_t *_uc)
204 {
205         mcontext_t *_mc;
206         u1         *pc;
207         void       *critical;
208
209         _mc = _uc->uc_mcontext.uc_regs;
210
211         pc = (u1 *) _mc->gregs[PT_NIP];
212
213         critical = critical_find_restart_point(pc);
214
215         if (critical)
216                 _mc->gregs[PT_NIP] = (ptrint) critical;
217 }
218 #endif
219
220
221 /*
222  * These are local overrides for various environment variables in Emacs.
223  * Please do not remove this and leave it at the end of the file, where
224  * Emacs will automagically detect them.
225  * ---------------------------------------------------------------------
226  * Local variables:
227  * mode: c
228  * indent-tabs-mode: t
229  * c-basic-offset: 4
230  * tab-width: 4
231  * End:
232  */