1e43640b51607bd05e8c7969847c647b2b2246fb
[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 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
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
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
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
81
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
111 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
112
113
114 /* On PPC processors that support the the Signal Processing Extension
115 (SPE) APU, the general-purpose registers are 64 bits long.
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.
122
123 GDB itself continues to claim the general-purpose registers are 32
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.
128
129 This is the structure filled in by PTRACE_GETEVRREGS and written to
130 the inferior's registers by PTRACE_SETEVRREGS. */
131 struct 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. */
143 int have_ptrace_getvrregs = 1;
144
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. */
149 int have_ptrace_getsetevrregs = 1;
150
151 /* *INDENT-OFF* */
152 /* registers layout, as presented by the ptrace interface:
153 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
154 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
155 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
156 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
157 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
158 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
159 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
160 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
161 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
162 /* *INDENT_ON * */
163
164 static int
165 ppc_register_u_addr (int regno)
166 {
167 int u_addr = -1;
168 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
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. */
171 int wordsize = sizeof (long);
172
173 /* General purpose registers occupy 1 slot each in the buffer */
174 if (regno >= tdep->ppc_gp0_regnum
175 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
176 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
177
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. */
181 if (tdep->ppc_fp0_regnum >= 0
182 && regno >= tdep->ppc_fp0_regnum
183 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
184 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
185
186 /* UISA special purpose registers: 1 slot each */
187 if (regno == gdbarch_pc_regnum (current_gdbarch))
188 u_addr = PT_NIP * wordsize;
189 if (regno == tdep->ppc_lr_regnum)
190 u_addr = PT_LNK * wordsize;
191 if (regno == tdep->ppc_cr_regnum)
192 u_addr = PT_CCR * wordsize;
193 if (regno == tdep->ppc_xer_regnum)
194 u_addr = PT_XER * wordsize;
195 if (regno == tdep->ppc_ctr_regnum)
196 u_addr = PT_CTR * wordsize;
197 #ifdef PT_MQ
198 if (regno == tdep->ppc_mq_regnum)
199 u_addr = PT_MQ * wordsize;
200 #endif
201 if (regno == tdep->ppc_ps_regnum)
202 u_addr = PT_MSR * wordsize;
203 if (tdep->ppc_fpscr_regnum >= 0
204 && regno == tdep->ppc_fpscr_regnum)
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 }
218 return u_addr;
219 }
220
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. */
224 static void
225 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
226 {
227 int ret;
228 int offset = 0;
229 gdb_vrregset_t regs;
230 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
231 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
232
233 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
234 if (ret < 0)
235 {
236 if (errno == EIO)
237 {
238 have_ptrace_getvrregs = 0;
239 return;
240 }
241 perror_with_name (_("Unable to fetch AltiVec register"));
242 }
243
244 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
245 long on the hardware. We deal only with the lower 4 bytes of the
246 vector. VRSAVE is at the end of the array in a 4 bytes slot, so
247 there is no need to define an offset for it. */
248 if (regno == (tdep->ppc_vrsave_regnum - 1))
249 offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
250
251 regcache_raw_supply (regcache, regno,
252 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
253 }
254
255 /* Fetch the top 32 bits of TID's general-purpose registers and the
256 SPE-specific registers, and place the results in EVRREGSET. If we
257 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
258 zeros.
259
260 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
261 PTRACE_SETEVRREGS requests are supported is isolated here, and in
262 set_spe_registers. */
263 static void
264 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
265 {
266 if (have_ptrace_getsetevrregs)
267 {
268 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
269 return;
270 else
271 {
272 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
273 we just return zeros. */
274 if (errno == EIO)
275 have_ptrace_getsetevrregs = 0;
276 else
277 /* Anything else needs to be reported. */
278 perror_with_name (_("Unable to fetch SPE registers"));
279 }
280 }
281
282 memset (evrregset, 0, sizeof (*evrregset));
283 }
284
285 /* Supply values from TID for SPE-specific raw registers: the upper
286 halves of the GPRs, the accumulator, and the spefscr. REGNO must
287 be the number of an upper half register, acc, spefscr, or -1 to
288 supply the values of all registers. */
289 static void
290 fetch_spe_register (struct regcache *regcache, int tid, int regno)
291 {
292 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
293 struct gdb_evrregset_t evrregs;
294
295 gdb_assert (sizeof (evrregs.evr[0])
296 == register_size (current_gdbarch, tdep->ppc_ev0_upper_regnum));
297 gdb_assert (sizeof (evrregs.acc)
298 == register_size (current_gdbarch, tdep->ppc_acc_regnum));
299 gdb_assert (sizeof (evrregs.spefscr)
300 == register_size (current_gdbarch, tdep->ppc_spefscr_regnum));
301
302 get_spe_registers (tid, &evrregs);
303
304 if (regno == -1)
305 {
306 int i;
307
308 for (i = 0; i < ppc_num_gprs; i++)
309 regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
310 &evrregs.evr[i]);
311 }
312 else if (tdep->ppc_ev0_upper_regnum <= regno
313 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
314 regcache_raw_supply (regcache, regno,
315 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
316
317 if (regno == -1
318 || regno == tdep->ppc_acc_regnum)
319 regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
320
321 if (regno == -1
322 || regno == tdep->ppc_spefscr_regnum)
323 regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
324 &evrregs.spefscr);
325 }
326
327 static void
328 fetch_register (struct regcache *regcache, int tid, int regno)
329 {
330 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
331 /* This isn't really an address. But ptrace thinks of it as one. */
332 CORE_ADDR regaddr = ppc_register_u_addr (regno);
333 int bytes_transferred;
334 unsigned int offset; /* Offset of registers within the u area. */
335 char buf[MAX_REGISTER_SIZE];
336
337 if (altivec_register_p (regno))
338 {
339 /* If this is the first time through, or if it is not the first
340 time through, and we have comfirmed that there is kernel
341 support for such a ptrace request, then go and fetch the
342 register. */
343 if (have_ptrace_getvrregs)
344 {
345 fetch_altivec_register (regcache, tid, regno);
346 return;
347 }
348 /* If we have discovered that there is no ptrace support for
349 AltiVec registers, fall through and return zeroes, because
350 regaddr will be -1 in this case. */
351 }
352 else if (spe_register_p (regno))
353 {
354 fetch_spe_register (regcache, tid, regno);
355 return;
356 }
357
358 if (regaddr == -1)
359 {
360 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */
361 regcache_raw_supply (regcache, regno, buf);
362 return;
363 }
364
365 /* Read the raw register using sizeof(long) sized chunks. On a
366 32-bit platform, 64-bit floating-point registers will require two
367 transfers. */
368 for (bytes_transferred = 0;
369 bytes_transferred < register_size (current_gdbarch, regno);
370 bytes_transferred += sizeof (long))
371 {
372 errno = 0;
373 *(long *) &buf[bytes_transferred]
374 = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
375 regaddr += sizeof (long);
376 if (errno != 0)
377 {
378 char message[128];
379 sprintf (message, "reading register %s (#%d)",
380 gdbarch_register_name (current_gdbarch, regno), regno);
381 perror_with_name (message);
382 }
383 }
384
385 /* Now supply the register. Keep in mind that the regcache's idea
386 of the register's size may not be a multiple of sizeof
387 (long). */
388 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
389 {
390 /* Little-endian values are always found at the left end of the
391 bytes transferred. */
392 regcache_raw_supply (regcache, regno, buf);
393 }
394 else if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
395 {
396 /* Big-endian values are found at the right end of the bytes
397 transferred. */
398 size_t padding = (bytes_transferred
399 - register_size (current_gdbarch, regno));
400 regcache_raw_supply (regcache, regno, buf + padding);
401 }
402 else
403 internal_error (__FILE__, __LINE__,
404 _("fetch_register: unexpected byte order: %d"),
405 gdbarch_byte_order (current_gdbarch));
406 }
407
408 static void
409 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
410 {
411 int i;
412 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
413 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
414 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
415 int offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
416
417 for (i = 0; i < num_of_vrregs; i++)
418 {
419 /* The last 2 registers of this set are only 32 bit long, not
420 128. However an offset is necessary only for VSCR because it
421 occupies a whole vector, while VRSAVE occupies a full 4 bytes
422 slot. */
423 if (i == (num_of_vrregs - 2))
424 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
425 *vrregsetp + i * vrregsize + offset);
426 else
427 regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
428 *vrregsetp + i * vrregsize);
429 }
430 }
431
432 static void
433 fetch_altivec_registers (struct regcache *regcache, int tid)
434 {
435 int ret;
436 gdb_vrregset_t regs;
437
438 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
439 if (ret < 0)
440 {
441 if (errno == EIO)
442 {
443 have_ptrace_getvrregs = 0;
444 return;
445 }
446 perror_with_name (_("Unable to fetch AltiVec registers"));
447 }
448 supply_vrregset (regcache, &regs);
449 }
450
451 static void
452 fetch_ppc_registers (struct regcache *regcache, int tid)
453 {
454 int i;
455 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
456
457 for (i = 0; i < ppc_num_gprs; i++)
458 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
459 if (tdep->ppc_fp0_regnum >= 0)
460 for (i = 0; i < ppc_num_fprs; i++)
461 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
462 fetch_register (regcache, tid, gdbarch_pc_regnum (current_gdbarch));
463 if (tdep->ppc_ps_regnum != -1)
464 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
465 if (tdep->ppc_cr_regnum != -1)
466 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
467 if (tdep->ppc_lr_regnum != -1)
468 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
469 if (tdep->ppc_ctr_regnum != -1)
470 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
471 if (tdep->ppc_xer_regnum != -1)
472 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
473 if (tdep->ppc_mq_regnum != -1)
474 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
475 if (tdep->ppc_fpscr_regnum != -1)
476 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
477 if (have_ptrace_getvrregs)
478 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
479 fetch_altivec_registers (regcache, tid);
480 if (tdep->ppc_ev0_upper_regnum >= 0)
481 fetch_spe_register (regcache, tid, -1);
482 }
483
484 /* Fetch registers from the child process. Fetch all registers if
485 regno == -1, otherwise fetch all general registers or all floating
486 point registers depending upon the value of regno. */
487 static void
488 ppc_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
489 {
490 /* Overload thread id onto process id */
491 int tid = TIDGET (inferior_ptid);
492
493 /* No thread id, just use process id */
494 if (tid == 0)
495 tid = PIDGET (inferior_ptid);
496
497 if (regno == -1)
498 fetch_ppc_registers (regcache, tid);
499 else
500 fetch_register (regcache, tid, regno);
501 }
502
503 /* Store one register. */
504 static void
505 store_altivec_register (const struct regcache *regcache, int tid, int regno)
506 {
507 int ret;
508 int offset = 0;
509 gdb_vrregset_t regs;
510 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
511 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
512
513 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
514 if (ret < 0)
515 {
516 if (errno == EIO)
517 {
518 have_ptrace_getvrregs = 0;
519 return;
520 }
521 perror_with_name (_("Unable to fetch AltiVec register"));
522 }
523
524 /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
525 long on the hardware. */
526 if (regno == (tdep->ppc_vrsave_regnum - 1))
527 offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
528
529 regcache_raw_collect (regcache, regno,
530 regs + (regno - tdep->ppc_vr0_regnum) * vrregsize + offset);
531
532 ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
533 if (ret < 0)
534 perror_with_name (_("Unable to store AltiVec register"));
535 }
536
537 /* Assuming TID referrs to an SPE process, set the top halves of TID's
538 general-purpose registers and its SPE-specific registers to the
539 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
540 nothing.
541
542 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
543 PTRACE_SETEVRREGS requests are supported is isolated here, and in
544 get_spe_registers. */
545 static void
546 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
547 {
548 if (have_ptrace_getsetevrregs)
549 {
550 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
551 return;
552 else
553 {
554 /* EIO means that the PTRACE_SETEVRREGS request isn't
555 supported; we fail silently, and don't try the call
556 again. */
557 if (errno == EIO)
558 have_ptrace_getsetevrregs = 0;
559 else
560 /* Anything else needs to be reported. */
561 perror_with_name (_("Unable to set SPE registers"));
562 }
563 }
564 }
565
566 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
567 If REGNO is -1, write the values of all the SPE-specific
568 registers. */
569 static void
570 store_spe_register (const struct regcache *regcache, int tid, int regno)
571 {
572 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
573 struct gdb_evrregset_t evrregs;
574
575 gdb_assert (sizeof (evrregs.evr[0])
576 == register_size (current_gdbarch, tdep->ppc_ev0_upper_regnum));
577 gdb_assert (sizeof (evrregs.acc)
578 == register_size (current_gdbarch, tdep->ppc_acc_regnum));
579 gdb_assert (sizeof (evrregs.spefscr)
580 == register_size (current_gdbarch, tdep->ppc_spefscr_regnum));
581
582 if (regno == -1)
583 /* Since we're going to write out every register, the code below
584 should store to every field of evrregs; if that doesn't happen,
585 make it obvious by initializing it with suspicious values. */
586 memset (&evrregs, 42, sizeof (evrregs));
587 else
588 /* We can only read and write the entire EVR register set at a
589 time, so to write just a single register, we do a
590 read-modify-write maneuver. */
591 get_spe_registers (tid, &evrregs);
592
593 if (regno == -1)
594 {
595 int i;
596
597 for (i = 0; i < ppc_num_gprs; i++)
598 regcache_raw_collect (regcache,
599 tdep->ppc_ev0_upper_regnum + i,
600 &evrregs.evr[i]);
601 }
602 else if (tdep->ppc_ev0_upper_regnum <= regno
603 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
604 regcache_raw_collect (regcache, regno,
605 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
606
607 if (regno == -1
608 || regno == tdep->ppc_acc_regnum)
609 regcache_raw_collect (regcache,
610 tdep->ppc_acc_regnum,
611 &evrregs.acc);
612
613 if (regno == -1
614 || regno == tdep->ppc_spefscr_regnum)
615 regcache_raw_collect (regcache,
616 tdep->ppc_spefscr_regnum,
617 &evrregs.spefscr);
618
619 /* Write back the modified register set. */
620 set_spe_registers (tid, &evrregs);
621 }
622
623 static void
624 store_register (const struct regcache *regcache, int tid, int regno)
625 {
626 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
627 /* This isn't really an address. But ptrace thinks of it as one. */
628 CORE_ADDR regaddr = ppc_register_u_addr (regno);
629 int i;
630 size_t bytes_to_transfer;
631 char buf[MAX_REGISTER_SIZE];
632
633 if (altivec_register_p (regno))
634 {
635 store_altivec_register (regcache, tid, regno);
636 return;
637 }
638 else if (spe_register_p (regno))
639 {
640 store_spe_register (regcache, tid, regno);
641 return;
642 }
643
644 if (regaddr == -1)
645 return;
646
647 /* First collect the register. Keep in mind that the regcache's
648 idea of the register's size may not be a multiple of sizeof
649 (long). */
650 memset (buf, 0, sizeof buf);
651 bytes_to_transfer = align_up (register_size (current_gdbarch, regno),
652 sizeof (long));
653 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
654 {
655 /* Little-endian values always sit at the left end of the buffer. */
656 regcache_raw_collect (regcache, regno, buf);
657 }
658 else if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
659 {
660 /* Big-endian values sit at the right end of the buffer. */
661 size_t padding = (bytes_to_transfer
662 - register_size (current_gdbarch, regno));
663 regcache_raw_collect (regcache, regno, buf + padding);
664 }
665
666 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
667 {
668 errno = 0;
669 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
670 *(long *) &buf[i]);
671 regaddr += sizeof (long);
672
673 if (errno == EIO
674 && regno == tdep->ppc_fpscr_regnum)
675 {
676 /* Some older kernel versions don't allow fpscr to be written. */
677 continue;
678 }
679
680 if (errno != 0)
681 {
682 char message[128];
683 sprintf (message, "writing register %s (#%d)",
684 gdbarch_register_name (current_gdbarch, regno), regno);
685 perror_with_name (message);
686 }
687 }
688 }
689
690 static void
691 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
692 {
693 int i;
694 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
695 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
696 int vrregsize = register_size (current_gdbarch, tdep->ppc_vr0_regnum);
697 int offset = vrregsize - register_size (current_gdbarch, tdep->ppc_vrsave_regnum);
698
699 for (i = 0; i < num_of_vrregs; i++)
700 {
701 /* The last 2 registers of this set are only 32 bit long, not
702 128, but only VSCR is fetched as a 16 bytes quantity. */
703 if (i == (num_of_vrregs - 2))
704 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
705 *vrregsetp + i * vrregsize + offset);
706 else
707 regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
708 *vrregsetp + i * vrregsize);
709 }
710 }
711
712 static void
713 store_altivec_registers (const struct regcache *regcache, int tid)
714 {
715 int ret;
716 gdb_vrregset_t regs;
717
718 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
719 if (ret < 0)
720 {
721 if (errno == EIO)
722 {
723 have_ptrace_getvrregs = 0;
724 return;
725 }
726 perror_with_name (_("Couldn't get AltiVec registers"));
727 }
728
729 fill_vrregset (regcache, &regs);
730
731 if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
732 perror_with_name (_("Couldn't write AltiVec registers"));
733 }
734
735 static void
736 store_ppc_registers (const struct regcache *regcache, int tid)
737 {
738 int i;
739 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
740
741 for (i = 0; i < ppc_num_gprs; i++)
742 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
743 if (tdep->ppc_fp0_regnum >= 0)
744 for (i = 0; i < ppc_num_fprs; i++)
745 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
746 store_register (regcache, tid, gdbarch_pc_regnum (current_gdbarch));
747 if (tdep->ppc_ps_regnum != -1)
748 store_register (regcache, tid, tdep->ppc_ps_regnum);
749 if (tdep->ppc_cr_regnum != -1)
750 store_register (regcache, tid, tdep->ppc_cr_regnum);
751 if (tdep->ppc_lr_regnum != -1)
752 store_register (regcache, tid, tdep->ppc_lr_regnum);
753 if (tdep->ppc_ctr_regnum != -1)
754 store_register (regcache, tid, tdep->ppc_ctr_regnum);
755 if (tdep->ppc_xer_regnum != -1)
756 store_register (regcache, tid, tdep->ppc_xer_regnum);
757 if (tdep->ppc_mq_regnum != -1)
758 store_register (regcache, tid, tdep->ppc_mq_regnum);
759 if (tdep->ppc_fpscr_regnum != -1)
760 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
761 if (have_ptrace_getvrregs)
762 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
763 store_altivec_registers (regcache, tid);
764 if (tdep->ppc_ev0_upper_regnum >= 0)
765 store_spe_register (regcache, tid, -1);
766 }
767
768 static int
769 ppc_linux_check_watch_resources (int type, int cnt, int ot)
770 {
771 int tid;
772 ptid_t ptid = inferior_ptid;
773
774 /* DABR (data address breakpoint register) is optional for PPC variants.
775 Some variants have one DABR, others have none. So CNT can't be larger
776 than 1. */
777 if (cnt > 1)
778 return 0;
779
780 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG and whether
781 the target has DABR. If either answer is no, the ptrace call will
782 return -1. Fail in that case. */
783 tid = TIDGET (ptid);
784 if (tid == 0)
785 tid = PIDGET (ptid);
786
787 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
788 return 0;
789 return 1;
790 }
791
792 static int
793 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
794 {
795 /* Handle sub-8-byte quantities. */
796 if (len <= 0)
797 return 0;
798
799 /* addr+len must fall in the 8 byte watchable region. */
800 if ((addr + len) > (addr & ~7) + 8)
801 return 0;
802
803 return 1;
804 }
805
806 /* The cached DABR value, to install in new threads. */
807 static long saved_dabr_value;
808
809 /* Set a watchpoint of type TYPE at address ADDR. */
810 static int
811 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw)
812 {
813 struct lwp_info *lp;
814 ptid_t ptid;
815 long dabr_value;
816
817 dabr_value = addr & ~7;
818 switch (rw)
819 {
820 case hw_read:
821 /* Set read and translate bits. */
822 dabr_value |= 5;
823 break;
824 case hw_write:
825 /* Set write and translate bits. */
826 dabr_value |= 6;
827 break;
828 case hw_access:
829 /* Set read, write and translate bits. */
830 dabr_value |= 7;
831 break;
832 }
833
834 ALL_LWPS (lp, ptid)
835 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
836 return -1;
837 saved_dabr_value = dabr_value;
838 return 0;
839 }
840
841 static int
842 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw)
843 {
844 struct lwp_info *lp;
845 ptid_t ptid;
846 long dabr_value = 0;
847
848 saved_dabr_value = 0;
849 ALL_LWPS (lp, ptid)
850 if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value) < 0)
851 return -1;
852 return 0;
853 }
854
855 static void
856 ppc_linux_new_thread (ptid_t ptid)
857 {
858 ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0, saved_dabr_value);
859 }
860
861 static int
862 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
863 {
864 struct siginfo *siginfo_p;
865
866 siginfo_p = linux_nat_get_siginfo (inferior_ptid);
867
868 if (siginfo_p->si_signo != SIGTRAP
869 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
870 return 0;
871
872 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
873 return 1;
874 }
875
876 static int
877 ppc_linux_stopped_by_watchpoint (void)
878 {
879 CORE_ADDR addr;
880 return ppc_linux_stopped_data_address (&current_target, &addr);
881 }
882
883 static void
884 ppc_linux_store_inferior_registers (struct regcache *regcache, int regno)
885 {
886 /* Overload thread id onto process id */
887 int tid = TIDGET (inferior_ptid);
888
889 /* No thread id, just use process id */
890 if (tid == 0)
891 tid = PIDGET (inferior_ptid);
892
893 if (regno >= 0)
894 store_register (regcache, tid, regno);
895 else
896 store_ppc_registers (regcache, tid);
897 }
898
899 /* Functions for transferring registers between a gregset_t or fpregset_t
900 (see sys/ucontext.h) and gdb's regcache. The word size is that used
901 by the ptrace interface, not the current program's ABI. eg. If a
902 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
903 read or write 64-bit gregsets. This is to suit the host libthread_db. */
904
905 void
906 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
907 {
908 const struct regset *regset = ppc_linux_gregset (sizeof (long));
909
910 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
911 }
912
913 void
914 fill_gregset (const struct regcache *regcache,
915 gdb_gregset_t *gregsetp, int regno)
916 {
917 const struct regset *regset = ppc_linux_gregset (sizeof (long));
918
919 if (regno == -1)
920 memset (gregsetp, 0, sizeof (*gregsetp));
921 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
922 }
923
924 void
925 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
926 {
927 const struct regset *regset = ppc_linux_fpregset ();
928
929 ppc_supply_fpregset (regset, regcache, -1,
930 fpregsetp, sizeof (*fpregsetp));
931 }
932
933 void
934 fill_fpregset (const struct regcache *regcache,
935 gdb_fpregset_t *fpregsetp, int regno)
936 {
937 const struct regset *regset = ppc_linux_fpregset ();
938
939 ppc_collect_fpregset (regset, regcache, regno,
940 fpregsetp, sizeof (*fpregsetp));
941 }
942
943 static const struct target_desc *
944 ppc_linux_read_description (struct target_ops *ops)
945 {
946 if (have_ptrace_getsetevrregs)
947 {
948 struct gdb_evrregset_t evrregset;
949 int tid = TIDGET (inferior_ptid);
950
951 if (tid == 0)
952 tid = PIDGET (inferior_ptid);
953
954 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
955 return tdesc_powerpc_e500;
956 else
957 {
958 /* EIO means that the PTRACE_GETEVRREGS request isn't supported. */
959 if (errno == EIO)
960 return NULL;
961 else
962 /* Anything else needs to be reported. */
963 perror_with_name (_("Unable to fetch SPE registers"));
964 }
965 }
966
967 return NULL;
968 }
969
970 void _initialize_ppc_linux_nat (void);
971
972 void
973 _initialize_ppc_linux_nat (void)
974 {
975 struct target_ops *t;
976
977 /* Fill in the generic GNU/Linux methods. */
978 t = linux_target ();
979
980 /* Add our register access methods. */
981 t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
982 t->to_store_registers = ppc_linux_store_inferior_registers;
983
984 /* Add our watchpoint methods. */
985 t->to_can_use_hw_breakpoint = ppc_linux_check_watch_resources;
986 t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
987 t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
988 t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
989 t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
990 t->to_stopped_data_address = ppc_linux_stopped_data_address;
991
992 t->to_read_description = ppc_linux_read_description;
993
994 /* Register the target. */
995 linux_nat_add_target (t);
996 linux_nat_set_new_thread (t, ppc_linux_new_thread);
997 }