Upload Tizen:Base source
[external/gdb.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_GETVSXREGS, 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 /* Check whether we have at least one free DVC register.  */
1689 static int
1690 can_use_watchpoint_cond_accel (void)
1691 {
1692   struct thread_points *p;
1693   int tid = TIDGET (inferior_ptid);
1694   int cnt = booke_debug_info.num_condition_regs, i;
1695   CORE_ADDR tmp_value;
1696
1697   if (!have_ptrace_booke_interface () || cnt == 0)
1698     return 0;
1699
1700   p = booke_find_thread_points_by_tid (tid, 0);
1701
1702   if (p)
1703     {
1704       for (i = 0; i < max_slots_number; i++)
1705         if (p->hw_breaks[i].hw_break != NULL
1706             && (p->hw_breaks[i].hw_break->condition_mode
1707                 != PPC_BREAKPOINT_CONDITION_NONE))
1708           cnt--;
1709
1710       /* There are no available slots now.  */
1711       if (cnt <= 0)
1712         return 0;
1713     }
1714
1715   return 1;
1716 }
1717
1718 /* Calculate the enable bits and the contents of the Data Value Compare
1719    debug register present in BookE processors.
1720
1721    ADDR is the address to be watched, LEN is the length of watched data
1722    and DATA_VALUE is the value which will trigger the watchpoint.
1723    On exit, CONDITION_MODE will hold the enable bits for the DVC, and
1724    CONDITION_VALUE will hold the value which should be put in the
1725    DVC register.  */
1726 static void
1727 calculate_dvc (CORE_ADDR addr, int len, CORE_ADDR data_value,
1728                uint32_t *condition_mode, uint64_t *condition_value)
1729 {
1730   int i, num_byte_enable, align_offset, num_bytes_off_dvc,
1731       rightmost_enabled_byte;
1732   CORE_ADDR addr_end_data, addr_end_dvc;
1733
1734   /* The DVC register compares bytes within fixed-length windows which
1735      are word-aligned, with length equal to that of the DVC register.
1736      We need to calculate where our watch region is relative to that
1737      window and enable comparison of the bytes which fall within it.  */
1738
1739   align_offset = addr % booke_debug_info.sizeof_condition;
1740   addr_end_data = addr + len;
1741   addr_end_dvc = (addr - align_offset
1742                   + booke_debug_info.sizeof_condition);
1743   num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
1744                          addr_end_data - addr_end_dvc : 0;
1745   num_byte_enable = len - num_bytes_off_dvc;
1746   /* Here, bytes are numbered from right to left.  */
1747   rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
1748                               addr_end_dvc - addr_end_data : 0;
1749
1750   *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
1751   for (i = 0; i < num_byte_enable; i++)
1752     *condition_mode |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
1753
1754   /* Now we need to match the position within the DVC of the comparison
1755      value with where the watch region is relative to the window
1756      (i.e., the ALIGN_OFFSET).  */
1757
1758   *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
1759                       << rightmost_enabled_byte * 8);
1760 }
1761
1762 /* Return the number of memory locations that need to be accessed to
1763    evaluate the expression which generated the given value chain.
1764    Returns -1 if there's any register access involved, or if there are
1765    other kinds of values which are not acceptable in a condition
1766    expression (e.g., lval_computed or lval_internalvar).  */
1767 static int
1768 num_memory_accesses (struct value *v)
1769 {
1770   int found_memory_cnt = 0;
1771   struct value *head = v;
1772
1773   /* The idea here is that evaluating an expression generates a series
1774      of values, one holding the value of every subexpression.  (The
1775      expression a*b+c has five subexpressions: a, b, a*b, c, and
1776      a*b+c.)  GDB's values hold almost enough information to establish
1777      the criteria given above --- they identify memory lvalues,
1778      register lvalues, computed values, etcetera.  So we can evaluate
1779      the expression, and then scan the chain of values that leaves
1780      behind to determine the memory locations involved in the evaluation
1781      of an expression.
1782
1783      However, I don't think that the values returned by inferior
1784      function calls are special in any way.  So this function may not
1785      notice that an expression contains an inferior function call.
1786      FIXME.  */
1787
1788   for (; v; v = value_next (v))
1789     {
1790       /* Constants and values from the history are fine.  */
1791       if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
1792         continue;
1793       else if (VALUE_LVAL (v) == lval_memory)
1794         {
1795           /* A lazy memory lvalue is one that GDB never needed to fetch;
1796              we either just used its address (e.g., `a' in `a.b') or
1797              we never needed it at all (e.g., `a' in `a,b').  */
1798           if (!value_lazy (v))
1799             found_memory_cnt++;
1800         }
1801       /* Other kinds of values are not fine. */
1802       else
1803         return -1;
1804     }
1805
1806   return found_memory_cnt;
1807 }
1808
1809 /* Verifies whether the expression COND can be implemented using the
1810    DVC (Data Value Compare) register in BookE processors.  The expression
1811    must test the watch value for equality with a constant expression.
1812    If the function returns 1, DATA_VALUE will contain the constant against
1813    which the watch value should be compared.  */
1814 static int
1815 check_condition (CORE_ADDR watch_addr, struct expression *cond,
1816                  CORE_ADDR *data_value)
1817 {
1818   int pc = 1, num_accesses_left, num_accesses_right;
1819   struct value *left_val, *right_val, *left_chain, *right_chain;
1820
1821   if (cond->elts[0].opcode != BINOP_EQUAL)
1822     return 0;
1823
1824   fetch_subexp_value (cond, &pc, &left_val, NULL, &left_chain);
1825   num_accesses_left = num_memory_accesses (left_chain);
1826
1827   if (left_val == NULL || num_accesses_left < 0)
1828     {
1829       free_value_chain (left_chain);
1830
1831       return 0;
1832     }
1833
1834   fetch_subexp_value (cond, &pc, &right_val, NULL, &right_chain);
1835   num_accesses_right = num_memory_accesses (right_chain);
1836
1837   if (right_val == NULL || num_accesses_right < 0)
1838     {
1839       free_value_chain (left_chain);
1840       free_value_chain (right_chain);
1841
1842       return 0;
1843     }
1844
1845   if (num_accesses_left == 1 && num_accesses_right == 0
1846       && VALUE_LVAL (left_val) == lval_memory
1847       && value_address (left_val) == watch_addr)
1848     *data_value = value_as_long (right_val);
1849   else if (num_accesses_left == 0 && num_accesses_right == 1
1850            && VALUE_LVAL (right_val) == lval_memory
1851            && value_address (right_val) == watch_addr)
1852     *data_value = value_as_long (left_val);
1853   else
1854     {
1855       free_value_chain (left_chain);
1856       free_value_chain (right_chain);
1857
1858       return 0;
1859     }
1860
1861   free_value_chain (left_chain);
1862   free_value_chain (right_chain);
1863
1864   return 1;
1865 }
1866
1867 /* Return non-zero if the target is capable of using hardware to evaluate
1868    the condition expression, thus only triggering the watchpoint when it is
1869    true.  */
1870 static int
1871 ppc_linux_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
1872                                           struct expression *cond)
1873 {
1874   CORE_ADDR data_value;
1875
1876   return (have_ptrace_booke_interface ()
1877           && booke_debug_info.num_condition_regs > 0
1878           && check_condition (addr, cond, &data_value));
1879 }
1880
1881 static int
1882 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
1883                              struct expression *cond)
1884 {
1885   struct lwp_info *lp;
1886   ptid_t ptid;
1887   int ret = -1;
1888
1889   if (have_ptrace_booke_interface ())
1890     {
1891       struct ppc_hw_breakpoint p;
1892       CORE_ADDR data_value;
1893
1894       if (cond && can_use_watchpoint_cond_accel ()
1895           && check_condition (addr, cond, &data_value))
1896         calculate_dvc (addr, len, data_value, &p.condition_mode,
1897                        &p.condition_value);
1898       else
1899         {
1900           p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1901           p.condition_value = 0;
1902         }
1903
1904       p.version         = PPC_DEBUG_CURRENT_VERSION;
1905       p.trigger_type    = get_trigger_type (rw);
1906       p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1907       p.addr            = (uint64_t) addr;
1908       p.addr2           = 0;
1909
1910       ALL_LWPS (lp, ptid)
1911         booke_insert_point (&p, TIDGET (ptid));
1912
1913       ret = 0;
1914     }
1915   else
1916     {
1917       long dabr_value;
1918       long read_mode, write_mode;
1919
1920       if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1921         {
1922           /* PowerPC 440 requires only the read/write flags to be passed
1923              to the kernel.  */
1924           read_mode  = 1;
1925           write_mode = 2;
1926         }
1927       else
1928         {
1929           /* PowerPC 970 and other DABR-based processors are required to pass
1930              the Breakpoint Translation bit together with the flags.  */
1931           read_mode  = 5;
1932           write_mode = 6;
1933         }
1934
1935       dabr_value = addr & ~(read_mode | write_mode);
1936       switch (rw)
1937         {
1938           case hw_read:
1939             /* Set read and translate bits.  */
1940             dabr_value |= read_mode;
1941             break;
1942           case hw_write:
1943             /* Set write and translate bits.  */
1944             dabr_value |= write_mode;
1945             break;
1946           case hw_access:
1947             /* Set read, write and translate bits.  */
1948             dabr_value |= read_mode | write_mode;
1949             break;
1950         }
1951
1952       saved_dabr_value = dabr_value;
1953
1954       ALL_LWPS (lp, ptid)
1955         if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0,
1956                     saved_dabr_value) < 0)
1957           return -1;
1958
1959       ret = 0;
1960     }
1961
1962   return ret;
1963 }
1964
1965 static int
1966 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
1967                              struct expression *cond)
1968 {
1969   struct lwp_info *lp;
1970   ptid_t ptid;
1971   int ret = -1;
1972
1973   if (have_ptrace_booke_interface ())
1974     {
1975       struct ppc_hw_breakpoint p;
1976       CORE_ADDR data_value;
1977
1978       if (cond && booke_debug_info.num_condition_regs > 0
1979           && check_condition (addr, cond, &data_value))
1980         calculate_dvc (addr, len, data_value, &p.condition_mode,
1981                        &p.condition_value);
1982       else
1983         {
1984           p.condition_mode  = PPC_BREAKPOINT_CONDITION_NONE;
1985           p.condition_value = 0;
1986         }
1987
1988       p.version         = PPC_DEBUG_CURRENT_VERSION;
1989       p.trigger_type    = get_trigger_type (rw);
1990       p.addr_mode       = PPC_BREAKPOINT_MODE_EXACT;
1991       p.addr            = (uint64_t) addr;
1992       p.addr2           = 0;
1993
1994       ALL_LWPS (lp, ptid)
1995         booke_remove_point (&p, TIDGET (ptid));
1996
1997       ret = 0;
1998     }
1999   else
2000     {
2001       saved_dabr_value = 0;
2002       ALL_LWPS (lp, ptid)
2003         if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0,
2004                     saved_dabr_value) < 0)
2005           return -1;
2006
2007       ret = 0;
2008     }
2009
2010   return ret;
2011 }
2012
2013 static void
2014 ppc_linux_new_thread (ptid_t ptid)
2015 {
2016   int tid = TIDGET (ptid);
2017
2018   if (have_ptrace_booke_interface ())
2019     {
2020       int i;
2021       struct thread_points *p;
2022       struct hw_break_tuple *hw_breaks;
2023
2024       if (VEC_empty (thread_points_p, ppc_threads))
2025         return;
2026
2027       /* Get a list of breakpoints from any thread. */
2028       p = VEC_last (thread_points_p, ppc_threads);
2029       hw_breaks = p->hw_breaks;
2030
2031       /* Copy that thread's breakpoints and watchpoints to the new thread. */
2032       for (i = 0; i < max_slots_number; i++)
2033         if (hw_breaks[i].hw_break)
2034           booke_insert_point (hw_breaks[i].hw_break, tid);
2035     }
2036   else
2037     ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
2038 }
2039
2040 static void
2041 ppc_linux_thread_exit (struct thread_info *tp, int silent)
2042 {
2043   int i;
2044   int tid = TIDGET (tp->ptid);
2045   struct hw_break_tuple *hw_breaks;
2046   struct thread_points *t = NULL, *p;
2047
2048   if (!have_ptrace_booke_interface ())
2049     return;
2050
2051   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
2052     if (p->tid == tid)
2053       {
2054         t = p;
2055         break;
2056       }
2057
2058   if (t == NULL)
2059     return;
2060
2061   VEC_unordered_remove (thread_points_p, ppc_threads, i);
2062
2063   hw_breaks = t->hw_breaks;
2064
2065   for (i = 0; i < max_slots_number; i++)
2066     if (hw_breaks[i].hw_break)
2067       xfree (hw_breaks[i].hw_break);
2068
2069   xfree (t->hw_breaks);
2070   xfree (t);
2071 }
2072
2073 static int
2074 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
2075 {
2076   struct siginfo *siginfo_p;
2077
2078   siginfo_p = linux_nat_get_siginfo (inferior_ptid);
2079
2080   if (siginfo_p->si_signo != SIGTRAP
2081       || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
2082     return 0;
2083
2084   if (have_ptrace_booke_interface ())
2085     {
2086       int i;
2087       struct thread_points *t;
2088       struct hw_break_tuple *hw_breaks;
2089       /* The index (or slot) of the *point is passed in the si_errno field.  */
2090       int slot = siginfo_p->si_errno;
2091
2092       t = booke_find_thread_points_by_tid (TIDGET (inferior_ptid), 0);
2093
2094       /* Find out if this *point is a hardware breakpoint.
2095          If so, we should return 0.  */
2096       if (t)
2097         {
2098           hw_breaks = t->hw_breaks;
2099           for (i = 0; i < max_slots_number; i++)
2100            if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
2101                && hw_breaks[i].hw_break->trigger_type
2102                     == PPC_BREAKPOINT_TRIGGER_EXECUTE)
2103              return 0;
2104         }
2105     }
2106
2107   *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
2108   return 1;
2109 }
2110
2111 static int
2112 ppc_linux_stopped_by_watchpoint (void)
2113 {
2114   CORE_ADDR addr;
2115   return ppc_linux_stopped_data_address (&current_target, &addr);
2116 }
2117
2118 static int
2119 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
2120                                         CORE_ADDR addr,
2121                                         CORE_ADDR start, int length)
2122 {
2123   int mask;
2124
2125   if (have_ptrace_booke_interface ()
2126       && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
2127     return start <= addr && start + length >= addr;
2128   else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
2129     mask = 3;
2130   else
2131     mask = 7;
2132
2133   addr &= ~mask;
2134
2135   /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
2136   return start <= addr + mask && start + length - 1 >= addr;
2137 }
2138
2139 static void
2140 ppc_linux_store_inferior_registers (struct target_ops *ops,
2141                                     struct regcache *regcache, int regno)
2142 {
2143   /* Overload thread id onto process id */
2144   int tid = TIDGET (inferior_ptid);
2145
2146   /* No thread id, just use process id */
2147   if (tid == 0)
2148     tid = PIDGET (inferior_ptid);
2149
2150   if (regno >= 0)
2151     store_register (regcache, tid, regno);
2152   else
2153     store_ppc_registers (regcache, tid);
2154 }
2155
2156 /* Functions for transferring registers between a gregset_t or fpregset_t
2157    (see sys/ucontext.h) and gdb's regcache.  The word size is that used
2158    by the ptrace interface, not the current program's ABI.  eg. If a
2159    powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
2160    read or write 64-bit gregsets.  This is to suit the host libthread_db.  */
2161
2162 void
2163 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
2164 {
2165   const struct regset *regset = ppc_linux_gregset (sizeof (long));
2166
2167   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
2168 }
2169
2170 void
2171 fill_gregset (const struct regcache *regcache,
2172               gdb_gregset_t *gregsetp, int regno)
2173 {
2174   const struct regset *regset = ppc_linux_gregset (sizeof (long));
2175
2176   if (regno == -1)
2177     memset (gregsetp, 0, sizeof (*gregsetp));
2178   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
2179 }
2180
2181 void
2182 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
2183 {
2184   const struct regset *regset = ppc_linux_fpregset ();
2185
2186   ppc_supply_fpregset (regset, regcache, -1,
2187                        fpregsetp, sizeof (*fpregsetp));
2188 }
2189
2190 void
2191 fill_fpregset (const struct regcache *regcache,
2192                gdb_fpregset_t *fpregsetp, int regno)
2193 {
2194   const struct regset *regset = ppc_linux_fpregset ();
2195
2196   ppc_collect_fpregset (regset, regcache, regno,
2197                         fpregsetp, sizeof (*fpregsetp));
2198 }
2199
2200 static int
2201 ppc_linux_target_wordsize (void)
2202 {
2203   int wordsize = 4;
2204
2205   /* Check for 64-bit inferior process.  This is the case when the host is
2206      64-bit, and in addition the top bit of the MSR register is set.  */
2207 #ifdef __powerpc64__
2208   long msr;
2209
2210   int tid = TIDGET (inferior_ptid);
2211   if (tid == 0)
2212     tid = PIDGET (inferior_ptid);
2213
2214   errno = 0;
2215   msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
2216   if (errno == 0 && msr < 0)
2217     wordsize = 8;
2218 #endif
2219
2220   return wordsize;
2221 }
2222
2223 static int
2224 ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
2225                       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
2226 {
2227   int sizeof_auxv_field = ppc_linux_target_wordsize ();
2228   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
2229   gdb_byte *ptr = *readptr;
2230
2231   if (endptr == ptr)
2232     return 0;
2233
2234   if (endptr - ptr < sizeof_auxv_field * 2)
2235     return -1;
2236
2237   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2238   ptr += sizeof_auxv_field;
2239   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2240   ptr += sizeof_auxv_field;
2241
2242   *readptr = ptr;
2243   return 1;
2244 }
2245
2246 static const struct target_desc *
2247 ppc_linux_read_description (struct target_ops *ops)
2248 {
2249   int altivec = 0;
2250   int vsx = 0;
2251   int isa205 = 0;
2252   int cell = 0;
2253
2254   int tid = TIDGET (inferior_ptid);
2255   if (tid == 0)
2256     tid = PIDGET (inferior_ptid);
2257
2258   if (have_ptrace_getsetevrregs)
2259     {
2260       struct gdb_evrregset_t evrregset;
2261
2262       if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
2263         return tdesc_powerpc_e500l;
2264
2265       /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
2266          Anything else needs to be reported.  */
2267       else if (errno != EIO)
2268         perror_with_name (_("Unable to fetch SPE registers"));
2269     }
2270
2271   if (have_ptrace_getsetvsxregs)
2272     {
2273       gdb_vsxregset_t vsxregset;
2274
2275       if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
2276         vsx = 1;
2277
2278       /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
2279          Anything else needs to be reported.  */
2280       else if (errno != EIO)
2281         perror_with_name (_("Unable to fetch VSX registers"));
2282     }
2283
2284   if (have_ptrace_getvrregs)
2285     {
2286       gdb_vrregset_t vrregset;
2287
2288       if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
2289         altivec = 1;
2290
2291       /* EIO means that the PTRACE_GETVRREGS request isn't supported.
2292          Anything else needs to be reported.  */
2293       else if (errno != EIO)
2294         perror_with_name (_("Unable to fetch AltiVec registers"));
2295     }
2296
2297   /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
2298      the FPSCR from 32 bits to 64 bits. Even though Power 7 supports this
2299      ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
2300      PPC_FEATURE_ARCH_2_06.  Since for now the only bits used in the higher
2301      half of the register are for Decimal Floating Point, we check if that
2302      feature is available to decide the size of the FPSCR.  */
2303   if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
2304     isa205 = 1;
2305
2306   if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
2307     cell = 1;
2308
2309   if (ppc_linux_target_wordsize () == 8)
2310     {
2311       if (cell)
2312         return tdesc_powerpc_cell64l;
2313       else if (vsx)
2314         return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
2315       else if (altivec)
2316         return isa205? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
2317
2318       return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
2319     }
2320
2321   if (cell)
2322     return tdesc_powerpc_cell32l;
2323   else if (vsx)
2324     return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
2325   else if (altivec)
2326     return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
2327
2328   return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
2329 }
2330
2331 void _initialize_ppc_linux_nat (void);
2332
2333 void
2334 _initialize_ppc_linux_nat (void)
2335 {
2336   struct target_ops *t;
2337
2338   /* Fill in the generic GNU/Linux methods.  */
2339   t = linux_target ();
2340
2341   /* Add our register access methods.  */
2342   t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
2343   t->to_store_registers = ppc_linux_store_inferior_registers;
2344
2345   /* Add our breakpoint/watchpoint methods.  */
2346   t->to_can_use_hw_breakpoint = ppc_linux_can_use_hw_breakpoint;
2347   t->to_insert_hw_breakpoint = ppc_linux_insert_hw_breakpoint;
2348   t->to_remove_hw_breakpoint = ppc_linux_remove_hw_breakpoint;
2349   t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
2350   t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
2351   t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
2352   t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
2353   t->to_stopped_data_address = ppc_linux_stopped_data_address;
2354   t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
2355   t->to_can_accel_watchpoint_condition = ppc_linux_can_accel_watchpoint_condition;
2356
2357   t->to_read_description = ppc_linux_read_description;
2358   t->to_auxv_parse = ppc_linux_auxv_parse;
2359
2360   observer_attach_thread_exit (ppc_linux_thread_exit);
2361
2362   /* Register the target.  */
2363   linux_nat_add_target (t);
2364   linux_nat_set_new_thread (t, ppc_linux_new_thread);
2365 }