18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2020-2023 Loongson Technology Corporation Limited */
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/sample.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[PERF_REG_LOONGARCH_MAX];
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define REG(r) ({							\
168c2ecf20Sopenharmony_ci	Dwarf_Word val = 0;						\
178c2ecf20Sopenharmony_ci	perf_reg_value(&val, user_regs, PERF_REG_LOONGARCH_##r);	\
188c2ecf20Sopenharmony_ci	val;								\
198c2ecf20Sopenharmony_ci})
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	dwarf_regs[0]  = 0;
228c2ecf20Sopenharmony_ci	dwarf_regs[1]  = REG(R1);
238c2ecf20Sopenharmony_ci	dwarf_regs[2]  = REG(R2);
248c2ecf20Sopenharmony_ci	dwarf_regs[3]  = REG(R3);
258c2ecf20Sopenharmony_ci	dwarf_regs[4]  = REG(R4);
268c2ecf20Sopenharmony_ci	dwarf_regs[5]  = REG(R5);
278c2ecf20Sopenharmony_ci	dwarf_regs[6]  = REG(R6);
288c2ecf20Sopenharmony_ci	dwarf_regs[7]  = REG(R7);
298c2ecf20Sopenharmony_ci	dwarf_regs[8]  = REG(R8);
308c2ecf20Sopenharmony_ci	dwarf_regs[9]  = REG(R9);
318c2ecf20Sopenharmony_ci	dwarf_regs[10] = REG(R10);
328c2ecf20Sopenharmony_ci	dwarf_regs[11] = REG(R11);
338c2ecf20Sopenharmony_ci	dwarf_regs[12] = REG(R12);
348c2ecf20Sopenharmony_ci	dwarf_regs[13] = REG(R13);
358c2ecf20Sopenharmony_ci	dwarf_regs[14] = REG(R14);
368c2ecf20Sopenharmony_ci	dwarf_regs[15] = REG(R15);
378c2ecf20Sopenharmony_ci	dwarf_regs[16] = REG(R16);
388c2ecf20Sopenharmony_ci	dwarf_regs[17] = REG(R17);
398c2ecf20Sopenharmony_ci	dwarf_regs[18] = REG(R18);
408c2ecf20Sopenharmony_ci	dwarf_regs[19] = REG(R19);
418c2ecf20Sopenharmony_ci	dwarf_regs[20] = REG(R20);
428c2ecf20Sopenharmony_ci	dwarf_regs[21] = REG(R21);
438c2ecf20Sopenharmony_ci	dwarf_regs[22] = REG(R22);
448c2ecf20Sopenharmony_ci	dwarf_regs[23] = REG(R23);
458c2ecf20Sopenharmony_ci	dwarf_regs[24] = REG(R24);
468c2ecf20Sopenharmony_ci	dwarf_regs[25] = REG(R25);
478c2ecf20Sopenharmony_ci	dwarf_regs[26] = REG(R26);
488c2ecf20Sopenharmony_ci	dwarf_regs[27] = REG(R27);
498c2ecf20Sopenharmony_ci	dwarf_regs[28] = REG(R28);
508c2ecf20Sopenharmony_ci	dwarf_regs[29] = REG(R29);
518c2ecf20Sopenharmony_ci	dwarf_regs[30] = REG(R30);
528c2ecf20Sopenharmony_ci	dwarf_regs[31] = REG(R31);
538c2ecf20Sopenharmony_ci	dwfl_thread_state_register_pc(thread, REG(PC));
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return dwfl_thread_state_registers(thread, 0, PERF_REG_LOONGARCH_MAX, dwarf_regs);
568c2ecf20Sopenharmony_ci}
57