1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/string.h>
7 #include <internal/lib.h> // page_size
8 #include "../../../util/machine.h"
9 #include "../../../util/map.h"
10 #include "../../../util/symbol.h"
11 #include <linux/ctype.h>
13 #include <symbol/kallsyms.h>
15 #if defined(__x86_64__)
17 struct extra_kernel_map_info
{
20 struct extra_kernel_map
*maps
;
21 bool get_entry_trampolines
;
25 static int add_extra_kernel_map(struct extra_kernel_map_info
*mi
, u64 start
,
26 u64 end
, u64 pgoff
, const char *name
)
28 if (mi
->cnt
>= mi
->max_cnt
) {
32 mi
->max_cnt
= mi
->max_cnt
? mi
->max_cnt
* 2 : 32;
33 sz
= sizeof(struct extra_kernel_map
) * mi
->max_cnt
;
34 buf
= realloc(mi
->maps
, sz
);
40 mi
->maps
[mi
->cnt
].start
= start
;
41 mi
->maps
[mi
->cnt
].end
= end
;
42 mi
->maps
[mi
->cnt
].pgoff
= pgoff
;
43 strlcpy(mi
->maps
[mi
->cnt
].name
, name
, KMAP_NAME_LEN
);
50 static int find_extra_kernel_maps(void *arg
, const char *name
, char type
,
53 struct extra_kernel_map_info
*mi
= arg
;
55 if (!mi
->entry_trampoline
&& kallsyms2elf_binding(type
) == STB_GLOBAL
&&
56 !strcmp(name
, "_entry_trampoline")) {
57 mi
->entry_trampoline
= start
;
61 if (is_entry_trampoline(name
)) {
62 u64 end
= start
+ page_size
;
64 return add_extra_kernel_map(mi
, start
, end
, 0, name
);
70 int machine__create_extra_kernel_maps(struct machine
*machine
,
73 struct extra_kernel_map_info mi
= { .cnt
= 0, };
74 char filename
[PATH_MAX
];
78 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
80 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
83 ret
= kallsyms__parse(filename
, &mi
, find_extra_kernel_maps
);
87 if (!mi
.entry_trampoline
)
90 for (i
= 0; i
< mi
.cnt
; i
++) {
91 struct extra_kernel_map
*xm
= &mi
.maps
[i
];
93 xm
->pgoff
= mi
.entry_trampoline
;
94 ret
= machine__create_extra_kernel_map(machine
, kernel
, xm
);
99 machine
->trampolines_mapped
= mi
.cnt
;