1 // SPDX-License-Identifier: GPL-2.0
3 * Early cpufeature override framework
5 * Copyright (C) 2020 Google LLC
6 * Author: Marc Zyngier <maz@kernel.org>
9 #include <linux/ctype.h>
10 #include <linux/kernel.h>
11 #include <linux/libfdt.h>
13 #include <asm/cacheflush.h>
14 #include <asm/cpufeature.h>
15 #include <asm/setup.h>
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
23 char name[FTR_DESC_NAME_LEN];
24 struct arm64_ftr_override *override;
26 char name[FTR_DESC_FIELD_LEN];
28 bool (*filter)(u64 val);
32 static bool __init mmfr1_vh_filter(u64 val)
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.
40 return !(is_kernel_in_hyp_mode() && val == 0);
43 static const struct ftr_set_desc mmfr1 __initconst = {
44 .name = "id_aa64mmfr1",
45 .override = &id_aa64mmfr1_override,
47 { "vh", ID_AA64MMFR1_VHE_SHIFT, mmfr1_vh_filter },
52 static const struct ftr_set_desc pfr1 __initconst = {
53 .name = "id_aa64pfr1",
54 .override = &id_aa64pfr1_override,
56 { "bt", ID_AA64PFR1_BT_SHIFT },
57 { "mte", ID_AA64PFR1_MTE_SHIFT},
62 static const struct ftr_set_desc isar1 __initconst = {
63 .name = "id_aa64isar1",
64 .override = &id_aa64isar1_override,
66 { "gpi", ID_AA64ISAR1_GPI_SHIFT },
67 { "gpa", ID_AA64ISAR1_GPA_SHIFT },
68 { "api", ID_AA64ISAR1_API_SHIFT },
69 { "apa", ID_AA64ISAR1_APA_SHIFT },
74 extern struct arm64_ftr_override kaslr_feature_override;
76 static const struct ftr_set_desc kaslr __initconst = {
78 #ifdef CONFIG_RANDOMIZE_BASE
79 .override = &kaslr_feature_override,
87 static const struct ftr_set_desc * const regs[] __initconst = {
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" },
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" },
108 static int __init find_field(const char *cmdline,
109 const struct ftr_set_desc *reg, int f, u64 *v)
111 char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
114 len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
115 reg->name, reg->fields[f].name);
117 if (!parameqn(cmdline, opt, len))
120 return kstrtou64(cmdline + len, 0, v);
123 static void __init match_options(const char *cmdline)
127 for (i = 0; i < ARRAY_SIZE(regs); i++) {
130 if (!regs[i]->override)
133 for (f = 0; strlen(regs[i]->fields[f].name); f++) {
134 u64 shift = regs[i]->fields[f].shift;
135 u64 mask = 0xfUL << shift;
138 if (find_field(cmdline, regs[i], f, &v))
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.
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;
153 regs[i]->override->val &= ~mask;
154 regs[i]->override->val |= (v << shift) & mask;
155 regs[i]->override->mask |= mask;
162 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
169 cmdline = skip_spaces(cmdline);
171 for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
175 len = min(len, ARRAY_SIZE(buf) - 1);
176 strncpy(buf, cmdline, len);
179 if (strcmp(buf, "--") == 0)
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);
192 static __init const u8 *get_bootargs_cmdline(void)
198 fdt = get_early_fdt_ptr();
202 node = fdt_path_offset(fdt, "/chosen");
206 prop = fdt_getprop(fdt, node, "bootargs", NULL);
210 return strlen(prop) ? prop : NULL;
213 static __init void parse_cmdline(void)
215 const u8 *prop = get_bootargs_cmdline();
217 if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop)
218 __parse_cmdline(CONFIG_CMDLINE, true);
220 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop)
221 __parse_cmdline(prop, true);
224 /* Keep checkers quiet */
225 void init_feature_override(void);
227 asmlinkage void __init init_feature_override(void)
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;
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));