1 /* PPC GNU/Linux native support.
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "extract-store-integer.h"
23 #include "gdbthread.h"
28 #include "linux-nat.h"
29 #include <sys/types.h>
32 #include <sys/ioctl.h>
34 #include "gdbsupport/gdb_wait.h"
36 #include <sys/procfs.h>
37 #include "nat/gdb_ptrace.h"
38 #include "nat/linux-ptrace.h"
39 #include "inf-ptrace.h"
41 #include <unordered_map>
44 /* Prototypes for supply_gregset etc. */
47 #include "ppc-linux-tdep.h"
49 /* Required when using the AUXV. */
50 #include "elf/common.h"
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"
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
64 #ifndef PTRACE_SET_DEBUGREG
65 #define PTRACE_SET_DEBUGREG 26
67 #ifndef PTRACE_GETSIGINFO
68 #define PTRACE_GETSIGINFO 0x4202
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
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
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. */
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
101 struct ppc_hw_breakpoint
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 */
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
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
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 */
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 */
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 */
147 /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
149 #define PPC_DEBUG_CURRENT_VERSION 1
151 /* Similarly for the general-purpose (gp0 -- gp31)
152 and floating-point registers (fp0 -- fp31). */
153 #ifndef PTRACE_GETREGS
154 #define PTRACE_GETREGS 12
156 #ifndef PTRACE_SETREGS
157 #define PTRACE_SETREGS 13
159 #ifndef PTRACE_GETFPREGS
160 #define PTRACE_GETFPREGS 14
162 #ifndef PTRACE_SETFPREGS
163 #define PTRACE_SETFPREGS 15
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.
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.
179 The layout is like this (where x is the actual value of the vscr reg): */
183 |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
184 <-------> <-------><-------><->
187 |.|.|.|.|.....|.|.|.|.||X|.|.|.||.|
188 <-------> <-------><-------><->
192 typedef char gdb_vrregset_t
[PPC_LINUX_SIZEOF_VRREGSET
];
194 /* This is the layout of the POWER7 VSX registers and the way they overlap
195 with the existing FPR and VMX registers.
197 VSR doubleword 0 VSR doubleword 1
198 ----------------------------------------------------------------
200 ----------------------------------------------------------------
202 ----------------------------------------------------------------
205 ----------------------------------------------------------------
206 VSR[30] | FPR[30] | |
207 ----------------------------------------------------------------
208 VSR[31] | FPR[31] | |
209 ----------------------------------------------------------------
211 ----------------------------------------------------------------
213 ----------------------------------------------------------------
216 ----------------------------------------------------------------
218 ----------------------------------------------------------------
220 ----------------------------------------------------------------
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
226 typedef char gdb_vsxregset_t
[PPC_LINUX_SIZEOF_VSXREGSET
];
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.
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.
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
247 unsigned long evr
[32];
248 unsigned long long acc
;
249 unsigned long spefscr
;
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;
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
263 int have_ptrace_getvrregs
= 1;
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
269 int have_ptrace_getsetevrregs
= 1;
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;
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;
283 /* Private arch info associated with each thread lwp_info object, used
284 for debug register handling. */
288 /* When true, indicates that the debug registers installed in the
289 thread no longer correspond to the watchpoints and breakpoints
291 bool debug_regs_stale
;
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. */
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.
303 The interface is only detected once, testing the ptrace calls. The
304 result can indicate that no interface is available.
306 The Linux kernel provides two different sets of ptrace requests to
307 handle hardware watchpoints and breakpoints for Power:
309 - PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
310 PPC_PTRACE_DELHWDEBUG.
314 - PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
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. */
323 class ppc_linux_dreg_interface
327 ppc_linux_dreg_interface ()
328 : m_interface (), m_hwdebug_info ()
332 DISABLE_COPY_AND_ASSIGN (ppc_linux_dreg_interface
);
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. */
340 gdb_assert (detected_p ());
341 return *m_interface
== HWDEBUG
;
346 gdb_assert (detected_p ());
347 return *m_interface
== DEBUGREG
;
350 bool unavailable_p ()
352 gdb_assert (detected_p ());
353 return *m_interface
== UNAVAILABLE
;
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 ()
360 gdb_assert (hwdebug_p ());
362 return m_hwdebug_info
;
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. */
370 return m_interface
.has_value ();
373 /* Detect the available interface, if any, if it hasn't been detected
374 before, using PTID for the necessary ptrace calls. */
376 void detect (const ptid_t
&ptid
)
378 if (m_interface
.has_value ())
381 gdb_assert (ptid
.lwp_p ());
383 bool no_features
= false;
385 if (ptrace (PPC_PTRACE_GETHWDBGINFO
, ptid
.lwp (), 0, &m_hwdebug_info
)
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
397 if (m_hwdebug_info
.features
!= 0)
399 m_interface
.emplace (HWDEBUG
);
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.
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
417 Other errors, such as ENODEV, could be more permanent and cause
418 a failure for any thread.
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. */
428 if (no_features
|| errno
== EIO
)
432 if (ptrace (PTRACE_GET_DEBUGREG
, ptid
.lwp (), 0, &wp
) >= 0)
434 m_interface
.emplace (DEBUGREG
);
440 warning (_("Error when detecting the debug register interface. "
441 "Debug registers will be unavailable."));
443 m_interface
.emplace (UNAVAILABLE
);
449 /* HWDEBUG represents the set of calls PPC_PTRACE_GETHWDBGINFO,
450 PPC_PTRACE_SETHWDEBUG and PPC_PTRACE_DELHWDEBUG.
452 DEBUGREG represents the set of calls PTRACE_SET_DEBUGREG and
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. */
459 enum debug_reg_interface
466 /* The interface option. Initialized if has_value () returns true. */
467 std::optional
<enum debug_reg_interface
> m_interface
;
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
;
474 /* Per-process information. This includes the hardware watchpoints and
475 breakpoints that GDB requested to this target. */
477 struct ppc_linux_process_info
479 /* The list of hardware watchpoints and breakpoints that GDB requested
482 Only used when the interface is HWDEBUG. */
483 std::list
<struct ppc_hw_breakpoint
> requested_hw_bps
;
485 /* The watchpoint value that GDB requested for this process.
487 Only used when the interface is DEBUGREG. */
488 std::optional
<long> requested_wp_val
;
491 struct ppc_linux_nat_target final
: public linux_nat_target
493 /* Add our register access methods. */
494 void fetch_registers (struct regcache
*, int) override
;
495 void store_registers (struct regcache
*, int) override
;
497 /* Add our breakpoint/watchpoint methods. */
498 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
500 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*)
503 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*)
506 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
508 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
509 struct expression
*) override
;
511 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
512 struct expression
*) override
;
514 int insert_mask_watchpoint (CORE_ADDR
, CORE_ADDR
, enum target_hw_bp_type
)
517 int remove_mask_watchpoint (CORE_ADDR
, CORE_ADDR
, enum target_hw_bp_type
)
520 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
522 bool can_accel_watchpoint_condition (CORE_ADDR
, int, int, struct expression
*)
525 int masked_watch_num_registers (CORE_ADDR
, CORE_ADDR
) override
;
527 int ranged_break_num_registers () override
;
529 const struct target_desc
*read_description () override
;
531 int auxv_parse (const gdb_byte
**readptr
,
532 const gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
535 /* Override linux_nat_target low methods. */
536 bool low_stopped_by_watchpoint () override
;
538 bool low_stopped_data_address (CORE_ADDR
*) override
;
540 void low_new_thread (struct lwp_info
*lp
) override
;
542 void low_delete_thread (arch_lwp_info
*) override
;
544 void low_new_fork (struct lwp_info
*, pid_t
) override
;
546 void low_new_clone (struct lwp_info
*, pid_t
) override
;
548 void low_init_process (pid_t pid
) override
;
550 void low_forget_process (pid_t pid
) override
;
552 void low_prepare_to_resume (struct lwp_info
*) override
;
556 void copy_thread_dreg_state (const ptid_t
&parent_ptid
,
557 const ptid_t
&child_ptid
);
559 void mark_thread_stale (struct lwp_info
*lp
);
561 void mark_debug_registers_changed (pid_t pid
);
563 void register_hw_breakpoint (pid_t pid
,
564 const struct ppc_hw_breakpoint
&bp
);
566 void clear_hw_breakpoint (pid_t pid
,
567 const struct ppc_hw_breakpoint
&a
);
569 void register_wp (pid_t pid
, long wp_value
);
571 void clear_wp (pid_t pid
);
573 bool can_use_watchpoint_cond_accel (void);
575 void calculate_dvc (CORE_ADDR addr
, int len
,
576 CORE_ADDR data_value
,
577 uint32_t *condition_mode
,
578 uint64_t *condition_value
);
580 int check_condition (CORE_ADDR watch_addr
,
581 struct expression
*cond
,
582 CORE_ADDR
*data_value
, int *len
);
584 int num_memory_accesses (const std::vector
<value_ref_ptr
> &chain
);
586 int get_trigger_type (enum target_hw_bp_type type
);
588 void create_watchpoint_request (struct ppc_hw_breakpoint
*p
,
591 enum target_hw_bp_type type
,
592 struct expression
*cond
,
595 bool hwdebug_point_cmp (const struct ppc_hw_breakpoint
&a
,
596 const struct ppc_hw_breakpoint
&b
);
598 void init_arch_lwp_info (struct lwp_info
*lp
);
600 arch_lwp_info
*get_arch_lwp_info (struct lwp_info
*lp
);
602 /* The ptrace interface we'll use to install hardware watchpoints and
603 breakpoints (debug registers). */
604 ppc_linux_dreg_interface m_dreg_interface
;
606 /* A map from pids to structs containing info specific to each
608 std::unordered_map
<pid_t
, ppc_linux_process_info
> m_process_info
;
610 /* Callable object to hash ptids by their lwp number. */
613 std::size_t operator() (const ptid_t
&ptid
) const
615 return std::hash
<long>{} (ptid
.lwp ());
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.
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
;
629 static ppc_linux_nat_target the_ppc_linux_nat_target
;
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 */
647 ppc_register_u_addr (struct gdbarch
*gdbarch
, int regno
)
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);
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
);
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);
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
;
680 if (regno
== tdep
->ppc_mq_regnum
)
681 u_addr
= PT_MQ
* wordsize
;
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
)
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
;
708 u_addr
= PT_FPSCR
* wordsize
;
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. */
717 fetch_vsx_registers (struct regcache
*regcache
, int tid
, int regno
)
720 gdb_vsxregset_t regs
;
721 const struct regset
*vsxregset
= ppc_linux_vsxregset ();
723 ret
= ptrace (PTRACE_GETVSXREGS
, tid
, 0, ®s
);
728 have_ptrace_getsetvsxregs
= 0;
731 perror_with_name (_("Unable to fetch VSX registers"));
734 vsxregset
->supply_regset (vsxregset
, regcache
, regno
, ®s
,
735 PPC_LINUX_SIZEOF_VSXREGSET
);
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. */
742 fetch_altivec_registers (struct regcache
*regcache
, int tid
,
747 struct gdbarch
*gdbarch
= regcache
->arch ();
748 const struct regset
*vrregset
= ppc_linux_vrregset (gdbarch
);
750 ret
= ptrace (PTRACE_GETVRREGS
, tid
, 0, ®s
);
755 have_ptrace_getvrregs
= 0;
758 perror_with_name (_("Unable to fetch AltiVec registers"));
761 vrregset
->supply_regset (vrregset
, regcache
, regno
, ®s
,
762 PPC_LINUX_SIZEOF_VRREGSET
);
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
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. */
774 get_spe_registers (int tid
, struct gdb_evrregset_t
*evrregset
)
776 if (have_ptrace_getsetevrregs
)
778 if (ptrace (PTRACE_GETEVRREGS
, tid
, 0, evrregset
) >= 0)
782 /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
783 we just return zeros. */
785 have_ptrace_getsetevrregs
= 0;
787 /* Anything else needs to be reported. */
788 perror_with_name (_("Unable to fetch SPE registers"));
792 memset (evrregset
, 0, sizeof (*evrregset
));
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. */
800 fetch_spe_register (struct regcache
*regcache
, int tid
, int regno
)
802 struct gdbarch
*gdbarch
= regcache
->arch ();
803 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
804 struct gdb_evrregset_t evrregs
;
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
));
813 get_spe_registers (tid
, &evrregs
);
819 for (i
= 0; i
< ppc_num_gprs
; i
++)
820 regcache
->raw_supply (tdep
->ppc_ev0_upper_regnum
+ i
, &evrregs
.evr
[i
]);
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
]);
828 || regno
== tdep
->ppc_acc_regnum
)
829 regcache
->raw_supply (tdep
->ppc_acc_regnum
, &evrregs
.acc
);
832 || regno
== tdep
->ppc_spefscr_regnum
)
833 regcache
->raw_supply (tdep
->ppc_spefscr_regnum
, &evrregs
.spefscr
);
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. */
843 fetch_regset (struct regcache
*regcache
, int tid
,
844 int regset_id
, int regsetsize
, const struct regset
*regset
)
846 void *buf
= alloca (regsetsize
);
850 iov
.iov_len
= regsetsize
;
852 if (ptrace (PTRACE_GETREGSET
, tid
, regset_id
, &iov
) < 0)
854 if (errno
== ENODATA
)
855 regset
->supply_regset (regset
, regcache
, -1, NULL
, regsetsize
);
857 perror_with_name (_("Couldn't get register set"));
860 regset
->supply_regset (regset
, regcache
, -1, buf
, regsetsize
);
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. */
869 store_regset (const struct regcache
*regcache
, int tid
, int regnum
,
870 int regset_id
, int regsetsize
, const struct regset
*regset
)
872 void *buf
= alloca (regsetsize
);
876 iov
.iov_len
= regsetsize
;
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"));
883 regset
->collect_regset (regset
, regcache
, regnum
, buf
, regsetsize
);
885 if (ptrace (PTRACE_SETREGSET
, tid
, regset_id
, &iov
) < 0)
886 perror_with_name (_("Couldn't set register set"));
889 /* Check whether the kernel provides a register set with number
890 REGSET_ID of size REGSETSIZE for process/thread TID. */
893 check_regset (int tid
, int regset_id
, int regsetsize
)
895 void *buf
= alloca (regsetsize
);
899 iov
.iov_len
= regsetsize
;
901 if (ptrace (PTRACE_GETREGSET
, tid
, regset_id
, &iov
) >= 0
909 fetch_register (struct regcache
*regcache
, int tid
, int regno
)
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
];
918 if (altivec_register_p (gdbarch
, regno
))
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
924 if (have_ptrace_getvrregs
)
926 fetch_altivec_registers (regcache
, tid
, regno
);
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. */
933 else if (vsx_register_p (gdbarch
, regno
))
935 if (have_ptrace_getsetvsxregs
)
937 fetch_vsx_registers (regcache
, tid
, regno
);
941 else if (spe_register_p (gdbarch
, regno
))
943 fetch_spe_register (regcache
, tid
, regno
);
946 else if (regno
== PPC_DSCR_REGNUM
)
948 gdb_assert (tdep
->ppc_dscr_regnum
!= -1);
950 fetch_regset (regcache
, tid
, NT_PPC_DSCR
,
951 PPC_LINUX_SIZEOF_DSCRREGSET
,
952 &ppc32_linux_dscrregset
);
955 else if (regno
== PPC_PPR_REGNUM
)
957 gdb_assert (tdep
->ppc_ppr_regnum
!= -1);
959 fetch_regset (regcache
, tid
, NT_PPC_PPR
,
960 PPC_LINUX_SIZEOF_PPRREGSET
,
961 &ppc32_linux_pprregset
);
964 else if (regno
== PPC_TAR_REGNUM
)
966 gdb_assert (tdep
->ppc_tar_regnum
!= -1);
968 fetch_regset (regcache
, tid
, NT_PPC_TAR
,
969 PPC_LINUX_SIZEOF_TARREGSET
,
970 &ppc32_linux_tarregset
);
973 else if (PPC_IS_EBB_REGNUM (regno
))
975 gdb_assert (tdep
->have_ebb
);
977 fetch_regset (regcache
, tid
, NT_PPC_EBB
,
978 PPC_LINUX_SIZEOF_EBBREGSET
,
979 &ppc32_linux_ebbregset
);
982 else if (PPC_IS_PMU_REGNUM (regno
))
984 gdb_assert (tdep
->ppc_mmcr0_regnum
!= -1);
986 fetch_regset (regcache
, tid
, NT_PPC_PMU
,
987 PPC_LINUX_SIZEOF_PMUREGSET
,
988 &ppc32_linux_pmuregset
);
991 else if (PPC_IS_TMSPR_REGNUM (regno
))
993 gdb_assert (tdep
->have_htm_spr
);
995 fetch_regset (regcache
, tid
, NT_PPC_TM_SPR
,
996 PPC_LINUX_SIZEOF_TM_SPRREGSET
,
997 &ppc32_linux_tm_sprregset
);
1000 else if (PPC_IS_CKPTGP_REGNUM (regno
))
1002 gdb_assert (tdep
->have_htm_core
);
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
),
1012 else if (PPC_IS_CKPTFP_REGNUM (regno
))
1014 gdb_assert (tdep
->have_htm_fpu
);
1016 fetch_regset (regcache
, tid
, NT_PPC_TM_CFPR
,
1017 PPC_LINUX_SIZEOF_CFPRREGSET
,
1018 &ppc32_linux_cfprregset
);
1021 else if (PPC_IS_CKPTVMX_REGNUM (regno
))
1023 gdb_assert (tdep
->have_htm_altivec
);
1025 const struct regset
*cvmxregset
= ppc_linux_cvmxregset (gdbarch
);
1026 fetch_regset (regcache
, tid
, NT_PPC_TM_CVMX
,
1027 PPC_LINUX_SIZEOF_CVMXREGSET
,
1031 else if (PPC_IS_CKPTVSX_REGNUM (regno
))
1033 gdb_assert (tdep
->have_htm_vsx
);
1035 fetch_regset (regcache
, tid
, NT_PPC_TM_CVSX
,
1036 PPC_LINUX_SIZEOF_CVSXREGSET
,
1037 &ppc32_linux_cvsxregset
);
1040 else if (regno
== PPC_CPPR_REGNUM
)
1042 gdb_assert (tdep
->ppc_cppr_regnum
!= -1);
1044 fetch_regset (regcache
, tid
, NT_PPC_TM_CPPR
,
1045 PPC_LINUX_SIZEOF_CPPRREGSET
,
1046 &ppc32_linux_cpprregset
);
1049 else if (regno
== PPC_CDSCR_REGNUM
)
1051 gdb_assert (tdep
->ppc_cdscr_regnum
!= -1);
1053 fetch_regset (regcache
, tid
, NT_PPC_TM_CDSCR
,
1054 PPC_LINUX_SIZEOF_CDSCRREGSET
,
1055 &ppc32_linux_cdscrregset
);
1058 else if (regno
== PPC_CTAR_REGNUM
)
1060 gdb_assert (tdep
->ppc_ctar_regnum
!= -1);
1062 fetch_regset (regcache
, tid
, NT_PPC_TM_CTAR
,
1063 PPC_LINUX_SIZEOF_CTARREGSET
,
1064 &ppc32_linux_ctarregset
);
1070 memset (buf
, '\0', register_size (gdbarch
, regno
)); /* Supply zeroes */
1071 regcache
->raw_supply (regno
, buf
);
1075 /* Read the raw register using sizeof(long) sized chunks. On a
1076 32-bit platform, 64-bit floating-point registers will require two
1078 for (bytes_transferred
= 0;
1079 bytes_transferred
< register_size (gdbarch
, regno
);
1080 bytes_transferred
+= sizeof (long))
1085 l
= ptrace (PTRACE_PEEKUSER
, tid
, (PTRACE_TYPE_ARG3
) regaddr
, 0);
1086 regaddr
+= sizeof (long);
1090 xsnprintf (message
, sizeof (message
), "reading register %s (#%d)",
1091 gdbarch_register_name (gdbarch
, regno
), regno
);
1092 perror_with_name (message
);
1094 memcpy (&buf
[bytes_transferred
], &l
, sizeof (l
));
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
1100 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_LITTLE
)
1102 /* Little-endian values are always found at the left end of the
1103 bytes transferred. */
1104 regcache
->raw_supply (regno
, buf
);
1106 else if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1108 /* Big-endian values are found at the right end of the bytes
1110 size_t padding
= (bytes_transferred
- register_size (gdbarch
, regno
));
1111 regcache
->raw_supply (regno
, buf
+ padding
);
1114 internal_error (_("fetch_register: unexpected byte order: %d"),
1115 gdbarch_byte_order (gdbarch
));
1118 /* This function actually issues the request to ptrace, telling
1119 it to get all general-purpose registers and put them into the
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. */
1127 fetch_all_gp_regs (struct regcache
*regcache
, int tid
)
1129 gdb_gregset_t gregset
;
1131 if (ptrace (PTRACE_GETREGS
, tid
, 0, (void *) &gregset
) < 0)
1135 have_ptrace_getsetregs
= 0;
1138 perror_with_name (_("Couldn't get general-purpose registers"));
1141 supply_gregset (regcache
, (const gdb_gregset_t
*) &gregset
);
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. */
1153 fetch_gp_regs (struct regcache
*regcache
, int tid
)
1155 struct gdbarch
*gdbarch
= regcache
->arch ();
1156 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
1159 if (have_ptrace_getsetregs
)
1160 if (fetch_all_gp_regs (regcache
, tid
))
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
);
1170 /* This function actually issues the request to ptrace, telling
1171 it to get all floating-point registers and put them into the
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. */
1179 fetch_all_fp_regs (struct regcache
*regcache
, int tid
)
1181 gdb_fpregset_t fpregs
;
1183 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (void *) &fpregs
) < 0)
1187 have_ptrace_getsetfpregs
= 0;
1190 perror_with_name (_("Couldn't get floating-point registers"));
1193 supply_fpregset (regcache
, (const gdb_fpregset_t
*) &fpregs
);
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. */
1205 fetch_fp_regs (struct regcache
*regcache
, int tid
)
1207 struct gdbarch
*gdbarch
= regcache
->arch ();
1208 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
1211 if (have_ptrace_getsetfpregs
)
1212 if (fetch_all_fp_regs (regcache
, tid
))
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
);
1223 fetch_ppc_registers (struct regcache
*regcache
, int tid
)
1225 struct gdbarch
*gdbarch
= regcache
->arch ();
1226 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
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
))
1246 fetch_register (regcache
, tid
, PPC_ORIG_R3_REGNUM
);
1247 fetch_register (regcache
, tid
, PPC_TRAP_REGNUM
);
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
);
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
)
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
),
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
)
1298 const struct regset
*cvmxregset
= ppc_linux_cvmxregset (gdbarch
);
1299 fetch_regset (regcache
, tid
, NT_PPC_TM_CVMX
,
1300 PPC_LINUX_SIZEOF_CVMXREGSET
,
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
);
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. */
1325 ppc_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
1327 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
1330 fetch_ppc_registers (regcache
, tid
);
1332 fetch_register (regcache
, tid
, regno
);
1336 store_vsx_registers (const struct regcache
*regcache
, int tid
, int regno
)
1339 gdb_vsxregset_t regs
;
1340 const struct regset
*vsxregset
= ppc_linux_vsxregset ();
1342 ret
= ptrace (PTRACE_GETVSXREGS
, tid
, 0, ®s
);
1347 have_ptrace_getsetvsxregs
= 0;
1350 perror_with_name (_("Unable to fetch VSX registers"));
1353 vsxregset
->collect_regset (vsxregset
, regcache
, regno
, ®s
,
1354 PPC_LINUX_SIZEOF_VSXREGSET
);
1356 ret
= ptrace (PTRACE_SETVSXREGS
, tid
, 0, ®s
);
1358 perror_with_name (_("Unable to store VSX registers"));
1362 store_altivec_registers (const struct regcache
*regcache
, int tid
,
1366 gdb_vrregset_t regs
;
1367 struct gdbarch
*gdbarch
= regcache
->arch ();
1368 const struct regset
*vrregset
= ppc_linux_vrregset (gdbarch
);
1370 ret
= ptrace (PTRACE_GETVRREGS
, tid
, 0, ®s
);
1375 have_ptrace_getvrregs
= 0;
1378 perror_with_name (_("Unable to fetch AltiVec registers"));
1381 vrregset
->collect_regset (vrregset
, regcache
, regno
, ®s
,
1382 PPC_LINUX_SIZEOF_VRREGSET
);
1384 ret
= ptrace (PTRACE_SETVRREGS
, tid
, 0, ®s
);
1386 perror_with_name (_("Unable to store AltiVec registers"));
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
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. */
1398 set_spe_registers (int tid
, struct gdb_evrregset_t
*evrregset
)
1400 if (have_ptrace_getsetevrregs
)
1402 if (ptrace (PTRACE_SETEVRREGS
, tid
, 0, evrregset
) >= 0)
1406 /* EIO means that the PTRACE_SETEVRREGS request isn't
1407 supported; we fail silently, and don't try the call
1410 have_ptrace_getsetevrregs
= 0;
1412 /* Anything else needs to be reported. */
1413 perror_with_name (_("Unable to set SPE registers"));
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
1422 store_spe_register (const struct regcache
*regcache
, int tid
, int regno
)
1424 struct gdbarch
*gdbarch
= regcache
->arch ();
1425 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
1426 struct gdb_evrregset_t evrregs
;
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
));
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
));
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
);
1450 for (i
= 0; i
< ppc_num_gprs
; i
++)
1451 regcache
->raw_collect (tdep
->ppc_ev0_upper_regnum
+ i
,
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
]);
1460 || regno
== tdep
->ppc_acc_regnum
)
1461 regcache
->raw_collect (tdep
->ppc_acc_regnum
,
1465 || regno
== tdep
->ppc_spefscr_regnum
)
1466 regcache
->raw_collect (tdep
->ppc_spefscr_regnum
,
1469 /* Write back the modified register set. */
1470 set_spe_registers (tid
, &evrregs
);
1474 store_register (const struct regcache
*regcache
, int tid
, int regno
)
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
);
1481 size_t bytes_to_transfer
;
1482 gdb_byte buf
[PPC_MAX_REGISTER_SIZE
];
1484 if (altivec_register_p (gdbarch
, regno
))
1486 store_altivec_registers (regcache
, tid
, regno
);
1489 else if (vsx_register_p (gdbarch
, regno
))
1491 store_vsx_registers (regcache
, tid
, regno
);
1494 else if (spe_register_p (gdbarch
, regno
))
1496 store_spe_register (regcache
, tid
, regno
);
1499 else if (regno
== PPC_DSCR_REGNUM
)
1501 gdb_assert (tdep
->ppc_dscr_regnum
!= -1);
1503 store_regset (regcache
, tid
, regno
, NT_PPC_DSCR
,
1504 PPC_LINUX_SIZEOF_DSCRREGSET
,
1505 &ppc32_linux_dscrregset
);
1508 else if (regno
== PPC_PPR_REGNUM
)
1510 gdb_assert (tdep
->ppc_ppr_regnum
!= -1);
1512 store_regset (regcache
, tid
, regno
, NT_PPC_PPR
,
1513 PPC_LINUX_SIZEOF_PPRREGSET
,
1514 &ppc32_linux_pprregset
);
1517 else if (regno
== PPC_TAR_REGNUM
)
1519 gdb_assert (tdep
->ppc_tar_regnum
!= -1);
1521 store_regset (regcache
, tid
, regno
, NT_PPC_TAR
,
1522 PPC_LINUX_SIZEOF_TARREGSET
,
1523 &ppc32_linux_tarregset
);
1526 else if (PPC_IS_EBB_REGNUM (regno
))
1528 gdb_assert (tdep
->have_ebb
);
1530 store_regset (regcache
, tid
, regno
, NT_PPC_EBB
,
1531 PPC_LINUX_SIZEOF_EBBREGSET
,
1532 &ppc32_linux_ebbregset
);
1535 else if (PPC_IS_PMU_REGNUM (regno
))
1537 gdb_assert (tdep
->ppc_mmcr0_regnum
!= -1);
1539 store_regset (regcache
, tid
, regno
, NT_PPC_PMU
,
1540 PPC_LINUX_SIZEOF_PMUREGSET
,
1541 &ppc32_linux_pmuregset
);
1544 else if (PPC_IS_TMSPR_REGNUM (regno
))
1546 gdb_assert (tdep
->have_htm_spr
);
1548 store_regset (regcache
, tid
, regno
, NT_PPC_TM_SPR
,
1549 PPC_LINUX_SIZEOF_TM_SPRREGSET
,
1550 &ppc32_linux_tm_sprregset
);
1553 else if (PPC_IS_CKPTGP_REGNUM (regno
))
1555 gdb_assert (tdep
->have_htm_core
);
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
),
1565 else if (PPC_IS_CKPTFP_REGNUM (regno
))
1567 gdb_assert (tdep
->have_htm_fpu
);
1569 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CFPR
,
1570 PPC_LINUX_SIZEOF_CFPRREGSET
,
1571 &ppc32_linux_cfprregset
);
1574 else if (PPC_IS_CKPTVMX_REGNUM (regno
))
1576 gdb_assert (tdep
->have_htm_altivec
);
1578 const struct regset
*cvmxregset
= ppc_linux_cvmxregset (gdbarch
);
1579 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CVMX
,
1580 PPC_LINUX_SIZEOF_CVMXREGSET
,
1584 else if (PPC_IS_CKPTVSX_REGNUM (regno
))
1586 gdb_assert (tdep
->have_htm_vsx
);
1588 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CVSX
,
1589 PPC_LINUX_SIZEOF_CVSXREGSET
,
1590 &ppc32_linux_cvsxregset
);
1593 else if (regno
== PPC_CPPR_REGNUM
)
1595 gdb_assert (tdep
->ppc_cppr_regnum
!= -1);
1597 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CPPR
,
1598 PPC_LINUX_SIZEOF_CPPRREGSET
,
1599 &ppc32_linux_cpprregset
);
1602 else if (regno
== PPC_CDSCR_REGNUM
)
1604 gdb_assert (tdep
->ppc_cdscr_regnum
!= -1);
1606 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CDSCR
,
1607 PPC_LINUX_SIZEOF_CDSCRREGSET
,
1608 &ppc32_linux_cdscrregset
);
1611 else if (regno
== PPC_CTAR_REGNUM
)
1613 gdb_assert (tdep
->ppc_ctar_regnum
!= -1);
1615 store_regset (regcache
, tid
, regno
, NT_PPC_TM_CTAR
,
1616 PPC_LINUX_SIZEOF_CTARREGSET
,
1617 &ppc32_linux_ctarregset
);
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
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
)
1631 /* Little-endian values always sit at the left end of the buffer. */
1632 regcache
->raw_collect (regno
, buf
);
1634 else if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
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
);
1641 for (i
= 0; i
< bytes_to_transfer
; i
+= sizeof (long))
1645 memcpy (&l
, &buf
[i
], sizeof (l
));
1647 ptrace (PTRACE_POKEUSER
, tid
, (PTRACE_TYPE_ARG3
) regaddr
, l
);
1648 regaddr
+= sizeof (long);
1651 && (regno
== tdep
->ppc_fpscr_regnum
1652 || regno
== PPC_ORIG_R3_REGNUM
1653 || regno
== PPC_TRAP_REGNUM
))
1655 /* Some older kernel versions don't allow fpscr, orig_r3
1656 or trap to be written. */
1663 xsnprintf (message
, sizeof (message
), "writing register %s (#%d)",
1664 gdbarch_register_name (gdbarch
, regno
), regno
);
1665 perror_with_name (message
);
1670 /* This function actually issues the request to ptrace, telling
1671 it to store all general-purpose registers present in the specified
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. */
1679 store_all_gp_regs (const struct regcache
*regcache
, int tid
, int regno
)
1681 gdb_gregset_t gregset
;
1683 if (ptrace (PTRACE_GETREGS
, tid
, 0, (void *) &gregset
) < 0)
1687 have_ptrace_getsetregs
= 0;
1690 perror_with_name (_("Couldn't get general-purpose registers"));
1693 fill_gregset (regcache
, &gregset
, regno
);
1695 if (ptrace (PTRACE_SETREGS
, tid
, 0, (void *) &gregset
) < 0)
1699 have_ptrace_getsetregs
= 0;
1702 perror_with_name (_("Couldn't set general-purpose registers"));
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. */
1715 store_gp_regs (const struct regcache
*regcache
, int tid
, int regno
)
1717 struct gdbarch
*gdbarch
= regcache
->arch ();
1718 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
1721 if (have_ptrace_getsetregs
)
1722 if (store_all_gp_regs (regcache
, tid
, regno
))
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
);
1732 /* This function actually issues the request to ptrace, telling
1733 it to store all floating-point registers present in the specified
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. */
1741 store_all_fp_regs (const struct regcache
*regcache
, int tid
, int regno
)
1743 gdb_fpregset_t fpregs
;
1745 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (void *) &fpregs
) < 0)
1749 have_ptrace_getsetfpregs
= 0;
1752 perror_with_name (_("Couldn't get floating-point registers"));
1755 fill_fpregset (regcache
, &fpregs
, regno
);
1757 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (void *) &fpregs
) < 0)
1761 have_ptrace_getsetfpregs
= 0;
1764 perror_with_name (_("Couldn't set floating-point registers"));
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. */
1777 store_fp_regs (const struct regcache
*regcache
, int tid
, int regno
)
1779 struct gdbarch
*gdbarch
= regcache
->arch ();
1780 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
1783 if (have_ptrace_getsetfpregs
)
1784 if (store_all_fp_regs (regcache
, tid
, regno
))
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
);
1795 store_ppc_registers (const struct regcache
*regcache
, int tid
)
1797 struct gdbarch
*gdbarch
= regcache
->arch ();
1798 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
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
))
1820 store_register (regcache
, tid
, PPC_ORIG_R3_REGNUM
);
1821 store_register (regcache
, tid
, PPC_TRAP_REGNUM
);
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
);
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
);
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
);
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. */
1860 ppc_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
1862 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
1865 store_register (regcache
, tid
, regno
);
1867 store_ppc_registers (regcache
, tid
);
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. */
1877 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
1879 const struct regset
*regset
= ppc_linux_gregset (sizeof (long));
1881 ppc_supply_gregset (regset
, regcache
, -1, gregsetp
, sizeof (*gregsetp
));
1885 fill_gregset (const struct regcache
*regcache
,
1886 gdb_gregset_t
*gregsetp
, int regno
)
1888 const struct regset
*regset
= ppc_linux_gregset (sizeof (long));
1891 memset (gregsetp
, 0, sizeof (*gregsetp
));
1892 ppc_collect_gregset (regset
, regcache
, regno
, gregsetp
, sizeof (*gregsetp
));
1896 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
* fpregsetp
)
1898 const struct regset
*regset
= ppc_linux_fpregset ();
1900 ppc_supply_fpregset (regset
, regcache
, -1,
1901 fpregsetp
, sizeof (*fpregsetp
));
1905 fill_fpregset (const struct regcache
*regcache
,
1906 gdb_fpregset_t
*fpregsetp
, int regno
)
1908 const struct regset
*regset
= ppc_linux_fpregset ();
1910 ppc_collect_fpregset (regset
, regcache
, regno
,
1911 fpregsetp
, sizeof (*fpregsetp
));
1915 ppc_linux_nat_target::auxv_parse (const gdb_byte
**readptr
,
1916 const gdb_byte
*endptr
, CORE_ADDR
*typep
,
1919 gdb_assert (inferior_ptid
!= null_ptid
);
1921 int tid
= inferior_ptid
.lwp ();
1923 tid
= inferior_ptid
.pid ();
1925 int sizeof_auxv_field
= ppc_linux_target_wordsize (tid
);
1927 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
1928 const gdb_byte
*ptr
= *readptr
;
1933 if (endptr
- ptr
< sizeof_auxv_field
* 2)
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
;
1945 const struct target_desc
*
1946 ppc_linux_nat_target::read_description ()
1948 if (inferior_ptid
== null_ptid
)
1949 return this->beneath ()->read_description ();
1951 int tid
= inferior_ptid
.pid ();
1953 if (have_ptrace_getsetevrregs
)
1955 struct gdb_evrregset_t evrregset
;
1957 if (ptrace (PTRACE_GETEVRREGS
, tid
, 0, &evrregset
) >= 0)
1958 return tdesc_powerpc_e500l
;
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"));
1966 struct ppc_linux_features features
= ppc_linux_no_features
;
1968 features
.wordsize
= ppc_linux_target_wordsize (tid
);
1970 CORE_ADDR hwcap
= linux_get_hwcap ();
1971 CORE_ADDR hwcap2
= linux_get_hwcap2 ();
1973 if (have_ptrace_getsetvsxregs
1974 && (hwcap
& PPC_FEATURE_HAS_VSX
))
1976 gdb_vsxregset_t vsxregset
;
1978 if (ptrace (PTRACE_GETVSXREGS
, tid
, 0, &vsxregset
) >= 0)
1979 features
.vsx
= true;
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"));
1987 if (have_ptrace_getvrregs
1988 && (hwcap
& PPC_FEATURE_HAS_ALTIVEC
))
1990 gdb_vrregset_t vrregset
;
1992 if (ptrace (PTRACE_GETVRREGS
, tid
, 0, &vrregset
) >= 0)
1993 features
.altivec
= true;
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"));
2001 features
.isa205
= ppc_linux_has_isa205 (hwcap
);
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
))
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
))
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;
2023 return ppc_linux_match_description (features
);
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
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. */
2042 /* Check if we support and have enough resources to install a hardware
2043 watchpoint or breakpoint. See the description in target.h. */
2046 ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type
, int cnt
,
2049 int total_hw_wp
, total_hw_bp
;
2051 m_dreg_interface
.detect (inferior_ptid
);
2053 if (m_dreg_interface
.unavailable_p ())
2056 if (m_dreg_interface
.hwdebug_p ())
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
;
2066 gdb_assert (m_dreg_interface
.debugreg_p ());
2068 /* With the DEBUGREG ptrace interface, we should consider having 1
2069 hardware watchpoint and no hardware breakpoints. */
2074 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
2075 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
2077 if (total_hw_wp
== 0)
2079 else if (cnt
+ ot
> total_hw_wp
)
2084 else if (type
== bp_hardware_breakpoint
)
2086 if (total_hw_bp
== 0)
2088 else if (cnt
> total_hw_bp
)
2097 /* Returns 1 if we can watch LEN bytes at address ADDR, 0 otherwise. */
2100 ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
2102 /* Handle sub-8-byte quantities. */
2106 m_dreg_interface
.detect (inferior_ptid
);
2108 if (m_dreg_interface
.unavailable_p ())
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
2115 if (m_dreg_interface
.hwdebug_p ())
2117 const struct ppc_debug_info
&hwdebug_info
= (m_dreg_interface
2119 int region_size
= hwdebug_info
.data_bp_alignment
;
2120 int region_align
= region_size
;
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. */
2127 && hwdebug_info
.features
& PPC_DEBUG_FEATURE_DATA_BP_RANGE
2128 && (linux_get_hwcap () & PPC_FEATURE_BOOKE
))
2130 /* Check if the processor provides DAWR interface. */
2131 if (hwdebug_info
.features
& PPC_DEBUG_FEATURE_DATA_BP_DAWR
)
2133 /* DAWR interface allows to watch up to 512 byte wide ranges. */
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
))
2141 /* Server processors provide one hardware watchpoint and addr+len should
2142 fall in the watchable region provided by the ptrace interface. */
2144 && (addr
+ len
> (addr
& ~(region_align
- 1)) + region_size
))
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. */
2154 gdb_assert (m_dreg_interface
.debugreg_p ());
2156 if (((linux_get_hwcap () & PPC_FEATURE_BOOKE
)
2157 && (addr
+ len
) > (addr
& ~3) + 4)
2158 || (addr
+ len
) > (addr
& ~7) + 8)
2165 /* This function compares two ppc_hw_breakpoint structs
2169 ppc_linux_nat_target::hwdebug_point_cmp (const struct ppc_hw_breakpoint
&a
,
2170 const struct ppc_hw_breakpoint
&b
)
2172 return (a
.trigger_type
== b
.trigger_type
2173 && a
.addr_mode
== b
.addr_mode
2174 && a
.condition_mode
== b
.condition_mode
2176 && a
.addr2
== b
.addr2
2177 && a
.condition_value
== b
.condition_value
);
2180 /* Return the number of registers needed for a ranged breakpoint. */
2183 ppc_linux_nat_target::ranged_break_num_registers ()
2185 m_dreg_interface
.detect (inferior_ptid
);
2187 return ((m_dreg_interface
.hwdebug_p ()
2188 && (m_dreg_interface
.hwdebug_info ().features
2189 & PPC_DEBUG_FEATURE_INSN_BP_RANGE
))?
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. */
2199 ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
2200 struct bp_target_info
*bp_tgt
)
2202 struct ppc_hw_breakpoint p
;
2204 m_dreg_interface
.detect (inferior_ptid
);
2206 if (!m_dreg_interface
.hwdebug_p ())
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;
2217 p
.addr_mode
= PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE
;
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
;
2225 p
.addr_mode
= PPC_BREAKPOINT_MODE_EXACT
;
2229 register_hw_breakpoint (inferior_ptid
.pid (), p
);
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. */
2240 ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
2241 struct bp_target_info
*bp_tgt
)
2243 struct ppc_hw_breakpoint p
;
2245 m_dreg_interface
.detect (inferior_ptid
);
2247 if (!m_dreg_interface
.hwdebug_p ())
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;
2258 p
.addr_mode
= PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE
;
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
;
2266 p
.addr_mode
= PPC_BREAKPOINT_MODE_EXACT
;
2270 clear_hw_breakpoint (inferior_ptid
.pid (), p
);
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. */
2279 ppc_linux_nat_target::get_trigger_type (enum target_hw_bp_type type
)
2283 if (type
== hw_read
)
2284 t
= PPC_BREAKPOINT_TRIGGER_READ
;
2285 else if (type
== hw_write
)
2286 t
= PPC_BREAKPOINT_TRIGGER_WRITE
;
2288 t
= PPC_BREAKPOINT_TRIGGER_READ
| PPC_BREAKPOINT_TRIGGER_WRITE
;
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. */
2299 ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
2300 target_hw_bp_type rw
)
2302 struct ppc_hw_breakpoint p
;
2304 gdb_assert (m_dreg_interface
.hwdebug_p ());
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
;
2312 p
.condition_value
= 0;
2314 register_hw_breakpoint (inferior_ptid
.pid (), p
);
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. */
2325 ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
2326 target_hw_bp_type rw
)
2328 struct ppc_hw_breakpoint p
;
2330 gdb_assert (m_dreg_interface
.hwdebug_p ());
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
;
2338 p
.condition_value
= 0;
2340 clear_hw_breakpoint (inferior_ptid
.pid (), p
);
2345 /* Check whether we have at least one free DVC register for the threads
2346 of the pid of inferior_ptid. */
2349 ppc_linux_nat_target::can_use_watchpoint_cond_accel (void)
2351 m_dreg_interface
.detect (inferior_ptid
);
2353 if (!m_dreg_interface
.hwdebug_p ())
2356 int cnt
= m_dreg_interface
.hwdebug_info ().num_condition_regs
;
2361 auto process_it
= m_process_info
.find (inferior_ptid
.pid ());
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 ())
2368 for (const ppc_hw_breakpoint
&bp
: process_it
->second
.requested_hw_bps
)
2369 if (bp
.condition_mode
!= PPC_BREAKPOINT_CONDITION_NONE
)
2378 /* Calculate the enable bits and the contents of the Data Value Compare
2379 debug register present in BookE processors.
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
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
)
2393 const struct ppc_debug_info
&hwdebug_info
= (m_dreg_interface
.
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
;
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. */
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;
2416 *condition_mode
= PPC_BREAKPOINT_CONDITION_AND
;
2417 for (i
= 0; i
< num_byte_enable
; i
++)
2419 |= PPC_BREAKPOINT_CONDITION_BE (i
+ rightmost_enabled_byte
);
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). */
2425 *condition_value
= ((uint64_t) data_value
>> num_bytes_off_dvc
* 8
2426 << rightmost_enabled_byte
* 8);
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). */
2436 ppc_linux_nat_target::num_memory_accesses (const std::vector
<value_ref_ptr
>
2439 int found_memory_cnt
= 0;
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
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.
2456 for (const value_ref_ptr
&iter
: chain
)
2458 struct value
*v
= iter
.get ();
2460 /* Constants and values from the history are fine. */
2461 if (v
->lval () == not_lval
|| !v
->deprecated_modifiable ())
2463 else if (v
->lval () == lval_memory
)
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'). */
2471 /* Other kinds of values are not fine. */
2476 return found_memory_cnt
;
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
2487 ppc_linux_nat_target::check_condition (CORE_ADDR watch_addr
,
2488 struct expression
*cond
,
2489 CORE_ADDR
*data_value
, int *len
)
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
;
2495 expr::equal_operation
*eqop
2496 = dynamic_cast<expr::equal_operation
*> (cond
->op
.get ());
2497 if (eqop
== nullptr)
2499 expr::operation
*lhs
= eqop
->get_lhs ();
2500 expr::operation
*rhs
= eqop
->get_rhs ();
2502 fetch_subexp_value (cond
, lhs
, &left_val
, NULL
, &left_chain
, false);
2503 num_accesses_left
= num_memory_accesses (left_chain
);
2505 if (left_val
== NULL
|| num_accesses_left
< 0)
2508 fetch_subexp_value (cond
, rhs
, &right_val
, NULL
, &right_chain
, false);
2509 num_accesses_right
= num_memory_accesses (right_chain
);
2511 if (right_val
== NULL
|| num_accesses_right
< 0)
2514 if (num_accesses_left
== 1 && num_accesses_right
== 0
2515 && left_val
->lval () == lval_memory
2516 && left_val
->address () == watch_addr
)
2518 *data_value
= value_as_long (right_val
);
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 ();
2524 else if (num_accesses_left
== 0 && num_accesses_right
== 1
2525 && right_val
->lval () == lval_memory
2526 && right_val
->address () == watch_addr
)
2528 *data_value
= value_as_long (left_val
);
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 ();
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
2545 ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr
,
2547 struct expression
*cond
)
2549 CORE_ADDR data_value
;
2551 m_dreg_interface
.detect (inferior_ptid
);
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
));
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. */
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
,
2570 const struct ppc_debug_info
&hwdebug_info
= (m_dreg_interface
2574 || !(hwdebug_info
.features
& PPC_DEBUG_FEATURE_DATA_BP_RANGE
))
2577 CORE_ADDR data_value
;
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
,
2583 calculate_dvc (addr
, len
, data_value
, &p
->condition_mode
,
2584 &p
->condition_value
);
2587 p
->condition_mode
= PPC_BREAKPOINT_CONDITION_NONE
;
2588 p
->condition_value
= 0;
2591 p
->addr_mode
= PPC_BREAKPOINT_MODE_EXACT
;
2596 p
->addr_mode
= PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE
;
2597 p
->condition_mode
= PPC_BREAKPOINT_CONDITION_NONE
;
2598 p
->condition_value
= 0;
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.
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
;
2609 p
->version
= PPC_DEBUG_CURRENT_VERSION
;
2610 p
->trigger_type
= get_trigger_type (type
);
2611 p
->addr
= (uint64_t) addr
;
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. */
2619 ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr
, int len
,
2620 enum target_hw_bp_type type
,
2621 struct expression
*cond
)
2623 m_dreg_interface
.detect (inferior_ptid
);
2625 if (m_dreg_interface
.unavailable_p ())
2628 if (m_dreg_interface
.hwdebug_p ())
2630 struct ppc_hw_breakpoint p
;
2632 create_watchpoint_request (&p
, addr
, len
, type
, cond
, 1);
2634 register_hw_breakpoint (inferior_ptid
.pid (), p
);
2638 gdb_assert (m_dreg_interface
.debugreg_p ());
2641 long read_mode
, write_mode
;
2643 if (linux_get_hwcap () & PPC_FEATURE_BOOKE
)
2645 /* PowerPC 440 requires only the read/write flags to be passed
2652 /* PowerPC 970 and other DABR-based processors are required to pass
2653 the Breakpoint Translation bit together with the flags. */
2658 wp_value
= addr
& ~(read_mode
| write_mode
);
2662 /* Set read and translate bits. */
2663 wp_value
|= read_mode
;
2666 /* Set write and translate bits. */
2667 wp_value
|= write_mode
;
2670 /* Set read, write and translate bits. */
2671 wp_value
|= read_mode
| write_mode
;
2675 register_wp (inferior_ptid
.pid (), wp_value
);
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
2686 ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr
, int len
,
2687 enum target_hw_bp_type type
,
2688 struct expression
*cond
)
2690 gdb_assert (!m_dreg_interface
.unavailable_p ());
2692 if (m_dreg_interface
.hwdebug_p ())
2694 struct ppc_hw_breakpoint p
;
2696 create_watchpoint_request (&p
, addr
, len
, type
, cond
, 0);
2698 clear_hw_breakpoint (inferior_ptid
.pid (), p
);
2702 gdb_assert (m_dreg_interface
.debugreg_p ());
2704 clear_wp (inferior_ptid
.pid ());
2710 /* Implement the "low_init_process" target_ops method. */
2713 ppc_linux_nat_target::low_init_process (pid_t pid
)
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
2720 m_dreg_interface
.detect (ptid_t (pid
, pid
));
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.
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. */
2732 ppc_linux_nat_target::low_forget_process (pid_t pid
)
2734 if ((!m_dreg_interface
.detected_p ())
2735 || (m_dreg_interface
.unavailable_p ()))
2738 ptid_t
pid_ptid (pid
, 0, 0);
2740 m_process_info
.erase (pid
);
2742 if (m_dreg_interface
.hwdebug_p ())
2744 for (auto it
= m_installed_hw_bps
.begin ();
2745 it
!= m_installed_hw_bps
.end ();)
2747 if (it
->first
.matches (pid_ptid
))
2748 it
= m_installed_hw_bps
.erase (it
);
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.
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.
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.
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. */
2776 ppc_linux_nat_target::low_new_fork (struct lwp_info
*parent
,
2779 if ((!m_dreg_interface
.detected_p ())
2780 || (m_dreg_interface
.unavailable_p ()))
2783 auto process_it
= m_process_info
.find (parent
->ptid
.pid ());
2785 if (process_it
!= m_process_info
.end ())
2786 m_process_info
[child_pid
] = m_process_info
[parent
->ptid
.pid ()];
2788 if (m_dreg_interface
.hwdebug_p ())
2790 ptid_t
child_ptid (child_pid
, child_pid
, 0);
2792 copy_thread_dreg_state (parent
->ptid
, child_ptid
);
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. */
2803 ppc_linux_nat_target::low_new_clone (struct lwp_info
*parent
,
2806 if ((!m_dreg_interface
.detected_p ())
2807 || (m_dreg_interface
.unavailable_p ()))
2810 if (m_dreg_interface
.hwdebug_p ())
2812 ptid_t
child_ptid (parent
->ptid
.pid (), child_lwp
, 0);
2814 copy_thread_dreg_state (parent
->ptid
, child_ptid
);
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. */
2824 ppc_linux_nat_target::low_new_thread (struct lwp_info
*lp
)
2826 init_arch_lwp_info (lp
);
2828 mark_thread_stale (lp
);
2831 /* Delete the per-thread debug register stale flag. */
2834 ppc_linux_nat_target::low_delete_thread (struct arch_lwp_info
2837 if (lp_arch_info
!= NULL
)
2839 if (m_dreg_interface
.detected_p ()
2840 && m_dreg_interface
.hwdebug_p ())
2841 m_installed_hw_bps
.erase (lp_arch_info
->lwp_ptid
);
2843 xfree (lp_arch_info
);
2847 /* Install or delete debug registers in thread LP so that it matches what
2848 GDB requested before it is resumed. */
2851 ppc_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lp
)
2853 if ((!m_dreg_interface
.detected_p ())
2854 || (m_dreg_interface
.unavailable_p ()))
2857 /* We have to re-install or clear the debug registers if we set the
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. */
2867 arch_lwp_info
*lp_arch_info
= get_arch_lwp_info (lp
);
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
);
2876 gdb_assert (lp
->ptid
.lwp_p ());
2878 auto process_it
= m_process_info
.find (lp
->ptid
.pid ());
2880 if (m_dreg_interface
.hwdebug_p ())
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
);
2886 if (installed_it
!= m_installed_hw_bps
.end ())
2888 auto &bp_list
= installed_it
->second
;
2890 for (auto bp_it
= bp_list
.begin (); bp_it
!= bp_list
.end ();)
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
2897 if (ptrace (PPC_PTRACE_DELHWDEBUG
, lp
->ptid
.lwp (), 0,
2899 if (errno
!= ENOENT
)
2900 perror_with_name (_("Error deleting hardware "
2901 "breakpoint or watchpoint"));
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
);
2910 gdb_assert (bp_list
.empty ());
2913 /* Now we install all the requested hardware breakpoints and
2914 watchpoints and update the thread state. */
2916 if (process_it
!= m_process_info
.end ())
2918 auto &bp_list
= m_installed_hw_bps
[lp
->ptid
];
2920 for (ppc_hw_breakpoint bp
2921 : process_it
->second
.requested_hw_bps
)
2923 long slot
= ptrace (PPC_PTRACE_SETHWDEBUG
, lp
->ptid
.lwp (),
2927 perror_with_name (_("Error setting hardware "
2928 "breakpoint or watchpoint"));
2930 /* Keep track of which slots we installed in this
2932 bp_list
.emplace (bp_list
.begin (), slot
, bp
);
2938 gdb_assert (m_dreg_interface
.debugreg_p ());
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"));
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 ())
2952 long wp
= *(process_it
->second
.requested_wp_val
);
2954 if (ptrace (PTRACE_SET_DEBUGREG
, lp
->ptid
.lwp (), 0, wp
) < 0)
2955 perror_with_name (_("Error setting hardware watchpoint"));
2959 lp_arch_info
->debug_regs_stale
= false;
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. */
2967 ppc_linux_nat_target::low_stopped_data_address (CORE_ADDR
*addr_p
)
2971 if (!linux_nat_get_siginfo (inferior_ptid
, &siginfo
))
2974 if (siginfo
.si_signo
!= SIGTRAP
2975 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
2978 gdb_assert (!m_dreg_interface
.unavailable_p ());
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 ())
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
;
2992 auto installed_it
= m_installed_hw_bps
.find (inferior_ptid
);
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 ());
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
))
3005 *addr_p
= (CORE_ADDR
) (uintptr_t) siginfo
.si_addr
;
3009 /* Return true if INFERIOR_PTID is known to have been stopped by a
3010 hardware watchpoint, false otherwise. */
3013 ppc_linux_nat_target::low_stopped_by_watchpoint ()
3016 return low_stopped_data_address (&addr
);
3020 ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr
,
3024 gdb_assert (!m_dreg_interface
.unavailable_p ());
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
)
3038 /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
3039 return start
<= addr
+ mask
&& start
+ length
- 1 >= addr
;
3042 /* Return the number of registers needed for a masked hardware watchpoint. */
3045 ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr
,
3048 m_dreg_interface
.detect (inferior_ptid
);
3050 if (!m_dreg_interface
.hwdebug_p ()
3051 || (m_dreg_interface
.hwdebug_info ().features
3052 & PPC_DEBUG_FEATURE_DATA_BP_MASK
) == 0)
3054 else if ((mask
& 0xC0000000) != 0xC0000000)
3056 warning (_("The given mask covers kernel address space "
3057 "and cannot be used.\n"));
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
3070 ppc_linux_nat_target::copy_thread_dreg_state (const ptid_t
&parent_ptid
,
3071 const ptid_t
&child_ptid
)
3073 gdb_assert (m_dreg_interface
.hwdebug_p ());
3075 auto installed_it
= m_installed_hw_bps
.find (parent_ptid
);
3077 if (installed_it
!= m_installed_hw_bps
.end ())
3078 m_installed_hw_bps
[child_ptid
] = m_installed_hw_bps
[parent_ptid
];
3081 /* Mark the debug register stale flag for the new thread, if we have
3082 already detected which debug register interface we use. */
3085 ppc_linux_nat_target::mark_thread_stale (struct lwp_info
*lp
)
3087 if ((!m_dreg_interface
.detected_p ())
3088 || (m_dreg_interface
.unavailable_p ()))
3091 arch_lwp_info
*lp_arch_info
= get_arch_lwp_info (lp
);
3093 lp_arch_info
->debug_regs_stale
= true;
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. */
3101 ppc_linux_nat_target::mark_debug_registers_changed (pid_t pid
)
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. */
3106 iterate_over_lwps (ptid_t (pid
),
3107 [this] (struct lwp_info
*lp
) -> int {
3108 this->mark_thread_stale (lp
);
3112 iterate_over_lwps (ptid_t (pid
),
3113 [] (struct lwp_info
*lp
) -> int {
3114 if (!lwp_is_stopped (lp
))
3115 linux_stop_lwp (lp
);
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. */
3127 ppc_linux_nat_target::register_hw_breakpoint (pid_t pid
,
3129 ppc_hw_breakpoint
&bp
)
3131 gdb_assert (m_dreg_interface
.hwdebug_p ());
3133 m_process_info
[pid
].requested_hw_bps
.push_back (bp
);
3135 mark_debug_registers_changed (pid
);
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. */
3145 ppc_linux_nat_target::clear_hw_breakpoint (pid_t pid
,
3146 const struct ppc_hw_breakpoint
&bp
)
3148 gdb_assert (m_dreg_interface
.hwdebug_p ());
3150 auto process_it
= m_process_info
.find (pid
);
3152 gdb_assert (process_it
!= m_process_info
.end ());
3154 auto bp_it
= std::find_if (process_it
->second
.requested_hw_bps
.begin (),
3155 process_it
->second
.requested_hw_bps
.end (),
3157 (const struct ppc_hw_breakpoint
&curr
)
3158 { return hwdebug_point_cmp (bp
, curr
); }
3161 /* If GDB is removing a watchpoint, it must have been inserted. */
3162 gdb_assert (bp_it
!= process_it
->second
.requested_hw_bps
.end ());
3164 process_it
->second
.requested_hw_bps
.erase (bp_it
);
3166 mark_debug_registers_changed (pid
);
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. */
3176 ppc_linux_nat_target::register_wp (pid_t pid
, long wp_value
)
3178 gdb_assert (m_dreg_interface
.debugreg_p ());
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 ());
3184 m_process_info
[pid
].requested_wp_val
.emplace (wp_value
);
3186 mark_debug_registers_changed (pid
);
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. */
3196 ppc_linux_nat_target::clear_wp (pid_t pid
)
3198 gdb_assert (m_dreg_interface
.debugreg_p ());
3200 auto process_it
= m_process_info
.find (pid
);
3202 gdb_assert (process_it
!= m_process_info
.end ());
3203 gdb_assert (process_it
->second
.requested_wp_val
.has_value ());
3205 process_it
->second
.requested_wp_val
.reset ();
3207 mark_debug_registers_changed (pid
);
3210 /* Initialize the arch-specific thread state for LWP, if it not already
3214 ppc_linux_nat_target::init_arch_lwp_info (struct lwp_info
*lp
)
3216 if (lwp_arch_private_info (lp
) == NULL
)
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
;
3224 /* Get the arch-specific thread state for LWP, creating it if
3228 ppc_linux_nat_target::get_arch_lwp_info (struct lwp_info
*lp
)
3230 init_arch_lwp_info (lp
);
3232 return lwp_arch_private_info (lp
);
3235 void _initialize_ppc_linux_nat ();
3237 _initialize_ppc_linux_nat ()
3239 linux_target
= &the_ppc_linux_nat_target
;
3241 /* Register the target. */
3242 add_inf_child_target (linux_target
);