xref: /openbmc/linux/tools/perf/arch/s390/annotate/instructions.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2696e2457SJiri Olsa #include <linux/compiler.h>
3696e2457SJiri Olsa 
s390_call__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)40b58a77cSThomas Richter static int s390_call__parse(struct arch *arch, struct ins_operands *ops,
585a84e4fSArnaldo Carvalho de Melo 			    struct map_symbol *ms)
60b58a77cSThomas Richter {
70b58a77cSThomas Richter 	char *endptr, *tok, *name;
885a84e4fSArnaldo Carvalho de Melo 	struct map *map = ms->map;
90b58a77cSThomas Richter 	struct addr_map_symbol target = {
10d46a4cdfSArnaldo Carvalho de Melo 		.ms = { .map = map, },
110b58a77cSThomas Richter 	};
120b58a77cSThomas Richter 
130b58a77cSThomas Richter 	tok = strchr(ops->raw, ',');
140b58a77cSThomas Richter 	if (!tok)
150b58a77cSThomas Richter 		return -1;
160b58a77cSThomas Richter 
170b58a77cSThomas Richter 	ops->target.addr = strtoull(tok + 1, &endptr, 16);
180b58a77cSThomas Richter 
190b58a77cSThomas Richter 	name = strchr(endptr, '<');
200b58a77cSThomas Richter 	if (name == NULL)
210b58a77cSThomas Richter 		return -1;
220b58a77cSThomas Richter 
230b58a77cSThomas Richter 	name++;
240b58a77cSThomas Richter 
250b58a77cSThomas Richter 	if (arch->objdump.skip_functions_char &&
260b58a77cSThomas Richter 	    strchr(name, arch->objdump.skip_functions_char))
270b58a77cSThomas Richter 		return -1;
280b58a77cSThomas Richter 
290b58a77cSThomas Richter 	tok = strchr(name, '>');
300b58a77cSThomas Richter 	if (tok == NULL)
310b58a77cSThomas Richter 		return -1;
320b58a77cSThomas Richter 
330b58a77cSThomas Richter 	*tok = '\0';
340b58a77cSThomas Richter 	ops->target.name = strdup(name);
350b58a77cSThomas Richter 	*tok = '>';
360b58a77cSThomas Richter 
370b58a77cSThomas Richter 	if (ops->target.name == NULL)
380b58a77cSThomas Richter 		return -1;
390b58a77cSThomas Richter 	target.addr = map__objdump_2mem(map, ops->target.addr);
400b58a77cSThomas Richter 
41f2eaea09SArnaldo Carvalho de Melo 	if (maps__find_ams(ms->maps, &target) == 0 &&
42*78a1f7cdSIan Rogers 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
43d46a4cdfSArnaldo Carvalho de Melo 		ops->target.sym = target.ms.sym;
440b58a77cSThomas Richter 
450b58a77cSThomas Richter 	return 0;
460b58a77cSThomas Richter }
470b58a77cSThomas Richter 
480b58a77cSThomas Richter static struct ins_ops s390_call_ops = {
49bc3bb795SArnaldo Carvalho de Melo 	.parse	   = s390_call__parse,
500b58a77cSThomas Richter 	.scnprintf = call__scnprintf,
510b58a77cSThomas Richter };
520b58a77cSThomas Richter 
s390_mov__parse(struct arch * arch __maybe_unused,struct ins_operands * ops,struct map_symbol * ms __maybe_unused)530b58a77cSThomas Richter static int s390_mov__parse(struct arch *arch __maybe_unused,
540b58a77cSThomas Richter 			   struct ins_operands *ops,
550b58a77cSThomas Richter 			   struct map_symbol *ms __maybe_unused)
560b4b6b78SThomas Richter {
570b4b6b78SThomas Richter 	char *s = strchr(ops->raw, ','), *target, *endptr;
5885a84e4fSArnaldo Carvalho de Melo 
590b4b6b78SThomas Richter 	if (s == NULL)
600b4b6b78SThomas Richter 		return -1;
610b4b6b78SThomas Richter 
620b4b6b78SThomas Richter 	*s = '\0';
630b4b6b78SThomas Richter 	ops->source.raw = strdup(ops->raw);
640b4b6b78SThomas Richter 	*s = ',';
650b4b6b78SThomas Richter 
660b4b6b78SThomas Richter 	if (ops->source.raw == NULL)
670b4b6b78SThomas Richter 		return -1;
680b4b6b78SThomas Richter 
690b4b6b78SThomas Richter 	target = ++s;
700b4b6b78SThomas Richter 	ops->target.raw = strdup(target);
710b4b6b78SThomas Richter 	if (ops->target.raw == NULL)
720b4b6b78SThomas Richter 		goto out_free_source;
730b4b6b78SThomas Richter 
740b4b6b78SThomas Richter 	ops->target.addr = strtoull(target, &endptr, 16);
750b4b6b78SThomas Richter 	if (endptr == target)
760b4b6b78SThomas Richter 		goto out_free_target;
770b4b6b78SThomas Richter 
780b4b6b78SThomas Richter 	s = strchr(endptr, '<');
790b4b6b78SThomas Richter 	if (s == NULL)
800b4b6b78SThomas Richter 		goto out_free_target;
810b4b6b78SThomas Richter 	endptr = strchr(s + 1, '>');
820b4b6b78SThomas Richter 	if (endptr == NULL)
830b4b6b78SThomas Richter 		goto out_free_target;
840b4b6b78SThomas Richter 
850b4b6b78SThomas Richter 	*endptr = '\0';
860b4b6b78SThomas Richter 	ops->target.name = strdup(s + 1);
870b4b6b78SThomas Richter 	*endptr = '>';
880b4b6b78SThomas Richter 	if (ops->target.name == NULL)
890b4b6b78SThomas Richter 		goto out_free_target;
900b4b6b78SThomas Richter 
910b4b6b78SThomas Richter 	return 0;
920b4b6b78SThomas Richter 
930b4b6b78SThomas Richter out_free_target:
940b4b6b78SThomas Richter 	zfree(&ops->target.raw);
950b4b6b78SThomas Richter out_free_source:
960b4b6b78SThomas Richter 	zfree(&ops->source.raw);
970b4b6b78SThomas Richter 	return -1;
980b4b6b78SThomas Richter }
990b4b6b78SThomas Richter 
1000b4b6b78SThomas Richter 
1010b4b6b78SThomas Richter static struct ins_ops s390_mov_ops = {
1020b4b6b78SThomas Richter 	.parse	   = s390_mov__parse,
1030b4b6b78SThomas Richter 	.scnprintf = mov__scnprintf,
1040b4b6b78SThomas Richter };
1050b4b6b78SThomas Richter 
s390__associate_ins_ops(struct arch * arch,const char * name)1060b4b6b78SThomas Richter static struct ins_ops *s390__associate_ins_ops(struct arch *arch, const char *name)
1070b4b6b78SThomas Richter {
1080b4b6b78SThomas Richter 	struct ins_ops *ops = NULL;
109d9f8dfa9SChristian Borntraeger 
110d9f8dfa9SChristian Borntraeger 	/* catch all kind of jumps */
111d9f8dfa9SChristian Borntraeger 	if (strchr(name, 'j') ||
112d9f8dfa9SChristian Borntraeger 	    !strncmp(name, "bct", 3) ||
113d9f8dfa9SChristian Borntraeger 	    !strncmp(name, "br", 2))
114d9f8dfa9SChristian Borntraeger 		ops = &jump_ops;
115d9f8dfa9SChristian Borntraeger 	/* override call/returns */
116d9f8dfa9SChristian Borntraeger 	if (!strcmp(name, "bras") ||
117d9f8dfa9SChristian Borntraeger 	    !strcmp(name, "brasl") ||
118d9f8dfa9SChristian Borntraeger 	    !strcmp(name, "basr"))
119d9f8dfa9SChristian Borntraeger 		ops = &s390_call_ops;
120d9f8dfa9SChristian Borntraeger 	if (!strcmp(name, "br"))
121d9f8dfa9SChristian Borntraeger 		ops = &ret_ops;
1220b58a77cSThomas Richter 	/* override load/store relative to PC */
123d9f8dfa9SChristian Borntraeger 	if (!strcmp(name, "lrl") ||
124d9f8dfa9SChristian Borntraeger 	    !strcmp(name, "lgrl") ||
1250b4b6b78SThomas Richter 	    !strcmp(name, "lgfrl") ||
1260b4b6b78SThomas Richter 	    !strcmp(name, "llgfrl") ||
1270b4b6b78SThomas Richter 	    !strcmp(name, "strl") ||
1280b4b6b78SThomas Richter 	    !strcmp(name, "stgrl"))
1290b4b6b78SThomas Richter 		ops = &s390_mov_ops;
1300b4b6b78SThomas Richter 
1310b4b6b78SThomas Richter 	if (ops)
1320b4b6b78SThomas Richter 		arch__associate_ins_ops(arch, name, ops);
133d9f8dfa9SChristian Borntraeger 	return ops;
13436c26360SThomas Richter }
135d9f8dfa9SChristian Borntraeger 
s390__cpuid_parse(struct arch * arch,char * cpuid)136d9f8dfa9SChristian Borntraeger static int s390__cpuid_parse(struct arch *arch, char *cpuid)
137d9f8dfa9SChristian Borntraeger {
138d9f8dfa9SChristian Borntraeger 	unsigned int family;
139c59124faSThomas Richter 	char model[16], model_c[16], cpumf_v[16], cpumf_a[16];
140c59124faSThomas Richter 	int ret;
141c59124faSThomas Richter 
142c59124faSThomas Richter 	/*
143c59124faSThomas Richter 	 * cpuid string format:
144c59124faSThomas Richter 	 * "IBM,family,model-capacity,model[,cpum_cf-version,cpum_cf-authorization]"
145c59124faSThomas Richter 	 */
146c59124faSThomas Richter 	ret = sscanf(cpuid, "%*[^,],%u,%[^,],%[^,],%[^,],%s", &family, model_c,
147c59124faSThomas Richter 		     model, cpumf_v, cpumf_a);
148c59124faSThomas Richter 	if (ret >= 2) {
149c59124faSThomas Richter 		arch->family = family;
150c59124faSThomas Richter 		arch->model = 0;
151c59124faSThomas Richter 		return 0;
152c59124faSThomas Richter 	}
153c59124faSThomas Richter 
154c59124faSThomas Richter 	return -1;
155c59124faSThomas Richter }
156c59124faSThomas Richter 
s390__annotate_init(struct arch * arch,char * cpuid __maybe_unused)157c59124faSThomas Richter static int s390__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
158c59124faSThomas Richter {
159c59124faSThomas Richter 	int err = 0;
160696e2457SJiri Olsa 
161d9f8dfa9SChristian Borntraeger 	if (!arch->initialized) {
162c59124faSThomas Richter 		arch->initialized = true;
163c59124faSThomas Richter 		arch->associate_instruction_ops = s390__associate_ins_ops;
164d9f8dfa9SChristian Borntraeger 		if (cpuid) {
165d9f8dfa9SChristian Borntraeger 			if (s390__cpuid_parse(arch, cpuid))
166d9f8dfa9SChristian Borntraeger 				err = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING;
16742d7a910SArnaldo Carvalho de Melo 		}
16842d7a910SArnaldo Carvalho de Melo 	}
16942d7a910SArnaldo Carvalho de Melo 
17042d7a910SArnaldo Carvalho de Melo 	return err;
171d9f8dfa9SChristian Borntraeger }
172d9f8dfa9SChristian Borntraeger