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