Support for Windows OS Thread Information Block.
[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, 2009, 2010 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 /* Required when using the AUXV. */
48 #include "elf/common.h"
49 #include "auxv.h"
50
51 /* This sometimes isn't defined. */
52 #ifndef PT_ORIG_R3
53 #define PT_ORIG_R3 34
54 #endif
55 #ifndef PT_TRAP
56 #define PT_TRAP 40
57 #endif
58
59 /* The PPC_FEATURE_* defines should be provided by <asm/cputable.h>.
60 If they aren't, we can provide them ourselves (their values are fixed
61 because they are part of the kernel ABI). They are used in the AT_HWCAP
62 entry of the AUXV. */
63 #ifndef PPC_FEATURE_CELL
64 #define PPC_FEATURE_CELL 0x00010000
65 #endif
66 #ifndef PPC_FEATURE_BOOKE
67 #define PPC_FEATURE_BOOKE 0x00008000
68 #endif
69 #ifndef PPC_FEATURE_HAS_DFP
70 #define PPC_FEATURE_HAS_DFP 0x00000400 /* Decimal Floating Point. */
71 #endif
72
73 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
74 configure time check. Some older glibc's (for instance 2.2.1)
75 don't have a specific powerpc version of ptrace.h, and fall back on
76 a generic one. In such cases, sys/ptrace.h defines
77 PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
78 ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
79 PTRACE_SETVRREGS to be. This also makes a configury check pretty
80 much useless. */
81
82 /* These definitions should really come from the glibc header files,
83 but Glibc doesn't know about the vrregs yet. */
84 #ifndef PTRACE_GETVRREGS
85 #define PTRACE_GETVRREGS 18
86 #define PTRACE_SETVRREGS 19
87 #endif
88
89 /* PTRACE requests for POWER7 VSX registers. */
90 #ifndef PTRACE_GETVSXREGS
91 #define PTRACE_GETVSXREGS 27
92 #define PTRACE_SETVSXREGS 28
93 #endif
94
95 /* Similarly for the ptrace requests for getting / setting the SPE
96 registers (ev0 -- ev31, acc, and spefscr). See the description of
97 gdb_evrregset_t for details. */
98 #ifndef PTRACE_GETEVRREGS
99 #define PTRACE_GETEVRREGS 20
100 #define PTRACE_SETEVRREGS 21
101 #endif
102
103 /* Similarly for the hardware watchpoint support. */
104 #ifndef PTRACE_GET_DEBUGREG
105 #define PTRACE_GET_DEBUGREG 25
106 #endif
107 #ifndef PTRACE_SET_DEBUGREG
108 #define PTRACE_SET_DEBUGREG 26
109 #endif
110 #ifndef PTRACE_GETSIGINFO
111 #define PTRACE_GETSIGINFO 0x4202
112 #endif
113
114 /* Similarly for the general-purpose (gp0 -- gp31)
115 and floating-point registers (fp0 -- fp31). */
116 #ifndef PTRACE_GETREGS
117 #define PTRACE_GETREGS 12
118 #endif
119 #ifndef PTRACE_SETREGS
120 #define PTRACE_SETREGS 13
121 #endif
122 #ifndef PTRACE_GETFPREGS
123 #define PTRACE_GETFPREGS 14
124 #endif
125 #ifndef PTRACE_SETFPREGS
126 #define PTRACE_SETFPREGS 15
127 #endif
128
129 /* This oddity is because the Linux kernel defines elf_vrregset_t as
130 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
131 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
132 the vrsave as an extra 4 bytes at the end. I opted for creating a
133 flat array of chars, so that it is easier to manipulate for gdb.
134
135 There are 32 vector registers 16 bytes longs, plus a VSCR register
136 which is only 4 bytes long, but is fetched as a 16 bytes
137 quantity. Up to here we have the elf_vrregset_t structure.
138 Appended to this there is space for the VRSAVE register: 4 bytes.
139 Even though this vrsave register is not included in the regset
140 typedef, it is handled by the ptrace requests.
141
142 Note that GNU/Linux doesn't support little endian PPC hardware,
143 therefore the offset at which the real value of the VSCR register
144 is located will be always 12 bytes.
145
146 The layout is like this (where x is the actual value of the vscr reg): */
147
148 /* *INDENT-OFF* */
149 /*
150 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
151 <-------> <-------><-------><->
152 VR0 VR31 VSCR VRSAVE
153 */
154 /* *INDENT-ON* */
155
156 #define SIZEOF_VRREGS 33*16+4
157
158 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
159
160 /* This is the layout of the POWER7 VSX registers and the way they overlap
161 with the existing FPR and VMX registers.
162
163 VSR doubleword 0 VSR doubleword 1
164 ----------------------------------------------------------------
165 VSR[0] | FPR[0] | |
166 ----------------------------------------------------------------
167 VSR[1] | FPR[1] | |
168 ----------------------------------------------------------------
169 | ... | |
170 | ... | |
171 ----------------------------------------------------------------
172 VSR[30] | FPR[30] | |
173 ----------------------------------------------------------------
174 VSR[31] | FPR[31] | |
175 ----------------------------------------------------------------
176 VSR[32] | VR[0] |
177 ----------------------------------------------------------------
178 VSR[33] | VR[1] |
179 ----------------------------------------------------------------
180 | ... |
181 | ... |
182 ----------------------------------------------------------------
183 VSR[62] | VR[30] |
184 ----------------------------------------------------------------
185 VSR[63] | VR[31] |
186 ----------------------------------------------------------------
187
188 VSX has 64 128bit registers. The first 32 registers overlap with
189 the FP registers (doubleword 0) and hence extend them with additional
190 64 bits (doubleword 1). The other 32 regs overlap with the VMX
191 registers. */
192 #define SIZEOF_VSXREGS 32*8
193
194 typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
195
196 /* On PPC processors that support the the Signal Processing Extension
197 (SPE) APU, the general-purpose registers are 64 bits long.
198 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
199 ptrace calls only access the lower half of each register, to allow
200 them to behave the same way they do on non-SPE systems. There's a
201 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
202 read and write the top halves of all the general-purpose registers
203 at once, along with some SPE-specific registers.
204
205 GDB itself continues to claim the general-purpose registers are 32
206 bits long. It has unnamed raw registers that hold the upper halves
207 of the gprs, and the the full 64-bit SIMD views of the registers,
208 'ev0' -- 'ev31', are pseudo-registers that splice the top and
209 bottom halves together.
210
211 This is the structure filled in by PTRACE_GETEVRREGS and written to
212 the inferior's registers by PTRACE_SETEVRREGS. */
213 struct gdb_evrregset_t
214 {
215 unsigned long evr[32];
216 unsigned long long acc;
217 unsigned long spefscr;
218 };
219
220 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
221 PTRACE_SETVSXREGS requests, for reading and writing the VSX
222 POWER7 registers 0 through 31. Zero if we've tried one of them and
223 gotten an error. Note that VSX registers 32 through 63 overlap
224 with VR registers 0 through 31. */
225 int have_ptrace_getsetvsxregs = 1;
226
227 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
228 PTRACE_SETVRREGS requests, for reading and writing the Altivec
229 registers. Zero if we've tried one of them and gotten an
230 error. */
231 int have_ptrace_getvrregs = 1;
232
233 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
234 PTRACE_SETEVRREGS requests, for reading and writing the SPE
235 registers. Zero if we've tried one of them and gotten an
236 error. */
237 int have_ptrace_getsetevrregs = 1;
238
239 /* Non-zero if our kernel may support the PTRACE_GETREGS and
240 PTRACE_SETREGS requests, for reading and writing the
241 general-purpose registers. Zero if we've tried one of
242 them and gotten an error. */
243 int have_ptrace_getsetregs = 1;
244
245 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
246 PTRACE_SETFPREGS requests, for reading and writing the
247 floating-pointers registers. Zero if we've tried one of
248 them and gotten an error. */
249 int have_ptrace_getsetfpregs = 1;
250
251 /* *INDENT-OFF* */
252 /* registers layout, as presented by the ptrace interface:
253 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
254 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
255 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
256 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
257 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
258 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
259 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
260 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
261 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
262 /* *INDENT_ON * */
263
264 static int
265 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
266 {
267 int u_addr = -1;
268 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
269 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
270 interface, and not the wordsize of the program's ABI. */
271 int wordsize = sizeof (long);
272
273 /* General purpose registers occupy 1 slot each in the buffer */
274 if (regno >= tdep->ppc_gp0_regnum
275 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
276 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
277
278 /* Floating point regs: eight bytes each in both 32- and 64-bit
279 ptrace interfaces. Thus, two slots each in 32-bit interface, one
280 slot each in 64-bit interface. */
281 if (tdep->ppc_fp0_regnum >= 0
282 && regno >= tdep->ppc_fp0_regnum
283 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
284 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
285
286 /* UISA special purpose registers: 1 slot each */
287 if (regno == gdbarch_pc_regnum (gdbarch))
288 u_addr = PT_NIP * wordsize;
289 if (regno == tdep->ppc_lr_regnum)
290 u_addr = PT_LNK * wordsize;
291 if (regno == tdep->ppc_cr_regnum)
292 u_addr = PT_CCR * wordsize;
293 if (regno == tdep->ppc_xer_regnum)
294 u_addr = PT_XER * wordsize;
295 if (regno == tdep->ppc_ctr_regnum)
296 u_addr = PT_CTR * wordsize;
297 #ifdef PT_MQ
298 if (regno == tdep->ppc_mq_regnum)
299 u_addr = PT_MQ * wordsize;
300 #endif
301 if (regno == tdep->ppc_ps_regnum)
302 u_addr = PT_MSR * wordsize;
303 if (regno == PPC_ORIG_R3_REGNUM)
304 u_addr = PT_ORIG_R3 * wordsize;
305 if (regno == PPC_TRAP_REGNUM)
306 u_addr = PT_TRAP * wordsize;
307 if (tdep->ppc_fpscr_regnum >= 0
308 && regno == tdep->ppc_fpscr_regnum)
309 {
310 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
311 kernel headers incorrectly contained the 32-bit definition of
312 PT_FPSCR. For the 32-bit definition, floating-point
313 registers occupy two 32-bit "slots", and the FPSCR lives in
314 the second half of such a slot-pair (hence +1). For 64-bit,
315 the FPSCR instead occupies the full 64-bit 2-word-slot and
316 hence no adjustment is necessary. Hack around this. */
317 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
318 u_addr = (48 + 32) * wordsize;
319 /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
320 slot and not just its second word. The PT_FPSCR supplied when
321 GDB is compiled as a 32-bit app doesn't reflect this. */
322 else if (wordsize == 4 && register_size (gdbarch, regno) == 8
323 && PT_FPSCR == (48 + 2*32 + 1))
324 u_addr = (48 + 2*32) * wordsize;
325 else
326 u_addr = PT_FPSCR * wordsize;
327 }
328 return u_addr;
329 }
330
331 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
332 registers set mechanism, as opposed to the interface for all the
333 other registers, that stores/fetches each register individually. */
334 static void
335 fetch_vsx_register (struct regcache *regcache, int tid, int regno)
336 {
337 int ret;
338 gdb_vsxregset_t regs;
339 struct gdbarch *gdbarch = get_regcache_arch (regcache);
340 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
341 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
342
343 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
344 if (ret < 0)
345 {
346 if (errno == EIO)
347 {
348 have_ptrace_getsetvsxregs = 0;
349 return;
350 }
351 perror_with_name (_("Unable to fetch VSX register"));
352 }
353
354 regcache_raw_supply (regcache, regno,
355 regs + (regno - tdep->ppc_vsr0_upper_regnum)
356 * vsxregsize);
357 }
358
359 /* The Linux kernel ptrace interface for AltiVec registers uses the
360 registers set mechanism, as opposed to the interface for all the
361 other registers, that stores/fetches each register individually. */
362 static void
363 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
364 {
365 int ret;
366 int offset = 0;
367 gdb_vrregset_t regs;
368 struct gdbarch *gdbarch = get_regcache_arch (regcache);
369 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
370 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
371
372 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
373 if (ret < 0)
374 {
375 if (errno == EIO)
376 {
377 have_ptrace_getvrregs = 0;
378 return;
379 }
380 perror_with_name (_("Unable to fetch AltiVec register"));
381 }
382
383 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
384 long on the hardware. We deal only with the lower 4 bytes of the
385 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
386 there is no need to define an offset for it. */
387 if (regno == (tdep->ppc_vrsave_regnum - 1))
388 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
389
390 regcache_raw_supply (regcache, regno,
391 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
392 }
393
394 /* Fetch the top 32 bits of TID's general-purpose registers and the
395 SPE-specific registers, and place the results in EVRREGSET. If we
396 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
397 zeros.
398
399 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
400 PTRACE_SETEVRREGS requests are supported is isolated here, and in
401 set_spe_registers. */
402 static void
403 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
404 {
405 if (have_ptrace_getsetevrregs)
406 {
407 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
408 return;
409 else
410 {
411 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
412 we just return zeros. */
413 if (errno == EIO)
414 have_ptrace_getsetevrregs = 0;
415 else
416 /* Anything else needs to be reported. */
417 perror_with_name (_("Unable to fetch SPE registers"));
418 }
419 }
420
421 memset (evrregset, 0, sizeof (*evrregset));
422 }
423
424 /* Supply values from TID for SPE-specific raw registers: the upper
425 halves of the GPRs, the accumulator, and the spefscr. REGNO must
426 be the number of an upper half register, acc, spefscr, or -1 to
427 supply the values of all registers. */
428 static void
429 fetch_spe_register (struct regcache *regcache, int tid, int regno)
430 {
431 struct gdbarch *gdbarch = get_regcache_arch (regcache);
432 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
433 struct gdb_evrregset_t evrregs;
434
435 gdb_assert (sizeof (evrregs.evr[0])
436 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
437 gdb_assert (sizeof (evrregs.acc)
438 == register_size (gdbarch, tdep->ppc_acc_regnum));
439 gdb_assert (sizeof (evrregs.spefscr)
440 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
441
442 get_spe_registers (tid, &evrregs);
443
444 if (regno == -1)
445 {
446 int i;
447
448 for (i = 0; i < ppc_num_gprs; i++)
449 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
450 &evrregs.evr[i]);
451 }
452 else if (tdep->ppc_ev0_upper_regnum <= regno
453 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
454 regcache_raw_supply (regcache, regno,
455 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
456
457 if (regno == -1
458 || regno == tdep->ppc_acc_regnum)
459 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
460
461 if (regno == -1
462 || regno == tdep->ppc_spefscr_regnum)
463 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
464 &evrregs.spefscr);
465 }
466
467 static void
468 fetch_register (struct regcache *regcache, int tid, int regno)
469 {
470 struct gdbarch *gdbarch = get_regcache_arch (regcache);
471 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
472 /* This isn't really an address. But ptrace thinks of it as one. */
473 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
474 int bytes_transferred;
475 unsigned int offset; /* Offset of registers within the u area. */
476 char buf[MAX_REGISTER_SIZE];
477
478 if (altivec_register_p (gdbarch, regno))
479 {
480 /* If this is the first time through, or if it is not the first
481 time through, and we have comfirmed that there is kernel
482 support for such a ptrace request, then go and fetch the
483 register. */
484 if (have_ptrace_getvrregs)
485 {
486 fetch_altivec_register (regcache, tid, regno);
487 return;
488 }
489 /* If we have discovered that there is no ptrace support for
490 AltiVec registers, fall through and return zeroes, because
491 regaddr will be -1 in this case. */
492 }
493 if (vsx_register_p (gdbarch, regno))
494 {
495 if (have_ptrace_getsetvsxregs)
496 {
497 fetch_vsx_register (regcache, tid, regno);
498 return;
499 }
500 }
501 else if (spe_register_p (gdbarch, regno))
502 {
503 fetch_spe_register (regcache, tid, regno);
504 return;
505 }
506
507 if (regaddr == -1)
508 {
509 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
510 regcache_raw_supply (regcache, regno, buf);
511 return;
512 }
513
514 /* Read the raw register using sizeof(long) sized chunks. On a
515 32-bit platform, 64-bit floating-point registers will require two
516 transfers. */
517 for (bytes_transferred = 0;
518 bytes_transferred < register_size (gdbarch, regno);
519 bytes_transferred += sizeof (long))
520 {
521 errno = 0;
522 *(long *) &buf[bytes_transferred]
523 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
524 regaddr += sizeof (long);
525 if (errno != 0)
526 {
527 char message[128];
528 sprintf (message, "reading register %s (#%d)",
529 gdbarch_register_name (gdbarch, regno), regno);
530 perror_with_name (message);
531 }
532 }
533
534 /* Now supply the register. Keep in mind that the regcache's idea
535 of the register's size may not be a multiple of sizeof
536 (long). */
537 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
538 {
539 /* Little-endian values are always found at the left end of the
540 bytes transferred. */
541 regcache_raw_supply (regcache, regno, buf);
542 }
543 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
544 {
545 /* Big-endian values are found at the right end of the bytes
546 transferred. */
547 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
548 regcache_raw_supply (regcache, regno, buf + padding);
549 }
550 else
551 internal_error (__FILE__, __LINE__,
552 _("fetch_register: unexpected byte order: %d"),
553 gdbarch_byte_order (gdbarch));
554 }
555
556 static void
557 supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
558 {
559 int i;
560 struct gdbarch *gdbarch = get_regcache_arch (regcache);
561 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
562 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
563
564 for (i = 0; i < ppc_num_vshrs; i++)
565 {
566 regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
567 *vsxregsetp + i * vsxregsize);
568 }
569 }
570
571 static void
572 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
573 {
574 int i;
575 struct gdbarch *gdbarch = get_regcache_arch (regcache);
576 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
577 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
578 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
579 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
580
581 for (i = 0; i < num_of_vrregs; i++)
582 {
583 /* The last 2 registers of this set are only 32 bit long, not
584 128. However an offset is necessary only for VSCR because it
585 occupies a whole vector, while VRSAVE occupies a full 4 bytes
586 slot. */
587 if (i == (num_of_vrregs - 2))
588 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
589 *vrregsetp + i * vrregsize + offset);
590 else
591 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
592 *vrregsetp + i * vrregsize);
593 }
594 }
595
596 static void
597 fetch_vsx_registers (struct regcache *regcache, int tid)
598 {
599 int ret;
600 gdb_vsxregset_t regs;
601
602 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
603 if (ret < 0)
604 {
605 if (errno == EIO)
606 {
607 have_ptrace_getsetvsxregs = 0;
608 return;
609 }
610 perror_with_name (_("Unable to fetch VSX registers"));
611 }
612 supply_vsxregset (regcache, &regs);
613 }
614
615 static void
616 fetch_altivec_registers (struct regcache *regcache, int tid)
617 {
618 int ret;
619 gdb_vrregset_t regs;
620
621 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
622 if (ret < 0)
623 {
624 if (errno == EIO)
625 {
626 have_ptrace_getvrregs = 0;
627 return;
628 }
629 perror_with_name (_("Unable to fetch AltiVec registers"));
630 }
631 supply_vrregset (regcache, &regs);
632 }
633
634 /* This function actually issues the request to ptrace, telling
635 it to get all general-purpose registers and put them into the
636 specified regset.
637
638 If the ptrace request does not exist, this function returns 0
639 and properly sets the have_ptrace_* flag. If the request fails,
640 this function calls perror_with_name. Otherwise, if the request
641 succeeds, then the regcache gets filled and 1 is returned. */
642 static int
643 fetch_all_gp_regs (struct regcache *regcache, int tid)
644 {
645 struct gdbarch *gdbarch = get_regcache_arch (regcache);
646 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
647 gdb_gregset_t gregset;
648
649 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
650 {
651 if (errno == EIO)
652 {
653 have_ptrace_getsetregs = 0;
654 return 0;
655 }
656 perror_with_name (_("Couldn't get general-purpose registers."));
657 }
658
659 supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
660
661 return 1;
662 }
663
664 /* This is a wrapper for the fetch_all_gp_regs function. It is
665 responsible for verifying if this target has the ptrace request
666 that can be used to fetch all general-purpose registers at one
667 shot. If it doesn't, then we should fetch them using the
668 old-fashioned way, which is to iterate over the registers and
669 request them one by one. */
670 static void
671 fetch_gp_regs (struct regcache *regcache, int tid)
672 {
673 struct gdbarch *gdbarch = get_regcache_arch (regcache);
674 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
675 int i;
676
677 if (have_ptrace_getsetregs)
678 if (fetch_all_gp_regs (regcache, tid))
679 return;
680
681 /* If we've hit this point, it doesn't really matter which
682 architecture we are using. We just need to read the
683 registers in the "old-fashioned way". */
684 for (i = 0; i < ppc_num_gprs; i++)
685 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
686 }
687
688 /* This function actually issues the request to ptrace, telling
689 it to get all floating-point registers and put them into the
690 specified regset.
691
692 If the ptrace request does not exist, this function returns 0
693 and properly sets the have_ptrace_* flag. If the request fails,
694 this function calls perror_with_name. Otherwise, if the request
695 succeeds, then the regcache gets filled and 1 is returned. */
696 static int
697 fetch_all_fp_regs (struct regcache *regcache, int tid)
698 {
699 gdb_fpregset_t fpregs;
700
701 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
702 {
703 if (errno == EIO)
704 {
705 have_ptrace_getsetfpregs = 0;
706 return 0;
707 }
708 perror_with_name (_("Couldn't get floating-point registers."));
709 }
710
711 supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
712
713 return 1;
714 }
715
716 /* This is a wrapper for the fetch_all_fp_regs function. It is
717 responsible for verifying if this target has the ptrace request
718 that can be used to fetch all floating-point registers at one
719 shot. If it doesn't, then we should fetch them using the
720 old-fashioned way, which is to iterate over the registers and
721 request them one by one. */
722 static void
723 fetch_fp_regs (struct regcache *regcache, int tid)
724 {
725 struct gdbarch *gdbarch = get_regcache_arch (regcache);
726 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
727 int i;
728
729 if (have_ptrace_getsetfpregs)
730 if (fetch_all_fp_regs (regcache, tid))
731 return;
732
733 /* If we've hit this point, it doesn't really matter which
734 architecture we are using. We just need to read the
735 registers in the "old-fashioned way". */
736 for (i = 0; i < ppc_num_fprs; i++)
737 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
738 }
739
740 static void
741 fetch_ppc_registers (struct regcache *regcache, int tid)
742 {
743 int i;
744 struct gdbarch *gdbarch = get_regcache_arch (regcache);
745 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
746
747 fetch_gp_regs (regcache, tid);
748 if (tdep->ppc_fp0_regnum >= 0)
749 fetch_fp_regs (regcache, tid);
750 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
751 if (tdep->ppc_ps_regnum != -1)
752 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
753 if (tdep->ppc_cr_regnum != -1)
754 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
755 if (tdep->ppc_lr_regnum != -1)
756 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
757 if (tdep->ppc_ctr_regnum != -1)
758 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
759 if (tdep->ppc_xer_regnum != -1)
760 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
761 if (tdep->ppc_mq_regnum != -1)
762 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
763 if (ppc_linux_trap_reg_p (gdbarch))
764 {
765 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
766 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
767 }
768 if (tdep->ppc_fpscr_regnum != -1)
769 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
770 if (have_ptrace_getvrregs)
771 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
772 fetch_altivec_registers (regcache, tid);
773 if (have_ptrace_getsetvsxregs)
774 if (tdep->ppc_vsr0_upper_regnum != -1)
775 fetch_vsx_registers (regcache, tid);
776 if (tdep->ppc_ev0_upper_regnum >= 0)
777 fetch_spe_register (regcache, tid, -1);
778 }
779
780 /* Fetch registers from the child process. Fetch all registers if
781 regno == -1, otherwise fetch all general registers or all floating
782 point registers depending upon the value of regno. */
783 static void
784 ppc_linux_fetch_inferior_registers (struct target_ops *ops,
785 struct regcache *regcache, int regno)
786 {
787 /* Overload thread id onto process id */
788 int tid = TIDGET (inferior_ptid);
789
790 /* No thread id, just use process id */
791 if (tid == 0)
792 tid = PIDGET (inferior_ptid);
793
794 if (regno == -1)
795 fetch_ppc_registers (regcache, tid);
796 else
797 fetch_register (regcache, tid, regno);
798 }
799
800 /* Store one VSX register. */
801 static void
802 store_vsx_register (const struct regcache *regcache, int tid, int regno)
803 {
804 int ret;
805 gdb_vsxregset_t regs;
806 struct gdbarch *gdbarch = get_regcache_arch (regcache);
807 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
808 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
809
810 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
811 if (ret < 0)
812 {
813 if (errno == EIO)
814 {
815 have_ptrace_getsetvsxregs = 0;
816 return;
817 }
818 perror_with_name (_("Unable to fetch VSX register"));
819 }
820
821 regcache_raw_collect (regcache, regno, regs +
822 (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
823
824 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
825 if (ret < 0)
826 perror_with_name (_("Unable to store VSX register"));
827 }
828
829 /* Store one register. */
830 static void
831 store_altivec_register (const struct regcache *regcache, int tid, int regno)
832 {
833 int ret;
834 int offset = 0;
835 gdb_vrregset_t regs;
836 struct gdbarch *gdbarch = get_regcache_arch (regcache);
837 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
838 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
839
840 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
841 if (ret < 0)
842 {
843 if (errno == EIO)
844 {
845 have_ptrace_getvrregs = 0;
846 return;
847 }
848 perror_with_name (_("Unable to fetch AltiVec register"));
849 }
850
851 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
852 long on the hardware. */
853 if (regno == (tdep->ppc_vrsave_regnum - 1))
854 offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
855
856 regcache_raw_collect (regcache, regno,
857 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
858
859 ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
860 if (ret < 0)
861 perror_with_name (_("Unable to store AltiVec register"));
862 }
863
864 /* Assuming TID referrs to an SPE process, set the top halves of TID's
865 general-purpose registers and its SPE-specific registers to the
866 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
867 nothing.
868
869 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
870 PTRACE_SETEVRREGS requests are supported is isolated here, and in
871 get_spe_registers. */
872 static void
873 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
874 {
875 if (have_ptrace_getsetevrregs)
876 {
877 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
878 return;
879 else
880 {
881 /* EIO means that the PTRACE_SETEVRREGS request isn't
882 supported; we fail silently, and don't try the call
883 again. */
884 if (errno == EIO)
885 have_ptrace_getsetevrregs = 0;
886 else
887 /* Anything else needs to be reported. */
888 perror_with_name (_("Unable to set SPE registers"));
889 }
890 }
891 }
892
893 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
894 If REGNO is -1, write the values of all the SPE-specific
895 registers. */
896 static void
897 store_spe_register (const struct regcache *regcache, int tid, int regno)
898 {
899 struct gdbarch *gdbarch = get_regcache_arch (regcache);
900 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
901 struct gdb_evrregset_t evrregs;
902
903 gdb_assert (sizeof (evrregs.evr[0])
904 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
905 gdb_assert (sizeof (evrregs.acc)
906 == register_size (gdbarch, tdep->ppc_acc_regnum));
907 gdb_assert (sizeof (evrregs.spefscr)
908 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
909
910 if (regno == -1)
911 /* Since we're going to write out every register, the code below
912 should store to every field of evrregs; if that doesn't happen,
913 make it obvious by initializing it with suspicious values. */
914 memset (&evrregs, 42, sizeof (evrregs));
915 else
916 /* We can only read and write the entire EVR register set at a
917 time, so to write just a single register, we do a
918 read-modify-write maneuver. */
919 get_spe_registers (tid, &evrregs);
920
921 if (regno == -1)
922 {
923 int i;
924
925 for (i = 0; i < ppc_num_gprs; i++)
926 regcache_raw_collect (regcache,
927 tdep->ppc_ev0_upper_regnum + i,
928 &evrregs.evr[i]);
929 }
930 else if (tdep->ppc_ev0_upper_regnum <= regno
931 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
932 regcache_raw_collect (regcache, regno,
933 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
934
935 if (regno == -1
936 || regno == tdep->ppc_acc_regnum)
937 regcache_raw_collect (regcache,
938 tdep->ppc_acc_regnum,
939 &evrregs.acc);
940
941 if (regno == -1
942 || regno == tdep->ppc_spefscr_regnum)
943 regcache_raw_collect (regcache,
944 tdep->ppc_spefscr_regnum,
945 &evrregs.spefscr);
946
947 /* Write back the modified register set. */
948 set_spe_registers (tid, &evrregs);
949 }
950
951 static void
952 store_register (const struct regcache *regcache, int tid, int regno)
953 {
954 struct gdbarch *gdbarch = get_regcache_arch (regcache);
955 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
956 /* This isn't really an address. But ptrace thinks of it as one. */
957 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
958 int i;
959 size_t bytes_to_transfer;
960 char buf[MAX_REGISTER_SIZE];
961
962 if (altivec_register_p (gdbarch, regno))
963 {
964 store_altivec_register (regcache, tid, regno);
965 return;
966 }
967 if (vsx_register_p (gdbarch, regno))
968 {
969 store_vsx_register (regcache, tid, regno);
970 return;
971 }
972 else if (spe_register_p (gdbarch, regno))
973 {
974 store_spe_register (regcache, tid, regno);
975 return;
976 }
977
978 if (regaddr == -1)
979 return;
980
981 /* First collect the register. Keep in mind that the regcache's
982 idea of the register's size may not be a multiple of sizeof
983 (long). */
984 memset (buf, 0, sizeof buf);
985 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
986 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
987 {
988 /* Little-endian values always sit at the left end of the buffer. */
989 regcache_raw_collect (regcache, regno, buf);
990 }
991 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
992 {
993 /* Big-endian values sit at the right end of the buffer. */
994 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
995 regcache_raw_collect (regcache, regno, buf + padding);
996 }
997
998 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
999 {
1000 errno = 0;
1001 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
1002 *(long *) &buf[i]);
1003 regaddr += sizeof (long);
1004
1005 if (errno == EIO
1006 && (regno == tdep->ppc_fpscr_regnum
1007 || regno == PPC_ORIG_R3_REGNUM
1008 || regno == PPC_TRAP_REGNUM))
1009 {
1010 /* Some older kernel versions don't allow fpscr, orig_r3
1011 or trap to be written. */
1012 continue;
1013 }
1014
1015 if (errno != 0)
1016 {
1017 char message[128];
1018 sprintf (message, "writing register %s (#%d)",
1019 gdbarch_register_name (gdbarch, regno), regno);
1020 perror_with_name (message);
1021 }
1022 }
1023 }
1024
1025 static void
1026 fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
1027 {
1028 int i;
1029 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1030 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1031 int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
1032
1033 for (i = 0; i < ppc_num_vshrs; i++)
1034 regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
1035 *vsxregsetp + i * vsxregsize);
1036 }
1037
1038 static void
1039 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
1040 {
1041 int i;
1042 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1043 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1044 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
1045 int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
1046 int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
1047
1048 for (i = 0; i < num_of_vrregs; i++)
1049 {
1050 /* The last 2 registers of this set are only 32 bit long, not
1051 128, but only VSCR is fetched as a 16 bytes quantity. */
1052 if (i == (num_of_vrregs - 2))
1053 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1054 *vrregsetp + i * vrregsize + offset);
1055 else
1056 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
1057 *vrregsetp + i * vrregsize);
1058 }
1059 }
1060
1061 static void
1062 store_vsx_registers (const struct regcache *regcache, int tid)
1063 {
1064 int ret;
1065 gdb_vsxregset_t regs;
1066
1067 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
1068 if (ret < 0)
1069 {
1070 if (errno == EIO)
1071 {
1072 have_ptrace_getsetvsxregs = 0;
1073 return;
1074 }
1075 perror_with_name (_("Couldn't get VSX registers"));
1076 }
1077
1078 fill_vsxregset (regcache, &regs);
1079
1080 if (ptrace (PTRACE_SETVSXREGS, tid, 0, &regs) < 0)
1081 perror_with_name (_("Couldn't write VSX registers"));
1082 }
1083
1084 static void
1085 store_altivec_registers (const struct regcache *regcache, int tid)
1086 {
1087 int ret;
1088 gdb_vrregset_t regs;
1089
1090 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
1091 if (ret < 0)
1092 {
1093 if (errno == EIO)
1094 {
1095 have_ptrace_getvrregs = 0;
1096 return;
1097 }
1098 perror_with_name (_("Couldn't get AltiVec registers"));
1099 }
1100
1101 fill_vrregset (regcache, &regs);
1102
1103 if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
1104 perror_with_name (_("Couldn't write AltiVec registers"));
1105 }
1106
1107 /* This function actually issues the request to ptrace, telling
1108 it to store all general-purpose registers present in the specified
1109 regset.
1110
1111 If the ptrace request does not exist, this function returns 0
1112 and properly sets the have_ptrace_* flag. If the request fails,
1113 this function calls perror_with_name. Otherwise, if the request
1114 succeeds, then the regcache is stored and 1 is returned. */
1115 static int
1116 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1117 {
1118 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1119 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1120 gdb_gregset_t gregset;
1121
1122 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1123 {
1124 if (errno == EIO)
1125 {
1126 have_ptrace_getsetregs = 0;
1127 return 0;
1128 }
1129 perror_with_name (_("Couldn't get general-purpose registers."));
1130 }
1131
1132 fill_gregset (regcache, &gregset, regno);
1133
1134 if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
1135 {
1136 if (errno == EIO)
1137 {
1138 have_ptrace_getsetregs = 0;
1139 return 0;
1140 }
1141 perror_with_name (_("Couldn't set general-purpose registers."));
1142 }
1143
1144 return 1;
1145 }
1146
1147 /* This is a wrapper for the store_all_gp_regs function. It is
1148 responsible for verifying if this target has the ptrace request
1149 that can be used to store all general-purpose registers at one
1150 shot. If it doesn't, then we should store them using the
1151 old-fashioned way, which is to iterate over the registers and
1152 store them one by one. */
1153 static void
1154 store_gp_regs (const struct regcache *regcache, int tid, int regno)
1155 {
1156 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1157 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1158 int i;
1159
1160 if (have_ptrace_getsetregs)
1161 if (store_all_gp_regs (regcache, tid, regno))
1162 return;
1163
1164 /* If we hit this point, it doesn't really matter which
1165 architecture we are using. We just need to store the
1166 registers in the "old-fashioned way". */
1167 for (i = 0; i < ppc_num_gprs; i++)
1168 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1169 }
1170
1171 /* This function actually issues the request to ptrace, telling
1172 it to store all floating-point registers present in the specified
1173 regset.
1174
1175 If the ptrace request does not exist, this function returns 0
1176 and properly sets the have_ptrace_* flag. If the request fails,
1177 this function calls perror_with_name. Otherwise, if the request
1178 succeeds, then the regcache is stored and 1 is returned. */
1179 static int
1180 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
1181 {
1182 gdb_fpregset_t fpregs;
1183
1184 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1185 {
1186 if (errno == EIO)
1187 {
1188 have_ptrace_getsetfpregs = 0;
1189 return 0;
1190 }
1191 perror_with_name (_("Couldn't get floating-point registers."));
1192 }
1193
1194 fill_fpregset (regcache, &fpregs, regno);
1195
1196 if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
1197 {
1198 if (errno == EIO)
1199 {
1200 have_ptrace_getsetfpregs = 0;
1201 return 0;
1202 }
1203 perror_with_name (_("Couldn't set floating-point registers."));
1204 }
1205
1206 return 1;
1207 }
1208
1209 /* This is a wrapper for the store_all_fp_regs function. It is
1210 responsible for verifying if this target has the ptrace request
1211 that can be used to store all floating-point registers at one
1212 shot. If it doesn't, then we should store them using the
1213 old-fashioned way, which is to iterate over the registers and
1214 store them one by one. */
1215 static void
1216 store_fp_regs (const struct regcache *regcache, int tid, int regno)
1217 {
1218 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1219 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1220 int i;
1221
1222 if (have_ptrace_getsetfpregs)
1223 if (store_all_fp_regs (regcache, tid, regno))
1224 return;
1225
1226 /* If we hit this point, it doesn't really matter which
1227 architecture we are using. We just need to store the
1228 registers in the "old-fashioned way". */
1229 for (i = 0; i < ppc_num_fprs; i++)
1230 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
1231 }
1232
1233 static void
1234 store_ppc_registers (const struct regcache *regcache, int tid)
1235 {
1236 int i;
1237 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1238 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1239
1240 store_gp_regs (regcache, tid, -1);
1241 if (tdep->ppc_fp0_regnum >= 0)
1242 store_fp_regs (regcache, tid, -1);
1243 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1244 if (tdep->ppc_ps_regnum != -1)
1245 store_register (regcache, tid, tdep->ppc_ps_regnum);
1246 if (tdep->ppc_cr_regnum != -1)
1247 store_register (regcache, tid, tdep->ppc_cr_regnum);
1248 if (tdep->ppc_lr_regnum != -1)
1249 store_register (regcache, tid, tdep->ppc_lr_regnum);
1250 if (tdep->ppc_ctr_regnum != -1)
1251 store_register (regcache, tid, tdep->ppc_ctr_regnum);
1252 if (tdep->ppc_xer_regnum != -1)
1253 store_register (regcache, tid, tdep->ppc_xer_regnum);
1254 if (tdep->ppc_mq_regnum != -1)
1255 store_register (regcache, tid, tdep->ppc_mq_regnum);
1256 if (tdep->ppc_fpscr_regnum != -1)
1257 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
1258 if (ppc_linux_trap_reg_p (gdbarch))
1259 {
1260 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1261 store_register (regcache, tid, PPC_TRAP_REGNUM);
1262 }
1263 if (have_ptrace_getvrregs)
1264 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1265 store_altivec_registers (regcache, tid);
1266 if (have_ptrace_getsetvsxregs)
1267 if (tdep->ppc_vsr0_upper_regnum != -1)
1268 store_vsx_registers (regcache, tid);
1269 if (tdep->ppc_ev0_upper_regnum >= 0)
1270 store_spe_register (regcache, tid, -1);
1271 }
1272
1273 static int
1274 ppc_linux_check_watch_resources (int type, int cnt, int ot)
1275 {
1276 int tid;
1277 ptid_t ptid = inferior_ptid;
1278
1279 /* DABR (data address breakpoint register) is optional for PPC variants.
1280 Some variants have one DABR, others have none. So CNT can't be larger
1281 than 1. */
1282 if (cnt > 1)
1283 return 0;
1284
1285 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
1286 the target has DABR. If either answer is no, the ptrace call will
1287 return -1. Fail in that case. */
1288 tid = TIDGET (ptid);
1289 if (tid == 0)
1290 tid = PIDGET (ptid);
1291
1292 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1293 return 0;
1294 return 1;
1295 }
1296
1297 /* Fetch the AT_HWCAP entry from the aux vector. */
1298 unsigned long ppc_linux_get_hwcap (void)
1299 {
1300 CORE_ADDR field;
1301
1302 if (target_auxv_search (&current_target, AT_HWCAP, &field))
1303 return (unsigned long) field;
1304
1305 return 0;
1306 }
1307
1308 static int
1309 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1310 {
1311 /* Handle sub-8-byte quantities. */
1312 if (len <= 0)
1313 return 0;
1314
1315 /* addr+len must fall in the 8 byte watchable region for DABR-based
1316 processors. DAC-based processors, like the PowerPC 440, will use
1317 addresses aligned to 4-bytes due to the way the read/write flags are
1318 passed at the moment. */
1319 if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1320 && (addr + len) > (addr & ~3) + 4)
1321 || (addr + len) > (addr & ~7) + 8)
1322 return 0;
1323
1324 return 1;
1325 }
1326
1327 /* The cached DABR value, to install in new threads. */
1328 static long saved_dabr_value;
1329
1330 /* Set a watchpoint of type TYPE at address ADDR. */
1331 static int
1332 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
1333 {
1334 struct lwp_info *lp;
1335 ptid_t ptid;
1336 long dabr_value;
1337 long read_mode, write_mode;
1338
1339 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1340 {
1341 /* PowerPC 440 requires only the read/write flags to be passed
1342 to the kernel. */
1343 read_mode = 1;
1344 write_mode = 2;
1345 }
1346 else
1347 {
1348 /* PowerPC 970 and other DABR-based processors are required to pass
1349 the Breakpoint Translation bit together with the flags. */
1350 read_mode = 5;
1351 write_mode = 6;
1352 }
1353
1354 dabr_value = addr & ~(read_mode | write_mode);
1355 switch (rw)
1356 {
1357 case hw_read:
1358 /* Set read and translate bits. */
1359 dabr_value |= read_mode;
1360 break;
1361 case hw_write:
1362 /* Set write and translate bits. */
1363 dabr_value |= write_mode;
1364 break;
1365 case hw_access:
1366 /* Set read, write and translate bits. */
1367 dabr_value |= read_mode | write_mode;
1368 break;
1369 }
1370
1371 saved_dabr_value = dabr_value;
1372
1373 ALL_LWPS (lp, ptid)
1374 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1375 return -1;
1376
1377 return 0;
1378 }
1379
1380 static int
1381 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
1382 {
1383 struct lwp_info *lp;
1384 ptid_t ptid;
1385 long dabr_value = 0;
1386
1387 saved_dabr_value = 0;
1388 ALL_LWPS (lp, ptid)
1389 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
1390 return -1;
1391 return 0;
1392 }
1393
1394 static void
1395 ppc_linux_new_thread (ptid_t ptid)
1396 {
1397 ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value);
1398 }
1399
1400 static int
1401 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1402 {
1403 struct siginfo *siginfo_p;
1404
1405 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
1406
1407 if (siginfo_p->si_signo != SIGTRAP
1408 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1409 return 0;
1410
1411 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
1412 return 1;
1413 }
1414
1415 static int
1416 ppc_linux_stopped_by_watchpoint (void)
1417 {
1418 CORE_ADDR addr;
1419 return ppc_linux_stopped_data_address (&current_target, &addr);
1420 }
1421
1422 static int
1423 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
1424 CORE_ADDR addr,
1425 CORE_ADDR start, int length)
1426 {
1427 int mask;
1428
1429 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1430 mask = 3;
1431 else
1432 mask = 7;
1433
1434 addr &= ~mask;
1435
1436 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
1437 return start <= addr + mask && start + length - 1 >= addr;
1438 }
1439
1440 static void
1441 ppc_linux_store_inferior_registers (struct target_ops *ops,
1442 struct regcache *regcache, int regno)
1443 {
1444 /* Overload thread id onto process id */
1445 int tid = TIDGET (inferior_ptid);
1446
1447 /* No thread id, just use process id */
1448 if (tid == 0)
1449 tid = PIDGET (inferior_ptid);
1450
1451 if (regno >= 0)
1452 store_register (regcache, tid, regno);
1453 else
1454 store_ppc_registers (regcache, tid);
1455 }
1456
1457 /* Functions for transferring registers between a gregset_t or fpregset_t
1458 (see sys/ucontext.h) and gdb's regcache. The word size is that used
1459 by the ptrace interface, not the current program's ABI. eg. If a
1460 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
1461 read or write 64-bit gregsets. This is to suit the host libthread_db. */
1462
1463 void
1464 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1465 {
1466 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1467
1468 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1469 }
1470
1471 void
1472 fill_gregset (const struct regcache *regcache,
1473 gdb_gregset_t *gregsetp, int regno)
1474 {
1475 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1476
1477 if (regno == -1)
1478 memset (gregsetp, 0, sizeof (*gregsetp));
1479 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1480 }
1481
1482 void
1483 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1484 {
1485 const struct regset *regset = ppc_linux_fpregset ();
1486
1487 ppc_supply_fpregset (regset, regcache, -1,
1488 fpregsetp, sizeof (*fpregsetp));
1489 }
1490
1491 void
1492 fill_fpregset (const struct regcache *regcache,
1493 gdb_fpregset_t *fpregsetp, int regno)
1494 {
1495 const struct regset *regset = ppc_linux_fpregset ();
1496
1497 ppc_collect_fpregset (regset, regcache, regno,
1498 fpregsetp, sizeof (*fpregsetp));
1499 }
1500
1501 static int
1502 ppc_linux_target_wordsize (void)
1503 {
1504 int wordsize = 4;
1505
1506 /* Check for 64-bit inferior process. This is the case when the host is
1507 64-bit, and in addition the top bit of the MSR register is set. */
1508 #ifdef __powerpc64__
1509 long msr;
1510
1511 int tid = TIDGET (inferior_ptid);
1512 if (tid == 0)
1513 tid = PIDGET (inferior_ptid);
1514
1515 errno = 0;
1516 msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
1517 if (errno == 0 && msr < 0)
1518 wordsize = 8;
1519 #endif
1520
1521 return wordsize;
1522 }
1523
1524 static int
1525 ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
1526 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
1527 {
1528 int sizeof_auxv_field = ppc_linux_target_wordsize ();
1529 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
1530 gdb_byte *ptr = *readptr;
1531
1532 if (endptr == ptr)
1533 return 0;
1534
1535 if (endptr - ptr < sizeof_auxv_field * 2)
1536 return -1;
1537
1538 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
1539 ptr += sizeof_auxv_field;
1540 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
1541 ptr += sizeof_auxv_field;
1542
1543 *readptr = ptr;
1544 return 1;
1545 }
1546
1547 static const struct target_desc *
1548 ppc_linux_read_description (struct target_ops *ops)
1549 {
1550 int altivec = 0;
1551 int vsx = 0;
1552 int isa205 = 0;
1553 int cell = 0;
1554
1555 int tid = TIDGET (inferior_ptid);
1556 if (tid == 0)
1557 tid = PIDGET (inferior_ptid);
1558
1559 if (have_ptrace_getsetevrregs)
1560 {
1561 struct gdb_evrregset_t evrregset;
1562
1563 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
1564 return tdesc_powerpc_e500l;
1565
1566 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
1567 Anything else needs to be reported. */
1568 else if (errno != EIO)
1569 perror_with_name (_("Unable to fetch SPE registers"));
1570 }
1571
1572 if (have_ptrace_getsetvsxregs)
1573 {
1574 gdb_vsxregset_t vsxregset;
1575
1576 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
1577 vsx = 1;
1578
1579 /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
1580 Anything else needs to be reported. */
1581 else if (errno != EIO)
1582 perror_with_name (_("Unable to fetch VSX registers"));
1583 }
1584
1585 if (have_ptrace_getvrregs)
1586 {
1587 gdb_vrregset_t vrregset;
1588
1589 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
1590 altivec = 1;
1591
1592 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
1593 Anything else needs to be reported. */
1594 else if (errno != EIO)
1595 perror_with_name (_("Unable to fetch AltiVec registers"));
1596 }
1597
1598 /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
1599 the FPSCR from 32 bits to 64 bits. Even though Power 7 supports this
1600 ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
1601 PPC_FEATURE_ARCH_2_06. Since for now the only bits used in the higher
1602 half of the register are for Decimal Floating Point, we check if that
1603 feature is available to decide the size of the FPSCR. */
1604 if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
1605 isa205 = 1;
1606
1607 if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
1608 cell = 1;
1609
1610 if (ppc_linux_target_wordsize () == 8)
1611 {
1612 if (cell)
1613 return tdesc_powerpc_cell64l;
1614 else if (vsx)
1615 return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
1616 else if (altivec)
1617 return isa205? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
1618
1619 return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
1620 }
1621
1622 if (cell)
1623 return tdesc_powerpc_cell32l;
1624 else if (vsx)
1625 return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
1626 else if (altivec)
1627 return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
1628
1629 return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
1630 }
1631
1632 void _initialize_ppc_linux_nat (void);
1633
1634 void
1635 _initialize_ppc_linux_nat (void)
1636 {
1637 struct target_ops *t;
1638
1639 /* Fill in the generic GNU/Linux methods. */
1640 t = linux_target ();
1641
1642 /* Add our register access methods. */
1643 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
1644 t->to_store_registers = ppc_linux_store_inferior_registers;
1645
1646 /* Add our watchpoint methods. */
1647 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
1648 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
1649 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
1650 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
1651 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
1652 t->to_stopped_data_address = ppc_linux_stopped_data_address;
1653 t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
1654
1655 t->to_read_description = ppc_linux_read_description;
1656 t->to_auxv_parse = ppc_linux_auxv_parse;
1657
1658 /* Register the target. */
1659 linux_nat_add_target (t);
1660 linux_nat_set_new_thread (t, ppc_linux_new_thread);
1661 }
This page took 0.181659 seconds and 4 git commands to generate.