1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 SiFive
6 #include <linux/spinlock.h>
8 #include <linux/memory.h>
9 #include <linux/uaccess.h>
10 #include <linux/stop_machine.h>
11 #include <asm/kprobes.h>
12 #include <asm/cacheflush.h>
13 #include <asm/fixmap.h>
14 #include <asm/ftrace.h>
15 #include <asm/patch.h>
24 int riscv_patch_in_stop_machine = false;
28 * The fix_to_virt(, idx) needs a const value (not a dynamic variable of
29 * reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
30 * So use '__always_inline' and 'const unsigned int fixmap' here.
32 static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
34 uintptr_t uintaddr = (uintptr_t) addr;
37 if (core_kernel_text(uintaddr))
38 page = phys_to_page(__pa_symbol(addr));
39 else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
40 page = vmalloc_to_page(addr);
46 return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
47 (uintaddr & ~PAGE_MASK));
50 static void patch_unmap(int fixmap)
54 NOKPROBE_SYMBOL(patch_unmap);
56 static int patch_insn_write(void *addr, const void *insn, size_t len)
59 bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE;
63 * Before reaching here, it was expected to lock the text_mutex
64 * already, so we don't need to give another lock here and could
65 * ensure that it was safe between each cores.
67 * We're currently using stop_machine() for ftrace & kprobes, and while
68 * that ensures text_mutex is held before installing the mappings it
69 * does not ensure text_mutex is held by the calling thread. That's
70 * safe but triggers a lockdep failure, so just elide it for that
73 if (!riscv_patch_in_stop_machine)
74 lockdep_assert_held(&text_mutex);
77 patch_map(addr + len, FIX_TEXT_POKE1);
79 waddr = patch_map(addr, FIX_TEXT_POKE0);
81 ret = copy_to_kernel_nofault(waddr, insn, len);
83 patch_unmap(FIX_TEXT_POKE0);
86 patch_unmap(FIX_TEXT_POKE1);
90 NOKPROBE_SYMBOL(patch_insn_write);
92 static int patch_insn_write(void *addr, const void *insn, size_t len)
94 return copy_to_kernel_nofault(addr, insn, len);
96 NOKPROBE_SYMBOL(patch_insn_write);
97 #endif /* CONFIG_MMU */
99 int patch_text_nosync(void *addr, const void *insns, size_t len)
104 ret = patch_insn_write(tp, insns, len);
107 flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len);
111 NOKPROBE_SYMBOL(patch_text_nosync);
113 static int patch_text_cb(void *data)
115 struct patch_insn *patch = data;
119 if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
120 for (i = 0; ret == 0 && i < patch->ninsns; i++) {
121 len = GET_INSN_LENGTH(patch->insns[i]);
122 ret = patch_text_nosync(patch->addr + i * len,
123 &patch->insns[i], len);
125 atomic_inc(&patch->cpu_count);
127 while (atomic_read(&patch->cpu_count) <= num_online_cpus())
134 NOKPROBE_SYMBOL(patch_text_cb);
136 int patch_text(void *addr, u32 *insns, int ninsns)
139 struct patch_insn patch = {
143 .cpu_count = ATOMIC_INIT(0),
147 * kprobes takes text_mutex, before calling patch_text(), but as we call
148 * calls stop_machine(), the lockdep assertion in patch_insn_write()
149 * gets confused by the context in which the lock is taken.
150 * Instead, ensure the lock is held before calling stop_machine(), and
151 * set riscv_patch_in_stop_machine to skip the check in
152 * patch_insn_write().
154 lockdep_assert_held(&text_mutex);
155 riscv_patch_in_stop_machine = true;
156 ret = stop_machine_cpuslocked(patch_text_cb, &patch, cpu_online_mask);
157 riscv_patch_in_stop_machine = false;
160 NOKPROBE_SYMBOL(patch_text);