alternative.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 <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. const unsigned char *const *find_nop_table(void)
  131. {
  132. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  133. boot_cpu_has(X86_FEATURE_NOPL))
  134. return p6_nops;
  135. else
  136. return k8_nops;
  137. }
  138. #else /* CONFIG_X86_64 */
  139. const unsigned char *const *find_nop_table(void)
  140. {
  141. if (boot_cpu_has(X86_FEATURE_K8))
  142. return k8_nops;
  143. else if (boot_cpu_has(X86_FEATURE_K7))
  144. return k7_nops;
  145. else if (boot_cpu_has(X86_FEATURE_NOPL))
  146. return p6_nops;
  147. else
  148. return intel_nops;
  149. }
  150. #endif /* CONFIG_X86_64 */
  151. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  152. void add_nops(void *insns, unsigned int len)
  153. {
  154. const unsigned char *const *noptable = find_nop_table();
  155. while (len > 0) {
  156. unsigned int noplen = len;
  157. if (noplen > ASM_NOP_MAX)
  158. noplen = ASM_NOP_MAX;
  159. memcpy(insns, noptable[noplen], noplen);
  160. insns += noplen;
  161. len -= noplen;
  162. }
  163. }
  164. EXPORT_SYMBOL_GPL(add_nops);
  165. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  166. extern u8 *__smp_locks[], *__smp_locks_end[];
  167. /* Replace instructions with better alternatives for this CPU type.
  168. This runs before SMP is initialized to avoid SMP problems with
  169. self modifying code. This implies that assymetric systems where
  170. APs have less capabilities than the boot processor are not handled.
  171. Tough. Make sure you disable such features by hand. */
  172. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  173. {
  174. struct alt_instr *a;
  175. char insnbuf[MAX_PATCH_LEN];
  176. DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
  177. for (a = start; a < end; a++) {
  178. u8 *instr = a->instr;
  179. BUG_ON(a->replacementlen > a->instrlen);
  180. BUG_ON(a->instrlen > sizeof(insnbuf));
  181. if (!boot_cpu_has(a->cpuid))
  182. continue;
  183. #ifdef CONFIG_X86_64
  184. /* vsyscall code is not mapped yet. resolve it manually. */
  185. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  186. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  187. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  188. __func__, a->instr, instr);
  189. }
  190. #endif
  191. memcpy(insnbuf, a->replacement, a->replacementlen);
  192. add_nops(insnbuf + a->replacementlen,
  193. a->instrlen - a->replacementlen);
  194. text_poke_early(instr, insnbuf, a->instrlen);
  195. }
  196. }
  197. #ifdef CONFIG_SMP
  198. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  199. {
  200. u8 **ptr;
  201. for (ptr = start; ptr < end; ptr++) {
  202. if (*ptr < text)
  203. continue;
  204. if (*ptr > text_end)
  205. continue;
  206. /* turn DS segment override prefix into lock prefix */
  207. text_poke(*ptr, ((unsigned char []){0xf0}), 1);
  208. };
  209. }
  210. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  211. {
  212. u8 **ptr;
  213. if (noreplace_smp)
  214. return;
  215. for (ptr = start; ptr < end; ptr++) {
  216. if (*ptr < text)
  217. continue;
  218. if (*ptr > text_end)
  219. continue;
  220. /* turn lock prefix into DS segment override prefix */
  221. text_poke(*ptr, ((unsigned char []){0x3E}), 1);
  222. };
  223. }
  224. struct smp_alt_module {
  225. /* what is this ??? */
  226. struct module *mod;
  227. char *name;
  228. /* ptrs to lock prefixes */
  229. u8 **locks;
  230. u8 **locks_end;
  231. /* .text segment, needed to avoid patching init code ;) */
  232. u8 *text;
  233. u8 *text_end;
  234. struct list_head next;
  235. };
  236. static LIST_HEAD(smp_alt_modules);
  237. static DEFINE_MUTEX(smp_alt);
  238. static int smp_mode = 1; /* protected by smp_alt */
  239. void alternatives_smp_module_add(struct module *mod, char *name,
  240. void *locks, void *locks_end,
  241. void *text, void *text_end)
  242. {
  243. struct smp_alt_module *smp;
  244. if (noreplace_smp)
  245. return;
  246. if (smp_alt_once) {
  247. if (boot_cpu_has(X86_FEATURE_UP))
  248. alternatives_smp_unlock(locks, locks_end,
  249. text, text_end);
  250. return;
  251. }
  252. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  253. if (NULL == smp)
  254. return; /* we'll run the (safe but slow) SMP code then ... */
  255. smp->mod = mod;
  256. smp->name = name;
  257. smp->locks = locks;
  258. smp->locks_end = locks_end;
  259. smp->text = text;
  260. smp->text_end = text_end;
  261. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  262. __func__, smp->locks, smp->locks_end,
  263. smp->text, smp->text_end, smp->name);
  264. mutex_lock(&smp_alt);
  265. list_add_tail(&smp->next, &smp_alt_modules);
  266. if (boot_cpu_has(X86_FEATURE_UP))
  267. alternatives_smp_unlock(smp->locks, smp->locks_end,
  268. smp->text, smp->text_end);
  269. mutex_unlock(&smp_alt);
  270. }
  271. void alternatives_smp_module_del(struct module *mod)
  272. {
  273. struct smp_alt_module *item;
  274. if (smp_alt_once || noreplace_smp)
  275. return;
  276. mutex_lock(&smp_alt);
  277. list_for_each_entry(item, &smp_alt_modules, next) {
  278. if (mod != item->mod)
  279. continue;
  280. list_del(&item->next);
  281. mutex_unlock(&smp_alt);
  282. DPRINTK("%s: %s\n", __func__, item->name);
  283. kfree(item);
  284. return;
  285. }
  286. mutex_unlock(&smp_alt);
  287. }
  288. void alternatives_smp_switch(int smp)
  289. {
  290. struct smp_alt_module *mod;
  291. #ifdef CONFIG_LOCKDEP
  292. /*
  293. * Older binutils section handling bug prevented
  294. * alternatives-replacement from working reliably.
  295. *
  296. * If this still occurs then you should see a hang
  297. * or crash shortly after this line:
  298. */
  299. printk("lockdep: fixing up alternatives.\n");
  300. #endif
  301. if (noreplace_smp || smp_alt_once)
  302. return;
  303. BUG_ON(!smp && (num_online_cpus() > 1));
  304. mutex_lock(&smp_alt);
  305. /*
  306. * Avoid unnecessary switches because it forces JIT based VMs to
  307. * throw away all cached translations, which can be quite costly.
  308. */
  309. if (smp == smp_mode) {
  310. /* nothing */
  311. } else if (smp) {
  312. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  313. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  314. clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  315. list_for_each_entry(mod, &smp_alt_modules, next)
  316. alternatives_smp_lock(mod->locks, mod->locks_end,
  317. mod->text, mod->text_end);
  318. } else {
  319. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  320. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  321. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  322. list_for_each_entry(mod, &smp_alt_modules, next)
  323. alternatives_smp_unlock(mod->locks, mod->locks_end,
  324. mod->text, mod->text_end);
  325. }
  326. smp_mode = smp;
  327. mutex_unlock(&smp_alt);
  328. }
  329. #endif
  330. #ifdef CONFIG_PARAVIRT
  331. void apply_paravirt(struct paravirt_patch_site *start,
  332. struct paravirt_patch_site *end)
  333. {
  334. struct paravirt_patch_site *p;
  335. char insnbuf[MAX_PATCH_LEN];
  336. if (noreplace_paravirt)
  337. return;
  338. for (p = start; p < end; p++) {
  339. unsigned int used;
  340. BUG_ON(p->len > MAX_PATCH_LEN);
  341. /* prep the buffer with the original instructions */
  342. memcpy(insnbuf, p->instr, p->len);
  343. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  344. (unsigned long)p->instr, p->len);
  345. BUG_ON(used > p->len);
  346. /* Pad the rest with nops */
  347. add_nops(insnbuf + used, p->len - used);
  348. text_poke_early(p->instr, insnbuf, p->len);
  349. }
  350. }
  351. extern struct paravirt_patch_site __start_parainstructions[],
  352. __stop_parainstructions[];
  353. #endif /* CONFIG_PARAVIRT */
  354. void __init alternative_instructions(void)
  355. {
  356. /* The patching is not fully atomic, so try to avoid local interruptions
  357. that might execute the to be patched code.
  358. Other CPUs are not running. */
  359. stop_nmi();
  360. #ifdef CONFIG_X86_MCE
  361. stop_mce();
  362. #endif
  363. apply_alternatives(__alt_instructions, __alt_instructions_end);
  364. /* switch to patch-once-at-boottime-only mode and free the
  365. * tables in case we know the number of CPUs will never ever
  366. * change */
  367. #ifdef CONFIG_HOTPLUG_CPU
  368. if (num_possible_cpus() < 2)
  369. smp_alt_once = 1;
  370. #endif
  371. #ifdef CONFIG_SMP
  372. if (smp_alt_once) {
  373. if (1 == num_possible_cpus()) {
  374. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  375. set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
  376. set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
  377. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  378. _text, _etext);
  379. }
  380. } else {
  381. alternatives_smp_module_add(NULL, "core kernel",
  382. __smp_locks, __smp_locks_end,
  383. _text, _etext);
  384. /* Only switch to UP mode if we don't immediately boot others */
  385. if (num_possible_cpus() == 1 || setup_max_cpus <= 1)
  386. alternatives_smp_switch(0);
  387. }
  388. #endif
  389. apply_paravirt(__parainstructions, __parainstructions_end);
  390. if (smp_alt_once)
  391. free_init_pages("SMP alternatives",
  392. (unsigned long)__smp_locks,
  393. (unsigned long)__smp_locks_end);
  394. restart_nmi();
  395. #ifdef CONFIG_X86_MCE
  396. restart_mce();
  397. #endif
  398. }
  399. /**
  400. * text_poke_early - Update instructions on a live kernel at boot time
  401. * @addr: address to modify
  402. * @opcode: source of the copy
  403. * @len: length to copy
  404. *
  405. * When you use this code to patch more than one byte of an instruction
  406. * you need to make sure that other CPUs cannot execute this code in parallel.
  407. * Also no thread must be currently preempted in the middle of these
  408. * instructions. And on the local CPU you need to be protected again NMI or MCE
  409. * handlers seeing an inconsistent instruction while you patch.
  410. */
  411. void *text_poke_early(void *addr, const void *opcode, size_t len)
  412. {
  413. unsigned long flags;
  414. local_irq_save(flags);
  415. memcpy(addr, opcode, len);
  416. local_irq_restore(flags);
  417. sync_core();
  418. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  419. that causes hangs on some VIA CPUs. */
  420. return addr;
  421. }
  422. /**
  423. * text_poke - Update instructions on a live kernel
  424. * @addr: address to modify
  425. * @opcode: source of the copy
  426. * @len: length to copy
  427. *
  428. * Only atomic text poke/set should be allowed when not doing early patching.
  429. * It means the size must be writable atomically and the address must be aligned
  430. * in a way that permits an atomic write. It also makes sure we fit on a single
  431. * page.
  432. */
  433. void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  434. {
  435. unsigned long flags;
  436. char *vaddr;
  437. int nr_pages = 2;
  438. struct page *pages[2];
  439. int i;
  440. if (!core_kernel_text((unsigned long)addr)) {
  441. pages[0] = vmalloc_to_page(addr);
  442. pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
  443. } else {
  444. pages[0] = virt_to_page(addr);
  445. WARN_ON(!PageReserved(pages[0]));
  446. pages[1] = virt_to_page(addr + PAGE_SIZE);
  447. }
  448. BUG_ON(!pages[0]);
  449. if (!pages[1])
  450. nr_pages = 1;
  451. vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
  452. BUG_ON(!vaddr);
  453. local_irq_save(flags);
  454. memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
  455. local_irq_restore(flags);
  456. vunmap(vaddr);
  457. sync_core();
  458. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  459. that causes hangs on some VIA CPUs. */
  460. for (i = 0; i < len; i++)
  461. BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
  462. return addr;
  463. }