alternative.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_ALTERNATIVE_H
  3. #define _ASM_ALTERNATIVE_H
  4. #ifndef __ASSEMBLER__
  5. #include <linux/types.h>
  6. #include <linux/stddef.h>
  7. #include <linux/stringify.h>
  8. #include <asm/asm.h>
  9. struct alt_instr {
  10. s32 instr_offset; /* offset to original instruction */
  11. s32 replace_offset; /* offset to replacement instruction */
  12. u16 feature; /* feature bit set for replacement */
  13. u8 instrlen; /* length of original instruction */
  14. u8 replacementlen; /* length of new instruction */
  15. } __packed;
  16. /*
  17. * Debug flag that can be tested to see whether alternative
  18. * instructions were patched in already:
  19. */
  20. extern int alternatives_patched;
  21. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  22. extern void alternative_instructions(void);
  23. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  24. #define b_replacement(num) "664"#num
  25. #define e_replacement(num) "665"#num
  26. #define alt_end_marker "663"
  27. #define alt_slen "662b-661b"
  28. #define alt_total_slen alt_end_marker"b-661b"
  29. #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
  30. #define __OLDINSTR(oldinstr, num) \
  31. "661:\n\t" oldinstr "\n662:\n" \
  32. ".fill -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
  33. "((" alt_rlen(num) ")-(" alt_slen ")) / 4, 4, 0x03400000\n"
  34. #define OLDINSTR(oldinstr, num) \
  35. __OLDINSTR(oldinstr, num) \
  36. alt_end_marker ":\n"
  37. #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
  38. /*
  39. * Pad the second replacement alternative with additional NOPs if it is
  40. * additionally longer than the first replacement alternative.
  41. */
  42. #define OLDINSTR_2(oldinstr, num1, num2) \
  43. "661:\n\t" oldinstr "\n662:\n" \
  44. ".fill -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
  45. "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) / 4, " \
  46. "4, 0x03400000\n" \
  47. alt_end_marker ":\n"
  48. #define ALTINSTR_ENTRY(feature, num) \
  49. " .long 661b - .\n" /* label */ \
  50. " .long " b_replacement(num)"f - .\n" /* new instruction */ \
  51. " .short " __stringify(feature) "\n" /* feature bit */ \
  52. " .byte " alt_total_slen "\n" /* source len */ \
  53. " .byte " alt_rlen(num) "\n" /* replacement len */
  54. #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
  55. b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
  56. /* alternative assembly primitive: */
  57. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  58. OLDINSTR(oldinstr, 1) \
  59. ".pushsection .altinstructions,\"a\"\n" \
  60. ALTINSTR_ENTRY(feature, 1) \
  61. ".popsection\n" \
  62. ".subsection 1\n" \
  63. ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
  64. ".previous\n"
  65. #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
  66. OLDINSTR_2(oldinstr, 1, 2) \
  67. ".pushsection .altinstructions,\"a\"\n" \
  68. ALTINSTR_ENTRY(feature1, 1) \
  69. ALTINSTR_ENTRY(feature2, 2) \
  70. ".popsection\n" \
  71. ".subsection 1\n" \
  72. ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
  73. ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
  74. ".previous\n"
  75. /*
  76. * Alternative instructions for different CPU types or capabilities.
  77. *
  78. * This allows to use optimized instructions even on generic binary
  79. * kernels.
  80. *
  81. * length of oldinstr must be longer or equal the length of newinstr
  82. * It can be padded with nops as needed.
  83. *
  84. * For non barrier like inlines please define new variants
  85. * without volatile and memory clobber.
  86. */
  87. #define alternative(oldinstr, newinstr, feature) \
  88. (asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory"))
  89. #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
  90. (asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory"))
  91. #endif /* __ASSEMBLER__ */
  92. #endif /* _ASM_ALTERNATIVE_H */