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
14 #define JAVA_SIGNAL_H 1
17 #include <sys/syscall.h>
22 #define SIGNAL_HANDLER(_name) \
23 static void _name (int _dummy __attribute__ ((__unused__)))
25 #define MAKE_THROW_FRAME(_exception) \
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. */ \
39 #define HANDLE_DIVIDE_OVERFLOW \
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 \
60 if (_eip[0] == 0xf7) \
62 unsigned char _modrm = _eip[1]; \
64 if (_regs->eax == 0x80000000 \
65 && ((_modrm >> 3) & 7) == 7) /* Signed divide */ \
67 unsigned char _rm = _modrm & 7; \
68 _regs->edx = 0; /* the remainder is zero */ \
69 switch (_modrm >> 6) \
71 case 0: /* register indirect */ \
72 if (_rm == 5) /* 32-bit displacement */ \
74 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
77 case 1: /* register indirect + 8-bit displacement */ \
79 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
82 case 2: /* register indirect + 32-bit displacement */ \
84 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
91 _regs->eip = (unsigned long)_eip; \
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 \
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) \
123 ".byte 0 # Yes, this really is necessary\n" \
127 " movl $" #syscall ", %eax\n" \
131 RESTORE (restore, __NR_sigreturn)
132 static void restore (void) asm ("__restore");
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); \
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); \
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 */