]> git.ipfire.org Git - thirdparty/gcc.git/blame - libjava/include/x86_64-signal.h
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / libjava / include / x86_64-signal.h
CommitLineData
4e1fff64 1// x86_64-signal.h - Catch runtime signals and turn them into exceptions
2// on an x86_64 based GNU/Linux system.
3
71e45bc2 4/* Copyright (C) 2003, 2006, 2007, 2012 Free Software Foundation
4e1fff64 5
6 This file is part of libgcj.
7
8This software is copyrighted work licensed under the terms of the
9Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10details. */
11
12
a4fe931d 13#ifdef __x86_64__
14
4e1fff64 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
70f661cc 22#define HANDLE_FPE 1
23
24#define SIGNAL_HANDLER(_name) \
25static void _Jv_##_name (int, siginfo_t *, \
26 void *_p __attribute__ ((__unused__)))
27
28#define HANDLE_DIVIDE_OVERFLOW \
29do \
30{ \
31 struct ucontext *_uc = (struct ucontext *)_p; \
a1fefa1d 32 gregset_t &_gregs = _uc->uc_mcontext.gregs; \
33 unsigned char *_rip = (unsigned char *)_gregs[REG_RIP]; \
70f661cc 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 \
b64804e9 50 /* Skip 67h address size prefix. */ \
0e02c032 51 if (_rip[0] == 0x67) \
aea51dc8 52 _rip++; \
53 \
70f661cc 54 if ((_rip[0] & 0xf0) == 0x40) /* REX byte present. */ \
55 { \
56 unsigned char _rex = _rip[0] & 0x0f; \
57 _is_64_bit = (_rex & 0x08) != 0; \
58 _rip++; \
59 } \
60 \
61 /* Detect a signed division of Integer.MIN_VALUE or Long.MIN_VALUE. */ \
62 if (_rip[0] == 0xf7) \
63 { \
64 bool _min_value_dividend = false; \
65 unsigned char _modrm = _rip[1]; \
66 \
67 if (((_modrm >> 3) & 7) == 7) \
68 { \
69 if (_is_64_bit) \
a1fefa1d 70 _min_value_dividend = \
aea51dc8 71 _gregs[REG_RAX] == (greg_t)0x8000000000000000ULL; \
70f661cc 72 else \
a1fefa1d 73 _min_value_dividend = \
aea51dc8 74 (_gregs[REG_RAX] & 0xffffffff) == (greg_t)0x80000000ULL; \
70f661cc 75 } \
76 \
77 if (_min_value_dividend) \
78 { \
79 unsigned char _rm = _modrm & 7; \
a1fefa1d 80 _gregs[REG_RDX] = 0; /* the remainder is zero */ \
70f661cc 81 switch (_modrm >> 6) \
82 { \
83 case 0: /* register indirect */ \
84 if (_rm == 5) /* 32-bit displacement */ \
85 _rip += 4; \
86 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
87 _rip += 1; \
88 break; \
89 case 1: /* register indirect + 8-bit displacement */ \
90 _rip += 1; \
91 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
92 _rip += 1; \
93 break; \
94 case 2: /* register indirect + 32-bit displacement */ \
95 _rip += 4; \
96 if (_rm == 4) /* A SIB byte follows the ModR/M byte */ \
97 _rip += 1; \
98 break; \
99 case 3: \
100 break; \
101 } \
102 _rip += 2; \
a1fefa1d 103 _gregs[REG_RIP] = (greg_t)_rip; \
70f661cc 104 return; \
105 } \
106 } \
107} \
108while (0)
4e1fff64 109
110extern "C"
111{
112 struct kernel_sigaction
113 {
114 void (*k_sa_sigaction)(int,siginfo_t *,void *);
115 unsigned long k_sa_flags;
116 void (*k_sa_restorer) (void);
117 sigset_t k_sa_mask;
118 };
119}
120
15c73eb7 121#define MAKE_THROW_FRAME(_exception)
4e1fff64 122
123#define RESTORE(name, syscall) RESTORE2 (name, syscall)
124#define RESTORE2(name, syscall) \
125asm \
126 ( \
ccc173db 127 ".text\n" \
4e1fff64 128 ".byte 0 # Yes, this really is necessary\n" \
129 ".align 16\n" \
130 "__" #name ":\n" \
131 " movq $" #syscall ", %rax\n" \
132 " syscall\n" \
133 );
134
135/* The return code for realtime-signals. */
136RESTORE (restore_rt, __NR_rt_sigreturn)
fdcfb10d 137void restore_rt (void) asm ("__restore_rt")
138 __attribute__ ((visibility ("hidden")));
4e1fff64 139
140#define INIT_SEGV \
141do \
142 { \
4e1fff64 143 struct kernel_sigaction act; \
144 act.k_sa_sigaction = _Jv_catch_segv; \
145 sigemptyset (&act.k_sa_mask); \
146 act.k_sa_flags = SA_SIGINFO|0x4000000; \
147 act.k_sa_restorer = restore_rt; \
148 syscall (SYS_rt_sigaction, SIGSEGV, &act, NULL, _NSIG / 8); \
149 } \
150while (0)
151
70f661cc 152#define INIT_FPE \
153do \
154 { \
155 struct kernel_sigaction act; \
156 act.k_sa_sigaction = _Jv_catch_fpe; \
157 sigemptyset (&act.k_sa_mask); \
158 act.k_sa_flags = SA_SIGINFO|0x4000000; \
159 act.k_sa_restorer = restore_rt; \
160 syscall (SYS_rt_sigaction, SIGFPE, &act, NULL, _NSIG / 8); \
161 } \
162while (0)
163
164/* You might wonder why we use syscall(SYS_sigaction) in INIT_FPE
165 * instead of the standard sigaction(). This is necessary because of
166 * the shenanigans above where we increment the PC saved in the
167 * context and then return. This trick will only work when we are
168 * called _directly_ by the kernel, because linuxthreads wraps signal
169 * handlers and its wrappers do not copy the sigcontext struct back
170 * when returning from a signal handler. If we return from our divide
171 * handler to a linuxthreads wrapper, we will lose the PC adjustment
172 * we made and return to the faulting instruction again. Using
173 * syscall(SYS_sigaction) causes our handler to be called directly
174 * by the kernel, bypassing any wrappers. */
4e1fff64 175
a4fe931d 176#endif /* JAVA_SIGNAL_H */
d8a53361 177
a4fe931d 178#else /* __x86_64__ */
d8a53361 179
42b36080 180/* This is for the 32-bit subsystem on x86-64. */
d8a53361 181
a4fe931d 182#define sigcontext_struct sigcontext
183#include <java-signal-aux.h>
d8a53361 184
185#endif /* __x86_64__ */