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 (test_facility(156)) 39 pr_info("Spectre V2 mitigation: etokens\n"); 40 if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable) 41 pr_info("Spectre V2 mitigation: execute trampolines\n"); 42 if (__test_facility(82, S390_lowcore.alt_stfle_fac_list)) 43 pr_info("Spectre V2 mitigation: limited branch prediction\n"); 44 return 0; 45 } 46 arch_initcall(nospec_report); 47 48 #ifdef CONFIG_EXPOLINE 49 50 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF); 51 52 static int __init nospectre_v2_setup_early(char *str) 53 { 54 nospec_disable = 1; 55 return 0; 56 } 57 early_param("nospectre_v2", nospectre_v2_setup_early); 58 59 void __init nospec_auto_detect(void) 60 { 61 if (test_facility(156)) { 62 /* 63 * The machine supports etokens. 64 * Disable expolines and disable nobp. 65 */ 66 if (IS_ENABLED(CC_USING_EXPOLINE)) 67 nospec_disable = 1; 68 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 69 } else if (IS_ENABLED(CC_USING_EXPOLINE)) { 70 /* 71 * The kernel has been compiled with expolines. 72 * Keep expolines enabled and disable nobp. 73 */ 74 nospec_disable = 0; 75 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 76 } 77 /* 78 * If the kernel has not been compiled with expolines the 79 * nobp setting decides what is done, this depends on the 80 * CONFIG_KERNEL_NP option and the nobp/nospec parameters. 81 */ 82 } 83 84 static int __init spectre_v2_setup_early(char *str) 85 { 86 if (str && !strncmp(str, "on", 2)) { 87 nospec_disable = 0; 88 __clear_facility(82, S390_lowcore.alt_stfle_fac_list); 89 } 90 if (str && !strncmp(str, "off", 3)) 91 nospec_disable = 1; 92 if (str && !strncmp(str, "auto", 4)) 93 nospec_auto_detect(); 94 return 0; 95 } 96 early_param("spectre_v2", spectre_v2_setup_early); 97 98 static void __init_or_module __nospec_revert(s32 *start, s32 *end) 99 { 100 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type; 101 u8 *instr, *thunk, *br; 102 u8 insnbuf[6]; 103 s32 *epo; 104 105 /* Second part of the instruction replace is always a nop */ 106 for (epo = start; epo < end; epo++) { 107 instr = (u8 *) epo + *epo; 108 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04) 109 type = BRCL_EXPOLINE; /* brcl instruction */ 110 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05) 111 type = BRASL_EXPOLINE; /* brasl instruction */ 112 else 113 continue; 114 thunk = instr + (*(int *)(instr + 2)) * 2; 115 if (thunk[0] == 0xc6 && thunk[1] == 0x00) 116 /* exrl %r0,<target-br> */ 117 br = thunk + (*(int *)(thunk + 2)) * 2; 118 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 && 119 thunk[6] == 0x44 && thunk[7] == 0x00 && 120 (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 && 121 (thunk[1] & 0xf0) == (thunk[8] & 0xf0)) 122 /* larl %rx,<target br> + ex %r0,0(%rx) */ 123 br = thunk + (*(int *)(thunk + 2)) * 2; 124 else 125 continue; 126 /* Check for unconditional branch 0x07f? or 0x47f???? */ 127 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0) 128 continue; 129 130 memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4); 131 switch (type) { 132 case BRCL_EXPOLINE: 133 insnbuf[0] = br[0]; 134 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 135 if (br[0] == 0x47) { 136 /* brcl to b, replace with bc + nopr */ 137 insnbuf[2] = br[2]; 138 insnbuf[3] = br[3]; 139 } else { 140 /* brcl to br, replace with bcr + nop */ 141 } 142 break; 143 case BRASL_EXPOLINE: 144 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f); 145 if (br[0] == 0x47) { 146 /* brasl to b, replace with bas + nopr */ 147 insnbuf[0] = 0x4d; 148 insnbuf[2] = br[2]; 149 insnbuf[3] = br[3]; 150 } else { 151 /* brasl to br, replace with basr + nop */ 152 insnbuf[0] = 0x0d; 153 } 154 break; 155 } 156 157 s390_kernel_write(instr, insnbuf, 6); 158 } 159 } 160 161 void __init_or_module nospec_revert(s32 *start, s32 *end) 162 { 163 if (nospec_disable) 164 __nospec_revert(start, end); 165 } 166 167 extern s32 __nospec_call_start[], __nospec_call_end[]; 168 extern s32 __nospec_return_start[], __nospec_return_end[]; 169 void __init nospec_init_branches(void) 170 { 171 nospec_revert(__nospec_call_start, __nospec_call_end); 172 nospec_revert(__nospec_return_start, __nospec_return_end); 173 } 174 175 #endif /* CONFIG_EXPOLINE */ 176