blob: 5b8394a3a6b25a20ac49d7c113098385f06e5748 [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>
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>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050014#include <asm/cacheflush.h>
15#include <asm/io.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080016
Andi Kleenab144f52007-08-10 22:31:03 +020017#define MAX_PATCH_LEN (255-1)
18
Jan Beulich09488162007-07-21 17:10:25 +020019#ifdef CONFIG_HOTPLUG_CPU
20static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080021
Gerd Hoffmannd167a512006-06-26 13:56:16 +020022static int __init bootonly(char *str)
23{
24 smp_alt_once = 1;
25 return 1;
26}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020027__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020028#else
29#define smp_alt_once 1
30#endif
31
32static int debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020033
Gerd Hoffmannd167a512006-06-26 13:56:16 +020034static int __init debug_alt(char *str)
35{
36 debug_alternative = 1;
37 return 1;
38}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020039__setup("debug-alternative", debug_alt);
40
Jan Beulich09488162007-07-21 17:10:25 +020041static int noreplace_smp;
42
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020043static int __init setup_noreplace_smp(char *str)
44{
45 noreplace_smp = 1;
46 return 1;
47}
48__setup("noreplace-smp", setup_noreplace_smp);
49
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020050#ifdef CONFIG_PARAVIRT
51static int noreplace_paravirt = 0;
52
53static int __init setup_noreplace_paravirt(char *str)
54{
55 noreplace_paravirt = 1;
56 return 1;
57}
58__setup("noreplace-paravirt", setup_noreplace_paravirt);
59#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020060
Gerd Hoffmannd167a512006-06-26 13:56:16 +020061#define DPRINTK(fmt, args...) if (debug_alternative) \
62 printk(KERN_DEBUG fmt, args)
63
64#ifdef GENERIC_NOP1
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080065/* Use inline assembly to define this because the nops are defined
66 as inline assembly strings in the include files and we cannot
67 get them easily into strings. */
Jan Beulich121d7bf2007-10-17 18:04:37 +020068asm("\t.section .rodata, \"a\"\nintelnops: "
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080069 GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040070 GENERIC_NOP7 GENERIC_NOP8
71 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020072extern const unsigned char intelnops[];
73static const unsigned char *const intel_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080074 NULL,
75 intelnops,
76 intelnops + 1,
77 intelnops + 1 + 2,
78 intelnops + 1 + 2 + 3,
79 intelnops + 1 + 2 + 3 + 4,
80 intelnops + 1 + 2 + 3 + 4 + 5,
81 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
82 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
83};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020084#endif
85
86#ifdef K8_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +020087asm("\t.section .rodata, \"a\"\nk8nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +020088 K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040089 K8_NOP7 K8_NOP8
90 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020091extern const unsigned char k8nops[];
92static const unsigned char *const k8_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080093 NULL,
94 k8nops,
95 k8nops + 1,
96 k8nops + 1 + 2,
97 k8nops + 1 + 2 + 3,
98 k8nops + 1 + 2 + 3 + 4,
99 k8nops + 1 + 2 + 3 + 4 + 5,
100 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
101 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
102};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200103#endif
104
105#ifdef K7_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +0200106asm("\t.section .rodata, \"a\"\nk7nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200107 K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400108 K7_NOP7 K7_NOP8
109 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +0200110extern const unsigned char k7nops[];
111static const unsigned char *const k7_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800112 NULL,
113 k7nops,
114 k7nops + 1,
115 k7nops + 1 + 2,
116 k7nops + 1 + 2 + 3,
117 k7nops + 1 + 2 + 3 + 4,
118 k7nops + 1 + 2 + 3 + 4 + 5,
119 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
120 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
121};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200122#endif
123
Jan Beulich32c464f2007-10-17 18:04:41 +0200124#ifdef P6_NOP1
125asm("\t.section .rodata, \"a\"\np6nops: "
126 P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400127 P6_NOP7 P6_NOP8
128 "\t.previous");
Jan Beulich32c464f2007-10-17 18:04:41 +0200129extern const unsigned char p6nops[];
130static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
131 NULL,
132 p6nops,
133 p6nops + 1,
134 p6nops + 1 + 2,
135 p6nops + 1 + 2 + 3,
136 p6nops + 1 + 2 + 3 + 4,
137 p6nops + 1 + 2 + 3 + 4 + 5,
138 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
139 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
140};
141#endif
142
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200143#ifdef CONFIG_X86_64
144
145extern char __vsyscall_0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200146const unsigned char *const *find_nop_table(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200147{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700148 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
149 boot_cpu_has(X86_FEATURE_NOPL))
150 return p6_nops;
151 else
152 return k8_nops;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200153}
154
155#else /* CONFIG_X86_64 */
156
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200157const unsigned char *const *find_nop_table(void)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800158{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700159 if (boot_cpu_has(X86_FEATURE_K8))
160 return k8_nops;
161 else if (boot_cpu_has(X86_FEATURE_K7))
162 return k7_nops;
163 else if (boot_cpu_has(X86_FEATURE_NOPL))
164 return p6_nops;
165 else
166 return intel_nops;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800167}
168
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200169#endif /* CONFIG_X86_64 */
170
Andi Kleenab144f52007-08-10 22:31:03 +0200171/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500172void add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100173{
Jan Beulich121d7bf2007-10-17 18:04:37 +0200174 const unsigned char *const *noptable = find_nop_table();
Rusty Russell139ec7c2006-12-07 02:14:08 +0100175
176 while (len > 0) {
177 unsigned int noplen = len;
178 if (noplen > ASM_NOP_MAX)
179 noplen = ASM_NOP_MAX;
Andi Kleenab144f52007-08-10 22:31:03 +0200180 memcpy(insns, noptable[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100181 insns += noplen;
182 len -= noplen;
183 }
184}
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500185EXPORT_SYMBOL_GPL(add_nops);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100186
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200187extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200188extern u8 *__smp_locks[], *__smp_locks_end[];
189
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800190/* Replace instructions with better alternatives for this CPU type.
191 This runs before SMP is initialized to avoid SMP problems with
192 self modifying code. This implies that assymetric systems where
193 APs have less capabilities than the boot processor are not handled.
194 Tough. Make sure you disable such features by hand. */
195
196void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
197{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800198 struct alt_instr *a;
Andi Kleenab144f52007-08-10 22:31:03 +0200199 char insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800200
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800201 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800202 for (a = start; a < end; a++) {
Andi Kleenab144f52007-08-10 22:31:03 +0200203 u8 *instr = a->instr;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800204 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200205 BUG_ON(a->instrlen > sizeof(insnbuf));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800206 if (!boot_cpu_has(a->cpuid))
207 continue;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200208#ifdef CONFIG_X86_64
209 /* vsyscall code is not mapped yet. resolve it manually. */
210 if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
211 instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
212 DPRINTK("%s: vsyscall fixup: %p => %p\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800213 __func__, a->instr, instr);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200214 }
215#endif
Andi Kleenab144f52007-08-10 22:31:03 +0200216 memcpy(insnbuf, a->replacement, a->replacementlen);
217 add_nops(insnbuf + a->replacementlen,
218 a->instrlen - a->replacementlen);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500219 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800220 }
221}
222
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700223#ifdef CONFIG_SMP
224
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800225static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
226{
227 u8 **ptr;
228
229 for (ptr = start; ptr < end; ptr++) {
230 if (*ptr < text)
231 continue;
232 if (*ptr > text_end)
233 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400234 /* turn DS segment override prefix into lock prefix */
235 text_poke(*ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800236 };
237}
238
239static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
240{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800241 u8 **ptr;
242
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200243 if (noreplace_smp)
244 return;
245
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800246 for (ptr = start; ptr < end; ptr++) {
247 if (*ptr < text)
248 continue;
249 if (*ptr > text_end)
250 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400251 /* turn lock prefix into DS segment override prefix */
252 text_poke(*ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800253 };
254}
255
256struct smp_alt_module {
257 /* what is this ??? */
258 struct module *mod;
259 char *name;
260
261 /* ptrs to lock prefixes */
262 u8 **locks;
263 u8 **locks_end;
264
265 /* .text segment, needed to avoid patching init code ;) */
266 u8 *text;
267 u8 *text_end;
268
269 struct list_head next;
270};
271static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200272static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100273static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800274
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800275void alternatives_smp_module_add(struct module *mod, char *name,
276 void *locks, void *locks_end,
277 void *text, void *text_end)
278{
279 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800280
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200281 if (noreplace_smp)
282 return;
283
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800284 if (smp_alt_once) {
285 if (boot_cpu_has(X86_FEATURE_UP))
286 alternatives_smp_unlock(locks, locks_end,
287 text, text_end);
288 return;
289 }
290
291 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
292 if (NULL == smp)
293 return; /* we'll run the (safe but slow) SMP code then ... */
294
295 smp->mod = mod;
296 smp->name = name;
297 smp->locks = locks;
298 smp->locks_end = locks_end;
299 smp->text = text;
300 smp->text_end = text_end;
301 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800302 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800303 smp->text, smp->text_end, smp->name);
304
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200305 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800306 list_add_tail(&smp->next, &smp_alt_modules);
307 if (boot_cpu_has(X86_FEATURE_UP))
308 alternatives_smp_unlock(smp->locks, smp->locks_end,
309 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200310 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800311}
312
313void alternatives_smp_module_del(struct module *mod)
314{
315 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800316
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200317 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800318 return;
319
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200320 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800321 list_for_each_entry(item, &smp_alt_modules, next) {
322 if (mod != item->mod)
323 continue;
324 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200325 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800326 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800327 kfree(item);
328 return;
329 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200330 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800331}
332
333void alternatives_smp_switch(int smp)
334{
335 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800336
Ingo Molnar3047e992006-07-03 00:24:57 -0700337#ifdef CONFIG_LOCKDEP
338 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100339 * Older binutils section handling bug prevented
340 * alternatives-replacement from working reliably.
341 *
342 * If this still occurs then you should see a hang
343 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700344 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100345 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700346#endif
347
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200348 if (noreplace_smp || smp_alt_once)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800349 return;
350 BUG_ON(!smp && (num_online_cpus() > 1));
351
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200352 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100353
354 /*
355 * Avoid unnecessary switches because it forces JIT based VMs to
356 * throw away all cached translations, which can be quite costly.
357 */
358 if (smp == smp_mode) {
359 /* nothing */
360 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800361 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100362 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
363 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800364 list_for_each_entry(mod, &smp_alt_modules, next)
365 alternatives_smp_lock(mod->locks, mod->locks_end,
366 mod->text, mod->text_end);
367 } else {
368 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100369 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
370 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800371 list_for_each_entry(mod, &smp_alt_modules, next)
372 alternatives_smp_unlock(mod->locks, mod->locks_end,
373 mod->text, mod->text_end);
374 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100375 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200376 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800377}
378
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700379#endif
380
Rusty Russell139ec7c2006-12-07 02:14:08 +0100381#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200382void apply_paravirt(struct paravirt_patch_site *start,
383 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100384{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200385 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200386 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100387
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200388 if (noreplace_paravirt)
389 return;
390
Rusty Russell139ec7c2006-12-07 02:14:08 +0100391 for (p = start; p < end; p++) {
392 unsigned int used;
393
Andi Kleenab144f52007-08-10 22:31:03 +0200394 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700395 /* prep the buffer with the original instructions */
396 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700397 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
398 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200399
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200400 BUG_ON(used > p->len);
401
Rusty Russell139ec7c2006-12-07 02:14:08 +0100402 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200403 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500404 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100405 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100406}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200407extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100408 __stop_parainstructions[];
409#endif /* CONFIG_PARAVIRT */
410
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800411void __init alternative_instructions(void)
412{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200413 /* The patching is not fully atomic, so try to avoid local interruptions
414 that might execute the to be patched code.
415 Other CPUs are not running. */
416 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100417
418 /*
419 * Don't stop machine check exceptions while patching.
420 * MCEs only happen when something got corrupted and in this
421 * case we must do something about the corruption.
422 * Ignoring it is worse than a unlikely patching race.
423 * Also machine checks tend to be broadcast and if one CPU
424 * goes into machine check the others follow quickly, so we don't
425 * expect a machine check to cause undue problems during to code
426 * patching.
427 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200428
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800429 apply_alternatives(__alt_instructions, __alt_instructions_end);
430
431 /* switch to patch-once-at-boottime-only mode and free the
432 * tables in case we know the number of CPUs will never ever
433 * change */
434#ifdef CONFIG_HOTPLUG_CPU
435 if (num_possible_cpus() < 2)
436 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800437#endif
438
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700439#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800440 if (smp_alt_once) {
441 if (1 == num_possible_cpus()) {
442 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100443 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
444 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
445
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800446 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
447 _text, _etext);
448 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800449 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800450 alternatives_smp_module_add(NULL, "core kernel",
451 __smp_locks, __smp_locks_end,
452 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100453
454 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200455 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100456 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800457 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700458#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200459 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200460
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200461 if (smp_alt_once)
462 free_init_pages("SMP alternatives",
463 (unsigned long)__smp_locks,
464 (unsigned long)__smp_locks_end);
465
Andi Kleen8f4e9562007-07-22 11:12:32 +0200466 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800467}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200468
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500469/**
470 * text_poke_early - Update instructions on a live kernel at boot time
471 * @addr: address to modify
472 * @opcode: source of the copy
473 * @len: length to copy
474 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200475 * When you use this code to patch more than one byte of an instruction
476 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500477 * Also no thread must be currently preempted in the middle of these
478 * instructions. And on the local CPU you need to be protected again NMI or MCE
479 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200480 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500481void *text_poke_early(void *addr, const void *opcode, size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200482{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500483 unsigned long flags;
484 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200485 memcpy(addr, opcode, len);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500486 local_irq_restore(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200487 sync_core();
Andi Kleena534b672007-09-06 16:59:52 +0200488 /* Could also do a CLFLUSH here to speed up CPU recovery; but
489 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500490 return addr;
491}
492
493/**
494 * text_poke - Update instructions on a live kernel
495 * @addr: address to modify
496 * @opcode: source of the copy
497 * @len: length to copy
498 *
499 * Only atomic text poke/set should be allowed when not doing early patching.
500 * It means the size must be writable atomically and the address must be aligned
501 * in a way that permits an atomic write. It also makes sure we fit on a single
502 * page.
503 */
504void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
505{
506 unsigned long flags;
507 char *vaddr;
508 int nr_pages = 2;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400509 struct page *pages[2];
510 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500511
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400512 if (!core_kernel_text((unsigned long)addr)) {
513 pages[0] = vmalloc_to_page(addr);
514 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400515 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400516 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200517 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400518 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500519 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400520 BUG_ON(!pages[0]);
521 if (!pages[1])
522 nr_pages = 1;
523 vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
524 BUG_ON(!vaddr);
525 local_irq_save(flags);
526 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
527 local_irq_restore(flags);
528 vunmap(vaddr);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500529 sync_core();
530 /* Could also do a CLFLUSH here to speed up CPU recovery; but
531 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400532 for (i = 0; i < len; i++)
533 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500534 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200535}