btrace: honour scheduler-locking for all-stop targets
[deliverable/binutils-gdb.git] / gdb / aarch64-linux-nat.c
CommitLineData
9d19df75
MS
1/* Native-dependent code for GNU/Linux AArch64.
2
32d0add0 3 Copyright (C) 2011-2015 Free Software Foundation, Inc.
9d19df75
MS
4 Contributed by ARM Ltd.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22
23#include "inferior.h"
24#include "gdbcore.h"
25#include "regcache.h"
26#include "linux-nat.h"
27#include "target-descriptions.h"
28#include "auxv.h"
29#include "gdbcmd.h"
30#include "aarch64-tdep.h"
31#include "aarch64-linux-tdep.h"
607685ec 32#include "aarch32-linux-nat.h"
db3cb7cb 33#include "nat/aarch64-linux.h"
554717a3 34#include "nat/aarch64-linux-hw-point.h"
607685ec
YQ
35
36#include "elf/external.h"
9d19df75
MS
37#include "elf/common.h"
38
5826e159 39#include "nat/gdb_ptrace.h"
9d19df75 40#include <sys/utsname.h>
036cd381 41#include <asm/ptrace.h>
9d19df75
MS
42
43#include "gregset.h"
44
9d19df75
MS
45/* Defines ps_err_e, struct ps_prochandle. */
46#include "gdb_proc_service.h"
47
48#ifndef TRAP_HWBKPT
49#define TRAP_HWBKPT 0x0004
50#endif
51
d6c44983
YZ
52/* Per-process data. We don't bind this to a per-inferior registry
53 because of targets like x86 GNU/Linux that need to keep track of
54 processes that aren't bound to any inferior (e.g., fork children,
55 checkpoints). */
9d19df75 56
d6c44983 57struct aarch64_process_info
9d19df75 58{
d6c44983
YZ
59 /* Linked list. */
60 struct aarch64_process_info *next;
9d19df75 61
d6c44983
YZ
62 /* The process identifier. */
63 pid_t pid;
9d19df75 64
d6c44983
YZ
65 /* Copy of aarch64 hardware debug registers. */
66 struct aarch64_debug_reg_state state;
67};
68
69static struct aarch64_process_info *aarch64_process_list = NULL;
70
71/* Find process data for process PID. */
72
73static struct aarch64_process_info *
74aarch64_find_process_pid (pid_t pid)
75{
76 struct aarch64_process_info *proc;
77
78 for (proc = aarch64_process_list; proc; proc = proc->next)
79 if (proc->pid == pid)
80 return proc;
81
82 return NULL;
9d19df75
MS
83}
84
d6c44983
YZ
85/* Add process data for process PID. Returns newly allocated info
86 object. */
9d19df75 87
d6c44983
YZ
88static struct aarch64_process_info *
89aarch64_add_process (pid_t pid)
9d19df75 90{
d6c44983 91 struct aarch64_process_info *proc;
9d19df75 92
8d749320 93 proc = XCNEW (struct aarch64_process_info);
d6c44983 94 proc->pid = pid;
9d19df75 95
d6c44983
YZ
96 proc->next = aarch64_process_list;
97 aarch64_process_list = proc;
98
99 return proc;
100}
101
102/* Get data specific info for process PID, creating it if necessary.
103 Never returns NULL. */
104
105static struct aarch64_process_info *
106aarch64_process_info_get (pid_t pid)
9d19df75 107{
d6c44983
YZ
108 struct aarch64_process_info *proc;
109
110 proc = aarch64_find_process_pid (pid);
111 if (proc == NULL)
112 proc = aarch64_add_process (pid);
9d19df75 113
d6c44983 114 return proc;
9d19df75
MS
115}
116
d6c44983
YZ
117/* Called whenever GDB is no longer debugging process PID. It deletes
118 data structures that keep track of debug register state. */
9d19df75 119
d6c44983
YZ
120static void
121aarch64_forget_process (pid_t pid)
9d19df75 122{
d6c44983 123 struct aarch64_process_info *proc, **proc_link;
9d19df75 124
d6c44983
YZ
125 proc = aarch64_process_list;
126 proc_link = &aarch64_process_list;
127
128 while (proc != NULL)
9d19df75 129 {
d6c44983
YZ
130 if (proc->pid == pid)
131 {
132 *proc_link = proc->next;
9d19df75 133
d6c44983
YZ
134 xfree (proc);
135 return;
136 }
137
138 proc_link = &proc->next;
139 proc = *proc_link;
140 }
9d19df75
MS
141}
142
d6c44983 143/* Get debug registers state for process PID. */
9d19df75 144
db3cb7cb 145struct aarch64_debug_reg_state *
d6c44983 146aarch64_get_debug_reg_state (pid_t pid)
9d19df75 147{
d6c44983 148 return &aarch64_process_info_get (pid)->state;
9d19df75
MS
149}
150
9d19df75
MS
151/* Fill GDB's register array with the general-purpose register values
152 from the current thread. */
153
154static void
155fetch_gregs_from_thread (struct regcache *regcache)
156{
607685ec
YQ
157 int ret, tid;
158 struct gdbarch *gdbarch = get_regcache_arch (regcache);
9d19df75
MS
159 elf_gregset_t regs;
160 struct iovec iovec;
161
607685ec
YQ
162 /* Make sure REGS can hold all registers contents on both aarch64
163 and arm. */
164 gdb_static_assert (sizeof (regs) >= 18 * 4);
165
d89fa914 166 tid = ptid_get_lwp (inferior_ptid);
9d19df75
MS
167
168 iovec.iov_base = &regs;
607685ec
YQ
169 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
170 iovec.iov_len = 18 * 4;
171 else
172 iovec.iov_len = sizeof (regs);
9d19df75
MS
173
174 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
175 if (ret < 0)
176 perror_with_name (_("Unable to fetch general registers."));
177
607685ec
YQ
178 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
179 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
180 else
181 {
182 int regno;
183
184 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
185 regcache_raw_supply (regcache, regno, &regs[regno - AARCH64_X0_REGNUM]);
186 }
9d19df75
MS
187}
188
189/* Store to the current thread the valid general-purpose register
190 values in the GDB's register array. */
191
192static void
193store_gregs_to_thread (const struct regcache *regcache)
194{
607685ec 195 int ret, tid;
9d19df75
MS
196 elf_gregset_t regs;
197 struct iovec iovec;
607685ec 198 struct gdbarch *gdbarch = get_regcache_arch (regcache);
9d19df75 199
607685ec
YQ
200 /* Make sure REGS can hold all registers contents on both aarch64
201 and arm. */
202 gdb_static_assert (sizeof (regs) >= 18 * 4);
d89fa914 203 tid = ptid_get_lwp (inferior_ptid);
9d19df75
MS
204
205 iovec.iov_base = &regs;
607685ec
YQ
206 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
207 iovec.iov_len = 18 * 4;
208 else
209 iovec.iov_len = sizeof (regs);
9d19df75
MS
210
211 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
212 if (ret < 0)
213 perror_with_name (_("Unable to fetch general registers."));
214
607685ec
YQ
215 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
216 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
217 else
218 {
219 int regno;
220
221 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
222 if (REG_VALID == regcache_register_status (regcache, regno))
223 regcache_raw_collect (regcache, regno,
224 &regs[regno - AARCH64_X0_REGNUM]);
225 }
9d19df75
MS
226
227 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
228 if (ret < 0)
229 perror_with_name (_("Unable to store general registers."));
230}
231
232/* Fill GDB's register array with the fp/simd register values
233 from the current thread. */
234
235static void
236fetch_fpregs_from_thread (struct regcache *regcache)
237{
607685ec 238 int ret, tid;
9d19df75
MS
239 elf_fpregset_t regs;
240 struct iovec iovec;
607685ec
YQ
241 struct gdbarch *gdbarch = get_regcache_arch (regcache);
242
243 /* Make sure REGS can hold all VFP registers contents on both aarch64
244 and arm. */
245 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
9d19df75 246
d89fa914 247 tid = ptid_get_lwp (inferior_ptid);
9d19df75
MS
248
249 iovec.iov_base = &regs;
9d19df75 250
607685ec
YQ
251 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
252 {
253 iovec.iov_len = VFP_REGS_SIZE;
254
255 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
256 if (ret < 0)
257 perror_with_name (_("Unable to fetch VFP registers."));
258
259 aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
260 }
261 else
262 {
263 int regno;
264
265 iovec.iov_len = sizeof (regs);
9d19df75 266
607685ec
YQ
267 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
268 if (ret < 0)
269 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
9d19df75 270
607685ec
YQ
271 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
272 regcache_raw_supply (regcache, regno,
273 &regs.vregs[regno - AARCH64_V0_REGNUM]);
274
275 regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, &regs.fpsr);
276 regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, &regs.fpcr);
277 }
9d19df75
MS
278}
279
280/* Store to the current thread the valid fp/simd register
281 values in the GDB's register array. */
282
283static void
284store_fpregs_to_thread (const struct regcache *regcache)
285{
607685ec 286 int ret, tid;
9d19df75
MS
287 elf_fpregset_t regs;
288 struct iovec iovec;
607685ec 289 struct gdbarch *gdbarch = get_regcache_arch (regcache);
9d19df75 290
607685ec
YQ
291 /* Make sure REGS can hold all VFP registers contents on both aarch64
292 and arm. */
293 gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
d89fa914 294 tid = ptid_get_lwp (inferior_ptid);
9d19df75
MS
295
296 iovec.iov_base = &regs;
9d19df75 297
607685ec
YQ
298 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
299 {
300 iovec.iov_len = VFP_REGS_SIZE;
9d19df75 301
607685ec
YQ
302 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
303 if (ret < 0)
304 perror_with_name (_("Unable to fetch VFP registers."));
9d19df75 305
607685ec
YQ
306 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
307 }
308 else
309 {
310 int regno;
9d19df75 311
607685ec
YQ
312 iovec.iov_len = sizeof (regs);
313
314 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
315 if (ret < 0)
316 perror_with_name (_("Unable to fetch FP/SIMD registers."));
317
318 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
319 if (REG_VALID == regcache_register_status (regcache, regno))
320 regcache_raw_collect (regcache, regno,
321 (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
322
323 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
324 regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM,
325 (char *) &regs.fpsr);
326 if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
327 regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM,
328 (char *) &regs.fpcr);
329 }
330
331 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
332 {
333 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
334 if (ret < 0)
335 perror_with_name (_("Unable to store VFP registers."));
336 }
337 else
338 {
339 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
340 if (ret < 0)
341 perror_with_name (_("Unable to store FP/SIMD registers."));
342 }
9d19df75
MS
343}
344
345/* Implement the "to_fetch_register" target_ops method. */
346
347static void
348aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
349 struct regcache *regcache,
350 int regno)
351{
352 if (regno == -1)
353 {
354 fetch_gregs_from_thread (regcache);
355 fetch_fpregs_from_thread (regcache);
356 }
357 else if (regno < AARCH64_V0_REGNUM)
358 fetch_gregs_from_thread (regcache);
359 else
360 fetch_fpregs_from_thread (regcache);
361}
362
363/* Implement the "to_store_register" target_ops method. */
364
365static void
366aarch64_linux_store_inferior_registers (struct target_ops *ops,
367 struct regcache *regcache,
368 int regno)
369{
370 if (regno == -1)
371 {
372 store_gregs_to_thread (regcache);
373 store_fpregs_to_thread (regcache);
374 }
375 else if (regno < AARCH64_V0_REGNUM)
376 store_gregs_to_thread (regcache);
377 else
378 store_fpregs_to_thread (regcache);
379}
380
381/* Fill register REGNO (if it is a general-purpose register) in
382 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
383 do this for all registers. */
384
385void
386fill_gregset (const struct regcache *regcache,
387 gdb_gregset_t *gregsetp, int regno)
388{
d4d793bf
AA
389 regcache_collect_regset (&aarch64_linux_gregset, regcache,
390 regno, (gdb_byte *) gregsetp,
391 AARCH64_LINUX_SIZEOF_GREGSET);
9d19df75
MS
392}
393
394/* Fill GDB's register array with the general-purpose register values
395 in *GREGSETP. */
396
397void
398supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
399{
d4d793bf
AA
400 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
401 (const gdb_byte *) gregsetp,
402 AARCH64_LINUX_SIZEOF_GREGSET);
9d19df75
MS
403}
404
405/* Fill register REGNO (if it is a floating-point register) in
406 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
407 do this for all registers. */
408
409void
410fill_fpregset (const struct regcache *regcache,
411 gdb_fpregset_t *fpregsetp, int regno)
412{
d4d793bf
AA
413 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
414 regno, (gdb_byte *) fpregsetp,
415 AARCH64_LINUX_SIZEOF_FPREGSET);
9d19df75
MS
416}
417
418/* Fill GDB's register array with the floating-point register values
419 in *FPREGSETP. */
420
421void
422supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
423{
d4d793bf
AA
424 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
425 (const gdb_byte *) fpregsetp,
426 AARCH64_LINUX_SIZEOF_FPREGSET);
9d19df75
MS
427}
428
d6c44983
YZ
429/* linux_nat_new_fork hook. */
430
431static void
432aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
433{
434 pid_t parent_pid;
435 struct aarch64_debug_reg_state *parent_state;
436 struct aarch64_debug_reg_state *child_state;
437
438 /* NULL means no watchpoint has ever been set in the parent. In
439 that case, there's nothing to do. */
440 if (parent->arch_private == NULL)
441 return;
442
443 /* GDB core assumes the child inherits the watchpoints/hw
444 breakpoints of the parent, and will remove them all from the
445 forked off process. Copy the debug registers mirrors into the
446 new process so that all breakpoints and watchpoints can be
447 removed together. */
448
449 parent_pid = ptid_get_pid (parent->ptid);
450 parent_state = aarch64_get_debug_reg_state (parent_pid);
451 child_state = aarch64_get_debug_reg_state (child_pid);
452 *child_state = *parent_state;
453}
9d19df75
MS
454\f
455
456/* Called by libthread_db. Returns a pointer to the thread local
457 storage (or its descriptor). */
458
459ps_err_e
460ps_get_thread_area (const struct ps_prochandle *ph,
461 lwpid_t lwpid, int idx, void **base)
462{
463 struct iovec iovec;
464 uint64_t reg;
465
466 iovec.iov_base = &reg;
467 iovec.iov_len = sizeof (reg);
468
469 if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
470 return PS_ERR;
471
472 /* IDX is the bias from the thread pointer to the beginning of the
473 thread descriptor. It has to be subtracted due to implementation
474 quirks in libthread_db. */
475 *base = (void *) (reg - idx);
476
477 return PS_OK;
478}
479\f
480
2e97a79e
TT
481static void (*super_post_startup_inferior) (struct target_ops *self,
482 ptid_t ptid);
9d19df75
MS
483
484/* Implement the "to_post_startup_inferior" target_ops method. */
485
486static void
2e97a79e
TT
487aarch64_linux_child_post_startup_inferior (struct target_ops *self,
488 ptid_t ptid)
9d19df75 489{
d6c44983 490 aarch64_forget_process (ptid_get_pid (ptid));
af1b22f3 491 aarch64_linux_get_debug_reg_capacity (ptid_get_pid (ptid));
2e97a79e 492 super_post_startup_inferior (self, ptid);
9d19df75
MS
493}
494
607685ec
YQ
495extern struct target_desc *tdesc_arm_with_vfpv3;
496extern struct target_desc *tdesc_arm_with_neon;
497
9d19df75
MS
498/* Implement the "to_read_description" target_ops method. */
499
500static const struct target_desc *
501aarch64_linux_read_description (struct target_ops *ops)
502{
607685ec
YQ
503 CORE_ADDR at_phent;
504
505 if (target_auxv_search (ops, AT_PHENT, &at_phent) == 1)
506 {
507 if (at_phent == sizeof (Elf64_External_Phdr))
508 return tdesc_aarch64;
509 else
510 {
511 CORE_ADDR arm_hwcap = 0;
512
513 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
514 return ops->beneath->to_read_description (ops->beneath);
515
516#ifndef COMPAT_HWCAP_VFP
517#define COMPAT_HWCAP_VFP (1 << 6)
518#endif
519#ifndef COMPAT_HWCAP_NEON
520#define COMPAT_HWCAP_NEON (1 << 12)
521#endif
522#ifndef COMPAT_HWCAP_VFPv3
523#define COMPAT_HWCAP_VFPv3 (1 << 13)
524#endif
525
526 if (arm_hwcap & COMPAT_HWCAP_VFP)
527 {
528 char *buf;
529 const struct target_desc *result = NULL;
530
531 if (arm_hwcap & COMPAT_HWCAP_NEON)
532 result = tdesc_arm_with_neon;
533 else if (arm_hwcap & COMPAT_HWCAP_VFPv3)
534 result = tdesc_arm_with_vfpv3;
535
536 return result;
537 }
538
539 return NULL;
540 }
541 }
542
9d19df75
MS
543 return tdesc_aarch64;
544}
545
ade90bde
YQ
546/* Convert a native/host siginfo object, into/from the siginfo in the
547 layout of the inferiors' architecture. Returns true if any
548 conversion was done; false otherwise. If DIRECTION is 1, then copy
549 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
550 INF. */
551
552static int
553aarch64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
554{
555 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
556
557 /* Is the inferior 32-bit? If so, then do fixup the siginfo
558 object. */
559 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
560 {
561 if (direction == 0)
562 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
563 native);
564 else
565 aarch64_siginfo_from_compat_siginfo (native,
566 (struct compat_siginfo *) inf);
567
568 return 1;
569 }
570
571 return 0;
572}
573
9d19df75
MS
574/* Returns the number of hardware watchpoints of type TYPE that we can
575 set. Value is positive if we can set CNT watchpoints, zero if
576 setting watchpoints of type TYPE is not supported, and negative if
577 CNT is more than the maximum number of watchpoints of type TYPE
578 that we can support. TYPE is one of bp_hardware_watchpoint,
579 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
580 CNT is the number of such watchpoints used so far (including this
581 one). OTHERTYPE is non-zero if other types of watchpoints are
c2fbdc59 582 currently enabled. */
9d19df75
MS
583
584static int
5461485a 585aarch64_linux_can_use_hw_breakpoint (struct target_ops *self,
f486487f
SM
586 enum bptype type,
587 int cnt, int othertype)
9d19df75 588{
c2fbdc59
YQ
589 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
590 || type == bp_access_watchpoint || type == bp_watchpoint)
591 {
592 if (aarch64_num_wp_regs == 0)
593 return 0;
594 }
595 else if (type == bp_hardware_breakpoint)
596 {
597 if (aarch64_num_bp_regs == 0)
598 return 0;
599 }
600 else
601 gdb_assert_not_reached ("unexpected breakpoint type");
602
603 /* We always return 1 here because we don't have enough information
604 about possible overlap of addresses that they want to watch. As an
605 extreme example, consider the case where all the watchpoints watch
606 the same address and the same region length: then we can handle a
607 virtually unlimited number of watchpoints, due to debug register
608 sharing implemented via reference counts. */
9d19df75
MS
609 return 1;
610}
611
0d5ed153 612/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
9d19df75
MS
613 Return 0 on success, -1 on failure. */
614
615static int
23a26771
TT
616aarch64_linux_insert_hw_breakpoint (struct target_ops *self,
617 struct gdbarch *gdbarch,
9d19df75
MS
618 struct bp_target_info *bp_tgt)
619{
620 int ret;
0d5ed153 621 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
9d19df75 622 const int len = 4;
2ecd81c2 623 const enum target_hw_bp_type type = hw_execute;
c67ca4de
YQ
624 struct aarch64_debug_reg_state *state
625 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
9d19df75 626
c5e92cca 627 if (show_debug_regs)
9d19df75
MS
628 fprintf_unfiltered
629 (gdb_stdlog,
630 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
631 (unsigned long) addr, len);
632
c67ca4de 633 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
9d19df75 634
c5e92cca 635 if (show_debug_regs)
d6c44983 636 {
d6c44983 637 aarch64_show_debug_reg_state (state,
2fd0f80d 638 "insert_hw_breakpoint", addr, len, type);
d6c44983 639 }
9d19df75
MS
640
641 return ret;
642}
643
644/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
645 Return 0 on success, -1 on failure. */
646
647static int
a64dc96c
TT
648aarch64_linux_remove_hw_breakpoint (struct target_ops *self,
649 struct gdbarch *gdbarch,
9d19df75
MS
650 struct bp_target_info *bp_tgt)
651{
652 int ret;
653 CORE_ADDR addr = bp_tgt->placed_address;
654 const int len = 4;
2ecd81c2 655 const enum target_hw_bp_type type = hw_execute;
c67ca4de
YQ
656 struct aarch64_debug_reg_state *state
657 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
9d19df75 658
c5e92cca 659 if (show_debug_regs)
9d19df75
MS
660 fprintf_unfiltered
661 (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
662 (unsigned long) addr, len);
663
c67ca4de 664 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
9d19df75 665
c5e92cca 666 if (show_debug_regs)
d6c44983 667 {
d6c44983
YZ
668 aarch64_show_debug_reg_state (state,
669 "remove_hw_watchpoint", addr, len, type);
670 }
9d19df75
MS
671
672 return ret;
673}
674
9d19df75
MS
675/* Implement the "to_insert_watchpoint" target_ops method.
676
677 Insert a watchpoint to watch a memory region which starts at
678 address ADDR and whose length is LEN bytes. Watch memory accesses
679 of the type TYPE. Return 0 on success, -1 on failure. */
680
681static int
7bb99c53 682aarch64_linux_insert_watchpoint (struct target_ops *self,
f486487f
SM
683 CORE_ADDR addr, int len,
684 enum target_hw_bp_type type,
9d19df75
MS
685 struct expression *cond)
686{
687 int ret;
c67ca4de
YQ
688 struct aarch64_debug_reg_state *state
689 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
9d19df75 690
c5e92cca 691 if (show_debug_regs)
9d19df75
MS
692 fprintf_unfiltered (gdb_stdlog,
693 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
694 (unsigned long) addr, len);
695
696 gdb_assert (type != hw_execute);
697
c67ca4de 698 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
9d19df75 699
c5e92cca 700 if (show_debug_regs)
d6c44983 701 {
d6c44983
YZ
702 aarch64_show_debug_reg_state (state,
703 "insert_watchpoint", addr, len, type);
704 }
9d19df75
MS
705
706 return ret;
707}
708
709/* Implement the "to_remove_watchpoint" target_ops method.
710 Remove a watchpoint that watched the memory region which starts at
711 address ADDR, whose length is LEN bytes, and for accesses of the
712 type TYPE. Return 0 on success, -1 on failure. */
713
714static int
11b5219a 715aarch64_linux_remove_watchpoint (struct target_ops *self,
f486487f
SM
716 CORE_ADDR addr, int len,
717 enum target_hw_bp_type type,
9d19df75
MS
718 struct expression *cond)
719{
720 int ret;
c67ca4de
YQ
721 struct aarch64_debug_reg_state *state
722 = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
9d19df75 723
c5e92cca 724 if (show_debug_regs)
9d19df75
MS
725 fprintf_unfiltered (gdb_stdlog,
726 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
727 (unsigned long) addr, len);
728
729 gdb_assert (type != hw_execute);
730
c67ca4de 731 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
9d19df75 732
c5e92cca 733 if (show_debug_regs)
d6c44983 734 {
d6c44983
YZ
735 aarch64_show_debug_reg_state (state,
736 "remove_watchpoint", addr, len, type);
737 }
9d19df75
MS
738
739 return ret;
740}
741
742/* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */
743
744static int
31568a15
TT
745aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
746 CORE_ADDR addr, int len)
9d19df75 747{
39edd165 748 return aarch64_linux_region_ok_for_watchpoint (addr, len);
9d19df75
MS
749}
750
751/* Implement the "to_stopped_data_address" target_ops method. */
752
753static int
754aarch64_linux_stopped_data_address (struct target_ops *target,
755 CORE_ADDR *addr_p)
756{
757 siginfo_t siginfo;
758 int i, tid;
759 struct aarch64_debug_reg_state *state;
760
761 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
762 return 0;
763
764 /* This must be a hardware breakpoint. */
765 if (siginfo.si_signo != SIGTRAP
766 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
767 return 0;
768
769 /* Check if the address matches any watched address. */
d6c44983 770 state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
9d19df75
MS
771 for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
772 {
773 const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
774 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
775 const CORE_ADDR addr_watch = state->dr_addr_wp[i];
776
777 if (state->dr_ref_count_wp[i]
778 && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
779 && addr_trap >= addr_watch
780 && addr_trap < addr_watch + len)
781 {
782 *addr_p = addr_trap;
783 return 1;
784 }
785 }
786
787 return 0;
788}
789
790/* Implement the "to_stopped_by_watchpoint" target_ops method. */
791
792static int
6a109b6b 793aarch64_linux_stopped_by_watchpoint (struct target_ops *ops)
9d19df75
MS
794{
795 CORE_ADDR addr;
796
6a109b6b 797 return aarch64_linux_stopped_data_address (ops, &addr);
9d19df75
MS
798}
799
800/* Implement the "to_watchpoint_addr_within_range" target_ops method. */
801
802static int
803aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
804 CORE_ADDR addr,
805 CORE_ADDR start, int length)
806{
807 return start <= addr && start + length - 1 >= addr;
808}
809
750ce8d1
YQ
810/* Implement the "to_can_do_single_step" target_ops method. */
811
812static int
813aarch64_linux_can_do_single_step (struct target_ops *target)
814{
815 return 1;
816}
817
9d19df75
MS
818/* Define AArch64 maintenance commands. */
819
820static void
821add_show_debug_regs_command (void)
822{
823 /* A maintenance command to enable printing the internal DRi mirror
824 variables. */
825 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
c5e92cca 826 &show_debug_regs, _("\
9d19df75
MS
827Set whether to show variables that mirror the AArch64 debug registers."), _("\
828Show whether to show variables that mirror the AArch64 debug registers."), _("\
829Use \"on\" to enable, \"off\" to disable.\n\
830If enabled, the debug registers values are shown when GDB inserts\n\
831or removes a hardware breakpoint or watchpoint, and when the inferior\n\
832triggers a breakpoint or watchpoint."),
833 NULL,
834 NULL,
835 &maintenance_set_cmdlist,
836 &maintenance_show_cmdlist);
837}
838
839/* -Wmissing-prototypes. */
840void _initialize_aarch64_linux_nat (void);
841
842void
843_initialize_aarch64_linux_nat (void)
844{
845 struct target_ops *t;
846
847 /* Fill in the generic GNU/Linux methods. */
848 t = linux_target ();
849
850 add_show_debug_regs_command ();
851
852 /* Add our register access methods. */
853 t->to_fetch_registers = aarch64_linux_fetch_inferior_registers;
854 t->to_store_registers = aarch64_linux_store_inferior_registers;
855
856 t->to_read_description = aarch64_linux_read_description;
857
858 t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint;
859 t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint;
860 t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint;
861 t->to_region_ok_for_hw_watchpoint =
862 aarch64_linux_region_ok_for_hw_watchpoint;
863 t->to_insert_watchpoint = aarch64_linux_insert_watchpoint;
864 t->to_remove_watchpoint = aarch64_linux_remove_watchpoint;
865 t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint;
866 t->to_stopped_data_address = aarch64_linux_stopped_data_address;
867 t->to_watchpoint_addr_within_range =
868 aarch64_linux_watchpoint_addr_within_range;
750ce8d1 869 t->to_can_do_single_step = aarch64_linux_can_do_single_step;
9d19df75
MS
870
871 /* Override the GNU/Linux inferior startup hook. */
872 super_post_startup_inferior = t->to_post_startup_inferior;
873 t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior;
874
875 /* Register the target. */
876 linux_nat_add_target (t);
877 linux_nat_set_new_thread (t, aarch64_linux_new_thread);
d6c44983
YZ
878 linux_nat_set_new_fork (t, aarch64_linux_new_fork);
879 linux_nat_set_forget_process (t, aarch64_forget_process);
9d19df75 880 linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume);
ade90bde
YQ
881
882 /* Add our siginfo layout converter. */
883 linux_nat_set_siginfo_fixup (t, aarch64_linux_siginfo_fixup);
9d19df75 884}
This page took 0.236461 seconds and 4 git commands to generate.