GDBserver
/home/stan/gdb/src/gdb/gdbserver/linux-crisv32-low.c
Go to the documentation of this file.
00001 /* GNU/Linux/CRIS specific low level interface, 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 #include <sys/ptrace.h>
00022 
00023 /* Defined in auto-generated file reg-crisv32.c.  */
00024 void init_registers_crisv32 (void);
00025 extern const struct target_desc *tdesc_crisv32;
00026 
00027 /* CRISv32 */
00028 #define cris_num_regs 49
00029 
00030 #ifndef PTRACE_GET_THREAD_AREA
00031 #define PTRACE_GET_THREAD_AREA 25
00032 #endif
00033 
00034 /* Note: Ignoring USP (having the stack pointer in two locations causes trouble
00035    without any significant gain).  */
00036 
00037 /* Locations need to match <include/asm/arch/ptrace.h>.  */
00038 static int cris_regmap[] = {
00039   1*4, 2*4, 3*4, 4*4,
00040   5*4, 6*4, 7*4, 8*4,
00041   9*4, 10*4, 11*4, 12*4,
00042   13*4, 14*4, 24*4, 15*4,
00043 
00044   -1, -1, -1, 16*4,
00045   -1, 22*4, 23*4, 17*4,
00046   -1, -1, 21*4, 20*4,
00047   -1, 19*4, -1, 18*4,
00048 
00049   25*4,
00050 
00051   26*4, -1,   -1,   29*4,
00052   30*4, 31*4, 32*4, 33*4,
00053   34*4, 35*4, 36*4, 37*4,
00054   38*4, 39*4, 40*4, -1
00055 
00056 };
00057 
00058 extern int debug_threads;
00059 
00060 static CORE_ADDR
00061 cris_get_pc (struct regcache *regcache)
00062 {
00063   unsigned long pc;
00064   collect_register_by_name (regcache, "pc", &pc);
00065   if (debug_threads)
00066     fprintf (stderr, "stop pc is %08lx\n", pc);
00067   return pc;
00068 }
00069 
00070 static void
00071 cris_set_pc (struct regcache *regcache, CORE_ADDR pc)
00072 {
00073   unsigned long newpc = pc;
00074   supply_register_by_name (regcache, "pc", &newpc);
00075 }
00076 
00077 static const unsigned short cris_breakpoint = 0xe938;
00078 #define cris_breakpoint_len 2
00079 
00080 static int
00081 cris_breakpoint_at (CORE_ADDR where)
00082 {
00083   unsigned short insn;
00084 
00085   (*the_target->read_memory) (where, (unsigned char *) &insn,
00086                               cris_breakpoint_len);
00087   if (insn == cris_breakpoint)
00088     return 1;
00089 
00090   /* If necessary, recognize more trap instructions here.  GDB only uses the
00091      one.  */
00092   return 0;
00093 }
00094 
00095 /* We only place breakpoints in empty marker functions, and thread locking
00096    is outside of the function.  So rather than importing software single-step,
00097    we can just run until exit.  */
00098 
00099 /* FIXME: This function should not be needed, since we have PTRACE_SINGLESTEP
00100    for CRISv32.  Without it, td_ta_event_getmsg in thread_db_create_event
00101    will fail when debugging multi-threaded applications.  */
00102 
00103 static CORE_ADDR
00104 cris_reinsert_addr (void)
00105 {
00106   struct regcache *regcache = get_thread_regcache (current_inferior, 1);
00107   unsigned long pc;
00108   collect_register_by_name (regcache, "srp", &pc);
00109   return pc;
00110 }
00111 
00112 static void
00113 cris_write_data_breakpoint (struct regcache *regcache,
00114                             int bp, unsigned long start, unsigned long end)
00115 {
00116   switch (bp)
00117     {
00118     case 0:
00119       supply_register_by_name (regcache, "s3", &start);
00120       supply_register_by_name (regcache, "s4", &end);
00121       break;
00122     case 1:
00123       supply_register_by_name (regcache, "s5", &start);
00124       supply_register_by_name (regcache, "s6", &end);
00125       break;
00126     case 2:
00127       supply_register_by_name (regcache, "s7", &start);
00128       supply_register_by_name (regcache, "s8", &end);
00129       break;
00130     case 3:
00131       supply_register_by_name (regcache, "s9", &start);
00132       supply_register_by_name (regcache, "s10", &end);
00133       break;
00134     case 4:
00135       supply_register_by_name (regcache, "s11", &start);
00136       supply_register_by_name (regcache, "s12", &end);
00137       break;
00138     case 5:
00139       supply_register_by_name (regcache, "s13", &start);
00140       supply_register_by_name (regcache, "s14", &end);
00141       break;
00142     }
00143 }
00144 
00145 static int
00146 cris_insert_point (char type, CORE_ADDR addr, int len)
00147 {
00148   int bp;
00149   unsigned long bp_ctrl;
00150   unsigned long start, end;
00151   unsigned long ccs;
00152   struct regcache *regcache;
00153 
00154   /* Breakpoint/watchpoint types (GDB terminology):
00155      0 = memory breakpoint for instructions
00156      (not supported; done via memory write instead)
00157      1 = hardware breakpoint for instructions (not supported)
00158      2 = write watchpoint (supported)
00159      3 = read watchpoint (supported)
00160      4 = access watchpoint (supported).  */
00161 
00162   if (type < '2' || type > '4')
00163     {
00164       /* Unsupported.  */
00165       return 1;
00166     }
00167 
00168   regcache = get_thread_regcache (current_inferior, 1);
00169 
00170   /* Read watchpoints are set as access watchpoints, because of GDB's
00171      inability to deal with pure read watchpoints.  */
00172   if (type == '3')
00173     type = '4';
00174 
00175   /* Get the configuration register.  */
00176   collect_register_by_name (regcache, "s0", &bp_ctrl);
00177 
00178   /* The watchpoint allocation scheme is the simplest possible.
00179      For example, if a region is watched for read and
00180      a write watch is requested, a new watchpoint will
00181      be used.  Also, if a watch for a region that is already
00182      covered by one or more existing watchpoints, a new
00183      watchpoint will be used.  */
00184 
00185   /* First, find a free data watchpoint.  */
00186   for (bp = 0; bp < 6; bp++)
00187     {
00188       /* Each data watchpoint's control registers occupy 2 bits
00189          (hence the 3), starting at bit 2 for D0 (hence the 2)
00190          with 4 bits between for each watchpoint (yes, the 4).  */
00191       if (!(bp_ctrl & (0x3 << (2 + (bp * 4)))))
00192         break;
00193     }
00194 
00195   if (bp > 5)
00196     {
00197       /* We're out of watchpoints.  */
00198       return -1;
00199     }
00200 
00201   /* Configure the control register first.  */
00202   if (type == '3' || type == '4')
00203     {
00204       /* Trigger on read.  */
00205       bp_ctrl |= (1 << (2 + bp * 4));
00206     }
00207   if (type == '2' || type == '4')
00208     {
00209       /* Trigger on write.  */
00210       bp_ctrl |= (2 << (2 + bp * 4));
00211     }
00212 
00213   /* Setup the configuration register.  */
00214   supply_register_by_name (regcache, "s0", &bp_ctrl);
00215 
00216   /* Setup the range.  */
00217   start = addr;
00218   end = addr + len - 1;
00219 
00220   /* Configure the watchpoint register.  */
00221   cris_write_data_breakpoint (regcache, bp, start, end);
00222 
00223   collect_register_by_name (regcache, "ccs", &ccs);
00224   /* Set the S1 flag to enable watchpoints.  */
00225   ccs |= (1 << 19);
00226   supply_register_by_name (regcache, "ccs", &ccs);
00227 
00228   return 0;
00229 }
00230 
00231 static int
00232 cris_remove_point (char type, CORE_ADDR addr, int len)
00233 {
00234   int bp;
00235   unsigned long bp_ctrl;
00236   unsigned long start, end;
00237   struct regcache *regcache;
00238   unsigned long bp_d_regs[12];
00239 
00240   /* Breakpoint/watchpoint types:
00241      0 = memory breakpoint for instructions
00242      (not supported; done via memory write instead)
00243      1 = hardware breakpoint for instructions (not supported)
00244      2 = write watchpoint (supported)
00245      3 = read watchpoint (supported)
00246      4 = access watchpoint (supported).  */
00247   if (type < '2' || type > '4')
00248     return -1;
00249 
00250   regcache = get_thread_regcache (current_inferior, 1);
00251 
00252   /* Read watchpoints are set as access watchpoints, because of GDB's
00253      inability to deal with pure read watchpoints.  */
00254   if (type == '3')
00255     type = '4';
00256 
00257   /* Get the configuration register.  */
00258   collect_register_by_name (regcache, "s0", &bp_ctrl);
00259 
00260   /* Try to find a watchpoint that is configured for the
00261      specified range, then check that read/write also matches.  */
00262 
00263   /* Ugly pointer arithmetic, since I cannot rely on a
00264      single switch (addr) as there may be several watchpoints with
00265      the same start address for example.  */
00266 
00267   /* Get all range registers to simplify search.  */
00268   collect_register_by_name (regcache, "s3", &bp_d_regs[0]);
00269   collect_register_by_name (regcache, "s4", &bp_d_regs[1]);
00270   collect_register_by_name (regcache, "s5", &bp_d_regs[2]);
00271   collect_register_by_name (regcache, "s6", &bp_d_regs[3]);
00272   collect_register_by_name (regcache, "s7", &bp_d_regs[4]);
00273   collect_register_by_name (regcache, "s8", &bp_d_regs[5]);
00274   collect_register_by_name (regcache, "s9", &bp_d_regs[6]);
00275   collect_register_by_name (regcache, "s10", &bp_d_regs[7]);
00276   collect_register_by_name (regcache, "s11", &bp_d_regs[8]);
00277   collect_register_by_name (regcache, "s12", &bp_d_regs[9]);
00278   collect_register_by_name (regcache, "s13", &bp_d_regs[10]);
00279   collect_register_by_name (regcache, "s14", &bp_d_regs[11]);
00280 
00281   for (bp = 0; bp < 6; bp++)
00282     {
00283       if (bp_d_regs[bp * 2] == addr
00284           && bp_d_regs[bp * 2 + 1] == (addr + len - 1)) {
00285         /* Matching range.  */
00286         int bitpos = 2 + bp * 4;
00287         int rw_bits;
00288 
00289         /* Read/write bits for this BP.  */
00290         rw_bits = (bp_ctrl & (0x3 << bitpos)) >> bitpos;
00291 
00292         if ((type == '3' && rw_bits == 0x1)
00293             || (type == '2' && rw_bits == 0x2)
00294             || (type == '4' && rw_bits == 0x3))
00295           {
00296             /* Read/write matched.  */
00297             break;
00298           }
00299       }
00300     }
00301 
00302   if (bp > 5)
00303     {
00304       /* No watchpoint matched.  */
00305       return -1;
00306     }
00307 
00308   /* Found a matching watchpoint.  Now, deconfigure it by
00309      both disabling read/write in bp_ctrl and zeroing its
00310      start/end addresses.  */
00311   bp_ctrl &= ~(3 << (2 + (bp * 4)));
00312   /* Setup the configuration register.  */
00313   supply_register_by_name (regcache, "s0", &bp_ctrl);
00314 
00315   start = end = 0;
00316   /* Configure the watchpoint register.  */
00317   cris_write_data_breakpoint (regcache, bp, start, end);
00318 
00319   /* Note that we don't clear the S1 flag here.  It's done when continuing.  */
00320   return 0;
00321 }
00322 
00323 static int
00324 cris_stopped_by_watchpoint (void)
00325 {
00326   unsigned long exs;
00327   struct regcache *regcache = get_thread_regcache (current_inferior, 1);
00328 
00329   collect_register_by_name (regcache, "exs", &exs);
00330 
00331   return (((exs & 0xff00) >> 8) == 0xc);
00332 }
00333 
00334 static CORE_ADDR
00335 cris_stopped_data_address (void)
00336 {
00337   unsigned long eda;
00338   struct regcache *regcache = get_thread_regcache (current_inferior, 1);
00339 
00340   collect_register_by_name (regcache, "eda", &eda);
00341 
00342   /* FIXME: Possibly adjust to match watched range.  */
00343   return eda;
00344 }
00345 
00346 ps_err_e
00347 ps_get_thread_area (const struct ps_prochandle *ph,
00348                     lwpid_t lwpid, int idx, void **base)
00349 {
00350   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
00351     return PS_ERR;
00352 
00353   /* IDX is the bias from the thread pointer to the beginning of the
00354      thread descriptor.  It has to be subtracted due to implementation
00355      quirks in libthread_db.  */
00356   *base = (void *) ((char *) *base - idx);
00357   return PS_OK;
00358 }
00359 
00360 static void
00361 cris_fill_gregset (struct regcache *regcache, void *buf)
00362 {
00363   int i;
00364 
00365   for (i = 0; i < cris_num_regs; i++)
00366     {
00367       if (cris_regmap[i] != -1)
00368         collect_register (regcache, i, ((char *) buf) + cris_regmap[i]);
00369     }
00370 }
00371 
00372 static void
00373 cris_store_gregset (struct regcache *regcache, const void *buf)
00374 {
00375   int i;
00376 
00377   for (i = 0; i < cris_num_regs; i++)
00378     {
00379       if (cris_regmap[i] != -1)
00380         supply_register (regcache, i, ((char *) buf) + cris_regmap[i]);
00381     }
00382 }
00383 
00384 static void
00385 cris_arch_setup (void)
00386 {
00387   current_process ()->tdesc = tdesc_crisv32;
00388 }
00389 
00390 static struct regset_info cris_regsets[] = {
00391   { PTRACE_GETREGS, PTRACE_SETREGS, 0, cris_num_regs * 4,
00392     GENERAL_REGS, cris_fill_gregset, cris_store_gregset },
00393   { 0, 0, 0, -1, -1, NULL, NULL }
00394 };
00395 
00396 
00397 static struct regsets_info cris_regsets_info =
00398   {
00399     cris_regsets, /* regsets */
00400     0, /* num_regsets */
00401     NULL, /* disabled_regsets */
00402   };
00403 
00404 static struct usrregs_info cris_usrregs_info =
00405   {
00406     cris_num_regs,
00407     cris_regmap,
00408   };
00409 
00410 static struct regs_info regs_info =
00411   {
00412     NULL, /* regset_bitmap */
00413     &cris_usrregs_info,
00414     &cris_regsets_info
00415   };
00416 
00417 static const struct regs_info *
00418 cris_regs_info (void)
00419 {
00420   return &regs_info;
00421 }
00422 
00423 struct linux_target_ops the_low_target = {
00424   cris_arch_setup,
00425   cris_regs_info,
00426   NULL,
00427   NULL,
00428   NULL, /* fetch_register */
00429   cris_get_pc,
00430   cris_set_pc,
00431   (const unsigned char *) &cris_breakpoint,
00432   cris_breakpoint_len,
00433   cris_reinsert_addr,
00434   0,
00435   cris_breakpoint_at,
00436   cris_insert_point,
00437   cris_remove_point,
00438   cris_stopped_by_watchpoint,
00439   cris_stopped_data_address,
00440 };
00441 
00442 void
00443 initialize_low_arch (void)
00444 {
00445   init_registers_crisv32 ();
00446 
00447   initialize_regsets_info (&cris_regsets_info);
00448 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines