424e616b042339823e57ed317ab9b9820ae7efd2
[deliverable/binutils-gdb.git] / gdb / aarch64-linux-nat.c
1 /* Native-dependent code for GNU/Linux AArch64.
2
3 Copyright (C) 2011-2021 Free Software Foundation, Inc.
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"
32 #include "aarch32-linux-nat.h"
33 #include "aarch32-tdep.h"
34 #include "arch/arm.h"
35 #include "nat/aarch64-linux.h"
36 #include "nat/aarch64-linux-hw-point.h"
37 #include "nat/aarch64-sve-linux-ptrace.h"
38
39 #include "elf/external.h"
40 #include "elf/common.h"
41
42 #include "nat/gdb_ptrace.h"
43 #include <sys/utsname.h>
44 #include <asm/ptrace.h>
45
46 #include "gregset.h"
47 #include "linux-tdep.h"
48
49 /* Defines ps_err_e, struct ps_prochandle. */
50 #include "gdb_proc_service.h"
51 #include "arch-utils.h"
52
53 #include "arch/aarch64-mte-linux.h"
54
55 #ifndef TRAP_HWBKPT
56 #define TRAP_HWBKPT 0x0004
57 #endif
58
59 class aarch64_linux_nat_target final : public linux_nat_target
60 {
61 public:
62 /* Add our register access methods. */
63 void fetch_registers (struct regcache *, int) override;
64 void store_registers (struct regcache *, int) override;
65
66 const struct target_desc *read_description () override;
67
68 /* Add our hardware breakpoint and watchpoint implementation. */
69 int can_use_hw_breakpoint (enum bptype, int, int) override;
70 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
71 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
72 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
73 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
74 struct expression *) override;
75 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
76 struct expression *) override;
77 bool stopped_by_watchpoint () override;
78 bool stopped_data_address (CORE_ADDR *) override;
79 bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
80
81 int can_do_single_step () override;
82
83 /* Override the GNU/Linux inferior startup hook. */
84 void post_startup_inferior (ptid_t) override;
85
86 /* Override the GNU/Linux post attach hook. */
87 void post_attach (int pid) override;
88
89 /* These three defer to common nat/ code. */
90 void low_new_thread (struct lwp_info *lp) override
91 { aarch64_linux_new_thread (lp); }
92 void low_delete_thread (struct arch_lwp_info *lp) override
93 { aarch64_linux_delete_thread (lp); }
94 void low_prepare_to_resume (struct lwp_info *lp) override
95 { aarch64_linux_prepare_to_resume (lp); }
96
97 void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
98 void low_forget_process (pid_t pid) override;
99
100 /* Add our siginfo layout converter. */
101 bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
102 override;
103
104 struct gdbarch *thread_architecture (ptid_t) override;
105 };
106
107 static aarch64_linux_nat_target the_aarch64_linux_nat_target;
108
109 /* Per-process data. We don't bind this to a per-inferior registry
110 because of targets like x86 GNU/Linux that need to keep track of
111 processes that aren't bound to any inferior (e.g., fork children,
112 checkpoints). */
113
114 struct aarch64_process_info
115 {
116 /* Linked list. */
117 struct aarch64_process_info *next;
118
119 /* The process identifier. */
120 pid_t pid;
121
122 /* Copy of aarch64 hardware debug registers. */
123 struct aarch64_debug_reg_state state;
124 };
125
126 static struct aarch64_process_info *aarch64_process_list = NULL;
127
128 /* Find process data for process PID. */
129
130 static struct aarch64_process_info *
131 aarch64_find_process_pid (pid_t pid)
132 {
133 struct aarch64_process_info *proc;
134
135 for (proc = aarch64_process_list; proc; proc = proc->next)
136 if (proc->pid == pid)
137 return proc;
138
139 return NULL;
140 }
141
142 /* Add process data for process PID. Returns newly allocated info
143 object. */
144
145 static struct aarch64_process_info *
146 aarch64_add_process (pid_t pid)
147 {
148 struct aarch64_process_info *proc;
149
150 proc = XCNEW (struct aarch64_process_info);
151 proc->pid = pid;
152
153 proc->next = aarch64_process_list;
154 aarch64_process_list = proc;
155
156 return proc;
157 }
158
159 /* Get data specific info for process PID, creating it if necessary.
160 Never returns NULL. */
161
162 static struct aarch64_process_info *
163 aarch64_process_info_get (pid_t pid)
164 {
165 struct aarch64_process_info *proc;
166
167 proc = aarch64_find_process_pid (pid);
168 if (proc == NULL)
169 proc = aarch64_add_process (pid);
170
171 return proc;
172 }
173
174 /* Called whenever GDB is no longer debugging process PID. It deletes
175 data structures that keep track of debug register state. */
176
177 void
178 aarch64_linux_nat_target::low_forget_process (pid_t pid)
179 {
180 struct aarch64_process_info *proc, **proc_link;
181
182 proc = aarch64_process_list;
183 proc_link = &aarch64_process_list;
184
185 while (proc != NULL)
186 {
187 if (proc->pid == pid)
188 {
189 *proc_link = proc->next;
190
191 xfree (proc);
192 return;
193 }
194
195 proc_link = &proc->next;
196 proc = *proc_link;
197 }
198 }
199
200 /* Get debug registers state for process PID. */
201
202 struct aarch64_debug_reg_state *
203 aarch64_get_debug_reg_state (pid_t pid)
204 {
205 return &aarch64_process_info_get (pid)->state;
206 }
207
208 /* Fill GDB's register array with the general-purpose register values
209 from the current thread. */
210
211 static void
212 fetch_gregs_from_thread (struct regcache *regcache)
213 {
214 int ret, tid;
215 struct gdbarch *gdbarch = regcache->arch ();
216 elf_gregset_t regs;
217 struct iovec iovec;
218
219 /* Make sure REGS can hold all registers contents on both aarch64
220 and arm. */
221 gdb_static_assert (sizeof (regs) >= 18 * 4);
222
223 tid = regcache->ptid ().lwp ();
224
225 iovec.iov_base = &regs;
226 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
227 iovec.iov_len = 18 * 4;
228 else
229 iovec.iov_len = sizeof (regs);
230
231 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
232 if (ret < 0)
233 perror_with_name (_("Unable to fetch general registers."));
234
235 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
236 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1);
237 else
238 {
239 int regno;
240
241 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
242 regcache->raw_supply (regno, &regs[regno - AARCH64_X0_REGNUM]);
243 }
244 }
245
246 /* Store to the current thread the valid general-purpose register
247 values in the GDB's register array. */
248
249 static void
250 store_gregs_to_thread (const struct regcache *regcache)
251 {
252 int ret, tid;
253 elf_gregset_t regs;
254 struct iovec iovec;
255 struct gdbarch *gdbarch = regcache->arch ();
256
257 /* Make sure REGS can hold all registers contents on both aarch64
258 and arm. */
259 gdb_static_assert (sizeof (regs) >= 18 * 4);
260 tid = regcache->ptid ().lwp ();
261
262 iovec.iov_base = &regs;
263 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
264 iovec.iov_len = 18 * 4;
265 else
266 iovec.iov_len = sizeof (regs);
267
268 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
269 if (ret < 0)
270 perror_with_name (_("Unable to fetch general registers."));
271
272 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
273 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1);
274 else
275 {
276 int regno;
277
278 for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
279 if (REG_VALID == regcache->get_register_status (regno))
280 regcache->raw_collect (regno, &regs[regno - AARCH64_X0_REGNUM]);
281 }
282
283 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
284 if (ret < 0)
285 perror_with_name (_("Unable to store general registers."));
286 }
287
288 /* Fill GDB's register array with the fp/simd register values
289 from the current thread. */
290
291 static void
292 fetch_fpregs_from_thread (struct regcache *regcache)
293 {
294 int ret, tid;
295 elf_fpregset_t regs;
296 struct iovec iovec;
297 struct gdbarch *gdbarch = regcache->arch ();
298
299 /* Make sure REGS can hold all VFP registers contents on both aarch64
300 and arm. */
301 gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
302
303 tid = regcache->ptid ().lwp ();
304
305 iovec.iov_base = &regs;
306
307 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
308 {
309 iovec.iov_len = ARM_VFP3_REGS_SIZE;
310
311 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
312 if (ret < 0)
313 perror_with_name (_("Unable to fetch VFP registers."));
314
315 aarch32_vfp_regcache_supply (regcache, (gdb_byte *) &regs, 32);
316 }
317 else
318 {
319 int regno;
320
321 iovec.iov_len = sizeof (regs);
322
323 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
324 if (ret < 0)
325 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
326
327 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
328 regcache->raw_supply (regno, &regs.vregs[regno - AARCH64_V0_REGNUM]);
329
330 regcache->raw_supply (AARCH64_FPSR_REGNUM, &regs.fpsr);
331 regcache->raw_supply (AARCH64_FPCR_REGNUM, &regs.fpcr);
332 }
333 }
334
335 /* Store to the current thread the valid fp/simd register
336 values in the GDB's register array. */
337
338 static void
339 store_fpregs_to_thread (const struct regcache *regcache)
340 {
341 int ret, tid;
342 elf_fpregset_t regs;
343 struct iovec iovec;
344 struct gdbarch *gdbarch = regcache->arch ();
345
346 /* Make sure REGS can hold all VFP registers contents on both aarch64
347 and arm. */
348 gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
349 tid = regcache->ptid ().lwp ();
350
351 iovec.iov_base = &regs;
352
353 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
354 {
355 iovec.iov_len = ARM_VFP3_REGS_SIZE;
356
357 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
358 if (ret < 0)
359 perror_with_name (_("Unable to fetch VFP registers."));
360
361 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
362 }
363 else
364 {
365 int regno;
366
367 iovec.iov_len = sizeof (regs);
368
369 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
370 if (ret < 0)
371 perror_with_name (_("Unable to fetch FP/SIMD registers."));
372
373 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
374 if (REG_VALID == regcache->get_register_status (regno))
375 regcache->raw_collect
376 (regno, (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
377
378 if (REG_VALID == regcache->get_register_status (AARCH64_FPSR_REGNUM))
379 regcache->raw_collect (AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
380 if (REG_VALID == regcache->get_register_status (AARCH64_FPCR_REGNUM))
381 regcache->raw_collect (AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
382 }
383
384 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
385 {
386 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec);
387 if (ret < 0)
388 perror_with_name (_("Unable to store VFP registers."));
389 }
390 else
391 {
392 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
393 if (ret < 0)
394 perror_with_name (_("Unable to store FP/SIMD registers."));
395 }
396 }
397
398 /* Fill GDB's register array with the sve register values
399 from the current thread. */
400
401 static void
402 fetch_sveregs_from_thread (struct regcache *regcache)
403 {
404 std::unique_ptr<gdb_byte[]> base
405 = aarch64_sve_get_sveregs (regcache->ptid ().lwp ());
406 aarch64_sve_regs_copy_to_reg_buf (regcache, base.get ());
407 }
408
409 /* Store to the current thread the valid sve register
410 values in the GDB's register array. */
411
412 static void
413 store_sveregs_to_thread (struct regcache *regcache)
414 {
415 int ret;
416 struct iovec iovec;
417 int tid = regcache->ptid ().lwp ();
418
419 /* First store vector length to the thread. This is done first to ensure the
420 ptrace buffers read from the kernel are the correct size. */
421 if (!aarch64_sve_set_vq (tid, regcache))
422 perror_with_name (_("Unable to set VG register."));
423
424 /* Obtain a dump of SVE registers from ptrace. */
425 std::unique_ptr<gdb_byte[]> base = aarch64_sve_get_sveregs (tid);
426
427 /* Overwrite with regcache state. */
428 aarch64_sve_regs_copy_from_reg_buf (regcache, base.get ());
429
430 /* Write back to the kernel. */
431 iovec.iov_base = base.get ();
432 iovec.iov_len = ((struct user_sve_header *) base.get ())->size;
433 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_SVE, &iovec);
434
435 if (ret < 0)
436 perror_with_name (_("Unable to store sve registers"));
437 }
438
439 /* Fill GDB's register array with the pointer authentication mask values from
440 the current thread. */
441
442 static void
443 fetch_pauth_masks_from_thread (struct regcache *regcache)
444 {
445 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
446 int ret;
447 struct iovec iovec;
448 uint64_t pauth_regset[2] = {0, 0};
449 int tid = regcache->ptid ().lwp ();
450
451 iovec.iov_base = &pauth_regset;
452 iovec.iov_len = sizeof (pauth_regset);
453
454 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_PAC_MASK, &iovec);
455 if (ret != 0)
456 perror_with_name (_("unable to fetch pauth registers."));
457
458 regcache->raw_supply (AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base),
459 &pauth_regset[0]);
460 regcache->raw_supply (AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base),
461 &pauth_regset[1]);
462 }
463
464 /* Implement the "fetch_registers" target_ops method. */
465
466 void
467 aarch64_linux_nat_target::fetch_registers (struct regcache *regcache,
468 int regno)
469 {
470 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
471
472 if (regno == -1)
473 {
474 fetch_gregs_from_thread (regcache);
475 if (tdep->has_sve ())
476 fetch_sveregs_from_thread (regcache);
477 else
478 fetch_fpregs_from_thread (regcache);
479
480 if (tdep->has_pauth ())
481 fetch_pauth_masks_from_thread (regcache);
482 }
483 else if (regno < AARCH64_V0_REGNUM)
484 fetch_gregs_from_thread (regcache);
485 else if (tdep->has_sve ())
486 fetch_sveregs_from_thread (regcache);
487 else
488 fetch_fpregs_from_thread (regcache);
489
490 if (tdep->has_pauth ())
491 {
492 if (regno == AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base)
493 || regno == AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base))
494 fetch_pauth_masks_from_thread (regcache);
495 }
496 }
497
498 /* Implement the "store_registers" target_ops method. */
499
500 void
501 aarch64_linux_nat_target::store_registers (struct regcache *regcache,
502 int regno)
503 {
504 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
505
506 if (regno == -1)
507 {
508 store_gregs_to_thread (regcache);
509 if (tdep->has_sve ())
510 store_sveregs_to_thread (regcache);
511 else
512 store_fpregs_to_thread (regcache);
513 }
514 else if (regno < AARCH64_V0_REGNUM)
515 store_gregs_to_thread (regcache);
516 else if (tdep->has_sve ())
517 store_sveregs_to_thread (regcache);
518 else
519 store_fpregs_to_thread (regcache);
520 }
521
522 /* Fill register REGNO (if it is a general-purpose register) in
523 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
524 do this for all registers. */
525
526 void
527 fill_gregset (const struct regcache *regcache,
528 gdb_gregset_t *gregsetp, int regno)
529 {
530 regcache_collect_regset (&aarch64_linux_gregset, regcache,
531 regno, (gdb_byte *) gregsetp,
532 AARCH64_LINUX_SIZEOF_GREGSET);
533 }
534
535 /* Fill GDB's register array with the general-purpose register values
536 in *GREGSETP. */
537
538 void
539 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
540 {
541 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
542 (const gdb_byte *) gregsetp,
543 AARCH64_LINUX_SIZEOF_GREGSET);
544 }
545
546 /* Fill register REGNO (if it is a floating-point register) in
547 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
548 do this for all registers. */
549
550 void
551 fill_fpregset (const struct regcache *regcache,
552 gdb_fpregset_t *fpregsetp, int regno)
553 {
554 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
555 regno, (gdb_byte *) fpregsetp,
556 AARCH64_LINUX_SIZEOF_FPREGSET);
557 }
558
559 /* Fill GDB's register array with the floating-point register values
560 in *FPREGSETP. */
561
562 void
563 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
564 {
565 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
566 (const gdb_byte *) fpregsetp,
567 AARCH64_LINUX_SIZEOF_FPREGSET);
568 }
569
570 /* linux_nat_new_fork hook. */
571
572 void
573 aarch64_linux_nat_target::low_new_fork (struct lwp_info *parent,
574 pid_t child_pid)
575 {
576 pid_t parent_pid;
577 struct aarch64_debug_reg_state *parent_state;
578 struct aarch64_debug_reg_state *child_state;
579
580 /* NULL means no watchpoint has ever been set in the parent. In
581 that case, there's nothing to do. */
582 if (parent->arch_private == NULL)
583 return;
584
585 /* GDB core assumes the child inherits the watchpoints/hw
586 breakpoints of the parent, and will remove them all from the
587 forked off process. Copy the debug registers mirrors into the
588 new process so that all breakpoints and watchpoints can be
589 removed together. */
590
591 parent_pid = parent->ptid.pid ();
592 parent_state = aarch64_get_debug_reg_state (parent_pid);
593 child_state = aarch64_get_debug_reg_state (child_pid);
594 *child_state = *parent_state;
595 }
596 \f
597
598 /* Called by libthread_db. Returns a pointer to the thread local
599 storage (or its descriptor). */
600
601 ps_err_e
602 ps_get_thread_area (struct ps_prochandle *ph,
603 lwpid_t lwpid, int idx, void **base)
604 {
605 int is_64bit_p
606 = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 64);
607
608 return aarch64_ps_get_thread_area (ph, lwpid, idx, base, is_64bit_p);
609 }
610 \f
611
612 /* Implement the "post_startup_inferior" target_ops method. */
613
614 void
615 aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid)
616 {
617 low_forget_process (ptid.pid ());
618 aarch64_linux_get_debug_reg_capacity (ptid.pid ());
619 linux_nat_target::post_startup_inferior (ptid);
620 }
621
622 /* Implement the "post_attach" target_ops method. */
623
624 void
625 aarch64_linux_nat_target::post_attach (int pid)
626 {
627 low_forget_process (pid);
628 /* Set the hardware debug register capacity. If
629 aarch64_linux_get_debug_reg_capacity is not called
630 (as it is in aarch64_linux_child_post_startup_inferior) then
631 software watchpoints will be used instead of hardware
632 watchpoints when attaching to a target. */
633 aarch64_linux_get_debug_reg_capacity (pid);
634 linux_nat_target::post_attach (pid);
635 }
636
637 /* Implement the "read_description" target_ops method. */
638
639 const struct target_desc *
640 aarch64_linux_nat_target::read_description ()
641 {
642 int ret, tid;
643 gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
644 struct iovec iovec;
645
646 tid = inferior_ptid.lwp ();
647
648 iovec.iov_base = regbuf;
649 iovec.iov_len = ARM_VFP3_REGS_SIZE;
650
651 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
652 if (ret == 0)
653 return aarch32_read_description ();
654
655 CORE_ADDR hwcap = linux_get_hwcap (this);
656
657 return aarch64_read_description (aarch64_sve_get_vq (tid),
658 hwcap & AARCH64_HWCAP_PACA);
659 }
660
661 /* Convert a native/host siginfo object, into/from the siginfo in the
662 layout of the inferiors' architecture. Returns true if any
663 conversion was done; false otherwise. If DIRECTION is 1, then copy
664 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
665 INF. */
666
667 bool
668 aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
669 int direction)
670 {
671 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
672
673 /* Is the inferior 32-bit? If so, then do fixup the siginfo
674 object. */
675 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
676 {
677 if (direction == 0)
678 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
679 native);
680 else
681 aarch64_siginfo_from_compat_siginfo (native,
682 (struct compat_siginfo *) inf);
683
684 return true;
685 }
686
687 return false;
688 }
689
690 /* Returns the number of hardware watchpoints of type TYPE that we can
691 set. Value is positive if we can set CNT watchpoints, zero if
692 setting watchpoints of type TYPE is not supported, and negative if
693 CNT is more than the maximum number of watchpoints of type TYPE
694 that we can support. TYPE is one of bp_hardware_watchpoint,
695 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
696 CNT is the number of such watchpoints used so far (including this
697 one). OTHERTYPE is non-zero if other types of watchpoints are
698 currently enabled. */
699
700 int
701 aarch64_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
702 int cnt, int othertype)
703 {
704 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
705 || type == bp_access_watchpoint || type == bp_watchpoint)
706 {
707 if (aarch64_num_wp_regs == 0)
708 return 0;
709 }
710 else if (type == bp_hardware_breakpoint)
711 {
712 if (aarch64_num_bp_regs == 0)
713 return 0;
714 }
715 else
716 gdb_assert_not_reached ("unexpected breakpoint type");
717
718 /* We always return 1 here because we don't have enough information
719 about possible overlap of addresses that they want to watch. As an
720 extreme example, consider the case where all the watchpoints watch
721 the same address and the same region length: then we can handle a
722 virtually unlimited number of watchpoints, due to debug register
723 sharing implemented via reference counts. */
724 return 1;
725 }
726
727 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
728 Return 0 on success, -1 on failure. */
729
730 int
731 aarch64_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
732 struct bp_target_info *bp_tgt)
733 {
734 int ret;
735 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
736 int len;
737 const enum target_hw_bp_type type = hw_execute;
738 struct aarch64_debug_reg_state *state
739 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
740
741 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
742
743 if (show_debug_regs)
744 fprintf_unfiltered
745 (gdb_stdlog,
746 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
747 (unsigned long) addr, len);
748
749 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
750
751 if (show_debug_regs)
752 {
753 aarch64_show_debug_reg_state (state,
754 "insert_hw_breakpoint", addr, len, type);
755 }
756
757 return ret;
758 }
759
760 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
761 Return 0 on success, -1 on failure. */
762
763 int
764 aarch64_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
765 struct bp_target_info *bp_tgt)
766 {
767 int ret;
768 CORE_ADDR addr = bp_tgt->placed_address;
769 int len = 4;
770 const enum target_hw_bp_type type = hw_execute;
771 struct aarch64_debug_reg_state *state
772 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
773
774 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
775
776 if (show_debug_regs)
777 fprintf_unfiltered
778 (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
779 (unsigned long) addr, len);
780
781 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
782
783 if (show_debug_regs)
784 {
785 aarch64_show_debug_reg_state (state,
786 "remove_hw_watchpoint", addr, len, type);
787 }
788
789 return ret;
790 }
791
792 /* Implement the "insert_watchpoint" target_ops method.
793
794 Insert a watchpoint to watch a memory region which starts at
795 address ADDR and whose length is LEN bytes. Watch memory accesses
796 of the type TYPE. Return 0 on success, -1 on failure. */
797
798 int
799 aarch64_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
800 enum target_hw_bp_type type,
801 struct expression *cond)
802 {
803 int ret;
804 struct aarch64_debug_reg_state *state
805 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
806
807 if (show_debug_regs)
808 fprintf_unfiltered (gdb_stdlog,
809 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
810 (unsigned long) addr, len);
811
812 gdb_assert (type != hw_execute);
813
814 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
815
816 if (show_debug_regs)
817 {
818 aarch64_show_debug_reg_state (state,
819 "insert_watchpoint", addr, len, type);
820 }
821
822 return ret;
823 }
824
825 /* Implement the "remove_watchpoint" target_ops method.
826 Remove a watchpoint that watched the memory region which starts at
827 address ADDR, whose length is LEN bytes, and for accesses of the
828 type TYPE. Return 0 on success, -1 on failure. */
829
830 int
831 aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
832 enum target_hw_bp_type type,
833 struct expression *cond)
834 {
835 int ret;
836 struct aarch64_debug_reg_state *state
837 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
838
839 if (show_debug_regs)
840 fprintf_unfiltered (gdb_stdlog,
841 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
842 (unsigned long) addr, len);
843
844 gdb_assert (type != hw_execute);
845
846 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
847
848 if (show_debug_regs)
849 {
850 aarch64_show_debug_reg_state (state,
851 "remove_watchpoint", addr, len, type);
852 }
853
854 return ret;
855 }
856
857 /* Implement the "region_ok_for_hw_watchpoint" target_ops method. */
858
859 int
860 aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
861 {
862 return aarch64_linux_region_ok_for_watchpoint (addr, len);
863 }
864
865 /* Implement the "stopped_data_address" target_ops method. */
866
867 bool
868 aarch64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
869 {
870 siginfo_t siginfo;
871 int i;
872 struct aarch64_debug_reg_state *state;
873
874 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
875 return false;
876
877 /* This must be a hardware breakpoint. */
878 if (siginfo.si_signo != SIGTRAP
879 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
880 return false;
881
882 /* Make sure to ignore the top byte, otherwise we may not recognize a
883 hardware watchpoint hit. The stopped data addresses coming from the
884 kernel can potentially be tagged addresses. */
885 struct gdbarch *gdbarch = thread_architecture (inferior_ptid);
886 const CORE_ADDR addr_trap
887 = address_significant (gdbarch, (CORE_ADDR) siginfo.si_addr);
888
889 /* Check if the address matches any watched address. */
890 state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
891 for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
892 {
893 const unsigned int offset
894 = aarch64_watchpoint_offset (state->dr_ctrl_wp[i]);
895 const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
896 const CORE_ADDR addr_watch = state->dr_addr_wp[i] + offset;
897 const CORE_ADDR addr_watch_aligned = align_down (state->dr_addr_wp[i], 8);
898 const CORE_ADDR addr_orig = state->dr_addr_orig_wp[i];
899
900 if (state->dr_ref_count_wp[i]
901 && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
902 && addr_trap >= addr_watch_aligned
903 && addr_trap < addr_watch + len)
904 {
905 /* ADDR_TRAP reports the first address of the memory range
906 accessed by the CPU, regardless of what was the memory
907 range watched. Thus, a large CPU access that straddles
908 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
909 ADDR_TRAP that is lower than the
910 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
911
912 addr: | 4 | 5 | 6 | 7 | 8 |
913 |---- range watched ----|
914 |----------- range accessed ------------|
915
916 In this case, ADDR_TRAP will be 4.
917
918 To match a watchpoint known to GDB core, we must never
919 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
920 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
921 positive on kernels older than 4.10. See PR
922 external/20207. */
923 *addr_p = addr_orig;
924 return true;
925 }
926 }
927
928 return false;
929 }
930
931 /* Implement the "stopped_by_watchpoint" target_ops method. */
932
933 bool
934 aarch64_linux_nat_target::stopped_by_watchpoint ()
935 {
936 CORE_ADDR addr;
937
938 return stopped_data_address (&addr);
939 }
940
941 /* Implement the "watchpoint_addr_within_range" target_ops method. */
942
943 bool
944 aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
945 CORE_ADDR start, int length)
946 {
947 return start <= addr && start + length - 1 >= addr;
948 }
949
950 /* Implement the "can_do_single_step" target_ops method. */
951
952 int
953 aarch64_linux_nat_target::can_do_single_step ()
954 {
955 return 1;
956 }
957
958 /* Implement the "thread_architecture" target_ops method. */
959
960 struct gdbarch *
961 aarch64_linux_nat_target::thread_architecture (ptid_t ptid)
962 {
963 /* Return the gdbarch for the current thread. If the vector length has
964 changed since the last time this was called, then do a further lookup. */
965
966 uint64_t vq = aarch64_sve_get_vq (ptid.lwp ());
967
968 /* Find the current gdbarch the same way as process_stratum_target. Only
969 return it if the current vector length matches the one in the tdep. */
970 inferior *inf = find_inferior_ptid (this, ptid);
971 gdb_assert (inf != NULL);
972 if (vq == gdbarch_tdep (inf->gdbarch)->vq)
973 return inf->gdbarch;
974
975 /* We reach here if the vector length for the thread is different from its
976 value at process start. Lookup gdbarch via info (potentially creating a
977 new one), stashing the vector length inside id. Use -1 for when SVE
978 unavailable, to distinguish from an unset value of 0. */
979 struct gdbarch_info info;
980 gdbarch_info_init (&info);
981 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_aarch64, bfd_mach_aarch64);
982 info.id = (int *) (vq == 0 ? -1 : vq);
983 return gdbarch_find_by_info (info);
984 }
985
986 /* Define AArch64 maintenance commands. */
987
988 static void
989 add_show_debug_regs_command (void)
990 {
991 /* A maintenance command to enable printing the internal DRi mirror
992 variables. */
993 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
994 &show_debug_regs, _("\
995 Set whether to show variables that mirror the AArch64 debug registers."), _("\
996 Show whether to show variables that mirror the AArch64 debug registers."), _("\
997 Use \"on\" to enable, \"off\" to disable.\n\
998 If enabled, the debug registers values are shown when GDB inserts\n\
999 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
1000 triggers a breakpoint or watchpoint."),
1001 NULL,
1002 NULL,
1003 &maintenance_set_cmdlist,
1004 &maintenance_show_cmdlist);
1005 }
1006
1007 void _initialize_aarch64_linux_nat ();
1008 void
1009 _initialize_aarch64_linux_nat ()
1010 {
1011 add_show_debug_regs_command ();
1012
1013 /* Register the target. */
1014 linux_target = &the_aarch64_linux_nat_target;
1015 add_inf_child_target (&the_aarch64_linux_nat_target);
1016 }
This page took 0.053972 seconds and 4 git commands to generate.