xref: /openbmc/linux/arch/s390/kernel/alternative.c (revision 812f77b749a8ae11f58dacf0d3ed65e7ede47458)
1 #include <linux/module.h>
2 #include <asm/alternative.h>
3 #include <asm/facility.h>
4 
5 #define MAX_PATCH_LEN (255 - 1)
6 
7 static int __initdata_or_module alt_instr_disabled;
8 
9 static int __init disable_alternative_instructions(char *str)
10 {
11 	alt_instr_disabled = 1;
12 	return 0;
13 }
14 
15 early_param("noaltinstr", disable_alternative_instructions);
16 
17 struct brcl_insn {
18 	u16 opc;
19 	s32 disp;
20 } __packed;
21 
22 static u16 __initdata_or_module nop16 = 0x0700;
23 static u32 __initdata_or_module nop32 = 0x47000000;
24 static struct brcl_insn __initdata_or_module nop48 = {
25 	0xc004, 0
26 };
27 
28 static const void *nops[] __initdata_or_module = {
29 	&nop16,
30 	&nop32,
31 	&nop48
32 };
33 
34 static void __init_or_module add_jump_padding(void *insns, unsigned int len)
35 {
36 	struct brcl_insn brcl = {
37 		0xc0f4,
38 		len / 2
39 	};
40 
41 	memcpy(insns, &brcl, sizeof(brcl));
42 	insns += sizeof(brcl);
43 	len -= sizeof(brcl);
44 
45 	while (len > 0) {
46 		memcpy(insns, &nop16, 2);
47 		insns += 2;
48 		len -= 2;
49 	}
50 }
51 
52 static void __init_or_module add_padding(void *insns, unsigned int len)
53 {
54 	if (len > 6)
55 		add_jump_padding(insns, len);
56 	else if (len >= 2)
57 		memcpy(insns, nops[len / 2 - 1], len);
58 }
59 
60 static void __init_or_module __apply_alternatives(struct alt_instr *start,
61 						  struct alt_instr *end)
62 {
63 	struct alt_instr *a;
64 	u8 *instr, *replacement;
65 	u8 insnbuf[MAX_PATCH_LEN];
66 
67 	/*
68 	 * The scan order should be from start to end. A later scanned
69 	 * alternative code can overwrite previously scanned alternative code.
70 	 */
71 	for (a = start; a < end; a++) {
72 		int insnbuf_sz = 0;
73 
74 		instr = (u8 *)&a->instr_offset + a->instr_offset;
75 		replacement = (u8 *)&a->repl_offset + a->repl_offset;
76 
77 		if (!test_facility(a->facility))
78 			continue;
79 
80 		if (unlikely(a->instrlen % 2 || a->replacementlen % 2)) {
81 			WARN_ONCE(1, "cpu alternatives instructions length is "
82 				     "odd, skipping patching\n");
83 			continue;
84 		}
85 
86 		memcpy(insnbuf, replacement, a->replacementlen);
87 		insnbuf_sz = a->replacementlen;
88 
89 		if (a->instrlen > a->replacementlen) {
90 			add_padding(insnbuf + a->replacementlen,
91 				    a->instrlen - a->replacementlen);
92 			insnbuf_sz += a->instrlen - a->replacementlen;
93 		}
94 
95 		s390_kernel_write(instr, insnbuf, insnbuf_sz);
96 	}
97 }
98 
99 void __init_or_module apply_alternatives(struct alt_instr *start,
100 					 struct alt_instr *end)
101 {
102 	if (!alt_instr_disabled)
103 		__apply_alternatives(start, end);
104 }
105 
106 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
107 void __init apply_alternative_instructions(void)
108 {
109 	apply_alternatives(__alt_instructions, __alt_instructions_end);
110 }
111