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