Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/kthread.h>
0004 #include <linux/ftrace.h>
0005 #include <asm/asm-offsets.h>
0006 
0007 extern void my_direct_func1(unsigned long ip);
0008 extern void my_direct_func2(unsigned long ip);
0009 
0010 void my_direct_func1(unsigned long ip)
0011 {
0012     trace_printk("my direct func1 ip %lx\n", ip);
0013 }
0014 
0015 void my_direct_func2(unsigned long ip)
0016 {
0017     trace_printk("my direct func2 ip %lx\n", ip);
0018 }
0019 
0020 extern void my_tramp1(void *);
0021 extern void my_tramp2(void *);
0022 
0023 #ifdef CONFIG_X86_64
0024 
0025 #include <asm/ibt.h>
0026 
0027 asm (
0028 "   .pushsection    .text, \"ax\", @progbits\n"
0029 "   .type       my_tramp1, @function\n"
0030 "   .globl      my_tramp1\n"
0031 "   my_tramp1:"
0032     ASM_ENDBR
0033 "   pushq %rbp\n"
0034 "   movq %rsp, %rbp\n"
0035 "   pushq %rdi\n"
0036 "   movq 8(%rbp), %rdi\n"
0037 "   call my_direct_func1\n"
0038 "   popq %rdi\n"
0039 "   leave\n"
0040     ASM_RET
0041 "   .size       my_tramp1, .-my_tramp1\n"
0042 
0043 "   .type       my_tramp2, @function\n"
0044 "   .globl      my_tramp2\n"
0045 "   my_tramp2:"
0046     ASM_ENDBR
0047 "   pushq %rbp\n"
0048 "   movq %rsp, %rbp\n"
0049 "   pushq %rdi\n"
0050 "   movq 8(%rbp), %rdi\n"
0051 "   call my_direct_func2\n"
0052 "   popq %rdi\n"
0053 "   leave\n"
0054     ASM_RET
0055 "   .size       my_tramp2, .-my_tramp2\n"
0056 "   .popsection\n"
0057 );
0058 
0059 #endif /* CONFIG_X86_64 */
0060 
0061 #ifdef CONFIG_S390
0062 
0063 asm (
0064 "       .pushsection    .text, \"ax\", @progbits\n"
0065 "       .type           my_tramp1, @function\n"
0066 "       .globl          my_tramp1\n"
0067 "   my_tramp1:"
0068 "       lgr             %r1,%r15\n"
0069 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
0070 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
0071 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
0072 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
0073 "       lgr             %r2,%r0\n"
0074 "       brasl           %r14,my_direct_func1\n"
0075 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
0076 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
0077 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
0078 "       lgr             %r1,%r0\n"
0079 "       br              %r1\n"
0080 "       .size           my_tramp1, .-my_tramp1\n"
0081 "\n"
0082 "       .type           my_tramp2, @function\n"
0083 "       .globl          my_tramp2\n"
0084 "   my_tramp2:"
0085 "       lgr             %r1,%r15\n"
0086 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
0087 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
0088 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
0089 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
0090 "       lgr             %r2,%r0\n"
0091 "       brasl           %r14,my_direct_func2\n"
0092 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
0093 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
0094 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
0095 "       lgr             %r1,%r0\n"
0096 "       br              %r1\n"
0097 "       .size           my_tramp2, .-my_tramp2\n"
0098 "       .popsection\n"
0099 );
0100 
0101 #endif /* CONFIG_S390 */
0102 
0103 static unsigned long my_tramp = (unsigned long)my_tramp1;
0104 static unsigned long tramps[2] = {
0105     (unsigned long)my_tramp1,
0106     (unsigned long)my_tramp2,
0107 };
0108 
0109 static struct ftrace_ops direct;
0110 
0111 static int simple_thread(void *arg)
0112 {
0113     static int t;
0114     int ret = 0;
0115 
0116     while (!kthread_should_stop()) {
0117         set_current_state(TASK_INTERRUPTIBLE);
0118         schedule_timeout(2 * HZ);
0119 
0120         if (ret)
0121             continue;
0122         t ^= 1;
0123         ret = modify_ftrace_direct_multi(&direct, tramps[t]);
0124         if (!ret)
0125             my_tramp = tramps[t];
0126         WARN_ON_ONCE(ret);
0127     }
0128 
0129     return 0;
0130 }
0131 
0132 static struct task_struct *simple_tsk;
0133 
0134 static int __init ftrace_direct_multi_init(void)
0135 {
0136     int ret;
0137 
0138     ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
0139     ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
0140 
0141     ret = register_ftrace_direct_multi(&direct, my_tramp);
0142 
0143     if (!ret)
0144         simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
0145     return ret;
0146 }
0147 
0148 static void __exit ftrace_direct_multi_exit(void)
0149 {
0150     kthread_stop(simple_tsk);
0151     unregister_ftrace_direct_multi(&direct, my_tramp);
0152 }
0153 
0154 module_init(ftrace_direct_multi_init);
0155 module_exit(ftrace_direct_multi_exit);
0156 
0157 MODULE_AUTHOR("Jiri Olsa");
0158 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct_multi()");
0159 MODULE_LICENSE("GPL");