xref: /linux/tools/perf/arch/s390/annotate/instructions.c (revision c34e9ab9a612ee8b18273398ef75c207b01f516d)
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, struct disasm_line *dl __maybe_unused)
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 			   struct disasm_line *dl __maybe_unused)
57 {
58 	char *s = strchr(ops->raw, ','), *target, *endptr;
59 
60 	if (s == NULL)
61 		return -1;
62 
63 	*s = '\0';
64 	ops->source.raw = strdup(ops->raw);
65 	*s = ',';
66 
67 	if (ops->source.raw == NULL)
68 		return -1;
69 
70 	target = ++s;
71 	ops->target.raw = strdup(target);
72 	if (ops->target.raw == NULL)
73 		goto out_free_source;
74 
75 	ops->target.addr = strtoull(target, &endptr, 16);
76 	if (endptr == target)
77 		goto out_free_target;
78 
79 	s = strchr(endptr, '<');
80 	if (s == NULL)
81 		goto out_free_target;
82 	endptr = strchr(s + 1, '>');
83 	if (endptr == NULL)
84 		goto out_free_target;
85 
86 	*endptr = '\0';
87 	ops->target.name = strdup(s + 1);
88 	*endptr = '>';
89 	if (ops->target.name == NULL)
90 		goto out_free_target;
91 
92 	return 0;
93 
94 out_free_target:
95 	zfree(&ops->target.raw);
96 out_free_source:
97 	zfree(&ops->source.raw);
98 	return -1;
99 }
100 
101 
102 static struct ins_ops s390_mov_ops = {
103 	.parse	   = s390_mov__parse,
104 	.scnprintf = mov__scnprintf,
105 };
106 
107 static struct ins_ops *s390__associate_ins_ops(struct arch *arch, const char *name)
108 {
109 	struct ins_ops *ops = NULL;
110 
111 	/* catch all kind of jumps */
112 	if (strchr(name, 'j') ||
113 	    !strncmp(name, "bct", 3) ||
114 	    !strncmp(name, "br", 2))
115 		ops = &jump_ops;
116 	/* override call/returns */
117 	if (!strcmp(name, "bras") ||
118 	    !strcmp(name, "brasl") ||
119 	    !strcmp(name, "basr"))
120 		ops = &s390_call_ops;
121 	if (!strcmp(name, "br"))
122 		ops = &ret_ops;
123 	/* override load/store relative to PC */
124 	if (!strcmp(name, "lrl") ||
125 	    !strcmp(name, "lgrl") ||
126 	    !strcmp(name, "lgfrl") ||
127 	    !strcmp(name, "llgfrl") ||
128 	    !strcmp(name, "strl") ||
129 	    !strcmp(name, "stgrl"))
130 		ops = &s390_mov_ops;
131 
132 	if (ops)
133 		arch__associate_ins_ops(arch, name, ops);
134 	return ops;
135 }
136 
137 static int s390__cpuid_parse(struct arch *arch, char *cpuid)
138 {
139 	unsigned int family;
140 	char model[16], model_c[16], cpumf_v[16], cpumf_a[16];
141 	int ret;
142 
143 	/*
144 	 * cpuid string format:
145 	 * "IBM,family,model-capacity,model[,cpum_cf-version,cpum_cf-authorization]"
146 	 */
147 	ret = sscanf(cpuid, "%*[^,],%u,%[^,],%[^,],%[^,],%s", &family, model_c,
148 		     model, cpumf_v, cpumf_a);
149 	if (ret >= 2) {
150 		arch->family = family;
151 		arch->model = 0;
152 		return 0;
153 	}
154 
155 	return -1;
156 }
157 
158 static int s390__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
159 {
160 	int err = 0;
161 
162 	if (!arch->initialized) {
163 		arch->initialized = true;
164 		arch->associate_instruction_ops = s390__associate_ins_ops;
165 		if (cpuid) {
166 			if (s390__cpuid_parse(arch, cpuid))
167 				err = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING;
168 		}
169 		arch->e_machine = EM_S390;
170 		arch->e_flags = 0;
171 	}
172 
173 	return err;
174 }
175