xref: /linux/tools/perf/util/perf_regs.c (revision ec87c99de48933e8b9b530b08e51832b400256bd)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <string.h>
4 #include "perf_regs.h"
5 #include "util/sample.h"
6 #include "debug.h"
7 
8 int __weak arch_sdt_arg_parse_op(char *old_op __maybe_unused,
9 				 char **new_op __maybe_unused)
10 {
11 	return SDT_ARG_SKIP;
12 }
13 
14 uint64_t __weak arch__intr_reg_mask(void)
15 {
16 	return 0;
17 }
18 
19 uint64_t __weak arch__user_reg_mask(void)
20 {
21 	return 0;
22 }
23 
24 const char *perf_reg_name(int id, const char *arch)
25 {
26 	const char *reg_name = NULL;
27 
28 	if (!strcmp(arch, "csky"))
29 		reg_name = __perf_reg_name_csky(id);
30 	else if (!strcmp(arch, "loongarch"))
31 		reg_name = __perf_reg_name_loongarch(id);
32 	else if (!strcmp(arch, "mips"))
33 		reg_name = __perf_reg_name_mips(id);
34 	else if (!strcmp(arch, "powerpc"))
35 		reg_name = __perf_reg_name_powerpc(id);
36 	else if (!strcmp(arch, "riscv"))
37 		reg_name = __perf_reg_name_riscv(id);
38 	else if (!strcmp(arch, "s390"))
39 		reg_name = __perf_reg_name_s390(id);
40 	else if (!strcmp(arch, "x86"))
41 		reg_name = __perf_reg_name_x86(id);
42 	else if (!strcmp(arch, "arm"))
43 		reg_name = __perf_reg_name_arm(id);
44 	else if (!strcmp(arch, "arm64"))
45 		reg_name = __perf_reg_name_arm64(id);
46 
47 	return reg_name ?: "unknown";
48 }
49 
50 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
51 {
52 	int i, idx = 0;
53 	u64 mask = regs->mask;
54 
55 	if ((u64)id >= PERF_SAMPLE_REGS_CACHE_SIZE)
56 		return -EINVAL;
57 
58 	if (regs->cache_mask & (1ULL << id))
59 		goto out;
60 
61 	if (!(mask & (1ULL << id)))
62 		return -EINVAL;
63 
64 	for (i = 0; i < id; i++) {
65 		if (mask & (1ULL << i))
66 			idx++;
67 	}
68 
69 	regs->cache_mask |= (1ULL << id);
70 	regs->cache_regs[id] = regs->regs[idx];
71 
72 out:
73 	*valp = regs->cache_regs[id];
74 	return 0;
75 }
76 
77 uint64_t perf_arch_reg_ip(const char *arch)
78 {
79 	if (!strcmp(arch, "arm"))
80 		return __perf_reg_ip_arm();
81 	else if (!strcmp(arch, "arm64"))
82 		return __perf_reg_ip_arm64();
83 	else if (!strcmp(arch, "csky"))
84 		return __perf_reg_ip_csky();
85 	else if (!strcmp(arch, "loongarch"))
86 		return __perf_reg_ip_loongarch();
87 	else if (!strcmp(arch, "mips"))
88 		return __perf_reg_ip_mips();
89 	else if (!strcmp(arch, "powerpc"))
90 		return __perf_reg_ip_powerpc();
91 	else if (!strcmp(arch, "riscv"))
92 		return __perf_reg_ip_riscv();
93 	else if (!strcmp(arch, "s390"))
94 		return __perf_reg_ip_s390();
95 	else if (!strcmp(arch, "x86"))
96 		return __perf_reg_ip_x86();
97 
98 	pr_err("Fail to find IP register for arch %s, returns 0\n", arch);
99 	return 0;
100 }
101 
102 uint64_t perf_arch_reg_sp(const char *arch)
103 {
104 	if (!strcmp(arch, "arm"))
105 		return __perf_reg_sp_arm();
106 	else if (!strcmp(arch, "arm64"))
107 		return __perf_reg_sp_arm64();
108 	else if (!strcmp(arch, "csky"))
109 		return __perf_reg_sp_csky();
110 	else if (!strcmp(arch, "loongarch"))
111 		return __perf_reg_sp_loongarch();
112 	else if (!strcmp(arch, "mips"))
113 		return __perf_reg_sp_mips();
114 	else if (!strcmp(arch, "powerpc"))
115 		return __perf_reg_sp_powerpc();
116 	else if (!strcmp(arch, "riscv"))
117 		return __perf_reg_sp_riscv();
118 	else if (!strcmp(arch, "s390"))
119 		return __perf_reg_sp_s390();
120 	else if (!strcmp(arch, "x86"))
121 		return __perf_reg_sp_x86();
122 
123 	pr_err("Fail to find SP register for arch %s, returns 0\n", arch);
124 	return 0;
125 }
126