1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/device.h>
4 #include <asm/nospec-branch.h>
6 static int __init nobp_setup_early(char *str)
11 rc = kstrtobool(str, &enabled);
14 if (enabled && test_facility(82)) {
16 * The user explicitely requested nobp=1, enable it and
17 * disable the expoline support.
19 __set_facility(82, S390_lowcore.alt_stfle_fac_list);
20 if (IS_ENABLED(CONFIG_EXPOLINE))
23 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
27 early_param("nobp", nobp_setup_early);
29 static int __init nospec_setup_early(char *str)
31 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
34 early_param("nospec", nospec_setup_early);
36 static int __init nospec_report(void)
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");
44 arch_initcall(nospec_report);
46 #ifdef CONFIG_EXPOLINE
48 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
50 static int __init nospectre_v2_setup_early(char *str)
55 early_param("nospectre_v2", nospectre_v2_setup_early);
57 void __init nospec_auto_detect(void)
59 if (IS_ENABLED(CC_USING_EXPOLINE)) {
61 * The kernel has been compiled with expolines.
62 * Keep expolines enabled and disable nobp.
65 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
68 * If the kernel has not been compiled with expolines the
69 * nobp setting decides what is done, this depends on the
70 * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
74 static int __init spectre_v2_setup_early(char *str)
76 if (str && !strncmp(str, "on", 2)) {
78 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
80 if (str && !strncmp(str, "off", 3))
82 if (str && !strncmp(str, "auto", 4))
86 early_param("spectre_v2", spectre_v2_setup_early);
88 static void __init_or_module __nospec_revert(s32 *start, s32 *end)
90 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
91 u8 *instr, *thunk, *br;
95 /* Second part of the instruction replace is always a nop */
96 for (epo = start; epo < end; epo++) {
97 instr = (u8 *) epo + *epo;
98 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
99 type = BRCL_EXPOLINE; /* brcl instruction */
100 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
101 type = BRASL_EXPOLINE; /* brasl instruction */
104 thunk = instr + (*(int *)(instr + 2)) * 2;
105 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
106 /* exrl %r0,<target-br> */
107 br = thunk + (*(int *)(thunk + 2)) * 2;
108 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
109 thunk[6] == 0x44 && thunk[7] == 0x00 &&
110 (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
111 (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
112 /* larl %rx,<target br> + ex %r0,0(%rx) */
113 br = thunk + (*(int *)(thunk + 2)) * 2;
116 /* Check for unconditional branch 0x07f? or 0x47f???? */
117 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
120 memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4);
124 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
126 /* brcl to b, replace with bc + nopr */
130 /* brcl to br, replace with bcr + nop */
134 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
136 /* brasl to b, replace with bas + nopr */
141 /* brasl to br, replace with basr + nop */
147 s390_kernel_write(instr, insnbuf, 6);
151 void __init_or_module nospec_revert(s32 *start, s32 *end)
154 __nospec_revert(start, end);
157 extern s32 __nospec_call_start[], __nospec_call_end[];
158 extern s32 __nospec_return_start[], __nospec_return_end[];
159 void __init nospec_init_branches(void)
161 nospec_revert(__nospec_call_start, __nospec_call_end);
162 nospec_revert(__nospec_return_start, __nospec_return_end);
165 #endif /* CONFIG_EXPOLINE */