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