| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Perf annotate functions. |
| 4 |
* |
| 5 |
* Copyright (C) 2020-2023 Loongson Technology Corporation Limited |
| 6 |
*/ |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <linux/compiler.h> |
| 10 |
#include <linux/zalloc.h> |
| 11 |
#include "../disasm.h" |
| 12 |
#include "../map.h" |
| 13 |
#include "../maps.h" |
| 14 |
#include "../symbol.h" |
| 15 |
#include "../thread.h" |
| 16 |
|
| 17 |
static int loongarch_call__parse(const struct arch *arch, struct ins_operands *ops, |
| 18 |
struct map_symbol *ms, |
| 19 |
struct disasm_line *dl __maybe_unused) |
| 20 |
{ |
| 21 |
char *c, *endptr, *tok, *name; |
| 22 |
struct map *map = ms->map; |
| 23 |
struct addr_map_symbol target; |
| 24 |
|
| 25 |
c = strchr(ops->raw, '#'); |
| 26 |
if (c++ == NULL) |
| 27 |
return -1; |
| 28 |
|
| 29 |
ops->target.addr = strtoull(c, &endptr, 16); |
| 30 |
|
| 31 |
name = strchr(endptr, '<'); |
| 32 |
name++; |
| 33 |
|
| 34 |
if (arch->objdump.skip_functions_char && |
| 35 |
strchr(name, arch->objdump.skip_functions_char)) |
| 36 |
return -1; |
| 37 |
|
| 38 |
tok = strchr(name, '>'); |
| 39 |
if (tok == NULL) |
| 40 |
return -1; |
| 41 |
|
| 42 |
*tok = '\0'; |
| 43 |
ops->target.name = strdup(name); |
| 44 |
*tok = '>'; |
| 45 |
|
| 46 |
if (ops->target.name == NULL) |
| 47 |
return -1; |
| 48 |
|
| 49 |
target = (struct addr_map_symbol) { |
| 50 |
.ms = { .map = map__get(map), }, |
| 51 |
.addr = map__objdump_2mem(map, ops->target.addr), |
| 52 |
}; |
| 53 |
|
| 54 |
if (maps__find_ams(thread__maps(ms->thread), &target) == 0 && |
| 55 |
map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) |
| 56 |
ops->target.sym = target.ms.sym; |
| 57 |
|
| 58 |
addr_map_symbol__exit(&target); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static const struct ins_ops loongarch_call_ops = { |
| 63 |
.parse = loongarch_call__parse, |
| 64 |
.scnprintf = call__scnprintf, |
| 65 |
.is_call = true, |
| 66 |
}; |
| 67 |
|
| 68 |
static int loongarch_jump__parse(const struct arch *arch, struct ins_operands *ops, |
| 69 |
struct map_symbol *ms, |
| 70 |
struct disasm_line *dl __maybe_unused) |
| 71 |
|
| 72 |
{ |
| 73 |
struct map *map = ms->map; |
| 74 |
struct symbol *sym = ms->sym; |
| 75 |
struct addr_map_symbol target = { |
| 76 |
.ms = { .map = map__get(map), }, |
| 77 |
}; |
| 78 |
const char *c = strchr(ops->raw, '#'); |
| 79 |
u64 start, end; |
| 80 |
|
| 81 |
ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char); |
| 82 |
ops->jump.raw_func_start = strchr(ops->raw, '<'); |
| 83 |
|
| 84 |
if (ops->jump.raw_func_start && c > ops->jump.raw_func_start) |
| 85 |
c = NULL; |
| 86 |
|
| 87 |
if (c++ != NULL) |
| 88 |
ops->target.addr = strtoull(c, NULL, 16); |
| 89 |
else |
| 90 |
ops->target.addr = strtoull(ops->raw, NULL, 16); |
| 91 |
|
| 92 |
target.addr = map__objdump_2mem(map, ops->target.addr); |
| 93 |
start = map__unmap_ip(map, sym->start); |
| 94 |
end = map__unmap_ip(map, sym->end); |
| 95 |
|
| 96 |
ops->target.outside = target.addr < start || target.addr >= end; |
| 97 |
|
| 98 |
if (maps__find_ams(thread__maps(ms->thread), &target) == 0 && |
| 99 |
map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) |
| 100 |
ops->target.sym = target.ms.sym; |
| 101 |
|
| 102 |
if (!ops->target.outside) { |
| 103 |
ops->target.offset = target.addr - start; |
| 104 |
ops->target.offset_avail = true; |
| 105 |
} else { |
| 106 |
ops->target.offset_avail = false; |
| 107 |
} |
| 108 |
addr_map_symbol__exit(&target); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static const struct ins_ops loongarch_jump_ops = { |
| 113 |
.free = jump__delete, |
| 114 |
.parse = loongarch_jump__parse, |
| 115 |
.scnprintf = jump__scnprintf, |
| 116 |
.is_jump = true, |
| 117 |
}; |
| 118 |
|
| 119 |
static |
| 120 |
const struct ins_ops *loongarch__associate_ins_ops(struct arch *arch, const char *name) |
| 121 |
{ |
| 122 |
const struct ins_ops *ops = NULL; |
| 123 |
|
| 124 |
if (!strcmp(name, "bl")) |
| 125 |
ops = &loongarch_call_ops; |
| 126 |
else if (!strcmp(name, "jirl")) |
| 127 |
ops = &ret_ops; |
| 128 |
else if (!strcmp(name, "b") || |
| 129 |
!strncmp(name, "beq", 3) || |
| 130 |
!strncmp(name, "bne", 3) || |
| 131 |
!strncmp(name, "blt", 3) || |
| 132 |
!strncmp(name, "bge", 3) || |
| 133 |
!strncmp(name, "bltu", 4) || |
| 134 |
!strncmp(name, "bgeu", 4)) |
| 135 |
ops = &loongarch_jump_ops; |
| 136 |
else |
| 137 |
return NULL; |
| 138 |
|
| 139 |
arch__associate_ins_ops(arch, name, ops); |
| 140 |
|
| 141 |
return ops; |
| 142 |
} |
| 143 |
|
| 144 |
const struct arch *arch__new_loongarch(const struct e_machine_and_e_flags *id, |
| 145 |
const char *cpuid __maybe_unused) |
| 146 |
{ |
| 147 |
struct arch *arch = zalloc(sizeof(*arch)); |
| 148 |
|
| 149 |
if (!arch) |
| 150 |
return NULL; |
| 151 |
|
| 152 |
arch->name = "loongarch"; |
| 153 |
arch->id = *id; |
| 154 |
arch->associate_instruction_ops = loongarch__associate_ins_ops; |
| 155 |
arch->objdump.comment_char = '#'; |
| 156 |
return arch; |
| 157 |
} |