alternative.c 17 KB

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