alternative.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. u8 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. if (*insnbuf == 0xe8 && a->replacementlen == 5)
  202. *(s32 *)(insnbuf + 1) += a->replacement - a->instr;
  203. add_nops(insnbuf + a->replacementlen,
  204. a->instrlen - a->replacementlen);
  205. text_poke_early(instr, insnbuf, a->instrlen);
  206. }
  207. }
  208. #ifdef CONFIG_SMP
  209. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  210. {
  211. u8 **ptr;
  212. mutex_lock(&text_mutex);
  213. for (ptr = start; ptr < end; ptr++) {
  214. if (*ptr < text)
  215. continue;
  216. if (*ptr > text_end)
  217. continue;
  218. /* turn DS segment override prefix into lock prefix */
  219. text_poke(*ptr, ((unsigned char []){0xf0}), 1);
  220. };
  221. mutex_unlock(&text_mutex);
  222. }
  223. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  224. {
  225. u8 **ptr;
  226. if (noreplace_smp)
  227. return;
  228. mutex_lock(&text_mutex);
  229. for (ptr = start; ptr < end; ptr++) {
  230. if (*ptr < text)
  231. continue;
  232. if (*ptr > text_end)
  233. continue;
  234. /* turn lock prefix into DS segment override prefix */
  235. text_poke(*ptr, ((unsigned char []){0x3E}), 1);
  236. };
  237. mutex_unlock(&text_mutex);
  238. }
  239. struct smp_alt_module {
  240. /* what is this ??? */
  241. struct module *mod;
  242. char *name;
  243. /* ptrs to lock prefixes */
  244. u8 **locks;
  245. u8 **locks_end;
  246. /* .text segment, needed to avoid patching init code ;) */
  247. u8 *text;
  248. u8 *text_end;
  249. struct list_head next;
  250. };
  251. static LIST_HEAD(smp_alt_modules);
  252. static DEFINE_MUTEX(smp_alt);
  253. static int smp_mode = 1; /* protected by smp_alt */
  254. void __init_or_module alternatives_smp_module_add(struct module *mod,
  255. char *name,
  256. void *locks, void *locks_end,
  257. void *text, void *text_end)
  258. {
  259. struct smp_alt_module *smp;
  260. if (noreplace_smp)
  261. return;
  262. if (smp_alt_once) {
  263. if (boot_cpu_has(X86_FEATURE_UP))
  264. alternatives_smp_unlock(locks, locks_end,
  265. text, text_end);
  266. return;
  267. }
  268. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  269. if (NULL == smp)
  270. return; /* we'll run the (safe but slow) SMP code then ... */
  271. smp->mod = mod;
  272. smp->name = name;
  273. smp->locks = locks;
  274. smp->locks_end = locks_end;
  275. smp->text = text;
  276. smp->text_end = text_end;
  277. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  278. __func__, smp->locks, smp->locks_end,
  279. smp->text, smp->text_end, smp->name);
  280. mutex_lock(&smp_alt);
  281. list_add_tail(&smp->next, &smp_alt_modules);
  282. if (boot_cpu_has(X86_FEATURE_UP))
  283. alternatives_smp_unlock(smp->locks, smp->locks_end,
  284. smp->text, smp->text_end);
  285. mutex_unlock(&smp_alt);
  286. }
  287. void __init_or_module alternatives_smp_module_del(struct module *mod)
  288. {
  289. struct smp_alt_module *item;
  290. if (smp_alt_once || noreplace_smp)
  291. return;
  292. mutex_lock(&smp_alt);
  293. list_for_each_entry(item, &smp_alt_modules, next) {
  294. if (mod != item->mod)
  295. continue;
  296. list_del(&item->next);
  297. mutex_unlock(&smp_alt);
  298. DPRINTK("%s: %s\n", __func__, item->name);
  299. kfree(item);
  300. return;
  301. }
  302. mutex_unlock(&smp_alt);
  303. }
  304. void alternatives_smp_switch(int smp)
  305. {
  306. struct smp_alt_module *mod;
  307. #ifdef CONFIG_LOCKDEP
  308. /*
  309. * Older binutils section handling bug prevented
  310. * alternatives-replacement from working reliably.
  311. *
  312. * If this still occurs then you should see a hang
  313. * or crash shortly after this line:
  314. */
  315. printk("lockdep: fixing up alternatives.\n");
  316. #endif
  317. if (noreplace_smp || smp_alt_once)
  318. return;
  319. BUG_ON(!smp && (num_online_cpus() > 1));
  320. mutex_lock(&smp_alt);
  321. /*
  322. * Avoid unnecessary switches because it forces JIT based VMs to
  323. * throw away all cached translations, which can be quite costly.
  324. */
  325. if (smp == smp_mode) {
  326. /* nothing */
  327. } else if (smp) {
  328. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  329. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  330. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  331. list_for_each_entry(mod, &smp_alt_modules, next)
  332. alternatives_smp_lock(mod->locks, mod->locks_end,
  333. mod->text, mod->text_end);
  334. } else {
  335. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  336. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  337. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  338. list_for_each_entry(mod, &smp_alt_modules, next)
  339. alternatives_smp_unlock(mod->locks, mod->locks_end,
  340. mod->text, mod->text_end);
  341. }
  342. smp_mode = smp;
  343. mutex_unlock(&smp_alt);
  344. }
  345. /* Return 1 if the address range is reserved for smp-alternatives */
  346. int alternatives_text_reserved(void *start, void *end)
  347. {
  348. struct smp_alt_module *mod;
  349. u8 **ptr;
  350. u8 *text_start = start;
  351. u8 *text_end = end;
  352. list_for_each_entry(mod, &smp_alt_modules, next) {
  353. if (mod->text > text_end || mod->text_end < text_start)
  354. continue;
  355. for (ptr = mod->locks; ptr < mod->locks_end; ptr++)
  356. if (text_start <= *ptr && text_end >= *ptr)
  357. return 1;
  358. }
  359. return 0;
  360. }
  361. #endif
  362. #ifdef CONFIG_PARAVIRT
  363. void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
  364. struct paravirt_patch_site *end)
  365. {
  366. struct paravirt_patch_site *p;
  367. char insnbuf[MAX_PATCH_LEN];
  368. if (noreplace_paravirt)
  369. return;
  370. for (p = start; p < end; p++) {
  371. unsigned int used;
  372. BUG_ON(p->len > MAX_PATCH_LEN);
  373. /* prep the buffer with the original instructions */
  374. memcpy(insnbuf, p->instr, p->len);
  375. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  376. (unsigned long)p->instr, p->len);
  377. BUG_ON(used > p->len);
  378. /* Pad the rest with nops */
  379. add_nops(insnbuf + used, p->len - used);
  380. text_poke_early(p->instr, insnbuf, p->len);
  381. }
  382. }
  383. extern struct paravirt_patch_site __start_parainstructions[],
  384. __stop_parainstructions[];
  385. #endif /* CONFIG_PARAVIRT */
  386. void __init alternative_instructions(void)
  387. {
  388. /* The patching is not fully atomic, so try to avoid local interruptions
  389. that might execute the to be patched code.
  390. Other CPUs are not running. */
  391. stop_nmi();
  392. /*
  393. * Don't stop machine check exceptions while patching.
  394. * MCEs only happen when something got corrupted and in this
  395. * case we must do something about the corruption.
  396. * Ignoring it is worse than a unlikely patching race.
  397. * Also machine checks tend to be broadcast and if one CPU
  398. * goes into machine check the others follow quickly, so we don't
  399. * expect a machine check to cause undue problems during to code
  400. * patching.
  401. */
  402. apply_alternatives(__alt_instructions, __alt_instructions_end);
  403. /* switch to patch-once-at-boottime-only mode and free the
  404. * tables in case we know the number of CPUs will never ever
  405. * change */
  406. #ifdef CONFIG_HOTPLUG_CPU
  407. if (num_possible_cpus() < 2)
  408. smp_alt_once = 1;
  409. #endif
  410. #ifdef CONFIG_SMP
  411. if (smp_alt_once) {
  412. if (1 == num_possible_cpus()) {
  413. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  414. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  415. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  416. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  417. _text, _etext);
  418. }
  419. } else {
  420. alternatives_smp_module_add(NULL, "core kernel",
  421. __smp_locks, __smp_locks_end,
  422. _text, _etext);
  423. /* Only switch to UP mode if we don't immediately boot others */
  424. if (num_present_cpus() == 1 || setup_max_cpus <= 1)
  425. alternatives_smp_switch(0);
  426. }
  427. #endif
  428. apply_paravirt(__parainstructions, __parainstructions_end);
  429. if (smp_alt_once)
  430. free_init_pages("SMP alternatives",
  431. (unsigned long)__smp_locks,
  432. (unsigned long)__smp_locks_end);
  433. restart_nmi();
  434. }
  435. /**
  436. * text_poke_early - Update instructions on a live kernel at boot time
  437. * @addr: address to modify
  438. * @opcode: source of the copy
  439. * @len: length to copy
  440. *
  441. * When you use this code to patch more than one byte of an instruction
  442. * you need to make sure that other CPUs cannot execute this code in parallel.
  443. * Also no thread must be currently preempted in the middle of these
  444. * instructions. And on the local CPU you need to be protected again NMI or MCE
  445. * handlers seeing an inconsistent instruction while you patch.
  446. */
  447. static void *__init_or_module text_poke_early(void *addr, const void *opcode,
  448. size_t len)
  449. {
  450. unsigned long flags;
  451. local_irq_save(flags);
  452. memcpy(addr, opcode, len);
  453. sync_core();
  454. local_irq_restore(flags);
  455. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  456. that causes hangs on some VIA CPUs. */
  457. return addr;
  458. }
  459. /**
  460. * text_poke - Update instructions on a live kernel
  461. * @addr: address to modify
  462. * @opcode: source of the copy
  463. * @len: length to copy
  464. *
  465. * Only atomic text poke/set should be allowed when not doing early patching.
  466. * It means the size must be writable atomically and the address must be aligned
  467. * in a way that permits an atomic write. It also makes sure we fit on a single
  468. * page.
  469. *
  470. * Note: Must be called under text_mutex.
  471. */
  472. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  473. {
  474. unsigned long flags;
  475. char *vaddr;
  476. struct page *pages[2];
  477. int i;
  478. if (!core_kernel_text((unsigned long)addr)) {
  479. pages[0] = vmalloc_to_page(addr);
  480. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  481. } else {
  482. pages[0] = virt_to_page(addr);
  483. WARN_ON(!PageReserved(pages[0]));
  484. pages[1] = virt_to_page(addr + PAGE_SIZE);
  485. }
  486. BUG_ON(!pages[0]);
  487. local_irq_save(flags);
  488. set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
  489. if (pages[1])
  490. set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
  491. vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
  492. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  493. clear_fixmap(FIX_TEXT_POKE0);
  494. if (pages[1])
  495. clear_fixmap(FIX_TEXT_POKE1);
  496. local_flush_tlb();
  497. sync_core();
  498. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  499. that causes hangs on some VIA CPUs. */
  500. for (i = 0; i < len; i++)
  501. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  502. local_irq_restore(flags);
  503. return addr;
  504. }