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