alternative.h 7.9 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4
#ifndef __ASM_ALTERNATIVE_H
#define __ASM_ALTERNATIVE_H

5
#include <asm/cpucaps.h>
6
#include <asm/insn.h>
7

8 9
#define ARM64_CB_PATCH ARM64_NCAPS

10 11
#ifndef __ASSEMBLY__

12
#include <linux/init.h>
13 14 15 16 17 18 19 20 21 22 23 24
#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/stringify.h>

struct alt_instr {
	s32 orig_offset;	/* offset to original instruction */
	s32 alt_offset;		/* offset to replacement instruction */
	u16 cpufeature;		/* cpufeature bit set for replacement */
	u8  orig_len;		/* size of original instruction(s) */
	u8  alt_len;		/* size of new instruction(s), <= orig_len */
};

25 26 27
typedef void (*alternative_cb_t)(struct alt_instr *alt,
				 __le32 *origptr, __le32 *updptr, int nr_inst);

28
void __init apply_boot_alternatives(void);
29
void __init apply_alternatives_all(void);
30
bool alternative_is_applied(u16 cpufeature);
31 32 33 34 35 36

#ifdef CONFIG_MODULES
void apply_alternatives_module(void *start, size_t length);
#else
static inline void apply_alternatives_module(void *start, size_t length) { }
#endif
37

38
#define ALTINSTR_ENTRY(feature)					              \
39 40
	" .word 661b - .\n"				/* label           */ \
	" .word 663f - .\n"				/* new instruction */ \
41 42 43 44 45 46
	" .hword " __stringify(feature) "\n"		/* feature bit     */ \
	" .byte 662b-661b\n"				/* source len      */ \
	" .byte 664f-663f\n"				/* replacement len */

#define ALTINSTR_ENTRY_CB(feature, cb)					      \
	" .word 661b - .\n"				/* label           */ \
47
	" .word " __stringify(cb) "- .\n"		/* callback */	      \
48 49 50 51
	" .hword " __stringify(feature) "\n"		/* feature bit     */ \
	" .byte 662b-661b\n"				/* source len      */ \
	" .byte 664f-663f\n"				/* replacement len */

52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * alternative assembly primitive:
 *
 * If any of these .org directive fail, it means that insn1 and insn2
 * don't have the same length. This used to be written as
 *
 * .if ((664b-663b) != (662b-661b))
 * 	.error "Alternatives instruction length mismatch"
 * .endif
 *
 * but most assemblers die if insn1 or insn2 have a .inst. This should
 * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
 * containing commit 4e4d08cf7399b606 or c1baaddf8861).
65 66
 *
 * Alternatives with callbacks do not generate replacement instructions.
67
 */
68
#define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled)	\
69
	".if "__stringify(cfg_enabled)" == 1\n"				\
70 71 72 73
	"661:\n\t"							\
	oldinstr "\n"							\
	"662:\n"							\
	".pushsection .altinstructions,\"a\"\n"				\
74
	ALTINSTR_ENTRY(feature)						\
75 76 77 78 79 80
	".popsection\n"							\
	".pushsection .altinstr_replacement, \"a\"\n"			\
	"663:\n\t"							\
	newinstr "\n"							\
	"664:\n\t"							\
	".popsection\n\t"						\
81
	".org	. - (664b-663b) + (662b-661b)\n\t"			\
82
	".org	. - (662b-661b) + (664b-663b)\n"			\
83 84 85 86 87 88 89 90 91 92
	".endif\n"

#define __ALTERNATIVE_CFG_CB(oldinstr, feature, cfg_enabled, cb)	\
	".if "__stringify(cfg_enabled)" == 1\n"				\
	"661:\n\t"							\
	oldinstr "\n"							\
	"662:\n"							\
	".pushsection .altinstructions,\"a\"\n"				\
	ALTINSTR_ENTRY_CB(feature, cb)					\
	".popsection\n"							\
93 94
	"663:\n\t"							\
	"664:\n\t"							\
95 96 97
	".endif\n"

#define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...)	\
98
	__ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
99

100
#define ALTERNATIVE_CB(oldinstr, cb) \
101
	__ALTERNATIVE_CFG_CB(oldinstr, ARM64_CB_PATCH, 1, cb)
102 103
#else

104 105
#include <asm/assembler.h>

106 107 108 109 110 111 112 113
.macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
	.word \orig_offset - .
	.word \alt_offset - .
	.hword \feature
	.byte \orig_len
	.byte \alt_len
.endm

114 115
.macro alternative_insn insn1, insn2, cap, enable = 1
	.if \enable
116 117 118 119 120 121 122
661:	\insn1
662:	.pushsection .altinstructions, "a"
	altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
	.popsection
	.pushsection .altinstr_replacement, "ax"
663:	\insn2
664:	.popsection
123 124
	.org	. - (664b-663b) + (662b-661b)
	.org	. - (662b-661b) + (664b-663b)
125
	.endif
126 127
.endm

