alternative.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ".section .smp_locks,\"a\"\n" \
  30. ".balign 4\n" \
  31. ".long 671f - .\n" /* offset */ \
  32. ".previous\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, <= instrlen */
  45. };
  46. extern void alternative_instructions(void);
  47. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  48. struct module;
  49. #ifdef CONFIG_SMP
  50. extern void alternatives_smp_module_add(struct module *mod, char *name,
  51. void *locks, void *locks_end,
  52. void *text, void *text_end);
  53. extern void alternatives_smp_module_del(struct module *mod);
  54. extern void alternatives_smp_switch(int smp);
  55. extern int alternatives_text_reserved(void *start, void *end);
  56. extern bool skip_smp_alternatives;
  57. #else
  58. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  59. void *locks, void *locks_end,
  60. void *text, void *text_end) {}
  61. static inline void alternatives_smp_module_del(struct module *mod) {}
  62. static inline void alternatives_smp_switch(int smp) {}
  63. static inline int alternatives_text_reserved(void *start, void *end)
  64. {
  65. return 0;
  66. }
  67. #endif /* CONFIG_SMP */
  68. #define OLDINSTR(oldinstr) "661:\n\t" oldinstr "\n662:\n"
  69. #define b_replacement(number) "663"#number
  70. #define e_replacement(number) "664"#number
  71. #define alt_slen "662b-661b"
  72. #define alt_rlen(number) e_replacement(number)"f-"b_replacement(number)"f"
  73. #define ALTINSTR_ENTRY(feature, number) \
  74. " .long 661b - .\n" /* label */ \
  75. " .long " b_replacement(number)"f - .\n" /* new instruction */ \
  76. " .word " __stringify(feature) "\n" /* feature bit */ \
  77. " .byte " alt_slen "\n" /* source len */ \
  78. " .byte " alt_rlen(number) "\n" /* replacement len */
  79. #define DISCARD_ENTRY(number) /* rlen <= slen */ \
  80. " .byte 0xff + (" alt_rlen(number) ") - (" alt_slen ")\n"
  81. #define ALTINSTR_REPLACEMENT(newinstr, feature, number) /* replacement */ \
  82. b_replacement(number)":\n\t" newinstr "\n" e_replacement(number) ":\n\t"
  83. /* alternative assembly primitive: */
  84. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  85. OLDINSTR(oldinstr) \
  86. ".section .altinstructions,\"a\"\n" \
  87. ALTINSTR_ENTRY(feature, 1) \
  88. ".previous\n" \
  89. ".section .discard,\"aw\",@progbits\n" \
  90. DISCARD_ENTRY(1) \
  91. ".previous\n" \
  92. ".section .altinstr_replacement, \"ax\"\n" \
  93. ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
  94. ".previous"
  95. #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
  96. OLDINSTR(oldinstr) \
  97. ".section .altinstructions,\"a\"\n" \
  98. ALTINSTR_ENTRY(feature1, 1) \
  99. ALTINSTR_ENTRY(feature2, 2) \
  100. ".previous\n" \
  101. ".section .discard,\"aw\",@progbits\n" \
  102. DISCARD_ENTRY(1) \
  103. DISCARD_ENTRY(2) \
  104. ".previous\n" \
  105. ".section .altinstr_replacement, \"ax\"\n" \
  106. ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
  107. ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
  108. ".previous"
  109. /*
  110. * This must be included *after* the definition of ALTERNATIVE due to
  111. * <asm/arch_hweight.h>
  112. */
  113. #include <asm/cpufeature.h>
  114. /*
  115. * Alternative instructions for different CPU types or capabilities.
  116. *
  117. * This allows to use optimized instructions even on generic binary
  118. * kernels.
  119. *
  120. * length of oldinstr must be longer or equal the length of newinstr
  121. * It can be padded with nops as needed.
  122. *
  123. * For non barrier like inlines please define new variants
  124. * without volatile and memory clobber.
  125. */
  126. #define alternative(oldinstr, newinstr, feature) \
  127. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  128. /*
  129. * Alternative inline assembly with input.
  130. *
  131. * Pecularities:
  132. * No memory clobber here.
  133. * Argument numbers start with 1.
  134. * Best is to use constraints that are fixed size (like (%1) ... "r")
  135. * If you use variable sized constraints like "m" or "g" in the
  136. * replacement make sure to pad to the worst case length.
  137. * Leaving an unused argument 0 to keep API compatibility.
  138. */
  139. #define alternative_input(oldinstr, newinstr, feature, input...) \
  140. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  141. : : "i" (0), ## input)
  142. /* Like alternative_input, but with a single output argument */
  143. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  144. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  145. : output : "i" (0), ## input)
  146. /* Like alternative_io, but for replacing a direct call with another one. */
  147. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  148. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  149. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  150. /*
  151. * Like alternative_call, but there are two features and respective functions.
  152. * If CPU has feature2, function2 is used.
  153. * Otherwise, if CPU has feature1, function1 is used.
  154. * Otherwise, old function is used.
  155. */
  156. #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
  157. output, input...) \
  158. asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
  159. "call %P[new2]", feature2) \
  160. : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
  161. [new2] "i" (newfunc2), ## input)
  162. /*
  163. * use this macro(s) if you need more than one output parameter
  164. * in alternative_io
  165. */
  166. #define ASM_OUTPUT2(a...) a
  167. /*
  168. * use this macro if you need clobbers but no inputs in
  169. * alternative_{input,io,call}()
  170. */
  171. #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
  172. struct paravirt_patch_site;
  173. #ifdef CONFIG_PARAVIRT
  174. void apply_paravirt(struct paravirt_patch_site *start,
  175. struct paravirt_patch_site *end);
  176. #else
  177. static inline void apply_paravirt(struct paravirt_patch_site *start,
  178. struct paravirt_patch_site *end)
  179. {}
  180. #define __parainstructions NULL
  181. #define __parainstructions_end NULL
  182. #endif
  183. extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  184. /*
  185. * Clear and restore the kernel write-protection flag on the local CPU.
  186. * Allows the kernel to edit read-only pages.
  187. * Side-effect: any interrupt handler running between save and restore will have
  188. * the ability to write to read-only pages.
  189. *
  190. * Warning:
  191. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  192. * no thread can be preempted in the instructions being modified (no iret to an
  193. * invalid instruction possible) or if the instructions are changed from a
  194. * consistent state to another consistent state atomically.
  195. * More care must be taken when modifying code in the SMP case because of
  196. * Intel's errata. text_poke_smp() takes care that errata, but still
  197. * doesn't support NMI/MCE handler code modifying.
  198. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  199. * inconsistent instruction while you patch.
  200. */
  201. struct text_poke_param {
  202. void *addr;
  203. const void *opcode;
  204. size_t len;
  205. };
  206. extern void *text_poke(void *addr, const void *opcode, size_t len);
  207. extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
  208. extern void text_poke_smp_batch(struct text_poke_param *params, int n);
  209. #endif /* _ASM_X86_ALTERNATIVE_H */