1 /* src/vm/jit/arm/linux/md-os.c - machine dependent ARM Linux functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #define ucontext broken_glibc_ucontext
32 #define ucontext_t broken_glibc_ucontext_t
37 typedef struct ucontext {
38 unsigned long uc_flags;
39 struct ucontext *uc_link;
41 struct sigcontext uc_mcontext;
45 #define scontext_t struct sigcontext
49 #include "vm/jit/arm/md.h"
50 #include "vm/jit/arm/md-abi.h"
52 #include "threads/thread.hpp"
55 #include "vm/signallocal.hpp"
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/disass.h"
60 #include "vm/jit/executionstate.h"
61 #include "vm/jit/patcher-common.hpp"
62 #include "vm/jit/trap.hpp"
65 /* md_signal_handler_sigsegv ***************************************************
67 Signal handler for hardware exceptions.
69 *******************************************************************************/
71 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
84 _uc = (ucontext_t*) _p;
85 _sc = &_uc->uc_mcontext;
87 /* ATTENTION: glibc included messed up kernel headers we needed a
88 workaround for the ucontext structure. */
90 pv = (u1 *) _sc->arm_ip;
91 sp = (u1 *) _sc->arm_sp;
92 ra = (u1 *) _sc->arm_lr; /* this is correct for leafs */
93 xpc = (u1 *) _sc->arm_pc;
95 /* get exception-throwing instruction */
98 vm_abort("md_signal_handler_sigsegv: the program counter is NULL");
100 mcode = *((s4 *) xpc);
102 /* This is a NullPointerException. */
104 addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
108 /* Handle the trap. */
110 trap_handle(type, val, pv, sp, ra, xpc, _p);
114 /* md_signal_handler_sigill ****************************************************
116 Illegal Instruction signal handler for hardware exception checks.
118 *******************************************************************************/
120 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
122 ucontext_t* _uc = (ucontext_t*) _p;
123 scontext_t* _sc = &_uc->uc_mcontext;
125 /* ATTENTION: glibc included messed up kernel headers we needed a
126 workaround for the ucontext structure. */
128 void* pv = (void*) _sc->arm_ip;
129 void* sp = (void*) _sc->arm_sp;
130 void* ra = (void*) _sc->arm_lr; // The RA is correct for leaf methods.
131 void* xpc = (void*) _sc->arm_pc;
133 // Get the exception-throwing instruction.
134 uint32_t mcode = *((uint32_t*) xpc);
136 // Check if the trap instruction is valid.
137 // TODO Move this into patcher_handler.
138 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
139 // Check if the PC has been patched during our way to this
140 // signal handler (see PR85).
141 // NOTE: ARM uses SIGILL for other traps too, but it's OK to
142 // do this check anyway because it will fail.
143 if (patcher_is_patched_at(xpc) == true)
146 // We have a problem...
147 log_println("md_signal_handler_sigill: Unknown illegal instruction 0x%x at 0x%x", mcode, xpc);
148 #if defined(ENABLE_DISASSEMBLER)
149 (void) disassinstr(xpc);
151 vm_abort("Aborting...");
154 int type = (mcode >> 8) & 0x0fff;
155 intptr_t val = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
157 if (type == TRAP_COMPILER) {
158 /* The XPC is the RA minus 4, because the RA points to the
159 instruction after the call. */
161 xpc = (void*) (((uintptr_t) ra) - 4);
165 trap_handle(type, val, pv, sp, ra, xpc, _p);
169 /* md_signal_handler_sigusr1 ***************************************************
171 Signal handler for suspending threads.
173 *******************************************************************************/
175 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
176 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
183 _uc = (ucontext_t *) _p;
184 _sc = &_uc->uc_mcontext;
186 /* get the PC and SP for this thread */
187 pc = (u1 *) _sc->arm_pc;
188 sp = (u1 *) _sc->arm_sp;
190 /* now suspend the current thread */
191 threads_suspend_ack(pc, sp);
196 /* md_signal_handler_sigusr2 ***************************************************
198 Signal handler for profiling sampling.
200 *******************************************************************************/
202 #if defined(ENABLE_THREADS)
203 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
205 threadobject *thread;
210 thread = THREADOBJECT;
212 _uc = (ucontext_t*) _p;
213 _sc = &_uc->uc_mcontext;
215 pc = (u1 *) _sc->arm_pc;
223 * Read the given context into an executionstate.
225 * @param es execution state
226 * @param context machine context
228 void md_executionstate_read(executionstate_t *es, void *context)
234 _uc = (ucontext_t *) context;
235 _sc = &_uc->uc_mcontext;
237 /* ATTENTION: glibc included messed up kernel headers we needed a
238 workaround for the ucontext structure. */
240 /* read special registers */
242 es->pc = (u1 *) _sc->arm_pc;
243 es->sp = (u1 *) _sc->arm_sp;
244 es->pv = (u1 *) _sc->arm_ip;
245 es->ra = (u1 *) _sc->arm_lr;
247 /* read integer registers */
249 for (i = 0; i < INT_REG_CNT; i++)
250 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
252 /* read float registers */
254 for (i = 0; i < FLT_REG_CNT; i++)
255 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
260 * Write the given executionstate back to the context.
262 * @param es execution state
263 * @param context machine context
265 void md_executionstate_write(executionstate_t *es, void *context)
271 _uc = (ucontext_t *) context;
272 _sc = &_uc->uc_mcontext;
274 /* ATTENTION: glibc included messed up kernel headers we needed a
275 workaround for the ucontext structure. */
277 /* write integer registers */
279 for (i = 0; i < INT_REG_CNT; i++)
280 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
282 /* write special registers */
284 _sc->arm_pc = (ptrint) es->pc;
285 _sc->arm_sp = (ptrint) es->sp;
286 _sc->arm_ip = (ptrint) es->pv;
287 _sc->arm_lr = (ptrint) es->ra;
292 * These are local overrides for various environment variables in Emacs.
293 * Please do not remove this and leave it at the end of the file, where
294 * Emacs will automagically detect them.
295 * ---------------------------------------------------------------------
298 * indent-tabs-mode: t