insn.c 1.3 KB

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