patching.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <linux/smp.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/stop_machine.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/cacheflush.h>
  9. #include <asm/fixmap.h>
  10. #include <asm/insn.h>
  11. #include <asm/kprobes.h>
  12. #include <asm/patching.h>
  13. #include <asm/sections.h>
  14. static DEFINE_RAW_SPINLOCK(patch_lock);
  15. static bool is_exit_text(unsigned long addr)
  16. {
  17. /* discarded with init text/data */
  18. return system_state < SYSTEM_RUNNING &&
  19. addr >= (unsigned long)__exittext_begin &&
  20. addr < (unsigned long)__exittext_end;
  21. }
  22. static bool is_image_text(unsigned long addr)
  23. {
  24. return core_kernel_text(addr) || is_exit_text(addr);
  25. }
  26. static void __kprobes *patch_map(void *addr, int fixmap)
  27. {
  28. unsigned long uintaddr = (uintptr_t) addr;
  29. bool image = is_image_text(uintaddr);
  30. struct page *page;
  31. if (image)
  32. page = phys_to_page(__pa_symbol(addr));
  33. else if (IS_ENABLED(CONFIG_EXECMEM))
  34. page = vmalloc_to_page(addr);
  35. else
  36. return addr;
  37. BUG_ON(!page);
  38. return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
  39. (uintaddr & ~PAGE_MASK));
  40. }
  41. static void __kprobes patch_unmap(int fixmap)
  42. {
  43. clear_fixmap(fixmap);
  44. }
  45. /*
  46. * In ARMv8-A, A64 instructions have a fixed length of 32 bits and are always
  47. * little-endian.
  48. */
  49. int __kprobes aarch64_insn_read(void *addr, u32 *insnp)
  50. {
  51. int ret;
  52. __le32 val;
  53. ret = copy_from_kernel_nofault(&val, addr, AARCH64_INSN_SIZE);
  54. if (!ret)
  55. *insnp = le32_to_cpu(val);
  56. return ret;
  57. }
  58. static int __kprobes __aarch64_insn_write(void *addr, __le32 insn)
  59. {
  60. void *waddr = addr;
  61. unsigned long flags = 0;
  62. int ret;
  63. raw_spin_lock_irqsave(&patch_lock, flags);
  64. waddr = patch_map(addr, FIX_TEXT_POKE0);
  65. ret = copy_to_kernel_nofault(waddr, &insn, AARCH64_INSN_SIZE);
  66. patch_unmap(FIX_TEXT_POKE0);
  67. raw_spin_unlock_irqrestore(&patch_lock, flags);
  68. return ret;
  69. }
  70. int __kprobes aarch64_insn_write(void *addr, u32 insn)
  71. {
  72. return __aarch64_insn_write(addr, cpu_to_le32(insn));
  73. }
  74. noinstr int aarch64_insn_write_literal_u64(void *addr, u64 val)
  75. {
  76. u64 *waddr;
  77. unsigned long flags;
  78. int ret;
  79. raw_spin_lock_irqsave(&patch_lock, flags);
  80. waddr = patch_map(addr, FIX_TEXT_POKE0);
  81. ret = copy_to_kernel_nofault(waddr, &val, sizeof(val));
  82. patch_unmap(FIX_TEXT_POKE0);
  83. raw_spin_unlock_irqrestore(&patch_lock, flags);
  84. return ret;
  85. }
  86. typedef void text_poke_f(void *dst, void *src, size_t patched, size_t len);
  87. static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len)
  88. {
  89. unsigned long flags;
  90. size_t patched = 0;
  91. size_t size;
  92. void *waddr;
  93. void *ptr;
  94. raw_spin_lock_irqsave(&patch_lock, flags);
  95. while (patched < len) {
  96. ptr = addr + patched;
  97. size = min_t(size_t, PAGE_SIZE - offset_in_page(ptr),
  98. len - patched);
  99. waddr = patch_map(ptr, FIX_TEXT_POKE0);
  100. func(waddr, src, patched, size);
  101. patch_unmap(FIX_TEXT_POKE0);
  102. patched += size;
  103. }
  104. raw_spin_unlock_irqrestore(&patch_lock, flags);
  105. flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
  106. return addr;
  107. }
  108. static void text_poke_memcpy(void *dst, void *src, size_t patched, size_t len)
  109. {
  110. copy_to_kernel_nofault(dst, src + patched, len);
  111. }
  112. static void text_poke_memset(void *dst, void *src, size_t patched, size_t len)
  113. {
  114. u32 c = *(u32 *)src;
  115. memset32(dst, c, len / 4);
  116. }
  117. /**
  118. * aarch64_insn_copy - Copy instructions into (an unused part of) RX memory
  119. * @dst: address to modify
  120. * @src: source of the copy
  121. * @len: length to copy
  122. *
  123. * Useful for JITs to dump new code blocks into unused regions of RX memory.
  124. */
  125. noinstr void *aarch64_insn_copy(void *dst, void *src, size_t len)
  126. {
  127. /* A64 instructions must be word aligned */
  128. if ((uintptr_t)dst & 0x3)
  129. return NULL;
  130. return __text_poke(text_poke_memcpy, dst, src, len);
  131. }
  132. /**
  133. * aarch64_insn_set - memset for RX memory regions.
  134. * @dst: address to modify
  135. * @insn: value to set
  136. * @len: length of memory region.
  137. *
  138. * Useful for JITs to fill regions of RX memory with illegal instructions.
  139. */
  140. noinstr void *aarch64_insn_set(void *dst, u32 insn, size_t len)
  141. {
  142. if ((uintptr_t)dst & 0x3)
  143. return NULL;
  144. return __text_poke(text_poke_memset, dst, &insn, len);
  145. }
  146. int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
  147. {
  148. u32 *tp = addr;
  149. int ret;
  150. /* A64 instructions must be word aligned */
  151. if ((uintptr_t)tp & 0x3)
  152. return -EINVAL;
  153. ret = aarch64_insn_write(tp, insn);
  154. if (ret == 0)
  155. caches_clean_inval_pou((uintptr_t)tp,
  156. (uintptr_t)tp + AARCH64_INSN_SIZE);
  157. return ret;
  158. }
  159. struct aarch64_insn_patch {
  160. void **text_addrs;
  161. u32 *new_insns;
  162. int insn_cnt;
  163. atomic_t cpu_count;
  164. };
  165. static int __kprobes aarch64_insn_patch_text_cb(void *arg)
  166. {
  167. int i, ret = 0;
  168. struct aarch64_insn_patch *pp = arg;
  169. /* The last CPU becomes master */
  170. if (atomic_inc_return(&pp->cpu_count) == num_online_cpus()) {
  171. for (i = 0; ret == 0 && i < pp->insn_cnt; i++)
  172. ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i],
  173. pp->new_insns[i]);
  174. /* Notify other processors with an additional increment. */
  175. atomic_inc(&pp->cpu_count);
  176. } else {
  177. while (atomic_read(&pp->cpu_count) <= num_online_cpus())
  178. cpu_relax();
  179. isb();
  180. }
  181. return ret;
  182. }
  183. int __kprobes aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt)
  184. {
  185. struct aarch64_insn_patch patch = {
  186. .text_addrs = addrs,
  187. .new_insns = insns,
  188. .insn_cnt = cnt,
  189. .cpu_count = ATOMIC_INIT(0),
  190. };
  191. if (cnt <= 0)
  192. return -EINVAL;
  193. return stop_machine_cpuslocked(aarch64_insn_patch_text_cb, &patch,
  194. cpu_online_mask);
  195. }