GNU Linux-libre 6.6.31-gnu
[releases.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 #ifndef CONFIG_ARM64
7 #include <asm/asm-offsets.h>
8 #endif
9
10 extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
11                            unsigned int flags, struct pt_regs *regs);
12
13 void my_direct_func(struct vm_area_struct *vma, unsigned long address,
14                     unsigned int flags, struct pt_regs *regs)
15 {
16         trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
17                      vma, address, flags, regs);
18 }
19
20 extern void my_tramp(void *);
21
22 #ifdef CONFIG_X86_64
23
24 #include <asm/ibt.h>
25 #include <asm/nospec-branch.h>
26
27 asm (
28 "       .pushsection    .text, \"ax\", @progbits\n"
29 "       .type           my_tramp, @function\n"
30 "       .globl          my_tramp\n"
31 "   my_tramp:"
32         ASM_ENDBR
33 "       pushq %rbp\n"
34 "       movq %rsp, %rbp\n"
35         CALL_DEPTH_ACCOUNT
36 "       pushq %rdi\n"
37 "       pushq %rsi\n"
38 "       pushq %rdx\n"
39 "       pushq %rcx\n"
40 "       call my_direct_func\n"
41 "       popq %rcx\n"
42 "       popq %rdx\n"
43 "       popq %rsi\n"
44 "       popq %rdi\n"
45 "       leave\n"
46         ASM_RET
47 "       .size           my_tramp, .-my_tramp\n"
48 "       .popsection\n"
49 );
50
51 #endif /* CONFIG_X86_64 */
52
53 #ifdef CONFIG_S390
54
55 asm (
56 "       .pushsection    .text, \"ax\", @progbits\n"
57 "       .type           my_tramp, @function\n"
58 "       .globl          my_tramp\n"
59 "   my_tramp:"
60 "       lgr             %r1,%r15\n"
61 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
62 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
63 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
64 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
65 "       brasl           %r14,my_direct_func\n"
66 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
67 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
68 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
69 "       lgr             %r1,%r0\n"
70 "       br              %r1\n"
71 "       .size           my_tramp, .-my_tramp\n"
72 "       .popsection\n"
73 );
74
75 #endif /* CONFIG_S390 */
76
77 #ifdef CONFIG_ARM64
78
79 asm (
80 "       .pushsection    .text, \"ax\", @progbits\n"
81 "       .type           my_tramp, @function\n"
82 "       .globl          my_tramp\n"
83 "   my_tramp:"
84 "       hint    34\n" // bti    c
85 "       sub     sp, sp, #48\n"
86 "       stp     x9, x30, [sp]\n"
87 "       stp     x0, x1, [sp, #16]\n"
88 "       stp     x2, x3, [sp, #32]\n"
89 "       bl      my_direct_func\n"
90 "       ldp     x30, x9, [sp]\n"
91 "       ldp     x0, x1, [sp, #16]\n"
92 "       ldp     x2, x3, [sp, #32]\n"
93 "       add     sp, sp, #48\n"
94 "       ret     x9\n"
95 "       .size           my_tramp, .-my_tramp\n"
96 "       .popsection\n"
97 );
98
99 #endif /* CONFIG_ARM64 */
100
101 #ifdef CONFIG_LOONGARCH
102
103 asm (
104 "       .pushsection    .text, \"ax\", @progbits\n"
105 "       .type           my_tramp, @function\n"
106 "       .globl          my_tramp\n"
107 "   my_tramp:\n"
108 "       addi.d  $sp, $sp, -48\n"
109 "       st.d    $a0, $sp, 0\n"
110 "       st.d    $a1, $sp, 8\n"
111 "       st.d    $a2, $sp, 16\n"
112 "       st.d    $t0, $sp, 24\n"
113 "       st.d    $ra, $sp, 32\n"
114 "       bl      my_direct_func\n"
115 "       ld.d    $a0, $sp, 0\n"
116 "       ld.d    $a1, $sp, 8\n"
117 "       ld.d    $a2, $sp, 16\n"
118 "       ld.d    $t0, $sp, 24\n"
119 "       ld.d    $ra, $sp, 32\n"
120 "       addi.d  $sp, $sp, 48\n"
121 "       jr      $t0\n"
122 "       .size           my_tramp, .-my_tramp\n"
123 "       .popsection\n"
124 );
125
126 #endif /* CONFIG_LOONGARCH */
127
128 static struct ftrace_ops direct;
129
130 static int __init ftrace_direct_init(void)
131 {
132         ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
133
134         return register_ftrace_direct(&direct, (unsigned long) my_tramp);
135 }
136
137 static void __exit ftrace_direct_exit(void)
138 {
139         unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
140 }
141
142 module_init(ftrace_direct_init);
143 module_exit(ftrace_direct_exit);
144
145 MODULE_AUTHOR("Steven Rostedt");
146 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
147 MODULE_LICENSE("GPL");