Skip to content

Commit 4ceee5c

Browse files
kettenismordak
authored andcommitted
Implement PAC support.
ok patrick@
1 parent 39918db commit 4ceee5c

File tree

4 files changed

+88
-82
lines changed

4 files changed

+88
-82
lines changed

lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,43 @@ static lldb::addr_t ReadLinuxProcessAddressMask(lldb::ProcessSP process_sp,
820820
return address_mask;
821821
}
822822

823+
// Reads code or data address mask for the current OpenBSD process.
824+
static lldb::addr_t ReadOpenBSDProcessAddressMask(lldb::ProcessSP process_sp,
825+
llvm::StringRef reg_name) {
826+
// We set default value of mask such that no bits are masked out.
827+
uint64_t address_mask = 0ULL;
828+
// If Pointer Authentication feature is enabled then OpenBSD exposes
829+
// PAC data and code mask register. Try reading relevant register
830+
// below and merge it with default address mask calculated above.
831+
lldb::ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread();
832+
if (thread_sp) {
833+
lldb::RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
834+
if (reg_ctx_sp) {
835+
const RegisterInfo *reg_info =
836+
reg_ctx_sp->GetRegisterInfoByName(reg_name, 0);
837+
if (reg_info) {
838+
lldb::addr_t mask_reg_val = reg_ctx_sp->ReadRegisterAsUnsigned(
839+
reg_info->kinds[eRegisterKindLLDB], LLDB_INVALID_ADDRESS);
840+
if (mask_reg_val != LLDB_INVALID_ADDRESS)
841+
address_mask |= mask_reg_val;
842+
}
843+
}
844+
}
845+
return address_mask;
846+
}
847+
823848
lldb::addr_t ABISysV_arm64::FixCodeAddress(lldb::addr_t pc) {
824849
if (lldb::ProcessSP process_sp = GetProcessSP()) {
825850
if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSLinux() &&
826851
!process_sp->GetCodeAddressMask())
827852
process_sp->SetCodeAddressMask(
828853
ReadLinuxProcessAddressMask(process_sp, "code_mask"));
829854

855+
if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSOpenBSD() &&
856+
!process_sp->GetCodeAddressMask())
857+
process_sp->SetCodeAddressMask(
858+
ReadOpenBSDProcessAddressMask(process_sp, "code_mask"));
859+
830860
return FixAddress(pc, process_sp->GetCodeAddressMask());
831861
}
832862
return pc;
@@ -839,6 +869,11 @@ lldb::addr_t ABISysV_arm64::FixDataAddress(lldb::addr_t pc) {
839869
process_sp->SetDataAddressMask(
840870
ReadLinuxProcessAddressMask(process_sp, "data_mask"));
841871

872+
if (process_sp->GetTarget().GetArchitecture().GetTriple().isOSOpenBSD() &&
873+
!process_sp->GetDataAddressMask())
874+
process_sp->SetDataAddressMask(
875+
ReadOpenBSDProcessAddressMask(process_sp, "data_mask"));
876+
842877
return FixAddress(pc, process_sp->GetDataAddressMask());
843878
}
844879
return pc;

lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_arm64.cpp

Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// clang-format off
2929
#include <sys/types.h>
30+
#include <sys/ptrace.h>
3031
#include <sys/sysctl.h>
3132
#include <sys/time.h>
3233
#include <machine/cpu.h>
@@ -37,77 +38,6 @@ using namespace lldb_private::process_openbsd;
3738

3839
#define REG_CONTEXT_SIZE (GetGPRSize() + GetFPRSize())
3940

