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