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 
21 asm (
22 "	.pushsection    .text, \"ax\", @progbits\n"
23 "	.type		my_tramp, @function\n"
24 "	.globl		my_tramp\n"
25 "   my_tramp:"
26 	ASM_ENDBR
27 "	pushq %rbp\n"
28 "	movq %rsp, %rbp\n"
29 "	pushq %rdi\n"
30 "	call my_direct_func\n"
31 "	popq %rdi\n"
32 "	leave\n"
33 	ASM_RET
34 "	.size		my_tramp, .-my_tramp\n"
35 "	.popsection\n"
36 );
37 
38 #endif /* CONFIG_X86_64 */
39 
40 #ifdef CONFIG_S390
41 
42 asm (
43 "	.pushsection	.text, \"ax\", @progbits\n"
44 "	.type		my_tramp, @function\n"
45 "	.globl		my_tramp\n"
46 "   my_tramp:"
47 "	lgr		%r1,%r15\n"
48 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
49 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
50 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
51 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
52 "	brasl		%r14,my_direct_func\n"
53 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
54 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
55 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
56 "	lgr		%r1,%r0\n"
57 "	br		%r1\n"
58 "	.size		my_tramp, .-my_tramp\n"
59 "	.popsection\n"
60 );
61 
62 #endif /* CONFIG_S390 */
63 
64 static int __init ftrace_direct_init(void)
65 {
66 	return register_ftrace_direct((unsigned long)wake_up_process,
67 				     (unsigned long)my_tramp);
68 }
69 
70 static void __exit ftrace_direct_exit(void)
71 {
72 	unregister_ftrace_direct((unsigned long)wake_up_process,
73 				 (unsigned long)my_tramp);
74 }
75 
76 module_init(ftrace_direct_init);
77 module_exit(ftrace_direct_exit);
78 
79 MODULE_AUTHOR("Steven Rostedt");
80 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
81 MODULE_LICENSE("GPL");
82