* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[official-gcc.git] / libjava / include / x86_64-signal.h
blobd03b91439ef8c4e6c19bf853210b2f58ac00dcaf
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
10 details. */
13 #ifdef __x86_64__
15 #ifndef JAVA_SIGNAL_H
16 #define JAVA_SIGNAL_H 1
18 #include <signal.h>
19 #include <sys/syscall.h>
21 #define HANDLE_SEGV 1
22 #define HANDLE_FPE 1
24 #define SIGNAL_HANDLER(_name) \
25 static void _Jv_##_name (int, siginfo_t *, \
26 void *_p __attribute__ ((__unused__)))
28 #define HANDLE_DIVIDE_OVERFLOW \
29 do \
30 { \
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 \
44 * actually is. \
46 */ \
48 bool _is_64_bit = false; \
50 /* Skip 67h address size prefix. */ \
51 if (_rip[0] == 0x67) \
52 _rip++; \
54 if ((_rip[0] & 0xf0) == 0x40) /* REX byte present. */ \
55 { \
56 unsigned char _rex = _rip[0] & 0x0f; \
57 _is_64_bit = (_rex & 0x08) != 0; \
58 _rip++; \
59 } \
61 /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE. */ \
62 if (_rip[0] == 0xf7) \
63 { \
64 bool _min_value_dividend = false; \
65 unsigned char _modrm = _rip[1]; \
67 if (((_modrm >> 3) & 7) == 7) \
68 { \
69 if (_is_64_bit) \
70 _min_value_dividend = \
71 _gregs[REG_RAX] == (greg_t)0x8000000000000000ULL; \
72 else \
73 _min_value_dividend = \
74 (_gregs[REG_RAX] & 0xffffffff) == (greg_t)0x80000000ULL; \
75 } \
77 if (_min_value_dividend) \
78 { \
79 unsigned char _rm = _modrm & 7; \
80 _gregs[REG_RDX] = 0; /* the remainder is zero */ \
81 switch (_modrm >> 6) \
82 { \
83 case 0: /* register indirect */ \
84 if (_rm == 5) /* 32-bit displacement */ \
85 _rip += 4; \
86 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
87 _rip += 1; \
88 break; \
89 case 1: /* register indirect + 8-bit displacement */ \
90 _rip += 1; \
91 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
92 _rip += 1; \
93 break; \
94 case 2: /* register indirect + 32-bit displacement */ \
95 _rip += 4; \
96 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
97 _rip += 1; \
98 break; \
99 case 3: \
100 break; \
102 _rip += 2; \
103 _gregs[REG_RIP] = (greg_t)_rip; \
104 return; \
108 while (0)
110 extern "C"
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);
117 sigset_t k_sa_mask;
121 #define MAKE_THROW_FRAME(_exception)
123 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
124 #define RESTORE2(name, syscall) \
125 asm \
127 ".text\n" \
128 ".byte 0 # Yes, this really is necessary\n" \
129 ".align 16\n" \
130 "__" #name ":\n" \
131 " movq $" #syscall ", %rax\n" \
132 " syscall\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")));
140 #define INIT_SEGV \
141 do \
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); \
150 while (0)
152 #define INIT_FPE \
153 do \
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); \
162 while (0)
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__ */