alternative.c 13 KB

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