]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/aarch64-linux-nat.c
Add target description/feature for MTE registers
[thirdparty/binutils-gdb.git] / gdb / aarch64-linux-nat.c
CommitLineData
9d19df75
MS
1/* Native-dependent code for GNU/Linux AArch64.
2
b811d2c2 3 Copyright (C) 2011-2020 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
d55e5aa6 23#include "inferior.h"
4de283e4
TT
24#include "gdbcore.h"
25#include "regcache.h"
d55e5aa6 26#include "linux-nat.h"
4de283e4
TT
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"
d105cce5 33#include "aarch32-tdep.h"
350fab54 34#include "arch/arm.h"
d55e5aa6 35#include "nat/aarch64-linux.h"
4de283e4 36#include "nat/aarch64-linux-hw-point.h"
ba2d2bb2 37#include "nat/aarch64-sve-linux-ptrace.h"
4de283e4
TT
38
39#include "elf/external.h"
40#include "elf/common.h"
41
5826e159 42#include "nat/gdb_ptrace.h"
4de283e4
TT
43#include <sys/utsname.h>
44#include <asm/ptrace.h>
45
46#include "gregset.h"
47#include "linux-tdep.h"
9d19df75 48
9d19df75
MS
49/* Defines ps_err_e, struct ps_prochandle. */
50#include "gdb_proc_service.h"
4da037ef 51#include "arch-utils.h"
9d19df75 52
48d2ae75
LM
53#include "arch/aarch64-linux.h"
54
9d19df75
MS
55#ifndef TRAP_HWBKPT
56#define TRAP_HWBKPT 0x0004
57#endif
58
f6ac5f3d
PA
59class aarch64_linux_nat_target final : public linux_nat_target
60{
61public:
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;
57810aa7
PA
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;
f6ac5f3d
PA
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;
135340af 85
8363f9d5
RB
86 /* Override the GNU/Linux post attach hook. */
87 void post_attach (int pid) override;
88
135340af
PA
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;
4da037ef
AH
103
104 struct gdbarch *thread_architecture (ptid_t) override;
f6ac5f3d
PA
105};
106
107static aarch64_linux_nat_target the_aarch64_linux_nat_target;
108
d6c44983
YZ
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). */
9d19df75 113
d6c44983 114struct aarch64_process_info
9d19df75 115{
d6c44983
YZ
116 /* Linked list. */
117 struct aarch64_process_info *next;
9d19df75 118
d6c44983
YZ
119 /* The process identifier. */
120 pid_t pid;
9d19df75 121
d6c44983
YZ
122 /* Copy of aarch64 hardware debug registers. */
123 struct aarch64_debug_reg_state state;
124};
125
126static struct aarch64_process_info *aarch64_process_list = NULL;
127
128/* Find process data for process PID. */
129
130static struct aarch64_process_info *
131aarch64_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;
9d19df75
MS
140}
141
d6c44983
YZ
142/* Add process data for process PID. Returns newly allocated info
143 object. */
9d19df75 144
d6c44983
YZ
145static struct aarch64_process_info *
146aarch64_add_process (pid_t pid)
9d19df75 147{
d6c44983 148 struct aarch64_process_info *proc;
9d19df75 149
8d749320 150 proc = XCNEW (struct aarch64_process_info);
d6c44983 151 proc->pid = pid;
9d19df75 152
d6c44983
YZ
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
162static struct aarch64_process_info *
163aarch64_process_info_get (pid_t pid)
9d19df75 164{
d6c44983
YZ
165 struct aarch64_process_info *proc;
166
167 proc = aarch64_find_process_pid (pid);
168 if (proc == NULL)
169 proc = aarch64_add_process (pid);
9d19df75 170
d6c44983 171 return proc;
9d19df75
MS
172}
173
d6c44983
YZ
174/* Called whenever GDB is no longer debugging process PID. It deletes
175 data structures that keep track of debug register state. */
9d19df75 176
135340af
PA
177void
178aarch64_linux_nat_target::low_forget_process (pid_t pid)
9d19df75 179{
d6c44983 180 struct aarch64_process_info *proc, **proc_link;
9d19df75 181
d6c44983
YZ
182 proc = aarch64_process_list;
183 proc_link = &aarch64_process_list;
184
185 while (proc != NULL)
9d19df75 186 {
d6c44983
YZ
187 if (proc->pid == pid)
188 {
189 *proc_link = proc->next;
9d19df75 190
d6c44983
YZ
191 xfree (proc);
192 return;
193 }
194
195 proc_link = &proc->next;
196 proc = *proc_link;
197 }
9d19df75
MS
198}
199
d6c44983 200/* Get debug registers state for process PID. */
9d19df75 201
db3cb7cb 202struct aarch64_debug_reg_state *
d6c44983 203aarch64_get_debug_reg_state (pid_t pid)
9d19df75 204{
d6c44983 205 return &aarch64_process_info_get (pid)->state;
9d19df75
MS
206}
207
9d19df75
MS
208/* Fill GDB's register array with the general-purpose register values
209 from the current thread. */
210
211static void
212fetch_gregs_from_thread (struct regcache *regcache)
213{
607685ec 214 int ret, tid;
ac7936df 215 struct gdbarch *gdbarch = regcache->arch ();
9d19df75
MS
216 elf_gregset_t regs;
217 struct iovec iovec;
218
607685ec
YQ
219 /* Make sure REGS can hold all registers contents on both aarch64
220 and arm. */
221 gdb_static_assert (sizeof (regs) >= 18 * 4);
222
e38504b3 223 tid = regcache->ptid ().lwp ();
9d19df75
MS
224
225 iovec.iov_base = &regs;
607685ec
YQ
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);
9d19df75
MS
230
231 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
232 if (ret < 0)
233 perror_with_name (_("Unable to fetch general registers."));
234
607685ec
YQ
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++)
73e1c03f 242 regcache->raw_supply (regno, &regs[regno - AARCH64_X0_REGNUM]);
607685ec 243 }
9d19df75
MS
244}
245
246/* Store to the current thread the valid general-purpose register
247 values in the GDB's register array. */
248
249static void
250store_gregs_to_thread (const struct regcache *regcache)
251{
607685ec 252 int ret, tid;
9d19df75
MS
253 elf_gregset_t regs;
254 struct iovec iovec;
ac7936df 255 struct gdbarch *gdbarch = regcache->arch ();
9d19df75 256
607685ec
YQ
257 /* Make sure REGS can hold all registers contents on both aarch64
258 and arm. */
259 gdb_static_assert (sizeof (regs) >= 18 * 4);
e38504b3 260 tid = regcache->ptid ().lwp ();
9d19df75
MS
261
262 iovec.iov_base = &regs;
607685ec
YQ
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);
9d19df75
MS
267
268 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
269 if (ret < 0)
270 perror_with_name (_("Unable to fetch general registers."));
271
607685ec
YQ
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++)
0ec9f114 279 if (REG_VALID == regcache->get_register_status (regno))
34a79281 280 regcache->raw_collect (regno, &regs[regno - AARCH64_X0_REGNUM]);
607685ec 281 }
9d19df75
MS
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
291static void
292fetch_fpregs_from_thread (struct regcache *regcache)
293{
607685ec 294 int ret, tid;
9d19df75
MS
295 elf_fpregset_t regs;
296 struct iovec iovec;
ac7936df 297 struct gdbarch *gdbarch = regcache->arch ();
607685ec
YQ
298
299 /* Make sure REGS can hold all VFP registers contents on both aarch64
300 and arm. */
350fab54 301 gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
9d19df75 302
e38504b3 303 tid = regcache->ptid ().lwp ();
9d19df75
MS
304
305 iovec.iov_base = &regs;
9d19df75 306
607685ec
YQ
307 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
308 {
350fab54 309 iovec.iov_len = ARM_VFP3_REGS_SIZE;
607685ec
YQ
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);
9d19df75 322
607685ec
YQ
323 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
324 if (ret < 0)
325 perror_with_name (_("Unable to fetch vFP/SIMD registers."));
9d19df75 326
607685ec 327 for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
73e1c03f 328 regcache->raw_supply (regno, &regs.vregs[regno - AARCH64_V0_REGNUM]);
607685ec 329
73e1c03f
SM
330 regcache->raw_supply (AARCH64_FPSR_REGNUM, &regs.fpsr);
331 regcache->raw_supply (AARCH64_FPCR_REGNUM, &regs.fpcr);
607685ec 332 }
9d19df75
MS
333}
334
335/* Store to the current thread the valid fp/simd register
336 values in the GDB's register array. */
337
338static void
339store_fpregs_to_thread (const struct regcache *regcache)
340{
607685ec 341 int ret, tid;
9d19df75
MS
342 elf_fpregset_t regs;
343 struct iovec iovec;
ac7936df 344 struct gdbarch *gdbarch = regcache->arch ();
9d19df75 345
607685ec
YQ
346 /* Make sure REGS can hold all VFP registers contents on both aarch64
347 and arm. */
350fab54 348 gdb_static_assert (sizeof regs >= ARM_VFP3_REGS_SIZE);
e38504b3 349 tid = regcache->ptid ().lwp ();
9d19df75
MS
350
351 iovec.iov_base = &regs;
9d19df75 352
607685ec
YQ
353 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
354 {
350fab54 355 iovec.iov_len = ARM_VFP3_REGS_SIZE;
9d19df75 356
607685ec
YQ
357 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
358 if (ret < 0)
359 perror_with_name (_("Unable to fetch VFP registers."));
9d19df75 360
607685ec
YQ
361 aarch32_vfp_regcache_collect (regcache, (gdb_byte *) &regs, 32);
362 }
363 else
364 {
365 int regno;
9d19df75 366
607685ec
YQ
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++)
0ec9f114 374 if (REG_VALID == regcache->get_register_status (regno))
34a79281
SM
375 regcache->raw_collect
376 (regno, (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
607685ec 377
0ec9f114 378 if (REG_VALID == regcache->get_register_status (AARCH64_FPSR_REGNUM))
34a79281 379 regcache->raw_collect (AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
0ec9f114 380 if (REG_VALID == regcache->get_register_status (AARCH64_FPCR_REGNUM))
34a79281 381 regcache->raw_collect (AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
607685ec
YQ
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 }
9d19df75
MS
396}
397
e9902bfc
AH
398/* Fill GDB's register array with the sve register values
399 from the current thread. */
400
401static void
402fetch_sveregs_from_thread (struct regcache *regcache)
403{
404 std::unique_ptr<gdb_byte[]> base
e38504b3 405 = aarch64_sve_get_sveregs (regcache->ptid ().lwp ());
e9902bfc
AH
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
412static void
413store_sveregs_to_thread (struct regcache *regcache)
414{
415 int ret;
416 struct iovec iovec;
e38504b3 417 int tid = regcache->ptid ().lwp ();
e9902bfc 418
48574d91
AH
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
e9902bfc
AH
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
76bed0fd
AH
439/* Fill GDB's register array with the pointer authentication mask values from
440 the current thread. */
441
442static void
443fetch_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
f6ac5f3d 464/* Implement the "fetch_registers" target_ops method. */
9d19df75 465
f6ac5f3d
PA
466void
467aarch64_linux_nat_target::fetch_registers (struct regcache *regcache,
468 int regno)
9d19df75 469{
e9902bfc
AH
470 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
471
9d19df75
MS
472 if (regno == -1)
473 {
474 fetch_gregs_from_thread (regcache);
e9902bfc
AH
475 if (tdep->has_sve ())
476 fetch_sveregs_from_thread (regcache);
477 else
478 fetch_fpregs_from_thread (regcache);
76bed0fd
AH
479
480 if (tdep->has_pauth ())
481 fetch_pauth_masks_from_thread (regcache);
9d19df75
MS
482 }
483 else if (regno < AARCH64_V0_REGNUM)
484 fetch_gregs_from_thread (regcache);
e9902bfc
AH
485 else if (tdep->has_sve ())
486 fetch_sveregs_from_thread (regcache);
9d19df75
MS
487 else
488 fetch_fpregs_from_thread (regcache);
76bed0fd
AH
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 }
9d19df75
MS
496}
497
f6ac5f3d 498/* Implement the "store_registers" target_ops method. */
9d19df75 499
f6ac5f3d
PA
500void
501aarch64_linux_nat_target::store_registers (struct regcache *regcache,
502 int regno)
9d19df75 503{
e9902bfc
AH
504 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
505
9d19df75
MS
506 if (regno == -1)
507 {
508 store_gregs_to_thread (regcache);
e9902bfc
AH
509 if (tdep->has_sve ())
510 store_sveregs_to_thread (regcache);
511 else
512 store_fpregs_to_thread (regcache);
9d19df75
MS
513 }
514 else if (regno < AARCH64_V0_REGNUM)
515 store_gregs_to_thread (regcache);
e9902bfc
AH
516 else if (tdep->has_sve ())
517 store_sveregs_to_thread (regcache);
9d19df75
MS
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
526void
527fill_gregset (const struct regcache *regcache,
528 gdb_gregset_t *gregsetp, int regno)
529{
d4d793bf
AA
530 regcache_collect_regset (&aarch64_linux_gregset, regcache,
531 regno, (gdb_byte *) gregsetp,
532 AARCH64_LINUX_SIZEOF_GREGSET);
9d19df75
MS
533}
534
535/* Fill GDB's register array with the general-purpose register values
536 in *GREGSETP. */
537
538void
539supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
540{
d4d793bf
AA
541 regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
542 (const gdb_byte *) gregsetp,
543 AARCH64_LINUX_SIZEOF_GREGSET);
9d19df75
MS
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
550void
551fill_fpregset (const struct regcache *regcache,
552 gdb_fpregset_t *fpregsetp, int regno)
553{
d4d793bf
AA
554 regcache_collect_regset (&aarch64_linux_fpregset, regcache,
555 regno, (gdb_byte *) fpregsetp,
556 AARCH64_LINUX_SIZEOF_FPREGSET);
9d19df75
MS
557}
558
559/* Fill GDB's register array with the floating-point register values
560 in *FPREGSETP. */
561
562void
563supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
564{
d4d793bf
AA
565 regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
566 (const gdb_byte *) fpregsetp,
567 AARCH64_LINUX_SIZEOF_FPREGSET);
9d19df75
MS
568}
569
d6c44983
YZ
570/* linux_nat_new_fork hook. */
571
135340af
PA
572void
573aarch64_linux_nat_target::low_new_fork (struct lwp_info *parent,
574 pid_t child_pid)
d6c44983
YZ
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
e99b03dc 591 parent_pid = parent->ptid.pid ();
d6c44983
YZ
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}
9d19df75
MS
596\f
597
598/* Called by libthread_db. Returns a pointer to the thread local
599 storage (or its descriptor). */
600
601ps_err_e
754653a7 602ps_get_thread_area (struct ps_prochandle *ph,
9d19df75
MS
603 lwpid_t lwpid, int idx, void **base)
604{
a0cc84cd
YQ
605 int is_64bit_p
606 = (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 64);
9d19df75 607
a0cc84cd 608 return aarch64_ps_get_thread_area (ph, lwpid, idx, base, is_64bit_p);
9d19df75
MS
609}
610\f
611
f6ac5f3d 612/* Implement the "post_startup_inferior" target_ops method. */
9d19df75 613
f6ac5f3d
PA
614void
615aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid)
9d19df75 616{
e99b03dc
TT
617 low_forget_process (ptid.pid ());
618 aarch64_linux_get_debug_reg_capacity (ptid.pid ());
f6ac5f3d 619 linux_nat_target::post_startup_inferior (ptid);
9d19df75
MS
620}
621
8363f9d5
RB
622/* Implement the "post_attach" target_ops method. */
623
624void
625aarch64_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
f6ac5f3d 637/* Implement the "read_description" target_ops method. */
9d19df75 638
f6ac5f3d
PA
639const struct target_desc *
640aarch64_linux_nat_target::read_description ()
9d19df75 641{
6f67973b 642 int ret, tid;
350fab54 643 gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
6f67973b 644 struct iovec iovec;
607685ec 645
e38504b3 646 tid = inferior_ptid.lwp ();
607685ec 647
6f67973b 648 iovec.iov_base = regbuf;
350fab54 649 iovec.iov_len = ARM_VFP3_REGS_SIZE;
607685ec 650
6f67973b
YQ
651 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec);
652 if (ret == 0)
d105cce5 653 return aarch32_read_description ();
6dc0ebde 654
0f83012e 655 CORE_ADDR hwcap = linux_get_hwcap (this);
d7212f00
LM
656 CORE_ADDR hwcap2 = linux_get_hwcap2 (this);
657
658 bool mte_p = hwcap2 & HWCAP2_MTE;
ee4fbcfa 659
0f83012e 660 return aarch64_read_description (aarch64_sve_get_vq (tid),
d7212f00 661 hwcap & AARCH64_HWCAP_PACA, mte_p);
9d19df75
MS
662}
663
ade90bde
YQ
664/* Convert a native/host siginfo object, into/from the siginfo in the
665 layout of the inferiors' architecture. Returns true if any
666 conversion was done; false otherwise. If DIRECTION is 1, then copy
667 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
668 INF. */
669
135340af
PA
670bool
671aarch64_linux_nat_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
672 int direction)
ade90bde
YQ
673{
674 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
675
676 /* Is the inferior 32-bit? If so, then do fixup the siginfo
677 object. */
678 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
679 {
680 if (direction == 0)
681 aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
682 native);
683 else
684 aarch64_siginfo_from_compat_siginfo (native,
685 (struct compat_siginfo *) inf);
686
135340af 687 return true;
ade90bde
YQ
688 }
689
135340af 690 return false;
ade90bde
YQ
691}
692
9d19df75
MS
693/* Returns the number of hardware watchpoints of type TYPE that we can
694 set. Value is positive if we can set CNT watchpoints, zero if
695 setting watchpoints of type TYPE is not supported, and negative if
696 CNT is more than the maximum number of watchpoints of type TYPE
697 that we can support. TYPE is one of bp_hardware_watchpoint,
698 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
699 CNT is the number of such watchpoints used so far (including this
700 one). OTHERTYPE is non-zero if other types of watchpoints are
c2fbdc59 701 currently enabled. */
9d19df75 702
f6ac5f3d
PA
703int
704aarch64_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
705 int cnt, int othertype)
9d19df75 706{
c2fbdc59
YQ
707 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
708 || type == bp_access_watchpoint || type == bp_watchpoint)
709 {
710 if (aarch64_num_wp_regs == 0)
711 return 0;
712 }
713 else if (type == bp_hardware_breakpoint)
714 {
715 if (aarch64_num_bp_regs == 0)
716 return 0;
717 }
718 else
719 gdb_assert_not_reached ("unexpected breakpoint type");
720
721 /* We always return 1 here because we don't have enough information
722 about possible overlap of addresses that they want to watch. As an
723 extreme example, consider the case where all the watchpoints watch
724 the same address and the same region length: then we can handle a
725 virtually unlimited number of watchpoints, due to debug register
726 sharing implemented via reference counts. */
9d19df75
MS
727 return 1;
728}
729
0d5ed153 730/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
9d19df75
MS
731 Return 0 on success, -1 on failure. */
732
f6ac5f3d
PA
733int
734aarch64_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
735 struct bp_target_info *bp_tgt)
9d19df75
MS
736{
737 int ret;
0d5ed153 738 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
8d689ee5 739 int len;
2ecd81c2 740 const enum target_hw_bp_type type = hw_execute;
c67ca4de 741 struct aarch64_debug_reg_state *state
e99b03dc 742 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
9d19df75 743
8d689ee5
YQ
744 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
745
c5e92cca 746 if (show_debug_regs)
9d19df75
MS
747 fprintf_unfiltered
748 (gdb_stdlog,
749 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
750 (unsigned long) addr, len);
751
c67ca4de 752 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state);
9d19df75 753
c5e92cca 754 if (show_debug_regs)
d6c44983 755 {
d6c44983 756 aarch64_show_debug_reg_state (state,
2fd0f80d 757 "insert_hw_breakpoint", addr, len, type);
d6c44983 758 }
9d19df75
MS
759
760 return ret;
761}
762
763/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
764 Return 0 on success, -1 on failure. */
765
f6ac5f3d
PA
766int
767aarch64_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
768 struct bp_target_info *bp_tgt)
9d19df75
MS
769{
770 int ret;
771 CORE_ADDR addr = bp_tgt->placed_address;
8d689ee5 772 int len = 4;
2ecd81c2 773 const enum target_hw_bp_type type = hw_execute;
c67ca4de 774 struct aarch64_debug_reg_state *state
e99b03dc 775 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
9d19df75 776
8d689ee5
YQ
777 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
778
c5e92cca 779 if (show_debug_regs)
9d19df75
MS
780 fprintf_unfiltered
781 (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
782 (unsigned long) addr, len);
783
c67ca4de 784 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state);
9d19df75 785
c5e92cca 786 if (show_debug_regs)
d6c44983 787 {
d6c44983
YZ
788 aarch64_show_debug_reg_state (state,
789 "remove_hw_watchpoint", addr, len, type);
790 }
9d19df75
MS
791
792 return ret;
793}
794
f6ac5f3d 795/* Implement the "insert_watchpoint" target_ops method.
9d19df75
MS
796
797 Insert a watchpoint to watch a memory region which starts at
798 address ADDR and whose length is LEN bytes. Watch memory accesses
799 of the type TYPE. Return 0 on success, -1 on failure. */
800
f6ac5f3d
PA
801int
802aarch64_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
803 enum target_hw_bp_type type,
804 struct expression *cond)
9d19df75
MS
805{
806 int ret;
c67ca4de 807 struct aarch64_debug_reg_state *state
e99b03dc 808 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
9d19df75 809
c5e92cca 810 if (show_debug_regs)
9d19df75
MS
811 fprintf_unfiltered (gdb_stdlog,
812 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
813 (unsigned long) addr, len);
814
815 gdb_assert (type != hw_execute);
816
c67ca4de 817 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state);
9d19df75 818
c5e92cca 819 if (show_debug_regs)
d6c44983 820 {
d6c44983
YZ
821 aarch64_show_debug_reg_state (state,
822 "insert_watchpoint", addr, len, type);
823 }
9d19df75
MS
824
825 return ret;
826}
827
f6ac5f3d 828/* Implement the "remove_watchpoint" target_ops method.
9d19df75
MS
829 Remove a watchpoint that watched the memory region which starts at
830 address ADDR, whose length is LEN bytes, and for accesses of the
831 type TYPE. Return 0 on success, -1 on failure. */
832
f6ac5f3d
PA
833int
834aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
835 enum target_hw_bp_type type,
836 struct expression *cond)
9d19df75
MS
837{
838 int ret;
c67ca4de 839 struct aarch64_debug_reg_state *state
e99b03dc 840 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
9d19df75 841
c5e92cca 842 if (show_debug_regs)
9d19df75
MS
843 fprintf_unfiltered (gdb_stdlog,
844 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
845 (unsigned long) addr, len);
846
847 gdb_assert (type != hw_execute);
848
c67ca4de 849 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state);
9d19df75 850
c5e92cca 851 if (show_debug_regs)
d6c44983 852 {
d6c44983
YZ
853 aarch64_show_debug_reg_state (state,
854 "remove_watchpoint", addr, len, type);
855 }
9d19df75
MS
856
857 return ret;
858}
859
f6ac5f3d 860/* Implement the "region_ok_for_hw_watchpoint" target_ops method. */
9d19df75 861
f6ac5f3d
PA
862int
863aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
9d19df75 864{
39edd165 865 return aarch64_linux_region_ok_for_watchpoint (addr, len);
9d19df75
MS
866}
867
f6ac5f3d 868/* Implement the "stopped_data_address" target_ops method. */
9d19df75 869
57810aa7 870bool
f6ac5f3d 871aarch64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
9d19df75
MS
872{
873 siginfo_t siginfo;
cf4088a9 874 int i;
9d19df75
MS
875 struct aarch64_debug_reg_state *state;
876
877 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
57810aa7 878 return false;
9d19df75
MS
879
880 /* This must be a hardware breakpoint. */
881 if (siginfo.si_signo != SIGTRAP
882 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
57810aa7 883 return false;
9d19df75
MS
884
885 /* Check if the address matches any watched address. */
e99b03dc 886 state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
9d19df75
MS
887 for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
888 {
a3b60e45
JK
889 const unsigned int offset
890 = aarch64_watchpoint_offset (state->dr_ctrl_wp[i]);
9d19df75
MS
891 const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
892 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
a3b60e45
JK
893 const CORE_ADDR addr_watch = state->dr_addr_wp[i] + offset;
894 const CORE_ADDR addr_watch_aligned = align_down (state->dr_addr_wp[i], 8);
895 const CORE_ADDR addr_orig = state->dr_addr_orig_wp[i];
9d19df75
MS
896
897 if (state->dr_ref_count_wp[i]
898 && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
a3b60e45 899 && addr_trap >= addr_watch_aligned
9d19df75
MS
900 && addr_trap < addr_watch + len)
901 {
a3b60e45
JK
902 /* ADDR_TRAP reports the first address of the memory range
903 accessed by the CPU, regardless of what was the memory
904 range watched. Thus, a large CPU access that straddles
905 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
906 ADDR_TRAP that is lower than the
907 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
908
909 addr: | 4 | 5 | 6 | 7 | 8 |
910 |---- range watched ----|
911 |----------- range accessed ------------|
912
913 In this case, ADDR_TRAP will be 4.
914
915 To match a watchpoint known to GDB core, we must never
916 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
917 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
918 positive on kernels older than 4.10. See PR
919 external/20207. */
920 *addr_p = addr_orig;
57810aa7 921 return true;
9d19df75
MS
922 }
923 }
924
57810aa7 925 return false;
9d19df75
MS
926}
927
f6ac5f3d 928/* Implement the "stopped_by_watchpoint" target_ops method. */
9d19df75 929
57810aa7 930bool
f6ac5f3d 931aarch64_linux_nat_target::stopped_by_watchpoint ()
9d19df75
MS
932{
933 CORE_ADDR addr;
934
f6ac5f3d 935 return stopped_data_address (&addr);
9d19df75
MS
936}
937
f6ac5f3d 938/* Implement the "watchpoint_addr_within_range" target_ops method. */
9d19df75 939
57810aa7 940bool
f6ac5f3d
PA
941aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
942 CORE_ADDR start, int length)
9d19df75
MS
943{
944 return start <= addr && start + length - 1 >= addr;
945}
946
f6ac5f3d 947/* Implement the "can_do_single_step" target_ops method. */
750ce8d1 948
f6ac5f3d
PA
949int
950aarch64_linux_nat_target::can_do_single_step ()
750ce8d1
YQ
951{
952 return 1;
953}
954
4da037ef
AH
955/* Implement the "thread_architecture" target_ops method. */
956
957struct gdbarch *
958aarch64_linux_nat_target::thread_architecture (ptid_t ptid)
959{
960 /* Return the gdbarch for the current thread. If the vector length has
961 changed since the last time this was called, then do a further lookup. */
962
963 uint64_t vq = aarch64_sve_get_vq (ptid.lwp ());
964
965 /* Find the current gdbarch the same way as process_stratum_target. Only
966 return it if the current vector length matches the one in the tdep. */
5b6d1e4f 967 inferior *inf = find_inferior_ptid (this, ptid);
4da037ef
AH
968 gdb_assert (inf != NULL);
969 if (vq == gdbarch_tdep (inf->gdbarch)->vq)
970 return inf->gdbarch;
971
972 /* We reach here if the vector length for the thread is different from its
973 value at process start. Lookup gdbarch via info (potentially creating a
974 new one), stashing the vector length inside id. Use -1 for when SVE
975 unavailable, to distinguish from an unset value of 0. */
976 struct gdbarch_info info;
977 gdbarch_info_init (&info);
6a5206eb 978 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_aarch64, bfd_mach_aarch64);
4da037ef
AH
979 info.id = (int *) (vq == 0 ? -1 : vq);
980 return gdbarch_find_by_info (info);
981}
982
9d19df75
MS
983/* Define AArch64 maintenance commands. */
984
985static void
986add_show_debug_regs_command (void)
987{
988 /* A maintenance command to enable printing the internal DRi mirror
989 variables. */
990 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
c5e92cca 991 &show_debug_regs, _("\
9d19df75
MS
992Set whether to show variables that mirror the AArch64 debug registers."), _("\
993Show whether to show variables that mirror the AArch64 debug registers."), _("\
994Use \"on\" to enable, \"off\" to disable.\n\
995If enabled, the debug registers values are shown when GDB inserts\n\
996or removes a hardware breakpoint or watchpoint, and when the inferior\n\
997triggers a breakpoint or watchpoint."),
998 NULL,
999 NULL,
1000 &maintenance_set_cmdlist,
1001 &maintenance_show_cmdlist);
1002}
1003
6c265988 1004void _initialize_aarch64_linux_nat ();
9d19df75 1005void
6c265988 1006_initialize_aarch64_linux_nat ()
9d19df75 1007{
9d19df75
MS
1008 add_show_debug_regs_command ();
1009
9d19df75 1010 /* Register the target. */
f6ac5f3d 1011 linux_target = &the_aarch64_linux_nat_target;
d9f719f1 1012 add_inf_child_target (&the_aarch64_linux_nat_target);
9d19df75 1013}