ppc-linux-nat.c revision 1.11 1 /* PPC GNU/Linux native support.
2
3 Copyright (C) 1988-2024 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 "extract-store-integer.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "gdbthread.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "regset.h"
27 #include "target.h"
28 #include "linux-nat.h"
29 #include <sys/types.h>
30 #include <signal.h>
31 #include <sys/user.h>
32 #include <sys/ioctl.h>
33 #include <sys/uio.h>
34 #include "gdbsupport/gdb_wait.h"
35 #include <fcntl.h>
36 #include <sys/procfs.h>
37 #include "nat/gdb_ptrace.h"
38 #include "nat/linux-ptrace.h"
39 #include "inf-ptrace.h"
40 #include <algorithm>
41 #include <unordered_map>
42 #include <list>
43
44 /* Prototypes for supply_gregset etc. */
45 #include "gregset.h"
46 #include "ppc-tdep.h"
47 #include "ppc-linux-tdep.h"
48
49 /* Required when using the AUXV. */
50 #include "elf/common.h"
51 #include "auxv.h"
52
53 #include "arch/ppc-linux-common.h"
54 #include "arch/ppc-linux-tdesc.h"
55 #include "nat/ppc-linux.h"
56 #include "linux-tdep.h"
57 #include "expop.h"
58
59 /* Similarly for the hardware watchpoint support. These requests are used
60 when the PowerPC HWDEBUG ptrace interface is not available. */
61 #ifndef PTRACE_GET_DEBUGREG
62 #define PTRACE_GET_DEBUGREG 25
63 #endif
64 #ifndef PTRACE_SET_DEBUGREG
65 #define PTRACE_SET_DEBUGREG 26
66 #endif
67 #ifndef PTRACE_GETSIGINFO
68 #define PTRACE_GETSIGINFO 0x4202
69 #endif
70
71 /* These requests are used when the PowerPC HWDEBUG ptrace interface is
72 available. It exposes the debug facilities of PowerPC processors, as well
73 as additional features of BookE processors, such as ranged breakpoints and
74 watchpoints and hardware-accelerated condition evaluation. */
75 #ifndef PPC_PTRACE_GETHWDBGINFO
76
77 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG
78 ptrace interface is not present in ptrace.h, so we'll have to pretty much
79 include it all here so that the code at least compiles on older systems. */
80 #define PPC_PTRACE_GETHWDBGINFO 0x89
81 #define PPC_PTRACE_SETHWDEBUG 0x88
82 #define PPC_PTRACE_DELHWDEBUG 0x87
83
84 struct ppc_debug_info
85 {
86 uint32_t version; /* Only version 1 exists to date. */
87 uint32_t num_instruction_bps;
88 uint32_t num_data_bps;
89 uint32_t num_condition_regs;
90 uint32_t data_bp_alignment;
91 uint32_t sizeof_condition; /* size of the DVC register. */
92 uint64_t features;
93 };
94
95 /* Features will have bits indicating whether there is support for: */
96 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE 0x1
97 #define PPC_DEBUG_FEATURE_INSN_BP_MASK 0x2
98 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE 0x4
99 #define PPC_DEBUG_FEATURE_DATA_BP_MASK 0x8
100
101 struct ppc_hw_breakpoint
102 {
103 uint32_t version; /* currently, version must be 1 */
104 uint32_t trigger_type; /* only some combinations allowed */
105 uint32_t addr_mode; /* address match mode */
106 uint32_t condition_mode; /* break/watchpoint condition flags */
107 uint64_t addr; /* break/watchpoint address */
108 uint64_t addr2; /* range end or mask */
109 uint64_t condition_value; /* contents of the DVC register */
110 };
111
112 /* Trigger type. */
113 #define PPC_BREAKPOINT_TRIGGER_EXECUTE 0x1
114 #define PPC_BREAKPOINT_TRIGGER_READ 0x2
115 #define PPC_BREAKPOINT_TRIGGER_WRITE 0x4
116 #define PPC_BREAKPOINT_TRIGGER_RW 0x6
117
118 /* Address mode. */
119 #define PPC_BREAKPOINT_MODE_EXACT 0x0
120 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE 0x1
121 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE 0x2
122 #define PPC_BREAKPOINT_MODE_MASK 0x3
123
124 /* Condition mode. */
125 #define PPC_BREAKPOINT_CONDITION_NONE 0x0
126 #define PPC_BREAKPOINT_CONDITION_AND 0x1
127 #define PPC_BREAKPOINT_CONDITION_EXACT 0x1
128 #define PPC_BREAKPOINT_CONDITION_OR 0x2
129 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
130 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
131 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT 16
132 #define PPC_BREAKPOINT_CONDITION_BE(n) \
133 (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
134 #endif /* PPC_PTRACE_GETHWDBGINFO */
135
136 /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
137 watchpoint (up to 512 bytes). */
138 #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
139 #define PPC_DEBUG_FEATURE_DATA_BP_DAWR 0x10
140 #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
141
142 /* Feature defined on Linux kernel v5.1: Second watchpoint support. */
143 #ifndef PPC_DEBUG_FEATURE_DATA_BP_ARCH_31
144 #define PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 0x20
145 #endif /* PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 */
146
147 /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
148 available. */
149 #define PPC_DEBUG_CURRENT_VERSION 1
150
151 /* Similarly for the general-purpose (gp0 -- gp31)
152 and floating-point registers (fp0 -- fp31). */
153 #ifndef PTRACE_GETREGS
154 #define PTRACE_GETREGS 12
155 #endif
156 #ifndef PTRACE_SETREGS
157 #define PTRACE_SETREGS 13
158 #endif
159 #ifndef PTRACE_GETFPREGS
160 #define PTRACE_GETFPREGS 14
161 #endif
162 #ifndef PTRACE_SETFPREGS
163 #define PTRACE_SETFPREGS 15
164 #endif
165
166 /* This oddity is because the Linux kernel defines elf_vrregset_t as
167 an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
168 However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
169 the vrsave as an extra 4 bytes at the end. I opted for creating a
170 flat array of chars, so that it is easier to manipulate for gdb.
171
172 There are 32 vector registers 16 bytes longs, plus a VSCR register
173 which is only 4 bytes long, but is fetched as a 16 bytes
174 quantity. Up to here we have the elf_vrregset_t structure.
175 Appended to this there is space for the VRSAVE register: 4 bytes.
176 Even though this vrsave register is not included in the regset
177 typedef, it is handled by the ptrace requests.
178
179 The layout is like this (where x is the actual value of the vscr reg): */
180
181 /*
182 Big-Endian:
183 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
184 <-------> <-------><-------><->
185 VR0 VR31 VSCR VRSAVE
186 Little-Endian:
187 |.|.|.|.|.....|.|.|.|.||X|.|.|.||.|
188 <-------> <-------><-------><->
189 VR0 VR31 VSCR VRSAVE
190 */
191
192 typedef char gdb_vrregset_t[PPC_LINUX_SIZEOF_VRREGSET];
193
194 /* This is the layout of the POWER7 VSX registers and the way they overlap
195 with the existing FPR and VMX registers.
196
197 VSR doubleword 0 VSR doubleword 1
198 ----------------------------------------------------------------
199 VSR[0] | FPR[0] | |
200 ----------------------------------------------------------------
201 VSR[1] | FPR[1] | |
202 ----------------------------------------------------------------
203 | ... | |
204 | ... | |
205 ----------------------------------------------------------------
206 VSR[30] | FPR[30] | |
207 ----------------------------------------------------------------
208 VSR[31] | FPR[31] | |
209 ----------------------------------------------------------------
210 VSR[32] | VR[0] |
211 ----------------------------------------------------------------
212 VSR[33] | VR[1] |
213 ----------------------------------------------------------------
214 | ... |
215 | ... |
216 ----------------------------------------------------------------
217 VSR[62] | VR[30] |
218 ----------------------------------------------------------------
219 VSR[63] | VR[31] |
220 ----------------------------------------------------------------
221
222 VSX has 64 128bit registers. The first 32 registers overlap with
223 the FP registers (doubleword 0) and hence extend them with additional
224 64 bits (doubleword 1). The other 32 regs overlap with the VMX
225 registers. */
226 typedef char gdb_vsxregset_t[PPC_LINUX_SIZEOF_VSXREGSET];
227
228 /* On PPC processors that support the Signal Processing Extension
229 (SPE) APU, the general-purpose registers are 64 bits long.
230 However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
231 ptrace calls only access the lower half of each register, to allow
232 them to behave the same way they do on non-SPE systems. There's a
233 separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
234 read and write the top halves of all the general-purpose registers
235 at once, along with some SPE-specific registers.
236
237 GDB itself continues to claim the general-purpose registers are 32
238 bits long. It has unnamed raw registers that hold the upper halves
239 of the gprs, and the full 64-bit SIMD views of the registers,
240 'ev0' -- 'ev31', are pseudo-registers that splice the top and
241 bottom halves together.
242
243 This is the structure filled in by PTRACE_GETEVRREGS and written to
244 the inferior's registers by PTRACE_SETEVRREGS. */
245 struct gdb_evrregset_t
246 {
247 unsigned long evr[32];
248 unsigned long long acc;
249 unsigned long spefscr;
250 };
251
252 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
253 PTRACE_SETVSXREGS requests, for reading and writing the VSX
254 POWER7 registers 0 through 31. Zero if we've tried one of them and
255 gotten an error. Note that VSX registers 32 through 63 overlap
256 with VR registers 0 through 31. */
257 int have_ptrace_getsetvsxregs = 1;
258
259 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
260 PTRACE_SETVRREGS requests, for reading and writing the Altivec
261 registers. Zero if we've tried one of them and gotten an
262 error. */
263 int have_ptrace_getvrregs = 1;
264
265 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
266 PTRACE_SETEVRREGS requests, for reading and writing the SPE
267 registers. Zero if we've tried one of them and gotten an
268 error. */
269 int have_ptrace_getsetevrregs = 1;
270
271 /* Non-zero if our kernel may support the PTRACE_GETREGS and
272 PTRACE_SETREGS requests, for reading and writing the
273 general-purpose registers. Zero if we've tried one of
274 them and gotten an error. */
275 int have_ptrace_getsetregs = 1;
276
277 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
278 PTRACE_SETFPREGS requests, for reading and writing the
279 floating-pointers registers. Zero if we've tried one of
280 them and gotten an error. */
281 int have_ptrace_getsetfpregs = 1;
282
283 /* Private arch info associated with each thread lwp_info object, used
284 for debug register handling. */
285
286 struct arch_lwp_info
287 {
288 /* When true, indicates that the debug registers installed in the
289 thread no longer correspond to the watchpoints and breakpoints
290 requested by GDB. */
291 bool debug_regs_stale;
292
293 /* We need a back-reference to the PTID of the thread so that we can
294 cleanup the debug register state of the thread in
295 low_delete_thread. */
296 ptid_t lwp_ptid;
297 };
298
299 /* Class used to detect which set of ptrace requests that
300 ppc_linux_nat_target will use to install and remove hardware
301 breakpoints and watchpoints.
302
303 The interface is only detected once, testing the ptrace calls. The
304 result can indicate that no interface is available.
305
306 The Linux kernel provides two different sets of ptrace requests to
307 handle hardware watchpoints and breakpoints for Power:
308
309 - PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
310 PPC_PTRACE_DELHWDEBUG.
311
312 Or
313
314 - PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
315
316 The first set is the more flexible one and allows setting watchpoints
317 with a variable watched region length and, for BookE processors,
318 multiple types of debug registers (e.g. hardware breakpoints and
319 hardware-assisted conditions for watchpoints). The second one only
320 allows setting one debug register, a watchpoint, so we only use it if
321 the first one is not available. */
322
323 class ppc_linux_dreg_interface
324 {
325 public:
326
327 ppc_linux_dreg_interface ()
328 : m_interface (), m_hwdebug_info ()
329 {
330 };
331
332 DISABLE_COPY_AND_ASSIGN (ppc_linux_dreg_interface);
333
334 /* One and only one of these three functions returns true, indicating
335 whether the corresponding interface is the one we detected. The
336 interface must already have been detected as a precondition. */
337
338 bool hwdebug_p ()
339 {
340 gdb_assert (detected_p ());
341 return *m_interface == HWDEBUG;
342 }
343
344 bool debugreg_p ()
345 {
346 gdb_assert (detected_p ());
347 return *m_interface == DEBUGREG;
348 }
349
350 bool unavailable_p ()
351 {
352 gdb_assert (detected_p ());
353 return *m_interface == UNAVAILABLE;
354 }
355
356 /* Returns the debug register capabilities of the target. Should only
357 be called if the interface is HWDEBUG. */
358 const struct ppc_debug_info &hwdebug_info ()
359 {
360 gdb_assert (hwdebug_p ());
361
362 return m_hwdebug_info;
363 }
364
365 /* Returns true if the interface has already been detected. This is
366 useful for cases when we know there is no work to be done if the
367 interface hasn't been detected yet. */
368 bool detected_p ()
369 {
370 return m_interface.has_value ();
371 }
372
373 /* Detect the available interface, if any, if it hasn't been detected
374 before, using PTID for the necessary ptrace calls. */
375
376 void detect (const ptid_t &ptid)
377 {
378 if (m_interface.has_value ())
379 return;
380
381 gdb_assert (ptid.lwp_p ());
382
383 bool no_features = false;
384
385 if (ptrace (PPC_PTRACE_GETHWDBGINFO, ptid.lwp (), 0, &m_hwdebug_info)
386 >= 0)
387 {
388 /* If there are no advertised features, we don't use the
389 HWDEBUG interface and try the DEBUGREG interface instead.
390 It shouldn't be necessary to do this, however, when the
391 kernel is configured without CONFIG_HW_BREAKPOINTS (selected
392 by CONFIG_PERF_EVENTS), there is a bug that causes
393 watchpoints installed with the HWDEBUG interface not to
394 trigger. When this is the case, features will be zero,
395 which we use as an indicator to fall back to the DEBUGREG
396 interface. */
397 if (m_hwdebug_info.features != 0)
398 {
399 m_interface.emplace (HWDEBUG);
400 return;
401 }
402 else
403 no_features = true;
404 }
405
406 /* EIO indicates that the request is invalid, so we try DEBUGREG
407 next. Technically, it can also indicate other failures, but we
408 can't differentiate those.
409
410 Other errors could happen for various reasons. We could get an
411 ESRCH if the traced thread was killed by a signal. Trying to
412 detect the interface with another thread in the future would be
413 complicated, as callers would have to handle an "unknown
414 interface" case. It's also unclear if raising an exception
415 here would be safe.
416
417 Other errors, such as ENODEV, could be more permanent and cause
418 a failure for any thread.
419
420 For simplicity, with all errors other than EIO, we set the
421 interface to UNAVAILABLE and don't try DEBUGREG. If DEBUGREG
422 fails too, we'll also set the interface to UNAVAILABLE. It's
423 unlikely that trying the DEBUGREG interface with this same thread
424 would work, for errors other than EIO. This means that these
425 errors will cause hardware watchpoints and breakpoints to become
426 unavailable throughout a GDB session. */
427
428 if (no_features || errno == EIO)
429 {
430 unsigned long wp;
431
432 if (ptrace (PTRACE_GET_DEBUGREG, ptid.lwp (), 0, &wp) >= 0)
433 {
434 m_interface.emplace (DEBUGREG);
435 return;
436 }
437 }
438
439 if (errno != EIO)
440 warning (_("Error when detecting the debug register interface. "
441 "Debug registers will be unavailable."));
442
443 m_interface.emplace (UNAVAILABLE);
444 return;
445 }
446
447 private:
448
449 /* HWDEBUG represents the set of calls PPC_PTRACE_GETHWDBGINFO,
450 PPC_PTRACE_SETHWDEBUG and PPC_PTRACE_DELHWDEBUG.
451
452 DEBUGREG represents the set of calls PTRACE_SET_DEBUGREG and
453 PTRACE_GET_DEBUGREG.
454
455 UNAVAILABLE can indicate that the kernel doesn't support any of the
456 two sets of requests or that there was an error when we tried to
457 detect which interface is available. */
458
459 enum debug_reg_interface
460 {
461 UNAVAILABLE,
462 HWDEBUG,
463 DEBUGREG
464 };
465
466 /* The interface option. Initialized if has_value () returns true. */
467 std::optional<enum debug_reg_interface> m_interface;
468
469 /* The info returned by the kernel with PPC_PTRACE_GETHWDBGINFO. Only
470 valid if we determined that the interface is HWDEBUG. */
471 struct ppc_debug_info m_hwdebug_info;
472 };
473
474 /* Per-process information. This includes the hardware watchpoints and
475 breakpoints that GDB requested to this target. */
476
477 struct ppc_linux_process_info
478 {
479 /* The list of hardware watchpoints and breakpoints that GDB requested
480 for this process.
481
482 Only used when the interface is HWDEBUG. */
483 std::list<struct ppc_hw_breakpoint> requested_hw_bps;
484
485 /* The watchpoint value that GDB requested for this process.
486
487 Only used when the interface is DEBUGREG. */
488 std::optional<long> requested_wp_val;
489 };
490
491 struct ppc_linux_nat_target final : public linux_nat_target
492 {
493 /* Add our register access methods. */
494 void fetch_registers (struct regcache *, int) override;
495 void store_registers (struct regcache *, int) override;
496
497 /* Add our breakpoint/watchpoint methods. */
498 int can_use_hw_breakpoint (enum bptype, int, int) override;
499
500 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
501 override;
502
503 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
504 override;
505
506 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
507
508 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
509 struct expression *) override;
510
511 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
512 struct expression *) override;
513
514 int insert_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
515 override;
516
517 int remove_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
518 override;
519
520 bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
521
522 bool can_accel_watchpoint_condition (CORE_ADDR, int, int, struct expression *)
523 override;
524
525 int masked_watch_num_registers (CORE_ADDR, CORE_ADDR) override;
526
527 int ranged_break_num_registers () override;
528
529 const struct target_desc *read_description () override;
530
531 int auxv_parse (const gdb_byte **readptr,
532 const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
533 override;
534
535 /* Override linux_nat_target low methods. */
536 bool low_stopped_by_watchpoint () override;
537
538 bool low_stopped_data_address (CORE_ADDR *) override;
539
540 void low_new_thread (struct lwp_info *lp) override;
541
542 void low_delete_thread (arch_lwp_info *) override;
543
544 void low_new_fork (struct lwp_info *, pid_t) override;
545
546 void low_new_clone (struct lwp_info *, pid_t) override;
547
548 void low_init_process (pid_t pid) override;
549
550 void low_forget_process (pid_t pid) override;
551
552 void low_prepare_to_resume (struct lwp_info *) override;
553
554 private:
555
556 void copy_thread_dreg_state (const ptid_t &parent_ptid,
557 const ptid_t &child_ptid);
558
559 void mark_thread_stale (struct lwp_info *lp);
560
561 void mark_debug_registers_changed (pid_t pid);
562
563 void register_hw_breakpoint (pid_t pid,
564 const struct ppc_hw_breakpoint &bp);
565
566 void clear_hw_breakpoint (pid_t pid,
567 const struct ppc_hw_breakpoint &a);
568
569 void register_wp (pid_t pid, long wp_value);
570
571 void clear_wp (pid_t pid);
572
573 bool can_use_watchpoint_cond_accel (void);
574
575 void calculate_dvc (CORE_ADDR addr, int len,
576 CORE_ADDR data_value,
577 uint32_t *condition_mode,
578 uint64_t *condition_value);
579
580 int check_condition (CORE_ADDR watch_addr,
581 struct expression *cond,
582 CORE_ADDR *data_value, int *len);
583
584 int num_memory_accesses (const std::vector<value_ref_ptr> &chain);
585
586 int get_trigger_type (enum target_hw_bp_type type);
587
588 void create_watchpoint_request (struct ppc_hw_breakpoint *p,
589 CORE_ADDR addr,
590 int len,
591 enum target_hw_bp_type type,
592 struct expression *cond,
593 int insert);
594
595 bool hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
596 const struct ppc_hw_breakpoint &b);
597
598 void init_arch_lwp_info (struct lwp_info *lp);
599
600 arch_lwp_info *get_arch_lwp_info (struct lwp_info *lp);
601
602 /* The ptrace interface we'll use to install hardware watchpoints and
603 breakpoints (debug registers). */
604 ppc_linux_dreg_interface m_dreg_interface;
605
606 /* A map from pids to structs containing info specific to each
607 process. */
608 std::unordered_map<pid_t, ppc_linux_process_info> m_process_info;
609
610 /* Callable object to hash ptids by their lwp number. */
611 struct ptid_hash
612 {
613 std::size_t operator() (const ptid_t &ptid) const
614 {
615 return std::hash<long>{} (ptid.lwp ());
616 }
617 };
618
619 /* A map from ptid_t objects to a list of pairs of slots and hardware
620 breakpoint objects. This keeps track of which hardware breakpoints
621 and watchpoints were last installed in each slot of each thread.
622
623 Only used when the interface is HWDEBUG. */
624 std::unordered_map <ptid_t,
625 std::list<std::pair<long, ppc_hw_breakpoint>>,
626 ptid_hash> m_installed_hw_bps;
627 };
628
629 static ppc_linux_nat_target the_ppc_linux_nat_target;
630
631 /* registers layout, as presented by the ptrace interface:
632 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
633 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
634 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
635 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
636 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
637 PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
638 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
639 PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
640 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
641 PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
642 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
643 PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
644 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
645
646 static int
647 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
648 {
649 int u_addr = -1;
650 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
651 /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
652 interface, and not the wordsize of the program's ABI. */
653 int wordsize = sizeof (long);
654
655 /* General purpose registers occupy 1 slot each in the buffer. */
656 if (regno >= tdep->ppc_gp0_regnum
657 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
658 u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
659
660 /* Floating point regs: eight bytes each in both 32- and 64-bit
661 ptrace interfaces. Thus, two slots each in 32-bit interface, one
662 slot each in 64-bit interface. */
663 if (tdep->ppc_fp0_regnum >= 0
664 && regno >= tdep->ppc_fp0_regnum
665 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
666 u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
667
668 /* UISA special purpose registers: 1 slot each. */
669 if (regno == gdbarch_pc_regnum (gdbarch))
670 u_addr = PT_NIP * wordsize;
671 if (regno == tdep->ppc_lr_regnum)
672 u_addr = PT_LNK * wordsize;
673 if (regno == tdep->ppc_cr_regnum)
674 u_addr = PT_CCR * wordsize;
675 if (regno == tdep->ppc_xer_regnum)
676 u_addr = PT_XER * wordsize;
677 if (regno == tdep->ppc_ctr_regnum)
678 u_addr = PT_CTR * wordsize;
679 #ifdef PT_MQ
680 if (regno == tdep->ppc_mq_regnum)
681 u_addr = PT_MQ * wordsize;
682 #endif
683 if (regno == tdep->ppc_ps_regnum)
684 u_addr = PT_MSR * wordsize;
685 if (regno == PPC_ORIG_R3_REGNUM)
686 u_addr = PT_ORIG_R3 * wordsize;
687 if (regno == PPC_TRAP_REGNUM)
688 u_addr = PT_TRAP * wordsize;
689 if (tdep->ppc_fpscr_regnum >= 0
690 && regno == tdep->ppc_fpscr_regnum)
691 {
692 /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
693 kernel headers incorrectly contained the 32-bit definition of
694 PT_FPSCR. For the 32-bit definition, floating-point
695 registers occupy two 32-bit "slots", and the FPSCR lives in
696 the second half of such a slot-pair (hence +1). For 64-bit,
697 the FPSCR instead occupies the full 64-bit 2-word-slot and
698 hence no adjustment is necessary. Hack around this. */
699 if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
700 u_addr = (48 + 32) * wordsize;
701 /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
702 slot and not just its second word. The PT_FPSCR supplied when
703 GDB is compiled as a 32-bit app doesn't reflect this. */
704 else if (wordsize == 4 && register_size (gdbarch, regno) == 8
705 && PT_FPSCR == (48 + 2*32 + 1))
706 u_addr = (48 + 2*32) * wordsize;
707 else
708 u_addr = PT_FPSCR * wordsize;
709 }
710 return u_addr;
711 }
712
713 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
714 registers set mechanism, as opposed to the interface for all the
715 other registers, that stores/fetches each register individually. */
716 static void
717 fetch_vsx_registers (struct regcache *regcache, int tid, int regno)
718 {
719 int ret;
720 gdb_vsxregset_t regs;
721 const struct regset *vsxregset = ppc_linux_vsxregset ();
722
723 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
724 if (ret < 0)
725 {
726 if (errno == EIO)
727 {
728 have_ptrace_getsetvsxregs = 0;
729 return;
730 }
731 perror_with_name (_("Unable to fetch VSX registers"));
732 }
733
734 vsxregset->supply_regset (vsxregset, regcache, regno, ®s,
735 PPC_LINUX_SIZEOF_VSXREGSET);
736 }
737
738 /* The Linux kernel ptrace interface for AltiVec registers uses the
739 registers set mechanism, as opposed to the interface for all the
740 other registers, that stores/fetches each register individually. */
741 static void
742 fetch_altivec_registers (struct regcache *regcache, int tid,
743 int regno)
744 {
745 int ret;
746 gdb_vrregset_t regs;
747 struct gdbarch *gdbarch = regcache->arch ();
748 const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
749
750 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
751 if (ret < 0)
752 {
753 if (errno == EIO)
754 {
755 have_ptrace_getvrregs = 0;
756 return;
757 }
758 perror_with_name (_("Unable to fetch AltiVec registers"));
759 }
760
761 vrregset->supply_regset (vrregset, regcache, regno, ®s,
762 PPC_LINUX_SIZEOF_VRREGSET);
763 }
764
765 /* Fetch the top 32 bits of TID's general-purpose registers and the
766 SPE-specific registers, and place the results in EVRREGSET. If we
767 don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
768 zeros.
769
770 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
771 PTRACE_SETEVRREGS requests are supported is isolated here, and in
772 set_spe_registers. */
773 static void
774 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
775 {
776 if (have_ptrace_getsetevrregs)
777 {
778 if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
779 return;
780 else
781 {
782 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
783 we just return zeros. */
784 if (errno == EIO)
785 have_ptrace_getsetevrregs = 0;
786 else
787 /* Anything else needs to be reported. */
788 perror_with_name (_("Unable to fetch SPE registers"));
789 }
790 }
791
792 memset (evrregset, 0, sizeof (*evrregset));
793 }
794
795 /* Supply values from TID for SPE-specific raw registers: the upper
796 halves of the GPRs, the accumulator, and the spefscr. REGNO must
797 be the number of an upper half register, acc, spefscr, or -1 to
798 supply the values of all registers. */
799 static void
800 fetch_spe_register (struct regcache *regcache, int tid, int regno)
801 {
802 struct gdbarch *gdbarch = regcache->arch ();
803 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
804 struct gdb_evrregset_t evrregs;
805
806 gdb_assert (sizeof (evrregs.evr[0])
807 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
808 gdb_assert (sizeof (evrregs.acc)
809 == register_size (gdbarch, tdep->ppc_acc_regnum));
810 gdb_assert (sizeof (evrregs.spefscr)
811 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
812
813 get_spe_registers (tid, &evrregs);
814
815 if (regno == -1)
816 {
817 int i;
818
819 for (i = 0; i < ppc_num_gprs; i++)
820 regcache->raw_supply (tdep->ppc_ev0_upper_regnum + i, &evrregs.evr[i]);
821 }
822 else if (tdep->ppc_ev0_upper_regnum <= regno
823 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
824 regcache->raw_supply (regno,
825 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
826
827 if (regno == -1
828 || regno == tdep->ppc_acc_regnum)
829 regcache->raw_supply (tdep->ppc_acc_regnum, &evrregs.acc);
830
831 if (regno == -1
832 || regno == tdep->ppc_spefscr_regnum)
833 regcache->raw_supply (tdep->ppc_spefscr_regnum, &evrregs.spefscr);
834 }
835
836 /* Use ptrace to fetch all registers from the register set with note
837 type REGSET_ID, size REGSIZE, and layout described by REGSET, from
838 process/thread TID and supply their values to REGCACHE. If ptrace
839 returns ENODATA to indicate the regset is unavailable, mark the
840 registers as unavailable in REGCACHE. */
841
842 static void
843 fetch_regset (struct regcache *regcache, int tid,
844 int regset_id, int regsetsize, const struct regset *regset)
845 {
846 void *buf = alloca (regsetsize);
847 struct iovec iov;
848
849 iov.iov_base = buf;
850 iov.iov_len = regsetsize;
851
852 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
853 {
854 if (errno == ENODATA)
855 regset->supply_regset (regset, regcache, -1, NULL, regsetsize);
856 else
857 perror_with_name (_("Couldn't get register set"));
858 }
859 else
860 regset->supply_regset (regset, regcache, -1, buf, regsetsize);
861 }
862
863 /* Use ptrace to store register REGNUM of the regset with note type
864 REGSET_ID, size REGSETSIZE, and layout described by REGSET, from
865 REGCACHE back to process/thread TID. If REGNUM is -1 all registers
866 in the set are collected and stored. */
867
868 static void
869 store_regset (const struct regcache *regcache, int tid, int regnum,
870 int regset_id, int regsetsize, const struct regset *regset)
871 {
872 void *buf = alloca (regsetsize);
873 struct iovec iov;
874
875 iov.iov_base = buf;
876 iov.iov_len = regsetsize;
877
878 /* Make sure that the buffer that will be stored has up to date values
879 for the registers that won't be collected. */
880 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
881 perror_with_name (_("Couldn't get register set"));
882
883 regset->collect_regset (regset, regcache, regnum, buf, regsetsize);
884
885 if (ptrace (PTRACE_SETREGSET, tid, regset_id, &iov) < 0)
886 perror_with_name (_("Couldn't set register set"));
887 }
888
889 /* Check whether the kernel provides a register set with number
890 REGSET_ID of size REGSETSIZE for process/thread TID. */
891
892 static bool
893 check_regset (int tid, int regset_id, int regsetsize)
894 {
895 void *buf = alloca (regsetsize);
896 struct iovec iov;
897
898 iov.iov_base = buf;
899 iov.iov_len = regsetsize;
900
901 if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0
902 || errno == ENODATA)
903 return true;
904 else
905 return false;
906 }
907
908 static void
909 fetch_register (struct regcache *regcache, int tid, int regno)
910 {
911 struct gdbarch *gdbarch = regcache->arch ();
912 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
913 /* This isn't really an address. But ptrace thinks of it as one. */
914 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
915 int bytes_transferred;
916 gdb_byte buf[PPC_MAX_REGISTER_SIZE];
917
918 if (altivec_register_p (gdbarch, regno))
919 {
920 /* If this is the first time through, or if it is not the first
921 time through, and we have confirmed that there is kernel
922 support for such a ptrace request, then go and fetch the
923 register. */
924 if (have_ptrace_getvrregs)
925 {
926 fetch_altivec_registers (regcache, tid, regno);
927 return;
928 }
929 /* If we have discovered that there is no ptrace support for
930 AltiVec registers, fall through and return zeroes, because
931 regaddr will be -1 in this case. */
932 }
933 else if (vsx_register_p (gdbarch, regno))
934 {
935 if (have_ptrace_getsetvsxregs)
936 {
937 fetch_vsx_registers (regcache, tid, regno);
938 return;
939 }
940 }
941 else if (spe_register_p (gdbarch, regno))
942 {
943 fetch_spe_register (regcache, tid, regno);
944 return;
945 }
946 else if (regno == PPC_DSCR_REGNUM)
947 {
948 gdb_assert (tdep->ppc_dscr_regnum != -1);
949
950 fetch_regset (regcache, tid, NT_PPC_DSCR,
951 PPC_LINUX_SIZEOF_DSCRREGSET,
952 &ppc32_linux_dscrregset);
953 return;
954 }
955 else if (regno == PPC_PPR_REGNUM)
956 {
957 gdb_assert (tdep->ppc_ppr_regnum != -1);
958
959 fetch_regset (regcache, tid, NT_PPC_PPR,
960 PPC_LINUX_SIZEOF_PPRREGSET,
961 &ppc32_linux_pprregset);
962 return;
963 }
964 else if (regno == PPC_TAR_REGNUM)
965 {
966 gdb_assert (tdep->ppc_tar_regnum != -1);
967
968 fetch_regset (regcache, tid, NT_PPC_TAR,
969 PPC_LINUX_SIZEOF_TARREGSET,
970 &ppc32_linux_tarregset);
971 return;
972 }
973 else if (PPC_IS_EBB_REGNUM (regno))
974 {
975 gdb_assert (tdep->have_ebb);
976
977 fetch_regset (regcache, tid, NT_PPC_EBB,
978 PPC_LINUX_SIZEOF_EBBREGSET,
979 &ppc32_linux_ebbregset);
980 return;
981 }
982 else if (PPC_IS_PMU_REGNUM (regno))
983 {
984 gdb_assert (tdep->ppc_mmcr0_regnum != -1);
985
986 fetch_regset (regcache, tid, NT_PPC_PMU,
987 PPC_LINUX_SIZEOF_PMUREGSET,
988 &ppc32_linux_pmuregset);
989 return;
990 }
991 else if (PPC_IS_TMSPR_REGNUM (regno))
992 {
993 gdb_assert (tdep->have_htm_spr);
994
995 fetch_regset (regcache, tid, NT_PPC_TM_SPR,
996 PPC_LINUX_SIZEOF_TM_SPRREGSET,
997 &ppc32_linux_tm_sprregset);
998 return;
999 }
1000 else if (PPC_IS_CKPTGP_REGNUM (regno))
1001 {
1002 gdb_assert (tdep->have_htm_core);
1003
1004 const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
1005 fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
1006 (tdep->wordsize == 4?
1007 PPC32_LINUX_SIZEOF_CGPRREGSET
1008 : PPC64_LINUX_SIZEOF_CGPRREGSET),
1009 cgprregset);
1010 return;
1011 }
1012 else if (PPC_IS_CKPTFP_REGNUM (regno))
1013 {
1014 gdb_assert (tdep->have_htm_fpu);
1015
1016 fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
1017 PPC_LINUX_SIZEOF_CFPRREGSET,
1018 &ppc32_linux_cfprregset);
1019 return;
1020 }
1021 else if (PPC_IS_CKPTVMX_REGNUM (regno))
1022 {
1023 gdb_assert (tdep->have_htm_altivec);
1024
1025 const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
1026 fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
1027 PPC_LINUX_SIZEOF_CVMXREGSET,
1028 cvmxregset);
1029 return;
1030 }
1031 else if (PPC_IS_CKPTVSX_REGNUM (regno))
1032 {
1033 gdb_assert (tdep->have_htm_vsx);
1034
1035 fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
1036 PPC_LINUX_SIZEOF_CVSXREGSET,
1037 &ppc32_linux_cvsxregset);
1038 return;
1039 }
1040 else if (regno == PPC_CPPR_REGNUM)
1041 {
1042 gdb_assert (tdep->ppc_cppr_regnum != -1);
1043
1044 fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
1045 PPC_LINUX_SIZEOF_CPPRREGSET,
1046 &ppc32_linux_cpprregset);
1047 return;
1048 }
1049 else if (regno == PPC_CDSCR_REGNUM)
1050 {
1051 gdb_assert (tdep->ppc_cdscr_regnum != -1);
1052
1053 fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
1054 PPC_LINUX_SIZEOF_CDSCRREGSET,
1055 &ppc32_linux_cdscrregset);
1056 return;
1057 }
1058 else if (regno == PPC_CTAR_REGNUM)
1059 {
1060 gdb_assert (tdep->ppc_ctar_regnum != -1);
1061
1062 fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
1063 PPC_LINUX_SIZEOF_CTARREGSET,
1064 &ppc32_linux_ctarregset);
1065 return;
1066 }
1067
1068 if (regaddr == -1)
1069 {
1070 memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
1071 regcache->raw_supply (regno, buf);
1072 return;
1073 }
1074
1075 /* Read the raw register using sizeof(long) sized chunks. On a
1076 32-bit platform, 64-bit floating-point registers will require two
1077 transfers. */
1078 for (bytes_transferred = 0;
1079 bytes_transferred < register_size (gdbarch, regno);
1080 bytes_transferred += sizeof (long))
1081 {
1082 long l;
1083
1084 errno = 0;
1085 l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
1086 regaddr += sizeof (long);
1087 if (errno != 0)
1088 {
1089 char message[128];
1090 xsnprintf (message, sizeof (message), "reading register %s (#%d)",
1091 gdbarch_register_name (gdbarch, regno), regno);
1092 perror_with_name (message);
1093 }
1094 memcpy (&buf[bytes_transferred], &l, sizeof (l));
1095 }
1096
1097 /* Now supply the register. Keep in mind that the regcache's idea
1098 of the register's size may not be a multiple of sizeof
1099 (long). */
1100 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1101 {
1102 /* Little-endian values are always found at the left end of the
1103 bytes transferred. */
1104 regcache->raw_supply (regno, buf);
1105 }
1106 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1107 {
1108 /* Big-endian values are found at the right end of the bytes
1109 transferred. */
1110 size_t padding = (bytes_transferred - register_size (gdbarch, regno));
1111 regcache->raw_supply (regno, buf + padding);
1112 }
1113 else
1114 internal_error (_("fetch_register: unexpected byte order: %d"),
1115 gdbarch_byte_order (gdbarch));
1116 }
1117
1118 /* This function actually issues the request to ptrace, telling
1119 it to get all general-purpose registers and put them into the
1120 specified regset.
1121
1122 If the ptrace request does not exist, this function returns 0
1123 and properly sets the have_ptrace_* flag. If the request fails,
1124 this function calls perror_with_name. Otherwise, if the request
1125 succeeds, then the regcache gets filled and 1 is returned. */
1126 static int
1127 fetch_all_gp_regs (struct regcache *regcache, int tid)
1128 {
1129 gdb_gregset_t gregset;
1130
1131 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1132 {
1133 if (errno == EIO)
1134 {
1135 have_ptrace_getsetregs = 0;
1136 return 0;
1137 }
1138 perror_with_name (_("Couldn't get general-purpose registers"));
1139 }
1140
1141 supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
1142
1143 return 1;
1144 }
1145
1146 /* This is a wrapper for the fetch_all_gp_regs function. It is
1147 responsible for verifying if this target has the ptrace request
1148 that can be used to fetch all general-purpose registers at one
1149 shot. If it doesn't, then we should fetch them using the
1150 old-fashioned way, which is to iterate over the registers and
1151 request them one by one. */
1152 static void
1153 fetch_gp_regs (struct regcache *regcache, int tid)
1154 {
1155 struct gdbarch *gdbarch = regcache->arch ();
1156 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1157 int i;
1158
1159 if (have_ptrace_getsetregs)
1160 if (fetch_all_gp_regs (regcache, tid))
1161 return;
1162
1163 /* If we've hit this point, it doesn't really matter which
1164 architecture we are using. We just need to read the
1165 registers in the "old-fashioned way". */
1166 for (i = 0; i < ppc_num_gprs; i++)
1167 fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1168 }
1169
1170 /* This function actually issues the request to ptrace, telling
1171 it to get all floating-point registers and put them into the
1172 specified regset.
1173
1174 If the ptrace request does not exist, this function returns 0
1175 and properly sets the have_ptrace_* flag. If the request fails,
1176 this function calls perror_with_name. Otherwise, if the request
1177 succeeds, then the regcache gets filled and 1 is returned. */
1178 static int
1179 fetch_all_fp_regs (struct regcache *regcache, int tid)
1180 {
1181 gdb_fpregset_t fpregs;
1182
1183 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1184 {
1185 if (errno == EIO)
1186 {
1187 have_ptrace_getsetfpregs = 0;
1188 return 0;
1189 }
1190 perror_with_name (_("Couldn't get floating-point registers"));
1191 }
1192
1193 supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
1194
1195 return 1;
1196 }
1197
1198 /* This is a wrapper for the fetch_all_fp_regs function. It is
1199 responsible for verifying if this target has the ptrace request
1200 that can be used to fetch all floating-point registers at one
1201 shot. If it doesn't, then we should fetch them using the
1202 old-fashioned way, which is to iterate over the registers and
1203 request them one by one. */
1204 static void
1205 fetch_fp_regs (struct regcache *regcache, int tid)
1206 {
1207 struct gdbarch *gdbarch = regcache->arch ();
1208 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1209 int i;
1210
1211 if (have_ptrace_getsetfpregs)
1212 if (fetch_all_fp_regs (regcache, tid))
1213 return;
1214
1215 /* If we've hit this point, it doesn't really matter which
1216 architecture we are using. We just need to read the
1217 registers in the "old-fashioned way". */
1218 for (i = 0; i < ppc_num_fprs; i++)
1219 fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
1220 }
1221
1222 static void
1223 fetch_ppc_registers (struct regcache *regcache, int tid)
1224 {
1225 struct gdbarch *gdbarch = regcache->arch ();
1226 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1227
1228 fetch_gp_regs (regcache, tid);
1229 if (tdep->ppc_fp0_regnum >= 0)
1230 fetch_fp_regs (regcache, tid);
1231 fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1232 if (tdep->ppc_ps_regnum != -1)
1233 fetch_register (regcache, tid, tdep->ppc_ps_regnum);
1234 if (tdep->ppc_cr_regnum != -1)
1235 fetch_register (regcache, tid, tdep->ppc_cr_regnum);
1236 if (tdep->ppc_lr_regnum != -1)
1237 fetch_register (regcache, tid, tdep->ppc_lr_regnum);
1238 if (tdep->ppc_ctr_regnum != -1)
1239 fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
1240 if (tdep->ppc_xer_regnum != -1)
1241 fetch_register (regcache, tid, tdep->ppc_xer_regnum);
1242 if (tdep->ppc_mq_regnum != -1)
1243 fetch_register (regcache, tid, tdep->ppc_mq_regnum);
1244 if (ppc_linux_trap_reg_p (gdbarch))
1245 {
1246 fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1247 fetch_register (regcache, tid, PPC_TRAP_REGNUM);
1248 }
1249 if (tdep->ppc_fpscr_regnum != -1)
1250 fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
1251 if (have_ptrace_getvrregs)
1252 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1253 fetch_altivec_registers (regcache, tid, -1);
1254 if (have_ptrace_getsetvsxregs)
1255 if (tdep->ppc_vsr0_upper_regnum != -1)
1256 fetch_vsx_registers (regcache, tid, -1);
1257 if (tdep->ppc_ev0_upper_regnum >= 0)
1258 fetch_spe_register (regcache, tid, -1);
1259 if (tdep->ppc_ppr_regnum != -1)
1260 fetch_regset (regcache, tid, NT_PPC_PPR,
1261 PPC_LINUX_SIZEOF_PPRREGSET,
1262 &ppc32_linux_pprregset);
1263 if (tdep->ppc_dscr_regnum != -1)
1264 fetch_regset (regcache, tid, NT_PPC_DSCR,
1265 PPC_LINUX_SIZEOF_DSCRREGSET,
1266 &ppc32_linux_dscrregset);
1267 if (tdep->ppc_tar_regnum != -1)
1268 fetch_regset (regcache, tid, NT_PPC_TAR,
1269 PPC_LINUX_SIZEOF_TARREGSET,
1270 &ppc32_linux_tarregset);
1271 if (tdep->have_ebb)
1272 fetch_regset (regcache, tid, NT_PPC_EBB,
1273 PPC_LINUX_SIZEOF_EBBREGSET,
1274 &ppc32_linux_ebbregset);
1275 if (tdep->ppc_mmcr0_regnum != -1)
1276 fetch_regset (regcache, tid, NT_PPC_PMU,
1277 PPC_LINUX_SIZEOF_PMUREGSET,
1278 &ppc32_linux_pmuregset);
1279 if (tdep->have_htm_spr)
1280 fetch_regset (regcache, tid, NT_PPC_TM_SPR,
1281 PPC_LINUX_SIZEOF_TM_SPRREGSET,
1282 &ppc32_linux_tm_sprregset);
1283 if (tdep->have_htm_core)
1284 {
1285 const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
1286 fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
1287 (tdep->wordsize == 4?
1288 PPC32_LINUX_SIZEOF_CGPRREGSET
1289 : PPC64_LINUX_SIZEOF_CGPRREGSET),
1290 cgprregset);
1291 }
1292 if (tdep->have_htm_fpu)
1293 fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
1294 PPC_LINUX_SIZEOF_CFPRREGSET,
1295 &ppc32_linux_cfprregset);
1296 if (tdep->have_htm_altivec)
1297 {
1298 const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
1299 fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
1300 PPC_LINUX_SIZEOF_CVMXREGSET,
1301 cvmxregset);
1302 }
1303 if (tdep->have_htm_vsx)
1304 fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
1305 PPC_LINUX_SIZEOF_CVSXREGSET,
1306 &ppc32_linux_cvsxregset);
1307 if (tdep->ppc_cppr_regnum != -1)
1308 fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
1309 PPC_LINUX_SIZEOF_CPPRREGSET,
1310 &ppc32_linux_cpprregset);
1311 if (tdep->ppc_cdscr_regnum != -1)
1312 fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
1313 PPC_LINUX_SIZEOF_CDSCRREGSET,
1314 &ppc32_linux_cdscrregset);
1315 if (tdep->ppc_ctar_regnum != -1)
1316 fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
1317 PPC_LINUX_SIZEOF_CTARREGSET,
1318 &ppc32_linux_ctarregset);
1319 }
1320
1321 /* Fetch registers from the child process. Fetch all registers if
1322 regno == -1, otherwise fetch all general registers or all floating
1323 point registers depending upon the value of regno. */
1324 void
1325 ppc_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
1326 {
1327 pid_t tid = get_ptrace_pid (regcache->ptid ());
1328
1329 if (regno == -1)
1330 fetch_ppc_registers (regcache, tid);
1331 else
1332 fetch_register (regcache, tid, regno);
1333 }
1334
1335 static void
1336 store_vsx_registers (const struct regcache *regcache, int tid, int regno)
1337 {
1338 int ret;
1339 gdb_vsxregset_t regs;
1340 const struct regset *vsxregset = ppc_linux_vsxregset ();
1341
1342 ret = ptrace (PTRACE_GETVSXREGS, tid, 0, ®s);
1343 if (ret < 0)
1344 {
1345 if (errno == EIO)
1346 {
1347 have_ptrace_getsetvsxregs = 0;
1348 return;
1349 }
1350 perror_with_name (_("Unable to fetch VSX registers"));
1351 }
1352
1353 vsxregset->collect_regset (vsxregset, regcache, regno, ®s,
1354 PPC_LINUX_SIZEOF_VSXREGSET);
1355
1356 ret = ptrace (PTRACE_SETVSXREGS, tid, 0, ®s);
1357 if (ret < 0)
1358 perror_with_name (_("Unable to store VSX registers"));
1359 }
1360
1361 static void
1362 store_altivec_registers (const struct regcache *regcache, int tid,
1363 int regno)
1364 {
1365 int ret;
1366 gdb_vrregset_t regs;
1367 struct gdbarch *gdbarch = regcache->arch ();
1368 const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
1369
1370 ret = ptrace (PTRACE_GETVRREGS, tid, 0, ®s);
1371 if (ret < 0)
1372 {
1373 if (errno == EIO)
1374 {
1375 have_ptrace_getvrregs = 0;
1376 return;
1377 }
1378 perror_with_name (_("Unable to fetch AltiVec registers"));
1379 }
1380
1381 vrregset->collect_regset (vrregset, regcache, regno, ®s,
1382 PPC_LINUX_SIZEOF_VRREGSET);
1383
1384 ret = ptrace (PTRACE_SETVRREGS, tid, 0, ®s);
1385 if (ret < 0)
1386 perror_with_name (_("Unable to store AltiVec registers"));
1387 }
1388
1389 /* Assuming TID refers to an SPE process, set the top halves of TID's
1390 general-purpose registers and its SPE-specific registers to the
1391 values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
1392 nothing.
1393
1394 All the logic to deal with whether or not the PTRACE_GETEVRREGS and
1395 PTRACE_SETEVRREGS requests are supported is isolated here, and in
1396 get_spe_registers. */
1397 static void
1398 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
1399 {
1400 if (have_ptrace_getsetevrregs)
1401 {
1402 if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
1403 return;
1404 else
1405 {
1406 /* EIO means that the PTRACE_SETEVRREGS request isn't
1407 supported; we fail silently, and don't try the call
1408 again. */
1409 if (errno == EIO)
1410 have_ptrace_getsetevrregs = 0;
1411 else
1412 /* Anything else needs to be reported. */
1413 perror_with_name (_("Unable to set SPE registers"));
1414 }
1415 }
1416 }
1417
1418 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
1419 If REGNO is -1, write the values of all the SPE-specific
1420 registers. */
1421 static void
1422 store_spe_register (const struct regcache *regcache, int tid, int regno)
1423 {
1424 struct gdbarch *gdbarch = regcache->arch ();
1425 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1426 struct gdb_evrregset_t evrregs;
1427
1428 gdb_assert (sizeof (evrregs.evr[0])
1429 == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
1430 gdb_assert (sizeof (evrregs.acc)
1431 == register_size (gdbarch, tdep->ppc_acc_regnum));
1432 gdb_assert (sizeof (evrregs.spefscr)
1433 == register_size (gdbarch, tdep->ppc_spefscr_regnum));
1434
1435 if (regno == -1)
1436 /* Since we're going to write out every register, the code below
1437 should store to every field of evrregs; if that doesn't happen,
1438 make it obvious by initializing it with suspicious values. */
1439 memset (&evrregs, 42, sizeof (evrregs));
1440 else
1441 /* We can only read and write the entire EVR register set at a
1442 time, so to write just a single register, we do a
1443 read-modify-write maneuver. */
1444 get_spe_registers (tid, &evrregs);
1445
1446 if (regno == -1)
1447 {
1448 int i;
1449
1450 for (i = 0; i < ppc_num_gprs; i++)
1451 regcache->raw_collect (tdep->ppc_ev0_upper_regnum + i,
1452 &evrregs.evr[i]);
1453 }
1454 else if (tdep->ppc_ev0_upper_regnum <= regno
1455 && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
1456 regcache->raw_collect (regno,
1457 &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
1458
1459 if (regno == -1
1460 || regno == tdep->ppc_acc_regnum)
1461 regcache->raw_collect (tdep->ppc_acc_regnum,
1462 &evrregs.acc);
1463
1464 if (regno == -1
1465 || regno == tdep->ppc_spefscr_regnum)
1466 regcache->raw_collect (tdep->ppc_spefscr_regnum,
1467 &evrregs.spefscr);
1468
1469 /* Write back the modified register set. */
1470 set_spe_registers (tid, &evrregs);
1471 }
1472
1473 static void
1474 store_register (const struct regcache *regcache, int tid, int regno)
1475 {
1476 struct gdbarch *gdbarch = regcache->arch ();
1477 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1478 /* This isn't really an address. But ptrace thinks of it as one. */
1479 CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
1480 int i;
1481 size_t bytes_to_transfer;
1482 gdb_byte buf[PPC_MAX_REGISTER_SIZE];
1483
1484 if (altivec_register_p (gdbarch, regno))
1485 {
1486 store_altivec_registers (regcache, tid, regno);
1487 return;
1488 }
1489 else if (vsx_register_p (gdbarch, regno))
1490 {
1491 store_vsx_registers (regcache, tid, regno);
1492 return;
1493 }
1494 else if (spe_register_p (gdbarch, regno))
1495 {
1496 store_spe_register (regcache, tid, regno);
1497 return;
1498 }
1499 else if (regno == PPC_DSCR_REGNUM)
1500 {
1501 gdb_assert (tdep->ppc_dscr_regnum != -1);
1502
1503 store_regset (regcache, tid, regno, NT_PPC_DSCR,
1504 PPC_LINUX_SIZEOF_DSCRREGSET,
1505 &ppc32_linux_dscrregset);
1506 return;
1507 }
1508 else if (regno == PPC_PPR_REGNUM)
1509 {
1510 gdb_assert (tdep->ppc_ppr_regnum != -1);
1511
1512 store_regset (regcache, tid, regno, NT_PPC_PPR,
1513 PPC_LINUX_SIZEOF_PPRREGSET,
1514 &ppc32_linux_pprregset);
1515 return;
1516 }
1517 else if (regno == PPC_TAR_REGNUM)
1518 {
1519 gdb_assert (tdep->ppc_tar_regnum != -1);
1520
1521 store_regset (regcache, tid, regno, NT_PPC_TAR,
1522 PPC_LINUX_SIZEOF_TARREGSET,
1523 &ppc32_linux_tarregset);
1524 return;
1525 }
1526 else if (PPC_IS_EBB_REGNUM (regno))
1527 {
1528 gdb_assert (tdep->have_ebb);
1529
1530 store_regset (regcache, tid, regno, NT_PPC_EBB,
1531 PPC_LINUX_SIZEOF_EBBREGSET,
1532 &ppc32_linux_ebbregset);
1533 return;
1534 }
1535 else if (PPC_IS_PMU_REGNUM (regno))
1536 {
1537 gdb_assert (tdep->ppc_mmcr0_regnum != -1);
1538
1539 store_regset (regcache, tid, regno, NT_PPC_PMU,
1540 PPC_LINUX_SIZEOF_PMUREGSET,
1541 &ppc32_linux_pmuregset);
1542 return;
1543 }
1544 else if (PPC_IS_TMSPR_REGNUM (regno))
1545 {
1546 gdb_assert (tdep->have_htm_spr);
1547
1548 store_regset (regcache, tid, regno, NT_PPC_TM_SPR,
1549 PPC_LINUX_SIZEOF_TM_SPRREGSET,
1550 &ppc32_linux_tm_sprregset);
1551 return;
1552 }
1553 else if (PPC_IS_CKPTGP_REGNUM (regno))
1554 {
1555 gdb_assert (tdep->have_htm_core);
1556
1557 const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
1558 store_regset (regcache, tid, regno, NT_PPC_TM_CGPR,
1559 (tdep->wordsize == 4?
1560 PPC32_LINUX_SIZEOF_CGPRREGSET
1561 : PPC64_LINUX_SIZEOF_CGPRREGSET),
1562 cgprregset);
1563 return;
1564 }
1565 else if (PPC_IS_CKPTFP_REGNUM (regno))
1566 {
1567 gdb_assert (tdep->have_htm_fpu);
1568
1569 store_regset (regcache, tid, regno, NT_PPC_TM_CFPR,
1570 PPC_LINUX_SIZEOF_CFPRREGSET,
1571 &ppc32_linux_cfprregset);
1572 return;
1573 }
1574 else if (PPC_IS_CKPTVMX_REGNUM (regno))
1575 {
1576 gdb_assert (tdep->have_htm_altivec);
1577
1578 const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
1579 store_regset (regcache, tid, regno, NT_PPC_TM_CVMX,
1580 PPC_LINUX_SIZEOF_CVMXREGSET,
1581 cvmxregset);
1582 return;
1583 }
1584 else if (PPC_IS_CKPTVSX_REGNUM (regno))
1585 {
1586 gdb_assert (tdep->have_htm_vsx);
1587
1588 store_regset (regcache, tid, regno, NT_PPC_TM_CVSX,
1589 PPC_LINUX_SIZEOF_CVSXREGSET,
1590 &ppc32_linux_cvsxregset);
1591 return;
1592 }
1593 else if (regno == PPC_CPPR_REGNUM)
1594 {
1595 gdb_assert (tdep->ppc_cppr_regnum != -1);
1596
1597 store_regset (regcache, tid, regno, NT_PPC_TM_CPPR,
1598 PPC_LINUX_SIZEOF_CPPRREGSET,
1599 &ppc32_linux_cpprregset);
1600 return;
1601 }
1602 else if (regno == PPC_CDSCR_REGNUM)
1603 {
1604 gdb_assert (tdep->ppc_cdscr_regnum != -1);
1605
1606 store_regset (regcache, tid, regno, NT_PPC_TM_CDSCR,
1607 PPC_LINUX_SIZEOF_CDSCRREGSET,
1608 &ppc32_linux_cdscrregset);
1609 return;
1610 }
1611 else if (regno == PPC_CTAR_REGNUM)
1612 {
1613 gdb_assert (tdep->ppc_ctar_regnum != -1);
1614
1615 store_regset (regcache, tid, regno, NT_PPC_TM_CTAR,
1616 PPC_LINUX_SIZEOF_CTARREGSET,
1617 &ppc32_linux_ctarregset);
1618 return;
1619 }
1620
1621 if (regaddr == -1)
1622 return;
1623
1624 /* First collect the register. Keep in mind that the regcache's
1625 idea of the register's size may not be a multiple of sizeof
1626 (long). */
1627 memset (buf, 0, sizeof buf);
1628 bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
1629 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1630 {
1631 /* Little-endian values always sit at the left end of the buffer. */
1632 regcache->raw_collect (regno, buf);
1633 }
1634 else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1635 {
1636 /* Big-endian values sit at the right end of the buffer. */
1637 size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
1638 regcache->raw_collect (regno, buf + padding);
1639 }
1640
1641 for (i = 0; i < bytes_to_transfer; i += sizeof (long))
1642 {
1643 long l;
1644
1645 memcpy (&l, &buf[i], sizeof (l));
1646 errno = 0;
1647 ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
1648 regaddr += sizeof (long);
1649
1650 if (errno == EIO
1651 && (regno == tdep->ppc_fpscr_regnum
1652 || regno == PPC_ORIG_R3_REGNUM
1653 || regno == PPC_TRAP_REGNUM))
1654 {
1655 /* Some older kernel versions don't allow fpscr, orig_r3
1656 or trap to be written. */
1657 continue;
1658 }
1659
1660 if (errno != 0)
1661 {
1662 char message[128];
1663 xsnprintf (message, sizeof (message), "writing register %s (#%d)",
1664 gdbarch_register_name (gdbarch, regno), regno);
1665 perror_with_name (message);
1666 }
1667 }
1668 }
1669
1670 /* This function actually issues the request to ptrace, telling
1671 it to store all general-purpose registers present in the specified
1672 regset.
1673
1674 If the ptrace request does not exist, this function returns 0
1675 and properly sets the have_ptrace_* flag. If the request fails,
1676 this function calls perror_with_name. Otherwise, if the request
1677 succeeds, then the regcache is stored and 1 is returned. */
1678 static int
1679 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
1680 {
1681 gdb_gregset_t gregset;
1682
1683 if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
1684 {
1685 if (errno == EIO)
1686 {
1687 have_ptrace_getsetregs = 0;
1688 return 0;
1689 }
1690 perror_with_name (_("Couldn't get general-purpose registers"));
1691 }
1692
1693 fill_gregset (regcache, &gregset, regno);
1694
1695 if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
1696 {
1697 if (errno == EIO)
1698 {
1699 have_ptrace_getsetregs = 0;
1700 return 0;
1701 }
1702 perror_with_name (_("Couldn't set general-purpose registers"));
1703 }
1704
1705 return 1;
1706 }
1707
1708 /* This is a wrapper for the store_all_gp_regs function. It is
1709 responsible for verifying if this target has the ptrace request
1710 that can be used to store all general-purpose registers at one
1711 shot. If it doesn't, then we should store them using the
1712 old-fashioned way, which is to iterate over the registers and
1713 store them one by one. */
1714 static void
1715 store_gp_regs (const struct regcache *regcache, int tid, int regno)
1716 {
1717 struct gdbarch *gdbarch = regcache->arch ();
1718 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1719 int i;
1720
1721 if (have_ptrace_getsetregs)
1722 if (store_all_gp_regs (regcache, tid, regno))
1723 return;
1724
1725 /* If we hit this point, it doesn't really matter which
1726 architecture we are using. We just need to store the
1727 registers in the "old-fashioned way". */
1728 for (i = 0; i < ppc_num_gprs; i++)
1729 store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
1730 }
1731
1732 /* This function actually issues the request to ptrace, telling
1733 it to store all floating-point registers present in the specified
1734 regset.
1735
1736 If the ptrace request does not exist, this function returns 0
1737 and properly sets the have_ptrace_* flag. If the request fails,
1738 this function calls perror_with_name. Otherwise, if the request
1739 succeeds, then the regcache is stored and 1 is returned. */
1740 static int
1741 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
1742 {
1743 gdb_fpregset_t fpregs;
1744
1745 if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
1746 {
1747 if (errno == EIO)
1748 {
1749 have_ptrace_getsetfpregs = 0;
1750 return 0;
1751 }
1752 perror_with_name (_("Couldn't get floating-point registers"));
1753 }
1754
1755 fill_fpregset (regcache, &fpregs, regno);
1756
1757 if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
1758 {
1759 if (errno == EIO)
1760 {
1761 have_ptrace_getsetfpregs = 0;
1762 return 0;
1763 }
1764 perror_with_name (_("Couldn't set floating-point registers"));
1765 }
1766
1767 return 1;
1768 }
1769
1770 /* This is a wrapper for the store_all_fp_regs function. It is
1771 responsible for verifying if this target has the ptrace request
1772 that can be used to store all floating-point registers at one
1773 shot. If it doesn't, then we should store them using the
1774 old-fashioned way, which is to iterate over the registers and
1775 store them one by one. */
1776 static void
1777 store_fp_regs (const struct regcache *regcache, int tid, int regno)
1778 {
1779 struct gdbarch *gdbarch = regcache->arch ();
1780 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1781 int i;
1782
1783 if (have_ptrace_getsetfpregs)
1784 if (store_all_fp_regs (regcache, tid, regno))
1785 return;
1786
1787 /* If we hit this point, it doesn't really matter which
1788 architecture we are using. We just need to store the
1789 registers in the "old-fashioned way". */
1790 for (i = 0; i < ppc_num_fprs; i++)
1791 store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
1792 }
1793
1794 static void
1795 store_ppc_registers (const struct regcache *regcache, int tid)
1796 {
1797 struct gdbarch *gdbarch = regcache->arch ();
1798 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1799
1800 store_gp_regs (regcache, tid, -1);
1801 if (tdep->ppc_fp0_regnum >= 0)
1802 store_fp_regs (regcache, tid, -1);
1803 store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
1804 if (tdep->ppc_ps_regnum != -1)
1805 store_register (regcache, tid, tdep->ppc_ps_regnum);
1806 if (tdep->ppc_cr_regnum != -1)
1807 store_register (regcache, tid, tdep->ppc_cr_regnum);
1808 if (tdep->ppc_lr_regnum != -1)
1809 store_register (regcache, tid, tdep->ppc_lr_regnum);
1810 if (tdep->ppc_ctr_regnum != -1)
1811 store_register (regcache, tid, tdep->ppc_ctr_regnum);
1812 if (tdep->ppc_xer_regnum != -1)
1813 store_register (regcache, tid, tdep->ppc_xer_regnum);
1814 if (tdep->ppc_mq_regnum != -1)
1815 store_register (regcache, tid, tdep->ppc_mq_regnum);
1816 if (tdep->ppc_fpscr_regnum != -1)
1817 store_register (regcache, tid, tdep->ppc_fpscr_regnum);
1818 if (ppc_linux_trap_reg_p (gdbarch))
1819 {
1820 store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
1821 store_register (regcache, tid, PPC_TRAP_REGNUM);
1822 }
1823 if (have_ptrace_getvrregs)
1824 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
1825 store_altivec_registers (regcache, tid, -1);
1826 if (have_ptrace_getsetvsxregs)
1827 if (tdep->ppc_vsr0_upper_regnum != -1)
1828 store_vsx_registers (regcache, tid, -1);
1829 if (tdep->ppc_ev0_upper_regnum >= 0)
1830 store_spe_register (regcache, tid, -1);
1831 if (tdep->ppc_ppr_regnum != -1)
1832 store_regset (regcache, tid, -1, NT_PPC_PPR,
1833 PPC_LINUX_SIZEOF_PPRREGSET,
1834 &ppc32_linux_pprregset);
1835 if (tdep->ppc_dscr_regnum != -1)
1836 store_regset (regcache, tid, -1, NT_PPC_DSCR,
1837 PPC_LINUX_SIZEOF_DSCRREGSET,
1838 &ppc32_linux_dscrregset);
1839 if (tdep->ppc_tar_regnum != -1)
1840 store_regset (regcache, tid, -1, NT_PPC_TAR,
1841 PPC_LINUX_SIZEOF_TARREGSET,
1842 &ppc32_linux_tarregset);
1843
1844 if (tdep->ppc_mmcr0_regnum != -1)
1845 store_regset (regcache, tid, -1, NT_PPC_PMU,
1846 PPC_LINUX_SIZEOF_PMUREGSET,
1847 &ppc32_linux_pmuregset);
1848
1849 if (tdep->have_htm_spr)
1850 store_regset (regcache, tid, -1, NT_PPC_TM_SPR,
1851 PPC_LINUX_SIZEOF_TM_SPRREGSET,
1852 &ppc32_linux_tm_sprregset);
1853
1854 /* Because the EBB and checkpointed HTM registers can be
1855 unavailable, attempts to store them here would cause this
1856 function to fail most of the time, so we ignore them. */
1857 }
1858
1859 void
1860 ppc_linux_nat_target::store_registers (struct regcache *regcache, int regno)
1861 {
1862 pid_t tid = get_ptrace_pid (regcache->ptid ());
1863
1864 if (regno >= 0)
1865 store_register (regcache, tid, regno);
1866 else
1867 store_ppc_registers (regcache, tid);
1868 }
1869
1870 /* Functions for transferring registers between a gregset_t or fpregset_t
1871 (see sys/ucontext.h) and gdb's regcache. The word size is that used
1872 by the ptrace interface, not the current program's ABI. Eg. if a
1873 powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
1874 read or write 64-bit gregsets. This is to suit the host libthread_db. */
1875
1876 void
1877 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
1878 {
1879 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1880
1881 ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
1882 }
1883
1884 void
1885 fill_gregset (const struct regcache *regcache,
1886 gdb_gregset_t *gregsetp, int regno)
1887 {
1888 const struct regset *regset = ppc_linux_gregset (sizeof (long));
1889
1890 if (regno == -1)
1891 memset (gregsetp, 0, sizeof (*gregsetp));
1892 ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
1893 }
1894
1895 void
1896 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
1897 {
1898 const struct regset *regset = ppc_linux_fpregset ();
1899
1900 ppc_supply_fpregset (regset, regcache, -1,
1901 fpregsetp, sizeof (*fpregsetp));
1902 }
1903
1904 void
1905 fill_fpregset (const struct regcache *regcache,
1906 gdb_fpregset_t *fpregsetp, int regno)
1907 {
1908 const struct regset *regset = ppc_linux_fpregset ();
1909
1910 ppc_collect_fpregset (regset, regcache, regno,
1911 fpregsetp, sizeof (*fpregsetp));
1912 }
1913
1914 int
1915 ppc_linux_nat_target::auxv_parse (const gdb_byte **readptr,
1916 const gdb_byte *endptr, CORE_ADDR *typep,
1917 CORE_ADDR *valp)
1918 {
1919 gdb_assert (inferior_ptid != null_ptid);
1920
1921 int tid = inferior_ptid.lwp ();
1922 if (tid == 0)
1923 tid = inferior_ptid.pid ();
1924
1925 int sizeof_auxv_field = ppc_linux_target_wordsize (tid);
1926
1927 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
1928 const gdb_byte *ptr = *readptr;
1929
1930 if (endptr == ptr)
1931 return 0;
1932
1933 if (endptr - ptr < sizeof_auxv_field * 2)
1934 return -1;
1935
1936 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
1937 ptr += sizeof_auxv_field;
1938 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
1939 ptr += sizeof_auxv_field;
1940
1941 *readptr = ptr;
1942 return 1;
1943 }
1944
1945 const struct target_desc *
1946 ppc_linux_nat_target::read_description ()
1947 {
1948 if (inferior_ptid == null_ptid)
1949 return this->beneath ()->read_description ();
1950
1951 int tid = inferior_ptid.pid ();
1952
1953 if (have_ptrace_getsetevrregs)
1954 {
1955 struct gdb_evrregset_t evrregset;
1956
1957 if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
1958 return tdesc_powerpc_e500l;
1959
1960 /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
1961 Anything else needs to be reported. */
1962 else if (errno != EIO)
1963 perror_with_name (_("Unable to fetch SPE registers"));
1964 }
1965
1966 struct ppc_linux_features features = ppc_linux_no_features;
1967
1968 features.wordsize = ppc_linux_target_wordsize (tid);
1969
1970 CORE_ADDR hwcap = linux_get_hwcap ();
1971 CORE_ADDR hwcap2 = linux_get_hwcap2 ();
1972
1973 if (have_ptrace_getsetvsxregs
1974 && (hwcap & PPC_FEATURE_HAS_VSX))
1975 {
1976 gdb_vsxregset_t vsxregset;
1977
1978 if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
1979 features.vsx = true;
1980
1981 /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
1982 Anything else needs to be reported. */
1983 else if (errno != EIO)
1984 perror_with_name (_("Unable to fetch VSX registers"));
1985 }
1986
1987 if (have_ptrace_getvrregs
1988 && (hwcap & PPC_FEATURE_HAS_ALTIVEC))
1989 {
1990 gdb_vrregset_t vrregset;
1991
1992 if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
1993 features.altivec = true;
1994
1995 /* EIO means that the PTRACE_GETVRREGS request isn't supported.
1996 Anything else needs to be reported. */
1997 else if (errno != EIO)
1998 perror_with_name (_("Unable to fetch AltiVec registers"));
1999 }
2000
2001 features.isa205 = ppc_linux_has_isa205 (hwcap);
2002
2003 if ((hwcap2 & PPC_FEATURE2_DSCR)
2004 && check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET)
2005 && check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET))
2006 {
2007 features.ppr_dscr = true;
2008 if ((hwcap2 & PPC_FEATURE2_ARCH_2_07)
2009 && (hwcap2 & PPC_FEATURE2_TAR)
2010 && (hwcap2 & PPC_FEATURE2_EBB)
2011 && check_regset (tid, NT_PPC_TAR, PPC_LINUX_SIZEOF_TARREGSET)
2012 && check_regset (tid, NT_PPC_EBB, PPC_LINUX_SIZEOF_EBBREGSET)
2013 && check_regset (tid, NT_PPC_PMU, PPC_LINUX_SIZEOF_PMUREGSET))
2014 {
2015 features.isa207 = true;
2016 if ((hwcap2 & PPC_FEATURE2_HTM)
2017 && check_regset (tid, NT_PPC_TM_SPR,
2018 PPC_LINUX_SIZEOF_TM_SPRREGSET))
2019 features.htm = true;
2020 }
2021 }
2022
2023 return ppc_linux_match_description (features);
2024 }
2025
2026 /* Routines for installing hardware watchpoints and breakpoints. When
2027 GDB requests a hardware watchpoint or breakpoint to be installed, we
2028 register the request for the pid of inferior_ptid in a map with one
2029 entry per process. We then issue a stop request to all the threads of
2030 this process, and mark a per-thread flag indicating that their debug
2031 registers should be updated. Right before they are next resumed, we
2032 remove all previously installed debug registers and install all the
2033 ones GDB requested. We then update a map with one entry per thread
2034 that keeps track of what debug registers were last installed in each
2035 thread.
2036
2037 We use this second map to remove installed registers before installing
2038 the ones requested by GDB, and to copy the debug register state after
2039 a thread clones or forks, since depending on the kernel configuration,
2040 debug registers can be inherited. */
2041
2042 /* Check if we support and have enough resources to install a hardware
2043 watchpoint or breakpoint. See the description in target.h. */
2044
2045 int
2046 ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt,
2047 int ot)
2048 {
2049 int total_hw_wp, total_hw_bp;
2050
2051 m_dreg_interface.detect (inferior_ptid);
2052
2053 if (m_dreg_interface.unavailable_p ())
2054 return 0;
2055
2056 if (m_dreg_interface.hwdebug_p ())
2057 {
2058 /* When PowerPC HWDEBUG ptrace interface is available, the number of
2059 available hardware watchpoints and breakpoints is stored at the
2060 hwdebug_info struct. */
2061 total_hw_bp = m_dreg_interface.hwdebug_info ().num_instruction_bps;
2062 total_hw_wp = m_dreg_interface.hwdebug_info ().num_data_bps;
2063 }
2064 else
2065 {
2066 gdb_assert (m_dreg_interface.debugreg_p ());
2067
2068 /* With the DEBUGREG ptrace interface, we should consider having 1
2069 hardware watchpoint and no hardware breakpoints. */
2070 total_hw_bp = 0;
2071 total_hw_wp = 1;
2072 }
2073
2074 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
2075 || type == bp_access_watchpoint || type == bp_watchpoint)
2076 {
2077 if (total_hw_wp == 0)
2078 return 0;
2079 else if (cnt + ot > total_hw_wp)
2080 return -1;
2081 else
2082 return 1;
2083 }
2084 else if (type == bp_hardware_breakpoint)
2085 {
2086 if (total_hw_bp == 0)
2087 return 0;
2088 else if (cnt > total_hw_bp)
2089 return -1;
2090 else
2091 return 1;
2092 }
2093
2094 return 0;
2095 }
2096
2097 /* Returns 1 if we can watch LEN bytes at address ADDR, 0 otherwise. */
2098
2099 int
2100 ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2101 {
2102 /* Handle sub-8-byte quantities. */
2103 if (len <= 0)
2104 return 0;
2105
2106 m_dreg_interface.detect (inferior_ptid);
2107
2108 if (m_dreg_interface.unavailable_p ())
2109 return 0;
2110
2111 /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
2112 restrictions for watchpoints in the processors. In that case, we use that
2113 information to determine the hardcoded watchable region for
2114 watchpoints. */
2115 if (m_dreg_interface.hwdebug_p ())
2116 {
2117 const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
2118 .hwdebug_info ());
2119 int region_size = hwdebug_info.data_bp_alignment;
2120 int region_align = region_size;
2121
2122 /* Embedded DAC-based processors, like the PowerPC 440 have ranged
2123 watchpoints and can watch any access within an arbitrary memory
2124 region. This is useful to watch arrays and structs, for instance. It
2125 takes two hardware watchpoints though. */
2126 if (len > 1
2127 && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
2128 && (linux_get_hwcap () & PPC_FEATURE_BOOKE))
2129 return 2;
2130 /* Check if the processor provides DAWR interface. */
2131 if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
2132 {
2133 /* DAWR interface allows to watch up to 512 byte wide ranges. */
2134 region_size = 512;
2135 /* DAWR interface allows to watch up to 512 byte wide ranges which
2136 can't cross a 512 byte boundary on machines that don't have a
2137 second DAWR (P9 or less). */
2138 if (!(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_ARCH_31))
2139 region_align = 512;
2140 }
2141 /* Server processors provide one hardware watchpoint and addr+len should
2142 fall in the watchable region provided by the ptrace interface. */
2143 if (region_align
2144 && (addr + len > (addr & ~(region_align - 1)) + region_size))
2145 return 0;
2146 }
2147 /* addr+len must fall in the 8 byte watchable region for DABR-based
2148 processors (i.e., server processors). Without the new PowerPC HWDEBUG
2149 ptrace interface, DAC-based processors (i.e., embedded processors) will
2150 use addresses aligned to 4-bytes due to the way the read/write flags are
2151 passed in the old ptrace interface. */
2152 else
2153 {
2154 gdb_assert (m_dreg_interface.debugreg_p ());
2155
2156 if (((linux_get_hwcap () & PPC_FEATURE_BOOKE)
2157 && (addr + len) > (addr & ~3) + 4)
2158 || (addr + len) > (addr & ~7) + 8)
2159 return 0;
2160 }
2161
2162 return 1;
2163 }
2164
2165 /* This function compares two ppc_hw_breakpoint structs
2166 field-by-field. */
2167
2168 bool
2169 ppc_linux_nat_target::hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
2170 const struct ppc_hw_breakpoint &b)
2171 {
2172 return (a.trigger_type == b.trigger_type
2173 && a.addr_mode == b.addr_mode
2174 && a.condition_mode == b.condition_mode
2175 && a.addr == b.addr
2176 && a.addr2 == b.addr2
2177 && a.condition_value == b.condition_value);
2178 }
2179
2180 /* Return the number of registers needed for a ranged breakpoint. */
2181
2182 int
2183 ppc_linux_nat_target::ranged_break_num_registers ()
2184 {
2185 m_dreg_interface.detect (inferior_ptid);
2186
2187 return ((m_dreg_interface.hwdebug_p ()
2188 && (m_dreg_interface.hwdebug_info ().features
2189 & PPC_DEBUG_FEATURE_INSN_BP_RANGE))?
2190 2 : -1);
2191 }
2192
2193 /* Register the hardware breakpoint described by BP_TGT, to be inserted
2194 when the threads of inferior_ptid are resumed. Returns 0 for success,
2195 or -1 if the HWDEBUG interface that we need for hardware breakpoints
2196 is not available. */
2197
2198 int
2199 ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
2200 struct bp_target_info *bp_tgt)
2201 {
2202 struct ppc_hw_breakpoint p;
2203
2204 m_dreg_interface.detect (inferior_ptid);
2205
2206 if (!m_dreg_interface.hwdebug_p ())
2207 return -1;
2208
2209 p.version = PPC_DEBUG_CURRENT_VERSION;
2210 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
2211 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2212 p.addr = (uint64_t) (bp_tgt->placed_address = bp_tgt->reqstd_address);
2213 p.condition_value = 0;
2214
2215 if (bp_tgt->length)
2216 {
2217 p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
2218
2219 /* The breakpoint will trigger if the address of the instruction is
2220 within the defined range, as follows: p.addr <= address < p.addr2. */
2221 p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
2222 }
2223 else
2224 {
2225 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
2226 p.addr2 = 0;
2227 }
2228
2229 register_hw_breakpoint (inferior_ptid.pid (), p);
2230
2231 return 0;
2232 }
2233
2234 /* Clear a registration for the hardware breakpoint given by type BP_TGT.
2235 It will be removed from the threads of inferior_ptid when they are
2236 next resumed. Returns 0 for success, or -1 if the HWDEBUG interface
2237 that we need for hardware breakpoints is not available. */
2238
2239 int
2240 ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
2241 struct bp_target_info *bp_tgt)
2242 {
2243 struct ppc_hw_breakpoint p;
2244
2245 m_dreg_interface.detect (inferior_ptid);
2246
2247 if (!m_dreg_interface.hwdebug_p ())
2248 return -1;
2249
2250 p.version = PPC_DEBUG_CURRENT_VERSION;
2251 p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
2252 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2253 p.addr = (uint64_t) bp_tgt->placed_address;
2254 p.condition_value = 0;
2255
2256 if (bp_tgt->length)
2257 {
2258 p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
2259
2260 /* The breakpoint will trigger if the address of the instruction is within
2261 the defined range, as follows: p.addr <= address < p.addr2. */
2262 p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
2263 }
2264 else
2265 {
2266 p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
2267 p.addr2 = 0;
2268 }
2269
2270 clear_hw_breakpoint (inferior_ptid.pid (), p);
2271
2272 return 0;
2273 }
2274
2275 /* Return the trigger value to set in a ppc_hw_breakpoint object for a
2276 given hardware watchpoint TYPE. We assume type is not hw_execute. */
2277
2278 int
2279 ppc_linux_nat_target::get_trigger_type (enum target_hw_bp_type type)
2280 {
2281 int t;
2282
2283 if (type == hw_read)
2284 t = PPC_BREAKPOINT_TRIGGER_READ;
2285 else if (type == hw_write)
2286 t = PPC_BREAKPOINT_TRIGGER_WRITE;
2287 else
2288 t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
2289
2290 return t;
2291 }
2292
2293 /* Register a new masked watchpoint at ADDR using the mask MASK, to be
2294 inserted when the threads of inferior_ptid are resumed. RW may be
2295 hw_read for a read watchpoint, hw_write for a write watchpoint or
2296 hw_access for an access watchpoint. */
2297
2298 int
2299 ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
2300 target_hw_bp_type rw)
2301 {
2302 struct ppc_hw_breakpoint p;
2303
2304 gdb_assert (m_dreg_interface.hwdebug_p ());
2305
2306 p.version = PPC_DEBUG_CURRENT_VERSION;
2307 p.trigger_type = get_trigger_type (rw);
2308 p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
2309 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2310 p.addr = addr;
2311 p.addr2 = mask;
2312 p.condition_value = 0;
2313
2314 register_hw_breakpoint (inferior_ptid.pid (), p);
2315
2316 return 0;
2317 }
2318
2319 /* Clear a registration for a masked watchpoint at ADDR with the mask
2320 MASK. It will be removed from the threads of inferior_ptid when they
2321 are next resumed. RW may be hw_read for a read watchpoint, hw_write
2322 for a write watchpoint or hw_access for an access watchpoint. */
2323
2324 int
2325 ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
2326 target_hw_bp_type rw)
2327 {
2328 struct ppc_hw_breakpoint p;
2329
2330 gdb_assert (m_dreg_interface.hwdebug_p ());
2331
2332 p.version = PPC_DEBUG_CURRENT_VERSION;
2333 p.trigger_type = get_trigger_type (rw);
2334 p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
2335 p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2336 p.addr = addr;
2337 p.addr2 = mask;
2338 p.condition_value = 0;
2339
2340 clear_hw_breakpoint (inferior_ptid.pid (), p);
2341
2342 return 0;
2343 }
2344
2345 /* Check whether we have at least one free DVC register for the threads
2346 of the pid of inferior_ptid. */
2347
2348 bool
2349 ppc_linux_nat_target::can_use_watchpoint_cond_accel (void)
2350 {
2351 m_dreg_interface.detect (inferior_ptid);
2352
2353 if (!m_dreg_interface.hwdebug_p ())
2354 return false;
2355
2356 int cnt = m_dreg_interface.hwdebug_info ().num_condition_regs;
2357
2358 if (cnt == 0)
2359 return false;
2360
2361 auto process_it = m_process_info.find (inferior_ptid.pid ());
2362
2363 /* No breakpoints, watchpoints, tracepoints, or catchpoints have been
2364 requested for this process, we have at least one free DVC register. */
2365 if (process_it == m_process_info.end ())
2366 return true;
2367
2368 for (const ppc_hw_breakpoint &bp : process_it->second.requested_hw_bps)
2369 if (bp.condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
2370 cnt--;
2371
2372 if (cnt <= 0)
2373 return false;
2374
2375 return true;
2376 }
2377
2378 /* Calculate the enable bits and the contents of the Data Value Compare
2379 debug register present in BookE processors.
2380
2381 ADDR is the address to be watched, LEN is the length of watched data
2382 and DATA_VALUE is the value which will trigger the watchpoint.
2383 On exit, CONDITION_MODE will hold the enable bits for the DVC, and
2384 CONDITION_VALUE will hold the value which should be put in the
2385 DVC register. */
2386
2387 void
2388 ppc_linux_nat_target::calculate_dvc (CORE_ADDR addr, int len,
2389 CORE_ADDR data_value,
2390 uint32_t *condition_mode,
2391 uint64_t *condition_value)
2392 {
2393 const struct ppc_debug_info &hwdebug_info = (m_dreg_interface.
2394 hwdebug_info ());
2395
2396 int i, num_byte_enable, align_offset, num_bytes_off_dvc,
2397 rightmost_enabled_byte;
2398 CORE_ADDR addr_end_data, addr_end_dvc;
2399
2400 /* The DVC register compares bytes within fixed-length windows which
2401 are word-aligned, with length equal to that of the DVC register.
2402 We need to calculate where our watch region is relative to that
2403 window and enable comparison of the bytes which fall within it. */
2404
2405 align_offset = addr % hwdebug_info.sizeof_condition;
2406 addr_end_data = addr + len;
2407 addr_end_dvc = (addr - align_offset
2408 + hwdebug_info.sizeof_condition);
2409 num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
2410 addr_end_data - addr_end_dvc : 0;
2411 num_byte_enable = len - num_bytes_off_dvc;
2412 /* Here, bytes are numbered from right to left. */
2413 rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
2414 addr_end_dvc - addr_end_data : 0;
2415
2416 *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
2417 for (i = 0; i < num_byte_enable; i++)
2418 *condition_mode
2419 |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
2420
2421 /* Now we need to match the position within the DVC of the comparison
2422 value with where the watch region is relative to the window
2423 (i.e., the ALIGN_OFFSET). */
2424
2425 *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
2426 << rightmost_enabled_byte * 8);
2427 }
2428
2429 /* Return the number of memory locations that need to be accessed to
2430 evaluate the expression which generated the given value chain.
2431 Returns -1 if there's any register access involved, or if there are
2432 other kinds of values which are not acceptable in a condition
2433 expression (e.g., lval_computed or lval_internalvar). */
2434
2435 int
2436 ppc_linux_nat_target::num_memory_accesses (const std::vector<value_ref_ptr>
2437 &chain)
2438 {
2439 int found_memory_cnt = 0;
2440
2441 /* The idea here is that evaluating an expression generates a series
2442 of values, one holding the value of every subexpression. (The
2443 expression a*b+c has five subexpressions: a, b, a*b, c, and
2444 a*b+c.) GDB's values hold almost enough information to establish
2445 the criteria given above --- they identify memory lvalues,
2446 register lvalues, computed values, etcetera. So we can evaluate
2447 the expression, and then scan the chain of values that leaves
2448 behind to determine the memory locations involved in the evaluation
2449 of an expression.
2450
2451 However, I don't think that the values returned by inferior
2452 function calls are special in any way. So this function may not
2453 notice that an expression contains an inferior function call.
2454 FIXME. */
2455
2456 for (const value_ref_ptr &iter : chain)
2457 {
2458 struct value *v = iter.get ();
2459
2460 /* Constants and values from the history are fine. */
2461 if (v->lval () == not_lval || !v->deprecated_modifiable ())
2462 continue;
2463 else if (v->lval () == lval_memory)
2464 {
2465 /* A lazy memory lvalue is one that GDB never needed to fetch;
2466 we either just used its address (e.g., `a' in `a.b') or
2467 we never needed it at all (e.g., `a' in `a,b'). */
2468 if (!v->lazy ())
2469 found_memory_cnt++;
2470 }
2471 /* Other kinds of values are not fine. */
2472 else
2473 return -1;
2474 }
2475
2476 return found_memory_cnt;
2477 }
2478
2479 /* Verifies whether the expression COND can be implemented using the
2480 DVC (Data Value Compare) register in BookE processors. The expression
2481 must test the watch value for equality with a constant expression.
2482 If the function returns 1, DATA_VALUE will contain the constant against
2483 which the watch value should be compared and LEN will contain the size
2484 of the constant. */
2485
2486 int
2487 ppc_linux_nat_target::check_condition (CORE_ADDR watch_addr,
2488 struct expression *cond,
2489 CORE_ADDR *data_value, int *len)
2490 {
2491 int num_accesses_left, num_accesses_right;
2492 struct value *left_val, *right_val;
2493 std::vector<value_ref_ptr> left_chain, right_chain;
2494
2495 expr::equal_operation *eqop
2496 = dynamic_cast<expr::equal_operation *> (cond->op.get ());
2497 if (eqop == nullptr)
2498 return 0;
2499 expr::operation *lhs = eqop->get_lhs ();
2500 expr::operation *rhs = eqop->get_rhs ();
2501
2502 fetch_subexp_value (cond, lhs, &left_val, NULL, &left_chain, false);
2503 num_accesses_left = num_memory_accesses (left_chain);
2504
2505 if (left_val == NULL || num_accesses_left < 0)
2506 return 0;
2507
2508 fetch_subexp_value (cond, rhs, &right_val, NULL, &right_chain, false);
2509 num_accesses_right = num_memory_accesses (right_chain);
2510
2511 if (right_val == NULL || num_accesses_right < 0)
2512 return 0;
2513
2514 if (num_accesses_left == 1 && num_accesses_right == 0
2515 && left_val->lval () == lval_memory
2516 && left_val->address () == watch_addr)
2517 {
2518 *data_value = value_as_long (right_val);
2519
2520 /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
2521 the same type as the memory region referenced by LEFT_VAL. */
2522 *len = check_typedef (left_val->type ())->length ();
2523 }
2524 else if (num_accesses_left == 0 && num_accesses_right == 1
2525 && right_val->lval () == lval_memory
2526 && right_val->address () == watch_addr)
2527 {
2528 *data_value = value_as_long (left_val);
2529
2530 /* DATA_VALUE is the constant in LEFT_VAL, but actually has
2531 the same type as the memory region referenced by RIGHT_VAL. */
2532 *len = check_typedef (right_val->type ())->length ();
2533 }
2534 else
2535 return 0;
2536
2537 return 1;
2538 }
2539
2540 /* Return true if the target is capable of using hardware to evaluate the
2541 condition expression, thus only triggering the watchpoint when it is
2542 true. */
2543
2544 bool
2545 ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr,
2546 int len, int rw,
2547 struct expression *cond)
2548 {
2549 CORE_ADDR data_value;
2550
2551 m_dreg_interface.detect (inferior_ptid);
2552
2553 return (m_dreg_interface.hwdebug_p ()
2554 && (m_dreg_interface.hwdebug_info ().num_condition_regs > 0)
2555 && check_condition (addr, cond, &data_value, &len));
2556 }
2557
2558 /* Set up P with the parameters necessary to request a watchpoint covering
2559 LEN bytes starting at ADDR and if possible with condition expression COND
2560 evaluated by hardware. INSERT tells if we are creating a request for
2561 inserting or removing the watchpoint. */
2562
2563 void
2564 ppc_linux_nat_target::create_watchpoint_request (struct ppc_hw_breakpoint *p,
2565 CORE_ADDR addr, int len,
2566 enum target_hw_bp_type type,
2567 struct expression *cond,
2568 int insert)
2569 {
2570 const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
2571 .hwdebug_info ());
2572
2573 if (len == 1
2574 || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
2575 {
2576 int use_condition;
2577 CORE_ADDR data_value;
2578
2579 use_condition = (insert? can_use_watchpoint_cond_accel ()
2580 : hwdebug_info.num_condition_regs > 0);
2581 if (cond && use_condition && check_condition (addr, cond,
2582 &data_value, &len))
2583 calculate_dvc (addr, len, data_value, &p->condition_mode,
2584 &p->condition_value);
2585 else
2586 {
2587 p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2588 p->condition_value = 0;
2589 }
2590
2591 p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
2592 p->addr2 = 0;
2593 }
2594 else
2595 {
2596 p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
2597 p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
2598 p->condition_value = 0;
2599
2600 /* The watchpoint will trigger if the address of the memory access is
2601 within the defined range, as follows: p->addr <= address < p->addr2.
2602
2603 Note that the above sentence just documents how ptrace interprets
2604 its arguments; the watchpoint is set to watch the range defined by
2605 the user _inclusively_, as specified by the user interface. */
2606 p->addr2 = (uint64_t) addr + len;
2607 }
2608
2609 p->version = PPC_DEBUG_CURRENT_VERSION;
2610 p->trigger_type = get_trigger_type (type);
2611 p->addr = (uint64_t) addr;
2612 }
2613
2614 /* Register a watchpoint, to be inserted when the threads of the group of
2615 inferior_ptid are next resumed. Returns 0 on success, and -1 if there
2616 is no ptrace interface available to install the watchpoint. */
2617
2618 int
2619 ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
2620 enum target_hw_bp_type type,
2621 struct expression *cond)
2622 {
2623 m_dreg_interface.detect (inferior_ptid);
2624
2625 if (m_dreg_interface.unavailable_p ())
2626 return -1;
2627
2628 if (m_dreg_interface.hwdebug_p ())
2629 {
2630 struct ppc_hw_breakpoint p;
2631
2632 create_watchpoint_request (&p, addr, len, type, cond, 1);
2633
2634 register_hw_breakpoint (inferior_ptid.pid (), p);
2635 }
2636 else
2637 {
2638 gdb_assert (m_dreg_interface.debugreg_p ());
2639
2640 long wp_value;
2641 long read_mode, write_mode;
2642
2643 if (linux_get_hwcap () & PPC_FEATURE_BOOKE)
2644 {
2645 /* PowerPC 440 requires only the read/write flags to be passed
2646 to the kernel. */
2647 read_mode = 1;
2648 write_mode = 2;
2649 }
2650 else
2651 {
2652 /* PowerPC 970 and other DABR-based processors are required to pass
2653 the Breakpoint Translation bit together with the flags. */
2654 read_mode = 5;
2655 write_mode = 6;
2656 }
2657
2658 wp_value = addr & ~(read_mode | write_mode);
2659 switch (type)
2660 {
2661 case hw_read:
2662 /* Set read and translate bits. */
2663 wp_value |= read_mode;
2664 break;
2665 case hw_write:
2666 /* Set write and translate bits. */
2667 wp_value |= write_mode;
2668 break;
2669 case hw_access:
2670 /* Set read, write and translate bits. */
2671 wp_value |= read_mode | write_mode;
2672 break;
2673 }
2674
2675 register_wp (inferior_ptid.pid (), wp_value);
2676 }
2677
2678 return 0;
2679 }
2680
2681 /* Clear a registration for a hardware watchpoint. It will be removed
2682 from the threads of the group of inferior_ptid when they are next
2683 resumed. */
2684
2685 int
2686 ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
2687 enum target_hw_bp_type type,
2688 struct expression *cond)
2689 {
2690 gdb_assert (!m_dreg_interface.unavailable_p ());
2691
2692 if (m_dreg_interface.hwdebug_p ())
2693 {
2694 struct ppc_hw_breakpoint p;
2695
2696 create_watchpoint_request (&p, addr, len, type, cond, 0);
2697
2698 clear_hw_breakpoint (inferior_ptid.pid (), p);
2699 }
2700 else
2701 {
2702 gdb_assert (m_dreg_interface.debugreg_p ());
2703
2704 clear_wp (inferior_ptid.pid ());
2705 }
2706
2707 return 0;
2708 }
2709
2710 /* Implement the "low_init_process" target_ops method. */
2711
2712 void
2713 ppc_linux_nat_target::low_init_process (pid_t pid)
2714 {
2715 /* Set the hardware debug register capacity. This requires the process to be
2716 ptrace-stopped, otherwise detection will fail and software watchpoints will
2717 be used instead of hardware. If we allow this to be done lazily, we
2718 cannot guarantee that it's called when the process is ptrace-stopped, so
2719 do it now. */
2720 m_dreg_interface.detect (ptid_t (pid, pid));
2721 }
2722
2723 /* Clean up the per-process info associated with PID. When using the
2724 HWDEBUG interface, we also erase the per-thread state of installed
2725 debug registers for all the threads that belong to the group of PID.
2726
2727 Usually the thread state is cleaned up by low_delete_thread. We also
2728 do it here because low_new_thread is not called for the initial LWP,
2729 so low_delete_thread won't be able to clean up this state. */
2730
2731 void
2732 ppc_linux_nat_target::low_forget_process (pid_t pid)
2733 {
2734 if ((!m_dreg_interface.detected_p ())
2735 || (m_dreg_interface.unavailable_p ()))
2736 return;
2737
2738 ptid_t pid_ptid (pid, 0, 0);
2739
2740 m_process_info.erase (pid);
2741
2742 if (m_dreg_interface.hwdebug_p ())
2743 {
2744 for (auto it = m_installed_hw_bps.begin ();
2745 it != m_installed_hw_bps.end ();)
2746 {
2747 if (it->first.matches (pid_ptid))
2748 it = m_installed_hw_bps.erase (it);
2749 else
2750 it++;
2751 }
2752 }
2753 }
2754
2755 /* Copy the per-process state associated with the pid of PARENT to the
2756 state of CHILD_PID. GDB expects that a forked process will have the
2757 same hardware breakpoints and watchpoints as the parent.
2758
2759 If we're using the HWDEBUG interface, also copy the thread debug
2760 register state for the ptid of PARENT to the state for CHILD_PID.
2761
2762 Like for clone events, we assume the kernel will copy the debug
2763 registers from the parent thread to the child. The
2764 low_prepare_to_resume function is made to work even if it doesn't.
2765
2766 We copy the thread state here and not in low_new_thread since we don't
2767 have the pid of the parent in low_new_thread. Even if we did,
2768 low_new_thread might not be called immediately after the fork event is
2769 detected. For instance, with the checkpointing system (see
2770 linux-fork.c), the thread won't be added until GDB decides to switch
2771 to a new checkpointed process. At that point, the debug register
2772 state of the parent thread is unlikely to correspond to the state it
2773 had at the point when it forked. */
2774
2775 void
2776 ppc_linux_nat_target::low_new_fork (struct lwp_info *parent,
2777 pid_t child_pid)
2778 {
2779 if ((!m_dreg_interface.detected_p ())
2780 || (m_dreg_interface.unavailable_p ()))
2781 return;
2782
2783 auto process_it = m_process_info.find (parent->ptid.pid ());
2784
2785 if (process_it != m_process_info.end ())
2786 m_process_info[child_pid] = m_process_info[parent->ptid.pid ()];
2787
2788 if (m_dreg_interface.hwdebug_p ())
2789 {
2790 ptid_t child_ptid (child_pid, child_pid, 0);
2791
2792 copy_thread_dreg_state (parent->ptid, child_ptid);
2793 }
2794 }
2795
2796 /* Copy the thread debug register state from the PARENT thread to the the
2797 state for CHILD_LWP, if we're using the HWDEBUG interface. We assume
2798 the kernel copies the debug registers from one thread to another after
2799 a clone event. The low_prepare_to_resume function is made to work
2800 even if it doesn't. */
2801
2802 void
2803 ppc_linux_nat_target::low_new_clone (struct lwp_info *parent,
2804 pid_t child_lwp)
2805 {
2806 if ((!m_dreg_interface.detected_p ())
2807 || (m_dreg_interface.unavailable_p ()))
2808 return;
2809
2810 if (m_dreg_interface.hwdebug_p ())
2811 {
2812 ptid_t child_ptid (parent->ptid.pid (), child_lwp, 0);
2813
2814 copy_thread_dreg_state (parent->ptid, child_ptid);
2815 }
2816 }
2817
2818 /* Initialize the arch-specific thread state for LP so that it contains
2819 the ptid for lp, so that we can use it in low_delete_thread. Mark the
2820 new thread LP as stale so that we update its debug registers before
2821 resuming it. This is not called for the initial thread. */
2822
2823 void
2824 ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
2825 {
2826 init_arch_lwp_info (lp);
2827
2828 mark_thread_stale (lp);
2829 }
2830
2831 /* Delete the per-thread debug register stale flag. */
2832
2833 void
2834 ppc_linux_nat_target::low_delete_thread (struct arch_lwp_info
2835 *lp_arch_info)
2836 {
2837 if (lp_arch_info != NULL)
2838 {
2839 if (m_dreg_interface.detected_p ()
2840 && m_dreg_interface.hwdebug_p ())
2841 m_installed_hw_bps.erase (lp_arch_info->lwp_ptid);
2842
2843 xfree (lp_arch_info);
2844 }
2845 }
2846
2847 /* Install or delete debug registers in thread LP so that it matches what
2848 GDB requested before it is resumed. */
2849
2850 void
2851 ppc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
2852 {
2853 if ((!m_dreg_interface.detected_p ())
2854 || (m_dreg_interface.unavailable_p ()))
2855 return;
2856
2857 /* We have to re-install or clear the debug registers if we set the
2858 stale flag.
2859
2860 In addition, some kernels configurations can disable a hardware
2861 watchpoint after it is hit. Usually, GDB will remove and re-install
2862 a hardware watchpoint when the thread stops if "breakpoint
2863 always-inserted" is off, or to single-step a watchpoint. But so
2864 that we don't rely on this behavior, if we stop due to a hardware
2865 breakpoint or watchpoint, we also refresh our debug registers. */
2866
2867 arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
2868
2869 bool stale_dregs = (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
2870 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
2871 || lp_arch_info->debug_regs_stale);
2872
2873 if (!stale_dregs)
2874 return;
2875
2876 gdb_assert (lp->ptid.lwp_p ());
2877
2878 auto process_it = m_process_info.find (lp->ptid.pid ());
2879
2880 if (m_dreg_interface.hwdebug_p ())
2881 {
2882 /* First, delete any hardware watchpoint or breakpoint installed in
2883 the inferior and update the thread state. */
2884 auto installed_it = m_installed_hw_bps.find (lp->ptid);
2885
2886 if (installed_it != m_installed_hw_bps.end ())
2887 {
2888 auto &bp_list = installed_it->second;
2889
2890 for (auto bp_it = bp_list.begin (); bp_it != bp_list.end ();)
2891 {
2892 /* We ignore ENOENT to account for various possible kernel
2893 behaviors, e.g. the kernel might or might not copy debug
2894 registers across forks and clones, and we always copy
2895 the debug register state when fork and clone events are
2896 detected. */
2897 if (ptrace (PPC_PTRACE_DELHWDEBUG, lp->ptid.lwp (), 0,
2898 bp_it->first) < 0)
2899 if (errno != ENOENT)
2900 perror_with_name (_("Error deleting hardware "
2901 "breakpoint or watchpoint"));
2902
2903 /* We erase the entries one at a time after successfully
2904 removing the corresponding slot form the thread so that
2905 if we throw an exception above in a future iteration the
2906 map remains consistent. */
2907 bp_it = bp_list.erase (bp_it);
2908 }
2909
2910 gdb_assert (bp_list.empty ());
2911 }
2912
2913 /* Now we install all the requested hardware breakpoints and
2914 watchpoints and update the thread state. */
2915
2916 if (process_it != m_process_info.end ())
2917 {
2918 auto &bp_list = m_installed_hw_bps[lp->ptid];
2919
2920 for (ppc_hw_breakpoint bp
2921 : process_it->second.requested_hw_bps)
2922 {
2923 long slot = ptrace (PPC_PTRACE_SETHWDEBUG, lp->ptid.lwp (),
2924 0, &bp);
2925
2926 if (slot < 0)
2927 perror_with_name (_("Error setting hardware "
2928 "breakpoint or watchpoint"));
2929
2930 /* Keep track of which slots we installed in this
2931 thread. */
2932 bp_list.emplace (bp_list.begin (), slot, bp);
2933 }
2934 }
2935 }
2936 else
2937 {
2938 gdb_assert (m_dreg_interface.debugreg_p ());
2939
2940 /* Passing 0 to PTRACE_SET_DEBUGREG will clear the watchpoint. We
2941 always clear the watchpoint instead of just overwriting it, in
2942 case there is a request for a new watchpoint, because on some
2943 older kernel versions and configurations simply overwriting the
2944 watchpoint after it was hit would not re-enable it. */
2945 if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, 0) < 0)
2946 perror_with_name (_("Error clearing hardware watchpoint"));
2947
2948 /* GDB requested a watchpoint to be installed. */
2949 if (process_it != m_process_info.end ()
2950 && process_it->second.requested_wp_val.has_value ())
2951 {
2952 long wp = *(process_it->second.requested_wp_val);
2953
2954 if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, wp) < 0)
2955 perror_with_name (_("Error setting hardware watchpoint"));
2956 }
2957 }
2958
2959 lp_arch_info->debug_regs_stale = false;
2960 }
2961
2962 /* Return true if INFERIOR_PTID is known to have been stopped by a
2963 hardware watchpoint, false otherwise. If true is returned, write the
2964 address that the kernel reported as causing the SIGTRAP in ADDR_P. */
2965
2966 bool
2967 ppc_linux_nat_target::low_stopped_data_address (CORE_ADDR *addr_p)
2968 {
2969 siginfo_t siginfo;
2970
2971 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
2972 return false;
2973
2974 if (siginfo.si_signo != SIGTRAP
2975 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
2976 return false;
2977
2978 gdb_assert (!m_dreg_interface.unavailable_p ());
2979
2980 /* Check if this signal corresponds to a hardware breakpoint. We only
2981 need to check this if we're using the HWDEBUG interface, since the
2982 DEBUGREG interface only allows setting one hardware watchpoint. */
2983 if (m_dreg_interface.hwdebug_p ())
2984 {
2985 /* The index (or slot) of the *point is passed in the si_errno
2986 field. Currently, this is only the case if the kernel was
2987 configured with CONFIG_PPC_ADV_DEBUG_REGS. If not, we assume
2988 the kernel will set si_errno to a value that doesn't correspond
2989 to any real slot. */
2990 int slot = siginfo.si_errno;
2991
2992 auto installed_it = m_installed_hw_bps.find (inferior_ptid);
2993
2994 /* We must have installed slots for the thread if it got a
2995 TRAP_HWBKPT signal. */
2996 gdb_assert (installed_it != m_installed_hw_bps.end ());
2997
2998 for (const auto & slot_bp_pair : installed_it->second)
2999 if (slot_bp_pair.first == slot
3000 && (slot_bp_pair.second.trigger_type
3001 == PPC_BREAKPOINT_TRIGGER_EXECUTE))
3002 return false;
3003 }
3004
3005 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
3006 return true;
3007 }
3008
3009 /* Return true if INFERIOR_PTID is known to have been stopped by a
3010 hardware watchpoint, false otherwise. */
3011
3012 bool
3013 ppc_linux_nat_target::low_stopped_by_watchpoint ()
3014 {
3015 CORE_ADDR addr;
3016 return low_stopped_data_address (&addr);
3017 }
3018
3019 bool
3020 ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
3021 CORE_ADDR start,
3022 int length)
3023 {
3024 gdb_assert (!m_dreg_interface.unavailable_p ());
3025
3026 int mask;
3027
3028 if (m_dreg_interface.hwdebug_p ()
3029 && (linux_get_hwcap () & PPC_FEATURE_BOOKE))
3030 return start <= addr && start + length >= addr;
3031 else if (linux_get_hwcap () & PPC_FEATURE_BOOKE)
3032 mask = 3;
3033 else
3034 mask = 7;
3035
3036 addr &= ~mask;
3037
3038 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
3039 return start <= addr + mask && start + length - 1 >= addr;
3040 }
3041
3042 /* Return the number of registers needed for a masked hardware watchpoint. */
3043
3044 int
3045 ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr,
3046 CORE_ADDR mask)
3047 {
3048 m_dreg_interface.detect (inferior_ptid);
3049
3050 if (!m_dreg_interface.hwdebug_p ()
3051 || (m_dreg_interface.hwdebug_info ().features
3052 & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
3053 return -1;
3054 else if ((mask & 0xC0000000) != 0xC0000000)
3055 {
3056 warning (_("The given mask covers kernel address space "
3057 "and cannot be used.\n"));
3058
3059 return -2;
3060 }
3061 else
3062 return 2;
3063 }
3064
3065 /* Copy the per-thread debug register state, if any, from thread
3066 PARENT_PTID to thread CHILD_PTID, if the debug register being used is
3067 HWDEBUG. */
3068
3069 void
3070 ppc_linux_nat_target::copy_thread_dreg_state (const ptid_t &parent_ptid,
3071 const ptid_t &child_ptid)
3072 {
3073 gdb_assert (m_dreg_interface.hwdebug_p ());
3074
3075 auto installed_it = m_installed_hw_bps.find (parent_ptid);
3076
3077 if (installed_it != m_installed_hw_bps.end ())
3078 m_installed_hw_bps[child_ptid] = m_installed_hw_bps[parent_ptid];
3079 }
3080
3081 /* Mark the debug register stale flag for the new thread, if we have
3082 already detected which debug register interface we use. */
3083
3084 void
3085 ppc_linux_nat_target::mark_thread_stale (struct lwp_info *lp)
3086 {
3087 if ((!m_dreg_interface.detected_p ())
3088 || (m_dreg_interface.unavailable_p ()))
3089 return;
3090
3091 arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
3092
3093 lp_arch_info->debug_regs_stale = true;
3094 }
3095
3096 /* Mark all the threads of the group of PID as stale with respect to
3097 debug registers and issue a stop request to each such thread that
3098 isn't already stopped. */
3099
3100 void
3101 ppc_linux_nat_target::mark_debug_registers_changed (pid_t pid)
3102 {
3103 /* We do this in two passes to make sure all threads are marked even if
3104 we get an exception when stopping one of them. */
3105
3106 iterate_over_lwps (ptid_t (pid),
3107 [this] (struct lwp_info *lp) -> int {
3108 this->mark_thread_stale (lp);
3109 return 0;
3110 });
3111
3112 iterate_over_lwps (ptid_t (pid),
3113 [] (struct lwp_info *lp) -> int {
3114 if (!lwp_is_stopped (lp))
3115 linux_stop_lwp (lp);
3116 return 0;
3117 });
3118 }
3119
3120 /* Register a hardware breakpoint or watchpoint BP for the pid PID, then
3121 mark the stale flag for all threads of the group of PID, and issue a
3122 stop request for them. The breakpoint or watchpoint will be installed
3123 the next time each thread is resumed. Should only be used if the
3124 debug register interface is HWDEBUG. */
3125
3126 void
3127 ppc_linux_nat_target::register_hw_breakpoint (pid_t pid,
3128 const struct
3129 ppc_hw_breakpoint &bp)
3130 {
3131 gdb_assert (m_dreg_interface.hwdebug_p ());
3132
3133 m_process_info[pid].requested_hw_bps.push_back (bp);
3134
3135 mark_debug_registers_changed (pid);
3136 }
3137
3138 /* Clear a registration for a hardware breakpoint or watchpoint BP for
3139 the pid PID, then mark the stale flag for all threads of the group of
3140 PID, and issue a stop request for them. The breakpoint or watchpoint
3141 will be removed the next time each thread is resumed. Should only be
3142 used if the debug register interface is HWDEBUG. */
3143
3144 void
3145 ppc_linux_nat_target::clear_hw_breakpoint (pid_t pid,
3146 const struct ppc_hw_breakpoint &bp)
3147 {
3148 gdb_assert (m_dreg_interface.hwdebug_p ());
3149
3150 auto process_it = m_process_info.find (pid);
3151
3152 gdb_assert (process_it != m_process_info.end ());
3153
3154 auto bp_it = std::find_if (process_it->second.requested_hw_bps.begin (),
3155 process_it->second.requested_hw_bps.end (),
3156 [&bp, this]
3157 (const struct ppc_hw_breakpoint &curr)
3158 { return hwdebug_point_cmp (bp, curr); }
3159 );
3160
3161 /* If GDB is removing a watchpoint, it must have been inserted. */
3162 gdb_assert (bp_it != process_it->second.requested_hw_bps.end ());
3163
3164 process_it->second.requested_hw_bps.erase (bp_it);
3165
3166 mark_debug_registers_changed (pid);
3167 }
3168
3169 /* Register the hardware watchpoint value WP_VALUE for the pid PID,
3170 then mark the stale flag for all threads of the group of PID, and
3171 issue a stop request for them. The breakpoint or watchpoint will be
3172 installed the next time each thread is resumed. Should only be used
3173 if the debug register interface is DEBUGREG. */
3174
3175 void
3176 ppc_linux_nat_target::register_wp (pid_t pid, long wp_value)
3177 {
3178 gdb_assert (m_dreg_interface.debugreg_p ());
3179
3180 /* Our other functions should have told GDB that we only have one
3181 hardware watchpoint with this interface. */
3182 gdb_assert (!m_process_info[pid].requested_wp_val.has_value ());
3183
3184 m_process_info[pid].requested_wp_val.emplace (wp_value);
3185
3186 mark_debug_registers_changed (pid);
3187 }
3188
3189 /* Clear the hardware watchpoint registration for the pid PID, then mark
3190 the stale flag for all threads of the group of PID, and issue a stop
3191 request for them. The breakpoint or watchpoint will be installed the
3192 next time each thread is resumed. Should only be used if the debug
3193 register interface is DEBUGREG. */
3194
3195 void
3196 ppc_linux_nat_target::clear_wp (pid_t pid)
3197 {
3198 gdb_assert (m_dreg_interface.debugreg_p ());
3199
3200 auto process_it = m_process_info.find (pid);
3201
3202 gdb_assert (process_it != m_process_info.end ());
3203 gdb_assert (process_it->second.requested_wp_val.has_value ());
3204
3205 process_it->second.requested_wp_val.reset ();
3206
3207 mark_debug_registers_changed (pid);
3208 }
3209
3210 /* Initialize the arch-specific thread state for LWP, if it not already
3211 created. */
3212
3213 void
3214 ppc_linux_nat_target::init_arch_lwp_info (struct lwp_info *lp)
3215 {
3216 if (lwp_arch_private_info (lp) == NULL)
3217 {
3218 lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
3219 lwp_arch_private_info (lp)->debug_regs_stale = false;
3220 lwp_arch_private_info (lp)->lwp_ptid = lp->ptid;
3221 }
3222 }
3223
3224 /* Get the arch-specific thread state for LWP, creating it if
3225 necessary. */
3226
3227 arch_lwp_info *
3228 ppc_linux_nat_target::get_arch_lwp_info (struct lwp_info *lp)
3229 {
3230 init_arch_lwp_info (lp);
3231
3232 return lwp_arch_private_info (lp);
3233 }
3234
3235 void _initialize_ppc_linux_nat ();
3236 void
3237 _initialize_ppc_linux_nat ()
3238 {
3239 linux_target = &the_ppc_linux_nat_target;
3240
3241 /* Register the target. */
3242 add_inf_child_target (linux_target);
3243 }
3244