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