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