blob: 092a7b8be68dc8f7f9a0d7b1936686d779e9ef25 [file] [log] [blame]
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -04002#include <linux/sched.h>
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +02003#include <linux/mutex.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08004#include <linux/list.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +02005#include <linux/kprobes.h>
6#include <linux/mm.h>
7#include <linux/vmalloc.h>
Masami Hiramatsu3945dab2009-03-06 10:37:22 -05008#include <linux/memory.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08009#include <asm/alternative.h>
10#include <asm/sections.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020011#include <asm/pgtable.h>
Andi Kleen8f4e9562007-07-22 11:12:32 +020012#include <asm/mce.h>
13#include <asm/nmi.h>
Dave Jonesb0979762007-10-14 22:57:45 +020014#include <asm/vsyscall.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050015#include <asm/cacheflush.h>
16#include <asm/io.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080017
Andi Kleenab144f52007-08-10 22:31:03 +020018#define MAX_PATCH_LEN (255-1)
19
Jan Beulich09488162007-07-21 17:10:25 +020020#ifdef CONFIG_HOTPLUG_CPU
21static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080022
Gerd Hoffmannd167a512006-06-26 13:56:16 +020023static int __init bootonly(char *str)
24{
25 smp_alt_once = 1;
26 return 1;
27}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020028__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020029#else
30#define smp_alt_once 1
31#endif
32
33static int debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020034
Gerd Hoffmannd167a512006-06-26 13:56:16 +020035static int __init debug_alt(char *str)
36{
37 debug_alternative = 1;
38 return 1;
39}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020040__setup("debug-alternative", debug_alt);
41
Jan Beulich09488162007-07-21 17:10:25 +020042static int noreplace_smp;
43
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020044static int __init setup_noreplace_smp(char *str)
45{
46 noreplace_smp = 1;
47 return 1;
48}
49__setup("noreplace-smp", setup_noreplace_smp);
50
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020051#ifdef CONFIG_PARAVIRT
52static int noreplace_paravirt = 0;
53
54static int __init setup_noreplace_paravirt(char *str)
55{
56 noreplace_paravirt = 1;
57 return 1;
58}
59__setup("noreplace-paravirt", setup_noreplace_paravirt);
60#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020061
Gerd Hoffmannd167a512006-06-26 13:56:16 +020062#define DPRINTK(fmt, args...) if (debug_alternative) \
63 printk(KERN_DEBUG fmt, args)
64
65#ifdef GENERIC_NOP1
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080066/* Use inline assembly to define this because the nops are defined
67 as inline assembly strings in the include files and we cannot
68 get them easily into strings. */
Jan Beulich121d7bf2007-10-17 18:04:37 +020069asm("\t.section .rodata, \"a\"\nintelnops: "
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080070 GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040071 GENERIC_NOP7 GENERIC_NOP8
72 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020073extern const unsigned char intelnops[];
74static const unsigned char *const intel_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080075 NULL,
76 intelnops,
77 intelnops + 1,
78 intelnops + 1 + 2,
79 intelnops + 1 + 2 + 3,
80 intelnops + 1 + 2 + 3 + 4,
81 intelnops + 1 + 2 + 3 + 4 + 5,
82 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
83 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
84};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020085#endif
86
87#ifdef K8_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +020088asm("\t.section .rodata, \"a\"\nk8nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +020089 K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040090 K8_NOP7 K8_NOP8
91 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020092extern const unsigned char k8nops[];
93static const unsigned char *const k8_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080094 NULL,
95 k8nops,
96 k8nops + 1,
97 k8nops + 1 + 2,
98 k8nops + 1 + 2 + 3,
99 k8nops + 1 + 2 + 3 + 4,
100 k8nops + 1 + 2 + 3 + 4 + 5,
101 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
102 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
103};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200104#endif
105
106#ifdef K7_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +0200107asm("\t.section .rodata, \"a\"\nk7nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200108 K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400109 K7_NOP7 K7_NOP8
110 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +0200111extern const unsigned char k7nops[];
112static const unsigned char *const k7_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800113 NULL,
114 k7nops,
115 k7nops + 1,
116 k7nops + 1 + 2,
117 k7nops + 1 + 2 + 3,
118 k7nops + 1 + 2 + 3 + 4,
119 k7nops + 1 + 2 + 3 + 4 + 5,
120 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
121 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
122};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200123#endif
124
Jan Beulich32c464f2007-10-17 18:04:41 +0200125#ifdef P6_NOP1
126asm("\t.section .rodata, \"a\"\np6nops: "
127 P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400128 P6_NOP7 P6_NOP8
129 "\t.previous");
Jan Beulich32c464f2007-10-17 18:04:41 +0200130extern const unsigned char p6nops[];
131static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
132 NULL,
133 p6nops,
134 p6nops + 1,
135 p6nops + 1 + 2,
136 p6nops + 1 + 2 + 3,
137 p6nops + 1 + 2 + 3 + 4,
138 p6nops + 1 + 2 + 3 + 4 + 5,
139 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
140 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
141};
142#endif
143
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200144#ifdef CONFIG_X86_64
145
146extern char __vsyscall_0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200147const unsigned char *const *find_nop_table(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200148{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700149 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
150 boot_cpu_has(X86_FEATURE_NOPL))
151 return p6_nops;
152 else
153 return k8_nops;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200154}
155
156#else /* CONFIG_X86_64 */
157
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200158const unsigned char *const *find_nop_table(void)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800159{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700160 if (boot_cpu_has(X86_FEATURE_K8))
161 return k8_nops;
162 else if (boot_cpu_has(X86_FEATURE_K7))
163 return k7_nops;
164 else if (boot_cpu_has(X86_FEATURE_NOPL))
165 return p6_nops;
166 else
167 return intel_nops;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800168}
169
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200170#endif /* CONFIG_X86_64 */
171
Andi Kleenab144f52007-08-10 22:31:03 +0200172/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500173void add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100174{
Jan Beulich121d7bf2007-10-17 18:04:37 +0200175 const unsigned char *const *noptable = find_nop_table();
Rusty Russell139ec7c2006-12-07 02:14:08 +0100176
177 while (len > 0) {
178 unsigned int noplen = len;
179 if (noplen > ASM_NOP_MAX)
180 noplen = ASM_NOP_MAX;
Andi Kleenab144f52007-08-10 22:31:03 +0200181 memcpy(insns, noptable[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100182 insns += noplen;
183 len -= noplen;
184 }
185}
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500186EXPORT_SYMBOL_GPL(add_nops);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100187
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200188extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200189extern u8 *__smp_locks[], *__smp_locks_end[];
190
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800191/* Replace instructions with better alternatives for this CPU type.
192 This runs before SMP is initialized to avoid SMP problems with
193 self modifying code. This implies that assymetric systems where
194 APs have less capabilities than the boot processor are not handled.
195 Tough. Make sure you disable such features by hand. */
196
197void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
198{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800199 struct alt_instr *a;
Andi Kleenab144f52007-08-10 22:31:03 +0200200 char insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800201
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800202 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800203 for (a = start; a < end; a++) {
Andi Kleenab144f52007-08-10 22:31:03 +0200204 u8 *instr = a->instr;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800205 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200206 BUG_ON(a->instrlen > sizeof(insnbuf));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800207 if (!boot_cpu_has(a->cpuid))
208 continue;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200209#ifdef CONFIG_X86_64
210 /* vsyscall code is not mapped yet. resolve it manually. */
211 if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
212 instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
213 DPRINTK("%s: vsyscall fixup: %p => %p\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800214 __func__, a->instr, instr);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200215 }
216#endif
Andi Kleenab144f52007-08-10 22:31:03 +0200217 memcpy(insnbuf, a->replacement, a->replacementlen);
218 add_nops(insnbuf + a->replacementlen,
219 a->instrlen - a->replacementlen);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500220 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800221 }
222}
223
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700224#ifdef CONFIG_SMP
225
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800226static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
227{
228 u8 **ptr;
229
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500230 mutex_lock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800231 for (ptr = start; ptr < end; ptr++) {
232 if (*ptr < text)
233 continue;
234 if (*ptr > text_end)
235 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400236 /* turn DS segment override prefix into lock prefix */
237 text_poke(*ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800238 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500239 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800240}
241
242static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
243{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800244 u8 **ptr;
245
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200246 if (noreplace_smp)
247 return;
248
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500249 mutex_lock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800250 for (ptr = start; ptr < end; ptr++) {
251 if (*ptr < text)
252 continue;
253 if (*ptr > text_end)
254 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400255 /* turn lock prefix into DS segment override prefix */
256 text_poke(*ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800257 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500258 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800259}
260
261struct smp_alt_module {
262 /* what is this ??? */
263 struct module *mod;
264 char *name;
265
266 /* ptrs to lock prefixes */
267 u8 **locks;
268 u8 **locks_end;
269
270 /* .text segment, needed to avoid patching init code ;) */
271 u8 *text;
272 u8 *text_end;
273
274 struct list_head next;
275};
276static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200277static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100278static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800279
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800280void alternatives_smp_module_add(struct module *mod, char *name,
281 void *locks, void *locks_end,
282 void *text, void *text_end)
283{
284 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800285
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200286 if (noreplace_smp)
287 return;
288
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800289 if (smp_alt_once) {
290 if (boot_cpu_has(X86_FEATURE_UP))
291 alternatives_smp_unlock(locks, locks_end,
292 text, text_end);
293 return;
294 }
295
296 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
297 if (NULL == smp)
298 return; /* we'll run the (safe but slow) SMP code then ... */
299
300 smp->mod = mod;
301 smp->name = name;
302 smp->locks = locks;
303 smp->locks_end = locks_end;
304 smp->text = text;
305 smp->text_end = text_end;
306 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800307 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800308 smp->text, smp->text_end, smp->name);
309
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200310 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800311 list_add_tail(&smp->next, &smp_alt_modules);
312 if (boot_cpu_has(X86_FEATURE_UP))
313 alternatives_smp_unlock(smp->locks, smp->locks_end,
314 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200315 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800316}
317
318void alternatives_smp_module_del(struct module *mod)
319{
320 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800321
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200322 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800323 return;
324
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200325 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800326 list_for_each_entry(item, &smp_alt_modules, next) {
327 if (mod != item->mod)
328 continue;
329 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200330 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800331 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800332 kfree(item);
333 return;
334 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200335 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800336}
337
338void alternatives_smp_switch(int smp)
339{
340 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800341
Ingo Molnar3047e992006-07-03 00:24:57 -0700342#ifdef CONFIG_LOCKDEP
343 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100344 * Older binutils section handling bug prevented
345 * alternatives-replacement from working reliably.
346 *
347 * If this still occurs then you should see a hang
348 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700349 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100350 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700351#endif
352
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200353 if (noreplace_smp || smp_alt_once)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800354 return;
355 BUG_ON(!smp && (num_online_cpus() > 1));
356
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200357 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100358
359 /*
360 * Avoid unnecessary switches because it forces JIT based VMs to
361 * throw away all cached translations, which can be quite costly.
362 */
363 if (smp == smp_mode) {
364 /* nothing */
365 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800366 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100367 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
368 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800369 list_for_each_entry(mod, &smp_alt_modules, next)
370 alternatives_smp_lock(mod->locks, mod->locks_end,
371 mod->text, mod->text_end);
372 } else {
373 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100374 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
375 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800376 list_for_each_entry(mod, &smp_alt_modules, next)
377 alternatives_smp_unlock(mod->locks, mod->locks_end,
378 mod->text, mod->text_end);
379 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100380 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200381 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800382}
383
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700384#endif
385
Rusty Russell139ec7c2006-12-07 02:14:08 +0100386#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200387void apply_paravirt(struct paravirt_patch_site *start,
388 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100389{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200390 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200391 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100392
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200393 if (noreplace_paravirt)
394 return;
395
Rusty Russell139ec7c2006-12-07 02:14:08 +0100396 for (p = start; p < end; p++) {
397 unsigned int used;
398
Andi Kleenab144f52007-08-10 22:31:03 +0200399 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700400 /* prep the buffer with the original instructions */
401 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700402 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
403 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200404
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200405 BUG_ON(used > p->len);
406
Rusty Russell139ec7c2006-12-07 02:14:08 +0100407 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200408 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500409 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100410 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100411}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200412extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100413 __stop_parainstructions[];
414#endif /* CONFIG_PARAVIRT */
415
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800416void __init alternative_instructions(void)
417{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200418 /* The patching is not fully atomic, so try to avoid local interruptions
419 that might execute the to be patched code.
420 Other CPUs are not running. */
421 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100422
423 /*
424 * Don't stop machine check exceptions while patching.
425 * MCEs only happen when something got corrupted and in this
426 * case we must do something about the corruption.
427 * Ignoring it is worse than a unlikely patching race.
428 * Also machine checks tend to be broadcast and if one CPU
429 * goes into machine check the others follow quickly, so we don't
430 * expect a machine check to cause undue problems during to code
431 * patching.
432 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200433
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800434 apply_alternatives(__alt_instructions, __alt_instructions_end);
435
436 /* switch to patch-once-at-boottime-only mode and free the
437 * tables in case we know the number of CPUs will never ever
438 * change */
439#ifdef CONFIG_HOTPLUG_CPU
440 if (num_possible_cpus() < 2)
441 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800442#endif
443
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700444#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800445 if (smp_alt_once) {
446 if (1 == num_possible_cpus()) {
447 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100448 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
449 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
450
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800451 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
452 _text, _etext);
453 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800454 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800455 alternatives_smp_module_add(NULL, "core kernel",
456 __smp_locks, __smp_locks_end,
457 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100458
459 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200460 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100461 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800462 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700463#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200464 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200465
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200466 if (smp_alt_once)
467 free_init_pages("SMP alternatives",
468 (unsigned long)__smp_locks,
469 (unsigned long)__smp_locks_end);
470
Andi Kleen8f4e9562007-07-22 11:12:32 +0200471 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800472}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200473
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500474/**
475 * text_poke_early - Update instructions on a live kernel at boot time
476 * @addr: address to modify
477 * @opcode: source of the copy
478 * @len: length to copy
479 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200480 * When you use this code to patch more than one byte of an instruction
481 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500482 * Also no thread must be currently preempted in the middle of these
483 * instructions. And on the local CPU you need to be protected again NMI or MCE
484 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200485 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500486void *text_poke_early(void *addr, const void *opcode, size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200487{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500488 unsigned long flags;
489 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200490 memcpy(addr, opcode, len);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500491 local_irq_restore(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200492 sync_core();
Andi Kleena534b672007-09-06 16:59:52 +0200493 /* Could also do a CLFLUSH here to speed up CPU recovery; but
494 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500495 return addr;
496}
497
498/**
499 * text_poke - Update instructions on a live kernel
500 * @addr: address to modify
501 * @opcode: source of the copy
502 * @len: length to copy
503 *
504 * Only atomic text poke/set should be allowed when not doing early patching.
505 * It means the size must be writable atomically and the address must be aligned
506 * in a way that permits an atomic write. It also makes sure we fit on a single
507 * page.
508 */
509void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
510{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500511 char *vaddr;
512 int nr_pages = 2;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400513 struct page *pages[2];
514 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500515
Peter Zijlstra34754b62009-02-25 16:04:03 +0100516 might_sleep();
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400517 if (!core_kernel_text((unsigned long)addr)) {
518 pages[0] = vmalloc_to_page(addr);
519 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400520 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400521 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200522 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400523 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500524 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400525 BUG_ON(!pages[0]);
526 if (!pages[1])
527 nr_pages = 1;
528 vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
529 BUG_ON(!vaddr);
Peter Zijlstra34754b62009-02-25 16:04:03 +0100530 local_irq_disable();
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400531 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Peter Zijlstra34754b62009-02-25 16:04:03 +0100532 local_irq_enable();
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400533 vunmap(vaddr);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500534 sync_core();
535 /* Could also do a CLFLUSH here to speed up CPU recovery; but
536 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400537 for (i = 0; i < len; i++)
538 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500539 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200540}