1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/module.h> 2 #include <linux/module.h> 3 3 4 #include <linux/mm.h> /* for handle_mm_fault() 4 #include <linux/mm.h> /* for handle_mm_fault() */ 5 #include <linux/ftrace.h> 5 #include <linux/ftrace.h> 6 #include <linux/sched/stat.h> 6 #include <linux/sched/stat.h> 7 #ifndef CONFIG_ARM64 7 #ifndef CONFIG_ARM64 8 #include <asm/asm-offsets.h> 8 #include <asm/asm-offsets.h> 9 #endif 9 #endif 10 10 11 extern void my_direct_func(unsigned long ip); 11 extern void my_direct_func(unsigned long ip); 12 12 13 void my_direct_func(unsigned long ip) 13 void my_direct_func(unsigned long ip) 14 { 14 { 15 trace_printk("ip %lx\n", ip); 15 trace_printk("ip %lx\n", ip); 16 } 16 } 17 17 18 extern void my_tramp(void *); 18 extern void my_tramp(void *); 19 19 20 #ifdef CONFIG_RISCV << 21 #include <asm/asm.h> << 22 << 23 asm ( << 24 " .pushsection .text, \"ax\", @progbi << 25 " .type my_tramp, @function\n" << 26 " .globl my_tramp\n" << 27 " my_tramp:\n" << 28 " addi sp,sp,-3*"SZREG"\n" << 29 " "REG_S" a0,0*"SZREG"(sp)\n" << 30 " "REG_S" t0,1*"SZREG"(sp)\n" << 31 " "REG_S" ra,2*"SZREG"(sp)\n" << 32 " mv a0,t0\n" << 33 " call my_direct_func\n" << 34 " "REG_L" a0,0*"SZREG"(sp)\n" << 35 " "REG_L" t0,1*"SZREG"(sp)\n" << 36 " "REG_L" ra,2*"SZREG"(sp)\n" << 37 " addi sp,sp,3*"SZREG"\n" << 38 " jr t0\n" << 39 " .size my_tramp, .-my_tramp\n << 40 " .popsection\n" << 41 ); << 42 << 43 #endif /* CONFIG_RISCV */ << 44 << 45 #ifdef CONFIG_X86_64 20 #ifdef CONFIG_X86_64 46 21 47 #include <asm/ibt.h> 22 #include <asm/ibt.h> 48 #include <asm/nospec-branch.h> 23 #include <asm/nospec-branch.h> 49 24 50 asm ( 25 asm ( 51 " .pushsection .text, \"ax\", @progbi 26 " .pushsection .text, \"ax\", @progbits\n" 52 " .type my_tramp, @function\n" 27 " .type my_tramp, @function\n" 53 " .globl my_tramp\n" 28 " .globl my_tramp\n" 54 " my_tramp:" 29 " my_tramp:" 55 ASM_ENDBR 30 ASM_ENDBR 56 " pushq %rbp\n" 31 " pushq %rbp\n" 57 " movq %rsp, %rbp\n" 32 " movq %rsp, %rbp\n" 58 CALL_DEPTH_ACCOUNT 33 CALL_DEPTH_ACCOUNT 59 " pushq %rdi\n" 34 " pushq %rdi\n" 60 " movq 8(%rbp), %rdi\n" 35 " movq 8(%rbp), %rdi\n" 61 " call my_direct_func\n" 36 " call my_direct_func\n" 62 " popq %rdi\n" 37 " popq %rdi\n" 63 " leave\n" 38 " leave\n" 64 ASM_RET 39 ASM_RET 65 " .size my_tramp, .-my_tramp\n 40 " .size my_tramp, .-my_tramp\n" 66 " .popsection\n" 41 " .popsection\n" 67 ); 42 ); 68 43 69 #endif /* CONFIG_X86_64 */ 44 #endif /* CONFIG_X86_64 */ 70 45 71 #ifdef CONFIG_S390 46 #ifdef CONFIG_S390 72 47 73 asm ( 48 asm ( 74 " .pushsection .text, \"ax\", @progbi 49 " .pushsection .text, \"ax\", @progbits\n" 75 " .type my_tramp, @function\n" 50 " .type my_tramp, @function\n" 76 " .globl my_tramp\n" 51 " .globl my_tramp\n" 77 " my_tramp:" 52 " my_tramp:" 78 " lgr %r1,%r15\n" 53 " lgr %r1,%r15\n" 79 " stmg %r0,%r5,"__stringify(_ 54 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 80 " stg %r14,"__stringify(__SF 55 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 81 " aghi %r15,"__stringify(-STA 56 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" 82 " stg %r1,"__stringify(__SF_ 57 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" 83 " lgr %r2,%r0\n" 58 " lgr %r2,%r0\n" 84 " brasl %r14,my_direct_func\n" 59 " brasl %r14,my_direct_func\n" 85 " aghi %r15,"__stringify(STAC 60 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" 86 " lmg %r0,%r5,"__stringify(_ 61 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 87 " lg %r14,"__stringify(__SF 62 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 88 " lgr %r1,%r0\n" 63 " lgr %r1,%r0\n" 89 " br %r1\n" 64 " br %r1\n" 90 " .size my_tramp, .-my_tramp\n 65 " .size my_tramp, .-my_tramp\n" 91 " .popsection\n" 66 " .popsection\n" 92 ); 67 ); 93 68 94 #endif /* CONFIG_S390 */ 69 #endif /* CONFIG_S390 */ 95 70 96 #ifdef CONFIG_ARM64 71 #ifdef CONFIG_ARM64 97 72 98 asm ( 73 asm ( 99 " .pushsection .text, \"ax\", @progbi 74 " .pushsection .text, \"ax\", @progbits\n" 100 " .type my_tramp, @function\n" 75 " .type my_tramp, @function\n" 101 " .globl my_tramp\n" 76 " .globl my_tramp\n" 102 " my_tramp:" 77 " my_tramp:" 103 " hint 34\n" // bti c 78 " hint 34\n" // bti c 104 " sub sp, sp, #32\n" 79 " sub sp, sp, #32\n" 105 " stp x9, x30, [sp]\n" 80 " stp x9, x30, [sp]\n" 106 " str x0, [sp, #16]\n" 81 " str x0, [sp, #16]\n" 107 " mov x0, x30\n" 82 " mov x0, x30\n" 108 " bl my_direct_func\n" 83 " bl my_direct_func\n" 109 " ldp x30, x9, [sp]\n" 84 " ldp x30, x9, [sp]\n" 110 " ldr x0, [sp, #16]\n" 85 " ldr x0, [sp, #16]\n" 111 " add sp, sp, #32\n" 86 " add sp, sp, #32\n" 112 " ret x9\n" 87 " ret x9\n" 113 " .size my_tramp, .-my_tramp\n 88 " .size my_tramp, .-my_tramp\n" 114 " .popsection\n" 89 " .popsection\n" 115 ); 90 ); 116 91 117 #endif /* CONFIG_ARM64 */ 92 #endif /* CONFIG_ARM64 */ 118 93 119 #ifdef CONFIG_LOONGARCH 94 #ifdef CONFIG_LOONGARCH 120 95 121 #include <asm/asm.h> 96 #include <asm/asm.h> 122 asm ( 97 asm ( 123 " .pushsection .text, \"ax\", @progbi 98 " .pushsection .text, \"ax\", @progbits\n" 124 " .type my_tramp, @function\n" 99 " .type my_tramp, @function\n" 125 " .globl my_tramp\n" 100 " .globl my_tramp\n" 126 " my_tramp:\n" 101 " my_tramp:\n" 127 " addi.d $sp, $sp, -32\n" 102 " addi.d $sp, $sp, -32\n" 128 " st.d $a0, $sp, 0\n" 103 " st.d $a0, $sp, 0\n" 129 " st.d $t0, $sp, 8\n" 104 " st.d $t0, $sp, 8\n" 130 " st.d $ra, $sp, 16\n" 105 " st.d $ra, $sp, 16\n" 131 " move $a0, $t0\n" 106 " move $a0, $t0\n" 132 " bl my_direct_func\n" 107 " bl my_direct_func\n" 133 " ld.d $a0, $sp, 0\n" 108 " ld.d $a0, $sp, 0\n" 134 " ld.d $t0, $sp, 8\n" 109 " ld.d $t0, $sp, 8\n" 135 " ld.d $ra, $sp, 16\n" 110 " ld.d $ra, $sp, 16\n" 136 " addi.d $sp, $sp, 32\n" 111 " addi.d $sp, $sp, 32\n" 137 " jr $t0\n" 112 " jr $t0\n" 138 " .size my_tramp, .-my_tramp\n 113 " .size my_tramp, .-my_tramp\n" 139 " .popsection\n" 114 " .popsection\n" 140 ); 115 ); 141 116 142 #endif /* CONFIG_LOONGARCH */ 117 #endif /* CONFIG_LOONGARCH */ 143 118 144 static struct ftrace_ops direct; 119 static struct ftrace_ops direct; 145 120 146 static int __init ftrace_direct_multi_init(voi 121 static int __init ftrace_direct_multi_init(void) 147 { 122 { 148 ftrace_set_filter_ip(&direct, (unsigne 123 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0); 149 ftrace_set_filter_ip(&direct, (unsigne 124 ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0); 150 125 151 return register_ftrace_direct(&direct, 126 return register_ftrace_direct(&direct, (unsigned long) my_tramp); 152 } 127 } 153 128 154 static void __exit ftrace_direct_multi_exit(vo 129 static void __exit ftrace_direct_multi_exit(void) 155 { 130 { 156 unregister_ftrace_direct(&direct, (uns 131 unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true); 157 } 132 } 158 133 159 module_init(ftrace_direct_multi_init); 134 module_init(ftrace_direct_multi_init); 160 module_exit(ftrace_direct_multi_exit); 135 module_exit(ftrace_direct_multi_exit); 161 136 162 MODULE_AUTHOR("Jiri Olsa"); 137 MODULE_AUTHOR("Jiri Olsa"); 163 MODULE_DESCRIPTION("Example use case of using 138 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()"); 164 MODULE_LICENSE("GPL"); 139 MODULE_LICENSE("GPL"); 165 140
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.