18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_ALTERNATIVE_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_ALTERNATIVE_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/types.h>
68c2ecf20Sopenharmony_ci#include <linux/stringify.h>
78c2ecf20Sopenharmony_ci#include <asm/asm.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define ALTINSTR_FLAG_INV	(1 << 15)
108c2ecf20Sopenharmony_ci#define ALT_NOT(feat)		((feat) | ALTINSTR_FLAG_INV)
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/stddef.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * Alternative inline assembly for SMP.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * The LOCK_PREFIX macro defined here replaces the LOCK and
208c2ecf20Sopenharmony_ci * LOCK_PREFIX macros used everywhere in the source tree.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * SMP alternatives use the same data structures as the other
238c2ecf20Sopenharmony_ci * alternatives and the X86_FEATURE_UP flag to indicate the case of a
248c2ecf20Sopenharmony_ci * UP system running a SMP kernel.  The existing apply_alternatives()
258c2ecf20Sopenharmony_ci * works fine for patching a SMP kernel for UP.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * The SMP alternative tables can be kept after boot and contain both
288c2ecf20Sopenharmony_ci * UP and SMP versions of the instructions to allow switching back to
298c2ecf20Sopenharmony_ci * SMP at runtime, when hotplugging in a new CPU, which is especially
308c2ecf20Sopenharmony_ci * useful in virtualized environments.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * The very common lock prefix is handled as special case in a
338c2ecf20Sopenharmony_ci * separate table which is a pure address list without replacement ptr
348c2ecf20Sopenharmony_ci * and size information.  That keeps the table sizes small.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
388c2ecf20Sopenharmony_ci#define LOCK_PREFIX_HERE \
398c2ecf20Sopenharmony_ci		".pushsection .smp_locks,\"a\"\n"	\
408c2ecf20Sopenharmony_ci		".balign 4\n"				\
418c2ecf20Sopenharmony_ci		".long 671f - .\n" /* offset */		\
428c2ecf20Sopenharmony_ci		".popsection\n"				\
438c2ecf20Sopenharmony_ci		"671:"
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#else /* ! CONFIG_SMP */
488c2ecf20Sopenharmony_ci#define LOCK_PREFIX_HERE ""
498c2ecf20Sopenharmony_ci#define LOCK_PREFIX ""
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * objtool annotation to ignore the alternatives and only consider the original
548c2ecf20Sopenharmony_ci * instruction(s).
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci#define ANNOTATE_IGNORE_ALTERNATIVE				\
578c2ecf20Sopenharmony_ci	"999:\n\t"						\
588c2ecf20Sopenharmony_ci	".pushsection .discard.ignore_alts\n\t"			\
598c2ecf20Sopenharmony_ci	".long 999b - .\n\t"					\
608c2ecf20Sopenharmony_ci	".popsection\n\t"
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct alt_instr {
638c2ecf20Sopenharmony_ci	s32 instr_offset;	/* original instruction */
648c2ecf20Sopenharmony_ci	s32 repl_offset;	/* offset to replacement instruction */
658c2ecf20Sopenharmony_ci	u16 cpuid;		/* cpuid bit set for replacement */
668c2ecf20Sopenharmony_ci	u8  instrlen;		/* length of original instruction */
678c2ecf20Sopenharmony_ci	u8  replacementlen;	/* length of new instruction */
688c2ecf20Sopenharmony_ci} __packed;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Debug flag that can be tested to see whether alternative
728c2ecf20Sopenharmony_ci * instructions were patched in already:
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ciextern int alternatives_patched;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciextern void alternative_instructions(void);
778c2ecf20Sopenharmony_ciextern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
788c2ecf20Sopenharmony_ciextern void apply_retpolines(s32 *start, s32 *end);
798c2ecf20Sopenharmony_ciextern void apply_returns(s32 *start, s32 *end);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct module;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
848c2ecf20Sopenharmony_ciextern void alternatives_smp_module_add(struct module *mod, char *name,
858c2ecf20Sopenharmony_ci					void *locks, void *locks_end,
868c2ecf20Sopenharmony_ci					void *text, void *text_end);
878c2ecf20Sopenharmony_ciextern void alternatives_smp_module_del(struct module *mod);
888c2ecf20Sopenharmony_ciextern void alternatives_enable_smp(void);
898c2ecf20Sopenharmony_ciextern int alternatives_text_reserved(void *start, void *end);
908c2ecf20Sopenharmony_ciextern bool skip_smp_alternatives;
918c2ecf20Sopenharmony_ci#else
928c2ecf20Sopenharmony_cistatic inline void alternatives_smp_module_add(struct module *mod, char *name,
938c2ecf20Sopenharmony_ci					       void *locks, void *locks_end,
948c2ecf20Sopenharmony_ci					       void *text, void *text_end) {}
958c2ecf20Sopenharmony_cistatic inline void alternatives_smp_module_del(struct module *mod) {}
968c2ecf20Sopenharmony_cistatic inline void alternatives_enable_smp(void) {}
978c2ecf20Sopenharmony_cistatic inline int alternatives_text_reserved(void *start, void *end)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci#endif	/* CONFIG_SMP */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define b_replacement(num)	"664"#num
1048c2ecf20Sopenharmony_ci#define e_replacement(num)	"665"#num
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define alt_end_marker		"663"
1078c2ecf20Sopenharmony_ci#define alt_slen		"662b-661b"
1088c2ecf20Sopenharmony_ci#define alt_total_slen		alt_end_marker"b-661b"
1098c2ecf20Sopenharmony_ci#define alt_rlen(num)		e_replacement(num)"f-"b_replacement(num)"f"
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define OLDINSTR(oldinstr, num)						\
1128c2ecf20Sopenharmony_ci	"# ALT: oldnstr\n"						\
1138c2ecf20Sopenharmony_ci	"661:\n\t" oldinstr "\n662:\n"					\
1148c2ecf20Sopenharmony_ci	"# ALT: padding\n"						\
1158c2ecf20Sopenharmony_ci	".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "		\
1168c2ecf20Sopenharmony_ci		"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"		\
1178c2ecf20Sopenharmony_ci	alt_end_marker ":\n"
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * gas compatible max based on the idea from:
1218c2ecf20Sopenharmony_ci * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
1228c2ecf20Sopenharmony_ci *
1238c2ecf20Sopenharmony_ci * The additional "-" is needed because gas uses a "true" value of -1.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ci#define alt_max_short(a, b)	"((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Pad the second replacement alternative with additional NOPs if it is
1298c2ecf20Sopenharmony_ci * additionally longer than the first replacement alternative.
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_ci#define OLDINSTR_2(oldinstr, num1, num2) \
1328c2ecf20Sopenharmony_ci	"# ALT: oldinstr2\n"									\
1338c2ecf20Sopenharmony_ci	"661:\n\t" oldinstr "\n662:\n"								\
1348c2ecf20Sopenharmony_ci	"# ALT: padding2\n"									\
1358c2ecf20Sopenharmony_ci	".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "	\
1368c2ecf20Sopenharmony_ci		"(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"	\
1378c2ecf20Sopenharmony_ci	alt_end_marker ":\n"
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define OLDINSTR_3(oldinsn, n1, n2, n3)								\
1408c2ecf20Sopenharmony_ci	"# ALT: oldinstr3\n"									\
1418c2ecf20Sopenharmony_ci	"661:\n\t" oldinsn "\n662:\n"								\
1428c2ecf20Sopenharmony_ci	"# ALT: padding3\n"									\
1438c2ecf20Sopenharmony_ci	".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
1448c2ecf20Sopenharmony_ci		" - (" alt_slen ")) > 0) * "							\
1458c2ecf20Sopenharmony_ci		"(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
1468c2ecf20Sopenharmony_ci		" - (" alt_slen ")), 0x90\n"							\
1478c2ecf20Sopenharmony_ci	alt_end_marker ":\n"
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define ALTINSTR_ENTRY(feature, num)					      \
1508c2ecf20Sopenharmony_ci	" .long 661b - .\n"				/* label           */ \
1518c2ecf20Sopenharmony_ci	" .long " b_replacement(num)"f - .\n"		/* new instruction */ \
1528c2ecf20Sopenharmony_ci	" .word " __stringify(feature) "\n"		/* feature bit     */ \
1538c2ecf20Sopenharmony_ci	" .byte " alt_total_slen "\n"			/* source len      */ \
1548c2ecf20Sopenharmony_ci	" .byte " alt_rlen(num) "\n"			/* replacement len */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define ALTINSTR_REPLACEMENT(newinstr, feature, num)	/* replacement */	\
1578c2ecf20Sopenharmony_ci	"# ALT: replacement " #num "\n"						\
1588c2ecf20Sopenharmony_ci	b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/* alternative assembly primitive: */
1618c2ecf20Sopenharmony_ci#define ALTERNATIVE(oldinstr, newinstr, feature)			\
1628c2ecf20Sopenharmony_ci	OLDINSTR(oldinstr, 1)						\
1638c2ecf20Sopenharmony_ci	".pushsection .altinstructions,\"a\"\n"				\
1648c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feature, 1)					\
1658c2ecf20Sopenharmony_ci	".popsection\n"							\
1668c2ecf20Sopenharmony_ci	".pushsection .altinstr_replacement, \"ax\"\n"			\
1678c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinstr, feature, 1)			\
1688c2ecf20Sopenharmony_ci	".popsection\n"
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
1718c2ecf20Sopenharmony_ci	OLDINSTR_2(oldinstr, 1, 2)					\
1728c2ecf20Sopenharmony_ci	".pushsection .altinstructions,\"a\"\n"				\
1738c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feature1, 1)					\
1748c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feature2, 2)					\
1758c2ecf20Sopenharmony_ci	".popsection\n"							\
1768c2ecf20Sopenharmony_ci	".pushsection .altinstr_replacement, \"ax\"\n"			\
1778c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinstr1, feature1, 1)			\
1788c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinstr2, feature2, 2)			\
1798c2ecf20Sopenharmony_ci	".popsection\n"
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
1828c2ecf20Sopenharmony_ci#define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
1838c2ecf20Sopenharmony_ci	ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
1848c2ecf20Sopenharmony_ci		      newinstr_yes, feature)
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
1878c2ecf20Sopenharmony_ci	OLDINSTR_3(oldinsn, 1, 2, 3)						\
1888c2ecf20Sopenharmony_ci	".pushsection .altinstructions,\"a\"\n"					\
1898c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feat1, 1)						\
1908c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feat2, 2)						\
1918c2ecf20Sopenharmony_ci	ALTINSTR_ENTRY(feat3, 3)						\
1928c2ecf20Sopenharmony_ci	".popsection\n"								\
1938c2ecf20Sopenharmony_ci	".pushsection .altinstr_replacement, \"ax\"\n"				\
1948c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinsn1, feat1, 1)				\
1958c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinsn2, feat2, 2)				\
1968c2ecf20Sopenharmony_ci	ALTINSTR_REPLACEMENT(newinsn3, feat3, 3)				\
1978c2ecf20Sopenharmony_ci	".popsection\n"
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/*
2008c2ecf20Sopenharmony_ci * Alternative instructions for different CPU types or capabilities.
2018c2ecf20Sopenharmony_ci *
2028c2ecf20Sopenharmony_ci * This allows to use optimized instructions even on generic binary
2038c2ecf20Sopenharmony_ci * kernels.
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * length of oldinstr must be longer or equal the length of newinstr
2068c2ecf20Sopenharmony_ci * It can be padded with nops as needed.
2078c2ecf20Sopenharmony_ci *
2088c2ecf20Sopenharmony_ci * For non barrier like inlines please define new variants
2098c2ecf20Sopenharmony_ci * without volatile and memory clobber.
2108c2ecf20Sopenharmony_ci */
2118c2ecf20Sopenharmony_ci#define alternative(oldinstr, newinstr, feature)			\
2128c2ecf20Sopenharmony_ci	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
2158c2ecf20Sopenharmony_ci	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci#define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
2188c2ecf20Sopenharmony_ci	asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*
2218c2ecf20Sopenharmony_ci * Alternative inline assembly with input.
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci * Peculiarities:
2248c2ecf20Sopenharmony_ci * No memory clobber here.
2258c2ecf20Sopenharmony_ci * Argument numbers start with 1.
2268c2ecf20Sopenharmony_ci * Leaving an unused argument 0 to keep API compatibility.
2278c2ecf20Sopenharmony_ci */
2288c2ecf20Sopenharmony_ci#define alternative_input(oldinstr, newinstr, feature, input...)	\
2298c2ecf20Sopenharmony_ci	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
2308c2ecf20Sopenharmony_ci		: : "i" (0), ## input)
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/*
2338c2ecf20Sopenharmony_ci * This is similar to alternative_input. But it has two features and
2348c2ecf20Sopenharmony_ci * respective instructions.
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * If CPU has feature2, newinstr2 is used.
2378c2ecf20Sopenharmony_ci * Otherwise, if CPU has feature1, newinstr1 is used.
2388c2ecf20Sopenharmony_ci * Otherwise, oldinstr is used.
2398c2ecf20Sopenharmony_ci */
2408c2ecf20Sopenharmony_ci#define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2,	     \
2418c2ecf20Sopenharmony_ci			   feature2, input...)				     \
2428c2ecf20Sopenharmony_ci	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1,     \
2438c2ecf20Sopenharmony_ci		newinstr2, feature2)					     \
2448c2ecf20Sopenharmony_ci		: : "i" (0), ## input)
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* Like alternative_input, but with a single output argument */
2478c2ecf20Sopenharmony_ci#define alternative_io(oldinstr, newinstr, feature, output, input...)	\
2488c2ecf20Sopenharmony_ci	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
2498c2ecf20Sopenharmony_ci		: output : "i" (0), ## input)
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/* Like alternative_io, but for replacing a direct call with another one. */
2528c2ecf20Sopenharmony_ci#define alternative_call(oldfunc, newfunc, feature, output, input...)	\
2538c2ecf20Sopenharmony_ci	asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
2548c2ecf20Sopenharmony_ci		: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/*
2578c2ecf20Sopenharmony_ci * Like alternative_call, but there are two features and respective functions.
2588c2ecf20Sopenharmony_ci * If CPU has feature2, function2 is used.
2598c2ecf20Sopenharmony_ci * Otherwise, if CPU has feature1, function1 is used.
2608c2ecf20Sopenharmony_ci * Otherwise, old function is used.
2618c2ecf20Sopenharmony_ci */
2628c2ecf20Sopenharmony_ci#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2,   \
2638c2ecf20Sopenharmony_ci			   output, input...)				      \
2648c2ecf20Sopenharmony_ci	asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
2658c2ecf20Sopenharmony_ci		"call %P[new2]", feature2)				      \
2668c2ecf20Sopenharmony_ci		: output, ASM_CALL_CONSTRAINT				      \
2678c2ecf20Sopenharmony_ci		: [old] "i" (oldfunc), [new1] "i" (newfunc1),		      \
2688c2ecf20Sopenharmony_ci		  [new2] "i" (newfunc2), ## input)
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/*
2718c2ecf20Sopenharmony_ci * use this macro(s) if you need more than one output parameter
2728c2ecf20Sopenharmony_ci * in alternative_io
2738c2ecf20Sopenharmony_ci */
2748c2ecf20Sopenharmony_ci#define ASM_OUTPUT2(a...) a
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci/*
2778c2ecf20Sopenharmony_ci * use this macro if you need clobbers but no inputs in
2788c2ecf20Sopenharmony_ci * alternative_{input,io,call}()
2798c2ecf20Sopenharmony_ci */
2808c2ecf20Sopenharmony_ci#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci#else /* __ASSEMBLY__ */
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
2858c2ecf20Sopenharmony_ci	.macro LOCK_PREFIX
2868c2ecf20Sopenharmony_ci672:	lock
2878c2ecf20Sopenharmony_ci	.pushsection .smp_locks,"a"
2888c2ecf20Sopenharmony_ci	.balign 4
2898c2ecf20Sopenharmony_ci	.long 672b - .
2908c2ecf20Sopenharmony_ci	.popsection
2918c2ecf20Sopenharmony_ci	.endm
2928c2ecf20Sopenharmony_ci#else
2938c2ecf20Sopenharmony_ci	.macro LOCK_PREFIX
2948c2ecf20Sopenharmony_ci	.endm
2958c2ecf20Sopenharmony_ci#endif
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/*
2988c2ecf20Sopenharmony_ci * objtool annotation to ignore the alternatives and only consider the original
2998c2ecf20Sopenharmony_ci * instruction(s).
3008c2ecf20Sopenharmony_ci */
3018c2ecf20Sopenharmony_ci.macro ANNOTATE_IGNORE_ALTERNATIVE
3028c2ecf20Sopenharmony_ci	.Lannotate_\@:
3038c2ecf20Sopenharmony_ci	.pushsection .discard.ignore_alts
3048c2ecf20Sopenharmony_ci	.long .Lannotate_\@ - .
3058c2ecf20Sopenharmony_ci	.popsection
3068c2ecf20Sopenharmony_ci.endm
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/*
3098c2ecf20Sopenharmony_ci * Issue one struct alt_instr descriptor entry (need to put it into
3108c2ecf20Sopenharmony_ci * the section .altinstructions, see below). This entry contains
3118c2ecf20Sopenharmony_ci * enough information for the alternatives patching code to patch an
3128c2ecf20Sopenharmony_ci * instruction. See apply_alternatives().
3138c2ecf20Sopenharmony_ci */
3148c2ecf20Sopenharmony_ci.macro altinstruction_entry orig alt feature orig_len alt_len
3158c2ecf20Sopenharmony_ci	.long \orig - .
3168c2ecf20Sopenharmony_ci	.long \alt - .
3178c2ecf20Sopenharmony_ci	.word \feature
3188c2ecf20Sopenharmony_ci	.byte \orig_len
3198c2ecf20Sopenharmony_ci	.byte \alt_len
3208c2ecf20Sopenharmony_ci.endm
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci/*
3238c2ecf20Sopenharmony_ci * Define an alternative between two instructions. If @feature is
3248c2ecf20Sopenharmony_ci * present, early code in apply_alternatives() replaces @oldinstr with
3258c2ecf20Sopenharmony_ci * @newinstr. ".skip" directive takes care of proper instruction padding
3268c2ecf20Sopenharmony_ci * in case @newinstr is longer than @oldinstr.
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_ci.macro ALTERNATIVE oldinstr, newinstr, feature
3298c2ecf20Sopenharmony_ci140:
3308c2ecf20Sopenharmony_ci	\oldinstr
3318c2ecf20Sopenharmony_ci141:
3328c2ecf20Sopenharmony_ci	.skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
3338c2ecf20Sopenharmony_ci142:
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	.pushsection .altinstructions,"a"
3368c2ecf20Sopenharmony_ci	altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
3378c2ecf20Sopenharmony_ci	.popsection
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	.pushsection .altinstr_replacement,"ax"
3408c2ecf20Sopenharmony_ci143:
3418c2ecf20Sopenharmony_ci	\newinstr
3428c2ecf20Sopenharmony_ci144:
3438c2ecf20Sopenharmony_ci	.popsection
3448c2ecf20Sopenharmony_ci.endm
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci#define old_len			141b-140b
3478c2ecf20Sopenharmony_ci#define new_len1		144f-143f
3488c2ecf20Sopenharmony_ci#define new_len2		145f-144f
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci/*
3518c2ecf20Sopenharmony_ci * gas compatible max based on the idea from:
3528c2ecf20Sopenharmony_ci * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
3538c2ecf20Sopenharmony_ci *
3548c2ecf20Sopenharmony_ci * The additional "-" is needed because gas uses a "true" value of -1.
3558c2ecf20Sopenharmony_ci */
3568c2ecf20Sopenharmony_ci#define alt_max_short(a, b)	((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/*
3608c2ecf20Sopenharmony_ci * Same as ALTERNATIVE macro above but for two alternatives. If CPU
3618c2ecf20Sopenharmony_ci * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
3628c2ecf20Sopenharmony_ci * @feature2, it replaces @oldinstr with @feature2.
3638c2ecf20Sopenharmony_ci */
3648c2ecf20Sopenharmony_ci.macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
3658c2ecf20Sopenharmony_ci140:
3668c2ecf20Sopenharmony_ci	\oldinstr
3678c2ecf20Sopenharmony_ci141:
3688c2ecf20Sopenharmony_ci	.skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
3698c2ecf20Sopenharmony_ci		(alt_max_short(new_len1, new_len2) - (old_len)),0x90
3708c2ecf20Sopenharmony_ci142:
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	.pushsection .altinstructions,"a"
3738c2ecf20Sopenharmony_ci	altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
3748c2ecf20Sopenharmony_ci	altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
3758c2ecf20Sopenharmony_ci	.popsection
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	.pushsection .altinstr_replacement,"ax"
3788c2ecf20Sopenharmony_ci143:
3798c2ecf20Sopenharmony_ci	\newinstr1
3808c2ecf20Sopenharmony_ci144:
3818c2ecf20Sopenharmony_ci	\newinstr2
3828c2ecf20Sopenharmony_ci145:
3838c2ecf20Sopenharmony_ci	.popsection
3848c2ecf20Sopenharmony_ci.endm
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
3878c2ecf20Sopenharmony_ci#define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
3888c2ecf20Sopenharmony_ci	ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
3898c2ecf20Sopenharmony_ci	newinstr_yes, feature
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci#endif /* _ASM_X86_ALTERNATIVE_H */
394