1 #include <linux/module.h>
2 #include <asm/alternative.h>
3 #include <asm/facility.h>
5 #define MAX_PATCH_LEN (255 - 1)
7 static int __initdata_or_module alt_instr_disabled
;
9 static int __init
disable_alternative_instructions(char *str
)
11 alt_instr_disabled
= 1;
15 early_param("noaltinstr", disable_alternative_instructions
);
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
= {
28 static const void *nops
[] __initdata_or_module
= {
34 static void __init_or_module
add_jump_padding(void *insns
, unsigned int len
)
36 struct brcl_insn brcl
= {
41 memcpy(insns
, &brcl
, sizeof(brcl
));
42 insns
+= sizeof(brcl
);
46 memcpy(insns
, &nop16
, 2);
52 static void __init_or_module
add_padding(void *insns
, unsigned int len
)
55 add_jump_padding(insns
, len
);
57 memcpy(insns
, nops
[len
/ 2 - 1], len
);
60 static void __init_or_module
__apply_alternatives(struct alt_instr
*start
,
61 struct alt_instr
*end
)
64 u8
*instr
, *replacement
;
65 u8 insnbuf
[MAX_PATCH_LEN
];
68 * The scan order should be from start to end. A later scanned
69 * alternative code can overwrite previously scanned alternative code.
71 for (a
= start
; a
< end
; a
++) {
74 instr
= (u8
*)&a
->instr_offset
+ a
->instr_offset
;
75 replacement
= (u8
*)&a
->repl_offset
+ a
->repl_offset
;
77 if (!test_facility(a
->facility
))
80 if (unlikely(a
->instrlen
% 2 || a
->replacementlen
% 2)) {
81 WARN_ONCE(1, "cpu alternatives instructions length is "
82 "odd, skipping patching\n");
86 memcpy(insnbuf
, replacement
, a
->replacementlen
);
87 insnbuf_sz
= a
->replacementlen
;
89 if (a
->instrlen
> a
->replacementlen
) {
90 add_padding(insnbuf
+ a
->replacementlen
,
91 a
->instrlen
- a
->replacementlen
);
92 insnbuf_sz
+= a
->instrlen
- a
->replacementlen
;
95 s390_kernel_write(instr
, insnbuf
, insnbuf_sz
);
99 void __init_or_module
apply_alternatives(struct alt_instr
*start
,
100 struct alt_instr
*end
)
102 if (!alt_instr_disabled
)
103 __apply_alternatives(start
, end
);
106 extern struct alt_instr __alt_instructions
[], __alt_instructions_end
[];
107 void __init
apply_alternative_instructions(void)
109 apply_alternatives(__alt_instructions
, __alt_instructions_end
);