blob: 2c7c31893551dda8c9b06573a55221c315d59712 [file] [log] [blame]
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -05001// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/module.h>
3#include <linux/kthread.h>
4#include <linux/ftrace.h>
Heiko Carstens1254cfbc2021-10-12 15:38:02 +02005#include <asm/asm-offsets.h>
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -05006
Jiri Olsa0daf5cb2021-12-19 14:53:17 +01007extern void my_direct_func1(void);
8extern void my_direct_func2(void);
9
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050010void my_direct_func1(void)
11{
12 trace_printk("my direct func1\n");
13}
14
15void my_direct_func2(void)
16{
17 trace_printk("my direct func2\n");
18}
19
20extern void my_tramp1(void *);
21extern void my_tramp2(void *);
22
23static unsigned long my_ip = (unsigned long)schedule;
24
Heiko Carstens1254cfbc2021-10-12 15:38:02 +020025#ifdef CONFIG_X86_64
26
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050027asm (
28" .pushsection .text, \"ax\", @progbits\n"
Josh Poimboeuf9d907f12020-04-24 15:40:43 -050029" .type my_tramp1, @function\n"
Sami Tolvanen983df5f2020-11-13 10:34:14 -080030" .globl my_tramp1\n"
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050031" my_tramp1:"
32" pushq %rbp\n"
33" movq %rsp, %rbp\n"
34" call my_direct_func1\n"
35" leave\n"
Josh Poimboeuf9d907f12020-04-24 15:40:43 -050036" .size my_tramp1, .-my_tramp1\n"
Peter Zijlstrab17c2ba2021-12-04 14:43:41 +010037 ASM_RET
Josh Poimboeuf9d907f12020-04-24 15:40:43 -050038" .type my_tramp2, @function\n"
Sami Tolvanen983df5f2020-11-13 10:34:14 -080039" .globl my_tramp2\n"
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050040" my_tramp2:"
41" pushq %rbp\n"
42" movq %rsp, %rbp\n"
43" call my_direct_func2\n"
44" leave\n"
Peter Zijlstrab17c2ba2021-12-04 14:43:41 +010045 ASM_RET
Josh Poimboeuf9d907f12020-04-24 15:40:43 -050046" .size my_tramp2, .-my_tramp2\n"
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050047" .popsection\n"
48);
49
Heiko Carstens1254cfbc2021-10-12 15:38:02 +020050#endif /* CONFIG_X86_64 */
51
52#ifdef CONFIG_S390
53
54asm (
55" .pushsection .text, \"ax\", @progbits\n"
56" .type my_tramp1, @function\n"
57" .globl my_tramp1\n"
58" my_tramp1:"
59" lgr %r1,%r15\n"
60" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
61" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
62" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
63" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
64" brasl %r14,my_direct_func1\n"
65" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
66" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
67" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
68" lgr %r1,%r0\n"
69" br %r1\n"
70" .size my_tramp1, .-my_tramp1\n"
71" .type my_tramp2, @function\n"
72" .globl my_tramp2\n"
73" my_tramp2:"
74" lgr %r1,%r15\n"
75" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
76" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
77" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
78" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
79" brasl %r14,my_direct_func2\n"
80" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
81" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
82" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
83" lgr %r1,%r0\n"
84" br %r1\n"
85" .size my_tramp2, .-my_tramp2\n"
86" .popsection\n"
87);
88
89#endif /* CONFIG_S390 */
90
Steven Rostedt (VMware)ae0cc3b2019-11-14 14:41:47 -050091static unsigned long my_tramp = (unsigned long)my_tramp1;
92static unsigned long tramps[2] = {
93 (unsigned long)my_tramp1,
94 (unsigned long)my_tramp2,
95};
96
97static int simple_thread(void *arg)
98{
99 static int t;
100 int ret = 0;
101
102 while (!kthread_should_stop()) {
103 set_current_state(TASK_INTERRUPTIBLE);
104 schedule_timeout(2 * HZ);
105
106 if (ret)
107 continue;
108 t ^= 1;
109 ret = modify_ftrace_direct(my_ip, my_tramp, tramps[t]);
110 if (!ret)
111 my_tramp = tramps[t];
112 WARN_ON_ONCE(ret);
113 }
114
115 return 0;
116}
117
118static struct task_struct *simple_tsk;
119
120static int __init ftrace_direct_init(void)
121{
122 int ret;
123
124 ret = register_ftrace_direct(my_ip, my_tramp);
125 if (!ret)
126 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
127 return ret;
128}
129
130static void __exit ftrace_direct_exit(void)
131{
132 kthread_stop(simple_tsk);
133 unregister_ftrace_direct(my_ip, my_tramp);
134}
135
136module_init(ftrace_direct_init);
137module_exit(ftrace_direct_exit);
138
139MODULE_AUTHOR("Steven Rostedt");
140MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
141MODULE_LICENSE("GPL");