Gos-linux.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2003 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #include "unwind_i.h"
  23. #include "ucontext_i.h"
  24. #include <sys/syscall.h>
  25. HIDDEN void
  26. tdep_fetch_frame (struct dwarf_cursor *dw, unw_word_t ip, int need_unwind_info)
  27. {
  28. struct cursor *c = (struct cursor *) dw;
  29. assert(! need_unwind_info || dw->pi_valid);
  30. assert(! need_unwind_info || dw->pi.unwind_info);
  31. if (dw->pi_valid
  32. && dw->pi.unwind_info
  33. && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
  34. c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
  35. else
  36. c->sigcontext_format = X86_64_SCF_NONE;
  37. Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
  38. dw->ip, dw->cfa, c->sigcontext_format);
  39. }
  40. HIDDEN void
  41. tdep_cache_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
  42. {
  43. struct cursor *c = (struct cursor *) dw;
  44. rs->signal_frame = c->sigcontext_format;
  45. Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
  46. dw->ip, dw->cfa, c->sigcontext_format);
  47. }
  48. HIDDEN void
  49. tdep_reuse_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
  50. {
  51. struct cursor *c = (struct cursor *) dw;
  52. c->sigcontext_format = rs->signal_frame;
  53. if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
  54. {
  55. c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
  56. /* Offset from cfa to ucontext_t in signal frame. */
  57. c->frame_info.cfa_reg_offset = 0;
  58. c->sigcontext_addr = dw->cfa;
  59. }
  60. else
  61. c->sigcontext_addr = 0;
  62. Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
  63. dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
  64. (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
  65. ? c->frame_info.cfa_reg_offset : 0));
  66. }
  67. PROTECTED int
  68. unw_is_signal_frame (unw_cursor_t *cursor)
  69. {
  70. struct cursor *c = (struct cursor *) cursor;
  71. return c->sigcontext_format != X86_64_SCF_NONE;
  72. }
  73. PROTECTED int
  74. unw_handle_signal_frame (unw_cursor_t *cursor)
  75. {
  76. #if UNW_DEBUG /* To silence compiler warnings */
  77. /* Should not get here because we now use kernel-provided dwarf
  78. information for the signal trampoline and dwarf_step() works.
  79. Hence unw_step() should never call this function. Maybe
  80. restore old non-dwarf signal handling here, but then the
  81. gating on unw_is_signal_frame() needs to be removed. */
  82. struct cursor *c = (struct cursor *) cursor;
  83. Debug(1, "old format signal frame? format=%d addr=0x%lx cfa=0x%lx\n",
  84. c->sigcontext_format, c->sigcontext_addr, c->dwarf.cfa);
  85. #endif
  86. return -UNW_EBADFRAME;
  87. }
  88. #ifndef UNW_REMOTE_ONLY
  89. HIDDEN void *
  90. x86_64_r_uc_addr (ucontext_t *uc, int reg)
  91. {
  92. /* NOTE: common_init() in init.h inlines these for fast path access. */
  93. void *addr;
  94. switch (reg)
  95. {
  96. case UNW_X86_64_R8: addr = &uc->uc_mcontext.gregs[REG_R8]; break;
  97. case UNW_X86_64_R9: addr = &uc->uc_mcontext.gregs[REG_R9]; break;
  98. case UNW_X86_64_R10: addr = &uc->uc_mcontext.gregs[REG_R10]; break;
  99. case UNW_X86_64_R11: addr = &uc->uc_mcontext.gregs[REG_R11]; break;
  100. case UNW_X86_64_R12: addr = &uc->uc_mcontext.gregs[REG_R12]; break;
  101. case UNW_X86_64_R13: addr = &uc->uc_mcontext.gregs[REG_R13]; break;
  102. case UNW_X86_64_R14: addr = &uc->uc_mcontext.gregs[REG_R14]; break;
  103. case UNW_X86_64_R15: addr = &uc->uc_mcontext.gregs[REG_R15]; break;
  104. case UNW_X86_64_RDI: addr = &uc->uc_mcontext.gregs[REG_RDI]; break;
  105. case UNW_X86_64_RSI: addr = &uc->uc_mcontext.gregs[REG_RSI]; break;
  106. case UNW_X86_64_RBP: addr = &uc->uc_mcontext.gregs[REG_RBP]; break;
  107. case UNW_X86_64_RBX: addr = &uc->uc_mcontext.gregs[REG_RBX]; break;
  108. case UNW_X86_64_RDX: addr = &uc->uc_mcontext.gregs[REG_RDX]; break;
  109. case UNW_X86_64_RAX: addr = &uc->uc_mcontext.gregs[REG_RAX]; break;
  110. case UNW_X86_64_RCX: addr = &uc->uc_mcontext.gregs[REG_RCX]; break;
  111. case UNW_X86_64_RSP: addr = &uc->uc_mcontext.gregs[REG_RSP]; break;
  112. case UNW_X86_64_RIP: addr = &uc->uc_mcontext.gregs[REG_RIP]; break;
  113. default:
  114. addr = NULL;
  115. }
  116. return addr;
  117. }
  118. /* sigreturn() is a no-op on x86_64 glibc. */
  119. HIDDEN NORETURN void
  120. x86_64_sigreturn (unw_cursor_t *cursor)
  121. {
  122. struct cursor *c = (struct cursor *) cursor;
  123. struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
  124. Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
  125. (unsigned long long) c->dwarf.ip, sc);
  126. __asm__ __volatile__ ("mov %0, %%rsp;"
  127. "mov %1, %%rax;"
  128. "syscall"
  129. :: "r"(sc), "i"(SYS_rt_sigreturn)
  130. : "memory");
  131. abort();
  132. }
  133. #endif