1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Mapping of DWARF debug register numbers into register names.
4  *
5  * Copyright (C) 2010 Will Deacon, ARM Ltd.
6  */
7 
8 #include <errno.h>
9 #include <stddef.h>
10 #include <string.h>
11 #include <dwarf-regs.h>
12 #include <linux/ptrace.h> /* for struct user_pt_regs */
13 #include <linux/stringify.h>
14 #include "util.h"
15 
16 struct pt_regs_dwarfnum {
17 	const char *name;
18 	unsigned int dwarfnum;
19 };
20 
21 #define REG_DWARFNUM_NAME(r, num) {.name = r, .dwarfnum = num}
22 #define GPR_DWARFNUM_NAME(num) \
23 	{.name = __stringify(%x##num), .dwarfnum = num}
24 #define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0}
25 #define DWARFNUM2OFFSET(index) \
26 	(index * sizeof((struct user_pt_regs *)0)->regs[0])
27 
28 /*
29  * Reference:
30  * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0057b/IHI0057B_aadwarf64.pdf
31  */
32 static const struct pt_regs_dwarfnum regdwarfnum_table[] = {
33 	GPR_DWARFNUM_NAME(0),
34 	GPR_DWARFNUM_NAME(1),
35 	GPR_DWARFNUM_NAME(2),
36 	GPR_DWARFNUM_NAME(3),
37 	GPR_DWARFNUM_NAME(4),
38 	GPR_DWARFNUM_NAME(5),
39 	GPR_DWARFNUM_NAME(6),
40 	GPR_DWARFNUM_NAME(7),
41 	GPR_DWARFNUM_NAME(8),
42 	GPR_DWARFNUM_NAME(9),
43 	GPR_DWARFNUM_NAME(10),
44 	GPR_DWARFNUM_NAME(11),
45 	GPR_DWARFNUM_NAME(12),
46 	GPR_DWARFNUM_NAME(13),
47 	GPR_DWARFNUM_NAME(14),
48 	GPR_DWARFNUM_NAME(15),
49 	GPR_DWARFNUM_NAME(16),
50 	GPR_DWARFNUM_NAME(17),
51 	GPR_DWARFNUM_NAME(18),
52 	GPR_DWARFNUM_NAME(19),
53 	GPR_DWARFNUM_NAME(20),
54 	GPR_DWARFNUM_NAME(21),
55 	GPR_DWARFNUM_NAME(22),
56 	GPR_DWARFNUM_NAME(23),
57 	GPR_DWARFNUM_NAME(24),
58 	GPR_DWARFNUM_NAME(25),
59 	GPR_DWARFNUM_NAME(26),
60 	GPR_DWARFNUM_NAME(27),
61 	GPR_DWARFNUM_NAME(28),
62 	GPR_DWARFNUM_NAME(29),
63 	REG_DWARFNUM_NAME("%lr", 30),
64 	REG_DWARFNUM_NAME("%sp", 31),
65 	REG_DWARFNUM_END,
66 };
67 
68 /**
69  * get_arch_regstr() - lookup register name from it's DWARF register number
70  * @n:	the DWARF register number
71  *
72  * get_arch_regstr() returns the name of the register in struct
73  * regdwarfnum_table from it's DWARF register number. If the register is not
74  * found in the table, this returns NULL;
75  */
76 const char *get_arch_regstr(unsigned int n)
77 {
78 	const struct pt_regs_dwarfnum *roff;
79 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
80 		if (roff->dwarfnum == n)
81 			return roff->name;
82 	return NULL;
83 }
84 
85 int regs_query_register_offset(const char *name)
86 {
87 	const struct pt_regs_dwarfnum *roff;
88 
89 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
90 		if (!strcmp(roff->name, name))
91 			return DWARFNUM2OFFSET(roff->dwarfnum);
92 	return -EINVAL;
93 }
94