1 // SPDX-License-Identifier: GPL-2.0
3 * functions to patch RO kernel text during runtime
5 * Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
8 #include <linux/kernel.h>
9 #include <linux/spinlock.h>
10 #include <linux/kprobes.h>
12 #include <linux/stop_machine.h>
14 #include <asm/cacheflush.h>
15 #include <asm/fixmap.h>
16 #include <asm/patch.h>
23 static void __kprobes *patch_map(void *addr, int fixmap)
25 unsigned long uintaddr = (uintptr_t) addr;
26 bool module = !core_kernel_text(uintaddr);
29 if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
30 page = vmalloc_to_page(addr);
31 else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
32 page = virt_to_page(addr);
36 set_fixmap(fixmap, page_to_phys(page));
38 return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
41 static void __kprobes patch_unmap(int fixmap)
46 void __kprobes __patch_text(void *addr, unsigned int insn)
51 waddr = patch_map(addr, FIX_TEXT_POKE0);
54 flush_kernel_vmap_range(waddr, size);
55 patch_unmap(FIX_TEXT_POKE0);
56 flush_icache_range((uintptr_t)(addr),
57 (uintptr_t)(addr) + size);
60 static int __kprobes patch_text_stop_machine(void *data)
62 struct patch *patch = data;
64 __patch_text(patch->addr, patch->insn);
69 void __kprobes patch_text(void *addr, unsigned int insn)
71 struct patch patch = {
76 stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);