1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
4 * Extracted from probe-finder.c
6 * Written by Masami Hiramatsu <mhiramat@redhat.com>
10 #include <errno.h> /* for EINVAL */
11 #include <string.h> /* for strcmp */
12 #include <linux/ptrace.h> /* for struct pt_regs */
13 #include <linux/kernel.h> /* for offsetof */
14 #include <dwarf-regs.h>
17 * See arch/x86/kernel/ptrace.c.
20 * - Since struct pt_regs is defined differently for user and kernel,
21 * but we want to use 'ax, bx' instead of 'rax, rbx' (which is struct
22 * field name of user's pt_regs), we make REG_OFFSET_NAME to accept
23 * both string name and reg field name.
25 * - Since accessing x86_32's pt_regs from x86_64 building is difficult
26 * and vise versa, we simply fill offset with -1, so
27 * get_arch_regstr() still works but regs_query_register_offset()
29 * The only inconvenience caused by it now is that we are not allowed
30 * to generate BPF prologue for a x86_64 kernel if perf is built for
31 * x86_32. This is really a rare usecase.
33 * - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
34 * the order defined by dwarf.
37 struct pt_regs_offset
{
42 #define REG_OFFSET_END {.name = NULL, .offset = 0}
45 # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
46 # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = -1}
48 # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = -1}
49 # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
52 /* TODO: switching by dwarf address size */
54 static const struct pt_regs_offset x86_32_regoffset_table
[] = {
55 REG_OFFSET_NAME_32("%ax", eax
),
56 REG_OFFSET_NAME_32("%cx", ecx
),
57 REG_OFFSET_NAME_32("%dx", edx
),
58 REG_OFFSET_NAME_32("%bx", ebx
),
59 REG_OFFSET_NAME_32("$stack", esp
), /* Stack address instead of %sp */
60 REG_OFFSET_NAME_32("%bp", ebp
),
61 REG_OFFSET_NAME_32("%si", esi
),
62 REG_OFFSET_NAME_32("%di", edi
),
66 #define regoffset_table x86_32_regoffset_table
68 static const struct pt_regs_offset x86_64_regoffset_table
[] = {
69 REG_OFFSET_NAME_64("%ax", rax
),
70 REG_OFFSET_NAME_64("%dx", rdx
),
71 REG_OFFSET_NAME_64("%cx", rcx
),
72 REG_OFFSET_NAME_64("%bx", rbx
),
73 REG_OFFSET_NAME_64("%si", rsi
),
74 REG_OFFSET_NAME_64("%di", rdi
),
75 REG_OFFSET_NAME_64("%bp", rbp
),
76 REG_OFFSET_NAME_64("%sp", rsp
),
77 REG_OFFSET_NAME_64("%r8", r8
),
78 REG_OFFSET_NAME_64("%r9", r9
),
79 REG_OFFSET_NAME_64("%r10", r10
),
80 REG_OFFSET_NAME_64("%r11", r11
),
81 REG_OFFSET_NAME_64("%r12", r12
),
82 REG_OFFSET_NAME_64("%r13", r13
),
83 REG_OFFSET_NAME_64("%r14", r14
),
84 REG_OFFSET_NAME_64("%r15", r15
),
88 #define regoffset_table x86_64_regoffset_table
91 /* Minus 1 for the ending REG_OFFSET_END */
92 #define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
94 /* Return architecture dependent register string (for kprobe-tracer) */
95 const char *get_arch_regstr(unsigned int n
)
97 return (n
< ARCH_MAX_REGS
) ? regoffset_table
[n
].name
: NULL
;
100 /* Reuse code from arch/x86/kernel/ptrace.c */
102 * regs_query_register_offset() - query register offset from its name
103 * @name: the name of a register
105 * regs_query_register_offset() returns the offset of a register in struct
106 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
108 int regs_query_register_offset(const char *name
)
110 const struct pt_regs_offset
*roff
;
111 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
112 if (!strcmp(roff
->name
, name
))