Home | History | Annotate | Download | only in ppc32
      1 /* libunwind - a platform-independent unwind library
      2    Copyright (C) 2006-2007 IBM
      3    Contributed by
      4      Corey Ashford <cjashfor (at) us.ibm.com>
      5      Jose Flavio Aguilar Paulino <jflavio (at) br.ibm.com> <joseflavio (at) 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 static void *
     46 uc_addr (ucontext_t *uc, int reg)
     47 {
     48   void *addr;
     49 
     50   if ((unsigned) (reg - UNW_PPC32_R0) < 32)
     51     addr = &uc->uc_mcontext.uc_regs->gregs[reg - UNW_PPC32_R0];
     52 
     53   else
     54   if ( ((unsigned) (reg - UNW_PPC32_F0) < 32) &&
     55        ((unsigned) (reg - UNW_PPC32_F0) >= 0) )
     56     addr = &uc->uc_mcontext.uc_regs->fpregs.fpregs[reg - UNW_PPC32_F0];
     57 
     58   else
     59     {
     60       unsigned gregs_idx;
     61 
     62       switch (reg)
     63 	{
     64 	case UNW_PPC32_CTR:
     65 	  gregs_idx = CTR_IDX;
     66 	  break;
     67 	case UNW_PPC32_LR:
     68 	  gregs_idx = LINK_IDX;
     69 	  break;
     70 	case UNW_PPC32_XER:
     71 	  gregs_idx = XER_IDX;
     72 	  break;
     73 	case UNW_PPC32_CCR:
     74 	  gregs_idx = CCR_IDX;
     75 	  break;
     76 	default:
     77 	  return NULL;
     78 	}
     79       addr = &uc->uc_mcontext.uc_regs->gregs[gregs_idx];
     80     }
     81   return addr;
     82 }
     83 
     84 # ifdef UNW_LOCAL_ONLY
     85 
     86 HIDDEN void *
     87 tdep_uc_addr (ucontext_t *uc, int reg)
     88 {
     89   return uc_addr (uc, reg);
     90 }
     91 
     92 # endif	/* UNW_LOCAL_ONLY */
     93 
     94 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
     95 
     96 
     97 static void
     98 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
     99 {
    100   /* it's a no-op */
    101 }
    102 
    103 static int
    104 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
    105 			void *arg)
    106 {
    107   *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
    108   return 0;
    109 }
    110 
    111 static int
    112 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
    113 	    void *arg)
    114 {
    115   if (write)
    116     {
    117       /* ANDROID support update. */
    118 #ifdef UNW_LOCAL_ONLY
    119       if (map_local_is_writable (addr, sizeof(unw_word_t)))
    120         {
    121 #endif
    122           Debug (12, "mem[%lx] <- %lx\n", addr, *val);
    123           *(unw_word_t *) addr = *val;
    124 #ifdef UNW_LOCAL_ONLY
    125         }
    126       else
    127         {
    128           Debug (12, "Unwritable memory mem[%lx] <- %lx\n", addr, *val);
    129           return -1;
    130         }
    131 #endif
    132       /* End of ANDROID update. */
    133     }
    134   else
    135     {
    136       /* ANDROID support update. */
    137 #ifdef UNW_LOCAL_ONLY
    138       if (map_local_is_readable (addr, sizeof(unw_word_t)))
    139         {
    140 #endif
    141           *val = *(unw_word_t *) addr;
    142           Debug (12, "mem[%lx] -> %lx\n", addr, *val);
    143 #ifdef UNW_LOCAL_ONLY
    144         }
    145       else
    146         {
    147           Debug (12, "Unreadable memory mem[%lx] -> XXX\n", addr);
    148           return -1;
    149         }
    150 #endif
    151       /* End of ANDROID update. */
    152     }
    153   return 0;
    154 }
    155 
    156 static int
    157 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
    158 	    int write, void *arg)
    159 {
    160   unw_word_t *addr;
    161   ucontext_t *uc = arg;
    162 
    163   if ( ((unsigned int) (reg - UNW_PPC32_F0) < 32) &&
    164        ((unsigned int) (reg - UNW_PPC32_F0) >= 0))
    165     goto badreg;
    166 
    167   addr = uc_addr (uc, reg);
    168   if (!addr)
    169     goto badreg;
    170 
    171   if (write)
    172     {
    173       *(unw_word_t *) addr = *val;
    174       Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
    175     }
    176   else
    177     {
    178       *val = *(unw_word_t *) addr;
    179       Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
    180     }
    181   return 0;
    182 
    183 badreg:
    184   Debug (1, "bad register number %u\n", reg);
    185   return -UNW_EBADREG;
    186 }
    187 
    188 static int
    189 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
    190 	      int write, void *arg)
    191 {
    192   ucontext_t *uc = arg;
    193   unw_fpreg_t *addr;
    194 
    195   if ((unsigned) (reg - UNW_PPC32_F0) < 0)
    196     goto badreg;
    197 
    198   addr = uc_addr (uc, reg);
    199   if (!addr)
    200     goto badreg;
    201 
    202   if (write)
    203     {
    204       Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
    205       *(unw_fpreg_t *) addr = *val;
    206     }
    207   else
    208     {
    209       *val = *(unw_fpreg_t *) addr;
    210       Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
    211     }
    212   return 0;
    213 
    214 badreg:
    215   Debug (1, "bad register number %u\n", reg);
    216   /* attempt to access a non-preserved register */
    217   return -UNW_EBADREG;
    218 }
    219 
    220 static int
    221 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
    222 		      char *buf, size_t buf_len, unw_word_t *offp,
    223 		      void *arg)
    224 {
    225   return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp, arg);
    226 }
    227 
    228 HIDDEN void
    229 ppc32_local_addr_space_init (void)
    230 {
    231   memset (&local_addr_space, 0, sizeof (local_addr_space));
    232   local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
    233   local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
    234   local_addr_space.acc.put_unwind_info = put_unwind_info;
    235   local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
    236   local_addr_space.acc.access_mem = access_mem;
    237   local_addr_space.acc.access_reg = access_reg;
    238   local_addr_space.acc.access_fpreg = access_fpreg;
    239   local_addr_space.acc.resume = ppc32_local_resume;
    240   local_addr_space.acc.get_proc_name = get_static_proc_name;
    241   unw_flush_cache (&local_addr_space, 0, 0);
    242 
    243   map_local_init ();
    244 }
    245 
    246 #endif /* !UNW_REMOTE_ONLY */
    247