1 //===-- NativeRegisterContextNetBSD_x86_64.cpp ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #if defined(__x86_64__)
12 #include "NativeRegisterContextNetBSD_x86_64.h"
14 #include "lldb/Core/RegisterValue.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Status.h"
20 #include "Plugins/Process/Utility/RegisterContextNetBSD_x86_64.h"
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
32 using namespace lldb_private;
33 using namespace lldb_private::process_netbsd;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 // x86 64-bit general purpose registers.
41 static const uint32_t g_gpr_regnums_x86_64[] = {
42 lldb_rax_x86_64, lldb_rbx_x86_64, lldb_rcx_x86_64, lldb_rdx_x86_64,
43 lldb_rdi_x86_64, lldb_rsi_x86_64, lldb_rbp_x86_64, lldb_rsp_x86_64,
44 lldb_r8_x86_64, lldb_r9_x86_64, lldb_r10_x86_64, lldb_r11_x86_64,
45 lldb_r12_x86_64, lldb_r13_x86_64, lldb_r14_x86_64, lldb_r15_x86_64,
46 lldb_rip_x86_64, lldb_rflags_x86_64, lldb_cs_x86_64, lldb_fs_x86_64,
47 lldb_gs_x86_64, lldb_ss_x86_64, lldb_ds_x86_64, lldb_es_x86_64,
48 lldb_eax_x86_64, lldb_ebx_x86_64, lldb_ecx_x86_64, lldb_edx_x86_64,
49 lldb_edi_x86_64, lldb_esi_x86_64, lldb_ebp_x86_64, lldb_esp_x86_64,
50 lldb_r8d_x86_64, // Low 32 bits or r8
51 lldb_r9d_x86_64, // Low 32 bits or r9
52 lldb_r10d_x86_64, // Low 32 bits or r10
53 lldb_r11d_x86_64, // Low 32 bits or r11
54 lldb_r12d_x86_64, // Low 32 bits or r12
55 lldb_r13d_x86_64, // Low 32 bits or r13
56 lldb_r14d_x86_64, // Low 32 bits or r14
57 lldb_r15d_x86_64, // Low 32 bits or r15
58 lldb_ax_x86_64, lldb_bx_x86_64, lldb_cx_x86_64, lldb_dx_x86_64,
59 lldb_di_x86_64, lldb_si_x86_64, lldb_bp_x86_64, lldb_sp_x86_64,
60 lldb_r8w_x86_64, // Low 16 bits or r8
61 lldb_r9w_x86_64, // Low 16 bits or r9
62 lldb_r10w_x86_64, // Low 16 bits or r10
63 lldb_r11w_x86_64, // Low 16 bits or r11
64 lldb_r12w_x86_64, // Low 16 bits or r12
65 lldb_r13w_x86_64, // Low 16 bits or r13
66 lldb_r14w_x86_64, // Low 16 bits or r14
67 lldb_r15w_x86_64, // Low 16 bits or r15
68 lldb_ah_x86_64, lldb_bh_x86_64, lldb_ch_x86_64, lldb_dh_x86_64,
69 lldb_al_x86_64, lldb_bl_x86_64, lldb_cl_x86_64, lldb_dl_x86_64,
70 lldb_dil_x86_64, lldb_sil_x86_64, lldb_bpl_x86_64, lldb_spl_x86_64,
71 lldb_r8l_x86_64, // Low 8 bits or r8
72 lldb_r9l_x86_64, // Low 8 bits or r9
73 lldb_r10l_x86_64, // Low 8 bits or r10
74 lldb_r11l_x86_64, // Low 8 bits or r11
75 lldb_r12l_x86_64, // Low 8 bits or r12
76 lldb_r13l_x86_64, // Low 8 bits or r13
77 lldb_r14l_x86_64, // Low 8 bits or r14
78 lldb_r15l_x86_64, // Low 8 bits or r15
79 LLDB_INVALID_REGNUM // register sets need to end with this flag
81 static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) -
83 k_num_gpr_registers_x86_64,
84 "g_gpr_regnums_x86_64 has wrong number of register infos");
86 // Number of register sets provided by this context.
87 enum { k_num_extended_register_sets = 2, k_num_register_sets = 4 };
89 // Register sets for x86 64-bit.
90 static const RegisterSet g_reg_sets_x86_64[k_num_register_sets] = {
91 {"General Purpose Registers", "gpr", k_num_gpr_registers_x86_64,
92 g_gpr_regnums_x86_64},
95 #define REG_CONTEXT_SIZE (GetRegisterInfoInterface().GetGPRSize())
97 const int fpu_present = []() -> int {
104 mib[0] = CTL_MACHDEP;
105 mib[1] = CPU_FPU_PRESENT;
107 error = sysctl(mib, __arraycount(mib), &val, &len, NULL, 0);
109 errx(EXIT_FAILURE, "sysctl");
114 const int osfxsr = []() -> int {
121 mib[0] = CTL_MACHDEP;
124 error = sysctl(mib, __arraycount(mib), &val, &len, NULL, 0);
126 errx(EXIT_FAILURE, "sysctl");
131 const int fpu_save = []() -> int {
138 mib[0] = CTL_MACHDEP;
139 mib[1] = CPU_FPU_SAVE;
141 error = sysctl(mib, __arraycount(mib), &val, &len, NULL, 0);
143 errx(EXIT_FAILURE, "sysctl");
150 NativeRegisterContextNetBSD *
151 NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
152 const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
153 uint32_t concrete_frame_idx) {
154 return new NativeRegisterContextNetBSD_x86_64(target_arch, native_thread,
158 // ----------------------------------------------------------------------------
159 // NativeRegisterContextNetBSD_x86_64 members.
160 // ----------------------------------------------------------------------------
162 static RegisterInfoInterface *
163 CreateRegisterInfoInterface(const ArchSpec &target_arch) {
164 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
165 "Register setting path assumes this is a 64-bit host");
166 // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the
167 // x86_64 register context.
168 return new RegisterContextNetBSD_x86_64(target_arch);
171 NativeRegisterContextNetBSD_x86_64::NativeRegisterContextNetBSD_x86_64(
172 const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
173 uint32_t concrete_frame_idx)
174 : NativeRegisterContextNetBSD(native_thread, concrete_frame_idx,
175 CreateRegisterInfoInterface(target_arch)),
176 m_gpr_x86_64(), m_fpr_x86_64(), m_dbr_x86_64() {}
178 // CONSIDER after local and llgs debugging are merged, register set support can
179 // be moved into a base x86-64 class with IsRegisterSetAvailable made virtual.
180 uint32_t NativeRegisterContextNetBSD_x86_64::GetRegisterSetCount() const {
182 for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) {
183 if (GetSetForNativeRegNum(set_index) != -1)
191 NativeRegisterContextNetBSD_x86_64::GetRegisterSet(uint32_t set_index) const {
192 switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
193 case llvm::Triple::x86_64:
194 return &g_reg_sets_x86_64[set_index];
196 assert(false && "Unhandled target architecture.");
203 int NativeRegisterContextNetBSD_x86_64::GetSetForNativeRegNum(
205 if (reg_num <= k_last_gpr_x86_64)
207 else if (reg_num <= k_last_fpr_x86_64)
208 return (fpu_present == 1 && osfxsr == 1 && fpu_save >= 1) ? FPRegSet : -1;
209 else if (reg_num <= k_last_avx_x86_64)
211 else if (reg_num <= k_last_mpxr_x86_64)
213 else if (reg_num <= k_last_mpxc_x86_64)
215 else if (reg_num <= lldb_dr7_x86_64)
216 return DBRegSet; // DBR
221 int NativeRegisterContextNetBSD_x86_64::ReadRegisterSet(uint32_t set) {
237 int NativeRegisterContextNetBSD_x86_64::WriteRegisterSet(uint32_t set) {
255 NativeRegisterContextNetBSD_x86_64::ReadRegister(const RegisterInfo *reg_info,
256 RegisterValue ®_value) {
260 error.SetErrorString("reg_info NULL");
264 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
265 if (reg == LLDB_INVALID_REGNUM) {
266 // This is likely an internal register for lldb use only and should not be
268 error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb "
269 "register, cannot read directly",
274 int set = GetSetForNativeRegNum(reg);
276 // This is likely an internal register for lldb use only and should not be
278 error.SetErrorStringWithFormat("register \"%s\" is in unrecognized set",
283 if (ReadRegisterSet(set) != 0) {
284 // This is likely an internal register for lldb use only and should not be
286 error.SetErrorStringWithFormat(
287 "reading register set for register \"%s\" failed", reg_info->name);
292 case lldb_rax_x86_64:
293 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RAX];
295 case lldb_rbx_x86_64:
296 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RBX];
298 case lldb_rcx_x86_64:
299 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RCX];
301 case lldb_rdx_x86_64:
302 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RDX];
304 case lldb_rdi_x86_64:
305 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RDI];
307 case lldb_rsi_x86_64:
308 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RSI];
310 case lldb_rbp_x86_64:
311 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RBP];
313 case lldb_rsp_x86_64:
314 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RSP];
317 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R8];
320 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R9];
322 case lldb_r10_x86_64:
323 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R10];
325 case lldb_r11_x86_64:
326 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R11];
328 case lldb_r12_x86_64:
329 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R12];
331 case lldb_r13_x86_64:
332 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R13];
334 case lldb_r14_x86_64:
335 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R14];
337 case lldb_r15_x86_64:
338 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_R15];
340 case lldb_rip_x86_64:
341 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RIP];
343 case lldb_rflags_x86_64:
344 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_RFLAGS];
347 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_CS];
350 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_FS];
353 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_GS];
356 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_SS];
359 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_DS];
362 reg_value = (uint64_t)m_gpr_x86_64.regs[_REG_ES];
364 case lldb_fctrl_x86_64:
365 reg_value = (uint16_t)m_fpr_x86_64.fxstate.fx_cw;
367 case lldb_fstat_x86_64:
368 reg_value = (uint16_t)m_fpr_x86_64.fxstate.fx_sw;
370 case lldb_ftag_x86_64:
371 reg_value = (uint8_t)m_fpr_x86_64.fxstate.fx_tw;
373 case lldb_fop_x86_64:
374 reg_value = (uint64_t)m_fpr_x86_64.fxstate.fx_opcode;
376 case lldb_fiseg_x86_64:
377 reg_value = (uint64_t)m_fpr_x86_64.fxstate.fx_ip.fa_64;
379 case lldb_fioff_x86_64:
380 reg_value = (uint32_t)m_fpr_x86_64.fxstate.fx_ip.fa_32.fa_off;
382 case lldb_foseg_x86_64:
383 reg_value = (uint64_t)m_fpr_x86_64.fxstate.fx_dp.fa_64;
385 case lldb_fooff_x86_64:
386 reg_value = (uint32_t)m_fpr_x86_64.fxstate.fx_dp.fa_32.fa_off;
388 case lldb_mxcsr_x86_64:
389 reg_value = (uint32_t)m_fpr_x86_64.fxstate.fx_mxcsr;
391 case lldb_mxcsrmask_x86_64:
392 reg_value = (uint32_t)m_fpr_x86_64.fxstate.fx_mxcsr_mask;
394 case lldb_st0_x86_64:
395 case lldb_st1_x86_64:
396 case lldb_st2_x86_64:
397 case lldb_st3_x86_64:
398 case lldb_st4_x86_64:
399 case lldb_st5_x86_64:
400 case lldb_st6_x86_64:
401 case lldb_st7_x86_64:
402 reg_value.SetBytes(&m_fpr_x86_64.fxstate.fx_87_ac[reg - lldb_st0_x86_64],
403 reg_info->byte_size, endian::InlHostByteOrder());
405 case lldb_mm0_x86_64:
406 case lldb_mm1_x86_64:
407 case lldb_mm2_x86_64:
408 case lldb_mm3_x86_64:
409 case lldb_mm4_x86_64:
410 case lldb_mm5_x86_64:
411 case lldb_mm6_x86_64:
412 case lldb_mm7_x86_64:
413 reg_value.SetBytes(&m_fpr_x86_64.fxstate.fx_xmm[reg - lldb_mm0_x86_64],
414 reg_info->byte_size, endian::InlHostByteOrder());
416 case lldb_xmm0_x86_64:
417 case lldb_xmm1_x86_64:
418 case lldb_xmm2_x86_64:
419 case lldb_xmm3_x86_64:
420 case lldb_xmm4_x86_64:
421 case lldb_xmm5_x86_64:
422 case lldb_xmm6_x86_64:
423 case lldb_xmm7_x86_64:
424 case lldb_xmm8_x86_64:
425 case lldb_xmm9_x86_64:
426 case lldb_xmm10_x86_64:
427 case lldb_xmm11_x86_64:
428 case lldb_xmm12_x86_64:
429 case lldb_xmm13_x86_64:
430 case lldb_xmm14_x86_64:
431 case lldb_xmm15_x86_64:
432 reg_value.SetBytes(&m_fpr_x86_64.fxstate.fx_xmm[reg - lldb_xmm0_x86_64],
433 reg_info->byte_size, endian::InlHostByteOrder());
435 case lldb_dr0_x86_64:
436 case lldb_dr1_x86_64:
437 case lldb_dr2_x86_64:
438 case lldb_dr3_x86_64:
439 case lldb_dr4_x86_64:
440 case lldb_dr5_x86_64:
441 case lldb_dr6_x86_64:
442 case lldb_dr7_x86_64:
443 reg_value = (uint64_t)m_dbr_x86_64.dr[reg - lldb_dr0_x86_64];
450 Status NativeRegisterContextNetBSD_x86_64::WriteRegister(
451 const RegisterInfo *reg_info, const RegisterValue ®_value) {
456 error.SetErrorString("reg_info NULL");
460 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
461 if (reg == LLDB_INVALID_REGNUM) {
462 // This is likely an internal register for lldb use only and should not be
464 error.SetErrorStringWithFormat("register \"%s\" is an internal-only lldb "
465 "register, cannot read directly",
470 int set = GetSetForNativeRegNum(reg);
472 // This is likely an internal register for lldb use only and should not be
474 error.SetErrorStringWithFormat("register \"%s\" is in unrecognized set",
479 if (ReadRegisterSet(set) != 0) {
480 // This is likely an internal register for lldb use only and should not be
482 error.SetErrorStringWithFormat(
483 "reading register set for register \"%s\" failed", reg_info->name);
488 case lldb_rax_x86_64:
489 m_gpr_x86_64.regs[_REG_RAX] = reg_value.GetAsUInt64();
491 case lldb_rbx_x86_64:
492 m_gpr_x86_64.regs[_REG_RBX] = reg_value.GetAsUInt64();
494 case lldb_rcx_x86_64:
495 m_gpr_x86_64.regs[_REG_RCX] = reg_value.GetAsUInt64();
497 case lldb_rdx_x86_64:
498 m_gpr_x86_64.regs[_REG_RDX] = reg_value.GetAsUInt64();
500 case lldb_rdi_x86_64:
501 m_gpr_x86_64.regs[_REG_RDI] = reg_value.GetAsUInt64();
503 case lldb_rsi_x86_64:
504 m_gpr_x86_64.regs[_REG_RSI] = reg_value.GetAsUInt64();
506 case lldb_rbp_x86_64:
507 m_gpr_x86_64.regs[_REG_RBP] = reg_value.GetAsUInt64();
509 case lldb_rsp_x86_64:
510 m_gpr_x86_64.regs[_REG_RSP] = reg_value.GetAsUInt64();
513 m_gpr_x86_64.regs[_REG_R8] = reg_value.GetAsUInt64();
516 m_gpr_x86_64.regs[_REG_R9] = reg_value.GetAsUInt64();
518 case lldb_r10_x86_64:
519 m_gpr_x86_64.regs[_REG_R10] = reg_value.GetAsUInt64();
521 case lldb_r11_x86_64:
522 m_gpr_x86_64.regs[_REG_R11] = reg_value.GetAsUInt64();
524 case lldb_r12_x86_64:
525 m_gpr_x86_64.regs[_REG_R12] = reg_value.GetAsUInt64();
527 case lldb_r13_x86_64:
528 m_gpr_x86_64.regs[_REG_R13] = reg_value.GetAsUInt64();
530 case lldb_r14_x86_64:
531 m_gpr_x86_64.regs[_REG_R14] = reg_value.GetAsUInt64();
533 case lldb_r15_x86_64:
534 m_gpr_x86_64.regs[_REG_R15] = reg_value.GetAsUInt64();
536 case lldb_rip_x86_64:
537 m_gpr_x86_64.regs[_REG_RIP] = reg_value.GetAsUInt64();
539 case lldb_rflags_x86_64:
540 m_gpr_x86_64.regs[_REG_RFLAGS] = reg_value.GetAsUInt64();
543 m_gpr_x86_64.regs[_REG_CS] = reg_value.GetAsUInt64();
546 m_gpr_x86_64.regs[_REG_FS] = reg_value.GetAsUInt64();
549 m_gpr_x86_64.regs[_REG_GS] = reg_value.GetAsUInt64();
552 m_gpr_x86_64.regs[_REG_SS] = reg_value.GetAsUInt64();
555 m_gpr_x86_64.regs[_REG_DS] = reg_value.GetAsUInt64();
558 m_gpr_x86_64.regs[_REG_ES] = reg_value.GetAsUInt64();
560 case lldb_fctrl_x86_64:
561 m_fpr_x86_64.fxstate.fx_cw = reg_value.GetAsUInt16();
563 case lldb_fstat_x86_64:
564 m_fpr_x86_64.fxstate.fx_sw = reg_value.GetAsUInt16();
566 case lldb_ftag_x86_64:
567 m_fpr_x86_64.fxstate.fx_tw = reg_value.GetAsUInt8();
569 case lldb_fop_x86_64:
570 m_fpr_x86_64.fxstate.fx_opcode = reg_value.GetAsUInt16();
572 case lldb_fiseg_x86_64:
573 m_fpr_x86_64.fxstate.fx_ip.fa_64 = reg_value.GetAsUInt64();
575 case lldb_fioff_x86_64:
576 m_fpr_x86_64.fxstate.fx_ip.fa_32.fa_off = reg_value.GetAsUInt32();
578 case lldb_foseg_x86_64:
579 m_fpr_x86_64.fxstate.fx_dp.fa_64 = reg_value.GetAsUInt64();
581 case lldb_fooff_x86_64:
582 m_fpr_x86_64.fxstate.fx_dp.fa_32.fa_off = reg_value.GetAsUInt32();
584 case lldb_mxcsr_x86_64:
585 m_fpr_x86_64.fxstate.fx_mxcsr = reg_value.GetAsUInt32();
587 case lldb_mxcsrmask_x86_64:
588 m_fpr_x86_64.fxstate.fx_mxcsr_mask = reg_value.GetAsUInt32();
590 case lldb_st0_x86_64:
591 case lldb_st1_x86_64:
592 case lldb_st2_x86_64:
593 case lldb_st3_x86_64:
594 case lldb_st4_x86_64:
595 case lldb_st5_x86_64:
596 case lldb_st6_x86_64:
597 case lldb_st7_x86_64:
598 ::memcpy(&m_fpr_x86_64.fxstate.fx_87_ac[reg - lldb_st0_x86_64],
599 reg_value.GetBytes(), reg_value.GetByteSize());
601 case lldb_mm0_x86_64:
602 case lldb_mm1_x86_64:
603 case lldb_mm2_x86_64:
604 case lldb_mm3_x86_64:
605 case lldb_mm4_x86_64:
606 case lldb_mm5_x86_64:
607 case lldb_mm6_x86_64:
608 case lldb_mm7_x86_64:
609 ::memcpy(&m_fpr_x86_64.fxstate.fx_xmm[reg - lldb_mm0_x86_64],
610 reg_value.GetBytes(), reg_value.GetByteSize());
612 case lldb_xmm0_x86_64:
613 case lldb_xmm1_x86_64:
614 case lldb_xmm2_x86_64:
615 case lldb_xmm3_x86_64:
616 case lldb_xmm4_x86_64:
617 case lldb_xmm5_x86_64:
618 case lldb_xmm6_x86_64:
619 case lldb_xmm7_x86_64:
620 case lldb_xmm8_x86_64:
621 case lldb_xmm9_x86_64:
622 case lldb_xmm10_x86_64:
623 case lldb_xmm11_x86_64:
624 case lldb_xmm12_x86_64:
625 case lldb_xmm13_x86_64:
626 case lldb_xmm14_x86_64:
627 case lldb_xmm15_x86_64:
628 ::memcpy(&m_fpr_x86_64.fxstate.fx_xmm[reg - lldb_xmm0_x86_64],
629 reg_value.GetBytes(), reg_value.GetByteSize());
631 case lldb_dr0_x86_64:
632 case lldb_dr1_x86_64:
633 case lldb_dr2_x86_64:
634 case lldb_dr3_x86_64:
635 case lldb_dr4_x86_64:
636 case lldb_dr5_x86_64:
637 case lldb_dr6_x86_64:
638 case lldb_dr7_x86_64:
639 m_dbr_x86_64.dr[reg - lldb_dr0_x86_64] = reg_value.GetAsUInt64();
643 if (WriteRegisterSet(set) != 0)
644 error.SetErrorStringWithFormat("failed to write register set");
649 Status NativeRegisterContextNetBSD_x86_64::ReadAllRegisterValues(
650 lldb::DataBufferSP &data_sp) {
653 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
655 error.SetErrorStringWithFormat(
656 "failed to allocate DataBufferHeap instance of size %" PRIu64,
665 uint8_t *dst = data_sp->GetBytes();
666 if (dst == nullptr) {
667 error.SetErrorStringWithFormat("DataBufferHeap instance of size %" PRIu64
668 " returned a null pointer",
673 ::memcpy(dst, &m_gpr_x86_64, GetRegisterInfoInterface().GetGPRSize());
674 dst += GetRegisterInfoInterface().GetGPRSize();
676 RegisterValue value((uint64_t)-1);
677 const RegisterInfo *reg_info =
678 GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax");
679 if (reg_info == nullptr)
680 reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax");
684 Status NativeRegisterContextNetBSD_x86_64::WriteAllRegisterValues(
685 const lldb::DataBufferSP &data_sp) {
689 error.SetErrorStringWithFormat(
690 "NativeRegisterContextNetBSD_x86_64::%s invalid data_sp provided",
695 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
696 error.SetErrorStringWithFormat(
697 "NativeRegisterContextNetBSD_x86_64::%s data_sp contained mismatched "
698 "data size, expected %" PRIu64 ", actual %" PRIu64,
699 __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize());
703 uint8_t *src = data_sp->GetBytes();
704 if (src == nullptr) {
705 error.SetErrorStringWithFormat("NativeRegisterContextNetBSD_x86_64::%s "
706 "DataBuffer::GetBytes() returned a null "
711 ::memcpy(&m_gpr_x86_64, src, GetRegisterInfoInterface().GetGPRSize());
716 src += GetRegisterInfoInterface().GetGPRSize();
721 Status NativeRegisterContextNetBSD_x86_64::IsWatchpointHit(uint32_t wp_index,
723 if (wp_index >= NumSupportedHardwareWatchpoints())
724 return Status("Watchpoint index out of range");
726 RegisterValue reg_value;
727 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(lldb_dr6_x86_64);
728 Status error = ReadRegister(reg_info, reg_value);
734 uint64_t status_bits = reg_value.GetAsUInt64();
736 is_hit = status_bits & (1 << wp_index);
741 Status NativeRegisterContextNetBSD_x86_64::GetWatchpointHitIndex(
742 uint32_t &wp_index, lldb::addr_t trap_addr) {
743 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
744 for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {
746 Status error = IsWatchpointHit(wp_index, is_hit);
748 wp_index = LLDB_INVALID_INDEX32;
754 wp_index = LLDB_INVALID_INDEX32;
758 Status NativeRegisterContextNetBSD_x86_64::IsWatchpointVacant(uint32_t wp_index,
760 if (wp_index >= NumSupportedHardwareWatchpoints())
761 return Status("Watchpoint index out of range");
763 RegisterValue reg_value;
764 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(lldb_dr7_x86_64);
765 Status error = ReadRegister(reg_info, reg_value);
771 uint64_t control_bits = reg_value.GetAsUInt64();
773 is_vacant = !(control_bits & (1 << (2 * wp_index)));
778 Status NativeRegisterContextNetBSD_x86_64::SetHardwareWatchpointWithIndex(
779 lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {
781 if (wp_index >= NumSupportedHardwareWatchpoints())
782 return Status("Watchpoint index out of range");
784 // Read only watchpoints aren't supported on x86_64. Fall back to read/write
785 // waitchpoints instead.
786 // TODO: Add logic to detect when a write happens and ignore that watchpoint
788 if (watch_flags == 0x2)
791 if (watch_flags != 0x1 && watch_flags != 0x3)
792 return Status("Invalid read/write bits for watchpoint");
794 if (size != 1 && size != 2 && size != 4 && size != 8)
795 return Status("Invalid size for watchpoint");
798 Status error = IsWatchpointVacant(wp_index, is_vacant);
802 return Status("Watchpoint index not vacant");
804 RegisterValue reg_value;
805 const RegisterInfo *const reg_info_dr7 =
806 GetRegisterInfoAtIndex(lldb_dr7_x86_64);
807 error = ReadRegister(reg_info_dr7, reg_value);
811 // for watchpoints 0, 1, 2, or 3, respectively,
812 // set bits 1, 3, 5, or 7
813 uint64_t enable_bit = 1 << (2 * wp_index);
815 // set bits 16-17, 20-21, 24-25, or 28-29
816 // with 0b01 for write, and 0b11 for read/write
817 uint64_t rw_bits = watch_flags << (16 + 4 * wp_index);
819 // set bits 18-19, 22-23, 26-27, or 30-31
820 // with 0b00, 0b01, 0b10, or 0b11
821 // for 1, 2, 8 (if supported), or 4 bytes, respectively
822 uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
824 uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
826 uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
828 control_bits |= enable_bit | rw_bits | size_bits;
830 const RegisterInfo *const reg_info_drN =
831 GetRegisterInfoAtIndex(lldb_dr0_x86_64 + wp_index);
832 error = WriteRegister(reg_info_drN, RegisterValue(addr));
836 error = WriteRegister(reg_info_dr7, RegisterValue(control_bits));
844 bool NativeRegisterContextNetBSD_x86_64::ClearHardwareWatchpoint(
846 if (wp_index >= NumSupportedHardwareWatchpoints())
849 RegisterValue reg_value;
851 // for watchpoints 0, 1, 2, or 3, respectively,
852 // clear bits 0, 1, 2, or 3 of the debug status register (DR6)
853 const RegisterInfo *const reg_info_dr6 =
854 GetRegisterInfoAtIndex(lldb_dr6_x86_64);
855 Status error = ReadRegister(reg_info_dr6, reg_value);
858 uint64_t bit_mask = 1 << wp_index;
859 uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
860 error = WriteRegister(reg_info_dr6, RegisterValue(status_bits));
864 // for watchpoints 0, 1, 2, or 3, respectively,
865 // clear bits {0-1,16-19}, {2-3,20-23}, {4-5,24-27}, or {6-7,28-31}
866 // of the debug control register (DR7)
867 const RegisterInfo *const reg_info_dr7 =
868 GetRegisterInfoAtIndex(lldb_dr7_x86_64);
869 error = ReadRegister(reg_info_dr7, reg_value);
872 bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index));
873 uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
874 return WriteRegister(reg_info_dr7, RegisterValue(control_bits)).Success();
877 Status NativeRegisterContextNetBSD_x86_64::ClearAllHardwareWatchpoints() {
878 RegisterValue reg_value;
880 // clear bits {0-4} of the debug status register (DR6)
881 const RegisterInfo *const reg_info_dr6 =
882 GetRegisterInfoAtIndex(lldb_dr6_x86_64);
883 Status error = ReadRegister(reg_info_dr6, reg_value);
886 uint64_t bit_mask = 0xF;
887 uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask;
888 error = WriteRegister(reg_info_dr6, RegisterValue(status_bits));
892 // clear bits {0-7,16-31} of the debug control register (DR7)
893 const RegisterInfo *const reg_info_dr7 =
894 GetRegisterInfoAtIndex(lldb_dr7_x86_64);
895 error = ReadRegister(reg_info_dr7, reg_value);
898 bit_mask = 0xFF | (0xFFFF << 16);
899 uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask;
900 return WriteRegister(reg_info_dr7, RegisterValue(control_bits));
903 uint32_t NativeRegisterContextNetBSD_x86_64::SetHardwareWatchpoint(
904 lldb::addr_t addr, size_t size, uint32_t watch_flags) {
905 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
906 const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
907 for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {
909 Status error = IsWatchpointVacant(wp_index, is_vacant);
911 error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);
915 if (error.Fail() && log) {
916 log->Printf("NativeRegisterContextNetBSD_x86_64::%s Error: %s",
917 __FUNCTION__, error.AsCString());
920 return LLDB_INVALID_INDEX32;
924 NativeRegisterContextNetBSD_x86_64::GetWatchpointAddress(uint32_t wp_index) {
925 if (wp_index >= NumSupportedHardwareWatchpoints())
926 return LLDB_INVALID_ADDRESS;
927 RegisterValue reg_value;
928 const RegisterInfo *const reg_info_drN =
929 GetRegisterInfoAtIndex(lldb_dr0_x86_64 + wp_index);
930 if (ReadRegister(reg_info_drN, reg_value).Fail())
931 return LLDB_INVALID_ADDRESS;
932 return reg_value.GetAsUInt64();
935 uint32_t NativeRegisterContextNetBSD_x86_64::NumSupportedHardwareWatchpoints() {
936 // Available debug address registers: dr0, dr1, dr2, dr3
940 #endif // defined(__x86_64__)