1 /* Common target dependent for AArch64 systems.
3 Copyright (C) 2018-2023 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>
22 #include "gdbsupport/common-defs.h"
23 #include "elf/external.h"
24 #include "elf/common.h"
25 #include "aarch64-sve-linux-ptrace.h"
26 #include "arch/aarch64.h"
27 #include "gdbsupport/common-regcache.h"
28 #include "gdbsupport/byte-vector.h"
31 /* See nat/aarch64-sve-linux-ptrace.h. */
34 aarch64_sve_get_vq (int tid
)
37 struct user_sve_header header
;
39 iovec
.iov_len
= sizeof (header
);
40 iovec
.iov_base
= &header
;
42 /* Ptrace gives the vector length in bytes. Convert it to VQ, the number of
43 128bit chunks in a Z register. We use VQ because 128bits is the minimum
44 a Z register can increase in size. */
46 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_SVE
, &iovec
) < 0)
48 /* SVE is not supported. */
52 uint64_t vq
= sve_vq_from_vl (header
.vl
);
54 if (!sve_vl_valid (header
.vl
))
56 warning (_("Invalid SVE state from kernel; SVE disabled."));
63 /* See nat/aarch64-sve-linux-ptrace.h. */
66 aarch64_sve_set_vq (int tid
, uint64_t vq
)
69 struct user_sve_header header
;
71 iovec
.iov_len
= sizeof (header
);
72 iovec
.iov_base
= &header
;
74 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_SVE
, &iovec
) < 0)
76 /* SVE is not supported. */
80 header
.vl
= sve_vl_from_vq (vq
);
82 if (ptrace (PTRACE_SETREGSET
, tid
, NT_ARM_SVE
, &iovec
) < 0)
84 /* Vector length change failed. */
91 /* See nat/aarch64-sve-linux-ptrace.h. */
94 aarch64_sve_set_vq (int tid
, struct reg_buffer_common
*reg_buf
)
98 /* The VG register may not be valid if we've not collected any value yet.
99 This can happen, for example, if we're restoring the regcache after an
100 inferior function call, and the VG register comes after the Z
102 if (reg_buf
->get_register_status (AARCH64_SVE_VG_REGNUM
) != REG_VALID
)
104 /* If vg is not available yet, fetch it from ptrace. The VG value from
105 ptrace is likely the correct one. */
106 uint64_t vq
= aarch64_sve_get_vq (tid
);
108 /* If something went wrong, just bail out. */
112 reg_vg
= sve_vg_from_vq (vq
);
115 reg_buf
->raw_collect (AARCH64_SVE_VG_REGNUM
, ®_vg
);
117 return aarch64_sve_set_vq (tid
, sve_vq_from_vg (reg_vg
));
120 /* See nat/aarch64-sve-linux-ptrace.h. */
122 std::unique_ptr
<gdb_byte
[]>
123 aarch64_sve_get_sveregs (int tid
)
126 uint64_t vq
= aarch64_sve_get_vq (tid
);
129 perror_with_name (_("Unable to fetch SVE register header"));
131 /* A ptrace call with NT_ARM_SVE will return a header followed by either a
132 dump of all the SVE and FP registers, or an fpsimd structure (identical to
133 the one returned by NT_FPREGSET) if the kernel has not yet executed any
134 SVE code. Make sure we allocate enough space for a full SVE dump. */
136 iovec
.iov_len
= SVE_PT_SIZE (vq
, SVE_PT_REGS_SVE
);
137 std::unique_ptr
<gdb_byte
[]> buf (new gdb_byte
[iovec
.iov_len
]);
138 iovec
.iov_base
= buf
.get ();
140 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARM_SVE
, &iovec
) < 0)
141 perror_with_name (_("Unable to fetch SVE registers"));
146 /* If we are running in BE mode, byteswap the contents
147 of SRC to DST for SIZE bytes. Other, just copy the contents
151 aarch64_maybe_swab128 (gdb_byte
*dst
, const gdb_byte
*src
, size_t size
)
153 gdb_assert (src
!= nullptr && dst
!= nullptr);
154 gdb_assert (size
> 1);
156 #if (__BYTE_ORDER == __BIG_ENDIAN)
157 for (int i
= 0; i
< size
- 1; i
++)
158 dst
[i
] = src
[size
- i
];
160 memcpy (dst
, src
, size
);
164 /* See nat/aarch64-sve-linux-ptrace.h. */
167 aarch64_sve_regs_copy_to_reg_buf (struct reg_buffer_common
*reg_buf
,
170 char *base
= (char *) buf
;
171 struct user_sve_header
*header
= (struct user_sve_header
*) buf
;
173 uint64_t vq
= sve_vq_from_vl (header
->vl
);
174 uint64_t vg
= sve_vg_from_vl (header
->vl
);
176 /* Sanity check the data in the header. */
177 if (!sve_vl_valid (header
->vl
)
178 || SVE_PT_SIZE (vq
, header
->flags
) != header
->size
)
179 error (_("Invalid SVE header from kernel."));
181 /* Update VG. Note, the registers in the regcache will already be of the
183 reg_buf
->raw_supply (AARCH64_SVE_VG_REGNUM
, &vg
);
185 if (HAS_SVE_STATE (*header
))
187 /* The register dump contains a set of SVE registers. */
189 for (int i
= 0; i
< AARCH64_SVE_Z_REGS_NUM
; i
++)
190 reg_buf
->raw_supply (AARCH64_SVE_Z0_REGNUM
+ i
,
191 base
+ SVE_PT_SVE_ZREG_OFFSET (vq
, i
));
193 for (int i
= 0; i
< AARCH64_SVE_P_REGS_NUM
; i
++)
194 reg_buf
->raw_supply (AARCH64_SVE_P0_REGNUM
+ i
,
195 base
+ SVE_PT_SVE_PREG_OFFSET (vq
, i
));
197 reg_buf
->raw_supply (AARCH64_SVE_FFR_REGNUM
,
198 base
+ SVE_PT_SVE_FFR_OFFSET (vq
));
199 reg_buf
->raw_supply (AARCH64_FPSR_REGNUM
,
200 base
+ SVE_PT_SVE_FPSR_OFFSET (vq
));
201 reg_buf
->raw_supply (AARCH64_FPCR_REGNUM
,
202 base
+ SVE_PT_SVE_FPCR_OFFSET (vq
));
206 /* WARNING: SIMD state is laid out in memory in target-endian format,
207 while SVE state is laid out in an endianness-independent format (LE).
209 So we have a couple cases to consider:
211 1 - If the target is big endian, then SIMD state is big endian,
212 requiring a byteswap.
214 2 - If the target is little endian, then SIMD state is little endian,
215 which matches the SVE format, so no byteswap is needed. */
217 /* There is no SVE state yet - the register dump contains a fpsimd
218 structure instead. These registers still exist in the hardware, but
219 the kernel has not yet initialised them, and so they will be null. */
221 gdb_byte
*reg
= (gdb_byte
*) alloca (SVE_PT_SVE_ZREG_SIZE (vq
));
222 struct user_fpsimd_state
*fpsimd
223 = (struct user_fpsimd_state
*)(base
+ SVE_PT_FPSIMD_OFFSET
);
225 /* Make sure we have a zeroed register buffer. We will need the zero
227 memset (reg
, 0, SVE_PT_SVE_ZREG_SIZE (vq
));
229 /* Copy across the V registers from fpsimd structure to the Z registers,
230 ensuring the non overlapping state is set to null. */
232 for (int i
= 0; i
< AARCH64_SVE_Z_REGS_NUM
; i
++)
234 /* Handle big endian/little endian SIMD/SVE conversion. */
235 aarch64_maybe_swab128 (reg
, (const gdb_byte
*) &fpsimd
->vregs
[i
],
237 reg_buf
->raw_supply (AARCH64_SVE_Z0_REGNUM
+ i
, reg
);
240 reg_buf
->raw_supply (AARCH64_FPSR_REGNUM
, &fpsimd
->fpsr
);
241 reg_buf
->raw_supply (AARCH64_FPCR_REGNUM
, &fpsimd
->fpcr
);
243 /* Clear the SVE only registers. */
244 memset (reg
, 0, SVE_PT_SVE_ZREG_SIZE (vq
));
246 for (int i
= 0; i
< AARCH64_SVE_P_REGS_NUM
; i
++)
247 reg_buf
->raw_supply (AARCH64_SVE_P0_REGNUM
+ i
, reg
);
249 reg_buf
->raw_supply (AARCH64_SVE_FFR_REGNUM
, reg
);
253 /* See nat/aarch64-sve-linux-ptrace.h. */
256 aarch64_sve_regs_copy_from_reg_buf (const struct reg_buffer_common
*reg_buf
,
259 struct user_sve_header
*header
= (struct user_sve_header
*) buf
;
260 char *base
= (char *) buf
;
261 uint64_t vq
= sve_vq_from_vl (header
->vl
);
263 /* Sanity check the data in the header. */
264 if (!sve_vl_valid (header
->vl
)
265 || SVE_PT_SIZE (vq
, header
->flags
) != header
->size
)
266 error (_("Invalid SVE header from kernel."));
268 if (!HAS_SVE_STATE (*header
))
270 /* There is no SVE state yet - the register dump contains a fpsimd
271 structure instead. Where possible we want to write the reg_buf data
272 back to the kernel using the fpsimd structure. However, if we cannot
273 then we'll need to reformat the fpsimd into a full SVE structure,
274 resulting in the initialization of SVE state written back to the
275 kernel, which is why we try to avoid it. */
277 bool has_sve_state
= false;
278 gdb_byte
*reg
= (gdb_byte
*) alloca (SVE_PT_SVE_ZREG_SIZE (vq
));
279 struct user_fpsimd_state
*fpsimd
280 = (struct user_fpsimd_state
*)(base
+ SVE_PT_FPSIMD_OFFSET
);
282 memset (reg
, 0, SVE_PT_SVE_ZREG_SIZE (vq
));
284 /* Check in the reg_buf if any of the Z registers are set after the
285 first 128 bits, or if any of the other SVE registers are set. */
287 for (int i
= 0; i
< AARCH64_SVE_Z_REGS_NUM
; i
++)
289 has_sve_state
|= reg_buf
->raw_compare (AARCH64_SVE_Z0_REGNUM
+ i
,
290 reg
, sizeof (__int128_t
));
296 for (int i
= 0; i
< AARCH64_SVE_P_REGS_NUM
; i
++)
298 has_sve_state
|= reg_buf
->raw_compare (AARCH64_SVE_P0_REGNUM
+ i
,
305 has_sve_state
|= reg_buf
->raw_compare (AARCH64_SVE_FFR_REGNUM
,
308 /* If no SVE state exists, then use the existing fpsimd structure to
309 write out state and return. */
312 /* WARNING: SIMD state is laid out in memory in target-endian format,
313 while SVE state is laid out in an endianness-independent format
316 So we have a couple cases to consider:
318 1 - If the target is big endian, then SIMD state is big endian,
319 requiring a byteswap.
321 2 - If the target is little endian, then SIMD state is little
322 endian, which matches the SVE format, so no byteswap is needed. */
324 /* The collects of the Z registers will overflow the size of a vreg.
325 There is enough space in the structure to allow for this, but we
326 cannot overflow into the next register as we might not be
327 collecting every register. */
329 for (int i
= 0; i
< AARCH64_SVE_Z_REGS_NUM
; i
++)
332 == reg_buf
->get_register_status (AARCH64_SVE_Z0_REGNUM
+ i
))
334 reg_buf
->raw_collect (AARCH64_SVE_Z0_REGNUM
+ i
, reg
);
335 /* Handle big endian/little endian SIMD/SVE conversion. */
336 aarch64_maybe_swab128 ((gdb_byte
*) &fpsimd
->vregs
[i
], reg
,
341 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_FPSR_REGNUM
))
342 reg_buf
->raw_collect (AARCH64_FPSR_REGNUM
, &fpsimd
->fpsr
);
343 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_FPCR_REGNUM
))
344 reg_buf
->raw_collect (AARCH64_FPCR_REGNUM
, &fpsimd
->fpcr
);
349 /* Otherwise, reformat the fpsimd structure into a full SVE set, by
350 expanding the V registers (working backwards so we don't splat
351 registers before they are copied) and using null for everything else.
352 Note that enough space for a full SVE dump was originally allocated
355 header
->flags
|= SVE_PT_REGS_SVE
;
356 header
->size
= SVE_PT_SIZE (vq
, SVE_PT_REGS_SVE
);
358 memcpy (base
+ SVE_PT_SVE_FPSR_OFFSET (vq
), &fpsimd
->fpsr
,
360 memcpy (base
+ SVE_PT_SVE_FPCR_OFFSET (vq
), &fpsimd
->fpcr
,
363 for (int i
= AARCH64_SVE_Z_REGS_NUM
; i
>= 0 ; i
--)
365 memcpy (base
+ SVE_PT_SVE_ZREG_OFFSET (vq
, i
), &fpsimd
->vregs
[i
],
366 sizeof (__int128_t
));
370 /* Replace the kernel values with those from reg_buf. */
372 for (int i
= 0; i
< AARCH64_SVE_Z_REGS_NUM
; i
++)
373 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_SVE_Z0_REGNUM
+ i
))
374 reg_buf
->raw_collect (AARCH64_SVE_Z0_REGNUM
+ i
,
375 base
+ SVE_PT_SVE_ZREG_OFFSET (vq
, i
));
377 for (int i
= 0; i
< AARCH64_SVE_P_REGS_NUM
; i
++)
378 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_SVE_P0_REGNUM
+ i
))
379 reg_buf
->raw_collect (AARCH64_SVE_P0_REGNUM
+ i
,
380 base
+ SVE_PT_SVE_PREG_OFFSET (vq
, i
));
382 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_SVE_FFR_REGNUM
))
383 reg_buf
->raw_collect (AARCH64_SVE_FFR_REGNUM
,
384 base
+ SVE_PT_SVE_FFR_OFFSET (vq
));
385 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_FPSR_REGNUM
))
386 reg_buf
->raw_collect (AARCH64_FPSR_REGNUM
,
387 base
+ SVE_PT_SVE_FPSR_OFFSET (vq
));
388 if (REG_VALID
== reg_buf
->get_register_status (AARCH64_FPCR_REGNUM
))
389 reg_buf
->raw_collect (AARCH64_FPCR_REGNUM
,
390 base
+ SVE_PT_SVE_FPCR_OFFSET (vq
));