alternative.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef __ASM_ALTERNATIVE_H
  2. #define __ASM_ALTERNATIVE_H
  3. #ifndef __ASSEMBLY__
  4. #include <linux/init.h>
  5. #include <linux/kconfig.h>
  6. #include <linux/types.h>
  7. #include <linux/stddef.h>
  8. #include <linux/stringify.h>
  9. struct alt_instr {
  10. s32 orig_offset; /* offset to original instruction */
  11. s32 alt_offset; /* offset to replacement instruction */
  12. u16 cpufeature; /* cpufeature bit set for replacement */
  13. u8 orig_len; /* size of original instruction(s) */
  14. u8 alt_len; /* size of new instruction(s), <= orig_len */
  15. };
  16. void __init apply_alternatives_all(void);
  17. void apply_alternatives(void *start, size_t length);
  18. void free_alternatives_memory(void);
  19. #define ALTINSTR_ENTRY(feature) \
  20. " .word 661b - .\n" /* label */ \
  21. " .word 663f - .\n" /* new instruction */ \
  22. " .hword " __stringify(feature) "\n" /* feature bit */ \
  23. " .byte 662b-661b\n" /* source len */ \
  24. " .byte 664f-663f\n" /* replacement len */
  25. /*
  26. * alternative assembly primitive:
  27. *
  28. * If any of these .org directive fail, it means that insn1 and insn2
  29. * don't have the same length. This used to be written as
  30. *
  31. * .if ((664b-663b) != (662b-661b))
  32. * .error "Alternatives instruction length mismatch"
  33. * .endif
  34. *
  35. * but most assemblers die if insn1 or insn2 have a .inst. This should
  36. * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
  37. * containing commit 4e4d08cf7399b606 or c1baaddf8861).
  38. */
  39. #define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled) \
  40. ".if "__stringify(cfg_enabled)" == 1\n" \
  41. "661:\n\t" \
  42. oldinstr "\n" \
  43. "662:\n" \
  44. ".pushsection .altinstructions,\"a\"\n" \
  45. ALTINSTR_ENTRY(feature) \
  46. ".popsection\n" \
  47. ".pushsection .altinstr_replacement, \"a\"\n" \
  48. "663:\n\t" \
  49. newinstr "\n" \
  50. "664:\n\t" \
  51. ".popsection\n\t" \
  52. ".org . - (664b-663b) + (662b-661b)\n\t" \
  53. ".org . - (662b-661b) + (664b-663b)\n" \
  54. ".endif\n"
  55. #define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...) \
  56. __ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
  57. #else
  58. .macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
  59. .word \orig_offset - .
  60. .word \alt_offset - .
  61. .hword \feature
  62. .byte \orig_len
  63. .byte \alt_len
  64. .endm
  65. .macro alternative_insn insn1, insn2, cap, enable = 1
  66. .if \enable
  67. 661: \insn1
  68. 662: .pushsection .altinstructions, "a"
  69. altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
  70. .popsection
  71. .pushsection .altinstr_replacement, "ax"
  72. 663: \insn2
  73. 664: .popsection
  74. .org . - (664b-663b) + (662b-661b)
  75. .org . - (662b-661b) + (664b-663b)
  76. .endif
  77. .endm
  78. /*
  79. * Begin an alternative code sequence.
  80. *
  81. * The code that follows this macro will be assembled and linked as
  82. * normal. There are no restrictions on this code.
  83. */
  84. .macro alternative_if_not cap, enable = 1
  85. .if \enable
  86. .pushsection .altinstructions, "a"
  87. altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
  88. .popsection
  89. 661:
  90. .endif
  91. .endm
  92. /*
  93. * Provide the alternative code sequence.
  94. *
  95. * The code that follows this macro is assembled into a special
  96. * section to be used for dynamic patching. Code that follows this
  97. * macro must:
  98. *
  99. * 1. Be exactly the same length (in bytes) as the default code
  100. * sequence.
  101. *
  102. * 2. Not contain a branch target that is used outside of the
  103. * alternative sequence it is defined in (branches into an
  104. * alternative sequence are not fixed up).
  105. */
  106. .macro alternative_else, enable = 1
  107. .if \enable
  108. 662: .pushsection .altinstr_replacement, "ax"
  109. 663:
  110. .endif
  111. .endm
  112. /*
  113. * Complete an alternative code sequence.
  114. */
  115. .macro alternative_endif, enable = 1
  116. .if \enable
  117. 664: .popsection
  118. .org . - (664b-663b) + (662b-661b)
  119. .org . - (662b-661b) + (664b-663b)
  120. .endif
  121. .endm
  122. #define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...) \
  123. alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)
  124. #endif /* __ASSEMBLY__ */
  125. /*
  126. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature));
  127. *
  128. * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature, CONFIG_FOO));
  129. * N.B. If CONFIG_FOO is specified, but not selected, the whole block
  130. * will be omitted, including oldinstr.
  131. */
  132. #define ALTERNATIVE(oldinstr, newinstr, ...) \
  133. _ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)
  134. #endif /* __ASM_ALTERNATIVE_H */