alternative.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. static int int3_notify(struct notifier_block *self, unsigned long val, void *data)
  538. {
  539. struct die_args *args = data;
  540. /* bp_patching_in_progress */
  541. smp_rmb();
  542. if (likely(!bp_patching_in_progress))
  543. return NOTIFY_DONE;
  544. /* we are not interested in non-int3 faults and ring > 0 faults */
  545. if (val != DIE_INT3 || !args->regs || user_mode_vm(args->regs)
  546. || args->regs->ip != (unsigned long)bp_int3_addr)
  547. return NOTIFY_DONE;
  548. /* set up the specified breakpoint handler */
  549. args->regs->ip = (unsigned long) bp_int3_handler;
  550. return NOTIFY_STOP;
  551. }
  552. /**
  553. * text_poke_bp() -- update instructions on live kernel on SMP
  554. * @addr: address to patch
  555. * @opcode: opcode of new instruction
  556. * @len: length to copy
  557. * @handler: address to jump to when the temporary breakpoint is hit
  558. *
  559. * Modify multi-byte instruction by using int3 breakpoint on SMP.
  560. * In contrary to text_poke_smp(), we completely avoid stop_machine() here,
  561. * and achieve the synchronization using int3 breakpoint.
  562. *
  563. * The way it is done:
  564. * - add a int3 trap to the address that will be patched
  565. * - sync cores
  566. * - update all but the first byte of the patched range
  567. * - sync cores
  568. * - replace the first byte (int3) by the first byte of
  569. * replacing opcode
  570. * - sync cores
  571. *
  572. * Note: must be called under text_mutex.
  573. */
  574. void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
  575. {
  576. unsigned char int3 = 0xcc;
  577. bp_int3_handler = handler;
  578. bp_int3_addr = (u8 *)addr + sizeof(int3);
  579. bp_patching_in_progress = true;
  580. /*
  581. * Corresponding read barrier in int3 notifier for
  582. * making sure the in_progress flags is correctly ordered wrt.
  583. * patching
  584. */
  585. smp_wmb();
  586. text_poke(addr, &int3, sizeof(int3));
  587. on_each_cpu(do_sync_core, NULL, 1);
  588. if (len - sizeof(int3) > 0) {
  589. /* patch all but the first byte */
  590. text_poke((char *)addr + sizeof(int3),
  591. (const char *) opcode + sizeof(int3),
  592. len - sizeof(int3));
  593. /*
  594. * According to Intel, this core syncing is very likely
  595. * not necessary and we'd be safe even without it. But
  596. * better safe than sorry (plus there's not only Intel).
  597. */
  598. on_each_cpu(do_sync_core, NULL, 1);
  599. }
  600. /* patch the first byte */
  601. text_poke(addr, opcode, sizeof(int3));
  602. on_each_cpu(do_sync_core, NULL, 1);
  603. bp_patching_in_progress = false;
  604. smp_wmb();
  605. return addr;
  606. }
  607. /* this one needs to run before anything else handles it as a
  608. * regular exception */
  609. static struct notifier_block int3_nb = {
  610. .priority = 0x7fffffff,
  611. .notifier_call = int3_notify
  612. };
  613. static int __init int3_init(void)
  614. {
  615. return register_die_notifier(&int3_nb);
  616. }
  617. arch_initcall(int3_init);
  618. /*
  619. * Cross-modifying kernel text with stop_machine().
  620. * This code originally comes from immediate value.
  621. */
  622. static atomic_t stop_machine_first;
  623. static int wrote_text;
  624. struct text_poke_params {
  625. struct text_poke_param *params;
  626. int nparams;
  627. };
  628. static int __kprobes stop_machine_text_poke(void *data)
  629. {
  630. struct text_poke_params *tpp = data;
  631. struct text_poke_param *p;
  632. int i;
  633. if (atomic_xchg(&stop_machine_first, 0)) {
  634. for (i = 0; i < tpp->nparams; i++) {
  635. p = &tpp->params[i];
  636. text_poke(p->addr, p->opcode, p->len);
  637. }
  638. smp_wmb(); /* Make sure other cpus see that this has run */
  639. wrote_text = 1;
  640. } else {
  641. while (!wrote_text)
  642. cpu_relax();
  643. smp_mb(); /* Load wrote_text before following execution */
  644. }
  645. for (i = 0; i < tpp->nparams; i++) {
  646. p = &tpp->params[i];
  647. flush_icache_range((unsigned long)p->addr,
  648. (unsigned long)p->addr + p->len);
  649. }
  650. /*
  651. * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
  652. * that a core serializing instruction such as "cpuid" should be
  653. * executed on _each_ core before the new instruction is made visible.
  654. */
  655. sync_core();
  656. return 0;
  657. }
  658. /**
  659. * text_poke_smp - Update instructions on a live kernel on SMP
  660. * @addr: address to modify
  661. * @opcode: source of the copy
  662. * @len: length to copy
  663. *
  664. * Modify multi-byte instruction by using stop_machine() on SMP. This allows
  665. * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
  666. * should be allowed, since stop_machine() does _not_ protect code against
  667. * NMI and MCE.
  668. *
  669. * Note: Must be called under get_online_cpus() and text_mutex.
  670. */
  671. void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
  672. {
  673. struct text_poke_params tpp;
  674. struct text_poke_param p;
  675. p.addr = addr;
  676. p.opcode = opcode;
  677. p.len = len;
  678. tpp.params = &p;
  679. tpp.nparams = 1;
  680. atomic_set(&stop_machine_first, 1);
  681. wrote_text = 0;
  682. /* Use __stop_machine() because the caller already got online_cpus. */
  683. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  684. return addr;
  685. }
  686. /**
  687. * text_poke_smp_batch - Update instructions on a live kernel on SMP
  688. * @params: an array of text_poke parameters
  689. * @n: the number of elements in params.
  690. *
  691. * Modify multi-byte instruction by using stop_machine() on SMP. Since the
  692. * stop_machine() is heavy task, it is better to aggregate text_poke requests
  693. * and do it once if possible.
  694. *
  695. * Note: Must be called under get_online_cpus() and text_mutex.
  696. */
  697. void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
  698. {
  699. struct text_poke_params tpp = {.params = params, .nparams = n};
  700. atomic_set(&stop_machine_first, 1);
  701. wrote_text = 0;
  702. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  703. }