alternative.c 10 KB

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