alternative.c 9.5 KB

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