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