alternative.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef _ASM_X86_ALTERNATIVE_H
  2. #define _ASM_X86_ALTERNATIVE_H
  3. #ifndef __ASSEMBLY__
  4. #include <linux/types.h>
  5. #include <linux/stddef.h>
  6. #include <linux/stringify.h>
  7. #include <asm/asm.h>
  8. /*
  9. * Alternative inline assembly for SMP.
  10. *
  11. * The LOCK_PREFIX macro defined here replaces the LOCK and
  12. * LOCK_PREFIX macros used everywhere in the source tree.
  13. *
  14. * SMP alternatives use the same data structures as the other
  15. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  16. * UP system running a SMP kernel. The existing apply_alternatives()
  17. * works fine for patching a SMP kernel for UP.
  18. *
  19. * The SMP alternative tables can be kept after boot and contain both
  20. * UP and SMP versions of the instructions to allow switching back to
  21. * SMP at runtime, when hotplugging in a new CPU, which is especially
  22. * useful in virtualized environments.
  23. *
  24. * The very common lock prefix is handled as special case in a
  25. * separate table which is a pure address list without replacement ptr
  26. * and size information. That keeps the table sizes small.
  27. */
  28. #ifdef CONFIG_SMP
  29. #define LOCK_PREFIX_HERE \
  30. ".pushsection .smp_locks,\"a\"\n" \
  31. ".balign 4\n" \
  32. ".long 671f - .\n" /* offset */ \
  33. ".popsection\n" \
  34. "671:"
  35. #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
  36. #else /* ! CONFIG_SMP */
  37. #define LOCK_PREFIX_HERE ""
  38. #define LOCK_PREFIX ""
  39. #endif
  40. struct alt_instr {
  41. s32 instr_offset; /* original instruction */
  42. s32 repl_offset; /* offset to replacement instruction */
  43. u16 cpuid; /* cpuid bit set for replacement */
  44. u8 instrlen; /* length of original instruction */
  45. u8 replacementlen; /* length of new instruction */
  46. u8 padlen; /* length of build-time padding */
  47. } __packed;
  48. /*
  49. * Debug flag that can be tested to see whether alternative
  50. * instructions were patched in already:
  51. */
  52. extern int alternatives_patched;
  53. extern void alternative_instructions(void);
  54. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  55. struct module;
  56. #ifdef CONFIG_SMP
  57. extern void alternatives_smp_module_add(struct module *mod, char *name,
  58. void *locks, void *locks_end,
  59. void *text, void *text_end);
  60. extern void alternatives_smp_module_del(struct module *mod);
  61. extern void alternatives_enable_smp(void);
  62. extern int alternatives_text_reserved(void *start, void *end);
  63. extern bool skip_smp_alternatives;
  64. #else
  65. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  66. void *locks, void *locks_end,
  67. void *text, void *text_end) {}
  68. static inline void alternatives_smp_module_del(struct module *mod) {}
  69. static inline void alternatives_enable_smp(void) {}
  70. static inline int alternatives_text_reserved(void *start, void *end)
  71. {
  72. return 0;
  73. }
  74. #endif /* CONFIG_SMP */
  75. #define b_replacement(num) "664"#num
  76. #define e_replacement(num) "665"#num
  77. #define alt_end_marker "663"
  78. #define alt_slen "662b-661b"
  79. #define alt_pad_len alt_end_marker"b-662b"
  80. #define alt_total_slen alt_end_marker"b-661b"
  81. #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
  82. #define __OLDINSTR(oldinstr, num) \
  83. "661:\n\t" oldinstr "\n662:\n" \
  84. ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
  85. "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
  86. #define OLDINSTR(oldinstr, num) \
  87. __OLDINSTR(oldinstr, num) \
  88. alt_end_marker ":\n"
  89. /*
  90. * gas compatible max based on the idea from:
  91. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
  92. *
  93. * The additional "-" is needed because gas uses a "true" value of -1.
  94. */
  95. #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
  96. /*
  97. * Pad the second replacement alternative with additional NOPs if it is
  98. * additionally longer than the first replacement alternative.
  99. */
  100. #define OLDINSTR_2(oldinstr, num1, num2) \
  101. "661:\n\t" oldinstr "\n662:\n" \
  102. ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
  103. "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
  104. alt_end_marker ":\n"
  105. #define ALTINSTR_ENTRY(feature, num) \
  106. " .long 661b - .\n" /* label */ \
  107. " .long " b_replacement(num)"f - .\n" /* new instruction */ \
  108. " .word " __stringify(feature) "\n" /* feature bit */ \
  109. " .byte " alt_total_slen "\n" /* source len */ \
  110. " .byte " alt_rlen(num) "\n" /* replacement len */ \
  111. " .byte " alt_pad_len "\n" /* pad len */
  112. #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
  113. b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
  114. /* alternative assembly primitive: */
  115. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  116. OLDINSTR(oldinstr, 1) \
  117. ".pushsection .altinstructions,\"a\"\n" \
  118. ALTINSTR_ENTRY(feature, 1) \
  119. ".popsection\n" \
  120. ".pushsection .altinstr_replacement, \"ax\"\n" \
  121. ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
  122. ".popsection"
  123. #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
  124. OLDINSTR_2(oldinstr, 1, 2) \
  125. ".pushsection .altinstructions,\"a\"\n" \
  126. ALTINSTR_ENTRY(feature1, 1) \
  127. ALTINSTR_ENTRY(feature2, 2) \
  128. ".popsection\n" \
  129. ".pushsection .altinstr_replacement, \"ax\"\n" \
  130. ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
  131. ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
  132. ".popsection"
  133. /*
  134. * Alternative instructions for different CPU types or capabilities.
  135. *
  136. * This allows to use optimized instructions even on generic binary
  137. * kernels.
  138. *
  139. * length of oldinstr must be longer or equal the length of newinstr
  140. * It can be padded with nops as needed.
  141. *
  142. * For non barrier like inlines please define new variants
  143. * without volatile and memory clobber.
  144. */
  145. #define alternative(oldinstr, newinstr, feature) \
  146. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  147. #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
  148. asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
  149. /*
  150. * Alternative inline assembly with input.
  151. *
  152. * Pecularities:
  153. * No memory clobber here.
  154. * Argument numbers start with 1.
  155. * Best is to use constraints that are fixed size (like (%1) ... "r")
  156. * If you use variable sized constraints like "m" or "g" in the
  157. * replacement make sure to pad to the worst case length.
  158. * Leaving an unused argument 0 to keep API compatibility.
  159. */
  160. #define alternative_input(oldinstr, newinstr, feature, input...) \
  161. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  162. : : "i" (0), ## input)
  163. /*
  164. * This is similar to alternative_input. But it has two features and
  165. * respective instructions.
  166. *
  167. * If CPU has feature2, newinstr2 is used.
  168. * Otherwise, if CPU has feature1, newinstr1 is used.
  169. * Otherwise, oldinstr is used.
  170. */
  171. #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
  172. feature2, input...) \
  173. asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
  174. newinstr2, feature2) \
  175. : : "i" (0), ## input)
  176. /* Like alternative_input, but with a single output argument */
  177. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  178. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  179. : output : "i" (0), ## input)
  180. /* Like alternative_io, but for replacing a direct call with another one. */
  181. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  182. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  183. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  184. /*
  185. * Like alternative_call, but there are two features and respective functions.
  186. * If CPU has feature2, function2 is used.
  187. * Otherwise, if CPU has feature1, function1 is used.
  188. * Otherwise, old function is used.
  189. */
  190. #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
  191. output, input...) \
  192. { \
  193. register void *__sp asm(_ASM_SP); \
  194. asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
  195. "call %P[new2]", feature2) \
  196. : output, "+r" (__sp) \
  197. : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
  198. [new2] "i" (newfunc2), ## input); \
  199. }
  200. /*
  201. * use this macro(s) if you need more than one output parameter
  202. * in alternative_io
  203. */
  204. #define ASM_OUTPUT2(a...) a
  205. /*
  206. * use this macro if you need clobbers but no inputs in
  207. * alternative_{input,io,call}()
  208. */
  209. #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
  210. #endif /* __ASSEMBLY__ */
  211. #endif /* _ASM_X86_ALTERNATIVE_H */