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