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