1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
5 * Wrapper around kallsyms. Using kprobes to get its address when available.
7 * Can we mainline LTTng already so we don't have to waste our time doing this
10 * Copyright (C) 2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <linux/kprobes.h>
14 #include <linux/module.h>
15 #include <wrapper/kallsyms.h>
17 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,7,0))
19 #ifndef CONFIG_KPROBES
20 # error "LTTng-modules requires CONFIG_KPROBES on kernels >= 5.7.0"
23 #ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V1
24 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
27 #define LTTNG_FUNC_DESC_TYPE struct func_desc
28 #define LTTNG_FUNC_DESC_ADDR_NAME addr
32 #include <asm/types.h>
34 #define LTTNG_FUNC_DESC_TYPE func_descr_t
35 #define LTTNG_FUNC_DESC_ADDR_NAME entry
39 LTTNG_FUNC_DESC_TYPE kallsyms_lookup_name_func_desc
;
43 unsigned long (*kallsyms_lookup_name_sym
)(const char *name
);
46 int dummy_kprobe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
52 unsigned long do_get_kallsyms(void)
58 memset(&probe
, 0, sizeof(probe
));
59 probe
.pre_handler
= dummy_kprobe_handler
;
60 probe
.symbol_name
= "kallsyms_lookup_name";
61 #ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V2
63 * With powerpc64 ABIv2, we need the global entry point of
64 * kallsyms_lookup_name to call it later, while kprobe_register would
65 * automatically adjust the global entry point to the local entry point,
66 * when a kprobe was registered at a function entry. So we add 4 bytes
67 * which is the length of one instruction to kallsyms_lookup_name to
68 * avoid the adjustment.
72 ret
= register_kprobe(&probe
);
75 #ifdef LTTNG_CONFIG_PPC64_ELF_ABI_V2
76 /* Substract 4 bytes to get what we originally want */
77 addr
= (unsigned long)(((char *)probe
.addr
) - 4);
78 #elif defined(LTTNG_CONFIG_PPC64_ELF_ABI_V1)
80 * Build a function descriptor from the address of
81 * 'kallsyms_lookup_name' returned by kprobe and the toc of
82 * 'sprint_symbol' which is in the same compile unit and exported. I
83 * hate this on so many levels but it works.
85 kallsyms_lookup_name_func_desc
.LTTNG_FUNC_DESC_ADDR_NAME
= (unsigned long) probe
.addr
;
86 kallsyms_lookup_name_func_desc
.toc
= ((LTTNG_FUNC_DESC_TYPE
*) &sprint_symbol
)->toc
;
87 addr
= (unsigned long) &kallsyms_lookup_name_func_desc
;
89 addr
= (unsigned long)probe
.addr
;
92 #ifdef CONFIG_THUMB2_KERNEL
94 addr
|= 1; /* set bit 0 in address for thumb mode */
97 unregister_kprobe(&probe
);
101 unsigned long wrapper_kallsyms_lookup_name(const char *name
)
103 if (!kallsyms_lookup_name_sym
) {
104 kallsyms_lookup_name_sym
= (void *)do_get_kallsyms();
106 if (kallsyms_lookup_name_sym
)
107 return kallsyms_lookup_name_sym(name
);
109 printk_once(KERN_WARNING
"LTTng: requires kallsyms_lookup_name\n");
113 EXPORT_SYMBOL_GPL(wrapper_kallsyms_lookup_name
);