1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Perf annotate functions.
4  *
5  * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
6  */
7 
loongarch_call__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)8 static int loongarch_call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
9 {
10 	char *c, *endptr, *tok, *name;
11 	struct map *map = ms->map;
12 	struct addr_map_symbol target = {
13 		.ms = { .map = map, },
14 	};
15 
16 	c = strchr(ops->raw, '#');
17 	if (c++ == NULL)
18 		return -1;
19 
20 	ops->target.addr = strtoull(c, &endptr, 16);
21 
22 	name = strchr(endptr, '<');
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 
40 	target.addr = map__objdump_2mem(map, ops->target.addr);
41 
42 	if (maps__find_ams(ms->maps, &target) == 0 &&
43 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
44 		ops->target.sym = target.ms.sym;
45 
46 	return 0;
47 }
48 
49 static struct ins_ops loongarch_call_ops = {
50 	.parse	   = loongarch_call__parse,
51 	.scnprintf = call__scnprintf,
52 };
53 
loongarch_jump__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)54 static int loongarch_jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
55 {
56 	struct map *map = ms->map;
57 	struct symbol *sym = ms->sym;
58 	struct addr_map_symbol target = {
59 		.ms = { .map = map, },
60 	};
61 	const char *c = strchr(ops->raw, '#');
62 	u64 start, end;
63 
64 	ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char);
65 	ops->raw_func_start = strchr(ops->raw, '<');
66 
67 	if (ops->raw_func_start && c > ops->raw_func_start)
68 		c = NULL;
69 
70 	if (c++ != NULL)
71 		ops->target.addr = strtoull(c, NULL, 16);
72 	else
73 		ops->target.addr = strtoull(ops->raw, NULL, 16);
74 
75 	target.addr = map__objdump_2mem(map, ops->target.addr);
76 	start = map__unmap_ip(map, sym->start);
77 	end = map__unmap_ip(map, sym->end);
78 
79 	ops->target.outside = target.addr < start || target.addr > end;
80 
81 	if (maps__find_ams(ms->maps, &target) == 0 &&
82 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
83 		ops->target.sym = target.ms.sym;
84 
85 	if (!ops->target.outside) {
86 		ops->target.offset = target.addr - start;
87 		ops->target.offset_avail = true;
88 	} else {
89 		ops->target.offset_avail = false;
90 	}
91 
92 	return 0;
93 }
94 
95 static struct ins_ops loongarch_jump_ops = {
96 	.parse	   = loongarch_jump__parse,
97 	.scnprintf = jump__scnprintf,
98 };
99 
100 static
loongarch__associate_ins_ops(struct arch * arch,const char * name)101 struct ins_ops *loongarch__associate_ins_ops(struct arch *arch, const char *name)
102 {
103 	struct ins_ops *ops = NULL;
104 
105 	if (!strcmp(name, "bl"))
106 		ops = &loongarch_call_ops;
107 	else if (!strcmp(name, "jirl"))
108 		ops = &ret_ops;
109 	else if (!strcmp(name, "b") ||
110 		 !strncmp(name, "beq", 3) ||
111 		 !strncmp(name, "bne", 3) ||
112 		 !strncmp(name, "blt", 3) ||
113 		 !strncmp(name, "bge", 3) ||
114 		 !strncmp(name, "bltu", 4) ||
115 		 !strncmp(name, "bgeu", 4))
116 		ops = &loongarch_jump_ops;
117 	else
118 		return NULL;
119 
120 	arch__associate_ins_ops(arch, name, ops);
121 
122 	return ops;
123 }
124 
125 static
loongarch__annotate_init(struct arch * arch,char * cpuid __maybe_unused)126 int loongarch__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
127 {
128 	if (!arch->initialized) {
129 		arch->associate_instruction_ops = loongarch__associate_ins_ops;
130 		arch->initialized = true;
131 		arch->objdump.comment_char = '#';
132 	}
133 
134 	return 0;
135 }
136