alternative.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. 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 asymmetric 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. BUG_ON(a->cpuid >= NCAPINTS*32);
  193. if (!boot_cpu_has(a->cpuid))
  194. continue;
  195. #ifdef CONFIG_X86_64
  196. /* vsyscall code is not mapped yet. resolve it manually. */
  197. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  198. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  199. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  200. __func__, a->instr, instr);
  201. }
  202. #endif
  203. memcpy(insnbuf, a->replacement, a->replacementlen);
  204. if (*insnbuf == 0xe8 && a->replacementlen == 5)
  205. *(s32 *)(insnbuf + 1) += a->replacement - a->instr;
  206. add_nops(insnbuf + a->replacementlen,
  207. a->instrlen - a->replacementlen);
  208. text_poke_early(instr, insnbuf, a->instrlen);
  209. }
  210. }
  211. #ifdef CONFIG_SMP
  212. static void alternatives_smp_lock(const s32 *start, const s32 *end,
  213. u8 *text, u8 *text_end)
  214. {
  215. const s32 *poff;
  216. mutex_lock(&text_mutex);
  217. for (poff = start; poff < end; poff++) {
  218. u8 *ptr = (u8 *)poff + *poff;
  219. if (!*poff || ptr < text || ptr >= text_end)
  220. continue;
  221. /* turn DS segment override prefix into lock prefix */
  222. if (*ptr == 0x3e)
  223. text_poke(ptr, ((unsigned char []){0xf0}), 1);
  224. };
  225. mutex_unlock(&text_mutex);
  226. }
  227. static void alternatives_smp_unlock(const s32 *start, const s32 *end,
  228. u8 *text, u8 *text_end)
  229. {
  230. const s32 *poff;
  231. if (noreplace_smp)
  232. return;
  233. mutex_lock(&text_mutex);
  234. for (poff = start; poff < end; poff++) {
  235. u8 *ptr = (u8 *)poff + *poff;
  236. if (!*poff || ptr < text || ptr >= text_end)
  237. continue;
  238. /* turn lock prefix into DS segment override prefix */
  239. if (*ptr == 0xf0)
  240. text_poke(ptr, ((unsigned char []){0x3E}), 1);
  241. };
  242. mutex_unlock(&text_mutex);
  243. }
  244. struct smp_alt_module {
  245. /* what is this ??? */
  246. struct module *mod;
  247. char *name;
  248. /* ptrs to lock prefixes */
  249. const s32 *locks;
  250. const s32 *locks_end;
  251. /* .text segment, needed to avoid patching init code ;) */
  252. u8 *text;
  253. u8 *text_end;
  254. struct list_head next;
  255. };
  256. static LIST_HEAD(smp_alt_modules);
  257. static DEFINE_MUTEX(smp_alt);
  258. static int smp_mode = 1; /* protected by smp_alt */
  259. void __init_or_module alternatives_smp_module_add(struct module *mod,
  260. char *name,
  261. void *locks, void *locks_end,
  262. void *text, void *text_end)
  263. {
  264. struct smp_alt_module *smp;
  265. if (noreplace_smp)
  266. return;
  267. if (smp_alt_once) {
  268. if (boot_cpu_has(X86_FEATURE_UP))
  269. alternatives_smp_unlock(locks, locks_end,
  270. text, text_end);
  271. return;
  272. }
  273. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  274. if (NULL == smp)
  275. return; /* we'll run the (safe but slow) SMP code then ... */
  276. smp->mod = mod;
  277. smp->name = name;
  278. smp->locks = locks;
  279. smp->locks_end = locks_end;
  280. smp->text = text;
  281. smp->text_end = text_end;
  282. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  283. __func__, smp->locks, smp->locks_end,
  284. smp->text, smp->text_end, smp->name);
  285. mutex_lock(&smp_alt);
  286. list_add_tail(&smp->next, &smp_alt_modules);
  287. if (boot_cpu_has(X86_FEATURE_UP))
  288. alternatives_smp_unlock(smp->locks, smp->locks_end,
  289. smp->text, smp->text_end);
  290. mutex_unlock(&smp_alt);
  291. }
  292. void __init_or_module alternatives_smp_module_del(struct module *mod)
  293. {
  294. struct smp_alt_module *item;
  295. if (smp_alt_once || noreplace_smp)
  296. return;
  297. mutex_lock(&smp_alt);
  298. list_for_each_entry(item, &smp_alt_modules, next) {
  299. if (mod != item->mod)
  300. continue;
  301. list_del(&item->next);
  302. mutex_unlock(&smp_alt);
  303. DPRINTK("%s: %s\n", __func__, item->name);
  304. kfree(item);
  305. return;
  306. }
  307. mutex_unlock(&smp_alt);
  308. }
  309. bool skip_smp_alternatives;
  310. void alternatives_smp_switch(int smp)
  311. {
  312. struct smp_alt_module *mod;
  313. #ifdef CONFIG_LOCKDEP
  314. /*
  315. * Older binutils section handling bug prevented
  316. * alternatives-replacement from working reliably.
  317. *
  318. * If this still occurs then you should see a hang
  319. * or crash shortly after this line:
  320. */
  321. printk("lockdep: fixing up alternatives.\n");
  322. #endif
  323. if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
  324. return;
  325. BUG_ON(!smp && (num_online_cpus() > 1));
  326. mutex_lock(&smp_alt);
  327. /*
  328. * Avoid unnecessary switches because it forces JIT based VMs to
  329. * throw away all cached translations, which can be quite costly.
  330. */
  331. if (smp == smp_mode) {
  332. /* nothing */
  333. } else if (smp) {
  334. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  335. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  336. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  337. list_for_each_entry(mod, &smp_alt_modules, next)
  338. alternatives_smp_lock(mod->locks, mod->locks_end,
  339. mod->text, mod->text_end);
  340. } else {
  341. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  342. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  343. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  344. list_for_each_entry(mod, &smp_alt_modules, next)
  345. alternatives_smp_unlock(mod->locks, mod->locks_end,
  346. mod->text, mod->text_end);
  347. }
  348. smp_mode = smp;
  349. mutex_unlock(&smp_alt);
  350. }
  351. /* Return 1 if the address range is reserved for smp-alternatives */
  352. int alternatives_text_reserved(void *start, void *end)
  353. {
  354. struct smp_alt_module *mod;
  355. const s32 *poff;
  356. u8 *text_start = start;
  357. u8 *text_end = end;
  358. list_for_each_entry(mod, &smp_alt_modules, next) {
  359. if (mod->text > text_end || mod->text_end < text_start)
  360. continue;
  361. for (poff = mod->locks; poff < mod->locks_end; poff++) {
  362. const u8 *ptr = (const u8 *)poff + *poff;
  363. if (text_start <= ptr && text_end > ptr)
  364. return 1;
  365. }
  366. }
  367. return 0;
  368. }
  369. #endif
  370. #ifdef CONFIG_PARAVIRT
  371. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  372. struct paravirt_patch_site *end)
  373. {
  374. struct paravirt_patch_site *p;
  375. char insnbuf[MAX_PATCH_LEN];
  376. if (noreplace_paravirt)
  377. return;
  378. for (p = start; p < end; p++) {
  379. unsigned int used;
  380. BUG_ON(p->len > MAX_PATCH_LEN);
  381. /* prep the buffer with the original instructions */
  382. memcpy(insnbuf, p->instr, p->len);
  383. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  384. (unsigned long)p->instr, p->len);
  385. BUG_ON(used > p->len);
  386. /* Pad the rest with nops */
  387. add_nops(insnbuf + used, p->len - used);
  388. text_poke_early(p->instr, insnbuf, p->len);
  389. }
  390. }
  391. extern struct paravirt_patch_site __start_parainstructions[],
  392. __stop_parainstructions[];
  393. #endif /* CONFIG_PARAVIRT */
  394. void __init alternative_instructions(void)
  395. {
  396. /* The patching is not fully atomic, so try to avoid local interruptions
  397. that might execute the to be patched code.
  398. Other CPUs are not running. */
  399. stop_nmi();
  400. /*
  401. * Don't stop machine check exceptions while patching.
  402. * MCEs only happen when something got corrupted and in this
  403. * case we must do something about the corruption.
  404. * Ignoring it is worse than a unlikely patching race.
  405. * Also machine checks tend to be broadcast and if one CPU
  406. * goes into machine check the others follow quickly, so we don't
  407. * expect a machine check to cause undue problems during to code
  408. * patching.
  409. */
  410. apply_alternatives(__alt_instructions, __alt_instructions_end);
  411. /* switch to patch-once-at-boottime-only mode and free the
  412. * tables in case we know the number of CPUs will never ever
  413. * change */
  414. #ifdef CONFIG_HOTPLUG_CPU
  415. if (num_possible_cpus() < 2)
  416. smp_alt_once = 1;
  417. #endif
  418. #ifdef CONFIG_SMP
  419. if (smp_alt_once) {
  420. if (1 == num_possible_cpus()) {
  421. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  422. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  423. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  424. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  425. _text, _etext);
  426. }
  427. } else {
  428. alternatives_smp_module_add(NULL, "core kernel",
  429. __smp_locks, __smp_locks_end,
  430. _text, _etext);
  431. /* Only switch to UP mode if we don't immediately boot others */
  432. if (num_present_cpus() == 1 || setup_max_cpus <= 1)
  433. alternatives_smp_switch(0);
  434. }
  435. #endif
  436. apply_paravirt(__parainstructions, __parainstructions_end);
  437. if (smp_alt_once)
  438. free_init_pages("SMP alternatives",
  439. (unsigned long)__smp_locks,
  440. (unsigned long)__smp_locks_end);
  441. restart_nmi();
  442. }
  443. /**
  444. * text_poke_early - Update instructions on a live kernel at boot time
  445. * @addr: address to modify
  446. * @opcode: source of the copy
  447. * @len: length to copy
  448. *
  449. * When you use this code to patch more than one byte of an instruction
  450. * you need to make sure that other CPUs cannot execute this code in parallel.
  451. * Also no thread must be currently preempted in the middle of these
  452. * instructions. And on the local CPU you need to be protected again NMI or MCE
  453. * handlers seeing an inconsistent instruction while you patch.
  454. */
  455. void *__init_or_module text_poke_early(void *addr, const void *opcode,
  456. size_t len)
  457. {
  458. unsigned long flags;
  459. local_irq_save(flags);
  460. memcpy(addr, opcode, len);
  461. sync_core();
  462. local_irq_restore(flags);
  463. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  464. that causes hangs on some VIA CPUs. */
  465. return addr;
  466. }
  467. /**
  468. * text_poke - Update instructions on a live kernel
  469. * @addr: address to modify
  470. * @opcode: source of the copy
  471. * @len: length to copy
  472. *
  473. * Only atomic text poke/set should be allowed when not doing early patching.
  474. * It means the size must be writable atomically and the address must be aligned
  475. * in a way that permits an atomic write. It also makes sure we fit on a single
  476. * page.
  477. *
  478. * Note: Must be called under text_mutex.
  479. */
  480. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  481. {
  482. unsigned long flags;
  483. char *vaddr;
  484. struct page *pages[2];
  485. int i;
  486. if (!core_kernel_text((unsigned long)addr)) {
  487. pages[0] = vmalloc_to_page(addr);
  488. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  489. } else {
  490. pages[0] = virt_to_page(addr);
  491. WARN_ON(!PageReserved(pages[0]));
  492. pages[1] = virt_to_page(addr + PAGE_SIZE);
  493. }
  494. BUG_ON(!pages[0]);
  495. local_irq_save(flags);
  496. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  497. if (pages[1])
  498. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  499. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  500. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  501. clear_fixmap(FIX_TEXT_POKE0);
  502. if (pages[1])
  503. clear_fixmap(FIX_TEXT_POKE1);
  504. local_flush_tlb();
  505. sync_core();
  506. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  507. that causes hangs on some VIA CPUs. */
  508. for (i = 0; i < len; i++)
  509. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  510. local_irq_restore(flags);
  511. return addr;
  512. }
  513. /*
  514. * Cross-modifying kernel text with stop_machine().
  515. * This code originally comes from immediate value.
  516. */
  517. static atomic_t stop_machine_first;
  518. static int wrote_text;
  519. struct text_poke_params {
  520. struct text_poke_param *params;
  521. int nparams;
  522. };
  523. static int __kprobes stop_machine_text_poke(void *data)
  524. {
  525. struct text_poke_params *tpp = data;
  526. struct text_poke_param *p;
  527. int i;
  528. if (atomic_dec_and_test(&stop_machine_first)) {
  529. for (i = 0; i < tpp->nparams; i++) {
  530. p = &tpp->params[i];
  531. text_poke(p->addr, p->opcode, p->len);
  532. }
  533. smp_wmb(); /* Make sure other cpus see that this has run */
  534. wrote_text = 1;
  535. } else {
  536. while (!wrote_text)
  537. cpu_relax();
  538. smp_mb(); /* Load wrote_text before following execution */
  539. }
  540. for (i = 0; i < tpp->nparams; i++) {
  541. p = &tpp->params[i];
  542. flush_icache_range((unsigned long)p->addr,
  543. (unsigned long)p->addr + p->len);
  544. }
  545. /*
  546. * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
  547. * that a core serializing instruction such as "cpuid" should be
  548. * executed on _each_ core before the new instruction is made visible.
  549. */
  550. sync_core();
  551. return 0;
  552. }
  553. /**
  554. * text_poke_smp - Update instructions on a live kernel on SMP
  555. * @addr: address to modify
  556. * @opcode: source of the copy
  557. * @len: length to copy
  558. *
  559. * Modify multi-byte instruction by using stop_machine() on SMP. This allows
  560. * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
  561. * should be allowed, since stop_machine() does _not_ protect code against
  562. * NMI and MCE.
  563. *
  564. * Note: Must be called under get_online_cpus() and text_mutex.
  565. */
  566. void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
  567. {
  568. struct text_poke_params tpp;
  569. struct text_poke_param p;
  570. p.addr = addr;
  571. p.opcode = opcode;
  572. p.len = len;
  573. tpp.params = &p;
  574. tpp.nparams = 1;
  575. atomic_set(&stop_machine_first, 1);
  576. wrote_text = 0;
  577. /* Use __stop_machine() because the caller already got online_cpus. */
  578. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  579. return addr;
  580. }
  581. /**
  582. * text_poke_smp_batch - Update instructions on a live kernel on SMP
  583. * @params: an array of text_poke parameters
  584. * @n: the number of elements in params.
  585. *
  586. * Modify multi-byte instruction by using stop_machine() on SMP. Since the
  587. * stop_machine() is heavy task, it is better to aggregate text_poke requests
  588. * and do it once if possible.
  589. *
  590. * Note: Must be called under get_online_cpus() and text_mutex.
  591. */
  592. void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
  593. {
  594. struct text_poke_params tpp = {.params = params, .nparams = n};
  595. atomic_set(&stop_machine_first, 1);
  596. wrote_text = 0;
  597. __stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
  598. }
  599. #if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
  600. #ifdef CONFIG_X86_64
  601. unsigned char ideal_nop5[5] = { 0x66, 0x66, 0x66, 0x66, 0x90 };
  602. #else
  603. unsigned char ideal_nop5[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
  604. #endif
  605. void __init arch_init_ideal_nop5(void)
  606. {
  607. /*
  608. * There is no good nop for all x86 archs. This selection
  609. * algorithm should be unified with the one in find_nop_table(),
  610. * but this should be good enough for now.
  611. *
  612. * For cases other than the ones below, use the safe (as in
  613. * always functional) defaults above.
  614. */
  615. #ifdef CONFIG_X86_64
  616. /* Don't use these on 32 bits due to broken virtualizers */
  617. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  618. memcpy(ideal_nop5, p6_nops[5], 5);
  619. #endif
  620. }
  621. #endif