x86/alternatives: Cleanup DPRINTK macro
[linux-2.6-block.git] / arch / x86 / kernel / alternative.c
CommitLineData
c767a54b
JP
1#define pr_fmt(fmt) "SMP alternatives: " fmt
2
9a0b5817 3#include <linux/module.h>
f6a57033 4#include <linux/sched.h>
2f1dafe5 5#include <linux/mutex.h>
9a0b5817 6#include <linux/list.h>
8b5a10fc 7#include <linux/stringify.h>
19d36ccd
AK
8#include <linux/mm.h>
9#include <linux/vmalloc.h>
3945dab4 10#include <linux/memory.h>
3d55cc8a 11#include <linux/stop_machine.h>
5a0e3ad6 12#include <linux/slab.h>
fd4363ff 13#include <linux/kdebug.h>
9a0b5817
GH
14#include <asm/alternative.h>
15#include <asm/sections.h>
19d36ccd 16#include <asm/pgtable.h>
8f4e956b
AK
17#include <asm/mce.h>
18#include <asm/nmi.h>
e587cadd 19#include <asm/cacheflush.h>
78ff7fae 20#include <asm/tlbflush.h>
e587cadd 21#include <asm/io.h>
78ff7fae 22#include <asm/fixmap.h>
9a0b5817 23
ab144f5e
AK
24#define MAX_PATCH_LEN (255-1)
25
8b5a10fc 26static int __initdata_or_module debug_alternative;
b7fb4af0 27
d167a518
GH
28static int __init debug_alt(char *str)
29{
30 debug_alternative = 1;
31 return 1;
32}
d167a518
GH
33__setup("debug-alternative", debug_alt);
34
09488165
JB
35static int noreplace_smp;
36
b7fb4af0
JF
37static int __init setup_noreplace_smp(char *str)
38{
39 noreplace_smp = 1;
40 return 1;
41}
42__setup("noreplace-smp", setup_noreplace_smp);
43
959b4fdf 44#ifdef CONFIG_PARAVIRT
8b5a10fc 45static int __initdata_or_module noreplace_paravirt = 0;
959b4fdf
JF
46
47static int __init setup_noreplace_paravirt(char *str)
48{
49 noreplace_paravirt = 1;
50 return 1;
51}
52__setup("noreplace-paravirt", setup_noreplace_paravirt);
53#endif
b7fb4af0 54
db477a33
BP
55#define DPRINTK(fmt, args...) \
56do { \
57 if (debug_alternative) \
58 printk(KERN_DEBUG "%s: " fmt "\n", __func__, ##args); \
c767a54b 59} while (0)
d167a518 60
dc326fca
PA
61/*
62 * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
63 * that correspond to that nop. Getting from one nop to the next, we
64 * add to the array the offset that is equal to the sum of all sizes of
65 * nops preceding the one we are after.
66 *
67 * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
68 * nice symmetry of sizes of the previous nops.
69 */
8b5a10fc 70#if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
dc326fca
PA
71static const unsigned char intelnops[] =
72{
73 GENERIC_NOP1,
74 GENERIC_NOP2,
75 GENERIC_NOP3,
76 GENERIC_NOP4,
77 GENERIC_NOP5,
78 GENERIC_NOP6,
79 GENERIC_NOP7,
80 GENERIC_NOP8,
81 GENERIC_NOP5_ATOMIC
82};
83static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
84{
9a0b5817
GH
85 NULL,
86 intelnops,
87 intelnops + 1,
88 intelnops + 1 + 2,
89 intelnops + 1 + 2 + 3,
90 intelnops + 1 + 2 + 3 + 4,
91 intelnops + 1 + 2 + 3 + 4 + 5,
92 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
93 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
dc326fca 94 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
9a0b5817 95};
d167a518
GH
96#endif
97
98#ifdef K8_NOP1
dc326fca
PA
99static const unsigned char k8nops[] =
100{
101 K8_NOP1,
102 K8_NOP2,
103 K8_NOP3,
104 K8_NOP4,
105 K8_NOP5,
106 K8_NOP6,
107 K8_NOP7,
108 K8_NOP8,
109 K8_NOP5_ATOMIC
110};
111static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
112{
9a0b5817
GH
113 NULL,
114 k8nops,
115 k8nops + 1,
116 k8nops + 1 + 2,
117 k8nops + 1 + 2 + 3,
118 k8nops + 1 + 2 + 3 + 4,
119 k8nops + 1 + 2 + 3 + 4 + 5,
120 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
121 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
dc326fca 122 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
9a0b5817 123};
d167a518
GH
124#endif
125
8b5a10fc 126#if defined(K7_NOP1) && !defined(CONFIG_X86_64)
dc326fca
PA
127static const unsigned char k7nops[] =
128{
129 K7_NOP1,
130 K7_NOP2,
131 K7_NOP3,
132 K7_NOP4,
133 K7_NOP5,
134 K7_NOP6,
135 K7_NOP7,
136 K7_NOP8,
137 K7_NOP5_ATOMIC
138};
139static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
140{
9a0b5817
GH
141 NULL,
142 k7nops,
143 k7nops + 1,
144 k7nops + 1 + 2,
145 k7nops + 1 + 2 + 3,
146 k7nops + 1 + 2 + 3 + 4,
147 k7nops + 1 + 2 + 3 + 4 + 5,
148 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
149 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
dc326fca 150 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
9a0b5817 151};
d167a518
GH
152#endif
153
32c464f5 154#ifdef P6_NOP1
cb09cad4 155static const unsigned char p6nops[] =
dc326fca
PA
156{
157 P6_NOP1,
158 P6_NOP2,
159 P6_NOP3,
160 P6_NOP4,
161 P6_NOP5,
162 P6_NOP6,
163 P6_NOP7,
164 P6_NOP8,
165 P6_NOP5_ATOMIC
166};
167static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
168{
32c464f5
JB
169 NULL,
170 p6nops,
171 p6nops + 1,
172 p6nops + 1 + 2,
173 p6nops + 1 + 2 + 3,
174 p6nops + 1 + 2 + 3 + 4,
175 p6nops + 1 + 2 + 3 + 4 + 5,
176 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
177 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
dc326fca 178 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
32c464f5
JB
179};
180#endif
181
dc326fca 182/* Initialize these to a safe default */
d167a518 183#ifdef CONFIG_X86_64
dc326fca
PA
184const unsigned char * const *ideal_nops = p6_nops;
185#else
186const unsigned char * const *ideal_nops = intel_nops;
187#endif
d167a518 188
dc326fca 189void __init arch_init_ideal_nops(void)
d167a518 190{
dc326fca
PA
191 switch (boot_cpu_data.x86_vendor) {
192 case X86_VENDOR_INTEL:
d8d9766c
PA
193 /*
194 * Due to a decoder implementation quirk, some
195 * specific Intel CPUs actually perform better with
196 * the "k8_nops" than with the SDM-recommended NOPs.
197 */
198 if (boot_cpu_data.x86 == 6 &&
199 boot_cpu_data.x86_model >= 0x0f &&
200 boot_cpu_data.x86_model != 0x1c &&
201 boot_cpu_data.x86_model != 0x26 &&
202 boot_cpu_data.x86_model != 0x27 &&
203 boot_cpu_data.x86_model < 0x30) {
204 ideal_nops = k8_nops;
205 } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
dc326fca
PA
206 ideal_nops = p6_nops;
207 } else {
208#ifdef CONFIG_X86_64
209 ideal_nops = k8_nops;
210#else
211 ideal_nops = intel_nops;
212#endif
213 }
d6250a3f 214 break;
dc326fca
PA
215 default:
216#ifdef CONFIG_X86_64
217 ideal_nops = k8_nops;
218#else
219 if (boot_cpu_has(X86_FEATURE_K8))
220 ideal_nops = k8_nops;
221 else if (boot_cpu_has(X86_FEATURE_K7))
222 ideal_nops = k7_nops;
223 else
224 ideal_nops = intel_nops;
225#endif
226 }
9a0b5817
GH
227}
228
ab144f5e 229/* Use this to add nops to a buffer, then text_poke the whole buffer. */
8b5a10fc 230static void __init_or_module add_nops(void *insns, unsigned int len)
139ec7c4 231{
139ec7c4
RR
232 while (len > 0) {
233 unsigned int noplen = len;
234 if (noplen > ASM_NOP_MAX)
235 noplen = ASM_NOP_MAX;
dc326fca 236 memcpy(insns, ideal_nops[noplen], noplen);
139ec7c4
RR
237 insns += noplen;
238 len -= noplen;
239 }
240}
241
d167a518 242extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
5967ed87 243extern s32 __smp_locks[], __smp_locks_end[];
fa6f2cc7 244void *text_poke_early(void *addr, const void *opcode, size_t len);
d167a518 245
db477a33
BP
246/*
247 * Replace instructions with better alternatives for this CPU type. This runs
248 * before SMP is initialized to avoid SMP problems with self modifying code.
249 * This implies that asymmetric systems where APs have less capabilities than
250 * the boot processor are not handled. Tough. Make sure you disable such
251 * features by hand.
252 */
8b5a10fc
JB
253void __init_or_module apply_alternatives(struct alt_instr *start,
254 struct alt_instr *end)
9a0b5817 255{
9a0b5817 256 struct alt_instr *a;
59e97e4d 257 u8 *instr, *replacement;
1b1d9258 258 u8 insnbuf[MAX_PATCH_LEN];
9a0b5817 259
db477a33 260 DPRINTK("alt table %p -> %p", start, end);
50973133
FY
261 /*
262 * The scan order should be from start to end. A later scanned
db477a33 263 * alternative code can overwrite previously scanned alternative code.
50973133
FY
264 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
265 * patch code.
266 *
267 * So be careful if you want to change the scan order to any other
268 * order.
269 */
9a0b5817 270 for (a = start; a < end; a++) {
59e97e4d
AL
271 instr = (u8 *)&a->instr_offset + a->instr_offset;
272 replacement = (u8 *)&a->repl_offset + a->repl_offset;
9a0b5817 273 BUG_ON(a->replacementlen > a->instrlen);
ab144f5e 274 BUG_ON(a->instrlen > sizeof(insnbuf));
65fc985b 275 BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
9a0b5817
GH
276 if (!boot_cpu_has(a->cpuid))
277 continue;
59e97e4d 278
db477a33
BP
279 DPRINTK("feat: %d*32+%d, old: (%p, len: %d), repl: (%p, len: %d)",
280 a->cpuid >> 5,
281 a->cpuid & 0x1f,
282 instr, a->instrlen,
283 replacement, a->replacementlen);
284
59e97e4d
AL
285 memcpy(insnbuf, replacement, a->replacementlen);
286
287 /* 0xe8 is a relative jump; fix the offset. */
db477a33
BP
288 if (*insnbuf == 0xe8 && a->replacementlen == 5) {
289 *(s32 *)(insnbuf + 1) += replacement - instr;
290 DPRINTK("Fix CALL offset: 0x%x", *(s32 *)(insnbuf + 1));
291 }
59e97e4d
AL
292
293 add_nops(insnbuf + a->replacementlen,
294 a->instrlen - a->replacementlen);
295
e587cadd 296 text_poke_early(instr, insnbuf, a->instrlen);
9a0b5817
GH
297 }
298}
299
8ec4d41f
GH
300#ifdef CONFIG_SMP
301
5967ed87
JB
302static void alternatives_smp_lock(const s32 *start, const s32 *end,
303 u8 *text, u8 *text_end)
9a0b5817 304{
5967ed87 305 const s32 *poff;
9a0b5817 306
3945dab4 307 mutex_lock(&text_mutex);
5967ed87
JB
308 for (poff = start; poff < end; poff++) {
309 u8 *ptr = (u8 *)poff + *poff;
310
311 if (!*poff || ptr < text || ptr >= text_end)
9a0b5817 312 continue;
f88f07e0 313 /* turn DS segment override prefix into lock prefix */
d9c5841e
PA
314 if (*ptr == 0x3e)
315 text_poke(ptr, ((unsigned char []){0xf0}), 1);
4b8073e4 316 }
3945dab4 317 mutex_unlock(&text_mutex);
9a0b5817
GH
318}
319
5967ed87
JB
320static void alternatives_smp_unlock(const s32 *start, const s32 *end,
321 u8 *text, u8 *text_end)
9a0b5817 322{
5967ed87 323 const s32 *poff;
9a0b5817 324
3945dab4 325 mutex_lock(&text_mutex);
5967ed87
JB
326 for (poff = start; poff < end; poff++) {
327 u8 *ptr = (u8 *)poff + *poff;
328
329 if (!*poff || ptr < text || ptr >= text_end)
9a0b5817 330 continue;
f88f07e0 331 /* turn lock prefix into DS segment override prefix */
d9c5841e
PA
332 if (*ptr == 0xf0)
333 text_poke(ptr, ((unsigned char []){0x3E}), 1);
4b8073e4 334 }
3945dab4 335 mutex_unlock(&text_mutex);
9a0b5817
GH
336}
337
338struct smp_alt_module {
339 /* what is this ??? */
340 struct module *mod;
341 char *name;
342
343 /* ptrs to lock prefixes */
5967ed87
JB
344 const s32 *locks;
345 const s32 *locks_end;
9a0b5817
GH
346
347 /* .text segment, needed to avoid patching init code ;) */
348 u8 *text;
349 u8 *text_end;
350
351 struct list_head next;
352};
353static LIST_HEAD(smp_alt_modules);
2f1dafe5 354static DEFINE_MUTEX(smp_alt);
816afe4f 355static bool uniproc_patched = false; /* protected by smp_alt */
9a0b5817 356
8b5a10fc
JB
357void __init_or_module alternatives_smp_module_add(struct module *mod,
358 char *name,
359 void *locks, void *locks_end,
360 void *text, void *text_end)
9a0b5817
GH
361{
362 struct smp_alt_module *smp;
9a0b5817 363
816afe4f
RR
364 mutex_lock(&smp_alt);
365 if (!uniproc_patched)
366 goto unlock;
b7fb4af0 367
816afe4f
RR
368 if (num_possible_cpus() == 1)
369 /* Don't bother remembering, we'll never have to undo it. */
370 goto smp_unlock;
9a0b5817
GH
371
372 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
373 if (NULL == smp)
816afe4f
RR
374 /* we'll run the (safe but slow) SMP code then ... */
375 goto unlock;
9a0b5817
GH
376
377 smp->mod = mod;
378 smp->name = name;
379 smp->locks = locks;
380 smp->locks_end = locks_end;
381 smp->text = text;
382 smp->text_end = text_end;
db477a33
BP
383 DPRINTK("locks %p -> %p, text %p -> %p, name %s\n",
384 smp->locks, smp->locks_end,
9a0b5817
GH
385 smp->text, smp->text_end, smp->name);
386
9a0b5817 387 list_add_tail(&smp->next, &smp_alt_modules);
816afe4f
RR
388smp_unlock:
389 alternatives_smp_unlock(locks, locks_end, text, text_end);
390unlock:
2f1dafe5 391 mutex_unlock(&smp_alt);
9a0b5817
GH
392}
393
8b5a10fc 394void __init_or_module alternatives_smp_module_del(struct module *mod)
9a0b5817
GH
395{
396 struct smp_alt_module *item;
9a0b5817 397
2f1dafe5 398 mutex_lock(&smp_alt);
9a0b5817
GH
399 list_for_each_entry(item, &smp_alt_modules, next) {
400 if (mod != item->mod)
401 continue;
402 list_del(&item->next);
9a0b5817 403 kfree(item);
816afe4f 404 break;
9a0b5817 405 }
2f1dafe5 406 mutex_unlock(&smp_alt);
9a0b5817
GH
407}
408
816afe4f 409void alternatives_enable_smp(void)
9a0b5817
GH
410{
411 struct smp_alt_module *mod;
9a0b5817 412
816afe4f
RR
413 /* Why bother if there are no other CPUs? */
414 BUG_ON(num_possible_cpus() == 1);
9a0b5817 415
2f1dafe5 416 mutex_lock(&smp_alt);
ca74a6f8 417
816afe4f 418 if (uniproc_patched) {
c767a54b 419 pr_info("switching to SMP code\n");
816afe4f 420 BUG_ON(num_online_cpus() != 1);
53756d37
JF
421 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
422 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
9a0b5817
GH
423 list_for_each_entry(mod, &smp_alt_modules, next)
424 alternatives_smp_lock(mod->locks, mod->locks_end,
425 mod->text, mod->text_end);
816afe4f 426 uniproc_patched = false;
9a0b5817 427 }
2f1dafe5 428 mutex_unlock(&smp_alt);
9a0b5817
GH
429}
430
2cfa1978
MH
431/* Return 1 if the address range is reserved for smp-alternatives */
432int alternatives_text_reserved(void *start, void *end)
433{
434 struct smp_alt_module *mod;
5967ed87 435 const s32 *poff;
076dc4a6
MH
436 u8 *text_start = start;
437 u8 *text_end = end;
2cfa1978
MH
438
439 list_for_each_entry(mod, &smp_alt_modules, next) {
076dc4a6 440 if (mod->text > text_end || mod->text_end < text_start)
2cfa1978 441 continue;
5967ed87
JB
442 for (poff = mod->locks; poff < mod->locks_end; poff++) {
443 const u8 *ptr = (const u8 *)poff + *poff;
444
445 if (text_start <= ptr && text_end > ptr)
2cfa1978 446 return 1;
5967ed87 447 }
2cfa1978
MH
448 }
449
450 return 0;
451}
8ec4d41f
GH
452#endif
453
139ec7c4 454#ifdef CONFIG_PARAVIRT
8b5a10fc
JB
455void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
456 struct paravirt_patch_site *end)
139ec7c4 457{
98de032b 458 struct paravirt_patch_site *p;
ab144f5e 459 char insnbuf[MAX_PATCH_LEN];
139ec7c4 460
959b4fdf
JF
461 if (noreplace_paravirt)
462 return;
463
139ec7c4
RR
464 for (p = start; p < end; p++) {
465 unsigned int used;
466
ab144f5e 467 BUG_ON(p->len > MAX_PATCH_LEN);
d34fda4a
CW
468 /* prep the buffer with the original instructions */
469 memcpy(insnbuf, p->instr, p->len);
93b1eab3
JF
470 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
471 (unsigned long)p->instr, p->len);
7f63c41c 472
63f70270
JF
473 BUG_ON(used > p->len);
474
139ec7c4 475 /* Pad the rest with nops */
ab144f5e 476 add_nops(insnbuf + used, p->len - used);
e587cadd 477 text_poke_early(p->instr, insnbuf, p->len);
139ec7c4 478 }
139ec7c4 479}
98de032b 480extern struct paravirt_patch_site __start_parainstructions[],
139ec7c4
RR
481 __stop_parainstructions[];
482#endif /* CONFIG_PARAVIRT */
483
9a0b5817
GH
484void __init alternative_instructions(void)
485{
8f4e956b
AK
486 /* The patching is not fully atomic, so try to avoid local interruptions
487 that might execute the to be patched code.
488 Other CPUs are not running. */
489 stop_nmi();
123aa76e
AK
490
491 /*
492 * Don't stop machine check exceptions while patching.
493 * MCEs only happen when something got corrupted and in this
494 * case we must do something about the corruption.
495 * Ignoring it is worse than a unlikely patching race.
496 * Also machine checks tend to be broadcast and if one CPU
497 * goes into machine check the others follow quickly, so we don't
498 * expect a machine check to cause undue problems during to code
499 * patching.
500 */
8f4e956b 501
9a0b5817
GH
502 apply_alternatives(__alt_instructions, __alt_instructions_end);
503
8ec4d41f 504#ifdef CONFIG_SMP
816afe4f
RR
505 /* Patch to UP if other cpus not imminent. */
506 if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
507 uniproc_patched = true;
9a0b5817
GH
508 alternatives_smp_module_add(NULL, "core kernel",
509 __smp_locks, __smp_locks_end,
510 _text, _etext);
9a0b5817 511 }
8f4e956b 512
816afe4f 513 if (!uniproc_patched || num_possible_cpus() == 1)
f68fd5f4
FW
514 free_init_pages("SMP alternatives",
515 (unsigned long)__smp_locks,
516 (unsigned long)__smp_locks_end);
816afe4f
RR
517#endif
518
519 apply_paravirt(__parainstructions, __parainstructions_end);
f68fd5f4 520
8f4e956b 521 restart_nmi();
9a0b5817 522}
19d36ccd 523
e587cadd
MD
524/**
525 * text_poke_early - Update instructions on a live kernel at boot time
526 * @addr: address to modify
527 * @opcode: source of the copy
528 * @len: length to copy
529 *
19d36ccd
AK
530 * When you use this code to patch more than one byte of an instruction
531 * you need to make sure that other CPUs cannot execute this code in parallel.
e587cadd
MD
532 * Also no thread must be currently preempted in the middle of these
533 * instructions. And on the local CPU you need to be protected again NMI or MCE
534 * handlers seeing an inconsistent instruction while you patch.
19d36ccd 535 */
fa6f2cc7 536void *__init_or_module text_poke_early(void *addr, const void *opcode,
8b5a10fc 537 size_t len)
19d36ccd 538{
e587cadd
MD
539 unsigned long flags;
540 local_irq_save(flags);
19d36ccd 541 memcpy(addr, opcode, len);
e587cadd 542 sync_core();
5367b688 543 local_irq_restore(flags);
e587cadd
MD
544 /* Could also do a CLFLUSH here to speed up CPU recovery; but
545 that causes hangs on some VIA CPUs. */
546 return addr;
547}
548
549/**
550 * text_poke - Update instructions on a live kernel
551 * @addr: address to modify
552 * @opcode: source of the copy
553 * @len: length to copy
554 *
555 * Only atomic text poke/set should be allowed when not doing early patching.
556 * It means the size must be writable atomically and the address must be aligned
557 * in a way that permits an atomic write. It also makes sure we fit on a single
558 * page.
78ff7fae
MH
559 *
560 * Note: Must be called under text_mutex.
e587cadd 561 */
9c54b616 562void *text_poke(void *addr, const void *opcode, size_t len)
e587cadd 563{
78ff7fae 564 unsigned long flags;
e587cadd 565 char *vaddr;
b7b66baa
MD
566 struct page *pages[2];
567 int i;
e587cadd 568
b7b66baa
MD
569 if (!core_kernel_text((unsigned long)addr)) {
570 pages[0] = vmalloc_to_page(addr);
571 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
15a601eb 572 } else {
b7b66baa 573 pages[0] = virt_to_page(addr);
00c6b2d5 574 WARN_ON(!PageReserved(pages[0]));
b7b66baa 575 pages[1] = virt_to_page(addr + PAGE_SIZE);
e587cadd 576 }
b7b66baa 577 BUG_ON(!pages[0]);
7cf49427 578 local_irq_save(flags);
78ff7fae
MH
579 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
580 if (pages[1])
581 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
582 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
b7b66baa 583 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
78ff7fae
MH
584 clear_fixmap(FIX_TEXT_POKE0);
585 if (pages[1])
586 clear_fixmap(FIX_TEXT_POKE1);
587 local_flush_tlb();
19d36ccd 588 sync_core();
a534b679
AK
589 /* Could also do a CLFLUSH here to speed up CPU recovery; but
590 that causes hangs on some VIA CPUs. */
b7b66baa
MD
591 for (i = 0; i < len; i++)
592 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
7cf49427 593 local_irq_restore(flags);
e587cadd 594 return addr;
19d36ccd 595}
3d55cc8a 596
fd4363ff
JK
597static void do_sync_core(void *info)
598{
599 sync_core();
600}
601
602static bool bp_patching_in_progress;
603static void *bp_int3_handler, *bp_int3_addr;
604
17f41571 605int poke_int3_handler(struct pt_regs *regs)
fd4363ff 606{
fd4363ff
JK
607 /* bp_patching_in_progress */
608 smp_rmb();
609
610 if (likely(!bp_patching_in_progress))
17f41571 611 return 0;
fd4363ff 612
17f41571
JK
613 if (user_mode_vm(regs) || regs->ip != (unsigned long)bp_int3_addr)
614 return 0;
fd4363ff
JK
615
616 /* set up the specified breakpoint handler */
17f41571
JK
617 regs->ip = (unsigned long) bp_int3_handler;
618
619 return 1;
fd4363ff 620
fd4363ff 621}
17f41571 622
fd4363ff
JK
623/**
624 * text_poke_bp() -- update instructions on live kernel on SMP
625 * @addr: address to patch
626 * @opcode: opcode of new instruction
627 * @len: length to copy
628 * @handler: address to jump to when the temporary breakpoint is hit
629 *
630 * Modify multi-byte instruction by using int3 breakpoint on SMP.
ea8596bb
MH
631 * We completely avoid stop_machine() here, and achieve the
632 * synchronization using int3 breakpoint.
fd4363ff
JK
633 *
634 * The way it is done:
635 * - add a int3 trap to the address that will be patched
636 * - sync cores
637 * - update all but the first byte of the patched range
638 * - sync cores
639 * - replace the first byte (int3) by the first byte of
640 * replacing opcode
641 * - sync cores
642 *
643 * Note: must be called under text_mutex.
644 */
645void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
646{
647 unsigned char int3 = 0xcc;
648
649 bp_int3_handler = handler;
650 bp_int3_addr = (u8 *)addr + sizeof(int3);
651 bp_patching_in_progress = true;
652 /*
653 * Corresponding read barrier in int3 notifier for
654 * making sure the in_progress flags is correctly ordered wrt.
655 * patching
656 */
657 smp_wmb();
658
659 text_poke(addr, &int3, sizeof(int3));
660
661 on_each_cpu(do_sync_core, NULL, 1);
662
663 if (len - sizeof(int3) > 0) {
664 /* patch all but the first byte */
665 text_poke((char *)addr + sizeof(int3),
666 (const char *) opcode + sizeof(int3),
667 len - sizeof(int3));
668 /*
669 * According to Intel, this core syncing is very likely
670 * not necessary and we'd be safe even without it. But
671 * better safe than sorry (plus there's not only Intel).
672 */
673 on_each_cpu(do_sync_core, NULL, 1);
674 }
675
676 /* patch the first byte */
677 text_poke(addr, opcode, sizeof(int3));
678
679 on_each_cpu(do_sync_core, NULL, 1);
680
681 bp_patching_in_progress = false;
682 smp_wmb();
683
684 return addr;
685}
686