alternative.h 4.4 KB

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