* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/powerpc/codegen.h"
37 #include "vm/jit/powerpc/linux/md-abi.h"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/threads.h"
41 #endif
42
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45 #include "vm/stringlocal.h"
46 #include "vm/jit/asmpart.h"
47
48 #if defined(ENABLE_PROFILING)
49 # include "vm/jit/optimizing/profile.h"
50 #endif
51
52 #include "vm/jit/stacktrace.h"
53
54
55 /* md_signal_handler_sigsegv ***************************************************
56
57    Signal handler for hardware-exceptions.
58
59 *******************************************************************************/
60
61 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
62 {
63         stackframeinfo  sfi;
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         int             s1;
73         int16_t         disp;
74         int             d;
75         intptr_t        addr;
76         intptr_t        val;
77         int             type;
78         void           *p;
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         /* create stackframeinfo */
124
125         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
126
127         /* Handle the type. */
128
129         p = signal_handle(xpc, type, val);
130
131         /* remove stackframeinfo */
132
133         stacktrace_remove_stackframeinfo(&sfi);
134
135         /* set registers (only if exception object ready) */
136
137         if (p != NULL) {
138                 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
139                 _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
140                 _gregs[PT_NIP]         = (intptr_t) asm_handle_exception;
141         }
142 }
143
144
145 /* md_signal_handler_sigtrap ***************************************************
146
147    Signal handler for hardware-traps.
148
149 *******************************************************************************/
150
151 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
152 {
153         stackframeinfo  sfi;
154         ucontext_t     *_uc;
155         mcontext_t     *_mc;
156         unsigned long  *_gregs;
157         u1             *pv;
158         u1             *sp;
159         u1             *ra;
160         u1             *xpc;
161         u4              mcode;
162         int             s1;
163         intptr_t        val;
164         int             type;
165         void           *p;
166
167         _uc = (ucontext_t *) _p;
168
169 #if defined(__UCLIBC__)
170         _mc    = &(_uc->uc_mcontext);
171         _gregs = _mc->regs->gpr;
172 #else
173         _mc    = _uc->uc_mcontext.uc_regs;
174         _gregs = _mc->gregs;
175 #endif
176
177         pv  = (u1 *) _gregs[REG_PV];
178         sp  = (u1 *) _gregs[REG_SP];
179         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
180         xpc = (u1 *) _gregs[PT_NIP];
181
182         /* get exception-throwing instruction */
183
184         mcode = *((u4 *) xpc);
185
186         s1 = M_OP3_GET_A(mcode);
187
188         /* for now we only handle ArrayIndexOutOfBoundsException */
189
190         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
191         val  = _gregs[s1];
192
193         /* create stackframeinfo */
194
195         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
196
197         /* Handle the type. */
198
199         p = signal_handle(xpc, type, val);
200
201         /* remove stackframeinfo */
202
203         stacktrace_remove_stackframeinfo(&sfi);
204
205         /* set registers */
206
207         _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
208         _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
209         _gregs[PT_NIP]         = (intptr_t) asm_handle_exception;
210 }
211
212
213 /* md_signal_handler_sigusr2 ***************************************************
214
215    Signal handler for profiling sampling.
216
217 *******************************************************************************/
218
219 #if defined(ENABLE_THREADS)
220 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
221 {
222         threadobject  *tobj;
223         ucontext_t    *_uc;
224         mcontext_t    *_mc;
225         unsigned long *_gregs;
226         u1            *pc;
227
228         tobj = THREADOBJECT;
229
230         _uc = (ucontext_t *) _p;
231
232 #if defined(__UCLIBC__)
233         _mc    = &(_uc->uc_mcontext);
234         _gregs = _mc->regs->gpr;
235 #else
236         _mc    = _uc->uc_mcontext.uc_regs;
237         _gregs = _mc->gregs;
238 #endif
239
240         pc = (u1 *) _gregs[PT_NIP];
241
242         tobj->pc = pc;
243 }
244 #endif
245
246
247 /* md_critical_section_restart *************************************************
248
249    Search the critical sections tree for a matching section and set
250    the PC to the restart point, if necessary.
251
252 *******************************************************************************/
253
254 #if defined(ENABLE_THREADS)
255 void md_critical_section_restart(ucontext_t *_uc)
256 {
257         mcontext_t    *_mc;
258         unsigned long *_gregs;
259         u1            *pc;
260         u1            *npc;
261
262 #if defined(__UCLIBC__)
263         _mc    = &(_uc->uc_mcontext);
264         _gregs = _mc->regs->gpr;
265 #else
266         _mc    = _uc->uc_mcontext.uc_regs;
267         _gregs = _mc->gregs;
268 #endif
269
270         pc = (u1 *) _gregs[PT_NIP];
271
272         npc = critical_find_restart_point(pc);
273
274         if (npc != NULL)
275                 _gregs[PT_NIP] = (ptrint) npc;
276 }
277 #endif
278
279
280 /*
281  * These are local overrides for various environment variables in Emacs.
282  * Please do not remove this and leave it at the end of the file, where
283  * Emacs will automagically detect them.
284  * ---------------------------------------------------------------------
285  * Local variables:
286  * mode: c
287  * indent-tabs-mode: t
288  * c-basic-offset: 4
289  * tab-width: 4
290  * End:
291  */