Home | History | Annotate | Line # | Download | only in gdbserver
linux-arm-low.cc revision 1.1.1.3
      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 compatiblity 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 	for_each_thread (current_thread->id.pid (), [&] (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 	for_each_thread (current_thread->id.pid (), [&] (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, lwpid_of (current_thread), 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 /* Called when resuming a thread.
    823    If the debug regs have changed, update the thread's copies.  */
    824 void
    825 arm_target::low_prepare_to_resume (lwp_info *lwp)
    826 {
    827   struct thread_info *thread = get_lwp_thread (lwp);
    828   int pid = lwpid_of (thread);
    829   struct process_info *proc = find_process_pid (pid_of (thread));
    830   struct arch_process_info *proc_info = proc->priv->arch_private;
    831   struct arch_lwp_info *lwp_info = lwp->arch_private;
    832   int i;
    833 
    834   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
    835     if (lwp_info->bpts_changed[i])
    836       {
    837 	errno = 0;
    838 
    839 	if (arm_hwbp_control_is_enabled (proc_info->bpts[i].control))
    840 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    841 		      (PTRACE_TYPE_ARG3) ((i << 1) + 1),
    842 		      &proc_info->bpts[i].address) < 0)
    843 	    perror_with_name ("Unexpected error setting breakpoint address");
    844 
    845 	if (arm_hwbp_control_is_initialized (proc_info->bpts[i].control))
    846 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    847 		      (PTRACE_TYPE_ARG3) ((i << 1) + 2),
    848 		      &proc_info->bpts[i].control) < 0)
    849 	    perror_with_name ("Unexpected error setting breakpoint");
    850 
    851 	lwp_info->bpts_changed[i] = 0;
    852       }
    853 
    854   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
    855     if (lwp_info->wpts_changed[i])
    856       {
    857 	errno = 0;
    858 
    859 	if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
    860 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    861 		      (PTRACE_TYPE_ARG3) -((i << 1) + 1),
    862 		      &proc_info->wpts[i].address) < 0)
    863 	    perror_with_name ("Unexpected error setting watchpoint address");
    864 
    865 	if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
    866 	  if (ptrace (PTRACE_SETHBPREGS, pid,
    867 		      (PTRACE_TYPE_ARG3) -((i << 1) + 2),
    868 		      &proc_info->wpts[i].control) < 0)
    869 	    perror_with_name ("Unexpected error setting watchpoint");
    870 
    871 	lwp_info->wpts_changed[i] = 0;
    872       }
    873 }
    874 
    875 /* Find the next pc for a sigreturn or rt_sigreturn syscall.  In
    876    addition, set IS_THUMB depending on whether we will return to ARM
    877    or Thumb code.
    878    See arm-linux.h for stack layout details.  */
    879 static CORE_ADDR
    880 arm_sigreturn_next_pc (struct regcache *regcache, int svc_number,
    881 		       int *is_thumb)
    882 {
    883   unsigned long sp;
    884   unsigned long sp_data;
    885   /* Offset of PC register.  */
    886   int pc_offset = 0;
    887   CORE_ADDR next_pc = 0;
    888   uint32_t cpsr;
    889 
    890   gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);
    891 
    892   collect_register_by_name (regcache, "sp", &sp);
    893   the_target->read_memory (sp, (unsigned char *) &sp_data, 4);
    894 
    895   pc_offset = arm_linux_sigreturn_next_pc_offset
    896     (sp, sp_data, svc_number, __NR_sigreturn == svc_number ? 1 : 0);
    897 
    898   the_target->read_memory (sp + pc_offset, (unsigned char *) &next_pc, 4);
    899 
    900   /* Set IS_THUMB according the CPSR saved on the stack.  */
    901   the_target->read_memory (sp + pc_offset + 4, (unsigned char *) &cpsr, 4);
    902   *is_thumb = ((cpsr & CPSR_T) != 0);
    903 
    904   return next_pc;
    905 }
    906 
    907 /* When PC is at a syscall instruction, return the PC of the next
    908    instruction to be executed.  */
    909 static CORE_ADDR
    910 get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
    911 {
    912   CORE_ADDR next_pc = 0;
    913   CORE_ADDR pc = regcache_read_pc (self->regcache);
    914   int is_thumb = arm_is_thumb_mode ();
    915   ULONGEST svc_number = 0;
    916   regcache *regcache
    917     = gdb::checked_static_cast<struct regcache *> (self->regcache);
    918 
    919   if (is_thumb)
    920     {
    921       collect_register (regcache, 7, &svc_number);
    922       next_pc = pc + 2;
    923     }
    924   else
    925     {
    926       unsigned long this_instr;
    927       unsigned long svc_operand;
    928 
    929       target_read_memory (pc, (unsigned char *) &this_instr, 4);
    930       svc_operand = (0x00ffffff & this_instr);
    931 
    932       if (svc_operand)  /* OABI.  */
    933 	{
    934 	  svc_number = svc_operand - 0x900000;
    935 	}
    936       else /* EABI.  */
    937 	{
    938 	  collect_register (regcache, 7, &svc_number);
    939 	}
    940 
    941       next_pc = pc + 4;
    942     }
    943 
    944   /* This is a sigreturn or sigreturn_rt syscall.  */
    945   if (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn)
    946     {
    947       /* SIGRETURN or RT_SIGRETURN may affect the arm thumb mode, so
    948 	 update IS_THUMB.   */
    949       next_pc = arm_sigreturn_next_pc (regcache, svc_number, &is_thumb);
    950     }
    951 
    952   /* Addresses for calling Thumb functions have the bit 0 set.  */
    953   if (is_thumb)
    954     next_pc = MAKE_THUMB_ADDR (next_pc);
    955 
    956   return next_pc;
    957 }
    958 
    959 static const struct target_desc *
    960 arm_read_description (void)
    961 {
    962   unsigned long arm_hwcap = linux_get_hwcap (current_thread->id.pid (), 4);
    963 
    964   if (arm_hwcap & HWCAP_IWMMXT)
    965     return arm_linux_read_description (ARM_FP_TYPE_IWMMXT);
    966 
    967   if (arm_hwcap & HWCAP_VFP)
    968     {
    969       /* Make sure that the kernel supports reading VFP registers.  Support was
    970 	 added in 2.6.30.  */
    971       int pid = lwpid_of (current_thread);
    972       errno = 0;
    973       char *buf = (char *) alloca (ARM_VFP3_REGS_SIZE);
    974       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0 && errno == EIO)
    975 	return arm_linux_read_description (ARM_FP_TYPE_NONE);
    976 
    977       /* NEON implies either no VFP, or VFPv3-D32.  We only support
    978 	 it with VFP.  */
    979       if (arm_hwcap & HWCAP_NEON)
    980 	return aarch32_linux_read_description ();
    981       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
    982 	return arm_linux_read_description (ARM_FP_TYPE_VFPV3);
    983       else
    984 	return arm_linux_read_description (ARM_FP_TYPE_VFPV2);
    985     }
    986 
    987   /* The default configuration uses legacy FPA registers, probably
    988      simulated.  */
    989   return arm_linux_read_description (ARM_FP_TYPE_NONE);
    990 }
    991 
    992 void
    993 arm_target::low_arch_setup ()
    994 {
    995   int tid = lwpid_of (current_thread);
    996   int gpregs[18];
    997   struct iovec iov;
    998 
    999   /* Query hardware watchpoint/breakpoint capabilities.  */
   1000   arm_linux_init_hwbp_cap (tid);
   1001 
   1002   current_process ()->tdesc = arm_read_description ();
   1003 
   1004   iov.iov_base = gpregs;
   1005   iov.iov_len = sizeof (gpregs);
   1006 
   1007   /* Check if PTRACE_GETREGSET works.  */
   1008   if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0)
   1009     have_ptrace_getregset = TRIBOOL_TRUE;
   1010   else
   1011     have_ptrace_getregset = TRIBOOL_FALSE;
   1012 }
   1013 
   1014 bool
   1015 arm_target::supports_software_single_step ()
   1016 {
   1017   return true;
   1018 }
   1019 
   1020 /* Fetch the next possible PCs after the current instruction executes.  */
   1021 
   1022 std::vector<CORE_ADDR>
   1023 arm_target::low_get_next_pcs (regcache *regcache)
   1024 {
   1025   struct arm_get_next_pcs next_pcs_ctx;
   1026 
   1027   arm_get_next_pcs_ctor (&next_pcs_ctx,
   1028 			 &get_next_pcs_ops,
   1029 			 /* Byte order is ignored assumed as host.  */
   1030 			 0,
   1031 			 0,
   1032 			 1,
   1033 			 regcache);
   1034 
   1035   return arm_get_next_pcs (&next_pcs_ctx);
   1036 }
   1037 
   1038 /* Support for hardware single step.  */
   1039 
   1040 bool
   1041 arm_target::supports_hardware_single_step ()
   1042 {
   1043   return false;
   1044 }
   1045 
   1046 bool
   1047 arm_target::low_supports_catch_syscall ()
   1048 {
   1049   return true;
   1050 }
   1051 
   1052 /* Implementation of linux target ops method "low_get_syscall_trapinfo".  */
   1053 
   1054 void
   1055 arm_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
   1056 {
   1057   if (arm_is_thumb_mode ())
   1058     collect_register_by_name (regcache, "r7", sysno);
   1059   else
   1060     {
   1061       unsigned long pc;
   1062       unsigned long insn;
   1063 
   1064       collect_register_by_name (regcache, "pc", &pc);
   1065 
   1066       if (read_memory (pc - 4, (unsigned char *) &insn, 4))
   1067 	*sysno = UNKNOWN_SYSCALL;
   1068       else
   1069 	{
   1070 	  unsigned long svc_operand = (0x00ffffff & insn);
   1071 
   1072 	  if (svc_operand)
   1073 	    {
   1074 	      /* OABI */
   1075 	      *sysno = svc_operand - 0x900000;
   1076 	    }
   1077 	  else
   1078 	    {
   1079 	      /* EABI */
   1080 	      collect_register_by_name (regcache, "r7", sysno);
   1081 	    }
   1082 	}
   1083     }
   1084 }
   1085 
   1086 /* Register sets without using PTRACE_GETREGSET.  */
   1087 
   1088 static struct regset_info arm_regsets[] = {
   1089   { PTRACE_GETREGS, PTRACE_SETREGS, 0,
   1090     ARM_CORE_REGS_SIZE + ARM_INT_REGISTER_SIZE, GENERAL_REGS,
   1091     arm_fill_gregset, arm_store_gregset },
   1092   { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, IWMMXT_REGS_SIZE, EXTENDED_REGS,
   1093     arm_fill_wmmxregset, arm_store_wmmxregset },
   1094   { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, ARM_VFP3_REGS_SIZE, EXTENDED_REGS,
   1095     arm_fill_vfpregset, arm_store_vfpregset },
   1096   NULL_REGSET
   1097 };
   1098 
   1099 static struct regsets_info arm_regsets_info =
   1100   {
   1101     arm_regsets, /* regsets */
   1102     0, /* num_regsets */
   1103     NULL, /* disabled_regsets */
   1104   };
   1105 
   1106 static struct usrregs_info arm_usrregs_info =
   1107   {
   1108     arm_num_regs,
   1109     arm_regmap,
   1110   };
   1111 
   1112 static struct regs_info regs_info_arm =
   1113   {
   1114     NULL, /* regset_bitmap */
   1115     &arm_usrregs_info,
   1116     &arm_regsets_info
   1117   };
   1118 
   1119 const regs_info *
   1120 arm_target::get_regs_info ()
   1121 {
   1122   const struct target_desc *tdesc = current_process ()->tdesc;
   1123 
   1124   if (have_ptrace_getregset == TRIBOOL_TRUE
   1125       && (is_aarch32_linux_description (tdesc)
   1126 	  || arm_linux_get_tdesc_fp_type (tdesc) == ARM_FP_TYPE_VFPV3))
   1127     return &regs_info_aarch32;
   1128 
   1129   return &regs_info_arm;
   1130 }
   1131 
   1132 /* The linux target ops object.  */
   1133 
   1134 linux_process_target *the_linux_target = &the_arm_target;
   1135 
   1136 void
   1137 initialize_low_arch (void)
   1138 {
   1139   initialize_low_arch_aarch32 ();
   1140   initialize_regsets_info (&arm_regsets_info);
   1141 }
   1142