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