Home | History | Annotate | Line # | Download | only in gdb
ppc-linux-nat.c revision 1.9.2.1
      1 /* PPC GNU/Linux native support.
      2 
      3    Copyright (C) 1988-2023 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include "frame.h"
     22 #include "inferior.h"
     23 #include "gdbthread.h"
     24 #include "gdbcore.h"
     25 #include "regcache.h"
     26 #include "regset.h"
     27 #include "target.h"
     28 #include "linux-nat.h"
     29 #include <sys/types.h>
     30 #include <signal.h>
     31 #include <sys/user.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/uio.h>
     34 #include "gdbsupport/gdb_wait.h"
     35 #include <fcntl.h>
     36 #include <sys/procfs.h>
     37 #include "nat/gdb_ptrace.h"
     38 #include "nat/linux-ptrace.h"
     39 #include "inf-ptrace.h"
     40 #include <algorithm>
     41 #include <unordered_map>
     42 #include <list>
     43 
     44 /* Prototypes for supply_gregset etc.  */
     45 #include "gregset.h"
     46 #include "ppc-tdep.h"
     47 #include "ppc-linux-tdep.h"
     48 
     49 /* Required when using the AUXV.  */
     50 #include "elf/common.h"
     51 #include "auxv.h"
     52 
     53 #include "arch/ppc-linux-common.h"
     54 #include "arch/ppc-linux-tdesc.h"
     55 #include "nat/ppc-linux.h"
     56 #include "linux-tdep.h"
     57 #include "expop.h"
     58 
     59 /* Similarly for the hardware watchpoint support.  These requests are used
     60    when the PowerPC HWDEBUG ptrace interface is not available.  */
     61 #ifndef PTRACE_GET_DEBUGREG
     62 #define PTRACE_GET_DEBUGREG    25
     63 #endif
     64 #ifndef PTRACE_SET_DEBUGREG
     65 #define PTRACE_SET_DEBUGREG    26
     66 #endif
     67 #ifndef PTRACE_GETSIGINFO
     68 #define PTRACE_GETSIGINFO    0x4202
     69 #endif
     70 
     71 /* These requests are used when the PowerPC HWDEBUG ptrace interface is
     72    available.  It exposes the debug facilities of PowerPC processors, as well
     73    as additional features of BookE processors, such as ranged breakpoints and
     74    watchpoints and hardware-accelerated condition evaluation.  */
     75 #ifndef PPC_PTRACE_GETHWDBGINFO
     76 
     77 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG
     78    ptrace interface is not present in ptrace.h, so we'll have to pretty much
     79    include it all here so that the code at least compiles on older systems.  */
     80 #define PPC_PTRACE_GETHWDBGINFO 0x89
     81 #define PPC_PTRACE_SETHWDEBUG   0x88
     82 #define PPC_PTRACE_DELHWDEBUG   0x87
     83 
     84 struct ppc_debug_info
     85 {
     86 	uint32_t version;               /* Only version 1 exists to date.  */
     87 	uint32_t num_instruction_bps;
     88 	uint32_t num_data_bps;
     89 	uint32_t num_condition_regs;
     90 	uint32_t data_bp_alignment;
     91 	uint32_t sizeof_condition;      /* size of the DVC register.  */
     92 	uint64_t features;
     93 };
     94 
     95 /* Features will have bits indicating whether there is support for:  */
     96 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE         0x1
     97 #define PPC_DEBUG_FEATURE_INSN_BP_MASK          0x2
     98 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE         0x4
     99 #define PPC_DEBUG_FEATURE_DATA_BP_MASK          0x8
    100 
    101 struct ppc_hw_breakpoint
    102 {
    103 	uint32_t version;               /* currently, version must be 1 */
    104 	uint32_t trigger_type;          /* only some combinations allowed */
    105 	uint32_t addr_mode;             /* address match mode */
    106 	uint32_t condition_mode;        /* break/watchpoint condition flags */
    107 	uint64_t addr;                  /* break/watchpoint address */
    108 	uint64_t addr2;                 /* range end or mask */
    109 	uint64_t condition_value;       /* contents of the DVC register */
    110 };
    111 
    112 /* Trigger type.  */
    113 #define PPC_BREAKPOINT_TRIGGER_EXECUTE  0x1
    114 #define PPC_BREAKPOINT_TRIGGER_READ     0x2
    115 #define PPC_BREAKPOINT_TRIGGER_WRITE    0x4
    116 #define PPC_BREAKPOINT_TRIGGER_RW       0x6
    117 
    118 /* Address mode.  */
    119 #define PPC_BREAKPOINT_MODE_EXACT               0x0
    120 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE     0x1
    121 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE     0x2
    122 #define PPC_BREAKPOINT_MODE_MASK                0x3
    123 
    124 /* Condition mode.  */
    125 #define PPC_BREAKPOINT_CONDITION_NONE   0x0
    126 #define PPC_BREAKPOINT_CONDITION_AND    0x1
    127 #define PPC_BREAKPOINT_CONDITION_EXACT  0x1
    128 #define PPC_BREAKPOINT_CONDITION_OR     0x2
    129 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
    130 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
    131 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT       16
    132 #define PPC_BREAKPOINT_CONDITION_BE(n)  \
    133 	(1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
    134 #endif /* PPC_PTRACE_GETHWDBGINFO */
    135 
    136 /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
    137    watchpoint (up to 512 bytes).  */
    138 #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
    139 #define PPC_DEBUG_FEATURE_DATA_BP_DAWR	0x10
    140 #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
    141 
    142 /* Feature defined on Linux kernel v5.1: Second watchpoint support.  */
    143 #ifndef PPC_DEBUG_FEATURE_DATA_BP_ARCH_31
    144 #define PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 0x20
    145 #endif /* PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 */
    146 
    147 /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
    148    available.  */
    149 #define PPC_DEBUG_CURRENT_VERSION 1
    150 
    151 /* Similarly for the general-purpose (gp0 -- gp31)
    152    and floating-point registers (fp0 -- fp31).  */
    153 #ifndef PTRACE_GETREGS
    154 #define PTRACE_GETREGS 12
    155 #endif
    156 #ifndef PTRACE_SETREGS
    157 #define PTRACE_SETREGS 13
    158 #endif
    159 #ifndef PTRACE_GETFPREGS
    160 #define PTRACE_GETFPREGS 14
    161 #endif
    162 #ifndef PTRACE_SETFPREGS
    163 #define PTRACE_SETFPREGS 15
    164 #endif
    165 
    166 /* This oddity is because the Linux kernel defines elf_vrregset_t as
    167    an array of 33 16 bytes long elements.  I.e. it leaves out vrsave.
    168    However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
    169    the vrsave as an extra 4 bytes at the end.  I opted for creating a
    170    flat array of chars, so that it is easier to manipulate for gdb.
    171 
    172    There are 32 vector registers 16 bytes longs, plus a VSCR register
    173    which is only 4 bytes long, but is fetched as a 16 bytes
    174    quantity.  Up to here we have the elf_vrregset_t structure.
    175    Appended to this there is space for the VRSAVE register: 4 bytes.
    176    Even though this vrsave register is not included in the regset
    177    typedef, it is handled by the ptrace requests.
    178 
    179    The layout is like this (where x is the actual value of the vscr reg): */
    180 
    181 /* *INDENT-OFF* */
    182 /*
    183 Big-Endian:
    184    |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
    185    <------->     <-------><-------><->
    186      VR0           VR31     VSCR    VRSAVE
    187 Little-Endian:
    188    |.|.|.|.|.....|.|.|.|.||X|.|.|.||.|
    189    <------->     <-------><-------><->
    190      VR0           VR31     VSCR    VRSAVE
    191 */
    192 /* *INDENT-ON* */
    193 
    194 typedef char gdb_vrregset_t[PPC_LINUX_SIZEOF_VRREGSET];
    195 
    196 /* This is the layout of the POWER7 VSX registers and the way they overlap
    197    with the existing FPR and VMX registers.
    198 
    199 		    VSR doubleword 0               VSR doubleword 1
    200 	   ----------------------------------------------------------------
    201    VSR[0]  |             FPR[0]            |                              |
    202 	   ----------------------------------------------------------------
    203    VSR[1]  |             FPR[1]            |                              |
    204 	   ----------------------------------------------------------------
    205 	   |              ...              |                              |
    206 	   |              ...              |                              |
    207 	   ----------------------------------------------------------------
    208    VSR[30] |             FPR[30]           |                              |
    209 	   ----------------------------------------------------------------
    210    VSR[31] |             FPR[31]           |                              |
    211 	   ----------------------------------------------------------------
    212    VSR[32] |                             VR[0]                            |
    213 	   ----------------------------------------------------------------
    214    VSR[33] |                             VR[1]                            |
    215 	   ----------------------------------------------------------------
    216 	   |                              ...                             |
    217 	   |                              ...                             |
    218 	   ----------------------------------------------------------------
    219    VSR[62] |                             VR[30]                           |
    220 	   ----------------------------------------------------------------
    221    VSR[63] |                             VR[31]                           |
    222 	  ----------------------------------------------------------------
    223 
    224    VSX has 64 128bit registers.  The first 32 registers overlap with
    225    the FP registers (doubleword 0) and hence extend them with additional
    226    64 bits (doubleword 1).  The other 32 regs overlap with the VMX
    227    registers.  */
    228 typedef char gdb_vsxregset_t[PPC_LINUX_SIZEOF_VSXREGSET];
    229 
    230 /* On PPC processors that support the Signal Processing Extension
    231    (SPE) APU, the general-purpose registers are 64 bits long.
    232    However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
    233    ptrace calls only access the lower half of each register, to allow
    234    them to behave the same way they do on non-SPE systems.  There's a
    235    separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
    236    read and write the top halves of all the general-purpose registers
    237    at once, along with some SPE-specific registers.
    238 
    239    GDB itself continues to claim the general-purpose registers are 32
    240    bits long.  It has unnamed raw registers that hold the upper halves
    241    of the gprs, and the full 64-bit SIMD views of the registers,
    242    'ev0' -- 'ev31', are pseudo-registers that splice the top and
    243    bottom halves together.
    244 
    245    This is the structure filled in by PTRACE_GETEVRREGS and written to
    246    the inferior's registers by PTRACE_SETEVRREGS.  */
    247 struct gdb_evrregset_t
    248 {
    249   unsigned long evr[32];
    250   unsigned long long acc;
    251   unsigned long spefscr;
    252 };
    253 
    254 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
    255    PTRACE_SETVSXREGS requests, for reading and writing the VSX
    256    POWER7 registers 0 through 31.  Zero if we've tried one of them and
    257    gotten an error.  Note that VSX registers 32 through 63 overlap
    258    with VR registers 0 through 31.  */
    259 int have_ptrace_getsetvsxregs = 1;
    260 
    261 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
    262    PTRACE_SETVRREGS requests, for reading and writing the Altivec
    263    registers.  Zero if we've tried one of them and gotten an
    264    error.  */
    265 int have_ptrace_getvrregs = 1;
    266 
    267 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
    268    PTRACE_SETEVRREGS requests, for reading and writing the SPE
    269    registers.  Zero if we've tried one of them and gotten an
    270    error.  */
    271 int have_ptrace_getsetevrregs = 1;
    272 
    273 /* Non-zero if our kernel may support the PTRACE_GETREGS and
    274    PTRACE_SETREGS requests, for reading and writing the
    275    general-purpose registers.  Zero if we've tried one of
    276    them and gotten an error.  */
    277 int have_ptrace_getsetregs = 1;
    278 
    279 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
    280    PTRACE_SETFPREGS requests, for reading and writing the
    281    floating-pointers registers.  Zero if we've tried one of
    282    them and gotten an error.  */
    283 int have_ptrace_getsetfpregs = 1;
    284 
    285 /* Private arch info associated with each thread lwp_info object, used
    286    for debug register handling.  */
    287 
    288 struct arch_lwp_info
    289 {
    290   /* When true, indicates that the debug registers installed in the
    291      thread no longer correspond to the watchpoints and breakpoints
    292      requested by GDB.  */
    293   bool debug_regs_stale;
    294 
    295   /* We need a back-reference to the PTID of the thread so that we can
    296      cleanup the debug register state of the thread in
    297      low_delete_thread.  */
    298   ptid_t lwp_ptid;
    299 };
    300 
    301 /* Class used to detect which set of ptrace requests that
    302    ppc_linux_nat_target will use to install and remove hardware
    303    breakpoints and watchpoints.
    304 
    305    The interface is only detected once, testing the ptrace calls.  The
    306    result can indicate that no interface is available.
    307 
    308    The Linux kernel provides two different sets of ptrace requests to
    309    handle hardware watchpoints and breakpoints for Power:
    310 
    311    - PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
    312      PPC_PTRACE_DELHWDEBUG.
    313 
    314    Or
    315 
    316    - PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
    317 
    318    The first set is the more flexible one and allows setting watchpoints
    319    with a variable watched region length and, for BookE processors,
    320    multiple types of debug registers (e.g. hardware breakpoints and
    321    hardware-assisted conditions for watchpoints).  The second one only
    322    allows setting one debug register, a watchpoint, so we only use it if
    323    the first one is not available.  */
    324 
    325 class ppc_linux_dreg_interface
    326 {
    327 public:
    328 
    329   ppc_linux_dreg_interface ()
    330     : m_interface (), m_hwdebug_info ()
    331   {
    332   };
    333 
    334   DISABLE_COPY_AND_ASSIGN (ppc_linux_dreg_interface);
    335 
    336   /* One and only one of these three functions returns true, indicating
    337      whether the corresponding interface is the one we detected.  The
    338      interface must already have been detected as a precontidion.  */
    339 
    340   bool hwdebug_p ()
    341   {
    342     gdb_assert (detected_p ());
    343     return *m_interface == HWDEBUG;
    344   }
    345 
    346   bool debugreg_p ()
    347   {
    348     gdb_assert (detected_p ());
    349     return *m_interface == DEBUGREG;
    350   }
    351 
    352   bool unavailable_p ()
    353   {
    354     gdb_assert (detected_p ());
    355     return *m_interface == UNAVAILABLE;
    356   }
    357 
    358   /* Returns the debug register capabilities of the target.  Should only
    359      be called if the interface is HWDEBUG.  */
    360   const struct ppc_debug_info &hwdebug_info ()
    361   {
    362     gdb_assert (hwdebug_p ());
    363 
    364     return m_hwdebug_info;
    365   }
    366 
    367   /* Returns true if the interface has already been detected.  This is
    368      useful for cases when we know there is no work to be done if the
    369      interface hasn't been detected yet.  */
    370   bool detected_p ()
    371   {
    372     return m_interface.has_value ();
    373   }
    374 
    375   /* Detect the available interface, if any, if it hasn't been detected
    376      before, using PTID for the necessary ptrace calls.  */
    377 
    378   void detect (const ptid_t &ptid)
    379   {
    380     if (m_interface.has_value ())
    381       return;
    382 
    383     gdb_assert (ptid.lwp_p ());
    384 
    385     bool no_features = false;
    386 
    387     if (ptrace (PPC_PTRACE_GETHWDBGINFO, ptid.lwp (), 0, &m_hwdebug_info)
    388 	>= 0)
    389       {
    390 	/* If there are no advertised features, we don't use the
    391 	   HWDEBUG interface and try the DEBUGREG interface instead.
    392 	   It shouldn't be necessary to do this, however, when the
    393 	   kernel is configured without CONFIG_HW_BREAKPOINTS (selected
    394 	   by CONFIG_PERF_EVENTS), there is a bug that causes
    395 	   watchpoints installed with the HWDEBUG interface not to
    396 	   trigger.  When this is the case, features will be zero,
    397 	   which we use as an indicator to fall back to the DEBUGREG
    398 	   interface.  */
    399 	if (m_hwdebug_info.features != 0)
    400 	  {
    401 	    m_interface.emplace (HWDEBUG);
    402 	    return;
    403 	  }
    404 	else
    405 	  no_features = true;
    406       }
    407 
    408     /* EIO indicates that the request is invalid, so we try DEBUGREG
    409        next.  Technically, it can also indicate other failures, but we
    410        can't differentiate those.
    411 
    412        Other errors could happen for various reasons.  We could get an
    413        ESRCH if the traced thread was killed by a signal.  Trying to
    414        detect the interface with another thread in the future would be
    415        complicated, as callers would have to handle an "unknown
    416        interface" case.  It's also unclear if raising an exception
    417        here would be safe.
    418 
    419        Other errors, such as ENODEV, could be more permanent and cause
    420        a failure for any thread.
    421 
    422        For simplicity, with all errors other than EIO, we set the
    423        interface to UNAVAILABLE and don't try DEBUGREG.  If DEBUGREG
    424        fails too, we'll also set the interface to UNAVAILABLE.  It's
    425        unlikely that trying the DEBUGREG interface with this same thread
    426        would work, for errors other than EIO.  This means that these
    427        errors will cause hardware watchpoints and breakpoints to become
    428        unavailable throughout a GDB session.  */
    429 
    430     if (no_features || errno == EIO)
    431       {
    432 	unsigned long wp;
    433 
    434 	if (ptrace (PTRACE_GET_DEBUGREG, ptid.lwp (), 0, &wp) >= 0)
    435 	  {
    436 	    m_interface.emplace (DEBUGREG);
    437 	    return;
    438 	  }
    439       }
    440 
    441     if (errno != EIO)
    442       warning (_("Error when detecting the debug register interface. "
    443 		 "Debug registers will be unavailable."));
    444 
    445     m_interface.emplace (UNAVAILABLE);
    446     return;
    447   }
    448 
    449 private:
    450 
    451   /* HWDEBUG represents the set of calls PPC_PTRACE_GETHWDBGINFO,
    452      PPC_PTRACE_SETHWDEBUG and PPC_PTRACE_DELHWDEBUG.
    453 
    454      DEBUGREG represents the set of calls PTRACE_SET_DEBUGREG and
    455      PTRACE_GET_DEBUGREG.
    456 
    457      UNAVAILABLE can indicate that the kernel doesn't support any of the
    458      two sets of requests or that there was an error when we tried to
    459      detect wich interface is available.  */
    460 
    461   enum debug_reg_interface
    462     {
    463      UNAVAILABLE,
    464      HWDEBUG,
    465      DEBUGREG
    466     };
    467 
    468   /* The interface option.  Initialized if has_value () returns true.  */
    469   gdb::optional<enum debug_reg_interface> m_interface;
    470 
    471   /* The info returned by the kernel with PPC_PTRACE_GETHWDBGINFO.  Only
    472      valid if we determined that the interface is HWDEBUG.  */
    473   struct ppc_debug_info m_hwdebug_info;
    474 };
    475 
    476 /* Per-process information.  This includes the hardware watchpoints and
    477    breakpoints that GDB requested to this target.  */
    478 
    479 struct ppc_linux_process_info
    480 {
    481   /* The list of hardware watchpoints and breakpoints that GDB requested
    482      for this process.
    483 
    484      Only used when the interface is HWDEBUG.  */
    485   std::list<struct ppc_hw_breakpoint> requested_hw_bps;
    486 
    487   /* The watchpoint value that GDB requested for this process.
    488 
    489      Only used when the interface is DEBUGREG.  */
    490   gdb::optional<long> requested_wp_val;
    491 };
    492 
    493 struct ppc_linux_nat_target final : public linux_nat_target
    494 {
    495   /* Add our register access methods.  */
    496   void fetch_registers (struct regcache *, int) override;
    497   void store_registers (struct regcache *, int) override;
    498 
    499   /* Add our breakpoint/watchpoint methods.  */
    500   int can_use_hw_breakpoint (enum bptype, int, int) override;
    501 
    502   int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
    503     override;
    504 
    505   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
    506     override;
    507 
    508   int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
    509 
    510   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
    511 			 struct expression *) override;
    512 
    513   int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
    514 			 struct expression *) override;
    515 
    516   int insert_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
    517     override;
    518 
    519   int remove_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
    520     override;
    521 
    522   bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
    523 
    524   bool can_accel_watchpoint_condition (CORE_ADDR, int, int, struct expression *)
    525     override;
    526 
    527   int masked_watch_num_registers (CORE_ADDR, CORE_ADDR) override;
    528 
    529   int ranged_break_num_registers () override;
    530 
    531   const struct target_desc *read_description ()  override;
    532 
    533   int auxv_parse (const gdb_byte **readptr,
    534 		  const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
    535     override;
    536 
    537   /* Override linux_nat_target low methods.  */
    538   bool low_stopped_by_watchpoint () override;
    539 
    540   bool low_stopped_data_address (CORE_ADDR *) override;
    541 
    542   void low_new_thread (struct lwp_info *lp) override;
    543 
    544   void low_delete_thread (arch_lwp_info *) override;
    545 
    546   void low_new_fork (struct lwp_info *, pid_t) override;
    547 
    548   void low_new_clone (struct lwp_info *, pid_t) override;
    549 
    550   void low_forget_process (pid_t pid) override;
    551 
    552   void low_prepare_to_resume (struct lwp_info *) override;
    553 
    554 private:
    555 
    556   void copy_thread_dreg_state (const ptid_t &parent_ptid,
    557 			       const ptid_t &child_ptid);
    558 
    559   void mark_thread_stale (struct lwp_info *lp);
    560 
    561   void mark_debug_registers_changed (pid_t pid);
    562 
    563   void register_hw_breakpoint (pid_t pid,
    564 			       const struct ppc_hw_breakpoint &bp);
    565 
    566   void clear_hw_breakpoint (pid_t pid,
    567 			    const struct ppc_hw_breakpoint &a);
    568 
    569   void register_wp (pid_t pid, long wp_value);
    570 
    571   void clear_wp (pid_t pid);
    572 
    573   bool can_use_watchpoint_cond_accel (void);
    574 
    575   void calculate_dvc (CORE_ADDR addr, int len,
    576 		      CORE_ADDR data_value,
    577 		      uint32_t *condition_mode,
    578 		      uint64_t *condition_value);
    579 
    580   int check_condition (CORE_ADDR watch_addr,
    581 		       struct expression *cond,
    582 		       CORE_ADDR *data_value, int *len);
    583 
    584   int num_memory_accesses (const std::vector<value_ref_ptr> &chain);
    585 
    586   int get_trigger_type (enum target_hw_bp_type type);
    587 
    588   void create_watchpoint_request (struct ppc_hw_breakpoint *p,
    589 				  CORE_ADDR addr,
    590 				  int len,
    591 				  enum target_hw_bp_type type,
    592 				  struct expression *cond,
    593 				  int insert);
    594 
    595   bool hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
    596 			  const struct ppc_hw_breakpoint &b);
    597 
    598   void init_arch_lwp_info (struct lwp_info *lp);
    599 
    600   arch_lwp_info *get_arch_lwp_info (struct lwp_info *lp);
    601 
    602   /* The ptrace interface we'll use to install hardware watchpoints and
    603      breakpoints (debug registers).  */
    604   ppc_linux_dreg_interface m_dreg_interface;
    605 
    606   /* A map from pids to structs containing info specific to each
    607      process.  */
    608   std::unordered_map<pid_t, ppc_linux_process_info> m_process_info;
    609 
    610   /* Callable object to hash ptids by their lwp number.  */
    611   struct ptid_hash
    612   {
    613     std::size_t operator() (const ptid_t &ptid) const
    614     {
    615       return std::hash<long>{} (ptid.lwp ());
    616     }
    617   };
    618 
    619   /* A map from ptid_t objects to a list of pairs of slots and hardware
    620      breakpoint objects.  This keeps track of which hardware breakpoints
    621      and watchpoints were last installed in each slot of each thread.
    622 
    623      Only used when the interface is HWDEBUG.  */
    624   std::unordered_map <ptid_t,
    625 		      std::list<std::pair<long, ppc_hw_breakpoint>>,
    626 		      ptid_hash> m_installed_hw_bps;
    627 };
    628 
    629 static ppc_linux_nat_target the_ppc_linux_nat_target;
    630 
    631 /* *INDENT-OFF* */
    632 /* registers layout, as presented by the ptrace interface:
    633 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
    634 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
    635 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
    636 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
    637 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
    638 PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
    639 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
    640 PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
    641 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
    642 PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
    643 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
    644 PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
    645 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
    646 /* *INDENT_ON * */
    647 
    648 static int
    649 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
    650 {
    651   int u_addr = -1;
    652   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
    653   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
    654      interface, and not the wordsize of the program's ABI.  */
    655   int wordsize = sizeof (long);
    656 
    657   /* General purpose registers occupy 1 slot each in the buffer.  */
    658   if (regno >= tdep->ppc_gp0_regnum
    659       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
    660     u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
    661 
    662   /* Floating point regs: eight bytes each in both 32- and 64-bit
    663      ptrace interfaces.  Thus, two slots each in 32-bit interface, one
    664      slot each in 64-bit interface.  */
    665   if (tdep->ppc_fp0_regnum >= 0
    666       && regno >= tdep->ppc_fp0_regnum
    667       && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
    668     u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
    669 
    670   /* UISA special purpose registers: 1 slot each.  */
    671   if (regno == gdbarch_pc_regnum (gdbarch))
    672     u_addr = PT_NIP * wordsize;
    673   if (regno == tdep->ppc_lr_regnum)
    674     u_addr = PT_LNK * wordsize;
    675   if (regno == tdep->ppc_cr_regnum)
    676     u_addr = PT_CCR * wordsize;
    677   if (regno == tdep->ppc_xer_regnum)
    678     u_addr = PT_XER * wordsize;
    679   if (regno == tdep->ppc_ctr_regnum)
    680     u_addr = PT_CTR * wordsize;
    681 #ifdef PT_MQ
    682   if (regno == tdep->ppc_mq_regnum)
    683     u_addr = PT_MQ * wordsize;
    684 #endif
    685   if (regno == tdep->ppc_ps_regnum)
    686     u_addr = PT_MSR * wordsize;
    687   if (regno == PPC_ORIG_R3_REGNUM)
    688     u_addr = PT_ORIG_R3 * wordsize;
    689   if (regno == PPC_TRAP_REGNUM)
    690     u_addr = PT_TRAP * wordsize;
    691   if (tdep->ppc_fpscr_regnum >= 0
    692       && regno == tdep->ppc_fpscr_regnum)
    693     {
    694       /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
    695 	 kernel headers incorrectly contained the 32-bit definition of
    696 	 PT_FPSCR.  For the 32-bit definition, floating-point
    697 	 registers occupy two 32-bit "slots", and the FPSCR lives in
    698 	 the second half of such a slot-pair (hence +1).  For 64-bit,
    699 	 the FPSCR instead occupies the full 64-bit 2-word-slot and
    700 	 hence no adjustment is necessary.  Hack around this.  */
    701       if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
    702 	u_addr = (48 + 32) * wordsize;
    703       /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
    704 	 slot and not just its second word.  The PT_FPSCR supplied when
    705 	 GDB is compiled as a 32-bit app doesn't reflect this.  */
    706       else if (wordsize == 4 && register_size (gdbarch, regno) == 8
    707 	       && PT_FPSCR == (48 + 2*32 + 1))
    708 	u_addr = (48 + 2*32) * wordsize;
    709       else
    710 	u_addr = PT_FPSCR * wordsize;
    711     }
    712   return u_addr;
    713 }
    714 
    715 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
    716    registers set mechanism, as opposed to the interface for all the
    717    other registers, that stores/fetches each register individually.  */
    718 static void
    719 fetch_vsx_registers (struct regcache *regcache, int tid, int regno)
    720 {
    721   int ret;
    722   gdb_vsxregset_t regs;
    723   const struct regset *vsxregset = ppc_linux_vsxregset ();
    724 
    725   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
    726   if (ret < 0)
    727     {
    728       if (errno == EIO)
    729 	{
    730 	  have_ptrace_getsetvsxregs = 0;
    731 	  return;
    732 	}
    733       perror_with_name (_("Unable to fetch VSX registers"));
    734     }
    735 
    736   vsxregset->supply_regset (vsxregset, regcache, regno, &regs,
    737 			    PPC_LINUX_SIZEOF_VSXREGSET);
    738 }
    739 
    740 /* The Linux kernel ptrace interface for AltiVec registers uses the
    741    registers set mechanism, as opposed to the interface for all the
    742    other registers, that stores/fetches each register individually.  */
    743 static void
    744 fetch_altivec_registers (struct regcache *regcache, int tid,
    745 			 int regno)
    746 {
    747   int ret;
    748   gdb_vrregset_t regs;
    749   struct gdbarch *gdbarch = regcache->arch ();
    750   const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
    751 
    752   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
    753   if (ret < 0)
    754     {
    755       if (errno == EIO)
    756 	{
    757 	  have_ptrace_getvrregs = 0;
    758 	  return;
    759 	}
    760       perror_with_name (_("Unable to fetch AltiVec registers"));
    761     }
    762 
    763   vrregset->supply_regset (vrregset, regcache, regno, &regs,
    764 			   PPC_LINUX_SIZEOF_VRREGSET);
    765 }
    766 
    767 /* Fetch the top 32 bits of TID's general-purpose registers and the
    768    SPE-specific registers, and place the results in EVRREGSET.  If we
    769    don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
    770    zeros.
    771 
    772    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
    773    PTRACE_SETEVRREGS requests are supported is isolated here, and in
    774    set_spe_registers.  */
    775 static void
    776 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
    777 {
    778   if (have_ptrace_getsetevrregs)
    779     {
    780       if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
    781 	return;
    782       else
    783 	{
    784 	  /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
    785 	     we just return zeros.  */
    786 	  if (errno == EIO)
    787 	    have_ptrace_getsetevrregs = 0;
    788 	  else
    789 	    /* Anything else needs to be reported.  */
    790 	    perror_with_name (_("Unable to fetch SPE registers"));
    791 	}
    792     }
    793 
    794   memset (evrregset, 0, sizeof (*evrregset));
    795 }
    796 
    797 /* Supply values from TID for SPE-specific raw registers: the upper
    798    halves of the GPRs, the accumulator, and the spefscr.  REGNO must
    799    be the number of an upper half register, acc, spefscr, or -1 to
    800    supply the values of all registers.  */
    801 static void
    802 fetch_spe_register (struct regcache *regcache, int tid, int regno)
    803 {
    804   struct gdbarch *gdbarch = regcache->arch ();
    805   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
    806   struct gdb_evrregset_t evrregs;
    807 
    808   gdb_assert (sizeof (evrregs.evr[0])
    809 	      == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
    810   gdb_assert (sizeof (evrregs.acc)
    811 	      == register_size (gdbarch, tdep->ppc_acc_regnum));
    812   gdb_assert (sizeof (evrregs.spefscr)
    813 	      == register_size (gdbarch, tdep->ppc_spefscr_regnum));
    814 
    815   get_spe_registers (tid, &evrregs);
    816 
    817   if (regno == -1)
    818     {
    819       int i;
    820 
    821       for (i = 0; i < ppc_num_gprs; i++)
    822 	regcache->raw_supply (tdep->ppc_ev0_upper_regnum + i, &evrregs.evr[i]);
    823     }
    824   else if (tdep->ppc_ev0_upper_regnum <= regno
    825 	   && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
    826     regcache->raw_supply (regno,
    827 			  &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
    828 
    829   if (regno == -1
    830       || regno == tdep->ppc_acc_regnum)
    831     regcache->raw_supply (tdep->ppc_acc_regnum, &evrregs.acc);
    832 
    833   if (regno == -1
    834       || regno == tdep->ppc_spefscr_regnum)
    835     regcache->raw_supply (tdep->ppc_spefscr_regnum, &evrregs.spefscr);
    836 }
    837 
    838 /* Use ptrace to fetch all registers from the register set with note
    839    type REGSET_ID, size REGSIZE, and layout described by REGSET, from
    840    process/thread TID and supply their values to REGCACHE.  If ptrace
    841    returns ENODATA to indicate the regset is unavailable, mark the
    842    registers as unavailable in REGCACHE.  */
    843 
    844 static void
    845 fetch_regset (struct regcache *regcache, int tid,
    846 	      int regset_id, int regsetsize, const struct regset *regset)
    847 {
    848   void *buf = alloca (regsetsize);
    849   struct iovec iov;
    850 
    851   iov.iov_base = buf;
    852   iov.iov_len = regsetsize;
    853 
    854   if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
    855     {
    856       if (errno == ENODATA)
    857 	regset->supply_regset (regset, regcache, -1, NULL, regsetsize);
    858       else
    859 	perror_with_name (_("Couldn't get register set"));
    860     }
    861   else
    862     regset->supply_regset (regset, regcache, -1, buf, regsetsize);
    863 }
    864 
    865 /* Use ptrace to store register REGNUM of the regset with note type
    866    REGSET_ID, size REGSETSIZE, and layout described by REGSET, from
    867    REGCACHE back to process/thread TID.  If REGNUM is -1 all registers
    868    in the set are collected and stored.  */
    869 
    870 static void
    871 store_regset (const struct regcache *regcache, int tid, int regnum,
    872 	      int regset_id, int regsetsize, const struct regset *regset)
    873 {
    874   void *buf = alloca (regsetsize);
    875   struct iovec iov;
    876 
    877   iov.iov_base = buf;
    878   iov.iov_len = regsetsize;
    879 
    880   /* Make sure that the buffer that will be stored has up to date values
    881      for the registers that won't be collected.  */
    882   if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
    883     perror_with_name (_("Couldn't get register set"));
    884 
    885   regset->collect_regset (regset, regcache, regnum, buf, regsetsize);
    886 
    887   if (ptrace (PTRACE_SETREGSET, tid, regset_id, &iov) < 0)
    888     perror_with_name (_("Couldn't set register set"));
    889 }
    890 
    891 /* Check whether the kernel provides a register set with number
    892    REGSET_ID of size REGSETSIZE for process/thread TID.  */
    893 
    894 static bool
    895 check_regset (int tid, int regset_id, int regsetsize)
    896 {
    897   void *buf = alloca (regsetsize);
    898   struct iovec iov;
    899 
    900   iov.iov_base = buf;
    901   iov.iov_len = regsetsize;
    902 
    903   if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0
    904       || errno == ENODATA)
    905     return true;
    906   else
    907     return false;
    908 }
    909 
    910 static void
    911 fetch_register (struct regcache *regcache, int tid, int regno)
    912 {
    913   struct gdbarch *gdbarch = regcache->arch ();
    914   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
    915   /* This isn't really an address.  But ptrace thinks of it as one.  */
    916   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
    917   int bytes_transferred;
    918   gdb_byte buf[PPC_MAX_REGISTER_SIZE];
    919 
    920   if (altivec_register_p (gdbarch, regno))
    921     {
    922       /* If this is the first time through, or if it is not the first
    923 	 time through, and we have confirmed that there is kernel
    924 	 support for such a ptrace request, then go and fetch the
    925 	 register.  */
    926       if (have_ptrace_getvrregs)
    927        {
    928 	 fetch_altivec_registers (regcache, tid, regno);
    929 	 return;
    930        }
    931      /* If we have discovered that there is no ptrace support for
    932 	AltiVec registers, fall through and return zeroes, because
    933 	regaddr will be -1 in this case.  */
    934     }
    935   else if (vsx_register_p (gdbarch, regno))
    936     {
    937       if (have_ptrace_getsetvsxregs)
    938 	{
    939 	  fetch_vsx_registers (regcache, tid, regno);
    940 	  return;
    941 	}
    942     }
    943   else if (spe_register_p (gdbarch, regno))
    944     {
    945       fetch_spe_register (regcache, tid, regno);
    946       return;
    947     }
    948   else if (regno == PPC_DSCR_REGNUM)
    949     {
    950       gdb_assert (tdep->ppc_dscr_regnum != -1);
    951 
    952       fetch_regset (regcache, tid, NT_PPC_DSCR,
    953 		    PPC_LINUX_SIZEOF_DSCRREGSET,
    954 		    &ppc32_linux_dscrregset);
    955       return;
    956     }
    957   else if (regno == PPC_PPR_REGNUM)
    958     {
    959       gdb_assert (tdep->ppc_ppr_regnum != -1);
    960 
    961       fetch_regset (regcache, tid, NT_PPC_PPR,
    962 		    PPC_LINUX_SIZEOF_PPRREGSET,
    963 		    &ppc32_linux_pprregset);
    964       return;
    965     }
    966   else if (regno == PPC_TAR_REGNUM)
    967     {
    968       gdb_assert (tdep->ppc_tar_regnum != -1);
    969 
    970       fetch_regset (regcache, tid, NT_PPC_TAR,
    971 		    PPC_LINUX_SIZEOF_TARREGSET,
    972 		    &ppc32_linux_tarregset);
    973       return;
    974     }
    975   else if (PPC_IS_EBB_REGNUM (regno))
    976     {
    977       gdb_assert (tdep->have_ebb);
    978 
    979       fetch_regset (regcache, tid, NT_PPC_EBB,
    980 		    PPC_LINUX_SIZEOF_EBBREGSET,
    981 		    &ppc32_linux_ebbregset);
    982       return;
    983     }
    984   else if (PPC_IS_PMU_REGNUM (regno))
    985     {
    986       gdb_assert (tdep->ppc_mmcr0_regnum != -1);
    987 
    988       fetch_regset (regcache, tid, NT_PPC_PMU,
    989 		    PPC_LINUX_SIZEOF_PMUREGSET,
    990 		    &ppc32_linux_pmuregset);
    991       return;
    992     }
    993   else if (PPC_IS_TMSPR_REGNUM (regno))
    994     {
    995       gdb_assert (tdep->have_htm_spr);
    996 
    997       fetch_regset (regcache, tid, NT_PPC_TM_SPR,
    998 		    PPC_LINUX_SIZEOF_TM_SPRREGSET,
    999 		    &ppc32_linux_tm_sprregset);
   1000       return;
   1001     }
   1002   else if (PPC_IS_CKPTGP_REGNUM (regno))
   1003     {
   1004       gdb_assert (tdep->have_htm_core);
   1005 
   1006       const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
   1007       fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
   1008 		    (tdep->wordsize == 4?
   1009 		     PPC32_LINUX_SIZEOF_CGPRREGSET
   1010 		     : PPC64_LINUX_SIZEOF_CGPRREGSET),
   1011 		    cgprregset);
   1012       return;
   1013     }
   1014   else if (PPC_IS_CKPTFP_REGNUM (regno))
   1015     {
   1016       gdb_assert (tdep->have_htm_fpu);
   1017 
   1018       fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
   1019 		    PPC_LINUX_SIZEOF_CFPRREGSET,
   1020 		    &ppc32_linux_cfprregset);
   1021       return;
   1022     }
   1023   else if (PPC_IS_CKPTVMX_REGNUM (regno))
   1024     {
   1025       gdb_assert (tdep->have_htm_altivec);
   1026 
   1027       const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
   1028       fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
   1029 		    PPC_LINUX_SIZEOF_CVMXREGSET,
   1030 		    cvmxregset);
   1031       return;
   1032     }
   1033   else if (PPC_IS_CKPTVSX_REGNUM (regno))
   1034     {
   1035       gdb_assert (tdep->have_htm_vsx);
   1036 
   1037       fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
   1038 		    PPC_LINUX_SIZEOF_CVSXREGSET,
   1039 		    &ppc32_linux_cvsxregset);
   1040       return;
   1041     }
   1042   else if (regno == PPC_CPPR_REGNUM)
   1043     {
   1044       gdb_assert (tdep->ppc_cppr_regnum != -1);
   1045 
   1046       fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
   1047 		    PPC_LINUX_SIZEOF_CPPRREGSET,
   1048 		    &ppc32_linux_cpprregset);
   1049       return;
   1050     }
   1051   else if (regno == PPC_CDSCR_REGNUM)
   1052     {
   1053       gdb_assert (tdep->ppc_cdscr_regnum != -1);
   1054 
   1055       fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
   1056 		    PPC_LINUX_SIZEOF_CDSCRREGSET,
   1057 		    &ppc32_linux_cdscrregset);
   1058       return;
   1059     }
   1060   else if (regno == PPC_CTAR_REGNUM)
   1061     {
   1062       gdb_assert (tdep->ppc_ctar_regnum != -1);
   1063 
   1064       fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
   1065 		    PPC_LINUX_SIZEOF_CTARREGSET,
   1066 		    &ppc32_linux_ctarregset);
   1067       return;
   1068     }
   1069 
   1070   if (regaddr == -1)
   1071     {
   1072       memset (buf, '\0', register_size (gdbarch, regno));   /* Supply zeroes */
   1073       regcache->raw_supply (regno, buf);
   1074       return;
   1075     }
   1076 
   1077   /* Read the raw register using sizeof(long) sized chunks.  On a
   1078      32-bit platform, 64-bit floating-point registers will require two
   1079      transfers.  */
   1080   for (bytes_transferred = 0;
   1081        bytes_transferred < register_size (gdbarch, regno);
   1082        bytes_transferred += sizeof (long))
   1083     {
   1084       long l;
   1085 
   1086       errno = 0;
   1087       l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
   1088       regaddr += sizeof (long);
   1089       if (errno != 0)
   1090 	{
   1091 	  char message[128];
   1092 	  xsnprintf (message, sizeof (message), "reading register %s (#%d)",
   1093 		     gdbarch_register_name (gdbarch, regno), regno);
   1094 	  perror_with_name (message);
   1095 	}
   1096       memcpy (&buf[bytes_transferred], &l, sizeof (l));
   1097     }
   1098 
   1099   /* Now supply the register.  Keep in mind that the regcache's idea
   1100      of the register's size may not be a multiple of sizeof
   1101      (long).  */
   1102   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
   1103     {
   1104       /* Little-endian values are always found at the left end of the
   1105 	 bytes transferred.  */
   1106       regcache->raw_supply (regno, buf);
   1107     }
   1108   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1109     {
   1110       /* Big-endian values are found at the right end of the bytes
   1111 	 transferred.  */
   1112       size_t padding = (bytes_transferred - register_size (gdbarch, regno));
   1113       regcache->raw_supply (regno, buf + padding);
   1114     }
   1115   else
   1116     internal_error (_("fetch_register: unexpected byte order: %d"),
   1117 		    gdbarch_byte_order (gdbarch));
   1118 }
   1119 
   1120 /* This function actually issues the request to ptrace, telling
   1121    it to get all general-purpose registers and put them into the
   1122    specified regset.
   1123 
   1124    If the ptrace request does not exist, this function returns 0
   1125    and properly sets the have_ptrace_* flag.  If the request fails,
   1126    this function calls perror_with_name.  Otherwise, if the request
   1127    succeeds, then the regcache gets filled and 1 is returned.  */
   1128 static int
   1129 fetch_all_gp_regs (struct regcache *regcache, int tid)
   1130 {
   1131   gdb_gregset_t gregset;
   1132 
   1133   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
   1134     {
   1135       if (errno == EIO)
   1136 	{
   1137 	  have_ptrace_getsetregs = 0;
   1138 	  return 0;
   1139 	}
   1140       perror_with_name (_("Couldn't get general-purpose registers"));
   1141     }
   1142 
   1143   supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
   1144 
   1145   return 1;
   1146 }
   1147 
   1148 /* This is a wrapper for the fetch_all_gp_regs function.  It is
   1149    responsible for verifying if this target has the ptrace request
   1150    that can be used to fetch all general-purpose registers at one
   1151    shot.  If it doesn't, then we should fetch them using the
   1152    old-fashioned way, which is to iterate over the registers and
   1153    request them one by one.  */
   1154 static void
   1155 fetch_gp_regs (struct regcache *regcache, int tid)
   1156 {
   1157   struct gdbarch *gdbarch = regcache->arch ();
   1158   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1159   int i;
   1160 
   1161   if (have_ptrace_getsetregs)
   1162     if (fetch_all_gp_regs (regcache, tid))
   1163       return;
   1164 
   1165   /* If we've hit this point, it doesn't really matter which
   1166      architecture we are using.  We just need to read the
   1167      registers in the "old-fashioned way".  */
   1168   for (i = 0; i < ppc_num_gprs; i++)
   1169     fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
   1170 }
   1171 
   1172 /* This function actually issues the request to ptrace, telling
   1173    it to get all floating-point registers and put them into the
   1174    specified regset.
   1175 
   1176    If the ptrace request does not exist, this function returns 0
   1177    and properly sets the have_ptrace_* flag.  If the request fails,
   1178    this function calls perror_with_name.  Otherwise, if the request
   1179    succeeds, then the regcache gets filled and 1 is returned.  */
   1180 static int
   1181 fetch_all_fp_regs (struct regcache *regcache, int tid)
   1182 {
   1183   gdb_fpregset_t fpregs;
   1184 
   1185   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
   1186     {
   1187       if (errno == EIO)
   1188 	{
   1189 	  have_ptrace_getsetfpregs = 0;
   1190 	  return 0;
   1191 	}
   1192       perror_with_name (_("Couldn't get floating-point registers"));
   1193     }
   1194 
   1195   supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
   1196 
   1197   return 1;
   1198 }
   1199 
   1200 /* This is a wrapper for the fetch_all_fp_regs function.  It is
   1201    responsible for verifying if this target has the ptrace request
   1202    that can be used to fetch all floating-point registers at one
   1203    shot.  If it doesn't, then we should fetch them using the
   1204    old-fashioned way, which is to iterate over the registers and
   1205    request them one by one.  */
   1206 static void
   1207 fetch_fp_regs (struct regcache *regcache, int tid)
   1208 {
   1209   struct gdbarch *gdbarch = regcache->arch ();
   1210   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1211   int i;
   1212 
   1213   if (have_ptrace_getsetfpregs)
   1214     if (fetch_all_fp_regs (regcache, tid))
   1215       return;
   1216 
   1217   /* If we've hit this point, it doesn't really matter which
   1218      architecture we are using.  We just need to read the
   1219      registers in the "old-fashioned way".  */
   1220   for (i = 0; i < ppc_num_fprs; i++)
   1221     fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
   1222 }
   1223 
   1224 static void
   1225 fetch_ppc_registers (struct regcache *regcache, int tid)
   1226 {
   1227   struct gdbarch *gdbarch = regcache->arch ();
   1228   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1229 
   1230   fetch_gp_regs (regcache, tid);
   1231   if (tdep->ppc_fp0_regnum >= 0)
   1232     fetch_fp_regs (regcache, tid);
   1233   fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
   1234   if (tdep->ppc_ps_regnum != -1)
   1235     fetch_register (regcache, tid, tdep->ppc_ps_regnum);
   1236   if (tdep->ppc_cr_regnum != -1)
   1237     fetch_register (regcache, tid, tdep->ppc_cr_regnum);
   1238   if (tdep->ppc_lr_regnum != -1)
   1239     fetch_register (regcache, tid, tdep->ppc_lr_regnum);
   1240   if (tdep->ppc_ctr_regnum != -1)
   1241     fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
   1242   if (tdep->ppc_xer_regnum != -1)
   1243     fetch_register (regcache, tid, tdep->ppc_xer_regnum);
   1244   if (tdep->ppc_mq_regnum != -1)
   1245     fetch_register (regcache, tid, tdep->ppc_mq_regnum);
   1246   if (ppc_linux_trap_reg_p (gdbarch))
   1247     {
   1248       fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
   1249       fetch_register (regcache, tid, PPC_TRAP_REGNUM);
   1250     }
   1251   if (tdep->ppc_fpscr_regnum != -1)
   1252     fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
   1253   if (have_ptrace_getvrregs)
   1254     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
   1255       fetch_altivec_registers (regcache, tid, -1);
   1256   if (have_ptrace_getsetvsxregs)
   1257     if (tdep->ppc_vsr0_upper_regnum != -1)
   1258       fetch_vsx_registers (regcache, tid, -1);
   1259   if (tdep->ppc_ev0_upper_regnum >= 0)
   1260     fetch_spe_register (regcache, tid, -1);
   1261   if (tdep->ppc_ppr_regnum != -1)
   1262     fetch_regset (regcache, tid, NT_PPC_PPR,
   1263 		  PPC_LINUX_SIZEOF_PPRREGSET,
   1264 		  &ppc32_linux_pprregset);
   1265   if (tdep->ppc_dscr_regnum != -1)
   1266     fetch_regset (regcache, tid, NT_PPC_DSCR,
   1267 		  PPC_LINUX_SIZEOF_DSCRREGSET,
   1268 		  &ppc32_linux_dscrregset);
   1269   if (tdep->ppc_tar_regnum != -1)
   1270     fetch_regset (regcache, tid, NT_PPC_TAR,
   1271 		  PPC_LINUX_SIZEOF_TARREGSET,
   1272 		  &ppc32_linux_tarregset);
   1273   if (tdep->have_ebb)
   1274     fetch_regset (regcache, tid, NT_PPC_EBB,
   1275 		  PPC_LINUX_SIZEOF_EBBREGSET,
   1276 		  &ppc32_linux_ebbregset);
   1277   if (tdep->ppc_mmcr0_regnum != -1)
   1278     fetch_regset (regcache, tid, NT_PPC_PMU,
   1279 		  PPC_LINUX_SIZEOF_PMUREGSET,
   1280 		  &ppc32_linux_pmuregset);
   1281   if (tdep->have_htm_spr)
   1282     fetch_regset (regcache, tid, NT_PPC_TM_SPR,
   1283 		  PPC_LINUX_SIZEOF_TM_SPRREGSET,
   1284 		  &ppc32_linux_tm_sprregset);
   1285   if (tdep->have_htm_core)
   1286     {
   1287       const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
   1288       fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
   1289 		    (tdep->wordsize == 4?
   1290 		     PPC32_LINUX_SIZEOF_CGPRREGSET
   1291 		     : PPC64_LINUX_SIZEOF_CGPRREGSET),
   1292 		    cgprregset);
   1293     }
   1294   if (tdep->have_htm_fpu)
   1295     fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
   1296 		  PPC_LINUX_SIZEOF_CFPRREGSET,
   1297 		  &ppc32_linux_cfprregset);
   1298   if (tdep->have_htm_altivec)
   1299     {
   1300       const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
   1301       fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
   1302 		    PPC_LINUX_SIZEOF_CVMXREGSET,
   1303 		    cvmxregset);
   1304     }
   1305   if (tdep->have_htm_vsx)
   1306     fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
   1307 		  PPC_LINUX_SIZEOF_CVSXREGSET,
   1308 		  &ppc32_linux_cvsxregset);
   1309   if (tdep->ppc_cppr_regnum != -1)
   1310     fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
   1311 		  PPC_LINUX_SIZEOF_CPPRREGSET,
   1312 		  &ppc32_linux_cpprregset);
   1313   if (tdep->ppc_cdscr_regnum != -1)
   1314     fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
   1315 		  PPC_LINUX_SIZEOF_CDSCRREGSET,
   1316 		  &ppc32_linux_cdscrregset);
   1317   if (tdep->ppc_ctar_regnum != -1)
   1318     fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
   1319 		  PPC_LINUX_SIZEOF_CTARREGSET,
   1320 		  &ppc32_linux_ctarregset);
   1321 }
   1322 
   1323 /* Fetch registers from the child process.  Fetch all registers if
   1324    regno == -1, otherwise fetch all general registers or all floating
   1325    point registers depending upon the value of regno.  */
   1326 void
   1327 ppc_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
   1328 {
   1329   pid_t tid = get_ptrace_pid (regcache->ptid ());
   1330 
   1331   if (regno == -1)
   1332     fetch_ppc_registers (regcache, tid);
   1333   else
   1334     fetch_register (regcache, tid, regno);
   1335 }
   1336 
   1337 static void
   1338 store_vsx_registers (const struct regcache *regcache, int tid, int regno)
   1339 {
   1340   int ret;
   1341   gdb_vsxregset_t regs;
   1342   const struct regset *vsxregset = ppc_linux_vsxregset ();
   1343 
   1344   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
   1345   if (ret < 0)
   1346     {
   1347       if (errno == EIO)
   1348 	{
   1349 	  have_ptrace_getsetvsxregs = 0;
   1350 	  return;
   1351 	}
   1352       perror_with_name (_("Unable to fetch VSX registers"));
   1353     }
   1354 
   1355   vsxregset->collect_regset (vsxregset, regcache, regno, &regs,
   1356 			     PPC_LINUX_SIZEOF_VSXREGSET);
   1357 
   1358   ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
   1359   if (ret < 0)
   1360     perror_with_name (_("Unable to store VSX registers"));
   1361 }
   1362 
   1363 static void
   1364 store_altivec_registers (const struct regcache *regcache, int tid,
   1365 			 int regno)
   1366 {
   1367   int ret;
   1368   gdb_vrregset_t regs;
   1369   struct gdbarch *gdbarch = regcache->arch ();
   1370   const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
   1371 
   1372   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
   1373   if (ret < 0)
   1374     {
   1375       if (errno == EIO)
   1376 	{
   1377 	  have_ptrace_getvrregs = 0;
   1378 	  return;
   1379 	}
   1380       perror_with_name (_("Unable to fetch AltiVec registers"));
   1381     }
   1382 
   1383   vrregset->collect_regset (vrregset, regcache, regno, &regs,
   1384 			    PPC_LINUX_SIZEOF_VRREGSET);
   1385 
   1386   ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
   1387   if (ret < 0)
   1388     perror_with_name (_("Unable to store AltiVec registers"));
   1389 }
   1390 
   1391 /* Assuming TID refers to an SPE process, set the top halves of TID's
   1392    general-purpose registers and its SPE-specific registers to the
   1393    values in EVRREGSET.  If we don't support PTRACE_SETEVRREGS, do
   1394    nothing.
   1395 
   1396    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
   1397    PTRACE_SETEVRREGS requests are supported is isolated here, and in
   1398    get_spe_registers.  */
   1399 static void
   1400 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
   1401 {
   1402   if (have_ptrace_getsetevrregs)
   1403     {
   1404       if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
   1405 	return;
   1406       else
   1407 	{
   1408 	  /* EIO means that the PTRACE_SETEVRREGS request isn't
   1409 	     supported; we fail silently, and don't try the call
   1410 	     again.  */
   1411 	  if (errno == EIO)
   1412 	    have_ptrace_getsetevrregs = 0;
   1413 	  else
   1414 	    /* Anything else needs to be reported.  */
   1415 	    perror_with_name (_("Unable to set SPE registers"));
   1416 	}
   1417     }
   1418 }
   1419 
   1420 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
   1421    If REGNO is -1, write the values of all the SPE-specific
   1422    registers.  */
   1423 static void
   1424 store_spe_register (const struct regcache *regcache, int tid, int regno)
   1425 {
   1426   struct gdbarch *gdbarch = regcache->arch ();
   1427   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1428   struct gdb_evrregset_t evrregs;
   1429 
   1430   gdb_assert (sizeof (evrregs.evr[0])
   1431 	      == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
   1432   gdb_assert (sizeof (evrregs.acc)
   1433 	      == register_size (gdbarch, tdep->ppc_acc_regnum));
   1434   gdb_assert (sizeof (evrregs.spefscr)
   1435 	      == register_size (gdbarch, tdep->ppc_spefscr_regnum));
   1436 
   1437   if (regno == -1)
   1438     /* Since we're going to write out every register, the code below
   1439        should store to every field of evrregs; if that doesn't happen,
   1440        make it obvious by initializing it with suspicious values.  */
   1441     memset (&evrregs, 42, sizeof (evrregs));
   1442   else
   1443     /* We can only read and write the entire EVR register set at a
   1444        time, so to write just a single register, we do a
   1445        read-modify-write maneuver.  */
   1446     get_spe_registers (tid, &evrregs);
   1447 
   1448   if (regno == -1)
   1449     {
   1450       int i;
   1451 
   1452       for (i = 0; i < ppc_num_gprs; i++)
   1453 	regcache->raw_collect (tdep->ppc_ev0_upper_regnum + i,
   1454 			       &evrregs.evr[i]);
   1455     }
   1456   else if (tdep->ppc_ev0_upper_regnum <= regno
   1457 	   && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
   1458     regcache->raw_collect (regno,
   1459 			   &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
   1460 
   1461   if (regno == -1
   1462       || regno == tdep->ppc_acc_regnum)
   1463     regcache->raw_collect (tdep->ppc_acc_regnum,
   1464 			   &evrregs.acc);
   1465 
   1466   if (regno == -1
   1467       || regno == tdep->ppc_spefscr_regnum)
   1468     regcache->raw_collect (tdep->ppc_spefscr_regnum,
   1469 			   &evrregs.spefscr);
   1470 
   1471   /* Write back the modified register set.  */
   1472   set_spe_registers (tid, &evrregs);
   1473 }
   1474 
   1475 static void
   1476 store_register (const struct regcache *regcache, int tid, int regno)
   1477 {
   1478   struct gdbarch *gdbarch = regcache->arch ();
   1479   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1480   /* This isn't really an address.  But ptrace thinks of it as one.  */
   1481   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
   1482   int i;
   1483   size_t bytes_to_transfer;
   1484   gdb_byte buf[PPC_MAX_REGISTER_SIZE];
   1485 
   1486   if (altivec_register_p (gdbarch, regno))
   1487     {
   1488       store_altivec_registers (regcache, tid, regno);
   1489       return;
   1490     }
   1491   else if (vsx_register_p (gdbarch, regno))
   1492     {
   1493       store_vsx_registers (regcache, tid, regno);
   1494       return;
   1495     }
   1496   else if (spe_register_p (gdbarch, regno))
   1497     {
   1498       store_spe_register (regcache, tid, regno);
   1499       return;
   1500     }
   1501   else if (regno == PPC_DSCR_REGNUM)
   1502     {
   1503       gdb_assert (tdep->ppc_dscr_regnum != -1);
   1504 
   1505       store_regset (regcache, tid, regno, NT_PPC_DSCR,
   1506 		    PPC_LINUX_SIZEOF_DSCRREGSET,
   1507 		    &ppc32_linux_dscrregset);
   1508       return;
   1509     }
   1510   else if (regno == PPC_PPR_REGNUM)
   1511     {
   1512       gdb_assert (tdep->ppc_ppr_regnum != -1);
   1513 
   1514       store_regset (regcache, tid, regno, NT_PPC_PPR,
   1515 		    PPC_LINUX_SIZEOF_PPRREGSET,
   1516 		    &ppc32_linux_pprregset);
   1517       return;
   1518     }
   1519   else if (regno == PPC_TAR_REGNUM)
   1520     {
   1521       gdb_assert (tdep->ppc_tar_regnum != -1);
   1522 
   1523       store_regset (regcache, tid, regno, NT_PPC_TAR,
   1524 		    PPC_LINUX_SIZEOF_TARREGSET,
   1525 		    &ppc32_linux_tarregset);
   1526       return;
   1527     }
   1528   else if (PPC_IS_EBB_REGNUM (regno))
   1529     {
   1530       gdb_assert (tdep->have_ebb);
   1531 
   1532       store_regset (regcache, tid, regno, NT_PPC_EBB,
   1533 		    PPC_LINUX_SIZEOF_EBBREGSET,
   1534 		    &ppc32_linux_ebbregset);
   1535       return;
   1536     }
   1537   else if (PPC_IS_PMU_REGNUM (regno))
   1538     {
   1539       gdb_assert (tdep->ppc_mmcr0_regnum != -1);
   1540 
   1541       store_regset (regcache, tid, regno, NT_PPC_PMU,
   1542 		    PPC_LINUX_SIZEOF_PMUREGSET,
   1543 		    &ppc32_linux_pmuregset);
   1544       return;
   1545     }
   1546   else if (PPC_IS_TMSPR_REGNUM (regno))
   1547     {
   1548       gdb_assert (tdep->have_htm_spr);
   1549 
   1550       store_regset (regcache, tid, regno, NT_PPC_TM_SPR,
   1551 		    PPC_LINUX_SIZEOF_TM_SPRREGSET,
   1552 		    &ppc32_linux_tm_sprregset);
   1553       return;
   1554     }
   1555   else if (PPC_IS_CKPTGP_REGNUM (regno))
   1556     {
   1557       gdb_assert (tdep->have_htm_core);
   1558 
   1559       const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
   1560       store_regset (regcache, tid, regno, NT_PPC_TM_CGPR,
   1561 		    (tdep->wordsize == 4?
   1562 		     PPC32_LINUX_SIZEOF_CGPRREGSET
   1563 		     : PPC64_LINUX_SIZEOF_CGPRREGSET),
   1564 		    cgprregset);
   1565       return;
   1566     }
   1567   else if (PPC_IS_CKPTFP_REGNUM (regno))
   1568     {
   1569       gdb_assert (tdep->have_htm_fpu);
   1570 
   1571       store_regset (regcache, tid, regno, NT_PPC_TM_CFPR,
   1572 		    PPC_LINUX_SIZEOF_CFPRREGSET,
   1573 		    &ppc32_linux_cfprregset);
   1574       return;
   1575     }
   1576   else if (PPC_IS_CKPTVMX_REGNUM (regno))
   1577     {
   1578       gdb_assert (tdep->have_htm_altivec);
   1579 
   1580       const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
   1581       store_regset (regcache, tid, regno, NT_PPC_TM_CVMX,
   1582 		    PPC_LINUX_SIZEOF_CVMXREGSET,
   1583 		    cvmxregset);
   1584       return;
   1585     }
   1586   else if (PPC_IS_CKPTVSX_REGNUM (regno))
   1587     {
   1588       gdb_assert (tdep->have_htm_vsx);
   1589 
   1590       store_regset (regcache, tid, regno, NT_PPC_TM_CVSX,
   1591 		    PPC_LINUX_SIZEOF_CVSXREGSET,
   1592 		    &ppc32_linux_cvsxregset);
   1593       return;
   1594     }
   1595   else if (regno == PPC_CPPR_REGNUM)
   1596     {
   1597       gdb_assert (tdep->ppc_cppr_regnum != -1);
   1598 
   1599       store_regset (regcache, tid, regno, NT_PPC_TM_CPPR,
   1600 		    PPC_LINUX_SIZEOF_CPPRREGSET,
   1601 		    &ppc32_linux_cpprregset);
   1602       return;
   1603     }
   1604   else if (regno == PPC_CDSCR_REGNUM)
   1605     {
   1606       gdb_assert (tdep->ppc_cdscr_regnum != -1);
   1607 
   1608       store_regset (regcache, tid, regno, NT_PPC_TM_CDSCR,
   1609 		    PPC_LINUX_SIZEOF_CDSCRREGSET,
   1610 		    &ppc32_linux_cdscrregset);
   1611       return;
   1612     }
   1613   else if (regno == PPC_CTAR_REGNUM)
   1614     {
   1615       gdb_assert (tdep->ppc_ctar_regnum != -1);
   1616 
   1617       store_regset (regcache, tid, regno, NT_PPC_TM_CTAR,
   1618 		    PPC_LINUX_SIZEOF_CTARREGSET,
   1619 		    &ppc32_linux_ctarregset);
   1620       return;
   1621     }
   1622 
   1623   if (regaddr == -1)
   1624     return;
   1625 
   1626   /* First collect the register.  Keep in mind that the regcache's
   1627      idea of the register's size may not be a multiple of sizeof
   1628      (long).  */
   1629   memset (buf, 0, sizeof buf);
   1630   bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
   1631   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
   1632     {
   1633       /* Little-endian values always sit at the left end of the buffer.  */
   1634       regcache->raw_collect (regno, buf);
   1635     }
   1636   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1637     {
   1638       /* Big-endian values sit at the right end of the buffer.  */
   1639       size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
   1640       regcache->raw_collect (regno, buf + padding);
   1641     }
   1642 
   1643   for (i = 0; i < bytes_to_transfer; i += sizeof (long))
   1644     {
   1645       long l;
   1646 
   1647       memcpy (&l, &buf[i], sizeof (l));
   1648       errno = 0;
   1649       ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
   1650       regaddr += sizeof (long);
   1651 
   1652       if (errno == EIO
   1653 	  && (regno == tdep->ppc_fpscr_regnum
   1654 	      || regno == PPC_ORIG_R3_REGNUM
   1655 	      || regno == PPC_TRAP_REGNUM))
   1656 	{
   1657 	  /* Some older kernel versions don't allow fpscr, orig_r3
   1658 	     or trap to be written.  */
   1659 	  continue;
   1660 	}
   1661 
   1662       if (errno != 0)
   1663 	{
   1664 	  char message[128];
   1665 	  xsnprintf (message, sizeof (message), "writing register %s (#%d)",
   1666 		     gdbarch_register_name (gdbarch, regno), regno);
   1667 	  perror_with_name (message);
   1668 	}
   1669     }
   1670 }
   1671 
   1672 /* This function actually issues the request to ptrace, telling
   1673    it to store all general-purpose registers present in the specified
   1674    regset.
   1675 
   1676    If the ptrace request does not exist, this function returns 0
   1677    and properly sets the have_ptrace_* flag.  If the request fails,
   1678    this function calls perror_with_name.  Otherwise, if the request
   1679    succeeds, then the regcache is stored and 1 is returned.  */
   1680 static int
   1681 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
   1682 {
   1683   gdb_gregset_t gregset;
   1684 
   1685   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
   1686     {
   1687       if (errno == EIO)
   1688 	{
   1689 	  have_ptrace_getsetregs = 0;
   1690 	  return 0;
   1691 	}
   1692       perror_with_name (_("Couldn't get general-purpose registers"));
   1693     }
   1694 
   1695   fill_gregset (regcache, &gregset, regno);
   1696 
   1697   if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
   1698     {
   1699       if (errno == EIO)
   1700 	{
   1701 	  have_ptrace_getsetregs = 0;
   1702 	  return 0;
   1703 	}
   1704       perror_with_name (_("Couldn't set general-purpose registers"));
   1705     }
   1706 
   1707   return 1;
   1708 }
   1709 
   1710 /* This is a wrapper for the store_all_gp_regs function.  It is
   1711    responsible for verifying if this target has the ptrace request
   1712    that can be used to store all general-purpose registers at one
   1713    shot.  If it doesn't, then we should store them using the
   1714    old-fashioned way, which is to iterate over the registers and
   1715    store them one by one.  */
   1716 static void
   1717 store_gp_regs (const struct regcache *regcache, int tid, int regno)
   1718 {
   1719   struct gdbarch *gdbarch = regcache->arch ();
   1720   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1721   int i;
   1722 
   1723   if (have_ptrace_getsetregs)
   1724     if (store_all_gp_regs (regcache, tid, regno))
   1725       return;
   1726 
   1727   /* If we hit this point, it doesn't really matter which
   1728      architecture we are using.  We just need to store the
   1729      registers in the "old-fashioned way".  */
   1730   for (i = 0; i < ppc_num_gprs; i++)
   1731     store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
   1732 }
   1733 
   1734 /* This function actually issues the request to ptrace, telling
   1735    it to store all floating-point registers present in the specified
   1736    regset.
   1737 
   1738    If the ptrace request does not exist, this function returns 0
   1739    and properly sets the have_ptrace_* flag.  If the request fails,
   1740    this function calls perror_with_name.  Otherwise, if the request
   1741    succeeds, then the regcache is stored and 1 is returned.  */
   1742 static int
   1743 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
   1744 {
   1745   gdb_fpregset_t fpregs;
   1746 
   1747   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
   1748     {
   1749       if (errno == EIO)
   1750 	{
   1751 	  have_ptrace_getsetfpregs = 0;
   1752 	  return 0;
   1753 	}
   1754       perror_with_name (_("Couldn't get floating-point registers"));
   1755     }
   1756 
   1757   fill_fpregset (regcache, &fpregs, regno);
   1758 
   1759   if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
   1760     {
   1761       if (errno == EIO)
   1762 	{
   1763 	  have_ptrace_getsetfpregs = 0;
   1764 	  return 0;
   1765 	}
   1766       perror_with_name (_("Couldn't set floating-point registers"));
   1767     }
   1768 
   1769   return 1;
   1770 }
   1771 
   1772 /* This is a wrapper for the store_all_fp_regs function.  It is
   1773    responsible for verifying if this target has the ptrace request
   1774    that can be used to store all floating-point registers at one
   1775    shot.  If it doesn't, then we should store them using the
   1776    old-fashioned way, which is to iterate over the registers and
   1777    store them one by one.  */
   1778 static void
   1779 store_fp_regs (const struct regcache *regcache, int tid, int regno)
   1780 {
   1781   struct gdbarch *gdbarch = regcache->arch ();
   1782   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1783   int i;
   1784 
   1785   if (have_ptrace_getsetfpregs)
   1786     if (store_all_fp_regs (regcache, tid, regno))
   1787       return;
   1788 
   1789   /* If we hit this point, it doesn't really matter which
   1790      architecture we are using.  We just need to store the
   1791      registers in the "old-fashioned way".  */
   1792   for (i = 0; i < ppc_num_fprs; i++)
   1793     store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
   1794 }
   1795 
   1796 static void
   1797 store_ppc_registers (const struct regcache *regcache, int tid)
   1798 {
   1799   struct gdbarch *gdbarch = regcache->arch ();
   1800   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
   1801 
   1802   store_gp_regs (regcache, tid, -1);
   1803   if (tdep->ppc_fp0_regnum >= 0)
   1804     store_fp_regs (regcache, tid, -1);
   1805   store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
   1806   if (tdep->ppc_ps_regnum != -1)
   1807     store_register (regcache, tid, tdep->ppc_ps_regnum);
   1808   if (tdep->ppc_cr_regnum != -1)
   1809     store_register (regcache, tid, tdep->ppc_cr_regnum);
   1810   if (tdep->ppc_lr_regnum != -1)
   1811     store_register (regcache, tid, tdep->ppc_lr_regnum);
   1812   if (tdep->ppc_ctr_regnum != -1)
   1813     store_register (regcache, tid, tdep->ppc_ctr_regnum);
   1814   if (tdep->ppc_xer_regnum != -1)
   1815     store_register (regcache, tid, tdep->ppc_xer_regnum);
   1816   if (tdep->ppc_mq_regnum != -1)
   1817     store_register (regcache, tid, tdep->ppc_mq_regnum);
   1818   if (tdep->ppc_fpscr_regnum != -1)
   1819     store_register (regcache, tid, tdep->ppc_fpscr_regnum);
   1820   if (ppc_linux_trap_reg_p (gdbarch))
   1821     {
   1822       store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
   1823       store_register (regcache, tid, PPC_TRAP_REGNUM);
   1824     }
   1825   if (have_ptrace_getvrregs)
   1826     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
   1827       store_altivec_registers (regcache, tid, -1);
   1828   if (have_ptrace_getsetvsxregs)
   1829     if (tdep->ppc_vsr0_upper_regnum != -1)
   1830       store_vsx_registers (regcache, tid, -1);
   1831   if (tdep->ppc_ev0_upper_regnum >= 0)
   1832     store_spe_register (regcache, tid, -1);
   1833   if (tdep->ppc_ppr_regnum != -1)
   1834     store_regset (regcache, tid, -1, NT_PPC_PPR,
   1835 		  PPC_LINUX_SIZEOF_PPRREGSET,
   1836 		  &ppc32_linux_pprregset);
   1837   if (tdep->ppc_dscr_regnum != -1)
   1838     store_regset (regcache, tid, -1, NT_PPC_DSCR,
   1839 		  PPC_LINUX_SIZEOF_DSCRREGSET,
   1840 		  &ppc32_linux_dscrregset);
   1841   if (tdep->ppc_tar_regnum != -1)
   1842     store_regset (regcache, tid, -1, NT_PPC_TAR,
   1843 		  PPC_LINUX_SIZEOF_TARREGSET,
   1844 		  &ppc32_linux_tarregset);
   1845 
   1846   if (tdep->ppc_mmcr0_regnum != -1)
   1847     store_regset (regcache, tid, -1, NT_PPC_PMU,
   1848 		  PPC_LINUX_SIZEOF_PMUREGSET,
   1849 		  &ppc32_linux_pmuregset);
   1850 
   1851   if (tdep->have_htm_spr)
   1852     store_regset (regcache, tid, -1, NT_PPC_TM_SPR,
   1853 		  PPC_LINUX_SIZEOF_TM_SPRREGSET,
   1854 		  &ppc32_linux_tm_sprregset);
   1855 
   1856   /* Because the EBB and checkpointed HTM registers can be
   1857      unavailable, attempts to store them here would cause this
   1858      function to fail most of the time, so we ignore them.  */
   1859 }
   1860 
   1861 void
   1862 ppc_linux_nat_target::store_registers (struct regcache *regcache, int regno)
   1863 {
   1864   pid_t tid = get_ptrace_pid (regcache->ptid ());
   1865 
   1866   if (regno >= 0)
   1867     store_register (regcache, tid, regno);
   1868   else
   1869     store_ppc_registers (regcache, tid);
   1870 }
   1871 
   1872 /* Functions for transferring registers between a gregset_t or fpregset_t
   1873    (see sys/ucontext.h) and gdb's regcache.  The word size is that used
   1874    by the ptrace interface, not the current program's ABI.  Eg. if a
   1875    powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
   1876    read or write 64-bit gregsets.  This is to suit the host libthread_db.  */
   1877 
   1878 void
   1879 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
   1880 {
   1881   const struct regset *regset = ppc_linux_gregset (sizeof (long));
   1882 
   1883   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
   1884 }
   1885 
   1886 void
   1887 fill_gregset (const struct regcache *regcache,
   1888 	      gdb_gregset_t *gregsetp, int regno)
   1889 {
   1890   const struct regset *regset = ppc_linux_gregset (sizeof (long));
   1891 
   1892   if (regno == -1)
   1893     memset (gregsetp, 0, sizeof (*gregsetp));
   1894   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
   1895 }
   1896 
   1897 void
   1898 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
   1899 {
   1900   const struct regset *regset = ppc_linux_fpregset ();
   1901 
   1902   ppc_supply_fpregset (regset, regcache, -1,
   1903 		       fpregsetp, sizeof (*fpregsetp));
   1904 }
   1905 
   1906 void
   1907 fill_fpregset (const struct regcache *regcache,
   1908 	       gdb_fpregset_t *fpregsetp, int regno)
   1909 {
   1910   const struct regset *regset = ppc_linux_fpregset ();
   1911 
   1912   ppc_collect_fpregset (regset, regcache, regno,
   1913 			fpregsetp, sizeof (*fpregsetp));
   1914 }
   1915 
   1916 int
   1917 ppc_linux_nat_target::auxv_parse (const gdb_byte **readptr,
   1918 				  const gdb_byte *endptr, CORE_ADDR *typep,
   1919 				  CORE_ADDR *valp)
   1920 {
   1921   int tid = inferior_ptid.lwp ();
   1922   if (tid == 0)
   1923     tid = inferior_ptid.pid ();
   1924 
   1925   int sizeof_auxv_field = ppc_linux_target_wordsize (tid);
   1926 
   1927   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
   1928   const gdb_byte *ptr = *readptr;
   1929 
   1930   if (endptr == ptr)
   1931     return 0;
   1932 
   1933   if (endptr - ptr < sizeof_auxv_field * 2)
   1934     return -1;
   1935 
   1936   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
   1937   ptr += sizeof_auxv_field;
   1938   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
   1939   ptr += sizeof_auxv_field;
   1940 
   1941   *readptr = ptr;
   1942   return 1;
   1943 }
   1944 
   1945 const struct target_desc *
   1946 ppc_linux_nat_target::read_description ()
   1947 {
   1948   int tid = inferior_ptid.pid ();
   1949 
   1950   if (have_ptrace_getsetevrregs)
   1951     {
   1952       struct gdb_evrregset_t evrregset;
   1953 
   1954       if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
   1955 	return tdesc_powerpc_e500l;
   1956 
   1957       /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
   1958 	 Anything else needs to be reported.  */
   1959       else if (errno != EIO)
   1960 	perror_with_name (_("Unable to fetch SPE registers"));
   1961     }
   1962 
   1963   struct ppc_linux_features features = ppc_linux_no_features;
   1964 
   1965   features.wordsize = ppc_linux_target_wordsize (tid);
   1966 
   1967   CORE_ADDR hwcap = linux_get_hwcap ();
   1968   CORE_ADDR hwcap2 = linux_get_hwcap2 ();
   1969 
   1970   if (have_ptrace_getsetvsxregs
   1971       && (hwcap & PPC_FEATURE_HAS_VSX))
   1972     {
   1973       gdb_vsxregset_t vsxregset;
   1974 
   1975       if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
   1976 	features.vsx = true;
   1977 
   1978       /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
   1979 	 Anything else needs to be reported.  */
   1980       else if (errno != EIO)
   1981 	perror_with_name (_("Unable to fetch VSX registers"));
   1982     }
   1983 
   1984   if (have_ptrace_getvrregs
   1985       && (hwcap & PPC_FEATURE_HAS_ALTIVEC))
   1986     {
   1987       gdb_vrregset_t vrregset;
   1988 
   1989       if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
   1990 	features.altivec = true;
   1991 
   1992       /* EIO means that the PTRACE_GETVRREGS request isn't supported.
   1993 	 Anything else needs to be reported.  */
   1994       else if (errno != EIO)
   1995 	perror_with_name (_("Unable to fetch AltiVec registers"));
   1996     }
   1997 
   1998   features.isa205 = ppc_linux_has_isa205 (hwcap);
   1999 
   2000   if ((hwcap2 & PPC_FEATURE2_DSCR)
   2001       && check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET)
   2002       && check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET))
   2003     {
   2004       features.ppr_dscr = true;
   2005       if ((hwcap2 & PPC_FEATURE2_ARCH_2_07)
   2006 	  && (hwcap2 & PPC_FEATURE2_TAR)
   2007 	  && (hwcap2 & PPC_FEATURE2_EBB)
   2008 	  && check_regset (tid, NT_PPC_TAR, PPC_LINUX_SIZEOF_TARREGSET)
   2009 	  && check_regset (tid, NT_PPC_EBB, PPC_LINUX_SIZEOF_EBBREGSET)
   2010 	  && check_regset (tid, NT_PPC_PMU, PPC_LINUX_SIZEOF_PMUREGSET))
   2011 	{
   2012 	  features.isa207 = true;
   2013 	  if ((hwcap2 & PPC_FEATURE2_HTM)
   2014 	      && check_regset (tid, NT_PPC_TM_SPR,
   2015 			       PPC_LINUX_SIZEOF_TM_SPRREGSET))
   2016 	    features.htm = true;
   2017 	}
   2018     }
   2019 
   2020   return ppc_linux_match_description (features);
   2021 }
   2022 
   2023 /* Routines for installing hardware watchpoints and breakpoints.  When
   2024    GDB requests a hardware watchpoint or breakpoint to be installed, we
   2025    register the request for the pid of inferior_ptid in a map with one
   2026    entry per process.  We then issue a stop request to all the threads of
   2027    this process, and mark a per-thread flag indicating that their debug
   2028    registers should be updated.  Right before they are next resumed, we
   2029    remove all previously installed debug registers and install all the
   2030    ones GDB requested.  We then update a map with one entry per thread
   2031    that keeps track of what debug registers were last installed in each
   2032    thread.
   2033 
   2034    We use this second map to remove installed registers before installing
   2035    the ones requested by GDB, and to copy the debug register state after
   2036    a thread clones or forks, since depending on the kernel configuration,
   2037    debug registers can be inherited.  */
   2038 
   2039 /* Check if we support and have enough resources to install a hardware
   2040    watchpoint or breakpoint.  See the description in target.h.  */
   2041 
   2042 int
   2043 ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt,
   2044 					     int ot)
   2045 {
   2046   int total_hw_wp, total_hw_bp;
   2047 
   2048   m_dreg_interface.detect (inferior_ptid);
   2049 
   2050   if (m_dreg_interface.unavailable_p ())
   2051     return 0;
   2052 
   2053   if (m_dreg_interface.hwdebug_p ())
   2054     {
   2055       /* When PowerPC HWDEBUG ptrace interface is available, the number of
   2056 	 available hardware watchpoints and breakpoints is stored at the
   2057 	 hwdebug_info struct.  */
   2058       total_hw_bp = m_dreg_interface.hwdebug_info ().num_instruction_bps;
   2059       total_hw_wp = m_dreg_interface.hwdebug_info ().num_data_bps;
   2060     }
   2061   else
   2062     {
   2063       gdb_assert (m_dreg_interface.debugreg_p ());
   2064 
   2065       /* With the DEBUGREG ptrace interface, we should consider having 1
   2066 	 hardware watchpoint and no hardware breakpoints.  */
   2067       total_hw_bp = 0;
   2068       total_hw_wp = 1;
   2069     }
   2070 
   2071   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
   2072       || type == bp_access_watchpoint || type == bp_watchpoint)
   2073     {
   2074       if (total_hw_wp == 0)
   2075 	return 0;
   2076       else if (cnt + ot > total_hw_wp)
   2077 	return -1;
   2078       else
   2079 	return 1;
   2080     }
   2081   else if (type == bp_hardware_breakpoint)
   2082     {
   2083       if (total_hw_bp == 0)
   2084 	return 0;
   2085       else if (cnt > total_hw_bp)
   2086 	return -1;
   2087       else
   2088 	return 1;
   2089     }
   2090 
   2091   return 0;
   2092 }
   2093 
   2094 /* Returns 1 if we can watch LEN bytes at address ADDR, 0 otherwise.  */
   2095 
   2096 int
   2097 ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
   2098 {
   2099   /* Handle sub-8-byte quantities.  */
   2100   if (len <= 0)
   2101     return 0;
   2102 
   2103   m_dreg_interface.detect (inferior_ptid);
   2104 
   2105   if (m_dreg_interface.unavailable_p ())
   2106     return 0;
   2107 
   2108   /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
   2109      restrictions for watchpoints in the processors.  In that case, we use that
   2110      information to determine the hardcoded watchable region for
   2111      watchpoints.  */
   2112   if (m_dreg_interface.hwdebug_p ())
   2113     {
   2114       const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
   2115 						   .hwdebug_info ());
   2116       int region_size = hwdebug_info.data_bp_alignment;
   2117       int region_align = region_size;
   2118 
   2119       /* Embedded DAC-based processors, like the PowerPC 440 have ranged
   2120 	 watchpoints and can watch any access within an arbitrary memory
   2121 	 region. This is useful to watch arrays and structs, for instance.  It
   2122 	 takes two hardware watchpoints though.  */
   2123       if (len > 1
   2124 	  && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
   2125 	  && (linux_get_hwcap () & PPC_FEATURE_BOOKE))
   2126 	return 2;
   2127       /* Check if the processor provides DAWR interface.  */
   2128       if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
   2129 	{
   2130 	  /* DAWR interface allows to watch up to 512 byte wide ranges.  */
   2131 	  region_size = 512;
   2132 	  /* DAWR interface allows to watch up to 512 byte wide ranges which
   2133 	     can't cross a 512 byte bondary on machines that doesn't have a
   2134 	     second DAWR (P9 or less).  */
   2135 	  if (!(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_ARCH_31))
   2136 	    region_align = 512;
   2137 	}
   2138       /* Server processors provide one hardware watchpoint and addr+len should
   2139 	 fall in the watchable region provided by the ptrace interface.  */
   2140       if (region_align
   2141 	  && (addr + len > (addr & ~(region_align - 1)) + region_size))
   2142 	return 0;
   2143     }
   2144   /* addr+len must fall in the 8 byte watchable region for DABR-based
   2145      processors (i.e., server processors).  Without the new PowerPC HWDEBUG
   2146      ptrace interface, DAC-based processors (i.e., embedded processors) will
   2147      use addresses aligned to 4-bytes due to the way the read/write flags are
   2148      passed in the old ptrace interface.  */
   2149   else
   2150     {
   2151       gdb_assert (m_dreg_interface.debugreg_p ());
   2152 
   2153       if (((linux_get_hwcap () & PPC_FEATURE_BOOKE)
   2154 	   && (addr + len) > (addr & ~3) + 4)
   2155 	  || (addr + len) > (addr & ~7) + 8)
   2156 	return 0;
   2157     }
   2158 
   2159   return 1;
   2160 }
   2161 
   2162 /* This function compares two ppc_hw_breakpoint structs
   2163    field-by-field.  */
   2164 
   2165 bool
   2166 ppc_linux_nat_target::hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
   2167 					 const struct ppc_hw_breakpoint &b)
   2168 {
   2169   return (a.trigger_type == b.trigger_type
   2170 	  && a.addr_mode == b.addr_mode
   2171 	  && a.condition_mode == b.condition_mode
   2172 	  && a.addr == b.addr
   2173 	  && a.addr2 == b.addr2
   2174 	  && a.condition_value == b.condition_value);
   2175 }
   2176 
   2177 /* Return the number of registers needed for a ranged breakpoint.  */
   2178 
   2179 int
   2180 ppc_linux_nat_target::ranged_break_num_registers ()
   2181 {
   2182   m_dreg_interface.detect (inferior_ptid);
   2183 
   2184   return ((m_dreg_interface.hwdebug_p ()
   2185 	   && (m_dreg_interface.hwdebug_info ().features
   2186 	       & PPC_DEBUG_FEATURE_INSN_BP_RANGE))?
   2187 	  2 : -1);
   2188 }
   2189 
   2190 /* Register the hardware breakpoint described by BP_TGT, to be inserted
   2191    when the threads of inferior_ptid are resumed.  Returns 0 for success,
   2192    or -1 if the HWDEBUG interface that we need for hardware breakpoints
   2193    is not available.  */
   2194 
   2195 int
   2196 ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
   2197 					    struct bp_target_info *bp_tgt)
   2198 {
   2199   struct ppc_hw_breakpoint p;
   2200 
   2201   m_dreg_interface.detect (inferior_ptid);
   2202 
   2203   if (!m_dreg_interface.hwdebug_p ())
   2204     return -1;
   2205 
   2206   p.version = PPC_DEBUG_CURRENT_VERSION;
   2207   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
   2208   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2209   p.addr = (uint64_t) (bp_tgt->placed_address = bp_tgt->reqstd_address);
   2210   p.condition_value = 0;
   2211 
   2212   if (bp_tgt->length)
   2213     {
   2214       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
   2215 
   2216       /* The breakpoint will trigger if the address of the instruction is
   2217 	 within the defined range, as follows: p.addr <= address < p.addr2.  */
   2218       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
   2219     }
   2220   else
   2221     {
   2222       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
   2223       p.addr2 = 0;
   2224     }
   2225 
   2226   register_hw_breakpoint (inferior_ptid.pid (), p);
   2227 
   2228   return 0;
   2229 }
   2230 
   2231 /* Clear a registration for the hardware breakpoint given by type BP_TGT.
   2232    It will be removed from the threads of inferior_ptid when they are
   2233    next resumed.  Returns 0 for success, or -1 if the HWDEBUG interface
   2234    that we need for hardware breakpoints is not available.  */
   2235 
   2236 int
   2237 ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
   2238 					    struct bp_target_info *bp_tgt)
   2239 {
   2240   struct ppc_hw_breakpoint p;
   2241 
   2242   m_dreg_interface.detect (inferior_ptid);
   2243 
   2244   if (!m_dreg_interface.hwdebug_p ())
   2245     return -1;
   2246 
   2247   p.version = PPC_DEBUG_CURRENT_VERSION;
   2248   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
   2249   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2250   p.addr = (uint64_t) bp_tgt->placed_address;
   2251   p.condition_value = 0;
   2252 
   2253   if (bp_tgt->length)
   2254     {
   2255       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
   2256 
   2257       /* The breakpoint will trigger if the address of the instruction is within
   2258 	 the defined range, as follows: p.addr <= address < p.addr2.  */
   2259       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
   2260     }
   2261   else
   2262     {
   2263       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
   2264       p.addr2 = 0;
   2265     }
   2266 
   2267   clear_hw_breakpoint (inferior_ptid.pid (), p);
   2268 
   2269   return 0;
   2270 }
   2271 
   2272 /* Return the trigger value to set in a ppc_hw_breakpoint object for a
   2273    given hardware watchpoint TYPE.  We assume type is not hw_execute.  */
   2274 
   2275 int
   2276 ppc_linux_nat_target::get_trigger_type (enum target_hw_bp_type type)
   2277 {
   2278   int t;
   2279 
   2280   if (type == hw_read)
   2281     t = PPC_BREAKPOINT_TRIGGER_READ;
   2282   else if (type == hw_write)
   2283     t = PPC_BREAKPOINT_TRIGGER_WRITE;
   2284   else
   2285     t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
   2286 
   2287   return t;
   2288 }
   2289 
   2290 /* Register a new masked watchpoint at ADDR using the mask MASK, to be
   2291    inserted when the threads of inferior_ptid are resumed.  RW may be
   2292    hw_read for a read watchpoint, hw_write for a write watchpoint or
   2293    hw_access for an access watchpoint.  */
   2294 
   2295 int
   2296 ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr,  CORE_ADDR mask,
   2297 					      target_hw_bp_type rw)
   2298 {
   2299   struct ppc_hw_breakpoint p;
   2300 
   2301   gdb_assert (m_dreg_interface.hwdebug_p ());
   2302 
   2303   p.version = PPC_DEBUG_CURRENT_VERSION;
   2304   p.trigger_type = get_trigger_type (rw);
   2305   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
   2306   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2307   p.addr = addr;
   2308   p.addr2 = mask;
   2309   p.condition_value = 0;
   2310 
   2311   register_hw_breakpoint (inferior_ptid.pid (), p);
   2312 
   2313   return 0;
   2314 }
   2315 
   2316 /* Clear a registration for a masked watchpoint at ADDR with the mask
   2317    MASK.  It will be removed from the threads of inferior_ptid when they
   2318    are next resumed.  RW may be hw_read for a read watchpoint, hw_write
   2319    for a write watchpoint or hw_access for an access watchpoint.  */
   2320 
   2321 int
   2322 ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
   2323 					      target_hw_bp_type rw)
   2324 {
   2325   struct ppc_hw_breakpoint p;
   2326 
   2327   gdb_assert (m_dreg_interface.hwdebug_p ());
   2328 
   2329   p.version = PPC_DEBUG_CURRENT_VERSION;
   2330   p.trigger_type = get_trigger_type (rw);
   2331   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
   2332   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2333   p.addr = addr;
   2334   p.addr2 = mask;
   2335   p.condition_value = 0;
   2336 
   2337   clear_hw_breakpoint (inferior_ptid.pid (), p);
   2338 
   2339   return 0;
   2340 }
   2341 
   2342 /* Check whether we have at least one free DVC register for the threads
   2343    of the pid of inferior_ptid.  */
   2344 
   2345 bool
   2346 ppc_linux_nat_target::can_use_watchpoint_cond_accel (void)
   2347 {
   2348   m_dreg_interface.detect (inferior_ptid);
   2349 
   2350   if (!m_dreg_interface.hwdebug_p ())
   2351     return false;
   2352 
   2353   int cnt = m_dreg_interface.hwdebug_info ().num_condition_regs;
   2354 
   2355   if (cnt == 0)
   2356     return false;
   2357 
   2358   auto process_it = m_process_info.find (inferior_ptid.pid ());
   2359 
   2360   /* No breakpoints or watchpoints have been requested for this process,
   2361      we have at least one free DVC register.  */
   2362   if (process_it == m_process_info.end ())
   2363     return true;
   2364 
   2365   for (const ppc_hw_breakpoint &bp : process_it->second.requested_hw_bps)
   2366     if (bp.condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
   2367       cnt--;
   2368 
   2369   if (cnt <= 0)
   2370     return false;
   2371 
   2372   return true;
   2373 }
   2374 
   2375 /* Calculate the enable bits and the contents of the Data Value Compare
   2376    debug register present in BookE processors.
   2377 
   2378    ADDR is the address to be watched, LEN is the length of watched data
   2379    and DATA_VALUE is the value which will trigger the watchpoint.
   2380    On exit, CONDITION_MODE will hold the enable bits for the DVC, and
   2381    CONDITION_VALUE will hold the value which should be put in the
   2382    DVC register.  */
   2383 
   2384 void
   2385 ppc_linux_nat_target::calculate_dvc (CORE_ADDR addr, int len,
   2386 				     CORE_ADDR data_value,
   2387 				     uint32_t *condition_mode,
   2388 				     uint64_t *condition_value)
   2389 {
   2390   const struct ppc_debug_info &hwdebug_info = (m_dreg_interface.
   2391 					       hwdebug_info ());
   2392 
   2393   int i, num_byte_enable, align_offset, num_bytes_off_dvc,
   2394       rightmost_enabled_byte;
   2395   CORE_ADDR addr_end_data, addr_end_dvc;
   2396 
   2397   /* The DVC register compares bytes within fixed-length windows which
   2398      are word-aligned, with length equal to that of the DVC register.
   2399      We need to calculate where our watch region is relative to that
   2400      window and enable comparison of the bytes which fall within it.  */
   2401 
   2402   align_offset = addr % hwdebug_info.sizeof_condition;
   2403   addr_end_data = addr + len;
   2404   addr_end_dvc = (addr - align_offset
   2405 		  + hwdebug_info.sizeof_condition);
   2406   num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
   2407 			 addr_end_data - addr_end_dvc : 0;
   2408   num_byte_enable = len - num_bytes_off_dvc;
   2409   /* Here, bytes are numbered from right to left.  */
   2410   rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
   2411 			      addr_end_dvc - addr_end_data : 0;
   2412 
   2413   *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
   2414   for (i = 0; i < num_byte_enable; i++)
   2415     *condition_mode
   2416       |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
   2417 
   2418   /* Now we need to match the position within the DVC of the comparison
   2419      value with where the watch region is relative to the window
   2420      (i.e., the ALIGN_OFFSET).  */
   2421 
   2422   *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
   2423 		      << rightmost_enabled_byte * 8);
   2424 }
   2425 
   2426 /* Return the number of memory locations that need to be accessed to
   2427    evaluate the expression which generated the given value chain.
   2428    Returns -1 if there's any register access involved, or if there are
   2429    other kinds of values which are not acceptable in a condition
   2430    expression (e.g., lval_computed or lval_internalvar).  */
   2431 
   2432 int
   2433 ppc_linux_nat_target::num_memory_accesses (const std::vector<value_ref_ptr>
   2434 					   &chain)
   2435 {
   2436   int found_memory_cnt = 0;
   2437 
   2438   /* The idea here is that evaluating an expression generates a series
   2439      of values, one holding the value of every subexpression.  (The
   2440      expression a*b+c has five subexpressions: a, b, a*b, c, and
   2441      a*b+c.)  GDB's values hold almost enough information to establish
   2442      the criteria given above --- they identify memory lvalues,
   2443      register lvalues, computed values, etcetera.  So we can evaluate
   2444      the expression, and then scan the chain of values that leaves
   2445      behind to determine the memory locations involved in the evaluation
   2446      of an expression.
   2447 
   2448      However, I don't think that the values returned by inferior
   2449      function calls are special in any way.  So this function may not
   2450      notice that an expression contains an inferior function call.
   2451      FIXME.  */
   2452 
   2453   for (const value_ref_ptr &iter : chain)
   2454     {
   2455       struct value *v = iter.get ();
   2456 
   2457       /* Constants and values from the history are fine.  */
   2458       if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
   2459 	continue;
   2460       else if (VALUE_LVAL (v) == lval_memory)
   2461 	{
   2462 	  /* A lazy memory lvalue is one that GDB never needed to fetch;
   2463 	     we either just used its address (e.g., `a' in `a.b') or
   2464 	     we never needed it at all (e.g., `a' in `a,b').  */
   2465 	  if (!value_lazy (v))
   2466 	    found_memory_cnt++;
   2467 	}
   2468       /* Other kinds of values are not fine.  */
   2469       else
   2470 	return -1;
   2471     }
   2472 
   2473   return found_memory_cnt;
   2474 }
   2475 
   2476 /* Verifies whether the expression COND can be implemented using the
   2477    DVC (Data Value Compare) register in BookE processors.  The expression
   2478    must test the watch value for equality with a constant expression.
   2479    If the function returns 1, DATA_VALUE will contain the constant against
   2480    which the watch value should be compared and LEN will contain the size
   2481    of the constant.  */
   2482 
   2483 int
   2484 ppc_linux_nat_target::check_condition (CORE_ADDR watch_addr,
   2485 				       struct expression *cond,
   2486 				       CORE_ADDR *data_value, int *len)
   2487 {
   2488   int num_accesses_left, num_accesses_right;
   2489   struct value *left_val, *right_val;
   2490   std::vector<value_ref_ptr> left_chain, right_chain;
   2491 
   2492   expr::equal_operation *eqop
   2493     = dynamic_cast<expr::equal_operation *> (cond->op.get ());
   2494   if (eqop == nullptr)
   2495     return 0;
   2496   expr::operation *lhs = eqop->get_lhs ();
   2497   expr::operation *rhs = eqop->get_rhs ();
   2498 
   2499   fetch_subexp_value (cond, lhs, &left_val, NULL, &left_chain, false);
   2500   num_accesses_left = num_memory_accesses (left_chain);
   2501 
   2502   if (left_val == NULL || num_accesses_left < 0)
   2503     return 0;
   2504 
   2505   fetch_subexp_value (cond, rhs, &right_val, NULL, &right_chain, false);
   2506   num_accesses_right = num_memory_accesses (right_chain);
   2507 
   2508   if (right_val == NULL || num_accesses_right < 0)
   2509     return 0;
   2510 
   2511   if (num_accesses_left == 1 && num_accesses_right == 0
   2512       && VALUE_LVAL (left_val) == lval_memory
   2513       && value_address (left_val) == watch_addr)
   2514     {
   2515       *data_value = value_as_long (right_val);
   2516 
   2517       /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
   2518 	 the same type as the memory region referenced by LEFT_VAL.  */
   2519       *len = check_typedef (value_type (left_val))->length ();
   2520     }
   2521   else if (num_accesses_left == 0 && num_accesses_right == 1
   2522 	   && VALUE_LVAL (right_val) == lval_memory
   2523 	   && value_address (right_val) == watch_addr)
   2524     {
   2525       *data_value = value_as_long (left_val);
   2526 
   2527       /* DATA_VALUE is the constant in LEFT_VAL, but actually has
   2528 	 the same type as the memory region referenced by RIGHT_VAL.  */
   2529       *len = check_typedef (value_type (right_val))->length ();
   2530     }
   2531   else
   2532     return 0;
   2533 
   2534   return 1;
   2535 }
   2536 
   2537 /* Return true if the target is capable of using hardware to evaluate the
   2538    condition expression, thus only triggering the watchpoint when it is
   2539    true.  */
   2540 
   2541 bool
   2542 ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr,
   2543 						      int len, int rw,
   2544 						      struct expression *cond)
   2545 {
   2546   CORE_ADDR data_value;
   2547 
   2548   m_dreg_interface.detect (inferior_ptid);
   2549 
   2550   return (m_dreg_interface.hwdebug_p ()
   2551 	  && (m_dreg_interface.hwdebug_info ().num_condition_regs > 0)
   2552 	  && check_condition (addr, cond, &data_value, &len));
   2553 }
   2554 
   2555 /* Set up P with the parameters necessary to request a watchpoint covering
   2556    LEN bytes starting at ADDR and if possible with condition expression COND
   2557    evaluated by hardware.  INSERT tells if we are creating a request for
   2558    inserting or removing the watchpoint.  */
   2559 
   2560 void
   2561 ppc_linux_nat_target::create_watchpoint_request (struct ppc_hw_breakpoint *p,
   2562 						 CORE_ADDR addr, int len,
   2563 						 enum target_hw_bp_type type,
   2564 						 struct expression *cond,
   2565 						 int insert)
   2566 {
   2567   const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
   2568 					       .hwdebug_info ());
   2569 
   2570   if (len == 1
   2571       || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
   2572     {
   2573       int use_condition;
   2574       CORE_ADDR data_value;
   2575 
   2576       use_condition = (insert? can_use_watchpoint_cond_accel ()
   2577 			: hwdebug_info.num_condition_regs > 0);
   2578       if (cond && use_condition && check_condition (addr, cond,
   2579 						    &data_value, &len))
   2580 	calculate_dvc (addr, len, data_value, &p->condition_mode,
   2581 		       &p->condition_value);
   2582       else
   2583 	{
   2584 	  p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2585 	  p->condition_value = 0;
   2586 	}
   2587 
   2588       p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
   2589       p->addr2 = 0;
   2590     }
   2591   else
   2592     {
   2593       p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
   2594       p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
   2595       p->condition_value = 0;
   2596 
   2597       /* The watchpoint will trigger if the address of the memory access is
   2598 	 within the defined range, as follows: p->addr <= address < p->addr2.
   2599 
   2600 	 Note that the above sentence just documents how ptrace interprets
   2601 	 its arguments; the watchpoint is set to watch the range defined by
   2602 	 the user _inclusively_, as specified by the user interface.  */
   2603       p->addr2 = (uint64_t) addr + len;
   2604     }
   2605 
   2606   p->version = PPC_DEBUG_CURRENT_VERSION;
   2607   p->trigger_type = get_trigger_type (type);
   2608   p->addr = (uint64_t) addr;
   2609 }
   2610 
   2611 /* Register a watchpoint, to be inserted when the threads of the group of
   2612    inferior_ptid are next resumed.  Returns 0 on success, and -1 if there
   2613    is no ptrace interface available to install the watchpoint.  */
   2614 
   2615 int
   2616 ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
   2617 					 enum target_hw_bp_type type,
   2618 					 struct expression *cond)
   2619 {
   2620   m_dreg_interface.detect (inferior_ptid);
   2621 
   2622   if (m_dreg_interface.unavailable_p ())
   2623     return -1;
   2624 
   2625   if (m_dreg_interface.hwdebug_p ())
   2626     {
   2627       struct ppc_hw_breakpoint p;
   2628 
   2629       create_watchpoint_request (&p, addr, len, type, cond, 1);
   2630 
   2631       register_hw_breakpoint (inferior_ptid.pid (), p);
   2632     }
   2633   else
   2634     {
   2635       gdb_assert (m_dreg_interface.debugreg_p ());
   2636 
   2637       long wp_value;
   2638       long read_mode, write_mode;
   2639 
   2640       if (linux_get_hwcap () & PPC_FEATURE_BOOKE)
   2641 	{
   2642 	  /* PowerPC 440 requires only the read/write flags to be passed
   2643 	     to the kernel.  */
   2644 	  read_mode = 1;
   2645 	  write_mode = 2;
   2646 	}
   2647       else
   2648 	{
   2649 	  /* PowerPC 970 and other DABR-based processors are required to pass
   2650 	     the Breakpoint Translation bit together with the flags.  */
   2651 	  read_mode = 5;
   2652 	  write_mode = 6;
   2653 	}
   2654 
   2655       wp_value = addr & ~(read_mode | write_mode);
   2656       switch (type)
   2657 	{
   2658 	  case hw_read:
   2659 	    /* Set read and translate bits.  */
   2660 	    wp_value |= read_mode;
   2661 	    break;
   2662 	  case hw_write:
   2663 	    /* Set write and translate bits.  */
   2664 	    wp_value |= write_mode;
   2665 	    break;
   2666 	  case hw_access:
   2667 	    /* Set read, write and translate bits.  */
   2668 	    wp_value |= read_mode | write_mode;
   2669 	    break;
   2670 	}
   2671 
   2672       register_wp (inferior_ptid.pid (), wp_value);
   2673     }
   2674 
   2675   return 0;
   2676 }
   2677 
   2678 /* Clear a registration for a hardware watchpoint.  It will be removed
   2679    from the threads of the group of inferior_ptid when they are next
   2680    resumed.  */
   2681 
   2682 int
   2683 ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
   2684 					 enum target_hw_bp_type type,
   2685 					 struct expression *cond)
   2686 {
   2687   gdb_assert (!m_dreg_interface.unavailable_p ());
   2688 
   2689   if (m_dreg_interface.hwdebug_p ())
   2690     {
   2691       struct ppc_hw_breakpoint p;
   2692 
   2693       create_watchpoint_request (&p, addr, len, type, cond, 0);
   2694 
   2695       clear_hw_breakpoint (inferior_ptid.pid (), p);
   2696     }
   2697   else
   2698     {
   2699       gdb_assert (m_dreg_interface.debugreg_p ());
   2700 
   2701       clear_wp (inferior_ptid.pid ());
   2702     }
   2703 
   2704   return 0;
   2705 }
   2706 
   2707 /* Clean up the per-process info associated with PID.  When using the
   2708    HWDEBUG interface, we also erase the per-thread state of installed
   2709    debug registers for all the threads that belong to the group of PID.
   2710 
   2711    Usually the thread state is cleaned up by low_delete_thread.  We also
   2712    do it here because low_new_thread is not called for the initial LWP,
   2713    so low_delete_thread won't be able to clean up this state.  */
   2714 
   2715 void
   2716 ppc_linux_nat_target::low_forget_process (pid_t pid)
   2717 {
   2718   if ((!m_dreg_interface.detected_p ())
   2719       || (m_dreg_interface.unavailable_p ()))
   2720     return;
   2721 
   2722   ptid_t pid_ptid (pid, 0, 0);
   2723 
   2724   m_process_info.erase (pid);
   2725 
   2726   if (m_dreg_interface.hwdebug_p ())
   2727     {
   2728       for (auto it = m_installed_hw_bps.begin ();
   2729 	   it != m_installed_hw_bps.end ();)
   2730 	{
   2731 	  if (it->first.matches (pid_ptid))
   2732 	    it = m_installed_hw_bps.erase (it);
   2733 	  else
   2734 	    it++;
   2735 	}
   2736     }
   2737 }
   2738 
   2739 /* Copy the per-process state associated with the pid of PARENT to the
   2740    sate of CHILD_PID.  GDB expects that a forked process will have the
   2741    same hardware breakpoints and watchpoints as the parent.
   2742 
   2743    If we're using the HWDEBUG interface, also copy the thread debug
   2744    register state for the ptid of PARENT to the state for CHILD_PID.
   2745 
   2746    Like for clone events, we assume the kernel will copy the debug
   2747    registers from the parent thread to the child. The
   2748    low_prepare_to_resume function is made to work even if it doesn't.
   2749 
   2750    We copy the thread state here and not in low_new_thread since we don't
   2751    have the pid of the parent in low_new_thread.  Even if we did,
   2752    low_new_thread might not be called immediately after the fork event is
   2753    detected.  For instance, with the checkpointing system (see
   2754    linux-fork.c), the thread won't be added until GDB decides to switch
   2755    to a new checkpointed process.  At that point, the debug register
   2756    state of the parent thread is unlikely to correspond to the state it
   2757    had at the point when it forked.  */
   2758 
   2759 void
   2760 ppc_linux_nat_target::low_new_fork (struct lwp_info *parent,
   2761 				    pid_t child_pid)
   2762 {
   2763   if ((!m_dreg_interface.detected_p ())
   2764       || (m_dreg_interface.unavailable_p ()))
   2765     return;
   2766 
   2767   auto process_it = m_process_info.find (parent->ptid.pid ());
   2768 
   2769   if (process_it != m_process_info.end ())
   2770     m_process_info[child_pid] = m_process_info[parent->ptid.pid ()];
   2771 
   2772   if (m_dreg_interface.hwdebug_p ())
   2773     {
   2774       ptid_t child_ptid (child_pid, child_pid, 0);
   2775 
   2776       copy_thread_dreg_state (parent->ptid, child_ptid);
   2777     }
   2778 }
   2779 
   2780 /* Copy the thread debug register state from the PARENT thread to the the
   2781    state for CHILD_LWP, if we're using the HWDEBUG interface.  We assume
   2782    the kernel copies the debug registers from one thread to another after
   2783    a clone event.  The low_prepare_to_resume function is made to work
   2784    even if it doesn't.  */
   2785 
   2786 void
   2787 ppc_linux_nat_target::low_new_clone (struct lwp_info *parent,
   2788 				     pid_t child_lwp)
   2789 {
   2790   if ((!m_dreg_interface.detected_p ())
   2791       || (m_dreg_interface.unavailable_p ()))
   2792     return;
   2793 
   2794   if (m_dreg_interface.hwdebug_p ())
   2795     {
   2796       ptid_t child_ptid (parent->ptid.pid (), child_lwp, 0);
   2797 
   2798       copy_thread_dreg_state (parent->ptid, child_ptid);
   2799     }
   2800 }
   2801 
   2802 /* Initialize the arch-specific thread state for LP so that it contains
   2803    the ptid for lp, so that we can use it in low_delete_thread.  Mark the
   2804    new thread LP as stale so that we update its debug registers before
   2805    resuming it.  This is not called for the initial thread.  */
   2806 
   2807 void
   2808 ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
   2809 {
   2810   init_arch_lwp_info (lp);
   2811 
   2812   mark_thread_stale (lp);
   2813 }
   2814 
   2815 /* Delete the per-thread debug register stale flag.  */
   2816 
   2817 void
   2818 ppc_linux_nat_target::low_delete_thread (struct arch_lwp_info
   2819 					 *lp_arch_info)
   2820 {
   2821   if (lp_arch_info != NULL)
   2822     {
   2823       if (m_dreg_interface.detected_p ()
   2824 	  && m_dreg_interface.hwdebug_p ())
   2825 	m_installed_hw_bps.erase (lp_arch_info->lwp_ptid);
   2826 
   2827       xfree (lp_arch_info);
   2828     }
   2829 }
   2830 
   2831 /* Install or delete debug registers in thread LP so that it matches what
   2832    GDB requested before it is resumed.  */
   2833 
   2834 void
   2835 ppc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
   2836 {
   2837   if ((!m_dreg_interface.detected_p ())
   2838       || (m_dreg_interface.unavailable_p ()))
   2839     return;
   2840 
   2841   /* We have to re-install or clear the debug registers if we set the
   2842      stale flag.
   2843 
   2844      In addition, some kernels configurations can disable a hardware
   2845      watchpoint after it is hit.  Usually, GDB will remove and re-install
   2846      a hardware watchpoint when the thread stops if "breakpoint
   2847      always-inserted" is off, or to single-step a watchpoint.  But so
   2848      that we don't rely on this behavior, if we stop due to a hardware
   2849      breakpoint or watchpoint, we also refresh our debug registers.  */
   2850 
   2851   arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
   2852 
   2853   bool stale_dregs = (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
   2854 		      || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
   2855 		      || lp_arch_info->debug_regs_stale);
   2856 
   2857   if (!stale_dregs)
   2858     return;
   2859 
   2860   gdb_assert (lp->ptid.lwp_p ());
   2861 
   2862   auto process_it = m_process_info.find (lp->ptid.pid ());
   2863 
   2864   if (m_dreg_interface.hwdebug_p ())
   2865     {
   2866       /* First, delete any hardware watchpoint or breakpoint installed in
   2867 	 the inferior and update the thread state.  */
   2868       auto installed_it = m_installed_hw_bps.find (lp->ptid);
   2869 
   2870       if (installed_it != m_installed_hw_bps.end ())
   2871 	{
   2872 	  auto &bp_list = installed_it->second;
   2873 
   2874 	  for (auto bp_it = bp_list.begin (); bp_it != bp_list.end ();)
   2875 	    {
   2876 	      /* We ignore ENOENT to account for various possible kernel
   2877 		 behaviors, e.g. the kernel might or might not copy debug
   2878 		 registers across forks and clones, and we always copy
   2879 		 the debug register state when fork and clone events are
   2880 		 detected.  */
   2881 	      if (ptrace (PPC_PTRACE_DELHWDEBUG, lp->ptid.lwp (), 0,
   2882 			  bp_it->first) < 0)
   2883 		if (errno != ENOENT)
   2884 		  perror_with_name (_("Error deleting hardware "
   2885 				      "breakpoint or watchpoint"));
   2886 
   2887 	      /* We erase the entries one at a time after successfuly
   2888 		 removing the corresponding slot form the thread so that
   2889 		 if we throw an exception above in a future iteration the
   2890 		 map remains consistent.  */
   2891 	      bp_it = bp_list.erase (bp_it);
   2892 	    }
   2893 
   2894 	  gdb_assert (bp_list.empty ());
   2895 	}
   2896 
   2897       /* Now we install all the requested hardware breakpoints and
   2898 	 watchpoints and update the thread state.  */
   2899 
   2900       if (process_it != m_process_info.end ())
   2901 	{
   2902 	  auto &bp_list = m_installed_hw_bps[lp->ptid];
   2903 
   2904 	  for (ppc_hw_breakpoint bp
   2905 		 : process_it->second.requested_hw_bps)
   2906 	    {
   2907 	      long slot = ptrace (PPC_PTRACE_SETHWDEBUG, lp->ptid.lwp (),
   2908 				  0, &bp);
   2909 
   2910 	      if (slot < 0)
   2911 		perror_with_name (_("Error setting hardware "
   2912 				    "breakpoint or watchpoint"));
   2913 
   2914 	      /* Keep track of which slots we installed in this
   2915 		 thread.  */
   2916 	      bp_list.emplace (bp_list.begin (), slot, bp);
   2917 	    }
   2918 	}
   2919     }
   2920   else
   2921     {
   2922       gdb_assert (m_dreg_interface.debugreg_p ());
   2923 
   2924       /* Passing 0 to PTRACE_SET_DEBUGREG will clear the watchpoint.  We
   2925 	 always clear the watchpoint instead of just overwriting it, in
   2926 	 case there is a request for a new watchpoint, because on some
   2927 	 older kernel versions and configurations simply overwriting the
   2928 	 watchpoint after it was hit would not re-enable it.  */
   2929       if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, 0) < 0)
   2930 	perror_with_name (_("Error clearing hardware watchpoint"));
   2931 
   2932       /* GDB requested a watchpoint to be installed.  */
   2933       if (process_it != m_process_info.end ()
   2934 	  && process_it->second.requested_wp_val.has_value ())
   2935 	{
   2936 	  long wp = *(process_it->second.requested_wp_val);
   2937 
   2938 	  if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, wp) < 0)
   2939 	    perror_with_name (_("Error setting hardware watchpoint"));
   2940 	}
   2941     }
   2942 
   2943   lp_arch_info->debug_regs_stale = false;
   2944 }
   2945 
   2946 /* Return true if INFERIOR_PTID is known to have been stopped by a
   2947    hardware watchpoint, false otherwise.  If true is returned, write the
   2948    address that the kernel reported as causing the SIGTRAP in ADDR_P.  */
   2949 
   2950 bool
   2951 ppc_linux_nat_target::low_stopped_data_address (CORE_ADDR *addr_p)
   2952 {
   2953   siginfo_t siginfo;
   2954 
   2955   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
   2956     return false;
   2957 
   2958   if (siginfo.si_signo != SIGTRAP
   2959       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
   2960     return false;
   2961 
   2962   gdb_assert (!m_dreg_interface.unavailable_p ());
   2963 
   2964   /* Check if this signal corresponds to a hardware breakpoint.  We only
   2965      need to check this if we're using the HWDEBUG interface, since the
   2966      DEBUGREG interface only allows setting one hardware watchpoint.  */
   2967   if (m_dreg_interface.hwdebug_p ())
   2968     {
   2969       /* The index (or slot) of the *point is passed in the si_errno
   2970 	 field.  Currently, this is only the case if the kernel was
   2971 	 configured with CONFIG_PPC_ADV_DEBUG_REGS.  If not, we assume
   2972 	 the kernel will set si_errno to a value that doesn't correspond
   2973 	 to any real slot.  */
   2974       int slot = siginfo.si_errno;
   2975 
   2976       auto installed_it = m_installed_hw_bps.find (inferior_ptid);
   2977 
   2978       /* We must have installed slots for the thread if it got a
   2979 	 TRAP_HWBKPT signal.  */
   2980       gdb_assert (installed_it != m_installed_hw_bps.end ());
   2981 
   2982       for (const auto & slot_bp_pair : installed_it->second)
   2983 	if (slot_bp_pair.first == slot
   2984 	    && (slot_bp_pair.second.trigger_type
   2985 		== PPC_BREAKPOINT_TRIGGER_EXECUTE))
   2986 	  return false;
   2987     }
   2988 
   2989   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
   2990   return true;
   2991 }
   2992 
   2993 /* Return true if INFERIOR_PTID is known to have been stopped by a
   2994    hardware watchpoint, false otherwise.  */
   2995 
   2996 bool
   2997 ppc_linux_nat_target::low_stopped_by_watchpoint ()
   2998 {
   2999   CORE_ADDR addr;
   3000   return low_stopped_data_address (&addr);
   3001 }
   3002 
   3003 bool
   3004 ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
   3005 						    CORE_ADDR start,
   3006 						    int length)
   3007 {
   3008   gdb_assert (!m_dreg_interface.unavailable_p ());
   3009 
   3010   int mask;
   3011 
   3012   if (m_dreg_interface.hwdebug_p ()
   3013       && (linux_get_hwcap () & PPC_FEATURE_BOOKE))
   3014     return start <= addr && start + length >= addr;
   3015   else if (linux_get_hwcap () & PPC_FEATURE_BOOKE)
   3016     mask = 3;
   3017   else
   3018     mask = 7;
   3019 
   3020   addr &= ~mask;
   3021 
   3022   /* Check whether [start, start+length-1] intersects [addr, addr+mask].  */
   3023   return start <= addr + mask && start + length - 1 >= addr;
   3024 }
   3025 
   3026 /* Return the number of registers needed for a masked hardware watchpoint.  */
   3027 
   3028 int
   3029 ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr,
   3030 						  CORE_ADDR mask)
   3031 {
   3032   m_dreg_interface.detect (inferior_ptid);
   3033 
   3034   if (!m_dreg_interface.hwdebug_p ()
   3035       || (m_dreg_interface.hwdebug_info ().features
   3036 	  & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
   3037     return -1;
   3038   else if ((mask & 0xC0000000) != 0xC0000000)
   3039     {
   3040       warning (_("The given mask covers kernel address space "
   3041 		 "and cannot be used.\n"));
   3042 
   3043       return -2;
   3044     }
   3045   else
   3046     return 2;
   3047 }
   3048 
   3049 /* Copy the per-thread debug register state, if any, from thread
   3050    PARENT_PTID to thread CHILD_PTID, if the debug register being used is
   3051    HWDEBUG.  */
   3052 
   3053 void
   3054 ppc_linux_nat_target::copy_thread_dreg_state (const ptid_t &parent_ptid,
   3055 					      const ptid_t &child_ptid)
   3056 {
   3057   gdb_assert (m_dreg_interface.hwdebug_p ());
   3058 
   3059   auto installed_it = m_installed_hw_bps.find (parent_ptid);
   3060 
   3061   if (installed_it != m_installed_hw_bps.end ())
   3062     m_installed_hw_bps[child_ptid] = m_installed_hw_bps[parent_ptid];
   3063 }
   3064 
   3065 /* Mark the debug register stale flag for the new thread, if we have
   3066    already detected which debug register interface we use.  */
   3067 
   3068 void
   3069 ppc_linux_nat_target::mark_thread_stale (struct lwp_info *lp)
   3070 {
   3071   if ((!m_dreg_interface.detected_p ())
   3072       || (m_dreg_interface.unavailable_p ()))
   3073     return;
   3074 
   3075   arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
   3076 
   3077   lp_arch_info->debug_regs_stale = true;
   3078 }
   3079 
   3080 /* Mark all the threads of the group of PID as stale with respect to
   3081    debug registers and issue a stop request to each such thread that
   3082    isn't already stopped.  */
   3083 
   3084 void
   3085 ppc_linux_nat_target::mark_debug_registers_changed (pid_t pid)
   3086 {
   3087   /* We do this in two passes to make sure all threads are marked even if
   3088      we get an exception when stopping one of them.  */
   3089 
   3090   iterate_over_lwps (ptid_t (pid),
   3091 		     [this] (struct lwp_info *lp) -> int {
   3092 		       this->mark_thread_stale (lp);
   3093 		       return 0;
   3094 		     });
   3095 
   3096   iterate_over_lwps (ptid_t (pid),
   3097 		     [] (struct lwp_info *lp) -> int {
   3098 		       if (!lwp_is_stopped (lp))
   3099 			 linux_stop_lwp (lp);
   3100 		       return 0;
   3101 		     });
   3102 }
   3103 
   3104 /* Register a hardware breakpoint or watchpoint BP for the pid PID, then
   3105    mark the stale flag for all threads of the group of PID, and issue a
   3106    stop request for them.  The breakpoint or watchpoint will be installed
   3107    the next time each thread is resumed.  Should only be used if the
   3108    debug register interface is HWDEBUG.  */
   3109 
   3110 void
   3111 ppc_linux_nat_target::register_hw_breakpoint (pid_t pid,
   3112 					      const struct
   3113 					      ppc_hw_breakpoint &bp)
   3114 {
   3115   gdb_assert (m_dreg_interface.hwdebug_p ());
   3116 
   3117   m_process_info[pid].requested_hw_bps.push_back (bp);
   3118 
   3119   mark_debug_registers_changed (pid);
   3120 }
   3121 
   3122 /* Clear a registration for a hardware breakpoint or watchpoint BP for
   3123    the pid PID, then mark the stale flag for all threads of the group of
   3124    PID, and issue a stop request for them.  The breakpoint or watchpoint
   3125    will be removed the next time each thread is resumed.  Should only be
   3126    used if the debug register interface is HWDEBUG.  */
   3127 
   3128 void
   3129 ppc_linux_nat_target::clear_hw_breakpoint (pid_t pid,
   3130 					   const struct ppc_hw_breakpoint &bp)
   3131 {
   3132   gdb_assert (m_dreg_interface.hwdebug_p ());
   3133 
   3134   auto process_it = m_process_info.find (pid);
   3135 
   3136   gdb_assert (process_it != m_process_info.end ());
   3137 
   3138   auto bp_it = std::find_if (process_it->second.requested_hw_bps.begin (),
   3139 			     process_it->second.requested_hw_bps.end (),
   3140 			     [&bp, this]
   3141 			     (const struct ppc_hw_breakpoint &curr)
   3142 			     { return hwdebug_point_cmp (bp, curr); }
   3143 			     );
   3144 
   3145   /* If GDB is removing a watchpoint, it must have been inserted.  */
   3146   gdb_assert (bp_it != process_it->second.requested_hw_bps.end ());
   3147 
   3148   process_it->second.requested_hw_bps.erase (bp_it);
   3149 
   3150   mark_debug_registers_changed (pid);
   3151 }
   3152 
   3153 /* Register the hardware watchpoint value WP_VALUE for the pid PID,
   3154    then mark the stale flag for all threads of the group of PID, and
   3155    issue a stop request for them.  The breakpoint or watchpoint will be
   3156    installed the next time each thread is resumed.  Should only be used
   3157    if the debug register interface is DEBUGREG.  */
   3158 
   3159 void
   3160 ppc_linux_nat_target::register_wp (pid_t pid, long wp_value)
   3161 {
   3162   gdb_assert (m_dreg_interface.debugreg_p ());
   3163 
   3164   /* Our other functions should have told GDB that we only have one
   3165      hardware watchpoint with this interface.  */
   3166   gdb_assert (!m_process_info[pid].requested_wp_val.has_value ());
   3167 
   3168   m_process_info[pid].requested_wp_val.emplace (wp_value);
   3169 
   3170   mark_debug_registers_changed (pid);
   3171 }
   3172 
   3173 /* Clear the hardware watchpoint registration for the pid PID, then mark
   3174    the stale flag for all threads of the group of PID, and issue a stop
   3175    request for them.  The breakpoint or watchpoint will be installed the
   3176    next time each thread is resumed.  Should only be used if the debug
   3177    register interface is DEBUGREG.  */
   3178 
   3179 void
   3180 ppc_linux_nat_target::clear_wp (pid_t pid)
   3181 {
   3182   gdb_assert (m_dreg_interface.debugreg_p ());
   3183 
   3184   auto process_it = m_process_info.find (pid);
   3185 
   3186   gdb_assert (process_it != m_process_info.end ());
   3187   gdb_assert (process_it->second.requested_wp_val.has_value ());
   3188 
   3189   process_it->second.requested_wp_val.reset ();
   3190 
   3191   mark_debug_registers_changed (pid);
   3192 }
   3193 
   3194 /* Initialize the arch-specific thread state for LWP, if it not already
   3195    created.  */
   3196 
   3197 void
   3198 ppc_linux_nat_target::init_arch_lwp_info (struct lwp_info *lp)
   3199 {
   3200   if (lwp_arch_private_info (lp) == NULL)
   3201     {
   3202       lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
   3203       lwp_arch_private_info (lp)->debug_regs_stale = false;
   3204       lwp_arch_private_info (lp)->lwp_ptid = lp->ptid;
   3205     }
   3206 }
   3207 
   3208 /* Get the arch-specific thread state for LWP, creating it if
   3209    necessary.  */
   3210 
   3211 arch_lwp_info *
   3212 ppc_linux_nat_target::get_arch_lwp_info (struct lwp_info *lp)
   3213 {
   3214   init_arch_lwp_info (lp);
   3215 
   3216   return lwp_arch_private_info (lp);
   3217 }
   3218 
   3219 void _initialize_ppc_linux_nat ();
   3220 void
   3221 _initialize_ppc_linux_nat ()
   3222 {
   3223   linux_target = &the_ppc_linux_nat_target;
   3224 
   3225   /* Register the target.  */
   3226   add_inf_child_target (linux_target);
   3227 }
   3228