2004-07-21 Andrew Cagney <cagney@gnu.org>
[external/binutils.git] / gdb / ppc-linux-nat.c
1 /* PPC GNU/Linux native support.
2
3    Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002,
4    2003 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 2 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, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "regcache.h"
29 #include "gdb_assert.h"
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <signal.h>
34 #include <sys/user.h>
35 #include <sys/ioctl.h>
36 #include "gdb_wait.h"
37 #include <fcntl.h>
38 #include <sys/procfs.h>
39 #include <sys/ptrace.h>
40
41 /* Prototypes for supply_gregset etc. */
42 #include "gregset.h"
43 #include "ppc-tdep.h"
44
45 #ifndef PT_READ_U
46 #define PT_READ_U PTRACE_PEEKUSR
47 #endif
48 #ifndef PT_WRITE_U
49 #define PT_WRITE_U PTRACE_POKEUSR
50 #endif
51
52 /* Default the type of the ptrace transfer to int.  */
53 #ifndef PTRACE_XFER_TYPE
54 #define PTRACE_XFER_TYPE int
55 #endif
56
57 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
58    configure time check.  Some older glibc's (for instance 2.2.1)
59    don't have a specific powerpc version of ptrace.h, and fall back on
60    a generic one.  In such cases, sys/ptrace.h defines
61    PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
62    ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
63    PTRACE_SETVRREGS to be.  This also makes a configury check pretty
64    much useless.  */
65
66 /* These definitions should really come from the glibc header files,
67    but Glibc doesn't know about the vrregs yet.  */
68 #ifndef PTRACE_GETVRREGS
69 #define PTRACE_GETVRREGS 18
70 #define PTRACE_SETVRREGS 19
71 #endif
72
73
74 /* Similarly for the ptrace requests for getting / setting the SPE
75    registers (ev0 -- ev31, acc, and spefscr).  See the description of
76    gdb_evrregset_t for details.  */
77 #ifndef PTRACE_GETEVRREGS
78 #define PTRACE_GETEVRREGS 20
79 #define PTRACE_SETEVRREGS 21
80 #endif
81
82
83 /* This oddity is because the Linux kernel defines elf_vrregset_t as
84    an array of 33 16 bytes long elements.  I.e. it leaves out vrsave.
85    However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
86    the vrsave as an extra 4 bytes at the end.  I opted for creating a
87    flat array of chars, so that it is easier to manipulate for gdb.
88
89    There are 32 vector registers 16 bytes longs, plus a VSCR register
90    which is only 4 bytes long, but is fetched as a 16 bytes
91    quantity. Up to here we have the elf_vrregset_t structure.
92    Appended to this there is space for the VRSAVE register: 4 bytes.
93    Even though this vrsave register is not included in the regset
94    typedef, it is handled by the ptrace requests.
95
96    Note that GNU/Linux doesn't support little endian PPC hardware,
97    therefore the offset at which the real value of the VSCR register
98    is located will be always 12 bytes.
99
100    The layout is like this (where x is the actual value of the vscr reg): */
101
102 /* *INDENT-OFF* */
103 /*
104    |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
105    <------->     <-------><-------><->
106      VR0           VR31     VSCR    VRSAVE
107 */
108 /* *INDENT-ON* */
109
110 #define SIZEOF_VRREGS 33*16+4
111
112 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
113
114
115 /* On PPC processors that support the the Signal Processing Extension
116    (SPE) APU, the general-purpose registers are 64 bits long.
117    However, the ordinary Linux kernel PTRACE_PEEKUSR / PTRACE_POKEUSR
118    / PT_READ_U / PT_WRITE_U ptrace calls only access the lower half of
119    each register, to allow them to behave the same way they do on
120    non-SPE systems.  There's a separate pair of calls,
121    PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that read and write the top
122    halves of all the general-purpose registers at once, along with
123    some SPE-specific registers.
124
125    GDB itself continues to claim the general-purpose registers are 32
126    bits long; the full 64-bit registers are called 'ev0' -- 'ev31'.
127    The ev registers are raw registers, and the GPR's are pseudo-
128    registers mapped onto their lower halves.  This means that reading
129    and writing ev registers involves a mix of regset-at-once
130    PTRACE_{GET,SET}EVRREGS calls and register-at-a-time
131    PTRACE_{PEEK,POKE}USR calls.
132
133    This is the structure filled in by PTRACE_GETEVRREGS and written to
134    the inferior's registers by PTRACE_SETEVRREGS.  */
135 struct gdb_evrregset_t
136 {
137   unsigned long evr[32];
138   unsigned long long acc;
139   unsigned long spefscr;
140 };
141
142
143 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
144    PTRACE_SETVRREGS requests, for reading and writing the Altivec
145    registers.  Zero if we've tried one of them and gotten an
146    error.  */
147 int have_ptrace_getvrregs = 1;
148
149
150 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
151    PTRACE_SETEVRREGS requests, for reading and writing the SPE
152    registers.  Zero if we've tried one of them and gotten an
153    error.  */
154 int have_ptrace_getsetevrregs = 1;
155
156
157 int
158 kernel_u_size (void)
159 {
160   return (sizeof (struct user));
161 }
162
163 /* *INDENT-OFF* */
164 /* registers layout, as presented by the ptrace interface:
165 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
166 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
167 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
168 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
169 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
170 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
171 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
172 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
173 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
174 /* *INDENT_ON * */
175
176 static int
177 ppc_register_u_addr (int regno)
178 {
179   int u_addr = -1;
180   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
181   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
182      interface, and not the wordsize of the program's ABI.  */
183   int wordsize = sizeof (PTRACE_XFER_TYPE);
184
185   /* General purpose registers occupy 1 slot each in the buffer */
186   if (regno >= tdep->ppc_gp0_regnum 
187       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
188     u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
189
190   /* Floating point regs: eight bytes each in both 32- and 64-bit
191      ptrace interfaces.  Thus, two slots each in 32-bit interface, one
192      slot each in 64-bit interface.  */
193   if (tdep->ppc_fp0_regnum >= 0
194       && regno >= tdep->ppc_fp0_regnum
195       && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
196     u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
197
198   /* UISA special purpose registers: 1 slot each */
199   if (regno == PC_REGNUM)
200     u_addr = PT_NIP * wordsize;
201   if (regno == tdep->ppc_lr_regnum)
202     u_addr = PT_LNK * wordsize;
203   if (regno == tdep->ppc_cr_regnum)
204     u_addr = PT_CCR * wordsize;
205   if (regno == tdep->ppc_xer_regnum)
206     u_addr = PT_XER * wordsize;
207   if (regno == tdep->ppc_ctr_regnum)
208     u_addr = PT_CTR * wordsize;
209 #ifdef PT_MQ
210   if (regno == tdep->ppc_mq_regnum)
211     u_addr = PT_MQ * wordsize;
212 #endif
213   if (regno == tdep->ppc_ps_regnum)
214     u_addr = PT_MSR * wordsize;
215   if (tdep->ppc_fpscr_regnum >= 0
216       && regno == tdep->ppc_fpscr_regnum)
217     u_addr = PT_FPSCR * wordsize;
218
219   return u_addr;
220 }
221
222 /* The Linux kernel ptrace interface for AltiVec registers uses the
223    registers set mechanism, as opposed to the interface for all the
224    other registers, that stores/fetches each register individually.  */
225 static void
226 fetch_altivec_register (int tid, int regno)
227 {
228   int ret;
229   int offset = 0;
230   gdb_vrregset_t regs;
231   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
232   int vrregsize = DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vr0_regnum);
233
234   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
235   if (ret < 0)
236     {
237       if (errno == EIO)
238         {
239           have_ptrace_getvrregs = 0;
240           return;
241         }
242       perror_with_name ("Unable to fetch AltiVec register");
243     }
244  
245   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
246      long on the hardware.  We deal only with the lower 4 bytes of the
247      vector.  VRSAVE is at the end of the array in a 4 bytes slot, so
248      there is no need to define an offset for it.  */
249   if (regno == (tdep->ppc_vrsave_regnum - 1))
250     offset = vrregsize - DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vrsave_regnum);
251   
252   regcache_raw_supply (current_regcache, regno,
253                        regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
254 }
255
256 /* Fetch the top 32 bits of TID's general-purpose registers and the
257    SPE-specific registers, and place the results in EVRREGSET.  If we
258    don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
259    zeros.
260
261    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
262    PTRACE_SETEVRREGS requests are supported is isolated here, and in
263    set_spe_registers.  */
264 static void
265 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
266 {
267   if (have_ptrace_getsetevrregs)
268     {
269       if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
270         return;
271       else
272         {
273           /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
274              we just return zeros.  */
275           if (errno == EIO)
276             have_ptrace_getsetevrregs = 0;
277           else
278             /* Anything else needs to be reported.  */
279             perror_with_name ("Unable to fetch SPE registers");
280         }
281     }
282
283   memset (evrregset, 0, sizeof (*evrregset));
284 }
285
286 /* Assuming TID refers to an SPE process, store the full 64-bit value
287    of TID's ev register EV_REGNUM in DEST, getting the high bits from
288    EVRREGS and the low bits from the kernel via ptrace.  */
289 static void
290 read_spliced_spe_reg (int tid, int ev_regnum,
291                       struct gdb_evrregset_t *evrregs,
292                       char *dest)
293 {
294   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
295
296   /* Make sure we're trying to read an EV register; that's all we
297      handle.  */
298   gdb_assert (tdep->ppc_ev0_regnum <= ev_regnum
299               && ev_regnum <= tdep->ppc_ev31_regnum);
300
301   /* Make sure the sizes for the splicing add up.  */
302   gdb_assert (sizeof (evrregs->evr[0]) + sizeof (PTRACE_XFER_TYPE)
303               == register_size (current_gdbarch, ev_regnum));
304
305   {
306     /* The index of ev_regnum in evrregs->evr[].  */
307     int ev_index = ev_regnum - tdep->ppc_ev0_regnum;
308
309     /* The number of the corresponding general-purpose register, which
310        holds the lower 32 bits of the EV register.  */
311     int gpr_regnum = tdep->ppc_gp0_regnum + ev_index;
312
313     /* The offset of gpr_regnum in the process's uarea.  */
314     CORE_ADDR gpr_uoffset = ppc_register_u_addr (gpr_regnum);
315
316     /* The low word of the EV register's value.  */
317     PTRACE_XFER_TYPE low_word;
318
319     /* The PTRACE_PEEKUSR / PT_READ_U ptrace requests need to be able
320        to return arbitrary register values, so they can't return -1 to
321        indicate an error.  So we clear errno, and then check it after
322        the call.  */
323     errno = 0;
324     low_word = ptrace (PT_READ_U, tid, (PTRACE_ARG3_TYPE) gpr_uoffset, 0);
325   
326     if (errno != 0)
327       {
328         char message[128];
329         sprintf (message, "reading register %s (#%d)",
330                  REGISTER_NAME (ev_regnum), ev_regnum);
331         perror_with_name (message);
332       }
333
334     if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
335       {
336         memcpy (dest, &evrregs->evr[ev_index],
337                 sizeof (evrregs->evr[ev_index]));
338         * (PTRACE_XFER_TYPE *) (dest + sizeof (evrregs->evr[ev_index]))
339           = low_word;
340       }
341     else if (TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
342       {
343         * (PTRACE_XFER_TYPE *) dest = low_word;
344         memcpy (dest + sizeof (PTRACE_XFER_TYPE),
345                 &evrregs->evr[ev_index], sizeof (evrregs->evr[ev_index]));
346       }
347     else
348       gdb_assert (0);
349   }
350 }
351
352
353 /* On SPE machines, supply the full value of the SPE register REGNO
354    from TID.  This handles ev0 -- ev31 and acc, which are 64 bits
355    long, and spefscr, which is 32 bits long.  */
356 static void
357 fetch_spe_register (int tid, int regno)
358 {
359   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
360   struct gdb_evrregset_t evrregs;
361
362   get_spe_registers (tid, &evrregs);
363
364   if (tdep->ppc_ev0_regnum <= regno
365       && regno <= tdep->ppc_ev31_regnum)
366     {
367       char buf[MAX_REGISTER_SIZE];
368       read_spliced_spe_reg (tid, regno, &evrregs, buf);
369       regcache_raw_supply (current_regcache, regno, buf);
370     }
371   else if (regno == tdep->ppc_acc_regnum)
372     {
373       gdb_assert (sizeof (evrregs.acc)
374                   == register_size (current_gdbarch, regno));
375       regcache_raw_supply (current_regcache, regno, &evrregs.acc);
376     }
377   else if (regno == tdep->ppc_spefscr_regnum)
378     {
379       gdb_assert (sizeof (evrregs.spefscr)
380                   == register_size (current_gdbarch, regno));
381       regcache_raw_supply (current_regcache, regno, &evrregs.spefscr);
382     }
383   else
384     gdb_assert (0);
385 }
386
387 static void
388 fetch_register (int tid, int regno)
389 {
390   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
391   /* This isn't really an address.  But ptrace thinks of it as one.  */
392   CORE_ADDR regaddr = ppc_register_u_addr (regno);
393   int bytes_transferred;
394   unsigned int offset;         /* Offset of registers within the u area. */
395   char buf[MAX_REGISTER_SIZE];
396
397   /* Sanity check: this function should only be called to fetch raw
398      registers' values, never pseudoregisters' values.  */
399   if (tdep->ppc_gp0_regnum <= regno
400       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
401     gdb_assert (! tdep->ppc_gprs_pseudo_p);
402
403   if (altivec_register_p (regno))
404     {
405       /* If this is the first time through, or if it is not the first
406          time through, and we have comfirmed that there is kernel
407          support for such a ptrace request, then go and fetch the
408          register.  */
409       if (have_ptrace_getvrregs)
410        {
411          fetch_altivec_register (tid, regno);
412          return;
413        }
414      /* If we have discovered that there is no ptrace support for
415         AltiVec registers, fall through and return zeroes, because
416         regaddr will be -1 in this case.  */
417     }
418   else if (spe_register_p (regno))
419     {
420       fetch_spe_register (tid, regno);
421       return;
422     }
423
424   if (regaddr == -1)
425     {
426       memset (buf, '\0', DEPRECATED_REGISTER_RAW_SIZE (regno));   /* Supply zeroes */
427       regcache_raw_supply (current_regcache, regno, buf);
428       return;
429     }
430
431   /* Read the raw register using PTRACE_XFER_TYPE sized chunks.  On a
432      32-bit platform, 64-bit floating-point registers will require two
433      transfers.  */
434   for (bytes_transferred = 0;
435        bytes_transferred < register_size (current_gdbarch, regno);
436        bytes_transferred += sizeof (PTRACE_XFER_TYPE))
437     {
438       errno = 0;
439       *(PTRACE_XFER_TYPE *) & buf[bytes_transferred]
440         = ptrace (PT_READ_U, tid, (PTRACE_ARG3_TYPE) regaddr, 0);
441       regaddr += sizeof (PTRACE_XFER_TYPE);
442       if (errno != 0)
443         {
444           char message[128];
445           sprintf (message, "reading register %s (#%d)", 
446                    REGISTER_NAME (regno), regno);
447           perror_with_name (message);
448         }
449     }
450
451   /* Now supply the register.  Keep in mind that the regcache's idea
452      of the register's size may not be a multiple of sizeof
453      (PTRACE_XFER_TYPE).  */
454   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
455     {
456       /* Little-endian values are always found at the left end of the
457          bytes transferred.  */
458       regcache_raw_supply (current_regcache, regno, buf);
459     }
460   else if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
461     {
462       /* Big-endian values are found at the right end of the bytes
463          transferred.  */
464       size_t padding = (bytes_transferred
465                         - register_size (current_gdbarch, regno));
466       regcache_raw_supply (current_regcache, regno, buf + padding);
467     }
468   else 
469     gdb_assert (0);
470 }
471
472 static void
473 supply_vrregset (gdb_vrregset_t *vrregsetp)
474 {
475   int i;
476   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
477   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
478   int vrregsize = DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vr0_regnum);
479   int offset = vrregsize - DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vrsave_regnum);
480
481   for (i = 0; i < num_of_vrregs; i++)
482     {
483       /* The last 2 registers of this set are only 32 bit long, not
484          128.  However an offset is necessary only for VSCR because it
485          occupies a whole vector, while VRSAVE occupies a full 4 bytes
486          slot.  */
487       if (i == (num_of_vrregs - 2))
488         regcache_raw_supply (current_regcache, tdep->ppc_vr0_regnum + i,
489                              *vrregsetp + i * vrregsize + offset);
490       else
491         regcache_raw_supply (current_regcache, tdep->ppc_vr0_regnum + i,
492                              *vrregsetp + i * vrregsize);
493     }
494 }
495
496 static void
497 fetch_altivec_registers (int tid)
498 {
499   int ret;
500   gdb_vrregset_t regs;
501   
502   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
503   if (ret < 0)
504     {
505       if (errno == EIO)
506         {
507           have_ptrace_getvrregs = 0;
508           return;
509         }
510       perror_with_name ("Unable to fetch AltiVec registers");
511     }
512   supply_vrregset (&regs);
513 }
514
515 /* On SPE machines, fetch the full 64 bits of all the general-purpose
516    registers, as well as the SPE-specific registers 'acc' and
517    'spefscr'.  */
518 static void
519 fetch_spe_registers (int tid)
520 {
521   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
522   struct gdb_evrregset_t evrregs;
523   int i;
524
525   get_spe_registers (tid, &evrregs);
526
527   /* Splice and supply each of the EV registers.  */
528   for (i = 0; i < ppc_num_gprs; i++)
529     {
530       char buf[MAX_REGISTER_SIZE];
531
532       read_spliced_spe_reg (tid, tdep->ppc_ev0_regnum + i, &evrregs, buf);
533       regcache_raw_supply (current_regcache, tdep->ppc_ev0_regnum + i, buf);
534     }
535
536   /* Supply the SPE-specific registers.  */
537   regcache_raw_supply (current_regcache, tdep->ppc_acc_regnum, &evrregs.acc);
538   regcache_raw_supply (current_regcache, tdep->ppc_spefscr_regnum,
539                        &evrregs.spefscr);
540 }
541
542 static void 
543 fetch_ppc_registers (int tid)
544 {
545   int i;
546   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
547
548   if (! tdep->ppc_gprs_pseudo_p)
549     for (i = 0; i < ppc_num_gprs; i++)
550       fetch_register (tid, tdep->ppc_gp0_regnum + i);
551   if (tdep->ppc_fp0_regnum >= 0)
552     for (i = 0; i < ppc_num_fprs; i++)
553       fetch_register (tid, tdep->ppc_fp0_regnum + i);
554   fetch_register (tid, PC_REGNUM);
555   if (tdep->ppc_ps_regnum != -1)
556     fetch_register (tid, tdep->ppc_ps_regnum);
557   if (tdep->ppc_cr_regnum != -1)
558     fetch_register (tid, tdep->ppc_cr_regnum);
559   if (tdep->ppc_lr_regnum != -1)
560     fetch_register (tid, tdep->ppc_lr_regnum);
561   if (tdep->ppc_ctr_regnum != -1)
562     fetch_register (tid, tdep->ppc_ctr_regnum);
563   if (tdep->ppc_xer_regnum != -1)
564     fetch_register (tid, tdep->ppc_xer_regnum);
565   if (tdep->ppc_mq_regnum != -1)
566     fetch_register (tid, tdep->ppc_mq_regnum);
567   if (tdep->ppc_fpscr_regnum != -1)
568     fetch_register (tid, tdep->ppc_fpscr_regnum);
569   if (have_ptrace_getvrregs)
570     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
571       fetch_altivec_registers (tid);
572   if (tdep->ppc_ev0_regnum >= 0)
573     fetch_spe_registers (tid);
574 }
575
576 /* Fetch registers from the child process.  Fetch all registers if
577    regno == -1, otherwise fetch all general registers or all floating
578    point registers depending upon the value of regno.  */
579 void
580 fetch_inferior_registers (int regno)
581 {
582   /* Overload thread id onto process id */
583   int tid = TIDGET (inferior_ptid);
584
585   /* No thread id, just use process id */
586   if (tid == 0)
587     tid = PIDGET (inferior_ptid);
588
589   if (regno == -1)
590     fetch_ppc_registers (tid);
591   else 
592     fetch_register (tid, regno);
593 }
594
595 /* Store one register. */
596 static void
597 store_altivec_register (int tid, int regno)
598 {
599   int ret;
600   int offset = 0;
601   gdb_vrregset_t regs;
602   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
603   int vrregsize = DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vr0_regnum);
604
605   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
606   if (ret < 0)
607     {
608       if (errno == EIO)
609         {
610           have_ptrace_getvrregs = 0;
611           return;
612         }
613       perror_with_name ("Unable to fetch AltiVec register");
614     }
615
616   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
617      long on the hardware.  */
618   if (regno == (tdep->ppc_vrsave_regnum - 1))
619     offset = vrregsize - DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vrsave_regnum);
620
621   regcache_collect (regno,
622                     regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
623
624   ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
625   if (ret < 0)
626     perror_with_name ("Unable to store AltiVec register");
627 }
628
629 /* Assuming TID referrs to an SPE process, set the top halves of TID's
630    general-purpose registers and its SPE-specific registers to the
631    values in EVRREGSET.  If we don't support PTRACE_SETEVRREGS, do
632    nothing.
633
634    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
635    PTRACE_SETEVRREGS requests are supported is isolated here, and in
636    get_spe_registers.  */
637 static void
638 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
639 {
640   if (have_ptrace_getsetevrregs)
641     {
642       if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
643         return;
644       else
645         {
646           /* EIO means that the PTRACE_SETEVRREGS request isn't
647              supported; we fail silently, and don't try the call
648              again.  */
649           if (errno == EIO)
650             have_ptrace_getsetevrregs = 0;
651           else
652             /* Anything else needs to be reported.  */
653             perror_with_name ("Unable to set SPE registers");
654         }
655     }
656 }
657
658 /* Store the bytes at SRC as the contents of TID's EV register EV_REGNUM.
659    Write the less significant word to TID using ptrace, and copy the
660    more significant word to the appropriate slot in EVRREGS.  */
661 static void
662 write_spliced_spe_reg (int tid, int ev_regnum,
663                        struct gdb_evrregset_t *evrregs,
664                        char *src)
665 {
666   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
667
668   /* Make sure we're trying to write an EV register; that's all we
669      handle.  */
670   gdb_assert (tdep->ppc_ev0_regnum <= ev_regnum
671               && ev_regnum <= tdep->ppc_ev31_regnum);
672
673   /* Make sure the sizes for the splicing add up.  */
674   gdb_assert (sizeof (evrregs->evr[0]) + sizeof (PTRACE_XFER_TYPE)
675               == register_size (current_gdbarch, ev_regnum));
676
677   {
678     int ev_index = ev_regnum - tdep->ppc_ev0_regnum;
679
680     /* The number of the corresponding general-purpose register, which
681        holds the lower 32 bits of the EV register.  */
682     int gpr_regnum = tdep->ppc_gp0_regnum + ev_index;
683
684     /* The offset of gpr_regnum in the process's uarea.  */
685     CORE_ADDR gpr_uoffset = ppc_register_u_addr (gpr_regnum);
686
687     /* The PTRACE_POKEUSR / PT_WRITE_U ptrace requests need to be able
688        to return arbitrary register values, so they can't return -1 to
689        indicate an error.  So we clear errno, and check it again
690        afterwards.  */
691     errno = 0;
692
693     if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
694       {
695         memcpy (&evrregs->evr[ev_index], src, sizeof (evrregs->evr[ev_index]));
696         ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) gpr_uoffset,
697                 * (PTRACE_XFER_TYPE *) (src + sizeof (evrregs->evr[0])));
698       }
699     else if (TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
700       {
701         ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) gpr_uoffset,
702                 * (PTRACE_XFER_TYPE *) src);
703         memcpy (&evrregs->evr[ev_index], src + sizeof (PTRACE_XFER_TYPE),
704                 sizeof (evrregs->evr[ev_index]));
705       }
706     else 
707       gdb_assert (0);
708
709     if (errno != 0)
710       {
711         char message[128];
712         sprintf (message, "writing register %s (#%d)", 
713                  REGISTER_NAME (ev_regnum), ev_regnum);
714         perror_with_name (message);
715       }
716   }
717 }
718
719 /* Write GDB's value for the SPE register REGNO to TID.  */
720 static void
721 store_spe_register (int tid, int regno)
722 {
723   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
724   struct gdb_evrregset_t evrregs;
725
726   /* We can only read and write the entire EVR register set at a time,
727      so to write just a single register, we do a read-modify-write
728      maneuver.  */
729   get_spe_registers (tid, &evrregs);
730
731   if (tdep->ppc_ev0_regnum >= 0
732       && tdep->ppc_ev0_regnum <= regno && regno <= tdep->ppc_ev31_regnum)
733     {
734       char buf[MAX_REGISTER_SIZE];
735       regcache_collect (regno, buf);
736       write_spliced_spe_reg (tid, regno, &evrregs, buf);
737     }
738   else if (tdep->ppc_acc_regnum >= 0
739            && regno == tdep->ppc_acc_regnum)
740     {
741       gdb_assert (sizeof (evrregs.acc)
742                   == register_size (current_gdbarch, regno));
743       regcache_collect (regno, &evrregs.acc);
744     }
745   else if (tdep->ppc_spefscr_regnum >= 0
746            && regno == tdep->ppc_spefscr_regnum)
747     {
748       gdb_assert (sizeof (evrregs.spefscr)
749                   == register_size (current_gdbarch, regno));
750       regcache_collect (regno, &evrregs.spefscr);
751     }
752   else
753     gdb_assert (0);
754
755   /* Write back the modified register set.  */
756   set_spe_registers (tid, &evrregs);
757 }
758
759 static void
760 store_register (int tid, int regno)
761 {
762   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
763   /* This isn't really an address.  But ptrace thinks of it as one.  */
764   CORE_ADDR regaddr = ppc_register_u_addr (regno);
765   int i;
766   size_t bytes_to_transfer;
767   char buf[MAX_REGISTER_SIZE];
768
769   /* Sanity check: this function should only be called to store raw
770      registers' values, never pseudoregisters' values.  */
771   if (tdep->ppc_gp0_regnum <= regno
772       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
773     gdb_assert (! tdep->ppc_gprs_pseudo_p);
774
775   if (altivec_register_p (regno))
776     {
777       store_altivec_register (tid, regno);
778       return;
779     }
780   else if (spe_register_p (regno))
781     {
782       store_spe_register (tid, regno);
783       return;
784     }
785
786   if (regaddr == -1)
787     return;
788
789   /* First collect the register.  Keep in mind that the regcache's
790      idea of the register's size may not be a multiple of sizeof
791      (PTRACE_XFER_TYPE).  */
792   memset (buf, 0, sizeof buf);
793   bytes_to_transfer = align_up (register_size (current_gdbarch, regno),
794                                 sizeof (PTRACE_XFER_TYPE));
795   if (TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
796     {
797       /* Little-endian values always sit at the left end of the buffer.  */
798       regcache_raw_collect (current_regcache, regno, buf);
799     }
800   else if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
801     {
802       /* Big-endian values sit at the right end of the buffer.  */
803       size_t padding = (bytes_to_transfer
804                         - register_size (current_gdbarch, regno));
805       regcache_raw_collect (current_regcache, regno, buf + padding);
806     }
807
808   for (i = 0; i < bytes_to_transfer; i += sizeof (PTRACE_XFER_TYPE))
809     {
810       errno = 0;
811       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
812               *(PTRACE_XFER_TYPE *) & buf[i]);
813       regaddr += sizeof (PTRACE_XFER_TYPE);
814
815       if (errno == EIO 
816           && regno == tdep->ppc_fpscr_regnum)
817         {
818           /* Some older kernel versions don't allow fpscr to be written.  */
819           continue;
820         }
821
822       if (errno != 0)
823         {
824           char message[128];
825           sprintf (message, "writing register %s (#%d)", 
826                    REGISTER_NAME (regno), regno);
827           perror_with_name (message);
828         }
829     }
830 }
831
832 static void
833 fill_vrregset (gdb_vrregset_t *vrregsetp)
834 {
835   int i;
836   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
837   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
838   int vrregsize = DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vr0_regnum);
839   int offset = vrregsize - DEPRECATED_REGISTER_RAW_SIZE (tdep->ppc_vrsave_regnum);
840
841   for (i = 0; i < num_of_vrregs; i++)
842     {
843       /* The last 2 registers of this set are only 32 bit long, not
844          128, but only VSCR is fetched as a 16 bytes quantity.  */
845       if (i == (num_of_vrregs - 2))
846         regcache_collect (tdep->ppc_vr0_regnum + i,
847                           *vrregsetp + i * vrregsize + offset);
848       else
849         regcache_collect (tdep->ppc_vr0_regnum + i, *vrregsetp + i * vrregsize);
850     }
851 }
852
853 static void
854 store_altivec_registers (int tid)
855 {
856   int ret;
857   gdb_vrregset_t regs;
858
859   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
860   if (ret < 0)
861     {
862       if (errno == EIO)
863         {
864           have_ptrace_getvrregs = 0;
865           return;
866         }
867       perror_with_name ("Couldn't get AltiVec registers");
868     }
869
870   fill_vrregset (&regs);
871   
872   if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
873     perror_with_name ("Couldn't write AltiVec registers");
874 }
875
876 static void
877 store_spe_registers (int tid)
878 {
879   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
880   struct gdb_evrregset_t evrregs;
881   int i;
882
883   /* The code below should store to every field of evrregs; if that
884      doesn't happen, make it obvious by initializing it with
885      suspicious values.  */
886   memset (&evrregs, 42, sizeof (evrregs));
887
888   for (i = 0; i < ppc_num_gprs; i++)
889     {
890       char buf[MAX_REGISTER_SIZE];
891
892       regcache_collect (tdep->ppc_ev0_regnum + i, buf);
893       write_spliced_spe_reg (tid, tdep->ppc_ev0_regnum + i, &evrregs, buf);
894     }
895
896   gdb_assert (sizeof (evrregs.acc)
897               == register_size (current_gdbarch, tdep->ppc_acc_regnum));
898   regcache_collect (tdep->ppc_acc_regnum, &evrregs.acc);
899   gdb_assert (sizeof (evrregs.spefscr)
900               == register_size (current_gdbarch, tdep->ppc_spefscr_regnum));
901   regcache_collect (tdep->ppc_acc_regnum, &evrregs.spefscr);
902
903   set_spe_registers (tid, &evrregs);
904 }
905
906 static void
907 store_ppc_registers (int tid)
908 {
909   int i;
910   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
911   
912   if (! tdep->ppc_gprs_pseudo_p)
913     for (i = 0; i < ppc_num_gprs; i++)
914       store_register (tid, tdep->ppc_gp0_regnum + i);
915   if (tdep->ppc_fp0_regnum >= 0)
916     for (i = 0; i < ppc_num_fprs; i++)
917       store_register (tid, tdep->ppc_fp0_regnum + i);
918   store_register (tid, PC_REGNUM);
919   if (tdep->ppc_ps_regnum != -1)
920     store_register (tid, tdep->ppc_ps_regnum);
921   if (tdep->ppc_cr_regnum != -1)
922     store_register (tid, tdep->ppc_cr_regnum);
923   if (tdep->ppc_lr_regnum != -1)
924     store_register (tid, tdep->ppc_lr_regnum);
925   if (tdep->ppc_ctr_regnum != -1)
926     store_register (tid, tdep->ppc_ctr_regnum);
927   if (tdep->ppc_xer_regnum != -1)
928     store_register (tid, tdep->ppc_xer_regnum);
929   if (tdep->ppc_mq_regnum != -1)
930     store_register (tid, tdep->ppc_mq_regnum);
931   if (tdep->ppc_fpscr_regnum != -1)
932     store_register (tid, tdep->ppc_fpscr_regnum);
933   if (have_ptrace_getvrregs)
934     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
935       store_altivec_registers (tid);
936   if (tdep->ppc_ev0_regnum >= 0)
937     store_spe_registers (tid);
938 }
939
940 void
941 store_inferior_registers (int regno)
942 {
943   /* Overload thread id onto process id */
944   int tid = TIDGET (inferior_ptid);
945
946   /* No thread id, just use process id */
947   if (tid == 0)
948     tid = PIDGET (inferior_ptid);
949
950   if (regno >= 0)
951     store_register (tid, regno);
952   else
953     store_ppc_registers (tid);
954 }
955
956 void
957 supply_gregset (gdb_gregset_t *gregsetp)
958 {
959   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
960      interface, and not the wordsize of the program's ABI.  */
961   int wordsize = sizeof (PTRACE_XFER_TYPE);
962   ppc_linux_supply_gregset (current_regcache, -1, gregsetp,
963                             sizeof (gdb_gregset_t), wordsize);
964 }
965
966 static void
967 right_fill_reg (int regnum, void *reg)
968 {
969   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
970      interface, and not the wordsize of the program's ABI.  */
971   int wordsize = sizeof (PTRACE_XFER_TYPE);
972   /* Right fill the register.  */
973   regcache_raw_collect (current_regcache, regnum,
974                         ((bfd_byte *) reg
975                          + wordsize
976                          - register_size (current_gdbarch, regnum)));
977 }
978
979 void
980 fill_gregset (gdb_gregset_t *gregsetp, int regno)
981 {
982   int regi;
983   elf_greg_t *regp = (elf_greg_t *) gregsetp;
984   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); 
985   const int elf_ngreg = 48;
986
987
988   /* Start with zeros.  */
989   memset (regp, 0, elf_ngreg * sizeof (*regp));
990
991   for (regi = 0; regi < ppc_num_gprs; regi++)
992     {
993       if ((regno == -1) || regno == tdep->ppc_gp0_regnum + regi)
994         right_fill_reg (tdep->ppc_gp0_regnum + regi, (regp + PT_R0 + regi));
995     }
996
997   if ((regno == -1) || regno == PC_REGNUM)
998     right_fill_reg (PC_REGNUM, regp + PT_NIP);
999   if ((regno == -1) || regno == tdep->ppc_lr_regnum)
1000     right_fill_reg (tdep->ppc_lr_regnum, regp + PT_LNK);
1001   if ((regno == -1) || regno == tdep->ppc_cr_regnum)
1002     regcache_collect (tdep->ppc_cr_regnum, regp + PT_CCR);
1003   if ((regno == -1) || regno == tdep->ppc_xer_regnum)
1004     regcache_collect (tdep->ppc_xer_regnum, regp + PT_XER);
1005   if ((regno == -1) || regno == tdep->ppc_ctr_regnum)
1006     right_fill_reg (tdep->ppc_ctr_regnum, regp + PT_CTR);
1007 #ifdef PT_MQ
1008   if (((regno == -1) || regno == tdep->ppc_mq_regnum)
1009       && (tdep->ppc_mq_regnum != -1))
1010     right_fill_reg (tdep->ppc_mq_regnum, regp + PT_MQ);
1011 #endif
1012   if ((regno == -1) || regno == tdep->ppc_ps_regnum)
1013     right_fill_reg (tdep->ppc_ps_regnum, regp + PT_MSR);
1014 }
1015
1016 void
1017 supply_fpregset (gdb_fpregset_t * fpregsetp)
1018 {
1019   ppc_linux_supply_fpregset (NULL, current_regcache, -1, fpregsetp,
1020                              sizeof (gdb_fpregset_t));
1021 }
1022
1023 /* Given a pointer to a floating point register set in /proc format
1024    (fpregset_t *), update the register specified by REGNO from gdb's
1025    idea of the current floating point register set.  If REGNO is -1,
1026    update them all.  */
1027 void
1028 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
1029 {
1030   int regi;
1031   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); 
1032   bfd_byte *fpp = (void *) fpregsetp;
1033   
1034   if (ppc_floating_point_unit_p (current_gdbarch))
1035     {
1036       for (regi = 0; regi < ppc_num_fprs; regi++)
1037         {
1038           if ((regno == -1) || (regno == tdep->ppc_fp0_regnum + regi))
1039             regcache_collect (tdep->ppc_fp0_regnum + regi, fpp + 8 * regi);
1040         }
1041       if (regno == -1 || regno == tdep->ppc_fpscr_regnum)
1042         right_fill_reg (tdep->ppc_fpscr_regnum, (fpp + 8 * 32));
1043     }
1044 }