alternative.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _X86_64_ALTERNATIVE_H
  2. #define _X86_64_ALTERNATIVE_H
  3. #ifdef __KERNEL__
  4. #include <linux/types.h>
  5. struct alt_instr {
  6. u8 *instr; /* original instruction */
  7. u8 *replacement;
  8. u8 cpuid; /* cpuid bit set for replacement */
  9. u8 instrlen; /* length of original instruction */
  10. u8 replacementlen; /* length of new instruction, <= instrlen */
  11. u8 pad[5];
  12. };
  13. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  14. struct module;
  15. #ifdef CONFIG_SMP
  16. extern void alternatives_smp_module_add(struct module *mod, char *name,
  17. void *locks, void *locks_end,
  18. void *text, void *text_end);
  19. extern void alternatives_smp_module_del(struct module *mod);
  20. extern void alternatives_smp_switch(int smp);
  21. #else
  22. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  23. void *locks, void *locks_end,
  24. void *text, void *text_end) {}
  25. static inline void alternatives_smp_module_del(struct module *mod) {}
  26. static inline void alternatives_smp_switch(int smp) {}
  27. #endif
  28. #endif
  29. /*
  30. * Alternative instructions for different CPU types or capabilities.
  31. *
  32. * This allows to use optimized instructions even on generic binary
  33. * kernels.
  34. *
  35. * length of oldinstr must be longer or equal the length of newinstr
  36. * It can be padded with nops as needed.
  37. *
  38. * For non barrier like inlines please define new variants
  39. * without volatile and memory clobber.
  40. */
  41. #define alternative(oldinstr, newinstr, feature) \
  42. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  43. ".section .altinstructions,\"a\"\n" \
  44. " .align 8\n" \
  45. " .quad 661b\n" /* label */ \
  46. " .quad 663f\n" /* new instruction */ \
  47. " .byte %c0\n" /* feature bit */ \
  48. " .byte 662b-661b\n" /* sourcelen */ \
  49. " .byte 664f-663f\n" /* replacementlen */ \
  50. ".previous\n" \
  51. ".section .altinstr_replacement,\"ax\"\n" \
  52. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  53. ".previous" :: "i" (feature) : "memory")
  54. /*
  55. * Alternative inline assembly with input.
  56. *
  57. * Pecularities:
  58. * No memory clobber here.
  59. * Argument numbers start with 1.
  60. * Best is to use constraints that are fixed size (like (%1) ... "r")
  61. * If you use variable sized constraints like "m" or "g" in the
  62. * replacement make sure to pad to the worst case length.
  63. */
  64. #define alternative_input(oldinstr, newinstr, feature, input...) \
  65. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  66. ".section .altinstructions,\"a\"\n" \
  67. " .align 8\n" \
  68. " .quad 661b\n" /* label */ \
  69. " .quad 663f\n" /* new instruction */ \
  70. " .byte %c0\n" /* feature bit */ \
  71. " .byte 662b-661b\n" /* sourcelen */ \
  72. " .byte 664f-663f\n" /* replacementlen */ \
  73. ".previous\n" \
  74. ".section .altinstr_replacement,\"ax\"\n" \
  75. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  76. ".previous" :: "i" (feature), ##input)
  77. /* Like alternative_input, but with a single output argument */
  78. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  79. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  80. ".section .altinstructions,\"a\"\n" \
  81. " .align 8\n" \
  82. " .quad 661b\n" /* label */ \
  83. " .quad 663f\n" /* new instruction */ \
  84. " .byte %c[feat]\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" : output : [feat] "i" (feature), ##input)
  91. /*
  92. * Alternative inline assembly for SMP.
  93. *
  94. * alternative_smp() takes two versions (SMP first, UP second) and is
  95. * for more complex stuff such as spinlocks.
  96. *
  97. * The LOCK_PREFIX macro defined here replaces the LOCK and
  98. * LOCK_PREFIX macros used everywhere in the source tree.
  99. *
  100. * SMP alternatives use the same data structures as the other
  101. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  102. * UP system running a SMP kernel. The existing apply_alternatives()
  103. * works fine for patching a SMP kernel for UP.
  104. *
  105. * The SMP alternative tables can be kept after boot and contain both
  106. * UP and SMP versions of the instructions to allow switching back to
  107. * SMP at runtime, when hotplugging in a new CPU, which is especially
  108. * useful in virtualized environments.
  109. *
  110. * The very common lock prefix is handled as special case in a
  111. * separate table which is a pure address list without replacement ptr
  112. * and size information. That keeps the table sizes small.
  113. */
  114. #ifdef CONFIG_SMP
  115. #define alternative_smp(smpinstr, upinstr, args...) \
  116. asm volatile ("661:\n\t" smpinstr "\n662:\n" \
  117. ".section .smp_altinstructions,\"a\"\n" \
  118. " .align 8\n" \
  119. " .quad 661b\n" /* label */ \
  120. " .quad 663f\n" /* new instruction */ \
  121. " .byte 0x66\n" /* X86_FEATURE_UP */ \
  122. " .byte 662b-661b\n" /* sourcelen */ \
  123. " .byte 664f-663f\n" /* replacementlen */ \
  124. ".previous\n" \
  125. ".section .smp_altinstr_replacement,\"awx\"\n" \
  126. "663:\n\t" upinstr "\n" /* replacement */ \
  127. "664:\n\t.fill 662b-661b,1,0x42\n" /* space for original */ \
  128. ".previous" : args)
  129. #define LOCK_PREFIX \
  130. ".section .smp_locks,\"a\"\n" \
  131. " .align 8\n" \
  132. " .quad 661f\n" /* address */ \
  133. ".previous\n" \
  134. "661:\n\tlock; "
  135. #else /* ! CONFIG_SMP */
  136. #define alternative_smp(smpinstr, upinstr, args...) \
  137. asm volatile (upinstr : args)
  138. #define LOCK_PREFIX ""
  139. #endif
  140. #endif /* _X86_64_ALTERNATIVE_H */