alternative.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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;
  42. if (kernel_text_address(pc))
  43. return 1;
  44. replptr = (unsigned long)ALT_REPL_PTR(alt);
  45. if (pc >= replptr && pc <= (replptr + alt->alt_len))
  46. return 0;
  47. /*
  48. * Branching into *another* alternate sequence is doomed, and
  49. * we're not even trying to fix it up.
  50. */
  51. BUG();
  52. }
  53. #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
  54. static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
  55. {
  56. u32 insn;
  57. insn = le32_to_cpu(*altinsnptr);
  58. if (aarch64_insn_is_branch_imm(insn)) {
  59. s32 offset = aarch64_get_branch_offset(insn);
  60. unsigned long target;
  61. target = (unsigned long)altinsnptr + offset;
  62. /*
  63. * If we're branching inside the alternate sequence,
  64. * do not rewrite the instruction, as it is already
  65. * correct. Otherwise, generate the new instruction.
  66. */
  67. if (branch_insn_requires_update(alt, target)) {
  68. offset = target - (unsigned long)insnptr;
  69. insn = aarch64_set_branch_offset(insn, offset);
  70. }
  71. } else if (aarch64_insn_is_adrp(insn)) {
  72. s32 orig_offset, new_offset;
  73. unsigned long target;
  74. /*
  75. * If we're replacing an adrp instruction, which uses PC-relative
  76. * immediate addressing, adjust the offset to reflect the new
  77. * PC. adrp operates on 4K aligned addresses.
  78. */
  79. orig_offset = aarch64_insn_adrp_get_offset(insn);
  80. target = align_down(altinsnptr, SZ_4K) + orig_offset;
  81. new_offset = target - align_down(insnptr, SZ_4K);
  82. insn = aarch64_insn_adrp_set_offset(insn, new_offset);
  83. } else if (aarch64_insn_uses_literal(insn)) {
  84. /*
  85. * Disallow patching unhandled instructions using PC relative
  86. * literal addresses
  87. */
  88. BUG();
  89. }
  90. return insn;
  91. }
  92. static void patch_alternative(struct alt_instr *alt,
  93. __le32 *origptr, __le32 *updptr, int nr_inst)
  94. {
  95. __le32 *replptr;
  96. int i;
  97. replptr = ALT_REPL_PTR(alt);
  98. for (i = 0; i < nr_inst; i++) {
  99. u32 insn;
  100. insn = get_alt_insn(alt, origptr + i, replptr + i);
  101. updptr[i] = cpu_to_le32(insn);
  102. }
  103. }
  104. static void __apply_alternatives(void *alt_region, bool use_linear_alias)
  105. {
  106. struct alt_instr *alt;
  107. struct alt_region *region = alt_region;
  108. __le32 *origptr, *updptr;
  109. alternative_cb_t alt_cb;
  110. for (alt = region->begin; alt < region->end; alt++) {
  111. int nr_inst;
  112. /* Use ARM64_CB_PATCH as an unconditional patch */
  113. if (alt->cpufeature < ARM64_CB_PATCH &&
  114. !cpus_have_cap(alt->cpufeature))
  115. continue;
  116. if (alt->cpufeature == ARM64_CB_PATCH)
  117. BUG_ON(alt->alt_len != 0);
  118. else
  119. BUG_ON(alt->alt_len != alt->orig_len);
  120. pr_info_once("patching kernel code\n");
  121. origptr = ALT_ORIG_PTR(alt);
  122. updptr = use_linear_alias ? lm_alias(origptr) : origptr;
  123. nr_inst = alt->orig_len / AARCH64_INSN_SIZE;
  124. if (alt->cpufeature < ARM64_CB_PATCH)
  125. alt_cb = patch_alternative;
  126. else
  127. alt_cb = ALT_REPL_PTR(alt);
  128. alt_cb(alt, origptr, updptr, nr_inst);
  129. flush_icache_range((uintptr_t)origptr,
  130. (uintptr_t)(origptr + nr_inst));
  131. }
  132. }
  133. /*
  134. * We might be patching the stop_machine state machine, so implement a
  135. * really simple polling protocol here.
  136. */
  137. static int __apply_alternatives_multi_stop(void *unused)
  138. {
  139. struct alt_region region = {
  140. .begin = (struct alt_instr *)__alt_instructions,
  141. .end = (struct alt_instr *)__alt_instructions_end,
  142. };
  143. /* We always have a CPU 0 at this point (__init) */
  144. if (smp_processor_id()) {
  145. while (!READ_ONCE(alternatives_applied))
  146. cpu_relax();
  147. isb();
  148. } else {
  149. BUG_ON(alternatives_applied);
  150. __apply_alternatives(&region, true);
  151. /* Barriers provided by the cache flushing */
  152. WRITE_ONCE(alternatives_applied, 1);
  153. }
  154. return 0;
  155. }
  156. void __init apply_alternatives_all(void)
  157. {
  158. /* better not try code patching on a live SMP system */
  159. stop_machine(__apply_alternatives_multi_stop, NULL, cpu_online_mask);
  160. }
  161. void apply_alternatives(void *start, size_t length)
  162. {
  163. struct alt_region region = {
  164. .begin = start,
  165. .end = start + length,
  166. };
  167. __apply_alternatives(&region, false);
  168. }