LLDB mainline
NativeRegisterContextLinux_arm64.cpp
Go to the documentation of this file.
1//===-- NativeRegisterContextLinux_arm64.cpp ------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#if defined(__arm64__) || defined(__aarch64__)
10
14
15#include "lldb/Host/HostInfo.h"
19#include "lldb/Utility/Log.h"
21#include "lldb/Utility/Status.h"
22
29
30// System includes - They have to be included after framework includes because
31// they define some macros which collide with variable names in other modules
32#include <sys/uio.h>
33// NT_PRSTATUS and NT_FPREGSET definition
34#include <elf.h>
35#include <mutex>
36#include <optional>
37
38#ifndef NT_ARM_SVE
39#define NT_ARM_SVE 0x405 /* ARM Scalable Vector Extension */
40#endif
41
42#ifndef NT_ARM_SSVE
43#define NT_ARM_SSVE \
44 0x40b /* ARM Scalable Matrix Extension, Streaming SVE mode */
45#endif
46
47#ifndef NT_ARM_ZA
48#define NT_ARM_ZA 0x40c /* ARM Scalable Matrix Extension, Array Storage */
49#endif
50
51#ifndef NT_ARM_ZT
52#define NT_ARM_ZT \
53 0x40d /* ARM Scalable Matrix Extension 2, lookup table register */
54#endif
55
56#ifndef NT_ARM_PAC_MASK
57#define NT_ARM_PAC_MASK 0x406 /* Pointer authentication code masks */
58#endif
59
60#ifndef NT_ARM_TAGGED_ADDR_CTRL
61#define NT_ARM_TAGGED_ADDR_CTRL 0x409 /* Tagged address control register */
62#endif
63
64#ifndef NT_ARM_FPMR
65#define NT_ARM_FPMR 0x40e /* Floating point mode register */
66#endif
67
68#ifndef NT_ARM_POE
69#define NT_ARM_POE 0x40f /* Permission Overlay registers */
70#endif
71
72#ifndef NT_ARM_GCS
73#define NT_ARM_GCS 0x410 /* Guarded Control Stack control registers */
74#endif
75
76#ifndef HWCAP_PACA
77#define HWCAP_PACA (1 << 30)
78#endif
79
80#ifndef HWCAP_GCS
81#define HWCAP_GCS (1UL << 32)
82#endif
83
84#ifndef HWCAP2_MTE
85#define HWCAP2_MTE (1 << 18)
86#endif
87
88#ifndef HWCAP2_FPMR
89#define HWCAP2_FPMR (1UL << 48)
90#endif
91
92#ifndef HWCAP2_POE
93#define HWCAP2_POE (1ULL << 63)
94#endif
95
96using namespace lldb;
97using namespace lldb_private;
98using namespace lldb_private::process_linux;
99
100// A NativeRegisterContext is constructed per thread, but all threads' registers
101// will contain the same fields. Therefore this mutex prevents each instance
102// competing with the other, and subsequent instances from having to detect the
103// fields all over again.
104static std::mutex g_register_flags_detector_mutex;
105static Arm64RegisterFlagsDetector g_register_flags_detector;
106
107std::unique_ptr<NativeRegisterContextLinux>
109 const ArchSpec &target_arch, NativeThreadLinux &native_thread) {
110 switch (target_arch.GetMachine()) {
111 case llvm::Triple::arm:
112 return std::make_unique<NativeRegisterContextLinux_arm>(target_arch,
113 native_thread);
114 case llvm::Triple::aarch64: {
115 // Configure register sets supported by this AArch64 target.
116 // Read SVE header to check for SVE support.
117 struct sve::user_sve_header sve_header;
118 struct iovec ioVec;
119 ioVec.iov_base = &sve_header;
120 ioVec.iov_len = sizeof(sve_header);
121 unsigned int regset = NT_ARM_SVE;
122
123 Flags opt_regsets;
125 native_thread.GetID(), &regset,
126 &ioVec, sizeof(sve_header))
127 .Success())
129
130 // We may have the Scalable Matrix Extension (SME) which adds a
131 // streaming SVE mode. Systems can have SVE and/or SME.
132 ioVec.iov_len = sizeof(sve_header);
133 regset = NT_ARM_SSVE;
135 native_thread.GetID(), &regset,
136 &ioVec, sizeof(sve_header))
137 .Success())
139
140 sve::user_za_header za_header;
141 ioVec.iov_base = &za_header;
142 ioVec.iov_len = sizeof(za_header);
143 regset = NT_ARM_ZA;
145 native_thread.GetID(), &regset,
146 &ioVec, sizeof(za_header))
147 .Success())
149
150 // SME's ZT0 is a 512 bit register.
151 std::array<uint8_t, 64> zt_reg;
152 ioVec.iov_base = zt_reg.data();
153 ioVec.iov_len = zt_reg.size();
154 regset = NT_ARM_ZT;
156 native_thread.GetID(), &regset,
157 &ioVec, zt_reg.size())
158 .Success())
160
161 NativeProcessLinux &process = native_thread.GetProcess();
162
163 std::optional<uint64_t> auxv_at_hwcap =
165 if (auxv_at_hwcap && (*auxv_at_hwcap & HWCAP_PACA))
167
168 std::optional<uint64_t> auxv_at_hwcap2 =
170 if (auxv_at_hwcap2) {
171 if (*auxv_at_hwcap2 & HWCAP2_MTE)
173 if (*auxv_at_hwcap2 & HWCAP2_FPMR)
175 if (*auxv_at_hwcap & HWCAP_GCS)
177 if (*auxv_at_hwcap2 & HWCAP2_POE)
179 }
180
182
183 std::optional<uint64_t> auxv_at_hwcap3 =
185 std::lock_guard<std::mutex> lock(g_register_flags_detector_mutex);
186 if (!g_register_flags_detector.HasDetected())
187 g_register_flags_detector.DetectFields(auxv_at_hwcap.value_or(0),
188 auxv_at_hwcap2.value_or(0),
189 auxv_at_hwcap3.value_or(0));
190
191 auto register_info_up =
192 std::make_unique<RegisterInfoPOSIX_arm64>(target_arch, opt_regsets);
193 return std::make_unique<NativeRegisterContextLinux_arm64>(
194 target_arch, native_thread, std::move(register_info_up));
195 }
196 default:
197 llvm_unreachable("have no register context for architecture");
198 }
199}
200
201llvm::Expected<ArchSpec>
203 return DetermineArchitectureViaGPR(
205}
206
207NativeRegisterContextLinux_arm64::NativeRegisterContextLinux_arm64(
208 const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
209 std::unique_ptr<RegisterInfoPOSIX_arm64> register_info_up)
210 : NativeRegisterContextRegisterInfo(native_thread,
211 register_info_up.release()),
212 NativeRegisterContextLinux(native_thread) {
213 g_register_flags_detector.UpdateRegisterInfo(
214 GetRegisterInfoInterface().GetRegisterInfo(),
215 GetRegisterInfoInterface().GetRegisterCount());
216
217 ::memset(&m_fpr, 0, sizeof(m_fpr));
218 ::memset(&m_gpr_arm64, 0, sizeof(m_gpr_arm64));
219 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
220 ::memset(&m_hbp_regs, 0, sizeof(m_hbp_regs));
221 ::memset(&m_sve_header, 0, sizeof(m_sve_header));
222 ::memset(&m_pac_mask, 0, sizeof(m_pac_mask));
223 ::memset(&m_tls_regs, 0, sizeof(m_tls_regs));
224 ::memset(&m_sme_pseudo_regs, 0, sizeof(m_sme_pseudo_regs));
225 ::memset(&m_gcs_regs, 0, sizeof(m_gcs_regs));
226 ::memset(&m_poe_regs, 0, sizeof(m_poe_regs));
227 std::fill(m_zt_reg.begin(), m_zt_reg.end(), 0);
228
229 m_mte_ctrl_reg = 0;
230 m_fpmr_reg = 0;
231
232 // 16 is just a maximum value, query hardware for actual watchpoint count
233 m_max_hwp_supported = 16;
234 m_max_hbp_supported = 16;
235
236 m_refresh_hwdebug_info = true;
237
238 m_gpr_is_valid = false;
239 m_fpu_is_valid = false;
240 m_sve_buffer_is_valid = false;
241 m_sve_header_is_valid = false;
242 m_pac_mask_is_valid = false;
243 m_mte_ctrl_is_valid = false;
244 m_tls_is_valid = false;
245 m_zt_buffer_is_valid = false;
246 m_fpmr_is_valid = false;
247 m_gcs_is_valid = false;
248 m_poe_is_valid = false;
249
250 // SME adds the tpidr2 register
251 m_tls_size = GetRegisterInfo().IsSSVEPresent() ? sizeof(m_tls_regs)
252 : sizeof(m_tls_regs.tpidr_reg);
253
254 if (GetRegisterInfo().IsSVEPresent() || GetRegisterInfo().IsSSVEPresent())
255 m_sve_state = SVEState::Unknown;
256 else
257 m_sve_state = SVEState::Disabled;
258}
259
261NativeRegisterContextLinux_arm64::GetRegisterInfo() const {
262 return static_cast<RegisterInfoPOSIX_arm64 &>(*m_register_info_interface_up);
263}
264
265uint32_t NativeRegisterContextLinux_arm64::GetRegisterSetCount() const {
266 return GetRegisterInfo().GetRegisterSetCount();
267}
268
269const RegisterSet *
270NativeRegisterContextLinux_arm64::GetRegisterSet(uint32_t set_index) const {
271 return GetRegisterInfo().GetRegisterSet(set_index);
272}
273
274uint32_t NativeRegisterContextLinux_arm64::GetUserRegisterCount() const {
275 uint32_t count = 0;
276 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
277 count += GetRegisterSet(set_index)->num_registers;
278 return count;
279}
280
281Status
282NativeRegisterContextLinux_arm64::ReadRegister(const RegisterInfo *reg_info,
283 RegisterValue &reg_value) {
285
286 if (!reg_info) {
287 error = Status::FromErrorString("reg_info NULL");
288 return error;
289 }
290
291 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
292
293 if (reg == LLDB_INVALID_REGNUM)
295 "no lldb regnum for %s",
296 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
297
298 uint8_t *src;
299 uint32_t offset = LLDB_INVALID_INDEX32;
300 uint64_t sve_vg;
301 std::vector<uint8_t> sve_reg_non_live;
302
303 if (IsGPR(reg)) {
304 error = ReadGPR();
305 if (error.Fail())
306 return error;
307
308 offset = reg_info->byte_offset;
309 assert(offset < GetGPRSize());
310 src = (uint8_t *)GetGPRBuffer() + offset;
311
312 } else if (IsFPR(reg)) {
313 if (m_sve_state == SVEState::Disabled ||
314 m_sve_state == SVEState::StreamingFPSIMD) {
315 // FP registers come from the FP register set when:
316 // * We only have SVE in streaming mode, and we are in non-streaming mode.
317 // * We only have SIMD, no SVE in any mode.
318 error = ReadFPR();
319 if (error.Fail())
320 return error;
321
322 offset = CalculateFprOffset(reg_info,
323 m_sve_state == SVEState::StreamingFPSIMD);
324 assert(offset < GetFPRSize());
325 src = (uint8_t *)GetFPRBuffer() + offset;
326 } else {
327 // SVE or SSVE enabled, we will read and cache SVE ptrace data.
328 // In SIMD or Full mode, the data comes from the SVE regset. In streaming
329 // mode it comes from the streaming SVE regset.
330 error = ReadAllSVE();
331 if (error.Fail())
332 return error;
333
334 // FPSR and FPCR will be located right after Z registers in
335 // SVEState::FPSIMD while in SVEState::Full or SVEState::Streaming they
336 // will be located at the end of register data after an alignment
337 // correction based on currently selected vector length.
338 uint32_t sve_reg_num = LLDB_INVALID_REGNUM;
339 if (reg == GetRegisterInfo().GetRegNumFPSR()) {
340 sve_reg_num = reg;
341 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::Streaming)
342 offset = sve::PTraceFPSROffset(sve::vq_from_vl(m_sve_header.vl));
343 else if (m_sve_state == SVEState::FPSIMD)
344 offset = sve::ptrace_fpsimd_offset + (32 * 16);
345 } else if (reg == GetRegisterInfo().GetRegNumFPCR()) {
346 sve_reg_num = reg;
347 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::Streaming)
348 offset = sve::PTraceFPCROffset(sve::vq_from_vl(m_sve_header.vl));
349 else if (m_sve_state == SVEState::FPSIMD)
350 offset = sve::ptrace_fpsimd_offset + (32 * 16) + 4;
351 } else {
352 // Extract SVE Z register value register number for this reg_info
353 if (reg_info->value_regs &&
354 reg_info->value_regs[0] != LLDB_INVALID_REGNUM)
355 sve_reg_num = reg_info->value_regs[0];
356 offset = CalculateSVEOffset(GetRegisterInfoAtIndex(sve_reg_num));
357 }
358
359 assert(offset < GetSVEBufferSize());
360 src = (uint8_t *)GetSVEBuffer() + offset;
361 }
362 } else if (IsTLS(reg)) {
363 error = ReadTLS();
364 if (error.Fail())
365 return error;
366
367 offset = reg_info->byte_offset - GetRegisterInfo().GetTLSOffset();
368 assert(offset < GetTLSBufferSize());
369 src = (uint8_t *)GetTLSBuffer() + offset;
370 } else if (IsSVE(reg)) {
371 if (m_sve_state == SVEState::Disabled || m_sve_state == SVEState::Unknown)
372 return Status::FromErrorString("SVE disabled or not supported");
373
374 if (GetRegisterInfo().IsSVERegVG(reg)) {
375 error = ReadSVEHeader();
376 if (error.Fail())
377 return error;
378
379 sve_vg = GetSVERegVG();
380 src = (uint8_t *)&sve_vg;
381 } else if (m_sve_state == SVEState::StreamingFPSIMD) {
382 // When we only have streaming SVE and we are in non-streaming mode,
383 // we cannot read streaming SVE registers.
384
385 // P and FFR show as 0s.
386 if (GetRegisterInfo().IsSVEPReg(reg) ||
387 GetRegisterInfo().IsSVERegFFR(reg)) {
388 std::vector<uint8_t> fake_reg(reg_info->byte_size, 0);
389 reg_value.SetFromMemoryData(*reg_info, &fake_reg[0],
390 reg_info->byte_size, eByteOrderLittle,
391 error);
392 return error;
393 }
394
395 // For Z registers, zero extend the 128-bit FP register to Z register
396 // size.
397
398 error = ReadFPR();
399 if (error.Fail())
400 return error;
401
402 // As we told the client we have Z registers, our own internal offsets
403 // are set as if we were using an SVE context. We need to work out
404 // an offset within the FP context instead:
405 // struct user_fpsimd_state {
406 // __uint128_t vregs[32];
407 // __u32 fpsr;
408 // __u32 fpcr;
409 // __u32 __reserved[2];
410 // };
411 const uint32_t z_num = reg - GetRegisterInfo().GetRegNumSVEZ0();
412 offset = z_num * 16;
413 assert(offset < GetFPRSize());
414 src = (uint8_t *)GetFPRBuffer() + offset;
415
416 // Copy from FP into a fake Z value.
417 std::vector<uint8_t> fake_z(reg_info->byte_size, 0);
418 std::memcpy(&fake_z[0], src, 16 /* 128 bits */);
419 reg_value.SetFromMemoryData(*reg_info, &fake_z[0], reg_info->byte_size,
421
422 return error;
423 } else {
424 // SVE enabled, we will read and cache SVE ptrace data
425 error = ReadAllSVE();
426 if (error.Fail())
427 return error;
428
429 if (m_sve_state == SVEState::FPSIMD) {
430 // In FPSIMD state SVE payload mirrors legacy fpsimd struct and so
431 // just copy 16 bytes of v register to the start of z register. All
432 // other SVE register will be set to zero.
433 sve_reg_non_live.resize(reg_info->byte_size, 0);
434 src = sve_reg_non_live.data();
435
436 if (GetRegisterInfo().IsSVEZReg(reg)) {
437 offset = CalculateSVEOffset(reg_info);
438 assert(offset < GetSVEBufferSize());
439 ::memcpy(sve_reg_non_live.data(), (uint8_t *)GetSVEBuffer() + offset,
440 16);
441 }
442 } else {
443 offset = CalculateSVEOffset(reg_info);
444 assert(offset < GetSVEBufferSize());
445 src = (uint8_t *)GetSVEBuffer() + offset;
446 }
447 }
448 } else if (IsPAuth(reg)) {
449 error = ReadPAuthMask();
450 if (error.Fail())
451 return error;
452
453 offset = reg_info->byte_offset - GetRegisterInfo().GetPAuthOffset();
454 assert(offset < GetPACMaskSize());
455 src = (uint8_t *)GetPACMask() + offset;
456 } else if (IsMTE(reg)) {
457 error = ReadMTEControl();
458 if (error.Fail())
459 return error;
460
461 offset = reg_info->byte_offset - GetRegisterInfo().GetMTEOffset();
462 assert(offset < GetMTEControlSize());
463 src = (uint8_t *)GetMTEControl() + offset;
464 } else if (IsSME(reg)) {
465 if (GetRegisterInfo().IsSMERegZA(reg)) {
466 error = ReadZAHeader();
467 if (error.Fail())
468 return error;
469
470 // If there is only a header and no registers, ZA is inactive. Read as 0
471 // in this case.
472 if (m_za_header.size == sizeof(m_za_header)) {
473 // This will get reconfigured/reset later, so we are safe to use it.
474 // ZA is a square of VL * VL and the ptrace buffer also includes the
475 // header itself.
476 m_za_ptrace_payload.resize(((m_za_header.vl) * (m_za_header.vl)) +
477 GetZAHeaderSize());
478 std::fill(m_za_ptrace_payload.begin(), m_za_ptrace_payload.end(), 0);
479 } else {
480 // ZA is active, read the real register.
481 error = ReadZA();
482 if (error.Fail())
483 return error;
484 }
485
486 // ZA is part of the SME set but uses a separate member buffer for
487 // storage. Therefore its effective byte offset is always 0 even if it
488 // isn't 0 within the SME register set.
489 src = (uint8_t *)GetZABuffer() + GetZAHeaderSize();
490 } else if (GetRegisterInfo().IsSMERegZT(reg)) {
491 // Unlike ZA, the kernel will return register data for ZT0 when ZA is not
492 // enabled. This data will be all 0s so we don't have to invent anything
493 // like we did for ZA.
494 error = ReadZT();
495 if (error.Fail())
496 return error;
497
498 src = (uint8_t *)GetZTBuffer();
499 } else {
500 error = ReadSMESVG();
501 if (error.Fail())
502 return error;
503
504 // This is a psuedo so it never fails.
505 ReadSMEControl();
506
507 offset = reg_info->byte_offset - GetRegisterInfo().GetSMEOffset();
508 assert(offset < GetSMEPseudoBufferSize());
509 src = (uint8_t *)GetSMEPseudoBuffer() + offset;
510 }
511 } else if (IsFPMR(reg)) {
512 error = ReadFPMR();
513 if (error.Fail())
514 return error;
515
516 offset = reg_info->byte_offset - GetRegisterInfo().GetFPMROffset();
517 assert(offset < GetFPMRBufferSize());
518 src = (uint8_t *)GetFPMRBuffer() + offset;
519 } else if (IsGCS(reg)) {
520 error = ReadGCS();
521 if (error.Fail())
522 return error;
523
524 offset = reg_info->byte_offset - GetRegisterInfo().GetGCSOffset();
525 assert(offset < GetGCSBufferSize());
526 src = (uint8_t *)GetGCSBuffer() + offset;
527 } else if (IsPOE(reg)) {
528 error = ReadPOE();
529 if (error.Fail())
530 return error;
531
532 offset = reg_info->byte_offset - GetRegisterInfo().GetPOEOffset();
533 assert(offset < GetPOEBufferSize());
534 src = (uint8_t *)GetPOEBuffer() + offset;
535 } else
537 "failed - register wasn't recognized to be a GPR or an FPR, "
538 "write strategy unknown");
539
540 reg_value.SetFromMemoryData(*reg_info, src, reg_info->byte_size,
542
543 return error;
544}
545
546Status NativeRegisterContextLinux_arm64::WriteRegister(
547 const RegisterInfo *reg_info, const RegisterValue &reg_value) {
549
550 if (!reg_info)
551 return Status::FromErrorString("reg_info NULL");
552
553 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
554
555 if (reg == LLDB_INVALID_REGNUM)
557 "no lldb regnum for %s",
558 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
559
560 uint8_t *dst;
561 uint32_t offset = LLDB_INVALID_INDEX32;
562 std::vector<uint8_t> sve_reg_non_live;
563
564 if (IsGPR(reg)) {
565 error = ReadGPR();
566 if (error.Fail())
567 return error;
568
569 assert(reg_info->byte_offset < GetGPRSize());
570 dst = (uint8_t *)GetGPRBuffer() + reg_info->byte_offset;
571 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
572
573 return WriteGPR();
574 } else if (IsFPR(reg)) {
575 if (m_sve_state == SVEState::Disabled ||
576 m_sve_state == SVEState::StreamingFPSIMD) {
577 // SVE is not present, or we only have it in streaming mode and are
578 // currently outside of streaming mode. Take normal route for FPU register
579 // access.
580 error = ReadFPR();
581 if (error.Fail())
582 return error;
583
584 offset = CalculateFprOffset(reg_info,
585 m_sve_state == SVEState::StreamingFPSIMD);
586 assert(offset < GetFPRSize());
587 dst = (uint8_t *)GetFPRBuffer() + offset;
588 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
589
590 return WriteFPR();
591 } else {
592 // SVE enabled, we will read and cache SVE ptrace data.
593 error = ReadAllSVE();
594 if (error.Fail())
595 return error;
596
597 // FPSR and FPCR will be located right after Z registers in
598 // SVEState::FPSIMD while in SVEState::Full or SVEState::Streaming they
599 // will be located at the end of register data after an alignment
600 // correction based on currently selected vector length.
601 uint32_t sve_reg_num = LLDB_INVALID_REGNUM;
602 if (reg == GetRegisterInfo().GetRegNumFPSR()) {
603 sve_reg_num = reg;
604 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::Streaming)
605 offset = sve::PTraceFPSROffset(sve::vq_from_vl(m_sve_header.vl));
606 else if (m_sve_state == SVEState::FPSIMD)
607 offset = sve::ptrace_fpsimd_offset + (32 * 16);
608 } else if (reg == GetRegisterInfo().GetRegNumFPCR()) {
609 sve_reg_num = reg;
610 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::Streaming)
611 offset = sve::PTraceFPCROffset(sve::vq_from_vl(m_sve_header.vl));
612 else if (m_sve_state == SVEState::FPSIMD)
613 offset = sve::ptrace_fpsimd_offset + (32 * 16) + 4;
614 } else {
615 // Extract SVE Z register value register number for this reg_info
616 if (reg_info->value_regs &&
617 reg_info->value_regs[0] != LLDB_INVALID_REGNUM)
618 sve_reg_num = reg_info->value_regs[0];
619 offset = CalculateSVEOffset(GetRegisterInfoAtIndex(sve_reg_num));
620 }
621
622 assert(offset < GetSVEBufferSize());
623 dst = (uint8_t *)GetSVEBuffer() + offset;
624 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
625 return WriteAllSVE();
626 }
627 } else if (IsSVE(reg)) {
628 if (m_sve_state == SVEState::Disabled || m_sve_state == SVEState::Unknown) {
629 return Status::FromErrorString("SVE disabled or not supported");
630 } else if (m_sve_state == SVEState::StreamingFPSIMD) {
631 // When a target has SVE (in any state), the client is told that it has
632 // real SVE registers and that the FP registers are just subregisters
633 // of those SVE registers. This means that any FP write will be converted
634 // into an SVE write.
635 //
636 // If we get here, it did that, but we are outside of streaming mode
637 // on an SME only system. Meaning there's no way at all to write to actual
638 // SVE registers.
639 //
640 // Instead we will extract the bottom 128 bits of the register,
641 // write that via the standard FP route and then return the fake SVE
642 // values as usual.
643 //
644 // We can only do this for Z registers. P, FFR and VG have no SIMD
645 // equivalent.
646 if (GetRegisterInfo().IsSVERegVG(reg) ||
647 GetRegisterInfo().IsSVEPReg(reg) ||
648 GetRegisterInfo().IsSVERegFFR(reg))
650 "Cannot write SVE VG, P or FFR registers while outside of "
651 "streaming mode.");
652
653 // We have told the client that we only have Z registers and the V
654 // registers are subsets of Z. This means that the V byte offsets are
655 // actually for the SVE register context, which we cannot access right
656 // now. That is, v0 is offset 16, v1 is 16+vlen, and so on. So we will
657 // manually patch this data into the FP context and write it.
658 error = ReadFPR();
659 if (error.Fail())
660 return error;
661
662 uint32_t z_num = reg - GetRegisterInfo().GetRegNumSVEZ0();
663 offset = z_num * 16;
664 assert(offset < GetFPRSize());
665 dst = (uint8_t *)GetFPRBuffer() + offset;
666 // If we get here we must have a Z register. Assume we have 16 bytes aka
667 // 128 bits at least, enough to fill an FP V register.
668 ::memcpy(dst, reg_value.GetBytes(), 16);
669
670 return WriteFPR();
671 } else {
672 // Target has SVE enabled, we will read and cache SVE ptrace data
673 error = ReadAllSVE();
674 if (error.Fail())
675 return error;
676
677 if (GetRegisterInfo().IsSVERegVG(reg)) {
678 uint64_t vg_value = reg_value.GetAsUInt64();
679
680 if (sve::vl_valid(vg_value * 8)) {
681 if (m_sve_header_is_valid && vg_value == GetSVERegVG())
682 return error;
683
684 SetSVERegVG(vg_value);
685
686 error = WriteSVEHeader();
687 if (error.Success()) {
688 // Changing VG during streaming mode also changes the size of ZA.
689 if (m_sve_state == SVEState::Streaming)
690 m_za_header_is_valid = false;
691 ConfigureRegisterContext();
692 }
693
694 if (m_sve_header_is_valid && vg_value == GetSVERegVG())
695 return error;
696 }
697
698 return Status::FromErrorString("SVE vector length update failed.");
699 }
700
701 // If target supports SVE but currently in FPSIMD mode.
702 if (m_sve_state == SVEState::FPSIMD) {
703 // Here we will check if writing this SVE register enables
704 // SVEState::Full
705 bool set_sve_state_full = false;
706 const uint8_t *reg_bytes = (const uint8_t *)reg_value.GetBytes();
707 if (GetRegisterInfo().IsSVEZReg(reg)) {
708 for (uint32_t i = 16; i < reg_info->byte_size; i++) {
709 if (reg_bytes[i]) {
710 set_sve_state_full = true;
711 break;
712 }
713 }
714 } else if (GetRegisterInfo().IsSVEPReg(reg) ||
715 reg == GetRegisterInfo().GetRegNumSVEFFR()) {
716 for (uint32_t i = 0; i < reg_info->byte_size; i++) {
717 if (reg_bytes[i]) {
718 set_sve_state_full = true;
719 break;
720 }
721 }
722 }
723
724 if (!set_sve_state_full && GetRegisterInfo().IsSVEZReg(reg)) {
725 // We are writing a Z register which is zero beyond 16 bytes so copy
726 // first 16 bytes only as SVE payload mirrors legacy fpsimd structure
727 offset = CalculateSVEOffset(reg_info);
728 assert(offset < GetSVEBufferSize());
729 dst = (uint8_t *)GetSVEBuffer() + offset;
730 ::memcpy(dst, reg_value.GetBytes(), 16);
731
732 return WriteAllSVE();
733 } else
735 "SVE state change operation not supported");
736 } else {
737 offset = CalculateSVEOffset(reg_info);
738 assert(offset < GetSVEBufferSize());
739 dst = (uint8_t *)GetSVEBuffer() + offset;
740 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
741 return WriteAllSVE();
742 }
743 }
744 } else if (IsMTE(reg)) {
745 error = ReadMTEControl();
746 if (error.Fail())
747 return error;
748
749 offset = reg_info->byte_offset - GetRegisterInfo().GetMTEOffset();
750 assert(offset < GetMTEControlSize());
751 dst = (uint8_t *)GetMTEControl() + offset;
752 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
753
754 return WriteMTEControl();
755 } else if (IsTLS(reg)) {
756 error = ReadTLS();
757 if (error.Fail())
758 return error;
759
760 offset = reg_info->byte_offset - GetRegisterInfo().GetTLSOffset();
761 assert(offset < GetTLSBufferSize());
762 dst = (uint8_t *)GetTLSBuffer() + offset;
763 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
764
765 return WriteTLS();
766 } else if (IsSME(reg)) {
767 if (GetRegisterInfo().IsSMERegZA(reg)) {
768 error = ReadZA();
769 if (error.Fail())
770 return error;
771
772 // ZA is part of the SME set but not stored with the other SME registers.
773 // So its byte offset is effectively always 0.
774 dst = (uint8_t *)GetZABuffer() + GetZAHeaderSize();
775 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
776
777 // While this is writing a header that contains a vector length, the only
778 // way to change that is via the vg register. So here we assume the length
779 // will always be the current length and no reconfigure is needed.
780 return WriteZA();
781 } else if (GetRegisterInfo().IsSMERegZT(reg)) {
782 error = ReadZT();
783 if (error.Fail())
784 return error;
785
786 dst = (uint8_t *)GetZTBuffer();
787 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
788
789 return WriteZT();
790 } else
792 "Writing to SVG or SVCR is not supported.");
793 } else if (IsFPMR(reg)) {
794 error = ReadFPMR();
795 if (error.Fail())
796 return error;
797
798 offset = reg_info->byte_offset - GetRegisterInfo().GetFPMROffset();
799 assert(offset < GetFPMRBufferSize());
800 dst = (uint8_t *)GetFPMRBuffer() + offset;
801 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
802
803 return WriteFPMR();
804 } else if (IsGCS(reg)) {
805 error = ReadGCS();
806 if (error.Fail())
807 return error;
808
809 offset = reg_info->byte_offset - GetRegisterInfo().GetGCSOffset();
810 assert(offset < GetGCSBufferSize());
811 dst = (uint8_t *)GetGCSBuffer() + offset;
812 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
813
814 return WriteGCS();
815 } else if (IsPOE(reg)) {
816 error = ReadPOE();
817 if (error.Fail())
818 return error;
819
820 offset = reg_info->byte_offset - GetRegisterInfo().GetPOEOffset();
821 assert(offset < GetPOEBufferSize());
822 dst = (uint8_t *)GetPOEBuffer() + offset;
823 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
824
825 return WritePOE();
826 }
827
828 return Status::FromErrorString("Failed to write register value");
829}
830
831enum RegisterSetType : uint32_t {
832 GPR, // General purpose registers.
833 SVE, // Used for SVE registers in streaming or non-streaming mode.
834 FPR, // When there is no SVE, or SVE in FPSIMD mode, or streaming only SVE
835 // that is in non-streaming mode.
836 // Pointer authentication registers are read only, so not included here.
837 MTE, // Memory tagging control registers.
838 TLS, // Thread local storage registers.
839 SME, // ZA only, because SVCR and SVG are pseudo registers.
840 SME2, // ZT only.
841 FPMR, // Floating point mode control registers.
842 GCS, // Guarded Control Stack registers.
843 POE, // Permission Overlay registers.
844};
845
846static uint8_t *AddRegisterSetType(uint8_t *dst,
847 RegisterSetType register_set_type) {
848 *(reinterpret_cast<uint32_t *>(dst)) = register_set_type;
849 return dst + sizeof(uint32_t);
850}
851
852static uint8_t *AddSavedRegistersData(uint8_t *dst, void *src, size_t size) {
853 ::memcpy(dst, src, size);
854 return dst + size;
855}
856
857static uint8_t *AddSavedRegisters(uint8_t *dst,
858 enum RegisterSetType register_set_type,
859 void *src, size_t size) {
860 dst = AddRegisterSetType(dst, register_set_type);
861 return AddSavedRegistersData(dst, src, size);
862}
863
864Status
865NativeRegisterContextLinux_arm64::CacheAllRegisters(uint32_t &cached_size) {
867 cached_size = sizeof(RegisterSetType) + GetGPRBufferSize();
868 error = ReadGPR();
869 if (error.Fail())
870 return error;
871
872 if (GetRegisterInfo().IsZAPresent()) {
873 error = ReadZAHeader();
874 if (error.Fail())
875 return error;
876 // Use header size here because the buffer may contain fake data when ZA is
877 // disabled. We do not want to write this fake data (all 0s) because this
878 // would tell the kernel that we want ZA to become active. Which is the
879 // opposite of what we want in the case where it is currently inactive.
880 cached_size += sizeof(RegisterSetType) + m_za_header.size;
881 // For the same reason, we need to force it to be re-read so that it will
882 // always contain the real header.
883 m_za_buffer_is_valid = false;
884 error = ReadZA();
885 if (error.Fail())
886 return error;
887
888 // We will only be restoring ZT data if ZA is active. As writing to an
889 // inactive ZT enables ZA, which may not be desireable.
890 if (
891 // If we have ZT0, or in other words, if we have SME2.
892 GetRegisterInfo().IsZTPresent() &&
893 // And ZA is active, which means that ZT0 is also active.
894 m_za_header.size > sizeof(m_za_header)) {
895 cached_size += sizeof(RegisterSetType) + GetZTBufferSize();
896 // The kernel handles an inactive ZT0 for us, and it will read as 0s if
897 // inactive (unlike ZA where we fake that behaviour).
898 error = ReadZT();
899 if (error.Fail())
900 return error;
901 }
902 }
903
904 // If SVE is enabled we need not copy FPR separately, unless we are in the
905 // non-streaming mode of a streaming only process (as its non-streaming mode
906 // is FPSIMD, rather than SVE).
907 if ((GetRegisterInfo().IsSVEPresent() || GetRegisterInfo().IsSSVEPresent()) &&
908 m_sve_state != SVEState::StreamingFPSIMD) {
909 // Store mode and register data.
910 cached_size +=
911 sizeof(RegisterSetType) + sizeof(m_sve_state) + GetSVEBufferSize();
912 error = ReadAllSVE();
913 } else {
914 cached_size += sizeof(RegisterSetType) + GetFPRSize();
915 error = ReadFPR();
916 }
917 if (error.Fail())
918 return error;
919
920 if (GetRegisterInfo().IsMTEPresent()) {
921 cached_size += sizeof(RegisterSetType) + GetMTEControlSize();
922 error = ReadMTEControl();
923 if (error.Fail())
924 return error;
925 }
926
927 if (GetRegisterInfo().IsFPMRPresent()) {
928 cached_size += sizeof(RegisterSetType) + GetFPMRBufferSize();
929 error = ReadFPMR();
930 if (error.Fail())
931 return error;
932 }
933
934 if (GetRegisterInfo().IsGCSPresent()) {
935 cached_size += sizeof(RegisterSetType) + GetGCSBufferSize();
936 error = ReadGCS();
937 if (error.Fail())
938 return error;
939 }
940
941 if (GetRegisterInfo().IsPOEPresent()) {
942 cached_size += sizeof(RegisterSetType) + GetPOEBufferSize();
943 error = ReadPOE();
944 if (error.Fail())
945 return error;
946 }
947
948 // tpidr is always present but tpidr2 depends on SME.
949 cached_size += sizeof(RegisterSetType) + GetTLSBufferSize();
950 error = ReadTLS();
951
952 return error;
953}
954
955Status NativeRegisterContextLinux_arm64::ReadAllRegisterValues(
957 // AArch64 register data must contain GPRs and either FPR or SVE registers.
958 // SVE registers can be non-streaming (aka SVE) or streaming (aka SSVE).
959 // Finally an optional MTE register. Pointer Authentication (PAC) registers
960 // are read-only and will be skipped.
961
962 // In order to create register data checkpoint we first read all register
963 // values if not done already and calculate total size of register set data.
964 // We store all register values in data_sp by copying full PTrace data that
965 // corresponds to register sets enabled by current register context.
966
967 uint32_t reg_data_byte_size = 0;
968 Status error = CacheAllRegisters(reg_data_byte_size);
969 if (error.Fail())
970 return error;
971
972 data_sp.reset(new DataBufferHeap(reg_data_byte_size, 0));
973 uint8_t *dst = data_sp->GetBytes();
974
975 dst = AddSavedRegisters(dst, RegisterSetType::GPR, GetGPRBuffer(),
976 GetGPRBufferSize());
977
978 // Streaming SVE and the ZA register both use the streaming vector length.
979 // When you change this, the kernel will invalidate parts of the process
980 // state. Therefore we need a specific order of restoration for each mode, if
981 // we also have ZA to restore.
982 //
983 // Streaming mode enabled, ZA enabled:
984 // * Write streaming registers. This sets SVCR.SM and clears SVCR.ZA.
985 // * Write ZA, this set SVCR.ZA. The register data we provide is written to
986 // ZA.
987 // * Result is SVCR.SM and SVCR.ZA set, with the expected data in both
988 // register sets.
989 //
990 // Streaming mode disabled, ZA enabled:
991 // * Write ZA. This sets SVCR.ZA, and the ZA content. In the majority of cases
992 // the streaming vector length is changing, so the thread is converted into
993 // an FPSIMD thread if it is not already one. This also clears SVCR.SM.
994 // * Write SVE registers, which also clears SVCR.SM but most importantly, puts
995 // us into full SVE mode instead of FPSIMD mode (where the registers are
996 // actually the 128 bit Neon registers).
997 // * Result is we have SVCR.SM = 0, SVCR.ZA = 1 and the expected register
998 // state.
999 //
1000 // Restoring in different orders leads to things like the SVE registers being
1001 // truncated due to the FPSIMD mode and ZA being disabled or filled with 0s
1002 // (disabled and 0s looks the same from inside lldb since we fake the value
1003 // when it's disabled).
1004 //
1005 // For more information on this, look up the uses of the relevant NT_ARM_
1006 // constants and the functions vec_set_vector_length, sve_set_common and
1007 // za_set in the Linux Kernel.
1008
1009 if ((m_sve_state != SVEState::Streaming) && GetRegisterInfo().IsZAPresent()) {
1010 // Use the header size not the buffer size, as we may be using the buffer
1011 // for fake data, which we do not want to write out.
1012 assert(m_za_header.size <= GetZABufferSize());
1013 dst = AddSavedRegisters(dst, RegisterSetType::SME, GetZABuffer(),
1014 m_za_header.size);
1015 }
1016
1017 if ((GetRegisterInfo().IsSVEPresent() || GetRegisterInfo().IsSSVEPresent()) &&
1018 m_sve_state != SVEState::StreamingFPSIMD) {
1019 dst = AddRegisterSetType(dst, RegisterSetType::SVE);
1020 *(reinterpret_cast<SVEState *>(dst)) = m_sve_state;
1021 dst += sizeof(m_sve_state);
1022 dst = AddSavedRegistersData(dst, GetSVEBuffer(), GetSVEBufferSize());
1023 } else {
1024 dst = AddSavedRegisters(dst, RegisterSetType::FPR, GetFPRBuffer(),
1025 GetFPRSize());
1026 }
1027
1028 if ((m_sve_state == SVEState::Streaming) && GetRegisterInfo().IsZAPresent()) {
1029 assert(m_za_header.size <= GetZABufferSize());
1030 dst = AddSavedRegisters(dst, RegisterSetType::SME, GetZABuffer(),
1031 m_za_header.size);
1032 }
1033
1034 // If ZT0 is present and we are going to be restoring an active ZA (which
1035 // implies an active ZT0), then restore ZT0 after ZA has been set. This
1036 // prevents us enabling ZA accidentally after the restore of ZA disabled it.
1037 // If we leave ZA/ZT0 inactive and read ZT0, the kernel returns 0s. Therefore
1038 // there's nothing for us to restore if ZA was originally inactive.
1039 if (
1040 // If we have SME2 and therefore ZT0.
1041 GetRegisterInfo().IsZTPresent() &&
1042 // And ZA is enabled.
1043 m_za_header.size > sizeof(m_za_header))
1044 dst = AddSavedRegisters(dst, RegisterSetType::SME2, GetZTBuffer(),
1045 GetZTBufferSize());
1046
1047 if (GetRegisterInfo().IsMTEPresent()) {
1048 dst = AddSavedRegisters(dst, RegisterSetType::MTE, GetMTEControl(),
1049 GetMTEControlSize());
1050 }
1051
1052 if (GetRegisterInfo().IsFPMRPresent()) {
1053 dst = AddSavedRegisters(dst, RegisterSetType::FPMR, GetFPMRBuffer(),
1054 GetFPMRBufferSize());
1055 }
1056
1057 if (GetRegisterInfo().IsGCSPresent()) {
1058 dst = AddSavedRegisters(dst, RegisterSetType::GCS, GetGCSBuffer(),
1059 GetGCSBufferSize());
1060 }
1061
1062 if (GetRegisterInfo().IsPOEPresent()) {
1063 dst = AddSavedRegisters(dst, RegisterSetType::POE, GetPOEBuffer(),
1064 GetPOEBufferSize());
1065 }
1066
1067 dst = AddSavedRegisters(dst, RegisterSetType::TLS, GetTLSBuffer(),
1068 GetTLSBufferSize());
1069
1070 return error;
1071}
1072
1073static Status RestoreRegisters(void *buffer, const uint8_t **src, size_t len,
1074 bool &is_valid, std::function<Status()> writer) {
1075 ::memcpy(buffer, *src, len);
1076 is_valid = true;
1077 *src += len;
1078 return writer();
1079}
1080
1081Status NativeRegisterContextLinux_arm64::WriteAllRegisterValues(
1082 const lldb::DataBufferSP &data_sp) {
1083 // AArch64 register data must contain GPRs, either FPR or SVE registers
1084 // (which can be streaming or non-streaming) and optional MTE register.
1085 // Pointer Authentication (PAC) registers are read-only and will be skipped.
1086
1087 // We store all register values in data_sp by copying full PTrace data that
1088 // corresponds to register sets enabled by current register context. In order
1089 // to restore from register data checkpoint we will first restore GPRs, based
1090 // on size of remaining register data either SVE or FPRs should be restored
1091 // next. SVE is not enabled if we have register data size less than or equal
1092 // to size of GPR + FPR + MTE.
1093
1094 Status error;
1095 if (!data_sp) {
1097 "NativeRegisterContextLinux_arm64::%s invalid data_sp provided",
1098 __FUNCTION__);
1099 return error;
1100 }
1101
1102 const uint8_t *src = data_sp->GetBytes();
1103 if (src == nullptr) {
1105 "NativeRegisterContextLinux_arm64::%s "
1106 "DataBuffer::GetBytes() returned a null "
1107 "pointer",
1108 __FUNCTION__);
1109 return error;
1110 }
1111
1112 uint64_t reg_data_min_size =
1113 GetGPRBufferSize() + GetFPRSize() + 2 * (sizeof(RegisterSetType));
1114 if (data_sp->GetByteSize() < reg_data_min_size) {
1116 "NativeRegisterContextLinux_arm64::%s data_sp contained insufficient "
1117 "register data bytes, expected at least %" PRIu64 ", actual %" PRIu64,
1118 __FUNCTION__, reg_data_min_size, data_sp->GetByteSize());
1119 return error;
1120 }
1121
1122 const uint8_t *end = src + data_sp->GetByteSize();
1123 while (src < end) {
1124 const RegisterSetType kind =
1125 *reinterpret_cast<const RegisterSetType *>(src);
1126 src += sizeof(RegisterSetType);
1127
1128 switch (kind) {
1129 case RegisterSetType::GPR:
1130 error = RestoreRegisters(
1131 GetGPRBuffer(), &src, GetGPRBufferSize(), m_gpr_is_valid,
1132 std::bind(&NativeRegisterContextLinux_arm64::WriteGPR, this));
1133 break;
1134 case RegisterSetType::SVE:
1135 // Restore to the correct mode, streaming or not.
1136 m_sve_state = static_cast<SVEState>(*src);
1137 src += sizeof(m_sve_state);
1138
1139 // First write SVE header. We do not use RestoreRegisters because we do
1140 // not want src to be modified yet.
1141 ::memcpy(GetSVEHeader(), src, GetSVEHeaderSize());
1142 if (!sve::vl_valid(m_sve_header.vl)) {
1143 m_sve_header_is_valid = false;
1145 "NativeRegisterContextLinux_arm64::%s "
1146 "Invalid SVE header in data_sp",
1147 __FUNCTION__);
1148 return error;
1149 }
1150 m_sve_header_is_valid = true;
1151 error = WriteSVEHeader();
1152 if (error.Fail())
1153 return error;
1154
1155 // SVE header has been written configure SVE vector length if needed.
1156 // This could change ZA data too, but that will be restored again later
1157 // anyway.
1158 ConfigureRegisterContext();
1159
1160 // Write header and register data, incrementing src this time.
1161 error = RestoreRegisters(
1162 GetSVEBuffer(), &src, GetSVEBufferSize(), m_sve_buffer_is_valid,
1163 std::bind(&NativeRegisterContextLinux_arm64::WriteAllSVE, this));
1164 break;
1165 case RegisterSetType::FPR: {
1166 m_sve_buffer_is_valid = false;
1167 m_sve_header_is_valid = false;
1168 m_sve_state = SVEState::Unknown;
1169 ConfigureRegisterContext();
1170
1171 // If we are on an SME only system and currently in streaming mode, about
1172 // to restore non-streaming FP data.
1173 if (!GetRegisterInfo().IsSVEPresent() &&
1174 GetRegisterInfo().IsSSVEPresent() &&
1175 m_sve_state == SVEState::Streaming) {
1176 // We can only restore this data on kernel versions >= 6.19, so
1177 // attempt it and if it fails, we will skip restoring the data.
1178 //
1179 // To attempt the restore we write FPSIMD format data to NT_ARM_SVE,
1180 // with the vector length set to 0. If supported, this will switch
1181 // modes from streaming to non-streaming and update the FP registers
1182 // with the values we provided.
1183 //
1184 // This interface is only used by LLDB in this one specific
1185 // circumstance.
1186
1187 size_t data_size = sve::ptrace_fpsimd_offset + GetFPRSize();
1188 // NT_ARM_SVE data must be a multiple of 128 bits, and the FPU data size
1189 // is not, round up.
1190 data_size =
1191 (data_size + sve::vq_bytes - 1) / sve::vq_bytes * sve::vq_bytes;
1192 std::vector<uint8_t> sve_fpsimd_data(data_size);
1193
1194 sve::user_sve_header *header =
1195 reinterpret_cast<sve::user_sve_header *>(sve_fpsimd_data.data());
1196 std::memset(header, 0, sizeof(sve::user_sve_header));
1197 header->size = sve_fpsimd_data.size();
1198 // VL = 0 tells the process to exit streaming mode.
1199 header->vl = 0;
1201 std::memcpy(&sve_fpsimd_data[sve::ptrace_fpsimd_offset], src,
1202 GetFPRSize());
1203
1204 struct iovec ioVec;
1205 ioVec.iov_base = sve_fpsimd_data.data();
1206 ioVec.iov_len = sve_fpsimd_data.size();
1207
1208 // Even though the system does not have SVE, NT_ARM_SVE is used when
1209 // exiting streaming mode.
1210 error = WriteRegisterSet(&ioVec, sve_fpsimd_data.size(), NT_ARM_SVE);
1211
1212 // Consume FP register set.
1213 src += GetFPRSize();
1214
1215 if (error.Success()) {
1216 // Wrote FPU, and SVE overlaps FPU.
1217 m_fpu_is_valid = false;
1218 m_sve_buffer_is_valid = false;
1219 m_sve_header_is_valid = false;
1220
1221 m_sve_state = SVEState::Unknown;
1222 ConfigureRegisterContext();
1223 }
1224 // Else we failed to restore these registers, but we will try to restore
1225 // the others.
1226 } else {
1227 error = RestoreRegisters(
1228 GetFPRBuffer(), &src, GetFPRSize(), m_fpu_is_valid,
1229 std::bind(&NativeRegisterContextLinux_arm64::WriteFPR, this));
1230 }
1231 break;
1232 }
1233 case RegisterSetType::MTE:
1234 error = RestoreRegisters(
1235 GetMTEControl(), &src, GetMTEControlSize(), m_mte_ctrl_is_valid,
1236 std::bind(&NativeRegisterContextLinux_arm64::WriteMTEControl, this));
1237 break;
1238 case RegisterSetType::TLS:
1239 error = RestoreRegisters(
1240 GetTLSBuffer(), &src, GetTLSBufferSize(), m_tls_is_valid,
1241 std::bind(&NativeRegisterContextLinux_arm64::WriteTLS, this));
1242 break;
1243 case RegisterSetType::SME:
1244 // To enable or disable ZA you write the regset with or without register
1245 // data. The kernel detects this by looking at the ioVec's length, not the
1246 // ZA header size you pass in. Therefore we must write header and register
1247 // data (if present) in one go every time. Read the header only first just
1248 // to get the size.
1249 ::memcpy(GetZAHeader(), src, GetZAHeaderSize());
1250 // Read the header and register data. Can't use the buffer size here, it
1251 // may be incorrect due to being filled with dummy data previously. Resize
1252 // this so WriteZA uses the correct size.
1253 m_za_ptrace_payload.resize(m_za_header.size);
1254 ::memcpy(GetZABuffer(), src, GetZABufferSize());
1255 m_za_buffer_is_valid = true;
1256
1257 error = WriteZA();
1258 if (error.Fail())
1259 return error;
1260
1261 // Update size of ZA, which resizes the ptrace payload potentially
1262 // trashing our copy of the data we just wrote.
1263 ConfigureRegisterContext();
1264
1265 // ZA buffer now has proper size, read back the data we wrote above, from
1266 // ptrace.
1267 error = ReadZA();
1268 src += GetZABufferSize();
1269 break;
1270 case RegisterSetType::SME2:
1271 // Doing this would activate an inactive ZA, however we will only get here
1272 // if the state we are restoring had an active ZA. Restoring ZT0 will
1273 // always come after restoring ZA.
1274 error = RestoreRegisters(
1275 GetZTBuffer(), &src, GetZTBufferSize(), m_zt_buffer_is_valid,
1276 std::bind(&NativeRegisterContextLinux_arm64::WriteZT, this));
1277 break;
1278 case RegisterSetType::FPMR:
1279 error = RestoreRegisters(
1280 GetFPMRBuffer(), &src, GetFPMRBufferSize(), m_fpmr_is_valid,
1281 std::bind(&NativeRegisterContextLinux_arm64::WriteFPMR, this));
1282 break;
1283 case RegisterSetType::GCS: {
1284 // It is not permitted to enable GCS via ptrace. We can disable it, but
1285 // to keep things simple we will not revert any change to the
1286 // PR_SHADOW_STACK_ENABLE bit. Instead patch in the current enable bit
1287 // into the registers we are about to restore.
1288 m_gcs_is_valid = false;
1289 error = ReadGCS();
1290 if (error.Fail())
1291 return error;
1292
1293 uint64_t enable_bit = m_gcs_regs.features_enabled & 1UL;
1294 gcs_regs new_gcs_regs = *reinterpret_cast<const gcs_regs *>(src);
1295 new_gcs_regs.features_enabled =
1296 (new_gcs_regs.features_enabled & ~1UL) | enable_bit;
1297
1298 const uint8_t *new_gcs_src =
1299 reinterpret_cast<const uint8_t *>(&new_gcs_regs);
1300 error = RestoreRegisters(
1301 GetGCSBuffer(), &new_gcs_src, GetGCSBufferSize(), m_gcs_is_valid,
1302 std::bind(&NativeRegisterContextLinux_arm64::WriteGCS, this));
1303 src += GetGCSBufferSize();
1304
1305 break;
1306 }
1307 case RegisterSetType::POE:
1308 error = RestoreRegisters(
1309 GetPOEBuffer(), &src, GetPOEBufferSize(), m_poe_is_valid,
1310 std::bind(&NativeRegisterContextLinux_arm64::WritePOE, this));
1311 break;
1312 }
1313
1314 if (error.Fail())
1315 return error;
1316 }
1317
1318 return error;
1319}
1320
1321bool NativeRegisterContextLinux_arm64::IsGPR(unsigned reg) const {
1322 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
1324 return true;
1325 return false;
1326}
1327
1328bool NativeRegisterContextLinux_arm64::IsFPR(unsigned reg) const {
1329 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
1331 return true;
1332 return false;
1333}
1334
1335bool NativeRegisterContextLinux_arm64::IsSVE(unsigned reg) const {
1336 return GetRegisterInfo().IsSVEReg(reg);
1337}
1338
1339bool NativeRegisterContextLinux_arm64::IsSME(unsigned reg) const {
1340 return GetRegisterInfo().IsSMEReg(reg);
1341}
1342
1343bool NativeRegisterContextLinux_arm64::IsPAuth(unsigned reg) const {
1344 return GetRegisterInfo().IsPAuthReg(reg);
1345}
1346
1347bool NativeRegisterContextLinux_arm64::IsMTE(unsigned reg) const {
1348 return GetRegisterInfo().IsMTEReg(reg);
1349}
1350
1351bool NativeRegisterContextLinux_arm64::IsTLS(unsigned reg) const {
1352 return GetRegisterInfo().IsTLSReg(reg);
1353}
1354
1355bool NativeRegisterContextLinux_arm64::IsFPMR(unsigned reg) const {
1356 return GetRegisterInfo().IsFPMRReg(reg);
1357}
1358
1359bool NativeRegisterContextLinux_arm64::IsGCS(unsigned reg) const {
1360 return GetRegisterInfo().IsGCSReg(reg);
1361}
1362
1363bool NativeRegisterContextLinux_arm64::IsPOE(unsigned reg) const {
1364 return GetRegisterInfo().IsPOEReg(reg);
1365}
1366
1367llvm::Error NativeRegisterContextLinux_arm64::ReadHardwareDebugInfo() {
1368 if (!m_refresh_hwdebug_info) {
1369 return llvm::Error::success();
1370 }
1371
1372 ::pid_t tid = m_thread.GetID();
1373
1374 Status error = arm64::ReadHardwareDebugInfo(tid, m_max_hwp_supported,
1375 m_max_hbp_supported);
1376 if (error.Fail())
1377 return error.ToError();
1378
1379 m_refresh_hwdebug_info = false;
1380
1381 return llvm::Error::success();
1382}
1383
1384llvm::Error
1385NativeRegisterContextLinux_arm64::WriteHardwareDebugRegs(DREGType hwbType) {
1386 uint32_t max_supported =
1387 (hwbType == eDREGTypeWATCH) ? m_max_hwp_supported : m_max_hbp_supported;
1388 auto &regs = (hwbType == eDREGTypeWATCH) ? m_hwp_regs : m_hbp_regs;
1389 return arm64::WriteHardwareDebugRegs(hwbType, m_thread.GetID(), max_supported,
1390 regs)
1391 .ToError();
1392}
1393
1394Status NativeRegisterContextLinux_arm64::ReadGPR() {
1395 Status error;
1396
1397 if (m_gpr_is_valid)
1398 return error;
1399
1400 struct iovec ioVec;
1401 ioVec.iov_base = GetGPRBuffer();
1402 ioVec.iov_len = GetGPRBufferSize();
1403
1404 error = ReadRegisterSet(&ioVec, GetGPRBufferSize(), NT_PRSTATUS);
1405
1406 if (error.Success())
1407 m_gpr_is_valid = true;
1408
1409 return error;
1410}
1411
1412Status NativeRegisterContextLinux_arm64::WriteGPR() {
1413 Status error = ReadGPR();
1414 if (error.Fail())
1415 return error;
1416
1417 struct iovec ioVec;
1418 ioVec.iov_base = GetGPRBuffer();
1419 ioVec.iov_len = GetGPRBufferSize();
1420
1421 m_gpr_is_valid = false;
1422
1423 return WriteRegisterSet(&ioVec, GetGPRBufferSize(), NT_PRSTATUS);
1424}
1425
1426Status NativeRegisterContextLinux_arm64::ReadFPR() {
1427 Status error;
1428
1429 if (m_fpu_is_valid)
1430 return error;
1431
1432 struct iovec ioVec;
1433 ioVec.iov_base = GetFPRBuffer();
1434 ioVec.iov_len = GetFPRSize();
1435
1436 error = ReadRegisterSet(&ioVec, GetFPRSize(), NT_FPREGSET);
1437 if (error.Success())
1438 m_fpu_is_valid = true;
1439
1440 return error;
1441}
1442
1443Status NativeRegisterContextLinux_arm64::WriteFPR() {
1444 Status error = ReadFPR();
1445 if (error.Fail())
1446 return error;
1447
1448 struct iovec ioVec;
1449 ioVec.iov_base = GetFPRBuffer();
1450 ioVec.iov_len = GetFPRSize();
1451
1452 m_fpu_is_valid = false;
1453 // SVE Z registers overlap the FP registers.
1454 m_sve_buffer_is_valid = false;
1455 m_sve_header_is_valid = false;
1456
1457 return WriteRegisterSet(&ioVec, GetFPRSize(), NT_FPREGSET);
1458}
1459
1460void NativeRegisterContextLinux_arm64::InvalidateAllRegisters() {
1461 m_gpr_is_valid = false;
1462 m_fpu_is_valid = false;
1463 m_sve_buffer_is_valid = false;
1464 m_sve_header_is_valid = false;
1465 m_za_buffer_is_valid = false;
1466 m_za_header_is_valid = false;
1467 m_pac_mask_is_valid = false;
1468 m_mte_ctrl_is_valid = false;
1469 m_tls_is_valid = false;
1470 m_zt_buffer_is_valid = false;
1471 m_fpmr_is_valid = false;
1472 m_gcs_is_valid = false;
1473 m_poe_is_valid = false;
1474
1475 // Update SVE and ZA registers in case there is change in configuration.
1476 ConfigureRegisterContext();
1477}
1478
1479unsigned NativeRegisterContextLinux_arm64::GetSVERegSet() {
1480 switch (m_sve_state) {
1483 return NT_ARM_SSVE;
1484 default:
1485 return NT_ARM_SVE;
1486 }
1487}
1488
1489Status NativeRegisterContextLinux_arm64::ReadSVEHeader() {
1490 Status error;
1491
1492 if (m_sve_header_is_valid)
1493 return error;
1494
1495 struct iovec ioVec;
1496 ioVec.iov_base = GetSVEHeader();
1497 ioVec.iov_len = GetSVEHeaderSize();
1498
1499 error = ReadRegisterSet(&ioVec, GetSVEHeaderSize(), GetSVERegSet());
1500
1501 if (error.Success())
1502 m_sve_header_is_valid = true;
1503
1504 return error;
1505}
1506
1507Status NativeRegisterContextLinux_arm64::ReadPAuthMask() {
1508 Status error;
1509
1510 if (m_pac_mask_is_valid)
1511 return error;
1512
1513 struct iovec ioVec;
1514 ioVec.iov_base = GetPACMask();
1515 ioVec.iov_len = GetPACMaskSize();
1516
1517 error = ReadRegisterSet(&ioVec, GetPACMaskSize(), NT_ARM_PAC_MASK);
1518
1519 if (error.Success())
1520 m_pac_mask_is_valid = true;
1521
1522 return error;
1523}
1524
1525Status NativeRegisterContextLinux_arm64::WriteSVEHeader() {
1526 Status error;
1527
1528 error = ReadSVEHeader();
1529 if (error.Fail())
1530 return error;
1531
1532 struct iovec ioVec;
1533 ioVec.iov_base = GetSVEHeader();
1534 ioVec.iov_len = GetSVEHeaderSize();
1535
1536 m_sve_buffer_is_valid = false;
1537 m_sve_header_is_valid = false;
1538 m_fpu_is_valid = false;
1539
1540 return WriteRegisterSet(&ioVec, GetSVEHeaderSize(), GetSVERegSet());
1541}
1542
1543Status NativeRegisterContextLinux_arm64::ReadAllSVE() {
1544 Status error;
1545 if (m_sve_buffer_is_valid)
1546 return error;
1547
1548 struct iovec ioVec;
1549 ioVec.iov_base = GetSVEBuffer();
1550 ioVec.iov_len = GetSVEBufferSize();
1551
1552 error = ReadRegisterSet(&ioVec, GetSVEBufferSize(), GetSVERegSet());
1553
1554 if (error.Success())
1555 m_sve_buffer_is_valid = true;
1556
1557 return error;
1558}
1559
1560Status NativeRegisterContextLinux_arm64::WriteAllSVE() {
1561 Status error;
1562
1563 error = ReadAllSVE();
1564 if (error.Fail())
1565 return error;
1566
1567 struct iovec ioVec;
1568
1569 ioVec.iov_base = GetSVEBuffer();
1570 ioVec.iov_len = GetSVEBufferSize();
1571
1572 m_sve_buffer_is_valid = false;
1573 m_sve_header_is_valid = false;
1574 m_fpu_is_valid = false;
1575
1576 return WriteRegisterSet(&ioVec, GetSVEBufferSize(), GetSVERegSet());
1577}
1578
1579Status NativeRegisterContextLinux_arm64::ReadSMEControl() {
1580 // The real register is SVCR and is accessible from EL0. However we don't want
1581 // to have to JIT code into the target process so we'll just recreate it using
1582 // what we know from ptrace.
1583
1584 // Bit 0 indicates whether streaming mode is active.
1585 m_sme_pseudo_regs.ctrl_reg = m_sve_state == SVEState::Streaming;
1586
1587 // Bit 1 indicates whether the array storage is active.
1588 // It is active if we can read the header and the size field tells us that
1589 // there is register data following it.
1590 Status error = ReadZAHeader();
1591 if (error.Success() && (m_za_header.size > sizeof(m_za_header)))
1592 m_sme_pseudo_regs.ctrl_reg |= 2;
1593
1594 return error;
1595}
1596
1597Status NativeRegisterContextLinux_arm64::ReadMTEControl() {
1598 Status error;
1599
1600 if (m_mte_ctrl_is_valid)
1601 return error;
1602
1603 struct iovec ioVec;
1604 ioVec.iov_base = GetMTEControl();
1605 ioVec.iov_len = GetMTEControlSize();
1606
1607 error = ReadRegisterSet(&ioVec, GetMTEControlSize(), NT_ARM_TAGGED_ADDR_CTRL);
1608
1609 if (error.Success())
1610 m_mte_ctrl_is_valid = true;
1611
1612 return error;
1613}
1614
1615Status NativeRegisterContextLinux_arm64::WriteMTEControl() {
1616 Status error;
1617
1618 error = ReadMTEControl();
1619 if (error.Fail())
1620 return error;
1621
1622 struct iovec ioVec;
1623 ioVec.iov_base = GetMTEControl();
1624 ioVec.iov_len = GetMTEControlSize();
1625
1626 m_mte_ctrl_is_valid = false;
1627
1628 return WriteRegisterSet(&ioVec, GetMTEControlSize(), NT_ARM_TAGGED_ADDR_CTRL);
1629}
1630
1631Status NativeRegisterContextLinux_arm64::ReadTLS() {
1632 Status error;
1633
1634 if (m_tls_is_valid)
1635 return error;
1636
1637 struct iovec ioVec;
1638 ioVec.iov_base = GetTLSBuffer();
1639 ioVec.iov_len = GetTLSBufferSize();
1640
1641 error = ReadRegisterSet(&ioVec, GetTLSBufferSize(), NT_ARM_TLS);
1642
1643 if (error.Success())
1644 m_tls_is_valid = true;
1645
1646 return error;
1647}
1648
1649Status NativeRegisterContextLinux_arm64::WriteTLS() {
1650 Status error;
1651
1652 error = ReadTLS();
1653 if (error.Fail())
1654 return error;
1655
1656 struct iovec ioVec;
1657 ioVec.iov_base = GetTLSBuffer();
1658 ioVec.iov_len = GetTLSBufferSize();
1659
1660 m_tls_is_valid = false;
1661
1662 return WriteRegisterSet(&ioVec, GetTLSBufferSize(), NT_ARM_TLS);
1663}
1664
1665Status NativeRegisterContextLinux_arm64::ReadGCS() {
1666 Status error;
1667
1668 if (m_gcs_is_valid)
1669 return error;
1670
1671 struct iovec ioVec;
1672 ioVec.iov_base = GetGCSBuffer();
1673 ioVec.iov_len = GetGCSBufferSize();
1674
1675 error = ReadRegisterSet(&ioVec, GetGCSBufferSize(), NT_ARM_GCS);
1676
1677 if (error.Success())
1678 m_gcs_is_valid = true;
1679
1680 return error;
1681}
1682
1683Status NativeRegisterContextLinux_arm64::WriteGCS() {
1684 Status error;
1685
1686 error = ReadGCS();
1687 if (error.Fail())
1688 return error;
1689
1690 struct iovec ioVec;
1691 ioVec.iov_base = GetGCSBuffer();
1692 ioVec.iov_len = GetGCSBufferSize();
1693
1694 m_gcs_is_valid = false;
1695
1696 return WriteRegisterSet(&ioVec, GetGCSBufferSize(), NT_ARM_GCS);
1697}
1698
1699Status NativeRegisterContextLinux_arm64::ReadZAHeader() {
1700 Status error;
1701
1702 if (m_za_header_is_valid)
1703 return error;
1704
1705 struct iovec ioVec;
1706 ioVec.iov_base = GetZAHeader();
1707 ioVec.iov_len = GetZAHeaderSize();
1708
1709 error = ReadRegisterSet(&ioVec, GetZAHeaderSize(), NT_ARM_ZA);
1710
1711 if (error.Success())
1712 m_za_header_is_valid = true;
1713
1714 return error;
1715}
1716
1717Status NativeRegisterContextLinux_arm64::ReadZA() {
1718 Status error;
1719
1720 if (m_za_buffer_is_valid)
1721 return error;
1722
1723 struct iovec ioVec;
1724 ioVec.iov_base = GetZABuffer();
1725 ioVec.iov_len = GetZABufferSize();
1726
1727 error = ReadRegisterSet(&ioVec, GetZABufferSize(), NT_ARM_ZA);
1728
1729 if (error.Success())
1730 m_za_buffer_is_valid = true;
1731
1732 return error;
1733}
1734
1735Status NativeRegisterContextLinux_arm64::WriteZA() {
1736 // Note that because the ZA ptrace payload contains the header also, this
1737 // method will write both. This is done because writing only the header
1738 // will disable ZA, even if .size in the header is correct for an enabled ZA.
1739 Status error;
1740
1741 error = ReadZA();
1742 if (error.Fail())
1743 return error;
1744
1745 struct iovec ioVec;
1746 ioVec.iov_base = GetZABuffer();
1747 ioVec.iov_len = GetZABufferSize();
1748
1749 m_za_buffer_is_valid = false;
1750 m_za_header_is_valid = false;
1751 // Writing to ZA may enable ZA, which means ZT0 may change too.
1752 m_zt_buffer_is_valid = false;
1753
1754 return WriteRegisterSet(&ioVec, GetZABufferSize(), NT_ARM_ZA);
1755}
1756
1757Status NativeRegisterContextLinux_arm64::ReadZT() {
1758 Status error;
1759
1760 if (m_zt_buffer_is_valid)
1761 return error;
1762
1763 struct iovec ioVec;
1764 ioVec.iov_base = GetZTBuffer();
1765 ioVec.iov_len = GetZTBufferSize();
1766
1767 error = ReadRegisterSet(&ioVec, GetZTBufferSize(), NT_ARM_ZT);
1768 m_zt_buffer_is_valid = error.Success();
1769
1770 return error;
1771}
1772
1773Status NativeRegisterContextLinux_arm64::WriteZT() {
1774 Status error;
1775
1776 error = ReadZT();
1777 if (error.Fail())
1778 return error;
1779
1780 struct iovec ioVec;
1781 ioVec.iov_base = GetZTBuffer();
1782 ioVec.iov_len = GetZTBufferSize();
1783
1784 m_zt_buffer_is_valid = false;
1785 // Writing to an inactive ZT0 will enable ZA as well, which invalidates our
1786 // current copy of it.
1787 m_za_buffer_is_valid = false;
1788 m_za_header_is_valid = false;
1789
1790 return WriteRegisterSet(&ioVec, GetZTBufferSize(), NT_ARM_ZT);
1791}
1792
1793Status NativeRegisterContextLinux_arm64::ReadFPMR() {
1794 Status error;
1795
1796 if (m_fpmr_is_valid)
1797 return error;
1798
1799 struct iovec ioVec;
1800 ioVec.iov_base = GetFPMRBuffer();
1801 ioVec.iov_len = GetFPMRBufferSize();
1802
1803 error = ReadRegisterSet(&ioVec, GetFPMRBufferSize(), NT_ARM_FPMR);
1804
1805 if (error.Success())
1806 m_fpmr_is_valid = true;
1807
1808 return error;
1809}
1810
1811Status NativeRegisterContextLinux_arm64::WriteFPMR() {
1812 Status error;
1813
1814 error = ReadFPMR();
1815 if (error.Fail())
1816 return error;
1817
1818 struct iovec ioVec;
1819 ioVec.iov_base = GetFPMRBuffer();
1820 ioVec.iov_len = GetFPMRBufferSize();
1821
1822 m_fpmr_is_valid = false;
1823
1824 return WriteRegisterSet(&ioVec, GetFPMRBufferSize(), NT_ARM_FPMR);
1825}
1826
1827Status NativeRegisterContextLinux_arm64::ReadPOE() {
1828 Status error;
1829
1830 if (m_poe_is_valid)
1831 return error;
1832
1833 struct iovec ioVec;
1834 ioVec.iov_base = GetPOEBuffer();
1835 ioVec.iov_len = GetPOEBufferSize();
1836
1837 error = ReadRegisterSet(&ioVec, GetPOEBufferSize(), NT_ARM_POE);
1838
1839 if (error.Success())
1840 m_poe_is_valid = true;
1841
1842 return error;
1843}
1844
1845Status NativeRegisterContextLinux_arm64::WritePOE() {
1846 Status error;
1847
1848 error = ReadPOE();
1849 if (error.Fail())
1850 return error;
1851
1852 struct iovec ioVec;
1853 ioVec.iov_base = GetPOEBuffer();
1854 ioVec.iov_len = GetPOEBufferSize();
1855
1856 m_poe_is_valid = false;
1857
1858 return WriteRegisterSet(&ioVec, GetPOEBufferSize(), NT_ARM_POE);
1859}
1860
1861void NativeRegisterContextLinux_arm64::ConfigureRegisterContext() {
1862 // ConfigureRegisterContext gets called from InvalidateAllRegisters
1863 // on every stop and configures SVE vector length and whether we are in
1864 // streaming SVE mode.
1865 // If m_sve_state is set to SVEState::Disabled on first stop, code below will
1866 // be deemed non operational for the lifetime of current process.
1867 if (!m_sve_header_is_valid && m_sve_state != SVEState::Disabled) {
1868 // Systems may have SVE and/or SME. If they are SME only, the SVE regset
1869 // cannot be read from but the SME one can. If they have both SVE and SME,
1870 // only the active mode will return valid register data.
1871
1872 // Check for SME.
1873 m_sve_header_is_valid = false;
1874 m_sve_buffer_is_valid = false;
1875 m_sve_state = SVEState::Streaming;
1876 Status error = ReadSVEHeader();
1877
1878 bool has_sme = error.Success();
1879 bool sme_is_active =
1880 has_sme &&
1881 ((m_sve_header.flags & sve::ptrace_regs_mask) == sve::ptrace_regs_sve);
1882
1883 // Check for SVE.
1884 m_sve_header_is_valid = false;
1885 m_sve_buffer_is_valid = false;
1886 m_sve_state = SVEState::Full;
1887 error = ReadSVEHeader();
1888
1889 bool has_sve = error.Success();
1890 bool sve_is_active =
1891 has_sve &&
1892 ((m_sve_header.flags & sve::ptrace_regs_mask) == sve::ptrace_regs_sve);
1893 // We do not check this for streaming mode because the streaming mode regset
1894 // will never be in FP format.
1895 bool fp_is_active =
1896 has_sve && ((m_sve_header.flags & sve::ptrace_regs_mask) ==
1898
1899 if (sme_is_active)
1900 m_sve_state = SVEState::Streaming;
1901 else if (sve_is_active)
1902 m_sve_state = SVEState::Full;
1903 else if (fp_is_active)
1904 m_sve_state = SVEState::FPSIMD;
1905 else if (has_sme) {
1906 // We are in the non-streaming mode of an SME only system.
1907 m_sve_state = SVEState::StreamingFPSIMD;
1908 } else
1909 m_sve_state = SVEState::Disabled;
1910
1911 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::FPSIMD ||
1912 m_sve_state == SVEState::Streaming ||
1913 m_sve_state == SVEState::StreamingFPSIMD) {
1914
1915 m_sve_header_is_valid = false;
1916 m_sve_buffer_is_valid = false;
1917 error = ReadSVEHeader();
1918
1919 // On every stop we configure SVE vector length by calling
1920 // ConfigureVectorLengthSVE regardless of current SVEState of this thread.
1922 if (sve::vl_valid(m_sve_header.vl))
1923 vq = sve::vq_from_vl(m_sve_header.vl);
1924
1925 GetRegisterInfo().ConfigureVectorLengthSVE(vq);
1926 m_sve_ptrace_payload.resize(sve::PTraceSize(vq, sve::ptrace_regs_sve));
1927 }
1928 }
1929
1930 if (!m_za_header_is_valid) {
1931 Status error = ReadZAHeader();
1932 if (error.Success()) {
1934 if (sve::vl_valid(m_za_header.vl))
1935 vq = sve::vq_from_vl(m_za_header.vl);
1936
1937 GetRegisterInfo().ConfigureVectorLengthZA(vq);
1938 m_za_ptrace_payload.resize(m_za_header.size);
1939 m_za_buffer_is_valid = false;
1940 }
1941 }
1942}
1943
1944uint32_t NativeRegisterContextLinux_arm64::CalculateFprOffset(
1945 const RegisterInfo *reg_info, bool streaming_fpsimd) const {
1946 uint32_t offset = reg_info->byte_offset - GetGPRSize();
1947 if (!streaming_fpsimd)
1948 return offset;
1949
1950 // If we're outside of streaming mode on a streaming only target, the offsets
1951 // are relative to an SVE context. We need the offset into the actual FPR
1952 // context:
1953 // struct user_fpsimd_state {
1954 // __uint128_t vregs[32];
1955 // __u32 fpsr;
1956 // __u32 fpcr;
1957 // __u32 __reserved[2];
1958 // };
1959 const size_t fpsr_offset = 16 * 32;
1960 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
1961 if (reg == GetRegisterInfo().GetRegNumFPSR())
1962 offset = fpsr_offset;
1963 else if (reg == GetRegisterInfo().GetRegNumFPCR())
1964 offset = fpsr_offset + 4;
1965 else
1966 offset = 16 * (reg - GetRegisterInfo().GetRegNumFPV0());
1967
1968 return offset;
1969}
1970
1971uint32_t NativeRegisterContextLinux_arm64::CalculateSVEOffset(
1972 const RegisterInfo *reg_info) const {
1973 // Start of Z0 data is after GPRs plus 8 bytes of vg register
1974 uint32_t sve_reg_offset = LLDB_INVALID_INDEX32;
1975 if (m_sve_state == SVEState::FPSIMD) {
1976 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
1977 sve_reg_offset = sve::ptrace_fpsimd_offset +
1978 (reg - GetRegisterInfo().GetRegNumSVEZ0()) * 16;
1979 // Between non-streaming and streaming mode, the layout is identical.
1980 } else if (m_sve_state == SVEState::Full ||
1981 m_sve_state == SVEState::Streaming) {
1982 uint32_t sve_z0_offset = GetGPRSize() + 16;
1983 sve_reg_offset =
1984 sve::SigRegsOffset() + reg_info->byte_offset - sve_z0_offset;
1985 }
1986 return sve_reg_offset;
1987}
1988
1989Status NativeRegisterContextLinux_arm64::ReadSMESVG() {
1990 // This register is the streaming vector length, so we will get it from
1991 // NT_ARM_ZA regardless of the current streaming mode.
1992 Status error = ReadZAHeader();
1993 if (error.Success())
1994 m_sme_pseudo_regs.svg_reg = m_za_header.vl / 8;
1995
1996 return error;
1997}
1998
1999std::vector<uint32_t> NativeRegisterContextLinux_arm64::GetExpeditedRegisters(
2000 ExpeditedRegs expType) const {
2001 std::vector<uint32_t> expedited_reg_nums =
2003 // SVE, non-streaming vector length.
2004 if (m_sve_state == SVEState::FPSIMD || m_sve_state == SVEState::Full)
2005 expedited_reg_nums.push_back(GetRegisterInfo().GetRegNumSVEVG());
2006 // SME, streaming vector length. This is used by the ZA register which is
2007 // present even when streaming mode is not enabled.
2008 if (GetRegisterInfo().IsSSVEPresent())
2009 expedited_reg_nums.push_back(GetRegisterInfo().GetRegNumSMESVG());
2010
2011 return expedited_reg_nums;
2012}
2013
2014llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails>
2015NativeRegisterContextLinux_arm64::GetMemoryTaggingDetails(int32_t type) {
2017 return MemoryTaggingDetails{std::make_unique<MemoryTagManagerAArch64MTE>(),
2019 }
2020
2021 return llvm::createStringError(llvm::inconvertibleErrorCode(),
2022 "Unknown AArch64 memory tag type %d", type);
2023}
2024
2025lldb::addr_t NativeRegisterContextLinux_arm64::FixWatchpointHitAddress(
2026 lldb::addr_t hit_addr) {
2027 // Linux configures user-space virtual addresses with top byte ignored.
2028 // We set default value of mask such that top byte is masked out.
2029 lldb::addr_t mask = ~((1ULL << 56) - 1);
2030
2031 // Try to read pointer authentication data_mask register and calculate a
2032 // consolidated data address mask after ignoring the top byte.
2033 if (ReadPAuthMask().Success())
2034 mask |= m_pac_mask.data_mask;
2035
2036 return hit_addr & ~mask;
2037 ;
2038}
2039
2040#endif // defined (__arm64__) || defined (__aarch64__)
#define GPR(r16)
Definition ABIX86.cpp:145
static llvm::raw_ostream & error(Stream &strm)
#define HWCAP2_MTE
#define PTRACE_PEEKMTETAGS
Definition Ptrace.h:61
#define PTRACE_POKEMTETAGS
Definition Ptrace.h:64
#define PTRACE_GETREGSET
Definition Ptrace.h:36
struct _FPR FPR
#define HWCAP2_FPMR
@ AUXV_AT_HWCAP2
Extension of AT_HWCAP.
Definition AuxVector.h:59
@ AUXV_AT_HWCAP3
Extension of AT_HWCAP.
Definition AuxVector.h:60
@ AUXV_AT_HWCAP
Machine dependent hints about processor capabilities.
Definition AuxVector.h:49
size_t GetRegisterSetCount() const override
This class manages the storage and detection of register field information.
void DetectFields(uint64_t hwcap, uint64_t hwcap2, uint64_t hwcap3)
For the registers listed in this class, detect which fields are present.
void UpdateRegisterInfo(const RegisterInfo *reg_info, uint32_t num_regs)
Add the field information of any registers named in this class, to the relevant RegisterInfo instance...
bool HasDetected() const
Returns true if field detection has been run at least once.
A subclass of DataBuffer that stores a data buffer on the heap.
std::optional< uint64_t > GetAuxValue(enum AuxVector::EntryType type)
virtual std::vector< uint32_t > GetExpeditedRegisters(ExpeditedRegs expType) const
uint32_t SetFromMemoryData(const RegisterInfo &reg_info, const void *src, uint32_t src_len, lldb::ByteOrder src_byte_order, Status &error)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
const void * GetBytes() const
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
Manages communication with the inferior (debugee) process.
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, void *data=nullptr, size_t data_size=0, long *result=nullptr)
}
static std::unique_ptr< NativeRegisterContextLinux > CreateHostNativeRegisterContextLinux(const ArchSpec &target_arch, NativeThreadLinux &native_thread)
static llvm::Expected< ArchSpec > DetermineArchitecture(lldb::tid_t tid)
#define LLDB_INVALID_INDEX32
#define LLDB_INVALID_REGNUM
Status WriteHardwareDebugRegs(int hwbType, ::pid_t tid, uint32_t max_supported, const std::array< NativeRegisterContextDBReg::DREG, 16 > &regs)
Status ReadHardwareDebugInfo(::pid_t tid, uint32_t &max_hwp_supported, uint32_t &max_hbp_supported)
uint16_t vq_from_vl(uint16_t vl)
uint32_t PTraceFPSROffset(uint16_t vq)
uint32_t PTraceFPCROffset(uint16_t vq)
uint16_t vl_valid(uint16_t vl)
uint32_t PTraceSize(uint16_t vq, uint16_t flags)
A class that represents a running process on the host machine.
uint64_t pid_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
uint64_t tid_t
Definition lldb-types.h:84
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
uint32_t * value_regs
List of registers (terminated with LLDB_INVALID_REGNUM).
uint32_t byte_offset
The byte offset in the register context data where this register's value is found.
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.
Registers are grouped into register sets.
size_t num_registers
The number of registers in REGISTERS array below.