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.h"
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.h"
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 u1* 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. */
165 void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
167 if (type == TRAP_COMPILER) {
169 _sc->arm_ip = (uintptr_t) p; // set REG_PV correctly
172 /* Get and set the PV from the parent Java method. */
174 pv = md_codegen_get_pv_from_pc(ra);
176 _sc->arm_ip = (uintptr_t) pv;
182 /* md_signal_handler_sigusr1 ***************************************************
184 Signal handler for suspending threads.
186 *******************************************************************************/
188 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
189 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
196 _uc = (ucontext_t *) _p;
197 _sc = &_uc->uc_mcontext;
199 /* get the PC and SP for this thread */
200 pc = (u1 *) _sc->arm_pc;
201 sp = (u1 *) _sc->arm_sp;
203 /* now suspend the current thread */
204 threads_suspend_ack(pc, sp);
209 /* md_signal_handler_sigusr2 ***************************************************
211 Signal handler for profiling sampling.
213 *******************************************************************************/
215 #if defined(ENABLE_THREADS)
216 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
218 threadobject *thread;
223 thread = THREADOBJECT;
225 _uc = (ucontext_t*) _p;
226 _sc = &_uc->uc_mcontext;
228 pc = (u1 *) _sc->arm_pc;
236 * Read the given context into an executionstate.
238 * @param es execution state
239 * @param context machine context
241 void md_executionstate_read(executionstate_t *es, void *context)
247 _uc = (ucontext_t *) context;
248 _sc = &_uc->uc_mcontext;
250 /* ATTENTION: glibc included messed up kernel headers we needed a
251 workaround for the ucontext structure. */
253 /* read special registers */
255 es->pc = (u1 *) _sc->arm_pc;
256 es->sp = (u1 *) _sc->arm_sp;
257 es->pv = (u1 *) _sc->arm_ip;
258 es->ra = (u1 *) _sc->arm_lr;
260 /* read integer registers */
262 for (i = 0; i < INT_REG_CNT; i++)
263 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
265 /* read float registers */
267 for (i = 0; i < FLT_REG_CNT; i++)
268 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
273 * Write the given executionstate back to the context.
275 * @param es execution state
276 * @param context machine context
278 void md_executionstate_write(executionstate_t *es, void *context)
284 _uc = (ucontext_t *) context;
285 _sc = &_uc->uc_mcontext;
287 /* ATTENTION: glibc included messed up kernel headers we needed a
288 workaround for the ucontext structure. */
290 /* write integer registers */
292 for (i = 0; i < INT_REG_CNT; i++)
293 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
295 /* write special registers */
297 _sc->arm_pc = (ptrint) es->pc;
298 _sc->arm_sp = (ptrint) es->sp;
299 _sc->arm_ip = (ptrint) es->pv;
300 _sc->arm_lr = (ptrint) es->ra;
305 * These are local overrides for various environment variables in Emacs.
306 * Please do not remove this and leave it at the end of the file, where
307 * Emacs will automagically detect them.
308 * ---------------------------------------------------------------------
311 * indent-tabs-mode: t