x86/alternatives: Macrofy lock prefixes to work around GCC inlining bugs
[linux-2.6-microblaze.git] / arch / x86 / include / asm / alternative-asm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ALTERNATIVE_ASM_H
3 #define _ASM_X86_ALTERNATIVE_ASM_H
4
5 #ifdef __ASSEMBLY__
6
7 #include <asm/asm.h>
8
9 #ifdef CONFIG_SMP
10 .macro LOCK_PREFIX_HERE
11         .pushsection .smp_locks,"a"
12         .balign 4
13         .long 671f - .          # offset
14         .popsection
15 671:
16 .endm
17
18 .macro LOCK_PREFIX insn:vararg
19         LOCK_PREFIX_HERE
20         lock \insn
21 .endm
22 #else
23 .macro LOCK_PREFIX_HERE
24 .endm
25
26 .macro LOCK_PREFIX insn:vararg
27 .endm
28 #endif
29
30 /*
31  * Issue one struct alt_instr descriptor entry (need to put it into
32  * the section .altinstructions, see below). This entry contains
33  * enough information for the alternatives patching code to patch an
34  * instruction. See apply_alternatives().
35  */
36 .macro altinstruction_entry orig alt feature orig_len alt_len pad_len
37         .long \orig - .
38         .long \alt - .
39         .word \feature
40         .byte \orig_len
41         .byte \alt_len
42         .byte \pad_len
43 .endm
44
45 /*
46  * Define an alternative between two instructions. If @feature is
47  * present, early code in apply_alternatives() replaces @oldinstr with
48  * @newinstr. ".skip" directive takes care of proper instruction padding
49  * in case @newinstr is longer than @oldinstr.
50  */
51 .macro ALTERNATIVE oldinstr, newinstr, feature
52 140:
53         \oldinstr
54 141:
55         .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
56 142:
57
58         .pushsection .altinstructions,"a"
59         altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f,142b-141b
60         .popsection
61
62         .pushsection .altinstr_replacement,"ax"
63 143:
64         \newinstr
65 144:
66         .popsection
67 .endm
68
69 #define old_len                 141b-140b
70 #define new_len1                144f-143f
71 #define new_len2                145f-144f
72
73 /*
74  * gas compatible max based on the idea from:
75  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
76  *
77  * The additional "-" is needed because gas uses a "true" value of -1.
78  */
79 #define alt_max_short(a, b)     ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
80
81
82 /*
83  * Same as ALTERNATIVE macro above but for two alternatives. If CPU
84  * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
85  * @feature2, it replaces @oldinstr with @feature2.
86  */
87 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
88 140:
89         \oldinstr
90 141:
91         .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
92                 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
93 142:
94
95         .pushsection .altinstructions,"a"
96         altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f,142b-141b
97         altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f,142b-141b
98         .popsection
99
100         .pushsection .altinstr_replacement,"ax"
101 143:
102         \newinstr1
103 144:
104         \newinstr2
105 145:
106         .popsection
107 .endm
108
109 #endif  /*  __ASSEMBLY__  */
110
111 #endif /* _ASM_X86_ALTERNATIVE_ASM_H */