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>
6
7extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
8			   unsigned int flags, struct pt_regs *regs);
9
10void my_direct_func(struct vm_area_struct *vma, unsigned long address,
11		    unsigned int flags, struct pt_regs *regs)
12{
13	trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
14		     vma, address, flags, regs);
15}
16
17extern void my_tramp(void *);
18
19asm (
20"	.pushsection    .text, \"ax\", @progbits\n"
21"	.type		my_tramp, @function\n"
22"	.globl		my_tramp\n"
23"   my_tramp:"
24"	pushq %rbp\n"
25"	movq %rsp, %rbp\n"
26"	pushq %rdi\n"
27"	pushq %rsi\n"
28"	pushq %rdx\n"
29"	pushq %rcx\n"
30"	call my_direct_func\n"
31"	popq %rcx\n"
32"	popq %rdx\n"
33"	popq %rsi\n"
34"	popq %rdi\n"
35"	leave\n"
36	ASM_RET
37"	.size		my_tramp, .-my_tramp\n"
38"	.popsection\n"
39);
40
41
42static int __init ftrace_direct_init(void)
43{
44	return register_ftrace_direct((unsigned long)handle_mm_fault,
45				     (unsigned long)my_tramp);
46}
47
48static void __exit ftrace_direct_exit(void)
49{
50	unregister_ftrace_direct((unsigned long)handle_mm_fault,
51				 (unsigned long)my_tramp);
52}
53
54module_init(ftrace_direct_init);
55module_exit(ftrace_direct_exit);
56
57MODULE_AUTHOR("Steven Rostedt");
58MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
59MODULE_LICENSE("GPL");
60