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>
6 #include <asm/asm-offsets.h>
7 
8 extern void my_direct_func(struct task_struct *p);
9 
10 void my_direct_func(struct task_struct *p)
11 {
12 	trace_printk("waking up %s-%d\n", p->comm, p->pid);
13 }
14 
15 extern void my_tramp(void *);
16 
17 #ifdef CONFIG_X86_64
18 
19 #include <asm/ibt.h>
20 #include <asm/nospec-branch.h>
21 
22 asm (
23 "	.pushsection    .text, \"ax\", @progbits\n"
24 "	.type		my_tramp, @function\n"
25 "	.globl		my_tramp\n"
26 "   my_tramp:"
27 	ASM_ENDBR
28 "	pushq %rbp\n"
29 "	movq %rsp, %rbp\n"
30 	CALL_DEPTH_ACCOUNT
31 "	pushq %rdi\n"
32 "	call my_direct_func\n"
33 "	popq %rdi\n"
34 "	leave\n"
35 	ASM_RET
36 "	.size		my_tramp, .-my_tramp\n"
37 "	.popsection\n"
38 );
39 
40 #endif /* CONFIG_X86_64 */
41 
42 #ifdef CONFIG_S390
43 
44 asm (
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 */
65 
66 static int __init ftrace_direct_init(void)
67 {
68 	return register_ftrace_direct((unsigned long)wake_up_process,
69 				     (unsigned long)my_tramp);
70 }
71 
72 static void __exit ftrace_direct_exit(void)
73 {
74 	unregister_ftrace_direct((unsigned long)wake_up_process,
75 				 (unsigned long)my_tramp);
76 }
77 
78 module_init(ftrace_direct_init);
79 module_exit(ftrace_direct_exit);
80 
81 MODULE_AUTHOR("Steven Rostedt");
82 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
83 MODULE_LICENSE("GPL");
84