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 } fields[]; 29 }; 30 31 static const struct ftr_set_desc mmfr1 __initconst = { 32 .name = "id_aa64mmfr1", 33 .override = &id_aa64mmfr1_override, 34 .fields = { 35 { "vh", ID_AA64MMFR1_VHE_SHIFT }, 36 {} 37 }, 38 }; 39 40 static const struct ftr_set_desc pfr1 __initconst = { 41 .name = "id_aa64pfr1", 42 .override = &id_aa64pfr1_override, 43 .fields = { 44 { "bt", ID_AA64PFR1_BT_SHIFT }, 45 {} 46 }, 47 }; 48 49 static const struct ftr_set_desc isar1 __initconst = { 50 .name = "id_aa64isar1", 51 .override = &id_aa64isar1_override, 52 .fields = { 53 { "gpi", ID_AA64ISAR1_GPI_SHIFT }, 54 { "gpa", ID_AA64ISAR1_GPA_SHIFT }, 55 { "api", ID_AA64ISAR1_API_SHIFT }, 56 { "apa", ID_AA64ISAR1_APA_SHIFT }, 57 {} 58 }, 59 }; 60 61 extern struct arm64_ftr_override kaslr_feature_override; 62 63 static const struct ftr_set_desc kaslr __initconst = { 64 .name = "kaslr", 65 #ifdef CONFIG_RANDOMIZE_BASE 66 .override = &kaslr_feature_override, 67 #endif 68 .fields = { 69 { "disabled", 0 }, 70 {} 71 }, 72 }; 73 74 static const struct ftr_set_desc * const regs[] __initconst = { 75 &mmfr1, 76 &pfr1, 77 &isar1, 78 &kaslr, 79 }; 80 81 static const struct { 82 char alias[FTR_ALIAS_NAME_LEN]; 83 char feature[FTR_ALIAS_OPTION_LEN]; 84 } aliases[] __initconst = { 85 { "kvm-arm.mode=nvhe", "id_aa64mmfr1.vh=0" }, 86 { "kvm-arm.mode=protected", "id_aa64mmfr1.vh=0" }, 87 { "arm64.nobti", "id_aa64pfr1.bt=0" }, 88 { "arm64.nopauth", 89 "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 " 90 "id_aa64isar1.api=0 id_aa64isar1.apa=0" }, 91 { "nokaslr", "kaslr.disabled=1" }, 92 }; 93 94 static int __init find_field(const char *cmdline, 95 const struct ftr_set_desc *reg, int f, u64 *v) 96 { 97 char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; 98 int len; 99 100 len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=", 101 reg->name, reg->fields[f].name); 102 103 if (!parameqn(cmdline, opt, len)) 104 return -1; 105 106 return kstrtou64(cmdline + len, 0, v); 107 } 108 109 static void __init match_options(const char *cmdline) 110 { 111 int i; 112 113 for (i = 0; i < ARRAY_SIZE(regs); i++) { 114 int f; 115 116 if (!regs[i]->override) 117 continue; 118 119 for (f = 0; strlen(regs[i]->fields[f].name); f++) { 120 u64 shift = regs[i]->fields[f].shift; 121 u64 mask = 0xfUL << shift; 122 u64 v; 123 124 if (find_field(cmdline, regs[i], f, &v)) 125 continue; 126 127 regs[i]->override->val &= ~mask; 128 regs[i]->override->val |= (v << shift) & mask; 129 regs[i]->override->mask |= mask; 130 131 return; 132 } 133 } 134 } 135 136 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) 137 { 138 do { 139 char buf[256]; 140 size_t len; 141 int i; 142 143 cmdline = skip_spaces(cmdline); 144 145 for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++); 146 if (!len) 147 return; 148 149 len = min(len, ARRAY_SIZE(buf) - 1); 150 strncpy(buf, cmdline, len); 151 buf[len] = 0; 152 153 if (strcmp(buf, "--") == 0) 154 return; 155 156 cmdline += len; 157 158 match_options(buf); 159 160 for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++) 161 if (parameq(buf, aliases[i].alias)) 162 __parse_cmdline(aliases[i].feature, false); 163 } while (1); 164 } 165 166 static __init const u8 *get_bootargs_cmdline(void) 167 { 168 const u8 *prop; 169 void *fdt; 170 int node; 171 172 fdt = get_early_fdt_ptr(); 173 if (!fdt) 174 return NULL; 175 176 node = fdt_path_offset(fdt, "/chosen"); 177 if (node < 0) 178 return NULL; 179 180 prop = fdt_getprop(fdt, node, "bootargs", NULL); 181 if (!prop) 182 return NULL; 183 184 return strlen(prop) ? prop : NULL; 185 } 186 187 static __init void parse_cmdline(void) 188 { 189 const u8 *prop = get_bootargs_cmdline(); 190 191 if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop) 192 __parse_cmdline(CONFIG_CMDLINE, true); 193 194 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop) 195 __parse_cmdline(prop, true); 196 } 197 198 /* Keep checkers quiet */ 199 void init_feature_override(void); 200 201 asmlinkage void __init init_feature_override(void) 202 { 203 int i; 204 205 for (i = 0; i < ARRAY_SIZE(regs); i++) { 206 if (regs[i]->override) { 207 regs[i]->override->val = 0; 208 regs[i]->override->mask = 0; 209 } 210 } 211 212 parse_cmdline(); 213 214 for (i = 0; i < ARRAY_SIZE(regs); i++) { 215 if (regs[i]->override) 216 __flush_dcache_area(regs[i]->override, 217 sizeof(*regs[i]->override)); 218 } 219 } 220