blob: 846f61eb89c147870e802a3df7ed98075a7645e8 [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>
Dave Jonesb0979762007-10-14 22:57:45 +020017#include <asm/vsyscall.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050018#include <asm/cacheflush.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050019#include <asm/tlbflush.h>
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -050020#include <asm/io.h>
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -050021#include <asm/fixmap.h>
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080022
Andi Kleenab144f52007-08-10 22:31:03 +020023#define MAX_PATCH_LEN (255-1)
24
Jan Beulich09488162007-07-21 17:10:25 +020025#ifdef CONFIG_HOTPLUG_CPU
26static int smp_alt_once;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080027
Gerd Hoffmannd167a512006-06-26 13:56:16 +020028static int __init bootonly(char *str)
29{
30 smp_alt_once = 1;
31 return 1;
32}
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020033__setup("smp-alt-boot", bootonly);
Jan Beulich09488162007-07-21 17:10:25 +020034#else
35#define smp_alt_once 1
36#endif
37
Jan Beulich8b5a10f2009-08-19 08:40:48 +010038static int __initdata_or_module debug_alternative;
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020039
Gerd Hoffmannd167a512006-06-26 13:56:16 +020040static int __init debug_alt(char *str)
41{
42 debug_alternative = 1;
43 return 1;
44}
Gerd Hoffmannd167a512006-06-26 13:56:16 +020045__setup("debug-alternative", debug_alt);
46
Jan Beulich09488162007-07-21 17:10:25 +020047static int noreplace_smp;
48
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020049static int __init setup_noreplace_smp(char *str)
50{
51 noreplace_smp = 1;
52 return 1;
53}
54__setup("noreplace-smp", setup_noreplace_smp);
55
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020056#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +010057static int __initdata_or_module noreplace_paravirt = 0;
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +020058
59static int __init setup_noreplace_paravirt(char *str)
60{
61 noreplace_paravirt = 1;
62 return 1;
63}
64__setup("noreplace-paravirt", setup_noreplace_paravirt);
65#endif
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +020066
Gerd Hoffmannd167a512006-06-26 13:56:16 +020067#define DPRINTK(fmt, args...) if (debug_alternative) \
68 printk(KERN_DEBUG fmt, args)
69
H. Peter Anvindc326fc2011-04-18 15:19:51 -070070/*
71 * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes
72 * that correspond to that nop. Getting from one nop to the next, we
73 * add to the array the offset that is equal to the sum of all sizes of
74 * nops preceding the one we are after.
75 *
76 * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the
77 * nice symmetry of sizes of the previous nops.
78 */
Jan Beulich8b5a10f2009-08-19 08:40:48 +010079#if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -070080static const unsigned char intelnops[] =
81{
82 GENERIC_NOP1,
83 GENERIC_NOP2,
84 GENERIC_NOP3,
85 GENERIC_NOP4,
86 GENERIC_NOP5,
87 GENERIC_NOP6,
88 GENERIC_NOP7,
89 GENERIC_NOP8,
90 GENERIC_NOP5_ATOMIC
91};
92static const unsigned char * const intel_nops[ASM_NOP_MAX+2] =
93{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080094 NULL,
95 intelnops,
96 intelnops + 1,
97 intelnops + 1 + 2,
98 intelnops + 1 + 2 + 3,
99 intelnops + 1 + 2 + 3 + 4,
100 intelnops + 1 + 2 + 3 + 4 + 5,
101 intelnops + 1 + 2 + 3 + 4 + 5 + 6,
102 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700103 intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800104};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200105#endif
106
107#ifdef K8_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700108static const unsigned char k8nops[] =
109{
110 K8_NOP1,
111 K8_NOP2,
112 K8_NOP3,
113 K8_NOP4,
114 K8_NOP5,
115 K8_NOP6,
116 K8_NOP7,
117 K8_NOP8,
118 K8_NOP5_ATOMIC
119};
120static const unsigned char * const k8_nops[ASM_NOP_MAX+2] =
121{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800122 NULL,
123 k8nops,
124 k8nops + 1,
125 k8nops + 1 + 2,
126 k8nops + 1 + 2 + 3,
127 k8nops + 1 + 2 + 3 + 4,
128 k8nops + 1 + 2 + 3 + 4 + 5,
129 k8nops + 1 + 2 + 3 + 4 + 5 + 6,
130 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700131 k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800132};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200133#endif
134
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100135#if defined(K7_NOP1) && !defined(CONFIG_X86_64)
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700136static const unsigned char k7nops[] =
137{
138 K7_NOP1,
139 K7_NOP2,
140 K7_NOP3,
141 K7_NOP4,
142 K7_NOP5,
143 K7_NOP6,
144 K7_NOP7,
145 K7_NOP8,
146 K7_NOP5_ATOMIC
147};
148static const unsigned char * const k7_nops[ASM_NOP_MAX+2] =
149{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800150 NULL,
151 k7nops,
152 k7nops + 1,
153 k7nops + 1 + 2,
154 k7nops + 1 + 2 + 3,
155 k7nops + 1 + 2 + 3 + 4,
156 k7nops + 1 + 2 + 3 + 4 + 5,
157 k7nops + 1 + 2 + 3 + 4 + 5 + 6,
158 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700159 k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800160};
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200161#endif
162
Jan Beulich32c464f2007-10-17 18:04:41 +0200163#ifdef P6_NOP1
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700164static const unsigned char __initconst_or_module p6nops[] =
165{
166 P6_NOP1,
167 P6_NOP2,
168 P6_NOP3,
169 P6_NOP4,
170 P6_NOP5,
171 P6_NOP6,
172 P6_NOP7,
173 P6_NOP8,
174 P6_NOP5_ATOMIC
175};
176static const unsigned char * const p6_nops[ASM_NOP_MAX+2] =
177{
Jan Beulich32c464f2007-10-17 18:04:41 +0200178 NULL,
179 p6nops,
180 p6nops + 1,
181 p6nops + 1 + 2,
182 p6nops + 1 + 2 + 3,
183 p6nops + 1 + 2 + 3 + 4,
184 p6nops + 1 + 2 + 3 + 4 + 5,
185 p6nops + 1 + 2 + 3 + 4 + 5 + 6,
186 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700187 p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
Jan Beulich32c464f2007-10-17 18:04:41 +0200188};
189#endif
190
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700191/* Initialize these to a safe default */
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200192#ifdef CONFIG_X86_64
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700193const unsigned char * const *ideal_nops = p6_nops;
194#else
195const unsigned char * const *ideal_nops = intel_nops;
196#endif
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200197
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700198void __init arch_init_ideal_nops(void)
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200199{
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700200 switch (boot_cpu_data.x86_vendor) {
201 case X86_VENDOR_INTEL:
202 if (boot_cpu_has(X86_FEATURE_NOPL)) {
203 ideal_nops = p6_nops;
204 } else {
205#ifdef CONFIG_X86_64
206 ideal_nops = k8_nops;
207#else
208 ideal_nops = intel_nops;
209#endif
210 }
211
212 default:
213#ifdef CONFIG_X86_64
214 ideal_nops = k8_nops;
215#else
216 if (boot_cpu_has(X86_FEATURE_K8))
217 ideal_nops = k8_nops;
218 else if (boot_cpu_has(X86_FEATURE_K7))
219 ideal_nops = k7_nops;
220 else
221 ideal_nops = intel_nops;
222#endif
223 }
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200224}
225
Andi Kleenab144f52007-08-10 22:31:03 +0200226/* Use this to add nops to a buffer, then text_poke the whole buffer. */
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100227static void __init_or_module add_nops(void *insns, unsigned int len)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100228{
Rusty Russell139ec7c2006-12-07 02:14:08 +0100229 while (len > 0) {
230 unsigned int noplen = len;
231 if (noplen > ASM_NOP_MAX)
232 noplen = ASM_NOP_MAX;
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700233 memcpy(insns, ideal_nops[noplen], noplen);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100234 insns += noplen;
235 len -= noplen;
236 }
237}
238
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200239extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
Jan Beulich5967ed82010-04-21 16:08:14 +0100240extern s32 __smp_locks[], __smp_locks_end[];
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700241extern char __vsyscall_0;
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400242void *text_poke_early(void *addr, const void *opcode, size_t len);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200243
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800244/* Replace instructions with better alternatives for this CPU type.
245 This runs before SMP is initialized to avoid SMP problems with
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300246 self modifying code. This implies that asymmetric systems where
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800247 APs have less capabilities than the boot processor are not handled.
248 Tough. Make sure you disable such features by hand. */
249
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100250void __init_or_module apply_alternatives(struct alt_instr *start,
251 struct alt_instr *end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800252{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800253 struct alt_instr *a;
Jan Beulich1b1d9252009-12-18 16:12:56 +0000254 u8 insnbuf[MAX_PATCH_LEN];
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800255
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800256 DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800257 for (a = start; a < end; a++) {
Andi Kleenab144f52007-08-10 22:31:03 +0200258 u8 *instr = a->instr;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800259 BUG_ON(a->replacementlen > a->instrlen);
Andi Kleenab144f52007-08-10 22:31:03 +0200260 BUG_ON(a->instrlen > sizeof(insnbuf));
H. Peter Anvin3b770a22010-07-13 14:57:50 -0700261 BUG_ON(a->cpuid >= NCAPINTS*32);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800262 if (!boot_cpu_has(a->cpuid))
263 continue;
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200264#ifdef CONFIG_X86_64
265 /* vsyscall code is not mapped yet. resolve it manually. */
266 if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
267 instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
268 DPRINTK("%s: vsyscall fixup: %p => %p\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800269 __func__, a->instr, instr);
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200270 }
271#endif
Andi Kleenab144f52007-08-10 22:31:03 +0200272 memcpy(insnbuf, a->replacement, a->replacementlen);
Jan Beulich1b1d9252009-12-18 16:12:56 +0000273 if (*insnbuf == 0xe8 && a->replacementlen == 5)
274 *(s32 *)(insnbuf + 1) += a->replacement - a->instr;
Andi Kleenab144f52007-08-10 22:31:03 +0200275 add_nops(insnbuf + a->replacementlen,
276 a->instrlen - a->replacementlen);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500277 text_poke_early(instr, insnbuf, a->instrlen);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800278 }
279}
280
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700281#ifdef CONFIG_SMP
282
Jan Beulich5967ed82010-04-21 16:08:14 +0100283static void alternatives_smp_lock(const s32 *start, const s32 *end,
284 u8 *text, u8 *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800285{
Jan Beulich5967ed82010-04-21 16:08:14 +0100286 const s32 *poff;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800287
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500288 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100289 for (poff = start; poff < end; poff++) {
290 u8 *ptr = (u8 *)poff + *poff;
291
292 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800293 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400294 /* turn DS segment override prefix into lock prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700295 if (*ptr == 0x3e)
296 text_poke(ptr, ((unsigned char []){0xf0}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800297 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500298 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800299}
300
Jan Beulich5967ed82010-04-21 16:08:14 +0100301static void alternatives_smp_unlock(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
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200306 if (noreplace_smp)
307 return;
308
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500309 mutex_lock(&text_mutex);
Jan Beulich5967ed82010-04-21 16:08:14 +0100310 for (poff = start; poff < end; poff++) {
311 u8 *ptr = (u8 *)poff + *poff;
312
313 if (!*poff || ptr < text || ptr >= text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800314 continue;
Mathieu Desnoyersf88f07e2008-08-14 16:58:15 -0400315 /* turn lock prefix into DS segment override prefix */
H. Peter Anvind9c58412010-04-29 16:53:17 -0700316 if (*ptr == 0xf0)
317 text_poke(ptr, ((unsigned char []){0x3E}), 1);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800318 };
Masami Hiramatsu3945dab2009-03-06 10:37:22 -0500319 mutex_unlock(&text_mutex);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800320}
321
322struct smp_alt_module {
323 /* what is this ??? */
324 struct module *mod;
325 char *name;
326
327 /* ptrs to lock prefixes */
Jan Beulich5967ed82010-04-21 16:08:14 +0100328 const s32 *locks;
329 const s32 *locks_end;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800330
331 /* .text segment, needed to avoid patching init code ;) */
332 u8 *text;
333 u8 *text_end;
334
335 struct list_head next;
336};
337static LIST_HEAD(smp_alt_modules);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200338static DEFINE_MUTEX(smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100339static int smp_mode = 1; /* protected by smp_alt */
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800340
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100341void __init_or_module alternatives_smp_module_add(struct module *mod,
342 char *name,
343 void *locks, void *locks_end,
344 void *text, void *text_end)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800345{
346 struct smp_alt_module *smp;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800347
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200348 if (noreplace_smp)
349 return;
350
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800351 if (smp_alt_once) {
352 if (boot_cpu_has(X86_FEATURE_UP))
353 alternatives_smp_unlock(locks, locks_end,
354 text, text_end);
355 return;
356 }
357
358 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
359 if (NULL == smp)
360 return; /* we'll run the (safe but slow) SMP code then ... */
361
362 smp->mod = mod;
363 smp->name = name;
364 smp->locks = locks;
365 smp->locks_end = locks_end;
366 smp->text = text;
367 smp->text_end = text_end;
368 DPRINTK("%s: locks %p -> %p, text %p -> %p, name %s\n",
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800369 __func__, smp->locks, smp->locks_end,
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800370 smp->text, smp->text_end, smp->name);
371
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200372 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800373 list_add_tail(&smp->next, &smp_alt_modules);
374 if (boot_cpu_has(X86_FEATURE_UP))
375 alternatives_smp_unlock(smp->locks, smp->locks_end,
376 smp->text, smp->text_end);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200377 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800378}
379
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100380void __init_or_module alternatives_smp_module_del(struct module *mod)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800381{
382 struct smp_alt_module *item;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800383
Jeremy Fitzhardingeb7fb4af2007-05-02 19:27:13 +0200384 if (smp_alt_once || noreplace_smp)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800385 return;
386
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200387 mutex_lock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800388 list_for_each_entry(item, &smp_alt_modules, next) {
389 if (mod != item->mod)
390 continue;
391 list_del(&item->next);
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200392 mutex_unlock(&smp_alt);
Harvey Harrison77bf90e2008-03-03 11:37:23 -0800393 DPRINTK("%s: %s\n", __func__, item->name);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800394 kfree(item);
395 return;
396 }
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200397 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800398}
399
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800400bool skip_smp_alternatives;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800401void alternatives_smp_switch(int smp)
402{
403 struct smp_alt_module *mod;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800404
Ingo Molnar3047e992006-07-03 00:24:57 -0700405#ifdef CONFIG_LOCKDEP
406 /*
Ingo Molnar17abecf2008-01-30 13:33:24 +0100407 * Older binutils section handling bug prevented
408 * alternatives-replacement from working reliably.
409 *
410 * If this still occurs then you should see a hang
411 * or crash shortly after this line:
Ingo Molnar3047e992006-07-03 00:24:57 -0700412 */
Ingo Molnar17abecf2008-01-30 13:33:24 +0100413 printk("lockdep: fixing up alternatives.\n");
Ingo Molnar3047e992006-07-03 00:24:57 -0700414#endif
415
Suresh Siddha3fb82d52010-11-23 16:11:40 -0800416 if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800417 return;
418 BUG_ON(!smp && (num_online_cpus() > 1));
419
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200420 mutex_lock(&smp_alt);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100421
422 /*
423 * Avoid unnecessary switches because it forces JIT based VMs to
424 * throw away all cached translations, which can be quite costly.
425 */
426 if (smp == smp_mode) {
427 /* nothing */
428 } else if (smp) {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800429 printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100430 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
431 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800432 list_for_each_entry(mod, &smp_alt_modules, next)
433 alternatives_smp_lock(mod->locks, mod->locks_end,
434 mod->text, mod->text_end);
435 } else {
436 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100437 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
438 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800439 list_for_each_entry(mod, &smp_alt_modules, next)
440 alternatives_smp_unlock(mod->locks, mod->locks_end,
441 mod->text, mod->text_end);
442 }
Andi Kleenca74a6f2008-01-30 13:33:17 +0100443 smp_mode = smp;
Pekka Paalanen2f1dafe2008-05-12 21:21:01 +0200444 mutex_unlock(&smp_alt);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800445}
446
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500447/* Return 1 if the address range is reserved for smp-alternatives */
448int alternatives_text_reserved(void *start, void *end)
449{
450 struct smp_alt_module *mod;
Jan Beulich5967ed82010-04-21 16:08:14 +0100451 const s32 *poff;
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500452 u8 *text_start = start;
453 u8 *text_end = end;
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500454
455 list_for_each_entry(mod, &smp_alt_modules, next) {
Masami Hiramatsu076dc4a62010-02-05 12:16:47 -0500456 if (mod->text > text_end || mod->text_end < text_start)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500457 continue;
Jan Beulich5967ed82010-04-21 16:08:14 +0100458 for (poff = mod->locks; poff < mod->locks_end; poff++) {
459 const u8 *ptr = (const u8 *)poff + *poff;
460
461 if (text_start <= ptr && text_end > ptr)
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500462 return 1;
Jan Beulich5967ed82010-04-21 16:08:14 +0100463 }
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500464 }
465
466 return 0;
467}
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700468#endif
469
Rusty Russell139ec7c2006-12-07 02:14:08 +0100470#ifdef CONFIG_PARAVIRT
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100471void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
472 struct paravirt_patch_site *end)
Rusty Russell139ec7c2006-12-07 02:14:08 +0100473{
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200474 struct paravirt_patch_site *p;
Andi Kleenab144f52007-08-10 22:31:03 +0200475 char insnbuf[MAX_PATCH_LEN];
Rusty Russell139ec7c2006-12-07 02:14:08 +0100476
Jeremy Fitzhardinge959b4fd2007-05-02 19:27:16 +0200477 if (noreplace_paravirt)
478 return;
479
Rusty Russell139ec7c2006-12-07 02:14:08 +0100480 for (p = start; p < end; p++) {
481 unsigned int used;
482
Andi Kleenab144f52007-08-10 22:31:03 +0200483 BUG_ON(p->len > MAX_PATCH_LEN);
Chris Wrightd34fda42007-08-18 14:31:41 -0700484 /* prep the buffer with the original instructions */
485 memcpy(insnbuf, p->instr, p->len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700486 used = pv_init_ops.patch(p->instrtype, p->clobbers, insnbuf,
487 (unsigned long)p->instr, p->len);
Jeremy Fitzhardinge7f63c412007-05-02 19:27:13 +0200488
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200489 BUG_ON(used > p->len);
490
Rusty Russell139ec7c2006-12-07 02:14:08 +0100491 /* Pad the rest with nops */
Andi Kleenab144f52007-08-10 22:31:03 +0200492 add_nops(insnbuf + used, p->len - used);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500493 text_poke_early(p->instr, insnbuf, p->len);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100494 }
Rusty Russell139ec7c2006-12-07 02:14:08 +0100495}
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +0200496extern struct paravirt_patch_site __start_parainstructions[],
Rusty Russell139ec7c2006-12-07 02:14:08 +0100497 __stop_parainstructions[];
498#endif /* CONFIG_PARAVIRT */
499
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800500void __init alternative_instructions(void)
501{
Andi Kleen8f4e9562007-07-22 11:12:32 +0200502 /* The patching is not fully atomic, so try to avoid local interruptions
503 that might execute the to be patched code.
504 Other CPUs are not running. */
505 stop_nmi();
Andi Kleen123aa762009-02-12 13:39:27 +0100506
507 /*
508 * Don't stop machine check exceptions while patching.
509 * MCEs only happen when something got corrupted and in this
510 * case we must do something about the corruption.
511 * Ignoring it is worse than a unlikely patching race.
512 * Also machine checks tend to be broadcast and if one CPU
513 * goes into machine check the others follow quickly, so we don't
514 * expect a machine check to cause undue problems during to code
515 * patching.
516 */
Andi Kleen8f4e9562007-07-22 11:12:32 +0200517
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800518 apply_alternatives(__alt_instructions, __alt_instructions_end);
519
520 /* switch to patch-once-at-boottime-only mode and free the
521 * tables in case we know the number of CPUs will never ever
522 * change */
523#ifdef CONFIG_HOTPLUG_CPU
524 if (num_possible_cpus() < 2)
525 smp_alt_once = 1;
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800526#endif
527
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700528#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800529 if (smp_alt_once) {
530 if (1 == num_possible_cpus()) {
531 printk(KERN_INFO "SMP alternatives: switching to UP code\n");
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100532 set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
533 set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
534
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800535 alternatives_smp_unlock(__smp_locks, __smp_locks_end,
536 _text, _etext);
537 }
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800538 } else {
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800539 alternatives_smp_module_add(NULL, "core kernel",
540 __smp_locks, __smp_locks_end,
541 _text, _etext);
Andi Kleenca74a6f2008-01-30 13:33:17 +0100542
543 /* Only switch to UP mode if we don't immediately boot others */
Thomas Gleixner649c6652008-10-05 16:52:24 +0200544 if (num_present_cpus() == 1 || setup_max_cpus <= 1)
Andi Kleenca74a6f2008-01-30 13:33:17 +0100545 alternatives_smp_switch(0);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800546 }
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -0700547#endif
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200548 apply_paravirt(__parainstructions, __parainstructions_end);
Andi Kleen8f4e9562007-07-22 11:12:32 +0200549
Fengguang Wuf68fd5f2007-10-17 18:04:34 +0200550 if (smp_alt_once)
551 free_init_pages("SMP alternatives",
552 (unsigned long)__smp_locks,
553 (unsigned long)__smp_locks_end);
554
Andi Kleen8f4e9562007-07-22 11:12:32 +0200555 restart_nmi();
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800556}
Andi Kleen19d36cc2007-07-22 11:12:31 +0200557
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500558/**
559 * text_poke_early - Update instructions on a live kernel at boot time
560 * @addr: address to modify
561 * @opcode: source of the copy
562 * @len: length to copy
563 *
Andi Kleen19d36cc2007-07-22 11:12:31 +0200564 * When you use this code to patch more than one byte of an instruction
565 * you need to make sure that other CPUs cannot execute this code in parallel.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500566 * Also no thread must be currently preempted in the middle of these
567 * instructions. And on the local CPU you need to be protected again NMI or MCE
568 * handlers seeing an inconsistent instruction while you patch.
Andi Kleen19d36cc2007-07-22 11:12:31 +0200569 */
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400570void *__init_or_module text_poke_early(void *addr, const void *opcode,
Jan Beulich8b5a10f2009-08-19 08:40:48 +0100571 size_t len)
Andi Kleen19d36cc2007-07-22 11:12:31 +0200572{
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500573 unsigned long flags;
574 local_irq_save(flags);
Andi Kleen19d36cc2007-07-22 11:12:31 +0200575 memcpy(addr, opcode, len);
576 sync_core();
Ben Hutchings5367b682009-09-10 02:53:50 +0100577 local_irq_restore(flags);
Andi Kleena534b672007-09-06 16:59:52 +0200578 /* Could also do a CLFLUSH here to speed up CPU recovery; but
579 that causes hangs on some VIA CPUs. */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500580 return addr;
581}
582
583/**
584 * text_poke - Update instructions on a live kernel
585 * @addr: address to modify
586 * @opcode: source of the copy
587 * @len: length to copy
588 *
589 * Only atomic text poke/set should be allowed when not doing early patching.
590 * It means the size must be writable atomically and the address must be aligned
591 * in a way that permits an atomic write. It also makes sure we fit on a single
592 * page.
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500593 *
594 * Note: Must be called under text_mutex.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500595 */
596void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
597{
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500598 unsigned long flags;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500599 char *vaddr;
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400600 struct page *pages[2];
601 int i;
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500602
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400603 if (!core_kernel_text((unsigned long)addr)) {
604 pages[0] = vmalloc_to_page(addr);
605 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
Mathieu Desnoyers15a601e2008-03-12 11:54:16 -0400606 } else {
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400607 pages[0] = virt_to_page(addr);
Ingo Molnar00c6b2d2008-04-25 17:07:03 +0200608 WARN_ON(!PageReserved(pages[0]));
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400609 pages[1] = virt_to_page(addr + PAGE_SIZE);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500610 }
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400611 BUG_ON(!pages[0]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400612 local_irq_save(flags);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500613 set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
614 if (pages[1])
615 set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
616 vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400617 memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
Masami Hiramatsu78ff7fa2009-03-06 10:37:54 -0500618 clear_fixmap(FIX_TEXT_POKE0);
619 if (pages[1])
620 clear_fixmap(FIX_TEXT_POKE1);
621 local_flush_tlb();
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500622 sync_core();
623 /* Could also do a CLFLUSH here to speed up CPU recovery; but
624 that causes hangs on some VIA CPUs. */
Mathieu Desnoyersb7b66ba2008-04-24 11:03:33 -0400625 for (i = 0; i < len; i++)
626 BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
Masami Hiramatsu7cf49422009-03-09 12:40:40 -0400627 local_irq_restore(flags);
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500628 return addr;
Andi Kleen19d36cc2007-07-22 11:12:31 +0200629}
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500630
631/*
632 * Cross-modifying kernel text with stop_machine().
633 * This code originally comes from immediate value.
634 */
635static atomic_t stop_machine_first;
636static int wrote_text;
637
638struct text_poke_params {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900639 struct text_poke_param *params;
640 int nparams;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500641};
642
643static int __kprobes stop_machine_text_poke(void *data)
644{
645 struct text_poke_params *tpp = data;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900646 struct text_poke_param *p;
647 int i;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500648
649 if (atomic_dec_and_test(&stop_machine_first)) {
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900650 for (i = 0; i < tpp->nparams; i++) {
651 p = &tpp->params[i];
652 text_poke(p->addr, p->opcode, p->len);
653 }
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500654 smp_wmb(); /* Make sure other cpus see that this has run */
655 wrote_text = 1;
656 } else {
657 while (!wrote_text)
Masami Hiramatsue5a11012010-03-03 22:38:50 -0500658 cpu_relax();
659 smp_mb(); /* Load wrote_text before following execution */
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500660 }
661
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900662 for (i = 0; i < tpp->nparams; i++) {
663 p = &tpp->params[i];
664 flush_icache_range((unsigned long)p->addr,
665 (unsigned long)p->addr + p->len);
666 }
Mathieu Desnoyers0e00f7a2011-03-03 11:01:37 -0500667 /*
668 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
669 * that a core serializing instruction such as "cpuid" should be
670 * executed on _each_ core before the new instruction is made visible.
671 */
672 sync_core();
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500673 return 0;
674}
675
676/**
677 * text_poke_smp - Update instructions on a live kernel on SMP
678 * @addr: address to modify
679 * @opcode: source of the copy
680 * @len: length to copy
681 *
682 * Modify multi-byte instruction by using stop_machine() on SMP. This allows
683 * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
684 * should be allowed, since stop_machine() does _not_ protect code against
685 * NMI and MCE.
686 *
687 * Note: Must be called under get_online_cpus() and text_mutex.
688 */
689void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
690{
691 struct text_poke_params tpp;
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900692 struct text_poke_param p;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500693
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900694 p.addr = addr;
695 p.opcode = opcode;
696 p.len = len;
697 tpp.params = &p;
698 tpp.nparams = 1;
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500699 atomic_set(&stop_machine_first, 1);
700 wrote_text = 0;
Masami Hiramatsu3caa37512010-10-14 12:10:36 +0900701 /* Use __stop_machine() because the caller already got online_cpus. */
Jason Baron404ba5d2010-10-28 11:20:27 -0400702 __stop_machine(stop_machine_text_poke, (void *)&tpp, cpu_online_mask);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500703 return addr;
704}
705
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900706/**
707 * text_poke_smp_batch - Update instructions on a live kernel on SMP
708 * @params: an array of text_poke parameters
709 * @n: the number of elements in params.
710 *
711 * Modify multi-byte instruction by using stop_machine() on SMP. Since the
712 * stop_machine() is heavy task, it is better to aggregate text_poke requests
713 * and do it once if possible.
714 *
715 * Note: Must be called under get_online_cpus() and text_mutex.
716 */
717void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
718{
719 struct text_poke_params tpp = {.params = params, .nparams = n};
720
721 atomic_set(&stop_machine_first, 1);
722 wrote_text = 0;
Peter Zijlstrad91309f2011-02-11 22:07:46 +0100723 __stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900724}