1 /* PPC GNU/Linux native support.
3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "gdb_string.h"
29 #include "gdb_assert.h"
31 #include "linux-nat.h"
33 #include <sys/types.h>
34 #include <sys/param.h>
37 #include <sys/ioctl.h>
40 #include <sys/procfs.h>
41 #include <sys/ptrace.h>
43 /* Prototypes for supply_gregset etc. */
48 #define PT_READ_U PTRACE_PEEKUSR
51 #define PT_WRITE_U PTRACE_POKEUSR
54 /* Default the type of the ptrace transfer to int. */
55 #ifndef PTRACE_XFER_TYPE
56 #define PTRACE_XFER_TYPE int
59 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
60 configure time check. Some older glibc's (for instance 2.2.1)
61 don't have a specific powerpc version of ptrace.h, and fall back on
62 a generic one. In such cases, sys/ptrace.h defines
63 PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
64 ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
65 PTRACE_SETVRREGS to be. This also makes a configury check pretty
68 /* These definitions should really come from the glibc header files,
69 but Glibc doesn't know about the vrregs yet. */
70 #ifndef PTRACE_GETVRREGS
71 #define PTRACE_GETVRREGS 18
72 #define PTRACE_SETVRREGS 19
76 /* Similarly for the ptrace requests for getting / setting the SPE
77 registers (ev0 -- ev31, acc, and spefscr). See the description of
78 gdb_evrregset_t for details. */
79 #ifndef PTRACE_GETEVRREGS
80 #define PTRACE_GETEVRREGS 20
81 #define PTRACE_SETEVRREGS 21
84 /* Similarly for the hardware watchpoint support. */
85 #ifndef PTRACE_GET_DEBUGREG
86 #define PTRACE_GET_DEBUGREG 25
88 #ifndef PTRACE_SET_DEBUGREG
89 #define PTRACE_SET_DEBUGREG 26
91 #ifndef PTRACE_GETSIGINFO
92 #define PTRACE_GETSIGINFO 0x4202
95 /* This oddity is because the Linux kernel defines elf_vrregset_t as
96 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
97 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
98 the vrsave as an extra 4 bytes at the end. I opted for creating a
99 flat array of chars, so that it is easier to manipulate for gdb.
101 There are 32 vector registers 16 bytes longs, plus a VSCR register
102 which is only 4 bytes long, but is fetched as a 16 bytes
103 quantity. Up to here we have the elf_vrregset_t structure.
104 Appended to this there is space for the VRSAVE register: 4 bytes.
105 Even though this vrsave register is not included in the regset
106 typedef, it is handled by the ptrace requests.
108 Note that GNU/Linux doesn't support little endian PPC hardware,
109 therefore the offset at which the real value of the VSCR register
110 is located will be always 12 bytes.
112 The layout is like this (where x is the actual value of the vscr reg): */
116 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
117 <-------> <-------><-------><->
122 #define SIZEOF_VRREGS 33*16+4
124 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
127 /* On PPC processors that support the the Signal Processing Extension
128 (SPE) APU, the general-purpose registers are 64 bits long.
129 However, the ordinary Linux kernel PTRACE_PEEKUSR / PTRACE_POKEUSR
130 / PT_READ_U / PT_WRITE_U ptrace calls only access the lower half of
131 each register, to allow them to behave the same way they do on
132 non-SPE systems. There's a separate pair of calls,
133 PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that read and write the top
134 halves of all the general-purpose registers at once, along with
135 some SPE-specific registers.
137 GDB itself continues to claim the general-purpose registers are 32
138 bits long. It has unnamed raw registers that hold the upper halves
139 of the gprs, and the the full 64-bit SIMD views of the registers,
140 'ev0' -- 'ev31', are pseudo-registers that splice the top and
141 bottom halves together.
143 This is the structure filled in by PTRACE_GETEVRREGS and written to
144 the inferior's registers by PTRACE_SETEVRREGS. */
145 struct gdb_evrregset_t
147 unsigned long evr[32];
148 unsigned long long acc;
149 unsigned long spefscr;
153 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
154 PTRACE_SETVRREGS requests, for reading and writing the Altivec
155 registers. Zero if we've tried one of them and gotten an
157 int have_ptrace_getvrregs = 1;
159 static CORE_ADDR last_stopped_data_address = 0;
161 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
162 PTRACE_SETEVRREGS requests, for reading and writing the SPE
163 registers. Zero if we've tried one of them and gotten an
165 int have_ptrace_getsetevrregs = 1;
170 return (sizeof (struct user));
174 /* registers layout, as presented by the ptrace interface:
175 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
176 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
177 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
178 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
179 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
180 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
181 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
182 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
183 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
187 ppc_register_u_addr (int regno)
190 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
191 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
192 interface, and not the wordsize of the program's ABI. */
193 int wordsize = sizeof (PTRACE_XFER_TYPE);
195 /* General purpose registers occupy 1 slot each in the buffer */
196 if (regno >= tdep->ppc_gp0_regnum
197 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
198 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
200 /* Floating point regs: eight bytes each in both 32- and 64-bit
201 ptrace interfaces. Thus, two slots each in 32-bit interface, one
202 slot each in 64-bit interface. */
203 if (tdep->ppc_fp0_regnum >= 0
204 && regno >= tdep->ppc_fp0_regnum
205 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
206 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
208 /* UISA special purpose registers: 1 slot each */
209 if (regno == PC_REGNUM)
210 u_addr = PT_NIP * wordsize;
211 if (regno == tdep->ppc_lr_regnum)
212 u_addr = PT_LNK * wordsize;
213 if (regno == tdep->ppc_cr_regnum)
214 u_addr = PT_CCR * wordsize;
215 if (regno == tdep->ppc_xer_regnum)
216 u_addr = PT_XER * wordsize;
217 if (regno == tdep->ppc_ctr_regnum)
218 u_addr = PT_CTR * wordsize;
220 if (regno == tdep->ppc_mq_regnum)
221 u_addr = PT_MQ * wordsize;
223 if (regno == tdep->ppc_ps_regnum)
224 u_addr = PT_MSR * wordsize;
225 if (tdep->ppc_fpscr_regnum >= 0
226 && regno == tdep->ppc_fpscr_regnum)
228 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
229 kernel headers incorrectly contained the 32-bit definition of
230 PT_FPSCR. For the 32-bit definition, floating-point
231 registers occupy two 32-bit "slots", and the FPSCR lives in
232 the secondhalf of such a slot-pair (hence +1). For 64-bit,
233 the FPSCR instead occupies the full 64-bit 2-word-slot and
234 hence no adjustment is necessary. Hack around this. */
235 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
236 u_addr = (48 + 32) * wordsize;
238 u_addr = PT_FPSCR * wordsize;
243 /* The Linux kernel ptrace interface for AltiVec registers uses the
244 registers set mechanism, as opposed to the interface for all the
245 other registers, that stores/fetches each register individually. */
247 fetch_altivec_register (int tid, int regno)
252 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
253 int vrregsize = register_size (current_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 (current_gdbarch, tdep->ppc_vrsave_regnum);
273 regcache_raw_supply (current_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 (int tid, int regno)
314 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
315 struct gdb_evrregset_t evrregs;
317 gdb_assert (sizeof (evrregs.evr[0])
318 == register_size (current_gdbarch, tdep->ppc_ev0_upper_regnum));
319 gdb_assert (sizeof (evrregs.acc)
320 == register_size (current_gdbarch, tdep->ppc_acc_regnum));
321 gdb_assert (sizeof (evrregs.spefscr)
322 == register_size (current_gdbarch, tdep->ppc_spefscr_regnum));
324 get_spe_registers (tid, &evrregs);
330 for (i = 0; i < ppc_num_gprs; i++)
331 regcache_raw_supply (current_regcache, tdep->ppc_ev0_upper_regnum + i,
334 else if (tdep->ppc_ev0_upper_regnum <= regno
335 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
336 regcache_raw_supply (current_regcache, regno,
337 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
340 || regno == tdep->ppc_acc_regnum)
341 regcache_raw_supply (current_regcache, tdep->ppc_acc_regnum, &evrregs.acc);
344 || regno == tdep->ppc_spefscr_regnum)
345 regcache_raw_supply (current_regcache, tdep->ppc_spefscr_regnum,
350 fetch_register (int tid, int regno)
352 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
353 /* This isn't really an address. But ptrace thinks of it as one. */
354 CORE_ADDR regaddr = ppc_register_u_addr (regno);
355 int bytes_transferred;
356 unsigned int offset; /* Offset of registers within the u area. */
357 char buf[MAX_REGISTER_SIZE];
359 if (altivec_register_p (regno))
361 /* If this is the first time through, or if it is not the first
362 time through, and we have comfirmed that there is kernel
363 support for such a ptrace request, then go and fetch the
365 if (have_ptrace_getvrregs)
367 fetch_altivec_register (tid, regno);
370 /* If we have discovered that there is no ptrace support for
371 AltiVec registers, fall through and return zeroes, because
372 regaddr will be -1 in this case. */
374 else if (spe_register_p (regno))
376 fetch_spe_register (tid, regno);
382 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */
383 regcache_raw_supply (current_regcache, regno, buf);
387 /* Read the raw register using PTRACE_XFER_TYPE sized chunks. On a
388 32-bit platform, 64-bit floating-point registers will require two
390 for (bytes_transferred = 0;
391 bytes_transferred < register_size (current_gdbarch, regno);
392 bytes_transferred += sizeof (PTRACE_XFER_TYPE))
395 *(PTRACE_XFER_TYPE *) & buf[bytes_transferred]
396 = ptrace (PT_READ_U, tid, (PTRACE_ARG3_TYPE) regaddr, 0);
397 regaddr += sizeof (PTRACE_XFER_TYPE);
401 sprintf (message, "reading register %s (#%d)",
402 REGISTER_NAME (regno), regno);
403 perror_with_name (message);
407 /* Now supply the register. Keep in mind that the regcache's idea
408 of the register's size may not be a multiple of sizeof
409 (PTRACE_XFER_TYPE). */
410 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
412 /* Little-endian values are always found at the left end of the
413 bytes transferred. */
414 regcache_raw_supply (current_regcache, regno, buf);
416 else if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
418 /* Big-endian values are found at the right end of the bytes
420 size_t padding = (bytes_transferred
421 - register_size (current_gdbarch, regno));
422 regcache_raw_supply (current_regcache, regno, buf + padding);
425 internal_error (__FILE__, __LINE__,
426 _("fetch_register: unexpected byte order: %d"),
427 gdbarch_byte_order (current_gdbarch));
431 supply_vrregset (gdb_vrregset_t *vrregsetp)
434 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
435 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
436 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
437 int offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
439 for (i = 0; i < num_of_vrregs; i++)
441 /* The last 2 registers of this set are only 32 bit long, not
442 128. However an offset is necessary only for VSCR because it
443 occupies a whole vector, while VRSAVE occupies a full 4 bytes
445 if (i == (num_of_vrregs - 2))
446 regcache_raw_supply (current_regcache, tdep->ppc_vr0_regnum + i,
447 *vrregsetp + i * vrregsize + offset);
449 regcache_raw_supply (current_regcache, tdep->ppc_vr0_regnum + i,
450 *vrregsetp + i * vrregsize);
455 fetch_altivec_registers (int tid)
460 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
465 have_ptrace_getvrregs = 0;
468 perror_with_name (_("Unable to fetch AltiVec registers"));
470 supply_vrregset (®s);
474 fetch_ppc_registers (int tid)
477 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
479 for (i = 0; i < ppc_num_gprs; i++)
480 fetch_register (tid, tdep->ppc_gp0_regnum + i);
481 if (tdep->ppc_fp0_regnum >= 0)
482 for (i = 0; i < ppc_num_fprs; i++)
483 fetch_register (tid, tdep->ppc_fp0_regnum + i);
484 fetch_register (tid, PC_REGNUM);
485 if (tdep->ppc_ps_regnum != -1)
486 fetch_register (tid, tdep->ppc_ps_regnum);
487 if (tdep->ppc_cr_regnum != -1)
488 fetch_register (tid, tdep->ppc_cr_regnum);
489 if (tdep->ppc_lr_regnum != -1)
490 fetch_register (tid, tdep->ppc_lr_regnum);
491 if (tdep->ppc_ctr_regnum != -1)
492 fetch_register (tid, tdep->ppc_ctr_regnum);
493 if (tdep->ppc_xer_regnum != -1)
494 fetch_register (tid, tdep->ppc_xer_regnum);
495 if (tdep->ppc_mq_regnum != -1)
496 fetch_register (tid, tdep->ppc_mq_regnum);
497 if (tdep->ppc_fpscr_regnum != -1)
498 fetch_register (tid, tdep->ppc_fpscr_regnum);
499 if (have_ptrace_getvrregs)
500 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
501 fetch_altivec_registers (tid);
502 if (tdep->ppc_ev0_upper_regnum >= 0)
503 fetch_spe_register (tid, -1);
506 /* Fetch registers from the child process. Fetch all registers if
507 regno == -1, otherwise fetch all general registers or all floating
508 point registers depending upon the value of regno. */
510 ppc_linux_fetch_inferior_registers (int regno)
512 /* Overload thread id onto process id */
513 int tid = TIDGET (inferior_ptid);
515 /* No thread id, just use process id */
517 tid = PIDGET (inferior_ptid);
520 fetch_ppc_registers (tid);
522 fetch_register (tid, regno);
525 /* Store one register. */
527 store_altivec_register (int tid, int regno)
532 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
533 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
535 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
540 have_ptrace_getvrregs = 0;
543 perror_with_name (_("Unable to fetch AltiVec register"));
546 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
547 long on the hardware. */
548 if (regno == (tdep->ppc_vrsave_regnum - 1))
549 offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
551 regcache_raw_collect (current_regcache, regno,
552 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
554 ret = ptrace (PTRACE_SETVRREGS, tid, 0, ®s);
556 perror_with_name (_("Unable to store AltiVec register"));
559 /* Assuming TID referrs to an SPE process, set the top halves of TID's
560 general-purpose registers and its SPE-specific registers to the
561 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
564 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
565 PTRACE_SETEVRREGS requests are supported is isolated here, and in
566 get_spe_registers. */
568 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
570 if (have_ptrace_getsetevrregs)
572 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
576 /* EIO means that the PTRACE_SETEVRREGS request isn't
577 supported; we fail silently, and don't try the call
580 have_ptrace_getsetevrregs = 0;
582 /* Anything else needs to be reported. */
583 perror_with_name (_("Unable to set SPE registers"));
588 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
589 If REGNO is -1, write the values of all the SPE-specific
592 store_spe_register (int tid, int regno)
594 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
595 struct gdb_evrregset_t evrregs;
597 gdb_assert (sizeof (evrregs.evr[0])
598 == register_size (current_gdbarch, tdep->ppc_ev0_upper_regnum));
599 gdb_assert (sizeof (evrregs.acc)
600 == register_size (current_gdbarch, tdep->ppc_acc_regnum));
601 gdb_assert (sizeof (evrregs.spefscr)
602 == register_size (current_gdbarch, tdep->ppc_spefscr_regnum));
605 /* Since we're going to write out every register, the code below
606 should store to every field of evrregs; if that doesn't happen,
607 make it obvious by initializing it with suspicious values. */
608 memset (&evrregs, 42, sizeof (evrregs));
610 /* We can only read and write the entire EVR register set at a
611 time, so to write just a single register, we do a
612 read-modify-write maneuver. */
613 get_spe_registers (tid, &evrregs);
619 for (i = 0; i < ppc_num_gprs; i++)
620 regcache_raw_collect (current_regcache,
621 tdep->ppc_ev0_upper_regnum + i,
624 else if (tdep->ppc_ev0_upper_regnum <= regno
625 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
626 regcache_raw_collect (current_regcache, regno,
627 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
630 || regno == tdep->ppc_acc_regnum)
631 regcache_raw_collect (current_regcache,
632 tdep->ppc_acc_regnum,
636 || regno == tdep->ppc_spefscr_regnum)
637 regcache_raw_collect (current_regcache,
638 tdep->ppc_spefscr_regnum,
641 /* Write back the modified register set. */
642 set_spe_registers (tid, &evrregs);
646 store_register (int tid, int regno)
648 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
649 /* This isn't really an address. But ptrace thinks of it as one. */
650 CORE_ADDR regaddr = ppc_register_u_addr (regno);
652 size_t bytes_to_transfer;
653 char buf[MAX_REGISTER_SIZE];
655 if (altivec_register_p (regno))
657 store_altivec_register (tid, regno);
660 else if (spe_register_p (regno))
662 store_spe_register (tid, regno);
669 /* First collect the register. Keep in mind that the regcache's
670 idea of the register's size may not be a multiple of sizeof
671 (PTRACE_XFER_TYPE). */
672 memset (buf, 0, sizeof buf);
673 bytes_to_transfer = align_up (register_size (current_gdbarch, regno),
674 sizeof (PTRACE_XFER_TYPE));
675 if (TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
677 /* Little-endian values always sit at the left end of the buffer. */
678 regcache_raw_collect (current_regcache, regno, buf);
680 else if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
682 /* Big-endian values sit at the right end of the buffer. */
683 size_t padding = (bytes_to_transfer
684 - register_size (current_gdbarch, regno));
685 regcache_raw_collect (current_regcache, regno, buf + padding);
688 for (i = 0; i < bytes_to_transfer; i += sizeof (PTRACE_XFER_TYPE))
691 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
692 *(PTRACE_XFER_TYPE *) & buf[i]);
693 regaddr += sizeof (PTRACE_XFER_TYPE);
696 && regno == tdep->ppc_fpscr_regnum)
698 /* Some older kernel versions don't allow fpscr to be written. */
705 sprintf (message, "writing register %s (#%d)",
706 REGISTER_NAME (regno), regno);
707 perror_with_name (message);
713 fill_vrregset (gdb_vrregset_t *vrregsetp)
716 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
717 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
718 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
719 int offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
721 for (i = 0; i < num_of_vrregs; i++)
723 /* The last 2 registers of this set are only 32 bit long, not
724 128, but only VSCR is fetched as a 16 bytes quantity. */
725 if (i == (num_of_vrregs - 2))
726 regcache_raw_collect (current_regcache, tdep->ppc_vr0_regnum + i,
727 *vrregsetp + i * vrregsize + offset);
729 regcache_raw_collect (current_regcache, tdep->ppc_vr0_regnum + i,
730 *vrregsetp + i * vrregsize);
735 store_altivec_registers (int tid)
740 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
745 have_ptrace_getvrregs = 0;
748 perror_with_name (_("Couldn't get AltiVec registers"));
751 fill_vrregset (®s);
753 if (ptrace (PTRACE_SETVRREGS, tid, 0, ®s) < 0)
754 perror_with_name (_("Couldn't write AltiVec registers"));
758 store_ppc_registers (int tid)
761 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
763 for (i = 0; i < ppc_num_gprs; i++)
764 store_register (tid, tdep->ppc_gp0_regnum + i);
765 if (tdep->ppc_fp0_regnum >= 0)
766 for (i = 0; i < ppc_num_fprs; i++)
767 store_register (tid, tdep->ppc_fp0_regnum + i);
768 store_register (tid, PC_REGNUM);
769 if (tdep->ppc_ps_regnum != -1)
770 store_register (tid, tdep->ppc_ps_regnum);
771 if (tdep->ppc_cr_regnum != -1)
772 store_register (tid, tdep->ppc_cr_regnum);
773 if (tdep->ppc_lr_regnum != -1)
774 store_register (tid, tdep->ppc_lr_regnum);
775 if (tdep->ppc_ctr_regnum != -1)
776 store_register (tid, tdep->ppc_ctr_regnum);
777 if (tdep->ppc_xer_regnum != -1)
778 store_register (tid, tdep->ppc_xer_regnum);
779 if (tdep->ppc_mq_regnum != -1)
780 store_register (tid, tdep->ppc_mq_regnum);
781 if (tdep->ppc_fpscr_regnum != -1)
782 store_register (tid, tdep->ppc_fpscr_regnum);
783 if (have_ptrace_getvrregs)
784 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
785 store_altivec_registers (tid);
786 if (tdep->ppc_ev0_upper_regnum >= 0)
787 store_spe_register (tid, -1);
791 ppc_linux_check_watch_resources (int type, int cnt, int ot)
794 ptid_t ptid = inferior_ptid;
796 /* DABR (data address breakpoint register) is optional for PPC variants.
797 Some variants have one DABR, others have none. So CNT can't be larger
802 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
803 the target has DABR. If either answer is no, the ptrace call will
804 return -1. Fail in that case. */
809 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
815 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
817 /* Handle sub-8-byte quantities. */
821 /* addr+len must fall in the 8 byte watchable region. */
822 if ((addr + len) > (addr & ~7) + 8)
828 /* Set a watchpoint of type TYPE at address ADDR. */
830 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
834 ptid_t ptid = inferior_ptid;
836 dabr_value = addr & ~7;
840 /* Set read and translate bits. */
844 /* Set write and translate bits. */
848 /* Set read, write and translate bits. */
857 return ptrace (PTRACE_SET_DEBUGREG, tid, 0, dabr_value);
861 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len)
864 ptid_t ptid = inferior_ptid;
870 return ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0);
874 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
876 if (last_stopped_data_address)
878 *addr_p = last_stopped_data_address;
879 last_stopped_data_address = 0;
886 ppc_linux_stopped_by_watchpoint (void)
889 struct siginfo siginfo;
890 ptid_t ptid = inferior_ptid;
898 ptrace (PTRACE_GETSIGINFO, tid, (PTRACE_TYPE_ARG3) 0, &siginfo);
900 if (errno != 0 || siginfo.si_signo != SIGTRAP ||
901 (siginfo.si_code & 0xffff) != 0x0004)
904 last_stopped_data_address = (CORE_ADDR) siginfo.si_addr;
909 ppc_linux_store_inferior_registers (int regno)
911 /* Overload thread id onto process id */
912 int tid = TIDGET (inferior_ptid);
914 /* No thread id, just use process id */
916 tid = PIDGET (inferior_ptid);
919 store_register (tid, regno);
921 store_ppc_registers (tid);
925 supply_gregset (gdb_gregset_t *gregsetp)
927 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
928 interface, and not the wordsize of the program's ABI. */
929 int wordsize = sizeof (PTRACE_XFER_TYPE);
930 ppc_linux_supply_gregset (current_regcache, -1, gregsetp,
931 sizeof (gdb_gregset_t), wordsize);
935 right_fill_reg (int regnum, void *reg)
937 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
938 interface, and not the wordsize of the program's ABI. */
939 int wordsize = sizeof (PTRACE_XFER_TYPE);
940 /* Right fill the register. */
941 regcache_raw_collect (current_regcache, regnum,
944 - register_size (current_gdbarch, regnum)));
948 fill_gregset (gdb_gregset_t *gregsetp, int regno)
951 elf_greg_t *regp = (elf_greg_t *) gregsetp;
952 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
953 const int elf_ngreg = 48;
956 /* Start with zeros. */
957 memset (regp, 0, elf_ngreg * sizeof (*regp));
959 for (regi = 0; regi < ppc_num_gprs; regi++)
961 if ((regno == -1) || regno == tdep->ppc_gp0_regnum + regi)
962 right_fill_reg (tdep->ppc_gp0_regnum + regi, (regp + PT_R0 + regi));
965 if ((regno == -1) || regno == PC_REGNUM)
966 right_fill_reg (PC_REGNUM, regp + PT_NIP);
967 if ((regno == -1) || regno == tdep->ppc_lr_regnum)
968 right_fill_reg (tdep->ppc_lr_regnum, regp + PT_LNK);
969 if ((regno == -1) || regno == tdep->ppc_cr_regnum)
970 regcache_raw_collect (current_regcache, tdep->ppc_cr_regnum,
972 if ((regno == -1) || regno == tdep->ppc_xer_regnum)
973 regcache_raw_collect (current_regcache, tdep->ppc_xer_regnum,
975 if ((regno == -1) || regno == tdep->ppc_ctr_regnum)
976 right_fill_reg (tdep->ppc_ctr_regnum, regp + PT_CTR);
978 if (((regno == -1) || regno == tdep->ppc_mq_regnum)
979 && (tdep->ppc_mq_regnum != -1))
980 right_fill_reg (tdep->ppc_mq_regnum, regp + PT_MQ);
982 if ((regno == -1) || regno == tdep->ppc_ps_regnum)
983 right_fill_reg (tdep->ppc_ps_regnum, regp + PT_MSR);
987 supply_fpregset (gdb_fpregset_t * fpregsetp)
989 ppc_linux_supply_fpregset (NULL, current_regcache, -1, fpregsetp,
990 sizeof (gdb_fpregset_t));
993 /* Given a pointer to a floating point register set in /proc format
994 (fpregset_t *), update the register specified by REGNO from gdb's
995 idea of the current floating point register set. If REGNO is -1,
998 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
1001 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1002 bfd_byte *fpp = (void *) fpregsetp;
1004 if (ppc_floating_point_unit_p (current_gdbarch))
1006 for (regi = 0; regi < ppc_num_fprs; regi++)
1008 if ((regno == -1) || (regno == tdep->ppc_fp0_regnum + regi))
1009 regcache_raw_collect (current_regcache, tdep->ppc_fp0_regnum + regi,
1012 if (regno == -1 || regno == tdep->ppc_fpscr_regnum)
1013 right_fill_reg (tdep->ppc_fpscr_regnum, (fpp + 8 * 32));
1017 void _initialize_ppc_linux_nat (void);
1020 _initialize_ppc_linux_nat (void)
1022 struct target_ops *t;
1024 /* Fill in the generic GNU/Linux methods. */
1025 t = linux_target ();
1027 /* Add our register access methods. */
1028 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
1029 t->to_store_registers = ppc_linux_store_inferior_registers;
1031 /* Add our watchpoint methods. */
1032 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
1033 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
1034 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
1035 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
1036 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
1037 t->to_stopped_data_address = ppc_linux_stopped_data_address;
1039 /* Register the target. */