* exec.c: #include "arch-utils.h"
[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,
9b254dd1 4 2004, 2005, 2006, 2007, 2008 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"
c877c8e6
KB
23#include "frame.h"
24#include "inferior.h"
25#include "gdbcore.h"
4e052eda 26#include "regcache.h"
383f0f5b 27#include "gdb_assert.h"
10d6c8cd
DJ
28#include "target.h"
29#include "linux-nat.h"
c877c8e6 30
411cb3f9 31#include <stdint.h>
c877c8e6
KB
32#include <sys/types.h>
33#include <sys/param.h>
34#include <signal.h>
35#include <sys/user.h>
36#include <sys/ioctl.h>
2555fe1a 37#include "gdb_wait.h"
c877c8e6
KB
38#include <fcntl.h>
39#include <sys/procfs.h>
45229ea4 40#include <sys/ptrace.h>
c877c8e6 41
c60c0f5f
MS
42/* Prototypes for supply_gregset etc. */
43#include "gregset.h"
16333c4f 44#include "ppc-tdep.h"
c60c0f5f 45
9abe5450
EZ
46/* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
47 configure time check. Some older glibc's (for instance 2.2.1)
48 don't have a specific powerpc version of ptrace.h, and fall back on
49 a generic one. In such cases, sys/ptrace.h defines
50 PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
51 ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
52 PTRACE_SETVRREGS to be. This also makes a configury check pretty
53 much useless. */
54
55/* These definitions should really come from the glibc header files,
56 but Glibc doesn't know about the vrregs yet. */
57#ifndef PTRACE_GETVRREGS
58#define PTRACE_GETVRREGS 18
59#define PTRACE_SETVRREGS 19
60#endif
61
01904826
JB
62
63/* Similarly for the ptrace requests for getting / setting the SPE
64 registers (ev0 -- ev31, acc, and spefscr). See the description of
65 gdb_evrregset_t for details. */
66#ifndef PTRACE_GETEVRREGS
67#define PTRACE_GETEVRREGS 20
68#define PTRACE_SETEVRREGS 21
69#endif
70
e0d24f8d
WZ
71/* Similarly for the hardware watchpoint support. */
72#ifndef PTRACE_GET_DEBUGREG
73#define PTRACE_GET_DEBUGREG 25
74#endif
75#ifndef PTRACE_SET_DEBUGREG
76#define PTRACE_SET_DEBUGREG 26
77#endif
78#ifndef PTRACE_GETSIGINFO
79#define PTRACE_GETSIGINFO 0x4202
80#endif
01904826 81
9abe5450
EZ
82/* This oddity is because the Linux kernel defines elf_vrregset_t as
83 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
84 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
85 the vrsave as an extra 4 bytes at the end. I opted for creating a
86 flat array of chars, so that it is easier to manipulate for gdb.
87
88 There are 32 vector registers 16 bytes longs, plus a VSCR register
89 which is only 4 bytes long, but is fetched as a 16 bytes
90 quantity. Up to here we have the elf_vrregset_t structure.
91 Appended to this there is space for the VRSAVE register: 4 bytes.
92 Even though this vrsave register is not included in the regset
93 typedef, it is handled by the ptrace requests.
94
95 Note that GNU/Linux doesn't support little endian PPC hardware,
96 therefore the offset at which the real value of the VSCR register
97 is located will be always 12 bytes.
98
99 The layout is like this (where x is the actual value of the vscr reg): */
100
101/* *INDENT-OFF* */
102/*
103 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
104 <-------> <-------><-------><->
105 VR0 VR31 VSCR VRSAVE
106*/
107/* *INDENT-ON* */
108
109#define SIZEOF_VRREGS 33*16+4
110
111typedef char gdb_vrregset_t[SIZEOF_VRREGS];
112
01904826
JB
113
114/* On PPC processors that support the the Signal Processing Extension
115 (SPE) APU, the general-purpose registers are 64 bits long.
411cb3f9
PG
116 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
117 ptrace calls only access the lower half of each register, to allow
118 them to behave the same way they do on non-SPE systems. There's a
119 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
120 read and write the top halves of all the general-purpose registers
121 at once, along with some SPE-specific registers.
01904826
JB
122
123 GDB itself continues to claim the general-purpose registers are 32
6ced10dd
JB
124 bits long. It has unnamed raw registers that hold the upper halves
125 of the gprs, and the the full 64-bit SIMD views of the registers,
126 'ev0' -- 'ev31', are pseudo-registers that splice the top and
127 bottom halves together.
01904826
JB
128
129 This is the structure filled in by PTRACE_GETEVRREGS and written to
130 the inferior's registers by PTRACE_SETEVRREGS. */
131struct gdb_evrregset_t
132{
133 unsigned long evr[32];
134 unsigned long long acc;
135 unsigned long spefscr;
136};
137
138
139/* Non-zero if our kernel may support the PTRACE_GETVRREGS and
140 PTRACE_SETVRREGS requests, for reading and writing the Altivec
141 registers. Zero if we've tried one of them and gotten an
142 error. */
9abe5450
EZ
143int have_ptrace_getvrregs = 1;
144
01904826
JB
145/* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
146 PTRACE_SETEVRREGS requests, for reading and writing the SPE
147 registers. Zero if we've tried one of them and gotten an
148 error. */
149int have_ptrace_getsetevrregs = 1;
150
16333c4f
EZ
151/* *INDENT-OFF* */
152/* registers layout, as presented by the ptrace interface:
153PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
154PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
155PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
156PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
157PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
158PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
159PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
160PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
161PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
162/* *INDENT_ON * */
c877c8e6 163
45229ea4
EZ
164static int
165ppc_register_u_addr (int regno)
c877c8e6 166{
16333c4f 167 int u_addr = -1;
dc5cfeb6 168 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
56d0d96a
AC
169 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
170 interface, and not the wordsize of the program's ABI. */
411cb3f9 171 int wordsize = sizeof (long);
16333c4f
EZ
172
173 /* General purpose registers occupy 1 slot each in the buffer */
8bf659e8
JB
174 if (regno >= tdep->ppc_gp0_regnum
175 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
26e75e5c 176 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
16333c4f 177
49ff75ad
JB
178 /* Floating point regs: eight bytes each in both 32- and 64-bit
179 ptrace interfaces. Thus, two slots each in 32-bit interface, one
180 slot each in 64-bit interface. */
383f0f5b
JB
181 if (tdep->ppc_fp0_regnum >= 0
182 && regno >= tdep->ppc_fp0_regnum
366f009f
JB
183 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
184 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
16333c4f
EZ
185
186 /* UISA special purpose registers: 1 slot each */
3e8c568d 187 if (regno == gdbarch_pc_regnum (current_gdbarch))
49ff75ad 188 u_addr = PT_NIP * wordsize;
dc5cfeb6 189 if (regno == tdep->ppc_lr_regnum)
49ff75ad 190 u_addr = PT_LNK * wordsize;
dc5cfeb6 191 if (regno == tdep->ppc_cr_regnum)
49ff75ad 192 u_addr = PT_CCR * wordsize;
dc5cfeb6 193 if (regno == tdep->ppc_xer_regnum)
49ff75ad 194 u_addr = PT_XER * wordsize;
dc5cfeb6 195 if (regno == tdep->ppc_ctr_regnum)
49ff75ad 196 u_addr = PT_CTR * wordsize;
f8c59253 197#ifdef PT_MQ
dc5cfeb6 198 if (regno == tdep->ppc_mq_regnum)
49ff75ad 199 u_addr = PT_MQ * wordsize;
f8c59253 200#endif
dc5cfeb6 201 if (regno == tdep->ppc_ps_regnum)
49ff75ad 202 u_addr = PT_MSR * wordsize;
383f0f5b
JB
203 if (tdep->ppc_fpscr_regnum >= 0
204 && regno == tdep->ppc_fpscr_regnum)
8f135812
AC
205 {
206 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
207 kernel headers incorrectly contained the 32-bit definition of
208 PT_FPSCR. For the 32-bit definition, floating-point
209 registers occupy two 32-bit "slots", and the FPSCR lives in
210 the secondhalf of such a slot-pair (hence +1). For 64-bit,
211 the FPSCR instead occupies the full 64-bit 2-word-slot and
212 hence no adjustment is necessary. Hack around this. */
213 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
214 u_addr = (48 + 32) * wordsize;
215 else
216 u_addr = PT_FPSCR * wordsize;
217 }
16333c4f 218 return u_addr;
c877c8e6
KB
219}
220
9abe5450
EZ
221/* The Linux kernel ptrace interface for AltiVec registers uses the
222 registers set mechanism, as opposed to the interface for all the
223 other registers, that stores/fetches each register individually. */
224static void
56be3814 225fetch_altivec_register (struct regcache *regcache, int tid, int regno)
9abe5450
EZ
226{
227 int ret;
228 int offset = 0;
229 gdb_vrregset_t regs;
40a6adc1
MD
230 struct gdbarch *gdbarch = get_regcache_arch (regcache);
231 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
232 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
9abe5450
EZ
233
234 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
235 if (ret < 0)
236 {
237 if (errno == EIO)
238 {
239 have_ptrace_getvrregs = 0;
240 return;
241 }
e2e0b3e5 242 perror_with_name (_("Unable to fetch AltiVec register"));
9abe5450
EZ
243 }
244
245 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
246 long on the hardware. We deal only with the lower 4 bytes of the
247 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
248 there is no need to define an offset for it. */
249 if (regno == (tdep->ppc_vrsave_regnum - 1))
40a6adc1 250 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450 251
56be3814 252 regcache_raw_supply (regcache, regno,
23a6d369 253 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
9abe5450
EZ
254}
255
01904826
JB
256/* Fetch the top 32 bits of TID's general-purpose registers and the
257 SPE-specific registers, and place the results in EVRREGSET. If we
258 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
259 zeros.
260
261 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
262 PTRACE_SETEVRREGS requests are supported is isolated here, and in
263 set_spe_registers. */
264static void
265get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
266{
267 if (have_ptrace_getsetevrregs)
268 {
269 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
270 return;
271 else
272 {
273 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
274 we just return zeros. */
275 if (errno == EIO)
276 have_ptrace_getsetevrregs = 0;
277 else
278 /* Anything else needs to be reported. */
e2e0b3e5 279 perror_with_name (_("Unable to fetch SPE registers"));
01904826
JB
280 }
281 }
282
283 memset (evrregset, 0, sizeof (*evrregset));
284}
285
6ced10dd
JB
286/* Supply values from TID for SPE-specific raw registers: the upper
287 halves of the GPRs, the accumulator, and the spefscr. REGNO must
288 be the number of an upper half register, acc, spefscr, or -1 to
289 supply the values of all registers. */
01904826 290static void
56be3814 291fetch_spe_register (struct regcache *regcache, int tid, int regno)
01904826 292{
40a6adc1
MD
293 struct gdbarch *gdbarch = get_regcache_arch (regcache);
294 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01904826
JB
295 struct gdb_evrregset_t evrregs;
296
6ced10dd 297 gdb_assert (sizeof (evrregs.evr[0])
40a6adc1 298 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
6ced10dd 299 gdb_assert (sizeof (evrregs.acc)
40a6adc1 300 == register_size (gdbarch, tdep->ppc_acc_regnum));
6ced10dd 301 gdb_assert (sizeof (evrregs.spefscr)
40a6adc1 302 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
6ced10dd 303
01904826
JB
304 get_spe_registers (tid, &evrregs);
305
6ced10dd 306 if (regno == -1)
01904826 307 {
6ced10dd
JB
308 int i;
309
310 for (i = 0; i < ppc_num_gprs; i++)
56be3814 311 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
6ced10dd 312 &evrregs.evr[i]);
01904826 313 }
6ced10dd
JB
314 else if (tdep->ppc_ev0_upper_regnum <= regno
315 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
56be3814 316 regcache_raw_supply (regcache, regno,
6ced10dd
JB
317 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
318
319 if (regno == -1
320 || regno == tdep->ppc_acc_regnum)
56be3814 321 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
6ced10dd
JB
322
323 if (regno == -1
324 || regno == tdep->ppc_spefscr_regnum)
56be3814 325 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
6ced10dd 326 &evrregs.spefscr);
01904826
JB
327}
328
45229ea4 329static void
56be3814 330fetch_register (struct regcache *regcache, int tid, int regno)
45229ea4 331{
40a6adc1
MD
332 struct gdbarch *gdbarch = get_regcache_arch (regcache);
333 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
45229ea4 334 /* This isn't really an address. But ptrace thinks of it as one. */
0397dee1 335 CORE_ADDR regaddr = ppc_register_u_addr (regno);
4a19ea35 336 int bytes_transferred;
45229ea4 337 unsigned int offset; /* Offset of registers within the u area. */
d9d9c31f 338 char buf[MAX_REGISTER_SIZE];
45229ea4 339
9abe5450
EZ
340 if (altivec_register_p (regno))
341 {
342 /* If this is the first time through, or if it is not the first
343 time through, and we have comfirmed that there is kernel
344 support for such a ptrace request, then go and fetch the
345 register. */
346 if (have_ptrace_getvrregs)
347 {
56be3814 348 fetch_altivec_register (regcache, tid, regno);
9abe5450
EZ
349 return;
350 }
351 /* If we have discovered that there is no ptrace support for
352 AltiVec registers, fall through and return zeroes, because
353 regaddr will be -1 in this case. */
354 }
01904826
JB
355 else if (spe_register_p (regno))
356 {
56be3814 357 fetch_spe_register (regcache, tid, regno);
01904826
JB
358 return;
359 }
9abe5450 360
45229ea4
EZ
361 if (regaddr == -1)
362 {
40a6adc1 363 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
56be3814 364 regcache_raw_supply (regcache, regno, buf);
45229ea4
EZ
365 return;
366 }
367
411cb3f9 368 /* Read the raw register using sizeof(long) sized chunks. On a
56d0d96a
AC
369 32-bit platform, 64-bit floating-point registers will require two
370 transfers. */
4a19ea35 371 for (bytes_transferred = 0;
40a6adc1 372 bytes_transferred < register_size (gdbarch, regno);
411cb3f9 373 bytes_transferred += sizeof (long))
45229ea4
EZ
374 {
375 errno = 0;
411cb3f9
PG
376 *(long *) &buf[bytes_transferred]
377 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
378 regaddr += sizeof (long);
45229ea4
EZ
379 if (errno != 0)
380 {
bc97b3ba
JB
381 char message[128];
382 sprintf (message, "reading register %s (#%d)",
40a6adc1 383 gdbarch_register_name (gdbarch, regno), regno);
bc97b3ba 384 perror_with_name (message);
45229ea4
EZ
385 }
386 }
56d0d96a 387
4a19ea35
JB
388 /* Now supply the register. Keep in mind that the regcache's idea
389 of the register's size may not be a multiple of sizeof
411cb3f9 390 (long). */
40a6adc1 391 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
4a19ea35
JB
392 {
393 /* Little-endian values are always found at the left end of the
394 bytes transferred. */
56be3814 395 regcache_raw_supply (regcache, regno, buf);
4a19ea35 396 }
40a6adc1 397 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4a19ea35
JB
398 {
399 /* Big-endian values are found at the right end of the bytes
400 transferred. */
40a6adc1 401 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
56be3814 402 regcache_raw_supply (regcache, regno, buf + padding);
4a19ea35
JB
403 }
404 else
a44bddec 405 internal_error (__FILE__, __LINE__,
e2e0b3e5 406 _("fetch_register: unexpected byte order: %d"),
40a6adc1 407 gdbarch_byte_order (gdbarch));
45229ea4
EZ
408}
409
9abe5450 410static void
56be3814 411supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
9abe5450
EZ
412{
413 int i;
40a6adc1
MD
414 struct gdbarch *gdbarch = get_regcache_arch (regcache);
415 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 416 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
40a6adc1
MD
417 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
418 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450
EZ
419
420 for (i = 0; i < num_of_vrregs; i++)
421 {
422 /* The last 2 registers of this set are only 32 bit long, not
423 128. However an offset is necessary only for VSCR because it
424 occupies a whole vector, while VRSAVE occupies a full 4 bytes
425 slot. */
426 if (i == (num_of_vrregs - 2))
56be3814 427 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
23a6d369 428 *vrregsetp + i * vrregsize + offset);
9abe5450 429 else
56be3814 430 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
23a6d369 431 *vrregsetp + i * vrregsize);
9abe5450
EZ
432 }
433}
434
435static void
56be3814 436fetch_altivec_registers (struct regcache *regcache, int tid)
9abe5450
EZ
437{
438 int ret;
439 gdb_vrregset_t regs;
440
441 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
442 if (ret < 0)
443 {
444 if (errno == EIO)
445 {
446 have_ptrace_getvrregs = 0;
447 return;
448 }
e2e0b3e5 449 perror_with_name (_("Unable to fetch AltiVec registers"));
9abe5450 450 }
56be3814 451 supply_vrregset (regcache, &regs);
9abe5450
EZ
452}
453
45229ea4 454static void
56be3814 455fetch_ppc_registers (struct regcache *regcache, int tid)
45229ea4
EZ
456{
457 int i;
40a6adc1
MD
458 struct gdbarch *gdbarch = get_regcache_arch (regcache);
459 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 460
6ced10dd 461 for (i = 0; i < ppc_num_gprs; i++)
56be3814 462 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
32b99774
JB
463 if (tdep->ppc_fp0_regnum >= 0)
464 for (i = 0; i < ppc_num_fprs; i++)
56be3814 465 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
40a6adc1 466 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
32b99774 467 if (tdep->ppc_ps_regnum != -1)
56be3814 468 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
32b99774 469 if (tdep->ppc_cr_regnum != -1)
56be3814 470 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
32b99774 471 if (tdep->ppc_lr_regnum != -1)
56be3814 472 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
32b99774 473 if (tdep->ppc_ctr_regnum != -1)
56be3814 474 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
32b99774 475 if (tdep->ppc_xer_regnum != -1)
56be3814 476 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
e3f36dbd 477 if (tdep->ppc_mq_regnum != -1)
56be3814 478 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
32b99774 479 if (tdep->ppc_fpscr_regnum != -1)
56be3814 480 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
9abe5450
EZ
481 if (have_ptrace_getvrregs)
482 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
56be3814 483 fetch_altivec_registers (regcache, tid);
6ced10dd 484 if (tdep->ppc_ev0_upper_regnum >= 0)
56be3814 485 fetch_spe_register (regcache, tid, -1);
45229ea4
EZ
486}
487
488/* Fetch registers from the child process. Fetch all registers if
489 regno == -1, otherwise fetch all general registers or all floating
490 point registers depending upon the value of regno. */
10d6c8cd 491static void
56be3814 492ppc_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
45229ea4 493{
9abe5450 494 /* Overload thread id onto process id */
05f13b9c
EZ
495 int tid = TIDGET (inferior_ptid);
496
497 /* No thread id, just use process id */
498 if (tid == 0)
499 tid = PIDGET (inferior_ptid);
500
9abe5450 501 if (regno == -1)
56be3814 502 fetch_ppc_registers (regcache, tid);
45229ea4 503 else
56be3814 504 fetch_register (regcache, tid, regno);
45229ea4
EZ
505}
506
507/* Store one register. */
9abe5450 508static void
56be3814 509store_altivec_register (const struct regcache *regcache, int tid, int regno)
9abe5450
EZ
510{
511 int ret;
512 int offset = 0;
513 gdb_vrregset_t regs;
40a6adc1
MD
514 struct gdbarch *gdbarch = get_regcache_arch (regcache);
515 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
516 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
9abe5450
EZ
517
518 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
519 if (ret < 0)
520 {
521 if (errno == EIO)
522 {
523 have_ptrace_getvrregs = 0;
524 return;
525 }
e2e0b3e5 526 perror_with_name (_("Unable to fetch AltiVec register"));
9abe5450
EZ
527 }
528
529 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
530 long on the hardware. */
531 if (regno == (tdep->ppc_vrsave_regnum - 1))
40a6adc1 532 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450 533
56be3814 534 regcache_raw_collect (regcache, regno,
822c9732 535 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
9abe5450
EZ
536
537 ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
538 if (ret < 0)
e2e0b3e5 539 perror_with_name (_("Unable to store AltiVec register"));
9abe5450
EZ
540}
541
01904826
JB
542/* Assuming TID referrs to an SPE process, set the top halves of TID's
543 general-purpose registers and its SPE-specific registers to the
544 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
545 nothing.
546
547 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
548 PTRACE_SETEVRREGS requests are supported is isolated here, and in
549 get_spe_registers. */
550static void
551set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
552{
553 if (have_ptrace_getsetevrregs)
554 {
555 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
556 return;
557 else
558 {
559 /* EIO means that the PTRACE_SETEVRREGS request isn't
560 supported; we fail silently, and don't try the call
561 again. */
562 if (errno == EIO)
563 have_ptrace_getsetevrregs = 0;
564 else
565 /* Anything else needs to be reported. */
e2e0b3e5 566 perror_with_name (_("Unable to set SPE registers"));
01904826
JB
567 }
568 }
569}
570
6ced10dd
JB
571/* Write GDB's value for the SPE-specific raw register REGNO to TID.
572 If REGNO is -1, write the values of all the SPE-specific
573 registers. */
01904826 574static void
56be3814 575store_spe_register (const struct regcache *regcache, int tid, int regno)
01904826 576{
40a6adc1
MD
577 struct gdbarch *gdbarch = get_regcache_arch (regcache);
578 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01904826
JB
579 struct gdb_evrregset_t evrregs;
580
6ced10dd 581 gdb_assert (sizeof (evrregs.evr[0])
40a6adc1 582 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
6ced10dd 583 gdb_assert (sizeof (evrregs.acc)
40a6adc1 584 == register_size (gdbarch, tdep->ppc_acc_regnum));
6ced10dd 585 gdb_assert (sizeof (evrregs.spefscr)
40a6adc1 586 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
01904826 587
6ced10dd
JB
588 if (regno == -1)
589 /* Since we're going to write out every register, the code below
590 should store to every field of evrregs; if that doesn't happen,
591 make it obvious by initializing it with suspicious values. */
592 memset (&evrregs, 42, sizeof (evrregs));
593 else
594 /* We can only read and write the entire EVR register set at a
595 time, so to write just a single register, we do a
596 read-modify-write maneuver. */
597 get_spe_registers (tid, &evrregs);
598
599 if (regno == -1)
01904826 600 {
6ced10dd
JB
601 int i;
602
603 for (i = 0; i < ppc_num_gprs; i++)
56be3814 604 regcache_raw_collect (regcache,
6ced10dd
JB
605 tdep->ppc_ev0_upper_regnum + i,
606 &evrregs.evr[i]);
01904826 607 }
6ced10dd
JB
608 else if (tdep->ppc_ev0_upper_regnum <= regno
609 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
56be3814 610 regcache_raw_collect (regcache, regno,
6ced10dd
JB
611 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
612
613 if (regno == -1
614 || regno == tdep->ppc_acc_regnum)
56be3814 615 regcache_raw_collect (regcache,
6ced10dd
JB
616 tdep->ppc_acc_regnum,
617 &evrregs.acc);
618
619 if (regno == -1
620 || regno == tdep->ppc_spefscr_regnum)
56be3814 621 regcache_raw_collect (regcache,
6ced10dd
JB
622 tdep->ppc_spefscr_regnum,
623 &evrregs.spefscr);
01904826
JB
624
625 /* Write back the modified register set. */
626 set_spe_registers (tid, &evrregs);
627}
628
45229ea4 629static void
56be3814 630store_register (const struct regcache *regcache, int tid, int regno)
45229ea4 631{
40a6adc1
MD
632 struct gdbarch *gdbarch = get_regcache_arch (regcache);
633 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
45229ea4
EZ
634 /* This isn't really an address. But ptrace thinks of it as one. */
635 CORE_ADDR regaddr = ppc_register_u_addr (regno);
52f0bd74 636 int i;
4a19ea35 637 size_t bytes_to_transfer;
d9d9c31f 638 char buf[MAX_REGISTER_SIZE];
45229ea4 639
9abe5450 640 if (altivec_register_p (regno))
45229ea4 641 {
56be3814 642 store_altivec_register (regcache, tid, regno);
45229ea4
EZ
643 return;
644 }
01904826
JB
645 else if (spe_register_p (regno))
646 {
56be3814 647 store_spe_register (regcache, tid, regno);
01904826
JB
648 return;
649 }
45229ea4 650
9abe5450
EZ
651 if (regaddr == -1)
652 return;
653
4a19ea35
JB
654 /* First collect the register. Keep in mind that the regcache's
655 idea of the register's size may not be a multiple of sizeof
411cb3f9 656 (long). */
56d0d96a 657 memset (buf, 0, sizeof buf);
40a6adc1
MD
658 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
659 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
4a19ea35
JB
660 {
661 /* Little-endian values always sit at the left end of the buffer. */
56be3814 662 regcache_raw_collect (regcache, regno, buf);
4a19ea35 663 }
40a6adc1 664 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4a19ea35
JB
665 {
666 /* Big-endian values sit at the right end of the buffer. */
40a6adc1 667 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
56be3814 668 regcache_raw_collect (regcache, regno, buf + padding);
4a19ea35
JB
669 }
670
411cb3f9 671 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
45229ea4
EZ
672 {
673 errno = 0;
411cb3f9
PG
674 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
675 *(long *) &buf[i]);
676 regaddr += sizeof (long);
e3f36dbd
KB
677
678 if (errno == EIO
383f0f5b 679 && regno == tdep->ppc_fpscr_regnum)
e3f36dbd
KB
680 {
681 /* Some older kernel versions don't allow fpscr to be written. */
682 continue;
683 }
684
45229ea4
EZ
685 if (errno != 0)
686 {
bc97b3ba
JB
687 char message[128];
688 sprintf (message, "writing register %s (#%d)",
40a6adc1 689 gdbarch_register_name (gdbarch, regno), regno);
bc97b3ba 690 perror_with_name (message);
45229ea4
EZ
691 }
692 }
693}
694
9abe5450 695static void
56be3814 696fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
9abe5450
EZ
697{
698 int i;
40a6adc1
MD
699 struct gdbarch *gdbarch = get_regcache_arch (regcache);
700 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9abe5450 701 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
40a6adc1
MD
702 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
703 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
9abe5450
EZ
704
705 for (i = 0; i < num_of_vrregs; i++)
706 {
707 /* The last 2 registers of this set are only 32 bit long, not
708 128, but only VSCR is fetched as a 16 bytes quantity. */
709 if (i == (num_of_vrregs - 2))
56be3814 710 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
822c9732 711 *vrregsetp + i * vrregsize + offset);
9abe5450 712 else
56be3814 713 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
822c9732 714 *vrregsetp + i * vrregsize);
9abe5450
EZ
715 }
716}
717
718static void
56be3814 719store_altivec_registers (const struct regcache *regcache, int tid)
9abe5450
EZ
720{
721 int ret;
722 gdb_vrregset_t regs;
723
0897f59b 724 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
9abe5450
EZ
725 if (ret < 0)
726 {
727 if (errno == EIO)
728 {
729 have_ptrace_getvrregs = 0;
730 return;
731 }
e2e0b3e5 732 perror_with_name (_("Couldn't get AltiVec registers"));
9abe5450
EZ
733 }
734
56be3814 735 fill_vrregset (regcache, &regs);
9abe5450 736
0897f59b 737 if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
e2e0b3e5 738 perror_with_name (_("Couldn't write AltiVec registers"));
9abe5450
EZ
739}
740
45229ea4 741static void
56be3814 742store_ppc_registers (const struct regcache *regcache, int tid)
45229ea4
EZ
743{
744 int i;
40a6adc1
MD
745 struct gdbarch *gdbarch = get_regcache_arch (regcache);
746 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
45229ea4 747
6ced10dd 748 for (i = 0; i < ppc_num_gprs; i++)
56be3814 749 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
32b99774
JB
750 if (tdep->ppc_fp0_regnum >= 0)
751 for (i = 0; i < ppc_num_fprs; i++)
56be3814 752 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
40a6adc1 753 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
32b99774 754 if (tdep->ppc_ps_regnum != -1)
56be3814 755 store_register (regcache, tid, tdep->ppc_ps_regnum);
32b99774 756 if (tdep->ppc_cr_regnum != -1)
56be3814 757 store_register (regcache, tid, tdep->ppc_cr_regnum);
32b99774 758 if (tdep->ppc_lr_regnum != -1)
56be3814 759 store_register (regcache, tid, tdep->ppc_lr_regnum);
32b99774 760 if (tdep->ppc_ctr_regnum != -1)
56be3814 761 store_register (regcache, tid, tdep->ppc_ctr_regnum);
32b99774 762 if (tdep->ppc_xer_regnum != -1)
56be3814 763 store_register (regcache, tid, tdep->ppc_xer_regnum);
e3f36dbd 764 if (tdep->ppc_mq_regnum != -1)
56be3814 765 store_register (regcache, tid, tdep->ppc_mq_regnum);
32b99774 766 if (tdep->ppc_fpscr_regnum != -1)
56be3814 767 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
9abe5450
EZ
768 if (have_ptrace_getvrregs)
769 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
56be3814 770 store_altivec_registers (regcache, tid);
6ced10dd 771 if (tdep->ppc_ev0_upper_regnum >= 0)
56be3814 772 store_spe_register (regcache, tid, -1);
45229ea4
EZ
773}
774
e0d24f8d
WZ
775static int
776ppc_linux_check_watch_resources (int type, int cnt, int ot)
777{
778 int tid;
779 ptid_t ptid = inferior_ptid;
780
781 /* DABR (data address breakpoint register) is optional for PPC variants.
782 Some variants have one DABR, others have none. So CNT can't be larger
783 than 1. */
784 if (cnt > 1)
785 return 0;
786
787 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
788 the target has DABR. If either answer is no, the ptrace call will
789 return -1. Fail in that case. */
790 tid = TIDGET (ptid);
791 if (tid == 0)
792 tid = PIDGET (ptid);
793
794 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
795 return 0;
796 return 1;
797}
798
799static int
800ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
801{
802 /* Handle sub-8-byte quantities. */
803 if (len <= 0)
804 return 0;
805
806 /* addr+len must fall in the 8 byte watchable region. */
807 if ((addr + len) > (addr & ~7) + 8)
808 return 0;
809
810 return 1;
811}
812
9f0bdab8
DJ
813/* The cached DABR value, to install in new threads. */
814static long saved_dabr_value;
815
e0d24f8d 816/* Set a watchpoint of type TYPE at address ADDR. */
2c387241 817static int
e0d24f8d
WZ
818ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
819{
9f0bdab8
DJ
820 struct lwp_info *lp;
821 ptid_t ptid;
e0d24f8d 822 long dabr_value;
e0d24f8d
WZ
823
824 dabr_value = addr & ~7;
825 switch (rw)
826 {
827 case hw_read:
828 /* Set read and translate bits. */
829 dabr_value |= 5;
830 break;
831 case hw_write:
832 /* Set write and translate bits. */
833 dabr_value |= 6;
834 break;
835 case hw_access:
836 /* Set read, write and translate bits. */
837 dabr_value |= 7;
838 break;
839 }
840
1c86e440
LM
841 saved_dabr_value = dabr_value;
842
9f0bdab8
DJ
843 ALL_LWPS (lp, ptid)
844 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
845 return -1;
1c86e440 846
9f0bdab8 847 return 0;
e0d24f8d
WZ
848}
849
2c387241
AM
850static int
851ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
e0d24f8d 852{
9f0bdab8
DJ
853 struct lwp_info *lp;
854 ptid_t ptid;
855 long dabr_value = 0;
856
857 saved_dabr_value = 0;
858 ALL_LWPS (lp, ptid)
859 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
860 return -1;
861 return 0;
e0d24f8d
WZ
862}
863
9f0bdab8
DJ
864static void
865ppc_linux_new_thread (ptid_t ptid)
e0d24f8d 866{
9f0bdab8 867 ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value);
e0d24f8d
WZ
868}
869
870static int
9f0bdab8 871ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
e0d24f8d 872{
9f0bdab8 873 struct siginfo *siginfo_p;
e0d24f8d 874
9f0bdab8 875 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
e0d24f8d 876
9f0bdab8
DJ
877 if (siginfo_p->si_signo != SIGTRAP
878 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
e0d24f8d
WZ
879 return 0;
880
407f1a2e 881 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
e0d24f8d
WZ
882 return 1;
883}
884
9f0bdab8
DJ
885static int
886ppc_linux_stopped_by_watchpoint (void)
887{
888 CORE_ADDR addr;
889 return ppc_linux_stopped_data_address (&current_target, &addr);
890}
891
10d6c8cd 892static void
56be3814 893ppc_linux_store_inferior_registers (struct regcache *regcache, int regno)
45229ea4 894{
05f13b9c
EZ
895 /* Overload thread id onto process id */
896 int tid = TIDGET (inferior_ptid);
897
898 /* No thread id, just use process id */
899 if (tid == 0)
900 tid = PIDGET (inferior_ptid);
901
45229ea4 902 if (regno >= 0)
56be3814 903 store_register (regcache, tid, regno);
45229ea4 904 else
56be3814 905 store_ppc_registers (regcache, tid);
45229ea4
EZ
906}
907
f2db237a
AM
908/* Functions for transferring registers between a gregset_t or fpregset_t
909 (see sys/ucontext.h) and gdb's regcache. The word size is that used
910 by the ptrace interface, not the current program's ABI. eg. If a
911 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
912 read or write 64-bit gregsets. This is to suit the host libthread_db. */
913
50c9bd31 914void
7f7fe91e 915supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
c877c8e6 916{
f2db237a 917 const struct regset *regset = ppc_linux_gregset (sizeof (long));
f9be684a 918
f2db237a 919 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
c877c8e6
KB
920}
921
fdb28ac4 922void
7f7fe91e
UW
923fill_gregset (const struct regcache *regcache,
924 gdb_gregset_t *gregsetp, int regno)
fdb28ac4 925{
f2db237a 926 const struct regset *regset = ppc_linux_gregset (sizeof (long));
f9be684a 927
f2db237a
AM
928 if (regno == -1)
929 memset (gregsetp, 0, sizeof (*gregsetp));
930 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
fdb28ac4
KB
931}
932
50c9bd31 933void
7f7fe91e 934supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
c877c8e6 935{
f2db237a
AM
936 const struct regset *regset = ppc_linux_fpregset ();
937
938 ppc_supply_fpregset (regset, regcache, -1,
939 fpregsetp, sizeof (*fpregsetp));
c877c8e6 940}
fdb28ac4 941
fdb28ac4 942void
7f7fe91e
UW
943fill_fpregset (const struct regcache *regcache,
944 gdb_fpregset_t *fpregsetp, int regno)
fdb28ac4 945{
f2db237a
AM
946 const struct regset *regset = ppc_linux_fpregset ();
947
948 ppc_collect_fpregset (regset, regcache, regno,
949 fpregsetp, sizeof (*fpregsetp));
fdb28ac4 950}
10d6c8cd 951
310a98e1
DJ
952static const struct target_desc *
953ppc_linux_read_description (struct target_ops *ops)
954{
955 if (have_ptrace_getsetevrregs)
956 {
957 struct gdb_evrregset_t evrregset;
958 int tid = TIDGET (inferior_ptid);
959
960 if (tid == 0)
961 tid = PIDGET (inferior_ptid);
962
963 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
964 return tdesc_powerpc_e500;
965 else
966 {
967 /* EIO means that the PTRACE_GETEVRREGS request isn't supported. */
968 if (errno == EIO)
969 return NULL;
970 else
971 /* Anything else needs to be reported. */
972 perror_with_name (_("Unable to fetch SPE registers"));
973 }
974 }
975
976 return NULL;
977}
978
10d6c8cd
DJ
979void _initialize_ppc_linux_nat (void);
980
981void
982_initialize_ppc_linux_nat (void)
983{
984 struct target_ops *t;
985
986 /* Fill in the generic GNU/Linux methods. */
987 t = linux_target ();
988
989 /* Add our register access methods. */
990 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
991 t->to_store_registers = ppc_linux_store_inferior_registers;
992
e0d24f8d
WZ
993 /* Add our watchpoint methods. */
994 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
995 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
996 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
997 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
998 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
999 t->to_stopped_data_address = ppc_linux_stopped_data_address;
1000
310a98e1
DJ
1001 t->to_read_description = ppc_linux_read_description;
1002
10d6c8cd 1003 /* Register the target. */
f973ed9c 1004 linux_nat_add_target (t);
9f0bdab8 1005 linux_nat_set_new_thread (t, ppc_linux_new_thread);
10d6c8cd 1006}
This page took 0.744746 seconds and 4 git commands to generate.