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