Gstep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2001-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. #include "offsets.h"
  22. #include "unwind_i.h"
  23. static inline int
  24. linux_sigtramp (struct cursor *c, ia64_loc_t prev_cfm_loc,
  25. unw_word_t *num_regsp)
  26. {
  27. #if defined(UNW_LOCAL_ONLY) && !defined(__linux)
  28. return -UNW_EINVAL;
  29. #else
  30. unw_word_t sc_addr;
  31. int ret;
  32. if ((ret = ia64_get (c, IA64_LOC_ADDR (c->sp + 0x10
  33. + LINUX_SIGFRAME_ARG2_OFF, 0),
  34. &sc_addr)) < 0)
  35. return ret;
  36. c->sigcontext_addr = sc_addr;
  37. if (!IA64_IS_REG_LOC (c->loc[IA64_REG_IP])
  38. && IA64_GET_ADDR (c->loc[IA64_REG_IP]) == sc_addr + LINUX_SC_BR_OFF + 8)
  39. {
  40. /* Linux kernels before 2.4.19 and 2.5.10 had buggy
  41. unwind info for sigtramp. Fix it up here. */
  42. c->loc[IA64_REG_IP] = IA64_LOC_ADDR (sc_addr + LINUX_SC_IP_OFF, 0);
  43. c->cfm_loc = IA64_LOC_ADDR (sc_addr + LINUX_SC_CFM_OFF, 0);
  44. }
  45. /* do what can't be described by unwind directives: */
  46. c->loc[IA64_REG_PFS] = IA64_LOC_ADDR (sc_addr + LINUX_SC_AR_PFS_OFF, 0);
  47. c->ec_loc = prev_cfm_loc;
  48. *num_regsp = c->cfm & 0x7f; /* size of frame */
  49. return 0;
  50. #endif
  51. }
  52. static inline int
  53. linux_interrupt (struct cursor *c, ia64_loc_t prev_cfm_loc,
  54. unw_word_t *num_regsp, int marker)
  55. {
  56. #if defined(UNW_LOCAL_ONLY) && !(defined(__linux) && defined(__KERNEL__))
  57. return -UNW_EINVAL;
  58. #else
  59. unw_word_t sc_addr, num_regs;
  60. ia64_loc_t pfs_loc;
  61. sc_addr = c->sigcontext_addr = c->sp + 0x10;
  62. if ((c->pr & (1UL << LINUX_PT_P_NONSYS)) != 0)
  63. num_regs = c->cfm & 0x7f;
  64. else
  65. num_regs = 0;
  66. /* do what can't be described by unwind directives: */
  67. if (marker == ABI_MARKER_OLD_LINUX_INTERRUPT)
  68. pfs_loc = IA64_LOC_ADDR (sc_addr + LINUX_OLD_PT_PFS_OFF, 0);
  69. else
  70. pfs_loc = IA64_LOC_ADDR (sc_addr + LINUX_PT_PFS_OFF, 0);
  71. c->loc[IA64_REG_PFS] = pfs_loc;
  72. c->ec_loc = prev_cfm_loc;
  73. *num_regsp = num_regs; /* size of frame */
  74. return 0;
  75. #endif
  76. }
  77. static inline int
  78. hpux_sigtramp (struct cursor *c, ia64_loc_t prev_cfm_loc,
  79. unw_word_t *num_regsp)
  80. {
  81. #if defined(UNW_LOCAL_ONLY) && !defined(__hpux)
  82. return -UNW_EINVAL;
  83. #else
  84. unw_word_t sc_addr, bsp, bspstore;
  85. ia64_loc_t sc_loc;
  86. int ret, i;
  87. /* HP-UX passes the address of ucontext_t in r32: */
  88. if ((ret = ia64_get_stacked (c, 32, &sc_loc, NULL)) < 0)
  89. return ret;
  90. if ((ret = ia64_get (c, sc_loc, &sc_addr)) < 0)
  91. return ret;
  92. c->sigcontext_addr = sc_addr;
  93. /* Now mark all (preserved) registers as coming from the
  94. signal context: */
  95. c->cfm_loc = IA64_LOC_UC_REG (UNW_IA64_CFM, sc_addr);
  96. c->loc[IA64_REG_PRI_UNAT_MEM] = IA64_NULL_LOC;
  97. c->loc[IA64_REG_PSP] = IA64_LOC_UC_REG (UNW_IA64_GR + 12, sc_addr);
  98. c->loc[IA64_REG_BSP] = IA64_LOC_UC_REG (UNW_IA64_AR_BSP, sc_addr);
  99. c->loc[IA64_REG_BSPSTORE] = IA64_LOC_UC_REG (UNW_IA64_AR_BSPSTORE, sc_addr);
  100. c->loc[IA64_REG_PFS] = IA64_LOC_UC_REG (UNW_IA64_AR_PFS, sc_addr);
  101. c->loc[IA64_REG_RNAT] = IA64_LOC_UC_REG (UNW_IA64_AR_RNAT, sc_addr);
  102. c->loc[IA64_REG_IP] = IA64_LOC_UC_REG (UNW_IA64_IP, sc_addr);
  103. c->loc[IA64_REG_R4] = IA64_LOC_UC_REG (UNW_IA64_GR + 4, sc_addr);
  104. c->loc[IA64_REG_R5] = IA64_LOC_UC_REG (UNW_IA64_GR + 5, sc_addr);
  105. c->loc[IA64_REG_R6] = IA64_LOC_UC_REG (UNW_IA64_GR + 6, sc_addr);
  106. c->loc[IA64_REG_R7] = IA64_LOC_UC_REG (UNW_IA64_GR + 7, sc_addr);
  107. c->loc[IA64_REG_NAT4] = IA64_LOC_UC_REG (UNW_IA64_NAT + 4, sc_addr);
  108. c->loc[IA64_REG_NAT5] = IA64_LOC_UC_REG (UNW_IA64_NAT + 5, sc_addr);
  109. c->loc[IA64_REG_NAT6] = IA64_LOC_UC_REG (UNW_IA64_NAT + 6, sc_addr);
  110. c->loc[IA64_REG_NAT7] = IA64_LOC_UC_REG (UNW_IA64_NAT + 7, sc_addr);
  111. c->loc[IA64_REG_UNAT] = IA64_LOC_UC_REG (UNW_IA64_AR_UNAT, sc_addr);
  112. c->loc[IA64_REG_PR] = IA64_LOC_UC_REG (UNW_IA64_PR, sc_addr);
  113. c->loc[IA64_REG_LC] = IA64_LOC_UC_REG (UNW_IA64_AR_LC, sc_addr);
  114. c->loc[IA64_REG_FPSR] = IA64_LOC_UC_REG (UNW_IA64_AR_FPSR, sc_addr);
  115. c->loc[IA64_REG_B1] = IA64_LOC_UC_REG (UNW_IA64_BR + 1, sc_addr);
  116. c->loc[IA64_REG_B2] = IA64_LOC_UC_REG (UNW_IA64_BR + 2, sc_addr);
  117. c->loc[IA64_REG_B3] = IA64_LOC_UC_REG (UNW_IA64_BR + 3, sc_addr);
  118. c->loc[IA64_REG_B4] = IA64_LOC_UC_REG (UNW_IA64_BR + 4, sc_addr);
  119. c->loc[IA64_REG_B5] = IA64_LOC_UC_REG (UNW_IA64_BR + 5, sc_addr);
  120. c->loc[IA64_REG_F2] = IA64_LOC_UC_REG (UNW_IA64_FR + 2, sc_addr);
  121. c->loc[IA64_REG_F3] = IA64_LOC_UC_REG (UNW_IA64_FR + 3, sc_addr);
  122. c->loc[IA64_REG_F4] = IA64_LOC_UC_REG (UNW_IA64_FR + 4, sc_addr);
  123. c->loc[IA64_REG_F5] = IA64_LOC_UC_REG (UNW_IA64_FR + 5, sc_addr);
  124. for (i = 0; i < 16; ++i)
  125. c->loc[IA64_REG_F16 + i] = IA64_LOC_UC_REG (UNW_IA64_FR + 16 + i, sc_addr);
  126. c->pi.flags |= UNW_PI_FLAG_IA64_RBS_SWITCH;
  127. /* update the CFM cache: */
  128. if ((ret = ia64_get (c, c->cfm_loc, &c->cfm)) < 0)
  129. return ret;
  130. /* update the PSP cache: */
  131. if ((ret = ia64_get (c, c->loc[IA64_REG_PSP], &c->psp)) < 0)
  132. return ret;
  133. if ((ret = ia64_get (c, c->loc[IA64_REG_BSP], &bsp)) < 0
  134. || (ret = ia64_get (c, c->loc[IA64_REG_BSPSTORE], &bspstore)) < 0)
  135. return ret;
  136. if (bspstore < bsp)
  137. /* Dirty partition got spilled into the ucontext_t structure
  138. itself. We'll need to access it via uc_access(3). */
  139. rbs_switch (c, bsp, bspstore, IA64_LOC_UC_ADDR (bsp | 0x1f8, 0));
  140. c->ec_loc = prev_cfm_loc;
  141. *num_regsp = 0;
  142. return 0;
  143. #endif
  144. }
  145. static inline int
  146. check_rbs_switch (struct cursor *c)
  147. {
  148. unw_word_t saved_bsp, saved_bspstore, loadrs, ndirty;
  149. int ret = 0;
  150. saved_bsp = c->bsp;
  151. if (c->pi.flags & UNW_PI_FLAG_IA64_RBS_SWITCH)
  152. {
  153. /* Got ourselves a frame that has saved ar.bspstore, ar.bsp,
  154. and ar.rnat, so we're all set for rbs-switching: */
  155. if ((ret = ia64_get (c, c->loc[IA64_REG_BSP], &saved_bsp)) < 0
  156. || (ret = ia64_get (c, c->loc[IA64_REG_BSPSTORE], &saved_bspstore)))
  157. return ret;
  158. }
  159. else if ((c->abi_marker == ABI_MARKER_LINUX_SIGTRAMP
  160. || c->abi_marker == ABI_MARKER_OLD_LINUX_SIGTRAMP)
  161. && !IA64_IS_REG_LOC (c->loc[IA64_REG_BSP])
  162. && (IA64_GET_ADDR (c->loc[IA64_REG_BSP])
  163. == c->sigcontext_addr + LINUX_SC_AR_BSP_OFF))
  164. {
  165. /* When Linux delivers a signal on an alternate stack, it
  166. does things a bit differently from what the unwind
  167. conventions allow us to describe: instead of saving
  168. ar.rnat, ar.bsp, and ar.bspstore, it saves the former two
  169. plus the "loadrs" value. Because of this, we need to
  170. detect & record a potential rbs-area switch
  171. manually... */
  172. /* If ar.bsp has been saved already AND the current bsp is
  173. not equal to the saved value, then we know for sure that
  174. we're past the point where the backing store has been
  175. switched (and before the point where it's restored). */
  176. if ((ret = ia64_get (c, IA64_LOC_ADDR (c->sigcontext_addr
  177. + LINUX_SC_AR_BSP_OFF, 0),
  178. &saved_bsp) < 0)
  179. || (ret = ia64_get (c, IA64_LOC_ADDR (c->sigcontext_addr
  180. + LINUX_SC_LOADRS_OFF, 0),
  181. &loadrs) < 0))
  182. return ret;
  183. loadrs >>= 16;
  184. ndirty = rse_num_regs (c->bsp - loadrs, c->bsp);
  185. saved_bspstore = rse_skip_regs (saved_bsp, -ndirty);
  186. }
  187. if (saved_bsp == c->bsp)
  188. return 0;
  189. return rbs_switch (c, saved_bsp, saved_bspstore, c->loc[IA64_REG_RNAT]);
  190. }
  191. static inline int
  192. update_frame_state (struct cursor *c)
  193. {
  194. unw_word_t prev_ip, prev_sp, prev_bsp, ip, num_regs;
  195. ia64_loc_t prev_cfm_loc;
  196. int ret;
  197. prev_cfm_loc = c->cfm_loc;
  198. prev_ip = c->ip;
  199. prev_sp = c->sp;
  200. prev_bsp = c->bsp;
  201. /* Update the IP cache (do this first: if we reach the end of the
  202. frame-chain, the rest of the info may not be valid/useful
  203. anymore. */
  204. ret = ia64_get (c, c->loc[IA64_REG_IP], &ip);
  205. if (ret < 0)
  206. return ret;
  207. c->ip = ip;
  208. if ((ip & 0xc) != 0)
  209. {
  210. /* don't let obviously bad addresses pollute the cache */
  211. Debug (1, "rejecting bad ip=0x%lx\n", (long) c->ip);
  212. return -UNW_EINVALIDIP;
  213. }
  214. c->cfm_loc = c->loc[IA64_REG_PFS];
  215. /* update the CFM cache: */
  216. ret = ia64_get (c, c->cfm_loc, &c->cfm);
  217. if (ret < 0)
  218. return ret;
  219. /* Normally, AR.EC is stored in the CFM save-location. That
  220. save-location contains the full function-state as defined by
  221. AR.PFS. However, interruptions only save the frame-marker, not
  222. any other info in CFM. Instead, AR.EC gets saved on the first
  223. call by the interruption-handler. Thus, interruption-related
  224. frames need to track the _previous_ CFM save-location since
  225. that's were AR.EC is saved. We support this by setting ec_loc to
  226. cfm_loc by default and giving frames marked with an ABI-marker
  227. the chance to override this value with prev_cfm_loc. */
  228. c->ec_loc = c->cfm_loc;
  229. num_regs = 0;
  230. if (unlikely (c->abi_marker))
  231. {
  232. c->last_abi_marker = c->abi_marker;
  233. switch (ia64_get_abi_marker (c))
  234. {
  235. case ABI_MARKER_LINUX_SIGTRAMP:
  236. case ABI_MARKER_OLD_LINUX_SIGTRAMP:
  237. ia64_set_abi (c, ABI_LINUX);
  238. if ((ret = linux_sigtramp (c, prev_cfm_loc, &num_regs)) < 0)
  239. return ret;
  240. break;
  241. case ABI_MARKER_OLD_LINUX_INTERRUPT:
  242. case ABI_MARKER_LINUX_INTERRUPT:
  243. ia64_set_abi (c, ABI_LINUX);
  244. if ((ret = linux_interrupt (c, prev_cfm_loc, &num_regs,
  245. c->abi_marker)) < 0)
  246. return ret;
  247. break;
  248. case ABI_MARKER_HP_UX_SIGTRAMP:
  249. ia64_set_abi (c, ABI_HPUX);
  250. if ((ret = hpux_sigtramp (c, prev_cfm_loc, &num_regs)) < 0)
  251. return ret;
  252. break;
  253. default:
  254. Debug (1, "unknown ABI marker: ABI=%u, context=%u\n",
  255. c->abi_marker >> 8, c->abi_marker & 0xff);
  256. return -UNW_EINVAL;
  257. }
  258. Debug (12, "sigcontext_addr=%lx (ret=%d)\n",
  259. (unsigned long) c->sigcontext_addr, ret);
  260. c->sigcontext_off = c->sigcontext_addr - c->sp;
  261. /* update the IP cache: */
  262. if ((ret = ia64_get (c, c->loc[IA64_REG_IP], &ip)) < 0)
  263. return ret;
  264. c->ip = ip;
  265. if (ip == 0)
  266. /* end of frame-chain reached */
  267. return 0;
  268. }
  269. else
  270. num_regs = (c->cfm >> 7) & 0x7f; /* size of locals */
  271. if (!IA64_IS_NULL_LOC (c->loc[IA64_REG_BSP]))
  272. {
  273. ret = check_rbs_switch (c);
  274. if (ret < 0)
  275. return ret;
  276. }
  277. c->bsp = rse_skip_regs (c->bsp, -num_regs);
  278. c->sp = c->psp;
  279. c->abi_marker = 0;
  280. if (c->ip == prev_ip && c->sp == prev_sp && c->bsp == prev_bsp)
  281. {
  282. Dprintf ("%s: ip, sp, and bsp unchanged; stopping here (ip=0x%lx)\n",
  283. __FUNCTION__, (long) ip);
  284. return -UNW_EBADFRAME;
  285. }
  286. /* as we unwind, the saved ar.unat becomes the primary unat: */
  287. c->loc[IA64_REG_PRI_UNAT_MEM] = c->loc[IA64_REG_UNAT];
  288. /* restore the predicates: */
  289. ret = ia64_get (c, c->loc[IA64_REG_PR], &c->pr);
  290. if (ret < 0)
  291. return ret;
  292. c->pi_valid = 0;
  293. return 0;
  294. }
  295. PROTECTED int
  296. unw_step (unw_cursor_t *cursor)
  297. {
  298. struct cursor *c = (struct cursor *) cursor;
  299. int ret;
  300. Debug (1, "(cursor=%p, ip=0x%016lx)\n", c, (unsigned long) c->ip);
  301. if ((ret = ia64_find_save_locs (c)) >= 0
  302. && (ret = update_frame_state (c)) >= 0)
  303. ret = (c->ip == 0) ? 0 : 1;
  304. Debug (2, "returning %d (ip=0x%016lx)\n", ret, (unsigned long) c->ip);
  305. return ret;
  306. }