blob: 1f84794f0759327c387d602cddccb3d479188f92 [file] [log] [blame]
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -04002#include <linux/sched.h>
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +02003#include <linux/mutex.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08004#include <linux/list.h>
Jan Beulich8b5a10f2009-08-19 08:40:48 +01005#include <linux/stringify.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +02006#include <linux/kprobes.h>
7#include <linux/mm.h>
8#include <linux/vmalloc.h>
Masami Hiramatsu3945dab2009-03-06 10:37:22 -05009#include <linux/memory.h>
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -050010#include <linux/stop_machine.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080012#include <asm/alternative.h>
13#include <asm/sections.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020014#include <asm/pgtable.h>
Andi Kleen8f4e9562007-07-22 11:12:32 +020015#include <asm/mce.h>
16#include <asm/nmi.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050017#include <asm/cacheflush.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050018#include <asm/tlbflush.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050019#include <asm/io.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050020#include <asm/fixmap.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080021
Andi Kleenab144f52007-08-10 22:31:03 +020022#define MAX_PATCH_LEN (255-1)
23
Jan Beulich09488162007-07-21 17:10:25 +020024#ifdef CONFIG_HOTPLUG_CPU
25static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080026
Gerd Hoffmannd167a512006-06-26 13:56:16 +020027static int __init bootonly(char *str)
28{
29 smp_alt_once = 1;
30 return 1;
31}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020032__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020033#else
34#define smp_alt_once 1
35#endif
36
Jan Beulich8b5a10f2009-08-19 08:40:48 +010037static int __initdata_or_module debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020038
Gerd Hoffmannd167a512006-06-26 13:56:16 +020039static int __init debug_alt(char *str)
40{
41 debug_alternative = 1;
42 return 1;
43}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020044__setup("debug-alternative", debug_alt);
45
Jan Beulich09488162007-07-21 17:10:25 +020046static int noreplace_smp;
47
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020048static int __init setup_noreplace_smp(char *str)
49{
50 noreplace_smp = 1;
51 return 1;
52}
53__setup("noreplace-smp", setup_noreplace_smp);
54
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020055#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +010056static int __initdata_or_module noreplace_paravirt = 0;
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020057
58static int __init setup_noreplace_paravirt(char *str)
59{
60 noreplace_paravirt = 1;
61 return 1;
62}
63__setup("noreplace-paravirt", setup_noreplace_paravirt);
64#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020065
Gerd Hoffmannd167a512006-06-26 13:56:16 +020066#define DPRINTK(fmt, args...) if (debug_alternative) \
67 printk(KERN_DEBUG fmt, args)
68
H. Peter Anvindc326fc2011-04-18 15:19:51 -070069/*
70 * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
71 * that correspond to that nop. Getting from one nop to the next, we
72 * add to the array the offset that is equal to the sum of all sizes of
73 * nops preceding the one we are after.
74 *
75 * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
76 * nice symmetry of sizes of the previous nops.
77 */
Jan Beulich8b5a10f2009-08-19 08:40:48 +010078#if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -070079static const unsigned char intelnops[] =
80{
81 GENERIC_NOP1,
82 GENERIC_NOP2,
83 GENERIC_NOP3,
84 GENERIC_NOP4,
85 GENERIC_NOP5,
86 GENERIC_NOP6,
87 GENERIC_NOP7,
88 GENERIC_NOP8,
89 GENERIC_NOP5_ATOMIC
90};
91static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
92{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080093 NULL,
94 intelnops,
95 intelnops + 1,
96 intelnops + 1 + 2,
97 intelnops + 1 + 2 + 3,
98 intelnops + 1 + 2 + 3 + 4,
99 intelnops + 1 + 2 + 3 + 4 + 5,
100 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
101 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700102 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800103};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200104#endif
105
106#ifdef K8_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700107static const unsigned char k8nops[] =
108{
109 K8_NOP1,
110 K8_NOP2,
111 K8_NOP3,
112 K8_NOP4,
113 K8_NOP5,
114 K8_NOP6,
115 K8_NOP7,
116 K8_NOP8,
117 K8_NOP5_ATOMIC
118};
119static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
120{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800121 NULL,
122 k8nops,
123 k8nops + 1,
124 k8nops + 1 + 2,
125 k8nops + 1 + 2 + 3,
126 k8nops + 1 + 2 + 3 + 4,
127 k8nops + 1 + 2 + 3 + 4 + 5,
128 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
129 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700130 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800131};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200132#endif
133
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100134#if defined(K7_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700135static const unsigned char k7nops[] =
136{
137 K7_NOP1,
138 K7_NOP2,
139 K7_NOP3,
140 K7_NOP4,
141 K7_NOP5,
142 K7_NOP6,
143 K7_NOP7,
144 K7_NOP8,
145 K7_NOP5_ATOMIC
146};
147static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
148{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800149 NULL,
150 k7nops,
151 k7nops + 1,
152 k7nops + 1 + 2,
153 k7nops + 1 + 2 + 3,
154 k7nops + 1 + 2 + 3 + 4,
155 k7nops + 1 + 2 + 3 + 4 + 5,
156 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
157 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700158 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800159};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200160#endif
161
Jan Beulich32c464f2007-10-17 18:04:41 +0200162#ifdef P6_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700163static const unsigned char __initconst_or_module p6nops[] =
164{
165 P6_NOP1,
166 P6_NOP2,
167 P6_NOP3,
168 P6_NOP4,
169 P6_NOP5,
170 P6_NOP6,
171 P6_NOP7,
172 P6_NOP8,
173 P6_NOP5_ATOMIC
174};
175static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
176{
Jan Beulich32c464f2007-10-17 18:04:41 +0200177 NULL,
178 p6nops,
179 p6nops + 1,
180 p6nops + 1 + 2,
181 p6nops + 1 + 2 + 3,
182 p6nops + 1 + 2 + 3 + 4,
183 p6nops + 1 + 2 + 3 + 4 + 5,
184 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
185 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700186 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Jan Beulich32c464f2007-10-17 18:04:41 +0200187};
188#endif
189
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700190/* Initialize these to a safe default */
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200191#ifdef CONFIG_X86_64
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700192const unsigned char * const *ideal_nops = p6_nops;
193#else
194const unsigned char * const *ideal_nops = intel_nops;
195#endif
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200196
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700197void __init arch_init_ideal_nops(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200198{
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700199 switch (boot_cpu_data.x86_vendor) {
200 case X86_VENDOR_INTEL:
H. Peter Anvind8d97662011-04-18 15:31:57 -0700201 /*
202 * Due to a decoder implementation quirk, some
203 * specific Intel CPUs actually perform better with
204 * the "k8_nops" than with the SDM-recommended NOPs.
205 */
206 if (boot_cpu_data.x86 == 6 &&
207 boot_cpu_data.x86_model >= 0x0f &&
208 boot_cpu_data.x86_model != 0x1c &&
209 boot_cpu_data.x86_model != 0x26 &&
210 boot_cpu_data.x86_model != 0x27 &&
211 boot_cpu_data.x86_model < 0x30) {
212 ideal_nops = k8_nops;
213 } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700214 ideal_nops = p6_nops;
215 } else {
216#ifdef CONFIG_X86_64
217 ideal_nops = k8_nops;
218#else
219 ideal_nops = intel_nops;
220#endif
221 }
222
223 default:
224#ifdef CONFIG_X86_64
225 ideal_nops = k8_nops;
226#else
227 if (boot_cpu_has(X86_FEATURE_K8))
228 ideal_nops = k8_nops;
229 else if (boot_cpu_has(X86_FEATURE_K7))
230 ideal_nops = k7_nops;
231 else
232 ideal_nops = intel_nops;
233#endif
234 }
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200235}
236
Andi Kleenab144f52007-08-10 22:31:03 +0200237/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100238static void __init_or_module add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100239{
Rusty Russell139ec7c2006-12-07 02:14:08 +0100240 while (len > 0) {
241 unsigned int noplen = len;
242 if (noplen > ASM_NOP_MAX)
243 noplen = ASM_NOP_MAX;
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700244 memcpy(insns, ideal_nops[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100245 insns += noplen;
246 len -= noplen;
247 }
248}
249
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200250extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Jan Beulich5967ed82010-04-21 16:08:14 +0100251extern s32 __smp_locks[], __smp_locks_end[];
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400252void *text_poke_early(void *addr, const void *opcode, size_t len);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200253
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800254/* Replace instructions with better alternatives for this CPU type.
255 This runs before SMP is initialized to avoid SMP problems with
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300256 self modifying code. This implies that asymmetric systems where
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800257 APs have less capabilities than the boot processor are not handled.
258 Tough. Make sure you disable such features by hand. */
259
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100260void __init_or_module apply_alternatives(struct alt_instr *start,
261 struct alt_instr *end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800262{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800263 struct alt_instr *a;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400264 u8 *instr, *replacement;
Jan Beulich1b1d9252009-12-18 16:12:56 +0000265 u8 insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800266
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800267 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Fenghua Yu50973132011-05-17 15:29:12 -0700268 /*
269 * The scan order should be from start to end. A later scanned
270 * alternative code can overwrite a previous scanned alternative code.
271 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
272 * patch code.
273 *
274 * So be careful if you want to change the scan order to any other
275 * order.
276 */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800277 for (a = start; a < end; a++) {
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400278 instr = (u8 *)&a->instr_offset + a->instr_offset;
279 replacement = (u8 *)&a->repl_offset + a->repl_offset;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800280 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200281 BUG_ON(a->instrlen > sizeof(insnbuf));
H. Peter Anvin3b770a22010-07-13 14:57:50 -0700282 BUG_ON(a->cpuid >= NCAPINTS*32);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800283 if (!boot_cpu_has(a->cpuid))
284 continue;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400285
286 memcpy(insnbuf, replacement, a->replacementlen);
287
288 /* 0xe8 is a relative jump; fix the offset. */
289 if (*insnbuf == 0xe8 && a->replacementlen == 5)
290 *(s32 *)(insnbuf + 1) += replacement - instr;
291
292 add_nops(insnbuf + a->replacementlen,
293 a->instrlen - a->replacementlen);
294
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500295 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800296 }
297}
298
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700299#ifdef CONFIG_SMP
300
Jan Beulich5967ed82010-04-21 16:08:14 +0100301static void alternatives_smp_lock(const s32 *start, const s32 *end,
302 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800303{
Jan Beulich5967ed82010-04-21 16:08:14 +0100304 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800305
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500306 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100307 for (poff = start; poff < end; poff++) {
308 u8 *ptr = (u8 *)poff + *poff;
309
310 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800311 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400312 /* turn DS segment override prefix into lock prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700313 if (*ptr == 0x3e)
314 text_poke(ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800315 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500316 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800317}
318
Jan Beulich5967ed82010-04-21 16:08:14 +0100319static void alternatives_smp_unlock(const s32 *start, const s32 *end,
320 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800321{
Jan Beulich5967ed82010-04-21 16:08:14 +0100322 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800323
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200324 if (noreplace_smp)
325 return;
326
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500327 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100328 for (poff = start; poff < end; poff++) {
329 u8 *ptr = (u8 *)poff + *poff;
330
331 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800332 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400333 /* turn lock prefix into DS segment override prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700334 if (*ptr == 0xf0)
335 text_poke(ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800336 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500337 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800338}
339
340struct smp_alt_module {
341 /* what is this ??? */
342 struct module *mod;
343 char *name;
344
345 /* ptrs to lock prefixes */
Jan Beulich5967ed82010-04-21 16:08:14 +0100346 const s32 *locks;
347 const s32 *locks_end;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800348
349 /* .text segment, needed to avoid patching init code ;) */
350 u8 *text;
351 u8 *text_end;
352
353 struct list_head next;
354};
355static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200356static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100357static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800358
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100359void __init_or_module alternatives_smp_module_add(struct module *mod,
360 char *name,
361 void *locks, void *locks_end,
362 void *text, void *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800363{
364 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800365
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200366 if (noreplace_smp)
367 return;
368
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800369 if (smp_alt_once) {
370 if (boot_cpu_has(X86_FEATURE_UP))
371 alternatives_smp_unlock(locks, locks_end,
372 text, text_end);
373 return;
374 }
375
376 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
377 if (NULL == smp)
378 return; /* we'll run the (safe but slow) SMP code then ... */
379
380 smp->mod = mod;
381 smp->name = name;
382 smp->locks = locks;
383 smp->locks_end = locks_end;
384 smp->text = text;
385 smp->text_end = text_end;
386 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800387 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800388 smp->text, smp->text_end, smp->name);
389
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200390 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800391 list_add_tail(&smp->next, &smp_alt_modules);
392 if (boot_cpu_has(X86_FEATURE_UP))
393 alternatives_smp_unlock(smp->locks, smp->locks_end,
394 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200395 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800396}
397
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100398void __init_or_module alternatives_smp_module_del(struct module *mod)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800399{
400 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800401
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200402 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800403 return;
404
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200405 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800406 list_for_each_entry(item, &smp_alt_modules, next) {
407 if (mod != item->mod)
408 continue;
409 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200410 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800411 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800412 kfree(item);
413 return;
414 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200415 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800416}
417
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800418bool skip_smp_alternatives;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800419void alternatives_smp_switch(int smp)
420{
421 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800422
Ingo Molnar3047e992006-07-03 00:24:57 -0700423#ifdef CONFIG_LOCKDEP
424 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100425 * Older binutils section handling bug prevented
426 * alternatives-replacement from working reliably.
427 *
428 * If this still occurs then you should see a hang
429 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700430 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100431 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700432#endif
433
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800434 if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800435 return;
436 BUG_ON(!smp && (num_online_cpus() > 1));
437
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200438 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100439
440 /*
441 * Avoid unnecessary switches because it forces JIT based VMs to
442 * throw away all cached translations, which can be quite costly.
443 */
444 if (smp == smp_mode) {
445 /* nothing */
446 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800447 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100448 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
449 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800450 list_for_each_entry(mod, &smp_alt_modules, next)
451 alternatives_smp_lock(mod->locks, mod->locks_end,
452 mod->text, mod->text_end);
453 } else {
454 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100455 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
456 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800457 list_for_each_entry(mod, &smp_alt_modules, next)
458 alternatives_smp_unlock(mod->locks, mod->locks_end,
459 mod->text, mod->text_end);
460 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100461 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200462 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800463}
464
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500465/* Return 1 if the address range is reserved for smp-alternatives */
466int alternatives_text_reserved(void *start, void *end)
467{
468 struct smp_alt_module *mod;
Jan Beulich5967ed82010-04-21 16:08:14 +0100469 const s32 *poff;
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500470 u8 *text_start = start;
471 u8 *text_end = end;
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500472
473 list_for_each_entry(mod, &smp_alt_modules, next) {
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500474 if (mod->text > text_end || mod->text_end < text_start)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500475 continue;
Jan Beulich5967ed82010-04-21 16:08:14 +0100476 for (poff = mod->locks; poff < mod->locks_end; poff++) {
477 const u8 *ptr = (const u8 *)poff + *poff;
478
479 if (text_start <= ptr && text_end > ptr)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500480 return 1;
Jan Beulich5967ed82010-04-21 16:08:14 +0100481 }
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500482 }
483
484 return 0;
485}
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700486#endif
487
Rusty Russell139ec7c2006-12-07 02:14:08 +0100488#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100489void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
490 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100491{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200492 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200493 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100494
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200495 if (noreplace_paravirt)
496 return;
497
Rusty Russell139ec7c2006-12-07 02:14:08 +0100498 for (p = start; p < end; p++) {
499 unsigned int used;
500
Andi Kleenab144f52007-08-10 22:31:03 +0200501 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700502 /* prep the buffer with the original instructions */
503 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700504 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
505 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200506
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200507 BUG_ON(used > p->len);
508
Rusty Russell139ec7c2006-12-07 02:14:08 +0100509 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200510 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500511 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100512 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100513}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200514extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100515 __stop_parainstructions[];
516#endif /* CONFIG_PARAVIRT */
517
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800518void __init alternative_instructions(void)
519{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200520 /* The patching is not fully atomic, so try to avoid local interruptions
521 that might execute the to be patched code.
522 Other CPUs are not running. */
523 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100524
525 /*
526 * Don't stop machine check exceptions while patching.
527 * MCEs only happen when something got corrupted and in this
528 * case we must do something about the corruption.
529 * Ignoring it is worse than a unlikely patching race.
530 * Also machine checks tend to be broadcast and if one CPU
531 * goes into machine check the others follow quickly, so we don't
532 * expect a machine check to cause undue problems during to code
533 * patching.
534 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200535
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800536 apply_alternatives(__alt_instructions, __alt_instructions_end);
537
538 /* switch to patch-once-at-boottime-only mode and free the
539 * tables in case we know the number of CPUs will never ever
540 * change */
541#ifdef CONFIG_HOTPLUG_CPU
542 if (num_possible_cpus() < 2)
543 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800544#endif
545
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700546#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800547 if (smp_alt_once) {
548 if (1 == num_possible_cpus()) {
549 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100550 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
551 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
552
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800553 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
554 _text, _etext);
555 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800556 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800557 alternatives_smp_module_add(NULL, "core kernel",
558 __smp_locks, __smp_locks_end,
559 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100560
561 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200562 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100563 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800564 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700565#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200566 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200567
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200568 if (smp_alt_once)
569 free_init_pages("SMP alternatives",
570 (unsigned long)__smp_locks,
571 (unsigned long)__smp_locks_end);
572
Andi Kleen8f4e9562007-07-22 11:12:32 +0200573 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800574}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200575
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500576/**
577 * text_poke_early - Update instructions on a live kernel at boot time
578 * @addr: address to modify
579 * @opcode: source of the copy
580 * @len: length to copy
581 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200582 * When you use this code to patch more than one byte of an instruction
583 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500584 * Also no thread must be currently preempted in the middle of these
585 * instructions. And on the local CPU you need to be protected again NMI or MCE
586 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200587 */
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400588void *__init_or_module text_poke_early(void *addr, const void *opcode,
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100589 size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200590{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500591 unsigned long flags;
592 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200593 memcpy(addr, opcode, len);
594 sync_core();
Ben Hutchings5367b682009-09-10 02:53:50 +0100595 local_irq_restore(flags);
Andi Kleena534b672007-09-06 16:59:52 +0200596 /* Could also do a CLFLUSH here to speed up CPU recovery; but
597 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500598 return addr;
599}
600
601/**
602 * text_poke - Update instructions on a live kernel
603 * @addr: address to modify
604 * @opcode: source of the copy
605 * @len: length to copy
606 *
607 * Only atomic text poke/set should be allowed when not doing early patching.
608 * It means the size must be writable atomically and the address must be aligned
609 * in a way that permits an atomic write. It also makes sure we fit on a single
610 * page.
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500611 *
612 * Note: Must be called under text_mutex.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500613 */
614void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
615{
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500616 unsigned long flags;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500617 char *vaddr;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400618 struct page *pages[2];
619 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500620
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400621 if (!core_kernel_text((unsigned long)addr)) {
622 pages[0] = vmalloc_to_page(addr);
623 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400624 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400625 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200626 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400627 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500628 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400629 BUG_ON(!pages[0]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400630 local_irq_save(flags);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500631 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
632 if (pages[1])
633 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
634 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400635 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500636 clear_fixmap(FIX_TEXT_POKE0);
637 if (pages[1])
638 clear_fixmap(FIX_TEXT_POKE1);
639 local_flush_tlb();
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500640 sync_core();
641 /* Could also do a CLFLUSH here to speed up CPU recovery; but
642 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400643 for (i = 0; i < len; i++)
644 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400645 local_irq_restore(flags);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500646 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200647}
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500648
649/*
650 * Cross-modifying kernel text with stop_machine().
651 * This code originally comes from immediate value.
652 */
653static atomic_t stop_machine_first;
654static int wrote_text;
655
656struct text_poke_params {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900657 struct text_poke_param *params;
658 int nparams;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500659};
660
661static int __kprobes stop_machine_text_poke(void *data)
662{
663 struct text_poke_params *tpp = data;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900664 struct text_poke_param *p;
665 int i;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500666
667 if (atomic_dec_and_test(&stop_machine_first)) {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900668 for (i = 0; i < tpp->nparams; i++) {
669 p = &tpp->params[i];
670 text_poke(p->addr, p->opcode, p->len);
671 }
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500672 smp_wmb(); /* Make sure other cpus see that this has run */
673 wrote_text = 1;
674 } else {
675 while (!wrote_text)
Masami Hiramatsue5a11012010-03-03 22:38:50 -0500676 cpu_relax();
677 smp_mb(); /* Load wrote_text before following execution */
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500678 }
679
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900680 for (i = 0; i < tpp->nparams; i++) {
681 p = &tpp->params[i];
682 flush_icache_range((unsigned long)p->addr,
683 (unsigned long)p->addr + p->len);
684 }
Mathieu Desnoyers0e00f7a2011-03-03 11:01:37 -0500685 /*
686 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
687 * that a core serializing instruction such as "cpuid" should be
688 * executed on _each_ core before the new instruction is made visible.
689 */
690 sync_core();
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500691 return 0;
692}
693
694/**
695 * text_poke_smp - Update instructions on a live kernel on SMP
696 * @addr: address to modify
697 * @opcode: source of the copy
698 * @len: length to copy
699 *
700 * Modify multi-byte instruction by using stop_machine() on SMP. This allows
701 * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
702 * should be allowed, since stop_machine() does _not_ protect code against
703 * NMI and MCE.
704 *
705 * Note: Must be called under get_online_cpus() and text_mutex.
706 */
707void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
708{
709 struct text_poke_params tpp;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900710 struct text_poke_param p;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500711
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900712 p.addr = addr;
713 p.opcode = opcode;
714 p.len = len;
715 tpp.params = &p;
716 tpp.nparams = 1;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500717 atomic_set(&stop_machine_first, 1);
718 wrote_text = 0;
Masami Hiramatsu3caa37512010-10-14 12:10:36 +0900719 /* Use __stop_machine() because the caller already got online_cpus. */
Jason Baron404ba5d2010-10-28 11:20:27 -0400720 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500721 return addr;
722}
723
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900724/**
725 * text_poke_smp_batch - Update instructions on a live kernel on SMP
726 * @params: an array of text_poke parameters
727 * @n: the number of elements in params.
728 *
729 * Modify multi-byte instruction by using stop_machine() on SMP. Since the
730 * stop_machine() is heavy task, it is better to aggregate text_poke requests
731 * and do it once if possible.
732 *
733 * Note: Must be called under get_online_cpus() and text_mutex.
734 */
735void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
736{
737 struct text_poke_params tpp = {.params = params, .nparams = n};
738
739 atomic_set(&stop_machine_first, 1);
740 wrote_text = 0;
Rabin Vincent78345d22011-10-27 13:24:32 +0530741 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900742}