Linux Audio

Check our new training course

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