2006-07-08 Matthias Klose <doko@debian.org>
[official-gcc.git] / libjava / include / i386-signal.h
bloba30ceeb257f8176fc32fdd1fb09749c3417af1da
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 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)
27 #define HANDLE_DIVIDE_OVERFLOW \
28 do \
29 { \
30 void **_p = (void **)&_dummy; \
31 volatile struct sigcontext_struct *_regs = (struct sigcontext_struct *)++_p;\
33 register unsigned char *_eip = (unsigned char *)_regs->eip; \
35 /* According to the JVM spec, "if the dividend is the negative \
36 * integer of the smallest magnitude and the divisor is -1, then \
37 * overflow occurs and the result is equal to the dividend. Despite \
38 * the overflow, no exception occurs". \
40 * We handle this by inspecting the instruction which generated the \
41 * signal and advancing eip 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 if (_eip[0] == 0xf7) \
49 { \
50 unsigned char _modrm = _eip[1]; \
52 if (_regs->eax == 0x80000000 \
53 && ((_modrm >> 3) & 7) == 7) /* Signed divide */ \
54 { \
55 unsigned char _rm = _modrm & 7; \
56 _regs->edx = 0; /* the remainder is zero */ \
57 switch (_modrm >> 6) \
58 { \
59 case 0: /* register indirect */ \
60 if (_rm == 5) /* 32-bit displacement */ \
61 _eip += 4; \
62 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
63 _eip += 1; \
64 break; \
65 case 1: /* register indirect + 8-bit displacement */ \
66 _eip += 1; \
67 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
68 _eip += 1; \
69 break; \
70 case 2: /* register indirect + 32-bit displacement */ \
71 _eip += 4; \
72 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
73 _eip += 1; \
74 break; \
75 case 3: \
76 break; \
77 } \
78 _eip += 2; \
79 _regs->eip = (unsigned long)_eip; \
80 return; \
81 } \
82 } \
83 } \
84 while (0)
86 /* We use old_kernel_sigaction here because we're calling the kernel
87 directly rather than via glibc. The sigaction structure that the
88 syscall uses is a different shape from the one in userland and not
89 visible to us in a header file so we define it here. */
91 struct old_i386_kernel_sigaction {
92 void (*k_sa_handler) (int);
93 unsigned long k_sa_mask;
94 unsigned long k_sa_flags;
95 void (*sa_restorer) (void);
98 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
99 # define RESTORE2(name, syscall) \
100 asm \
102 ".text\n" \
103 ".byte 0 # Yes, this really is necessary\n" \
104 " .align 8\n" \
105 "__" #name ":\n" \
106 " popl %eax\n" \
107 " movl $" #syscall ", %eax\n" \
108 " int $0x80" \
111 RESTORE (restore, __NR_sigreturn)
112 static void restore (void) asm ("__restore");
114 #define INIT_SEGV \
115 do \
117 struct old_i386_kernel_sigaction kact; \
118 kact.k_sa_handler = catch_segv; \
119 kact.k_sa_mask = 0; \
120 kact.k_sa_flags = 0x4000000; \
121 kact.sa_restorer = restore; \
122 syscall (SYS_sigaction, SIGSEGV, &kact, NULL); \
124 while (0)
126 #define INIT_FPE \
127 do \
129 struct old_i386_kernel_sigaction kact; \
130 kact.k_sa_handler = catch_fpe; \
131 kact.k_sa_mask = 0; \
132 kact.k_sa_flags = 0x4000000; \
133 kact.sa_restorer = restore; \
134 syscall (SYS_sigaction, SIGFPE, &kact, NULL); \
136 while (0)
138 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
139 * instead of the standard sigaction(). This is necessary because of
140 * the shenanigans above where we increment the PC saved in the
141 * context and then return. This trick will only work when we are
142 * called _directly_ by the kernel, because linuxthreads wraps signal
143 * handlers and its wrappers do not copy the sigcontext struct back
144 * when returning from a signal handler. If we return from our divide
145 * handler to a linuxthreads wrapper, we will lose the PC adjustment
146 * we made and return to the faulting instruction again. Using
147 * syscall(SYS_sigaction) causes our handler to be called directly
148 * by the kernel, bypassing any wrappers.
150 * Also, there is at the present time no unwind info in the
151 * linuxthreads library's signal handlers and so we can't unwind
152 * through them anyway. */
154 #endif /* JAVA_SIGNAL_H */