alternative.h 6.4 KB

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