comment/style fixes
[official-gcc.git] / libjava / include / i386-signal.h
bloba3d3a1b75f094e4dce5c0c7a7958a73b448b036a
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, 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 #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 _Jv_##_name (int, siginfo_t *, \
24 void *_p __attribute__ ((__unused__)))
26 #define HANDLE_DIVIDE_OVERFLOW \
27 do \
28 { \
29 struct ucontext *_uc = (struct ucontext *)_p; \
30 gregset_t &_gregs = _uc->uc_mcontext.gregs; \
31 unsigned char *_eip = (unsigned char *)_gregs[REG_EIP]; \
33 /* According to the JVM spec, "if the dividend is the negative \
34 * integer of largest possible magnitude for the type and the \
35 * divisor is -1, then overflow occurs and the result is equal to \
36 * the dividend. Despite the overflow, no exception occurs". \
38 * We handle this by inspecting the instruction which generated the \
39 * signal and advancing ip to point to the following instruction. \
40 * As the instructions are variable length it is necessary to do a \
41 * little calculation to figure out where the following instruction \
42 * actually is. \
44 */ \
46 /* Detect a signed division of Integer.MIN_VALUE. */ \
47 if (_eip[0] == 0xf7) \
48 { \
49 bool _min_value_dividend = false; \
50 unsigned char _modrm = _eip[1]; \
52 if (((_modrm >> 3) & 7) == 7) /* Signed divide */ \
53 { \
54 _min_value_dividend = \
55 _gregs[REG_EAX] == (greg_t)0x80000000UL; \
56 } \
58 if (_min_value_dividend) \
59 { \
60 unsigned char _rm = _modrm & 7; \
61 _gregs[REG_EDX] = 0; /* the remainder is zero */ \
62 switch (_modrm >> 6) \
63 { \
64 case 0: /* register indirect */ \
65 if (_rm == 5) /* 32-bit displacement */ \
66 _eip += 4; \
67 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
68 _eip += 1; \
69 break; \
70 case 1: /* register indirect + 8-bit displacement */ \
71 _eip += 1; \
72 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
73 _eip += 1; \
74 break; \
75 case 2: /* register indirect + 32-bit displacement */ \
76 _eip += 4; \
77 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
78 _eip += 1; \
79 break; \
80 case 3: \
81 break; \
82 } \
83 _eip += 2; \
84 _gregs[REG_EIP] = (greg_t)_eip; \
85 return; \
86 } \
87 } \
88 } \
89 while (0)
91 /* We use kernel_sigaction here because we're calling the kernel
92 directly rather than via glibc. The sigaction structure that the
93 syscall uses is a different shape from the one in userland and not
94 visible to us in a header file so we define it here. */
96 extern "C"
98 struct kernel_sigaction
100 void (*k_sa_sigaction)(int,siginfo_t *,void *);
101 unsigned long k_sa_flags;
102 void (*k_sa_restorer) (void);
103 sigset_t k_sa_mask;
107 #define MAKE_THROW_FRAME(_exception)
109 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
110 #define RESTORE2(name, syscall) \
111 asm \
113 ".text\n" \
114 ".byte 0 # Yes, this really is necessary\n" \
115 " .align 16\n" \
116 "__" #name ":\n" \
117 " movl $" #syscall ", %eax\n" \
118 " int $0x80" \
121 /* The return code for realtime-signals. */
122 RESTORE (restore_rt, __NR_rt_sigreturn)
123 void restore_rt (void) asm ("__restore_rt")
124 __attribute__ ((visibility ("hidden")));
126 #define INIT_SEGV \
127 do \
129 struct kernel_sigaction act; \
130 act.k_sa_sigaction = _Jv_catch_segv; \
131 sigemptyset (&act.k_sa_mask); \
132 act.k_sa_flags = SA_SIGINFO|0x4000000; \
133 act.k_sa_restorer = restore_rt; \
134 syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
136 while (0)
138 #define INIT_FPE \
139 do \
141 struct kernel_sigaction act; \
142 act.k_sa_sigaction = _Jv_catch_fpe; \
143 sigemptyset (&act.k_sa_mask); \
144 act.k_sa_flags = SA_SIGINFO|0x4000000; \
145 act.k_sa_restorer = restore_rt; \
146 syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8); \
148 while (0)
150 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
151 * instead of the standard sigaction(). This is necessary because of
152 * the shenanigans above where we increment the PC saved in the
153 * context and then return. This trick will only work when we are
154 * called _directly_ by the kernel, because linuxthreads wraps signal
155 * handlers and its wrappers do not copy the sigcontext struct back
156 * when returning from a signal handler. If we return from our divide
157 * handler to a linuxthreads wrapper, we will lose the PC adjustment
158 * we made and return to the faulting instruction again. Using
159 * syscall(SYS_sigaction) causes our handler to be called directly
160 * by the kernel, bypassing any wrappers.
162 * Also, there may not be any unwind info in the linuxthreads
163 * library's signal handlers and so we can't unwind through them
164 * anyway. */
166 #endif /* JAVA_SIGNAL_H */