Ginit.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2006-2007 IBM
  3. Contributed by
  4. Corey Ashford <cjashfor@us.ibm.com>
  5. Jose Flavio Aguilar Paulino <jflavio@br.ibm.com> <joseflavio@gmail.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. #include <stdlib.h>
  24. #include <string.h>
  25. #include "ucontext_i.h"
  26. #include "unwind_i.h"
  27. #ifdef UNW_REMOTE_ONLY
  28. /* unw_local_addr_space is a NULL pointer in this case. */
  29. PROTECTED unw_addr_space_t unw_local_addr_space;
  30. #else /* !UNW_REMOTE_ONLY */
  31. static struct unw_addr_space local_addr_space;
  32. PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
  33. #define PAGE_SIZE 4096
  34. #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
  35. static void *
  36. uc_addr (ucontext_t *uc, int reg)
  37. {
  38. void *addr;
  39. if ((unsigned) (reg - UNW_PPC32_R0) < 32)
  40. addr = &uc->uc_mcontext.uc_regs->gregs[reg - UNW_PPC32_R0];
  41. else
  42. if ( ((unsigned) (reg - UNW_PPC32_F0) < 32) &&
  43. ((unsigned) (reg - UNW_PPC32_F0) >= 0) )
  44. addr = &uc->uc_mcontext.uc_regs->fpregs.fpregs[reg - UNW_PPC32_F0];
  45. else
  46. {
  47. unsigned gregs_idx;
  48. switch (reg)
  49. {
  50. case UNW_PPC32_CTR:
  51. gregs_idx = CTR_IDX;
  52. break;
  53. case UNW_PPC32_LR:
  54. gregs_idx = LINK_IDX;
  55. break;
  56. case UNW_PPC32_XER:
  57. gregs_idx = XER_IDX;
  58. break;
  59. case UNW_PPC32_CCR:
  60. gregs_idx = CCR_IDX;
  61. break;
  62. default:
  63. return NULL;
  64. }
  65. addr = &uc->uc_mcontext.uc_regs->gregs[gregs_idx];
  66. }
  67. return addr;
  68. }
  69. # ifdef UNW_LOCAL_ONLY
  70. HIDDEN void *
  71. tdep_uc_addr (ucontext_t *uc, int reg)
  72. {
  73. return uc_addr (uc, reg);
  74. }
  75. # endif /* UNW_LOCAL_ONLY */
  76. HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
  77. static void
  78. put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
  79. {
  80. /* it's a no-op */
  81. }
  82. static int
  83. get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
  84. void *arg)
  85. {
  86. *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
  87. return 0;
  88. }
  89. static int
  90. access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
  91. void *arg)
  92. {
  93. if (write)
  94. {
  95. Debug (12, "mem[%lx] <- %lx\n", addr, *val);
  96. *(unw_word_t *) addr = *val;
  97. }
  98. else
  99. {
  100. *val = *(unw_word_t *) addr;
  101. Debug (12, "mem[%lx] -> %lx\n", addr, *val);
  102. }
  103. return 0;
  104. }
  105. static int
  106. access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
  107. int write, void *arg)
  108. {
  109. unw_word_t *addr;
  110. ucontext_t *uc = arg;
  111. if ( ((unsigned int) (reg - UNW_PPC32_F0) < 32) &&
  112. ((unsigned int) (reg - UNW_PPC32_F0) >= 0))
  113. goto badreg;
  114. addr = uc_addr (uc, reg);
  115. if (!addr)
  116. goto badreg;
  117. if (write)
  118. {
  119. *(unw_word_t *) addr = *val;
  120. Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
  121. }
  122. else
  123. {
  124. *val = *(unw_word_t *) addr;
  125. Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
  126. }
  127. return 0;
  128. badreg:
  129. Debug (1, "bad register number %u\n", reg);
  130. return -UNW_EBADREG;
  131. }
  132. static int
  133. access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
  134. int write, void *arg)
  135. {
  136. ucontext_t *uc = arg;
  137. unw_fpreg_t *addr;
  138. if ((unsigned) (reg - UNW_PPC32_F0) < 0)
  139. goto badreg;
  140. addr = uc_addr (uc, reg);
  141. if (!addr)
  142. goto badreg;
  143. if (write)
  144. {
  145. Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
  146. *(unw_fpreg_t *) addr = *val;
  147. }
  148. else
  149. {
  150. *val = *(unw_fpreg_t *) addr;
  151. Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
  152. }
  153. return 0;
  154. badreg:
  155. Debug (1, "bad register number %u\n", reg);
  156. /* attempt to access a non-preserved register */
  157. return -UNW_EBADREG;
  158. }
  159. static int
  160. get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
  161. char *buf, size_t buf_len, unw_word_t *offp,
  162. void *arg)
  163. {
  164. return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
  165. }
  166. HIDDEN void
  167. ppc32_local_addr_space_init (void)
  168. {
  169. memset (&local_addr_space, 0, sizeof (local_addr_space));
  170. local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
  171. local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
  172. local_addr_space.acc.put_unwind_info = put_unwind_info;
  173. local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
  174. local_addr_space.acc.access_mem = access_mem;
  175. local_addr_space.acc.access_reg = access_reg;
  176. local_addr_space.acc.access_fpreg = access_fpreg;
  177. local_addr_space.acc.resume = ppc32_local_resume;
  178. local_addr_space.acc.get_proc_name = get_static_proc_name;
  179. unw_flush_cache (&local_addr_space, 0, 0);
  180. }
  181. #endif /* !UNW_REMOTE_ONLY */