1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <asm/alternative.h>
4 #include <asm/facility.h>
5 #include <asm/nospec-branch.h>
7 #define MAX_PATCH_LEN (255 - 1)
9 static int __initdata_or_module alt_instr_disabled;
11 static int __init disable_alternative_instructions(char *str)
13 alt_instr_disabled = 1;
17 early_param("noaltinstr", disable_alternative_instructions);
24 static u16 __initdata_or_module nop16 = 0x0700;
25 static u32 __initdata_or_module nop32 = 0x47000000;
26 static struct brcl_insn __initdata_or_module nop48 = {
30 static const void *nops[] __initdata_or_module = {
36 static void __init_or_module add_jump_padding(void *insns, unsigned int len)
38 struct brcl_insn brcl = {
43 memcpy(insns, &brcl, sizeof(brcl));
44 insns += sizeof(brcl);
48 memcpy(insns, &nop16, 2);
54 static void __init_or_module add_padding(void *insns, unsigned int len)
57 add_jump_padding(insns, len);
59 memcpy(insns, nops[len / 2 - 1], len);
62 static void __init_or_module __apply_alternatives(struct alt_instr *start,
63 struct alt_instr *end)
66 u8 *instr, *replacement;
67 u8 insnbuf[MAX_PATCH_LEN];
70 * The scan order should be from start to end. A later scanned
71 * alternative code can overwrite previously scanned alternative code.
73 for (a = start; a < end; a++) {
76 instr = (u8 *)&a->instr_offset + a->instr_offset;
77 replacement = (u8 *)&a->repl_offset + a->repl_offset;
79 if (!__test_facility(a->facility, alt_stfle_fac_list))
82 if (unlikely(a->instrlen % 2 || a->replacementlen % 2)) {
83 WARN_ONCE(1, "cpu alternatives instructions length is "
84 "odd, skipping patching\n");
88 memcpy(insnbuf, replacement, a->replacementlen);
89 insnbuf_sz = a->replacementlen;
91 if (a->instrlen > a->replacementlen) {
92 add_padding(insnbuf + a->replacementlen,
93 a->instrlen - a->replacementlen);
94 insnbuf_sz += a->instrlen - a->replacementlen;
97 s390_kernel_write(instr, insnbuf, insnbuf_sz);
101 void __init_or_module apply_alternatives(struct alt_instr *start,
102 struct alt_instr *end)
104 if (!alt_instr_disabled)
105 __apply_alternatives(start, end);
108 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
109 void __init apply_alternative_instructions(void)
111 apply_alternatives(__alt_instructions, __alt_instructions_end);