Grbs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2005 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. /* Logically, we like to think of the stack as a contiguous region of
  22. memory. Unfortunately, this logical view doesn't work for the
  23. register backing store, because the RSE is an asynchronous engine and
  24. because UNIX/Linux allow for stack-switching via sigaltstack(2).
  25. Specifically, this means that any given stacked register may or may
  26. not be backed up by memory in the current stack. If not, then the
  27. backing memory may be found in any of the "more inner" (younger)
  28. stacks. The routines in this file help manage the discontiguous
  29. nature of the register backing store. The routines are completely
  30. independent of UNIX/Linux, but each stack frame that switches the
  31. backing store is expected to reserve 4 words for use by libunwind. For
  32. example, in the Linux sigcontext, sc_fr[0] and sc_fr[1] serve this
  33. purpose. */
  34. #include "unwind_i.h"
  35. #if UNW_DEBUG
  36. HIDDEN const char *
  37. ia64_strloc (ia64_loc_t loc)
  38. {
  39. static char buf[128];
  40. if (IA64_IS_NULL_LOC (loc))
  41. return "<null>";
  42. buf[0] = '\0';
  43. if (IA64_IS_MEMSTK_NAT (loc))
  44. strcat (buf, "memstk_nat(");
  45. if (IA64_IS_UC_LOC (loc))
  46. strcat (buf, "uc(");
  47. if (IA64_IS_FP_LOC (loc))
  48. strcat (buf, "fp(");
  49. if (IA64_IS_REG_LOC (loc))
  50. sprintf (buf + strlen (buf), "%s", unw_regname (IA64_GET_REG (loc)));
  51. else
  52. sprintf (buf + strlen (buf), "0x%llx",
  53. (unsigned long long) IA64_GET_ADDR (loc));
  54. if (IA64_IS_FP_LOC (loc))
  55. strcat (buf, ")");
  56. if (IA64_IS_UC_LOC (loc))
  57. strcat (buf, ")");
  58. if (IA64_IS_MEMSTK_NAT (loc))
  59. strcat (buf, ")");
  60. return buf;
  61. }
  62. #endif /* UNW_DEBUG */
  63. HIDDEN int
  64. rbs_switch (struct cursor *c,
  65. unw_word_t saved_bsp, unw_word_t saved_bspstore,
  66. ia64_loc_t saved_rnat_loc)
  67. {
  68. struct rbs_area *rbs = &c->rbs_area[c->rbs_curr];
  69. unw_word_t lo, ndirty, rbs_base;
  70. int ret;
  71. Debug (10, "(left=%u, curr=%u)\n", c->rbs_left_edge, c->rbs_curr);
  72. /* Calculate address "lo" at which the backing store starts: */
  73. ndirty = rse_num_regs (saved_bspstore, saved_bsp);
  74. lo = rse_skip_regs (c->bsp, -ndirty);
  75. rbs->size = (rbs->end - lo);
  76. /* If the previously-recorded rbs-area is empty we don't need to
  77. track it and we can simply overwrite it... */
  78. if (rbs->size)
  79. {
  80. Debug (10, "inner=[0x%lx-0x%lx)\n",
  81. (long) (rbs->end - rbs->size), (long) rbs->end);
  82. c->rbs_curr = (c->rbs_curr + 1) % ARRAY_SIZE (c->rbs_area);
  83. rbs = c->rbs_area + c->rbs_curr;
  84. if (c->rbs_curr == c->rbs_left_edge)
  85. c->rbs_left_edge = (c->rbs_left_edge + 1) % ARRAY_SIZE (c->rbs_area);
  86. }
  87. if ((ret = rbs_get_base (c, saved_bspstore, &rbs_base)) < 0)
  88. return ret;
  89. rbs->end = saved_bspstore;
  90. rbs->size = saved_bspstore - rbs_base;
  91. rbs->rnat_loc = saved_rnat_loc;
  92. c->bsp = saved_bsp;
  93. Debug (10, "outer=[0x%llx-0x%llx), rnat@%s\n", (long long) rbs_base,
  94. (long long) rbs->end, ia64_strloc (rbs->rnat_loc));
  95. return 0;
  96. }
  97. HIDDEN int
  98. rbs_find_stacked (struct cursor *c, unw_word_t regs_to_skip,
  99. ia64_loc_t *locp, ia64_loc_t *rnat_locp)
  100. {
  101. unw_word_t nregs, bsp = c->bsp, curr = c->rbs_curr, n;
  102. unw_word_t left_edge = c->rbs_left_edge;
  103. #if UNW_DEBUG
  104. int reg = 32 + regs_to_skip;
  105. #endif
  106. while (!rbs_contains (&c->rbs_area[curr], bsp))
  107. {
  108. if (curr == left_edge)
  109. {
  110. Debug (1, "could not find register r%d!\n", reg);
  111. return -UNW_EBADREG;
  112. }
  113. n = rse_num_regs (c->rbs_area[curr].end, bsp);
  114. curr = (curr + ARRAY_SIZE (c->rbs_area) - 1) % ARRAY_SIZE (c->rbs_area);
  115. bsp = rse_skip_regs (c->rbs_area[curr].end - c->rbs_area[curr].size, n);
  116. }
  117. while (1)
  118. {
  119. nregs = rse_num_regs (bsp, c->rbs_area[curr].end);
  120. if (regs_to_skip < nregs)
  121. {
  122. /* found it: */
  123. unw_word_t addr;
  124. addr = rse_skip_regs (bsp, regs_to_skip);
  125. if (locp)
  126. *locp = rbs_loc (c->rbs_area + curr, addr);
  127. if (rnat_locp)
  128. *rnat_locp = rbs_get_rnat_loc (c->rbs_area + curr, addr);
  129. return 0;
  130. }
  131. if (curr == left_edge)
  132. {
  133. Debug (1, "could not find register r%d!\n", reg);
  134. return -UNW_EBADREG;
  135. }
  136. regs_to_skip -= nregs;
  137. curr = (curr + ARRAY_SIZE (c->rbs_area) - 1) % ARRAY_SIZE (c->rbs_area);
  138. bsp = c->rbs_area[curr].end - c->rbs_area[curr].size;
  139. }
  140. }
  141. #ifdef NEED_RBS_COVER_AND_FLUSH
  142. static inline int
  143. get_rnat (struct cursor *c, struct rbs_area *rbs, unw_word_t bsp,
  144. unw_word_t *__restrict rnatp)
  145. {
  146. ia64_loc_t rnat_locp = rbs_get_rnat_loc (rbs, bsp);
  147. return ia64_get (c, rnat_locp, rnatp);
  148. }
  149. /* Simulate the effect of "cover" followed by a "flushrs" for the
  150. target-frame. However, since the target-frame's backing store
  151. may not have space for the registers that got spilled onto other
  152. rbs-areas, we save those registers to DIRTY_PARTITION where
  153. we can then load them via a single "loadrs".
  154. This function returns the size of the dirty-partition that was
  155. created or a negative error-code in case of error.
  156. Note: This does not modify the rbs_area[] structure in any way. */
  157. HIDDEN int
  158. rbs_cover_and_flush (struct cursor *c, unw_word_t nregs,
  159. unw_word_t *dirty_partition, unw_word_t *dirty_rnat,
  160. unw_word_t *bspstore)
  161. {
  162. unw_word_t n, src_mask, dst_mask, bsp, *dst, src_rnat, dst_rnat = 0;
  163. unw_word_t curr = c->rbs_curr, left_edge = c->rbs_left_edge;
  164. struct rbs_area *rbs = c->rbs_area + curr;
  165. int ret;
  166. bsp = c->bsp;
  167. c->bsp = rse_skip_regs (bsp, nregs);
  168. if (likely (rbs_contains (rbs, bsp)))
  169. {
  170. /* at least _some_ registers are on rbs... */
  171. n = rse_num_regs (bsp, rbs->end);
  172. if (likely (n >= nregs))
  173. {
  174. /* common case #1: all registers are on current rbs... */
  175. /* got lucky: _all_ registers are on rbs... */
  176. ia64_loc_t rnat_loc = rbs_get_rnat_loc (rbs, c->bsp);
  177. *bspstore = c->bsp;
  178. if (IA64_IS_REG_LOC (rnat_loc))
  179. {
  180. unw_word_t rnat_addr = (unw_word_t)
  181. tdep_uc_addr (c->as_arg, UNW_IA64_AR_RNAT, NULL);
  182. rnat_loc = IA64_LOC_ADDR (rnat_addr, 0);
  183. }
  184. c->loc[IA64_REG_RNAT] = rnat_loc;
  185. return 0; /* all done */
  186. }
  187. nregs -= n; /* account for registers already on the rbs */
  188. assert (rse_skip_regs (c->bsp, -nregs) == rse_skip_regs (rbs->end, 0));
  189. }
  190. else
  191. /* Earlier frames also didn't get spilled; need to "loadrs" those,
  192. too... */
  193. nregs += rse_num_regs (rbs->end, bsp);
  194. /* OK, we need to copy NREGS registers to the dirty partition. */
  195. *bspstore = bsp = rbs->end;
  196. c->loc[IA64_REG_RNAT] = rbs->rnat_loc;
  197. assert (!IA64_IS_REG_LOC (rbs->rnat_loc));
  198. dst = dirty_partition;
  199. while (nregs > 0)
  200. {
  201. if (unlikely (!rbs_contains (rbs, bsp)))
  202. {
  203. /* switch to next non-empty rbs-area: */
  204. do
  205. {
  206. if (curr == left_edge)
  207. {
  208. Debug (0, "rbs-underflow while flushing %lu regs, "
  209. "bsp=0x%lx, dst=0x%p\n", (unsigned long) nregs,
  210. (unsigned long) bsp, dst);
  211. return -UNW_EBADREG;
  212. }
  213. assert (rse_num_regs (rbs->end, bsp) == 0);
  214. curr = (curr + ARRAY_SIZE (c->rbs_area) - 1)
  215. % ARRAY_SIZE (c->rbs_area);
  216. rbs = c->rbs_area + curr;
  217. bsp = rbs->end - rbs->size;
  218. }
  219. while (rbs->size == 0);
  220. if ((ret = get_rnat (c, rbs, bsp, &src_rnat)) < 0)
  221. return ret;
  222. }
  223. if (unlikely (rse_is_rnat_slot (bsp)))
  224. {
  225. bsp += 8;
  226. if ((ret = get_rnat (c, rbs, bsp, &src_rnat)) < 0)
  227. return ret;
  228. }
  229. if (unlikely (rse_is_rnat_slot ((unw_word_t) dst)))
  230. {
  231. *dst++ = dst_rnat;
  232. dst_rnat = 0;
  233. }
  234. src_mask = ((unw_word_t) 1) << rse_slot_num (bsp);
  235. dst_mask = ((unw_word_t) 1) << rse_slot_num ((unw_word_t) dst);
  236. if (src_rnat & src_mask)
  237. dst_rnat |= dst_mask;
  238. else
  239. dst_rnat &= ~dst_mask;
  240. /* copy one slot: */
  241. if ((ret = ia64_get (c, rbs_loc (rbs, bsp), dst)) < 0)
  242. return ret;
  243. /* advance to next slot: */
  244. --nregs;
  245. bsp += 8;
  246. ++dst;
  247. }
  248. if (unlikely (rse_is_rnat_slot ((unw_word_t) dst)))
  249. {
  250. /* The LOADRS instruction loads "the N bytes below the current
  251. BSP" but BSP can never point to an RNaT slot so if the last
  252. destination word happens to be an RNaT slot, we need to write
  253. that slot now. */
  254. *dst++ = dst_rnat;
  255. dst_rnat = 0;
  256. }
  257. *dirty_rnat = dst_rnat;
  258. return (char *) dst - (char *) dirty_partition;
  259. }
  260. #endif /* !UNW_REMOTE_ONLY */