1
2#include <linux/module.h>
3#include <linux/device.h>
4#include <linux/cpu.h>
5#include <asm/nospec-branch.h>
6
7static int __init nobp_setup_early(char *str)
8{
9 bool enabled;
10 int rc;
11
12 rc = kstrtobool(str, &enabled);
13 if (rc)
14 return rc;
15 if (enabled && test_facility(82)) {
16
17
18
19
20 __set_facility(82, alt_stfle_fac_list);
21 if (IS_ENABLED(CONFIG_EXPOLINE))
22 nospec_disable = 1;
23 } else {
24 __clear_facility(82, alt_stfle_fac_list);
25 }
26 return 0;
27}
28early_param("nobp", nobp_setup_early);
29
30static int __init nospec_setup_early(char *str)
31{
32 __clear_facility(82, alt_stfle_fac_list);
33 return 0;
34}
35early_param("nospec", nospec_setup_early);
36
37static int __init nospec_report(void)
38{
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");
45 return 0;
46}
47arch_initcall(nospec_report);
48
49#ifdef CONFIG_EXPOLINE
50
51int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
52
53static int __init nospectre_v2_setup_early(char *str)
54{
55 nospec_disable = 1;
56 return 0;
57}
58early_param("nospectre_v2", nospectre_v2_setup_early);
59
60void __init nospec_auto_detect(void)
61{
62 if (test_facility(156) || cpu_mitigations_off()) {
63
64
65
66
67 if (__is_defined(CC_USING_EXPOLINE))
68 nospec_disable = 1;
69 __clear_facility(82, alt_stfle_fac_list);
70 } else if (__is_defined(CC_USING_EXPOLINE)) {
71
72
73
74
75 nospec_disable = 0;
76 __clear_facility(82, alt_stfle_fac_list);
77 }
78
79
80
81
82
83}
84
85static int __init spectre_v2_setup_early(char *str)
86{
87 if (str && !strncmp(str, "on", 2)) {
88 nospec_disable = 0;
89 __clear_facility(82, alt_stfle_fac_list);
90 }
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;
96}
97early_param("spectre_v2", spectre_v2_setup_early);
98
99static void __init_or_module __nospec_revert(s32 *start, s32 *end)
100{
101 enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
102 static const u8 branch[] = { 0x47, 0x00, 0x07, 0x00 };
103 u8 *instr, *thunk, *br;
104 u8 insnbuf[6];
105 s32 *epo;
106
107
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;
112 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
113 type = BRASL_EXPOLINE;
114 else
115 continue;
116 thunk = instr + (*(int *)(instr + 2)) * 2;
117 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
118
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
125 br = thunk + (*(int *)(thunk + 2)) * 2;
126 else
127 continue;
128
129 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
130 continue;
131
132 memcpy(insnbuf + 2, branch, sizeof(branch));
133 switch (type) {
134 case BRCL_EXPOLINE:
135 insnbuf[0] = br[0];
136 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
137 if (br[0] == 0x47) {
138
139 insnbuf[2] = br[2];
140 insnbuf[3] = br[3];
141 } else {
142
143 }
144 break;
145 case BRASL_EXPOLINE:
146 insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
147 if (br[0] == 0x47) {
148
149 insnbuf[0] = 0x4d;
150 insnbuf[2] = br[2];
151 insnbuf[3] = br[3];
152 } else {
153
154 insnbuf[0] = 0x0d;
155 }
156 break;
157 }
158
159 s390_kernel_write(instr, insnbuf, 6);
160 }
161}
162
163void __init_or_module nospec_revert(s32 *start, s32 *end)
164{
165 if (nospec_disable)
166 __nospec_revert(start, end);
167}
168
169extern s32 __nospec_call_start[], __nospec_call_end[];
170extern s32 __nospec_return_start[], __nospec_return_end[];
171void __init nospec_init_branches(void)
172{
173 nospec_revert(__nospec_call_start, __nospec_call_end);
174 nospec_revert(__nospec_return_start, __nospec_return_end);
175}
176
177#endif
178