1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/device.h>
5 #include <asm/nospec-branch.h>
7 int nobp = IS_ENABLED(CONFIG_KERNEL_NOBP);
9 static int __init nobp_setup_early(char *str)
14 rc = kstrtobool(str, &enabled);
17 if (enabled && test_facility(82)) {
19 * The user explicitly requested nobp=1, enable it and
20 * disable the expoline support.
23 if (IS_ENABLED(CONFIG_EXPOLINE))
30 early_param("nobp", nobp_setup_early);
32 static int __init nospec_setup_early(char *str)
37 early_param("nospec", nospec_setup_early);
39 static int __init nospec_report(void)
41 if (test_facility(156))
42 pr_info("Spectre V2 mitigation: etokens\n");
43 if (nospec_uses_trampoline())
44 pr_info("Spectre V2 mitigation: execute trampolines\n");
46 pr_info("Spectre V2 mitigation: limited branch prediction\n");
49 arch_initcall(nospec_report);
51 #ifdef CONFIG_EXPOLINE
53 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
55 static int __init nospectre_v2_setup_early(char *str)
60 early_param("nospectre_v2", nospectre_v2_setup_early);
62 void __init nospec_auto_detect(void)
64 if (test_facility(156) || cpu_mitigations_off()) {
66 * The machine supports etokens.
67 * Disable expolines and disable nobp.
69 if (__is_defined(CC_USING_EXPOLINE))
72 } else if (__is_defined(CC_USING_EXPOLINE)) {
74 * The kernel has been compiled with expolines.
75 * Keep expolines enabled and disable nobp.
81 * If the kernel has not been compiled with expolines the
82 * nobp setting decides what is done, this depends on the
83 * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
87 static int __init spectre_v2_setup_early(char *str)
89 if (str && !strncmp(str, "on", 2)) {
93 if (str && !strncmp(str, "off", 3))
95 if (str && !strncmp(str, "auto", 4))
99 early_param("spectre_v2", spectre_v2_setup_early);
101 static void __init_or_module __nospec_revert(s32 *start, s32 *end)
103 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
104 static const u8 branch[] = { 0x47, 0x00, 0x07, 0x00 };
105 u8 *instr, *thunk, *br;
109 /* Second part of the instruction replace is always a nop */
110 memcpy(insnbuf + 2, branch, sizeof(branch));
111 for (epo = start; epo < end; epo++) {
112 instr = (u8 *) epo + *epo;
113 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
114 type = BRCL_EXPOLINE; /* brcl instruction */
115 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
116 type = BRASL_EXPOLINE; /* brasl instruction */
119 thunk = instr + (long)(*(int *)(instr + 2)) * 2;
120 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
121 /* exrl %r0,<target-br> */
122 br = thunk + (long)(*(int *)(thunk + 2)) * 2;
125 if (br[0] != 0x07 || (br[1] & 0xf0) != 0xf0)
129 /* brcl to thunk, replace with br + nop */
131 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
134 /* brasl to thunk, replace with basr + nop */
136 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
140 s390_kernel_write(instr, insnbuf, 6);
144 void __init_or_module nospec_revert(s32 *start, s32 *end)
147 __nospec_revert(start, end);
150 extern s32 __nospec_call_start[], __nospec_call_end[];
151 extern s32 __nospec_return_start[], __nospec_return_end[];
152 void __init nospec_init_branches(void)
154 nospec_revert(__nospec_call_start, __nospec_call_end);
155 nospec_revert(__nospec_return_start, __nospec_return_end);
158 #endif /* CONFIG_EXPOLINE */