162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <elfutils/libdwfl.h>
362306a36Sopenharmony_ci#include <linux/kernel.h>
462306a36Sopenharmony_ci#include "perf_regs.h"
562306a36Sopenharmony_ci#include "../../../util/unwind-libdw.h"
662306a36Sopenharmony_ci#include "../../../util/perf_regs.h"
762306a36Sopenharmony_ci#include "../../../util/sample.h"
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/* See backends/ppc_initreg.c and backends/ppc_regs.c in elfutils.  */
1062306a36Sopenharmony_cistatic const int special_regs[3][2] = {
1162306a36Sopenharmony_ci	{ 65, PERF_REG_POWERPC_LINK },
1262306a36Sopenharmony_ci	{ 101, PERF_REG_POWERPC_XER },
1362306a36Sopenharmony_ci	{ 109, PERF_REG_POWERPC_CTR },
1462306a36Sopenharmony_ci};
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cibool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg)
1762306a36Sopenharmony_ci{
1862306a36Sopenharmony_ci	struct unwind_info *ui = arg;
1962306a36Sopenharmony_ci	struct regs_dump *user_regs = &ui->sample->user_regs;
2062306a36Sopenharmony_ci	Dwarf_Word dwarf_regs[32], dwarf_nip;
2162306a36Sopenharmony_ci	size_t i;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define REG(r) ({						\
2462306a36Sopenharmony_ci	Dwarf_Word val = 0;					\
2562306a36Sopenharmony_ci	perf_reg_value(&val, user_regs, PERF_REG_POWERPC_##r);	\
2662306a36Sopenharmony_ci	val;							\
2762306a36Sopenharmony_ci})
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	dwarf_regs[0]  = REG(R0);
3062306a36Sopenharmony_ci	dwarf_regs[1]  = REG(R1);
3162306a36Sopenharmony_ci	dwarf_regs[2]  = REG(R2);
3262306a36Sopenharmony_ci	dwarf_regs[3]  = REG(R3);
3362306a36Sopenharmony_ci	dwarf_regs[4]  = REG(R4);
3462306a36Sopenharmony_ci	dwarf_regs[5]  = REG(R5);
3562306a36Sopenharmony_ci	dwarf_regs[6]  = REG(R6);
3662306a36Sopenharmony_ci	dwarf_regs[7]  = REG(R7);
3762306a36Sopenharmony_ci	dwarf_regs[8]  = REG(R8);
3862306a36Sopenharmony_ci	dwarf_regs[9]  = REG(R9);
3962306a36Sopenharmony_ci	dwarf_regs[10] = REG(R10);
4062306a36Sopenharmony_ci	dwarf_regs[11] = REG(R11);
4162306a36Sopenharmony_ci	dwarf_regs[12] = REG(R12);
4262306a36Sopenharmony_ci	dwarf_regs[13] = REG(R13);
4362306a36Sopenharmony_ci	dwarf_regs[14] = REG(R14);
4462306a36Sopenharmony_ci	dwarf_regs[15] = REG(R15);
4562306a36Sopenharmony_ci	dwarf_regs[16] = REG(R16);
4662306a36Sopenharmony_ci	dwarf_regs[17] = REG(R17);
4762306a36Sopenharmony_ci	dwarf_regs[18] = REG(R18);
4862306a36Sopenharmony_ci	dwarf_regs[19] = REG(R19);
4962306a36Sopenharmony_ci	dwarf_regs[20] = REG(R20);
5062306a36Sopenharmony_ci	dwarf_regs[21] = REG(R21);
5162306a36Sopenharmony_ci	dwarf_regs[22] = REG(R22);
5262306a36Sopenharmony_ci	dwarf_regs[23] = REG(R23);
5362306a36Sopenharmony_ci	dwarf_regs[24] = REG(R24);
5462306a36Sopenharmony_ci	dwarf_regs[25] = REG(R25);
5562306a36Sopenharmony_ci	dwarf_regs[26] = REG(R26);
5662306a36Sopenharmony_ci	dwarf_regs[27] = REG(R27);
5762306a36Sopenharmony_ci	dwarf_regs[28] = REG(R28);
5862306a36Sopenharmony_ci	dwarf_regs[29] = REG(R29);
5962306a36Sopenharmony_ci	dwarf_regs[30] = REG(R30);
6062306a36Sopenharmony_ci	dwarf_regs[31] = REG(R31);
6162306a36Sopenharmony_ci	if (!dwfl_thread_state_registers(thread, 0, 32, dwarf_regs))
6262306a36Sopenharmony_ci		return false;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	dwarf_nip = REG(NIP);
6562306a36Sopenharmony_ci	dwfl_thread_state_register_pc(thread, dwarf_nip);
6662306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(special_regs); i++) {
6762306a36Sopenharmony_ci		Dwarf_Word val = 0;
6862306a36Sopenharmony_ci		perf_reg_value(&val, user_regs, special_regs[i][1]);
6962306a36Sopenharmony_ci		if (!dwfl_thread_state_registers(thread,
7062306a36Sopenharmony_ci						 special_regs[i][0], 1,
7162306a36Sopenharmony_ci						 &val))
7262306a36Sopenharmony_ci			return false;
7362306a36Sopenharmony_ci	}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	return true;
7662306a36Sopenharmony_ci}
77