alternative.c 9.6 KB

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