xref: /openbmc/linux/tools/perf/arch/arm64/util/dwarf-regs.c (revision 3bb53c9f124bd9297f18d58a395cff59dfaf8541)
18ab596afSJean Pihet /*
28ab596afSJean Pihet  * Mapping of DWARF debug register numbers into register names.
38ab596afSJean Pihet  *
48ab596afSJean Pihet  * Copyright (C) 2010 Will Deacon, ARM Ltd.
58ab596afSJean Pihet  *
68ab596afSJean Pihet  * This program is free software; you can redistribute it and/or modify
78ab596afSJean Pihet  * it under the terms of the GNU General Public License version 2 as
88ab596afSJean Pihet  * published by the Free Software Foundation.
98ab596afSJean Pihet  */
108ab596afSJean Pihet 
118ab596afSJean Pihet #include <stddef.h>
128ab596afSJean Pihet #include <dwarf-regs.h>
13*3bb53c9fSHe Kuang #include <linux/ptrace.h> /* for struct user_pt_regs */
14*3bb53c9fSHe Kuang #include "util.h"
158ab596afSJean Pihet 
168ab596afSJean Pihet struct pt_regs_dwarfnum {
178ab596afSJean Pihet 	const char *name;
188ab596afSJean Pihet 	unsigned int dwarfnum;
198ab596afSJean Pihet };
208ab596afSJean Pihet 
218ab596afSJean Pihet #define REG_DWARFNUM_NAME(r, num) {.name = r, .dwarfnum = num}
228ab596afSJean Pihet #define GPR_DWARFNUM_NAME(num) \
238ab596afSJean Pihet 	{.name = STR(%x##num), .dwarfnum = num}
248ab596afSJean Pihet #define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0}
25*3bb53c9fSHe Kuang #define DWARFNUM2OFFSET(index) \
26*3bb53c9fSHe Kuang 	(index * sizeof((struct user_pt_regs *)0)->regs[0])
278ab596afSJean Pihet 
288ab596afSJean Pihet /*
298ab596afSJean Pihet  * Reference:
308ab596afSJean Pihet  * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0057b/IHI0057B_aadwarf64.pdf
318ab596afSJean Pihet  */
328ab596afSJean Pihet static const struct pt_regs_dwarfnum regdwarfnum_table[] = {
338ab596afSJean Pihet 	GPR_DWARFNUM_NAME(0),
348ab596afSJean Pihet 	GPR_DWARFNUM_NAME(1),
358ab596afSJean Pihet 	GPR_DWARFNUM_NAME(2),
368ab596afSJean Pihet 	GPR_DWARFNUM_NAME(3),
378ab596afSJean Pihet 	GPR_DWARFNUM_NAME(4),
388ab596afSJean Pihet 	GPR_DWARFNUM_NAME(5),
398ab596afSJean Pihet 	GPR_DWARFNUM_NAME(6),
408ab596afSJean Pihet 	GPR_DWARFNUM_NAME(7),
418ab596afSJean Pihet 	GPR_DWARFNUM_NAME(8),
428ab596afSJean Pihet 	GPR_DWARFNUM_NAME(9),
438ab596afSJean Pihet 	GPR_DWARFNUM_NAME(10),
448ab596afSJean Pihet 	GPR_DWARFNUM_NAME(11),
458ab596afSJean Pihet 	GPR_DWARFNUM_NAME(12),
468ab596afSJean Pihet 	GPR_DWARFNUM_NAME(13),
478ab596afSJean Pihet 	GPR_DWARFNUM_NAME(14),
488ab596afSJean Pihet 	GPR_DWARFNUM_NAME(15),
498ab596afSJean Pihet 	GPR_DWARFNUM_NAME(16),
508ab596afSJean Pihet 	GPR_DWARFNUM_NAME(17),
518ab596afSJean Pihet 	GPR_DWARFNUM_NAME(18),
528ab596afSJean Pihet 	GPR_DWARFNUM_NAME(19),
538ab596afSJean Pihet 	GPR_DWARFNUM_NAME(20),
548ab596afSJean Pihet 	GPR_DWARFNUM_NAME(21),
558ab596afSJean Pihet 	GPR_DWARFNUM_NAME(22),
568ab596afSJean Pihet 	GPR_DWARFNUM_NAME(23),
578ab596afSJean Pihet 	GPR_DWARFNUM_NAME(24),
588ab596afSJean Pihet 	GPR_DWARFNUM_NAME(25),
598ab596afSJean Pihet 	GPR_DWARFNUM_NAME(26),
608ab596afSJean Pihet 	GPR_DWARFNUM_NAME(27),
618ab596afSJean Pihet 	GPR_DWARFNUM_NAME(28),
628ab596afSJean Pihet 	GPR_DWARFNUM_NAME(29),
638ab596afSJean Pihet 	REG_DWARFNUM_NAME("%lr", 30),
648ab596afSJean Pihet 	REG_DWARFNUM_NAME("%sp", 31),
658ab596afSJean Pihet 	REG_DWARFNUM_END,
668ab596afSJean Pihet };
678ab596afSJean Pihet 
688ab596afSJean Pihet /**
698ab596afSJean Pihet  * get_arch_regstr() - lookup register name from it's DWARF register number
708ab596afSJean Pihet  * @n:	the DWARF register number
718ab596afSJean Pihet  *
728ab596afSJean Pihet  * get_arch_regstr() returns the name of the register in struct
738ab596afSJean Pihet  * regdwarfnum_table from it's DWARF register number. If the register is not
748ab596afSJean Pihet  * found in the table, this returns NULL;
758ab596afSJean Pihet  */
768ab596afSJean Pihet const char *get_arch_regstr(unsigned int n)
778ab596afSJean Pihet {
788ab596afSJean Pihet 	const struct pt_regs_dwarfnum *roff;
798ab596afSJean Pihet 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
808ab596afSJean Pihet 		if (roff->dwarfnum == n)
818ab596afSJean Pihet 			return roff->name;
828ab596afSJean Pihet 	return NULL;
838ab596afSJean Pihet }
84*3bb53c9fSHe Kuang 
85*3bb53c9fSHe Kuang int regs_query_register_offset(const char *name)
86*3bb53c9fSHe Kuang {
87*3bb53c9fSHe Kuang 	const struct pt_regs_dwarfnum *roff;
88*3bb53c9fSHe Kuang 
89*3bb53c9fSHe Kuang 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
90*3bb53c9fSHe Kuang 		if (!strcmp(roff->name, name))
91*3bb53c9fSHe Kuang 			return DWARFNUM2OFFSET(roff->dwarfnum);
92*3bb53c9fSHe Kuang 	return -EINVAL;
93*3bb53c9fSHe Kuang }
94