alternative.c 4.6 KB

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