^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _ASM_X86_ALTERNATIVE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_X86_ALTERNATIVE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #ifndef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Alternative inline assembly for SMP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * The LOCK_PREFIX macro defined here replaces the LOCK and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * LOCK_PREFIX macros used everywhere in the source tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * SMP alternatives use the same data structures as the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * alternatives and the X86_FEATURE_UP flag to indicate the case of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * UP system running a SMP kernel. The existing apply_alternatives()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * works fine for patching a SMP kernel for UP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * The SMP alternative tables can be kept after boot and contain both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * UP and SMP versions of the instructions to allow switching back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * SMP at runtime, when hotplugging in a new CPU, which is especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * useful in virtualized environments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * The very common lock prefix is handled as special case in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * separate table which is a pure address list without replacement ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * and size information. That keeps the table sizes small.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define LOCK_PREFIX_HERE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ".pushsection .smp_locks,\"a\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ".balign 4\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ".long 671f - .\n" /* offset */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ".popsection\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) "671:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #else /* ! CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define LOCK_PREFIX_HERE ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define LOCK_PREFIX ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * objtool annotation to ignore the alternatives and only consider the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * instruction(s).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define ANNOTATE_IGNORE_ALTERNATIVE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "999:\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ".pushsection .discard.ignore_alts\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ".long 999b - .\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ".popsection\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct alt_instr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) s32 instr_offset; /* original instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) s32 repl_offset; /* offset to replacement instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u16 cpuid; /* cpuid bit set for replacement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u8 instrlen; /* length of original instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u8 replacementlen; /* length of new instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u8 padlen; /* length of build-time padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Debug flag that can be tested to see whether alternative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * instructions were patched in already:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) extern int alternatives_patched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) extern void alternative_instructions(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) extern void alternatives_smp_module_add(struct module *mod, char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) void *locks, void *locks_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void *text, void *text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) extern void alternatives_smp_module_del(struct module *mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) extern void alternatives_enable_smp(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) extern int alternatives_text_reserved(void *start, void *end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) extern bool skip_smp_alternatives;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline void alternatives_smp_module_add(struct module *mod, char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void *locks, void *locks_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void *text, void *text_end) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline void alternatives_smp_module_del(struct module *mod) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static inline void alternatives_enable_smp(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static inline int alternatives_text_reserved(void *start, void *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #endif /* CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define b_replacement(num) "664"#num
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define e_replacement(num) "665"#num
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define alt_end_marker "663"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define alt_slen "662b-661b"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define alt_pad_len alt_end_marker"b-662b"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define alt_total_slen alt_end_marker"b-661b"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define OLDINSTR(oldinstr, num) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) "# ALT: oldnstr\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) "661:\n\t" oldinstr "\n662:\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "# ALT: padding\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) alt_end_marker ":\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * gas compatible max based on the idea from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * The additional "-" is needed because gas uses a "true" value of -1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Pad the second replacement alternative with additional NOPs if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * additionally longer than the first replacement alternative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define OLDINSTR_2(oldinstr, num1, num2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "# ALT: oldinstr2\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "661:\n\t" oldinstr "\n662:\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) "# ALT: padding2\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) alt_end_marker ":\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define OLDINSTR_3(oldinsn, n1, n2, n3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "# ALT: oldinstr3\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) "661:\n\t" oldinsn "\n662:\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) "# ALT: padding3\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) " - (" alt_slen ")) > 0) * " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) " - (" alt_slen ")), 0x90\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) alt_end_marker ":\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define ALTINSTR_ENTRY(feature, num) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) " .long 661b - .\n" /* label */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) " .long " b_replacement(num)"f - .\n" /* new instruction */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) " .word " __stringify(feature) "\n" /* feature bit */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) " .byte " alt_total_slen "\n" /* source len */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) " .byte " alt_rlen(num) "\n" /* replacement len */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) " .byte " alt_pad_len "\n" /* pad len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) "# ALT: replacement " #num "\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* alternative assembly primitive: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #define ALTERNATIVE(oldinstr, newinstr, feature) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) OLDINSTR(oldinstr, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ".pushsection .altinstructions,\"a\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ALTINSTR_ENTRY(feature, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ".popsection\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ".pushsection .altinstr_replacement, \"ax\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ".popsection\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) OLDINSTR_2(oldinstr, 1, 2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ".pushsection .altinstructions,\"a\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ALTINSTR_ENTRY(feature1, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ALTINSTR_ENTRY(feature2, 2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ".popsection\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ".pushsection .altinstr_replacement, \"ax\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ".popsection\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) OLDINSTR_3(oldinsn, 1, 2, 3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ".pushsection .altinstructions,\"a\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ALTINSTR_ENTRY(feat1, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ALTINSTR_ENTRY(feat2, 2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ALTINSTR_ENTRY(feat3, 3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ".popsection\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ".pushsection .altinstr_replacement, \"ax\"\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ALTINSTR_REPLACEMENT(newinsn1, feat1, 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ALTINSTR_REPLACEMENT(newinsn2, feat2, 2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ALTINSTR_REPLACEMENT(newinsn3, feat3, 3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ".popsection\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Alternative instructions for different CPU types or capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * This allows to use optimized instructions even on generic binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * kernels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * length of oldinstr must be longer or equal the length of newinstr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * It can be padded with nops as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * For non barrier like inlines please define new variants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * without volatile and memory clobber.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #define alternative(oldinstr, newinstr, feature) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Alternative inline assembly with input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * Peculiarities:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * No memory clobber here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * Argument numbers start with 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * Best is to use constraints that are fixed size (like (%1) ... "r")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * If you use variable sized constraints like "m" or "g" in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * replacement make sure to pad to the worst case length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Leaving an unused argument 0 to keep API compatibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #define alternative_input(oldinstr, newinstr, feature, input...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) : : "i" (0), ## input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * This is similar to alternative_input. But it has two features and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * respective instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * If CPU has feature2, newinstr2 is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * Otherwise, if CPU has feature1, newinstr1 is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Otherwise, oldinstr is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) feature2, input...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) newinstr2, feature2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) : : "i" (0), ## input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* Like alternative_input, but with a single output argument */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #define alternative_io(oldinstr, newinstr, feature, output, input...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) : output : "i" (0), ## input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Like alternative_io, but for replacing a direct call with another one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) #define alternative_call(oldfunc, newfunc, feature, output, input...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Like alternative_call, but there are two features and respective functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * If CPU has feature2, function2 is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * Otherwise, if CPU has feature1, function1 is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Otherwise, old function is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) output, input...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) "call %P[new2]", feature2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) : output, ASM_CALL_CONSTRAINT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) [new2] "i" (newfunc2), ## input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * use this macro(s) if you need more than one output parameter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * in alternative_io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #define ASM_OUTPUT2(a...) a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * use this macro if you need clobbers but no inputs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * alternative_{input,io,call}()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #endif /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) #endif /* _ASM_X86_ALTERNATIVE_H */