^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Security related flags and so on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright 2018, Michael Ellerman, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/nospec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/seq_buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/asm-prototypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/code-patching.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/security_features.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/inst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) u64 powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) enum branch_cache_flush_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) BRANCH_CACHE_FLUSH_NONE = 0x1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) BRANCH_CACHE_FLUSH_SW = 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) BRANCH_CACHE_FLUSH_HW = 0x4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static enum branch_cache_flush_type count_cache_flush_type = BRANCH_CACHE_FLUSH_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static enum branch_cache_flush_type link_stack_flush_type = BRANCH_CACHE_FLUSH_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) bool barrier_nospec_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static bool no_nospec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static bool btb_flush_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static bool no_spectrev2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void enable_barrier_nospec(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) barrier_nospec_enabled = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) do_barrier_nospec_fixups(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void setup_barrier_nospec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * But there's a good reason not to. The two flags we check below are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * both are enabled by default in the kernel, so if the hcall is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * functional they will be enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * On a system where the host firmware has been updated (so the ori
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * not been updated, we would like to enable the barrier. Dropping the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * we potentially enable the barrier on systems where the host firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * is not updated, but that's harmless as it's a no-op.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!no_nospec && !cpu_mitigations_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) enable_barrier_nospec(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int __init handle_nospectre_v1(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) no_nospec = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) early_param("nospectre_v1", handle_nospectre_v1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int barrier_nospec_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!!val == !!barrier_nospec_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) enable_barrier_nospec(!!val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int barrier_nospec_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *val = barrier_nospec_enabled ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) DEFINE_DEBUGFS_ATTRIBUTE(fops_barrier_nospec, barrier_nospec_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) barrier_nospec_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static __init int barrier_nospec_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) debugfs_create_file_unsafe("barrier_nospec", 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) powerpc_debugfs_root, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) &fops_barrier_nospec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) device_initcall(barrier_nospec_debugfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static __init int security_feature_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) debugfs_create_x64("security_features", 0400, powerpc_debugfs_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) &powerpc_security_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) device_initcall(security_feature_debugfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int __init handle_nospectre_v2(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) no_spectrev2 = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) early_param("nospectre_v2", handle_nospectre_v2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #endif /* CONFIG_PPC_FSL_BOOK3E || CONFIG_PPC_BOOK3S_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #ifdef CONFIG_PPC_FSL_BOOK3E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void setup_spectre_v2(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (no_spectrev2 || cpu_mitigations_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) do_btb_flush_fixups();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) btb_flush_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #endif /* CONFIG_PPC_FSL_BOOK3E */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #ifdef CONFIG_PPC_BOOK3S_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bool thread_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (rfi_flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct seq_buf s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) seq_buf_init(&s, buf, PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) seq_buf_printf(&s, "Mitigation: RFI Flush");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (thread_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) seq_buf_printf(&s, ", L1D private per thread");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) seq_buf_printf(&s, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return s.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (thread_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return sprintf(buf, "Vulnerable: L1D private per thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return sprintf(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return sprintf(buf, "Vulnerable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return cpu_show_meltdown(dev, attr, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct seq_buf s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) seq_buf_init(&s, buf, PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (barrier_nospec_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) seq_buf_printf(&s, "Vulnerable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) seq_buf_printf(&s, ", ori31 speculation barrier enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) seq_buf_printf(&s, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) seq_buf_printf(&s, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return s.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct seq_buf s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) bool bcs, ccd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) seq_buf_init(&s, buf, PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (bcs || ccd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) seq_buf_printf(&s, "Mitigation: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (bcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (bcs && ccd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) seq_buf_printf(&s, ", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (ccd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) seq_buf_printf(&s, "Indirect branch cache disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) } else if (count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) seq_buf_printf(&s, "Mitigation: Software count cache flush");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) seq_buf_printf(&s, " (hardware accelerated)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else if (btb_flush_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) seq_buf_printf(&s, "Vulnerable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (bcs || ccd || count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (link_stack_flush_type != BRANCH_CACHE_FLUSH_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) seq_buf_printf(&s, ", Software link stack flush");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (link_stack_flush_type == BRANCH_CACHE_FLUSH_HW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) seq_buf_printf(&s, " (hardware accelerated)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) seq_buf_printf(&s, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return s.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #ifdef CONFIG_PPC_BOOK3S_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Store-forwarding barrier support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static enum stf_barrier_type stf_enabled_flush_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static bool no_stf_barrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) bool stf_barrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int __init handle_no_stf_barrier(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) pr_info("stf-barrier: disabled on command line.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) no_stf_barrier = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) early_param("no_stf_barrier", handle_no_stf_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) enum stf_barrier_type stf_barrier_type_get(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return stf_enabled_flush_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* This is the generic flag used by other architectures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int __init handle_ssbd(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Until firmware tells us, we have the barrier with auto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) } else if (strncmp(p, "off", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) handle_no_stf_barrier(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) early_param("spec_store_bypass_disable", handle_ssbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* This is the generic flag used by other architectures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int __init handle_no_ssbd(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) handle_no_stf_barrier(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) early_param("nospec_store_bypass_disable", handle_no_ssbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void stf_barrier_enable(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) do_stf_barrier_fixups(stf_enabled_flush_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) do_stf_barrier_fixups(STF_BARRIER_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) stf_barrier = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) void setup_stf_barrier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) enum stf_barrier_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bool enable, hv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) hv = cpu_has_feature(CPU_FTR_HVMODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Default to fallback in case fw-features are not available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (cpu_has_feature(CPU_FTR_ARCH_300))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) type = STF_BARRIER_EIEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else if (cpu_has_feature(CPU_FTR_ARCH_207S))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) type = STF_BARRIER_SYNC_ORI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) else if (cpu_has_feature(CPU_FTR_ARCH_206))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) type = STF_BARRIER_FALLBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) type = STF_BARRIER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (type == STF_BARRIER_FALLBACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) pr_info("stf-barrier: fallback barrier available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) } else if (type == STF_BARRIER_SYNC_ORI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) pr_info("stf-barrier: hwsync barrier available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) } else if (type == STF_BARRIER_EIEIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) pr_info("stf-barrier: eieio barrier available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) stf_enabled_flush_types = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (!no_stf_barrier && !cpu_mitigations_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) stf_barrier_enable(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) const char *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) switch (stf_enabled_flush_types) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) case STF_BARRIER_EIEIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) type = "eieio";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) case STF_BARRIER_SYNC_ORI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) type = "hwsync";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) case STF_BARRIER_FALLBACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) type = "fallback";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) type = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return sprintf(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return sprintf(buf, "Vulnerable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static int ssb_prctl_get(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (stf_enabled_flush_types == STF_BARRIER_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * We don't have an explicit signal from firmware that we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * vulnerable or not, we only have certain CPU revisions that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * are known to be vulnerable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * We assume that if we're on another CPU, where the barrier is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * NONE, then we are not vulnerable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return PR_SPEC_NOT_AFFECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * If we do have a barrier type then we are vulnerable. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * barrier is not a global or per-process mitigation, so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * only value we can report here is PR_SPEC_ENABLE, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * appears as "vulnerable" in /proc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return PR_SPEC_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) switch (which) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case PR_SPEC_STORE_BYPASS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return ssb_prctl_get(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static int stf_barrier_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (val == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) else if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /* Only do anything if we're changing state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (enable != stf_barrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) stf_barrier_enable(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static int stf_barrier_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) *val = stf_barrier ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) DEFINE_DEBUGFS_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static __init int stf_barrier_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) debugfs_create_file_unsafe("stf_barrier", 0600, powerpc_debugfs_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) NULL, &fops_stf_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) device_initcall(stf_barrier_debugfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static void update_branch_cache_flush(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) u32 *site;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) site = &patch__call_kvm_flush_link_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) // This controls the branch from guest_exit_cont to kvm_flush_link_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (link_stack_flush_type == BRANCH_CACHE_FLUSH_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) patch_instruction_site(site, ppc_inst(PPC_INST_NOP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) // Could use HW flush, but that could also flush count cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) patch_branch_site(site, (u64)&kvm_flush_link_stack, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) // Patch out the bcctr first, then nop the rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) site = &patch__call_flush_branch_caches3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) patch_instruction_site(site, ppc_inst(PPC_INST_NOP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) site = &patch__call_flush_branch_caches2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) patch_instruction_site(site, ppc_inst(PPC_INST_NOP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) site = &patch__call_flush_branch_caches1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) patch_instruction_site(site, ppc_inst(PPC_INST_NOP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) // This controls the branch from _switch to flush_branch_caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) link_stack_flush_type == BRANCH_CACHE_FLUSH_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) // Nothing to be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) } else if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) link_stack_flush_type == BRANCH_CACHE_FLUSH_HW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) // Patch in the bcctr last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) site = &patch__call_flush_branch_caches1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) patch_instruction_site(site, ppc_inst(0x39207fff)); // li r9,0x7fff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) site = &patch__call_flush_branch_caches2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) patch_instruction_site(site, ppc_inst(0x7d2903a6)); // mtctr r9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) site = &patch__call_flush_branch_caches3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) patch_instruction_site(site, ppc_inst(PPC_INST_BCCTR_FLUSH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) patch_branch_site(site, (u64)&flush_branch_caches, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) // If we just need to flush the link stack, early return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) patch_instruction_site(&patch__flush_link_stack_return,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) ppc_inst(PPC_INST_BLR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) // If we have flush instruction, early return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) } else if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) patch_instruction_site(&patch__flush_count_cache_return,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ppc_inst(PPC_INST_BLR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static void toggle_branch_cache_flush(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) count_cache_flush_type = BRANCH_CACHE_FLUSH_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) pr_info("count-cache-flush: flush disabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) count_cache_flush_type = BRANCH_CACHE_FLUSH_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) pr_info("count-cache-flush: hardware flush enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) count_cache_flush_type = BRANCH_CACHE_FLUSH_SW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) pr_info("count-cache-flush: software flush enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_LINK_STACK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (link_stack_flush_type != BRANCH_CACHE_FLUSH_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) link_stack_flush_type = BRANCH_CACHE_FLUSH_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) pr_info("link-stack-flush: flush disabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (security_ftr_enabled(SEC_FTR_BCCTR_LINK_FLUSH_ASSIST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) link_stack_flush_type = BRANCH_CACHE_FLUSH_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) pr_info("link-stack-flush: hardware flush enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) link_stack_flush_type = BRANCH_CACHE_FLUSH_SW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) pr_info("link-stack-flush: software flush enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) update_branch_cache_flush();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) void setup_count_cache_flush(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) bool enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (no_spectrev2 || cpu_mitigations_off()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) pr_warn("Spectre v2 mitigations not fully under software control, can't disable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * There's no firmware feature flag/hypervisor bit to tell us we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * flush the link stack on context switch. So we set it here if we see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * either of the Spectre v2 mitigations that aim to protect userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) security_ftr_set(SEC_FTR_FLUSH_LINK_STACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) toggle_branch_cache_flush(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static int count_cache_flush_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (val == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) else if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) toggle_branch_cache_flush(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static int count_cache_flush_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) *val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) *val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) DEFINE_DEBUGFS_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) count_cache_flush_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static __init int count_cache_flush_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) debugfs_create_file_unsafe("count_cache_flush", 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) powerpc_debugfs_root, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) &fops_count_cache_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) device_initcall(count_cache_flush_debugfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #endif /* CONFIG_PPC_BOOK3S_64 */