Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/kthread.h> |
| 4 | #include <linux/ftrace.h> |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 5 | #include <asm/asm-offsets.h> |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 6 | |
Jiri Olsa | 0daf5cb | 2021-12-19 14:53:17 +0100 | [diff] [blame] | 7 | extern void my_direct_func1(void); |
| 8 | extern void my_direct_func2(void); |
| 9 | |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 10 | void my_direct_func1(void) |
| 11 | { |
| 12 | trace_printk("my direct func1\n"); |
| 13 | } |
| 14 | |
| 15 | void my_direct_func2(void) |
| 16 | { |
| 17 | trace_printk("my direct func2\n"); |
| 18 | } |
| 19 | |
| 20 | extern void my_tramp1(void *); |
| 21 | extern void my_tramp2(void *); |
| 22 | |
| 23 | static unsigned long my_ip = (unsigned long)schedule; |
| 24 | |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 25 | #ifdef CONFIG_X86_64 |
| 26 | |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 27 | asm ( |
| 28 | " .pushsection .text, \"ax\", @progbits\n" |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 29 | " .type my_tramp1, @function\n" |
Sami Tolvanen | 983df5f | 2020-11-13 10:34:14 -0800 | [diff] [blame] | 30 | " .globl my_tramp1\n" |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 31 | " my_tramp1:" |
| 32 | " pushq %rbp\n" |
| 33 | " movq %rsp, %rbp\n" |
| 34 | " call my_direct_func1\n" |
| 35 | " leave\n" |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 36 | " .size my_tramp1, .-my_tramp1\n" |
Peter Zijlstra | b17c2ba | 2021-12-04 14:43:41 +0100 | [diff] [blame] | 37 | ASM_RET |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 38 | " .type my_tramp2, @function\n" |
Sami Tolvanen | 983df5f | 2020-11-13 10:34:14 -0800 | [diff] [blame] | 39 | " .globl my_tramp2\n" |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 40 | " my_tramp2:" |
| 41 | " pushq %rbp\n" |
| 42 | " movq %rsp, %rbp\n" |
| 43 | " call my_direct_func2\n" |
| 44 | " leave\n" |
Peter Zijlstra | b17c2ba | 2021-12-04 14:43:41 +0100 | [diff] [blame] | 45 | ASM_RET |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 46 | " .size my_tramp2, .-my_tramp2\n" |
Steven Rostedt (VMware) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 47 | " .popsection\n" |
| 48 | ); |
| 49 | |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 50 | #endif /* CONFIG_X86_64 */ |
| 51 | |
| 52 | #ifdef CONFIG_S390 |
| 53 | |
| 54 | asm ( |
| 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) | ae0cc3b | 2019-11-14 14:41:47 -0500 | [diff] [blame] | 91 | static unsigned long my_tramp = (unsigned long)my_tramp1; |
| 92 | static unsigned long tramps[2] = { |
| 93 | (unsigned long)my_tramp1, |
| 94 | (unsigned long)my_tramp2, |
| 95 | }; |
| 96 | |
| 97 | static 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 | |
| 118 | static struct task_struct *simple_tsk; |
| 119 | |
| 120 | static 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 | |
| 130 | static void __exit ftrace_direct_exit(void) |
| 131 | { |
| 132 | kthread_stop(simple_tsk); |
| 133 | unregister_ftrace_direct(my_ip, my_tramp); |
| 134 | } |
| 135 | |
| 136 | module_init(ftrace_direct_init); |
| 137 | module_exit(ftrace_direct_exit); |
| 138 | |
| 139 | MODULE_AUTHOR("Steven Rostedt"); |
| 140 | MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()"); |
| 141 | MODULE_LICENSE("GPL"); |