9#if defined(__i386__) || defined(__x86_64__)
24#include <sys/ptrace.h>
25#include <sys/sysctl.h>
28#include <x86/cpu_extended_state.h>
29#include <x86/specialreg.h>
83 k_num_gpr_registers_x86_64,
84 "g_gpr_regnums_x86_64 has wrong number of register infos");
87static const uint32_t g_fpu_regnums_x86_64[] = {
105static_assert((
sizeof(g_fpu_regnums_x86_64) /
sizeof(g_fpu_regnums_x86_64[0])) -
107 k_num_fpr_registers_x86_64,
108 "g_fpu_regnums_x86_64 has wrong number of register infos");
119 k_num_avx_registers_x86_64,
120 "g_avx_regnums_x86_64 has wrong number of register infos");
122static const uint32_t g_mpx_regnums_x86_64[] = {
129static_assert((
sizeof(g_mpx_regnums_x86_64) /
sizeof(g_mpx_regnums_x86_64[0])) -
131 k_num_mpx_registers_x86_64,
132 "g_mpx_regnums_x86_64 has wrong number of register infos");
135static const uint32_t g_dbr_regnums_x86_64[] = {
140static_assert((
sizeof(g_dbr_regnums_x86_64) /
sizeof(g_dbr_regnums_x86_64[0])) -
142 k_num_dbr_registers_x86_64,
143 "g_dbr_regnums_x86_64 has wrong number of register infos");
159 k_num_gpr_registers_i386,
160 "g_gpr_regnums_i386 has wrong number of register infos");
163static const uint32_t g_fpu_regnums_i386[] = {
175static_assert((
sizeof(g_fpu_regnums_i386) /
sizeof(g_fpu_regnums_i386[0])) -
177 k_num_fpr_registers_i386,
178 "g_fpu_regnums_i386 has wrong number of register infos");
187 k_num_avx_registers_i386,
188 "g_avx_regnums_i386 has wrong number of register infos");
190static const uint32_t g_mpx_regnums_i386[] = {
197static_assert((
sizeof(g_mpx_regnums_i386) /
sizeof(g_mpx_regnums_i386[0])) -
199 k_num_mpx_registers_i386,
200 "g_mpx_regnums_i386 has wrong number of register infos");
203static const uint32_t g_dbr_regnums_i386[] = {
208static_assert((
sizeof(g_dbr_regnums_i386) /
sizeof(g_dbr_regnums_i386[0])) -
210 k_num_dbr_registers_i386,
211 "g_dbr_regnums_i386 has wrong number of register infos");
234 g_fpu_regnums_x86_64},
236 g_dbr_regnums_x86_64},
240 g_mpx_regnums_x86_64},
243#define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize())
246NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
248 return new NativeRegisterContextNetBSD_x86_64(target_arch, native_thread);
254CreateRegisterInfoInterface(
const ArchSpec &target_arch) {
255 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) {
259 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
260 "Register setting path assumes this is a 64-bit host");
267NativeRegisterContextNetBSD_x86_64::NativeRegisterContextNetBSD_x86_64(
270 native_thread, CreateRegisterInfoInterface(target_arch)),
272 assert(m_gpr.size() == GetRegisterInfoInterface().GetGPRSize());
273 std::array<uint32_t, MaxRegularRegSet + 1> first_regnos;
275 switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
276 case llvm::Triple::x86:
280 case llvm::Triple::x86_64:
285 llvm_unreachable(
"Unhandled target architecture.");
288 for (
int i : {FPRegSet, DBRegSet})
289 m_regset_offsets[i] = GetRegisterInfoInterface()
290 .GetRegisterInfo()[first_regnos[i]]
294uint32_t NativeRegisterContextNetBSD_x86_64::GetRegisterSetCount()
const {
299NativeRegisterContextNetBSD_x86_64::GetRegisterSet(uint32_t set_index)
const {
300 switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
301 case llvm::Triple::x86:
303 case llvm::Triple::x86_64:
306 llvm_unreachable(
"Unhandled target architecture.");
310std::optional<NativeRegisterContextNetBSD_x86_64::RegSetKind>
311NativeRegisterContextNetBSD_x86_64::GetSetForNativeRegNum(
312 uint32_t reg_num)
const {
313 switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
314 case llvm::Triple::x86:
315 if (reg_num >= k_first_gpr_i386 && reg_num <= k_last_gpr_i386)
317 if (reg_num >= k_first_fpr_i386 && reg_num <= k_last_fpr_i386)
319 if (reg_num >= k_first_avx_i386 && reg_num <= k_last_avx_i386)
321 if (reg_num >= k_first_mpxr_i386 && reg_num <= k_last_mpxr_i386)
323 if (reg_num >= k_first_mpxc_i386 && reg_num <= k_last_mpxc_i386)
325 if (reg_num >= k_first_dbr_i386 && reg_num <= k_last_dbr_i386)
328 case llvm::Triple::x86_64:
329 if (reg_num >= k_first_gpr_x86_64 && reg_num <= k_last_gpr_x86_64)
331 if (reg_num >= k_first_fpr_x86_64 && reg_num <= k_last_fpr_x86_64)
333 if (reg_num >= k_first_avx_x86_64 && reg_num <= k_last_avx_x86_64)
335 if (reg_num >= k_first_mpxr_x86_64 && reg_num <= k_last_mpxr_x86_64)
337 if (reg_num >= k_first_mpxc_x86_64 && reg_num <= k_last_mpxc_x86_64)
339 if (reg_num >= k_first_dbr_x86_64 && reg_num <= k_last_dbr_x86_64)
343 llvm_unreachable(
"Unhandled target architecture.");
346 llvm_unreachable(
"Register does not belong to any register set");
349Status NativeRegisterContextNetBSD_x86_64::ReadRegisterSet(RegSetKind set) {
352 return DoRegisterSet(PT_GETREGS, m_gpr.data());
354 return DoRegisterSet(PT_GETDBREGS, m_dbr.data());
358 struct iovec iov = {m_xstate.data(), m_xstate.size()};
359 Status ret = DoRegisterSet(PT_GETXSTATE, &iov);
360 assert(
reinterpret_cast<xstate *
>(m_xstate.data())->xs_rfbm & XCR0_X87);
364 llvm_unreachable(
"NativeRegisterContextNetBSD_x86_64::ReadRegisterSet");
367Status NativeRegisterContextNetBSD_x86_64::WriteRegisterSet(RegSetKind set) {
370 return DoRegisterSet(PT_SETREGS, m_gpr.data());
372 return DoRegisterSet(PT_SETDBREGS, m_dbr.data());
376 struct iovec iov = {&m_xstate,
sizeof(m_xstate)};
377 return DoRegisterSet(PT_SETXSTATE, &iov);
380 llvm_unreachable(
"NativeRegisterContextNetBSD_x86_64::WriteRegisterSet");
384NativeRegisterContextNetBSD_x86_64::ReadRegister(
const RegisterInfo *reg_info,
389 error.SetErrorString(
"reg_info NULL");
397 error.SetErrorStringWithFormat(
"register \"%s\" is an internal-only lldb "
398 "register, cannot read directly",
403 std::optional<RegSetKind> opt_set = GetSetForNativeRegNum(reg);
407 error.SetErrorStringWithFormat(
"register \"%s\" is in unrecognized set",
412 RegSetKind set = *opt_set;
413 error = ReadRegisterSet(set);
421 void *data = GetOffsetRegSetData(set, reg_info->
byte_offset);
422 FXSAVE *fpr =
reinterpret_cast<FXSAVE *
>(m_xstate.data() +
423 offsetof(xstate, xs_fxsave));
424 if (data == &fpr->
ftag)
432 std::optional<YMMSplitPtr> ymm_reg = GetYMMSplitReg(reg);
434 error.SetErrorStringWithFormat(
435 "register \"%s\" not supported by CPU/kernel", reg_info->
name);
439 endian::InlHostByteOrder());
444 llvm_unreachable(
"MPX regset should have returned error");
450Status NativeRegisterContextNetBSD_x86_64::WriteRegister(
456 error.SetErrorString(
"reg_info NULL");
464 error.SetErrorStringWithFormat(
"register \"%s\" is an internal-only lldb "
465 "register, cannot read directly",
470 std::optional<RegSetKind> opt_set = GetSetForNativeRegNum(reg);
474 error.SetErrorStringWithFormat(
"register \"%s\" is in unrecognized set",
479 RegSetKind set = *opt_set;
480 uint64_t new_xstate_bv = 0;
482 error = ReadRegisterSet(set);
489 ::memcpy(GetOffsetRegSetData(set, reg_info->
byte_offset),
493 void *data = GetOffsetRegSetData(set, reg_info->
byte_offset);
494 FXSAVE *fpr =
reinterpret_cast<FXSAVE *
>(m_xstate.data() +
495 offsetof(xstate, xs_fxsave));
496 if (data == &fpr->
ftag)
500 if (data >= &fpr->
xmm)
501 new_xstate_bv |= XCR0_SSE;
502 else if (data >= &fpr->
mxcsr && data < &fpr->stmm)
503 new_xstate_bv |= XCR0_SSE;
505 new_xstate_bv |= XCR0_X87;
509 std::optional<YMMSplitPtr> ymm_reg = GetYMMSplitReg(reg);
511 error.SetErrorStringWithFormat(
512 "register \"%s\" not supported by CPU/kernel", reg_info->
name);
517 new_xstate_bv |= XCR0_SSE | XCR0_YMM_Hi128;
522 llvm_unreachable(
"MPX regset should have returned error");
525 if (new_xstate_bv != 0)
526 reinterpret_cast<xstate *
>(m_xstate.data())->xs_xstate_bv |= new_xstate_bv;
527 return WriteRegisterSet(set);
530Status NativeRegisterContextNetBSD_x86_64::ReadAllRegisterValues(
535 error = ReadRegisterSet(GPRegSet);
539 uint8_t *dst = data_sp->GetBytes();
540 ::memcpy(dst, m_gpr.data(), GetRegisterInfoInterface().GetGPRSize());
541 dst += GetRegisterInfoInterface().GetGPRSize();
546Status NativeRegisterContextNetBSD_x86_64::WriteAllRegisterValues(
551 error.SetErrorStringWithFormat(
552 "NativeRegisterContextNetBSD_x86_64::%s invalid data_sp provided",
558 error.SetErrorStringWithFormat(
559 "NativeRegisterContextNetBSD_x86_64::%s data_sp contained mismatched "
560 "data size, expected %zu, actual %" PRIu64,
565 const uint8_t *src = data_sp->GetBytes();
566 if (src ==
nullptr) {
567 error.SetErrorStringWithFormat(
"NativeRegisterContextNetBSD_x86_64::%s "
568 "DataBuffer::GetBytes() returned a null "
573 ::memcpy(m_gpr.data(), src, GetRegisterInfoInterface().GetGPRSize());
575 error = WriteRegisterSet(GPRegSet);
578 src += GetRegisterInfoInterface().GetGPRSize();
583llvm::Error NativeRegisterContextNetBSD_x86_64::CopyHardwareWatchpointsFrom(
585 auto &r_source =
static_cast<NativeRegisterContextNetBSD_x86_64 &
>(source);
588 Status res = r_source.ReadRegister(GetDR(7), dr7);
592 return llvm::Error::success();
594 m_dbr = r_source.m_dbr;
595 res = WriteRegisterSet(DBRegSet);
601NativeRegisterContextNetBSD_x86_64::GetOffsetRegSetData(RegSetKind set,
609 base = m_xstate.data() + offsetof(xstate, xs_fxsave);
615 llvm_unreachable(
"GetRegSetData() is unsuitable for this regset.");
617 llvm_unreachable(
"MPX regset should have returned error");
619 assert(reg_offset >= m_regset_offsets[set]);
620 return base + (reg_offset - m_regset_offsets[set]);
623std::optional<NativeRegisterContextNetBSD_x86_64::YMMSplitPtr>
624NativeRegisterContextNetBSD_x86_64::GetYMMSplitReg(uint32_t reg) {
625 auto xst =
reinterpret_cast<xstate *
>(m_xstate.data());
626 if (!(xst->xs_rfbm & XCR0_SSE) || !(xst->xs_rfbm & XCR0_YMM_Hi128))
630 switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
631 case llvm::Triple::x86:
634 case llvm::Triple::x86_64:
638 llvm_unreachable(
"Unhandled target architecture.");
641 return YMMSplitPtr{&xst->xs_fxsave.fx_xmm[reg_index],
642 &xst->xs_ymm_hi128.xs_ymm[reg_index]};
static llvm::raw_ostream & error(Stream &strm)
constexpr size_t k_num_register_sets
static const RegisterSet g_reg_sets_i386[]
static const uint32_t g_gpr_regnums_x86_64[]
const uint32_t g_gpr_regnums_i386[]
static const RegisterSet g_reg_sets_x86_64[]
const uint32_t g_avx_regnums_i386[]
static const uint32_t g_avx_regnums_x86_64[]
An architecture specification class.
A subclass of DataBuffer that stores a data buffer on the heap.
RegisterInfo interface to patch RegisterInfo structure for archs.
uint16_t GetAsUInt16(uint16_t fail_value=UINT16_MAX, bool *success_ptr=nullptr) const
void SetUInt16(uint16_t uint)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
const void * GetBytes() const
uint32_t GetByteSize() const
llvm::Error ToError() const
bool Fail() const
Test for error condition.
#define LLDB_INVALID_REGNUM
A class that represents a running process on the host machine.
void YMMToXState(const YMMReg &input, void *xmm_bytes, void *ymmh_bytes)
uint16_t AbridgedToFullTagWord(uint8_t abridged_tw, uint16_t sw, llvm::ArrayRef< MMSReg > st_regs)
YMMReg XStateToYMM(const void *xmm_bytes, const void *ymmh_bytes)
@ k_num_avx_registers_i386
@ k_num_fpr_registers_i386
@ k_num_mpx_registers_i386
@ k_num_dbr_registers_i386
@ k_num_gpr_registers_i386
uint8_t FullToAbridgedTagWord(uint16_t tw)
@ k_num_fpr_registers_x86_64
@ k_num_avx_registers_x86_64
@ k_num_dbr_registers_x86_64
@ k_num_mpx_registers_x86_64
@ k_num_gpr_registers_x86_64
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
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.