merged volatile memory barriers
[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/signallocal.hpp"
32 #include "vm/vm.hpp"
33
34 #include "vm/jit/asmpart.h"
35 #include "vm/jit/trap.hpp"
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
77 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
78 {       
79         uint32_t        xpc, sp;
80         uint16_t        opc;
81         uint32_t        val, regval, off;
82         bool            adrreg;
83         void        *p;
84         actual_mcontext_t       *_mc;
85         actual_ucontext_t       *_uc;
86         int type;
87
88         _uc = (actual_ucontext_t*)_p;
89         _mc = &_uc->uc_mcontext;
90         sp = _mc->gregs[R_SP];
91         xpc = _mc->gregs[R_PC];
92         opc = *(uint16_t*)xpc;
93
94         /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
95         /* the first two are 2*32bit sized */
96         adrreg = false;
97         off = 0;
98         if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3)))   {
99                 if (opc & (1<<6)) adrreg = true;                /* M_XLD */
100                 val = opc & 0x0007;
101                 off = *(uint16_t*)(xpc+1);
102         } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6)))    {
103                 if (opc & (1<<3)) adrreg = true;                /* M_XST */
104                 val = (opc >> 9) & 0x0007;
105                 off = *(uint16_t*)(xpc+1);
106         } else {
107                 
108                 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
109                 */
110                 /* now check the 32 bit sized instructions */
111                 if ((opc & (2<<3)) == (2<<3))   {
112                         if (opc & (1<<6)) adrreg = true;                /* M_L*X */
113                         val = opc & 0x0007;
114                 } else if ((opc & (2<<6)) == (2<<6))    {
115                         if (opc & (1<<3)) adrreg = true;                /* M_S*X */
116                         val = (opc >> 9) & 0x0007;
117                 } else {
118                         vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
119                 }
120         }
121
122         /* val is now register number, adreg == true if it is an address regsiter */
123         regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
124         type   = regval;
125
126         /* Handle the type. */
127
128         p = trap_handle(type, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
129
130         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
131         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
132         _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
133 }
134
135
136 /* md_signal_handler_sigill *******************************************
137  *
138  * This handler is used to generate hardware exceptions.
139  * Type of exception derived from trap number.
140  * If an object is needed a tst instruction (2 byte long) has
141  * been created directly before the trap instruction (2 bytes long).
142  * the last 3 bit of this tst instruction contain the register number.
143  **********************************************************************/
144
145 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
146 {
147         uint32_t        xpc, sp, ra, pv;
148         uint16_t        opc;
149         uint32_t        type;
150         uint32_t        regval;
151         void        *p;
152         actual_mcontext_t       *_mc;
153         actual_ucontext_t       *_uc;
154
155         _uc = (actual_ucontext_t*)_p;
156         xpc = (uint32_t)siginfo->si_addr;
157
158         pv = NULL;      /* we have no pv */
159         ra = xpc;       /* that is ture for most cases */
160
161         if (siginfo->si_code == ILL_ILLOPC)     {
162                 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
163         }
164         if (siginfo->si_code != ILL_ILLTRP)     {
165                 vm_abort("md_signal_handler_sigill: Caught something not a trap");
166         }
167
168         opc = *(uint16_t*)(xpc-2);
169
170         assert( (opc&0xfff0) == 0x4e40 );
171         type = opc & 0x000f;            /* filter trap number */
172
173         _mc = &_uc->uc_mcontext;
174         sp = _mc->gregs[R_SP];
175
176         /* Figure out in which register the object causing the exception resides for appropiate exceptions
177          */
178         switch (type)   {
179                 case TRAP_NullPointerException:
180                 case TRAP_ArithmeticException:
181                 case TRAP_CHECK_EXCEPTION:
182                         /* nothing */
183                         break;
184
185                 case TRAP_ClassCastException: 
186                         regval = *(uint16_t*)(xpc-4);
187                         assert( (regval&0xfff0) == 0x4a00 );
188                         /* was in a address register */
189                         regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
190                         break;
191
192                 case TRAP_ArrayIndexOutOfBoundsException:
193                         regval = *(uint16_t*)(xpc-4);
194                         assert( (regval&0xfff0) == 0x4a00 );
195                         /* was a data register */
196                         regval = _mc->gregs[regval & 0x7];
197                         break;
198
199                 case TRAP_COMPILER:
200                         regval = *(uint16_t*)(xpc-4);
201                         assert( (regval&0xfff0) == 0x4a00 );
202                         /* was in a address register */
203                         regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
204
205                         pv = xpc-4;     /* the compiler stub consists of 2 instructions */
206                         ra = md_stacktrace_get_returnaddress(sp, 0);
207                         sp = sp + SIZEOF_VOID_P;
208                         xpc = ra - 2;
209                         break;
210
211                 case TRAP_PATCHER:
212                         xpc -= 2;
213                         ra = xpc;
214                         break;
215         }
216
217         /* Handle the trap. */
218
219         p = trap_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
220
221         switch (type)   {
222         case TRAP_COMPILER:
223                 if (p == NULL) {
224                         /* exception when compiling the method */
225                         java_object_t *o = builtin_retrieve_exception();
226
227                         _mc->gregs[R_SP] = sp;  /* remove RA from stack */
228
229                         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (uintptr_t) o;
230                         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
231                         _mc->gregs[R_PC]                             = (uintptr_t) asm_handle_exception;
232                 }
233                 else {
234                         /* compilation ok, execute */
235                         _mc->gregs[R_PC] = p;
236                 }
237                 break;
238
239         case TRAP_PATCHER:
240                 if (p == NULL) { 
241                         /* No exception while patching, continue. */
242                         _mc->gregs[R_PC] = xpc; 
243                         return; 
244                 }
245                 /* fall-through in case of exception */
246
247         default:
248                 /* a normal exception with normal expcetion handling */
249                 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (uintptr_t) p;
250                 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
251                 _mc->gregs[R_PC]                             = (uintptr_t) asm_handle_exception;
252         }
253 }
254
255
256 /*
257  * These are local overrides for various environment variables in Emacs.
258  * Please do not remove this and leave it at the end of the file, where
259  * Emacs will automagically detect them.
260  * ---------------------------------------------------------------------
261  * Local variables:
262  * mode: c
263  * indent-tabs-mode: t
264  * c-basic-offset: 4
265  * tab-width: 4
266  * End:
267  * vim:noexpandtab:sw=4:ts=4:
268  */