alternative.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/list.h>
  5. #include <linux/kprobes.h>
  6. #include <linux/mm.h>
  7. #include <linux/vmalloc.h>
  8. #include <asm/alternative.h>
  9. #include <asm/sections.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/mce.h>
  12. #include <asm/nmi.h>
  13. #ifdef CONFIG_HOTPLUG_CPU
  14. static int smp_alt_once;
  15. static int __init bootonly(char *str)
  16. {
  17. smp_alt_once = 1;
  18. return 1;
  19. }
  20. __setup("smp-alt-boot", bootonly);
  21. #else
  22. #define smp_alt_once 1
  23. #endif
  24. static int debug_alternative;
  25. static int __init debug_alt(char *str)
  26. {
  27. debug_alternative = 1;
  28. return 1;
  29. }
  30. __setup("debug-alternative", debug_alt);
  31. static int noreplace_smp;
  32. static int __init setup_noreplace_smp(char *str)
  33. {
  34. noreplace_smp = 1;
  35. return 1;
  36. }
  37. __setup("noreplace-smp", setup_noreplace_smp);
  38. #ifdef CONFIG_PARAVIRT
  39. static int noreplace_paravirt = 0;
  40. static int __init setup_noreplace_paravirt(char *str)
  41. {
  42. noreplace_paravirt = 1;
  43. return 1;
  44. }
  45. __setup("noreplace-paravirt", setup_noreplace_paravirt);
  46. #endif
  47. #define DPRINTK(fmt, args...) if (debug_alternative) \
  48. printk(KERN_DEBUG fmt, args)
  49. #ifdef GENERIC_NOP1
  50. /* Use inline assembly to define this because the nops are defined
  51. as inline assembly strings in the include files and we cannot
  52. get them easily into strings. */
  53. asm("\t.data\nintelnops: "
  54. GENERIC_NOP1 GENERIC_NOP2 GENERIC_NOP3 GENERIC_NOP4 GENERIC_NOP5 GENERIC_NOP6
  55. GENERIC_NOP7 GENERIC_NOP8);
  56. extern unsigned char intelnops[];
  57. static unsigned char *intel_nops[ASM_NOP_MAX+1] = {
  58. NULL,
  59. intelnops,
  60. intelnops + 1,
  61. intelnops + 1 + 2,
  62. intelnops + 1 + 2 + 3,
  63. intelnops + 1 + 2 + 3 + 4,
  64. intelnops + 1 + 2 + 3 + 4 + 5,
  65. intelnops + 1 + 2 + 3 + 4 + 5 + 6,
  66. intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  67. };
  68. #endif
  69. #ifdef K8_NOP1
  70. asm("\t.data\nk8nops: "
  71. K8_NOP1 K8_NOP2 K8_NOP3 K8_NOP4 K8_NOP5 K8_NOP6
  72. K8_NOP7 K8_NOP8);
  73. extern unsigned char k8nops[];
  74. static unsigned char *k8_nops[ASM_NOP_MAX+1] = {
  75. NULL,
  76. k8nops,
  77. k8nops + 1,
  78. k8nops + 1 + 2,
  79. k8nops + 1 + 2 + 3,
  80. k8nops + 1 + 2 + 3 + 4,
  81. k8nops + 1 + 2 + 3 + 4 + 5,
  82. k8nops + 1 + 2 + 3 + 4 + 5 + 6,
  83. k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  84. };
  85. #endif
  86. #ifdef K7_NOP1
  87. asm("\t.data\nk7nops: "
  88. K7_NOP1 K7_NOP2 K7_NOP3 K7_NOP4 K7_NOP5 K7_NOP6
  89. K7_NOP7 K7_NOP8);
  90. extern unsigned char k7nops[];
  91. static unsigned char *k7_nops[ASM_NOP_MAX+1] = {
  92. NULL,
  93. k7nops,
  94. k7nops + 1,
  95. k7nops + 1 + 2,
  96. k7nops + 1 + 2 + 3,
  97. k7nops + 1 + 2 + 3 + 4,
  98. k7nops + 1 + 2 + 3 + 4 + 5,
  99. k7nops + 1 + 2 + 3 + 4 + 5 + 6,
  100. k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
  101. };
  102. #endif
  103. #ifdef CONFIG_X86_64
  104. extern char __vsyscall_0;
  105. static inline unsigned char** find_nop_table(void)
  106. {
  107. return k8_nops;
  108. }
  109. #else /* CONFIG_X86_64 */
  110. static struct nop {
  111. int cpuid;
  112. unsigned char **noptable;
  113. } noptypes[] = {
  114. { X86_FEATURE_K8, k8_nops },
  115. { X86_FEATURE_K7, k7_nops },
  116. { -1, NULL }
  117. };
  118. static unsigned char** find_nop_table(void)
  119. {
  120. unsigned char **noptable = intel_nops;
  121. int i;
  122. for (i = 0; noptypes[i].cpuid >= 0; i++) {
  123. if (boot_cpu_has(noptypes[i].cpuid)) {
  124. noptable = noptypes[i].noptable;
  125. break;
  126. }
  127. }
  128. return noptable;
  129. }
  130. #endif /* CONFIG_X86_64 */
  131. static void nop_out(void *insns, unsigned int len)
  132. {
  133. unsigned char **noptable = find_nop_table();
  134. while (len > 0) {
  135. unsigned int noplen = len;
  136. if (noplen > ASM_NOP_MAX)
  137. noplen = ASM_NOP_MAX;
  138. text_poke(insns, noptable[noplen], noplen);
  139. insns += noplen;
  140. len -= noplen;
  141. }
  142. }
  143. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  144. extern u8 *__smp_locks[], *__smp_locks_end[];
  145. /* Replace instructions with better alternatives for this CPU type.
  146. This runs before SMP is initialized to avoid SMP problems with
  147. self modifying code. This implies that assymetric systems where
  148. APs have less capabilities than the boot processor are not handled.
  149. Tough. Make sure you disable such features by hand. */
  150. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  151. {
  152. struct alt_instr *a;
  153. u8 *instr;
  154. int diff;
  155. DPRINTK("%s: alt table %p -> %p\n", __FUNCTION__, start, end);
  156. for (a = start; a < end; a++) {
  157. BUG_ON(a->replacementlen > a->instrlen);
  158. if (!boot_cpu_has(a->cpuid))
  159. continue;
  160. instr = a->instr;
  161. #ifdef CONFIG_X86_64
  162. /* vsyscall code is not mapped yet. resolve it manually. */
  163. if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
  164. instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
  165. DPRINTK("%s: vsyscall fixup: %p => %p\n",
  166. __FUNCTION__, a->instr, instr);
  167. }
  168. #endif
  169. memcpy(instr, a->replacement, a->replacementlen);
  170. diff = a->instrlen - a->replacementlen;
  171. nop_out(instr + a->replacementlen, diff);
  172. }
  173. }
  174. #ifdef CONFIG_SMP
  175. static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  176. {
  177. u8 **ptr;
  178. for (ptr = start; ptr < end; ptr++) {
  179. if (*ptr < text)
  180. continue;
  181. if (*ptr > text_end)
  182. continue;
  183. text_poke(*ptr, ((unsigned char []){0xf0}), 1); /* add lock prefix */
  184. };
  185. }
  186. static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
  187. {
  188. u8 **ptr;
  189. if (noreplace_smp)
  190. return;
  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 (noreplace_smp)
  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 (smp_alt_once || noreplace_smp)
  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 (noreplace_smp || 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. list_for_each_entry(mod, &smp_alt_modules, next)
  286. alternatives_smp_lock(mod->locks, mod->locks_end,
  287. mod->text, mod->text_end);
  288. } else {
  289. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  290. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  291. set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
  292. list_for_each_entry(mod, &smp_alt_modules, next)
  293. alternatives_smp_unlock(mod->locks, mod->locks_end,
  294. mod->text, mod->text_end);
  295. }
  296. spin_unlock_irqrestore(&smp_alt, flags);
  297. }
  298. #endif
  299. #ifdef CONFIG_PARAVIRT
  300. void apply_paravirt(struct paravirt_patch_site *start,
  301. struct paravirt_patch_site *end)
  302. {
  303. struct paravirt_patch_site *p;
  304. if (noreplace_paravirt)
  305. return;
  306. for (p = start; p < end; p++) {
  307. unsigned int used;
  308. used = paravirt_ops.patch(p->instrtype, p->clobbers, p->instr,
  309. p->len);
  310. BUG_ON(used > p->len);
  311. /* Pad the rest with nops */
  312. nop_out(p->instr + used, p->len - used);
  313. }
  314. }
  315. extern struct paravirt_patch_site __start_parainstructions[],
  316. __stop_parainstructions[];
  317. #endif /* CONFIG_PARAVIRT */
  318. void __init alternative_instructions(void)
  319. {
  320. unsigned long flags;
  321. /* The patching is not fully atomic, so try to avoid local interruptions
  322. that might execute the to be patched code.
  323. Other CPUs are not running. */
  324. stop_nmi();
  325. #ifdef CONFIG_MCE
  326. stop_mce();
  327. #endif
  328. local_irq_save(flags);
  329. apply_alternatives(__alt_instructions, __alt_instructions_end);
  330. /* switch to patch-once-at-boottime-only mode and free the
  331. * tables in case we know the number of CPUs will never ever
  332. * change */
  333. #ifdef CONFIG_HOTPLUG_CPU
  334. if (num_possible_cpus() < 2)
  335. smp_alt_once = 1;
  336. #endif
  337. #ifdef CONFIG_SMP
  338. if (smp_alt_once) {
  339. if (1 == num_possible_cpus()) {
  340. printk(KERN_INFO "SMP alternatives: switching to UP code\n");
  341. set_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
  342. set_bit(X86_FEATURE_UP, cpu_data[0].x86_capability);
  343. alternatives_smp_unlock(__smp_locks, __smp_locks_end,
  344. _text, _etext);
  345. }
  346. free_init_pages("SMP alternatives",
  347. (unsigned long)__smp_locks,
  348. (unsigned long)__smp_locks_end);
  349. } else {
  350. alternatives_smp_module_add(NULL, "core kernel",
  351. __smp_locks, __smp_locks_end,
  352. _text, _etext);
  353. alternatives_smp_switch(0);
  354. }
  355. #endif
  356. apply_paravirt(__parainstructions, __parainstructions_end);
  357. local_irq_restore(flags);
  358. restart_nmi();
  359. #ifdef CONFIG_MCE
  360. restart_mce();
  361. #endif
  362. }
  363. /*
  364. * Warning:
  365. * When you use this code to patch more than one byte of an instruction
  366. * you need to make sure that other CPUs cannot execute this code in parallel.
  367. * Also no thread must be currently preempted in the middle of these instructions.
  368. * And on the local CPU you need to be protected again NMI or MCE handlers
  369. * seeing an inconsistent instruction while you patch.
  370. */
  371. void __kprobes text_poke(void *addr, unsigned char *opcode, int len)
  372. {
  373. memcpy(addr, opcode, len);
  374. sync_core();
  375. /* Not strictly needed, but can speed CPU recovery up. Ignore cross cacheline
  376. case. */
  377. if (cpu_has_clflush)
  378. asm("clflush (%0) " :: "r" (addr) : "memory");
  379. }