18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <elfutils/libdwfl.h>
58c2ecf20Sopenharmony_ci#include "../../util/unwind-libdw.h"
68c2ecf20Sopenharmony_ci#include "../../util/perf_regs.h"
78c2ecf20Sopenharmony_ci#include "../../util/event.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cibool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg)
108c2ecf20Sopenharmony_ci{
118c2ecf20Sopenharmony_ci	struct unwind_info *ui = arg;
128c2ecf20Sopenharmony_ci	struct regs_dump *user_regs = &ui->sample->user_regs;
138c2ecf20Sopenharmony_ci	Dwarf_Word dwarf_regs[32];
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define REG(r) ({						\
168c2ecf20Sopenharmony_ci	Dwarf_Word val = 0;					\
178c2ecf20Sopenharmony_ci	perf_reg_value(&val, user_regs, PERF_REG_RISCV_##r);	\
188c2ecf20Sopenharmony_ci	val;							\
198c2ecf20Sopenharmony_ci})
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	dwarf_regs[0]  = 0;
228c2ecf20Sopenharmony_ci	dwarf_regs[1]  = REG(RA);
238c2ecf20Sopenharmony_ci	dwarf_regs[2]  = REG(SP);
248c2ecf20Sopenharmony_ci	dwarf_regs[3]  = REG(GP);
258c2ecf20Sopenharmony_ci	dwarf_regs[4]  = REG(TP);
268c2ecf20Sopenharmony_ci	dwarf_regs[5]  = REG(T0);
278c2ecf20Sopenharmony_ci	dwarf_regs[6]  = REG(T1);
288c2ecf20Sopenharmony_ci	dwarf_regs[7]  = REG(T2);
298c2ecf20Sopenharmony_ci	dwarf_regs[8]  = REG(S0);
308c2ecf20Sopenharmony_ci	dwarf_regs[9]  = REG(S1);
318c2ecf20Sopenharmony_ci	dwarf_regs[10] = REG(A0);
328c2ecf20Sopenharmony_ci	dwarf_regs[11] = REG(A1);
338c2ecf20Sopenharmony_ci	dwarf_regs[12] = REG(A2);
348c2ecf20Sopenharmony_ci	dwarf_regs[13] = REG(A3);
358c2ecf20Sopenharmony_ci	dwarf_regs[14] = REG(A4);
368c2ecf20Sopenharmony_ci	dwarf_regs[15] = REG(A5);
378c2ecf20Sopenharmony_ci	dwarf_regs[16] = REG(A6);
388c2ecf20Sopenharmony_ci	dwarf_regs[17] = REG(A7);
398c2ecf20Sopenharmony_ci	dwarf_regs[18] = REG(S2);
408c2ecf20Sopenharmony_ci	dwarf_regs[19] = REG(S3);
418c2ecf20Sopenharmony_ci	dwarf_regs[20] = REG(S4);
428c2ecf20Sopenharmony_ci	dwarf_regs[21] = REG(S5);
438c2ecf20Sopenharmony_ci	dwarf_regs[22] = REG(S6);
448c2ecf20Sopenharmony_ci	dwarf_regs[23] = REG(S7);
458c2ecf20Sopenharmony_ci	dwarf_regs[24] = REG(S8);
468c2ecf20Sopenharmony_ci	dwarf_regs[25] = REG(S9);
478c2ecf20Sopenharmony_ci	dwarf_regs[26] = REG(S10);
488c2ecf20Sopenharmony_ci	dwarf_regs[27] = REG(S11);
498c2ecf20Sopenharmony_ci	dwarf_regs[28] = REG(T3);
508c2ecf20Sopenharmony_ci	dwarf_regs[29] = REG(T4);
518c2ecf20Sopenharmony_ci	dwarf_regs[30] = REG(T5);
528c2ecf20Sopenharmony_ci	dwarf_regs[31] = REG(T6);
538c2ecf20Sopenharmony_ci	dwfl_thread_state_register_pc(thread, REG(PC));
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return dwfl_thread_state_registers(thread, 0, PERF_REG_RISCV_MAX,
568c2ecf20Sopenharmony_ci					   dwarf_regs);
578c2ecf20Sopenharmony_ci}
58