WIP FPC-III support
[linux/fpc-iii.git] / arch / arm64 / kernel / alternative.c
bloba57cffb752e8955b8b9b9dbc144f632dead81402
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * alternative runtime patching
4 * inspired by the x86 version
6 * Copyright (C) 2014 ARM Ltd.
7 */
9 #define pr_fmt(fmt) "alternatives: " fmt
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>
16 #include <asm/insn.h>
17 #include <asm/sections.h>
18 #include <linux/stop_machine.h>
20 #define __ALT_PTR(a,f) ((void *)&(a)->f + (a)->f)
21 #define ALT_ORIG_PTR(a) __ALT_PTR(a, orig_offset)
22 #define ALT_REPL_PTR(a) __ALT_PTR(a, alt_offset)
24 /* Volatile, as we may be patching the guts of READ_ONCE() */
25 static volatile int all_alternatives_applied;
27 static DECLARE_BITMAP(applied_alternatives, ARM64_NCAPS);
29 struct alt_region {
30 struct alt_instr *begin;
31 struct alt_instr *end;
34 bool alternative_is_applied(u16 cpufeature)
36 if (WARN_ON(cpufeature >= ARM64_NCAPS))
37 return false;
39 return test_bit(cpufeature, applied_alternatives);
43 * Check if the target PC is within an alternative block.
45 static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
47 unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt);
48 return !(pc >= replptr && pc <= (replptr + alt->alt_len));
51 #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
53 static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
55 u32 insn;
57 insn = le32_to_cpu(*altinsnptr);
59 if (aarch64_insn_is_branch_imm(insn)) {
60 s32 offset = aarch64_get_branch_offset(insn);
61 unsigned long target;
63 target = (unsigned long)altinsnptr + offset;
66 * If we're branching inside the alternate sequence,
67 * do not rewrite the instruction, as it is already
68 * correct. Otherwise, generate the new instruction.
70 if (branch_insn_requires_update(alt, target)) {
71 offset = target - (unsigned long)insnptr;
72 insn = aarch64_set_branch_offset(insn, offset);
74 } else if (aarch64_insn_is_adrp(insn)) {
75 s32 orig_offset, new_offset;
76 unsigned long target;
79 * If we're replacing an adrp instruction, which uses PC-relative
80 * immediate addressing, adjust the offset to reflect the new
81 * PC. adrp operates on 4K aligned addresses.
83 orig_offset = aarch64_insn_adrp_get_offset(insn);
84 target = align_down(altinsnptr, SZ_4K) + orig_offset;
85 new_offset = target - align_down(insnptr, SZ_4K);
86 insn = aarch64_insn_adrp_set_offset(insn, new_offset);
87 } else if (aarch64_insn_uses_literal(insn)) {
89 * Disallow patching unhandled instructions using PC relative
90 * literal addresses
92 BUG();
95 return insn;
98 static void patch_alternative(struct alt_instr *alt,
99 __le32 *origptr, __le32 *updptr, int nr_inst)
101 __le32 *replptr;
102 int i;
104 replptr = ALT_REPL_PTR(alt);
105 for (i = 0; i < nr_inst; i++) {
106 u32 insn;
108 insn = get_alt_insn(alt, origptr + i, replptr + i);
109 updptr[i] = cpu_to_le32(insn);
114 * We provide our own, private D-cache cleaning function so that we don't
115 * accidentally call into the cache.S code, which is patched by us at
116 * runtime.
118 static void clean_dcache_range_nopatch(u64 start, u64 end)
120 u64 cur, d_size, ctr_el0;
122 ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0);
123 d_size = 4 << cpuid_feature_extract_unsigned_field(ctr_el0,
124 CTR_DMINLINE_SHIFT);
125 cur = start & ~(d_size - 1);
126 do {
128 * We must clean+invalidate to the PoC in order to avoid
129 * Cortex-A53 errata 826319, 827319, 824069 and 819472
130 * (this corresponds to ARM64_WORKAROUND_CLEAN_CACHE)
132 asm volatile("dc civac, %0" : : "r" (cur) : "memory");
133 } while (cur += d_size, cur < end);
136 static void __apply_alternatives(void *alt_region, bool is_module,
137 unsigned long *feature_mask)
139 struct alt_instr *alt;
140 struct alt_region *region = alt_region;
141 __le32 *origptr, *updptr;
142 alternative_cb_t alt_cb;
144 for (alt = region->begin; alt < region->end; alt++) {
145 int nr_inst;
147 if (!test_bit(alt->cpufeature, feature_mask))
148 continue;
150 /* Use ARM64_CB_PATCH as an unconditional patch */
151 if (alt->cpufeature < ARM64_CB_PATCH &&
152 !cpus_have_cap(alt->cpufeature))
153 continue;
155 if (alt->cpufeature == ARM64_CB_PATCH)
156 BUG_ON(alt->alt_len != 0);
157 else
158 BUG_ON(alt->alt_len != alt->orig_len);
160 pr_info_once("patching kernel code\n");
162 origptr = ALT_ORIG_PTR(alt);
163 updptr = is_module ? origptr : lm_alias(origptr);
164 nr_inst = alt->orig_len / AARCH64_INSN_SIZE;
166 if (alt->cpufeature < ARM64_CB_PATCH)
167 alt_cb = patch_alternative;
168 else
169 alt_cb = ALT_REPL_PTR(alt);
171 alt_cb(alt, origptr, updptr, nr_inst);
173 if (!is_module) {
174 clean_dcache_range_nopatch((u64)origptr,
175 (u64)(origptr + nr_inst));
180 * The core module code takes care of cache maintenance in
181 * flush_module_icache().
183 if (!is_module) {
184 dsb(ish);
185 __flush_icache_all();
186 isb();
188 /* Ignore ARM64_CB bit from feature mask */
189 bitmap_or(applied_alternatives, applied_alternatives,
190 feature_mask, ARM64_NCAPS);
191 bitmap_and(applied_alternatives, applied_alternatives,
192 cpu_hwcaps, ARM64_NCAPS);
197 * We might be patching the stop_machine state machine, so implement a
198 * really simple polling protocol here.
200 static int __apply_alternatives_multi_stop(void *unused)
202 struct alt_region region = {
203 .begin = (struct alt_instr *)__alt_instructions,
204 .end = (struct alt_instr *)__alt_instructions_end,
207 /* We always have a CPU 0 at this point (__init) */
208 if (smp_processor_id()) {
209 while (!all_alternatives_applied)
210 cpu_relax();
211 isb();
212 } else {
213 DECLARE_BITMAP(remaining_capabilities, ARM64_NPATCHABLE);
215 bitmap_complement(remaining_capabilities, boot_capabilities,
216 ARM64_NPATCHABLE);
218 BUG_ON(all_alternatives_applied);
219 __apply_alternatives(&region, false, remaining_capabilities);
220 /* Barriers provided by the cache flushing */
221 all_alternatives_applied = 1;
224 return 0;
227 void __init apply_alternatives_all(void)
229 /* better not try code patching on a live SMP system */
230 stop_machine(__apply_alternatives_multi_stop, NULL, cpu_online_mask);
234 * This is called very early in the boot process (directly after we run
235 * a feature detect on the boot CPU). No need to worry about other CPUs
236 * here.
238 void __init apply_boot_alternatives(void)
240 struct alt_region region = {
241 .begin = (struct alt_instr *)__alt_instructions,
242 .end = (struct alt_instr *)__alt_instructions_end,
245 /* If called on non-boot cpu things could go wrong */
246 WARN_ON(smp_processor_id() != 0);
248 __apply_alternatives(&region, false, &boot_capabilities[0]);
251 #ifdef CONFIG_MODULES
252 void apply_alternatives_module(void *start, size_t length)
254 struct alt_region region = {
255 .begin = start,
256 .end = start + length,
258 DECLARE_BITMAP(all_capabilities, ARM64_NPATCHABLE);
260 bitmap_fill(all_capabilities, ARM64_NPATCHABLE);
262 __apply_alternatives(&region, true, &all_capabilities[0]);
264 #endif