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
80 /* Similarly for the ptrace requests for getting / setting the SPE
81 registers (ev0 -- ev31, acc, and spefscr). See the description of
82 gdb_evrregset_t for details. */
83 #ifndef PTRACE_GETEVRREGS
84 #define PTRACE_GETEVRREGS 20
85 #define PTRACE_SETEVRREGS 21
88 /* Similarly for the hardware watchpoint support. */
89 #ifndef PTRACE_GET_DEBUGREG
90 #define PTRACE_GET_DEBUGREG 25
92 #ifndef PTRACE_SET_DEBUGREG
93 #define PTRACE_SET_DEBUGREG 26
95 #ifndef PTRACE_GETSIGINFO
96 #define PTRACE_GETSIGINFO 0x4202
99 /* This oddity is because the Linux kernel defines elf_vrregset_t as
100 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
101 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
102 the vrsave as an extra 4 bytes at the end. I opted for creating a
103 flat array of chars, so that it is easier to manipulate for gdb.
105 There are 32 vector registers 16 bytes longs, plus a VSCR register
106 which is only 4 bytes long, but is fetched as a 16 bytes
107 quantity. Up to here we have the elf_vrregset_t structure.
108 Appended to this there is space for the VRSAVE register: 4 bytes.
109 Even though this vrsave register is not included in the regset
110 typedef, it is handled by the ptrace requests.
112 Note that GNU/Linux doesn't support little endian PPC hardware,
113 therefore the offset at which the real value of the VSCR register
114 is located will be always 12 bytes.
116 The layout is like this (where x is the actual value of the vscr reg): */
120 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
121 <-------> <-------><-------><->
126 #define SIZEOF_VRREGS 33*16+4
128 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
131 /* On PPC processors that support the the Signal Processing Extension
132 (SPE) APU, the general-purpose registers are 64 bits long.
133 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
134 ptrace calls only access the lower half of each register, to allow
135 them to behave the same way they do on non-SPE systems. There's a
136 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
137 read and write the top halves of all the general-purpose registers
138 at once, along with some SPE-specific registers.
140 GDB itself continues to claim the general-purpose registers are 32
141 bits long. It has unnamed raw registers that hold the upper halves
142 of the gprs, and the the full 64-bit SIMD views of the registers,
143 'ev0' -- 'ev31', are pseudo-registers that splice the top and
144 bottom halves together.
146 This is the structure filled in by PTRACE_GETEVRREGS and written to
147 the inferior's registers by PTRACE_SETEVRREGS. */
148 struct gdb_evrregset_t
150 unsigned long evr[32];
151 unsigned long long acc;
152 unsigned long spefscr;
156 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
157 PTRACE_SETVRREGS requests, for reading and writing the Altivec
158 registers. Zero if we've tried one of them and gotten an
160 int have_ptrace_getvrregs = 1;
162 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
163 PTRACE_SETEVRREGS requests, for reading and writing the SPE
164 registers. Zero if we've tried one of them and gotten an
166 int have_ptrace_getsetevrregs = 1;
169 /* registers layout, as presented by the ptrace interface:
170 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
171 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
172 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
173 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
174 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
175 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
176 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
177 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
178 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
182 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
185 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
186 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
187 interface, and not the wordsize of the program's ABI. */
188 int wordsize = sizeof (long);
190 /* General purpose registers occupy 1 slot each in the buffer */
191 if (regno >= tdep->ppc_gp0_regnum
192 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
193 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
195 /* Floating point regs: eight bytes each in both 32- and 64-bit
196 ptrace interfaces. Thus, two slots each in 32-bit interface, one
197 slot each in 64-bit interface. */
198 if (tdep->ppc_fp0_regnum >= 0
199 && regno >= tdep->ppc_fp0_regnum
200 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
201 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
203 /* UISA special purpose registers: 1 slot each */
204 if (regno == gdbarch_pc_regnum (gdbarch))
205 u_addr = PT_NIP * wordsize;
206 if (regno == tdep->ppc_lr_regnum)
207 u_addr = PT_LNK * wordsize;
208 if (regno == tdep->ppc_cr_regnum)
209 u_addr = PT_CCR * wordsize;
210 if (regno == tdep->ppc_xer_regnum)
211 u_addr = PT_XER * wordsize;
212 if (regno == tdep->ppc_ctr_regnum)
213 u_addr = PT_CTR * wordsize;
215 if (regno == tdep->ppc_mq_regnum)
216 u_addr = PT_MQ * wordsize;
218 if (regno == tdep->ppc_ps_regnum)
219 u_addr = PT_MSR * wordsize;
220 if (regno == PPC_ORIG_R3_REGNUM)
221 u_addr = PT_ORIG_R3 * wordsize;
222 if (regno == PPC_TRAP_REGNUM)
223 u_addr = PT_TRAP * wordsize;
224 if (tdep->ppc_fpscr_regnum >= 0
225 && regno == tdep->ppc_fpscr_regnum)
227 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
228 kernel headers incorrectly contained the 32-bit definition of
229 PT_FPSCR. For the 32-bit definition, floating-point
230 registers occupy two 32-bit "slots", and the FPSCR lives in
231 the secondhalf of such a slot-pair (hence +1). For 64-bit,
232 the FPSCR instead occupies the full 64-bit 2-word-slot and
233 hence no adjustment is necessary. Hack around this. */
234 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
235 u_addr = (48 + 32) * wordsize;
237 u_addr = PT_FPSCR * wordsize;
242 /* The Linux kernel ptrace interface for AltiVec registers uses the
243 registers set mechanism, as opposed to the interface for all the
244 other registers, that stores/fetches each register individually. */
246 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
251 struct gdbarch *gdbarch = get_regcache_arch (regcache);
252 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
253 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
255 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
260 have_ptrace_getvrregs = 0;
263 perror_with_name (_("Unable to fetch AltiVec register"));
266 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
267 long on the hardware. We deal only with the lower 4 bytes of the
268 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
269 there is no need to define an offset for it. */
270 if (regno == (tdep->ppc_vrsave_regnum - 1))
271 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
273 regcache_raw_supply (regcache, regno,
274 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
277 /* Fetch the top 32 bits of TID's general-purpose registers and the
278 SPE-specific registers, and place the results in EVRREGSET. If we
279 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
282 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
283 PTRACE_SETEVRREGS requests are supported is isolated here, and in
284 set_spe_registers. */
286 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
288 if (have_ptrace_getsetevrregs)
290 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
294 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
295 we just return zeros. */
297 have_ptrace_getsetevrregs = 0;
299 /* Anything else needs to be reported. */
300 perror_with_name (_("Unable to fetch SPE registers"));
304 memset (evrregset, 0, sizeof (*evrregset));
307 /* Supply values from TID for SPE-specific raw registers: the upper
308 halves of the GPRs, the accumulator, and the spefscr. REGNO must
309 be the number of an upper half register, acc, spefscr, or -1 to
310 supply the values of all registers. */
312 fetch_spe_register (struct regcache *regcache, int tid, int regno)
314 struct gdbarch *gdbarch = get_regcache_arch (regcache);
315 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
316 struct gdb_evrregset_t evrregs;
318 gdb_assert (sizeof (evrregs.evr[0])
319 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
320 gdb_assert (sizeof (evrregs.acc)
321 == register_size (gdbarch, tdep->ppc_acc_regnum));
322 gdb_assert (sizeof (evrregs.spefscr)
323 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
325 get_spe_registers (tid, &evrregs);
331 for (i = 0; i < ppc_num_gprs; i++)
332 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
335 else if (tdep->ppc_ev0_upper_regnum <= regno
336 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
337 regcache_raw_supply (regcache, regno,
338 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
341 || regno == tdep->ppc_acc_regnum)
342 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
345 || regno == tdep->ppc_spefscr_regnum)
346 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
351 fetch_register (struct regcache *regcache, int tid, int regno)
353 struct gdbarch *gdbarch = get_regcache_arch (regcache);
354 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
355 /* This isn't really an address. But ptrace thinks of it as one. */
356 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
357 int bytes_transferred;
358 unsigned int offset; /* Offset of registers within the u area. */
359 char buf[MAX_REGISTER_SIZE];
361 if (altivec_register_p (gdbarch, regno))
363 /* If this is the first time through, or if it is not the first
364 time through, and we have comfirmed that there is kernel
365 support for such a ptrace request, then go and fetch the
367 if (have_ptrace_getvrregs)
369 fetch_altivec_register (regcache, tid, regno);
372 /* If we have discovered that there is no ptrace support for
373 AltiVec registers, fall through and return zeroes, because
374 regaddr will be -1 in this case. */
376 else if (spe_register_p (gdbarch, regno))
378 fetch_spe_register (regcache, tid, regno);
384 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
385 regcache_raw_supply (regcache, regno, buf);
389 /* Read the raw register using sizeof(long) sized chunks. On a
390 32-bit platform, 64-bit floating-point registers will require two
392 for (bytes_transferred = 0;
393 bytes_transferred < register_size (gdbarch, regno);
394 bytes_transferred += sizeof (long))
397 *(long *) &buf[bytes_transferred]
398 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
399 regaddr += sizeof (long);
403 sprintf (message, "reading register %s (#%d)",
404 gdbarch_register_name (gdbarch, regno), regno);
405 perror_with_name (message);
409 /* Now supply the register. Keep in mind that the regcache's idea
410 of the register's size may not be a multiple of sizeof
412 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
414 /* Little-endian values are always found at the left end of the
415 bytes transferred. */
416 regcache_raw_supply (regcache, regno, buf);
418 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
420 /* Big-endian values are found at the right end of the bytes
422 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
423 regcache_raw_supply (regcache, regno, buf + padding);
426 internal_error (__FILE__, __LINE__,
427 _("fetch_register: unexpected byte order: %d"),
428 gdbarch_byte_order (gdbarch));
432 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
435 struct gdbarch *gdbarch = get_regcache_arch (regcache);
436 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
437 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
438 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
439 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
441 for (i = 0; i < num_of_vrregs; i++)
443 /* The last 2 registers of this set are only 32 bit long, not
444 128. However an offset is necessary only for VSCR because it
445 occupies a whole vector, while VRSAVE occupies a full 4 bytes
447 if (i == (num_of_vrregs - 2))
448 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
449 *vrregsetp + i * vrregsize + offset);
451 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
452 *vrregsetp + i * vrregsize);
457 fetch_altivec_registers (struct regcache *regcache, int tid)
462 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
467 have_ptrace_getvrregs = 0;
470 perror_with_name (_("Unable to fetch AltiVec registers"));
472 supply_vrregset (regcache, ®s);
476 fetch_ppc_registers (struct regcache *regcache, int tid)
479 struct gdbarch *gdbarch = get_regcache_arch (regcache);
480 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
482 for (i = 0; i < ppc_num_gprs; i++)
483 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
484 if (tdep->ppc_fp0_regnum >= 0)
485 for (i = 0; i < ppc_num_fprs; i++)
486 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
487 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
488 if (tdep->ppc_ps_regnum != -1)
489 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
490 if (tdep->ppc_cr_regnum != -1)
491 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
492 if (tdep->ppc_lr_regnum != -1)
493 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
494 if (tdep->ppc_ctr_regnum != -1)
495 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
496 if (tdep->ppc_xer_regnum != -1)
497 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
498 if (tdep->ppc_mq_regnum != -1)
499 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
500 if (ppc_linux_trap_reg_p (gdbarch))
502 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
503 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
505 if (tdep->ppc_fpscr_regnum != -1)
506 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
507 if (have_ptrace_getvrregs)
508 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
509 fetch_altivec_registers (regcache, tid);
510 if (tdep->ppc_ev0_upper_regnum >= 0)
511 fetch_spe_register (regcache, tid, -1);
514 /* Fetch registers from the child process. Fetch all registers if
515 regno == -1, otherwise fetch all general registers or all floating
516 point registers depending upon the value of regno. */
518 ppc_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
520 /* Overload thread id onto process id */
521 int tid = TIDGET (inferior_ptid);
523 /* No thread id, just use process id */
525 tid = PIDGET (inferior_ptid);
528 fetch_ppc_registers (regcache, tid);
530 fetch_register (regcache, tid, regno);
533 /* Store one register. */
535 store_altivec_register (const struct regcache *regcache, int tid, int regno)
540 struct gdbarch *gdbarch = get_regcache_arch (regcache);
541 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
542 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
544 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
549 have_ptrace_getvrregs = 0;
552 perror_with_name (_("Unable to fetch AltiVec register"));
555 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
556 long on the hardware. */
557 if (regno == (tdep->ppc_vrsave_regnum - 1))
558 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
560 regcache_raw_collect (regcache, regno,
561 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
563 ret = ptrace (PTRACE_SETVRREGS, tid, 0, ®s);
565 perror_with_name (_("Unable to store AltiVec register"));
568 /* Assuming TID referrs to an SPE process, set the top halves of TID's
569 general-purpose registers and its SPE-specific registers to the
570 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
573 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
574 PTRACE_SETEVRREGS requests are supported is isolated here, and in
575 get_spe_registers. */
577 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
579 if (have_ptrace_getsetevrregs)
581 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
585 /* EIO means that the PTRACE_SETEVRREGS request isn't
586 supported; we fail silently, and don't try the call
589 have_ptrace_getsetevrregs = 0;
591 /* Anything else needs to be reported. */
592 perror_with_name (_("Unable to set SPE registers"));
597 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
598 If REGNO is -1, write the values of all the SPE-specific
601 store_spe_register (const struct regcache *regcache, int tid, int regno)
603 struct gdbarch *gdbarch = get_regcache_arch (regcache);
604 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
605 struct gdb_evrregset_t evrregs;
607 gdb_assert (sizeof (evrregs.evr[0])
608 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
609 gdb_assert (sizeof (evrregs.acc)
610 == register_size (gdbarch, tdep->ppc_acc_regnum));
611 gdb_assert (sizeof (evrregs.spefscr)
612 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
615 /* Since we're going to write out every register, the code below
616 should store to every field of evrregs; if that doesn't happen,
617 make it obvious by initializing it with suspicious values. */
618 memset (&evrregs, 42, sizeof (evrregs));
620 /* We can only read and write the entire EVR register set at a
621 time, so to write just a single register, we do a
622 read-modify-write maneuver. */
623 get_spe_registers (tid, &evrregs);
629 for (i = 0; i < ppc_num_gprs; i++)
630 regcache_raw_collect (regcache,
631 tdep->ppc_ev0_upper_regnum + i,
634 else if (tdep->ppc_ev0_upper_regnum <= regno
635 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
636 regcache_raw_collect (regcache, regno,
637 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
640 || regno == tdep->ppc_acc_regnum)
641 regcache_raw_collect (regcache,
642 tdep->ppc_acc_regnum,
646 || regno == tdep->ppc_spefscr_regnum)
647 regcache_raw_collect (regcache,
648 tdep->ppc_spefscr_regnum,
651 /* Write back the modified register set. */
652 set_spe_registers (tid, &evrregs);
656 store_register (const struct regcache *regcache, int tid, int regno)
658 struct gdbarch *gdbarch = get_regcache_arch (regcache);
659 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
660 /* This isn't really an address. But ptrace thinks of it as one. */
661 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
663 size_t bytes_to_transfer;
664 char buf[MAX_REGISTER_SIZE];
666 if (altivec_register_p (gdbarch, regno))
668 store_altivec_register (regcache, tid, regno);
671 else if (spe_register_p (gdbarch, regno))
673 store_spe_register (regcache, tid, regno);
680 /* First collect the register. Keep in mind that the regcache's
681 idea of the register's size may not be a multiple of sizeof
683 memset (buf, 0, sizeof buf);
684 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
685 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
687 /* Little-endian values always sit at the left end of the buffer. */
688 regcache_raw_collect (regcache, regno, buf);
690 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
692 /* Big-endian values sit at the right end of the buffer. */
693 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
694 regcache_raw_collect (regcache, regno, buf + padding);
697 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
700 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
702 regaddr += sizeof (long);
705 && (regno == tdep->ppc_fpscr_regnum
706 || regno == PPC_ORIG_R3_REGNUM
707 || regno == PPC_TRAP_REGNUM))
709 /* Some older kernel versions don't allow fpscr, orig_r3
710 or trap to be written. */
717 sprintf (message, "writing register %s (#%d)",
718 gdbarch_register_name (gdbarch, regno), regno);
719 perror_with_name (message);
725 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
728 struct gdbarch *gdbarch = get_regcache_arch (regcache);
729 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
730 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
731 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
732 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
734 for (i = 0; i < num_of_vrregs; i++)
736 /* The last 2 registers of this set are only 32 bit long, not
737 128, but only VSCR is fetched as a 16 bytes quantity. */
738 if (i == (num_of_vrregs - 2))
739 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
740 *vrregsetp + i * vrregsize + offset);
742 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
743 *vrregsetp + i * vrregsize);
748 store_altivec_registers (const struct regcache *regcache, int tid)
753 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
758 have_ptrace_getvrregs = 0;
761 perror_with_name (_("Couldn't get AltiVec registers"));
764 fill_vrregset (regcache, ®s);
766 if (ptrace (PTRACE_SETVRREGS, tid, 0, ®s) < 0)
767 perror_with_name (_("Couldn't write AltiVec registers"));
771 store_ppc_registers (const struct regcache *regcache, int tid)
774 struct gdbarch *gdbarch = get_regcache_arch (regcache);
775 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
777 for (i = 0; i < ppc_num_gprs; i++)
778 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
779 if (tdep->ppc_fp0_regnum >= 0)
780 for (i = 0; i < ppc_num_fprs; i++)
781 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
782 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
783 if (tdep->ppc_ps_regnum != -1)
784 store_register (regcache, tid, tdep->ppc_ps_regnum);
785 if (tdep->ppc_cr_regnum != -1)
786 store_register (regcache, tid, tdep->ppc_cr_regnum);
787 if (tdep->ppc_lr_regnum != -1)
788 store_register (regcache, tid, tdep->ppc_lr_regnum);
789 if (tdep->ppc_ctr_regnum != -1)
790 store_register (regcache, tid, tdep->ppc_ctr_regnum);
791 if (tdep->ppc_xer_regnum != -1)
792 store_register (regcache, tid, tdep->ppc_xer_regnum);
793 if (tdep->ppc_mq_regnum != -1)
794 store_register (regcache, tid, tdep->ppc_mq_regnum);
795 if (tdep->ppc_fpscr_regnum != -1)
796 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
797 if (ppc_linux_trap_reg_p (gdbarch))
799 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
800 store_register (regcache, tid, PPC_TRAP_REGNUM);
802 if (have_ptrace_getvrregs)
803 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
804 store_altivec_registers (regcache, tid);
805 if (tdep->ppc_ev0_upper_regnum >= 0)
806 store_spe_register (regcache, tid, -1);
810 ppc_linux_check_watch_resources (int type, int cnt, int ot)
813 ptid_t ptid = inferior_ptid;
815 /* DABR (data address breakpoint register) is optional for PPC variants.
816 Some variants have one DABR, others have none. So CNT can't be larger
821 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
822 the target has DABR. If either answer is no, the ptrace call will
823 return -1. Fail in that case. */
828 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
833 /* Fetch the AT_HWCAP entry from the aux vector. */
834 unsigned long ppc_linux_get_hwcap (void)
838 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
839 return (unsigned long) field;
845 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
847 /* Handle sub-8-byte quantities. */
851 /* addr+len must fall in the 8 byte watchable region for DABR-based
852 processors. DAC-based processors, like the PowerPC 440, will use
853 addresses aligned to 4-bytes due to the way the read/write flags are
854 passed at the moment. */
855 if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
856 && (addr + len) > (addr & ~3) + 4)
857 || (addr + len) > (addr & ~7) + 8)
863 /* The cached DABR value, to install in new threads. */
864 static long saved_dabr_value;
866 /* Set a watchpoint of type TYPE at address ADDR. */
868 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
873 long read_mode, write_mode;
875 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
877 /* PowerPC 440 requires only the read/write flags to be passed
884 /* PowerPC 970 and other DABR-based processors are required to pass
885 the Breakpoint Translation bit together with the flags. */
890 dabr_value = addr & ~(read_mode | write_mode);
894 /* Set read and translate bits. */
895 dabr_value |= read_mode;
898 /* Set write and translate bits. */
899 dabr_value |= write_mode;
902 /* Set read, write and translate bits. */
903 dabr_value |= read_mode | write_mode;
907 saved_dabr_value = dabr_value;
910 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
917 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
923 saved_dabr_value = 0;
925 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
931 ppc_linux_new_thread (ptid_t ptid)
933 ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value);
937 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
939 struct siginfo *siginfo_p;
941 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
943 if (siginfo_p->si_signo != SIGTRAP
944 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
947 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
952 ppc_linux_stopped_by_watchpoint (void)
955 return ppc_linux_stopped_data_address (¤t_target, &addr);
959 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
961 CORE_ADDR start, int length)
965 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
972 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
973 return start <= addr + mask && start + length - 1 >= addr;
977 ppc_linux_store_inferior_registers (struct regcache *regcache, int regno)
979 /* Overload thread id onto process id */
980 int tid = TIDGET (inferior_ptid);
982 /* No thread id, just use process id */
984 tid = PIDGET (inferior_ptid);
987 store_register (regcache, tid, regno);
989 store_ppc_registers (regcache, tid);
992 /* Functions for transferring registers between a gregset_t or fpregset_t
993 (see sys/ucontext.h) and gdb's regcache. The word size is that used
994 by the ptrace interface, not the current program's ABI. eg. If a
995 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
996 read or write 64-bit gregsets. This is to suit the host libthread_db. */
999 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1001 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1003 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1007 fill_gregset (const struct regcache *regcache,
1008 gdb_gregset_t *gregsetp, int regno)
1010 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1013 memset (gregsetp, 0, sizeof (*gregsetp));
1014 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1018 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1020 const struct regset *regset = ppc_linux_fpregset ();
1022 ppc_supply_fpregset (regset, regcache, -1,
1023 fpregsetp, sizeof (*fpregsetp));
1027 fill_fpregset (const struct regcache *regcache,
1028 gdb_fpregset_t *fpregsetp, int regno)
1030 const struct regset *regset = ppc_linux_fpregset ();
1032 ppc_collect_fpregset (regset, regcache, regno,
1033 fpregsetp, sizeof (*fpregsetp));
1036 static const struct target_desc *
1037 ppc_linux_read_description (struct target_ops *ops)
1041 int tid = TIDGET (inferior_ptid);
1043 tid = PIDGET (inferior_ptid);
1045 if (have_ptrace_getsetevrregs)
1047 struct gdb_evrregset_t evrregset;
1049 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
1050 return tdesc_powerpc_e500l;
1052 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
1053 Anything else needs to be reported. */
1054 else if (errno != EIO)
1055 perror_with_name (_("Unable to fetch SPE registers"));
1058 if (have_ptrace_getvrregs)
1060 gdb_vrregset_t vrregset;
1062 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
1065 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
1066 Anything else needs to be reported. */
1067 else if (errno != EIO)
1068 perror_with_name (_("Unable to fetch AltiVec registers"));
1071 /* Check for 64-bit inferior process. This is the case when the host is
1072 64-bit, and in addition the top bit of the MSR register is set. */
1073 #ifdef __powerpc64__
1077 msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
1078 if (errno == 0 && msr < 0)
1079 return altivec? tdesc_powerpc_altivec64l : tdesc_powerpc_64l;
1083 return altivec? tdesc_powerpc_altivec32l : tdesc_powerpc_32l;
1086 void _initialize_ppc_linux_nat (void);
1089 _initialize_ppc_linux_nat (void)
1091 struct target_ops *t;
1093 /* Fill in the generic GNU/Linux methods. */
1094 t = linux_target ();
1096 /* Add our register access methods. */
1097 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
1098 t->to_store_registers = ppc_linux_store_inferior_registers;
1100 /* Add our watchpoint methods. */
1101 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
1102 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
1103 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
1104 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
1105 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
1106 t->to_stopped_data_address = ppc_linux_stopped_data_address;
1107 t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
1109 t->to_read_description = ppc_linux_read_description;
1111 /* Register the target. */
1112 linux_nat_add_target (t);
1113 linux_nat_set_new_thread (t, ppc_linux_new_thread);