1 // SPDX-License-Identifier: GPL-2.0+
3 // Security related flags and so on.
5 // Copyright 2018, Michael Ellerman, IBM Corporation.
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/seq_buf.h>
12 #include <asm/asm-prototypes.h>
13 #include <asm/code-patching.h>
14 #include <asm/debugfs.h>
15 #include <asm/security_features.h>
16 #include <asm/setup.h>
19 unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
21 enum count_cache_flush_type {
22 COUNT_CACHE_FLUSH_NONE = 0x1,
23 COUNT_CACHE_FLUSH_SW = 0x2,
24 COUNT_CACHE_FLUSH_HW = 0x4,
26 static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
28 bool barrier_nospec_enabled;
29 static bool no_nospec;
30 static bool btb_flush_enabled;
31 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
32 static bool no_spectrev2;
35 static void enable_barrier_nospec(bool enable)
37 barrier_nospec_enabled = enable;
38 do_barrier_nospec_fixups(enable);
41 void setup_barrier_nospec(void)
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.
57 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
58 security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
60 if (!no_nospec && !cpu_mitigations_off())
61 enable_barrier_nospec(enable);
64 static int __init handle_nospectre_v1(char *p)
70 early_param("nospectre_v1", handle_nospectre_v1);
72 #ifdef CONFIG_DEBUG_FS
73 static int barrier_nospec_set(void *data, u64 val)
83 if (!!val == !!barrier_nospec_enabled)
86 enable_barrier_nospec(!!val);
91 static int barrier_nospec_get(void *data, u64 *val)
93 *val = barrier_nospec_enabled ? 1 : 0;
97 DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec,
98 barrier_nospec_get, barrier_nospec_set, "%llu\n");
100 static __init int barrier_nospec_debugfs_init(void)
102 debugfs_create_file("barrier_nospec", 0600, powerpc_debugfs_root, NULL,
103 &fops_barrier_nospec);
106 device_initcall(barrier_nospec_debugfs_init);
108 static __init int security_feature_debugfs_init(void)
110 debugfs_create_x64("security_features", 0400, powerpc_debugfs_root,
111 (u64 *)&powerpc_security_features);
114 device_initcall(security_feature_debugfs_init);
115 #endif /* CONFIG_DEBUG_FS */
117 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
118 static int __init handle_nospectre_v2(char *p)
124 early_param("nospectre_v2", handle_nospectre_v2);
125 #endif /* CONFIG_PPC_FSL_BOOK3E || CONFIG_PPC_BOOK3S_64 */
127 #ifdef CONFIG_PPC_FSL_BOOK3E
128 void setup_spectre_v2(void)
130 if (no_spectrev2 || cpu_mitigations_off())
131 do_btb_flush_fixups();
133 btb_flush_enabled = true;
135 #endif /* CONFIG_PPC_FSL_BOOK3E */
137 #ifdef CONFIG_PPC_BOOK3S_64
138 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
142 thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
144 if (rfi_flush || thread_priv) {
146 seq_buf_init(&s, buf, PAGE_SIZE - 1);
148 seq_buf_printf(&s, "Mitigation: ");
151 seq_buf_printf(&s, "RFI Flush");
153 if (rfi_flush && thread_priv)
154 seq_buf_printf(&s, ", ");
157 seq_buf_printf(&s, "L1D private per thread");
159 seq_buf_printf(&s, "\n");
164 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
165 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
166 return sprintf(buf, "Not affected\n");
168 return sprintf(buf, "Vulnerable\n");
172 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
176 seq_buf_init(&s, buf, PAGE_SIZE - 1);
178 if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
179 if (barrier_nospec_enabled)
180 seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
182 seq_buf_printf(&s, "Vulnerable");
184 if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
185 seq_buf_printf(&s, ", ori31 speculation barrier enabled");
187 seq_buf_printf(&s, "\n");
189 seq_buf_printf(&s, "Not affected\n");
194 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
199 seq_buf_init(&s, buf, PAGE_SIZE - 1);
201 bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
202 ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
205 seq_buf_printf(&s, "Mitigation: ");
208 seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
211 seq_buf_printf(&s, ", ");
214 seq_buf_printf(&s, "Indirect branch cache disabled");
215 } else if (count_cache_flush_type != COUNT_CACHE_FLUSH_NONE) {
216 seq_buf_printf(&s, "Mitigation: Software count cache flush");
218 if (count_cache_flush_type == COUNT_CACHE_FLUSH_HW)
219 seq_buf_printf(&s, " (hardware accelerated)");
220 } else if (btb_flush_enabled) {
221 seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
223 seq_buf_printf(&s, "Vulnerable");
226 seq_buf_printf(&s, "\n");
231 #ifdef CONFIG_PPC_BOOK3S_64
233 * Store-forwarding barrier support.
236 static enum stf_barrier_type stf_enabled_flush_types;
237 static bool no_stf_barrier;
240 static int __init handle_no_stf_barrier(char *p)
242 pr_info("stf-barrier: disabled on command line.");
243 no_stf_barrier = true;
247 early_param("no_stf_barrier", handle_no_stf_barrier);
249 /* This is the generic flag used by other architectures */
250 static int __init handle_ssbd(char *p)
252 if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
253 /* Until firmware tells us, we have the barrier with auto */
255 } else if (strncmp(p, "off", 3) == 0) {
256 handle_no_stf_barrier(NULL);
263 early_param("spec_store_bypass_disable", handle_ssbd);
265 /* This is the generic flag used by other architectures */
266 static int __init handle_no_ssbd(char *p)
268 handle_no_stf_barrier(NULL);
271 early_param("nospec_store_bypass_disable", handle_no_ssbd);
273 static void stf_barrier_enable(bool enable)
276 do_stf_barrier_fixups(stf_enabled_flush_types);
278 do_stf_barrier_fixups(STF_BARRIER_NONE);
280 stf_barrier = enable;
283 void setup_stf_barrier(void)
285 enum stf_barrier_type type;
288 hv = cpu_has_feature(CPU_FTR_HVMODE);
290 /* Default to fallback in case fw-features are not available */
291 if (cpu_has_feature(CPU_FTR_ARCH_300))
292 type = STF_BARRIER_EIEIO;
293 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
294 type = STF_BARRIER_SYNC_ORI;
295 else if (cpu_has_feature(CPU_FTR_ARCH_206))
296 type = STF_BARRIER_FALLBACK;
298 type = STF_BARRIER_NONE;
300 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
301 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
302 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
304 if (type == STF_BARRIER_FALLBACK) {
305 pr_info("stf-barrier: fallback barrier available\n");
306 } else if (type == STF_BARRIER_SYNC_ORI) {
307 pr_info("stf-barrier: hwsync barrier available\n");
308 } else if (type == STF_BARRIER_EIEIO) {
309 pr_info("stf-barrier: eieio barrier available\n");
312 stf_enabled_flush_types = type;
314 if (!no_stf_barrier && !cpu_mitigations_off())
315 stf_barrier_enable(enable);
318 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
320 if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
322 switch (stf_enabled_flush_types) {
323 case STF_BARRIER_EIEIO:
326 case STF_BARRIER_SYNC_ORI:
329 case STF_BARRIER_FALLBACK:
335 return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
338 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
339 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
340 return sprintf(buf, "Not affected\n");
342 return sprintf(buf, "Vulnerable\n");
345 #ifdef CONFIG_DEBUG_FS
346 static int stf_barrier_set(void *data, u64 val)
357 /* Only do anything if we're changing state */
358 if (enable != stf_barrier)
359 stf_barrier_enable(enable);
364 static int stf_barrier_get(void *data, u64 *val)
366 *val = stf_barrier ? 1 : 0;
370 DEFINE_SIMPLE_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set, "%llu\n");
372 static __init int stf_barrier_debugfs_init(void)
374 debugfs_create_file("stf_barrier", 0600, powerpc_debugfs_root, NULL, &fops_stf_barrier);
377 device_initcall(stf_barrier_debugfs_init);
378 #endif /* CONFIG_DEBUG_FS */
380 static void toggle_count_cache_flush(bool enable)
382 if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
383 patch_instruction_site(&patch__call_flush_count_cache, PPC_INST_NOP);
384 count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
385 pr_info("count-cache-flush: software flush disabled.\n");
389 patch_branch_site(&patch__call_flush_count_cache,
390 (u64)&flush_count_cache, BRANCH_SET_LINK);
392 if (!security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
393 count_cache_flush_type = COUNT_CACHE_FLUSH_SW;
394 pr_info("count-cache-flush: full software flush sequence enabled.\n");
398 patch_instruction_site(&patch__flush_count_cache_return, PPC_INST_BLR);
399 count_cache_flush_type = COUNT_CACHE_FLUSH_HW;
400 pr_info("count-cache-flush: hardware assisted flush sequence enabled\n");
403 void setup_count_cache_flush(void)
407 if (no_spectrev2 || cpu_mitigations_off()) {
408 if (security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED) ||
409 security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED))
410 pr_warn("Spectre v2 mitigations not under software control, can't disable\n");
415 toggle_count_cache_flush(enable);
418 #ifdef CONFIG_DEBUG_FS
419 static int count_cache_flush_set(void *data, u64 val)
430 toggle_count_cache_flush(enable);
435 static int count_cache_flush_get(void *data, u64 *val)
437 if (count_cache_flush_type == COUNT_CACHE_FLUSH_NONE)
445 DEFINE_SIMPLE_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
446 count_cache_flush_set, "%llu\n");
448 static __init int count_cache_flush_debugfs_init(void)
450 debugfs_create_file("count_cache_flush", 0600, powerpc_debugfs_root,
451 NULL, &fops_count_cache_flush);
454 device_initcall(count_cache_flush_debugfs_init);
455 #endif /* CONFIG_DEBUG_FS */
456 #endif /* CONFIG_PPC_BOOK3S_64 */