1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Early cpufeature override framework 4 * 5 * Copyright (C) 2020 Google LLC 6 * Author: Marc Zyngier <maz@kernel.org> 7 */ 8 9 #include <linux/ctype.h> 10 #include <linux/kernel.h> 11 #include <linux/libfdt.h> 12 13 #include <asm/cacheflush.h> 14 #include <asm/cpufeature.h> 15 #include <asm/setup.h> 16 17 #define FTR_DESC_NAME_LEN 20 18 #define FTR_DESC_FIELD_LEN 10 19 #define FTR_ALIAS_NAME_LEN 30 20 #define FTR_ALIAS_OPTION_LEN 80 21 22 struct ftr_set_desc { 23 char name[FTR_DESC_NAME_LEN]; 24 struct arm64_ftr_override *override; 25 struct { 26 char name[FTR_DESC_FIELD_LEN]; 27 u8 shift; 28 bool (*filter)(u64 val); 29 } fields[]; 30 }; 31 32 static bool __init mmfr1_vh_filter(u64 val) 33 { 34 /* 35 * If we ever reach this point while running VHE, we're 36 * guaranteed to be on one of these funky, VHE-stuck CPUs. If 37 * the user was trying to force nVHE on us, proceed with 38 * attitude adjustment. 39 */ 40 return !(is_kernel_in_hyp_mode() && val == 0); 41 } 42 43 static const struct ftr_set_desc mmfr1 __initconst = { 44 .name = "id_aa64mmfr1", 45 .override = &id_aa64mmfr1_override, 46 .fields = { 47 { "vh", ID_AA64MMFR1_VHE_SHIFT, mmfr1_vh_filter }, 48 {} 49 }, 50 }; 51 52 static const struct ftr_set_desc pfr1 __initconst = { 53 .name = "id_aa64pfr1", 54 .override = &id_aa64pfr1_override, 55 .fields = { 56 { "bt", ID_AA64PFR1_BT_SHIFT }, 57 { "mte", ID_AA64PFR1_MTE_SHIFT}, 58 {} 59 }, 60 }; 61 62 static const struct ftr_set_desc isar1 __initconst = { 63 .name = "id_aa64isar1", 64 .override = &id_aa64isar1_override, 65 .fields = { 66 { "gpi", ID_AA64ISAR1_GPI_SHIFT }, 67 { "gpa", ID_AA64ISAR1_GPA_SHIFT }, 68 { "api", ID_AA64ISAR1_API_SHIFT }, 69 { "apa", ID_AA64ISAR1_APA_SHIFT }, 70 {} 71 }, 72 }; 73 74 extern struct arm64_ftr_override kaslr_feature_override; 75 76 static const struct ftr_set_desc kaslr __initconst = { 77 .name = "kaslr", 78 #ifdef CONFIG_RANDOMIZE_BASE 79 .override = &kaslr_feature_override, 80 #endif 81 .fields = { 82 { "disabled", 0 }, 83 {} 84 }, 85 }; 86 87 static const struct ftr_set_desc * const regs[] __initconst = { 88 &mmfr1, 89 &pfr1, 90 &isar1, 91 &kaslr, 92 }; 93 94 static const struct { 95 char alias[FTR_ALIAS_NAME_LEN]; 96 char feature[FTR_ALIAS_OPTION_LEN]; 97 } aliases[] __initconst = { 98 { "kvm-arm.mode=nvhe", "id_aa64mmfr1.vh=0" }, 99 { "kvm-arm.mode=protected", "id_aa64mmfr1.vh=0" }, 100 { "arm64.nobti", "id_aa64pfr1.bt=0" }, 101 { "arm64.nopauth", 102 "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 " 103 "id_aa64isar1.api=0 id_aa64isar1.apa=0" }, 104 { "arm64.nomte", "id_aa64pfr1.mte=0" }, 105 { "nokaslr", "kaslr.disabled=1" }, 106 }; 107 108 static int __init find_field(const char *cmdline, 109 const struct ftr_set_desc *reg, int f, u64 *v) 110 { 111 char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; 112 int len; 113 114 len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=", 115 reg->name, reg->fields[f].name); 116 117 if (!parameqn(cmdline, opt, len)) 118 return -1; 119 120 return kstrtou64(cmdline + len, 0, v); 121 } 122 123 static void __init match_options(const char *cmdline) 124 { 125 int i; 126 127 for (i = 0; i < ARRAY_SIZE(regs); i++) { 128 int f; 129 130 if (!regs[i]->override) 131 continue; 132 133 for (f = 0; strlen(regs[i]->fields[f].name); f++) { 134 u64 shift = regs[i]->fields[f].shift; 135 u64 mask = 0xfUL << shift; 136 u64 v; 137 138 if (find_field(cmdline, regs[i], f, &v)) 139 continue; 140 141 /* 142 * If an override gets filtered out, advertise 143 * it by setting the value to 0xf, but 144 * clearing the mask... Yes, this is fragile. 145 */ 146 if (regs[i]->fields[f].filter && 147 !regs[i]->fields[f].filter(v)) { 148 regs[i]->override->val |= mask; 149 regs[i]->override->mask &= ~mask; 150 continue; 151 } 152 153 regs[i]->override->val &= ~mask; 154 regs[i]->override->val |= (v << shift) & mask; 155 regs[i]->override->mask |= mask; 156 157 return; 158 } 159 } 160 } 161 162 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) 163 { 164 do { 165 char buf[256]; 166 size_t len; 167 int i; 168 169 cmdline = skip_spaces(cmdline); 170 171 for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++); 172 if (!len) 173 return; 174 175 len = min(len, ARRAY_SIZE(buf) - 1); 176 strncpy(buf, cmdline, len); 177 buf[len] = 0; 178 179 if (strcmp(buf, "--") == 0) 180 return; 181 182 cmdline += len; 183 184 match_options(buf); 185 186 for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++) 187 if (parameq(buf, aliases[i].alias)) 188 __parse_cmdline(aliases[i].feature, false); 189 } while (1); 190 } 191 192 static __init const u8 *get_bootargs_cmdline(void) 193 { 194 const u8 *prop; 195 void *fdt; 196 int node; 197 198 fdt = get_early_fdt_ptr(); 199 if (!fdt) 200 return NULL; 201 202 node = fdt_path_offset(fdt, "/chosen"); 203 if (node < 0) 204 return NULL; 205 206 prop = fdt_getprop(fdt, node, "bootargs", NULL); 207 if (!prop) 208 return NULL; 209 210 return strlen(prop) ? prop : NULL; 211 } 212 213 static __init void parse_cmdline(void) 214 { 215 const u8 *prop = get_bootargs_cmdline(); 216 217 if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop) 218 __parse_cmdline(CONFIG_CMDLINE, true); 219 220 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop) 221 __parse_cmdline(prop, true); 222 } 223 224 /* Keep checkers quiet */ 225 void init_feature_override(void); 226 227 asmlinkage void __init init_feature_override(void) 228 { 229 int i; 230 231 for (i = 0; i < ARRAY_SIZE(regs); i++) { 232 if (regs[i]->override) { 233 regs[i]->override->val = 0; 234 regs[i]->override->mask = 0; 235 } 236 } 237 238 parse_cmdline(); 239 240 for (i = 0; i < ARRAY_SIZE(regs); i++) { 241 if (regs[i]->override) 242 dcache_clean_inval_poc((unsigned long)regs[i]->override, 243 (unsigned long)regs[i]->override + 244 sizeof(*regs[i]->override)); 245 } 246 } 247