PR middle-end/24998
[official-gcc.git] / libjava / include / i386-signal.h
bloba5c8ae4bf54a55c966f3b7cdcb4db19124616feb
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 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 #ifndef JAVA_SIGNAL_H
14 #define JAVA_SIGNAL_H 1
16 #include <signal.h>
17 #include <sys/syscall.h>
19 #define HANDLE_SEGV 1
20 #define HANDLE_FPE 1
22 #define SIGNAL_HANDLER(_name) \
23 static void _name (int _dummy __attribute__ ((__unused__)))
25 #define MAKE_THROW_FRAME(_exception) \
26 do \
27 { \
28 void **_p = (void **)&_dummy; \
29 volatile struct sigcontext_struct *_regs = (struct sigcontext_struct *)++_p; \
31 /* Advance the program counter so that it is after the start of the \
32 instruction: the x86 exception handler expects \
33 the PC to point to the instruction after a call. */ \
34 _regs->eip += 2; \
36 } \
37 while (0)
39 #define HANDLE_DIVIDE_OVERFLOW \
40 do \
41 { \
42 void **_p = (void **)&_dummy; \
43 volatile struct sigcontext_struct *_regs = (struct sigcontext_struct *)++_p;\
45 register unsigned char *_eip = (unsigned char *)_regs->eip; \
47 /* According to the JVM spec, "if the dividend is the negative \
48 * integer of the smallest magnitude and the divisor is -1, then \
49 * overflow occurs and the result is equal to the dividend. Despite \
50 * the overflow, no exception occurs". \
52 * We handle this by inspecting the instruction which generated the \
53 * signal and advancing eip to point to the following instruction. \
54 * As the instructions are variable length it is necessary to do a \
55 * little calculation to figure out where the following instruction \
56 * actually is. \
58 */ \
60 if (_eip[0] == 0xf7) \
61 { \
62 unsigned char _modrm = _eip[1]; \
64 if (_regs->eax == 0x80000000 \
65 && ((_modrm >> 3) & 7) == 7) /* Signed divide */ \
66 { \
67 unsigned char _rm = _modrm & 7; \
68 _regs->edx = 0; /* the remainder is zero */ \
69 switch (_modrm >> 6) \
70 { \
71 case 0: /* register indirect */ \
72 if (_rm == 5) /* 32-bit displacement */ \
73 _eip += 4; \
74 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
75 _eip += 1; \
76 break; \
77 case 1: /* register indirect + 8-bit displacement */ \
78 _eip += 1; \
79 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
80 _eip += 1; \
81 break; \
82 case 2: /* register indirect + 32-bit displacement */ \
83 _eip += 4; \
84 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
85 _eip += 1; \
86 break; \
87 case 3: \
88 break; \
89 } \
90 _eip += 2; \
91 _regs->eip = (unsigned long)_eip; \
92 return; \
93 } \
94 else \
95 { \
96 /* Advance the program counter so that it is after the start \
97 of the instruction: this is because the x86 exception \
98 handler expects the PC to point to the instruction after a \
99 call. */ \
100 _regs->eip += 2; \
104 while (0)
106 /* We use old_kernel_sigaction here because we're calling the kernel
107 directly rather than via glibc. The sigaction structure that the
108 syscall uses is a different shape from the one in userland and not
109 visible to us in a header file so we define it here. */
111 struct old_i386_kernel_sigaction {
112 void (*k_sa_handler) (int);
113 unsigned long k_sa_mask;
114 unsigned long k_sa_flags;
115 void (*sa_restorer) (void);
118 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
119 # define RESTORE2(name, syscall) \
120 asm \
122 ".text\n" \
123 ".byte 0 # Yes, this really is necessary\n" \
124 " .align 8\n" \
125 "__" #name ":\n" \
126 " popl %eax\n" \
127 " movl $" #syscall ", %eax\n" \
128 " int $0x80" \
131 RESTORE (restore, __NR_sigreturn)
132 static void restore (void) asm ("__restore");
134 #define INIT_SEGV \
135 do \
137 struct old_i386_kernel_sigaction kact; \
138 kact.k_sa_handler = catch_segv; \
139 kact.k_sa_mask = 0; \
140 kact.k_sa_flags = 0x4000000; \
141 kact.sa_restorer = restore; \
142 syscall (SYS_sigaction, SIGSEGV, &kact, NULL); \
144 while (0)
146 #define INIT_FPE \
147 do \
149 struct old_i386_kernel_sigaction kact; \
150 kact.k_sa_handler = catch_fpe; \
151 kact.k_sa_mask = 0; \
152 kact.k_sa_flags = 0x4000000; \
153 kact.sa_restorer = restore; \
154 syscall (SYS_sigaction, SIGFPE, &kact, NULL); \
156 while (0)
158 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
159 * instead of the standard sigaction(). This is necessary because of
160 * the shenanigans above where we increment the PC saved in the
161 * context and then return. This trick will only work when we are
162 * called _directly_ by the kernel, because linuxthreads wraps signal
163 * handlers and its wrappers do not copy the sigcontext struct back
164 * when returning from a signal handler. If we return from our divide
165 * handler to a linuxthreads wrapper, we will lose the PC adjustment
166 * we made and return to the faulting instruction again. Using
167 * syscall(SYS_sigaction) causes our handler to be called directly
168 * by the kernel, bypassing any wrappers.
170 * Also, there is at the present time no unwind info in the
171 * linuxthreads library's signal handlers and so we can't unwind
172 * through them anyway. */
174 #endif /* JAVA_SIGNAL_H */