Home | History | Annotate | Line # | Download | only in gdbserver
      1 /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
      2    Copyright (C) 1995-2024 Free Software Foundation, Inc.
      3 
      4    This file is part of GDB.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18 
     19 #include "linux-low.h"
     20 #include "arch/arm.h"
     21 #include "arch/arm-linux.h"
     22 #include "arch/arm-get-next-pcs.h"
     23 #include "linux-aarch32-low.h"
     24 #include "linux-aarch32-tdesc.h"
     25 #include "linux-arm-tdesc.h"
     26 #include "gdbsupport/gdb-checked-static-cast.h"
     27 
     28 #include <sys/uio.h>
     29 /* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
     30    On Bionic elf.h and linux/elf.h have conflicting definitions.  */
     31 #ifndef ELFMAG0
     32 #include <elf.h>
     33 #endif
     34 #include "nat/gdb_ptrace.h"
     35 #include <signal.h>
     36 #include <sys/syscall.h>
     37 
     38 #ifndef PTRACE_GET_THREAD_AREA
     39 #define PTRACE_GET_THREAD_AREA 22
     40 #endif
     41 
     42 #ifndef PTRACE_GETWMMXREGS
     43 # define PTRACE_GETWMMXREGS 18
     44 # define PTRACE_SETWMMXREGS 19
     45 #endif
     46 
     47 #ifndef PTRACE_GETVFPREGS
     48 # define PTRACE_GETVFPREGS 27
     49 # define PTRACE_SETVFPREGS 28
     50 #endif
     51 
     52 #ifndef PTRACE_GETHBPREGS
     53 #define PTRACE_GETHBPREGS 29
     54 #define PTRACE_SETHBPREGS 30
     55 #endif
     56 
     57 /* Linux target op definitions for the ARM architecture.  */
     58 
     59 class arm_target : public linux_process_target
     60 {
     61 public:
     62 
     63   const regs_info *get_regs_info () override;
     64 
     65   int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override;
     66 
     67   int breakpoint_kind_from_current_state (CORE_ADDR *pcptr) override;
     68 
     69   const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
     70 
     71   bool supports_software_single_step () override;
     72 
     73   bool supports_z_point_type (char z_type) override;
     74 
     75   bool supports_hardware_single_step () override;
     76 
     77 protected:
     78 
     79   void low_arch_setup () override;
     80 
     81   bool low_cannot_fetch_register (int regno) override;
     82 
     83   bool low_cannot_store_register (int regno) override;
     84 
     85   bool low_supports_breakpoints () override;
     86 
     87   CORE_ADDR low_get_pc (regcache *regcache) override;
     88 
     89   void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
     90 
     91   std::vector<CORE_ADDR> low_get_next_pcs (regcache *regcache) override;
     92 
     93   bool low_breakpoint_at (CORE_ADDR pc) override;
     94 
     95   int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
     96 			int size, raw_breakpoint *bp) override;
     97 
     98   int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
     99 			int size, raw_breakpoint *bp) override;
    100 
    101   bool low_stopped_by_watchpoint () override;
    102 
    103   CORE_ADDR low_stopped_data_address () override;
    104 
    105   arch_process_info *low_new_process () override;
    106 
    107   void low_delete_process (arch_process_info *info) override;
    108 
    109   void low_new_thread (lwp_info *) override;
    110 
    111   void low_delete_thread (arch_lwp_info *) override;
    112 
    113   void low_new_fork (process_info *parent, process_info *child) override;
    114 
    115   void low_prepare_to_resume (lwp_info *lwp) override;
    116 
    117   bool low_supports_catch_syscall () override;
    118 
    119   void low_get_syscall_trapinfo (regcache *regcache, int *sysno) override;
    120 };
    121 
    122 /* The singleton target ops object.  */
    123 
    124 static arm_target the_arm_target;
    125 
    126 bool
    127 arm_target::low_supports_breakpoints ()
    128 {
    129   return true;
    130 }
    131 
    132 CORE_ADDR
    133 arm_target::low_get_pc (regcache *regcache)
    134 {
    135   return linux_get_pc_32bit (regcache);
    136 }
    137 
    138 void
    139 arm_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
    140 {
    141   linux_set_pc_32bit (regcache, pc);
    142 }
    143 
    144 int
    145 arm_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
    146 {
    147   return arm_breakpoint_kind_from_pc (pcptr);
    148 }
    149 
    150 int
    151 arm_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
    152 {
    153   return arm_breakpoint_kind_from_current_state (pcptr);
    154 }
    155 
    156 const gdb_byte *
    157 arm_target::sw_breakpoint_from_kind (int kind, int *size)
    158 {
    159   return arm_sw_breakpoint_from_kind (kind, size);
    160 }
    161 
    162 bool
    163 arm_target::low_breakpoint_at (CORE_ADDR pc)
    164 {
    165   return arm_breakpoint_at (pc);
    166 }
    167 
    168 /* Information describing the hardware breakpoint capabilities.  */
    169 static struct
    170 {
    171   unsigned char arch;
    172   unsigned char max_wp_length;
    173   unsigned char wp_count;
    174   unsigned char bp_count;
    175 } arm_linux_hwbp_cap;
    176 
    177 /* Enum describing the different types of ARM hardware break-/watch-points.  */
    178 typedef enum
    179 {
    180   arm_hwbp_break = 0,
    181   arm_hwbp_load = 1,
    182   arm_hwbp_store = 2,
    183   arm_hwbp_access = 3
    184 } arm_hwbp_type;
    185 
    186 /* Type describing an ARM Hardware Breakpoint Control register value.  */
    187 typedef unsigned int arm_hwbp_control_t;
    188 
    189 /* Structure used to keep track of hardware break-/watch-points.  */
    190 struct arm_linux_hw_breakpoint
    191 {
    192   /* Address to break on, or being watched.  */
    193   unsigned int address;
    194   /* Control register for break-/watch- point.  */
    195   arm_hwbp_control_t control;
    196 };
    197 
    198 /* Since we cannot dynamically allocate subfields of arch_process_info,
    199    assume a maximum number of supported break-/watchpoints.  */
    200 #define MAX_BPTS 32
    201 #define MAX_WPTS 32
    202 
    203 /* Per-process arch-specific data we want to keep.  */
    204 struct arch_process_info
    205 {
    206   /* Hardware breakpoints for this process.  */
    207   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
    208   /* Hardware watchpoints for this process.  */
    209   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
    210 };
    211 
    212 /* Per-thread arch-specific data we want to keep.  */
    213 struct arch_lwp_info
    214 {
    215   /* Non-zero if our copy differs from what's recorded in the thread.  */
    216   char bpts_changed[MAX_BPTS];
    217   char wpts_changed[MAX_WPTS];
    218   /* Cached stopped data address.  */
    219   CORE_ADDR stopped_data_address;
    220 };
    221 
    222 /* These are in <asm/elf.h> in current kernels.  */
    223 #define HWCAP_VFP       64
    224 #define HWCAP_IWMMXT    512
    225 #define HWCAP_NEON      4096
    226 #define HWCAP_VFPv3     8192
    227 #define HWCAP_VFPv3D16  16384
    228 
    229 #ifdef HAVE_SYS_REG_H
    230 #include <sys/reg.h>
    231 #endif
    232 
    233 #define arm_num_regs 26
    234 
    235 static int arm_regmap[] = {
    236   0, 4, 8, 12, 16, 20, 24, 28,
    237   32, 36, 40, 44, 48, 52, 56, 60,
    238   -1, -1, -1, -1, -1, -1, -1, -1, -1,
    239   64
    240 };
    241 
    242 /* Forward declarations needed for get_next_pcs ops.  */
    243 static ULONGEST get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
    244 							   int len,
    245 							   int byte_order);
    246 
    247 static CORE_ADDR get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
    248 						CORE_ADDR val);
    249 
    250 static CORE_ADDR get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
    251 
    252 static int get_next_pcs_is_thumb (struct arm_get_next_pcs *self);
    253 
    254 /* get_next_pcs operations.  */
    255 static struct arm_get_next_pcs_ops get_next_pcs_ops = {
    256   get_next_pcs_read_memory_unsigned_integer,
    257   get_next_pcs_syscall_next_pc,
    258   get_next_pcs_addr_bits_remove,
    259   get_next_pcs_is_thumb,
    260   arm_linux_get_next_pcs_fixup,
    261 };
    262 
    263 bool
    264 arm_target::low_cannot_store_register (int regno)
    265 {
    266   return (regno >= arm_num_regs);
    267 }
    268 
    269 bool
    270 arm_target::low_cannot_fetch_register (int regno)
    271 {
    272   return (regno >= arm_num_regs);
    273 }
    274 
    275 static void
    276 arm_fill_wmmxregset (struct regcache *regcache, void *buf)
    277 {
    278   if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
    279     return;
    280 
    281   for (int i = 0; i < 16; i++)
    282     collect_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
    283 
    284   /* We only have access to wcssf, wcasf, and wcgr0-wcgr3.  */
    285   for (int i = 0; i < 6; i++)
    286     collect_register (regcache, arm_num_regs + i + 16,
    287 		      (char *) buf + 16 * 8 + i * 4);
    288 }
    289 
    290 static void
    291 arm_store_wmmxregset (struct regcache *regcache, const void *buf)
    292 {
    293   if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
    294     return;
    295 
    296   for (int i = 0; i < 16; i++)
    297     supply_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
    298 
    299   /* We only have access to wcssf, wcasf, and wcgr0-wcgr3.  */
    300   for (int i = 0; i < 6; i++)
    301     supply_register (regcache, arm_num_regs + i + 16,
    302 		     (char *) buf + 16 * 8 + i * 4);
    303 }
    304 
    305 static void
    306 arm_fill_vfpregset (struct regcache *regcache, void *buf)
    307 {
    308   int num;
    309 
    310   if (is_aarch32_linux_description (regcache->tdesc))
    311     num = 32;
    312   else
    313     {
    314       arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
    315 
    316       if (fp_type == ARM_FP_TYPE_VFPV3)
    317 	num = 32;
    318       else if (fp_type == ARM_FP_TYPE_VFPV2)
    319 	num = 16;
    320       else
    321 	return;
    322     }
    323 
    324   arm_fill_vfpregset_num (regcache, buf, num);
    325 }
    326 
    327 /* Wrapper of UNMAKE_THUMB_ADDR for get_next_pcs.  */
    328 static CORE_ADDR
    329 get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self, CORE_ADDR val)
    330 {
    331   return UNMAKE_THUMB_ADDR (val);
    332 }
    333 
    334 static void
    335 arm_store_vfpregset (struct regcache *regcache, const void *buf)
    336 {
    337   int num;
    338 
    339   if (is_aarch32_linux_description (regcache->tdesc))
    340     num = 32;
    341   else
    342     {
    343       arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
    344 
    345       if (fp_type == ARM_FP_TYPE_VFPV3)
    346 	num = 32;
    347       else if (fp_type == ARM_FP_TYPE_VFPV2)
    348 	num = 16;
    349       else
    350 	return;
    351     }
    352 
    353   arm_store_vfpregset_num (regcache, buf, num);
    354 }
    355 
    356 /* Wrapper of arm_is_thumb_mode for get_next_pcs.  */
    357 static int
    358 get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
    359 {
    360   return arm_is_thumb_mode ();
    361 }
    362 
    363 /* Read memory from the inferior.
    364    BYTE_ORDER is ignored and there to keep compatibility with GDB's
    365    read_memory_unsigned_integer. */
    366 static ULONGEST
    367 get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
    368 					   int len,
    369 					   int byte_order)
    370 {
    371   ULONGEST res;
    372 
    373   res = 0;
    374   target_read_memory (memaddr, (unsigned char *) &res, len);
    375 
    376   return res;
    377 }
    378 
    379 /* Fetch the thread-local storage pointer for libthread_db.  */
    380 
    381 ps_err_e
    382 ps_get_thread_area (struct ps_prochandle *ph,
    383 		    lwpid_t lwpid, int idx, void **base)
    384 {
    385   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
    386     return PS_ERR;
    387 
    388   /* IDX is the bias from the thread pointer to the beginning of the
    389      thread descriptor.  It has to be subtracted due to implementation
    390      quirks in libthread_db.  */
    391   *base = (void *) ((char *)*base - idx);
    392 
    393   return PS_OK;
    394 }
    395 
    396 
    397 /* Query Hardware Breakpoint information for the target we are attached to
    398    (using PID as ptrace argument) and set up arm_linux_hwbp_cap.  */
    399 static void
    400 arm_linux_init_hwbp_cap (int pid)
    401 {
    402   unsigned int val;
    403 
    404   if (ptrace (PTRACE_GETHBPREGS, pid, 0, &val) < 0)
    405     return;
    406 
    407   arm_linux_hwbp_cap.arch = (unsigned char)((val >> 24) & 0xff);
    408   if (arm_linux_hwbp_cap.arch == 0)
    409     return;
    410 
    411   arm_linux_hwbp_cap.max_wp_length = (unsigned char)((val >> 16) & 0xff);
    412   arm_linux_hwbp_cap.wp_count = (unsigned char)((val >> 8) & 0xff);
    413   arm_linux_hwbp_cap.bp_count = (unsigned char)(val & 0xff);
    414 
    415   if (arm_linux_hwbp_cap.wp_count > MAX_WPTS)
    416     internal_error ("Unsupported number of watchpoints");
    417   if (arm_linux_hwbp_cap.bp_count > MAX_BPTS)
    418     internal_error ("Unsupported number of breakpoints");
    419 }
    420 
    421 /* How many hardware breakpoints are available?  */
    422 static int
    423 arm_linux_get_hw_breakpoint_count (void)
    424 {
    425   return arm_linux_hwbp_cap.bp_count;
    426 }
    427 
    428 /* How many hardware watchpoints are available?  */
    429 static int
    430 arm_linux_get_hw_watchpoint_count (void)
    431 {
    432   return arm_linux_hwbp_cap.wp_count;
    433 }
    434 
    435 /* Maximum length of area watched by hardware watchpoint.  */
    436 static int
    437 arm_linux_get_hw_watchpoint_max_length (void)
    438 {
    439   return arm_linux_hwbp_cap.max_wp_length;
    440 }
    441 
    442 /* Initialize an ARM hardware break-/watch-point control register value.
    443    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
    444    type of break-/watch-point; ENABLE indicates whether the point is enabled.
    445    */
    446 static arm_hwbp_control_t
    447 arm_hwbp_control_initialize (unsigned byte_address_select,
    448 			     arm_hwbp_type hwbp_type,
    449 			     int enable)
    450 {
    451   gdb_assert ((byte_address_select & ~0xffU) == 0);
    452   gdb_assert (hwbp_type != arm_hwbp_break
    453 	      || ((byte_address_select & 0xfU) != 0));
    454 
    455   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
    456 }
    457 
    458 /* Does the breakpoint control value CONTROL have the enable bit set?  */
    459 static int
    460 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
    461 {
    462   return control & 0x1;
    463 }
    464 
    465 /* Is the breakpoint control value CONTROL initialized?  */
    466 static int
    467 arm_hwbp_control_is_initialized (arm_hwbp_control_t control)
    468 {
    469   return control != 0;
    470 }
    471 
    472 /* Change a breakpoint control word so that it is in the disabled state.  */
    473 static arm_hwbp_control_t
    474 arm_hwbp_control_disable (arm_hwbp_control_t control)
    475 {
    476   return control & ~0x1;
    477 }
    478 
    479 /* Are two break-/watch-points equal?  */
    480 static int
    481 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
    482 			       const struct arm_linux_hw_breakpoint *p2)
    483 {
    484   return p1->address == p2->address && p1->control == p2->control;
    485 }
    486 
    487 /* Convert a raw breakpoint type to an enum arm_hwbp_type.  */
    488 
    489 static arm_hwbp_type
    490 raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
    491 {
    492   switch (raw_type)
    493     {
    494     case raw_bkpt_type_hw:
    495       return arm_hwbp_break;
    496     case raw_bkpt_type_write_wp:
    497       return arm_hwbp_store;
    498     case raw_bkpt_type_read_wp:
    499       return arm_hwbp_load;
    500     case raw_bkpt_type_access_wp:
    501       return arm_hwbp_access;
    502     default:
    503       gdb_assert_not_reached ("unhandled raw type");
    504     }
    505 }
    506 
    507 /* Initialize the hardware breakpoint structure P for a breakpoint or
    508    watchpoint at ADDR to LEN.  The type of watchpoint is given in TYPE.
    509    Returns -1 if TYPE is unsupported, or -2 if the particular combination
    510    of ADDR and LEN cannot be implemented.  Otherwise, returns 0 if TYPE
    511    represents a breakpoint and 1 if type represents a watchpoint.  */
    512 static int
    513 arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
    514 			       int len, struct arm_linux_hw_breakpoint *p)
    515 {
    516   arm_hwbp_type hwbp_type;
    517   unsigned mask;
    518 
    519   hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);
    520 
    521   if (hwbp_type == arm_hwbp_break)
    522     {
    523       /* For breakpoints, the length field encodes the mode.  */
    524       switch (len)
    525 	{
    526 	case 2:	 /* 16-bit Thumb mode breakpoint */
    527 	case 3:  /* 32-bit Thumb mode breakpoint */
    528 	  mask = 0x3;
    529 	  addr &= ~1;
    530 	  break;
    531 	case 4:  /* 32-bit ARM mode breakpoint */
    532 	  mask = 0xf;
    533 	  addr &= ~3;
    534 	  break;
    535 	default:
    536 	  /* Unsupported. */
    537 	  return -2;
    538 	}
    539     }
    540   else
    541     {
    542       CORE_ADDR max_wp_length = arm_linux_get_hw_watchpoint_max_length ();
    543       CORE_ADDR aligned_addr;
    544 
    545       /* Can not set watchpoints for zero or negative lengths.  */
    546       if (len <= 0)
    547 	return -2;
    548       /* The current ptrace interface can only handle watchpoints that are a
    549 	 power of 2.  */
    550       if ((len & (len - 1)) != 0)
    551 	return -2;
    552 
    553       /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
    554 	 range covered by a watchpoint.  */
    555       aligned_addr = addr & ~(max_wp_length - 1);
    556       if (aligned_addr + max_wp_length < addr + len)
    557 	return -2;
    558 
    559       mask = (1 << len) - 1;
    560     }
    561 
    562   p->address = (unsigned int) addr;
    563   p->control = arm_hwbp_control_initialize (mask, hwbp_type, 1);
    564 
    565   return hwbp_type != arm_hwbp_break;
    566 }
    567 
    568 /* Callback to mark a watch-/breakpoint to be updated in all threads of
    569    the current process.  */
    570 
    571 static void
    572 update_registers_callback (thread_info *thread, int watch, int i)
    573 {
    574   struct lwp_info *lwp = get_thread_lwp (thread);
    575 
    576   /* The actual update is done later just before resuming the lwp,
    577      we just mark that the registers need updating.  */
    578   if (watch)
    579     lwp->arch_private->wpts_changed[i] = 1;
    580   else
    581     lwp->arch_private->bpts_changed[i] = 1;
    582 
    583   /* If the lwp isn't stopped, force it to momentarily pause, so
    584      we can update its breakpoint registers.  */
    585   if (!lwp->stopped)
    586     linux_stop_lwp (lwp);
    587 }
    588 
    589 bool
    590 arm_target::supports_z_point_type (char z_type)
    591 {
    592   switch (z_type)
    593     {
    594     case Z_PACKET_SW_BP:
    595     case Z_PACKET_HW_BP:
    596     case Z_PACKET_WRITE_WP:
    597     case Z_PACKET_READ_WP:
    598     case Z_PACKET_ACCESS_WP:
    599       return true;
    600     default:
    601       /* Leave the handling of sw breakpoints with the gdb client.  */
    602       return false;
    603     }
    604 }
    605 
    606 /* Insert hardware break-/watchpoint.  */
    607 int
    608 arm_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
    609 			      int len, raw_breakpoint *bp)
    610 {
    611   struct process_info *proc = current_process ();
    612   struct arm_linux_hw_breakpoint p, *pts;
    613   int watch, i, count;
    614 
    615   watch = arm_linux_hw_point_initialize (type, addr, len, &p);
    616   if (watch < 0)
    617     {
    618       /* Unsupported.  */
    619       return watch == -1 ? 1 : -1;
    620     }
    621 
    622   if (watch)
    623     {
    624       count = arm_linux_get_hw_watchpoint_count ();
    625       pts = proc->priv->arch_private->wpts;
    626     }
    627   else
    628     {
    629       count = arm_linux_get_hw_breakpoint_count ();
    630       pts = proc->priv->arch_private->bpts;
    631     }
    632 
    633   for (i = 0; i < count; i++)
    634     if (!arm_hwbp_control_is_enabled (pts[i].control))
    635       {
    636 	pts[i] = p;
    637 
    638 	/* Only update the threads of the current process.  */
    639 	current_process ()->for_each_thread ([&] (thread_info *thread)
    640 	  {
    641 	    update_registers_callback (thread, watch, i);
    642 	  });
    643 
    644 	return 0;
    645       }
    646 
    647   /* We're out of watchpoints.  */
    648   return -1;
    649 }
    650 
    651 /* Remove hardware break-/watchpoint.  */
    652 int
    653 arm_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
    654 			      int len, raw_breakpoint *bp)
    655 {
    656   struct process_info *proc = current_process ();
    657   struct arm_linux_hw_breakpoint p, *pts;
    658   int watch, i, count;
    659 
    660   watch = arm_linux_hw_point_initialize (type, addr, len, &p);
    661   if (watch < 0)
    662     {
    663       /* Unsupported.  */
    664       return -1;
    665     }
    666 
    667   if (watch)
    668     {
    669       count = arm_linux_get_hw_watchpoint_count ();
    670       pts = proc->priv->arch_private->wpts;
    671     }
    672   else
    673     {
    674       count = arm_linux_get_hw_breakpoint_count ();
    675       pts = proc->priv->arch_private->bpts;
    676     }
    677 
    678   for (i = 0; i < count; i++)
    679     if (arm_linux_hw_breakpoint_equal (&p, pts + i))
    680       {
    681 	pts[i].control = arm_hwbp_control_disable (pts[i].control);
    682 
    683 	/* Only update the threads of the current process.  */
    684 	current_process ()->for_each_thread ([&] (thread_info *thread)
    685 	  {
    686 	    update_registers_callback (thread, watch, i);
    687 	  });
    688 
    689 	return 0;
    690       }
    691 
    692   /* No watchpoint matched.  */
    693   return -1;
    694 }
    695 
    696 /* Return whether current thread is stopped due to a watchpoint.  */
    697 bool
    698 arm_target::low_stopped_by_watchpoint ()
    699 {
    700   struct lwp_info *lwp = get_thread_lwp (current_thread);
    701   siginfo_t siginfo;
    702 
    703   /* We must be able to set hardware watchpoints.  */
    704   if (arm_linux_get_hw_watchpoint_count () == 0)
    705     return false;
    706 
    707   /* Retrieve siginfo.  */
    708   errno = 0;
    709   ptrace (PTRACE_GETSIGINFO, current_thread->id.lwp (), 0, &siginfo);
    710   if (errno != 0)
    711     return false;
    712 
    713   /* This must be a hardware breakpoint.  */
    714   if (siginfo.si_signo != SIGTRAP
    715       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
    716     return false;
    717 
    718   /* If we are in a positive slot then we're looking at a breakpoint and not
    719      a watchpoint.  */
    720   if (siginfo.si_errno >= 0)
    721     return false;
    722 
    723   /* Cache stopped data address for use by arm_stopped_data_address.  */
    724   lwp->arch_private->stopped_data_address
    725     = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
    726 
    727   return true;
    728 }
    729 
    730 /* Return data address that triggered watchpoint.  Called only if
    731    low_stopped_by_watchpoint returned true.  */
    732 CORE_ADDR
    733 arm_target::low_stopped_data_address ()
    734 {
    735   struct lwp_info *lwp = get_thread_lwp (current_thread);
    736   return lwp->arch_private->stopped_data_address;
    737 }
    738 
    739 /* Called when a new process is created.  */
    740 arch_process_info *
    741 arm_target::low_new_process ()
    742 {
    743   struct arch_process_info *info = XCNEW (struct arch_process_info);
    744   return info;
    745 }
    746 
    747 /* Called when a process is being deleted.  */
    748 
    749 void
    750 arm_target::low_delete_process (arch_process_info *info)
    751 {
    752   xfree (info);
    753 }
    754 
    755 /* Called when a new thread is detected.  */
    756 void
    757 arm_target::low_new_thread (lwp_info *lwp)
    758 {
    759   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
    760   int i;
    761 
    762   for (i = 0; i < MAX_BPTS; i++)
    763     info->bpts_changed[i] = 1;
    764   for (i = 0; i < MAX_WPTS; i++)
    765     info->wpts_changed[i] = 1;
    766 
    767   lwp->arch_private = info;
    768 }
    769 
    770 /* Function to call when a thread is being deleted.  */
    771 
    772 void
    773 arm_target::low_delete_thread (arch_lwp_info *arch_lwp)
    774 {
    775   xfree (arch_lwp);
    776 }
    777 
    778 void
    779 arm_target::low_new_fork (process_info *parent, process_info *child)
    780 {
    781   struct arch_process_info *parent_proc_info;
    782   struct arch_process_info *child_proc_info;
    783   struct lwp_info *child_lwp;
    784   struct arch_lwp_info *child_lwp_info;
    785   int i;
    786 
    787   /* These are allocated by linux_add_process.  */
    788   gdb_assert (parent->priv != NULL
    789 	      && parent->priv->arch_private != NULL);
    790   gdb_assert (child->priv != NULL
    791 	      && child->priv->arch_private != NULL);
    792 
    793   parent_proc_info = parent->priv->arch_private;
    794   child_proc_info = child->priv->arch_private;
    795 
    796   /* Linux kernel before 2.6.33 commit
    797      72f674d203cd230426437cdcf7dd6f681dad8b0d
    798      will inherit hardware debug registers from parent
    799      on fork/vfork/clone.  Newer Linux kernels create such tasks with
    800      zeroed debug registers.
    801 
    802      GDB core assumes the child inherits the watchpoints/hw
    803      breakpoints of the parent, and will remove them all from the
    804      forked off process.  Copy the debug registers mirrors into the
    805      new process so that all breakpoints and watchpoints can be
    806      removed together.  The debug registers mirror will become zeroed
    807      in the end before detaching the forked off process, thus making
    808      this compatible with older Linux kernels too.  */
    809 
    810   *child_proc_info = *parent_proc_info;
    811 
    812   /* Mark all the hardware breakpoints and watchpoints as changed to
    813      make sure that the registers will be updated.  */
    814   child_lwp = find_lwp_pid (ptid_t (child->pid));
    815   child_lwp_info = child_lwp->arch_private;
    816   for (i = 0; i < MAX_BPTS; i++)
    817     child_lwp_info->bpts_changed[i] = 1;
    818   for (i = 0; i < MAX_WPTS; i++)
    819     child_lwp_info->wpts_changed[i] = 1;
    820 }
    821 
    822 /* For PID, set the address register of hardware breakpoint pair I to
    823    ADDRESS.  */
    824 
    825 static void
    826 sethbpregs_hwbp_address (int pid, int i, unsigned int address)
    827 {
    828   PTRACE_TYPE_ARG3 address_reg = (PTRACE_TYPE_ARG3) ((i << 1) + 1);
    829 
    830   errno = 0;
    831 
    832   if (ptrace (PTRACE_SETHBPREGS, pid, address_reg, &address) < 0)
    833     perror_with_name (_("Unexpected error updating breakpoint address"));
    834 }
    835 
    836 /* For PID, set the control register of hardware breakpoint pair I to
    837    CONTROL.  */
    838 
    839 static void
    840 sethbpregs_hwbp_control (int pid, int i, arm_hwbp_control_t control)
    841 {
    842   PTRACE_TYPE_ARG3 control_reg = (PTRACE_TYPE_ARG3) ((i << 1) + 2);
    843 
    844   errno = 0;
    845 
    846   if (ptrace (PTRACE_SETHBPREGS, pid, control_reg, &control) < 0)
    847     perror_with_name (_("Unexpected error setting breakpoint control"));
    848 }
    849 
    850 /* Called when resuming a thread.
    851    If the debug regs have changed, update the thread's copies.  */
    852 void
    853 arm_target::low_prepare_to_resume (lwp_info *lwp)
    854 {
    855   thread_info *thread = lwp->thread;
    856   int pid = thread->id.lwp ();
    857   process_info *proc = find_process_pid (thread->id.pid ());
    858   struct arch_process_info *proc_info = proc->priv->arch_private;
    859   struct arch_lwp_info *lwp_info = lwp->arch_private;
    860   int i;
    861 
    862   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
    863     if (lwp_info->bpts_changed[i])
    864       {
    865 	unsigned int address = proc_info->bpts[i].address;
    866 	arm_hwbp_control_t control = proc_info->bpts[i].control;
    867 
    868 	if (!arm_hwbp_control_is_initialized (control))
    869 	  {
    870 	    /* Nothing to do.  */
    871 	  }
    872 	else if (!arm_hwbp_control_is_enabled (control))
    873 	  {
    874 	    /* Disable hardware breakpoint, just write the control
    875 	       register.  */
    876 	    sethbpregs_hwbp_control (pid, i, control);
    877 	  }
    878 	else
    879 	  {
    880 	    /* See arm_linux_nat_target::low_prepare_to_resume for detailed
    881 	       comment.  */
    882 	    unsigned int aligned_address = address & ~0x7U;
    883 	    if (aligned_address != address)
    884 	      {
    885 		sethbpregs_hwbp_address (pid, i, aligned_address);
    886 		sethbpregs_hwbp_control (pid, i, control);
    887 	      }
    888 	    sethbpregs_hwbp_address (pid, i, address);
    889 	    sethbpregs_hwbp_control (pid, i, control);
    890 	  }
    891 
    892 	lwp_info->bpts_changed[i] = 0;
    893       }
    894 
    895   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
    896     if (lwp_info->wpts_changed[i])
    897       {
    898 	errno = 0;
    899 
    900 	if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
    901 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    902 		      (PTRACE_TYPE_ARG3) -((i << 1) + 1),
    903 		      &proc_info->wpts[i].address) < 0)
    904 	    perror_with_name ("Unexpected error setting watchpoint address");
    905 
    906 	if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
    907 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    908 		      (PTRACE_TYPE_ARG3) -((i << 1) + 2),
    909 		      &proc_info->wpts[i].control) < 0)
    910 	    perror_with_name ("Unexpected error setting watchpoint");
    911 
    912 	lwp_info->wpts_changed[i] = 0;
    913       }
    914 }
    915 
    916 /* Find the next pc for a sigreturn or rt_sigreturn syscall.  In
    917    addition, set IS_THUMB depending on whether we will return to ARM
    918    or Thumb code.
    919    See arm-linux.h for stack layout details.  */
    920 static CORE_ADDR
    921 arm_sigreturn_next_pc (struct regcache *regcache, int svc_number,
    922 		       int *is_thumb)
    923 {
    924   unsigned long sp;
    925   unsigned long sp_data;
    926   /* Offset of PC register.  */
    927   int pc_offset = 0;
    928   CORE_ADDR next_pc = 0;
    929   uint32_t cpsr;
    930 
    931   gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);
    932 
    933   collect_register_by_name (regcache, "sp", &sp);
    934   the_target->read_memory (sp, (unsigned char *) &sp_data, 4);
    935 
    936   pc_offset = arm_linux_sigreturn_next_pc_offset
    937     (sp, sp_data, svc_number, __NR_sigreturn == svc_number ? 1 : 0);
    938 
    939   the_target->read_memory (sp + pc_offset, (unsigned char *) &next_pc, 4);
    940 
    941   /* Set IS_THUMB according the CPSR saved on the stack.  */
    942   the_target->read_memory (sp + pc_offset + 4, (unsigned char *) &cpsr, 4);
    943   *is_thumb = ((cpsr & CPSR_T) != 0);
    944 
    945   return next_pc;
    946 }
    947 
    948 /* When PC is at a syscall instruction, return the PC of the next
    949    instruction to be executed.  */
    950 static CORE_ADDR
    951 get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
    952 {
    953   CORE_ADDR next_pc = 0;
    954   CORE_ADDR pc = regcache_read_pc (self->regcache);
    955   int is_thumb = arm_is_thumb_mode ();
    956   ULONGEST svc_number = 0;
    957   regcache *regcache
    958     = gdb::checked_static_cast<struct regcache *> (self->regcache);
    959 
    960   if (is_thumb)
    961     {
    962       collect_register (regcache, 7, &svc_number);
    963       next_pc = pc + 2;
    964     }
    965   else
    966     {
    967       unsigned long this_instr;
    968       unsigned long svc_operand;
    969 
    970       target_read_memory (pc, (unsigned char *) &this_instr, 4);
    971       svc_operand = (0x00ffffff & this_instr);
    972 
    973       if (svc_operand)  /* OABI.  */
    974 	{
    975 	  svc_number = svc_operand - 0x900000;
    976 	}
    977       else /* EABI.  */
    978 	{
    979 	  collect_register (regcache, 7, &svc_number);
    980 	}
    981 
    982       next_pc = pc + 4;
    983     }
    984 
    985   /* This is a sigreturn or sigreturn_rt syscall.  */
    986   if (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn)
    987     {
    988       /* SIGRETURN or RT_SIGRETURN may affect the arm thumb mode, so
    989 	 update IS_THUMB.   */
    990       next_pc = arm_sigreturn_next_pc (regcache, svc_number, &is_thumb);
    991     }
    992 
    993   /* Addresses for calling Thumb functions have the bit 0 set.  */
    994   if (is_thumb)
    995     next_pc = MAKE_THUMB_ADDR (next_pc);
    996 
    997   return next_pc;
    998 }
    999 
   1000 static const struct target_desc *
   1001 arm_read_description (void)
   1002 {
   1003   unsigned long arm_hwcap = linux_get_hwcap (current_thread->id.pid (), 4);
   1004 
   1005   if (arm_hwcap & HWCAP_IWMMXT)
   1006     return arm_linux_read_description (ARM_FP_TYPE_IWMMXT);
   1007 
   1008   if (arm_hwcap & HWCAP_VFP)
   1009     {
   1010       /* Make sure that the kernel supports reading VFP registers.  Support was
   1011 	 added in 2.6.30.  */
   1012       int pid = current_thread->id.lwp ();
   1013       errno = 0;
   1014       char *buf = (char *) alloca (ARM_VFP3_REGS_SIZE);
   1015       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0 && errno == EIO)
   1016 	return arm_linux_read_description (ARM_FP_TYPE_NONE);
   1017 
   1018       /* NEON implies either no VFP, or VFPv3-D32.  We only support
   1019 	 it with VFP.  */
   1020       if (arm_hwcap & HWCAP_NEON)
   1021 	return aarch32_linux_read_description ();
   1022       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
   1023 	return arm_linux_read_description (ARM_FP_TYPE_VFPV3);
   1024       else
   1025 	return arm_linux_read_description (ARM_FP_TYPE_VFPV2);
   1026     }
   1027 
   1028   /* The default configuration uses legacy FPA registers, probably
   1029      simulated.  */
   1030   return arm_linux_read_description (ARM_FP_TYPE_NONE);
   1031 }
   1032 
   1033 void
   1034 arm_target::low_arch_setup ()
   1035 {
   1036   int tid = current_thread->id.lwp ();
   1037   int gpregs[18];
   1038   struct iovec iov;
   1039 
   1040   /* Query hardware watchpoint/breakpoint capabilities.  */
   1041   arm_linux_init_hwbp_cap (tid);
   1042 
   1043   current_process ()->tdesc = arm_read_description ();
   1044 
   1045   iov.iov_base = gpregs;
   1046   iov.iov_len = sizeof (gpregs);
   1047 
   1048   /* Check if PTRACE_GETREGSET works.  */
   1049   if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0)
   1050     have_ptrace_getregset = TRIBOOL_TRUE;
   1051   else
   1052     have_ptrace_getregset = TRIBOOL_FALSE;
   1053 }
   1054 
   1055 bool
   1056 arm_target::supports_software_single_step ()
   1057 {
   1058   return true;
   1059 }
   1060 
   1061 /* Fetch the next possible PCs after the current instruction executes.  */
   1062 
   1063 std::vector<CORE_ADDR>
   1064 arm_target::low_get_next_pcs (regcache *regcache)
   1065 {
   1066   struct arm_get_next_pcs next_pcs_ctx;
   1067 
   1068   arm_get_next_pcs_ctor (&next_pcs_ctx,
   1069 			 &get_next_pcs_ops,
   1070 			 /* Byte order is ignored assumed as host.  */
   1071 			 0,
   1072 			 0,
   1073 			 1,
   1074 			 regcache);
   1075 
   1076   return arm_get_next_pcs (&next_pcs_ctx);
   1077 }
   1078 
   1079 /* Support for hardware single step.  */
   1080 
   1081 bool
   1082 arm_target::supports_hardware_single_step ()
   1083 {
   1084   return false;
   1085 }
   1086 
   1087 bool
   1088 arm_target::low_supports_catch_syscall ()
   1089 {
   1090   return true;
   1091 }
   1092 
   1093 /* Implementation of linux target ops method "low_get_syscall_trapinfo".  */
   1094 
   1095 void
   1096 arm_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
   1097 {
   1098   if (arm_is_thumb_mode ())
   1099     collect_register_by_name (regcache, "r7", sysno);
   1100   else
   1101     {
   1102       unsigned long pc;
   1103       unsigned long insn;
   1104 
   1105       collect_register_by_name (regcache, "pc", &pc);
   1106 
   1107       if (read_memory (pc - 4, (unsigned char *) &insn, 4))
   1108 	*sysno = UNKNOWN_SYSCALL;
   1109       else
   1110 	{
   1111 	  unsigned long svc_operand = (0x00ffffff & insn);
   1112 
   1113 	  if (svc_operand)
   1114 	    {
   1115 	      /* OABI */
   1116 	      *sysno = svc_operand - 0x900000;
   1117 	    }
   1118 	  else
   1119 	    {
   1120 	      /* EABI */
   1121 	      collect_register_by_name (regcache, "r7", sysno);
   1122 	    }
   1123 	}
   1124     }
   1125 }
   1126 
   1127 /* Register sets without using PTRACE_GETREGSET.  */
   1128 
   1129 static struct regset_info arm_regsets[] = {
   1130   { PTRACE_GETREGS, PTRACE_SETREGS, 0,
   1131     ARM_CORE_REGS_SIZE + ARM_INT_REGISTER_SIZE, GENERAL_REGS,
   1132     arm_fill_gregset, arm_store_gregset },
   1133   { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, IWMMXT_REGS_SIZE, EXTENDED_REGS,
   1134     arm_fill_wmmxregset, arm_store_wmmxregset },
   1135   { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, ARM_VFP3_REGS_SIZE, EXTENDED_REGS,
   1136     arm_fill_vfpregset, arm_store_vfpregset },
   1137   NULL_REGSET
   1138 };
   1139 
   1140 static struct regsets_info arm_regsets_info =
   1141   {
   1142     arm_regsets, /* regsets */
   1143     0, /* num_regsets */
   1144     NULL, /* disabled_regsets */
   1145   };
   1146 
   1147 static struct usrregs_info arm_usrregs_info =
   1148   {
   1149     arm_num_regs,
   1150     arm_regmap,
   1151   };
   1152 
   1153 static struct regs_info regs_info_arm =
   1154   {
   1155     NULL, /* regset_bitmap */
   1156     &arm_usrregs_info,
   1157     &arm_regsets_info
   1158   };
   1159 
   1160 const regs_info *
   1161 arm_target::get_regs_info ()
   1162 {
   1163   const struct target_desc *tdesc = current_process ()->tdesc;
   1164 
   1165   if (have_ptrace_getregset == TRIBOOL_TRUE
   1166       && (is_aarch32_linux_description (tdesc)
   1167 	  || arm_linux_get_tdesc_fp_type (tdesc) == ARM_FP_TYPE_VFPV3))
   1168     return &regs_info_aarch32;
   1169 
   1170   return &regs_info_arm;
   1171 }
   1172 
   1173 /* The linux target ops object.  */
   1174 
   1175 linux_process_target *the_linux_target = &the_arm_target;
   1176 
   1177 void
   1178 initialize_low_arch (void)
   1179 {
   1180   initialize_low_arch_aarch32 ();
   1181   initialize_regsets_info (&arm_regsets_info);
   1182 }
   1183