1 /* src/vm/jit/i386/solaris/md-os.c - machine dependent i386 Solaris functions
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
33 #include "vm/jit/i386/codegen.h"
34 #include "vm/jit/i386/md.h"
36 #include "threads/thread.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/signallocal.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/executionstate.h"
43 #include "vm/jit/stacktrace.hpp"
44 #include "vm/jit/trap.h"
47 /* md_signal_handler_sigsegv ***************************************************
49 Signal handler for hardware exceptions.
51 *******************************************************************************/
53 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
71 _uc = (ucontext_t *) _p;
72 _mc = &_uc->uc_mcontext;
74 pv = NULL; /* is resolved during stackframeinfo creation */
75 sp = (u1 *) _mc->gregs[ESP];
76 xpc = (u1 *) _mc->gregs[EIP];
77 ra = xpc; /* return address is equal to XPC */
79 /* get exception-throwing instruction */
81 opc = M_ALD_MEM_GET_OPC(xpc);
82 mod = M_ALD_MEM_GET_MOD(xpc);
83 rm = M_ALD_MEM_GET_RM(xpc);
85 /* for values see emit_mov_mem_reg and emit_mem */
87 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
88 /* this was a hardware-exception */
90 d = M_ALD_MEM_GET_REG(xpc);
91 disp = M_ALD_MEM_GET_DISP(xpc);
93 /* we use the exception type as load displacement */
97 /* ATTENTION: The _mc->gregs layout is completely crazy! The
98 registers are reversed starting with number 4 for REG_EDI
99 (see /usr/include/sys/ucontext.h). We have to convert that
102 val = _mc->gregs[EAX - d];
104 if (type == TRAP_COMPILER) {
105 /* The PV from the compiler stub is equal to the XPC. */
109 /* We use a framesize of zero here because the call pushed
110 the return addres onto the stack. */
112 ra = md_stacktrace_get_returnaddress(sp, 0);
114 /* Skip the RA on the stack. */
116 sp = sp + 1 * SIZEOF_VOID_P;
118 /* The XPC is the RA minus 2, because the RA points to the
119 instruction after the call. */
125 /* this was a normal NPE */
127 type = TRAP_NullPointerException;
131 /* Handle the trap. */
133 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
137 if (type == TRAP_COMPILER) {
139 o = builtin_retrieve_exception();
141 _mc->gregs[ESP] = (uintptr_t) sp; /* Remove RA from stack. */
143 _mc->gregs[EAX] = (uintptr_t) o;
144 _mc->gregs[ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
145 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
148 _mc->gregs[EIP] = (uintptr_t) p;
152 _mc->gregs[EAX] = (uintptr_t) p;
153 _mc->gregs[ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
154 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
159 /* md_signal_handler_sigfpe ****************************************************
161 ArithmeticException signal handler for hardware divide by zero
164 *******************************************************************************/
166 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
178 _uc = (ucontext_t *) _p;
179 _mc = &_uc->uc_mcontext;
181 pv = NULL; /* is resolved during stackframeinfo creation */
182 sp = (u1 *) _mc->gregs[ESP];
183 xpc = (u1 *) _mc->gregs[EIP];
184 ra = xpc; /* return address is equal to xpc */
186 /* This is an ArithmeticException. */
188 type = TRAP_ArithmeticException;
191 /* Handle the trap. */
193 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
197 _mc->gregs[EAX] = (uintptr_t) p;
198 _mc->gregs[ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
199 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
203 /* md_signal_handler_sigill ****************************************************
205 Signal handler for hardware patcher traps (ud2).
207 *******************************************************************************/
209 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
221 _uc = (ucontext_t *) _p;
222 _mc = &_uc->uc_mcontext;
224 pv = NULL; /* is resolved during stackframeinfo creation */
225 sp = (u1 *) _mc->gregs[ESP];
226 xpc = (u1 *) _mc->gregs[EIP];
227 ra = xpc; /* return address is equal to xpc */
232 /* Handle the trap. */
234 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
239 _mc->gregs[EAX] = (uintptr_t) p;
240 _mc->gregs[ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
241 _mc->gregs[EIP] = (uintptr_t) asm_handle_exception;
246 /* md_signal_handler_sigusr1 ***************************************************
248 Signal handler for suspending threads.
250 *******************************************************************************/
252 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
253 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
260 _uc = (ucontext_t *) _p;
261 _mc = &_uc->uc_mcontext;
263 /* get the PC and SP for this thread */
264 pc = (u1 *) _mc->gregs[EIP];
265 sp = (u1 *) _mc->gregs[ESP];
267 /* now suspend the current thread */
268 threads_suspend_ack(pc, sp);
273 /* md_signal_handler_sigusr2 ***************************************************
275 Signal handler for profiling sampling.
277 *******************************************************************************/
279 #if defined(ENABLE_THREADS)
280 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
289 _uc = (ucontext_t *) _p;
290 _mc = &_uc->uc_mcontext;
292 pc = (u1 *) _mc->gregs[EIP];
299 /* md_executionstate_read ******************************************************
301 Read the given context into an executionstate for Replacement.
303 *******************************************************************************/
305 void md_executionstate_read(executionstate_t *es, void *context)
311 _uc = (ucontext_t *) context;
312 _mc = &_uc->uc_mcontext;
314 /* read special registers */
315 es->pc = (u1 *) _mc->gregs[EIP];
316 es->sp = (u1 *) _mc->gregs[ESP];
317 es->pv = NULL; /* pv must be looked up via AVL tree */
319 /* read integer registers */
320 for (i = 0; i < INT_REG_CNT; i++)
321 es->intregs[i] = _mc->gregs[EAX - i];
323 /* read float registers */
324 for (i = 0; i < FLT_REG_CNT; i++)
325 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
329 /* md_executionstate_write *****************************************************
331 Write the given executionstate back to the context for Replacement.
333 *******************************************************************************/
335 void md_executionstate_write(executionstate_t *es, void *context)
341 _uc = (ucontext_t *) context;
342 _mc = &_uc->uc_mcontext;
344 /* write integer registers */
345 for (i = 0; i < INT_REG_CNT; i++)
346 _mc->gregs[EAX - i] = es->intregs[i];
348 /* write special registers */
349 _mc->gregs[EIP] = (ptrint) es->pc;
350 _mc->gregs[ESP] = (ptrint) es->sp;
355 * These are local overrides for various environment variables in Emacs.
356 * Please do not remove this and leave it at the end of the file, where
357 * Emacs will automagically detect them.
358 * ---------------------------------------------------------------------
361 * indent-tabs-mode: t