40-
// ARM64 general purpose registers.
41-
static const uint32_t g_gpr_regnums_arm64[] = {
42-
gpr_x0_arm64, gpr_x1_arm64, gpr_x2_arm64, gpr_x3_arm64,
43-
gpr_x4_arm64, gpr_x5_arm64, gpr_x6_arm64, gpr_x7_arm64,
44-
gpr_x8_arm64, gpr_x9_arm64, gpr_x10_arm64, gpr_x11_arm64,
45-
gpr_x12_arm64, gpr_x13_arm64, gpr_x14_arm64, gpr_x15_arm64,
46-
gpr_x16_arm64, gpr_x17_arm64, gpr_x18_arm64, gpr_x19_arm64,
47-
gpr_x20_arm64, gpr_x21_arm64, gpr_x22_arm64, gpr_x23_arm64,
48-
gpr_x24_arm64, gpr_x25_arm64, gpr_x26_arm64, gpr_x27_arm64,
49-
gpr_x28_arm64, gpr_fp_arm64, gpr_lr_arm64, gpr_sp_arm64,
50-
gpr_pc_arm64, gpr_cpsr_arm64, gpr_w0_arm64, gpr_w1_arm64,
51-
gpr_w2_arm64, gpr_w3_arm64, gpr_w4_arm64, gpr_w5_arm64,
52-
gpr_w6_arm64, gpr_w7_arm64, gpr_w8_arm64, gpr_w9_arm64,
53-
gpr_w10_arm64, gpr_w11_arm64, gpr_w12_arm64, gpr_w13_arm64,
54-
gpr_w14_arm64, gpr_w15_arm64, gpr_w16_arm64, gpr_w17_arm64,
55-
gpr_w18_arm64, gpr_w19_arm64, gpr_w20_arm64, gpr_w21_arm64,
56-
gpr_w22_arm64, gpr_w23_arm64, gpr_w24_arm64, gpr_w25_arm64,
57-
gpr_w26_arm64, gpr_w27_arm64, gpr_w28_arm64,
58-
LLDB_INVALID_REGNUM // register sets need to end with this flag
59-
};
60-
static_assert(((sizeof g_gpr_regnums_arm64 / sizeof g_gpr_regnums_arm64[0]) -
61-
1) == k_num_gpr_registers_arm64,
62-
"g_gpr_regnums_arm64 has wrong number of register infos");
63-
64-
// ARM64 floating point registers.
65-
static const uint32_t g_fpu_regnums_arm64[] = {
66-
fpu_v0_arm64, fpu_v1_arm64, fpu_v2_arm64, fpu_v3_arm64,
67-
fpu_v4_arm64, fpu_v5_arm64, fpu_v6_arm64, fpu_v7_arm64,
68-
fpu_v8_arm64, fpu_v9_arm64, fpu_v10_arm64, fpu_v11_arm64,
69-
fpu_v12_arm64, fpu_v13_arm64, fpu_v14_arm64, fpu_v15_arm64,
70-
fpu_v16_arm64, fpu_v17_arm64, fpu_v18_arm64, fpu_v19_arm64,
71-
fpu_v20_arm64, fpu_v21_arm64, fpu_v22_arm64, fpu_v23_arm64,
72-
fpu_v24_arm64, fpu_v25_arm64, fpu_v26_arm64, fpu_v27_arm64,
73-
fpu_v28_arm64, fpu_v29_arm64, fpu_v30_arm64, fpu_v31_arm64,
74-
75-
fpu_s0_arm64, fpu_s1_arm64, fpu_s2_arm64, fpu_s3_arm64,
76-
fpu_s4_arm64, fpu_s5_arm64, fpu_s6_arm64, fpu_s7_arm64,
77-
fpu_s8_arm64, fpu_s9_arm64, fpu_s10_arm64, fpu_s11_arm64,
78-
fpu_s12_arm64, fpu_s13_arm64, fpu_s14_arm64, fpu_s15_arm64,
79-
fpu_s16_arm64, fpu_s17_arm64, fpu_s18_arm64, fpu_s19_arm64,
80-
fpu_s20_arm64, fpu_s21_arm64, fpu_s22_arm64, fpu_s23_arm64,
81-
fpu_s24_arm64, fpu_s25_arm64, fpu_s26_arm64, fpu_s27_arm64,
82-
fpu_s28_arm64, fpu_s29_arm64, fpu_s30_arm64, fpu_s31_arm64,
83-
84-
fpu_d0_arm64, fpu_d1_arm64, fpu_d2_arm64, fpu_d3_arm64,
85-
fpu_d4_arm64, fpu_d5_arm64, fpu_d6_arm64, fpu_d7_arm64,
86-
fpu_d8_arm64, fpu_d9_arm64, fpu_d10_arm64, fpu_d11_arm64,
87-
fpu_d12_arm64, fpu_d13_arm64, fpu_d14_arm64, fpu_d15_arm64,
88-
fpu_d16_arm64, fpu_d17_arm64, fpu_d18_arm64, fpu_d19_arm64,
89-
fpu_d20_arm64, fpu_d21_arm64, fpu_d22_arm64, fpu_d23_arm64,
90-
fpu_d24_arm64, fpu_d25_arm64, fpu_d26_arm64, fpu_d27_arm64,
91-
fpu_d28_arm64, fpu_d29_arm64, fpu_d30_arm64, fpu_d31_arm64,
92-
fpu_fpsr_arm64, fpu_fpcr_arm64,
93-
LLDB_INVALID_REGNUM // register sets need to end with this flag
94-
};
95-
static_assert(((sizeof g_fpu_regnums_arm64 / sizeof g_fpu_regnums_arm64[0]) -
96-
1) == k_num_fpr_registers_arm64,
97-
"g_fpu_regnums_arm64 has wrong number of register infos");
98-
99-
namespace {
100-
// Number of register sets provided by this context.
101-
enum { k_num_register_sets = 2 };
102-
}
103-
104-
// Register sets for ARM64.
105-
static const RegisterSet g_reg_sets_arm64[k_num_register_sets] = {
106-
{"General Purpose Registers", "gpr", k_num_gpr_registers_arm64,
107-
g_gpr_regnums_arm64},
108-
{"Floating Point Registers", "fpu", k_num_fpr_registers_arm64,
109-
g_fpu_regnums_arm64}};
110-
11141
std::unique_ptr<NativeRegisterContextOpenBSD>
11242
NativeRegisterContextOpenBSD::CreateHostNativeRegisterContextOpenBSD(
11343
const ArchSpec &target_arch, NativeThreadProtocol &native_thread) {
@@ -122,7 +52,9 @@ static RegisterInfoInterface *
12252
CreateRegisterInfoInterface(const ArchSpec &target_arch) {
12353
assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
12454
"Register setting path assumes this is a 64-bit host");
125-
return new RegisterInfoPOSIX_arm64(target_arch, 0);
55+
56+
Flags opt_regsets = RegisterInfoPOSIX_arm64::eRegsetMaskPAuth;
57+
return new RegisterInfoPOSIX_arm64(target_arch, opt_regsets);
12658
}
12759

