gdb/amd-dbgapi-target: Use inf param in detach
[binutils-gdb.git] / gdb / aarch64-linux-tdep.c
1 /* Target-dependent code for GNU/Linux AArch64.
2
3 Copyright (C) 2009-2023 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 "gdbarch.h"
24 #include "glibc-tdep.h"
25 #include "linux-tdep.h"
26 #include "aarch64-tdep.h"
27 #include "aarch64-linux-tdep.h"
28 #include "osabi.h"
29 #include "solib-svr4.h"
30 #include "symtab.h"
31 #include "tramp-frame.h"
32 #include "trad-frame.h"
33 #include "target.h"
34 #include "target/target.h"
35 #include "expop.h"
36 #include "auxv.h"
37
38 #include "regcache.h"
39 #include "regset.h"
40
41 #include "stap-probe.h"
42 #include "parser-defs.h"
43 #include "user-regs.h"
44 #include "xml-syscall.h"
45 #include <ctype.h>
46
47 #include "record-full.h"
48 #include "linux-record.h"
49
50 #include "arch/aarch64-mte-linux.h"
51
52 #include "arch-utils.h"
53 #include "value.h"
54
55 #include "gdbsupport/selftest.h"
56
57 #include "elf/common.h"
58 #include "elf/aarch64.h"
59
60 /* Signal frame handling.
61
62 +------------+ ^
63 | saved lr | |
64 +->| saved fp |--+
65 | | |
66 | | |
67 | +------------+
68 | | saved lr |
69 +--| saved fp |
70 ^ | |
71 | | |
72 | +------------+
73 ^ | |
74 | | signal |
75 | | | SIGTRAMP_FRAME (struct rt_sigframe)
76 | | saved regs |
77 +--| saved sp |--> interrupted_sp
78 | | saved pc |--> interrupted_pc
79 | | |
80 | +------------+
81 | | saved lr |--> default_restorer (movz x8, NR_sys_rt_sigreturn; svc 0)
82 +--| saved fp |<- FP
83 | | NORMAL_FRAME
84 | |<- SP
85 +------------+
86
87 On signal delivery, the kernel will create a signal handler stack
88 frame and setup the return address in LR to point at restorer stub.
89 The signal stack frame is defined by:
90
91 struct rt_sigframe
92 {
93 siginfo_t info;
94 struct ucontext uc;
95 };
96
97 The ucontext has the following form:
98 struct ucontext
99 {
100 unsigned long uc_flags;
101 struct ucontext *uc_link;
102 stack_t uc_stack;
103 sigset_t uc_sigmask;
104 struct sigcontext uc_mcontext;
105 };
106
107 struct sigcontext
108 {
109 unsigned long fault_address;
110 unsigned long regs[31];
111 unsigned long sp; / * 31 * /
112 unsigned long pc; / * 32 * /
113 unsigned long pstate; / * 33 * /
114 __u8 __reserved[4096]
115 };
116
117 The reserved space in sigcontext contains additional structures, each starting
118 with a aarch64_ctx, which specifies a unique identifier and the total size of
119 the structure. The final structure in reserved will start will a null
120 aarch64_ctx. The penultimate entry in reserved may be a extra_context which
121 then points to a further block of reserved space.
122
123 struct aarch64_ctx {
124 u32 magic;
125 u32 size;
126 };
127
128 The restorer stub will always have the form:
129
130 d28015a8 movz x8, #0xad
131 d4000001 svc #0x0
132
133 This is a system call sys_rt_sigreturn.
134
135 We detect signal frames by snooping the return code for the restorer
136 instruction sequence.
137
138 The handler then needs to recover the saved register set from
139 ucontext.uc_mcontext. */
140
141 /* These magic numbers need to reflect the layout of the kernel
142 defined struct rt_sigframe and ucontext. */
143 #define AARCH64_SIGCONTEXT_REG_SIZE 8
144 #define AARCH64_RT_SIGFRAME_UCONTEXT_OFFSET 128
145 #define AARCH64_UCONTEXT_SIGCONTEXT_OFFSET 176
146 #define AARCH64_SIGCONTEXT_XO_OFFSET 8
147 #define AARCH64_SIGCONTEXT_RESERVED_OFFSET 288
148
149 #define AARCH64_SIGCONTEXT_RESERVED_SIZE 4096
150
151 /* Unique identifiers that may be used for aarch64_ctx.magic. */
152 #define AARCH64_EXTRA_MAGIC 0x45585401
153 #define AARCH64_FPSIMD_MAGIC 0x46508001
154 #define AARCH64_SVE_MAGIC 0x53564501
155
156 /* Defines for the extra_context that follows an AARCH64_EXTRA_MAGIC. */
157 #define AARCH64_EXTRA_DATAP_OFFSET 8
158
159 /* Defines for the fpsimd that follows an AARCH64_FPSIMD_MAGIC. */
160 #define AARCH64_FPSIMD_FPSR_OFFSET 8
161 #define AARCH64_FPSIMD_FPCR_OFFSET 12
162 #define AARCH64_FPSIMD_V0_OFFSET 16
163 #define AARCH64_FPSIMD_VREG_SIZE 16
164
165 /* Defines for the sve structure that follows an AARCH64_SVE_MAGIC. */
166 #define AARCH64_SVE_CONTEXT_VL_OFFSET 8
167 #define AARCH64_SVE_CONTEXT_REGS_OFFSET 16
168 #define AARCH64_SVE_CONTEXT_P_REGS_OFFSET(vq) (32 * vq * 16)
169 #define AARCH64_SVE_CONTEXT_FFR_OFFSET(vq) \
170 (AARCH64_SVE_CONTEXT_P_REGS_OFFSET (vq) + (16 * vq * 2))
171 #define AARCH64_SVE_CONTEXT_SIZE(vq) \
172 (AARCH64_SVE_CONTEXT_FFR_OFFSET (vq) + (vq * 2))
173
174
175 /* Read an aarch64_ctx, returning the magic value, and setting *SIZE to the
176 size, or return 0 on error. */
177
178 static uint32_t
179 read_aarch64_ctx (CORE_ADDR ctx_addr, enum bfd_endian byte_order,
180 uint32_t *size)
181 {
182 uint32_t magic = 0;
183 gdb_byte buf[4];
184
185 if (target_read_memory (ctx_addr, buf, 4) != 0)
186 return 0;
187 magic = extract_unsigned_integer (buf, 4, byte_order);
188
189 if (target_read_memory (ctx_addr + 4, buf, 4) != 0)
190 return 0;
191 *size = extract_unsigned_integer (buf, 4, byte_order);
192
193 return magic;
194 }
195
196 /* Given CACHE, use the trad_frame* functions to restore the FPSIMD
197 registers from a signal frame.
198
199 VREG_NUM is the number of the V register being restored, OFFSET is the
200 address containing the register value, BYTE_ORDER is the endianness and
201 HAS_SVE tells us if we have a valid SVE context or not. */
202
203 static void
204 aarch64_linux_restore_vreg (struct trad_frame_cache *cache, int num_regs,
205 int vreg_num, CORE_ADDR offset,
206 enum bfd_endian byte_order, bool has_sve)
207 {
208 /* WARNING: SIMD state is laid out in memory in target-endian format.
209
210 So we have a couple cases to consider:
211
212 1 - If the target is big endian, then SIMD state is big endian,
213 requiring a byteswap.
214
215 2 - If the target is little endian, then SIMD state is little endian, so
216 no byteswap is needed. */
217
218 if (byte_order == BFD_ENDIAN_BIG)
219 {
220 gdb_byte buf[V_REGISTER_SIZE];
221
222 if (target_read_memory (offset, buf, V_REGISTER_SIZE) != 0)
223 {
224 size_t size = V_REGISTER_SIZE/2;
225
226 /* Read the two halves of the V register in reverse byte order. */
227 CORE_ADDR u64 = extract_unsigned_integer (buf, size,
228 byte_order);
229 CORE_ADDR l64 = extract_unsigned_integer (buf + size, size,
230 byte_order);
231
232 /* Copy the reversed bytes to the buffer. */
233 store_unsigned_integer (buf, size, BFD_ENDIAN_LITTLE, l64);
234 store_unsigned_integer (buf + size , size, BFD_ENDIAN_LITTLE, u64);
235
236 /* Now we can store the correct bytes for the V register. */
237 trad_frame_set_reg_value_bytes (cache, AARCH64_V0_REGNUM + vreg_num,
238 {buf, V_REGISTER_SIZE});
239 trad_frame_set_reg_value_bytes (cache,
240 num_regs + AARCH64_Q0_REGNUM
241 + vreg_num, {buf, Q_REGISTER_SIZE});
242 trad_frame_set_reg_value_bytes (cache,
243 num_regs + AARCH64_D0_REGNUM
244 + vreg_num, {buf, D_REGISTER_SIZE});
245 trad_frame_set_reg_value_bytes (cache,
246 num_regs + AARCH64_S0_REGNUM
247 + vreg_num, {buf, S_REGISTER_SIZE});
248 trad_frame_set_reg_value_bytes (cache,
249 num_regs + AARCH64_H0_REGNUM
250 + vreg_num, {buf, H_REGISTER_SIZE});
251 trad_frame_set_reg_value_bytes (cache,
252 num_regs + AARCH64_B0_REGNUM
253 + vreg_num, {buf, B_REGISTER_SIZE});
254
255 if (has_sve)
256 trad_frame_set_reg_value_bytes (cache,
257 num_regs + AARCH64_SVE_V0_REGNUM
258 + vreg_num, {buf, V_REGISTER_SIZE});
259 }
260 return;
261 }
262
263 /* Little endian, just point at the address containing the register
264 value. */
265 trad_frame_set_reg_addr (cache, AARCH64_V0_REGNUM + vreg_num, offset);
266 trad_frame_set_reg_addr (cache, num_regs + AARCH64_Q0_REGNUM + vreg_num,
267 offset);
268 trad_frame_set_reg_addr (cache, num_regs + AARCH64_D0_REGNUM + vreg_num,
269 offset);
270 trad_frame_set_reg_addr (cache, num_regs + AARCH64_S0_REGNUM + vreg_num,
271 offset);
272 trad_frame_set_reg_addr (cache, num_regs + AARCH64_H0_REGNUM + vreg_num,
273 offset);
274 trad_frame_set_reg_addr (cache, num_regs + AARCH64_B0_REGNUM + vreg_num,
275 offset);
276
277 if (has_sve)
278 trad_frame_set_reg_addr (cache, num_regs + AARCH64_SVE_V0_REGNUM
279 + vreg_num, offset);
280
281 }
282
283 /* Implement the "init" method of struct tramp_frame. */
284
285 static void
286 aarch64_linux_sigframe_init (const struct tramp_frame *self,
287 frame_info_ptr this_frame,
288 struct trad_frame_cache *this_cache,
289 CORE_ADDR func)
290 {
291 struct gdbarch *gdbarch = get_frame_arch (this_frame);
292 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
293 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
294 CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
295 CORE_ADDR sigcontext_addr = (sp + AARCH64_RT_SIGFRAME_UCONTEXT_OFFSET
296 + AARCH64_UCONTEXT_SIGCONTEXT_OFFSET );
297 CORE_ADDR section = sigcontext_addr + AARCH64_SIGCONTEXT_RESERVED_OFFSET;
298 CORE_ADDR section_end = section + AARCH64_SIGCONTEXT_RESERVED_SIZE;
299 CORE_ADDR fpsimd = 0;
300 CORE_ADDR sve_regs = 0;
301 uint32_t size, magic;
302 bool extra_found = false;
303 int num_regs = gdbarch_num_regs (gdbarch);
304
305 /* Read in the integer registers. */
306
307 for (int i = 0; i < 31; i++)
308 {
309 trad_frame_set_reg_addr (this_cache,
310 AARCH64_X0_REGNUM + i,
311 sigcontext_addr + AARCH64_SIGCONTEXT_XO_OFFSET
312 + i * AARCH64_SIGCONTEXT_REG_SIZE);
313 }
314 trad_frame_set_reg_addr (this_cache, AARCH64_SP_REGNUM,
315 sigcontext_addr + AARCH64_SIGCONTEXT_XO_OFFSET
316 + 31 * AARCH64_SIGCONTEXT_REG_SIZE);
317 trad_frame_set_reg_addr (this_cache, AARCH64_PC_REGNUM,
318 sigcontext_addr + AARCH64_SIGCONTEXT_XO_OFFSET
319 + 32 * AARCH64_SIGCONTEXT_REG_SIZE);
320
321 /* Search for the FP and SVE sections, stopping at null. */
322 while ((magic = read_aarch64_ctx (section, byte_order, &size)) != 0
323 && size != 0)
324 {
325 switch (magic)
326 {
327 case AARCH64_FPSIMD_MAGIC:
328 fpsimd = section;
329 section += size;
330 break;
331
332 case AARCH64_SVE_MAGIC:
333 {
334 /* Check if the section is followed by a full SVE dump, and set
335 sve_regs if it is. */
336 gdb_byte buf[4];
337 uint16_t vq;
338
339 if (!tdep->has_sve ())
340 break;
341
342 if (target_read_memory (section + AARCH64_SVE_CONTEXT_VL_OFFSET,
343 buf, 2) != 0)
344 {
345 section += size;
346 break;
347 }
348 vq = sve_vq_from_vl (extract_unsigned_integer (buf, 2, byte_order));
349
350 if (vq != tdep->vq)
351 error (_("Invalid vector length in signal frame %d vs %s."), vq,
352 pulongest (tdep->vq));
353
354 if (size >= AARCH64_SVE_CONTEXT_SIZE (vq))
355 sve_regs = section + AARCH64_SVE_CONTEXT_REGS_OFFSET;
356
357 section += size;
358 break;
359 }
360
361 case AARCH64_EXTRA_MAGIC:
362 {
363 /* Extra is always the last valid section in reserved and points to
364 an additional block of memory filled with more sections. Reset
365 the address to the extra section and continue looking for more
366 structures. */
367 gdb_byte buf[8];
368
369 if (target_read_memory (section + AARCH64_EXTRA_DATAP_OFFSET,
370 buf, 8) != 0)
371 {
372 section += size;
373 break;
374 }
375
376 section = extract_unsigned_integer (buf, 8, byte_order);
377 extra_found = true;
378 break;
379 }
380
381 default:
382 section += size;
383 break;
384 }
385
386 /* Prevent searching past the end of the reserved section. The extra
387 section does not have a hard coded limit - we have to rely on it ending
388 with nulls. */
389 if (!extra_found && section > section_end)
390 break;
391 }
392
393 if (sve_regs != 0)
394 {
395 CORE_ADDR offset;
396
397 for (int i = 0; i < 32; i++)
398 {
399 offset = sve_regs + (i * tdep->vq * 16);
400 trad_frame_set_reg_addr (this_cache, AARCH64_SVE_Z0_REGNUM + i,
401 offset);
402 trad_frame_set_reg_addr (this_cache,
403 num_regs + AARCH64_SVE_V0_REGNUM + i,
404 offset);
405 trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_Q0_REGNUM + i,
406 offset);
407 trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_D0_REGNUM + i,
408 offset);
409 trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_S0_REGNUM + i,
410 offset);
411 trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_H0_REGNUM + i,
412 offset);
413 trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_B0_REGNUM + i,
414 offset);
415 }
416
417 offset = sve_regs + AARCH64_SVE_CONTEXT_P_REGS_OFFSET (tdep->vq);
418 for (int i = 0; i < 16; i++)
419 trad_frame_set_reg_addr (this_cache, AARCH64_SVE_P0_REGNUM + i,
420 offset + (i * tdep->vq * 2));
421
422 offset = sve_regs + AARCH64_SVE_CONTEXT_FFR_OFFSET (tdep->vq);
423 trad_frame_set_reg_addr (this_cache, AARCH64_SVE_FFR_REGNUM, offset);
424 }
425
426 if (fpsimd != 0)
427 {
428 trad_frame_set_reg_addr (this_cache, AARCH64_FPSR_REGNUM,
429 fpsimd + AARCH64_FPSIMD_FPSR_OFFSET);
430 trad_frame_set_reg_addr (this_cache, AARCH64_FPCR_REGNUM,
431 fpsimd + AARCH64_FPSIMD_FPCR_OFFSET);
432
433 /* If there was no SVE section then set up the V registers. */
434 if (sve_regs == 0)
435 {
436 for (int i = 0; i < 32; i++)
437 {
438 CORE_ADDR offset = (fpsimd + AARCH64_FPSIMD_V0_OFFSET
439 + (i * AARCH64_FPSIMD_VREG_SIZE));
440
441 aarch64_linux_restore_vreg (this_cache, num_regs, i, offset,
442 byte_order, tdep->has_sve ());
443 }
444 }
445 }
446
447 trad_frame_set_id (this_cache, frame_id_build (sp, func));
448 }
449
450 static const struct tramp_frame aarch64_linux_rt_sigframe =
451 {
452 SIGTRAMP_FRAME,
453 4,
454 {
455 /* movz x8, 0x8b (S=1,o=10,h=0,i=0x8b,r=8)
456 Soo1 0010 1hhi iiii iiii iiii iiir rrrr */
457 {0xd2801168, ULONGEST_MAX},
458
459 /* svc 0x0 (o=0, l=1)
460 1101 0100 oooi iiii iiii iiii iii0 00ll */
461 {0xd4000001, ULONGEST_MAX},
462 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
463 },
464 aarch64_linux_sigframe_init
465 };
466
467 /* Register maps. */
468
469 static const struct regcache_map_entry aarch64_linux_gregmap[] =
470 {
471 { 31, AARCH64_X0_REGNUM, 8 }, /* x0 ... x30 */
472 { 1, AARCH64_SP_REGNUM, 8 },
473 { 1, AARCH64_PC_REGNUM, 8 },
474 { 1, AARCH64_CPSR_REGNUM, 8 },
475 { 0 }
476 };
477
478 static const struct regcache_map_entry aarch64_linux_fpregmap[] =
479 {
480 { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */
481 { 1, AARCH64_FPSR_REGNUM, 4 },
482 { 1, AARCH64_FPCR_REGNUM, 4 },
483 { 0 }
484 };
485
486 /* Register set definitions. */
487
488 const struct regset aarch64_linux_gregset =
489 {
490 aarch64_linux_gregmap,
491 regcache_supply_regset, regcache_collect_regset
492 };
493
494 const struct regset aarch64_linux_fpregset =
495 {
496 aarch64_linux_fpregmap,
497 regcache_supply_regset, regcache_collect_regset
498 };
499
500 /* The fields in an SVE header at the start of a SVE regset. */
501
502 #define SVE_HEADER_SIZE_LENGTH 4
503 #define SVE_HEADER_MAX_SIZE_LENGTH 4
504 #define SVE_HEADER_VL_LENGTH 2
505 #define SVE_HEADER_MAX_VL_LENGTH 2
506 #define SVE_HEADER_FLAGS_LENGTH 2
507 #define SVE_HEADER_RESERVED_LENGTH 2
508
509 #define SVE_HEADER_SIZE_OFFSET 0
510 #define SVE_HEADER_MAX_SIZE_OFFSET \
511 (SVE_HEADER_SIZE_OFFSET + SVE_HEADER_SIZE_LENGTH)
512 #define SVE_HEADER_VL_OFFSET \
513 (SVE_HEADER_MAX_SIZE_OFFSET + SVE_HEADER_MAX_SIZE_LENGTH)
514 #define SVE_HEADER_MAX_VL_OFFSET \
515 (SVE_HEADER_VL_OFFSET + SVE_HEADER_VL_LENGTH)
516 #define SVE_HEADER_FLAGS_OFFSET \
517 (SVE_HEADER_MAX_VL_OFFSET + SVE_HEADER_MAX_VL_LENGTH)
518 #define SVE_HEADER_RESERVED_OFFSET \
519 (SVE_HEADER_FLAGS_OFFSET + SVE_HEADER_FLAGS_LENGTH)
520 #define SVE_HEADER_SIZE \
521 (SVE_HEADER_RESERVED_OFFSET + SVE_HEADER_RESERVED_LENGTH)
522
523 #define SVE_HEADER_FLAG_SVE 1
524
525 /* Get VQ value from SVE section in the core dump. */
526
527 static uint64_t
528 aarch64_linux_core_read_vq (struct gdbarch *gdbarch, bfd *abfd)
529 {
530 gdb_byte header[SVE_HEADER_SIZE];
531 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
532 asection *sve_section = bfd_get_section_by_name (abfd, ".reg-aarch-sve");
533
534 if (sve_section == nullptr)
535 {
536 /* No SVE state. */
537 return 0;
538 }
539
540 size_t size = bfd_section_size (sve_section);
541
542 /* Check extended state size. */
543 if (size < SVE_HEADER_SIZE)
544 {
545 warning (_("'.reg-aarch-sve' section in core file too small."));
546 return 0;
547 }
548
549 if (!bfd_get_section_contents (abfd, sve_section, header, 0, SVE_HEADER_SIZE))
550 {
551 warning (_("Couldn't read sve header from "
552 "'.reg-aarch-sve' section in core file."));
553 return 0;
554 }
555
556 uint64_t vl = extract_unsigned_integer (header + SVE_HEADER_VL_OFFSET,
557 SVE_HEADER_VL_LENGTH, byte_order);
558 uint64_t vq = sve_vq_from_vl (vl);
559
560 if (vq > AARCH64_MAX_SVE_VQ)
561 {
562 warning (_("SVE Vector length in core file not supported by this version"
563 " of GDB. (VQ=%s)"), pulongest (vq));
564 return 0;
565 }
566 else if (vq == 0)
567 {
568 warning (_("SVE Vector length in core file is invalid. (VQ=%s"),
569 pulongest (vq));
570 return 0;
571 }
572
573 return vq;
574 }
575
576 /* Supply register REGNUM from BUF to REGCACHE, using the register map
577 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
578 If BUF is NULL, set the registers to "unavailable" status. */
579
580 static void
581 aarch64_linux_supply_sve_regset (const struct regset *regset,
582 struct regcache *regcache,
583 int regnum, const void *buf, size_t size)
584 {
585 gdb_byte *header = (gdb_byte *) buf;
586 struct gdbarch *gdbarch = regcache->arch ();
587 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
588
589 if (buf == nullptr)
590 return regcache->supply_regset (regset, regnum, nullptr, size);
591 gdb_assert (size > SVE_HEADER_SIZE);
592
593 /* BUF contains an SVE header followed by a register dump of either the
594 passed in SVE regset or a NEON fpregset. */
595
596 /* Extract required fields from the header. */
597 ULONGEST vl = extract_unsigned_integer (header + SVE_HEADER_VL_OFFSET,
598 SVE_HEADER_VL_LENGTH, byte_order);
599 uint16_t flags = extract_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
600 SVE_HEADER_FLAGS_LENGTH,
601 byte_order);
602
603 if (regnum == -1 || regnum == AARCH64_SVE_VG_REGNUM)
604 {
605 gdb_byte vg_target[8];
606 store_integer ((gdb_byte *)&vg_target, sizeof (uint64_t), byte_order,
607 sve_vg_from_vl (vl));
608 regcache->raw_supply (AARCH64_SVE_VG_REGNUM, &vg_target);
609 }
610
611 if (flags & SVE_HEADER_FLAG_SVE)
612 {
613 /* Register dump is a SVE structure. */
614 regcache->supply_regset (regset, regnum,
615 (gdb_byte *) buf + SVE_HEADER_SIZE,
616 size - SVE_HEADER_SIZE);
617 }
618 else
619 {
620 /* Register dump is a fpsimd structure. First clear the SVE
621 registers. */
622 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
623 regcache->raw_supply_zeroed (AARCH64_SVE_Z0_REGNUM + i);
624 for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
625 regcache->raw_supply_zeroed (AARCH64_SVE_P0_REGNUM + i);
626 regcache->raw_supply_zeroed (AARCH64_SVE_FFR_REGNUM);
627
628 /* Then supply the fpsimd registers. */
629 regcache->supply_regset (&aarch64_linux_fpregset, regnum,
630 (gdb_byte *) buf + SVE_HEADER_SIZE,
631 size - SVE_HEADER_SIZE);
632 }
633 }
634
635 /* Collect register REGNUM from REGCACHE to BUF, using the register
636 map in REGSET. If REGNUM is -1, do this for all registers in
637 REGSET. */
638
639 static void
640 aarch64_linux_collect_sve_regset (const struct regset *regset,
641 const struct regcache *regcache,
642 int regnum, void *buf, size_t size)
643 {
644 gdb_byte *header = (gdb_byte *) buf;
645 struct gdbarch *gdbarch = regcache->arch ();
646 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
647 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
648 uint64_t vq = tdep->vq;
649
650 gdb_assert (buf != NULL);
651 gdb_assert (size > SVE_HEADER_SIZE);
652
653 /* BUF starts with a SVE header prior to the register dump. */
654
655 store_unsigned_integer (header + SVE_HEADER_SIZE_OFFSET,
656 SVE_HEADER_SIZE_LENGTH, byte_order, size);
657 store_unsigned_integer (header + SVE_HEADER_MAX_SIZE_OFFSET,
658 SVE_HEADER_MAX_SIZE_LENGTH, byte_order, size);
659 store_unsigned_integer (header + SVE_HEADER_VL_OFFSET, SVE_HEADER_VL_LENGTH,
660 byte_order, sve_vl_from_vq (vq));
661 store_unsigned_integer (header + SVE_HEADER_MAX_VL_OFFSET,
662 SVE_HEADER_MAX_VL_LENGTH, byte_order,
663 sve_vl_from_vq (vq));
664 store_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
665 SVE_HEADER_FLAGS_LENGTH, byte_order,
666 SVE_HEADER_FLAG_SVE);
667 store_unsigned_integer (header + SVE_HEADER_RESERVED_OFFSET,
668 SVE_HEADER_RESERVED_LENGTH, byte_order, 0);
669
670 /* The SVE register dump follows. */
671 regcache->collect_regset (regset, regnum, (gdb_byte *) buf + SVE_HEADER_SIZE,
672 size - SVE_HEADER_SIZE);
673 }
674
675 /* Implement the "iterate_over_regset_sections" gdbarch method. */
676
677 static void
678 aarch64_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
679 iterate_over_regset_sections_cb *cb,
680 void *cb_data,
681 const struct regcache *regcache)
682 {
683 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
684
685 cb (".reg", AARCH64_LINUX_SIZEOF_GREGSET, AARCH64_LINUX_SIZEOF_GREGSET,
686 &aarch64_linux_gregset, NULL, cb_data);
687
688 if (tdep->has_sve ())
689 {
690 /* Create this on the fly in order to handle vector register sizes. */
691 const struct regcache_map_entry sve_regmap[] =
692 {
693 { 32, AARCH64_SVE_Z0_REGNUM, (int) (tdep->vq * 16) },
694 { 16, AARCH64_SVE_P0_REGNUM, (int) (tdep->vq * 16 / 8) },
695 { 1, AARCH64_SVE_FFR_REGNUM, (int) (tdep->vq * 16 / 8) },
696 { 1, AARCH64_FPSR_REGNUM, 4 },
697 { 1, AARCH64_FPCR_REGNUM, 4 },
698 { 0 }
699 };
700
701 const struct regset aarch64_linux_sve_regset =
702 {
703 sve_regmap,
704 aarch64_linux_supply_sve_regset, aarch64_linux_collect_sve_regset,
705 REGSET_VARIABLE_SIZE
706 };
707
708 cb (".reg-aarch-sve",
709 SVE_HEADER_SIZE + regcache_map_entry_size (aarch64_linux_fpregmap),
710 SVE_HEADER_SIZE + regcache_map_entry_size (sve_regmap),
711 &aarch64_linux_sve_regset, "SVE registers", cb_data);
712 }
713 else
714 cb (".reg2", AARCH64_LINUX_SIZEOF_FPREGSET, AARCH64_LINUX_SIZEOF_FPREGSET,
715 &aarch64_linux_fpregset, NULL, cb_data);
716
717
718 if (tdep->has_pauth ())
719 {
720 /* Create this on the fly in order to handle the variable location. */
721 const struct regcache_map_entry pauth_regmap[] =
722 {
723 { 2, AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base), 8},
724 { 0 }
725 };
726
727 const struct regset aarch64_linux_pauth_regset =
728 {
729 pauth_regmap, regcache_supply_regset, regcache_collect_regset
730 };
731
732 cb (".reg-aarch-pauth", AARCH64_LINUX_SIZEOF_PAUTH,
733 AARCH64_LINUX_SIZEOF_PAUTH, &aarch64_linux_pauth_regset,
734 "pauth registers", cb_data);
735 }
736
737 /* Handle MTE registers. */
738 if (tdep->has_mte ())
739 {
740 /* Create this on the fly in order to handle the variable location. */
741 const struct regcache_map_entry mte_regmap[] =
742 {
743 { 1, tdep->mte_reg_base, 8},
744 { 0 }
745 };
746
747 const struct regset aarch64_linux_mte_regset =
748 {
749 mte_regmap, regcache_supply_regset, regcache_collect_regset
750 };
751
752 cb (".reg-aarch-mte", AARCH64_LINUX_SIZEOF_MTE_REGSET,
753 AARCH64_LINUX_SIZEOF_MTE_REGSET, &aarch64_linux_mte_regset,
754 "MTE registers", cb_data);
755 }
756
757 /* Handle the TLS registers. */
758 if (tdep->has_tls ())
759 {
760 gdb_assert (tdep->tls_regnum_base != -1);
761 gdb_assert (tdep->tls_register_count > 0);
762
763 int sizeof_tls_regset
764 = AARCH64_TLS_REGISTER_SIZE * tdep->tls_register_count;
765
766 const struct regcache_map_entry tls_regmap[] =
767 {
768 { tdep->tls_register_count, tdep->tls_regnum_base,
769 AARCH64_TLS_REGISTER_SIZE },
770 { 0 }
771 };
772
773 const struct regset aarch64_linux_tls_regset =
774 {
775 tls_regmap, regcache_supply_regset, regcache_collect_regset,
776 REGSET_VARIABLE_SIZE
777 };
778
779 cb (".reg-aarch-tls", sizeof_tls_regset, sizeof_tls_regset,
780 &aarch64_linux_tls_regset, "TLS register", cb_data);
781 }
782 }
783
784 /* Implement the "core_read_description" gdbarch method. */
785
786 static const struct target_desc *
787 aarch64_linux_core_read_description (struct gdbarch *gdbarch,
788 struct target_ops *target, bfd *abfd)
789 {
790 gdb::optional<gdb::byte_vector> auxv = target_read_auxv_raw (target);
791 CORE_ADDR hwcap = linux_get_hwcap (auxv, target, gdbarch);
792 CORE_ADDR hwcap2 = linux_get_hwcap2 (auxv, target, gdbarch);
793
794 aarch64_features features;
795 features.vq = aarch64_linux_core_read_vq (gdbarch, abfd);
796 features.pauth = hwcap & AARCH64_HWCAP_PACA;
797 features.mte = hwcap2 & HWCAP2_MTE;
798
799 /* Handle the TLS section. */
800 asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
801 if (tls != nullptr)
802 {
803 size_t size = bfd_section_size (tls);
804 /* Convert the size to the number of actual registers, by
805 dividing by 8. */
806 features.tls = size / AARCH64_TLS_REGISTER_SIZE;
807 }
808
809 return aarch64_read_description (features);
810 }
811
812 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
813 gdbarch.h. */
814
815 static int
816 aarch64_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
817 {
818 return (*s == '#' || isdigit (*s) /* Literal number. */
819 || *s == '[' /* Register indirection. */
820 || isalpha (*s)); /* Register value. */
821 }
822
823 /* This routine is used to parse a special token in AArch64's assembly.
824
825 The special tokens parsed by it are:
826
827 - Register displacement (e.g, [fp, #-8])
828
829 It returns one if the special token has been parsed successfully,
830 or zero if the current token is not considered special. */
831
832 static expr::operation_up
833 aarch64_stap_parse_special_token (struct gdbarch *gdbarch,
834 struct stap_parse_info *p)
835 {
836 if (*p->arg == '[')
837 {
838 /* Temporary holder for lookahead. */
839 const char *tmp = p->arg;
840 char *endp;
841 /* Used to save the register name. */
842 const char *start;
843 int len;
844 int got_minus = 0;
845 long displacement;
846
847 ++tmp;
848 start = tmp;
849
850 /* Register name. */
851 while (isalnum (*tmp))
852 ++tmp;
853
854 if (*tmp != ',')
855 return {};
856
857 len = tmp - start;
858 std::string regname (start, len);
859
860 if (user_reg_map_name_to_regnum (gdbarch, regname.c_str (), len) == -1)
861 error (_("Invalid register name `%s' on expression `%s'."),
862 regname.c_str (), p->saved_arg);
863
864 ++tmp;
865 tmp = skip_spaces (tmp);
866 /* Now we expect a number. It can begin with '#' or simply
867 a digit. */
868 if (*tmp == '#')
869 ++tmp;
870
871 if (*tmp == '-')
872 {
873 ++tmp;
874 got_minus = 1;
875 }
876 else if (*tmp == '+')
877 ++tmp;
878
879 if (!isdigit (*tmp))
880 return {};
881
882 displacement = strtol (tmp, &endp, 10);
883 tmp = endp;
884
885 /* Skipping last `]'. */
886 if (*tmp++ != ']')
887 return {};
888 p->arg = tmp;
889
890 using namespace expr;
891
892 /* The displacement. */
893 struct type *long_type = builtin_type (gdbarch)->builtin_long;
894 if (got_minus)
895 displacement = -displacement;
896 operation_up disp = make_operation<long_const_operation> (long_type,
897 displacement);
898
899 /* The register name. */
900 operation_up reg
901 = make_operation<register_operation> (std::move (regname));
902
903 operation_up sum
904 = make_operation<add_operation> (std::move (reg), std::move (disp));
905
906 /* Casting to the expected type. */
907 struct type *arg_ptr_type = lookup_pointer_type (p->arg_type);
908 sum = make_operation<unop_cast_operation> (std::move (sum),
909 arg_ptr_type);
910 return make_operation<unop_ind_operation> (std::move (sum));
911 }
912 return {};
913 }
914
915 /* AArch64 process record-replay constructs: syscall, signal etc. */
916
917 static linux_record_tdep aarch64_linux_record_tdep;
918
919 /* Enum that defines the AArch64 linux specific syscall identifiers used for
920 process record/replay. */
921
922 enum aarch64_syscall {
923 aarch64_sys_io_setup = 0,
924 aarch64_sys_io_destroy = 1,
925 aarch64_sys_io_submit = 2,
926 aarch64_sys_io_cancel = 3,
927 aarch64_sys_io_getevents = 4,
928 aarch64_sys_setxattr = 5,
929 aarch64_sys_lsetxattr = 6,
930 aarch64_sys_fsetxattr = 7,
931 aarch64_sys_getxattr = 8,
932 aarch64_sys_lgetxattr = 9,
933 aarch64_sys_fgetxattr = 10,
934 aarch64_sys_listxattr = 11,
935 aarch64_sys_llistxattr = 12,
936 aarch64_sys_flistxattr = 13,
937 aarch64_sys_removexattr = 14,
938 aarch64_sys_lremovexattr = 15,
939 aarch64_sys_fremovexattr = 16,
940 aarch64_sys_getcwd = 17,
941 aarch64_sys_lookup_dcookie = 18,
942 aarch64_sys_eventfd2 = 19,
943 aarch64_sys_epoll_create1 = 20,
944 aarch64_sys_epoll_ctl = 21,
945 aarch64_sys_epoll_pwait = 22,
946 aarch64_sys_dup = 23,
947 aarch64_sys_dup3 = 24,
948 aarch64_sys_fcntl = 25,
949 aarch64_sys_inotify_init1 = 26,
950 aarch64_sys_inotify_add_watch = 27,
951 aarch64_sys_inotify_rm_watch = 28,
952 aarch64_sys_ioctl = 29,
953 aarch64_sys_ioprio_set = 30,
954 aarch64_sys_ioprio_get = 31,
955 aarch64_sys_flock = 32,
956 aarch64_sys_mknodat = 33,
957 aarch64_sys_mkdirat = 34,
958 aarch64_sys_unlinkat = 35,
959 aarch64_sys_symlinkat = 36,
960 aarch64_sys_linkat = 37,
961 aarch64_sys_renameat = 38,
962 aarch64_sys_umount2 = 39,
963 aarch64_sys_mount = 40,
964 aarch64_sys_pivot_root = 41,
965 aarch64_sys_nfsservctl = 42,
966 aarch64_sys_statfs = 43,
967 aarch64_sys_fstatfs = 44,
968 aarch64_sys_truncate = 45,
969 aarch64_sys_ftruncate = 46,
970 aarch64_sys_fallocate = 47,
971 aarch64_sys_faccessat = 48,
972 aarch64_sys_chdir = 49,
973 aarch64_sys_fchdir = 50,
974 aarch64_sys_chroot = 51,
975 aarch64_sys_fchmod = 52,
976 aarch64_sys_fchmodat = 53,
977 aarch64_sys_fchownat = 54,
978 aarch64_sys_fchown = 55,
979 aarch64_sys_openat = 56,
980 aarch64_sys_close = 57,
981 aarch64_sys_vhangup = 58,
982 aarch64_sys_pipe2 = 59,
983 aarch64_sys_quotactl = 60,
984 aarch64_sys_getdents64 = 61,
985 aarch64_sys_lseek = 62,
986 aarch64_sys_read = 63,
987 aarch64_sys_write = 64,
988 aarch64_sys_readv = 65,
989 aarch64_sys_writev = 66,
990 aarch64_sys_pread64 = 67,
991 aarch64_sys_pwrite64 = 68,
992 aarch64_sys_preadv = 69,
993 aarch64_sys_pwritev = 70,
994 aarch64_sys_sendfile = 71,
995 aarch64_sys_pselect6 = 72,
996 aarch64_sys_ppoll = 73,
997 aarch64_sys_signalfd4 = 74,
998 aarch64_sys_vmsplice = 75,
999 aarch64_sys_splice = 76,
1000 aarch64_sys_tee = 77,
1001 aarch64_sys_readlinkat = 78,
1002 aarch64_sys_newfstatat = 79,
1003 aarch64_sys_fstat = 80,
1004 aarch64_sys_sync = 81,
1005 aarch64_sys_fsync = 82,
1006 aarch64_sys_fdatasync = 83,
1007 aarch64_sys_sync_file_range2 = 84,
1008 aarch64_sys_sync_file_range = 84,
1009 aarch64_sys_timerfd_create = 85,
1010 aarch64_sys_timerfd_settime = 86,
1011 aarch64_sys_timerfd_gettime = 87,
1012 aarch64_sys_utimensat = 88,
1013 aarch64_sys_acct = 89,
1014 aarch64_sys_capget = 90,
1015 aarch64_sys_capset = 91,
1016 aarch64_sys_personality = 92,
1017 aarch64_sys_exit = 93,
1018 aarch64_sys_exit_group = 94,
1019 aarch64_sys_waitid = 95,
1020 aarch64_sys_set_tid_address = 96,
1021 aarch64_sys_unshare = 97,
1022 aarch64_sys_futex = 98,
1023 aarch64_sys_set_robust_list = 99,
1024 aarch64_sys_get_robust_list = 100,
1025 aarch64_sys_nanosleep = 101,
1026 aarch64_sys_getitimer = 102,
1027 aarch64_sys_setitimer = 103,
1028 aarch64_sys_kexec_load = 104,
1029 aarch64_sys_init_module = 105,
1030 aarch64_sys_delete_module = 106,
1031 aarch64_sys_timer_create = 107,
1032 aarch64_sys_timer_gettime = 108,
1033 aarch64_sys_timer_getoverrun = 109,
1034 aarch64_sys_timer_settime = 110,
1035 aarch64_sys_timer_delete = 111,
1036 aarch64_sys_clock_settime = 112,
1037 aarch64_sys_clock_gettime = 113,
1038 aarch64_sys_clock_getres = 114,
1039 aarch64_sys_clock_nanosleep = 115,
1040 aarch64_sys_syslog = 116,
1041 aarch64_sys_ptrace = 117,
1042 aarch64_sys_sched_setparam = 118,
1043 aarch64_sys_sched_setscheduler = 119,
1044 aarch64_sys_sched_getscheduler = 120,
1045 aarch64_sys_sched_getparam = 121,
1046 aarch64_sys_sched_setaffinity = 122,
1047 aarch64_sys_sched_getaffinity = 123,
1048 aarch64_sys_sched_yield = 124,
1049 aarch64_sys_sched_get_priority_max = 125,
1050 aarch64_sys_sched_get_priority_min = 126,
1051 aarch64_sys_sched_rr_get_interval = 127,
1052 aarch64_sys_kill = 129,
1053 aarch64_sys_tkill = 130,
1054 aarch64_sys_tgkill = 131,
1055 aarch64_sys_sigaltstack = 132,
1056 aarch64_sys_rt_sigsuspend = 133,
1057 aarch64_sys_rt_sigaction = 134,
1058 aarch64_sys_rt_sigprocmask = 135,
1059 aarch64_sys_rt_sigpending = 136,
1060 aarch64_sys_rt_sigtimedwait = 137,
1061 aarch64_sys_rt_sigqueueinfo = 138,
1062 aarch64_sys_rt_sigreturn = 139,
1063 aarch64_sys_setpriority = 140,
1064 aarch64_sys_getpriority = 141,
1065 aarch64_sys_reboot = 142,
1066 aarch64_sys_setregid = 143,
1067 aarch64_sys_setgid = 144,
1068 aarch64_sys_setreuid = 145,
1069 aarch64_sys_setuid = 146,
1070 aarch64_sys_setresuid = 147,
1071 aarch64_sys_getresuid = 148,
1072 aarch64_sys_setresgid = 149,
1073 aarch64_sys_getresgid = 150,
1074 aarch64_sys_setfsuid = 151,
1075 aarch64_sys_setfsgid = 152,
1076 aarch64_sys_times = 153,
1077 aarch64_sys_setpgid = 154,
1078 aarch64_sys_getpgid = 155,
1079 aarch64_sys_getsid = 156,
1080 aarch64_sys_setsid = 157,
1081 aarch64_sys_getgroups = 158,
1082 aarch64_sys_setgroups = 159,
1083 aarch64_sys_uname = 160,
1084 aarch64_sys_sethostname = 161,
1085 aarch64_sys_setdomainname = 162,
1086 aarch64_sys_getrlimit = 163,
1087 aarch64_sys_setrlimit = 164,
1088 aarch64_sys_getrusage = 165,
1089 aarch64_sys_umask = 166,
1090 aarch64_sys_prctl = 167,
1091 aarch64_sys_getcpu = 168,
1092 aarch64_sys_gettimeofday = 169,
1093 aarch64_sys_settimeofday = 170,
1094 aarch64_sys_adjtimex = 171,
1095 aarch64_sys_getpid = 172,
1096 aarch64_sys_getppid = 173,
1097 aarch64_sys_getuid = 174,
1098 aarch64_sys_geteuid = 175,
1099 aarch64_sys_getgid = 176,
1100 aarch64_sys_getegid = 177,
1101 aarch64_sys_gettid = 178,
1102 aarch64_sys_sysinfo = 179,
1103 aarch64_sys_mq_open = 180,
1104 aarch64_sys_mq_unlink = 181,
1105 aarch64_sys_mq_timedsend = 182,
1106 aarch64_sys_mq_timedreceive = 183,
1107 aarch64_sys_mq_notify = 184,
1108 aarch64_sys_mq_getsetattr = 185,
1109 aarch64_sys_msgget = 186,
1110 aarch64_sys_msgctl = 187,
1111 aarch64_sys_msgrcv = 188,
1112 aarch64_sys_msgsnd = 189,
1113 aarch64_sys_semget = 190,
1114 aarch64_sys_semctl = 191,
1115 aarch64_sys_semtimedop = 192,
1116 aarch64_sys_semop = 193,
1117 aarch64_sys_shmget = 194,
1118 aarch64_sys_shmctl = 195,
1119 aarch64_sys_shmat = 196,
1120 aarch64_sys_shmdt = 197,
1121 aarch64_sys_socket = 198,
1122 aarch64_sys_socketpair = 199,
1123 aarch64_sys_bind = 200,
1124 aarch64_sys_listen = 201,
1125 aarch64_sys_accept = 202,
1126 aarch64_sys_connect = 203,
1127 aarch64_sys_getsockname = 204,
1128 aarch64_sys_getpeername = 205,
1129 aarch64_sys_sendto = 206,
1130 aarch64_sys_recvfrom = 207,
1131 aarch64_sys_setsockopt = 208,
1132 aarch64_sys_getsockopt = 209,
1133 aarch64_sys_shutdown = 210,
1134 aarch64_sys_sendmsg = 211,
1135 aarch64_sys_recvmsg = 212,
1136 aarch64_sys_readahead = 213,
1137 aarch64_sys_brk = 214,
1138 aarch64_sys_munmap = 215,
1139 aarch64_sys_mremap = 216,
1140 aarch64_sys_add_key = 217,
1141 aarch64_sys_request_key = 218,
1142 aarch64_sys_keyctl = 219,
1143 aarch64_sys_clone = 220,
1144 aarch64_sys_execve = 221,
1145 aarch64_sys_mmap = 222,
1146 aarch64_sys_fadvise64 = 223,
1147 aarch64_sys_swapon = 224,
1148 aarch64_sys_swapoff = 225,
1149 aarch64_sys_mprotect = 226,
1150 aarch64_sys_msync = 227,
1151 aarch64_sys_mlock = 228,
1152 aarch64_sys_munlock = 229,
1153 aarch64_sys_mlockall = 230,
1154 aarch64_sys_munlockall = 231,
1155 aarch64_sys_mincore = 232,
1156 aarch64_sys_madvise = 233,
1157 aarch64_sys_remap_file_pages = 234,
1158 aarch64_sys_mbind = 235,
1159 aarch64_sys_get_mempolicy = 236,
1160 aarch64_sys_set_mempolicy = 237,
1161 aarch64_sys_migrate_pages = 238,
1162 aarch64_sys_move_pages = 239,
1163 aarch64_sys_rt_tgsigqueueinfo = 240,
1164 aarch64_sys_perf_event_open = 241,
1165 aarch64_sys_accept4 = 242,
1166 aarch64_sys_recvmmsg = 243,
1167 aarch64_sys_wait4 = 260,
1168 aarch64_sys_prlimit64 = 261,
1169 aarch64_sys_fanotify_init = 262,
1170 aarch64_sys_fanotify_mark = 263,
1171 aarch64_sys_name_to_handle_at = 264,
1172 aarch64_sys_open_by_handle_at = 265,
1173 aarch64_sys_clock_adjtime = 266,
1174 aarch64_sys_syncfs = 267,
1175 aarch64_sys_setns = 268,
1176 aarch64_sys_sendmmsg = 269,
1177 aarch64_sys_process_vm_readv = 270,
1178 aarch64_sys_process_vm_writev = 271,
1179 aarch64_sys_kcmp = 272,
1180 aarch64_sys_finit_module = 273,
1181 aarch64_sys_sched_setattr = 274,
1182 aarch64_sys_sched_getattr = 275,
1183 aarch64_sys_getrandom = 278
1184 };
1185
1186 /* aarch64_canonicalize_syscall maps syscall ids from the native AArch64
1187 linux set of syscall ids into a canonical set of syscall ids used by
1188 process record. */
1189
1190 static enum gdb_syscall
1191 aarch64_canonicalize_syscall (enum aarch64_syscall syscall_number)
1192 {
1193 #define SYSCALL_MAP(SYSCALL) case aarch64_sys_##SYSCALL: \
1194 return gdb_sys_##SYSCALL
1195
1196 #define UNSUPPORTED_SYSCALL_MAP(SYSCALL) case aarch64_sys_##SYSCALL: \
1197 return gdb_sys_no_syscall
1198
1199 switch (syscall_number)
1200 {
1201 SYSCALL_MAP (io_setup);
1202 SYSCALL_MAP (io_destroy);
1203 SYSCALL_MAP (io_submit);
1204 SYSCALL_MAP (io_cancel);
1205 SYSCALL_MAP (io_getevents);
1206
1207 SYSCALL_MAP (setxattr);
1208 SYSCALL_MAP (lsetxattr);
1209 SYSCALL_MAP (fsetxattr);
1210 SYSCALL_MAP (getxattr);
1211 SYSCALL_MAP (lgetxattr);
1212 SYSCALL_MAP (fgetxattr);
1213 SYSCALL_MAP (listxattr);
1214 SYSCALL_MAP (llistxattr);
1215 SYSCALL_MAP (flistxattr);
1216 SYSCALL_MAP (removexattr);
1217 SYSCALL_MAP (lremovexattr);
1218 SYSCALL_MAP (fremovexattr);
1219 SYSCALL_MAP (getcwd);
1220 SYSCALL_MAP (lookup_dcookie);
1221 SYSCALL_MAP (eventfd2);
1222 SYSCALL_MAP (epoll_create1);
1223 SYSCALL_MAP (epoll_ctl);
1224 SYSCALL_MAP (epoll_pwait);
1225 SYSCALL_MAP (dup);
1226 SYSCALL_MAP (dup3);
1227 SYSCALL_MAP (fcntl);
1228 SYSCALL_MAP (inotify_init1);
1229 SYSCALL_MAP (inotify_add_watch);
1230 SYSCALL_MAP (inotify_rm_watch);
1231 SYSCALL_MAP (ioctl);
1232 SYSCALL_MAP (ioprio_set);
1233 SYSCALL_MAP (ioprio_get);
1234 SYSCALL_MAP (flock);
1235 SYSCALL_MAP (mknodat);
1236 SYSCALL_MAP (mkdirat);
1237 SYSCALL_MAP (unlinkat);
1238 SYSCALL_MAP (symlinkat);
1239 SYSCALL_MAP (linkat);
1240 SYSCALL_MAP (renameat);
1241 UNSUPPORTED_SYSCALL_MAP (umount2);
1242 SYSCALL_MAP (mount);
1243 SYSCALL_MAP (pivot_root);
1244 SYSCALL_MAP (nfsservctl);
1245 SYSCALL_MAP (statfs);
1246 SYSCALL_MAP (truncate);
1247 SYSCALL_MAP (ftruncate);
1248 SYSCALL_MAP (fallocate);
1249 SYSCALL_MAP (faccessat);
1250 SYSCALL_MAP (fchdir);
1251 SYSCALL_MAP (chroot);
1252 SYSCALL_MAP (fchmod);
1253 SYSCALL_MAP (fchmodat);
1254 SYSCALL_MAP (fchownat);
1255 SYSCALL_MAP (fchown);
1256 SYSCALL_MAP (openat);
1257 SYSCALL_MAP (close);
1258 SYSCALL_MAP (vhangup);
1259 SYSCALL_MAP (pipe2);
1260 SYSCALL_MAP (quotactl);
1261 SYSCALL_MAP (getdents64);
1262 SYSCALL_MAP (lseek);
1263 SYSCALL_MAP (read);
1264 SYSCALL_MAP (write);
1265 SYSCALL_MAP (readv);
1266 SYSCALL_MAP (writev);
1267 SYSCALL_MAP (pread64);
1268 SYSCALL_MAP (pwrite64);
1269 UNSUPPORTED_SYSCALL_MAP (preadv);
1270 UNSUPPORTED_SYSCALL_MAP (pwritev);
1271 SYSCALL_MAP (sendfile);
1272 SYSCALL_MAP (pselect6);
1273 SYSCALL_MAP (ppoll);
1274 UNSUPPORTED_SYSCALL_MAP (signalfd4);
1275 SYSCALL_MAP (vmsplice);
1276 SYSCALL_MAP (splice);
1277 SYSCALL_MAP (tee);
1278 SYSCALL_MAP (readlinkat);
1279 SYSCALL_MAP (newfstatat);
1280
1281 SYSCALL_MAP (fstat);
1282 SYSCALL_MAP (sync);
1283 SYSCALL_MAP (fsync);
1284 SYSCALL_MAP (fdatasync);
1285 SYSCALL_MAP (sync_file_range);
1286 UNSUPPORTED_SYSCALL_MAP (timerfd_create);
1287 UNSUPPORTED_SYSCALL_MAP (timerfd_settime);
1288 UNSUPPORTED_SYSCALL_MAP (timerfd_gettime);
1289 UNSUPPORTED_SYSCALL_MAP (utimensat);
1290 SYSCALL_MAP (acct);
1291 SYSCALL_MAP (capget);
1292 SYSCALL_MAP (capset);
1293 SYSCALL_MAP (personality);
1294 SYSCALL_MAP (exit);
1295 SYSCALL_MAP (exit_group);
1296 SYSCALL_MAP (waitid);
1297 SYSCALL_MAP (set_tid_address);
1298 SYSCALL_MAP (unshare);
1299 SYSCALL_MAP (futex);
1300 SYSCALL_MAP (set_robust_list);
1301 SYSCALL_MAP (get_robust_list);
1302 SYSCALL_MAP (nanosleep);
1303
1304 SYSCALL_MAP (getitimer);
1305 SYSCALL_MAP (setitimer);
1306 SYSCALL_MAP (kexec_load);
1307 SYSCALL_MAP (init_module);
1308 SYSCALL_MAP (delete_module);
1309 SYSCALL_MAP (timer_create);
1310 SYSCALL_MAP (timer_settime);
1311 SYSCALL_MAP (timer_gettime);
1312 SYSCALL_MAP (timer_getoverrun);
1313 SYSCALL_MAP (timer_delete);
1314 SYSCALL_MAP (clock_settime);
1315 SYSCALL_MAP (clock_gettime);
1316 SYSCALL_MAP (clock_getres);
1317 SYSCALL_MAP (clock_nanosleep);
1318 SYSCALL_MAP (syslog);
1319 SYSCALL_MAP (ptrace);
1320 SYSCALL_MAP (sched_setparam);
1321 SYSCALL_MAP (sched_setscheduler);
1322 SYSCALL_MAP (sched_getscheduler);
1323 SYSCALL_MAP (sched_getparam);
1324 SYSCALL_MAP (sched_setaffinity);
1325 SYSCALL_MAP (sched_getaffinity);
1326 SYSCALL_MAP (sched_yield);
1327 SYSCALL_MAP (sched_get_priority_max);
1328 SYSCALL_MAP (sched_get_priority_min);
1329 SYSCALL_MAP (sched_rr_get_interval);
1330 SYSCALL_MAP (kill);
1331 SYSCALL_MAP (tkill);
1332 SYSCALL_MAP (tgkill);
1333 SYSCALL_MAP (sigaltstack);
1334 SYSCALL_MAP (rt_sigsuspend);
1335 SYSCALL_MAP (rt_sigaction);
1336 SYSCALL_MAP (rt_sigprocmask);
1337 SYSCALL_MAP (rt_sigpending);
1338 SYSCALL_MAP (rt_sigtimedwait);
1339 SYSCALL_MAP (rt_sigqueueinfo);
1340 SYSCALL_MAP (rt_sigreturn);
1341 SYSCALL_MAP (setpriority);
1342 SYSCALL_MAP (getpriority);
1343 SYSCALL_MAP (reboot);
1344 SYSCALL_MAP (setregid);
1345 SYSCALL_MAP (setgid);
1346 SYSCALL_MAP (setreuid);
1347 SYSCALL_MAP (setuid);
1348 SYSCALL_MAP (setresuid);
1349 SYSCALL_MAP (getresuid);
1350 SYSCALL_MAP (setresgid);
1351 SYSCALL_MAP (getresgid);
1352 SYSCALL_MAP (setfsuid);
1353 SYSCALL_MAP (setfsgid);
1354 SYSCALL_MAP (times);
1355 SYSCALL_MAP (setpgid);
1356 SYSCALL_MAP (getpgid);
1357 SYSCALL_MAP (getsid);
1358 SYSCALL_MAP (setsid);
1359 SYSCALL_MAP (getgroups);
1360 SYSCALL_MAP (setgroups);
1361 SYSCALL_MAP (uname);
1362 SYSCALL_MAP (sethostname);
1363 SYSCALL_MAP (setdomainname);
1364 SYSCALL_MAP (getrlimit);
1365 SYSCALL_MAP (setrlimit);
1366 SYSCALL_MAP (getrusage);
1367 SYSCALL_MAP (umask);
1368 SYSCALL_MAP (prctl);
1369 SYSCALL_MAP (getcpu);
1370 SYSCALL_MAP (gettimeofday);
1371 SYSCALL_MAP (settimeofday);
1372 SYSCALL_MAP (adjtimex);
1373 SYSCALL_MAP (getpid);
1374 SYSCALL_MAP (getppid);
1375 SYSCALL_MAP (getuid);
1376 SYSCALL_MAP (geteuid);
1377 SYSCALL_MAP (getgid);
1378 SYSCALL_MAP (getegid);
1379 SYSCALL_MAP (gettid);
1380 SYSCALL_MAP (sysinfo);
1381 SYSCALL_MAP (mq_open);
1382 SYSCALL_MAP (mq_unlink);
1383 SYSCALL_MAP (mq_timedsend);
1384 SYSCALL_MAP (mq_timedreceive);
1385 SYSCALL_MAP (mq_notify);
1386 SYSCALL_MAP (mq_getsetattr);
1387 SYSCALL_MAP (msgget);
1388 SYSCALL_MAP (msgctl);
1389 SYSCALL_MAP (msgrcv);
1390 SYSCALL_MAP (msgsnd);
1391 SYSCALL_MAP (semget);
1392 SYSCALL_MAP (semctl);
1393 SYSCALL_MAP (semtimedop);
1394 SYSCALL_MAP (semop);
1395 SYSCALL_MAP (shmget);
1396 SYSCALL_MAP (shmctl);
1397 SYSCALL_MAP (shmat);
1398 SYSCALL_MAP (shmdt);
1399 SYSCALL_MAP (socket);
1400 SYSCALL_MAP (socketpair);
1401 SYSCALL_MAP (bind);
1402 SYSCALL_MAP (listen);
1403 SYSCALL_MAP (accept);
1404 SYSCALL_MAP (connect);
1405 SYSCALL_MAP (getsockname);
1406 SYSCALL_MAP (getpeername);
1407 SYSCALL_MAP (sendto);
1408 SYSCALL_MAP (recvfrom);
1409 SYSCALL_MAP (setsockopt);
1410 SYSCALL_MAP (getsockopt);
1411 SYSCALL_MAP (shutdown);
1412 SYSCALL_MAP (sendmsg);
1413 SYSCALL_MAP (recvmsg);
1414 SYSCALL_MAP (readahead);
1415 SYSCALL_MAP (brk);
1416 SYSCALL_MAP (munmap);
1417 SYSCALL_MAP (mremap);
1418 SYSCALL_MAP (add_key);
1419 SYSCALL_MAP (request_key);
1420 SYSCALL_MAP (keyctl);
1421 SYSCALL_MAP (clone);
1422 SYSCALL_MAP (execve);
1423
1424 case aarch64_sys_mmap:
1425 return gdb_sys_mmap2;
1426
1427 SYSCALL_MAP (fadvise64);
1428 SYSCALL_MAP (swapon);
1429 SYSCALL_MAP (swapoff);
1430 SYSCALL_MAP (mprotect);
1431 SYSCALL_MAP (msync);
1432 SYSCALL_MAP (mlock);
1433 SYSCALL_MAP (munlock);
1434 SYSCALL_MAP (mlockall);
1435 SYSCALL_MAP (munlockall);
1436 SYSCALL_MAP (mincore);
1437 SYSCALL_MAP (madvise);
1438 SYSCALL_MAP (remap_file_pages);
1439 SYSCALL_MAP (mbind);
1440 SYSCALL_MAP (get_mempolicy);
1441 SYSCALL_MAP (set_mempolicy);
1442 SYSCALL_MAP (migrate_pages);
1443 SYSCALL_MAP (move_pages);
1444 UNSUPPORTED_SYSCALL_MAP (rt_tgsigqueueinfo);
1445 UNSUPPORTED_SYSCALL_MAP (perf_event_open);
1446 UNSUPPORTED_SYSCALL_MAP (accept4);
1447 UNSUPPORTED_SYSCALL_MAP (recvmmsg);
1448
1449 SYSCALL_MAP (wait4);
1450
1451 UNSUPPORTED_SYSCALL_MAP (prlimit64);
1452 UNSUPPORTED_SYSCALL_MAP (fanotify_init);
1453 UNSUPPORTED_SYSCALL_MAP (fanotify_mark);
1454 UNSUPPORTED_SYSCALL_MAP (name_to_handle_at);
1455 UNSUPPORTED_SYSCALL_MAP (open_by_handle_at);
1456 UNSUPPORTED_SYSCALL_MAP (clock_adjtime);
1457 UNSUPPORTED_SYSCALL_MAP (syncfs);
1458 UNSUPPORTED_SYSCALL_MAP (setns);
1459 UNSUPPORTED_SYSCALL_MAP (sendmmsg);
1460 UNSUPPORTED_SYSCALL_MAP (process_vm_readv);
1461 UNSUPPORTED_SYSCALL_MAP (process_vm_writev);
1462 UNSUPPORTED_SYSCALL_MAP (kcmp);
1463 UNSUPPORTED_SYSCALL_MAP (finit_module);
1464 UNSUPPORTED_SYSCALL_MAP (sched_setattr);
1465 UNSUPPORTED_SYSCALL_MAP (sched_getattr);
1466 SYSCALL_MAP (getrandom);
1467 default:
1468 return gdb_sys_no_syscall;
1469 }
1470 }
1471
1472 /* Retrieve the syscall number at a ptrace syscall-stop, either on syscall entry
1473 or exit. Return -1 upon error. */
1474
1475 static LONGEST
1476 aarch64_linux_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1477 {
1478 struct regcache *regs = get_thread_regcache (thread);
1479 LONGEST ret;
1480
1481 /* Get the system call number from register x8. */
1482 regs->cooked_read (AARCH64_X0_REGNUM + 8, &ret);
1483
1484 /* On exit from a successful execve, we will be in a new process and all the
1485 registers will be cleared - x0 to x30 will be 0, except for a 1 in x7.
1486 This function will only ever get called when stopped at the entry or exit
1487 of a syscall, so by checking for 0 in x0 (arg0/retval), x1 (arg1), x8
1488 (syscall), x29 (FP) and x30 (LR) we can infer:
1489 1) Either inferior is at exit from successful execve.
1490 2) Or inferior is at entry to a call to io_setup with invalid arguments and
1491 a corrupted FP and LR.
1492 It should be safe enough to assume case 1. */
1493 if (ret == 0)
1494 {
1495 LONGEST x1 = -1, fp = -1, lr = -1;
1496 regs->cooked_read (AARCH64_X0_REGNUM + 1, &x1);
1497 regs->cooked_read (AARCH64_FP_REGNUM, &fp);
1498 regs->cooked_read (AARCH64_LR_REGNUM, &lr);
1499 if (x1 == 0 && fp ==0 && lr == 0)
1500 return aarch64_sys_execve;
1501 }
1502
1503 return ret;
1504 }
1505
1506 /* Record all registers but PC register for process-record. */
1507
1508 static int
1509 aarch64_all_but_pc_registers_record (struct regcache *regcache)
1510 {
1511 int i;
1512
1513 for (i = AARCH64_X0_REGNUM; i < AARCH64_PC_REGNUM; i++)
1514 if (record_full_arch_list_add_reg (regcache, i))
1515 return -1;
1516
1517 if (record_full_arch_list_add_reg (regcache, AARCH64_CPSR_REGNUM))
1518 return -1;
1519
1520 return 0;
1521 }
1522
1523 /* Handler for aarch64 system call instruction recording. */
1524
1525 static int
1526 aarch64_linux_syscall_record (struct regcache *regcache,
1527 unsigned long svc_number)
1528 {
1529 int ret = 0;
1530 enum gdb_syscall syscall_gdb;
1531
1532 syscall_gdb =
1533 aarch64_canonicalize_syscall ((enum aarch64_syscall) svc_number);
1534
1535 if (syscall_gdb < 0)
1536 {
1537 gdb_printf (gdb_stderr,
1538 _("Process record and replay target doesn't "
1539 "support syscall number %s\n"),
1540 plongest (svc_number));
1541 return -1;
1542 }
1543
1544 if (syscall_gdb == gdb_sys_sigreturn
1545 || syscall_gdb == gdb_sys_rt_sigreturn)
1546 {
1547 if (aarch64_all_but_pc_registers_record (regcache))
1548 return -1;
1549 return 0;
1550 }
1551
1552 ret = record_linux_system_call (syscall_gdb, regcache,
1553 &aarch64_linux_record_tdep);
1554 if (ret != 0)
1555 return ret;
1556
1557 /* Record the return value of the system call. */
1558 if (record_full_arch_list_add_reg (regcache, AARCH64_X0_REGNUM))
1559 return -1;
1560 /* Record LR. */
1561 if (record_full_arch_list_add_reg (regcache, AARCH64_LR_REGNUM))
1562 return -1;
1563 /* Record CPSR. */
1564 if (record_full_arch_list_add_reg (regcache, AARCH64_CPSR_REGNUM))
1565 return -1;
1566
1567 return 0;
1568 }
1569
1570 /* Implement the "gcc_target_options" gdbarch method. */
1571
1572 static std::string
1573 aarch64_linux_gcc_target_options (struct gdbarch *gdbarch)
1574 {
1575 /* GCC doesn't know "-m64". */
1576 return {};
1577 }
1578
1579 /* Helper to get the allocation tag from a 64-bit ADDRESS.
1580
1581 Return the allocation tag if successful and nullopt otherwise. */
1582
1583 static gdb::optional<CORE_ADDR>
1584 aarch64_mte_get_atag (CORE_ADDR address)
1585 {
1586 gdb::byte_vector tags;
1587
1588 /* Attempt to fetch the allocation tag. */
1589 if (!target_fetch_memtags (address, 1, tags,
1590 static_cast<int> (memtag_type::allocation)))
1591 return {};
1592
1593 /* Only one tag should've been returned. Make sure we got exactly that. */
1594 if (tags.size () != 1)
1595 error (_("Target returned an unexpected number of tags."));
1596
1597 /* Although our tags are 4 bits in size, they are stored in a
1598 byte. */
1599 return tags[0];
1600 }
1601
1602 /* Implement the tagged_address_p gdbarch method. */
1603
1604 static bool
1605 aarch64_linux_tagged_address_p (struct gdbarch *gdbarch, struct value *address)
1606 {
1607 gdb_assert (address != nullptr);
1608
1609 CORE_ADDR addr = value_as_address (address);
1610
1611 /* Remove the top byte for the memory range check. */
1612 addr = gdbarch_remove_non_address_bits (gdbarch, addr);
1613
1614 /* Check if the page that contains ADDRESS is mapped with PROT_MTE. */
1615 if (!linux_address_in_memtag_page (addr))
1616 return false;
1617
1618 /* We have a valid tag in the top byte of the 64-bit address. */
1619 return true;
1620 }
1621
1622 /* Implement the memtag_matches_p gdbarch method. */
1623
1624 static bool
1625 aarch64_linux_memtag_matches_p (struct gdbarch *gdbarch,
1626 struct value *address)
1627 {
1628 gdb_assert (address != nullptr);
1629
1630 /* Make sure we are dealing with a tagged address to begin with. */
1631 if (!aarch64_linux_tagged_address_p (gdbarch, address))
1632 return true;
1633
1634 CORE_ADDR addr = value_as_address (address);
1635
1636 /* Fetch the allocation tag for ADDRESS. */
1637 gdb::optional<CORE_ADDR> atag
1638 = aarch64_mte_get_atag (gdbarch_remove_non_address_bits (gdbarch, addr));
1639
1640 if (!atag.has_value ())
1641 return true;
1642
1643 /* Fetch the logical tag for ADDRESS. */
1644 gdb_byte ltag = aarch64_mte_get_ltag (addr);
1645
1646 /* Are the tags the same? */
1647 return ltag == *atag;
1648 }
1649
1650 /* Implement the set_memtags gdbarch method. */
1651
1652 static bool
1653 aarch64_linux_set_memtags (struct gdbarch *gdbarch, struct value *address,
1654 size_t length, const gdb::byte_vector &tags,
1655 memtag_type tag_type)
1656 {
1657 gdb_assert (!tags.empty ());
1658 gdb_assert (address != nullptr);
1659
1660 CORE_ADDR addr = value_as_address (address);
1661
1662 /* Set the logical tag or the allocation tag. */
1663 if (tag_type == memtag_type::logical)
1664 {
1665 /* When setting logical tags, we don't care about the length, since
1666 we are only setting a single logical tag. */
1667 addr = aarch64_mte_set_ltag (addr, tags[0]);
1668
1669 /* Update the value's content with the tag. */
1670 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1671 gdb_byte *srcbuf = address->contents_raw ().data ();
1672 store_unsigned_integer (srcbuf, sizeof (addr), byte_order, addr);
1673 }
1674 else
1675 {
1676 /* Remove the top byte. */
1677 addr = gdbarch_remove_non_address_bits (gdbarch, addr);
1678
1679 /* Make sure we are dealing with a tagged address to begin with. */
1680 if (!aarch64_linux_tagged_address_p (gdbarch, address))
1681 return false;
1682
1683 /* With G being the number of tag granules and N the number of tags
1684 passed in, we can have the following cases:
1685
1686 1 - G == N: Store all the N tags to memory.
1687
1688 2 - G < N : Warn about having more tags than granules, but write G
1689 tags.
1690
1691 3 - G > N : This is a "fill tags" operation. We should use the tags
1692 as a pattern to fill the granules repeatedly until we have
1693 written G tags to memory.
1694 */
1695
1696 size_t g = aarch64_mte_get_tag_granules (addr, length,
1697 AARCH64_MTE_GRANULE_SIZE);
1698 size_t n = tags.size ();
1699
1700 if (g < n)
1701 warning (_("Got more tags than memory granules. Tags will be "
1702 "truncated."));
1703 else if (g > n)
1704 warning (_("Using tag pattern to fill memory range."));
1705
1706 if (!target_store_memtags (addr, length, tags,
1707 static_cast<int> (memtag_type::allocation)))
1708 return false;
1709 }
1710 return true;
1711 }
1712
1713 /* Implement the get_memtag gdbarch method. */
1714
1715 static struct value *
1716 aarch64_linux_get_memtag (struct gdbarch *gdbarch, struct value *address,
1717 memtag_type tag_type)
1718 {
1719 gdb_assert (address != nullptr);
1720
1721 CORE_ADDR addr = value_as_address (address);
1722 CORE_ADDR tag = 0;
1723
1724 /* Get the logical tag or the allocation tag. */
1725 if (tag_type == memtag_type::logical)
1726 tag = aarch64_mte_get_ltag (addr);
1727 else
1728 {
1729 /* Make sure we are dealing with a tagged address to begin with. */
1730 if (!aarch64_linux_tagged_address_p (gdbarch, address))
1731 return nullptr;
1732
1733 /* Remove the top byte. */
1734 addr = gdbarch_remove_non_address_bits (gdbarch, addr);
1735 gdb::optional<CORE_ADDR> atag = aarch64_mte_get_atag (addr);
1736
1737 if (!atag.has_value ())
1738 return nullptr;
1739
1740 tag = *atag;
1741 }
1742
1743 /* Convert the tag to a value. */
1744 return value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
1745 tag);
1746 }
1747
1748 /* Implement the memtag_to_string gdbarch method. */
1749
1750 static std::string
1751 aarch64_linux_memtag_to_string (struct gdbarch *gdbarch, struct value *tag_value)
1752 {
1753 if (tag_value == nullptr)
1754 return "";
1755
1756 CORE_ADDR tag = value_as_address (tag_value);
1757
1758 return string_printf ("0x%s", phex_nz (tag, sizeof (tag)));
1759 }
1760
1761 /* AArch64 Linux implementation of the report_signal_info gdbarch
1762 hook. Displays information about possible memory tag violations. */
1763
1764 static void
1765 aarch64_linux_report_signal_info (struct gdbarch *gdbarch,
1766 struct ui_out *uiout,
1767 enum gdb_signal siggnal)
1768 {
1769 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1770
1771 if (!tdep->has_mte () || siggnal != GDB_SIGNAL_SEGV)
1772 return;
1773
1774 CORE_ADDR fault_addr = 0;
1775 long si_code = 0;
1776
1777 try
1778 {
1779 /* Sigcode tells us if the segfault is actually a memory tag
1780 violation. */
1781 si_code = parse_and_eval_long ("$_siginfo.si_code");
1782
1783 fault_addr
1784 = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
1785 }
1786 catch (const gdb_exception_error &exception)
1787 {
1788 exception_print (gdb_stderr, exception);
1789 return;
1790 }
1791
1792 /* If this is not a memory tag violation, just return. */
1793 if (si_code != SEGV_MTEAERR && si_code != SEGV_MTESERR)
1794 return;
1795
1796 uiout->text ("\n");
1797
1798 uiout->field_string ("sigcode-meaning", _("Memory tag violation"));
1799
1800 /* For synchronous faults, show additional information. */
1801 if (si_code == SEGV_MTESERR)
1802 {
1803 uiout->text (_(" while accessing address "));
1804 uiout->field_core_addr ("fault-addr", gdbarch, fault_addr);
1805 uiout->text ("\n");
1806
1807 gdb::optional<CORE_ADDR> atag
1808 = aarch64_mte_get_atag (gdbarch_remove_non_address_bits (gdbarch,
1809 fault_addr));
1810 gdb_byte ltag = aarch64_mte_get_ltag (fault_addr);
1811
1812 if (!atag.has_value ())
1813 uiout->text (_("Allocation tag unavailable"));
1814 else
1815 {
1816 uiout->text (_("Allocation tag "));
1817 uiout->field_string ("allocation-tag", hex_string (*atag));
1818 uiout->text ("\n");
1819 uiout->text (_("Logical tag "));
1820 uiout->field_string ("logical-tag", hex_string (ltag));
1821 }
1822 }
1823 else
1824 {
1825 uiout->text ("\n");
1826 uiout->text (_("Fault address unavailable"));
1827 }
1828 }
1829
1830 /* AArch64 Linux implementation of the gdbarch_create_memtag_section hook. */
1831
1832 static asection *
1833 aarch64_linux_create_memtag_section (struct gdbarch *gdbarch, bfd *obfd,
1834 CORE_ADDR address, size_t size)
1835 {
1836 gdb_assert (obfd != nullptr);
1837 gdb_assert (size > 0);
1838
1839 /* Create the section and associated program header.
1840
1841 Make sure the section's flags has SEC_HAS_CONTENTS, otherwise BFD will
1842 refuse to write data to this section. */
1843 asection *mte_section
1844 = bfd_make_section_anyway_with_flags (obfd, "memtag", SEC_HAS_CONTENTS);
1845
1846 if (mte_section == nullptr)
1847 return nullptr;
1848
1849 bfd_set_section_vma (mte_section, address);
1850 /* The size of the memory range covered by the memory tags. We reuse the
1851 section's rawsize field for this purpose. */
1852 mte_section->rawsize = size;
1853
1854 /* Fetch the number of tags we need to save. */
1855 size_t tags_count
1856 = aarch64_mte_get_tag_granules (address, size, AARCH64_MTE_GRANULE_SIZE);
1857 /* Tags are stored packed as 2 tags per byte. */
1858 bfd_set_section_size (mte_section, (tags_count + 1) >> 1);
1859 /* Store program header information. */
1860 bfd_record_phdr (obfd, PT_AARCH64_MEMTAG_MTE, 1, 0, 0, 0, 0, 0, 1,
1861 &mte_section);
1862
1863 return mte_section;
1864 }
1865
1866 /* Maximum number of tags to request. */
1867 #define MAX_TAGS_TO_TRANSFER 1024
1868
1869 /* AArch64 Linux implementation of the gdbarch_fill_memtag_section hook. */
1870
1871 static bool
1872 aarch64_linux_fill_memtag_section (struct gdbarch *gdbarch, asection *osec)
1873 {
1874 /* We only handle MTE tags for now. */
1875
1876 size_t segment_size = osec->rawsize;
1877 CORE_ADDR start_address = bfd_section_vma (osec);
1878 CORE_ADDR end_address = start_address + segment_size;
1879
1880 /* Figure out how many tags we need to store in this memory range. */
1881 size_t granules = aarch64_mte_get_tag_granules (start_address, segment_size,
1882 AARCH64_MTE_GRANULE_SIZE);
1883
1884 /* If there are no tag granules to fetch, just return. */
1885 if (granules == 0)
1886 return true;
1887
1888 CORE_ADDR address = start_address;
1889
1890 /* Vector of tags. */
1891 gdb::byte_vector tags;
1892
1893 while (granules > 0)
1894 {
1895 /* Transfer tags in chunks. */
1896 gdb::byte_vector tags_read;
1897 size_t xfer_len
1898 = ((granules >= MAX_TAGS_TO_TRANSFER)
1899 ? MAX_TAGS_TO_TRANSFER * AARCH64_MTE_GRANULE_SIZE
1900 : granules * AARCH64_MTE_GRANULE_SIZE);
1901
1902 if (!target_fetch_memtags (address, xfer_len, tags_read,
1903 static_cast<int> (memtag_type::allocation)))
1904 {
1905 warning (_("Failed to read MTE tags from memory range [%s,%s)."),
1906 phex_nz (start_address, sizeof (start_address)),
1907 phex_nz (end_address, sizeof (end_address)));
1908 return false;
1909 }
1910
1911 /* Transfer over the tags that have been read. */
1912 tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
1913
1914 /* Adjust the remaining granules and starting address. */
1915 granules -= tags_read.size ();
1916 address += tags_read.size () * AARCH64_MTE_GRANULE_SIZE;
1917 }
1918
1919 /* Pack the MTE tag bits. */
1920 aarch64_mte_pack_tags (tags);
1921
1922 if (!bfd_set_section_contents (osec->owner, osec, tags.data (),
1923 0, tags.size ()))
1924 {
1925 warning (_("Failed to write %s bytes of corefile memory "
1926 "tag content (%s)."),
1927 pulongest (tags.size ()),
1928 bfd_errmsg (bfd_get_error ()));
1929 }
1930 return true;
1931 }
1932
1933 /* AArch64 Linux implementation of the gdbarch_decode_memtag_section
1934 hook. Decode a memory tag section and return the requested tags.
1935
1936 The section is guaranteed to cover the [ADDRESS, ADDRESS + length)
1937 range. */
1938
1939 static gdb::byte_vector
1940 aarch64_linux_decode_memtag_section (struct gdbarch *gdbarch,
1941 bfd_section *section,
1942 int type,
1943 CORE_ADDR address, size_t length)
1944 {
1945 gdb_assert (section != nullptr);
1946
1947 /* The requested address must not be less than section->vma. */
1948 gdb_assert (section->vma <= address);
1949
1950 /* Figure out how many tags we need to fetch in this memory range. */
1951 size_t granules = aarch64_mte_get_tag_granules (address, length,
1952 AARCH64_MTE_GRANULE_SIZE);
1953 /* Sanity check. */
1954 gdb_assert (granules > 0);
1955
1956 /* Fetch the total number of tags in the range [VMA, address + length). */
1957 size_t granules_from_vma
1958 = aarch64_mte_get_tag_granules (section->vma,
1959 address - section->vma + length,
1960 AARCH64_MTE_GRANULE_SIZE);
1961
1962 /* Adjust the tags vector to contain the exact number of packed bytes. */
1963 gdb::byte_vector tags (((granules - 1) >> 1) + 1);
1964
1965 /* Figure out the starting offset into the packed tags data. */
1966 file_ptr offset = ((granules_from_vma - granules) >> 1);
1967
1968 if (!bfd_get_section_contents (section->owner, section, tags.data (),
1969 offset, tags.size ()))
1970 error (_("Couldn't read contents from memtag section."));
1971
1972 /* At this point, the tags are packed 2 per byte. Unpack them before
1973 returning. */
1974 bool skip_first = ((granules_from_vma - granules) % 2) != 0;
1975 aarch64_mte_unpack_tags (tags, skip_first);
1976
1977 /* Resize to the exact number of tags that was requested. */
1978 tags.resize (granules);
1979
1980 return tags;
1981 }
1982
1983 static void
1984 aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1985 {
1986 static const char *const stap_integer_prefixes[] = { "#", "", NULL };
1987 static const char *const stap_register_prefixes[] = { "", NULL };
1988 static const char *const stap_register_indirection_prefixes[] = { "[",
1989 NULL };
1990 static const char *const stap_register_indirection_suffixes[] = { "]",
1991 NULL };
1992 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1993
1994 tdep->lowest_pc = 0x8000;
1995
1996 linux_init_abi (info, gdbarch, 1);
1997
1998 set_solib_svr4_fetch_link_map_offsets (gdbarch,
1999 linux_lp64_fetch_link_map_offsets);
2000
2001 /* Enable TLS support. */
2002 set_gdbarch_fetch_tls_load_module_address (gdbarch,
2003 svr4_fetch_objfile_link_map);
2004
2005 /* Shared library handling. */
2006 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2007 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
2008
2009 tramp_frame_prepend_unwinder (gdbarch, &aarch64_linux_rt_sigframe);
2010
2011 /* Enable longjmp. */
2012 tdep->jb_pc = 11;
2013
2014 set_gdbarch_iterate_over_regset_sections
2015 (gdbarch, aarch64_linux_iterate_over_regset_sections);
2016 set_gdbarch_core_read_description
2017 (gdbarch, aarch64_linux_core_read_description);
2018
2019 /* SystemTap related. */
2020 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
2021 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
2022 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
2023 stap_register_indirection_prefixes);
2024 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
2025 stap_register_indirection_suffixes);
2026 set_gdbarch_stap_is_single_operand (gdbarch, aarch64_stap_is_single_operand);
2027 set_gdbarch_stap_parse_special_token (gdbarch,
2028 aarch64_stap_parse_special_token);
2029
2030 /* Reversible debugging, process record. */
2031 set_gdbarch_process_record (gdbarch, aarch64_process_record);
2032 /* Syscall record. */
2033 tdep->aarch64_syscall_record = aarch64_linux_syscall_record;
2034
2035 /* MTE-specific settings and hooks. */
2036 if (tdep->has_mte ())
2037 {
2038 /* Register a hook for checking if an address is tagged or not. */
2039 set_gdbarch_tagged_address_p (gdbarch, aarch64_linux_tagged_address_p);
2040
2041 /* Register a hook for checking if there is a memory tag match. */
2042 set_gdbarch_memtag_matches_p (gdbarch,
2043 aarch64_linux_memtag_matches_p);
2044
2045 /* Register a hook for setting the logical/allocation tags for
2046 a range of addresses. */
2047 set_gdbarch_set_memtags (gdbarch, aarch64_linux_set_memtags);
2048
2049 /* Register a hook for extracting the logical/allocation tag from an
2050 address. */
2051 set_gdbarch_get_memtag (gdbarch, aarch64_linux_get_memtag);
2052
2053 /* Set the allocation tag granule size to 16 bytes. */
2054 set_gdbarch_memtag_granule_size (gdbarch, AARCH64_MTE_GRANULE_SIZE);
2055
2056 /* Register a hook for converting a memory tag to a string. */
2057 set_gdbarch_memtag_to_string (gdbarch, aarch64_linux_memtag_to_string);
2058
2059 set_gdbarch_report_signal_info (gdbarch,
2060 aarch64_linux_report_signal_info);
2061
2062 /* Core file helpers. */
2063
2064 /* Core file helper to create a memory tag section for a particular
2065 PT_LOAD segment. */
2066 set_gdbarch_create_memtag_section
2067 (gdbarch, aarch64_linux_create_memtag_section);
2068
2069 /* Core file helper to fill a memory tag section with tag data. */
2070 set_gdbarch_fill_memtag_section
2071 (gdbarch, aarch64_linux_fill_memtag_section);
2072
2073 /* Core file helper to decode a memory tag section. */
2074 set_gdbarch_decode_memtag_section (gdbarch,
2075 aarch64_linux_decode_memtag_section);
2076 }
2077
2078 /* Initialize the aarch64_linux_record_tdep. */
2079 /* These values are the size of the type that will be used in a system
2080 call. They are obtained from Linux Kernel source. */
2081 aarch64_linux_record_tdep.size_pointer
2082 = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
2083 aarch64_linux_record_tdep.size__old_kernel_stat = 32;
2084 aarch64_linux_record_tdep.size_tms = 32;
2085 aarch64_linux_record_tdep.size_loff_t = 8;
2086 aarch64_linux_record_tdep.size_flock = 32;
2087 aarch64_linux_record_tdep.size_oldold_utsname = 45;
2088 aarch64_linux_record_tdep.size_ustat = 32;
2089 aarch64_linux_record_tdep.size_old_sigaction = 32;
2090 aarch64_linux_record_tdep.size_old_sigset_t = 8;
2091 aarch64_linux_record_tdep.size_rlimit = 16;
2092 aarch64_linux_record_tdep.size_rusage = 144;
2093 aarch64_linux_record_tdep.size_timeval = 16;
2094 aarch64_linux_record_tdep.size_timezone = 8;
2095 aarch64_linux_record_tdep.size_old_gid_t = 2;
2096 aarch64_linux_record_tdep.size_old_uid_t = 2;
2097 aarch64_linux_record_tdep.size_fd_set = 128;
2098 aarch64_linux_record_tdep.size_old_dirent = 280;
2099 aarch64_linux_record_tdep.size_statfs = 120;
2100 aarch64_linux_record_tdep.size_statfs64 = 120;
2101 aarch64_linux_record_tdep.size_sockaddr = 16;
2102 aarch64_linux_record_tdep.size_int
2103 = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
2104 aarch64_linux_record_tdep.size_long
2105 = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
2106 aarch64_linux_record_tdep.size_ulong
2107 = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
2108 aarch64_linux_record_tdep.size_msghdr = 56;
2109 aarch64_linux_record_tdep.size_itimerval = 32;
2110 aarch64_linux_record_tdep.size_stat = 144;
2111 aarch64_linux_record_tdep.size_old_utsname = 325;
2112 aarch64_linux_record_tdep.size_sysinfo = 112;
2113 aarch64_linux_record_tdep.size_msqid_ds = 120;
2114 aarch64_linux_record_tdep.size_shmid_ds = 112;
2115 aarch64_linux_record_tdep.size_new_utsname = 390;
2116 aarch64_linux_record_tdep.size_timex = 208;
2117 aarch64_linux_record_tdep.size_mem_dqinfo = 24;
2118 aarch64_linux_record_tdep.size_if_dqblk = 72;
2119 aarch64_linux_record_tdep.size_fs_quota_stat = 80;
2120 aarch64_linux_record_tdep.size_timespec = 16;
2121 aarch64_linux_record_tdep.size_pollfd = 8;
2122 aarch64_linux_record_tdep.size_NFS_FHSIZE = 32;
2123 aarch64_linux_record_tdep.size_knfsd_fh = 132;
2124 aarch64_linux_record_tdep.size_TASK_COMM_LEN = 16;
2125 aarch64_linux_record_tdep.size_sigaction = 32;
2126 aarch64_linux_record_tdep.size_sigset_t = 8;
2127 aarch64_linux_record_tdep.size_siginfo_t = 128;
2128 aarch64_linux_record_tdep.size_cap_user_data_t = 8;
2129 aarch64_linux_record_tdep.size_stack_t = 24;
2130 aarch64_linux_record_tdep.size_off_t = 8;
2131 aarch64_linux_record_tdep.size_stat64 = 144;
2132 aarch64_linux_record_tdep.size_gid_t = 4;
2133 aarch64_linux_record_tdep.size_uid_t = 4;
2134 aarch64_linux_record_tdep.size_PAGE_SIZE = 4096;
2135 aarch64_linux_record_tdep.size_flock64 = 32;
2136 aarch64_linux_record_tdep.size_user_desc = 16;
2137 aarch64_linux_record_tdep.size_io_event = 32;
2138 aarch64_linux_record_tdep.size_iocb = 64;
2139 aarch64_linux_record_tdep.size_epoll_event = 12;
2140 aarch64_linux_record_tdep.size_itimerspec = 32;
2141 aarch64_linux_record_tdep.size_mq_attr = 64;
2142 aarch64_linux_record_tdep.size_termios = 36;
2143 aarch64_linux_record_tdep.size_termios2 = 44;
2144 aarch64_linux_record_tdep.size_pid_t = 4;
2145 aarch64_linux_record_tdep.size_winsize = 8;
2146 aarch64_linux_record_tdep.size_serial_struct = 72;
2147 aarch64_linux_record_tdep.size_serial_icounter_struct = 80;
2148 aarch64_linux_record_tdep.size_hayes_esp_config = 12;
2149 aarch64_linux_record_tdep.size_size_t = 8;
2150 aarch64_linux_record_tdep.size_iovec = 16;
2151 aarch64_linux_record_tdep.size_time_t = 8;
2152
2153 /* These values are the second argument of system call "sys_ioctl".
2154 They are obtained from Linux Kernel source. */
2155 aarch64_linux_record_tdep.ioctl_TCGETS = 0x5401;
2156 aarch64_linux_record_tdep.ioctl_TCSETS = 0x5402;
2157 aarch64_linux_record_tdep.ioctl_TCSETSW = 0x5403;
2158 aarch64_linux_record_tdep.ioctl_TCSETSF = 0x5404;
2159 aarch64_linux_record_tdep.ioctl_TCGETA = 0x5405;
2160 aarch64_linux_record_tdep.ioctl_TCSETA = 0x5406;
2161 aarch64_linux_record_tdep.ioctl_TCSETAW = 0x5407;
2162 aarch64_linux_record_tdep.ioctl_TCSETAF = 0x5408;
2163 aarch64_linux_record_tdep.ioctl_TCSBRK = 0x5409;
2164 aarch64_linux_record_tdep.ioctl_TCXONC = 0x540a;
2165 aarch64_linux_record_tdep.ioctl_TCFLSH = 0x540b;
2166 aarch64_linux_record_tdep.ioctl_TIOCEXCL = 0x540c;
2167 aarch64_linux_record_tdep.ioctl_TIOCNXCL = 0x540d;
2168 aarch64_linux_record_tdep.ioctl_TIOCSCTTY = 0x540e;
2169 aarch64_linux_record_tdep.ioctl_TIOCGPGRP = 0x540f;
2170 aarch64_linux_record_tdep.ioctl_TIOCSPGRP = 0x5410;
2171 aarch64_linux_record_tdep.ioctl_TIOCOUTQ = 0x5411;
2172 aarch64_linux_record_tdep.ioctl_TIOCSTI = 0x5412;
2173 aarch64_linux_record_tdep.ioctl_TIOCGWINSZ = 0x5413;
2174 aarch64_linux_record_tdep.ioctl_TIOCSWINSZ = 0x5414;
2175 aarch64_linux_record_tdep.ioctl_TIOCMGET = 0x5415;
2176 aarch64_linux_record_tdep.ioctl_TIOCMBIS = 0x5416;
2177 aarch64_linux_record_tdep.ioctl_TIOCMBIC = 0x5417;
2178 aarch64_linux_record_tdep.ioctl_TIOCMSET = 0x5418;
2179 aarch64_linux_record_tdep.ioctl_TIOCGSOFTCAR = 0x5419;
2180 aarch64_linux_record_tdep.ioctl_TIOCSSOFTCAR = 0x541a;
2181 aarch64_linux_record_tdep.ioctl_FIONREAD = 0x541b;
2182 aarch64_linux_record_tdep.ioctl_TIOCINQ = 0x541b;
2183 aarch64_linux_record_tdep.ioctl_TIOCLINUX = 0x541c;
2184 aarch64_linux_record_tdep.ioctl_TIOCCONS = 0x541d;
2185 aarch64_linux_record_tdep.ioctl_TIOCGSERIAL = 0x541e;
2186 aarch64_linux_record_tdep.ioctl_TIOCSSERIAL = 0x541f;
2187 aarch64_linux_record_tdep.ioctl_TIOCPKT = 0x5420;
2188 aarch64_linux_record_tdep.ioctl_FIONBIO = 0x5421;
2189 aarch64_linux_record_tdep.ioctl_TIOCNOTTY = 0x5422;
2190 aarch64_linux_record_tdep.ioctl_TIOCSETD = 0x5423;
2191 aarch64_linux_record_tdep.ioctl_TIOCGETD = 0x5424;
2192 aarch64_linux_record_tdep.ioctl_TCSBRKP = 0x5425;
2193 aarch64_linux_record_tdep.ioctl_TIOCTTYGSTRUCT = 0x5426;
2194 aarch64_linux_record_tdep.ioctl_TIOCSBRK = 0x5427;
2195 aarch64_linux_record_tdep.ioctl_TIOCCBRK = 0x5428;
2196 aarch64_linux_record_tdep.ioctl_TIOCGSID = 0x5429;
2197 aarch64_linux_record_tdep.ioctl_TCGETS2 = 0x802c542a;
2198 aarch64_linux_record_tdep.ioctl_TCSETS2 = 0x402c542b;
2199 aarch64_linux_record_tdep.ioctl_TCSETSW2 = 0x402c542c;
2200 aarch64_linux_record_tdep.ioctl_TCSETSF2 = 0x402c542d;
2201 aarch64_linux_record_tdep.ioctl_TIOCGPTN = 0x80045430;
2202 aarch64_linux_record_tdep.ioctl_TIOCSPTLCK = 0x40045431;
2203 aarch64_linux_record_tdep.ioctl_FIONCLEX = 0x5450;
2204 aarch64_linux_record_tdep.ioctl_FIOCLEX = 0x5451;
2205 aarch64_linux_record_tdep.ioctl_FIOASYNC = 0x5452;
2206 aarch64_linux_record_tdep.ioctl_TIOCSERCONFIG = 0x5453;
2207 aarch64_linux_record_tdep.ioctl_TIOCSERGWILD = 0x5454;
2208 aarch64_linux_record_tdep.ioctl_TIOCSERSWILD = 0x5455;
2209 aarch64_linux_record_tdep.ioctl_TIOCGLCKTRMIOS = 0x5456;
2210 aarch64_linux_record_tdep.ioctl_TIOCSLCKTRMIOS = 0x5457;
2211 aarch64_linux_record_tdep.ioctl_TIOCSERGSTRUCT = 0x5458;
2212 aarch64_linux_record_tdep.ioctl_TIOCSERGETLSR = 0x5459;
2213 aarch64_linux_record_tdep.ioctl_TIOCSERGETMULTI = 0x545a;
2214 aarch64_linux_record_tdep.ioctl_TIOCSERSETMULTI = 0x545b;
2215 aarch64_linux_record_tdep.ioctl_TIOCMIWAIT = 0x545c;
2216 aarch64_linux_record_tdep.ioctl_TIOCGICOUNT = 0x545d;
2217 aarch64_linux_record_tdep.ioctl_TIOCGHAYESESP = 0x545e;
2218 aarch64_linux_record_tdep.ioctl_TIOCSHAYESESP = 0x545f;
2219 aarch64_linux_record_tdep.ioctl_FIOQSIZE = 0x5460;
2220
2221 /* These values are the second argument of system call "sys_fcntl"
2222 and "sys_fcntl64". They are obtained from Linux Kernel source. */
2223 aarch64_linux_record_tdep.fcntl_F_GETLK = 5;
2224 aarch64_linux_record_tdep.fcntl_F_GETLK64 = 12;
2225 aarch64_linux_record_tdep.fcntl_F_SETLK64 = 13;
2226 aarch64_linux_record_tdep.fcntl_F_SETLKW64 = 14;
2227
2228 /* The AArch64 syscall calling convention: reg x0-x6 for arguments,
2229 reg x8 for syscall number and return value in reg x0. */
2230 aarch64_linux_record_tdep.arg1 = AARCH64_X0_REGNUM + 0;
2231 aarch64_linux_record_tdep.arg2 = AARCH64_X0_REGNUM + 1;
2232 aarch64_linux_record_tdep.arg3 = AARCH64_X0_REGNUM + 2;
2233 aarch64_linux_record_tdep.arg4 = AARCH64_X0_REGNUM + 3;
2234 aarch64_linux_record_tdep.arg5 = AARCH64_X0_REGNUM + 4;
2235 aarch64_linux_record_tdep.arg6 = AARCH64_X0_REGNUM + 5;
2236 aarch64_linux_record_tdep.arg7 = AARCH64_X0_REGNUM + 6;
2237
2238 /* `catch syscall' */
2239 set_xml_syscall_file_name (gdbarch, "syscalls/aarch64-linux.xml");
2240 set_gdbarch_get_syscall_number (gdbarch, aarch64_linux_get_syscall_number);
2241
2242 /* Displaced stepping. */
2243 set_gdbarch_max_insn_length (gdbarch, 4);
2244 set_gdbarch_displaced_step_buffer_length
2245 (gdbarch, 4 * AARCH64_DISPLACED_MODIFIED_INSNS);
2246 set_gdbarch_displaced_step_copy_insn (gdbarch,
2247 aarch64_displaced_step_copy_insn);
2248 set_gdbarch_displaced_step_fixup (gdbarch, aarch64_displaced_step_fixup);
2249 set_gdbarch_displaced_step_hw_singlestep (gdbarch,
2250 aarch64_displaced_step_hw_singlestep);
2251
2252 set_gdbarch_gcc_target_options (gdbarch, aarch64_linux_gcc_target_options);
2253 }
2254
2255 #if GDB_SELF_TEST
2256
2257 namespace selftests {
2258
2259 /* Verify functions to read and write logical tags. */
2260
2261 static void
2262 aarch64_linux_ltag_tests (void)
2263 {
2264 /* We have 4 bits of tags, but we test writing all the bits of the top
2265 byte of address. */
2266 for (int i = 0; i < 1 << 8; i++)
2267 {
2268 CORE_ADDR addr = ((CORE_ADDR) i << 56) | 0xdeadbeef;
2269 SELF_CHECK (aarch64_mte_get_ltag (addr) == (i & 0xf));
2270
2271 addr = aarch64_mte_set_ltag (0xdeadbeef, i);
2272 SELF_CHECK (addr = ((CORE_ADDR) (i & 0xf) << 56) | 0xdeadbeef);
2273 }
2274 }
2275
2276 } // namespace selftests
2277 #endif /* GDB_SELF_TEST */
2278
2279 void _initialize_aarch64_linux_tdep ();
2280 void
2281 _initialize_aarch64_linux_tdep ()
2282 {
2283 gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_LINUX,
2284 aarch64_linux_init_abi);
2285
2286 #if GDB_SELF_TEST
2287 selftests::register_test ("aarch64-linux-tagged-address",
2288 selftests::aarch64_linux_ltag_tests);
2289 #endif
2290 }