gdb/ppc-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* PPC GNU/Linux native support.

  2.    Copyright (C) 1988-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "observer.h"
  16. #include "frame.h"
  17. #include "inferior.h"
  18. #include "gdbthread.h"
  19. #include "gdbcore.h"
  20. #include "regcache.h"
  21. #include "target.h"
  22. #include "linux-nat.h"

  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #include <signal.h>
  26. #include <sys/user.h>
  27. #include <sys/ioctl.h>
  28. #include "gdb_wait.h"
  29. #include <fcntl.h>
  30. #include <sys/procfs.h>
  31. #include <sys/ptrace.h>

  32. /* Prototypes for supply_gregset etc.  */
  33. #include "gregset.h"
  34. #include "ppc-tdep.h"
  35. #include "ppc-linux-tdep.h"

  36. /* Required when using the AUXV.  */
  37. #include "elf/common.h"
  38. #include "auxv.h"

  39. /* This sometimes isn't defined.  */
  40. #ifndef PT_ORIG_R3
  41. #define PT_ORIG_R3 34
  42. #endif
  43. #ifndef PT_TRAP
  44. #define PT_TRAP 40
  45. #endif

  46. /* The PPC_FEATURE_* defines should be provided by <asm/cputable.h>.
  47.    If they aren't, we can provide them ourselves (their values are fixed
  48.    because they are part of the kernel ABI).  They are used in the AT_HWCAP
  49.    entry of the AUXV.  */
  50. #ifndef PPC_FEATURE_CELL
  51. #define PPC_FEATURE_CELL 0x00010000
  52. #endif
  53. #ifndef PPC_FEATURE_BOOKE
  54. #define PPC_FEATURE_BOOKE 0x00008000
  55. #endif
  56. #ifndef PPC_FEATURE_HAS_DFP
  57. #define PPC_FEATURE_HAS_DFP        0x00000400  /* Decimal Floating Point.  */
  58. #endif

  59. /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
  60.    configure time check.  Some older glibc's (for instance 2.2.1)
  61.    don't have a specific powerpc version of ptrace.h, and fall back on
  62.    a generic one.  In such cases, sys/ptrace.h defines
  63.    PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
  64.    ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
  65.    PTRACE_SETVRREGS to be.  This also makes a configury check pretty
  66.    much useless.  */

  67. /* These definitions should really come from the glibc header files,
  68.    but Glibc doesn't know about the vrregs yet.  */
  69. #ifndef PTRACE_GETVRREGS
  70. #define PTRACE_GETVRREGS 18
  71. #define PTRACE_SETVRREGS 19
  72. #endif

  73. /* PTRACE requests for POWER7 VSX registers.  */
  74. #ifndef PTRACE_GETVSXREGS
  75. #define PTRACE_GETVSXREGS 27
  76. #define PTRACE_SETVSXREGS 28
  77. #endif

  78. /* Similarly for the ptrace requests for getting / setting the SPE
  79.    registers (ev0 -- ev31, acc, and spefscr).  See the description of
  80.    gdb_evrregset_t for details.  */
  81. #ifndef PTRACE_GETEVRREGS
  82. #define PTRACE_GETEVRREGS 20
  83. #define PTRACE_SETEVRREGS 21
  84. #endif

  85. /* Similarly for the hardware watchpoint support.  These requests are used
  86.    when the PowerPC HWDEBUG ptrace interface is not available.  */
  87. #ifndef PTRACE_GET_DEBUGREG
  88. #define PTRACE_GET_DEBUGREG    25
  89. #endif
  90. #ifndef PTRACE_SET_DEBUGREG
  91. #define PTRACE_SET_DEBUGREG    26
  92. #endif
  93. #ifndef PTRACE_GETSIGINFO
  94. #define PTRACE_GETSIGINFO    0x4202
  95. #endif

  96. /* These requests are used when the PowerPC HWDEBUG ptrace interface is
  97.    available.  It exposes the debug facilities of PowerPC processors, as well
  98.    as additional features of BookE processors, such as ranged breakpoints and
  99.    watchpoints and hardware-accelerated condition evaluation.  */
  100. #ifndef PPC_PTRACE_GETHWDBGINFO

  101. /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG
  102.    ptrace interface is not present in ptrace.h, so we'll have to pretty much
  103.    include it all here so that the code at least compiles on older systems.  */
  104. #define PPC_PTRACE_GETHWDBGINFO 0x89
  105. #define PPC_PTRACE_SETHWDEBUG   0x88
  106. #define PPC_PTRACE_DELHWDEBUG   0x87

  107. struct ppc_debug_info
  108. {
  109.         uint32_t version;               /* Only version 1 exists to date.  */
  110.         uint32_t num_instruction_bps;
  111.         uint32_t num_data_bps;
  112.         uint32_t num_condition_regs;
  113.         uint32_t data_bp_alignment;
  114.         uint32_t sizeof_condition;      /* size of the DVC register.  */
  115.         uint64_t features;
  116. };

  117. /* Features will have bits indicating whether there is support for:  */
  118. #define PPC_DEBUG_FEATURE_INSN_BP_RANGE         0x1
  119. #define PPC_DEBUG_FEATURE_INSN_BP_MASK          0x2
  120. #define PPC_DEBUG_FEATURE_DATA_BP_RANGE         0x4
  121. #define PPC_DEBUG_FEATURE_DATA_BP_MASK          0x8

  122. struct ppc_hw_breakpoint
  123. {
  124.         uint32_t version;               /* currently, version must be 1 */
  125.         uint32_t trigger_type;          /* only some combinations allowed */
  126.         uint32_t addr_mode;             /* address match mode */
  127.         uint32_t condition_mode;        /* break/watchpoint condition flags */
  128.         uint64_t addr;                  /* break/watchpoint address */
  129.         uint64_t addr2;                 /* range end or mask */
  130.         uint64_t condition_value;       /* contents of the DVC register */
  131. };

  132. /* Trigger type.  */
  133. #define PPC_BREAKPOINT_TRIGGER_EXECUTE  0x1
  134. #define PPC_BREAKPOINT_TRIGGER_READ     0x2
  135. #define PPC_BREAKPOINT_TRIGGER_WRITE    0x4
  136. #define PPC_BREAKPOINT_TRIGGER_RW       0x6

  137. /* Address mode.  */
  138. #define PPC_BREAKPOINT_MODE_EXACT               0x0
  139. #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE     0x1
  140. #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE     0x2
  141. #define PPC_BREAKPOINT_MODE_MASK                0x3

  142. /* Condition mode.  */
  143. #define PPC_BREAKPOINT_CONDITION_NONE   0x0
  144. #define PPC_BREAKPOINT_CONDITION_AND    0x1
  145. #define PPC_BREAKPOINT_CONDITION_EXACT  0x1
  146. #define PPC_BREAKPOINT_CONDITION_OR     0x2
  147. #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
  148. #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
  149. #define PPC_BREAKPOINT_CONDITION_BE_SHIFT       16
  150. #define PPC_BREAKPOINT_CONDITION_BE(n)  \
  151.         (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
  152. #endif /* PPC_PTRACE_GETHWDBGINFO */

  153. /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
  154.    watchpoint (up to 512 bytes).  */
  155. #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
  156. #define PPC_DEBUG_FEATURE_DATA_BP_DAWR        0x10
  157. #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */

  158. /* Similarly for the general-purpose (gp0 -- gp31)
  159.    and floating-point registers (fp0 -- fp31).  */
  160. #ifndef PTRACE_GETREGS
  161. #define PTRACE_GETREGS 12
  162. #endif
  163. #ifndef PTRACE_SETREGS
  164. #define PTRACE_SETREGS 13
  165. #endif
  166. #ifndef PTRACE_GETFPREGS
  167. #define PTRACE_GETFPREGS 14
  168. #endif
  169. #ifndef PTRACE_SETFPREGS
  170. #define PTRACE_SETFPREGS 15
  171. #endif

  172. /* This oddity is because the Linux kernel defines elf_vrregset_t as
  173.    an array of 33 16 bytes long elements.  I.e. it leaves out vrsave.
  174.    However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
  175.    the vrsave as an extra 4 bytes at the endI opted for creating a
  176.    flat array of chars, so that it is easier to manipulate for gdb.

  177.    There are 32 vector registers 16 bytes longs, plus a VSCR register
  178.    which is only 4 bytes long, but is fetched as a 16 bytes
  179.    quantity.  Up to here we have the elf_vrregset_t structure.
  180.    Appended to this there is space for the VRSAVE register: 4 bytes.
  181.    Even though this vrsave register is not included in the regset
  182.    typedef, it is handled by the ptrace requests.

  183.    Note that GNU/Linux doesn't support little endian PPC hardware,
  184.    therefore the offset at which the real value of the VSCR register
  185.    is located will be always 12 bytes.

  186.    The layout is like this (where x is the actual value of the vscr reg): */

  187. /* *INDENT-OFF* */
  188. /*
  189.    |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
  190.    <------->     <-------><-------><->
  191.      VR0           VR31     VSCR    VRSAVE
  192. */
  193. /* *INDENT-ON* */

  194. #define SIZEOF_VRREGS 33*16+4

  195. typedef char gdb_vrregset_t[SIZEOF_VRREGS];

  196. /* This is the layout of the POWER7 VSX registers and the way they overlap
  197.    with the existing FPR and VMX registers.

  198.                     VSR doubleword 0               VSR doubleword 1
  199.            ----------------------------------------------------------------
  200.    VSR[0]  |             FPR[0]            |                              |
  201.            ----------------------------------------------------------------
  202.    VSR[1]  |             FPR[1]            |                              |
  203.            ----------------------------------------------------------------
  204.            |              ...              |                              |
  205.            |              ...              |                              |
  206.            ----------------------------------------------------------------
  207.    VSR[30] |             FPR[30]           |                              |
  208.            ----------------------------------------------------------------
  209.    VSR[31] |             FPR[31]           |                              |
  210.            ----------------------------------------------------------------
  211.    VSR[32] |                             VR[0]                            |
  212.            ----------------------------------------------------------------
  213.    VSR[33] |                             VR[1]                            |
  214.            ----------------------------------------------------------------
  215.            |                              ...                             |
  216.            |                              ...                             |
  217.            ----------------------------------------------------------------
  218.    VSR[62] |                             VR[30]                           |
  219.            ----------------------------------------------------------------
  220.    VSR[63] |                             VR[31]                           |
  221.           ----------------------------------------------------------------

  222.    VSX has 64 128bit registers.  The first 32 registers overlap with
  223.    the FP registers (doubleword 0) and hence extend them with additional
  224.    64 bits (doubleword 1).  The other 32 regs overlap with the VMX
  225.    registers.  */
  226. #define SIZEOF_VSXREGS 32*8

  227. typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];

  228. /* On PPC processors that support the Signal Processing Extension
  229.    (SPE) APU, the general-purpose registers are 64 bits long.
  230.    However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
  231.    ptrace calls only access the lower half of each register, to allow
  232.    them to behave the same way they do on non-SPE systems.  There's a
  233.    separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
  234.    read and write the top halves of all the general-purpose registers
  235.    at once, along with some SPE-specific registers.

  236.    GDB itself continues to claim the general-purpose registers are 32
  237.    bits long.  It has unnamed raw registers that hold the upper halves
  238.    of the gprs, and the full 64-bit SIMD views of the registers,
  239.    'ev0' -- 'ev31', are pseudo-registers that splice the top and
  240.    bottom halves together.

  241.    This is the structure filled in by PTRACE_GETEVRREGS and written to
  242.    the inferior's registers by PTRACE_SETEVRREGS.  */
  243. struct gdb_evrregset_t
  244. {
  245.   unsigned long evr[32];
  246.   unsigned long long acc;
  247.   unsigned long spefscr;
  248. };

  249. /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
  250.    PTRACE_SETVSXREGS requests, for reading and writing the VSX
  251.    POWER7 registers 0 through 31.  Zero if we've tried one of them and
  252.    gotten an error.  Note that VSX registers 32 through 63 overlap
  253.    with VR registers 0 through 31.  */
  254. int have_ptrace_getsetvsxregs = 1;

  255. /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
  256.    PTRACE_SETVRREGS requests, for reading and writing the Altivec
  257.    registers.  Zero if we've tried one of them and gotten an
  258.    error.  */
  259. int have_ptrace_getvrregs = 1;

  260. /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
  261.    PTRACE_SETEVRREGS requests, for reading and writing the SPE
  262.    registers.  Zero if we've tried one of them and gotten an
  263.    error.  */
  264. int have_ptrace_getsetevrregs = 1;

  265. /* Non-zero if our kernel may support the PTRACE_GETREGS and
  266.    PTRACE_SETREGS requests, for reading and writing the
  267.    general-purpose registers.  Zero if we've tried one of
  268.    them and gotten an error.  */
  269. int have_ptrace_getsetregs = 1;

  270. /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
  271.    PTRACE_SETFPREGS requests, for reading and writing the
  272.    floating-pointers registers.  Zero if we've tried one of
  273.    them and gotten an error.  */
  274. int have_ptrace_getsetfpregs = 1;

  275. /* *INDENT-OFF* */
  276. /* registers layout, as presented by the ptrace interface:
  277. PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
  278. PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
  279. PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
  280. PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
  281. PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
  282. PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
  283. PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
  284. PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
  285. PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
  286. PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
  287. PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
  288. PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
  289. PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
  290. /* *INDENT_ON * */

  291. static int
  292. ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
  293. {
  294.   int u_addr = -1;
  295.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  296.   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
  297.      interface, and not the wordsize of the program's ABI.  */
  298.   int wordsize = sizeof (long);

  299.   /* General purpose registers occupy 1 slot each in the buffer.  */
  300.   if (regno >= tdep->ppc_gp0_regnum
  301.       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
  302.     u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);

  303.   /* Floating point regs: eight bytes each in both 32- and 64-bit
  304.      ptrace interfaces.  Thus, two slots each in 32-bit interface, one
  305.      slot each in 64-bit interface.  */
  306.   if (tdep->ppc_fp0_regnum >= 0
  307.       && regno >= tdep->ppc_fp0_regnum
  308.       && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
  309.     u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);

  310.   /* UISA special purpose registers: 1 slot each.  */
  311.   if (regno == gdbarch_pc_regnum (gdbarch))
  312.     u_addr = PT_NIP * wordsize;
  313.   if (regno == tdep->ppc_lr_regnum)
  314.     u_addr = PT_LNK * wordsize;
  315.   if (regno == tdep->ppc_cr_regnum)
  316.     u_addr = PT_CCR * wordsize;
  317.   if (regno == tdep->ppc_xer_regnum)
  318.     u_addr = PT_XER * wordsize;
  319.   if (regno == tdep->ppc_ctr_regnum)
  320.     u_addr = PT_CTR * wordsize;
  321. #ifdef PT_MQ
  322.   if (regno == tdep->ppc_mq_regnum)
  323.     u_addr = PT_MQ * wordsize;
  324. #endif
  325.   if (regno == tdep->ppc_ps_regnum)
  326.     u_addr = PT_MSR * wordsize;
  327.   if (regno == PPC_ORIG_R3_REGNUM)
  328.     u_addr = PT_ORIG_R3 * wordsize;
  329.   if (regno == PPC_TRAP_REGNUM)
  330.     u_addr = PT_TRAP * wordsize;
  331.   if (tdep->ppc_fpscr_regnum >= 0
  332.       && regno == tdep->ppc_fpscr_regnum)
  333.     {
  334.       /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
  335.          kernel headers incorrectly contained the 32-bit definition of
  336.          PT_FPSCR.  For the 32-bit definition, floating-point
  337.          registers occupy two 32-bit "slots", and the FPSCR lives in
  338.          the second half of such a slot-pair (hence +1).  For 64-bit,
  339.          the FPSCR instead occupies the full 64-bit 2-word-slot and
  340.          hence no adjustment is necessary.  Hack around this.  */
  341.       if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
  342.         u_addr = (48 + 32) * wordsize;
  343.       /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
  344.          slot and not just its second word.  The PT_FPSCR supplied when
  345.          GDB is compiled as a 32-bit app doesn't reflect this.  */
  346.       else if (wordsize == 4 && register_size (gdbarch, regno) == 8
  347.                && PT_FPSCR == (48 + 2*32 + 1))
  348.         u_addr = (48 + 2*32) * wordsize;
  349.       else
  350.         u_addr = PT_FPSCR * wordsize;
  351.     }
  352.   return u_addr;
  353. }

  354. /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
  355.    registers set mechanism, as opposed to the interface for all the
  356.    other registers, that stores/fetches each register individually.  */
  357. static void
  358. fetch_vsx_register (struct regcache *regcache, int tid, int regno)
  359. {
  360.   int ret;
  361.   gdb_vsxregset_t regs;
  362.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  363.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  364.   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);

  365.   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  366.   if (ret < 0)
  367.     {
  368.       if (errno == EIO)
  369.         {
  370.           have_ptrace_getsetvsxregs = 0;
  371.           return;
  372.         }
  373.       perror_with_name (_("Unable to fetch VSX register"));
  374.     }

  375.   regcache_raw_supply (regcache, regno,
  376.                        regs + (regno - tdep->ppc_vsr0_upper_regnum)
  377.                        * vsxregsize);
  378. }

  379. /* The Linux kernel ptrace interface for AltiVec registers uses the
  380.    registers set mechanism, as opposed to the interface for all the
  381.    other registers, that stores/fetches each register individually.  */
  382. static void
  383. fetch_altivec_register (struct regcache *regcache, int tid, int regno)
  384. {
  385.   int ret;
  386.   int offset = 0;
  387.   gdb_vrregset_t regs;
  388.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  389.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  390.   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);

  391.   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  392.   if (ret < 0)
  393.     {
  394.       if (errno == EIO)
  395.         {
  396.           have_ptrace_getvrregs = 0;
  397.           return;
  398.         }
  399.       perror_with_name (_("Unable to fetch AltiVec register"));
  400.     }

  401.   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
  402.      long on the hardware.  We deal only with the lower 4 bytes of the
  403.      vector.  VRSAVE is at the end of the array in a 4 bytes slot, so
  404.      there is no need to define an offset for it.  */
  405.   if (regno == (tdep->ppc_vrsave_regnum - 1))
  406.     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);

  407.   regcache_raw_supply (regcache, regno,
  408.                        regs + (regno
  409.                                - tdep->ppc_vr0_regnum) * vrregsize + offset);
  410. }

  411. /* Fetch the top 32 bits of TID's general-purpose registers and the
  412.    SPE-specific registers, and place the results in EVRREGSET.  If we
  413.    don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
  414.    zeros.

  415.    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
  416.    PTRACE_SETEVRREGS requests are supported is isolated here, and in
  417.    set_spe_registers.  */
  418. static void
  419. get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
  420. {
  421.   if (have_ptrace_getsetevrregs)
  422.     {
  423.       if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
  424.         return;
  425.       else
  426.         {
  427.           /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
  428.              we just return zeros.  */
  429.           if (errno == EIO)
  430.             have_ptrace_getsetevrregs = 0;
  431.           else
  432.             /* Anything else needs to be reported.  */
  433.             perror_with_name (_("Unable to fetch SPE registers"));
  434.         }
  435.     }

  436.   memset (evrregset, 0, sizeof (*evrregset));
  437. }

  438. /* Supply values from TID for SPE-specific raw registers: the upper
  439.    halves of the GPRs, the accumulator, and the spefscr.  REGNO must
  440.    be the number of an upper half register, acc, spefscr, or -1 to
  441.    supply the values of all registers.  */
  442. static void
  443. fetch_spe_register (struct regcache *regcache, int tid, int regno)
  444. {
  445.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  446.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  447.   struct gdb_evrregset_t evrregs;

  448.   gdb_assert (sizeof (evrregs.evr[0])
  449.               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
  450.   gdb_assert (sizeof (evrregs.acc)
  451.               == register_size (gdbarch, tdep->ppc_acc_regnum));
  452.   gdb_assert (sizeof (evrregs.spefscr)
  453.               == register_size (gdbarch, tdep->ppc_spefscr_regnum));

  454.   get_spe_registers (tid, &evrregs);

  455.   if (regno == -1)
  456.     {
  457.       int i;

  458.       for (i = 0; i < ppc_num_gprs; i++)
  459.         regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
  460.                              &evrregs.evr[i]);
  461.     }
  462.   else if (tdep->ppc_ev0_upper_regnum <= regno
  463.            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
  464.     regcache_raw_supply (regcache, regno,
  465.                          &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);

  466.   if (regno == -1
  467.       || regno == tdep->ppc_acc_regnum)
  468.     regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);

  469.   if (regno == -1
  470.       || regno == tdep->ppc_spefscr_regnum)
  471.     regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
  472.                          &evrregs.spefscr);
  473. }

  474. static void
  475. fetch_register (struct regcache *regcache, int tid, int regno)
  476. {
  477.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  478.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  479.   /* This isn't really an address.  But ptrace thinks of it as one.  */
  480.   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
  481.   int bytes_transferred;
  482.   unsigned int offset;         /* Offset of registers within the u area.  */
  483.   gdb_byte buf[MAX_REGISTER_SIZE];

  484.   if (altivec_register_p (gdbarch, regno))
  485.     {
  486.       /* If this is the first time through, or if it is not the first
  487.          time through, and we have comfirmed that there is kernel
  488.          support for such a ptrace request, then go and fetch the
  489.          register.  */
  490.       if (have_ptrace_getvrregs)
  491.        {
  492.          fetch_altivec_register (regcache, tid, regno);
  493.          return;
  494.        }
  495.      /* If we have discovered that there is no ptrace support for
  496.         AltiVec registers, fall through and return zeroes, because
  497.         regaddr will be -1 in this case.  */
  498.     }
  499.   if (vsx_register_p (gdbarch, regno))
  500.     {
  501.       if (have_ptrace_getsetvsxregs)
  502.         {
  503.           fetch_vsx_register (regcache, tid, regno);
  504.           return;
  505.         }
  506.     }
  507.   else if (spe_register_p (gdbarch, regno))
  508.     {
  509.       fetch_spe_register (regcache, tid, regno);
  510.       return;
  511.     }

  512.   if (regaddr == -1)
  513.     {
  514.       memset (buf, '\0', register_size (gdbarch, regno));   /* Supply zeroes */
  515.       regcache_raw_supply (regcache, regno, buf);
  516.       return;
  517.     }

  518.   /* Read the raw register using sizeof(long) sized chunks.  On a
  519.      32-bit platform, 64-bit floating-point registers will require two
  520.      transfers.  */
  521.   for (bytes_transferred = 0;
  522.        bytes_transferred < register_size (gdbarch, regno);
  523.        bytes_transferred += sizeof (long))
  524.     {
  525.       long l;

  526.       errno = 0;
  527.       l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
  528.       regaddr += sizeof (long);
  529.       if (errno != 0)
  530.         {
  531.           char message[128];
  532.           xsnprintf (message, sizeof (message), "reading register %s (#%d)",
  533.                      gdbarch_register_name (gdbarch, regno), regno);
  534.           perror_with_name (message);
  535.         }
  536.       memcpy (&buf[bytes_transferred], &l, sizeof (l));
  537.     }

  538.   /* Now supply the register.  Keep in mind that the regcache's idea
  539.      of the register's size may not be a multiple of sizeof
  540.      (long).  */
  541.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  542.     {
  543.       /* Little-endian values are always found at the left end of the
  544.          bytes transferred.  */
  545.       regcache_raw_supply (regcache, regno, buf);
  546.     }
  547.   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  548.     {
  549.       /* Big-endian values are found at the right end of the bytes
  550.          transferred.  */
  551.       size_t padding = (bytes_transferred - register_size (gdbarch, regno));
  552.       regcache_raw_supply (regcache, regno, buf + padding);
  553.     }
  554.   else
  555.     internal_error (__FILE__, __LINE__,
  556.                     _("fetch_register: unexpected byte order: %d"),
  557.                     gdbarch_byte_order (gdbarch));
  558. }

  559. static void
  560. supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
  561. {
  562.   int i;
  563.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  564.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  565.   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);

  566.   for (i = 0; i < ppc_num_vshrs; i++)
  567.     {
  568.         regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
  569.                              *vsxregsetp + i * vsxregsize);
  570.     }
  571. }

  572. static void
  573. supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
  574. {
  575.   int i;
  576.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  577.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  578.   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
  579.   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
  580.   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);

  581.   for (i = 0; i < num_of_vrregs; i++)
  582.     {
  583.       /* The last 2 registers of this set are only 32 bit long, not
  584.          128.  However an offset is necessary only for VSCR because it
  585.          occupies a whole vector, while VRSAVE occupies a full 4 bytes
  586.          slot.  */
  587.       if (i == (num_of_vrregs - 2))
  588.         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
  589.                              *vrregsetp + i * vrregsize + offset);
  590.       else
  591.         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
  592.                              *vrregsetp + i * vrregsize);
  593.     }
  594. }

  595. static void
  596. fetch_vsx_registers (struct regcache *regcache, int tid)
  597. {
  598.   int ret;
  599.   gdb_vsxregset_t regs;

  600.   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  601.   if (ret < 0)
  602.     {
  603.       if (errno == EIO)
  604.         {
  605.           have_ptrace_getsetvsxregs = 0;
  606.           return;
  607.         }
  608.       perror_with_name (_("Unable to fetch VSX registers"));
  609.     }
  610.   supply_vsxregset (regcache, &regs);
  611. }

  612. static void
  613. fetch_altivec_registers (struct regcache *regcache, int tid)
  614. {
  615.   int ret;
  616.   gdb_vrregset_t regs;

  617.   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  618.   if (ret < 0)
  619.     {
  620.       if (errno == EIO)
  621.         {
  622.           have_ptrace_getvrregs = 0;
  623.           return;
  624.         }
  625.       perror_with_name (_("Unable to fetch AltiVec registers"));
  626.     }
  627.   supply_vrregset (regcache, &regs);
  628. }

  629. /* This function actually issues the request to ptrace, telling
  630.    it to get all general-purpose registers and put them into the
  631.    specified regset.

  632.    If the ptrace request does not exist, this function returns 0
  633.    and properly sets the have_ptrace_* flag.  If the request fails,
  634.    this function calls perror_with_name.  Otherwise, if the request
  635.    succeeds, then the regcache gets filled and 1 is returned.  */
  636. static int
  637. fetch_all_gp_regs (struct regcache *regcache, int tid)
  638. {
  639.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  640.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  641.   gdb_gregset_t gregset;

  642.   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
  643.     {
  644.       if (errno == EIO)
  645.         {
  646.           have_ptrace_getsetregs = 0;
  647.           return 0;
  648.         }
  649.       perror_with_name (_("Couldn't get general-purpose registers."));
  650.     }

  651.   supply_gregset (regcache, (const gdb_gregset_t *) &gregset);

  652.   return 1;
  653. }

  654. /* This is a wrapper for the fetch_all_gp_regs function.  It is
  655.    responsible for verifying if this target has the ptrace request
  656.    that can be used to fetch all general-purpose registers at one
  657.    shot.  If it doesn't, then we should fetch them using the
  658.    old-fashioned way, which is to iterate over the registers and
  659.    request them one by one.  */
  660. static void
  661. fetch_gp_regs (struct regcache *regcache, int tid)
  662. {
  663.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  664.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  665.   int i;

  666.   if (have_ptrace_getsetregs)
  667.     if (fetch_all_gp_regs (regcache, tid))
  668.       return;

  669.   /* If we've hit this point, it doesn't really matter which
  670.      architecture we are using.  We just need to read the
  671.      registers in the "old-fashioned way".  */
  672.   for (i = 0; i < ppc_num_gprs; i++)
  673.     fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
  674. }

  675. /* This function actually issues the request to ptrace, telling
  676.    it to get all floating-point registers and put them into the
  677.    specified regset.

  678.    If the ptrace request does not exist, this function returns 0
  679.    and properly sets the have_ptrace_* flag.  If the request fails,
  680.    this function calls perror_with_name.  Otherwise, if the request
  681.    succeeds, then the regcache gets filled and 1 is returned.  */
  682. static int
  683. fetch_all_fp_regs (struct regcache *regcache, int tid)
  684. {
  685.   gdb_fpregset_t fpregs;

  686.   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
  687.     {
  688.       if (errno == EIO)
  689.         {
  690.           have_ptrace_getsetfpregs = 0;
  691.           return 0;
  692.         }
  693.       perror_with_name (_("Couldn't get floating-point registers."));
  694.     }

  695.   supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);

  696.   return 1;
  697. }

  698. /* This is a wrapper for the fetch_all_fp_regs function.  It is
  699.    responsible for verifying if this target has the ptrace request
  700.    that can be used to fetch all floating-point registers at one
  701.    shot.  If it doesn't, then we should fetch them using the
  702.    old-fashioned way, which is to iterate over the registers and
  703.    request them one by one.  */
  704. static void
  705. fetch_fp_regs (struct regcache *regcache, int tid)
  706. {
  707.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  708.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  709.   int i;

  710.   if (have_ptrace_getsetfpregs)
  711.     if (fetch_all_fp_regs (regcache, tid))
  712.       return;

  713.   /* If we've hit this point, it doesn't really matter which
  714.      architecture we are using.  We just need to read the
  715.      registers in the "old-fashioned way".  */
  716.   for (i = 0; i < ppc_num_fprs; i++)
  717.     fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
  718. }

  719. static void
  720. fetch_ppc_registers (struct regcache *regcache, int tid)
  721. {
  722.   int i;
  723.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  724.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  725.   fetch_gp_regs (regcache, tid);
  726.   if (tdep->ppc_fp0_regnum >= 0)
  727.     fetch_fp_regs (regcache, tid);
  728.   fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
  729.   if (tdep->ppc_ps_regnum != -1)
  730.     fetch_register (regcache, tid, tdep->ppc_ps_regnum);
  731.   if (tdep->ppc_cr_regnum != -1)
  732.     fetch_register (regcache, tid, tdep->ppc_cr_regnum);
  733.   if (tdep->ppc_lr_regnum != -1)
  734.     fetch_register (regcache, tid, tdep->ppc_lr_regnum);
  735.   if (tdep->ppc_ctr_regnum != -1)
  736.     fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
  737.   if (tdep->ppc_xer_regnum != -1)
  738.     fetch_register (regcache, tid, tdep->ppc_xer_regnum);
  739.   if (tdep->ppc_mq_regnum != -1)
  740.     fetch_register (regcache, tid, tdep->ppc_mq_regnum);
  741.   if (ppc_linux_trap_reg_p (gdbarch))
  742.     {
  743.       fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
  744.       fetch_register (regcache, tid, PPC_TRAP_REGNUM);
  745.     }
  746.   if (tdep->ppc_fpscr_regnum != -1)
  747.     fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
  748.   if (have_ptrace_getvrregs)
  749.     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
  750.       fetch_altivec_registers (regcache, tid);
  751.   if (have_ptrace_getsetvsxregs)
  752.     if (tdep->ppc_vsr0_upper_regnum != -1)
  753.       fetch_vsx_registers (regcache, tid);
  754.   if (tdep->ppc_ev0_upper_regnum >= 0)
  755.     fetch_spe_register (regcache, tid, -1);
  756. }

  757. /* Fetch registers from the child process.  Fetch all registers if
  758.    regno == -1, otherwise fetch all general registers or all floating
  759.    point registers depending upon the value of regno.  */
  760. static void
  761. ppc_linux_fetch_inferior_registers (struct target_ops *ops,
  762.                                     struct regcache *regcache, int regno)
  763. {
  764.   /* Overload thread id onto process id.  */
  765.   int tid = ptid_get_lwp (inferior_ptid);

  766.   /* No thread id, just use process id.  */
  767.   if (tid == 0)
  768.     tid = ptid_get_pid (inferior_ptid);

  769.   if (regno == -1)
  770.     fetch_ppc_registers (regcache, tid);
  771.   else
  772.     fetch_register (regcache, tid, regno);
  773. }

  774. /* Store one VSX register.  */
  775. static void
  776. store_vsx_register (const struct regcache *regcache, int tid, int regno)
  777. {
  778.   int ret;
  779.   gdb_vsxregset_t regs;
  780.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  781.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  782.   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);

  783.   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  784.   if (ret < 0)
  785.     {
  786.       if (errno == EIO)
  787.         {
  788.           have_ptrace_getsetvsxregs = 0;
  789.           return;
  790.         }
  791.       perror_with_name (_("Unable to fetch VSX register"));
  792.     }

  793.   regcache_raw_collect (regcache, regno, regs +
  794.                         (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);

  795.   ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
  796.   if (ret < 0)
  797.     perror_with_name (_("Unable to store VSX register"));
  798. }

  799. /* Store one register.  */
  800. static void
  801. store_altivec_register (const struct regcache *regcache, int tid, int regno)
  802. {
  803.   int ret;
  804.   int offset = 0;
  805.   gdb_vrregset_t regs;
  806.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  807.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  808.   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);

  809.   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  810.   if (ret < 0)
  811.     {
  812.       if (errno == EIO)
  813.         {
  814.           have_ptrace_getvrregs = 0;
  815.           return;
  816.         }
  817.       perror_with_name (_("Unable to fetch AltiVec register"));
  818.     }

  819.   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
  820.      long on the hardware.  */
  821.   if (regno == (tdep->ppc_vrsave_regnum - 1))
  822.     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);

  823.   regcache_raw_collect (regcache, regno,
  824.                         regs + (regno
  825.                                 - tdep->ppc_vr0_regnum) * vrregsize + offset);

  826.   ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
  827.   if (ret < 0)
  828.     perror_with_name (_("Unable to store AltiVec register"));
  829. }

  830. /* Assuming TID referrs to an SPE process, set the top halves of TID's
  831.    general-purpose registers and its SPE-specific registers to the
  832.    values in EVRREGSET.  If we don't support PTRACE_SETEVRREGS, do
  833.    nothing.

  834.    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
  835.    PTRACE_SETEVRREGS requests are supported is isolated here, and in
  836.    get_spe_registers.  */
  837. static void
  838. set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
  839. {
  840.   if (have_ptrace_getsetevrregs)
  841.     {
  842.       if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
  843.         return;
  844.       else
  845.         {
  846.           /* EIO means that the PTRACE_SETEVRREGS request isn't
  847.              supported; we fail silently, and don't try the call
  848.              again.  */
  849.           if (errno == EIO)
  850.             have_ptrace_getsetevrregs = 0;
  851.           else
  852.             /* Anything else needs to be reported.  */
  853.             perror_with_name (_("Unable to set SPE registers"));
  854.         }
  855.     }
  856. }

  857. /* Write GDB's value for the SPE-specific raw register REGNO to TID.
  858.    If REGNO is -1, write the values of all the SPE-specific
  859.    registers.  */
  860. static void
  861. store_spe_register (const struct regcache *regcache, int tid, int regno)
  862. {
  863.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  864.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  865.   struct gdb_evrregset_t evrregs;

  866.   gdb_assert (sizeof (evrregs.evr[0])
  867.               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
  868.   gdb_assert (sizeof (evrregs.acc)
  869.               == register_size (gdbarch, tdep->ppc_acc_regnum));
  870.   gdb_assert (sizeof (evrregs.spefscr)
  871.               == register_size (gdbarch, tdep->ppc_spefscr_regnum));

  872.   if (regno == -1)
  873.     /* Since we're going to write out every register, the code below
  874.        should store to every field of evrregs; if that doesn't happen,
  875.        make it obvious by initializing it with suspicious values.  */
  876.     memset (&evrregs, 42, sizeof (evrregs));
  877.   else
  878.     /* We can only read and write the entire EVR register set at a
  879.        time, so to write just a single register, we do a
  880.        read-modify-write maneuver.  */
  881.     get_spe_registers (tid, &evrregs);

  882.   if (regno == -1)
  883.     {
  884.       int i;

  885.       for (i = 0; i < ppc_num_gprs; i++)
  886.         regcache_raw_collect (regcache,
  887.                               tdep->ppc_ev0_upper_regnum + i,
  888.                               &evrregs.evr[i]);
  889.     }
  890.   else if (tdep->ppc_ev0_upper_regnum <= regno
  891.            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
  892.     regcache_raw_collect (regcache, regno,
  893.                           &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);

  894.   if (regno == -1
  895.       || regno == tdep->ppc_acc_regnum)
  896.     regcache_raw_collect (regcache,
  897.                           tdep->ppc_acc_regnum,
  898.                           &evrregs.acc);

  899.   if (regno == -1
  900.       || regno == tdep->ppc_spefscr_regnum)
  901.     regcache_raw_collect (regcache,
  902.                           tdep->ppc_spefscr_regnum,
  903.                           &evrregs.spefscr);

  904.   /* Write back the modified register set.  */
  905.   set_spe_registers (tid, &evrregs);
  906. }

  907. static void
  908. store_register (const struct regcache *regcache, int tid, int regno)
  909. {
  910.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  911.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  912.   /* This isn't really an address.  But ptrace thinks of it as one.  */
  913.   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
  914.   int i;
  915.   size_t bytes_to_transfer;
  916.   gdb_byte buf[MAX_REGISTER_SIZE];

  917.   if (altivec_register_p (gdbarch, regno))
  918.     {
  919.       store_altivec_register (regcache, tid, regno);
  920.       return;
  921.     }
  922.   if (vsx_register_p (gdbarch, regno))
  923.     {
  924.       store_vsx_register (regcache, tid, regno);
  925.       return;
  926.     }
  927.   else if (spe_register_p (gdbarch, regno))
  928.     {
  929.       store_spe_register (regcache, tid, regno);
  930.       return;
  931.     }

  932.   if (regaddr == -1)
  933.     return;

  934.   /* First collect the register.  Keep in mind that the regcache's
  935.      idea of the register's size may not be a multiple of sizeof
  936.      (long).  */
  937.   memset (buf, 0, sizeof buf);
  938.   bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
  939.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  940.     {
  941.       /* Little-endian values always sit at the left end of the buffer.  */
  942.       regcache_raw_collect (regcache, regno, buf);
  943.     }
  944.   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  945.     {
  946.       /* Big-endian values sit at the right end of the buffer.  */
  947.       size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
  948.       regcache_raw_collect (regcache, regno, buf + padding);
  949.     }

  950.   for (i = 0; i < bytes_to_transfer; i += sizeof (long))
  951.     {
  952.       long l;

  953.       memcpy (&l, &buf[i], sizeof (l));
  954.       errno = 0;
  955.       ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
  956.       regaddr += sizeof (long);

  957.       if (errno == EIO
  958.           && (regno == tdep->ppc_fpscr_regnum
  959.               || regno == PPC_ORIG_R3_REGNUM
  960.               || regno == PPC_TRAP_REGNUM))
  961.         {
  962.           /* Some older kernel versions don't allow fpscr, orig_r3
  963.              or trap to be written.  */
  964.           continue;
  965.         }

  966.       if (errno != 0)
  967.         {
  968.           char message[128];
  969.           xsnprintf (message, sizeof (message), "writing register %s (#%d)",
  970.                      gdbarch_register_name (gdbarch, regno), regno);
  971.           perror_with_name (message);
  972.         }
  973.     }
  974. }

  975. static void
  976. fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
  977. {
  978.   int i;
  979.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  980.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  981.   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);

  982.   for (i = 0; i < ppc_num_vshrs; i++)
  983.     regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
  984.                           *vsxregsetp + i * vsxregsize);
  985. }

  986. static void
  987. fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
  988. {
  989.   int i;
  990.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  991.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  992.   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
  993.   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
  994.   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);

  995.   for (i = 0; i < num_of_vrregs; i++)
  996.     {
  997.       /* The last 2 registers of this set are only 32 bit long, not
  998.          128, but only VSCR is fetched as a 16 bytes quantity.  */
  999.       if (i == (num_of_vrregs - 2))
  1000.         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
  1001.                               *vrregsetp + i * vrregsize + offset);
  1002.       else
  1003.         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
  1004.                               *vrregsetp + i * vrregsize);
  1005.     }
  1006. }

  1007. static void
  1008. store_vsx_registers (const struct regcache *regcache, int tid)
  1009. {
  1010.   int ret;
  1011.   gdb_vsxregset_t regs;

  1012.   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  1013.   if (ret < 0)
  1014.     {
  1015.       if (errno == EIO)
  1016.         {
  1017.           have_ptrace_getsetvsxregs = 0;
  1018.           return;
  1019.         }
  1020.       perror_with_name (_("Couldn't get VSX registers"));
  1021.     }

  1022.   fill_vsxregset (regcache, &regs);

  1023.   if (ptrace (PTRACE_SETVSXREGS, tid, 0, &regs) < 0)
  1024.     perror_with_name (_("Couldn't write VSX registers"));
  1025. }

  1026. static void
  1027. store_altivec_registers (const struct regcache *regcache, int tid)
  1028. {
  1029.   int ret;
  1030.   gdb_vrregset_t regs;

  1031.   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  1032.   if (ret < 0)
  1033.     {
  1034.       if (errno == EIO)
  1035.         {
  1036.           have_ptrace_getvrregs = 0;
  1037.           return;
  1038.         }
  1039.       perror_with_name (_("Couldn't get AltiVec registers"));
  1040.     }

  1041.   fill_vrregset (regcache, &regs);

  1042.   if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
  1043.     perror_with_name (_("Couldn't write AltiVec registers"));
  1044. }

  1045. /* This function actually issues the request to ptrace, telling
  1046.    it to store all general-purpose registers present in the specified
  1047.    regset.

  1048.    If the ptrace request does not exist, this function returns 0
  1049.    and properly sets the have_ptrace_* flag.  If the request fails,
  1050.    this function calls perror_with_name.  Otherwise, if the request
  1051.    succeeds, then the regcache is stored and 1 is returned.  */
  1052. static int
  1053. store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
  1054. {
  1055.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1056.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1057.   gdb_gregset_t gregset;

  1058.   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
  1059.     {
  1060.       if (errno == EIO)
  1061.         {
  1062.           have_ptrace_getsetregs = 0;
  1063.           return 0;
  1064.         }
  1065.       perror_with_name (_("Couldn't get general-purpose registers."));
  1066.     }

  1067.   fill_gregset (regcache, &gregset, regno);

  1068.   if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
  1069.     {
  1070.       if (errno == EIO)
  1071.         {
  1072.           have_ptrace_getsetregs = 0;
  1073.           return 0;
  1074.         }
  1075.       perror_with_name (_("Couldn't set general-purpose registers."));
  1076.     }

  1077.   return 1;
  1078. }

  1079. /* This is a wrapper for the store_all_gp_regs function.  It is
  1080.    responsible for verifying if this target has the ptrace request
  1081.    that can be used to store all general-purpose registers at one
  1082.    shot.  If it doesn't, then we should store them using the
  1083.    old-fashioned way, which is to iterate over the registers and
  1084.    store them one by one.  */
  1085. static void
  1086. store_gp_regs (const struct regcache *regcache, int tid, int regno)
  1087. {
  1088.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1089.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1090.   int i;

  1091.   if (have_ptrace_getsetregs)
  1092.     if (store_all_gp_regs (regcache, tid, regno))
  1093.       return;

  1094.   /* If we hit this point, it doesn't really matter which
  1095.      architecture we are using.  We just need to store the
  1096.      registers in the "old-fashioned way".  */
  1097.   for (i = 0; i < ppc_num_gprs; i++)
  1098.     store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
  1099. }

  1100. /* This function actually issues the request to ptrace, telling
  1101.    it to store all floating-point registers present in the specified
  1102.    regset.

  1103.    If the ptrace request does not exist, this function returns 0
  1104.    and properly sets the have_ptrace_* flag.  If the request fails,
  1105.    this function calls perror_with_name.  Otherwise, if the request
  1106.    succeeds, then the regcache is stored and 1 is returned.  */
  1107. static int
  1108. store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
  1109. {
  1110.   gdb_fpregset_t fpregs;

  1111.   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
  1112.     {
  1113.       if (errno == EIO)
  1114.         {
  1115.           have_ptrace_getsetfpregs = 0;
  1116.           return 0;
  1117.         }
  1118.       perror_with_name (_("Couldn't get floating-point registers."));
  1119.     }

  1120.   fill_fpregset (regcache, &fpregs, regno);

  1121.   if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
  1122.     {
  1123.       if (errno == EIO)
  1124.         {
  1125.           have_ptrace_getsetfpregs = 0;
  1126.           return 0;
  1127.         }
  1128.       perror_with_name (_("Couldn't set floating-point registers."));
  1129.     }

  1130.   return 1;
  1131. }

  1132. /* This is a wrapper for the store_all_fp_regs function.  It is
  1133.    responsible for verifying if this target has the ptrace request
  1134.    that can be used to store all floating-point registers at one
  1135.    shot.  If it doesn't, then we should store them using the
  1136.    old-fashioned way, which is to iterate over the registers and
  1137.    store them one by one.  */
  1138. static void
  1139. store_fp_regs (const struct regcache *regcache, int tid, int regno)
  1140. {
  1141.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1142.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1143.   int i;

  1144.   if (have_ptrace_getsetfpregs)
  1145.     if (store_all_fp_regs (regcache, tid, regno))
  1146.       return;

  1147.   /* If we hit this point, it doesn't really matter which
  1148.      architecture we are using.  We just need to store the
  1149.      registers in the "old-fashioned way".  */
  1150.   for (i = 0; i < ppc_num_fprs; i++)
  1151.     store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
  1152. }

  1153. static void
  1154. store_ppc_registers (const struct regcache *regcache, int tid)
  1155. {
  1156.   int i;
  1157.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1158.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1159.   store_gp_regs (regcache, tid, -1);
  1160.   if (tdep->ppc_fp0_regnum >= 0)
  1161.     store_fp_regs (regcache, tid, -1);
  1162.   store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
  1163.   if (tdep->ppc_ps_regnum != -1)
  1164.     store_register (regcache, tid, tdep->ppc_ps_regnum);
  1165.   if (tdep->ppc_cr_regnum != -1)
  1166.     store_register (regcache, tid, tdep->ppc_cr_regnum);
  1167.   if (tdep->ppc_lr_regnum != -1)
  1168.     store_register (regcache, tid, tdep->ppc_lr_regnum);
  1169.   if (tdep->ppc_ctr_regnum != -1)
  1170.     store_register (regcache, tid, tdep->ppc_ctr_regnum);
  1171.   if (tdep->ppc_xer_regnum != -1)
  1172.     store_register (regcache, tid, tdep->ppc_xer_regnum);
  1173.   if (tdep->ppc_mq_regnum != -1)
  1174.     store_register (regcache, tid, tdep->ppc_mq_regnum);
  1175.   if (tdep->ppc_fpscr_regnum != -1)
  1176.     store_register (regcache, tid, tdep->ppc_fpscr_regnum);
  1177.   if (ppc_linux_trap_reg_p (gdbarch))
  1178.     {
  1179.       store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
  1180.       store_register (regcache, tid, PPC_TRAP_REGNUM);
  1181.     }
  1182.   if (have_ptrace_getvrregs)
  1183.     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
  1184.       store_altivec_registers (regcache, tid);
  1185.   if (have_ptrace_getsetvsxregs)
  1186.     if (tdep->ppc_vsr0_upper_regnum != -1)
  1187.       store_vsx_registers (regcache, tid);
  1188.   if (tdep->ppc_ev0_upper_regnum >= 0)
  1189.     store_spe_register (regcache, tid, -1);
  1190. }

  1191. /* Fetch the AT_HWCAP entry from the aux vector.  */
  1192. static unsigned long
  1193. ppc_linux_get_hwcap (void)
  1194. {
  1195.   CORE_ADDR field;

  1196.   if (target_auxv_search (&current_target, AT_HWCAP, &field))
  1197.     return (unsigned long) field;

  1198.   return 0;
  1199. }

  1200. /* The cached DABR value, to install in new threads.
  1201.    This variable is used when the PowerPC HWDEBUG ptrace
  1202.    interface is not available.  */
  1203. static long saved_dabr_value;

  1204. /* Global structure that will store information about the available
  1205.    features provided by the PowerPC HWDEBUG ptrace interface.  */
  1206. static struct ppc_debug_info hwdebug_info;

  1207. /* Global variable that holds the maximum number of slots that the
  1208.    kernel will use.  This is only used when PowerPC HWDEBUG ptrace interface
  1209.    is available.  */
  1210. static size_t max_slots_number = 0;

  1211. struct hw_break_tuple
  1212. {
  1213.   long slot;
  1214.   struct ppc_hw_breakpoint *hw_break;
  1215. };

  1216. /* This is an internal VEC created to store information about *points inserted
  1217.    for each thread.  This is used when PowerPC HWDEBUG ptrace interface is
  1218.    available.  */
  1219. typedef struct thread_points
  1220.   {
  1221.     /* The TID to which this *point relates.  */
  1222.     int tid;
  1223.     /* Information about the *point, such as its address, type, etc.

  1224.        Each element inside this vector corresponds to a hardware
  1225.        breakpoint or watchpoint in the thread represented by TID.  The maximum
  1226.        size of these vector is MAX_SLOTS_NUMBER.  If the hw_break element of
  1227.        the tuple is NULL, then the position in the vector is free.  */
  1228.     struct hw_break_tuple *hw_breaks;
  1229.   } *thread_points_p;
  1230. DEF_VEC_P (thread_points_p);

  1231. VEC(thread_points_p) *ppc_threads = NULL;

  1232. /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
  1233.    available.  */
  1234. #define PPC_DEBUG_CURRENT_VERSION 1

  1235. /* Returns non-zero if we support the PowerPC HWDEBUG ptrace interface.  */
  1236. static int
  1237. have_ptrace_hwdebug_interface (void)
  1238. {
  1239.   static int have_ptrace_hwdebug_interface = -1;

  1240.   if (have_ptrace_hwdebug_interface == -1)
  1241.     {
  1242.       int tid;

  1243.       tid = ptid_get_lwp (inferior_ptid);
  1244.       if (tid == 0)
  1245.         tid = ptid_get_pid (inferior_ptid);

  1246.       /* Check for kernel support for PowerPC HWDEBUG ptrace interface.  */
  1247.       if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info) >= 0)
  1248.         {
  1249.           /* Check whether PowerPC HWDEBUG ptrace interface is functional and
  1250.              provides any supported feature.  */
  1251.           if (hwdebug_info.features != 0)
  1252.             {
  1253.               have_ptrace_hwdebug_interface = 1;
  1254.               max_slots_number = hwdebug_info.num_instruction_bps
  1255.                 + hwdebug_info.num_data_bps
  1256.                 + hwdebug_info.num_condition_regs;
  1257.               return have_ptrace_hwdebug_interface;
  1258.             }
  1259.         }
  1260.       /* Old school interface and no PowerPC HWDEBUG ptrace support.  */
  1261.       have_ptrace_hwdebug_interface = 0;
  1262.       memset (&hwdebug_info, 0, sizeof (struct ppc_debug_info));
  1263.     }

  1264.   return have_ptrace_hwdebug_interface;
  1265. }

  1266. static int
  1267. ppc_linux_can_use_hw_breakpoint (struct target_ops *self,
  1268.                                  int type, int cnt, int ot)
  1269. {
  1270.   int total_hw_wp, total_hw_bp;

  1271.   if (have_ptrace_hwdebug_interface ())
  1272.     {
  1273.       /* When PowerPC HWDEBUG ptrace interface is available, the number of
  1274.          available hardware watchpoints and breakpoints is stored at the
  1275.          hwdebug_info struct.  */
  1276.       total_hw_bp = hwdebug_info.num_instruction_bps;
  1277.       total_hw_wp = hwdebug_info.num_data_bps;
  1278.     }
  1279.   else
  1280.     {
  1281.       /* When we do not have PowerPC HWDEBUG ptrace interface, we should
  1282.          consider having 1 hardware watchpoint and no hardware breakpoints.  */
  1283.       total_hw_bp = 0;
  1284.       total_hw_wp = 1;
  1285.     }

  1286.   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
  1287.       || type == bp_access_watchpoint || type == bp_watchpoint)
  1288.     {
  1289.       if (cnt + ot > total_hw_wp)
  1290.         return -1;
  1291.     }
  1292.   else if (type == bp_hardware_breakpoint)
  1293.     {
  1294.       if (total_hw_bp == 0)
  1295.         {
  1296.           /* No hardware breakpoint support. */
  1297.           return 0;
  1298.         }
  1299.       if (cnt > total_hw_bp)
  1300.         return -1;
  1301.     }

  1302.   if (!have_ptrace_hwdebug_interface ())
  1303.     {
  1304.       int tid;
  1305.       ptid_t ptid = inferior_ptid;

  1306.       /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG
  1307.          and whether the target has DABR.  If either answer is no, the
  1308.          ptrace call will return -1.  Fail in that case.  */
  1309.       tid = ptid_get_lwp (ptid);
  1310.       if (tid == 0)
  1311.         tid = ptid_get_pid (ptid);

  1312.       if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
  1313.         return 0;
  1314.     }

  1315.   return 1;
  1316. }

  1317. static int
  1318. ppc_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
  1319.                                        CORE_ADDR addr, int len)
  1320. {
  1321.   /* Handle sub-8-byte quantities.  */
  1322.   if (len <= 0)
  1323.     return 0;

  1324.   /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
  1325.      restrictions for watchpoints in the processors.  In that case, we use that
  1326.      information to determine the hardcoded watchable region for
  1327.      watchpoints.  */
  1328.   if (have_ptrace_hwdebug_interface ())
  1329.     {
  1330.       int region_size;
  1331.       /* Embedded DAC-based processors, like the PowerPC 440 have ranged
  1332.          watchpoints and can watch any access within an arbitrary memory
  1333.          region. This is useful to watch arrays and structs, for instance.  It
  1334.          takes two hardware watchpoints though.  */
  1335.       if (len > 1
  1336.           && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
  1337.           && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
  1338.         return 2;
  1339.       /* Check if the processor provides DAWR interface.  */
  1340.       if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
  1341.         /* DAWR interface allows to watch up to 512 byte wide ranges which
  1342.            can't cross a 512 byte boundary.  */
  1343.         region_size = 512;
  1344.       else
  1345.         region_size = hwdebug_info.data_bp_alignment;
  1346.       /* Server processors provide one hardware watchpoint and addr+len should
  1347.          fall in the watchable region provided by the ptrace interface.  */
  1348.       if (region_size
  1349.           && (addr + len > (addr & ~(region_size - 1)) + region_size))
  1350.         return 0;
  1351.     }
  1352.   /* addr+len must fall in the 8 byte watchable region for DABR-based
  1353.      processors (i.e., server processors).  Without the new PowerPC HWDEBUG
  1354.      ptrace interface, DAC-based processors (i.e., embedded processors) will
  1355.      use addresses aligned to 4-bytes due to the way the read/write flags are
  1356.      passed in the old ptrace interface.  */
  1357.   else if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
  1358.            && (addr + len) > (addr & ~3) + 4)
  1359.            || (addr + len) > (addr & ~7) + 8)
  1360.     return 0;

  1361.   return 1;
  1362. }

  1363. /* This function compares two ppc_hw_breakpoint structs field-by-field.  */
  1364. static int
  1365. hwdebug_point_cmp (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
  1366. {
  1367.   return (a->trigger_type == b->trigger_type
  1368.           && a->addr_mode == b->addr_mode
  1369.           && a->condition_mode == b->condition_mode
  1370.           && a->addr == b->addr
  1371.           && a->addr2 == b->addr2
  1372.           && a->condition_value == b->condition_value);
  1373. }

  1374. /* This function can be used to retrieve a thread_points by the TID of the
  1375.    related process/thread.  If nothing has been found, and ALLOC_NEW is 0,
  1376.    it returns NULL.  If ALLOC_NEW is non-zero, a new thread_points for the
  1377.    provided TID will be created and returned.  */
  1378. static struct thread_points *
  1379. hwdebug_find_thread_points_by_tid (int tid, int alloc_new)
  1380. {
  1381.   int i;
  1382.   struct thread_points *t;

  1383.   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
  1384.     if (t->tid == tid)
  1385.       return t;

  1386.   t = NULL;

  1387.   /* Do we need to allocate a new point_item
  1388.      if the wanted one does not exist?  */
  1389.   if (alloc_new)
  1390.     {
  1391.       t = xmalloc (sizeof (struct thread_points));
  1392.       t->hw_breaks
  1393.         = xzalloc (max_slots_number * sizeof (struct hw_break_tuple));
  1394.       t->tid = tid;
  1395.       VEC_safe_push (thread_points_p, ppc_threads, t);
  1396.     }

  1397.   return t;
  1398. }

  1399. /* This function is a generic wrapper that is responsible for inserting a
  1400.    *point (i.e., calling `ptrace' in order to issue the request to the
  1401.    kernel) and registering it internally in GDB.  */
  1402. static void
  1403. hwdebug_insert_point (struct ppc_hw_breakpoint *b, int tid)
  1404. {
  1405.   int i;
  1406.   long slot;
  1407.   struct ppc_hw_breakpoint *p = xmalloc (sizeof (struct ppc_hw_breakpoint));
  1408.   struct hw_break_tuple *hw_breaks;
  1409.   struct cleanup *c = make_cleanup (xfree, p);
  1410.   struct thread_points *t;
  1411.   struct hw_break_tuple *tuple;

  1412.   memcpy (p, b, sizeof (struct ppc_hw_breakpoint));

  1413.   errno = 0;
  1414.   slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p);
  1415.   if (slot < 0)
  1416.     perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));

  1417.   /* Everything went fine, so we have to register this *point.  */
  1418.   t = hwdebug_find_thread_points_by_tid (tid, 1);
  1419.   gdb_assert (t != NULL);
  1420.   hw_breaks = t->hw_breaks;

  1421.   /* Find a free element in the hw_breaks vector.  */
  1422.   for (i = 0; i < max_slots_number; i++)
  1423.     if (hw_breaks[i].hw_break == NULL)
  1424.       {
  1425.         hw_breaks[i].slot = slot;
  1426.         hw_breaks[i].hw_break = p;
  1427.         break;
  1428.       }

  1429.   gdb_assert (i != max_slots_number);

  1430.   discard_cleanups (c);
  1431. }

  1432. /* This function is a generic wrapper that is responsible for removing a
  1433.    *point (i.e., calling `ptrace' in order to issue the request to the
  1434.    kernel), and unregistering it internally at GDB.  */
  1435. static void
  1436. hwdebug_remove_point (struct ppc_hw_breakpoint *b, int tid)
  1437. {
  1438.   int i;
  1439.   struct hw_break_tuple *hw_breaks;
  1440.   struct thread_points *t;

  1441.   t = hwdebug_find_thread_points_by_tid (tid, 0);
  1442.   gdb_assert (t != NULL);
  1443.   hw_breaks = t->hw_breaks;

  1444.   for (i = 0; i < max_slots_number; i++)
  1445.     if (hw_breaks[i].hw_break && hwdebug_point_cmp (hw_breaks[i].hw_break, b))
  1446.       break;

  1447.   gdb_assert (i != max_slots_number);

  1448.   /* We have to ignore ENOENT errors because the kernel implements hardware
  1449.      breakpoints/watchpoints as "one-shot", that is, they are automatically
  1450.      deleted when hit.  */
  1451.   errno = 0;
  1452.   if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
  1453.     if (errno != ENOENT)
  1454.       perror_with_name (_("Unexpected error deleting "
  1455.                           "breakpoint or watchpoint"));

  1456.   xfree (hw_breaks[i].hw_break);
  1457.   hw_breaks[i].hw_break = NULL;
  1458. }

  1459. /* Return the number of registers needed for a ranged breakpoint.  */

  1460. static int
  1461. ppc_linux_ranged_break_num_registers (struct target_ops *target)
  1462. {
  1463.   return ((have_ptrace_hwdebug_interface ()
  1464.            && hwdebug_info.features & PPC_DEBUG_FEATURE_INSN_BP_RANGE)?
  1465.           2 : -1);
  1466. }

  1467. /* Insert the hardware breakpoint described by BP_TGT.  Returns 0 for
  1468.    success, 1 if hardware breakpoints are not supported or -1 for failure.  */

  1469. static int
  1470. ppc_linux_insert_hw_breakpoint (struct target_ops *self,
  1471.                                 struct gdbarch *gdbarch,
  1472.                                   struct bp_target_info *bp_tgt)
  1473. {
  1474.   struct lwp_info *lp;
  1475.   struct ppc_hw_breakpoint p;

  1476.   if (!have_ptrace_hwdebug_interface ())
  1477.     return -1;

  1478.   p.version = PPC_DEBUG_CURRENT_VERSION;
  1479.   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
  1480.   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1481.   p.addr = (uint64_t) (bp_tgt->placed_address = bp_tgt->reqstd_address);
  1482.   p.condition_value = 0;

  1483.   if (bp_tgt->length)
  1484.     {
  1485.       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;

  1486.       /* The breakpoint will trigger if the address of the instruction is
  1487.          within the defined range, as follows: p.addr <= address < p.addr2.  */
  1488.       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
  1489.     }
  1490.   else
  1491.     {
  1492.       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  1493.       p.addr2 = 0;
  1494.     }

  1495.   ALL_LWPS (lp)
  1496.     hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));

  1497.   return 0;
  1498. }

  1499. static int
  1500. ppc_linux_remove_hw_breakpoint (struct target_ops *self,
  1501.                                 struct gdbarch *gdbarch,
  1502.                                   struct bp_target_info *bp_tgt)
  1503. {
  1504.   struct lwp_info *lp;
  1505.   struct ppc_hw_breakpoint p;

  1506.   if (!have_ptrace_hwdebug_interface ())
  1507.     return -1;

  1508.   p.version = PPC_DEBUG_CURRENT_VERSION;
  1509.   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
  1510.   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1511.   p.addr = (uint64_t) bp_tgt->placed_address;
  1512.   p.condition_value = 0;

  1513.   if (bp_tgt->length)
  1514.     {
  1515.       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;

  1516.       /* The breakpoint will trigger if the address of the instruction is within
  1517.          the defined range, as follows: p.addr <= address < p.addr2.  */
  1518.       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
  1519.     }
  1520.   else
  1521.     {
  1522.       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  1523.       p.addr2 = 0;
  1524.     }

  1525.   ALL_LWPS (lp)
  1526.     hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));

  1527.   return 0;
  1528. }

  1529. static int
  1530. get_trigger_type (int rw)
  1531. {
  1532.   int t;

  1533.   if (rw == hw_read)
  1534.     t = PPC_BREAKPOINT_TRIGGER_READ;
  1535.   else if (rw == hw_write)
  1536.     t = PPC_BREAKPOINT_TRIGGER_WRITE;
  1537.   else
  1538.     t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;

  1539.   return t;
  1540. }

  1541. /* Insert a new masked watchpoint at ADDR using the mask MASK.
  1542.    RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
  1543.    or hw_access for an access watchpoint.  Returns 0 on success and throws
  1544.    an error on failure.  */

  1545. static int
  1546. ppc_linux_insert_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
  1547.                                   CORE_ADDR mask, int rw)
  1548. {
  1549.   struct lwp_info *lp;
  1550.   struct ppc_hw_breakpoint p;

  1551.   gdb_assert (have_ptrace_hwdebug_interface ());

  1552.   p.version = PPC_DEBUG_CURRENT_VERSION;
  1553.   p.trigger_type = get_trigger_type (rw);
  1554.   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
  1555.   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1556.   p.addr = addr;
  1557.   p.addr2 = mask;
  1558.   p.condition_value = 0;

  1559.   ALL_LWPS (lp)
  1560.     hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));

  1561.   return 0;
  1562. }

  1563. /* Remove a masked watchpoint at ADDR with the mask MASK.
  1564.    RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
  1565.    or hw_access for an access watchpoint.  Returns 0 on success and throws
  1566.    an error on failure.  */

  1567. static int
  1568. ppc_linux_remove_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
  1569.                                   CORE_ADDR mask, int rw)
  1570. {
  1571.   struct lwp_info *lp;
  1572.   struct ppc_hw_breakpoint p;

  1573.   gdb_assert (have_ptrace_hwdebug_interface ());

  1574.   p.version = PPC_DEBUG_CURRENT_VERSION;
  1575.   p.trigger_type = get_trigger_type (rw);
  1576.   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
  1577.   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1578.   p.addr = addr;
  1579.   p.addr2 = mask;
  1580.   p.condition_value = 0;

  1581.   ALL_LWPS (lp)
  1582.     hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));

  1583.   return 0;
  1584. }

  1585. /* Check whether we have at least one free DVC register.  */
  1586. static int
  1587. can_use_watchpoint_cond_accel (void)
  1588. {
  1589.   struct thread_points *p;
  1590.   int tid = ptid_get_lwp (inferior_ptid);
  1591.   int cnt = hwdebug_info.num_condition_regs, i;
  1592.   CORE_ADDR tmp_value;

  1593.   if (!have_ptrace_hwdebug_interface () || cnt == 0)
  1594.     return 0;

  1595.   p = hwdebug_find_thread_points_by_tid (tid, 0);

  1596.   if (p)
  1597.     {
  1598.       for (i = 0; i < max_slots_number; i++)
  1599.         if (p->hw_breaks[i].hw_break != NULL
  1600.             && (p->hw_breaks[i].hw_break->condition_mode
  1601.                 != PPC_BREAKPOINT_CONDITION_NONE))
  1602.           cnt--;

  1603.       /* There are no available slots now.  */
  1604.       if (cnt <= 0)
  1605.         return 0;
  1606.     }

  1607.   return 1;
  1608. }

  1609. /* Calculate the enable bits and the contents of the Data Value Compare
  1610.    debug register present in BookE processors.

  1611.    ADDR is the address to be watched, LEN is the length of watched data
  1612.    and DATA_VALUE is the value which will trigger the watchpoint.
  1613.    On exit, CONDITION_MODE will hold the enable bits for the DVC, and
  1614.    CONDITION_VALUE will hold the value which should be put in the
  1615.    DVC register.  */
  1616. static void
  1617. calculate_dvc (CORE_ADDR addr, int len, CORE_ADDR data_value,
  1618.                uint32_t *condition_mode, uint64_t *condition_value)
  1619. {
  1620.   int i, num_byte_enable, align_offset, num_bytes_off_dvc,
  1621.       rightmost_enabled_byte;
  1622.   CORE_ADDR addr_end_data, addr_end_dvc;

  1623.   /* The DVC register compares bytes within fixed-length windows which
  1624.      are word-aligned, with length equal to that of the DVC register.
  1625.      We need to calculate where our watch region is relative to that
  1626.      window and enable comparison of the bytes which fall within it.  */

  1627.   align_offset = addr % hwdebug_info.sizeof_condition;
  1628.   addr_end_data = addr + len;
  1629.   addr_end_dvc = (addr - align_offset
  1630.                   + hwdebug_info.sizeof_condition);
  1631.   num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
  1632.                          addr_end_data - addr_end_dvc : 0;
  1633.   num_byte_enable = len - num_bytes_off_dvc;
  1634.   /* Here, bytes are numbered from right to left.  */
  1635.   rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
  1636.                               addr_end_dvc - addr_end_data : 0;

  1637.   *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
  1638.   for (i = 0; i < num_byte_enable; i++)
  1639.     *condition_mode
  1640.       |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);

  1641.   /* Now we need to match the position within the DVC of the comparison
  1642.      value with where the watch region is relative to the window
  1643.      (i.e., the ALIGN_OFFSET).  */

  1644.   *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
  1645.                       << rightmost_enabled_byte * 8);
  1646. }

  1647. /* Return the number of memory locations that need to be accessed to
  1648.    evaluate the expression which generated the given value chain.
  1649.    Returns -1 if there's any register access involved, or if there are
  1650.    other kinds of values which are not acceptable in a condition
  1651.    expression (e.g., lval_computed or lval_internalvar).  */
  1652. static int
  1653. num_memory_accesses (struct value *v)
  1654. {
  1655.   int found_memory_cnt = 0;
  1656.   struct value *head = v;

  1657.   /* The idea here is that evaluating an expression generates a series
  1658.      of values, one holding the value of every subexpression.  (The
  1659.      expression a*b+c has five subexpressions: a, b, a*b, c, and
  1660.      a*b+c.)  GDB's values hold almost enough information to establish
  1661.      the criteria given above --- they identify memory lvalues,
  1662.      register lvalues, computed values, etcetera.  So we can evaluate
  1663.      the expression, and then scan the chain of values that leaves
  1664.      behind to determine the memory locations involved in the evaluation
  1665.      of an expression.

  1666.      However, I don't think that the values returned by inferior
  1667.      function calls are special in any way.  So this function may not
  1668.      notice that an expression contains an inferior function call.
  1669.      FIXME.  */

  1670.   for (; v; v = value_next (v))
  1671.     {
  1672.       /* Constants and values from the history are fine.  */
  1673.       if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
  1674.         continue;
  1675.       else if (VALUE_LVAL (v) == lval_memory)
  1676.         {
  1677.           /* A lazy memory lvalue is one that GDB never needed to fetch;
  1678.              we either just used its address (e.g., `a' in `a.b') or
  1679.              we never needed it at all (e.g., `a' in `a,b').  */
  1680.           if (!value_lazy (v))
  1681.             found_memory_cnt++;
  1682.         }
  1683.       /* Other kinds of values are not fine.  */
  1684.       else
  1685.         return -1;
  1686.     }

  1687.   return found_memory_cnt;
  1688. }

  1689. /* Verifies whether the expression COND can be implemented using the
  1690.    DVC (Data Value Compare) register in BookE processors.  The expression
  1691.    must test the watch value for equality with a constant expression.
  1692.    If the function returns 1, DATA_VALUE will contain the constant against
  1693.    which the watch value should be compared and LEN will contain the size
  1694.    of the constant.  */
  1695. static int
  1696. check_condition (CORE_ADDR watch_addr, struct expression *cond,
  1697.                  CORE_ADDR *data_value, int *len)
  1698. {
  1699.   int pc = 1, num_accesses_left, num_accesses_right;
  1700.   struct value *left_val, *right_val, *left_chain, *right_chain;

  1701.   if (cond->elts[0].opcode != BINOP_EQUAL)
  1702.     return 0;

  1703.   fetch_subexp_value (cond, &pc, &left_val, NULL, &left_chain, 0);
  1704.   num_accesses_left = num_memory_accesses (left_chain);

  1705.   if (left_val == NULL || num_accesses_left < 0)
  1706.     {
  1707.       free_value_chain (left_chain);

  1708.       return 0;
  1709.     }

  1710.   fetch_subexp_value (cond, &pc, &right_val, NULL, &right_chain, 0);
  1711.   num_accesses_right = num_memory_accesses (right_chain);

  1712.   if (right_val == NULL || num_accesses_right < 0)
  1713.     {
  1714.       free_value_chain (left_chain);
  1715.       free_value_chain (right_chain);

  1716.       return 0;
  1717.     }

  1718.   if (num_accesses_left == 1 && num_accesses_right == 0
  1719.       && VALUE_LVAL (left_val) == lval_memory
  1720.       && value_address (left_val) == watch_addr)
  1721.     {
  1722.       *data_value = value_as_long (right_val);

  1723.       /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
  1724.          the same type as the memory region referenced by LEFT_VAL.  */
  1725.       *len = TYPE_LENGTH (check_typedef (value_type (left_val)));
  1726.     }
  1727.   else if (num_accesses_left == 0 && num_accesses_right == 1
  1728.            && VALUE_LVAL (right_val) == lval_memory
  1729.            && value_address (right_val) == watch_addr)
  1730.     {
  1731.       *data_value = value_as_long (left_val);

  1732.       /* DATA_VALUE is the constant in LEFT_VAL, but actually has
  1733.          the same type as the memory region referenced by RIGHT_VAL.  */
  1734.       *len = TYPE_LENGTH (check_typedef (value_type (right_val)));
  1735.     }
  1736.   else
  1737.     {
  1738.       free_value_chain (left_chain);
  1739.       free_value_chain (right_chain);

  1740.       return 0;
  1741.     }

  1742.   free_value_chain (left_chain);
  1743.   free_value_chain (right_chain);

  1744.   return 1;
  1745. }

  1746. /* Return non-zero if the target is capable of using hardware to evaluate
  1747.    the condition expression, thus only triggering the watchpoint when it is
  1748.    true.  */
  1749. static int
  1750. ppc_linux_can_accel_watchpoint_condition (struct target_ops *self,
  1751.                                           CORE_ADDR addr, int len, int rw,
  1752.                                           struct expression *cond)
  1753. {
  1754.   CORE_ADDR data_value;

  1755.   return (have_ptrace_hwdebug_interface ()
  1756.           && hwdebug_info.num_condition_regs > 0
  1757.           && check_condition (addr, cond, &data_value, &len));
  1758. }

  1759. /* Set up P with the parameters necessary to request a watchpoint covering
  1760.    LEN bytes starting at ADDR and if possible with condition expression COND
  1761.    evaluated by hardware.  INSERT tells if we are creating a request for
  1762.    inserting or removing the watchpoint.  */

  1763. static void
  1764. create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
  1765.                            int len, int rw, struct expression *cond,
  1766.                            int insert)
  1767. {
  1768.   if (len == 1
  1769.       || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
  1770.     {
  1771.       int use_condition;
  1772.       CORE_ADDR data_value;

  1773.       use_condition = (insert? can_use_watchpoint_cond_accel ()
  1774.                         : hwdebug_info.num_condition_regs > 0);
  1775.       if (cond && use_condition && check_condition (addr, cond,
  1776.                                                     &data_value, &len))
  1777.         calculate_dvc (addr, len, data_value, &p->condition_mode,
  1778.                        &p->condition_value);
  1779.       else
  1780.         {
  1781.           p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1782.           p->condition_value = 0;
  1783.         }

  1784.       p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  1785.       p->addr2 = 0;
  1786.     }
  1787.   else
  1788.     {
  1789.       p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
  1790.       p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1791.       p->condition_value = 0;

  1792.       /* The watchpoint will trigger if the address of the memory access is
  1793.          within the defined range, as follows: p->addr <= address < p->addr2.

  1794.          Note that the above sentence just documents how ptrace interprets
  1795.          its arguments; the watchpoint is set to watch the range defined by
  1796.          the user _inclusively_, as specified by the user interface.  */
  1797.       p->addr2 = (uint64_t) addr + len;
  1798.     }

  1799.   p->version = PPC_DEBUG_CURRENT_VERSION;
  1800.   p->trigger_type = get_trigger_type (rw);
  1801.   p->addr = (uint64_t) addr;
  1802. }

  1803. static int
  1804. ppc_linux_insert_watchpoint (struct target_ops *self,
  1805.                              CORE_ADDR addr, int len, int rw,
  1806.                              struct expression *cond)
  1807. {
  1808.   struct lwp_info *lp;
  1809.   int ret = -1;

  1810.   if (have_ptrace_hwdebug_interface ())
  1811.     {
  1812.       struct ppc_hw_breakpoint p;

  1813.       create_watchpoint_request (&p, addr, len, rw, cond, 1);

  1814.       ALL_LWPS (lp)
  1815.         hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));

  1816.       ret = 0;
  1817.     }
  1818.   else
  1819.     {
  1820.       long dabr_value;
  1821.       long read_mode, write_mode;

  1822.       if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
  1823.         {
  1824.           /* PowerPC 440 requires only the read/write flags to be passed
  1825.              to the kernel.  */
  1826.           read_mode = 1;
  1827.           write_mode = 2;
  1828.         }
  1829.       else
  1830.         {
  1831.           /* PowerPC 970 and other DABR-based processors are required to pass
  1832.              the Breakpoint Translation bit together with the flags.  */
  1833.           read_mode = 5;
  1834.           write_mode = 6;
  1835.         }

  1836.       dabr_value = addr & ~(read_mode | write_mode);
  1837.       switch (rw)
  1838.         {
  1839.           case hw_read:
  1840.             /* Set read and translate bits.  */
  1841.             dabr_value |= read_mode;
  1842.             break;
  1843.           case hw_write:
  1844.             /* Set write and translate bits.  */
  1845.             dabr_value |= write_mode;
  1846.             break;
  1847.           case hw_access:
  1848.             /* Set read, write and translate bits.  */
  1849.             dabr_value |= read_mode | write_mode;
  1850.             break;
  1851.         }

  1852.       saved_dabr_value = dabr_value;

  1853.       ALL_LWPS (lp)
  1854.         if (ptrace (PTRACE_SET_DEBUGREG, ptid_get_lwp (lp->ptid), 0,
  1855.                     saved_dabr_value) < 0)
  1856.           return -1;

  1857.       ret = 0;
  1858.     }

  1859.   return ret;
  1860. }

  1861. static int
  1862. ppc_linux_remove_watchpoint (struct target_ops *self,
  1863.                              CORE_ADDR addr, int len, int rw,
  1864.                              struct expression *cond)
  1865. {
  1866.   struct lwp_info *lp;
  1867.   int ret = -1;

  1868.   if (have_ptrace_hwdebug_interface ())
  1869.     {
  1870.       struct ppc_hw_breakpoint p;

  1871.       create_watchpoint_request (&p, addr, len, rw, cond, 0);

  1872.       ALL_LWPS (lp)
  1873.         hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));

  1874.       ret = 0;
  1875.     }
  1876.   else
  1877.     {
  1878.       saved_dabr_value = 0;
  1879.       ALL_LWPS (lp)
  1880.         if (ptrace (PTRACE_SET_DEBUGREG, ptid_get_lwp (lp->ptid), 0,
  1881.                     saved_dabr_value) < 0)
  1882.           return -1;

  1883.       ret = 0;
  1884.     }

  1885.   return ret;
  1886. }

  1887. static void
  1888. ppc_linux_new_thread (struct lwp_info *lp)
  1889. {
  1890.   int tid = ptid_get_lwp (lp->ptid);

  1891.   if (have_ptrace_hwdebug_interface ())
  1892.     {
  1893.       int i;
  1894.       struct thread_points *p;
  1895.       struct hw_break_tuple *hw_breaks;

  1896.       if (VEC_empty (thread_points_p, ppc_threads))
  1897.         return;

  1898.       /* Get a list of breakpoints from any thread.  */
  1899.       p = VEC_last (thread_points_p, ppc_threads);
  1900.       hw_breaks = p->hw_breaks;

  1901.       /* Copy that thread's breakpoints and watchpoints to the new thread.  */
  1902.       for (i = 0; i < max_slots_number; i++)
  1903.         if (hw_breaks[i].hw_break)
  1904.           {
  1905.             /* Older kernels did not make new threads inherit their parent
  1906.                thread's debug state, so we always clear the slot and replicate
  1907.                the debug state ourselves, ensuring compatibility with all
  1908.                kernels.  */

  1909.             /* The ppc debug resource accounting is done through "slots".
  1910.                Ask the kernel the deallocate this specific *point's slot.  */
  1911.             ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot);

  1912.             hwdebug_insert_point (hw_breaks[i].hw_break, tid);
  1913.           }
  1914.     }
  1915.   else
  1916.     ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
  1917. }

  1918. static void
  1919. ppc_linux_thread_exit (struct thread_info *tp, int silent)
  1920. {
  1921.   int i;
  1922.   int tid = ptid_get_lwp (tp->ptid);
  1923.   struct hw_break_tuple *hw_breaks;
  1924.   struct thread_points *t = NULL, *p;

  1925.   if (!have_ptrace_hwdebug_interface ())
  1926.     return;

  1927.   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
  1928.     if (p->tid == tid)
  1929.       {
  1930.         t = p;
  1931.         break;
  1932.       }

  1933.   if (t == NULL)
  1934.     return;

  1935.   VEC_unordered_remove (thread_points_p, ppc_threads, i);

  1936.   hw_breaks = t->hw_breaks;

  1937.   for (i = 0; i < max_slots_number; i++)
  1938.     if (hw_breaks[i].hw_break)
  1939.       xfree (hw_breaks[i].hw_break);

  1940.   xfree (t->hw_breaks);
  1941.   xfree (t);
  1942. }

  1943. static int
  1944. ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
  1945. {
  1946.   siginfo_t siginfo;

  1947.   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  1948.     return 0;

  1949.   if (siginfo.si_signo != SIGTRAP
  1950.       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  1951.     return 0;

  1952.   if (have_ptrace_hwdebug_interface ())
  1953.     {
  1954.       int i;
  1955.       struct thread_points *t;
  1956.       struct hw_break_tuple *hw_breaks;
  1957.       /* The index (or slot) of the *point is passed in the si_errno field.  */
  1958.       int slot = siginfo.si_errno;

  1959.       t = hwdebug_find_thread_points_by_tid (ptid_get_lwp (inferior_ptid), 0);

  1960.       /* Find out if this *point is a hardware breakpoint.
  1961.          If so, we should return 0.  */
  1962.       if (t)
  1963.         {
  1964.           hw_breaks = t->hw_breaks;
  1965.           for (i = 0; i < max_slots_number; i++)
  1966.            if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
  1967.                && hw_breaks[i].hw_break->trigger_type
  1968.                     == PPC_BREAKPOINT_TRIGGER_EXECUTE)
  1969.              return 0;
  1970.         }
  1971.     }

  1972.   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
  1973.   return 1;
  1974. }

  1975. static int
  1976. ppc_linux_stopped_by_watchpoint (struct target_ops *ops)
  1977. {
  1978.   CORE_ADDR addr;
  1979.   return ppc_linux_stopped_data_address (ops, &addr);
  1980. }

  1981. static int
  1982. ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
  1983.                                         CORE_ADDR addr,
  1984.                                         CORE_ADDR start, int length)
  1985. {
  1986.   int mask;

  1987.   if (have_ptrace_hwdebug_interface ()
  1988.       && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
  1989.     return start <= addr && start + length >= addr;
  1990.   else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
  1991.     mask = 3;
  1992.   else
  1993.     mask = 7;

  1994.   addr &= ~mask;

  1995.   /* Check whether [start, start+length-1] intersects [addr, addr+mask].  */
  1996.   return start <= addr + mask && start + length - 1 >= addr;
  1997. }

  1998. /* Return the number of registers needed for a masked hardware watchpoint.  */

  1999. static int
  2000. ppc_linux_masked_watch_num_registers (struct target_ops *target,
  2001.                                       CORE_ADDR addr, CORE_ADDR mask)
  2002. {
  2003.   if (!have_ptrace_hwdebug_interface ()
  2004.            || (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
  2005.     return -1;
  2006.   else if ((mask & 0xC0000000) != 0xC0000000)
  2007.     {
  2008.       warning (_("The given mask covers kernel address space "
  2009.                  "and cannot be used.\n"));

  2010.       return -2;
  2011.     }
  2012.   else
  2013.     return 2;
  2014. }

  2015. static void
  2016. ppc_linux_store_inferior_registers (struct target_ops *ops,
  2017.                                     struct regcache *regcache, int regno)
  2018. {
  2019.   /* Overload thread id onto process id.  */
  2020.   int tid = ptid_get_lwp (inferior_ptid);

  2021.   /* No thread id, just use process id.  */
  2022.   if (tid == 0)
  2023.     tid = ptid_get_pid (inferior_ptid);

  2024.   if (regno >= 0)
  2025.     store_register (regcache, tid, regno);
  2026.   else
  2027.     store_ppc_registers (regcache, tid);
  2028. }

  2029. /* Functions for transferring registers between a gregset_t or fpregset_t
  2030.    (see sys/ucontext.h) and gdb's regcache.  The word size is that used
  2031.    by the ptrace interface, not the current program's ABI.  Eg. if a
  2032.    powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
  2033.    read or write 64-bit gregsets.  This is to suit the host libthread_db.  */

  2034. void
  2035. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  2036. {
  2037.   const struct regset *regset = ppc_linux_gregset (sizeof (long));

  2038.   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
  2039. }

  2040. void
  2041. fill_gregset (const struct regcache *regcache,
  2042.               gdb_gregset_t *gregsetp, int regno)
  2043. {
  2044.   const struct regset *regset = ppc_linux_gregset (sizeof (long));

  2045.   if (regno == -1)
  2046.     memset (gregsetp, 0, sizeof (*gregsetp));
  2047.   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
  2048. }

  2049. void
  2050. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
  2051. {
  2052.   const struct regset *regset = ppc_linux_fpregset ();

  2053.   ppc_supply_fpregset (regset, regcache, -1,
  2054.                        fpregsetp, sizeof (*fpregsetp));
  2055. }

  2056. void
  2057. fill_fpregset (const struct regcache *regcache,
  2058.                gdb_fpregset_t *fpregsetp, int regno)
  2059. {
  2060.   const struct regset *regset = ppc_linux_fpregset ();

  2061.   ppc_collect_fpregset (regset, regcache, regno,
  2062.                         fpregsetp, sizeof (*fpregsetp));
  2063. }

  2064. static int
  2065. ppc_linux_target_wordsize (void)
  2066. {
  2067.   int wordsize = 4;

  2068.   /* Check for 64-bit inferior process.  This is the case when the host is
  2069.      64-bit, and in addition the top bit of the MSR register is set.  */
  2070. #ifdef __powerpc64__
  2071.   long msr;

  2072.   int tid = ptid_get_lwp (inferior_ptid);
  2073.   if (tid == 0)
  2074.     tid = ptid_get_pid (inferior_ptid);

  2075.   errno = 0;
  2076.   msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
  2077.   if (errno == 0 && msr < 0)
  2078.     wordsize = 8;
  2079. #endif

  2080.   return wordsize;
  2081. }

  2082. static int
  2083. ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
  2084.                       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  2085. {
  2086.   int sizeof_auxv_field = ppc_linux_target_wordsize ();
  2087.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  2088.   gdb_byte *ptr = *readptr;

  2089.   if (endptr == ptr)
  2090.     return 0;

  2091.   if (endptr - ptr < sizeof_auxv_field * 2)
  2092.     return -1;

  2093.   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  2094.   ptr += sizeof_auxv_field;
  2095.   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  2096.   ptr += sizeof_auxv_field;

  2097.   *readptr = ptr;
  2098.   return 1;
  2099. }

  2100. static const struct target_desc *
  2101. ppc_linux_read_description (struct target_ops *ops)
  2102. {
  2103.   int altivec = 0;
  2104.   int vsx = 0;
  2105.   int isa205 = 0;
  2106.   int cell = 0;

  2107.   int tid = ptid_get_lwp (inferior_ptid);
  2108.   if (tid == 0)
  2109.     tid = ptid_get_pid (inferior_ptid);

  2110.   if (have_ptrace_getsetevrregs)
  2111.     {
  2112.       struct gdb_evrregset_t evrregset;

  2113.       if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
  2114.         return tdesc_powerpc_e500l;

  2115.       /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
  2116.          Anything else needs to be reported.  */
  2117.       else if (errno != EIO)
  2118.         perror_with_name (_("Unable to fetch SPE registers"));
  2119.     }

  2120.   if (have_ptrace_getsetvsxregs)
  2121.     {
  2122.       gdb_vsxregset_t vsxregset;

  2123.       if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
  2124.         vsx = 1;

  2125.       /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
  2126.          Anything else needs to be reported.  */
  2127.       else if (errno != EIO)
  2128.         perror_with_name (_("Unable to fetch VSX registers"));
  2129.     }

  2130.   if (have_ptrace_getvrregs)
  2131.     {
  2132.       gdb_vrregset_t vrregset;

  2133.       if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
  2134.         altivec = 1;

  2135.       /* EIO means that the PTRACE_GETVRREGS request isn't supported.
  2136.          Anything else needs to be reported.  */
  2137.       else if (errno != EIO)
  2138.         perror_with_name (_("Unable to fetch AltiVec registers"));
  2139.     }

  2140.   /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
  2141.      the FPSCR from 32 bits to 64 bits.  Even though Power 7 supports this
  2142.      ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
  2143.      PPC_FEATURE_ARCH_2_06.  Since for now the only bits used in the higher
  2144.      half of the register are for Decimal Floating Point, we check if that
  2145.      feature is available to decide the size of the FPSCR.  */
  2146.   if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
  2147.     isa205 = 1;

  2148.   if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
  2149.     cell = 1;

  2150.   if (ppc_linux_target_wordsize () == 8)
  2151.     {
  2152.       if (cell)
  2153.         return tdesc_powerpc_cell64l;
  2154.       else if (vsx)
  2155.         return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
  2156.       else if (altivec)
  2157.         return isa205
  2158.           ? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;

  2159.       return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
  2160.     }

  2161.   if (cell)
  2162.     return tdesc_powerpc_cell32l;
  2163.   else if (vsx)
  2164.     return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
  2165.   else if (altivec)
  2166.     return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;

  2167.   return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
  2168. }

  2169. void _initialize_ppc_linux_nat (void);

  2170. void
  2171. _initialize_ppc_linux_nat (void)
  2172. {
  2173.   struct target_ops *t;

  2174.   /* Fill in the generic GNU/Linux methods.  */
  2175.   t = linux_target ();

  2176.   /* Add our register access methods.  */
  2177.   t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
  2178.   t->to_store_registers = ppc_linux_store_inferior_registers;

  2179.   /* Add our breakpoint/watchpoint methods.  */
  2180.   t->to_can_use_hw_breakpoint = ppc_linux_can_use_hw_breakpoint;
  2181.   t->to_insert_hw_breakpoint = ppc_linux_insert_hw_breakpoint;
  2182.   t->to_remove_hw_breakpoint = ppc_linux_remove_hw_breakpoint;
  2183.   t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
  2184.   t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
  2185.   t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
  2186.   t->to_insert_mask_watchpoint = ppc_linux_insert_mask_watchpoint;
  2187.   t->to_remove_mask_watchpoint = ppc_linux_remove_mask_watchpoint;
  2188.   t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
  2189.   t->to_stopped_data_address = ppc_linux_stopped_data_address;
  2190.   t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
  2191.   t->to_can_accel_watchpoint_condition
  2192.     = ppc_linux_can_accel_watchpoint_condition;
  2193.   t->to_masked_watch_num_registers = ppc_linux_masked_watch_num_registers;
  2194.   t->to_ranged_break_num_registers = ppc_linux_ranged_break_num_registers;

  2195.   t->to_read_description = ppc_linux_read_description;
  2196.   t->to_auxv_parse = ppc_linux_auxv_parse;

  2197.   observer_attach_thread_exit (ppc_linux_thread_exit);

  2198.   /* Register the target.  */
  2199.   linux_nat_add_target (t);
  2200.   linux_nat_set_new_thread (t, ppc_linux_new_thread);
  2201. }