[PowerPC] Add support for PPR and DSCR
[deliverable/binutils-gdb.git] / gdb / ppc-linux-nat.c
1 /* PPC GNU/Linux native support.
2
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "observable.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "gdbthread.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "target.h"
29 #include "linux-nat.h"
30 #include <sys/types.h>
31 #include <signal.h>
32 #include <sys/user.h>
33 #include <sys/ioctl.h>
34 #include <sys/uio.h>
35 #include "gdb_wait.h"
36 #include <fcntl.h>
37 #include <sys/procfs.h>
38 #include "nat/gdb_ptrace.h"
39 #include "inf-ptrace.h"
40
41 /* Prototypes for supply_gregset etc. */
42 #include "gregset.h"
43 #include "ppc-tdep.h"
44 #include "ppc-linux-tdep.h"
45
46 /* Required when using the AUXV. */
47 #include "elf/common.h"
48 #include "auxv.h"
49
50 #include "arch/ppc-linux-common.h"
51 #include "arch/ppc-linux-tdesc.h"
52 #include "nat/ppc-linux.h"
53
54 /* Similarly for the hardware watchpoint support. These requests are used
55 when the PowerPC HWDEBUG ptrace interface is not available. */
56 #ifndef PTRACE_GET_DEBUGREG
57 #define PTRACE_GET_DEBUGREG 25
58 #endif
59 #ifndef PTRACE_SET_DEBUGREG
60 #define PTRACE_SET_DEBUGREG 26
61 #endif
62 #ifndef PTRACE_GETSIGINFO
63 #define PTRACE_GETSIGINFO 0x4202
64 #endif
65
66 /* These requests are used when the PowerPC HWDEBUG ptrace interface is
67 available. It exposes the debug facilities of PowerPC processors, as well
68 as additional features of BookE processors, such as ranged breakpoints and
69 watchpoints and hardware-accelerated condition evaluation. */
70 #ifndef PPC_PTRACE_GETHWDBGINFO
71
72 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG
73 ptrace interface is not present in ptrace.h, so we'll have to pretty much
74 include it all here so that the code at least compiles on older systems. */
75 #define PPC_PTRACE_GETHWDBGINFO 0x89
76 #define PPC_PTRACE_SETHWDEBUG 0x88
77 #define PPC_PTRACE_DELHWDEBUG 0x87
78
79 struct ppc_debug_info
80 {
81 uint32_t version; /* Only version 1 exists to date. */
82 uint32_t num_instruction_bps;
83 uint32_t num_data_bps;
84 uint32_t num_condition_regs;
85 uint32_t data_bp_alignment;
86 uint32_t sizeof_condition; /* size of the DVC register. */
87 uint64_t features;
88 };
89
90 /* Features will have bits indicating whether there is support for: */
91 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE 0x1
92 #define PPC_DEBUG_FEATURE_INSN_BP_MASK 0x2
93 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE 0x4
94 #define PPC_DEBUG_FEATURE_DATA_BP_MASK 0x8
95
96 struct ppc_hw_breakpoint
97 {
98 uint32_t version; /* currently, version must be 1 */
99 uint32_t trigger_type; /* only some combinations allowed */
100 uint32_t addr_mode; /* address match mode */
101 uint32_t condition_mode; /* break/watchpoint condition flags */
102 uint64_t addr; /* break/watchpoint address */
103 uint64_t addr2; /* range end or mask */
104 uint64_t condition_value; /* contents of the DVC register */
105 };
106
107 /* Trigger type. */
108 #define PPC_BREAKPOINT_TRIGGER_EXECUTE 0x1
109 #define PPC_BREAKPOINT_TRIGGER_READ 0x2
110 #define PPC_BREAKPOINT_TRIGGER_WRITE 0x4
111 #define PPC_BREAKPOINT_TRIGGER_RW 0x6
112
113 /* Address mode. */
114 #define PPC_BREAKPOINT_MODE_EXACT 0x0
115 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE 0x1
116 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE 0x2
117 #define PPC_BREAKPOINT_MODE_MASK 0x3
118
119 /* Condition mode. */
120 #define PPC_BREAKPOINT_CONDITION_NONE 0x0
121 #define PPC_BREAKPOINT_CONDITION_AND 0x1
122 #define PPC_BREAKPOINT_CONDITION_EXACT 0x1
123 #define PPC_BREAKPOINT_CONDITION_OR 0x2
124 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
125 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
126 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT 16
127 #define PPC_BREAKPOINT_CONDITION_BE(n) \
128 (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
129 #endif /* PPC_PTRACE_GETHWDBGINFO */
130
131 /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
132 watchpoint (up to 512 bytes). */
133 #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
134 #define PPC_DEBUG_FEATURE_DATA_BP_DAWR 0x10
135 #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
136
137 /* Similarly for the general-purpose (gp0 -- gp31)
138 and floating-point registers (fp0 -- fp31). */
139 #ifndef PTRACE_GETREGS
140 #define PTRACE_GETREGS 12
141 #endif
142 #ifndef PTRACE_SETREGS
143 #define PTRACE_SETREGS 13
144 #endif
145 #ifndef PTRACE_GETFPREGS
146 #define PTRACE_GETFPREGS 14
147 #endif
148 #ifndef PTRACE_SETFPREGS
149 #define PTRACE_SETFPREGS 15
150 #endif
151
152 /* This oddity is because the Linux kernel defines elf_vrregset_t as
153 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
154 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
155 the vrsave as an extra 4 bytes at the end. I opted for creating a
156 flat array of chars, so that it is easier to manipulate for gdb.
157
158 There are 32 vector registers 16 bytes longs, plus a VSCR register
159 which is only 4 bytes long, but is fetched as a 16 bytes
160 quantity. Up to here we have the elf_vrregset_t structure.
161 Appended to this there is space for the VRSAVE register: 4 bytes.
162 Even though this vrsave register is not included in the regset
163 typedef, it is handled by the ptrace requests.
164
165 The layout is like this (where x is the actual value of the vscr reg): */
166
167 /* *INDENT-OFF* */
168 /*
169 Big-Endian:
170 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
171 <-------> <-------><-------><->
172 VR0 VR31 VSCR VRSAVE
173 Little-Endian:
174 |.|.|.|.|.....|.|.|.|.||X|.|.|.||.|
175 <-------> <-------><-------><->
176 VR0 VR31 VSCR VRSAVE
177 */
178 /* *INDENT-ON* */
179
180 typedef char gdb_vrregset_t[PPC_LINUX_SIZEOF_VRREGSET];
181
182 /* This is the layout of the POWER7 VSX registers and the way they overlap
183 with the existing FPR and VMX registers.
184
185 VSR doubleword 0 VSR doubleword 1
186 ----------------------------------------------------------------
187 VSR[0] | FPR[0] | |
188 ----------------------------------------------------------------
189 VSR[1] | FPR[1] | |
190 ----------------------------------------------------------------
191 | ... | |
192 | ... | |
193 ----------------------------------------------------------------
194 VSR[30] | FPR[30] | |
195 ----------------------------------------------------------------
196 VSR[31] | FPR[31] | |
197 ----------------------------------------------------------------
198 VSR[32] | VR[0] |
199 ----------------------------------------------------------------
200 VSR[33] | VR[1] |
201 ----------------------------------------------------------------
202 | ... |
203 | ... |
204 ----------------------------------------------------------------
205 VSR[62] | VR[30] |
206 ----------------------------------------------------------------
207 VSR[63] | VR[31] |
208 ----------------------------------------------------------------
209
210 VSX has 64 128bit registers. The first 32 registers overlap with
211 the FP registers (doubleword 0) and hence extend them with additional
212 64 bits (doubleword 1). The other 32 regs overlap with the VMX
213 registers. */
214 typedef char gdb_vsxregset_t[PPC_LINUX_SIZEOF_VSXREGSET];
215
216 /* On PPC processors that support the Signal Processing Extension
217 (SPE) APU, the general-purpose registers are 64 bits long.
218 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
219 ptrace calls only access the lower half of each register, to allow
220 them to behave the same way they do on non-SPE systems. There's a
221 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
222 read and write the top halves of all the general-purpose registers
223 at once, along with some SPE-specific registers.
224
225 GDB itself continues to claim the general-purpose registers are 32
226 bits long. It has unnamed raw registers that hold the upper halves
227 of the gprs, and the full 64-bit SIMD views of the registers,
228 'ev0' -- 'ev31', are pseudo-registers that splice the top and
229 bottom halves together.
230
231 This is the structure filled in by PTRACE_GETEVRREGS and written to
232 the inferior's registers by PTRACE_SETEVRREGS. */
233 struct gdb_evrregset_t
234 {
235 unsigned long evr[32];
236 unsigned long long acc;
237 unsigned long spefscr;
238 };
239
240 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
241 PTRACE_SETVSXREGS requests, for reading and writing the VSX
242 POWER7 registers 0 through 31. Zero if we've tried one of them and
243 gotten an error. Note that VSX registers 32 through 63 overlap
244 with VR registers 0 through 31. */
245 int have_ptrace_getsetvsxregs = 1;
246
247 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
248 PTRACE_SETVRREGS requests, for reading and writing the Altivec
249 registers. Zero if we've tried one of them and gotten an
250 error. */
251 int have_ptrace_getvrregs = 1;
252
253 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
254 PTRACE_SETEVRREGS requests, for reading and writing the SPE
255 registers. Zero if we've tried one of them and gotten an
256 error. */
257 int have_ptrace_getsetevrregs = 1;
258
259 /* Non-zero if our kernel may support the PTRACE_GETREGS and
260 PTRACE_SETREGS requests, for reading and writing the
261 general-purpose registers. Zero if we've tried one of
262 them and gotten an error. */
263 int have_ptrace_getsetregs = 1;
264
265 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
266 PTRACE_SETFPREGS requests, for reading and writing the
267 floating-pointers registers. Zero if we've tried one of
268 them and gotten an error. */
269 int have_ptrace_getsetfpregs = 1;
270
271 struct ppc_linux_nat_target final : public linux_nat_target
272 {
273 /* Add our register access methods. */
274 void fetch_registers (struct regcache *, int) override;
275 void store_registers (struct regcache *, int) override;
276
277 /* Add our breakpoint/watchpoint methods. */
278 int can_use_hw_breakpoint (enum bptype, int, int) override;
279
280 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
281 override;
282
283 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
284 override;
285
286 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
287
288 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
289 struct expression *) override;
290
291 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
292 struct expression *) override;
293
294 int insert_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
295 override;
296
297 int remove_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
298 override;
299
300 bool stopped_by_watchpoint () override;
301
302 bool stopped_data_address (CORE_ADDR *) override;
303
304 bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
305
306 bool can_accel_watchpoint_condition (CORE_ADDR, int, int, struct expression *)
307 override;
308
309 int masked_watch_num_registers (CORE_ADDR, CORE_ADDR) override;
310
311 int ranged_break_num_registers () override;
312
313 const struct target_desc *read_description () override;
314
315 int auxv_parse (gdb_byte **readptr,
316 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
317 override;
318
319 /* Override linux_nat_target low methods. */
320 void low_new_thread (struct lwp_info *lp) override;
321 };
322
323 static ppc_linux_nat_target the_ppc_linux_nat_target;
324
325 /* *INDENT-OFF* */
326 /* registers layout, as presented by the ptrace interface:
327 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
328 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
329 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
330 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
331 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
332 PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
333 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
334 PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
335 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
336 PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
337 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
338 PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
339 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
340 /* *INDENT_ON * */
341
342 static int
343 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
344 {
345 int u_addr = -1;
346 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
347 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
348 interface, and not the wordsize of the program's ABI. */
349 int wordsize = sizeof (long);
350
351 /* General purpose registers occupy 1 slot each in the buffer. */
352 if (regno >= tdep->ppc_gp0_regnum
353 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
354 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
355
356 /* Floating point regs: eight bytes each in both 32- and 64-bit
357 ptrace interfaces. Thus, two slots each in 32-bit interface, one
358 slot each in 64-bit interface. */
359 if (tdep->ppc_fp0_regnum >= 0
360 && regno >= tdep->ppc_fp0_regnum
361 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
362 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
363
364 /* UISA special purpose registers: 1 slot each. */
365 if (regno == gdbarch_pc_regnum (gdbarch))
366 u_addr = PT_NIP * wordsize;
367 if (regno == tdep->ppc_lr_regnum)
368 u_addr = PT_LNK * wordsize;
369 if (regno == tdep->ppc_cr_regnum)
370 u_addr = PT_CCR * wordsize;
371 if (regno == tdep->ppc_xer_regnum)
372 u_addr = PT_XER * wordsize;
373 if (regno == tdep->ppc_ctr_regnum)
374 u_addr = PT_CTR * wordsize;
375 #ifdef PT_MQ
376 if (regno == tdep->ppc_mq_regnum)
377 u_addr = PT_MQ * wordsize;
378 #endif
379 if (regno == tdep->ppc_ps_regnum)
380 u_addr = PT_MSR * wordsize;
381 if (regno == PPC_ORIG_R3_REGNUM)
382 u_addr = PT_ORIG_R3 * wordsize;
383 if (regno == PPC_TRAP_REGNUM)
384 u_addr = PT_TRAP * wordsize;
385 if (tdep->ppc_fpscr_regnum >= 0
386 && regno == tdep->ppc_fpscr_regnum)
387 {
388 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
389 kernel headers incorrectly contained the 32-bit definition of
390 PT_FPSCR. For the 32-bit definition, floating-point
391 registers occupy two 32-bit "slots", and the FPSCR lives in
392 the second half of such a slot-pair (hence +1). For 64-bit,
393 the FPSCR instead occupies the full 64-bit 2-word-slot and
394 hence no adjustment is necessary. Hack around this. */
395 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
396 u_addr = (48 + 32) * wordsize;
397 /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
398 slot and not just its second word. The PT_FPSCR supplied when
399 GDB is compiled as a 32-bit app doesn't reflect this. */
400 else if (wordsize == 4 && register_size (gdbarch, regno) == 8
401 && PT_FPSCR == (48 + 2*32 + 1))
402 u_addr = (48 + 2*32) * wordsize;
403 else
404 u_addr = PT_FPSCR * wordsize;
405 }
406 return u_addr;
407 }
408
409 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
410 registers set mechanism, as opposed to the interface for all the
411 other registers, that stores/fetches each register individually. */
412 static void
413 fetch_vsx_registers (struct regcache *regcache, int tid, int regno)
414 {
415 int ret;
416 gdb_vsxregset_t regs;
417 const struct regset *vsxregset = ppc_linux_vsxregset ();
418
419 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
420 if (ret < 0)
421 {
422 if (errno == EIO)
423 {
424 have_ptrace_getsetvsxregs = 0;
425 return;
426 }
427 perror_with_name (_("Unable to fetch VSX registers"));
428 }
429
430 vsxregset->supply_regset (vsxregset, regcache, regno, &regs,
431 PPC_LINUX_SIZEOF_VSXREGSET);
432 }
433
434 /* The Linux kernel ptrace interface for AltiVec registers uses the
435 registers set mechanism, as opposed to the interface for all the
436 other registers, that stores/fetches each register individually. */
437 static void
438 fetch_altivec_registers (struct regcache *regcache, int tid,
439 int regno)
440 {
441 int ret;
442 gdb_vrregset_t regs;
443 struct gdbarch *gdbarch = regcache->arch ();
444 const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
445
446 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
447 if (ret < 0)
448 {
449 if (errno == EIO)
450 {
451 have_ptrace_getvrregs = 0;
452 return;
453 }
454 perror_with_name (_("Unable to fetch AltiVec registers"));
455 }
456
457 vrregset->supply_regset (vrregset, regcache, regno, &regs,
458 PPC_LINUX_SIZEOF_VRREGSET);
459 }
460
461 /* Fetch the top 32 bits of TID's general-purpose registers and the
462 SPE-specific registers, and place the results in EVRREGSET. If we
463 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
464 zeros.
465
466 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
467 PTRACE_SETEVRREGS requests are supported is isolated here, and in
468 set_spe_registers. */
469 static void
470 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
471 {
472 if (have_ptrace_getsetevrregs)
473 {
474 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
475 return;
476 else
477 {
478 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
479 we just return zeros. */
480 if (errno == EIO)
481 have_ptrace_getsetevrregs = 0;
482 else
483 /* Anything else needs to be reported. */
484 perror_with_name (_("Unable to fetch SPE registers"));
485 }
486 }
487
488 memset (evrregset, 0, sizeof (*evrregset));
489 }
490
491 /* Supply values from TID for SPE-specific raw registers: the upper
492 halves of the GPRs, the accumulator, and the spefscr. REGNO must
493 be the number of an upper half register, acc, spefscr, or -1 to
494 supply the values of all registers. */
495 static void
496 fetch_spe_register (struct regcache *regcache, int tid, int regno)
497 {
498 struct gdbarch *gdbarch = regcache->arch ();
499 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
500 struct gdb_evrregset_t evrregs;
501
502 gdb_assert (sizeof (evrregs.evr[0])
503 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
504 gdb_assert (sizeof (evrregs.acc)
505 == register_size (gdbarch, tdep->ppc_acc_regnum));
506 gdb_assert (sizeof (evrregs.spefscr)
507 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
508
509 get_spe_registers (tid, &evrregs);
510
511 if (regno == -1)
512 {
513 int i;
514
515 for (i = 0; i < ppc_num_gprs; i++)
516 regcache->raw_supply (tdep->ppc_ev0_upper_regnum + i, &evrregs.evr[i]);
517 }
518 else if (tdep->ppc_ev0_upper_regnum <= regno
519 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
520 regcache->raw_supply (regno,
521 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
522
523 if (regno == -1
524 || regno == tdep->ppc_acc_regnum)
525 regcache->raw_supply (tdep->ppc_acc_regnum, &evrregs.acc);
526
527 if (regno == -1
528 || regno == tdep->ppc_spefscr_regnum)
529 regcache->raw_supply (tdep->ppc_spefscr_regnum, &evrregs.spefscr);
530 }
531
532 /* Use ptrace to fetch all registers from the register set with note
533 type REGSET_ID, size REGSIZE, and layout described by REGSET, from
534 process/thread TID and supply their values to REGCACHE. If ptrace
535 returns ENODATA to indicate the regset is unavailable, mark the
536 registers as unavailable in REGCACHE. */
537
538 static void
539 fetch_regset (struct regcache *regcache, int tid,
540 int regset_id, int regsetsize, const struct regset *regset)
541 {
542 void *buf = alloca (regsetsize);
543 struct iovec iov;
544
545 iov.iov_base = buf;
546 iov.iov_len = regsetsize;
547
548 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
549 {
550 if (errno == ENODATA)
551 regset->supply_regset (regset, regcache, -1, NULL, regsetsize);
552 else
553 perror_with_name (_("Couldn't get register set"));
554 }
555 else
556 regset->supply_regset (regset, regcache, -1, buf, regsetsize);
557 }
558
559 /* Use ptrace to store register REGNUM of the regset with note type
560 REGSET_ID, size REGSETSIZE, and layout described by REGSET, from
561 REGCACHE back to process/thread TID. If REGNUM is -1 all registers
562 in the set are collected and stored. */
563
564 static void
565 store_regset (const struct regcache *regcache, int tid, int regnum,
566 int regset_id, int regsetsize, const struct regset *regset)
567 {
568 void *buf = alloca (regsetsize);
569 struct iovec iov;
570
571 iov.iov_base = buf;
572 iov.iov_len = regsetsize;
573
574 /* Make sure that the buffer that will be stored has up to date values
575 for the registers that won't be collected. */
576 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
577 perror_with_name (_("Couldn't get register set"));
578
579 regset->collect_regset (regset, regcache, regnum, buf, regsetsize);
580
581 if (ptrace (PTRACE_SETREGSET, tid, regset_id, &iov) < 0)
582 perror_with_name (_("Couldn't set register set"));
583 }
584
585 /* Check whether the kernel provides a register set with number
586 REGSET_ID of size REGSETSIZE for process/thread TID. */
587
588 static bool
589 check_regset (int tid, int regset_id, int regsetsize)
590 {
591 void *buf = alloca (regsetsize);
592 struct iovec iov;
593
594 iov.iov_base = buf;
595 iov.iov_len = regsetsize;
596
597 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0
598 || errno == ENODATA)
599 return true;
600 else
601 return false;
602 }
603
604 static void
605 fetch_register (struct regcache *regcache, int tid, int regno)
606 {
607 struct gdbarch *gdbarch = regcache->arch ();
608 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
609 /* This isn't really an address. But ptrace thinks of it as one. */
610 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
611 int bytes_transferred;
612 gdb_byte buf[PPC_MAX_REGISTER_SIZE];
613
614 if (altivec_register_p (gdbarch, regno))
615 {
616 /* If this is the first time through, or if it is not the first
617 time through, and we have comfirmed that there is kernel
618 support for such a ptrace request, then go and fetch the
619 register. */
620 if (have_ptrace_getvrregs)
621 {
622 fetch_altivec_registers (regcache, tid, regno);
623 return;
624 }
625 /* If we have discovered that there is no ptrace support for
626 AltiVec registers, fall through and return zeroes, because
627 regaddr will be -1 in this case. */
628 }
629 else if (vsx_register_p (gdbarch, regno))
630 {
631 if (have_ptrace_getsetvsxregs)
632 {
633 fetch_vsx_registers (regcache, tid, regno);
634 return;
635 }
636 }
637 else if (spe_register_p (gdbarch, regno))
638 {
639 fetch_spe_register (regcache, tid, regno);
640 return;
641 }
642 else if (regno == PPC_DSCR_REGNUM)
643 {
644 gdb_assert (tdep->ppc_dscr_regnum != -1);
645
646 fetch_regset (regcache, tid, NT_PPC_DSCR,
647 PPC_LINUX_SIZEOF_DSCRREGSET,
648 &ppc32_linux_dscrregset);
649 return;
650 }
651 else if (regno == PPC_PPR_REGNUM)
652 {
653 gdb_assert (tdep->ppc_ppr_regnum != -1);
654
655 fetch_regset (regcache, tid, NT_PPC_PPR,
656 PPC_LINUX_SIZEOF_PPRREGSET,
657 &ppc32_linux_pprregset);
658 return;
659 }
660
661 if (regaddr == -1)
662 {
663 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
664 regcache->raw_supply (regno, buf);
665 return;
666 }
667
668 /* Read the raw register using sizeof(long) sized chunks. On a
669 32-bit platform, 64-bit floating-point registers will require two
670 transfers. */
671 for (bytes_transferred = 0;
672 bytes_transferred < register_size (gdbarch, regno);
673 bytes_transferred += sizeof (long))
674 {
675 long l;
676
677 errno = 0;
678 l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
679 regaddr += sizeof (long);
680 if (errno != 0)
681 {
682 char message[128];
683 xsnprintf (message, sizeof (message), "reading register %s (#%d)",
684 gdbarch_register_name (gdbarch, regno), regno);
685 perror_with_name (message);
686 }
687 memcpy (&buf[bytes_transferred], &l, sizeof (l));
688 }
689
690 /* Now supply the register. Keep in mind that the regcache's idea
691 of the register's size may not be a multiple of sizeof
692 (long). */
693 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
694 {
695 /* Little-endian values are always found at the left end of the
696 bytes transferred. */
697 regcache->raw_supply (regno, buf);
698 }
699 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
700 {
701 /* Big-endian values are found at the right end of the bytes
702 transferred. */
703 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
704 regcache->raw_supply (regno, buf + padding);
705 }
706 else
707 internal_error (__FILE__, __LINE__,
708 _("fetch_register: unexpected byte order: %d"),
709 gdbarch_byte_order (gdbarch));
710 }
711
712 /* This function actually issues the request to ptrace, telling
713 it to get all general-purpose registers and put them into the
714 specified regset.
715
716 If the ptrace request does not exist, this function returns 0
717 and properly sets the have_ptrace_* flag. If the request fails,
718 this function calls perror_with_name. Otherwise, if the request
719 succeeds, then the regcache gets filled and 1 is returned. */
720 static int
721 fetch_all_gp_regs (struct regcache *regcache, int tid)
722 {
723 gdb_gregset_t gregset;
724
725 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
726 {
727 if (errno == EIO)
728 {
729 have_ptrace_getsetregs = 0;
730 return 0;
731 }
732 perror_with_name (_("Couldn't get general-purpose registers."));
733 }
734
735 supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
736
737 return 1;
738 }
739
740 /* This is a wrapper for the fetch_all_gp_regs function. It is
741 responsible for verifying if this target has the ptrace request
742 that can be used to fetch all general-purpose registers at one
743 shot. If it doesn't, then we should fetch them using the
744 old-fashioned way, which is to iterate over the registers and
745 request them one by one. */
746 static void
747 fetch_gp_regs (struct regcache *regcache, int tid)
748 {
749 struct gdbarch *gdbarch = regcache->arch ();
750 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
751 int i;
752
753 if (have_ptrace_getsetregs)
754 if (fetch_all_gp_regs (regcache, tid))
755 return;
756
757 /* If we've hit this point, it doesn't really matter which
758 architecture we are using. We just need to read the
759 registers in the "old-fashioned way". */
760 for (i = 0; i < ppc_num_gprs; i++)
761 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
762 }
763
764 /* This function actually issues the request to ptrace, telling
765 it to get all floating-point registers and put them into the
766 specified regset.
767
768 If the ptrace request does not exist, this function returns 0
769 and properly sets the have_ptrace_* flag. If the request fails,
770 this function calls perror_with_name. Otherwise, if the request
771 succeeds, then the regcache gets filled and 1 is returned. */
772 static int
773 fetch_all_fp_regs (struct regcache *regcache, int tid)
774 {
775 gdb_fpregset_t fpregs;
776
777 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
778 {
779 if (errno == EIO)
780 {
781 have_ptrace_getsetfpregs = 0;
782 return 0;
783 }
784 perror_with_name (_("Couldn't get floating-point registers."));
785 }
786
787 supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
788
789 return 1;
790 }
791
792 /* This is a wrapper for the fetch_all_fp_regs function. It is
793 responsible for verifying if this target has the ptrace request
794 that can be used to fetch all floating-point registers at one
795 shot. If it doesn't, then we should fetch them using the
796 old-fashioned way, which is to iterate over the registers and
797 request them one by one. */
798 static void
799 fetch_fp_regs (struct regcache *regcache, int tid)
800 {
801 struct gdbarch *gdbarch = regcache->arch ();
802 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
803 int i;
804
805 if (have_ptrace_getsetfpregs)
806 if (fetch_all_fp_regs (regcache, tid))
807 return;
808
809 /* If we've hit this point, it doesn't really matter which
810 architecture we are using. We just need to read the
811 registers in the "old-fashioned way". */
812 for (i = 0; i < ppc_num_fprs; i++)
813 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
814 }
815
816 static void
817 fetch_ppc_registers (struct regcache *regcache, int tid)
818 {
819 struct gdbarch *gdbarch = regcache->arch ();
820 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
821
822 fetch_gp_regs (regcache, tid);
823 if (tdep->ppc_fp0_regnum >= 0)
824 fetch_fp_regs (regcache, tid);
825 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
826 if (tdep->ppc_ps_regnum != -1)
827 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
828 if (tdep->ppc_cr_regnum != -1)
829 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
830 if (tdep->ppc_lr_regnum != -1)
831 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
832 if (tdep->ppc_ctr_regnum != -1)
833 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
834 if (tdep->ppc_xer_regnum != -1)
835 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
836 if (tdep->ppc_mq_regnum != -1)
837 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
838 if (ppc_linux_trap_reg_p (gdbarch))
839 {
840 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
841 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
842 }
843 if (tdep->ppc_fpscr_regnum != -1)
844 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
845 if (have_ptrace_getvrregs)
846 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
847 fetch_altivec_registers (regcache, tid, -1);
848 if (have_ptrace_getsetvsxregs)
849 if (tdep->ppc_vsr0_upper_regnum != -1)
850 fetch_vsx_registers (regcache, tid, -1);
851 if (tdep->ppc_ev0_upper_regnum >= 0)
852 fetch_spe_register (regcache, tid, -1);
853 if (tdep->ppc_ppr_regnum != -1)
854 fetch_regset (regcache, tid, NT_PPC_PPR,
855 PPC_LINUX_SIZEOF_PPRREGSET,
856 &ppc32_linux_pprregset);
857 if (tdep->ppc_dscr_regnum != -1)
858 fetch_regset (regcache, tid, NT_PPC_DSCR,
859 PPC_LINUX_SIZEOF_DSCRREGSET,
860 &ppc32_linux_dscrregset);
861 }
862
863 /* Fetch registers from the child process. Fetch all registers if
864 regno == -1, otherwise fetch all general registers or all floating
865 point registers depending upon the value of regno. */
866 void
867 ppc_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
868 {
869 pid_t tid = get_ptrace_pid (regcache->ptid ());
870
871 if (regno == -1)
872 fetch_ppc_registers (regcache, tid);
873 else
874 fetch_register (regcache, tid, regno);
875 }
876
877 static void
878 store_vsx_registers (const struct regcache *regcache, int tid, int regno)
879 {
880 int ret;
881 gdb_vsxregset_t regs;
882 const struct regset *vsxregset = ppc_linux_vsxregset ();
883
884 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
885 if (ret < 0)
886 {
887 if (errno == EIO)
888 {
889 have_ptrace_getsetvsxregs = 0;
890 return;
891 }
892 perror_with_name (_("Unable to fetch VSX registers"));
893 }
894
895 vsxregset->collect_regset (vsxregset, regcache, regno, &regs,
896 PPC_LINUX_SIZEOF_VSXREGSET);
897
898 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
899 if (ret < 0)
900 perror_with_name (_("Unable to store VSX registers"));
901 }
902
903 static void
904 store_altivec_registers (const struct regcache *regcache, int tid,
905 int regno)
906 {
907 int ret;
908 gdb_vrregset_t regs;
909 struct gdbarch *gdbarch = regcache->arch ();
910 const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
911
912 ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
913 if (ret < 0)
914 {
915 if (errno == EIO)
916 {
917 have_ptrace_getvrregs = 0;
918 return;
919 }
920 perror_with_name (_("Unable to fetch AltiVec registers"));
921 }
922
923 vrregset->collect_regset (vrregset, regcache, regno, &regs,
924 PPC_LINUX_SIZEOF_VRREGSET);
925
926 ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
927 if (ret < 0)
928 perror_with_name (_("Unable to store AltiVec registers"));
929 }
930
931 /* Assuming TID referrs to an SPE process, set the top halves of TID's
932 general-purpose registers and its SPE-specific registers to the
933 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
934 nothing.
935
936 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
937 PTRACE_SETEVRREGS requests are supported is isolated here, and in
938 get_spe_registers. */
939 static void
940 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
941 {
942 if (have_ptrace_getsetevrregs)
943 {
944 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
945 return;
946 else
947 {
948 /* EIO means that the PTRACE_SETEVRREGS request isn't
949 supported; we fail silently, and don't try the call
950 again. */
951 if (errno == EIO)
952 have_ptrace_getsetevrregs = 0;
953 else
954 /* Anything else needs to be reported. */
955 perror_with_name (_("Unable to set SPE registers"));
956 }
957 }
958 }
959
960 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
961 If REGNO is -1, write the values of all the SPE-specific
962 registers. */
963 static void
964 store_spe_register (const struct regcache *regcache, int tid, int regno)
965 {
966 struct gdbarch *gdbarch = regcache->arch ();
967 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
968 struct gdb_evrregset_t evrregs;
969
970 gdb_assert (sizeof (evrregs.evr[0])
971 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
972 gdb_assert (sizeof (evrregs.acc)
973 == register_size (gdbarch, tdep->ppc_acc_regnum));
974 gdb_assert (sizeof (evrregs.spefscr)
975 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
976
977 if (regno == -1)
978 /* Since we're going to write out every register, the code below
979 should store to every field of evrregs; if that doesn't happen,
980 make it obvious by initializing it with suspicious values. */
981 memset (&evrregs, 42, sizeof (evrregs));
982 else
983 /* We can only read and write the entire EVR register set at a
984 time, so to write just a single register, we do a
985 read-modify-write maneuver. */
986 get_spe_registers (tid, &evrregs);
987
988 if (regno == -1)
989 {
990 int i;
991
992 for (i = 0; i < ppc_num_gprs; i++)
993 regcache->raw_collect (tdep->ppc_ev0_upper_regnum + i,
994 &evrregs.evr[i]);
995 }
996 else if (tdep->ppc_ev0_upper_regnum <= regno
997 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
998 regcache->raw_collect (regno,
999 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
1000
1001 if (regno == -1
1002 || regno == tdep->ppc_acc_regnum)
1003 regcache->raw_collect (tdep->ppc_acc_regnum,
1004 &evrregs.acc);
1005
1006 if (regno == -1
1007 || regno == tdep->ppc_spefscr_regnum)
1008 regcache->raw_collect (tdep->ppc_spefscr_regnum,
1009 &evrregs.spefscr);
1010
1011 /* Write back the modified register set. */
1012 set_spe_registers (tid, &evrregs);
1013 }
1014
1015 static void
1016 store_register (const struct regcache *regcache, int tid, int regno)
1017 {
1018 struct gdbarch *gdbarch = regcache->arch ();
1019 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1020 /* This isn't really an address. But ptrace thinks of it as one. */
1021 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
1022 int i;
1023 size_t bytes_to_transfer;
1024 gdb_byte buf[PPC_MAX_REGISTER_SIZE];
1025
1026 if (altivec_register_p (gdbarch, regno))
1027 {
1028 store_altivec_registers (regcache, tid, regno);
1029 return;
1030 }
1031 else if (vsx_register_p (gdbarch, regno))
1032 {
1033 store_vsx_registers (regcache, tid, regno);
1034 return;
1035 }
1036 else if (spe_register_p (gdbarch, regno))
1037 {
1038 store_spe_register (regcache, tid, regno);
1039 return;
1040 }
1041 else if (regno == PPC_DSCR_REGNUM)
1042 {
1043 gdb_assert (tdep->ppc_dscr_regnum != -1);
1044
1045 store_regset (regcache, tid, regno, NT_PPC_DSCR,
1046 PPC_LINUX_SIZEOF_DSCRREGSET,
1047 &ppc32_linux_dscrregset);
1048 return;
1049 }
1050 else if (regno == PPC_PPR_REGNUM)
1051 {
1052 gdb_assert (tdep->ppc_ppr_regnum != -1);
1053
1054 store_regset (regcache, tid, regno, NT_PPC_PPR,
1055 PPC_LINUX_SIZEOF_PPRREGSET,
1056 &ppc32_linux_pprregset);
1057 return;
1058 }
1059
1060 if (regaddr == -1)
1061 return;
1062
1063 /* First collect the register. Keep in mind that the regcache's
1064 idea of the register's size may not be a multiple of sizeof
1065 (long). */
1066 memset (buf, 0, sizeof buf);
1067 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
1068 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1069 {
1070 /* Little-endian values always sit at the left end of the buffer. */
1071 regcache->raw_collect (regno, buf);
1072 }
1073 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1074 {
1075 /* Big-endian values sit at the right end of the buffer. */
1076 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
1077 regcache->raw_collect (regno, buf + padding);
1078 }
1079
1080 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
1081 {
1082 long l;
1083
1084 memcpy (&l, &buf[i], sizeof (l));
1085 errno = 0;
1086 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
1087 regaddr += sizeof (long);
1088
1089 if (errno == EIO
1090 && (regno == tdep->ppc_fpscr_regnum
1091 || regno == PPC_ORIG_R3_REGNUM
1092 || regno == PPC_TRAP_REGNUM))
1093 {
1094 /* Some older kernel versions don't allow fpscr, orig_r3
1095 or trap to be written. */
1096 continue;
1097 }
1098
1099 if (errno != 0)
1100 {
1101 char message[128];
1102 xsnprintf (message, sizeof (message), "writing register %s (#%d)",
1103 gdbarch_register_name (gdbarch, regno), regno);
1104 perror_with_name (message);
1105 }
1106 }
1107 }
1108
1109 /* This function actually issues the request to ptrace, telling
1110 it to store all general-purpose registers present in the specified
1111 regset.
1112
1113 If the ptrace request does not exist, this function returns 0
1114 and properly sets the have_ptrace_* flag. If the request fails,
1115 this function calls perror_with_name. Otherwise, if the request
1116 succeeds, then the regcache is stored and 1 is returned. */
1117 static int
1118 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1119 {
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 = regcache->arch ();
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 = regcache->arch ();
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 struct gdbarch *gdbarch = regcache->arch ();
1237 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1238
1239 store_gp_regs (regcache, tid, -1);
1240 if (tdep->ppc_fp0_regnum >= 0)
1241 store_fp_regs (regcache, tid, -1);
1242 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1243 if (tdep->ppc_ps_regnum != -1)
1244 store_register (regcache, tid, tdep->ppc_ps_regnum);
1245 if (tdep->ppc_cr_regnum != -1)
1246 store_register (regcache, tid, tdep->ppc_cr_regnum);
1247 if (tdep->ppc_lr_regnum != -1)
1248 store_register (regcache, tid, tdep->ppc_lr_regnum);
1249 if (tdep->ppc_ctr_regnum != -1)
1250 store_register (regcache, tid, tdep->ppc_ctr_regnum);
1251 if (tdep->ppc_xer_regnum != -1)
1252 store_register (regcache, tid, tdep->ppc_xer_regnum);
1253 if (tdep->ppc_mq_regnum != -1)
1254 store_register (regcache, tid, tdep->ppc_mq_regnum);
1255 if (tdep->ppc_fpscr_regnum != -1)
1256 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
1257 if (ppc_linux_trap_reg_p (gdbarch))
1258 {
1259 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1260 store_register (regcache, tid, PPC_TRAP_REGNUM);
1261 }
1262 if (have_ptrace_getvrregs)
1263 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1264 store_altivec_registers (regcache, tid, -1);
1265 if (have_ptrace_getsetvsxregs)
1266 if (tdep->ppc_vsr0_upper_regnum != -1)
1267 store_vsx_registers (regcache, tid, -1);
1268 if (tdep->ppc_ev0_upper_regnum >= 0)
1269 store_spe_register (regcache, tid, -1);
1270 if (tdep->ppc_ppr_regnum != -1)
1271 store_regset (regcache, tid, -1, NT_PPC_PPR,
1272 PPC_LINUX_SIZEOF_PPRREGSET,
1273 &ppc32_linux_pprregset);
1274 if (tdep->ppc_dscr_regnum != -1)
1275 store_regset (regcache, tid, -1, NT_PPC_DSCR,
1276 PPC_LINUX_SIZEOF_DSCRREGSET,
1277 &ppc32_linux_dscrregset);
1278 }
1279
1280 /* Fetch the AT_HWCAP entry from the aux vector. */
1281 static CORE_ADDR
1282 ppc_linux_get_hwcap (void)
1283 {
1284 CORE_ADDR field;
1285
1286 if (target_auxv_search (current_top_target (), AT_HWCAP, &field) != 1)
1287 return 0;
1288
1289 return field;
1290 }
1291
1292 /* Fetch the AT_HWCAP2 entry from the aux vector. */
1293
1294 static CORE_ADDR
1295 ppc_linux_get_hwcap2 (void)
1296 {
1297 CORE_ADDR field;
1298
1299 if (target_auxv_search (current_top_target (), AT_HWCAP2, &field) != 1)
1300 return 0;
1301
1302 return field;
1303 }
1304
1305 /* The cached DABR value, to install in new threads.
1306 This variable is used when the PowerPC HWDEBUG ptrace
1307 interface is not available. */
1308 static long saved_dabr_value;
1309
1310 /* Global structure that will store information about the available
1311 features provided by the PowerPC HWDEBUG ptrace interface. */
1312 static struct ppc_debug_info hwdebug_info;
1313
1314 /* Global variable that holds the maximum number of slots that the
1315 kernel will use. This is only used when PowerPC HWDEBUG ptrace interface
1316 is available. */
1317 static size_t max_slots_number = 0;
1318
1319 struct hw_break_tuple
1320 {
1321 long slot;
1322 struct ppc_hw_breakpoint *hw_break;
1323 };
1324
1325 /* This is an internal VEC created to store information about *points inserted
1326 for each thread. This is used when PowerPC HWDEBUG ptrace interface is
1327 available. */
1328 typedef struct thread_points
1329 {
1330 /* The TID to which this *point relates. */
1331 int tid;
1332 /* Information about the *point, such as its address, type, etc.
1333
1334 Each element inside this vector corresponds to a hardware
1335 breakpoint or watchpoint in the thread represented by TID. The maximum
1336 size of these vector is MAX_SLOTS_NUMBER. If the hw_break element of
1337 the tuple is NULL, then the position in the vector is free. */
1338 struct hw_break_tuple *hw_breaks;
1339 } *thread_points_p;
1340 DEF_VEC_P (thread_points_p);
1341
1342 VEC(thread_points_p) *ppc_threads = NULL;
1343
1344 /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
1345 available. */
1346 #define PPC_DEBUG_CURRENT_VERSION 1
1347
1348 /* Returns non-zero if we support the PowerPC HWDEBUG ptrace interface. */
1349 static int
1350 have_ptrace_hwdebug_interface (void)
1351 {
1352 static int have_ptrace_hwdebug_interface = -1;
1353
1354 if (have_ptrace_hwdebug_interface == -1)
1355 {
1356 int tid;
1357
1358 tid = inferior_ptid.lwp ();
1359 if (tid == 0)
1360 tid = inferior_ptid.pid ();
1361
1362 /* Check for kernel support for PowerPC HWDEBUG ptrace interface. */
1363 if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info) >= 0)
1364 {
1365 /* Check whether PowerPC HWDEBUG ptrace interface is functional and
1366 provides any supported feature. */
1367 if (hwdebug_info.features != 0)
1368 {
1369 have_ptrace_hwdebug_interface = 1;
1370 max_slots_number = hwdebug_info.num_instruction_bps
1371 + hwdebug_info.num_data_bps
1372 + hwdebug_info.num_condition_regs;
1373 return have_ptrace_hwdebug_interface;
1374 }
1375 }
1376 /* Old school interface and no PowerPC HWDEBUG ptrace support. */
1377 have_ptrace_hwdebug_interface = 0;
1378 memset (&hwdebug_info, 0, sizeof (struct ppc_debug_info));
1379 }
1380
1381 return have_ptrace_hwdebug_interface;
1382 }
1383
1384 int
1385 ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
1386 {
1387 int total_hw_wp, total_hw_bp;
1388
1389 if (have_ptrace_hwdebug_interface ())
1390 {
1391 /* When PowerPC HWDEBUG ptrace interface is available, the number of
1392 available hardware watchpoints and breakpoints is stored at the
1393 hwdebug_info struct. */
1394 total_hw_bp = hwdebug_info.num_instruction_bps;
1395 total_hw_wp = hwdebug_info.num_data_bps;
1396 }
1397 else
1398 {
1399 /* When we do not have PowerPC HWDEBUG ptrace interface, we should
1400 consider having 1 hardware watchpoint and no hardware breakpoints. */
1401 total_hw_bp = 0;
1402 total_hw_wp = 1;
1403 }
1404
1405 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
1406 || type == bp_access_watchpoint || type == bp_watchpoint)
1407 {
1408 if (cnt + ot > total_hw_wp)
1409 return -1;
1410 }
1411 else if (type == bp_hardware_breakpoint)
1412 {
1413 if (total_hw_bp == 0)
1414 {
1415 /* No hardware breakpoint support. */
1416 return 0;
1417 }
1418 if (cnt > total_hw_bp)
1419 return -1;
1420 }
1421
1422 if (!have_ptrace_hwdebug_interface ())
1423 {
1424 int tid;
1425 ptid_t ptid = inferior_ptid;
1426
1427 /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG
1428 and whether the target has DABR. If either answer is no, the
1429 ptrace call will return -1. Fail in that case. */
1430 tid = ptid.lwp ();
1431 if (tid == 0)
1432 tid = ptid.pid ();
1433
1434 if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
1435 return 0;
1436 }
1437
1438 return 1;
1439 }
1440
1441 int
1442 ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1443 {
1444 /* Handle sub-8-byte quantities. */
1445 if (len <= 0)
1446 return 0;
1447
1448 /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
1449 restrictions for watchpoints in the processors. In that case, we use that
1450 information to determine the hardcoded watchable region for
1451 watchpoints. */
1452 if (have_ptrace_hwdebug_interface ())
1453 {
1454 int region_size;
1455 /* Embedded DAC-based processors, like the PowerPC 440 have ranged
1456 watchpoints and can watch any access within an arbitrary memory
1457 region. This is useful to watch arrays and structs, for instance. It
1458 takes two hardware watchpoints though. */
1459 if (len > 1
1460 && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
1461 && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1462 return 2;
1463 /* Check if the processor provides DAWR interface. */
1464 if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
1465 /* DAWR interface allows to watch up to 512 byte wide ranges which
1466 can't cross a 512 byte boundary. */
1467 region_size = 512;
1468 else
1469 region_size = hwdebug_info.data_bp_alignment;
1470 /* Server processors provide one hardware watchpoint and addr+len should
1471 fall in the watchable region provided by the ptrace interface. */
1472 if (region_size
1473 && (addr + len > (addr & ~(region_size - 1)) + region_size))
1474 return 0;
1475 }
1476 /* addr+len must fall in the 8 byte watchable region for DABR-based
1477 processors (i.e., server processors). Without the new PowerPC HWDEBUG
1478 ptrace interface, DAC-based processors (i.e., embedded processors) will
1479 use addresses aligned to 4-bytes due to the way the read/write flags are
1480 passed in the old ptrace interface. */
1481 else if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
1482 && (addr + len) > (addr & ~3) + 4)
1483 || (addr + len) > (addr & ~7) + 8)
1484 return 0;
1485
1486 return 1;
1487 }
1488
1489 /* This function compares two ppc_hw_breakpoint structs field-by-field. */
1490 static int
1491 hwdebug_point_cmp (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
1492 {
1493 return (a->trigger_type == b->trigger_type
1494 && a->addr_mode == b->addr_mode
1495 && a->condition_mode == b->condition_mode
1496 && a->addr == b->addr
1497 && a->addr2 == b->addr2
1498 && a->condition_value == b->condition_value);
1499 }
1500
1501 /* This function can be used to retrieve a thread_points by the TID of the
1502 related process/thread. If nothing has been found, and ALLOC_NEW is 0,
1503 it returns NULL. If ALLOC_NEW is non-zero, a new thread_points for the
1504 provided TID will be created and returned. */
1505 static struct thread_points *
1506 hwdebug_find_thread_points_by_tid (int tid, int alloc_new)
1507 {
1508 int i;
1509 struct thread_points *t;
1510
1511 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
1512 if (t->tid == tid)
1513 return t;
1514
1515 t = NULL;
1516
1517 /* Do we need to allocate a new point_item
1518 if the wanted one does not exist? */
1519 if (alloc_new)
1520 {
1521 t = XNEW (struct thread_points);
1522 t->hw_breaks = XCNEWVEC (struct hw_break_tuple, max_slots_number);
1523 t->tid = tid;
1524 VEC_safe_push (thread_points_p, ppc_threads, t);
1525 }
1526
1527 return t;
1528 }
1529
1530 /* This function is a generic wrapper that is responsible for inserting a
1531 *point (i.e., calling `ptrace' in order to issue the request to the
1532 kernel) and registering it internally in GDB. */
1533 static void
1534 hwdebug_insert_point (struct ppc_hw_breakpoint *b, int tid)
1535 {
1536 int i;
1537 long slot;
1538 gdb::unique_xmalloc_ptr<ppc_hw_breakpoint> p (XDUP (ppc_hw_breakpoint, b));
1539 struct hw_break_tuple *hw_breaks;
1540 struct thread_points *t;
1541
1542 errno = 0;
1543 slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p.get ());
1544 if (slot < 0)
1545 perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
1546
1547 /* Everything went fine, so we have to register this *point. */
1548 t = hwdebug_find_thread_points_by_tid (tid, 1);
1549 gdb_assert (t != NULL);
1550 hw_breaks = t->hw_breaks;
1551
1552 /* Find a free element in the hw_breaks vector. */
1553 for (i = 0; i < max_slots_number; i++)
1554 if (hw_breaks[i].hw_break == NULL)
1555 {
1556 hw_breaks[i].slot = slot;
1557 hw_breaks[i].hw_break = p.release ();
1558 break;
1559 }
1560
1561 gdb_assert (i != max_slots_number);
1562 }
1563
1564 /* This function is a generic wrapper that is responsible for removing a
1565 *point (i.e., calling `ptrace' in order to issue the request to the
1566 kernel), and unregistering it internally at GDB. */
1567 static void
1568 hwdebug_remove_point (struct ppc_hw_breakpoint *b, int tid)
1569 {
1570 int i;
1571 struct hw_break_tuple *hw_breaks;
1572 struct thread_points *t;
1573
1574 t = hwdebug_find_thread_points_by_tid (tid, 0);
1575 gdb_assert (t != NULL);
1576 hw_breaks = t->hw_breaks;
1577
1578 for (i = 0; i < max_slots_number; i++)
1579 if (hw_breaks[i].hw_break && hwdebug_point_cmp (hw_breaks[i].hw_break, b))
1580 break;
1581
1582 gdb_assert (i != max_slots_number);
1583
1584 /* We have to ignore ENOENT errors because the kernel implements hardware
1585 breakpoints/watchpoints as "one-shot", that is, they are automatically
1586 deleted when hit. */
1587 errno = 0;
1588 if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
1589 if (errno != ENOENT)
1590 perror_with_name (_("Unexpected error deleting "
1591 "breakpoint or watchpoint"));
1592
1593 xfree (hw_breaks[i].hw_break);
1594 hw_breaks[i].hw_break = NULL;
1595 }
1596
1597 /* Return the number of registers needed for a ranged breakpoint. */
1598
1599 int
1600 ppc_linux_nat_target::ranged_break_num_registers ()
1601 {
1602 return ((have_ptrace_hwdebug_interface ()
1603 && hwdebug_info.features & PPC_DEBUG_FEATURE_INSN_BP_RANGE)?
1604 2 : -1);
1605 }
1606
1607 /* Insert the hardware breakpoint described by BP_TGT. Returns 0 for
1608 success, 1 if hardware breakpoints are not supported or -1 for failure. */
1609
1610 int
1611 ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1612 struct bp_target_info *bp_tgt)
1613 {
1614 struct lwp_info *lp;
1615 struct ppc_hw_breakpoint p;
1616
1617 if (!have_ptrace_hwdebug_interface ())
1618 return -1;
1619
1620 p.version = PPC_DEBUG_CURRENT_VERSION;
1621 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1622 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1623 p.addr = (uint64_t) (bp_tgt->placed_address = bp_tgt->reqstd_address);
1624 p.condition_value = 0;
1625
1626 if (bp_tgt->length)
1627 {
1628 p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
1629
1630 /* The breakpoint will trigger if the address of the instruction is
1631 within the defined range, as follows: p.addr <= address < p.addr2. */
1632 p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
1633 }
1634 else
1635 {
1636 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1637 p.addr2 = 0;
1638 }
1639
1640 ALL_LWPS (lp)
1641 hwdebug_insert_point (&p, lp->ptid.lwp ());
1642
1643 return 0;
1644 }
1645
1646 int
1647 ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1648 struct bp_target_info *bp_tgt)
1649 {
1650 struct lwp_info *lp;
1651 struct ppc_hw_breakpoint p;
1652
1653 if (!have_ptrace_hwdebug_interface ())
1654 return -1;
1655
1656 p.version = PPC_DEBUG_CURRENT_VERSION;
1657 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
1658 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1659 p.addr = (uint64_t) bp_tgt->placed_address;
1660 p.condition_value = 0;
1661
1662 if (bp_tgt->length)
1663 {
1664 p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
1665
1666 /* The breakpoint will trigger if the address of the instruction is within
1667 the defined range, as follows: p.addr <= address < p.addr2. */
1668 p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
1669 }
1670 else
1671 {
1672 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1673 p.addr2 = 0;
1674 }
1675
1676 ALL_LWPS (lp)
1677 hwdebug_remove_point (&p, lp->ptid.lwp ());
1678
1679 return 0;
1680 }
1681
1682 static int
1683 get_trigger_type (enum target_hw_bp_type type)
1684 {
1685 int t;
1686
1687 if (type == hw_read)
1688 t = PPC_BREAKPOINT_TRIGGER_READ;
1689 else if (type == hw_write)
1690 t = PPC_BREAKPOINT_TRIGGER_WRITE;
1691 else
1692 t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
1693
1694 return t;
1695 }
1696
1697 /* Insert a new masked watchpoint at ADDR using the mask MASK.
1698 RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
1699 or hw_access for an access watchpoint. Returns 0 on success and throws
1700 an error on failure. */
1701
1702 int
1703 ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
1704 target_hw_bp_type rw)
1705 {
1706 struct lwp_info *lp;
1707 struct ppc_hw_breakpoint p;
1708
1709 gdb_assert (have_ptrace_hwdebug_interface ());
1710
1711 p.version = PPC_DEBUG_CURRENT_VERSION;
1712 p.trigger_type = get_trigger_type (rw);
1713 p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
1714 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1715 p.addr = addr;
1716 p.addr2 = mask;
1717 p.condition_value = 0;
1718
1719 ALL_LWPS (lp)
1720 hwdebug_insert_point (&p, lp->ptid.lwp ());
1721
1722 return 0;
1723 }
1724
1725 /* Remove a masked watchpoint at ADDR with the mask MASK.
1726 RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
1727 or hw_access for an access watchpoint. Returns 0 on success and throws
1728 an error on failure. */
1729
1730 int
1731 ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
1732 target_hw_bp_type rw)
1733 {
1734 struct lwp_info *lp;
1735 struct ppc_hw_breakpoint p;
1736
1737 gdb_assert (have_ptrace_hwdebug_interface ());
1738
1739 p.version = PPC_DEBUG_CURRENT_VERSION;
1740 p.trigger_type = get_trigger_type (rw);
1741 p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
1742 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1743 p.addr = addr;
1744 p.addr2 = mask;
1745 p.condition_value = 0;
1746
1747 ALL_LWPS (lp)
1748 hwdebug_remove_point (&p, lp->ptid.lwp ());
1749
1750 return 0;
1751 }
1752
1753 /* Check whether we have at least one free DVC register. */
1754 static int
1755 can_use_watchpoint_cond_accel (void)
1756 {
1757 struct thread_points *p;
1758 int tid = inferior_ptid.lwp ();
1759 int cnt = hwdebug_info.num_condition_regs, i;
1760
1761 if (!have_ptrace_hwdebug_interface () || cnt == 0)
1762 return 0;
1763
1764 p = hwdebug_find_thread_points_by_tid (tid, 0);
1765
1766 if (p)
1767 {
1768 for (i = 0; i < max_slots_number; i++)
1769 if (p->hw_breaks[i].hw_break != NULL
1770 && (p->hw_breaks[i].hw_break->condition_mode
1771 != PPC_BREAKPOINT_CONDITION_NONE))
1772 cnt--;
1773
1774 /* There are no available slots now. */
1775 if (cnt <= 0)
1776 return 0;
1777 }
1778
1779 return 1;
1780 }
1781
1782 /* Calculate the enable bits and the contents of the Data Value Compare
1783 debug register present in BookE processors.
1784
1785 ADDR is the address to be watched, LEN is the length of watched data
1786 and DATA_VALUE is the value which will trigger the watchpoint.
1787 On exit, CONDITION_MODE will hold the enable bits for the DVC, and
1788 CONDITION_VALUE will hold the value which should be put in the
1789 DVC register. */
1790 static void
1791 calculate_dvc (CORE_ADDR addr, int len, CORE_ADDR data_value,
1792 uint32_t *condition_mode, uint64_t *condition_value)
1793 {
1794 int i, num_byte_enable, align_offset, num_bytes_off_dvc,
1795 rightmost_enabled_byte;
1796 CORE_ADDR addr_end_data, addr_end_dvc;
1797
1798 /* The DVC register compares bytes within fixed-length windows which
1799 are word-aligned, with length equal to that of the DVC register.
1800 We need to calculate where our watch region is relative to that
1801 window and enable comparison of the bytes which fall within it. */
1802
1803 align_offset = addr % hwdebug_info.sizeof_condition;
1804 addr_end_data = addr + len;
1805 addr_end_dvc = (addr - align_offset
1806 + hwdebug_info.sizeof_condition);
1807 num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
1808 addr_end_data - addr_end_dvc : 0;
1809 num_byte_enable = len - num_bytes_off_dvc;
1810 /* Here, bytes are numbered from right to left. */
1811 rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
1812 addr_end_dvc - addr_end_data : 0;
1813
1814 *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
1815 for (i = 0; i < num_byte_enable; i++)
1816 *condition_mode
1817 |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
1818
1819 /* Now we need to match the position within the DVC of the comparison
1820 value with where the watch region is relative to the window
1821 (i.e., the ALIGN_OFFSET). */
1822
1823 *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
1824 << rightmost_enabled_byte * 8);
1825 }
1826
1827 /* Return the number of memory locations that need to be accessed to
1828 evaluate the expression which generated the given value chain.
1829 Returns -1 if there's any register access involved, or if there are
1830 other kinds of values which are not acceptable in a condition
1831 expression (e.g., lval_computed or lval_internalvar). */
1832 static int
1833 num_memory_accesses (const std::vector<value_ref_ptr> &chain)
1834 {
1835 int found_memory_cnt = 0;
1836
1837 /* The idea here is that evaluating an expression generates a series
1838 of values, one holding the value of every subexpression. (The
1839 expression a*b+c has five subexpressions: a, b, a*b, c, and
1840 a*b+c.) GDB's values hold almost enough information to establish
1841 the criteria given above --- they identify memory lvalues,
1842 register lvalues, computed values, etcetera. So we can evaluate
1843 the expression, and then scan the chain of values that leaves
1844 behind to determine the memory locations involved in the evaluation
1845 of an expression.
1846
1847 However, I don't think that the values returned by inferior
1848 function calls are special in any way. So this function may not
1849 notice that an expression contains an inferior function call.
1850 FIXME. */
1851
1852 for (const value_ref_ptr &iter : chain)
1853 {
1854 struct value *v = iter.get ();
1855
1856 /* Constants and values from the history are fine. */
1857 if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
1858 continue;
1859 else if (VALUE_LVAL (v) == lval_memory)
1860 {
1861 /* A lazy memory lvalue is one that GDB never needed to fetch;
1862 we either just used its address (e.g., `a' in `a.b') or
1863 we never needed it at all (e.g., `a' in `a,b'). */
1864 if (!value_lazy (v))
1865 found_memory_cnt++;
1866 }
1867 /* Other kinds of values are not fine. */
1868 else
1869 return -1;
1870 }
1871
1872 return found_memory_cnt;
1873 }
1874
1875 /* Verifies whether the expression COND can be implemented using the
1876 DVC (Data Value Compare) register in BookE processors. The expression
1877 must test the watch value for equality with a constant expression.
1878 If the function returns 1, DATA_VALUE will contain the constant against
1879 which the watch value should be compared and LEN will contain the size
1880 of the constant. */
1881 static int
1882 check_condition (CORE_ADDR watch_addr, struct expression *cond,
1883 CORE_ADDR *data_value, int *len)
1884 {
1885 int pc = 1, num_accesses_left, num_accesses_right;
1886 struct value *left_val, *right_val;
1887 std::vector<value_ref_ptr> left_chain, right_chain;
1888
1889 if (cond->elts[0].opcode != BINOP_EQUAL)
1890 return 0;
1891
1892 fetch_subexp_value (cond, &pc, &left_val, NULL, &left_chain, 0);
1893 num_accesses_left = num_memory_accesses (left_chain);
1894
1895 if (left_val == NULL || num_accesses_left < 0)
1896 return 0;
1897
1898 fetch_subexp_value (cond, &pc, &right_val, NULL, &right_chain, 0);
1899 num_accesses_right = num_memory_accesses (right_chain);
1900
1901 if (right_val == NULL || num_accesses_right < 0)
1902 return 0;
1903
1904 if (num_accesses_left == 1 && num_accesses_right == 0
1905 && VALUE_LVAL (left_val) == lval_memory
1906 && value_address (left_val) == watch_addr)
1907 {
1908 *data_value = value_as_long (right_val);
1909
1910 /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
1911 the same type as the memory region referenced by LEFT_VAL. */
1912 *len = TYPE_LENGTH (check_typedef (value_type (left_val)));
1913 }
1914 else if (num_accesses_left == 0 && num_accesses_right == 1
1915 && VALUE_LVAL (right_val) == lval_memory
1916 && value_address (right_val) == watch_addr)
1917 {
1918 *data_value = value_as_long (left_val);
1919
1920 /* DATA_VALUE is the constant in LEFT_VAL, but actually has
1921 the same type as the memory region referenced by RIGHT_VAL. */
1922 *len = TYPE_LENGTH (check_typedef (value_type (right_val)));
1923 }
1924 else
1925 return 0;
1926
1927 return 1;
1928 }
1929
1930 /* Return non-zero if the target is capable of using hardware to evaluate
1931 the condition expression, thus only triggering the watchpoint when it is
1932 true. */
1933 bool
1934 ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr, int len,
1935 int rw,
1936 struct expression *cond)
1937 {
1938 CORE_ADDR data_value;
1939
1940 return (have_ptrace_hwdebug_interface ()
1941 && hwdebug_info.num_condition_regs > 0
1942 && check_condition (addr, cond, &data_value, &len));
1943 }
1944
1945 /* Set up P with the parameters necessary to request a watchpoint covering
1946 LEN bytes starting at ADDR and if possible with condition expression COND
1947 evaluated by hardware. INSERT tells if we are creating a request for
1948 inserting or removing the watchpoint. */
1949
1950 static void
1951 create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
1952 int len, enum target_hw_bp_type type,
1953 struct expression *cond, int insert)
1954 {
1955 if (len == 1
1956 || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
1957 {
1958 int use_condition;
1959 CORE_ADDR data_value;
1960
1961 use_condition = (insert? can_use_watchpoint_cond_accel ()
1962 : hwdebug_info.num_condition_regs > 0);
1963 if (cond && use_condition && check_condition (addr, cond,
1964 &data_value, &len))
1965 calculate_dvc (addr, len, data_value, &p->condition_mode,
1966 &p->condition_value);
1967 else
1968 {
1969 p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1970 p->condition_value = 0;
1971 }
1972
1973 p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
1974 p->addr2 = 0;
1975 }
1976 else
1977 {
1978 p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
1979 p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
1980 p->condition_value = 0;
1981
1982 /* The watchpoint will trigger if the address of the memory access is
1983 within the defined range, as follows: p->addr <= address < p->addr2.
1984
1985 Note that the above sentence just documents how ptrace interprets
1986 its arguments; the watchpoint is set to watch the range defined by
1987 the user _inclusively_, as specified by the user interface. */
1988 p->addr2 = (uint64_t) addr + len;
1989 }
1990
1991 p->version = PPC_DEBUG_CURRENT_VERSION;
1992 p->trigger_type = get_trigger_type (type);
1993 p->addr = (uint64_t) addr;
1994 }
1995
1996 int
1997 ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
1998 enum target_hw_bp_type type,
1999 struct expression *cond)
2000 {
2001 struct lwp_info *lp;
2002 int ret = -1;
2003
2004 if (have_ptrace_hwdebug_interface ())
2005 {
2006 struct ppc_hw_breakpoint p;
2007
2008 create_watchpoint_request (&p, addr, len, type, cond, 1);
2009
2010 ALL_LWPS (lp)
2011 hwdebug_insert_point (&p, lp->ptid.lwp ());
2012
2013 ret = 0;
2014 }
2015 else
2016 {
2017 long dabr_value;
2018 long read_mode, write_mode;
2019
2020 if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
2021 {
2022 /* PowerPC 440 requires only the read/write flags to be passed
2023 to the kernel. */
2024 read_mode = 1;
2025 write_mode = 2;
2026 }
2027 else
2028 {
2029 /* PowerPC 970 and other DABR-based processors are required to pass
2030 the Breakpoint Translation bit together with the flags. */
2031 read_mode = 5;
2032 write_mode = 6;
2033 }
2034
2035 dabr_value = addr & ~(read_mode | write_mode);
2036 switch (type)
2037 {
2038 case hw_read:
2039 /* Set read and translate bits. */
2040 dabr_value |= read_mode;
2041 break;
2042 case hw_write:
2043 /* Set write and translate bits. */
2044 dabr_value |= write_mode;
2045 break;
2046 case hw_access:
2047 /* Set read, write and translate bits. */
2048 dabr_value |= read_mode | write_mode;
2049 break;
2050 }
2051
2052 saved_dabr_value = dabr_value;
2053
2054 ALL_LWPS (lp)
2055 if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0,
2056 saved_dabr_value) < 0)
2057 return -1;
2058
2059 ret = 0;
2060 }
2061
2062 return ret;
2063 }
2064
2065 int
2066 ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
2067 enum target_hw_bp_type type,
2068 struct expression *cond)
2069 {
2070 struct lwp_info *lp;
2071 int ret = -1;
2072
2073 if (have_ptrace_hwdebug_interface ())
2074 {
2075 struct ppc_hw_breakpoint p;
2076
2077 create_watchpoint_request (&p, addr, len, type, cond, 0);
2078
2079 ALL_LWPS (lp)
2080 hwdebug_remove_point (&p, lp->ptid.lwp ());
2081
2082 ret = 0;
2083 }
2084 else
2085 {
2086 saved_dabr_value = 0;
2087 ALL_LWPS (lp)
2088 if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0,
2089 saved_dabr_value) < 0)
2090 return -1;
2091
2092 ret = 0;
2093 }
2094
2095 return ret;
2096 }
2097
2098 void
2099 ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
2100 {
2101 int tid = lp->ptid.lwp ();
2102
2103 if (have_ptrace_hwdebug_interface ())
2104 {
2105 int i;
2106 struct thread_points *p;
2107 struct hw_break_tuple *hw_breaks;
2108
2109 if (VEC_empty (thread_points_p, ppc_threads))
2110 return;
2111
2112 /* Get a list of breakpoints from any thread. */
2113 p = VEC_last (thread_points_p, ppc_threads);
2114 hw_breaks = p->hw_breaks;
2115
2116 /* Copy that thread's breakpoints and watchpoints to the new thread. */
2117 for (i = 0; i < max_slots_number; i++)
2118 if (hw_breaks[i].hw_break)
2119 {
2120 /* Older kernels did not make new threads inherit their parent
2121 thread's debug state, so we always clear the slot and replicate
2122 the debug state ourselves, ensuring compatibility with all
2123 kernels. */
2124
2125 /* The ppc debug resource accounting is done through "slots".
2126 Ask the kernel the deallocate this specific *point's slot. */
2127 ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot);
2128
2129 hwdebug_insert_point (hw_breaks[i].hw_break, tid);
2130 }
2131 }
2132 else
2133 ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
2134 }
2135
2136 static void
2137 ppc_linux_thread_exit (struct thread_info *tp, int silent)
2138 {
2139 int i;
2140 int tid = tp->ptid.lwp ();
2141 struct hw_break_tuple *hw_breaks;
2142 struct thread_points *t = NULL, *p;
2143
2144 if (!have_ptrace_hwdebug_interface ())
2145 return;
2146
2147 for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
2148 if (p->tid == tid)
2149 {
2150 t = p;
2151 break;
2152 }
2153
2154 if (t == NULL)
2155 return;
2156
2157 VEC_unordered_remove (thread_points_p, ppc_threads, i);
2158
2159 hw_breaks = t->hw_breaks;
2160
2161 for (i = 0; i < max_slots_number; i++)
2162 if (hw_breaks[i].hw_break)
2163 xfree (hw_breaks[i].hw_break);
2164
2165 xfree (t->hw_breaks);
2166 xfree (t);
2167 }
2168
2169 bool
2170 ppc_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
2171 {
2172 siginfo_t siginfo;
2173
2174 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
2175 return false;
2176
2177 if (siginfo.si_signo != SIGTRAP
2178 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
2179 return false;
2180
2181 if (have_ptrace_hwdebug_interface ())
2182 {
2183 int i;
2184 struct thread_points *t;
2185 struct hw_break_tuple *hw_breaks;
2186 /* The index (or slot) of the *point is passed in the si_errno field. */
2187 int slot = siginfo.si_errno;
2188
2189 t = hwdebug_find_thread_points_by_tid (inferior_ptid.lwp (), 0);
2190
2191 /* Find out if this *point is a hardware breakpoint.
2192 If so, we should return 0. */
2193 if (t)
2194 {
2195 hw_breaks = t->hw_breaks;
2196 for (i = 0; i < max_slots_number; i++)
2197 if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
2198 && hw_breaks[i].hw_break->trigger_type
2199 == PPC_BREAKPOINT_TRIGGER_EXECUTE)
2200 return false;
2201 }
2202 }
2203
2204 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
2205 return true;
2206 }
2207
2208 bool
2209 ppc_linux_nat_target::stopped_by_watchpoint ()
2210 {
2211 CORE_ADDR addr;
2212 return stopped_data_address (&addr);
2213 }
2214
2215 bool
2216 ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
2217 CORE_ADDR start,
2218 int length)
2219 {
2220 int mask;
2221
2222 if (have_ptrace_hwdebug_interface ()
2223 && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
2224 return start <= addr && start + length >= addr;
2225 else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
2226 mask = 3;
2227 else
2228 mask = 7;
2229
2230 addr &= ~mask;
2231
2232 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
2233 return start <= addr + mask && start + length - 1 >= addr;
2234 }
2235
2236 /* Return the number of registers needed for a masked hardware watchpoint. */
2237
2238 int
2239 ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
2240 {
2241 if (!have_ptrace_hwdebug_interface ()
2242 || (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
2243 return -1;
2244 else if ((mask & 0xC0000000) != 0xC0000000)
2245 {
2246 warning (_("The given mask covers kernel address space "
2247 "and cannot be used.\n"));
2248
2249 return -2;
2250 }
2251 else
2252 return 2;
2253 }
2254
2255 void
2256 ppc_linux_nat_target::store_registers (struct regcache *regcache, int regno)
2257 {
2258 pid_t tid = get_ptrace_pid (regcache->ptid ());
2259
2260 if (regno >= 0)
2261 store_register (regcache, tid, regno);
2262 else
2263 store_ppc_registers (regcache, tid);
2264 }
2265
2266 /* Functions for transferring registers between a gregset_t or fpregset_t
2267 (see sys/ucontext.h) and gdb's regcache. The word size is that used
2268 by the ptrace interface, not the current program's ABI. Eg. if a
2269 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
2270 read or write 64-bit gregsets. This is to suit the host libthread_db. */
2271
2272 void
2273 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
2274 {
2275 const struct regset *regset = ppc_linux_gregset (sizeof (long));
2276
2277 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
2278 }
2279
2280 void
2281 fill_gregset (const struct regcache *regcache,
2282 gdb_gregset_t *gregsetp, int regno)
2283 {
2284 const struct regset *regset = ppc_linux_gregset (sizeof (long));
2285
2286 if (regno == -1)
2287 memset (gregsetp, 0, sizeof (*gregsetp));
2288 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
2289 }
2290
2291 void
2292 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
2293 {
2294 const struct regset *regset = ppc_linux_fpregset ();
2295
2296 ppc_supply_fpregset (regset, regcache, -1,
2297 fpregsetp, sizeof (*fpregsetp));
2298 }
2299
2300 void
2301 fill_fpregset (const struct regcache *regcache,
2302 gdb_fpregset_t *fpregsetp, int regno)
2303 {
2304 const struct regset *regset = ppc_linux_fpregset ();
2305
2306 ppc_collect_fpregset (regset, regcache, regno,
2307 fpregsetp, sizeof (*fpregsetp));
2308 }
2309
2310 int
2311 ppc_linux_nat_target::auxv_parse (gdb_byte **readptr,
2312 gdb_byte *endptr, CORE_ADDR *typep,
2313 CORE_ADDR *valp)
2314 {
2315 int tid = inferior_ptid.lwp ();
2316 if (tid == 0)
2317 tid = inferior_ptid.pid ();
2318
2319 int sizeof_auxv_field = ppc_linux_target_wordsize (tid);
2320
2321 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
2322 gdb_byte *ptr = *readptr;
2323
2324 if (endptr == ptr)
2325 return 0;
2326
2327 if (endptr - ptr < sizeof_auxv_field * 2)
2328 return -1;
2329
2330 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2331 ptr += sizeof_auxv_field;
2332 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
2333 ptr += sizeof_auxv_field;
2334
2335 *readptr = ptr;
2336 return 1;
2337 }
2338
2339 const struct target_desc *
2340 ppc_linux_nat_target::read_description ()
2341 {
2342 int tid = inferior_ptid.lwp ();
2343 if (tid == 0)
2344 tid = inferior_ptid.pid ();
2345
2346 if (have_ptrace_getsetevrregs)
2347 {
2348 struct gdb_evrregset_t evrregset;
2349
2350 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
2351 return tdesc_powerpc_e500l;
2352
2353 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
2354 Anything else needs to be reported. */
2355 else if (errno != EIO)
2356 perror_with_name (_("Unable to fetch SPE registers"));
2357 }
2358
2359 struct ppc_linux_features features = ppc_linux_no_features;
2360
2361 features.wordsize = ppc_linux_target_wordsize (tid);
2362
2363 CORE_ADDR hwcap = ppc_linux_get_hwcap ();
2364 CORE_ADDR hwcap2 = ppc_linux_get_hwcap2 ();
2365
2366 if (have_ptrace_getsetvsxregs
2367 && (hwcap & PPC_FEATURE_HAS_VSX))
2368 {
2369 gdb_vsxregset_t vsxregset;
2370
2371 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
2372 features.vsx = true;
2373
2374 /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
2375 Anything else needs to be reported. */
2376 else if (errno != EIO)
2377 perror_with_name (_("Unable to fetch VSX registers"));
2378 }
2379
2380 if (have_ptrace_getvrregs
2381 && (hwcap & PPC_FEATURE_HAS_ALTIVEC))
2382 {
2383 gdb_vrregset_t vrregset;
2384
2385 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
2386 features.altivec = true;
2387
2388 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
2389 Anything else needs to be reported. */
2390 else if (errno != EIO)
2391 perror_with_name (_("Unable to fetch AltiVec registers"));
2392 }
2393
2394 if (hwcap & PPC_FEATURE_CELL)
2395 features.cell = true;
2396
2397 features.isa205 = ppc_linux_has_isa205 (hwcap);
2398
2399 if ((hwcap2 & PPC_FEATURE2_DSCR)
2400 && check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET)
2401 && check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET))
2402 features.ppr_dscr = true;
2403
2404 return ppc_linux_match_description (features);
2405 }
2406
2407 void
2408 _initialize_ppc_linux_nat (void)
2409 {
2410 linux_target = &the_ppc_linux_nat_target;
2411
2412 gdb::observers::thread_exit.attach (ppc_linux_thread_exit);
2413
2414 /* Register the target. */
2415 add_inf_child_target (linux_target);
2416 }
This page took 0.110779 seconds and 5 git commands to generate.