(codegen_emit_stub_builtin): Removed.
[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  *      linux specific initializations
72  */
73 void md_init_linux()
74 {
75         struct sigaction act;
76         
77         act.sa_sigaction = md_signal_handler_sigill;
78         act.sa_flags     = SA_NODEFER | SA_SIGINFO;
79
80         if (sigaction(SIGILL, &act, NULL) == -1)        {
81                 vm_abort("md_linux_init: Error registering signal handler");
82         }
83 }
84
85 /* md_signal_handler_sigsegv ******************************************
86  *
87  * Invoked when a Nullpointerexception occured, or when the vm 
88  * crashes, hard to tell the difference. 
89  **********************************************************************/
90 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
91 {       
92         uint32_t        xpc, sp;
93         uint16_t        opc;
94         uint32_t        val, regval, off;
95         bool            adrreg;
96         void        *p;
97         actual_mcontext_t       *_mc;
98         actual_ucontext_t       *_uc;
99
100
101         _uc = (actual_ucontext_t*)_p;
102         _mc = &_uc->uc_mcontext;
103         sp = _mc->gregs[R_SP];
104         xpc = _mc->gregs[R_PC];
105         opc = *(uint16_t*)xpc;
106
107         /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
108         /* the first two are 2*32bit sized */
109         adrreg = false;
110         off = 0;
111         if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3)))   {
112                 if (opc & (1<<6)) adrreg = true;                /* M_XLD */
113                 val = opc & 0x0007;
114                 off = *(uint16_t*)(xpc+1);
115         } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6)))    {
116                 if (opc & (1<<3)) adrreg = true;                /* M_XST */
117                 val = (opc >> 9) & 0x0007;
118                 off = *(uint16_t*)(xpc+1);
119         } else {
120                 
121                 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
122                 */
123                 /* now check the 32 bit sized instructions */
124                 if ((opc & (2<<3)) == (2<<3))   {
125                         if (opc & (1<<6)) adrreg = true;                /* M_L*X */
126                         val = opc & 0x0007;
127                 } else if ((opc & (2<<6)) == (2<<6))    {
128                         if (opc & (1<<3)) adrreg = true;                /* M_S*X */
129                         val = (opc >> 9) & 0x0007;
130                 } else {
131                         vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
132                 }
133         }
134
135         /* val is now register number, adreg == true if it is an address regsiter */
136         regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
137         /*
138         if (regval != 0)        {
139                 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", regval);
140         }*/
141
142
143         /*fprintf(stderr, "SEGV: sp=%x, xpc=%x, regval=%x\n", sp, xpc, regval);
144         */
145
146         /* Handle the type. */
147
148         p = signal_handle(EXCEPTION_HARDWARE_NULLPOINTER, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
149
150         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
151         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
152         _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
153 }
154
155 /* md_signal_handler_sigill *******************************************
156  *
157  * This handler is used to generate hardware exceptions.
158  * Type of exception derived from trap number.
159  * If an object is needed a tst instruction (2 byte long) has
160  * been created directly before the trap instruction (2 bytes long).
161  * the last 3 bit of this tst instruction contain the register number.
162  **********************************************************************/
163 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
164 {
165         uint32_t        xpc, sp;
166         uint16_t        opc;
167         uint32_t        type;
168         uint32_t        val, regval;
169         void        *p;
170         actual_mcontext_t       *_mc;
171         actual_ucontext_t       *_uc;
172
173         _uc = (actual_ucontext_t*)_p;
174         xpc = (uint32_t)siginfo->si_addr;
175
176         if (siginfo->si_code == ILL_ILLOPC)     {
177                 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
178         }
179         if (siginfo->si_code != ILL_ILLTRP)     {
180                 vm_abort("md_signal_handler_sigill: Caught something not a trap");
181         }
182
183         opc = *(uint16_t*)(xpc-2);
184
185         assert( (opc&0xfff0) == 0x4e40 );
186         type = opc & 0x000f;            /* filter trap number */
187
188         _mc = &_uc->uc_mcontext;
189         sp = _mc->gregs[R_SP];
190
191         /* Figure out in which register the object causing the exception resides for appropiate exceptions
192          */
193         switch (type)   {
194                 case EXCEPTION_HARDWARE_ARITHMETIC:
195                 case EXCEPTION_HARDWARE_EXCEPTION:
196                         /* nothing */
197                         break;
198                 case EXCEPTION_HARDWARE_CLASSCAST: 
199                         regval = *(uint16_t*)(xpc-4);
200                         assert( (regval&0xfff0) == 0x4a00 );
201                         /* was in a address register */
202                         regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
203                         break;
204                 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
205                         regval = *(uint16_t*)(xpc-4);
206                         assert( (regval&0xfff0) == 0x4a00 );
207                         /* was a data register */
208                         regval = _mc->gregs[regval & 0x7];
209                         break;
210                 case M68K_EXCEPTION_HARDWARE_NULLPOINTER:
211                         type = EXCEPTION_HARDWARE_NULLPOINTER;
212                         break;
213
214                 default: assert(0);
215         }
216
217         /*fprintf(stderr, "NEW HWE: sp=%x, xpc=%x, tpye=%x, regval=%x\n", sp, xpc, type, regval);
218         */
219
220         /* Handle the type. */
221
222         p = signal_handle(type, val, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
223
224         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
225         _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
226         _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
227 }
228
229 /* md_signal_handler_sigusr1 ***************************************************
230
231    Signal handler for suspending threads.
232
233 *******************************************************************************/
234
235 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
236 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
237 {
238         ucontext_t *_uc;
239         mcontext_t *_mc;
240         u1         *pc;
241         u1         *sp;
242
243         _uc = (ucontext_t *) _p;
244         _mc = &_uc->uc_mcontext;
245
246         /* get the PC and SP for this thread */
247         pc = (u1 *) _mc->gregs[R_PC];
248         sp = (u1 *) _mc->gregs[R_SP];
249
250         /* now suspend the current thread */
251         threads_suspend_ack(pc, sp);
252 }
253 #endif
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  */