**Note:** The register reading and writing depends on new register flavor support in thread_get_state/thread_set_state in the kernel, which will be first available in macOS 15.4. The Apple M4 line of cores includes the Scalable Matrix Extension (SME) feature. The M4s do not implement Scalable Vector Extension (SVE), although the processor is in Streaming SVE Mode when the SME is being used. The most obvious side effects of being in SSVE Mode are that (on the M4 cores) NEON instructions cannot be used, and watchpoints may get false positives, the address comparisons are done at a lowered granularity. When SSVE mode is enabled, the kernel will provide the Streaming Vector Length register, which is a maximum of 64 bytes with the M4. Also provided are SVCR (with bits indicating if SSVE mode and SME mode are enabled), TPIDR2, SVL. Then the SVE registers Z0..31 (SVL bytes long), P0..15 (SVL/8 bytes), the ZA matrix register (SVL*SVL bytes), and the M4 supports SME2, so the ZT0 register (64 bytes). When SSVE/SME are disabled, none of these registers are provided by the kernel - reads and writes of them will fail. Unlike Linux, lldb cannot modify the SVL through a thread_set_state call, or change the processor state's SSVE/SME status. There is also no way for a process to request a lowered SVL size today, so the work that David did to handle VL/SVL changing while stepping through a process is not an issue on Darwin today. But debugserver should be providing everything necessary so we can reuse all of David's work on resizing the register contexts in lldb if it happens in the future. debugbserver sends svl, svcr, and tpidr2 in the expedited registers when a thread stops, if SSVE|SME mode are enabled (if the kernel allows it to read the ARM_SME_STATE register set). While the maximum SVL is 64 bytes on M4, the AArch64 maximum possible SVL is 256; this would give us a 64k ZA register. If debugserver sized all of its register contexts assuming the largest possible SVL, we could easily use 2MB more memory for the register contexts of all threads in a process -- and on iOS et al, processes must run within a small memory allotment and this would push us over that. Much of the work in debugserver was changing the arm64 register context from being a static compile-time array of register sets, to being initialized at runtime if debugserver is running on a machine with SME. The ZA is only created to the machine's actual maximum SVL. The size of the 32 SVE Z registers is less significant so I am statically allocating those to the architecturally largest possible SVL value today. Also, debugserver includes information about registers that share the same part of the register file. e.g. S0 and D0 are the lower parts of the NEON 128-bit V0 register. And when running on an SME machine, v0 is the lower 128 bits of the SVE Z0 register. So the register maps used when defining the VFP registers must differ depending on the capabilities of the cpu at runtime. I also changed register reading in debugserver, where formerly when debugserver was asked to read a register, and the thread_get_state read of that register failed, it would return all zero's. This is necessary when constructing a `g` packet that gets all registers - because there is no separation between register bytes, the offsets are fixed. But when we are asking for a single register (e.g. Z0) when not in SSVE/SME mode, this should return an error. This does mean that when you're running on an SME capabable machine, but not in SME mode, and do `register read -a`, lldb will report that 48 SVE registers were unavailable and 5 SME registers were unavailable. But that's only when `-a` is used. The register reading and writing depends on new register flavor support in thread_get_state/thread_set_state in the kernel, which is not yet in a release. The test case I wrote is skipped on current OSes. I pilfered the SME register setup from some of David's existing SME test files; there were a few Linux specific details in those tests that they weren't easy to reuse on Darwin. rdar://121608074
144 lines
5.2 KiB
C++
144 lines
5.2 KiB
C++
//===-- ArchitectureAArch64.cpp -------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Plugins/Architecture/AArch64/ArchitectureAArch64.h"
|
|
#include "lldb/Core/PluginManager.h"
|
|
#include "lldb/Target/RegisterContext.h"
|
|
#include "lldb/Utility/ArchSpec.h"
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
|
#include "lldb/Utility/DataExtractor.h"
|
|
|
|
using namespace lldb_private;
|
|
using namespace lldb;
|
|
|
|
LLDB_PLUGIN_DEFINE(ArchitectureAArch64)
|
|
|
|
void ArchitectureAArch64::Initialize() {
|
|
PluginManager::RegisterPlugin(GetPluginNameStatic(),
|
|
"AArch64-specific algorithms",
|
|
&ArchitectureAArch64::Create);
|
|
}
|
|
|
|
void ArchitectureAArch64::Terminate() {
|
|
PluginManager::UnregisterPlugin(&ArchitectureAArch64::Create);
|
|
}
|
|
|
|
std::unique_ptr<Architecture>
|
|
ArchitectureAArch64::Create(const ArchSpec &arch) {
|
|
auto machine = arch.GetMachine();
|
|
if (machine != llvm::Triple::aarch64 && machine != llvm::Triple::aarch64_be &&
|
|
machine != llvm::Triple::aarch64_32) {
|
|
return nullptr;
|
|
}
|
|
return std::unique_ptr<Architecture>(new ArchitectureAArch64());
|
|
}
|
|
|
|
static void
|
|
UpdateARM64SVERegistersInfos(DynamicRegisterInfo::reg_collection_range regs,
|
|
uint64_t vg) {
|
|
// SVE Z register size is vg x 8 bytes.
|
|
uint32_t z_reg_byte_size = vg * 8;
|
|
|
|
// SVE vector length has changed, accordingly set size of Z, P and FFR
|
|
// registers. Also invalidate register offsets it will be recalculated
|
|
// after SVE register size update.
|
|
for (auto ® : regs) {
|
|
if (reg.value_regs == nullptr) {
|
|
if (reg.name[0] == 'z' && isdigit(reg.name[1]))
|
|
reg.byte_size = z_reg_byte_size;
|
|
else if (reg.name[0] == 'p' && isdigit(reg.name[1]))
|
|
reg.byte_size = vg;
|
|
else if (strcmp(reg.name, "ffr") == 0)
|
|
reg.byte_size = vg;
|
|
}
|
|
reg.byte_offset = LLDB_INVALID_INDEX32;
|
|
}
|
|
}
|
|
|
|
static void
|
|
UpdateARM64SMERegistersInfos(DynamicRegisterInfo::reg_collection_range regs,
|
|
uint64_t svg) {
|
|
for (auto ® : regs) {
|
|
if (strcmp(reg.name, "za") == 0) {
|
|
// ZA is a register with size (svg*8) * (svg*8). A square essentially.
|
|
reg.byte_size = (svg * 8) * (svg * 8);
|
|
}
|
|
reg.byte_offset = LLDB_INVALID_INDEX32;
|
|
}
|
|
}
|
|
|
|
bool ArchitectureAArch64::ReconfigureRegisterInfo(DynamicRegisterInfo ®_info,
|
|
DataExtractor ®_data,
|
|
RegisterContext ®_context
|
|
|
|
) const {
|
|
// Once we start to reconfigure registers, we cannot read any of them.
|
|
// So we must read VG and SVG up front.
|
|
|
|
const uint64_t fail_value = LLDB_INVALID_ADDRESS;
|
|
std::optional<uint64_t> vg_reg_value;
|
|
const RegisterInfo *vg_reg_info = reg_info.GetRegisterInfo("vg");
|
|
if (vg_reg_info) {
|
|
uint32_t vg_reg_num = vg_reg_info->kinds[eRegisterKindLLDB];
|
|
uint64_t reg_value =
|
|
reg_context.ReadRegisterAsUnsigned(vg_reg_num, fail_value);
|
|
if (reg_value != fail_value && reg_value <= 32)
|
|
vg_reg_value = reg_value;
|
|
}
|
|
|
|
std::optional<uint64_t> svg_reg_value;
|
|
const RegisterInfo *svg_reg_info = reg_info.GetRegisterInfo("svg");
|
|
if (svg_reg_info) {
|
|
uint32_t svg_reg_num = svg_reg_info->kinds[eRegisterKindLLDB];
|
|
uint64_t reg_value =
|
|
reg_context.ReadRegisterAsUnsigned(svg_reg_num, fail_value);
|
|
if (reg_value != fail_value && reg_value <= 32)
|
|
svg_reg_value = reg_value;
|
|
}
|
|
if (!svg_reg_value) {
|
|
const RegisterInfo *darwin_svg_reg_info = reg_info.GetRegisterInfo("svl");
|
|
if (darwin_svg_reg_info) {
|
|
uint32_t svg_reg_num = darwin_svg_reg_info->kinds[eRegisterKindLLDB];
|
|
uint64_t reg_value =
|
|
reg_context.ReadRegisterAsUnsigned(svg_reg_num, fail_value);
|
|
// UpdateARM64SVERegistersInfos and UpdateARM64SMERegistersInfos
|
|
// expect the number of 8-byte granules; darwin provides number of
|
|
// bytes.
|
|
if (reg_value != fail_value && reg_value <= 256) {
|
|
svg_reg_value = reg_value / 8;
|
|
// Apple hardware only implements Streaming SVE mode, so
|
|
// the non-streaming Vector Length is not reported by the
|
|
// kernel. Set both svg and vg to this svl value.
|
|
if (!vg_reg_value)
|
|
vg_reg_value = reg_value / 8;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!vg_reg_value && !svg_reg_value)
|
|
return false;
|
|
|
|
auto regs = reg_info.registers<DynamicRegisterInfo::reg_collection_range>();
|
|
if (vg_reg_value)
|
|
UpdateARM64SVERegistersInfos(regs, *vg_reg_value);
|
|
if (svg_reg_value)
|
|
UpdateARM64SMERegistersInfos(regs, *svg_reg_value);
|
|
|
|
// At this point if we have updated any registers, their offsets will all be
|
|
// invalid. If we did, we need to update them all.
|
|
reg_info.ConfigureOffsets();
|
|
// From here we are able to read registers again.
|
|
|
|
// Make a heap based buffer that is big enough to store all registers
|
|
reg_data.SetData(
|
|
std::make_shared<DataBufferHeap>(reg_info.GetRegisterDataByteSize(), 0));
|
|
reg_data.SetByteOrder(reg_context.GetByteOrder());
|
|
|
|
return true;
|
|
}
|