1 // i386-signal.h - Catch runtime signals and turn them into exceptions
2 // on an i386 based Linux system.
4 /* Copyright (C) 1998, 1999, 2001, 2002, 2006, 2007 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 #define JAVA_SIGNAL_H 1
17 #include <sys/syscall.h>
22 #define SIGNAL_HANDLER(_name) \
23 static void _Jv_##_name (int, siginfo_t *, \
24 void *_p __attribute__ ((__unused__)))
26 #define HANDLE_DIVIDE_OVERFLOW \
29 struct ucontext *_uc = (struct ucontext *)_p; \
30 gregset_t &_gregs = _uc->uc_mcontext.gregs; \
31 unsigned char *_eip = (unsigned char *)_gregs[REG_EIP]; \
33 /* According to the JVM spec, "if the dividend is the negative \
34 * integer of largest possible magnitude for the type and the \
35 * divisor is -1, then overflow occurs and the result is equal to \
36 * the dividend. Despite the overflow, no exception occurs". \
38 * We handle this by inspecting the instruction which generated the \
39 * signal and advancing ip to point to the following instruction. \
40 * As the instructions are variable length it is necessary to do a \
41 * little calculation to figure out where the following instruction \
46 /* Detect a signed division of Integer.MIN_VALUE. */ \
47 if (_eip[0] == 0xf7) \
49 bool _min_value_dividend = false; \
50 unsigned char _modrm = _eip[1]; \
52 if (((_modrm >> 3) & 7) == 7) /* Signed divide */ \
54 _min_value_dividend = \
55 _gregs[REG_EAX] == (greg_t)0x80000000UL; \
58 if (_min_value_dividend) \
60 unsigned char _rm = _modrm & 7; \
61 _gregs[REG_EDX] = 0; /* the remainder is zero */ \
62 switch (_modrm >> 6) \
64 case 0: /* register indirect */ \
65 if (_rm == 5) /* 32-bit displacement */ \
67 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
70 case 1: /* register indirect + 8-bit displacement */ \
72 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
75 case 2: /* register indirect + 32-bit displacement */ \
77 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
84 _gregs[REG_EIP] = (greg_t)_eip; \
91 /* We use kernel_sigaction here because we're calling the kernel
92 directly rather than via glibc. The sigaction structure that the
93 syscall uses is a different shape from the one in userland and not
94 visible to us in a header file so we define it here. */
98 struct kernel_sigaction
100 void (*k_sa_sigaction
)(int,siginfo_t
*,void *);
101 unsigned long k_sa_flags
;
102 void (*k_sa_restorer
) (void);
107 #define MAKE_THROW_FRAME(_exception)
109 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
110 #define RESTORE2(name, syscall) \
114 ".byte 0 # Yes, this really is necessary\n" \
117 " movl $" #syscall ", %eax\n" \
121 /* The return code for realtime-signals. */
122 RESTORE (restore_rt
, __NR_rt_sigreturn
)
123 void restore_rt (void) asm ("__restore_rt")
124 __attribute__ ((visibility ("hidden")));
129 struct kernel_sigaction act; \
130 act.k_sa_sigaction = _Jv_catch_segv; \
131 sigemptyset (&act.k_sa_mask); \
132 act.k_sa_flags = SA_SIGINFO|0x4000000; \
133 act.k_sa_restorer = restore_rt; \
134 syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
141 struct kernel_sigaction act; \
142 act.k_sa_sigaction = _Jv_catch_fpe; \
143 sigemptyset (&act.k_sa_mask); \
144 act.k_sa_flags = SA_SIGINFO|0x4000000; \
145 act.k_sa_restorer = restore_rt; \
146 syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8); \
150 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
151 * instead of the standard sigaction(). This is necessary because of
152 * the shenanigans above where we increment the PC saved in the
153 * context and then return. This trick will only work when we are
154 * called _directly_ by the kernel, because linuxthreads wraps signal
155 * handlers and its wrappers do not copy the sigcontext struct back
156 * when returning from a signal handler. If we return from our divide
157 * handler to a linuxthreads wrapper, we will lose the PC adjustment
158 * we made and return to the faulting instruction again. Using
159 * syscall(SYS_sigaction) causes our handler to be called directly
160 * by the kernel, bypassing any wrappers.
162 * Also, there may not be any unwind info in the linuxthreads
163 * library's signal handlers and so we can't unwind through them
166 #endif /* JAVA_SIGNAL_H */