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