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"
40 #include <sys/ucontext.h> /* has br0ken ucontext_t*/
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:
46 typedef struct actual_fpregset {
51 typedef struct actual_mcontext {
53 gregset_t gregs; /* 0...7 = %d0-%d7, 8...15 = %a0-%a7 */
54 actual_fpregset_t fpregs;
57 #define GREGS_ADRREG_OFF 8
59 typedef struct actual_ucontext {
60 unsigned long uc_flags;
61 struct actual_ucontext *uc_link;
63 struct actual_mcontext uc_mcontext;
64 unsigned long uc_filler[80];
65 sigset_t uc_sigmask; /* mask last for extensibility */
70 * linux specific initializations
76 act.sa_sigaction = md_signal_handler_sigill;
77 act.sa_flags = SA_NODEFER | SA_SIGINFO;
79 if (sigaction(SIGILL, &act, NULL) == -1) {
80 vm_abort("md_linux_init: Error registering signal handler");
84 /* md_signal_handler_sigsegv ******************************************
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)
94 uint32_t val, regval, off;
99 _mc = &_uc->uc_mcontext;
100 sp = _mc->gregs[R_SP];
101 xpc = _mc->gregs[R_PC];
102 opc = *(uint16_t*)xpc;
104 /* m68k uses a whole bunch of difficult to differ load instructions where null pointer check could occure */
105 /* the first two are 2*32bit sized */
108 if ((opc & ((2<<12) | (5<<3))) == ((2<<12) | (5<<3))) {
109 if (opc & (1<<6)) adrreg = true; /* M_XLD */
111 off = *(uint16_t*)(xpc+1);
112 } else if ((opc & ((2<<12) | (5<<6))) == ((2<<12) | (5<<6))) {
113 if (opc & (1<<3)) adrreg = true; /* M_XST */
114 val = (opc >> 9) & 0x0007;
115 off = *(uint16_t*)(xpc+1);
118 /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
120 /* now check the 32 bit sized instructions */
121 if ((opc & (2<<3)) == (2<<3)) {
122 if (opc & (1<<6)) adrreg = true; /* M_L*X */
124 } else if ((opc & (2<<6)) == (2<<6)) {
125 if (opc & (1<<3)) adrreg = true; /* M_S*X */
126 val = (opc >> 9) & 0x0007;
128 vm_abort("md_signal_handler_sigsegv: unhandeled faulting opcode %x", opc);
132 /* val is now register number, adreg == true if it is an address regsiter */
133 regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
136 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", regval);
140 /*fprintf(stderr, "SEGV: sp=%x, xpc=%x, regval=%x\n", sp, xpc, regval);
143 /* create stackframeinfo */
145 stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, xpc, xpc);
147 /* Handle the type. */
149 p = signal_handle(xpc, EXCEPTION_HARDWARE_NULLPOINTER, regval);
151 /* remove stackframeinfo */
153 stacktrace_remove_stackframeinfo(&sfi);
155 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
156 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
157 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
160 /* md_signal_handler_sigill *******************************************
162 * This handler is used to generate hardware exceptions.
163 * Type of exception derived from trap number.
164 * If an object is needed a tst instruction (2 byte long) has
165 * been created directly before the trap instruction (2 bytes long).
166 * the last 3 bit of this tst instruction contain the register number.
167 **********************************************************************/
168 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, actual_ucontext_t *_uc)
174 uint32_t val, regval;
178 xpc = siginfo->si_addr;
180 if (siginfo->si_code == ILL_ILLOPC) {
181 vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
183 if (siginfo->si_code != ILL_ILLTRP) {
184 vm_abort("md_signal_handler_sigill: Caught something not a trap");
187 opc = *(uint16_t*)(xpc-2);
189 assert( (opc&0xfff0) == 0x4e40 );
190 type = opc & 0x000f; /* filter trap number */
192 _mc = &_uc->uc_mcontext;
193 sp = _mc->gregs[R_SP];
195 /* Figure out in which register the object causing the exception resides for appropiate exceptions
198 case EXCEPTION_HARDWARE_ARITHMETIC:
199 case EXCEPTION_HARDWARE_EXCEPTION:
202 case EXCEPTION_HARDWARE_CLASSCAST:
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 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
209 regval = *(uint16_t*)(xpc-4);
210 assert( (regval&0xfff0) == 0x4a00 );
211 /* was a data register */
212 regval = _mc->gregs[regval & 0x7];
214 case M68K_EXCEPTION_HARDWARE_NULLPOINTER:
215 type = EXCEPTION_HARDWARE_NULLPOINTER;
221 /*fprintf(stderr, "NEW HWE: sp=%x, xpc=%x, tpye=%x, regval=%x\n", sp, xpc, type, regval);
224 /* create stackframeinfo */
226 stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, xpc, xpc);
228 /* Handle the type. */
230 p = signal_handle(xpc, type, val);
232 /* remove stackframeinfo */
234 stacktrace_remove_stackframeinfo(&sfi);
236 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1] = (intptr_t) p;
237 _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
238 _mc->gregs[R_PC] = (intptr_t) asm_handle_exception;
241 /* md_signal_handler_sigusr1 ***************************************************
243 Signal handler for suspending threads.
245 *******************************************************************************/
247 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
248 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
255 _uc = (ucontext_t *) _p;
256 _mc = &_uc->uc_mcontext;
258 /* get the PC and SP for this thread */
259 pc = (u1 *) _mc->gregs[R_PC];
260 sp = (u1 *) _mc->gregs[R_SP];
262 /* now suspend the current thread */
263 threads_suspend_ack(pc, sp);
269 * These are local overrides for various environment variables in Emacs.
270 * Please do not remove this and leave it at the end of the file, where
271 * Emacs will automagically detect them.
272 * ---------------------------------------------------------------------
275 * indent-tabs-mode: t
279 * vim:noexpandtab:sw=4:ts=4: