Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 2 | /* |
| 3 | * alternative runtime patching |
| 4 | * inspired by the x86 version |
| 5 | * |
| 6 | * Copyright (C) 2014 ARM Ltd. |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) "alternatives: " fmt |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <asm/cacheflush.h> |
| 14 | #include <asm/alternative.h> |
| 15 | #include <asm/cpufeature.h> |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 16 | #include <asm/insn.h> |
James Morse | ee78fdc | 2016-08-24 18:27:28 +0100 | [diff] [blame] | 17 | #include <asm/sections.h> |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 18 | #include <linux/stop_machine.h> |
| 19 | |
Luc Van Oostenryck | 15ad6ac | 2017-06-29 16:40:12 +0200 | [diff] [blame] | 20 | #define __ALT_PTR(a,f) ((void *)&(a)->f + (a)->f) |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 21 | #define ALT_ORIG_PTR(a) __ALT_PTR(a, orig_offset) |
| 22 | #define ALT_REPL_PTR(a) __ALT_PTR(a, alt_offset) |
| 23 | |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 24 | static int all_alternatives_applied; |
| 25 | |
| 26 | static DECLARE_BITMAP(applied_alternatives, ARM64_NCAPS); |
James Morse | 6d99b68 | 2018-01-08 15:38:06 +0000 | [diff] [blame] | 27 | |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 28 | struct alt_region { |
| 29 | struct alt_instr *begin; |
| 30 | struct alt_instr *end; |
| 31 | }; |
| 32 | |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 33 | bool alternative_is_applied(u16 cpufeature) |
| 34 | { |
| 35 | if (WARN_ON(cpufeature >= ARM64_NCAPS)) |
| 36 | return false; |
| 37 | |
| 38 | return test_bit(cpufeature, applied_alternatives); |
| 39 | } |
| 40 | |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 41 | /* |
| 42 | * Check if the target PC is within an alternative block. |
| 43 | */ |
| 44 | static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc) |
| 45 | { |
| 46 | unsigned long replptr; |
| 47 | |
| 48 | if (kernel_text_address(pc)) |
Gustavo A. R. Silva | 3c4d913 | 2018-08-07 18:59:57 -0500 | [diff] [blame] | 49 | return true; |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 50 | |
| 51 | replptr = (unsigned long)ALT_REPL_PTR(alt); |
| 52 | if (pc >= replptr && pc <= (replptr + alt->alt_len)) |
Gustavo A. R. Silva | 3c4d913 | 2018-08-07 18:59:57 -0500 | [diff] [blame] | 53 | return false; |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Branching into *another* alternate sequence is doomed, and |
| 57 | * we're not even trying to fix it up. |
| 58 | */ |
| 59 | BUG(); |
| 60 | } |
| 61 | |
Suzuki K Poulose | c831b2a | 2016-09-09 14:07:13 +0100 | [diff] [blame] | 62 | #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1)) |
| 63 | |
Luc Van Oostenryck | 15ad6ac | 2017-06-29 16:40:12 +0200 | [diff] [blame] | 64 | static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr) |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 65 | { |
| 66 | u32 insn; |
| 67 | |
| 68 | insn = le32_to_cpu(*altinsnptr); |
| 69 | |
| 70 | if (aarch64_insn_is_branch_imm(insn)) { |
| 71 | s32 offset = aarch64_get_branch_offset(insn); |
| 72 | unsigned long target; |
| 73 | |
| 74 | target = (unsigned long)altinsnptr + offset; |
| 75 | |
| 76 | /* |
| 77 | * If we're branching inside the alternate sequence, |
| 78 | * do not rewrite the instruction, as it is already |
| 79 | * correct. Otherwise, generate the new instruction. |
| 80 | */ |
| 81 | if (branch_insn_requires_update(alt, target)) { |
| 82 | offset = target - (unsigned long)insnptr; |
| 83 | insn = aarch64_set_branch_offset(insn, offset); |
| 84 | } |
Suzuki K Poulose | c831b2a | 2016-09-09 14:07:13 +0100 | [diff] [blame] | 85 | } else if (aarch64_insn_is_adrp(insn)) { |
| 86 | s32 orig_offset, new_offset; |
| 87 | unsigned long target; |
| 88 | |
| 89 | /* |
| 90 | * If we're replacing an adrp instruction, which uses PC-relative |
| 91 | * immediate addressing, adjust the offset to reflect the new |
| 92 | * PC. adrp operates on 4K aligned addresses. |
| 93 | */ |
| 94 | orig_offset = aarch64_insn_adrp_get_offset(insn); |
| 95 | target = align_down(altinsnptr, SZ_4K) + orig_offset; |
| 96 | new_offset = target - align_down(insnptr, SZ_4K); |
| 97 | insn = aarch64_insn_adrp_set_offset(insn, new_offset); |
Suzuki K Poulose | baa763b | 2016-09-09 14:07:11 +0100 | [diff] [blame] | 98 | } else if (aarch64_insn_uses_literal(insn)) { |
| 99 | /* |
| 100 | * Disallow patching unhandled instructions using PC relative |
| 101 | * literal addresses |
| 102 | */ |
| 103 | BUG(); |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return insn; |
| 107 | } |
| 108 | |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 109 | static void patch_alternative(struct alt_instr *alt, |
| 110 | __le32 *origptr, __le32 *updptr, int nr_inst) |
| 111 | { |
| 112 | __le32 *replptr; |
| 113 | int i; |
| 114 | |
| 115 | replptr = ALT_REPL_PTR(alt); |
| 116 | for (i = 0; i < nr_inst; i++) { |
| 117 | u32 insn; |
| 118 | |
| 119 | insn = get_alt_insn(alt, origptr + i, replptr + i); |
| 120 | updptr[i] = cpu_to_le32(insn); |
| 121 | } |
| 122 | } |
| 123 | |
Will Deacon | 4293886 | 2018-06-22 09:31:15 +0100 | [diff] [blame] | 124 | /* |
| 125 | * We provide our own, private D-cache cleaning function so that we don't |
| 126 | * accidentally call into the cache.S code, which is patched by us at |
| 127 | * runtime. |
| 128 | */ |
| 129 | static void clean_dcache_range_nopatch(u64 start, u64 end) |
| 130 | { |
| 131 | u64 cur, d_size, ctr_el0; |
| 132 | |
| 133 | ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0); |
| 134 | d_size = 4 << cpuid_feature_extract_unsigned_field(ctr_el0, |
| 135 | CTR_DMINLINE_SHIFT); |
| 136 | cur = start & ~(d_size - 1); |
| 137 | do { |
| 138 | /* |
| 139 | * We must clean+invalidate to the PoC in order to avoid |
| 140 | * Cortex-A53 errata 826319, 827319, 824069 and 819472 |
| 141 | * (this corresponds to ARM64_WORKAROUND_CLEAN_CACHE) |
| 142 | */ |
| 143 | asm volatile("dc civac, %0" : : "r" (cur) : "memory"); |
| 144 | } while (cur += d_size, cur < end); |
| 145 | } |
| 146 | |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 147 | static void __apply_alternatives(void *alt_region, bool is_module, |
| 148 | unsigned long *feature_mask) |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 149 | { |
| 150 | struct alt_instr *alt; |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 151 | struct alt_region *region = alt_region; |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 152 | __le32 *origptr, *updptr; |
| 153 | alternative_cb_t alt_cb; |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 154 | |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 155 | for (alt = region->begin; alt < region->end; alt++) { |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 156 | int nr_inst; |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 157 | |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 158 | if (!test_bit(alt->cpufeature, feature_mask)) |
| 159 | continue; |
| 160 | |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 161 | /* Use ARM64_CB_PATCH as an unconditional patch */ |
| 162 | if (alt->cpufeature < ARM64_CB_PATCH && |
| 163 | !cpus_have_cap(alt->cpufeature)) |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 164 | continue; |
| 165 | |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 166 | if (alt->cpufeature == ARM64_CB_PATCH) |
| 167 | BUG_ON(alt->alt_len != 0); |
| 168 | else |
| 169 | BUG_ON(alt->alt_len != alt->orig_len); |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 170 | |
| 171 | pr_info_once("patching kernel code\n"); |
| 172 | |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 173 | origptr = ALT_ORIG_PTR(alt); |
Will Deacon | 4293886 | 2018-06-22 09:31:15 +0100 | [diff] [blame] | 174 | updptr = is_module ? origptr : lm_alias(origptr); |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 175 | nr_inst = alt->orig_len / AARCH64_INSN_SIZE; |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 176 | |
Marc Zyngier | dea5e2a | 2017-12-03 12:02:14 +0000 | [diff] [blame] | 177 | if (alt->cpufeature < ARM64_CB_PATCH) |
| 178 | alt_cb = patch_alternative; |
| 179 | else |
| 180 | alt_cb = ALT_REPL_PTR(alt); |
| 181 | |
| 182 | alt_cb(alt, origptr, updptr, nr_inst); |
Marc Zyngier | 7616fc8 | 2015-06-01 10:47:40 +0100 | [diff] [blame] | 183 | |
Will Deacon | 4293886 | 2018-06-22 09:31:15 +0100 | [diff] [blame] | 184 | if (!is_module) { |
| 185 | clean_dcache_range_nopatch((u64)origptr, |
| 186 | (u64)(origptr + nr_inst)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * The core module code takes care of cache maintenance in |
| 192 | * flush_module_icache(). |
| 193 | */ |
| 194 | if (!is_module) { |
| 195 | dsb(ish); |
| 196 | __flush_icache_all(); |
| 197 | isb(); |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 198 | |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 199 | /* Ignore ARM64_CB bit from feature mask */ |
| 200 | bitmap_or(applied_alternatives, applied_alternatives, |
| 201 | feature_mask, ARM64_NCAPS); |
| 202 | bitmap_and(applied_alternatives, applied_alternatives, |
| 203 | cpu_hwcaps, ARM64_NCAPS); |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 204 | } |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 207 | /* |
| 208 | * We might be patching the stop_machine state machine, so implement a |
| 209 | * really simple polling protocol here. |
| 210 | */ |
| 211 | static int __apply_alternatives_multi_stop(void *unused) |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 212 | { |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 213 | struct alt_region region = { |
James Morse | ee78fdc | 2016-08-24 18:27:28 +0100 | [diff] [blame] | 214 | .begin = (struct alt_instr *)__alt_instructions, |
| 215 | .end = (struct alt_instr *)__alt_instructions_end, |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 218 | /* We always have a CPU 0 at this point (__init) */ |
| 219 | if (smp_processor_id()) { |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 220 | while (!READ_ONCE(all_alternatives_applied)) |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 221 | cpu_relax(); |
Will Deacon | 04b8637 | 2015-08-04 18:52:09 +0100 | [diff] [blame] | 222 | isb(); |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 223 | } else { |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 224 | DECLARE_BITMAP(remaining_capabilities, ARM64_NPATCHABLE); |
| 225 | |
| 226 | bitmap_complement(remaining_capabilities, boot_capabilities, |
| 227 | ARM64_NPATCHABLE); |
| 228 | |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 229 | BUG_ON(all_alternatives_applied); |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 230 | __apply_alternatives(®ion, false, remaining_capabilities); |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 231 | /* Barriers provided by the cache flushing */ |
Julien Thierry | e9ab7a2 | 2019-01-31 14:58:52 +0000 | [diff] [blame] | 232 | WRITE_ONCE(all_alternatives_applied, 1); |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void __init apply_alternatives_all(void) |
| 239 | { |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 240 | /* better not try code patching on a live SMP system */ |
Will Deacon | ef5e724 | 2015-07-28 19:07:28 +0100 | [diff] [blame] | 241 | stop_machine(__apply_alternatives_multi_stop, NULL, cpu_online_mask); |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 244 | /* |
| 245 | * This is called very early in the boot process (directly after we run |
| 246 | * a feature detect on the boot CPU). No need to worry about other CPUs |
| 247 | * here. |
| 248 | */ |
| 249 | void __init apply_boot_alternatives(void) |
| 250 | { |
| 251 | struct alt_region region = { |
| 252 | .begin = (struct alt_instr *)__alt_instructions, |
| 253 | .end = (struct alt_instr *)__alt_instructions_end, |
| 254 | }; |
| 255 | |
| 256 | /* If called on non-boot cpu things could go wrong */ |
| 257 | WARN_ON(smp_processor_id() != 0); |
| 258 | |
| 259 | __apply_alternatives(®ion, false, &boot_capabilities[0]); |
| 260 | } |
| 261 | |
Will Deacon | 4293886 | 2018-06-22 09:31:15 +0100 | [diff] [blame] | 262 | #ifdef CONFIG_MODULES |
| 263 | void apply_alternatives_module(void *start, size_t length) |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 264 | { |
| 265 | struct alt_region region = { |
| 266 | .begin = start, |
| 267 | .end = start + length, |
| 268 | }; |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 269 | DECLARE_BITMAP(all_capabilities, ARM64_NPATCHABLE); |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 270 | |
Daniel Thompson | 0ceb0d5 | 2019-01-31 14:58:53 +0000 | [diff] [blame] | 271 | bitmap_fill(all_capabilities, ARM64_NPATCHABLE); |
| 272 | |
| 273 | __apply_alternatives(®ion, true, &all_capabilities[0]); |
Andre Przywara | e039ee4 | 2014-11-14 15:54:08 +0000 | [diff] [blame] | 274 | } |
Will Deacon | 4293886 | 2018-06-22 09:31:15 +0100 | [diff] [blame] | 275 | #endif |