x86_64-signal.h 5.6 KB

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