1 // x86_64-signal.h - Catch runtime signals and turn them into exceptions
2 // on an x86_64 based GNU/Linux system.
4 /* Copyright (C) 2003, 2006 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
16 #define JAVA_SIGNAL_H 1
19 #include <sys/syscall.h>
24 #define SIGNAL_HANDLER(_name) \
25 static void _Jv_##_name (int, siginfo_t *, \
26 void *_p __attribute__ ((__unused__)))
28 #define HANDLE_DIVIDE_OVERFLOW \
31 struct ucontext *_uc = (struct ucontext *)_p; \
32 volatile struct sigcontext *_sc = (struct sigcontext *) &_uc->uc_mcontext; \
34 register unsigned char *_rip = (unsigned char *)_sc->rip; \
36 /* According to the JVM spec, "if the dividend is the negative \
37 * integer of largest possible magnitude for the type and the \
38 * divisor is -1, then overflow occurs and the result is equal to \
39 * the dividend. Despite the overflow, no exception occurs". \
41 * We handle this by inspecting the instruction which generated the \
42 * signal and advancing ip to point to the following instruction. \
43 * As the instructions are variable length it is necessary to do a \
44 * little calculation to figure out where the following instruction \
49 bool _is_64_bit = false; \
51 if ((_rip[0] & 0xf0) == 0x40) /* REX byte present. */ \
53 unsigned char _rex = _rip[0] & 0x0f; \
54 _is_64_bit = (_rex & 0x08) != 0; \
58 /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE. */ \
59 if (_rip[0] == 0xf7) \
61 bool _min_value_dividend = false; \
62 unsigned char _modrm = _rip[1]; \
64 if (((_modrm >> 3) & 7) == 7) \
67 _min_value_dividend = (_sc->rax == 0x8000000000000000L); \
69 _min_value_dividend = ((_sc->rax & 0xffffffff) == 0x80000000); \
72 if (_min_value_dividend) \
74 unsigned char _rm = _modrm & 7; \
75 _sc->rdx = 0; /* the remainder is zero */ \
76 switch (_modrm >> 6) \
78 case 0: /* register indirect */ \
79 if (_rm == 5) /* 32-bit displacement */ \
81 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
84 case 1: /* register indirect + 8-bit displacement */ \
86 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
89 case 2: /* register indirect + 32-bit displacement */ \
91 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
98 _sc->rip = (unsigned long)_rip; \
107 struct kernel_sigaction
109 void (*k_sa_sigaction
)(int,siginfo_t
*,void *);
110 unsigned long k_sa_flags
;
111 void (*k_sa_restorer
) (void);
116 #define MAKE_THROW_FRAME(_exception)
118 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
119 #define RESTORE2(name, syscall) \
123 ".byte 0 # Yes, this really is necessary\n" \
126 " movq $" #syscall ", %rax\n" \
130 /* The return code for realtime-signals. */
131 RESTORE (restore_rt
, __NR_rt_sigreturn
)
132 void restore_rt (void) asm ("__restore_rt")
133 __attribute__ ((visibility ("hidden")));
138 struct kernel_sigaction act; \
139 act.k_sa_sigaction = _Jv_catch_segv; \
140 sigemptyset (&act.k_sa_mask); \
141 act.k_sa_flags = SA_SIGINFO|0x4000000; \
142 act.k_sa_restorer = restore_rt; \
143 syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
150 struct kernel_sigaction act; \
151 act.k_sa_sigaction = _Jv_catch_fpe; \
152 sigemptyset (&act.k_sa_mask); \
153 act.k_sa_flags = SA_SIGINFO|0x4000000; \
154 act.k_sa_restorer = restore_rt; \
155 syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8); \
159 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
160 * instead of the standard sigaction(). This is necessary because of
161 * the shenanigans above where we increment the PC saved in the
162 * context and then return. This trick will only work when we are
163 * called _directly_ by the kernel, because linuxthreads wraps signal
164 * handlers and its wrappers do not copy the sigcontext struct back
165 * when returning from a signal handler. If we return from our divide
166 * handler to a linuxthreads wrapper, we will lose the PC adjustment
167 * we made and return to the faulting instruction again. Using
168 * syscall(SYS_sigaction) causes our handler to be called directly
169 * by the kernel, bypassing any wrappers. */
171 #endif /* JAVA_SIGNAL_H */
173 #else /* __x86_64__ */
175 /* This is for the 32-bit subsystem on x86-64. */
177 #define sigcontext_struct sigcontext
178 #include <java-signal-aux.h>
180 #endif /* __x86_64__ */