128
/*
129 130 131 132 133 134 135 136 137 138 139 140
 * Alternative sequences
 *
 * The code for the case where the capability is not present will be
 * assembled and linked as normal. There are no restrictions on this
 * code.
 *
 * The code for the case where the capability is present will be
 * assembled into a special section to be used for dynamic patching.
 * Code for that case must:
 *
 * 1. Be exactly the same length (in bytes) as the default code
 *    sequence.
141
 *
142 143 144 145 146 147 148
 * 2. Not contain a branch target that is used outside of the
 *    alternative sequence it is defined in (branches into an
 *    alternative sequence are not fixed up).
 */

/*
 * Begin an alternative code sequence.
149
 */
150
.macro alternative_if_not cap
151
	.set .Lasm_alt_mode, 0
152 153 154 155 156 157
	.pushsection .altinstructions, "a"
	altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
	.popsection
661:
.endm

158 159 160 161 162 163 164 165 166 167
.macro alternative_if cap
	.set .Lasm_alt_mode, 1
	.pushsection .altinstructions, "a"
	altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
	.popsection
	.pushsection .altinstr_replacement, "ax"
	.align 2	/* So GAS knows label 661 is suitably aligned */
661:
.endm

168 169 170 171 172 173 174 175
.macro alternative_cb cb
	.set .Lasm_alt_mode, 0
	.pushsection .altinstructions, "a"
	altinstruction_entry 661f, \cb, ARM64_CB_PATCH, 662f-661f, 0
	.popsection
661:
.endm

176
/*
177
 * Provide the other half of the alternative code sequence.
178
 */
179
.macro alternative_else
180 181 182 183 184 185
662:
	.if .Lasm_alt_mode==0
	.pushsection .altinstr_replacement, "ax"
	.else
	.popsection
	.endif
186 187 188 189 190 191
663:
.endm

/*
 * Complete an alternative code sequence.
 */
192
.macro alternative_endif
193 194 195 196
664:
	.if .Lasm_alt_mode==0
	.popsection
	.endif
197 198 199 200
	.org	. - (664b-663b) + (662b-661b)
	.org	. - (662b-661b) + (664b-663b)
.endm

201 202 203 204 205 206 207
/*
 * Callback-based alternative epilogue
 */
.macro alternative_cb_end
662:
.endm

208 209 210 211 212 213 214 215 216 217 218
/*
 * Provides a trivial alternative or default sequence consisting solely
 * of NOPs. The number of NOPs is chosen automatically to match the
 * previous case.
 */
.macro alternative_else_nop_endif
alternative_else
	nops	(662b-661b) / AARCH64_INSN_SIZE
alternative_endif
.endm

219 220 221
#define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...)	\
	alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)

222 223
.macro user_alt, label, oldinstr, newinstr, cond
9999:	alternative_insn "\oldinstr", "\newinstr", \cond
224
	_asm_extable 9999b, \label
225
.endm
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*
 * Generate the assembly for UAO alternatives with exception table entries.
 * This is complicated as there is no post-increment or pair versions of the
 * unprivileged instructions, and USER() only works for single instructions.
 */
#ifdef CONFIG_ARM64_UAO
	.macro uao_ldp l, reg1, reg2, addr, post_inc
		alternative_if_not ARM64_HAS_UAO
8888:			ldp	\reg1, \reg2, [\addr], \post_inc;
8889:			nop;
			nop;
		alternative_else
			ldtr	\reg1, [\addr];
			ldtr	\reg2, [\addr, #8];
			add	\addr, \addr, \post_inc;
		alternative_endif

244 245
		_asm_extable	8888b,\l;
		_asm_extable	8889b,\l;
246 247 248 249 250 251 252 253 254 255 256 257 258
	.endm

	.macro uao_stp l, reg1, reg2, addr, post_inc
		alternative_if_not ARM64_HAS_UAO
8888:			stp	\reg1, \reg2, [\addr], \post_inc;
8889:			nop;
			nop;
		alternative_else
			sttr	\reg1, [\addr];
			sttr	\reg2, [\addr, #8];
			add	\addr, \addr, \post_inc;
		alternative_endif

259 260
		_asm_extable	8888b,\l;
		_asm_extable	8889b,\l;
261 262 263 264 265 266 267 268 269 270 271
	.endm

	.macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
		alternative_if_not ARM64_HAS_UAO
8888:			\inst	\reg, [\addr], \post_inc;
			nop;
		alternative_else
			\alt_inst	\reg, [\addr];
			add		\addr, \addr, \post_inc;
		alternative_endif

272
		_asm_extable	8888b,\l;
273 274 275 276 277 278 279 280 281 282 283 284 285
	.endm
#else
	.macro uao_ldp l, reg1, reg2, addr, post_inc
		USER(\l, ldp \reg1, \reg2, [\addr], \post_inc)
	.endm
	.macro uao_stp l, reg1, reg2, addr, post_inc
		USER(\l, stp \reg1, \reg2, [\addr], \post_inc)
	.endm
	.macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
		USER(\l, \inst \reg, [\addr], \post_inc)
	.endm
#endif

286 287
#endif  /*  __ASSEMBLY__  */

288 289 290 291 292 293 294 295 296 297
/*
 * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature));
 *
 * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature, CONFIG_FOO));
 * N.B. If CONFIG_FOO is specified, but not selected, the whole block
 *      will be omitted, including oldinstr.
 */
#define ALTERNATIVE(oldinstr, newinstr, ...)   \
	_ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)

298
#endif /* __ASM_ALTERNATIVE_H */