alternative.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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/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 <linux/kdebug.h>
  13. #include <asm/text-patching.h>
  14. #include <asm/alternative.h>
  15. #include <asm/sections.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/mce.h>
  18. #include <asm/nmi.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/io.h>
  22. #include <asm/fixmap.h>
  23. int __read_mostly alternatives_patched;
  24. EXPORT_SYMBOL_GPL(alternatives_patched);
  25. #define MAX_PATCH_LEN (255-1)
  26. static int __initdata_or_module debug_alternative;
  27. static int __init debug_alt(char *str)
  28. {
  29. debug_alternative = 1;
  30. return 1;
  31. }
  32. __setup("debug-alternative", debug_alt);
  33. static int noreplace_smp;
  34. static int __init setup_noreplace_smp(char *str)
  35. {
  36. noreplace_smp = 1;
  37. return 1;
  38. }
  39. __setup("noreplace-smp", setup_noreplace_smp);
  40. #ifdef CONFIG_PARAVIRT
  41. static int __initdata_or_module noreplace_paravirt = 0;
  42. static int __init setup_noreplace_paravirt(char *str)
  43. {
  44. noreplace_paravirt = 1;
  45. return 1;
  46. }
  47. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  48. #endif
  49. #define DPRINTK(fmt, args...) \
  50. do { \
  51. if (debug_alternative) \
  52. printk(KERN_DEBUG "%s: " fmt "\n", __func__, ##args); \
  53. } while (0)
  54. #define DUMP_BYTES(buf, len, fmt, args...) \
  55. do { \
  56. if (unlikely(debug_alternative)) { \
  57. int j; \
  58. \
  59. if (!(len)) \
  60. break; \
  61. \
  62. printk(KERN_DEBUG fmt, ##args); \
  63. for (j = 0; j < (len) - 1; j++) \
  64. printk(KERN_CONT "%02hhx ", buf[j]); \
  65. printk(KERN_CONT "%02hhx\n", buf[j]); \
  66. } \
  67. } while (0)
  68. /*
  69. * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
  70. * that correspond to that nop. Getting from one nop to the next, we
  71. * add to the array the offset that is equal to the sum of all sizes of
  72. * nops preceding the one we are after.
  73. *
  74. * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
  75. * nice symmetry of sizes of the previous nops.
  76. */
  77. #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
  78. static const unsigned char intelnops[] =
  79. {
  80. GENERIC_NOP1,
  81. GENERIC_NOP2,
  82. GENERIC_NOP3,
  83. GENERIC_NOP4,
  84. GENERIC_NOP5,
  85. GENERIC_NOP6,
  86. GENERIC_NOP7,
  87. GENERIC_NOP8,
  88. GENERIC_NOP5_ATOMIC
  89. };
  90. static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
  91. {
  92. NULL,
  93. intelnops,
  94. intelnops + 1,
  95. intelnops + 1 + 2,
  96. intelnops + 1 + 2 + 3,
  97. intelnops + 1 + 2 + 3 + 4,
  98. intelnops + 1 + 2 + 3 + 4 + 5,
  99. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  100. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  101. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  102. };
  103. #endif
  104. #ifdef K8_NOP1
  105. static const unsigned char k8nops[] =
  106. {
  107. K8_NOP1,
  108. K8_NOP2,
  109. K8_NOP3,
  110. K8_NOP4,
  111. K8_NOP5,
  112. K8_NOP6,
  113. K8_NOP7,
  114. K8_NOP8,
  115. K8_NOP5_ATOMIC
  116. };
  117. static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
  118. {
  119. NULL,
  120. k8nops,
  121. k8nops + 1,
  122. k8nops + 1 + 2,
  123. k8nops + 1 + 2 + 3,
  124. k8nops + 1 + 2 + 3 + 4,
  125. k8nops + 1 + 2 + 3 + 4 + 5,
  126. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  127. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  128. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  129. };
  130. #endif
  131. #if defined(K7_NOP1) && !defined(CONFIG_X86_64)
  132. static const unsigned char k7nops[] =
  133. {
  134. K7_NOP1,
  135. K7_NOP2,
  136. K7_NOP3,
  137. K7_NOP4,
  138. K7_NOP5,
  139. K7_NOP6,
  140. K7_NOP7,
  141. K7_NOP8,
  142. K7_NOP5_ATOMIC
  143. };
  144. static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
  145. {
  146. NULL,
  147. k7nops,
  148. k7nops + 1,
  149. k7nops + 1 + 2,
  150. k7nops + 1 + 2 + 3,
  151. k7nops + 1 + 2 + 3 + 4,
  152. k7nops + 1 + 2 + 3 + 4 + 5,
  153. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  154. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  155. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  156. };
  157. #endif
  158. #ifdef P6_NOP1
  159. static const unsigned char p6nops[] =
  160. {
  161. P6_NOP1,
  162. P6_NOP2,
  163. P6_NOP3,
  164. P6_NOP4,
  165. P6_NOP5,
  166. P6_NOP6,
  167. P6_NOP7,
  168. P6_NOP8,
  169. P6_NOP5_ATOMIC
  170. };
  171. static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
  172. {
  173. NULL,
  174. p6nops,
  175. p6nops + 1,
  176. p6nops + 1 + 2,
  177. p6nops + 1 + 2 + 3,
  178. p6nops + 1 + 2 + 3 + 4,
  179. p6nops + 1 + 2 + 3 + 4 + 5,
  180. p6nops + 1 + 2 + 3 + 4 + 5 + 6,
  181. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  182. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  183. };
  184. #endif
  185. /* Initialize these to a safe default */
  186. #ifdef CONFIG_X86_64
  187. const unsigned char * const *ideal_nops = p6_nops;
  188. #else
  189. const unsigned char * const *ideal_nops = intel_nops;
  190. #endif
  191. void __init arch_init_ideal_nops(void)
  192. {
  193. switch (boot_cpu_data.x86_vendor) {
  194. case X86_VENDOR_INTEL:
  195. /*
  196. * Due to a decoder implementation quirk, some
  197. * specific Intel CPUs actually perform better with
  198. * the "k8_nops" than with the SDM-recommended NOPs.
  199. */
  200. if (boot_cpu_data.x86 == 6 &&
  201. boot_cpu_data.x86_model >= 0x0f &&
  202. boot_cpu_data.x86_model != 0x1c &&
  203. boot_cpu_data.x86_model != 0x26 &&
  204. boot_cpu_data.x86_model != 0x27 &&
  205. boot_cpu_data.x86_model < 0x30) {
  206. ideal_nops = k8_nops;
  207. } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
  208. ideal_nops = p6_nops;
  209. } else {
  210. #ifdef CONFIG_X86_64
  211. ideal_nops = k8_nops;
  212. #else
  213. ideal_nops = intel_nops;
  214. #endif
  215. }
  216. break;
  217. case X86_VENDOR_AMD:
  218. if (boot_cpu_data.x86 > 0xf) {
  219. ideal_nops = p6_nops;
  220. return;
  221. }
  222. /* fall through */
  223. default:
  224. #ifdef CONFIG_X86_64
  225. ideal_nops = k8_nops;
  226. #else
  227. if (boot_cpu_has(X86_FEATURE_K8))
  228. ideal_nops = k8_nops;
  229. else if (boot_cpu_has(X86_FEATURE_K7))
  230. ideal_nops = k7_nops;
  231. else
  232. ideal_nops = intel_nops;
  233. #endif
  234. }
  235. }
  236. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  237. static void __init_or_module add_nops(void *insns, unsigned int len)
  238. {
  239. while (len > 0) {
  240. unsigned int noplen = len;
  241. if (noplen > ASM_NOP_MAX)
  242. noplen = ASM_NOP_MAX;
  243. memcpy(insns, ideal_nops[noplen], noplen);
  244. insns += noplen;
  245. len -= noplen;
  246. }
  247. }
  248. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  249. extern s32 __smp_locks[], __smp_locks_end[];
  250. void *text_poke_early(void *addr, const void *opcode, size_t len);
  251. /*
  252. * Are we looking at a near JMP with a 1 or 4-byte displacement.
  253. */
  254. static inline bool is_jmp(const u8 opcode)
  255. {
  256. return opcode == 0xeb || opcode == 0xe9;
  257. }
  258. static void __init_or_module
  259. recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insnbuf)
  260. {
  261. u8 *next_rip, *tgt_rip;
  262. s32 n_dspl, o_dspl;
  263. int repl_len;
  264. if (a->replacementlen != 5)
  265. return;
  266. o_dspl = *(s32 *)(insnbuf + 1);
  267. /* next_rip of the replacement JMP */
  268. next_rip = repl_insn + a->replacementlen;
  269. /* target rip of the replacement JMP */
  270. tgt_rip = next_rip + o_dspl;
  271. n_dspl = tgt_rip - orig_insn;
  272. DPRINTK("target RIP: %p, new_displ: 0x%x", tgt_rip, n_dspl);
  273. if (tgt_rip - orig_insn >= 0) {
  274. if (n_dspl - 2 <= 127)
  275. goto two_byte_jmp;
  276. else
  277. goto five_byte_jmp;
  278. /* negative offset */
  279. } else {
  280. if (((n_dspl - 2) & 0xff) == (n_dspl - 2))
  281. goto two_byte_jmp;
  282. else
  283. goto five_byte_jmp;
  284. }
  285. two_byte_jmp:
  286. n_dspl -= 2;
  287. insnbuf[0] = 0xeb;
  288. insnbuf[1] = (s8)n_dspl;
  289. add_nops(insnbuf + 2, 3);
  290. repl_len = 2;
  291. goto done;
  292. five_byte_jmp:
  293. n_dspl -= 5;
  294. insnbuf[0] = 0xe9;
  295. *(s32 *)&insnbuf[1] = n_dspl;
  296. repl_len = 5;
  297. done:
  298. DPRINTK("final displ: 0x%08x, JMP 0x%lx",
  299. n_dspl, (unsigned long)orig_insn + n_dspl + repl_len);
  300. }
  301. /*
  302. * "noinline" to cause control flow change and thus invalidate I$ and
  303. * cause refetch after modification.
  304. */
  305. static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr)
  306. {
  307. unsigned long flags;
  308. if (instr[0] != 0x90)
  309. return;
  310. local_irq_save(flags);
  311. add_nops(instr + (a->instrlen - a->padlen), a->padlen);
  312. local_irq_restore(flags);
  313. DUMP_BYTES(instr, a->instrlen, "%p: [%d:%d) optimized NOPs: ",
  314. instr, a->instrlen - a->padlen, a->padlen);
  315. }
  316. /*
  317. * Replace instructions with better alternatives for this CPU type. This runs
  318. * before SMP is initialized to avoid SMP problems with self modifying code.
  319. * This implies that asymmetric systems where APs have less capabilities than
  320. * the boot processor are not handled. Tough. Make sure you disable such
  321. * features by hand.
  322. *
  323. * Marked "noinline" to cause control flow change and thus insn cache
  324. * to refetch changed I$ lines.
  325. */
  326. void __init_or_module noinline apply_alternatives(struct alt_instr *start,
  327. struct alt_instr *end)
  328. {
  329. struct alt_instr *a;
  330. u8 *instr, *replacement;
  331. u8 insnbuf[MAX_PATCH_LEN];
  332. DPRINTK("alt table %p -> %p", start, end);
  333. /*
  334. * The scan order should be from start to end. A later scanned
  335. * alternative code can overwrite previously scanned alternative code.
  336. * Some kernel functions (e.g. memcpy, memset, etc) use this order to
  337. * patch code.
  338. *
  339. * So be careful if you want to change the scan order to any other
  340. * order.
  341. */
  342. for (a = start; a < end; a++) {
  343. int insnbuf_sz = 0;
  344. instr = (u8 *)&a->instr_offset + a->instr_offset;
  345. replacement = (u8 *)&a->repl_offset + a->repl_offset;
  346. BUG_ON(a->instrlen > sizeof(insnbuf));
  347. BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
  348. if (!boot_cpu_has(a->cpuid)) {
  349. if (a->padlen > 1)
  350. optimize_nops(a, instr);
  351. continue;
  352. }
  353. DPRINTK("feat: %d*32+%d, old: (%p, len: %d), repl: (%p, len: %d), pad: %d",
  354. a->cpuid >> 5,
  355. a->cpuid & 0x1f,
  356. instr, a->instrlen,
  357. replacement, a->replacementlen, a->padlen);
  358. DUMP_BYTES(instr, a->instrlen, "%p: old_insn: ", instr);
  359. DUMP_BYTES(replacement, a->replacementlen, "%p: rpl_insn: ", replacement);
  360. memcpy(insnbuf, replacement, a->replacementlen);
  361. insnbuf_sz = a->replacementlen;
  362. /*
  363. * 0xe8 is a relative jump; fix the offset.
  364. *
  365. * Instruction length is checked before the opcode to avoid
  366. * accessing uninitialized bytes for zero-length replacements.
  367. */
  368. if (a->replacementlen == 5 && *insnbuf == 0xe8) {
  369. *(s32 *)(insnbuf + 1) += replacement - instr;
  370. DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
  371. *(s32 *)(insnbuf + 1),
  372. (unsigned long)instr + *(s32 *)(insnbuf + 1) + 5);
  373. }
  374. if (a->replacementlen && is_jmp(replacement[0]))
  375. recompute_jump(a, instr, replacement, insnbuf);
  376. if (a->instrlen > a->replacementlen) {
  377. add_nops(insnbuf + a->replacementlen,
  378. a->instrlen - a->replacementlen);
  379. insnbuf_sz += a->instrlen - a->replacementlen;
  380. }
  381. DUMP_BYTES(insnbuf, insnbuf_sz, "%p: final_insn: ", instr);
  382. text_poke_early(instr, insnbuf, insnbuf_sz);
  383. }
  384. }
  385. #ifdef CONFIG_SMP
  386. static void alternatives_smp_lock(const s32 *start, const s32 *end,
  387. u8 *text, u8 *text_end)
  388. {
  389. const s32 *poff;
  390. mutex_lock(&text_mutex);
  391. for (poff = start; poff < end; poff++) {
  392. u8 *ptr = (u8 *)poff + *poff;
  393. if (!*poff || ptr < text || ptr >= text_end)
  394. continue;
  395. /* turn DS segment override prefix into lock prefix */
  396. if (*ptr == 0x3e)
  397. text_poke(ptr, ((unsigned char []){0xf0}), 1);
  398. }
  399. mutex_unlock(&text_mutex);
  400. }
  401. static void alternatives_smp_unlock(const s32 *start, const s32 *end,
  402. u8 *text, u8 *text_end)
  403. {
  404. const s32 *poff;
  405. mutex_lock(&text_mutex);
  406. for (poff = start; poff < end; poff++) {
  407. u8 *ptr = (u8 *)poff + *poff;
  408. if (!*poff || ptr < text || ptr >= text_end)
  409. continue;
  410. /* turn lock prefix into DS segment override prefix */
  411. if (*ptr == 0xf0)
  412. text_poke(ptr, ((unsigned char []){0x3E}), 1);
  413. }
  414. mutex_unlock(&text_mutex);
  415. }
  416. struct smp_alt_module {
  417. /* what is this ??? */
  418. struct module *mod;
  419. char *name;
  420. /* ptrs to lock prefixes */
  421. const s32 *locks;
  422. const s32 *locks_end;
  423. /* .text segment, needed to avoid patching init code ;) */
  424. u8 *text;
  425. u8 *text_end;
  426. struct list_head next;
  427. };
  428. static LIST_HEAD(smp_alt_modules);
  429. static DEFINE_MUTEX(smp_alt);
  430. static bool uniproc_patched = false; /* protected by smp_alt */
  431. void __init_or_module alternatives_smp_module_add(struct module *mod,
  432. char *name,
  433. void *locks, void *locks_end,
  434. void *text, void *text_end)
  435. {
  436. struct smp_alt_module *smp;
  437. mutex_lock(&smp_alt);
  438. if (!uniproc_patched)
  439. goto unlock;
  440. if (num_possible_cpus() == 1)
  441. /* Don't bother remembering, we'll never have to undo it. */
  442. goto smp_unlock;
  443. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  444. if (NULL == smp)
  445. /* we'll run the (safe but slow) SMP code then ... */
  446. goto unlock;
  447. smp->mod = mod;
  448. smp->name = name;
  449. smp->locks = locks;
  450. smp->locks_end = locks_end;
  451. smp->text = text;
  452. smp->text_end = text_end;
  453. DPRINTK("locks %p -> %p, text %p -> %p, name %s\n",
  454. smp->locks, smp->locks_end,
  455. smp->text, smp->text_end, smp->name);
  456. list_add_tail(&smp->next, &smp_alt_modules);
  457. smp_unlock:
  458. alternatives_smp_unlock(locks, locks_end, text, text_end);
  459. unlock:
  460. mutex_unlock(&smp_alt);
  461. }
  462. void __init_or_module alternatives_smp_module_del(struct module *mod)
  463. {
  464. struct smp_alt_module *item;
  465. mutex_lock(&smp_alt);
  466. list_for_each_entry(item, &smp_alt_modules, next) {
  467. if (mod != item->mod)
  468. continue;
  469. list_del(&item->next);
  470. kfree(item);
  471. break;
  472. }
  473. mutex_unlock(&smp_alt);
  474. }
  475. void alternatives_enable_smp(void)
  476. {
  477. struct smp_alt_module *mod;
  478. /* Why bother if there are no other CPUs? */
  479. BUG_ON(num_possible_cpus() == 1);
  480. mutex_lock(&smp_alt);
  481. if (uniproc_patched) {
  482. pr_info("switching to SMP code\n");
  483. BUG_ON(num_online_cpus() != 1);
  484. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  485. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  486. list_for_each_entry(mod, &smp_alt_modules, next)
  487. alternatives_smp_lock(mod->locks, mod->locks_end,
  488. mod->text, mod->text_end);
  489. uniproc_patched = false;
  490. }
  491. mutex_unlock(&smp_alt);
  492. }
  493. /* Return 1 if the address range is reserved for smp-alternatives */
  494. int alternatives_text_reserved(void *start, void *end)
  495. {
  496. struct smp_alt_module *mod;
  497. const s32 *poff;
  498. u8 *text_start = start;
  499. u8 *text_end = end;
  500. list_for_each_entry(mod, &smp_alt_modules, next) {
  501. if (mod->text > text_end || mod->text_end < text_start)
  502. continue;
  503. for (poff = mod->locks; poff < mod->locks_end; poff++) {
  504. const u8 *ptr = (const u8 *)poff + *poff;
  505. if (text_start <= ptr && text_end > ptr)
  506. return 1;
  507. }
  508. }
  509. return 0;
  510. }
  511. #endif /* CONFIG_SMP */
  512. #ifdef CONFIG_PARAVIRT
  513. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  514. struct paravirt_patch_site *end)
  515. {
  516. struct paravirt_patch_site *p;
  517. char insnbuf[MAX_PATCH_LEN];
  518. if (noreplace_paravirt)
  519. return;
  520. for (p = start; p < end; p++) {
  521. unsigned int used;
  522. BUG_ON(p->len > MAX_PATCH_LEN);
  523. /* prep the buffer with the original instructions */
  524. memcpy(insnbuf, p->instr, p->len);
  525. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  526. (unsigned long)p->instr, p->len);
  527. BUG_ON(used > p->len);
  528. /* Pad the rest with nops */
  529. add_nops(insnbuf + used, p->len - used);
  530. text_poke_early(p->instr, insnbuf, p->len);
  531. }
  532. }
  533. extern struct paravirt_patch_site __start_parainstructions[],
  534. __stop_parainstructions[];
  535. #endif /* CONFIG_PARAVIRT */
  536. void __init alternative_instructions(void)
  537. {
  538. /* The patching is not fully atomic, so try to avoid local interruptions
  539. that might execute the to be patched code.
  540. Other CPUs are not running. */
  541. stop_nmi();
  542. /*
  543. * Don't stop machine check exceptions while patching.
  544. * MCEs only happen when something got corrupted and in this
  545. * case we must do something about the corruption.
  546. * Ignoring it is worse than a unlikely patching race.
  547. * Also machine checks tend to be broadcast and if one CPU
  548. * goes into machine check the others follow quickly, so we don't
  549. * expect a machine check to cause undue problems during to code
  550. * patching.
  551. */
  552. apply_alternatives(__alt_instructions, __alt_instructions_end);
  553. #ifdef CONFIG_SMP
  554. /* Patch to UP if other cpus not imminent. */
  555. if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
  556. uniproc_patched = true;
  557. alternatives_smp_module_add(NULL, "core kernel",
  558. __smp_locks, __smp_locks_end,
  559. _text, _etext);
  560. }
  561. if (!uniproc_patched || num_possible_cpus() == 1)
  562. free_init_pages("SMP alternatives",
  563. (unsigned long)__smp_locks,
  564. (unsigned long)__smp_locks_end);
  565. #endif
  566. apply_paravirt(__parainstructions, __parainstructions_end);
  567. restart_nmi();
  568. alternatives_patched = 1;
  569. }
  570. /**
  571. * text_poke_early - Update instructions on a live kernel at boot time
  572. * @addr: address to modify
  573. * @opcode: source of the copy
  574. * @len: length to copy
  575. *
  576. * When you use this code to patch more than one byte of an instruction
  577. * you need to make sure that other CPUs cannot execute this code in parallel.
  578. * Also no thread must be currently preempted in the middle of these
  579. * instructions. And on the local CPU you need to be protected again NMI or MCE
  580. * handlers seeing an inconsistent instruction while you patch.
  581. */
  582. void *__init_or_module text_poke_early(void *addr, const void *opcode,
  583. size_t len)
  584. {
  585. unsigned long flags;
  586. local_irq_save(flags);
  587. memcpy(addr, opcode, len);
  588. local_irq_restore(flags);
  589. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  590. that causes hangs on some VIA CPUs. */
  591. return addr;
  592. }
  593. /**
  594. * text_poke - Update instructions on a live kernel
  595. * @addr: address to modify
  596. * @opcode: source of the copy
  597. * @len: length to copy
  598. *
  599. * Only atomic text poke/set should be allowed when not doing early patching.
  600. * It means the size must be writable atomically and the address must be aligned
  601. * in a way that permits an atomic write. It also makes sure we fit on a single
  602. * page.
  603. *
  604. * Note: Must be called under text_mutex.
  605. */
  606. void *text_poke(void *addr, const void *opcode, size_t len)
  607. {
  608. unsigned long flags;
  609. char *vaddr;
  610. struct page *pages[2];
  611. int i;
  612. if (!core_kernel_text((unsigned long)addr)) {
  613. pages[0] = vmalloc_to_page(addr);
  614. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  615. } else {
  616. pages[0] = virt_to_page(addr);
  617. WARN_ON(!PageReserved(pages[0]));
  618. pages[1] = virt_to_page(addr + PAGE_SIZE);
  619. }
  620. BUG_ON(!pages[0]);
  621. local_irq_save(flags);
  622. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  623. if (pages[1])
  624. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  625. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  626. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  627. clear_fixmap(FIX_TEXT_POKE0);
  628. if (pages[1])
  629. clear_fixmap(FIX_TEXT_POKE1);
  630. local_flush_tlb();
  631. sync_core();
  632. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  633. that causes hangs on some VIA CPUs. */
  634. for (i = 0; i < len; i++)
  635. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  636. local_irq_restore(flags);
  637. return addr;
  638. }
  639. static void do_sync_core(void *info)
  640. {
  641. sync_core();
  642. }
  643. static bool bp_patching_in_progress;
  644. static void *bp_int3_handler, *bp_int3_addr;
  645. int poke_int3_handler(struct pt_regs *regs)
  646. {
  647. /* bp_patching_in_progress */
  648. smp_rmb();
  649. if (likely(!bp_patching_in_progress))
  650. return 0;
  651. if (user_mode(regs) || regs->ip != (unsigned long)bp_int3_addr)
  652. return 0;
  653. /* set up the specified breakpoint handler */
  654. regs->ip = (unsigned long) bp_int3_handler;
  655. return 1;
  656. }
  657. /**
  658. * text_poke_bp() -- update instructions on live kernel on SMP
  659. * @addr: address to patch
  660. * @opcode: opcode of new instruction
  661. * @len: length to copy
  662. * @handler: address to jump to when the temporary breakpoint is hit
  663. *
  664. * Modify multi-byte instruction by using int3 breakpoint on SMP.
  665. * We completely avoid stop_machine() here, and achieve the
  666. * synchronization using int3 breakpoint.
  667. *
  668. * The way it is done:
  669. * - add a int3 trap to the address that will be patched
  670. * - sync cores
  671. * - update all but the first byte of the patched range
  672. * - sync cores
  673. * - replace the first byte (int3) by the first byte of
  674. * replacing opcode
  675. * - sync cores
  676. *
  677. * Note: must be called under text_mutex.
  678. */
  679. void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
  680. {
  681. unsigned char int3 = 0xcc;
  682. bp_int3_handler = handler;
  683. bp_int3_addr = (u8 *)addr + sizeof(int3);
  684. bp_patching_in_progress = true;
  685. /*
  686. * Corresponding read barrier in int3 notifier for
  687. * making sure the in_progress flags is correctly ordered wrt.
  688. * patching
  689. */
  690. smp_wmb();
  691. text_poke(addr, &int3, sizeof(int3));
  692. on_each_cpu(do_sync_core, NULL, 1);
  693. if (len - sizeof(int3) > 0) {
  694. /* patch all but the first byte */
  695. text_poke((char *)addr + sizeof(int3),
  696. (const char *) opcode + sizeof(int3),
  697. len - sizeof(int3));
  698. /*
  699. * According to Intel, this core syncing is very likely
  700. * not necessary and we'd be safe even without it. But
  701. * better safe than sorry (plus there's not only Intel).
  702. */
  703. on_each_cpu(do_sync_core, NULL, 1);
  704. }
  705. /* patch the first byte */
  706. text_poke(addr, opcode, sizeof(int3));
  707. on_each_cpu(do_sync_core, NULL, 1);
  708. bp_patching_in_progress = false;
  709. smp_wmb();
  710. return addr;
  711. }