Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 2 | #define pr_fmt(fmt) "SMP alternatives: " fmt |
| 3 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 4 | #include <linux/module.h> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 5 | #include <linux/sched.h> |
Pekka Paalanen | 2f1dafe | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6 | #include <linux/mutex.h> |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 7 | #include <linux/list.h> |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 8 | #include <linux/stringify.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 9 | #include <linux/mm.h> |
| 10 | #include <linux/vmalloc.h> |
Masami Hiramatsu | 3945dab | 2009-03-06 10:37:22 -0500 | [diff] [blame] | 11 | #include <linux/memory.h> |
Masami Hiramatsu | 3d55cc8 | 2010-02-25 08:34:38 -0500 | [diff] [blame] | 12 | #include <linux/stop_machine.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 14 | #include <linux/kdebug.h> |
Masami Hiramatsu | c13324a | 2019-02-13 01:12:15 +0900 | [diff] [blame] | 15 | #include <linux/kprobes.h> |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 16 | #include <linux/mmu_context.h> |
Andy Lutomirski | 35de5b0 | 2016-04-26 12:23:24 -0700 | [diff] [blame] | 17 | #include <asm/text-patching.h> |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 18 | #include <asm/alternative.h> |
| 19 | #include <asm/sections.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 20 | #include <asm/pgtable.h> |
Andi Kleen | 8f4e956 | 2007-07-22 11:12:32 +0200 | [diff] [blame] | 21 | #include <asm/mce.h> |
| 22 | #include <asm/nmi.h> |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 23 | #include <asm/cacheflush.h> |
Masami Hiramatsu | 78ff7fa | 2009-03-06 10:37:54 -0500 | [diff] [blame] | 24 | #include <asm/tlbflush.h> |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 25 | #include <asm/io.h> |
Masami Hiramatsu | 78ff7fa | 2009-03-06 10:37:54 -0500 | [diff] [blame] | 26 | #include <asm/fixmap.h> |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 27 | |
Ingo Molnar | 5e907bb | 2015-04-30 09:09:26 +0200 | [diff] [blame] | 28 | int __read_mostly alternatives_patched; |
| 29 | |
| 30 | EXPORT_SYMBOL_GPL(alternatives_patched); |
| 31 | |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 32 | #define MAX_PATCH_LEN (255-1) |
| 33 | |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 34 | static int __initdata_or_module debug_alternative; |
Jeremy Fitzhardinge | b7fb4af | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 35 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 36 | static int __init debug_alt(char *str) |
| 37 | { |
| 38 | debug_alternative = 1; |
| 39 | return 1; |
| 40 | } |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 41 | __setup("debug-alternative", debug_alt); |
| 42 | |
Jan Beulich | 0948816 | 2007-07-21 17:10:25 +0200 | [diff] [blame] | 43 | static int noreplace_smp; |
| 44 | |
Jeremy Fitzhardinge | b7fb4af | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 45 | static int __init setup_noreplace_smp(char *str) |
| 46 | { |
| 47 | noreplace_smp = 1; |
| 48 | return 1; |
| 49 | } |
| 50 | __setup("noreplace-smp", setup_noreplace_smp); |
| 51 | |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 52 | #define DPRINTK(fmt, args...) \ |
| 53 | do { \ |
| 54 | if (debug_alternative) \ |
| 55 | printk(KERN_DEBUG "%s: " fmt "\n", __func__, ##args); \ |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 56 | } while (0) |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 57 | |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 58 | #define DUMP_BYTES(buf, len, fmt, args...) \ |
| 59 | do { \ |
| 60 | if (unlikely(debug_alternative)) { \ |
| 61 | int j; \ |
| 62 | \ |
| 63 | if (!(len)) \ |
| 64 | break; \ |
| 65 | \ |
| 66 | printk(KERN_DEBUG fmt, ##args); \ |
| 67 | for (j = 0; j < (len) - 1; j++) \ |
| 68 | printk(KERN_CONT "%02hhx ", buf[j]); \ |
| 69 | printk(KERN_CONT "%02hhx\n", buf[j]); \ |
| 70 | } \ |
| 71 | } while (0) |
| 72 | |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Each GENERIC_NOPX is of X bytes, and defined as an array of bytes |
| 75 | * that correspond to that nop. Getting from one nop to the next, we |
| 76 | * add to the array the offset that is equal to the sum of all sizes of |
| 77 | * nops preceding the one we are after. |
| 78 | * |
| 79 | * Note: The GENERIC_NOP5_ATOMIC is at the end, as it breaks the |
| 80 | * nice symmetry of sizes of the previous nops. |
| 81 | */ |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 82 | #if defined(GENERIC_NOP1) && !defined(CONFIG_X86_64) |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 83 | static const unsigned char intelnops[] = |
| 84 | { |
| 85 | GENERIC_NOP1, |
| 86 | GENERIC_NOP2, |
| 87 | GENERIC_NOP3, |
| 88 | GENERIC_NOP4, |
| 89 | GENERIC_NOP5, |
| 90 | GENERIC_NOP6, |
| 91 | GENERIC_NOP7, |
| 92 | GENERIC_NOP8, |
| 93 | GENERIC_NOP5_ATOMIC |
| 94 | }; |
| 95 | static const unsigned char * const intel_nops[ASM_NOP_MAX+2] = |
| 96 | { |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 97 | NULL, |
| 98 | intelnops, |
| 99 | intelnops + 1, |
| 100 | intelnops + 1 + 2, |
| 101 | intelnops + 1 + 2 + 3, |
| 102 | intelnops + 1 + 2 + 3 + 4, |
| 103 | intelnops + 1 + 2 + 3 + 4 + 5, |
| 104 | intelnops + 1 + 2 + 3 + 4 + 5 + 6, |
| 105 | intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7, |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 106 | intelnops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 107 | }; |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | #ifdef K8_NOP1 |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 111 | static const unsigned char k8nops[] = |
| 112 | { |
| 113 | K8_NOP1, |
| 114 | K8_NOP2, |
| 115 | K8_NOP3, |
| 116 | K8_NOP4, |
| 117 | K8_NOP5, |
| 118 | K8_NOP6, |
| 119 | K8_NOP7, |
| 120 | K8_NOP8, |
| 121 | K8_NOP5_ATOMIC |
| 122 | }; |
| 123 | static const unsigned char * const k8_nops[ASM_NOP_MAX+2] = |
| 124 | { |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 125 | NULL, |
| 126 | k8nops, |
| 127 | k8nops + 1, |
| 128 | k8nops + 1 + 2, |
| 129 | k8nops + 1 + 2 + 3, |
| 130 | k8nops + 1 + 2 + 3 + 4, |
| 131 | k8nops + 1 + 2 + 3 + 4 + 5, |
| 132 | k8nops + 1 + 2 + 3 + 4 + 5 + 6, |
| 133 | k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7, |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 134 | k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 135 | }; |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 136 | #endif |
| 137 | |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 138 | #if defined(K7_NOP1) && !defined(CONFIG_X86_64) |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 139 | static const unsigned char k7nops[] = |
| 140 | { |
| 141 | K7_NOP1, |
| 142 | K7_NOP2, |
| 143 | K7_NOP3, |
| 144 | K7_NOP4, |
| 145 | K7_NOP5, |
| 146 | K7_NOP6, |
| 147 | K7_NOP7, |
| 148 | K7_NOP8, |
| 149 | K7_NOP5_ATOMIC |
| 150 | }; |
| 151 | static const unsigned char * const k7_nops[ASM_NOP_MAX+2] = |
| 152 | { |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 153 | NULL, |
| 154 | k7nops, |
| 155 | k7nops + 1, |
| 156 | k7nops + 1 + 2, |
| 157 | k7nops + 1 + 2 + 3, |
| 158 | k7nops + 1 + 2 + 3 + 4, |
| 159 | k7nops + 1 + 2 + 3 + 4 + 5, |
| 160 | k7nops + 1 + 2 + 3 + 4 + 5 + 6, |
| 161 | k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7, |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 162 | k7nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 163 | }; |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 164 | #endif |
| 165 | |
Jan Beulich | 32c464f | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 166 | #ifdef P6_NOP1 |
Avi Kivity | cb09cad | 2012-08-22 13:03:48 +0300 | [diff] [blame] | 167 | static const unsigned char p6nops[] = |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 168 | { |
| 169 | P6_NOP1, |
| 170 | P6_NOP2, |
| 171 | P6_NOP3, |
| 172 | P6_NOP4, |
| 173 | P6_NOP5, |
| 174 | P6_NOP6, |
| 175 | P6_NOP7, |
| 176 | P6_NOP8, |
| 177 | P6_NOP5_ATOMIC |
| 178 | }; |
| 179 | static const unsigned char * const p6_nops[ASM_NOP_MAX+2] = |
| 180 | { |
Jan Beulich | 32c464f | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 181 | NULL, |
| 182 | p6nops, |
| 183 | p6nops + 1, |
| 184 | p6nops + 1 + 2, |
| 185 | p6nops + 1 + 2 + 3, |
| 186 | p6nops + 1 + 2 + 3 + 4, |
| 187 | p6nops + 1 + 2 + 3 + 4 + 5, |
| 188 | p6nops + 1 + 2 + 3 + 4 + 5 + 6, |
| 189 | p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7, |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 190 | p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, |
Jan Beulich | 32c464f | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 191 | }; |
| 192 | #endif |
| 193 | |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 194 | /* Initialize these to a safe default */ |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_X86_64 |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 196 | const unsigned char * const *ideal_nops = p6_nops; |
| 197 | #else |
| 198 | const unsigned char * const *ideal_nops = intel_nops; |
| 199 | #endif |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 200 | |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 201 | void __init arch_init_ideal_nops(void) |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 202 | { |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 203 | switch (boot_cpu_data.x86_vendor) { |
| 204 | case X86_VENDOR_INTEL: |
H. Peter Anvin | d8d9766 | 2011-04-18 15:31:57 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Due to a decoder implementation quirk, some |
| 207 | * specific Intel CPUs actually perform better with |
| 208 | * the "k8_nops" than with the SDM-recommended NOPs. |
| 209 | */ |
| 210 | if (boot_cpu_data.x86 == 6 && |
| 211 | boot_cpu_data.x86_model >= 0x0f && |
| 212 | boot_cpu_data.x86_model != 0x1c && |
| 213 | boot_cpu_data.x86_model != 0x26 && |
| 214 | boot_cpu_data.x86_model != 0x27 && |
| 215 | boot_cpu_data.x86_model < 0x30) { |
| 216 | ideal_nops = k8_nops; |
| 217 | } else if (boot_cpu_has(X86_FEATURE_NOPL)) { |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 218 | ideal_nops = p6_nops; |
| 219 | } else { |
| 220 | #ifdef CONFIG_X86_64 |
| 221 | ideal_nops = k8_nops; |
| 222 | #else |
| 223 | ideal_nops = intel_nops; |
| 224 | #endif |
| 225 | } |
Alan Cox | d6250a3 | 2012-07-25 16:28:19 +0100 | [diff] [blame] | 226 | break; |
Borislav Petkov | f21262b | 2015-05-11 10:15:46 +0200 | [diff] [blame] | 227 | |
Pu Wen | c3fecca | 2018-09-23 17:35:01 +0800 | [diff] [blame] | 228 | case X86_VENDOR_HYGON: |
| 229 | ideal_nops = p6_nops; |
| 230 | return; |
| 231 | |
Borislav Petkov | f21262b | 2015-05-11 10:15:46 +0200 | [diff] [blame] | 232 | case X86_VENDOR_AMD: |
| 233 | if (boot_cpu_data.x86 > 0xf) { |
| 234 | ideal_nops = p6_nops; |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* fall through */ |
| 239 | |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 240 | default: |
| 241 | #ifdef CONFIG_X86_64 |
| 242 | ideal_nops = k8_nops; |
| 243 | #else |
| 244 | if (boot_cpu_has(X86_FEATURE_K8)) |
| 245 | ideal_nops = k8_nops; |
| 246 | else if (boot_cpu_has(X86_FEATURE_K7)) |
| 247 | ideal_nops = k7_nops; |
| 248 | else |
| 249 | ideal_nops = intel_nops; |
| 250 | #endif |
| 251 | } |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 254 | /* Use this to add nops to a buffer, then text_poke the whole buffer. */ |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 255 | static void __init_or_module add_nops(void *insns, unsigned int len) |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 256 | { |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 257 | while (len > 0) { |
| 258 | unsigned int noplen = len; |
| 259 | if (noplen > ASM_NOP_MAX) |
| 260 | noplen = ASM_NOP_MAX; |
H. Peter Anvin | dc326fc | 2011-04-18 15:19:51 -0700 | [diff] [blame] | 261 | memcpy(insns, ideal_nops[noplen], noplen); |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 262 | insns += noplen; |
| 263 | len -= noplen; |
| 264 | } |
| 265 | } |
| 266 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 267 | extern struct alt_instr __alt_instructions[], __alt_instructions_end[]; |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 268 | extern s32 __smp_locks[], __smp_locks_end[]; |
Nadav Amit | 0a203df | 2019-04-25 17:11:33 -0700 | [diff] [blame] | 269 | void text_poke_early(void *addr, const void *opcode, size_t len); |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 270 | |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 271 | /* |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 272 | * Are we looking at a near JMP with a 1 or 4-byte displacement. |
| 273 | */ |
| 274 | static inline bool is_jmp(const u8 opcode) |
| 275 | { |
| 276 | return opcode == 0xeb || opcode == 0xe9; |
| 277 | } |
| 278 | |
| 279 | static void __init_or_module |
| 280 | recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insnbuf) |
| 281 | { |
| 282 | u8 *next_rip, *tgt_rip; |
| 283 | s32 n_dspl, o_dspl; |
| 284 | int repl_len; |
| 285 | |
| 286 | if (a->replacementlen != 5) |
| 287 | return; |
| 288 | |
| 289 | o_dspl = *(s32 *)(insnbuf + 1); |
| 290 | |
| 291 | /* next_rip of the replacement JMP */ |
| 292 | next_rip = repl_insn + a->replacementlen; |
| 293 | /* target rip of the replacement JMP */ |
| 294 | tgt_rip = next_rip + o_dspl; |
| 295 | n_dspl = tgt_rip - orig_insn; |
| 296 | |
Borislav Petkov | 0e6c16c | 2018-01-26 13:11:36 +0100 | [diff] [blame] | 297 | DPRINTK("target RIP: %px, new_displ: 0x%x", tgt_rip, n_dspl); |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 298 | |
| 299 | if (tgt_rip - orig_insn >= 0) { |
| 300 | if (n_dspl - 2 <= 127) |
| 301 | goto two_byte_jmp; |
| 302 | else |
| 303 | goto five_byte_jmp; |
| 304 | /* negative offset */ |
| 305 | } else { |
| 306 | if (((n_dspl - 2) & 0xff) == (n_dspl - 2)) |
| 307 | goto two_byte_jmp; |
| 308 | else |
| 309 | goto five_byte_jmp; |
| 310 | } |
| 311 | |
| 312 | two_byte_jmp: |
| 313 | n_dspl -= 2; |
| 314 | |
| 315 | insnbuf[0] = 0xeb; |
| 316 | insnbuf[1] = (s8)n_dspl; |
| 317 | add_nops(insnbuf + 2, 3); |
| 318 | |
| 319 | repl_len = 2; |
| 320 | goto done; |
| 321 | |
| 322 | five_byte_jmp: |
| 323 | n_dspl -= 5; |
| 324 | |
| 325 | insnbuf[0] = 0xe9; |
| 326 | *(s32 *)&insnbuf[1] = n_dspl; |
| 327 | |
| 328 | repl_len = 5; |
| 329 | |
| 330 | done: |
| 331 | |
| 332 | DPRINTK("final displ: 0x%08x, JMP 0x%lx", |
| 333 | n_dspl, (unsigned long)orig_insn + n_dspl + repl_len); |
| 334 | } |
| 335 | |
Borislav Petkov | 34bfab0 | 2016-12-03 16:02:58 +0100 | [diff] [blame] | 336 | /* |
| 337 | * "noinline" to cause control flow change and thus invalidate I$ and |
| 338 | * cause refetch after modification. |
| 339 | */ |
| 340 | static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr) |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 341 | { |
Thomas Gleixner | 66c117d | 2015-09-03 12:34:55 +0200 | [diff] [blame] | 342 | unsigned long flags; |
Borislav Petkov | 612e8e9 | 2018-01-10 12:28:16 +0100 | [diff] [blame] | 343 | int i; |
Thomas Gleixner | 66c117d | 2015-09-03 12:34:55 +0200 | [diff] [blame] | 344 | |
Borislav Petkov | 612e8e9 | 2018-01-10 12:28:16 +0100 | [diff] [blame] | 345 | for (i = 0; i < a->padlen; i++) { |
| 346 | if (instr[i] != 0x90) |
| 347 | return; |
| 348 | } |
Borislav Petkov | 69df353 | 2015-04-04 23:07:42 +0200 | [diff] [blame] | 349 | |
Thomas Gleixner | 66c117d | 2015-09-03 12:34:55 +0200 | [diff] [blame] | 350 | local_irq_save(flags); |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 351 | add_nops(instr + (a->instrlen - a->padlen), a->padlen); |
Thomas Gleixner | 66c117d | 2015-09-03 12:34:55 +0200 | [diff] [blame] | 352 | local_irq_restore(flags); |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 353 | |
Borislav Petkov | 0e6c16c | 2018-01-26 13:11:36 +0100 | [diff] [blame] | 354 | DUMP_BYTES(instr, a->instrlen, "%px: [%d:%d) optimized NOPs: ", |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 355 | instr, a->instrlen - a->padlen, a->padlen); |
| 356 | } |
| 357 | |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 358 | /* |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 359 | * Replace instructions with better alternatives for this CPU type. This runs |
| 360 | * before SMP is initialized to avoid SMP problems with self modifying code. |
| 361 | * This implies that asymmetric systems where APs have less capabilities than |
| 362 | * the boot processor are not handled. Tough. Make sure you disable such |
| 363 | * features by hand. |
Borislav Petkov | 34bfab0 | 2016-12-03 16:02:58 +0100 | [diff] [blame] | 364 | * |
| 365 | * Marked "noinline" to cause control flow change and thus insn cache |
| 366 | * to refetch changed I$ lines. |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 367 | */ |
Borislav Petkov | 34bfab0 | 2016-12-03 16:02:58 +0100 | [diff] [blame] | 368 | void __init_or_module noinline apply_alternatives(struct alt_instr *start, |
| 369 | struct alt_instr *end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 370 | { |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 371 | struct alt_instr *a; |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 372 | u8 *instr, *replacement; |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 373 | u8 insnbuf[MAX_PATCH_LEN]; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 374 | |
Borislav Petkov | 0e6c16c | 2018-01-26 13:11:36 +0100 | [diff] [blame] | 375 | DPRINTK("alt table %px, -> %px", start, end); |
Fenghua Yu | 5097313 | 2011-05-17 15:29:12 -0700 | [diff] [blame] | 376 | /* |
| 377 | * The scan order should be from start to end. A later scanned |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 378 | * alternative code can overwrite previously scanned alternative code. |
Fenghua Yu | 5097313 | 2011-05-17 15:29:12 -0700 | [diff] [blame] | 379 | * Some kernel functions (e.g. memcpy, memset, etc) use this order to |
| 380 | * patch code. |
| 381 | * |
| 382 | * So be careful if you want to change the scan order to any other |
| 383 | * order. |
| 384 | */ |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 385 | for (a = start; a < end; a++) { |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 386 | int insnbuf_sz = 0; |
| 387 | |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 388 | instr = (u8 *)&a->instr_offset + a->instr_offset; |
| 389 | replacement = (u8 *)&a->repl_offset + a->repl_offset; |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 390 | BUG_ON(a->instrlen > sizeof(insnbuf)); |
Borislav Petkov | 65fc985 | 2013-03-20 15:07:23 +0100 | [diff] [blame] | 391 | BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 392 | if (!boot_cpu_has(a->cpuid)) { |
| 393 | if (a->padlen > 1) |
| 394 | optimize_nops(a, instr); |
| 395 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 396 | continue; |
Borislav Petkov | 4fd4b6e | 2015-01-10 20:34:07 +0100 | [diff] [blame] | 397 | } |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 398 | |
Borislav Petkov | c1d4e41 | 2018-12-10 12:30:30 +0100 | [diff] [blame] | 399 | DPRINTK("feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d), pad: %d", |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 400 | a->cpuid >> 5, |
| 401 | a->cpuid & 0x1f, |
Borislav Petkov | c1d4e41 | 2018-12-10 12:30:30 +0100 | [diff] [blame] | 402 | instr, instr, a->instrlen, |
Borislav Petkov | dbe4058 | 2015-04-04 15:34:43 +0200 | [diff] [blame] | 403 | replacement, a->replacementlen, a->padlen); |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 404 | |
Borislav Petkov | 0e6c16c | 2018-01-26 13:11:36 +0100 | [diff] [blame] | 405 | DUMP_BYTES(instr, a->instrlen, "%px: old_insn: ", instr); |
| 406 | DUMP_BYTES(replacement, a->replacementlen, "%px: rpl_insn: ", replacement); |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 407 | |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 408 | memcpy(insnbuf, replacement, a->replacementlen); |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 409 | insnbuf_sz = a->replacementlen; |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 410 | |
Mateusz Jurczyk | fc152d22 | 2017-05-24 15:55:00 +0200 | [diff] [blame] | 411 | /* |
| 412 | * 0xe8 is a relative jump; fix the offset. |
| 413 | * |
| 414 | * Instruction length is checked before the opcode to avoid |
| 415 | * accessing uninitialized bytes for zero-length replacements. |
| 416 | */ |
| 417 | if (a->replacementlen == 5 && *insnbuf == 0xe8) { |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 418 | *(s32 *)(insnbuf + 1) += replacement - instr; |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 419 | DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx", |
| 420 | *(s32 *)(insnbuf + 1), |
| 421 | (unsigned long)instr + *(s32 *)(insnbuf + 1) + 5); |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 422 | } |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 423 | |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 424 | if (a->replacementlen && is_jmp(replacement[0])) |
| 425 | recompute_jump(a, instr, replacement, insnbuf); |
| 426 | |
| 427 | if (a->instrlen > a->replacementlen) { |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 428 | add_nops(insnbuf + a->replacementlen, |
| 429 | a->instrlen - a->replacementlen); |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 430 | insnbuf_sz += a->instrlen - a->replacementlen; |
| 431 | } |
Borislav Petkov | 0e6c16c | 2018-01-26 13:11:36 +0100 | [diff] [blame] | 432 | DUMP_BYTES(insnbuf, insnbuf_sz, "%px: final_insn: ", instr); |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 433 | |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 434 | text_poke_early(instr, insnbuf, insnbuf_sz); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Gerd Hoffmann | 8ec4d41 | 2006-07-01 04:36:18 -0700 | [diff] [blame] | 438 | #ifdef CONFIG_SMP |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 439 | static void alternatives_smp_lock(const s32 *start, const s32 *end, |
| 440 | u8 *text, u8 *text_end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 441 | { |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 442 | const s32 *poff; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 443 | |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 444 | for (poff = start; poff < end; poff++) { |
| 445 | u8 *ptr = (u8 *)poff + *poff; |
| 446 | |
| 447 | if (!*poff || ptr < text || ptr >= text_end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 448 | continue; |
Mathieu Desnoyers | f88f07e | 2008-08-14 16:58:15 -0400 | [diff] [blame] | 449 | /* turn DS segment override prefix into lock prefix */ |
H. Peter Anvin | d9c5841 | 2010-04-29 16:53:17 -0700 | [diff] [blame] | 450 | if (*ptr == 0x3e) |
| 451 | text_poke(ptr, ((unsigned char []){0xf0}), 1); |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 452 | } |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 455 | static void alternatives_smp_unlock(const s32 *start, const s32 *end, |
| 456 | u8 *text, u8 *text_end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 457 | { |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 458 | const s32 *poff; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 459 | |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 460 | for (poff = start; poff < end; poff++) { |
| 461 | u8 *ptr = (u8 *)poff + *poff; |
| 462 | |
| 463 | if (!*poff || ptr < text || ptr >= text_end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 464 | continue; |
Mathieu Desnoyers | f88f07e | 2008-08-14 16:58:15 -0400 | [diff] [blame] | 465 | /* turn lock prefix into DS segment override prefix */ |
H. Peter Anvin | d9c5841 | 2010-04-29 16:53:17 -0700 | [diff] [blame] | 466 | if (*ptr == 0xf0) |
| 467 | text_poke(ptr, ((unsigned char []){0x3E}), 1); |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 468 | } |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | struct smp_alt_module { |
| 472 | /* what is this ??? */ |
| 473 | struct module *mod; |
| 474 | char *name; |
| 475 | |
| 476 | /* ptrs to lock prefixes */ |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 477 | const s32 *locks; |
| 478 | const s32 *locks_end; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 479 | |
| 480 | /* .text segment, needed to avoid patching init code ;) */ |
| 481 | u8 *text; |
| 482 | u8 *text_end; |
| 483 | |
| 484 | struct list_head next; |
| 485 | }; |
| 486 | static LIST_HEAD(smp_alt_modules); |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 487 | static bool uniproc_patched = false; /* protected by text_mutex */ |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 488 | |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 489 | void __init_or_module alternatives_smp_module_add(struct module *mod, |
| 490 | char *name, |
| 491 | void *locks, void *locks_end, |
| 492 | void *text, void *text_end) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 493 | { |
| 494 | struct smp_alt_module *smp; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 495 | |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 496 | mutex_lock(&text_mutex); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 497 | if (!uniproc_patched) |
| 498 | goto unlock; |
Jeremy Fitzhardinge | b7fb4af | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 499 | |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 500 | if (num_possible_cpus() == 1) |
| 501 | /* Don't bother remembering, we'll never have to undo it. */ |
| 502 | goto smp_unlock; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 503 | |
| 504 | smp = kzalloc(sizeof(*smp), GFP_KERNEL); |
| 505 | if (NULL == smp) |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 506 | /* we'll run the (safe but slow) SMP code then ... */ |
| 507 | goto unlock; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 508 | |
| 509 | smp->mod = mod; |
| 510 | smp->name = name; |
| 511 | smp->locks = locks; |
| 512 | smp->locks_end = locks_end; |
| 513 | smp->text = text; |
| 514 | smp->text_end = text_end; |
Borislav Petkov | db477a3 | 2014-12-30 20:27:09 +0100 | [diff] [blame] | 515 | DPRINTK("locks %p -> %p, text %p -> %p, name %s\n", |
| 516 | smp->locks, smp->locks_end, |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 517 | smp->text, smp->text_end, smp->name); |
| 518 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 519 | list_add_tail(&smp->next, &smp_alt_modules); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 520 | smp_unlock: |
| 521 | alternatives_smp_unlock(locks, locks_end, text, text_end); |
| 522 | unlock: |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 523 | mutex_unlock(&text_mutex); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 526 | void __init_or_module alternatives_smp_module_del(struct module *mod) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 527 | { |
| 528 | struct smp_alt_module *item; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 529 | |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 530 | mutex_lock(&text_mutex); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 531 | list_for_each_entry(item, &smp_alt_modules, next) { |
| 532 | if (mod != item->mod) |
| 533 | continue; |
| 534 | list_del(&item->next); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 535 | kfree(item); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 536 | break; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 537 | } |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 538 | mutex_unlock(&text_mutex); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 541 | void alternatives_enable_smp(void) |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 542 | { |
| 543 | struct smp_alt_module *mod; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 544 | |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 545 | /* Why bother if there are no other CPUs? */ |
| 546 | BUG_ON(num_possible_cpus() == 1); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 547 | |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 548 | mutex_lock(&text_mutex); |
Andi Kleen | ca74a6f | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 549 | |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 550 | if (uniproc_patched) { |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 551 | pr_info("switching to SMP code\n"); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 552 | BUG_ON(num_online_cpus() != 1); |
Jeremy Fitzhardinge | 53756d3 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 553 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP); |
| 554 | clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 555 | list_for_each_entry(mod, &smp_alt_modules, next) |
| 556 | alternatives_smp_lock(mod->locks, mod->locks_end, |
| 557 | mod->text, mod->text_end); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 558 | uniproc_patched = false; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 559 | } |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 560 | mutex_unlock(&text_mutex); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 563 | /* |
| 564 | * Return 1 if the address range is reserved for SMP-alternatives. |
| 565 | * Must hold text_mutex. |
| 566 | */ |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 567 | int alternatives_text_reserved(void *start, void *end) |
| 568 | { |
| 569 | struct smp_alt_module *mod; |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 570 | const s32 *poff; |
Masami Hiramatsu | 076dc4a | 2010-02-05 12:16:47 -0500 | [diff] [blame] | 571 | u8 *text_start = start; |
| 572 | u8 *text_end = end; |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 573 | |
Zhou Chengming | e846d13 | 2017-11-02 09:18:21 +0800 | [diff] [blame] | 574 | lockdep_assert_held(&text_mutex); |
| 575 | |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 576 | list_for_each_entry(mod, &smp_alt_modules, next) { |
Masami Hiramatsu | 076dc4a | 2010-02-05 12:16:47 -0500 | [diff] [blame] | 577 | if (mod->text > text_end || mod->text_end < text_start) |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 578 | continue; |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 579 | for (poff = mod->locks; poff < mod->locks_end; poff++) { |
| 580 | const u8 *ptr = (const u8 *)poff + *poff; |
| 581 | |
| 582 | if (text_start <= ptr && text_end > ptr) |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 583 | return 1; |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 584 | } |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
Borislav Petkov | 48c7a25 | 2015-01-05 13:48:41 +0100 | [diff] [blame] | 589 | #endif /* CONFIG_SMP */ |
Gerd Hoffmann | 8ec4d41 | 2006-07-01 04:36:18 -0700 | [diff] [blame] | 590 | |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 591 | #ifdef CONFIG_PARAVIRT |
Jan Beulich | 8b5a10f | 2009-08-19 08:40:48 +0100 | [diff] [blame] | 592 | void __init_or_module apply_paravirt(struct paravirt_patch_site *start, |
| 593 | struct paravirt_patch_site *end) |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 594 | { |
Jeremy Fitzhardinge | 98de032b | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 595 | struct paravirt_patch_site *p; |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 596 | char insnbuf[MAX_PATCH_LEN]; |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 597 | |
| 598 | for (p = start; p < end; p++) { |
| 599 | unsigned int used; |
| 600 | |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 601 | BUG_ON(p->len > MAX_PATCH_LEN); |
Chris Wright | d34fda4 | 2007-08-18 14:31:41 -0700 | [diff] [blame] | 602 | /* prep the buffer with the original instructions */ |
| 603 | memcpy(insnbuf, p->instr, p->len); |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 604 | used = pv_ops.init.patch(p->instrtype, insnbuf, |
Jeremy Fitzhardinge | 93b1eab | 2007-10-16 11:51:29 -0700 | [diff] [blame] | 605 | (unsigned long)p->instr, p->len); |
Jeremy Fitzhardinge | 7f63c41 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 606 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 607 | BUG_ON(used > p->len); |
| 608 | |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 609 | /* Pad the rest with nops */ |
Andi Kleen | ab144f5 | 2007-08-10 22:31:03 +0200 | [diff] [blame] | 610 | add_nops(insnbuf + used, p->len - used); |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 611 | text_poke_early(p->instr, insnbuf, p->len); |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 612 | } |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 613 | } |
Jeremy Fitzhardinge | 98de032b | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 614 | extern struct paravirt_patch_site __start_parainstructions[], |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 615 | __stop_parainstructions[]; |
| 616 | #endif /* CONFIG_PARAVIRT */ |
| 617 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 618 | void __init alternative_instructions(void) |
| 619 | { |
Andi Kleen | 8f4e956 | 2007-07-22 11:12:32 +0200 | [diff] [blame] | 620 | /* The patching is not fully atomic, so try to avoid local interruptions |
| 621 | that might execute the to be patched code. |
| 622 | Other CPUs are not running. */ |
| 623 | stop_nmi(); |
Andi Kleen | 123aa76 | 2009-02-12 13:39:27 +0100 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * Don't stop machine check exceptions while patching. |
| 627 | * MCEs only happen when something got corrupted and in this |
| 628 | * case we must do something about the corruption. |
| 629 | * Ignoring it is worse than a unlikely patching race. |
| 630 | * Also machine checks tend to be broadcast and if one CPU |
| 631 | * goes into machine check the others follow quickly, so we don't |
| 632 | * expect a machine check to cause undue problems during to code |
| 633 | * patching. |
| 634 | */ |
Andi Kleen | 8f4e956 | 2007-07-22 11:12:32 +0200 | [diff] [blame] | 635 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 636 | apply_alternatives(__alt_instructions, __alt_instructions_end); |
| 637 | |
Gerd Hoffmann | 8ec4d41 | 2006-07-01 04:36:18 -0700 | [diff] [blame] | 638 | #ifdef CONFIG_SMP |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 639 | /* Patch to UP if other cpus not imminent. */ |
| 640 | if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) { |
| 641 | uniproc_patched = true; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 642 | alternatives_smp_module_add(NULL, "core kernel", |
| 643 | __smp_locks, __smp_locks_end, |
| 644 | _text, _etext); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 645 | } |
Andi Kleen | 8f4e956 | 2007-07-22 11:12:32 +0200 | [diff] [blame] | 646 | |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 647 | if (!uniproc_patched || num_possible_cpus() == 1) |
Fengguang Wu | f68fd5f | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 648 | free_init_pages("SMP alternatives", |
| 649 | (unsigned long)__smp_locks, |
| 650 | (unsigned long)__smp_locks_end); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 651 | #endif |
| 652 | |
| 653 | apply_paravirt(__parainstructions, __parainstructions_end); |
Fengguang Wu | f68fd5f | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 654 | |
Andi Kleen | 8f4e956 | 2007-07-22 11:12:32 +0200 | [diff] [blame] | 655 | restart_nmi(); |
Ingo Molnar | 5e907bb | 2015-04-30 09:09:26 +0200 | [diff] [blame] | 656 | alternatives_patched = 1; |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 657 | } |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 658 | |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 659 | /** |
| 660 | * text_poke_early - Update instructions on a live kernel at boot time |
| 661 | * @addr: address to modify |
| 662 | * @opcode: source of the copy |
| 663 | * @len: length to copy |
| 664 | * |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 665 | * When you use this code to patch more than one byte of an instruction |
| 666 | * you need to make sure that other CPUs cannot execute this code in parallel. |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 667 | * Also no thread must be currently preempted in the middle of these |
| 668 | * instructions. And on the local CPU you need to be protected again NMI or MCE |
| 669 | * handlers seeing an inconsistent instruction while you patch. |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 670 | */ |
Nadav Amit | 0a203df | 2019-04-25 17:11:33 -0700 | [diff] [blame] | 671 | void __init_or_module text_poke_early(void *addr, const void *opcode, |
| 672 | size_t len) |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 673 | { |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 674 | unsigned long flags; |
Nadav Amit | f2c65fb | 2019-04-25 17:11:31 -0700 | [diff] [blame] | 675 | |
| 676 | if (boot_cpu_has(X86_FEATURE_NX) && |
| 677 | is_module_text_address((unsigned long)addr)) { |
| 678 | /* |
| 679 | * Modules text is marked initially as non-executable, so the |
| 680 | * code cannot be running and speculative code-fetches are |
| 681 | * prevented. Just change the code. |
| 682 | */ |
| 683 | memcpy(addr, opcode, len); |
| 684 | } else { |
| 685 | local_irq_save(flags); |
| 686 | memcpy(addr, opcode, len); |
| 687 | local_irq_restore(flags); |
| 688 | sync_core(); |
| 689 | |
| 690 | /* |
| 691 | * Could also do a CLFLUSH here to speed up CPU recovery; but |
| 692 | * that causes hangs on some VIA CPUs. |
| 693 | */ |
| 694 | } |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 695 | } |
| 696 | |
Nadav Amit | 4fc1970 | 2019-04-26 16:22:46 -0700 | [diff] [blame] | 697 | __ro_after_init struct mm_struct *poking_mm; |
| 698 | __ro_after_init unsigned long poking_addr; |
| 699 | |
Nadav Amit | e836673 | 2019-04-25 17:11:21 -0700 | [diff] [blame] | 700 | static void *__text_poke(void *addr, const void *opcode, size_t len) |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 701 | { |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 702 | bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE; |
| 703 | struct page *pages[2] = {NULL}; |
| 704 | temp_mm_state_t prev; |
Masami Hiramatsu | 78ff7fa | 2009-03-06 10:37:54 -0500 | [diff] [blame] | 705 | unsigned long flags; |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 706 | pte_t pte, *ptep; |
| 707 | spinlock_t *ptl; |
| 708 | pgprot_t pgprot; |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 709 | |
Pavel Tatashin | 6fffacb | 2018-07-19 16:55:27 -0400 | [diff] [blame] | 710 | /* |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 711 | * While boot memory allocator is running we cannot use struct pages as |
| 712 | * they are not yet initialized. There is no way to recover. |
Pavel Tatashin | 6fffacb | 2018-07-19 16:55:27 -0400 | [diff] [blame] | 713 | */ |
| 714 | BUG_ON(!after_bootmem); |
| 715 | |
Mathieu Desnoyers | b7b66ba | 2008-04-24 11:03:33 -0400 | [diff] [blame] | 716 | if (!core_kernel_text((unsigned long)addr)) { |
| 717 | pages[0] = vmalloc_to_page(addr); |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 718 | if (cross_page_boundary) |
| 719 | pages[1] = vmalloc_to_page(addr + PAGE_SIZE); |
Mathieu Desnoyers | 15a601e | 2008-03-12 11:54:16 -0400 | [diff] [blame] | 720 | } else { |
Mathieu Desnoyers | b7b66ba | 2008-04-24 11:03:33 -0400 | [diff] [blame] | 721 | pages[0] = virt_to_page(addr); |
Ingo Molnar | 00c6b2d | 2008-04-25 17:07:03 +0200 | [diff] [blame] | 722 | WARN_ON(!PageReserved(pages[0])); |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 723 | if (cross_page_boundary) |
| 724 | pages[1] = virt_to_page(addr + PAGE_SIZE); |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 725 | } |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 726 | /* |
| 727 | * If something went wrong, crash and burn since recovery paths are not |
| 728 | * implemented. |
| 729 | */ |
| 730 | BUG_ON(!pages[0] || (cross_page_boundary && !pages[1])); |
| 731 | |
Masami Hiramatsu | 7cf4942 | 2009-03-09 12:40:40 -0400 | [diff] [blame] | 732 | local_irq_save(flags); |
Nadav Amit | b3fd8e8 | 2019-04-25 17:11:27 -0700 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * Map the page without the global bit, as TLB flushing is done with |
| 736 | * flush_tlb_mm_range(), which is intended for non-global PTEs. |
| 737 | */ |
| 738 | pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL); |
| 739 | |
| 740 | /* |
| 741 | * The lock is not really needed, but this allows to avoid open-coding. |
| 742 | */ |
| 743 | ptep = get_locked_pte(poking_mm, poking_addr, &ptl); |
| 744 | |
| 745 | /* |
| 746 | * This must not fail; preallocated in poking_init(). |
| 747 | */ |
| 748 | VM_BUG_ON(!ptep); |
| 749 | |
| 750 | pte = mk_pte(pages[0], pgprot); |
| 751 | set_pte_at(poking_mm, poking_addr, ptep, pte); |
| 752 | |
| 753 | if (cross_page_boundary) { |
| 754 | pte = mk_pte(pages[1], pgprot); |
| 755 | set_pte_at(poking_mm, poking_addr + PAGE_SIZE, ptep + 1, pte); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Loading the temporary mm behaves as a compiler barrier, which |
| 760 | * guarantees that the PTE will be set at the time memcpy() is done. |
| 761 | */ |
| 762 | prev = use_temporary_mm(poking_mm); |
| 763 | |
| 764 | kasan_disable_current(); |
| 765 | memcpy((u8 *)poking_addr + offset_in_page(addr), opcode, len); |
| 766 | kasan_enable_current(); |
| 767 | |
| 768 | /* |
| 769 | * Ensure that the PTE is only cleared after the instructions of memcpy |
| 770 | * were issued by using a compiler barrier. |
| 771 | */ |
| 772 | barrier(); |
| 773 | |
| 774 | pte_clear(poking_mm, poking_addr, ptep); |
| 775 | if (cross_page_boundary) |
| 776 | pte_clear(poking_mm, poking_addr + PAGE_SIZE, ptep + 1); |
| 777 | |
| 778 | /* |
| 779 | * Loading the previous page-table hierarchy requires a serializing |
| 780 | * instruction that already allows the core to see the updated version. |
| 781 | * Xen-PV is assumed to serialize execution in a similar manner. |
| 782 | */ |
| 783 | unuse_temporary_mm(prev); |
| 784 | |
| 785 | /* |
| 786 | * Flushing the TLB might involve IPIs, which would require enabled |
| 787 | * IRQs, but not if the mm is not used, as it is in this point. |
| 788 | */ |
| 789 | flush_tlb_mm_range(poking_mm, poking_addr, poking_addr + |
| 790 | (cross_page_boundary ? 2 : 1) * PAGE_SIZE, |
| 791 | PAGE_SHIFT, false); |
| 792 | |
| 793 | /* |
| 794 | * If the text does not match what we just wrote then something is |
| 795 | * fundamentally screwy; there's nothing we can really do about that. |
| 796 | */ |
| 797 | BUG_ON(memcmp(addr, opcode, len)); |
| 798 | |
| 799 | pte_unmap_unlock(ptep, ptl); |
Masami Hiramatsu | 7cf4942 | 2009-03-09 12:40:40 -0400 | [diff] [blame] | 800 | local_irq_restore(flags); |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 801 | return addr; |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 802 | } |
Masami Hiramatsu | 3d55cc8 | 2010-02-25 08:34:38 -0500 | [diff] [blame] | 803 | |
Nadav Amit | e836673 | 2019-04-25 17:11:21 -0700 | [diff] [blame] | 804 | /** |
| 805 | * text_poke - Update instructions on a live kernel |
| 806 | * @addr: address to modify |
| 807 | * @opcode: source of the copy |
| 808 | * @len: length to copy |
| 809 | * |
| 810 | * Only atomic text poke/set should be allowed when not doing early patching. |
| 811 | * It means the size must be writable atomically and the address must be aligned |
| 812 | * in a way that permits an atomic write. It also makes sure we fit on a single |
| 813 | * page. |
Nadav Amit | 3950746 | 2019-04-25 17:11:41 -0700 | [diff] [blame] | 814 | * |
| 815 | * Note that the caller must ensure that if the modified code is part of a |
| 816 | * module, the module would not be removed during poking. This can be achieved |
| 817 | * by registering a module notifier, and ordering module removal and patching |
| 818 | * trough a mutex. |
Nadav Amit | e836673 | 2019-04-25 17:11:21 -0700 | [diff] [blame] | 819 | */ |
| 820 | void *text_poke(void *addr, const void *opcode, size_t len) |
| 821 | { |
| 822 | lockdep_assert_held(&text_mutex); |
| 823 | |
| 824 | return __text_poke(addr, opcode, len); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * text_poke_kgdb - Update instructions on a live kernel by kgdb |
| 829 | * @addr: address to modify |
| 830 | * @opcode: source of the copy |
| 831 | * @len: length to copy |
| 832 | * |
| 833 | * Only atomic text poke/set should be allowed when not doing early patching. |
| 834 | * It means the size must be writable atomically and the address must be aligned |
| 835 | * in a way that permits an atomic write. It also makes sure we fit on a single |
| 836 | * page. |
| 837 | * |
| 838 | * Context: should only be used by kgdb, which ensures no other core is running, |
| 839 | * despite the fact it does not hold the text_mutex. |
| 840 | */ |
| 841 | void *text_poke_kgdb(void *addr, const void *opcode, size_t len) |
| 842 | { |
| 843 | return __text_poke(addr, opcode, len); |
| 844 | } |
| 845 | |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 846 | static void do_sync_core(void *info) |
| 847 | { |
| 848 | sync_core(); |
| 849 | } |
| 850 | |
| 851 | static bool bp_patching_in_progress; |
| 852 | static void *bp_int3_handler, *bp_int3_addr; |
| 853 | |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 854 | int poke_int3_handler(struct pt_regs *regs) |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 855 | { |
Peter Zijlstra | 0165132 | 2017-07-31 12:21:54 +0200 | [diff] [blame] | 856 | /* |
| 857 | * Having observed our INT3 instruction, we now must observe |
| 858 | * bp_patching_in_progress. |
| 859 | * |
| 860 | * in_progress = TRUE INT3 |
| 861 | * WMB RMB |
| 862 | * write INT3 if (in_progress) |
| 863 | * |
| 864 | * Idem for bp_int3_handler. |
| 865 | */ |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 866 | smp_rmb(); |
| 867 | |
| 868 | if (likely(!bp_patching_in_progress)) |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 869 | return 0; |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 870 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 871 | if (user_mode(regs) || regs->ip != (unsigned long)bp_int3_addr) |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 872 | return 0; |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 873 | |
| 874 | /* set up the specified breakpoint handler */ |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 875 | regs->ip = (unsigned long) bp_int3_handler; |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 876 | |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 877 | return 1; |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 878 | } |
Masami Hiramatsu | c13324a | 2019-02-13 01:12:15 +0900 | [diff] [blame] | 879 | NOKPROBE_SYMBOL(poke_int3_handler); |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 880 | |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 881 | /** |
| 882 | * text_poke_bp() -- update instructions on live kernel on SMP |
| 883 | * @addr: address to patch |
| 884 | * @opcode: opcode of new instruction |
| 885 | * @len: length to copy |
| 886 | * @handler: address to jump to when the temporary breakpoint is hit |
| 887 | * |
| 888 | * Modify multi-byte instruction by using int3 breakpoint on SMP. |
Masami Hiramatsu | ea8596b | 2013-07-18 20:47:53 +0900 | [diff] [blame] | 889 | * We completely avoid stop_machine() here, and achieve the |
| 890 | * synchronization using int3 breakpoint. |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 891 | * |
| 892 | * The way it is done: |
| 893 | * - add a int3 trap to the address that will be patched |
| 894 | * - sync cores |
| 895 | * - update all but the first byte of the patched range |
| 896 | * - sync cores |
| 897 | * - replace the first byte (int3) by the first byte of |
| 898 | * replacing opcode |
| 899 | * - sync cores |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 900 | */ |
Nadav Amit | 0a203df | 2019-04-25 17:11:33 -0700 | [diff] [blame] | 901 | void text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 902 | { |
| 903 | unsigned char int3 = 0xcc; |
| 904 | |
| 905 | bp_int3_handler = handler; |
| 906 | bp_int3_addr = (u8 *)addr + sizeof(int3); |
| 907 | bp_patching_in_progress = true; |
Jiri Kosina | 9222f60 | 2018-08-28 08:55:14 +0200 | [diff] [blame] | 908 | |
| 909 | lockdep_assert_held(&text_mutex); |
| 910 | |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 911 | /* |
Peter Zijlstra | 0165132 | 2017-07-31 12:21:54 +0200 | [diff] [blame] | 912 | * Corresponding read barrier in int3 notifier for making sure the |
| 913 | * in_progress and handler are correctly ordered wrt. patching. |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 914 | */ |
| 915 | smp_wmb(); |
| 916 | |
| 917 | text_poke(addr, &int3, sizeof(int3)); |
| 918 | |
| 919 | on_each_cpu(do_sync_core, NULL, 1); |
| 920 | |
| 921 | if (len - sizeof(int3) > 0) { |
| 922 | /* patch all but the first byte */ |
| 923 | text_poke((char *)addr + sizeof(int3), |
| 924 | (const char *) opcode + sizeof(int3), |
| 925 | len - sizeof(int3)); |
| 926 | /* |
| 927 | * According to Intel, this core syncing is very likely |
| 928 | * not necessary and we'd be safe even without it. But |
| 929 | * better safe than sorry (plus there's not only Intel). |
| 930 | */ |
| 931 | on_each_cpu(do_sync_core, NULL, 1); |
| 932 | } |
| 933 | |
| 934 | /* patch the first byte */ |
| 935 | text_poke(addr, opcode, sizeof(int3)); |
| 936 | |
| 937 | on_each_cpu(do_sync_core, NULL, 1); |
Peter Zijlstra | 0165132 | 2017-07-31 12:21:54 +0200 | [diff] [blame] | 938 | /* |
| 939 | * sync_core() implies an smp_mb() and orders this store against |
| 940 | * the writing of the new instruction. |
| 941 | */ |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 942 | bp_patching_in_progress = false; |
Jiri Kosina | fd4363f | 2013-07-12 11:21:48 +0200 | [diff] [blame] | 943 | } |
| 944 | |