1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/module.h> 3 #include <linux/device.h> 4 #include <asm/nospec-branch.h> 5 6 static int __init nobp_setup_early(char *str) 7 { 8 bool enabled; 9 int rc; 10 11 rc = kstrtobool(str, &enabled); 12 if (rc) 13 return rc; 14 if (enabled && test_facility(82)) { 15 /* 16 * The user explicitely requested nobp=1, enable it and 17 * disable the expoline support. 18 */ 19 __set_facility(82, S390_lowcore.alt_stfle_fac_list); 20 if (IS_ENABLED(CONFIG_EXPOLINE)) 21 nospec_disable = 1; 22 } else { 23 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 24 } 25 return 0; 26 } 27 early_param("nobp", nobp_setup_early); 28 29 static int __init nospec_setup_early(char *str) 30 { 31 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 32 return 0; 33 } 34 early_param("nospec", nospec_setup_early); 35 36 static int __init nospec_report(void) 37 { 38 if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable) 39 pr_info("Spectre V2 mitigation: execute trampolines.\n"); 40 if (__test_facility(82, S390_lowcore.alt_stfle_fac_list)) 41 pr_info("Spectre V2 mitigation: limited branch prediction.\n"); 42 return 0; 43 } 44 arch_initcall(nospec_report); 45 46 #ifdef CONFIG_SYSFS 47 ssize_t cpu_show_spectre_v1(struct device *dev, 48 struct device_attribute *attr, char *buf) 49 { 50 return sprintf(buf, "Mitigation: __user pointer sanitization\n"); 51 } 52 53 ssize_t cpu_show_spectre_v2(struct device *dev, 54 struct device_attribute *attr, char *buf) 55 { 56 if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable) 57 return sprintf(buf, "Mitigation: execute trampolines\n"); 58 if (__test_facility(82, S390_lowcore.alt_stfle_fac_list)) 59 return sprintf(buf, "Mitigation: limited branch prediction.\n"); 60 return sprintf(buf, "Vulnerable\n"); 61 } 62 #endif 63 64 #ifdef CONFIG_EXPOLINE 65 66 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF); 67 68 static int __init nospectre_v2_setup_early(char *str) 69 { 70 nospec_disable = 1; 71 return 0; 72 } 73 early_param("nospectre_v2", nospectre_v2_setup_early); 74 75 void __init nospec_auto_detect(void) 76 { 77 if (IS_ENABLED(CC_USING_EXPOLINE)) { 78 /* 79 * The kernel has been compiled with expolines. 80 * Keep expolines enabled and disable nobp. 81 */ 82 nospec_disable = 0; 83 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 84 } 85 /* 86 * If the kernel has not been compiled with expolines the 87 * nobp setting decides what is done, this depends on the 88 * CONFIG_KERNEL_NP option and the nobp/nospec parameters. 89 */ 90 } 91 92 static int __init spectre_v2_setup_early(char *str) 93 { 94 if (str && !strncmp(str, "on", 2)) { 95 nospec_disable = 0; 96 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 97 } 98 if (str && !strncmp(str, "off", 3)) 99 nospec_disable = 1; 100 if (str && !strncmp(str, "auto", 4)) 101 nospec_auto_detect(); 102 return 0; 103 } 104 early_param("spectre_v2", spectre_v2_setup_early); 105 106 static void __init_or_module __nospec_revert(s32 *start, s32 *end) 107 { 108 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type; 109 u8 *instr, *thunk, *br; 110 u8 insnbuf[6]; 111 s32 *epo; 112 113 /* Second part of the instruction replace is always a nop */ 114 memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x00, 0x00 }, 4); 115 for (epo = start; epo < end; epo++) { 116 instr = (u8 *) epo + *epo; 117 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04) 118 type = BRCL_EXPOLINE; /* brcl instruction */ 119 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05) 120 type = BRASL_EXPOLINE; /* brasl instruction */ 121 else 122 continue; 123 thunk = instr + (*(int *)(instr + 2)) * 2; 124 if (thunk[0] == 0xc6 && thunk[1] == 0x00) 125 /* exrl %r0,<target-br> */ 126 br = thunk + (*(int *)(thunk + 2)) * 2; 127 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 && 128 thunk[6] == 0x44 && thunk[7] == 0x00 && 129 (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 && 130 (thunk[1] & 0xf0) == (thunk[8] & 0xf0)) 131 /* larl %rx,<target br> + ex %r0,0(%rx) */ 132 br = thunk + (*(int *)(thunk + 2)) * 2; 133 else 134 continue; 135 if (br[0] != 0x07 || (br[1] & 0xf0) != 0xf0) 136 continue; 137 switch (type) { 138 case BRCL_EXPOLINE: 139 /* brcl to thunk, replace with br + nop */ 140 insnbuf[0] = br[0]; 141 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 142 break; 143 case BRASL_EXPOLINE: 144 /* brasl to thunk, replace with basr + nop */ 145 insnbuf[0] = 0x0d; 146 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 147 break; 148 } 149 150 s390_kernel_write(instr, insnbuf, 6); 151 } 152 } 153 154 void __init_or_module nospec_revert(s32 *start, s32 *end) 155 { 156 if (nospec_disable) 157 __nospec_revert(start, end); 158 } 159 160 extern s32 __nospec_call_start[], __nospec_call_end[]; 161 extern s32 __nospec_return_start[], __nospec_return_end[]; 162 void __init nospec_init_branches(void) 163 { 164 nospec_revert(__nospec_call_start, __nospec_call_end); 165 nospec_revert(__nospec_return_start, __nospec_return_end); 166 } 167 168 #endif /* CONFIG_EXPOLINE */ 169