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/signallocal.hpp"
34 #include "vm/jit/asmpart.h"
35 #include "vm/jit/trap.hpp"
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 **********************************************************************/
77 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
81 uint32_t val, regval, off;
84 actual_mcontext_t *_mc;
85 actual_ucontext_t *_uc;
88 _uc = (actual_ucontext_t*)_p;
89 _mc = &_uc->uc_mcontext;
90 sp = _mc->gregs[R_SP];
91 xpc = _mc->gregs[R_PC];
92 opc = *(uint16_t*)xpc;
94 /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
95 /* the first two are 2*32bit sized */
98 if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3))) {
99 if (opc & (1<<6)) adrreg = true; /* M_XLD */
101 off = *(uint16_t*)(xpc+1);
102 } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6))) {
103 if (opc & (1<<3)) adrreg = true; /* M_XST */
104 val = (opc >> 9) & 0x0007;
105 off = *(uint16_t*)(xpc+1);
108 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
110 /* now check the 32 bit sized instructions */
111 if ((opc & (2<<3)) == (2<<3)) {
112 if (opc & (1<<6)) adrreg = true; /* M_L*X */
114 } else if ((opc & (2<<6)) == (2<<6)) {
115 if (opc & (1<<3)) adrreg = true; /* M_S*X */
116 val = (opc >> 9) & 0x0007;
118 vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
122 /* val is now register number, adreg == true if it is an address regsiter */
123 regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
126 /* Handle the type. */
128 p = trap_handle(type, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
130 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
131 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
132 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
136 /* md_signal_handler_sigill *******************************************
138 * This handler is used to generate hardware exceptions.
139 * Type of exception derived from trap number.
140 * If an object is needed a tst instruction (2 byte long) has
141 * been created directly before the trap instruction (2 bytes long).
142 * the last 3 bit of this tst instruction contain the register number.
143 **********************************************************************/
145 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
147 uint32_t xpc, sp, ra, pv;
152 actual_mcontext_t *_mc;
153 actual_ucontext_t *_uc;
155 _uc = (actual_ucontext_t*)_p;
156 xpc = (uint32_t)siginfo->si_addr;
158 pv = NULL; /* we have no pv */
159 ra = xpc; /* that is ture for most cases */
161 if (siginfo->si_code == ILL_ILLOPC) {
162 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
164 if (siginfo->si_code != ILL_ILLTRP) {
165 vm_abort("md_signal_handler_sigill: Caught something not a trap");
168 opc = *(uint16_t*)(xpc-2);
170 assert( (opc&0xfff0) == 0x4e40 );
171 type = opc & 0x000f; /* filter trap number */
173 _mc = &_uc->uc_mcontext;
174 sp = _mc->gregs[R_SP];
176 /* Figure out in which register the object causing the exception resides for appropiate exceptions
179 case TRAP_NullPointerException:
180 case TRAP_ArithmeticException:
181 case TRAP_CHECK_EXCEPTION:
185 case TRAP_ClassCastException:
186 regval = *(uint16_t*)(xpc-4);
187 assert( (regval&0xfff0) == 0x4a00 );
188 /* was in a address register */
189 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
192 case TRAP_ArrayIndexOutOfBoundsException:
193 regval = *(uint16_t*)(xpc-4);
194 assert( (regval&0xfff0) == 0x4a00 );
195 /* was a data register */
196 regval = _mc->gregs[regval & 0x7];
200 regval = *(uint16_t*)(xpc-4);
201 assert( (regval&0xfff0) == 0x4a00 );
202 /* was in a address register */
203 regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
205 pv = xpc-4; /* the compiler stub consists of 2 instructions */
206 ra = md_stacktrace_get_returnaddress(sp, 0);
207 sp = sp + SIZEOF_VOID_P;
217 /* Handle the trap. */
219 p = trap_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
224 /* exception when compiling the method */
225 java_object_t *o = builtin_retrieve_exception();
227 _mc->gregs[R_SP] = sp; /* remove RA from stack */
229 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (uintptr_t) o;
230 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
231 _mc->gregs[R_PC] = (uintptr_t) asm_handle_exception;
234 /* compilation ok, execute */
235 _mc->gregs[R_PC] = p;
241 /* No exception while patching, continue. */
242 _mc->gregs[R_PC] = xpc;
245 /* fall-through in case of exception */
248 /* a normal exception with normal expcetion handling */
249 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (uintptr_t) p;
250 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
251 _mc->gregs[R_PC] = (uintptr_t) asm_handle_exception;
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 * ---------------------------------------------------------------------
263 * indent-tabs-mode: t
267 * vim:noexpandtab:sw=4:ts=4: