code-patching.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2008 Michael Ellerman, IBM Corporation.
  4. */
  5. #include <linux/kprobes.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/init.h>
  8. #include <linux/cpuhotplug.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/jump_label.h>
  11. #include <asm/tlbflush.h>
  12. #include <asm/page.h>
  13. #include <asm/code-patching.h>
  14. #include <asm/inst.h>
  15. static int __patch_instruction(u32 *exec_addr, ppc_inst_t instr, u32 *patch_addr)
  16. {
  17. if (!ppc_inst_prefixed(instr)) {
  18. u32 val = ppc_inst_val(instr);
  19. __put_kernel_nofault(patch_addr, &val, u32, failed);
  20. } else {
  21. u64 val = ppc_inst_as_ulong(instr);
  22. __put_kernel_nofault(patch_addr, &val, u64, failed);
  23. }
  24. asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
  25. "r" (exec_addr));
  26. return 0;
  27. failed:
  28. return -EPERM;
  29. }
  30. int raw_patch_instruction(u32 *addr, ppc_inst_t instr)
  31. {
  32. return __patch_instruction(addr, instr, addr);
  33. }
  34. #ifdef CONFIG_STRICT_KERNEL_RWX
  35. static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
  36. static int map_patch_area(void *addr, unsigned long text_poke_addr);
  37. static void unmap_patch_area(unsigned long addr);
  38. static int text_area_cpu_up(unsigned int cpu)
  39. {
  40. struct vm_struct *area;
  41. unsigned long addr;
  42. int err;
  43. area = get_vm_area(PAGE_SIZE, VM_ALLOC);
  44. if (!area) {
  45. WARN_ONCE(1, "Failed to create text area for cpu %d\n",
  46. cpu);
  47. return -1;
  48. }
  49. // Map/unmap the area to ensure all page tables are pre-allocated
  50. addr = (unsigned long)area->addr;
  51. err = map_patch_area(empty_zero_page, addr);
  52. if (err)
  53. return err;
  54. unmap_patch_area(addr);
  55. this_cpu_write(text_poke_area, area);
  56. return 0;
  57. }
  58. static int text_area_cpu_down(unsigned int cpu)
  59. {
  60. free_vm_area(this_cpu_read(text_poke_area));
  61. return 0;
  62. }
  63. static __ro_after_init DEFINE_STATIC_KEY_FALSE(poking_init_done);
  64. /*
  65. * Although BUG_ON() is rude, in this case it should only happen if ENOMEM, and
  66. * we judge it as being preferable to a kernel that will crash later when
  67. * someone tries to use patch_instruction().
  68. */
  69. void __init poking_init(void)
  70. {
  71. BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  72. "powerpc/text_poke:online", text_area_cpu_up,
  73. text_area_cpu_down));
  74. static_branch_enable(&poking_init_done);
  75. }
  76. static unsigned long get_patch_pfn(void *addr)
  77. {
  78. if (IS_ENABLED(CONFIG_MODULES) && is_vmalloc_or_module_addr(addr))
  79. return vmalloc_to_pfn(addr);
  80. else
  81. return __pa_symbol(addr) >> PAGE_SHIFT;
  82. }
  83. /*
  84. * This can be called for kernel text or a module.
  85. */
  86. static int map_patch_area(void *addr, unsigned long text_poke_addr)
  87. {
  88. unsigned long pfn = get_patch_pfn(addr);
  89. return map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
  90. }
  91. static void unmap_patch_area(unsigned long addr)
  92. {
  93. pte_t *ptep;
  94. pmd_t *pmdp;
  95. pud_t *pudp;
  96. p4d_t *p4dp;
  97. pgd_t *pgdp;
  98. pgdp = pgd_offset_k(addr);
  99. if (WARN_ON(pgd_none(*pgdp)))
  100. return;
  101. p4dp = p4d_offset(pgdp, addr);
  102. if (WARN_ON(p4d_none(*p4dp)))
  103. return;
  104. pudp = pud_offset(p4dp, addr);
  105. if (WARN_ON(pud_none(*pudp)))
  106. return;
  107. pmdp = pmd_offset(pudp, addr);
  108. if (WARN_ON(pmd_none(*pmdp)))
  109. return;
  110. ptep = pte_offset_kernel(pmdp, addr);
  111. if (WARN_ON(pte_none(*ptep)))
  112. return;
  113. /*
  114. * In hash, pte_clear flushes the tlb, in radix, we have to
  115. */
  116. pte_clear(&init_mm, addr, ptep);
  117. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  118. }
  119. static int __do_patch_instruction(u32 *addr, ppc_inst_t instr)
  120. {
  121. int err;
  122. u32 *patch_addr;
  123. unsigned long text_poke_addr;
  124. pte_t *pte;
  125. unsigned long pfn = get_patch_pfn(addr);
  126. text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr & PAGE_MASK;
  127. patch_addr = (u32 *)(text_poke_addr + offset_in_page(addr));
  128. pte = virt_to_kpte(text_poke_addr);
  129. __set_pte_at(&init_mm, text_poke_addr, pte, pfn_pte(pfn, PAGE_KERNEL), 0);
  130. /* See ptesync comment in radix__set_pte_at() */
  131. if (radix_enabled())
  132. asm volatile("ptesync": : :"memory");
  133. err = __patch_instruction(addr, instr, patch_addr);
  134. pte_clear(&init_mm, text_poke_addr, pte);
  135. flush_tlb_kernel_range(text_poke_addr, text_poke_addr + PAGE_SIZE);
  136. return err;
  137. }
  138. static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
  139. {
  140. int err;
  141. unsigned long flags;
  142. /*
  143. * During early early boot patch_instruction is called
  144. * when text_poke_area is not ready, but we still need
  145. * to allow patching. We just do the plain old patching
  146. */
  147. if (!static_branch_likely(&poking_init_done))
  148. return raw_patch_instruction(addr, instr);
  149. local_irq_save(flags);
  150. err = __do_patch_instruction(addr, instr);
  151. local_irq_restore(flags);
  152. return err;
  153. }
  154. #else /* !CONFIG_STRICT_KERNEL_RWX */
  155. static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
  156. {
  157. return raw_patch_instruction(addr, instr);
  158. }
  159. #endif /* CONFIG_STRICT_KERNEL_RWX */
  160. __ro_after_init DEFINE_STATIC_KEY_FALSE(init_mem_is_free);
  161. int patch_instruction(u32 *addr, ppc_inst_t instr)
  162. {
  163. /* Make sure we aren't patching a freed init section */
  164. if (static_branch_likely(&init_mem_is_free) && init_section_contains(addr, 4))
  165. return 0;
  166. return do_patch_instruction(addr, instr);
  167. }
  168. NOKPROBE_SYMBOL(patch_instruction);
  169. int patch_branch(u32 *addr, unsigned long target, int flags)
  170. {
  171. ppc_inst_t instr;
  172. if (create_branch(&instr, addr, target, flags))
  173. return -ERANGE;
  174. return patch_instruction(addr, instr);
  175. }
  176. /*
  177. * Helper to check if a given instruction is a conditional branch
  178. * Derived from the conditional checks in analyse_instr()
  179. */
  180. bool is_conditional_branch(ppc_inst_t instr)
  181. {
  182. unsigned int opcode = ppc_inst_primary_opcode(instr);
  183. if (opcode == 16) /* bc, bca, bcl, bcla */
  184. return true;
  185. if (opcode == 19) {
  186. switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
  187. case 16: /* bclr, bclrl */
  188. case 528: /* bcctr, bcctrl */
  189. case 560: /* bctar, bctarl */
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. NOKPROBE_SYMBOL(is_conditional_branch);
  196. int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
  197. unsigned long target, int flags)
  198. {
  199. long offset;
  200. offset = target;
  201. if (! (flags & BRANCH_ABSOLUTE))
  202. offset = offset - (unsigned long)addr;
  203. /* Check we can represent the target in the instruction format */
  204. if (!is_offset_in_cond_branch_range(offset))
  205. return 1;
  206. /* Mask out the flags and target, so they don't step on each other. */
  207. *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
  208. return 0;
  209. }
  210. int instr_is_relative_branch(ppc_inst_t instr)
  211. {
  212. if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
  213. return 0;
  214. return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
  215. }
  216. int instr_is_relative_link_branch(ppc_inst_t instr)
  217. {
  218. return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
  219. }
  220. static unsigned long branch_iform_target(const u32 *instr)
  221. {
  222. signed long imm;
  223. imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
  224. /* If the top bit of the immediate value is set this is negative */
  225. if (imm & 0x2000000)
  226. imm -= 0x4000000;
  227. if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
  228. imm += (unsigned long)instr;
  229. return (unsigned long)imm;
  230. }
  231. static unsigned long branch_bform_target(const u32 *instr)
  232. {
  233. signed long imm;
  234. imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
  235. /* If the top bit of the immediate value is set this is negative */
  236. if (imm & 0x8000)
  237. imm -= 0x10000;
  238. if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
  239. imm += (unsigned long)instr;
  240. return (unsigned long)imm;
  241. }
  242. unsigned long branch_target(const u32 *instr)
  243. {
  244. if (instr_is_branch_iform(ppc_inst_read(instr)))
  245. return branch_iform_target(instr);
  246. else if (instr_is_branch_bform(ppc_inst_read(instr)))
  247. return branch_bform_target(instr);
  248. return 0;
  249. }
  250. int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
  251. {
  252. unsigned long target;
  253. target = branch_target(src);
  254. if (instr_is_branch_iform(ppc_inst_read(src)))
  255. return create_branch(instr, dest, target,
  256. ppc_inst_val(ppc_inst_read(src)));
  257. else if (instr_is_branch_bform(ppc_inst_read(src)))
  258. return create_cond_branch(instr, dest, target,
  259. ppc_inst_val(ppc_inst_read(src)));
  260. return 1;
  261. }