alternative.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /* alternative assembly primitive: */
  69. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  70. \
  71. "661:\n\t" oldinstr "\n662:\n" \
  72. ".section .altinstructions,\"a\"\n" \
  73. " .long 661b - .\n" /* label */ \
  74. " .long 663f - .\n" /* new instruction */ \
  75. " .word " __stringify(feature) "\n" /* feature bit */ \
  76. " .byte 662b-661b\n" /* sourcelen */ \
  77. " .byte 664f-663f\n" /* replacementlen */ \
  78. ".previous\n" \
  79. ".section .discard,\"aw\",@progbits\n" \
  80. " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
  81. ".previous\n" \
  82. ".section .altinstr_replacement, \"ax\"\n" \
  83. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  84. ".previous"
  85. /*
  86. * This must be included *after* the definition of ALTERNATIVE due to
  87. * <asm/arch_hweight.h>
  88. */
  89. #include <asm/cpufeature.h>
  90. /*
  91. * Alternative instructions for different CPU types or capabilities.
  92. *
  93. * This allows to use optimized instructions even on generic binary
  94. * kernels.
  95. *
  96. * length of oldinstr must be longer or equal the length of newinstr
  97. * It can be padded with nops as needed.
  98. *
  99. * For non barrier like inlines please define new variants
  100. * without volatile and memory clobber.
  101. */
  102. #define alternative(oldinstr, newinstr, feature) \
  103. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  104. /*
  105. * Alternative inline assembly with input.
  106. *
  107. * Pecularities:
  108. * No memory clobber here.
  109. * Argument numbers start with 1.
  110. * Best is to use constraints that are fixed size (like (%1) ... "r")
  111. * If you use variable sized constraints like "m" or "g" in the
  112. * replacement make sure to pad to the worst case length.
  113. * Leaving an unused argument 0 to keep API compatibility.
  114. */
  115. #define alternative_input(oldinstr, newinstr, feature, input...) \
  116. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  117. : : "i" (0), ## input)
  118. /* Like alternative_input, but with a single output argument */
  119. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  120. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  121. : output : "i" (0), ## input)
  122. /* Like alternative_io, but for replacing a direct call with another one. */
  123. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  124. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  125. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  126. /*
  127. * use this macro(s) if you need more than one output parameter
  128. * in alternative_io
  129. */
  130. #define ASM_OUTPUT2(a...) a
  131. /*
  132. * use this macro if you need clobbers but no inputs in
  133. * alternative_{input,io,call}()
  134. */
  135. #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
  136. struct paravirt_patch_site;
  137. #ifdef CONFIG_PARAVIRT
  138. void apply_paravirt(struct paravirt_patch_site *start,
  139. struct paravirt_patch_site *end);
  140. #else
  141. static inline void apply_paravirt(struct paravirt_patch_site *start,
  142. struct paravirt_patch_site *end)
  143. {}
  144. #define __parainstructions NULL
  145. #define __parainstructions_end NULL
  146. #endif
  147. extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  148. /*
  149. * Clear and restore the kernel write-protection flag on the local CPU.
  150. * Allows the kernel to edit read-only pages.
  151. * Side-effect: any interrupt handler running between save and restore will have
  152. * the ability to write to read-only pages.
  153. *
  154. * Warning:
  155. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  156. * no thread can be preempted in the instructions being modified (no iret to an
  157. * invalid instruction possible) or if the instructions are changed from a
  158. * consistent state to another consistent state atomically.
  159. * More care must be taken when modifying code in the SMP case because of
  160. * Intel's errata. text_poke_smp() takes care that errata, but still
  161. * doesn't support NMI/MCE handler code modifying.
  162. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  163. * inconsistent instruction while you patch.
  164. */
  165. struct text_poke_param {
  166. void *addr;
  167. const void *opcode;
  168. size_t len;
  169. };
  170. extern void *text_poke(void *addr, const void *opcode, size_t len);
  171. extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
  172. extern void text_poke_smp_batch(struct text_poke_param *params, int n);
  173. #endif /* _ASM_X86_ALTERNATIVE_H */