alternative.h 4.7 KB

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