alternative.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. #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, ...) \
  58. do { \
  59. if (debug_alternative) \
  60. printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
  61. } while (0)
  62. /*
  63. * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
  64. * that correspond to that nop. Getting from one nop to the next, we
  65. * add to the array the offset that is equal to the sum of all sizes of
  66. * nops preceding the one we are after.
  67. *
  68. * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
  69. * nice symmetry of sizes of the previous nops.
  70. */
  71. #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
  72. static const unsigned char intelnops[] =
  73. {
  74. GENERIC_NOP1,
  75. GENERIC_NOP2,
  76. GENERIC_NOP3,
  77. GENERIC_NOP4,
  78. GENERIC_NOP5,
  79. GENERIC_NOP6,
  80. GENERIC_NOP7,
  81. GENERIC_NOP8,
  82. GENERIC_NOP5_ATOMIC
  83. };
  84. static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
  85. {
  86. NULL,
  87. intelnops,
  88. intelnops + 1,
  89. intelnops + 1 + 2,
  90. intelnops + 1 + 2 + 3,
  91. intelnops + 1 + 2 + 3 + 4,
  92. intelnops + 1 + 2 + 3 + 4 + 5,
  93. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  94. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  95. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  96. };
  97. #endif
  98. #ifdef K8_NOP1
  99. static const unsigned char k8nops[] =
  100. {
  101. K8_NOP1,
  102. K8_NOP2,
  103. K8_NOP3,
  104. K8_NOP4,
  105. K8_NOP5,
  106. K8_NOP6,
  107. K8_NOP7,
  108. K8_NOP8,
  109. K8_NOP5_ATOMIC
  110. };
  111. static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
  112. {
  113. NULL,
  114. k8nops,
  115. k8nops + 1,
  116. k8nops + 1 + 2,
  117. k8nops + 1 + 2 + 3,
  118. k8nops + 1 + 2 + 3 + 4,
  119. k8nops + 1 + 2 + 3 + 4 + 5,
  120. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  121. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  122. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  123. };
  124. #endif
  125. #if defined(K7_NOP1) && !defined(CONFIG_X86_64)
  126. static const unsigned char k7nops[] =
  127. {
  128. K7_NOP1,
  129. K7_NOP2,
  130. K7_NOP3,
  131. K7_NOP4,
  132. K7_NOP5,
  133. K7_NOP6,
  134. K7_NOP7,
  135. K7_NOP8,
  136. K7_NOP5_ATOMIC
  137. };
  138. static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
  139. {
  140. NULL,
  141. k7nops,
  142. k7nops + 1,
  143. k7nops + 1 + 2,
  144. k7nops + 1 + 2 + 3,
  145. k7nops + 1 + 2 + 3 + 4,
  146. k7nops + 1 + 2 + 3 + 4 + 5,
  147. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  148. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  149. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  150. };
  151. #endif
  152. #ifdef P6_NOP1
  153. static const unsigned char __initconst_or_module p6nops[] =
  154. {
  155. P6_NOP1,
  156. P6_NOP2,
  157. P6_NOP3,
  158. P6_NOP4,
  159. P6_NOP5,
  160. P6_NOP6,
  161. P6_NOP7,
  162. P6_NOP8,
  163. P6_NOP5_ATOMIC
  164. };
  165. static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
  166. {
  167. NULL,
  168. p6nops,
  169. p6nops + 1,
  170. p6nops + 1 + 2,
  171. p6nops + 1 + 2 + 3,
  172. p6nops + 1 + 2 + 3 + 4,
  173. p6nops + 1 + 2 + 3 + 4 + 5,
  174. p6nops + 1 + 2 + 3 + 4 + 5 + 6,
  175. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  176. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  177. };
  178. #endif
  179. /* Initialize these to a safe default */
  180. #ifdef CONFIG_X86_64
  181. const unsigned char * const *ideal_nops = p6_nops;
  182. #else
  183. const unsigned char * const *ideal_nops = intel_nops;
  184. #endif
  185. void __init arch_init_ideal_nops(void)
  186. {
  187. switch (boot_cpu_data.x86_vendor) {
  188. case X86_VENDOR_INTEL:
  189. /*
  190. * Due to a decoder implementation quirk, some
  191. * specific Intel CPUs actually perform better with
  192. * the "k8_nops" than with the SDM-recommended NOPs.
  193. */
  194. if (boot_cpu_data.x86 == 6 &&
  195. boot_cpu_data.x86_model >= 0x0f &&
  196. boot_cpu_data.x86_model != 0x1c &&
  197. boot_cpu_data.x86_model != 0x26 &&
  198. boot_cpu_data.x86_model != 0x27 &&
  199. boot_cpu_data.x86_model < 0x30) {
  200. ideal_nops = k8_nops;
  201. } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
  202. ideal_nops = p6_nops;
  203. } else {
  204. #ifdef CONFIG_X86_64
  205. ideal_nops = k8_nops;
  206. #else
  207. ideal_nops = intel_nops;
  208. #endif
  209. }
  210. default:
  211. #ifdef CONFIG_X86_64
  212. ideal_nops = k8_nops;
  213. #else
  214. if (boot_cpu_has(X86_FEATURE_K8))
  215. ideal_nops = k8_nops;
  216. else if (boot_cpu_has(X86_FEATURE_K7))
  217. ideal_nops = k7_nops;
  218. else
  219. ideal_nops = intel_nops;
  220. #endif
  221. }
  222. }
  223. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  224. static void __init_or_module add_nops(void *insns, unsigned int len)
  225. {
  226. while (len > 0) {
  227. unsigned int noplen = len;
  228. if (noplen > ASM_NOP_MAX)
  229. noplen = ASM_NOP_MAX;
  230. memcpy(insns, ideal_nops[noplen], noplen);
  231. insns += noplen;
  232. len -= noplen;
  233. }
  234. }
  235. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  236. extern s32 __smp_locks[], __smp_locks_end[];
  237. void *text_poke_early(void *addr, const void *opcode, size_t len);
  238. /* Replace instructions with better alternatives for this CPU type.
  239. This runs before SMP is initialized to avoid SMP problems with
  240. self modifying code. This implies that asymmetric systems where
  241. APs have less capabilities than the boot processor are not handled.
  242. Tough. Make sure you disable such features by hand. */
  243. void __init_or_module apply_alternatives(struct alt_instr *start,
  244. struct alt_instr *end)
  245. {
  246. struct alt_instr *a;
  247. u8 *instr, *replacement;
  248. u8 insnbuf[MAX_PATCH_LEN];
  249. DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
  250. /*
  251. * The scan order should be from start to end. A later scanned
  252. * alternative code can overwrite a previous scanned alternative code.
  253. * Some kernel functions (e.g. memcpy, memset, etc) use this order to
  254. * patch code.
  255. *
  256. * So be careful if you want to change the scan order to any other
  257. * order.
  258. */
  259. for (a = start; a < end; a++) {
  260. instr = (u8 *)&a->instr_offset + a->instr_offset;
  261. replacement = (u8 *)&a->repl_offset + a->repl_offset;
  262. BUG_ON(a->replacementlen > a->instrlen);
  263. BUG_ON(a->instrlen > sizeof(insnbuf));
  264. BUG_ON(a->cpuid >= NCAPINTS*32);
  265. if (!boot_cpu_has(a->cpuid))
  266. continue;
  267. memcpy(insnbuf, replacement, a->replacementlen);
  268. /* 0xe8 is a relative jump; fix the offset. */
  269. if (*insnbuf == 0xe8 && a->replacementlen == 5)
  270. *(s32 *)(insnbuf + 1) += replacement - instr;
  271. add_nops(insnbuf + a->replacementlen,
  272. a->instrlen - a->replacementlen);
  273. text_poke_early(instr, insnbuf, a->instrlen);
  274. }
  275. }
  276. #ifdef CONFIG_SMP
  277. static void alternatives_smp_lock(const s32 *start, const s32 *end,
  278. u8 *text, u8 *text_end)
  279. {
  280. const s32 *poff;
  281. mutex_lock(&text_mutex);
  282. for (poff = start; poff < end; poff++) {
  283. u8 *ptr = (u8 *)poff + *poff;
  284. if (!*poff || ptr < text || ptr >= text_end)
  285. continue;
  286. /* turn DS segment override prefix into lock prefix */
  287. if (*ptr == 0x3e)
  288. text_poke(ptr, ((unsigned char []){0xf0}), 1);
  289. };
  290. mutex_unlock(&text_mutex);
  291. }
  292. static void alternatives_smp_unlock(const s32 *start, const s32 *end,
  293. u8 *text, u8 *text_end)
  294. {
  295. const s32 *poff;
  296. if (noreplace_smp)
  297. return;
  298. mutex_lock(&text_mutex);
  299. for (poff = start; poff < end; poff++) {
  300. u8 *ptr = (u8 *)poff + *poff;
  301. if (!*poff || ptr < text || ptr >= text_end)
  302. continue;
  303. /* turn lock prefix into DS segment override prefix */
  304. if (*ptr == 0xf0)
  305. text_poke(ptr, ((unsigned char []){0x3E}), 1);
  306. };
  307. mutex_unlock(&text_mutex);
  308. }
  309. struct smp_alt_module {
  310. /* what is this ??? */
  311. struct module *mod;
  312. char *name;
  313. /* ptrs to lock prefixes */
  314. const s32 *locks;
  315. const s32 *locks_end;
  316. /* .text segment, needed to avoid patching init code ;) */
  317. u8 *text;
  318. u8 *text_end;
  319. struct list_head next;
  320. };
  321. static LIST_HEAD(smp_alt_modules);
  322. static DEFINE_MUTEX(smp_alt);
  323. static int smp_mode = 1; /* protected by smp_alt */
  324. void __init_or_module alternatives_smp_module_add(struct module *mod,
  325. char *name,
  326. void *locks, void *locks_end,
  327. void *text, void *text_end)
  328. {
  329. struct smp_alt_module *smp;
  330. if (noreplace_smp)
  331. return;
  332. if (smp_alt_once) {
  333. if (boot_cpu_has(X86_FEATURE_UP))
  334. alternatives_smp_unlock(locks, locks_end,
  335. text, text_end);
  336. return;
  337. }
  338. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  339. if (NULL == smp)
  340. return; /* we'll run the (safe but slow) SMP code then ... */
  341. smp->mod = mod;
  342. smp->name = name;
  343. smp->locks = locks;
  344. smp->locks_end = locks_end;
  345. smp->text = text;
  346. smp->text_end = text_end;
  347. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  348. __func__, smp->locks, smp->locks_end,
  349. smp->text, smp->text_end, smp->name);
  350. mutex_lock(&smp_alt);
  351. list_add_tail(&smp->next, &smp_alt_modules);
  352. if (boot_cpu_has(X86_FEATURE_UP))
  353. alternatives_smp_unlock(smp->locks, smp->locks_end,
  354. smp->text, smp->text_end);
  355. mutex_unlock(&smp_alt);
  356. }
  357. void __init_or_module alternatives_smp_module_del(struct module *mod)
  358. {
  359. struct smp_alt_module *item;
  360. if (smp_alt_once || noreplace_smp)
  361. return;
  362. mutex_lock(&smp_alt);
  363. list_for_each_entry(item, &smp_alt_modules, next) {
  364. if (mod != item->mod)
  365. continue;
  366. list_del(&item->next);
  367. mutex_unlock(&smp_alt);
  368. DPRINTK("%s: %s\n", __func__, item->name);
  369. kfree(item);
  370. return;
  371. }
  372. mutex_unlock(&smp_alt);
  373. }
  374. bool skip_smp_alternatives;
  375. void alternatives_smp_switch(int smp)
  376. {
  377. struct smp_alt_module *mod;
  378. #ifdef CONFIG_LOCKDEP
  379. /*
  380. * Older binutils section handling bug prevented
  381. * alternatives-replacement from working reliably.
  382. *
  383. * If this still occurs then you should see a hang
  384. * or crash shortly after this line:
  385. */
  386. pr_info("lockdep: fixing up alternatives\n");
  387. #endif
  388. if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
  389. return;
  390. BUG_ON(!smp && (num_online_cpus() > 1));
  391. mutex_lock(&smp_alt);
  392. /*
  393. * Avoid unnecessary switches because it forces JIT based VMs to
  394. * throw away all cached translations, which can be quite costly.
  395. */
  396. if (smp == smp_mode) {
  397. /* nothing */
  398. } else if (smp) {
  399. pr_info("switching to SMP code\n");
  400. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  401. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  402. list_for_each_entry(mod, &smp_alt_modules, next)
  403. alternatives_smp_lock(mod->locks, mod->locks_end,
  404. mod->text, mod->text_end);
  405. } else {
  406. pr_info("switching to UP code\n");
  407. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  408. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  409. list_for_each_entry(mod, &smp_alt_modules, next)
  410. alternatives_smp_unlock(mod->locks, mod->locks_end,
  411. mod->text, mod->text_end);
  412. }
  413. smp_mode = smp;
  414. mutex_unlock(&smp_alt);
  415. }
  416. /* Return 1 if the address range is reserved for smp-alternatives */
  417. int alternatives_text_reserved(void *start, void *end)
  418. {
  419. struct smp_alt_module *mod;
  420. const s32 *poff;
  421. u8 *text_start = start;
  422. u8 *text_end = end;
  423. list_for_each_entry(mod, &smp_alt_modules, next) {
  424. if (mod->text > text_end || mod->text_end < text_start)
  425. continue;
  426. for (poff = mod->locks; poff < mod->locks_end; poff++) {
  427. const u8 *ptr = (const u8 *)poff + *poff;
  428. if (text_start <= ptr && text_end > ptr)
  429. return 1;
  430. }
  431. }
  432. return 0;
  433. }
  434. #endif
  435. #ifdef CONFIG_PARAVIRT
  436. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  437. struct paravirt_patch_site *end)
  438. {
  439. struct paravirt_patch_site *p;
  440. char insnbuf[MAX_PATCH_LEN];
  441. if (noreplace_paravirt)
  442. return;
  443. for (p = start; p < end; p++) {
  444. unsigned int used;
  445. BUG_ON(p->len > MAX_PATCH_LEN);
  446. /* prep the buffer with the original instructions */
  447. memcpy(insnbuf, p->instr, p->len);
  448. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  449. (unsigned long)p->instr, p->len);
  450. BUG_ON(used > p->len);
  451. /* Pad the rest with nops */
  452. add_nops(insnbuf + used, p->len - used);
  453. text_poke_early(p->instr, insnbuf, p->len);
  454. }
  455. }
  456. extern struct paravirt_patch_site __start_parainstructions[],
  457. __stop_parainstructions[];
  458. #endif /* CONFIG_PARAVIRT */
  459. void __init alternative_instructions(void)
  460. {
  461. /* The patching is not fully atomic, so try to avoid local interruptions
  462. that might execute the to be patched code.
  463. Other CPUs are not running. */
  464. stop_nmi();
  465. /*
  466. * Don't stop machine check exceptions while patching.
  467. * MCEs only happen when something got corrupted and in this
  468. * case we must do something about the corruption.
  469. * Ignoring it is worse than a unlikely patching race.
  470. * Also machine checks tend to be broadcast and if one CPU
  471. * goes into machine check the others follow quickly, so we don't
  472. * expect a machine check to cause undue problems during to code
  473. * patching.
  474. */
  475. apply_alternatives(__alt_instructions, __alt_instructions_end);
  476. /* switch to patch-once-at-boottime-only mode and free the
  477. * tables in case we know the number of CPUs will never ever
  478. * change */
  479. #ifdef CONFIG_HOTPLUG_CPU
  480. if (num_possible_cpus() < 2)
  481. smp_alt_once = 1;
  482. #endif
  483. #ifdef CONFIG_SMP
  484. if (smp_alt_once) {
  485. if (1 == num_possible_cpus()) {
  486. pr_info("switching to UP code\n");
  487. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  488. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  489. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  490. _text, _etext);
  491. }
  492. } else {
  493. alternatives_smp_module_add(NULL, "core kernel",
  494. __smp_locks, __smp_locks_end,
  495. _text, _etext);
  496. /* Only switch to UP mode if we don't immediately boot others */
  497. if (num_present_cpus() == 1 || setup_max_cpus <= 1)
  498. alternatives_smp_switch(0);
  499. }
  500. #endif
  501. apply_paravirt(__parainstructions, __parainstructions_end);
  502. if (smp_alt_once)
  503. free_init_pages("SMP alternatives",
  504. (unsigned long)__smp_locks,
  505. (unsigned long)__smp_locks_end);
  506. restart_nmi();
  507. }
  508. /**
  509. * text_poke_early - Update instructions on a live kernel at boot time
  510. * @addr: address to modify
  511. * @opcode: source of the copy
  512. * @len: length to copy
  513. *
  514. * When you use this code to patch more than one byte of an instruction
  515. * you need to make sure that other CPUs cannot execute this code in parallel.
  516. * Also no thread must be currently preempted in the middle of these
  517. * instructions. And on the local CPU you need to be protected again NMI or MCE
  518. * handlers seeing an inconsistent instruction while you patch.
  519. */
  520. void *__init_or_module text_poke_early(void *addr, const void *opcode,
  521. size_t len)
  522. {
  523. unsigned long flags;
  524. local_irq_save(flags);
  525. memcpy(addr, opcode, len);
  526. sync_core();
  527. local_irq_restore(flags);
  528. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  529. that causes hangs on some VIA CPUs. */
  530. return addr;
  531. }
  532. /**
  533. * text_poke - Update instructions on a live kernel
  534. * @addr: address to modify
  535. * @opcode: source of the copy
  536. * @len: length to copy
  537. *
  538. * Only atomic text poke/set should be allowed when not doing early patching.
  539. * It means the size must be writable atomically and the address must be aligned
  540. * in a way that permits an atomic write. It also makes sure we fit on a single
  541. * page.
  542. *
  543. * Note: Must be called under text_mutex.
  544. */
  545. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  546. {
  547. unsigned long flags;
  548. char *vaddr;
  549. struct page *pages[2];
  550. int i;
  551. if (!core_kernel_text((unsigned long)addr)) {
  552. pages[0] = vmalloc_to_page(addr);
  553. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  554. } else {
  555. pages[0] = virt_to_page(addr);
  556. WARN_ON(!PageReserved(pages[0]));
  557. pages[1] = virt_to_page(addr + PAGE_SIZE);
  558. }
  559. BUG_ON(!pages[0]);
  560. local_irq_save(flags);
  561. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  562. if (pages[1])
  563. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  564. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  565. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  566. clear_fixmap(FIX_TEXT_POKE0);
  567. if (pages[1])
  568. clear_fixmap(FIX_TEXT_POKE1);
  569. local_flush_tlb();
  570. sync_core();
  571. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  572. that causes hangs on some VIA CPUs. */
  573. for (i = 0; i < len; i++)
  574. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  575. local_irq_restore(flags);
  576. return addr;
  577. }
  578. /*
  579. * Cross-modifying kernel text with stop_machine().
  580. * This code originally comes from immediate value.
  581. */
  582. static atomic_t stop_machine_first;
  583. static int wrote_text;
  584. struct text_poke_params {
  585. struct text_poke_param *params;
  586. int nparams;
  587. };
  588. static int __kprobes stop_machine_text_poke(void *data)
  589. {
  590. struct text_poke_params *tpp = data;
  591. struct text_poke_param *p;
  592. int i;
  593. if (atomic_xchg(&stop_machine_first, 0)) {
  594. for (i = 0; i < tpp->nparams; i++) {
  595. p = &tpp->params[i];
  596. text_poke(p->addr, p->opcode, p->len);
  597. }
  598. smp_wmb(); /* Make sure other cpus see that this has run */
  599. wrote_text = 1;
  600. } else {
  601. while (!wrote_text)
  602. cpu_relax();
  603. smp_mb(); /* Load wrote_text before following execution */
  604. }
  605. for (i = 0; i < tpp->nparams; i++) {
  606. p = &tpp->params[i];
  607. flush_icache_range((unsigned long)p->addr,
  608. (unsigned long)p->addr + p->len);
  609. }
  610. /*
  611. * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
  612. * that a core serializing instruction such as "cpuid" should be
  613. * executed on _each_ core before the new instruction is made visible.
  614. */
  615. sync_core();
  616. return 0;
  617. }
  618. /**
  619. * text_poke_smp - Update instructions on a live kernel on SMP
  620. * @addr: address to modify
  621. * @opcode: source of the copy
  622. * @len: length to copy
  623. *
  624. * Modify multi-byte instruction by using stop_machine() on SMP. This allows
  625. * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
  626. * should be allowed, since stop_machine() does _not_ protect code against
  627. * NMI and MCE.
  628. *
  629. * Note: Must be called under get_online_cpus() and text_mutex.
  630. */
  631. void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
  632. {
  633. struct text_poke_params tpp;
  634. struct text_poke_param p;
  635. p.addr = addr;
  636. p.opcode = opcode;
  637. p.len = len;
  638. tpp.params = &p;
  639. tpp.nparams = 1;
  640. atomic_set(&stop_machine_first, 1);
  641. wrote_text = 0;
  642. /* Use __stop_machine() because the caller already got online_cpus. */
  643. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  644. return addr;
  645. }
  646. /**
  647. * text_poke_smp_batch - Update instructions on a live kernel on SMP
  648. * @params: an array of text_poke parameters
  649. * @n: the number of elements in params.
  650. *
  651. * Modify multi-byte instruction by using stop_machine() on SMP. Since the
  652. * stop_machine() is heavy task, it is better to aggregate text_poke requests
  653. * and do it once if possible.
  654. *
  655. * Note: Must be called under get_online_cpus() and text_mutex.
  656. */
  657. void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
  658. {
  659. struct text_poke_params tpp = {.params = params, .nparams = n};
  660. atomic_set(&stop_machine_first, 1);
  661. wrote_text = 0;
  662. __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
  663. }