blob: f5765870257124108ff0ad237653bb306bf126bf [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>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050016#include <asm/tlbflush.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050017#include <asm/io.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050018#include <asm/fixmap.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080019
Andi Kleenab144f52007-08-10 22:31:03 +020020#define MAX_PATCH_LEN (255-1)
21
Jan Beulich09488162007-07-21 17:10:25 +020022#ifdef CONFIG_HOTPLUG_CPU
23static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080024
Gerd Hoffmannd167a512006-06-26 13:56:16 +020025static int __init bootonly(char *str)
26{
27 smp_alt_once = 1;
28 return 1;
29}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020030__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020031#else
32#define smp_alt_once 1
33#endif
34
35static int debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020036
Gerd Hoffmannd167a512006-06-26 13:56:16 +020037static int __init debug_alt(char *str)
38{
39 debug_alternative = 1;
40 return 1;
41}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020042__setup("debug-alternative", debug_alt);
43
Jan Beulich09488162007-07-21 17:10:25 +020044static int noreplace_smp;
45
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020046static int __init setup_noreplace_smp(char *str)
47{
48 noreplace_smp = 1;
49 return 1;
50}
51__setup("noreplace-smp", setup_noreplace_smp);
52
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020053#ifdef CONFIG_PARAVIRT
54static int noreplace_paravirt = 0;
55
56static int __init setup_noreplace_paravirt(char *str)
57{
58 noreplace_paravirt = 1;
59 return 1;
60}
61__setup("noreplace-paravirt", setup_noreplace_paravirt);
62#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020063
Gerd Hoffmannd167a512006-06-26 13:56:16 +020064#define DPRINTK(fmt, args...) if (debug_alternative) \
65 printk(KERN_DEBUG fmt, args)
66
67#ifdef GENERIC_NOP1
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080068/* Use inline assembly to define this because the nops are defined
69 as inline assembly strings in the include files and we cannot
70 get them easily into strings. */
Jan Beulich121d7bf2007-10-17 18:04:37 +020071asm("\t.section .rodata, \"a\"\nintelnops: "
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080072 GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040073 GENERIC_NOP7 GENERIC_NOP8
74 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020075extern const unsigned char intelnops[];
76static const unsigned char *const intel_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080077 NULL,
78 intelnops,
79 intelnops + 1,
80 intelnops + 1 + 2,
81 intelnops + 1 + 2 + 3,
82 intelnops + 1 + 2 + 3 + 4,
83 intelnops + 1 + 2 + 3 + 4 + 5,
84 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
85 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
86};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020087#endif
88
89#ifdef K8_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +020090asm("\t.section .rodata, \"a\"\nk8nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +020091 K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -040092 K8_NOP7 K8_NOP8
93 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +020094extern const unsigned char k8nops[];
95static const unsigned char *const k8_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080096 NULL,
97 k8nops,
98 k8nops + 1,
99 k8nops + 1 + 2,
100 k8nops + 1 + 2 + 3,
101 k8nops + 1 + 2 + 3 + 4,
102 k8nops + 1 + 2 + 3 + 4 + 5,
103 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
104 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
105};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200106#endif
107
108#ifdef K7_NOP1
Jan Beulich121d7bf2007-10-17 18:04:37 +0200109asm("\t.section .rodata, \"a\"\nk7nops: "
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200110 K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400111 K7_NOP7 K7_NOP8
112 "\t.previous");
Jan Beulich121d7bf2007-10-17 18:04:37 +0200113extern const unsigned char k7nops[];
114static const unsigned char *const k7_nops[ASM_NOP_MAX+1] = {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800115 NULL,
116 k7nops,
117 k7nops + 1,
118 k7nops + 1 + 2,
119 k7nops + 1 + 2 + 3,
120 k7nops + 1 + 2 + 3 + 4,
121 k7nops + 1 + 2 + 3 + 4 + 5,
122 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
123 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
124};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200125#endif
126
Jan Beulich32c464f2007-10-17 18:04:41 +0200127#ifdef P6_NOP1
128asm("\t.section .rodata, \"a\"\np6nops: "
129 P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
Steven Rostedtf4be31e2008-04-09 19:04:07 -0400130 P6_NOP7 P6_NOP8
131 "\t.previous");
Jan Beulich32c464f2007-10-17 18:04:41 +0200132extern const unsigned char p6nops[];
133static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
134 NULL,
135 p6nops,
136 p6nops + 1,
137 p6nops + 1 + 2,
138 p6nops + 1 + 2 + 3,
139 p6nops + 1 + 2 + 3 + 4,
140 p6nops + 1 + 2 + 3 + 4 + 5,
141 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
142 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
143};
144#endif
145
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200146#ifdef CONFIG_X86_64
147
148extern char __vsyscall_0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200149const unsigned char *const *find_nop_table(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200150{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700151 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
152 boot_cpu_has(X86_FEATURE_NOPL))
153 return p6_nops;
154 else
155 return k8_nops;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200156}
157
158#else /* CONFIG_X86_64 */
159
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200160const unsigned char *const *find_nop_table(void)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800161{
H. Peter Anvinf31d7312008-08-18 17:50:33 -0700162 if (boot_cpu_has(X86_FEATURE_K8))
163 return k8_nops;
164 else if (boot_cpu_has(X86_FEATURE_K7))
165 return k7_nops;
166 else if (boot_cpu_has(X86_FEATURE_NOPL))
167 return p6_nops;
168 else
169 return intel_nops;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800170}
171
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200172#endif /* CONFIG_X86_64 */
173
Andi Kleenab144f52007-08-10 22:31:03 +0200174/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500175void add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100176{
Jan Beulich121d7bf2007-10-17 18:04:37 +0200177 const unsigned char *const *noptable = find_nop_table();
Rusty Russell139ec7c2006-12-07 02:14:08 +0100178
179 while (len > 0) {
180 unsigned int noplen = len;
181 if (noplen > ASM_NOP_MAX)
182 noplen = ASM_NOP_MAX;
Andi Kleenab144f52007-08-10 22:31:03 +0200183 memcpy(insns, noptable[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100184 insns += noplen;
185 len -= noplen;
186 }
187}
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500188EXPORT_SYMBOL_GPL(add_nops);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100189
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
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800204 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800205 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",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800216 __func__, a->instr, instr);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200217 }
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);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500222 text_poke_early(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
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500232 mutex_lock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800233 for (ptr = start; ptr < end; ptr++) {
234 if (*ptr < text)
235 continue;
236 if (*ptr > text_end)
237 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400238 /* turn DS segment override prefix into lock prefix */
239 text_poke(*ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800240 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500241 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800242}
243
244static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
245{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800246 u8 **ptr;
247
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200248 if (noreplace_smp)
249 return;
250
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500251 mutex_lock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800252 for (ptr = start; ptr < end; ptr++) {
253 if (*ptr < text)
254 continue;
255 if (*ptr > text_end)
256 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400257 /* turn lock prefix into DS segment override prefix */
258 text_poke(*ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800259 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500260 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800261}
262
263struct smp_alt_module {
264 /* what is this ??? */
265 struct module *mod;
266 char *name;
267
268 /* ptrs to lock prefixes */
269 u8 **locks;
270 u8 **locks_end;
271
272 /* .text segment, needed to avoid patching init code ;) */
273 u8 *text;
274 u8 *text_end;
275
276 struct list_head next;
277};
278static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200279static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100280static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800281
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800282void alternatives_smp_module_add(struct module *mod, char *name,
283 void *locks, void *locks_end,
284 void *text, void *text_end)
285{
286 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800287
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200288 if (noreplace_smp)
289 return;
290
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800291 if (smp_alt_once) {
292 if (boot_cpu_has(X86_FEATURE_UP))
293 alternatives_smp_unlock(locks, locks_end,
294 text, text_end);
295 return;
296 }
297
298 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
299 if (NULL == smp)
300 return; /* we'll run the (safe but slow) SMP code then ... */
301
302 smp->mod = mod;
303 smp->name = name;
304 smp->locks = locks;
305 smp->locks_end = locks_end;
306 smp->text = text;
307 smp->text_end = text_end;
308 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800309 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800310 smp->text, smp->text_end, smp->name);
311
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200312 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800313 list_add_tail(&smp->next, &smp_alt_modules);
314 if (boot_cpu_has(X86_FEATURE_UP))
315 alternatives_smp_unlock(smp->locks, smp->locks_end,
316 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200317 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800318}
319
320void alternatives_smp_module_del(struct module *mod)
321{
322 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800323
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200324 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800325 return;
326
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200327 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800328 list_for_each_entry(item, &smp_alt_modules, next) {
329 if (mod != item->mod)
330 continue;
331 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200332 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800333 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800334 kfree(item);
335 return;
336 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200337 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800338}
339
340void alternatives_smp_switch(int smp)
341{
342 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800343
Ingo Molnar3047e992006-07-03 00:24:57 -0700344#ifdef CONFIG_LOCKDEP
345 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100346 * Older binutils section handling bug prevented
347 * alternatives-replacement from working reliably.
348 *
349 * If this still occurs then you should see a hang
350 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700351 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100352 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700353#endif
354
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200355 if (noreplace_smp || smp_alt_once)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800356 return;
357 BUG_ON(!smp && (num_online_cpus() > 1));
358
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200359 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100360
361 /*
362 * Avoid unnecessary switches because it forces JIT based VMs to
363 * throw away all cached translations, which can be quite costly.
364 */
365 if (smp == smp_mode) {
366 /* nothing */
367 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800368 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100369 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
370 clear_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_lock(mod->locks, mod->locks_end,
373 mod->text, mod->text_end);
374 } else {
375 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100376 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
377 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800378 list_for_each_entry(mod, &smp_alt_modules, next)
379 alternatives_smp_unlock(mod->locks, mod->locks_end,
380 mod->text, mod->text_end);
381 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100382 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200383 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800384}
385
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700386#endif
387
Rusty Russell139ec7c2006-12-07 02:14:08 +0100388#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200389void apply_paravirt(struct paravirt_patch_site *start,
390 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100391{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200392 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200393 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100394
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200395 if (noreplace_paravirt)
396 return;
397
Rusty Russell139ec7c2006-12-07 02:14:08 +0100398 for (p = start; p < end; p++) {
399 unsigned int used;
400
Andi Kleenab144f52007-08-10 22:31:03 +0200401 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700402 /* prep the buffer with the original instructions */
403 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700404 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
405 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200406
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200407 BUG_ON(used > p->len);
408
Rusty Russell139ec7c2006-12-07 02:14:08 +0100409 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200410 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500411 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100412 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100413}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200414extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100415 __stop_parainstructions[];
416#endif /* CONFIG_PARAVIRT */
417
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800418void __init alternative_instructions(void)
419{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200420 /* The patching is not fully atomic, so try to avoid local interruptions
421 that might execute the to be patched code.
422 Other CPUs are not running. */
423 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100424
425 /*
426 * Don't stop machine check exceptions while patching.
427 * MCEs only happen when something got corrupted and in this
428 * case we must do something about the corruption.
429 * Ignoring it is worse than a unlikely patching race.
430 * Also machine checks tend to be broadcast and if one CPU
431 * goes into machine check the others follow quickly, so we don't
432 * expect a machine check to cause undue problems during to code
433 * patching.
434 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200435
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800436 apply_alternatives(__alt_instructions, __alt_instructions_end);
437
438 /* switch to patch-once-at-boottime-only mode and free the
439 * tables in case we know the number of CPUs will never ever
440 * change */
441#ifdef CONFIG_HOTPLUG_CPU
442 if (num_possible_cpus() < 2)
443 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800444#endif
445
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700446#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800447 if (smp_alt_once) {
448 if (1 == num_possible_cpus()) {
449 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100450 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
451 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
452
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800453 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
454 _text, _etext);
455 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800456 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800457 alternatives_smp_module_add(NULL, "core kernel",
458 __smp_locks, __smp_locks_end,
459 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100460
461 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200462 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100463 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800464 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700465#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200466 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200467
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200468 if (smp_alt_once)
469 free_init_pages("SMP alternatives",
470 (unsigned long)__smp_locks,
471 (unsigned long)__smp_locks_end);
472
Andi Kleen8f4e9562007-07-22 11:12:32 +0200473 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800474}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200475
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500476/**
477 * text_poke_early - Update instructions on a live kernel at boot time
478 * @addr: address to modify
479 * @opcode: source of the copy
480 * @len: length to copy
481 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200482 * When you use this code to patch more than one byte of an instruction
483 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500484 * Also no thread must be currently preempted in the middle of these
485 * instructions. And on the local CPU you need to be protected again NMI or MCE
486 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200487 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500488void *text_poke_early(void *addr, const void *opcode, size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200489{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500490 unsigned long flags;
491 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200492 memcpy(addr, opcode, len);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500493 local_irq_restore(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200494 sync_core();
Andi Kleena534b672007-09-06 16:59:52 +0200495 /* Could also do a CLFLUSH here to speed up CPU recovery; but
496 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500497 return addr;
498}
499
500/**
501 * text_poke - Update instructions on a live kernel
502 * @addr: address to modify
503 * @opcode: source of the copy
504 * @len: length to copy
505 *
506 * Only atomic text poke/set should be allowed when not doing early patching.
507 * It means the size must be writable atomically and the address must be aligned
508 * in a way that permits an atomic write. It also makes sure we fit on a single
509 * page.
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500510 *
511 * Note: Must be called under text_mutex.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500512 */
513void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
514{
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500515 unsigned long flags;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500516 char *vaddr;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400517 struct page *pages[2];
518 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500519
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400520 if (!core_kernel_text((unsigned long)addr)) {
521 pages[0] = vmalloc_to_page(addr);
522 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400523 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400524 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200525 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400526 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500527 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400528 BUG_ON(!pages[0]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400529 local_irq_save(flags);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500530 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
531 if (pages[1])
532 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
533 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400534 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500535 clear_fixmap(FIX_TEXT_POKE0);
536 if (pages[1])
537 clear_fixmap(FIX_TEXT_POKE1);
538 local_flush_tlb();
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500539 sync_core();
540 /* Could also do a CLFLUSH here to speed up CPU recovery; but
541 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400542 for (i = 0; i < len; i++)
543 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400544 local_irq_restore(flags);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500545 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200546}