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