162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <elfutils/libdwfl.h>
562306a36Sopenharmony_ci#include "perf_regs.h"
662306a36Sopenharmony_ci#include "../../util/unwind-libdw.h"
762306a36Sopenharmony_ci#include "../../util/perf_regs.h"
862306a36Sopenharmony_ci#include "../../util/sample.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cibool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg)
1162306a36Sopenharmony_ci{
1262306a36Sopenharmony_ci	struct unwind_info *ui = arg;
1362306a36Sopenharmony_ci	struct regs_dump *user_regs = &ui->sample->user_regs;
1462306a36Sopenharmony_ci	Dwarf_Word dwarf_regs[32];
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define REG(r) ({						\
1762306a36Sopenharmony_ci	Dwarf_Word val = 0;					\
1862306a36Sopenharmony_ci	perf_reg_value(&val, user_regs, PERF_REG_RISCV_##r);	\
1962306a36Sopenharmony_ci	val;							\
2062306a36Sopenharmony_ci})
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	dwarf_regs[0]  = 0;
2362306a36Sopenharmony_ci	dwarf_regs[1]  = REG(RA);
2462306a36Sopenharmony_ci	dwarf_regs[2]  = REG(SP);
2562306a36Sopenharmony_ci	dwarf_regs[3]  = REG(GP);
2662306a36Sopenharmony_ci	dwarf_regs[4]  = REG(TP);
2762306a36Sopenharmony_ci	dwarf_regs[5]  = REG(T0);
2862306a36Sopenharmony_ci	dwarf_regs[6]  = REG(T1);
2962306a36Sopenharmony_ci	dwarf_regs[7]  = REG(T2);
3062306a36Sopenharmony_ci	dwarf_regs[8]  = REG(S0);
3162306a36Sopenharmony_ci	dwarf_regs[9]  = REG(S1);
3262306a36Sopenharmony_ci	dwarf_regs[10] = REG(A0);
3362306a36Sopenharmony_ci	dwarf_regs[11] = REG(A1);
3462306a36Sopenharmony_ci	dwarf_regs[12] = REG(A2);
3562306a36Sopenharmony_ci	dwarf_regs[13] = REG(A3);
3662306a36Sopenharmony_ci	dwarf_regs[14] = REG(A4);
3762306a36Sopenharmony_ci	dwarf_regs[15] = REG(A5);
3862306a36Sopenharmony_ci	dwarf_regs[16] = REG(A6);
3962306a36Sopenharmony_ci	dwarf_regs[17] = REG(A7);
4062306a36Sopenharmony_ci	dwarf_regs[18] = REG(S2);
4162306a36Sopenharmony_ci	dwarf_regs[19] = REG(S3);
4262306a36Sopenharmony_ci	dwarf_regs[20] = REG(S4);
4362306a36Sopenharmony_ci	dwarf_regs[21] = REG(S5);
4462306a36Sopenharmony_ci	dwarf_regs[22] = REG(S6);
4562306a36Sopenharmony_ci	dwarf_regs[23] = REG(S7);
4662306a36Sopenharmony_ci	dwarf_regs[24] = REG(S8);
4762306a36Sopenharmony_ci	dwarf_regs[25] = REG(S9);
4862306a36Sopenharmony_ci	dwarf_regs[26] = REG(S10);
4962306a36Sopenharmony_ci	dwarf_regs[27] = REG(S11);
5062306a36Sopenharmony_ci	dwarf_regs[28] = REG(T3);
5162306a36Sopenharmony_ci	dwarf_regs[29] = REG(T4);
5262306a36Sopenharmony_ci	dwarf_regs[30] = REG(T5);
5362306a36Sopenharmony_ci	dwarf_regs[31] = REG(T6);
5462306a36Sopenharmony_ci	dwfl_thread_state_register_pc(thread, REG(PC));
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	return dwfl_thread_state_registers(thread, 0, PERF_REG_RISCV_MAX,
5762306a36Sopenharmony_ci					   dwarf_regs);
5862306a36Sopenharmony_ci}
59