samples: ftrace: Save required argument registers in sample trampolines
[platform/kernel/linux-starfive.git] / samples / ftrace / ftrace-direct-too.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3
4 #include <linux/mm.h> /* for handle_mm_fault() */
5 #include <linux/ftrace.h>
6 #include <asm/asm-offsets.h>
7
8 extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
9                            unsigned int flags, struct pt_regs *regs);
10
11 void my_direct_func(struct vm_area_struct *vma, unsigned long address,
12                     unsigned int flags, struct pt_regs *regs)
13 {
14         trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
15                      vma, address, flags, regs);
16 }
17
18 extern void my_tramp(void *);
19
20 #ifdef CONFIG_X86_64
21
22 #include <asm/ibt.h>
23 #include <asm/nospec-branch.h>
24
25 asm (
26 "       .pushsection    .text, \"ax\", @progbits\n"
27 "       .type           my_tramp, @function\n"
28 "       .globl          my_tramp\n"
29 "   my_tramp:"
30         ASM_ENDBR
31 "       pushq %rbp\n"
32 "       movq %rsp, %rbp\n"
33         CALL_DEPTH_ACCOUNT
34 "       pushq %rdi\n"
35 "       pushq %rsi\n"
36 "       pushq %rdx\n"
37 "       pushq %rcx\n"
38 "       call my_direct_func\n"
39 "       popq %rcx\n"
40 "       popq %rdx\n"
41 "       popq %rsi\n"
42 "       popq %rdi\n"
43 "       leave\n"
44         ASM_RET
45 "       .size           my_tramp, .-my_tramp\n"
46 "       .popsection\n"
47 );
48
49 #endif /* CONFIG_X86_64 */
50
51 #ifdef CONFIG_S390
52
53 asm (
54 "       .pushsection    .text, \"ax\", @progbits\n"
55 "       .type           my_tramp, @function\n"
56 "       .globl          my_tramp\n"
57 "   my_tramp:"
58 "       lgr             %r1,%r15\n"
59 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
60 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
61 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
62 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
63 "       brasl           %r14,my_direct_func\n"
64 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
65 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
66 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
67 "       lgr             %r1,%r0\n"
68 "       br              %r1\n"
69 "       .size           my_tramp, .-my_tramp\n"
70 "       .popsection\n"
71 );
72
73 #endif /* CONFIG_S390 */
74
75 #ifdef CONFIG_LOONGARCH
76
77 asm (
78 "       .pushsection    .text, \"ax\", @progbits\n"
79 "       .type           my_tramp, @function\n"
80 "       .globl          my_tramp\n"
81 "   my_tramp:\n"
82 "       addi.d  $sp, $sp, -48\n"
83 "       st.d    $a0, $sp, 0\n"
84 "       st.d    $a1, $sp, 8\n"
85 "       st.d    $a2, $sp, 16\n"
86 "       st.d    $t0, $sp, 24\n"
87 "       st.d    $ra, $sp, 32\n"
88 "       bl      my_direct_func\n"
89 "       ld.d    $a0, $sp, 0\n"
90 "       ld.d    $a1, $sp, 8\n"
91 "       ld.d    $a2, $sp, 16\n"
92 "       ld.d    $t0, $sp, 24\n"
93 "       ld.d    $ra, $sp, 32\n"
94 "       addi.d  $sp, $sp, 48\n"
95 "       jr      $t0\n"
96 "       .size           my_tramp, .-my_tramp\n"
97 "       .popsection\n"
98 );
99
100 #endif /* CONFIG_LOONGARCH */
101
102 static struct ftrace_ops direct;
103
104 static int __init ftrace_direct_init(void)
105 {
106         ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
107
108         return register_ftrace_direct(&direct, (unsigned long) my_tramp);
109 }
110
111 static void __exit ftrace_direct_exit(void)
112 {
113         unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
114 }
115
116 module_init(ftrace_direct_init);
117 module_exit(ftrace_direct_exit);
118
119 MODULE_AUTHOR("Steven Rostedt");
120 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
121 MODULE_LICENSE("GPL");