1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License.  See the file "COPYING" in the main directory of this archive
5 * for more details.
6 *
7 * Some parts derived from x86 version of this file.
8 *
9 * Copyright (C) 2013 Cavium, Inc.
10 * Copyright (C) 2021 Loongson Technology Corporation Limited
11 */
12
13#include <linux/perf_event.h>
14
15#include <asm/ptrace.h>
16
17#ifdef CONFIG_32BIT
18u64 perf_reg_abi(struct task_struct *tsk)
19{
20	return PERF_SAMPLE_REGS_ABI_32;
21}
22#else /* Must be CONFIG_64BIT */
23u64 perf_reg_abi(struct task_struct *tsk)
24{
25	if (test_tsk_thread_flag(tsk, TIF_32BIT_REGS))
26		return PERF_SAMPLE_REGS_ABI_32;
27	else
28		return PERF_SAMPLE_REGS_ABI_64;
29}
30#endif /* CONFIG_32BIT */
31
32int perf_reg_validate(u64 mask)
33{
34	if (!mask)
35		return -EINVAL;
36	if (mask & ~((1ull << PERF_REG_LOONGARCH_MAX) - 1))
37		return -EINVAL;
38	return 0;
39}
40
41u64 perf_reg_value(struct pt_regs *regs, int idx)
42{
43	if (WARN_ON_ONCE((u32)idx >= PERF_REG_LOONGARCH_MAX))
44		return 0;
45
46	if ((u32)idx == PERF_REG_LOONGARCH_PC)
47		return regs->csr_era;
48
49	return regs->regs[idx];
50}
51
52void perf_get_regs_user(struct perf_regs *regs_user,
53			struct pt_regs *regs)
54{
55	regs_user->regs = task_pt_regs(current);
56	regs_user->abi = perf_reg_abi(current);
57}
58