OSDN Git Service

Remove conflict indicator.
[pf3gnuchains/gcc-fork.git] / libjava / include / i386-signal.h
1 // i386-signal.h - Catch runtime signals and turn them into exceptions.
2
3 /* Copyright (C) 1998, 1999, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 /* This technique should work for all i386 based Unices which conform
12  * to iBCS2.  This includes all versions of Linux more recent than 1.3 
13  */
14
15
16 #ifndef JAVA_SIGNAL_H
17 #define JAVA_SIGNAL_H 1
18
19 #include <signal.h>
20 #include <sys/syscall.h>
21
22 #define HANDLE_SEGV 1
23 #define HANDLE_FPE 1
24
25 #define SIGNAL_HANDLER(_name)   \
26 static void _name (int _dummy)
27
28 #define MAKE_THROW_FRAME(_exception)                                    \
29 do                                                                      \
30 {                                                                       \
31   void **_p = (void **)&_dummy;                                         \
32   struct sigcontext_struct *_regs = (struct sigcontext_struct *)++_p;   \
33                                                                         \
34   /* Advance the program counter so that it is after the start of the   \
35      instruction:  the x86 exception handler expects                    \
36      the PC to point to the instruction after a call. */                \
37   _regs->eip += 2;                                                      \
38                                                                         \
39 }                                                                       \
40 while (0)
41
42 #define HANDLE_DIVIDE_OVERFLOW                                          \
43 do                                                                      \
44 {                                                                       \
45   void **_p = (void **)&_dummy;                                         \
46   struct sigcontext_struct *_regs = (struct sigcontext_struct *)++_p;   \
47                                                                         \
48   register unsigned char *_eip = (unsigned char *)_regs->eip;           \
49                                                                         \
50   /* According to the JVM spec, "if the dividend is the negative        \
51    * integer of the smallest magnitude and the divisor is -1, then      \
52    * overflow occurs and the result is equal to the dividend.  Despite  \
53    * the overflow, no exception occurs".                                \
54                                                                         \
55    * We handle this by inspecting the instruction which generated the   \
56    * signal and advancing eip to point to the following instruction.    \
57    * As the instructions are variable length it is necessary to do a    \
58    * little calculation to figure out where the following instruction   \
59    * actually is.                                                       \
60                                                                         \
61    */                                                                   \
62                                                                         \
63   if (_eip[0] == 0xf7)                                                  \
64     {                                                                   \
65       unsigned char _modrm = _eip[1];                                   \
66                                                                         \
67       if (_regs->eax == 0x80000000                                      \
68           && ((_modrm >> 3) & 7) == 7) /* Signed divide */              \
69         {                                                               \
70           _regs->edx = 0; /* the remainder is zero */                   \
71           switch (_modrm >> 6)                                          \
72             {                                                           \
73             case 0:                                                     \
74               if ((_modrm & 7) == 5)                                    \
75                 _eip += 4;                                              \
76               break;                                                    \
77             case 1:                                                     \
78               _eip += 1;                                                \
79               break;                                                    \
80             case 2:                                                     \
81               _eip += 4;                                                \
82               break;                                                    \
83             case 3:                                                     \
84               break;                                                    \
85             }                                                           \
86           _eip += 2;                                                    \
87           _regs->eip = (unsigned long)_eip;                             \
88           return;                                                       \
89         }                                                               \
90       else                                                              \
91         {                                                               \
92           /* Advance the program counter so that it is after the start  \
93              of the instruction: this is because the x86 exception      \
94              handler expects the PC to point to the instruction after a \
95              call. */                                                   \
96           _regs->eip += 2;                                              \
97         }                                                               \
98     }                                                                   \
99 }                                                                       \
100 while (0)
101
102 #define INIT_SEGV                                               \
103 do                                                              \
104   {                                                             \
105     nullp = new java::lang::NullPointerException ();            \
106     struct sigaction act;                                       \
107     act.sa_handler = catch_segv;                                \
108     sigemptyset (&act.sa_mask);                                 \
109     act.sa_flags = 0;                                           \
110     syscall (SYS_sigaction, SIGSEGV, &act, NULL);               \
111   }                                                             \
112 while (0)  
113
114 #define INIT_FPE                                                \
115 do                                                              \
116   {                                                             \
117     arithexception = new java::lang::ArithmeticException        \
118       (JvNewStringLatin1 ("/ by zero"));                        \
119     struct sigaction act;                                       \
120     act.sa_handler = catch_fpe;                                 \
121     sigemptyset (&act.sa_mask);                                 \
122     act.sa_flags = 0;                                           \
123     syscall (SYS_sigaction, SIGFPE, &act, NULL);                \
124   }                                                             \
125 while (0)  
126
127 /* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
128  * instead of the standard sigaction().  This is necessary because of
129  * the shenanigans above where we increment the PC saved in the
130  * context and then return.  This trick will only work when we are
131  * called _directly_ by the kernel, because linuxthreads wraps signal
132  * handlers and its wrappers do not copy the sigcontext struct back
133  * when returning from a signal handler.  If we return from our divide
134  * handler to a linuxthreads wrapper, we will lose the PC adjustment
135  * we made and return to the faulting instruction again.  Using
136  * syscall(SYS_sigaction) causes our handler to be called directly by
137  * the kernel, bypassing any wrappers.  This is a kludge, and a future
138  * version of this handler will do something better.  */
139
140 #endif /* JAVA_SIGNAL_H */
141