0001
0002 #include <linux/module.h>
0003 #include <linux/device.h>
0004 #include <linux/cpu.h>
0005 #include <asm/nospec-branch.h>
0006
0007 static int __init nobp_setup_early(char *str)
0008 {
0009 bool enabled;
0010 int rc;
0011
0012 rc = kstrtobool(str, &enabled);
0013 if (rc)
0014 return rc;
0015 if (enabled && test_facility(82)) {
0016
0017
0018
0019
0020 __set_facility(82, alt_stfle_fac_list);
0021 if (IS_ENABLED(CONFIG_EXPOLINE))
0022 nospec_disable = 1;
0023 } else {
0024 __clear_facility(82, alt_stfle_fac_list);
0025 }
0026 return 0;
0027 }
0028 early_param("nobp", nobp_setup_early);
0029
0030 static int __init nospec_setup_early(char *str)
0031 {
0032 __clear_facility(82, alt_stfle_fac_list);
0033 return 0;
0034 }
0035 early_param("nospec", nospec_setup_early);
0036
0037 static int __init nospec_report(void)
0038 {
0039 if (test_facility(156))
0040 pr_info("Spectre V2 mitigation: etokens\n");
0041 if (nospec_uses_trampoline())
0042 pr_info("Spectre V2 mitigation: execute trampolines\n");
0043 if (__test_facility(82, alt_stfle_fac_list))
0044 pr_info("Spectre V2 mitigation: limited branch prediction\n");
0045 return 0;
0046 }
0047 arch_initcall(nospec_report);
0048
0049 #ifdef CONFIG_EXPOLINE
0050
0051 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
0052
0053 static int __init nospectre_v2_setup_early(char *str)
0054 {
0055 nospec_disable = 1;
0056 return 0;
0057 }
0058 early_param("nospectre_v2", nospectre_v2_setup_early);
0059
0060 void __init nospec_auto_detect(void)
0061 {
0062 if (test_facility(156) || cpu_mitigations_off()) {
0063
0064
0065
0066
0067 if (__is_defined(CC_USING_EXPOLINE))
0068 nospec_disable = 1;
0069 __clear_facility(82, alt_stfle_fac_list);
0070 } else if (__is_defined(CC_USING_EXPOLINE)) {
0071
0072
0073
0074
0075 nospec_disable = 0;
0076 __clear_facility(82, alt_stfle_fac_list);
0077 }
0078
0079
0080
0081
0082
0083 }
0084
0085 static int __init spectre_v2_setup_early(char *str)
0086 {
0087 if (str && !strncmp(str, "on", 2)) {
0088 nospec_disable = 0;
0089 __clear_facility(82, alt_stfle_fac_list);
0090 }
0091 if (str && !strncmp(str, "off", 3))
0092 nospec_disable = 1;
0093 if (str && !strncmp(str, "auto", 4))
0094 nospec_auto_detect();
0095 return 0;
0096 }
0097 early_param("spectre_v2", spectre_v2_setup_early);
0098
0099 static void __init_or_module __nospec_revert(s32 *start, s32 *end)
0100 {
0101 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
0102 static const u8 branch[] = { 0x47, 0x00, 0x07, 0x00 };
0103 u8 *instr, *thunk, *br;
0104 u8 insnbuf[6];
0105 s32 *epo;
0106
0107
0108 memcpy(insnbuf + 2, branch, sizeof(branch));
0109 for (epo = start; epo < end; epo++) {
0110 instr = (u8 *) epo + *epo;
0111 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
0112 type = BRCL_EXPOLINE;
0113 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
0114 type = BRASL_EXPOLINE;
0115 else
0116 continue;
0117 thunk = instr + (*(int *)(instr + 2)) * 2;
0118 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
0119
0120 br = thunk + (*(int *)(thunk + 2)) * 2;
0121 else
0122 continue;
0123 if (br[0] != 0x07 || (br[1] & 0xf0) != 0xf0)
0124 continue;
0125 switch (type) {
0126 case BRCL_EXPOLINE:
0127
0128 insnbuf[0] = br[0];
0129 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
0130 break;
0131 case BRASL_EXPOLINE:
0132
0133 insnbuf[0] = 0x0d;
0134 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
0135 break;
0136 }
0137
0138 s390_kernel_write(instr, insnbuf, 6);
0139 }
0140 }
0141
0142 void __init_or_module nospec_revert(s32 *start, s32 *end)
0143 {
0144 if (nospec_disable)
0145 __nospec_revert(start, end);
0146 }
0147
0148 extern s32 __nospec_call_start[], __nospec_call_end[];
0149 extern s32 __nospec_return_start[], __nospec_return_end[];
0150 void __init nospec_init_branches(void)
0151 {
0152 nospec_revert(__nospec_call_start, __nospec_call_end);
0153 nospec_revert(__nospec_return_start, __nospec_return_end);
0154 }
0155
0156 #endif