blob: 1729d720299a0950f57320636274f30787696bbd [file] [log] [blame]
Joe Perchesc767a542012-05-21 19:50:07 -07001#define pr_fmt(fmt) "SMP alternatives: " fmt
2
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08003#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -04004#include <linux/sched.h>
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +02005#include <linux/mutex.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08006#include <linux/list.h>
Jan Beulich8b5a10f2009-08-19 08:40:48 +01007#include <linux/stringify.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +02008#include <linux/kprobes.h>
9#include <linux/mm.h>
10#include <linux/vmalloc.h>
Masami Hiramatsu3945dab2009-03-06 10:37:22 -050011#include <linux/memory.h>
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -050012#include <linux/stop_machine.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080014#include <asm/alternative.h>
15#include <asm/sections.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020016#include <asm/pgtable.h>
Andi Kleen8f4e9562007-07-22 11:12:32 +020017#include <asm/mce.h>
18#include <asm/nmi.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050019#include <asm/cacheflush.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050020#include <asm/tlbflush.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050021#include <asm/io.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050022#include <asm/fixmap.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080023
Andi Kleenab144f52007-08-10 22:31:03 +020024#define MAX_PATCH_LEN (255-1)
25
Jan Beulich09488162007-07-21 17:10:25 +020026#ifdef CONFIG_HOTPLUG_CPU
27static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080028
Gerd Hoffmannd167a512006-06-26 13:56:16 +020029static int __init bootonly(char *str)
30{
31 smp_alt_once = 1;
32 return 1;
33}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020034__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020035#else
36#define smp_alt_once 1
37#endif
38
Jan Beulich8b5a10f2009-08-19 08:40:48 +010039static int __initdata_or_module debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020040
Gerd Hoffmannd167a512006-06-26 13:56:16 +020041static int __init debug_alt(char *str)
42{
43 debug_alternative = 1;
44 return 1;
45}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020046__setup("debug-alternative", debug_alt);
47
Jan Beulich09488162007-07-21 17:10:25 +020048static int noreplace_smp;
49
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020050static int __init setup_noreplace_smp(char *str)
51{
52 noreplace_smp = 1;
53 return 1;
54}
55__setup("noreplace-smp", setup_noreplace_smp);
56
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020057#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +010058static int __initdata_or_module noreplace_paravirt = 0;
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020059
60static int __init setup_noreplace_paravirt(char *str)
61{
62 noreplace_paravirt = 1;
63 return 1;
64}
65__setup("noreplace-paravirt", setup_noreplace_paravirt);
66#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020067
Joe Perchesc767a542012-05-21 19:50:07 -070068#define DPRINTK(fmt, ...) \
69do { \
70 if (debug_alternative) \
71 printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
72} while (0)
Gerd Hoffmannd167a512006-06-26 13:56:16 +020073
H. Peter Anvindc326fc2011-04-18 15:19:51 -070074/*
75 * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
76 * that correspond to that nop. Getting from one nop to the next, we
77 * add to the array the offset that is equal to the sum of all sizes of
78 * nops preceding the one we are after.
79 *
80 * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
81 * nice symmetry of sizes of the previous nops.
82 */
Jan Beulich8b5a10f2009-08-19 08:40:48 +010083#if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -070084static const unsigned char intelnops[] =
85{
86 GENERIC_NOP1,
87 GENERIC_NOP2,
88 GENERIC_NOP3,
89 GENERIC_NOP4,
90 GENERIC_NOP5,
91 GENERIC_NOP6,
92 GENERIC_NOP7,
93 GENERIC_NOP8,
94 GENERIC_NOP5_ATOMIC
95};
96static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
97{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080098 NULL,
99 intelnops,
100 intelnops + 1,
101 intelnops + 1 + 2,
102 intelnops + 1 + 2 + 3,
103 intelnops + 1 + 2 + 3 + 4,
104 intelnops + 1 + 2 + 3 + 4 + 5,
105 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
106 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700107 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800108};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200109#endif
110
111#ifdef K8_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700112static const unsigned char k8nops[] =
113{
114 K8_NOP1,
115 K8_NOP2,
116 K8_NOP3,
117 K8_NOP4,
118 K8_NOP5,
119 K8_NOP6,
120 K8_NOP7,
121 K8_NOP8,
122 K8_NOP5_ATOMIC
123};
124static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
125{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800126 NULL,
127 k8nops,
128 k8nops + 1,
129 k8nops + 1 + 2,
130 k8nops + 1 + 2 + 3,
131 k8nops + 1 + 2 + 3 + 4,
132 k8nops + 1 + 2 + 3 + 4 + 5,
133 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
134 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700135 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800136};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200137#endif
138
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100139#if defined(K7_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700140static const unsigned char k7nops[] =
141{
142 K7_NOP1,
143 K7_NOP2,
144 K7_NOP3,
145 K7_NOP4,
146 K7_NOP5,
147 K7_NOP6,
148 K7_NOP7,
149 K7_NOP8,
150 K7_NOP5_ATOMIC
151};
152static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
153{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800154 NULL,
155 k7nops,
156 k7nops + 1,
157 k7nops + 1 + 2,
158 k7nops + 1 + 2 + 3,
159 k7nops + 1 + 2 + 3 + 4,
160 k7nops + 1 + 2 + 3 + 4 + 5,
161 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
162 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700163 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800164};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200165#endif
166
Jan Beulich32c464f2007-10-17 18:04:41 +0200167#ifdef P6_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700168static const unsigned char __initconst_or_module p6nops[] =
169{
170 P6_NOP1,
171 P6_NOP2,
172 P6_NOP3,
173 P6_NOP4,
174 P6_NOP5,
175 P6_NOP6,
176 P6_NOP7,
177 P6_NOP8,
178 P6_NOP5_ATOMIC
179};
180static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
181{
Jan Beulich32c464f2007-10-17 18:04:41 +0200182 NULL,
183 p6nops,
184 p6nops + 1,
185 p6nops + 1 + 2,
186 p6nops + 1 + 2 + 3,
187 p6nops + 1 + 2 + 3 + 4,
188 p6nops + 1 + 2 + 3 + 4 + 5,
189 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
190 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700191 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Jan Beulich32c464f2007-10-17 18:04:41 +0200192};
193#endif
194
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700195/* Initialize these to a safe default */
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200196#ifdef CONFIG_X86_64
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700197const unsigned char * const *ideal_nops = p6_nops;
198#else
199const unsigned char * const *ideal_nops = intel_nops;
200#endif
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200201
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700202void __init arch_init_ideal_nops(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200203{
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700204 switch (boot_cpu_data.x86_vendor) {
205 case X86_VENDOR_INTEL:
H. Peter Anvind8d97662011-04-18 15:31:57 -0700206 /*
207 * Due to a decoder implementation quirk, some
208 * specific Intel CPUs actually perform better with
209 * the "k8_nops" than with the SDM-recommended NOPs.
210 */
211 if (boot_cpu_data.x86 == 6 &&
212 boot_cpu_data.x86_model >= 0x0f &&
213 boot_cpu_data.x86_model != 0x1c &&
214 boot_cpu_data.x86_model != 0x26 &&
215 boot_cpu_data.x86_model != 0x27 &&
216 boot_cpu_data.x86_model < 0x30) {
217 ideal_nops = k8_nops;
218 } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700219 ideal_nops = p6_nops;
220 } else {
221#ifdef CONFIG_X86_64
222 ideal_nops = k8_nops;
223#else
224 ideal_nops = intel_nops;
225#endif
226 }
227
228 default:
229#ifdef CONFIG_X86_64
230 ideal_nops = k8_nops;
231#else
232 if (boot_cpu_has(X86_FEATURE_K8))
233 ideal_nops = k8_nops;
234 else if (boot_cpu_has(X86_FEATURE_K7))
235 ideal_nops = k7_nops;
236 else
237 ideal_nops = intel_nops;
238#endif
239 }
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200240}
241
Andi Kleenab144f52007-08-10 22:31:03 +0200242/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100243static void __init_or_module add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100244{
Rusty Russell139ec7c2006-12-07 02:14:08 +0100245 while (len > 0) {
246 unsigned int noplen = len;
247 if (noplen > ASM_NOP_MAX)
248 noplen = ASM_NOP_MAX;
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700249 memcpy(insns, ideal_nops[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100250 insns += noplen;
251 len -= noplen;
252 }
253}
254
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200255extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Jan Beulich5967ed82010-04-21 16:08:14 +0100256extern s32 __smp_locks[], __smp_locks_end[];
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400257void *text_poke_early(void *addr, const void *opcode, size_t len);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200258
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800259/* Replace instructions with better alternatives for this CPU type.
260 This runs before SMP is initialized to avoid SMP problems with
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300261 self modifying code. This implies that asymmetric systems where
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800262 APs have less capabilities than the boot processor are not handled.
263 Tough. Make sure you disable such features by hand. */
264
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100265void __init_or_module apply_alternatives(struct alt_instr *start,
266 struct alt_instr *end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800267{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800268 struct alt_instr *a;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400269 u8 *instr, *replacement;
Jan Beulich1b1d9252009-12-18 16:12:56 +0000270 u8 insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800271
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800272 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Fenghua Yu50973132011-05-17 15:29:12 -0700273 /*
274 * The scan order should be from start to end. A later scanned
275 * alternative code can overwrite a previous scanned alternative code.
276 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
277 * patch code.
278 *
279 * So be careful if you want to change the scan order to any other
280 * order.
281 */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800282 for (a = start; a < end; a++) {
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400283 instr = (u8 *)&a->instr_offset + a->instr_offset;
284 replacement = (u8 *)&a->repl_offset + a->repl_offset;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800285 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200286 BUG_ON(a->instrlen > sizeof(insnbuf));
H. Peter Anvin3b770a22010-07-13 14:57:50 -0700287 BUG_ON(a->cpuid >= NCAPINTS*32);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800288 if (!boot_cpu_has(a->cpuid))
289 continue;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400290
291 memcpy(insnbuf, replacement, a->replacementlen);
292
293 /* 0xe8 is a relative jump; fix the offset. */
294 if (*insnbuf == 0xe8 && a->replacementlen == 5)
295 *(s32 *)(insnbuf + 1) += replacement - instr;
296
297 add_nops(insnbuf + a->replacementlen,
298 a->instrlen - a->replacementlen);
299
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500300 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800301 }
302}
303
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700304#ifdef CONFIG_SMP
305
Jan Beulich5967ed82010-04-21 16:08:14 +0100306static void alternatives_smp_lock(const s32 *start, const s32 *end,
307 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800308{
Jan Beulich5967ed82010-04-21 16:08:14 +0100309 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800310
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500311 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100312 for (poff = start; poff < end; poff++) {
313 u8 *ptr = (u8 *)poff + *poff;
314
315 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800316 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400317 /* turn DS segment override prefix into lock prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700318 if (*ptr == 0x3e)
319 text_poke(ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800320 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500321 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800322}
323
Jan Beulich5967ed82010-04-21 16:08:14 +0100324static void alternatives_smp_unlock(const s32 *start, const s32 *end,
325 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800326{
Jan Beulich5967ed82010-04-21 16:08:14 +0100327 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800328
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200329 if (noreplace_smp)
330 return;
331
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500332 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100333 for (poff = start; poff < end; poff++) {
334 u8 *ptr = (u8 *)poff + *poff;
335
336 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800337 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400338 /* turn lock prefix into DS segment override prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700339 if (*ptr == 0xf0)
340 text_poke(ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800341 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500342 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800343}
344
345struct smp_alt_module {
346 /* what is this ??? */
347 struct module *mod;
348 char *name;
349
350 /* ptrs to lock prefixes */
Jan Beulich5967ed82010-04-21 16:08:14 +0100351 const s32 *locks;
352 const s32 *locks_end;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800353
354 /* .text segment, needed to avoid patching init code ;) */
355 u8 *text;
356 u8 *text_end;
357
358 struct list_head next;
359};
360static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200361static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100362static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800363
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100364void __init_or_module alternatives_smp_module_add(struct module *mod,
365 char *name,
366 void *locks, void *locks_end,
367 void *text, void *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800368{
369 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800370
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200371 if (noreplace_smp)
372 return;
373
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800374 if (smp_alt_once) {
375 if (boot_cpu_has(X86_FEATURE_UP))
376 alternatives_smp_unlock(locks, locks_end,
377 text, text_end);
378 return;
379 }
380
381 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
382 if (NULL == smp)
383 return; /* we'll run the (safe but slow) SMP code then ... */
384
385 smp->mod = mod;
386 smp->name = name;
387 smp->locks = locks;
388 smp->locks_end = locks_end;
389 smp->text = text;
390 smp->text_end = text_end;
391 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800392 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800393 smp->text, smp->text_end, smp->name);
394
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200395 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800396 list_add_tail(&smp->next, &smp_alt_modules);
397 if (boot_cpu_has(X86_FEATURE_UP))
398 alternatives_smp_unlock(smp->locks, smp->locks_end,
399 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200400 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800401}
402
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100403void __init_or_module alternatives_smp_module_del(struct module *mod)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800404{
405 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800406
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200407 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800408 return;
409
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200410 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800411 list_for_each_entry(item, &smp_alt_modules, next) {
412 if (mod != item->mod)
413 continue;
414 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200415 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800416 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800417 kfree(item);
418 return;
419 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200420 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800421}
422
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800423bool skip_smp_alternatives;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800424void alternatives_smp_switch(int smp)
425{
426 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800427
Ingo Molnar3047e992006-07-03 00:24:57 -0700428#ifdef CONFIG_LOCKDEP
429 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100430 * Older binutils section handling bug prevented
431 * alternatives-replacement from working reliably.
432 *
433 * If this still occurs then you should see a hang
434 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700435 */
Joe Perchesc767a542012-05-21 19:50:07 -0700436 pr_info("lockdep: fixing up alternatives\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700437#endif
438
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800439 if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800440 return;
441 BUG_ON(!smp && (num_online_cpus() > 1));
442
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200443 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100444
445 /*
446 * Avoid unnecessary switches because it forces JIT based VMs to
447 * throw away all cached translations, which can be quite costly.
448 */
449 if (smp == smp_mode) {
450 /* nothing */
451 } else if (smp) {
Joe Perchesc767a542012-05-21 19:50:07 -0700452 pr_info("switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100453 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
454 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800455 list_for_each_entry(mod, &smp_alt_modules, next)
456 alternatives_smp_lock(mod->locks, mod->locks_end,
457 mod->text, mod->text_end);
458 } else {
Joe Perchesc767a542012-05-21 19:50:07 -0700459 pr_info("switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100460 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
461 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800462 list_for_each_entry(mod, &smp_alt_modules, next)
463 alternatives_smp_unlock(mod->locks, mod->locks_end,
464 mod->text, mod->text_end);
465 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100466 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200467 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800468}
469
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500470/* Return 1 if the address range is reserved for smp-alternatives */
471int alternatives_text_reserved(void *start, void *end)
472{
473 struct smp_alt_module *mod;
Jan Beulich5967ed82010-04-21 16:08:14 +0100474 const s32 *poff;
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500475 u8 *text_start = start;
476 u8 *text_end = end;
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500477
478 list_for_each_entry(mod, &smp_alt_modules, next) {
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500479 if (mod->text > text_end || mod->text_end < text_start)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500480 continue;
Jan Beulich5967ed82010-04-21 16:08:14 +0100481 for (poff = mod->locks; poff < mod->locks_end; poff++) {
482 const u8 *ptr = (const u8 *)poff + *poff;
483
484 if (text_start <= ptr && text_end > ptr)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500485 return 1;
Jan Beulich5967ed82010-04-21 16:08:14 +0100486 }
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500487 }
488
489 return 0;
490}
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700491#endif
492
Rusty Russell139ec7c2006-12-07 02:14:08 +0100493#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100494void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
495 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100496{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200497 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200498 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100499
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200500 if (noreplace_paravirt)
501 return;
502
Rusty Russell139ec7c2006-12-07 02:14:08 +0100503 for (p = start; p < end; p++) {
504 unsigned int used;
505
Andi Kleenab144f52007-08-10 22:31:03 +0200506 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700507 /* prep the buffer with the original instructions */
508 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700509 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
510 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200511
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200512 BUG_ON(used > p->len);
513
Rusty Russell139ec7c2006-12-07 02:14:08 +0100514 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200515 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500516 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100517 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100518}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200519extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100520 __stop_parainstructions[];
521#endif /* CONFIG_PARAVIRT */
522
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800523void __init alternative_instructions(void)
524{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200525 /* The patching is not fully atomic, so try to avoid local interruptions
526 that might execute the to be patched code.
527 Other CPUs are not running. */
528 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100529
530 /*
531 * Don't stop machine check exceptions while patching.
532 * MCEs only happen when something got corrupted and in this
533 * case we must do something about the corruption.
534 * Ignoring it is worse than a unlikely patching race.
535 * Also machine checks tend to be broadcast and if one CPU
536 * goes into machine check the others follow quickly, so we don't
537 * expect a machine check to cause undue problems during to code
538 * patching.
539 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200540
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800541 apply_alternatives(__alt_instructions, __alt_instructions_end);
542
543 /* switch to patch-once-at-boottime-only mode and free the
544 * tables in case we know the number of CPUs will never ever
545 * change */
546#ifdef CONFIG_HOTPLUG_CPU
547 if (num_possible_cpus() < 2)
548 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800549#endif
550
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700551#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800552 if (smp_alt_once) {
553 if (1 == num_possible_cpus()) {
Joe Perchesc767a542012-05-21 19:50:07 -0700554 pr_info("switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100555 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
556 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
557
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800558 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
559 _text, _etext);
560 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800561 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800562 alternatives_smp_module_add(NULL, "core kernel",
563 __smp_locks, __smp_locks_end,
564 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100565
566 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200567 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100568 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800569 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700570#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200571 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200572
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200573 if (smp_alt_once)
574 free_init_pages("SMP alternatives",
575 (unsigned long)__smp_locks,
576 (unsigned long)__smp_locks_end);
577
Andi Kleen8f4e9562007-07-22 11:12:32 +0200578 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800579}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200580
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500581/**
582 * text_poke_early - Update instructions on a live kernel at boot time
583 * @addr: address to modify
584 * @opcode: source of the copy
585 * @len: length to copy
586 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200587 * When you use this code to patch more than one byte of an instruction
588 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500589 * Also no thread must be currently preempted in the middle of these
590 * instructions. And on the local CPU you need to be protected again NMI or MCE
591 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200592 */
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400593void *__init_or_module text_poke_early(void *addr, const void *opcode,
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100594 size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200595{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500596 unsigned long flags;
597 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200598 memcpy(addr, opcode, len);
599 sync_core();
Ben Hutchings5367b682009-09-10 02:53:50 +0100600 local_irq_restore(flags);
Andi Kleena534b672007-09-06 16:59:52 +0200601 /* Could also do a CLFLUSH here to speed up CPU recovery; but
602 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500603 return addr;
604}
605
606/**
607 * text_poke - Update instructions on a live kernel
608 * @addr: address to modify
609 * @opcode: source of the copy
610 * @len: length to copy
611 *
612 * Only atomic text poke/set should be allowed when not doing early patching.
613 * It means the size must be writable atomically and the address must be aligned
614 * in a way that permits an atomic write. It also makes sure we fit on a single
615 * page.
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500616 *
617 * Note: Must be called under text_mutex.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500618 */
619void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
620{
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500621 unsigned long flags;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500622 char *vaddr;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400623 struct page *pages[2];
624 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500625
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400626 if (!core_kernel_text((unsigned long)addr)) {
627 pages[0] = vmalloc_to_page(addr);
628 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400629 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400630 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200631 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400632 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500633 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400634 BUG_ON(!pages[0]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400635 local_irq_save(flags);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500636 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
637 if (pages[1])
638 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
639 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400640 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500641 clear_fixmap(FIX_TEXT_POKE0);
642 if (pages[1])
643 clear_fixmap(FIX_TEXT_POKE1);
644 local_flush_tlb();
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500645 sync_core();
646 /* Could also do a CLFLUSH here to speed up CPU recovery; but
647 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400648 for (i = 0; i < len; i++)
649 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400650 local_irq_restore(flags);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500651 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200652}
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500653
654/*
655 * Cross-modifying kernel text with stop_machine().
656 * This code originally comes from immediate value.
657 */
658static atomic_t stop_machine_first;
659static int wrote_text;
660
661struct text_poke_params {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900662 struct text_poke_param *params;
663 int nparams;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500664};
665
666static int __kprobes stop_machine_text_poke(void *data)
667{
668 struct text_poke_params *tpp = data;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900669 struct text_poke_param *p;
670 int i;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500671
672 if (atomic_dec_and_test(&stop_machine_first)) {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900673 for (i = 0; i < tpp->nparams; i++) {
674 p = &tpp->params[i];
675 text_poke(p->addr, p->opcode, p->len);
676 }
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500677 smp_wmb(); /* Make sure other cpus see that this has run */
678 wrote_text = 1;
679 } else {
680 while (!wrote_text)
Masami Hiramatsue5a11012010-03-03 22:38:50 -0500681 cpu_relax();
682 smp_mb(); /* Load wrote_text before following execution */
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500683 }
684
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900685 for (i = 0; i < tpp->nparams; i++) {
686 p = &tpp->params[i];
687 flush_icache_range((unsigned long)p->addr,
688 (unsigned long)p->addr + p->len);
689 }
Mathieu Desnoyers0e00f7a2011-03-03 11:01:37 -0500690 /*
691 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
692 * that a core serializing instruction such as "cpuid" should be
693 * executed on _each_ core before the new instruction is made visible.
694 */
695 sync_core();
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500696 return 0;
697}
698
699/**
700 * text_poke_smp - Update instructions on a live kernel on SMP
701 * @addr: address to modify
702 * @opcode: source of the copy
703 * @len: length to copy
704 *
705 * Modify multi-byte instruction by using stop_machine() on SMP. This allows
706 * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
707 * should be allowed, since stop_machine() does _not_ protect code against
708 * NMI and MCE.
709 *
710 * Note: Must be called under get_online_cpus() and text_mutex.
711 */
712void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
713{
714 struct text_poke_params tpp;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900715 struct text_poke_param p;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500716
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900717 p.addr = addr;
718 p.opcode = opcode;
719 p.len = len;
720 tpp.params = &p;
721 tpp.nparams = 1;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500722 atomic_set(&stop_machine_first, 1);
723 wrote_text = 0;
Masami Hiramatsu3caa37512010-10-14 12:10:36 +0900724 /* Use __stop_machine() because the caller already got online_cpus. */
Jason Baron404ba5d2010-10-28 11:20:27 -0400725 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500726 return addr;
727}
728
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900729/**
730 * text_poke_smp_batch - Update instructions on a live kernel on SMP
731 * @params: an array of text_poke parameters
732 * @n: the number of elements in params.
733 *
734 * Modify multi-byte instruction by using stop_machine() on SMP. Since the
735 * stop_machine() is heavy task, it is better to aggregate text_poke requests
736 * and do it once if possible.
737 *
738 * Note: Must be called under get_online_cpus() and text_mutex.
739 */
740void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
741{
742 struct text_poke_params tpp = {.params = params, .nparams = n};
743
744 atomic_set(&stop_machine_first, 1);
745 wrote_text = 0;
Rabin Vincent78345d22011-10-27 13:24:32 +0530746 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900747}