alternative.c 14 KB

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