GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / s390 / kernel / nospec-branch.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/device.h>
4 #include <asm/facility.h>
5 #include <asm/nospec-branch.h>
6
7 static 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                  * The user explicitely requested nobp=1, enable it and
18                  * disable the expoline support.
19                  */
20                 __set_facility(82, S390_lowcore.alt_stfle_fac_list);
21                 if (IS_ENABLED(CONFIG_EXPOLINE))
22                         nospec_disable = 1;
23         } else {
24                 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
25         }
26         return 0;
27 }
28 early_param("nobp", nobp_setup_early);
29
30 static int __init nospec_setup_early(char *str)
31 {
32         __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
33         return 0;
34 }
35 early_param("nospec", nospec_setup_early);
36
37 static int __init nospec_report(void)
38 {
39         if (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable)
40                 pr_info("Spectre V2 mitigation: execute trampolines.\n");
41         if (__test_facility(82, S390_lowcore.alt_stfle_fac_list))
42                 pr_info("Spectre V2 mitigation: limited branch prediction.\n");
43         return 0;
44 }
45 arch_initcall(nospec_report);
46
47 #ifdef CONFIG_EXPOLINE
48
49 int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
50
51 static int __init nospectre_v2_setup_early(char *str)
52 {
53         nospec_disable = 1;
54         return 0;
55 }
56 early_param("nospectre_v2", nospectre_v2_setup_early);
57
58 void __init nospec_auto_detect(void)
59 {
60         if (IS_ENABLED(CC_USING_EXPOLINE)) {
61                 /*
62                  * The kernel has been compiled with expolines.
63                  * Keep expolines enabled and disable nobp.
64                  */
65                 nospec_disable = 0;
66                 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
67         }
68         /*
69          * If the kernel has not been compiled with expolines the
70          * nobp setting decides what is done, this depends on the
71          * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
72          */
73 }
74
75 static int __init spectre_v2_setup_early(char *str)
76 {
77         if (str && !strncmp(str, "on", 2)) {
78                 nospec_disable = 0;
79                 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
80         }
81         if (str && !strncmp(str, "off", 3))
82                 nospec_disable = 1;
83         if (str && !strncmp(str, "auto", 4))
84                 nospec_auto_detect();
85         return 0;
86 }
87 early_param("spectre_v2", spectre_v2_setup_early);
88
89 static void __init_or_module __nospec_revert(s32 *start, s32 *end)
90 {
91         enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
92         u8 *instr, *thunk, *br;
93         u8 insnbuf[6];
94         s32 *epo;
95
96         /* Second part of the instruction replace is always a nop */
97         for (epo = start; epo < end; epo++) {
98                 instr = (u8 *) epo + *epo;
99                 if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
100                         type = BRCL_EXPOLINE;   /* brcl instruction */
101                 else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
102                         type = BRASL_EXPOLINE;  /* brasl instruction */
103                 else
104                         continue;
105                 thunk = instr + (*(int *)(instr + 2)) * 2;
106                 if (thunk[0] == 0xc6 && thunk[1] == 0x00)
107                         /* exrl %r0,<target-br> */
108                         br = thunk + (*(int *)(thunk + 2)) * 2;
109                 else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
110                          thunk[6] == 0x44 && thunk[7] == 0x00 &&
111                          (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
112                          (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
113                         /* larl %rx,<target br> + ex %r0,0(%rx) */
114                         br = thunk + (*(int *)(thunk + 2)) * 2;
115                 else
116                         continue;
117                 /* Check for unconditional branch 0x07f? or 0x47f???? */
118                 if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
119                         continue;
120
121                 memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4);
122                 switch (type) {
123                 case BRCL_EXPOLINE:
124                         insnbuf[0] = br[0];
125                         insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
126                         if (br[0] == 0x47) {
127                                 /* brcl to b, replace with bc + nopr */
128                                 insnbuf[2] = br[2];
129                                 insnbuf[3] = br[3];
130                         } else {
131                                 /* brcl to br, replace with bcr + nop */
132                         }
133                         break;
134                 case BRASL_EXPOLINE:
135                         insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
136                         if (br[0] == 0x47) {
137                                 /* brasl to b, replace with bas + nopr */
138                                 insnbuf[0] = 0x4d;
139                                 insnbuf[2] = br[2];
140                                 insnbuf[3] = br[3];
141                         } else {
142                                 /* brasl to br, replace with basr + nop */
143                                 insnbuf[0] = 0x0d;
144                         }
145                         break;
146                 }
147
148                 s390_kernel_write(instr, insnbuf, 6);
149         }
150 }
151
152 void __init_or_module nospec_revert(s32 *start, s32 *end)
153 {
154         if (nospec_disable)
155                 __nospec_revert(start, end);
156 }
157
158 extern s32 __nospec_call_start[], __nospec_call_end[];
159 extern s32 __nospec_return_start[], __nospec_return_end[];
160 void __init nospec_init_branches(void)
161 {
162         nospec_revert(__nospec_call_start, __nospec_call_end);
163         nospec_revert(__nospec_return_start, __nospec_return_end);
164 }
165
166 #endif /* CONFIG_EXPOLINE */