alternative.h 6.3 KB

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