1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_ALTERNATIVE_ASM_H
3 #define _ASM_S390_ALTERNATIVE_ASM_H
4 
5 #ifdef __ASSEMBLY__
6 
7 /*
8  * Check the length of an instruction sequence. The length may not be larger
9  * than 254 bytes and it has to be divisible by 2.
10  */
11 .macro alt_len_check start,end
12 	.if ( \end - \start ) > 254
13 	.error "cpu alternatives does not support instructions blocks > 254 bytes\n"
14 	.endif
15 	.if ( \end - \start ) % 2
16 	.error "cpu alternatives instructions length is odd\n"
17 	.endif
18 .endm
19 
20 /*
21  * Issue one struct alt_instr descriptor entry (need to put it into
22  * the section .altinstructions, see below). This entry contains
23  * enough information for the alternatives patching code to patch an
24  * instruction. See apply_alternatives().
25  */
26 .macro alt_entry orig_start, orig_end, alt_start, alt_end, feature
27 	.long	\orig_start - .
28 	.long	\alt_start - .
29 	.word	\feature
30 	.byte	\orig_end - \orig_start
31 	.byte	\alt_end - \alt_start
32 .endm
33 
34 /*
35  * Fill up @bytes with nops. The macro emits 6-byte nop instructions
36  * for the bulk of the area, possibly followed by a 4-byte and/or
37  * a 2-byte nop if the size of the area is not divisible by 6.
38  */
39 .macro alt_pad_fill bytes
40 	.rept	( \bytes ) / 6
41 	brcl	0,0
42 	.endr
43 	.rept	( \bytes ) % 6 / 4
44 	nop
45 	.endr
46 	.rept	( \bytes ) % 6 % 4 / 2
47 	nopr
48 	.endr
49 .endm
50 
51 /*
52  * Fill up @bytes with nops. If the number of bytes is larger
53  * than 6, emit a jg instruction to branch over all nops, then
54  * fill an area of size (@bytes - 6) with nop instructions.
55  */
56 .macro alt_pad bytes
57 	.if ( \bytes > 0 )
58 	.if ( \bytes > 6 )
59 	jg	. + \bytes
60 	alt_pad_fill \bytes - 6
61 	.else
62 	alt_pad_fill \bytes
63 	.endif
64 	.endif
65 .endm
66 
67 /*
68  * Define an alternative between two instructions. If @feature is
69  * present, early code in apply_alternatives() replaces @oldinstr with
70  * @newinstr. ".skip" directive takes care of proper instruction padding
71  * in case @newinstr is longer than @oldinstr.
72  */
73 .macro ALTERNATIVE oldinstr, newinstr, feature
74 	.pushsection .altinstr_replacement,"ax"
75 770:	\newinstr
76 771:	.popsection
77 772:	\oldinstr
78 773:	alt_len_check 770b, 771b
79 	alt_len_check 772b, 773b
80 	alt_pad ( ( 771b - 770b ) - ( 773b - 772b ) )
81 774:	.pushsection .altinstructions,"a"
82 	alt_entry 772b, 774b, 770b, 771b, \feature
83 	.popsection
84 .endm
85 
86 /*
87  * Define an alternative between two instructions. If @feature is
88  * present, early code in apply_alternatives() replaces @oldinstr with
89  * @newinstr. ".skip" directive takes care of proper instruction padding
90  * in case @newinstr is longer than @oldinstr.
91  */
92 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
93 	.pushsection .altinstr_replacement,"ax"
94 770:	\newinstr1
95 771:	\newinstr2
96 772:	.popsection
97 773:	\oldinstr
98 774:	alt_len_check 770b, 771b
99 	alt_len_check 771b, 772b
100 	alt_len_check 773b, 774b
101 	.if ( 771b - 770b > 772b - 771b )
102 	alt_pad ( ( 771b - 770b ) - ( 774b - 773b ) )
103 	.else
104 	alt_pad ( ( 772b - 771b ) - ( 774b - 773b ) )
105 	.endif
106 775:	.pushsection .altinstructions,"a"
107 	alt_entry 773b, 775b, 770b, 771b,\feature1
108 	alt_entry 773b, 775b, 771b, 772b,\feature2
109 	.popsection
110 .endm
111 
112 #endif	/*  __ASSEMBLY__  */
113 
114 #endif /* _ASM_S390_ALTERNATIVE_ASM_H */
115