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