Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 3 | #include <linux/spinlock.h> |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 4 | #include <linux/kprobes.h> |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 5 | #include <linux/mm.h> |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 6 | #include <linux/stop_machine.h> |
| 7 | |
| 8 | #include <asm/cacheflush.h> |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 9 | #include <asm/fixmap.h> |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 10 | #include <asm/smp_plat.h> |
| 11 | #include <asm/opcodes.h> |
Wang Nan | fca08f3 | 2015-01-09 10:19:49 +0800 | [diff] [blame] | 12 | #include <asm/patch.h> |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 13 | |
| 14 | struct patch { |
| 15 | void *addr; |
| 16 | unsigned int insn; |
| 17 | }; |
| 18 | |
Peter Zijlstra | 7a7a8f5 | 2020-02-07 12:57:37 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_MMU |
Yang Shi | 143c2a8 | 2019-02-13 17:14:23 +0100 | [diff] [blame] | 20 | static DEFINE_RAW_SPINLOCK(patch_lock); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 21 | |
| 22 | static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags) |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 23 | { |
| 24 | unsigned int uintaddr = (uintptr_t) addr; |
| 25 | bool module = !core_kernel_text(uintaddr); |
| 26 | struct page *page; |
| 27 | |
Laura Abbott | 0f5bf6d | 2017-02-06 16:31:58 -0800 | [diff] [blame] | 28 | if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 29 | page = vmalloc_to_page(addr); |
Laura Abbott | 0f5bf6d | 2017-02-06 16:31:58 -0800 | [diff] [blame] | 30 | else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 31 | page = virt_to_page(addr); |
| 32 | else |
| 33 | return addr; |
| 34 | |
| 35 | if (flags) |
Yang Shi | 143c2a8 | 2019-02-13 17:14:23 +0100 | [diff] [blame] | 36 | raw_spin_lock_irqsave(&patch_lock, *flags); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 37 | |
| 38 | set_fixmap(fixmap, page_to_phys(page)); |
| 39 | |
| 40 | return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK)); |
| 41 | } |
| 42 | |
| 43 | static void __kprobes patch_unmap(int fixmap, unsigned long *flags) |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 44 | { |
| 45 | clear_fixmap(fixmap); |
| 46 | |
| 47 | if (flags) |
Yang Shi | 143c2a8 | 2019-02-13 17:14:23 +0100 | [diff] [blame] | 48 | raw_spin_unlock_irqrestore(&patch_lock, *flags); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 49 | } |
Peter Zijlstra | 7a7a8f5 | 2020-02-07 12:57:37 +0100 | [diff] [blame] | 50 | #else |
| 51 | static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags) |
| 52 | { |
| 53 | return addr; |
| 54 | } |
| 55 | static void __kprobes patch_unmap(int fixmap, unsigned long *flags) { } |
| 56 | #endif |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 57 | |
| 58 | void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap) |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 59 | { |
| 60 | bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 61 | unsigned int uintaddr = (uintptr_t) addr; |
| 62 | bool twopage = false; |
| 63 | unsigned long flags; |
| 64 | void *waddr = addr; |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 65 | int size; |
| 66 | |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 67 | if (remap) |
| 68 | waddr = patch_map(addr, FIX_TEXT_POKE0, &flags); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 69 | |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 70 | if (thumb2 && __opcode_is_thumb16(insn)) { |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 71 | *(u16 *)waddr = __opcode_to_mem_thumb16(insn); |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 72 | size = sizeof(u16); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 73 | } else if (thumb2 && (uintaddr & 2)) { |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 74 | u16 first = __opcode_thumb32_first(insn); |
| 75 | u16 second = __opcode_thumb32_second(insn); |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 76 | u16 *addrh0 = waddr; |
| 77 | u16 *addrh1 = waddr + 2; |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 78 | |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 79 | twopage = (uintaddr & ~PAGE_MASK) == PAGE_SIZE - 2; |
| 80 | if (twopage && remap) |
| 81 | addrh1 = patch_map(addr + 2, FIX_TEXT_POKE1, NULL); |
| 82 | |
| 83 | *addrh0 = __opcode_to_mem_thumb16(first); |
| 84 | *addrh1 = __opcode_to_mem_thumb16(second); |
| 85 | |
| 86 | if (twopage && addrh1 != addr + 2) { |
| 87 | flush_kernel_vmap_range(addrh1, 2); |
| 88 | patch_unmap(FIX_TEXT_POKE1, NULL); |
| 89 | } |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 90 | |
| 91 | size = sizeof(u32); |
| 92 | } else { |
| 93 | if (thumb2) |
| 94 | insn = __opcode_to_mem_thumb32(insn); |
| 95 | else |
| 96 | insn = __opcode_to_mem_arm(insn); |
| 97 | |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 98 | *(u32 *)waddr = insn; |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 99 | size = sizeof(u32); |
| 100 | } |
| 101 | |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 102 | if (waddr != addr) { |
| 103 | flush_kernel_vmap_range(waddr, twopage ? size / 2 : size); |
| 104 | patch_unmap(FIX_TEXT_POKE0, &flags); |
Peter Zijlstra | 7a7a8f5 | 2020-02-07 12:57:37 +0100 | [diff] [blame] | 105 | } |
Rabin Vincent | ab0615e | 2014-04-24 23:28:57 +0200 | [diff] [blame] | 106 | |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 107 | flush_icache_range((uintptr_t)(addr), |
| 108 | (uintptr_t)(addr) + size); |
| 109 | } |
| 110 | |
| 111 | static int __kprobes patch_text_stop_machine(void *data) |
| 112 | { |
| 113 | struct patch *patch = data; |
| 114 | |
| 115 | __patch_text(patch->addr, patch->insn); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | void __kprobes patch_text(void *addr, unsigned int insn) |
| 121 | { |
| 122 | struct patch patch = { |
| 123 | .addr = addr, |
| 124 | .insn = insn, |
| 125 | }; |
| 126 | |
Thomas Gleixner | 9489cc8 | 2017-05-24 10:15:38 +0200 | [diff] [blame] | 127 | stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL); |
Rabin Vincent | b21d55e | 2012-02-18 17:50:51 +0100 | [diff] [blame] | 128 | } |