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