vms-unwind.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Fallback frame unwinding for Alpha/VMS.
  2. Copyright (C) 1996-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
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) 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. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <vms/pdscdef.h>
  22. #include <vms/libicb.h>
  23. #include <vms/chfctxdef.h>
  24. #include <vms/chfdef.h>
  25. #define MD_FALLBACK_FRAME_STATE_FOR alpha_vms_fallback_frame_state
  26. typedef void * ADDR;
  27. typedef unsigned long long REG;
  28. typedef PDSCDEF * PV;
  29. #define REG_AT(addr) (*(REG *)(addr))
  30. #define ADDR_AT(addr) (*(ADDR *)(addr))
  31. /* Compute pointer to procedure descriptor (Procedure Value) from Frame
  32. Pointer FP, according to the rules in [ABI-3.5.1 Current Procedure]. */
  33. #define PV_FOR(FP) \
  34. (((FP) != 0) \
  35. ? (((REG_AT (FP) & 0x7) == 0) ? *(PDSCDEF **)(FP) : (PDSCDEF *)(FP)) : 0)
  36. extern int SYS$GL_CALL_HANDL;
  37. /* This is actually defined as a "long", but in system code where longs
  38. are always 4bytes while GCC longs might be 8bytes. */
  39. #define UPDATE_FS_FOR_CFA_GR(FS, GRN, LOC, CFA) \
  40. do { \
  41. (FS)->regs.reg[GRN].how = REG_SAVED_OFFSET; \
  42. (FS)->regs.reg[GRN].loc.offset = (_Unwind_Sword) ((REG) (LOC) - (REG) (CFA)); \
  43. } while (0);
  44. #define GIVEUP_ON_FAILURE(STATUS) \
  45. { if ((((STATUS) & 1) != 1)) return _URC_END_OF_STACK; }
  46. #define DENOTES_EXC_DISPATCHER(PV) ((PV) == (ADDR) (REG) SYS$GL_CALL_HANDL)
  47. #define RA_COLUMN (__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__)
  48. static int
  49. alpha_vms_fallback_frame_state (struct _Unwind_Context *context,
  50. _Unwind_FrameState *fs)
  51. {
  52. static int eh_debug = -1;
  53. /* Our goal is to update FS to reflect the state one step up CONTEXT, that
  54. is: the CFA, return address and *saved* registers locations associated
  55. with the function designated by CONTEXT->ra. We are called when the
  56. libgcc unwinder has not found any dwarf FDE for this address, which
  57. typically happens when trying to propagate a language exception through a
  58. signal global vector or frame based handler.
  59. The CONTEXT->reg[] entries reflect the state/location of register saves
  60. so designate values live at the CONTEXT->ra point. Of precious value to
  61. us here is the frame pointer (r29), which gets us a procedure value. */
  62. PV pv = (context->reg[29] != 0) ? PV_FOR (ADDR_AT (context->reg[29])) : 0;
  63. int pkind = pv ? pv->pdsc$w_flags & 0xf : 0;
  64. /* VMS procedure kind, as indicated by the procedure descriptor. We only
  65. know how to deal with FP_STACK or FP_REGISTER here. */
  66. ADDR new_cfa = 0;
  67. /* CFA we will establish for the caller, computed in different ways,
  68. e.g. depending whether we cross an exception dispatcher frame. */
  69. CHFCTX *chfctx = 0;
  70. /* Pointer to the VMS CHF context associated with an exception dispatcher
  71. frame, if we happen to come across one. */
  72. int i,j;
  73. if (eh_debug == -1)
  74. {
  75. char * eh_debug_env = getenv ("EH_DEBUG");
  76. eh_debug = eh_debug_env ? atoi (eh_debug_env) : 0;
  77. }
  78. if (eh_debug)
  79. printf ("MD_FALLBACK running ...\n");
  80. /* We only know how to deal with stack or reg frame procedures, so give
  81. up if we're handed anything else. */
  82. if (pkind != PDSC$K_KIND_FP_STACK && pkind != PDSC$K_KIND_FP_REGISTER)
  83. return _URC_END_OF_STACK;
  84. if (eh_debug)
  85. printf ("FALLBACK: CTX FP = 0x%p, PV = 0x%p, EN = 0x%llx, RA = 0x%p\n",
  86. ADDR_AT (context->reg[29]), pv, pv->pdsc$q_entry, context->ra);
  87. fs->retaddr_column = RA_COLUMN;
  88. /* If PV designates a VMS exception vector or condition handler, we need to
  89. do as if the caller was the signaling point and estabish the state of the
  90. intermediate VMS code (CFA, RA and saved register locations) as if it was
  91. a single regular function. This requires special processing.
  92. The datastructures available from an condition dispatcher frame (signal
  93. context) do not contain the values of most callee-saved registers, so
  94. whathever PV designates, we need to account for the registers it saves.
  95. Besides, we need to express all the locations with respect to a
  96. consistent CFA value, so we compute this first. */
  97. if (DENOTES_EXC_DISPATCHER (pv))
  98. {
  99. /* The CFA to establish is the signaling point's stack pointer. We
  100. compute it using the system invocation context unwinding services and
  101. save the CHF context data pointer along the way for later uses. */
  102. INVO_CONTEXT_BLK icb;
  103. int status, invo_handle;
  104. if (eh_debug)
  105. printf ("FALLBACK: SYS$HANDLER\n");
  106. icb.libicb$q_ireg [29] = REG_AT (context->reg[29]);
  107. icb.libicb$q_ireg [30] = 0;
  108. invo_handle = LIB$GET_INVO_HANDLE (&icb);
  109. status = LIB$GET_INVO_CONTEXT (invo_handle, &icb);
  110. GIVEUP_ON_FAILURE (status);
  111. chfctx = (CHFCTX *) icb.libicb$ph_chfctx_addr;
  112. status = LIB$GET_PREV_INVO_CONTEXT (&icb);
  113. GIVEUP_ON_FAILURE (status);
  114. new_cfa = (ADDR) icb.libicb$q_ireg[30];
  115. }
  116. else
  117. {
  118. /* The CFA to establish is the SP value on entry of the procedure
  119. designated by PV, which we compute as the corresponding frame base
  120. register value + frame size. Note that the frame base may differ
  121. from CONTEXT->cfa, typically if the caller has performed dynamic
  122. stack allocations. */
  123. int base_reg = pv->pdsc$w_flags & PDSC$M_BASE_REG_IS_FP ? 29 : 30;
  124. ADDR base_addr = ADDR_AT (context->reg[base_reg]);
  125. new_cfa = base_addr + pv->pdsc$l_size;
  126. }
  127. /* State to compute the caller's CFA by adding an offset to the current
  128. one in CONTEXT. */
  129. fs->regs.cfa_how = CFA_REG_OFFSET;
  130. fs->regs.cfa_reg = __builtin_dwarf_sp_column ();
  131. fs->regs.cfa_offset = new_cfa - context->cfa;
  132. /* Regular unwind first, accounting for the register saves performed by
  133. the procedure designated by PV. */
  134. switch (pkind)
  135. {
  136. case PDSC$K_KIND_FP_STACK:
  137. {
  138. /* The saved registers are all located in the Register Save Area,
  139. except for the procedure value register (R27) found at the frame
  140. base address. */
  141. int base_reg = pv->pdsc$w_flags & PDSC$M_BASE_REG_IS_FP ? 29 : 30;
  142. ADDR base_addr = ADDR_AT (context->reg[base_reg]);
  143. ADDR rsa_addr = base_addr + pv->pdsc$w_rsa_offset;
  144. if (eh_debug)
  145. printf ("FALLBACK: STACK frame procedure\n");
  146. UPDATE_FS_FOR_CFA_GR (fs, 27, base_addr, new_cfa);
  147. /* The first RSA entry is for the return address register, R26. */
  148. UPDATE_FS_FOR_CFA_GR (fs, 26, rsa_addr, new_cfa);
  149. UPDATE_FS_FOR_CFA_GR (fs, RA_COLUMN, rsa_addr, new_cfa);
  150. /* The following entries are for registers marked as saved according
  151. to ireg_mask. */
  152. for (i = 0, j = 0; i < 32; i++)
  153. if ((1 << i) & pv->pdsc$l_ireg_mask)
  154. UPDATE_FS_FOR_CFA_GR (fs, i, rsa_addr + 8 * ++j, new_cfa);
  155. /* ??? floating point registers ? */
  156. break;
  157. }
  158. case PDSC$K_KIND_FP_REGISTER:
  159. {
  160. if (eh_debug)
  161. printf ("FALLBACK: REGISTER frame procedure\n");
  162. fs->regs.reg[RA_COLUMN].how = REG_SAVED_REG;
  163. fs->regs.reg[RA_COLUMN].loc.reg = pv->pdsc$b_save_ra;
  164. fs->regs.reg[29].how = REG_SAVED_REG;
  165. fs->regs.reg[29].loc.reg = pv->pdsc$b_save_fp;
  166. break;
  167. }
  168. default:
  169. /* Should never reach here. */
  170. return _URC_END_OF_STACK;
  171. }
  172. /* If PV designates an exception dispatcher, we have to adjust the return
  173. address column to get at the signal occurrence point, and account for
  174. what the CHF context contains. */
  175. if (DENOTES_EXC_DISPATCHER (pv))
  176. {
  177. /* The PC of the instruction causing the condition is available from the
  178. signal argument vector. Extra saved register values are available
  179. from the mechargs array. */
  180. CHF$SIGNAL_ARRAY *sigargs
  181. = (CHF$SIGNAL_ARRAY *) chfctx->chfctx$q_sigarglst;
  182. CHF$MECH_ARRAY *mechargs
  183. = (CHF$MECH_ARRAY *) chfctx->chfctx$q_mcharglst;
  184. ADDR condpc_addr
  185. = &((int *)(&sigargs->chf$l_sig_name)) [sigargs->chf$is_sig_args-2];
  186. ADDR rei_frame_addr = (void *) mechargs->chf$q_mch_esf_addr;
  187. /* Adjust the return address location. */
  188. UPDATE_FS_FOR_CFA_GR (fs, RA_COLUMN, condpc_addr, new_cfa);
  189. /* The frame pointer at the condition point is available from the
  190. chf context directly. */
  191. UPDATE_FS_FOR_CFA_GR (fs, 29, &chfctx->chfctx$q_expt_fp, new_cfa);
  192. /* Registers available from the mechargs array. */
  193. UPDATE_FS_FOR_CFA_GR (fs, 0, &mechargs->chf$q_mch_savr0, new_cfa);
  194. UPDATE_FS_FOR_CFA_GR (fs, 1, &mechargs->chf$q_mch_savr1, new_cfa);
  195. UPDATE_FS_FOR_CFA_GR (fs, 16, &mechargs->chf$q_mch_savr16, new_cfa);
  196. UPDATE_FS_FOR_CFA_GR (fs, 17, &mechargs->chf$q_mch_savr17, new_cfa);
  197. UPDATE_FS_FOR_CFA_GR (fs, 18, &mechargs->chf$q_mch_savr18, new_cfa);
  198. UPDATE_FS_FOR_CFA_GR (fs, 19, &mechargs->chf$q_mch_savr19, new_cfa);
  199. UPDATE_FS_FOR_CFA_GR (fs, 20, &mechargs->chf$q_mch_savr20, new_cfa);
  200. UPDATE_FS_FOR_CFA_GR (fs, 21, &mechargs->chf$q_mch_savr21, new_cfa);
  201. UPDATE_FS_FOR_CFA_GR (fs, 22, &mechargs->chf$q_mch_savr22, new_cfa);
  202. UPDATE_FS_FOR_CFA_GR (fs, 23, &mechargs->chf$q_mch_savr23, new_cfa);
  203. UPDATE_FS_FOR_CFA_GR (fs, 24, &mechargs->chf$q_mch_savr24, new_cfa);
  204. UPDATE_FS_FOR_CFA_GR (fs, 25, &mechargs->chf$q_mch_savr25, new_cfa);
  205. UPDATE_FS_FOR_CFA_GR (fs, 26, &mechargs->chf$q_mch_savr26, new_cfa);
  206. UPDATE_FS_FOR_CFA_GR (fs, 27, &mechargs->chf$q_mch_savr27, new_cfa);
  207. UPDATE_FS_FOR_CFA_GR (fs, 28, &mechargs->chf$q_mch_savr28, new_cfa);
  208. /* Registers R2 to R7 are available from the rei frame pointer. */
  209. for (i = 2; i <= 7; i ++)
  210. UPDATE_FS_FOR_CFA_GR (fs, i, rei_frame_addr+(i - 2)*8, new_cfa);
  211. /* ??? floating point registers ? */
  212. }
  213. fs->signal_frame = 1;
  214. return _URC_NO_REASON;
  215. }