1 /* src/vm/jit/m68k/linux/md-os.c - linux specific functions
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
8 This file is part of CACAO.
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.
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.
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
33 #include "vm/exceptions.h"
34 #include "vm/jit/asmpart.h"
35 #include "vm/signallocal.h"
39 /*#include <signal.h>*/
41 #include <sys/ucontext.h> /* has br0ken ucontext_t*/
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:
47 typedef struct actual_fpregset {
52 typedef struct actual_mcontext {
54 gregset_t gregs; /* 0...7 = %d0-%d7, 8...15 = %a0-%a7 */
55 actual_fpregset_t fpregs;
58 #define GREGS_ADRREG_OFF 8
60 typedef struct actual_ucontext {
61 unsigned long uc_flags;
62 struct actual_ucontext *uc_link;
64 struct actual_mcontext uc_mcontext;
65 unsigned long uc_filler[80];
66 sigset_t uc_sigmask; /* mask last for extensibility */
71 /* md_signal_handler_sigsegv ******************************************
73 * Invoked when a Nullpointerexception occured, or when the vm
74 * crashes, hard to tell the difference.
75 **********************************************************************/
76 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
80 uint32_t val, regval, off;
83 actual_mcontext_t *_mc;
84 actual_ucontext_t *_uc;
87 _uc = (actual_ucontext_t*)_p;
88 _mc = &_uc->uc_mcontext;
89 sp = _mc->gregs[R_SP];
90 xpc = _mc->gregs[R_PC];
91 opc = *(uint16_t*)xpc;
93 /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
94 /* the first two are 2*32bit sized */
97 if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3))) {
98 if (opc & (1<<6)) adrreg = true; /* M_XLD */
100 off = *(uint16_t*)(xpc+1);
101 } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6))) {
102 if (opc & (1<<3)) adrreg = true; /* M_XST */
103 val = (opc >> 9) & 0x0007;
104 off = *(uint16_t*)(xpc+1);
107 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
109 /* now check the 32 bit sized instructions */
110 if ((opc & (2<<3)) == (2<<3)) {
111 if (opc & (1<<6)) adrreg = true; /* M_L*X */
113 } else if ((opc & (2<<6)) == (2<<6)) {
114 if (opc & (1<<3)) adrreg = true; /* M_S*X */
115 val = (opc >> 9) & 0x0007;
117 vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
121 /* val is now register number, adreg == true if it is an address regsiter */
122 regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
125 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", regval);
129 /*fprintf(stderr, "SEGV: sp=%x, xpc=%x, regval=%x\n", sp, xpc, regval);
132 /* Handle the type. */
134 p = signal_handle(EXCEPTION_HARDWARE_NULLPOINTER, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
136 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
137 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
138 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
141 /* md_signal_handler_sigill *******************************************
143 * This handler is used to generate hardware exceptions.
144 * Type of exception derived from trap number.
145 * If an object is needed a tst instruction (2 byte long) has
146 * been created directly before the trap instruction (2 bytes long).
147 * the last 3 bit of this tst instruction contain the register number.
148 **********************************************************************/
149 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
151 uint32_t xpc, sp, ra, pv;
156 actual_mcontext_t *_mc;
157 actual_ucontext_t *_uc;
159 _uc = (actual_ucontext_t*)_p;
160 xpc = (uint32_t)siginfo->si_addr;
162 pv = NULL; /* we have no pv */
163 ra = xpc; /* that is ture for most cases */
165 if (siginfo->si_code == ILL_ILLOPC) {
166 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
168 if (siginfo->si_code != ILL_ILLTRP) {
169 vm_abort("md_signal_handler_sigill: Caught something not a trap");
172 opc = *(uint16_t*)(xpc-2);
174 assert( (opc&0xfff0) == 0x4e40 );
175 type = opc & 0x000f; /* filter trap number */
177 _mc = &_uc->uc_mcontext;
178 sp = _mc->gregs[R_SP];
180 /* Figure out in which register the object causing the exception resides for appropiate exceptions
183 case EXCEPTION_HARDWARE_ARITHMETIC:
184 case EXCEPTION_HARDWARE_EXCEPTION:
187 case EXCEPTION_HARDWARE_CLASSCAST:
188 regval = *(uint16_t*)(xpc-4);
189 assert( (regval&0xfff0) == 0x4a00 );
190 /* was in a address register */
191 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
193 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
194 regval = *(uint16_t*)(xpc-4);
195 assert( (regval&0xfff0) == 0x4a00 );
196 /* was a data register */
197 regval = _mc->gregs[regval & 0x7];
199 case M68K_EXCEPTION_HARDWARE_NULLPOINTER:
200 type = EXCEPTION_HARDWARE_NULLPOINTER;
202 case EXCEPTION_HARDWARE_COMPILER:
203 regval = *(uint16_t*)(xpc-4);
204 assert( (regval&0xfff0) == 0x4a00 );
205 /* was in a address register */
206 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
208 pv = xpc-4; /* the compiler stub consists of 2 instructions */
209 ra = md_stacktrace_get_returnaddress(sp, 0);
210 sp = sp + SIZEOF_VOID_P;
213 case EXCEPTION_HARDWARE_PATCHER:
221 /*fprintf(stderr, "NEW HWE: sp=%x, xpc=%x, tpye=%x, regval=%x\n", sp, xpc, type, regval);
224 /* Handle the type. */
225 p = signal_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
229 case EXCEPTION_HARDWARE_COMPILER:
231 /* exception when compiling the method */
232 java_object_t *o = exceptions_get_and_clear_exceptions();
234 _mc->gregs[R_SP] = sp; /* remove RA from stack */
236 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) o;
237 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
238 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
241 /* compilation ok, execute */
242 _mc->gregs[R_PC] = p;
246 case EXCEPTION_HARDWARE_PATCHER:
248 /* no expcetion while patching, continue */
249 _mc->gregs[R_PC] = xpc;
252 /* fall-through in case of exception */
254 /* a normal exception with normal expcetion handling */
255 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
256 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
257 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
261 /* md_signal_handler_sigusr1 ***************************************************
263 Signal handler for suspending threads.
265 *******************************************************************************/
267 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
268 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
275 _uc = (ucontext_t *) _p;
276 _mc = &_uc->uc_mcontext;
278 /* get the PC and SP for this thread */
279 pc = (u1 *) _mc->gregs[R_PC];
280 sp = (u1 *) _mc->gregs[R_SP];
282 /* now suspend the current thread */
283 threads_suspend_ack(pc, sp);
289 * These are local overrides for various environment variables in Emacs.
290 * Please do not remove this and leave it at the end of the file, where
291 * Emacs will automagically detect them.
292 * ---------------------------------------------------------------------
295 * indent-tabs-mode: t
299 * vim:noexpandtab:sw=4:ts=4: