GDBserver
/home/stan/gdb/src/gdb/gdbserver/linux-sparc-low.c
Go to the documentation of this file.
00001 /* Low level interface to ptrace, for the remote server for GDB.
00002    Copyright (C) 1995-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #include "server.h"
00020 #include "linux-low.h"
00021 
00022 #include <sys/ptrace.h>
00023 
00024 #include "gdb_proc_service.h"
00025 
00026 /* The stack pointer is offset from the stack frame by a BIAS of 2047
00027    (0x7ff) for 64-bit code.  BIAS is likely to be defined on SPARC
00028    hosts, so undefine it first.  */
00029 #undef BIAS
00030 #define BIAS 2047
00031 
00032 #ifdef HAVE_SYS_REG_H
00033 #include <sys/reg.h>
00034 #endif
00035 
00036 #define INSN_SIZE 4
00037 
00038 #define SPARC_R_REGS_NUM 32
00039 #define SPARC_F_REGS_NUM 48
00040 #define SPARC_CONTROL_REGS_NUM 6
00041 
00042 #define sparc_num_regs \
00043   (SPARC_R_REGS_NUM + SPARC_F_REGS_NUM + SPARC_CONTROL_REGS_NUM)
00044 
00045 /* Each offset is multiplied by 8, because of the register size.
00046    These offsets apply to the buffer sent/filled by ptrace.
00047    Additionally, the array elements order corresponds to the .dat file, and the
00048    gdb's registers enumeration order.  */
00049 
00050 static int sparc_regmap[] = {
00051   /* These offsets correspond to GET/SETREGSET.  */
00052         -1,  0*8,  1*8,  2*8,  3*8,  4*8,  5*8,  6*8,    /* g0 .. g7 */
00053         7*8,  8*8,  9*8, 10*8, 11*8, 12*8, 13*8, 14*8,   /* o0 .. o5, sp, o7 */
00054         -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,    /* l0 .. l7 */
00055         -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,    /* i0 .. i5, fp, i7 */
00056 
00057   /* Floating point registers offsets correspond to GET/SETFPREGSET.  */
00058     0*4,  1*4,  2*4,  3*4,  4*4,  5*4,  6*4,  7*4,         /*  f0 ..  f7 */
00059     8*4,  9*4, 10*4, 11*4, 12*4, 13*4, 14*4, 15*4,         /*  f8 .. f15 */
00060    16*4, 17*4, 18*4, 19*4, 20*4, 21*4, 22*4, 23*4,         /* f16 .. f23 */
00061    24*4, 25*4, 26*4, 27*4, 28*4, 29*4, 30*4, 31*4,         /* f24 .. f31 */
00062 
00063   /* F32 offset starts next to f31: 31*4+4 = 16 * 8.  */
00064    16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8,         /* f32 .. f46 */
00065    24*8, 25*8, 26*8, 27*8, 28*8, 29*8, 30*8, 31*8,         /* f48 .. f62 */
00066 
00067    17 *8, /*    pc */
00068    18 *8, /*   npc */
00069    16 *8, /* state */
00070   /* FSR offset also corresponds to GET/SETFPREGSET, ans is placed
00071      next to f62.  */
00072    32 *8, /*   fsr */
00073       -1, /*  fprs */
00074   /* Y register is 32-bits length, but gdb takes care of that.  */
00075    19 *8, /*     y */
00076 
00077 };
00078 
00079 
00080 struct regs_range_t
00081 {
00082   int regno_start;
00083   int regno_end;
00084 };
00085 
00086 static const struct regs_range_t gregs_ranges[] = {
00087  {  0, 31 }, /*   g0 .. i7  */
00088  { 80, 82 }, /*   pc .. state */
00089  { 84, 85 }  /* fprs .. y */
00090 };
00091 
00092 #define N_GREGS_RANGES (sizeof (gregs_ranges) / sizeof (struct regs_range_t))
00093 
00094 static const struct regs_range_t fpregs_ranges[] = {
00095  { 32, 79 }, /* f0 .. f62  */
00096  { 83, 83 }  /* fsr */
00097 };
00098 
00099 #define N_FPREGS_RANGES (sizeof (fpregs_ranges) / sizeof (struct regs_range_t))
00100 
00101 /* Defined in auto-generated file reg-sparc64.c.  */
00102 void init_registers_sparc64 (void);
00103 extern const struct target_desc *tdesc_sparc64;
00104 
00105 static int
00106 sparc_cannot_store_register (int regno)
00107 {
00108   return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
00109 }
00110 
00111 static int
00112 sparc_cannot_fetch_register (int regno)
00113 {
00114   return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
00115 }
00116 
00117 static void
00118 sparc_fill_gregset_to_stack (struct regcache *regcache, const void *buf)
00119 {
00120   int i;
00121   CORE_ADDR addr = 0;
00122   unsigned char tmp_reg_buf[8];
00123   const int l0_regno = find_regno (regcache->tdesc, "l0");
00124   const int i7_regno = l0_regno + 15;
00125 
00126   /* These registers have to be stored in the stack.  */
00127   memcpy (&addr,
00128           ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
00129           sizeof (addr));
00130 
00131   addr += BIAS;
00132 
00133   for (i = l0_regno; i <= i7_regno; i++)
00134     {
00135       collect_register (regcache, i, tmp_reg_buf);
00136       (*the_target->write_memory) (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
00137       addr += sizeof (tmp_reg_buf);
00138     }
00139 }
00140 
00141 static void
00142 sparc_fill_gregset (struct regcache *regcache, void *buf)
00143 {
00144   int i;
00145   int range;
00146 
00147   for (range = 0; range < N_GREGS_RANGES; range++)
00148     for (i = gregs_ranges[range].regno_start;
00149          i <= gregs_ranges[range].regno_end; i++)
00150       if (sparc_regmap[i] != -1)
00151         collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
00152 
00153   sparc_fill_gregset_to_stack (regcache, buf);
00154 }
00155 
00156 static void
00157 sparc_fill_fpregset (struct regcache *regcache, void *buf)
00158 {
00159   int i;
00160   int range;
00161 
00162   for (range = 0; range < N_FPREGS_RANGES; range++)
00163     for (i = fpregs_ranges[range].regno_start;
00164          i <= fpregs_ranges[range].regno_end; i++)
00165       collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
00166 
00167 }
00168 
00169 static void
00170 sparc_store_gregset_from_stack (struct regcache *regcache, const void *buf)
00171 {
00172   int i;
00173   CORE_ADDR addr = 0;
00174   unsigned char tmp_reg_buf[8];
00175   const int l0_regno = find_regno (regcache->tdesc, "l0");
00176   const int i7_regno = l0_regno + 15;
00177 
00178   /* These registers have to be obtained from the stack.  */
00179   memcpy (&addr,
00180           ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
00181           sizeof (addr));
00182 
00183   addr += BIAS;
00184 
00185   for (i = l0_regno; i <= i7_regno; i++)
00186     {
00187       (*the_target->read_memory) (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
00188       supply_register (regcache, i, tmp_reg_buf);
00189       addr += sizeof (tmp_reg_buf);
00190     }
00191 }
00192 
00193 static void
00194 sparc_store_gregset (struct regcache *regcache, const void *buf)
00195 {
00196   int i;
00197   char zerobuf[8];
00198   int range;
00199 
00200   memset (zerobuf, 0, sizeof (zerobuf));
00201 
00202   for (range = 0; range < N_GREGS_RANGES; range++)
00203     for (i = gregs_ranges[range].regno_start;
00204          i <= gregs_ranges[range].regno_end; i++)
00205       if (sparc_regmap[i] != -1)
00206         supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
00207       else
00208         supply_register (regcache, i, zerobuf);
00209 
00210   sparc_store_gregset_from_stack (regcache, buf);
00211 }
00212 
00213 static void
00214 sparc_store_fpregset (struct regcache *regcache, const void *buf)
00215 {
00216   int i;
00217   int range;
00218 
00219   for (range = 0; range < N_FPREGS_RANGES; range++)
00220     for (i = fpregs_ranges[range].regno_start;
00221          i <= fpregs_ranges[range].regno_end;
00222          i++)
00223       supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
00224 }
00225 
00226 extern int debug_threads;
00227 
00228 static CORE_ADDR
00229 sparc_get_pc (struct regcache *regcache)
00230 {
00231   unsigned long pc;
00232   collect_register_by_name (regcache, "pc", &pc);
00233   if (debug_threads)
00234     fprintf (stderr, "stop pc is %08lx\n", pc);
00235   return pc;
00236 }
00237 
00238 static const unsigned char sparc_breakpoint[INSN_SIZE] = {
00239   0x91, 0xd0, 0x20, 0x01
00240 };
00241 #define sparc_breakpoint_len INSN_SIZE
00242 
00243 
00244 static int
00245 sparc_breakpoint_at (CORE_ADDR where)
00246 {
00247   unsigned char insn[INSN_SIZE];
00248 
00249   (*the_target->read_memory) (where, (unsigned char *) insn, sizeof (insn));
00250 
00251   if (memcmp (sparc_breakpoint, insn, sizeof (insn)) == 0)
00252     return 1;
00253 
00254   /* If necessary, recognize more trap instructions here.  GDB only
00255      uses TRAP Always.  */
00256 
00257   return 0;
00258 }
00259 
00260 /* We only place breakpoints in empty marker functions, and thread locking
00261    is outside of the function.  So rather than importing software single-step,
00262    we can just run until exit.  */
00263 static CORE_ADDR
00264 sparc_reinsert_addr (void)
00265 {
00266   struct regcache *regcache = get_thread_regcache (current_inferior, 1);
00267   CORE_ADDR lr;
00268   /* O7 is the equivalent to the 'lr' of other archs.  */
00269   collect_register_by_name (regcache, "o7", &lr);
00270   return lr;
00271 }
00272 
00273 static void
00274 sparc_arch_setup (void)
00275 {
00276   current_process ()->tdesc = tdesc_sparc64;
00277 }
00278 
00279 static struct regset_info sparc_regsets[] = {
00280   { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
00281     GENERAL_REGS,
00282     sparc_fill_gregset, sparc_store_gregset },
00283   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (fpregset_t),
00284     FP_REGS,
00285     sparc_fill_fpregset, sparc_store_fpregset },
00286   { 0, 0, 0, -1, -1, NULL, NULL }
00287 };
00288 
00289 static struct regsets_info sparc_regsets_info =
00290   {
00291     sparc_regsets, /* regsets */
00292     0, /* num_regsets */
00293     NULL, /* disabled_regsets */
00294   };
00295 
00296 static struct usrregs_info sparc_usrregs_info =
00297   {
00298     sparc_num_regs,
00299     /* No regmap needs to be provided since this impl. doesn't use
00300        USRREGS.  */
00301     NULL
00302   };
00303 
00304 static struct regs_info regs_info =
00305   {
00306     NULL, /* regset_bitmap */
00307     &sparc_usrregs_info,
00308     &sparc_regsets_info
00309   };
00310 
00311 static const struct regs_info *
00312 sparc_regs_info (void)
00313 {
00314   return &regs_info;
00315 }
00316 
00317 struct linux_target_ops the_low_target = {
00318   sparc_arch_setup,
00319   sparc_regs_info,
00320   sparc_cannot_fetch_register,
00321   sparc_cannot_store_register,
00322   NULL, /* fetch_register */
00323   sparc_get_pc,
00324   /* No sparc_set_pc is needed.  */
00325   NULL,
00326   (const unsigned char *) sparc_breakpoint,
00327   sparc_breakpoint_len,
00328   sparc_reinsert_addr,
00329   0,
00330   sparc_breakpoint_at,
00331   NULL, NULL, NULL, NULL,
00332   NULL, NULL
00333 };
00334 
00335 void
00336 initialize_low_arch (void)
00337 {
00338   /* Initialize the Linux target descriptions.  */
00339   init_registers_sparc64 ();
00340 
00341   initialize_regsets_info (&sparc_regsets_info);
00342 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines