1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
4  * Extracted from probe-finder.c
5  *
6  * Written by Masami Hiramatsu <mhiramat@redhat.com>
7  */
8 
9 #include <stddef.h>
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>
15 
16 /*
17  * See arch/x86/kernel/ptrace.c.
18  * Different from it:
19  *
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.
24  *
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()
28  *    returns error.
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.
32  *
33  *  - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
34  *    the order defined by dwarf.
35  */
36 
37 struct pt_regs_offset {
38 	const char *name;
39 	int offset;
40 };
41 
42 #define REG_OFFSET_END {.name = NULL, .offset = 0}
43 
44 #ifdef __x86_64__
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}
47 #else
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)}
50 #endif
51 
52 /* TODO: switching by dwarf address size */
53 #ifndef __x86_64__
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),
63 	REG_OFFSET_END,
64 };
65 
66 #define regoffset_table x86_32_regoffset_table
67 #else
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),
85 	REG_OFFSET_END,
86 };
87 
88 #define regoffset_table x86_64_regoffset_table
89 #endif
90 
91 /* Minus 1 for the ending REG_OFFSET_END */
92 #define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
93 
94 /* Return architecture dependent register string (for kprobe-tracer) */
get_arch_regstr(unsigned int n)95 const char *get_arch_regstr(unsigned int n)
96 {
97 	return (n < ARCH_MAX_REGS) ? regoffset_table[n].name : NULL;
98 }
99 
100 /* Reuse code from arch/x86/kernel/ptrace.c */
101 /**
102  * regs_query_register_offset() - query register offset from its name
103  * @name:	the name of a register
104  *
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;
107  */
regs_query_register_offset(const char * name)108 int regs_query_register_offset(const char *name)
109 {
110 	const struct pt_regs_offset *roff;
111 	for (roff = regoffset_table; roff->name != NULL; roff++)
112 		if (!strcmp(roff->name, name))
113 			return roff->offset;
114 	return -EINVAL;
115 }
116 
117 struct dwarf_regs_idx {
118 	const char *name;
119 	int idx;
120 };
121 
122 static const struct dwarf_regs_idx x86_regidx_table[] = {
123 	{ "rax", 0 }, { "eax", 0 }, { "ax", 0 }, { "al", 0 },
124 	{ "rdx", 1 }, { "edx", 1 }, { "dx", 1 }, { "dl", 1 },
125 	{ "rcx", 2 }, { "ecx", 2 }, { "cx", 2 }, { "cl", 2 },
126 	{ "rbx", 3 }, { "edx", 3 }, { "bx", 3 }, { "bl", 3 },
127 	{ "rsi", 4 }, { "esi", 4 }, { "si", 4 }, { "sil", 4 },
128 	{ "rdi", 5 }, { "edi", 5 }, { "di", 5 }, { "dil", 5 },
129 	{ "rbp", 6 }, { "ebp", 6 }, { "bp", 6 }, { "bpl", 6 },
130 	{ "rsp", 7 }, { "esp", 7 }, { "sp", 7 }, { "spl", 7 },
131 	{ "r8", 8 }, { "r8d", 8 }, { "r8w", 8 }, { "r8b", 8 },
132 	{ "r9", 9 }, { "r9d", 9 }, { "r9w", 9 }, { "r9b", 9 },
133 	{ "r10", 10 }, { "r10d", 10 }, { "r10w", 10 }, { "r10b", 10 },
134 	{ "r11", 11 }, { "r11d", 11 }, { "r11w", 11 }, { "r11b", 11 },
135 	{ "r12", 12 }, { "r12d", 12 }, { "r12w", 12 }, { "r12b", 12 },
136 	{ "r13", 13 }, { "r13d", 13 }, { "r13w", 13 }, { "r13b", 13 },
137 	{ "r14", 14 }, { "r14d", 14 }, { "r14w", 14 }, { "r14b", 14 },
138 	{ "r15", 15 }, { "r15d", 15 }, { "r15w", 15 }, { "r15b", 15 },
139 	{ "rip", DWARF_REG_PC },
140 };
141 
get_arch_regnum(const char * name)142 int get_arch_regnum(const char *name)
143 {
144 	unsigned int i;
145 
146 	if (*name != '%')
147 		return -EINVAL;
148 
149 	for (i = 0; i < ARRAY_SIZE(x86_regidx_table); i++)
150 		if (!strcmp(x86_regidx_table[i].name, name + 1))
151 			return x86_regidx_table[i].idx;
152 	return -ENOENT;
153 }
154