Merged revisions 7797-7917 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 7864 2007-05-03 21:17:26Z 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         unsigned long     *_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
82 #if defined(__UCLIBC__)
83         _mc    = &(_uc->uc_mcontext);
84         _gregs = _mc->regs->gpr;
85 #else
86         _mc    = _uc->uc_mcontext.uc_regs;
87         _gregs = _mc->gregs;
88 #endif
89
90         pv  = (u1 *) _gregs[REG_PV];
91         sp  = (u1 *) _gregs[REG_SP];
92         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
93         xpc = (u1 *) _gregs[PT_NIP];
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         _gregs[REG_ITMP1_XPTR] = (ptrint) o;
130         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
131         _gregs[PT_NIP]         = (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         unsigned long     *_gregs;
146         u1                *pv;
147         u1                *sp;
148         u1                *ra;
149         u1                *xpc;
150         u4                 mcode;
151         s4                 s1;
152         ptrint             val;
153         s4                 type;
154         java_objectheader *o;
155
156         _uc = (ucontext_t *) _p;
157
158 #if defined(__UCLIBC__)
159         _mc    = &(_uc->uc_mcontext);
160         _gregs = _mc->regs->gpr;
161 #else
162         _mc    = _uc->uc_mcontext.uc_regs;
163         _gregs = _mc->gregs;
164 #endif
165
166         pv  = (u1 *) _gregs[REG_PV];
167         sp  = (u1 *) _gregs[REG_SP];
168         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
169         xpc = (u1 *) _gregs[PT_NIP];
170
171         /* get exception-throwing instruction */
172
173         mcode = *((u4 *) xpc);
174
175         s1 = M_OP3_GET_A(mcode);
176
177         /* for now we only handle ArrayIndexOutOfBoundsException */
178
179         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
180         val  = _gregs[s1];
181
182         /* generate appropriate exception */
183
184         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
185
186         /* set registers */
187
188         _gregs[REG_ITMP1_XPTR] = (ptrint) o;
189         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
190         _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
191 }
192
193
194 /* md_signal_handler_sigusr2 ***************************************************
195
196    Signal handler for profiling sampling.
197
198 *******************************************************************************/
199
200 #if defined(ENABLE_THREADS)
201 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
202 {
203         threadobject  *tobj;
204         ucontext_t    *_uc;
205         mcontext_t    *_mc;
206         unsigned long *_gregs;
207         u1            *pc;
208
209         tobj = THREADOBJECT;
210
211         _uc = (ucontext_t *) _p;
212
213 #if defined(__UCLIBC__)
214         _mc    = &(_uc->uc_mcontext);
215         _gregs = _mc->regs->gpr;
216 #else
217         _mc    = _uc->uc_mcontext.uc_regs;
218         _gregs = _mc->gregs;
219 #endif
220
221         pc = (u1 *) _gregs[PT_NIP];
222
223         tobj->pc = pc;
224 }
225
226
227 /* md_critical_section_restart *************************************************
228
229    Search the critical sections tree for a matching section and set
230    the PC to the restart point, if necessary.
231
232 *******************************************************************************/
233
234 void md_critical_section_restart(ucontext_t *_uc)
235 {
236         mcontext_t    *_mc;
237         unsigned long *_gregs;
238         u1            *pc;
239         u1            *npc;
240
241 #if defined(__UCLIBC__)
242         _mc    = &(_uc->uc_mcontext);
243         _gregs = _mc->regs->gpr;
244 #else
245         _mc    = _uc->uc_mcontext.uc_regs;
246         _gregs = _mc->gregs;
247 #endif
248
249         pc = (u1 *) _gregs[PT_NIP];
250
251         npc = critical_find_restart_point(pc);
252
253         if (npc != NULL) {
254                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
255
256                 _gregs[PT_NIP] = (ptrint) npc;
257         }
258 }
259 #endif
260
261
262 /*
263  * These are local overrides for various environment variables in Emacs.
264  * Please do not remove this and leave it at the end of the file, where
265  * Emacs will automagically detect them.
266  * ---------------------------------------------------------------------
267  * Local variables:
268  * mode: c
269  * indent-tabs-mode: t
270  * c-basic-offset: 4
271  * tab-width: 4
272  * End:
273  */