alternative.h 8.1 KB

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