aix-unwind.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* DWARF2 EH unwinding support for AIX.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Useful register numbers. */
  20. #define R_LR 65
  21. #define R_CR2 70
  22. #define R_XER 76
  23. #define R_FIRST_ALTIVEC 77
  24. #define R_VRSAVE 109
  25. #define R_VSCR 110
  26. /* If the current unwind info (FS) does not contain explicit info
  27. saving R2, then we have to do a minor amount of code reading to
  28. figure out if it was saved. The big problem here is that the
  29. code that does the save/restore is generated by the linker, so
  30. we have no good way to determine at compile time what to do. */
  31. #ifdef __64BIT__
  32. #define MD_FROB_UPDATE_CONTEXT(CTX, FS) \
  33. do { \
  34. if ((FS)->regs.reg[2].how == REG_UNSAVED) \
  35. { \
  36. unsigned int *insn \
  37. = (unsigned int *) \
  38. _Unwind_GetGR ((CTX), R_LR); \
  39. if (*insn == 0xE8410028) \
  40. _Unwind_SetGRPtr ((CTX), 2, (CTX)->cfa + 40); \
  41. } \
  42. } while (0)
  43. #else
  44. #define MD_FROB_UPDATE_CONTEXT(CTX, FS) \
  45. do { \
  46. if ((FS)->regs.reg[2].how == REG_UNSAVED) \
  47. { \
  48. unsigned int *insn \
  49. = (unsigned int *) \
  50. _Unwind_GetGR ((CTX), R_LR); \
  51. if (*insn == 0x80410014) \
  52. _Unwind_SetGRPtr ((CTX), 2, (CTX)->cfa + 20); \
  53. } \
  54. } while (0)
  55. #endif
  56. /* Now on to MD_FALLBACK_FRAME_STATE_FOR.
  57. 32bit AIX 5.2, 5.3, 6.1, 7.X and
  58. 64bit AIX 6.1, 7.X only at this stage. */
  59. #include <stdlib.h>
  60. #include <stddef.h>
  61. #include <signal.h>
  62. #include <sys/machine.h>
  63. #ifdef __64BIT__
  64. typedef struct __context64 mstate_t;
  65. #else
  66. typedef struct mstsave mstate_t;
  67. #endif
  68. #define MD_FALLBACK_FRAME_STATE_FOR ppc_aix_fallback_frame_state
  69. /* If we are compiling on AIX < 5.3, the VMX related datastructs are not
  70. defined and we take measures to obtain proper runtime behavior if the
  71. compiled code happens to run on a later version with VMX enabled. */
  72. #ifndef MSR_VMX
  73. #define MSR_VMX 0x2000000
  74. #endif
  75. typedef unsigned int uint;
  76. typedef struct { uint v[4]; } vreg_t;
  77. typedef struct {
  78. vreg_t regs[32];
  79. uint pad1 [3];
  80. uint vscr;
  81. uint vrsave;
  82. uint pad2 [3];
  83. } vstate_t;
  84. #define EXT_CONTEXT_MARK 0x45435458
  85. #define EXT_CONTEXT_SIZE 4096
  86. #define BUMPER_SIZE (EXT_CONTEXT_SIZE - sizeof(vstate_t) - (5 * sizeof(int)))
  87. typedef struct {
  88. uint pad1 [4];
  89. vstate_t vstate;
  90. char bumper [BUMPER_SIZE];
  91. int mark;
  92. } extended_context_t;
  93. typedef struct {
  94. char bumper [offsetof (ucontext_t, uc_stack) + sizeof (stack_t)];
  95. extended_context_t * ectx;
  96. int mark;
  97. } vmx_ucontext_t;
  98. /* Determine whether CONTEXT designates a signal handler, and return the
  99. associated ucontext_t address if so. Return NULL otherwise. */
  100. static ucontext_t *
  101. ucontext_for (struct _Unwind_Context *context)
  102. {
  103. const unsigned int * ra = context->ra;
  104. /* AIX 5.2, 5.3, 6.1 and 7.X, threaded or not, share common patterns
  105. and feature variants depending on the configured kernel (unix_mp
  106. or unix_64). */
  107. #ifdef __64BIT__
  108. if (*(ra - 5) == 0x4c00012c /* isync */
  109. && *(ra - 4) == 0xe8ec0000 /* ld r7,0(r12) */
  110. && *(ra - 3) == 0xe84c0008 /* ld r2,8(r12) */
  111. && *(ra - 2) == 0x7ce903a6 /* mtctr r7 */
  112. && *(ra - 1) == 0x4e800421 /* bctrl */
  113. && *(ra - 0) == 0x7de27b78) /* mr r2,r15 <-- context->ra */
  114. {
  115. /* unix_64 */
  116. if (*(ra - 6) == 0x7d000164) /* mtmsrd r8 */
  117. {
  118. /* AIX 6.1, 7.1 and 7.2 */
  119. return (ucontext_t *)(context->cfa + 0x70);
  120. }
  121. }
  122. #else
  123. if (*(ra - 5) == 0x4c00012c /* isync */
  124. && *(ra - 4) == 0x80ec0000 /* lwz r7,0(r12) */
  125. && *(ra - 3) == 0x804c0004 /* lwz r2,4(r12) */
  126. && *(ra - 2) == 0x7ce903a6 /* mtctr r7 */
  127. && *(ra - 1) == 0x4e800421 /* bctrl */
  128. && *(ra - 0) == 0x7dc37378) /* mr r3,r14 <-- context->ra */
  129. {
  130. /* unix_64 */
  131. if (*(ra - 6) == 0x7d000164) /* mtmsrd r8 */
  132. {
  133. switch (*(ra + 18))
  134. {
  135. /* AIX 5.2 */
  136. case 0x835a0520: /* lwz r26,1312(r26) */
  137. return (ucontext_t *)(context->cfa + 0x70);
  138. /* AIX 5.3 */
  139. case 0x835a0570: /* lwz r26,1392(r26) */
  140. return (ucontext_t *)(context->cfa + 0x40);
  141. /* AIX 6.1 and 7.1 */
  142. case 0x2c1a0000: /* cmpwi r26,0 */
  143. return (ucontext_t *)(context->cfa + 0x40);
  144. /* AIX 7.2 */
  145. case 0x3800000a: /* li r0,A */
  146. return (ucontext_t *)(context->cfa + 0x40);
  147. default:
  148. return 0;
  149. }
  150. }
  151. /* unix_mp */
  152. if (*(ra - 6) == 0x7d000124) /* mtmsr r8 */
  153. {
  154. typedef struct {
  155. char pad[56];
  156. ucontext_t ucontext;
  157. siginfo_t siginfo;
  158. } aix52_stack_t;
  159. aix52_stack_t * frame = (aix52_stack_t *) context->cfa;
  160. return &frame->ucontext;
  161. }
  162. }
  163. #endif
  164. return 0;
  165. }
  166. /* The fallback proper. */
  167. #ifdef __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__
  168. #define RETURN_COLUMN __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__
  169. #else
  170. #define RETURN_COLUMN ARG_POINTER_REGNUM
  171. #endif
  172. #define REGISTER_CFA_OFFSET_FOR(FS,REGNO,ADDR,CFA)\
  173. do { \
  174. (FS)->regs.reg[REGNO].how = REG_SAVED_OFFSET; \
  175. (FS)->regs.reg[REGNO].loc.offset = (long) (ADDR) - (CFA); \
  176. } while (0)
  177. static _Unwind_Reason_Code
  178. ppc_aix_fallback_frame_state (struct _Unwind_Context *context,
  179. _Unwind_FrameState *fs)
  180. {
  181. ucontext_t * uctx = ucontext_for (context);
  182. mstate_t * mctx;
  183. long new_cfa;
  184. int i;
  185. if (uctx == NULL)
  186. return _URC_END_OF_STACK;
  187. mctx = &uctx->uc_mcontext.jmp_context;
  188. /* The "kernel" frame cfa is the stack pointer at the signal occurrence
  189. point. */
  190. new_cfa = mctx->gpr[__LIBGCC_STACK_POINTER_REGNUM__];
  191. fs->regs.cfa_how = CFA_REG_OFFSET;
  192. fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
  193. fs->regs.cfa_offset = new_cfa - (long) context->cfa;
  194. /* And we state how to find the various registers it has saved with
  195. relative offset rules from there. */
  196. for (i = 0; i < 32; i++)
  197. if (i != __LIBGCC_STACK_POINTER_REGNUM__)
  198. REGISTER_CFA_OFFSET_FOR (fs, i, &mctx->gpr[i], new_cfa);
  199. REGISTER_CFA_OFFSET_FOR (fs, R_CR2, &mctx->cr, new_cfa);
  200. REGISTER_CFA_OFFSET_FOR (fs, R_XER, &mctx->xer, new_cfa);
  201. REGISTER_CFA_OFFSET_FOR (fs, R_LR, &mctx->lr, new_cfa);
  202. fs->retaddr_column = RETURN_COLUMN;
  203. REGISTER_CFA_OFFSET_FOR (fs, RETURN_COLUMN, &mctx->iar, new_cfa);
  204. fs->signal_frame = 1;
  205. /* Honor FP Ever Used ... */
  206. if (mctx->fpeu)
  207. {
  208. for (i = 0; i < 32; i++)
  209. REGISTER_CFA_OFFSET_FOR (fs, i+32, &mctx->fpr[i], new_cfa);
  210. }
  211. /* Honor VMX context, if any. We expect the msr bit never to be set in
  212. environments where there is no VMX support, e.g. on AIX < 5.3. */
  213. if (mctx->msr & MSR_VMX)
  214. {
  215. vmx_ucontext_t * uc = (vmx_ucontext_t *) uctx;
  216. if (uc->mark == EXT_CONTEXT_MARK && uc->ectx->mark == EXT_CONTEXT_MARK)
  217. {
  218. vstate_t * vstate = &uc->ectx->vstate;
  219. for (i = 0; i < 32; i++)
  220. REGISTER_CFA_OFFSET_FOR
  221. (fs, i+R_FIRST_ALTIVEC, &vstate->regs[i], new_cfa);
  222. REGISTER_CFA_OFFSET_FOR (fs, R_VSCR, &vstate->vscr, new_cfa);
  223. REGISTER_CFA_OFFSET_FOR (fs, R_VRSAVE, &vstate->vrsave, new_cfa);
  224. }
  225. }
  226. return _URC_NO_REASON;
  227. }