alternative.c 11 KB

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