alternative.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/mutex.h>
  4. #include <linux/list.h>
  5. #include <linux/stringify.h>
  6. #include <linux/kprobes.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 <asm/alternative.h>
  13. #include <asm/sections.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/mce.h>
  16. #include <asm/nmi.h>
  17. #include <asm/vsyscall.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. #ifdef CONFIG_HOTPLUG_CPU
  24. static int smp_alt_once;
  25. static int __init bootonly(char *str)
  26. {
  27. smp_alt_once = 1;
  28. return 1;
  29. }
  30. __setup("smp-alt-boot", bootonly);
  31. #else
  32. #define smp_alt_once 1
  33. #endif
  34. static int __initdata_or_module debug_alternative;
  35. static int __init debug_alt(char *str)
  36. {
  37. debug_alternative = 1;
  38. return 1;
  39. }
  40. __setup("debug-alternative", debug_alt);
  41. static int noreplace_smp;
  42. static int __init setup_noreplace_smp(char *str)
  43. {
  44. noreplace_smp = 1;
  45. return 1;
  46. }
  47. __setup("noreplace-smp", setup_noreplace_smp);
  48. #ifdef CONFIG_PARAVIRT
  49. static int __initdata_or_module noreplace_paravirt = 0;
  50. static int __init setup_noreplace_paravirt(char *str)
  51. {
  52. noreplace_paravirt = 1;
  53. return 1;
  54. }
  55. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  56. #endif
  57. #define DPRINTK(fmt, args...) if (debug_alternative) \
  58. printk(KERN_DEBUG fmt, args)
  59. #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
  60. /* Use inline assembly to define this because the nops are defined
  61. as inline assembly strings in the include files and we cannot
  62. get them easily into strings. */
  63. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nintelnops: "
  64. GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
  65. GENERIC_NOP7 GENERIC_NOP8
  66. "\t.previous");
  67. extern const unsigned char intelnops[];
  68. static const unsigned char *const __initconst_or_module
  69. intel_nops[ASM_NOP_MAX+1] = {
  70. NULL,
  71. intelnops,
  72. intelnops + 1,
  73. intelnops + 1 + 2,
  74. intelnops + 1 + 2 + 3,
  75. intelnops + 1 + 2 + 3 + 4,
  76. intelnops + 1 + 2 + 3 + 4 + 5,
  77. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  78. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  79. };
  80. #endif
  81. #ifdef K8_NOP1
  82. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nk8nops: "
  83. K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
  84. K8_NOP7 K8_NOP8
  85. "\t.previous");
  86. extern const unsigned char k8nops[];
  87. static const unsigned char *const __initconst_or_module
  88. k8_nops[ASM_NOP_MAX+1] = {
  89. NULL,
  90. k8nops,
  91. k8nops + 1,
  92. k8nops + 1 + 2,
  93. k8nops + 1 + 2 + 3,
  94. k8nops + 1 + 2 + 3 + 4,
  95. k8nops + 1 + 2 + 3 + 4 + 5,
  96. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  97. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  98. };
  99. #endif
  100. #if defined(K7_NOP1) && !defined(CONFIG_X86_64)
  101. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nk7nops: "
  102. K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
  103. K7_NOP7 K7_NOP8
  104. "\t.previous");
  105. extern const unsigned char k7nops[];
  106. static const unsigned char *const __initconst_or_module
  107. k7_nops[ASM_NOP_MAX+1] = {
  108. NULL,
  109. k7nops,
  110. k7nops + 1,
  111. k7nops + 1 + 2,
  112. k7nops + 1 + 2 + 3,
  113. k7nops + 1 + 2 + 3 + 4,
  114. k7nops + 1 + 2 + 3 + 4 + 5,
  115. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  116. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  117. };
  118. #endif
  119. #ifdef P6_NOP1
  120. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\np6nops: "
  121. P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
  122. P6_NOP7 P6_NOP8
  123. "\t.previous");
  124. extern const unsigned char p6nops[];
  125. static const unsigned char *const __initconst_or_module
  126. p6_nops[ASM_NOP_MAX+1] = {
  127. NULL,
  128. p6nops,
  129. p6nops + 1,
  130. p6nops + 1 + 2,
  131. p6nops + 1 + 2 + 3,
  132. p6nops + 1 + 2 + 3 + 4,
  133. p6nops + 1 + 2 + 3 + 4 + 5,
  134. p6nops + 1 + 2 + 3 + 4 + 5 + 6,
  135. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  136. };
  137. #endif
  138. #ifdef CONFIG_X86_64
  139. extern char __vsyscall_0;
  140. static const unsigned char *const *__init_or_module find_nop_table(void)
  141. {
  142. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  143. boot_cpu_has(X86_FEATURE_NOPL))
  144. return p6_nops;
  145. else
  146. return k8_nops;
  147. }
  148. #else /* CONFIG_X86_64 */
  149. static const unsigned char *const *__init_or_module find_nop_table(void)
  150. {
  151. if (boot_cpu_has(X86_FEATURE_K8))
  152. return k8_nops;
  153. else if (boot_cpu_has(X86_FEATURE_K7))
  154. return k7_nops;
  155. else if (boot_cpu_has(X86_FEATURE_NOPL))
  156. return p6_nops;
  157. else
  158. return intel_nops;
  159. }
  160. #endif /* CONFIG_X86_64 */
  161. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  162. static void __init_or_module add_nops(void *insns, unsigned int len)
  163. {
  164. const unsigned char *const *noptable = find_nop_table();
  165. while (len > 0) {
  166. unsigned int noplen = len;
  167. if (noplen > ASM_NOP_MAX)
  168. noplen = ASM_NOP_MAX;
  169. memcpy(insns, noptable[noplen], noplen);
  170. insns += noplen;
  171. len -= noplen;
  172. }
  173. }
  174. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  175. extern s32 __smp_locks[], __smp_locks_end[];
  176. static void *text_poke_early(void *addr, const void *opcode, size_t len);
  177. /* Replace instructions with better alternatives for this CPU type.
  178. This runs before SMP is initialized to avoid SMP problems with
  179. self modifying code. This implies that assymetric systems where
  180. APs have less capabilities than the boot processor are not handled.
  181. Tough. Make sure you disable such features by hand. */
  182. void __init_or_module apply_alternatives(struct alt_instr *start,
  183. struct alt_instr *end)
  184. {
  185. struct alt_instr *a;
  186. u8 insnbuf[MAX_PATCH_LEN];
  187. DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
  188. for (a = start; a < end; a++) {
  189. u8 *instr = a->instr;
  190. BUG_ON(a->replacementlen > a->instrlen);
  191. BUG_ON(a->instrlen > sizeof(insnbuf));
  192. if (!boot_cpu_has(a->cpuid))
  193. continue;
  194. #ifdef CONFIG_X86_64
  195. /* vsyscall code is not mapped yet. resolve it manually. */
  196. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  197. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  198. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  199. __func__, a->instr, instr);
  200. }
  201. #endif
  202. memcpy(insnbuf, a->replacement, a->replacementlen);
  203. if (*insnbuf == 0xe8 && a->replacementlen == 5)
  204. *(s32 *)(insnbuf + 1) += a->replacement - a->instr;
  205. add_nops(insnbuf + a->replacementlen,
  206. a->instrlen - a->replacementlen);
  207. text_poke_early(instr, insnbuf, a->instrlen);
  208. }
  209. }
  210. #ifdef CONFIG_SMP
  211. static void alternatives_smp_lock(const s32 *start, const s32 *end,
  212. u8 *text, u8 *text_end)
  213. {
  214. const s32 *poff;
  215. mutex_lock(&text_mutex);
  216. for (poff = start; poff < end; poff++) {
  217. u8 *ptr = (u8 *)poff + *poff;
  218. if (!*poff || ptr < text || ptr >= text_end)
  219. continue;
  220. /* turn DS segment override prefix into lock prefix */
  221. if (*ptr == 0x3e)
  222. text_poke(ptr, ((unsigned char []){0xf0}), 1);
  223. };
  224. mutex_unlock(&text_mutex);
  225. }
  226. static void alternatives_smp_unlock(const s32 *start, const s32 *end,
  227. u8 *text, u8 *text_end)
  228. {
  229. const s32 *poff;
  230. if (noreplace_smp)
  231. return;
  232. mutex_lock(&text_mutex);
  233. for (poff = start; poff < end; poff++) {
  234. u8 *ptr = (u8 *)poff + *poff;
  235. if (!*poff || ptr < text || ptr >= text_end)
  236. continue;
  237. /* turn lock prefix into DS segment override prefix */
  238. if (*ptr == 0xf0)
  239. text_poke(ptr, ((unsigned char []){0x3E}), 1);
  240. };
  241. mutex_unlock(&text_mutex);
  242. }
  243. struct smp_alt_module {
  244. /* what is this ??? */
  245. struct module *mod;
  246. char *name;
  247. /* ptrs to lock prefixes */
  248. const s32 *locks;
  249. const s32 *locks_end;
  250. /* .text segment, needed to avoid patching init code ;) */
  251. u8 *text;
  252. u8 *text_end;
  253. struct list_head next;
  254. };
  255. static LIST_HEAD(smp_alt_modules);
  256. static DEFINE_MUTEX(smp_alt);
  257. static int smp_mode = 1; /* protected by smp_alt */
  258. void __init_or_module alternatives_smp_module_add(struct module *mod,
  259. char *name,
  260. void *locks, void *locks_end,
  261. void *text, void *text_end)
  262. {
  263. struct smp_alt_module *smp;
  264. if (noreplace_smp)
  265. return;
  266. if (smp_alt_once) {
  267. if (boot_cpu_has(X86_FEATURE_UP))
  268. alternatives_smp_unlock(locks, locks_end,
  269. text, text_end);
  270. return;
  271. }
  272. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  273. if (NULL == smp)
  274. return; /* we'll run the (safe but slow) SMP code then ... */
  275. smp->mod = mod;
  276. smp->name = name;
  277. smp->locks = locks;
  278. smp->locks_end = locks_end;
  279. smp->text = text;
  280. smp->text_end = text_end;
  281. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  282. __func__, smp->locks, smp->locks_end,
  283. smp->text, smp->text_end, smp->name);
  284. mutex_lock(&smp_alt);
  285. list_add_tail(&smp->next, &smp_alt_modules);
  286. if (boot_cpu_has(X86_FEATURE_UP))
  287. alternatives_smp_unlock(smp->locks, smp->locks_end,
  288. smp->text, smp->text_end);
  289. mutex_unlock(&smp_alt);
  290. }
  291. void __init_or_module alternatives_smp_module_del(struct module *mod)
  292. {
  293. struct smp_alt_module *item;
  294. if (smp_alt_once || noreplace_smp)
  295. return;
  296. mutex_lock(&smp_alt);
  297. list_for_each_entry(item, &smp_alt_modules, next) {
  298. if (mod != item->mod)
  299. continue;
  300. list_del(&item->next);
  301. mutex_unlock(&smp_alt);
  302. DPRINTK("%s: %s\n", __func__, item->name);
  303. kfree(item);
  304. return;
  305. }
  306. mutex_unlock(&smp_alt);
  307. }
  308. void alternatives_smp_switch(int smp)
  309. {
  310. struct smp_alt_module *mod;
  311. #ifdef CONFIG_LOCKDEP
  312. /*
  313. * Older binutils section handling bug prevented
  314. * alternatives-replacement from working reliably.
  315. *
  316. * If this still occurs then you should see a hang
  317. * or crash shortly after this line:
  318. */
  319. printk("lockdep: fixing up alternatives.\n");
  320. #endif
  321. if (noreplace_smp || smp_alt_once)
  322. return;
  323. BUG_ON(!smp && (num_online_cpus() > 1));
  324. mutex_lock(&smp_alt);
  325. /*
  326. * Avoid unnecessary switches because it forces JIT based VMs to
  327. * throw away all cached translations, which can be quite costly.
  328. */
  329. if (smp == smp_mode) {
  330. /* nothing */
  331. } else if (smp) {
  332. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  333. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  334. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  335. list_for_each_entry(mod, &smp_alt_modules, next)
  336. alternatives_smp_lock(mod->locks, mod->locks_end,
  337. mod->text, mod->text_end);
  338. } else {
  339. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  340. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  341. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  342. list_for_each_entry(mod, &smp_alt_modules, next)
  343. alternatives_smp_unlock(mod->locks, mod->locks_end,
  344. mod->text, mod->text_end);
  345. }
  346. smp_mode = smp;
  347. mutex_unlock(&smp_alt);
  348. }
  349. /* Return 1 if the address range is reserved for smp-alternatives */
  350. int alternatives_text_reserved(void *start, void *end)
  351. {
  352. struct smp_alt_module *mod;
  353. const s32 *poff;
  354. u8 *text_start = start;
  355. u8 *text_end = end;
  356. list_for_each_entry(mod, &smp_alt_modules, next) {
  357. if (mod->text > text_end || mod->text_end < text_start)
  358. continue;
  359. for (poff = mod->locks; poff < mod->locks_end; poff++) {
  360. const u8 *ptr = (const u8 *)poff + *poff;
  361. if (text_start <= ptr && text_end > ptr)
  362. return 1;
  363. }
  364. }
  365. return 0;
  366. }
  367. #endif
  368. #ifdef CONFIG_PARAVIRT
  369. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  370. struct paravirt_patch_site *end)
  371. {
  372. struct paravirt_patch_site *p;
  373. char insnbuf[MAX_PATCH_LEN];
  374. if (noreplace_paravirt)
  375. return;
  376. for (p = start; p < end; p++) {
  377. unsigned int used;
  378. BUG_ON(p->len > MAX_PATCH_LEN);
  379. /* prep the buffer with the original instructions */
  380. memcpy(insnbuf, p->instr, p->len);
  381. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  382. (unsigned long)p->instr, p->len);
  383. BUG_ON(used > p->len);
  384. /* Pad the rest with nops */
  385. add_nops(insnbuf + used, p->len - used);
  386. text_poke_early(p->instr, insnbuf, p->len);
  387. }
  388. }
  389. extern struct paravirt_patch_site __start_parainstructions[],
  390. __stop_parainstructions[];
  391. #endif /* CONFIG_PARAVIRT */
  392. void __init alternative_instructions(void)
  393. {
  394. /* The patching is not fully atomic, so try to avoid local interruptions
  395. that might execute the to be patched code.
  396. Other CPUs are not running. */
  397. stop_nmi();
  398. /*
  399. * Don't stop machine check exceptions while patching.
  400. * MCEs only happen when something got corrupted and in this
  401. * case we must do something about the corruption.
  402. * Ignoring it is worse than a unlikely patching race.
  403. * Also machine checks tend to be broadcast and if one CPU
  404. * goes into machine check the others follow quickly, so we don't
  405. * expect a machine check to cause undue problems during to code
  406. * patching.
  407. */
  408. apply_alternatives(__alt_instructions, __alt_instructions_end);
  409. /* switch to patch-once-at-boottime-only mode and free the
  410. * tables in case we know the number of CPUs will never ever
  411. * change */
  412. #ifdef CONFIG_HOTPLUG_CPU
  413. if (num_possible_cpus() < 2)
  414. smp_alt_once = 1;
  415. #endif
  416. #ifdef CONFIG_SMP
  417. if (smp_alt_once) {
  418. if (1 == num_possible_cpus()) {
  419. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  420. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  421. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  422. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  423. _text, _etext);
  424. }
  425. } else {
  426. alternatives_smp_module_add(NULL, "core kernel",
  427. __smp_locks, __smp_locks_end,
  428. _text, _etext);
  429. /* Only switch to UP mode if we don't immediately boot others */
  430. if (num_present_cpus() == 1 || setup_max_cpus <= 1)
  431. alternatives_smp_switch(0);
  432. }
  433. #endif
  434. apply_paravirt(__parainstructions, __parainstructions_end);
  435. if (smp_alt_once)
  436. free_init_pages("SMP alternatives",
  437. (unsigned long)__smp_locks,
  438. (unsigned long)__smp_locks_end);
  439. restart_nmi();
  440. }
  441. /**
  442. * text_poke_early - Update instructions on a live kernel at boot time
  443. * @addr: address to modify
  444. * @opcode: source of the copy
  445. * @len: length to copy
  446. *
  447. * When you use this code to patch more than one byte of an instruction
  448. * you need to make sure that other CPUs cannot execute this code in parallel.
  449. * Also no thread must be currently preempted in the middle of these
  450. * instructions. And on the local CPU you need to be protected again NMI or MCE
  451. * handlers seeing an inconsistent instruction while you patch.
  452. */
  453. static void *__init_or_module text_poke_early(void *addr, const void *opcode,
  454. size_t len)
  455. {
  456. unsigned long flags;
  457. local_irq_save(flags);
  458. memcpy(addr, opcode, len);
  459. sync_core();
  460. local_irq_restore(flags);
  461. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  462. that causes hangs on some VIA CPUs. */
  463. return addr;
  464. }
  465. /**
  466. * text_poke - Update instructions on a live kernel
  467. * @addr: address to modify
  468. * @opcode: source of the copy
  469. * @len: length to copy
  470. *
  471. * Only atomic text poke/set should be allowed when not doing early patching.
  472. * It means the size must be writable atomically and the address must be aligned
  473. * in a way that permits an atomic write. It also makes sure we fit on a single
  474. * page.
  475. *
  476. * Note: Must be called under text_mutex.
  477. */
  478. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  479. {
  480. unsigned long flags;
  481. char *vaddr;
  482. struct page *pages[2];
  483. int i;
  484. if (!core_kernel_text((unsigned long)addr)) {
  485. pages[0] = vmalloc_to_page(addr);
  486. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  487. } else {
  488. pages[0] = virt_to_page(addr);
  489. WARN_ON(!PageReserved(pages[0]));
  490. pages[1] = virt_to_page(addr + PAGE_SIZE);
  491. }
  492. BUG_ON(!pages[0]);
  493. local_irq_save(flags);
  494. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  495. if (pages[1])
  496. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  497. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  498. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  499. clear_fixmap(FIX_TEXT_POKE0);
  500. if (pages[1])
  501. clear_fixmap(FIX_TEXT_POKE1);
  502. local_flush_tlb();
  503. sync_core();
  504. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  505. that causes hangs on some VIA CPUs. */
  506. for (i = 0; i < len; i++)
  507. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  508. local_irq_restore(flags);
  509. return addr;
  510. }
  511. /*
  512. * Cross-modifying kernel text with stop_machine().
  513. * This code originally comes from immediate value.
  514. */
  515. static atomic_t stop_machine_first;
  516. static int wrote_text;
  517. struct text_poke_params {
  518. void *addr;
  519. const void *opcode;
  520. size_t len;
  521. };
  522. static int __kprobes stop_machine_text_poke(void *data)
  523. {
  524. struct text_poke_params *tpp = data;
  525. if (atomic_dec_and_test(&stop_machine_first)) {
  526. text_poke(tpp->addr, tpp->opcode, tpp->len);
  527. smp_wmb(); /* Make sure other cpus see that this has run */
  528. wrote_text = 1;
  529. } else {
  530. while (!wrote_text)
  531. cpu_relax();
  532. smp_mb(); /* Load wrote_text before following execution */
  533. }
  534. flush_icache_range((unsigned long)tpp->addr,
  535. (unsigned long)tpp->addr + tpp->len);
  536. return 0;
  537. }
  538. /**
  539. * text_poke_smp - Update instructions on a live kernel on SMP
  540. * @addr: address to modify
  541. * @opcode: source of the copy
  542. * @len: length to copy
  543. *
  544. * Modify multi-byte instruction by using stop_machine() on SMP. This allows
  545. * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
  546. * should be allowed, since stop_machine() does _not_ protect code against
  547. * NMI and MCE.
  548. *
  549. * Note: Must be called under get_online_cpus() and text_mutex.
  550. */
  551. void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
  552. {
  553. struct text_poke_params tpp;
  554. tpp.addr = addr;
  555. tpp.opcode = opcode;
  556. tpp.len = len;
  557. atomic_set(&stop_machine_first, 1);
  558. wrote_text = 0;
  559. stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
  560. return addr;
  561. }