0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) "alternatives: " fmt
0010
0011 #include <linux/init.h>
0012 #include <linux/cpu.h>
0013 #include <asm/cacheflush.h>
0014 #include <asm/alternative.h>
0015 #include <asm/cpufeature.h>
0016 #include <asm/insn.h>
0017 #include <asm/sections.h>
0018 #include <linux/stop_machine.h>
0019
0020 #define __ALT_PTR(a, f) ((void *)&(a)->f + (a)->f)
0021 #define ALT_ORIG_PTR(a) __ALT_PTR(a, orig_offset)
0022 #define ALT_REPL_PTR(a) __ALT_PTR(a, alt_offset)
0023
0024
0025 static volatile int all_alternatives_applied;
0026
0027 static DECLARE_BITMAP(applied_alternatives, ARM64_NCAPS);
0028
0029 struct alt_region {
0030 struct alt_instr *begin;
0031 struct alt_instr *end;
0032 };
0033
0034 bool alternative_is_applied(u16 cpufeature)
0035 {
0036 if (WARN_ON(cpufeature >= ARM64_NCAPS))
0037 return false;
0038
0039 return test_bit(cpufeature, applied_alternatives);
0040 }
0041
0042
0043
0044
0045 static __always_inline bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
0046 {
0047 unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt);
0048 return !(pc >= replptr && pc <= (replptr + alt->alt_len));
0049 }
0050
0051 #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
0052
0053 static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
0054 {
0055 u32 insn;
0056
0057 insn = le32_to_cpu(*altinsnptr);
0058
0059 if (aarch64_insn_is_branch_imm(insn)) {
0060 s32 offset = aarch64_get_branch_offset(insn);
0061 unsigned long target;
0062
0063 target = (unsigned long)altinsnptr + offset;
0064
0065
0066
0067
0068
0069
0070 if (branch_insn_requires_update(alt, target)) {
0071 offset = target - (unsigned long)insnptr;
0072 insn = aarch64_set_branch_offset(insn, offset);
0073 }
0074 } else if (aarch64_insn_is_adrp(insn)) {
0075 s32 orig_offset, new_offset;
0076 unsigned long target;
0077
0078
0079
0080
0081
0082
0083 orig_offset = aarch64_insn_adrp_get_offset(insn);
0084 target = align_down(altinsnptr, SZ_4K) + orig_offset;
0085 new_offset = target - align_down(insnptr, SZ_4K);
0086 insn = aarch64_insn_adrp_set_offset(insn, new_offset);
0087 } else if (aarch64_insn_uses_literal(insn)) {
0088
0089
0090
0091
0092 BUG();
0093 }
0094
0095 return insn;
0096 }
0097
0098 static noinstr void patch_alternative(struct alt_instr *alt,
0099 __le32 *origptr, __le32 *updptr, int nr_inst)
0100 {
0101 __le32 *replptr;
0102 int i;
0103
0104 replptr = ALT_REPL_PTR(alt);
0105 for (i = 0; i < nr_inst; i++) {
0106 u32 insn;
0107
0108 insn = get_alt_insn(alt, origptr + i, replptr + i);
0109 updptr[i] = cpu_to_le32(insn);
0110 }
0111 }
0112
0113
0114
0115
0116
0117
0118 static void clean_dcache_range_nopatch(u64 start, u64 end)
0119 {
0120 u64 cur, d_size, ctr_el0;
0121
0122 ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0);
0123 d_size = 4 << cpuid_feature_extract_unsigned_field(ctr_el0,
0124 CTR_EL0_DminLine_SHIFT);
0125 cur = start & ~(d_size - 1);
0126 do {
0127
0128
0129
0130
0131
0132 asm volatile("dc civac, %0" : : "r" (cur) : "memory");
0133 } while (cur += d_size, cur < end);
0134 }
0135
0136 static void __nocfi __apply_alternatives(struct alt_region *region, bool is_module,
0137 unsigned long *feature_mask)
0138 {
0139 struct alt_instr *alt;
0140 __le32 *origptr, *updptr;
0141 alternative_cb_t alt_cb;
0142
0143 for (alt = region->begin; alt < region->end; alt++) {
0144 int nr_inst;
0145
0146 if (!test_bit(alt->cpufeature, feature_mask))
0147 continue;
0148
0149
0150 if (alt->cpufeature < ARM64_CB_PATCH &&
0151 !cpus_have_cap(alt->cpufeature))
0152 continue;
0153
0154 if (alt->cpufeature == ARM64_CB_PATCH)
0155 BUG_ON(alt->alt_len != 0);
0156 else
0157 BUG_ON(alt->alt_len != alt->orig_len);
0158
0159 pr_info_once("patching kernel code\n");
0160
0161 origptr = ALT_ORIG_PTR(alt);
0162 updptr = is_module ? origptr : lm_alias(origptr);
0163 nr_inst = alt->orig_len / AARCH64_INSN_SIZE;
0164
0165 if (alt->cpufeature < ARM64_CB_PATCH)
0166 alt_cb = patch_alternative;
0167 else
0168 alt_cb = ALT_REPL_PTR(alt);
0169
0170 alt_cb(alt, origptr, updptr, nr_inst);
0171
0172 if (!is_module) {
0173 clean_dcache_range_nopatch((u64)origptr,
0174 (u64)(origptr + nr_inst));
0175 }
0176 }
0177
0178
0179
0180
0181
0182 if (!is_module) {
0183 dsb(ish);
0184 icache_inval_all_pou();
0185 isb();
0186
0187
0188 bitmap_or(applied_alternatives, applied_alternatives,
0189 feature_mask, ARM64_NCAPS);
0190 bitmap_and(applied_alternatives, applied_alternatives,
0191 cpu_hwcaps, ARM64_NCAPS);
0192 }
0193 }
0194
0195
0196
0197
0198
0199 static int __apply_alternatives_multi_stop(void *unused)
0200 {
0201 struct alt_region region = {
0202 .begin = (struct alt_instr *)__alt_instructions,
0203 .end = (struct alt_instr *)__alt_instructions_end,
0204 };
0205
0206
0207 if (smp_processor_id()) {
0208 while (!all_alternatives_applied)
0209 cpu_relax();
0210 isb();
0211 } else {
0212 DECLARE_BITMAP(remaining_capabilities, ARM64_NPATCHABLE);
0213
0214 bitmap_complement(remaining_capabilities, boot_capabilities,
0215 ARM64_NPATCHABLE);
0216
0217 BUG_ON(all_alternatives_applied);
0218 __apply_alternatives(®ion, false, remaining_capabilities);
0219
0220 all_alternatives_applied = 1;
0221 }
0222
0223 return 0;
0224 }
0225
0226 void __init apply_alternatives_all(void)
0227 {
0228
0229 stop_machine(__apply_alternatives_multi_stop, NULL, cpu_online_mask);
0230 }
0231
0232
0233
0234
0235
0236
0237 void __init apply_boot_alternatives(void)
0238 {
0239 struct alt_region region = {
0240 .begin = (struct alt_instr *)__alt_instructions,
0241 .end = (struct alt_instr *)__alt_instructions_end,
0242 };
0243
0244
0245 WARN_ON(smp_processor_id() != 0);
0246
0247 __apply_alternatives(®ion, false, &boot_capabilities[0]);
0248 }
0249
0250 #ifdef CONFIG_MODULES
0251 void apply_alternatives_module(void *start, size_t length)
0252 {
0253 struct alt_region region = {
0254 .begin = start,
0255 .end = start + length,
0256 };
0257 DECLARE_BITMAP(all_capabilities, ARM64_NPATCHABLE);
0258
0259 bitmap_fill(all_capabilities, ARM64_NPATCHABLE);
0260
0261 __apply_alternatives(®ion, true, &all_capabilities[0]);
0262 }
0263 #endif