• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _ASM_X86_ALTERNATIVE_H
2 #define _ASM_X86_ALTERNATIVE_H
3 
4 #ifndef __ASSEMBLY__
5 
6 #include <linux/types.h>
7 #include <linux/stddef.h>
8 #include <linux/stringify.h>
9 #include <asm/asm.h>
10 #include <asm/ptrace.h>
11 
12 /*
13  * Alternative inline assembly for SMP.
14  *
15  * The LOCK_PREFIX macro defined here replaces the LOCK and
16  * LOCK_PREFIX macros used everywhere in the source tree.
17  *
18  * SMP alternatives use the same data structures as the other
19  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
20  * UP system running a SMP kernel.  The existing apply_alternatives()
21  * works fine for patching a SMP kernel for UP.
22  *
23  * The SMP alternative tables can be kept after boot and contain both
24  * UP and SMP versions of the instructions to allow switching back to
25  * SMP at runtime, when hotplugging in a new CPU, which is especially
26  * useful in virtualized environments.
27  *
28  * The very common lock prefix is handled as special case in a
29  * separate table which is a pure address list without replacement ptr
30  * and size information.  That keeps the table sizes small.
31  */
32 
33 #ifdef CONFIG_SMP
34 #define LOCK_PREFIX_HERE \
35 		".pushsection .smp_locks,\"a\"\n"	\
36 		".balign 4\n"				\
37 		".long 671f - .\n" /* offset */		\
38 		".popsection\n"				\
39 		"671:"
40 
41 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
42 
43 #else /* ! CONFIG_SMP */
44 #define LOCK_PREFIX_HERE ""
45 #define LOCK_PREFIX ""
46 #endif
47 
48 struct alt_instr {
49 	s32 instr_offset;	/* original instruction */
50 	s32 repl_offset;	/* offset to replacement instruction */
51 	u16 cpuid;		/* cpuid bit set for replacement */
52 	u8  instrlen;		/* length of original instruction */
53 	u8  replacementlen;	/* length of new instruction */
54 	u8  padlen;		/* length of build-time padding */
55 } __packed;
56 
57 /*
58  * Debug flag that can be tested to see whether alternative
59  * instructions were patched in already:
60  */
61 extern int alternatives_patched;
62 
63 extern void alternative_instructions(void);
64 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
65 
66 struct module;
67 
68 #ifdef CONFIG_SMP
69 extern void alternatives_smp_module_add(struct module *mod, char *name,
70 					void *locks, void *locks_end,
71 					void *text, void *text_end);
72 extern void alternatives_smp_module_del(struct module *mod);
73 extern void alternatives_enable_smp(void);
74 extern int alternatives_text_reserved(void *start, void *end);
75 extern bool skip_smp_alternatives;
76 #else
alternatives_smp_module_add(struct module * mod,char * name,void * locks,void * locks_end,void * text,void * text_end)77 static inline void alternatives_smp_module_add(struct module *mod, char *name,
78 					       void *locks, void *locks_end,
79 					       void *text, void *text_end) {}
alternatives_smp_module_del(struct module * mod)80 static inline void alternatives_smp_module_del(struct module *mod) {}
alternatives_enable_smp(void)81 static inline void alternatives_enable_smp(void) {}
alternatives_text_reserved(void * start,void * end)82 static inline int alternatives_text_reserved(void *start, void *end)
83 {
84 	return 0;
85 }
86 #endif	/* CONFIG_SMP */
87 
88 #define b_replacement(num)	"664"#num
89 #define e_replacement(num)	"665"#num
90 
91 #define alt_end_marker		"663"
92 #define alt_slen		"662b-661b"
93 #define alt_pad_len		alt_end_marker"b-662b"
94 #define alt_total_slen		alt_end_marker"b-661b"
95 #define alt_rlen(num)		e_replacement(num)"f-"b_replacement(num)"f"
96 
97 #define __OLDINSTR(oldinstr, num)					\
98 	"661:\n\t" oldinstr "\n662:\n"					\
99 	".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "		\
100 		"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
101 
102 #define OLDINSTR(oldinstr, num)						\
103 	__OLDINSTR(oldinstr, num)					\
104 	alt_end_marker ":\n"
105 
106 /*
107  * gas compatible max based on the idea from:
108  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
109  *
110  * The additional "-" is needed because gas uses a "true" value of -1.
111  */
112 #define alt_max_short(a, b)	"((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
113 
114 /*
115  * Pad the second replacement alternative with additional NOPs if it is
116  * additionally longer than the first replacement alternative.
117  */
118 #define OLDINSTR_2(oldinstr, num1, num2) \
119 	"661:\n\t" oldinstr "\n662:\n"								\
120 	".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "	\
121 		"(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"	\
122 	alt_end_marker ":\n"
123 
124 #define ALTINSTR_ENTRY(feature, num)					      \
125 	" .long 661b - .\n"				/* label           */ \
126 	" .long " b_replacement(num)"f - .\n"		/* new instruction */ \
127 	" .word " __stringify(feature) "\n"		/* feature bit     */ \
128 	" .byte " alt_total_slen "\n"			/* source len      */ \
129 	" .byte " alt_rlen(num) "\n"			/* replacement len */ \
130 	" .byte " alt_pad_len "\n"			/* pad len */
131 
132 #define ALTINSTR_REPLACEMENT(newinstr, feature, num)	/* replacement */     \
133 	b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
134 
135 /* alternative assembly primitive: */
136 #define ALTERNATIVE(oldinstr, newinstr, feature)			\
137 	OLDINSTR(oldinstr, 1)						\
138 	".pushsection .altinstructions,\"a\"\n"				\
139 	ALTINSTR_ENTRY(feature, 1)					\
140 	".popsection\n"							\
141 	".pushsection .altinstr_replacement, \"ax\"\n"			\
142 	ALTINSTR_REPLACEMENT(newinstr, feature, 1)			\
143 	".popsection\n"
144 
145 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
146 	OLDINSTR_2(oldinstr, 1, 2)					\
147 	".pushsection .altinstructions,\"a\"\n"				\
148 	ALTINSTR_ENTRY(feature1, 1)					\
149 	ALTINSTR_ENTRY(feature2, 2)					\
150 	".popsection\n"							\
151 	".pushsection .altinstr_replacement, \"ax\"\n"			\
152 	ALTINSTR_REPLACEMENT(newinstr1, feature1, 1)			\
153 	ALTINSTR_REPLACEMENT(newinstr2, feature2, 2)			\
154 	".popsection\n"
155 
156 /*
157  * Alternative instructions for different CPU types or capabilities.
158  *
159  * This allows to use optimized instructions even on generic binary
160  * kernels.
161  *
162  * length of oldinstr must be longer or equal the length of newinstr
163  * It can be padded with nops as needed.
164  *
165  * For non barrier like inlines please define new variants
166  * without volatile and memory clobber.
167  */
168 #define alternative(oldinstr, newinstr, feature)			\
169 	asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
170 
171 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
172 	asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
173 
174 /*
175  * Alternative inline assembly with input.
176  *
177  * Pecularities:
178  * No memory clobber here.
179  * Argument numbers start with 1.
180  * Best is to use constraints that are fixed size (like (%1) ... "r")
181  * If you use variable sized constraints like "m" or "g" in the
182  * replacement make sure to pad to the worst case length.
183  * Leaving an unused argument 0 to keep API compatibility.
184  */
185 #define alternative_input(oldinstr, newinstr, feature, input...)	\
186 	asm volatile (ALTERNATIVE(oldinstr, newinstr, feature)		\
187 		: : "i" (0), ## input)
188 
189 /*
190  * This is similar to alternative_input. But it has two features and
191  * respective instructions.
192  *
193  * If CPU has feature2, newinstr2 is used.
194  * Otherwise, if CPU has feature1, newinstr1 is used.
195  * Otherwise, oldinstr is used.
196  */
197 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2,	     \
198 			   feature2, input...)				     \
199 	asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1,	     \
200 		newinstr2, feature2)					     \
201 		: : "i" (0), ## input)
202 
203 /* Like alternative_input, but with a single output argument */
204 #define alternative_io(oldinstr, newinstr, feature, output, input...)	\
205 	asm volatile (ALTERNATIVE(oldinstr, newinstr, feature)		\
206 		: output : "i" (0), ## input)
207 
208 /* Like alternative_io, but for replacing a direct call with another one. */
209 #define alternative_call(oldfunc, newfunc, feature, output, input...)	\
210 	asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
211 		: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
212 
213 /*
214  * Like alternative_call, but there are two features and respective functions.
215  * If CPU has feature2, function2 is used.
216  * Otherwise, if CPU has feature1, function1 is used.
217  * Otherwise, old function is used.
218  */
219 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2,   \
220 			   output, input...)				      \
221 	asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
222 		"call %P[new2]", feature2)				      \
223 		: output : [old] "i" (oldfunc), [new1] "i" (newfunc1),	      \
224 		[new2] "i" (newfunc2), ## input)
225 
226 /*
227  * use this macro(s) if you need more than one output parameter
228  * in alternative_io
229  */
230 #define ASM_OUTPUT2(a...) a
231 
232 /*
233  * use this macro if you need clobbers but no inputs in
234  * alternative_{input,io,call}()
235  */
236 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
237 
238 struct paravirt_patch_site;
239 #ifdef CONFIG_PARAVIRT
240 void apply_paravirt(struct paravirt_patch_site *start,
241 		    struct paravirt_patch_site *end);
242 #else
apply_paravirt(struct paravirt_patch_site * start,struct paravirt_patch_site * end)243 static inline void apply_paravirt(struct paravirt_patch_site *start,
244 				  struct paravirt_patch_site *end)
245 {}
246 #define __parainstructions	NULL
247 #define __parainstructions_end	NULL
248 #endif
249 
250 extern void *text_poke_early(void *addr, const void *opcode, size_t len);
251 
252 /*
253  * Clear and restore the kernel write-protection flag on the local CPU.
254  * Allows the kernel to edit read-only pages.
255  * Side-effect: any interrupt handler running between save and restore will have
256  * the ability to write to read-only pages.
257  *
258  * Warning:
259  * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
260  * no thread can be preempted in the instructions being modified (no iret to an
261  * invalid instruction possible) or if the instructions are changed from a
262  * consistent state to another consistent state atomically.
263  * On the local CPU you need to be protected again NMI or MCE handlers seeing an
264  * inconsistent instruction while you patch.
265  */
266 extern void *text_poke(void *addr, const void *opcode, size_t len);
267 extern int poke_int3_handler(struct pt_regs *regs);
268 extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
269 
270 #endif /* __ASSEMBLY__ */
271 
272 #endif /* _ASM_X86_ALTERNATIVE_H */
273