jump_label.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/cacheflush.h>
  13. #include <asm/patch.h>
  14. #define RISCV_INSN_NOP 0x00000013U
  15. #define RISCV_INSN_JAL 0x0000006fU
  16. bool arch_jump_label_transform_queue(struct jump_entry *entry,
  17. enum jump_label_type type)
  18. {
  19. void *addr = (void *)jump_entry_code(entry);
  20. u32 insn;
  21. if (type == JUMP_LABEL_JMP) {
  22. long offset = jump_entry_target(entry) - jump_entry_code(entry);
  23. if (WARN_ON(offset & 1 || offset < -524288 || offset >= 524288))
  24. return true;
  25. insn = RISCV_INSN_JAL |
  26. (((u32)offset & GENMASK(19, 12)) << (12 - 12)) |
  27. (((u32)offset & GENMASK(11, 11)) << (20 - 11)) |
  28. (((u32)offset & GENMASK(10, 1)) << (21 - 1)) |
  29. (((u32)offset & GENMASK(20, 20)) << (31 - 20));
  30. } else {
  31. insn = RISCV_INSN_NOP;
  32. }
  33. mutex_lock(&text_mutex);
  34. patch_insn_write(addr, &insn, sizeof(insn));
  35. mutex_unlock(&text_mutex);
  36. return true;
  37. }
  38. void arch_jump_label_transform_apply(void)
  39. {
  40. flush_icache_all();
  41. }