| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <elf.h> |
| 5 |
|
| 6 |
#include "cfi.h" |
| 7 |
#include "error.h" |
| 8 |
#include "helpers.h" |
| 9 |
#include "platform.h" // IWYU pragma: associated |
| 10 |
#include "program.h" |
| 11 |
#include "register_state.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* The ABI specification can be found at: |
| 15 |
* https://developer.arm.com/architectures/system-architectures/software-standards/abi |
| 16 |
* https://github.com/ARM-software/abi-aa/releases |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "arch_arm_defs.inc" |
| 20 |
|
| 21 |
static const struct drgn_cfi_row default_dwarf_cfi_row_arm = DRGN_CFI_ROW( |
| 22 |
// The psABI defines the CFA as the value of the stack pointer in the |
| 23 |
// calling frame. |
| 24 |
[DRGN_REGISTER_NUMBER(r13)] = { DRGN_CFI_RULE_CFA_PLUS_OFFSET }, |
| 25 |
// The psABI defines that callee-saved registers default to |
| 26 |
// DW_CFA_same_value. |
| 27 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r4)), |
| 28 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r5)), |
| 29 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r6)), |
| 30 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r7)), |
| 31 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r8)), |
| 32 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r9)), |
| 33 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r10)), |
| 34 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r11)), |
| 35 |
DRGN_CFI_SAME_VALUE_INIT(DRGN_REGISTER_NUMBER(r14)), |
| 36 |
); |
| 37 |
|
| 38 |
static struct drgn_error *fallback_unwind_arm(struct drgn_program *prog, |
| 39 |
struct drgn_register_state *regs, |
| 40 |
struct drgn_register_state **ret) |
| 41 |
{ |
| 42 |
// GCC and Clang generate different frame pointer layouts. See "GCC/LLVM |
| 43 |
// frame pointer incompatibility on ARM" [1] and Linux kernel commit |
| 44 |
// b4d5ec9b39f8 ("ARM: 8992/1: Fix unwind_frame for clang-built |
| 45 |
// kernels"). For now, let's not bother with it. |
| 46 |
// |
| 47 |
// 1: https://discourse.llvm.org/t/gcc-llvm-frame-pointer-incompatibility-on-arm/32906 |
| 48 |
return &drgn_stop; |
| 49 |
} |
| 50 |
|
| 51 |
// elf_gregset_t (in PRSTATUS) and struct pt_regs are identical. |
| 52 |
static struct drgn_error * |
| 53 |
get_initial_registers_from_struct_arm(struct drgn_program *prog, |
| 54 |
const void *buf, size_t size, |
| 55 |
struct drgn_register_state **ret) |
| 56 |
{ |
| 57 |
if (size < 68) { |
| 58 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 59 |
"registers are truncated"); |
| 60 |
} |
| 61 |
|
| 62 |
struct drgn_register_state *regs = |
| 63 |
drgn_register_state_create(cpsr, true); |
| 64 |
if (!regs) |
| 65 |
return &drgn_enomem; |
| 66 |
|
| 67 |
drgn_register_state_set_range_from_buffer(regs, r13, r14, |
| 68 |
(uint32_t *)buf + 13); |
| 69 |
drgn_register_state_set_range_from_buffer(regs, r4, r11, |
| 70 |
(uint32_t *)buf + 4); |
| 71 |
drgn_register_state_set_range_from_buffer(regs, r0, r3, buf); |
| 72 |
drgn_register_state_set_from_buffer(regs, r12, (uint32_t *)buf + 12); |
| 73 |
drgn_register_state_set_range_from_buffer(regs, r15, cpsr, |
| 74 |
(uint32_t *)buf + 15); |
| 75 |
drgn_register_state_set_pc_from_register(prog, regs, r15); |
| 76 |
|
| 77 |
*ret = regs; |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
static struct drgn_error * |
| 82 |
pt_regs_get_initial_registers_arm(const struct drgn_object *obj, |
| 83 |
struct drgn_register_state **ret) |
| 84 |
{ |
| 85 |
return get_initial_registers_from_struct_arm(drgn_object_program(obj), |
| 86 |
drgn_object_buffer(obj), |
| 87 |
drgn_object_size(obj), |
| 88 |
ret); |
| 89 |
} |
| 90 |
|
| 91 |
static struct drgn_error * |
| 92 |
prstatus_get_initial_registers_arm(struct drgn_program *prog, |
| 93 |
const void *prstatus, size_t size, |
| 94 |
struct drgn_register_state **ret) |
| 95 |
{ |
| 96 |
// offsetof(struct elf_prstatus, pr_reg) |
| 97 |
static const size_t pr_reg_offset = 72; |
| 98 |
if (size < pr_reg_offset) { |
| 99 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 100 |
"NT_PRSTATUS is truncated"); |
| 101 |
} |
| 102 |
return get_initial_registers_from_struct_arm(prog, |
| 103 |
(char *)prstatus + pr_reg_offset, |
| 104 |
size - pr_reg_offset, ret); |
| 105 |
} |
| 106 |
|
| 107 |
// The Linux kernel saves the callee-saved registers in |
| 108 |
// task_thread_info(task)->cpu_context (with type struct cpu_context_save). See |
| 109 |
// __switch_to() in arch/arm/kernel/entry-armv.S (as of Linux v6.12). |
| 110 |
static struct drgn_error * |
| 111 |
linux_kernel_get_initial_registers_arm(const struct drgn_object *task_obj, |
| 112 |
struct drgn_register_state **ret) |
| 113 |
{ |
| 114 |
struct drgn_error *err; |
| 115 |
struct drgn_program *prog = drgn_object_program(task_obj); |
| 116 |
|
| 117 |
DRGN_OBJECT(cpu_context_obj, prog); |
| 118 |
err = linux_helper_task_thread_info(&cpu_context_obj, task_obj); |
| 119 |
if (err) |
| 120 |
return err; |
| 121 |
err = drgn_object_member_dereference(&cpu_context_obj, &cpu_context_obj, |
| 122 |
"cpu_context"); |
| 123 |
if (err) |
| 124 |
return err; |
| 125 |
if (cpu_context_obj.encoding != DRGN_OBJECT_ENCODING_BUFFER || |
| 126 |
drgn_object_size(&cpu_context_obj) < 40) { |
| 127 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 128 |
"cpu_context is truncated"); |
| 129 |
} |
| 130 |
err = drgn_object_read(&cpu_context_obj, &cpu_context_obj); |
| 131 |
if (err) |
| 132 |
return err; |
| 133 |
|
| 134 |
const void *buf = drgn_object_buffer(&cpu_context_obj); |
| 135 |
struct drgn_register_state *regs = |
| 136 |
drgn_register_state_create(r15, false); |
| 137 |
if (!regs) |
| 138 |
return &drgn_enomem; |
| 139 |
|
| 140 |
drgn_register_state_set_range_from_buffer(regs, r13, r14, |
| 141 |
(uint32_t *)buf + 8); |
| 142 |
drgn_register_state_set_range_from_buffer(regs, r4, r11, buf); |
| 143 |
drgn_register_state_set_pc_from_register(prog, regs, r14); |
| 144 |
*ret = regs; |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
static struct drgn_error * |
| 149 |
apply_elf_reloc_arm(const struct drgn_relocating_section *relocating, |
| 150 |
uint64_t r_offset, uint32_t r_type, const int64_t *r_addend, |
| 151 |
uint64_t sym_value) |
| 152 |
{ |
| 153 |
switch (r_type) { |
| 154 |
case R_ARM_NONE: |
| 155 |
return NULL; |
| 156 |
case R_ARM_ABS32: |
| 157 |
return drgn_reloc_add32(relocating, r_offset, r_addend, |
| 158 |
sym_value); |
| 159 |
case R_ARM_REL32: |
| 160 |
return drgn_reloc_add32(relocating, r_offset, r_addend, |
| 161 |
sym_value |
| 162 |
- (relocating->addr + r_offset)); |
| 163 |
default: |
| 164 |
return DRGN_UNKNOWN_RELOCATION_TYPE(r_type); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
struct pgtable_iterator_arm { |
| 169 |
struct pgtable_iterator it; |
| 170 |
union { |
| 171 |
// For LPAE. |
| 172 |
struct { |
| 173 |
uint64_t cached_entries[3]; |
| 174 |
uint32_t cached_virt_addr; |
| 175 |
}; |
| 176 |
// For non-LPAE. |
| 177 |
struct { |
| 178 |
uint32_t cached_entry; |
| 179 |
uint32_t cached_index; |
| 180 |
}; |
| 181 |
}; |
| 182 |
}; |
| 183 |
|
| 184 |
static struct drgn_error * |
| 185 |
linux_kernel_pgtable_iterator_create_arm(struct drgn_program *prog, |
| 186 |
struct pgtable_iterator **ret) |
| 187 |
{ |
| 188 |
struct pgtable_iterator_arm *it = malloc(sizeof(*it)); |
| 189 |
if (!it) |
| 190 |
return &drgn_enomem; |
| 191 |
*ret = &it->it; |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
static void linux_kernel_pgtable_iterator_destroy_arm(struct pgtable_iterator *it) |
| 196 |
{ |
| 197 |
free(container_of(it, struct pgtable_iterator_arm, it)); |
| 198 |
} |
| 199 |
|
| 200 |
static void linux_kernel_pgtable_iterator_init_arm(struct drgn_program *prog, |
| 201 |
struct pgtable_iterator *_it) |
| 202 |
{ |
| 203 |
struct pgtable_iterator_arm *it = |
| 204 |
container_of(_it, struct pgtable_iterator_arm, it); |
| 205 |
memset(it->cached_entries, 0, sizeof(it->cached_entries)); |
| 206 |
it->cached_virt_addr = 0; |
| 207 |
} |
| 208 |
|
| 209 |
static struct drgn_error * |
| 210 |
linux_kernel_pgtable_iterator_next_arm_lpae(struct drgn_program *prog, |
| 211 |
struct pgtable_iterator_arm *it, |
| 212 |
uint64_t *virt_addr_ret, |
| 213 |
uint64_t *phys_addr_ret) |
| 214 |
{ |
| 215 |
struct drgn_error *err; |
| 216 |
const uint32_t virt_addr = it->it.virt_addr; |
| 217 |
|
| 218 |
const uint64_t phys_addr_mask = 0xfffffff000; |
| 219 |
uint32_t index_mask = 0x3; |
| 220 |
uint64_t table = it->it.pgtable; |
| 221 |
bool table_physical = false; |
| 222 |
for (int level = 2;; level--) { |
| 223 |
int level_shift = 12 + 9 * level; |
| 224 |
uint32_t index = (virt_addr >> level_shift) & index_mask; |
| 225 |
uint32_t cached_index = |
| 226 |
(it->cached_virt_addr >> level_shift) & index_mask; |
| 227 |
uint64_t *entry_ptr = &it->cached_entries[2 - level]; |
| 228 |
if (index != cached_index) |
| 229 |
memset(entry_ptr, 0, (level + 1) * 8); |
| 230 |
if (!*entry_ptr) { |
| 231 |
err = drgn_program_read_u64(prog, table + index * 8, |
| 232 |
table_physical, entry_ptr); |
| 233 |
if (err) |
| 234 |
return err; |
| 235 |
} |
| 236 |
uint64_t entry = *entry_ptr; |
| 237 |
|
| 238 |
index_mask = 0x1ff; |
| 239 |
table = entry & phys_addr_mask; |
| 240 |
table_physical = true; |
| 241 |
|
| 242 |
if (level == 0 || (entry & 0x3) != 0x3) { |
| 243 |
uint64_t mask = (UINT64_C(1) << level_shift) - 1; |
| 244 |
*virt_addr_ret = virt_addr & ~mask; |
| 245 |
if ((entry & 0x3) == (level == 0 ? 0x3 : 0x1)) |
| 246 |
*phys_addr_ret = table & ~mask; |
| 247 |
else |
| 248 |
*phys_addr_ret = UINT64_MAX; |
| 249 |
it->cached_virt_addr = virt_addr; |
| 250 |
it->it.virt_addr = (virt_addr | mask) + 1; |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static struct drgn_error * |
| 257 |
linux_kernel_pgtable_iterator_next_arm(struct drgn_program *prog, |
| 258 |
struct pgtable_iterator *_it, |
| 259 |
uint64_t *virt_addr_ret, |
| 260 |
uint64_t *phys_addr_ret) |
| 261 |
{ |
| 262 |
struct drgn_error *err; |
| 263 |
struct pgtable_iterator_arm *it = |
| 264 |
container_of(_it, struct pgtable_iterator_arm, it); |
| 265 |
|
| 266 |
if (prog->vmcoreinfo.arm_lpae) { |
| 267 |
return linux_kernel_pgtable_iterator_next_arm_lpae(prog, it, |
| 268 |
virt_addr_ret, |
| 269 |
phys_addr_ret); |
| 270 |
} |
| 271 |
|
| 272 |
const uint32_t virt_addr = it->it.virt_addr; |
| 273 |
|
| 274 |
uint32_t index = virt_addr >> 20; |
| 275 |
if (it->cached_index != index || !it->cached_entry) { |
| 276 |
err = drgn_program_read_u32(prog, it->it.pgtable + index * 4, |
| 277 |
false, &it->cached_entry); |
| 278 |
if (err) |
| 279 |
return err; |
| 280 |
it->cached_index = index; |
| 281 |
} |
| 282 |
uint32_t entry = it->cached_entry; |
| 283 |
|
| 284 |
if ((entry & 0x3) != 0x1) { |
| 285 |
uint32_t mask = (UINT32_C(1) << 20) - 1; |
| 286 |
if ((entry & 0x40002) == 0x40002) { |
| 287 |
// Supersection (16MB) |
| 288 |
mask = (UINT32_C(1) << 24) - 1; |
| 289 |
*phys_addr_ret = (entry & ~mask) |
| 290 |
| ((uint64_t)(entry & 0xf00000) << 12); |
| 291 |
} else if ((entry & 0x40002) == 0x2) { |
| 292 |
// Section (1MB) |
| 293 |
*phys_addr_ret = entry & ~mask; |
| 294 |
} else { |
| 295 |
// Invalid |
| 296 |
*phys_addr_ret = UINT64_MAX; |
| 297 |
} |
| 298 |
*virt_addr_ret = virt_addr & ~mask; |
| 299 |
it->it.virt_addr = (virt_addr | mask) + 1; |
| 300 |
return NULL; |
| 301 |
} |
| 302 |
|
| 303 |
uint32_t table = entry & ~((UINT32_C(1) << 10) - 1); |
| 304 |
index = (virt_addr >> 12) & 0xff; |
| 305 |
err = drgn_program_read_u32(prog, table + index * 4, true, &entry); |
| 306 |
if (err) |
| 307 |
return err; |
| 308 |
|
| 309 |
uint32_t mask = (UINT32_C(1) << 12) - 1; |
| 310 |
if ((entry & 0x2)) { |
| 311 |
// Small page (4KB) |
| 312 |
*phys_addr_ret = entry & ~mask; |
| 313 |
} else if ((entry & 0x3) == 0x1) { |
| 314 |
// Large page (16KB) |
| 315 |
mask = (UINT32_C(1) << 16) - 1; |
| 316 |
*phys_addr_ret = entry & ~mask; |
| 317 |
} else { |
| 318 |
*phys_addr_ret = UINT64_MAX; |
| 319 |
} |
| 320 |
*virt_addr_ret = virt_addr & ~mask; |
| 321 |
it->it.virt_addr = (virt_addr | mask) + 1; |
| 322 |
return NULL; |
| 323 |
} |
| 324 |
|
| 325 |
// StrongARM 1100 and RiscPC technically use different values for |
| 326 |
// SECTION_SIZE_BITS and MAX_PHYSMEM_BITS, but it's vanishingly unlikely that |
| 327 |
// drgn will run on those platforms. Ignoring those, these haven't changed since |
| 328 |
// Linux kernel commit db57f88e4ccb ("ARM: 8411/1: Add default SPARSEMEM |
| 329 |
// settings") (in v4.6). |
| 330 |
static int |
| 331 |
linux_kernel_section_size_bits_fallback_arm(struct drgn_program *prog) |
| 332 |
{ |
| 333 |
return 28; |
| 334 |
} |
| 335 |
|
| 336 |
static int linux_kernel_max_physmem_bits_fallback_arm(struct drgn_program *prog) |
| 337 |
{ |
| 338 |
return 36; |
| 339 |
} |
| 340 |
|
| 341 |
const struct drgn_architecture_info arch_info_arm = { |
| 342 |
.name = "Arm", |
| 343 |
.arch = DRGN_ARCH_ARM, |
| 344 |
.default_flags = DRGN_PLATFORM_IS_LITTLE_ENDIAN, |
| 345 |
.scalar_alignment = { 1, 2, 4, 8, 8 }, |
| 346 |
DRGN_ARCHITECTURE_REGISTERS, |
| 347 |
.default_dwarf_cfi_row = &default_dwarf_cfi_row_arm, |
| 348 |
.fallback_unwind = fallback_unwind_arm, |
| 349 |
.pt_regs_get_initial_registers = pt_regs_get_initial_registers_arm, |
| 350 |
.prstatus_get_initial_registers = prstatus_get_initial_registers_arm, |
| 351 |
.linux_kernel_get_initial_registers = |
| 352 |
linux_kernel_get_initial_registers_arm, |
| 353 |
.apply_elf_reloc = apply_elf_reloc_arm, |
| 354 |
.linux_kernel_pgtable_iterator_create = |
| 355 |
linux_kernel_pgtable_iterator_create_arm, |
| 356 |
.linux_kernel_pgtable_iterator_destroy = |
| 357 |
linux_kernel_pgtable_iterator_destroy_arm, |
| 358 |
.linux_kernel_pgtable_iterator_init = |
| 359 |
linux_kernel_pgtable_iterator_init_arm, |
| 360 |
.linux_kernel_pgtable_iterator_next = |
| 361 |
linux_kernel_pgtable_iterator_next_arm, |
| 362 |
.linux_kernel_section_size_bits_fallback = |
| 363 |
linux_kernel_section_size_bits_fallback_arm, |
| 364 |
.linux_kernel_max_physmem_bits_fallback = |
| 365 |
linux_kernel_max_physmem_bits_fallback_arm, |
| 366 |
}; |