alternative.c 10 KB

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