1 /* PPC GNU/Linux native support.
3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdb_string.h"
27 #include "gdb_assert.h"
29 #include "linux-nat.h"
32 #include <sys/types.h>
33 #include <sys/param.h>
36 #include <sys/ioctl.h>
39 #include <sys/procfs.h>
40 #include <sys/ptrace.h>
42 /* Prototypes for supply_gregset etc. */
45 #include "ppc-linux-tdep.h"
47 /* Required when using the AUXV. */
48 #include "elf/common.h"
51 /* This sometimes isn't defined. */
59 #ifndef PPC_FEATURE_BOOKE
60 #define PPC_FEATURE_BOOKE 0x00008000
63 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
64 configure time check. Some older glibc's (for instance 2.2.1)
65 don't have a specific powerpc version of ptrace.h, and fall back on
66 a generic one. In such cases, sys/ptrace.h defines
67 PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
68 ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
69 PTRACE_SETVRREGS to be. This also makes a configury check pretty
72 /* These definitions should really come from the glibc header files,
73 but Glibc doesn't know about the vrregs yet. */
74 #ifndef PTRACE_GETVRREGS
75 #define PTRACE_GETVRREGS 18
76 #define PTRACE_SETVRREGS 19
79 /* PTRACE requests for POWER7 VSX registers. */
80 #ifndef PTRACE_GETVSXREGS
81 #define PTRACE_GETVSXREGS 27
82 #define PTRACE_SETVSXREGS 28
85 /* Similarly for the ptrace requests for getting / setting the SPE
86 registers (ev0 -- ev31, acc, and spefscr). See the description of
87 gdb_evrregset_t for details. */
88 #ifndef PTRACE_GETEVRREGS
89 #define PTRACE_GETEVRREGS 20
90 #define PTRACE_SETEVRREGS 21
93 /* Similarly for the hardware watchpoint support. */
94 #ifndef PTRACE_GET_DEBUGREG
95 #define PTRACE_GET_DEBUGREG 25
97 #ifndef PTRACE_SET_DEBUGREG
98 #define PTRACE_SET_DEBUGREG 26
100 #ifndef PTRACE_GETSIGINFO
101 #define PTRACE_GETSIGINFO 0x4202
104 /* This oddity is because the Linux kernel defines elf_vrregset_t as
105 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
106 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
107 the vrsave as an extra 4 bytes at the end. I opted for creating a
108 flat array of chars, so that it is easier to manipulate for gdb.
110 There are 32 vector registers 16 bytes longs, plus a VSCR register
111 which is only 4 bytes long, but is fetched as a 16 bytes
112 quantity. Up to here we have the elf_vrregset_t structure.
113 Appended to this there is space for the VRSAVE register: 4 bytes.
114 Even though this vrsave register is not included in the regset
115 typedef, it is handled by the ptrace requests.
117 Note that GNU/Linux doesn't support little endian PPC hardware,
118 therefore the offset at which the real value of the VSCR register
119 is located will be always 12 bytes.
121 The layout is like this (where x is the actual value of the vscr reg): */
125 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
126 <-------> <-------><-------><->
131 #define SIZEOF_VRREGS 33*16+4
133 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
135 /* This is the layout of the POWER7 VSX registers and the way they overlap
136 with the existing FPR and VMX registers.
138 VSR doubleword 0 VSR doubleword 1
139 ----------------------------------------------------------------
141 ----------------------------------------------------------------
143 ----------------------------------------------------------------
146 ----------------------------------------------------------------
147 VSR[30] | FPR[30] | |
148 ----------------------------------------------------------------
149 VSR[31] | FPR[31] | |
150 ----------------------------------------------------------------
152 ----------------------------------------------------------------
154 ----------------------------------------------------------------
157 ----------------------------------------------------------------
159 ----------------------------------------------------------------
161 ----------------------------------------------------------------
163 VSX has 64 128bit registers. The first 32 registers overlap with
164 the FP registers (doubleword 0) and hence extend them with additional
165 64 bits (doubleword 1). The other 32 regs overlap with the VMX
167 #define SIZEOF_VSXREGS 32*8
169 typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
171 /* On PPC processors that support the the Signal Processing Extension
172 (SPE) APU, the general-purpose registers are 64 bits long.
173 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
174 ptrace calls only access the lower half of each register, to allow
175 them to behave the same way they do on non-SPE systems. There's a
176 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
177 read and write the top halves of all the general-purpose registers
178 at once, along with some SPE-specific registers.
180 GDB itself continues to claim the general-purpose registers are 32
181 bits long. It has unnamed raw registers that hold the upper halves
182 of the gprs, and the the full 64-bit SIMD views of the registers,
183 'ev0' -- 'ev31', are pseudo-registers that splice the top and
184 bottom halves together.
186 This is the structure filled in by PTRACE_GETEVRREGS and written to
187 the inferior's registers by PTRACE_SETEVRREGS. */
188 struct gdb_evrregset_t
190 unsigned long evr[32];
191 unsigned long long acc;
192 unsigned long spefscr;
195 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
196 PTRACE_SETVSXREGS requests, for reading and writing the VSX
197 POWER7 registers 0 through 31. Zero if we've tried one of them and
198 gotten an error. Note that VSX registers 32 through 63 overlap
199 with VR registers 0 through 31. */
200 int have_ptrace_getsetvsxregs = 1;
202 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
203 PTRACE_SETVRREGS requests, for reading and writing the Altivec
204 registers. Zero if we've tried one of them and gotten an
206 int have_ptrace_getvrregs = 1;
208 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
209 PTRACE_SETEVRREGS requests, for reading and writing the SPE
210 registers. Zero if we've tried one of them and gotten an
212 int have_ptrace_getsetevrregs = 1;
215 /* registers layout, as presented by the ptrace interface:
216 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
217 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
218 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
219 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
220 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
221 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
222 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
223 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
224 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
228 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
231 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
232 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
233 interface, and not the wordsize of the program's ABI. */
234 int wordsize = sizeof (long);
236 /* General purpose registers occupy 1 slot each in the buffer */
237 if (regno >= tdep->ppc_gp0_regnum
238 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
239 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
241 /* Floating point regs: eight bytes each in both 32- and 64-bit
242 ptrace interfaces. Thus, two slots each in 32-bit interface, one
243 slot each in 64-bit interface. */
244 if (tdep->ppc_fp0_regnum >= 0
245 && regno >= tdep->ppc_fp0_regnum
246 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
247 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
249 /* UISA special purpose registers: 1 slot each */
250 if (regno == gdbarch_pc_regnum (gdbarch))
251 u_addr = PT_NIP * wordsize;
252 if (regno == tdep->ppc_lr_regnum)
253 u_addr = PT_LNK * wordsize;
254 if (regno == tdep->ppc_cr_regnum)
255 u_addr = PT_CCR * wordsize;
256 if (regno == tdep->ppc_xer_regnum)
257 u_addr = PT_XER * wordsize;
258 if (regno == tdep->ppc_ctr_regnum)
259 u_addr = PT_CTR * wordsize;
261 if (regno == tdep->ppc_mq_regnum)
262 u_addr = PT_MQ * wordsize;
264 if (regno == tdep->ppc_ps_regnum)
265 u_addr = PT_MSR * wordsize;
266 if (regno == PPC_ORIG_R3_REGNUM)
267 u_addr = PT_ORIG_R3 * wordsize;
268 if (regno == PPC_TRAP_REGNUM)
269 u_addr = PT_TRAP * wordsize;
270 if (tdep->ppc_fpscr_regnum >= 0
271 && regno == tdep->ppc_fpscr_regnum)
273 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
274 kernel headers incorrectly contained the 32-bit definition of
275 PT_FPSCR. For the 32-bit definition, floating-point
276 registers occupy two 32-bit "slots", and the FPSCR lives in
277 the secondhalf of such a slot-pair (hence +1). For 64-bit,
278 the FPSCR instead occupies the full 64-bit 2-word-slot and
279 hence no adjustment is necessary. Hack around this. */
280 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
281 u_addr = (48 + 32) * wordsize;
283 u_addr = PT_FPSCR * wordsize;
288 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
289 registers set mechanism, as opposed to the interface for all the
290 other registers, that stores/fetches each register individually. */
292 fetch_vsx_register (struct regcache *regcache, int tid, int regno)
295 gdb_vsxregset_t regs;
296 struct gdbarch *gdbarch = get_regcache_arch (regcache);
297 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
298 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
300 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
305 have_ptrace_getsetvsxregs = 0;
308 perror_with_name (_("Unable to fetch VSX register"));
311 regcache_raw_supply (regcache, regno,
312 regs + (regno - tdep->ppc_vsr0_upper_regnum)
316 /* The Linux kernel ptrace interface for AltiVec registers uses the
317 registers set mechanism, as opposed to the interface for all the
318 other registers, that stores/fetches each register individually. */
320 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
325 struct gdbarch *gdbarch = get_regcache_arch (regcache);
326 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
327 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
329 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
334 have_ptrace_getvrregs = 0;
337 perror_with_name (_("Unable to fetch AltiVec register"));
340 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
341 long on the hardware. We deal only with the lower 4 bytes of the
342 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
343 there is no need to define an offset for it. */
344 if (regno == (tdep->ppc_vrsave_regnum - 1))
345 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
347 regcache_raw_supply (regcache, regno,
348 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
351 /* Fetch the top 32 bits of TID's general-purpose registers and the
352 SPE-specific registers, and place the results in EVRREGSET. If we
353 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
356 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
357 PTRACE_SETEVRREGS requests are supported is isolated here, and in
358 set_spe_registers. */
360 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
362 if (have_ptrace_getsetevrregs)
364 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
368 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
369 we just return zeros. */
371 have_ptrace_getsetevrregs = 0;
373 /* Anything else needs to be reported. */
374 perror_with_name (_("Unable to fetch SPE registers"));
378 memset (evrregset, 0, sizeof (*evrregset));
381 /* Supply values from TID for SPE-specific raw registers: the upper
382 halves of the GPRs, the accumulator, and the spefscr. REGNO must
383 be the number of an upper half register, acc, spefscr, or -1 to
384 supply the values of all registers. */
386 fetch_spe_register (struct regcache *regcache, int tid, int regno)
388 struct gdbarch *gdbarch = get_regcache_arch (regcache);
389 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
390 struct gdb_evrregset_t evrregs;
392 gdb_assert (sizeof (evrregs.evr[0])
393 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
394 gdb_assert (sizeof (evrregs.acc)
395 == register_size (gdbarch, tdep->ppc_acc_regnum));
396 gdb_assert (sizeof (evrregs.spefscr)
397 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
399 get_spe_registers (tid, &evrregs);
405 for (i = 0; i < ppc_num_gprs; i++)
406 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
409 else if (tdep->ppc_ev0_upper_regnum <= regno
410 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
411 regcache_raw_supply (regcache, regno,
412 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
415 || regno == tdep->ppc_acc_regnum)
416 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
419 || regno == tdep->ppc_spefscr_regnum)
420 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
425 fetch_register (struct regcache *regcache, int tid, int regno)
427 struct gdbarch *gdbarch = get_regcache_arch (regcache);
428 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
429 /* This isn't really an address. But ptrace thinks of it as one. */
430 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
431 int bytes_transferred;
432 unsigned int offset; /* Offset of registers within the u area. */
433 char buf[MAX_REGISTER_SIZE];
435 if (altivec_register_p (gdbarch, regno))
437 /* If this is the first time through, or if it is not the first
438 time through, and we have comfirmed that there is kernel
439 support for such a ptrace request, then go and fetch the
441 if (have_ptrace_getvrregs)
443 fetch_altivec_register (regcache, tid, regno);
446 /* If we have discovered that there is no ptrace support for
447 AltiVec registers, fall through and return zeroes, because
448 regaddr will be -1 in this case. */
450 if (vsx_register_p (gdbarch, regno))
452 if (have_ptrace_getsetvsxregs)
454 fetch_vsx_register (regcache, tid, regno);
458 else if (spe_register_p (gdbarch, regno))
460 fetch_spe_register (regcache, tid, regno);
466 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
467 regcache_raw_supply (regcache, regno, buf);
471 /* Read the raw register using sizeof(long) sized chunks. On a
472 32-bit platform, 64-bit floating-point registers will require two
474 for (bytes_transferred = 0;
475 bytes_transferred < register_size (gdbarch, regno);
476 bytes_transferred += sizeof (long))
479 *(long *) &buf[bytes_transferred]
480 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
481 regaddr += sizeof (long);
485 sprintf (message, "reading register %s (#%d)",
486 gdbarch_register_name (gdbarch, regno), regno);
487 perror_with_name (message);
491 /* Now supply the register. Keep in mind that the regcache's idea
492 of the register's size may not be a multiple of sizeof
494 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
496 /* Little-endian values are always found at the left end of the
497 bytes transferred. */
498 regcache_raw_supply (regcache, regno, buf);
500 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
502 /* Big-endian values are found at the right end of the bytes
504 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
505 regcache_raw_supply (regcache, regno, buf + padding);
508 internal_error (__FILE__, __LINE__,
509 _("fetch_register: unexpected byte order: %d"),
510 gdbarch_byte_order (gdbarch));
514 supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
517 struct gdbarch *gdbarch = get_regcache_arch (regcache);
518 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
519 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
521 for (i = 0; i < ppc_num_vshrs; i++)
523 regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
524 *vsxregsetp + i * vsxregsize);
529 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
532 struct gdbarch *gdbarch = get_regcache_arch (regcache);
533 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
534 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
535 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
536 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
538 for (i = 0; i < num_of_vrregs; i++)
540 /* The last 2 registers of this set are only 32 bit long, not
541 128. However an offset is necessary only for VSCR because it
542 occupies a whole vector, while VRSAVE occupies a full 4 bytes
544 if (i == (num_of_vrregs - 2))
545 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
546 *vrregsetp + i * vrregsize + offset);
548 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
549 *vrregsetp + i * vrregsize);
554 fetch_vsx_registers (struct regcache *regcache, int tid)
557 gdb_vsxregset_t regs;
559 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
564 have_ptrace_getsetvsxregs = 0;
567 perror_with_name (_("Unable to fetch VSX registers"));
569 supply_vsxregset (regcache, ®s);
573 fetch_altivec_registers (struct regcache *regcache, int tid)
578 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
583 have_ptrace_getvrregs = 0;
586 perror_with_name (_("Unable to fetch AltiVec registers"));
588 supply_vrregset (regcache, ®s);
592 fetch_ppc_registers (struct regcache *regcache, int tid)
595 struct gdbarch *gdbarch = get_regcache_arch (regcache);
596 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
598 for (i = 0; i < ppc_num_gprs; i++)
599 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
600 if (tdep->ppc_fp0_regnum >= 0)
601 for (i = 0; i < ppc_num_fprs; i++)
602 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
603 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
604 if (tdep->ppc_ps_regnum != -1)
605 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
606 if (tdep->ppc_cr_regnum != -1)
607 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
608 if (tdep->ppc_lr_regnum != -1)
609 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
610 if (tdep->ppc_ctr_regnum != -1)
611 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
612 if (tdep->ppc_xer_regnum != -1)
613 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
614 if (tdep->ppc_mq_regnum != -1)
615 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
616 if (ppc_linux_trap_reg_p (gdbarch))
618 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
619 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
621 if (tdep->ppc_fpscr_regnum != -1)
622 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
623 if (have_ptrace_getvrregs)
624 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
625 fetch_altivec_registers (regcache, tid);
626 if (have_ptrace_getsetvsxregs)
627 if (tdep->ppc_vsr0_upper_regnum != -1)
628 fetch_vsx_registers (regcache, tid);
629 if (tdep->ppc_ev0_upper_regnum >= 0)
630 fetch_spe_register (regcache, tid, -1);
633 /* Fetch registers from the child process. Fetch all registers if
634 regno == -1, otherwise fetch all general registers or all floating
635 point registers depending upon the value of regno. */
637 ppc_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
639 /* Overload thread id onto process id */
640 int tid = TIDGET (inferior_ptid);
642 /* No thread id, just use process id */
644 tid = PIDGET (inferior_ptid);
647 fetch_ppc_registers (regcache, tid);
649 fetch_register (regcache, tid, regno);
652 /* Store one VSX register. */
654 store_vsx_register (const struct regcache *regcache, int tid, int regno)
657 gdb_vsxregset_t regs;
658 struct gdbarch *gdbarch = get_regcache_arch (regcache);
659 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
660 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
662 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, ®s);
667 have_ptrace_getsetvsxregs = 0;
670 perror_with_name (_("Unable to fetch VSX register"));
673 regcache_raw_collect (regcache, regno, regs +
674 (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
676 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, ®s);
678 perror_with_name (_("Unable to store VSX register"));
681 /* Store one register. */
683 store_altivec_register (const struct regcache *regcache, int tid, int regno)
688 struct gdbarch *gdbarch = get_regcache_arch (regcache);
689 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
690 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
692 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
697 have_ptrace_getvrregs = 0;
700 perror_with_name (_("Unable to fetch AltiVec register"));
703 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
704 long on the hardware. */
705 if (regno == (tdep->ppc_vrsave_regnum - 1))
706 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
708 regcache_raw_collect (regcache, regno,
709 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
711 ret = ptrace (PTRACE_SETVRREGS, tid, 0, ®s);
713 perror_with_name (_("Unable to store AltiVec register"));
716 /* Assuming TID referrs to an SPE process, set the top halves of TID's
717 general-purpose registers and its SPE-specific registers to the
718 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
721 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
722 PTRACE_SETEVRREGS requests are supported is isolated here, and in
723 get_spe_registers. */
725 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
727 if (have_ptrace_getsetevrregs)
729 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
733 /* EIO means that the PTRACE_SETEVRREGS request isn't
734 supported; we fail silently, and don't try the call
737 have_ptrace_getsetevrregs = 0;
739 /* Anything else needs to be reported. */
740 perror_with_name (_("Unable to set SPE registers"));
745 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
746 If REGNO is -1, write the values of all the SPE-specific
749 store_spe_register (const struct regcache *regcache, int tid, int regno)
751 struct gdbarch *gdbarch = get_regcache_arch (regcache);
752 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
753 struct gdb_evrregset_t evrregs;
755 gdb_assert (sizeof (evrregs.evr[0])
756 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
757 gdb_assert (sizeof (evrregs.acc)
758 == register_size (gdbarch, tdep->ppc_acc_regnum));
759 gdb_assert (sizeof (evrregs.spefscr)
760 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
763 /* Since we're going to write out every register, the code below
764 should store to every field of evrregs; if that doesn't happen,
765 make it obvious by initializing it with suspicious values. */
766 memset (&evrregs, 42, sizeof (evrregs));
768 /* We can only read and write the entire EVR register set at a
769 time, so to write just a single register, we do a
770 read-modify-write maneuver. */
771 get_spe_registers (tid, &evrregs);
777 for (i = 0; i < ppc_num_gprs; i++)
778 regcache_raw_collect (regcache,
779 tdep->ppc_ev0_upper_regnum + i,
782 else if (tdep->ppc_ev0_upper_regnum <= regno
783 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
784 regcache_raw_collect (regcache, regno,
785 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
788 || regno == tdep->ppc_acc_regnum)
789 regcache_raw_collect (regcache,
790 tdep->ppc_acc_regnum,
794 || regno == tdep->ppc_spefscr_regnum)
795 regcache_raw_collect (regcache,
796 tdep->ppc_spefscr_regnum,
799 /* Write back the modified register set. */
800 set_spe_registers (tid, &evrregs);
804 store_register (const struct regcache *regcache, int tid, int regno)
806 struct gdbarch *gdbarch = get_regcache_arch (regcache);
807 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
808 /* This isn't really an address. But ptrace thinks of it as one. */
809 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
811 size_t bytes_to_transfer;
812 char buf[MAX_REGISTER_SIZE];
814 if (altivec_register_p (gdbarch, regno))
816 store_altivec_register (regcache, tid, regno);
819 if (vsx_register_p (gdbarch, regno))
821 store_vsx_register (regcache, tid, regno);
824 else if (spe_register_p (gdbarch, regno))
826 store_spe_register (regcache, tid, regno);
833 /* First collect the register. Keep in mind that the regcache's
834 idea of the register's size may not be a multiple of sizeof
836 memset (buf, 0, sizeof buf);
837 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
838 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
840 /* Little-endian values always sit at the left end of the buffer. */
841 regcache_raw_collect (regcache, regno, buf);
843 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
845 /* Big-endian values sit at the right end of the buffer. */
846 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
847 regcache_raw_collect (regcache, regno, buf + padding);
850 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
853 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
855 regaddr += sizeof (long);
858 && (regno == tdep->ppc_fpscr_regnum
859 || regno == PPC_ORIG_R3_REGNUM
860 || regno == PPC_TRAP_REGNUM))
862 /* Some older kernel versions don't allow fpscr, orig_r3
863 or trap to be written. */
870 sprintf (message, "writing register %s (#%d)",
871 gdbarch_register_name (gdbarch, regno), regno);
872 perror_with_name (message);
878 fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
881 struct gdbarch *gdbarch = get_regcache_arch (regcache);
882 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
883 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
885 for (i = 0; i < ppc_num_vshrs; i++)
886 regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
887 *vsxregsetp + i * vsxregsize);
891 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
894 struct gdbarch *gdbarch = get_regcache_arch (regcache);
895 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
896 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
897 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
898 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
900 for (i = 0; i < num_of_vrregs; i++)
902 /* The last 2 registers of this set are only 32 bit long, not
903 128, but only VSCR is fetched as a 16 bytes quantity. */
904 if (i == (num_of_vrregs - 2))
905 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
906 *vrregsetp + i * vrregsize + offset);
908 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
909 *vrregsetp + i * vrregsize);
914 store_vsx_registers (const struct regcache *regcache, int tid)
917 gdb_vsxregset_t regs;
919 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
924 have_ptrace_getsetvsxregs = 0;
927 perror_with_name (_("Couldn't get VSX registers"));
930 fill_vsxregset (regcache, ®s);
932 if (ptrace (PTRACE_SETVSXREGS, tid, 0, ®s) < 0)
933 perror_with_name (_("Couldn't write VSX registers"));
937 store_altivec_registers (const struct regcache *regcache, int tid)
942 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
947 have_ptrace_getvrregs = 0;
950 perror_with_name (_("Couldn't get AltiVec registers"));
953 fill_vrregset (regcache, ®s);
955 if (ptrace (PTRACE_SETVRREGS, tid, 0, ®s) < 0)
956 perror_with_name (_("Couldn't write AltiVec registers"));
960 store_ppc_registers (const struct regcache *regcache, int tid)
963 struct gdbarch *gdbarch = get_regcache_arch (regcache);
964 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
966 for (i = 0; i < ppc_num_gprs; i++)
967 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
968 if (tdep->ppc_fp0_regnum >= 0)
969 for (i = 0; i < ppc_num_fprs; i++)
970 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
971 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
972 if (tdep->ppc_ps_regnum != -1)
973 store_register (regcache, tid, tdep->ppc_ps_regnum);
974 if (tdep->ppc_cr_regnum != -1)
975 store_register (regcache, tid, tdep->ppc_cr_regnum);
976 if (tdep->ppc_lr_regnum != -1)
977 store_register (regcache, tid, tdep->ppc_lr_regnum);
978 if (tdep->ppc_ctr_regnum != -1)
979 store_register (regcache, tid, tdep->ppc_ctr_regnum);
980 if (tdep->ppc_xer_regnum != -1)
981 store_register (regcache, tid, tdep->ppc_xer_regnum);
982 if (tdep->ppc_mq_regnum != -1)
983 store_register (regcache, tid, tdep->ppc_mq_regnum);
984 if (tdep->ppc_fpscr_regnum != -1)
985 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
986 if (ppc_linux_trap_reg_p (gdbarch))
988 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
989 store_register (regcache, tid, PPC_TRAP_REGNUM);
991 if (have_ptrace_getvrregs)
992 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
993 store_altivec_registers (regcache, tid);
994 if (have_ptrace_getsetvsxregs)
995 if (tdep->ppc_vsr0_upper_regnum != -1)
996 store_vsx_registers (regcache, tid);
997 if (tdep->ppc_ev0_upper_regnum >= 0)
998 store_spe_register (regcache, tid, -1);
1002 ppc_linux_check_watch_resources (int type, int cnt, int ot)
1005 ptid_t ptid = inferior_ptid;
1007 /* DABR (data address breakpoint register) is optional for PPC variants.
1008 Some variants have one DABR, others have none. So CNT can't be larger
1013 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
1014 the target has DABR. If either answer is no, the ptrace call will
1015 return -1. Fail in that case. */
1016 tid = TIDGET (ptid);
1018 tid = PIDGET (ptid);
1020 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1025 /* Fetch the AT_HWCAP entry from the aux vector. */
1026 unsigned long ppc_linux_get_hwcap (void)
1030 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
1031 return (unsigned long) field;
1037 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1039 /* Handle sub-8-byte quantities. */
1043 /* addr+len must fall in the 8 byte watchable region for DABR-based
1044 processors. DAC-based processors, like the PowerPC 440, will use
1045 addresses aligned to 4-bytes due to the way the read/write flags are
1046 passed at the moment. */
1047 if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1048 && (addr + len) > (addr & ~3) + 4)
1049 || (addr + len) > (addr & ~7) + 8)
1055 /* The cached DABR value, to install in new threads. */
1056 static long saved_dabr_value;
1058 /* Set a watchpoint of type TYPE at address ADDR. */
1060 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
1062 struct lwp_info *lp;
1065 long read_mode, write_mode;
1067 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1069 /* PowerPC 440 requires only the read/write flags to be passed
1076 /* PowerPC 970 and other DABR-based processors are required to pass
1077 the Breakpoint Translation bit together with the flags. */
1082 dabr_value = addr & ~(read_mode | write_mode);
1086 /* Set read and translate bits. */
1087 dabr_value |= read_mode;
1090 /* Set write and translate bits. */
1091 dabr_value |= write_mode;
1094 /* Set read, write and translate bits. */
1095 dabr_value |= read_mode | write_mode;
1099 saved_dabr_value = dabr_value;
1102 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1109 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
1111 struct lwp_info *lp;
1113 long dabr_value = 0;
1115 saved_dabr_value = 0;
1117 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1123 ppc_linux_new_thread (ptid_t ptid)
1125 ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value);
1129 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1131 struct siginfo *siginfo_p;
1133 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
1135 if (siginfo_p->si_signo != SIGTRAP
1136 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1139 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
1144 ppc_linux_stopped_by_watchpoint (void)
1147 return ppc_linux_stopped_data_address (¤t_target, &addr);
1151 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
1153 CORE_ADDR start, int length)
1157 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1164 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
1165 return start <= addr + mask && start + length - 1 >= addr;
1169 ppc_linux_store_inferior_registers (struct regcache *regcache, int regno)
1171 /* Overload thread id onto process id */
1172 int tid = TIDGET (inferior_ptid);
1174 /* No thread id, just use process id */
1176 tid = PIDGET (inferior_ptid);
1179 store_register (regcache, tid, regno);
1181 store_ppc_registers (regcache, tid);
1184 /* Functions for transferring registers between a gregset_t or fpregset_t
1185 (see sys/ucontext.h) and gdb's regcache. The word size is that used
1186 by the ptrace interface, not the current program's ABI. eg. If a
1187 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
1188 read or write 64-bit gregsets. This is to suit the host libthread_db. */
1191 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1193 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1195 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1199 fill_gregset (const struct regcache *regcache,
1200 gdb_gregset_t *gregsetp, int regno)
1202 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1205 memset (gregsetp, 0, sizeof (*gregsetp));
1206 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1210 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1212 const struct regset *regset = ppc_linux_fpregset ();
1214 ppc_supply_fpregset (regset, regcache, -1,
1215 fpregsetp, sizeof (*fpregsetp));
1219 fill_fpregset (const struct regcache *regcache,
1220 gdb_fpregset_t *fpregsetp, int regno)
1222 const struct regset *regset = ppc_linux_fpregset ();
1224 ppc_collect_fpregset (regset, regcache, regno,
1225 fpregsetp, sizeof (*fpregsetp));
1228 static const struct target_desc *
1229 ppc_linux_read_description (struct target_ops *ops)
1234 int tid = TIDGET (inferior_ptid);
1236 tid = PIDGET (inferior_ptid);
1238 if (have_ptrace_getsetevrregs)
1240 struct gdb_evrregset_t evrregset;
1242 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
1243 return tdesc_powerpc_e500l;
1245 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
1246 Anything else needs to be reported. */
1247 else if (errno != EIO)
1248 perror_with_name (_("Unable to fetch SPE registers"));
1251 if (have_ptrace_getsetvsxregs)
1253 gdb_vsxregset_t vsxregset;
1255 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
1258 /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
1259 Anything else needs to be reported. */
1260 else if (errno != EIO)
1261 perror_with_name (_("Unable to fetch VSX registers"));
1264 if (have_ptrace_getvrregs)
1266 gdb_vrregset_t vrregset;
1268 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
1271 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
1272 Anything else needs to be reported. */
1273 else if (errno != EIO)
1274 perror_with_name (_("Unable to fetch AltiVec registers"));
1277 /* Check for 64-bit inferior process. This is the case when the host is
1278 64-bit, and in addition the top bit of the MSR register is set. */
1279 #ifdef __powerpc64__
1283 msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
1284 if (errno == 0 && msr < 0)
1287 return tdesc_powerpc_vsx64l;
1289 return tdesc_powerpc_altivec64l;
1291 return tdesc_powerpc_64l;
1297 return tdesc_powerpc_vsx32l;
1299 return tdesc_powerpc_altivec32l;
1301 return tdesc_powerpc_32l;
1304 void _initialize_ppc_linux_nat (void);
1307 _initialize_ppc_linux_nat (void)
1309 struct target_ops *t;
1311 /* Fill in the generic GNU/Linux methods. */
1312 t = linux_target ();
1314 /* Add our register access methods. */
1315 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
1316 t->to_store_registers = ppc_linux_store_inferior_registers;
1318 /* Add our watchpoint methods. */
1319 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
1320 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
1321 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
1322 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
1323 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
1324 t->to_stopped_data_address = ppc_linux_stopped_data_address;
1325 t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
1327 t->to_read_description = ppc_linux_read_description;
1329 /* Register the target. */
1330 linux_nat_add_target (t);
1331 linux_nat_set_new_thread (t, ppc_linux_new_thread);