OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[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, 2007  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   gregset_t &_gregs = _uc->uc_mcontext.gregs;                           \
33   unsigned char *_rip = (unsigned char *)_gregs[REG_RIP];               \
34                                                                         \
35   /* According to the JVM spec, "if the dividend is the negative        \
36    * integer of largest possible magnitude for the type and the         \
37    * divisor is -1, then overflow occurs and the result is equal to     \
38    * the dividend.  Despite the overflow, no exception occurs".         \
39                                                                         \
40    * We handle this by inspecting the instruction which generated the   \
41    * signal and advancing ip 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.                                                       \
45                                                                         \
46    */                                                                   \
47                                                                         \
48   bool _is_64_bit = false;                                              \
49                                                                         \
50   if ((_rip[0] & 0xf0) == 0x40)  /* REX byte present.  */               \
51     {                                                                   \
52       unsigned char _rex = _rip[0] & 0x0f;                              \
53       _is_64_bit = (_rex & 0x08) != 0;                                  \
54       _rip++;                                                           \
55     }                                                                   \
56                                                                         \
57   /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE.  */ \
58   if (_rip[0] == 0xf7)                                                  \
59     {                                                                   \
60       bool _min_value_dividend = false;                                 \
61       unsigned char _modrm = _rip[1];                                   \
62                                                                         \
63       if (((_modrm >> 3) & 7) == 7)                                     \
64         {                                                               \
65           if (_is_64_bit)                                               \
66             _min_value_dividend =                                       \
67               _gregs[REG_RAX] == (greg_t)0x8000000000000000UL;          \
68           else                                                          \
69             _min_value_dividend =                                       \
70               (_gregs[REG_RAX] & 0xffffffff) == (greg_t)0x80000000UL;   \
71         }                                                               \
72                                                                         \
73       if (_min_value_dividend)                                          \
74         {                                                               \
75           unsigned char _rm = _modrm & 7;                               \
76           _gregs[REG_RDX] = 0; /* the remainder is zero */              \
77           switch (_modrm >> 6)                                          \
78             {                                                           \
79             case 0:  /* register indirect */                            \
80               if (_rm == 5)   /* 32-bit displacement */                 \
81                 _rip += 4;                                              \
82               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
83                 _rip += 1;                                              \
84               break;                                                    \
85             case 1:  /* register indirect + 8-bit displacement */       \
86               _rip += 1;                                                \
87               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
88                 _rip += 1;                                              \
89               break;                                                    \
90             case 2:  /* register indirect + 32-bit displacement */      \
91               _rip += 4;                                                \
92               if (_rm == 4)  /* A SIB byte follows the ModR/M byte */   \
93                 _rip += 1;                                              \
94               break;                                                    \
95             case 3:                                                     \
96               break;                                                    \
97             }                                                           \
98           _rip += 2;                                                    \
99           _gregs[REG_RIP] = (greg_t)_rip;                               \
100           return;                                                       \
101         }                                                               \
102     }                                                                   \
103 }                                                                       \
104 while (0)
105
106 extern "C" 
107 {
108   struct kernel_sigaction 
109   {
110     void (*k_sa_sigaction)(int,siginfo_t *,void *);
111     unsigned long k_sa_flags;
112     void (*k_sa_restorer) (void);
113     sigset_t k_sa_mask;
114   };
115 }
116
117 #define MAKE_THROW_FRAME(_exception)
118
119 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
120 #define RESTORE2(name, syscall)                 \
121 asm                                             \
122   (                                             \
123    ".text\n"                                    \
124    ".byte 0  # Yes, this really is necessary\n" \
125    ".align 16\n"                                \
126    "__" #name ":\n"                             \
127    "    movq $" #syscall ", %rax\n"             \
128    "    syscall\n"                              \
129    );
130
131 /* The return code for realtime-signals.  */
132 RESTORE (restore_rt, __NR_rt_sigreturn)
133 void restore_rt (void) asm ("__restore_rt")
134   __attribute__ ((visibility ("hidden")));
135
136 #define INIT_SEGV                                               \
137 do                                                              \
138   {                                                             \
139     struct kernel_sigaction act;                                \
140     act.k_sa_sigaction = _Jv_catch_segv;                        \
141     sigemptyset (&act.k_sa_mask);                               \
142     act.k_sa_flags = SA_SIGINFO|0x4000000;                      \
143     act.k_sa_restorer = restore_rt;                             \
144     syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
145   }                                                             \
146 while (0)  
147
148 #define INIT_FPE                                                \
149 do                                                              \
150   {                                                             \
151     struct kernel_sigaction act;                                \
152     act.k_sa_sigaction = _Jv_catch_fpe;                         \
153     sigemptyset (&act.k_sa_mask);                               \
154     act.k_sa_flags = SA_SIGINFO|0x4000000;                      \
155     act.k_sa_restorer = restore_rt;                             \
156     syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8);  \
157   }                                                             \
158 while (0)  
159
160 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
161  * instead of the standard sigaction().  This is necessary because of
162  * the shenanigans above where we increment the PC saved in the
163  * context and then return.  This trick will only work when we are
164  * called _directly_ by the kernel, because linuxthreads wraps signal
165  * handlers and its wrappers do not copy the sigcontext struct back
166  * when returning from a signal handler.  If we return from our divide
167  * handler to a linuxthreads wrapper, we will lose the PC adjustment
168  * we made and return to the faulting instruction again.  Using
169  * syscall(SYS_sigaction) causes our handler to be called directly
170  * by the kernel, bypassing any wrappers.  */
171
172 #endif /* JAVA_SIGNAL_H */
173
174 #else /* __x86_64__ */
175
176 /* This is for the 32-bit subsystem on x86-64.  */
177
178 #define sigcontext_struct sigcontext
179 #include <java-signal-aux.h>
180
181 #endif /* __x86_64__ */