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	116
21 
22 static u64 __boot_status __initdata;
23 
24 struct ftr_set_desc {
25 	char 				name[FTR_DESC_NAME_LEN];
26 	struct arm64_ftr_override	*override;
27 	struct {
28 		char			name[FTR_DESC_FIELD_LEN];
29 		u8			shift;
30 		u8			width;
31 		bool			(*filter)(u64 val);
32 	} 				fields[];
33 };
34 
35 #define FIELD(n, s, f)	{ .name = n, .shift = s, .width = 4, .filter = f }
36 
37 static bool __init mmfr1_vh_filter(u64 val)
38 {
39 	/*
40 	 * If we ever reach this point while running VHE, we're
41 	 * guaranteed to be on one of these funky, VHE-stuck CPUs. If
42 	 * the user was trying to force nVHE on us, proceed with
43 	 * attitude adjustment.
44 	 */
45 	return !(__boot_status == (BOOT_CPU_FLAG_E2H | BOOT_CPU_MODE_EL2) &&
46 		 val == 0);
47 }
48 
49 static const struct ftr_set_desc mmfr1 __initconst = {
50 	.name		= "id_aa64mmfr1",
51 	.override	= &id_aa64mmfr1_override,
52 	.fields		= {
53 		FIELD("vh", ID_AA64MMFR1_EL1_VH_SHIFT, mmfr1_vh_filter),
54 		{}
55 	},
56 };
57 
58 static bool __init pfr0_sve_filter(u64 val)
59 {
60 	/*
61 	 * Disabling SVE also means disabling all the features that
62 	 * are associated with it. The easiest way to do it is just to
63 	 * override id_aa64zfr0_el1 to be 0.
64 	 */
65 	if (!val) {
66 		id_aa64zfr0_override.val = 0;
67 		id_aa64zfr0_override.mask = GENMASK(63, 0);
68 	}
69 
70 	return true;
71 }
72 
73 static const struct ftr_set_desc pfr0 __initconst = {
74 	.name		= "id_aa64pfr0",
75 	.override	= &id_aa64pfr0_override,
76 	.fields		= {
77 	        FIELD("sve", ID_AA64PFR0_EL1_SVE_SHIFT, pfr0_sve_filter),
78 		{}
79 	},
80 };
81 
82 static bool __init pfr1_sme_filter(u64 val)
83 {
84 	/*
85 	 * Similarly to SVE, disabling SME also means disabling all
86 	 * the features that are associated with it. Just set
87 	 * id_aa64smfr0_el1 to 0 and don't look back.
88 	 */
89 	if (!val) {
90 		id_aa64smfr0_override.val = 0;
91 		id_aa64smfr0_override.mask = GENMASK(63, 0);
92 	}
93 
94 	return true;
95 }
96 
97 static const struct ftr_set_desc pfr1 __initconst = {
98 	.name		= "id_aa64pfr1",
99 	.override	= &id_aa64pfr1_override,
100 	.fields		= {
101 		FIELD("bt", ID_AA64PFR1_EL1_BT_SHIFT, NULL ),
102 		FIELD("mte", ID_AA64PFR1_EL1_MTE_SHIFT, NULL),
103 		FIELD("sme", ID_AA64PFR1_EL1_SME_SHIFT, pfr1_sme_filter),
104 		{}
105 	},
106 };
107 
108 static const struct ftr_set_desc isar1 __initconst = {
109 	.name		= "id_aa64isar1",
110 	.override	= &id_aa64isar1_override,
111 	.fields		= {
112 		FIELD("gpi", ID_AA64ISAR1_EL1_GPI_SHIFT, NULL),
113 		FIELD("gpa", ID_AA64ISAR1_EL1_GPA_SHIFT, NULL),
114 		FIELD("api", ID_AA64ISAR1_EL1_API_SHIFT, NULL),
115 		FIELD("apa", ID_AA64ISAR1_EL1_APA_SHIFT, NULL),
116 		{}
117 	},
118 };
119 
120 static const struct ftr_set_desc isar2 __initconst = {
121 	.name		= "id_aa64isar2",
122 	.override	= &id_aa64isar2_override,
123 	.fields		= {
124 		FIELD("gpa3", ID_AA64ISAR2_EL1_GPA3_SHIFT, NULL),
125 		FIELD("apa3", ID_AA64ISAR2_EL1_APA3_SHIFT, NULL),
126 		FIELD("mops", ID_AA64ISAR2_EL1_MOPS_SHIFT, NULL),
127 		{}
128 	},
129 };
130 
131 static const struct ftr_set_desc smfr0 __initconst = {
132 	.name		= "id_aa64smfr0",
133 	.override	= &id_aa64smfr0_override,
134 	.fields		= {
135 		FIELD("smever", ID_AA64SMFR0_EL1_SMEver_SHIFT, NULL),
136 		/* FA64 is a one bit field... :-/ */
137 		{ "fa64", ID_AA64SMFR0_EL1_FA64_SHIFT, 1, },
138 		{}
139 	},
140 };
141 
142 extern struct arm64_ftr_override kaslr_feature_override;
143 
144 static const struct ftr_set_desc kaslr __initconst = {
145 	.name		= "kaslr",
146 #ifdef CONFIG_RANDOMIZE_BASE
147 	.override	= &kaslr_feature_override,
148 #endif
149 	.fields		= {
150 		FIELD("disabled", 0, NULL),
151 		{}
152 	},
153 };
154 
155 static const struct ftr_set_desc * const regs[] __initconst = {
156 	&mmfr1,
157 	&pfr0,
158 	&pfr1,
159 	&isar1,
160 	&isar2,
161 	&smfr0,
162 	&kaslr,
163 };
164 
165 static const struct {
166 	char	alias[FTR_ALIAS_NAME_LEN];
167 	char	feature[FTR_ALIAS_OPTION_LEN];
168 } aliases[] __initconst = {
169 	{ "kvm-arm.mode=nvhe",		"id_aa64mmfr1.vh=0" },
170 	{ "kvm-arm.mode=protected",	"id_aa64mmfr1.vh=0" },
171 	{ "arm64.nosve",		"id_aa64pfr0.sve=0" },
172 	{ "arm64.nosme",		"id_aa64pfr1.sme=0" },
173 	{ "arm64.nobti",		"id_aa64pfr1.bt=0" },
174 	{ "arm64.nopauth",
175 	  "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 "
176 	  "id_aa64isar1.api=0 id_aa64isar1.apa=0 "
177 	  "id_aa64isar2.gpa3=0 id_aa64isar2.apa3=0"	   },
178 	{ "arm64.nomops",		"id_aa64isar2.mops=0" },
179 	{ "arm64.nomte",		"id_aa64pfr1.mte=0" },
180 	{ "nokaslr",			"kaslr.disabled=1" },
181 };
182 
183 static int __init parse_nokaslr(char *unused)
184 {
185 	/* nokaslr param handling is done by early cpufeature code */
186 	return 0;
187 }
188 early_param("nokaslr", parse_nokaslr);
189 
190 static int __init find_field(const char *cmdline,
191 			     const struct ftr_set_desc *reg, int f, u64 *v)
192 {
193 	char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
194 	int len;
195 
196 	len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
197 		       reg->name, reg->fields[f].name);
198 
199 	if (!parameqn(cmdline, opt, len))
200 		return -1;
201 
202 	return kstrtou64(cmdline + len, 0, v);
203 }
204 
205 static void __init match_options(const char *cmdline)
206 {
207 	int i;
208 
209 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
210 		int f;
211 
212 		if (!regs[i]->override)
213 			continue;
214 
215 		for (f = 0; strlen(regs[i]->fields[f].name); f++) {
216 			u64 shift = regs[i]->fields[f].shift;
217 			u64 width = regs[i]->fields[f].width ?: 4;
218 			u64 mask = GENMASK_ULL(shift + width - 1, shift);
219 			u64 v;
220 
221 			if (find_field(cmdline, regs[i], f, &v))
222 				continue;
223 
224 			/*
225 			 * If an override gets filtered out, advertise
226 			 * it by setting the value to the all-ones while
227 			 * clearing the mask... Yes, this is fragile.
228 			 */
229 			if (regs[i]->fields[f].filter &&
230 			    !regs[i]->fields[f].filter(v)) {
231 				regs[i]->override->val  |= mask;
232 				regs[i]->override->mask &= ~mask;
233 				continue;
234 			}
235 
236 			regs[i]->override->val  &= ~mask;
237 			regs[i]->override->val  |= (v << shift) & mask;
238 			regs[i]->override->mask |= mask;
239 
240 			return;
241 		}
242 	}
243 }
244 
245 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
246 {
247 	do {
248 		char buf[256];
249 		size_t len;
250 		int i;
251 
252 		cmdline = skip_spaces(cmdline);
253 
254 		for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
255 		if (!len)
256 			return;
257 
258 		len = min(len, ARRAY_SIZE(buf) - 1);
259 		strncpy(buf, cmdline, len);
260 		buf[len] = 0;
261 
262 		if (strcmp(buf, "--") == 0)
263 			return;
264 
265 		cmdline += len;
266 
267 		match_options(buf);
268 
269 		for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++)
270 			if (parameq(buf, aliases[i].alias))
271 				__parse_cmdline(aliases[i].feature, false);
272 	} while (1);
273 }
274 
275 static __init const u8 *get_bootargs_cmdline(void)
276 {
277 	const u8 *prop;
278 	void *fdt;
279 	int node;
280 
281 	fdt = get_early_fdt_ptr();
282 	if (!fdt)
283 		return NULL;
284 
285 	node = fdt_path_offset(fdt, "/chosen");
286 	if (node < 0)
287 		return NULL;
288 
289 	prop = fdt_getprop(fdt, node, "bootargs", NULL);
290 	if (!prop)
291 		return NULL;
292 
293 	return strlen(prop) ? prop : NULL;
294 }
295 
296 static __init void parse_cmdline(void)
297 {
298 	const u8 *prop = get_bootargs_cmdline();
299 
300 	if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop)
301 		__parse_cmdline(CONFIG_CMDLINE, true);
302 
303 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop)
304 		__parse_cmdline(prop, true);
305 }
306 
307 /* Keep checkers quiet */
308 void init_feature_override(u64 boot_status);
309 
310 asmlinkage void __init init_feature_override(u64 boot_status)
311 {
312 	int i;
313 
314 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
315 		if (regs[i]->override) {
316 			regs[i]->override->val  = 0;
317 			regs[i]->override->mask = 0;
318 		}
319 	}
320 
321 	__boot_status = boot_status;
322 
323 	parse_cmdline();
324 
325 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
326 		if (regs[i]->override)
327 			dcache_clean_inval_poc((unsigned long)regs[i]->override,
328 					    (unsigned long)regs[i]->override +
329 					    sizeof(*regs[i]->override));
330 	}
331 }
332