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>
16 #include <asm/sections.h>
24 int riscv_patch_in_stop_machine = false;
28 static inline bool is_kernel_exittext(uintptr_t addr)
30 return system_state < SYSTEM_RUNNING &&
31 addr >= (uintptr_t)__exittext_begin &&
32 addr < (uintptr_t)__exittext_end;
36 * The fix_to_virt(, idx) needs a const value (not a dynamic variable of
37 * reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
38 * So use '__always_inline' and 'const unsigned int fixmap' here.
40 static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
42 uintptr_t uintaddr = (uintptr_t) addr;
45 if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr))
46 page = phys_to_page(__pa_symbol(addr));
47 else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
48 page = vmalloc_to_page(addr);
54 return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
55 (uintaddr & ~PAGE_MASK));
58 static void patch_unmap(int fixmap)
62 NOKPROBE_SYMBOL(patch_unmap);
64 static int patch_insn_write(void *addr, const void *insn, size_t len)
67 bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE;
71 * Before reaching here, it was expected to lock the text_mutex
72 * already, so we don't need to give another lock here and could
73 * ensure that it was safe between each cores.
75 * We're currently using stop_machine() for ftrace & kprobes, and while
76 * that ensures text_mutex is held before installing the mappings it
77 * does not ensure text_mutex is held by the calling thread. That's
78 * safe but triggers a lockdep failure, so just elide it for that
81 if (!riscv_patch_in_stop_machine)
82 lockdep_assert_held(&text_mutex);
85 patch_map(addr + len, FIX_TEXT_POKE1);
87 waddr = patch_map(addr, FIX_TEXT_POKE0);
89 ret = copy_to_kernel_nofault(waddr, insn, len);
91 patch_unmap(FIX_TEXT_POKE0);
94 patch_unmap(FIX_TEXT_POKE1);
98 NOKPROBE_SYMBOL(patch_insn_write);
100 static int patch_insn_write(void *addr, const void *insn, size_t len)
102 return copy_to_kernel_nofault(addr, insn, len);
104 NOKPROBE_SYMBOL(patch_insn_write);
105 #endif /* CONFIG_MMU */
107 int patch_text_nosync(void *addr, const void *insns, size_t len)
112 ret = patch_insn_write(tp, insns, len);
115 flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len);
119 NOKPROBE_SYMBOL(patch_text_nosync);
121 static int patch_text_cb(void *data)
123 struct patch_insn *patch = data;
126 if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
128 patch_text_nosync(patch->addr, &patch->insn,
129 GET_INSN_LENGTH(patch->insn));
130 atomic_inc(&patch->cpu_count);
132 while (atomic_read(&patch->cpu_count) <= num_online_cpus())
139 NOKPROBE_SYMBOL(patch_text_cb);
141 int patch_text(void *addr, u32 insn)
144 struct patch_insn patch = {
147 .cpu_count = ATOMIC_INIT(0),
151 * kprobes takes text_mutex, before calling patch_text(), but as we call
152 * calls stop_machine(), the lockdep assertion in patch_insn_write()
153 * gets confused by the context in which the lock is taken.
154 * Instead, ensure the lock is held before calling stop_machine(), and
155 * set riscv_patch_in_stop_machine to skip the check in
156 * patch_insn_write().
158 lockdep_assert_held(&text_mutex);
159 riscv_patch_in_stop_machine = true;
160 ret = stop_machine_cpuslocked(patch_text_cb, &patch, cpu_online_mask);
161 riscv_patch_in_stop_machine = false;
164 NOKPROBE_SYMBOL(patch_text);