1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/device.h>
5 #include <asm/nospec-branch.h>
7 static int __init nobp_setup_early(char *str)
12 rc = kstrtobool(str, &enabled);
15 if (enabled && test_facility(82)) {
17 * The user explicitely requested nobp=1, enable it and
18 * disable the expoline support.
20 __set_facility(82, alt_stfle_fac_list);
21 if (IS_ENABLED(CONFIG_EXPOLINE))
24 __clear_facility(82, alt_stfle_fac_list);
28 early_param("nobp", nobp_setup_early);
30 static int __init nospec_setup_early(char *str)
32 __clear_facility(82, alt_stfle_fac_list);
35 early_param("nospec", nospec_setup_early);
37 static int __init nospec_report(void)
39 if (test_facility(156))
40 pr_info("Spectre V2 mitigation: etokens\n");
41 if (__is_defined(CC_USING_EXPOLINE) && !nospec_disable)
42 pr_info("Spectre V2 mitigation: execute trampolines\n");
43 if (__test_facility(82, alt_stfle_fac_list))
44 pr_info("Spectre V2 mitigation: limited branch prediction\n");
47 arch_initcall(nospec_report);
49 #ifdef CONFIG_EXPOLINE
51 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
53 static int __init nospectre_v2_setup_early(char *str)
58 early_param("nospectre_v2", nospectre_v2_setup_early);
60 void __init nospec_auto_detect(void)
62 if (test_facility(156) || cpu_mitigations_off()) {
64 * The machine supports etokens.
65 * Disable expolines and disable nobp.
67 if (__is_defined(CC_USING_EXPOLINE))
69 __clear_facility(82, alt_stfle_fac_list);
70 } else if (__is_defined(CC_USING_EXPOLINE)) {
72 * The kernel has been compiled with expolines.
73 * Keep expolines enabled and disable nobp.
76 __clear_facility(82, 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)) {
89 __clear_facility(82, alt_stfle_fac_list);
91 if (str && !strncmp(str, "off", 3))
93 if (str && !strncmp(str, "auto", 4))
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 static const u8 branch[] = { 0x47, 0x00, 0x07, 0x00 };
103 u8 *instr, *thunk, *br;
107 /* Second part of the instruction replace is always a nop */
108 for (epo = start; epo < end; epo++) {
109 instr = (u8 *) epo + *epo;
110 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
111 type = BRCL_EXPOLINE; /* brcl instruction */
112 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
113 type = BRASL_EXPOLINE; /* brasl instruction */
116 thunk = instr + (*(int *)(instr + 2)) * 2;
117 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
118 /* exrl %r0,<target-br> */
119 br = thunk + (*(int *)(thunk + 2)) * 2;
120 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
121 thunk[6] == 0x44 && thunk[7] == 0x00 &&
122 (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
123 (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
124 /* larl %rx,<target br> + ex %r0,0(%rx) */
125 br = thunk + (*(int *)(thunk + 2)) * 2;
128 /* Check for unconditional branch 0x07f? or 0x47f???? */
129 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
132 memcpy(insnbuf + 2, branch, sizeof(branch));
136 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
138 /* brcl to b, replace with bc + nopr */
142 /* brcl to br, replace with bcr + nop */
146 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
148 /* brasl to b, replace with bas + nopr */
153 /* brasl to br, replace with basr + nop */
159 s390_kernel_write(instr, insnbuf, 6);
163 void __init_or_module nospec_revert(s32 *start, s32 *end)
166 __nospec_revert(start, end);
169 extern s32 __nospec_call_start[], __nospec_call_end[];
170 extern s32 __nospec_return_start[], __nospec_return_end[];
171 void __init nospec_init_branches(void)
173 nospec_revert(__nospec_call_start, __nospec_call_end);
174 nospec_revert(__nospec_return_start, __nospec_return_end);
177 #endif /* CONFIG_EXPOLINE */