alternative.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * alternative runtime patching
  3. * inspired by the x86 version
  4. *
  5. * Copyright (C) 2014 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define pr_fmt(fmt) "alternatives: " fmt
  20. #include <linux/init.h>
  21. #include <linux/cpu.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/alternative.h>
  24. #include <asm/cpufeature.h>
  25. #include <asm/insn.h>
  26. #include <asm/sections.h>
  27. #include <linux/stop_machine.h>
  28. #define __ALT_PTR(a,f) ((void *)&(a)->f + (a)->f)
  29. #define ALT_ORIG_PTR(a) __ALT_PTR(a, orig_offset)
  30. #define ALT_REPL_PTR(a) __ALT_PTR(a, alt_offset)
  31. int alternatives_applied;
  32. struct alt_region {
  33. struct alt_instr *begin;
  34. struct alt_instr *end;
  35. };
  36. /*
  37. * Check if the target PC is within an alternative block.
  38. */
  39. static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
  40. {
  41. unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt);
  42. return !(pc >= replptr && pc <= (replptr + alt->alt_len));
  43. }
  44. #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
  45. static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
  46. {
  47. u32 insn;
  48. insn = le32_to_cpu(*altinsnptr);
  49. if (aarch64_insn_is_branch_imm(insn)) {
  50. s32 offset = aarch64_get_branch_offset(insn);
  51. unsigned long target;
  52. target = (unsigned long)altinsnptr + offset;
  53. /*
  54. * If we're branching inside the alternate sequence,
  55. * do not rewrite the instruction, as it is already
  56. * correct. Otherwise, generate the new instruction.
  57. */
  58. if (branch_insn_requires_update(alt, target)) {
  59. offset = target - (unsigned long)insnptr;
  60. insn = aarch64_set_branch_offset(insn, offset);
  61. }
  62. } else if (aarch64_insn_is_adrp(insn)) {
  63. s32 orig_offset, new_offset;
  64. unsigned long target;
  65. /*
  66. * If we're replacing an adrp instruction, which uses PC-relative
  67. * immediate addressing, adjust the offset to reflect the new
  68. * PC. adrp operates on 4K aligned addresses.
  69. */
  70. orig_offset = aarch64_insn_adrp_get_offset(insn);
  71. target = align_down(altinsnptr, SZ_4K) + orig_offset;
  72. new_offset = target - align_down(insnptr, SZ_4K);
  73. insn = aarch64_insn_adrp_set_offset(insn, new_offset);
  74. } else if (aarch64_insn_uses_literal(insn)) {
  75. /*
  76. * Disallow patching unhandled instructions using PC relative
  77. * literal addresses
  78. */
  79. BUG();
  80. }
  81. return insn;
  82. }
  83. static void patch_alternative(struct alt_instr *alt,
  84. __le32 *origptr, __le32 *updptr, int nr_inst)
  85. {
  86. __le32 *replptr;
  87. int i;
  88. replptr = ALT_REPL_PTR(alt);
  89. for (i = 0; i < nr_inst; i++) {
  90. u32 insn;
  91. insn = get_alt_insn(alt, origptr + i, replptr + i);
  92. updptr[i] = cpu_to_le32(insn);
  93. }
  94. }
  95. /*
  96. * We provide our own, private D-cache cleaning function so that we don't
  97. * accidentally call into the cache.S code, which is patched by us at
  98. * runtime.
  99. */
  100. static void clean_dcache_range_nopatch(u64 start, u64 end)
  101. {
  102. u64 cur, d_size, ctr_el0;
  103. ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0);
  104. d_size = 4 << cpuid_feature_extract_unsigned_field(ctr_el0,
  105. CTR_DMINLINE_SHIFT);
  106. cur = start & ~(d_size - 1);
  107. do {
  108. /*
  109. * We must clean+invalidate to the PoC in order to avoid
  110. * Cortex-A53 errata 826319, 827319, 824069 and 819472
  111. * (this corresponds to ARM64_WORKAROUND_CLEAN_CACHE)
  112. */
  113. asm volatile("dc civac, %0" : : "r" (cur) : "memory");
  114. } while (cur += d_size, cur < end);
  115. }
  116. static void __apply_alternatives(void *alt_region, bool is_module)
  117. {
  118. struct alt_instr *alt;
  119. struct alt_region *region = alt_region;
  120. __le32 *origptr, *updptr;
  121. alternative_cb_t alt_cb;
  122. for (alt = region->begin; alt < region->end; alt++) {
  123. int nr_inst;
  124. /* Use ARM64_CB_PATCH as an unconditional patch */
  125. if (alt->cpufeature < ARM64_CB_PATCH &&
  126. !cpus_have_cap(alt->cpufeature))
  127. continue;
  128. if (alt->cpufeature == ARM64_CB_PATCH)
  129. BUG_ON(alt->alt_len != 0);
  130. else
  131. BUG_ON(alt->alt_len != alt->orig_len);
  132. pr_info_once("patching kernel code\n");
  133. origptr = ALT_ORIG_PTR(alt);
  134. updptr = is_module ? origptr : lm_alias(origptr);
  135. nr_inst = alt->orig_len / AARCH64_INSN_SIZE;
  136. if (alt->cpufeature < ARM64_CB_PATCH)
  137. alt_cb = patch_alternative;
  138. else
  139. alt_cb = ALT_REPL_PTR(alt);
  140. alt_cb(alt, origptr, updptr, nr_inst);
  141. if (!is_module) {
  142. clean_dcache_range_nopatch((u64)origptr,
  143. (u64)(origptr + nr_inst));
  144. }
  145. }
  146. /*
  147. * The core module code takes care of cache maintenance in
  148. * flush_module_icache().
  149. */
  150. if (!is_module) {
  151. dsb(ish);
  152. __flush_icache_all();
  153. isb();
  154. }
  155. }
  156. /*
  157. * We might be patching the stop_machine state machine, so implement a
  158. * really simple polling protocol here.
  159. */
  160. static int __apply_alternatives_multi_stop(void *unused)
  161. {
  162. struct alt_region region = {
  163. .begin = (struct alt_instr *)__alt_instructions,
  164. .end = (struct alt_instr *)__alt_instructions_end,
  165. };
  166. /* We always have a CPU 0 at this point (__init) */
  167. if (smp_processor_id()) {
  168. while (!READ_ONCE(alternatives_applied))
  169. cpu_relax();
  170. isb();
  171. } else {
  172. BUG_ON(alternatives_applied);
  173. __apply_alternatives(&region, false);
  174. /* Barriers provided by the cache flushing */
  175. WRITE_ONCE(alternatives_applied, 1);
  176. }
  177. return 0;
  178. }
  179. void __init apply_alternatives_all(void)
  180. {
  181. /* better not try code patching on a live SMP system */
  182. stop_machine(__apply_alternatives_multi_stop, NULL, cpu_online_mask);
  183. }
  184. #ifdef CONFIG_MODULES
  185. void apply_alternatives_module(void *start, size_t length)
  186. {
  187. struct alt_region region = {
  188. .begin = start,
  189. .end = start + length,
  190. };
  191. __apply_alternatives(&region, true);
  192. }
  193. #endif