1 /* src/vm/jit/m68k/linux/md-os.c - m68k linux specific functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
28 #include "vm/jit/m68k/md.h"
29 #include "vm/jit/m68k/linux/md-abi.h"
31 #include "vm/exceptions.h"
32 #include "vm/signallocal.h"
35 #include "vm/jit/asmpart.h"
36 #include "vm/jit/trap.h"
40 /*#include <signal.h>*/
42 #include <sys/ucontext.h> /* has br0ken ucontext_t*/
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:
48 typedef struct actual_fpregset {
53 typedef struct actual_mcontext {
55 gregset_t gregs; /* 0...7 = %d0-%d7, 8...15 = %a0-%a7 */
56 actual_fpregset_t fpregs;
59 #define GREGS_ADRREG_OFF 8
61 typedef struct actual_ucontext {
62 unsigned long uc_flags;
63 struct actual_ucontext *uc_link;
65 struct actual_mcontext uc_mcontext;
66 unsigned long uc_filler[80];
67 sigset_t uc_sigmask; /* mask last for extensibility */
72 /* md_signal_handler_sigsegv ******************************************
74 * Invoked when a Nullpointerexception occured, or when the vm
75 * crashes, hard to tell the difference.
76 **********************************************************************/
78 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
82 uint32_t val, regval, off;
85 actual_mcontext_t *_mc;
86 actual_ucontext_t *_uc;
89 _uc = (actual_ucontext_t*)_p;
90 _mc = &_uc->uc_mcontext;
91 sp = _mc->gregs[R_SP];
92 xpc = _mc->gregs[R_PC];
93 opc = *(uint16_t*)xpc;
95 /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
96 /* the first two are 2*32bit sized */
99 if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3))) {
100 if (opc & (1<<6)) adrreg = true; /* M_XLD */
102 off = *(uint16_t*)(xpc+1);
103 } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6))) {
104 if (opc & (1<<3)) adrreg = true; /* M_XST */
105 val = (opc >> 9) & 0x0007;
106 off = *(uint16_t*)(xpc+1);
109 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
111 /* now check the 32 bit sized instructions */
112 if ((opc & (2<<3)) == (2<<3)) {
113 if (opc & (1<<6)) adrreg = true; /* M_L*X */
115 } else if ((opc & (2<<6)) == (2<<6)) {
116 if (opc & (1<<3)) adrreg = true; /* M_S*X */
117 val = (opc >> 9) & 0x0007;
119 vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
123 /* val is now register number, adreg == true if it is an address regsiter */
124 regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
127 /* Handle the type. */
129 p = trap_handle(type, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
131 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
132 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
133 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
137 /* md_signal_handler_sigill *******************************************
139 * This handler is used to generate hardware exceptions.
140 * Type of exception derived from trap number.
141 * If an object is needed a tst instruction (2 byte long) has
142 * been created directly before the trap instruction (2 bytes long).
143 * the last 3 bit of this tst instruction contain the register number.
144 **********************************************************************/
146 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
148 uint32_t xpc, sp, ra, pv;
153 actual_mcontext_t *_mc;
154 actual_ucontext_t *_uc;
156 _uc = (actual_ucontext_t*)_p;
157 xpc = (uint32_t)siginfo->si_addr;
159 pv = NULL; /* we have no pv */
160 ra = xpc; /* that is ture for most cases */
162 if (siginfo->si_code == ILL_ILLOPC) {
163 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
165 if (siginfo->si_code != ILL_ILLTRP) {
166 vm_abort("md_signal_handler_sigill: Caught something not a trap");
169 opc = *(uint16_t*)(xpc-2);
171 assert( (opc&0xfff0) == 0x4e40 );
172 type = opc & 0x000f; /* filter trap number */
174 _mc = &_uc->uc_mcontext;
175 sp = _mc->gregs[R_SP];
177 /* Figure out in which register the object causing the exception resides for appropiate exceptions
180 case TRAP_NullPointerException:
181 case TRAP_ArithmeticException:
182 case TRAP_CHECK_EXCEPTION:
186 case TRAP_ClassCastException:
187 regval = *(uint16_t*)(xpc-4);
188 assert( (regval&0xfff0) == 0x4a00 );
189 /* was in a address register */
190 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
193 case TRAP_ArrayIndexOutOfBoundsException:
194 regval = *(uint16_t*)(xpc-4);
195 assert( (regval&0xfff0) == 0x4a00 );
196 /* was a data register */
197 regval = _mc->gregs[regval & 0x7];
201 regval = *(uint16_t*)(xpc-4);
202 assert( (regval&0xfff0) == 0x4a00 );
203 /* was in a address register */
204 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
206 pv = xpc-4; /* the compiler stub consists of 2 instructions */
207 ra = md_stacktrace_get_returnaddress(sp, 0);
208 sp = sp + SIZEOF_VOID_P;
218 /* Handle the trap. */
220 p = trap_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
225 /* exception when compiling the method */
226 java_object_t *o = builtin_retrieve_exception();
228 _mc->gregs[R_SP] = sp; /* remove RA from stack */
230 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (uintptr_t) o;
231 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
232 _mc->gregs[R_PC] = (uintptr_t) asm_handle_exception;
235 /* compilation ok, execute */
236 _mc->gregs[R_PC] = p;
242 /* No exception while patching, continue. */
243 _mc->gregs[R_PC] = xpc;
246 /* fall-through in case of exception */
249 /* a normal exception with normal expcetion handling */
250 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (uintptr_t) p;
251 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
252 _mc->gregs[R_PC] = (uintptr_t) asm_handle_exception;
257 /* md_signal_handler_sigusr1 ***************************************************
259 Signal handler for suspending threads.
261 *******************************************************************************/
263 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
264 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
271 _uc = (ucontext_t *) _p;
272 _mc = &_uc->uc_mcontext;
274 /* get the PC and SP for this thread */
275 pc = (u1 *) _mc->gregs[R_PC];
276 sp = (u1 *) _mc->gregs[R_SP];
278 /* now suspend the current thread */
279 threads_suspend_ack(pc, sp);
285 * These are local overrides for various environment variables in Emacs.
286 * Please do not remove this and leave it at the end of the file, where
287 * Emacs will automagically detect them.
288 * ---------------------------------------------------------------------
291 * indent-tabs-mode: t
295 * vim:noexpandtab:sw=4:ts=4: