Ginit.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
  6. This file is part of libunwind.
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/mman.h>
  29. #include "unwind_i.h"
  30. #ifdef UNW_REMOTE_ONLY
  31. /* unw_local_addr_space is a NULL pointer in this case. */
  32. PROTECTED unw_addr_space_t unw_local_addr_space;
  33. #else /* !UNW_REMOTE_ONLY */
  34. static struct unw_addr_space local_addr_space;
  35. PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
  36. HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
  37. /* XXX fix me: there is currently no way to locate the dyn-info list
  38. by a remote unwinder. On ia64, this is done via a special
  39. unwind-table entry. Perhaps something similar can be done with
  40. DWARF2 unwind info. */
  41. static void
  42. put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
  43. {
  44. /* it's a no-op */
  45. }
  46. static int
  47. get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
  48. void *arg)
  49. {
  50. *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
  51. return 0;
  52. }
  53. #define PAGE_SIZE 4096
  54. #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
  55. static int (*mem_validate_func) (void *addr, size_t len);
  56. static int msync_validate (void *addr, size_t len)
  57. {
  58. return msync (addr, len, MS_ASYNC);
  59. }
  60. #ifdef HAVE_MINCORE
  61. static int mincore_validate (void *addr, size_t len)
  62. {
  63. unsigned char mvec[2]; /* Unaligned access may cross page boundary */
  64. return mincore (addr, len, mvec);
  65. }
  66. #endif
  67. /* Initialise memory validation method. On linux kernels <2.6.21,
  68. mincore() returns incorrect value for MAP_PRIVATE mappings,
  69. such as stacks. If mincore() was available at compile time,
  70. check if we can actually use it. If not, use msync() instead. */
  71. HIDDEN void
  72. tdep_init_mem_validate (void)
  73. {
  74. #ifdef HAVE_MINCORE
  75. unsigned char present = 1;
  76. if (mincore (&present, 1, &present) == 0)
  77. {
  78. Debug(1, "using mincore to validate memory\n");
  79. mem_validate_func = mincore_validate;
  80. }
  81. else
  82. #endif
  83. {
  84. Debug(1, "using msync to validate memory\n");
  85. mem_validate_func = msync_validate;
  86. }
  87. }
  88. /* Cache of already validated addresses */
  89. #define NLGA 4
  90. static unw_word_t last_good_addr[NLGA];
  91. static int lga_victim;
  92. static int
  93. validate_mem (unw_word_t addr)
  94. {
  95. int i, victim;
  96. size_t len;
  97. if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
  98. len = PAGE_SIZE;
  99. else
  100. len = PAGE_SIZE * 2;
  101. addr = PAGE_START(addr);
  102. if (addr == 0)
  103. return -1;
  104. for (i = 0; i < NLGA; i++)
  105. {
  106. if (last_good_addr[i] && (addr == last_good_addr[i]))
  107. return 0;
  108. }
  109. if (mem_validate_func ((void *) addr, len) == -1)
  110. return -1;
  111. victim = lga_victim;
  112. for (i = 0; i < NLGA; i++) {
  113. if (!last_good_addr[victim]) {
  114. last_good_addr[victim] = addr;
  115. return 0;
  116. }
  117. victim = (victim + 1) % NLGA;
  118. }
  119. /* All slots full. Evict the victim. */
  120. last_good_addr[victim] = addr;
  121. victim = (victim + 1) % NLGA;
  122. lga_victim = victim;
  123. return 0;
  124. }
  125. static int
  126. access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
  127. void *arg)
  128. {
  129. if (unlikely (write))
  130. {
  131. Debug (16, "mem[%016lx] <- %lx\n", addr, *val);
  132. *(unw_word_t *) addr = *val;
  133. }
  134. else
  135. {
  136. /* validate address */
  137. const struct cursor *c = (const struct cursor *)arg;
  138. if (likely (c != 0) && unlikely (c->validate)
  139. && unlikely (validate_mem (addr)))
  140. return -1;
  141. *val = *(unw_word_t *) addr;
  142. Debug (16, "mem[%016lx] -> %lx\n", addr, *val);
  143. }
  144. return 0;
  145. }
  146. static int
  147. access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
  148. void *arg)
  149. {
  150. unw_word_t *addr;
  151. ucontext_t *uc = ((struct cursor *)arg)->uc;
  152. if (unw_is_fpreg (reg))
  153. goto badreg;
  154. if (!(addr = x86_64_r_uc_addr (uc, reg)))
  155. goto badreg;
  156. if (write)
  157. {
  158. *(unw_word_t *) addr = *val;
  159. Debug (12, "%s <- 0x%016lx\n", unw_regname (reg), *val);
  160. }
  161. else
  162. {
  163. *val = *(unw_word_t *) addr;
  164. Debug (12, "%s -> 0x%016lx\n", unw_regname (reg), *val);
  165. }
  166. return 0;
  167. badreg:
  168. Debug (1, "bad register number %u\n", reg);
  169. return -UNW_EBADREG;
  170. }
  171. static int
  172. access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
  173. int write, void *arg)
  174. {
  175. ucontext_t *uc = ((struct cursor *)arg)->uc;
  176. unw_fpreg_t *addr;
  177. if (!unw_is_fpreg (reg))
  178. goto badreg;
  179. if (!(addr = x86_64_r_uc_addr (uc, reg)))
  180. goto badreg;
  181. if (write)
  182. {
  183. Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
  184. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  185. *(unw_fpreg_t *) addr = *val;
  186. }
  187. else
  188. {
  189. *val = *(unw_fpreg_t *) addr;
  190. Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
  191. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  192. }
  193. return 0;
  194. badreg:
  195. Debug (1, "bad register number %u\n", reg);
  196. /* attempt to access a non-preserved register */
  197. return -UNW_EBADREG;
  198. }
  199. static int
  200. get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
  201. char *buf, size_t buf_len, unw_word_t *offp,
  202. void *arg)
  203. {
  204. #if HAVE_SGX
  205. return -1;
  206. #else
  207. return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
  208. #endif
  209. }
  210. HIDDEN void
  211. x86_64_local_addr_space_init (void)
  212. {
  213. memset (&local_addr_space, 0, sizeof (local_addr_space));
  214. local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
  215. local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
  216. local_addr_space.acc.put_unwind_info = put_unwind_info;
  217. local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
  218. local_addr_space.acc.access_mem = access_mem;
  219. local_addr_space.acc.access_reg = access_reg;
  220. local_addr_space.acc.access_fpreg = access_fpreg;
  221. local_addr_space.acc.resume = x86_64_local_resume;
  222. local_addr_space.acc.get_proc_name = get_static_proc_name;
  223. unw_flush_cache (&local_addr_space, 0, 0);
  224. memset (last_good_addr, 0, sizeof (unw_word_t) * NLGA);
  225. lga_victim = 0;
  226. }
  227. #endif /* !UNW_REMOTE_ONLY */