blob: 206ea2ca63cc5beb221c238ff81172244de262b9 [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>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080011
Jan Beulich09488162007-07-21 17:10:25 +020012#ifdef CONFIG_HOTPLUG_CPU
13static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080014
Gerd Hoffmannd167a512006-06-26 13:56:16 +020015static int __init bootonly(char *str)
16{
17 smp_alt_once = 1;
18 return 1;
19}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020020__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020021#else
22#define smp_alt_once 1
23#endif
24
25static int debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020026
Gerd Hoffmannd167a512006-06-26 13:56:16 +020027static int __init debug_alt(char *str)
28{
29 debug_alternative = 1;
30 return 1;
31}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020032__setup("debug-alternative", debug_alt);
33
Jan Beulich09488162007-07-21 17:10:25 +020034static int noreplace_smp;
35
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020036static int __init setup_noreplace_smp(char *str)
37{
38 noreplace_smp = 1;
39 return 1;
40}
41__setup("noreplace-smp", setup_noreplace_smp);
42
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020043#ifdef CONFIG_PARAVIRT
44static int noreplace_paravirt = 0;
45
46static int __init setup_noreplace_paravirt(char *str)
47{
48 noreplace_paravirt = 1;
49 return 1;
50}
51__setup("noreplace-paravirt", setup_noreplace_paravirt);
52#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020053
Gerd Hoffmannd167a512006-06-26 13:56:16 +020054#define DPRINTK(fmt, args...) if (debug_alternative) \
55 printk(KERN_DEBUG fmt, args)
56
57#ifdef GENERIC_NOP1
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080058/* Use inline assembly to define this because the nops are defined
59 as inline assembly strings in the include files and we cannot
60 get them easily into strings. */
61asm("\t.data\nintelnops: "
62 GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
63 GENERIC_NOP7 GENERIC_NOP8);
Gerd Hoffmannd167a512006-06-26 13:56:16 +020064extern unsigned char intelnops[];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080065static unsigned char *intel_nops[ASM_NOP_MAX+1] = {
66 NULL,
67 intelnops,
68 intelnops + 1,
69 intelnops + 1 + 2,
70 intelnops + 1 + 2 + 3,
71 intelnops + 1 + 2 + 3 + 4,
72 intelnops + 1 + 2 + 3 + 4 + 5,
73 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
74 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
75};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020076#endif
77
78#ifdef K8_NOP1
79asm("\t.data\nk8nops: "
80 K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
81 K8_NOP7 K8_NOP8);
82extern unsigned char k8nops[];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080083static unsigned char *k8_nops[ASM_NOP_MAX+1] = {
84 NULL,
85 k8nops,
86 k8nops + 1,
87 k8nops + 1 + 2,
88 k8nops + 1 + 2 + 3,
89 k8nops + 1 + 2 + 3 + 4,
90 k8nops + 1 + 2 + 3 + 4 + 5,
91 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
92 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
93};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020094#endif
95
96#ifdef K7_NOP1
97asm("\t.data\nk7nops: "
98 K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
99 K7_NOP7 K7_NOP8);
100extern unsigned char k7nops[];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800101static unsigned char *k7_nops[ASM_NOP_MAX+1] = {
102 NULL,
103 k7nops,
104 k7nops + 1,
105 k7nops + 1 + 2,
106 k7nops + 1 + 2 + 3,
107 k7nops + 1 + 2 + 3 + 4,
108 k7nops + 1 + 2 + 3 + 4 + 5,
109 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
110 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
111};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200112#endif
113
114#ifdef CONFIG_X86_64
115
116extern char __vsyscall_0;
117static inline unsigned char** find_nop_table(void)
118{
119 return k8_nops;
120}
121
122#else /* CONFIG_X86_64 */
123
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800124static struct nop {
125 int cpuid;
126 unsigned char **noptable;
127} noptypes[] = {
128 { X86_FEATURE_K8, k8_nops },
129 { X86_FEATURE_K7, k7_nops },
130 { -1, NULL }
131};
132
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800133static unsigned char** find_nop_table(void)
134{
135 unsigned char **noptable = intel_nops;
136 int i;
137
138 for (i = 0; noptypes[i].cpuid >= 0; i++) {
139 if (boot_cpu_has(noptypes[i].cpuid)) {
140 noptable = noptypes[i].noptable;
141 break;
142 }
143 }
144 return noptable;
145}
146
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200147#endif /* CONFIG_X86_64 */
148
Rusty Russell139ec7c2006-12-07 02:14:08 +0100149static void nop_out(void *insns, unsigned int len)
150{
151 unsigned char **noptable = find_nop_table();
152
153 while (len > 0) {
154 unsigned int noplen = len;
155 if (noplen > ASM_NOP_MAX)
156 noplen = ASM_NOP_MAX;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200157 text_poke(insns, noptable[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100158 insns += noplen;
159 len -= noplen;
160 }
161}
162
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200163extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200164extern u8 *__smp_locks[], *__smp_locks_end[];
165
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800166/* Replace instructions with better alternatives for this CPU type.
167 This runs before SMP is initialized to avoid SMP problems with
168 self modifying code. This implies that assymetric systems where
169 APs have less capabilities than the boot processor are not handled.
170 Tough. Make sure you disable such features by hand. */
171
172void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
173{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800174 struct alt_instr *a;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200175 u8 *instr;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100176 int diff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800177
178 DPRINTK("%s: alt table %p -> %p\n", __FUNCTION__, start, end);
179 for (a = start; a < end; a++) {
180 BUG_ON(a->replacementlen > a->instrlen);
181 if (!boot_cpu_has(a->cpuid))
182 continue;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200183 instr = a->instr;
184#ifdef CONFIG_X86_64
185 /* vsyscall code is not mapped yet. resolve it manually. */
186 if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
187 instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
188 DPRINTK("%s: vsyscall fixup: %p => %p\n",
189 __FUNCTION__, a->instr, instr);
190 }
191#endif
192 memcpy(instr, a->replacement, a->replacementlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800193 diff = a->instrlen - a->replacementlen;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100194 nop_out(instr + a->replacementlen, diff);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800195 }
196}
197
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700198#ifdef CONFIG_SMP
199
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800200static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
201{
202 u8 **ptr;
203
204 for (ptr = start; ptr < end; ptr++) {
205 if (*ptr < text)
206 continue;
207 if (*ptr > text_end)
208 continue;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200209 text_poke(*ptr, ((unsigned char []){0xf0}), 1); /* add lock prefix */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800210 };
211}
212
213static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
214{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800215 u8 **ptr;
216
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200217 if (noreplace_smp)
218 return;
219
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800220 for (ptr = start; ptr < end; ptr++) {
221 if (*ptr < text)
222 continue;
223 if (*ptr > text_end)
224 continue;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100225 nop_out(*ptr, 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800226 };
227}
228
229struct smp_alt_module {
230 /* what is this ??? */
231 struct module *mod;
232 char *name;
233
234 /* ptrs to lock prefixes */
235 u8 **locks;
236 u8 **locks_end;
237
238 /* .text segment, needed to avoid patching init code ;) */
239 u8 *text;
240 u8 *text_end;
241
242 struct list_head next;
243};
244static LIST_HEAD(smp_alt_modules);
245static DEFINE_SPINLOCK(smp_alt);
246
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800247void alternatives_smp_module_add(struct module *mod, char *name,
248 void *locks, void *locks_end,
249 void *text, void *text_end)
250{
251 struct smp_alt_module *smp;
252 unsigned long flags;
253
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200254 if (noreplace_smp)
255 return;
256
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800257 if (smp_alt_once) {
258 if (boot_cpu_has(X86_FEATURE_UP))
259 alternatives_smp_unlock(locks, locks_end,
260 text, text_end);
261 return;
262 }
263
264 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
265 if (NULL == smp)
266 return; /* we'll run the (safe but slow) SMP code then ... */
267
268 smp->mod = mod;
269 smp->name = name;
270 smp->locks = locks;
271 smp->locks_end = locks_end;
272 smp->text = text;
273 smp->text_end = text_end;
274 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
275 __FUNCTION__, smp->locks, smp->locks_end,
276 smp->text, smp->text_end, smp->name);
277
278 spin_lock_irqsave(&smp_alt, flags);
279 list_add_tail(&smp->next, &smp_alt_modules);
280 if (boot_cpu_has(X86_FEATURE_UP))
281 alternatives_smp_unlock(smp->locks, smp->locks_end,
282 smp->text, smp->text_end);
283 spin_unlock_irqrestore(&smp_alt, flags);
284}
285
286void alternatives_smp_module_del(struct module *mod)
287{
288 struct smp_alt_module *item;
289 unsigned long flags;
290
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200291 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800292 return;
293
294 spin_lock_irqsave(&smp_alt, flags);
295 list_for_each_entry(item, &smp_alt_modules, next) {
296 if (mod != item->mod)
297 continue;
298 list_del(&item->next);
299 spin_unlock_irqrestore(&smp_alt, flags);
300 DPRINTK("%s: %s\n", __FUNCTION__, item->name);
301 kfree(item);
302 return;
303 }
304 spin_unlock_irqrestore(&smp_alt, flags);
305}
306
307void alternatives_smp_switch(int smp)
308{
309 struct smp_alt_module *mod;
310 unsigned long flags;
311
Ingo Molnar3047e992006-07-03 00:24:57 -0700312#ifdef CONFIG_LOCKDEP
313 /*
314 * A not yet fixed binutils section handling bug prevents
315 * alternatives-replacement from working reliably, so turn
316 * it off:
317 */
318 printk("lockdep: not fixing up alternatives.\n");
319 return;
320#endif
321
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200322 if (noreplace_smp || smp_alt_once)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800323 return;
324 BUG_ON(!smp && (num_online_cpus() > 1));
325
326 spin_lock_irqsave(&smp_alt, flags);
327 if (smp) {
328 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
329 clear_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
330 clear_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800331 list_for_each_entry(mod, &smp_alt_modules, next)
332 alternatives_smp_lock(mod->locks, mod->locks_end,
333 mod->text, mod->text_end);
334 } else {
335 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
336 set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
337 set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800338 list_for_each_entry(mod, &smp_alt_modules, next)
339 alternatives_smp_unlock(mod->locks, mod->locks_end,
340 mod->text, mod->text_end);
341 }
342 spin_unlock_irqrestore(&smp_alt, flags);
343}
344
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700345#endif
346
Rusty Russell139ec7c2006-12-07 02:14:08 +0100347#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200348void apply_paravirt(struct paravirt_patch_site *start,
349 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100350{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200351 struct paravirt_patch_site *p;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100352
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200353 if (noreplace_paravirt)
354 return;
355
Rusty Russell139ec7c2006-12-07 02:14:08 +0100356 for (p = start; p < end; p++) {
357 unsigned int used;
358
359 used = paravirt_ops.patch(p->instrtype, p->clobbers, p->instr,
360 p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200361
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200362 BUG_ON(used > p->len);
363
Rusty Russell139ec7c2006-12-07 02:14:08 +0100364 /* Pad the rest with nops */
365 nop_out(p->instr + used, p->len - used);
366 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100367}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200368extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100369 __stop_parainstructions[];
370#endif /* CONFIG_PARAVIRT */
371
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800372void __init alternative_instructions(void)
373{
Zachary Amsdene51959f2006-10-19 23:29:04 -0700374 unsigned long flags;
Zachary Amsdene51959f2006-10-19 23:29:04 -0700375
376 local_irq_save(flags);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800377 apply_alternatives(__alt_instructions, __alt_instructions_end);
378
379 /* switch to patch-once-at-boottime-only mode and free the
380 * tables in case we know the number of CPUs will never ever
381 * change */
382#ifdef CONFIG_HOTPLUG_CPU
383 if (num_possible_cpus() < 2)
384 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800385#endif
386
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700387#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800388 if (smp_alt_once) {
389 if (1 == num_possible_cpus()) {
390 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
391 set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
392 set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800393 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
394 _text, _etext);
395 }
396 free_init_pages("SMP alternatives",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700397 (unsigned long)__smp_locks,
398 (unsigned long)__smp_locks_end);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800399 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800400 alternatives_smp_module_add(NULL, "core kernel",
401 __smp_locks, __smp_locks_end,
402 _text, _etext);
403 alternatives_smp_switch(0);
404 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700405#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200406 apply_paravirt(__parainstructions, __parainstructions_end);
Zachary Amsdene51959f2006-10-19 23:29:04 -0700407 local_irq_restore(flags);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800408}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200409
410/*
411 * Warning:
412 * When you use this code to patch more than one byte of an instruction
413 * you need to make sure that other CPUs cannot execute this code in parallel.
414 * Also no thread must be currently preempted in the middle of these instructions.
415 * And on the local CPU you need to be protected again NMI or MCE handlers
416 * seeing an inconsistent instruction while you patch.
417 */
418void __kprobes text_poke(void *oaddr, unsigned char *opcode, int len)
419{
420 u8 *addr = oaddr;
421 if (!pte_write(*lookup_address((unsigned long)addr))) {
422 struct page *p[2] = { virt_to_page(addr), virt_to_page(addr+PAGE_SIZE) };
423 addr = vmap(p, 2, VM_MAP, PAGE_KERNEL);
424 if (!addr)
425 return;
426 addr += ((unsigned long)oaddr) % PAGE_SIZE;
427 }
428 memcpy(addr, opcode, len);
429 sync_core();
430 /* Not strictly needed, but can speed CPU recovery up. Ignore cross cacheline
431 case. */
432 if (cpu_has_clflush)
433 asm("clflush (%0) " :: "r" (oaddr) : "memory");
434 if (addr != oaddr)
435 vunmap(addr);
436}