alternative.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/list.h>
  5. #include <asm/alternative.h>
  6. #include <asm/sections.h>
  7. static int noreplace_smp = 0;
  8. static int smp_alt_once = 0;
  9. static int debug_alternative = 0;
  10. static int __init bootonly(char *str)
  11. {
  12. smp_alt_once = 1;
  13. return 1;
  14. }
  15. __setup("smp-alt-boot", bootonly);
  16. static int __init debug_alt(char *str)
  17. {
  18. debug_alternative = 1;
  19. return 1;
  20. }
  21. __setup("debug-alternative", debug_alt);
  22. static int __init setup_noreplace_smp(char *str)
  23. {
  24. noreplace_smp = 1;
  25. return 1;
  26. }
  27. __setup("noreplace-smp", setup_noreplace_smp);
  28. #ifdef CONFIG_PARAVIRT
  29. static int noreplace_paravirt = 0;
  30. static int __init setup_noreplace_paravirt(char *str)
  31. {
  32. noreplace_paravirt = 1;
  33. return 1;
  34. }
  35. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  36. #endif
  37. #define DPRINTK(fmt, args...) if (debug_alternative) \
  38. printk(KERN_DEBUG fmt, args)
  39. #ifdef GENERIC_NOP1
  40. /* Use inline assembly to define this because the nops are defined
  41. as inline assembly strings in the include files and we cannot
  42. get them easily into strings. */
  43. asm("\t.data\nintelnops: "
  44. GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
  45. GENERIC_NOP7 GENERIC_NOP8);
  46. extern unsigned char intelnops[];
  47. static unsigned char *intel_nops[ASM_NOP_MAX+1] = {
  48. NULL,
  49. intelnops,
  50. intelnops + 1,
  51. intelnops + 1 + 2,
  52. intelnops + 1 + 2 + 3,
  53. intelnops + 1 + 2 + 3 + 4,
  54. intelnops + 1 + 2 + 3 + 4 + 5,
  55. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  56. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  57. };
  58. #endif
  59. #ifdef K8_NOP1
  60. asm("\t.data\nk8nops: "
  61. K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
  62. K8_NOP7 K8_NOP8);
  63. extern unsigned char k8nops[];
  64. static unsigned char *k8_nops[ASM_NOP_MAX+1] = {
  65. NULL,
  66. k8nops,
  67. k8nops + 1,
  68. k8nops + 1 + 2,
  69. k8nops + 1 + 2 + 3,
  70. k8nops + 1 + 2 + 3 + 4,
  71. k8nops + 1 + 2 + 3 + 4 + 5,
  72. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  73. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  74. };
  75. #endif
  76. #ifdef K7_NOP1
  77. asm("\t.data\nk7nops: "
  78. K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
  79. K7_NOP7 K7_NOP8);
  80. extern unsigned char k7nops[];
  81. static unsigned char *k7_nops[ASM_NOP_MAX+1] = {
  82. NULL,
  83. k7nops,
  84. k7nops + 1,
  85. k7nops + 1 + 2,
  86. k7nops + 1 + 2 + 3,
  87. k7nops + 1 + 2 + 3 + 4,
  88. k7nops + 1 + 2 + 3 + 4 + 5,
  89. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  90. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  91. };
  92. #endif
  93. #ifdef CONFIG_X86_64
  94. extern char __vsyscall_0;
  95. static inline unsigned char** find_nop_table(void)
  96. {
  97. return k8_nops;
  98. }
  99. #else /* CONFIG_X86_64 */
  100. static struct nop {
  101. int cpuid;
  102. unsigned char **noptable;
  103. } noptypes[] = {
  104. { X86_FEATURE_K8, k8_nops },
  105. { X86_FEATURE_K7, k7_nops },
  106. { -1, NULL }
  107. };
  108. static unsigned char** find_nop_table(void)
  109. {
  110. unsigned char **noptable = intel_nops;
  111. int i;
  112. for (i = 0; noptypes[i].cpuid >= 0; i++) {
  113. if (boot_cpu_has(noptypes[i].cpuid)) {
  114. noptable = noptypes[i].noptable;
  115. break;
  116. }
  117. }
  118. return noptable;
  119. }
  120. #endif /* CONFIG_X86_64 */
  121. static void nop_out(void *insns, unsigned int len)
  122. {
  123. unsigned char **noptable = find_nop_table();
  124. while (len > 0) {
  125. unsigned int noplen = len;
  126. if (noplen > ASM_NOP_MAX)
  127. noplen = ASM_NOP_MAX;
  128. memcpy(insns, noptable[noplen], noplen);
  129. insns += noplen;
  130. len -= noplen;
  131. }
  132. }
  133. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  134. extern u8 *__smp_locks[], *__smp_locks_end[];
  135. /* Replace instructions with better alternatives for this CPU type.
  136. This runs before SMP is initialized to avoid SMP problems with
  137. self modifying code. This implies that assymetric systems where
  138. APs have less capabilities than the boot processor are not handled.
  139. Tough. Make sure you disable such features by hand. */
  140. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  141. {
  142. struct alt_instr *a;
  143. u8 *instr;
  144. int diff;
  145. DPRINTK("%s: alt table %p -> %p\n", __FUNCTION__, start, end);
  146. for (a = start; a < end; a++) {
  147. BUG_ON(a->replacementlen > a->instrlen);
  148. if (!boot_cpu_has(a->cpuid))
  149. continue;
  150. instr = a->instr;
  151. #ifdef CONFIG_X86_64
  152. /* vsyscall code is not mapped yet. resolve it manually. */
  153. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  154. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  155. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  156. __FUNCTION__, a->instr, instr);
  157. }
  158. #endif
  159. memcpy(instr, a->replacement, a->replacementlen);
  160. diff = a->instrlen - a->replacementlen;
  161. nop_out(instr + a->replacementlen, diff);
  162. }
  163. }
  164. #ifdef CONFIG_SMP
  165. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  166. {
  167. u8 **ptr;
  168. for (ptr = start; ptr < end; ptr++) {
  169. if (*ptr < text)
  170. continue;
  171. if (*ptr > text_end)
  172. continue;
  173. **ptr = 0xf0; /* lock prefix */
  174. };
  175. }
  176. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  177. {
  178. u8 **ptr;
  179. if (noreplace_smp)
  180. return;
  181. for (ptr = start; ptr < end; ptr++) {
  182. if (*ptr < text)
  183. continue;
  184. if (*ptr > text_end)
  185. continue;
  186. nop_out(*ptr, 1);
  187. };
  188. }
  189. struct smp_alt_module {
  190. /* what is this ??? */
  191. struct module *mod;
  192. char *name;
  193. /* ptrs to lock prefixes */
  194. u8 **locks;
  195. u8 **locks_end;
  196. /* .text segment, needed to avoid patching init code ;) */
  197. u8 *text;
  198. u8 *text_end;
  199. struct list_head next;
  200. };
  201. static LIST_HEAD(smp_alt_modules);
  202. static DEFINE_SPINLOCK(smp_alt);
  203. void alternatives_smp_module_add(struct module *mod, char *name,
  204. void *locks, void *locks_end,
  205. void *text, void *text_end)
  206. {
  207. struct smp_alt_module *smp;
  208. unsigned long flags;
  209. if (noreplace_smp)
  210. return;
  211. if (smp_alt_once) {
  212. if (boot_cpu_has(X86_FEATURE_UP))
  213. alternatives_smp_unlock(locks, locks_end,
  214. text, text_end);
  215. return;
  216. }
  217. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  218. if (NULL == smp)
  219. return; /* we'll run the (safe but slow) SMP code then ... */
  220. smp->mod = mod;
  221. smp->name = name;
  222. smp->locks = locks;
  223. smp->locks_end = locks_end;
  224. smp->text = text;
  225. smp->text_end = text_end;
  226. DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
  227. __FUNCTION__, smp->locks, smp->locks_end,
  228. smp->text, smp->text_end, smp->name);
  229. spin_lock_irqsave(&smp_alt, flags);
  230. list_add_tail(&smp->next, &smp_alt_modules);
  231. if (boot_cpu_has(X86_FEATURE_UP))
  232. alternatives_smp_unlock(smp->locks, smp->locks_end,
  233. smp->text, smp->text_end);
  234. spin_unlock_irqrestore(&smp_alt, flags);
  235. }
  236. void alternatives_smp_module_del(struct module *mod)
  237. {
  238. struct smp_alt_module *item;
  239. unsigned long flags;
  240. if (smp_alt_once || noreplace_smp)
  241. return;
  242. spin_lock_irqsave(&smp_alt, flags);
  243. list_for_each_entry(item, &smp_alt_modules, next) {
  244. if (mod != item->mod)
  245. continue;
  246. list_del(&item->next);
  247. spin_unlock_irqrestore(&smp_alt, flags);
  248. DPRINTK("%s: %s\n", __FUNCTION__, item->name);
  249. kfree(item);
  250. return;
  251. }
  252. spin_unlock_irqrestore(&smp_alt, flags);
  253. }
  254. void alternatives_smp_switch(int smp)
  255. {
  256. struct smp_alt_module *mod;
  257. unsigned long flags;
  258. #ifdef CONFIG_LOCKDEP
  259. /*
  260. * A not yet fixed binutils section handling bug prevents
  261. * alternatives-replacement from working reliably, so turn
  262. * it off:
  263. */
  264. printk("lockdep: not fixing up alternatives.\n");
  265. return;
  266. #endif
  267. if (noreplace_smp || smp_alt_once)
  268. return;
  269. BUG_ON(!smp && (num_online_cpus() > 1));
  270. spin_lock_irqsave(&smp_alt, flags);
  271. if (smp) {
  272. printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
  273. clear_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  274. clear_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
  275. list_for_each_entry(mod, &smp_alt_modules, next)
  276. alternatives_smp_lock(mod->locks, mod->locks_end,
  277. mod->text, mod->text_end);
  278. } else {
  279. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  280. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  281. set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
  282. list_for_each_entry(mod, &smp_alt_modules, next)
  283. alternatives_smp_unlock(mod->locks, mod->locks_end,
  284. mod->text, mod->text_end);
  285. }
  286. spin_unlock_irqrestore(&smp_alt, flags);
  287. }
  288. #endif
  289. #ifdef CONFIG_PARAVIRT
  290. void apply_paravirt(struct paravirt_patch_site *start,
  291. struct paravirt_patch_site *end)
  292. {
  293. struct paravirt_patch_site *p;
  294. if (noreplace_paravirt)
  295. return;
  296. for (p = start; p < end; p++) {
  297. unsigned int used;
  298. used = paravirt_ops.patch(p->instrtype, p->clobbers, p->instr,
  299. p->len);
  300. BUG_ON(used > p->len);
  301. /* Pad the rest with nops */
  302. nop_out(p->instr + used, p->len - used);
  303. }
  304. /* Sync to be conservative, in case we patched following
  305. * instructions */
  306. sync_core();
  307. }
  308. extern struct paravirt_patch_site __start_parainstructions[],
  309. __stop_parainstructions[];
  310. #endif /* CONFIG_PARAVIRT */
  311. void __init alternative_instructions(void)
  312. {
  313. unsigned long flags;
  314. local_irq_save(flags);
  315. apply_alternatives(__alt_instructions, __alt_instructions_end);
  316. /* switch to patch-once-at-boottime-only mode and free the
  317. * tables in case we know the number of CPUs will never ever
  318. * change */
  319. #ifdef CONFIG_HOTPLUG_CPU
  320. if (num_possible_cpus() < 2)
  321. smp_alt_once = 1;
  322. #else
  323. smp_alt_once = 1;
  324. #endif
  325. #ifdef CONFIG_SMP
  326. if (smp_alt_once) {
  327. if (1 == num_possible_cpus()) {
  328. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  329. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  330. set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
  331. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  332. _text, _etext);
  333. }
  334. free_init_pages("SMP alternatives",
  335. (unsigned long)__smp_locks,
  336. (unsigned long)__smp_locks_end);
  337. } else {
  338. alternatives_smp_module_add(NULL, "core kernel",
  339. __smp_locks, __smp_locks_end,
  340. _text, _etext);
  341. alternatives_smp_switch(0);
  342. }
  343. #endif
  344. apply_paravirt(__parainstructions, __parainstructions_end);
  345. local_irq_restore(flags);
  346. }