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