mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / s390 / kernel / nospec-branch.c
blob6956902bba122a7dbe553ca2a10fae56842ca8b4
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/device.h>
4 #include <linux/cpu.h>
5 #include <asm/facility.h>
6 #include <asm/nospec-branch.h>
8 static int __init nobp_setup_early(char *str)
10 bool enabled;
11 int rc;
13 rc = kstrtobool(str, &enabled);
14 if (rc)
15 return rc;
16 if (enabled && test_facility(82)) {
18 * The user explicitely requested nobp=1, enable it and
19 * disable the expoline support.
21 __set_facility(82, S390_lowcore.alt_stfle_fac_list);
22 if (IS_ENABLED(CONFIG_EXPOLINE))
23 nospec_disable = 1;
24 } else {
25 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
27 return 0;
29 early_param("nobp", nobp_setup_early);
31 static int __init nospec_setup_early(char *str)
33 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
34 return 0;
36 early_param("nospec", nospec_setup_early);
38 static int __init nospec_report(void)
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;
46 arch_initcall(nospec_report);
48 #ifdef CONFIG_EXPOLINE
50 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
52 static int __init nospectre_v2_setup_early(char *str)
54 nospec_disable = 1;
55 return 0;
57 early_param("nospectre_v2", nospectre_v2_setup_early);
60 void __init nospec_auto_detect(void)
62 if (cpu_mitigations_off()) {
64 * Disable expolines and disable nobp.
66 if (IS_ENABLED(CC_USING_EXPOLINE))
67 nospec_disable = 1;
68 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
70 if (IS_ENABLED(CC_USING_EXPOLINE)) {
72 * The kernel has been compiled with expolines.
73 * Keep expolines enabled and disable nobp.
75 nospec_disable = 0;
76 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
79 * If the kernel has not been compiled with expolines the
80 * nobp setting decides what is done, this depends on the
81 * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
85 static int __init spectre_v2_setup_early(char *str)
87 if (str && !strncmp(str, "on", 2)) {
88 nospec_disable = 0;
89 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
91 if (str && !strncmp(str, "off", 3))
92 nospec_disable = 1;
93 if (str && !strncmp(str, "auto", 4))
94 nospec_auto_detect();
95 return 0;
97 early_param("spectre_v2", spectre_v2_setup_early);
99 static void __init_or_module __nospec_revert(s32 *start, s32 *end)
101 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
102 u8 *instr, *thunk, *br;
103 u8 insnbuf[6];
104 s32 *epo;
106 /* Second part of the instruction replace is always a nop */
107 for (epo = start; epo < end; epo++) {
108 instr = (u8 *) epo + *epo;
109 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
110 type = BRCL_EXPOLINE; /* brcl instruction */
111 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
112 type = BRASL_EXPOLINE; /* brasl instruction */
113 else
114 continue;
115 thunk = instr + (*(int *)(instr + 2)) * 2;
116 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
117 /* exrl %r0,<target-br> */
118 br = thunk + (*(int *)(thunk + 2)) * 2;
119 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
120 thunk[6] == 0x44 && thunk[7] == 0x00 &&
121 (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
122 (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
123 /* larl %rx,<target br> + ex %r0,0(%rx) */
124 br = thunk + (*(int *)(thunk + 2)) * 2;
125 else
126 continue;
127 /* Check for unconditional branch 0x07f? or 0x47f???? */
128 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
129 continue;
131 memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4);
132 switch (type) {
133 case BRCL_EXPOLINE:
134 insnbuf[0] = br[0];
135 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
136 if (br[0] == 0x47) {
137 /* brcl to b, replace with bc + nopr */
138 insnbuf[2] = br[2];
139 insnbuf[3] = br[3];
140 } else {
141 /* brcl to br, replace with bcr + nop */
143 break;
144 case BRASL_EXPOLINE:
145 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
146 if (br[0] == 0x47) {
147 /* brasl to b, replace with bas + nopr */
148 insnbuf[0] = 0x4d;
149 insnbuf[2] = br[2];
150 insnbuf[3] = br[3];
151 } else {
152 /* brasl to br, replace with basr + nop */
153 insnbuf[0] = 0x0d;
155 break;
158 s390_kernel_write(instr, insnbuf, 6);
162 void __init_or_module nospec_revert(s32 *start, s32 *end)
164 if (nospec_disable)
165 __nospec_revert(start, end);
168 extern s32 __nospec_call_start[], __nospec_call_end[];
169 extern s32 __nospec_return_start[], __nospec_return_end[];
170 void __init nospec_init_branches(void)
172 nospec_revert(__nospec_call_start, __nospec_call_end);
173 nospec_revert(__nospec_return_start, __nospec_return_end);
176 #endif /* CONFIG_EXPOLINE */