Merged trunk and subtype.
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <ucontext.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/powerpc/codegen.h"
35 #include "vm/jit/powerpc/md.h"
36 #include "vm/jit/powerpc/linux/md-abi.h"
37
38 #include "threads/thread.hpp"
39
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.h"
42 #include "vm/os.hpp"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46
47 #if defined(ENABLE_PROFILING)
48 # include "vm/jit/optimizing/profile.h"
49 #endif
50
51 #include "vm/jit/trap.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55
56    Signal handler for hardware-exceptions.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         ucontext_t     *_uc;
63         mcontext_t     *_mc;
64         unsigned long  *_gregs;
65         u1             *pv;
66         u1             *sp;
67         u1             *ra;
68         u1             *xpc;
69         u4              mcode;
70         int             s1;
71         int16_t         disp;
72         int             d;
73         intptr_t        addr;
74         intptr_t        val;
75         int             type;
76         void           *p;
77
78         _uc = (ucontext_t *) _p;
79
80 #if defined(__UCLIBC__)
81         _mc    = &(_uc->uc_mcontext);
82         _gregs = _mc->regs->gpr;
83 #else
84         _mc    = _uc->uc_mcontext.uc_regs;
85         _gregs = _mc->gregs;
86 #endif
87
88         pv  = (u1 *) _gregs[REG_PV];
89         sp  = (u1 *) _gregs[REG_SP];
90         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
91         xpc = (u1 *) _gregs[PT_NIP];
92
93         /* get exception-throwing instruction */
94
95         mcode = *((u4 *) xpc);
96
97         s1   = M_INSTR_OP2_IMM_A(mcode);
98         disp = M_INSTR_OP2_IMM_I(mcode);
99         d    = M_INSTR_OP2_IMM_D(mcode);
100
101         val  = _gregs[d];
102
103         /* check for special-load */
104
105         if (s1 == REG_ZERO) {
106                 /* we use the exception type as load displacement */
107
108                 type = disp;
109
110                 if (type == TRAP_COMPILER) {
111                         /* The XPC is the RA minus 4, because the RA points to the
112                            instruction after the call. */
113
114                         xpc = ra - 4;
115                 }
116         }
117         else {
118                 /* This is a normal NPE: addr must be NULL and the NPE-type
119                    define is 0. */
120
121                 addr = _gregs[s1];
122                 type = addr;
123         }
124
125         /* Handle the trap. */
126
127         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
128
129         /* Set registers. */
130
131         switch (type) {
132         case TRAP_COMPILER:
133                 if (p != NULL) {
134                         _gregs[REG_PV] = (uintptr_t) p;
135                         _gregs[PT_NIP] = (uintptr_t) p;
136                         break;
137                 }
138
139                 /* Get and set the PV from the parent Java method. */
140
141                 pv = md_codegen_get_pv_from_pc(ra);
142
143                 _gregs[REG_PV] = (uintptr_t) pv;
144
145                 /* Get the exception object. */
146
147                 p = builtin_retrieve_exception();
148
149                 assert(p != NULL);
150
151                 /* fall-through */
152
153         case TRAP_PATCHER:
154                 if (p == NULL)
155                         break;
156
157                 /* fall-through */
158                 
159         default:
160                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
161                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
162                 _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
163         }
164 }
165
166
167 /* md_signal_handler_sigtrap ***************************************************
168
169    Signal handler for hardware-traps.
170
171 *******************************************************************************/
172
173 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
174 {
175         ucontext_t     *_uc;
176         mcontext_t     *_mc;
177         unsigned long  *_gregs;
178         u1             *pv;
179         u1             *sp;
180         u1             *ra;
181         u1             *xpc;
182         u4              mcode;
183         int             s1;
184         intptr_t        val;
185         int             type;
186         void           *p;
187
188         _uc = (ucontext_t *) _p;
189
190 #if defined(__UCLIBC__)
191         _mc    = &(_uc->uc_mcontext);
192         _gregs = _mc->regs->gpr;
193 #else
194         _mc    = _uc->uc_mcontext.uc_regs;
195         _gregs = _mc->gregs;
196 #endif
197
198         pv  = (u1 *) _gregs[REG_PV];
199         sp  = (u1 *) _gregs[REG_SP];
200         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
201         xpc = (u1 *) _gregs[PT_NIP];
202
203         /* get exception-throwing instruction */
204
205         mcode = *((u4 *) xpc);
206
207         s1 = M_OP3_GET_A(mcode);
208
209         /* For now we only handle ArrayIndexOutOfBoundsException. */
210
211         type = TRAP_ArrayIndexOutOfBoundsException;
212         val  = _gregs[s1];
213
214         /* Handle the trap. */
215
216         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
217
218         /* Set registers. */
219
220         _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
221         _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
222         _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
223 }
224
225
226 /* md_signal_handler_sigusr1 ***************************************************
227
228    Signal handler for suspending threads.
229
230 *******************************************************************************/
231
232 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
233 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
234 {
235         ucontext_t    *_uc;
236         mcontext_t    *_mc;
237         unsigned long *_gregs;
238         u1            *pc;
239         u1            *sp;
240
241         _uc = (ucontext_t *) _p;
242
243 #if defined(__UCLIBC__)
244         _mc    = &(_uc->uc_mcontext);
245         _gregs = _mc->regs->gpr;
246 #else
247         _mc    = _uc->uc_mcontext.uc_regs;
248         _gregs = _mc->gregs;
249 #endif
250
251         /* get the PC and SP for this thread */
252
253         pc = (u1 *) _gregs[PT_NIP];
254         sp = (u1 *) _gregs[REG_SP];
255
256         /* now suspend the current thread */
257
258         threads_suspend_ack(pc, sp);
259 }
260 #endif
261
262
263 /* md_signal_handler_sigusr2 ***************************************************
264
265    Signal handler for profiling sampling.
266
267 *******************************************************************************/
268
269 #if defined(ENABLE_THREADS)
270 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
271 {
272         threadobject  *tobj;
273         ucontext_t    *_uc;
274         mcontext_t    *_mc;
275         unsigned long *_gregs;
276         u1            *pc;
277
278         tobj = THREADOBJECT;
279
280         _uc = (ucontext_t *) _p;
281
282 #if defined(__UCLIBC__)
283         _mc    = &(_uc->uc_mcontext);
284         _gregs = _mc->regs->gpr;
285 #else
286         _mc    = _uc->uc_mcontext.uc_regs;
287         _gregs = _mc->gregs;
288 #endif
289
290         pc = (u1 *) _gregs[PT_NIP];
291
292         tobj->pc = pc;
293 }
294 #endif
295
296
297 /* md_executionstate_read ******************************************************
298
299    Read the given context into an executionstate.
300
301 *******************************************************************************/
302
303 void md_executionstate_read(executionstate_t *es, void *context)
304 {
305         ucontext_t    *_uc;
306         mcontext_t    *_mc;
307         unsigned long *_gregs;
308         s4              i;
309
310         _uc = (ucontext_t *) context;
311
312 #if defined(__UCLIBC__)
313 #error Please port md_executionstate_read to __UCLIBC__
314 #else
315         _mc    = _uc->uc_mcontext.uc_regs;
316         _gregs = _mc->gregs;
317 #endif
318
319         /* read special registers */
320         es->pc = (u1 *) _gregs[PT_NIP];
321         es->sp = (u1 *) _gregs[REG_SP];
322         es->pv = (u1 *) _gregs[REG_PV];
323         es->ra = (u1 *) _gregs[PT_LNK];
324
325         /* read integer registers */
326         for (i = 0; i < INT_REG_CNT; i++)
327                 es->intregs[i] = _gregs[i];
328
329         /* read float registers */
330         /* Do not use the assignment operator '=', as the type of
331          * the _mc->fpregs[i] can cause invalid conversions. */
332
333         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
334         os_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
335 }
336
337
338 /* md_executionstate_write *****************************************************
339
340    Write the given executionstate back to the context.
341
342 *******************************************************************************/
343
344 void md_executionstate_write(executionstate_t *es, void *context)
345 {
346         ucontext_t    *_uc;
347         mcontext_t    *_mc;
348         unsigned long *_gregs;
349         s4              i;
350
351         _uc = (ucontext_t *) context;
352
353 #if defined(__UCLIBC__)
354 #error Please port md_executionstate_write to __UCLIBC__
355 #else
356         _mc    = _uc->uc_mcontext.uc_regs;
357         _gregs = _mc->gregs;
358 #endif
359
360         /* write integer registers */
361         for (i = 0; i < INT_REG_CNT; i++)
362                 _gregs[i] = es->intregs[i];
363
364         /* write float registers */
365         /* Do not use the assignment operator '=', as the type of
366          * the _mc->fpregs[i] can cause invalid conversions. */
367
368         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
369         os_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
370
371         /* write special registers */
372         _gregs[PT_NIP] = (ptrint) es->pc;
373         _gregs[REG_SP] = (ptrint) es->sp;
374         _gregs[REG_PV] = (ptrint) es->pv;
375         _gregs[PT_LNK] = (ptrint) es->ra;
376 }
377
378
379 /*
380  * These are local overrides for various environment variables in Emacs.
381  * Please do not remove this and leave it at the end of the file, where
382  * Emacs will automagically detect them.
383  * ---------------------------------------------------------------------
384  * Local variables:
385  * mode: c
386  * indent-tabs-mode: t
387  * c-basic-offset: 4
388  * tab-width: 4
389  * End:
390  */