alternative.c 11 KB

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