1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_ALTERNATIVE_H
3 #define _ASM_S390_ALTERNATIVE_H
7 #include <linux/types.h>
8 #include <linux/stddef.h>
9 #include <linux/stringify.h>
12 s32 instr_offset; /* original instruction */
13 s32 repl_offset; /* offset to replacement instruction */
14 u16 facility; /* facility bit set for replacement */
15 u8 instrlen; /* length of original instruction */
16 u8 replacementlen; /* length of new instruction */
19 void apply_alternative_instructions(void);
20 void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
23 * |661: |662: |6620 |663:
24 * +-----------+---------------------+
25 * | oldinstr | oldinstr_padding |
26 * | +----------+----------+
28 * | | >6 bytes |6/4/2 nops|
29 * | |6 bytes jg----------->
30 * +-----------+---------------------+
31 * ^^ static padding ^^
33 * .altinstr_replacement section
34 * +---------------------+-----------+
36 * | alternative instr 1 |
37 * +-----------+---------+- - - - - -+
39 * | alternative instr 2 | padding
40 * +---------------------+- - - - - -+
43 * .altinstructions section
44 * +---------------------------------+
45 * | alt_instr entries for each |
46 * | alternative instr |
47 * +---------------------------------+
50 #define b_altinstr(num) "664"#num
51 #define e_altinstr(num) "665"#num
53 #define e_oldinstr_pad_end "663"
54 #define oldinstr_len "662b-661b"
55 #define oldinstr_total_len e_oldinstr_pad_end"b-661b"
56 #define altinstr_len(num) e_altinstr(num)"b-"b_altinstr(num)"b"
57 #define oldinstr_pad_len(num) \
58 "-(((" altinstr_len(num) ")-(" oldinstr_len ")) > 0) * " \
59 "((" altinstr_len(num) ")-(" oldinstr_len "))"
61 #define INSTR_LEN_SANITY_CHECK(len) \
62 ".if " len " > 254\n" \
63 "\t.error \"cpu alternatives does not support instructions " \
64 "blocks > 254 bytes\"\n" \
66 ".if (" len ") %% 2\n" \
67 "\t.error \"cpu alternatives instructions length is odd\"\n" \
70 #define OLDINSTR_PADDING(oldinstr, num) \
71 ".if " oldinstr_pad_len(num) " > 6\n" \
72 "\tjg " e_oldinstr_pad_end "f\n" \
74 "\t.rept (" oldinstr_pad_len(num) " - (6620b-662b)) / 2\n" \
77 "\t.rept " oldinstr_pad_len(num) " / 6\n" \
80 "\t.rept " oldinstr_pad_len(num) " %% 6 / 4\n" \
83 "\t.rept " oldinstr_pad_len(num) " %% 6 %% 4 / 2\n" \
88 #define OLDINSTR(oldinstr, num) \
89 "661:\n\t" oldinstr "\n662:\n" \
90 OLDINSTR_PADDING(oldinstr, num) \
91 e_oldinstr_pad_end ":\n" \
92 INSTR_LEN_SANITY_CHECK(oldinstr_len)
94 #define OLDINSTR_2(oldinstr, num1, num2) \
95 "661:\n\t" oldinstr "\n662:\n" \
96 ".if " altinstr_len(num1) " < " altinstr_len(num2) "\n" \
97 OLDINSTR_PADDING(oldinstr, num2) \
99 OLDINSTR_PADDING(oldinstr, num1) \
101 e_oldinstr_pad_end ":\n" \
102 INSTR_LEN_SANITY_CHECK(oldinstr_len)
104 #define ALTINSTR_ENTRY(facility, num) \
105 "\t.long 661b - .\n" /* old instruction */ \
106 "\t.long " b_altinstr(num)"b - .\n" /* alt instruction */ \
107 "\t.word " __stringify(facility) "\n" /* facility bit */ \
108 "\t.byte " oldinstr_total_len "\n" /* source len */ \
109 "\t.byte " altinstr_len(num) "\n" /* alt instruction len */
111 #define ALTINSTR_REPLACEMENT(altinstr, num) /* replacement */ \
112 b_altinstr(num)":\n\t" altinstr "\n" e_altinstr(num) ":\n" \
113 INSTR_LEN_SANITY_CHECK(altinstr_len(num))
115 /* alternative assembly primitive: */
116 #define ALTERNATIVE(oldinstr, altinstr, facility) \
117 ".pushsection .altinstr_replacement, \"ax\"\n" \
118 ALTINSTR_REPLACEMENT(altinstr, 1) \
120 OLDINSTR(oldinstr, 1) \
121 ".pushsection .altinstructions,\"a\"\n" \
122 ALTINSTR_ENTRY(facility, 1) \
125 #define ALTERNATIVE_2(oldinstr, altinstr1, facility1, altinstr2, facility2)\
126 ".pushsection .altinstr_replacement, \"ax\"\n" \
127 ALTINSTR_REPLACEMENT(altinstr1, 1) \
128 ALTINSTR_REPLACEMENT(altinstr2, 2) \
130 OLDINSTR_2(oldinstr, 1, 2) \
131 ".pushsection .altinstructions,\"a\"\n" \
132 ALTINSTR_ENTRY(facility1, 1) \
133 ALTINSTR_ENTRY(facility2, 2) \
137 * Alternative instructions for different CPU types or capabilities.
139 * This allows to use optimized instructions even on generic binary
142 * oldinstr is padded with jump and nops at compile time if altinstr is
143 * longer. altinstr is padded with jump and nops at run-time during patching.
145 * For non barrier like inlines please define new variants
146 * without volatile and memory clobber.
148 #define alternative(oldinstr, altinstr, facility) \
149 asm_inline volatile(ALTERNATIVE(oldinstr, altinstr, facility) : : : "memory")
151 #define alternative_2(oldinstr, altinstr1, facility1, altinstr2, facility2) \
152 asm_inline volatile(ALTERNATIVE_2(oldinstr, altinstr1, facility1, \
153 altinstr2, facility2) ::: "memory")
155 /* Alternative inline assembly with input. */
156 #define alternative_input(oldinstr, newinstr, feature, input...) \
157 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
160 /* Like alternative_input, but with a single output argument */
161 #define alternative_io(oldinstr, altinstr, facility, output, input...) \
162 asm_inline volatile(ALTERNATIVE(oldinstr, altinstr, facility) \
165 /* Use this macro if more than one output parameter is needed. */
166 #define ASM_OUTPUT2(a...) a
168 /* Use this macro if clobbers are needed without inputs. */
169 #define ASM_NO_INPUT_CLOBBER(clobber...) : clobber
171 #endif /* __ASSEMBLY__ */
173 #endif /* _ASM_S390_ALTERNATIVE_H */