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