linux-unwind.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  2. Contributed by ARM Ltd.
  3. This file is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This file is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. /* Always include AArch64 unwinder header file. */
  19. #include "config/aarch64/aarch64-unwind.h"
  20. #ifndef inhibit_libc
  21. #include <signal.h>
  22. #include <sys/ucontext.h>
  23. /* Since insns are always stored LE, on a BE system the opcodes will
  24. be loaded byte-reversed. Therefore, define two sets of opcodes,
  25. one for LE and one for BE. */
  26. #if __AARCH64EB__
  27. #define MOVZ_X8_8B 0x681180d2
  28. #define SVC_0 0x010000d4
  29. #else
  30. #define MOVZ_X8_8B 0xd2801168
  31. #define SVC_0 0xd4000001
  32. #endif
  33. #define MD_FALLBACK_FRAME_STATE_FOR aarch64_fallback_frame_state
  34. static _Unwind_Reason_Code
  35. aarch64_fallback_frame_state (struct _Unwind_Context *context,
  36. _Unwind_FrameState * fs)
  37. {
  38. /* The kernel creates an rt_sigframe on the stack immediately prior
  39. to delivering a signal.
  40. This structure must have the same shape as the linux kernel
  41. equivalent. */
  42. struct rt_sigframe
  43. {
  44. siginfo_t info;
  45. ucontext_t uc;
  46. };
  47. struct rt_sigframe *rt_;
  48. _Unwind_Ptr new_cfa;
  49. unsigned *pc = context->ra;
  50. struct sigcontext *sc;
  51. struct _aarch64_ctx *extension_marker;
  52. int i;
  53. /* A signal frame will have a return address pointing to
  54. __default_sa_restorer. This code is hardwired as:
  55. 0xd2801168 movz x8, #0x8b
  56. 0xd4000001 svc 0x0
  57. */
  58. if (pc[0] != MOVZ_X8_8B || pc[1] != SVC_0)
  59. {
  60. return _URC_END_OF_STACK;
  61. }
  62. rt_ = context->cfa;
  63. sc = &rt_->uc.uc_mcontext;
  64. /* This define duplicates the definition in aarch64.md */
  65. #define SP_REGNUM 31
  66. new_cfa = (_Unwind_Ptr) sc;
  67. fs->regs.cfa_how = CFA_REG_OFFSET;
  68. fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
  69. fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
  70. for (i = 0; i < AARCH64_DWARF_NUMBER_R; i++)
  71. {
  72. fs->regs.reg[AARCH64_DWARF_R0 + i].how = REG_SAVED_OFFSET;
  73. fs->regs.reg[AARCH64_DWARF_R0 + i].loc.offset =
  74. (_Unwind_Ptr) & (sc->regs[i]) - new_cfa;
  75. }
  76. /* The core context may be extended with an arbitrary set of
  77. additional contexts appended sequentially. Each additional
  78. context contains a magic identifier and size in bytes. The size
  79. field can be used to skip over unrecognized context extensions.
  80. The end of the context sequence is marked by a context with magic
  81. 0 or size 0. */
  82. for (extension_marker = (struct _aarch64_ctx *) &sc->__reserved;
  83. extension_marker->magic;
  84. extension_marker = (struct _aarch64_ctx *)
  85. ((unsigned char *) extension_marker + extension_marker->size))
  86. {
  87. if (extension_marker->magic == FPSIMD_MAGIC)
  88. {
  89. struct fpsimd_context *ctx =
  90. (struct fpsimd_context *) extension_marker;
  91. int i;
  92. for (i = 0; i < AARCH64_DWARF_NUMBER_V; i++)
  93. {
  94. _Unwind_Sword offset;
  95. fs->regs.reg[AARCH64_DWARF_V0 + i].how = REG_SAVED_OFFSET;
  96. /* sigcontext contains 32 128bit registers for V0 to
  97. V31. The kernel will have saved the contents of the
  98. V registers. We want to unwind the callee save D
  99. registers. Each D register comprises the least
  100. significant half of the corresponding V register. We
  101. need to offset into the saved V register dependent on
  102. our endianness to find the saved D register. */
  103. offset = (_Unwind_Ptr) & (ctx->vregs[i]) - new_cfa;
  104. /* The endianness adjustment code below expects that a
  105. saved V register is 16 bytes. */
  106. gcc_assert (sizeof (ctx->vregs[0]) == 16);
  107. #if defined (__AARCH64EB__)
  108. offset = offset + 8;
  109. #endif
  110. fs->regs.reg[AARCH64_DWARF_V0 + i].loc.offset = offset;
  111. }
  112. }
  113. else
  114. {
  115. /* There is context provided that we do not recognize! */
  116. }
  117. }
  118. fs->regs.reg[31].how = REG_SAVED_OFFSET;
  119. fs->regs.reg[31].loc.offset = (_Unwind_Ptr) & (sc->sp) - new_cfa;
  120. fs->signal_frame = 1;
  121. fs->regs.reg[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__].how =
  122. REG_SAVED_VAL_OFFSET;
  123. fs->regs.reg[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__].loc.offset =
  124. (_Unwind_Ptr) (sc->pc) - new_cfa;
  125. fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__;
  126. return _URC_NO_REASON;
  127. }
  128. #endif