alternative.c 19 KB

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