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 if (!GetRegisterInfo().IsSVEPresent() &&
1167 GetRegisterInfo().IsSSVEPresent()) {
1168 // On an SME only system, if we get here then we were outside of
1169 // streaming mode when the registers were saved. We may be in streaming
1170 // mode at the current moment, so we need to to exit it. The kernel
1171 // allows us to do this by writing FPSIMD format data to the
1172 // non-streaming SVE register set, with a vector length of 0 set. This
1173 // is only done in this specific situation.
1174
1175 size_t data_size = sve::ptrace_fpsimd_offset + GetFPRSize();
1176 // NT_ARM_SVE data must be a multiple of 128 bits, and the FPU data size
1177 // is not, round up.
1178 data_size =
1179 (data_size + sve::vq_bytes - 1) / sve::vq_bytes * sve::vq_bytes;
1180 std::vector<uint8_t> sve_fpsimd_data(data_size);
1181
1182 sve::user_sve_header *header =
1183 reinterpret_cast<sve::user_sve_header *>(sve_fpsimd_data.data());
1184 std::memset(header, 0, sizeof(sve::user_sve_header));
1185 header->size = sve_fpsimd_data.size();
1186 // VL = 0 tells the process to exit streaming mode.
1187 header->vl = 0;
1189 std::memcpy(&sve_fpsimd_data[sve::ptrace_fpsimd_offset], src,
1190 GetFPRSize());
1191
1192 struct iovec ioVec;
1193 ioVec.iov_base = sve_fpsimd_data.data();
1194 ioVec.iov_len = sve_fpsimd_data.size();
1195
1196 // Even though the system does not have SVE, NT_ARM_SVE is used when
1197 // exiting streaming mode.
1198 error = WriteRegisterSet(&ioVec, sve_fpsimd_data.size(), NT_ARM_SVE);
1199
1200 // Wrote FPU, and SVE overlaps FPU.
1201 m_fpu_is_valid = false;
1202 m_sve_buffer_is_valid = false;
1203 m_sve_header_is_valid = false;
1204
1205 m_sve_state = SVEState::Unknown;
1206 ConfigureRegisterContext();
1207
1208 // Consume FP register set.
1209 src += GetFPRSize();
1210 } else {
1211 error = RestoreRegisters(
1212 GetFPRBuffer(), &src, GetFPRSize(), m_fpu_is_valid,
1213 std::bind(&NativeRegisterContextLinux_arm64::WriteFPR, this));
1214 }
1215 break;
1216 }
1217 case RegisterSetType::MTE:
1218 error = RestoreRegisters(
1219 GetMTEControl(), &src, GetMTEControlSize(), m_mte_ctrl_is_valid,
1220 std::bind(&NativeRegisterContextLinux_arm64::WriteMTEControl, this));
1221 break;
1222 case RegisterSetType::TLS:
1223 error = RestoreRegisters(
1224 GetTLSBuffer(), &src, GetTLSBufferSize(), m_tls_is_valid,
1225 std::bind(&NativeRegisterContextLinux_arm64::WriteTLS, this));
1226 break;
1227 case RegisterSetType::SME:
1228 // To enable or disable ZA you write the regset with or without register
1229 // data. The kernel detects this by looking at the ioVec's length, not the
1230 // ZA header size you pass in. Therefore we must write header and register
1231 // data (if present) in one go every time. Read the header only first just
1232 // to get the size.
1233 ::memcpy(GetZAHeader(), src, GetZAHeaderSize());
1234 // Read the header and register data. Can't use the buffer size here, it
1235 // may be incorrect due to being filled with dummy data previously. Resize
1236 // this so WriteZA uses the correct size.
1237 m_za_ptrace_payload.resize(m_za_header.size);
1238 ::memcpy(GetZABuffer(), src, GetZABufferSize());
1239 m_za_buffer_is_valid = true;
1240
1241 error = WriteZA();
1242 if (error.Fail())
1243 return error;
1244
1245 // Update size of ZA, which resizes the ptrace payload potentially
1246 // trashing our copy of the data we just wrote.
1247 ConfigureRegisterContext();
1248
1249 // ZA buffer now has proper size, read back the data we wrote above, from
1250 // ptrace.
1251 error = ReadZA();
1252 src += GetZABufferSize();
1253 break;
1254 case RegisterSetType::SME2:
1255 // Doing this would activate an inactive ZA, however we will only get here
1256 // if the state we are restoring had an active ZA. Restoring ZT0 will
1257 // always come after restoring ZA.
1258 error = RestoreRegisters(
1259 GetZTBuffer(), &src, GetZTBufferSize(), m_zt_buffer_is_valid,
1260 std::bind(&NativeRegisterContextLinux_arm64::WriteZT, this));
1261 break;
1262 case RegisterSetType::FPMR:
1263 error = RestoreRegisters(
1264 GetFPMRBuffer(), &src, GetFPMRBufferSize(), m_fpmr_is_valid,
1265 std::bind(&NativeRegisterContextLinux_arm64::WriteFPMR, this));
1266 break;
1267 case RegisterSetType::GCS: {
1268 // It is not permitted to enable GCS via ptrace. We can disable it, but
1269 // to keep things simple we will not revert any change to the
1270 // PR_SHADOW_STACK_ENABLE bit. Instead patch in the current enable bit
1271 // into the registers we are about to restore.
1272 m_gcs_is_valid = false;
1273 error = ReadGCS();
1274 if (error.Fail())
1275 return error;
1276
1277 uint64_t enable_bit = m_gcs_regs.features_enabled & 1UL;
1278 gcs_regs new_gcs_regs = *reinterpret_cast<const gcs_regs *>(src);
1279 new_gcs_regs.features_enabled =
1280 (new_gcs_regs.features_enabled & ~1UL) | enable_bit;
1281
1282 const uint8_t *new_gcs_src =
1283 reinterpret_cast<const uint8_t *>(&new_gcs_regs);
1284 error = RestoreRegisters(
1285 GetGCSBuffer(), &new_gcs_src, GetGCSBufferSize(), m_gcs_is_valid,
1286 std::bind(&NativeRegisterContextLinux_arm64::WriteGCS, this));
1287 src += GetGCSBufferSize();
1288
1289 break;
1290 }
1291 case RegisterSetType::POE:
1292 error = RestoreRegisters(
1293 GetPOEBuffer(), &src, GetPOEBufferSize(), m_poe_is_valid,
1294 std::bind(&NativeRegisterContextLinux_arm64::WritePOE, this));
1295 break;
1296 }
1297
1298 if (error.Fail())
1299 return error;
1300 }
1301
1302 return error;
1303}
1304
1305bool NativeRegisterContextLinux_arm64::IsGPR(unsigned reg) const {
1306 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
1308 return true;
1309 return false;
1310}
1311
1312bool NativeRegisterContextLinux_arm64::IsFPR(unsigned reg) const {
1313 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
1315 return true;
1316 return false;
1317}
1318
1319bool NativeRegisterContextLinux_arm64::IsSVE(unsigned reg) const {
1320 return GetRegisterInfo().IsSVEReg(reg);
1321}
1322
1323bool NativeRegisterContextLinux_arm64::IsSME(unsigned reg) const {
1324 return GetRegisterInfo().IsSMEReg(reg);
1325}
1326
1327bool NativeRegisterContextLinux_arm64::IsPAuth(unsigned reg) const {
1328 return GetRegisterInfo().IsPAuthReg(reg);
1329}
1330
1331bool NativeRegisterContextLinux_arm64::IsMTE(unsigned reg) const {
1332 return GetRegisterInfo().IsMTEReg(reg);
1333}
1334
1335bool NativeRegisterContextLinux_arm64::IsTLS(unsigned reg) const {
1336 return GetRegisterInfo().IsTLSReg(reg);
1337}
1338
1339bool NativeRegisterContextLinux_arm64::IsFPMR(unsigned reg) const {
1340 return GetRegisterInfo().IsFPMRReg(reg);
1341}
1342
1343bool NativeRegisterContextLinux_arm64::IsGCS(unsigned reg) const {
1344 return GetRegisterInfo().IsGCSReg(reg);
1345}
1346
1347bool NativeRegisterContextLinux_arm64::IsPOE(unsigned reg) const {
1348 return GetRegisterInfo().IsPOEReg(reg);
1349}
1350
1351llvm::Error NativeRegisterContextLinux_arm64::ReadHardwareDebugInfo() {
1352 if (!m_refresh_hwdebug_info) {
1353 return llvm::Error::success();
1354 }
1355
1356 ::pid_t tid = m_thread.GetID();
1357
1358 Status error = arm64::ReadHardwareDebugInfo(tid, m_max_hwp_supported,
1359 m_max_hbp_supported);
1360 if (error.Fail())
1361 return error.ToError();
1362
1363 m_refresh_hwdebug_info = false;
1364
1365 return llvm::Error::success();
1366}
1367
1368llvm::Error
1369NativeRegisterContextLinux_arm64::WriteHardwareDebugRegs(DREGType hwbType) {
1370 uint32_t max_supported =
1371 (hwbType == eDREGTypeWATCH) ? m_max_hwp_supported : m_max_hbp_supported;
1372 auto &regs = (hwbType == eDREGTypeWATCH) ? m_hwp_regs : m_hbp_regs;
1373 return arm64::WriteHardwareDebugRegs(hwbType, m_thread.GetID(), max_supported,
1374 regs)
1375 .ToError();
1376}
1377
1378Status NativeRegisterContextLinux_arm64::ReadGPR() {
1379 Status error;
1380
1381 if (m_gpr_is_valid)
1382 return error;
1383
1384 struct iovec ioVec;
1385 ioVec.iov_base = GetGPRBuffer();
1386 ioVec.iov_len = GetGPRBufferSize();
1387
1388 error = ReadRegisterSet(&ioVec, GetGPRBufferSize(), NT_PRSTATUS);
1389
1390 if (error.Success())
1391 m_gpr_is_valid = true;
1392
1393 return error;
1394}
1395
1396Status NativeRegisterContextLinux_arm64::WriteGPR() {
1397 Status error = ReadGPR();
1398 if (error.Fail())
1399 return error;
1400
1401 struct iovec ioVec;
1402 ioVec.iov_base = GetGPRBuffer();
1403 ioVec.iov_len = GetGPRBufferSize();
1404
1405 m_gpr_is_valid = false;
1406
1407 return WriteRegisterSet(&ioVec, GetGPRBufferSize(), NT_PRSTATUS);
1408}
1409
1410Status NativeRegisterContextLinux_arm64::ReadFPR() {
1411 Status error;
1412
1413 if (m_fpu_is_valid)
1414 return error;
1415
1416 struct iovec ioVec;
1417 ioVec.iov_base = GetFPRBuffer();
1418 ioVec.iov_len = GetFPRSize();
1419
1420 error = ReadRegisterSet(&ioVec, GetFPRSize(), NT_FPREGSET);
1421 if (error.Success())
1422 m_fpu_is_valid = true;
1423
1424 return error;
1425}
1426
1427Status NativeRegisterContextLinux_arm64::WriteFPR() {
1428 Status error = ReadFPR();
1429 if (error.Fail())
1430 return error;
1431
1432 struct iovec ioVec;
1433 ioVec.iov_base = GetFPRBuffer();
1434 ioVec.iov_len = GetFPRSize();
1435
1436 m_fpu_is_valid = false;
1437 // SVE Z registers overlap the FP registers.
1438 m_sve_buffer_is_valid = false;
1439 m_sve_header_is_valid = false;
1440
1441 return WriteRegisterSet(&ioVec, GetFPRSize(), NT_FPREGSET);
1442}
1443
1444void NativeRegisterContextLinux_arm64::InvalidateAllRegisters() {
1445 m_gpr_is_valid = false;
1446 m_fpu_is_valid = false;
1447 m_sve_buffer_is_valid = false;
1448 m_sve_header_is_valid = false;
1449 m_za_buffer_is_valid = false;
1450 m_za_header_is_valid = false;
1451 m_pac_mask_is_valid = false;
1452 m_mte_ctrl_is_valid = false;
1453 m_tls_is_valid = false;
1454 m_zt_buffer_is_valid = false;
1455 m_fpmr_is_valid = false;
1456 m_gcs_is_valid = false;
1457 m_poe_is_valid = false;
1458
1459 // Update SVE and ZA registers in case there is change in configuration.
1460 ConfigureRegisterContext();
1461}
1462
1463unsigned NativeRegisterContextLinux_arm64::GetSVERegSet() {
1464 switch (m_sve_state) {
1467 return NT_ARM_SSVE;
1468 default:
1469 return NT_ARM_SVE;
1470 }
1471}
1472
1473Status NativeRegisterContextLinux_arm64::ReadSVEHeader() {
1474 Status error;
1475
1476 if (m_sve_header_is_valid)
1477 return error;
1478
1479 struct iovec ioVec;
1480 ioVec.iov_base = GetSVEHeader();
1481 ioVec.iov_len = GetSVEHeaderSize();
1482
1483 error = ReadRegisterSet(&ioVec, GetSVEHeaderSize(), GetSVERegSet());
1484
1485 if (error.Success())
1486 m_sve_header_is_valid = true;
1487
1488 return error;
1489}
1490
1491Status NativeRegisterContextLinux_arm64::ReadPAuthMask() {
1492 Status error;
1493
1494 if (m_pac_mask_is_valid)
1495 return error;
1496
1497 struct iovec ioVec;
1498 ioVec.iov_base = GetPACMask();
1499 ioVec.iov_len = GetPACMaskSize();
1500
1501 error = ReadRegisterSet(&ioVec, GetPACMaskSize(), NT_ARM_PAC_MASK);
1502
1503 if (error.Success())
1504 m_pac_mask_is_valid = true;
1505
1506 return error;
1507}
1508
1509Status NativeRegisterContextLinux_arm64::WriteSVEHeader() {
1510 Status error;
1511
1512 error = ReadSVEHeader();
1513 if (error.Fail())
1514 return error;
1515
1516 struct iovec ioVec;
1517 ioVec.iov_base = GetSVEHeader();
1518 ioVec.iov_len = GetSVEHeaderSize();
1519
1520 m_sve_buffer_is_valid = false;
1521 m_sve_header_is_valid = false;
1522 m_fpu_is_valid = false;
1523
1524 return WriteRegisterSet(&ioVec, GetSVEHeaderSize(), GetSVERegSet());
1525}
1526
1527Status NativeRegisterContextLinux_arm64::ReadAllSVE() {
1528 Status error;
1529 if (m_sve_buffer_is_valid)
1530 return error;
1531
1532 struct iovec ioVec;
1533 ioVec.iov_base = GetSVEBuffer();
1534 ioVec.iov_len = GetSVEBufferSize();
1535
1536 error = ReadRegisterSet(&ioVec, GetSVEBufferSize(), GetSVERegSet());
1537
1538 if (error.Success())
1539 m_sve_buffer_is_valid = true;
1540
1541 return error;
1542}
1543
1544Status NativeRegisterContextLinux_arm64::WriteAllSVE() {
1545 Status error;
1546
1547 error = ReadAllSVE();
1548 if (error.Fail())
1549 return error;
1550
1551 struct iovec ioVec;
1552
1553 ioVec.iov_base = GetSVEBuffer();
1554 ioVec.iov_len = GetSVEBufferSize();
1555
1556 m_sve_buffer_is_valid = false;
1557 m_sve_header_is_valid = false;
1558 m_fpu_is_valid = false;
1559
1560 return WriteRegisterSet(&ioVec, GetSVEBufferSize(), GetSVERegSet());
1561}
1562
1563Status NativeRegisterContextLinux_arm64::ReadSMEControl() {
1564 // The real register is SVCR and is accessible from EL0. However we don't want
1565 // to have to JIT code into the target process so we'll just recreate it using
1566 // what we know from ptrace.
1567
1568 // Bit 0 indicates whether streaming mode is active.
1569 m_sme_pseudo_regs.ctrl_reg = m_sve_state == SVEState::Streaming;
1570
1571 // Bit 1 indicates whether the array storage is active.
1572 // It is active if we can read the header and the size field tells us that
1573 // there is register data following it.
1574 Status error = ReadZAHeader();
1575 if (error.Success() && (m_za_header.size > sizeof(m_za_header)))
1576 m_sme_pseudo_regs.ctrl_reg |= 2;
1577
1578 return error;
1579}
1580
1581Status NativeRegisterContextLinux_arm64::ReadMTEControl() {
1582 Status error;
1583
1584 if (m_mte_ctrl_is_valid)
1585 return error;
1586
1587 struct iovec ioVec;
1588 ioVec.iov_base = GetMTEControl();
1589 ioVec.iov_len = GetMTEControlSize();
1590
1591 error = ReadRegisterSet(&ioVec, GetMTEControlSize(), NT_ARM_TAGGED_ADDR_CTRL);
1592
1593 if (error.Success())
1594 m_mte_ctrl_is_valid = true;
1595
1596 return error;
1597}
1598
1599Status NativeRegisterContextLinux_arm64::WriteMTEControl() {
1600 Status error;
1601
1602 error = ReadMTEControl();
1603 if (error.Fail())
1604 return error;
1605
1606 struct iovec ioVec;
1607 ioVec.iov_base = GetMTEControl();
1608 ioVec.iov_len = GetMTEControlSize();
1609
1610 m_mte_ctrl_is_valid = false;
1611
1612 return WriteRegisterSet(&ioVec, GetMTEControlSize(), NT_ARM_TAGGED_ADDR_CTRL);
1613}
1614
1615Status NativeRegisterContextLinux_arm64::ReadTLS() {
1616 Status error;
1617
1618 if (m_tls_is_valid)
1619 return error;
1620
1621 struct iovec ioVec;
1622 ioVec.iov_base = GetTLSBuffer();
1623 ioVec.iov_len = GetTLSBufferSize();
1624
1625 error = ReadRegisterSet(&ioVec, GetTLSBufferSize(), NT_ARM_TLS);
1626
1627 if (error.Success())
1628 m_tls_is_valid = true;
1629
1630 return error;
1631}
1632
1633Status NativeRegisterContextLinux_arm64::WriteTLS() {
1634 Status error;
1635
1636 error = ReadTLS();
1637 if (error.Fail())
1638 return error;
1639
1640 struct iovec ioVec;
1641 ioVec.iov_base = GetTLSBuffer();
1642 ioVec.iov_len = GetTLSBufferSize();
1643
1644 m_tls_is_valid = false;
1645
1646 return WriteRegisterSet(&ioVec, GetTLSBufferSize(), NT_ARM_TLS);
1647}
1648
1649Status NativeRegisterContextLinux_arm64::ReadGCS() {
1650 Status error;
1651
1652 if (m_gcs_is_valid)
1653 return error;
1654
1655 struct iovec ioVec;
1656 ioVec.iov_base = GetGCSBuffer();
1657 ioVec.iov_len = GetGCSBufferSize();
1658
1659 error = ReadRegisterSet(&ioVec, GetGCSBufferSize(), NT_ARM_GCS);
1660
1661 if (error.Success())
1662 m_gcs_is_valid = true;
1663
1664 return error;
1665}
1666
1667Status NativeRegisterContextLinux_arm64::WriteGCS() {
1668 Status error;
1669
1670 error = ReadGCS();
1671 if (error.Fail())
1672 return error;
1673
1674 struct iovec ioVec;
1675 ioVec.iov_base = GetGCSBuffer();
1676 ioVec.iov_len = GetGCSBufferSize();
1677
1678 m_gcs_is_valid = false;
1679
1680 return WriteRegisterSet(&ioVec, GetGCSBufferSize(), NT_ARM_GCS);
1681}
1682
1683Status NativeRegisterContextLinux_arm64::ReadZAHeader() {
1684 Status error;
1685
1686 if (m_za_header_is_valid)
1687 return error;
1688
1689 struct iovec ioVec;
1690 ioVec.iov_base = GetZAHeader();
1691 ioVec.iov_len = GetZAHeaderSize();
1692
1693 error = ReadRegisterSet(&ioVec, GetZAHeaderSize(), NT_ARM_ZA);
1694
1695 if (error.Success())
1696 m_za_header_is_valid = true;
1697
1698 return error;
1699}
1700
1701Status NativeRegisterContextLinux_arm64::ReadZA() {
1702 Status error;
1703
1704 if (m_za_buffer_is_valid)
1705 return error;
1706
1707 struct iovec ioVec;
1708 ioVec.iov_base = GetZABuffer();
1709 ioVec.iov_len = GetZABufferSize();
1710
1711 error = ReadRegisterSet(&ioVec, GetZABufferSize(), NT_ARM_ZA);
1712
1713 if (error.Success())
1714 m_za_buffer_is_valid = true;
1715
1716 return error;
1717}
1718
1719Status NativeRegisterContextLinux_arm64::WriteZA() {
1720 // Note that because the ZA ptrace payload contains the header also, this
1721 // method will write both. This is done because writing only the header
1722 // will disable ZA, even if .size in the header is correct for an enabled ZA.
1723 Status error;
1724
1725 error = ReadZA();
1726 if (error.Fail())
1727 return error;
1728
1729 struct iovec ioVec;
1730 ioVec.iov_base = GetZABuffer();
1731 ioVec.iov_len = GetZABufferSize();
1732
1733 m_za_buffer_is_valid = false;
1734 m_za_header_is_valid = false;
1735 // Writing to ZA may enable ZA, which means ZT0 may change too.
1736 m_zt_buffer_is_valid = false;
1737
1738 return WriteRegisterSet(&ioVec, GetZABufferSize(), NT_ARM_ZA);
1739}
1740
1741Status NativeRegisterContextLinux_arm64::ReadZT() {
1742 Status error;
1743
1744 if (m_zt_buffer_is_valid)
1745 return error;
1746
1747 struct iovec ioVec;
1748 ioVec.iov_base = GetZTBuffer();
1749 ioVec.iov_len = GetZTBufferSize();
1750
1751 error = ReadRegisterSet(&ioVec, GetZTBufferSize(), NT_ARM_ZT);
1752 m_zt_buffer_is_valid = error.Success();
1753
1754 return error;
1755}
1756
1757Status NativeRegisterContextLinux_arm64::WriteZT() {
1758 Status error;
1759
1760 error = ReadZT();
1761 if (error.Fail())
1762 return error;
1763
1764 struct iovec ioVec;
1765 ioVec.iov_base = GetZTBuffer();
1766 ioVec.iov_len = GetZTBufferSize();
1767
1768 m_zt_buffer_is_valid = false;
1769 // Writing to an inactive ZT0 will enable ZA as well, which invalidates our
1770 // current copy of it.
1771 m_za_buffer_is_valid = false;
1772 m_za_header_is_valid = false;
1773
1774 return WriteRegisterSet(&ioVec, GetZTBufferSize(), NT_ARM_ZT);
1775}
1776
1777Status NativeRegisterContextLinux_arm64::ReadFPMR() {
1778 Status error;
1779
1780 if (m_fpmr_is_valid)
1781 return error;
1782
1783 struct iovec ioVec;
1784 ioVec.iov_base = GetFPMRBuffer();
1785 ioVec.iov_len = GetFPMRBufferSize();
1786
1787 error = ReadRegisterSet(&ioVec, GetFPMRBufferSize(), NT_ARM_FPMR);
1788
1789 if (error.Success())
1790 m_fpmr_is_valid = true;
1791
1792 return error;
1793}
1794
1795Status NativeRegisterContextLinux_arm64::WriteFPMR() {
1796 Status error;
1797
1798 error = ReadFPMR();
1799 if (error.Fail())
1800 return error;
1801
1802 struct iovec ioVec;
1803 ioVec.iov_base = GetFPMRBuffer();
1804 ioVec.iov_len = GetFPMRBufferSize();
1805
1806 m_fpmr_is_valid = false;
1807
1808 return WriteRegisterSet(&ioVec, GetFPMRBufferSize(), NT_ARM_FPMR);
1809}
1810
1811Status NativeRegisterContextLinux_arm64::ReadPOE() {
1812 Status error;
1813
1814 if (m_poe_is_valid)
1815 return error;
1816
1817 struct iovec ioVec;
1818 ioVec.iov_base = GetPOEBuffer();
1819 ioVec.iov_len = GetPOEBufferSize();
1820
1821 error = ReadRegisterSet(&ioVec, GetPOEBufferSize(), NT_ARM_POE);
1822
1823 if (error.Success())
1824 m_poe_is_valid = true;
1825
1826 return error;
1827}
1828
1829Status NativeRegisterContextLinux_arm64::WritePOE() {
1830 Status error;
1831
1832 error = ReadPOE();
1833 if (error.Fail())
1834 return error;
1835
1836 struct iovec ioVec;
1837 ioVec.iov_base = GetPOEBuffer();
1838 ioVec.iov_len = GetPOEBufferSize();
1839
1840 m_poe_is_valid = false;
1841
1842 return WriteRegisterSet(&ioVec, GetPOEBufferSize(), NT_ARM_POE);
1843}
1844
1845void NativeRegisterContextLinux_arm64::ConfigureRegisterContext() {
1846 // ConfigureRegisterContext gets called from InvalidateAllRegisters
1847 // on every stop and configures SVE vector length and whether we are in
1848 // streaming SVE mode.
1849 // If m_sve_state is set to SVEState::Disabled on first stop, code below will
1850 // be deemed non operational for the lifetime of current process.
1851 if (!m_sve_header_is_valid && m_sve_state != SVEState::Disabled) {
1852 // Systems may have SVE and/or SME. If they are SME only, the SVE regset
1853 // cannot be read from but the SME one can. If they have both SVE and SME,
1854 // only the active mode will return valid register data.
1855
1856 // Check for SME.
1857 m_sve_header_is_valid = false;
1858 m_sve_buffer_is_valid = false;
1859 m_sve_state = SVEState::Streaming;
1860 Status error = ReadSVEHeader();
1861
1862 bool has_sme = error.Success();
1863 bool sme_is_active =
1864 has_sme &&
1865 ((m_sve_header.flags & sve::ptrace_regs_mask) == sve::ptrace_regs_sve);
1866
1867 // Check for SVE.
1868 m_sve_header_is_valid = false;
1869 m_sve_buffer_is_valid = false;
1870 m_sve_state = SVEState::Full;
1871 error = ReadSVEHeader();
1872
1873 bool has_sve = error.Success();
1874 bool sve_is_active =
1875 has_sve &&
1876 ((m_sve_header.flags & sve::ptrace_regs_mask) == sve::ptrace_regs_sve);
1877 // We do not check this for streaming mode because the streaming mode regset
1878 // will never be in FP format.
1879 bool fp_is_active =
1880 has_sve && ((m_sve_header.flags & sve::ptrace_regs_mask) ==
1882
1883 if (sme_is_active)
1884 m_sve_state = SVEState::Streaming;
1885 else if (sve_is_active)
1886 m_sve_state = SVEState::Full;
1887 else if (fp_is_active)
1888 m_sve_state = SVEState::FPSIMD;
1889 else if (has_sme) {
1890 // We are in the non-streaming mode of an SME only system.
1891 m_sve_state = SVEState::StreamingFPSIMD;
1892 } else
1893 m_sve_state = SVEState::Disabled;
1894
1895 if (m_sve_state == SVEState::Full || m_sve_state == SVEState::FPSIMD ||
1896 m_sve_state == SVEState::Streaming ||
1897 m_sve_state == SVEState::StreamingFPSIMD) {
1898
1899 m_sve_header_is_valid = false;
1900 m_sve_buffer_is_valid = false;
1901 error = ReadSVEHeader();
1902
1903 // On every stop we configure SVE vector length by calling
1904 // ConfigureVectorLengthSVE regardless of current SVEState of this thread.
1906 if (sve::vl_valid(m_sve_header.vl))
1907 vq = sve::vq_from_vl(m_sve_header.vl);
1908
1909 GetRegisterInfo().ConfigureVectorLengthSVE(vq);
1910 m_sve_ptrace_payload.resize(sve::PTraceSize(vq, sve::ptrace_regs_sve));
1911 }
1912 }
1913
1914 if (!m_za_header_is_valid) {
1915 Status error = ReadZAHeader();
1916 if (error.Success()) {
1918 if (sve::vl_valid(m_za_header.vl))
1919 vq = sve::vq_from_vl(m_za_header.vl);
1920
1921 GetRegisterInfo().ConfigureVectorLengthZA(vq);
1922 m_za_ptrace_payload.resize(m_za_header.size);
1923 m_za_buffer_is_valid = false;
1924 }
1925 }
1926}
1927
1928uint32_t NativeRegisterContextLinux_arm64::CalculateFprOffset(
1929 const RegisterInfo *reg_info, bool streaming_fpsimd) const {
1930 uint32_t offset = reg_info->byte_offset - GetGPRSize();
1931 if (!streaming_fpsimd)
1932 return offset;
1933
1934 // If we're outside of streaming mode on a streaming only target, the offsets
1935 // are relative to an SVE context. We need the offset into the actual FPR
1936 // context:
1937 // struct user_fpsimd_state {
1938 // __uint128_t vregs[32];
1939 // __u32 fpsr;
1940 // __u32 fpcr;
1941 // __u32 __reserved[2];
1942 // };
1943 const size_t fpsr_offset = 16 * 32;
1944 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
1945 if (reg == GetRegisterInfo().GetRegNumFPSR())
1946 offset = fpsr_offset;
1947 else if (reg == GetRegisterInfo().GetRegNumFPCR())
1948 offset = fpsr_offset + 4;
1949 else
1950 offset = 16 * (reg - GetRegisterInfo().GetRegNumFPV0());
1951
1952 return offset;
1953}
1954
1955uint32_t NativeRegisterContextLinux_arm64::CalculateSVEOffset(
1956 const RegisterInfo *reg_info) const {
1957 // Start of Z0 data is after GPRs plus 8 bytes of vg register
1958 uint32_t sve_reg_offset = LLDB_INVALID_INDEX32;
1959 if (m_sve_state == SVEState::FPSIMD) {
1960 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
1961 sve_reg_offset = sve::ptrace_fpsimd_offset +
1962 (reg - GetRegisterInfo().GetRegNumSVEZ0()) * 16;
1963 // Between non-streaming and streaming mode, the layout is identical.
1964 } else if (m_sve_state == SVEState::Full ||
1965 m_sve_state == SVEState::Streaming) {
1966 uint32_t sve_z0_offset = GetGPRSize() + 16;
1967 sve_reg_offset =
1968 sve::SigRegsOffset() + reg_info->byte_offset - sve_z0_offset;
1969 }
1970 return sve_reg_offset;
1971}
1972
1973Status NativeRegisterContextLinux_arm64::ReadSMESVG() {
1974 // This register is the streaming vector length, so we will get it from
1975 // NT_ARM_ZA regardless of the current streaming mode.
1976 Status error = ReadZAHeader();
1977 if (error.Success())
1978 m_sme_pseudo_regs.svg_reg = m_za_header.vl / 8;
1979
1980 return error;
1981}
1982
1983std::vector<uint32_t> NativeRegisterContextLinux_arm64::GetExpeditedRegisters(
1984 ExpeditedRegs expType) const {
1985 std::vector<uint32_t> expedited_reg_nums =
1987 // SVE, non-streaming vector length.
1988 if (m_sve_state == SVEState::FPSIMD || m_sve_state == SVEState::Full)
1989 expedited_reg_nums.push_back(GetRegisterInfo().GetRegNumSVEVG());
1990 // SME, streaming vector length. This is used by the ZA register which is
1991 // present even when streaming mode is not enabled.
1992 if (GetRegisterInfo().IsSSVEPresent())
1993 expedited_reg_nums.push_back(GetRegisterInfo().GetRegNumSMESVG());
1994
1995 return expedited_reg_nums;
1996}
1997
1998llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails>
1999NativeRegisterContextLinux_arm64::GetMemoryTaggingDetails(int32_t type) {
2001 return MemoryTaggingDetails{std::make_unique<MemoryTagManagerAArch64MTE>(),
2003 }
2004
2005 return llvm::createStringError(llvm::inconvertibleErrorCode(),
2006 "Unknown AArch64 memory tag type %d", type);
2007}
2008
2009lldb::addr_t NativeRegisterContextLinux_arm64::FixWatchpointHitAddress(
2010 lldb::addr_t hit_addr) {
2011 // Linux configures user-space virtual addresses with top byte ignored.
2012 // We set default value of mask such that top byte is masked out.
2013 lldb::addr_t mask = ~((1ULL << 56) - 1);
2014
2015 // Try to read pointer authentication data_mask register and calculate a
2016 // consolidated data address mask after ignoring the top byte.
2017 if (ReadPAuthMask().Success())
2018 mask |= m_pac_mask.data_mask;
2019
2020 return hit_addr & ~mask;
2021 ;
2022}
2023
2024#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.