xref: /linux/tools/perf/arch/s390/annotate/instructions.c (revision 0e2b2a76278153d1ac312b0691cb65dabb9aef3e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 
4 static int s390_call__parse(struct arch *arch, struct ins_operands *ops,
5 			    struct map_symbol *ms)
6 {
7 	char *endptr, *tok, *name;
8 	struct map *map = ms->map;
9 	struct addr_map_symbol target = {
10 		.ms = { .map = map, },
11 	};
12 
13 	tok = strchr(ops->raw, ',');
14 	if (!tok)
15 		return -1;
16 
17 	ops->target.addr = strtoull(tok + 1, &endptr, 16);
18 
19 	name = strchr(endptr, '<');
20 	if (name == NULL)
21 		return -1;
22 
23 	name++;
24 
25 	if (arch->objdump.skip_functions_char &&
26 	    strchr(name, arch->objdump.skip_functions_char))
27 		return -1;
28 
29 	tok = strchr(name, '>');
30 	if (tok == NULL)
31 		return -1;
32 
33 	*tok = '\0';
34 	ops->target.name = strdup(name);
35 	*tok = '>';
36 
37 	if (ops->target.name == NULL)
38 		return -1;
39 	target.addr = map__objdump_2mem(map, ops->target.addr);
40 
41 	if (maps__find_ams(ms->maps, &target) == 0 &&
42 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
43 		ops->target.sym = target.ms.sym;
44 
45 	return 0;
46 }
47 
48 static struct ins_ops s390_call_ops = {
49 	.parse	   = s390_call__parse,
50 	.scnprintf = call__scnprintf,
51 };
52 
53 static int s390_mov__parse(struct arch *arch __maybe_unused,
54 			   struct ins_operands *ops,
55 			   struct map_symbol *ms __maybe_unused)
56 {
57 	char *s = strchr(ops->raw, ','), *target, *endptr;
58 
59 	if (s == NULL)
60 		return -1;
61 
62 	*s = '\0';
63 	ops->source.raw = strdup(ops->raw);
64 	*s = ',';
65 
66 	if (ops->source.raw == NULL)
67 		return -1;
68 
69 	target = ++s;
70 	ops->target.raw = strdup(target);
71 	if (ops->target.raw == NULL)
72 		goto out_free_source;
73 
74 	ops->target.addr = strtoull(target, &endptr, 16);
75 	if (endptr == target)
76 		goto out_free_target;
77 
78 	s = strchr(endptr, '<');
79 	if (s == NULL)
80 		goto out_free_target;
81 	endptr = strchr(s + 1, '>');
82 	if (endptr == NULL)
83 		goto out_free_target;
84 
85 	*endptr = '\0';
86 	ops->target.name = strdup(s + 1);
87 	*endptr = '>';
88 	if (ops->target.name == NULL)
89 		goto out_free_target;
90 
91 	return 0;
92 
93 out_free_target:
94 	zfree(&ops->target.raw);
95 out_free_source:
96 	zfree(&ops->source.raw);
97 	return -1;
98 }
99 
100 
101 static struct ins_ops s390_mov_ops = {
102 	.parse	   = s390_mov__parse,
103 	.scnprintf = mov__scnprintf,
104 };
105 
106 static struct ins_ops *s390__associate_ins_ops(struct arch *arch, const char *name)
107 {
108 	struct ins_ops *ops = NULL;
109 
110 	/* catch all kind of jumps */
111 	if (strchr(name, 'j') ||
112 	    !strncmp(name, "bct", 3) ||
113 	    !strncmp(name, "br", 2))
114 		ops = &jump_ops;
115 	/* override call/returns */
116 	if (!strcmp(name, "bras") ||
117 	    !strcmp(name, "brasl") ||
118 	    !strcmp(name, "basr"))
119 		ops = &s390_call_ops;
120 	if (!strcmp(name, "br"))
121 		ops = &ret_ops;
122 	/* override load/store relative to PC */
123 	if (!strcmp(name, "lrl") ||
124 	    !strcmp(name, "lgrl") ||
125 	    !strcmp(name, "lgfrl") ||
126 	    !strcmp(name, "llgfrl") ||
127 	    !strcmp(name, "strl") ||
128 	    !strcmp(name, "stgrl"))
129 		ops = &s390_mov_ops;
130 
131 	if (ops)
132 		arch__associate_ins_ops(arch, name, ops);
133 	return ops;
134 }
135 
136 static int s390__cpuid_parse(struct arch *arch, char *cpuid)
137 {
138 	unsigned int family;
139 	char model[16], model_c[16], cpumf_v[16], cpumf_a[16];
140 	int ret;
141 
142 	/*
143 	 * cpuid string format:
144 	 * "IBM,family,model-capacity,model[,cpum_cf-version,cpum_cf-authorization]"
145 	 */
146 	ret = sscanf(cpuid, "%*[^,],%u,%[^,],%[^,],%[^,],%s", &family, model_c,
147 		     model, cpumf_v, cpumf_a);
148 	if (ret >= 2) {
149 		arch->family = family;
150 		arch->model = 0;
151 		return 0;
152 	}
153 
154 	return -1;
155 }
156 
157 static int s390__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
158 {
159 	int err = 0;
160 
161 	if (!arch->initialized) {
162 		arch->initialized = true;
163 		arch->associate_instruction_ops = s390__associate_ins_ops;
164 		if (cpuid) {
165 			if (s390__cpuid_parse(arch, cpuid))
166 				err = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING;
167 		}
168 	}
169 
170 	return err;
171 }
172