Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/FreeBSD/NativeRegisterContextFreeBSD_arm64.cpp
96380 views
//===-- NativeRegisterContextFreeBSD_arm64.cpp ----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#if defined(__aarch64__)910#include "NativeRegisterContextFreeBSD_arm64.h"1112#include "lldb/Utility/DataBufferHeap.h"13#include "lldb/Utility/RegisterValue.h"14#include "lldb/Utility/Status.h"1516#include "Plugins/Process/FreeBSD/NativeProcessFreeBSD.h"17#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"18#include "Plugins/Process/Utility/RegisterFlagsDetector_arm64.h"19#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"2021// clang-format off22#include <sys/param.h>23#include <sys/ptrace.h>24#include <sys/types.h>25// clang-format on2627using namespace lldb;28using namespace lldb_private;29using namespace lldb_private::process_freebsd;3031// A NativeRegisterContext is constructed per thread, but all threads' registers32// will contain the same fields. Therefore this mutex prevents each instance33// competing with the other, and subsequent instances from having to detect the34// fields all over again.35static std::mutex g_register_flags_detector_mutex;36static Arm64RegisterFlagsDetector g_register_flags_detector;3738NativeRegisterContextFreeBSD *39NativeRegisterContextFreeBSD::CreateHostNativeRegisterContextFreeBSD(40const ArchSpec &target_arch, NativeThreadFreeBSD &native_thread) {41std::lock_guard<std::mutex> lock(g_register_flags_detector_mutex);42if (!g_register_flags_detector.HasDetected()) {43NativeProcessFreeBSD &process = native_thread.GetProcess();44g_register_flags_detector.DetectFields(45process.GetAuxValue(AuxVector::AUXV_FREEBSD_AT_HWCAP).value_or(0),46process.GetAuxValue(AuxVector::AUXV_AT_HWCAP2).value_or(0));47}4849return new NativeRegisterContextFreeBSD_arm64(target_arch, native_thread);50}5152NativeRegisterContextFreeBSD_arm64::NativeRegisterContextFreeBSD_arm64(53const ArchSpec &target_arch, NativeThreadFreeBSD &native_thread)54: NativeRegisterContextRegisterInfo(55native_thread, new RegisterInfoPOSIX_arm64(target_arch, 0))56#ifdef LLDB_HAS_FREEBSD_WATCHPOINT57,58m_read_dbreg(false)59#endif60{61g_register_flags_detector.UpdateRegisterInfo(62GetRegisterInfoInterface().GetRegisterInfo(),63GetRegisterInfoInterface().GetRegisterCount());6465::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));66::memset(&m_hbp_regs, 0, sizeof(m_hbp_regs));67}6869RegisterInfoPOSIX_arm64 &70NativeRegisterContextFreeBSD_arm64::GetRegisterInfo() const {71return static_cast<RegisterInfoPOSIX_arm64 &>(*m_register_info_interface_up);72}7374uint32_t NativeRegisterContextFreeBSD_arm64::GetRegisterSetCount() const {75return GetRegisterInfo().GetRegisterSetCount();76}7778const RegisterSet *79NativeRegisterContextFreeBSD_arm64::GetRegisterSet(uint32_t set_index) const {80return GetRegisterInfo().GetRegisterSet(set_index);81}8283uint32_t NativeRegisterContextFreeBSD_arm64::GetUserRegisterCount() const {84uint32_t count = 0;85for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)86count += GetRegisterSet(set_index)->num_registers;87return count;88}8990Status NativeRegisterContextFreeBSD_arm64::ReadRegisterSet(uint32_t set) {91switch (set) {92case RegisterInfoPOSIX_arm64::GPRegSet:93return NativeProcessFreeBSD::PtraceWrapper(PT_GETREGS, m_thread.GetID(),94m_reg_data.data());95case RegisterInfoPOSIX_arm64::FPRegSet:96return NativeProcessFreeBSD::PtraceWrapper(97PT_GETFPREGS, m_thread.GetID(),98m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));99}100llvm_unreachable("NativeRegisterContextFreeBSD_arm64::ReadRegisterSet");101}102103Status NativeRegisterContextFreeBSD_arm64::WriteRegisterSet(uint32_t set) {104switch (set) {105case RegisterInfoPOSIX_arm64::GPRegSet:106return NativeProcessFreeBSD::PtraceWrapper(PT_SETREGS, m_thread.GetID(),107m_reg_data.data());108case RegisterInfoPOSIX_arm64::FPRegSet:109return NativeProcessFreeBSD::PtraceWrapper(110PT_SETFPREGS, m_thread.GetID(),111m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));112}113llvm_unreachable("NativeRegisterContextFreeBSD_arm64::WriteRegisterSet");114}115116Status117NativeRegisterContextFreeBSD_arm64::ReadRegister(const RegisterInfo *reg_info,118RegisterValue ®_value) {119Status error;120121if (!reg_info) {122error.SetErrorString("reg_info NULL");123return error;124}125126const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];127128if (reg == LLDB_INVALID_REGNUM)129return Status("no lldb regnum for %s", reg_info && reg_info->name130? reg_info->name131: "<unknown register>");132133uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);134error = ReadRegisterSet(set);135if (error.Fail())136return error;137138assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());139reg_value.SetBytes(m_reg_data.data() + reg_info->byte_offset,140reg_info->byte_size, endian::InlHostByteOrder());141return error;142}143144Status NativeRegisterContextFreeBSD_arm64::WriteRegister(145const RegisterInfo *reg_info, const RegisterValue ®_value) {146Status error;147148if (!reg_info)149return Status("reg_info NULL");150151const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];152153if (reg == LLDB_INVALID_REGNUM)154return Status("no lldb regnum for %s", reg_info && reg_info->name155? reg_info->name156: "<unknown register>");157158uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);159error = ReadRegisterSet(set);160if (error.Fail())161return error;162163assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());164::memcpy(m_reg_data.data() + reg_info->byte_offset, reg_value.GetBytes(),165reg_info->byte_size);166167return WriteRegisterSet(set);168}169170Status NativeRegisterContextFreeBSD_arm64::ReadAllRegisterValues(171lldb::WritableDataBufferSP &data_sp) {172Status error;173174error = ReadRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);175if (error.Fail())176return error;177178error = ReadRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);179if (error.Fail())180return error;181182data_sp.reset(new DataBufferHeap(m_reg_data.size(), 0));183uint8_t *dst = data_sp->GetBytes();184::memcpy(dst, m_reg_data.data(), m_reg_data.size());185186return error;187}188189Status NativeRegisterContextFreeBSD_arm64::WriteAllRegisterValues(190const lldb::DataBufferSP &data_sp) {191Status error;192193if (!data_sp) {194error.SetErrorStringWithFormat(195"NativeRegisterContextFreeBSD_arm64::%s invalid data_sp provided",196__FUNCTION__);197return error;198}199200if (data_sp->GetByteSize() != m_reg_data.size()) {201error.SetErrorStringWithFormat(202"NativeRegisterContextFreeBSD_arm64::%s data_sp contained mismatched "203"data size, expected %" PRIu64 ", actual %" PRIu64,204__FUNCTION__, m_reg_data.size(), data_sp->GetByteSize());205return error;206}207208const uint8_t *src = data_sp->GetBytes();209if (src == nullptr) {210error.SetErrorStringWithFormat("NativeRegisterContextFreeBSD_arm64::%s "211"DataBuffer::GetBytes() returned a null "212"pointer",213__FUNCTION__);214return error;215}216::memcpy(m_reg_data.data(), src, m_reg_data.size());217218error = WriteRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);219if (error.Fail())220return error;221222return WriteRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);223}224225llvm::Error NativeRegisterContextFreeBSD_arm64::CopyHardwareWatchpointsFrom(226NativeRegisterContextFreeBSD &source) {227#ifdef LLDB_HAS_FREEBSD_WATCHPOINT228auto &r_source = static_cast<NativeRegisterContextFreeBSD_arm64 &>(source);229llvm::Error error = r_source.ReadHardwareDebugInfo();230if (error)231return error;232233m_dbreg = r_source.m_dbreg;234m_hbp_regs = r_source.m_hbp_regs;235m_hwp_regs = r_source.m_hwp_regs;236m_max_hbp_supported = r_source.m_max_hbp_supported;237m_max_hwp_supported = r_source.m_max_hwp_supported;238m_read_dbreg = true;239240// on FreeBSD this writes both breakpoints and watchpoints241return WriteHardwareDebugRegs(eDREGTypeWATCH);242#else243return llvm::Error::success();244#endif245}246247llvm::Error NativeRegisterContextFreeBSD_arm64::ReadHardwareDebugInfo() {248#ifdef LLDB_HAS_FREEBSD_WATCHPOINT249Log *log = GetLog(POSIXLog::Registers);250251// we're fully stateful, so no need to reread control registers ever252if (m_read_dbreg)253return llvm::Error::success();254255Status res = NativeProcessFreeBSD::PtraceWrapper(PT_GETDBREGS,256m_thread.GetID(), &m_dbreg);257if (res.Fail())258return res.ToError();259260LLDB_LOG(log, "m_dbreg read: debug_ver={0}, nbkpts={1}, nwtpts={2}",261m_dbreg.db_debug_ver, m_dbreg.db_nbkpts, m_dbreg.db_nwtpts);262m_max_hbp_supported = m_dbreg.db_nbkpts;263m_max_hwp_supported = m_dbreg.db_nwtpts;264assert(m_max_hbp_supported <= m_hbp_regs.size());265assert(m_max_hwp_supported <= m_hwp_regs.size());266267m_read_dbreg = true;268return llvm::Error::success();269#else270return llvm::createStringError(271llvm::inconvertibleErrorCode(),272"Hardware breakpoints/watchpoints require FreeBSD 14.0");273#endif274}275276llvm::Error277NativeRegisterContextFreeBSD_arm64::WriteHardwareDebugRegs(DREGType) {278#ifdef LLDB_HAS_FREEBSD_WATCHPOINT279assert(m_read_dbreg && "dbregs must be read before writing them back");280281// copy data from m_*_regs to m_dbreg before writing it back282for (uint32_t i = 0; i < m_max_hbp_supported; i++) {283m_dbreg.db_breakregs[i].dbr_addr = m_hbp_regs[i].address;284m_dbreg.db_breakregs[i].dbr_ctrl = m_hbp_regs[i].control;285}286for (uint32_t i = 0; i < m_max_hwp_supported; i++) {287m_dbreg.db_watchregs[i].dbw_addr = m_hwp_regs[i].address;288m_dbreg.db_watchregs[i].dbw_ctrl = m_hwp_regs[i].control;289}290291return NativeProcessFreeBSD::PtraceWrapper(PT_SETDBREGS, m_thread.GetID(),292&m_dbreg)293.ToError();294#else295return llvm::createStringError(296llvm::inconvertibleErrorCode(),297"Hardware breakpoints/watchpoints require FreeBSD 14.0");298#endif299}300301#endif // defined (__aarch64__)302303304