jump_label.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Emil Renner Berthing
  4. *
  5. * Based on arch/arm64/kernel/jump_label.c
  6. */
  7. #include <linux/jump_label.h>
  8. #include <linux/kernel.h>
  9. #include <linux/memory.h>
  10. #include <linux/mutex.h>
  11. #include <asm/bug.h>
  12. #include <asm/patch.h>
  13. #define RISCV_INSN_NOP 0x00000013U
  14. #define RISCV_INSN_JAL 0x0000006fU
  15. void arch_jump_label_transform(struct jump_entry *entry,
  16. enum jump_label_type type)
  17. {
  18. void *addr = (void *)jump_entry_code(entry);
  19. u32 insn;
  20. if (type == JUMP_LABEL_JMP) {
  21. long offset = jump_entry_target(entry) - jump_entry_code(entry);
  22. if (WARN_ON(offset & 1 || offset < -524288 || offset >= 524288))
  23. return;
  24. insn = RISCV_INSN_JAL |
  25. (((u32)offset & GENMASK(19, 12)) << (12 - 12)) |
  26. (((u32)offset & GENMASK(11, 11)) << (20 - 11)) |
  27. (((u32)offset & GENMASK(10, 1)) << (21 - 1)) |
  28. (((u32)offset & GENMASK(20, 20)) << (31 - 20));
  29. } else {
  30. insn = RISCV_INSN_NOP;
  31. }
  32. mutex_lock(&text_mutex);
  33. patch_text_nosync(addr, &insn, sizeof(insn));
  34. mutex_unlock(&text_mutex);
  35. }
  36. void arch_jump_label_transform_static(struct jump_entry *entry,
  37. enum jump_label_type type)
  38. {
  39. /*
  40. * We use the same instructions in the arch_static_branch and
  41. * arch_static_branch_jump inline functions, so there's no
  42. * need to patch them up here.
  43. * The core will call arch_jump_label_transform when those
  44. * instructions need to be replaced.
  45. */
  46. }