12860
static llvm::APInt uint128ToAPInt(__uint128_t in) {
@@ -145,23 +77,25 @@ NativeRegisterContextOpenBSD_arm64::NativeRegisterContextOpenBSD_arm64(
14577
CreateRegisterInfoInterface(target_arch)),
14678
m_gpr(), m_fpr() {}
14779

148-
uint32_t NativeRegisterContextOpenBSD_arm64::GetUserRegisterCount() const {
149-
uint32_t count = 0;
150-
for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index)
151-
count += g_reg_sets_arm64[set_index].num_registers;
152-
return count;
80+
RegisterInfoPOSIX_arm64 &
81+
NativeRegisterContextOpenBSD_arm64::GetRegisterInfo() const {
82+
return static_cast<RegisterInfoPOSIX_arm64 &>(*m_register_info_interface_up);
15383
}
15484

15585
uint32_t NativeRegisterContextOpenBSD_arm64::GetRegisterSetCount() const {
156-
return k_num_register_sets;
86+
return GetRegisterInfo().GetRegisterSetCount();
15787
}
15888

15989
const RegisterSet *
16090
NativeRegisterContextOpenBSD_arm64::GetRegisterSet(uint32_t set_index) const {
161-
if (set_index < k_num_register_sets)
162-
return &g_reg_sets_arm64[set_index];
91+
return GetRegisterInfo().GetRegisterSet(set_index);
92+
}
16393

164-
return nullptr;
94+
uint32_t NativeRegisterContextOpenBSD_arm64::GetUserRegisterCount() const {
95+
uint32_t count = 0;
96+
for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
97+
count += GetRegisterSet(set_index)->num_registers;
98+
return count;
16599
}
166100

167101
Status
@@ -202,6 +136,18 @@ NativeRegisterContextOpenBSD_arm64::ReadRegister(const RegisterInfo *reg_info,
202136
return error;
203137
}
204138

139+
if (GetRegisterInfo().IsPAuthReg(reg)) {
140+
uint32_t offset;
141+
142+
offset = reg_info->byte_offset - GetRegisterInfo().GetPAuthOffset();
143+
reg_value = (uint64_t)m_pacmask[offset > 0];
144+
if (reg_value.GetByteSize() > reg_info->byte_size) {
145+
reg_value.SetType(reg_info);
146+
}
147+
148+
return error;
149+
}
150+
205151
switch (reg) {
206152
case gpr_x0_arm64:
207153
case gpr_x1_arm64:
@@ -527,6 +473,8 @@ int NativeRegisterContextOpenBSD_arm64::GetSetForNativeRegNum(
527473
return GPRegSet;
528474
else if (reg_num >= k_first_fpr_arm64 && reg_num <= k_last_fpr_arm64)
529475
return FPRegSet;
476+
else if (GetRegisterInfo().IsPAuthReg(reg_num))
477+
return PACMaskRegSet;
530478
else
531479
return -1;
532480
}
@@ -539,6 +487,9 @@ int NativeRegisterContextOpenBSD_arm64::ReadRegisterSet(uint32_t set) {
539487
case FPRegSet:
540488
ReadFPR();
541489
return 0;
490+
case PACMaskRegSet:
491+
ReadPACMask();
492+
return 0;
542493
default:
543494
break;
544495
}
@@ -558,4 +509,16 @@ int NativeRegisterContextOpenBSD_arm64::WriteRegisterSet(uint32_t set) {
558509
}
559510
return -1;
560511
}
512+
513+
Status NativeRegisterContextOpenBSD_arm64::ReadPACMask() {
514+
#ifdef PT_PACMASK
515+
return NativeProcessOpenBSD::PtraceWrapper(PT_PACMASK, GetProcessPid(),
516+
&m_pacmask, sizeof(m_pacmask));
517+
#else
518+
Status error;
519+
::memset(&m_pacmask, 0, sizeof(m_pacmask));
520+
return error;
521+
#endif
522+
}
523+
561524
#endif

lldb/source/Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD_arm64.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// clang-format on
1717

1818
#include "Plugins/Process/OpenBSD/NativeRegisterContextOpenBSD.h"
19+
#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
1920
#include "Plugins/Process/Utility/lldb-arm64-register-enums.h"
2021

2122
namespace lldb_private {
@@ -65,16 +66,21 @@ class NativeRegisterContextOpenBSD_arm64 : public NativeRegisterContextOpenBSD {
6566

6667
private:
6768
// Private member types.
68-
enum { GPRegSet, FPRegSet };
69+
enum { GPRegSet, FPRegSet, PACMaskRegSet };
6970

7071
// Private member variables.
7172
struct reg m_gpr;
7273
struct fpreg m_fpr;
74+
register_t m_pacmask[2];
7375

7476
int GetSetForNativeRegNum(int reg_num) const;
7577

7678
int ReadRegisterSet(uint32_t set);
7779
int WriteRegisterSet(uint32_t set);
80+
81+
RegisterInfoPOSIX_arm64 &GetRegisterInfo() const;
82+
83+
Status ReadPACMask();
7884
};
7985

8086
} // namespace process_openbsd

lldb/source/Plugins/Process/elf-core/RegisterUtilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum {
6767
NT_AUXV = 11,
6868
NT_REGS = 20,
6969
NT_FPREGS = 21,
70+
NT_PACMASK = 24,
7071
};
7172
}
7273

@@ -121,6 +122,7 @@ constexpr RegsetDesc AARCH64_SVE_Desc[] = {
121122

122123
constexpr RegsetDesc AARCH64_PAC_Desc[] = {
123124
{llvm::Triple::Linux, llvm::Triple::aarch64, llvm::ELF::NT_ARM_PAC_MASK},
125+
{llvm::Triple::OpenBSD, llvm::Triple::aarch64, OPENBSD::NT_PACMASK},
124126
};
125127

126128
constexpr RegsetDesc PPC_VMX_Desc[] = {

0 commit comments

Comments
 (0)