insn.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bug.h>
  3. #include <linux/kernel.h>
  4. #include <asm/opcodes.h>
  5. static unsigned long
  6. __arm_gen_branch_thumb2(unsigned long pc, unsigned long addr, bool link)
  7. {
  8. unsigned long s, j1, j2, i1, i2, imm10, imm11;
  9. unsigned long first, second;
  10. long offset;
  11. offset = (long)addr - (long)(pc + 4);
  12. if (offset < -16777216 || offset > 16777214) {
  13. WARN_ON_ONCE(1);
  14. return 0;
  15. }
  16. s = (offset >> 24) & 0x1;
  17. i1 = (offset >> 23) & 0x1;
  18. i2 = (offset >> 22) & 0x1;
  19. imm10 = (offset >> 12) & 0x3ff;
  20. imm11 = (offset >> 1) & 0x7ff;
  21. j1 = (!i1) ^ s;
  22. j2 = (!i2) ^ s;
  23. first = 0xf000 | (s << 10) | imm10;
  24. second = 0x9000 | (j1 << 13) | (j2 << 11) | imm11;
  25. if (link)
  26. second |= 1 << 14;
  27. return __opcode_thumb32_compose(first, second);
  28. }
  29. static unsigned long
  30. __arm_gen_branch_arm(unsigned long pc, unsigned long addr, bool link)
  31. {
  32. unsigned long opcode = 0xea000000;
  33. long offset;
  34. if (link)
  35. opcode |= 1 << 24;
  36. offset = (long)addr - (long)(pc + 8);
  37. if (unlikely(offset < -33554432 || offset > 33554428)) {
  38. WARN_ON_ONCE(1);
  39. return 0;
  40. }
  41. offset = (offset >> 2) & 0x00ffffff;
  42. return opcode | offset;
  43. }
  44. unsigned long
  45. __arm_gen_branch(unsigned long pc, unsigned long addr, bool link)
  46. {
  47. if (IS_ENABLED(CONFIG_THUMB2_KERNEL))
  48. return __arm_gen_branch_thumb2(pc, addr, link);
  49. else
  50. return __arm_gen_branch_arm(pc, addr, link);
  51. }