x86/paravirt: Remove 'noreplace-paravirt' cmdline option
[pandora-kernel.git] / arch / x86 / kernel / alternative.c
1 #include <linux/module.h>
2 #include <linux/sched.h>
3 #include <linux/mutex.h>
4 #include <linux/list.h>
5 #include <linux/stringify.h>
6 #include <linux/kprobes.h>
7 #include <linux/mm.h>
8 #include <linux/vmalloc.h>
9 #include <linux/memory.h>
10 #include <linux/stop_machine.h>
11 #include <linux/slab.h>
12 #include <asm/alternative.h>
13 #include <asm/sections.h>
14 #include <asm/pgtable.h>
15 #include <asm/mce.h>
16 #include <asm/nmi.h>
17 #include <asm/cacheflush.h>
18 #include <asm/tlbflush.h>
19 #include <asm/io.h>
20 #include <asm/fixmap.h>
21
22 #define MAX_PATCH_LEN (255-1)
23
24 static int __initdata_or_module debug_alternative;
25
26 static int __init debug_alt(char *str)
27 {
28         debug_alternative = 1;
29         return 1;
30 }
31 __setup("debug-alternative", debug_alt);
32
33 static int noreplace_smp;
34
35 static int __init setup_noreplace_smp(char *str)
36 {
37         noreplace_smp = 1;
38         return 1;
39 }
40 __setup("noreplace-smp", setup_noreplace_smp);
41
42 #define DPRINTK(fmt, args...)                                           \
43 do {                                                                    \
44         if (debug_alternative)                                          \
45                 printk(KERN_DEBUG "%s: " fmt "\n", __func__, ##args);   \
46 } while (0)
47
48 #define DUMP_BYTES(buf, len, fmt, args...)                              \
49 do {                                                                    \
50         if (unlikely(debug_alternative)) {                              \
51                 int j;                                                  \
52                                                                         \
53                 if (!(len))                                             \
54                         break;                                          \
55                                                                         \
56                 printk(KERN_DEBUG fmt, ##args);                         \
57                 for (j = 0; j < (len) - 1; j++)                         \
58                         printk(KERN_CONT "%02hhx ", buf[j]);            \
59                 printk(KERN_CONT "%02hhx\n", buf[j]);                   \
60         }                                                               \
61 } while (0)
62
63 /*
64  * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
65  * that correspond to that nop. Getting from one nop to the next, we
66  * add to the array the offset that is equal to the sum of all sizes of
67  * nops preceding the one we are after.
68  *
69  * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
70  * nice symmetry of sizes of the previous nops.
71  */
72 #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
73 static const unsigned char intelnops[] =
74 {
75         GENERIC_NOP1,
76         GENERIC_NOP2,
77         GENERIC_NOP3,
78         GENERIC_NOP4,
79         GENERIC_NOP5,
80         GENERIC_NOP6,
81         GENERIC_NOP7,
82         GENERIC_NOP8,
83         GENERIC_NOP5_ATOMIC
84 };
85 static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
86 {
87         NULL,
88         intelnops,
89         intelnops + 1,
90         intelnops + 1 + 2,
91         intelnops + 1 + 2 + 3,
92         intelnops + 1 + 2 + 3 + 4,
93         intelnops + 1 + 2 + 3 + 4 + 5,
94         intelnops + 1 + 2 + 3 + 4 + 5 + 6,
95         intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
96         intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
97 };
98 #endif
99
100 #ifdef K8_NOP1
101 static const unsigned char k8nops[] =
102 {
103         K8_NOP1,
104         K8_NOP2,
105         K8_NOP3,
106         K8_NOP4,
107         K8_NOP5,
108         K8_NOP6,
109         K8_NOP7,
110         K8_NOP8,
111         K8_NOP5_ATOMIC
112 };
113 static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
114 {
115         NULL,
116         k8nops,
117         k8nops + 1,
118         k8nops + 1 + 2,
119         k8nops + 1 + 2 + 3,
120         k8nops + 1 + 2 + 3 + 4,
121         k8nops + 1 + 2 + 3 + 4 + 5,
122         k8nops + 1 + 2 + 3 + 4 + 5 + 6,
123         k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
124         k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
125 };
126 #endif
127
128 #if defined(K7_NOP1) && !defined(CONFIG_X86_64)
129 static const unsigned char k7nops[] =
130 {
131         K7_NOP1,
132         K7_NOP2,
133         K7_NOP3,
134         K7_NOP4,
135         K7_NOP5,
136         K7_NOP6,
137         K7_NOP7,
138         K7_NOP8,
139         K7_NOP5_ATOMIC
140 };
141 static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
142 {
143         NULL,
144         k7nops,
145         k7nops + 1,
146         k7nops + 1 + 2,
147         k7nops + 1 + 2 + 3,
148         k7nops + 1 + 2 + 3 + 4,
149         k7nops + 1 + 2 + 3 + 4 + 5,
150         k7nops + 1 + 2 + 3 + 4 + 5 + 6,
151         k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
152         k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
153 };
154 #endif
155
156 #ifdef P6_NOP1
157 static const unsigned char p6nops[] =
158 {
159         P6_NOP1,
160         P6_NOP2,
161         P6_NOP3,
162         P6_NOP4,
163         P6_NOP5,
164         P6_NOP6,
165         P6_NOP7,
166         P6_NOP8,
167         P6_NOP5_ATOMIC
168 };
169 static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
170 {
171         NULL,
172         p6nops,
173         p6nops + 1,
174         p6nops + 1 + 2,
175         p6nops + 1 + 2 + 3,
176         p6nops + 1 + 2 + 3 + 4,
177         p6nops + 1 + 2 + 3 + 4 + 5,
178         p6nops + 1 + 2 + 3 + 4 + 5 + 6,
179         p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
180         p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
181 };
182 #endif
183
184 /* Initialize these to a safe default */
185 #ifdef CONFIG_X86_64
186 const unsigned char * const *ideal_nops = p6_nops;
187 #else
188 const unsigned char * const *ideal_nops = intel_nops;
189 #endif
190
191 void __init arch_init_ideal_nops(void)
192 {
193         switch (boot_cpu_data.x86_vendor) {
194         case X86_VENDOR_INTEL:
195                 /*
196                  * Due to a decoder implementation quirk, some
197                  * specific Intel CPUs actually perform better with
198                  * the "k8_nops" than with the SDM-recommended NOPs.
199                  */
200                 if (boot_cpu_data.x86 == 6 &&
201                     boot_cpu_data.x86_model >= 0x0f &&
202                     boot_cpu_data.x86_model != 0x1c &&
203                     boot_cpu_data.x86_model != 0x26 &&
204                     boot_cpu_data.x86_model != 0x27 &&
205                     boot_cpu_data.x86_model < 0x30) {
206                         ideal_nops = k8_nops;
207                 } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
208                            ideal_nops = p6_nops;
209                 } else {
210 #ifdef CONFIG_X86_64
211                         ideal_nops = k8_nops;
212 #else
213                         ideal_nops = intel_nops;
214 #endif
215                 }
216                 break;
217         default:
218 #ifdef CONFIG_X86_64
219                 ideal_nops = k8_nops;
220 #else
221                 if (boot_cpu_has(X86_FEATURE_K8))
222                         ideal_nops = k8_nops;
223                 else if (boot_cpu_has(X86_FEATURE_K7))
224                         ideal_nops = k7_nops;
225                 else
226                         ideal_nops = intel_nops;
227 #endif
228         }
229 }
230
231 /* Use this to add nops to a buffer, then text_poke the whole buffer. */
232 static void __init_or_module add_nops(void *insns, unsigned int len)
233 {
234         while (len > 0) {
235                 unsigned int noplen = len;
236                 if (noplen > ASM_NOP_MAX)
237                         noplen = ASM_NOP_MAX;
238                 memcpy(insns, ideal_nops[noplen], noplen);
239                 insns += noplen;
240                 len -= noplen;
241         }
242 }
243
244 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
245 extern s32 __smp_locks[], __smp_locks_end[];
246 void *text_poke_early(void *addr, const void *opcode, size_t len);
247
248 /*
249  * Are we looking at a near JMP with a 1 or 4-byte displacement.
250  */
251 static inline bool is_jmp(const u8 opcode)
252 {
253         return opcode == 0xeb || opcode == 0xe9;
254 }
255
256 static void __init_or_module
257 recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insnbuf)
258 {
259         u8 *next_rip, *tgt_rip;
260         s32 n_dspl, o_dspl;
261         int repl_len;
262
263         if (a->replacementlen != 5)
264                 return;
265
266         o_dspl = *(s32 *)(insnbuf + 1);
267
268         /* next_rip of the replacement JMP */
269         next_rip = repl_insn + a->replacementlen;
270         /* target rip of the replacement JMP */
271         tgt_rip  = next_rip + o_dspl;
272         n_dspl = tgt_rip - orig_insn;
273
274         DPRINTK("target RIP: %p, new_displ: 0x%x", tgt_rip, n_dspl);
275
276         if (tgt_rip - orig_insn >= 0) {
277                 if (n_dspl - 2 <= 127)
278                         goto two_byte_jmp;
279                 else
280                         goto five_byte_jmp;
281         /* negative offset */
282         } else {
283                 if (((n_dspl - 2) & 0xff) == (n_dspl - 2))
284                         goto two_byte_jmp;
285                 else
286                         goto five_byte_jmp;
287         }
288
289 two_byte_jmp:
290         n_dspl -= 2;
291
292         insnbuf[0] = 0xeb;
293         insnbuf[1] = (s8)n_dspl;
294         add_nops(insnbuf + 2, 3);
295
296         repl_len = 2;
297         goto done;
298
299 five_byte_jmp:
300         n_dspl -= 5;
301
302         insnbuf[0] = 0xe9;
303         *(s32 *)&insnbuf[1] = n_dspl;
304
305         repl_len = 5;
306
307 done:
308
309         DPRINTK("final displ: 0x%08x, JMP 0x%lx",
310                 n_dspl, (unsigned long)orig_insn + n_dspl + repl_len);
311 }
312
313 static void __init_or_module optimize_nops(struct alt_instr *a, u8 *instr)
314 {
315         unsigned long flags;
316         int i;
317
318         for (i = 0; i < a->padlen; i++) {
319                 if (instr[i] != 0x90)
320                         return;
321         }
322
323         local_irq_save(flags);
324         add_nops(instr + (a->instrlen - a->padlen), a->padlen);
325         sync_core();
326         local_irq_restore(flags);
327
328         DUMP_BYTES(instr, a->instrlen, "%p: [%d:%d) optimized NOPs: ",
329                    instr, a->instrlen - a->padlen, a->padlen);
330 }
331
332 /*
333  * Replace instructions with better alternatives for this CPU type. This runs
334  * before SMP is initialized to avoid SMP problems with self modifying code.
335  * This implies that asymmetric systems where APs have less capabilities than
336  * the boot processor are not handled. Tough. Make sure you disable such
337  * features by hand.
338  */
339 void __init_or_module apply_alternatives(struct alt_instr *start,
340                                          struct alt_instr *end)
341 {
342         struct alt_instr *a;
343         u8 *instr, *replacement;
344         u8 insnbuf[MAX_PATCH_LEN];
345
346         DPRINTK("alt table %p -> %p", start, end);
347         /*
348          * The scan order should be from start to end. A later scanned
349          * alternative code can overwrite previously scanned alternative code.
350          * Some kernel functions (e.g. memcpy, memset, etc) use this order to
351          * patch code.
352          *
353          * So be careful if you want to change the scan order to any other
354          * order.
355          */
356         for (a = start; a < end; a++) {
357                 int insnbuf_sz = 0;
358
359                 instr = (u8 *)&a->instr_offset + a->instr_offset;
360                 replacement = (u8 *)&a->repl_offset + a->repl_offset;
361                 BUG_ON(a->instrlen > sizeof(insnbuf));
362                 BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
363                 if (!boot_cpu_has(a->cpuid)) {
364                         if (a->padlen > 1)
365                                 optimize_nops(a, instr);
366
367                         continue;
368                 }
369
370                 DPRINTK("feat: %d*32+%d, old: (%p, len: %d), repl: (%p, len: %d), pad: %d",
371                         a->cpuid >> 5,
372                         a->cpuid & 0x1f,
373                         instr, a->instrlen,
374                         replacement, a->replacementlen, a->padlen);
375
376                 DUMP_BYTES(instr, a->instrlen, "%p: old_insn: ", instr);
377                 DUMP_BYTES(replacement, a->replacementlen, "%p: rpl_insn: ", replacement);
378
379                 memcpy(insnbuf, replacement, a->replacementlen);
380                 insnbuf_sz = a->replacementlen;
381
382                 /* 0xe8 is a relative jump; fix the offset. */
383                 if (*insnbuf == 0xe8 && a->replacementlen == 5) {
384                         *(s32 *)(insnbuf + 1) += replacement - instr;
385                         DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
386                                 *(s32 *)(insnbuf + 1),
387                                 (unsigned long)instr + *(s32 *)(insnbuf + 1) + 5);
388                 }
389
390                 if (a->replacementlen && is_jmp(replacement[0]))
391                         recompute_jump(a, instr, replacement, insnbuf);
392
393                 if (a->instrlen > a->replacementlen) {
394                         add_nops(insnbuf + a->replacementlen,
395                                  a->instrlen - a->replacementlen);
396                         insnbuf_sz += a->instrlen - a->replacementlen;
397                 }
398                 DUMP_BYTES(insnbuf, insnbuf_sz, "%p: final_insn: ", instr);
399
400                 text_poke_early(instr, insnbuf, insnbuf_sz);
401         }
402 }
403
404 #ifdef CONFIG_SMP
405 static void alternatives_smp_lock(const s32 *start, const s32 *end,
406                                   u8 *text, u8 *text_end)
407 {
408         const s32 *poff;
409
410         for (poff = start; poff < end; poff++) {
411                 u8 *ptr = (u8 *)poff + *poff;
412
413                 if (!*poff || ptr < text || ptr >= text_end)
414                         continue;
415                 /* turn DS segment override prefix into lock prefix */
416                 if (*ptr == 0x3e)
417                         text_poke(ptr, ((unsigned char []){0xf0}), 1);
418         };
419 }
420
421 static void alternatives_smp_unlock(const s32 *start, const s32 *end,
422                                     u8 *text, u8 *text_end)
423 {
424         const s32 *poff;
425
426         for (poff = start; poff < end; poff++) {
427                 u8 *ptr = (u8 *)poff + *poff;
428
429                 if (!*poff || ptr < text || ptr >= text_end)
430                         continue;
431                 /* turn lock prefix into DS segment override prefix */
432                 if (*ptr == 0xf0)
433                         text_poke(ptr, ((unsigned char []){0x3E}), 1);
434         };
435 }
436
437 struct smp_alt_module {
438         /* what is this ??? */
439         struct module   *mod;
440         char            *name;
441
442         /* ptrs to lock prefixes */
443         const s32       *locks;
444         const s32       *locks_end;
445
446         /* .text segment, needed to avoid patching init code ;) */
447         u8              *text;
448         u8              *text_end;
449
450         struct list_head next;
451 };
452 static LIST_HEAD(smp_alt_modules);
453 static bool uniproc_patched = false;    /* protected by text_mutex */
454
455 void __init_or_module alternatives_smp_module_add(struct module *mod,
456                                                   char *name,
457                                                   void *locks, void *locks_end,
458                                                   void *text,  void *text_end)
459 {
460         struct smp_alt_module *smp;
461
462         mutex_lock(&text_mutex);
463         if (!uniproc_patched)
464                 goto unlock;
465
466         if (num_possible_cpus() == 1)
467                 /* Don't bother remembering, we'll never have to undo it. */
468                 goto smp_unlock;
469
470         smp = kzalloc(sizeof(*smp), GFP_KERNEL);
471         if (NULL == smp)
472                 /* we'll run the (safe but slow) SMP code then ... */
473                 goto unlock;
474
475         smp->mod        = mod;
476         smp->name       = name;
477         smp->locks      = locks;
478         smp->locks_end  = locks_end;
479         smp->text       = text;
480         smp->text_end   = text_end;
481         DPRINTK("locks %p -> %p, text %p -> %p, name %s\n",
482                 smp->locks, smp->locks_end,
483                 smp->text, smp->text_end, smp->name);
484
485         list_add_tail(&smp->next, &smp_alt_modules);
486 smp_unlock:
487         alternatives_smp_unlock(locks, locks_end, text, text_end);
488 unlock:
489         mutex_unlock(&text_mutex);
490 }
491
492 void __init_or_module alternatives_smp_module_del(struct module *mod)
493 {
494         struct smp_alt_module *item;
495
496         mutex_lock(&text_mutex);
497         list_for_each_entry(item, &smp_alt_modules, next) {
498                 if (mod != item->mod)
499                         continue;
500                 list_del(&item->next);
501                 kfree(item);
502                 break;
503         }
504         mutex_unlock(&text_mutex);
505 }
506
507 void alternatives_enable_smp(void)
508 {
509         struct smp_alt_module *mod;
510
511 #ifdef CONFIG_LOCKDEP
512         /*
513          * Older binutils section handling bug prevented
514          * alternatives-replacement from working reliably.
515          *
516          * If this still occurs then you should see a hang
517          * or crash shortly after this line:
518          */
519         printk("lockdep: fixing up alternatives.\n");
520 #endif
521
522         /* Why bother if there are no other CPUs? */
523         BUG_ON(num_possible_cpus() == 1);
524
525         mutex_lock(&text_mutex);
526
527         if (uniproc_patched) {
528                 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
529                 BUG_ON(num_online_cpus() != 1);
530                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
531                 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
532                 list_for_each_entry(mod, &smp_alt_modules, next)
533                         alternatives_smp_lock(mod->locks, mod->locks_end,
534                                               mod->text, mod->text_end);
535                 uniproc_patched = false;
536         }
537         mutex_unlock(&text_mutex);
538 }
539
540 /*
541  * Return 1 if the address range is reserved for SMP-alternatives.
542  * Must hold text_mutex.
543  */
544 int alternatives_text_reserved(void *start, void *end)
545 {
546         struct smp_alt_module *mod;
547         const s32 *poff;
548         u8 *text_start = start;
549         u8 *text_end = end;
550
551         lockdep_assert_held(&text_mutex);
552
553         list_for_each_entry(mod, &smp_alt_modules, next) {
554                 if (mod->text > text_end || mod->text_end < text_start)
555                         continue;
556                 for (poff = mod->locks; poff < mod->locks_end; poff++) {
557                         const u8 *ptr = (const u8 *)poff + *poff;
558
559                         if (text_start <= ptr && text_end > ptr)
560                                 return 1;
561                 }
562         }
563
564         return 0;
565 }
566 #endif /* CONFIG_SMP */
567
568 #ifdef CONFIG_PARAVIRT
569 void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
570                                      struct paravirt_patch_site *end)
571 {
572         struct paravirt_patch_site *p;
573         char insnbuf[MAX_PATCH_LEN];
574
575         for (p = start; p < end; p++) {
576                 unsigned int used;
577
578                 BUG_ON(p->len > MAX_PATCH_LEN);
579                 /* prep the buffer with the original instructions */
580                 memcpy(insnbuf, p->instr, p->len);
581                 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
582                                          (unsigned long)p->instr, p->len);
583
584                 BUG_ON(used > p->len);
585
586                 /* Pad the rest with nops */
587                 add_nops(insnbuf + used, p->len - used);
588                 text_poke_early(p->instr, insnbuf, p->len);
589         }
590 }
591 extern struct paravirt_patch_site __start_parainstructions[],
592         __stop_parainstructions[];
593 #endif  /* CONFIG_PARAVIRT */
594
595 void __init alternative_instructions(void)
596 {
597         /* The patching is not fully atomic, so try to avoid local interruptions
598            that might execute the to be patched code.
599            Other CPUs are not running. */
600         stop_nmi();
601
602         /*
603          * Don't stop machine check exceptions while patching.
604          * MCEs only happen when something got corrupted and in this
605          * case we must do something about the corruption.
606          * Ignoring it is worse than a unlikely patching race.
607          * Also machine checks tend to be broadcast and if one CPU
608          * goes into machine check the others follow quickly, so we don't
609          * expect a machine check to cause undue problems during to code
610          * patching.
611          */
612
613         apply_alternatives(__alt_instructions, __alt_instructions_end);
614
615 #ifdef CONFIG_SMP
616         /* Patch to UP if other cpus not imminent. */
617         if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
618                 uniproc_patched = true;
619                 alternatives_smp_module_add(NULL, "core kernel",
620                                             __smp_locks, __smp_locks_end,
621                                             _text, _etext);
622         }
623
624         if (!uniproc_patched || num_possible_cpus() == 1)
625                 free_init_pages("SMP alternatives",
626                                 (unsigned long)__smp_locks,
627                                 (unsigned long)__smp_locks_end);
628 #endif
629
630         apply_paravirt(__parainstructions, __parainstructions_end);
631
632         restart_nmi();
633 }
634
635 /**
636  * text_poke_early - Update instructions on a live kernel at boot time
637  * @addr: address to modify
638  * @opcode: source of the copy
639  * @len: length to copy
640  *
641  * When you use this code to patch more than one byte of an instruction
642  * you need to make sure that other CPUs cannot execute this code in parallel.
643  * Also no thread must be currently preempted in the middle of these
644  * instructions. And on the local CPU you need to be protected again NMI or MCE
645  * handlers seeing an inconsistent instruction while you patch.
646  */
647 void *__init_or_module text_poke_early(void *addr, const void *opcode,
648                                               size_t len)
649 {
650         unsigned long flags;
651         local_irq_save(flags);
652         memcpy(addr, opcode, len);
653         sync_core();
654         local_irq_restore(flags);
655         /* Could also do a CLFLUSH here to speed up CPU recovery; but
656            that causes hangs on some VIA CPUs. */
657         return addr;
658 }
659
660 /**
661  * text_poke - Update instructions on a live kernel
662  * @addr: address to modify
663  * @opcode: source of the copy
664  * @len: length to copy
665  *
666  * Only atomic text poke/set should be allowed when not doing early patching.
667  * It means the size must be writable atomically and the address must be aligned
668  * in a way that permits an atomic write. It also makes sure we fit on a single
669  * page.
670  *
671  * Note: Must be called under text_mutex.
672  */
673 void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
674 {
675         unsigned long flags;
676         char *vaddr;
677         struct page *pages[2];
678         int i;
679
680         if (!core_kernel_text((unsigned long)addr)) {
681                 pages[0] = vmalloc_to_page(addr);
682                 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
683         } else {
684                 pages[0] = virt_to_page(addr);
685                 WARN_ON(!PageReserved(pages[0]));
686                 pages[1] = virt_to_page(addr + PAGE_SIZE);
687         }
688         BUG_ON(!pages[0]);
689         local_irq_save(flags);
690         set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
691         if (pages[1])
692                 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
693         vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
694         memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
695         clear_fixmap(FIX_TEXT_POKE0);
696         if (pages[1])
697                 clear_fixmap(FIX_TEXT_POKE1);
698         local_flush_tlb();
699         sync_core();
700         /* Could also do a CLFLUSH here to speed up CPU recovery; but
701            that causes hangs on some VIA CPUs. */
702         for (i = 0; i < len; i++)
703                 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
704         local_irq_restore(flags);
705         return addr;
706 }
707
708 /*
709  * Cross-modifying kernel text with stop_machine().
710  * This code originally comes from immediate value.
711  */
712 static atomic_t stop_machine_first;
713 static int wrote_text;
714
715 struct text_poke_params {
716         struct text_poke_param *params;
717         int nparams;
718 };
719
720 static int __kprobes stop_machine_text_poke(void *data)
721 {
722         struct text_poke_params *tpp = data;
723         struct text_poke_param *p;
724         int i;
725
726         if (atomic_dec_and_test(&stop_machine_first)) {
727                 for (i = 0; i < tpp->nparams; i++) {
728                         p = &tpp->params[i];
729                         text_poke(p->addr, p->opcode, p->len);
730                 }
731                 smp_wmb();      /* Make sure other cpus see that this has run */
732                 wrote_text = 1;
733         } else {
734                 while (!wrote_text)
735                         cpu_relax();
736                 smp_mb();       /* Load wrote_text before following execution */
737         }
738
739         for (i = 0; i < tpp->nparams; i++) {
740                 p = &tpp->params[i];
741                 flush_icache_range((unsigned long)p->addr,
742                                    (unsigned long)p->addr + p->len);
743         }
744         /*
745          * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
746          * that a core serializing instruction such as "cpuid" should be
747          * executed on _each_ core before the new instruction is made visible.
748          */
749         sync_core();
750         return 0;
751 }
752
753 /**
754  * text_poke_smp - Update instructions on a live kernel on SMP
755  * @addr: address to modify
756  * @opcode: source of the copy
757  * @len: length to copy
758  *
759  * Modify multi-byte instruction by using stop_machine() on SMP. This allows
760  * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
761  * should be allowed, since stop_machine() does _not_ protect code against
762  * NMI and MCE.
763  *
764  * Note: Must be called under get_online_cpus() and text_mutex.
765  */
766 void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
767 {
768         struct text_poke_params tpp;
769         struct text_poke_param p;
770
771         p.addr = addr;
772         p.opcode = opcode;
773         p.len = len;
774         tpp.params = &p;
775         tpp.nparams = 1;
776         atomic_set(&stop_machine_first, 1);
777         wrote_text = 0;
778         /* Use __stop_machine() because the caller already got online_cpus. */
779         __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
780         return addr;
781 }
782
783 /**
784  * text_poke_smp_batch - Update instructions on a live kernel on SMP
785  * @params: an array of text_poke parameters
786  * @n: the number of elements in params.
787  *
788  * Modify multi-byte instruction by using stop_machine() on SMP. Since the
789  * stop_machine() is heavy task, it is better to aggregate text_poke requests
790  * and do it once if possible.
791  *
792  * Note: Must be called under get_online_cpus() and text_mutex.
793  */
794 void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
795 {
796         struct text_poke_params tpp = {.params = params, .nparams = n};
797
798         atomic_set(&stop_machine_first, 1);
799         wrote_text = 0;
800         __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
801 }