jump_label.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/jump_label.h>
  3. #include <linux/kernel.h>
  4. #include <linux/memory.h>
  5. #include <linux/mutex.h>
  6. #include <linux/uaccess.h>
  7. #include <asm/cacheflush.h>
  8. #define NOP32_HI 0xc400
  9. #define NOP32_LO 0x4820
  10. #define BSR_LINK 0xe000
  11. void arch_jump_label_transform(struct jump_entry *entry,
  12. enum jump_label_type type)
  13. {
  14. unsigned long addr = jump_entry_code(entry);
  15. u16 insn[2];
  16. int ret = 0;
  17. if (type == JUMP_LABEL_JMP) {
  18. long offset = jump_entry_target(entry) - jump_entry_code(entry);
  19. if (WARN_ON(offset & 1 || offset < -67108864 || offset >= 67108864))
  20. return;
  21. offset = offset >> 1;
  22. insn[0] = BSR_LINK |
  23. ((uint16_t)((unsigned long) offset >> 16) & 0x3ff);
  24. insn[1] = (uint16_t)((unsigned long) offset & 0xffff);
  25. } else {
  26. insn[0] = NOP32_HI;
  27. insn[1] = NOP32_LO;
  28. }
  29. ret = copy_to_kernel_nofault((void *)addr, insn, 4);
  30. WARN_ON(ret);
  31. flush_icache_range(addr, addr + 4);
  32. }
  33. void arch_jump_label_transform_static(struct jump_entry *entry,
  34. enum jump_label_type type)
  35. {
  36. /*
  37. * We use the same instructions in the arch_static_branch and
  38. * arch_static_branch_jump inline functions, so there's no
  39. * need to patch them up here.
  40. * The core will call arch_jump_label_transform when those
  41. * instructions need to be replaced.
  42. */
  43. arch_jump_label_transform(entry, type);
  44. }