1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/string.h>
6 #include "../../util/util.h"
7 #include "../../util/machine.h"
8 #include "../../util/map.h"
9 #include "../../util/symbol.h"
10 #include <linux/ctype.h>
12 #include <symbol/kallsyms.h>
14 #if defined(__x86_64__)
16 struct extra_kernel_map_info {
19 struct extra_kernel_map *maps;
20 bool get_entry_trampolines;
24 static int add_extra_kernel_map(struct extra_kernel_map_info *mi, u64 start,
25 u64 end, u64 pgoff, const char *name)
27 if (mi->cnt >= mi->max_cnt) {
31 mi->max_cnt = mi->max_cnt ? mi->max_cnt * 2 : 32;
32 sz = sizeof(struct extra_kernel_map) * mi->max_cnt;
33 buf = realloc(mi->maps, sz);
39 mi->maps[mi->cnt].start = start;
40 mi->maps[mi->cnt].end = end;
41 mi->maps[mi->cnt].pgoff = pgoff;
42 strlcpy(mi->maps[mi->cnt].name, name, KMAP_NAME_LEN);
49 static int find_extra_kernel_maps(void *arg, const char *name, char type,
52 struct extra_kernel_map_info *mi = arg;
54 if (!mi->entry_trampoline && kallsyms2elf_binding(type) == STB_GLOBAL &&
55 !strcmp(name, "_entry_trampoline")) {
56 mi->entry_trampoline = start;
60 if (is_entry_trampoline(name)) {
61 u64 end = start + page_size;
63 return add_extra_kernel_map(mi, start, end, 0, name);
69 int machine__create_extra_kernel_maps(struct machine *machine,
72 struct extra_kernel_map_info mi = { .cnt = 0, };
73 char filename[PATH_MAX];
77 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
79 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
82 ret = kallsyms__parse(filename, &mi, find_extra_kernel_maps);
86 if (!mi.entry_trampoline)
89 for (i = 0; i < mi.cnt; i++) {
90 struct extra_kernel_map *xm = &mi.maps[i];
92 xm->pgoff = mi.entry_trampoline;
93 ret = machine__create_extra_kernel_map(machine, kernel, xm);
98 machine->trampolines_mapped = mi.cnt;