* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[cacao.git] / src / vm / jit / m68k / linux / md-os.c
1 /* src/vm/jit/m68k/linux/md-os.c - linux specific 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 #include "config.h"
28
29 #include "md-os.h"
30 #include "md-abi.h"
31
32 #include "vm/vm.h"
33 #include "vm/exceptions.h"
34 #include "vm/jit/asmpart.h"
35 #include "vm/signallocal.h"
36
37 #include <assert.h>
38 #include <stdlib.h>
39 /*#include <signal.h>*/
40 #include <stdint.h>
41 #include <sys/ucontext.h> /* has br0ken ucontext_t*/
42
43 /*
44  *  The glibc bundeled with my devels system ucontext.h differs with kernels ucontext.h. Not good.
45  *  The following stuff is taken from 2.6.10 + freescale coldfire patches:
46  */
47 typedef struct actual_fpregset {
48         int f_fpcntl[3];
49         int f_fpregs[8*3];
50 } actual_fpregset_t;
51
52 typedef struct actual_mcontext {
53         int version;
54         gregset_t gregs;        /* 0...7 = %d0-%d7, 8...15 = %a0-%a7 */
55         actual_fpregset_t fpregs;
56 } actual_mcontext_t;
57
58 #define GREGS_ADRREG_OFF        8
59
60 typedef struct actual_ucontext {
61         unsigned long     uc_flags;
62         struct actual_ucontext  *uc_link;
63         stack_t           uc_stack;
64         struct actual_mcontext   uc_mcontext;
65         unsigned long     uc_filler[80];
66         sigset_t          uc_sigmask;   /* mask last for extensibility */
67 } actual_ucontext_t;
68
69
70
71 /* md_signal_handler_sigsegv ******************************************
72  *
73  * Invoked when a Nullpointerexception occured, or when the vm 
74  * crashes, hard to tell the difference. 
75  **********************************************************************/
76 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
77 {       
78         uint32_t        xpc, sp;
79         uint16_t        opc;
80         uint32_t        val, regval, off;
81         bool            adrreg;
82         void        *p;
83         actual_mcontext_t       *_mc;
84         actual_ucontext_t       *_uc;
85
86
87         _uc = (actual_ucontext_t*)_p;
88         _mc = &_uc->uc_mcontext;
89         sp = _mc->gregs[R_SP];
90         xpc = _mc->gregs[R_PC];
91         opc = *(uint16_t*)xpc;
92
93         /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
94         /* the first two are 2*32bit sized */
95         adrreg = false;
96         off = 0;
97         if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3)))   {
98                 if (opc & (1<<6)) adrreg = true;                /* M_XLD */
99                 val = opc & 0x0007;
100                 off = *(uint16_t*)(xpc+1);
101         } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6)))    {
102                 if (opc & (1<<3)) adrreg = true;                /* M_XST */
103                 val = (opc >> 9) & 0x0007;
104                 off = *(uint16_t*)(xpc+1);
105         } else {
106                 
107                 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
108                 */
109                 /* now check the 32 bit sized instructions */
110                 if ((opc & (2<<3)) == (2<<3))   {
111                         if (opc & (1<<6)) adrreg = true;                /* M_L*X */
112                         val = opc & 0x0007;
113                 } else if ((opc & (2<<6)) == (2<<6))    {
114                         if (opc & (1<<3)) adrreg = true;                /* M_S*X */
115                         val = (opc >> 9) & 0x0007;
116                 } else {
117                         vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
118                 }
119         }
120
121         /* val is now register number, adreg == true if it is an address regsiter */
122         regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
123         /*
124         if (regval != 0)        {
125                 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", regval);
126         }*/
127
128
129         /*fprintf(stderr, "SEGV: sp=%x, xpc=%x, regval=%x\n", sp, xpc, regval);
130         */
131
132         /* Handle the type. */
133
134         p = signal_handle(EXCEPTION_HARDWARE_NULLPOINTER, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
135
136         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
137         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
138         _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
139 }
140
141 /* md_signal_handler_sigill *******************************************
142  *
143  * This handler is used to generate hardware exceptions.
144  * Type of exception derived from trap number.
145  * If an object is needed a tst instruction (2 byte long) has
146  * been created directly before the trap instruction (2 bytes long).
147  * the last 3 bit of this tst instruction contain the register number.
148  **********************************************************************/
149 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
150 {
151         uint32_t        xpc, sp, ra, pv;
152         uint16_t        opc;
153         uint32_t        type;
154         uint32_t        regval;
155         void        *p;
156         actual_mcontext_t       *_mc;
157         actual_ucontext_t       *_uc;
158
159         _uc = (actual_ucontext_t*)_p;
160         xpc = (uint32_t)siginfo->si_addr;
161
162         pv = NULL;      /* we have no pv */
163         ra = xpc;       /* that is ture for most cases */
164
165         if (siginfo->si_code == ILL_ILLOPC)     {
166                 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
167         }
168         if (siginfo->si_code != ILL_ILLTRP)     {
169                 vm_abort("md_signal_handler_sigill: Caught something not a trap");
170         }
171
172         opc = *(uint16_t*)(xpc-2);
173
174         assert( (opc&0xfff0) == 0x4e40 );
175         type = opc & 0x000f;            /* filter trap number */
176
177         _mc = &_uc->uc_mcontext;
178         sp = _mc->gregs[R_SP];
179
180         /* Figure out in which register the object causing the exception resides for appropiate exceptions
181          */
182         switch (type)   {
183                 case EXCEPTION_HARDWARE_ARITHMETIC:
184                 case EXCEPTION_HARDWARE_EXCEPTION:
185                         /* nothing */
186                         break;
187                 case EXCEPTION_HARDWARE_CLASSCAST: 
188                         regval = *(uint16_t*)(xpc-4);
189                         assert( (regval&0xfff0) == 0x4a00 );
190                         /* was in a address register */
191                         regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
192                         break;
193                 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
194                         regval = *(uint16_t*)(xpc-4);
195                         assert( (regval&0xfff0) == 0x4a00 );
196                         /* was a data register */
197                         regval = _mc->gregs[regval & 0x7];
198                         break;
199                 case M68K_EXCEPTION_HARDWARE_NULLPOINTER:
200                         type = EXCEPTION_HARDWARE_NULLPOINTER;
201                         break;
202                 case EXCEPTION_HARDWARE_COMPILER:
203                         regval = *(uint16_t*)(xpc-4);
204                         assert( (regval&0xfff0) == 0x4a00 );
205                         /* was in a address register */
206                         regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
207
208                         pv = xpc-4;     /* the compiler stub consists of 2 instructions */
209                         ra = md_stacktrace_get_returnaddress(sp, 0);
210                         sp = sp + SIZEOF_VOID_P;
211                         xpc = ra - 2;
212                         break;
213                 case EXCEPTION_HARDWARE_PATCHER:
214                         xpc -= 2;
215                         ra = xpc;
216                         break;
217
218                 default: assert(0);
219         }
220
221         /*fprintf(stderr, "NEW HWE: sp=%x, xpc=%x, tpye=%x, regval=%x\n", sp, xpc, type, regval);
222         */
223
224         /* Handle the type. */
225         p = signal_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
226
227
228         switch (type)   {
229                 case EXCEPTION_HARDWARE_COMPILER:
230                         if (p == NULL)  {
231                                 /* exception when compiling the method */
232                                 java_object_t *o = exceptions_get_and_clear_exceptions();
233
234                                 _mc->gregs[R_SP] = sp;  /* remove RA from stack */
235
236                                 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) o;
237                                 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
238                                 _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
239
240                         } else  {
241                                 /* compilation ok, execute */
242                                 _mc->gregs[R_PC] = p;
243                         }
244                         break;
245
246                 case EXCEPTION_HARDWARE_PATCHER:
247                         if (p == NULL) { 
248                                 /* no expcetion while patching, continue */
249                                 _mc->gregs[R_PC] = xpc; 
250                                 return; 
251                         }
252                         /* fall-through in case of exception */
253                 default:
254                         /* a normal exception with normal expcetion handling */
255                         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
256                         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
257                         _mc->gregs[R_PC]                             = (intptr_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         u1         *pc;
273         u1         *sp;
274
275         _uc = (ucontext_t *) _p;
276         _mc = &_uc->uc_mcontext;
277
278         /* get the PC and SP for this thread */
279         pc = (u1 *) _mc->gregs[R_PC];
280         sp = (u1 *) _mc->gregs[R_SP];
281
282         /* now suspend the current thread */
283         threads_suspend_ack(pc, sp);
284 }
285 #endif
286
287
288 /*
289  * These are local overrides for various environment variables in Emacs.
290  * Please do not remove this and leave it at the end of the file, where
291  * Emacs will automagically detect them.
292  * ---------------------------------------------------------------------
293  * Local variables:
294  * mode: c
295  * indent-tabs-mode: t
296  * c-basic-offset: 4
297  * tab-width: 4
298  * End:
299  * vim:noexpandtab:sw=4:ts=4:
300  */