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