Gresume.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2001-2004 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include <stdlib.h>
  22. #include "unwind_i.h"
  23. #include "offsets.h"
  24. #ifndef UNW_REMOTE_ONLY
  25. static inline int
  26. local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
  27. {
  28. #if defined(__linux)
  29. unw_word_t dirty_partition[2048]; /* AR.RSC.LOADRS is a 14-bit field */
  30. unw_word_t val, sol, sof, pri_unat, n, pfs, bspstore, dirty_rnat;
  31. struct cursor *c = (struct cursor *) cursor;
  32. struct
  33. {
  34. unw_word_t r1;
  35. unw_word_t r4;
  36. unw_word_t r5;
  37. unw_word_t r6;
  38. unw_word_t r7;
  39. unw_word_t r15;
  40. unw_word_t r16;
  41. unw_word_t r17;
  42. unw_word_t r18;
  43. }
  44. extra;
  45. int ret, dirty_size;
  46. # define GET_NAT(n) \
  47. do \
  48. { \
  49. ret = tdep_access_reg (c, UNW_IA64_NAT + (n), &val, 0); \
  50. if (ret < 0) \
  51. return ret; \
  52. if (val) \
  53. pri_unat |= (unw_word_t) 1 << n; \
  54. } \
  55. while (0)
  56. /* ensure c->pi is up-to-date: */
  57. if ((ret = ia64_make_proc_info (c)) < 0)
  58. return ret;
  59. /* Copy contents of r4-r7 into "extra", so that their values end up
  60. contiguous, so we can use a single (primary-) UNaT value. */
  61. if ((ret = ia64_get (c, c->loc[IA64_REG_R4], &extra.r4)) < 0
  62. || (ret = ia64_get (c, c->loc[IA64_REG_R5], &extra.r5)) < 0
  63. || (ret = ia64_get (c, c->loc[IA64_REG_R6], &extra.r6)) < 0
  64. || (ret = ia64_get (c, c->loc[IA64_REG_R7], &extra.r7)) < 0)
  65. return ret;
  66. /* Form the primary UNaT value: */
  67. pri_unat = 0;
  68. GET_NAT (4); GET_NAT(5);
  69. GET_NAT (6); GET_NAT(7);
  70. n = (((uintptr_t) &extra.r4) / 8 - 4) % 64;
  71. pri_unat = (pri_unat << n) | (pri_unat >> (64 - n));
  72. if (unlikely (c->sigcontext_addr))
  73. {
  74. struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
  75. # define PR_SCRATCH 0xffc0 /* p6-p15 are scratch */
  76. # define PR_PRESERVED (~(PR_SCRATCH | 1))
  77. /* We're returning to a frame that was (either directly or
  78. indirectly) interrupted by a signal. We have to restore
  79. _both_ "preserved" and "scratch" registers. That doesn't
  80. leave us any registers to work with, and the only way we can
  81. achieve this is by doing a sigreturn().
  82. Note: it might be tempting to think that we don't have to
  83. restore the scratch registers when returning to a frame that
  84. was indirectly interrupted by a signal. However, that is not
  85. safe because that frame and its descendants could have been
  86. using a special convention that stores "preserved" state in
  87. scratch registers. For example, the Linux fsyscall
  88. convention does this with r11 (to save ar.pfs) and b6 (to
  89. save "rp"). */
  90. sc->sc_gr[12] = c->psp;
  91. c->psp = c->sigcontext_addr - c->sigcontext_off;
  92. sof = (c->cfm & 0x7f);
  93. if ((dirty_size = rbs_cover_and_flush (c, sof, dirty_partition,
  94. &dirty_rnat, &bspstore)) < 0)
  95. return dirty_size;
  96. /* Clear the "in-syscall" flag, because in general we won't be
  97. returning to the interruption-point and we need all registers
  98. restored. */
  99. sc->sc_flags &= ~IA64_SC_FLAG_IN_SYSCALL;
  100. sc->sc_ip = c->ip;
  101. sc->sc_cfm = c->cfm & (((unw_word_t) 1 << 38) - 1);
  102. sc->sc_pr = (c->pr & ~PR_SCRATCH) | (sc->sc_pr & ~PR_PRESERVED);
  103. if ((ret = ia64_get (c, c->loc[IA64_REG_PFS], &sc->sc_ar_pfs)) < 0
  104. || (ret = ia64_get (c, c->loc[IA64_REG_FPSR], &sc->sc_ar_fpsr)) < 0
  105. || (ret = ia64_get (c, c->loc[IA64_REG_UNAT], &sc->sc_ar_unat)) < 0)
  106. return ret;
  107. sc->sc_gr[1] = c->pi.gp;
  108. if (c->eh_valid_mask & 0x1) sc->sc_gr[15] = c->eh_args[0];
  109. if (c->eh_valid_mask & 0x2) sc->sc_gr[16] = c->eh_args[1];
  110. if (c->eh_valid_mask & 0x4) sc->sc_gr[17] = c->eh_args[2];
  111. if (c->eh_valid_mask & 0x8) sc->sc_gr[18] = c->eh_args[3];
  112. Debug (9, "sc: r15=%lx,r16=%lx,r17=%lx,r18=%lx\n",
  113. (long) sc->sc_gr[15], (long) sc->sc_gr[16],
  114. (long) sc->sc_gr[17], (long) sc->sc_gr[18]);
  115. }
  116. else
  117. {
  118. /* Account for the fact that _Uia64_install_context() will
  119. return via br.ret, which will decrement bsp by size-of-locals. */
  120. if ((ret = ia64_get (c, c->loc[IA64_REG_PFS], &pfs)) < 0)
  121. return ret;
  122. sol = (pfs >> 7) & 0x7f;
  123. if ((dirty_size = rbs_cover_and_flush (c, sol, dirty_partition,
  124. &dirty_rnat, &bspstore)) < 0)
  125. return dirty_size;
  126. extra.r1 = c->pi.gp;
  127. extra.r15 = c->eh_args[0];
  128. extra.r16 = c->eh_args[1];
  129. extra.r17 = c->eh_args[2];
  130. extra.r18 = c->eh_args[3];
  131. Debug (9, "extra: r15=%lx,r16=%lx,r17=%lx,r18=%lx\n",
  132. (long) extra.r15, (long) extra.r16,
  133. (long) extra.r17, (long) extra.r18);
  134. }
  135. Debug (8, "resuming at ip=%lx\n", (long) c->ip);
  136. ia64_install_cursor (c, pri_unat, (unw_word_t *) &extra,
  137. bspstore, dirty_size, dirty_partition + dirty_size/8,
  138. dirty_rnat);
  139. #elif defined(__hpux)
  140. struct cursor *c = (struct cursor *) cursor;
  141. setcontext (c->as_arg); /* should not return */
  142. #endif
  143. return -UNW_EINVAL;
  144. }
  145. HIDDEN int
  146. ia64_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
  147. {
  148. return local_resume (as, cursor, arg);
  149. }
  150. #endif /* !UNW_REMOTE_ONLY */
  151. #ifndef UNW_LOCAL_ONLY
  152. static inline int
  153. remote_install_cursor (struct cursor *c)
  154. {
  155. int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
  156. int write, void *);
  157. int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
  158. int write, void *);
  159. unw_fpreg_t fpval;
  160. unw_word_t val;
  161. int reg;
  162. #if defined(__linux) && !defined(UNW_REMOTE_ONLY)
  163. if (c->as == unw_local_addr_space)
  164. {
  165. /* Take a short-cut: we directly resume out of the cursor and
  166. all we need to do is make sure that all locations point to
  167. memory, not registers. Furthermore, R4-R7 and NAT4-NAT7 are
  168. taken care of by ia64_local_resume() so they don't need to be
  169. handled here. */
  170. # define MEMIFY(preg, reg) \
  171. do { \
  172. if (IA64_IS_REG_LOC (c->loc[(preg)])) \
  173. c->loc[(preg)] = IA64_LOC_ADDR ((unw_word_t) \
  174. tdep_uc_addr(c->as_arg, (reg), \
  175. NULL), 0); \
  176. } while (0)
  177. MEMIFY (IA64_REG_PR, UNW_IA64_PR);
  178. MEMIFY (IA64_REG_PFS, UNW_IA64_AR_PFS);
  179. MEMIFY (IA64_REG_RNAT, UNW_IA64_AR_RNAT);
  180. MEMIFY (IA64_REG_UNAT, UNW_IA64_AR_UNAT);
  181. MEMIFY (IA64_REG_LC, UNW_IA64_AR_LC);
  182. MEMIFY (IA64_REG_FPSR, UNW_IA64_AR_FPSR);
  183. MEMIFY (IA64_REG_IP, UNW_IA64_BR + 0);
  184. MEMIFY (IA64_REG_B1, UNW_IA64_BR + 1);
  185. MEMIFY (IA64_REG_B2, UNW_IA64_BR + 2);
  186. MEMIFY (IA64_REG_B3, UNW_IA64_BR + 3);
  187. MEMIFY (IA64_REG_B4, UNW_IA64_BR + 4);
  188. MEMIFY (IA64_REG_B5, UNW_IA64_BR + 5);
  189. MEMIFY (IA64_REG_F2, UNW_IA64_FR + 2);
  190. MEMIFY (IA64_REG_F3, UNW_IA64_FR + 3);
  191. MEMIFY (IA64_REG_F4, UNW_IA64_FR + 4);
  192. MEMIFY (IA64_REG_F5, UNW_IA64_FR + 5);
  193. MEMIFY (IA64_REG_F16, UNW_IA64_FR + 16);
  194. MEMIFY (IA64_REG_F17, UNW_IA64_FR + 17);
  195. MEMIFY (IA64_REG_F18, UNW_IA64_FR + 18);
  196. MEMIFY (IA64_REG_F19, UNW_IA64_FR + 19);
  197. MEMIFY (IA64_REG_F20, UNW_IA64_FR + 20);
  198. MEMIFY (IA64_REG_F21, UNW_IA64_FR + 21);
  199. MEMIFY (IA64_REG_F22, UNW_IA64_FR + 22);
  200. MEMIFY (IA64_REG_F23, UNW_IA64_FR + 23);
  201. MEMIFY (IA64_REG_F24, UNW_IA64_FR + 24);
  202. MEMIFY (IA64_REG_F25, UNW_IA64_FR + 25);
  203. MEMIFY (IA64_REG_F26, UNW_IA64_FR + 26);
  204. MEMIFY (IA64_REG_F27, UNW_IA64_FR + 27);
  205. MEMIFY (IA64_REG_F28, UNW_IA64_FR + 28);
  206. MEMIFY (IA64_REG_F29, UNW_IA64_FR + 29);
  207. MEMIFY (IA64_REG_F30, UNW_IA64_FR + 30);
  208. MEMIFY (IA64_REG_F31, UNW_IA64_FR + 31);
  209. }
  210. else
  211. #endif /* __linux && !UNW_REMOTE_ONLY */
  212. {
  213. access_reg = c->as->acc.access_reg;
  214. access_fpreg = c->as->acc.access_fpreg;
  215. Debug (8, "copying out cursor state\n");
  216. for (reg = 0; reg <= UNW_REG_LAST; ++reg)
  217. {
  218. if (unw_is_fpreg (reg))
  219. {
  220. if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
  221. (*access_fpreg) (c->as, reg, &fpval, 1, c->as_arg);
  222. }
  223. else
  224. {
  225. if (tdep_access_reg (c, reg, &val, 0) >= 0)
  226. (*access_reg) (c->as, reg, &val, 1, c->as_arg);
  227. }
  228. }
  229. }
  230. return (*c->as->acc.resume) (c->as, (unw_cursor_t *) c, c->as_arg);
  231. }
  232. #endif /* !UNW_LOCAL_ONLY */
  233. PROTECTED int
  234. unw_resume (unw_cursor_t *cursor)
  235. {
  236. struct cursor *c = (struct cursor *) cursor;
  237. Debug (1, "(cursor=%p, ip=0x%016lx)\n", c, (unsigned long) c->ip);
  238. #ifdef UNW_LOCAL_ONLY
  239. return local_resume (c->as, cursor, c->as_arg);
  240. #else
  241. return remote_install_cursor (c);
  242. #endif
  243. }