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