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, 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
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 gregset_t &_gregs = _uc->uc_mcontext.gregs; \
33 unsigned char *_rip = (unsigned char *)_gregs[REG_RIP]; \
35 /* According to the JVM spec, "if the dividend is the negative \
36 * integer of largest possible magnitude for the type and the \
37 * divisor is -1, then overflow occurs and the result is equal to \
38 * the dividend. Despite the overflow, no exception occurs". \
40 * We handle this by inspecting the instruction which generated the \
41 * signal and advancing ip to point to the following instruction. \
42 * As the instructions are variable length it is necessary to do a \
43 * little calculation to figure out where the following instruction \
48 bool _is_64_bit = false; \
50 /* Skip 67h address size prefix. */ \
51 if (_rip[0] == 0x67) \
54 if ((_rip[0] & 0xf0) == 0x40) /* REX byte present. */ \
56 unsigned char _rex = _rip[0] & 0x0f; \
57 _is_64_bit = (_rex & 0x08) != 0; \
61 /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE. */ \
62 if (_rip[0] == 0xf7) \
64 bool _min_value_dividend = false; \
65 unsigned char _modrm = _rip[1]; \
67 if (((_modrm >> 3) & 7) == 7) \
70 _min_value_dividend = \
71 _gregs[REG_RAX] == (greg_t)0x8000000000000000ULL; \
73 _min_value_dividend = \
74 (_gregs[REG_RAX] & 0xffffffff) == (greg_t)0x80000000ULL; \
77 if (_min_value_dividend) \
79 unsigned char _rm = _modrm & 7; \
80 _gregs[REG_RDX] = 0; /* the remainder is zero */ \
81 switch (_modrm >> 6) \
83 case 0: /* register indirect */ \
84 if (_rm == 5) /* 32-bit displacement */ \
86 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
89 case 1: /* register indirect + 8-bit displacement */ \
91 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
94 case 2: /* register indirect + 32-bit displacement */ \
96 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
103 _gregs[REG_RIP] = (greg_t)_rip; \
112 struct kernel_sigaction
114 void (*k_sa_sigaction
)(int,siginfo_t
*,void *);
115 unsigned long k_sa_flags
;
116 void (*k_sa_restorer
) (void);
121 #define MAKE_THROW_FRAME(_exception)
123 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
124 #define RESTORE2(name, syscall) \
128 ".byte 0 # Yes, this really is necessary\n" \
131 " movq $" #syscall ", %rax\n" \
135 /* The return code for realtime-signals. */
136 RESTORE (restore_rt
, __NR_rt_sigreturn
)
137 void restore_rt (void) asm ("__restore_rt")
138 __attribute__ ((visibility ("hidden")));
143 struct kernel_sigaction act; \
144 act.k_sa_sigaction = _Jv_catch_segv; \
145 sigemptyset (&act.k_sa_mask); \
146 act.k_sa_flags = SA_SIGINFO|0x4000000; \
147 act.k_sa_restorer = restore_rt; \
148 syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
155 struct kernel_sigaction act; \
156 act.k_sa_sigaction = _Jv_catch_fpe; \
157 sigemptyset (&act.k_sa_mask); \
158 act.k_sa_flags = SA_SIGINFO|0x4000000; \
159 act.k_sa_restorer = restore_rt; \
160 syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8); \
164 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
165 * instead of the standard sigaction(). This is necessary because of
166 * the shenanigans above where we increment the PC saved in the
167 * context and then return. This trick will only work when we are
168 * called _directly_ by the kernel, because linuxthreads wraps signal
169 * handlers and its wrappers do not copy the sigcontext struct back
170 * when returning from a signal handler. If we return from our divide
171 * handler to a linuxthreads wrapper, we will lose the PC adjustment
172 * we made and return to the faulting instruction again. Using
173 * syscall(SYS_sigaction) causes our handler to be called directly
174 * by the kernel, bypassing any wrappers. */
176 #endif /* JAVA_SIGNAL_H */
178 #else /* __x86_64__ */
180 /* This is for the 32-bit subsystem on x86-64. */
182 #define sigcontext_struct sigcontext
183 #include <java-signal-aux.h>
185 #endif /* __x86_64__ */