alternative.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #define MAX_PATCH_LEN (255-1)
  15. #ifdef CONFIG_HOTPLUG_CPU
  16. static int smp_alt_once;
  17. static int __init bootonly(char *str)
  18. {
  19. smp_alt_once = 1;
  20. return 1;
  21. }
  22. __setup("smp-alt-boot", bootonly);
  23. #else
  24. #define smp_alt_once 1
  25. #endif
  26. static int debug_alternative;
  27. static int __init debug_alt(char *str)
  28. {
  29. debug_alternative = 1;
  30. return 1;
  31. }
  32. __setup("debug-alternative", debug_alt);
  33. static int noreplace_smp;
  34. static int __init setup_noreplace_smp(char *str)
  35. {
  36. noreplace_smp = 1;
  37. return 1;
  38. }
  39. __setup("noreplace-smp", setup_noreplace_smp);
  40. #ifdef CONFIG_PARAVIRT
  41. static int noreplace_paravirt = 0;
  42. static int __init setup_noreplace_paravirt(char *str)
  43. {
  44. noreplace_paravirt = 1;
  45. return 1;
  46. }
  47. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  48. #endif
  49. #define DPRINTK(fmt, args...) if (debug_alternative) \
  50. printk(KERN_DEBUG fmt, args)
  51. #ifdef GENERIC_NOP1
  52. /* Use inline assembly to define this because the nops are defined
  53. as inline assembly strings in the include files and we cannot
  54. get them easily into strings. */
  55. asm("\t.section .rodata, \"a\"\nintelnops: "
  56. GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
  57. GENERIC_NOP7 GENERIC_NOP8);
  58. extern const unsigned char intelnops[];
  59. static const unsigned char *const intel_nops[ASM_NOP_MAX+1] = {
  60. NULL,
  61. intelnops,
  62. intelnops + 1,
  63. intelnops + 1 + 2,
  64. intelnops + 1 + 2 + 3,
  65. intelnops + 1 + 2 + 3 + 4,
  66. intelnops + 1 + 2 + 3 + 4 + 5,
  67. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  68. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  69. };
  70. #endif
  71. #ifdef K8_NOP1
  72. asm("\t.section .rodata, \"a\"\nk8nops: "
  73. K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
  74. K8_NOP7 K8_NOP8);
  75. extern const unsigned char k8nops[];
  76. static const unsigned char *const k8_nops[ASM_NOP_MAX+1] = {
  77. NULL,
  78. k8nops,
  79. k8nops + 1,
  80. k8nops + 1 + 2,
  81. k8nops + 1 + 2 + 3,
  82. k8nops + 1 + 2 + 3 + 4,
  83. k8nops + 1 + 2 + 3 + 4 + 5,
  84. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  85. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  86. };
  87. #endif
  88. #ifdef K7_NOP1
  89. asm("\t.section .rodata, \"a\"\nk7nops: "
  90. K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
  91. K7_NOP7 K7_NOP8);
  92. extern const unsigned char k7nops[];
  93. static const unsigned char *const k7_nops[ASM_NOP_MAX+1] = {
  94. NULL,
  95. k7nops,
  96. k7nops + 1,
  97. k7nops + 1 + 2,
  98. k7nops + 1 + 2 + 3,
  99. k7nops + 1 + 2 + 3 + 4,
  100. k7nops + 1 + 2 + 3 + 4 + 5,
  101. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  102. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  103. };
  104. #endif
  105. #ifdef P6_NOP1
  106. asm("\t.section .rodata, \"a\"\np6nops: "
  107. P6_NOP1 P6_NOP2 P6_NOP3 P6_NOP4 P6_NOP5 P6_NOP6
  108. P6_NOP7 P6_NOP8);
  109. extern const unsigned char p6nops[];
  110. static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
  111. NULL,
  112. p6nops,
  113. p6nops + 1,
  114. p6nops + 1 + 2,
  115. p6nops + 1 + 2 + 3,
  116. p6nops + 1 + 2 + 3 + 4,
  117. p6nops + 1 + 2 + 3 + 4 + 5,
  118. p6nops + 1 + 2 + 3 + 4 + 5 + 6,
  119. p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  120. };
  121. #endif
  122. #ifdef CONFIG_X86_64
  123. extern char __vsyscall_0;
  124. static inline const unsigned char*const * find_nop_table(void)
  125. {
  126. return boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  127. boot_cpu_data.x86 < 6 ? k8_nops : p6_nops;
  128. }
  129. #else /* CONFIG_X86_64 */
  130. static const struct nop {
  131. int cpuid;
  132. const unsigned char *const *noptable;
  133. } noptypes[] = {
  134. { X86_FEATURE_K8, k8_nops },
  135. { X86_FEATURE_K7, k7_nops },
  136. { X86_FEATURE_P4, p6_nops },
  137. { X86_FEATURE_P3, p6_nops },
  138. { -1, NULL }
  139. };
  140. static const unsigned char*const * find_nop_table(void)
  141. {
  142. const unsigned char *const *noptable = intel_nops;
  143. int i;
  144. for (i = 0; noptypes[i].cpuid >= 0; i++) {
  145. if (boot_cpu_has(noptypes[i].cpuid)) {
  146. noptable = noptypes[i].noptable;
  147. break;
  148. }
  149. }
  150. return noptable;
  151. }
  152. #endif /* CONFIG_X86_64 */
  153. /* Use this to add nops to a buffer, then text_poke the whole buffer. */
  154. static void add_nops(void *insns, unsigned int len)
  155. {
  156. const unsigned char *const *noptable = find_nop_table();
  157. while (len > 0) {
  158. unsigned int noplen = len;
  159. if (noplen > ASM_NOP_MAX)
  160. noplen = ASM_NOP_MAX;
  161. memcpy(insns, noptable[noplen], noplen);
  162. insns += noplen;
  163. len -= noplen;
  164. }
  165. }
  166. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  167. extern u8 *__smp_locks[], *__smp_locks_end[];
  168. /* Replace instructions with better alternatives for this CPU type.
  169. This runs before SMP is initialized to avoid SMP problems with
  170. self modifying code. This implies that assymetric systems where
  171. APs have less capabilities than the boot processor are not handled.
  172. Tough. Make sure you disable such features by hand. */
  173. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  174. {
  175. struct alt_instr *a;
  176. char insnbuf[MAX_PATCH_LEN];
  177. DPRINTK("%s: alt table %p -> %p\n", __FUNCTION__, start, end);
  178. for (a = start; a < end; a++) {
  179. u8 *instr = a->instr;
  180. BUG_ON(a->replacementlen > a->instrlen);
  181. BUG_ON(a->instrlen > sizeof(insnbuf));
  182. if (!boot_cpu_has(a->cpuid))
  183. continue;
  184. #ifdef CONFIG_X86_64
  185. /* vsyscall code is not mapped yet. resolve it manually. */
  186. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  187. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  188. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  189. __FUNCTION__, a->instr, instr);
  190. }
  191. #endif
  192. memcpy(insnbuf, a->replacement, a->replacementlen);
  193. add_nops(insnbuf + a->replacementlen,
  194. a->instrlen - a->replacementlen);
  195. text_poke(instr, insnbuf, a->instrlen);
  196. }
  197. }
  198. #ifdef CONFIG_SMP
  199. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  200. {
  201. u8 **ptr;
  202. for (ptr = start; ptr < end; ptr++) {
  203. if (*ptr < text)
  204. continue;
  205. if (*ptr > text_end)
  206. continue;
  207. text_poke(*ptr, ((unsigned char []){0xf0}), 1); /* add lock prefix */
  208. };
  209. }
  210. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  211. {
  212. u8 **ptr;
  213. char insn[1];
  214. if (noreplace_smp)
  215. return;
  216. add_nops(insn, 1);
  217. for (ptr = start; ptr < end; ptr++) {
  218. if (*ptr < text)
  219. continue;
  220. if (*ptr > text_end)
  221. continue;
  222. text_poke(*ptr, insn, 1);
  223. };
  224. }
  225. struct smp_alt_module {
  226. /* what is this ??? */
  227. struct module *mod;
  228. char *name;
  229. /* ptrs to lock prefixes */
  230. u8 **locks;
  231. u8 **locks_end;
  232. /* .text segment, needed to avoid patching init code ;) */
  233. u8 *text;
  234. u8 *text_end;
  235. struct list_head next;
  236. };
  237. static LIST_HEAD(smp_alt_modules);
  238. static DEFINE_SPINLOCK(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. unsigned long flags;
  245. if (noreplace_smp)
  246. return;
  247. if (smp_alt_once) {
  248. if (boot_cpu_has(X86_FEATURE_UP))
  249. alternatives_smp_unlock(locks, locks_end,
  250. text, text_end);
  251. return;
  252. }
  253. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  254. if (NULL == smp)
  255. return; /* we'll run the (safe but slow) SMP code then ... */
  256. smp->mod = mod;
  257. smp->name = name;
  258. smp->locks = locks;
  259. smp->locks_end = locks_end;
  260. smp->text = text;
  261. smp->text_end = text_end;
  262. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  263. __FUNCTION__, smp->locks, smp->locks_end,
  264. smp->text, smp->text_end, smp->name);
  265. spin_lock_irqsave(&smp_alt, flags);
  266. list_add_tail(&smp->next, &smp_alt_modules);
  267. if (boot_cpu_has(X86_FEATURE_UP))
  268. alternatives_smp_unlock(smp->locks, smp->locks_end,
  269. smp->text, smp->text_end);
  270. spin_unlock_irqrestore(&smp_alt, flags);
  271. }
  272. void alternatives_smp_module_del(struct module *mod)
  273. {
  274. struct smp_alt_module *item;
  275. unsigned long flags;
  276. if (smp_alt_once || noreplace_smp)
  277. return;
  278. spin_lock_irqsave(&smp_alt, flags);
  279. list_for_each_entry(item, &smp_alt_modules, next) {
  280. if (mod != item->mod)
  281. continue;
  282. list_del(&item->next);
  283. spin_unlock_irqrestore(&smp_alt, flags);
  284. DPRINTK("%s: %s\n", __FUNCTION__, item->name);
  285. kfree(item);
  286. return;
  287. }
  288. spin_unlock_irqrestore(&smp_alt, flags);
  289. }
  290. void alternatives_smp_switch(int smp)
  291. {
  292. struct smp_alt_module *mod;
  293. unsigned long flags;
  294. #ifdef CONFIG_LOCKDEP
  295. /*
  296. * A not yet fixed binutils section handling bug prevents
  297. * alternatives-replacement from working reliably, so turn
  298. * it off:
  299. */
  300. printk("lockdep: not fixing up alternatives.\n");
  301. return;
  302. #endif
  303. if (noreplace_smp || smp_alt_once)
  304. return;
  305. BUG_ON(!smp && (num_online_cpus() > 1));
  306. spin_lock_irqsave(&smp_alt, flags);
  307. if (smp) {
  308. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  309. clear_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  310. clear_bit(X86_FEATURE_UP, cpu_data(0).x86_capability);
  311. list_for_each_entry(mod, &smp_alt_modules, next)
  312. alternatives_smp_lock(mod->locks, mod->locks_end,
  313. mod->text, mod->text_end);
  314. } else {
  315. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  316. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  317. set_bit(X86_FEATURE_UP, cpu_data(0).x86_capability);
  318. list_for_each_entry(mod, &smp_alt_modules, next)
  319. alternatives_smp_unlock(mod->locks, mod->locks_end,
  320. mod->text, mod->text_end);
  321. }
  322. spin_unlock_irqrestore(&smp_alt, flags);
  323. }
  324. #endif
  325. #ifdef CONFIG_PARAVIRT
  326. void apply_paravirt(struct paravirt_patch_site *start,
  327. struct paravirt_patch_site *end)
  328. {
  329. struct paravirt_patch_site *p;
  330. char insnbuf[MAX_PATCH_LEN];
  331. if (noreplace_paravirt)
  332. return;
  333. for (p = start; p < end; p++) {
  334. unsigned int used;
  335. BUG_ON(p->len > MAX_PATCH_LEN);
  336. /* prep the buffer with the original instructions */
  337. memcpy(insnbuf, p->instr, p->len);
  338. used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
  339. (unsigned long)p->instr, p->len);
  340. BUG_ON(used > p->len);
  341. /* Pad the rest with nops */
  342. add_nops(insnbuf + used, p->len - used);
  343. text_poke(p->instr, insnbuf, p->len);
  344. }
  345. }
  346. extern struct paravirt_patch_site __start_parainstructions[],
  347. __stop_parainstructions[];
  348. #endif /* CONFIG_PARAVIRT */
  349. void __init alternative_instructions(void)
  350. {
  351. unsigned long flags;
  352. /* The patching is not fully atomic, so try to avoid local interruptions
  353. that might execute the to be patched code.
  354. Other CPUs are not running. */
  355. stop_nmi();
  356. #ifdef CONFIG_X86_MCE
  357. stop_mce();
  358. #endif
  359. local_irq_save(flags);
  360. apply_alternatives(__alt_instructions, __alt_instructions_end);
  361. /* switch to patch-once-at-boottime-only mode and free the
  362. * tables in case we know the number of CPUs will never ever
  363. * change */
  364. #ifdef CONFIG_HOTPLUG_CPU
  365. if (num_possible_cpus() < 2)
  366. smp_alt_once = 1;
  367. #endif
  368. #ifdef CONFIG_SMP
  369. if (smp_alt_once) {
  370. if (1 == num_possible_cpus()) {
  371. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  372. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  373. set_bit(X86_FEATURE_UP, cpu_data(0).x86_capability);
  374. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  375. _text, _etext);
  376. }
  377. } else {
  378. alternatives_smp_module_add(NULL, "core kernel",
  379. __smp_locks, __smp_locks_end,
  380. _text, _etext);
  381. alternatives_smp_switch(0);
  382. }
  383. #endif
  384. apply_paravirt(__parainstructions, __parainstructions_end);
  385. local_irq_restore(flags);
  386. if (smp_alt_once)
  387. free_init_pages("SMP alternatives",
  388. (unsigned long)__smp_locks,
  389. (unsigned long)__smp_locks_end);
  390. restart_nmi();
  391. #ifdef CONFIG_X86_MCE
  392. restart_mce();
  393. #endif
  394. }
  395. /*
  396. * Warning:
  397. * When you use this code to patch more than one byte of an instruction
  398. * you need to make sure that other CPUs cannot execute this code in parallel.
  399. * Also no thread must be currently preempted in the middle of these instructions.
  400. * And on the local CPU you need to be protected again NMI or MCE handlers
  401. * seeing an inconsistent instruction while you patch.
  402. */
  403. void __kprobes text_poke(void *addr, unsigned char *opcode, int len)
  404. {
  405. memcpy(addr, opcode, len);
  406. sync_core();
  407. /* Could also do a CLFLUSH here to speed up CPU recovery; but
  408. that causes hangs on some VIA CPUs. */
  409. }