Gos-freebsd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
  3. This file is part of libunwind.
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <sys/ucontext.h>
  24. #include <machine/sigframe.h>
  25. #include <signal.h>
  26. #include <stddef.h>
  27. #include "unwind_i.h"
  28. #include "ucontext_i.h"
  29. PROTECTED int
  30. unw_is_signal_frame (unw_cursor_t *cursor)
  31. {
  32. /* XXXKIB */
  33. struct cursor *c = (struct cursor *) cursor;
  34. unw_word_t w0, w1, w2, b0, ip;
  35. unw_addr_space_t as;
  36. unw_accessors_t *a;
  37. void *arg;
  38. int ret;
  39. as = c->dwarf.as;
  40. a = unw_get_accessors (as);
  41. arg = c->dwarf.as_arg;
  42. /* Check if RIP points at sigreturn sequence.
  43. 48 8d 7c 24 10 lea SIGF_UC(%rsp),%rdi
  44. 6a 00 pushq $0
  45. 48 c7 c0 a1 01 00 00 movq $SYS_sigreturn,%rax
  46. 0f 05 syscall
  47. f4 0: hlt
  48. eb fd jmp 0b
  49. */
  50. ip = c->dwarf.ip;
  51. c->sigcontext_format = X86_64_SCF_NONE;
  52. if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0
  53. || (ret = (*a->access_mem) (as, ip + 8, &w1, 0, arg)) < 0
  54. || (ret = (*a->access_mem) (as, ip + 16, &w2, 0, arg)) < 0)
  55. return 0;
  56. w2 &= 0xffffff;
  57. if (w0 == 0x48006a10247c8d48 &&
  58. w1 == 0x050f000001a1c0c7 &&
  59. w2 == 0x0000000000fdebf4)
  60. {
  61. c->sigcontext_format = X86_64_SCF_FREEBSD_SIGFRAME;
  62. return (c->sigcontext_format);
  63. }
  64. /* Check if RIP points at standard syscall sequence.
  65. 49 89 ca mov %rcx,%r10
  66. 0f 05 syscall
  67. */
  68. if ((ret = (*a->access_mem) (as, ip - 5, &b0, 0, arg)) < 0)
  69. return (0);
  70. Debug (12, "b0 0x%lx\n", b0);
  71. if ((b0 & 0xffffffffffffff) == 0x050fca89490000 ||
  72. (b0 & 0xffffffffff) == 0x050fca8949)
  73. {
  74. c->sigcontext_format = X86_64_SCF_FREEBSD_SYSCALL;
  75. return (c->sigcontext_format);
  76. }
  77. return (X86_64_SCF_NONE);
  78. }
  79. PROTECTED int
  80. unw_handle_signal_frame (unw_cursor_t *cursor)
  81. {
  82. struct cursor *c = (struct cursor *) cursor;
  83. unw_word_t ucontext;
  84. int ret;
  85. if (c->sigcontext_format == X86_64_SCF_FREEBSD_SIGFRAME)
  86. {
  87. ucontext = c->dwarf.cfa + offsetof(struct sigframe, sf_uc);
  88. c->sigcontext_addr = c->dwarf.cfa;
  89. Debug(1, "signal frame, skip over trampoline\n");
  90. struct dwarf_loc rsp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
  91. ret = dwarf_get (&c->dwarf, rsp_loc, &c->dwarf.cfa);
  92. if (ret < 0)
  93. {
  94. Debug (2, "returning %d\n", ret);
  95. return ret;
  96. }
  97. c->dwarf.loc[RAX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RAX, 0);
  98. c->dwarf.loc[RDX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDX, 0);
  99. c->dwarf.loc[RCX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RCX, 0);
  100. c->dwarf.loc[RBX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBX, 0);
  101. c->dwarf.loc[RSI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSI, 0);
  102. c->dwarf.loc[RDI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDI, 0);
  103. c->dwarf.loc[RBP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBP, 0);
  104. c->dwarf.loc[RSP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
  105. c->dwarf.loc[ R8] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
  106. c->dwarf.loc[ R9] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
  107. c->dwarf.loc[R10] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
  108. c->dwarf.loc[R11] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
  109. c->dwarf.loc[R12] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
  110. c->dwarf.loc[R13] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
  111. c->dwarf.loc[R14] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
  112. c->dwarf.loc[R15] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
  113. c->dwarf.loc[RIP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RIP, 0);
  114. return 0;
  115. }
  116. else if (c->sigcontext_format == X86_64_SCF_FREEBSD_SYSCALL)
  117. {
  118. c->dwarf.loc[RCX] = c->dwarf.loc[R10];
  119. /* rsp_loc = DWARF_LOC(c->dwarf.cfa - 8, 0); */
  120. /* rbp_loc = c->dwarf.loc[RBP]; */
  121. c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
  122. ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
  123. Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
  124. (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
  125. (unsigned long long) c->dwarf.ip);
  126. if (ret < 0)
  127. {
  128. Debug (2, "returning %d\n", ret);
  129. return ret;
  130. }
  131. c->dwarf.cfa += 8;
  132. return 1;
  133. }
  134. else
  135. return -UNW_EBADFRAME;
  136. }
  137. #ifndef UNW_REMOTE_ONLY
  138. HIDDEN void *
  139. x86_64_r_uc_addr (ucontext_t *uc, int reg)
  140. {
  141. /* NOTE: common_init() in init.h inlines these for fast path access. */
  142. void *addr;
  143. switch (reg)
  144. {
  145. case UNW_X86_64_R8: addr = &uc->uc_mcontext.mc_r8; break;
  146. case UNW_X86_64_R9: addr = &uc->uc_mcontext.mc_r9; break;
  147. case UNW_X86_64_R10: addr = &uc->uc_mcontext.mc_r10; break;
  148. case UNW_X86_64_R11: addr = &uc->uc_mcontext.mc_r11; break;
  149. case UNW_X86_64_R12: addr = &uc->uc_mcontext.mc_r12; break;
  150. case UNW_X86_64_R13: addr = &uc->uc_mcontext.mc_r13; break;
  151. case UNW_X86_64_R14: addr = &uc->uc_mcontext.mc_r14; break;
  152. case UNW_X86_64_R15: addr = &uc->uc_mcontext.mc_r15; break;
  153. case UNW_X86_64_RDI: addr = &uc->uc_mcontext.mc_rdi; break;
  154. case UNW_X86_64_RSI: addr = &uc->uc_mcontext.mc_rsi; break;
  155. case UNW_X86_64_RBP: addr = &uc->uc_mcontext.mc_rbp; break;
  156. case UNW_X86_64_RBX: addr = &uc->uc_mcontext.mc_rbx; break;
  157. case UNW_X86_64_RDX: addr = &uc->uc_mcontext.mc_rdx; break;
  158. case UNW_X86_64_RAX: addr = &uc->uc_mcontext.mc_rax; break;
  159. case UNW_X86_64_RCX: addr = &uc->uc_mcontext.mc_rcx; break;
  160. case UNW_X86_64_RSP: addr = &uc->uc_mcontext.mc_rsp; break;
  161. case UNW_X86_64_RIP: addr = &uc->uc_mcontext.mc_rip; break;
  162. default:
  163. addr = NULL;
  164. }
  165. return addr;
  166. }
  167. HIDDEN NORETURN void
  168. x86_64_sigreturn (unw_cursor_t *cursor)
  169. {
  170. struct cursor *c = (struct cursor *) cursor;
  171. ucontext_t *uc = (ucontext_t *)(c->sigcontext_addr +
  172. offsetof(struct sigframe, sf_uc));
  173. Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
  174. (unsigned long long) c->dwarf.ip, uc);
  175. sigreturn(uc);
  176. abort();
  177. }
  178. #endif