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 		{}
58 	},
59 };
60 
61 static const struct ftr_set_desc isar1 __initconst = {
62 	.name		= "id_aa64isar1",
63 	.override	= &id_aa64isar1_override,
64 	.fields		= {
65 	        { "gpi", ID_AA64ISAR1_GPI_SHIFT },
66 	        { "gpa", ID_AA64ISAR1_GPA_SHIFT },
67 	        { "api", ID_AA64ISAR1_API_SHIFT },
68 	        { "apa", ID_AA64ISAR1_APA_SHIFT },
69 		{}
70 	},
71 };
72 
73 extern struct arm64_ftr_override kaslr_feature_override;
74 
75 static const struct ftr_set_desc kaslr __initconst = {
76 	.name		= "kaslr",
77 #ifdef CONFIG_RANDOMIZE_BASE
78 	.override	= &kaslr_feature_override,
79 #endif
80 	.fields		= {
81 		{ "disabled", 0 },
82 		{}
83 	},
84 };
85 
86 static const struct ftr_set_desc * const regs[] __initconst = {
87 	&mmfr1,
88 	&pfr1,
89 	&isar1,
90 	&kaslr,
91 };
92 
93 static const struct {
94 	char	alias[FTR_ALIAS_NAME_LEN];
95 	char	feature[FTR_ALIAS_OPTION_LEN];
96 } aliases[] __initconst = {
97 	{ "kvm-arm.mode=nvhe",		"id_aa64mmfr1.vh=0" },
98 	{ "kvm-arm.mode=protected",	"id_aa64mmfr1.vh=0" },
99 	{ "arm64.nobti",		"id_aa64pfr1.bt=0" },
100 	{ "arm64.nopauth",
101 	  "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 "
102 	  "id_aa64isar1.api=0 id_aa64isar1.apa=0"	   },
103 	{ "nokaslr",			"kaslr.disabled=1" },
104 };
105 
106 static int __init find_field(const char *cmdline,
107 			     const struct ftr_set_desc *reg, int f, u64 *v)
108 {
109 	char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
110 	int len;
111 
112 	len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
113 		       reg->name, reg->fields[f].name);
114 
115 	if (!parameqn(cmdline, opt, len))
116 		return -1;
117 
118 	return kstrtou64(cmdline + len, 0, v);
119 }
120 
121 static void __init match_options(const char *cmdline)
122 {
123 	int i;
124 
125 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
126 		int f;
127 
128 		if (!regs[i]->override)
129 			continue;
130 
131 		for (f = 0; strlen(regs[i]->fields[f].name); f++) {
132 			u64 shift = regs[i]->fields[f].shift;
133 			u64 mask = 0xfUL << shift;
134 			u64 v;
135 
136 			if (find_field(cmdline, regs[i], f, &v))
137 				continue;
138 
139 			/*
140 			 * If an override gets filtered out, advertise
141 			 * it by setting the value to 0xf, but
142 			 * clearing the mask... Yes, this is fragile.
143 			 */
144 			if (regs[i]->fields[f].filter &&
145 			    !regs[i]->fields[f].filter(v)) {
146 				regs[i]->override->val  |= mask;
147 				regs[i]->override->mask &= ~mask;
148 				continue;
149 			}
150 
151 			regs[i]->override->val  &= ~mask;
152 			regs[i]->override->val  |= (v << shift) & mask;
153 			regs[i]->override->mask |= mask;
154 
155 			return;
156 		}
157 	}
158 }
159 
160 static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
161 {
162 	do {
163 		char buf[256];
164 		size_t len;
165 		int i;
166 
167 		cmdline = skip_spaces(cmdline);
168 
169 		for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
170 		if (!len)
171 			return;
172 
173 		len = min(len, ARRAY_SIZE(buf) - 1);
174 		strncpy(buf, cmdline, len);
175 		buf[len] = 0;
176 
177 		if (strcmp(buf, "--") == 0)
178 			return;
179 
180 		cmdline += len;
181 
182 		match_options(buf);
183 
184 		for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++)
185 			if (parameq(buf, aliases[i].alias))
186 				__parse_cmdline(aliases[i].feature, false);
187 	} while (1);
188 }
189 
190 static __init const u8 *get_bootargs_cmdline(void)
191 {
192 	const u8 *prop;
193 	void *fdt;
194 	int node;
195 
196 	fdt = get_early_fdt_ptr();
197 	if (!fdt)
198 		return NULL;
199 
200 	node = fdt_path_offset(fdt, "/chosen");
201 	if (node < 0)
202 		return NULL;
203 
204 	prop = fdt_getprop(fdt, node, "bootargs", NULL);
205 	if (!prop)
206 		return NULL;
207 
208 	return strlen(prop) ? prop : NULL;
209 }
210 
211 static __init void parse_cmdline(void)
212 {
213 	const u8 *prop = get_bootargs_cmdline();
214 
215 	if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !prop)
216 		__parse_cmdline(CONFIG_CMDLINE, true);
217 
218 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && prop)
219 		__parse_cmdline(prop, true);
220 }
221 
222 /* Keep checkers quiet */
223 void init_feature_override(void);
224 
225 asmlinkage void __init init_feature_override(void)
226 {
227 	int i;
228 
229 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
230 		if (regs[i]->override) {
231 			regs[i]->override->val  = 0;
232 			regs[i]->override->mask = 0;
233 		}
234 	}
235 
236 	parse_cmdline();
237 
238 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
239 		if (regs[i]->override)
240 			dcache_clean_inval_poc((unsigned long)regs[i]->override,
241 					    (unsigned long)regs[i]->override +
242 					    sizeof(*regs[i]->override));
243 	}
244 }
245