1#ifndef _ASM_X86_ALTERNATIVE_H
2#define _ASM_X86_ALTERNATIVE_H
3
4#include <linux/types.h>
5#include <linux/stddef.h>
6#include <linux/stringify.h>
7#include <asm/asm.h>
8#include <asm/ptrace.h>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#ifdef CONFIG_SMP
32#define LOCK_PREFIX_HERE \
33 ".pushsection .smp_locks,\"a\"\n" \
34 ".balign 4\n" \
35 ".long 671f - .\n" \
36 ".popsection\n" \
37 "671:"
38
39#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
40
41#else
42#define LOCK_PREFIX_HERE ""
43#define LOCK_PREFIX ""
44#endif
45
46struct alt_instr {
47 s32 instr_offset;
48 s32 repl_offset;
49 u16 cpuid;
50 u8 instrlen;
51 u8 replacementlen;
52 u8 padlen;
53} __packed;
54
55
56
57
58
59extern int alternatives_patched;
60
61extern void alternative_instructions(void);
62extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
63
64struct module;
65
66#ifdef CONFIG_SMP
67extern void alternatives_smp_module_add(struct module *mod, char *name,
68 void *locks, void *locks_end,
69 void *text, void *text_end);
70extern void alternatives_smp_module_del(struct module *mod);
71extern void alternatives_enable_smp(void);
72extern int alternatives_text_reserved(void *start, void *end);
73extern bool skip_smp_alternatives;
74#else
75static inline void alternatives_smp_module_add(struct module *mod, char *name,
76 void *locks, void *locks_end,
77 void *text, void *text_end) {}
78static inline void alternatives_smp_module_del(struct module *mod) {}
79static inline void alternatives_enable_smp(void) {}
80static inline int alternatives_text_reserved(void *start, void *end)
81{
82 return 0;
83}
84#endif
85
86#define b_replacement(num) "664"#num
87#define e_replacement(num) "665"#num
88
89#define alt_end_marker "663"
90#define alt_slen "662b-661b"
91#define alt_pad_len alt_end_marker"b-662b"
92#define alt_total_slen alt_end_marker"b-661b"
93#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
94
95#define __OLDINSTR(oldinstr, num) \
96 "661:\n\t" oldinstr "\n662:\n" \
97 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
98 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
99
100#define OLDINSTR(oldinstr, num) \
101 __OLDINSTR(oldinstr, num) \
102 alt_end_marker ":\n"
103
104
105
106
107
108
109
110#define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") - (" b ")))))"
111
112
113
114
115
116#define OLDINSTR_2(oldinstr, num1, num2) \
117 "661:\n\t" oldinstr "\n662:\n" \
118 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
119 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
120 alt_end_marker ":\n"
121
122#define ALTINSTR_ENTRY(feature, num) \
123 " .long 661b - .\n" \
124 " .long " b_replacement(num)"f - .\n" \
125 " .word " __stringify(feature) "\n" \
126 " .byte " alt_total_slen "\n" \
127 " .byte " alt_rlen(num) "\n" \
128 " .byte " alt_pad_len "\n"
129
130#define ALTINSTR_REPLACEMENT(newinstr, feature, num) \
131 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
132
133
134#define ALTERNATIVE(oldinstr, newinstr, feature) \
135 OLDINSTR(oldinstr, 1) \
136 ".pushsection .altinstructions,\"a\"\n" \
137 ALTINSTR_ENTRY(feature, 1) \
138 ".popsection\n" \
139 ".pushsection .altinstr_replacement, \"ax\"\n" \
140 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
141 ".popsection"
142
143#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
144 OLDINSTR_2(oldinstr, 1, 2) \
145 ".pushsection .altinstructions,\"a\"\n" \
146 ALTINSTR_ENTRY(feature1, 1) \
147 ALTINSTR_ENTRY(feature2, 2) \
148 ".popsection\n" \
149 ".pushsection .altinstr_replacement, \"ax\"\n" \
150 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
151 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
152 ".popsection"
153
154
155
156
157
158#include <asm/cpufeature.h>
159
160
161
162
163
164
165
166
167
168
169
170
171
172#define alternative(oldinstr, newinstr, feature) \
173 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
174
175#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
176 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
177
178
179
180
181
182
183
184
185
186
187
188
189#define alternative_input(oldinstr, newinstr, feature, input...) \
190 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
191 : : "i" (0), ## input)
192
193
194
195
196
197
198
199
200
201#define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
202 feature2, input...) \
203 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
204 newinstr2, feature2) \
205 : : "i" (0), ## input)
206
207
208#define alternative_io(oldinstr, newinstr, feature, output, input...) \
209 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
210 : output : "i" (0), ## input)
211
212
213#define alternative_call(oldfunc, newfunc, feature, output, input...) \
214 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
215 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
216
217
218
219
220
221
222
223#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
224 output, input...) \
225 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
226 "call %P[new2]", feature2) \
227 : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
228 [new2] "i" (newfunc2), ## input)
229
230
231
232
233
234#define ASM_OUTPUT2(a...) a
235
236
237
238
239
240#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
241
242struct paravirt_patch_site;
243#ifdef CONFIG_PARAVIRT
244void apply_paravirt(struct paravirt_patch_site *start,
245 struct paravirt_patch_site *end);
246#else
247static inline void apply_paravirt(struct paravirt_patch_site *start,
248 struct paravirt_patch_site *end)
249{}
250#define __parainstructions NULL
251#define __parainstructions_end NULL
252#endif
253
254extern void *text_poke_early(void *addr, const void *opcode, size_t len);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270extern void *text_poke(void *addr, const void *opcode, size_t len);
271extern int poke_int3_handler(struct pt_regs *regs);
272extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
273
274#endif
275