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