OSDN Git Service

* include/posix-threads.h (_Jv_ThreadDebugSuspend): Declare.
[pf3gnuchains/gcc-fork.git] / libjava / include / x86_64-signal.h
1 // x86_64-signal.h - Catch runtime signals and turn them into exceptions
2 // on an x86_64 based GNU/Linux system.
3
4 /* Copyright (C) 2003, 2006  Free Software Foundation
5
6    This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10 details.  */
11
12
13 #ifdef __x86_64__
14
15 #ifndef JAVA_SIGNAL_H
16 #define JAVA_SIGNAL_H 1
17
18 #include <signal.h>
19 #include <sys/syscall.h>
20
21 #define HANDLE_SEGV 1
22 #define HANDLE_FPE 1
23
24 #define SIGNAL_HANDLER(_name)                                   \
25 static void _Jv_##_name (int, siginfo_t *,                      \
26                          void *_p __attribute__ ((__unused__)))
27
28 #define HANDLE_DIVIDE_OVERFLOW                                          \
29 do                                                                      \
30 {                                                                       \
31   struct ucontext *_uc = (struct ucontext *)_p;                         \
32   volatile struct sigcontext *_sc = (struct sigcontext *) &_uc->uc_mcontext; \
33                                                                         \
34   register unsigned char *_rip = (unsigned char *)_sc->rip;             \
35                                                                         \
36   /* According to the JVM spec, "if the dividend is the negative        \
37    * integer of largest possible magnitude for the type and the         \
38    * divisor is -1, then overflow occurs and the result is equal to     \
39    * the dividend.  Despite the overflow, no exception occurs".         \
40                                                                         \
41    * We handle this by inspecting the instruction which generated the   \
42    * signal and advancing ip to point to the following instruction.     \
43    * As the instructions are variable length it is necessary to do a    \
44    * little calculation to figure out where the following instruction   \
45    * actually is.                                                       \
46                                                                         \
47    */                                                                   \
48                                                                         \
49   bool _is_64_bit = false;                                              \
50                                                                         \
51   if ((_rip[0] & 0xf0) == 0x40)  /* REX byte present.  */               \
52     {                                                                   \
53       unsigned char _rex = _rip[0] & 0x0f;                              \
54       _is_64_bit = (_rex & 0x08) != 0;                                  \
55       _rip++;                                                           \
56     }                                                                   \
57                                                                         \
58   /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE.  */ \
59   if (_rip[0] == 0xf7)                                                  \
60     {                                                                   \
61       bool _min_value_dividend = false;                                 \
62       unsigned char _modrm = _rip[1];                                   \
63                                                                         \
64       if (((_modrm >> 3) & 7) == 7)                                     \
65         {                                                               \
66           if (_is_64_bit)                                               \
67             _min_value_dividend = (_sc->rax == 0x8000000000000000L);    \
68           else                                                          \
69             _min_value_dividend = ((_sc->rax & 0xffffffff) == 0x80000000); \
70         }                                                               \
71                                                                         \
72       if (_min_value_dividend)                                          \
73         {                                                               \
74           unsigned char _rm = _modrm & 7;                               \
75           _sc->rdx = 0; /* the remainder is zero */                     \
76           switch (_modrm >> 6)                                          \
77             {                                                           \
78             case 0:  /* register indirect */                            \
79               if (_rm == 5)   /* 32-bit displacement */                 \
80                 _rip += 4;                                              \
81               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
82                 _rip += 1;                                              \
83               break;                                                    \
84             case 1:  /* register indirect + 8-bit displacement */       \
85               _rip += 1;                                                \
86               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
87                 _rip += 1;                                              \
88               break;                                                    \
89             case 2:  /* register indirect + 32-bit displacement */      \
90               _rip += 4;                                                \
91               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
92                 _rip += 1;                                              \
93               break;                                                    \
94             case 3:                                                     \
95               break;                                                    \
96             }                                                           \
97           _rip += 2;                                                    \
98           _sc->rip = (unsigned long)_rip;                               \
99           return;                                                       \
100         }                                                               \
101     }                                                                   \
102 }                                                                       \
103 while (0)
104
105 extern "C" 
106 {
107   struct kernel_sigaction 
108   {
109     void (*k_sa_sigaction)(int,siginfo_t *,void *);
110     unsigned long k_sa_flags;
111     void (*k_sa_restorer) (void);
112     sigset_t k_sa_mask;
113   };
114 }
115
116 #define MAKE_THROW_FRAME(_exception)
117
118 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
119 #define RESTORE2(name, syscall)                 \
120 asm                                             \
121   (                                             \
122    ".text\n"                                    \
123    ".byte 0  # Yes, this really is necessary\n" \
124    ".align 16\n"                                \
125    "__" #name ":\n"                             \
126    "    movq $" #syscall ", %rax\n"             \
127    "    syscall\n"                              \
128    );
129
130 /* The return code for realtime-signals.  */
131 RESTORE (restore_rt, __NR_rt_sigreturn)
132 void restore_rt (void) asm ("__restore_rt")
133   __attribute__ ((visibility ("hidden")));
134
135 #define INIT_SEGV                                               \
136 do                                                              \
137   {                                                             \
138     struct kernel_sigaction act;                                \
139     act.k_sa_sigaction = _Jv_catch_segv;                        \
140     sigemptyset (&act.k_sa_mask);                               \
141     act.k_sa_flags = SA_SIGINFO|0x4000000;                      \
142     act.k_sa_restorer = restore_rt;                             \
143     syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
144   }                                                             \
145 while (0)  
146
147 #define INIT_FPE                                                \
148 do                                                              \
149   {                                                             \
150     struct kernel_sigaction act;                                \
151     act.k_sa_sigaction = _Jv_catch_fpe;                         \
152     sigemptyset (&act.k_sa_mask);                               \
153     act.k_sa_flags = SA_SIGINFO|0x4000000;                      \
154     act.k_sa_restorer = restore_rt;                             \
155     syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8);  \
156   }                                                             \
157 while (0)  
158
159 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
160  * instead of the standard sigaction().  This is necessary because of
161  * the shenanigans above where we increment the PC saved in the
162  * context and then return.  This trick will only work when we are
163  * called _directly_ by the kernel, because linuxthreads wraps signal
164  * handlers and its wrappers do not copy the sigcontext struct back
165  * when returning from a signal handler.  If we return from our divide
166  * handler to a linuxthreads wrapper, we will lose the PC adjustment
167  * we made and return to the faulting instruction again.  Using
168  * syscall(SYS_sigaction) causes our handler to be called directly
169  * by the kernel, bypassing any wrappers.  */
170
171 #endif /* JAVA_SIGNAL_H */
172
173 #else /* __x86_64__ */
174
175 /* This is for the 32-bit subsystem on x86-64.  */
176
177 #define sigcontext_struct sigcontext
178 #include <java-signal-aux.h>
179
180 #endif /* __x86_64__ */