blob: e1c9cf079503e31d4a58ed55af6eae64ee0af04d [file] [log] [blame]
Michael Ellerman9a868f62018-03-27 23:01:44 +11001// SPDX-License-Identifier: GPL-2.0+
2//
3// Security related flags and so on.
4//
5// Copyright 2018, Michael Ellerman, IBM Corporation.
6
Breno Leitao42e2acd2018-10-22 11:54:12 -03007#include <linux/cpu.h>
Michael Ellerman9a868f62018-03-27 23:01:44 +11008#include <linux/kernel.h>
Michael Ellerman8ad33042018-03-27 23:01:48 +11009#include <linux/device.h>
Michael Ellermanff348352018-03-27 23:01:49 +110010#include <linux/seq_buf.h>
Michael Ellerman8ad33042018-03-27 23:01:48 +110011
Michael Ellermanee13cb242018-07-24 01:07:54 +100012#include <asm/asm-prototypes.h>
13#include <asm/code-patching.h>
Nicholas Piggina048a072018-05-22 09:00:00 +100014#include <asm/debugfs.h>
Michael Ellerman9a868f62018-03-27 23:01:44 +110015#include <asm/security_features.h>
Michal Suchanek2eea7f02018-04-24 14:15:55 +100016#include <asm/setup.h>
Michael Ellerman9a868f62018-03-27 23:01:44 +110017
18
Mauricio Faria de Oliveirae7347a82018-03-30 14:28:24 -030019unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
Michael Ellerman8ad33042018-03-27 23:01:48 +110020
Michael Ellermanee13cb242018-07-24 01:07:54 +100021enum count_cache_flush_type {
22 COUNT_CACHE_FLUSH_NONE = 0x1,
23 COUNT_CACHE_FLUSH_SW = 0x2,
24 COUNT_CACHE_FLUSH_HW = 0x4,
25};
Diana Craciun7d8bad99b2018-12-12 16:03:02 +020026static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
Michael Ellermanee13cb242018-07-24 01:07:54 +100027
Michal Suchanek815069c2018-04-24 14:15:56 +100028bool barrier_nospec_enabled;
Diana Craciuncf175dc2018-07-28 09:06:32 +100029static bool no_nospec;
Diana Craciunf633a8a2018-12-12 16:03:04 +020030static bool btb_flush_enabled;
31#ifdef CONFIG_PPC_FSL_BOOK3E
32static bool no_spectrev2;
33#endif
Michal Suchanek2eea7f02018-04-24 14:15:55 +100034
35static void enable_barrier_nospec(bool enable)
36{
37 barrier_nospec_enabled = enable;
38 do_barrier_nospec_fixups(enable);
39}
40
Michal Suchanekcb3d6752018-04-24 14:15:57 +100041void setup_barrier_nospec(void)
42{
43 bool enable;
44
45 /*
46 * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
47 * But there's a good reason not to. The two flags we check below are
48 * both are enabled by default in the kernel, so if the hcall is not
49 * functional they will be enabled.
50 * On a system where the host firmware has been updated (so the ori
51 * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
52 * not been updated, we would like to enable the barrier. Dropping the
53 * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
54 * we potentially enable the barrier on systems where the host firmware
55 * is not updated, but that's harmless as it's a no-op.
56 */
57 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
58 security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
59
Josh Poimboeuf782e69e2019-04-12 15:39:30 -050060 if (!no_nospec && !cpu_mitigations_off())
Diana Craciuncf175dc2018-07-28 09:06:32 +100061 enable_barrier_nospec(enable);
Michal Suchanekcb3d6752018-04-24 14:15:57 +100062}
63
Diana Craciuncf175dc2018-07-28 09:06:32 +100064static int __init handle_nospectre_v1(char *p)
65{
66 no_nospec = true;
67
68 return 0;
69}
70early_param("nospectre_v1", handle_nospectre_v1);
71
Michal Suchanekcb3d6752018-04-24 14:15:57 +100072#ifdef CONFIG_DEBUG_FS
73static int barrier_nospec_set(void *data, u64 val)
74{
75 switch (val) {
76 case 0:
77 case 1:
78 break;
79 default:
80 return -EINVAL;
81 }
82
83 if (!!val == !!barrier_nospec_enabled)
84 return 0;
85
86 enable_barrier_nospec(!!val);
87
88 return 0;
89}
90
91static int barrier_nospec_get(void *data, u64 *val)
92{
93 *val = barrier_nospec_enabled ? 1 : 0;
94 return 0;
95}
96
97DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec,
98 barrier_nospec_get, barrier_nospec_set, "%llu\n");
99
100static __init int barrier_nospec_debugfs_init(void)
101{
102 debugfs_create_file("barrier_nospec", 0600, powerpc_debugfs_root, NULL,
103 &fops_barrier_nospec);
104 return 0;
105}
106device_initcall(barrier_nospec_debugfs_init);
Michael Ellerman398af572019-04-09 23:14:20 +1000107
108static __init int security_feature_debugfs_init(void)
109{
110 debugfs_create_x64("security_features", 0400, powerpc_debugfs_root,
111 (u64 *)&powerpc_security_features);
112 return 0;
113}
114device_initcall(security_feature_debugfs_init);
Michal Suchanekcb3d6752018-04-24 14:15:57 +1000115#endif /* CONFIG_DEBUG_FS */
116
Diana Craciunf633a8a2018-12-12 16:03:04 +0200117#ifdef CONFIG_PPC_FSL_BOOK3E
118static int __init handle_nospectre_v2(char *p)
119{
120 no_spectrev2 = true;
121
122 return 0;
123}
124early_param("nospectre_v2", handle_nospectre_v2);
125void setup_spectre_v2(void)
126{
Josh Poimboeuf782e69e2019-04-12 15:39:30 -0500127 if (no_spectrev2 || cpu_mitigations_off())
Diana Craciunf633a8a2018-12-12 16:03:04 +0200128 do_btb_flush_fixups();
129 else
130 btb_flush_enabled = true;
131}
132#endif /* CONFIG_PPC_FSL_BOOK3E */
133
Diana Craciun406d2b62018-07-28 09:06:36 +1000134#ifdef CONFIG_PPC_BOOK3S_64
Michael Ellerman8ad33042018-03-27 23:01:48 +1100135ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
136{
Michael Ellermanff348352018-03-27 23:01:49 +1100137 bool thread_priv;
138
139 thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
140
141 if (rfi_flush || thread_priv) {
142 struct seq_buf s;
143 seq_buf_init(&s, buf, PAGE_SIZE - 1);
144
145 seq_buf_printf(&s, "Mitigation: ");
146
147 if (rfi_flush)
148 seq_buf_printf(&s, "RFI Flush");
149
150 if (rfi_flush && thread_priv)
151 seq_buf_printf(&s, ", ");
152
153 if (thread_priv)
154 seq_buf_printf(&s, "L1D private per thread");
155
156 seq_buf_printf(&s, "\n");
157
158 return s.len;
159 }
160
161 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
162 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
163 return sprintf(buf, "Not affected\n");
Michael Ellerman8ad33042018-03-27 23:01:48 +1100164
165 return sprintf(buf, "Vulnerable\n");
166}
Diana Craciun406d2b62018-07-28 09:06:36 +1000167#endif
Michael Ellerman56986012018-03-27 23:01:52 +1100168
169ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
170{
Michael Ellerman6d44acae2018-07-09 16:25:21 +1000171 struct seq_buf s;
Michael Ellerman56986012018-03-27 23:01:52 +1100172
Michael Ellerman6d44acae2018-07-09 16:25:21 +1000173 seq_buf_init(&s, buf, PAGE_SIZE - 1);
Michal Suchaneka3775142018-05-28 15:19:14 +0200174
Michael Ellerman6d44acae2018-07-09 16:25:21 +1000175 if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
176 if (barrier_nospec_enabled)
177 seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
178 else
179 seq_buf_printf(&s, "Vulnerable");
180
181 if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
182 seq_buf_printf(&s, ", ori31 speculation barrier enabled");
183
184 seq_buf_printf(&s, "\n");
185 } else
186 seq_buf_printf(&s, "Not affected\n");
187
188 return s.len;
Michael Ellerman56986012018-03-27 23:01:52 +1100189}
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100190
191ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
192{
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100193 struct seq_buf s;
Michael Ellerman6d44acae2018-07-09 16:25:21 +1000194 bool bcs, ccd;
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100195
196 seq_buf_init(&s, buf, PAGE_SIZE - 1);
197
198 bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
199 ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100200
Michael Ellerman92edf8d2019-03-21 15:24:33 +1100201 if (bcs || ccd) {
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100202 seq_buf_printf(&s, "Mitigation: ");
203
Michael Ellerman92edf8d2019-03-21 15:24:33 +1100204 if (bcs)
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100205 seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
206
Michael Ellerman92edf8d2019-03-21 15:24:33 +1100207 if (bcs && ccd)
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100208 seq_buf_printf(&s, ", ");
209
Michael Ellerman92edf8d2019-03-21 15:24:33 +1100210 if (ccd)
211 seq_buf_printf(&s, "Indirect branch cache disabled");
212 } else if (count_cache_flush_type != COUNT_CACHE_FLUSH_NONE) {
213 seq_buf_printf(&s, "Mitigation: Software count cache flush");
Michael Ellermanee13cb242018-07-24 01:07:54 +1000214
215 if (count_cache_flush_type == COUNT_CACHE_FLUSH_HW)
Michael Ellerman92edf8d2019-03-21 15:24:33 +1100216 seq_buf_printf(&s, " (hardware accelerated)");
Diana Craciundfa88652018-12-12 16:03:09 +0200217 } else if (btb_flush_enabled) {
218 seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
219 } else {
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100220 seq_buf_printf(&s, "Vulnerable");
Diana Craciundfa88652018-12-12 16:03:09 +0200221 }
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100222
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100223 seq_buf_printf(&s, "\n");
224
225 return s.len;
226}
Nicholas Piggina048a072018-05-22 09:00:00 +1000227
Diana Craciun6453b532018-07-28 09:06:33 +1000228#ifdef CONFIG_PPC_BOOK3S_64
Nicholas Piggina048a072018-05-22 09:00:00 +1000229/*
230 * Store-forwarding barrier support.
231 */
232
233static enum stf_barrier_type stf_enabled_flush_types;
234static bool no_stf_barrier;
235bool stf_barrier;
236
237static int __init handle_no_stf_barrier(char *p)
238{
239 pr_info("stf-barrier: disabled on command line.");
240 no_stf_barrier = true;
241 return 0;
242}
243
244early_param("no_stf_barrier", handle_no_stf_barrier);
245
246/* This is the generic flag used by other architectures */
247static int __init handle_ssbd(char *p)
248{
249 if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
250 /* Until firmware tells us, we have the barrier with auto */
251 return 0;
252 } else if (strncmp(p, "off", 3) == 0) {
253 handle_no_stf_barrier(NULL);
254 return 0;
255 } else
256 return 1;
257
258 return 0;
259}
260early_param("spec_store_bypass_disable", handle_ssbd);
261
262/* This is the generic flag used by other architectures */
263static int __init handle_no_ssbd(char *p)
264{
265 handle_no_stf_barrier(NULL);
266 return 0;
267}
268early_param("nospec_store_bypass_disable", handle_no_ssbd);
269
270static void stf_barrier_enable(bool enable)
271{
272 if (enable)
273 do_stf_barrier_fixups(stf_enabled_flush_types);
274 else
275 do_stf_barrier_fixups(STF_BARRIER_NONE);
276
277 stf_barrier = enable;
278}
279
280void setup_stf_barrier(void)
281{
282 enum stf_barrier_type type;
283 bool enable, hv;
284
285 hv = cpu_has_feature(CPU_FTR_HVMODE);
286
287 /* Default to fallback in case fw-features are not available */
288 if (cpu_has_feature(CPU_FTR_ARCH_300))
289 type = STF_BARRIER_EIEIO;
290 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
291 type = STF_BARRIER_SYNC_ORI;
292 else if (cpu_has_feature(CPU_FTR_ARCH_206))
293 type = STF_BARRIER_FALLBACK;
294 else
295 type = STF_BARRIER_NONE;
296
297 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
298 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
299 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
300
301 if (type == STF_BARRIER_FALLBACK) {
302 pr_info("stf-barrier: fallback barrier available\n");
303 } else if (type == STF_BARRIER_SYNC_ORI) {
304 pr_info("stf-barrier: hwsync barrier available\n");
305 } else if (type == STF_BARRIER_EIEIO) {
306 pr_info("stf-barrier: eieio barrier available\n");
307 }
308
309 stf_enabled_flush_types = type;
310
Josh Poimboeuf782e69e2019-04-12 15:39:30 -0500311 if (!no_stf_barrier && !cpu_mitigations_off())
Nicholas Piggina048a072018-05-22 09:00:00 +1000312 stf_barrier_enable(enable);
313}
314
315ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
316{
317 if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
318 const char *type;
319 switch (stf_enabled_flush_types) {
320 case STF_BARRIER_EIEIO:
321 type = "eieio";
322 break;
323 case STF_BARRIER_SYNC_ORI:
324 type = "hwsync";
325 break;
326 case STF_BARRIER_FALLBACK:
327 type = "fallback";
328 break;
329 default:
330 type = "unknown";
331 }
332 return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
333 }
334
335 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
336 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
337 return sprintf(buf, "Not affected\n");
338
339 return sprintf(buf, "Vulnerable\n");
340}
341
342#ifdef CONFIG_DEBUG_FS
343static int stf_barrier_set(void *data, u64 val)
344{
345 bool enable;
346
347 if (val == 1)
348 enable = true;
349 else if (val == 0)
350 enable = false;
351 else
352 return -EINVAL;
353
354 /* Only do anything if we're changing state */
355 if (enable != stf_barrier)
356 stf_barrier_enable(enable);
357
358 return 0;
359}
360
361static int stf_barrier_get(void *data, u64 *val)
362{
363 *val = stf_barrier ? 1 : 0;
364 return 0;
365}
366
367DEFINE_SIMPLE_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set, "%llu\n");
368
369static __init int stf_barrier_debugfs_init(void)
370{
371 debugfs_create_file("stf_barrier", 0600, powerpc_debugfs_root, NULL, &fops_stf_barrier);
372 return 0;
373}
374device_initcall(stf_barrier_debugfs_init);
375#endif /* CONFIG_DEBUG_FS */
Michael Ellermanee13cb242018-07-24 01:07:54 +1000376
377static void toggle_count_cache_flush(bool enable)
378{
379 if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
380 patch_instruction_site(&patch__call_flush_count_cache, PPC_INST_NOP);
381 count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
382 pr_info("count-cache-flush: software flush disabled.\n");
383 return;
384 }
385
386 patch_branch_site(&patch__call_flush_count_cache,
387 (u64)&flush_count_cache, BRANCH_SET_LINK);
388
389 if (!security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
390 count_cache_flush_type = COUNT_CACHE_FLUSH_SW;
391 pr_info("count-cache-flush: full software flush sequence enabled.\n");
392 return;
393 }
394
395 patch_instruction_site(&patch__flush_count_cache_return, PPC_INST_BLR);
396 count_cache_flush_type = COUNT_CACHE_FLUSH_HW;
397 pr_info("count-cache-flush: hardware assisted flush sequence enabled\n");
398}
399
400void setup_count_cache_flush(void)
401{
402 toggle_count_cache_flush(true);
403}
404
405#ifdef CONFIG_DEBUG_FS
406static int count_cache_flush_set(void *data, u64 val)
407{
408 bool enable;
409
410 if (val == 1)
411 enable = true;
412 else if (val == 0)
413 enable = false;
414 else
415 return -EINVAL;
416
417 toggle_count_cache_flush(enable);
418
419 return 0;
420}
421
422static int count_cache_flush_get(void *data, u64 *val)
423{
424 if (count_cache_flush_type == COUNT_CACHE_FLUSH_NONE)
425 *val = 0;
426 else
427 *val = 1;
428
429 return 0;
430}
431
432DEFINE_SIMPLE_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
433 count_cache_flush_set, "%llu\n");
434
435static __init int count_cache_flush_debugfs_init(void)
436{
437 debugfs_create_file("count_cache_flush", 0600, powerpc_debugfs_root,
438 NULL, &fops_count_cache_flush);
439 return 0;
440}
441device_initcall(count_cache_flush_debugfs_init);
442#endif /* CONFIG_DEBUG_FS */
Diana Craciun6453b532018-07-28 09:06:33 +1000443#endif /* CONFIG_PPC_BOOK3S_64 */