blob: 45d79ea890aee92107f451b9554cb69080c00d0b [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>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08003#include <linux/spinlock.h>
4#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>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08008#include <asm/alternative.h>
9#include <asm/sections.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020010#include <asm/pgtable.h>
Andi Kleen8f4e9562007-07-22 11:12:32 +020011#include <asm/mce.h>
12#include <asm/nmi.h>
Dave Jonesb0979762007-10-14 22:57:45 +020013#include <asm/vsyscall.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080014
Andi Kleenab144f52007-08-10 22:31:03 +020015#define MAX_PATCH_LEN (255-1)
16
Jan Beulich09488162007-07-21 17:10:25 +020017#ifdef CONFIG_HOTPLUG_CPU
18static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080019
Gerd Hoffmannd167a512006-06-26 13:56:16 +020020static int __init bootonly(char *str)
21{
22 smp_alt_once = 1;
23 return 1;
24}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020025__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020026#else
27#define smp_alt_once 1
28#endif
29
30static int debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020031
Gerd Hoffmannd167a512006-06-26 13:56:16 +020032static int __init debug_alt(char *str)
33{
34 debug_alternative = 1;
35 return 1;
36}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020037__setup("debug-alternative", debug_alt);
38
Jan Beulich09488162007-07-21 17:10:25 +020039static int noreplace_smp;
40
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020041static int __init setup_noreplace_smp(char *str)
42{
43 noreplace_smp = 1;
44 return 1;
45}
46__setup("noreplace-smp", setup_noreplace_smp);
47
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020048#ifdef CONFIG_PARAVIRT
49static int noreplace_paravirt = 0;
50
51static int __init setup_noreplace_paravirt(char *str)
52{
53 noreplace_paravirt = 1;
54 return 1;
55}
56__setup("noreplace-paravirt", setup_noreplace_paravirt);
57#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020058
Gerd Hoffmannd167a512006-06-26 13:56:16 +020059#define DPRINTK(fmt, args...) if (debug_alternative) \
60 printk(KERN_DEBUG fmt, args)
61
62#ifdef GENERIC_NOP1
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080063/* Use inline assembly to define this because the nops are defined
64 as inline assembly strings in the include files and we cannot
65 get them easily into strings. */
Jan Beulich121d7bf2007-10-17 18:04:37 +020066asm("\t.section .rodata, \"a\"\nintelnops: "
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080067 GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
68 GENERIC_NOP7 GENERIC_NOP8);
Jan Beulich121d7bf2007-10-17 18:04:37 +020069extern const unsigned char intelnops[];
70static const unsigned char *const intel_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080071 NULL,
72 intelnops,
73 intelnops + 1,
74 intelnops + 1 + 2,
75 intelnops + 1 + 2 + 3,
76 intelnops + 1 + 2 + 3 + 4,
77 intelnops + 1 + 2 + 3 + 4 + 5,
78 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
79 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
80};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020081#endif
82
83#ifdef K8_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +020084asm("\t.section .rodata, \"a\"\nk8nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +020085 K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
86 K8_NOP7 K8_NOP8);
Jan Beulich121d7bf2007-10-17 18:04:37 +020087extern const unsigned char k8nops[];
88static const unsigned char *const k8_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080089 NULL,
90 k8nops,
91 k8nops + 1,
92 k8nops + 1 + 2,
93 k8nops + 1 + 2 + 3,
94 k8nops + 1 + 2 + 3 + 4,
95 k8nops + 1 + 2 + 3 + 4 + 5,
96 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
97 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
98};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020099#endif
100
101#ifdef K7_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +0200102asm("\t.section .rodata, \"a\"\nk7nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200103 K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
104 K7_NOP7 K7_NOP8);
Jan Beulich121d7bf2007-10-17 18:04:37 +0200105extern const unsigned char k7nops[];
106static const unsigned char *const k7_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800107 NULL,
108 k7nops,
109 k7nops + 1,
110 k7nops + 1 + 2,
111 k7nops + 1 + 2 + 3,
112 k7nops + 1 + 2 + 3 + 4,
113 k7nops + 1 + 2 + 3 + 4 + 5,
114 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
115 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
116};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200117#endif
118
Jan Beulich32c464f2007-10-17 18:04:41 +0200119#ifdef P6_NOP1
120asm("\t.section .rodata, \"a\"\np6nops: "
121 P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
122 P6_NOP7 P6_NOP8);
123extern const unsigned char p6nops[];
124static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
125 NULL,
126 p6nops,
127 p6nops + 1,
128 p6nops + 1 + 2,
129 p6nops + 1 + 2 + 3,
130 p6nops + 1 + 2 + 3 + 4,
131 p6nops + 1 + 2 + 3 + 4 + 5,
132 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
133 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
134};
135#endif
136
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200137#ifdef CONFIG_X86_64
138
139extern char __vsyscall_0;
Jan Beulich121d7bf2007-10-17 18:04:37 +0200140static inline const unsigned char*const * find_nop_table(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200141{
Jan Beulich32c464f2007-10-17 18:04:41 +0200142 return boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
143 boot_cpu_data.x86 < 6 ? k8_nops : p6_nops;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200144}
145
146#else /* CONFIG_X86_64 */
147
Jan Beulich121d7bf2007-10-17 18:04:37 +0200148static const struct nop {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800149 int cpuid;
Jan Beulich121d7bf2007-10-17 18:04:37 +0200150 const unsigned char *const *noptable;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800151} noptypes[] = {
152 { X86_FEATURE_K8, k8_nops },
153 { X86_FEATURE_K7, k7_nops },
Jan Beulich32c464f2007-10-17 18:04:41 +0200154 { X86_FEATURE_P4, p6_nops },
155 { X86_FEATURE_P3, p6_nops },
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800156 { -1, NULL }
157};
158
Jan Beulich121d7bf2007-10-17 18:04:37 +0200159static const unsigned char*const * find_nop_table(void)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800160{
Jan Beulich121d7bf2007-10-17 18:04:37 +0200161 const unsigned char *const *noptable = intel_nops;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800162 int i;
163
164 for (i = 0; noptypes[i].cpuid >= 0; i++) {
165 if (boot_cpu_has(noptypes[i].cpuid)) {
166 noptable = noptypes[i].noptable;
167 break;
168 }
169 }
170 return noptable;
171}
172
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200173#endif /* CONFIG_X86_64 */
174
Andi Kleenab144f52007-08-10 22:31:03 +0200175/* Use this to add nops to a buffer, then text_poke the whole buffer. */
176static void add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100177{
Jan Beulich121d7bf2007-10-17 18:04:37 +0200178 const unsigned char *const *noptable = find_nop_table();
Rusty Russell139ec7c2006-12-07 02:14:08 +0100179
180 while (len > 0) {
181 unsigned int noplen = len;
182 if (noplen > ASM_NOP_MAX)
183 noplen = ASM_NOP_MAX;
Andi Kleenab144f52007-08-10 22:31:03 +0200184 memcpy(insns, noptable[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100185 insns += noplen;
186 len -= noplen;
187 }
188}
189
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200190extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200191extern u8 *__smp_locks[], *__smp_locks_end[];
192
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800193/* Replace instructions with better alternatives for this CPU type.
194 This runs before SMP is initialized to avoid SMP problems with
195 self modifying code. This implies that assymetric systems where
196 APs have less capabilities than the boot processor are not handled.
197 Tough. Make sure you disable such features by hand. */
198
199void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
200{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800201 struct alt_instr *a;
Andi Kleenab144f52007-08-10 22:31:03 +0200202 char insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800203
204 DPRINTK("%s: alt table %p -> %p\n", __FUNCTION__, start, end);
205 for (a = start; a < end; a++) {
Andi Kleenab144f52007-08-10 22:31:03 +0200206 u8 *instr = a->instr;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800207 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200208 BUG_ON(a->instrlen > sizeof(insnbuf));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800209 if (!boot_cpu_has(a->cpuid))
210 continue;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200211#ifdef CONFIG_X86_64
212 /* vsyscall code is not mapped yet. resolve it manually. */
213 if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
214 instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
215 DPRINTK("%s: vsyscall fixup: %p => %p\n",
216 __FUNCTION__, a->instr, instr);
217 }
218#endif
Andi Kleenab144f52007-08-10 22:31:03 +0200219 memcpy(insnbuf, a->replacement, a->replacementlen);
220 add_nops(insnbuf + a->replacementlen,
221 a->instrlen - a->replacementlen);
222 text_poke(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800223 }
224}
225
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700226#ifdef CONFIG_SMP
227
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800228static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
229{
230 u8 **ptr;
231
232 for (ptr = start; ptr < end; ptr++) {
233 if (*ptr < text)
234 continue;
235 if (*ptr > text_end)
236 continue;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200237 text_poke(*ptr, ((unsigned char []){0xf0}), 1); /* add lock prefix */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800238 };
239}
240
241static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
242{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800243 u8 **ptr;
Andi Kleenab144f52007-08-10 22:31:03 +0200244 char insn[1];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800245
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200246 if (noreplace_smp)
247 return;
248
Andi Kleenab144f52007-08-10 22:31:03 +0200249 add_nops(insn, 1);
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;
Andi Kleenab144f52007-08-10 22:31:03 +0200255 text_poke(*ptr, insn, 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800256 };
257}
258
259struct smp_alt_module {
260 /* what is this ??? */
261 struct module *mod;
262 char *name;
263
264 /* ptrs to lock prefixes */
265 u8 **locks;
266 u8 **locks_end;
267
268 /* .text segment, needed to avoid patching init code ;) */
269 u8 *text;
270 u8 *text_end;
271
272 struct list_head next;
273};
274static LIST_HEAD(smp_alt_modules);
275static DEFINE_SPINLOCK(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100276static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800277
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800278void alternatives_smp_module_add(struct module *mod, char *name,
279 void *locks, void *locks_end,
280 void *text, void *text_end)
281{
282 struct smp_alt_module *smp;
283 unsigned long flags;
284
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200285 if (noreplace_smp)
286 return;
287
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800288 if (smp_alt_once) {
289 if (boot_cpu_has(X86_FEATURE_UP))
290 alternatives_smp_unlock(locks, locks_end,
291 text, text_end);
292 return;
293 }
294
295 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
296 if (NULL == smp)
297 return; /* we'll run the (safe but slow) SMP code then ... */
298
299 smp->mod = mod;
300 smp->name = name;
301 smp->locks = locks;
302 smp->locks_end = locks_end;
303 smp->text = text;
304 smp->text_end = text_end;
305 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
306 __FUNCTION__, smp->locks, smp->locks_end,
307 smp->text, smp->text_end, smp->name);
308
309 spin_lock_irqsave(&smp_alt, flags);
310 list_add_tail(&smp->next, &smp_alt_modules);
311 if (boot_cpu_has(X86_FEATURE_UP))
312 alternatives_smp_unlock(smp->locks, smp->locks_end,
313 smp->text, smp->text_end);
314 spin_unlock_irqrestore(&smp_alt, flags);
315}
316
317void alternatives_smp_module_del(struct module *mod)
318{
319 struct smp_alt_module *item;
320 unsigned long flags;
321
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200322 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800323 return;
324
325 spin_lock_irqsave(&smp_alt, flags);
326 list_for_each_entry(item, &smp_alt_modules, next) {
327 if (mod != item->mod)
328 continue;
329 list_del(&item->next);
330 spin_unlock_irqrestore(&smp_alt, flags);
331 DPRINTK("%s: %s\n", __FUNCTION__, item->name);
332 kfree(item);
333 return;
334 }
335 spin_unlock_irqrestore(&smp_alt, flags);
336}
337
338void alternatives_smp_switch(int smp)
339{
340 struct smp_alt_module *mod;
341 unsigned long flags;
342
Ingo Molnar3047e992006-07-03 00:24:57 -0700343#ifdef CONFIG_LOCKDEP
344 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100345 * Older binutils section handling bug prevented
346 * alternatives-replacement from working reliably.
347 *
348 * If this still occurs then you should see a hang
349 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700350 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100351 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700352#endif
353
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200354 if (noreplace_smp || smp_alt_once)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800355 return;
356 BUG_ON(!smp && (num_online_cpus() > 1));
357
358 spin_lock_irqsave(&smp_alt, flags);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100359
360 /*
361 * Avoid unnecessary switches because it forces JIT based VMs to
362 * throw away all cached translations, which can be quite costly.
363 */
364 if (smp == smp_mode) {
365 /* nothing */
366 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800367 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100368 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
369 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800370 list_for_each_entry(mod, &smp_alt_modules, next)
371 alternatives_smp_lock(mod->locks, mod->locks_end,
372 mod->text, mod->text_end);
373 } else {
374 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100375 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
376 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800377 list_for_each_entry(mod, &smp_alt_modules, next)
378 alternatives_smp_unlock(mod->locks, mod->locks_end,
379 mod->text, mod->text_end);
380 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100381 smp_mode = smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800382 spin_unlock_irqrestore(&smp_alt, flags);
383}
384
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700385#endif
386
Rusty Russell139ec7c2006-12-07 02:14:08 +0100387#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200388void apply_paravirt(struct paravirt_patch_site *start,
389 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100390{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200391 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200392 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100393
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200394 if (noreplace_paravirt)
395 return;
396
Rusty Russell139ec7c2006-12-07 02:14:08 +0100397 for (p = start; p < end; p++) {
398 unsigned int used;
399
Andi Kleenab144f52007-08-10 22:31:03 +0200400 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700401 /* prep the buffer with the original instructions */
402 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700403 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
404 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200405
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200406 BUG_ON(used > p->len);
407
Rusty Russell139ec7c2006-12-07 02:14:08 +0100408 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200409 add_nops(insnbuf + used, p->len - used);
410 text_poke(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100411 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100412}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200413extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100414 __stop_parainstructions[];
415#endif /* CONFIG_PARAVIRT */
416
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800417void __init alternative_instructions(void)
418{
Zachary Amsdene51959f2006-10-19 23:29:04 -0700419 unsigned long flags;
Zachary Amsdene51959f2006-10-19 23:29:04 -0700420
Andi Kleen8f4e9562007-07-22 11:12:32 +0200421 /* The patching is not fully atomic, so try to avoid local interruptions
422 that might execute the to be patched code.
423 Other CPUs are not running. */
424 stop_nmi();
Adrian Bunkd2d02512007-08-10 22:31:06 +0200425#ifdef CONFIG_X86_MCE
Andi Kleen8f4e9562007-07-22 11:12:32 +0200426 stop_mce();
427#endif
428
Zachary Amsdene51959f2006-10-19 23:29:04 -0700429 local_irq_save(flags);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800430 apply_alternatives(__alt_instructions, __alt_instructions_end);
431
432 /* switch to patch-once-at-boottime-only mode and free the
433 * tables in case we know the number of CPUs will never ever
434 * change */
435#ifdef CONFIG_HOTPLUG_CPU
436 if (num_possible_cpus() < 2)
437 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800438#endif
439
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700440#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800441 if (smp_alt_once) {
442 if (1 == num_possible_cpus()) {
443 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100444 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
445 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
446
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800447 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
448 _text, _etext);
449 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800450 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800451 alternatives_smp_module_add(NULL, "core kernel",
452 __smp_locks, __smp_locks_end,
453 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100454
455 /* Only switch to UP mode if we don't immediately boot others */
456 if (num_possible_cpus() == 1 || setup_max_cpus <= 1)
457 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800458 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700459#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200460 apply_paravirt(__parainstructions, __parainstructions_end);
Zachary Amsdene51959f2006-10-19 23:29:04 -0700461 local_irq_restore(flags);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200462
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200463 if (smp_alt_once)
464 free_init_pages("SMP alternatives",
465 (unsigned long)__smp_locks,
466 (unsigned long)__smp_locks_end);
467
Andi Kleen8f4e9562007-07-22 11:12:32 +0200468 restart_nmi();
Adrian Bunkd2d02512007-08-10 22:31:06 +0200469#ifdef CONFIG_X86_MCE
Andi Kleen8f4e9562007-07-22 11:12:32 +0200470 restart_mce();
471#endif
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800472}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200473
474/*
475 * Warning:
476 * When you use this code to patch more than one byte of an instruction
477 * you need to make sure that other CPUs cannot execute this code in parallel.
478 * Also no thread must be currently preempted in the middle of these instructions.
479 * And on the local CPU you need to be protected again NMI or MCE handlers
480 * seeing an inconsistent instruction while you patch.
481 */
Linus Torvalds602033e2007-07-26 12:07:21 -0700482void __kprobes text_poke(void *addr, unsigned char *opcode, int len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200483{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200484 memcpy(addr, opcode, len);
485 sync_core();
Andi Kleena534b672007-09-06 16:59:52 +0200486 /* Could also do a CLFLUSH here to speed up CPU recovery; but
487 that causes hangs on some VIA CPUs. */
Andi Kleen19d36cc2007-07-22 11:12:31 +0200488}