2010-05-11 Pierre Muller <muller@ics.u-strasbg.fr>
[platform/upstream/binutils.git] / gdb / ppc-linux-nat.c
1 /* PPC GNU/Linux native support.
2
3    Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003,
4    2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "observer.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "gdbthread.h"
27 #include "gdbcore.h"
28 #include "regcache.h"
29 #include "gdb_assert.h"
30 #include "target.h"
31 #include "linux-nat.h"
32
33 #include <stdint.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <signal.h>
37 #include <sys/user.h>
38 #include <sys/ioctl.h>
39 #include "gdb_wait.h"
40 #include <fcntl.h>
41 #include <sys/procfs.h>
42 #include <sys/ptrace.h>
43
44 /* Prototypes for supply_gregset etc. */
45 #include "gregset.h"
46 #include "ppc-tdep.h"
47 #include "ppc-linux-tdep.h"
48
49 /* Required when using the AUXV.  */
50 #include "elf/common.h"
51 #include "auxv.h"
52
53 /* This sometimes isn't defined.  */
54 #ifndef PT_ORIG_R3
55 #define PT_ORIG_R3 34
56 #endif
57 #ifndef PT_TRAP
58 #define PT_TRAP 40
59 #endif
60
61 /* The PPC_FEATURE_* defines should be provided by <asm/cputable.h>.
62    If they aren't, we can provide them ourselves (their values are fixed
63    because they are part of the kernel ABI).  They are used in the AT_HWCAP
64    entry of the AUXV.  */
65 #ifndef PPC_FEATURE_CELL
66 #define PPC_FEATURE_CELL 0x00010000
67 #endif
68 #ifndef PPC_FEATURE_BOOKE
69 #define PPC_FEATURE_BOOKE 0x00008000
70 #endif
71 #ifndef PPC_FEATURE_HAS_DFP
72 #define PPC_FEATURE_HAS_DFP     0x00000400  /* Decimal Floating Point.  */
73 #endif
74
75 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
76    configure time check.  Some older glibc's (for instance 2.2.1)
77    don't have a specific powerpc version of ptrace.h, and fall back on
78    a generic one.  In such cases, sys/ptrace.h defines
79    PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
80    ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
81    PTRACE_SETVRREGS to be.  This also makes a configury check pretty
82    much useless.  */
83
84 /* These definitions should really come from the glibc header files,
85    but Glibc doesn't know about the vrregs yet.  */
86 #ifndef PTRACE_GETVRREGS
87 #define PTRACE_GETVRREGS 18
88 #define PTRACE_SETVRREGS 19
89 #endif
90
91 /* PTRACE requests for POWER7 VSX registers.  */
92 #ifndef PTRACE_GETVSXREGS
93 #define PTRACE_GETVSXREGS 27
94 #define PTRACE_SETVSXREGS 28
95 #endif
96
97 /* Similarly for the ptrace requests for getting / setting the SPE
98    registers (ev0 -- ev31, acc, and spefscr).  See the description of
99    gdb_evrregset_t for details.  */
100 #ifndef PTRACE_GETEVRREGS
101 #define PTRACE_GETEVRREGS 20
102 #define PTRACE_SETEVRREGS 21
103 #endif
104
105 /* Similarly for the hardware watchpoint support.  These requests are used
106    when the BookE kernel interface is not available.  */
107 #ifndef PTRACE_GET_DEBUGREG
108 #define PTRACE_GET_DEBUGREG    25
109 #endif
110 #ifndef PTRACE_SET_DEBUGREG
111 #define PTRACE_SET_DEBUGREG    26
112 #endif
113 #ifndef PTRACE_GETSIGINFO
114 #define PTRACE_GETSIGINFO    0x4202
115 #endif
116
117 /* These requests are used when the BookE kernel interface is available.
118    It exposes the additional debug features of BookE processors, such as
119    ranged breakpoints and watchpoints and hardware-accelerated condition
120    evaluation.  */
121 #ifndef PPC_PTRACE_GETHWDBGINFO
122
123 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the new BookE
124    interface is not present in ptrace.h, so we'll have to pretty much include
125    it all here so that the code at least compiles on older systems.  */
126 #define PPC_PTRACE_GETHWDBGINFO 0x89
127 #define PPC_PTRACE_SETHWDEBUG   0x88
128 #define PPC_PTRACE_DELHWDEBUG   0x87
129
130 struct ppc_debug_info
131 {
132         uint32_t version;               /* Only version 1 exists to date */
133         uint32_t num_instruction_bps;
134         uint32_t num_data_bps;
135         uint32_t num_condition_regs;
136         uint32_t data_bp_alignment;
137         uint32_t sizeof_condition;      /* size of the DVC register */
138         uint64_t features;
139 };
140
141 /* Features will have bits indicating whether there is support for:  */
142 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE         0x1
143 #define PPC_DEBUG_FEATURE_INSN_BP_MASK          0x2
144 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE         0x4
145 #define PPC_DEBUG_FEATURE_DATA_BP_MASK          0x8
146
147 struct ppc_hw_breakpoint
148 {
149         uint32_t version;               /* currently, version must be 1 */
150         uint32_t trigger_type;          /* only some combinations allowed */
151         uint32_t addr_mode;             /* address match mode */
152         uint32_t condition_mode;        /* break/watchpoint condition flags */
153         uint64_t addr;                  /* break/watchpoint address */
154         uint64_t addr2;                 /* range end or mask */
155         uint64_t condition_value;       /* contents of the DVC register */
156 };
157
158 /* Trigger type.  */
159 #define PPC_BREAKPOINT_TRIGGER_EXECUTE  0x1
160 #define PPC_BREAKPOINT_TRIGGER_READ     0x2
161 #define PPC_BREAKPOINT_TRIGGER_WRITE    0x4
162 #define PPC_BREAKPOINT_TRIGGER_RW       0x6
163
164 /* Address mode.  */
165 #define PPC_BREAKPOINT_MODE_EXACT               0x0
166 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE     0x1
167 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE     0x2
168 #define PPC_BREAKPOINT_MODE_MASK                0x3
169
170 /* Condition mode.  */
171 #define PPC_BREAKPOINT_CONDITION_NONE   0x0
172 #define PPC_BREAKPOINT_CONDITION_AND    0x1
173 #define PPC_BREAKPOINT_CONDITION_EXACT  0x1
174 #define PPC_BREAKPOINT_CONDITION_OR     0x2
175 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
176 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
177 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT       16
178 #define PPC_BREAKPOINT_CONDITION_BE(n)  \
179         (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
180 #endif /* PPC_PTRACE_GETHWDBGINFO */
181
182
183
184 /* Similarly for the general-purpose (gp0 -- gp31)
185    and floating-point registers (fp0 -- fp31).  */
186 #ifndef PTRACE_GETREGS
187 #define PTRACE_GETREGS 12
188 #endif
189 #ifndef PTRACE_SETREGS
190 #define PTRACE_SETREGS 13
191 #endif
192 #ifndef PTRACE_GETFPREGS
193 #define PTRACE_GETFPREGS 14
194 #endif
195 #ifndef PTRACE_SETFPREGS
196 #define PTRACE_SETFPREGS 15
197 #endif
198
199 /* This oddity is because the Linux kernel defines elf_vrregset_t as
200    an array of 33 16 bytes long elements.  I.e. it leaves out vrsave.
201    However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
202    the vrsave as an extra 4 bytes at the end.  I opted for creating a
203    flat array of chars, so that it is easier to manipulate for gdb.
204
205    There are 32 vector registers 16 bytes longs, plus a VSCR register
206    which is only 4 bytes long, but is fetched as a 16 bytes
207    quantity. Up to here we have the elf_vrregset_t structure.
208    Appended to this there is space for the VRSAVE register: 4 bytes.
209    Even though this vrsave register is not included in the regset
210    typedef, it is handled by the ptrace requests.
211
212    Note that GNU/Linux doesn't support little endian PPC hardware,
213    therefore the offset at which the real value of the VSCR register
214    is located will be always 12 bytes.
215
216    The layout is like this (where x is the actual value of the vscr reg): */
217
218 /* *INDENT-OFF* */
219 /*
220    |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
221    <------->     <-------><-------><->
222      VR0           VR31     VSCR    VRSAVE
223 */
224 /* *INDENT-ON* */
225
226 #define SIZEOF_VRREGS 33*16+4
227
228 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
229
230 /* This is the layout of the POWER7 VSX registers and the way they overlap
231    with the existing FPR and VMX registers.
232
233                     VSR doubleword 0               VSR doubleword 1
234            ----------------------------------------------------------------
235    VSR[0]  |             FPR[0]            |                              |
236            ----------------------------------------------------------------
237    VSR[1]  |             FPR[1]            |                              |
238            ----------------------------------------------------------------
239            |              ...              |                              |
240            |              ...              |                              |
241            ----------------------------------------------------------------
242    VSR[30] |             FPR[30]           |                              |
243            ----------------------------------------------------------------
244    VSR[31] |             FPR[31]           |                              |
245            ----------------------------------------------------------------
246    VSR[32] |                             VR[0]                            |
247            ----------------------------------------------------------------
248    VSR[33] |                             VR[1]                            |
249            ----------------------------------------------------------------
250            |                              ...                             |
251            |                              ...                             |
252            ----------------------------------------------------------------
253    VSR[62] |                             VR[30]                           |
254            ----------------------------------------------------------------
255    VSR[63] |                             VR[31]                           |
256           ----------------------------------------------------------------
257
258    VSX has 64 128bit registers.  The first 32 registers overlap with
259    the FP registers (doubleword 0) and hence extend them with additional
260    64 bits (doubleword 1).  The other 32 regs overlap with the VMX
261    registers.  */
262 #define SIZEOF_VSXREGS 32*8
263
264 typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
265
266 /* On PPC processors that support the the Signal Processing Extension
267    (SPE) APU, the general-purpose registers are 64 bits long.
268    However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
269    ptrace calls only access the lower half of each register, to allow
270    them to behave the same way they do on non-SPE systems.  There's a
271    separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
272    read and write the top halves of all the general-purpose registers
273    at once, along with some SPE-specific registers.
274
275    GDB itself continues to claim the general-purpose registers are 32
276    bits long.  It has unnamed raw registers that hold the upper halves
277    of the gprs, and the the full 64-bit SIMD views of the registers,
278    'ev0' -- 'ev31', are pseudo-registers that splice the top and
279    bottom halves together.
280
281    This is the structure filled in by PTRACE_GETEVRREGS and written to
282    the inferior's registers by PTRACE_SETEVRREGS.  */
283 struct gdb_evrregset_t
284 {
285   unsigned long evr[32];
286   unsigned long long acc;
287   unsigned long spefscr;
288 };
289
290 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
291    PTRACE_SETVSXREGS requests, for reading and writing the VSX
292    POWER7 registers 0 through 31.  Zero if we've tried one of them and
293    gotten an error.  Note that VSX registers 32 through 63 overlap
294    with VR registers 0 through 31.  */
295 int have_ptrace_getsetvsxregs = 1;
296
297 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
298    PTRACE_SETVRREGS requests, for reading and writing the Altivec
299    registers.  Zero if we've tried one of them and gotten an
300    error.  */
301 int have_ptrace_getvrregs = 1;
302
303 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
304    PTRACE_SETEVRREGS requests, for reading and writing the SPE
305    registers.  Zero if we've tried one of them and gotten an
306    error.  */
307 int have_ptrace_getsetevrregs = 1;
308
309 /* Non-zero if our kernel may support the PTRACE_GETREGS and
310    PTRACE_SETREGS requests, for reading and writing the
311    general-purpose registers.  Zero if we've tried one of
312    them and gotten an error.  */
313 int have_ptrace_getsetregs = 1;
314
315 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
316    PTRACE_SETFPREGS requests, for reading and writing the
317    floating-pointers registers.  Zero if we've tried one of
318    them and gotten an error.  */
319 int have_ptrace_getsetfpregs = 1;
320
321 /* *INDENT-OFF* */
322 /* registers layout, as presented by the ptrace interface:
323 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
324 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
325 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
326 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
327 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
328 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
329 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
330 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
331 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
332 /* *INDENT_ON * */
333
334 static int
335 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
336 {
337   int u_addr = -1;
338   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
339   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
340      interface, and not the wordsize of the program's ABI.  */
341   int wordsize = sizeof (long);
342
343   /* General purpose registers occupy 1 slot each in the buffer */
344   if (regno >= tdep->ppc_gp0_regnum 
345       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
346     u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
347
348   /* Floating point regs: eight bytes each in both 32- and 64-bit
349      ptrace interfaces.  Thus, two slots each in 32-bit interface, one
350      slot each in 64-bit interface.  */
351   if (tdep->ppc_fp0_regnum >= 0
352       && regno >= tdep->ppc_fp0_regnum
353       && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
354     u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
355
356   /* UISA special purpose registers: 1 slot each */
357   if (regno == gdbarch_pc_regnum (gdbarch))
358     u_addr = PT_NIP * wordsize;
359   if (regno == tdep->ppc_lr_regnum)
360     u_addr = PT_LNK * wordsize;
361   if (regno == tdep->ppc_cr_regnum)
362     u_addr = PT_CCR * wordsize;
363   if (regno == tdep->ppc_xer_regnum)
364     u_addr = PT_XER * wordsize;
365   if (regno == tdep->ppc_ctr_regnum)
366     u_addr = PT_CTR * wordsize;
367 #ifdef PT_MQ
368   if (regno == tdep->ppc_mq_regnum)
369     u_addr = PT_MQ * wordsize;
370 #endif
371   if (regno == tdep->ppc_ps_regnum)
372     u_addr = PT_MSR * wordsize;
373   if (regno == PPC_ORIG_R3_REGNUM)
374     u_addr = PT_ORIG_R3 * wordsize;
375   if (regno == PPC_TRAP_REGNUM)
376     u_addr = PT_TRAP * wordsize;
377   if (tdep->ppc_fpscr_regnum >= 0
378       && regno == tdep->ppc_fpscr_regnum)
379     {
380       /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
381          kernel headers incorrectly contained the 32-bit definition of
382          PT_FPSCR.  For the 32-bit definition, floating-point
383          registers occupy two 32-bit "slots", and the FPSCR lives in
384          the second half of such a slot-pair (hence +1).  For 64-bit,
385          the FPSCR instead occupies the full 64-bit 2-word-slot and
386          hence no adjustment is necessary.  Hack around this.  */
387       if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
388         u_addr = (48 + 32) * wordsize;
389       /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
390          slot and not just its second word.  The PT_FPSCR supplied when
391          GDB is compiled as a 32-bit app doesn't reflect this.  */
392       else if (wordsize == 4 && register_size (gdbarch, regno) == 8
393                && PT_FPSCR == (48 + 2*32 + 1))
394         u_addr = (48 + 2*32) * wordsize;
395       else
396         u_addr = PT_FPSCR * wordsize;
397     }
398   return u_addr;
399 }
400
401 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
402    registers set mechanism, as opposed to the interface for all the
403    other registers, that stores/fetches each register individually.  */
404 static void
405 fetch_vsx_register (struct regcache *regcache, int tid, int regno)
406 {
407   int ret;
408   gdb_vsxregset_t regs;
409   struct gdbarch *gdbarch = get_regcache_arch (regcache);
410   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
411   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
412
413   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
414   if (ret < 0)
415     {
416       if (errno == EIO)
417         {
418           have_ptrace_getsetvsxregs = 0;
419           return;
420         }
421       perror_with_name (_("Unable to fetch VSX register"));
422     }
423
424   regcache_raw_supply (regcache, regno,
425                        regs + (regno - tdep->ppc_vsr0_upper_regnum)
426                        * vsxregsize);
427 }
428
429 /* The Linux kernel ptrace interface for AltiVec registers uses the
430    registers set mechanism, as opposed to the interface for all the
431    other registers, that stores/fetches each register individually.  */
432 static void
433 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
434 {
435   int ret;
436   int offset = 0;
437   gdb_vrregset_t regs;
438   struct gdbarch *gdbarch = get_regcache_arch (regcache);
439   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
440   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
441
442   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
443   if (ret < 0)
444     {
445       if (errno == EIO)
446         {
447           have_ptrace_getvrregs = 0;
448           return;
449         }
450       perror_with_name (_("Unable to fetch AltiVec register"));
451     }
452  
453   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
454      long on the hardware.  We deal only with the lower 4 bytes of the
455      vector.  VRSAVE is at the end of the array in a 4 bytes slot, so
456      there is no need to define an offset for it.  */
457   if (regno == (tdep->ppc_vrsave_regnum - 1))
458     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
459   
460   regcache_raw_supply (regcache, regno,
461                        regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
462 }
463
464 /* Fetch the top 32 bits of TID's general-purpose registers and the
465    SPE-specific registers, and place the results in EVRREGSET.  If we
466    don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
467    zeros.
468
469    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
470    PTRACE_SETEVRREGS requests are supported is isolated here, and in
471    set_spe_registers.  */
472 static void
473 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
474 {
475   if (have_ptrace_getsetevrregs)
476     {
477       if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
478         return;
479       else
480         {
481           /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
482              we just return zeros.  */
483           if (errno == EIO)
484             have_ptrace_getsetevrregs = 0;
485           else
486             /* Anything else needs to be reported.  */
487             perror_with_name (_("Unable to fetch SPE registers"));
488         }
489     }
490
491   memset (evrregset, 0, sizeof (*evrregset));
492 }
493
494 /* Supply values from TID for SPE-specific raw registers: the upper
495    halves of the GPRs, the accumulator, and the spefscr.  REGNO must
496    be the number of an upper half register, acc, spefscr, or -1 to
497    supply the values of all registers.  */
498 static void
499 fetch_spe_register (struct regcache *regcache, int tid, int regno)
500 {
501   struct gdbarch *gdbarch = get_regcache_arch (regcache);
502   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
503   struct gdb_evrregset_t evrregs;
504
505   gdb_assert (sizeof (evrregs.evr[0])
506               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
507   gdb_assert (sizeof (evrregs.acc)
508               == register_size (gdbarch, tdep->ppc_acc_regnum));
509   gdb_assert (sizeof (evrregs.spefscr)
510               == register_size (gdbarch, tdep->ppc_spefscr_regnum));
511
512   get_spe_registers (tid, &evrregs);
513
514   if (regno == -1)
515     {
516       int i;
517
518       for (i = 0; i < ppc_num_gprs; i++)
519         regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
520                              &evrregs.evr[i]);
521     }
522   else if (tdep->ppc_ev0_upper_regnum <= regno
523            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
524     regcache_raw_supply (regcache, regno,
525                          &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
526
527   if (regno == -1
528       || regno == tdep->ppc_acc_regnum)
529     regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
530
531   if (regno == -1
532       || regno == tdep->ppc_spefscr_regnum)
533     regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
534                          &evrregs.spefscr);
535 }
536
537 static void
538 fetch_register (struct regcache *regcache, int tid, int regno)
539 {
540   struct gdbarch *gdbarch = get_regcache_arch (regcache);
541   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
542   /* This isn't really an address.  But ptrace thinks of it as one.  */
543   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
544   int bytes_transferred;
545   unsigned int offset;         /* Offset of registers within the u area. */
546   char buf[MAX_REGISTER_SIZE];
547
548   if (altivec_register_p (gdbarch, regno))
549     {
550       /* If this is the first time through, or if it is not the first
551          time through, and we have comfirmed that there is kernel
552          support for such a ptrace request, then go and fetch the
553          register.  */
554       if (have_ptrace_getvrregs)
555        {
556          fetch_altivec_register (regcache, tid, regno);
557          return;
558        }
559      /* If we have discovered that there is no ptrace support for
560         AltiVec registers, fall through and return zeroes, because
561         regaddr will be -1 in this case.  */
562     }
563   if (vsx_register_p (gdbarch, regno))
564     {
565       if (have_ptrace_getsetvsxregs)
566         {
567           fetch_vsx_register (regcache, tid, regno);
568           return;
569         }
570     }
571   else if (spe_register_p (gdbarch, regno))
572     {
573       fetch_spe_register (regcache, tid, regno);
574       return;
575     }
576
577   if (regaddr == -1)
578     {
579       memset (buf, '\0', register_size (gdbarch, regno));   /* Supply zeroes */
580       regcache_raw_supply (regcache, regno, buf);
581       return;
582     }
583
584   /* Read the raw register using sizeof(long) sized chunks.  On a
585      32-bit platform, 64-bit floating-point registers will require two
586      transfers.  */
587   for (bytes_transferred = 0;
588        bytes_transferred < register_size (gdbarch, regno);
589        bytes_transferred += sizeof (long))
590     {
591       errno = 0;
592       *(long *) &buf[bytes_transferred]
593         = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
594       regaddr += sizeof (long);
595       if (errno != 0)
596         {
597           char message[128];
598           sprintf (message, "reading register %s (#%d)", 
599                    gdbarch_register_name (gdbarch, regno), regno);
600           perror_with_name (message);
601         }
602     }
603
604   /* Now supply the register.  Keep in mind that the regcache's idea
605      of the register's size may not be a multiple of sizeof
606      (long).  */
607   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
608     {
609       /* Little-endian values are always found at the left end of the
610          bytes transferred.  */
611       regcache_raw_supply (regcache, regno, buf);
612     }
613   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
614     {
615       /* Big-endian values are found at the right end of the bytes
616          transferred.  */
617       size_t padding = (bytes_transferred - register_size (gdbarch, regno));
618       regcache_raw_supply (regcache, regno, buf + padding);
619     }
620   else 
621     internal_error (__FILE__, __LINE__,
622                     _("fetch_register: unexpected byte order: %d"),
623                     gdbarch_byte_order (gdbarch));
624 }
625
626 static void
627 supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
628 {
629   int i;
630   struct gdbarch *gdbarch = get_regcache_arch (regcache);
631   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
632   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
633
634   for (i = 0; i < ppc_num_vshrs; i++)
635     {
636         regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
637                              *vsxregsetp + i * vsxregsize);
638     }
639 }
640
641 static void
642 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
643 {
644   int i;
645   struct gdbarch *gdbarch = get_regcache_arch (regcache);
646   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
647   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
648   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
649   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
650
651   for (i = 0; i < num_of_vrregs; i++)
652     {
653       /* The last 2 registers of this set are only 32 bit long, not
654          128.  However an offset is necessary only for VSCR because it
655          occupies a whole vector, while VRSAVE occupies a full 4 bytes
656          slot.  */
657       if (i == (num_of_vrregs - 2))
658         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
659                              *vrregsetp + i * vrregsize + offset);
660       else
661         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
662                              *vrregsetp + i * vrregsize);
663     }
664 }
665
666 static void
667 fetch_vsx_registers (struct regcache *regcache, int tid)
668 {
669   int ret;
670   gdb_vsxregset_t regs;
671
672   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
673   if (ret < 0)
674     {
675       if (errno == EIO)
676         {
677           have_ptrace_getsetvsxregs = 0;
678           return;
679         }
680       perror_with_name (_("Unable to fetch VSX registers"));
681     }
682   supply_vsxregset (regcache, &regs);
683 }
684
685 static void
686 fetch_altivec_registers (struct regcache *regcache, int tid)
687 {
688   int ret;
689   gdb_vrregset_t regs;
690   
691   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
692   if (ret < 0)
693     {
694       if (errno == EIO)
695         {
696           have_ptrace_getvrregs = 0;
697           return;
698         }
699       perror_with_name (_("Unable to fetch AltiVec registers"));
700     }
701   supply_vrregset (regcache, &regs);
702 }
703
704 /* This function actually issues the request to ptrace, telling
705    it to get all general-purpose registers and put them into the
706    specified regset.
707    
708    If the ptrace request does not exist, this function returns 0
709    and properly sets the have_ptrace_* flag.  If the request fails,
710    this function calls perror_with_name.  Otherwise, if the request
711    succeeds, then the regcache gets filled and 1 is returned.  */
712 static int
713 fetch_all_gp_regs (struct regcache *regcache, int tid)
714 {
715   struct gdbarch *gdbarch = get_regcache_arch (regcache);
716   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
717   gdb_gregset_t gregset;
718
719   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
720     {
721       if (errno == EIO)
722         {
723           have_ptrace_getsetregs = 0;
724           return 0;
725         }
726       perror_with_name (_("Couldn't get general-purpose registers."));
727     }
728
729   supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
730
731   return 1;
732 }
733
734 /* This is a wrapper for the fetch_all_gp_regs function.  It is
735    responsible for verifying if this target has the ptrace request
736    that can be used to fetch all general-purpose registers at one
737    shot.  If it doesn't, then we should fetch them using the
738    old-fashioned way, which is to iterate over the registers and
739    request them one by one.  */
740 static void
741 fetch_gp_regs (struct regcache *regcache, int tid)
742 {
743   struct gdbarch *gdbarch = get_regcache_arch (regcache);
744   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
745   int i;
746
747   if (have_ptrace_getsetregs)
748     if (fetch_all_gp_regs (regcache, tid))
749       return;
750
751   /* If we've hit this point, it doesn't really matter which
752      architecture we are using.  We just need to read the
753      registers in the "old-fashioned way".  */
754   for (i = 0; i < ppc_num_gprs; i++)
755     fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
756 }
757
758 /* This function actually issues the request to ptrace, telling
759    it to get all floating-point registers and put them into the
760    specified regset.
761    
762    If the ptrace request does not exist, this function returns 0
763    and properly sets the have_ptrace_* flag.  If the request fails,
764    this function calls perror_with_name.  Otherwise, if the request
765    succeeds, then the regcache gets filled and 1 is returned.  */
766 static int
767 fetch_all_fp_regs (struct regcache *regcache, int tid)
768 {
769   gdb_fpregset_t fpregs;
770
771   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
772     {
773       if (errno == EIO)
774         {
775           have_ptrace_getsetfpregs = 0;
776           return 0;
777         }
778       perror_with_name (_("Couldn't get floating-point registers."));
779     }
780
781   supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
782
783   return 1;
784 }
785
786 /* This is a wrapper for the fetch_all_fp_regs function.  It is
787    responsible for verifying if this target has the ptrace request
788    that can be used to fetch all floating-point registers at one
789    shot.  If it doesn't, then we should fetch them using the
790    old-fashioned way, which is to iterate over the registers and
791    request them one by one.  */
792 static void
793 fetch_fp_regs (struct regcache *regcache, int tid)
794 {
795   struct gdbarch *gdbarch = get_regcache_arch (regcache);
796   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
797   int i;
798
799   if (have_ptrace_getsetfpregs)
800     if (fetch_all_fp_regs (regcache, tid))
801       return;
802  
803   /* If we've hit this point, it doesn't really matter which
804      architecture we are using.  We just need to read the
805      registers in the "old-fashioned way".  */
806   for (i = 0; i < ppc_num_fprs; i++)
807     fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
808 }
809
810 static void 
811 fetch_ppc_registers (struct regcache *regcache, int tid)
812 {
813   int i;
814   struct gdbarch *gdbarch = get_regcache_arch (regcache);
815   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
816
817   fetch_gp_regs (regcache, tid);
818   if (tdep->ppc_fp0_regnum >= 0)
819     fetch_fp_regs (regcache, tid);
820   fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
821   if (tdep->ppc_ps_regnum != -1)
822     fetch_register (regcache, tid, tdep->ppc_ps_regnum);
823   if (tdep->ppc_cr_regnum != -1)
824     fetch_register (regcache, tid, tdep->ppc_cr_regnum);
825   if (tdep->ppc_lr_regnum != -1)
826     fetch_register (regcache, tid, tdep->ppc_lr_regnum);
827   if (tdep->ppc_ctr_regnum != -1)
828     fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
829   if (tdep->ppc_xer_regnum != -1)
830     fetch_register (regcache, tid, tdep->ppc_xer_regnum);
831   if (tdep->ppc_mq_regnum != -1)
832     fetch_register (regcache, tid, tdep->ppc_mq_regnum);
833   if (ppc_linux_trap_reg_p (gdbarch))
834     {
835       fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
836       fetch_register (regcache, tid, PPC_TRAP_REGNUM);
837     }
838   if (tdep->ppc_fpscr_regnum != -1)
839     fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
840   if (have_ptrace_getvrregs)
841     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
842       fetch_altivec_registers (regcache, tid);
843   if (have_ptrace_getsetvsxregs)
844     if (tdep->ppc_vsr0_upper_regnum != -1)
845       fetch_vsx_registers (regcache, tid);
846   if (tdep->ppc_ev0_upper_regnum >= 0)
847     fetch_spe_register (regcache, tid, -1);
848 }
849
850 /* Fetch registers from the child process.  Fetch all registers if
851    regno == -1, otherwise fetch all general registers or all floating
852    point registers depending upon the value of regno.  */
853 static void
854 ppc_linux_fetch_inferior_registers (struct target_ops *ops,
855                                     struct regcache *regcache, int regno)
856 {
857   /* Overload thread id onto process id */
858   int tid = TIDGET (inferior_ptid);
859
860   /* No thread id, just use process id */
861   if (tid == 0)
862     tid = PIDGET (inferior_ptid);
863
864   if (regno == -1)
865     fetch_ppc_registers (regcache, tid);
866   else 
867     fetch_register (regcache, tid, regno);
868 }
869
870 /* Store one VSX register. */
871 static void
872 store_vsx_register (const struct regcache *regcache, int tid, int regno)
873 {
874   int ret;
875   gdb_vsxregset_t regs;
876   struct gdbarch *gdbarch = get_regcache_arch (regcache);
877   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
878   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
879
880   ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
881   if (ret < 0)
882     {
883       if (errno == EIO)
884         {
885           have_ptrace_getsetvsxregs = 0;
886           return;
887         }
888       perror_with_name (_("Unable to fetch VSX register"));
889     }
890
891   regcache_raw_collect (regcache, regno, regs +
892                         (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
893
894   ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
895   if (ret < 0)
896     perror_with_name (_("Unable to store VSX register"));
897 }
898
899 /* Store one register. */
900 static void
901 store_altivec_register (const struct regcache *regcache, int tid, int regno)
902 {
903   int ret;
904   int offset = 0;
905   gdb_vrregset_t regs;
906   struct gdbarch *gdbarch = get_regcache_arch (regcache);
907   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
908   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
909
910   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
911   if (ret < 0)
912     {
913       if (errno == EIO)
914         {
915           have_ptrace_getvrregs = 0;
916           return;
917         }
918       perror_with_name (_("Unable to fetch AltiVec register"));
919     }
920
921   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
922      long on the hardware.  */
923   if (regno == (tdep->ppc_vrsave_regnum - 1))
924     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
925
926   regcache_raw_collect (regcache, regno,
927                         regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
928
929   ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
930   if (ret < 0)
931     perror_with_name (_("Unable to store AltiVec register"));
932 }
933
934 /* Assuming TID referrs to an SPE process, set the top halves of TID's
935    general-purpose registers and its SPE-specific registers to the
936    values in EVRREGSET.  If we don't support PTRACE_SETEVRREGS, do
937    nothing.
938
939    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
940    PTRACE_SETEVRREGS requests are supported is isolated here, and in
941    get_spe_registers.  */
942 static void
943 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
944 {
945   if (have_ptrace_getsetevrregs)
946     {
947       if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
948         return;
949       else
950         {
951           /* EIO means that the PTRACE_SETEVRREGS request isn't
952              supported; we fail silently, and don't try the call
953              again.  */
954           if (errno == EIO)
955             have_ptrace_getsetevrregs = 0;
956           else
957             /* Anything else needs to be reported.  */
958             perror_with_name (_("Unable to set SPE registers"));
959         }
960     }
961 }
962
963 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
964    If REGNO is -1, write the values of all the SPE-specific
965    registers.  */
966 static void
967 store_spe_register (const struct regcache *regcache, int tid, int regno)
968 {
969   struct gdbarch *gdbarch = get_regcache_arch (regcache);
970   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
971   struct gdb_evrregset_t evrregs;
972
973   gdb_assert (sizeof (evrregs.evr[0])
974               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
975   gdb_assert (sizeof (evrregs.acc)
976               == register_size (gdbarch, tdep->ppc_acc_regnum));
977   gdb_assert (sizeof (evrregs.spefscr)
978               == register_size (gdbarch, tdep->ppc_spefscr_regnum));
979
980   if (regno == -1)
981     /* Since we're going to write out every register, the code below
982        should store to every field of evrregs; if that doesn't happen,
983        make it obvious by initializing it with suspicious values.  */
984     memset (&evrregs, 42, sizeof (evrregs));
985   else
986     /* We can only read and write the entire EVR register set at a
987        time, so to write just a single register, we do a
988        read-modify-write maneuver.  */
989     get_spe_registers (tid, &evrregs);
990
991   if (regno == -1)
992     {
993       int i;
994
995       for (i = 0; i < ppc_num_gprs; i++)
996         regcache_raw_collect (regcache,
997                               tdep->ppc_ev0_upper_regnum + i,
998                               &evrregs.evr[i]);
999     }
1000   else if (tdep->ppc_ev0_upper_regnum <= regno
1001            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
1002     regcache_raw_collect (regcache, regno,
1003                           &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
1004
1005   if (regno == -1
1006       || regno == tdep->ppc_acc_regnum)
1007     regcache_raw_collect (regcache,
1008                           tdep->ppc_acc_regnum,
1009                           &evrregs.acc);
1010
1011   if (regno == -1
1012       || regno == tdep->ppc_spefscr_regnum)
1013     regcache_raw_collect (regcache,
1014                           tdep->ppc_spefscr_regnum,
1015                           &evrregs.spefscr);
1016
1017   /* Write back the modified register set.  */
1018   set_spe_registers (tid, &evrregs);
1019 }
1020
1021 static void
1022 store_register (const struct regcache *regcache, int tid, int regno)
1023 {
1024   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1025   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1026   /* This isn't really an address.  But ptrace thinks of it as one.  */
1027   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
1028   int i;
1029   size_t bytes_to_transfer;
1030   char buf[MAX_REGISTER_SIZE];
1031
1032   if (altivec_register_p (gdbarch, regno))
1033     {
1034       store_altivec_register (regcache, tid, regno);
1035       return;
1036     }
1037   if (vsx_register_p (gdbarch, regno))
1038     {
1039       store_vsx_register (regcache, tid, regno);
1040       return;
1041     }
1042   else if (spe_register_p (gdbarch, regno))
1043     {
1044       store_spe_register (regcache, tid, regno);
1045       return;
1046     }
1047
1048   if (regaddr == -1)
1049     return;
1050
1051   /* First collect the register.  Keep in mind that the regcache's
1052      idea of the register's size may not be a multiple of sizeof
1053      (long).  */
1054   memset (buf, 0, sizeof buf);
1055   bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
1056   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1057     {
1058       /* Little-endian values always sit at the left end of the buffer.  */
1059       regcache_raw_collect (regcache, regno, buf);
1060     }
1061   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1062     {
1063       /* Big-endian values sit at the right end of the buffer.  */
1064       size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
1065       regcache_raw_collect (regcache, regno, buf + padding);
1066     }
1067
1068   for (i = 0; i < bytes_to_transfer; i += sizeof (long))
1069     {
1070       errno = 0;
1071       ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
1072               *(long *) &buf[i]);
1073       regaddr += sizeof (long);
1074
1075       if (errno == EIO 
1076           && (regno == tdep->ppc_fpscr_regnum
1077               || regno == PPC_ORIG_R3_REGNUM
1078               || regno == PPC_TRAP_REGNUM))
1079         {
1080           /* Some older kernel versions don't allow fpscr, orig_r3
1081              or trap to be written.  */
1082           continue;
1083         }
1084
1085       if (errno != 0)
1086         {
1087           char message[128];
1088           sprintf (message, "writing register %s (#%d)", 
1089                    gdbarch_register_name (gdbarch, regno), regno);
1090           perror_with_name (message);
1091         }
1092     }
1093 }
1094
1095 static void
1096 fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
1097 {
1098   int i;
1099   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1100   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1101   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
1102
1103   for (i = 0; i < ppc_num_vshrs; i++)
1104     regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
1105                           *vsxregsetp + i * vsxregsize);
1106 }
1107
1108 static void
1109 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
1110 {
1111   int i;
1112   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1113   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1114   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
1115   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
1116   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
1117
1118   for (i = 0; i < num_of_vrregs; i++)
1119     {
1120       /* The last 2 registers of this set are only 32 bit long, not
1121          128, but only VSCR is fetched as a 16 bytes quantity.  */
1122       if (i == (num_of_vrregs - 2))
1123         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1124                               *vrregsetp + i * vrregsize + offset);
1125       else
1126         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1127                               *vrregsetp + i * vrregsize);
1128     }
1129 }
1130
1131 static void
1132 store_vsx_registers (const struct regcache *regcache, int tid)
1133 {
1134   int ret;
1135   gdb_vsxregset_t regs;
1136
1137   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
1138   if (ret < 0)
1139     {
1140       if (errno == EIO)
1141         {
1142           have_ptrace_getsetvsxregs = 0;
1143           return;
1144         }
1145       perror_with_name (_("Couldn't get VSX registers"));
1146     }
1147
1148   fill_vsxregset (regcache, &regs);
1149
1150   if (ptrace (PTRACE_SETVSXREGS, tid, 0, &regs) < 0)
1151     perror_with_name (_("Couldn't write VSX registers"));
1152 }
1153
1154 static void
1155 store_altivec_registers (const struct regcache *regcache, int tid)
1156 {
1157   int ret;
1158   gdb_vrregset_t regs;
1159
1160   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
1161   if (ret < 0)
1162     {
1163       if (errno == EIO)
1164         {
1165           have_ptrace_getvrregs = 0;
1166           return;
1167         }
1168       perror_with_name (_("Couldn't get AltiVec registers"));
1169     }
1170
1171   fill_vrregset (regcache, &regs);
1172   
1173   if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
1174     perror_with_name (_("Couldn't write AltiVec registers"));
1175 }
1176
1177 /* This function actually issues the request to ptrace, telling
1178    it to store all general-purpose registers present in the specified
1179    regset.
1180    
1181    If the ptrace request does not exist, this function returns 0
1182    and properly sets the have_ptrace_* flag.  If the request fails,
1183    this function calls perror_with_name.  Otherwise, if the request
1184    succeeds, then the regcache is stored and 1 is returned.  */
1185 static int
1186 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1187 {
1188   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1189   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1190   gdb_gregset_t gregset;
1191
1192   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1193     {
1194       if (errno == EIO)
1195         {
1196           have_ptrace_getsetregs = 0;
1197           return 0;
1198         }
1199       perror_with_name (_("Couldn't get general-purpose registers."));
1200     }
1201
1202   fill_gregset (regcache, &gregset, regno);
1203
1204   if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
1205     {
1206       if (errno == EIO)
1207         {
1208           have_ptrace_getsetregs = 0;
1209           return 0;
1210         }
1211       perror_with_name (_("Couldn't set general-purpose registers."));
1212     }
1213
1214   return 1;
1215 }
1216
1217 /* This is a wrapper for the store_all_gp_regs function.  It is
1218    responsible for verifying if this target has the ptrace request
1219    that can be used to store all general-purpose registers at one
1220    shot.  If it doesn't, then we should store them using the
1221    old-fashioned way, which is to iterate over the registers and
1222    store them one by one.  */
1223 static void
1224 store_gp_regs (const struct regcache *regcache, int tid, int regno)
1225 {
1226   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1227   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1228   int i;
1229
1230   if (have_ptrace_getsetregs)
1231     if (store_all_gp_regs (regcache, tid, regno))
1232       return;
1233
1234   /* If we hit this point, it doesn't really matter which
1235      architecture we are using.  We just need to store the
1236      registers in the "old-fashioned way".  */
1237   for (i = 0; i < ppc_num_gprs; i++)
1238     store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1239 }
1240
1241 /* This function actually issues the request to ptrace, telling
1242    it to store all floating-point registers present in the specified
1243    regset.
1244    
1245    If the ptrace request does not exist, this function returns 0
1246    and properly sets the have_ptrace_* flag.  If the request fails,
1247    this function calls perror_with_name.  Otherwise, if the request
1248    succeeds, then the regcache is stored and 1 is returned.  */
1249 static int
1250 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
1251 {
1252   gdb_fpregset_t fpregs;
1253
1254   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1255     {
1256       if (errno == EIO)
1257         {
1258           have_ptrace_getsetfpregs = 0;
1259           return 0;
1260         }
1261       perror_with_name (_("Couldn't get floating-point registers."));
1262     }
1263
1264   fill_fpregset (regcache, &fpregs, regno);
1265
1266   if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
1267     {
1268       if (errno == EIO)
1269         {
1270           have_ptrace_getsetfpregs = 0;
1271           return 0;
1272         }
1273       perror_with_name (_("Couldn't set floating-point registers."));
1274     }
1275
1276   return 1;
1277 }
1278
1279 /* This is a wrapper for the store_all_fp_regs function.  It is
1280    responsible for verifying if this target has the ptrace request
1281    that can be used to store all floating-point registers at one
1282    shot.  If it doesn't, then we should store them using the
1283    old-fashioned way, which is to iterate over the registers and
1284    store them one by one.  */
1285 static void
1286 store_fp_regs (const struct regcache *regcache, int tid, int regno)
1287 {
1288   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1289   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1290   int i;
1291
1292   if (have_ptrace_getsetfpregs)
1293     if (store_all_fp_regs (regcache, tid, regno))
1294       return;
1295
1296   /* If we hit this point, it doesn't really matter which
1297      architecture we are using.  We just need to store the
1298      registers in the "old-fashioned way".  */
1299   for (i = 0; i < ppc_num_fprs; i++)
1300     store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
1301 }
1302
1303 static void
1304 store_ppc_registers (const struct regcache *regcache, int tid)
1305 {
1306   int i;
1307   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1308   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1309  
1310   store_gp_regs (regcache, tid, -1);
1311   if (tdep->ppc_fp0_regnum >= 0)
1312     store_fp_regs (regcache, tid, -1);
1313   store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1314   if (tdep->ppc_ps_regnum != -1)
1315     store_register (regcache, tid, tdep->ppc_ps_regnum);
1316   if (tdep->ppc_cr_regnum != -1)
1317     store_register (regcache, tid, tdep->ppc_cr_regnum);
1318   if (tdep->ppc_lr_regnum != -1)
1319     store_register (regcache, tid, tdep->ppc_lr_regnum);
1320   if (tdep->ppc_ctr_regnum != -1)
1321     store_register (regcache, tid, tdep->ppc_ctr_regnum);
1322   if (tdep->ppc_xer_regnum != -1)
1323     store_register (regcache, tid, tdep->ppc_xer_regnum);
1324   if (tdep->ppc_mq_regnum != -1)
1325     store_register (regcache, tid, tdep->ppc_mq_regnum);
1326   if (tdep->ppc_fpscr_regnum != -1)
1327     store_register (regcache, tid, tdep->ppc_fpscr_regnum);
1328   if (ppc_linux_trap_reg_p (gdbarch))
1329     {
1330       store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1331       store_register (regcache, tid, PPC_TRAP_REGNUM);
1332     }
1333   if (have_ptrace_getvrregs)
1334     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1335       store_altivec_registers (regcache, tid);
1336   if (have_ptrace_getsetvsxregs)
1337     if (tdep->ppc_vsr0_upper_regnum != -1)
1338       store_vsx_registers (regcache, tid);
1339   if (tdep->ppc_ev0_upper_regnum >= 0)
1340     store_spe_register (regcache, tid, -1);
1341 }
1342
1343 /* Fetch the AT_HWCAP entry from the aux vector.  */
1344 unsigned long ppc_linux_get_hwcap (void)
1345 {
1346   CORE_ADDR field;
1347
1348   if (target_auxv_search (&current_target, AT_HWCAP, &field))
1349     return (unsigned long) field;
1350
1351   return 0;
1352 }
1353
1354 /* The cached DABR value, to install in new threads.
1355    This variable is used when we are dealing with non-BookE
1356    processors.  */
1357 static long saved_dabr_value;
1358
1359 /* Global structure that will store information about the available
1360    features on this BookE processor.  */
1361 static struct ppc_debug_info booke_debug_info;
1362
1363 /* Global variable that holds the maximum number of slots that the
1364    kernel will use.  This is only used when the processor is BookE.  */
1365 static size_t max_slots_number = 0;
1366
1367 struct hw_break_tuple
1368 {
1369   long slot;
1370   struct ppc_hw_breakpoint *hw_break;
1371 };
1372
1373 /* This is an internal VEC created to store information about *points inserted
1374    for each thread.  This is used for BookE processors.  */
1375 typedef struct thread_points
1376   {
1377     /* The TID to which this *point relates.  */
1378     int tid;
1379     /* Information about the *point, such as its address, type, etc.
1380
1381        Each element inside this vector corresponds to a hardware
1382        breakpoint or watchpoint in the thread represented by TID.  The maximum
1383        size of these vector is MAX_SLOTS_NUMBER.  If the hw_break element of
1384        the tuple is NULL, then the position in the vector is free.  */
1385     struct hw_break_tuple *hw_breaks;
1386   } *thread_points_p;
1387 DEF_VEC_P (thread_points_p);
1388
1389 VEC(thread_points_p) *ppc_threads = NULL;
1390
1391 /* The version of the kernel interface that we will use if the processor is
1392    BookE.  */
1393 #define PPC_DEBUG_CURRENT_VERSION 1
1394
1395 /* Returns non-zero if we support the ptrace interface which enables
1396    booke debugging resources.  */
1397 static int
1398 have_ptrace_booke_interface (void)
1399 {
1400   static int have_ptrace_booke_interface = -1;
1401
1402   if (have_ptrace_booke_interface == -1)
1403     {
1404       int tid;
1405
1406       tid = TIDGET (inferior_ptid);
1407       if (tid == 0)
1408         tid = PIDGET (inferior_ptid);
1409
1410       /* Check for kernel support for BOOKE debug registers.  */
1411       if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &booke_debug_info) >= 0)
1412         {
1413           have_ptrace_booke_interface = 1;
1414           max_slots_number = booke_debug_info.num_instruction_bps
1415             + booke_debug_info.num_data_bps + booke_debug_info.num_condition_regs;
1416         }
1417       else
1418         {
1419           /* Old school interface and no BOOKE debug registers support.  */
1420           have_ptrace_booke_interface = 0;
1421           memset (&booke_debug_info, 0, sizeof (struct ppc_debug_info));
1422         }
1423     }
1424
1425   return have_ptrace_booke_interface;
1426 }
1427
1428 static int
1429 ppc_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
1430 {
1431   int total_hw_wp, total_hw_bp;
1432
1433   if (have_ptrace_booke_interface ())
1434     {
1435       /* For PPC BookE processors, the number of available hardware
1436          watchpoints and breakpoints is stored at the booke_debug_info
1437          struct.  */
1438       total_hw_bp = booke_debug_info.num_instruction_bps;
1439       total_hw_wp = booke_debug_info.num_data_bps;
1440     }
1441   else
1442     {
1443       /* For PPC server processors, we accept 1 hardware watchpoint and 0
1444          hardware breakpoints.  */
1445       total_hw_bp = 0;
1446       total_hw_wp = 1;
1447     }
1448
1449   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
1450       || type == bp_access_watchpoint || type == bp_watchpoint)
1451     {
1452       if (cnt > total_hw_wp)
1453         return -1;
1454     }
1455   else if (type == bp_hardware_breakpoint)
1456     {
1457       if (cnt > total_hw_bp)
1458         return -1;
1459     }
1460
1461   if (!have_ptrace_booke_interface ())
1462     {
1463       int tid;
1464       ptid_t ptid = inferior_ptid;
1465
1466       /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
1467          the target has DABR.  If either answer is no, the ptrace call will
1468          return -1.  Fail in that case.  */
1469       tid = TIDGET (ptid);
1470       if (tid == 0)
1471         tid = PIDGET (ptid);
1472
1473       if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1474         return 0;
1475     }
1476
1477   return 1;
1478 }
1479
1480 static int
1481 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1482 {
1483   /* Handle sub-8-byte quantities.  */
1484   if (len <= 0)
1485     return 0;
1486
1487   /* The new BookE ptrace interface tells if there are alignment restrictions
1488      for watchpoints in the processors.  In that case, we use that information
1489      to determine the hardcoded watchable region for watchpoints.  */
1490   if (have_ptrace_booke_interface ())
1491     {
1492       if (booke_debug_info.data_bp_alignment
1493           && (addr + len > (addr & ~(booke_debug_info.data_bp_alignment - 1))
1494               + booke_debug_info.data_bp_alignment))
1495           return 0;
1496     }
1497   /* addr+len must fall in the 8 byte watchable region for DABR-based
1498      processors (i.e., server processors).  Without the new BookE ptrace
1499      interface, DAC-based processors (i.e., embedded processors) will use
1500      addresses aligned to 4-bytes due to the way the read/write flags are
1501      passed in the old ptrace interface.  */
1502   else if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1503            && (addr + len) > (addr & ~3) + 4)
1504            || (addr + len) > (addr & ~7) + 8)
1505     return 0;
1506
1507   return 1;
1508 }
1509
1510 /* This function compares two ppc_hw_breakpoint structs field-by-field.  */
1511 static int
1512 booke_cmp_hw_point (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
1513 {
1514   return (a->trigger_type       == b->trigger_type
1515           && a->addr_mode       == b->addr_mode
1516           && a->condition_mode  == b->condition_mode
1517           && a->addr            == b->addr
1518           && a->addr2           == b->addr2
1519           && a->condition_value == b->condition_value);
1520 }
1521
1522 /* This function can be used to retrieve a thread_points by the TID of the
1523    related process/thread.  If nothing has been found, and ALLOC_NEW is 0,
1524    it returns NULL.  If ALLOC_NEW is non-zero, a new thread_points for the
1525    provided TID will be created and returned.  */
1526 static struct thread_points *
1527 booke_find_thread_points_by_tid (int tid, int alloc_new)
1528 {
1529   int i;
1530   struct thread_points *t;
1531
1532   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
1533     if (t->tid == tid)
1534       return t;
1535
1536   t = NULL;
1537
1538   /* Do we need to allocate a new point_item
1539      if the wanted one does not exist?  */
1540   if (alloc_new)
1541     {
1542       t = xmalloc (sizeof (struct thread_points));
1543       t->hw_breaks = xzalloc (max_slots_number * sizeof (struct hw_break_tuple));
1544       t->tid = tid;
1545       VEC_safe_push (thread_points_p, ppc_threads, t);
1546     }
1547
1548   return t;
1549 }
1550
1551 /* This function is a generic wrapper that is responsible for inserting a
1552    *point (i.e., calling `ptrace' in order to issue the request to the
1553    kernel) and registering it internally in GDB.  */
1554 static void
1555 booke_insert_point (struct ppc_hw_breakpoint *b, int tid)
1556 {
1557   int i;
1558   long slot;
1559   struct ppc_hw_breakpoint *p = xmalloc (sizeof (struct ppc_hw_breakpoint));
1560   struct hw_break_tuple *hw_breaks;
1561   struct cleanup *c = make_cleanup (xfree, p);
1562   struct thread_points *t;
1563   struct hw_break_tuple *tuple;
1564
1565   memcpy (p, b, sizeof (struct ppc_hw_breakpoint));
1566
1567   errno = 0;
1568   slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p);
1569   if (slot < 0)
1570     perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
1571
1572   /* Everything went fine, so we have to register this *point.  */
1573   t = booke_find_thread_points_by_tid (tid, 1);
1574   gdb_assert (t != NULL);
1575   hw_breaks = t->hw_breaks;
1576
1577   /* Find a free element in the hw_breaks vector.  */
1578   for (i = 0; i < max_slots_number; i++)
1579     if (hw_breaks[i].hw_break == NULL)
1580       {
1581         hw_breaks[i].slot = slot;
1582         hw_breaks[i].hw_break = p;
1583         break;
1584       }
1585
1586   gdb_assert (i != max_slots_number);
1587
1588   discard_cleanups (c);
1589 }
1590
1591 /* This function is a generic wrapper that is responsible for removing a
1592    *point (i.e., calling `ptrace' in order to issue the request to the
1593    kernel), and unregistering it internally at GDB.  */
1594 static void
1595 booke_remove_point (struct ppc_hw_breakpoint *b, int tid)
1596 {
1597   int i;
1598   struct hw_break_tuple *hw_breaks;
1599   struct thread_points *t;
1600
1601   t = booke_find_thread_points_by_tid (tid, 0);
1602   gdb_assert (t != NULL);
1603   hw_breaks = t->hw_breaks;
1604
1605   for (i = 0; i < max_slots_number; i++)
1606     if (hw_breaks[i].hw_break && booke_cmp_hw_point (hw_breaks[i].hw_break, b))
1607       break;
1608
1609   gdb_assert (i != max_slots_number);
1610
1611   /* We have to ignore ENOENT errors because the kernel implements hardware
1612      breakpoints/watchpoints as "one-shot", that is, they are automatically
1613      deleted when hit.  */
1614   errno = 0;
1615   if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
1616     if (errno != ENOENT)
1617       perror_with_name (_("Unexpected error deleting breakpoint or watchpoint"));
1618
1619   xfree (hw_breaks[i].hw_break);
1620   hw_breaks[i].hw_break = NULL;
1621 }
1622
1623 static int
1624 ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
1625                                   struct bp_target_info *bp_tgt)
1626 {
1627   ptid_t ptid;
1628   struct lwp_info *lp;
1629   struct ppc_hw_breakpoint p;
1630
1631   if (!have_ptrace_booke_interface ())
1632     return -1;
1633
1634   p.version         = PPC_DEBUG_CURRENT_VERSION;
1635   p.trigger_type    = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1636   p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1637   p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1638   p.addr            = (uint64_t) bp_tgt->placed_address;
1639   p.addr2           = 0;
1640   p.condition_value = 0;
1641
1642   ALL_LWPS (lp, ptid)
1643     booke_insert_point (&p, TIDGET (ptid));
1644
1645   return 0;
1646 }
1647
1648 static int
1649 ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
1650                                   struct bp_target_info *bp_tgt)
1651 {
1652   ptid_t ptid;
1653   struct lwp_info *lp;
1654   struct ppc_hw_breakpoint p;
1655
1656   if (!have_ptrace_booke_interface ())
1657     return -1;
1658
1659   p.version         = PPC_DEBUG_CURRENT_VERSION;
1660   p.trigger_type    = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1661   p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1662   p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1663   p.addr            = (uint64_t) bp_tgt->placed_address;
1664   p.addr2           = 0;
1665   p.condition_value = 0;
1666
1667   ALL_LWPS (lp, ptid)
1668     booke_remove_point (&p, TIDGET (ptid));
1669
1670   return 0;
1671 }
1672
1673 static int
1674 get_trigger_type (int rw)
1675 {
1676   int t;
1677
1678   if (rw == hw_read)
1679     t = PPC_BREAKPOINT_TRIGGER_READ;
1680   else if (rw == hw_write)
1681     t = PPC_BREAKPOINT_TRIGGER_WRITE;
1682   else
1683     t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
1684
1685   return t;
1686 }
1687
1688 static int
1689 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
1690 {
1691   struct lwp_info *lp;
1692   ptid_t ptid;
1693   int ret = -1;
1694
1695   if (have_ptrace_booke_interface ())
1696     {
1697       struct ppc_hw_breakpoint p;
1698
1699       p.version         = PPC_DEBUG_CURRENT_VERSION;
1700       p.trigger_type    = get_trigger_type (rw);
1701       p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1702       p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1703       p.addr            = (uint64_t) addr;
1704       p.addr2           = 0;
1705       p.condition_value = 0;
1706
1707       ALL_LWPS (lp, ptid)
1708         booke_insert_point (&p, TIDGET (ptid));
1709
1710       ret = 0;
1711     }
1712   else
1713     {
1714       long dabr_value;
1715       long read_mode, write_mode;
1716
1717       if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1718         {
1719           /* PowerPC 440 requires only the read/write flags to be passed
1720              to the kernel.  */
1721           read_mode  = 1;
1722           write_mode = 2;
1723         }
1724       else
1725         {
1726           /* PowerPC 970 and other DABR-based processors are required to pass
1727              the Breakpoint Translation bit together with the flags.  */
1728           read_mode  = 5;
1729           write_mode = 6;
1730         }
1731
1732       dabr_value = addr & ~(read_mode | write_mode);
1733       switch (rw)
1734         {
1735           case hw_read:
1736             /* Set read and translate bits.  */
1737             dabr_value |= read_mode;
1738             break;
1739           case hw_write:
1740             /* Set write and translate bits.  */
1741             dabr_value |= write_mode;
1742             break;
1743           case hw_access:
1744             /* Set read, write and translate bits.  */
1745             dabr_value |= read_mode | write_mode;
1746             break;
1747         }
1748
1749       saved_dabr_value = dabr_value;
1750
1751       ALL_LWPS (lp, ptid)
1752         if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1753           return -1;
1754
1755       ret = 0;
1756     }
1757
1758   return ret;
1759 }
1760
1761 static int
1762 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
1763 {
1764   struct lwp_info *lp;
1765   ptid_t ptid;
1766   int ret = -1;
1767
1768   if (have_ptrace_booke_interface ())
1769     {
1770       struct ppc_hw_breakpoint p;
1771
1772       p.version         = PPC_DEBUG_CURRENT_VERSION;
1773       p.trigger_type    = get_trigger_type (rw);
1774       p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1775       p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1776       p.addr            = (uint64_t) addr;
1777       p.addr2           = 0;
1778       p.condition_value = 0;
1779
1780       ALL_LWPS (lp, ptid)
1781         booke_remove_point (&p, TIDGET (ptid));
1782
1783       ret = 0;
1784     }
1785   else
1786     {
1787       saved_dabr_value = 0;
1788       ALL_LWPS (lp, ptid)
1789         if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1790           return -1;
1791
1792       ret = 0;
1793     }
1794
1795   return ret;
1796 }
1797
1798 static void
1799 ppc_linux_new_thread (ptid_t ptid)
1800 {
1801   int tid = TIDGET (ptid);
1802
1803   if (have_ptrace_booke_interface ())
1804     {
1805       int i;
1806       struct thread_points *p;
1807       struct hw_break_tuple *hw_breaks;
1808
1809       if (VEC_empty (thread_points_p, ppc_threads))
1810         return;
1811
1812       /* Get a list of breakpoints from any thread. */
1813       p = VEC_last (thread_points_p, ppc_threads);
1814       hw_breaks = p->hw_breaks;
1815
1816       /* Copy that thread's breakpoints and watchpoints to the new thread. */
1817       for (i = 0; i < max_slots_number; i++)
1818         if (hw_breaks[i].hw_break)
1819           booke_insert_point (hw_breaks[i].hw_break, tid);
1820     }
1821   else
1822     ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
1823 }
1824
1825 static void
1826 ppc_linux_thread_exit (struct thread_info *tp, int silent)
1827 {
1828   int i;
1829   int tid = TIDGET (tp->ptid);
1830   struct hw_break_tuple *hw_breaks;
1831   struct thread_points *t = NULL, *p;
1832
1833   if (!have_ptrace_booke_interface ())
1834     return;
1835
1836   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
1837     if (p->tid == tid)
1838       {
1839         t = p;
1840         break;
1841       }
1842
1843   if (t == NULL)
1844     return;
1845
1846   VEC_unordered_remove (thread_points_p, ppc_threads, i);
1847
1848   hw_breaks = t->hw_breaks;
1849
1850   for (i = 0; i < max_slots_number; i++)
1851     if (hw_breaks[i].hw_break)
1852       xfree (hw_breaks[i].hw_break);
1853
1854   xfree (t->hw_breaks);
1855   xfree (t);
1856 }
1857
1858 static int
1859 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1860 {
1861   struct siginfo *siginfo_p;
1862
1863   siginfo_p = linux_nat_get_siginfo (inferior_ptid);
1864
1865   if (siginfo_p->si_signo != SIGTRAP
1866       || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1867     return 0;
1868
1869   if (have_ptrace_booke_interface ())
1870     {
1871       int i;
1872       struct thread_points *t;
1873       struct hw_break_tuple *hw_breaks;
1874       /* The index (or slot) of the *point is passed in the si_errno field.  */
1875       int slot = siginfo_p->si_errno;
1876
1877       t = booke_find_thread_points_by_tid (TIDGET (inferior_ptid), 0);
1878
1879       /* Find out if this *point is a hardware breakpoint.
1880          If so, we should return 0.  */
1881       if (t)
1882         {
1883           hw_breaks = t->hw_breaks;
1884           for (i = 0; i < max_slots_number; i++)
1885            if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
1886                && hw_breaks[i].hw_break->trigger_type
1887                     == PPC_BREAKPOINT_TRIGGER_EXECUTE)
1888              return 0;
1889         }
1890     }
1891
1892   *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
1893   return 1;
1894 }
1895
1896 static int
1897 ppc_linux_stopped_by_watchpoint (void)
1898 {
1899   CORE_ADDR addr;
1900   return ppc_linux_stopped_data_address (&current_target, &addr);
1901 }
1902
1903 static int
1904 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
1905                                         CORE_ADDR addr,
1906                                         CORE_ADDR start, int length)
1907 {
1908   int mask;
1909
1910   if (have_ptrace_booke_interface ()
1911       && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1912     return start <= addr && start + length >= addr;
1913   else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1914     mask = 3;
1915   else
1916     mask = 7;
1917
1918   addr &= ~mask;
1919
1920   /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
1921   return start <= addr + mask && start + length - 1 >= addr;
1922 }
1923
1924 static void
1925 ppc_linux_store_inferior_registers (struct target_ops *ops,
1926                                     struct regcache *regcache, int regno)
1927 {
1928   /* Overload thread id onto process id */
1929   int tid = TIDGET (inferior_ptid);
1930
1931   /* No thread id, just use process id */
1932   if (tid == 0)
1933     tid = PIDGET (inferior_ptid);
1934
1935   if (regno >= 0)
1936     store_register (regcache, tid, regno);
1937   else
1938     store_ppc_registers (regcache, tid);
1939 }
1940
1941 /* Functions for transferring registers between a gregset_t or fpregset_t
1942    (see sys/ucontext.h) and gdb's regcache.  The word size is that used
1943    by the ptrace interface, not the current program's ABI.  eg. If a
1944    powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
1945    read or write 64-bit gregsets.  This is to suit the host libthread_db.  */
1946
1947 void
1948 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1949 {
1950   const struct regset *regset = ppc_linux_gregset (sizeof (long));
1951
1952   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1953 }
1954
1955 void
1956 fill_gregset (const struct regcache *regcache,
1957               gdb_gregset_t *gregsetp, int regno)
1958 {
1959   const struct regset *regset = ppc_linux_gregset (sizeof (long));
1960
1961   if (regno == -1)
1962     memset (gregsetp, 0, sizeof (*gregsetp));
1963   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1964 }
1965
1966 void
1967 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1968 {
1969   const struct regset *regset = ppc_linux_fpregset ();
1970
1971   ppc_supply_fpregset (regset, regcache, -1,
1972                        fpregsetp, sizeof (*fpregsetp));
1973 }
1974
1975 void
1976 fill_fpregset (const struct regcache *regcache,
1977                gdb_fpregset_t *fpregsetp, int regno)
1978 {
1979   const struct regset *regset = ppc_linux_fpregset ();
1980
1981   ppc_collect_fpregset (regset, regcache, regno,
1982                         fpregsetp, sizeof (*fpregsetp));
1983 }
1984
1985 static int
1986 ppc_linux_target_wordsize (void)
1987 {
1988   int wordsize = 4;
1989
1990   /* Check for 64-bit inferior process.  This is the case when the host is
1991      64-bit, and in addition the top bit of the MSR register is set.  */
1992 #ifdef __powerpc64__
1993   long msr;
1994
1995   int tid = TIDGET (inferior_ptid);
1996   if (tid == 0)
1997     tid = PIDGET (inferior_ptid);
1998
1999   errno = 0;
2000   msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
2001   if (errno == 0 && msr < 0)
2002     wordsize = 8;
2003 #endif
2004
2005   return wordsize;
2006 }
2007
2008 static int
2009 ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
2010                       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
2011 {
2012   int sizeof_auxv_field = ppc_linux_target_wordsize ();
2013   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
2014   gdb_byte *ptr = *readptr;
2015
2016   if (endptr == ptr)
2017     return 0;
2018
2019   if (endptr - ptr < sizeof_auxv_field * 2)
2020     return -1;
2021
2022   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2023   ptr += sizeof_auxv_field;
2024   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2025   ptr += sizeof_auxv_field;
2026
2027   *readptr = ptr;
2028   return 1;
2029 }
2030
2031 static const struct target_desc *
2032 ppc_linux_read_description (struct target_ops *ops)
2033 {
2034   int altivec = 0;
2035   int vsx = 0;
2036   int isa205 = 0;
2037   int cell = 0;
2038
2039   int tid = TIDGET (inferior_ptid);
2040   if (tid == 0)
2041     tid = PIDGET (inferior_ptid);
2042
2043   if (have_ptrace_getsetevrregs)
2044     {
2045       struct gdb_evrregset_t evrregset;
2046
2047       if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
2048         return tdesc_powerpc_e500l;
2049
2050       /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
2051          Anything else needs to be reported.  */
2052       else if (errno != EIO)
2053         perror_with_name (_("Unable to fetch SPE registers"));
2054     }
2055
2056   if (have_ptrace_getsetvsxregs)
2057     {
2058       gdb_vsxregset_t vsxregset;
2059
2060       if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
2061         vsx = 1;
2062
2063       /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
2064          Anything else needs to be reported.  */
2065       else if (errno != EIO)
2066         perror_with_name (_("Unable to fetch VSX registers"));
2067     }
2068
2069   if (have_ptrace_getvrregs)
2070     {
2071       gdb_vrregset_t vrregset;
2072
2073       if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
2074         altivec = 1;
2075
2076       /* EIO means that the PTRACE_GETVRREGS request isn't supported.
2077          Anything else needs to be reported.  */
2078       else if (errno != EIO)
2079         perror_with_name (_("Unable to fetch AltiVec registers"));
2080     }
2081
2082   /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
2083      the FPSCR from 32 bits to 64 bits. Even though Power 7 supports this
2084      ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
2085      PPC_FEATURE_ARCH_2_06.  Since for now the only bits used in the higher
2086      half of the register are for Decimal Floating Point, we check if that
2087      feature is available to decide the size of the FPSCR.  */
2088   if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
2089     isa205 = 1;
2090
2091   if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
2092     cell = 1;
2093
2094   if (ppc_linux_target_wordsize () == 8)
2095     {
2096       if (cell)
2097         return tdesc_powerpc_cell64l;
2098       else if (vsx)
2099         return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
2100       else if (altivec)
2101         return isa205? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
2102
2103       return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
2104     }
2105
2106   if (cell)
2107     return tdesc_powerpc_cell32l;
2108   else if (vsx)
2109     return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
2110   else if (altivec)
2111     return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
2112
2113   return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
2114 }
2115
2116 void _initialize_ppc_linux_nat (void);
2117
2118 void
2119 _initialize_ppc_linux_nat (void)
2120 {
2121   struct target_ops *t;
2122
2123   /* Fill in the generic GNU/Linux methods.  */
2124   t = linux_target ();
2125
2126   /* Add our register access methods.  */
2127   t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
2128   t->to_store_registers = ppc_linux_store_inferior_registers;
2129
2130   /* Add our breakpoint/watchpoint methods.  */
2131   t->to_can_use_hw_breakpoint = ppc_linux_can_use_hw_breakpoint;
2132   t->to_insert_hw_breakpoint = ppc_linux_insert_hw_breakpoint;
2133   t->to_remove_hw_breakpoint = ppc_linux_remove_hw_breakpoint;
2134   t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
2135   t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
2136   t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
2137   t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
2138   t->to_stopped_data_address = ppc_linux_stopped_data_address;
2139   t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
2140
2141   t->to_read_description = ppc_linux_read_description;
2142   t->to_auxv_parse = ppc_linux_auxv_parse;
2143
2144   observer_attach_thread_exit (ppc_linux_thread_exit);
2145
2146   /* Register the target.  */
2147   linux_nat_add_target (t);
2148   linux_nat_set_new_thread (t, ppc_linux_new_thread);
2149 }