Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/kernel.h>
0003 #include <linux/spinlock.h>
0004 #include <linux/kprobes.h>
0005 #include <linux/mm.h>
0006 #include <linux/stop_machine.h>
0007 
0008 #include <asm/cacheflush.h>
0009 #include <asm/fixmap.h>
0010 #include <asm/smp_plat.h>
0011 #include <asm/opcodes.h>
0012 #include <asm/patch.h>
0013 
0014 struct patch {
0015     void *addr;
0016     unsigned int insn;
0017 };
0018 
0019 #ifdef CONFIG_MMU
0020 static DEFINE_RAW_SPINLOCK(patch_lock);
0021 
0022 static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
0023 {
0024     unsigned int uintaddr = (uintptr_t) addr;
0025     bool module = !core_kernel_text(uintaddr);
0026     struct page *page;
0027 
0028     if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
0029         page = vmalloc_to_page(addr);
0030     else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
0031         page = virt_to_page(addr);
0032     else
0033         return addr;
0034 
0035     if (flags)
0036         raw_spin_lock_irqsave(&patch_lock, *flags);
0037 
0038     set_fixmap(fixmap, page_to_phys(page));
0039 
0040     return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
0041 }
0042 
0043 static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
0044 {
0045     clear_fixmap(fixmap);
0046 
0047     if (flags)
0048         raw_spin_unlock_irqrestore(&patch_lock, *flags);
0049 }
0050 #else
0051 static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
0052 {
0053     return addr;
0054 }
0055 static void __kprobes patch_unmap(int fixmap, unsigned long *flags) { }
0056 #endif
0057 
0058 void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
0059 {
0060     bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
0061     unsigned int uintaddr = (uintptr_t) addr;
0062     bool twopage = false;
0063     unsigned long flags;
0064     void *waddr = addr;
0065     int size;
0066 
0067     if (remap)
0068         waddr = patch_map(addr, FIX_TEXT_POKE0, &flags);
0069 
0070     if (thumb2 && __opcode_is_thumb16(insn)) {
0071         *(u16 *)waddr = __opcode_to_mem_thumb16(insn);
0072         size = sizeof(u16);
0073     } else if (thumb2 && (uintaddr & 2)) {
0074         u16 first = __opcode_thumb32_first(insn);
0075         u16 second = __opcode_thumb32_second(insn);
0076         u16 *addrh0 = waddr;
0077         u16 *addrh1 = waddr + 2;
0078 
0079         twopage = (uintaddr & ~PAGE_MASK) == PAGE_SIZE - 2;
0080         if (twopage && remap)
0081             addrh1 = patch_map(addr + 2, FIX_TEXT_POKE1, NULL);
0082 
0083         *addrh0 = __opcode_to_mem_thumb16(first);
0084         *addrh1 = __opcode_to_mem_thumb16(second);
0085 
0086         if (twopage && addrh1 != addr + 2) {
0087             flush_kernel_vmap_range(addrh1, 2);
0088             patch_unmap(FIX_TEXT_POKE1, NULL);
0089         }
0090 
0091         size = sizeof(u32);
0092     } else {
0093         if (thumb2)
0094             insn = __opcode_to_mem_thumb32(insn);
0095         else
0096             insn = __opcode_to_mem_arm(insn);
0097 
0098         *(u32 *)waddr = insn;
0099         size = sizeof(u32);
0100     }
0101 
0102     if (waddr != addr) {
0103         flush_kernel_vmap_range(waddr, twopage ? size / 2 : size);
0104         patch_unmap(FIX_TEXT_POKE0, &flags);
0105     }
0106 
0107     flush_icache_range((uintptr_t)(addr),
0108                (uintptr_t)(addr) + size);
0109 }
0110 
0111 static int __kprobes patch_text_stop_machine(void *data)
0112 {
0113     struct patch *patch = data;
0114 
0115     __patch_text(patch->addr, patch->insn);
0116 
0117     return 0;
0118 }
0119 
0120 void __kprobes patch_text(void *addr, unsigned int insn)
0121 {
0122     struct patch patch = {
0123         .addr = addr,
0124         .insn = insn,
0125     };
0126 
0127     stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);
0128 }