alternative.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/mutex.h>
  4. #include <linux/list.h>
  5. #include <linux/stringify.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/mm.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/memory.h>
  10. #include <asm/alternative.h>
  11. #include <asm/sections.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/mce.h>
  14. #include <asm/nmi.h>
  15. #include <asm/vsyscall.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/io.h>
  19. #include <asm/fixmap.h>
  20. #define MAX_PATCH_LEN (255-1)
  21. #ifdef CONFIG_HOTPLUG_CPU
  22. static int smp_alt_once;
  23. static int __init bootonly(char *str)
  24. {
  25. smp_alt_once = 1;
  26. return 1;
  27. }
  28. __setup("smp-alt-boot", bootonly);
  29. #else
  30. #define smp_alt_once 1
  31. #endif
  32. static int __initdata_or_module debug_alternative;
  33. static int __init debug_alt(char *str)
  34. {
  35. debug_alternative = 1;
  36. return 1;
  37. }
  38. __setup("debug-alternative", debug_alt);
  39. static int noreplace_smp;
  40. static int __init setup_noreplace_smp(char *str)
  41. {
  42. noreplace_smp = 1;
  43. return 1;
  44. }
  45. __setup("noreplace-smp", setup_noreplace_smp);
  46. #ifdef CONFIG_PARAVIRT
  47. static int __initdata_or_module noreplace_paravirt = 0;
  48. static int __init setup_noreplace_paravirt(char *str)
  49. {
  50. noreplace_paravirt = 1;
  51. return 1;
  52. }
  53. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  54. #endif
  55. #define DPRINTK(fmt, args...) if (debug_alternative) \
  56. printk(KERN_DEBUG fmt, args)
  57. #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
  58. /* Use inline assembly to define this because the nops are defined
  59. as inline assembly strings in the include files and we cannot
  60. get them easily into strings. */
  61. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nintelnops: "
  62. GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
  63. GENERIC_NOP7 GENERIC_NOP8
  64. "\t.previous");
  65. extern const unsigned char intelnops[];
  66. static const unsigned char *const __initconst_or_module
  67. intel_nops[ASM_NOP_MAX+1] = {
  68. NULL,
  69. intelnops,
  70. intelnops + 1,
  71. intelnops + 1 + 2,
  72. intelnops + 1 + 2 + 3,
  73. intelnops + 1 + 2 + 3 + 4,
  74. intelnops + 1 + 2 + 3 + 4 + 5,
  75. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  76. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  77. };
  78. #endif
  79. #ifdef K8_NOP1
  80. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nk8nops: "
  81. K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
  82. K8_NOP7 K8_NOP8
  83. "\t.previous");
  84. extern const unsigned char k8nops[];
  85. static const unsigned char *const __initconst_or_module
  86. k8_nops[ASM_NOP_MAX+1] = {
  87. NULL,
  88. k8nops,
  89. k8nops + 1,
  90. k8nops + 1 + 2,
  91. k8nops + 1 + 2 + 3,
  92. k8nops + 1 + 2 + 3 + 4,
  93. k8nops + 1 + 2 + 3 + 4 + 5,
  94. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  95. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  96. };
  97. #endif
  98. #if defined(K7_NOP1) && !defined(CONFIG_X86_64)
  99. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\nk7nops: "
  100. K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
  101. K7_NOP7 K7_NOP8
  102. "\t.previous");
  103. extern const unsigned char k7nops[];
  104. static const unsigned char *const __initconst_or_module
  105. k7_nops[ASM_NOP_MAX+1] = {
  106. NULL,
  107. k7nops,
  108. k7nops + 1,
  109. k7nops + 1 + 2,
  110. k7nops + 1 + 2 + 3,
  111. k7nops + 1 + 2 + 3 + 4,
  112. k7nops + 1 + 2 + 3 + 4 + 5,
  113. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  114. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  115. };
  116. #endif
  117. #ifdef P6_NOP1
  118. asm("\t" __stringify(__INITRODATA_OR_MODULE) "\np6nops: "
  119. P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
  120. P6_NOP7 P6_NOP8
  121. "\t.previous");
  122. extern const unsigned char p6nops[];
  123. static const unsigned char *const __initconst_or_module
  124. p6_nops[ASM_NOP_MAX+1] = {
  125. NULL,
  126. p6nops,
  127. p6nops + 1,
  128. p6nops + 1 + 2,
  129. p6nops + 1 + 2 + 3,
  130. p6nops + 1 + 2 + 3 + 4,
  131. p6nops + 1 + 2 + 3 + 4 + 5,
  132. p6nops + 1 + 2 + 3 + 4 + 5 + 6,
  133. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  134. };
  135. #endif
  136. #ifdef CONFIG_X86_64
  137. extern char __vsyscall_0;
  138. static const unsigned char *const *__init_or_module find_nop_table(void)
  139. {
  140. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  141. boot_cpu_has(X86_FEATURE_NOPL))
  142. return p6_nops;
  143. else
  144. return k8_nops;
  145. }
  146. #else /* CONFIG_X86_64 */
  147. static const unsigned char *const *__init_or_module find_nop_table(void)
  148. {
  149. if (boot_cpu_has(X86_FEATURE_K8))
  150. return k8_nops;
  151. else if (boot_cpu_has(X86_FEATURE_K7))
  152. return k7_nops;
  153. else if (boot_cpu_has(X86_FEATURE_NOPL))
  154. return p6_nops;
  155. else
  156. return intel_nops;
  157. }
  158. #endif /* CONFIG_X86_64 */
  159. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  160. static void __init_or_module add_nops(void *insns, unsigned int len)
  161. {
  162. const unsigned char *const *noptable = find_nop_table();
  163. while (len > 0) {
  164. unsigned int noplen = len;
  165. if (noplen > ASM_NOP_MAX)
  166. noplen = ASM_NOP_MAX;
  167. memcpy(insns, noptable[noplen], noplen);
  168. insns += noplen;
  169. len -= noplen;
  170. }
  171. }
  172. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  173. extern u8 *__smp_locks[], *__smp_locks_end[];
  174. static void *text_poke_early(void *addr, const void *opcode, size_t len);
  175. /* Replace instructions with better alternatives for this CPU type.
  176. This runs before SMP is initialized to avoid SMP problems with
  177. self modifying code. This implies that assymetric systems where
  178. APs have less capabilities than the boot processor are not handled.
  179. Tough. Make sure you disable such features by hand. */
  180. void __init_or_module apply_alternatives(struct alt_instr *start,
  181. struct alt_instr *end)
  182. {
  183. struct alt_instr *a;
  184. char insnbuf[MAX_PATCH_LEN];
  185. DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
  186. for (a = start; a < end; a++) {
  187. u8 *instr = a->instr;
  188. BUG_ON(a->replacementlen > a->instrlen);
  189. BUG_ON(a->instrlen > sizeof(insnbuf));
  190. if (!boot_cpu_has(a->cpuid))
  191. continue;
  192. #ifdef CONFIG_X86_64
  193. /* vsyscall code is not mapped yet. resolve it manually. */
  194. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  195. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  196. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  197. __func__, a->instr, instr);
  198. }
  199. #endif
  200. memcpy(insnbuf, a->replacement, a->replacementlen);
  201. add_nops(insnbuf + a->replacementlen,
  202. a->instrlen - a->replacementlen);
  203. text_poke_early(instr, insnbuf, a->instrlen);
  204. }
  205. }
  206. #ifdef CONFIG_SMP
  207. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  208. {
  209. u8 **ptr;
  210. mutex_lock(&text_mutex);
  211. for (ptr = start; ptr < end; ptr++) {
  212. if (*ptr < text)
  213. continue;
  214. if (*ptr > text_end)
  215. continue;
  216. /* turn DS segment override prefix into lock prefix */
  217. text_poke(*ptr, ((unsigned char []){0xf0}), 1);
  218. };
  219. mutex_unlock(&text_mutex);
  220. }
  221. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  222. {
  223. u8 **ptr;
  224. if (noreplace_smp)
  225. return;
  226. mutex_lock(&text_mutex);
  227. for (ptr = start; ptr < end; ptr++) {
  228. if (*ptr < text)
  229. continue;
  230. if (*ptr > text_end)
  231. continue;
  232. /* turn lock prefix into DS segment override prefix */
  233. text_poke(*ptr, ((unsigned char []){0x3E}), 1);
  234. };
  235. mutex_unlock(&text_mutex);
  236. }
  237. struct smp_alt_module {
  238. /* what is this ??? */
  239. struct module *mod;
  240. char *name;
  241. /* ptrs to lock prefixes */
  242. u8 **locks;
  243. u8 **locks_end;
  244. /* .text segment, needed to avoid patching init code ;) */
  245. u8 *text;
  246. u8 *text_end;
  247. struct list_head next;
  248. };
  249. static LIST_HEAD(smp_alt_modules);
  250. static DEFINE_MUTEX(smp_alt);
  251. static int smp_mode = 1; /* protected by smp_alt */
  252. void __init_or_module alternatives_smp_module_add(struct module *mod,
  253. char *name,
  254. void *locks, void *locks_end,
  255. void *text, void *text_end)
  256. {
  257. struct smp_alt_module *smp;
  258. if (noreplace_smp)
  259. return;
  260. if (smp_alt_once) {
  261. if (boot_cpu_has(X86_FEATURE_UP))
  262. alternatives_smp_unlock(locks, locks_end,
  263. text, text_end);
  264. return;
  265. }
  266. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  267. if (NULL == smp)
  268. return; /* we'll run the (safe but slow) SMP code then ... */
  269. smp->mod = mod;
  270. smp->name = name;
  271. smp->locks = locks;
  272. smp->locks_end = locks_end;
  273. smp->text = text;
  274. smp->text_end = text_end;
  275. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  276. __func__, smp->locks, smp->locks_end,
  277. smp->text, smp->text_end, smp->name);
  278. mutex_lock(&smp_alt);
  279. list_add_tail(&smp->next, &smp_alt_modules);
  280. if (boot_cpu_has(X86_FEATURE_UP))
  281. alternatives_smp_unlock(smp->locks, smp->locks_end,
  282. smp->text, smp->text_end);
  283. mutex_unlock(&smp_alt);
  284. }
  285. void __init_or_module alternatives_smp_module_del(struct module *mod)
  286. {
  287. struct smp_alt_module *item;
  288. if (smp_alt_once || noreplace_smp)
  289. return;
  290. mutex_lock(&smp_alt);
  291. list_for_each_entry(item, &smp_alt_modules, next) {
  292. if (mod != item->mod)
  293. continue;
  294. list_del(&item->next);
  295. mutex_unlock(&smp_alt);
  296. DPRINTK("%s: %s\n", __func__, item->name);
  297. kfree(item);
  298. return;
  299. }
  300. mutex_unlock(&smp_alt);
  301. }
  302. void alternatives_smp_switch(int smp)
  303. {
  304. struct smp_alt_module *mod;
  305. #ifdef CONFIG_LOCKDEP
  306. /*
  307. * Older binutils section handling bug prevented
  308. * alternatives-replacement from working reliably.
  309. *
  310. * If this still occurs then you should see a hang
  311. * or crash shortly after this line:
  312. */
  313. printk("lockdep: fixing up alternatives.\n");
  314. #endif
  315. if (noreplace_smp || smp_alt_once)
  316. return;
  317. BUG_ON(!smp && (num_online_cpus() > 1));
  318. mutex_lock(&smp_alt);
  319. /*
  320. * Avoid unnecessary switches because it forces JIT based VMs to
  321. * throw away all cached translations, which can be quite costly.
  322. */
  323. if (smp == smp_mode) {
  324. /* nothing */
  325. } else if (smp) {
  326. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  327. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  328. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  329. list_for_each_entry(mod, &smp_alt_modules, next)
  330. alternatives_smp_lock(mod->locks, mod->locks_end,
  331. mod->text, mod->text_end);
  332. } else {
  333. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  334. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  335. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  336. list_for_each_entry(mod, &smp_alt_modules, next)
  337. alternatives_smp_unlock(mod->locks, mod->locks_end,
  338. mod->text, mod->text_end);
  339. }
  340. smp_mode = smp;
  341. mutex_unlock(&smp_alt);
  342. }
  343. #endif
  344. #ifdef CONFIG_PARAVIRT
  345. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  346. struct paravirt_patch_site *end)
  347. {
  348. struct paravirt_patch_site *p;
  349. char insnbuf[MAX_PATCH_LEN];
  350. if (noreplace_paravirt)
  351. return;
  352. for (p = start; p < end; p++) {
  353. unsigned int used;
  354. BUG_ON(p->len > MAX_PATCH_LEN);
  355. /* prep the buffer with the original instructions */
  356. memcpy(insnbuf, p->instr, p->len);
  357. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  358. (unsigned long)p->instr, p->len);
  359. BUG_ON(used > p->len);
  360. /* Pad the rest with nops */
  361. add_nops(insnbuf + used, p->len - used);
  362. text_poke_early(p->instr, insnbuf, p->len);
  363. }
  364. }
  365. extern struct paravirt_patch_site __start_parainstructions[],
  366. __stop_parainstructions[];
  367. #endif /* CONFIG_PARAVIRT */
  368. void __init alternative_instructions(void)
  369. {
  370. /* The patching is not fully atomic, so try to avoid local interruptions
  371. that might execute the to be patched code.
  372. Other CPUs are not running. */
  373. stop_nmi();
  374. /*
  375. * Don't stop machine check exceptions while patching.
  376. * MCEs only happen when something got corrupted and in this
  377. * case we must do something about the corruption.
  378. * Ignoring it is worse than a unlikely patching race.
  379. * Also machine checks tend to be broadcast and if one CPU
  380. * goes into machine check the others follow quickly, so we don't
  381. * expect a machine check to cause undue problems during to code
  382. * patching.
  383. */
  384. apply_alternatives(__alt_instructions, __alt_instructions_end);
  385. /* switch to patch-once-at-boottime-only mode and free the
  386. * tables in case we know the number of CPUs will never ever
  387. * change */
  388. #ifdef CONFIG_HOTPLUG_CPU
  389. if (num_possible_cpus() < 2)
  390. smp_alt_once = 1;
  391. #endif
  392. #ifdef CONFIG_SMP
  393. if (smp_alt_once) {
  394. if (1 == num_possible_cpus()) {
  395. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  396. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  397. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  398. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  399. _text, _etext);
  400. }
  401. } else {
  402. alternatives_smp_module_add(NULL, "core kernel",
  403. __smp_locks, __smp_locks_end,
  404. _text, _etext);
  405. /* Only switch to UP mode if we don't immediately boot others */
  406. if (num_present_cpus() == 1 || setup_max_cpus <= 1)
  407. alternatives_smp_switch(0);
  408. }
  409. #endif
  410. apply_paravirt(__parainstructions, __parainstructions_end);
  411. if (smp_alt_once)
  412. free_init_pages("SMP alternatives",
  413. (unsigned long)__smp_locks,
  414. (unsigned long)__smp_locks_end);
  415. restart_nmi();
  416. }
  417. /**
  418. * text_poke_early - Update instructions on a live kernel at boot time
  419. * @addr: address to modify
  420. * @opcode: source of the copy
  421. * @len: length to copy
  422. *
  423. * When you use this code to patch more than one byte of an instruction
  424. * you need to make sure that other CPUs cannot execute this code in parallel.
  425. * Also no thread must be currently preempted in the middle of these
  426. * instructions. And on the local CPU you need to be protected again NMI or MCE
  427. * handlers seeing an inconsistent instruction while you patch.
  428. */
  429. static void *__init_or_module text_poke_early(void *addr, const void *opcode,
  430. size_t len)
  431. {
  432. unsigned long flags;
  433. local_irq_save(flags);
  434. memcpy(addr, opcode, len);
  435. sync_core();
  436. local_irq_restore(flags);
  437. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  438. that causes hangs on some VIA CPUs. */
  439. return addr;
  440. }
  441. /**
  442. * text_poke - Update instructions on a live kernel
  443. * @addr: address to modify
  444. * @opcode: source of the copy
  445. * @len: length to copy
  446. *
  447. * Only atomic text poke/set should be allowed when not doing early patching.
  448. * It means the size must be writable atomically and the address must be aligned
  449. * in a way that permits an atomic write. It also makes sure we fit on a single
  450. * page.
  451. *
  452. * Note: Must be called under text_mutex.
  453. */
  454. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  455. {
  456. unsigned long flags;
  457. char *vaddr;
  458. struct page *pages[2];
  459. int i;
  460. if (!core_kernel_text((unsigned long)addr)) {
  461. pages[0] = vmalloc_to_page(addr);
  462. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  463. } else {
  464. pages[0] = virt_to_page(addr);
  465. WARN_ON(!PageReserved(pages[0]));
  466. pages[1] = virt_to_page(addr + PAGE_SIZE);
  467. }
  468. BUG_ON(!pages[0]);
  469. local_irq_save(flags);
  470. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  471. if (pages[1])
  472. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  473. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  474. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  475. clear_fixmap(FIX_TEXT_POKE0);
  476. if (pages[1])
  477. clear_fixmap(FIX_TEXT_POKE1);
  478. local_flush_tlb();
  479. sync_core();
  480. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  481. that causes hangs on some VIA CPUs. */
  482. for (i = 0; i < len; i++)
  483. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  484. local_irq_restore(flags);
  485. return addr;
  486. }