* src/vm/jit/patcher-common.cpp (patcher_add_patch_ref): 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, 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         default:
154                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
155                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
156                 _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
157         }
158 }
159
160
161 /**
162  * Signal handler for patcher calls.
163  */
164 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
165 {
166         ucontext_t* _uc = (ucontext_t*) _p;
167         mcontext_t* _mc;
168         unsigned long* _gregs;
169
170 #if defined(__UCLIBC__)
171         _mc    = &(_uc->uc_mcontext);
172         _gregs = _mc->regs->gpr;
173 #else
174         _mc    = _uc->uc_mcontext.uc_regs;
175         _gregs = _mc->gregs;
176 #endif
177
178         /* get register values */
179
180         void* pv = (void*) _gregs[REG_PV];
181         void* sp = (void*) _gregs[REG_SP];
182         void* ra = (void*) _gregs[PT_LNK]; // The RA is correct for leag methods.
183         void* xpc =(void*) _gregs[PT_NIP];
184
185         // This signal is always a patcher.
186         int      type = TRAP_PATCHER;
187         intptr_t val  = 0;
188
189         // Handle the trap.
190         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
191
192         // Set registers if we have an exception, continue execution
193         // otherwise.
194         if (p != NULL) {
195                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
196                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
197                 _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
198         }
199 }
200
201
202 /* md_signal_handler_sigtrap ***************************************************
203
204    Signal handler for hardware-traps.
205
206 *******************************************************************************/
207
208 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
209 {
210         ucontext_t     *_uc;
211         mcontext_t     *_mc;
212         unsigned long  *_gregs;
213         u1             *pv;
214         u1             *sp;
215         u1             *ra;
216         u1             *xpc;
217         u4              mcode;
218         int             s1;
219         intptr_t        val;
220         int             type;
221         void           *p;
222
223         _uc = (ucontext_t *) _p;
224
225 #if defined(__UCLIBC__)
226         _mc    = &(_uc->uc_mcontext);
227         _gregs = _mc->regs->gpr;
228 #else
229         _mc    = _uc->uc_mcontext.uc_regs;
230         _gregs = _mc->gregs;
231 #endif
232
233         pv  = (u1 *) _gregs[REG_PV];
234         sp  = (u1 *) _gregs[REG_SP];
235         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
236         xpc = (u1 *) _gregs[PT_NIP];
237
238         /* get exception-throwing instruction */
239
240         mcode = *((u4 *) xpc);
241
242         s1 = M_OP3_GET_A(mcode);
243
244         /* For now we only handle ArrayIndexOutOfBoundsException. */
245
246         type = TRAP_ArrayIndexOutOfBoundsException;
247         val  = _gregs[s1];
248
249         /* Handle the trap. */
250
251         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
252
253         /* Set registers. */
254
255         _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
256         _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
257         _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
258 }
259
260
261 /* md_signal_handler_sigusr1 ***************************************************
262
263    Signal handler for suspending threads.
264
265 *******************************************************************************/
266
267 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
268 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
269 {
270         ucontext_t    *_uc;
271         mcontext_t    *_mc;
272         unsigned long *_gregs;
273         u1            *pc;
274         u1            *sp;
275
276         _uc = (ucontext_t *) _p;
277
278 #if defined(__UCLIBC__)
279         _mc    = &(_uc->uc_mcontext);
280         _gregs = _mc->regs->gpr;
281 #else
282         _mc    = _uc->uc_mcontext.uc_regs;
283         _gregs = _mc->gregs;
284 #endif
285
286         /* get the PC and SP for this thread */
287
288         pc = (u1 *) _gregs[PT_NIP];
289         sp = (u1 *) _gregs[REG_SP];
290
291         /* now suspend the current thread */
292
293         threads_suspend_ack(pc, sp);
294 }
295 #endif
296
297
298 /* md_signal_handler_sigusr2 ***************************************************
299
300    Signal handler for profiling sampling.
301
302 *******************************************************************************/
303
304 #if defined(ENABLE_THREADS)
305 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
306 {
307         threadobject  *tobj;
308         ucontext_t    *_uc;
309         mcontext_t    *_mc;
310         unsigned long *_gregs;
311         u1            *pc;
312
313         tobj = THREADOBJECT;
314
315         _uc = (ucontext_t *) _p;
316
317 #if defined(__UCLIBC__)
318         _mc    = &(_uc->uc_mcontext);
319         _gregs = _mc->regs->gpr;
320 #else
321         _mc    = _uc->uc_mcontext.uc_regs;
322         _gregs = _mc->gregs;
323 #endif
324
325         pc = (u1 *) _gregs[PT_NIP];
326
327         tobj->pc = pc;
328 }
329 #endif
330
331
332 /* md_executionstate_read ******************************************************
333
334    Read the given context into an executionstate.
335
336 *******************************************************************************/
337
338 void md_executionstate_read(executionstate_t *es, void *context)
339 {
340         ucontext_t    *_uc;
341         mcontext_t    *_mc;
342         unsigned long *_gregs;
343         s4              i;
344
345         _uc = (ucontext_t *) context;
346
347 #if defined(__UCLIBC__)
348 #error Please port md_executionstate_read to __UCLIBC__
349 #else
350         _mc    = _uc->uc_mcontext.uc_regs;
351         _gregs = _mc->gregs;
352 #endif
353
354         /* read special registers */
355         es->pc = (u1 *) _gregs[PT_NIP];
356         es->sp = (u1 *) _gregs[REG_SP];
357         es->pv = (u1 *) _gregs[REG_PV];
358         es->ra = (u1 *) _gregs[PT_LNK];
359
360         /* read integer registers */
361         for (i = 0; i < INT_REG_CNT; i++)
362                 es->intregs[i] = _gregs[i];
363
364         /* read 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(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
370 }
371
372
373 /* md_executionstate_write *****************************************************
374
375    Write the given executionstate back to the context.
376
377 *******************************************************************************/
378
379 void md_executionstate_write(executionstate_t *es, void *context)
380 {
381         ucontext_t    *_uc;
382         mcontext_t    *_mc;
383         unsigned long *_gregs;
384         s4              i;
385
386         _uc = (ucontext_t *) context;
387
388 #if defined(__UCLIBC__)
389 #error Please port md_executionstate_write to __UCLIBC__
390 #else
391         _mc    = _uc->uc_mcontext.uc_regs;
392         _gregs = _mc->gregs;
393 #endif
394
395         /* write integer registers */
396         for (i = 0; i < INT_REG_CNT; i++)
397                 _gregs[i] = es->intregs[i];
398
399         /* write float registers */
400         /* Do not use the assignment operator '=', as the type of
401          * the _mc->fpregs[i] can cause invalid conversions. */
402
403         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
404         os_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
405
406         /* write special registers */
407         _gregs[PT_NIP] = (ptrint) es->pc;
408         _gregs[REG_SP] = (ptrint) es->sp;
409         _gregs[REG_PV] = (ptrint) es->pv;
410         _gregs[PT_LNK] = (ptrint) es->ra;
411 }
412
413
414 /*
415  * These are local overrides for various environment variables in Emacs.
416  * Please do not remove this and leave it at the end of the file, where
417  * Emacs will automagically detect them.
418  * ---------------------------------------------------------------------
419  * Local variables:
420  * mode: c
421  * indent-tabs-mode: t
422  * c-basic-offset: 4
423  * tab-width: 4
424  * End:
425  */