Merge tag 'hardening-v6.5-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / samples / ftrace / ftrace-direct.c
CommitLineData
b06457c8
SRV
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/module.h>
3
4#include <linux/sched.h> /* for wake_up_process() */
5#include <linux/ftrace.h>
1254cfbc 6#include <asm/asm-offsets.h>
b06457c8 7
0daf5cb2
JO
8extern void my_direct_func(struct task_struct *p);
9
b06457c8
SRV
10void my_direct_func(struct task_struct *p)
11{
760f8bc7 12 trace_printk("waking up %s-%d\n", p->comm, p->pid);
b06457c8
SRV
13}
14
15extern void my_tramp(void *);
16
1254cfbc
HC
17#ifdef CONFIG_X86_64
18
0aec21cf 19#include <asm/ibt.h>
01678fbc 20#include <asm/nospec-branch.h>
0aec21cf 21
b06457c8
SRV
22asm (
23" .pushsection .text, \"ax\", @progbits\n"
9d907f1a 24" .type my_tramp, @function\n"
983df5f2 25" .globl my_tramp\n"
b06457c8 26" my_tramp:"
0aec21cf 27 ASM_ENDBR
b06457c8
SRV
28" pushq %rbp\n"
29" movq %rsp, %rbp\n"
ee3e2469 30 CALL_DEPTH_ACCOUNT
b06457c8
SRV
31" pushq %rdi\n"
32" call my_direct_func\n"
33" popq %rdi\n"
34" leave\n"
b17c2baa 35 ASM_RET
9d907f1a 36" .size my_tramp, .-my_tramp\n"
b06457c8
SRV
37" .popsection\n"
38);
39
1254cfbc
HC
40#endif /* CONFIG_X86_64 */
41
42#ifdef CONFIG_S390
43
44asm (
45" .pushsection .text, \"ax\", @progbits\n"
46" .type my_tramp, @function\n"
47" .globl my_tramp\n"
48" my_tramp:"
49" lgr %r1,%r15\n"
50" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
51" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
52" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
53" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
54" brasl %r14,my_direct_func\n"
55" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
56" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
57" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
58" lgr %r1,%r0\n"
59" br %r1\n"
60" .size my_tramp, .-my_tramp\n"
61" .popsection\n"
62);
63
64#endif /* CONFIG_S390 */
b06457c8 65
22f367a6
YT
66#ifdef CONFIG_LOONGARCH
67
68asm (
69" .pushsection .text, \"ax\", @progbits\n"
70" .type my_tramp, @function\n"
71" .globl my_tramp\n"
72" my_tramp:\n"
73" addi.d $sp, $sp, -32\n"
74" st.d $a0, $sp, 0\n"
75" st.d $t0, $sp, 8\n"
76" st.d $ra, $sp, 16\n"
77" bl my_direct_func\n"
78" ld.d $a0, $sp, 0\n"
79" ld.d $t0, $sp, 8\n"
80" ld.d $ra, $sp, 16\n"
81" addi.d $sp, $sp, 32\n"
82" jr $t0\n"
83" .size my_tramp, .-my_tramp\n"
84" .popsection\n"
85);
86
87#endif /* CONFIG_LOONGARCH */
88
23edf483
FR
89static struct ftrace_ops direct;
90
b06457c8
SRV
91static int __init ftrace_direct_init(void)
92{
23edf483
FR
93 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
94
da8bdfbd 95 return register_ftrace_direct(&direct, (unsigned long) my_tramp);
b06457c8
SRV
96}
97
98static void __exit ftrace_direct_exit(void)
99{
da8bdfbd 100 unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
b06457c8
SRV
101}
102
103module_init(ftrace_direct_init);
104module_exit(ftrace_direct_exit);
105
106MODULE_AUTHOR("Steven Rostedt");
da8bdfbd 107MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
b06457c8 108MODULE_LICENSE("GPL");