jump_label.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * jump label x86 support
  4. *
  5. * Copyright (C) 2009 Jason Baron <[email protected]>
  6. *
  7. */
  8. #include <linux/jump_label.h>
  9. #include <linux/memory.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/module.h>
  12. #include <linux/list.h>
  13. #include <linux/jhash.h>
  14. #include <linux/cpu.h>
  15. #include <asm/kprobes.h>
  16. #include <asm/alternative.h>
  17. #include <asm/text-patching.h>
  18. #include <asm/insn.h>
  19. int arch_jump_entry_size(struct jump_entry *entry)
  20. {
  21. struct insn insn = {};
  22. insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
  23. BUG_ON(insn.length != 2 && insn.length != 5);
  24. return insn.length;
  25. }
  26. struct jump_label_patch {
  27. const void *code;
  28. int size;
  29. };
  30. static struct jump_label_patch
  31. __jump_label_patch(struct jump_entry *entry, enum jump_label_type type)
  32. {
  33. const void *expect, *code, *nop;
  34. const void *addr, *dest;
  35. int size;
  36. addr = (void *)jump_entry_code(entry);
  37. dest = (void *)jump_entry_target(entry);
  38. size = arch_jump_entry_size(entry);
  39. switch (size) {
  40. case JMP8_INSN_SIZE:
  41. code = text_gen_insn(JMP8_INSN_OPCODE, addr, dest);
  42. nop = x86_nops[size];
  43. break;
  44. case JMP32_INSN_SIZE:
  45. code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
  46. nop = x86_nops[size];
  47. break;
  48. default: BUG();
  49. }
  50. if (type == JUMP_LABEL_JMP)
  51. expect = nop;
  52. else
  53. expect = code;
  54. if (memcmp(addr, expect, size)) {
  55. /*
  56. * The location is not an op that we were expecting.
  57. * Something went wrong. Crash the box, as something could be
  58. * corrupting the kernel.
  59. */
  60. pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph != %5ph)) size:%d type:%d\n",
  61. addr, addr, addr, expect, size, type);
  62. BUG();
  63. }
  64. if (type == JUMP_LABEL_NOP)
  65. code = nop;
  66. return (struct jump_label_patch){.code = code, .size = size};
  67. }
  68. static __always_inline void
  69. __jump_label_transform(struct jump_entry *entry,
  70. enum jump_label_type type,
  71. int init)
  72. {
  73. const struct jump_label_patch jlp = __jump_label_patch(entry, type);
  74. /*
  75. * As long as only a single processor is running and the code is still
  76. * not marked as RO, text_poke_early() can be used; Checking that
  77. * system_state is SYSTEM_BOOTING guarantees it. It will be set to
  78. * SYSTEM_SCHEDULING before other cores are awaken and before the
  79. * code is write-protected.
  80. *
  81. * At the time the change is being done, just ignore whether we
  82. * are doing nop -> jump or jump -> nop transition, and assume
  83. * always nop being the 'currently valid' instruction
  84. */
  85. if (init || system_state == SYSTEM_BOOTING) {
  86. text_poke_early((void *)jump_entry_code(entry), jlp.code, jlp.size);
  87. return;
  88. }
  89. text_poke_bp((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
  90. }
  91. static void __ref jump_label_transform(struct jump_entry *entry,
  92. enum jump_label_type type,
  93. int init)
  94. {
  95. mutex_lock(&text_mutex);
  96. __jump_label_transform(entry, type, init);
  97. mutex_unlock(&text_mutex);
  98. }
  99. void arch_jump_label_transform(struct jump_entry *entry,
  100. enum jump_label_type type)
  101. {
  102. jump_label_transform(entry, type, 0);
  103. }
  104. bool arch_jump_label_transform_queue(struct jump_entry *entry,
  105. enum jump_label_type type)
  106. {
  107. struct jump_label_patch jlp;
  108. if (system_state == SYSTEM_BOOTING) {
  109. /*
  110. * Fallback to the non-batching mode.
  111. */
  112. arch_jump_label_transform(entry, type);
  113. return true;
  114. }
  115. mutex_lock(&text_mutex);
  116. jlp = __jump_label_patch(entry, type);
  117. text_poke_queue((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
  118. mutex_unlock(&text_mutex);
  119. return true;
  120. }
  121. void arch_jump_label_transform_apply(void)
  122. {
  123. mutex_lock(&text_mutex);
  124. text_poke_finish();
  125. mutex_unlock(&text_mutex);
  126. }