blob: c15cf9a25e279fc9934549a2c4a7d6faf26766fd [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 Beulich8b5a10f2009-08-19 08:40:48 +010026static int __initdata_or_module debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020027
Gerd Hoffmannd167a512006-06-26 13:56:16 +020028static int __init debug_alt(char *str)
29{
30 debug_alternative = 1;
31 return 1;
32}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020033__setup("debug-alternative", debug_alt);
34
Jan Beulich09488162007-07-21 17:10:25 +020035static int noreplace_smp;
36
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020037static int __init setup_noreplace_smp(char *str)
38{
39 noreplace_smp = 1;
40 return 1;
41}
42__setup("noreplace-smp", setup_noreplace_smp);
43
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020044#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +010045static int __initdata_or_module noreplace_paravirt = 0;
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020046
47static int __init setup_noreplace_paravirt(char *str)
48{
49 noreplace_paravirt = 1;
50 return 1;
51}
52__setup("noreplace-paravirt", setup_noreplace_paravirt);
53#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020054
Joe Perchesc767a542012-05-21 19:50:07 -070055#define DPRINTK(fmt, ...) \
56do { \
57 if (debug_alternative) \
58 printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
59} while (0)
Gerd Hoffmannd167a512006-06-26 13:56:16 +020060
H. Peter Anvindc326fc2011-04-18 15:19:51 -070061/*
62 * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
63 * that correspond to that nop. Getting from one nop to the next, we
64 * add to the array the offset that is equal to the sum of all sizes of
65 * nops preceding the one we are after.
66 *
67 * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
68 * nice symmetry of sizes of the previous nops.
69 */
Jan Beulich8b5a10f2009-08-19 08:40:48 +010070#if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -070071static const unsigned char intelnops[] =
72{
73 GENERIC_NOP1,
74 GENERIC_NOP2,
75 GENERIC_NOP3,
76 GENERIC_NOP4,
77 GENERIC_NOP5,
78 GENERIC_NOP6,
79 GENERIC_NOP7,
80 GENERIC_NOP8,
81 GENERIC_NOP5_ATOMIC
82};
83static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
84{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080085 NULL,
86 intelnops,
87 intelnops + 1,
88 intelnops + 1 + 2,
89 intelnops + 1 + 2 + 3,
90 intelnops + 1 + 2 + 3 + 4,
91 intelnops + 1 + 2 + 3 + 4 + 5,
92 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
93 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -070094 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080095};
Gerd Hoffmannd167a512006-06-26 13:56:16 +020096#endif
97
98#ifdef K8_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -070099static const unsigned char k8nops[] =
100{
101 K8_NOP1,
102 K8_NOP2,
103 K8_NOP3,
104 K8_NOP4,
105 K8_NOP5,
106 K8_NOP6,
107 K8_NOP7,
108 K8_NOP8,
109 K8_NOP5_ATOMIC
110};
111static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
112{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800113 NULL,
114 k8nops,
115 k8nops + 1,
116 k8nops + 1 + 2,
117 k8nops + 1 + 2 + 3,
118 k8nops + 1 + 2 + 3 + 4,
119 k8nops + 1 + 2 + 3 + 4 + 5,
120 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
121 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700122 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800123};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200124#endif
125
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100126#if defined(K7_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700127static const unsigned char k7nops[] =
128{
129 K7_NOP1,
130 K7_NOP2,
131 K7_NOP3,
132 K7_NOP4,
133 K7_NOP5,
134 K7_NOP6,
135 K7_NOP7,
136 K7_NOP8,
137 K7_NOP5_ATOMIC
138};
139static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
140{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800141 NULL,
142 k7nops,
143 k7nops + 1,
144 k7nops + 1 + 2,
145 k7nops + 1 + 2 + 3,
146 k7nops + 1 + 2 + 3 + 4,
147 k7nops + 1 + 2 + 3 + 4 + 5,
148 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
149 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700150 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800151};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200152#endif
153
Jan Beulich32c464f2007-10-17 18:04:41 +0200154#ifdef P6_NOP1
Avi Kivitycb09cad2012-08-22 13:03:48 +0300155static const unsigned char p6nops[] =
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700156{
157 P6_NOP1,
158 P6_NOP2,
159 P6_NOP3,
160 P6_NOP4,
161 P6_NOP5,
162 P6_NOP6,
163 P6_NOP7,
164 P6_NOP8,
165 P6_NOP5_ATOMIC
166};
167static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
168{
Jan Beulich32c464f2007-10-17 18:04:41 +0200169 NULL,
170 p6nops,
171 p6nops + 1,
172 p6nops + 1 + 2,
173 p6nops + 1 + 2 + 3,
174 p6nops + 1 + 2 + 3 + 4,
175 p6nops + 1 + 2 + 3 + 4 + 5,
176 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
177 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700178 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Jan Beulich32c464f2007-10-17 18:04:41 +0200179};
180#endif
181
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700182/* Initialize these to a safe default */
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200183#ifdef CONFIG_X86_64
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700184const unsigned char * const *ideal_nops = p6_nops;
185#else
186const unsigned char * const *ideal_nops = intel_nops;
187#endif
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200188
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700189void __init arch_init_ideal_nops(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200190{
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700191 switch (boot_cpu_data.x86_vendor) {
192 case X86_VENDOR_INTEL:
H. Peter Anvind8d97662011-04-18 15:31:57 -0700193 /*
194 * Due to a decoder implementation quirk, some
195 * specific Intel CPUs actually perform better with
196 * the "k8_nops" than with the SDM-recommended NOPs.
197 */
198 if (boot_cpu_data.x86 == 6 &&
199 boot_cpu_data.x86_model >= 0x0f &&
200 boot_cpu_data.x86_model != 0x1c &&
201 boot_cpu_data.x86_model != 0x26 &&
202 boot_cpu_data.x86_model != 0x27 &&
203 boot_cpu_data.x86_model < 0x30) {
204 ideal_nops = k8_nops;
205 } else if (boot_cpu_has(X86_FEATURE_NOPL)) {
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700206 ideal_nops = p6_nops;
207 } else {
208#ifdef CONFIG_X86_64
209 ideal_nops = k8_nops;
210#else
211 ideal_nops = intel_nops;
212#endif
213 }
Alan Coxd6250a32012-07-25 16:28:19 +0100214 break;
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700215 default:
216#ifdef CONFIG_X86_64
217 ideal_nops = k8_nops;
218#else
219 if (boot_cpu_has(X86_FEATURE_K8))
220 ideal_nops = k8_nops;
221 else if (boot_cpu_has(X86_FEATURE_K7))
222 ideal_nops = k7_nops;
223 else
224 ideal_nops = intel_nops;
225#endif
226 }
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200227}
228
Andi Kleenab144f52007-08-10 22:31:03 +0200229/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100230static void __init_or_module add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100231{
Rusty Russell139ec7c2006-12-07 02:14:08 +0100232 while (len > 0) {
233 unsigned int noplen = len;
234 if (noplen > ASM_NOP_MAX)
235 noplen = ASM_NOP_MAX;
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700236 memcpy(insns, ideal_nops[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100237 insns += noplen;
238 len -= noplen;
239 }
240}
241
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200242extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Jan Beulich5967ed82010-04-21 16:08:14 +0100243extern s32 __smp_locks[], __smp_locks_end[];
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400244void *text_poke_early(void *addr, const void *opcode, size_t len);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200245
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800246/* Replace instructions with better alternatives for this CPU type.
247 This runs before SMP is initialized to avoid SMP problems with
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300248 self modifying code. This implies that asymmetric systems where
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800249 APs have less capabilities than the boot processor are not handled.
250 Tough. Make sure you disable such features by hand. */
251
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100252void __init_or_module apply_alternatives(struct alt_instr *start,
253 struct alt_instr *end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800254{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800255 struct alt_instr *a;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400256 u8 *instr, *replacement;
Jan Beulich1b1d9252009-12-18 16:12:56 +0000257 u8 insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800258
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800259 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Fenghua Yu50973132011-05-17 15:29:12 -0700260 /*
261 * The scan order should be from start to end. A later scanned
262 * alternative code can overwrite a previous scanned alternative code.
263 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
264 * patch code.
265 *
266 * So be careful if you want to change the scan order to any other
267 * order.
268 */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800269 for (a = start; a < end; a++) {
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400270 instr = (u8 *)&a->instr_offset + a->instr_offset;
271 replacement = (u8 *)&a->repl_offset + a->repl_offset;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800272 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200273 BUG_ON(a->instrlen > sizeof(insnbuf));
Borislav Petkov65fc9852013-03-20 15:07:23 +0100274 BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800275 if (!boot_cpu_has(a->cpuid))
276 continue;
Andy Lutomirski59e97e42011-07-13 09:24:10 -0400277
278 memcpy(insnbuf, replacement, a->replacementlen);
279
280 /* 0xe8 is a relative jump; fix the offset. */
281 if (*insnbuf == 0xe8 && a->replacementlen == 5)
282 *(s32 *)(insnbuf + 1) += replacement - instr;
283
284 add_nops(insnbuf + a->replacementlen,
285 a->instrlen - a->replacementlen);
286
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500287 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800288 }
289}
290
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700291#ifdef CONFIG_SMP
292
Jan Beulich5967ed82010-04-21 16:08:14 +0100293static void alternatives_smp_lock(const s32 *start, const s32 *end,
294 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800295{
Jan Beulich5967ed82010-04-21 16:08:14 +0100296 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800297
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500298 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100299 for (poff = start; poff < end; poff++) {
300 u8 *ptr = (u8 *)poff + *poff;
301
302 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800303 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400304 /* turn DS segment override prefix into lock prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700305 if (*ptr == 0x3e)
306 text_poke(ptr, ((unsigned char []){0xf0}), 1);
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +0200307 }
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500308 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800309}
310
Jan Beulich5967ed82010-04-21 16:08:14 +0100311static void alternatives_smp_unlock(const s32 *start, const s32 *end,
312 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800313{
Jan Beulich5967ed82010-04-21 16:08:14 +0100314 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800315
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500316 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100317 for (poff = start; poff < end; poff++) {
318 u8 *ptr = (u8 *)poff + *poff;
319
320 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800321 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400322 /* turn lock prefix into DS segment override prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700323 if (*ptr == 0xf0)
324 text_poke(ptr, ((unsigned char []){0x3E}), 1);
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +0200325 }
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500326 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800327}
328
329struct smp_alt_module {
330 /* what is this ??? */
331 struct module *mod;
332 char *name;
333
334 /* ptrs to lock prefixes */
Jan Beulich5967ed82010-04-21 16:08:14 +0100335 const s32 *locks;
336 const s32 *locks_end;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800337
338 /* .text segment, needed to avoid patching init code ;) */
339 u8 *text;
340 u8 *text_end;
341
342 struct list_head next;
343};
344static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200345static DEFINE_MUTEX(smp_alt);
Rusty Russell816afe42012-08-06 17:29:49 +0930346static bool uniproc_patched = false; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800347
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100348void __init_or_module alternatives_smp_module_add(struct module *mod,
349 char *name,
350 void *locks, void *locks_end,
351 void *text, void *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800352{
353 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800354
Rusty Russell816afe42012-08-06 17:29:49 +0930355 mutex_lock(&smp_alt);
356 if (!uniproc_patched)
357 goto unlock;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200358
Rusty Russell816afe42012-08-06 17:29:49 +0930359 if (num_possible_cpus() == 1)
360 /* Don't bother remembering, we'll never have to undo it. */
361 goto smp_unlock;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800362
363 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
364 if (NULL == smp)
Rusty Russell816afe42012-08-06 17:29:49 +0930365 /* we'll run the (safe but slow) SMP code then ... */
366 goto unlock;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800367
368 smp->mod = mod;
369 smp->name = name;
370 smp->locks = locks;
371 smp->locks_end = locks_end;
372 smp->text = text;
373 smp->text_end = text_end;
374 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800375 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800376 smp->text, smp->text_end, smp->name);
377
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800378 list_add_tail(&smp->next, &smp_alt_modules);
Rusty Russell816afe42012-08-06 17:29:49 +0930379smp_unlock:
380 alternatives_smp_unlock(locks, locks_end, text, text_end);
381unlock:
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200382 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800383}
384
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100385void __init_or_module alternatives_smp_module_del(struct module *mod)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800386{
387 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800388
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200389 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800390 list_for_each_entry(item, &smp_alt_modules, next) {
391 if (mod != item->mod)
392 continue;
393 list_del(&item->next);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800394 kfree(item);
Rusty Russell816afe42012-08-06 17:29:49 +0930395 break;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800396 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200397 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800398}
399
Rusty Russell816afe42012-08-06 17:29:49 +0930400void alternatives_enable_smp(void)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800401{
402 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800403
Ingo Molnar3047e992006-07-03 00:24:57 -0700404#ifdef CONFIG_LOCKDEP
405 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100406 * Older binutils section handling bug prevented
407 * alternatives-replacement from working reliably.
408 *
409 * If this still occurs then you should see a hang
410 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700411 */
Joe Perchesc767a542012-05-21 19:50:07 -0700412 pr_info("lockdep: fixing up alternatives\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700413#endif
414
Rusty Russell816afe42012-08-06 17:29:49 +0930415 /* Why bother if there are no other CPUs? */
416 BUG_ON(num_possible_cpus() == 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800417
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200418 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100419
Rusty Russell816afe42012-08-06 17:29:49 +0930420 if (uniproc_patched) {
Joe Perchesc767a542012-05-21 19:50:07 -0700421 pr_info("switching to SMP code\n");
Rusty Russell816afe42012-08-06 17:29:49 +0930422 BUG_ON(num_online_cpus() != 1);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100423 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
424 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800425 list_for_each_entry(mod, &smp_alt_modules, next)
426 alternatives_smp_lock(mod->locks, mod->locks_end,
427 mod->text, mod->text_end);
Rusty Russell816afe42012-08-06 17:29:49 +0930428 uniproc_patched = false;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800429 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200430 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800431}
432
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500433/* Return 1 if the address range is reserved for smp-alternatives */
434int alternatives_text_reserved(void *start, void *end)
435{
436 struct smp_alt_module *mod;
Jan Beulich5967ed82010-04-21 16:08:14 +0100437 const s32 *poff;
Masami Hiramatsu076dc4a2010-02-05 12:16:47 -0500438 u8 *text_start = start;
439 u8 *text_end = end;
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500440
441 list_for_each_entry(mod, &smp_alt_modules, next) {
Masami Hiramatsu076dc4a2010-02-05 12:16:47 -0500442 if (mod->text > text_end || mod->text_end < text_start)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500443 continue;
Jan Beulich5967ed82010-04-21 16:08:14 +0100444 for (poff = mod->locks; poff < mod->locks_end; poff++) {
445 const u8 *ptr = (const u8 *)poff + *poff;
446
447 if (text_start <= ptr && text_end > ptr)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500448 return 1;
Jan Beulich5967ed82010-04-21 16:08:14 +0100449 }
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500450 }
451
452 return 0;
453}
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700454#endif
455
Rusty Russell139ec7c2006-12-07 02:14:08 +0100456#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100457void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
458 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100459{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200460 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200461 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100462
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200463 if (noreplace_paravirt)
464 return;
465
Rusty Russell139ec7c2006-12-07 02:14:08 +0100466 for (p = start; p < end; p++) {
467 unsigned int used;
468
Andi Kleenab144f52007-08-10 22:31:03 +0200469 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700470 /* prep the buffer with the original instructions */
471 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700472 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
473 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200474
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200475 BUG_ON(used > p->len);
476
Rusty Russell139ec7c2006-12-07 02:14:08 +0100477 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200478 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500479 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100480 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100481}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200482extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100483 __stop_parainstructions[];
484#endif /* CONFIG_PARAVIRT */
485
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800486void __init alternative_instructions(void)
487{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200488 /* The patching is not fully atomic, so try to avoid local interruptions
489 that might execute the to be patched code.
490 Other CPUs are not running. */
491 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100492
493 /*
494 * Don't stop machine check exceptions while patching.
495 * MCEs only happen when something got corrupted and in this
496 * case we must do something about the corruption.
497 * Ignoring it is worse than a unlikely patching race.
498 * Also machine checks tend to be broadcast and if one CPU
499 * goes into machine check the others follow quickly, so we don't
500 * expect a machine check to cause undue problems during to code
501 * patching.
502 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200503
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800504 apply_alternatives(__alt_instructions, __alt_instructions_end);
505
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700506#ifdef CONFIG_SMP
Rusty Russell816afe42012-08-06 17:29:49 +0930507 /* Patch to UP if other cpus not imminent. */
508 if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
509 uniproc_patched = true;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800510 alternatives_smp_module_add(NULL, "core kernel",
511 __smp_locks, __smp_locks_end,
512 _text, _etext);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800513 }
Andi Kleen8f4e9562007-07-22 11:12:32 +0200514
Rusty Russell816afe42012-08-06 17:29:49 +0930515 if (!uniproc_patched || num_possible_cpus() == 1)
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200516 free_init_pages("SMP alternatives",
517 (unsigned long)__smp_locks,
518 (unsigned long)__smp_locks_end);
Rusty Russell816afe42012-08-06 17:29:49 +0930519#endif
520
521 apply_paravirt(__parainstructions, __parainstructions_end);
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200522
Andi Kleen8f4e9562007-07-22 11:12:32 +0200523 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800524}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200525
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500526/**
527 * text_poke_early - Update instructions on a live kernel at boot time
528 * @addr: address to modify
529 * @opcode: source of the copy
530 * @len: length to copy
531 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200532 * When you use this code to patch more than one byte of an instruction
533 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500534 * Also no thread must be currently preempted in the middle of these
535 * instructions. And on the local CPU you need to be protected again NMI or MCE
536 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200537 */
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400538void *__init_or_module text_poke_early(void *addr, const void *opcode,
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100539 size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200540{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500541 unsigned long flags;
542 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200543 memcpy(addr, opcode, len);
544 sync_core();
Ben Hutchings5367b682009-09-10 02:53:50 +0100545 local_irq_restore(flags);
Andi Kleena534b672007-09-06 16:59:52 +0200546 /* Could also do a CLFLUSH here to speed up CPU recovery; but
547 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500548 return addr;
549}
550
551/**
552 * text_poke - Update instructions on a live kernel
553 * @addr: address to modify
554 * @opcode: source of the copy
555 * @len: length to copy
556 *
557 * Only atomic text poke/set should be allowed when not doing early patching.
558 * It means the size must be writable atomically and the address must be aligned
559 * in a way that permits an atomic write. It also makes sure we fit on a single
560 * page.
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500561 *
562 * Note: Must be called under text_mutex.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500563 */
564void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
565{
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500566 unsigned long flags;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500567 char *vaddr;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400568 struct page *pages[2];
569 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500570
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400571 if (!core_kernel_text((unsigned long)addr)) {
572 pages[0] = vmalloc_to_page(addr);
573 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400574 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400575 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200576 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400577 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500578 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400579 BUG_ON(!pages[0]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400580 local_irq_save(flags);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500581 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
582 if (pages[1])
583 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
584 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400585 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500586 clear_fixmap(FIX_TEXT_POKE0);
587 if (pages[1])
588 clear_fixmap(FIX_TEXT_POKE1);
589 local_flush_tlb();
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500590 sync_core();
591 /* Could also do a CLFLUSH here to speed up CPU recovery; but
592 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400593 for (i = 0; i < len; i++)
594 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400595 local_irq_restore(flags);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500596 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200597}
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500598
599/*
600 * Cross-modifying kernel text with stop_machine().
601 * This code originally comes from immediate value.
602 */
603static atomic_t stop_machine_first;
604static int wrote_text;
605
606struct text_poke_params {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900607 struct text_poke_param *params;
608 int nparams;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500609};
610
611static int __kprobes stop_machine_text_poke(void *data)
612{
613 struct text_poke_params *tpp = data;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900614 struct text_poke_param *p;
615 int i;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500616
OGAWA Hirofumi2f747592012-06-07 22:20:18 +0900617 if (atomic_xchg(&stop_machine_first, 0)) {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900618 for (i = 0; i < tpp->nparams; i++) {
619 p = &tpp->params[i];
620 text_poke(p->addr, p->opcode, p->len);
621 }
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500622 smp_wmb(); /* Make sure other cpus see that this has run */
623 wrote_text = 1;
624 } else {
625 while (!wrote_text)
Masami Hiramatsue5a11012010-03-03 22:38:50 -0500626 cpu_relax();
627 smp_mb(); /* Load wrote_text before following execution */
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500628 }
629
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900630 for (i = 0; i < tpp->nparams; i++) {
631 p = &tpp->params[i];
632 flush_icache_range((unsigned long)p->addr,
633 (unsigned long)p->addr + p->len);
634 }
Mathieu Desnoyers0e00f7a2011-03-03 11:01:37 -0500635 /*
636 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
637 * that a core serializing instruction such as "cpuid" should be
638 * executed on _each_ core before the new instruction is made visible.
639 */
640 sync_core();
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500641 return 0;
642}
643
644/**
645 * text_poke_smp - Update instructions on a live kernel on SMP
646 * @addr: address to modify
647 * @opcode: source of the copy
648 * @len: length to copy
649 *
650 * Modify multi-byte instruction by using stop_machine() on SMP. This allows
651 * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
652 * should be allowed, since stop_machine() does _not_ protect code against
653 * NMI and MCE.
654 *
655 * Note: Must be called under get_online_cpus() and text_mutex.
656 */
657void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
658{
659 struct text_poke_params tpp;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900660 struct text_poke_param p;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500661
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900662 p.addr = addr;
663 p.opcode = opcode;
664 p.len = len;
665 tpp.params = &p;
666 tpp.nparams = 1;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500667 atomic_set(&stop_machine_first, 1);
668 wrote_text = 0;
Masami Hiramatsu3caa37512010-10-14 12:10:36 +0900669 /* Use __stop_machine() because the caller already got online_cpus. */
Jason Baron404ba5d2010-10-28 11:20:27 -0400670 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500671 return addr;
672}
673
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900674/**
675 * text_poke_smp_batch - Update instructions on a live kernel on SMP
676 * @params: an array of text_poke parameters
677 * @n: the number of elements in params.
678 *
679 * Modify multi-byte instruction by using stop_machine() on SMP. Since the
680 * stop_machine() is heavy task, it is better to aggregate text_poke requests
681 * and do it once if possible.
682 *
683 * Note: Must be called under get_online_cpus() and text_mutex.
684 */
685void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
686{
687 struct text_poke_params tpp = {.params = params, .nparams = n};
688
689 atomic_set(&stop_machine_first, 1);
690 wrote_text = 0;
Rabin Vincent78345d22011-10-27 13:24:32 +0530691 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900692}