alternative.c 20 KB

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