alternative.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <linux/stop_machine.h>
  27. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  28. struct alt_region {
  29. struct alt_instr *begin;
  30. struct alt_instr *end;
  31. };
  32. /*
  33. * Decode the imm field of a b/bl instruction, and return the byte
  34. * offset as a signed value (so it can be used when computing a new
  35. * branch target).
  36. */
  37. static s32 get_branch_offset(u32 insn)
  38. {
  39. s32 imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_26, insn);
  40. /* sign-extend the immediate before turning it into a byte offset */
  41. return (imm << 6) >> 4;
  42. }
  43. static u32 get_alt_insn(u8 *insnptr, u8 *altinsnptr)
  44. {
  45. u32 insn;
  46. aarch64_insn_read(altinsnptr, &insn);
  47. /* Stop the world on instructions we don't support... */
  48. BUG_ON(aarch64_insn_is_cbz(insn));
  49. BUG_ON(aarch64_insn_is_cbnz(insn));
  50. BUG_ON(aarch64_insn_is_bcond(insn));
  51. /* ... and there is probably more. */
  52. if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn)) {
  53. enum aarch64_insn_branch_type type;
  54. unsigned long target;
  55. if (aarch64_insn_is_b(insn))
  56. type = AARCH64_INSN_BRANCH_NOLINK;
  57. else
  58. type = AARCH64_INSN_BRANCH_LINK;
  59. target = (unsigned long)altinsnptr + get_branch_offset(insn);
  60. insn = aarch64_insn_gen_branch_imm((unsigned long)insnptr,
  61. target, type);
  62. }
  63. return insn;
  64. }
  65. static int __apply_alternatives(void *alt_region)
  66. {
  67. struct alt_instr *alt;
  68. struct alt_region *region = alt_region;
  69. u8 *origptr, *replptr;
  70. for (alt = region->begin; alt < region->end; alt++) {
  71. u32 insn;
  72. int i;
  73. if (!cpus_have_cap(alt->cpufeature))
  74. continue;
  75. BUG_ON(alt->alt_len != alt->orig_len);
  76. pr_info_once("patching kernel code\n");
  77. origptr = (u8 *)&alt->orig_offset + alt->orig_offset;
  78. replptr = (u8 *)&alt->alt_offset + alt->alt_offset;
  79. for (i = 0; i < alt->alt_len; i += sizeof(insn)) {
  80. insn = get_alt_insn(origptr + i, replptr + i);
  81. aarch64_insn_write(origptr + i, insn);
  82. }
  83. flush_icache_range((uintptr_t)origptr,
  84. (uintptr_t)(origptr + alt->alt_len));
  85. }
  86. return 0;
  87. }
  88. void apply_alternatives_all(void)
  89. {
  90. struct alt_region region = {
  91. .begin = __alt_instructions,
  92. .end = __alt_instructions_end,
  93. };
  94. /* better not try code patching on a live SMP system */
  95. stop_machine(__apply_alternatives, &region, NULL);
  96. }
  97. void apply_alternatives(void *start, size_t length)
  98. {
  99. struct alt_region region = {
  100. .begin = start,
  101. .end = start + length,
  102. };
  103. __apply_alternatives(&region);
  104. }
  105. void free_alternatives_memory(void)
  106. {
  107. free_reserved_area(__alt_instructions, __alt_instructions_end,
  108. 0, "alternatives");
  109. }