Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / samples / ftrace / ftrace-direct-too.c
CommitLineData
156473a0
SRV
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>
71db948b 6#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
1254cfbc 7#include <asm/asm-offsets.h>
8c3526fb 8#endif
156473a0 9
8564c315
FR
10extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
11 unsigned int flags, struct pt_regs *regs);
0daf5cb2 12
8564c315
FR
13void my_direct_func(struct vm_area_struct *vma, unsigned long address,
14 unsigned int flags, struct pt_regs *regs)
156473a0 15{
8564c315
FR
16 trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
17 vma, address, flags, regs);
156473a0
SRV
18}
19
20extern void my_tramp(void *);
21
629291dd
SS
22#ifdef CONFIG_RISCV
23#include <asm/asm.h>
24
25asm (
26" .pushsection .text, \"ax\", @progbits\n"
27" .type my_tramp, @function\n"
28" .globl my_tramp\n"
29" my_tramp:\n"
30" addi sp,sp,-5*"SZREG"\n"
31" "REG_S" a0,0*"SZREG"(sp)\n"
32" "REG_S" a1,1*"SZREG"(sp)\n"
33" "REG_S" a2,2*"SZREG"(sp)\n"
34" "REG_S" t0,3*"SZREG"(sp)\n"
35" "REG_S" ra,4*"SZREG"(sp)\n"
36" call my_direct_func\n"
37" "REG_L" a0,0*"SZREG"(sp)\n"
38" "REG_L" a1,1*"SZREG"(sp)\n"
39" "REG_L" a2,2*"SZREG"(sp)\n"
40" "REG_L" t0,3*"SZREG"(sp)\n"
41" "REG_L" ra,4*"SZREG"(sp)\n"
42" addi sp,sp,5*"SZREG"\n"
43" jr t0\n"
44" .size my_tramp, .-my_tramp\n"
45" .popsection\n"
46);
47
48#endif /* CONFIG_RISCV */
49
1254cfbc
HC
50#ifdef CONFIG_X86_64
51
0aec21cf 52#include <asm/ibt.h>
01678fbc 53#include <asm/nospec-branch.h>
0aec21cf 54
156473a0
SRV
55asm (
56" .pushsection .text, \"ax\", @progbits\n"
9d907f1a 57" .type my_tramp, @function\n"
983df5f2 58" .globl my_tramp\n"
156473a0 59" my_tramp:"
0aec21cf 60 ASM_ENDBR
156473a0
SRV
61" pushq %rbp\n"
62" movq %rsp, %rbp\n"
ee3e2469 63 CALL_DEPTH_ACCOUNT
156473a0
SRV
64" pushq %rdi\n"
65" pushq %rsi\n"
66" pushq %rdx\n"
8564c315 67" pushq %rcx\n"
156473a0 68" call my_direct_func\n"
8564c315 69" popq %rcx\n"
156473a0
SRV
70" popq %rdx\n"
71" popq %rsi\n"
72" popq %rdi\n"
73" leave\n"
b17c2baa 74 ASM_RET
9d907f1a 75" .size my_tramp, .-my_tramp\n"
156473a0
SRV
76" .popsection\n"
77);
78
1254cfbc
HC
79#endif /* CONFIG_X86_64 */
80
81#ifdef CONFIG_S390
82
83asm (
84" .pushsection .text, \"ax\", @progbits\n"
85" .type my_tramp, @function\n"
86" .globl my_tramp\n"
87" my_tramp:"
88" lgr %r1,%r15\n"
89" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
90" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
91" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
92" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
93" brasl %r14,my_direct_func\n"
94" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
95" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
96" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
97" lgr %r1,%r0\n"
98" br %r1\n"
99" .size my_tramp, .-my_tramp\n"
100" .popsection\n"
101);
102
103#endif /* CONFIG_S390 */
156473a0 104
8c3526fb
FR
105#ifdef CONFIG_ARM64
106
107asm (
108" .pushsection .text, \"ax\", @progbits\n"
109" .type my_tramp, @function\n"
110" .globl my_tramp\n"
111" my_tramp:"
e332938e 112" hint 34\n" // bti c
8c3526fb
FR
113" sub sp, sp, #48\n"
114" stp x9, x30, [sp]\n"
115" stp x0, x1, [sp, #16]\n"
116" stp x2, x3, [sp, #32]\n"
117" bl my_direct_func\n"
118" ldp x30, x9, [sp]\n"
119" ldp x0, x1, [sp, #16]\n"
120" ldp x2, x3, [sp, #32]\n"
121" add sp, sp, #48\n"
122" ret x9\n"
123" .size my_tramp, .-my_tramp\n"
124" .popsection\n"
125);
126
127#endif /* CONFIG_ARM64 */
128
22f367a6
YT
129#ifdef CONFIG_LOONGARCH
130
131asm (
132" .pushsection .text, \"ax\", @progbits\n"
133" .type my_tramp, @function\n"
134" .globl my_tramp\n"
135" my_tramp:\n"
136" addi.d $sp, $sp, -48\n"
137" st.d $a0, $sp, 0\n"
138" st.d $a1, $sp, 8\n"
139" st.d $a2, $sp, 16\n"
140" st.d $t0, $sp, 24\n"
141" st.d $ra, $sp, 32\n"
142" bl my_direct_func\n"
143" ld.d $a0, $sp, 0\n"
144" ld.d $a1, $sp, 8\n"
145" ld.d $a2, $sp, 16\n"
146" ld.d $t0, $sp, 24\n"
147" ld.d $ra, $sp, 32\n"
148" addi.d $sp, $sp, 48\n"
149" jr $t0\n"
150" .size my_tramp, .-my_tramp\n"
151" .popsection\n"
152);
153
154#endif /* CONFIG_LOONGARCH */
155
71db948b
NR
156#ifdef CONFIG_PPC
157#include <asm/ppc_asm.h>
158
159#ifdef CONFIG_PPC64
160#define STACK_FRAME_SIZE 64
161#define STACK_FRAME_ARG1 32
162#define STACK_FRAME_ARG2 40
163#define STACK_FRAME_ARG3 48
164#define STACK_FRAME_ARG4 56
165#else
166#define STACK_FRAME_SIZE 32
167#define STACK_FRAME_ARG1 16
168#define STACK_FRAME_ARG2 20
169#define STACK_FRAME_ARG3 24
170#define STACK_FRAME_ARG4 28
171#endif
172
173#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
174#define PPC64_TOC_SAVE_AND_UPDATE \
175" std 2, 24(1)\n" \
176" bcl 20, 31, 1f\n" \
177" 1: mflr 12\n" \
178" ld 2, (99f - 1b)(12)\n"
179#define PPC64_TOC_RESTORE \
180" ld 2, 24(1)\n"
181#define PPC64_TOC \
182" 99: .quad .TOC.@tocbase\n"
183#else
184#define PPC64_TOC_SAVE_AND_UPDATE ""
185#define PPC64_TOC_RESTORE ""
186#define PPC64_TOC ""
187#endif
188
189#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
190#define PPC_FTRACE_RESTORE_LR \
191 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
192" mtlr 0\n"
193#define PPC_FTRACE_RET \
194" blr\n"
195#else
196#define PPC_FTRACE_RESTORE_LR \
197 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
198" mtctr 0\n"
199#define PPC_FTRACE_RET \
200" mtlr 0\n" \
201" bctr\n"
202#endif
203
204asm (
205" .pushsection .text, \"ax\", @progbits\n"
206" .type my_tramp, @function\n"
207" .globl my_tramp\n"
208" my_tramp:\n"
209 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
210 PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
211" mflr 0\n"
212 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
213 PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
214 PPC64_TOC_SAVE_AND_UPDATE
215 PPC_STL" 3, "__stringify(STACK_FRAME_ARG1)"(1)\n"
216 PPC_STL" 4, "__stringify(STACK_FRAME_ARG2)"(1)\n"
217 PPC_STL" 5, "__stringify(STACK_FRAME_ARG3)"(1)\n"
218 PPC_STL" 6, "__stringify(STACK_FRAME_ARG4)"(1)\n"
219" bl my_direct_func\n"
220 PPC_LL" 6, "__stringify(STACK_FRAME_ARG4)"(1)\n"
221 PPC_LL" 5, "__stringify(STACK_FRAME_ARG3)"(1)\n"
222 PPC_LL" 4, "__stringify(STACK_FRAME_ARG2)"(1)\n"
223 PPC_LL" 3, "__stringify(STACK_FRAME_ARG1)"(1)\n"
224 PPC64_TOC_RESTORE
225" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
226 PPC_FTRACE_RESTORE_LR
227" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
228 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
229 PPC_FTRACE_RET
230 PPC64_TOC
231" .size my_tramp, .-my_tramp\n"
232" .popsection\n"
233);
234
235#endif /* CONFIG_PPC */
236
23edf483
FR
237static struct ftrace_ops direct;
238
156473a0
SRV
239static int __init ftrace_direct_init(void)
240{
23edf483
FR
241 ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
242
da8bdfbd 243 return register_ftrace_direct(&direct, (unsigned long) my_tramp);
156473a0
SRV
244}
245
246static void __exit ftrace_direct_exit(void)
247{
da8bdfbd 248 unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
156473a0
SRV
249}
250
251module_init(ftrace_direct_init);
252module_exit(ftrace_direct_exit);
253
254MODULE_AUTHOR("Steven Rostedt");
da8bdfbd 255MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
156473a0 256MODULE_LICENSE("GPL");