alternative.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 <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*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. #ifdef CONFIG_LOCKDEP
  360. /*
  361. * Older binutils section handling bug prevented
  362. * alternatives-replacement from working reliably.
  363. *
  364. * If this still occurs then you should see a hang
  365. * or crash shortly after this line:
  366. */
  367. pr_info("lockdep: fixing up alternatives\n");
  368. #endif
  369. /* Why bother if there are no other CPUs? */
  370. BUG_ON(num_possible_cpus() == 1);
  371. mutex_lock(&smp_alt);
  372. if (uniproc_patched) {
  373. pr_info("switching to SMP code\n");
  374. BUG_ON(num_online_cpus() != 1);
  375. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  376. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  377. list_for_each_entry(mod, &smp_alt_modules, next)
  378. alternatives_smp_lock(mod->locks, mod->locks_end,
  379. mod->text, mod->text_end);
  380. uniproc_patched = false;
  381. }
  382. mutex_unlock(&smp_alt);
  383. }
  384. /* Return 1 if the address range is reserved for smp-alternatives */
  385. int alternatives_text_reserved(void *start, void *end)
  386. {
  387. struct smp_alt_module *mod;
  388. const s32 *poff;
  389. u8 *text_start = start;
  390. u8 *text_end = end;
  391. list_for_each_entry(mod, &smp_alt_modules, next) {
  392. if (mod->text > text_end || mod->text_end < text_start)
  393. continue;
  394. for (poff = mod->locks; poff < mod->locks_end; poff++) {
  395. const u8 *ptr = (const u8 *)poff + *poff;
  396. if (text_start <= ptr && text_end > ptr)
  397. return 1;
  398. }
  399. }
  400. return 0;
  401. }
  402. #endif
  403. #ifdef CONFIG_PARAVIRT
  404. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  405. struct paravirt_patch_site *end)
  406. {
  407. struct paravirt_patch_site *p;
  408. char insnbuf[MAX_PATCH_LEN];
  409. if (noreplace_paravirt)
  410. return;
  411. for (p = start; p < end; p++) {
  412. unsigned int used;
  413. BUG_ON(p->len > MAX_PATCH_LEN);
  414. /* prep the buffer with the original instructions */
  415. memcpy(insnbuf, p->instr, p->len);
  416. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  417. (unsigned long)p->instr, p->len);
  418. BUG_ON(used > p->len);
  419. /* Pad the rest with nops */
  420. add_nops(insnbuf + used, p->len - used);
  421. text_poke_early(p->instr, insnbuf, p->len);
  422. }
  423. }
  424. extern struct paravirt_patch_site __start_parainstructions[],
  425. __stop_parainstructions[];
  426. #endif /* CONFIG_PARAVIRT */
  427. void __init alternative_instructions(void)
  428. {
  429. /* The patching is not fully atomic, so try to avoid local interruptions
  430. that might execute the to be patched code.
  431. Other CPUs are not running. */
  432. stop_nmi();
  433. /*
  434. * Don't stop machine check exceptions while patching.
  435. * MCEs only happen when something got corrupted and in this
  436. * case we must do something about the corruption.
  437. * Ignoring it is worse than a unlikely patching race.
  438. * Also machine checks tend to be broadcast and if one CPU
  439. * goes into machine check the others follow quickly, so we don't
  440. * expect a machine check to cause undue problems during to code
  441. * patching.
  442. */
  443. apply_alternatives(__alt_instructions, __alt_instructions_end);
  444. #ifdef CONFIG_SMP
  445. /* Patch to UP if other cpus not imminent. */
  446. if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
  447. uniproc_patched = true;
  448. alternatives_smp_module_add(NULL, "core kernel",
  449. __smp_locks, __smp_locks_end,
  450. _text, _etext);
  451. }
  452. if (!uniproc_patched || num_possible_cpus() == 1)
  453. free_init_pages("SMP alternatives",
  454. (unsigned long)__smp_locks,
  455. (unsigned long)__smp_locks_end);
  456. #endif
  457. apply_paravirt(__parainstructions, __parainstructions_end);
  458. restart_nmi();
  459. }
  460. /**
  461. * text_poke_early - Update instructions on a live kernel at boot time
  462. * @addr: address to modify
  463. * @opcode: source of the copy
  464. * @len: length to copy
  465. *
  466. * When you use this code to patch more than one byte of an instruction
  467. * you need to make sure that other CPUs cannot execute this code in parallel.
  468. * Also no thread must be currently preempted in the middle of these
  469. * instructions. And on the local CPU you need to be protected again NMI or MCE
  470. * handlers seeing an inconsistent instruction while you patch.
  471. */
  472. void *__init_or_module text_poke_early(void *addr, const void *opcode,
  473. size_t len)
  474. {
  475. unsigned long flags;
  476. local_irq_save(flags);
  477. memcpy(addr, opcode, len);
  478. sync_core();
  479. local_irq_restore(flags);
  480. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  481. that causes hangs on some VIA CPUs. */
  482. return addr;
  483. }
  484. /**
  485. * text_poke - Update instructions on a live kernel
  486. * @addr: address to modify
  487. * @opcode: source of the copy
  488. * @len: length to copy
  489. *
  490. * Only atomic text poke/set should be allowed when not doing early patching.
  491. * It means the size must be writable atomically and the address must be aligned
  492. * in a way that permits an atomic write. It also makes sure we fit on a single
  493. * page.
  494. *
  495. * Note: Must be called under text_mutex.
  496. */
  497. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  498. {
  499. unsigned long flags;
  500. char *vaddr;
  501. struct page *pages[2];
  502. int i;
  503. if (!core_kernel_text((unsigned long)addr)) {
  504. pages[0] = vmalloc_to_page(addr);
  505. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  506. } else {
  507. pages[0] = virt_to_page(addr);
  508. WARN_ON(!PageReserved(pages[0]));
  509. pages[1] = virt_to_page(addr + PAGE_SIZE);
  510. }
  511. BUG_ON(!pages[0]);
  512. local_irq_save(flags);
  513. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  514. if (pages[1])
  515. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  516. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  517. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  518. clear_fixmap(FIX_TEXT_POKE0);
  519. if (pages[1])
  520. clear_fixmap(FIX_TEXT_POKE1);
  521. local_flush_tlb();
  522. sync_core();
  523. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  524. that causes hangs on some VIA CPUs. */
  525. for (i = 0; i < len; i++)
  526. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  527. local_irq_restore(flags);
  528. return addr;
  529. }
  530. /*
  531. * Cross-modifying kernel text with stop_machine().
  532. * This code originally comes from immediate value.
  533. */
  534. static atomic_t stop_machine_first;
  535. static int wrote_text;
  536. struct text_poke_params {
  537. struct text_poke_param *params;
  538. int nparams;
  539. };
  540. static int __kprobes stop_machine_text_poke(void *data)
  541. {
  542. struct text_poke_params *tpp = data;
  543. struct text_poke_param *p;
  544. int i;
  545. if (atomic_xchg(&stop_machine_first, 0)) {
  546. for (i = 0; i < tpp->nparams; i++) {
  547. p = &tpp->params[i];
  548. text_poke(p->addr, p->opcode, p->len);
  549. }
  550. smp_wmb(); /* Make sure other cpus see that this has run */
  551. wrote_text = 1;
  552. } else {
  553. while (!wrote_text)
  554. cpu_relax();
  555. smp_mb(); /* Load wrote_text before following execution */
  556. }
  557. for (i = 0; i < tpp->nparams; i++) {
  558. p = &tpp->params[i];
  559. flush_icache_range((unsigned long)p->addr,
  560. (unsigned long)p->addr + p->len);
  561. }
  562. /*
  563. * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
  564. * that a core serializing instruction such as "cpuid" should be
  565. * executed on _each_ core before the new instruction is made visible.
  566. */
  567. sync_core();
  568. return 0;
  569. }
  570. /**
  571. * text_poke_smp - Update instructions on a live kernel on SMP
  572. * @addr: address to modify
  573. * @opcode: source of the copy
  574. * @len: length to copy
  575. *
  576. * Modify multi-byte instruction by using stop_machine() on SMP. This allows
  577. * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
  578. * should be allowed, since stop_machine() does _not_ protect code against
  579. * NMI and MCE.
  580. *
  581. * Note: Must be called under get_online_cpus() and text_mutex.
  582. */
  583. void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
  584. {
  585. struct text_poke_params tpp;
  586. struct text_poke_param p;
  587. p.addr = addr;
  588. p.opcode = opcode;
  589. p.len = len;
  590. tpp.params = &p;
  591. tpp.nparams = 1;
  592. atomic_set(&stop_machine_first, 1);
  593. wrote_text = 0;
  594. /* Use __stop_machine() because the caller already got online_cpus. */
  595. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  596. return addr;
  597. }
  598. /**
  599. * text_poke_smp_batch - Update instructions on a live kernel on SMP
  600. * @params: an array of text_poke parameters
  601. * @n: the number of elements in params.
  602. *
  603. * Modify multi-byte instruction by using stop_machine() on SMP. Since the
  604. * stop_machine() is heavy task, it is better to aggregate text_poke requests
  605. * and do it once if possible.
  606. *
  607. * Note: Must be called under get_online_cpus() and text_mutex.
  608. */
  609. void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
  610. {
  611. struct text_poke_params tpp = {.params = params, .nparams = n};
  612. atomic_set(&stop_machine_first, 1);
  613. wrote_text = 0;
  614. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  615. }