alternative.c 17 KB

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