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, 2009, 2010 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"
26 #include "gdbthread.h"
29 #include "gdb_assert.h"
31 #include "linux-nat.h"
34 #include <sys/types.h>
35 #include <sys/param.h>
38 #include <sys/ioctl.h>
41 #include <sys/procfs.h>
42 #include <sys/ptrace.h>
44 /* Prototypes for supply_gregset etc. */
47 #include "ppc-linux-tdep.h"
49 /* Required when using the AUXV. */
50 #include "elf/common.h"
53 /* This sometimes isn't defined. */
61 /* The PPC_FEATURE_* defines should be provided by <asm/cputable.h>.
62 If they aren't, we can provide them ourselves (their values are fixed
63 because they are part of the kernel ABI). They are used in the AT_HWCAP
65 #ifndef PPC_FEATURE_CELL
66 #define PPC_FEATURE_CELL 0x00010000
68 #ifndef PPC_FEATURE_BOOKE
69 #define PPC_FEATURE_BOOKE 0x00008000
71 #ifndef PPC_FEATURE_HAS_DFP
72 #define PPC_FEATURE_HAS_DFP 0x00000400 /* Decimal Floating Point. */
75 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
76 configure time check. Some older glibc's (for instance 2.2.1)
77 don't have a specific powerpc version of ptrace.h, and fall back on
78 a generic one. In such cases, sys/ptrace.h defines
79 PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
80 ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
81 PTRACE_SETVRREGS to be. This also makes a configury check pretty
84 /* These definitions should really come from the glibc header files,
85 but Glibc doesn't know about the vrregs yet. */
86 #ifndef PTRACE_GETVRREGS
87 #define PTRACE_GETVRREGS 18
88 #define PTRACE_SETVRREGS 19
91 /* PTRACE requests for POWER7 VSX registers. */
92 #ifndef PTRACE_GETVSXREGS
93 #define PTRACE_GETVSXREGS 27
94 #define PTRACE_SETVSXREGS 28
97 /* Similarly for the ptrace requests for getting / setting the SPE
98 registers (ev0 -- ev31, acc, and spefscr). See the description of
99 gdb_evrregset_t for details. */
100 #ifndef PTRACE_GETEVRREGS
101 #define PTRACE_GETEVRREGS 20
102 #define PTRACE_SETEVRREGS 21
105 /* Similarly for the hardware watchpoint support. These requests are used
106 when the BookE kernel interface is not available. */
107 #ifndef PTRACE_GET_DEBUGREG
108 #define PTRACE_GET_DEBUGREG 25
110 #ifndef PTRACE_SET_DEBUGREG
111 #define PTRACE_SET_DEBUGREG 26
113 #ifndef PTRACE_GETSIGINFO
114 #define PTRACE_GETSIGINFO 0x4202
117 /* These requests are used when the BookE kernel interface is available.
118 It exposes the additional debug features of BookE processors, such as
119 ranged breakpoints and watchpoints and hardware-accelerated condition
121 #ifndef PPC_PTRACE_GETHWDBGINFO
123 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the new BookE
124 interface is not present in ptrace.h, so we'll have to pretty much include
125 it all here so that the code at least compiles on older systems. */
126 #define PPC_PTRACE_GETHWDBGINFO 0x89
127 #define PPC_PTRACE_SETHWDEBUG 0x88
128 #define PPC_PTRACE_DELHWDEBUG 0x87
130 struct ppc_debug_info
132 uint32_t version; /* Only version 1 exists to date */
133 uint32_t num_instruction_bps;
134 uint32_t num_data_bps;
135 uint32_t num_condition_regs;
136 uint32_t data_bp_alignment;
137 uint32_t sizeof_condition; /* size of the DVC register */
141 /* Features will have bits indicating whether there is support for: */
142 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE 0x1
143 #define PPC_DEBUG_FEATURE_INSN_BP_MASK 0x2
144 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE 0x4
145 #define PPC_DEBUG_FEATURE_DATA_BP_MASK 0x8
147 struct ppc_hw_breakpoint
149 uint32_t version; /* currently, version must be 1 */
150 uint32_t trigger_type; /* only some combinations allowed */
151 uint32_t addr_mode; /* address match mode */
152 uint32_t condition_mode; /* break/watchpoint condition flags */
153 uint64_t addr; /* break/watchpoint address */
154 uint64_t addr2; /* range end or mask */
155 uint64_t condition_value; /* contents of the DVC register */
159 #define PPC_BREAKPOINT_TRIGGER_EXECUTE 0x1
160 #define PPC_BREAKPOINT_TRIGGER_READ 0x2
161 #define PPC_BREAKPOINT_TRIGGER_WRITE 0x4
162 #define PPC_BREAKPOINT_TRIGGER_RW 0x6
165 #define PPC_BREAKPOINT_MODE_EXACT 0x0
166 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE 0x1
167 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE 0x2
168 #define PPC_BREAKPOINT_MODE_MASK 0x3
170 /* Condition mode. */
171 #define PPC_BREAKPOINT_CONDITION_NONE 0x0
172 #define PPC_BREAKPOINT_CONDITION_AND 0x1
173 #define PPC_BREAKPOINT_CONDITION_EXACT 0x1
174 #define PPC_BREAKPOINT_CONDITION_OR 0x2
175 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
176 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
177 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT 16
178 #define PPC_BREAKPOINT_CONDITION_BE(n) \
179 (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
180 #endif /* PPC_PTRACE_GETHWDBGINFO */
184 /* Similarly for the general-purpose (gp0 -- gp31)
185 and floating-point registers (fp0 -- fp31). */
186 #ifndef PTRACE_GETREGS
187 #define PTRACE_GETREGS 12
189 #ifndef PTRACE_SETREGS
190 #define PTRACE_SETREGS 13
192 #ifndef PTRACE_GETFPREGS
193 #define PTRACE_GETFPREGS 14
195 #ifndef PTRACE_SETFPREGS
196 #define PTRACE_SETFPREGS 15
199 /* This oddity is because the Linux kernel defines elf_vrregset_t as
200 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
201 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
202 the vrsave as an extra 4 bytes at the end. I opted for creating a
203 flat array of chars, so that it is easier to manipulate for gdb.
205 There are 32 vector registers 16 bytes longs, plus a VSCR register
206 which is only 4 bytes long, but is fetched as a 16 bytes
207 quantity. Up to here we have the elf_vrregset_t structure.
208 Appended to this there is space for the VRSAVE register: 4 bytes.
209 Even though this vrsave register is not included in the regset
210 typedef, it is handled by the ptrace requests.
212 Note that GNU/Linux doesn't support little endian PPC hardware,
213 therefore the offset at which the real value of the VSCR register
214 is located will be always 12 bytes.
216 The layout is like this (where x is the actual value of the vscr reg): */
220 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
221 <-------> <-------><-------><->
226 #define SIZEOF_VRREGS 33*16+4
228 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
230 /* This is the layout of the POWER7 VSX registers and the way they overlap
231 with the existing FPR and VMX registers.
233 VSR doubleword 0 VSR doubleword 1
234 ----------------------------------------------------------------
236 ----------------------------------------------------------------
238 ----------------------------------------------------------------
241 ----------------------------------------------------------------
242 VSR[30] | FPR[30] | |
243 ----------------------------------------------------------------
244 VSR[31] | FPR[31] | |
245 ----------------------------------------------------------------
247 ----------------------------------------------------------------
249 ----------------------------------------------------------------
252 ----------------------------------------------------------------
254 ----------------------------------------------------------------
256 ----------------------------------------------------------------
258 VSX has 64 128bit registers. The first 32 registers overlap with
259 the FP registers (doubleword 0) and hence extend them with additional
260 64 bits (doubleword 1). The other 32 regs overlap with the VMX
262 #define SIZEOF_VSXREGS 32*8
264 typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
266 /* On PPC processors that support the the Signal Processing Extension
267 (SPE) APU, the general-purpose registers are 64 bits long.
268 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
269 ptrace calls only access the lower half of each register, to allow
270 them to behave the same way they do on non-SPE systems. There's a
271 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
272 read and write the top halves of all the general-purpose registers
273 at once, along with some SPE-specific registers.
275 GDB itself continues to claim the general-purpose registers are 32
276 bits long. It has unnamed raw registers that hold the upper halves
277 of the gprs, and the the full 64-bit SIMD views of the registers,
278 'ev0' -- 'ev31', are pseudo-registers that splice the top and
279 bottom halves together.
281 This is the structure filled in by PTRACE_GETEVRREGS and written to
282 the inferior's registers by PTRACE_SETEVRREGS. */
283 struct gdb_evrregset_t
285 unsigned long evr[32];
286 unsigned long long acc;
287 unsigned long spefscr;
290 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
291 PTRACE_SETVSXREGS requests, for reading and writing the VSX
292 POWER7 registers 0 through 31. Zero if we've tried one of them and
293 gotten an error. Note that VSX registers 32 through 63 overlap
294 with VR registers 0 through 31. */
295 int have_ptrace_getsetvsxregs = 1;
297 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
298 PTRACE_SETVRREGS requests, for reading and writing the Altivec
299 registers. Zero if we've tried one of them and gotten an
301 int have_ptrace_getvrregs = 1;
303 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
304 PTRACE_SETEVRREGS requests, for reading and writing the SPE
305 registers. Zero if we've tried one of them and gotten an
307 int have_ptrace_getsetevrregs = 1;
309 /* Non-zero if our kernel may support the PTRACE_GETREGS and
310 PTRACE_SETREGS requests, for reading and writing the
311 general-purpose registers. Zero if we've tried one of
312 them and gotten an error. */
313 int have_ptrace_getsetregs = 1;
315 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
316 PTRACE_SETFPREGS requests, for reading and writing the
317 floating-pointers registers. Zero if we've tried one of
318 them and gotten an error. */
319 int have_ptrace_getsetfpregs = 1;
322 /* registers layout, as presented by the ptrace interface:
323 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
324 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
325 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
326 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
327 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
328 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
329 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
330 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
331 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
335 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
338 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
339 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
340 interface, and not the wordsize of the program's ABI. */
341 int wordsize = sizeof (long);
343 /* General purpose registers occupy 1 slot each in the buffer */
344 if (regno >= tdep->ppc_gp0_regnum
345 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
346 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
348 /* Floating point regs: eight bytes each in both 32- and 64-bit
349 ptrace interfaces. Thus, two slots each in 32-bit interface, one
350 slot each in 64-bit interface. */
351 if (tdep->ppc_fp0_regnum >= 0
352 && regno >= tdep->ppc_fp0_regnum
353 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
354 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
356 /* UISA special purpose registers: 1 slot each */
357 if (regno == gdbarch_pc_regnum (gdbarch))
358 u_addr = PT_NIP * wordsize;
359 if (regno == tdep->ppc_lr_regnum)
360 u_addr = PT_LNK * wordsize;
361 if (regno == tdep->ppc_cr_regnum)
362 u_addr = PT_CCR * wordsize;
363 if (regno == tdep->ppc_xer_regnum)
364 u_addr = PT_XER * wordsize;
365 if (regno == tdep->ppc_ctr_regnum)
366 u_addr = PT_CTR * wordsize;
368 if (regno == tdep->ppc_mq_regnum)
369 u_addr = PT_MQ * wordsize;
371 if (regno == tdep->ppc_ps_regnum)
372 u_addr = PT_MSR * wordsize;
373 if (regno == PPC_ORIG_R3_REGNUM)
374 u_addr = PT_ORIG_R3 * wordsize;
375 if (regno == PPC_TRAP_REGNUM)
376 u_addr = PT_TRAP * wordsize;
377 if (tdep->ppc_fpscr_regnum >= 0
378 && regno == tdep->ppc_fpscr_regnum)
380 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
381 kernel headers incorrectly contained the 32-bit definition of
382 PT_FPSCR. For the 32-bit definition, floating-point
383 registers occupy two 32-bit "slots", and the FPSCR lives in
384 the second half of such a slot-pair (hence +1). For 64-bit,
385 the FPSCR instead occupies the full 64-bit 2-word-slot and
386 hence no adjustment is necessary. Hack around this. */
387 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
388 u_addr = (48 + 32) * wordsize;
389 /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
390 slot and not just its second word. The PT_FPSCR supplied when
391 GDB is compiled as a 32-bit app doesn't reflect this. */
392 else if (wordsize == 4 && register_size (gdbarch, regno) == 8
393 && PT_FPSCR == (48 + 2*32 + 1))
394 u_addr = (48 + 2*32) * wordsize;
396 u_addr = PT_FPSCR * wordsize;
401 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
402 registers set mechanism, as opposed to the interface for all the
403 other registers, that stores/fetches each register individually. */
405 fetch_vsx_register (struct regcache *regcache, int tid, int regno)
408 gdb_vsxregset_t regs;
409 struct gdbarch *gdbarch = get_regcache_arch (regcache);
410 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
411 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
413 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
418 have_ptrace_getsetvsxregs = 0;
421 perror_with_name (_("Unable to fetch VSX register"));
424 regcache_raw_supply (regcache, regno,
425 regs + (regno - tdep->ppc_vsr0_upper_regnum)
429 /* The Linux kernel ptrace interface for AltiVec registers uses the
430 registers set mechanism, as opposed to the interface for all the
431 other registers, that stores/fetches each register individually. */
433 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
438 struct gdbarch *gdbarch = get_regcache_arch (regcache);
439 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
440 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
442 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
447 have_ptrace_getvrregs = 0;
450 perror_with_name (_("Unable to fetch AltiVec register"));
453 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
454 long on the hardware. We deal only with the lower 4 bytes of the
455 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
456 there is no need to define an offset for it. */
457 if (regno == (tdep->ppc_vrsave_regnum - 1))
458 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
460 regcache_raw_supply (regcache, regno,
461 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
464 /* Fetch the top 32 bits of TID's general-purpose registers and the
465 SPE-specific registers, and place the results in EVRREGSET. If we
466 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
469 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
470 PTRACE_SETEVRREGS requests are supported is isolated here, and in
471 set_spe_registers. */
473 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
475 if (have_ptrace_getsetevrregs)
477 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
481 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
482 we just return zeros. */
484 have_ptrace_getsetevrregs = 0;
486 /* Anything else needs to be reported. */
487 perror_with_name (_("Unable to fetch SPE registers"));
491 memset (evrregset, 0, sizeof (*evrregset));
494 /* Supply values from TID for SPE-specific raw registers: the upper
495 halves of the GPRs, the accumulator, and the spefscr. REGNO must
496 be the number of an upper half register, acc, spefscr, or -1 to
497 supply the values of all registers. */
499 fetch_spe_register (struct regcache *regcache, int tid, int regno)
501 struct gdbarch *gdbarch = get_regcache_arch (regcache);
502 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
503 struct gdb_evrregset_t evrregs;
505 gdb_assert (sizeof (evrregs.evr[0])
506 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
507 gdb_assert (sizeof (evrregs.acc)
508 == register_size (gdbarch, tdep->ppc_acc_regnum));
509 gdb_assert (sizeof (evrregs.spefscr)
510 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
512 get_spe_registers (tid, &evrregs);
518 for (i = 0; i < ppc_num_gprs; i++)
519 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
522 else if (tdep->ppc_ev0_upper_regnum <= regno
523 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
524 regcache_raw_supply (regcache, regno,
525 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
528 || regno == tdep->ppc_acc_regnum)
529 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
532 || regno == tdep->ppc_spefscr_regnum)
533 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
538 fetch_register (struct regcache *regcache, int tid, int regno)
540 struct gdbarch *gdbarch = get_regcache_arch (regcache);
541 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
542 /* This isn't really an address. But ptrace thinks of it as one. */
543 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
544 int bytes_transferred;
545 unsigned int offset; /* Offset of registers within the u area. */
546 char buf[MAX_REGISTER_SIZE];
548 if (altivec_register_p (gdbarch, regno))
550 /* If this is the first time through, or if it is not the first
551 time through, and we have comfirmed that there is kernel
552 support for such a ptrace request, then go and fetch the
554 if (have_ptrace_getvrregs)
556 fetch_altivec_register (regcache, tid, regno);
559 /* If we have discovered that there is no ptrace support for
560 AltiVec registers, fall through and return zeroes, because
561 regaddr will be -1 in this case. */
563 if (vsx_register_p (gdbarch, regno))
565 if (have_ptrace_getsetvsxregs)
567 fetch_vsx_register (regcache, tid, regno);
571 else if (spe_register_p (gdbarch, regno))
573 fetch_spe_register (regcache, tid, regno);
579 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
580 regcache_raw_supply (regcache, regno, buf);
584 /* Read the raw register using sizeof(long) sized chunks. On a
585 32-bit platform, 64-bit floating-point registers will require two
587 for (bytes_transferred = 0;
588 bytes_transferred < register_size (gdbarch, regno);
589 bytes_transferred += sizeof (long))
592 *(long *) &buf[bytes_transferred]
593 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
594 regaddr += sizeof (long);
598 sprintf (message, "reading register %s (#%d)",
599 gdbarch_register_name (gdbarch, regno), regno);
600 perror_with_name (message);
604 /* Now supply the register. Keep in mind that the regcache's idea
605 of the register's size may not be a multiple of sizeof
607 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
609 /* Little-endian values are always found at the left end of the
610 bytes transferred. */
611 regcache_raw_supply (regcache, regno, buf);
613 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
615 /* Big-endian values are found at the right end of the bytes
617 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
618 regcache_raw_supply (regcache, regno, buf + padding);
621 internal_error (__FILE__, __LINE__,
622 _("fetch_register: unexpected byte order: %d"),
623 gdbarch_byte_order (gdbarch));
627 supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
630 struct gdbarch *gdbarch = get_regcache_arch (regcache);
631 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
632 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
634 for (i = 0; i < ppc_num_vshrs; i++)
636 regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
637 *vsxregsetp + i * vsxregsize);
642 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
645 struct gdbarch *gdbarch = get_regcache_arch (regcache);
646 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
647 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
648 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
649 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
651 for (i = 0; i < num_of_vrregs; i++)
653 /* The last 2 registers of this set are only 32 bit long, not
654 128. However an offset is necessary only for VSCR because it
655 occupies a whole vector, while VRSAVE occupies a full 4 bytes
657 if (i == (num_of_vrregs - 2))
658 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
659 *vrregsetp + i * vrregsize + offset);
661 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
662 *vrregsetp + i * vrregsize);
667 fetch_vsx_registers (struct regcache *regcache, int tid)
670 gdb_vsxregset_t regs;
672 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
677 have_ptrace_getsetvsxregs = 0;
680 perror_with_name (_("Unable to fetch VSX registers"));
682 supply_vsxregset (regcache, ®s);
686 fetch_altivec_registers (struct regcache *regcache, int tid)
691 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
696 have_ptrace_getvrregs = 0;
699 perror_with_name (_("Unable to fetch AltiVec registers"));
701 supply_vrregset (regcache, ®s);
704 /* This function actually issues the request to ptrace, telling
705 it to get all general-purpose registers and put them into the
708 If the ptrace request does not exist, this function returns 0
709 and properly sets the have_ptrace_* flag. If the request fails,
710 this function calls perror_with_name. Otherwise, if the request
711 succeeds, then the regcache gets filled and 1 is returned. */
713 fetch_all_gp_regs (struct regcache *regcache, int tid)
715 struct gdbarch *gdbarch = get_regcache_arch (regcache);
716 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
717 gdb_gregset_t gregset;
719 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
723 have_ptrace_getsetregs = 0;
726 perror_with_name (_("Couldn't get general-purpose registers."));
729 supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
734 /* This is a wrapper for the fetch_all_gp_regs function. It is
735 responsible for verifying if this target has the ptrace request
736 that can be used to fetch all general-purpose registers at one
737 shot. If it doesn't, then we should fetch them using the
738 old-fashioned way, which is to iterate over the registers and
739 request them one by one. */
741 fetch_gp_regs (struct regcache *regcache, int tid)
743 struct gdbarch *gdbarch = get_regcache_arch (regcache);
744 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
747 if (have_ptrace_getsetregs)
748 if (fetch_all_gp_regs (regcache, tid))
751 /* If we've hit this point, it doesn't really matter which
752 architecture we are using. We just need to read the
753 registers in the "old-fashioned way". */
754 for (i = 0; i < ppc_num_gprs; i++)
755 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
758 /* This function actually issues the request to ptrace, telling
759 it to get all floating-point registers and put them into the
762 If the ptrace request does not exist, this function returns 0
763 and properly sets the have_ptrace_* flag. If the request fails,
764 this function calls perror_with_name. Otherwise, if the request
765 succeeds, then the regcache gets filled and 1 is returned. */
767 fetch_all_fp_regs (struct regcache *regcache, int tid)
769 gdb_fpregset_t fpregs;
771 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
775 have_ptrace_getsetfpregs = 0;
778 perror_with_name (_("Couldn't get floating-point registers."));
781 supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
786 /* This is a wrapper for the fetch_all_fp_regs function. It is
787 responsible for verifying if this target has the ptrace request
788 that can be used to fetch all floating-point registers at one
789 shot. If it doesn't, then we should fetch them using the
790 old-fashioned way, which is to iterate over the registers and
791 request them one by one. */
793 fetch_fp_regs (struct regcache *regcache, int tid)
795 struct gdbarch *gdbarch = get_regcache_arch (regcache);
796 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
799 if (have_ptrace_getsetfpregs)
800 if (fetch_all_fp_regs (regcache, tid))
803 /* If we've hit this point, it doesn't really matter which
804 architecture we are using. We just need to read the
805 registers in the "old-fashioned way". */
806 for (i = 0; i < ppc_num_fprs; i++)
807 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
811 fetch_ppc_registers (struct regcache *regcache, int tid)
814 struct gdbarch *gdbarch = get_regcache_arch (regcache);
815 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
817 fetch_gp_regs (regcache, tid);
818 if (tdep->ppc_fp0_regnum >= 0)
819 fetch_fp_regs (regcache, tid);
820 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
821 if (tdep->ppc_ps_regnum != -1)
822 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
823 if (tdep->ppc_cr_regnum != -1)
824 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
825 if (tdep->ppc_lr_regnum != -1)
826 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
827 if (tdep->ppc_ctr_regnum != -1)
828 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
829 if (tdep->ppc_xer_regnum != -1)
830 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
831 if (tdep->ppc_mq_regnum != -1)
832 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
833 if (ppc_linux_trap_reg_p (gdbarch))
835 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
836 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
838 if (tdep->ppc_fpscr_regnum != -1)
839 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
840 if (have_ptrace_getvrregs)
841 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
842 fetch_altivec_registers (regcache, tid);
843 if (have_ptrace_getsetvsxregs)
844 if (tdep->ppc_vsr0_upper_regnum != -1)
845 fetch_vsx_registers (regcache, tid);
846 if (tdep->ppc_ev0_upper_regnum >= 0)
847 fetch_spe_register (regcache, tid, -1);
850 /* Fetch registers from the child process. Fetch all registers if
851 regno == -1, otherwise fetch all general registers or all floating
852 point registers depending upon the value of regno. */
854 ppc_linux_fetch_inferior_registers (struct target_ops *ops,
855 struct regcache *regcache, int regno)
857 /* Overload thread id onto process id */
858 int tid = TIDGET (inferior_ptid);
860 /* No thread id, just use process id */
862 tid = PIDGET (inferior_ptid);
865 fetch_ppc_registers (regcache, tid);
867 fetch_register (regcache, tid, regno);
870 /* Store one VSX register. */
872 store_vsx_register (const struct regcache *regcache, int tid, int regno)
875 gdb_vsxregset_t regs;
876 struct gdbarch *gdbarch = get_regcache_arch (regcache);
877 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
878 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
880 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, ®s);
885 have_ptrace_getsetvsxregs = 0;
888 perror_with_name (_("Unable to fetch VSX register"));
891 regcache_raw_collect (regcache, regno, regs +
892 (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
894 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, ®s);
896 perror_with_name (_("Unable to store VSX register"));
899 /* Store one register. */
901 store_altivec_register (const struct regcache *regcache, int tid, int regno)
906 struct gdbarch *gdbarch = get_regcache_arch (regcache);
907 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
908 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
910 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
915 have_ptrace_getvrregs = 0;
918 perror_with_name (_("Unable to fetch AltiVec register"));
921 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
922 long on the hardware. */
923 if (regno == (tdep->ppc_vrsave_regnum - 1))
924 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
926 regcache_raw_collect (regcache, regno,
927 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
929 ret = ptrace (PTRACE_SETVRREGS, tid, 0, ®s);
931 perror_with_name (_("Unable to store AltiVec register"));
934 /* Assuming TID referrs to an SPE process, set the top halves of TID's
935 general-purpose registers and its SPE-specific registers to the
936 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
939 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
940 PTRACE_SETEVRREGS requests are supported is isolated here, and in
941 get_spe_registers. */
943 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
945 if (have_ptrace_getsetevrregs)
947 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
951 /* EIO means that the PTRACE_SETEVRREGS request isn't
952 supported; we fail silently, and don't try the call
955 have_ptrace_getsetevrregs = 0;
957 /* Anything else needs to be reported. */
958 perror_with_name (_("Unable to set SPE registers"));
963 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
964 If REGNO is -1, write the values of all the SPE-specific
967 store_spe_register (const struct regcache *regcache, int tid, int regno)
969 struct gdbarch *gdbarch = get_regcache_arch (regcache);
970 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
971 struct gdb_evrregset_t evrregs;
973 gdb_assert (sizeof (evrregs.evr[0])
974 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
975 gdb_assert (sizeof (evrregs.acc)
976 == register_size (gdbarch, tdep->ppc_acc_regnum));
977 gdb_assert (sizeof (evrregs.spefscr)
978 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
981 /* Since we're going to write out every register, the code below
982 should store to every field of evrregs; if that doesn't happen,
983 make it obvious by initializing it with suspicious values. */
984 memset (&evrregs, 42, sizeof (evrregs));
986 /* We can only read and write the entire EVR register set at a
987 time, so to write just a single register, we do a
988 read-modify-write maneuver. */
989 get_spe_registers (tid, &evrregs);
995 for (i = 0; i < ppc_num_gprs; i++)
996 regcache_raw_collect (regcache,
997 tdep->ppc_ev0_upper_regnum + i,
1000 else if (tdep->ppc_ev0_upper_regnum <= regno
1001 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
1002 regcache_raw_collect (regcache, regno,
1003 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
1006 || regno == tdep->ppc_acc_regnum)
1007 regcache_raw_collect (regcache,
1008 tdep->ppc_acc_regnum,
1012 || regno == tdep->ppc_spefscr_regnum)
1013 regcache_raw_collect (regcache,
1014 tdep->ppc_spefscr_regnum,
1017 /* Write back the modified register set. */
1018 set_spe_registers (tid, &evrregs);
1022 store_register (const struct regcache *regcache, int tid, int regno)
1024 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1025 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1026 /* This isn't really an address. But ptrace thinks of it as one. */
1027 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
1029 size_t bytes_to_transfer;
1030 char buf[MAX_REGISTER_SIZE];
1032 if (altivec_register_p (gdbarch, regno))
1034 store_altivec_register (regcache, tid, regno);
1037 if (vsx_register_p (gdbarch, regno))
1039 store_vsx_register (regcache, tid, regno);
1042 else if (spe_register_p (gdbarch, regno))
1044 store_spe_register (regcache, tid, regno);
1051 /* First collect the register. Keep in mind that the regcache's
1052 idea of the register's size may not be a multiple of sizeof
1054 memset (buf, 0, sizeof buf);
1055 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
1056 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1058 /* Little-endian values always sit at the left end of the buffer. */
1059 regcache_raw_collect (regcache, regno, buf);
1061 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1063 /* Big-endian values sit at the right end of the buffer. */
1064 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
1065 regcache_raw_collect (regcache, regno, buf + padding);
1068 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
1071 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
1073 regaddr += sizeof (long);
1076 && (regno == tdep->ppc_fpscr_regnum
1077 || regno == PPC_ORIG_R3_REGNUM
1078 || regno == PPC_TRAP_REGNUM))
1080 /* Some older kernel versions don't allow fpscr, orig_r3
1081 or trap to be written. */
1088 sprintf (message, "writing register %s (#%d)",
1089 gdbarch_register_name (gdbarch, regno), regno);
1090 perror_with_name (message);
1096 fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
1099 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1100 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1101 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
1103 for (i = 0; i < ppc_num_vshrs; i++)
1104 regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
1105 *vsxregsetp + i * vsxregsize);
1109 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
1112 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1113 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1114 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
1115 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
1116 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
1118 for (i = 0; i < num_of_vrregs; i++)
1120 /* The last 2 registers of this set are only 32 bit long, not
1121 128, but only VSCR is fetched as a 16 bytes quantity. */
1122 if (i == (num_of_vrregs - 2))
1123 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1124 *vrregsetp + i * vrregsize + offset);
1126 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1127 *vrregsetp + i * vrregsize);
1132 store_vsx_registers (const struct regcache *regcache, int tid)
1135 gdb_vsxregset_t regs;
1137 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
1142 have_ptrace_getsetvsxregs = 0;
1145 perror_with_name (_("Couldn't get VSX registers"));
1148 fill_vsxregset (regcache, ®s);
1150 if (ptrace (PTRACE_SETVSXREGS, tid, 0, ®s) < 0)
1151 perror_with_name (_("Couldn't write VSX registers"));
1155 store_altivec_registers (const struct regcache *regcache, int tid)
1158 gdb_vrregset_t regs;
1160 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
1165 have_ptrace_getvrregs = 0;
1168 perror_with_name (_("Couldn't get AltiVec registers"));
1171 fill_vrregset (regcache, ®s);
1173 if (ptrace (PTRACE_SETVRREGS, tid, 0, ®s) < 0)
1174 perror_with_name (_("Couldn't write AltiVec registers"));
1177 /* This function actually issues the request to ptrace, telling
1178 it to store all general-purpose registers present in the specified
1181 If the ptrace request does not exist, this function returns 0
1182 and properly sets the have_ptrace_* flag. If the request fails,
1183 this function calls perror_with_name. Otherwise, if the request
1184 succeeds, then the regcache is stored and 1 is returned. */
1186 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1188 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1189 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1190 gdb_gregset_t gregset;
1192 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1196 have_ptrace_getsetregs = 0;
1199 perror_with_name (_("Couldn't get general-purpose registers."));
1202 fill_gregset (regcache, &gregset, regno);
1204 if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
1208 have_ptrace_getsetregs = 0;
1211 perror_with_name (_("Couldn't set general-purpose registers."));
1217 /* This is a wrapper for the store_all_gp_regs function. It is
1218 responsible for verifying if this target has the ptrace request
1219 that can be used to store all general-purpose registers at one
1220 shot. If it doesn't, then we should store them using the
1221 old-fashioned way, which is to iterate over the registers and
1222 store them one by one. */
1224 store_gp_regs (const struct regcache *regcache, int tid, int regno)
1226 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1227 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1230 if (have_ptrace_getsetregs)
1231 if (store_all_gp_regs (regcache, tid, regno))
1234 /* If we hit this point, it doesn't really matter which
1235 architecture we are using. We just need to store the
1236 registers in the "old-fashioned way". */
1237 for (i = 0; i < ppc_num_gprs; i++)
1238 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1241 /* This function actually issues the request to ptrace, telling
1242 it to store all floating-point registers present in the specified
1245 If the ptrace request does not exist, this function returns 0
1246 and properly sets the have_ptrace_* flag. If the request fails,
1247 this function calls perror_with_name. Otherwise, if the request
1248 succeeds, then the regcache is stored and 1 is returned. */
1250 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
1252 gdb_fpregset_t fpregs;
1254 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1258 have_ptrace_getsetfpregs = 0;
1261 perror_with_name (_("Couldn't get floating-point registers."));
1264 fill_fpregset (regcache, &fpregs, regno);
1266 if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
1270 have_ptrace_getsetfpregs = 0;
1273 perror_with_name (_("Couldn't set floating-point registers."));
1279 /* This is a wrapper for the store_all_fp_regs function. It is
1280 responsible for verifying if this target has the ptrace request
1281 that can be used to store all floating-point registers at one
1282 shot. If it doesn't, then we should store them using the
1283 old-fashioned way, which is to iterate over the registers and
1284 store them one by one. */
1286 store_fp_regs (const struct regcache *regcache, int tid, int regno)
1288 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1289 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1292 if (have_ptrace_getsetfpregs)
1293 if (store_all_fp_regs (regcache, tid, regno))
1296 /* If we hit this point, it doesn't really matter which
1297 architecture we are using. We just need to store the
1298 registers in the "old-fashioned way". */
1299 for (i = 0; i < ppc_num_fprs; i++)
1300 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
1304 store_ppc_registers (const struct regcache *regcache, int tid)
1307 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1308 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1310 store_gp_regs (regcache, tid, -1);
1311 if (tdep->ppc_fp0_regnum >= 0)
1312 store_fp_regs (regcache, tid, -1);
1313 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1314 if (tdep->ppc_ps_regnum != -1)
1315 store_register (regcache, tid, tdep->ppc_ps_regnum);
1316 if (tdep->ppc_cr_regnum != -1)
1317 store_register (regcache, tid, tdep->ppc_cr_regnum);
1318 if (tdep->ppc_lr_regnum != -1)
1319 store_register (regcache, tid, tdep->ppc_lr_regnum);
1320 if (tdep->ppc_ctr_regnum != -1)
1321 store_register (regcache, tid, tdep->ppc_ctr_regnum);
1322 if (tdep->ppc_xer_regnum != -1)
1323 store_register (regcache, tid, tdep->ppc_xer_regnum);
1324 if (tdep->ppc_mq_regnum != -1)
1325 store_register (regcache, tid, tdep->ppc_mq_regnum);
1326 if (tdep->ppc_fpscr_regnum != -1)
1327 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
1328 if (ppc_linux_trap_reg_p (gdbarch))
1330 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1331 store_register (regcache, tid, PPC_TRAP_REGNUM);
1333 if (have_ptrace_getvrregs)
1334 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1335 store_altivec_registers (regcache, tid);
1336 if (have_ptrace_getsetvsxregs)
1337 if (tdep->ppc_vsr0_upper_regnum != -1)
1338 store_vsx_registers (regcache, tid);
1339 if (tdep->ppc_ev0_upper_regnum >= 0)
1340 store_spe_register (regcache, tid, -1);
1343 /* Fetch the AT_HWCAP entry from the aux vector. */
1344 unsigned long ppc_linux_get_hwcap (void)
1348 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
1349 return (unsigned long) field;
1354 /* The cached DABR value, to install in new threads.
1355 This variable is used when we are dealing with non-BookE
1357 static long saved_dabr_value;
1359 /* Global structure that will store information about the available
1360 features on this BookE processor. */
1361 static struct ppc_debug_info booke_debug_info;
1363 /* Global variable that holds the maximum number of slots that the
1364 kernel will use. This is only used when the processor is BookE. */
1365 static size_t max_slots_number = 0;
1367 struct hw_break_tuple
1370 struct ppc_hw_breakpoint *hw_break;
1373 /* This is an internal VEC created to store information about *points inserted
1374 for each thread. This is used for BookE processors. */
1375 typedef struct thread_points
1377 /* The TID to which this *point relates. */
1379 /* Information about the *point, such as its address, type, etc.
1381 Each element inside this vector corresponds to a hardware
1382 breakpoint or watchpoint in the thread represented by TID. The maximum
1383 size of these vector is MAX_SLOTS_NUMBER. If the hw_break element of
1384 the tuple is NULL, then the position in the vector is free. */
1385 struct hw_break_tuple *hw_breaks;
1387 DEF_VEC_P (thread_points_p);
1389 VEC(thread_points_p) *ppc_threads = NULL;
1391 /* The version of the kernel interface that we will use if the processor is
1393 #define PPC_DEBUG_CURRENT_VERSION 1
1395 /* Returns non-zero if we support the ptrace interface which enables
1396 booke debugging resources. */
1398 have_ptrace_booke_interface (void)
1400 static int have_ptrace_booke_interface = -1;
1402 if (have_ptrace_booke_interface == -1)
1406 tid = TIDGET (inferior_ptid);
1408 tid = PIDGET (inferior_ptid);
1410 /* Check for kernel support for BOOKE debug registers. */
1411 if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &booke_debug_info) >= 0)
1413 have_ptrace_booke_interface = 1;
1414 max_slots_number = booke_debug_info.num_instruction_bps
1415 + booke_debug_info.num_data_bps + booke_debug_info.num_condition_regs;
1419 /* Old school interface and no BOOKE debug registers support. */
1420 have_ptrace_booke_interface = 0;
1421 memset (&booke_debug_info, 0, sizeof (struct ppc_debug_info));
1425 return have_ptrace_booke_interface;
1429 ppc_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
1431 int total_hw_wp, total_hw_bp;
1433 if (have_ptrace_booke_interface ())
1435 /* For PPC BookE processors, the number of available hardware
1436 watchpoints and breakpoints is stored at the booke_debug_info
1438 total_hw_bp = booke_debug_info.num_instruction_bps;
1439 total_hw_wp = booke_debug_info.num_data_bps;
1443 /* For PPC server processors, we accept 1 hardware watchpoint and 0
1444 hardware breakpoints. */
1449 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
1450 || type == bp_access_watchpoint || type == bp_watchpoint)
1452 if (cnt > total_hw_wp)
1455 else if (type == bp_hardware_breakpoint)
1457 if (cnt > total_hw_bp)
1461 if (!have_ptrace_booke_interface ())
1464 ptid_t ptid = inferior_ptid;
1466 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
1467 the target has DABR. If either answer is no, the ptrace call will
1468 return -1. Fail in that case. */
1469 tid = TIDGET (ptid);
1471 tid = PIDGET (ptid);
1473 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1481 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1483 /* Handle sub-8-byte quantities. */
1487 /* The new BookE ptrace interface tells if there are alignment restrictions
1488 for watchpoints in the processors. In that case, we use that information
1489 to determine the hardcoded watchable region for watchpoints. */
1490 if (have_ptrace_booke_interface ())
1492 if (booke_debug_info.data_bp_alignment
1493 && (addr + len > (addr & ~(booke_debug_info.data_bp_alignment - 1))
1494 + booke_debug_info.data_bp_alignment))
1497 /* addr+len must fall in the 8 byte watchable region for DABR-based
1498 processors (i.e., server processors). Without the new BookE ptrace
1499 interface, DAC-based processors (i.e., embedded processors) will use
1500 addresses aligned to 4-bytes due to the way the read/write flags are
1501 passed in the old ptrace interface. */
1502 else if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1503 && (addr + len) > (addr & ~3) + 4)
1504 || (addr + len) > (addr & ~7) + 8)
1510 /* This function compares two ppc_hw_breakpoint structs field-by-field. */
1512 booke_cmp_hw_point (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
1514 return (a->trigger_type == b->trigger_type
1515 && a->addr_mode == b->addr_mode
1516 && a->condition_mode == b->condition_mode
1517 && a->addr == b->addr
1518 && a->addr2 == b->addr2
1519 && a->condition_value == b->condition_value);
1522 /* This function can be used to retrieve a thread_points by the TID of the
1523 related process/thread. If nothing has been found, and ALLOC_NEW is 0,
1524 it returns NULL. If ALLOC_NEW is non-zero, a new thread_points for the
1525 provided TID will be created and returned. */
1526 static struct thread_points *
1527 booke_find_thread_points_by_tid (int tid, int alloc_new)
1530 struct thread_points *t;
1532 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
1538 /* Do we need to allocate a new point_item
1539 if the wanted one does not exist? */
1542 t = xmalloc (sizeof (struct thread_points));
1543 t->hw_breaks = xzalloc (max_slots_number * sizeof (struct hw_break_tuple));
1545 VEC_safe_push (thread_points_p, ppc_threads, t);
1551 /* This function is a generic wrapper that is responsible for inserting a
1552 *point (i.e., calling `ptrace' in order to issue the request to the
1553 kernel) and registering it internally in GDB. */
1555 booke_insert_point (struct ppc_hw_breakpoint *b, int tid)
1559 struct ppc_hw_breakpoint *p = xmalloc (sizeof (struct ppc_hw_breakpoint));
1560 struct hw_break_tuple *hw_breaks;
1561 struct cleanup *c = make_cleanup (xfree, p);
1562 struct thread_points *t;
1563 struct hw_break_tuple *tuple;
1565 memcpy (p, b, sizeof (struct ppc_hw_breakpoint));
1568 slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p);
1570 perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
1572 /* Everything went fine, so we have to register this *point. */
1573 t = booke_find_thread_points_by_tid (tid, 1);
1574 gdb_assert (t != NULL);
1575 hw_breaks = t->hw_breaks;
1577 /* Find a free element in the hw_breaks vector. */
1578 for (i = 0; i < max_slots_number; i++)
1579 if (hw_breaks[i].hw_break == NULL)
1581 hw_breaks[i].slot = slot;
1582 hw_breaks[i].hw_break = p;
1586 gdb_assert (i != max_slots_number);
1588 discard_cleanups (c);
1591 /* This function is a generic wrapper that is responsible for removing a
1592 *point (i.e., calling `ptrace' in order to issue the request to the
1593 kernel), and unregistering it internally at GDB. */
1595 booke_remove_point (struct ppc_hw_breakpoint *b, int tid)
1598 struct hw_break_tuple *hw_breaks;
1599 struct thread_points *t;
1601 t = booke_find_thread_points_by_tid (tid, 0);
1602 gdb_assert (t != NULL);
1603 hw_breaks = t->hw_breaks;
1605 for (i = 0; i < max_slots_number; i++)
1606 if (hw_breaks[i].hw_break && booke_cmp_hw_point (hw_breaks[i].hw_break, b))
1609 gdb_assert (i != max_slots_number);
1611 /* We have to ignore ENOENT errors because the kernel implements hardware
1612 breakpoints/watchpoints as "one-shot", that is, they are automatically
1613 deleted when hit. */
1615 if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
1616 if (errno != ENOENT)
1617 perror_with_name (_("Unexpected error deleting breakpoint or watchpoint"));
1619 xfree (hw_breaks[i].hw_break);
1620 hw_breaks[i].hw_break = NULL;
1624 ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
1625 struct bp_target_info *bp_tgt)
1628 struct lwp_info *lp;
1629 struct ppc_hw_breakpoint p;
1631 if (!have_ptrace_booke_interface ())
1634 p.version = PPC_DEBUG_CURRENT_VERSION;
1635 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1636 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1637 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1638 p.addr = (uint64_t) bp_tgt->placed_address;
1640 p.condition_value = 0;
1643 booke_insert_point (&p, TIDGET (ptid));
1649 ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
1650 struct bp_target_info *bp_tgt)
1653 struct lwp_info *lp;
1654 struct ppc_hw_breakpoint p;
1656 if (!have_ptrace_booke_interface ())
1659 p.version = PPC_DEBUG_CURRENT_VERSION;
1660 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1661 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1662 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1663 p.addr = (uint64_t) bp_tgt->placed_address;
1665 p.condition_value = 0;
1668 booke_remove_point (&p, TIDGET (ptid));
1674 get_trigger_type (int rw)
1679 t = PPC_BREAKPOINT_TRIGGER_READ;
1680 else if (rw == hw_write)
1681 t = PPC_BREAKPOINT_TRIGGER_WRITE;
1683 t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
1689 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
1691 struct lwp_info *lp;
1695 if (have_ptrace_booke_interface ())
1697 struct ppc_hw_breakpoint p;
1699 p.version = PPC_DEBUG_CURRENT_VERSION;
1700 p.trigger_type = get_trigger_type (rw);
1701 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1702 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1703 p.addr = (uint64_t) addr;
1705 p.condition_value = 0;
1708 booke_insert_point (&p, TIDGET (ptid));
1715 long read_mode, write_mode;
1717 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1719 /* PowerPC 440 requires only the read/write flags to be passed
1726 /* PowerPC 970 and other DABR-based processors are required to pass
1727 the Breakpoint Translation bit together with the flags. */
1732 dabr_value = addr & ~(read_mode | write_mode);
1736 /* Set read and translate bits. */
1737 dabr_value |= read_mode;
1740 /* Set write and translate bits. */
1741 dabr_value |= write_mode;
1744 /* Set read, write and translate bits. */
1745 dabr_value |= read_mode | write_mode;
1749 saved_dabr_value = dabr_value;
1752 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1762 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
1764 struct lwp_info *lp;
1768 if (have_ptrace_booke_interface ())
1770 struct ppc_hw_breakpoint p;
1772 p.version = PPC_DEBUG_CURRENT_VERSION;
1773 p.trigger_type = get_trigger_type (rw);
1774 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1775 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1776 p.addr = (uint64_t) addr;
1778 p.condition_value = 0;
1781 booke_remove_point (&p, TIDGET (ptid));
1787 saved_dabr_value = 0;
1789 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1799 ppc_linux_new_thread (ptid_t ptid)
1801 int tid = TIDGET (ptid);
1803 if (have_ptrace_booke_interface ())
1806 struct thread_points *p;
1807 struct hw_break_tuple *hw_breaks;
1809 if (VEC_empty (thread_points_p, ppc_threads))
1812 /* Get a list of breakpoints from any thread. */
1813 p = VEC_last (thread_points_p, ppc_threads);
1814 hw_breaks = p->hw_breaks;
1816 /* Copy that thread's breakpoints and watchpoints to the new thread. */
1817 for (i = 0; i < max_slots_number; i++)
1818 if (hw_breaks[i].hw_break)
1819 booke_insert_point (hw_breaks[i].hw_break, tid);
1822 ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
1826 ppc_linux_thread_exit (struct thread_info *tp, int silent)
1829 int tid = TIDGET (tp->ptid);
1830 struct hw_break_tuple *hw_breaks;
1831 struct thread_points *t = NULL, *p;
1833 if (!have_ptrace_booke_interface ())
1836 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
1846 VEC_unordered_remove (thread_points_p, ppc_threads, i);
1848 hw_breaks = t->hw_breaks;
1850 for (i = 0; i < max_slots_number; i++)
1851 if (hw_breaks[i].hw_break)
1852 xfree (hw_breaks[i].hw_break);
1854 xfree (t->hw_breaks);
1859 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1861 struct siginfo *siginfo_p;
1863 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
1865 if (siginfo_p->si_signo != SIGTRAP
1866 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1869 if (have_ptrace_booke_interface ())
1872 struct thread_points *t;
1873 struct hw_break_tuple *hw_breaks;
1874 /* The index (or slot) of the *point is passed in the si_errno field. */
1875 int slot = siginfo_p->si_errno;
1877 t = booke_find_thread_points_by_tid (TIDGET (inferior_ptid), 0);
1879 /* Find out if this *point is a hardware breakpoint.
1880 If so, we should return 0. */
1883 hw_breaks = t->hw_breaks;
1884 for (i = 0; i < max_slots_number; i++)
1885 if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
1886 && hw_breaks[i].hw_break->trigger_type
1887 == PPC_BREAKPOINT_TRIGGER_EXECUTE)
1892 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
1897 ppc_linux_stopped_by_watchpoint (void)
1900 return ppc_linux_stopped_data_address (¤t_target, &addr);
1904 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
1906 CORE_ADDR start, int length)
1910 if (have_ptrace_booke_interface ()
1911 && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1912 return start <= addr && start + length >= addr;
1913 else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1920 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
1921 return start <= addr + mask && start + length - 1 >= addr;
1925 ppc_linux_store_inferior_registers (struct target_ops *ops,
1926 struct regcache *regcache, int regno)
1928 /* Overload thread id onto process id */
1929 int tid = TIDGET (inferior_ptid);
1931 /* No thread id, just use process id */
1933 tid = PIDGET (inferior_ptid);
1936 store_register (regcache, tid, regno);
1938 store_ppc_registers (regcache, tid);
1941 /* Functions for transferring registers between a gregset_t or fpregset_t
1942 (see sys/ucontext.h) and gdb's regcache. The word size is that used
1943 by the ptrace interface, not the current program's ABI. eg. If a
1944 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
1945 read or write 64-bit gregsets. This is to suit the host libthread_db. */
1948 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1950 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1952 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1956 fill_gregset (const struct regcache *regcache,
1957 gdb_gregset_t *gregsetp, int regno)
1959 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1962 memset (gregsetp, 0, sizeof (*gregsetp));
1963 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1967 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1969 const struct regset *regset = ppc_linux_fpregset ();
1971 ppc_supply_fpregset (regset, regcache, -1,
1972 fpregsetp, sizeof (*fpregsetp));
1976 fill_fpregset (const struct regcache *regcache,
1977 gdb_fpregset_t *fpregsetp, int regno)
1979 const struct regset *regset = ppc_linux_fpregset ();
1981 ppc_collect_fpregset (regset, regcache, regno,
1982 fpregsetp, sizeof (*fpregsetp));
1986 ppc_linux_target_wordsize (void)
1990 /* Check for 64-bit inferior process. This is the case when the host is
1991 64-bit, and in addition the top bit of the MSR register is set. */
1992 #ifdef __powerpc64__
1995 int tid = TIDGET (inferior_ptid);
1997 tid = PIDGET (inferior_ptid);
2000 msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
2001 if (errno == 0 && msr < 0)
2009 ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
2010 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
2012 int sizeof_auxv_field = ppc_linux_target_wordsize ();
2013 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
2014 gdb_byte *ptr = *readptr;
2019 if (endptr - ptr < sizeof_auxv_field * 2)
2022 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2023 ptr += sizeof_auxv_field;
2024 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2025 ptr += sizeof_auxv_field;
2031 static const struct target_desc *
2032 ppc_linux_read_description (struct target_ops *ops)
2039 int tid = TIDGET (inferior_ptid);
2041 tid = PIDGET (inferior_ptid);
2043 if (have_ptrace_getsetevrregs)
2045 struct gdb_evrregset_t evrregset;
2047 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
2048 return tdesc_powerpc_e500l;
2050 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
2051 Anything else needs to be reported. */
2052 else if (errno != EIO)
2053 perror_with_name (_("Unable to fetch SPE registers"));
2056 if (have_ptrace_getsetvsxregs)
2058 gdb_vsxregset_t vsxregset;
2060 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
2063 /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
2064 Anything else needs to be reported. */
2065 else if (errno != EIO)
2066 perror_with_name (_("Unable to fetch VSX registers"));
2069 if (have_ptrace_getvrregs)
2071 gdb_vrregset_t vrregset;
2073 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
2076 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
2077 Anything else needs to be reported. */
2078 else if (errno != EIO)
2079 perror_with_name (_("Unable to fetch AltiVec registers"));
2082 /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
2083 the FPSCR from 32 bits to 64 bits. Even though Power 7 supports this
2084 ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
2085 PPC_FEATURE_ARCH_2_06. Since for now the only bits used in the higher
2086 half of the register are for Decimal Floating Point, we check if that
2087 feature is available to decide the size of the FPSCR. */
2088 if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
2091 if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
2094 if (ppc_linux_target_wordsize () == 8)
2097 return tdesc_powerpc_cell64l;
2099 return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
2101 return isa205? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
2103 return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
2107 return tdesc_powerpc_cell32l;
2109 return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
2111 return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
2113 return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
2116 void _initialize_ppc_linux_nat (void);
2119 _initialize_ppc_linux_nat (void)
2121 struct target_ops *t;
2123 /* Fill in the generic GNU/Linux methods. */
2124 t = linux_target ();
2126 /* Add our register access methods. */
2127 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
2128 t->to_store_registers = ppc_linux_store_inferior_registers;
2130 /* Add our breakpoint/watchpoint methods. */
2131 t->to_can_use_hw_breakpoint = ppc_linux_can_use_hw_breakpoint;
2132 t->to_insert_hw_breakpoint = ppc_linux_insert_hw_breakpoint;
2133 t->to_remove_hw_breakpoint = ppc_linux_remove_hw_breakpoint;
2134 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
2135 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
2136 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
2137 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
2138 t->to_stopped_data_address = ppc_linux_stopped_data_address;
2139 t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
2141 t->to_read_description = ppc_linux_read_description;
2142 t->to_auxv_parse = ppc_linux_auxv_parse;
2144 observer_attach_thread_exit (ppc_linux_thread_exit);
2146 /* Register the target. */
2147 linux_nat_add_target (t);
2148 linux_nat_set_new_thread (t, ppc_linux_new_thread);