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/string.h>
10 #include <linux/uaccess.h>
11 #include <linux/stop_machine.h>
12 #include <asm/kprobes.h>
13 #include <asm/cacheflush.h>
14 #include <asm/fixmap.h>
15 #include <asm/ftrace.h>
16 #include <asm/patch.h>
25 int riscv_patch_in_stop_machine = false;
29 * The fix_to_virt(, idx) needs a const value (not a dynamic variable of
30 * reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
31 * So use '__always_inline' and 'const unsigned int fixmap' here.
33 static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
35 uintptr_t uintaddr = (uintptr_t) addr;
38 if (core_kernel_text(uintaddr))
39 page = phys_to_page(__pa_symbol(addr));
40 else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
41 page = vmalloc_to_page(addr);
47 return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
48 (uintaddr & ~PAGE_MASK));
51 static void patch_unmap(int fixmap)
55 NOKPROBE_SYMBOL(patch_unmap);
57 static int __patch_insn_set(void *addr, u8 c, size_t len)
60 bool across_pages = (((uintptr_t)addr & ~PAGE_MASK) + len) > PAGE_SIZE;
63 * Only two pages can be mapped at a time for writing.
65 if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
68 * Before reaching here, it was expected to lock the text_mutex
69 * already, so we don't need to give another lock here and could
70 * ensure that it was safe between each cores.
72 lockdep_assert_held(&text_mutex);
75 patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
77 waddr = patch_map(addr, FIX_TEXT_POKE0);
79 memset(waddr, c, len);
81 patch_unmap(FIX_TEXT_POKE0);
84 patch_unmap(FIX_TEXT_POKE1);
88 NOKPROBE_SYMBOL(__patch_insn_set);
90 static int __patch_insn_write(void *addr, const void *insn, size_t len)
93 bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE;
97 * Only two pages can be mapped at a time for writing.
99 if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
103 * Before reaching here, it was expected to lock the text_mutex
104 * already, so we don't need to give another lock here and could
105 * ensure that it was safe between each cores.
107 * We're currently using stop_machine() for ftrace & kprobes, and while
108 * that ensures text_mutex is held before installing the mappings it
109 * does not ensure text_mutex is held by the calling thread. That's
110 * safe but triggers a lockdep failure, so just elide it for that
113 if (!riscv_patch_in_stop_machine)
114 lockdep_assert_held(&text_mutex);
117 patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
119 waddr = patch_map(addr, FIX_TEXT_POKE0);
121 ret = copy_to_kernel_nofault(waddr, insn, len);
123 patch_unmap(FIX_TEXT_POKE0);
126 patch_unmap(FIX_TEXT_POKE1);
130 NOKPROBE_SYMBOL(__patch_insn_write);
132 static int __patch_insn_set(void *addr, u8 c, size_t len)
134 memset(addr, c, len);
138 NOKPROBE_SYMBOL(__patch_insn_set);
140 static int __patch_insn_write(void *addr, const void *insn, size_t len)
142 return copy_to_kernel_nofault(addr, insn, len);
144 NOKPROBE_SYMBOL(__patch_insn_write);
145 #endif /* CONFIG_MMU */
147 static int patch_insn_set(void *addr, u8 c, size_t len)
154 * __patch_insn_set() can only work on 2 pages at a time so call it in a
155 * loop with len <= 2 * PAGE_SIZE.
157 while (patched < len && !ret) {
158 size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
159 ret = __patch_insn_set(addr + patched, c, size);
166 NOKPROBE_SYMBOL(patch_insn_set);
168 int patch_text_set_nosync(void *addr, u8 c, size_t len)
173 ret = patch_insn_set(tp, c, len);
176 flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len);
180 NOKPROBE_SYMBOL(patch_text_set_nosync);
182 static int patch_insn_write(void *addr, const void *insn, size_t len)
189 * Copy the instructions to the destination address, two pages at a time
190 * because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE.
192 while (patched < len && !ret) {
193 size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
194 ret = __patch_insn_write(addr + patched, insn + patched, size);
201 NOKPROBE_SYMBOL(patch_insn_write);
203 int patch_text_nosync(void *addr, const void *insns, size_t len)
208 ret = patch_insn_write(tp, insns, len);
211 flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len);
215 NOKPROBE_SYMBOL(patch_text_nosync);
217 static int patch_text_cb(void *data)
219 struct patch_insn *patch = data;
223 if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
224 for (i = 0; ret == 0 && i < patch->ninsns; i++) {
225 len = GET_INSN_LENGTH(patch->insns[i]);
226 ret = patch_text_nosync(patch->addr + i * len,
227 &patch->insns[i], len);
229 atomic_inc(&patch->cpu_count);
231 while (atomic_read(&patch->cpu_count) <= num_online_cpus())
238 NOKPROBE_SYMBOL(patch_text_cb);
240 int patch_text(void *addr, u32 *insns, int ninsns)
243 struct patch_insn patch = {
247 .cpu_count = ATOMIC_INIT(0),
251 * kprobes takes text_mutex, before calling patch_text(), but as we call
252 * calls stop_machine(), the lockdep assertion in patch_insn_write()
253 * gets confused by the context in which the lock is taken.
254 * Instead, ensure the lock is held before calling stop_machine(), and
255 * set riscv_patch_in_stop_machine to skip the check in
256 * patch_insn_write().
258 lockdep_assert_held(&text_mutex);
259 riscv_patch_in_stop_machine = true;
260 ret = stop_machine_cpuslocked(patch_text_cb, &patch, cpu_online_mask);
261 riscv_patch_in_stop_machine = false;
264 NOKPROBE_SYMBOL(patch_text);