opcodes.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/opcodes.c
  4. *
  5. * A32 condition code lookup feature moved from nwfpe/fpopcode.c
  6. */
  7. #include <linux/module.h>
  8. #include <asm/opcodes.h>
  9. #define ARM_OPCODE_CONDITION_UNCOND 0xf
  10. /*
  11. * condition code lookup table
  12. * index into the table is test code: EQ, NE, ... LT, GT, AL, NV
  13. *
  14. * bit position in short is condition code: NZCV
  15. */
  16. static const unsigned short cc_map[16] = {
  17. 0xF0F0, /* EQ == Z set */
  18. 0x0F0F, /* NE */
  19. 0xCCCC, /* CS == C set */
  20. 0x3333, /* CC */
  21. 0xFF00, /* MI == N set */
  22. 0x00FF, /* PL */
  23. 0xAAAA, /* VS == V set */
  24. 0x5555, /* VC */
  25. 0x0C0C, /* HI == C set && Z clear */
  26. 0xF3F3, /* LS == C clear || Z set */
  27. 0xAA55, /* GE == (N==V) */
  28. 0x55AA, /* LT == (N!=V) */
  29. 0x0A05, /* GT == (!Z && (N==V)) */
  30. 0xF5FA, /* LE == (Z || (N!=V)) */
  31. 0xFFFF, /* AL always */
  32. 0 /* NV */
  33. };
  34. /*
  35. * Returns:
  36. * ARM_OPCODE_CONDTEST_FAIL - if condition fails
  37. * ARM_OPCODE_CONDTEST_PASS - if condition passes (including AL)
  38. * ARM_OPCODE_CONDTEST_UNCOND - if NV condition, or separate unconditional
  39. * opcode space from v5 onwards
  40. *
  41. * Code that tests whether a conditional instruction would pass its condition
  42. * check should check that return value == ARM_OPCODE_CONDTEST_PASS.
  43. *
  44. * Code that tests if a condition means that the instruction would be executed
  45. * (regardless of conditional or unconditional) should instead check that the
  46. * return value != ARM_OPCODE_CONDTEST_FAIL.
  47. */
  48. asmlinkage unsigned int arm_check_condition(u32 opcode, u32 psr)
  49. {
  50. u32 cc_bits = opcode >> 28;
  51. u32 psr_cond = psr >> 28;
  52. unsigned int ret;
  53. if (cc_bits != ARM_OPCODE_CONDITION_UNCOND) {
  54. if ((cc_map[cc_bits] >> (psr_cond)) & 1)
  55. ret = ARM_OPCODE_CONDTEST_PASS;
  56. else
  57. ret = ARM_OPCODE_CONDTEST_FAIL;
  58. } else {
  59. ret = ARM_OPCODE_CONDTEST_UNCOND;
  60. }
  61. return ret;
  62. }
  63. EXPORT_SYMBOL_GPL(arm_check_condition);