1 /* Target-dependent code for GNU/Linux AArch64.
2 
3    Copyright (C) 2009-2024 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 
22 #include "extract-store-integer.h"
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 #include "arch/aarch64-scalable-linux.h"
52 
53 #include "arch-utils.h"
54 #include "value.h"
55 
56 #include "gdbsupport/selftest.h"
57 
58 #include "elf/common.h"
59 #include "elf/aarch64.h"
60 #include "arch/aarch64-insn.h"
61 
62 /* For std::pow */
63 #include <cmath>
64 
65 /* Signal frame handling.
66 
67       +------------+  ^
68       | saved lr   |  |
69    +->| saved fp   |--+
70    |  |            |
71    |  |            |
72    |  +------------+
73    |  | saved lr   |
74    +--| saved fp   |
75    ^  |            |
76    |  |            |
77    |  +------------+
78    ^  |            |
79    |  | signal     |
80    |  |            |        SIGTRAMP_FRAME (struct rt_sigframe)
81    |  | saved regs |
82    +--| saved sp   |--> interrupted_sp
83    |  | saved pc   |--> interrupted_pc
84    |  |            |
85    |  +------------+
86    |  | saved lr   |--> default_restorer (movz x8, NR_sys_rt_sigreturn; svc 0)
87    +--| saved fp   |<- FP
88       |            |         NORMAL_FRAME
89       |            |<- SP
90       +------------+
91 
92   On signal delivery, the kernel will create a signal handler stack
93   frame and setup the return address in LR to point at restorer stub.
94   The signal stack frame is defined by:
95 
96   struct rt_sigframe
97   {
98     siginfo_t info;
99     struct ucontext uc;
100   };
101 
102   The ucontext has the following form:
103   struct ucontext
104   {
105     unsigned long uc_flags;
106     struct ucontext *uc_link;
107     stack_t uc_stack;
108     sigset_t uc_sigmask;
109     struct sigcontext uc_mcontext;
110   };
111 
112   struct sigcontext
113   {
114     unsigned long fault_address;
115     unsigned long regs[31];
116     unsigned long sp;                   / * 31 * /
117     unsigned long pc;                   / * 32 * /
118     unsigned long pstate;     / * 33 * /
119     __u8 __reserved[4096]
120   };
121 
122   The reserved space in sigcontext contains additional structures, each starting
123   with a aarch64_ctx, which specifies a unique identifier and the total size of
124   the structure.  The final structure in reserved will start will a null
125   aarch64_ctx.  The penultimate entry in reserved may be a extra_context which
126   then points to a further block of reserved space.
127 
128   struct aarch64_ctx {
129           u32 magic;
130           u32 size;
131   };
132 
133   The restorer stub will always have the form:
134 
135   d28015a8        movz    x8, #0xad
136   d4000001        svc     #0x0
137 
138   This is a system call sys_rt_sigreturn.
139 
140   We detect signal frames by snooping the return code for the restorer
141   instruction sequence.
142 
143   The handler then needs to recover the saved register set from
144   ucontext.uc_mcontext.  */
145 
146 /* These magic numbers need to reflect the layout of the kernel
147    defined struct rt_sigframe and ucontext.  */
148 #define AARCH64_SIGCONTEXT_REG_SIZE             8
149 #define AARCH64_RT_SIGFRAME_UCONTEXT_OFFSET     128
150 #define AARCH64_UCONTEXT_SIGCONTEXT_OFFSET      176
151 #define AARCH64_SIGCONTEXT_XO_OFFSET            8
152 #define AARCH64_SIGCONTEXT_RESERVED_OFFSET      288
153 
154 #define AARCH64_SIGCONTEXT_RESERVED_SIZE          4096
155 
156 /* Unique identifiers that may be used for aarch64_ctx.magic.  */
157 #define AARCH64_EXTRA_MAGIC                       0x45585401
158 #define AARCH64_FPSIMD_MAGIC                      0x46508001
159 #define AARCH64_SVE_MAGIC                         0x53564501
160 #define AARCH64_ZA_MAGIC                          0x54366345
161 #define AARCH64_TPIDR2_MAGIC                      0x54504902
162 #define AARCH64_ZT_MAGIC                          0x5a544e01
163 
164 /* Defines for the extra_context that follows an AARCH64_EXTRA_MAGIC.  */
165 #define AARCH64_EXTRA_DATAP_OFFSET                8
166 
167 /* Defines for the fpsimd that follows an AARCH64_FPSIMD_MAGIC.  */
168 #define AARCH64_FPSIMD_FPSR_OFFSET                8
169 #define AARCH64_FPSIMD_FPCR_OFFSET                12
170 #define AARCH64_FPSIMD_V0_OFFSET                  16
171 #define AARCH64_FPSIMD_VREG_SIZE                  16
172 
173 /* Defines for the sve structure that follows an AARCH64_SVE_MAGIC.  */
174 #define AARCH64_SVE_CONTEXT_VL_OFFSET             8
175 #define AARCH64_SVE_CONTEXT_FLAGS_OFFSET          10
176 #define AARCH64_SVE_CONTEXT_REGS_OFFSET           16
177 #define AARCH64_SVE_CONTEXT_P_REGS_OFFSET(vq) (32 * vq * 16)
178 #define AARCH64_SVE_CONTEXT_FFR_OFFSET(vq) \
179   (AARCH64_SVE_CONTEXT_P_REGS_OFFSET (vq) + (16 * vq * 2))
180 #define AARCH64_SVE_CONTEXT_SIZE(vq) \
181   (AARCH64_SVE_CONTEXT_FFR_OFFSET (vq) + (vq * 2))
182 /* Flag indicating the SVE Context describes streaming mode.  */
183 #define SVE_SIG_FLAG_SM                                     0x1
184 
185 /* SME constants.  */
186 #define AARCH64_SME_CONTEXT_SVL_OFFSET            8
187 #define AARCH64_SME_CONTEXT_REGS_OFFSET           16
188 #define AARCH64_SME_CONTEXT_ZA_SIZE(svq) \
189   ((sve_vl_from_vq (svq) * sve_vl_from_vq (svq)))
190 #define AARCH64_SME_CONTEXT_SIZE(svq) \
191   (AARCH64_SME_CONTEXT_REGS_OFFSET + AARCH64_SME_CONTEXT_ZA_SIZE (svq))
192 
193 /* TPIDR2 register value offset in the TPIDR2 signal frame context.  */
194 #define AARCH64_TPIDR2_CONTEXT_TPIDR2_OFFSET      8
195 
196 /* SME2 (ZT) constants.  */
197 /* Offset of the field containing the number of registers in the SME2 signal
198    context state.  */
199 #define AARCH64_SME2_CONTEXT_NREGS_OFFSET         8
200 /* Offset of the beginning of the register data for the first ZT register in
201    the signal context state.  */
202 #define AARCH64_SME2_CONTEXT_REGS_OFFSET          16
203 
204 /* Holds information about the signal frame.  */
205 struct aarch64_linux_sigframe
206 {
207   /* The stack pointer value.  */
208   CORE_ADDR sp = 0;
209   /* The sigcontext address.  */
210   CORE_ADDR sigcontext_address = 0;
211   /* The start/end signal frame section addresses.  */
212   CORE_ADDR section = 0;
213   CORE_ADDR section_end = 0;
214 
215   /* Starting address of the section containing the general purpose
216      registers.  */
217   CORE_ADDR gpr_section = 0;
218   /* Starting address of the section containing the FPSIMD registers.  */
219   CORE_ADDR fpsimd_section = 0;
220   /* Starting address of the section containing the SVE registers.  */
221   CORE_ADDR sve_section = 0;
222   /* Starting address of the section containing the ZA register.  */
223   CORE_ADDR za_section = 0;
224   /* Starting address of the section containing the TPIDR2 register.  */
225   CORE_ADDR tpidr2_section = 0;
226   /* Starting address of the section containing the ZT registers.  */
227   CORE_ADDR zt_section = 0;
228   /* Starting address of the section containing extra information.  */
229   CORE_ADDR extra_section = 0;
230 
231   /* The vector length (SVE or SSVE).  */
232   ULONGEST vl = 0;
233   /* The streaming vector length (SSVE/ZA).  */
234   ULONGEST svl = 0;
235   /* Number of ZT registers in this context.  */
236   unsigned int zt_register_count = 0;
237 
238   /* True if we are in streaming mode, false otherwise.  */
239   bool streaming_mode = false;
240   /* True if we have a ZA payload, false otherwise.  */
241   bool za_payload = false;
242   /* True if we have a ZT entry in the signal context, false otherwise.  */
243   bool zt_available = false;
244 };
245 
246 /* Read an aarch64_ctx, returning the magic value, and setting *SIZE to the
247    size, or return 0 on error.  */
248 
249 static uint32_t
read_aarch64_ctx(CORE_ADDR ctx_addr,enum bfd_endian byte_order,uint32_t * size)250 read_aarch64_ctx (CORE_ADDR ctx_addr, enum bfd_endian byte_order,
251                       uint32_t *size)
252 {
253   uint32_t magic = 0;
254   gdb_byte buf[4];
255 
256   if (target_read_memory (ctx_addr, buf, 4) != 0)
257     return 0;
258   magic = extract_unsigned_integer (buf, 4, byte_order);
259 
260   if (target_read_memory (ctx_addr + 4, buf, 4) != 0)
261     return 0;
262   *size = extract_unsigned_integer (buf, 4, byte_order);
263 
264   return magic;
265 }
266 
267 /* Given CACHE, use the trad_frame* functions to restore the FPSIMD
268    registers from a signal frame.
269 
270    FPSIMD_CONTEXT is the address of the signal frame context containing FPSIMD
271    data.  */
272 
273 static void
aarch64_linux_restore_vregs(struct gdbarch * gdbarch,struct trad_frame_cache * cache,CORE_ADDR fpsimd_context)274 aarch64_linux_restore_vregs (struct gdbarch *gdbarch,
275                                    struct trad_frame_cache *cache,
276                                    CORE_ADDR fpsimd_context)
277 {
278   /* WARNING: SIMD state is laid out in memory in target-endian format.
279 
280      So we have a couple cases to consider:
281 
282      1 - If the target is big endian, then SIMD state is big endian,
283      requiring a byteswap.
284 
285      2 - If the target is little endian, then SIMD state is little endian, so
286      no byteswap is needed. */
287 
288   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
289   int num_regs = gdbarch_num_regs (gdbarch);
290   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
291 
292   for (int i = 0; i < 32; i++)
293     {
294       CORE_ADDR offset = (fpsimd_context + AARCH64_FPSIMD_V0_OFFSET
295                                 + (i * AARCH64_FPSIMD_VREG_SIZE));
296 
297       gdb_byte buf[V_REGISTER_SIZE];
298 
299       /* Read the contents of the V register.  */
300       if (target_read_memory (offset, buf, V_REGISTER_SIZE))
301           error (_("Failed to read fpsimd register from signal context."));
302 
303       if (byte_order == BFD_ENDIAN_BIG)
304           {
305             size_t size = V_REGISTER_SIZE/2;
306 
307             /* Read the two halves of the V register in reverse byte order.  */
308             CORE_ADDR u64 = extract_unsigned_integer (buf, size,
309                                                                 byte_order);
310             CORE_ADDR l64 = extract_unsigned_integer (buf + size, size,
311                                                                 byte_order);
312 
313             /* Copy the reversed bytes to the buffer.  */
314             store_unsigned_integer (buf, size, BFD_ENDIAN_LITTLE, l64);
315             store_unsigned_integer (buf + size , size, BFD_ENDIAN_LITTLE, u64);
316 
317             /* Now we can store the correct bytes for the V register.  */
318             trad_frame_set_reg_value_bytes (cache, AARCH64_V0_REGNUM + i,
319                                                     {buf, V_REGISTER_SIZE});
320             trad_frame_set_reg_value_bytes (cache,
321                                                     num_regs + AARCH64_Q0_REGNUM
322                                                     + i, {buf, Q_REGISTER_SIZE});
323             trad_frame_set_reg_value_bytes (cache,
324                                                     num_regs + AARCH64_D0_REGNUM
325                                                     + i, {buf, D_REGISTER_SIZE});
326             trad_frame_set_reg_value_bytes (cache,
327                                                     num_regs + AARCH64_S0_REGNUM
328                                                     + i, {buf, S_REGISTER_SIZE});
329             trad_frame_set_reg_value_bytes (cache,
330                                                     num_regs + AARCH64_H0_REGNUM
331                                                     + i, {buf, H_REGISTER_SIZE});
332             trad_frame_set_reg_value_bytes (cache,
333                                                     num_regs + AARCH64_B0_REGNUM
334                                                     + i, {buf, B_REGISTER_SIZE});
335 
336             if (tdep->has_sve ())
337               trad_frame_set_reg_value_bytes (cache,
338                                                       num_regs + AARCH64_SVE_V0_REGNUM
339                                                       + i, {buf, V_REGISTER_SIZE});
340           }
341       else
342           {
343             /* Little endian, just point at the address containing the register
344                value.  */
345             trad_frame_set_reg_addr (cache, AARCH64_V0_REGNUM + i, offset);
346             trad_frame_set_reg_addr (cache, num_regs + AARCH64_Q0_REGNUM + i,
347                                            offset);
348             trad_frame_set_reg_addr (cache, num_regs + AARCH64_D0_REGNUM + i,
349                                            offset);
350             trad_frame_set_reg_addr (cache, num_regs + AARCH64_S0_REGNUM + i,
351                                            offset);
352             trad_frame_set_reg_addr (cache, num_regs + AARCH64_H0_REGNUM + i,
353                                            offset);
354             trad_frame_set_reg_addr (cache, num_regs + AARCH64_B0_REGNUM + i,
355                                            offset);
356 
357             if (tdep->has_sve ())
358               trad_frame_set_reg_addr (cache, num_regs + AARCH64_SVE_V0_REGNUM
359                                              + i, offset);
360           }
361 
362       if (tdep->has_sve ())
363           {
364             /* If SVE is supported for this target, zero out the Z
365                registers then copy the first 16 bytes of each of the V
366                registers to the associated Z register.  Otherwise the Z
367                registers will contain uninitialized data.  */
368             std::vector<gdb_byte> z_buffer (tdep->vq * 16);
369 
370             /* We have already handled the endianness swap above, so we don't need
371                to worry about it here.  */
372             memcpy (z_buffer.data (), buf, V_REGISTER_SIZE);
373             trad_frame_set_reg_value_bytes (cache,
374                                                     AARCH64_SVE_Z0_REGNUM + i,
375                                                     z_buffer);
376           }
377     }
378 }
379 
380 /* Given a signal frame THIS_FRAME, read the signal frame information into
381    SIGNAL_FRAME.  */
382 
383 static void
aarch64_linux_read_signal_frame_info(const frame_info_ptr & this_frame,aarch64_linux_sigframe & signal_frame)384 aarch64_linux_read_signal_frame_info (const frame_info_ptr &this_frame,
385                                               aarch64_linux_sigframe &signal_frame)
386 {
387   signal_frame.sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
388   signal_frame.sigcontext_address
389     = signal_frame.sp + AARCH64_RT_SIGFRAME_UCONTEXT_OFFSET
390       + AARCH64_UCONTEXT_SIGCONTEXT_OFFSET;
391   signal_frame.section
392     = signal_frame.sigcontext_address + AARCH64_SIGCONTEXT_RESERVED_OFFSET;
393   signal_frame.section_end
394     = signal_frame.section + AARCH64_SIGCONTEXT_RESERVED_SIZE;
395 
396   signal_frame.gpr_section
397     = signal_frame.sigcontext_address + AARCH64_SIGCONTEXT_XO_OFFSET;
398 
399   /* Search for all the other sections, stopping at null.  */
400   CORE_ADDR section = signal_frame.section;
401   CORE_ADDR section_end = signal_frame.section_end;
402   uint32_t size, magic;
403   bool extra_found = false;
404   enum bfd_endian byte_order
405     = gdbarch_byte_order (get_frame_arch (this_frame));
406 
407   while ((magic = read_aarch64_ctx (section, byte_order, &size)) != 0
408            && size != 0)
409     {
410       switch (magic)
411           {
412           case AARCH64_FPSIMD_MAGIC:
413             {
414               signal_frame.fpsimd_section = section;
415               section += size;
416               break;
417             }
418 
419           case AARCH64_SVE_MAGIC:
420             {
421               /* Check if the section is followed by a full SVE dump, and set
422                  sve_regs if it is.  */
423               gdb_byte buf[4];
424 
425               /* Extract the vector length.  */
426               if (target_read_memory (section + AARCH64_SVE_CONTEXT_VL_OFFSET,
427                                             buf, 2) != 0)
428                 {
429                     warning (_("Failed to read the vector length from the SVE "
430                                  "signal frame context."));
431                     section += size;
432                     break;
433                 }
434 
435               signal_frame.vl = extract_unsigned_integer (buf, 2, byte_order);
436 
437               /* Extract the flags to check if we are in streaming mode.  */
438               if (target_read_memory (section
439                                             + AARCH64_SVE_CONTEXT_FLAGS_OFFSET,
440                                             buf, 2) != 0)
441                 {
442                     warning (_("Failed to read the flags from the SVE signal frame"
443                                  " context."));
444                     section += size;
445                     break;
446                 }
447 
448               uint16_t flags = extract_unsigned_integer (buf, 2, byte_order);
449 
450               /* Is this SSVE data? If so, we are in streaming mode.  */
451               signal_frame.streaming_mode
452                 = (flags & SVE_SIG_FLAG_SM) ? true : false;
453 
454               ULONGEST vq = sve_vq_from_vl (signal_frame.vl);
455               if (size >= AARCH64_SVE_CONTEXT_SIZE (vq))
456                 {
457                     signal_frame.sve_section
458                       = section + AARCH64_SVE_CONTEXT_REGS_OFFSET;
459                 }
460               section += size;
461               break;
462             }
463 
464           case AARCH64_ZA_MAGIC:
465             {
466               /* Check if the section is followed by a full ZA dump, and set
467                  za_state if it is.  */
468               gdb_byte buf[2];
469 
470               /* Extract the streaming vector length.  */
471               if (target_read_memory (section + AARCH64_SME_CONTEXT_SVL_OFFSET,
472                                             buf, 2) != 0)
473                 {
474                     warning (_("Failed to read the streaming vector length from "
475                                  "ZA signal frame context."));
476                     section += size;
477                     break;
478                 }
479 
480               signal_frame.svl = extract_unsigned_integer (buf, 2, byte_order);
481               ULONGEST svq = sve_vq_from_vl (signal_frame.svl);
482 
483               if (size >= AARCH64_SME_CONTEXT_SIZE (svq))
484                 {
485                     signal_frame.za_section
486                       = section + AARCH64_SME_CONTEXT_REGS_OFFSET;
487                     signal_frame.za_payload = true;
488                 }
489               section += size;
490               break;
491             }
492 
493           case AARCH64_TPIDR2_MAGIC:
494             {
495               /* This is context containing the tpidr2 register.  */
496               signal_frame.tpidr2_section = section;
497               section += size;
498               break;
499             }
500           case AARCH64_ZT_MAGIC:
501             {
502               gdb_byte buf[2];
503 
504               /* Extract the number of ZT registers available in this
505                  context.  */
506               if (target_read_memory (section + AARCH64_SME2_CONTEXT_NREGS_OFFSET,
507                                             buf, 2) != 0)
508                 {
509                     warning (_("Failed to read the number of ZT registers from the "
510                                  "ZT signal frame context."));
511                     section += size;
512                     break;
513                 }
514 
515               signal_frame.zt_register_count
516                 = extract_unsigned_integer (buf, 2, byte_order);
517 
518               /* This is a context containing the ZT registers.  This should only
519                  exist if we also have the ZA context.  The presence of the ZT
520                  context without the ZA context is invalid.  */
521               signal_frame.zt_section = section;
522               signal_frame.zt_available = true;
523 
524               section += size;
525               break;
526             }
527           case AARCH64_EXTRA_MAGIC:
528             {
529               /* Extra is always the last valid section in reserved and points to
530                  an additional block of memory filled with more sections. Reset
531                  the address to the extra section and continue looking for more
532                  structures.  */
533               gdb_byte buf[8];
534 
535               if (target_read_memory (section + AARCH64_EXTRA_DATAP_OFFSET,
536                                             buf, 8) != 0)
537                 {
538                     warning (_("Failed to read the extra section address from the"
539                                  " signal frame context."));
540                     section += size;
541                     break;
542                 }
543 
544               section = extract_unsigned_integer (buf, 8, byte_order);
545               signal_frame.extra_section = section;
546               extra_found = true;
547               break;
548             }
549 
550           default:
551             section += size;
552             break;
553           }
554 
555       /* Prevent searching past the end of the reserved section.  The extra
556            section does not have a hard coded limit - we have to rely on it ending
557            with nulls.  */
558       if (!extra_found && section > section_end)
559           break;
560     }
561 
562     /* Sanity check that if the ZT entry exists, the ZA entry must also
563        exist.  */
564     if (signal_frame.zt_available && !signal_frame.za_payload)
565       error (_("While reading signal context information, found a ZT context "
566                  "without a ZA context, which is invalid."));
567 }
568 
569 /* Implement the "init" method of struct tramp_frame.  */
570 
571 static void
aarch64_linux_sigframe_init(const struct tramp_frame * self,const frame_info_ptr & this_frame,struct trad_frame_cache * this_cache,CORE_ADDR func)572 aarch64_linux_sigframe_init (const struct tramp_frame *self,
573                                    const frame_info_ptr &this_frame,
574                                    struct trad_frame_cache *this_cache,
575                                    CORE_ADDR func)
576 {
577   /* Read the signal context information.  */
578   struct aarch64_linux_sigframe signal_frame;
579   aarch64_linux_read_signal_frame_info (this_frame, signal_frame);
580 
581   /* Now we have all the data required to restore the registers from the
582      signal frame.  */
583 
584   /* Restore the general purpose registers.  */
585   CORE_ADDR offset = signal_frame.gpr_section;
586   for (int i = 0; i < 31; i++)
587     {
588       trad_frame_set_reg_addr (this_cache, AARCH64_X0_REGNUM + i, offset);
589       offset += AARCH64_SIGCONTEXT_REG_SIZE;
590     }
591   trad_frame_set_reg_addr (this_cache, AARCH64_SP_REGNUM, offset);
592   offset += AARCH64_SIGCONTEXT_REG_SIZE;
593   trad_frame_set_reg_addr (this_cache, AARCH64_PC_REGNUM, offset);
594 
595   struct gdbarch *gdbarch = get_frame_arch (this_frame);
596   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
597 
598   /* Restore the SVE / FPSIMD registers.  */
599   if (tdep->has_sve () && signal_frame.sve_section != 0)
600     {
601       ULONGEST vq = sve_vq_from_vl (signal_frame.vl);
602       CORE_ADDR sve_regs = signal_frame.sve_section;
603 
604       /* Restore VG.  */
605       trad_frame_set_reg_value (this_cache, AARCH64_SVE_VG_REGNUM,
606                                         sve_vg_from_vl (signal_frame.vl));
607 
608       int num_regs = gdbarch_num_regs (gdbarch);
609       for (int i = 0; i < 32; i++)
610           {
611             offset = sve_regs + (i * vq * 16);
612             trad_frame_set_reg_addr (this_cache, AARCH64_SVE_Z0_REGNUM + i,
613                                            offset);
614             trad_frame_set_reg_addr (this_cache,
615                                            num_regs + AARCH64_SVE_V0_REGNUM + i,
616                                            offset);
617             trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_Q0_REGNUM + i,
618                                            offset);
619             trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_D0_REGNUM + i,
620                                            offset);
621             trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_S0_REGNUM + i,
622                                            offset);
623             trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_H0_REGNUM + i,
624                                            offset);
625             trad_frame_set_reg_addr (this_cache, num_regs + AARCH64_B0_REGNUM + i,
626                                            offset);
627           }
628 
629       offset = sve_regs + AARCH64_SVE_CONTEXT_P_REGS_OFFSET (vq);
630       for (int i = 0; i < 16; i++)
631           trad_frame_set_reg_addr (this_cache, AARCH64_SVE_P0_REGNUM + i,
632                                          offset + (i * vq * 2));
633 
634       offset = sve_regs + AARCH64_SVE_CONTEXT_FFR_OFFSET (vq);
635       trad_frame_set_reg_addr (this_cache, AARCH64_SVE_FFR_REGNUM, offset);
636     }
637 
638   /* Restore the FPSIMD registers.  */
639   if (signal_frame.fpsimd_section != 0)
640     {
641       CORE_ADDR fpsimd = signal_frame.fpsimd_section;
642 
643       trad_frame_set_reg_addr (this_cache, AARCH64_FPSR_REGNUM,
644                                      fpsimd + AARCH64_FPSIMD_FPSR_OFFSET);
645       trad_frame_set_reg_addr (this_cache, AARCH64_FPCR_REGNUM,
646                                      fpsimd + AARCH64_FPSIMD_FPCR_OFFSET);
647 
648       /* If there was no SVE section then set up the V registers.  */
649       if (!tdep->has_sve () || signal_frame.sve_section == 0)
650           aarch64_linux_restore_vregs (gdbarch, this_cache, fpsimd);
651     }
652 
653   /* Restore the SME registers.  */
654   if (tdep->has_sme ())
655     {
656       if (signal_frame.za_section != 0)
657           {
658             /* Restore the ZA state.  */
659             trad_frame_set_reg_addr (this_cache, tdep->sme_za_regnum,
660                                            signal_frame.za_section);
661           }
662 
663       /* Restore/Reconstruct SVCR.  */
664       ULONGEST svcr = 0;
665       svcr |= signal_frame.za_payload ? SVCR_ZA_BIT : 0;
666       svcr |= signal_frame.streaming_mode ? SVCR_SM_BIT : 0;
667       trad_frame_set_reg_value (this_cache, tdep->sme_svcr_regnum, svcr);
668 
669       /* Restore SVG.  */
670       trad_frame_set_reg_value (this_cache, tdep->sme_svg_regnum,
671                                         sve_vg_from_vl (signal_frame.svl));
672 
673       /* Handle SME2 (ZT).  */
674       if (tdep->has_sme2 ()
675             && signal_frame.za_section != 0
676             && signal_frame.zt_register_count > 0)
677           {
678             /* Is ZA state available?  */
679             gdb_assert (svcr & SVCR_ZA_BIT);
680 
681             /* Restore the ZT state.  For now we assume that we only have
682                a single ZT register.  If/When more ZT registers appear, we
683                should update the code to handle that case accordingly.  */
684             trad_frame_set_reg_addr (this_cache, tdep->sme2_zt0_regnum,
685                                            signal_frame.zt_section
686                                            + AARCH64_SME2_CONTEXT_REGS_OFFSET);
687           }
688     }
689 
690   /* Restore the tpidr2 register, if the target supports it and if there is
691      an entry for it.  */
692   if (signal_frame.tpidr2_section != 0 && tdep->has_tls ()
693       && tdep->tls_register_count >= 2)
694     {
695       /* Restore tpidr2.  */
696       trad_frame_set_reg_addr (this_cache, tdep->tls_regnum_base + 1,
697                                      signal_frame.tpidr2_section
698                                      + AARCH64_TPIDR2_CONTEXT_TPIDR2_OFFSET);
699     }
700 
701   trad_frame_set_id (this_cache, frame_id_build (signal_frame.sp, func));
702 }
703 
704 /* Implements the "prev_arch" method of struct tramp_frame.  */
705 
706 static struct gdbarch *
aarch64_linux_sigframe_prev_arch(const frame_info_ptr & this_frame,void ** frame_cache)707 aarch64_linux_sigframe_prev_arch (const frame_info_ptr &this_frame,
708                                           void **frame_cache)
709 {
710   struct trad_frame_cache *cache
711     = (struct trad_frame_cache *) *frame_cache;
712 
713   gdb_assert (cache != nullptr);
714 
715   struct aarch64_linux_sigframe signal_frame;
716   aarch64_linux_read_signal_frame_info (this_frame, signal_frame);
717 
718   /* The SVE vector length and the SME vector length may change from frame to
719      frame.  Make sure we report the correct architecture to the previous
720      frame.
721 
722      We can reuse the next frame's architecture here, as it should be mostly
723      the same, except for potential different vg and svg values.  */
724   const struct target_desc *tdesc
725     = gdbarch_target_desc (get_frame_arch (this_frame));
726   aarch64_features features = aarch64_features_from_target_desc (tdesc);
727   features.vq = sve_vq_from_vl (signal_frame.vl);
728   features.svq = (uint8_t) sve_vq_from_vl (signal_frame.svl);
729 
730   struct gdbarch_info info;
731   info.bfd_arch_info = bfd_lookup_arch (bfd_arch_aarch64, bfd_mach_aarch64);
732   info.target_desc = aarch64_read_description (features);
733   return gdbarch_find_by_info (info);
734 }
735 
736 static const struct tramp_frame aarch64_linux_rt_sigframe =
737 {
738   SIGTRAMP_FRAME,
739   4,
740   {
741     /* movz x8, 0x8b (S=1,o=10,h=0,i=0x8b,r=8)
742        Soo1 0010 1hhi iiii iiii iiii iiir rrrr  */
743     {0xd2801168, ULONGEST_MAX},
744 
745     /* svc  0x0      (o=0, l=1)
746        1101 0100 oooi iiii iiii iiii iii0 00ll  */
747     {0xd4000001, ULONGEST_MAX},
748     {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
749   },
750   aarch64_linux_sigframe_init,
751   nullptr, /* validate */
752   aarch64_linux_sigframe_prev_arch, /* prev_arch */
753 };
754 
755 /* Register maps.  */
756 
757 static const struct regcache_map_entry aarch64_linux_gregmap[] =
758   {
759     { 31, AARCH64_X0_REGNUM, 8 }, /* x0 ... x30 */
760     { 1, AARCH64_SP_REGNUM, 8 },
761     { 1, AARCH64_PC_REGNUM, 8 },
762     { 1, AARCH64_CPSR_REGNUM, 8 },
763     { 0 }
764   };
765 
766 static const struct regcache_map_entry aarch64_linux_fpregmap[] =
767   {
768     { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */
769     { 1, AARCH64_FPSR_REGNUM, 4 },
770     { 1, AARCH64_FPCR_REGNUM, 4 },
771     { 0 }
772   };
773 
774 /* Register set definitions.  */
775 
776 const struct regset aarch64_linux_gregset =
777   {
778     aarch64_linux_gregmap,
779     regcache_supply_regset, regcache_collect_regset
780   };
781 
782 const struct regset aarch64_linux_fpregset =
783   {
784     aarch64_linux_fpregmap,
785     regcache_supply_regset, regcache_collect_regset
786   };
787 
788 /* The fields in an SVE header at the start of a SVE regset.  */
789 
790 #define SVE_HEADER_SIZE_LENGTH                    4
791 #define SVE_HEADER_MAX_SIZE_LENGTH      4
792 #define SVE_HEADER_VL_LENGTH            2
793 #define SVE_HEADER_MAX_VL_LENGTH        2
794 #define SVE_HEADER_FLAGS_LENGTH                   2
795 #define SVE_HEADER_RESERVED_LENGTH      2
796 
797 #define SVE_HEADER_SIZE_OFFSET                    0
798 #define SVE_HEADER_MAX_SIZE_OFFSET      \
799   (SVE_HEADER_SIZE_OFFSET + SVE_HEADER_SIZE_LENGTH)
800 #define SVE_HEADER_VL_OFFSET            \
801   (SVE_HEADER_MAX_SIZE_OFFSET + SVE_HEADER_MAX_SIZE_LENGTH)
802 #define SVE_HEADER_MAX_VL_OFFSET        \
803   (SVE_HEADER_VL_OFFSET + SVE_HEADER_VL_LENGTH)
804 #define SVE_HEADER_FLAGS_OFFSET                   \
805   (SVE_HEADER_MAX_VL_OFFSET + SVE_HEADER_MAX_VL_LENGTH)
806 #define SVE_HEADER_RESERVED_OFFSET      \
807   (SVE_HEADER_FLAGS_OFFSET + SVE_HEADER_FLAGS_LENGTH)
808 #define SVE_HEADER_SIZE                           \
809   (SVE_HEADER_RESERVED_OFFSET + SVE_HEADER_RESERVED_LENGTH)
810 
811 #define SVE_HEADER_FLAG_SVE             1
812 
813 /* Get the vector quotient (VQ) or streaming vector quotient (SVQ) value
814    from the section named SECTION_NAME.
815 
816    Return non-zero if successful and 0 otherwise.  */
817 
818 static uint64_t
aarch64_linux_core_read_vq(struct gdbarch * gdbarch,bfd * abfd,const char * section_name)819 aarch64_linux_core_read_vq (struct gdbarch *gdbarch, bfd *abfd,
820                                   const char *section_name)
821 {
822   gdb_assert (section_name != nullptr);
823 
824   asection *section = bfd_get_section_by_name (abfd, section_name);
825 
826   if (section == nullptr)
827     {
828       /* No SVE state.  */
829       return 0;
830     }
831 
832   size_t size = bfd_section_size (section);
833 
834   /* Check extended state size.  */
835   if (size < SVE_HEADER_SIZE)
836     {
837       warning (_("'%s' core file section is too small. "
838                      "Expected %s bytes, got %s bytes"), section_name,
839                      pulongest (SVE_HEADER_SIZE), pulongest (size));
840       return 0;
841     }
842 
843   gdb_byte header[SVE_HEADER_SIZE];
844 
845   if (!bfd_get_section_contents (abfd, section, header, 0, SVE_HEADER_SIZE))
846     {
847       warning (_("Couldn't read sve header from "
848                      "'%s' core file section."), section_name);
849       return 0;
850     }
851 
852   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
853   uint64_t vq
854     = sve_vq_from_vl (extract_unsigned_integer (header + SVE_HEADER_VL_OFFSET,
855                                                             SVE_HEADER_VL_LENGTH,
856                                                             byte_order));
857 
858   if (vq > AARCH64_MAX_SVE_VQ || vq == 0)
859     {
860       warning (_("SVE/SSVE vector length in core file is invalid."
861                      " (max vq=%d) (detected vq=%s)"), AARCH64_MAX_SVE_VQ,
862                  pulongest (vq));
863       return 0;
864     }
865 
866   return vq;
867 }
868 
869 /* Get the vector quotient (VQ) value from CORE_BFD's sections.
870 
871    Return non-zero if successful and 0 otherwise.  */
872 
873 static uint64_t
aarch64_linux_core_read_vq_from_sections(struct gdbarch * gdbarch,bfd * core_bfd)874 aarch64_linux_core_read_vq_from_sections (struct gdbarch *gdbarch,
875                                                     bfd *core_bfd)
876 {
877   /* First check if we have a SSVE section.  If so, check if it is active.  */
878   asection *section = bfd_get_section_by_name (core_bfd, ".reg-aarch-ssve");
879 
880   if (section != nullptr)
881     {
882       /* We've found a SSVE section, so now fetch its data.  */
883       gdb_byte header[SVE_HEADER_SIZE];
884 
885       if (bfd_get_section_contents (core_bfd, section, header, 0,
886                                             SVE_HEADER_SIZE))
887           {
888             /* Check if the SSVE section has SVE contents.  */
889             enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
890             uint16_t flags
891               = extract_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
892                                                   SVE_HEADER_FLAGS_LENGTH, byte_order);
893 
894             if (flags & SVE_HEADER_FLAG_SVE)
895               {
896                 /* The SSVE state is active, so return the vector length from the
897                      the SSVE section.  */
898                 return aarch64_linux_core_read_vq (gdbarch, core_bfd,
899                                                              ".reg-aarch-ssve");
900               }
901           }
902     }
903 
904   /* No valid SSVE section.  Return the vq from the SVE section (if any).  */
905   return aarch64_linux_core_read_vq (gdbarch, core_bfd, ".reg-aarch-sve");
906 }
907 
908 /* Supply register REGNUM from BUF to REGCACHE, using the register map
909    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
910    If BUF is nullptr, set the registers to "unavailable" status.  */
911 
912 static void
supply_sve_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)913 supply_sve_regset (const struct regset *regset,
914                        struct regcache *regcache,
915                        int regnum, const void *buf, size_t size)
916 {
917   gdb_byte *header = (gdb_byte *) buf;
918   struct gdbarch *gdbarch = regcache->arch ();
919   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
920 
921   if (buf == nullptr)
922     return regcache->supply_regset (regset, regnum, nullptr, size);
923   gdb_assert (size > SVE_HEADER_SIZE);
924 
925   /* BUF contains an SVE header followed by a register dump of either the
926      passed in SVE regset or a NEON fpregset.  */
927 
928   /* Extract required fields from the header.  */
929   ULONGEST vl = extract_unsigned_integer (header + SVE_HEADER_VL_OFFSET,
930                                                     SVE_HEADER_VL_LENGTH, byte_order);
931   uint16_t flags = extract_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
932                                                        SVE_HEADER_FLAGS_LENGTH,
933                                                        byte_order);
934 
935   if (regnum == -1 || regnum == AARCH64_SVE_VG_REGNUM)
936     {
937       gdb_byte vg_target[8];
938       store_integer ((gdb_byte *)&vg_target, sizeof (uint64_t), byte_order,
939                          sve_vg_from_vl (vl));
940       regcache->raw_supply (AARCH64_SVE_VG_REGNUM, &vg_target);
941     }
942 
943   if (flags & SVE_HEADER_FLAG_SVE)
944     {
945       /* Register dump is a SVE structure.  */
946       regcache->supply_regset (regset, regnum,
947                                      (gdb_byte *) buf + SVE_HEADER_SIZE,
948                                      size - SVE_HEADER_SIZE);
949     }
950   else
951     {
952       /* Register dump is a fpsimd structure.  First clear the SVE
953            registers.  */
954       for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
955           regcache->raw_supply_zeroed (AARCH64_SVE_Z0_REGNUM + i);
956       for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
957           regcache->raw_supply_zeroed (AARCH64_SVE_P0_REGNUM + i);
958       regcache->raw_supply_zeroed (AARCH64_SVE_FFR_REGNUM);
959 
960       /* Then supply the fpsimd registers.  */
961       regcache->supply_regset (&aarch64_linux_fpregset, regnum,
962                                      (gdb_byte *) buf + SVE_HEADER_SIZE,
963                                      size - SVE_HEADER_SIZE);
964     }
965 }
966 
967 /* Collect an inactive SVE register set state.  This is equivalent to a
968    fpsimd layout.
969 
970    Collect the data from REGCACHE to BUF, using the register
971    map in REGSET.  */
972 
973 static void
collect_inactive_sve_regset(const struct regcache * regcache,void * buf,size_t size,int vg_regnum)974 collect_inactive_sve_regset (const struct regcache *regcache,
975                                    void *buf, size_t size, int vg_regnum)
976 {
977   gdb_byte *header = (gdb_byte *) buf;
978   struct gdbarch *gdbarch = regcache->arch ();
979   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
980 
981   gdb_assert (buf != nullptr);
982   gdb_assert (size >= SVE_CORE_DUMMY_SIZE);
983 
984   /* Zero out everything first.  */
985   memset ((gdb_byte *) buf, 0, SVE_CORE_DUMMY_SIZE);
986 
987   /* BUF starts with a SVE header prior to the register dump.  */
988 
989   /* Dump the default size of an empty SVE payload.  */
990   uint32_t real_size = SVE_CORE_DUMMY_SIZE;
991   store_unsigned_integer (header + SVE_HEADER_SIZE_OFFSET,
992                                 SVE_HEADER_SIZE_LENGTH, byte_order, real_size);
993 
994   /* Dump a dummy max size.  */
995   uint32_t max_size = SVE_CORE_DUMMY_MAX_SIZE;
996   store_unsigned_integer (header + SVE_HEADER_MAX_SIZE_OFFSET,
997                                 SVE_HEADER_MAX_SIZE_LENGTH, byte_order, max_size);
998 
999   /* Dump the vector length.  */
1000   ULONGEST vg = 0;
1001   regcache->raw_collect (vg_regnum, &vg);
1002   uint16_t vl = sve_vl_from_vg (vg);
1003   store_unsigned_integer (header + SVE_HEADER_VL_OFFSET, SVE_HEADER_VL_LENGTH,
1004                                 byte_order, vl);
1005 
1006   /* Dump the standard maximum vector length.  */
1007   uint16_t max_vl = SVE_CORE_DUMMY_MAX_VL;
1008   store_unsigned_integer (header + SVE_HEADER_MAX_VL_OFFSET,
1009                                 SVE_HEADER_MAX_VL_LENGTH, byte_order,
1010                                 max_vl);
1011 
1012   /* The rest of the fields are zero.  */
1013   uint16_t flags = SVE_CORE_DUMMY_FLAGS;
1014   store_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
1015                                 SVE_HEADER_FLAGS_LENGTH, byte_order,
1016                                 flags);
1017   uint16_t reserved = SVE_CORE_DUMMY_RESERVED;
1018   store_unsigned_integer (header + SVE_HEADER_RESERVED_OFFSET,
1019                                 SVE_HEADER_RESERVED_LENGTH, byte_order, reserved);
1020 
1021   /* We are done with the header part of it.  Now dump the register state
1022      in the FPSIMD format.  */
1023 
1024   /* Dump the first 128 bits of each of the Z registers.  */
1025   header += AARCH64_SVE_CONTEXT_REGS_OFFSET;
1026   for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
1027     regcache->raw_collect_part (AARCH64_SVE_Z0_REGNUM + i, 0, V_REGISTER_SIZE,
1028                                         header + V_REGISTER_SIZE * i);
1029 
1030   /* Dump FPSR and FPCR.  */
1031   header += 32 * V_REGISTER_SIZE;
1032   regcache->raw_collect (AARCH64_FPSR_REGNUM, header);
1033   regcache->raw_collect (AARCH64_FPCR_REGNUM, header + 4);
1034 
1035   /* Dump two reserved empty fields of 4 bytes.  */
1036   header += 8;
1037   memset (header, 0, 8);
1038 
1039   /* We should have a FPSIMD-formatted register dump now.  */
1040 }
1041 
1042 /* Collect register REGNUM from REGCACHE to BUF, using the register
1043    map in REGSET.  If REGNUM is -1, do this for all registers in
1044    REGSET.  */
1045 
1046 static void
collect_sve_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1047 collect_sve_regset (const struct regset *regset,
1048                         const struct regcache *regcache,
1049                         int regnum, void *buf, size_t size)
1050 {
1051   gdb_byte *header = (gdb_byte *) buf;
1052   struct gdbarch *gdbarch = regcache->arch ();
1053   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1054   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1055   uint64_t vq = tdep->vq;
1056 
1057   gdb_assert (buf != NULL);
1058   gdb_assert (size > SVE_HEADER_SIZE);
1059 
1060   /* BUF starts with a SVE header prior to the register dump.  */
1061 
1062   store_unsigned_integer (header + SVE_HEADER_SIZE_OFFSET,
1063                                 SVE_HEADER_SIZE_LENGTH, byte_order, size);
1064   uint32_t max_size = SVE_CORE_DUMMY_MAX_SIZE;
1065   store_unsigned_integer (header + SVE_HEADER_MAX_SIZE_OFFSET,
1066                                 SVE_HEADER_MAX_SIZE_LENGTH, byte_order, max_size);
1067   store_unsigned_integer (header + SVE_HEADER_VL_OFFSET, SVE_HEADER_VL_LENGTH,
1068                                 byte_order, sve_vl_from_vq (vq));
1069   uint16_t max_vl = SVE_CORE_DUMMY_MAX_VL;
1070   store_unsigned_integer (header + SVE_HEADER_MAX_VL_OFFSET,
1071                                 SVE_HEADER_MAX_VL_LENGTH, byte_order,
1072                                 max_vl);
1073   uint16_t flags = SVE_HEADER_FLAG_SVE;
1074   store_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
1075                                 SVE_HEADER_FLAGS_LENGTH, byte_order,
1076                                 flags);
1077   uint16_t reserved = SVE_CORE_DUMMY_RESERVED;
1078   store_unsigned_integer (header + SVE_HEADER_RESERVED_OFFSET,
1079                                 SVE_HEADER_RESERVED_LENGTH, byte_order, reserved);
1080 
1081   /* The SVE register dump follows.  */
1082   regcache->collect_regset (regset, regnum, (gdb_byte *) buf + SVE_HEADER_SIZE,
1083                                   size - SVE_HEADER_SIZE);
1084 }
1085 
1086 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1087    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1088    If BUF is NULL, set the registers to "unavailable" status.  */
1089 
1090 static void
aarch64_linux_supply_sve_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)1091 aarch64_linux_supply_sve_regset (const struct regset *regset,
1092                                          struct regcache *regcache,
1093                                          int regnum, const void *buf, size_t size)
1094 {
1095   struct gdbarch *gdbarch = regcache->arch ();
1096   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1097 
1098   if (tdep->has_sme ())
1099     {
1100       ULONGEST svcr = 0;
1101       regcache->raw_collect (tdep->sme_svcr_regnum, &svcr);
1102 
1103       /* Is streaming mode enabled?  */
1104       if (svcr & SVCR_SM_BIT)
1105           /* If so, don't load SVE data from the SVE section.  The data to be
1106              used is in the SSVE section.  */
1107           return;
1108     }
1109   /* If streaming mode is not enabled, load the SVE regcache data from the SVE
1110      section.  */
1111   supply_sve_regset (regset, regcache, regnum, buf, size);
1112 }
1113 
1114 /* Collect register REGNUM from REGCACHE to BUF, using the register
1115    map in REGSET.  If REGNUM is -1, do this for all registers in
1116    REGSET.  */
1117 
1118 static void
aarch64_linux_collect_sve_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1119 aarch64_linux_collect_sve_regset (const struct regset *regset,
1120                                           const struct regcache *regcache,
1121                                           int regnum, void *buf, size_t size)
1122 {
1123   struct gdbarch *gdbarch = regcache->arch ();
1124   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1125   bool streaming_mode = false;
1126 
1127   if (tdep->has_sme ())
1128     {
1129       ULONGEST svcr = 0;
1130       regcache->raw_collect (tdep->sme_svcr_regnum, &svcr);
1131 
1132       /* Is streaming mode enabled?  */
1133       if (svcr & SVCR_SM_BIT)
1134           {
1135             /* If so, don't dump SVE regcache data to the SVE section.  The SVE
1136                data should be dumped to the SSVE section.  Dump an empty SVE
1137                block instead.  */
1138             streaming_mode = true;
1139           }
1140     }
1141 
1142   /* If streaming mode is not enabled or there is no SME support, dump the
1143      SVE regcache data to the SVE section.  */
1144 
1145   /* Check if we have an active SVE state (non-zero Z/P/FFR registers).
1146      If so, then we need to dump registers in the SVE format.
1147 
1148      Otherwise we should dump the registers in the FPSIMD format.  */
1149   if (sve_state_is_empty (regcache) || streaming_mode)
1150     collect_inactive_sve_regset (regcache, buf, size, AARCH64_SVE_VG_REGNUM);
1151   else
1152     collect_sve_regset (regset, regcache, regnum, buf, size);
1153 }
1154 
1155 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1156    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1157    If BUF is NULL, set the registers to "unavailable" status.  */
1158 
1159 static void
aarch64_linux_supply_ssve_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)1160 aarch64_linux_supply_ssve_regset (const struct regset *regset,
1161                                           struct regcache *regcache,
1162                                           int regnum, const void *buf, size_t size)
1163 {
1164   gdb_byte *header = (gdb_byte *) buf;
1165   struct gdbarch *gdbarch = regcache->arch ();
1166   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1167   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1168 
1169   uint16_t flags = extract_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
1170                                                        SVE_HEADER_FLAGS_LENGTH,
1171                                                        byte_order);
1172 
1173   /* Since SVCR's bits are inferred from the data we have in the header of the
1174      SSVE section, we need to initialize it to zero first, so that it doesn't
1175      carry garbage data.  */
1176   ULONGEST svcr = 0;
1177   regcache->raw_supply (tdep->sme_svcr_regnum, &svcr);
1178 
1179   /* Is streaming mode enabled?  */
1180   if (flags & SVE_HEADER_FLAG_SVE)
1181     {
1182       /* Streaming mode is active, so flip the SM bit.  */
1183       svcr = SVCR_SM_BIT;
1184       regcache->raw_supply (tdep->sme_svcr_regnum, &svcr);
1185 
1186       /* Fetch the SVE data from the SSVE section.  */
1187       supply_sve_regset (regset, regcache, regnum, buf, size);
1188     }
1189 }
1190 
1191 /* Collect register REGNUM from REGCACHE to BUF, using the register
1192    map in REGSET.  If REGNUM is -1, do this for all registers in
1193    REGSET.  */
1194 
1195 static void
aarch64_linux_collect_ssve_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1196 aarch64_linux_collect_ssve_regset (const struct regset *regset,
1197                                            const struct regcache *regcache,
1198                                            int regnum, void *buf, size_t size)
1199 {
1200   struct gdbarch *gdbarch = regcache->arch ();
1201   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1202   ULONGEST svcr = 0;
1203   regcache->raw_collect (tdep->sme_svcr_regnum, &svcr);
1204 
1205   /* Is streaming mode enabled?  */
1206   if (svcr & SVCR_SM_BIT)
1207     {
1208       /* If so, dump SVE regcache data to the SSVE section.  */
1209       collect_sve_regset (regset, regcache, regnum, buf, size);
1210     }
1211   else
1212     {
1213       /* Otherwise dump an empty SVE block to the SSVE section with the
1214            streaming vector length.  */
1215       collect_inactive_sve_regset (regcache, buf, size, tdep->sme_svg_regnum);
1216     }
1217 }
1218 
1219 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1220    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1221    If BUF is NULL, set the registers to "unavailable" status.  */
1222 
1223 static void
aarch64_linux_supply_za_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)1224 aarch64_linux_supply_za_regset (const struct regset *regset,
1225                                         struct regcache *regcache,
1226                                         int regnum, const void *buf, size_t size)
1227 {
1228   gdb_byte *header = (gdb_byte *) buf;
1229   struct gdbarch *gdbarch = regcache->arch ();
1230   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1231 
1232   /* Handle an empty buffer.  */
1233   if (buf == nullptr)
1234     return regcache->supply_regset (regset, regnum, nullptr, size);
1235 
1236   if (size < SVE_HEADER_SIZE)
1237     error (_("ZA state header size (%s) invalid.  Should be at least %s."),
1238              pulongest (size), pulongest (SVE_HEADER_SIZE));
1239 
1240   /* The ZA register note in a core file can have a couple of states:
1241 
1242      1 - Just the header without the payload.  This means that there is no
1243            ZA data, and we should populate only SVCR and SVG registers on GDB's
1244            side.  The ZA data should be marked as unavailable.
1245 
1246      2 - The header with an additional data payload.  This means there is
1247            actual ZA data, and we should populate ZA, SVCR and SVG.  */
1248 
1249   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1250 
1251   /* Populate SVG.  */
1252   ULONGEST svg
1253     = sve_vg_from_vl (extract_unsigned_integer (header + SVE_HEADER_VL_OFFSET,
1254                                                             SVE_HEADER_VL_LENGTH,
1255                                                             byte_order));
1256   regcache->raw_supply (tdep->sme_svg_regnum, &svg);
1257 
1258   size_t data_size
1259     = extract_unsigned_integer (header + SVE_HEADER_SIZE_OFFSET,
1260                                         SVE_HEADER_SIZE_LENGTH, byte_order)
1261       - SVE_HEADER_SIZE;
1262 
1263   /* Populate SVCR.  */
1264   bool has_za_payload = (data_size > 0);
1265   ULONGEST svcr;
1266   regcache->raw_collect (tdep->sme_svcr_regnum, &svcr);
1267 
1268   /* If we have a ZA payload, enable bit 2 of SVCR, otherwise clear it.  This
1269      register gets updated by the SVE/SSVE-handling functions as well, as they
1270      report the SM bit 1.  */
1271   if (has_za_payload)
1272     svcr |= SVCR_ZA_BIT;
1273   else
1274     svcr &= ~SVCR_ZA_BIT;
1275 
1276   /* Update SVCR in the register buffer.  */
1277   regcache->raw_supply (tdep->sme_svcr_regnum, &svcr);
1278 
1279   /* Populate the register cache with ZA register contents, if we have any.  */
1280   buf = has_za_payload ? (gdb_byte *) buf + SVE_HEADER_SIZE : nullptr;
1281 
1282   size_t za_bytes = std::pow (sve_vl_from_vg (svg), 2);
1283 
1284   /* Update ZA in the register buffer.  */
1285   if (has_za_payload)
1286     {
1287       /* Check that the payload size is sane.  */
1288       if (size < SVE_HEADER_SIZE + za_bytes)
1289           {
1290             error (_("ZA header + payload size (%s) invalid.  Should be at "
1291                        "least %s."),
1292                      pulongest (size), pulongest (SVE_HEADER_SIZE + za_bytes));
1293           }
1294 
1295       regcache->raw_supply (tdep->sme_za_regnum, buf);
1296     }
1297   else
1298     {
1299       gdb_byte za_zeroed[za_bytes];
1300       memset (za_zeroed, 0, za_bytes);
1301       regcache->raw_supply (tdep->sme_za_regnum, za_zeroed);
1302     }
1303 }
1304 
1305 /* Collect register REGNUM from REGCACHE to BUF, using the register
1306    map in REGSET.  If REGNUM is -1, do this for all registers in
1307    REGSET.  */
1308 
1309 static void
aarch64_linux_collect_za_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1310 aarch64_linux_collect_za_regset (const struct regset *regset,
1311                                          const struct regcache *regcache,
1312                                          int regnum, void *buf, size_t size)
1313 {
1314   gdb_assert (buf != nullptr);
1315 
1316   /* Sanity check the dump size.  */
1317   gdb_assert (size >= SVE_HEADER_SIZE);
1318 
1319   /* The ZA register note in a core file can have a couple of states:
1320 
1321      1 - Just the header without the payload.  This means that there is no
1322            ZA data, and we should dump just the header.
1323 
1324      2 - The header with an additional data payload.  This means there is
1325            actual ZA data, and we should dump both the header and the ZA data
1326            payload.  */
1327 
1328   aarch64_gdbarch_tdep *tdep
1329     = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
1330 
1331   /* Determine if we have ZA state from the SVCR register ZA bit.  */
1332   ULONGEST svcr;
1333   regcache->raw_collect (tdep->sme_svcr_regnum, &svcr);
1334 
1335   /* Check the ZA payload.  */
1336   bool has_za_payload = (svcr & SVCR_ZA_BIT) != 0;
1337   size = has_za_payload ? size : SVE_HEADER_SIZE;
1338 
1339   /* Write the size and max_size fields.  */
1340   gdb_byte *header = (gdb_byte *) buf;
1341   enum bfd_endian byte_order = gdbarch_byte_order (regcache->arch ());
1342   store_unsigned_integer (header + SVE_HEADER_SIZE_OFFSET,
1343                                 SVE_HEADER_SIZE_LENGTH, byte_order, size);
1344 
1345   uint32_t max_size
1346     = SVE_HEADER_SIZE + std::pow (sve_vl_from_vq (tdep->sme_svq), 2);
1347   store_unsigned_integer (header + SVE_HEADER_MAX_SIZE_OFFSET,
1348                                 SVE_HEADER_MAX_SIZE_LENGTH, byte_order, max_size);
1349 
1350   /* Output the other fields of the ZA header (vl, max_vl, flags and
1351      reserved).  */
1352   uint64_t svq = tdep->sme_svq;
1353   store_unsigned_integer (header + SVE_HEADER_VL_OFFSET, SVE_HEADER_VL_LENGTH,
1354                                 byte_order, sve_vl_from_vq (svq));
1355 
1356   uint16_t max_vl = SVE_CORE_DUMMY_MAX_VL;
1357   store_unsigned_integer (header + SVE_HEADER_MAX_VL_OFFSET,
1358                                 SVE_HEADER_MAX_VL_LENGTH, byte_order,
1359                                 max_vl);
1360 
1361   uint16_t flags = SVE_CORE_DUMMY_FLAGS;
1362   store_unsigned_integer (header + SVE_HEADER_FLAGS_OFFSET,
1363                                 SVE_HEADER_FLAGS_LENGTH, byte_order, flags);
1364 
1365   uint16_t reserved = SVE_CORE_DUMMY_RESERVED;
1366   store_unsigned_integer (header + SVE_HEADER_RESERVED_OFFSET,
1367                                 SVE_HEADER_RESERVED_LENGTH, byte_order, reserved);
1368 
1369   buf = has_za_payload ? (gdb_byte *) buf + SVE_HEADER_SIZE : nullptr;
1370 
1371   /* Dump the register cache contents for the ZA register to the buffer.  */
1372   regcache->collect_regset (regset, regnum, (gdb_byte *) buf,
1373                                   size - SVE_HEADER_SIZE);
1374 }
1375 
1376 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1377    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1378    If BUF is NULL, set the registers to "unavailable" status.  */
1379 
1380 static void
aarch64_linux_supply_zt_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)1381 aarch64_linux_supply_zt_regset (const struct regset *regset,
1382                                         struct regcache *regcache,
1383                                         int regnum, const void *buf, size_t size)
1384 {
1385   /* Read the ZT register note from a core file into the register buffer.  */
1386 
1387   /* Make sure the buffer contains at least the expected amount of data we are
1388      supposed to get.  */
1389   gdb_assert (size >= AARCH64_SME2_ZT0_SIZE);
1390 
1391   /* Handle an empty buffer.  */
1392   if (buf == nullptr)
1393     return regcache->supply_regset (regset, regnum, nullptr, size);
1394 
1395   aarch64_gdbarch_tdep *tdep
1396     = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
1397 
1398   /* Supply the ZT0 register contents.  */
1399   regcache->raw_supply (tdep->sme2_zt0_regnum, buf);
1400 }
1401 
1402 /* Collect register REGNUM from REGCACHE to BUF, using the register
1403    map in REGSET.  If REGNUM is -1, do this for all registers in
1404    REGSET.  */
1405 
1406 static void
aarch64_linux_collect_zt_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1407 aarch64_linux_collect_zt_regset (const struct regset *regset,
1408                                          const struct regcache *regcache,
1409                                          int regnum, void *buf, size_t size)
1410 {
1411   /* Read the ZT register contents from the register buffer into the core
1412      file section.  */
1413 
1414   /* Make sure the buffer can hold the data we need to return.  */
1415   gdb_assert (size >= AARCH64_SME2_ZT0_SIZE);
1416   gdb_assert (buf != nullptr);
1417 
1418   aarch64_gdbarch_tdep *tdep
1419     = gdbarch_tdep<aarch64_gdbarch_tdep> (regcache->arch ());
1420 
1421   /* Dump the register cache contents for the ZT register to the buffer.  */
1422   regcache->collect_regset (regset, tdep->sme2_zt0_regnum, buf,
1423                                   AARCH64_SME2_ZT0_SIZE);
1424 }
1425 
1426 /* Implement the "iterate_over_regset_sections" gdbarch method.  */
1427 
1428 static void
aarch64_linux_iterate_over_regset_sections(struct gdbarch * gdbarch,iterate_over_regset_sections_cb * cb,void * cb_data,const struct regcache * regcache)1429 aarch64_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
1430                                                       iterate_over_regset_sections_cb *cb,
1431                                                       void *cb_data,
1432                                                       const struct regcache *regcache)
1433 {
1434   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1435 
1436   cb (".reg", AARCH64_LINUX_SIZEOF_GREGSET, AARCH64_LINUX_SIZEOF_GREGSET,
1437       &aarch64_linux_gregset, NULL, cb_data);
1438 
1439   if (tdep->has_sve ())
1440     {
1441       /* Create this on the fly in order to handle vector register sizes.  */
1442       const struct regcache_map_entry sve_regmap[] =
1443           {
1444             { 32, AARCH64_SVE_Z0_REGNUM, (int) (tdep->vq * 16) },
1445             { 16, AARCH64_SVE_P0_REGNUM, (int) (tdep->vq * 16 / 8) },
1446             { 1, AARCH64_SVE_FFR_REGNUM, (int) (tdep->vq * 16 / 8) },
1447             { 1, AARCH64_FPSR_REGNUM, 4 },
1448             { 1, AARCH64_FPCR_REGNUM, 4 },
1449             { 0 }
1450           };
1451 
1452       const struct regset aarch64_linux_ssve_regset =
1453           {
1454             sve_regmap,
1455             aarch64_linux_supply_ssve_regset, aarch64_linux_collect_ssve_regset,
1456             REGSET_VARIABLE_SIZE
1457           };
1458 
1459       /* If SME is supported in the core file, process the SSVE section first,
1460            and the SVE section last.  This is because we need information from
1461            the SSVE set to determine if streaming mode is active.  If streaming
1462            mode is active, we need to extract the data from the SSVE section.
1463 
1464            Otherwise, if streaming mode is not active, we fetch the data from the
1465            SVE section.  */
1466       if (tdep->has_sme ())
1467           {
1468             cb (".reg-aarch-ssve",
1469                 SVE_HEADER_SIZE
1470                 + regcache_map_entry_size (aarch64_linux_fpregmap),
1471                 SVE_HEADER_SIZE + regcache_map_entry_size (sve_regmap),
1472                 &aarch64_linux_ssve_regset, "SSVE registers", cb_data);
1473           }
1474 
1475       /* Handle the SVE register set.  */
1476       const struct regset aarch64_linux_sve_regset =
1477           {
1478             sve_regmap,
1479             aarch64_linux_supply_sve_regset, aarch64_linux_collect_sve_regset,
1480             REGSET_VARIABLE_SIZE
1481           };
1482 
1483       cb (".reg-aarch-sve",
1484             SVE_HEADER_SIZE + regcache_map_entry_size (aarch64_linux_fpregmap),
1485             SVE_HEADER_SIZE + regcache_map_entry_size (sve_regmap),
1486             &aarch64_linux_sve_regset, "SVE registers", cb_data);
1487     }
1488   else
1489     cb (".reg2", AARCH64_LINUX_SIZEOF_FPREGSET, AARCH64_LINUX_SIZEOF_FPREGSET,
1490           &aarch64_linux_fpregset, NULL, cb_data);
1491 
1492   if (tdep->has_sme ())
1493     {
1494       /* Setup the register set information for a ZA register set core
1495            dump.  */
1496 
1497       /* Create this on the fly in order to handle the ZA register size.  */
1498       const struct regcache_map_entry za_regmap[] =
1499           {
1500             { 1, tdep->sme_za_regnum,
1501               (int) std::pow (sve_vl_from_vq (tdep->sme_svq), 2) },
1502             { 0 }
1503           };
1504 
1505       const struct regset aarch64_linux_za_regset =
1506           {
1507             za_regmap,
1508             aarch64_linux_supply_za_regset, aarch64_linux_collect_za_regset,
1509             REGSET_VARIABLE_SIZE
1510           };
1511 
1512       cb (".reg-aarch-za",
1513             SVE_HEADER_SIZE,
1514             SVE_HEADER_SIZE + std::pow (sve_vl_from_vq (tdep->sme_svq), 2),
1515             &aarch64_linux_za_regset, "ZA register", cb_data);
1516 
1517       /* Handle SME2 (ZT) as well, which is only available if SME is
1518            available.  */
1519       if (tdep->has_sme2 ())
1520           {
1521             const struct regcache_map_entry zt_regmap[] =
1522               {
1523                 { 1, tdep->sme2_zt0_regnum, AARCH64_SME2_ZT0_SIZE },
1524                 { 0 }
1525               };
1526 
1527             /* We set the register set size to REGSET_VARIABLE_SIZE here because
1528                in the future there might be more ZT registers.  */
1529             const struct regset aarch64_linux_zt_regset =
1530               {
1531                 zt_regmap,
1532                 aarch64_linux_supply_zt_regset, aarch64_linux_collect_zt_regset,
1533                 REGSET_VARIABLE_SIZE
1534               };
1535 
1536             cb (".reg-aarch-zt",
1537                 AARCH64_SME2_ZT0_SIZE,
1538                 AARCH64_SME2_ZT0_SIZE,
1539                 &aarch64_linux_zt_regset, "ZT registers", cb_data);
1540           }
1541     }
1542 
1543   if (tdep->has_pauth ())
1544     {
1545       /* Create this on the fly in order to handle the variable location.  */
1546       const struct regcache_map_entry pauth_regmap[] =
1547           {
1548             { 2, AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base), 8},
1549             { 0 }
1550           };
1551 
1552       const struct regset aarch64_linux_pauth_regset =
1553           {
1554             pauth_regmap, regcache_supply_regset, regcache_collect_regset
1555           };
1556 
1557       cb (".reg-aarch-pauth", AARCH64_LINUX_SIZEOF_PAUTH,
1558             AARCH64_LINUX_SIZEOF_PAUTH, &aarch64_linux_pauth_regset,
1559             "pauth registers", cb_data);
1560     }
1561 
1562   /* Handle MTE registers.  */
1563   if (tdep->has_mte ())
1564     {
1565       /* Create this on the fly in order to handle the variable location.  */
1566       const struct regcache_map_entry mte_regmap[] =
1567           {
1568             { 1, tdep->mte_reg_base, 8},
1569             { 0 }
1570           };
1571 
1572       const struct regset aarch64_linux_mte_regset =
1573           {
1574             mte_regmap, regcache_supply_regset, regcache_collect_regset
1575           };
1576 
1577       cb (".reg-aarch-mte", AARCH64_LINUX_SIZEOF_MTE_REGSET,
1578             AARCH64_LINUX_SIZEOF_MTE_REGSET, &aarch64_linux_mte_regset,
1579             "MTE registers", cb_data);
1580     }
1581 
1582   /* Handle the TLS registers.  */
1583   if (tdep->has_tls ())
1584     {
1585       gdb_assert (tdep->tls_regnum_base != -1);
1586       gdb_assert (tdep->tls_register_count > 0);
1587 
1588       int sizeof_tls_regset
1589           = AARCH64_TLS_REGISTER_SIZE * tdep->tls_register_count;
1590 
1591       const struct regcache_map_entry tls_regmap[] =
1592           {
1593             { tdep->tls_register_count, tdep->tls_regnum_base,
1594               AARCH64_TLS_REGISTER_SIZE },
1595             { 0 }
1596           };
1597 
1598       const struct regset aarch64_linux_tls_regset =
1599           {
1600             tls_regmap, regcache_supply_regset, regcache_collect_regset,
1601             REGSET_VARIABLE_SIZE
1602           };
1603 
1604       cb (".reg-aarch-tls", sizeof_tls_regset, sizeof_tls_regset,
1605             &aarch64_linux_tls_regset, "TLS register", cb_data);
1606     }
1607 }
1608 
1609 /* Implement the "core_read_description" gdbarch method.  */
1610 
1611 static const struct target_desc *
aarch64_linux_core_read_description(struct gdbarch * gdbarch,struct target_ops * target,bfd * abfd)1612 aarch64_linux_core_read_description (struct gdbarch *gdbarch,
1613                                              struct target_ops *target, bfd *abfd)
1614 {
1615   std::optional<gdb::byte_vector> auxv = target_read_auxv_raw (target);
1616   CORE_ADDR hwcap = linux_get_hwcap (auxv, target, gdbarch);
1617   CORE_ADDR hwcap2 = linux_get_hwcap2 (auxv, target, gdbarch);
1618 
1619   aarch64_features features;
1620 
1621   /* We need to extract the SVE data from the .reg-aarch-sve section or the
1622      .reg-aarch-ssve section depending on which one was active when the core
1623      file was generated.
1624 
1625      If the SSVE section contains SVE data, then it is considered active.
1626      Otherwise the SVE section is considered active.  This guarantees we will
1627      have the correct target description with the correct SVE vector
1628      length.  */
1629   features.vq = aarch64_linux_core_read_vq_from_sections (gdbarch, abfd);
1630   features.pauth = hwcap & AARCH64_HWCAP_PACA;
1631   features.mte = hwcap2 & HWCAP2_MTE;
1632 
1633   /* Handle the TLS section.  */
1634   asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
1635   if (tls != nullptr)
1636     {
1637       size_t size = bfd_section_size (tls);
1638       /* Convert the size to the number of actual registers, by
1639            dividing by 8.  */
1640       features.tls = size / AARCH64_TLS_REGISTER_SIZE;
1641     }
1642 
1643   features.svq
1644     = aarch64_linux_core_read_vq (gdbarch, abfd, ".reg-aarch-za");
1645 
1646   /* Are the ZT registers available?  */
1647   if (bfd_get_section_by_name (abfd, ".reg-aarch-zt") != nullptr)
1648     {
1649       /* Check if ZA is also available, otherwise this is an invalid
1650            combination.  */
1651       if (bfd_get_section_by_name (abfd, ".reg-aarch-za") != nullptr)
1652           features.sme2 = true;
1653       else
1654           warning (_("While reading core file sections, found ZT registers entry "
1655                        "but no ZA register entry.  The ZT contents will be "
1656                        "ignored"));
1657     }
1658 
1659   return aarch64_read_description (features);
1660 }
1661 
1662 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
1663    gdbarch.h.  */
1664 
1665 static int
aarch64_stap_is_single_operand(struct gdbarch * gdbarch,const char * s)1666 aarch64_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
1667 {
1668   return (*s == '#' || isdigit (*s) /* Literal number.  */
1669             || *s == '[' /* Register indirection.  */
1670             || isalpha (*s)); /* Register value.  */
1671 }
1672 
1673 /* This routine is used to parse a special token in AArch64's assembly.
1674 
1675    The special tokens parsed by it are:
1676 
1677       - Register displacement (e.g, [fp, #-8])
1678 
1679    It returns one if the special token has been parsed successfully,
1680    or zero if the current token is not considered special.  */
1681 
1682 static expr::operation_up
aarch64_stap_parse_special_token(struct gdbarch * gdbarch,struct stap_parse_info * p)1683 aarch64_stap_parse_special_token (struct gdbarch *gdbarch,
1684                                           struct stap_parse_info *p)
1685 {
1686   if (*p->arg == '[')
1687     {
1688       /* Temporary holder for lookahead.  */
1689       const char *tmp = p->arg;
1690       char *endp;
1691       /* Used to save the register name.  */
1692       const char *start;
1693       int len;
1694       int got_minus = 0;
1695       long displacement;
1696 
1697       ++tmp;
1698       start = tmp;
1699 
1700       /* Register name.  */
1701       while (isalnum (*tmp))
1702           ++tmp;
1703 
1704       if (*tmp != ',')
1705           return {};
1706 
1707       len = tmp - start;
1708       std::string regname (start, len);
1709 
1710       if (user_reg_map_name_to_regnum (gdbarch, regname.c_str (), len) == -1)
1711           error (_("Invalid register name `%s' on expression `%s'."),
1712                  regname.c_str (), p->saved_arg);
1713 
1714       ++tmp;
1715       tmp = skip_spaces (tmp);
1716       /* Now we expect a number.  It can begin with '#' or simply
1717            a digit.  */
1718       if (*tmp == '#')
1719           ++tmp;
1720 
1721       if (*tmp == '-')
1722           {
1723             ++tmp;
1724             got_minus = 1;
1725           }
1726       else if (*tmp == '+')
1727           ++tmp;
1728 
1729       if (!isdigit (*tmp))
1730           return {};
1731 
1732       displacement = strtol (tmp, &endp, 10);
1733       tmp = endp;
1734 
1735       /* Skipping last `]'.  */
1736       if (*tmp++ != ']')
1737           return {};
1738       p->arg = tmp;
1739 
1740       using namespace expr;
1741 
1742       /* The displacement.  */
1743       struct type *long_type = builtin_type (gdbarch)->builtin_long;
1744       if (got_minus)
1745           displacement = -displacement;
1746       operation_up disp = make_operation<long_const_operation> (long_type,
1747                                                                                 displacement);
1748 
1749       /* The register name.  */
1750       operation_up reg
1751           = make_operation<register_operation> (std::move (regname));
1752 
1753       operation_up sum
1754           = make_operation<add_operation> (std::move (reg), std::move (disp));
1755 
1756       /* Casting to the expected type.  */
1757       struct type *arg_ptr_type = lookup_pointer_type (p->arg_type);
1758       sum = make_operation<unop_cast_operation> (std::move (sum),
1759                                                              arg_ptr_type);
1760       return make_operation<unop_ind_operation> (std::move (sum));
1761     }
1762   return {};
1763 }
1764 
1765 /* AArch64 process record-replay constructs: syscall, signal etc.  */
1766 
1767 static linux_record_tdep aarch64_linux_record_tdep;
1768 
1769 /* Enum that defines the AArch64 linux specific syscall identifiers used for
1770    process record/replay.  */
1771 
1772 enum aarch64_syscall {
1773   aarch64_sys_io_setup = 0,
1774   aarch64_sys_io_destroy = 1,
1775   aarch64_sys_io_submit = 2,
1776   aarch64_sys_io_cancel = 3,
1777   aarch64_sys_io_getevents = 4,
1778   aarch64_sys_setxattr = 5,
1779   aarch64_sys_lsetxattr = 6,
1780   aarch64_sys_fsetxattr = 7,
1781   aarch64_sys_getxattr = 8,
1782   aarch64_sys_lgetxattr = 9,
1783   aarch64_sys_fgetxattr = 10,
1784   aarch64_sys_listxattr = 11,
1785   aarch64_sys_llistxattr = 12,
1786   aarch64_sys_flistxattr = 13,
1787   aarch64_sys_removexattr = 14,
1788   aarch64_sys_lremovexattr = 15,
1789   aarch64_sys_fremovexattr = 16,
1790   aarch64_sys_getcwd = 17,
1791   aarch64_sys_lookup_dcookie = 18,
1792   aarch64_sys_eventfd2 = 19,
1793   aarch64_sys_epoll_create1 = 20,
1794   aarch64_sys_epoll_ctl = 21,
1795   aarch64_sys_epoll_pwait = 22,
1796   aarch64_sys_dup = 23,
1797   aarch64_sys_dup3 = 24,
1798   aarch64_sys_fcntl = 25,
1799   aarch64_sys_inotify_init1 = 26,
1800   aarch64_sys_inotify_add_watch = 27,
1801   aarch64_sys_inotify_rm_watch = 28,
1802   aarch64_sys_ioctl = 29,
1803   aarch64_sys_ioprio_set = 30,
1804   aarch64_sys_ioprio_get = 31,
1805   aarch64_sys_flock = 32,
1806   aarch64_sys_mknodat = 33,
1807   aarch64_sys_mkdirat = 34,
1808   aarch64_sys_unlinkat = 35,
1809   aarch64_sys_symlinkat = 36,
1810   aarch64_sys_linkat = 37,
1811   aarch64_sys_renameat = 38,
1812   aarch64_sys_umount2 = 39,
1813   aarch64_sys_mount = 40,
1814   aarch64_sys_pivot_root = 41,
1815   aarch64_sys_nfsservctl = 42,
1816   aarch64_sys_statfs = 43,
1817   aarch64_sys_fstatfs = 44,
1818   aarch64_sys_truncate = 45,
1819   aarch64_sys_ftruncate = 46,
1820   aarch64_sys_fallocate = 47,
1821   aarch64_sys_faccessat = 48,
1822   aarch64_sys_chdir = 49,
1823   aarch64_sys_fchdir = 50,
1824   aarch64_sys_chroot = 51,
1825   aarch64_sys_fchmod = 52,
1826   aarch64_sys_fchmodat = 53,
1827   aarch64_sys_fchownat = 54,
1828   aarch64_sys_fchown = 55,
1829   aarch64_sys_openat = 56,
1830   aarch64_sys_close = 57,
1831   aarch64_sys_vhangup = 58,
1832   aarch64_sys_pipe2 = 59,
1833   aarch64_sys_quotactl = 60,
1834   aarch64_sys_getdents64 = 61,
1835   aarch64_sys_lseek = 62,
1836   aarch64_sys_read = 63,
1837   aarch64_sys_write = 64,
1838   aarch64_sys_readv = 65,
1839   aarch64_sys_writev = 66,
1840   aarch64_sys_pread64 = 67,
1841   aarch64_sys_pwrite64 = 68,
1842   aarch64_sys_preadv = 69,
1843   aarch64_sys_pwritev = 70,
1844   aarch64_sys_sendfile = 71,
1845   aarch64_sys_pselect6 = 72,
1846   aarch64_sys_ppoll = 73,
1847   aarch64_sys_signalfd4 = 74,
1848   aarch64_sys_vmsplice = 75,
1849   aarch64_sys_splice = 76,
1850   aarch64_sys_tee = 77,
1851   aarch64_sys_readlinkat = 78,
1852   aarch64_sys_newfstatat = 79,
1853   aarch64_sys_fstat = 80,
1854   aarch64_sys_sync = 81,
1855   aarch64_sys_fsync = 82,
1856   aarch64_sys_fdatasync = 83,
1857   aarch64_sys_sync_file_range2 = 84,
1858   aarch64_sys_sync_file_range = 84,
1859   aarch64_sys_timerfd_create = 85,
1860   aarch64_sys_timerfd_settime = 86,
1861   aarch64_sys_timerfd_gettime = 87,
1862   aarch64_sys_utimensat = 88,
1863   aarch64_sys_acct = 89,
1864   aarch64_sys_capget = 90,
1865   aarch64_sys_capset = 91,
1866   aarch64_sys_personality = 92,
1867   aarch64_sys_exit = 93,
1868   aarch64_sys_exit_group = 94,
1869   aarch64_sys_waitid = 95,
1870   aarch64_sys_set_tid_address = 96,
1871   aarch64_sys_unshare = 97,
1872   aarch64_sys_futex = 98,
1873   aarch64_sys_set_robust_list = 99,
1874   aarch64_sys_get_robust_list = 100,
1875   aarch64_sys_nanosleep = 101,
1876   aarch64_sys_getitimer = 102,
1877   aarch64_sys_setitimer = 103,
1878   aarch64_sys_kexec_load = 104,
1879   aarch64_sys_init_module = 105,
1880   aarch64_sys_delete_module = 106,
1881   aarch64_sys_timer_create = 107,
1882   aarch64_sys_timer_gettime = 108,
1883   aarch64_sys_timer_getoverrun = 109,
1884   aarch64_sys_timer_settime = 110,
1885   aarch64_sys_timer_delete = 111,
1886   aarch64_sys_clock_settime = 112,
1887   aarch64_sys_clock_gettime = 113,
1888   aarch64_sys_clock_getres = 114,
1889   aarch64_sys_clock_nanosleep = 115,
1890   aarch64_sys_syslog = 116,
1891   aarch64_sys_ptrace = 117,
1892   aarch64_sys_sched_setparam = 118,
1893   aarch64_sys_sched_setscheduler = 119,
1894   aarch64_sys_sched_getscheduler = 120,
1895   aarch64_sys_sched_getparam = 121,
1896   aarch64_sys_sched_setaffinity = 122,
1897   aarch64_sys_sched_getaffinity = 123,
1898   aarch64_sys_sched_yield = 124,
1899   aarch64_sys_sched_get_priority_max = 125,
1900   aarch64_sys_sched_get_priority_min = 126,
1901   aarch64_sys_sched_rr_get_interval = 127,
1902   aarch64_sys_kill = 129,
1903   aarch64_sys_tkill = 130,
1904   aarch64_sys_tgkill = 131,
1905   aarch64_sys_sigaltstack = 132,
1906   aarch64_sys_rt_sigsuspend = 133,
1907   aarch64_sys_rt_sigaction = 134,
1908   aarch64_sys_rt_sigprocmask = 135,
1909   aarch64_sys_rt_sigpending = 136,
1910   aarch64_sys_rt_sigtimedwait = 137,
1911   aarch64_sys_rt_sigqueueinfo = 138,
1912   aarch64_sys_rt_sigreturn = 139,
1913   aarch64_sys_setpriority = 140,
1914   aarch64_sys_getpriority = 141,
1915   aarch64_sys_reboot = 142,
1916   aarch64_sys_setregid = 143,
1917   aarch64_sys_setgid = 144,
1918   aarch64_sys_setreuid = 145,
1919   aarch64_sys_setuid = 146,
1920   aarch64_sys_setresuid = 147,
1921   aarch64_sys_getresuid = 148,
1922   aarch64_sys_setresgid = 149,
1923   aarch64_sys_getresgid = 150,
1924   aarch64_sys_setfsuid = 151,
1925   aarch64_sys_setfsgid = 152,
1926   aarch64_sys_times = 153,
1927   aarch64_sys_setpgid = 154,
1928   aarch64_sys_getpgid = 155,
1929   aarch64_sys_getsid = 156,
1930   aarch64_sys_setsid = 157,
1931   aarch64_sys_getgroups = 158,
1932   aarch64_sys_setgroups = 159,
1933   aarch64_sys_uname = 160,
1934   aarch64_sys_sethostname = 161,
1935   aarch64_sys_setdomainname = 162,
1936   aarch64_sys_getrlimit = 163,
1937   aarch64_sys_setrlimit = 164,
1938   aarch64_sys_getrusage = 165,
1939   aarch64_sys_umask = 166,
1940   aarch64_sys_prctl = 167,
1941   aarch64_sys_getcpu = 168,
1942   aarch64_sys_gettimeofday = 169,
1943   aarch64_sys_settimeofday = 170,
1944   aarch64_sys_adjtimex = 171,
1945   aarch64_sys_getpid = 172,
1946   aarch64_sys_getppid = 173,
1947   aarch64_sys_getuid = 174,
1948   aarch64_sys_geteuid = 175,
1949   aarch64_sys_getgid = 176,
1950   aarch64_sys_getegid = 177,
1951   aarch64_sys_gettid = 178,
1952   aarch64_sys_sysinfo = 179,
1953   aarch64_sys_mq_open = 180,
1954   aarch64_sys_mq_unlink = 181,
1955   aarch64_sys_mq_timedsend = 182,
1956   aarch64_sys_mq_timedreceive = 183,
1957   aarch64_sys_mq_notify = 184,
1958   aarch64_sys_mq_getsetattr = 185,
1959   aarch64_sys_msgget = 186,
1960   aarch64_sys_msgctl = 187,
1961   aarch64_sys_msgrcv = 188,
1962   aarch64_sys_msgsnd = 189,
1963   aarch64_sys_semget = 190,
1964   aarch64_sys_semctl = 191,
1965   aarch64_sys_semtimedop = 192,
1966   aarch64_sys_semop = 193,
1967   aarch64_sys_shmget = 194,
1968   aarch64_sys_shmctl = 195,
1969   aarch64_sys_shmat = 196,
1970   aarch64_sys_shmdt = 197,
1971   aarch64_sys_socket = 198,
1972   aarch64_sys_socketpair = 199,
1973   aarch64_sys_bind = 200,
1974   aarch64_sys_listen = 201,
1975   aarch64_sys_accept = 202,
1976   aarch64_sys_connect = 203,
1977   aarch64_sys_getsockname = 204,
1978   aarch64_sys_getpeername = 205,
1979   aarch64_sys_sendto = 206,
1980   aarch64_sys_recvfrom = 207,
1981   aarch64_sys_setsockopt = 208,
1982   aarch64_sys_getsockopt = 209,
1983   aarch64_sys_shutdown = 210,
1984   aarch64_sys_sendmsg = 211,
1985   aarch64_sys_recvmsg = 212,
1986   aarch64_sys_readahead = 213,
1987   aarch64_sys_brk = 214,
1988   aarch64_sys_munmap = 215,
1989   aarch64_sys_mremap = 216,
1990   aarch64_sys_add_key = 217,
1991   aarch64_sys_request_key = 218,
1992   aarch64_sys_keyctl = 219,
1993   aarch64_sys_clone = 220,
1994   aarch64_sys_execve = 221,
1995   aarch64_sys_mmap = 222,
1996   aarch64_sys_fadvise64 = 223,
1997   aarch64_sys_swapon = 224,
1998   aarch64_sys_swapoff = 225,
1999   aarch64_sys_mprotect = 226,
2000   aarch64_sys_msync = 227,
2001   aarch64_sys_mlock = 228,
2002   aarch64_sys_munlock = 229,
2003   aarch64_sys_mlockall = 230,
2004   aarch64_sys_munlockall = 231,
2005   aarch64_sys_mincore = 232,
2006   aarch64_sys_madvise = 233,
2007   aarch64_sys_remap_file_pages = 234,
2008   aarch64_sys_mbind = 235,
2009   aarch64_sys_get_mempolicy = 236,
2010   aarch64_sys_set_mempolicy = 237,
2011   aarch64_sys_migrate_pages = 238,
2012   aarch64_sys_move_pages = 239,
2013   aarch64_sys_rt_tgsigqueueinfo = 240,
2014   aarch64_sys_perf_event_open = 241,
2015   aarch64_sys_accept4 = 242,
2016   aarch64_sys_recvmmsg = 243,
2017   aarch64_sys_wait4 = 260,
2018   aarch64_sys_prlimit64 = 261,
2019   aarch64_sys_fanotify_init = 262,
2020   aarch64_sys_fanotify_mark = 263,
2021   aarch64_sys_name_to_handle_at = 264,
2022   aarch64_sys_open_by_handle_at = 265,
2023   aarch64_sys_clock_adjtime = 266,
2024   aarch64_sys_syncfs = 267,
2025   aarch64_sys_setns = 268,
2026   aarch64_sys_sendmmsg = 269,
2027   aarch64_sys_process_vm_readv = 270,
2028   aarch64_sys_process_vm_writev = 271,
2029   aarch64_sys_kcmp = 272,
2030   aarch64_sys_finit_module = 273,
2031   aarch64_sys_sched_setattr = 274,
2032   aarch64_sys_sched_getattr = 275,
2033   aarch64_sys_getrandom = 278
2034 };
2035 
2036 /* aarch64_canonicalize_syscall maps syscall ids from the native AArch64
2037    linux set of syscall ids into a canonical set of syscall ids used by
2038    process record.  */
2039 
2040 static enum gdb_syscall
aarch64_canonicalize_syscall(enum aarch64_syscall syscall_number)2041 aarch64_canonicalize_syscall (enum aarch64_syscall syscall_number)
2042 {
2043 #define SYSCALL_MAP(SYSCALL) case aarch64_sys_##SYSCALL: \
2044   return gdb_sys_##SYSCALL
2045 
2046 #define UNSUPPORTED_SYSCALL_MAP(SYSCALL) case aarch64_sys_##SYSCALL: \
2047   return gdb_sys_no_syscall
2048 
2049   switch (syscall_number)
2050     {
2051       SYSCALL_MAP (io_setup);
2052       SYSCALL_MAP (io_destroy);
2053       SYSCALL_MAP (io_submit);
2054       SYSCALL_MAP (io_cancel);
2055       SYSCALL_MAP (io_getevents);
2056 
2057       SYSCALL_MAP (setxattr);
2058       SYSCALL_MAP (lsetxattr);
2059       SYSCALL_MAP (fsetxattr);
2060       SYSCALL_MAP (getxattr);
2061       SYSCALL_MAP (lgetxattr);
2062       SYSCALL_MAP (fgetxattr);
2063       SYSCALL_MAP (listxattr);
2064       SYSCALL_MAP (llistxattr);
2065       SYSCALL_MAP (flistxattr);
2066       SYSCALL_MAP (removexattr);
2067       SYSCALL_MAP (lremovexattr);
2068       SYSCALL_MAP (fremovexattr);
2069       SYSCALL_MAP (getcwd);
2070       SYSCALL_MAP (lookup_dcookie);
2071       SYSCALL_MAP (eventfd2);
2072       SYSCALL_MAP (epoll_create1);
2073       SYSCALL_MAP (epoll_ctl);
2074       SYSCALL_MAP (epoll_pwait);
2075       SYSCALL_MAP (dup);
2076       SYSCALL_MAP (dup3);
2077       SYSCALL_MAP (fcntl);
2078       SYSCALL_MAP (inotify_init1);
2079       SYSCALL_MAP (inotify_add_watch);
2080       SYSCALL_MAP (inotify_rm_watch);
2081       SYSCALL_MAP (ioctl);
2082       SYSCALL_MAP (ioprio_set);
2083       SYSCALL_MAP (ioprio_get);
2084       SYSCALL_MAP (flock);
2085       SYSCALL_MAP (mknodat);
2086       SYSCALL_MAP (mkdirat);
2087       SYSCALL_MAP (unlinkat);
2088       SYSCALL_MAP (symlinkat);
2089       SYSCALL_MAP (linkat);
2090       SYSCALL_MAP (renameat);
2091       UNSUPPORTED_SYSCALL_MAP (umount2);
2092       SYSCALL_MAP (mount);
2093       SYSCALL_MAP (pivot_root);
2094       SYSCALL_MAP (nfsservctl);
2095       SYSCALL_MAP (statfs);
2096       SYSCALL_MAP (truncate);
2097       SYSCALL_MAP (ftruncate);
2098       SYSCALL_MAP (fallocate);
2099       SYSCALL_MAP (faccessat);
2100       SYSCALL_MAP (fchdir);
2101       SYSCALL_MAP (chroot);
2102       SYSCALL_MAP (fchmod);
2103       SYSCALL_MAP (fchmodat);
2104       SYSCALL_MAP (fchownat);
2105       SYSCALL_MAP (fchown);
2106       SYSCALL_MAP (openat);
2107       SYSCALL_MAP (close);
2108       SYSCALL_MAP (vhangup);
2109       SYSCALL_MAP (pipe2);
2110       SYSCALL_MAP (quotactl);
2111       SYSCALL_MAP (getdents64);
2112       SYSCALL_MAP (lseek);
2113       SYSCALL_MAP (read);
2114       SYSCALL_MAP (write);
2115       SYSCALL_MAP (readv);
2116       SYSCALL_MAP (writev);
2117       SYSCALL_MAP (pread64);
2118       SYSCALL_MAP (pwrite64);
2119       UNSUPPORTED_SYSCALL_MAP (preadv);
2120       UNSUPPORTED_SYSCALL_MAP (pwritev);
2121       SYSCALL_MAP (sendfile);
2122       SYSCALL_MAP (pselect6);
2123       SYSCALL_MAP (ppoll);
2124       UNSUPPORTED_SYSCALL_MAP (signalfd4);
2125       SYSCALL_MAP (vmsplice);
2126       SYSCALL_MAP (splice);
2127       SYSCALL_MAP (tee);
2128       SYSCALL_MAP (readlinkat);
2129       SYSCALL_MAP (newfstatat);
2130 
2131       SYSCALL_MAP (fstat);
2132       SYSCALL_MAP (sync);
2133       SYSCALL_MAP (fsync);
2134       SYSCALL_MAP (fdatasync);
2135       SYSCALL_MAP (sync_file_range);
2136       UNSUPPORTED_SYSCALL_MAP (timerfd_create);
2137       UNSUPPORTED_SYSCALL_MAP (timerfd_settime);
2138       UNSUPPORTED_SYSCALL_MAP (timerfd_gettime);
2139       UNSUPPORTED_SYSCALL_MAP (utimensat);
2140       SYSCALL_MAP (acct);
2141       SYSCALL_MAP (capget);
2142       SYSCALL_MAP (capset);
2143       SYSCALL_MAP (personality);
2144       SYSCALL_MAP (exit);
2145       SYSCALL_MAP (exit_group);
2146       SYSCALL_MAP (waitid);
2147       SYSCALL_MAP (set_tid_address);
2148       SYSCALL_MAP (unshare);
2149       SYSCALL_MAP (futex);
2150       SYSCALL_MAP (set_robust_list);
2151       SYSCALL_MAP (get_robust_list);
2152       SYSCALL_MAP (nanosleep);
2153 
2154       SYSCALL_MAP (getitimer);
2155       SYSCALL_MAP (setitimer);
2156       SYSCALL_MAP (kexec_load);
2157       SYSCALL_MAP (init_module);
2158       SYSCALL_MAP (delete_module);
2159       SYSCALL_MAP (timer_create);
2160       SYSCALL_MAP (timer_settime);
2161       SYSCALL_MAP (timer_gettime);
2162       SYSCALL_MAP (timer_getoverrun);
2163       SYSCALL_MAP (timer_delete);
2164       SYSCALL_MAP (clock_settime);
2165       SYSCALL_MAP (clock_gettime);
2166       SYSCALL_MAP (clock_getres);
2167       SYSCALL_MAP (clock_nanosleep);
2168       SYSCALL_MAP (syslog);
2169       SYSCALL_MAP (ptrace);
2170       SYSCALL_MAP (sched_setparam);
2171       SYSCALL_MAP (sched_setscheduler);
2172       SYSCALL_MAP (sched_getscheduler);
2173       SYSCALL_MAP (sched_getparam);
2174       SYSCALL_MAP (sched_setaffinity);
2175       SYSCALL_MAP (sched_getaffinity);
2176       SYSCALL_MAP (sched_yield);
2177       SYSCALL_MAP (sched_get_priority_max);
2178       SYSCALL_MAP (sched_get_priority_min);
2179       SYSCALL_MAP (sched_rr_get_interval);
2180       SYSCALL_MAP (kill);
2181       SYSCALL_MAP (tkill);
2182       SYSCALL_MAP (tgkill);
2183       SYSCALL_MAP (sigaltstack);
2184       SYSCALL_MAP (rt_sigsuspend);
2185       SYSCALL_MAP (rt_sigaction);
2186       SYSCALL_MAP (rt_sigprocmask);
2187       SYSCALL_MAP (rt_sigpending);
2188       SYSCALL_MAP (rt_sigtimedwait);
2189       SYSCALL_MAP (rt_sigqueueinfo);
2190       SYSCALL_MAP (rt_sigreturn);
2191       SYSCALL_MAP (setpriority);
2192       SYSCALL_MAP (getpriority);
2193       SYSCALL_MAP (reboot);
2194       SYSCALL_MAP (setregid);
2195       SYSCALL_MAP (setgid);
2196       SYSCALL_MAP (setreuid);
2197       SYSCALL_MAP (setuid);
2198       SYSCALL_MAP (setresuid);
2199       SYSCALL_MAP (getresuid);
2200       SYSCALL_MAP (setresgid);
2201       SYSCALL_MAP (getresgid);
2202       SYSCALL_MAP (setfsuid);
2203       SYSCALL_MAP (setfsgid);
2204       SYSCALL_MAP (times);
2205       SYSCALL_MAP (setpgid);
2206       SYSCALL_MAP (getpgid);
2207       SYSCALL_MAP (getsid);
2208       SYSCALL_MAP (setsid);
2209       SYSCALL_MAP (getgroups);
2210       SYSCALL_MAP (setgroups);
2211       SYSCALL_MAP (uname);
2212       SYSCALL_MAP (sethostname);
2213       SYSCALL_MAP (setdomainname);
2214       SYSCALL_MAP (getrlimit);
2215       SYSCALL_MAP (setrlimit);
2216       SYSCALL_MAP (getrusage);
2217       SYSCALL_MAP (umask);
2218       SYSCALL_MAP (prctl);
2219       SYSCALL_MAP (getcpu);
2220       SYSCALL_MAP (gettimeofday);
2221       SYSCALL_MAP (settimeofday);
2222       SYSCALL_MAP (adjtimex);
2223       SYSCALL_MAP (getpid);
2224       SYSCALL_MAP (getppid);
2225       SYSCALL_MAP (getuid);
2226       SYSCALL_MAP (geteuid);
2227       SYSCALL_MAP (getgid);
2228       SYSCALL_MAP (getegid);
2229       SYSCALL_MAP (gettid);
2230       SYSCALL_MAP (sysinfo);
2231       SYSCALL_MAP (mq_open);
2232       SYSCALL_MAP (mq_unlink);
2233       SYSCALL_MAP (mq_timedsend);
2234       SYSCALL_MAP (mq_timedreceive);
2235       SYSCALL_MAP (mq_notify);
2236       SYSCALL_MAP (mq_getsetattr);
2237       SYSCALL_MAP (msgget);
2238       SYSCALL_MAP (msgctl);
2239       SYSCALL_MAP (msgrcv);
2240       SYSCALL_MAP (msgsnd);
2241       SYSCALL_MAP (semget);
2242       SYSCALL_MAP (semctl);
2243       SYSCALL_MAP (semtimedop);
2244       SYSCALL_MAP (semop);
2245       SYSCALL_MAP (shmget);
2246       SYSCALL_MAP (shmctl);
2247       SYSCALL_MAP (shmat);
2248       SYSCALL_MAP (shmdt);
2249       SYSCALL_MAP (socket);
2250       SYSCALL_MAP (socketpair);
2251       SYSCALL_MAP (bind);
2252       SYSCALL_MAP (listen);
2253       SYSCALL_MAP (accept);
2254       SYSCALL_MAP (connect);
2255       SYSCALL_MAP (getsockname);
2256       SYSCALL_MAP (getpeername);
2257       SYSCALL_MAP (sendto);
2258       SYSCALL_MAP (recvfrom);
2259       SYSCALL_MAP (setsockopt);
2260       SYSCALL_MAP (getsockopt);
2261       SYSCALL_MAP (shutdown);
2262       SYSCALL_MAP (sendmsg);
2263       SYSCALL_MAP (recvmsg);
2264       SYSCALL_MAP (readahead);
2265       SYSCALL_MAP (brk);
2266       SYSCALL_MAP (munmap);
2267       SYSCALL_MAP (mremap);
2268       SYSCALL_MAP (add_key);
2269       SYSCALL_MAP (request_key);
2270       SYSCALL_MAP (keyctl);
2271       SYSCALL_MAP (clone);
2272       SYSCALL_MAP (execve);
2273 
2274     case aarch64_sys_mmap:
2275       return gdb_sys_mmap2;
2276 
2277       SYSCALL_MAP (fadvise64);
2278       SYSCALL_MAP (swapon);
2279       SYSCALL_MAP (swapoff);
2280       SYSCALL_MAP (mprotect);
2281       SYSCALL_MAP (msync);
2282       SYSCALL_MAP (mlock);
2283       SYSCALL_MAP (munlock);
2284       SYSCALL_MAP (mlockall);
2285       SYSCALL_MAP (munlockall);
2286       SYSCALL_MAP (mincore);
2287       SYSCALL_MAP (madvise);
2288       SYSCALL_MAP (remap_file_pages);
2289       SYSCALL_MAP (mbind);
2290       SYSCALL_MAP (get_mempolicy);
2291       SYSCALL_MAP (set_mempolicy);
2292       SYSCALL_MAP (migrate_pages);
2293       SYSCALL_MAP (move_pages);
2294       UNSUPPORTED_SYSCALL_MAP (rt_tgsigqueueinfo);
2295       UNSUPPORTED_SYSCALL_MAP (perf_event_open);
2296       UNSUPPORTED_SYSCALL_MAP (accept4);
2297       UNSUPPORTED_SYSCALL_MAP (recvmmsg);
2298 
2299       SYSCALL_MAP (wait4);
2300 
2301       UNSUPPORTED_SYSCALL_MAP (prlimit64);
2302       UNSUPPORTED_SYSCALL_MAP (fanotify_init);
2303       UNSUPPORTED_SYSCALL_MAP (fanotify_mark);
2304       UNSUPPORTED_SYSCALL_MAP (name_to_handle_at);
2305       UNSUPPORTED_SYSCALL_MAP (open_by_handle_at);
2306       UNSUPPORTED_SYSCALL_MAP (clock_adjtime);
2307       UNSUPPORTED_SYSCALL_MAP (syncfs);
2308       UNSUPPORTED_SYSCALL_MAP (setns);
2309       UNSUPPORTED_SYSCALL_MAP (sendmmsg);
2310       UNSUPPORTED_SYSCALL_MAP (process_vm_readv);
2311       UNSUPPORTED_SYSCALL_MAP (process_vm_writev);
2312       UNSUPPORTED_SYSCALL_MAP (kcmp);
2313       UNSUPPORTED_SYSCALL_MAP (finit_module);
2314       UNSUPPORTED_SYSCALL_MAP (sched_setattr);
2315       UNSUPPORTED_SYSCALL_MAP (sched_getattr);
2316       SYSCALL_MAP (getrandom);
2317   default:
2318     return gdb_sys_no_syscall;
2319   }
2320 }
2321 
2322 /* Retrieve the syscall number at a ptrace syscall-stop, either on syscall entry
2323    or exit.  Return -1 upon error.  */
2324 
2325 static LONGEST
aarch64_linux_get_syscall_number(struct gdbarch * gdbarch,thread_info * thread)2326 aarch64_linux_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
2327 {
2328   struct regcache *regs = get_thread_regcache (thread);
2329   LONGEST ret;
2330 
2331   /* Get the system call number from register x8.  */
2332   regs->cooked_read (AARCH64_X0_REGNUM + 8, &ret);
2333 
2334   /* On exit from a successful execve, we will be in a new process and all the
2335      registers will be cleared - x0 to x30 will be 0, except for a 1 in x7.
2336      This function will only ever get called when stopped at the entry or exit
2337      of a syscall, so by checking for 0 in x0 (arg0/retval), x1 (arg1), x8
2338      (syscall), x29 (FP) and x30 (LR) we can infer:
2339      1) Either inferior is at exit from successful execve.
2340      2) Or inferior is at entry to a call to io_setup with invalid arguments and
2341           a corrupted FP and LR.
2342      It should be safe enough to assume case 1.  */
2343   if (ret == 0)
2344     {
2345       LONGEST x1 = -1, fp = -1, lr = -1;
2346       regs->cooked_read (AARCH64_X0_REGNUM + 1, &x1);
2347       regs->cooked_read (AARCH64_FP_REGNUM, &fp);
2348       regs->cooked_read (AARCH64_LR_REGNUM, &lr);
2349       if (x1 == 0 && fp ==0 && lr == 0)
2350           return aarch64_sys_execve;
2351     }
2352 
2353   return ret;
2354 }
2355 
2356 /* Record all registers but PC register for process-record.  */
2357 
2358 static int
aarch64_all_but_pc_registers_record(struct regcache * regcache)2359 aarch64_all_but_pc_registers_record (struct regcache *regcache)
2360 {
2361   int i;
2362 
2363   for (i = AARCH64_X0_REGNUM; i < AARCH64_PC_REGNUM; i++)
2364     if (record_full_arch_list_add_reg (regcache, i))
2365       return -1;
2366 
2367   if (record_full_arch_list_add_reg (regcache, AARCH64_CPSR_REGNUM))
2368     return -1;
2369 
2370   return 0;
2371 }
2372 
2373 /* Handler for aarch64 system call instruction recording.  */
2374 
2375 static int
aarch64_linux_syscall_record(struct regcache * regcache,unsigned long svc_number)2376 aarch64_linux_syscall_record (struct regcache *regcache,
2377                                     unsigned long svc_number)
2378 {
2379   int ret = 0;
2380   enum gdb_syscall syscall_gdb;
2381 
2382   syscall_gdb =
2383     aarch64_canonicalize_syscall ((enum aarch64_syscall) svc_number);
2384 
2385   if (syscall_gdb < 0)
2386     {
2387       gdb_printf (gdb_stderr,
2388                       _("Process record and replay target doesn't "
2389                         "support syscall number %s\n"),
2390                       plongest (svc_number));
2391       return -1;
2392     }
2393 
2394   if (syscall_gdb == gdb_sys_sigreturn
2395       || syscall_gdb == gdb_sys_rt_sigreturn)
2396    {
2397      if (aarch64_all_but_pc_registers_record (regcache))
2398        return -1;
2399      return 0;
2400    }
2401 
2402   ret = record_linux_system_call (syscall_gdb, regcache,
2403                                           &aarch64_linux_record_tdep);
2404   if (ret != 0)
2405     return ret;
2406 
2407   /* Record the return value of the system call.  */
2408   if (record_full_arch_list_add_reg (regcache, AARCH64_X0_REGNUM))
2409     return -1;
2410   /* Record LR.  */
2411   if (record_full_arch_list_add_reg (regcache, AARCH64_LR_REGNUM))
2412     return -1;
2413   /* Record CPSR.  */
2414   if (record_full_arch_list_add_reg (regcache, AARCH64_CPSR_REGNUM))
2415     return -1;
2416 
2417   return 0;
2418 }
2419 
2420 /* Implement the "gcc_target_options" gdbarch method.  */
2421 
2422 static std::string
aarch64_linux_gcc_target_options(struct gdbarch * gdbarch)2423 aarch64_linux_gcc_target_options (struct gdbarch *gdbarch)
2424 {
2425   /* GCC doesn't know "-m64".  */
2426   return {};
2427 }
2428 
2429 /* Helper to get the allocation tag from a 64-bit ADDRESS.
2430 
2431    Return the allocation tag if successful and nullopt otherwise.  */
2432 
2433 static std::optional<CORE_ADDR>
aarch64_mte_get_atag(CORE_ADDR address)2434 aarch64_mte_get_atag (CORE_ADDR address)
2435 {
2436   gdb::byte_vector tags;
2437 
2438   /* Attempt to fetch the allocation tag.  */
2439   if (!target_fetch_memtags (address, 1, tags,
2440                                    static_cast<int> (memtag_type::allocation)))
2441     return {};
2442 
2443   /* Only one tag should've been returned.  Make sure we got exactly that.  */
2444   if (tags.size () != 1)
2445     error (_("Target returned an unexpected number of tags."));
2446 
2447   /* Although our tags are 4 bits in size, they are stored in a
2448      byte.  */
2449   return tags[0];
2450 }
2451 
2452 /* Implement the tagged_address_p gdbarch method.  */
2453 
2454 static bool
aarch64_linux_tagged_address_p(struct gdbarch * gdbarch,CORE_ADDR address)2455 aarch64_linux_tagged_address_p (struct gdbarch *gdbarch, CORE_ADDR address)
2456 {
2457   /* Remove the top byte for the memory range check.  */
2458   address = gdbarch_remove_non_address_bits (gdbarch, address);
2459 
2460   /* Check if the page that contains ADDRESS is mapped with PROT_MTE.  */
2461   if (!linux_address_in_memtag_page (address))
2462     return false;
2463 
2464   /* We have a valid tag in the top byte of the 64-bit address.  */
2465   return true;
2466 }
2467 
2468 /* Implement the memtag_matches_p gdbarch method.  */
2469 
2470 static bool
aarch64_linux_memtag_matches_p(struct gdbarch * gdbarch,struct value * address)2471 aarch64_linux_memtag_matches_p (struct gdbarch *gdbarch,
2472                                         struct value *address)
2473 {
2474   gdb_assert (address != nullptr);
2475 
2476   CORE_ADDR addr = value_as_address (address);
2477 
2478   /* Fetch the allocation tag for ADDRESS.  */
2479   std::optional<CORE_ADDR> atag
2480     = aarch64_mte_get_atag (gdbarch_remove_non_address_bits (gdbarch, addr));
2481 
2482   if (!atag.has_value ())
2483     return true;
2484 
2485   /* Fetch the logical tag for ADDRESS.  */
2486   gdb_byte ltag = aarch64_mte_get_ltag (addr);
2487 
2488   /* Are the tags the same?  */
2489   return ltag == *atag;
2490 }
2491 
2492 /* Implement the set_memtags gdbarch method.  */
2493 
2494 static bool
aarch64_linux_set_memtags(struct gdbarch * gdbarch,struct value * address,size_t length,const gdb::byte_vector & tags,memtag_type tag_type)2495 aarch64_linux_set_memtags (struct gdbarch *gdbarch, struct value *address,
2496                                  size_t length, const gdb::byte_vector &tags,
2497                                  memtag_type tag_type)
2498 {
2499   gdb_assert (!tags.empty ());
2500   gdb_assert (address != nullptr);
2501 
2502   CORE_ADDR addr = value_as_address (address);
2503 
2504   /* Set the logical tag or the allocation tag.  */
2505   if (tag_type == memtag_type::logical)
2506     {
2507       /* When setting logical tags, we don't care about the length, since
2508            we are only setting a single logical tag.  */
2509       addr = aarch64_mte_set_ltag (addr, tags[0]);
2510 
2511       /* Update the value's content with the tag.  */
2512       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2513       gdb_byte *srcbuf = address->contents_raw ().data ();
2514       store_unsigned_integer (srcbuf, sizeof (addr), byte_order, addr);
2515     }
2516   else
2517     {
2518       /* Remove the top byte.  */
2519       addr = gdbarch_remove_non_address_bits (gdbarch, addr);
2520 
2521       /* With G being the number of tag granules and N the number of tags
2522            passed in, we can have the following cases:
2523 
2524            1 - G == N: Store all the N tags to memory.
2525 
2526            2 - G < N : Warn about having more tags than granules, but write G
2527                          tags.
2528 
2529            3 - G > N : This is a "fill tags" operation.  We should use the tags
2530                          as a pattern to fill the granules repeatedly until we have
2531                          written G tags to memory.
2532       */
2533 
2534       size_t g = aarch64_mte_get_tag_granules (addr, length,
2535                                                          AARCH64_MTE_GRANULE_SIZE);
2536       size_t n = tags.size ();
2537 
2538       if (g < n)
2539           warning (_("Got more tags than memory granules.  Tags will be "
2540                        "truncated."));
2541       else if (g > n)
2542           warning (_("Using tag pattern to fill memory range."));
2543 
2544       if (!target_store_memtags (addr, length, tags,
2545                                          static_cast<int> (memtag_type::allocation)))
2546           return false;
2547     }
2548   return true;
2549 }
2550 
2551 /* Implement the get_memtag gdbarch method.  */
2552 
2553 static struct value *
aarch64_linux_get_memtag(struct gdbarch * gdbarch,struct value * address,memtag_type tag_type)2554 aarch64_linux_get_memtag (struct gdbarch *gdbarch, struct value *address,
2555                                 memtag_type tag_type)
2556 {
2557   gdb_assert (address != nullptr);
2558 
2559   CORE_ADDR addr = value_as_address (address);
2560   CORE_ADDR tag = 0;
2561 
2562   /* Get the logical tag or the allocation tag.  */
2563   if (tag_type == memtag_type::logical)
2564     tag = aarch64_mte_get_ltag (addr);
2565   else
2566     {
2567       /* Remove the top byte.  */
2568       addr = gdbarch_remove_non_address_bits (gdbarch, addr);
2569       std::optional<CORE_ADDR> atag = aarch64_mte_get_atag (addr);
2570 
2571       if (!atag.has_value ())
2572           return nullptr;
2573 
2574       tag = *atag;
2575     }
2576 
2577   /* Convert the tag to a value.  */
2578   return value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
2579                                     tag);
2580 }
2581 
2582 /* Implement the memtag_to_string gdbarch method.  */
2583 
2584 static std::string
aarch64_linux_memtag_to_string(struct gdbarch * gdbarch,struct value * tag_value)2585 aarch64_linux_memtag_to_string (struct gdbarch *gdbarch, struct value *tag_value)
2586 {
2587   if (tag_value == nullptr)
2588     return "";
2589 
2590   CORE_ADDR tag = value_as_address (tag_value);
2591 
2592   return string_printf ("0x%s", phex_nz (tag, sizeof (tag)));
2593 }
2594 
2595 /* AArch64 Linux implementation of the report_signal_info gdbarch
2596    hook.  Displays information about possible memory tag violations.  */
2597 
2598 static void
aarch64_linux_report_signal_info(struct gdbarch * gdbarch,struct ui_out * uiout,enum gdb_signal siggnal)2599 aarch64_linux_report_signal_info (struct gdbarch *gdbarch,
2600                                           struct ui_out *uiout,
2601                                           enum gdb_signal siggnal)
2602 {
2603   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2604 
2605   if (!tdep->has_mte () || siggnal != GDB_SIGNAL_SEGV)
2606     return;
2607 
2608   CORE_ADDR fault_addr = 0;
2609   long si_code = 0;
2610 
2611   try
2612     {
2613       /* Sigcode tells us if the segfault is actually a memory tag
2614            violation.  */
2615       si_code = parse_and_eval_long ("$_siginfo.si_code");
2616 
2617       fault_addr
2618           = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
2619     }
2620   catch (const gdb_exception_error &exception)
2621     {
2622       exception_print (gdb_stderr, exception);
2623       return;
2624     }
2625 
2626   /* If this is not a memory tag violation, just return.  */
2627   if (si_code != SEGV_MTEAERR && si_code != SEGV_MTESERR)
2628     return;
2629 
2630   uiout->text ("\n");
2631 
2632   uiout->field_string ("sigcode-meaning", _("Memory tag violation"));
2633 
2634   /* For synchronous faults, show additional information.  */
2635   if (si_code == SEGV_MTESERR)
2636     {
2637       uiout->text (_(" while accessing address "));
2638       uiout->field_core_addr ("fault-addr", gdbarch, fault_addr);
2639       uiout->text ("\n");
2640 
2641       std::optional<CORE_ADDR> atag
2642           = aarch64_mte_get_atag (gdbarch_remove_non_address_bits (gdbarch,
2643                                                                                  fault_addr));
2644       gdb_byte ltag = aarch64_mte_get_ltag (fault_addr);
2645 
2646       if (!atag.has_value ())
2647           uiout->text (_("Allocation tag unavailable"));
2648       else
2649           {
2650             uiout->text (_("Allocation tag "));
2651             uiout->field_string ("allocation-tag", hex_string (*atag));
2652             uiout->text ("\n");
2653             uiout->text (_("Logical tag "));
2654             uiout->field_string ("logical-tag", hex_string (ltag));
2655           }
2656     }
2657   else
2658     {
2659       uiout->text ("\n");
2660       uiout->text (_("Fault address unavailable"));
2661     }
2662 }
2663 
2664 /* AArch64 Linux implementation of the gdbarch_create_memtag_section hook.  */
2665 
2666 static asection *
aarch64_linux_create_memtag_section(struct gdbarch * gdbarch,bfd * obfd,CORE_ADDR address,size_t size)2667 aarch64_linux_create_memtag_section (struct gdbarch *gdbarch, bfd *obfd,
2668                                              CORE_ADDR address, size_t size)
2669 {
2670   gdb_assert (obfd != nullptr);
2671   gdb_assert (size > 0);
2672 
2673   /* Create the section and associated program header.
2674 
2675      Make sure the section's flags has SEC_HAS_CONTENTS, otherwise BFD will
2676      refuse to write data to this section.  */
2677   asection *mte_section
2678     = bfd_make_section_anyway_with_flags (obfd, "memtag", SEC_HAS_CONTENTS);
2679 
2680   if (mte_section == nullptr)
2681     return nullptr;
2682 
2683   bfd_set_section_vma (mte_section, address);
2684   /* The size of the memory range covered by the memory tags.  We reuse the
2685      section's rawsize field for this purpose.  */
2686   mte_section->rawsize = size;
2687 
2688   /* Fetch the number of tags we need to save.  */
2689   size_t tags_count
2690     = aarch64_mte_get_tag_granules (address, size, AARCH64_MTE_GRANULE_SIZE);
2691   /* Tags are stored packed as 2 tags per byte.  */
2692   bfd_set_section_size (mte_section, (tags_count + 1) >> 1);
2693   /* Store program header information.  */
2694   bfd_record_phdr (obfd, PT_AARCH64_MEMTAG_MTE, 1, 0, 0, 0, 0, 0, 1,
2695                        &mte_section);
2696 
2697   return mte_section;
2698 }
2699 
2700 /* Maximum number of tags to request.  */
2701 #define MAX_TAGS_TO_TRANSFER 1024
2702 
2703 /* AArch64 Linux implementation of the gdbarch_fill_memtag_section hook.  */
2704 
2705 static bool
aarch64_linux_fill_memtag_section(struct gdbarch * gdbarch,asection * osec)2706 aarch64_linux_fill_memtag_section (struct gdbarch *gdbarch, asection *osec)
2707 {
2708   /* We only handle MTE tags for now.  */
2709 
2710   size_t segment_size = osec->rawsize;
2711   CORE_ADDR start_address = bfd_section_vma (osec);
2712   CORE_ADDR end_address = start_address + segment_size;
2713 
2714   /* Figure out how many tags we need to store in this memory range.  */
2715   size_t granules = aarch64_mte_get_tag_granules (start_address, segment_size,
2716                                                               AARCH64_MTE_GRANULE_SIZE);
2717 
2718   /* If there are no tag granules to fetch, just return.  */
2719   if (granules == 0)
2720     return true;
2721 
2722   CORE_ADDR address = start_address;
2723 
2724   /* Vector of tags.  */
2725   gdb::byte_vector tags;
2726 
2727   while (granules > 0)
2728     {
2729       /* Transfer tags in chunks.  */
2730       gdb::byte_vector tags_read;
2731       size_t xfer_len
2732           = ((granules >= MAX_TAGS_TO_TRANSFER)
2733             ? MAX_TAGS_TO_TRANSFER * AARCH64_MTE_GRANULE_SIZE
2734             : granules * AARCH64_MTE_GRANULE_SIZE);
2735 
2736       if (!target_fetch_memtags (address, xfer_len, tags_read,
2737                                          static_cast<int> (memtag_type::allocation)))
2738           {
2739             warning (_("Failed to read MTE tags from memory range [%s,%s)."),
2740                          phex_nz (start_address, sizeof (start_address)),
2741                          phex_nz (end_address, sizeof (end_address)));
2742             return false;
2743           }
2744 
2745       /* Transfer over the tags that have been read.  */
2746       tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
2747 
2748       /* Adjust the remaining granules and starting address.  */
2749       granules -= tags_read.size ();
2750       address += tags_read.size () * AARCH64_MTE_GRANULE_SIZE;
2751     }
2752 
2753   /* Pack the MTE tag bits.  */
2754   aarch64_mte_pack_tags (tags);
2755 
2756   if (!bfd_set_section_contents (osec->owner, osec, tags.data (),
2757                                          0, tags.size ()))
2758     {
2759       warning (_("Failed to write %s bytes of corefile memory "
2760                      "tag content (%s)."),
2761                  pulongest (tags.size ()),
2762                  bfd_errmsg (bfd_get_error ()));
2763     }
2764   return true;
2765 }
2766 
2767 /* AArch64 Linux implementation of the gdbarch_decode_memtag_section
2768    hook.  Decode a memory tag section and return the requested tags.
2769 
2770    The section is guaranteed to cover the [ADDRESS, ADDRESS + length)
2771    range.  */
2772 
2773 static gdb::byte_vector
aarch64_linux_decode_memtag_section(struct gdbarch * gdbarch,bfd_section * section,int type,CORE_ADDR address,size_t length)2774 aarch64_linux_decode_memtag_section (struct gdbarch *gdbarch,
2775                                              bfd_section *section,
2776                                              int type,
2777                                              CORE_ADDR address, size_t length)
2778 {
2779   gdb_assert (section != nullptr);
2780 
2781   /* The requested address must not be less than section->vma.  */
2782   gdb_assert (section->vma <= address);
2783 
2784   /* Figure out how many tags we need to fetch in this memory range.  */
2785   size_t granules = aarch64_mte_get_tag_granules (address, length,
2786                                                               AARCH64_MTE_GRANULE_SIZE);
2787   /* Sanity check.  */
2788   gdb_assert (granules > 0);
2789 
2790   /* Fetch the total number of tags in the range [VMA, address + length).  */
2791   size_t granules_from_vma
2792     = aarch64_mte_get_tag_granules (section->vma,
2793                                             address - section->vma + length,
2794                                             AARCH64_MTE_GRANULE_SIZE);
2795 
2796   /* Adjust the tags vector to contain the exact number of packed bytes.  */
2797   gdb::byte_vector tags (((granules - 1) >> 1) + 1);
2798 
2799   /* Figure out the starting offset into the packed tags data.  */
2800   file_ptr offset = ((granules_from_vma - granules) >> 1);
2801 
2802   if (!bfd_get_section_contents (section->owner, section, tags.data (),
2803                                          offset, tags.size ()))
2804     error (_("Couldn't read contents from memtag section."));
2805 
2806   /* At this point, the tags are packed 2 per byte.  Unpack them before
2807      returning.  */
2808   bool skip_first = ((granules_from_vma - granules) % 2) != 0;
2809   aarch64_mte_unpack_tags (tags, skip_first);
2810 
2811   /* Resize to the exact number of tags that was requested.  */
2812   tags.resize (granules);
2813 
2814   return tags;
2815 }
2816 
2817 /* AArch64 Linux implementation of the
2818    gdbarch_use_target_description_from_corefile_notes hook.  */
2819 
2820 static bool
aarch64_use_target_description_from_corefile_notes(gdbarch * gdbarch,bfd * obfd)2821 aarch64_use_target_description_from_corefile_notes (gdbarch *gdbarch,
2822                                                                 bfd *obfd)
2823 {
2824   /* Sanity check.  */
2825   gdb_assert (obfd != nullptr);
2826 
2827   /* If the corefile contains any SVE or SME register data, we don't want to
2828      use the target description note, as it may be incorrect.
2829 
2830      Currently the target description note contains a potentially incorrect
2831      target description if the originating program changed the SVE or SME
2832      vector lengths mid-execution.
2833 
2834      Once we support per-thread target description notes in the corefiles, we
2835      can always trust those notes whenever they are available.  */
2836   if (bfd_get_section_by_name (obfd, ".reg-aarch-sve") != nullptr
2837       || bfd_get_section_by_name (obfd, ".reg-aarch-za") != nullptr
2838       || bfd_get_section_by_name (obfd, ".reg-aarch-zt") != nullptr)
2839     return false;
2840 
2841   return true;
2842 }
2843 
2844 static void
aarch64_linux_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)2845 aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2846 {
2847   static const char *const stap_integer_prefixes[] = { "#", "", NULL };
2848   static const char *const stap_register_prefixes[] = { "", NULL };
2849   static const char *const stap_register_indirection_prefixes[] = { "[",
2850                                                                                     NULL };
2851   static const char *const stap_register_indirection_suffixes[] = { "]",
2852                                                                                     NULL };
2853   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2854 
2855   tdep->lowest_pc = 0x8000;
2856 
2857   linux_init_abi (info, gdbarch, 1);
2858 
2859   set_solib_svr4_fetch_link_map_offsets (gdbarch,
2860                                                    linux_lp64_fetch_link_map_offsets);
2861 
2862   /* Enable TLS support.  */
2863   set_gdbarch_fetch_tls_load_module_address (gdbarch,
2864                                                        svr4_fetch_objfile_link_map);
2865 
2866   /* Shared library handling.  */
2867   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2868   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
2869 
2870   tramp_frame_prepend_unwinder (gdbarch, &aarch64_linux_rt_sigframe);
2871 
2872   /* Enable longjmp.  */
2873   tdep->jb_pc = 11;
2874 
2875   set_gdbarch_iterate_over_regset_sections
2876     (gdbarch, aarch64_linux_iterate_over_regset_sections);
2877   set_gdbarch_core_read_description
2878     (gdbarch, aarch64_linux_core_read_description);
2879 
2880   /* SystemTap related.  */
2881   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
2882   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
2883   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
2884                                                       stap_register_indirection_prefixes);
2885   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
2886                                                       stap_register_indirection_suffixes);
2887   set_gdbarch_stap_is_single_operand (gdbarch, aarch64_stap_is_single_operand);
2888   set_gdbarch_stap_parse_special_token (gdbarch,
2889                                                   aarch64_stap_parse_special_token);
2890 
2891   /* Reversible debugging, process record.  */
2892   set_gdbarch_process_record (gdbarch, aarch64_process_record);
2893   /* Syscall record.  */
2894   tdep->aarch64_syscall_record = aarch64_linux_syscall_record;
2895 
2896   /* MTE-specific settings and hooks.  */
2897   if (tdep->has_mte ())
2898     {
2899       /* Register a hook for checking if an address is tagged or not.  */
2900       set_gdbarch_tagged_address_p (gdbarch, aarch64_linux_tagged_address_p);
2901 
2902       /* Register a hook for checking if there is a memory tag match.  */
2903       set_gdbarch_memtag_matches_p (gdbarch,
2904                                             aarch64_linux_memtag_matches_p);
2905 
2906       /* Register a hook for setting the logical/allocation tags for
2907            a range of addresses.  */
2908       set_gdbarch_set_memtags (gdbarch, aarch64_linux_set_memtags);
2909 
2910       /* Register a hook for extracting the logical/allocation tag from an
2911            address.  */
2912       set_gdbarch_get_memtag (gdbarch, aarch64_linux_get_memtag);
2913 
2914       /* Set the allocation tag granule size to 16 bytes.  */
2915       set_gdbarch_memtag_granule_size (gdbarch, AARCH64_MTE_GRANULE_SIZE);
2916 
2917       /* Register a hook for converting a memory tag to a string.  */
2918       set_gdbarch_memtag_to_string (gdbarch, aarch64_linux_memtag_to_string);
2919 
2920       set_gdbarch_report_signal_info (gdbarch,
2921                                               aarch64_linux_report_signal_info);
2922 
2923       /* Core file helpers.  */
2924 
2925       /* Core file helper to create a memory tag section for a particular
2926            PT_LOAD segment.  */
2927       set_gdbarch_create_memtag_section
2928           (gdbarch, aarch64_linux_create_memtag_section);
2929 
2930       /* Core file helper to fill a memory tag section with tag data.  */
2931       set_gdbarch_fill_memtag_section
2932           (gdbarch, aarch64_linux_fill_memtag_section);
2933 
2934       /* Core file helper to decode a memory tag section.  */
2935       set_gdbarch_decode_memtag_section (gdbarch,
2936                                                    aarch64_linux_decode_memtag_section);
2937     }
2938 
2939   /* Initialize the aarch64_linux_record_tdep.  */
2940   /* These values are the size of the type that will be used in a system
2941      call.  They are obtained from Linux Kernel source.  */
2942   aarch64_linux_record_tdep.size_pointer
2943     = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
2944   aarch64_linux_record_tdep.size__old_kernel_stat = 32;
2945   aarch64_linux_record_tdep.size_tms = 32;
2946   aarch64_linux_record_tdep.size_loff_t = 8;
2947   aarch64_linux_record_tdep.size_flock = 32;
2948   aarch64_linux_record_tdep.size_oldold_utsname = 45;
2949   aarch64_linux_record_tdep.size_ustat = 32;
2950   aarch64_linux_record_tdep.size_old_sigaction = 32;
2951   aarch64_linux_record_tdep.size_old_sigset_t = 8;
2952   aarch64_linux_record_tdep.size_rlimit = 16;
2953   aarch64_linux_record_tdep.size_rusage = 144;
2954   aarch64_linux_record_tdep.size_timeval = 16;
2955   aarch64_linux_record_tdep.size_timezone = 8;
2956   aarch64_linux_record_tdep.size_old_gid_t = 2;
2957   aarch64_linux_record_tdep.size_old_uid_t = 2;
2958   aarch64_linux_record_tdep.size_fd_set = 128;
2959   aarch64_linux_record_tdep.size_old_dirent = 280;
2960   aarch64_linux_record_tdep.size_statfs = 120;
2961   aarch64_linux_record_tdep.size_statfs64 = 120;
2962   aarch64_linux_record_tdep.size_sockaddr = 16;
2963   aarch64_linux_record_tdep.size_int
2964     = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
2965   aarch64_linux_record_tdep.size_long
2966     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
2967   aarch64_linux_record_tdep.size_ulong
2968     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
2969   aarch64_linux_record_tdep.size_msghdr = 56;
2970   aarch64_linux_record_tdep.size_itimerval = 32;
2971   aarch64_linux_record_tdep.size_stat = 144;
2972   aarch64_linux_record_tdep.size_old_utsname = 325;
2973   aarch64_linux_record_tdep.size_sysinfo = 112;
2974   aarch64_linux_record_tdep.size_msqid_ds = 120;
2975   aarch64_linux_record_tdep.size_shmid_ds = 112;
2976   aarch64_linux_record_tdep.size_new_utsname = 390;
2977   aarch64_linux_record_tdep.size_timex = 208;
2978   aarch64_linux_record_tdep.size_mem_dqinfo = 24;
2979   aarch64_linux_record_tdep.size_if_dqblk = 72;
2980   aarch64_linux_record_tdep.size_fs_quota_stat = 80;
2981   aarch64_linux_record_tdep.size_timespec = 16;
2982   aarch64_linux_record_tdep.size_pollfd = 8;
2983   aarch64_linux_record_tdep.size_NFS_FHSIZE = 32;
2984   aarch64_linux_record_tdep.size_knfsd_fh = 132;
2985   aarch64_linux_record_tdep.size_TASK_COMM_LEN = 16;
2986   aarch64_linux_record_tdep.size_sigaction = 32;
2987   aarch64_linux_record_tdep.size_sigset_t = 8;
2988   aarch64_linux_record_tdep.size_siginfo_t = 128;
2989   aarch64_linux_record_tdep.size_cap_user_data_t = 8;
2990   aarch64_linux_record_tdep.size_stack_t = 24;
2991   aarch64_linux_record_tdep.size_off_t = 8;
2992   aarch64_linux_record_tdep.size_stat64 = 144;
2993   aarch64_linux_record_tdep.size_gid_t = 4;
2994   aarch64_linux_record_tdep.size_uid_t = 4;
2995   aarch64_linux_record_tdep.size_PAGE_SIZE = 4096;
2996   aarch64_linux_record_tdep.size_flock64 = 32;
2997   aarch64_linux_record_tdep.size_user_desc = 16;
2998   aarch64_linux_record_tdep.size_io_event = 32;
2999   aarch64_linux_record_tdep.size_iocb = 64;
3000   aarch64_linux_record_tdep.size_epoll_event = 12;
3001   aarch64_linux_record_tdep.size_itimerspec = 32;
3002   aarch64_linux_record_tdep.size_mq_attr = 64;
3003   aarch64_linux_record_tdep.size_termios = 36;
3004   aarch64_linux_record_tdep.size_termios2 = 44;
3005   aarch64_linux_record_tdep.size_pid_t = 4;
3006   aarch64_linux_record_tdep.size_winsize = 8;
3007   aarch64_linux_record_tdep.size_serial_struct = 72;
3008   aarch64_linux_record_tdep.size_serial_icounter_struct = 80;
3009   aarch64_linux_record_tdep.size_hayes_esp_config = 12;
3010   aarch64_linux_record_tdep.size_size_t = 8;
3011   aarch64_linux_record_tdep.size_iovec = 16;
3012   aarch64_linux_record_tdep.size_time_t = 8;
3013 
3014   /* These values are the second argument of system call "sys_ioctl".
3015      They are obtained from Linux Kernel source.  */
3016   aarch64_linux_record_tdep.ioctl_TCGETS = 0x5401;
3017   aarch64_linux_record_tdep.ioctl_TCSETS = 0x5402;
3018   aarch64_linux_record_tdep.ioctl_TCSETSW = 0x5403;
3019   aarch64_linux_record_tdep.ioctl_TCSETSF = 0x5404;
3020   aarch64_linux_record_tdep.ioctl_TCGETA = 0x5405;
3021   aarch64_linux_record_tdep.ioctl_TCSETA = 0x5406;
3022   aarch64_linux_record_tdep.ioctl_TCSETAW = 0x5407;
3023   aarch64_linux_record_tdep.ioctl_TCSETAF = 0x5408;
3024   aarch64_linux_record_tdep.ioctl_TCSBRK = 0x5409;
3025   aarch64_linux_record_tdep.ioctl_TCXONC = 0x540a;
3026   aarch64_linux_record_tdep.ioctl_TCFLSH = 0x540b;
3027   aarch64_linux_record_tdep.ioctl_TIOCEXCL = 0x540c;
3028   aarch64_linux_record_tdep.ioctl_TIOCNXCL = 0x540d;
3029   aarch64_linux_record_tdep.ioctl_TIOCSCTTY = 0x540e;
3030   aarch64_linux_record_tdep.ioctl_TIOCGPGRP = 0x540f;
3031   aarch64_linux_record_tdep.ioctl_TIOCSPGRP = 0x5410;
3032   aarch64_linux_record_tdep.ioctl_TIOCOUTQ = 0x5411;
3033   aarch64_linux_record_tdep.ioctl_TIOCSTI = 0x5412;
3034   aarch64_linux_record_tdep.ioctl_TIOCGWINSZ = 0x5413;
3035   aarch64_linux_record_tdep.ioctl_TIOCSWINSZ = 0x5414;
3036   aarch64_linux_record_tdep.ioctl_TIOCMGET = 0x5415;
3037   aarch64_linux_record_tdep.ioctl_TIOCMBIS = 0x5416;
3038   aarch64_linux_record_tdep.ioctl_TIOCMBIC = 0x5417;
3039   aarch64_linux_record_tdep.ioctl_TIOCMSET = 0x5418;
3040   aarch64_linux_record_tdep.ioctl_TIOCGSOFTCAR = 0x5419;
3041   aarch64_linux_record_tdep.ioctl_TIOCSSOFTCAR = 0x541a;
3042   aarch64_linux_record_tdep.ioctl_FIONREAD = 0x541b;
3043   aarch64_linux_record_tdep.ioctl_TIOCINQ = 0x541b;
3044   aarch64_linux_record_tdep.ioctl_TIOCLINUX = 0x541c;
3045   aarch64_linux_record_tdep.ioctl_TIOCCONS = 0x541d;
3046   aarch64_linux_record_tdep.ioctl_TIOCGSERIAL = 0x541e;
3047   aarch64_linux_record_tdep.ioctl_TIOCSSERIAL = 0x541f;
3048   aarch64_linux_record_tdep.ioctl_TIOCPKT = 0x5420;
3049   aarch64_linux_record_tdep.ioctl_FIONBIO = 0x5421;
3050   aarch64_linux_record_tdep.ioctl_TIOCNOTTY = 0x5422;
3051   aarch64_linux_record_tdep.ioctl_TIOCSETD = 0x5423;
3052   aarch64_linux_record_tdep.ioctl_TIOCGETD = 0x5424;
3053   aarch64_linux_record_tdep.ioctl_TCSBRKP = 0x5425;
3054   aarch64_linux_record_tdep.ioctl_TIOCTTYGSTRUCT = 0x5426;
3055   aarch64_linux_record_tdep.ioctl_TIOCSBRK = 0x5427;
3056   aarch64_linux_record_tdep.ioctl_TIOCCBRK = 0x5428;
3057   aarch64_linux_record_tdep.ioctl_TIOCGSID = 0x5429;
3058   aarch64_linux_record_tdep.ioctl_TCGETS2 = 0x802c542a;
3059   aarch64_linux_record_tdep.ioctl_TCSETS2 = 0x402c542b;
3060   aarch64_linux_record_tdep.ioctl_TCSETSW2 = 0x402c542c;
3061   aarch64_linux_record_tdep.ioctl_TCSETSF2 = 0x402c542d;
3062   aarch64_linux_record_tdep.ioctl_TIOCGPTN = 0x80045430;
3063   aarch64_linux_record_tdep.ioctl_TIOCSPTLCK = 0x40045431;
3064   aarch64_linux_record_tdep.ioctl_FIONCLEX = 0x5450;
3065   aarch64_linux_record_tdep.ioctl_FIOCLEX = 0x5451;
3066   aarch64_linux_record_tdep.ioctl_FIOASYNC = 0x5452;
3067   aarch64_linux_record_tdep.ioctl_TIOCSERCONFIG = 0x5453;
3068   aarch64_linux_record_tdep.ioctl_TIOCSERGWILD = 0x5454;
3069   aarch64_linux_record_tdep.ioctl_TIOCSERSWILD = 0x5455;
3070   aarch64_linux_record_tdep.ioctl_TIOCGLCKTRMIOS = 0x5456;
3071   aarch64_linux_record_tdep.ioctl_TIOCSLCKTRMIOS = 0x5457;
3072   aarch64_linux_record_tdep.ioctl_TIOCSERGSTRUCT = 0x5458;
3073   aarch64_linux_record_tdep.ioctl_TIOCSERGETLSR = 0x5459;
3074   aarch64_linux_record_tdep.ioctl_TIOCSERGETMULTI = 0x545a;
3075   aarch64_linux_record_tdep.ioctl_TIOCSERSETMULTI = 0x545b;
3076   aarch64_linux_record_tdep.ioctl_TIOCMIWAIT = 0x545c;
3077   aarch64_linux_record_tdep.ioctl_TIOCGICOUNT = 0x545d;
3078   aarch64_linux_record_tdep.ioctl_TIOCGHAYESESP = 0x545e;
3079   aarch64_linux_record_tdep.ioctl_TIOCSHAYESESP = 0x545f;
3080   aarch64_linux_record_tdep.ioctl_FIOQSIZE = 0x5460;
3081 
3082   /* These values are the second argument of system call "sys_fcntl"
3083      and "sys_fcntl64".  They are obtained from Linux Kernel source.  */
3084   aarch64_linux_record_tdep.fcntl_F_GETLK = 5;
3085   aarch64_linux_record_tdep.fcntl_F_GETLK64 = 12;
3086   aarch64_linux_record_tdep.fcntl_F_SETLK64 = 13;
3087   aarch64_linux_record_tdep.fcntl_F_SETLKW64 = 14;
3088 
3089   /* The AArch64 syscall calling convention: reg x0-x6 for arguments,
3090      reg x8 for syscall number and return value in reg x0.  */
3091   aarch64_linux_record_tdep.arg1 = AARCH64_X0_REGNUM + 0;
3092   aarch64_linux_record_tdep.arg2 = AARCH64_X0_REGNUM + 1;
3093   aarch64_linux_record_tdep.arg3 = AARCH64_X0_REGNUM + 2;
3094   aarch64_linux_record_tdep.arg4 = AARCH64_X0_REGNUM + 3;
3095   aarch64_linux_record_tdep.arg5 = AARCH64_X0_REGNUM + 4;
3096   aarch64_linux_record_tdep.arg6 = AARCH64_X0_REGNUM + 5;
3097   aarch64_linux_record_tdep.arg7 = AARCH64_X0_REGNUM + 6;
3098 
3099   /* `catch syscall' */
3100   set_xml_syscall_file_name (gdbarch, "syscalls/aarch64-linux.xml");
3101   set_gdbarch_get_syscall_number (gdbarch, aarch64_linux_get_syscall_number);
3102 
3103   /* Displaced stepping.  */
3104   set_gdbarch_max_insn_length (gdbarch, 4);
3105   set_gdbarch_displaced_step_buffer_length
3106     (gdbarch, 4 * AARCH64_DISPLACED_MODIFIED_INSNS);
3107   set_gdbarch_displaced_step_copy_insn (gdbarch,
3108                                                   aarch64_displaced_step_copy_insn);
3109   set_gdbarch_displaced_step_fixup (gdbarch, aarch64_displaced_step_fixup);
3110   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
3111                                                       aarch64_displaced_step_hw_singlestep);
3112 
3113   set_gdbarch_gcc_target_options (gdbarch, aarch64_linux_gcc_target_options);
3114 
3115   /* Hook to decide if the target description should be obtained from
3116      corefile target description note(s) or inferred from the corefile
3117      sections.  */
3118   set_gdbarch_use_target_description_from_corefile_notes (gdbarch,
3119                                   aarch64_use_target_description_from_corefile_notes);
3120 }
3121 
3122 #if GDB_SELF_TEST
3123 
3124 namespace selftests {
3125 
3126 /* Verify functions to read and write logical tags.  */
3127 
3128 static void
aarch64_linux_ltag_tests(void)3129 aarch64_linux_ltag_tests (void)
3130 {
3131   /* We have 4 bits of tags, but we test writing all the bits of the top
3132      byte of address.  */
3133   for (int i = 0; i < 1 << 8; i++)
3134     {
3135       CORE_ADDR addr = ((CORE_ADDR) i << 56) | 0xdeadbeef;
3136       SELF_CHECK (aarch64_mte_get_ltag (addr) == (i & 0xf));
3137 
3138       addr = aarch64_mte_set_ltag (0xdeadbeef, i);
3139       SELF_CHECK (addr = ((CORE_ADDR) (i & 0xf) << 56) | 0xdeadbeef);
3140     }
3141 }
3142 
3143 } // namespace selftests
3144 #endif /* GDB_SELF_TEST */
3145 
3146 void _initialize_aarch64_linux_tdep ();
3147 void
_initialize_aarch64_linux_tdep()3148 _initialize_aarch64_linux_tdep ()
3149 {
3150   gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_LINUX,
3151                                 aarch64_linux_init_abi);
3152 
3153 #if GDB_SELF_TEST
3154   selftests::register_test ("aarch64-linux-tagged-address",
3155                                   selftests::aarch64_linux_ltag_tests);
3156 #endif
3157 }
3158