18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd.
48c2ecf20Sopenharmony_ci * Mapping of DWARF debug register numbers into register names.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <stddef.h>
88c2ecf20Sopenharmony_ci#include <errno.h> /* for EINVAL */
98c2ecf20Sopenharmony_ci#include <string.h> /* for strcmp */
108c2ecf20Sopenharmony_ci#include <dwarf-regs.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct pt_regs_dwarfnum {
138c2ecf20Sopenharmony_ci	const char *name;
148c2ecf20Sopenharmony_ci	unsigned int dwarfnum;
158c2ecf20Sopenharmony_ci};
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define REG_DWARFNUM_NAME(r, num) {.name = r, .dwarfnum = num}
188c2ecf20Sopenharmony_ci#define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct pt_regs_dwarfnum riscv_dwarf_regs_table[] = {
218c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%zero", 0),
228c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%ra", 1),
238c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%sp", 2),
248c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%gp", 3),
258c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%tp", 4),
268c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t0", 5),
278c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t1", 6),
288c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t2", 7),
298c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s0", 8),
308c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s1", 9),
318c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a0", 10),
328c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a1", 11),
338c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a2", 12),
348c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a3", 13),
358c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a4", 14),
368c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a5", 15),
378c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a6", 16),
388c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%a7", 17),
398c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s2", 18),
408c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s3", 19),
418c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s4", 20),
428c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s5", 21),
438c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s6", 22),
448c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s7", 23),
458c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s8", 24),
468c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s9", 25),
478c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s10", 26),
488c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%s11", 27),
498c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t3", 28),
508c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t4", 29),
518c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t5", 30),
528c2ecf20Sopenharmony_ci	REG_DWARFNUM_NAME("%t6", 31),
538c2ecf20Sopenharmony_ci	REG_DWARFNUM_END,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define RISCV_MAX_REGS ((sizeof(riscv_dwarf_regs_table) / \
578c2ecf20Sopenharmony_ci		 sizeof(riscv_dwarf_regs_table[0])) - 1)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciconst char *get_arch_regstr(unsigned int n)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	return (n < RISCV_MAX_REGS) ? riscv_dwarf_regs_table[n].name : NULL;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciint regs_query_register_offset(const char *name)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	const struct pt_regs_dwarfnum *roff;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	for (roff = riscv_dwarf_regs_table; roff->name; roff++)
698c2ecf20Sopenharmony_ci		if (!strcmp(roff->name, name))
708c2ecf20Sopenharmony_ci			return roff->dwarfnum;
718c2ecf20Sopenharmony_ci	return -EINVAL;
728c2ecf20Sopenharmony_ci}
73