xref: /linux-6.15/arch/powerpc/kernel/security.c (revision d93e5e2d)
19a868f63SMichael Ellerman // SPDX-License-Identifier: GPL-2.0+
29a868f63SMichael Ellerman //
39a868f63SMichael Ellerman // Security related flags and so on.
49a868f63SMichael Ellerman //
59a868f63SMichael Ellerman // Copyright 2018, Michael Ellerman, IBM Corporation.
69a868f63SMichael Ellerman 
742e2acdeSBreno Leitao #include <linux/cpu.h>
89a868f63SMichael Ellerman #include <linux/kernel.h>
98ad33041SMichael Ellerman #include <linux/device.h>
10*d93e5e2dSMichael Ellerman #include <linux/nospec.h>
11*d93e5e2dSMichael Ellerman #include <linux/prctl.h>
12ff348355SMichael Ellerman #include <linux/seq_buf.h>
138ad33041SMichael Ellerman 
14ee13cb24SMichael Ellerman #include <asm/asm-prototypes.h>
15ee13cb24SMichael Ellerman #include <asm/code-patching.h>
16a048a07dSNicholas Piggin #include <asm/debugfs.h>
179a868f63SMichael Ellerman #include <asm/security_features.h>
182eea7f06SMichal Suchanek #include <asm/setup.h>
199a868f63SMichael Ellerman 
209a868f63SMichael Ellerman 
213b05a1e5SGeert Uytterhoeven u64 powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
228ad33041SMichael Ellerman 
23ee13cb24SMichael Ellerman enum count_cache_flush_type {
24ee13cb24SMichael Ellerman 	COUNT_CACHE_FLUSH_NONE	= 0x1,
25ee13cb24SMichael Ellerman 	COUNT_CACHE_FLUSH_SW	= 0x2,
26ee13cb24SMichael Ellerman 	COUNT_CACHE_FLUSH_HW	= 0x4,
27ee13cb24SMichael Ellerman };
287d8bad99SDiana Craciun static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
2939e72bf9SMichael Ellerman static bool link_stack_flush_enabled;
30ee13cb24SMichael Ellerman 
31815069caSMichal Suchanek bool barrier_nospec_enabled;
32cf175dc3SDiana Craciun static bool no_nospec;
33f633a8adSDiana Craciun static bool btb_flush_enabled;
34d8f0e0b0SChristopher M. Riedl #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
35f633a8adSDiana Craciun static bool no_spectrev2;
36f633a8adSDiana Craciun #endif
372eea7f06SMichal Suchanek 
382eea7f06SMichal Suchanek static void enable_barrier_nospec(bool enable)
392eea7f06SMichal Suchanek {
402eea7f06SMichal Suchanek 	barrier_nospec_enabled = enable;
412eea7f06SMichal Suchanek 	do_barrier_nospec_fixups(enable);
422eea7f06SMichal Suchanek }
432eea7f06SMichal Suchanek 
44cb3d6759SMichal Suchanek void setup_barrier_nospec(void)
45cb3d6759SMichal Suchanek {
46cb3d6759SMichal Suchanek 	bool enable;
47cb3d6759SMichal Suchanek 
48cb3d6759SMichal Suchanek 	/*
49cb3d6759SMichal Suchanek 	 * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
50cb3d6759SMichal Suchanek 	 * But there's a good reason not to. The two flags we check below are
51cb3d6759SMichal Suchanek 	 * both are enabled by default in the kernel, so if the hcall is not
52cb3d6759SMichal Suchanek 	 * functional they will be enabled.
53cb3d6759SMichal Suchanek 	 * On a system where the host firmware has been updated (so the ori
54cb3d6759SMichal Suchanek 	 * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
55cb3d6759SMichal Suchanek 	 * not been updated, we would like to enable the barrier. Dropping the
56cb3d6759SMichal Suchanek 	 * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
57cb3d6759SMichal Suchanek 	 * we potentially enable the barrier on systems where the host firmware
58cb3d6759SMichal Suchanek 	 * is not updated, but that's harmless as it's a no-op.
59cb3d6759SMichal Suchanek 	 */
60cb3d6759SMichal Suchanek 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
61cb3d6759SMichal Suchanek 		 security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
62cb3d6759SMichal Suchanek 
63782e69efSJosh Poimboeuf 	if (!no_nospec && !cpu_mitigations_off())
64cb3d6759SMichal Suchanek 		enable_barrier_nospec(enable);
65cb3d6759SMichal Suchanek }
66cb3d6759SMichal Suchanek 
67cf175dc3SDiana Craciun static int __init handle_nospectre_v1(char *p)
68cf175dc3SDiana Craciun {
69cf175dc3SDiana Craciun 	no_nospec = true;
70cf175dc3SDiana Craciun 
71cf175dc3SDiana Craciun 	return 0;
72cf175dc3SDiana Craciun }
73cf175dc3SDiana Craciun early_param("nospectre_v1", handle_nospectre_v1);
74cf175dc3SDiana Craciun 
75cb3d6759SMichal Suchanek #ifdef CONFIG_DEBUG_FS
76cb3d6759SMichal Suchanek static int barrier_nospec_set(void *data, u64 val)
77cb3d6759SMichal Suchanek {
78cb3d6759SMichal Suchanek 	switch (val) {
79cb3d6759SMichal Suchanek 	case 0:
80cb3d6759SMichal Suchanek 	case 1:
81cb3d6759SMichal Suchanek 		break;
82cb3d6759SMichal Suchanek 	default:
83cb3d6759SMichal Suchanek 		return -EINVAL;
84cb3d6759SMichal Suchanek 	}
85cb3d6759SMichal Suchanek 
86cb3d6759SMichal Suchanek 	if (!!val == !!barrier_nospec_enabled)
87cb3d6759SMichal Suchanek 		return 0;
88cb3d6759SMichal Suchanek 
89cb3d6759SMichal Suchanek 	enable_barrier_nospec(!!val);
90cb3d6759SMichal Suchanek 
91cb3d6759SMichal Suchanek 	return 0;
92cb3d6759SMichal Suchanek }
93cb3d6759SMichal Suchanek 
94cb3d6759SMichal Suchanek static int barrier_nospec_get(void *data, u64 *val)
95cb3d6759SMichal Suchanek {
96cb3d6759SMichal Suchanek 	*val = barrier_nospec_enabled ? 1 : 0;
97cb3d6759SMichal Suchanek 	return 0;
98cb3d6759SMichal Suchanek }
99cb3d6759SMichal Suchanek 
100090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_barrier_nospec, barrier_nospec_get,
101090d5ab9SYueHaibing 			 barrier_nospec_set, "%llu\n");
102cb3d6759SMichal Suchanek 
103cb3d6759SMichal Suchanek static __init int barrier_nospec_debugfs_init(void)
104cb3d6759SMichal Suchanek {
105090d5ab9SYueHaibing 	debugfs_create_file_unsafe("barrier_nospec", 0600,
106090d5ab9SYueHaibing 				   powerpc_debugfs_root, NULL,
107cb3d6759SMichal Suchanek 				   &fops_barrier_nospec);
108cb3d6759SMichal Suchanek 	return 0;
109cb3d6759SMichal Suchanek }
110cb3d6759SMichal Suchanek device_initcall(barrier_nospec_debugfs_init);
111398af571SMichael Ellerman 
112398af571SMichael Ellerman static __init int security_feature_debugfs_init(void)
113398af571SMichael Ellerman {
114398af571SMichael Ellerman 	debugfs_create_x64("security_features", 0400, powerpc_debugfs_root,
1153b05a1e5SGeert Uytterhoeven 			   &powerpc_security_features);
116398af571SMichael Ellerman 	return 0;
117398af571SMichael Ellerman }
118398af571SMichael Ellerman device_initcall(security_feature_debugfs_init);
119cb3d6759SMichal Suchanek #endif /* CONFIG_DEBUG_FS */
120cb3d6759SMichal Suchanek 
121d8f0e0b0SChristopher M. Riedl #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
122f633a8adSDiana Craciun static int __init handle_nospectre_v2(char *p)
123f633a8adSDiana Craciun {
124f633a8adSDiana Craciun 	no_spectrev2 = true;
125f633a8adSDiana Craciun 
126f633a8adSDiana Craciun 	return 0;
127f633a8adSDiana Craciun }
128f633a8adSDiana Craciun early_param("nospectre_v2", handle_nospectre_v2);
129d8f0e0b0SChristopher M. Riedl #endif /* CONFIG_PPC_FSL_BOOK3E || CONFIG_PPC_BOOK3S_64 */
130d8f0e0b0SChristopher M. Riedl 
131d8f0e0b0SChristopher M. Riedl #ifdef CONFIG_PPC_FSL_BOOK3E
132f633a8adSDiana Craciun void setup_spectre_v2(void)
133f633a8adSDiana Craciun {
134782e69efSJosh Poimboeuf 	if (no_spectrev2 || cpu_mitigations_off())
135f633a8adSDiana Craciun 		do_btb_flush_fixups();
136f633a8adSDiana Craciun 	else
137f633a8adSDiana Craciun 		btb_flush_enabled = true;
138f633a8adSDiana Craciun }
139f633a8adSDiana Craciun #endif /* CONFIG_PPC_FSL_BOOK3E */
140f633a8adSDiana Craciun 
141406d2b6aSDiana Craciun #ifdef CONFIG_PPC_BOOK3S_64
1428ad33041SMichael Ellerman ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1438ad33041SMichael Ellerman {
144ff348355SMichael Ellerman 	bool thread_priv;
145ff348355SMichael Ellerman 
146ff348355SMichael Ellerman 	thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
147ff348355SMichael Ellerman 
1484e706af3SGustavo L. F. Walbon 	if (rfi_flush) {
149ff348355SMichael Ellerman 		struct seq_buf s;
150ff348355SMichael Ellerman 		seq_buf_init(&s, buf, PAGE_SIZE - 1);
151ff348355SMichael Ellerman 
1524e706af3SGustavo L. F. Walbon 		seq_buf_printf(&s, "Mitigation: RFI Flush");
153ff348355SMichael Ellerman 		if (thread_priv)
1544e706af3SGustavo L. F. Walbon 			seq_buf_printf(&s, ", L1D private per thread");
155ff348355SMichael Ellerman 
156ff348355SMichael Ellerman 		seq_buf_printf(&s, "\n");
157ff348355SMichael Ellerman 
158ff348355SMichael Ellerman 		return s.len;
159ff348355SMichael Ellerman 	}
160ff348355SMichael Ellerman 
1614e706af3SGustavo L. F. Walbon 	if (thread_priv)
1624e706af3SGustavo L. F. Walbon 		return sprintf(buf, "Vulnerable: L1D private per thread\n");
1634e706af3SGustavo L. F. Walbon 
164ff348355SMichael Ellerman 	if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
165ff348355SMichael Ellerman 	    !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
166ff348355SMichael Ellerman 		return sprintf(buf, "Not affected\n");
1678ad33041SMichael Ellerman 
1688ad33041SMichael Ellerman 	return sprintf(buf, "Vulnerable\n");
1698ad33041SMichael Ellerman }
1708e6b6da9SAnthony Steinhauser 
1718e6b6da9SAnthony Steinhauser ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1728e6b6da9SAnthony Steinhauser {
1738e6b6da9SAnthony Steinhauser 	return cpu_show_meltdown(dev, attr, buf);
1748e6b6da9SAnthony Steinhauser }
175406d2b6aSDiana Craciun #endif
17656986016SMichael Ellerman 
17756986016SMichael Ellerman ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
17856986016SMichael Ellerman {
1796d44acaeSMichael Ellerman 	struct seq_buf s;
18056986016SMichael Ellerman 
1816d44acaeSMichael Ellerman 	seq_buf_init(&s, buf, PAGE_SIZE - 1);
1826d44acaeSMichael Ellerman 
1836d44acaeSMichael Ellerman 	if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
184a3775145SMichal Suchanek 		if (barrier_nospec_enabled)
1856d44acaeSMichael Ellerman 			seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
1866d44acaeSMichael Ellerman 		else
1876d44acaeSMichael Ellerman 			seq_buf_printf(&s, "Vulnerable");
188a3775145SMichal Suchanek 
1896d44acaeSMichael Ellerman 		if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
1906d44acaeSMichael Ellerman 			seq_buf_printf(&s, ", ori31 speculation barrier enabled");
1916d44acaeSMichael Ellerman 
1926d44acaeSMichael Ellerman 		seq_buf_printf(&s, "\n");
1936d44acaeSMichael Ellerman 	} else
1946d44acaeSMichael Ellerman 		seq_buf_printf(&s, "Not affected\n");
1956d44acaeSMichael Ellerman 
1966d44acaeSMichael Ellerman 	return s.len;
19756986016SMichael Ellerman }
198d6fbe1c5SMichael Ellerman 
199d6fbe1c5SMichael Ellerman ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
200d6fbe1c5SMichael Ellerman {
201d6fbe1c5SMichael Ellerman 	struct seq_buf s;
2026d44acaeSMichael Ellerman 	bool bcs, ccd;
203d6fbe1c5SMichael Ellerman 
204d6fbe1c5SMichael Ellerman 	seq_buf_init(&s, buf, PAGE_SIZE - 1);
205d6fbe1c5SMichael Ellerman 
206d6fbe1c5SMichael Ellerman 	bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
207d6fbe1c5SMichael Ellerman 	ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
208d6fbe1c5SMichael Ellerman 
20992edf8dfSMichael Ellerman 	if (bcs || ccd) {
210d6fbe1c5SMichael Ellerman 		seq_buf_printf(&s, "Mitigation: ");
211d6fbe1c5SMichael Ellerman 
21292edf8dfSMichael Ellerman 		if (bcs)
213d6fbe1c5SMichael Ellerman 			seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
214d6fbe1c5SMichael Ellerman 
21592edf8dfSMichael Ellerman 		if (bcs && ccd)
216ee13cb24SMichael Ellerman 			seq_buf_printf(&s, ", ");
21792edf8dfSMichael Ellerman 
21892edf8dfSMichael Ellerman 		if (ccd)
219ee13cb24SMichael Ellerman 			seq_buf_printf(&s, "Indirect branch cache disabled");
22039e72bf9SMichael Ellerman 
22139e72bf9SMichael Ellerman 		if (link_stack_flush_enabled)
22239e72bf9SMichael Ellerman 			seq_buf_printf(&s, ", Software link stack flush");
22339e72bf9SMichael Ellerman 
22492edf8dfSMichael Ellerman 	} else if (count_cache_flush_type != COUNT_CACHE_FLUSH_NONE) {
22592edf8dfSMichael Ellerman 		seq_buf_printf(&s, "Mitigation: Software count cache flush");
226ee13cb24SMichael Ellerman 
227ee13cb24SMichael Ellerman 		if (count_cache_flush_type == COUNT_CACHE_FLUSH_HW)
228ee13cb24SMichael Ellerman 			seq_buf_printf(&s, " (hardware accelerated)");
22939e72bf9SMichael Ellerman 
23039e72bf9SMichael Ellerman 		if (link_stack_flush_enabled)
23139e72bf9SMichael Ellerman 			seq_buf_printf(&s, ", Software link stack flush");
23239e72bf9SMichael Ellerman 
233dfa88658SDiana Craciun 	} else if (btb_flush_enabled) {
234dfa88658SDiana Craciun 		seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
235dfa88658SDiana Craciun 	} else {
236d6fbe1c5SMichael Ellerman 		seq_buf_printf(&s, "Vulnerable");
237dfa88658SDiana Craciun 	}
238d6fbe1c5SMichael Ellerman 
239d6fbe1c5SMichael Ellerman 	seq_buf_printf(&s, "\n");
240d6fbe1c5SMichael Ellerman 
241d6fbe1c5SMichael Ellerman 	return s.len;
242d6fbe1c5SMichael Ellerman }
243a048a07dSNicholas Piggin 
2446453b532SDiana Craciun #ifdef CONFIG_PPC_BOOK3S_64
245a048a07dSNicholas Piggin /*
246a048a07dSNicholas Piggin  * Store-forwarding barrier support.
247a048a07dSNicholas Piggin  */
248a048a07dSNicholas Piggin 
249a048a07dSNicholas Piggin static enum stf_barrier_type stf_enabled_flush_types;
250a048a07dSNicholas Piggin static bool no_stf_barrier;
251a048a07dSNicholas Piggin bool stf_barrier;
252a048a07dSNicholas Piggin 
253a048a07dSNicholas Piggin static int __init handle_no_stf_barrier(char *p)
254a048a07dSNicholas Piggin {
255a048a07dSNicholas Piggin 	pr_info("stf-barrier: disabled on command line.");
256a048a07dSNicholas Piggin 	no_stf_barrier = true;
257a048a07dSNicholas Piggin 	return 0;
258a048a07dSNicholas Piggin }
259a048a07dSNicholas Piggin 
260a048a07dSNicholas Piggin early_param("no_stf_barrier", handle_no_stf_barrier);
261a048a07dSNicholas Piggin 
262a048a07dSNicholas Piggin /* This is the generic flag used by other architectures */
263a048a07dSNicholas Piggin static int __init handle_ssbd(char *p)
264a048a07dSNicholas Piggin {
265a048a07dSNicholas Piggin 	if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
266a048a07dSNicholas Piggin 		/* Until firmware tells us, we have the barrier with auto */
267a048a07dSNicholas Piggin 		return 0;
268a048a07dSNicholas Piggin 	} else if (strncmp(p, "off", 3) == 0) {
269a048a07dSNicholas Piggin 		handle_no_stf_barrier(NULL);
270a048a07dSNicholas Piggin 		return 0;
271a048a07dSNicholas Piggin 	} else
272a048a07dSNicholas Piggin 		return 1;
273a048a07dSNicholas Piggin 
274a048a07dSNicholas Piggin 	return 0;
275a048a07dSNicholas Piggin }
276a048a07dSNicholas Piggin early_param("spec_store_bypass_disable", handle_ssbd);
277a048a07dSNicholas Piggin 
278a048a07dSNicholas Piggin /* This is the generic flag used by other architectures */
279a048a07dSNicholas Piggin static int __init handle_no_ssbd(char *p)
280a048a07dSNicholas Piggin {
281a048a07dSNicholas Piggin 	handle_no_stf_barrier(NULL);
282a048a07dSNicholas Piggin 	return 0;
283a048a07dSNicholas Piggin }
284a048a07dSNicholas Piggin early_param("nospec_store_bypass_disable", handle_no_ssbd);
285a048a07dSNicholas Piggin 
286a048a07dSNicholas Piggin static void stf_barrier_enable(bool enable)
287a048a07dSNicholas Piggin {
288a048a07dSNicholas Piggin 	if (enable)
289a048a07dSNicholas Piggin 		do_stf_barrier_fixups(stf_enabled_flush_types);
290a048a07dSNicholas Piggin 	else
291a048a07dSNicholas Piggin 		do_stf_barrier_fixups(STF_BARRIER_NONE);
292a048a07dSNicholas Piggin 
293a048a07dSNicholas Piggin 	stf_barrier = enable;
294a048a07dSNicholas Piggin }
295a048a07dSNicholas Piggin 
296a048a07dSNicholas Piggin void setup_stf_barrier(void)
297a048a07dSNicholas Piggin {
298a048a07dSNicholas Piggin 	enum stf_barrier_type type;
299a048a07dSNicholas Piggin 	bool enable, hv;
300a048a07dSNicholas Piggin 
301a048a07dSNicholas Piggin 	hv = cpu_has_feature(CPU_FTR_HVMODE);
302a048a07dSNicholas Piggin 
303a048a07dSNicholas Piggin 	/* Default to fallback in case fw-features are not available */
304a048a07dSNicholas Piggin 	if (cpu_has_feature(CPU_FTR_ARCH_300))
305a048a07dSNicholas Piggin 		type = STF_BARRIER_EIEIO;
306a048a07dSNicholas Piggin 	else if (cpu_has_feature(CPU_FTR_ARCH_207S))
307a048a07dSNicholas Piggin 		type = STF_BARRIER_SYNC_ORI;
308a048a07dSNicholas Piggin 	else if (cpu_has_feature(CPU_FTR_ARCH_206))
309a048a07dSNicholas Piggin 		type = STF_BARRIER_FALLBACK;
310a048a07dSNicholas Piggin 	else
311a048a07dSNicholas Piggin 		type = STF_BARRIER_NONE;
312a048a07dSNicholas Piggin 
313a048a07dSNicholas Piggin 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
314a048a07dSNicholas Piggin 		(security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
315a048a07dSNicholas Piggin 		 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
316a048a07dSNicholas Piggin 
317a048a07dSNicholas Piggin 	if (type == STF_BARRIER_FALLBACK) {
318a048a07dSNicholas Piggin 		pr_info("stf-barrier: fallback barrier available\n");
319a048a07dSNicholas Piggin 	} else if (type == STF_BARRIER_SYNC_ORI) {
320a048a07dSNicholas Piggin 		pr_info("stf-barrier: hwsync barrier available\n");
321a048a07dSNicholas Piggin 	} else if (type == STF_BARRIER_EIEIO) {
322a048a07dSNicholas Piggin 		pr_info("stf-barrier: eieio barrier available\n");
323a048a07dSNicholas Piggin 	}
324a048a07dSNicholas Piggin 
325a048a07dSNicholas Piggin 	stf_enabled_flush_types = type;
326a048a07dSNicholas Piggin 
327782e69efSJosh Poimboeuf 	if (!no_stf_barrier && !cpu_mitigations_off())
328a048a07dSNicholas Piggin 		stf_barrier_enable(enable);
329a048a07dSNicholas Piggin }
330a048a07dSNicholas Piggin 
331a048a07dSNicholas Piggin ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
332a048a07dSNicholas Piggin {
333a048a07dSNicholas Piggin 	if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
334a048a07dSNicholas Piggin 		const char *type;
335a048a07dSNicholas Piggin 		switch (stf_enabled_flush_types) {
336a048a07dSNicholas Piggin 		case STF_BARRIER_EIEIO:
337a048a07dSNicholas Piggin 			type = "eieio";
338a048a07dSNicholas Piggin 			break;
339a048a07dSNicholas Piggin 		case STF_BARRIER_SYNC_ORI:
340a048a07dSNicholas Piggin 			type = "hwsync";
341a048a07dSNicholas Piggin 			break;
342a048a07dSNicholas Piggin 		case STF_BARRIER_FALLBACK:
343a048a07dSNicholas Piggin 			type = "fallback";
344a048a07dSNicholas Piggin 			break;
345a048a07dSNicholas Piggin 		default:
346a048a07dSNicholas Piggin 			type = "unknown";
347a048a07dSNicholas Piggin 		}
348a048a07dSNicholas Piggin 		return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
349a048a07dSNicholas Piggin 	}
350a048a07dSNicholas Piggin 
351a048a07dSNicholas Piggin 	if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
352a048a07dSNicholas Piggin 	    !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
353a048a07dSNicholas Piggin 		return sprintf(buf, "Not affected\n");
354a048a07dSNicholas Piggin 
355a048a07dSNicholas Piggin 	return sprintf(buf, "Vulnerable\n");
356a048a07dSNicholas Piggin }
357a048a07dSNicholas Piggin 
358*d93e5e2dSMichael Ellerman static int ssb_prctl_get(struct task_struct *task)
359*d93e5e2dSMichael Ellerman {
360*d93e5e2dSMichael Ellerman 	if (stf_enabled_flush_types == STF_BARRIER_NONE)
361*d93e5e2dSMichael Ellerman 		/*
362*d93e5e2dSMichael Ellerman 		 * We don't have an explicit signal from firmware that we're
363*d93e5e2dSMichael Ellerman 		 * vulnerable or not, we only have certain CPU revisions that
364*d93e5e2dSMichael Ellerman 		 * are known to be vulnerable.
365*d93e5e2dSMichael Ellerman 		 *
366*d93e5e2dSMichael Ellerman 		 * We assume that if we're on another CPU, where the barrier is
367*d93e5e2dSMichael Ellerman 		 * NONE, then we are not vulnerable.
368*d93e5e2dSMichael Ellerman 		 */
369*d93e5e2dSMichael Ellerman 		return PR_SPEC_NOT_AFFECTED;
370*d93e5e2dSMichael Ellerman 	else
371*d93e5e2dSMichael Ellerman 		/*
372*d93e5e2dSMichael Ellerman 		 * If we do have a barrier type then we are vulnerable. The
373*d93e5e2dSMichael Ellerman 		 * barrier is not a global or per-process mitigation, so the
374*d93e5e2dSMichael Ellerman 		 * only value we can report here is PR_SPEC_ENABLE, which
375*d93e5e2dSMichael Ellerman 		 * appears as "vulnerable" in /proc.
376*d93e5e2dSMichael Ellerman 		 */
377*d93e5e2dSMichael Ellerman 		return PR_SPEC_ENABLE;
378*d93e5e2dSMichael Ellerman 
379*d93e5e2dSMichael Ellerman 	return -EINVAL;
380*d93e5e2dSMichael Ellerman }
381*d93e5e2dSMichael Ellerman 
382*d93e5e2dSMichael Ellerman int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
383*d93e5e2dSMichael Ellerman {
384*d93e5e2dSMichael Ellerman 	switch (which) {
385*d93e5e2dSMichael Ellerman 	case PR_SPEC_STORE_BYPASS:
386*d93e5e2dSMichael Ellerman 		return ssb_prctl_get(task);
387*d93e5e2dSMichael Ellerman 	default:
388*d93e5e2dSMichael Ellerman 		return -ENODEV;
389*d93e5e2dSMichael Ellerman 	}
390*d93e5e2dSMichael Ellerman }
391*d93e5e2dSMichael Ellerman 
392a048a07dSNicholas Piggin #ifdef CONFIG_DEBUG_FS
393a048a07dSNicholas Piggin static int stf_barrier_set(void *data, u64 val)
394a048a07dSNicholas Piggin {
395a048a07dSNicholas Piggin 	bool enable;
396a048a07dSNicholas Piggin 
397a048a07dSNicholas Piggin 	if (val == 1)
398a048a07dSNicholas Piggin 		enable = true;
399a048a07dSNicholas Piggin 	else if (val == 0)
400a048a07dSNicholas Piggin 		enable = false;
401a048a07dSNicholas Piggin 	else
402a048a07dSNicholas Piggin 		return -EINVAL;
403a048a07dSNicholas Piggin 
404a048a07dSNicholas Piggin 	/* Only do anything if we're changing state */
405a048a07dSNicholas Piggin 	if (enable != stf_barrier)
406a048a07dSNicholas Piggin 		stf_barrier_enable(enable);
407a048a07dSNicholas Piggin 
408a048a07dSNicholas Piggin 	return 0;
409a048a07dSNicholas Piggin }
410a048a07dSNicholas Piggin 
411a048a07dSNicholas Piggin static int stf_barrier_get(void *data, u64 *val)
412a048a07dSNicholas Piggin {
413a048a07dSNicholas Piggin 	*val = stf_barrier ? 1 : 0;
414a048a07dSNicholas Piggin 	return 0;
415a048a07dSNicholas Piggin }
416a048a07dSNicholas Piggin 
417090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set,
418090d5ab9SYueHaibing 			 "%llu\n");
419a048a07dSNicholas Piggin 
420a048a07dSNicholas Piggin static __init int stf_barrier_debugfs_init(void)
421a048a07dSNicholas Piggin {
422090d5ab9SYueHaibing 	debugfs_create_file_unsafe("stf_barrier", 0600, powerpc_debugfs_root,
423090d5ab9SYueHaibing 				   NULL, &fops_stf_barrier);
424a048a07dSNicholas Piggin 	return 0;
425a048a07dSNicholas Piggin }
426a048a07dSNicholas Piggin device_initcall(stf_barrier_debugfs_init);
427a048a07dSNicholas Piggin #endif /* CONFIG_DEBUG_FS */
428ee13cb24SMichael Ellerman 
42939e72bf9SMichael Ellerman static void no_count_cache_flush(void)
430ee13cb24SMichael Ellerman {
431ee13cb24SMichael Ellerman 	count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
432ee13cb24SMichael Ellerman 	pr_info("count-cache-flush: software flush disabled.\n");
43339e72bf9SMichael Ellerman }
43439e72bf9SMichael Ellerman 
43539e72bf9SMichael Ellerman static void toggle_count_cache_flush(bool enable)
43639e72bf9SMichael Ellerman {
43739e72bf9SMichael Ellerman 	if (!security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE) &&
43839e72bf9SMichael Ellerman 	    !security_ftr_enabled(SEC_FTR_FLUSH_LINK_STACK))
43939e72bf9SMichael Ellerman 		enable = false;
44039e72bf9SMichael Ellerman 
44139e72bf9SMichael Ellerman 	if (!enable) {
44239e72bf9SMichael Ellerman 		patch_instruction_site(&patch__call_flush_count_cache, PPC_INST_NOP);
443af2e8c68SMichael Ellerman #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
444af2e8c68SMichael Ellerman 		patch_instruction_site(&patch__call_kvm_flush_link_stack, PPC_INST_NOP);
445af2e8c68SMichael Ellerman #endif
44639e72bf9SMichael Ellerman 		pr_info("link-stack-flush: software flush disabled.\n");
44739e72bf9SMichael Ellerman 		link_stack_flush_enabled = false;
44839e72bf9SMichael Ellerman 		no_count_cache_flush();
449ee13cb24SMichael Ellerman 		return;
450ee13cb24SMichael Ellerman 	}
451ee13cb24SMichael Ellerman 
45239e72bf9SMichael Ellerman 	// This enables the branch from _switch to flush_count_cache
453ee13cb24SMichael Ellerman 	patch_branch_site(&patch__call_flush_count_cache,
454ee13cb24SMichael Ellerman 			  (u64)&flush_count_cache, BRANCH_SET_LINK);
455ee13cb24SMichael Ellerman 
456af2e8c68SMichael Ellerman #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
457af2e8c68SMichael Ellerman 	// This enables the branch from guest_exit_cont to kvm_flush_link_stack
458af2e8c68SMichael Ellerman 	patch_branch_site(&patch__call_kvm_flush_link_stack,
459af2e8c68SMichael Ellerman 			  (u64)&kvm_flush_link_stack, BRANCH_SET_LINK);
460af2e8c68SMichael Ellerman #endif
461af2e8c68SMichael Ellerman 
46239e72bf9SMichael Ellerman 	pr_info("link-stack-flush: software flush enabled.\n");
46339e72bf9SMichael Ellerman 	link_stack_flush_enabled = true;
46439e72bf9SMichael Ellerman 
46539e72bf9SMichael Ellerman 	// If we just need to flush the link stack, patch an early return
46639e72bf9SMichael Ellerman 	if (!security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
46739e72bf9SMichael Ellerman 		patch_instruction_site(&patch__flush_link_stack_return, PPC_INST_BLR);
46839e72bf9SMichael Ellerman 		no_count_cache_flush();
46939e72bf9SMichael Ellerman 		return;
47039e72bf9SMichael Ellerman 	}
47139e72bf9SMichael Ellerman 
472ee13cb24SMichael Ellerman 	if (!security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
473ee13cb24SMichael Ellerman 		count_cache_flush_type = COUNT_CACHE_FLUSH_SW;
474ee13cb24SMichael Ellerman 		pr_info("count-cache-flush: full software flush sequence enabled.\n");
475ee13cb24SMichael Ellerman 		return;
476ee13cb24SMichael Ellerman 	}
477ee13cb24SMichael Ellerman 
478ee13cb24SMichael Ellerman 	patch_instruction_site(&patch__flush_count_cache_return, PPC_INST_BLR);
479ee13cb24SMichael Ellerman 	count_cache_flush_type = COUNT_CACHE_FLUSH_HW;
480ee13cb24SMichael Ellerman 	pr_info("count-cache-flush: hardware assisted flush sequence enabled\n");
481ee13cb24SMichael Ellerman }
482ee13cb24SMichael Ellerman 
483ee13cb24SMichael Ellerman void setup_count_cache_flush(void)
484ee13cb24SMichael Ellerman {
485d8f0e0b0SChristopher M. Riedl 	bool enable = true;
486d8f0e0b0SChristopher M. Riedl 
487d8f0e0b0SChristopher M. Riedl 	if (no_spectrev2 || cpu_mitigations_off()) {
488d8f0e0b0SChristopher M. Riedl 		if (security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED) ||
489d8f0e0b0SChristopher M. Riedl 		    security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED))
49039e72bf9SMichael Ellerman 			pr_warn("Spectre v2 mitigations not fully under software control, can't disable\n");
491d8f0e0b0SChristopher M. Riedl 
492d8f0e0b0SChristopher M. Riedl 		enable = false;
493d8f0e0b0SChristopher M. Riedl 	}
494d8f0e0b0SChristopher M. Riedl 
49539e72bf9SMichael Ellerman 	/*
49639e72bf9SMichael Ellerman 	 * There's no firmware feature flag/hypervisor bit to tell us we need to
49739e72bf9SMichael Ellerman 	 * flush the link stack on context switch. So we set it here if we see
49839e72bf9SMichael Ellerman 	 * either of the Spectre v2 mitigations that aim to protect userspace.
49939e72bf9SMichael Ellerman 	 */
50039e72bf9SMichael Ellerman 	if (security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED) ||
50139e72bf9SMichael Ellerman 	    security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE))
50239e72bf9SMichael Ellerman 		security_ftr_set(SEC_FTR_FLUSH_LINK_STACK);
50339e72bf9SMichael Ellerman 
504d8f0e0b0SChristopher M. Riedl 	toggle_count_cache_flush(enable);
505ee13cb24SMichael Ellerman }
506ee13cb24SMichael Ellerman 
507ee13cb24SMichael Ellerman #ifdef CONFIG_DEBUG_FS
508ee13cb24SMichael Ellerman static int count_cache_flush_set(void *data, u64 val)
509ee13cb24SMichael Ellerman {
510ee13cb24SMichael Ellerman 	bool enable;
511ee13cb24SMichael Ellerman 
512ee13cb24SMichael Ellerman 	if (val == 1)
513ee13cb24SMichael Ellerman 		enable = true;
514ee13cb24SMichael Ellerman 	else if (val == 0)
515ee13cb24SMichael Ellerman 		enable = false;
516ee13cb24SMichael Ellerman 	else
517ee13cb24SMichael Ellerman 		return -EINVAL;
518ee13cb24SMichael Ellerman 
519ee13cb24SMichael Ellerman 	toggle_count_cache_flush(enable);
520ee13cb24SMichael Ellerman 
521ee13cb24SMichael Ellerman 	return 0;
522ee13cb24SMichael Ellerman }
523ee13cb24SMichael Ellerman 
524ee13cb24SMichael Ellerman static int count_cache_flush_get(void *data, u64 *val)
525ee13cb24SMichael Ellerman {
526ee13cb24SMichael Ellerman 	if (count_cache_flush_type == COUNT_CACHE_FLUSH_NONE)
527ee13cb24SMichael Ellerman 		*val = 0;
528ee13cb24SMichael Ellerman 	else
529ee13cb24SMichael Ellerman 		*val = 1;
530ee13cb24SMichael Ellerman 
531ee13cb24SMichael Ellerman 	return 0;
532ee13cb24SMichael Ellerman }
533ee13cb24SMichael Ellerman 
534090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
535ee13cb24SMichael Ellerman 			 count_cache_flush_set, "%llu\n");
536ee13cb24SMichael Ellerman 
537ee13cb24SMichael Ellerman static __init int count_cache_flush_debugfs_init(void)
538ee13cb24SMichael Ellerman {
539090d5ab9SYueHaibing 	debugfs_create_file_unsafe("count_cache_flush", 0600,
540090d5ab9SYueHaibing 				   powerpc_debugfs_root, NULL,
541090d5ab9SYueHaibing 				   &fops_count_cache_flush);
542ee13cb24SMichael Ellerman 	return 0;
543ee13cb24SMichael Ellerman }
544ee13cb24SMichael Ellerman device_initcall(count_cache_flush_debugfs_init);
545ee13cb24SMichael Ellerman #endif /* CONFIG_DEBUG_FS */
5466453b532SDiana Craciun #endif /* CONFIG_PPC_BOOK3S_64 */
547