Ginit.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002 Hewlett-Packard Co
  3. Copyright (C) 2007 David Mosberger-Tang
  4. Contributed by David Mosberger-Tang <dmosberger@gmail.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. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "unwind_i.h"
  28. #ifdef UNW_REMOTE_ONLY
  29. /* unw_local_addr_space is a NULL pointer in this case. */
  30. PROTECTED unw_addr_space_t unw_local_addr_space;
  31. #else /* !UNW_REMOTE_ONLY */
  32. static struct unw_addr_space local_addr_space;
  33. PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
  34. # ifdef UNW_LOCAL_ONLY
  35. HIDDEN void *
  36. tdep_uc_addr (ucontext_t *uc, int reg)
  37. {
  38. return x86_r_uc_addr (uc, reg);
  39. }
  40. # endif /* UNW_LOCAL_ONLY */
  41. HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
  42. /* XXX fix me: there is currently no way to locate the dyn-info list
  43. by a remote unwinder. On ia64, this is done via a special
  44. unwind-table entry. Perhaps something similar can be done with
  45. DWARF2 unwind info. */
  46. static void
  47. put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
  48. {
  49. /* it's a no-op */
  50. }
  51. static int
  52. get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
  53. void *arg)
  54. {
  55. *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
  56. return 0;
  57. }
  58. #undef PAGE_SIZE
  59. #define PAGE_SIZE 4096
  60. #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
  61. /* Cache of already validated addresses */
  62. #define NLGA 4
  63. static unw_word_t last_good_addr[NLGA];
  64. static int lga_victim;
  65. static int
  66. validate_mem (unw_word_t addr)
  67. {
  68. int i, victim;
  69. #ifdef HAVE_MINCORE
  70. unsigned char mvec[2]; /* Unaligned access may cross page boundary */
  71. #endif
  72. size_t len;
  73. if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
  74. len = PAGE_SIZE;
  75. else
  76. len = PAGE_SIZE * 2;
  77. addr = PAGE_START(addr);
  78. if (addr == 0)
  79. return -1;
  80. for (i = 0; i < NLGA; i++)
  81. {
  82. if (last_good_addr[i] && (addr == last_good_addr[i]))
  83. return 0;
  84. }
  85. #ifdef HAVE_MINCORE
  86. if (mincore ((void *) addr, len, mvec) == -1)
  87. #else
  88. if (msync ((void *) addr, len, MS_ASYNC) == -1)
  89. #endif
  90. return -1;
  91. victim = lga_victim;
  92. for (i = 0; i < NLGA; i++) {
  93. if (!last_good_addr[victim]) {
  94. last_good_addr[victim] = addr;
  95. return 0;
  96. }
  97. victim = (victim + 1) % NLGA;
  98. }
  99. /* All slots full. Evict the victim. */
  100. last_good_addr[victim] = addr;
  101. victim = (victim + 1) % NLGA;
  102. lga_victim = victim;
  103. return 0;
  104. }
  105. static int
  106. access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
  107. void *arg)
  108. {
  109. if (write)
  110. {
  111. Debug (16, "mem[%x] <- %x\n", addr, *val);
  112. *(unw_word_t *) addr = *val;
  113. }
  114. else
  115. {
  116. /* validate address */
  117. const struct cursor *c = (const struct cursor *)arg;
  118. if (c && c->validate && validate_mem(addr))
  119. return -1;
  120. *val = *(unw_word_t *) addr;
  121. Debug (16, "mem[%x] -> %x\n", addr, *val);
  122. }
  123. return 0;
  124. }
  125. static int
  126. access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
  127. void *arg)
  128. {
  129. unw_word_t *addr;
  130. ucontext_t *uc = ((struct cursor *)arg)->uc;
  131. if (unw_is_fpreg (reg))
  132. goto badreg;
  133. if (!(addr = x86_r_uc_addr (uc, reg)))
  134. goto badreg;
  135. if (write)
  136. {
  137. *(unw_word_t *) addr = *val;
  138. Debug (12, "%s <- %x\n", unw_regname (reg), *val);
  139. }
  140. else
  141. {
  142. *val = *(unw_word_t *) addr;
  143. Debug (12, "%s -> %x\n", unw_regname (reg), *val);
  144. }
  145. return 0;
  146. badreg:
  147. Debug (1, "bad register number %u\n", reg);
  148. return -UNW_EBADREG;
  149. }
  150. static int
  151. access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
  152. int write, void *arg)
  153. {
  154. ucontext_t *uc = ((struct cursor *)arg)->uc;
  155. unw_fpreg_t *addr;
  156. if (!unw_is_fpreg (reg))
  157. goto badreg;
  158. if (!(addr = x86_r_uc_addr (uc, reg)))
  159. goto badreg;
  160. if (write)
  161. {
  162. Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
  163. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  164. *(unw_fpreg_t *) addr = *val;
  165. }
  166. else
  167. {
  168. *val = *(unw_fpreg_t *) addr;
  169. Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
  170. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  171. }
  172. return 0;
  173. badreg:
  174. Debug (1, "bad register number %u\n", reg);
  175. /* attempt to access a non-preserved register */
  176. return -UNW_EBADREG;
  177. }
  178. static int
  179. get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
  180. char *buf, size_t buf_len, unw_word_t *offp,
  181. void *arg)
  182. {
  183. #if HAVE_SGX
  184. return -1;
  185. #else
  186. return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
  187. #endif
  188. }
  189. HIDDEN void
  190. x86_local_addr_space_init (void)
  191. {
  192. memset (&local_addr_space, 0, sizeof (local_addr_space));
  193. local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
  194. local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
  195. local_addr_space.acc.put_unwind_info = put_unwind_info;
  196. local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
  197. local_addr_space.acc.access_mem = access_mem;
  198. local_addr_space.acc.access_reg = access_reg;
  199. local_addr_space.acc.access_fpreg = access_fpreg;
  200. local_addr_space.acc.resume = x86_local_resume;
  201. local_addr_space.acc.get_proc_name = get_static_proc_name;
  202. unw_flush_cache (&local_addr_space, 0, 0);
  203. }
  204. #endif /* !UNW_REMOTE_ONLY */