insn.c 1.3 KB

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