gas: correct sb_add_char() 2nd parameter type
[binutils-gdb.git] / gdb / nat / aarch64-scalable-linux-ptrace.c
blob81bb8eab4a8fa4967e8d8e01fcb34e810c0ec020
1 /* Common native Linux code for the AArch64 scalable extensions: SVE and SME.
3 Copyright (C) 2018-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/utsname.h>
21 #include <sys/uio.h>
22 #include "elf/external.h"
23 #include "elf/common.h"
24 #include "aarch64-scalable-linux-ptrace.h"
25 #include "arch/aarch64.h"
26 #include "gdbsupport/common-regcache.h"
27 #include "gdbsupport/byte-vector.h"
28 #include <endian.h>
29 #include "arch/aarch64-scalable-linux.h"
31 /* See nat/aarch64-scalable-linux-ptrace.h. */
33 bool
34 aarch64_has_sve_state (int tid)
36 struct user_sve_header header;
38 if (!read_sve_header (tid, header))
39 return false;
41 if ((header.flags & SVE_PT_REGS_SVE) == 0)
42 return false;
44 if (sizeof (header) == header.size)
45 return false;
47 return true;
50 /* See nat/aarch64-scalable-linux-ptrace.h. */
52 bool
53 aarch64_has_ssve_state (int tid)
55 struct user_sve_header header;
57 if (!read_ssve_header (tid, header))
58 return false;
60 if ((header.flags & SVE_PT_REGS_SVE) == 0)
61 return false;
63 if (sizeof (header) == header.size)
64 return false;
66 return true;
69 /* See nat/aarch64-scalable-linux-ptrace.h. */
71 bool
72 aarch64_has_za_state (int tid)
74 struct user_za_header header;
76 if (!read_za_header (tid, header))
77 return false;
79 if (sizeof (header) == header.size)
80 return false;
82 return true;
85 /* See nat/aarch64-scalable-linux-ptrace.h. */
87 bool
88 read_sve_header (int tid, struct user_sve_header &header)
90 struct iovec iovec;
92 iovec.iov_len = sizeof (header);
93 iovec.iov_base = &header;
95 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_SVE, &iovec) < 0)
97 /* SVE is not supported. */
98 return false;
100 return true;
103 /* See nat/aarch64-scalable-linux-ptrace.h. */
105 bool
106 write_sve_header (int tid, const struct user_sve_header &header)
108 struct iovec iovec;
110 iovec.iov_len = sizeof (header);
111 iovec.iov_base = (void *) &header;
113 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_SVE, &iovec) < 0)
115 /* SVE is not supported. */
116 return false;
118 return true;
121 /* See nat/aarch64-scalable-linux-ptrace.h. */
123 bool
124 read_ssve_header (int tid, struct user_sve_header &header)
126 struct iovec iovec;
128 iovec.iov_len = sizeof (header);
129 iovec.iov_base = &header;
131 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_SSVE, &iovec) < 0)
133 /* SSVE is not supported. */
134 return false;
136 return true;
139 /* See nat/aarch64-scalable-linux-ptrace.h. */
141 bool
142 write_ssve_header (int tid, const struct user_sve_header &header)
144 struct iovec iovec;
146 iovec.iov_len = sizeof (header);
147 iovec.iov_base = (void *) &header;
149 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_SSVE, &iovec) < 0)
151 /* SSVE is not supported. */
152 return false;
154 return true;
157 /* See nat/aarch64-scalable-linux-ptrace.h. */
159 bool
160 read_za_header (int tid, struct user_za_header &header)
162 struct iovec iovec;
164 iovec.iov_len = sizeof (header);
165 iovec.iov_base = &header;
167 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
169 /* ZA is not supported. */
170 return false;
172 return true;
175 /* See nat/aarch64-scalable-linux-ptrace.h. */
177 bool
178 write_za_header (int tid, const struct user_za_header &header)
180 struct iovec iovec;
182 iovec.iov_len = sizeof (header);
183 iovec.iov_base = (void *) &header;
185 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
187 /* ZA is not supported. */
188 return false;
190 return true;
193 /* Given VL, the streaming vector length for SME, return true if it is valid
194 and false otherwise. */
196 static bool
197 aarch64_sme_vl_valid (size_t vl)
199 return (vl == 16 || vl == 32 || vl == 64 || vl == 128 || vl == 256);
202 /* Given VL, the vector length for SVE, return true if it is valid and false
203 otherwise. SVE_state is true when the check is for the SVE register set.
204 Otherwise the check is for the SSVE register set. */
206 static bool
207 aarch64_sve_vl_valid (const bool sve_state, size_t vl)
209 if (sve_state)
210 return sve_vl_valid (vl);
212 /* We have an active SSVE state, where the valid vector length values are
213 more restrictive. */
214 return aarch64_sme_vl_valid (vl);
217 /* See nat/aarch64-scalable-linux-ptrace.h. */
219 uint64_t
220 aarch64_sve_get_vq (int tid)
222 struct iovec iovec;
223 struct user_sve_header header;
224 iovec.iov_len = sizeof (header);
225 iovec.iov_base = &header;
227 /* Figure out which register set to use for the request. The vector length
228 for SVE can be different from the vector length for SSVE. */
229 bool has_sve_state = !aarch64_has_ssve_state (tid);
230 if (ptrace (PTRACE_GETREGSET, tid, has_sve_state? NT_ARM_SVE : NT_ARM_SSVE,
231 &iovec) < 0)
233 /* SVE is not supported. */
234 return 0;
237 /* Ptrace gives the vector length in bytes. Convert it to VQ, the number of
238 128bit chunks in a Z register. We use VQ because 128 bits is the minimum
239 a Z register can increase in size. */
240 uint64_t vq = sve_vq_from_vl (header.vl);
242 if (!aarch64_sve_vl_valid (has_sve_state, header.vl))
244 warning (_("Invalid SVE state from kernel; SVE disabled."));
245 return 0;
248 return vq;
251 /* See nat/aarch64-scalable-linux-ptrace.h. */
253 bool
254 aarch64_sve_set_vq (int tid, uint64_t vq)
256 struct iovec iovec;
257 struct user_sve_header header;
259 iovec.iov_len = sizeof (header);
260 iovec.iov_base = &header;
262 /* Figure out which register set to use for the request. The vector length
263 for SVE can be different from the vector length for SSVE. */
264 bool has_sve_state = !aarch64_has_ssve_state (tid);
265 if (ptrace (PTRACE_GETREGSET, tid, has_sve_state? NT_ARM_SVE : NT_ARM_SSVE,
266 &iovec) < 0)
268 /* SVE/SSVE is not supported. */
269 return false;
272 header.vl = sve_vl_from_vq (vq);
274 if (ptrace (PTRACE_SETREGSET, tid, has_sve_state? NT_ARM_SVE : NT_ARM_SSVE,
275 &iovec) < 0)
277 /* Vector length change failed. */
278 return false;
281 return true;
284 /* See nat/aarch64-scalable-linux-ptrace.h. */
286 bool
287 aarch64_sve_set_vq (int tid, struct reg_buffer_common *reg_buf)
289 uint64_t reg_vg = 0;
291 /* The VG register may not be valid if we've not collected any value yet.
292 This can happen, for example, if we're restoring the regcache after an
293 inferior function call, and the VG register comes after the Z
294 registers. */
295 if (reg_buf->get_register_status (AARCH64_SVE_VG_REGNUM) != REG_VALID)
297 /* If vg is not available yet, fetch it from ptrace. The VG value from
298 ptrace is likely the correct one. */
299 uint64_t vq = aarch64_sve_get_vq (tid);
301 /* If something went wrong, just bail out. */
302 if (vq == 0)
303 return false;
305 reg_vg = sve_vg_from_vq (vq);
307 else
308 reg_buf->raw_collect (AARCH64_SVE_VG_REGNUM, &reg_vg);
310 return aarch64_sve_set_vq (tid, sve_vq_from_vg (reg_vg));
313 /* See nat/aarch64-scalable-linux-ptrace.h. */
315 uint64_t
316 aarch64_za_get_svq (int tid)
318 struct user_za_header header;
319 if (!read_za_header (tid, header))
320 return 0;
322 uint64_t vq = sve_vq_from_vl (header.vl);
324 if (!aarch64_sve_vl_valid (false, header.vl))
326 warning (_("Invalid ZA state from kernel; ZA disabled."));
327 return 0;
330 return vq;
333 /* See nat/aarch64-scalable-linux-ptrace.h. */
335 bool
336 aarch64_za_set_svq (int tid, uint64_t vq)
338 struct iovec iovec;
340 /* Read the NT_ARM_ZA header. */
341 struct user_za_header header;
342 if (!read_za_header (tid, header))
344 /* ZA is not supported. */
345 return false;
348 /* If the size is the correct one already, don't update it. If we do
349 update the streaming vector length, we will invalidate the register
350 state for ZA, and we do not want that. */
351 if (header.vl == sve_vl_from_vq (vq))
352 return true;
354 /* The streaming vector length is about to get updated. Set the new value
355 in the NT_ARM_ZA header and adjust the size as well. */
357 header.vl = sve_vl_from_vq (vq);
358 header.size = sizeof (struct user_za_header);
360 /* Update the NT_ARM_ZA register set with the new streaming vector
361 length. */
362 iovec.iov_len = sizeof (header);
363 iovec.iov_base = &header;
365 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
367 /* Streaming vector length change failed. */
368 return false;
371 /* At this point we have successfully adjusted the streaming vector length
372 for the NT_ARM_ZA register set, and it should have no payload
373 (no ZA state). */
375 return true;
378 /* See nat/aarch64-scalable-linux-ptrace.h. */
380 bool
381 aarch64_za_set_svq (int tid, const struct reg_buffer_common *reg_buf,
382 int svg_regnum)
384 uint64_t reg_svg = 0;
386 /* The svg register may not be valid if we've not collected any value yet.
387 This can happen, for example, if we're restoring the regcache after an
388 inferior function call, and the svg register comes after the Z
389 registers. */
390 if (reg_buf->get_register_status (svg_regnum) != REG_VALID)
392 /* If svg is not available yet, fetch it from ptrace. The svg value from
393 ptrace is likely the correct one. */
394 uint64_t svq = aarch64_za_get_svq (tid);
396 /* If something went wrong, just bail out. */
397 if (svq == 0)
398 return false;
400 reg_svg = sve_vg_from_vq (svq);
402 else
403 reg_buf->raw_collect (svg_regnum, &reg_svg);
405 return aarch64_za_set_svq (tid, sve_vq_from_vg (reg_svg));
408 /* See nat/aarch64-scalable-linux-ptrace.h. */
410 gdb::byte_vector
411 aarch64_fetch_sve_regset (int tid)
413 uint64_t vq = aarch64_sve_get_vq (tid);
415 if (vq == 0)
416 perror_with_name (_("Unable to fetch SVE/SSVE vector length"));
418 /* A ptrace call with NT_ARM_SVE will return a header followed by either a
419 dump of all the SVE and FP registers, or an fpsimd structure (identical to
420 the one returned by NT_FPREGSET) if the kernel has not yet executed any
421 SVE code. Make sure we allocate enough space for a full SVE dump. */
423 gdb::byte_vector sve_state (SVE_PT_SIZE (vq, SVE_PT_REGS_SVE), 0);
425 struct iovec iovec;
426 iovec.iov_base = sve_state.data ();
427 iovec.iov_len = sve_state.size ();
429 bool has_sve_state = !aarch64_has_ssve_state (tid);
430 if (ptrace (PTRACE_GETREGSET, tid, has_sve_state? NT_ARM_SVE : NT_ARM_SSVE,
431 &iovec) < 0)
432 perror_with_name (_("Unable to fetch SVE/SSVE registers"));
434 return sve_state;
437 /* See nat/aarch64-scalable-linux-ptrace.h. */
439 void
440 aarch64_store_sve_regset (int tid, const gdb::byte_vector &sve_state)
442 struct iovec iovec;
443 /* We need to cast from (const void *) here. */
444 iovec.iov_base = (void *) sve_state.data ();
445 iovec.iov_len = sve_state.size ();
447 bool has_sve_state = !aarch64_has_ssve_state (tid);
448 if (ptrace (PTRACE_SETREGSET, tid, has_sve_state? NT_ARM_SVE : NT_ARM_SSVE,
449 &iovec) < 0)
450 perror_with_name (_("Unable to store SVE/SSVE registers"));
453 /* See nat/aarch64-scalable-linux-ptrace.h. */
455 gdb::byte_vector
456 aarch64_fetch_za_regset (int tid)
458 struct user_za_header header;
459 if (!read_za_header (tid, header))
460 error (_("Failed to read NT_ARM_ZA header."));
462 if (!aarch64_sme_vl_valid (header.vl))
463 error (_("Found invalid vector length for NT_ARM_ZA."));
465 struct iovec iovec;
466 iovec.iov_len = header.size;
467 gdb::byte_vector za_state (header.size);
468 iovec.iov_base = za_state.data ();
470 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
471 perror_with_name (_("Failed to fetch NT_ARM_ZA register set."));
473 return za_state;
476 /* See nat/aarch64-scalable-linux-ptrace.h. */
478 void
479 aarch64_store_za_regset (int tid, const gdb::byte_vector &za_state)
481 struct iovec iovec;
482 /* We need to cast from (const void *) here. */
483 iovec.iov_base = (void *) za_state.data ();
484 iovec.iov_len = za_state.size ();
486 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
487 perror_with_name (_("Failed to write to the NT_ARM_ZA register set."));
490 /* See nat/aarch64-scalable-linux-ptrace.h. */
492 void
493 aarch64_initialize_za_regset (int tid)
495 /* First fetch the NT_ARM_ZA header so we can fetch the streaming vector
496 length. */
497 struct user_za_header header;
498 if (!read_za_header (tid, header))
499 error (_("Failed to read NT_ARM_ZA header."));
501 /* The vector should be default-initialized to zero, and we should account
502 for the payload as well. */
503 std::vector<gdb_byte> za_new_state (ZA_PT_SIZE (sve_vq_from_vl (header.vl)));
505 /* Adjust the header size since we are adding the initialized ZA
506 payload. */
507 header.size = ZA_PT_SIZE (sve_vq_from_vl (header.vl));
509 /* Overlay the modified header onto the new ZA state. */
510 const gdb_byte *base = (gdb_byte *) &header;
511 memcpy (za_new_state.data (), base, sizeof (user_za_header));
513 /* Set the ptrace request up and update the NT_ARM_ZA register set. */
514 struct iovec iovec;
515 iovec.iov_len = za_new_state.size ();
516 iovec.iov_base = za_new_state.data ();
518 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_ZA, &iovec) < 0)
519 perror_with_name (_("Failed to initialize the NT_ARM_ZA register set."));
521 if (supports_zt_registers (tid))
523 /* If this target supports SME2, upon initializing ZA, we also need to
524 initialize the ZT registers with 0 values. Do so now. */
525 gdb::byte_vector zt_new_state (AARCH64_SME2_ZT0_SIZE, 0);
526 aarch64_store_zt_regset (tid, zt_new_state);
529 /* The NT_ARM_ZA register set should now contain a zero-initialized ZA
530 payload. */
533 /* See nat/aarch64-scalable-linux-ptrace.h. */
535 gdb::byte_vector
536 aarch64_fetch_zt_regset (int tid)
538 /* Read NT_ARM_ZT. This register set is only available if
539 the ZA bit is non-zero. */
540 gdb::byte_vector zt_state (AARCH64_SME2_ZT0_SIZE);
542 struct iovec iovec;
543 iovec.iov_len = AARCH64_SME2_ZT0_SIZE;
544 iovec.iov_base = zt_state.data ();
546 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_ZT, &iovec) < 0)
547 perror_with_name (_("Failed to fetch NT_ARM_ZT register set."));
549 return zt_state;
552 /* See nat/aarch64-scalable-linux-ptrace.h. */
554 void
555 aarch64_store_zt_regset (int tid, const gdb::byte_vector &zt_state)
557 gdb_assert (zt_state.size () == AARCH64_SME2_ZT0_SIZE
558 || zt_state.size () == 0);
560 /* We need to be mindful of writing data to NT_ARM_ZT. If the ZA bit
561 is 0 and we write something to ZT, it will flip the ZA bit.
563 Right now this is taken care of by callers of this function. */
564 struct iovec iovec;
565 iovec.iov_len = zt_state.size ();
566 iovec.iov_base = (void *) zt_state.data ();
568 /* Write the contents of ZT_STATE to the NT_ARM_ZT register set. */
569 if (ptrace (PTRACE_SETREGSET, tid, NT_ARM_ZT, &iovec) < 0)
570 perror_with_name (_("Failed to write to the NT_ARM_ZT register set."));
573 /* See nat/aarch64-scalable-linux-ptrace.h. */
575 bool
576 supports_zt_registers (int tid)
578 gdb_byte zt_state[AARCH64_SME2_ZT0_SIZE];
580 struct iovec iovec;
581 iovec.iov_len = AARCH64_SME2_ZT0_SIZE;
582 iovec.iov_base = (void *) zt_state;
584 if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_ZT, &iovec) < 0)
585 return false;
587 return true;
590 /* If we are running in BE mode, byteswap the contents
591 of SRC to DST for SIZE bytes. Other, just copy the contents
592 from SRC to DST. */
594 static void
595 aarch64_maybe_swab128 (gdb_byte *dst, const gdb_byte *src, size_t size)
597 gdb_assert (src != nullptr && dst != nullptr);
598 gdb_assert (size > 1);
600 #if (__BYTE_ORDER == __BIG_ENDIAN)
601 for (int i = 0; i < size - 1; i++)
602 dst[i] = src[size - i];
603 #else
604 memcpy (dst, src, size);
605 #endif
608 /* See nat/aarch64-scalable-linux-ptrace.h. */
610 void
611 aarch64_sve_regs_copy_to_reg_buf (int tid, struct reg_buffer_common *reg_buf)
613 gdb::byte_vector sve_state = aarch64_fetch_sve_regset (tid);
615 gdb_byte *base = sve_state.data ();
616 struct user_sve_header *header
617 = (struct user_sve_header *) sve_state.data ();
619 uint64_t vq = sve_vq_from_vl (header->vl);
620 uint64_t vg = sve_vg_from_vl (header->vl);
622 /* Sanity check the data in the header. */
623 if (!sve_vl_valid (header->vl)
624 || SVE_PT_SIZE (vq, header->flags) != header->size)
625 error (_("Invalid SVE header from kernel."));
627 /* Update VG. Note, the registers in the regcache will already be of the
628 correct length. */
629 reg_buf->raw_supply (AARCH64_SVE_VG_REGNUM, &vg);
631 if (HAS_SVE_STATE (*header))
633 /* The register dump contains a set of SVE registers. */
635 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
636 reg_buf->raw_supply (AARCH64_SVE_Z0_REGNUM + i,
637 base + SVE_PT_SVE_ZREG_OFFSET (vq, i));
639 for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
640 reg_buf->raw_supply (AARCH64_SVE_P0_REGNUM + i,
641 base + SVE_PT_SVE_PREG_OFFSET (vq, i));
643 reg_buf->raw_supply (AARCH64_SVE_FFR_REGNUM,
644 base + SVE_PT_SVE_FFR_OFFSET (vq));
645 reg_buf->raw_supply (AARCH64_FPSR_REGNUM,
646 base + SVE_PT_SVE_FPSR_OFFSET (vq));
647 reg_buf->raw_supply (AARCH64_FPCR_REGNUM,
648 base + SVE_PT_SVE_FPCR_OFFSET (vq));
650 else
652 /* WARNING: SIMD state is laid out in memory in target-endian format,
653 while SVE state is laid out in an endianness-independent format (LE).
655 So we have a couple cases to consider:
657 1 - If the target is big endian, then SIMD state is big endian,
658 requiring a byteswap.
660 2 - If the target is little endian, then SIMD state is little endian,
661 which matches the SVE format, so no byteswap is needed. */
663 /* There is no SVE state yet - the register dump contains a fpsimd
664 structure instead. These registers still exist in the hardware, but
665 the kernel has not yet initialised them, and so they will be null. */
667 gdb_byte *reg = (gdb_byte *) alloca (SVE_PT_SVE_ZREG_SIZE (vq));
668 struct user_fpsimd_state *fpsimd
669 = (struct user_fpsimd_state *)(base + SVE_PT_FPSIMD_OFFSET);
671 /* Make sure we have a zeroed register buffer. We will need the zero
672 padding below. */
673 memset (reg, 0, SVE_PT_SVE_ZREG_SIZE (vq));
675 /* Copy across the V registers from fpsimd structure to the Z registers,
676 ensuring the non overlapping state is set to null. */
678 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
680 /* Handle big endian/little endian SIMD/SVE conversion. */
681 aarch64_maybe_swab128 (reg, (const gdb_byte *) &fpsimd->vregs[i],
682 V_REGISTER_SIZE);
683 reg_buf->raw_supply (AARCH64_SVE_Z0_REGNUM + i, reg);
686 reg_buf->raw_supply (AARCH64_FPSR_REGNUM,
687 (const gdb_byte *) &fpsimd->fpsr);
688 reg_buf->raw_supply (AARCH64_FPCR_REGNUM,
689 (const gdb_byte *) &fpsimd->fpcr);
691 /* Clear the SVE only registers. */
692 memset (reg, 0, SVE_PT_SVE_ZREG_SIZE (vq));
694 for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
695 reg_buf->raw_supply (AARCH64_SVE_P0_REGNUM + i, reg);
697 reg_buf->raw_supply (AARCH64_SVE_FFR_REGNUM, reg);
700 /* At this point we have updated the register cache with the contents of
701 the NT_ARM_SVE register set. */
704 /* See nat/aarch64-scalable-linux-ptrace.h. */
706 void
707 aarch64_sve_regs_copy_from_reg_buf (int tid,
708 struct reg_buffer_common *reg_buf)
710 /* First store the vector length to the thread. This is done first to
711 ensure the ptrace buffers read from the kernel are the correct size. */
712 if (!aarch64_sve_set_vq (tid, reg_buf))
713 perror_with_name (_("Unable to set VG register"));
715 /* Obtain a dump of SVE registers from ptrace. */
716 gdb::byte_vector sve_state = aarch64_fetch_sve_regset (tid);
718 struct user_sve_header *header = (struct user_sve_header *) sve_state.data ();
719 uint64_t vq = sve_vq_from_vl (header->vl);
721 gdb::byte_vector new_state (SVE_PT_SIZE (32, SVE_PT_REGS_SVE), 0);
722 memcpy (new_state.data (), sve_state.data (), sve_state.size ());
723 header = (struct user_sve_header *) new_state.data ();
724 gdb_byte *base = new_state.data ();
726 /* Sanity check the data in the header. */
727 if (!sve_vl_valid (header->vl)
728 || SVE_PT_SIZE (vq, header->flags) != header->size)
729 error (_("Invalid SVE header from kernel."));
731 if (!HAS_SVE_STATE (*header))
733 /* There is no SVE state yet - the register dump contains a fpsimd
734 structure instead. Where possible we want to write the reg_buf data
735 back to the kernel using the fpsimd structure. However, if we cannot
736 then we'll need to reformat the fpsimd into a full SVE structure,
737 resulting in the initialization of SVE state written back to the
738 kernel, which is why we try to avoid it. */
740 /* Buffer (using the maximum size a Z register) used to look for zeroed
741 out sve state. */
742 gdb_byte reg[256];
743 memset (reg, 0, sizeof (reg));
745 /* Check in the reg_buf if any of the Z registers are set after the
746 first 128 bits, or if any of the other SVE registers are set. */
747 bool has_sve_state = false;
748 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
750 if (!reg_buf->raw_compare (AARCH64_SVE_Z0_REGNUM + i, reg,
751 V_REGISTER_SIZE))
753 has_sve_state = true;
754 break;
758 if (!has_sve_state)
759 for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
761 if (!reg_buf->raw_compare (AARCH64_SVE_P0_REGNUM + i, reg, 0))
763 has_sve_state = true;
764 break;
768 if (!has_sve_state)
769 has_sve_state
770 = !reg_buf->raw_compare (AARCH64_SVE_FFR_REGNUM, reg, 0);
772 struct user_fpsimd_state *fpsimd
773 = (struct user_fpsimd_state *)(base + SVE_PT_FPSIMD_OFFSET);
775 /* If no SVE state exists, then use the existing fpsimd structure to
776 write out state and return. */
777 if (!has_sve_state)
779 /* WARNING: SIMD state is laid out in memory in target-endian format,
780 while SVE state is laid out in an endianness-independent format
781 (LE).
783 So we have a couple cases to consider:
785 1 - If the target is big endian, then SIMD state is big endian,
786 requiring a byteswap.
788 2 - If the target is little endian, then SIMD state is little
789 endian, which matches the SVE format, so no byteswap is needed. */
791 /* The collects of the Z registers will overflow the size of a vreg.
792 There is enough space in the structure to allow for this, but we
793 cannot overflow into the next register as we might not be
794 collecting every register. */
796 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
798 if (REG_VALID
799 == reg_buf->get_register_status (AARCH64_SVE_Z0_REGNUM + i))
801 reg_buf->raw_collect (AARCH64_SVE_Z0_REGNUM + i, reg);
802 /* Handle big endian/little endian SIMD/SVE conversion. */
803 aarch64_maybe_swab128 ((gdb_byte *) &fpsimd->vregs[i], reg,
804 V_REGISTER_SIZE);
808 if (REG_VALID == reg_buf->get_register_status (AARCH64_FPSR_REGNUM))
809 reg_buf->raw_collect (AARCH64_FPSR_REGNUM,
810 (gdb_byte *) &fpsimd->fpsr);
811 if (REG_VALID == reg_buf->get_register_status (AARCH64_FPCR_REGNUM))
812 reg_buf->raw_collect (AARCH64_FPCR_REGNUM,
813 (gdb_byte *) &fpsimd->fpcr);
815 /* At this point we have collected all the data from the register
816 cache and we are ready to update the FPSIMD register content
817 of the thread. */
819 /* Fall through so we can update the thread's contents with the
820 FPSIMD register cache values. */
822 else
824 /* Otherwise, reformat the fpsimd structure into a full SVE set, by
825 expanding the V registers (working backwards so we don't splat
826 registers before they are copied) and using zero for everything
827 else.
828 Note that enough space for a full SVE dump was originally allocated
829 for base. */
831 header->flags |= SVE_PT_REGS_SVE;
832 header->size = SVE_PT_SIZE (vq, SVE_PT_REGS_SVE);
834 memcpy (base + SVE_PT_SVE_FPSR_OFFSET (vq), &fpsimd->fpsr,
835 sizeof (uint32_t));
836 memcpy (base + SVE_PT_SVE_FPCR_OFFSET (vq), &fpsimd->fpcr,
837 sizeof (uint32_t));
839 for (int i = AARCH64_SVE_Z_REGS_NUM - 1; i >= 0 ; i--)
841 memcpy (base + SVE_PT_SVE_ZREG_OFFSET (vq, i), &fpsimd->vregs[i],
842 sizeof (__int128_t));
845 /* At this point we have converted the FPSIMD layout to an SVE
846 layout and copied the register data.
848 Fall through so we can update the thread's contents with the SVE
849 register cache values. */
852 else
854 /* We already have SVE state for this thread, so we just need to update
855 the values of the registers. */
856 for (int i = 0; i < AARCH64_SVE_Z_REGS_NUM; i++)
857 if (REG_VALID == reg_buf->get_register_status (AARCH64_SVE_Z0_REGNUM
858 + i))
859 reg_buf->raw_collect (AARCH64_SVE_Z0_REGNUM + i,
860 base + SVE_PT_SVE_ZREG_OFFSET (vq, i));
862 for (int i = 0; i < AARCH64_SVE_P_REGS_NUM; i++)
863 if (REG_VALID == reg_buf->get_register_status (AARCH64_SVE_P0_REGNUM
864 + i))
865 reg_buf->raw_collect (AARCH64_SVE_P0_REGNUM + i,
866 base + SVE_PT_SVE_PREG_OFFSET (vq, i));
868 if (REG_VALID == reg_buf->get_register_status (AARCH64_SVE_FFR_REGNUM))
869 reg_buf->raw_collect (AARCH64_SVE_FFR_REGNUM,
870 base + SVE_PT_SVE_FFR_OFFSET (vq));
871 if (REG_VALID == reg_buf->get_register_status (AARCH64_FPSR_REGNUM))
872 reg_buf->raw_collect (AARCH64_FPSR_REGNUM,
873 base + SVE_PT_SVE_FPSR_OFFSET (vq));
874 if (REG_VALID == reg_buf->get_register_status (AARCH64_FPCR_REGNUM))
875 reg_buf->raw_collect (AARCH64_FPCR_REGNUM,
876 base + SVE_PT_SVE_FPCR_OFFSET (vq));
879 /* At this point we have collected all the data from the register cache and
880 we are ready to update the SVE/FPSIMD register contents of the thread.
882 sve_state should contain all the data in the correct format, ready to be
883 passed on to ptrace. */
884 aarch64_store_sve_regset (tid, new_state);
887 /* See nat/aarch64-scalable-linux-ptrace.h. */
889 void
890 aarch64_za_regs_copy_to_reg_buf (int tid, struct reg_buffer_common *reg_buf,
891 int za_regnum, int svg_regnum,
892 int svcr_regnum)
894 /* Fetch the current ZA state from the thread. */
895 gdb::byte_vector za_state = aarch64_fetch_za_regset (tid);
897 /* Sanity check. */
898 gdb_assert (!za_state.empty ());
900 gdb_byte *base = za_state.data ();
901 struct user_za_header *header = (struct user_za_header *) base;
903 /* If we have ZA state, read it. Otherwise, make the contents of ZA
904 in the register cache all zeroes. This is how we present the ZA
905 state when it is not initialized. */
906 uint64_t svcr_value = 0;
907 if (aarch64_has_za_state (tid))
909 /* Sanity check the data in the header. */
910 if (!sve_vl_valid (header->vl)
911 || ZA_PT_SIZE (sve_vq_from_vl (header->vl)) != header->size)
913 error (_("Found invalid streaming vector length in NT_ARM_ZA"
914 " register set"));
917 reg_buf->raw_supply (za_regnum, base + ZA_PT_ZA_OFFSET);
918 svcr_value |= SVCR_ZA_BIT;
920 else
922 size_t za_bytes = header->vl * header->vl;
923 gdb_byte za_zeroed[za_bytes];
924 memset (za_zeroed, 0, za_bytes);
925 reg_buf->raw_supply (za_regnum, za_zeroed);
928 /* Handle the svg and svcr registers separately. We need to calculate
929 their values manually, as the Linux Kernel doesn't expose those
930 explicitly. */
931 svcr_value |= aarch64_has_ssve_state (tid)? SVCR_SM_BIT : 0;
932 uint64_t svg_value = sve_vg_from_vl (header->vl);
934 /* Update the contents of svg and svcr registers. */
935 reg_buf->raw_supply (svg_regnum, &svg_value);
936 reg_buf->raw_supply (svcr_regnum, &svcr_value);
938 /* The register buffer should now contain the updated copy of the NT_ARM_ZA
939 state. */
942 /* See nat/aarch64-scalable-linux-ptrace.h. */
944 void
945 aarch64_za_regs_copy_from_reg_buf (int tid,
946 struct reg_buffer_common *reg_buf,
947 int za_regnum, int svg_regnum,
948 int svcr_regnum)
950 /* REG_BUF contains the updated ZA state. We need to extract that state
951 and write it to the thread TID. */
954 /* First check if there is a change to the streaming vector length. Two
955 outcomes are possible here:
957 1 - The streaming vector length in the register cache differs from the
958 one currently on the thread state. This means that we will need to
959 update the NT_ARM_ZA register set to reflect the new streaming vector
960 length.
962 2 - The streaming vector length in the register cache is the same as in
963 the thread state. This means we do not need to update the NT_ARM_ZA
964 register set for a new streaming vector length, and we only need to
965 deal with changes to za, svg and svcr.
967 None of the two possibilities above imply that the ZA state actually
968 exists. They only determine what needs to be done with any ZA content
969 based on the state of the streaming vector length. */
971 /* First fetch the NT_ARM_ZA header so we can fetch the streaming vector
972 length. */
973 struct user_za_header header;
974 if (!read_za_header (tid, header))
975 error (_("Failed to read NT_ARM_ZA header."));
977 /* Fetch the current streaming vector length. */
978 uint64_t old_svg = sve_vg_from_vl (header.vl);
980 /* Fetch the (potentially) new streaming vector length. */
981 uint64_t new_svg;
982 reg_buf->raw_collect (svg_regnum, &new_svg);
984 /* Did the streaming vector length change? */
985 bool svg_changed = new_svg != old_svg;
987 /* First store the streaming vector length to the thread. This is done
988 first to ensure the ptrace buffers read from the kernel are the correct
989 size. If the streaming vector length is the same as the current one, it
990 won't be updated. */
991 if (!aarch64_za_set_svq (tid, reg_buf, svg_regnum))
992 error (_("Unable to set svg register"));
994 bool has_za_state = aarch64_has_za_state (tid);
996 size_t za_bytes = sve_vl_from_vg (old_svg) * sve_vl_from_vg (old_svg);
997 gdb_byte za_zeroed[za_bytes];
998 memset (za_zeroed, 0, za_bytes);
1000 /* If the streaming vector length changed, zero out the contents of ZA in
1001 the register cache. Otherwise, we will need to update the ZA contents
1002 in the thread with the ZA contents from the register cache, and they will
1003 differ in size. */
1004 if (svg_changed)
1005 reg_buf->raw_supply (za_regnum, za_zeroed);
1007 /* When we update svg, we don't automatically initialize the ZA buffer. If
1008 we have no ZA state and the ZA register contents in the register cache are
1009 zero, just return and leave the ZA register cache contents as zero. */
1010 if (!has_za_state
1011 && reg_buf->raw_compare (za_regnum, za_zeroed, 0))
1013 /* No ZA state in the thread or in the register cache. This was likely
1014 just an adjustment of the streaming vector length. Let this fall
1015 through and update svcr and svg in the register cache. */
1017 else
1019 /* If there is no ZA state but the register cache contains ZA data, we
1020 need to initialize the ZA data through ptrace. First we initialize
1021 all the bytes of ZA to zero. */
1022 if (!has_za_state
1023 && !reg_buf->raw_compare (za_regnum, za_zeroed, 0))
1024 aarch64_initialize_za_regset (tid);
1026 /* From this point onwards, it is assumed we have a ZA payload in
1027 the NT_ARM_ZA register set for this thread, and we need to update
1028 such state based on the contents of the register cache. */
1030 /* Fetch the current ZA state from the thread. */
1031 gdb::byte_vector za_state = aarch64_fetch_za_regset (tid);
1033 gdb_byte *base = za_state.data ();
1034 struct user_za_header *za_header = (struct user_za_header *) base;
1035 uint64_t svq = sve_vq_from_vl (za_header->vl);
1037 /* Sanity check the data in the header. */
1038 if (!sve_vl_valid (za_header->vl)
1039 || ZA_PT_SIZE (svq) != za_header->size)
1040 error (_("Invalid vector length or payload size when reading ZA."));
1042 /* Overwrite the ZA state contained in the thread with the ZA state from
1043 the register cache. */
1044 if (REG_VALID == reg_buf->get_register_status (za_regnum))
1045 reg_buf->raw_collect (za_regnum, base + ZA_PT_ZA_OFFSET);
1047 /* Write back the ZA state to the thread's NT_ARM_ZA register set. */
1048 aarch64_store_za_regset (tid, za_state);
1051 /* Update svcr and svg accordingly. */
1052 uint64_t svcr_value = 0;
1053 svcr_value |= aarch64_has_ssve_state (tid)? SVCR_SM_BIT : 0;
1054 svcr_value |= aarch64_has_za_state (tid)? SVCR_ZA_BIT : 0;
1055 reg_buf->raw_supply (svcr_regnum, &svcr_value);
1057 /* At this point we have written the data contained in the register cache to
1058 the thread's NT_ARM_ZA register set. */
1061 /* See nat/aarch64-scalable-linux-ptrace.h. */
1063 void
1064 aarch64_zt_regs_copy_to_reg_buf (int tid, struct reg_buffer_common *reg_buf,
1065 int zt_regnum)
1067 /* If we have ZA state, read the ZT state. Otherwise, make the contents of
1068 ZT in the register cache all zeroes. This is how we present the ZT
1069 state when it is not initialized (ZA not active). */
1070 if (aarch64_has_za_state (tid))
1072 /* Fetch the current ZT state from the thread. */
1073 gdb::byte_vector zt_state = aarch64_fetch_zt_regset (tid);
1075 /* Sanity check. */
1076 gdb_assert (!zt_state.empty ());
1078 /* Copy the ZT data to the register buffer. */
1079 reg_buf->raw_supply (zt_regnum, zt_state.data ());
1081 else
1083 /* Zero out ZT. */
1084 gdb::byte_vector zt_zeroed (AARCH64_SME2_ZT0_SIZE, 0);
1085 reg_buf->raw_supply (zt_regnum, zt_zeroed.data ());
1088 /* The register buffer should now contain the updated copy of the NT_ARM_ZT
1089 state. */
1092 /* See nat/aarch64-scalable-linux-ptrace.h. */
1094 void
1095 aarch64_zt_regs_copy_from_reg_buf (int tid,
1096 struct reg_buffer_common *reg_buf,
1097 int zt_regnum)
1099 /* Do we have a valid ZA state? */
1100 bool valid_za = aarch64_has_za_state (tid);
1102 /* Is the register buffer contents for ZT all zeroes? */
1103 gdb::byte_vector zt_bytes (AARCH64_SME2_ZT0_SIZE, 0);
1104 bool zt_is_all_zeroes
1105 = reg_buf->raw_compare (zt_regnum, zt_bytes.data (), 0);
1107 /* If ZA state is valid or if we have non-zero data for ZT in the register
1108 buffer, we will invoke ptrace to write the ZT state. Otherwise we don't
1109 have to do anything here. */
1110 if (valid_za || !zt_is_all_zeroes)
1112 if (!valid_za)
1114 /* ZA state is not valid. That means we need to initialize the ZA
1115 state prior to writing the ZT state. */
1116 aarch64_initialize_za_regset (tid);
1119 /* Extract the ZT data from the register buffer. */
1120 reg_buf->raw_collect (zt_regnum, zt_bytes.data ());
1122 /* Write the ZT data to thread TID. */
1123 aarch64_store_zt_regset (tid, zt_bytes);
1126 /* At this point we have (potentially) written the data contained in the
1127 register cache to the thread's NT_ARM_ZT register set. */