Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 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> |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 6 | #include <asm/asm-offsets.h> |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 7 | |
Jiri Olsa | 0daf5cb | 2021-12-19 14:53:17 +0100 | [diff] [blame] | 8 | extern void my_direct_func(struct vm_area_struct *vma, |
| 9 | unsigned long address, unsigned int flags); |
| 10 | |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 11 | void my_direct_func(struct vm_area_struct *vma, |
| 12 | unsigned long address, unsigned int flags) |
| 13 | { |
| 14 | trace_printk("handle mm fault vma=%p address=%lx flags=%x\n", |
| 15 | vma, address, flags); |
| 16 | } |
| 17 | |
| 18 | extern void my_tramp(void *); |
| 19 | |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 20 | #ifdef CONFIG_X86_64 |
| 21 | |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 22 | asm ( |
| 23 | " .pushsection .text, \"ax\", @progbits\n" |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 24 | " .type my_tramp, @function\n" |
Sami Tolvanen | 983df5f | 2020-11-13 10:34:14 -0800 | [diff] [blame] | 25 | " .globl my_tramp\n" |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 26 | " my_tramp:" |
| 27 | " pushq %rbp\n" |
| 28 | " movq %rsp, %rbp\n" |
| 29 | " pushq %rdi\n" |
| 30 | " pushq %rsi\n" |
| 31 | " pushq %rdx\n" |
| 32 | " call my_direct_func\n" |
| 33 | " popq %rdx\n" |
| 34 | " popq %rsi\n" |
| 35 | " popq %rdi\n" |
| 36 | " leave\n" |
Peter Zijlstra | b17c2ba | 2021-12-04 14:43:41 +0100 | [diff] [blame] | 37 | ASM_RET |
Josh Poimboeuf | 9d907f1 | 2020-04-24 15:40:43 -0500 | [diff] [blame] | 38 | " .size my_tramp, .-my_tramp\n" |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 39 | " .popsection\n" |
| 40 | ); |
| 41 | |
Heiko Carstens | 1254cfbc | 2021-10-12 15:38:02 +0200 | [diff] [blame] | 42 | #endif /* CONFIG_X86_64 */ |
| 43 | |
| 44 | #ifdef CONFIG_S390 |
| 45 | |
| 46 | asm ( |
| 47 | " .pushsection .text, \"ax\", @progbits\n" |
| 48 | " .type my_tramp, @function\n" |
| 49 | " .globl my_tramp\n" |
| 50 | " my_tramp:" |
| 51 | " lgr %r1,%r15\n" |
| 52 | " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" |
| 53 | " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" |
| 54 | " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" |
| 55 | " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" |
| 56 | " brasl %r14,my_direct_func\n" |
| 57 | " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" |
| 58 | " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" |
| 59 | " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" |
| 60 | " lgr %r1,%r0\n" |
| 61 | " br %r1\n" |
| 62 | " .size my_tramp, .-my_tramp\n" |
| 63 | " .popsection\n" |
| 64 | ); |
| 65 | |
| 66 | #endif /* CONFIG_S390 */ |
Steven Rostedt (VMware) | 156473a | 2019-11-08 15:27:45 -0500 | [diff] [blame] | 67 | |
| 68 | static int __init ftrace_direct_init(void) |
| 69 | { |
| 70 | return register_ftrace_direct((unsigned long)handle_mm_fault, |
| 71 | (unsigned long)my_tramp); |
| 72 | } |
| 73 | |
| 74 | static void __exit ftrace_direct_exit(void) |
| 75 | { |
| 76 | unregister_ftrace_direct((unsigned long)handle_mm_fault, |
| 77 | (unsigned long)my_tramp); |
| 78 | } |
| 79 | |
| 80 | module_init(ftrace_direct_init); |
| 81 | module_exit(ftrace_direct_exit); |
| 82 | |
| 83 | MODULE_AUTHOR("Steven Rostedt"); |
| 84 | MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()"); |
| 85 | MODULE_LICENSE("GPL"); |