alternative.c 19 KB

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