Jose Flavio Aguilar Paulino | b33021e | 2007-08-02 09:59:43 -0600 | [diff] [blame^] | 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 | |
| 7 | This file is part of libunwind. |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining |
| 10 | a copy of this software and associated documentation files (the |
| 11 | "Software"), to deal in the Software without restriction, including |
| 12 | without limitation the rights to use, copy, modify, merge, publish, |
| 13 | distribute, sublicense, and/or sell copies of the Software, and to |
| 14 | permit persons to whom the Software is furnished to do so, subject to |
| 15 | the following conditions: |
| 16 | |
| 17 | The above copyright notice and this permission notice shall be |
| 18 | included in all copies or substantial portions of the Software. |
| 19 | |
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 24 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 25 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 26 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "ucontext_i.h" |
| 32 | #include "unwind_i.h" |
| 33 | |
| 34 | #ifdef UNW_REMOTE_ONLY |
| 35 | |
| 36 | /* unw_local_addr_space is a NULL pointer in this case. */ |
| 37 | PROTECTED unw_addr_space_t unw_local_addr_space; |
| 38 | |
| 39 | #else /* !UNW_REMOTE_ONLY */ |
| 40 | |
| 41 | static struct unw_addr_space local_addr_space; |
| 42 | |
| 43 | PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space; |
| 44 | |
| 45 | |
| 46 | #define PAGE_SIZE 4096 |
| 47 | #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1)) |
| 48 | |
| 49 | |
| 50 | static void * |
| 51 | uc_addr (ucontext_t *uc, int reg) |
| 52 | { |
| 53 | void *addr; |
| 54 | |
| 55 | if ((unsigned) (reg - UNW_PPC64_R0) < 32) |
| 56 | addr = &uc->uc_mcontext.gp_regs[reg - UNW_PPC64_R0]; |
| 57 | |
| 58 | else if ((unsigned) (reg - UNW_PPC64_F0) < 32) |
| 59 | addr = &uc->uc_mcontext.fp_regs[reg - UNW_PPC64_F0]; |
| 60 | |
| 61 | else if ((unsigned) (reg - UNW_PPC64_V0) < 32) |
| 62 | addr = (uc->uc_mcontext.v_regs == 0) ? NULL : &uc->uc_mcontext.v_regs->vrregs[reg - UNW_PPC64_V0][0]; |
| 63 | |
| 64 | else |
| 65 | { |
| 66 | unsigned gregs_idx; |
| 67 | |
| 68 | switch (reg) |
| 69 | { |
| 70 | case UNW_PPC64_NIP: |
| 71 | gregs_idx = NIP_IDX; |
| 72 | break; |
| 73 | case UNW_PPC64_CTR: |
| 74 | gregs_idx = CTR_IDX; |
| 75 | break; |
| 76 | case UNW_PPC64_LR: |
| 77 | gregs_idx = LINK_IDX; |
| 78 | break; |
| 79 | case UNW_PPC64_XER: |
| 80 | gregs_idx = XER_IDX; |
| 81 | break; |
| 82 | case UNW_PPC64_CR0: |
| 83 | gregs_idx = CCR_IDX; |
| 84 | break; |
| 85 | default: |
| 86 | return NULL; |
| 87 | } |
| 88 | addr = &uc->uc_mcontext.gp_regs[gregs_idx]; |
| 89 | } |
| 90 | return addr; |
| 91 | } |
| 92 | |
| 93 | # ifdef UNW_LOCAL_ONLY |
| 94 | |
| 95 | HIDDEN void * |
| 96 | tdep_uc_addr (ucontext_t *uc, int reg) |
| 97 | { |
| 98 | return uc_addr (uc, reg); |
| 99 | } |
| 100 | |
| 101 | # endif /* UNW_LOCAL_ONLY */ |
| 102 | |
| 103 | HIDDEN unw_dyn_info_list_t _U_dyn_info_list; |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg) |
| 108 | { |
| 109 | /* it's a no-op */ |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr, |
| 114 | void *arg) |
| 115 | { |
| 116 | *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write, |
| 122 | void *arg) |
| 123 | { |
| 124 | if (write) |
| 125 | { |
| 126 | Debug (12, "mem[%lx] <- %lx\n", addr, *val); |
| 127 | *(unw_word_t *) addr = *val; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | *val = *(unw_word_t *) addr; |
| 132 | Debug (12, "mem[%lx] -> %lx\n", addr, *val); |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int |
| 138 | access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, |
| 139 | int write, void *arg) |
| 140 | { |
| 141 | unw_word_t *addr; |
| 142 | ucontext_t *uc = arg; |
| 143 | |
| 144 | if ((unsigned int) (reg - UNW_PPC64_F0) < 32) |
| 145 | goto badreg; |
| 146 | if ((unsigned int) (reg - UNW_PPC64_V0) < 32) |
| 147 | goto badreg; |
| 148 | |
| 149 | addr = uc_addr (uc, reg); |
| 150 | if (!addr) |
| 151 | goto badreg; |
| 152 | |
| 153 | if (write) |
| 154 | { |
| 155 | *(unw_word_t *) addr = *val; |
| 156 | Debug (12, "%s <- %lx\n", unw_regname (reg), *val); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | *val = *(unw_word_t *) addr; |
| 161 | Debug (12, "%s -> %lx\n", unw_regname (reg), *val); |
| 162 | } |
| 163 | return 0; |
| 164 | |
| 165 | badreg: |
| 166 | Debug (1, "bad register number %u\n", reg); |
| 167 | return -UNW_EBADREG; |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val, |
| 172 | int write, void *arg) |
| 173 | { |
| 174 | ucontext_t *uc = arg; |
| 175 | unw_fpreg_t *addr; |
| 176 | |
| 177 | if ((unsigned) (reg - UNW_PPC64_F0) < 0) |
| 178 | goto badreg; |
| 179 | |
| 180 | if ((unsigned) (reg - UNW_PPC64_V0) >= 32) |
| 181 | goto badreg; |
| 182 | |
| 183 | |
| 184 | addr = uc_addr (uc, reg); |
| 185 | if (!addr) |
| 186 | goto badreg; |
| 187 | |
| 188 | if (write) |
| 189 | { |
| 190 | Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val); |
| 191 | *(unw_fpreg_t *) addr = *val; |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | *val = *(unw_fpreg_t *) addr; |
| 196 | Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val); |
| 197 | } |
| 198 | return 0; |
| 199 | |
| 200 | badreg: |
| 201 | Debug (1, "bad register number %u\n", reg); |
| 202 | /* attempt to access a non-preserved register */ |
| 203 | return -UNW_EBADREG; |
| 204 | } |
| 205 | |
| 206 | static int |
| 207 | get_static_proc_name (unw_addr_space_t as, unw_word_t ip, |
| 208 | char *buf, size_t buf_len, unw_word_t *offp, |
| 209 | void *arg) |
| 210 | { |
| 211 | return _Uelf64_get_proc_name (getpid (), ip, buf, buf_len, offp); |
| 212 | } |
| 213 | |
| 214 | HIDDEN void |
| 215 | ppc64_local_addr_space_init (void) |
| 216 | { |
| 217 | memset (&local_addr_space, 0, sizeof (local_addr_space)); |
| 218 | local_addr_space.caching_policy = UNW_CACHE_GLOBAL; |
| 219 | local_addr_space.acc.find_proc_info = dwarf_find_proc_info; |
| 220 | local_addr_space.acc.put_unwind_info = put_unwind_info; |
| 221 | local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr; |
| 222 | local_addr_space.acc.access_mem = access_mem; |
| 223 | local_addr_space.acc.access_reg = access_reg; |
| 224 | local_addr_space.acc.access_fpreg = access_fpreg; |
| 225 | local_addr_space.acc.resume = ppc64_local_resume; |
| 226 | local_addr_space.acc.get_proc_name = get_static_proc_name; |
| 227 | unw_flush_cache (&local_addr_space, 0, 0); |
| 228 | } |
| 229 | |
| 230 | #endif /* !UNW_REMOTE_ONLY */ |