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