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