gdb/gdbserver/linux-arm-low.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
  2.    Copyright (C) 1995-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "server.h"
  15. #include "linux-low.h"

  16. /* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
  17.    On Bionic elf.h and linux/elf.h have conflicting definitions.  */
  18. #ifndef ELFMAG0
  19. #include <elf.h>
  20. #endif
  21. #include <sys/ptrace.h>
  22. #include <signal.h>

  23. /* Defined in auto-generated files.  */
  24. void init_registers_arm (void);
  25. extern const struct target_desc *tdesc_arm;

  26. void init_registers_arm_with_iwmmxt (void);
  27. extern const struct target_desc *tdesc_arm_with_iwmmxt;

  28. void init_registers_arm_with_vfpv2 (void);
  29. extern const struct target_desc *tdesc_arm_with_vfpv2;

  30. void init_registers_arm_with_vfpv3 (void);
  31. extern const struct target_desc *tdesc_arm_with_vfpv3;

  32. void init_registers_arm_with_neon (void);
  33. extern const struct target_desc *tdesc_arm_with_neon;

  34. #ifndef PTRACE_GET_THREAD_AREA
  35. #define PTRACE_GET_THREAD_AREA 22
  36. #endif

  37. #ifndef PTRACE_GETWMMXREGS
  38. # define PTRACE_GETWMMXREGS 18
  39. # define PTRACE_SETWMMXREGS 19
  40. #endif

  41. #ifndef PTRACE_GETVFPREGS
  42. # define PTRACE_GETVFPREGS 27
  43. # define PTRACE_SETVFPREGS 28
  44. #endif

  45. #ifndef PTRACE_GETHBPREGS
  46. #define PTRACE_GETHBPREGS 29
  47. #define PTRACE_SETHBPREGS 30
  48. #endif

  49. /* Information describing the hardware breakpoint capabilities.  */
  50. static struct
  51. {
  52.   unsigned char arch;
  53.   unsigned char max_wp_length;
  54.   unsigned char wp_count;
  55.   unsigned char bp_count;
  56. } arm_linux_hwbp_cap;

  57. /* Enum describing the different types of ARM hardware break-/watch-points.  */
  58. typedef enum
  59. {
  60.   arm_hwbp_break = 0,
  61.   arm_hwbp_load = 1,
  62.   arm_hwbp_store = 2,
  63.   arm_hwbp_access = 3
  64. } arm_hwbp_type;

  65. /* Type describing an ARM Hardware Breakpoint Control register value.  */
  66. typedef unsigned int arm_hwbp_control_t;

  67. /* Structure used to keep track of hardware break-/watch-points.  */
  68. struct arm_linux_hw_breakpoint
  69. {
  70.   /* Address to break on, or being watched.  */
  71.   unsigned int address;
  72.   /* Control register for break-/watch- point.  */
  73.   arm_hwbp_control_t control;
  74. };

  75. /* Since we cannot dynamically allocate subfields of arch_process_info,
  76.    assume a maximum number of supported break-/watchpoints.  */
  77. #define MAX_BPTS 32
  78. #define MAX_WPTS 32

  79. /* Per-process arch-specific data we want to keep.  */
  80. struct arch_process_info
  81. {
  82.   /* Hardware breakpoints for this process.  */
  83.   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
  84.   /* Hardware watchpoints for this process.  */
  85.   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
  86. };

  87. /* Per-thread arch-specific data we want to keep.  */
  88. struct arch_lwp_info
  89. {
  90.   /* Non-zero if our copy differs from what's recorded in the thread.  */
  91.   char bpts_changed[MAX_BPTS];
  92.   char wpts_changed[MAX_WPTS];
  93.   /* Cached stopped data address.  */
  94.   CORE_ADDR stopped_data_address;
  95. };

  96. static unsigned long arm_hwcap;

  97. /* These are in <asm/elf.h> in current kernels.  */
  98. #define HWCAP_VFP       64
  99. #define HWCAP_IWMMXT    512
  100. #define HWCAP_NEON      4096
  101. #define HWCAP_VFPv3     8192
  102. #define HWCAP_VFPv3D16  16384

  103. #ifdef HAVE_SYS_REG_H
  104. #include <sys/reg.h>
  105. #endif

  106. #define arm_num_regs 26

  107. static int arm_regmap[] = {
  108.   0, 4, 8, 12, 16, 20, 24, 28,
  109.   32, 36, 40, 44, 48, 52, 56, 60,
  110.   -1, -1, -1, -1, -1, -1, -1, -1, -1,
  111.   64
  112. };

  113. static int
  114. arm_cannot_store_register (int regno)
  115. {
  116.   return (regno >= arm_num_regs);
  117. }

  118. static int
  119. arm_cannot_fetch_register (int regno)
  120. {
  121.   return (regno >= arm_num_regs);
  122. }

  123. static void
  124. arm_fill_gregset (struct regcache *regcache, void *buf)
  125. {
  126.   int i;

  127.   for (i = 0; i < arm_num_regs; i++)
  128.     if (arm_regmap[i] != -1)
  129.       collect_register (regcache, i, ((char *) buf) + arm_regmap[i]);
  130. }

  131. static void
  132. arm_store_gregset (struct regcache *regcache, const void *buf)
  133. {
  134.   int i;
  135.   char zerobuf[8];

  136.   memset (zerobuf, 0, 8);
  137.   for (i = 0; i < arm_num_regs; i++)
  138.     if (arm_regmap[i] != -1)
  139.       supply_register (regcache, i, ((char *) buf) + arm_regmap[i]);
  140.     else
  141.       supply_register (regcache, i, zerobuf);
  142. }

  143. static void
  144. arm_fill_wmmxregset (struct regcache *regcache, void *buf)
  145. {
  146.   int i;

  147.   if (!(arm_hwcap & HWCAP_IWMMXT))
  148.     return;

  149.   for (i = 0; i < 16; i++)
  150.     collect_register (regcache, arm_num_regs + i, (char *) buf + i * 8);

  151.   /* We only have access to wcssf, wcasf, and wcgr0-wcgr3.  */
  152.   for (i = 0; i < 6; i++)
  153.     collect_register (regcache, arm_num_regs + i + 16,
  154.                       (char *) buf + 16 * 8 + i * 4);
  155. }

  156. static void
  157. arm_store_wmmxregset (struct regcache *regcache, const void *buf)
  158. {
  159.   int i;

  160.   if (!(arm_hwcap & HWCAP_IWMMXT))
  161.     return;

  162.   for (i = 0; i < 16; i++)
  163.     supply_register (regcache, arm_num_regs + i, (char *) buf + i * 8);

  164.   /* We only have access to wcssf, wcasf, and wcgr0-wcgr3.  */
  165.   for (i = 0; i < 6; i++)
  166.     supply_register (regcache, arm_num_regs + i + 16,
  167.                      (char *) buf + 16 * 8 + i * 4);
  168. }

  169. static void
  170. arm_fill_vfpregset (struct regcache *regcache, void *buf)
  171. {
  172.   int i, num, base;

  173.   if (!(arm_hwcap & HWCAP_VFP))
  174.     return;

  175.   if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
  176.     num = 32;
  177.   else
  178.     num = 16;

  179.   base = find_regno (regcache->tdesc, "d0");
  180.   for (i = 0; i < num; i++)
  181.     collect_register (regcache, base + i, (char *) buf + i * 8);

  182.   collect_register_by_name (regcache, "fpscr", (char *) buf + 32 * 8);
  183. }

  184. static void
  185. arm_store_vfpregset (struct regcache *regcache, const void *buf)
  186. {
  187.   int i, num, base;

  188.   if (!(arm_hwcap & HWCAP_VFP))
  189.     return;

  190.   if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
  191.     num = 32;
  192.   else
  193.     num = 16;

  194.   base = find_regno (regcache->tdesc, "d0");
  195.   for (i = 0; i < num; i++)
  196.     supply_register (regcache, base + i, (char *) buf + i * 8);

  197.   supply_register_by_name (regcache, "fpscr", (char *) buf + 32 * 8);
  198. }

  199. extern int debug_threads;

  200. static CORE_ADDR
  201. arm_get_pc (struct regcache *regcache)
  202. {
  203.   unsigned long pc;
  204.   collect_register_by_name (regcache, "pc", &pc);
  205.   if (debug_threads)
  206.     debug_printf ("stop pc is %08lx\n", pc);
  207.   return pc;
  208. }

  209. static void
  210. arm_set_pc (struct regcache *regcache, CORE_ADDR pc)
  211. {
  212.   unsigned long newpc = pc;
  213.   supply_register_by_name (regcache, "pc", &newpc);
  214. }

  215. /* Correct in either endianness.  */
  216. static const unsigned long arm_breakpoint = 0xef9f0001;
  217. #define arm_breakpoint_len 4
  218. static const unsigned short thumb_breakpoint = 0xde01;
  219. static const unsigned short thumb2_breakpoint[] = { 0xf7f0, 0xa000 };

  220. /* For new EABI binaries.  We recognize it regardless of which ABI
  221.    is used for gdbserver, so single threaded debugging should work
  222.    OK, but for multi-threaded debugging we only insert the current
  223.    ABI's breakpoint instruction.  For now at least.  */
  224. static const unsigned long arm_eabi_breakpoint = 0xe7f001f0;

  225. static int
  226. arm_breakpoint_at (CORE_ADDR where)
  227. {
  228.   struct regcache *regcache = get_thread_regcache (current_thread, 1);
  229.   unsigned long cpsr;

  230.   collect_register_by_name (regcache, "cpsr", &cpsr);

  231.   if (cpsr & 0x20)
  232.     {
  233.       /* Thumb mode.  */
  234.       unsigned short insn;

  235.       (*the_target->read_memory) (where, (unsigned char *) &insn, 2);
  236.       if (insn == thumb_breakpoint)
  237.         return 1;

  238.       if (insn == thumb2_breakpoint[0])
  239.         {
  240.           (*the_target->read_memory) (where + 2, (unsigned char *) &insn, 2);
  241.           if (insn == thumb2_breakpoint[1])
  242.             return 1;
  243.         }
  244.     }
  245.   else
  246.     {
  247.       /* ARM mode.  */
  248.       unsigned long insn;

  249.       (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
  250.       if (insn == arm_breakpoint)
  251.         return 1;

  252.       if (insn == arm_eabi_breakpoint)
  253.         return 1;
  254.     }

  255.   return 0;
  256. }

  257. /* We only place breakpoints in empty marker functions, and thread locking
  258.    is outside of the function.  So rather than importing software single-step,
  259.    we can just run until exit.  */
  260. static CORE_ADDR
  261. arm_reinsert_addr (void)
  262. {
  263.   struct regcache *regcache = get_thread_regcache (current_thread, 1);
  264.   unsigned long pc;
  265.   collect_register_by_name (regcache, "lr", &pc);
  266.   return pc;
  267. }

  268. /* Fetch the thread-local storage pointer for libthread_db.  */

  269. ps_err_e
  270. ps_get_thread_area (const struct ps_prochandle *ph,
  271.                     lwpid_t lwpid, int idx, void **base)
  272. {
  273.   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  274.     return PS_ERR;

  275.   /* IDX is the bias from the thread pointer to the beginning of the
  276.      thread descriptor.  It has to be subtracted due to implementation
  277.      quirks in libthread_db.  */
  278.   *base = (void *) ((char *)*base - idx);

  279.   return PS_OK;
  280. }


  281. /* Query Hardware Breakpoint information for the target we are attached to
  282.    (using PID as ptrace argument) and set up arm_linux_hwbp_cap.  */
  283. static void
  284. arm_linux_init_hwbp_cap (int pid)
  285. {
  286.   unsigned int val;

  287.   if (ptrace (PTRACE_GETHBPREGS, pid, 0, &val) < 0)
  288.     return;

  289.   arm_linux_hwbp_cap.arch = (unsigned char)((val >> 24) & 0xff);
  290.   if (arm_linux_hwbp_cap.arch == 0)
  291.     return;

  292.   arm_linux_hwbp_cap.max_wp_length = (unsigned char)((val >> 16) & 0xff);
  293.   arm_linux_hwbp_cap.wp_count = (unsigned char)((val >> 8) & 0xff);
  294.   arm_linux_hwbp_cap.bp_count = (unsigned char)(val & 0xff);

  295.   if (arm_linux_hwbp_cap.wp_count > MAX_WPTS)
  296.     internal_error (__FILE__, __LINE__, "Unsupported number of watchpoints");
  297.   if (arm_linux_hwbp_cap.bp_count > MAX_BPTS)
  298.     internal_error (__FILE__, __LINE__, "Unsupported number of breakpoints");
  299. }

  300. /* How many hardware breakpoints are available?  */
  301. static int
  302. arm_linux_get_hw_breakpoint_count (void)
  303. {
  304.   return arm_linux_hwbp_cap.bp_count;
  305. }

  306. /* How many hardware watchpoints are available?  */
  307. static int
  308. arm_linux_get_hw_watchpoint_count (void)
  309. {
  310.   return arm_linux_hwbp_cap.wp_count;
  311. }

  312. /* Maximum length of area watched by hardware watchpoint.  */
  313. static int
  314. arm_linux_get_hw_watchpoint_max_length (void)
  315. {
  316.   return arm_linux_hwbp_cap.max_wp_length;
  317. }

  318. /* Initialize an ARM hardware break-/watch-point control register value.
  319.    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
  320.    type of break-/watch-point; ENABLE indicates whether the point is enabled.
  321.    */
  322. static arm_hwbp_control_t
  323. arm_hwbp_control_initialize (unsigned byte_address_select,
  324.                              arm_hwbp_type hwbp_type,
  325.                              int enable)
  326. {
  327.   gdb_assert ((byte_address_select & ~0xffU) == 0);
  328.   gdb_assert (hwbp_type != arm_hwbp_break
  329.               || ((byte_address_select & 0xfU) != 0));

  330.   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
  331. }

  332. /* Does the breakpoint control value CONTROL have the enable bit set?  */
  333. static int
  334. arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
  335. {
  336.   return control & 0x1;
  337. }

  338. /* Is the breakpoint control value CONTROL initialized?  */
  339. static int
  340. arm_hwbp_control_is_initialized (arm_hwbp_control_t control)
  341. {
  342.   return control != 0;
  343. }

  344. /* Change a breakpoint control word so that it is in the disabled state.  */
  345. static arm_hwbp_control_t
  346. arm_hwbp_control_disable (arm_hwbp_control_t control)
  347. {
  348.   return control & ~0x1;
  349. }

  350. /* Are two break-/watch-points equal?  */
  351. static int
  352. arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
  353.                                const struct arm_linux_hw_breakpoint *p2)
  354. {
  355.   return p1->address == p2->address && p1->control == p2->control;
  356. }

  357. /* Convert a raw breakpoint type to an enum arm_hwbp_type.  */

  358. static int
  359. raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
  360. {
  361.   switch (raw_type)
  362.     {
  363.     case raw_bkpt_type_hw:
  364.       return arm_hwbp_break;
  365.     case raw_bkpt_type_write_wp:
  366.       return arm_hwbp_store;
  367.     case raw_bkpt_type_read_wp:
  368.       return arm_hwbp_load;
  369.     case raw_bkpt_type_access_wp:
  370.       return arm_hwbp_access;
  371.     default:
  372.       gdb_assert_not_reached ("unhandled raw type");
  373.     }
  374. }

  375. /* Initialize the hardware breakpoint structure P for a breakpoint or
  376.    watchpoint at ADDR to LEN.  The type of watchpoint is given in TYPE.
  377.    Returns -1 if TYPE is unsupported, or -2 if the particular combination
  378.    of ADDR and LEN cannot be implemented.  Otherwise, returns 0 if TYPE
  379.    represents a breakpoint and 1 if type represents a watchpoint.  */
  380. static int
  381. arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
  382.                                int len, struct arm_linux_hw_breakpoint *p)
  383. {
  384.   arm_hwbp_type hwbp_type;
  385.   unsigned mask;

  386.   hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);

  387.   if (hwbp_type == arm_hwbp_break)
  388.     {
  389.       /* For breakpoints, the length field encodes the mode.  */
  390.       switch (len)
  391.         {
  392.         case 2:         /* 16-bit Thumb mode breakpoint */
  393.         case 3/* 32-bit Thumb mode breakpoint */
  394.           mask = 0x3;
  395.           addr &= ~1;
  396.           break;
  397.         case 4/* 32-bit ARM mode breakpoint */
  398.           mask = 0xf;
  399.           addr &= ~3;
  400.           break;
  401.         default:
  402.           /* Unsupported. */
  403.           return -2;
  404.         }
  405.     }
  406.   else
  407.     {
  408.       CORE_ADDR max_wp_length = arm_linux_get_hw_watchpoint_max_length ();
  409.       CORE_ADDR aligned_addr;

  410.       /* Can not set watchpoints for zero or negative lengths.  */
  411.       if (len <= 0)
  412.         return -2;
  413.       /* The current ptrace interface can only handle watchpoints that are a
  414.          power of 2.  */
  415.       if ((len & (len - 1)) != 0)
  416.         return -2;

  417.       /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
  418.          range covered by a watchpoint.  */
  419.       aligned_addr = addr & ~(max_wp_length - 1);
  420.       if (aligned_addr + max_wp_length < addr + len)
  421.         return -2;

  422.       mask = (1 << len) - 1;
  423.     }

  424.   p->address = (unsigned int) addr;
  425.   p->control = arm_hwbp_control_initialize (mask, hwbp_type, 1);

  426.   return hwbp_type != arm_hwbp_break;
  427. }

  428. /* Callback to mark a watch-/breakpoint to be updated in all threads of
  429.    the current process.  */

  430. struct update_registers_data
  431. {
  432.   int watch;
  433.   int i;
  434. };

  435. static int
  436. update_registers_callback (struct inferior_list_entry *entry, void *arg)
  437. {
  438.   struct thread_info *thread = (struct thread_info *) entry;
  439.   struct lwp_info *lwp = get_thread_lwp (thread);
  440.   struct update_registers_data *data = (struct update_registers_data *) arg;

  441.   /* Only update the threads of the current process.  */
  442.   if (pid_of (thread) == pid_of (current_thread))
  443.     {
  444.       /* The actual update is done later just before resuming the lwp,
  445.          we just mark that the registers need updating.  */
  446.       if (data->watch)
  447.         lwp->arch_private->wpts_changed[data->i] = 1;
  448.       else
  449.         lwp->arch_private->bpts_changed[data->i] = 1;

  450.       /* If the lwp isn't stopped, force it to momentarily pause, so
  451.          we can update its breakpoint registers.  */
  452.       if (!lwp->stopped)
  453.         linux_stop_lwp (lwp);
  454.     }

  455.   return 0;
  456. }

  457. static int
  458. arm_supports_z_point_type (char z_type)
  459. {
  460.   switch (z_type)
  461.     {
  462.     case Z_PACKET_HW_BP:
  463.     case Z_PACKET_WRITE_WP:
  464.     case Z_PACKET_READ_WP:
  465.     case Z_PACKET_ACCESS_WP:
  466.       return 1;
  467.     default:
  468.       /* Leave the handling of sw breakpoints with the gdb client.  */
  469.       return 0;
  470.     }
  471. }

  472. /* Insert hardware break-/watchpoint.  */
  473. static int
  474. arm_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  475.                   int len, struct raw_breakpoint *bp)
  476. {
  477.   struct process_info *proc = current_process ();
  478.   struct arm_linux_hw_breakpoint p, *pts;
  479.   int watch, i, count;

  480.   watch = arm_linux_hw_point_initialize (type, addr, len, &p);
  481.   if (watch < 0)
  482.     {
  483.       /* Unsupported.  */
  484.       return watch == -1 ? 1 : -1;
  485.     }

  486.   if (watch)
  487.     {
  488.       count = arm_linux_get_hw_watchpoint_count ();
  489.       pts = proc->private->arch_private->wpts;
  490.     }
  491.   else
  492.     {
  493.       count = arm_linux_get_hw_breakpoint_count ();
  494.       pts = proc->private->arch_private->bpts;
  495.     }

  496.   for (i = 0; i < count; i++)
  497.     if (!arm_hwbp_control_is_enabled (pts[i].control))
  498.       {
  499.         struct update_registers_data data = { watch, i };
  500.         pts[i] = p;
  501.         find_inferior (&all_threads, update_registers_callback, &data);
  502.         return 0;
  503.       }

  504.   /* We're out of watchpoints.  */
  505.   return -1;
  506. }

  507. /* Remove hardware break-/watchpoint.  */
  508. static int
  509. arm_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  510.                   int len, struct raw_breakpoint *bp)
  511. {
  512.   struct process_info *proc = current_process ();
  513.   struct arm_linux_hw_breakpoint p, *pts;
  514.   int watch, i, count;

  515.   watch = arm_linux_hw_point_initialize (type, addr, len, &p);
  516.   if (watch < 0)
  517.     {
  518.       /* Unsupported.  */
  519.       return -1;
  520.     }

  521.   if (watch)
  522.     {
  523.       count = arm_linux_get_hw_watchpoint_count ();
  524.       pts = proc->private->arch_private->wpts;
  525.     }
  526.   else
  527.     {
  528.       count = arm_linux_get_hw_breakpoint_count ();
  529.       pts = proc->private->arch_private->bpts;
  530.     }

  531.   for (i = 0; i < count; i++)
  532.     if (arm_linux_hw_breakpoint_equal (&p, pts + i))
  533.       {
  534.         struct update_registers_data data = { watch, i };
  535.         pts[i].control = arm_hwbp_control_disable (pts[i].control);
  536.         find_inferior (&all_threads, update_registers_callback, &data);
  537.         return 0;
  538.       }

  539.   /* No watchpoint matched.  */
  540.   return -1;
  541. }

  542. /* Return whether current thread is stopped due to a watchpoint.  */
  543. static int
  544. arm_stopped_by_watchpoint (void)
  545. {
  546.   struct lwp_info *lwp = get_thread_lwp (current_thread);
  547.   siginfo_t siginfo;

  548.   /* We must be able to set hardware watchpoints.  */
  549.   if (arm_linux_get_hw_watchpoint_count () == 0)
  550.     return 0;

  551.   /* Retrieve siginfo.  */
  552.   errno = 0;
  553.   ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread), 0, &siginfo);
  554.   if (errno != 0)
  555.     return 0;

  556.   /* This must be a hardware breakpoint.  */
  557.   if (siginfo.si_signo != SIGTRAP
  558.       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  559.     return 0;

  560.   /* If we are in a positive slot then we're looking at a breakpoint and not
  561.      a watchpoint.  */
  562.   if (siginfo.si_errno >= 0)
  563.     return 0;

  564.   /* Cache stopped data address for use by arm_stopped_data_address.  */
  565.   lwp->arch_private->stopped_data_address
  566.     = (CORE_ADDR) (uintptr_t) siginfo.si_addr;

  567.   return 1;
  568. }

  569. /* Return data address that triggered watchpoint.  Called only if
  570.    arm_stopped_by_watchpoint returned true.  */
  571. static CORE_ADDR
  572. arm_stopped_data_address (void)
  573. {
  574.   struct lwp_info *lwp = get_thread_lwp (current_thread);
  575.   return lwp->arch_private->stopped_data_address;
  576. }

  577. /* Called when a new process is created.  */
  578. static struct arch_process_info *
  579. arm_new_process (void)
  580. {
  581.   struct arch_process_info *info = xcalloc (1, sizeof (*info));
  582.   return info;
  583. }

  584. /* Called when a new thread is detected.  */
  585. static struct arch_lwp_info *
  586. arm_new_thread (void)
  587. {
  588.   struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
  589.   int i;

  590.   for (i = 0; i < MAX_BPTS; i++)
  591.     info->bpts_changed[i] = 1;
  592.   for (i = 0; i < MAX_WPTS; i++)
  593.     info->wpts_changed[i] = 1;

  594.   return info;
  595. }

  596. /* Called when resuming a thread.
  597.    If the debug regs have changed, update the thread's copies.  */
  598. static void
  599. arm_prepare_to_resume (struct lwp_info *lwp)
  600. {
  601.   struct thread_info *thread = get_lwp_thread (lwp);
  602.   int pid = lwpid_of (thread);
  603.   struct process_info *proc = find_process_pid (pid_of (thread));
  604.   struct arch_process_info *proc_info = proc->private->arch_private;
  605.   struct arch_lwp_info *lwp_info = lwp->arch_private;
  606.   int i;

  607.   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
  608.     if (lwp_info->bpts_changed[i])
  609.       {
  610.         errno = 0;

  611.         if (arm_hwbp_control_is_enabled (proc_info->bpts[i].control))
  612.           if (ptrace (PTRACE_SETHBPREGS, pid,
  613.                       (PTRACE_TYPE_ARG3) ((i << 1) + 1),
  614.                       &proc_info->bpts[i].address) < 0)
  615.             perror_with_name ("Unexpected error setting breakpoint address");

  616.         if (arm_hwbp_control_is_initialized (proc_info->bpts[i].control))
  617.           if (ptrace (PTRACE_SETHBPREGS, pid,
  618.                       (PTRACE_TYPE_ARG3) ((i << 1) + 2),
  619.                       &proc_info->bpts[i].control) < 0)
  620.             perror_with_name ("Unexpected error setting breakpoint");

  621.         lwp_info->bpts_changed[i] = 0;
  622.       }

  623.   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
  624.     if (lwp_info->wpts_changed[i])
  625.       {
  626.         errno = 0;

  627.         if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
  628.           if (ptrace (PTRACE_SETHBPREGS, pid,
  629.                       (PTRACE_TYPE_ARG3) -((i << 1) + 1),
  630.                       &proc_info->wpts[i].address) < 0)
  631.             perror_with_name ("Unexpected error setting watchpoint address");

  632.         if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
  633.           if (ptrace (PTRACE_SETHBPREGS, pid,
  634.                       (PTRACE_TYPE_ARG3) -((i << 1) + 2),
  635.                       &proc_info->wpts[i].control) < 0)
  636.             perror_with_name ("Unexpected error setting watchpoint");

  637.         lwp_info->wpts_changed[i] = 0;
  638.       }
  639. }


  640. static int
  641. arm_get_hwcap (unsigned long *valp)
  642. {
  643.   unsigned char *data = alloca (8);
  644.   int offset = 0;

  645.   while ((*the_target->read_auxv) (offset, data, 8) == 8)
  646.     {
  647.       unsigned int *data_p = (unsigned int *)data;
  648.       if (data_p[0] == AT_HWCAP)
  649.         {
  650.           *valp = data_p[1];
  651.           return 1;
  652.         }

  653.       offset += 8;
  654.     }

  655.   *valp = 0;
  656.   return 0;
  657. }

  658. static const struct target_desc *
  659. arm_read_description (void)
  660. {
  661.   int pid = lwpid_of (current_thread);

  662.   /* Query hardware watchpoint/breakpoint capabilities.  */
  663.   arm_linux_init_hwbp_cap (pid);

  664.   arm_hwcap = 0;
  665.   if (arm_get_hwcap (&arm_hwcap) == 0)
  666.     return tdesc_arm;

  667.   if (arm_hwcap & HWCAP_IWMMXT)
  668.     return tdesc_arm_with_iwmmxt;

  669.   if (arm_hwcap & HWCAP_VFP)
  670.     {
  671.       const struct target_desc *result;
  672.       char *buf;

  673.       /* NEON implies either no VFP, or VFPv3-D32.  We only support
  674.          it with VFP.  */
  675.       if (arm_hwcap & HWCAP_NEON)
  676.         result = tdesc_arm_with_neon;
  677.       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
  678.         result = tdesc_arm_with_vfpv3;
  679.       else
  680.         result = tdesc_arm_with_vfpv2;

  681.       /* Now make sure that the kernel supports reading these
  682.          registers.  Support was added in 2.6.30.  */
  683.       errno = 0;
  684.       buf = xmalloc (32 * 8 + 4);
  685.       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
  686.           && errno == EIO)
  687.         {
  688.           arm_hwcap = 0;
  689.           result = tdesc_arm;
  690.         }
  691.       free (buf);

  692.       return result;
  693.     }

  694.   /* The default configuration uses legacy FPA registers, probably
  695.      simulated.  */
  696.   return tdesc_arm;
  697. }

  698. static void
  699. arm_arch_setup (void)
  700. {
  701.   current_process ()->tdesc = arm_read_description ();
  702. }

  703. static struct regset_info arm_regsets[] = {
  704.   { PTRACE_GETREGS, PTRACE_SETREGS, 0, 18 * 4,
  705.     GENERAL_REGS,
  706.     arm_fill_gregset, arm_store_gregset },
  707.   { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, 16 * 8 + 6 * 4,
  708.     EXTENDED_REGS,
  709.     arm_fill_wmmxregset, arm_store_wmmxregset },
  710.   { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, 32 * 8 + 4,
  711.     EXTENDED_REGS,
  712.     arm_fill_vfpregset, arm_store_vfpregset },
  713.   { 0, 0, 0, -1, -1, NULL, NULL }
  714. };

  715. static struct regsets_info arm_regsets_info =
  716.   {
  717.     arm_regsets, /* regsets */
  718.     0, /* num_regsets */
  719.     NULL, /* disabled_regsets */
  720.   };

  721. static struct usrregs_info arm_usrregs_info =
  722.   {
  723.     arm_num_regs,
  724.     arm_regmap,
  725.   };

  726. static struct regs_info regs_info =
  727.   {
  728.     NULL, /* regset_bitmap */
  729.     &arm_usrregs_info,
  730.     &arm_regsets_info
  731.   };

  732. static const struct regs_info *
  733. arm_regs_info (void)
  734. {
  735.   return &regs_info;
  736. }

  737. struct linux_target_ops the_low_target = {
  738.   arm_arch_setup,
  739.   arm_regs_info,
  740.   arm_cannot_fetch_register,
  741.   arm_cannot_store_register,
  742.   NULL, /* fetch_register */
  743.   arm_get_pc,
  744.   arm_set_pc,

  745.   /* Define an ARM-mode breakpoint; we only set breakpoints in the C
  746.      library, which is most likely to be ARM.  If the kernel supports
  747.      clone events, we will never insert a breakpoint, so even a Thumb
  748.      C library will work; so will mixing EABI/non-EABI gdbserver and
  749.      application.  */
  750. #ifndef __ARM_EABI__
  751.   (const unsigned char *) &arm_breakpoint,
  752. #else
  753.   (const unsigned char *) &arm_eabi_breakpoint,
  754. #endif
  755.   arm_breakpoint_len,
  756.   arm_reinsert_addr,
  757.   0,
  758.   arm_breakpoint_at,
  759.   arm_supports_z_point_type,
  760.   arm_insert_point,
  761.   arm_remove_point,
  762.   arm_stopped_by_watchpoint,
  763.   arm_stopped_data_address,
  764.   NULL, /* collect_ptrace_register */
  765.   NULL, /* supply_ptrace_register */
  766.   NULL, /* siginfo_fixup */
  767.   arm_new_process,
  768.   arm_new_thread,
  769.   arm_prepare_to_resume,
  770. };

  771. void
  772. initialize_low_arch (void)
  773. {
  774.   /* Initialize the Linux target descriptions.  */
  775.   init_registers_arm ();
  776.   init_registers_arm_with_iwmmxt ();
  777.   init_registers_arm_with_vfpv2 ();
  778.   init_registers_arm_with_vfpv3 ();
  779.   init_registers_arm_with_neon ();

  780.   initialize_regsets_info (&arm_regsets_info);
  781. }