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 <asm/asm.h>
  6. /*
  7. * Alternative inline assembly for SMP.
  8. *
  9. * The LOCK_PREFIX macro defined here replaces the LOCK and
  10. * LOCK_PREFIX macros used everywhere in the source tree.
  11. *
  12. * SMP alternatives use the same data structures as the other
  13. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  14. * UP system running a SMP kernel. The existing apply_alternatives()
  15. * works fine for patching a SMP kernel for UP.
  16. *
  17. * The SMP alternative tables can be kept after boot and contain both
  18. * UP and SMP versions of the instructions to allow switching back to
  19. * SMP at runtime, when hotplugging in a new CPU, which is especially
  20. * useful in virtualized environments.
  21. *
  22. * The very common lock prefix is handled as special case in a
  23. * separate table which is a pure address list without replacement ptr
  24. * and size information. That keeps the table sizes small.
  25. */
  26. #ifdef CONFIG_SMP
  27. #define LOCK_PREFIX \
  28. ".section .smp_locks,\"a\"\n" \
  29. _ASM_ALIGN "\n" \
  30. _ASM_PTR "661f\n" /* address */ \
  31. ".previous\n" \
  32. "661:\n\tlock; "
  33. #else /* ! CONFIG_SMP */
  34. #define LOCK_PREFIX ""
  35. #endif
  36. /* This must be included *after* the definition of LOCK_PREFIX */
  37. #include <asm/cpufeature.h>
  38. struct alt_instr {
  39. u8 *instr; /* original instruction */
  40. u8 *replacement;
  41. u8 cpuid; /* cpuid bit set for replacement */
  42. u8 instrlen; /* length of original instruction */
  43. u8 replacementlen; /* length of new instruction, <= instrlen */
  44. u8 pad1;
  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. #else
  59. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  60. void *locks, void *locks_end,
  61. void *text, void *text_end) {}
  62. static inline void alternatives_smp_module_del(struct module *mod) {}
  63. static inline void alternatives_smp_switch(int smp) {}
  64. #endif /* CONFIG_SMP */
  65. const unsigned char *const *find_nop_table(void);
  66. /*
  67. * Alternative instructions for different CPU types or capabilities.
  68. *
  69. * This allows to use optimized instructions even on generic binary
  70. * kernels.
  71. *
  72. * length of oldinstr must be longer or equal the length of newinstr
  73. * It can be padded with nops as needed.
  74. *
  75. * For non barrier like inlines please define new variants
  76. * without volatile and memory clobber.
  77. */
  78. #define alternative(oldinstr, newinstr, feature) \
  79. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  80. ".section .altinstructions,\"a\"\n" \
  81. _ASM_ALIGN "\n" \
  82. _ASM_PTR "661b\n" /* label */ \
  83. _ASM_PTR "663f\n" /* new instruction */ \
  84. " .byte %c0\n" /* feature bit */ \
  85. " .byte 662b-661b\n" /* sourcelen */ \
  86. " .byte 664f-663f\n" /* replacementlen */ \
  87. ".previous\n" \
  88. ".section .altinstr_replacement,\"ax\"\n" \
  89. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  90. ".previous" :: "i" (feature) : "memory")
  91. /*
  92. * Alternative inline assembly with input.
  93. *
  94. * Pecularities:
  95. * No memory clobber here.
  96. * Argument numbers start with 1.
  97. * Best is to use constraints that are fixed size (like (%1) ... "r")
  98. * If you use variable sized constraints like "m" or "g" in the
  99. * replacement make sure to pad to the worst case length.
  100. */
  101. #define alternative_input(oldinstr, newinstr, feature, input...) \
  102. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  103. ".section .altinstructions,\"a\"\n" \
  104. _ASM_ALIGN "\n" \
  105. _ASM_PTR "661b\n" /* label */ \
  106. _ASM_PTR "663f\n" /* new instruction */ \
  107. " .byte %c0\n" /* feature bit */ \
  108. " .byte 662b-661b\n" /* sourcelen */ \
  109. " .byte 664f-663f\n" /* replacementlen */ \
  110. ".previous\n" \
  111. ".section .altinstr_replacement,\"ax\"\n" \
  112. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  113. ".previous" :: "i" (feature), ##input)
  114. /* Like alternative_input, but with a single output argument */
  115. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  116. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  117. ".section .altinstructions,\"a\"\n" \
  118. _ASM_ALIGN "\n" \
  119. _ASM_PTR "661b\n" /* label */ \
  120. _ASM_PTR "663f\n" /* new instruction */ \
  121. " .byte %c[feat]\n" /* feature bit */ \
  122. " .byte 662b-661b\n" /* sourcelen */ \
  123. " .byte 664f-663f\n" /* replacementlen */ \
  124. ".previous\n" \
  125. ".section .altinstr_replacement,\"ax\"\n" \
  126. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  127. ".previous" : output : [feat] "i" (feature), ##input)
  128. /*
  129. * use this macro(s) if you need more than one output parameter
  130. * in alternative_io
  131. */
  132. #define ASM_OUTPUT2(a, b) a, b
  133. struct paravirt_patch_site;
  134. #ifdef CONFIG_PARAVIRT
  135. void apply_paravirt(struct paravirt_patch_site *start,
  136. struct paravirt_patch_site *end);
  137. #else
  138. static inline void apply_paravirt(struct paravirt_patch_site *start,
  139. struct paravirt_patch_site *end)
  140. {}
  141. #define __parainstructions NULL
  142. #define __parainstructions_end NULL
  143. #endif
  144. extern void add_nops(void *insns, unsigned int len);
  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.
  158. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  159. * inconsistent instruction while you patch.
  160. * The _early version expects the memory to already be RW.
  161. */
  162. extern void *text_poke(void *addr, const void *opcode, size_t len);
  163. extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  164. #endif /* _ASM_X86_ALTERNATIVE_H */