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