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