[PATCH] Unexpected EOF read from connection with GDB after inferior exits.
[deliverable/binutils-gdb.git] / gdb / ppc-linux-nat.c
CommitLineData
9abe5450 1/* PPC GNU/Linux native support.
2555fe1a 2
6aba47ca 3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
c877c8e6
KB
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c877c8e6
KB
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c877c8e6
KB
20
21#include "defs.h"
e162d11b 22#include "gdb_string.h"
6ffbb7ab 23#include "observer.h"
c877c8e6
KB
24#include "frame.h"
25#include "inferior.h"
6ffbb7ab 26#include "gdbthread.h"
c877c8e6 27#include "gdbcore.h"
4e052eda 28#include "regcache.h"
383f0f5b 29#include "gdb_assert.h"
10d6c8cd
DJ
30#include "target.h"
31#include "linux-nat.h"
c877c8e6 32
411cb3f9 33#include <stdint.h>
c877c8e6
KB
34#include <sys/types.h>
35#include <sys/param.h>
36#include <signal.h>
37#include <sys/user.h>
38#include <sys/ioctl.h>
2555fe1a 39#include "gdb_wait.h"
c877c8e6
KB
40#include <fcntl.h>
41#include <sys/procfs.h>
45229ea4 42#include <sys/ptrace.h>
c877c8e6 43
c60c0f5f
MS
44/* Prototypes for supply_gregset etc. */
45#include "gregset.h"
16333c4f 46#include "ppc-tdep.h"
7284e1be
UW
47#include "ppc-linux-tdep.h"
48
b7622095
LM
49/* Required when using the AUXV. */
50#include "elf/common.h"
51#include "auxv.h"
52
7284e1be
UW
53/* This sometimes isn't defined. */
54#ifndef PT_ORIG_R3
55#define PT_ORIG_R3 34
56#endif
57#ifndef PT_TRAP
58#define PT_TRAP 40
59#endif
c60c0f5f 60
69abc51c
TJB
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
64 entry of the AUXV. */
f4d9bade
UW
65#ifndef PPC_FEATURE_CELL
66#define PPC_FEATURE_CELL 0x00010000
67#endif
b7622095
LM
68#ifndef PPC_FEATURE_BOOKE
69#define PPC_FEATURE_BOOKE 0x00008000
70#endif
f04c6d38
TJB
71#ifndef PPC_FEATURE_HAS_DFP
72#define PPC_FEATURE_HAS_DFP 0x00000400 /* Decimal Floating Point. */
69abc51c 73#endif
b7622095 74
9abe5450
EZ
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
82 much useless. */
83
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
89#endif
90
604c2f83
LM
91/* PTRACE requests for POWER7 VSX registers. */
92#ifndef PTRACE_GETVSXREGS
93#define PTRACE_GETVSXREGS 27
94#define PTRACE_SETVSXREGS 28
95#endif
01904826
JB
96
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
103#endif
104
6ffbb7ab
TJB
105/* Similarly for the hardware watchpoint support. These requests are used
106 when the BookE kernel interface is not available. */
e0d24f8d
WZ
107#ifndef PTRACE_GET_DEBUGREG
108#define PTRACE_GET_DEBUGREG 25
109#endif
110#ifndef PTRACE_SET_DEBUGREG
111#define PTRACE_SET_DEBUGREG 26
112#endif
113#ifndef PTRACE_GETSIGINFO
114#define PTRACE_GETSIGINFO 0x4202
115#endif
01904826 116
6ffbb7ab
TJB
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
120 evaluation. */
121#ifndef PPC_PTRACE_GETHWDBGINFO
122
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
129
130struct ppc_debug_info
131{
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 */
138 uint64_t features;
139};
140
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
146
147struct ppc_hw_breakpoint
148{
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 */
156};
157
158/* Trigger type. */
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
163
164/* Address mode. */
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
169
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 */
181
182
183
1dfe79e8
SDJ
184/* Similarly for the general-purpose (gp0 -- gp31)
185 and floating-point registers (fp0 -- fp31). */
186#ifndef PTRACE_GETREGS
187#define PTRACE_GETREGS 12
188#endif
189#ifndef PTRACE_SETREGS
190#define PTRACE_SETREGS 13
191#endif
192#ifndef PTRACE_GETFPREGS
193#define PTRACE_GETFPREGS 14
194#endif
195#ifndef PTRACE_SETFPREGS
196#define PTRACE_SETFPREGS 15
197#endif
198
9abe5450
EZ
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.
204
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.
211
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.
215
216 The layout is like this (where x is the actual value of the vscr reg): */
217
218/* *INDENT-OFF* */
219/*
220 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
221 <-------> <-------><-------><->
222 VR0 VR31 VSCR VRSAVE
223*/
224/* *INDENT-ON* */
225
226#define SIZEOF_VRREGS 33*16+4
227
228typedef char gdb_vrregset_t[SIZEOF_VRREGS];
229
604c2f83
LM
230/* This is the layout of the POWER7 VSX registers and the way they overlap
231 with the existing FPR and VMX registers.
232
233 VSR doubleword 0 VSR doubleword 1
234 ----------------------------------------------------------------
235 VSR[0] | FPR[0] | |
236 ----------------------------------------------------------------
237 VSR[1] | FPR[1] | |
238 ----------------------------------------------------------------
239 | ... | |
240 | ... | |
241 ----------------------------------------------------------------
242 VSR[30] | FPR[30] | |
243 ----------------------------------------------------------------
244 VSR[31] | FPR[31] | |
245 ----------------------------------------------------------------
246 VSR[32] | VR[0] |
247 ----------------------------------------------------------------
248 VSR[33] | VR[1] |
249 ----------------------------------------------------------------
250 | ... |
251 | ... |
252 ----------------------------------------------------------------
253 VSR[62] | VR[30] |
254 ----------------------------------------------------------------
255 VSR[63] | VR[31] |
256 ----------------------------------------------------------------
257
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
261 registers. */
262#define SIZEOF_VSXREGS 32*8
263
264typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
01904826
JB
265
266/* On PPC processors that support the the Signal Processing Extension
267 (SPE) APU, the general-purpose registers are 64 bits long.
411cb3f9
PG
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.
01904826
JB
274
275 GDB itself continues to claim the general-purpose registers are 32
6ced10dd
JB
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.
01904826
JB
280
281 This is the structure filled in by PTRACE_GETEVRREGS and written to
282 the inferior's registers by PTRACE_SETEVRREGS. */
283struct gdb_evrregset_t
284{
285 unsigned long evr[32];
286 unsigned long long acc;
287 unsigned long spefscr;
288};
289
604c2f83
LM
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. */
295int have_ptrace_getsetvsxregs = 1;
01904826
JB
296
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
300 error. */
9abe5450
EZ
301int have_ptrace_getvrregs = 1;
302
01904826
JB
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
306 error. */
307int have_ptrace_getsetevrregs = 1;
308
1dfe79e8
SDJ
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. */
313int have_ptrace_getsetregs = 1;
314
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. */
319int have_ptrace_getsetfpregs = 1;
320
16333c4f
EZ
321/* *INDENT-OFF* */
322/* registers layout, as presented by the ptrace interface:
323PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
324PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
325PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
326PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
327PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
328PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
329PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
330PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
331PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
332/* *INDENT_ON * */
c877c8e6 333
45229ea4 334static int
e101270f 335ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
c877c8e6 336{
16333c4f 337 int u_addr = -1;
e101270f 338 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
56d0d96a
AC
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. */
411cb3f9 341 int wordsize = sizeof (long);
16333c4f
EZ
342
343 /* General purpose registers occupy 1 slot each in the buffer */
8bf659e8
JB
344 if (regno >= tdep->ppc_gp0_regnum
345 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
26e75e5c 346 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
16333c4f 347
49ff75ad
JB
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. */
383f0f5b
JB
351 if (tdep->ppc_fp0_regnum >= 0
352 && regno >= tdep->ppc_fp0_regnum
366f009f
JB
353 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
354 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
16333c4f
EZ
355
356 /* UISA special purpose registers: 1 slot each */
e101270f 357 if (regno == gdbarch_pc_regnum (gdbarch))
49ff75ad 358 u_addr = PT_NIP * wordsize;
dc5cfeb6 359 if (regno == tdep->ppc_lr_regnum)
49ff75ad 360 u_addr = PT_LNK * wordsize;
dc5cfeb6 361 if (regno == tdep->ppc_cr_regnum)
49ff75ad 362 u_addr = PT_CCR * wordsize;
dc5cfeb6 363 if (regno == tdep->ppc_xer_regnum)
49ff75ad 364 u_addr = PT_XER * wordsize;
dc5cfeb6 365 if (regno == tdep->ppc_ctr_regnum)
49ff75ad 366 u_addr = PT_CTR * wordsize;
f8c59253 367#ifdef PT_MQ
dc5cfeb6 368 if (regno == tdep->ppc_mq_regnum)
49ff75ad 369 u_addr = PT_MQ * wordsize;
f8c59253 370#endif
dc5cfeb6 371 if (regno == tdep->ppc_ps_regnum)
49ff75ad 372 u_addr = PT_MSR * wordsize;
7284e1be
UW
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;
383f0f5b
JB
377 if (tdep->ppc_fpscr_regnum >= 0
378 && regno == tdep->ppc_fpscr_regnum)
8f135812
AC
379 {
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
69abc51c 384 the second half of such a slot-pair (hence +1). For 64-bit,
8f135812
AC
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;
69abc51c
TJB
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;
8f135812
AC
395 else
396 u_addr = PT_FPSCR * wordsize;
397 }
16333c4f 398 return u_addr;
c877c8e6
KB
399}
400
604c2f83
LM
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. */
404static void
405fetch_vsx_register (struct regcache *regcache, int tid, int regno)
406{
407 int ret;
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);
412
413 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
414 if (ret < 0)
415 {
416 if (errno == EIO)
417 {
418 have_ptrace_getsetvsxregs = 0;
419 return;
420 }
421 perror_with_name (_("Unable to fetch VSX register"));
422 }
423
424 regcache_raw_supply (regcache, regno,
425 regs + (regno - tdep->ppc_vsr0_upper_regnum)
426 * vsxregsize);
427}
428
9abe5450
EZ
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. */
432static void
56be3814 433fetch_altivec_register (struct regcache *regcache, int tid, int regno)
9abe5450
EZ
434{
435 int ret;
436 int offset = 0;
437 gdb_vrregset_t regs;
40a6adc1
MD
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);
9abe5450
EZ
441
442 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
443 if (ret < 0)
444 {
445 if (errno == EIO)
446 {
447 have_ptrace_getvrregs = 0;
448 return;
449 }
e2e0b3e5 450 perror_with_name (_("Unable to fetch AltiVec register"));
9abe5450
EZ
451 }
452
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))
40a6adc1 458 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450 459
56be3814 460 regcache_raw_supply (regcache, regno,
23a6d369 461 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
9abe5450
EZ
462}
463
01904826
JB
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
467 zeros.
468
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. */
472static void
473get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
474{
475 if (have_ptrace_getsetevrregs)
476 {
477 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
478 return;
479 else
480 {
481 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
482 we just return zeros. */
483 if (errno == EIO)
484 have_ptrace_getsetevrregs = 0;
485 else
486 /* Anything else needs to be reported. */
e2e0b3e5 487 perror_with_name (_("Unable to fetch SPE registers"));
01904826
JB
488 }
489 }
490
491 memset (evrregset, 0, sizeof (*evrregset));
492}
493
6ced10dd
JB
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. */
01904826 498static void
56be3814 499fetch_spe_register (struct regcache *regcache, int tid, int regno)
01904826 500{
40a6adc1
MD
501 struct gdbarch *gdbarch = get_regcache_arch (regcache);
502 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01904826
JB
503 struct gdb_evrregset_t evrregs;
504
6ced10dd 505 gdb_assert (sizeof (evrregs.evr[0])
40a6adc1 506 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
6ced10dd 507 gdb_assert (sizeof (evrregs.acc)
40a6adc1 508 == register_size (gdbarch, tdep->ppc_acc_regnum));
6ced10dd 509 gdb_assert (sizeof (evrregs.spefscr)
40a6adc1 510 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
6ced10dd 511
01904826
JB
512 get_spe_registers (tid, &evrregs);
513
6ced10dd 514 if (regno == -1)
01904826 515 {
6ced10dd
JB
516 int i;
517
518 for (i = 0; i < ppc_num_gprs; i++)
56be3814 519 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
6ced10dd 520 &evrregs.evr[i]);
01904826 521 }
6ced10dd
JB
522 else if (tdep->ppc_ev0_upper_regnum <= regno
523 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
56be3814 524 regcache_raw_supply (regcache, regno,
6ced10dd
JB
525 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
526
527 if (regno == -1
528 || regno == tdep->ppc_acc_regnum)
56be3814 529 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
6ced10dd
JB
530
531 if (regno == -1
532 || regno == tdep->ppc_spefscr_regnum)
56be3814 533 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
6ced10dd 534 &evrregs.spefscr);
01904826
JB
535}
536
45229ea4 537static void
56be3814 538fetch_register (struct regcache *regcache, int tid, int regno)
45229ea4 539{
40a6adc1
MD
540 struct gdbarch *gdbarch = get_regcache_arch (regcache);
541 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
45229ea4 542 /* This isn't really an address. But ptrace thinks of it as one. */
e101270f 543 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
4a19ea35 544 int bytes_transferred;
45229ea4 545 unsigned int offset; /* Offset of registers within the u area. */
d9d9c31f 546 char buf[MAX_REGISTER_SIZE];
45229ea4 547
be8626e0 548 if (altivec_register_p (gdbarch, regno))
9abe5450
EZ
549 {
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
553 register. */
554 if (have_ptrace_getvrregs)
555 {
56be3814 556 fetch_altivec_register (regcache, tid, regno);
9abe5450
EZ
557 return;
558 }
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. */
562 }
604c2f83
LM
563 if (vsx_register_p (gdbarch, regno))
564 {
565 if (have_ptrace_getsetvsxregs)
566 {
567 fetch_vsx_register (regcache, tid, regno);
568 return;
569 }
570 }
be8626e0 571 else if (spe_register_p (gdbarch, regno))
01904826 572 {
56be3814 573 fetch_spe_register (regcache, tid, regno);
01904826
JB
574 return;
575 }
9abe5450 576
45229ea4
EZ
577 if (regaddr == -1)
578 {
40a6adc1 579 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
56be3814 580 regcache_raw_supply (regcache, regno, buf);
45229ea4
EZ
581 return;
582 }
583
411cb3f9 584 /* Read the raw register using sizeof(long) sized chunks. On a
56d0d96a
AC
585 32-bit platform, 64-bit floating-point registers will require two
586 transfers. */
4a19ea35 587 for (bytes_transferred = 0;
40a6adc1 588 bytes_transferred < register_size (gdbarch, regno);
411cb3f9 589 bytes_transferred += sizeof (long))
45229ea4
EZ
590 {
591 errno = 0;
411cb3f9
PG
592 *(long *) &buf[bytes_transferred]
593 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
594 regaddr += sizeof (long);
45229ea4
EZ
595 if (errno != 0)
596 {
bc97b3ba
JB
597 char message[128];
598 sprintf (message, "reading register %s (#%d)",
40a6adc1 599 gdbarch_register_name (gdbarch, regno), regno);
bc97b3ba 600 perror_with_name (message);
45229ea4
EZ
601 }
602 }
56d0d96a 603
4a19ea35
JB
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
411cb3f9 606 (long). */
40a6adc1 607 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
4a19ea35
JB
608 {
609 /* Little-endian values are always found at the left end of the
610 bytes transferred. */
56be3814 611 regcache_raw_supply (regcache, regno, buf);
4a19ea35 612 }
40a6adc1 613 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4a19ea35
JB
614 {
615 /* Big-endian values are found at the right end of the bytes
616 transferred. */
40a6adc1 617 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
56be3814 618 regcache_raw_supply (regcache, regno, buf + padding);
4a19ea35
JB
619 }
620 else
a44bddec 621 internal_error (__FILE__, __LINE__,
e2e0b3e5 622 _("fetch_register: unexpected byte order: %d"),
40a6adc1 623 gdbarch_byte_order (gdbarch));
45229ea4
EZ
624}
625
604c2f83
LM
626static void
627supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
628{
629 int i;
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);
633
634 for (i = 0; i < ppc_num_vshrs; i++)
635 {
636 regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
637 *vsxregsetp + i * vsxregsize);
638 }
639}
640
9abe5450 641static void
56be3814 642supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
9abe5450
EZ
643{
644 int i;
40a6adc1
MD
645 struct gdbarch *gdbarch = get_regcache_arch (regcache);
646 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 647 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
40a6adc1
MD
648 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
649 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450
EZ
650
651 for (i = 0; i < num_of_vrregs; i++)
652 {
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
656 slot. */
657 if (i == (num_of_vrregs - 2))
56be3814 658 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
23a6d369 659 *vrregsetp + i * vrregsize + offset);
9abe5450 660 else
56be3814 661 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
23a6d369 662 *vrregsetp + i * vrregsize);
9abe5450
EZ
663 }
664}
665
604c2f83
LM
666static void
667fetch_vsx_registers (struct regcache *regcache, int tid)
668{
669 int ret;
670 gdb_vsxregset_t regs;
671
672 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
673 if (ret < 0)
674 {
675 if (errno == EIO)
676 {
677 have_ptrace_getsetvsxregs = 0;
678 return;
679 }
680 perror_with_name (_("Unable to fetch VSX registers"));
681 }
682 supply_vsxregset (regcache, &regs);
683}
684
9abe5450 685static void
56be3814 686fetch_altivec_registers (struct regcache *regcache, int tid)
9abe5450
EZ
687{
688 int ret;
689 gdb_vrregset_t regs;
690
691 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
692 if (ret < 0)
693 {
694 if (errno == EIO)
695 {
696 have_ptrace_getvrregs = 0;
697 return;
698 }
e2e0b3e5 699 perror_with_name (_("Unable to fetch AltiVec registers"));
9abe5450 700 }
56be3814 701 supply_vrregset (regcache, &regs);
9abe5450
EZ
702}
703
1dfe79e8
SDJ
704/* This function actually issues the request to ptrace, telling
705 it to get all general-purpose registers and put them into the
706 specified regset.
707
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. */
712static int
713fetch_all_gp_regs (struct regcache *regcache, int tid)
714{
715 struct gdbarch *gdbarch = get_regcache_arch (regcache);
716 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
717 gdb_gregset_t gregset;
718
719 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
720 {
721 if (errno == EIO)
722 {
723 have_ptrace_getsetregs = 0;
724 return 0;
725 }
726 perror_with_name (_("Couldn't get general-purpose registers."));
727 }
728
729 supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
730
731 return 1;
732}
733
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. */
740static void
741fetch_gp_regs (struct regcache *regcache, int tid)
742{
743 struct gdbarch *gdbarch = get_regcache_arch (regcache);
744 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
745 int i;
746
747 if (have_ptrace_getsetregs)
748 if (fetch_all_gp_regs (regcache, tid))
749 return;
750
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);
756}
757
758/* This function actually issues the request to ptrace, telling
759 it to get all floating-point registers and put them into the
760 specified regset.
761
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. */
766static int
767fetch_all_fp_regs (struct regcache *regcache, int tid)
768{
769 gdb_fpregset_t fpregs;
770
771 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
772 {
773 if (errno == EIO)
774 {
775 have_ptrace_getsetfpregs = 0;
776 return 0;
777 }
778 perror_with_name (_("Couldn't get floating-point registers."));
779 }
780
781 supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
782
783 return 1;
784}
785
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. */
792static void
793fetch_fp_regs (struct regcache *regcache, int tid)
794{
795 struct gdbarch *gdbarch = get_regcache_arch (regcache);
796 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
797 int i;
798
799 if (have_ptrace_getsetfpregs)
800 if (fetch_all_fp_regs (regcache, tid))
801 return;
802
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);
808}
809
45229ea4 810static void
56be3814 811fetch_ppc_registers (struct regcache *regcache, int tid)
45229ea4
EZ
812{
813 int i;
40a6adc1
MD
814 struct gdbarch *gdbarch = get_regcache_arch (regcache);
815 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 816
1dfe79e8 817 fetch_gp_regs (regcache, tid);
32b99774 818 if (tdep->ppc_fp0_regnum >= 0)
1dfe79e8 819 fetch_fp_regs (regcache, tid);
40a6adc1 820 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
32b99774 821 if (tdep->ppc_ps_regnum != -1)
56be3814 822 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
32b99774 823 if (tdep->ppc_cr_regnum != -1)
56be3814 824 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
32b99774 825 if (tdep->ppc_lr_regnum != -1)
56be3814 826 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
32b99774 827 if (tdep->ppc_ctr_regnum != -1)
56be3814 828 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
32b99774 829 if (tdep->ppc_xer_regnum != -1)
56be3814 830 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
e3f36dbd 831 if (tdep->ppc_mq_regnum != -1)
56be3814 832 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
7284e1be
UW
833 if (ppc_linux_trap_reg_p (gdbarch))
834 {
835 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
836 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
837 }
32b99774 838 if (tdep->ppc_fpscr_regnum != -1)
56be3814 839 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
9abe5450
EZ
840 if (have_ptrace_getvrregs)
841 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
56be3814 842 fetch_altivec_registers (regcache, tid);
604c2f83
LM
843 if (have_ptrace_getsetvsxregs)
844 if (tdep->ppc_vsr0_upper_regnum != -1)
845 fetch_vsx_registers (regcache, tid);
6ced10dd 846 if (tdep->ppc_ev0_upper_regnum >= 0)
56be3814 847 fetch_spe_register (regcache, tid, -1);
45229ea4
EZ
848}
849
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. */
10d6c8cd 853static void
28439f5e
PA
854ppc_linux_fetch_inferior_registers (struct target_ops *ops,
855 struct regcache *regcache, int regno)
45229ea4 856{
9abe5450 857 /* Overload thread id onto process id */
05f13b9c
EZ
858 int tid = TIDGET (inferior_ptid);
859
860 /* No thread id, just use process id */
861 if (tid == 0)
862 tid = PIDGET (inferior_ptid);
863
9abe5450 864 if (regno == -1)
56be3814 865 fetch_ppc_registers (regcache, tid);
45229ea4 866 else
56be3814 867 fetch_register (regcache, tid, regno);
45229ea4
EZ
868}
869
604c2f83
LM
870/* Store one VSX register. */
871static void
872store_vsx_register (const struct regcache *regcache, int tid, int regno)
873{
874 int ret;
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);
879
880 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
881 if (ret < 0)
882 {
883 if (errno == EIO)
884 {
885 have_ptrace_getsetvsxregs = 0;
886 return;
887 }
888 perror_with_name (_("Unable to fetch VSX register"));
889 }
890
891 regcache_raw_collect (regcache, regno, regs +
892 (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
893
894 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
895 if (ret < 0)
896 perror_with_name (_("Unable to store VSX register"));
897}
898
45229ea4 899/* Store one register. */
9abe5450 900static void
56be3814 901store_altivec_register (const struct regcache *regcache, int tid, int regno)
9abe5450
EZ
902{
903 int ret;
904 int offset = 0;
905 gdb_vrregset_t regs;
40a6adc1
MD
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);
9abe5450
EZ
909
910 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
911 if (ret < 0)
912 {
913 if (errno == EIO)
914 {
915 have_ptrace_getvrregs = 0;
916 return;
917 }
e2e0b3e5 918 perror_with_name (_("Unable to fetch AltiVec register"));
9abe5450
EZ
919 }
920
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))
40a6adc1 924 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450 925
56be3814 926 regcache_raw_collect (regcache, regno,
822c9732 927 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
9abe5450
EZ
928
929 ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
930 if (ret < 0)
e2e0b3e5 931 perror_with_name (_("Unable to store AltiVec register"));
9abe5450
EZ
932}
933
01904826
JB
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
937 nothing.
938
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. */
942static void
943set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
944{
945 if (have_ptrace_getsetevrregs)
946 {
947 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
948 return;
949 else
950 {
951 /* EIO means that the PTRACE_SETEVRREGS request isn't
952 supported; we fail silently, and don't try the call
953 again. */
954 if (errno == EIO)
955 have_ptrace_getsetevrregs = 0;
956 else
957 /* Anything else needs to be reported. */
e2e0b3e5 958 perror_with_name (_("Unable to set SPE registers"));
01904826
JB
959 }
960 }
961}
962
6ced10dd
JB
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
965 registers. */
01904826 966static void
56be3814 967store_spe_register (const struct regcache *regcache, int tid, int regno)
01904826 968{
40a6adc1
MD
969 struct gdbarch *gdbarch = get_regcache_arch (regcache);
970 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01904826
JB
971 struct gdb_evrregset_t evrregs;
972
6ced10dd 973 gdb_assert (sizeof (evrregs.evr[0])
40a6adc1 974 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
6ced10dd 975 gdb_assert (sizeof (evrregs.acc)
40a6adc1 976 == register_size (gdbarch, tdep->ppc_acc_regnum));
6ced10dd 977 gdb_assert (sizeof (evrregs.spefscr)
40a6adc1 978 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
01904826 979
6ced10dd
JB
980 if (regno == -1)
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));
985 else
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);
990
991 if (regno == -1)
01904826 992 {
6ced10dd
JB
993 int i;
994
995 for (i = 0; i < ppc_num_gprs; i++)
56be3814 996 regcache_raw_collect (regcache,
6ced10dd
JB
997 tdep->ppc_ev0_upper_regnum + i,
998 &evrregs.evr[i]);
01904826 999 }
6ced10dd
JB
1000 else if (tdep->ppc_ev0_upper_regnum <= regno
1001 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
56be3814 1002 regcache_raw_collect (regcache, regno,
6ced10dd
JB
1003 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
1004
1005 if (regno == -1
1006 || regno == tdep->ppc_acc_regnum)
56be3814 1007 regcache_raw_collect (regcache,
6ced10dd
JB
1008 tdep->ppc_acc_regnum,
1009 &evrregs.acc);
1010
1011 if (regno == -1
1012 || regno == tdep->ppc_spefscr_regnum)
56be3814 1013 regcache_raw_collect (regcache,
6ced10dd
JB
1014 tdep->ppc_spefscr_regnum,
1015 &evrregs.spefscr);
01904826
JB
1016
1017 /* Write back the modified register set. */
1018 set_spe_registers (tid, &evrregs);
1019}
1020
45229ea4 1021static void
56be3814 1022store_register (const struct regcache *regcache, int tid, int regno)
45229ea4 1023{
40a6adc1
MD
1024 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1025 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
45229ea4 1026 /* This isn't really an address. But ptrace thinks of it as one. */
e101270f 1027 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
52f0bd74 1028 int i;
4a19ea35 1029 size_t bytes_to_transfer;
d9d9c31f 1030 char buf[MAX_REGISTER_SIZE];
45229ea4 1031
be8626e0 1032 if (altivec_register_p (gdbarch, regno))
45229ea4 1033 {
56be3814 1034 store_altivec_register (regcache, tid, regno);
45229ea4
EZ
1035 return;
1036 }
604c2f83
LM
1037 if (vsx_register_p (gdbarch, regno))
1038 {
1039 store_vsx_register (regcache, tid, regno);
1040 return;
1041 }
be8626e0 1042 else if (spe_register_p (gdbarch, regno))
01904826 1043 {
56be3814 1044 store_spe_register (regcache, tid, regno);
01904826
JB
1045 return;
1046 }
45229ea4 1047
9abe5450
EZ
1048 if (regaddr == -1)
1049 return;
1050
4a19ea35
JB
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
411cb3f9 1053 (long). */
56d0d96a 1054 memset (buf, 0, sizeof buf);
40a6adc1
MD
1055 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
1056 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
4a19ea35
JB
1057 {
1058 /* Little-endian values always sit at the left end of the buffer. */
56be3814 1059 regcache_raw_collect (regcache, regno, buf);
4a19ea35 1060 }
40a6adc1 1061 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4a19ea35
JB
1062 {
1063 /* Big-endian values sit at the right end of the buffer. */
40a6adc1 1064 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
56be3814 1065 regcache_raw_collect (regcache, regno, buf + padding);
4a19ea35
JB
1066 }
1067
411cb3f9 1068 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
45229ea4
EZ
1069 {
1070 errno = 0;
411cb3f9
PG
1071 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
1072 *(long *) &buf[i]);
1073 regaddr += sizeof (long);
e3f36dbd
KB
1074
1075 if (errno == EIO
7284e1be
UW
1076 && (regno == tdep->ppc_fpscr_regnum
1077 || regno == PPC_ORIG_R3_REGNUM
1078 || regno == PPC_TRAP_REGNUM))
e3f36dbd 1079 {
7284e1be
UW
1080 /* Some older kernel versions don't allow fpscr, orig_r3
1081 or trap to be written. */
e3f36dbd
KB
1082 continue;
1083 }
1084
45229ea4
EZ
1085 if (errno != 0)
1086 {
bc97b3ba
JB
1087 char message[128];
1088 sprintf (message, "writing register %s (#%d)",
40a6adc1 1089 gdbarch_register_name (gdbarch, regno), regno);
bc97b3ba 1090 perror_with_name (message);
45229ea4
EZ
1091 }
1092 }
1093}
1094
604c2f83
LM
1095static void
1096fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
1097{
1098 int i;
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);
1102
1103 for (i = 0; i < ppc_num_vshrs; i++)
1104 regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
1105 *vsxregsetp + i * vsxregsize);
1106}
1107
9abe5450 1108static void
56be3814 1109fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
9abe5450
EZ
1110{
1111 int i;
40a6adc1
MD
1112 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1113 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 1114 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
40a6adc1
MD
1115 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
1116 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450
EZ
1117
1118 for (i = 0; i < num_of_vrregs; i++)
1119 {
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))
56be3814 1123 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
822c9732 1124 *vrregsetp + i * vrregsize + offset);
9abe5450 1125 else
56be3814 1126 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
822c9732 1127 *vrregsetp + i * vrregsize);
9abe5450
EZ
1128 }
1129}
1130
604c2f83
LM
1131static void
1132store_vsx_registers (const struct regcache *regcache, int tid)
1133{
1134 int ret;
1135 gdb_vsxregset_t regs;
1136
1137 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
1138 if (ret < 0)
1139 {
1140 if (errno == EIO)
1141 {
1142 have_ptrace_getsetvsxregs = 0;
1143 return;
1144 }
1145 perror_with_name (_("Couldn't get VSX registers"));
1146 }
1147
1148 fill_vsxregset (regcache, &regs);
1149
1150 if (ptrace (PTRACE_SETVSXREGS, tid, 0, &regs) < 0)
1151 perror_with_name (_("Couldn't write VSX registers"));
1152}
1153
9abe5450 1154static void
56be3814 1155store_altivec_registers (const struct regcache *regcache, int tid)
9abe5450
EZ
1156{
1157 int ret;
1158 gdb_vrregset_t regs;
1159
0897f59b 1160 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
9abe5450
EZ
1161 if (ret < 0)
1162 {
1163 if (errno == EIO)
1164 {
1165 have_ptrace_getvrregs = 0;
1166 return;
1167 }
e2e0b3e5 1168 perror_with_name (_("Couldn't get AltiVec registers"));
9abe5450
EZ
1169 }
1170
56be3814 1171 fill_vrregset (regcache, &regs);
9abe5450 1172
0897f59b 1173 if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
e2e0b3e5 1174 perror_with_name (_("Couldn't write AltiVec registers"));
9abe5450
EZ
1175}
1176
1dfe79e8
SDJ
1177/* This function actually issues the request to ptrace, telling
1178 it to store all general-purpose registers present in the specified
1179 regset.
1180
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. */
1185static int
1186store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1187{
1188 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1189 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1190 gdb_gregset_t gregset;
1191
1192 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1193 {
1194 if (errno == EIO)
1195 {
1196 have_ptrace_getsetregs = 0;
1197 return 0;
1198 }
1199 perror_with_name (_("Couldn't get general-purpose registers."));
1200 }
1201
1202 fill_gregset (regcache, &gregset, regno);
1203
1204 if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
1205 {
1206 if (errno == EIO)
1207 {
1208 have_ptrace_getsetregs = 0;
1209 return 0;
1210 }
1211 perror_with_name (_("Couldn't set general-purpose registers."));
1212 }
1213
1214 return 1;
1215}
1216
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. */
45229ea4 1223static void
1dfe79e8 1224store_gp_regs (const struct regcache *regcache, int tid, int regno)
45229ea4 1225{
40a6adc1
MD
1226 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1227 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1dfe79e8
SDJ
1228 int i;
1229
1230 if (have_ptrace_getsetregs)
1231 if (store_all_gp_regs (regcache, tid, regno))
1232 return;
1233
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". */
6ced10dd 1237 for (i = 0; i < ppc_num_gprs; i++)
56be3814 1238 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1dfe79e8
SDJ
1239}
1240
1241/* This function actually issues the request to ptrace, telling
1242 it to store all floating-point registers present in the specified
1243 regset.
1244
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. */
1249static int
1250store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
1251{
1252 gdb_fpregset_t fpregs;
1253
1254 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1255 {
1256 if (errno == EIO)
1257 {
1258 have_ptrace_getsetfpregs = 0;
1259 return 0;
1260 }
1261 perror_with_name (_("Couldn't get floating-point registers."));
1262 }
1263
1264 fill_fpregset (regcache, &fpregs, regno);
1265
1266 if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
1267 {
1268 if (errno == EIO)
1269 {
1270 have_ptrace_getsetfpregs = 0;
1271 return 0;
1272 }
1273 perror_with_name (_("Couldn't set floating-point registers."));
1274 }
1275
1276 return 1;
1277}
1278
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. */
1285static void
1286store_fp_regs (const struct regcache *regcache, int tid, int regno)
1287{
1288 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1289 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1290 int i;
1291
1292 if (have_ptrace_getsetfpregs)
1293 if (store_all_fp_regs (regcache, tid, regno))
1294 return;
1295
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);
1301}
1302
1303static void
1304store_ppc_registers (const struct regcache *regcache, int tid)
1305{
1306 int i;
1307 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1308 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1309
1310 store_gp_regs (regcache, tid, -1);
32b99774 1311 if (tdep->ppc_fp0_regnum >= 0)
1dfe79e8 1312 store_fp_regs (regcache, tid, -1);
40a6adc1 1313 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
32b99774 1314 if (tdep->ppc_ps_regnum != -1)
56be3814 1315 store_register (regcache, tid, tdep->ppc_ps_regnum);
32b99774 1316 if (tdep->ppc_cr_regnum != -1)
56be3814 1317 store_register (regcache, tid, tdep->ppc_cr_regnum);
32b99774 1318 if (tdep->ppc_lr_regnum != -1)
56be3814 1319 store_register (regcache, tid, tdep->ppc_lr_regnum);
32b99774 1320 if (tdep->ppc_ctr_regnum != -1)
56be3814 1321 store_register (regcache, tid, tdep->ppc_ctr_regnum);
32b99774 1322 if (tdep->ppc_xer_regnum != -1)
56be3814 1323 store_register (regcache, tid, tdep->ppc_xer_regnum);
e3f36dbd 1324 if (tdep->ppc_mq_regnum != -1)
56be3814 1325 store_register (regcache, tid, tdep->ppc_mq_regnum);
32b99774 1326 if (tdep->ppc_fpscr_regnum != -1)
56be3814 1327 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
7284e1be
UW
1328 if (ppc_linux_trap_reg_p (gdbarch))
1329 {
1330 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1331 store_register (regcache, tid, PPC_TRAP_REGNUM);
1332 }
9abe5450
EZ
1333 if (have_ptrace_getvrregs)
1334 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
56be3814 1335 store_altivec_registers (regcache, tid);
604c2f83
LM
1336 if (have_ptrace_getsetvsxregs)
1337 if (tdep->ppc_vsr0_upper_regnum != -1)
1338 store_vsx_registers (regcache, tid);
6ced10dd 1339 if (tdep->ppc_ev0_upper_regnum >= 0)
56be3814 1340 store_spe_register (regcache, tid, -1);
45229ea4
EZ
1341}
1342
6ffbb7ab
TJB
1343/* Fetch the AT_HWCAP entry from the aux vector. */
1344unsigned long ppc_linux_get_hwcap (void)
1345{
1346 CORE_ADDR field;
1347
1348 if (target_auxv_search (&current_target, AT_HWCAP, &field))
1349 return (unsigned long) field;
1350
1351 return 0;
1352}
1353
1354/* The cached DABR value, to install in new threads.
1355 This variable is used when we are dealing with non-BookE
1356 processors. */
1357static long saved_dabr_value;
1358
1359/* Global structure that will store information about the available
1360 features on this BookE processor. */
1361static struct ppc_debug_info booke_debug_info;
1362
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. */
1365static size_t max_slots_number = 0;
1366
1367struct hw_break_tuple
1368{
1369 long slot;
1370 struct ppc_hw_breakpoint *hw_break;
1371};
1372
1373/* This is an internal VEC created to store information about *points inserted
1374 for each thread. This is used for BookE processors. */
1375typedef struct thread_points
1376 {
1377 /* The TID to which this *point relates. */
1378 int tid;
1379 /* Information about the *point, such as its address, type, etc.
1380
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;
1386 } *thread_points_p;
1387DEF_VEC_P (thread_points_p);
1388
1389VEC(thread_points_p) *ppc_threads = NULL;
1390
1391/* The version of the kernel interface that we will use if the processor is
1392 BookE. */
1393#define PPC_DEBUG_CURRENT_VERSION 1
1394
1395/* Returns non-zero if we support the ptrace interface which enables
1396 booke debugging resources. */
e0d24f8d 1397static int
6ffbb7ab 1398have_ptrace_booke_interface (void)
e0d24f8d 1399{
6ffbb7ab 1400 static int have_ptrace_booke_interface = -1;
e0d24f8d 1401
6ffbb7ab
TJB
1402 if (have_ptrace_booke_interface == -1)
1403 {
1404 int tid;
e0d24f8d 1405
6ffbb7ab
TJB
1406 tid = TIDGET (inferior_ptid);
1407 if (tid == 0)
1408 tid = PIDGET (inferior_ptid);
e0d24f8d 1409
6ffbb7ab
TJB
1410 /* Check for kernel support for BOOKE debug registers. */
1411 if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &booke_debug_info) >= 0)
1412 {
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;
1416 }
1417 else
1418 {
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));
1422 }
1423 }
1424
1425 return have_ptrace_booke_interface;
e0d24f8d
WZ
1426}
1427
6ffbb7ab
TJB
1428static int
1429ppc_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
b7622095 1430{
6ffbb7ab 1431 int total_hw_wp, total_hw_bp;
b7622095 1432
6ffbb7ab
TJB
1433 if (have_ptrace_booke_interface ())
1434 {
1435 /* For PPC BookE processors, the number of available hardware
1436 watchpoints and breakpoints is stored at the booke_debug_info
1437 struct. */
1438 total_hw_bp = booke_debug_info.num_instruction_bps;
1439 total_hw_wp = booke_debug_info.num_data_bps;
1440 }
1441 else
1442 {
1443 /* For PPC server processors, we accept 1 hardware watchpoint and 0
1444 hardware breakpoints. */
1445 total_hw_bp = 0;
1446 total_hw_wp = 1;
1447 }
b7622095 1448
6ffbb7ab
TJB
1449 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
1450 || type == bp_access_watchpoint || type == bp_watchpoint)
1451 {
1452 if (cnt > total_hw_wp)
1453 return -1;
1454 }
1455 else if (type == bp_hardware_breakpoint)
1456 {
1457 if (cnt > total_hw_bp)
1458 return -1;
1459 }
1460
1461 if (!have_ptrace_booke_interface ())
1462 {
1463 int tid;
1464 ptid_t ptid = inferior_ptid;
1465
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);
1470 if (tid == 0)
1471 tid = PIDGET (ptid);
1472
1473 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1474 return 0;
1475 }
1476
1477 return 1;
b7622095
LM
1478}
1479
e0d24f8d
WZ
1480static int
1481ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1482{
1483 /* Handle sub-8-byte quantities. */
1484 if (len <= 0)
1485 return 0;
1486
6ffbb7ab
TJB
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 ())
1491 {
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))
1495 return 0;
1496 }
b7622095 1497 /* addr+len must fall in the 8 byte watchable region for DABR-based
6ffbb7ab
TJB
1498 processors (i.e., server processors). Without the new BookE ptrace
1499 interface, DAC-based processors (i.e., embedded processors) will use
b7622095 1500 addresses aligned to 4-bytes due to the way the read/write flags are
6ffbb7ab
TJB
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)
e0d24f8d
WZ
1505 return 0;
1506
1507 return 1;
1508}
1509
6ffbb7ab 1510/* This function compares two ppc_hw_breakpoint structs field-by-field. */
e4166a49 1511static int
6ffbb7ab
TJB
1512booke_cmp_hw_point (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
1513{
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);
1520}
1521
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. */
1526static struct thread_points *
1527booke_find_thread_points_by_tid (int tid, int alloc_new)
1528{
1529 int i;
1530 struct thread_points *t;
1531
1532 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
1533 if (t->tid == tid)
1534 return t;
1535
1536 t = NULL;
1537
1538 /* Do we need to allocate a new point_item
1539 if the wanted one does not exist? */
1540 if (alloc_new)
1541 {
1542 t = xmalloc (sizeof (struct thread_points));
1543 t->hw_breaks = xzalloc (max_slots_number * sizeof (struct hw_break_tuple));
1544 t->tid = tid;
1545 VEC_safe_push (thread_points_p, ppc_threads, t);
1546 }
1547
1548 return t;
1549}
1550
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. */
1554static void
1555booke_insert_point (struct ppc_hw_breakpoint *b, int tid)
1556{
1557 int i;
1558 long slot;
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;
1564
1565 memcpy (p, b, sizeof (struct ppc_hw_breakpoint));
1566
1567 errno = 0;
1568 slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p);
1569 if (slot < 0)
1570 perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
1571
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;
1576
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)
1580 {
1581 hw_breaks[i].slot = slot;
1582 hw_breaks[i].hw_break = p;
1583 break;
1584 }
1585
1586 gdb_assert (i != max_slots_number);
1587
1588 discard_cleanups (c);
1589}
1590
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. */
1594static void
1595booke_remove_point (struct ppc_hw_breakpoint *b, int tid)
1596{
1597 int i;
1598 struct hw_break_tuple *hw_breaks;
1599 struct thread_points *t;
1600
1601 t = booke_find_thread_points_by_tid (tid, 0);
1602 gdb_assert (t != NULL);
1603 hw_breaks = t->hw_breaks;
1604
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))
1607 break;
1608
1609 gdb_assert (i != max_slots_number);
1610
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. */
1614 errno = 0;
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"));
1618
1619 xfree (hw_breaks[i].hw_break);
1620 hw_breaks[i].hw_break = NULL;
1621}
9f0bdab8 1622
2c387241 1623static int
6ffbb7ab
TJB
1624ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
1625 struct bp_target_info *bp_tgt)
e0d24f8d 1626{
6ffbb7ab 1627 ptid_t ptid;
9f0bdab8 1628 struct lwp_info *lp;
6ffbb7ab
TJB
1629 struct ppc_hw_breakpoint p;
1630
1631 if (!have_ptrace_booke_interface ())
1632 return -1;
1633
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;
1639 p.addr2 = 0;
1640 p.condition_value = 0;
1641
1642 ALL_LWPS (lp, ptid)
1643 booke_insert_point (&p, TIDGET (ptid));
1644
1645 return 0;
1646}
1647
1648static int
1649ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
1650 struct bp_target_info *bp_tgt)
1651{
9f0bdab8 1652 ptid_t ptid;
6ffbb7ab
TJB
1653 struct lwp_info *lp;
1654 struct ppc_hw_breakpoint p;
b7622095 1655
6ffbb7ab
TJB
1656 if (!have_ptrace_booke_interface ())
1657 return -1;
1658
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;
1664 p.addr2 = 0;
1665 p.condition_value = 0;
1666
1667 ALL_LWPS (lp, ptid)
1668 booke_remove_point (&p, TIDGET (ptid));
1669
1670 return 0;
1671}
1672
1673static int
1674get_trigger_type (int rw)
1675{
1676 int t;
1677
1678 if (rw == hw_read)
1679 t = PPC_BREAKPOINT_TRIGGER_READ;
1680 else if (rw == hw_write)
1681 t = PPC_BREAKPOINT_TRIGGER_WRITE;
b7622095 1682 else
6ffbb7ab
TJB
1683 t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
1684
1685 return t;
1686}
1687
1688static int
1689ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
1690{
1691 struct lwp_info *lp;
1692 ptid_t ptid;
1693 int ret = -1;
1694
1695 if (have_ptrace_booke_interface ())
e0d24f8d 1696 {
6ffbb7ab
TJB
1697 struct ppc_hw_breakpoint p;
1698
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;
1704 p.addr2 = 0;
1705 p.condition_value = 0;
1706
1707 ALL_LWPS (lp, ptid)
1708 booke_insert_point (&p, TIDGET (ptid));
1709
1710 ret = 0;
e0d24f8d 1711 }
6ffbb7ab
TJB
1712 else
1713 {
1714 long dabr_value;
1715 long read_mode, write_mode;
e0d24f8d 1716
6ffbb7ab
TJB
1717 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1718 {
1719 /* PowerPC 440 requires only the read/write flags to be passed
1720 to the kernel. */
1721 read_mode = 1;
1722 write_mode = 2;
1723 }
1724 else
1725 {
1726 /* PowerPC 970 and other DABR-based processors are required to pass
1727 the Breakpoint Translation bit together with the flags. */
1728 read_mode = 5;
1729 write_mode = 6;
1730 }
1c86e440 1731
6ffbb7ab
TJB
1732 dabr_value = addr & ~(read_mode | write_mode);
1733 switch (rw)
1734 {
1735 case hw_read:
1736 /* Set read and translate bits. */
1737 dabr_value |= read_mode;
1738 break;
1739 case hw_write:
1740 /* Set write and translate bits. */
1741 dabr_value |= write_mode;
1742 break;
1743 case hw_access:
1744 /* Set read, write and translate bits. */
1745 dabr_value |= read_mode | write_mode;
1746 break;
1747 }
1c86e440 1748
6ffbb7ab
TJB
1749 saved_dabr_value = dabr_value;
1750
1751 ALL_LWPS (lp, ptid)
1752 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1753 return -1;
1754
1755 ret = 0;
1756 }
1757
1758 return ret;
e0d24f8d
WZ
1759}
1760
2c387241
AM
1761static int
1762ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
e0d24f8d 1763{
9f0bdab8
DJ
1764 struct lwp_info *lp;
1765 ptid_t ptid;
6ffbb7ab 1766 int ret = -1;
9f0bdab8 1767
6ffbb7ab
TJB
1768 if (have_ptrace_booke_interface ())
1769 {
1770 struct ppc_hw_breakpoint p;
1771
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;
1777 p.addr2 = 0;
1778 p.condition_value = 0;
1779
1780 ALL_LWPS (lp, ptid)
1781 booke_remove_point (&p, TIDGET (ptid));
1782
1783 ret = 0;
1784 }
1785 else
1786 {
1787 saved_dabr_value = 0;
1788 ALL_LWPS (lp, ptid)
1789 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1790 return -1;
1791
1792 ret = 0;
1793 }
1794
1795 return ret;
e0d24f8d
WZ
1796}
1797
9f0bdab8
DJ
1798static void
1799ppc_linux_new_thread (ptid_t ptid)
e0d24f8d 1800{
6ffbb7ab
TJB
1801 int tid = TIDGET (ptid);
1802
1803 if (have_ptrace_booke_interface ())
1804 {
1805 int i;
1806 struct thread_points *p;
1807 struct hw_break_tuple *hw_breaks;
1808
1809 if (VEC_empty (thread_points_p, ppc_threads))
1810 return;
1811
1812 /* Get a list of breakpoints from any thread. */
1813 p = VEC_last (thread_points_p, ppc_threads);
1814 hw_breaks = p->hw_breaks;
1815
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);
1820 }
1821 else
1822 ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
1823}
1824
1825static void
1826ppc_linux_thread_exit (struct thread_info *tp, int silent)
1827{
1828 int i;
1829 int tid = TIDGET (tp->ptid);
1830 struct hw_break_tuple *hw_breaks;
1831 struct thread_points *t = NULL, *p;
1832
1833 if (!have_ptrace_booke_interface ())
1834 return;
1835
1836 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
1837 if (p->tid == tid)
1838 {
1839 t = p;
1840 break;
1841 }
1842
1843 if (t == NULL)
1844 return;
1845
1846 VEC_unordered_remove (thread_points_p, ppc_threads, i);
1847
1848 hw_breaks = t->hw_breaks;
1849
1850 for (i = 0; i < max_slots_number; i++)
1851 if (hw_breaks[i].hw_break)
1852 xfree (hw_breaks[i].hw_break);
1853
1854 xfree (t->hw_breaks);
1855 xfree (t);
e0d24f8d
WZ
1856}
1857
1858static int
9f0bdab8 1859ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
e0d24f8d 1860{
9f0bdab8 1861 struct siginfo *siginfo_p;
e0d24f8d 1862
9f0bdab8 1863 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
e0d24f8d 1864
9f0bdab8
DJ
1865 if (siginfo_p->si_signo != SIGTRAP
1866 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
e0d24f8d
WZ
1867 return 0;
1868
6ffbb7ab
TJB
1869 if (have_ptrace_booke_interface ())
1870 {
1871 int i;
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;
1876
1877 t = booke_find_thread_points_by_tid (TIDGET (inferior_ptid), 0);
1878
1879 /* Find out if this *point is a hardware breakpoint.
1880 If so, we should return 0. */
1881 if (t)
1882 {
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)
1888 return 0;
1889 }
1890 }
1891
407f1a2e 1892 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
e0d24f8d
WZ
1893 return 1;
1894}
1895
9f0bdab8
DJ
1896static int
1897ppc_linux_stopped_by_watchpoint (void)
1898{
1899 CORE_ADDR addr;
1900 return ppc_linux_stopped_data_address (&current_target, &addr);
1901}
1902
5009afc5
AS
1903static int
1904ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
1905 CORE_ADDR addr,
1906 CORE_ADDR start, int length)
1907{
b7622095
LM
1908 int mask;
1909
6ffbb7ab
TJB
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)
b7622095
LM
1914 mask = 3;
1915 else
1916 mask = 7;
1917
1918 addr &= ~mask;
1919
1920 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
1921 return start <= addr + mask && start + length - 1 >= addr;
5009afc5
AS
1922}
1923
10d6c8cd 1924static void
28439f5e
PA
1925ppc_linux_store_inferior_registers (struct target_ops *ops,
1926 struct regcache *regcache, int regno)
45229ea4 1927{
05f13b9c
EZ
1928 /* Overload thread id onto process id */
1929 int tid = TIDGET (inferior_ptid);
1930
1931 /* No thread id, just use process id */
1932 if (tid == 0)
1933 tid = PIDGET (inferior_ptid);
1934
45229ea4 1935 if (regno >= 0)
56be3814 1936 store_register (regcache, tid, regno);
45229ea4 1937 else
56be3814 1938 store_ppc_registers (regcache, tid);
45229ea4
EZ
1939}
1940
f2db237a
AM
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. */
1946
50c9bd31 1947void
7f7fe91e 1948supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
c877c8e6 1949{
f2db237a 1950 const struct regset *regset = ppc_linux_gregset (sizeof (long));
f9be684a 1951
f2db237a 1952 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
c877c8e6
KB
1953}
1954
fdb28ac4 1955void
7f7fe91e
UW
1956fill_gregset (const struct regcache *regcache,
1957 gdb_gregset_t *gregsetp, int regno)
fdb28ac4 1958{
f2db237a 1959 const struct regset *regset = ppc_linux_gregset (sizeof (long));
f9be684a 1960
f2db237a
AM
1961 if (regno == -1)
1962 memset (gregsetp, 0, sizeof (*gregsetp));
1963 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
fdb28ac4
KB
1964}
1965
50c9bd31 1966void
7f7fe91e 1967supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
c877c8e6 1968{
f2db237a
AM
1969 const struct regset *regset = ppc_linux_fpregset ();
1970
1971 ppc_supply_fpregset (regset, regcache, -1,
1972 fpregsetp, sizeof (*fpregsetp));
c877c8e6 1973}
fdb28ac4 1974
fdb28ac4 1975void
7f7fe91e
UW
1976fill_fpregset (const struct regcache *regcache,
1977 gdb_fpregset_t *fpregsetp, int regno)
fdb28ac4 1978{
f2db237a
AM
1979 const struct regset *regset = ppc_linux_fpregset ();
1980
1981 ppc_collect_fpregset (regset, regcache, regno,
1982 fpregsetp, sizeof (*fpregsetp));
fdb28ac4 1983}
10d6c8cd 1984
409c383c
UW
1985static int
1986ppc_linux_target_wordsize (void)
1987{
1988 int wordsize = 4;
1989
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__
1993 long msr;
1994
1995 int tid = TIDGET (inferior_ptid);
1996 if (tid == 0)
1997 tid = PIDGET (inferior_ptid);
1998
1999 errno = 0;
2000 msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
2001 if (errno == 0 && msr < 0)
2002 wordsize = 8;
2003#endif
2004
2005 return wordsize;
2006}
2007
2008static int
2009ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
2010 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
2011{
2012 int sizeof_auxv_field = ppc_linux_target_wordsize ();
e17a4113 2013 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
409c383c
UW
2014 gdb_byte *ptr = *readptr;
2015
2016 if (endptr == ptr)
2017 return 0;
2018
2019 if (endptr - ptr < sizeof_auxv_field * 2)
2020 return -1;
2021
e17a4113 2022 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
409c383c 2023 ptr += sizeof_auxv_field;
e17a4113 2024 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
409c383c
UW
2025 ptr += sizeof_auxv_field;
2026
2027 *readptr = ptr;
2028 return 1;
2029}
2030
310a98e1
DJ
2031static const struct target_desc *
2032ppc_linux_read_description (struct target_ops *ops)
2033{
7284e1be 2034 int altivec = 0;
604c2f83 2035 int vsx = 0;
69abc51c 2036 int isa205 = 0;
f4d9bade 2037 int cell = 0;
7284e1be
UW
2038
2039 int tid = TIDGET (inferior_ptid);
2040 if (tid == 0)
2041 tid = PIDGET (inferior_ptid);
2042
310a98e1
DJ
2043 if (have_ptrace_getsetevrregs)
2044 {
2045 struct gdb_evrregset_t evrregset;
310a98e1
DJ
2046
2047 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
7284e1be
UW
2048 return tdesc_powerpc_e500l;
2049
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"));
2054 }
2055
604c2f83
LM
2056 if (have_ptrace_getsetvsxregs)
2057 {
2058 gdb_vsxregset_t vsxregset;
2059
2060 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
2061 vsx = 1;
2062
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"));
2067 }
2068
7284e1be
UW
2069 if (have_ptrace_getvrregs)
2070 {
2071 gdb_vrregset_t vrregset;
2072
2073 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
2074 altivec = 1;
2075
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"));
310a98e1
DJ
2080 }
2081
f04c6d38
TJB
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)
69abc51c
TJB
2089 isa205 = 1;
2090
f4d9bade
UW
2091 if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
2092 cell = 1;
2093
409c383c
UW
2094 if (ppc_linux_target_wordsize () == 8)
2095 {
f4d9bade
UW
2096 if (cell)
2097 return tdesc_powerpc_cell64l;
2098 else if (vsx)
409c383c
UW
2099 return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
2100 else if (altivec)
2101 return isa205? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
2102
2103 return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
2104 }
7284e1be 2105
f4d9bade
UW
2106 if (cell)
2107 return tdesc_powerpc_cell32l;
2108 else if (vsx)
69abc51c 2109 return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
604c2f83 2110 else if (altivec)
69abc51c 2111 return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
604c2f83 2112
69abc51c 2113 return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
310a98e1
DJ
2114}
2115
10d6c8cd
DJ
2116void _initialize_ppc_linux_nat (void);
2117
2118void
2119_initialize_ppc_linux_nat (void)
2120{
2121 struct target_ops *t;
2122
2123 /* Fill in the generic GNU/Linux methods. */
2124 t = linux_target ();
2125
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;
2129
6ffbb7ab
TJB
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;
e0d24f8d
WZ
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;
5009afc5 2139 t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
e0d24f8d 2140
310a98e1 2141 t->to_read_description = ppc_linux_read_description;
409c383c 2142 t->to_auxv_parse = ppc_linux_auxv_parse;
310a98e1 2143
6ffbb7ab
TJB
2144 observer_attach_thread_exit (ppc_linux_thread_exit);
2145
10d6c8cd 2146 /* Register the target. */
f973ed9c 2147 linux_nat_add_target (t);
9f0bdab8 2148 linux_nat_set_new_thread (t, ppc_linux_new_thread);
10d6c8cd 2149}
This page took 1.015167 seconds and 4 git commands to generate.