.. | .. |
---|
1 | | -/* SPDX-License-Identifier: GPL-2.0 */ |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
2 | 2 | /* |
---|
3 | 3 | * Copyright (C) 2013 Linaro Limited |
---|
4 | 4 | * Author: AKASHI Takahiro <takahiro.akashi@linaro.org> |
---|
.. | .. |
---|
7 | 7 | |
---|
8 | 8 | #include <linux/ftrace.h> |
---|
9 | 9 | #include <linux/uaccess.h> |
---|
| 10 | +#include <linux/memory.h> |
---|
10 | 11 | #include <asm/cacheflush.h> |
---|
| 12 | +#include <asm/patch.h> |
---|
11 | 13 | |
---|
12 | 14 | #ifdef CONFIG_DYNAMIC_FTRACE |
---|
| 15 | +int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) |
---|
| 16 | +{ |
---|
| 17 | + mutex_lock(&text_mutex); |
---|
| 18 | + return 0; |
---|
| 19 | +} |
---|
| 20 | + |
---|
| 21 | +int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex) |
---|
| 22 | +{ |
---|
| 23 | + mutex_unlock(&text_mutex); |
---|
| 24 | + return 0; |
---|
| 25 | +} |
---|
| 26 | + |
---|
13 | 27 | static int ftrace_check_current_call(unsigned long hook_pos, |
---|
14 | 28 | unsigned int *expected) |
---|
15 | 29 | { |
---|
.. | .. |
---|
24 | 38 | * Read the text we want to modify; |
---|
25 | 39 | * return must be -EFAULT on read error |
---|
26 | 40 | */ |
---|
27 | | - if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE)) |
---|
| 41 | + if (copy_from_kernel_nofault(replaced, (void *)hook_pos, |
---|
| 42 | + MCOUNT_INSN_SIZE)) |
---|
28 | 43 | return -EFAULT; |
---|
29 | 44 | |
---|
30 | 45 | /* |
---|
.. | .. |
---|
32 | 47 | * return must be -EINVAL on failed comparison |
---|
33 | 48 | */ |
---|
34 | 49 | if (memcmp(expected, replaced, sizeof(replaced))) { |
---|
35 | | - pr_err("%p: expected (%08x %08x) but get (%08x %08x)", |
---|
| 50 | + pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n", |
---|
36 | 51 | (void *)hook_pos, expected[0], expected[1], replaced[0], |
---|
37 | 52 | replaced[1]); |
---|
38 | 53 | return -EINVAL; |
---|
.. | .. |
---|
46 | 61 | { |
---|
47 | 62 | unsigned int call[2]; |
---|
48 | 63 | unsigned int nops[2] = {NOP4, NOP4}; |
---|
49 | | - int ret = 0; |
---|
50 | 64 | |
---|
51 | 65 | make_call(hook_pos, target, call); |
---|
52 | 66 | |
---|
53 | | - /* replace the auipc-jalr pair at once */ |
---|
54 | | - ret = probe_kernel_write((void *)hook_pos, enable ? call : nops, |
---|
55 | | - MCOUNT_INSN_SIZE); |
---|
56 | | - /* return must be -EPERM on write error */ |
---|
57 | | - if (ret) |
---|
| 67 | + /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */ |
---|
| 68 | + if (patch_text_nosync |
---|
| 69 | + ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) |
---|
58 | 70 | return -EPERM; |
---|
59 | | - |
---|
60 | | - smp_mb(); |
---|
61 | | - flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE); |
---|
62 | 71 | |
---|
63 | 72 | return 0; |
---|
64 | 73 | } |
---|