xref: /linux/arch/s390/kernel/alternative.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/cpu.h>
4 #include <linux/smp.h>
5 #include <asm/text-patching.h>
6 #include <asm/alternative.h>
7 #include <asm/facility.h>
8 #include <asm/nospec-branch.h>
9 
10 static int __initdata_or_module alt_instr_disabled;
11 
12 static int __init disable_alternative_instructions(char *str)
13 {
14 	alt_instr_disabled = 1;
15 	return 0;
16 }
17 
18 early_param("noaltinstr", disable_alternative_instructions);
19 
20 static void __init_or_module __apply_alternatives(struct alt_instr *start,
21 						  struct alt_instr *end)
22 {
23 	struct alt_instr *a;
24 	u8 *instr, *replacement;
25 
26 	/*
27 	 * The scan order should be from start to end. A later scanned
28 	 * alternative code can overwrite previously scanned alternative code.
29 	 */
30 	for (a = start; a < end; a++) {
31 		instr = (u8 *)&a->instr_offset + a->instr_offset;
32 		replacement = (u8 *)&a->repl_offset + a->repl_offset;
33 
34 		if (!__test_facility(a->facility, alt_stfle_fac_list))
35 			continue;
36 		s390_kernel_write(instr, replacement, a->instrlen);
37 	}
38 }
39 
40 void __init_or_module apply_alternatives(struct alt_instr *start,
41 					 struct alt_instr *end)
42 {
43 	if (!alt_instr_disabled)
44 		__apply_alternatives(start, end);
45 }
46 
47 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
48 void __init apply_alternative_instructions(void)
49 {
50 	apply_alternatives(__alt_instructions, __alt_instructions_end);
51 }
52 
53 static void do_sync_core(void *info)
54 {
55 	sync_core();
56 }
57 
58 void text_poke_sync(void)
59 {
60 	on_each_cpu(do_sync_core, NULL, 1);
61 }
62 
63 void text_poke_sync_lock(void)
64 {
65 	cpus_read_lock();
66 	text_poke_sync();
67 	cpus_read_unlock();
68 }
69