1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/module.h> 3 #include <linux/kthread.h> 4 #include <linux/ftrace.h> 5 #include <asm/asm-offsets.h> 6 7 extern void my_direct_func1(void); 8 extern void my_direct_func2(void); 9 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 25 #ifdef CONFIG_X86_64 26 27 #include <asm/ibt.h> 28 29 asm ( 30 " .pushsection .text, \"ax\", @progbits\n" 31 " .type my_tramp1, @function\n" 32 " .globl my_tramp1\n" 33 " my_tramp1:" 34 ASM_ENDBR 35 " pushq %rbp\n" 36 " movq %rsp, %rbp\n" 37 " call my_direct_func1\n" 38 " leave\n" 39 " .size my_tramp1, .-my_tramp1\n" 40 ASM_RET 41 42 " .type my_tramp2, @function\n" 43 " .globl my_tramp2\n" 44 " my_tramp2:" 45 ASM_ENDBR 46 " pushq %rbp\n" 47 " movq %rsp, %rbp\n" 48 " call my_direct_func2\n" 49 " leave\n" 50 ASM_RET 51 " .size my_tramp2, .-my_tramp2\n" 52 " .popsection\n" 53 ); 54 55 #endif /* CONFIG_X86_64 */ 56 57 #ifdef CONFIG_S390 58 59 asm ( 60 " .pushsection .text, \"ax\", @progbits\n" 61 " .type my_tramp1, @function\n" 62 " .globl my_tramp1\n" 63 " my_tramp1:" 64 " lgr %r1,%r15\n" 65 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 66 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 67 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" 68 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" 69 " brasl %r14,my_direct_func1\n" 70 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" 71 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 72 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 73 " lgr %r1,%r0\n" 74 " br %r1\n" 75 " .size my_tramp1, .-my_tramp1\n" 76 " .type my_tramp2, @function\n" 77 " .globl my_tramp2\n" 78 " my_tramp2:" 79 " lgr %r1,%r15\n" 80 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 81 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 82 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" 83 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" 84 " brasl %r14,my_direct_func2\n" 85 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" 86 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 87 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 88 " lgr %r1,%r0\n" 89 " br %r1\n" 90 " .size my_tramp2, .-my_tramp2\n" 91 " .popsection\n" 92 ); 93 94 #endif /* CONFIG_S390 */ 95 96 static unsigned long my_tramp = (unsigned long)my_tramp1; 97 static unsigned long tramps[2] = { 98 (unsigned long)my_tramp1, 99 (unsigned long)my_tramp2, 100 }; 101 102 static int simple_thread(void *arg) 103 { 104 static int t; 105 int ret = 0; 106 107 while (!kthread_should_stop()) { 108 set_current_state(TASK_INTERRUPTIBLE); 109 schedule_timeout(2 * HZ); 110 111 if (ret) 112 continue; 113 t ^= 1; 114 ret = modify_ftrace_direct(my_ip, my_tramp, tramps[t]); 115 if (!ret) 116 my_tramp = tramps[t]; 117 WARN_ON_ONCE(ret); 118 } 119 120 return 0; 121 } 122 123 static struct task_struct *simple_tsk; 124 125 static int __init ftrace_direct_init(void) 126 { 127 int ret; 128 129 ret = register_ftrace_direct(my_ip, my_tramp); 130 if (!ret) 131 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn"); 132 return ret; 133 } 134 135 static void __exit ftrace_direct_exit(void) 136 { 137 kthread_stop(simple_tsk); 138 unregister_ftrace_direct(my_ip, my_tramp); 139 } 140 141 module_init(ftrace_direct_init); 142 module_exit(ftrace_direct_exit); 143 144 MODULE_AUTHOR("Steven Rostedt"); 145 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()"); 146 MODULE_LICENSE("GPL"); 147