18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/errno.h>
58c2ecf20Sopenharmony_ci#include <linux/kernel.h>
68c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
78c2ecf20Sopenharmony_ci#include <linux/bug.h>
88c2ecf20Sopenharmony_ci#include <asm/perf_regs.h>
98c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciu64 perf_reg_value(struct pt_regs *regs, int idx)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE((u32)idx >= PERF_REG_RISCV_MAX))
148c2ecf20Sopenharmony_ci		return 0;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	return ((unsigned long *)regs)[idx];
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define REG_RESERVED (~((1ULL << PERF_REG_RISCV_MAX) - 1))
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciint perf_reg_validate(u64 mask)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	if (!mask || mask & REG_RESERVED)
248c2ecf20Sopenharmony_ci		return -EINVAL;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	return 0;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciu64 perf_reg_abi(struct task_struct *task)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci#if __riscv_xlen == 64
328c2ecf20Sopenharmony_ci	return PERF_SAMPLE_REGS_ABI_64;
338c2ecf20Sopenharmony_ci#else
348c2ecf20Sopenharmony_ci	return PERF_SAMPLE_REGS_ABI_32;
358c2ecf20Sopenharmony_ci#endif
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid perf_get_regs_user(struct perf_regs *regs_user,
398c2ecf20Sopenharmony_ci			struct pt_regs *regs)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	regs_user->regs = task_pt_regs(current);
428c2ecf20Sopenharmony_ci	regs_user->abi = perf_reg_abi(current);
438c2ecf20Sopenharmony_ci}
44